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 #define S390_MAPPING_ERROR (~(dma_addr_t) 0x0)
19 static struct kmem_cache
*dma_region_table_cache
;
20 static struct kmem_cache
*dma_page_table_cache
;
21 static int s390_iommu_strict
;
23 static int zpci_refresh_global(struct zpci_dev
*zdev
)
25 return zpci_refresh_trans((u64
) zdev
->fh
<< 32, zdev
->start_dma
,
26 zdev
->iommu_pages
* PAGE_SIZE
);
29 unsigned long *dma_alloc_cpu_table(void)
31 unsigned long *table
, *entry
;
33 table
= kmem_cache_alloc(dma_region_table_cache
, GFP_ATOMIC
);
37 for (entry
= table
; entry
< table
+ ZPCI_TABLE_ENTRIES
; entry
++)
38 *entry
= ZPCI_TABLE_INVALID
;
42 static void dma_free_cpu_table(void *table
)
44 kmem_cache_free(dma_region_table_cache
, table
);
47 static unsigned long *dma_alloc_page_table(void)
49 unsigned long *table
, *entry
;
51 table
= kmem_cache_alloc(dma_page_table_cache
, GFP_ATOMIC
);
55 for (entry
= table
; entry
< table
+ ZPCI_PT_ENTRIES
; entry
++)
56 *entry
= ZPCI_PTE_INVALID
;
60 static void dma_free_page_table(void *table
)
62 kmem_cache_free(dma_page_table_cache
, table
);
65 static unsigned long *dma_get_seg_table_origin(unsigned long *entry
)
69 if (reg_entry_isvalid(*entry
))
70 sto
= get_rt_sto(*entry
);
72 sto
= dma_alloc_cpu_table();
76 set_rt_sto(entry
, sto
);
77 validate_rt_entry(entry
);
78 entry_clr_protected(entry
);
83 static unsigned long *dma_get_page_table_origin(unsigned long *entry
)
87 if (reg_entry_isvalid(*entry
))
88 pto
= get_st_pto(*entry
);
90 pto
= dma_alloc_page_table();
93 set_st_pto(entry
, pto
);
94 validate_st_entry(entry
);
95 entry_clr_protected(entry
);
100 unsigned long *dma_walk_cpu_trans(unsigned long *rto
, dma_addr_t dma_addr
)
102 unsigned long *sto
, *pto
;
103 unsigned int rtx
, sx
, px
;
105 rtx
= calc_rtx(dma_addr
);
106 sto
= dma_get_seg_table_origin(&rto
[rtx
]);
110 sx
= calc_sx(dma_addr
);
111 pto
= dma_get_page_table_origin(&sto
[sx
]);
115 px
= calc_px(dma_addr
);
119 void dma_update_cpu_trans(unsigned long *entry
, void *page_addr
, int flags
)
121 if (flags
& ZPCI_PTE_INVALID
) {
122 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 unsigned long irq_flags
;
140 unsigned long *entry
;
146 spin_lock_irqsave(&zdev
->dma_table_lock
, irq_flags
);
147 if (!zdev
->dma_table
) {
152 for (i
= 0; i
< nr_pages
; i
++) {
153 entry
= dma_walk_cpu_trans(zdev
->dma_table
, dma_addr
);
158 dma_update_cpu_trans(entry
, page_addr
, flags
);
159 page_addr
+= PAGE_SIZE
;
160 dma_addr
+= PAGE_SIZE
;
164 if (rc
&& ((flags
& ZPCI_PTE_VALID_MASK
) == ZPCI_PTE_VALID
)) {
165 flags
= ZPCI_PTE_INVALID
;
167 page_addr
-= PAGE_SIZE
;
168 dma_addr
-= PAGE_SIZE
;
169 entry
= dma_walk_cpu_trans(zdev
->dma_table
, dma_addr
);
172 dma_update_cpu_trans(entry
, page_addr
, flags
);
176 spin_unlock_irqrestore(&zdev
->dma_table_lock
, irq_flags
);
180 static int __dma_purge_tlb(struct zpci_dev
*zdev
, dma_addr_t dma_addr
,
181 size_t size
, int flags
)
184 * With zdev->tlb_refresh == 0, rpcit is not required to establish new
185 * translations when previously invalid translation-table entries are
186 * validated. With lazy unmap, rpcit is skipped for previously valid
187 * entries, but a global rpcit is then required before any address can
188 * be re-used, i.e. after each iommu bitmap wrap-around.
190 if ((flags
& ZPCI_PTE_VALID_MASK
) == ZPCI_PTE_VALID
) {
191 if (!zdev
->tlb_refresh
)
194 if (!s390_iommu_strict
)
198 return zpci_refresh_trans((u64
) zdev
->fh
<< 32, dma_addr
,
202 static int dma_update_trans(struct zpci_dev
*zdev
, unsigned long pa
,
203 dma_addr_t dma_addr
, size_t size
, int flags
)
207 rc
= __dma_update_trans(zdev
, pa
, dma_addr
, size
, flags
);
211 rc
= __dma_purge_tlb(zdev
, dma_addr
, size
, flags
);
212 if (rc
&& ((flags
& ZPCI_PTE_VALID_MASK
) == ZPCI_PTE_VALID
))
213 __dma_update_trans(zdev
, pa
, dma_addr
, size
, ZPCI_PTE_INVALID
);
218 void dma_free_seg_table(unsigned long entry
)
220 unsigned long *sto
= get_rt_sto(entry
);
223 for (sx
= 0; sx
< ZPCI_TABLE_ENTRIES
; sx
++)
224 if (reg_entry_isvalid(sto
[sx
]))
225 dma_free_page_table(get_st_pto(sto
[sx
]));
227 dma_free_cpu_table(sto
);
230 void dma_cleanup_tables(unsigned long *table
)
237 for (rtx
= 0; rtx
< ZPCI_TABLE_ENTRIES
; rtx
++)
238 if (reg_entry_isvalid(table
[rtx
]))
239 dma_free_seg_table(table
[rtx
]);
241 dma_free_cpu_table(table
);
244 static unsigned long __dma_alloc_iommu(struct device
*dev
,
245 unsigned long start
, int size
)
247 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
248 unsigned long boundary_size
;
250 boundary_size
= ALIGN(dma_get_seg_boundary(dev
) + 1,
251 PAGE_SIZE
) >> PAGE_SHIFT
;
252 return iommu_area_alloc(zdev
->iommu_bitmap
, zdev
->iommu_pages
,
253 start
, size
, zdev
->start_dma
>> PAGE_SHIFT
,
257 static dma_addr_t
dma_alloc_address(struct device
*dev
, int size
)
259 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
260 unsigned long offset
, flags
;
262 spin_lock_irqsave(&zdev
->iommu_bitmap_lock
, flags
);
263 offset
= __dma_alloc_iommu(dev
, zdev
->next_bit
, size
);
265 if (!s390_iommu_strict
) {
266 /* global flush before DMA addresses are reused */
267 if (zpci_refresh_global(zdev
))
270 bitmap_andnot(zdev
->iommu_bitmap
, zdev
->iommu_bitmap
,
271 zdev
->lazy_bitmap
, zdev
->iommu_pages
);
272 bitmap_zero(zdev
->lazy_bitmap
, zdev
->iommu_pages
);
275 offset
= __dma_alloc_iommu(dev
, 0, size
);
279 zdev
->next_bit
= offset
+ size
;
280 spin_unlock_irqrestore(&zdev
->iommu_bitmap_lock
, flags
);
282 return zdev
->start_dma
+ offset
* PAGE_SIZE
;
285 spin_unlock_irqrestore(&zdev
->iommu_bitmap_lock
, flags
);
286 return S390_MAPPING_ERROR
;
289 static void dma_free_address(struct device
*dev
, dma_addr_t dma_addr
, int size
)
291 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
292 unsigned long flags
, offset
;
294 offset
= (dma_addr
- zdev
->start_dma
) >> PAGE_SHIFT
;
296 spin_lock_irqsave(&zdev
->iommu_bitmap_lock
, flags
);
297 if (!zdev
->iommu_bitmap
)
300 if (s390_iommu_strict
)
301 bitmap_clear(zdev
->iommu_bitmap
, offset
, size
);
303 bitmap_set(zdev
->lazy_bitmap
, offset
, size
);
306 spin_unlock_irqrestore(&zdev
->iommu_bitmap_lock
, flags
);
309 static inline void zpci_err_dma(unsigned long rc
, unsigned long addr
)
314 } __packed data
= {rc
, addr
};
316 zpci_err_hex(&data
, sizeof(data
));
319 static dma_addr_t
s390_dma_map_pages(struct device
*dev
, struct page
*page
,
320 unsigned long offset
, size_t size
,
321 enum dma_data_direction direction
,
324 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
325 unsigned long pa
= page_to_phys(page
) + offset
;
326 int flags
= ZPCI_PTE_VALID
;
327 unsigned long nr_pages
;
331 /* This rounds up number of pages based on size and offset */
332 nr_pages
= iommu_num_pages(pa
, size
, PAGE_SIZE
);
333 dma_addr
= dma_alloc_address(dev
, nr_pages
);
334 if (dma_addr
== S390_MAPPING_ERROR
) {
339 /* Use rounded up size */
340 size
= nr_pages
* PAGE_SIZE
;
342 if (direction
== DMA_NONE
|| direction
== DMA_TO_DEVICE
)
343 flags
|= ZPCI_TABLE_PROTECTED
;
345 ret
= dma_update_trans(zdev
, pa
, dma_addr
, size
, flags
);
349 atomic64_add(nr_pages
, &zdev
->mapped_pages
);
350 return dma_addr
+ (offset
& ~PAGE_MASK
);
353 dma_free_address(dev
, dma_addr
, nr_pages
);
355 zpci_err("map error:\n");
356 zpci_err_dma(ret
, pa
);
357 return S390_MAPPING_ERROR
;
360 static void s390_dma_unmap_pages(struct device
*dev
, dma_addr_t dma_addr
,
361 size_t size
, enum dma_data_direction direction
,
364 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
367 npages
= iommu_num_pages(dma_addr
, size
, PAGE_SIZE
);
368 dma_addr
= dma_addr
& PAGE_MASK
;
369 ret
= dma_update_trans(zdev
, 0, dma_addr
, npages
* PAGE_SIZE
,
372 zpci_err("unmap error:\n");
373 zpci_err_dma(ret
, dma_addr
);
377 atomic64_add(npages
, &zdev
->unmapped_pages
);
378 dma_free_address(dev
, dma_addr
, npages
);
381 static void *s390_dma_alloc(struct device
*dev
, size_t size
,
382 dma_addr_t
*dma_handle
, gfp_t flag
,
385 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
390 size
= PAGE_ALIGN(size
);
391 page
= alloc_pages(flag
, get_order(size
));
395 pa
= page_to_phys(page
);
396 map
= s390_dma_map_pages(dev
, page
, 0, size
, DMA_BIDIRECTIONAL
, 0);
397 if (dma_mapping_error(dev
, map
)) {
398 free_pages(pa
, get_order(size
));
402 atomic64_add(size
/ PAGE_SIZE
, &zdev
->allocated_pages
);
408 static void s390_dma_free(struct device
*dev
, size_t size
,
409 void *pa
, dma_addr_t dma_handle
,
412 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
414 size
= PAGE_ALIGN(size
);
415 atomic64_sub(size
/ PAGE_SIZE
, &zdev
->allocated_pages
);
416 s390_dma_unmap_pages(dev
, dma_handle
, size
, DMA_BIDIRECTIONAL
, 0);
417 free_pages((unsigned long) pa
, get_order(size
));
420 /* Map a segment into a contiguous dma address area */
421 static int __s390_dma_map_sg(struct device
*dev
, struct scatterlist
*sg
,
422 size_t size
, dma_addr_t
*handle
,
423 enum dma_data_direction dir
)
425 unsigned long nr_pages
= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
426 struct zpci_dev
*zdev
= to_zpci(to_pci_dev(dev
));
427 dma_addr_t dma_addr_base
, dma_addr
;
428 int flags
= ZPCI_PTE_VALID
;
429 struct scatterlist
*s
;
430 unsigned long pa
= 0;
433 dma_addr_base
= dma_alloc_address(dev
, nr_pages
);
434 if (dma_addr_base
== S390_MAPPING_ERROR
)
437 dma_addr
= dma_addr_base
;
438 if (dir
== DMA_NONE
|| dir
== DMA_TO_DEVICE
)
439 flags
|= ZPCI_TABLE_PROTECTED
;
441 for (s
= sg
; dma_addr
< dma_addr_base
+ size
; s
= sg_next(s
)) {
442 pa
= page_to_phys(sg_page(s
));
443 ret
= __dma_update_trans(zdev
, pa
, dma_addr
,
444 s
->offset
+ s
->length
, flags
);
448 dma_addr
+= s
->offset
+ s
->length
;
450 ret
= __dma_purge_tlb(zdev
, dma_addr_base
, size
, flags
);
454 *handle
= dma_addr_base
;
455 atomic64_add(nr_pages
, &zdev
->mapped_pages
);
460 dma_update_trans(zdev
, 0, dma_addr_base
, dma_addr
- dma_addr_base
,
462 dma_free_address(dev
, dma_addr_base
, nr_pages
);
463 zpci_err("map error:\n");
464 zpci_err_dma(ret
, pa
);
468 static int s390_dma_map_sg(struct device
*dev
, struct scatterlist
*sg
,
469 int nr_elements
, enum dma_data_direction dir
,
472 struct scatterlist
*s
= sg
, *start
= sg
, *dma
= sg
;
473 unsigned int max
= dma_get_max_seg_size(dev
);
474 unsigned int size
= s
->offset
+ s
->length
;
475 unsigned int offset
= s
->offset
;
478 for (i
= 1; i
< nr_elements
; i
++) {
481 s
->dma_address
= S390_MAPPING_ERROR
;
484 if (s
->offset
|| (size
& ~PAGE_MASK
) ||
485 size
+ s
->length
> max
) {
486 if (__s390_dma_map_sg(dev
, start
, size
,
487 &dma
->dma_address
, dir
))
490 dma
->dma_address
+= offset
;
491 dma
->dma_length
= size
- offset
;
493 size
= offset
= s
->offset
;
500 if (__s390_dma_map_sg(dev
, start
, size
, &dma
->dma_address
, dir
))
503 dma
->dma_address
+= offset
;
504 dma
->dma_length
= size
- offset
;
508 for_each_sg(sg
, s
, count
, i
)
509 s390_dma_unmap_pages(dev
, sg_dma_address(s
), sg_dma_len(s
),
515 static void s390_dma_unmap_sg(struct device
*dev
, struct scatterlist
*sg
,
516 int nr_elements
, enum dma_data_direction dir
,
519 struct scatterlist
*s
;
522 for_each_sg(sg
, s
, nr_elements
, i
) {
524 s390_dma_unmap_pages(dev
, s
->dma_address
, s
->dma_length
,
531 static int s390_mapping_error(struct device
*dev
, dma_addr_t dma_addr
)
533 return dma_addr
== S390_MAPPING_ERROR
;
536 int zpci_dma_init_device(struct zpci_dev
*zdev
)
541 * At this point, if the device is part of an IOMMU domain, this would
542 * be a strong hint towards a bug in the IOMMU API (common) code and/or
543 * simultaneous access via IOMMU and DMA API. So let's issue a warning.
545 WARN_ON(zdev
->s390_domain
);
547 spin_lock_init(&zdev
->iommu_bitmap_lock
);
548 spin_lock_init(&zdev
->dma_table_lock
);
550 zdev
->dma_table
= dma_alloc_cpu_table();
551 if (!zdev
->dma_table
) {
557 * Restrict the iommu bitmap size to the minimum of the following:
559 * - 3-level pagetable address limit minus start_dma offset
560 * - DMA address range allowed by the hardware (clp query pci fn)
562 * Also set zdev->end_dma to the actual end address of the usable
563 * range, instead of the theoretical maximum as reported by hardware.
565 zdev
->start_dma
= PAGE_ALIGN(zdev
->start_dma
);
566 zdev
->iommu_size
= min3((u64
) high_memory
,
567 ZPCI_TABLE_SIZE_RT
- zdev
->start_dma
,
568 zdev
->end_dma
- zdev
->start_dma
+ 1);
569 zdev
->end_dma
= zdev
->start_dma
+ zdev
->iommu_size
- 1;
570 zdev
->iommu_pages
= zdev
->iommu_size
>> PAGE_SHIFT
;
571 zdev
->iommu_bitmap
= vzalloc(zdev
->iommu_pages
/ 8);
572 if (!zdev
->iommu_bitmap
) {
576 if (!s390_iommu_strict
) {
577 zdev
->lazy_bitmap
= vzalloc(zdev
->iommu_pages
/ 8);
578 if (!zdev
->lazy_bitmap
) {
584 rc
= zpci_register_ioat(zdev
, 0, zdev
->start_dma
, zdev
->end_dma
,
585 (u64
) zdev
->dma_table
);
591 vfree(zdev
->iommu_bitmap
);
592 zdev
->iommu_bitmap
= NULL
;
593 vfree(zdev
->lazy_bitmap
);
594 zdev
->lazy_bitmap
= NULL
;
596 dma_free_cpu_table(zdev
->dma_table
);
597 zdev
->dma_table
= NULL
;
602 void zpci_dma_exit_device(struct zpci_dev
*zdev
)
605 * At this point, if the device is part of an IOMMU domain, this would
606 * be a strong hint towards a bug in the IOMMU API (common) code and/or
607 * simultaneous access via IOMMU and DMA API. So let's issue a warning.
609 WARN_ON(zdev
->s390_domain
);
611 if (zpci_unregister_ioat(zdev
, 0))
614 dma_cleanup_tables(zdev
->dma_table
);
615 zdev
->dma_table
= NULL
;
616 vfree(zdev
->iommu_bitmap
);
617 zdev
->iommu_bitmap
= NULL
;
618 vfree(zdev
->lazy_bitmap
);
619 zdev
->lazy_bitmap
= NULL
;
624 static int __init
dma_alloc_cpu_table_caches(void)
626 dma_region_table_cache
= kmem_cache_create("PCI_DMA_region_tables",
627 ZPCI_TABLE_SIZE
, ZPCI_TABLE_ALIGN
,
629 if (!dma_region_table_cache
)
632 dma_page_table_cache
= kmem_cache_create("PCI_DMA_page_tables",
633 ZPCI_PT_SIZE
, ZPCI_PT_ALIGN
,
635 if (!dma_page_table_cache
) {
636 kmem_cache_destroy(dma_region_table_cache
);
642 int __init
zpci_dma_init(void)
644 return dma_alloc_cpu_table_caches();
647 void zpci_dma_exit(void)
649 kmem_cache_destroy(dma_page_table_cache
);
650 kmem_cache_destroy(dma_region_table_cache
);
653 #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
655 static int __init
dma_debug_do_init(void)
657 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES
);
660 fs_initcall(dma_debug_do_init
);
662 const struct dma_map_ops s390_pci_dma_ops
= {
663 .alloc
= s390_dma_alloc
,
664 .free
= s390_dma_free
,
665 .map_sg
= s390_dma_map_sg
,
666 .unmap_sg
= s390_dma_unmap_sg
,
667 .map_page
= s390_dma_map_pages
,
668 .unmap_page
= s390_dma_unmap_pages
,
669 .mapping_error
= s390_mapping_error
,
670 /* if we support direct DMA this must be conditional */
672 /* dma_supported is unconditionally true without a callback */
674 EXPORT_SYMBOL_GPL(s390_pci_dma_ops
);
676 static int __init
s390_iommu_setup(char *str
)
678 if (!strncmp(str
, "strict", 6))
679 s390_iommu_strict
= 1;
683 __setup("s390_iommu=", s390_iommu_setup
);