1 // SPDX-License-Identifier: GPL-2.0
3 * IOMMU API for s390 PCI devices
5 * Copyright IBM Corp. 2015
10 #include <linux/iommu.h>
11 #include <linux/iommu-helper.h>
12 #include <linux/sizes.h>
13 #include <linux/rculist.h>
14 #include <linux/rcupdate.h>
15 #include <asm/pci_dma.h>
17 #include "dma-iommu.h"
19 static const struct iommu_ops s390_iommu_ops;
21 static struct kmem_cache *dma_region_table_cache;
22 static struct kmem_cache *dma_page_table_cache;
24 static u64 s390_iommu_aperture;
25 static u32 s390_iommu_aperture_factor = 1;
28 struct iommu_domain domain;
29 struct list_head devices;
30 struct zpci_iommu_ctrs ctrs;
31 unsigned long *dma_table;
36 static inline unsigned int calc_rtx(dma_addr_t ptr)
38 return ((unsigned long)ptr >> ZPCI_RT_SHIFT) & ZPCI_INDEX_MASK;
41 static inline unsigned int calc_sx(dma_addr_t ptr)
43 return ((unsigned long)ptr >> ZPCI_ST_SHIFT) & ZPCI_INDEX_MASK;
46 static inline unsigned int calc_px(dma_addr_t ptr)
48 return ((unsigned long)ptr >> PAGE_SHIFT) & ZPCI_PT_MASK;
51 static inline void set_pt_pfaa(unsigned long *entry, phys_addr_t pfaa)
53 *entry &= ZPCI_PTE_FLAG_MASK;
54 *entry |= (pfaa & ZPCI_PTE_ADDR_MASK);
57 static inline void set_rt_sto(unsigned long *entry, phys_addr_t sto)
59 *entry &= ZPCI_RTE_FLAG_MASK;
60 *entry |= (sto & ZPCI_RTE_ADDR_MASK);
61 *entry |= ZPCI_TABLE_TYPE_RTX;
64 static inline void set_st_pto(unsigned long *entry, phys_addr_t pto)
66 *entry &= ZPCI_STE_FLAG_MASK;
67 *entry |= (pto & ZPCI_STE_ADDR_MASK);
68 *entry |= ZPCI_TABLE_TYPE_SX;
71 static inline void validate_rt_entry(unsigned long *entry)
73 *entry &= ~ZPCI_TABLE_VALID_MASK;
74 *entry &= ~ZPCI_TABLE_OFFSET_MASK;
75 *entry |= ZPCI_TABLE_VALID;
76 *entry |= ZPCI_TABLE_LEN_RTX;
79 static inline void validate_st_entry(unsigned long *entry)
81 *entry &= ~ZPCI_TABLE_VALID_MASK;
82 *entry |= ZPCI_TABLE_VALID;
85 static inline void invalidate_pt_entry(unsigned long *entry)
87 WARN_ON_ONCE((*entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_INVALID);
88 *entry &= ~ZPCI_PTE_VALID_MASK;
89 *entry |= ZPCI_PTE_INVALID;
92 static inline void validate_pt_entry(unsigned long *entry)
94 WARN_ON_ONCE((*entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID);
95 *entry &= ~ZPCI_PTE_VALID_MASK;
96 *entry |= ZPCI_PTE_VALID;
99 static inline void entry_set_protected(unsigned long *entry)
101 *entry &= ~ZPCI_TABLE_PROT_MASK;
102 *entry |= ZPCI_TABLE_PROTECTED;
105 static inline void entry_clr_protected(unsigned long *entry)
107 *entry &= ~ZPCI_TABLE_PROT_MASK;
108 *entry |= ZPCI_TABLE_UNPROTECTED;
111 static inline int reg_entry_isvalid(unsigned long entry)
113 return (entry & ZPCI_TABLE_VALID_MASK) == ZPCI_TABLE_VALID;
116 static inline int pt_entry_isvalid(unsigned long entry)
118 return (entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID;
121 static inline unsigned long *get_rt_sto(unsigned long entry)
123 if ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_RTX)
124 return phys_to_virt(entry & ZPCI_RTE_ADDR_MASK);
129 static inline unsigned long *get_st_pto(unsigned long entry)
131 if ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_SX)
132 return phys_to_virt(entry & ZPCI_STE_ADDR_MASK);
137 static int __init dma_alloc_cpu_table_caches(void)
139 dma_region_table_cache = kmem_cache_create("PCI_DMA_region_tables",
143 if (!dma_region_table_cache)
146 dma_page_table_cache = kmem_cache_create("PCI_DMA_page_tables",
150 if (!dma_page_table_cache) {
151 kmem_cache_destroy(dma_region_table_cache);
157 static unsigned long *dma_alloc_cpu_table(gfp_t gfp)
159 unsigned long *table, *entry;
161 table = kmem_cache_alloc(dma_region_table_cache, gfp);
165 for (entry = table; entry < table + ZPCI_TABLE_ENTRIES; entry++)
166 *entry = ZPCI_TABLE_INVALID;
170 static void dma_free_cpu_table(void *table)
172 kmem_cache_free(dma_region_table_cache, table);
175 static void dma_free_page_table(void *table)
177 kmem_cache_free(dma_page_table_cache, table);
180 static void dma_free_seg_table(unsigned long entry)
182 unsigned long *sto = get_rt_sto(entry);
185 for (sx = 0; sx < ZPCI_TABLE_ENTRIES; sx++)
186 if (reg_entry_isvalid(sto[sx]))
187 dma_free_page_table(get_st_pto(sto[sx]));
189 dma_free_cpu_table(sto);
192 static void dma_cleanup_tables(unsigned long *table)
199 for (rtx = 0; rtx < ZPCI_TABLE_ENTRIES; rtx++)
200 if (reg_entry_isvalid(table[rtx]))
201 dma_free_seg_table(table[rtx]);
203 dma_free_cpu_table(table);
206 static unsigned long *dma_alloc_page_table(gfp_t gfp)
208 unsigned long *table, *entry;
210 table = kmem_cache_alloc(dma_page_table_cache, gfp);
214 for (entry = table; entry < table + ZPCI_PT_ENTRIES; entry++)
215 *entry = ZPCI_PTE_INVALID;
219 static unsigned long *dma_get_seg_table_origin(unsigned long *rtep, gfp_t gfp)
221 unsigned long old_rte, rte;
224 rte = READ_ONCE(*rtep);
225 if (reg_entry_isvalid(rte)) {
226 sto = get_rt_sto(rte);
228 sto = dma_alloc_cpu_table(gfp);
232 set_rt_sto(&rte, virt_to_phys(sto));
233 validate_rt_entry(&rte);
234 entry_clr_protected(&rte);
236 old_rte = cmpxchg(rtep, ZPCI_TABLE_INVALID, rte);
237 if (old_rte != ZPCI_TABLE_INVALID) {
238 /* Somone else was faster, use theirs */
239 dma_free_cpu_table(sto);
240 sto = get_rt_sto(old_rte);
246 static unsigned long *dma_get_page_table_origin(unsigned long *step, gfp_t gfp)
248 unsigned long old_ste, ste;
251 ste = READ_ONCE(*step);
252 if (reg_entry_isvalid(ste)) {
253 pto = get_st_pto(ste);
255 pto = dma_alloc_page_table(gfp);
258 set_st_pto(&ste, virt_to_phys(pto));
259 validate_st_entry(&ste);
260 entry_clr_protected(&ste);
262 old_ste = cmpxchg(step, ZPCI_TABLE_INVALID, ste);
263 if (old_ste != ZPCI_TABLE_INVALID) {
264 /* Somone else was faster, use theirs */
265 dma_free_page_table(pto);
266 pto = get_st_pto(old_ste);
272 static unsigned long *dma_walk_cpu_trans(unsigned long *rto, dma_addr_t dma_addr, gfp_t gfp)
274 unsigned long *sto, *pto;
275 unsigned int rtx, sx, px;
277 rtx = calc_rtx(dma_addr);
278 sto = dma_get_seg_table_origin(&rto[rtx], gfp);
282 sx = calc_sx(dma_addr);
283 pto = dma_get_page_table_origin(&sto[sx], gfp);
287 px = calc_px(dma_addr);
291 static void dma_update_cpu_trans(unsigned long *ptep, phys_addr_t page_addr, int flags)
295 pte = READ_ONCE(*ptep);
296 if (flags & ZPCI_PTE_INVALID) {
297 invalidate_pt_entry(&pte);
299 set_pt_pfaa(&pte, page_addr);
300 validate_pt_entry(&pte);
303 if (flags & ZPCI_TABLE_PROTECTED)
304 entry_set_protected(&pte);
306 entry_clr_protected(&pte);
311 static struct s390_domain *to_s390_domain(struct iommu_domain *dom)
313 return container_of(dom, struct s390_domain, domain);
316 static bool s390_iommu_capable(struct device *dev, enum iommu_cap cap)
318 struct zpci_dev *zdev = to_zpci_dev(dev);
321 case IOMMU_CAP_CACHE_COHERENCY:
323 case IOMMU_CAP_DEFERRED_FLUSH:
324 return zdev->pft != PCI_FUNC_TYPE_ISM;
330 static struct iommu_domain *s390_domain_alloc_paging(struct device *dev)
332 struct s390_domain *s390_domain;
334 s390_domain = kzalloc(sizeof(*s390_domain), GFP_KERNEL);
338 s390_domain->dma_table = dma_alloc_cpu_table(GFP_KERNEL);
339 if (!s390_domain->dma_table) {
343 s390_domain->domain.geometry.force_aperture = true;
344 s390_domain->domain.geometry.aperture_start = 0;
345 s390_domain->domain.geometry.aperture_end = ZPCI_TABLE_SIZE_RT - 1;
347 spin_lock_init(&s390_domain->list_lock);
348 INIT_LIST_HEAD_RCU(&s390_domain->devices);
350 return &s390_domain->domain;
353 static void s390_iommu_rcu_free_domain(struct rcu_head *head)
355 struct s390_domain *s390_domain = container_of(head, struct s390_domain, rcu);
357 dma_cleanup_tables(s390_domain->dma_table);
361 static void s390_domain_free(struct iommu_domain *domain)
363 struct s390_domain *s390_domain = to_s390_domain(domain);
366 WARN_ON(!list_empty(&s390_domain->devices));
369 call_rcu(&s390_domain->rcu, s390_iommu_rcu_free_domain);
372 static void s390_iommu_detach_device(struct iommu_domain *domain,
375 struct s390_domain *s390_domain = to_s390_domain(domain);
376 struct zpci_dev *zdev = to_zpci_dev(dev);
379 spin_lock_irqsave(&s390_domain->list_lock, flags);
380 list_del_rcu(&zdev->iommu_list);
381 spin_unlock_irqrestore(&s390_domain->list_lock, flags);
383 zpci_unregister_ioat(zdev, 0);
384 zdev->s390_domain = NULL;
385 zdev->dma_table = NULL;
388 static int s390_iommu_attach_device(struct iommu_domain *domain,
391 struct s390_domain *s390_domain = to_s390_domain(domain);
392 struct zpci_dev *zdev = to_zpci_dev(dev);
400 if (WARN_ON(domain->geometry.aperture_start > zdev->end_dma ||
401 domain->geometry.aperture_end < zdev->start_dma))
404 if (zdev->s390_domain)
405 s390_iommu_detach_device(&zdev->s390_domain->domain, dev);
407 cc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
408 virt_to_phys(s390_domain->dma_table), &status);
410 * If the device is undergoing error recovery the reset code
411 * will re-establish the new domain.
413 if (cc && status != ZPCI_PCI_ST_FUNC_NOT_AVAIL)
416 zdev->dma_table = s390_domain->dma_table;
417 zdev->s390_domain = s390_domain;
419 spin_lock_irqsave(&s390_domain->list_lock, flags);
420 list_add_rcu(&zdev->iommu_list, &s390_domain->devices);
421 spin_unlock_irqrestore(&s390_domain->list_lock, flags);
426 static void s390_iommu_get_resv_regions(struct device *dev,
427 struct list_head *list)
429 struct zpci_dev *zdev = to_zpci_dev(dev);
430 struct iommu_resv_region *region;
432 if (zdev->start_dma) {
433 region = iommu_alloc_resv_region(0, zdev->start_dma, 0,
434 IOMMU_RESV_RESERVED, GFP_KERNEL);
437 list_add_tail(®ion->list, list);
440 if (zdev->end_dma < ZPCI_TABLE_SIZE_RT - 1) {
441 region = iommu_alloc_resv_region(zdev->end_dma + 1,
442 ZPCI_TABLE_SIZE_RT - zdev->end_dma - 1,
443 0, IOMMU_RESV_RESERVED, GFP_KERNEL);
446 list_add_tail(®ion->list, list);
450 static struct iommu_device *s390_iommu_probe_device(struct device *dev)
452 struct zpci_dev *zdev;
454 if (!dev_is_pci(dev))
455 return ERR_PTR(-ENODEV);
457 zdev = to_zpci_dev(dev);
459 if (zdev->start_dma > zdev->end_dma ||
460 zdev->start_dma > ZPCI_TABLE_SIZE_RT - 1)
461 return ERR_PTR(-EINVAL);
463 if (zdev->end_dma > ZPCI_TABLE_SIZE_RT - 1)
464 zdev->end_dma = ZPCI_TABLE_SIZE_RT - 1;
466 if (zdev->tlb_refresh)
467 dev->iommu->shadow_on_flush = 1;
469 return &zdev->iommu_dev;
472 static void s390_iommu_release_device(struct device *dev)
474 struct zpci_dev *zdev = to_zpci_dev(dev);
477 * release_device is expected to detach any domain currently attached
478 * to the device, but keep it attached to other devices in the group.
481 s390_iommu_detach_device(&zdev->s390_domain->domain, dev);
484 static int zpci_refresh_all(struct zpci_dev *zdev)
486 return zpci_refresh_trans((u64)zdev->fh << 32, zdev->start_dma,
487 zdev->end_dma - zdev->start_dma + 1);
490 static void s390_iommu_flush_iotlb_all(struct iommu_domain *domain)
492 struct s390_domain *s390_domain = to_s390_domain(domain);
493 struct zpci_dev *zdev;
496 list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) {
497 atomic64_inc(&s390_domain->ctrs.global_rpcits);
498 zpci_refresh_all(zdev);
503 static void s390_iommu_iotlb_sync(struct iommu_domain *domain,
504 struct iommu_iotlb_gather *gather)
506 struct s390_domain *s390_domain = to_s390_domain(domain);
507 size_t size = gather->end - gather->start + 1;
508 struct zpci_dev *zdev;
510 /* If gather was never added to there is nothing to flush */
515 list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) {
516 atomic64_inc(&s390_domain->ctrs.sync_rpcits);
517 zpci_refresh_trans((u64)zdev->fh << 32, gather->start,
523 static int s390_iommu_iotlb_sync_map(struct iommu_domain *domain,
524 unsigned long iova, size_t size)
526 struct s390_domain *s390_domain = to_s390_domain(domain);
527 struct zpci_dev *zdev;
531 list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) {
532 if (!zdev->tlb_refresh)
534 atomic64_inc(&s390_domain->ctrs.sync_map_rpcits);
535 ret = zpci_refresh_trans((u64)zdev->fh << 32,
538 * let the hypervisor discover invalidated entries
539 * allowing it to free IOVAs and unpin pages
541 if (ret == -ENOMEM) {
542 ret = zpci_refresh_all(zdev);
552 static int s390_iommu_validate_trans(struct s390_domain *s390_domain,
553 phys_addr_t pa, dma_addr_t dma_addr,
554 unsigned long nr_pages, int flags,
557 phys_addr_t page_addr = pa & PAGE_MASK;
558 unsigned long *entry;
562 for (i = 0; i < nr_pages; i++) {
563 entry = dma_walk_cpu_trans(s390_domain->dma_table, dma_addr,
565 if (unlikely(!entry)) {
569 dma_update_cpu_trans(entry, page_addr, flags);
570 page_addr += PAGE_SIZE;
571 dma_addr += PAGE_SIZE;
578 dma_addr -= PAGE_SIZE;
579 entry = dma_walk_cpu_trans(s390_domain->dma_table,
583 dma_update_cpu_trans(entry, 0, ZPCI_PTE_INVALID);
589 static int s390_iommu_invalidate_trans(struct s390_domain *s390_domain,
590 dma_addr_t dma_addr, unsigned long nr_pages)
592 unsigned long *entry;
596 for (i = 0; i < nr_pages; i++) {
597 entry = dma_walk_cpu_trans(s390_domain->dma_table, dma_addr,
599 if (unlikely(!entry)) {
603 dma_update_cpu_trans(entry, 0, ZPCI_PTE_INVALID);
604 dma_addr += PAGE_SIZE;
610 static int s390_iommu_map_pages(struct iommu_domain *domain,
611 unsigned long iova, phys_addr_t paddr,
612 size_t pgsize, size_t pgcount,
613 int prot, gfp_t gfp, size_t *mapped)
615 struct s390_domain *s390_domain = to_s390_domain(domain);
616 size_t size = pgcount << __ffs(pgsize);
617 int flags = ZPCI_PTE_VALID, rc = 0;
622 if (iova < s390_domain->domain.geometry.aperture_start ||
623 (iova + size - 1) > s390_domain->domain.geometry.aperture_end)
626 if (!IS_ALIGNED(iova | paddr, pgsize))
629 if (!(prot & IOMMU_WRITE))
630 flags |= ZPCI_TABLE_PROTECTED;
632 rc = s390_iommu_validate_trans(s390_domain, paddr, iova,
633 pgcount, flags, gfp);
636 atomic64_add(pgcount, &s390_domain->ctrs.mapped_pages);
642 static phys_addr_t s390_iommu_iova_to_phys(struct iommu_domain *domain,
645 struct s390_domain *s390_domain = to_s390_domain(domain);
646 unsigned long *rto, *sto, *pto;
647 unsigned long ste, pte, rte;
648 unsigned int rtx, sx, px;
649 phys_addr_t phys = 0;
651 if (iova < domain->geometry.aperture_start ||
652 iova > domain->geometry.aperture_end)
655 rtx = calc_rtx(iova);
658 rto = s390_domain->dma_table;
660 rte = READ_ONCE(rto[rtx]);
661 if (reg_entry_isvalid(rte)) {
662 sto = get_rt_sto(rte);
663 ste = READ_ONCE(sto[sx]);
664 if (reg_entry_isvalid(ste)) {
665 pto = get_st_pto(ste);
666 pte = READ_ONCE(pto[px]);
667 if (pt_entry_isvalid(pte))
668 phys = pte & ZPCI_PTE_ADDR_MASK;
675 static size_t s390_iommu_unmap_pages(struct iommu_domain *domain,
677 size_t pgsize, size_t pgcount,
678 struct iommu_iotlb_gather *gather)
680 struct s390_domain *s390_domain = to_s390_domain(domain);
681 size_t size = pgcount << __ffs(pgsize);
684 if (WARN_ON(iova < s390_domain->domain.geometry.aperture_start ||
685 (iova + size - 1) > s390_domain->domain.geometry.aperture_end))
688 rc = s390_iommu_invalidate_trans(s390_domain, iova, pgcount);
692 iommu_iotlb_gather_add_range(gather, iova, size);
693 atomic64_add(pgcount, &s390_domain->ctrs.unmapped_pages);
698 static void s390_iommu_probe_finalize(struct device *dev)
700 iommu_setup_dma_ops(dev, 0, U64_MAX);
703 struct zpci_iommu_ctrs *zpci_get_iommu_ctrs(struct zpci_dev *zdev)
705 if (!zdev || !zdev->s390_domain)
707 return &zdev->s390_domain->ctrs;
710 int zpci_init_iommu(struct zpci_dev *zdev)
715 rc = iommu_device_sysfs_add(&zdev->iommu_dev, NULL, NULL,
716 "s390-iommu.%08x", zdev->fid);
720 rc = iommu_device_register(&zdev->iommu_dev, &s390_iommu_ops, NULL);
724 zdev->start_dma = PAGE_ALIGN(zdev->start_dma);
725 aperture_size = min3(s390_iommu_aperture,
726 ZPCI_TABLE_SIZE_RT - zdev->start_dma,
727 zdev->end_dma - zdev->start_dma + 1);
728 zdev->end_dma = zdev->start_dma + aperture_size - 1;
733 iommu_device_sysfs_remove(&zdev->iommu_dev);
739 void zpci_destroy_iommu(struct zpci_dev *zdev)
741 iommu_device_unregister(&zdev->iommu_dev);
742 iommu_device_sysfs_remove(&zdev->iommu_dev);
745 static int __init s390_iommu_setup(char *str)
747 if (!strcmp(str, "strict")) {
748 pr_warn("s390_iommu=strict deprecated; use iommu.strict=1 instead\n");
749 iommu_set_dma_strict();
754 __setup("s390_iommu=", s390_iommu_setup);
756 static int __init s390_iommu_aperture_setup(char *str)
758 if (kstrtou32(str, 10, &s390_iommu_aperture_factor))
759 s390_iommu_aperture_factor = 1;
763 __setup("s390_iommu_aperture=", s390_iommu_aperture_setup);
765 static int __init s390_iommu_init(void)
769 iommu_dma_forcedac = true;
770 s390_iommu_aperture = (u64)virt_to_phys(high_memory);
771 if (!s390_iommu_aperture_factor)
772 s390_iommu_aperture = ULONG_MAX;
774 s390_iommu_aperture *= s390_iommu_aperture_factor;
776 rc = dma_alloc_cpu_table_caches();
782 subsys_initcall(s390_iommu_init);
784 static const struct iommu_ops s390_iommu_ops = {
785 .capable = s390_iommu_capable,
786 .domain_alloc_paging = s390_domain_alloc_paging,
787 .probe_device = s390_iommu_probe_device,
788 .probe_finalize = s390_iommu_probe_finalize,
789 .release_device = s390_iommu_release_device,
790 .device_group = generic_device_group,
791 .pgsize_bitmap = SZ_4K,
792 .get_resv_regions = s390_iommu_get_resv_regions,
793 .default_domain_ops = &(const struct iommu_domain_ops) {
794 .attach_dev = s390_iommu_attach_device,
795 .map_pages = s390_iommu_map_pages,
796 .unmap_pages = s390_iommu_unmap_pages,
797 .flush_iotlb_all = s390_iommu_flush_iotlb_all,
798 .iotlb_sync = s390_iommu_iotlb_sync,
799 .iotlb_sync_map = s390_iommu_iotlb_sync_map,
800 .iova_to_phys = s390_iommu_iova_to_phys,
801 .free = s390_domain_free,