1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2012
6 * Jan Glauber <jang@linux.vnet.ibm.com>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/export.h>
12 #include <linux/iommu-helper.h>
13 #include <linux/dma-map-ops.h>
14 #include <linux/vmalloc.h>
15 #include <linux/pci.h>
16 #include <asm/pci_dma.h>
18 static struct kmem_cache *dma_region_table_cache;
19 static struct kmem_cache *dma_page_table_cache;
20 static int s390_iommu_strict;
21 static u64 s390_iommu_aperture;
22 static u32 s390_iommu_aperture_factor = 1;
24 static int zpci_refresh_global(struct zpci_dev *zdev)
26 return zpci_refresh_trans((u64) zdev->fh << 32, zdev->start_dma,
27 zdev->iommu_pages * PAGE_SIZE);
30 unsigned long *dma_alloc_cpu_table(gfp_t gfp)
32 unsigned long *table, *entry;
34 table = kmem_cache_alloc(dma_region_table_cache, gfp);
38 for (entry = table; entry < table + ZPCI_TABLE_ENTRIES; entry++)
39 *entry = ZPCI_TABLE_INVALID;
43 static void dma_free_cpu_table(void *table)
45 kmem_cache_free(dma_region_table_cache, table);
48 static unsigned long *dma_alloc_page_table(gfp_t gfp)
50 unsigned long *table, *entry;
52 table = kmem_cache_alloc(dma_page_table_cache, gfp);
56 for (entry = table; entry < table + ZPCI_PT_ENTRIES; entry++)
57 *entry = ZPCI_PTE_INVALID;
61 static void dma_free_page_table(void *table)
63 kmem_cache_free(dma_page_table_cache, table);
66 static unsigned long *dma_get_seg_table_origin(unsigned long *rtep, gfp_t gfp)
68 unsigned long old_rte, rte;
71 rte = READ_ONCE(*rtep);
72 if (reg_entry_isvalid(rte)) {
73 sto = get_rt_sto(rte);
75 sto = dma_alloc_cpu_table(gfp);
79 set_rt_sto(&rte, virt_to_phys(sto));
80 validate_rt_entry(&rte);
81 entry_clr_protected(&rte);
83 old_rte = cmpxchg(rtep, ZPCI_TABLE_INVALID, rte);
84 if (old_rte != ZPCI_TABLE_INVALID) {
85 /* Somone else was faster, use theirs */
86 dma_free_cpu_table(sto);
87 sto = get_rt_sto(old_rte);
93 static unsigned long *dma_get_page_table_origin(unsigned long *step, gfp_t gfp)
95 unsigned long old_ste, ste;
98 ste = READ_ONCE(*step);
99 if (reg_entry_isvalid(ste)) {
100 pto = get_st_pto(ste);
102 pto = dma_alloc_page_table(gfp);
105 set_st_pto(&ste, virt_to_phys(pto));
106 validate_st_entry(&ste);
107 entry_clr_protected(&ste);
109 old_ste = cmpxchg(step, ZPCI_TABLE_INVALID, ste);
110 if (old_ste != ZPCI_TABLE_INVALID) {
111 /* Somone else was faster, use theirs */
112 dma_free_page_table(pto);
113 pto = get_st_pto(old_ste);
119 unsigned long *dma_walk_cpu_trans(unsigned long *rto, dma_addr_t dma_addr,
122 unsigned long *sto, *pto;
123 unsigned int rtx, sx, px;
125 rtx = calc_rtx(dma_addr);
126 sto = dma_get_seg_table_origin(&rto[rtx], gfp);
130 sx = calc_sx(dma_addr);
131 pto = dma_get_page_table_origin(&sto[sx], gfp);
135 px = calc_px(dma_addr);
139 void dma_update_cpu_trans(unsigned long *ptep, phys_addr_t page_addr, int flags)
143 pte = READ_ONCE(*ptep);
144 if (flags & ZPCI_PTE_INVALID) {
145 invalidate_pt_entry(&pte);
147 set_pt_pfaa(&pte, page_addr);
148 validate_pt_entry(&pte);
151 if (flags & ZPCI_TABLE_PROTECTED)
152 entry_set_protected(&pte);
154 entry_clr_protected(&pte);
159 static int __dma_update_trans(struct zpci_dev *zdev, phys_addr_t pa,
160 dma_addr_t dma_addr, size_t size, int flags)
162 unsigned int nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
163 phys_addr_t page_addr = (pa & PAGE_MASK);
164 unsigned long *entry;
170 if (!zdev->dma_table)
173 for (i = 0; i < nr_pages; i++) {
174 entry = dma_walk_cpu_trans(zdev->dma_table, dma_addr,
180 dma_update_cpu_trans(entry, page_addr, flags);
181 page_addr += PAGE_SIZE;
182 dma_addr += PAGE_SIZE;
186 if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)) {
187 flags = ZPCI_PTE_INVALID;
189 page_addr -= PAGE_SIZE;
190 dma_addr -= PAGE_SIZE;
191 entry = dma_walk_cpu_trans(zdev->dma_table, dma_addr,
195 dma_update_cpu_trans(entry, page_addr, flags);
201 static int __dma_purge_tlb(struct zpci_dev *zdev, dma_addr_t dma_addr,
202 size_t size, int flags)
204 unsigned long irqflags;
208 * With zdev->tlb_refresh == 0, rpcit is not required to establish new
209 * translations when previously invalid translation-table entries are
210 * validated. With lazy unmap, rpcit is skipped for previously valid
211 * entries, but a global rpcit is then required before any address can
212 * be re-used, i.e. after each iommu bitmap wrap-around.
214 if ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID) {
215 if (!zdev->tlb_refresh)
218 if (!s390_iommu_strict)
222 ret = zpci_refresh_trans((u64) zdev->fh << 32, dma_addr,
224 if (ret == -ENOMEM && !s390_iommu_strict) {
225 /* enable the hypervisor to free some resources */
226 if (zpci_refresh_global(zdev))
229 spin_lock_irqsave(&zdev->iommu_bitmap_lock, irqflags);
230 bitmap_andnot(zdev->iommu_bitmap, zdev->iommu_bitmap,
231 zdev->lazy_bitmap, zdev->iommu_pages);
232 bitmap_zero(zdev->lazy_bitmap, zdev->iommu_pages);
233 spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, irqflags);
240 static int dma_update_trans(struct zpci_dev *zdev, phys_addr_t pa,
241 dma_addr_t dma_addr, size_t size, int flags)
245 rc = __dma_update_trans(zdev, pa, dma_addr, size, flags);
249 rc = __dma_purge_tlb(zdev, dma_addr, size, flags);
250 if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID))
251 __dma_update_trans(zdev, pa, dma_addr, size, ZPCI_PTE_INVALID);
256 void dma_free_seg_table(unsigned long entry)
258 unsigned long *sto = get_rt_sto(entry);
261 for (sx = 0; sx < ZPCI_TABLE_ENTRIES; sx++)
262 if (reg_entry_isvalid(sto[sx]))
263 dma_free_page_table(get_st_pto(sto[sx]));
265 dma_free_cpu_table(sto);
268 void dma_cleanup_tables(unsigned long *table)
275 for (rtx = 0; rtx < ZPCI_TABLE_ENTRIES; rtx++)
276 if (reg_entry_isvalid(table[rtx]))
277 dma_free_seg_table(table[rtx]);
279 dma_free_cpu_table(table);
282 static unsigned long __dma_alloc_iommu(struct device *dev,
283 unsigned long start, int size)
285 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
287 return iommu_area_alloc(zdev->iommu_bitmap, zdev->iommu_pages,
288 start, size, zdev->start_dma >> PAGE_SHIFT,
289 dma_get_seg_boundary_nr_pages(dev, PAGE_SHIFT),
293 static dma_addr_t dma_alloc_address(struct device *dev, int size)
295 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
296 unsigned long offset, flags;
298 spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags);
299 offset = __dma_alloc_iommu(dev, zdev->next_bit, size);
301 if (!s390_iommu_strict) {
302 /* global flush before DMA addresses are reused */
303 if (zpci_refresh_global(zdev))
306 bitmap_andnot(zdev->iommu_bitmap, zdev->iommu_bitmap,
307 zdev->lazy_bitmap, zdev->iommu_pages);
308 bitmap_zero(zdev->lazy_bitmap, zdev->iommu_pages);
311 offset = __dma_alloc_iommu(dev, 0, size);
315 zdev->next_bit = offset + size;
316 spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);
318 return zdev->start_dma + offset * PAGE_SIZE;
321 spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);
322 return DMA_MAPPING_ERROR;
325 static void dma_free_address(struct device *dev, dma_addr_t dma_addr, int size)
327 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
328 unsigned long flags, offset;
330 offset = (dma_addr - zdev->start_dma) >> PAGE_SHIFT;
332 spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags);
333 if (!zdev->iommu_bitmap)
336 if (s390_iommu_strict)
337 bitmap_clear(zdev->iommu_bitmap, offset, size);
339 bitmap_set(zdev->lazy_bitmap, offset, size);
342 spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);
345 static inline void zpci_err_dma(unsigned long rc, unsigned long addr)
350 } __packed data = {rc, addr};
352 zpci_err_hex(&data, sizeof(data));
355 static dma_addr_t s390_dma_map_pages(struct device *dev, struct page *page,
356 unsigned long offset, size_t size,
357 enum dma_data_direction direction,
360 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
361 unsigned long pa = page_to_phys(page) + offset;
362 int flags = ZPCI_PTE_VALID;
363 unsigned long nr_pages;
367 /* This rounds up number of pages based on size and offset */
368 nr_pages = iommu_num_pages(pa, size, PAGE_SIZE);
369 dma_addr = dma_alloc_address(dev, nr_pages);
370 if (dma_addr == DMA_MAPPING_ERROR) {
375 /* Use rounded up size */
376 size = nr_pages * PAGE_SIZE;
378 if (direction == DMA_NONE || direction == DMA_TO_DEVICE)
379 flags |= ZPCI_TABLE_PROTECTED;
381 ret = dma_update_trans(zdev, pa, dma_addr, size, flags);
385 atomic64_add(nr_pages, &zdev->mapped_pages);
386 return dma_addr + (offset & ~PAGE_MASK);
389 dma_free_address(dev, dma_addr, nr_pages);
391 zpci_err("map error:\n");
392 zpci_err_dma(ret, pa);
393 return DMA_MAPPING_ERROR;
396 static void s390_dma_unmap_pages(struct device *dev, dma_addr_t dma_addr,
397 size_t size, enum dma_data_direction direction,
400 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
403 npages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
404 dma_addr = dma_addr & PAGE_MASK;
405 ret = dma_update_trans(zdev, 0, dma_addr, npages * PAGE_SIZE,
408 zpci_err("unmap error:\n");
409 zpci_err_dma(ret, dma_addr);
413 atomic64_add(npages, &zdev->unmapped_pages);
414 dma_free_address(dev, dma_addr, npages);
417 static void *s390_dma_alloc(struct device *dev, size_t size,
418 dma_addr_t *dma_handle, gfp_t flag,
421 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
426 size = PAGE_ALIGN(size);
427 page = alloc_pages(flag | __GFP_ZERO, get_order(size));
431 pa = page_to_phys(page);
432 map = s390_dma_map_pages(dev, page, 0, size, DMA_BIDIRECTIONAL, 0);
433 if (dma_mapping_error(dev, map)) {
434 __free_pages(page, get_order(size));
438 atomic64_add(size / PAGE_SIZE, &zdev->allocated_pages);
441 return phys_to_virt(pa);
444 static void s390_dma_free(struct device *dev, size_t size,
445 void *vaddr, dma_addr_t dma_handle,
448 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
450 size = PAGE_ALIGN(size);
451 atomic64_sub(size / PAGE_SIZE, &zdev->allocated_pages);
452 s390_dma_unmap_pages(dev, dma_handle, size, DMA_BIDIRECTIONAL, 0);
453 free_pages((unsigned long)vaddr, get_order(size));
456 /* Map a segment into a contiguous dma address area */
457 static int __s390_dma_map_sg(struct device *dev, struct scatterlist *sg,
458 size_t size, dma_addr_t *handle,
459 enum dma_data_direction dir)
461 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
462 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
463 dma_addr_t dma_addr_base, dma_addr;
464 int flags = ZPCI_PTE_VALID;
465 struct scatterlist *s;
469 dma_addr_base = dma_alloc_address(dev, nr_pages);
470 if (dma_addr_base == DMA_MAPPING_ERROR)
473 dma_addr = dma_addr_base;
474 if (dir == DMA_NONE || dir == DMA_TO_DEVICE)
475 flags |= ZPCI_TABLE_PROTECTED;
477 for (s = sg; dma_addr < dma_addr_base + size; s = sg_next(s)) {
478 pa = page_to_phys(sg_page(s));
479 ret = __dma_update_trans(zdev, pa, dma_addr,
480 s->offset + s->length, flags);
484 dma_addr += s->offset + s->length;
486 ret = __dma_purge_tlb(zdev, dma_addr_base, size, flags);
490 *handle = dma_addr_base;
491 atomic64_add(nr_pages, &zdev->mapped_pages);
496 dma_update_trans(zdev, 0, dma_addr_base, dma_addr - dma_addr_base,
498 dma_free_address(dev, dma_addr_base, nr_pages);
499 zpci_err("map error:\n");
500 zpci_err_dma(ret, pa);
504 static int s390_dma_map_sg(struct device *dev, struct scatterlist *sg,
505 int nr_elements, enum dma_data_direction dir,
508 struct scatterlist *s = sg, *start = sg, *dma = sg;
509 unsigned int max = dma_get_max_seg_size(dev);
510 unsigned int size = s->offset + s->length;
511 unsigned int offset = s->offset;
512 int count = 0, i, ret;
514 for (i = 1; i < nr_elements; i++) {
519 if (s->offset || (size & ~PAGE_MASK) ||
520 size + s->length > max) {
521 ret = __s390_dma_map_sg(dev, start, size,
522 &dma->dma_address, dir);
526 dma->dma_address += offset;
527 dma->dma_length = size - offset;
529 size = offset = s->offset;
536 ret = __s390_dma_map_sg(dev, start, size, &dma->dma_address, dir);
540 dma->dma_address += offset;
541 dma->dma_length = size - offset;
545 for_each_sg(sg, s, count, i)
546 s390_dma_unmap_pages(dev, sg_dma_address(s), sg_dma_len(s),
552 static void s390_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
553 int nr_elements, enum dma_data_direction dir,
556 struct scatterlist *s;
559 for_each_sg(sg, s, nr_elements, i) {
561 s390_dma_unmap_pages(dev, s->dma_address, s->dma_length,
568 int zpci_dma_init_device(struct zpci_dev *zdev)
574 * At this point, if the device is part of an IOMMU domain, this would
575 * be a strong hint towards a bug in the IOMMU API (common) code and/or
576 * simultaneous access via IOMMU and DMA API. So let's issue a warning.
578 WARN_ON(zdev->s390_domain);
580 spin_lock_init(&zdev->iommu_bitmap_lock);
582 zdev->dma_table = dma_alloc_cpu_table(GFP_KERNEL);
583 if (!zdev->dma_table) {
589 * Restrict the iommu bitmap size to the minimum of the following:
590 * - s390_iommu_aperture which defaults to high_memory
591 * - 3-level pagetable address limit minus start_dma offset
592 * - DMA address range allowed by the hardware (clp query pci fn)
594 * Also set zdev->end_dma to the actual end address of the usable
595 * range, instead of the theoretical maximum as reported by hardware.
597 * This limits the number of concurrently usable DMA mappings since
598 * for each DMA mapped memory address we need a DMA address including
599 * extra DMA addresses for multiple mappings of the same memory address.
601 zdev->start_dma = PAGE_ALIGN(zdev->start_dma);
602 zdev->iommu_size = min3(s390_iommu_aperture,
603 ZPCI_TABLE_SIZE_RT - zdev->start_dma,
604 zdev->end_dma - zdev->start_dma + 1);
605 zdev->end_dma = zdev->start_dma + zdev->iommu_size - 1;
606 zdev->iommu_pages = zdev->iommu_size >> PAGE_SHIFT;
607 zdev->iommu_bitmap = vzalloc(zdev->iommu_pages / 8);
608 if (!zdev->iommu_bitmap) {
612 if (!s390_iommu_strict) {
613 zdev->lazy_bitmap = vzalloc(zdev->iommu_pages / 8);
614 if (!zdev->lazy_bitmap) {
620 if (zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
621 virt_to_phys(zdev->dma_table), &status)) {
628 vfree(zdev->iommu_bitmap);
629 zdev->iommu_bitmap = NULL;
630 vfree(zdev->lazy_bitmap);
631 zdev->lazy_bitmap = NULL;
633 dma_free_cpu_table(zdev->dma_table);
634 zdev->dma_table = NULL;
639 int zpci_dma_exit_device(struct zpci_dev *zdev)
644 * At this point, if the device is part of an IOMMU domain, this would
645 * be a strong hint towards a bug in the IOMMU API (common) code and/or
646 * simultaneous access via IOMMU and DMA API. So let's issue a warning.
648 WARN_ON(zdev->s390_domain);
649 if (zdev_enabled(zdev))
650 cc = zpci_unregister_ioat(zdev, 0);
652 * cc == 3 indicates the function is gone already. This can happen
653 * if the function was deconfigured/disabled suddenly and we have not
654 * received a new handle yet.
659 dma_cleanup_tables(zdev->dma_table);
660 zdev->dma_table = NULL;
661 vfree(zdev->iommu_bitmap);
662 zdev->iommu_bitmap = NULL;
663 vfree(zdev->lazy_bitmap);
664 zdev->lazy_bitmap = NULL;
669 static int __init dma_alloc_cpu_table_caches(void)
671 dma_region_table_cache = kmem_cache_create("PCI_DMA_region_tables",
672 ZPCI_TABLE_SIZE, ZPCI_TABLE_ALIGN,
674 if (!dma_region_table_cache)
677 dma_page_table_cache = kmem_cache_create("PCI_DMA_page_tables",
678 ZPCI_PT_SIZE, ZPCI_PT_ALIGN,
680 if (!dma_page_table_cache) {
681 kmem_cache_destroy(dma_region_table_cache);
687 int __init zpci_dma_init(void)
689 s390_iommu_aperture = (u64)virt_to_phys(high_memory);
690 if (!s390_iommu_aperture_factor)
691 s390_iommu_aperture = ULONG_MAX;
693 s390_iommu_aperture *= s390_iommu_aperture_factor;
695 return dma_alloc_cpu_table_caches();
698 void zpci_dma_exit(void)
700 kmem_cache_destroy(dma_page_table_cache);
701 kmem_cache_destroy(dma_region_table_cache);
704 const struct dma_map_ops s390_pci_dma_ops = {
705 .alloc = s390_dma_alloc,
706 .free = s390_dma_free,
707 .map_sg = s390_dma_map_sg,
708 .unmap_sg = s390_dma_unmap_sg,
709 .map_page = s390_dma_map_pages,
710 .unmap_page = s390_dma_unmap_pages,
711 .mmap = dma_common_mmap,
712 .get_sgtable = dma_common_get_sgtable,
713 .alloc_pages = dma_common_alloc_pages,
714 .free_pages = dma_common_free_pages,
715 /* dma_supported is unconditionally true without a callback */
717 EXPORT_SYMBOL_GPL(s390_pci_dma_ops);
719 static int __init s390_iommu_setup(char *str)
721 if (!strcmp(str, "strict"))
722 s390_iommu_strict = 1;
726 __setup("s390_iommu=", s390_iommu_setup);
728 static int __init s390_iommu_aperture_setup(char *str)
730 if (kstrtou32(str, 10, &s390_iommu_aperture_factor))
731 s390_iommu_aperture_factor = 1;
735 __setup("s390_iommu_aperture=", s390_iommu_aperture_setup);