aboutsummaryrefslogtreecommitdiffstats
path: root/arch/s390
diff options
context:
space:
mode:
authorJan Glauber <jang@linux.vnet.ibm.com>2012-11-29 08:33:30 -0500
committerMartin Schwidefsky <schwidefsky@de.ibm.com>2012-11-30 11:47:23 -0500
commit828b35f60eb0148f994bb13e328df94578b07142 (patch)
tree2e06065339ebbfb9aab46f3167fe77ac952ac6fa /arch/s390
parent9a4da8a5b109906a64bed5aaeb83bf4edb1f5888 (diff)
s390/pci: DMA support
Add DMA IOMMU support using 4K page table entries. Implement dma_map_ops. Signed-off-by: Jan Glauber <jang@linux.vnet.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
Diffstat (limited to 'arch/s390')
-rw-r--r--arch/s390/include/asm/dma-mapping.h76
-rw-r--r--arch/s390/include/asm/dma.h19
-rw-r--r--arch/s390/include/asm/pci.h21
-rw-r--r--arch/s390/include/asm/pci_dma.h196
-rw-r--r--arch/s390/pci/Makefile2
-rw-r--r--arch/s390/pci/pci.c36
-rw-r--r--arch/s390/pci/pci_clp.c4
-rw-r--r--arch/s390/pci/pci_dma.c505
8 files changed, 848 insertions, 11 deletions
diff --git a/arch/s390/include/asm/dma-mapping.h b/arch/s390/include/asm/dma-mapping.h
new file mode 100644
index 000000000000..8a32f7dfd3af
--- /dev/null
+++ b/arch/s390/include/asm/dma-mapping.h
@@ -0,0 +1,76 @@
1#ifndef _ASM_S390_DMA_MAPPING_H
2#define _ASM_S390_DMA_MAPPING_H
3
4#include <linux/kernel.h>
5#include <linux/types.h>
6#include <linux/mm.h>
7#include <linux/scatterlist.h>
8#include <linux/dma-attrs.h>
9#include <linux/dma-debug.h>
10#include <linux/io.h>
11
12#define DMA_ERROR_CODE (~(dma_addr_t) 0x0)
13
14extern struct dma_map_ops s390_dma_ops;
15
16static inline struct dma_map_ops *get_dma_ops(struct device *dev)
17{
18 return &s390_dma_ops;
19}
20
21extern int dma_set_mask(struct device *dev, u64 mask);
22extern int dma_is_consistent(struct device *dev, dma_addr_t dma_handle);
23extern void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
24 enum dma_data_direction direction);
25
26#define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
27#define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
28
29#include <asm-generic/dma-mapping-common.h>
30
31static inline int dma_supported(struct device *dev, u64 mask)
32{
33 struct dma_map_ops *dma_ops = get_dma_ops(dev);
34
35 if (dma_ops->dma_supported == NULL)
36 return 1;
37 return dma_ops->dma_supported(dev, mask);
38}
39
40static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
41{
42 if (!dev->dma_mask)
43 return 0;
44 return addr + size - 1 <= *dev->dma_mask;
45}
46
47static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
48{
49 struct dma_map_ops *dma_ops = get_dma_ops(dev);
50
51 if (dma_ops->mapping_error)
52 return dma_ops->mapping_error(dev, dma_addr);
53 return (dma_addr == 0UL);
54}
55
56static inline void *dma_alloc_coherent(struct device *dev, size_t size,
57 dma_addr_t *dma_handle, gfp_t flag)
58{
59 struct dma_map_ops *ops = get_dma_ops(dev);
60 void *ret;
61
62 ret = ops->alloc(dev, size, dma_handle, flag, NULL);
63 debug_dma_alloc_coherent(dev, size, *dma_handle, ret);
64 return ret;
65}
66
67static inline void dma_free_coherent(struct device *dev, size_t size,
68 void *cpu_addr, dma_addr_t dma_handle)
69{
70 struct dma_map_ops *dma_ops = get_dma_ops(dev);
71
72 dma_ops->free(dev, size, cpu_addr, dma_handle, NULL);
73 debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
74}
75
76#endif /* _ASM_S390_DMA_MAPPING_H */
diff --git a/arch/s390/include/asm/dma.h b/arch/s390/include/asm/dma.h
index 6fb6de4f15b0..de015d85e3e5 100644
--- a/arch/s390/include/asm/dma.h
+++ b/arch/s390/include/asm/dma.h
@@ -1,14 +1,13 @@
1/* 1#ifndef _ASM_S390_DMA_H
2 * S390 version 2#define _ASM_S390_DMA_H
3 */
4
5#ifndef _ASM_DMA_H
6#define _ASM_DMA_H
7 3
8#include <asm/io.h> /* need byte IO */ 4#include <asm/io.h>
9 5
6/*
7 * MAX_DMA_ADDRESS is ambiguous because on s390 its completely unrelated
8 * to DMA. It _is_ used for the s390 memory zone split at 2GB caused
9 * by the 31 bit heritage.
10 */
10#define MAX_DMA_ADDRESS 0x80000000 11#define MAX_DMA_ADDRESS 0x80000000
11 12
12#define free_dma(x) do { } while (0) 13#endif /* _ASM_S390_DMA_H */
13
14#endif /* _ASM_DMA_H */
diff --git a/arch/s390/include/asm/pci.h b/arch/s390/include/asm/pci.h
index 2a6084fa4b1a..e9dc0090ac43 100644
--- a/arch/s390/include/asm/pci.h
+++ b/arch/s390/include/asm/pci.h
@@ -75,8 +75,23 @@ struct zpci_dev {
75 struct msi_map *msi_map[ZPCI_NR_MSI_VECS]; 75 struct msi_map *msi_map[ZPCI_NR_MSI_VECS];
76 unsigned int aisb; /* number of the summary bit */ 76 unsigned int aisb; /* number of the summary bit */
77 77
78 /* DMA stuff */
79 unsigned long *dma_table;
80 spinlock_t dma_table_lock;
81 int tlb_refresh;
82
83 spinlock_t iommu_bitmap_lock;
84 unsigned long *iommu_bitmap;
85 unsigned long iommu_size;
86 unsigned long iommu_pages;
87 unsigned int next_bit;
88
78 struct zpci_bar_struct bars[PCI_BAR_COUNT]; 89 struct zpci_bar_struct bars[PCI_BAR_COUNT];
79 90
91 u64 start_dma; /* Start of available DMA addresses */
92 u64 end_dma; /* End of available DMA addresses */
93 u64 dma_mask; /* DMA address space mask */
94
80 enum pci_bus_speed max_bus_speed; 95 enum pci_bus_speed max_bus_speed;
81}; 96};
82 97
@@ -95,6 +110,8 @@ int zpci_enable_device(struct zpci_dev *);
95void zpci_stop_device(struct zpci_dev *); 110void zpci_stop_device(struct zpci_dev *);
96void zpci_free_device(struct zpci_dev *); 111void zpci_free_device(struct zpci_dev *);
97int zpci_scan_device(struct zpci_dev *); 112int zpci_scan_device(struct zpci_dev *);
113int zpci_register_ioat(struct zpci_dev *, u8, u64, u64, u64);
114int zpci_unregister_ioat(struct zpci_dev *, u8);
98 115
99/* CLP */ 116/* CLP */
100int clp_find_pci_devices(void); 117int clp_find_pci_devices(void);
@@ -115,4 +132,8 @@ struct zpci_dev *get_zdev(struct pci_dev *);
115struct zpci_dev *get_zdev_by_fid(u32); 132struct zpci_dev *get_zdev_by_fid(u32);
116bool zpci_fid_present(u32); 133bool zpci_fid_present(u32);
117 134
135/* DMA */
136int zpci_dma_init(void);
137void zpci_dma_exit(void);
138
118#endif 139#endif
diff --git a/arch/s390/include/asm/pci_dma.h b/arch/s390/include/asm/pci_dma.h
new file mode 100644
index 000000000000..30b4c179c38c
--- /dev/null
+++ b/arch/s390/include/asm/pci_dma.h
@@ -0,0 +1,196 @@
1#ifndef _ASM_S390_PCI_DMA_H
2#define _ASM_S390_PCI_DMA_H
3
4/* I/O Translation Anchor (IOTA) */
5enum zpci_ioat_dtype {
6 ZPCI_IOTA_STO = 0,
7 ZPCI_IOTA_RTTO = 1,
8 ZPCI_IOTA_RSTO = 2,
9 ZPCI_IOTA_RFTO = 3,
10 ZPCI_IOTA_PFAA = 4,
11 ZPCI_IOTA_IOPFAA = 5,
12 ZPCI_IOTA_IOPTO = 7
13};
14
15#define ZPCI_IOTA_IOT_ENABLED 0x800UL
16#define ZPCI_IOTA_DT_ST (ZPCI_IOTA_STO << 2)
17#define ZPCI_IOTA_DT_RT (ZPCI_IOTA_RTTO << 2)
18#define ZPCI_IOTA_DT_RS (ZPCI_IOTA_RSTO << 2)
19#define ZPCI_IOTA_DT_RF (ZPCI_IOTA_RFTO << 2)
20#define ZPCI_IOTA_DT_PF (ZPCI_IOTA_PFAA << 2)
21#define ZPCI_IOTA_FS_4K 0
22#define ZPCI_IOTA_FS_1M 1
23#define ZPCI_IOTA_FS_2G 2
24#define ZPCI_KEY (PAGE_DEFAULT_KEY << 5)
25
26#define ZPCI_IOTA_STO_FLAG (ZPCI_IOTA_IOT_ENABLED | ZPCI_KEY | ZPCI_IOTA_DT_ST)
27#define ZPCI_IOTA_RTTO_FLAG (ZPCI_IOTA_IOT_ENABLED | ZPCI_KEY | ZPCI_IOTA_DT_RT)
28#define ZPCI_IOTA_RSTO_FLAG (ZPCI_IOTA_IOT_ENABLED | ZPCI_KEY | ZPCI_IOTA_DT_RS)
29#define ZPCI_IOTA_RFTO_FLAG (ZPCI_IOTA_IOT_ENABLED | ZPCI_KEY | ZPCI_IOTA_DT_RF)
30#define ZPCI_IOTA_RFAA_FLAG (ZPCI_IOTA_IOT_ENABLED | ZPCI_KEY | ZPCI_IOTA_DT_PF | ZPCI_IOTA_FS_2G)
31
32/* I/O Region and segment tables */
33#define ZPCI_INDEX_MASK 0x7ffUL
34
35#define ZPCI_TABLE_TYPE_MASK 0xc
36#define ZPCI_TABLE_TYPE_RFX 0xc
37#define ZPCI_TABLE_TYPE_RSX 0x8
38#define ZPCI_TABLE_TYPE_RTX 0x4
39#define ZPCI_TABLE_TYPE_SX 0x0
40
41#define ZPCI_TABLE_LEN_RFX 0x3
42#define ZPCI_TABLE_LEN_RSX 0x3
43#define ZPCI_TABLE_LEN_RTX 0x3
44
45#define ZPCI_TABLE_OFFSET_MASK 0xc0
46#define ZPCI_TABLE_SIZE 0x4000
47#define ZPCI_TABLE_ALIGN ZPCI_TABLE_SIZE
48#define ZPCI_TABLE_ENTRY_SIZE (sizeof(unsigned long))
49#define ZPCI_TABLE_ENTRIES (ZPCI_TABLE_SIZE / ZPCI_TABLE_ENTRY_SIZE)
50
51#define ZPCI_TABLE_BITS 11
52#define ZPCI_PT_BITS 8
53#define ZPCI_ST_SHIFT (ZPCI_PT_BITS + PAGE_SHIFT)
54#define ZPCI_RT_SHIFT (ZPCI_ST_SHIFT + ZPCI_TABLE_BITS)
55
56#define ZPCI_RTE_FLAG_MASK 0x3fffUL
57#define ZPCI_RTE_ADDR_MASK (~ZPCI_RTE_FLAG_MASK)
58#define ZPCI_STE_FLAG_MASK 0x7ffUL
59#define ZPCI_STE_ADDR_MASK (~ZPCI_STE_FLAG_MASK)
60
61/* I/O Page tables */
62#define ZPCI_PTE_VALID_MASK 0x400
63#define ZPCI_PTE_INVALID 0x400
64#define ZPCI_PTE_VALID 0x000
65#define ZPCI_PT_SIZE 0x800
66#define ZPCI_PT_ALIGN ZPCI_PT_SIZE
67#define ZPCI_PT_ENTRIES (ZPCI_PT_SIZE / ZPCI_TABLE_ENTRY_SIZE)
68#define ZPCI_PT_MASK (ZPCI_PT_ENTRIES - 1)
69
70#define ZPCI_PTE_FLAG_MASK 0xfffUL
71#define ZPCI_PTE_ADDR_MASK (~ZPCI_PTE_FLAG_MASK)
72
73/* Shared bits */
74#define ZPCI_TABLE_VALID 0x00
75#define ZPCI_TABLE_INVALID 0x20
76#define ZPCI_TABLE_PROTECTED 0x200
77#define ZPCI_TABLE_UNPROTECTED 0x000
78
79#define ZPCI_TABLE_VALID_MASK 0x20
80#define ZPCI_TABLE_PROT_MASK 0x200
81
82static inline unsigned int calc_rtx(dma_addr_t ptr)
83{
84 return ((unsigned long) ptr >> ZPCI_RT_SHIFT) & ZPCI_INDEX_MASK;
85}
86
87static inline unsigned int calc_sx(dma_addr_t ptr)
88{
89 return ((unsigned long) ptr >> ZPCI_ST_SHIFT) & ZPCI_INDEX_MASK;
90}
91
92static inline unsigned int calc_px(dma_addr_t ptr)
93{
94 return ((unsigned long) ptr >> PAGE_SHIFT) & ZPCI_PT_MASK;
95}
96
97static inline void set_pt_pfaa(unsigned long *entry, void *pfaa)
98{
99 *entry &= ZPCI_PTE_FLAG_MASK;
100 *entry |= ((unsigned long) pfaa & ZPCI_PTE_ADDR_MASK);
101}
102
103static inline void set_rt_sto(unsigned long *entry, void *sto)
104{
105 *entry &= ZPCI_RTE_FLAG_MASK;
106 *entry |= ((unsigned long) sto & ZPCI_RTE_ADDR_MASK);
107 *entry |= ZPCI_TABLE_TYPE_RTX;
108}
109
110static inline void set_st_pto(unsigned long *entry, void *pto)
111{
112 *entry &= ZPCI_STE_FLAG_MASK;
113 *entry |= ((unsigned long) pto & ZPCI_STE_ADDR_MASK);
114 *entry |= ZPCI_TABLE_TYPE_SX;
115}
116
117static inline void validate_rt_entry(unsigned long *entry)
118{
119 *entry &= ~ZPCI_TABLE_VALID_MASK;
120 *entry &= ~ZPCI_TABLE_OFFSET_MASK;
121 *entry |= ZPCI_TABLE_VALID;
122 *entry |= ZPCI_TABLE_LEN_RTX;
123}
124
125static inline void validate_st_entry(unsigned long *entry)
126{
127 *entry &= ~ZPCI_TABLE_VALID_MASK;
128 *entry |= ZPCI_TABLE_VALID;
129}
130
131static inline void invalidate_table_entry(unsigned long *entry)
132{
133 *entry &= ~ZPCI_TABLE_VALID_MASK;
134 *entry |= ZPCI_TABLE_INVALID;
135}
136
137static inline void invalidate_pt_entry(unsigned long *entry)
138{
139 WARN_ON_ONCE((*entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_INVALID);
140 *entry &= ~ZPCI_PTE_VALID_MASK;
141 *entry |= ZPCI_PTE_INVALID;
142}
143
144static inline void validate_pt_entry(unsigned long *entry)
145{
146 WARN_ON_ONCE((*entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID);
147 *entry &= ~ZPCI_PTE_VALID_MASK;
148 *entry |= ZPCI_PTE_VALID;
149}
150
151static inline void entry_set_protected(unsigned long *entry)
152{
153 *entry &= ~ZPCI_TABLE_PROT_MASK;
154 *entry |= ZPCI_TABLE_PROTECTED;
155}
156
157static inline void entry_clr_protected(unsigned long *entry)
158{
159 *entry &= ~ZPCI_TABLE_PROT_MASK;
160 *entry |= ZPCI_TABLE_UNPROTECTED;
161}
162
163static inline int reg_entry_isvalid(unsigned long entry)
164{
165 return (entry & ZPCI_TABLE_VALID_MASK) == ZPCI_TABLE_VALID;
166}
167
168static inline int pt_entry_isvalid(unsigned long entry)
169{
170 return (entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID;
171}
172
173static inline int entry_isprotected(unsigned long entry)
174{
175 return (entry & ZPCI_TABLE_PROT_MASK) == ZPCI_TABLE_PROTECTED;
176}
177
178static inline unsigned long *get_rt_sto(unsigned long entry)
179{
180 return ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_RTX)
181 ? (unsigned long *) (entry & ZPCI_RTE_ADDR_MASK)
182 : NULL;
183}
184
185static inline unsigned long *get_st_pto(unsigned long entry)
186{
187 return ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_SX)
188 ? (unsigned long *) (entry & ZPCI_STE_ADDR_MASK)
189 : NULL;
190}
191
192/* Prototypes */
193int zpci_dma_init_device(struct zpci_dev *);
194void zpci_dma_exit_device(struct zpci_dev *);
195
196#endif
diff --git a/arch/s390/pci/Makefile b/arch/s390/pci/Makefile
index 628be7bc006c..4590596d8b25 100644
--- a/arch/s390/pci/Makefile
+++ b/arch/s390/pci/Makefile
@@ -2,4 +2,4 @@
2# Makefile for the s390 PCI subsystem. 2# Makefile for the s390 PCI subsystem.
3# 3#
4 4
5obj-$(CONFIG_PCI) += pci.o pci_clp.o pci_msi.o 5obj-$(CONFIG_PCI) += pci.o pci_dma.o pci_clp.o pci_msi.o
diff --git a/arch/s390/pci/pci.c b/arch/s390/pci/pci.c
index d11dc8a25f34..5a2ef9e75c97 100644
--- a/arch/s390/pci/pci.c
+++ b/arch/s390/pci/pci.c
@@ -34,6 +34,7 @@
34#include <asm/facility.h> 34#include <asm/facility.h>
35#include <asm/pci_insn.h> 35#include <asm/pci_insn.h>
36#include <asm/pci_clp.h> 36#include <asm/pci_clp.h>
37#include <asm/pci_dma.h>
37 38
38#define DEBUG /* enable pr_debug */ 39#define DEBUG /* enable pr_debug */
39 40
@@ -232,6 +233,25 @@ static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args
232 return rc; 233 return rc;
233} 234}
234 235
236/* Modify PCI: Register I/O address translation parameters */
237int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
238 u64 base, u64 limit, u64 iota)
239{
240 struct mod_pci_args args = { base, limit, iota };
241
242 WARN_ON_ONCE(iota & 0x3fff);
243 args.iota |= ZPCI_IOTA_RTTO_FLAG;
244 return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args);
245}
246
247/* Modify PCI: Unregister I/O address translation parameters */
248int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
249{
250 struct mod_pci_args args = { 0, 0, 0 };
251
252 return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args);
253}
254
235/* Modify PCI: Unregister adapter interruptions */ 255/* Modify PCI: Unregister adapter interruptions */
236static int zpci_unregister_airq(struct zpci_dev *zdev) 256static int zpci_unregister_airq(struct zpci_dev *zdev)
237{ 257{
@@ -602,6 +622,7 @@ static void zpci_remove_device(struct pci_dev *pdev)
602 622
603 dev_info(&pdev->dev, "Removing device %u\n", zdev->domain); 623 dev_info(&pdev->dev, "Removing device %u\n", zdev->domain);
604 zdev->state = ZPCI_FN_STATE_CONFIGURED; 624 zdev->state = ZPCI_FN_STATE_CONFIGURED;
625 zpci_dma_exit_device(zdev);
605 zpci_unmap_resources(pdev); 626 zpci_unmap_resources(pdev);
606 list_del(&zdev->entry); /* can be called from init */ 627 list_del(&zdev->entry); /* can be called from init */
607 zdev->pdev = NULL; 628 zdev->pdev = NULL;
@@ -887,7 +908,14 @@ int zpci_enable_device(struct zpci_dev *zdev)
887 if (rc) 908 if (rc)
888 goto out; 909 goto out;
889 pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid); 910 pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid);
911
912 rc = zpci_dma_init_device(zdev);
913 if (rc)
914 goto out_dma;
890 return 0; 915 return 0;
916
917out_dma:
918 clp_disable_fh(zdev);
891out: 919out:
892 return rc; 920 return rc;
893} 921}
@@ -929,6 +957,7 @@ out:
929 957
930void zpci_stop_device(struct zpci_dev *zdev) 958void zpci_stop_device(struct zpci_dev *zdev)
931{ 959{
960 zpci_dma_exit_device(zdev);
932 /* 961 /*
933 * Note: SCLP disables fh via set-pci-fn so don't 962 * Note: SCLP disables fh via set-pci-fn so don't
934 * do that here. 963 * do that here.
@@ -953,6 +982,7 @@ int zpci_scan_device(struct zpci_dev *zdev)
953 return 0; 982 return 0;
954 983
955out: 984out:
985 zpci_dma_exit_device(zdev);
956 clp_disable_fh(zdev); 986 clp_disable_fh(zdev);
957 return -EIO; 987 return -EIO;
958} 988}
@@ -1028,6 +1058,10 @@ static int __init pci_base_init(void)
1028 if (rc) 1058 if (rc)
1029 goto out_irq; 1059 goto out_irq;
1030 1060
1061 rc = zpci_dma_init();
1062 if (rc)
1063 goto out_dma;
1064
1031 rc = clp_find_pci_devices(); 1065 rc = clp_find_pci_devices();
1032 if (rc) 1066 if (rc)
1033 goto out_find; 1067 goto out_find;
@@ -1036,6 +1070,8 @@ static int __init pci_base_init(void)
1036 return 0; 1070 return 0;
1037 1071
1038out_find: 1072out_find:
1073 zpci_dma_exit();
1074out_dma:
1039 zpci_irq_exit(); 1075 zpci_irq_exit();
1040out_irq: 1076out_irq:
1041 zpci_msihash_exit(); 1077 zpci_msihash_exit();
diff --git a/arch/s390/pci/pci_clp.c b/arch/s390/pci/pci_clp.c
index 72694fb6d525..7f4ce8d874a4 100644
--- a/arch/s390/pci/pci_clp.c
+++ b/arch/s390/pci/pci_clp.c
@@ -48,6 +48,8 @@ static void clp_free_block(void *ptr)
48static void clp_store_query_pci_fngrp(struct zpci_dev *zdev, 48static void clp_store_query_pci_fngrp(struct zpci_dev *zdev,
49 struct clp_rsp_query_pci_grp *response) 49 struct clp_rsp_query_pci_grp *response)
50{ 50{
51 zdev->tlb_refresh = response->refresh;
52 zdev->dma_mask = response->dasm;
51 zdev->msi_addr = response->msia; 53 zdev->msi_addr = response->msia;
52 54
53 pr_debug("Supported number of MSI vectors: %u\n", response->noi); 55 pr_debug("Supported number of MSI vectors: %u\n", response->noi);
@@ -97,6 +99,8 @@ static int clp_store_query_pci_fn(struct zpci_dev *zdev,
97 zdev->bars[i].val = le32_to_cpu(response->bar[i]); 99 zdev->bars[i].val = le32_to_cpu(response->bar[i]);
98 zdev->bars[i].size = response->bar_size[i]; 100 zdev->bars[i].size = response->bar_size[i];
99 } 101 }
102 zdev->start_dma = response->sdma;
103 zdev->end_dma = response->edma;
100 zdev->pchid = response->pchid; 104 zdev->pchid = response->pchid;
101 zdev->pfgid = response->pfgid; 105 zdev->pfgid = response->pfgid;
102 return 0; 106 return 0;
diff --git a/arch/s390/pci/pci_dma.c b/arch/s390/pci/pci_dma.c
new file mode 100644
index 000000000000..de48625b3590
--- /dev/null
+++ b/arch/s390/pci/pci_dma.c
@@ -0,0 +1,505 @@
1/*
2 * Copyright IBM Corp. 2012
3 *
4 * Author(s):
5 * Jan Glauber <jang@linux.vnet.ibm.com>
6 */
7
8#include <linux/kernel.h>
9#include <linux/slab.h>
10#include <linux/export.h>
11#include <linux/iommu-helper.h>
12#include <linux/dma-mapping.h>
13#include <linux/pci.h>
14#include <asm/pci_dma.h>
15
16static enum zpci_ioat_dtype zpci_ioat_dt = ZPCI_IOTA_RTTO;
17
18static struct kmem_cache *dma_region_table_cache;
19static struct kmem_cache *dma_page_table_cache;
20
21static unsigned long *dma_alloc_cpu_table(void)
22{
23 unsigned long *table, *entry;
24
25 table = kmem_cache_alloc(dma_region_table_cache, GFP_ATOMIC);
26 if (!table)
27 return NULL;
28
29 for (entry = table; entry < table + ZPCI_TABLE_ENTRIES; entry++)
30 *entry = ZPCI_TABLE_INVALID | ZPCI_TABLE_PROTECTED;
31 return table;
32}
33
34static void dma_free_cpu_table(void *table)
35{
36 kmem_cache_free(dma_region_table_cache, table);
37}
38
39static unsigned long *dma_alloc_page_table(void)
40{
41 unsigned long *table, *entry;
42
43 table = kmem_cache_alloc(dma_page_table_cache, GFP_ATOMIC);
44 if (!table)
45 return NULL;
46
47 for (entry = table; entry < table + ZPCI_PT_ENTRIES; entry++)
48 *entry = ZPCI_PTE_INVALID | ZPCI_TABLE_PROTECTED;
49 return table;
50}
51
52static void dma_free_page_table(void *table)
53{
54 kmem_cache_free(dma_page_table_cache, table);
55}
56
57static unsigned long *dma_get_seg_table_origin(unsigned long *entry)
58{
59 unsigned long *sto;
60
61 if (reg_entry_isvalid(*entry))
62 sto = get_rt_sto(*entry);
63 else {
64 sto = dma_alloc_cpu_table();
65 if (!sto)
66 return NULL;
67
68 set_rt_sto(entry, sto);
69 validate_rt_entry(entry);
70 entry_clr_protected(entry);
71 }
72 return sto;
73}
74
75static unsigned long *dma_get_page_table_origin(unsigned long *entry)
76{
77 unsigned long *pto;
78
79 if (reg_entry_isvalid(*entry))
80 pto = get_st_pto(*entry);
81 else {
82 pto = dma_alloc_page_table();
83 if (!pto)
84 return NULL;
85 set_st_pto(entry, pto);
86 validate_st_entry(entry);
87 entry_clr_protected(entry);
88 }
89 return pto;
90}
91
92static unsigned long *dma_walk_cpu_trans(unsigned long *rto, dma_addr_t dma_addr)
93{
94 unsigned long *sto, *pto;
95 unsigned int rtx, sx, px;
96
97 rtx = calc_rtx(dma_addr);
98 sto = dma_get_seg_table_origin(&rto[rtx]);
99 if (!sto)
100 return NULL;
101
102 sx = calc_sx(dma_addr);
103 pto = dma_get_page_table_origin(&sto[sx]);
104 if (!pto)
105 return NULL;
106
107 px = calc_px(dma_addr);
108 return &pto[px];
109}
110
111static void dma_update_cpu_trans(struct zpci_dev *zdev, void *page_addr,
112 dma_addr_t dma_addr, int flags)
113{
114 unsigned long *entry;
115
116 entry = dma_walk_cpu_trans(zdev->dma_table, dma_addr);
117 if (!entry) {
118 WARN_ON_ONCE(1);
119 return;
120 }
121
122 if (flags & ZPCI_PTE_INVALID) {
123 invalidate_pt_entry(entry);
124 return;
125 } else {
126 set_pt_pfaa(entry, page_addr);
127 validate_pt_entry(entry);
128 }
129
130 if (flags & ZPCI_TABLE_PROTECTED)
131 entry_set_protected(entry);
132 else
133 entry_clr_protected(entry);
134}
135
136static int dma_update_trans(struct zpci_dev *zdev, unsigned long pa,
137 dma_addr_t dma_addr, size_t size, int flags)
138{
139 unsigned int nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
140 u8 *page_addr = (u8 *) (pa & PAGE_MASK);
141 dma_addr_t start_dma_addr = dma_addr;
142 unsigned long irq_flags;
143 int i, rc = 0;
144
145 if (!nr_pages)
146 return -EINVAL;
147
148 spin_lock_irqsave(&zdev->dma_table_lock, irq_flags);
149 if (!zdev->dma_table) {
150 dev_err(&zdev->pdev->dev, "Missing DMA table\n");
151 goto no_refresh;
152 }
153
154 for (i = 0; i < nr_pages; i++) {
155 dma_update_cpu_trans(zdev, page_addr, dma_addr, flags);
156 page_addr += PAGE_SIZE;
157 dma_addr += PAGE_SIZE;
158 }
159
160 /*
161 * rpcit is not required to establish new translations when previously
162 * invalid translation-table entries are validated, however it is
163 * required when altering previously valid entries.
164 */
165 if (!zdev->tlb_refresh &&
166 ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID))
167 /*
168 * TODO: also need to check that the old entry is indeed INVALID
169 * and not only for one page but for the whole range...
170 * -> now we WARN_ON in that case but with lazy unmap that
171 * needs to be redone!
172 */
173 goto no_refresh;
174 rc = rpcit_instr((u64) zdev->fh << 32, start_dma_addr,
175 nr_pages * PAGE_SIZE);
176
177no_refresh:
178 spin_unlock_irqrestore(&zdev->dma_table_lock, irq_flags);
179 return rc;
180}
181
182static void dma_free_seg_table(unsigned long entry)
183{
184 unsigned long *sto = get_rt_sto(entry);
185 int sx;
186
187 for (sx = 0; sx < ZPCI_TABLE_ENTRIES; sx++)
188 if (reg_entry_isvalid(sto[sx]))
189 dma_free_page_table(get_st_pto(sto[sx]));
190
191 dma_free_cpu_table(sto);
192}
193
194static void dma_cleanup_tables(struct zpci_dev *zdev)
195{
196 unsigned long *table = zdev->dma_table;
197 int rtx;
198
199 if (!zdev || !zdev->dma_table)
200 return;
201
202 for (rtx = 0; rtx < ZPCI_TABLE_ENTRIES; rtx++)
203 if (reg_entry_isvalid(table[rtx]))
204 dma_free_seg_table(table[rtx]);
205
206 dma_free_cpu_table(table);
207 zdev->dma_table = NULL;
208}
209
210static unsigned long __dma_alloc_iommu(struct zpci_dev *zdev, unsigned long start,
211 int size)
212{
213 unsigned long boundary_size = 0x1000000;
214
215 return iommu_area_alloc(zdev->iommu_bitmap, zdev->iommu_pages,
216 start, size, 0, boundary_size, 0);
217}
218
219static unsigned long dma_alloc_iommu(struct zpci_dev *zdev, int size)
220{
221 unsigned long offset, flags;
222
223 spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags);
224 offset = __dma_alloc_iommu(zdev, zdev->next_bit, size);
225 if (offset == -1)
226 offset = __dma_alloc_iommu(zdev, 0, size);
227
228 if (offset != -1) {
229 zdev->next_bit = offset + size;
230 if (zdev->next_bit >= zdev->iommu_pages)
231 zdev->next_bit = 0;
232 }
233 spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);
234 return offset;
235}
236
237static void dma_free_iommu(struct zpci_dev *zdev, unsigned long offset, int size)
238{
239 unsigned long flags;
240
241 spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags);
242 if (!zdev->iommu_bitmap)
243 goto out;
244 bitmap_clear(zdev->iommu_bitmap, offset, size);
245 if (offset >= zdev->next_bit)
246 zdev->next_bit = offset + size;
247out:
248 spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);
249}
250
251int dma_set_mask(struct device *dev, u64 mask)
252{
253 if (!dev->dma_mask || !dma_supported(dev, mask))
254 return -EIO;
255
256 *dev->dma_mask = mask;
257 return 0;
258}
259EXPORT_SYMBOL_GPL(dma_set_mask);
260
261static dma_addr_t s390_dma_map_pages(struct device *dev, struct page *page,
262 unsigned long offset, size_t size,
263 enum dma_data_direction direction,
264 struct dma_attrs *attrs)
265{
266 struct zpci_dev *zdev = get_zdev(container_of(dev, struct pci_dev, dev));
267 unsigned long nr_pages, iommu_page_index;
268 unsigned long pa = page_to_phys(page) + offset;
269 int flags = ZPCI_PTE_VALID;
270 dma_addr_t dma_addr;
271
272 WARN_ON_ONCE(offset > PAGE_SIZE);
273
274 /* This rounds up number of pages based on size and offset */
275 nr_pages = iommu_num_pages(pa, size, PAGE_SIZE);
276 iommu_page_index = dma_alloc_iommu(zdev, nr_pages);
277 if (iommu_page_index == -1)
278 goto out_err;
279
280 /* Use rounded up size */
281 size = nr_pages * PAGE_SIZE;
282
283 dma_addr = zdev->start_dma + iommu_page_index * PAGE_SIZE;
284 if (dma_addr + size > zdev->end_dma) {
285 dev_err(dev, "(dma_addr: 0x%16.16LX + size: 0x%16.16lx) > end_dma: 0x%16.16Lx\n",
286 dma_addr, size, zdev->end_dma);
287 goto out_free;
288 }
289
290 if (direction == DMA_NONE || direction == DMA_TO_DEVICE)
291 flags |= ZPCI_TABLE_PROTECTED;
292
293 if (!dma_update_trans(zdev, pa, dma_addr, size, flags))
294 return dma_addr + offset;
295
296out_free:
297 dma_free_iommu(zdev, iommu_page_index, nr_pages);
298out_err:
299 dev_err(dev, "Failed to map addr: %lx\n", pa);
300 return DMA_ERROR_CODE;
301}
302
303static void s390_dma_unmap_pages(struct device *dev, dma_addr_t dma_addr,
304 size_t size, enum dma_data_direction direction,
305 struct dma_attrs *attrs)
306{
307 struct zpci_dev *zdev = get_zdev(container_of(dev, struct pci_dev, dev));
308 unsigned long iommu_page_index;
309 int npages;
310
311 npages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
312 dma_addr = dma_addr & PAGE_MASK;
313 if (dma_update_trans(zdev, 0, dma_addr, npages * PAGE_SIZE,
314 ZPCI_TABLE_PROTECTED | ZPCI_PTE_INVALID))
315 dev_err(dev, "Failed to unmap addr: %Lx\n", dma_addr);
316
317 iommu_page_index = (dma_addr - zdev->start_dma) >> PAGE_SHIFT;
318 dma_free_iommu(zdev, iommu_page_index, npages);
319}
320
321static void *s390_dma_alloc(struct device *dev, size_t size,
322 dma_addr_t *dma_handle, gfp_t flag,
323 struct dma_attrs *attrs)
324{
325 struct page *page;
326 unsigned long pa;
327 dma_addr_t map;
328
329 size = PAGE_ALIGN(size);
330 page = alloc_pages(flag, get_order(size));
331 if (!page)
332 return NULL;
333 pa = page_to_phys(page);
334 memset((void *) pa, 0, size);
335
336 map = s390_dma_map_pages(dev, page, pa % PAGE_SIZE,
337 size, DMA_BIDIRECTIONAL, NULL);
338 if (dma_mapping_error(dev, map)) {
339 free_pages(pa, get_order(size));
340 return NULL;
341 }
342
343 if (dma_handle)
344 *dma_handle = map;
345 return (void *) pa;
346}
347
348static void s390_dma_free(struct device *dev, size_t size,
349 void *pa, dma_addr_t dma_handle,
350 struct dma_attrs *attrs)
351{
352 s390_dma_unmap_pages(dev, dma_handle, PAGE_ALIGN(size),
353 DMA_BIDIRECTIONAL, NULL);
354 free_pages((unsigned long) pa, get_order(size));
355}
356
357static int s390_dma_map_sg(struct device *dev, struct scatterlist *sg,
358 int nr_elements, enum dma_data_direction dir,
359 struct dma_attrs *attrs)
360{
361 int mapped_elements = 0;
362 struct scatterlist *s;
363 int i;
364
365 for_each_sg(sg, s, nr_elements, i) {
366 struct page *page = sg_page(s);
367 s->dma_address = s390_dma_map_pages(dev, page, s->offset,
368 s->length, dir, NULL);
369 if (!dma_mapping_error(dev, s->dma_address)) {
370 s->dma_length = s->length;
371 mapped_elements++;
372 } else
373 goto unmap;
374 }
375out:
376 return mapped_elements;
377
378unmap:
379 for_each_sg(sg, s, mapped_elements, i) {
380 if (s->dma_address)
381 s390_dma_unmap_pages(dev, s->dma_address, s->dma_length,
382 dir, NULL);
383 s->dma_address = 0;
384 s->dma_length = 0;
385 }
386 mapped_elements = 0;
387 goto out;
388}
389
390static void s390_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
391 int nr_elements, enum dma_data_direction dir,
392 struct dma_attrs *attrs)
393{
394 struct scatterlist *s;
395 int i;
396
397 for_each_sg(sg, s, nr_elements, i) {
398 s390_dma_unmap_pages(dev, s->dma_address, s->dma_length, dir, NULL);
399 s->dma_address = 0;
400 s->dma_length = 0;
401 }
402}
403
404int zpci_dma_init_device(struct zpci_dev *zdev)
405{
406 unsigned int bitmap_order;
407 int rc;
408
409 spin_lock_init(&zdev->iommu_bitmap_lock);
410 spin_lock_init(&zdev->dma_table_lock);
411
412 zdev->dma_table = dma_alloc_cpu_table();
413 if (!zdev->dma_table) {
414 rc = -ENOMEM;
415 goto out_clean;
416 }
417
418 zdev->iommu_size = (unsigned long) high_memory - PAGE_OFFSET;
419 zdev->iommu_pages = zdev->iommu_size >> PAGE_SHIFT;
420 bitmap_order = get_order(zdev->iommu_pages / 8);
421 pr_info("iommu_size: 0x%lx iommu_pages: 0x%lx bitmap_order: %i\n",
422 zdev->iommu_size, zdev->iommu_pages, bitmap_order);
423
424 zdev->iommu_bitmap = (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
425 bitmap_order);
426 if (!zdev->iommu_bitmap) {
427 rc = -ENOMEM;
428 goto out_reg;
429 }
430
431 rc = zpci_register_ioat(zdev,
432 0,
433 zdev->start_dma + PAGE_OFFSET,
434 zdev->start_dma + zdev->iommu_size - 1,
435 (u64) zdev->dma_table);
436 if (rc)
437 goto out_reg;
438 return 0;
439
440out_reg:
441 dma_free_cpu_table(zdev->dma_table);
442out_clean:
443 return rc;
444}
445
446void zpci_dma_exit_device(struct zpci_dev *zdev)
447{
448 zpci_unregister_ioat(zdev, 0);
449 dma_cleanup_tables(zdev);
450 free_pages((unsigned long) zdev->iommu_bitmap,
451 get_order(zdev->iommu_pages / 8));
452 zdev->iommu_bitmap = NULL;
453 zdev->next_bit = 0;
454}
455
456static int __init dma_alloc_cpu_table_caches(void)
457{
458 dma_region_table_cache = kmem_cache_create("PCI_DMA_region_tables",
459 ZPCI_TABLE_SIZE, ZPCI_TABLE_ALIGN,
460 0, NULL);
461 if (!dma_region_table_cache)
462 return -ENOMEM;
463
464 dma_page_table_cache = kmem_cache_create("PCI_DMA_page_tables",
465 ZPCI_PT_SIZE, ZPCI_PT_ALIGN,
466 0, NULL);
467 if (!dma_page_table_cache) {
468 kmem_cache_destroy(dma_region_table_cache);
469 return -ENOMEM;
470 }
471 return 0;
472}
473
474int __init zpci_dma_init(void)
475{
476 return dma_alloc_cpu_table_caches();
477}
478
479void zpci_dma_exit(void)
480{
481 kmem_cache_destroy(dma_page_table_cache);
482 kmem_cache_destroy(dma_region_table_cache);
483}
484
485#define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
486
487static int __init dma_debug_do_init(void)
488{
489 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
490 return 0;
491}
492fs_initcall(dma_debug_do_init);
493
494struct dma_map_ops s390_dma_ops = {
495 .alloc = s390_dma_alloc,
496 .free = s390_dma_free,
497 .map_sg = s390_dma_map_sg,
498 .unmap_sg = s390_dma_unmap_sg,
499 .map_page = s390_dma_map_pages,
500 .unmap_page = s390_dma_unmap_pages,
501 /* if we support direct DMA this must be conditional */
502 .is_phys = 0,
503 /* dma_supported is unconditionally true without a callback */
504};
505EXPORT_SYMBOL_GPL(s390_dma_ops);