2 * Copyright IBM Corp. 2012
5 * Jan Glauber <jang@linux.vnet.ibm.com>
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>
16 static struct kmem_cache
*dma_region_table_cache
;
17 static struct kmem_cache
*dma_page_table_cache
;
19 static unsigned long *dma_alloc_cpu_table(void)
21 unsigned long *table
, *entry
;
23 table
= kmem_cache_alloc(dma_region_table_cache
, GFP_ATOMIC
);
27 for (entry
= table
; entry
< table
+ ZPCI_TABLE_ENTRIES
; entry
++)
28 *entry
= ZPCI_TABLE_INVALID
| ZPCI_TABLE_PROTECTED
;
32 static void dma_free_cpu_table(void *table
)
34 kmem_cache_free(dma_region_table_cache
, table
);
37 static unsigned long *dma_alloc_page_table(void)
39 unsigned long *table
, *entry
;
41 table
= kmem_cache_alloc(dma_page_table_cache
, GFP_ATOMIC
);
45 for (entry
= table
; entry
< table
+ ZPCI_PT_ENTRIES
; entry
++)
46 *entry
= ZPCI_PTE_INVALID
| ZPCI_TABLE_PROTECTED
;
50 static void dma_free_page_table(void *table
)
52 kmem_cache_free(dma_page_table_cache
, table
);
55 static unsigned long *dma_get_seg_table_origin(unsigned long *entry
)
59 if (reg_entry_isvalid(*entry
))
60 sto
= get_rt_sto(*entry
);
62 sto
= dma_alloc_cpu_table();
66 set_rt_sto(entry
, sto
);
67 validate_rt_entry(entry
);
68 entry_clr_protected(entry
);
73 static unsigned long *dma_get_page_table_origin(unsigned long *entry
)
77 if (reg_entry_isvalid(*entry
))
78 pto
= get_st_pto(*entry
);
80 pto
= dma_alloc_page_table();
83 set_st_pto(entry
, pto
);
84 validate_st_entry(entry
);
85 entry_clr_protected(entry
);
90 static unsigned long *dma_walk_cpu_trans(unsigned long *rto
, dma_addr_t dma_addr
)
92 unsigned long *sto
, *pto
;
93 unsigned int rtx
, sx
, px
;
95 rtx
= calc_rtx(dma_addr
);
96 sto
= dma_get_seg_table_origin(&rto
[rtx
]);
100 sx
= calc_sx(dma_addr
);
101 pto
= dma_get_page_table_origin(&sto
[sx
]);
105 px
= calc_px(dma_addr
);
109 static void dma_update_cpu_trans(struct zpci_dev
*zdev
, void *page_addr
,
110 dma_addr_t dma_addr
, int flags
)
112 unsigned long *entry
;
114 entry
= dma_walk_cpu_trans(zdev
->dma_table
, dma_addr
);
120 if (flags
& ZPCI_PTE_INVALID
) {
121 invalidate_pt_entry(entry
);
124 set_pt_pfaa(entry
, page_addr
);
125 validate_pt_entry(entry
);
128 if (flags
& ZPCI_TABLE_PROTECTED
)
129 entry_set_protected(entry
);
131 entry_clr_protected(entry
);
134 static int dma_update_trans(struct zpci_dev
*zdev
, unsigned long pa
,
135 dma_addr_t dma_addr
, size_t size
, int flags
)
137 unsigned int nr_pages
= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
138 u8
*page_addr
= (u8
*) (pa
& PAGE_MASK
);
139 dma_addr_t start_dma_addr
= dma_addr
;
140 unsigned long irq_flags
;
146 spin_lock_irqsave(&zdev
->dma_table_lock
, irq_flags
);
147 if (!zdev
->dma_table
) {
148 dev_err(&zdev
->pdev
->dev
, "Missing DMA table\n");
152 for (i
= 0; i
< nr_pages
; i
++) {
153 dma_update_cpu_trans(zdev
, page_addr
, dma_addr
, flags
);
154 page_addr
+= PAGE_SIZE
;
155 dma_addr
+= PAGE_SIZE
;
159 * rpcit is not required to establish new translations when previously
160 * invalid translation-table entries are validated, however it is
161 * required when altering previously valid entries.
163 if (!zdev
->tlb_refresh
&&
164 ((flags
& ZPCI_PTE_VALID_MASK
) == ZPCI_PTE_VALID
))
166 * TODO: also need to check that the old entry is indeed INVALID
167 * and not only for one page but for the whole range...
168 * -> now we WARN_ON in that case but with lazy unmap that
169 * needs to be redone!
173 rc
= s390pci_refresh_trans((u64
) zdev
->fh
<< 32, start_dma_addr
,
174 nr_pages
* PAGE_SIZE
);
177 spin_unlock_irqrestore(&zdev
->dma_table_lock
, irq_flags
);
181 static void dma_free_seg_table(unsigned long entry
)
183 unsigned long *sto
= get_rt_sto(entry
);
186 for (sx
= 0; sx
< ZPCI_TABLE_ENTRIES
; sx
++)
187 if (reg_entry_isvalid(sto
[sx
]))
188 dma_free_page_table(get_st_pto(sto
[sx
]));
190 dma_free_cpu_table(sto
);
193 static void dma_cleanup_tables(struct zpci_dev
*zdev
)
195 unsigned long *table
;
198 if (!zdev
|| !zdev
->dma_table
)
201 table
= zdev
->dma_table
;
202 for (rtx
= 0; rtx
< ZPCI_TABLE_ENTRIES
; rtx
++)
203 if (reg_entry_isvalid(table
[rtx
]))
204 dma_free_seg_table(table
[rtx
]);
206 dma_free_cpu_table(table
);
207 zdev
->dma_table
= NULL
;
210 static unsigned long __dma_alloc_iommu(struct zpci_dev
*zdev
, unsigned long start
,
213 unsigned long boundary_size
= 0x1000000;
215 return iommu_area_alloc(zdev
->iommu_bitmap
, zdev
->iommu_pages
,
216 start
, size
, 0, boundary_size
, 0);
219 static unsigned long dma_alloc_iommu(struct zpci_dev
*zdev
, int size
)
221 unsigned long offset
, flags
;
223 spin_lock_irqsave(&zdev
->iommu_bitmap_lock
, flags
);
224 offset
= __dma_alloc_iommu(zdev
, zdev
->next_bit
, size
);
226 offset
= __dma_alloc_iommu(zdev
, 0, size
);
229 zdev
->next_bit
= offset
+ size
;
230 if (zdev
->next_bit
>= zdev
->iommu_pages
)
233 spin_unlock_irqrestore(&zdev
->iommu_bitmap_lock
, flags
);
237 static void dma_free_iommu(struct zpci_dev
*zdev
, unsigned long offset
, int size
)
241 spin_lock_irqsave(&zdev
->iommu_bitmap_lock
, flags
);
242 if (!zdev
->iommu_bitmap
)
244 bitmap_clear(zdev
->iommu_bitmap
, offset
, size
);
245 if (offset
>= zdev
->next_bit
)
246 zdev
->next_bit
= offset
+ size
;
248 spin_unlock_irqrestore(&zdev
->iommu_bitmap_lock
, flags
);
251 int dma_set_mask(struct device
*dev
, u64 mask
)
253 if (!dev
->dma_mask
|| !dma_supported(dev
, mask
))
256 *dev
->dma_mask
= mask
;
259 EXPORT_SYMBOL_GPL(dma_set_mask
);
261 static 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
)
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
;
272 /* This rounds up number of pages based on size and offset */
273 nr_pages
= iommu_num_pages(pa
, size
, PAGE_SIZE
);
274 iommu_page_index
= dma_alloc_iommu(zdev
, nr_pages
);
275 if (iommu_page_index
== -1)
278 /* Use rounded up size */
279 size
= nr_pages
* PAGE_SIZE
;
281 dma_addr
= zdev
->start_dma
+ iommu_page_index
* PAGE_SIZE
;
282 if (dma_addr
+ size
> zdev
->end_dma
) {
283 dev_err(dev
, "(dma_addr: 0x%16.16LX + size: 0x%16.16lx) > end_dma: 0x%16.16Lx\n",
284 dma_addr
, size
, zdev
->end_dma
);
288 if (direction
== DMA_NONE
|| direction
== DMA_TO_DEVICE
)
289 flags
|= ZPCI_TABLE_PROTECTED
;
291 if (!dma_update_trans(zdev
, pa
, dma_addr
, size
, flags
)) {
292 atomic64_add(nr_pages
, (atomic64_t
*) &zdev
->fmb
->mapped_pages
);
293 return dma_addr
+ (offset
& ~PAGE_MASK
);
297 dma_free_iommu(zdev
, iommu_page_index
, nr_pages
);
299 dev_err(dev
, "Failed to map addr: %lx\n", pa
);
300 return DMA_ERROR_CODE
;
303 static 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
)
307 struct zpci_dev
*zdev
= get_zdev(container_of(dev
, struct pci_dev
, dev
));
308 unsigned long iommu_page_index
;
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
);
317 atomic64_add(npages
, (atomic64_t
*) &zdev
->fmb
->unmapped_pages
);
318 iommu_page_index
= (dma_addr
- zdev
->start_dma
) >> PAGE_SHIFT
;
319 dma_free_iommu(zdev
, iommu_page_index
, npages
);
322 static void *s390_dma_alloc(struct device
*dev
, size_t size
,
323 dma_addr_t
*dma_handle
, gfp_t flag
,
324 struct dma_attrs
*attrs
)
326 struct zpci_dev
*zdev
= get_zdev(container_of(dev
, struct pci_dev
, dev
));
331 size
= PAGE_ALIGN(size
);
332 page
= alloc_pages(flag
, get_order(size
));
336 atomic64_add(size
/ PAGE_SIZE
, (atomic64_t
*) &zdev
->fmb
->allocated_pages
);
337 pa
= page_to_phys(page
);
338 memset((void *) pa
, 0, size
);
340 map
= s390_dma_map_pages(dev
, page
, pa
% PAGE_SIZE
,
341 size
, DMA_BIDIRECTIONAL
, NULL
);
342 if (dma_mapping_error(dev
, map
)) {
343 free_pages(pa
, get_order(size
));
352 static void s390_dma_free(struct device
*dev
, size_t size
,
353 void *pa
, dma_addr_t dma_handle
,
354 struct dma_attrs
*attrs
)
356 s390_dma_unmap_pages(dev
, dma_handle
, PAGE_ALIGN(size
),
357 DMA_BIDIRECTIONAL
, NULL
);
358 free_pages((unsigned long) pa
, get_order(size
));
361 static int s390_dma_map_sg(struct device
*dev
, struct scatterlist
*sg
,
362 int nr_elements
, enum dma_data_direction dir
,
363 struct dma_attrs
*attrs
)
365 int mapped_elements
= 0;
366 struct scatterlist
*s
;
369 for_each_sg(sg
, s
, nr_elements
, i
) {
370 struct page
*page
= sg_page(s
);
371 s
->dma_address
= s390_dma_map_pages(dev
, page
, s
->offset
,
372 s
->length
, dir
, NULL
);
373 if (!dma_mapping_error(dev
, s
->dma_address
)) {
374 s
->dma_length
= s
->length
;
380 return mapped_elements
;
383 for_each_sg(sg
, s
, mapped_elements
, i
) {
385 s390_dma_unmap_pages(dev
, s
->dma_address
, s
->dma_length
,
394 static void s390_dma_unmap_sg(struct device
*dev
, struct scatterlist
*sg
,
395 int nr_elements
, enum dma_data_direction dir
,
396 struct dma_attrs
*attrs
)
398 struct scatterlist
*s
;
401 for_each_sg(sg
, s
, nr_elements
, i
) {
402 s390_dma_unmap_pages(dev
, s
->dma_address
, s
->dma_length
, dir
, NULL
);
408 int zpci_dma_init_device(struct zpci_dev
*zdev
)
410 unsigned int bitmap_order
;
413 spin_lock_init(&zdev
->iommu_bitmap_lock
);
414 spin_lock_init(&zdev
->dma_table_lock
);
416 zdev
->dma_table
= dma_alloc_cpu_table();
417 if (!zdev
->dma_table
) {
422 zdev
->iommu_size
= (unsigned long) high_memory
- PAGE_OFFSET
;
423 zdev
->iommu_pages
= zdev
->iommu_size
>> PAGE_SHIFT
;
424 bitmap_order
= get_order(zdev
->iommu_pages
/ 8);
425 pr_info("iommu_size: 0x%lx iommu_pages: 0x%lx bitmap_order: %i\n",
426 zdev
->iommu_size
, zdev
->iommu_pages
, bitmap_order
);
428 zdev
->iommu_bitmap
= (void *) __get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
430 if (!zdev
->iommu_bitmap
) {
435 rc
= zpci_register_ioat(zdev
,
437 zdev
->start_dma
+ PAGE_OFFSET
,
438 zdev
->start_dma
+ zdev
->iommu_size
- 1,
439 (u64
) zdev
->dma_table
);
445 dma_free_cpu_table(zdev
->dma_table
);
450 void zpci_dma_exit_device(struct zpci_dev
*zdev
)
452 zpci_unregister_ioat(zdev
, 0);
453 dma_cleanup_tables(zdev
);
454 free_pages((unsigned long) zdev
->iommu_bitmap
,
455 get_order(zdev
->iommu_pages
/ 8));
456 zdev
->iommu_bitmap
= NULL
;
460 static int __init
dma_alloc_cpu_table_caches(void)
462 dma_region_table_cache
= kmem_cache_create("PCI_DMA_region_tables",
463 ZPCI_TABLE_SIZE
, ZPCI_TABLE_ALIGN
,
465 if (!dma_region_table_cache
)
468 dma_page_table_cache
= kmem_cache_create("PCI_DMA_page_tables",
469 ZPCI_PT_SIZE
, ZPCI_PT_ALIGN
,
471 if (!dma_page_table_cache
) {
472 kmem_cache_destroy(dma_region_table_cache
);
478 int __init
zpci_dma_init(void)
480 return dma_alloc_cpu_table_caches();
483 void zpci_dma_exit(void)
485 kmem_cache_destroy(dma_page_table_cache
);
486 kmem_cache_destroy(dma_region_table_cache
);
489 #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
491 static int __init
dma_debug_do_init(void)
493 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES
);
496 fs_initcall(dma_debug_do_init
);
498 struct dma_map_ops s390_dma_ops
= {
499 .alloc
= s390_dma_alloc
,
500 .free
= s390_dma_free
,
501 .map_sg
= s390_dma_map_sg
,
502 .unmap_sg
= s390_dma_unmap_sg
,
503 .map_page
= s390_dma_map_pages
,
504 .unmap_page
= s390_dma_unmap_pages
,
505 /* if we support direct DMA this must be conditional */
507 /* dma_supported is unconditionally true without a callback */
509 EXPORT_SYMBOL_GPL(s390_dma_ops
);