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/vmalloc.h>
14 #include <linux/pci.h>
15 #include <asm/pci_dma.h>
17 static struct kmem_cache
*dma_region_table_cache
;
18 static struct kmem_cache
*dma_page_table_cache
;
19 static int s390_iommu_strict
;
21 static int zpci_refresh_global(struct zpci_dev
*zdev
)
23 return zpci_refresh_trans((u64
) zdev
->fh
<< 32, zdev
->start_dma
,
24 zdev
->iommu_pages
* PAGE_SIZE
);
27 unsigned long *dma_alloc_cpu_table(void)
29 unsigned long *table
, *entry
;
31 table
= kmem_cache_alloc(dma_region_table_cache
, GFP_ATOMIC
);
35 for (entry
= table
; entry
< table
+ ZPCI_TABLE_ENTRIES
; entry
++)
36 *entry
= ZPCI_TABLE_INVALID
;
40 static void dma_free_cpu_table(void *table
)
42 kmem_cache_free(dma_region_table_cache
, table
);
45 static unsigned long *dma_alloc_page_table(void)
47 unsigned long *table
, *entry
;
49 table
= kmem_cache_alloc(dma_page_table_cache
, GFP_ATOMIC
);
53 for (entry
= table
; entry
< table
+ ZPCI_PT_ENTRIES
; entry
++)
54 *entry
= ZPCI_PTE_INVALID
;
58 static void dma_free_page_table(void *table
)
60 kmem_cache_free(dma_page_table_cache
, table
);
63 static unsigned long *dma_get_seg_table_origin(unsigned long *entry
)
67 if (reg_entry_isvalid(*entry
))
68 sto
= get_rt_sto(*entry
);
70 sto
= dma_alloc_cpu_table();
74 set_rt_sto(entry
, sto
);
75 validate_rt_entry(entry
);
76 entry_clr_protected(entry
);
81 static unsigned long *dma_get_page_table_origin(unsigned long *entry
)
85 if (reg_entry_isvalid(*entry
))
86 pto
= get_st_pto(*entry
);
88 pto
= dma_alloc_page_table();
91 set_st_pto(entry
, pto
);
92 validate_st_entry(entry
);
93 entry_clr_protected(entry
);
98 unsigned long *dma_walk_cpu_trans(unsigned long *rto
, dma_addr_t dma_addr
)
100 unsigned long *sto
, *pto
;
101 unsigned int rtx
, sx
, px
;
103 rtx
= calc_rtx(dma_addr
);
104 sto
= dma_get_seg_table_origin(&rto
[rtx
]);
108 sx
= calc_sx(dma_addr
);
109 pto
= dma_get_page_table_origin(&sto
[sx
]);
113 px
= calc_px(dma_addr
);
117 void dma_update_cpu_trans(unsigned long *entry
, void *page_addr
, int flags
)
119 if (flags
& ZPCI_PTE_INVALID
) {
120 invalidate_pt_entry(entry
);
122 set_pt_pfaa(entry
, page_addr
);
123 validate_pt_entry(entry
);
126 if (flags
& ZPCI_TABLE_PROTECTED
)
127 entry_set_protected(entry
);
129 entry_clr_protected(entry
);
132 static int __dma_update_trans(struct zpci_dev
*zdev
, unsigned long pa
,
133 dma_addr_t dma_addr
, size_t size
, int flags
)
135 unsigned int nr_pages
= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
136 u8
*page_addr
= (u8
*) (pa
& PAGE_MASK
);
137 unsigned long irq_flags
;
138 unsigned long *entry
;
144 spin_lock_irqsave(&zdev
->dma_table_lock
, irq_flags
);
145 if (!zdev
->dma_table
) {
150 for (i
= 0; i
< nr_pages
; i
++) {
151 entry
= dma_walk_cpu_trans(zdev
->dma_table
, dma_addr
);
156 dma_update_cpu_trans(entry
, page_addr
, flags
);
157 page_addr
+= PAGE_SIZE
;
158 dma_addr
+= PAGE_SIZE
;
162 if (rc
&& ((flags
& ZPCI_PTE_VALID_MASK
) == ZPCI_PTE_VALID
)) {
163 flags
= ZPCI_PTE_INVALID
;
165 page_addr
-= PAGE_SIZE
;
166 dma_addr
-= PAGE_SIZE
;
167 entry
= dma_walk_cpu_trans(zdev
->dma_table
, dma_addr
);
170 dma_update_cpu_trans(entry
, page_addr
, flags
);
174 spin_unlock_irqrestore(&zdev
->dma_table_lock
, irq_flags
);
178 static int __dma_purge_tlb(struct zpci_dev
*zdev
, dma_addr_t dma_addr
,
179 size_t size
, int flags
)
182 * With zdev->tlb_refresh == 0, rpcit is not required to establish new
183 * translations when previously invalid translation-table entries are
184 * validated. With lazy unmap, it also is skipped for previously valid
185 * entries, but a global rpcit is then required before any address can
186 * be re-used, i.e. after each iommu bitmap wrap-around.
188 if (!zdev
->tlb_refresh
&&
189 (!s390_iommu_strict
||
190 ((flags
& ZPCI_PTE_VALID_MASK
) == ZPCI_PTE_VALID
)))
193 return zpci_refresh_trans((u64
) zdev
->fh
<< 32, dma_addr
,
197 static int dma_update_trans(struct zpci_dev
*zdev
, unsigned long pa
,
198 dma_addr_t dma_addr
, size_t size
, int flags
)
202 rc
= __dma_update_trans(zdev
, pa
, dma_addr
, size
, flags
);
206 rc
= __dma_purge_tlb(zdev
, dma_addr
, size
, flags
);
207 if (rc
&& ((flags
& ZPCI_PTE_VALID_MASK
) == ZPCI_PTE_VALID
))
208 __dma_update_trans(zdev
, pa
, dma_addr
, size
, ZPCI_PTE_INVALID
);
213 void dma_free_seg_table(unsigned long entry
)
215 unsigned long *sto
= get_rt_sto(entry
);
218 for (sx
= 0; sx
< ZPCI_TABLE_ENTRIES
; sx
++)
219 if (reg_entry_isvalid(sto
[sx
]))
220 dma_free_page_table(get_st_pto(sto
[sx
]));
222 dma_free_cpu_table(sto
);
225 void dma_cleanup_tables(unsigned long *table
)
232 for (rtx
= 0; rtx
< ZPCI_TABLE_ENTRIES
; rtx
++)
233 if (reg_entry_isvalid(table
[rtx
]))
234 dma_free_seg_table(table
[rtx
]);
236 dma_free_cpu_table(table
);
239 static unsigned long __dma_alloc_iommu(struct device
*dev
,
240 unsigned long start
, int size
)
242 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
243 unsigned long boundary_size
;
245 boundary_size
= ALIGN(dma_get_seg_boundary(dev
) + 1,
246 PAGE_SIZE
) >> PAGE_SHIFT
;
247 return iommu_area_alloc(zdev
->iommu_bitmap
, zdev
->iommu_pages
,
248 start
, size
, zdev
->start_dma
>> PAGE_SHIFT
,
252 static dma_addr_t
dma_alloc_address(struct device
*dev
, int size
)
254 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
255 unsigned long offset
, flags
;
257 spin_lock_irqsave(&zdev
->iommu_bitmap_lock
, flags
);
258 offset
= __dma_alloc_iommu(dev
, zdev
->next_bit
, size
);
260 if (!zdev
->tlb_refresh
&& !s390_iommu_strict
) {
261 /* global flush before DMA addresses are reused */
262 if (zpci_refresh_global(zdev
))
265 bitmap_andnot(zdev
->iommu_bitmap
, zdev
->iommu_bitmap
,
266 zdev
->lazy_bitmap
, zdev
->iommu_pages
);
267 bitmap_zero(zdev
->lazy_bitmap
, zdev
->iommu_pages
);
270 offset
= __dma_alloc_iommu(dev
, 0, size
);
274 zdev
->next_bit
= offset
+ size
;
275 spin_unlock_irqrestore(&zdev
->iommu_bitmap_lock
, flags
);
277 return zdev
->start_dma
+ offset
* PAGE_SIZE
;
280 spin_unlock_irqrestore(&zdev
->iommu_bitmap_lock
, flags
);
281 return DMA_ERROR_CODE
;
284 static void dma_free_address(struct device
*dev
, dma_addr_t dma_addr
, int size
)
286 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
287 unsigned long flags
, offset
;
289 offset
= (dma_addr
- zdev
->start_dma
) >> PAGE_SHIFT
;
291 spin_lock_irqsave(&zdev
->iommu_bitmap_lock
, flags
);
292 if (!zdev
->iommu_bitmap
)
295 if (zdev
->tlb_refresh
|| s390_iommu_strict
)
296 bitmap_clear(zdev
->iommu_bitmap
, offset
, size
);
298 bitmap_set(zdev
->lazy_bitmap
, offset
, size
);
301 spin_unlock_irqrestore(&zdev
->iommu_bitmap_lock
, flags
);
304 static inline void zpci_err_dma(unsigned long rc
, unsigned long addr
)
309 } __packed data
= {rc
, addr
};
311 zpci_err_hex(&data
, sizeof(data
));
314 static dma_addr_t
s390_dma_map_pages(struct device
*dev
, struct page
*page
,
315 unsigned long offset
, size_t size
,
316 enum dma_data_direction direction
,
319 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
320 unsigned long pa
= page_to_phys(page
) + offset
;
321 int flags
= ZPCI_PTE_VALID
;
322 unsigned long nr_pages
;
326 /* This rounds up number of pages based on size and offset */
327 nr_pages
= iommu_num_pages(pa
, size
, PAGE_SIZE
);
328 dma_addr
= dma_alloc_address(dev
, nr_pages
);
329 if (dma_addr
== DMA_ERROR_CODE
) {
334 /* Use rounded up size */
335 size
= nr_pages
* PAGE_SIZE
;
337 if (direction
== DMA_NONE
|| direction
== DMA_TO_DEVICE
)
338 flags
|= ZPCI_TABLE_PROTECTED
;
340 ret
= dma_update_trans(zdev
, pa
, dma_addr
, size
, flags
);
344 atomic64_add(nr_pages
, &zdev
->mapped_pages
);
345 return dma_addr
+ (offset
& ~PAGE_MASK
);
348 dma_free_address(dev
, dma_addr
, nr_pages
);
350 zpci_err("map error:\n");
351 zpci_err_dma(ret
, pa
);
352 return DMA_ERROR_CODE
;
355 static void s390_dma_unmap_pages(struct device
*dev
, dma_addr_t dma_addr
,
356 size_t size
, enum dma_data_direction direction
,
359 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
362 npages
= iommu_num_pages(dma_addr
, size
, PAGE_SIZE
);
363 dma_addr
= dma_addr
& PAGE_MASK
;
364 ret
= dma_update_trans(zdev
, 0, dma_addr
, npages
* PAGE_SIZE
,
367 zpci_err("unmap error:\n");
368 zpci_err_dma(ret
, dma_addr
);
372 atomic64_add(npages
, &zdev
->unmapped_pages
);
373 dma_free_address(dev
, dma_addr
, npages
);
376 static void *s390_dma_alloc(struct device
*dev
, size_t size
,
377 dma_addr_t
*dma_handle
, gfp_t flag
,
380 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
385 size
= PAGE_ALIGN(size
);
386 page
= alloc_pages(flag
, get_order(size
));
390 pa
= page_to_phys(page
);
391 memset((void *) pa
, 0, size
);
393 map
= s390_dma_map_pages(dev
, page
, 0, size
, DMA_BIDIRECTIONAL
, 0);
394 if (dma_mapping_error(dev
, map
)) {
395 free_pages(pa
, get_order(size
));
399 atomic64_add(size
/ PAGE_SIZE
, &zdev
->allocated_pages
);
405 static void s390_dma_free(struct device
*dev
, size_t size
,
406 void *pa
, dma_addr_t dma_handle
,
409 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
411 size
= PAGE_ALIGN(size
);
412 atomic64_sub(size
/ PAGE_SIZE
, &zdev
->allocated_pages
);
413 s390_dma_unmap_pages(dev
, dma_handle
, size
, DMA_BIDIRECTIONAL
, 0);
414 free_pages((unsigned long) pa
, get_order(size
));
417 /* Map a segment into a contiguous dma address area */
418 static int __s390_dma_map_sg(struct device
*dev
, struct scatterlist
*sg
,
419 size_t size
, dma_addr_t
*handle
,
420 enum dma_data_direction dir
)
422 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
423 dma_addr_t dma_addr_base
, dma_addr
;
424 int flags
= ZPCI_PTE_VALID
;
425 struct scatterlist
*s
;
429 size
= PAGE_ALIGN(size
);
430 dma_addr_base
= dma_alloc_address(dev
, size
>> PAGE_SHIFT
);
431 if (dma_addr_base
== DMA_ERROR_CODE
)
434 dma_addr
= dma_addr_base
;
435 if (dir
== DMA_NONE
|| dir
== DMA_TO_DEVICE
)
436 flags
|= ZPCI_TABLE_PROTECTED
;
438 for (s
= sg
; dma_addr
< dma_addr_base
+ size
; s
= sg_next(s
)) {
439 pa
= page_to_phys(sg_page(s
)) + s
->offset
;
440 ret
= __dma_update_trans(zdev
, pa
, dma_addr
, s
->length
, flags
);
444 dma_addr
+= s
->length
;
446 ret
= __dma_purge_tlb(zdev
, dma_addr_base
, size
, flags
);
450 *handle
= dma_addr_base
;
451 atomic64_add(size
>> PAGE_SHIFT
, &zdev
->mapped_pages
);
456 dma_update_trans(zdev
, 0, dma_addr_base
, dma_addr
- dma_addr_base
,
458 dma_free_address(dev
, dma_addr_base
, size
>> PAGE_SHIFT
);
459 zpci_err("map error:\n");
460 zpci_err_dma(ret
, pa
);
464 static int s390_dma_map_sg(struct device
*dev
, struct scatterlist
*sg
,
465 int nr_elements
, enum dma_data_direction dir
,
468 struct scatterlist
*s
= sg
, *start
= sg
, *dma
= sg
;
469 unsigned int max
= dma_get_max_seg_size(dev
);
470 unsigned int size
= s
->offset
+ s
->length
;
471 unsigned int offset
= s
->offset
;
474 for (i
= 1; i
< nr_elements
; i
++) {
477 s
->dma_address
= DMA_ERROR_CODE
;
480 if (s
->offset
|| (size
& ~PAGE_MASK
) ||
481 size
+ s
->length
> max
) {
482 if (__s390_dma_map_sg(dev
, start
, size
,
483 &dma
->dma_address
, dir
))
486 dma
->dma_address
+= offset
;
487 dma
->dma_length
= size
- offset
;
489 size
= offset
= s
->offset
;
496 if (__s390_dma_map_sg(dev
, start
, size
, &dma
->dma_address
, dir
))
499 dma
->dma_address
+= offset
;
500 dma
->dma_length
= size
- offset
;
504 for_each_sg(sg
, s
, count
, i
)
505 s390_dma_unmap_pages(dev
, sg_dma_address(s
), sg_dma_len(s
),
511 static void s390_dma_unmap_sg(struct device
*dev
, struct scatterlist
*sg
,
512 int nr_elements
, enum dma_data_direction dir
,
515 struct scatterlist
*s
;
518 for_each_sg(sg
, s
, nr_elements
, i
) {
520 s390_dma_unmap_pages(dev
, s
->dma_address
, s
->dma_length
,
527 int zpci_dma_init_device(struct zpci_dev
*zdev
)
532 * At this point, if the device is part of an IOMMU domain, this would
533 * be a strong hint towards a bug in the IOMMU API (common) code and/or
534 * simultaneous access via IOMMU and DMA API. So let's issue a warning.
536 WARN_ON(zdev
->s390_domain
);
538 spin_lock_init(&zdev
->iommu_bitmap_lock
);
539 spin_lock_init(&zdev
->dma_table_lock
);
541 zdev
->dma_table
= dma_alloc_cpu_table();
542 if (!zdev
->dma_table
) {
548 * Restrict the iommu bitmap size to the minimum of the following:
550 * - 3-level pagetable address limit minus start_dma offset
551 * - DMA address range allowed by the hardware (clp query pci fn)
553 * Also set zdev->end_dma to the actual end address of the usable
554 * range, instead of the theoretical maximum as reported by hardware.
556 zdev
->start_dma
= PAGE_ALIGN(zdev
->start_dma
);
557 zdev
->iommu_size
= min3((u64
) high_memory
,
558 ZPCI_TABLE_SIZE_RT
- zdev
->start_dma
,
559 zdev
->end_dma
- zdev
->start_dma
+ 1);
560 zdev
->end_dma
= zdev
->start_dma
+ zdev
->iommu_size
- 1;
561 zdev
->iommu_pages
= zdev
->iommu_size
>> PAGE_SHIFT
;
562 zdev
->iommu_bitmap
= vzalloc(zdev
->iommu_pages
/ 8);
563 if (!zdev
->iommu_bitmap
) {
567 if (!zdev
->tlb_refresh
&& !s390_iommu_strict
) {
568 zdev
->lazy_bitmap
= vzalloc(zdev
->iommu_pages
/ 8);
569 if (!zdev
->lazy_bitmap
) {
575 rc
= zpci_register_ioat(zdev
, 0, zdev
->start_dma
, zdev
->end_dma
,
576 (u64
) zdev
->dma_table
);
582 vfree(zdev
->iommu_bitmap
);
583 zdev
->iommu_bitmap
= NULL
;
584 vfree(zdev
->lazy_bitmap
);
585 zdev
->lazy_bitmap
= NULL
;
587 dma_free_cpu_table(zdev
->dma_table
);
588 zdev
->dma_table
= NULL
;
593 void zpci_dma_exit_device(struct zpci_dev
*zdev
)
596 * At this point, if the device is part of an IOMMU domain, this would
597 * be a strong hint towards a bug in the IOMMU API (common) code and/or
598 * simultaneous access via IOMMU and DMA API. So let's issue a warning.
600 WARN_ON(zdev
->s390_domain
);
602 zpci_unregister_ioat(zdev
, 0);
603 dma_cleanup_tables(zdev
->dma_table
);
604 zdev
->dma_table
= NULL
;
605 vfree(zdev
->iommu_bitmap
);
606 zdev
->iommu_bitmap
= NULL
;
607 vfree(zdev
->lazy_bitmap
);
608 zdev
->lazy_bitmap
= NULL
;
613 static int __init
dma_alloc_cpu_table_caches(void)
615 dma_region_table_cache
= kmem_cache_create("PCI_DMA_region_tables",
616 ZPCI_TABLE_SIZE
, ZPCI_TABLE_ALIGN
,
618 if (!dma_region_table_cache
)
621 dma_page_table_cache
= kmem_cache_create("PCI_DMA_page_tables",
622 ZPCI_PT_SIZE
, ZPCI_PT_ALIGN
,
624 if (!dma_page_table_cache
) {
625 kmem_cache_destroy(dma_region_table_cache
);
631 int __init
zpci_dma_init(void)
633 return dma_alloc_cpu_table_caches();
636 void zpci_dma_exit(void)
638 kmem_cache_destroy(dma_page_table_cache
);
639 kmem_cache_destroy(dma_region_table_cache
);
642 #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
644 static int __init
dma_debug_do_init(void)
646 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES
);
649 fs_initcall(dma_debug_do_init
);
651 struct dma_map_ops s390_pci_dma_ops
= {
652 .alloc
= s390_dma_alloc
,
653 .free
= s390_dma_free
,
654 .map_sg
= s390_dma_map_sg
,
655 .unmap_sg
= s390_dma_unmap_sg
,
656 .map_page
= s390_dma_map_pages
,
657 .unmap_page
= s390_dma_unmap_pages
,
658 /* if we support direct DMA this must be conditional */
660 /* dma_supported is unconditionally true without a callback */
662 EXPORT_SYMBOL_GPL(s390_pci_dma_ops
);
664 static int __init
s390_iommu_setup(char *str
)
666 if (!strncmp(str
, "strict", 6))
667 s390_iommu_strict
= 1;
671 __setup("s390_iommu=", s390_iommu_setup
);