Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'iommu-updates-v6.7' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu

Pull iommu updates from Joerg Roedel:
"Core changes:
- Make default-domains mandatory for all IOMMU drivers
- Remove group refcounting
- Add generic_single_device_group() helper and consolidate drivers
- Cleanup map/unmap ops
- Scaling improvements for the IOVA rcache depot
- Convert dart & iommufd to the new domain_alloc_paging()

ARM-SMMU:
- Device-tree binding update:
- Add qcom,sm7150-smmu-v2 for Adreno on SM7150 SoC
- SMMUv2:
- Support for Qualcomm SDM670 (MDSS) and SM7150 SoCs
- SMMUv3:
- Large refactoring of the context descriptor code to move the CD
table into the master, paving the way for '->set_dev_pasid()'
support on non-SVA domains
- Minor cleanups to the SVA code

Intel VT-d:
- Enable debugfs to dump domain attached to a pasid
- Remove an unnecessary inline function

AMD IOMMU:
- Initial patches for SVA support (not complete yet)

S390 IOMMU:
- DMA-API conversion and optimized IOTLB flushing

And some smaller fixes and improvements"

* tag 'iommu-updates-v6.7' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu: (102 commits)
iommu/dart: Remove the force_bypass variable
iommu/dart: Call apple_dart_finalize_domain() as part of alloc_paging()
iommu/dart: Convert to domain_alloc_paging()
iommu/dart: Move the blocked domain support to a global static
iommu/dart: Use static global identity domains
iommufd: Convert to alloc_domain_paging()
iommu/vt-d: Use ops->blocked_domain
iommu/vt-d: Update the definition of the blocking domain
iommu: Move IOMMU_DOMAIN_BLOCKED global statics to ops->blocked_domain
Revert "iommu/vt-d: Remove unused function"
iommu/amd: Remove DMA_FQ type from domain allocation path
iommu: change iommu_map_sgtable to return signed values
iommu/virtio: Add __counted_by for struct viommu_request and use struct_size()
iommu/vt-d: debugfs: Support dumping a specified page table
iommu/vt-d: debugfs: Create/remove debugfs file per {device, pasid}
iommu/vt-d: debugfs: Dump entry pointing to huge page
iommu/vt-d: Remove unused function
iommu/arm-smmu-v3-sva: Remove bond refcount
iommu/arm-smmu-v3-sva: Remove unused iommu_sva handle
iommu/arm-smmu-v3: Rename cdcfg to cd_table
...

+2148 -3918
+5 -4
Documentation/admin-guide/kernel-parameters.txt
··· 2227 2227 forcing Dual Address Cycle for PCI cards supporting 2228 2228 greater than 32-bit addressing. 2229 2229 2230 - iommu.strict= [ARM64, X86] Configure TLB invalidation behaviour 2230 + iommu.strict= [ARM64, X86, S390] Configure TLB invalidation behaviour 2231 2231 Format: { "0" | "1" } 2232 2232 0 - Lazy mode. 2233 2233 Request that DMA unmap operations use deferred ··· 5694 5694 s390_iommu= [HW,S390] 5695 5695 Set s390 IOTLB flushing mode 5696 5696 strict 5697 - With strict flushing every unmap operation will result in 5698 - an IOTLB flush. Default is lazy flushing before reuse, 5699 - which is faster. 5697 + With strict flushing every unmap operation will result 5698 + in an IOTLB flush. Default is lazy flushing before 5699 + reuse, which is faster. Deprecated, equivalent to 5700 + iommu.strict=1. 5700 5701 5701 5702 s390_iommu_aperture= [KNL,S390] 5702 5703 Specifies the size of the per device DMA address space
+2
Documentation/devicetree/bindings/iommu/arm,smmu.yaml
··· 110 110 - qcom,sdm630-smmu-v2 111 111 - qcom,sdm845-smmu-v2 112 112 - qcom,sm6350-smmu-v2 113 + - qcom,sm7150-smmu-v2 113 114 - const: qcom,adreno-smmu 114 115 - const: qcom,smmu-v2 115 116 - description: Qcom Adreno GPUs on Google Cheza platform ··· 410 409 contains: 411 410 enum: 412 411 - qcom,sm6350-smmu-v2 412 + - qcom,sm7150-smmu-v2 413 413 - qcom,sm8150-smmu-500 414 414 - qcom,sm8250-smmu-500 415 415 then:
-1
arch/arm/configs/multi_v7_defconfig
··· 1076 1076 CONFIG_OMAP_IOMMU=y 1077 1077 CONFIG_OMAP_IOMMU_DEBUG=y 1078 1078 CONFIG_ROCKCHIP_IOMMU=y 1079 - CONFIG_TEGRA_IOMMU_GART=y 1080 1079 CONFIG_TEGRA_IOMMU_SMMU=y 1081 1080 CONFIG_EXYNOS_IOMMU=y 1082 1081 CONFIG_QCOM_IOMMU=y
-1
arch/arm/configs/tegra_defconfig
··· 292 292 CONFIG_CROS_EC=y 293 293 CONFIG_CROS_EC_I2C=m 294 294 CONFIG_CROS_EC_SPI=m 295 - CONFIG_TEGRA_IOMMU_GART=y 296 295 CONFIG_TEGRA_IOMMU_SMMU=y 297 296 CONFIG_ARCH_TEGRA_2x_SOC=y 298 297 CONFIG_ARCH_TEGRA_3x_SOC=y
+24 -29
arch/powerpc/kernel/iommu.c
··· 1280 1280 /* 1281 1281 * A simple iommu_ops to allow less cruft in generic VFIO code. 1282 1282 */ 1283 - static int spapr_tce_blocking_iommu_attach_dev(struct iommu_domain *dom, 1284 - struct device *dev) 1283 + static int 1284 + spapr_tce_platform_iommu_attach_dev(struct iommu_domain *platform_domain, 1285 + struct device *dev) 1285 1286 { 1287 + struct iommu_domain *domain = iommu_get_domain_for_dev(dev); 1286 1288 struct iommu_group *grp = iommu_group_get(dev); 1287 1289 struct iommu_table_group *table_group; 1288 1290 int ret = -EINVAL; 1291 + 1292 + /* At first attach the ownership is already set */ 1293 + if (!domain) 1294 + return 0; 1289 1295 1290 1296 if (!grp) 1291 1297 return -ENODEV; ··· 1303 1297 return ret; 1304 1298 } 1305 1299 1306 - static void spapr_tce_blocking_iommu_set_platform_dma(struct device *dev) 1307 - { 1308 - struct iommu_group *grp = iommu_group_get(dev); 1309 - struct iommu_table_group *table_group; 1300 + static const struct iommu_domain_ops spapr_tce_platform_domain_ops = { 1301 + .attach_dev = spapr_tce_platform_iommu_attach_dev, 1302 + }; 1310 1303 1311 - table_group = iommu_group_get_iommudata(grp); 1312 - table_group->ops->release_ownership(table_group); 1313 - } 1304 + static struct iommu_domain spapr_tce_platform_domain = { 1305 + .type = IOMMU_DOMAIN_PLATFORM, 1306 + .ops = &spapr_tce_platform_domain_ops, 1307 + }; 1314 1308 1315 - static const struct iommu_domain_ops spapr_tce_blocking_domain_ops = { 1316 - .attach_dev = spapr_tce_blocking_iommu_attach_dev, 1309 + static struct iommu_domain spapr_tce_blocked_domain = { 1310 + .type = IOMMU_DOMAIN_BLOCKED, 1311 + /* 1312 + * FIXME: SPAPR mixes blocked and platform behaviors, the blocked domain 1313 + * also sets the dma_api ops 1314 + */ 1315 + .ops = &spapr_tce_platform_domain_ops, 1317 1316 }; 1318 1317 1319 1318 static bool spapr_tce_iommu_capable(struct device *dev, enum iommu_cap cap) ··· 1331 1320 } 1332 1321 1333 1322 return false; 1334 - } 1335 - 1336 - static struct iommu_domain *spapr_tce_iommu_domain_alloc(unsigned int type) 1337 - { 1338 - struct iommu_domain *dom; 1339 - 1340 - if (type != IOMMU_DOMAIN_BLOCKED) 1341 - return NULL; 1342 - 1343 - dom = kzalloc(sizeof(*dom), GFP_KERNEL); 1344 - if (!dom) 1345 - return NULL; 1346 - 1347 - dom->ops = &spapr_tce_blocking_domain_ops; 1348 - 1349 - return dom; 1350 1323 } 1351 1324 1352 1325 static struct iommu_device *spapr_tce_iommu_probe_device(struct device *dev) ··· 1366 1371 } 1367 1372 1368 1373 static const struct iommu_ops spapr_tce_iommu_ops = { 1374 + .default_domain = &spapr_tce_platform_domain, 1375 + .blocked_domain = &spapr_tce_blocked_domain, 1369 1376 .capable = spapr_tce_iommu_capable, 1370 - .domain_alloc = spapr_tce_iommu_domain_alloc, 1371 1377 .probe_device = spapr_tce_iommu_probe_device, 1372 1378 .release_device = spapr_tce_iommu_release_device, 1373 1379 .device_group = spapr_tce_iommu_device_group, 1374 - .set_platform_dma_ops = spapr_tce_blocking_iommu_set_platform_dma, 1375 1380 }; 1376 1381 1377 1382 static struct attribute *spapr_tce_iommu_attrs[] = {
-11
arch/s390/include/asm/pci.h
··· 159 159 unsigned long *dma_table; 160 160 int tlb_refresh; 161 161 162 - spinlock_t iommu_bitmap_lock; 163 - unsigned long *iommu_bitmap; 164 - unsigned long *lazy_bitmap; 165 - unsigned long iommu_size; 166 - unsigned long iommu_pages; 167 - unsigned int next_bit; 168 - 169 162 struct iommu_device iommu_dev; /* IOMMU core handle */ 170 163 171 164 char res_name[16]; ··· 173 180 struct zpci_fmb *fmb; 174 181 u16 fmb_update; /* update interval */ 175 182 u16 fmb_length; 176 - /* software counters */ 177 - atomic64_t allocated_pages; 178 - atomic64_t mapped_pages; 179 - atomic64_t unmapped_pages; 180 183 181 184 u8 version; 182 185 enum pci_bus_speed max_bus_speed;
+3
arch/s390/include/asm/pci_clp.h
··· 50 50 #define CLP_UTIL_STR_LEN 64 51 51 #define CLP_PFIP_NR_SEGMENTS 4 52 52 53 + /* PCI function type numbers */ 54 + #define PCI_FUNC_TYPE_ISM 0x5 /* ISM device */ 55 + 53 56 extern bool zpci_unique_uid; 54 57 55 58 struct clp_rsp_slpc_pci {
+9 -110
arch/s390/include/asm/pci_dma.h
··· 82 82 #define ZPCI_TABLE_VALID_MASK 0x20 83 83 #define ZPCI_TABLE_PROT_MASK 0x200 84 84 85 - static inline unsigned int calc_rtx(dma_addr_t ptr) 86 - { 87 - return ((unsigned long) ptr >> ZPCI_RT_SHIFT) & ZPCI_INDEX_MASK; 88 - } 85 + struct zpci_iommu_ctrs { 86 + atomic64_t mapped_pages; 87 + atomic64_t unmapped_pages; 88 + atomic64_t global_rpcits; 89 + atomic64_t sync_map_rpcits; 90 + atomic64_t sync_rpcits; 91 + }; 89 92 90 - static inline unsigned int calc_sx(dma_addr_t ptr) 91 - { 92 - return ((unsigned long) ptr >> ZPCI_ST_SHIFT) & ZPCI_INDEX_MASK; 93 - } 93 + struct zpci_dev; 94 94 95 - static inline unsigned int calc_px(dma_addr_t ptr) 96 - { 97 - return ((unsigned long) ptr >> PAGE_SHIFT) & ZPCI_PT_MASK; 98 - } 99 - 100 - static inline void set_pt_pfaa(unsigned long *entry, phys_addr_t pfaa) 101 - { 102 - *entry &= ZPCI_PTE_FLAG_MASK; 103 - *entry |= (pfaa & ZPCI_PTE_ADDR_MASK); 104 - } 105 - 106 - static inline void set_rt_sto(unsigned long *entry, phys_addr_t sto) 107 - { 108 - *entry &= ZPCI_RTE_FLAG_MASK; 109 - *entry |= (sto & ZPCI_RTE_ADDR_MASK); 110 - *entry |= ZPCI_TABLE_TYPE_RTX; 111 - } 112 - 113 - static inline void set_st_pto(unsigned long *entry, phys_addr_t pto) 114 - { 115 - *entry &= ZPCI_STE_FLAG_MASK; 116 - *entry |= (pto & ZPCI_STE_ADDR_MASK); 117 - *entry |= ZPCI_TABLE_TYPE_SX; 118 - } 119 - 120 - static inline void validate_rt_entry(unsigned long *entry) 121 - { 122 - *entry &= ~ZPCI_TABLE_VALID_MASK; 123 - *entry &= ~ZPCI_TABLE_OFFSET_MASK; 124 - *entry |= ZPCI_TABLE_VALID; 125 - *entry |= ZPCI_TABLE_LEN_RTX; 126 - } 127 - 128 - static inline void validate_st_entry(unsigned long *entry) 129 - { 130 - *entry &= ~ZPCI_TABLE_VALID_MASK; 131 - *entry |= ZPCI_TABLE_VALID; 132 - } 133 - 134 - static inline void invalidate_pt_entry(unsigned long *entry) 135 - { 136 - WARN_ON_ONCE((*entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_INVALID); 137 - *entry &= ~ZPCI_PTE_VALID_MASK; 138 - *entry |= ZPCI_PTE_INVALID; 139 - } 140 - 141 - static inline void validate_pt_entry(unsigned long *entry) 142 - { 143 - WARN_ON_ONCE((*entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID); 144 - *entry &= ~ZPCI_PTE_VALID_MASK; 145 - *entry |= ZPCI_PTE_VALID; 146 - } 147 - 148 - static inline void entry_set_protected(unsigned long *entry) 149 - { 150 - *entry &= ~ZPCI_TABLE_PROT_MASK; 151 - *entry |= ZPCI_TABLE_PROTECTED; 152 - } 153 - 154 - static inline void entry_clr_protected(unsigned long *entry) 155 - { 156 - *entry &= ~ZPCI_TABLE_PROT_MASK; 157 - *entry |= ZPCI_TABLE_UNPROTECTED; 158 - } 159 - 160 - static inline int reg_entry_isvalid(unsigned long entry) 161 - { 162 - return (entry & ZPCI_TABLE_VALID_MASK) == ZPCI_TABLE_VALID; 163 - } 164 - 165 - static inline int pt_entry_isvalid(unsigned long entry) 166 - { 167 - return (entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID; 168 - } 169 - 170 - static inline unsigned long *get_rt_sto(unsigned long entry) 171 - { 172 - if ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_RTX) 173 - return phys_to_virt(entry & ZPCI_RTE_ADDR_MASK); 174 - else 175 - return NULL; 176 - 177 - } 178 - 179 - static inline unsigned long *get_st_pto(unsigned long entry) 180 - { 181 - if ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_SX) 182 - return phys_to_virt(entry & ZPCI_STE_ADDR_MASK); 183 - else 184 - return NULL; 185 - } 186 - 187 - /* Prototypes */ 188 - void dma_free_seg_table(unsigned long); 189 - unsigned long *dma_alloc_cpu_table(gfp_t gfp); 190 - void dma_cleanup_tables(unsigned long *); 191 - unsigned long *dma_walk_cpu_trans(unsigned long *rto, dma_addr_t dma_addr, 192 - gfp_t gfp); 193 - void dma_update_cpu_trans(unsigned long *entry, phys_addr_t page_addr, int flags); 194 - 195 - extern const struct dma_map_ops s390_pci_dma_ops; 196 - 95 + struct zpci_iommu_ctrs *zpci_get_iommu_ctrs(struct zpci_dev *zdev); 197 96 198 97 #endif
+1 -1
arch/s390/pci/Makefile
··· 3 3 # Makefile for the s390 PCI subsystem. 4 4 # 5 5 6 - obj-$(CONFIG_PCI) += pci.o pci_irq.o pci_dma.o pci_clp.o pci_sysfs.o \ 6 + obj-$(CONFIG_PCI) += pci.o pci_irq.o pci_clp.o pci_sysfs.o \ 7 7 pci_event.o pci_debug.o pci_insn.o pci_mmio.o \ 8 8 pci_bus.o pci_kvm_hook.o 9 9 obj-$(CONFIG_PCI_IOV) += pci_iov.o
+15 -20
arch/s390/pci/pci.c
··· 124 124 125 125 WARN_ON_ONCE(iota & 0x3fff); 126 126 fib.pba = base; 127 - fib.pal = limit; 127 + /* Work around off by one in ISM virt device */ 128 + if (zdev->pft == PCI_FUNC_TYPE_ISM && limit > base) 129 + fib.pal = limit + (1 << 12); 130 + else 131 + fib.pal = limit; 128 132 fib.iota = iota | ZPCI_IOTA_RTTO_FLAG; 129 133 fib.gd = zdev->gisa; 130 134 cc = zpci_mod_fc(req, &fib, status); ··· 157 153 int zpci_fmb_enable_device(struct zpci_dev *zdev) 158 154 { 159 155 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE); 156 + struct zpci_iommu_ctrs *ctrs; 160 157 struct zpci_fib fib = {0}; 161 158 u8 cc, status; 162 159 ··· 170 165 WARN_ON((u64) zdev->fmb & 0xf); 171 166 172 167 /* reset software counters */ 173 - atomic64_set(&zdev->allocated_pages, 0); 174 - atomic64_set(&zdev->mapped_pages, 0); 175 - atomic64_set(&zdev->unmapped_pages, 0); 168 + ctrs = zpci_get_iommu_ctrs(zdev); 169 + if (ctrs) { 170 + atomic64_set(&ctrs->mapped_pages, 0); 171 + atomic64_set(&ctrs->unmapped_pages, 0); 172 + atomic64_set(&ctrs->global_rpcits, 0); 173 + atomic64_set(&ctrs->sync_map_rpcits, 0); 174 + atomic64_set(&ctrs->sync_rpcits, 0); 175 + } 176 + 176 177 177 178 fib.fmb_addr = virt_to_phys(zdev->fmb); 178 179 fib.gd = zdev->gisa; ··· 593 582 pdev->no_vf_scan = 1; 594 583 595 584 pdev->dev.groups = zpci_attr_groups; 596 - pdev->dev.dma_ops = &s390_pci_dma_ops; 597 585 zpci_map_resources(pdev); 598 586 599 587 for (i = 0; i < PCI_STD_NUM_BARS; i++) { ··· 766 756 if (zdev->dma_table) 767 757 rc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, 768 758 virt_to_phys(zdev->dma_table), &status); 769 - else 770 - rc = zpci_dma_init_device(zdev); 771 759 if (rc) { 772 760 zpci_disable_device(zdev); 773 761 return rc; ··· 873 865 if (zdev->zbus->bus) 874 866 zpci_bus_remove_device(zdev, false); 875 867 876 - if (zdev->dma_table) { 877 - rc = zpci_dma_exit_device(zdev); 878 - if (rc) 879 - return rc; 880 - } 881 868 if (zdev_enabled(zdev)) { 882 869 rc = zpci_disable_device(zdev); 883 870 if (rc) ··· 921 918 if (zdev->zbus->bus) 922 919 zpci_bus_remove_device(zdev, false); 923 920 924 - if (zdev->dma_table) 925 - zpci_dma_exit_device(zdev); 926 921 if (zdev_enabled(zdev)) 927 922 zpci_disable_device(zdev); 928 923 ··· 1110 1109 if (rc) 1111 1110 goto out_irq; 1112 1111 1113 - rc = zpci_dma_init(); 1114 - if (rc) 1115 - goto out_dma; 1116 - 1117 1112 rc = clp_scan_pci_devices(); 1118 1113 if (rc) 1119 1114 goto out_find; ··· 1119 1122 return 0; 1120 1123 1121 1124 out_find: 1122 - zpci_dma_exit(); 1123 - out_dma: 1124 1125 zpci_irq_exit(); 1125 1126 out_irq: 1126 1127 zpci_mem_exit();
-5
arch/s390/pci/pci_bus.c
··· 47 47 rc = zpci_enable_device(zdev); 48 48 if (rc) 49 49 return rc; 50 - rc = zpci_dma_init_device(zdev); 51 - if (rc) { 52 - zpci_disable_device(zdev); 53 - return rc; 54 - } 55 50 } 56 51 57 52 if (!zdev->has_resources) {
+9 -3
arch/s390/pci/pci_debug.c
··· 53 53 }; 54 54 55 55 static char *pci_sw_names[] = { 56 - "Allocated pages", 57 56 "Mapped pages", 58 57 "Unmapped pages", 58 + "Global RPCITs", 59 + "Sync Map RPCITs", 60 + "Sync RPCITs", 59 61 }; 60 62 61 63 static void pci_fmb_show(struct seq_file *m, char *name[], int length, ··· 71 69 72 70 static void pci_sw_counter_show(struct seq_file *m) 73 71 { 74 - struct zpci_dev *zdev = m->private; 75 - atomic64_t *counter = &zdev->allocated_pages; 72 + struct zpci_iommu_ctrs *ctrs = zpci_get_iommu_ctrs(m->private); 73 + atomic64_t *counter; 76 74 int i; 77 75 76 + if (!ctrs) 77 + return; 78 + 79 + counter = &ctrs->mapped_pages; 78 80 for (i = 0; i < ARRAY_SIZE(pci_sw_names); i++, counter++) 79 81 seq_printf(m, "%26s:\t%llu\n", pci_sw_names[i], 80 82 atomic64_read(counter));
-746
arch/s390/pci/pci_dma.c
··· 1 - // SPDX-License-Identifier: GPL-2.0 2 - /* 3 - * Copyright IBM Corp. 2012 4 - * 5 - * Author(s): 6 - * Jan Glauber <jang@linux.vnet.ibm.com> 7 - */ 8 - 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> 17 - 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; 23 - 24 - static int zpci_refresh_global(struct zpci_dev *zdev) 25 - { 26 - return zpci_refresh_trans((u64) zdev->fh << 32, zdev->start_dma, 27 - zdev->iommu_pages * PAGE_SIZE); 28 - } 29 - 30 - unsigned long *dma_alloc_cpu_table(gfp_t gfp) 31 - { 32 - unsigned long *table, *entry; 33 - 34 - table = kmem_cache_alloc(dma_region_table_cache, gfp); 35 - if (!table) 36 - return NULL; 37 - 38 - for (entry = table; entry < table + ZPCI_TABLE_ENTRIES; entry++) 39 - *entry = ZPCI_TABLE_INVALID; 40 - return table; 41 - } 42 - 43 - static void dma_free_cpu_table(void *table) 44 - { 45 - kmem_cache_free(dma_region_table_cache, table); 46 - } 47 - 48 - static unsigned long *dma_alloc_page_table(gfp_t gfp) 49 - { 50 - unsigned long *table, *entry; 51 - 52 - table = kmem_cache_alloc(dma_page_table_cache, gfp); 53 - if (!table) 54 - return NULL; 55 - 56 - for (entry = table; entry < table + ZPCI_PT_ENTRIES; entry++) 57 - *entry = ZPCI_PTE_INVALID; 58 - return table; 59 - } 60 - 61 - static void dma_free_page_table(void *table) 62 - { 63 - kmem_cache_free(dma_page_table_cache, table); 64 - } 65 - 66 - static unsigned long *dma_get_seg_table_origin(unsigned long *rtep, gfp_t gfp) 67 - { 68 - unsigned long old_rte, rte; 69 - unsigned long *sto; 70 - 71 - rte = READ_ONCE(*rtep); 72 - if (reg_entry_isvalid(rte)) { 73 - sto = get_rt_sto(rte); 74 - } else { 75 - sto = dma_alloc_cpu_table(gfp); 76 - if (!sto) 77 - return NULL; 78 - 79 - set_rt_sto(&rte, virt_to_phys(sto)); 80 - validate_rt_entry(&rte); 81 - entry_clr_protected(&rte); 82 - 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); 88 - } 89 - } 90 - return sto; 91 - } 92 - 93 - static unsigned long *dma_get_page_table_origin(unsigned long *step, gfp_t gfp) 94 - { 95 - unsigned long old_ste, ste; 96 - unsigned long *pto; 97 - 98 - ste = READ_ONCE(*step); 99 - if (reg_entry_isvalid(ste)) { 100 - pto = get_st_pto(ste); 101 - } else { 102 - pto = dma_alloc_page_table(gfp); 103 - if (!pto) 104 - return NULL; 105 - set_st_pto(&ste, virt_to_phys(pto)); 106 - validate_st_entry(&ste); 107 - entry_clr_protected(&ste); 108 - 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); 114 - } 115 - } 116 - return pto; 117 - } 118 - 119 - unsigned long *dma_walk_cpu_trans(unsigned long *rto, dma_addr_t dma_addr, 120 - gfp_t gfp) 121 - { 122 - unsigned long *sto, *pto; 123 - unsigned int rtx, sx, px; 124 - 125 - rtx = calc_rtx(dma_addr); 126 - sto = dma_get_seg_table_origin(&rto[rtx], gfp); 127 - if (!sto) 128 - return NULL; 129 - 130 - sx = calc_sx(dma_addr); 131 - pto = dma_get_page_table_origin(&sto[sx], gfp); 132 - if (!pto) 133 - return NULL; 134 - 135 - px = calc_px(dma_addr); 136 - return &pto[px]; 137 - } 138 - 139 - void dma_update_cpu_trans(unsigned long *ptep, phys_addr_t page_addr, int flags) 140 - { 141 - unsigned long pte; 142 - 143 - pte = READ_ONCE(*ptep); 144 - if (flags & ZPCI_PTE_INVALID) { 145 - invalidate_pt_entry(&pte); 146 - } else { 147 - set_pt_pfaa(&pte, page_addr); 148 - validate_pt_entry(&pte); 149 - } 150 - 151 - if (flags & ZPCI_TABLE_PROTECTED) 152 - entry_set_protected(&pte); 153 - else 154 - entry_clr_protected(&pte); 155 - 156 - xchg(ptep, pte); 157 - } 158 - 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) 161 - { 162 - unsigned int nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; 163 - phys_addr_t page_addr = (pa & PAGE_MASK); 164 - unsigned long *entry; 165 - int i, rc = 0; 166 - 167 - if (!nr_pages) 168 - return -EINVAL; 169 - 170 - if (!zdev->dma_table) 171 - return -EINVAL; 172 - 173 - for (i = 0; i < nr_pages; i++) { 174 - entry = dma_walk_cpu_trans(zdev->dma_table, dma_addr, 175 - GFP_ATOMIC); 176 - if (!entry) { 177 - rc = -ENOMEM; 178 - goto undo_cpu_trans; 179 - } 180 - dma_update_cpu_trans(entry, page_addr, flags); 181 - page_addr += PAGE_SIZE; 182 - dma_addr += PAGE_SIZE; 183 - } 184 - 185 - undo_cpu_trans: 186 - if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)) { 187 - flags = ZPCI_PTE_INVALID; 188 - while (i-- > 0) { 189 - page_addr -= PAGE_SIZE; 190 - dma_addr -= PAGE_SIZE; 191 - entry = dma_walk_cpu_trans(zdev->dma_table, dma_addr, 192 - GFP_ATOMIC); 193 - if (!entry) 194 - break; 195 - dma_update_cpu_trans(entry, page_addr, flags); 196 - } 197 - } 198 - return rc; 199 - } 200 - 201 - static int __dma_purge_tlb(struct zpci_dev *zdev, dma_addr_t dma_addr, 202 - size_t size, int flags) 203 - { 204 - unsigned long irqflags; 205 - int ret; 206 - 207 - /* 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. 213 - */ 214 - if ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID) { 215 - if (!zdev->tlb_refresh) 216 - return 0; 217 - } else { 218 - if (!s390_iommu_strict) 219 - return 0; 220 - } 221 - 222 - ret = zpci_refresh_trans((u64) zdev->fh << 32, dma_addr, 223 - PAGE_ALIGN(size)); 224 - if (ret == -ENOMEM && !s390_iommu_strict) { 225 - /* enable the hypervisor to free some resources */ 226 - if (zpci_refresh_global(zdev)) 227 - goto out; 228 - 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); 234 - ret = 0; 235 - } 236 - out: 237 - return ret; 238 - } 239 - 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) 242 - { 243 - int rc; 244 - 245 - rc = __dma_update_trans(zdev, pa, dma_addr, size, flags); 246 - if (rc) 247 - return rc; 248 - 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); 252 - 253 - return rc; 254 - } 255 - 256 - void dma_free_seg_table(unsigned long entry) 257 - { 258 - unsigned long *sto = get_rt_sto(entry); 259 - int sx; 260 - 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])); 264 - 265 - dma_free_cpu_table(sto); 266 - } 267 - 268 - void dma_cleanup_tables(unsigned long *table) 269 - { 270 - int rtx; 271 - 272 - if (!table) 273 - return; 274 - 275 - for (rtx = 0; rtx < ZPCI_TABLE_ENTRIES; rtx++) 276 - if (reg_entry_isvalid(table[rtx])) 277 - dma_free_seg_table(table[rtx]); 278 - 279 - dma_free_cpu_table(table); 280 - } 281 - 282 - static unsigned long __dma_alloc_iommu(struct device *dev, 283 - unsigned long start, int size) 284 - { 285 - struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); 286 - 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), 290 - 0); 291 - } 292 - 293 - static dma_addr_t dma_alloc_address(struct device *dev, int size) 294 - { 295 - struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); 296 - unsigned long offset, flags; 297 - 298 - spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags); 299 - offset = __dma_alloc_iommu(dev, zdev->next_bit, size); 300 - if (offset == -1) { 301 - if (!s390_iommu_strict) { 302 - /* global flush before DMA addresses are reused */ 303 - if (zpci_refresh_global(zdev)) 304 - goto out_error; 305 - 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); 309 - } 310 - /* wrap-around */ 311 - offset = __dma_alloc_iommu(dev, 0, size); 312 - if (offset == -1) 313 - goto out_error; 314 - } 315 - zdev->next_bit = offset + size; 316 - spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags); 317 - 318 - return zdev->start_dma + offset * PAGE_SIZE; 319 - 320 - out_error: 321 - spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags); 322 - return DMA_MAPPING_ERROR; 323 - } 324 - 325 - static void dma_free_address(struct device *dev, dma_addr_t dma_addr, int size) 326 - { 327 - struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); 328 - unsigned long flags, offset; 329 - 330 - offset = (dma_addr - zdev->start_dma) >> PAGE_SHIFT; 331 - 332 - spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags); 333 - if (!zdev->iommu_bitmap) 334 - goto out; 335 - 336 - if (s390_iommu_strict) 337 - bitmap_clear(zdev->iommu_bitmap, offset, size); 338 - else 339 - bitmap_set(zdev->lazy_bitmap, offset, size); 340 - 341 - out: 342 - spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags); 343 - } 344 - 345 - static inline void zpci_err_dma(unsigned long rc, unsigned long addr) 346 - { 347 - struct { 348 - unsigned long rc; 349 - unsigned long addr; 350 - } __packed data = {rc, addr}; 351 - 352 - zpci_err_hex(&data, sizeof(data)); 353 - } 354 - 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, 358 - unsigned long attrs) 359 - { 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; 364 - dma_addr_t dma_addr; 365 - int ret; 366 - 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) { 371 - ret = -ENOSPC; 372 - goto out_err; 373 - } 374 - 375 - /* Use rounded up size */ 376 - size = nr_pages * PAGE_SIZE; 377 - 378 - if (direction == DMA_NONE || direction == DMA_TO_DEVICE) 379 - flags |= ZPCI_TABLE_PROTECTED; 380 - 381 - ret = dma_update_trans(zdev, pa, dma_addr, size, flags); 382 - if (ret) 383 - goto out_free; 384 - 385 - atomic64_add(nr_pages, &zdev->mapped_pages); 386 - return dma_addr + (offset & ~PAGE_MASK); 387 - 388 - out_free: 389 - dma_free_address(dev, dma_addr, nr_pages); 390 - out_err: 391 - zpci_err("map error:\n"); 392 - zpci_err_dma(ret, pa); 393 - return DMA_MAPPING_ERROR; 394 - } 395 - 396 - static void s390_dma_unmap_pages(struct device *dev, dma_addr_t dma_addr, 397 - size_t size, enum dma_data_direction direction, 398 - unsigned long attrs) 399 - { 400 - struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); 401 - int npages, ret; 402 - 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, 406 - ZPCI_PTE_INVALID); 407 - if (ret) { 408 - zpci_err("unmap error:\n"); 409 - zpci_err_dma(ret, dma_addr); 410 - return; 411 - } 412 - 413 - atomic64_add(npages, &zdev->unmapped_pages); 414 - dma_free_address(dev, dma_addr, npages); 415 - } 416 - 417 - static void *s390_dma_alloc(struct device *dev, size_t size, 418 - dma_addr_t *dma_handle, gfp_t flag, 419 - unsigned long attrs) 420 - { 421 - struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); 422 - struct page *page; 423 - phys_addr_t pa; 424 - dma_addr_t map; 425 - 426 - size = PAGE_ALIGN(size); 427 - page = alloc_pages(flag | __GFP_ZERO, get_order(size)); 428 - if (!page) 429 - return NULL; 430 - 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)); 435 - return NULL; 436 - } 437 - 438 - atomic64_add(size / PAGE_SIZE, &zdev->allocated_pages); 439 - if (dma_handle) 440 - *dma_handle = map; 441 - return phys_to_virt(pa); 442 - } 443 - 444 - static void s390_dma_free(struct device *dev, size_t size, 445 - void *vaddr, dma_addr_t dma_handle, 446 - unsigned long attrs) 447 - { 448 - struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); 449 - 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)); 454 - } 455 - 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) 460 - { 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; 466 - phys_addr_t pa = 0; 467 - int ret; 468 - 469 - dma_addr_base = dma_alloc_address(dev, nr_pages); 470 - if (dma_addr_base == DMA_MAPPING_ERROR) 471 - return -ENOMEM; 472 - 473 - dma_addr = dma_addr_base; 474 - if (dir == DMA_NONE || dir == DMA_TO_DEVICE) 475 - flags |= ZPCI_TABLE_PROTECTED; 476 - 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); 481 - if (ret) 482 - goto unmap; 483 - 484 - dma_addr += s->offset + s->length; 485 - } 486 - ret = __dma_purge_tlb(zdev, dma_addr_base, size, flags); 487 - if (ret) 488 - goto unmap; 489 - 490 - *handle = dma_addr_base; 491 - atomic64_add(nr_pages, &zdev->mapped_pages); 492 - 493 - return ret; 494 - 495 - unmap: 496 - dma_update_trans(zdev, 0, dma_addr_base, dma_addr - dma_addr_base, 497 - ZPCI_PTE_INVALID); 498 - dma_free_address(dev, dma_addr_base, nr_pages); 499 - zpci_err("map error:\n"); 500 - zpci_err_dma(ret, pa); 501 - return ret; 502 - } 503 - 504 - static int s390_dma_map_sg(struct device *dev, struct scatterlist *sg, 505 - int nr_elements, enum dma_data_direction dir, 506 - unsigned long attrs) 507 - { 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; 513 - 514 - for (i = 1; i < nr_elements; i++) { 515 - s = sg_next(s); 516 - 517 - s->dma_length = 0; 518 - 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); 523 - if (ret) 524 - goto unmap; 525 - 526 - dma->dma_address += offset; 527 - dma->dma_length = size - offset; 528 - 529 - size = offset = s->offset; 530 - start = s; 531 - dma = sg_next(dma); 532 - count++; 533 - } 534 - size += s->length; 535 - } 536 - ret = __s390_dma_map_sg(dev, start, size, &dma->dma_address, dir); 537 - if (ret) 538 - goto unmap; 539 - 540 - dma->dma_address += offset; 541 - dma->dma_length = size - offset; 542 - 543 - return count + 1; 544 - unmap: 545 - for_each_sg(sg, s, count, i) 546 - s390_dma_unmap_pages(dev, sg_dma_address(s), sg_dma_len(s), 547 - dir, attrs); 548 - 549 - return ret; 550 - } 551 - 552 - static void s390_dma_unmap_sg(struct device *dev, struct scatterlist *sg, 553 - int nr_elements, enum dma_data_direction dir, 554 - unsigned long attrs) 555 - { 556 - struct scatterlist *s; 557 - int i; 558 - 559 - for_each_sg(sg, s, nr_elements, i) { 560 - if (s->dma_length) 561 - s390_dma_unmap_pages(dev, s->dma_address, s->dma_length, 562 - dir, attrs); 563 - s->dma_address = 0; 564 - s->dma_length = 0; 565 - } 566 - } 567 - 568 - static unsigned long *bitmap_vzalloc(size_t bits, gfp_t flags) 569 - { 570 - size_t n = BITS_TO_LONGS(bits); 571 - size_t bytes; 572 - 573 - if (unlikely(check_mul_overflow(n, sizeof(unsigned long), &bytes))) 574 - return NULL; 575 - 576 - return vzalloc(bytes); 577 - } 578 - 579 - int zpci_dma_init_device(struct zpci_dev *zdev) 580 - { 581 - u8 status; 582 - int rc; 583 - 584 - /* 585 - * At this point, if the device is part of an IOMMU domain, this would 586 - * be a strong hint towards a bug in the IOMMU API (common) code and/or 587 - * simultaneous access via IOMMU and DMA API. So let's issue a warning. 588 - */ 589 - WARN_ON(zdev->s390_domain); 590 - 591 - spin_lock_init(&zdev->iommu_bitmap_lock); 592 - 593 - zdev->dma_table = dma_alloc_cpu_table(GFP_KERNEL); 594 - if (!zdev->dma_table) { 595 - rc = -ENOMEM; 596 - goto out; 597 - } 598 - 599 - /* 600 - * Restrict the iommu bitmap size to the minimum of the following: 601 - * - s390_iommu_aperture which defaults to high_memory 602 - * - 3-level pagetable address limit minus start_dma offset 603 - * - DMA address range allowed by the hardware (clp query pci fn) 604 - * 605 - * Also set zdev->end_dma to the actual end address of the usable 606 - * range, instead of the theoretical maximum as reported by hardware. 607 - * 608 - * This limits the number of concurrently usable DMA mappings since 609 - * for each DMA mapped memory address we need a DMA address including 610 - * extra DMA addresses for multiple mappings of the same memory address. 611 - */ 612 - zdev->start_dma = PAGE_ALIGN(zdev->start_dma); 613 - zdev->iommu_size = min3(s390_iommu_aperture, 614 - ZPCI_TABLE_SIZE_RT - zdev->start_dma, 615 - zdev->end_dma - zdev->start_dma + 1); 616 - zdev->end_dma = zdev->start_dma + zdev->iommu_size - 1; 617 - zdev->iommu_pages = zdev->iommu_size >> PAGE_SHIFT; 618 - zdev->iommu_bitmap = bitmap_vzalloc(zdev->iommu_pages, GFP_KERNEL); 619 - if (!zdev->iommu_bitmap) { 620 - rc = -ENOMEM; 621 - goto free_dma_table; 622 - } 623 - if (!s390_iommu_strict) { 624 - zdev->lazy_bitmap = bitmap_vzalloc(zdev->iommu_pages, GFP_KERNEL); 625 - if (!zdev->lazy_bitmap) { 626 - rc = -ENOMEM; 627 - goto free_bitmap; 628 - } 629 - 630 - } 631 - if (zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, 632 - virt_to_phys(zdev->dma_table), &status)) { 633 - rc = -EIO; 634 - goto free_bitmap; 635 - } 636 - 637 - return 0; 638 - free_bitmap: 639 - vfree(zdev->iommu_bitmap); 640 - zdev->iommu_bitmap = NULL; 641 - vfree(zdev->lazy_bitmap); 642 - zdev->lazy_bitmap = NULL; 643 - free_dma_table: 644 - dma_free_cpu_table(zdev->dma_table); 645 - zdev->dma_table = NULL; 646 - out: 647 - return rc; 648 - } 649 - 650 - int zpci_dma_exit_device(struct zpci_dev *zdev) 651 - { 652 - int cc = 0; 653 - 654 - /* 655 - * At this point, if the device is part of an IOMMU domain, this would 656 - * be a strong hint towards a bug in the IOMMU API (common) code and/or 657 - * simultaneous access via IOMMU and DMA API. So let's issue a warning. 658 - */ 659 - WARN_ON(zdev->s390_domain); 660 - if (zdev_enabled(zdev)) 661 - cc = zpci_unregister_ioat(zdev, 0); 662 - /* 663 - * cc == 3 indicates the function is gone already. This can happen 664 - * if the function was deconfigured/disabled suddenly and we have not 665 - * received a new handle yet. 666 - */ 667 - if (cc && cc != 3) 668 - return -EIO; 669 - 670 - dma_cleanup_tables(zdev->dma_table); 671 - zdev->dma_table = NULL; 672 - vfree(zdev->iommu_bitmap); 673 - zdev->iommu_bitmap = NULL; 674 - vfree(zdev->lazy_bitmap); 675 - zdev->lazy_bitmap = NULL; 676 - zdev->next_bit = 0; 677 - return 0; 678 - } 679 - 680 - static int __init dma_alloc_cpu_table_caches(void) 681 - { 682 - dma_region_table_cache = kmem_cache_create("PCI_DMA_region_tables", 683 - ZPCI_TABLE_SIZE, ZPCI_TABLE_ALIGN, 684 - 0, NULL); 685 - if (!dma_region_table_cache) 686 - return -ENOMEM; 687 - 688 - dma_page_table_cache = kmem_cache_create("PCI_DMA_page_tables", 689 - ZPCI_PT_SIZE, ZPCI_PT_ALIGN, 690 - 0, NULL); 691 - if (!dma_page_table_cache) { 692 - kmem_cache_destroy(dma_region_table_cache); 693 - return -ENOMEM; 694 - } 695 - return 0; 696 - } 697 - 698 - int __init zpci_dma_init(void) 699 - { 700 - s390_iommu_aperture = (u64)virt_to_phys(high_memory); 701 - if (!s390_iommu_aperture_factor) 702 - s390_iommu_aperture = ULONG_MAX; 703 - else 704 - s390_iommu_aperture *= s390_iommu_aperture_factor; 705 - 706 - return dma_alloc_cpu_table_caches(); 707 - } 708 - 709 - void zpci_dma_exit(void) 710 - { 711 - kmem_cache_destroy(dma_page_table_cache); 712 - kmem_cache_destroy(dma_region_table_cache); 713 - } 714 - 715 - const struct dma_map_ops s390_pci_dma_ops = { 716 - .alloc = s390_dma_alloc, 717 - .free = s390_dma_free, 718 - .map_sg = s390_dma_map_sg, 719 - .unmap_sg = s390_dma_unmap_sg, 720 - .map_page = s390_dma_map_pages, 721 - .unmap_page = s390_dma_unmap_pages, 722 - .mmap = dma_common_mmap, 723 - .get_sgtable = dma_common_get_sgtable, 724 - .alloc_pages = dma_common_alloc_pages, 725 - .free_pages = dma_common_free_pages, 726 - /* dma_supported is unconditionally true without a callback */ 727 - }; 728 - EXPORT_SYMBOL_GPL(s390_pci_dma_ops); 729 - 730 - static int __init s390_iommu_setup(char *str) 731 - { 732 - if (!strcmp(str, "strict")) 733 - s390_iommu_strict = 1; 734 - return 1; 735 - } 736 - 737 - __setup("s390_iommu=", s390_iommu_setup); 738 - 739 - static int __init s390_iommu_aperture_setup(char *str) 740 - { 741 - if (kstrtou32(str, 10, &s390_iommu_aperture_factor)) 742 - s390_iommu_aperture_factor = 1; 743 - return 1; 744 - } 745 - 746 - __setup("s390_iommu_aperture=", s390_iommu_aperture_setup);
+11 -6
arch/s390/pci/pci_event.c
··· 59 59 } 60 60 } 61 61 62 - static bool is_passed_through(struct zpci_dev *zdev) 62 + static bool is_passed_through(struct pci_dev *pdev) 63 63 { 64 - return zdev->s390_domain; 64 + struct zpci_dev *zdev = to_zpci(pdev); 65 + bool ret; 66 + 67 + mutex_lock(&zdev->kzdev_lock); 68 + ret = !!zdev->kzdev; 69 + mutex_unlock(&zdev->kzdev_lock); 70 + 71 + return ret; 65 72 } 66 73 67 74 static bool is_driver_supported(struct pci_driver *driver) ··· 183 176 } 184 177 pdev->error_state = pci_channel_io_frozen; 185 178 186 - if (is_passed_through(to_zpci(pdev))) { 179 + if (is_passed_through(pdev)) { 187 180 pr_info("%s: Cannot be recovered in the host because it is a pass-through device\n", 188 181 pci_name(pdev)); 189 182 goto out_unlock; ··· 246 239 * we will inject the error event and let the guest recover the device 247 240 * itself. 248 241 */ 249 - if (is_passed_through(to_zpci(pdev))) 242 + if (is_passed_through(pdev)) 250 243 goto out; 251 244 driver = to_pci_driver(pdev->dev.driver); 252 245 if (driver && driver->err_handler && driver->err_handler->error_detected) ··· 313 306 /* Even though the device is already gone we still 314 307 * need to free zPCI resources as part of the disable. 315 308 */ 316 - if (zdev->dma_table) 317 - zpci_dma_exit_device(zdev); 318 309 if (zdev_enabled(zdev)) 319 310 zpci_disable_device(zdev); 320 311 zdev->state = ZPCI_FN_STATE_STANDBY;
+8 -11
arch/s390/pci/pci_sysfs.c
··· 56 56 struct pci_dev *pdev = to_pci_dev(dev); 57 57 struct zpci_dev *zdev = to_zpci(pdev); 58 58 int ret = 0; 59 + u8 status; 59 60 60 61 /* Can't use device_remove_self() here as that would lead us to lock 61 62 * the pci_rescan_remove_lock while holding the device' kernfs lock. ··· 83 82 pci_lock_rescan_remove(); 84 83 if (pci_dev_is_added(pdev)) { 85 84 pci_stop_and_remove_bus_device(pdev); 86 - if (zdev->dma_table) { 87 - ret = zpci_dma_exit_device(zdev); 88 - if (ret) 89 - goto out; 90 - } 91 - 92 85 if (zdev_enabled(zdev)) { 93 86 ret = zpci_disable_device(zdev); 94 87 /* ··· 100 105 ret = zpci_enable_device(zdev); 101 106 if (ret) 102 107 goto out; 103 - ret = zpci_dma_init_device(zdev); 104 - if (ret) { 105 - zpci_disable_device(zdev); 106 - goto out; 108 + 109 + if (zdev->dma_table) { 110 + ret = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, 111 + virt_to_phys(zdev->dma_table), &status); 112 + if (ret) 113 + zpci_disable_device(zdev); 107 114 } 108 - pci_rescan_bus(zdev->zbus->bus); 109 115 } 110 116 out: 117 + pci_rescan_bus(zdev->zbus->bus); 111 118 pci_unlock_rescan_remove(); 112 119 if (kn) 113 120 sysfs_unbreak_active_protection(kn);
+2 -13
drivers/iommu/Kconfig
··· 95 95 choice 96 96 prompt "IOMMU default domain type" 97 97 depends on IOMMU_API 98 - default IOMMU_DEFAULT_DMA_LAZY if X86 98 + default IOMMU_DEFAULT_DMA_LAZY if X86 || S390 99 99 default IOMMU_DEFAULT_DMA_STRICT 100 100 help 101 101 Choose the type of IOMMU domain used to manage DMA API usage by ··· 150 150 151 151 # IOMMU-agnostic DMA-mapping layer 152 152 config IOMMU_DMA 153 - def_bool ARM64 || X86 153 + def_bool ARM64 || X86 || S390 154 154 select DMA_OPS 155 155 select IOMMU_API 156 156 select IOMMU_IOVA ··· 239 239 select IOMMU_API 240 240 help 241 241 Support for the IOMMU introduced in the Allwinner H6 SoCs. 242 - 243 - config TEGRA_IOMMU_GART 244 - bool "Tegra GART IOMMU Support" 245 - depends on ARCH_TEGRA_2x_SOC 246 - depends on TEGRA_MC 247 - select IOMMU_API 248 - help 249 - Enables support for remapping discontiguous physical memory 250 - shared with the operating system into contiguous I/O virtual 251 - space through the GART (Graphics Address Relocation Table) 252 - hardware included on Tegra SoCs. 253 242 254 243 config TEGRA_IOMMU_SMMU 255 244 bool "NVIDIA Tegra SMMU Support"
-1
drivers/iommu/Makefile
··· 20 20 obj-$(CONFIG_OMAP_IOMMU_DEBUG) += omap-iommu-debug.o 21 21 obj-$(CONFIG_ROCKCHIP_IOMMU) += rockchip-iommu.o 22 22 obj-$(CONFIG_SUN50I_IOMMU) += sun50i-iommu.o 23 - obj-$(CONFIG_TEGRA_IOMMU_GART) += tegra-gart.o 24 23 obj-$(CONFIG_TEGRA_IOMMU_SMMU) += tegra-smmu.o 25 24 obj-$(CONFIG_EXYNOS_IOMMU) += exynos-iommu.o 26 25 obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o
-9
drivers/iommu/amd/Kconfig
··· 23 23 your BIOS for an option to enable it or if you have an IVRS ACPI 24 24 table. 25 25 26 - config AMD_IOMMU_V2 27 - tristate "AMD IOMMU Version 2 driver" 28 - depends on AMD_IOMMU 29 - select MMU_NOTIFIER 30 - help 31 - This option enables support for the AMD IOMMUv2 features of the IOMMU 32 - hardware. Select this option if you want to use devices that support 33 - the PCI PRI and PASID interface. 34 - 35 26 config AMD_IOMMU_DEBUGFS 36 27 bool "Enable AMD IOMMU internals in DebugFS" 37 28 depends on AMD_IOMMU && IOMMU_DEBUGFS
-1
drivers/iommu/amd/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 2 obj-$(CONFIG_AMD_IOMMU) += iommu.o init.o quirks.o io_pgtable.o io_pgtable_v2.o 3 3 obj-$(CONFIG_AMD_IOMMU_DEBUGFS) += debugfs.o 4 - obj-$(CONFIG_AMD_IOMMU_V2) += iommu_v2.o
+22 -13
drivers/iommu/amd/amd_iommu.h
··· 38 38 extern enum io_pgtable_fmt amd_iommu_pgtable; 39 39 extern int amd_iommu_gpt_level; 40 40 41 - /* IOMMUv2 specific functions */ 42 - struct iommu_domain; 43 - 44 41 bool amd_iommu_v2_supported(void); 45 42 struct amd_iommu *get_amd_iommu(unsigned int idx); 46 43 u8 amd_iommu_pc_get_max_banks(unsigned int idx); ··· 48 51 int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, 49 52 u8 fxn, u64 *value); 50 53 51 - int amd_iommu_register_ppr_notifier(struct notifier_block *nb); 52 - int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb); 53 - void amd_iommu_domain_direct_map(struct iommu_domain *dom); 54 - int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids); 54 + /* Device capabilities */ 55 + int amd_iommu_pdev_enable_cap_pri(struct pci_dev *pdev); 56 + void amd_iommu_pdev_disable_cap_pri(struct pci_dev *pdev); 57 + 55 58 int amd_iommu_flush_page(struct iommu_domain *dom, u32 pasid, u64 address); 56 59 void amd_iommu_update_and_flush_device_table(struct protection_domain *domain); 57 60 void amd_iommu_domain_update(struct protection_domain *domain); ··· 84 87 (pdev->device == PCI_DEVICE_ID_RD890_IOMMU); 85 88 } 86 89 87 - static inline bool iommu_feature(struct amd_iommu *iommu, u64 mask) 90 + static inline bool check_feature(u64 mask) 88 91 { 89 - return !!(iommu->features & mask); 92 + return (amd_iommu_efr & mask); 93 + } 94 + 95 + static inline bool check_feature2(u64 mask) 96 + { 97 + return (amd_iommu_efr2 & mask); 98 + } 99 + 100 + static inline int check_feature_gpt_level(void) 101 + { 102 + return ((amd_iommu_efr >> FEATURE_GATS_SHIFT) & FEATURE_GATS_MASK); 103 + } 104 + 105 + static inline bool amd_iommu_gt_ppr_supported(void) 106 + { 107 + return (check_feature(FEATURE_GT) && 108 + check_feature(FEATURE_PPR)); 90 109 } 91 110 92 111 static inline u64 iommu_virt_to_phys(void *vaddr) ··· 118 105 static inline 119 106 void amd_iommu_domain_set_pt_root(struct protection_domain *domain, u64 root) 120 107 { 121 - atomic64_set(&domain->iop.pt_root, root); 122 108 domain->iop.root = (u64 *)(root & PAGE_MASK); 123 109 domain->iop.mode = root & 7; /* lowest 3 bits encode pgtable mode */ 124 110 } ··· 157 145 void amd_iommu_domain_set_pgtable(struct protection_domain *domain, 158 146 u64 *root, int mode); 159 147 struct dev_table_entry *get_dev_table(struct amd_iommu *iommu); 160 - 161 - extern u64 amd_iommu_efr; 162 - extern u64 amd_iommu_efr2; 163 148 164 149 extern bool amd_iommu_snp_en; 165 150 #endif
+24 -28
drivers/iommu/amd/amd_iommu_types.h
··· 462 462 #define PD_IOMMUV2_MASK BIT(3) /* domain has gcr3 table */ 463 463 #define PD_GIOV_MASK BIT(4) /* domain enable GIOV support */ 464 464 465 + /* Timeout stuff */ 466 + #define LOOP_TIMEOUT 100000 467 + #define MMIO_STATUS_TIMEOUT 2000000 468 + 465 469 extern bool amd_iommu_dump; 466 470 #define DUMP_printk(format, arg...) \ 467 471 do { \ ··· 520 516 #define APERTURE_RANGE_INDEX(a) ((a) >> APERTURE_RANGE_SHIFT) 521 517 #define APERTURE_PAGE_INDEX(a) (((a) >> 21) & 0x3fULL) 522 518 523 - /* 524 - * This struct is used to pass information about 525 - * incoming PPR faults around. 526 - */ 527 - struct amd_iommu_fault { 528 - u64 address; /* IO virtual address of the fault*/ 529 - u32 pasid; /* Address space identifier */ 530 - u32 sbdf; /* Originating PCI device id */ 531 - u16 tag; /* PPR tag */ 532 - u16 flags; /* Fault flags */ 533 - 534 - }; 535 - 536 519 537 520 struct amd_iommu; 538 521 struct iommu_domain; ··· 546 555 struct io_pgtable iop; 547 556 int mode; 548 557 u64 *root; 549 - atomic64_t pt_root; /* pgtable root and pgtable mode */ 550 558 u64 *pgd; /* v2 pgtable pgd pointer */ 551 559 }; 552 560 ··· 678 688 /* Extended features 2 */ 679 689 u64 features2; 680 690 681 - /* IOMMUv2 */ 682 - bool is_iommu_v2; 683 - 684 691 /* PCI device id of the IOMMU device */ 685 692 u16 devid; 686 693 ··· 798 811 bool cmd_line; 799 812 }; 800 813 814 + #define AMD_IOMMU_DEVICE_FLAG_ATS_SUP 0x1 /* ATS feature supported */ 815 + #define AMD_IOMMU_DEVICE_FLAG_PRI_SUP 0x2 /* PRI feature supported */ 816 + #define AMD_IOMMU_DEVICE_FLAG_PASID_SUP 0x4 /* PASID context supported */ 817 + /* Device may request execution on memory pages */ 818 + #define AMD_IOMMU_DEVICE_FLAG_EXEC_SUP 0x8 819 + /* Device may request super-user privileges */ 820 + #define AMD_IOMMU_DEVICE_FLAG_PRIV_SUP 0x10 821 + 801 822 /* 802 823 * This struct contains device specific data for the IOMMU 803 824 */ ··· 818 823 struct protection_domain *domain; /* Domain the device is bound to */ 819 824 struct device *dev; 820 825 u16 devid; /* PCI Device ID */ 821 - bool iommu_v2; /* Device can make use of IOMMUv2 */ 822 - struct { 823 - bool enabled; 824 - int qdep; 825 - } ats; /* ATS state */ 826 - bool pri_tlp; /* PASID TLB required for 826 + 827 + u32 flags; /* Holds AMD_IOMMU_DEVICE_FLAG_<*> */ 828 + int ats_qdep; 829 + u8 ats_enabled :1; /* ATS state */ 830 + u8 pri_enabled :1; /* PRI state */ 831 + u8 pasid_enabled:1; /* PASID state */ 832 + u8 pri_tlp :1; /* PASID TLB required for 827 833 PPR completions */ 834 + u8 ppr :1; /* Enable device PPR support */ 828 835 bool use_vapic; /* Enable device to use vapic mode */ 829 836 bool defer_attach; 830 837 ··· 893 896 /* allocation bitmap for domain ids */ 894 897 extern unsigned long *amd_iommu_pd_alloc_bitmap; 895 898 896 - /* Smallest max PASID supported by any IOMMU in the system */ 897 - extern u32 amd_iommu_max_pasid; 898 - 899 - extern bool amd_iommu_v2_present; 900 - 901 899 extern bool amd_iommu_force_isolation; 902 900 903 901 /* Max levels of glxval supported */ 904 902 extern int amd_iommu_max_glx_val; 903 + 904 + /* Global EFR and EFR2 registers */ 905 + extern u64 amd_iommu_efr; 906 + extern u64 amd_iommu_efr2; 905 907 906 908 /* 907 909 * This function flushes all internal caches of
+43 -78
drivers/iommu/amd/init.c
··· 83 83 #define ACPI_DEVFLAG_LINT1 0x80 84 84 #define ACPI_DEVFLAG_ATSDIS 0x10000000 85 85 86 - #define LOOP_TIMEOUT 2000000 87 - 88 86 #define IVRS_GET_SBDF_ID(seg, bus, dev, fn) (((seg & 0xffff) << 16) | ((bus & 0xff) << 8) \ 89 87 | ((dev & 0x1f) << 3) | (fn & 0x7)) 90 88 ··· 185 187 bool amd_iommu_np_cache __read_mostly; 186 188 bool amd_iommu_iotlb_sup __read_mostly = true; 187 189 188 - u32 amd_iommu_max_pasid __read_mostly = ~0; 189 - 190 - bool amd_iommu_v2_present __read_mostly; 191 190 static bool amd_iommu_pc_present __read_mostly; 192 191 bool amdr_ivrs_remap_support __read_mostly; 193 192 ··· 267 272 * Iterate through all the IOMMUs to get common EFR 268 273 * masks among all IOMMUs and warn if found inconsistency. 269 274 */ 270 - static void get_global_efr(void) 275 + static __init void get_global_efr(void) 271 276 { 272 277 struct amd_iommu *iommu; 273 278 ··· 297 302 } 298 303 299 304 pr_info("Using global IVHD EFR:%#llx, EFR2:%#llx\n", amd_iommu_efr, amd_iommu_efr2); 300 - } 301 - 302 - static bool check_feature_on_all_iommus(u64 mask) 303 - { 304 - return !!(amd_iommu_efr & mask); 305 - } 306 - 307 - static inline int check_feature_gpt_level(void) 308 - { 309 - return ((amd_iommu_efr >> FEATURE_GATS_SHIFT) & FEATURE_GATS_MASK); 310 305 } 311 306 312 307 /* ··· 384 399 u64 start = iommu_virt_to_phys((void *)iommu->cmd_sem); 385 400 u64 entry = start & PM_ADDR_MASK; 386 401 387 - if (!check_feature_on_all_iommus(FEATURE_SNP)) 402 + if (!check_feature(FEATURE_SNP)) 388 403 return; 389 404 390 405 /* Note: ··· 854 869 void *buf = (void *)__get_free_pages(gfp, order); 855 870 856 871 if (buf && 857 - check_feature_on_all_iommus(FEATURE_SNP) && 872 + check_feature(FEATURE_SNP) && 858 873 set_memory_4k((unsigned long)buf, (1 << order))) { 859 874 free_pages((unsigned long)buf, order); 860 875 buf = NULL; ··· 970 985 iommu_feature_enable(iommu, CONTROL_GAINT_EN); 971 986 iommu_feature_enable(iommu, CONTROL_GALOG_EN); 972 987 973 - for (i = 0; i < LOOP_TIMEOUT; ++i) { 988 + for (i = 0; i < MMIO_STATUS_TIMEOUT; ++i) { 974 989 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET); 975 990 if (status & (MMIO_STATUS_GALOG_RUN_MASK)) 976 991 break; 977 992 udelay(10); 978 993 } 979 994 980 - if (WARN_ON(i >= LOOP_TIMEOUT)) 995 + if (WARN_ON(i >= MMIO_STATUS_TIMEOUT)) 981 996 return -EINVAL; 982 997 983 998 return 0; ··· 1033 1048 1034 1049 static void iommu_enable_gt(struct amd_iommu *iommu) 1035 1050 { 1036 - if (!iommu_feature(iommu, FEATURE_GT)) 1051 + if (!check_feature(FEATURE_GT)) 1037 1052 return; 1038 1053 1039 1054 iommu_feature_enable(iommu, CONTROL_GT_EN); ··· 1972 1987 u64 val; 1973 1988 struct pci_dev *pdev = iommu->dev; 1974 1989 1975 - if (!iommu_feature(iommu, FEATURE_PC)) 1990 + if (!check_feature(FEATURE_PC)) 1976 1991 return; 1977 1992 1978 1993 amd_iommu_pc_present = true; ··· 1999 2014 struct device_attribute *attr, 2000 2015 char *buf) 2001 2016 { 2002 - struct amd_iommu *iommu = dev_to_amd_iommu(dev); 2003 - return sysfs_emit(buf, "%llx:%llx\n", iommu->features2, iommu->features); 2017 + return sysfs_emit(buf, "%llx:%llx\n", amd_iommu_efr, amd_iommu_efr2); 2004 2018 } 2005 2019 static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL); 2006 2020 ··· 2035 2051 features = readq(iommu->mmio_base + MMIO_EXT_FEATURES); 2036 2052 features2 = readq(iommu->mmio_base + MMIO_EXT_FEATURES2); 2037 2053 2038 - if (!iommu->features) { 2039 - iommu->features = features; 2040 - iommu->features2 = features2; 2054 + if (!amd_iommu_efr) { 2055 + amd_iommu_efr = features; 2056 + amd_iommu_efr2 = features2; 2041 2057 return; 2042 2058 } 2043 2059 ··· 2045 2061 * Sanity check and warn if EFR values from 2046 2062 * IVHD and MMIO conflict. 2047 2063 */ 2048 - if (features != iommu->features || 2049 - features2 != iommu->features2) { 2064 + if (features != amd_iommu_efr || 2065 + features2 != amd_iommu_efr2) { 2050 2066 pr_warn(FW_WARN 2051 2067 "EFR mismatch. Use IVHD EFR (%#llx : %#llx), EFR2 (%#llx : %#llx).\n", 2052 - features, iommu->features, 2053 - features2, iommu->features2); 2068 + features, amd_iommu_efr, 2069 + features2, amd_iommu_efr2); 2054 2070 } 2055 2071 } 2056 2072 ··· 2076 2092 2077 2093 late_iommu_features_init(iommu); 2078 2094 2079 - if (iommu_feature(iommu, FEATURE_GT)) { 2095 + if (check_feature(FEATURE_GT)) { 2080 2096 int glxval; 2081 - u32 max_pasid; 2082 2097 u64 pasmax; 2083 2098 2084 - pasmax = iommu->features & FEATURE_PASID_MASK; 2099 + pasmax = amd_iommu_efr & FEATURE_PASID_MASK; 2085 2100 pasmax >>= FEATURE_PASID_SHIFT; 2086 - max_pasid = (1 << (pasmax + 1)) - 1; 2101 + iommu->iommu.max_pasids = (1 << (pasmax + 1)) - 1; 2087 2102 2088 - amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid); 2103 + BUG_ON(iommu->iommu.max_pasids & ~PASID_MASK); 2089 2104 2090 - BUG_ON(amd_iommu_max_pasid & ~PASID_MASK); 2091 - 2092 - glxval = iommu->features & FEATURE_GLXVAL_MASK; 2105 + glxval = amd_iommu_efr & FEATURE_GLXVAL_MASK; 2093 2106 glxval >>= FEATURE_GLXVAL_SHIFT; 2094 2107 2095 2108 if (amd_iommu_max_glx_val == -1) ··· 2095 2114 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval); 2096 2115 } 2097 2116 2098 - if (iommu_feature(iommu, FEATURE_GT) && 2099 - iommu_feature(iommu, FEATURE_PPR)) { 2100 - iommu->is_iommu_v2 = true; 2101 - amd_iommu_v2_present = true; 2102 - } 2103 - 2104 - if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu)) 2117 + if (check_feature(FEATURE_PPR) && alloc_ppr_log(iommu)) 2105 2118 return -ENOMEM; 2106 2119 2107 2120 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) { ··· 2107 2132 init_iommu_perf_ctr(iommu); 2108 2133 2109 2134 if (amd_iommu_pgtable == AMD_IOMMU_V2) { 2110 - if (!iommu_feature(iommu, FEATURE_GIOSUP) || 2111 - !iommu_feature(iommu, FEATURE_GT)) { 2135 + if (!check_feature(FEATURE_GIOSUP) || 2136 + !check_feature(FEATURE_GT)) { 2112 2137 pr_warn("Cannot enable v2 page table for DMA-API. Fallback to v1.\n"); 2113 - amd_iommu_pgtable = AMD_IOMMU_V1; 2114 - } else if (iommu_default_passthrough()) { 2115 - pr_warn("V2 page table doesn't support passthrough mode. Fallback to v1.\n"); 2116 2138 amd_iommu_pgtable = AMD_IOMMU_V1; 2117 2139 } 2118 2140 } ··· 2158 2186 2159 2187 static void print_iommu_info(void) 2160 2188 { 2189 + int i; 2161 2190 static const char * const feat_str[] = { 2162 2191 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]", 2163 2192 "IA", "GA", "HE", "PC" 2164 2193 }; 2165 - struct amd_iommu *iommu; 2166 2194 2167 - for_each_iommu(iommu) { 2168 - struct pci_dev *pdev = iommu->dev; 2169 - int i; 2195 + if (amd_iommu_efr) { 2196 + pr_info("Extended features (%#llx, %#llx):", amd_iommu_efr, amd_iommu_efr2); 2170 2197 2171 - pci_info(pdev, "Found IOMMU cap 0x%x\n", iommu->cap_ptr); 2172 - 2173 - if (iommu->cap & (1 << IOMMU_CAP_EFR)) { 2174 - pr_info("Extended features (%#llx, %#llx):", iommu->features, iommu->features2); 2175 - 2176 - for (i = 0; i < ARRAY_SIZE(feat_str); ++i) { 2177 - if (iommu_feature(iommu, (1ULL << i))) 2178 - pr_cont(" %s", feat_str[i]); 2179 - } 2180 - 2181 - if (iommu->features & FEATURE_GAM_VAPIC) 2182 - pr_cont(" GA_vAPIC"); 2183 - 2184 - if (iommu->features & FEATURE_SNP) 2185 - pr_cont(" SNP"); 2186 - 2187 - pr_cont("\n"); 2198 + for (i = 0; i < ARRAY_SIZE(feat_str); ++i) { 2199 + if (check_feature(1ULL << i)) 2200 + pr_cont(" %s", feat_str[i]); 2188 2201 } 2202 + 2203 + if (check_feature(FEATURE_GAM_VAPIC)) 2204 + pr_cont(" GA_vAPIC"); 2205 + 2206 + if (check_feature(FEATURE_SNP)) 2207 + pr_cont(" SNP"); 2208 + 2209 + pr_cont("\n"); 2189 2210 } 2211 + 2190 2212 if (irq_remapping_enabled) { 2191 2213 pr_info("Interrupt remapping enabled\n"); 2192 2214 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE) ··· 2866 2900 * Need to set and poll check the GALOGRun bit to zero before 2867 2901 * we can set/ modify GA Log registers safely. 2868 2902 */ 2869 - for (i = 0; i < LOOP_TIMEOUT; ++i) { 2903 + for (i = 0; i < MMIO_STATUS_TIMEOUT; ++i) { 2870 2904 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET); 2871 2905 if (!(status & MMIO_STATUS_GALOG_RUN_MASK)) 2872 2906 break; 2873 2907 udelay(10); 2874 2908 } 2875 2909 2876 - if (WARN_ON(i >= LOOP_TIMEOUT)) 2910 + if (WARN_ON(i >= MMIO_STATUS_TIMEOUT)) 2877 2911 return; 2878 2912 } 2879 2913 2880 2914 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) && 2881 - !check_feature_on_all_iommus(FEATURE_GAM_VAPIC)) { 2915 + !check_feature(FEATURE_GAM_VAPIC)) { 2882 2916 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA; 2883 2917 return; 2884 2918 } ··· 3664 3698 * (i.e. EFR[SNPSup]=1), IOMMUv2 page table cannot be used without 3665 3699 * setting up IOMMUv1 page table. 3666 3700 */ 3667 - return amd_iommu_v2_present && !amd_iommu_snp_en; 3701 + return amd_iommu_gt_ppr_supported() && !amd_iommu_snp_en; 3668 3702 } 3669 - EXPORT_SYMBOL(amd_iommu_v2_supported); 3670 3703 3671 3704 struct amd_iommu *get_amd_iommu(unsigned int idx) 3672 3705 { ··· 3789 3824 return -EINVAL; 3790 3825 } 3791 3826 3792 - amd_iommu_snp_en = check_feature_on_all_iommus(FEATURE_SNP); 3827 + amd_iommu_snp_en = check_feature(FEATURE_SNP); 3793 3828 if (!amd_iommu_snp_en) 3794 3829 return -EINVAL; 3795 3830
+4 -4
drivers/iommu/amd/io_pgtable_v2.c
··· 363 363 if (!(pdom->flags & PD_IOMMUV2_MASK)) 364 364 return; 365 365 366 - /* 367 - * Make changes visible to IOMMUs. No need to clear gcr3 entry 368 - * as gcr3 table is already freed. 369 - */ 366 + /* Clear gcr3 entry */ 367 + amd_iommu_domain_clear_gcr3(&pdom->domain, 0); 368 + 369 + /* Make changes visible to IOMMUs */ 370 370 amd_iommu_domain_update(pdom); 371 371 372 372 /* Free page table */
+243 -338
drivers/iommu/amd/iommu.c
··· 45 45 46 46 #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28)) 47 47 48 - #define LOOP_TIMEOUT 100000 49 - 50 48 /* IO virtual address start page frame number */ 51 49 #define IOVA_START_PFN (1) 52 50 #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT) ··· 66 68 const struct iommu_ops amd_iommu_ops; 67 69 const struct iommu_dirty_ops amd_dirty_ops; 68 70 69 - static ATOMIC_NOTIFIER_HEAD(ppr_notifier); 70 71 int amd_iommu_max_glx_val = -1; 71 72 72 73 /* ··· 78 81 struct kmem_cache *amd_iommu_irq_cache; 79 82 80 83 static void detach_device(struct device *dev); 81 - static int domain_enable_v2(struct protection_domain *domain, int pasids); 82 84 83 85 /**************************************************************************** 84 86 * ··· 320 324 return entry->group; 321 325 } 322 326 323 - static bool pci_iommuv2_capable(struct pci_dev *pdev) 327 + static inline bool pdev_pasid_supported(struct iommu_dev_data *dev_data) 324 328 { 325 - static const int caps[] = { 326 - PCI_EXT_CAP_ID_PRI, 327 - PCI_EXT_CAP_ID_PASID, 328 - }; 329 - int i, pos; 329 + return (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP); 330 + } 330 331 331 - if (!pci_ats_supported(pdev)) 332 - return false; 332 + static u32 pdev_get_caps(struct pci_dev *pdev) 333 + { 334 + int features; 335 + u32 flags = 0; 333 336 334 - for (i = 0; i < 2; ++i) { 335 - pos = pci_find_ext_capability(pdev, caps[i]); 336 - if (pos == 0) 337 - return false; 337 + if (pci_ats_supported(pdev)) 338 + flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP; 339 + 340 + if (pci_pri_supported(pdev)) 341 + flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP; 342 + 343 + features = pci_pasid_features(pdev); 344 + if (features >= 0) { 345 + flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP; 346 + 347 + if (features & PCI_PASID_CAP_EXEC) 348 + flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP; 349 + 350 + if (features & PCI_PASID_CAP_PRIV) 351 + flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP; 338 352 } 339 353 340 - return true; 354 + return flags; 355 + } 356 + 357 + static inline int pdev_enable_cap_ats(struct pci_dev *pdev) 358 + { 359 + struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev); 360 + int ret = -EINVAL; 361 + 362 + if (dev_data->ats_enabled) 363 + return 0; 364 + 365 + if (amd_iommu_iotlb_sup && 366 + (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP)) { 367 + ret = pci_enable_ats(pdev, PAGE_SHIFT); 368 + if (!ret) { 369 + dev_data->ats_enabled = 1; 370 + dev_data->ats_qdep = pci_ats_queue_depth(pdev); 371 + } 372 + } 373 + 374 + return ret; 375 + } 376 + 377 + static inline void pdev_disable_cap_ats(struct pci_dev *pdev) 378 + { 379 + struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev); 380 + 381 + if (dev_data->ats_enabled) { 382 + pci_disable_ats(pdev); 383 + dev_data->ats_enabled = 0; 384 + } 385 + } 386 + 387 + int amd_iommu_pdev_enable_cap_pri(struct pci_dev *pdev) 388 + { 389 + struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev); 390 + int ret = -EINVAL; 391 + 392 + if (dev_data->pri_enabled) 393 + return 0; 394 + 395 + if (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) { 396 + /* 397 + * First reset the PRI state of the device. 398 + * FIXME: Hardcode number of outstanding requests for now 399 + */ 400 + if (!pci_reset_pri(pdev) && !pci_enable_pri(pdev, 32)) { 401 + dev_data->pri_enabled = 1; 402 + dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev); 403 + 404 + ret = 0; 405 + } 406 + } 407 + 408 + return ret; 409 + } 410 + 411 + void amd_iommu_pdev_disable_cap_pri(struct pci_dev *pdev) 412 + { 413 + struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev); 414 + 415 + if (dev_data->pri_enabled) { 416 + pci_disable_pri(pdev); 417 + dev_data->pri_enabled = 0; 418 + } 419 + } 420 + 421 + static inline int pdev_enable_cap_pasid(struct pci_dev *pdev) 422 + { 423 + struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev); 424 + int ret = -EINVAL; 425 + 426 + if (dev_data->pasid_enabled) 427 + return 0; 428 + 429 + if (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) { 430 + /* Only allow access to user-accessible pages */ 431 + ret = pci_enable_pasid(pdev, 0); 432 + if (!ret) 433 + dev_data->pasid_enabled = 1; 434 + } 435 + 436 + return ret; 437 + } 438 + 439 + static inline void pdev_disable_cap_pasid(struct pci_dev *pdev) 440 + { 441 + struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev); 442 + 443 + if (dev_data->pasid_enabled) { 444 + pci_disable_pasid(pdev); 445 + dev_data->pasid_enabled = 0; 446 + } 447 + } 448 + 449 + static void pdev_enable_caps(struct pci_dev *pdev) 450 + { 451 + pdev_enable_cap_ats(pdev); 452 + pdev_enable_cap_pasid(pdev); 453 + amd_iommu_pdev_enable_cap_pri(pdev); 454 + 455 + } 456 + 457 + static void pdev_disable_caps(struct pci_dev *pdev) 458 + { 459 + pdev_disable_cap_ats(pdev); 460 + pdev_disable_cap_pasid(pdev); 461 + amd_iommu_pdev_disable_cap_pri(pdev); 341 462 } 342 463 343 464 /* ··· 514 401 * it'll be forced to go into translation mode. 515 402 */ 516 403 if ((iommu_default_passthrough() || !amd_iommu_force_isolation) && 517 - dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) { 518 - dev_data->iommu_v2 = iommu->is_iommu_v2; 404 + dev_is_pci(dev) && amd_iommu_gt_ppr_supported()) { 405 + dev_data->flags = pdev_get_caps(to_pci_dev(dev)); 519 406 } 520 407 521 408 dev_iommu_priv_set(dev, dev_data); ··· 816 703 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET); 817 704 } 818 705 819 - static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw) 820 - { 821 - struct amd_iommu_fault fault; 822 - 823 - if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) { 824 - pr_err_ratelimited("Unknown PPR request received\n"); 825 - return; 826 - } 827 - 828 - fault.address = raw[1]; 829 - fault.pasid = PPR_PASID(raw[0]); 830 - fault.sbdf = PCI_SEG_DEVID_TO_SBDF(iommu->pci_seg->id, PPR_DEVID(raw[0])); 831 - fault.tag = PPR_TAG(raw[0]); 832 - fault.flags = PPR_FLAGS(raw[0]); 833 - 834 - atomic_notifier_call_chain(&ppr_notifier, 0, &fault); 835 - } 836 - 837 706 static void iommu_poll_ppr_log(struct amd_iommu *iommu) 838 707 { 839 708 u32 head, tail; ··· 861 766 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE; 862 767 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET); 863 768 864 - /* Handle PPR entry */ 865 - iommu_handle_ppr_entry(iommu, entry); 769 + /* TODO: PPR Handler will be added when we add IOPF support */ 866 770 867 771 /* Refresh ring-buffer information */ 868 772 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET); ··· 1190 1096 } 1191 1097 1192 1098 static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, u32 pasid, 1193 - int status, int tag, bool gn) 1099 + int status, int tag, u8 gn) 1194 1100 { 1195 1101 memset(cmd, 0, sizeof(*cmd)); 1196 1102 ··· 1394 1300 1395 1301 void iommu_flush_all_caches(struct amd_iommu *iommu) 1396 1302 { 1397 - if (iommu_feature(iommu, FEATURE_IA)) { 1303 + if (check_feature(FEATURE_IA)) { 1398 1304 amd_iommu_flush_all(iommu); 1399 1305 } else { 1400 1306 amd_iommu_flush_dte_all(iommu); ··· 1413 1319 struct iommu_cmd cmd; 1414 1320 int qdep; 1415 1321 1416 - qdep = dev_data->ats.qdep; 1322 + qdep = dev_data->ats_qdep; 1417 1323 iommu = rlookup_amd_iommu(dev_data->dev); 1418 1324 if (!iommu) 1419 1325 return -EINVAL; ··· 1464 1370 return ret; 1465 1371 } 1466 1372 1467 - if (dev_data->ats.enabled) 1373 + if (dev_data->ats_enabled) 1468 1374 ret = device_flush_iotlb(dev_data, 0, ~0UL); 1469 1375 1470 1376 return ret; ··· 1497 1403 1498 1404 list_for_each_entry(dev_data, &domain->dev_list, list) { 1499 1405 1500 - if (!dev_data->ats.enabled) 1406 + if (!dev_data->ats_enabled) 1501 1407 continue; 1502 1408 1503 1409 ret |= device_flush_iotlb(dev_data, address, size); ··· 1673 1579 free_page((unsigned long)domain->gcr3_tbl); 1674 1580 } 1675 1581 1582 + /* 1583 + * Number of GCR3 table levels required. Level must be 4-Kbyte 1584 + * page and can contain up to 512 entries. 1585 + */ 1586 + static int get_gcr3_levels(int pasids) 1587 + { 1588 + int levels; 1589 + 1590 + if (pasids == -1) 1591 + return amd_iommu_max_glx_val; 1592 + 1593 + levels = get_count_order(pasids); 1594 + 1595 + return levels ? (DIV_ROUND_UP(levels, 9) - 1) : levels; 1596 + } 1597 + 1598 + /* Note: This function expects iommu_domain->lock to be held prior calling the function. */ 1599 + static int setup_gcr3_table(struct protection_domain *domain, int pasids) 1600 + { 1601 + int levels = get_gcr3_levels(pasids); 1602 + 1603 + if (levels > amd_iommu_max_glx_val) 1604 + return -EINVAL; 1605 + 1606 + domain->gcr3_tbl = alloc_pgtable_page(domain->nid, GFP_ATOMIC); 1607 + if (domain->gcr3_tbl == NULL) 1608 + return -ENOMEM; 1609 + 1610 + domain->glx = levels; 1611 + domain->flags |= PD_IOMMUV2_MASK; 1612 + 1613 + amd_iommu_domain_update(domain); 1614 + 1615 + return 0; 1616 + } 1617 + 1676 1618 static void set_dte_entry(struct amd_iommu *iommu, u16 devid, 1677 1619 struct protection_domain *domain, bool ats, bool ppr) 1678 1620 { ··· 1737 1607 if (ats) 1738 1608 flags |= DTE_FLAG_IOTLB; 1739 1609 1740 - if (ppr) { 1741 - if (iommu_feature(iommu, FEATURE_EPHSUP)) 1742 - pte_root |= 1ULL << DEV_ENTRY_PPR; 1743 - } 1610 + if (ppr) 1611 + pte_root |= 1ULL << DEV_ENTRY_PPR; 1744 1612 1745 1613 if (domain->dirty_tracking) 1746 1614 pte_root |= DTE_FLAG_HAD; ··· 1818 1690 iommu = rlookup_amd_iommu(dev_data->dev); 1819 1691 if (!iommu) 1820 1692 return; 1821 - ats = dev_data->ats.enabled; 1693 + ats = dev_data->ats_enabled; 1822 1694 1823 1695 /* Update data structures */ 1824 1696 dev_data->domain = domain; ··· 1834 1706 1835 1707 /* Update device table */ 1836 1708 set_dte_entry(iommu, dev_data->devid, domain, 1837 - ats, dev_data->iommu_v2); 1709 + ats, dev_data->ppr); 1838 1710 clone_aliases(iommu, dev_data->dev); 1839 1711 1840 1712 device_flush_dte(dev_data); ··· 1869 1741 domain->dev_cnt -= 1; 1870 1742 } 1871 1743 1872 - static void pdev_iommuv2_disable(struct pci_dev *pdev) 1873 - { 1874 - pci_disable_ats(pdev); 1875 - pci_disable_pri(pdev); 1876 - pci_disable_pasid(pdev); 1877 - } 1878 - 1879 - static int pdev_pri_ats_enable(struct pci_dev *pdev) 1880 - { 1881 - int ret; 1882 - 1883 - /* Only allow access to user-accessible pages */ 1884 - ret = pci_enable_pasid(pdev, 0); 1885 - if (ret) 1886 - return ret; 1887 - 1888 - /* First reset the PRI state of the device */ 1889 - ret = pci_reset_pri(pdev); 1890 - if (ret) 1891 - goto out_err_pasid; 1892 - 1893 - /* Enable PRI */ 1894 - /* FIXME: Hardcode number of outstanding requests for now */ 1895 - ret = pci_enable_pri(pdev, 32); 1896 - if (ret) 1897 - goto out_err_pasid; 1898 - 1899 - ret = pci_enable_ats(pdev, PAGE_SHIFT); 1900 - if (ret) 1901 - goto out_err_pri; 1902 - 1903 - return 0; 1904 - 1905 - out_err_pri: 1906 - pci_disable_pri(pdev); 1907 - 1908 - out_err_pasid: 1909 - pci_disable_pasid(pdev); 1910 - 1911 - return ret; 1912 - } 1913 - 1914 1744 /* 1915 1745 * If a device is not yet associated with a domain, this function makes the 1916 1746 * device visible in the domain ··· 1877 1791 struct protection_domain *domain) 1878 1792 { 1879 1793 struct iommu_dev_data *dev_data; 1880 - struct pci_dev *pdev; 1881 1794 unsigned long flags; 1882 - int ret; 1795 + int ret = 0; 1883 1796 1884 1797 spin_lock_irqsave(&domain->lock, flags); 1885 1798 ··· 1886 1801 1887 1802 spin_lock(&dev_data->lock); 1888 1803 1889 - ret = -EBUSY; 1890 - if (dev_data->domain != NULL) 1804 + if (dev_data->domain != NULL) { 1805 + ret = -EBUSY; 1891 1806 goto out; 1892 - 1893 - if (!dev_is_pci(dev)) 1894 - goto skip_ats_check; 1895 - 1896 - pdev = to_pci_dev(dev); 1897 - if (domain->flags & PD_IOMMUV2_MASK) { 1898 - struct iommu_domain *def_domain = iommu_get_dma_domain(dev); 1899 - 1900 - ret = -EINVAL; 1901 - 1902 - /* 1903 - * In case of using AMD_IOMMU_V1 page table mode and the device 1904 - * is enabling for PPR/ATS support (using v2 table), 1905 - * we need to make sure that the domain type is identity map. 1906 - */ 1907 - if ((amd_iommu_pgtable == AMD_IOMMU_V1) && 1908 - def_domain->type != IOMMU_DOMAIN_IDENTITY) { 1909 - goto out; 1910 - } 1911 - 1912 - if (dev_data->iommu_v2) { 1913 - if (pdev_pri_ats_enable(pdev) != 0) 1914 - goto out; 1915 - 1916 - dev_data->ats.enabled = true; 1917 - dev_data->ats.qdep = pci_ats_queue_depth(pdev); 1918 - dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev); 1919 - } 1920 - } else if (amd_iommu_iotlb_sup && 1921 - pci_enable_ats(pdev, PAGE_SHIFT) == 0) { 1922 - dev_data->ats.enabled = true; 1923 - dev_data->ats.qdep = pci_ats_queue_depth(pdev); 1924 1807 } 1925 1808 1926 - skip_ats_check: 1927 - ret = 0; 1809 + if (dev_is_pci(dev)) 1810 + pdev_enable_caps(to_pci_dev(dev)); 1928 1811 1929 1812 do_attach(dev_data, domain); 1930 1813 ··· 1940 1887 1941 1888 do_detach(dev_data); 1942 1889 1943 - if (!dev_is_pci(dev)) 1944 - goto out; 1945 - 1946 - if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2) 1947 - pdev_iommuv2_disable(to_pci_dev(dev)); 1948 - else if (dev_data->ats.enabled) 1949 - pci_disable_ats(to_pci_dev(dev)); 1950 - 1951 - dev_data->ats.enabled = false; 1890 + if (dev_is_pci(dev)) 1891 + pdev_disable_caps(to_pci_dev(dev)); 1952 1892 1953 1893 out: 1954 1894 spin_unlock(&dev_data->lock); ··· 2031 1985 if (!iommu) 2032 1986 continue; 2033 1987 set_dte_entry(iommu, dev_data->devid, domain, 2034 - dev_data->ats.enabled, dev_data->iommu_v2); 1988 + dev_data->ats_enabled, dev_data->ppr); 2035 1989 clone_aliases(iommu, dev_data->dev); 2036 1990 } 2037 1991 } ··· 2065 2019 static void cleanup_domain(struct protection_domain *domain) 2066 2020 { 2067 2021 struct iommu_dev_data *entry; 2068 - unsigned long flags; 2069 2022 2070 - spin_lock_irqsave(&domain->lock, flags); 2023 + lockdep_assert_held(&domain->lock); 2024 + 2025 + if (!domain->dev_cnt) 2026 + return; 2071 2027 2072 2028 while (!list_empty(&domain->dev_list)) { 2073 2029 entry = list_first_entry(&domain->dev_list, ··· 2077 2029 BUG_ON(!entry->domain); 2078 2030 do_detach(entry); 2079 2031 } 2080 - 2081 - spin_unlock_irqrestore(&domain->lock, flags); 2032 + WARN_ON(domain->dev_cnt != 0); 2082 2033 } 2083 2034 2084 2035 static void protection_domain_free(struct protection_domain *domain) ··· 2087 2040 2088 2041 if (domain->iop.pgtbl_cfg.tlb) 2089 2042 free_io_pgtable_ops(&domain->iop.iop.ops); 2043 + 2044 + if (domain->flags & PD_IOMMUV2_MASK) 2045 + free_gcr3_table(domain); 2046 + 2047 + if (domain->iop.root) 2048 + free_page((unsigned long)domain->iop.root); 2090 2049 2091 2050 if (domain->id) 2092 2051 domain_id_free(domain->id); ··· 2106 2053 2107 2054 BUG_ON(mode < PAGE_MODE_NONE || mode > PAGE_MODE_6_LEVEL); 2108 2055 2109 - spin_lock_init(&domain->lock); 2110 - domain->id = domain_id_alloc(); 2111 - if (!domain->id) 2112 - return -ENOMEM; 2113 - INIT_LIST_HEAD(&domain->dev_list); 2114 - 2115 2056 if (mode != PAGE_MODE_NONE) { 2116 2057 pt_root = (void *)get_zeroed_page(GFP_KERNEL); 2117 - if (!pt_root) { 2118 - domain_id_free(domain->id); 2058 + if (!pt_root) 2119 2059 return -ENOMEM; 2120 - } 2121 2060 } 2122 2061 2123 2062 amd_iommu_domain_set_pgtable(domain, pt_root, mode); ··· 2119 2074 2120 2075 static int protection_domain_init_v2(struct protection_domain *domain) 2121 2076 { 2122 - spin_lock_init(&domain->lock); 2123 - domain->id = domain_id_alloc(); 2124 - if (!domain->id) 2125 - return -ENOMEM; 2126 - INIT_LIST_HEAD(&domain->dev_list); 2127 - 2128 2077 domain->flags |= PD_GIOV_MASK; 2129 2078 2130 2079 domain->domain.pgsize_bitmap = AMD_IOMMU_PGSIZES_V2; 2131 2080 2132 - if (domain_enable_v2(domain, 1)) { 2133 - domain_id_free(domain->id); 2081 + if (setup_gcr3_table(domain, 1)) 2134 2082 return -ENOMEM; 2135 - } 2136 2083 2137 2084 return 0; 2138 2085 } ··· 2134 2097 struct io_pgtable_ops *pgtbl_ops; 2135 2098 struct protection_domain *domain; 2136 2099 int pgtable; 2137 - int mode = DEFAULT_PGTABLE_LEVEL; 2138 2100 int ret; 2139 - 2140 - /* 2141 - * Force IOMMU v1 page table when iommu=pt and 2142 - * when allocating domain for pass-through devices. 2143 - */ 2144 - if (type == IOMMU_DOMAIN_IDENTITY) { 2145 - pgtable = AMD_IOMMU_V1; 2146 - mode = PAGE_MODE_NONE; 2147 - } else if (type == IOMMU_DOMAIN_UNMANAGED) { 2148 - pgtable = AMD_IOMMU_V1; 2149 - } else if (type == IOMMU_DOMAIN_DMA || type == IOMMU_DOMAIN_DMA_FQ) { 2150 - pgtable = amd_iommu_pgtable; 2151 - } else { 2152 - return NULL; 2153 - } 2154 2101 2155 2102 domain = kzalloc(sizeof(*domain), GFP_KERNEL); 2156 2103 if (!domain) 2157 2104 return NULL; 2158 2105 2106 + domain->id = domain_id_alloc(); 2107 + if (!domain->id) 2108 + goto out_err; 2109 + 2110 + spin_lock_init(&domain->lock); 2111 + INIT_LIST_HEAD(&domain->dev_list); 2112 + domain->nid = NUMA_NO_NODE; 2113 + 2114 + switch (type) { 2115 + /* No need to allocate io pgtable ops in passthrough mode */ 2116 + case IOMMU_DOMAIN_IDENTITY: 2117 + return domain; 2118 + case IOMMU_DOMAIN_DMA: 2119 + pgtable = amd_iommu_pgtable; 2120 + break; 2121 + /* 2122 + * Force IOMMU v1 page table when allocating 2123 + * domain for pass-through devices. 2124 + */ 2125 + case IOMMU_DOMAIN_UNMANAGED: 2126 + pgtable = AMD_IOMMU_V1; 2127 + break; 2128 + default: 2129 + goto out_err; 2130 + } 2131 + 2159 2132 switch (pgtable) { 2160 2133 case AMD_IOMMU_V1: 2161 - ret = protection_domain_init_v1(domain, mode); 2134 + ret = protection_domain_init_v1(domain, DEFAULT_PGTABLE_LEVEL); 2162 2135 break; 2163 2136 case AMD_IOMMU_V2: 2164 2137 ret = protection_domain_init_v2(domain); 2165 2138 break; 2166 2139 default: 2167 2140 ret = -EINVAL; 2141 + break; 2168 2142 } 2169 2143 2170 2144 if (ret) 2171 2145 goto out_err; 2172 2146 2173 - /* No need to allocate io pgtable ops in passthrough mode */ 2174 - if (type == IOMMU_DOMAIN_IDENTITY) 2175 - return domain; 2176 - 2177 - domain->nid = NUMA_NO_NODE; 2178 - 2179 2147 pgtbl_ops = alloc_io_pgtable_ops(pgtable, &domain->iop.pgtbl_cfg, domain); 2180 - if (!pgtbl_ops) { 2181 - domain_id_free(domain->id); 2148 + if (!pgtbl_ops) 2182 2149 goto out_err; 2183 - } 2184 2150 2185 2151 return domain; 2186 2152 out_err: 2187 - kfree(domain); 2153 + protection_domain_free(domain); 2188 2154 return NULL; 2189 2155 } 2190 2156 ··· 2276 2236 static void amd_iommu_domain_free(struct iommu_domain *dom) 2277 2237 { 2278 2238 struct protection_domain *domain; 2279 - 2280 - domain = to_pdomain(dom); 2281 - 2282 - if (domain->dev_cnt > 0) 2283 - cleanup_domain(domain); 2284 - 2285 - BUG_ON(domain->dev_cnt != 0); 2239 + unsigned long flags; 2286 2240 2287 2241 if (!dom) 2288 2242 return; 2289 2243 2290 - if (domain->flags & PD_IOMMUV2_MASK) 2291 - free_gcr3_table(domain); 2244 + domain = to_pdomain(dom); 2245 + 2246 + spin_lock_irqsave(&domain->lock, flags); 2247 + 2248 + cleanup_domain(domain); 2249 + 2250 + spin_unlock_irqrestore(&domain->lock, flags); 2292 2251 2293 2252 protection_domain_free(domain); 2294 2253 } ··· 2335 2296 return ret; 2336 2297 } 2337 2298 2338 - static void amd_iommu_iotlb_sync_map(struct iommu_domain *dom, 2339 - unsigned long iova, size_t size) 2299 + static int amd_iommu_iotlb_sync_map(struct iommu_domain *dom, 2300 + unsigned long iova, size_t size) 2340 2301 { 2341 2302 struct protection_domain *domain = to_pdomain(dom); 2342 2303 struct io_pgtable_ops *ops = &domain->iop.iop.ops; 2343 2304 2344 2305 if (ops->map_pages) 2345 2306 domain_flush_np_cache(domain, iova, size); 2307 + return 0; 2346 2308 } 2347 2309 2348 2310 static int amd_iommu_map_pages(struct iommu_domain *dom, unsigned long iova, ··· 2581 2541 2582 2542 return dev_data->defer_attach; 2583 2543 } 2584 - EXPORT_SYMBOL_GPL(amd_iommu_is_attach_deferred); 2585 2544 2586 2545 static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain) 2587 2546 { ··· 2620 2581 * and require remapping. 2621 2582 * - SNP is enabled, because it prohibits DTE[Mode]=0. 2622 2583 */ 2623 - if (dev_data->iommu_v2 && 2584 + if (pdev_pasid_supported(dev_data) && 2624 2585 !cc_platform_has(CC_ATTR_MEM_ENCRYPT) && 2625 2586 !amd_iommu_snp_en) { 2626 2587 return IOMMU_DOMAIN_IDENTITY; ··· 2665 2626 } 2666 2627 }; 2667 2628 2668 - /***************************************************************************** 2669 - * 2670 - * The next functions do a basic initialization of IOMMU for pass through 2671 - * mode 2672 - * 2673 - * In passthrough mode the IOMMU is initialized and enabled but not used for 2674 - * DMA-API translation. 2675 - * 2676 - *****************************************************************************/ 2677 - 2678 - /* IOMMUv2 specific functions */ 2679 - int amd_iommu_register_ppr_notifier(struct notifier_block *nb) 2680 - { 2681 - return atomic_notifier_chain_register(&ppr_notifier, nb); 2682 - } 2683 - EXPORT_SYMBOL(amd_iommu_register_ppr_notifier); 2684 - 2685 - int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb) 2686 - { 2687 - return atomic_notifier_chain_unregister(&ppr_notifier, nb); 2688 - } 2689 - EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier); 2690 - 2691 - void amd_iommu_domain_direct_map(struct iommu_domain *dom) 2692 - { 2693 - struct protection_domain *domain = to_pdomain(dom); 2694 - unsigned long flags; 2695 - 2696 - spin_lock_irqsave(&domain->lock, flags); 2697 - 2698 - if (domain->iop.pgtbl_cfg.tlb) 2699 - free_io_pgtable_ops(&domain->iop.iop.ops); 2700 - 2701 - spin_unlock_irqrestore(&domain->lock, flags); 2702 - } 2703 - EXPORT_SYMBOL(amd_iommu_domain_direct_map); 2704 - 2705 - /* Note: This function expects iommu_domain->lock to be held prior calling the function. */ 2706 - static int domain_enable_v2(struct protection_domain *domain, int pasids) 2707 - { 2708 - int levels; 2709 - 2710 - /* Number of GCR3 table levels required */ 2711 - for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9) 2712 - levels += 1; 2713 - 2714 - if (levels > amd_iommu_max_glx_val) 2715 - return -EINVAL; 2716 - 2717 - domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC); 2718 - if (domain->gcr3_tbl == NULL) 2719 - return -ENOMEM; 2720 - 2721 - domain->glx = levels; 2722 - domain->flags |= PD_IOMMUV2_MASK; 2723 - 2724 - amd_iommu_domain_update(domain); 2725 - 2726 - return 0; 2727 - } 2728 - 2729 - int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids) 2730 - { 2731 - struct protection_domain *pdom = to_pdomain(dom); 2732 - unsigned long flags; 2733 - int ret; 2734 - 2735 - spin_lock_irqsave(&pdom->lock, flags); 2736 - 2737 - /* 2738 - * Save us all sanity checks whether devices already in the 2739 - * domain support IOMMUv2. Just force that the domain has no 2740 - * devices attached when it is switched into IOMMUv2 mode. 2741 - */ 2742 - ret = -EBUSY; 2743 - if (pdom->dev_cnt > 0 || pdom->flags & PD_IOMMUV2_MASK) 2744 - goto out; 2745 - 2746 - if (!pdom->gcr3_tbl) 2747 - ret = domain_enable_v2(pdom, pasids); 2748 - 2749 - out: 2750 - spin_unlock_irqrestore(&pdom->lock, flags); 2751 - return ret; 2752 - } 2753 - EXPORT_SYMBOL(amd_iommu_domain_enable_v2); 2754 - 2755 2629 static int __flush_pasid(struct protection_domain *domain, u32 pasid, 2756 2630 u64 address, bool size) 2757 2631 { ··· 2702 2750 There might be non-IOMMUv2 capable devices in an IOMMUv2 2703 2751 * domain. 2704 2752 */ 2705 - if (!dev_data->ats.enabled) 2753 + if (!dev_data->ats_enabled) 2706 2754 continue; 2707 2755 2708 - qdep = dev_data->ats.qdep; 2756 + qdep = dev_data->ats_qdep; 2709 2757 iommu = rlookup_amd_iommu(dev_data->dev); 2710 2758 if (!iommu) 2711 2759 continue; ··· 2746 2794 2747 2795 return ret; 2748 2796 } 2749 - EXPORT_SYMBOL(amd_iommu_flush_page); 2750 2797 2751 2798 static int __amd_iommu_flush_tlb(struct protection_domain *domain, u32 pasid) 2752 2799 { ··· 2765 2814 2766 2815 return ret; 2767 2816 } 2768 - EXPORT_SYMBOL(amd_iommu_flush_tlb); 2769 2817 2770 2818 static u64 *__get_gcr3_pte(u64 *root, int level, u32 pasid, bool alloc) 2771 2819 { ··· 2844 2894 2845 2895 return ret; 2846 2896 } 2847 - EXPORT_SYMBOL(amd_iommu_domain_set_gcr3); 2848 2897 2849 2898 int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, u32 pasid) 2850 2899 { ··· 2857 2908 2858 2909 return ret; 2859 2910 } 2860 - EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3); 2861 2911 2862 2912 int amd_iommu_complete_ppr(struct pci_dev *pdev, u32 pasid, 2863 2913 int status, int tag) ··· 2875 2927 2876 2928 return iommu_queue_command(iommu, &cmd); 2877 2929 } 2878 - EXPORT_SYMBOL(amd_iommu_complete_ppr); 2879 - 2880 - int amd_iommu_device_info(struct pci_dev *pdev, 2881 - struct amd_iommu_device_info *info) 2882 - { 2883 - int max_pasids; 2884 - int pos; 2885 - 2886 - if (pdev == NULL || info == NULL) 2887 - return -EINVAL; 2888 - 2889 - if (!amd_iommu_v2_supported()) 2890 - return -EINVAL; 2891 - 2892 - memset(info, 0, sizeof(*info)); 2893 - 2894 - if (pci_ats_supported(pdev)) 2895 - info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP; 2896 - 2897 - pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI); 2898 - if (pos) 2899 - info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP; 2900 - 2901 - pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID); 2902 - if (pos) { 2903 - int features; 2904 - 2905 - max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1)); 2906 - max_pasids = min(max_pasids, (1 << 20)); 2907 - 2908 - info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP; 2909 - info->max_pasids = min(pci_max_pasids(pdev), max_pasids); 2910 - 2911 - features = pci_pasid_features(pdev); 2912 - if (features & PCI_PASID_CAP_EXEC) 2913 - info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP; 2914 - if (features & PCI_PASID_CAP_PRIV) 2915 - info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP; 2916 - } 2917 - 2918 - return 0; 2919 - } 2920 - EXPORT_SYMBOL(amd_iommu_device_info); 2921 2930 2922 2931 #ifdef CONFIG_IRQ_REMAP 2923 2932
-996
drivers/iommu/amd/iommu_v2.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-only 2 - /* 3 - * Copyright (C) 2010-2012 Advanced Micro Devices, Inc. 4 - * Author: Joerg Roedel <jroedel@suse.de> 5 - */ 6 - 7 - #define pr_fmt(fmt) "AMD-Vi: " fmt 8 - 9 - #include <linux/refcount.h> 10 - #include <linux/mmu_notifier.h> 11 - #include <linux/amd-iommu.h> 12 - #include <linux/mm_types.h> 13 - #include <linux/profile.h> 14 - #include <linux/module.h> 15 - #include <linux/sched.h> 16 - #include <linux/sched/mm.h> 17 - #include <linux/wait.h> 18 - #include <linux/pci.h> 19 - #include <linux/gfp.h> 20 - #include <linux/cc_platform.h> 21 - 22 - #include "amd_iommu.h" 23 - 24 - MODULE_LICENSE("GPL v2"); 25 - MODULE_AUTHOR("Joerg Roedel <jroedel@suse.de>"); 26 - 27 - #define PRI_QUEUE_SIZE 512 28 - 29 - struct pri_queue { 30 - atomic_t inflight; 31 - bool finish; 32 - int status; 33 - }; 34 - 35 - struct pasid_state { 36 - struct list_head list; /* For global state-list */ 37 - refcount_t count; /* Reference count */ 38 - unsigned mmu_notifier_count; /* Counting nested mmu_notifier 39 - calls */ 40 - struct mm_struct *mm; /* mm_struct for the faults */ 41 - struct mmu_notifier mn; /* mmu_notifier handle */ 42 - struct pri_queue pri[PRI_QUEUE_SIZE]; /* PRI tag states */ 43 - struct device_state *device_state; /* Link to our device_state */ 44 - u32 pasid; /* PASID index */ 45 - bool invalid; /* Used during setup and 46 - teardown of the pasid */ 47 - spinlock_t lock; /* Protect pri_queues and 48 - mmu_notifer_count */ 49 - wait_queue_head_t wq; /* To wait for count == 0 */ 50 - }; 51 - 52 - struct device_state { 53 - struct list_head list; 54 - u32 sbdf; 55 - atomic_t count; 56 - struct pci_dev *pdev; 57 - struct pasid_state **states; 58 - struct iommu_domain *domain; 59 - int pasid_levels; 60 - int max_pasids; 61 - amd_iommu_invalid_ppr_cb inv_ppr_cb; 62 - amd_iommu_invalidate_ctx inv_ctx_cb; 63 - spinlock_t lock; 64 - wait_queue_head_t wq; 65 - }; 66 - 67 - struct fault { 68 - struct work_struct work; 69 - struct device_state *dev_state; 70 - struct pasid_state *state; 71 - struct mm_struct *mm; 72 - u64 address; 73 - u32 pasid; 74 - u16 tag; 75 - u16 finish; 76 - u16 flags; 77 - }; 78 - 79 - static LIST_HEAD(state_list); 80 - static DEFINE_SPINLOCK(state_lock); 81 - 82 - static struct workqueue_struct *iommu_wq; 83 - 84 - static void free_pasid_states(struct device_state *dev_state); 85 - 86 - static struct device_state *__get_device_state(u32 sbdf) 87 - { 88 - struct device_state *dev_state; 89 - 90 - list_for_each_entry(dev_state, &state_list, list) { 91 - if (dev_state->sbdf == sbdf) 92 - return dev_state; 93 - } 94 - 95 - return NULL; 96 - } 97 - 98 - static struct device_state *get_device_state(u32 sbdf) 99 - { 100 - struct device_state *dev_state; 101 - unsigned long flags; 102 - 103 - spin_lock_irqsave(&state_lock, flags); 104 - dev_state = __get_device_state(sbdf); 105 - if (dev_state != NULL) 106 - atomic_inc(&dev_state->count); 107 - spin_unlock_irqrestore(&state_lock, flags); 108 - 109 - return dev_state; 110 - } 111 - 112 - static void free_device_state(struct device_state *dev_state) 113 - { 114 - struct iommu_group *group; 115 - 116 - /* Get rid of any remaining pasid states */ 117 - free_pasid_states(dev_state); 118 - 119 - /* 120 - * Wait until the last reference is dropped before freeing 121 - * the device state. 122 - */ 123 - wait_event(dev_state->wq, !atomic_read(&dev_state->count)); 124 - 125 - /* 126 - * First detach device from domain - No more PRI requests will arrive 127 - * from that device after it is unbound from the IOMMUv2 domain. 128 - */ 129 - group = iommu_group_get(&dev_state->pdev->dev); 130 - if (WARN_ON(!group)) 131 - return; 132 - 133 - iommu_detach_group(dev_state->domain, group); 134 - 135 - iommu_group_put(group); 136 - 137 - /* Everything is down now, free the IOMMUv2 domain */ 138 - iommu_domain_free(dev_state->domain); 139 - 140 - /* Finally get rid of the device-state */ 141 - kfree(dev_state); 142 - } 143 - 144 - static void put_device_state(struct device_state *dev_state) 145 - { 146 - if (atomic_dec_and_test(&dev_state->count)) 147 - wake_up(&dev_state->wq); 148 - } 149 - 150 - /* Must be called under dev_state->lock */ 151 - static struct pasid_state **__get_pasid_state_ptr(struct device_state *dev_state, 152 - u32 pasid, bool alloc) 153 - { 154 - struct pasid_state **root, **ptr; 155 - int level, index; 156 - 157 - level = dev_state->pasid_levels; 158 - root = dev_state->states; 159 - 160 - while (true) { 161 - 162 - index = (pasid >> (9 * level)) & 0x1ff; 163 - ptr = &root[index]; 164 - 165 - if (level == 0) 166 - break; 167 - 168 - if (*ptr == NULL) { 169 - if (!alloc) 170 - return NULL; 171 - 172 - *ptr = (void *)get_zeroed_page(GFP_ATOMIC); 173 - if (*ptr == NULL) 174 - return NULL; 175 - } 176 - 177 - root = (struct pasid_state **)*ptr; 178 - level -= 1; 179 - } 180 - 181 - return ptr; 182 - } 183 - 184 - static int set_pasid_state(struct device_state *dev_state, 185 - struct pasid_state *pasid_state, 186 - u32 pasid) 187 - { 188 - struct pasid_state **ptr; 189 - unsigned long flags; 190 - int ret; 191 - 192 - spin_lock_irqsave(&dev_state->lock, flags); 193 - ptr = __get_pasid_state_ptr(dev_state, pasid, true); 194 - 195 - ret = -ENOMEM; 196 - if (ptr == NULL) 197 - goto out_unlock; 198 - 199 - ret = -ENOMEM; 200 - if (*ptr != NULL) 201 - goto out_unlock; 202 - 203 - *ptr = pasid_state; 204 - 205 - ret = 0; 206 - 207 - out_unlock: 208 - spin_unlock_irqrestore(&dev_state->lock, flags); 209 - 210 - return ret; 211 - } 212 - 213 - static void clear_pasid_state(struct device_state *dev_state, u32 pasid) 214 - { 215 - struct pasid_state **ptr; 216 - unsigned long flags; 217 - 218 - spin_lock_irqsave(&dev_state->lock, flags); 219 - ptr = __get_pasid_state_ptr(dev_state, pasid, true); 220 - 221 - if (ptr == NULL) 222 - goto out_unlock; 223 - 224 - *ptr = NULL; 225 - 226 - out_unlock: 227 - spin_unlock_irqrestore(&dev_state->lock, flags); 228 - } 229 - 230 - static struct pasid_state *get_pasid_state(struct device_state *dev_state, 231 - u32 pasid) 232 - { 233 - struct pasid_state **ptr, *ret = NULL; 234 - unsigned long flags; 235 - 236 - spin_lock_irqsave(&dev_state->lock, flags); 237 - ptr = __get_pasid_state_ptr(dev_state, pasid, false); 238 - 239 - if (ptr == NULL) 240 - goto out_unlock; 241 - 242 - ret = *ptr; 243 - if (ret) 244 - refcount_inc(&ret->count); 245 - 246 - out_unlock: 247 - spin_unlock_irqrestore(&dev_state->lock, flags); 248 - 249 - return ret; 250 - } 251 - 252 - static void free_pasid_state(struct pasid_state *pasid_state) 253 - { 254 - kfree(pasid_state); 255 - } 256 - 257 - static void put_pasid_state(struct pasid_state *pasid_state) 258 - { 259 - if (refcount_dec_and_test(&pasid_state->count)) 260 - wake_up(&pasid_state->wq); 261 - } 262 - 263 - static void put_pasid_state_wait(struct pasid_state *pasid_state) 264 - { 265 - if (!refcount_dec_and_test(&pasid_state->count)) 266 - wait_event(pasid_state->wq, !refcount_read(&pasid_state->count)); 267 - free_pasid_state(pasid_state); 268 - } 269 - 270 - static void unbind_pasid(struct pasid_state *pasid_state) 271 - { 272 - struct iommu_domain *domain; 273 - 274 - domain = pasid_state->device_state->domain; 275 - 276 - /* 277 - * Mark pasid_state as invalid, no more faults will we added to the 278 - * work queue after this is visible everywhere. 279 - */ 280 - pasid_state->invalid = true; 281 - 282 - /* Make sure this is visible */ 283 - smp_wmb(); 284 - 285 - /* After this the device/pasid can't access the mm anymore */ 286 - amd_iommu_domain_clear_gcr3(domain, pasid_state->pasid); 287 - 288 - /* Make sure no more pending faults are in the queue */ 289 - flush_workqueue(iommu_wq); 290 - } 291 - 292 - static void free_pasid_states_level1(struct pasid_state **tbl) 293 - { 294 - int i; 295 - 296 - for (i = 0; i < 512; ++i) { 297 - if (tbl[i] == NULL) 298 - continue; 299 - 300 - free_page((unsigned long)tbl[i]); 301 - } 302 - } 303 - 304 - static void free_pasid_states_level2(struct pasid_state **tbl) 305 - { 306 - struct pasid_state **ptr; 307 - int i; 308 - 309 - for (i = 0; i < 512; ++i) { 310 - if (tbl[i] == NULL) 311 - continue; 312 - 313 - ptr = (struct pasid_state **)tbl[i]; 314 - free_pasid_states_level1(ptr); 315 - } 316 - } 317 - 318 - static void free_pasid_states(struct device_state *dev_state) 319 - { 320 - struct pasid_state *pasid_state; 321 - int i; 322 - 323 - for (i = 0; i < dev_state->max_pasids; ++i) { 324 - pasid_state = get_pasid_state(dev_state, i); 325 - if (pasid_state == NULL) 326 - continue; 327 - 328 - put_pasid_state(pasid_state); 329 - 330 - /* Clear the pasid state so that the pasid can be re-used */ 331 - clear_pasid_state(dev_state, pasid_state->pasid); 332 - 333 - /* 334 - * This will call the mn_release function and 335 - * unbind the PASID 336 - */ 337 - mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm); 338 - 339 - put_pasid_state_wait(pasid_state); /* Reference taken in 340 - amd_iommu_bind_pasid */ 341 - 342 - /* Drop reference taken in amd_iommu_bind_pasid */ 343 - put_device_state(dev_state); 344 - } 345 - 346 - if (dev_state->pasid_levels == 2) 347 - free_pasid_states_level2(dev_state->states); 348 - else if (dev_state->pasid_levels == 1) 349 - free_pasid_states_level1(dev_state->states); 350 - else 351 - BUG_ON(dev_state->pasid_levels != 0); 352 - 353 - free_page((unsigned long)dev_state->states); 354 - } 355 - 356 - static struct pasid_state *mn_to_state(struct mmu_notifier *mn) 357 - { 358 - return container_of(mn, struct pasid_state, mn); 359 - } 360 - 361 - static void mn_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn, 362 - struct mm_struct *mm, 363 - unsigned long start, unsigned long end) 364 - { 365 - struct pasid_state *pasid_state; 366 - struct device_state *dev_state; 367 - 368 - pasid_state = mn_to_state(mn); 369 - dev_state = pasid_state->device_state; 370 - 371 - if ((start ^ (end - 1)) < PAGE_SIZE) 372 - amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, 373 - start); 374 - else 375 - amd_iommu_flush_tlb(dev_state->domain, pasid_state->pasid); 376 - } 377 - 378 - static void mn_release(struct mmu_notifier *mn, struct mm_struct *mm) 379 - { 380 - struct pasid_state *pasid_state; 381 - struct device_state *dev_state; 382 - bool run_inv_ctx_cb; 383 - 384 - might_sleep(); 385 - 386 - pasid_state = mn_to_state(mn); 387 - dev_state = pasid_state->device_state; 388 - run_inv_ctx_cb = !pasid_state->invalid; 389 - 390 - if (run_inv_ctx_cb && dev_state->inv_ctx_cb) 391 - dev_state->inv_ctx_cb(dev_state->pdev, pasid_state->pasid); 392 - 393 - unbind_pasid(pasid_state); 394 - } 395 - 396 - static const struct mmu_notifier_ops iommu_mn = { 397 - .release = mn_release, 398 - .arch_invalidate_secondary_tlbs = mn_arch_invalidate_secondary_tlbs, 399 - }; 400 - 401 - static void set_pri_tag_status(struct pasid_state *pasid_state, 402 - u16 tag, int status) 403 - { 404 - unsigned long flags; 405 - 406 - spin_lock_irqsave(&pasid_state->lock, flags); 407 - pasid_state->pri[tag].status = status; 408 - spin_unlock_irqrestore(&pasid_state->lock, flags); 409 - } 410 - 411 - static void finish_pri_tag(struct device_state *dev_state, 412 - struct pasid_state *pasid_state, 413 - u16 tag) 414 - { 415 - unsigned long flags; 416 - 417 - spin_lock_irqsave(&pasid_state->lock, flags); 418 - if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) && 419 - pasid_state->pri[tag].finish) { 420 - amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid, 421 - pasid_state->pri[tag].status, tag); 422 - pasid_state->pri[tag].finish = false; 423 - pasid_state->pri[tag].status = PPR_SUCCESS; 424 - } 425 - spin_unlock_irqrestore(&pasid_state->lock, flags); 426 - } 427 - 428 - static void handle_fault_error(struct fault *fault) 429 - { 430 - int status; 431 - 432 - if (!fault->dev_state->inv_ppr_cb) { 433 - set_pri_tag_status(fault->state, fault->tag, PPR_INVALID); 434 - return; 435 - } 436 - 437 - status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev, 438 - fault->pasid, 439 - fault->address, 440 - fault->flags); 441 - switch (status) { 442 - case AMD_IOMMU_INV_PRI_RSP_SUCCESS: 443 - set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS); 444 - break; 445 - case AMD_IOMMU_INV_PRI_RSP_INVALID: 446 - set_pri_tag_status(fault->state, fault->tag, PPR_INVALID); 447 - break; 448 - case AMD_IOMMU_INV_PRI_RSP_FAIL: 449 - set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE); 450 - break; 451 - default: 452 - BUG(); 453 - } 454 - } 455 - 456 - static bool access_error(struct vm_area_struct *vma, struct fault *fault) 457 - { 458 - unsigned long requested = 0; 459 - 460 - if (fault->flags & PPR_FAULT_EXEC) 461 - requested |= VM_EXEC; 462 - 463 - if (fault->flags & PPR_FAULT_READ) 464 - requested |= VM_READ; 465 - 466 - if (fault->flags & PPR_FAULT_WRITE) 467 - requested |= VM_WRITE; 468 - 469 - return (requested & ~vma->vm_flags) != 0; 470 - } 471 - 472 - static void do_fault(struct work_struct *work) 473 - { 474 - struct fault *fault = container_of(work, struct fault, work); 475 - struct vm_area_struct *vma; 476 - vm_fault_t ret = VM_FAULT_ERROR; 477 - unsigned int flags = 0; 478 - struct mm_struct *mm; 479 - u64 address; 480 - 481 - mm = fault->state->mm; 482 - address = fault->address; 483 - 484 - if (fault->flags & PPR_FAULT_USER) 485 - flags |= FAULT_FLAG_USER; 486 - if (fault->flags & PPR_FAULT_WRITE) 487 - flags |= FAULT_FLAG_WRITE; 488 - flags |= FAULT_FLAG_REMOTE; 489 - 490 - mmap_read_lock(mm); 491 - vma = vma_lookup(mm, address); 492 - if (!vma) 493 - /* failed to get a vma in the right range */ 494 - goto out; 495 - 496 - /* Check if we have the right permissions on the vma */ 497 - if (access_error(vma, fault)) 498 - goto out; 499 - 500 - ret = handle_mm_fault(vma, address, flags, NULL); 501 - out: 502 - mmap_read_unlock(mm); 503 - 504 - if (ret & VM_FAULT_ERROR) 505 - /* failed to service fault */ 506 - handle_fault_error(fault); 507 - 508 - finish_pri_tag(fault->dev_state, fault->state, fault->tag); 509 - 510 - put_pasid_state(fault->state); 511 - 512 - kfree(fault); 513 - } 514 - 515 - static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data) 516 - { 517 - struct amd_iommu_fault *iommu_fault; 518 - struct pasid_state *pasid_state; 519 - struct device_state *dev_state; 520 - struct pci_dev *pdev = NULL; 521 - unsigned long flags; 522 - struct fault *fault; 523 - bool finish; 524 - u16 tag, devid, seg_id; 525 - int ret; 526 - 527 - iommu_fault = data; 528 - tag = iommu_fault->tag & 0x1ff; 529 - finish = (iommu_fault->tag >> 9) & 1; 530 - 531 - seg_id = PCI_SBDF_TO_SEGID(iommu_fault->sbdf); 532 - devid = PCI_SBDF_TO_DEVID(iommu_fault->sbdf); 533 - pdev = pci_get_domain_bus_and_slot(seg_id, PCI_BUS_NUM(devid), 534 - devid & 0xff); 535 - if (!pdev) 536 - return -ENODEV; 537 - 538 - ret = NOTIFY_DONE; 539 - 540 - /* In kdump kernel pci dev is not initialized yet -> send INVALID */ 541 - if (amd_iommu_is_attach_deferred(&pdev->dev)) { 542 - amd_iommu_complete_ppr(pdev, iommu_fault->pasid, 543 - PPR_INVALID, tag); 544 - goto out; 545 - } 546 - 547 - dev_state = get_device_state(iommu_fault->sbdf); 548 - if (dev_state == NULL) 549 - goto out; 550 - 551 - pasid_state = get_pasid_state(dev_state, iommu_fault->pasid); 552 - if (pasid_state == NULL || pasid_state->invalid) { 553 - /* We know the device but not the PASID -> send INVALID */ 554 - amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid, 555 - PPR_INVALID, tag); 556 - goto out_drop_state; 557 - } 558 - 559 - spin_lock_irqsave(&pasid_state->lock, flags); 560 - atomic_inc(&pasid_state->pri[tag].inflight); 561 - if (finish) 562 - pasid_state->pri[tag].finish = true; 563 - spin_unlock_irqrestore(&pasid_state->lock, flags); 564 - 565 - fault = kzalloc(sizeof(*fault), GFP_ATOMIC); 566 - if (fault == NULL) { 567 - /* We are OOM - send success and let the device re-fault */ 568 - finish_pri_tag(dev_state, pasid_state, tag); 569 - goto out_drop_state; 570 - } 571 - 572 - fault->dev_state = dev_state; 573 - fault->address = iommu_fault->address; 574 - fault->state = pasid_state; 575 - fault->tag = tag; 576 - fault->finish = finish; 577 - fault->pasid = iommu_fault->pasid; 578 - fault->flags = iommu_fault->flags; 579 - INIT_WORK(&fault->work, do_fault); 580 - 581 - queue_work(iommu_wq, &fault->work); 582 - 583 - ret = NOTIFY_OK; 584 - 585 - out_drop_state: 586 - 587 - if (ret != NOTIFY_OK && pasid_state) 588 - put_pasid_state(pasid_state); 589 - 590 - put_device_state(dev_state); 591 - 592 - out: 593 - pci_dev_put(pdev); 594 - return ret; 595 - } 596 - 597 - static struct notifier_block ppr_nb = { 598 - .notifier_call = ppr_notifier, 599 - }; 600 - 601 - int amd_iommu_bind_pasid(struct pci_dev *pdev, u32 pasid, 602 - struct task_struct *task) 603 - { 604 - struct pasid_state *pasid_state; 605 - struct device_state *dev_state; 606 - struct mm_struct *mm; 607 - u32 sbdf; 608 - int ret; 609 - 610 - might_sleep(); 611 - 612 - if (!amd_iommu_v2_supported()) 613 - return -ENODEV; 614 - 615 - sbdf = get_pci_sbdf_id(pdev); 616 - dev_state = get_device_state(sbdf); 617 - 618 - if (dev_state == NULL) 619 - return -EINVAL; 620 - 621 - ret = -EINVAL; 622 - if (pasid >= dev_state->max_pasids) 623 - goto out; 624 - 625 - ret = -ENOMEM; 626 - pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL); 627 - if (pasid_state == NULL) 628 - goto out; 629 - 630 - 631 - refcount_set(&pasid_state->count, 1); 632 - init_waitqueue_head(&pasid_state->wq); 633 - spin_lock_init(&pasid_state->lock); 634 - 635 - mm = get_task_mm(task); 636 - pasid_state->mm = mm; 637 - pasid_state->device_state = dev_state; 638 - pasid_state->pasid = pasid; 639 - pasid_state->invalid = true; /* Mark as valid only if we are 640 - done with setting up the pasid */ 641 - pasid_state->mn.ops = &iommu_mn; 642 - 643 - if (pasid_state->mm == NULL) 644 - goto out_free; 645 - 646 - ret = mmu_notifier_register(&pasid_state->mn, mm); 647 - if (ret) 648 - goto out_free; 649 - 650 - ret = set_pasid_state(dev_state, pasid_state, pasid); 651 - if (ret) 652 - goto out_unregister; 653 - 654 - ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid, 655 - __pa(pasid_state->mm->pgd)); 656 - if (ret) 657 - goto out_clear_state; 658 - 659 - /* Now we are ready to handle faults */ 660 - pasid_state->invalid = false; 661 - 662 - /* 663 - * Drop the reference to the mm_struct here. We rely on the 664 - * mmu_notifier release call-back to inform us when the mm 665 - * is going away. 666 - */ 667 - mmput(mm); 668 - 669 - return 0; 670 - 671 - out_clear_state: 672 - clear_pasid_state(dev_state, pasid); 673 - 674 - out_unregister: 675 - mmu_notifier_unregister(&pasid_state->mn, mm); 676 - mmput(mm); 677 - 678 - out_free: 679 - free_pasid_state(pasid_state); 680 - 681 - out: 682 - put_device_state(dev_state); 683 - 684 - return ret; 685 - } 686 - EXPORT_SYMBOL(amd_iommu_bind_pasid); 687 - 688 - void amd_iommu_unbind_pasid(struct pci_dev *pdev, u32 pasid) 689 - { 690 - struct pasid_state *pasid_state; 691 - struct device_state *dev_state; 692 - u32 sbdf; 693 - 694 - might_sleep(); 695 - 696 - if (!amd_iommu_v2_supported()) 697 - return; 698 - 699 - sbdf = get_pci_sbdf_id(pdev); 700 - dev_state = get_device_state(sbdf); 701 - if (dev_state == NULL) 702 - return; 703 - 704 - if (pasid >= dev_state->max_pasids) 705 - goto out; 706 - 707 - pasid_state = get_pasid_state(dev_state, pasid); 708 - if (pasid_state == NULL) 709 - goto out; 710 - /* 711 - * Drop reference taken here. We are safe because we still hold 712 - * the reference taken in the amd_iommu_bind_pasid function. 713 - */ 714 - put_pasid_state(pasid_state); 715 - 716 - /* Clear the pasid state so that the pasid can be re-used */ 717 - clear_pasid_state(dev_state, pasid_state->pasid); 718 - 719 - /* 720 - * Call mmu_notifier_unregister to drop our reference 721 - * to pasid_state->mm 722 - */ 723 - mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm); 724 - 725 - put_pasid_state_wait(pasid_state); /* Reference taken in 726 - amd_iommu_bind_pasid */ 727 - out: 728 - /* Drop reference taken in this function */ 729 - put_device_state(dev_state); 730 - 731 - /* Drop reference taken in amd_iommu_bind_pasid */ 732 - put_device_state(dev_state); 733 - } 734 - EXPORT_SYMBOL(amd_iommu_unbind_pasid); 735 - 736 - int amd_iommu_init_device(struct pci_dev *pdev, int pasids) 737 - { 738 - struct device_state *dev_state; 739 - struct iommu_group *group; 740 - unsigned long flags; 741 - int ret, tmp; 742 - u32 sbdf; 743 - 744 - might_sleep(); 745 - 746 - /* 747 - * When memory encryption is active the device is likely not in a 748 - * direct-mapped domain. Forbid using IOMMUv2 functionality for now. 749 - */ 750 - if (cc_platform_has(CC_ATTR_MEM_ENCRYPT)) 751 - return -ENODEV; 752 - 753 - if (!amd_iommu_v2_supported()) 754 - return -ENODEV; 755 - 756 - if (pasids <= 0 || pasids > (PASID_MASK + 1)) 757 - return -EINVAL; 758 - 759 - sbdf = get_pci_sbdf_id(pdev); 760 - 761 - dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL); 762 - if (dev_state == NULL) 763 - return -ENOMEM; 764 - 765 - spin_lock_init(&dev_state->lock); 766 - init_waitqueue_head(&dev_state->wq); 767 - dev_state->pdev = pdev; 768 - dev_state->sbdf = sbdf; 769 - 770 - tmp = pasids; 771 - for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9) 772 - dev_state->pasid_levels += 1; 773 - 774 - atomic_set(&dev_state->count, 1); 775 - dev_state->max_pasids = pasids; 776 - 777 - ret = -ENOMEM; 778 - dev_state->states = (void *)get_zeroed_page(GFP_KERNEL); 779 - if (dev_state->states == NULL) 780 - goto out_free_dev_state; 781 - 782 - dev_state->domain = iommu_domain_alloc(&pci_bus_type); 783 - if (dev_state->domain == NULL) 784 - goto out_free_states; 785 - 786 - /* See iommu_is_default_domain() */ 787 - dev_state->domain->type = IOMMU_DOMAIN_IDENTITY; 788 - amd_iommu_domain_direct_map(dev_state->domain); 789 - 790 - ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids); 791 - if (ret) 792 - goto out_free_domain; 793 - 794 - group = iommu_group_get(&pdev->dev); 795 - if (!group) { 796 - ret = -EINVAL; 797 - goto out_free_domain; 798 - } 799 - 800 - ret = iommu_attach_group(dev_state->domain, group); 801 - if (ret != 0) 802 - goto out_drop_group; 803 - 804 - iommu_group_put(group); 805 - 806 - spin_lock_irqsave(&state_lock, flags); 807 - 808 - if (__get_device_state(sbdf) != NULL) { 809 - spin_unlock_irqrestore(&state_lock, flags); 810 - ret = -EBUSY; 811 - goto out_free_domain; 812 - } 813 - 814 - list_add_tail(&dev_state->list, &state_list); 815 - 816 - spin_unlock_irqrestore(&state_lock, flags); 817 - 818 - return 0; 819 - 820 - out_drop_group: 821 - iommu_group_put(group); 822 - 823 - out_free_domain: 824 - iommu_domain_free(dev_state->domain); 825 - 826 - out_free_states: 827 - free_page((unsigned long)dev_state->states); 828 - 829 - out_free_dev_state: 830 - kfree(dev_state); 831 - 832 - return ret; 833 - } 834 - EXPORT_SYMBOL(amd_iommu_init_device); 835 - 836 - void amd_iommu_free_device(struct pci_dev *pdev) 837 - { 838 - struct device_state *dev_state; 839 - unsigned long flags; 840 - u32 sbdf; 841 - 842 - if (!amd_iommu_v2_supported()) 843 - return; 844 - 845 - sbdf = get_pci_sbdf_id(pdev); 846 - 847 - spin_lock_irqsave(&state_lock, flags); 848 - 849 - dev_state = __get_device_state(sbdf); 850 - if (dev_state == NULL) { 851 - spin_unlock_irqrestore(&state_lock, flags); 852 - return; 853 - } 854 - 855 - list_del(&dev_state->list); 856 - 857 - spin_unlock_irqrestore(&state_lock, flags); 858 - 859 - put_device_state(dev_state); 860 - free_device_state(dev_state); 861 - } 862 - EXPORT_SYMBOL(amd_iommu_free_device); 863 - 864 - int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev, 865 - amd_iommu_invalid_ppr_cb cb) 866 - { 867 - struct device_state *dev_state; 868 - unsigned long flags; 869 - u32 sbdf; 870 - int ret; 871 - 872 - if (!amd_iommu_v2_supported()) 873 - return -ENODEV; 874 - 875 - sbdf = get_pci_sbdf_id(pdev); 876 - 877 - spin_lock_irqsave(&state_lock, flags); 878 - 879 - ret = -EINVAL; 880 - dev_state = __get_device_state(sbdf); 881 - if (dev_state == NULL) 882 - goto out_unlock; 883 - 884 - dev_state->inv_ppr_cb = cb; 885 - 886 - ret = 0; 887 - 888 - out_unlock: 889 - spin_unlock_irqrestore(&state_lock, flags); 890 - 891 - return ret; 892 - } 893 - EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb); 894 - 895 - int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev, 896 - amd_iommu_invalidate_ctx cb) 897 - { 898 - struct device_state *dev_state; 899 - unsigned long flags; 900 - u32 sbdf; 901 - int ret; 902 - 903 - if (!amd_iommu_v2_supported()) 904 - return -ENODEV; 905 - 906 - sbdf = get_pci_sbdf_id(pdev); 907 - 908 - spin_lock_irqsave(&state_lock, flags); 909 - 910 - ret = -EINVAL; 911 - dev_state = __get_device_state(sbdf); 912 - if (dev_state == NULL) 913 - goto out_unlock; 914 - 915 - dev_state->inv_ctx_cb = cb; 916 - 917 - ret = 0; 918 - 919 - out_unlock: 920 - spin_unlock_irqrestore(&state_lock, flags); 921 - 922 - return ret; 923 - } 924 - EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb); 925 - 926 - static int __init amd_iommu_v2_init(void) 927 - { 928 - int ret; 929 - 930 - if (!amd_iommu_v2_supported()) { 931 - pr_info("AMD IOMMUv2 functionality not available on this system - This is not a bug.\n"); 932 - /* 933 - * Load anyway to provide the symbols to other modules 934 - * which may use AMD IOMMUv2 optionally. 935 - */ 936 - return 0; 937 - } 938 - 939 - ret = -ENOMEM; 940 - iommu_wq = alloc_workqueue("amd_iommu_v2", WQ_MEM_RECLAIM, 0); 941 - if (iommu_wq == NULL) 942 - goto out; 943 - 944 - amd_iommu_register_ppr_notifier(&ppr_nb); 945 - 946 - pr_info("AMD IOMMUv2 loaded and initialized\n"); 947 - 948 - return 0; 949 - 950 - out: 951 - return ret; 952 - } 953 - 954 - static void __exit amd_iommu_v2_exit(void) 955 - { 956 - struct device_state *dev_state, *next; 957 - unsigned long flags; 958 - LIST_HEAD(freelist); 959 - 960 - if (!amd_iommu_v2_supported()) 961 - return; 962 - 963 - amd_iommu_unregister_ppr_notifier(&ppr_nb); 964 - 965 - flush_workqueue(iommu_wq); 966 - 967 - /* 968 - * The loop below might call flush_workqueue(), so call 969 - * destroy_workqueue() after it 970 - */ 971 - spin_lock_irqsave(&state_lock, flags); 972 - 973 - list_for_each_entry_safe(dev_state, next, &state_list, list) { 974 - WARN_ON_ONCE(1); 975 - 976 - put_device_state(dev_state); 977 - list_del(&dev_state->list); 978 - list_add_tail(&dev_state->list, &freelist); 979 - } 980 - 981 - spin_unlock_irqrestore(&state_lock, flags); 982 - 983 - /* 984 - * Since free_device_state waits on the count to be zero, 985 - * we need to free dev_state outside the spinlock. 986 - */ 987 - list_for_each_entry_safe(dev_state, next, &freelist, list) { 988 - list_del(&dev_state->list); 989 - free_device_state(dev_state); 990 - } 991 - 992 - destroy_workqueue(iommu_wq); 993 - } 994 - 995 - module_init(amd_iommu_v2_init); 996 - module_exit(amd_iommu_v2_exit);
+85 -57
drivers/iommu/apple-dart.c
··· 196 196 * @lock: lock for hardware operations involving this dart 197 197 * @pgsize: pagesize supported by this DART 198 198 * @supports_bypass: indicates if this DART supports bypass mode 199 - * @force_bypass: force bypass mode due to pagesize mismatch? 200 199 * @sid2group: maps stream ids to iommu_groups 201 200 * @iommu: iommu core device 202 201 */ ··· 216 217 u32 pgsize; 217 218 u32 num_streams; 218 219 u32 supports_bypass : 1; 219 - u32 force_bypass : 1; 220 220 221 221 struct iommu_group *sid2group[DART_MAX_STREAMS]; 222 222 struct iommu_device iommu; ··· 504 506 apple_dart_domain_flush_tlb(to_dart_domain(domain)); 505 507 } 506 508 507 - static void apple_dart_iotlb_sync_map(struct iommu_domain *domain, 508 - unsigned long iova, size_t size) 509 + static int apple_dart_iotlb_sync_map(struct iommu_domain *domain, 510 + unsigned long iova, size_t size) 509 511 { 510 512 apple_dart_domain_flush_tlb(to_dart_domain(domain)); 513 + return 0; 511 514 } 512 515 513 516 static phys_addr_t apple_dart_iova_to_phys(struct iommu_domain *domain, ··· 567 568 stream_map->dart->hw->invalidate_tlb(stream_map); 568 569 } 569 570 570 - static int apple_dart_finalize_domain(struct iommu_domain *domain, 571 + static int apple_dart_finalize_domain(struct apple_dart_domain *dart_domain, 571 572 struct apple_dart_master_cfg *cfg) 572 573 { 573 - struct apple_dart_domain *dart_domain = to_dart_domain(domain); 574 574 struct apple_dart *dart = cfg->stream_maps[0].dart; 575 575 struct io_pgtable_cfg pgtbl_cfg; 576 576 int ret = 0; 577 577 int i, j; 578 + 579 + if (dart->pgsize > PAGE_SIZE) 580 + return -EINVAL; 578 581 579 582 mutex_lock(&dart_domain->init_lock); 580 583 ··· 598 597 .iommu_dev = dart->dev, 599 598 }; 600 599 601 - dart_domain->pgtbl_ops = 602 - alloc_io_pgtable_ops(dart->hw->fmt, &pgtbl_cfg, domain); 600 + dart_domain->pgtbl_ops = alloc_io_pgtable_ops(dart->hw->fmt, &pgtbl_cfg, 601 + &dart_domain->domain); 603 602 if (!dart_domain->pgtbl_ops) { 604 603 ret = -ENOMEM; 605 604 goto done; 606 605 } 607 606 608 - domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap; 609 - domain->geometry.aperture_start = 0; 610 - domain->geometry.aperture_end = (dma_addr_t)DMA_BIT_MASK(dart->ias); 611 - domain->geometry.force_aperture = true; 607 + dart_domain->domain.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap; 608 + dart_domain->domain.geometry.aperture_start = 0; 609 + dart_domain->domain.geometry.aperture_end = 610 + (dma_addr_t)DMA_BIT_MASK(dart->ias); 611 + dart_domain->domain.geometry.force_aperture = true; 612 612 613 613 dart_domain->finalized = true; 614 614 ··· 653 651 true); 654 652 } 655 653 656 - static int apple_dart_attach_dev(struct iommu_domain *domain, 657 - struct device *dev) 654 + static int apple_dart_attach_dev_paging(struct iommu_domain *domain, 655 + struct device *dev) 658 656 { 659 657 int ret, i; 660 658 struct apple_dart_stream_map *stream_map; 661 659 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev); 662 660 struct apple_dart_domain *dart_domain = to_dart_domain(domain); 663 661 664 - if (cfg->stream_maps[0].dart->force_bypass && 665 - domain->type != IOMMU_DOMAIN_IDENTITY) 666 - return -EINVAL; 667 - if (!cfg->stream_maps[0].dart->supports_bypass && 668 - domain->type == IOMMU_DOMAIN_IDENTITY) 669 - return -EINVAL; 670 - 671 - ret = apple_dart_finalize_domain(domain, cfg); 662 + ret = apple_dart_finalize_domain(dart_domain, cfg); 672 663 if (ret) 673 664 return ret; 674 665 675 - switch (domain->type) { 676 - default: 677 - ret = apple_dart_domain_add_streams(dart_domain, cfg); 678 - if (ret) 679 - return ret; 666 + ret = apple_dart_domain_add_streams(dart_domain, cfg); 667 + if (ret) 668 + return ret; 680 669 681 - for_each_stream_map(i, cfg, stream_map) 682 - apple_dart_setup_translation(dart_domain, stream_map); 683 - break; 684 - case IOMMU_DOMAIN_BLOCKED: 685 - for_each_stream_map(i, cfg, stream_map) 686 - apple_dart_hw_disable_dma(stream_map); 687 - break; 688 - case IOMMU_DOMAIN_IDENTITY: 689 - for_each_stream_map(i, cfg, stream_map) 690 - apple_dart_hw_enable_bypass(stream_map); 691 - break; 692 - } 693 - 694 - return ret; 670 + for_each_stream_map(i, cfg, stream_map) 671 + apple_dart_setup_translation(dart_domain, stream_map); 672 + return 0; 695 673 } 674 + 675 + static int apple_dart_attach_dev_identity(struct iommu_domain *domain, 676 + struct device *dev) 677 + { 678 + struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev); 679 + struct apple_dart_stream_map *stream_map; 680 + int i; 681 + 682 + if (!cfg->stream_maps[0].dart->supports_bypass) 683 + return -EINVAL; 684 + 685 + for_each_stream_map(i, cfg, stream_map) 686 + apple_dart_hw_enable_bypass(stream_map); 687 + return 0; 688 + } 689 + 690 + static const struct iommu_domain_ops apple_dart_identity_ops = { 691 + .attach_dev = apple_dart_attach_dev_identity, 692 + }; 693 + 694 + static struct iommu_domain apple_dart_identity_domain = { 695 + .type = IOMMU_DOMAIN_IDENTITY, 696 + .ops = &apple_dart_identity_ops, 697 + }; 698 + 699 + static int apple_dart_attach_dev_blocked(struct iommu_domain *domain, 700 + struct device *dev) 701 + { 702 + struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev); 703 + struct apple_dart_stream_map *stream_map; 704 + int i; 705 + 706 + for_each_stream_map(i, cfg, stream_map) 707 + apple_dart_hw_disable_dma(stream_map); 708 + return 0; 709 + } 710 + 711 + static const struct iommu_domain_ops apple_dart_blocked_ops = { 712 + .attach_dev = apple_dart_attach_dev_blocked, 713 + }; 714 + 715 + static struct iommu_domain apple_dart_blocked_domain = { 716 + .type = IOMMU_DOMAIN_BLOCKED, 717 + .ops = &apple_dart_blocked_ops, 718 + }; 696 719 697 720 static struct iommu_device *apple_dart_probe_device(struct device *dev) 698 721 { ··· 744 717 kfree(cfg); 745 718 } 746 719 747 - static struct iommu_domain *apple_dart_domain_alloc(unsigned int type) 720 + static struct iommu_domain *apple_dart_domain_alloc_paging(struct device *dev) 748 721 { 749 722 struct apple_dart_domain *dart_domain; 750 - 751 - if (type != IOMMU_DOMAIN_DMA && type != IOMMU_DOMAIN_UNMANAGED && 752 - type != IOMMU_DOMAIN_IDENTITY && type != IOMMU_DOMAIN_BLOCKED) 753 - return NULL; 754 723 755 724 dart_domain = kzalloc(sizeof(*dart_domain), GFP_KERNEL); 756 725 if (!dart_domain) ··· 754 731 755 732 mutex_init(&dart_domain->init_lock); 756 733 757 - /* no need to allocate pgtbl_ops or do any other finalization steps */ 758 - if (type == IOMMU_DOMAIN_IDENTITY || type == IOMMU_DOMAIN_BLOCKED) 759 - dart_domain->finalized = true; 734 + if (dev) { 735 + struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev); 736 + int ret; 760 737 738 + ret = apple_dart_finalize_domain(dart_domain, cfg); 739 + if (ret) { 740 + kfree(dart_domain); 741 + return ERR_PTR(ret); 742 + } 743 + } 761 744 return &dart_domain->domain; 762 745 } 763 746 ··· 798 769 cfg_dart = cfg->stream_maps[0].dart; 799 770 if (cfg_dart) { 800 771 if (cfg_dart->supports_bypass != dart->supports_bypass) 801 - return -EINVAL; 802 - if (cfg_dart->force_bypass != dart->force_bypass) 803 772 return -EINVAL; 804 773 if (cfg_dart->pgsize != dart->pgsize) 805 774 return -EINVAL; ··· 940 913 { 941 914 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev); 942 915 943 - if (cfg->stream_maps[0].dart->force_bypass) 916 + if (cfg->stream_maps[0].dart->pgsize > PAGE_SIZE) 944 917 return IOMMU_DOMAIN_IDENTITY; 945 918 if (!cfg->stream_maps[0].dart->supports_bypass) 946 919 return IOMMU_DOMAIN_DMA; ··· 974 947 } 975 948 976 949 static const struct iommu_ops apple_dart_iommu_ops = { 977 - .domain_alloc = apple_dart_domain_alloc, 950 + .identity_domain = &apple_dart_identity_domain, 951 + .blocked_domain = &apple_dart_blocked_domain, 952 + .domain_alloc_paging = apple_dart_domain_alloc_paging, 978 953 .probe_device = apple_dart_probe_device, 979 954 .release_device = apple_dart_release_device, 980 955 .device_group = apple_dart_device_group, ··· 986 957 .pgsize_bitmap = -1UL, /* Restricted during dart probe */ 987 958 .owner = THIS_MODULE, 988 959 .default_domain_ops = &(const struct iommu_domain_ops) { 989 - .attach_dev = apple_dart_attach_dev, 960 + .attach_dev = apple_dart_attach_dev_paging, 990 961 .map_pages = apple_dart_map_pages, 991 962 .unmap_pages = apple_dart_unmap_pages, 992 963 .flush_iotlb_all = apple_dart_flush_iotlb_all, ··· 1140 1111 goto err_clk_disable; 1141 1112 } 1142 1113 1143 - dart->force_bypass = dart->pgsize > PAGE_SIZE; 1144 - 1145 1114 ret = apple_dart_hw_reset(dart); 1146 1115 if (ret) 1147 1116 goto err_clk_disable; ··· 1163 1136 dev_info( 1164 1137 &pdev->dev, 1165 1138 "DART [pagesize %x, %d streams, bypass support: %d, bypass forced: %d] initialized\n", 1166 - dart->pgsize, dart->num_streams, dart->supports_bypass, dart->force_bypass); 1139 + dart->pgsize, dart->num_streams, dart->supports_bypass, 1140 + dart->pgsize > PAGE_SIZE); 1167 1141 return 0; 1168 1142 1169 1143 err_sysfs_remove:
+43 -28
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c
··· 25 25 #define mn_to_smmu(mn) container_of(mn, struct arm_smmu_mmu_notifier, mn) 26 26 27 27 struct arm_smmu_bond { 28 - struct iommu_sva sva; 29 28 struct mm_struct *mm; 30 29 struct arm_smmu_mmu_notifier *smmu_mn; 31 30 struct list_head list; 32 - refcount_t refs; 33 31 }; 34 32 35 33 #define sva_to_bond(handle) \ 36 34 container_of(handle, struct arm_smmu_bond, sva) 37 35 38 36 static DEFINE_MUTEX(sva_lock); 37 + 38 + /* 39 + * Write the CD to the CD tables for all masters that this domain is attached 40 + * to. Note that this is only used to update existing CD entries in the target 41 + * CD table, for which it's assumed that arm_smmu_write_ctx_desc can't fail. 42 + */ 43 + static void arm_smmu_update_ctx_desc_devices(struct arm_smmu_domain *smmu_domain, 44 + int ssid, 45 + struct arm_smmu_ctx_desc *cd) 46 + { 47 + struct arm_smmu_master *master; 48 + unsigned long flags; 49 + 50 + spin_lock_irqsave(&smmu_domain->devices_lock, flags); 51 + list_for_each_entry(master, &smmu_domain->devices, domain_head) { 52 + arm_smmu_write_ctx_desc(master, ssid, cd); 53 + } 54 + spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); 55 + } 39 56 40 57 /* 41 58 * Check if the CPU ASID is available on the SMMU side. If a private context ··· 79 62 return cd; 80 63 } 81 64 82 - smmu_domain = container_of(cd, struct arm_smmu_domain, s1_cfg.cd); 65 + smmu_domain = container_of(cd, struct arm_smmu_domain, cd); 83 66 smmu = smmu_domain->smmu; 84 67 85 68 ret = xa_alloc(&arm_smmu_asid_xa, &new_asid, cd, ··· 97 80 * be some overlap between use of both ASIDs, until we invalidate the 98 81 * TLB. 99 82 */ 100 - arm_smmu_write_ctx_desc(smmu_domain, IOMMU_NO_PASID, cd); 83 + arm_smmu_update_ctx_desc_devices(smmu_domain, IOMMU_NO_PASID, cd); 101 84 102 85 /* Invalidate TLB entries previously associated with that context */ 103 86 arm_smmu_tlb_inv_asid(smmu, asid); ··· 264 247 * DMA may still be running. Keep the cd valid to avoid C_BAD_CD events, 265 248 * but disable translation. 266 249 */ 267 - arm_smmu_write_ctx_desc(smmu_domain, mm->pasid, &quiet_cd); 250 + arm_smmu_update_ctx_desc_devices(smmu_domain, mm->pasid, &quiet_cd); 268 251 269 252 arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_mn->cd->asid); 270 253 arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, 0, 0); ··· 290 273 struct mm_struct *mm) 291 274 { 292 275 int ret; 276 + unsigned long flags; 293 277 struct arm_smmu_ctx_desc *cd; 294 278 struct arm_smmu_mmu_notifier *smmu_mn; 279 + struct arm_smmu_master *master; 295 280 296 281 list_for_each_entry(smmu_mn, &smmu_domain->mmu_notifiers, list) { 297 282 if (smmu_mn->mn.mm == mm) { ··· 323 304 goto err_free_cd; 324 305 } 325 306 326 - ret = arm_smmu_write_ctx_desc(smmu_domain, mm->pasid, cd); 307 + spin_lock_irqsave(&smmu_domain->devices_lock, flags); 308 + list_for_each_entry(master, &smmu_domain->devices, domain_head) { 309 + ret = arm_smmu_write_ctx_desc(master, mm->pasid, cd); 310 + if (ret) { 311 + list_for_each_entry_from_reverse(master, &smmu_domain->devices, domain_head) 312 + arm_smmu_write_ctx_desc(master, mm->pasid, NULL); 313 + break; 314 + } 315 + } 316 + spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); 327 317 if (ret) 328 318 goto err_put_notifier; 329 319 ··· 357 329 return; 358 330 359 331 list_del(&smmu_mn->list); 360 - arm_smmu_write_ctx_desc(smmu_domain, mm->pasid, NULL); 332 + 333 + arm_smmu_update_ctx_desc_devices(smmu_domain, mm->pasid, NULL); 361 334 362 335 /* 363 336 * If we went through clear(), we've already invalidated, and no ··· 374 345 arm_smmu_free_shared_cd(cd); 375 346 } 376 347 377 - static struct iommu_sva * 378 - __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) 348 + static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) 379 349 { 380 350 int ret; 381 351 struct arm_smmu_bond *bond; ··· 383 355 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 384 356 385 357 if (!master || !master->sva_enabled) 386 - return ERR_PTR(-ENODEV); 387 - 388 - /* If bind() was already called for this {dev, mm} pair, reuse it. */ 389 - list_for_each_entry(bond, &master->bonds, list) { 390 - if (bond->mm == mm) { 391 - refcount_inc(&bond->refs); 392 - return &bond->sva; 393 - } 394 - } 358 + return -ENODEV; 395 359 396 360 bond = kzalloc(sizeof(*bond), GFP_KERNEL); 397 361 if (!bond) 398 - return ERR_PTR(-ENOMEM); 362 + return -ENOMEM; 399 363 400 364 bond->mm = mm; 401 - bond->sva.dev = dev; 402 - refcount_set(&bond->refs, 1); 403 365 404 366 bond->smmu_mn = arm_smmu_mmu_notifier_get(smmu_domain, mm); 405 367 if (IS_ERR(bond->smmu_mn)) { ··· 398 380 } 399 381 400 382 list_add(&bond->list, &master->bonds); 401 - return &bond->sva; 383 + return 0; 402 384 403 385 err_free_bond: 404 386 kfree(bond); 405 - return ERR_PTR(ret); 387 + return ret; 406 388 } 407 389 408 390 bool arm_smmu_sva_supported(struct arm_smmu_device *smmu) ··· 568 550 } 569 551 } 570 552 571 - if (!WARN_ON(!bond) && refcount_dec_and_test(&bond->refs)) { 553 + if (!WARN_ON(!bond)) { 572 554 list_del(&bond->list); 573 555 arm_smmu_mmu_notifier_put(bond->smmu_mn); 574 556 kfree(bond); ··· 580 562 struct device *dev, ioasid_t id) 581 563 { 582 564 int ret = 0; 583 - struct iommu_sva *handle; 584 565 struct mm_struct *mm = domain->mm; 585 566 586 567 mutex_lock(&sva_lock); 587 - handle = __arm_smmu_sva_bind(dev, mm); 588 - if (IS_ERR(handle)) 589 - ret = PTR_ERR(handle); 568 + ret = __arm_smmu_sva_bind(dev, mm); 590 569 mutex_unlock(&sva_lock); 591 570 592 571 return ret;
+123 -130
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
··· 971 971 arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd); 972 972 } 973 973 974 - static void arm_smmu_sync_cd(struct arm_smmu_domain *smmu_domain, 974 + static void arm_smmu_sync_cd(struct arm_smmu_master *master, 975 975 int ssid, bool leaf) 976 976 { 977 977 size_t i; 978 - unsigned long flags; 979 - struct arm_smmu_master *master; 980 978 struct arm_smmu_cmdq_batch cmds; 981 - struct arm_smmu_device *smmu = smmu_domain->smmu; 979 + struct arm_smmu_device *smmu = master->smmu; 982 980 struct arm_smmu_cmdq_ent cmd = { 983 981 .opcode = CMDQ_OP_CFGI_CD, 984 982 .cfgi = { ··· 986 988 }; 987 989 988 990 cmds.num = 0; 989 - 990 - spin_lock_irqsave(&smmu_domain->devices_lock, flags); 991 - list_for_each_entry(master, &smmu_domain->devices, domain_head) { 992 - for (i = 0; i < master->num_streams; i++) { 993 - cmd.cfgi.sid = master->streams[i].id; 994 - arm_smmu_cmdq_batch_add(smmu, &cmds, &cmd); 995 - } 991 + for (i = 0; i < master->num_streams; i++) { 992 + cmd.cfgi.sid = master->streams[i].id; 993 + arm_smmu_cmdq_batch_add(smmu, &cmds, &cmd); 996 994 } 997 - spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); 998 995 999 996 arm_smmu_cmdq_batch_submit(smmu, &cmds); 1000 997 } ··· 1019 1026 WRITE_ONCE(*dst, cpu_to_le64(val)); 1020 1027 } 1021 1028 1022 - static __le64 *arm_smmu_get_cd_ptr(struct arm_smmu_domain *smmu_domain, 1023 - u32 ssid) 1029 + static __le64 *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid) 1024 1030 { 1025 1031 __le64 *l1ptr; 1026 1032 unsigned int idx; 1027 1033 struct arm_smmu_l1_ctx_desc *l1_desc; 1028 - struct arm_smmu_device *smmu = smmu_domain->smmu; 1029 - struct arm_smmu_ctx_desc_cfg *cdcfg = &smmu_domain->s1_cfg.cdcfg; 1034 + struct arm_smmu_device *smmu = master->smmu; 1035 + struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; 1030 1036 1031 - if (smmu_domain->s1_cfg.s1fmt == STRTAB_STE_0_S1FMT_LINEAR) 1032 - return cdcfg->cdtab + ssid * CTXDESC_CD_DWORDS; 1037 + if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_LINEAR) 1038 + return cd_table->cdtab + ssid * CTXDESC_CD_DWORDS; 1033 1039 1034 1040 idx = ssid >> CTXDESC_SPLIT; 1035 - l1_desc = &cdcfg->l1_desc[idx]; 1041 + l1_desc = &cd_table->l1_desc[idx]; 1036 1042 if (!l1_desc->l2ptr) { 1037 1043 if (arm_smmu_alloc_cd_leaf_table(smmu, l1_desc)) 1038 1044 return NULL; 1039 1045 1040 - l1ptr = cdcfg->cdtab + idx * CTXDESC_L1_DESC_DWORDS; 1046 + l1ptr = cd_table->cdtab + idx * CTXDESC_L1_DESC_DWORDS; 1041 1047 arm_smmu_write_cd_l1_desc(l1ptr, l1_desc); 1042 1048 /* An invalid L1CD can be cached */ 1043 - arm_smmu_sync_cd(smmu_domain, ssid, false); 1049 + arm_smmu_sync_cd(master, ssid, false); 1044 1050 } 1045 1051 idx = ssid & (CTXDESC_L2_ENTRIES - 1); 1046 1052 return l1_desc->l2ptr + idx * CTXDESC_CD_DWORDS; 1047 1053 } 1048 1054 1049 - int arm_smmu_write_ctx_desc(struct arm_smmu_domain *smmu_domain, int ssid, 1055 + int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, 1050 1056 struct arm_smmu_ctx_desc *cd) 1051 1057 { 1052 1058 /* ··· 1062 1070 u64 val; 1063 1071 bool cd_live; 1064 1072 __le64 *cdptr; 1073 + struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; 1065 1074 1066 - if (WARN_ON(ssid >= (1 << smmu_domain->s1_cfg.s1cdmax))) 1075 + if (WARN_ON(ssid >= (1 << cd_table->s1cdmax))) 1067 1076 return -E2BIG; 1068 1077 1069 - cdptr = arm_smmu_get_cd_ptr(smmu_domain, ssid); 1078 + cdptr = arm_smmu_get_cd_ptr(master, ssid); 1070 1079 if (!cdptr) 1071 1080 return -ENOMEM; 1072 1081 ··· 1091 1098 cdptr[3] = cpu_to_le64(cd->mair); 1092 1099 1093 1100 /* 1094 - * STE is live, and the SMMU might read dwords of this CD in any 1101 + * STE may be live, and the SMMU might read dwords of this CD in any 1095 1102 * order. Ensure that it observes valid values before reading 1096 1103 * V=1. 1097 1104 */ 1098 - arm_smmu_sync_cd(smmu_domain, ssid, true); 1105 + arm_smmu_sync_cd(master, ssid, true); 1099 1106 1100 1107 val = cd->tcr | 1101 1108 #ifdef __BIG_ENDIAN ··· 1107 1114 FIELD_PREP(CTXDESC_CD_0_ASID, cd->asid) | 1108 1115 CTXDESC_CD_0_V; 1109 1116 1110 - if (smmu_domain->stall_enabled) 1117 + if (cd_table->stall_enabled) 1111 1118 val |= CTXDESC_CD_0_S; 1112 1119 } 1113 1120 ··· 1121 1128 * without first making the structure invalid. 1122 1129 */ 1123 1130 WRITE_ONCE(cdptr[0], cpu_to_le64(val)); 1124 - arm_smmu_sync_cd(smmu_domain, ssid, true); 1131 + arm_smmu_sync_cd(master, ssid, true); 1125 1132 return 0; 1126 1133 } 1127 1134 1128 - static int arm_smmu_alloc_cd_tables(struct arm_smmu_domain *smmu_domain) 1135 + static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master) 1129 1136 { 1130 1137 int ret; 1131 1138 size_t l1size; 1132 1139 size_t max_contexts; 1133 - struct arm_smmu_device *smmu = smmu_domain->smmu; 1134 - struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg; 1135 - struct arm_smmu_ctx_desc_cfg *cdcfg = &cfg->cdcfg; 1140 + struct arm_smmu_device *smmu = master->smmu; 1141 + struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; 1136 1142 1137 - max_contexts = 1 << cfg->s1cdmax; 1143 + cd_table->stall_enabled = master->stall_enabled; 1144 + cd_table->s1cdmax = master->ssid_bits; 1145 + max_contexts = 1 << cd_table->s1cdmax; 1138 1146 1139 1147 if (!(smmu->features & ARM_SMMU_FEAT_2_LVL_CDTAB) || 1140 1148 max_contexts <= CTXDESC_L2_ENTRIES) { 1141 - cfg->s1fmt = STRTAB_STE_0_S1FMT_LINEAR; 1142 - cdcfg->num_l1_ents = max_contexts; 1149 + cd_table->s1fmt = STRTAB_STE_0_S1FMT_LINEAR; 1150 + cd_table->num_l1_ents = max_contexts; 1143 1151 1144 1152 l1size = max_contexts * (CTXDESC_CD_DWORDS << 3); 1145 1153 } else { 1146 - cfg->s1fmt = STRTAB_STE_0_S1FMT_64K_L2; 1147 - cdcfg->num_l1_ents = DIV_ROUND_UP(max_contexts, 1154 + cd_table->s1fmt = STRTAB_STE_0_S1FMT_64K_L2; 1155 + cd_table->num_l1_ents = DIV_ROUND_UP(max_contexts, 1148 1156 CTXDESC_L2_ENTRIES); 1149 1157 1150 - cdcfg->l1_desc = devm_kcalloc(smmu->dev, cdcfg->num_l1_ents, 1151 - sizeof(*cdcfg->l1_desc), 1158 + cd_table->l1_desc = devm_kcalloc(smmu->dev, cd_table->num_l1_ents, 1159 + sizeof(*cd_table->l1_desc), 1152 1160 GFP_KERNEL); 1153 - if (!cdcfg->l1_desc) 1161 + if (!cd_table->l1_desc) 1154 1162 return -ENOMEM; 1155 1163 1156 - l1size = cdcfg->num_l1_ents * (CTXDESC_L1_DESC_DWORDS << 3); 1164 + l1size = cd_table->num_l1_ents * (CTXDESC_L1_DESC_DWORDS << 3); 1157 1165 } 1158 1166 1159 - cdcfg->cdtab = dmam_alloc_coherent(smmu->dev, l1size, &cdcfg->cdtab_dma, 1167 + cd_table->cdtab = dmam_alloc_coherent(smmu->dev, l1size, &cd_table->cdtab_dma, 1160 1168 GFP_KERNEL); 1161 - if (!cdcfg->cdtab) { 1169 + if (!cd_table->cdtab) { 1162 1170 dev_warn(smmu->dev, "failed to allocate context descriptor\n"); 1163 1171 ret = -ENOMEM; 1164 1172 goto err_free_l1; ··· 1168 1174 return 0; 1169 1175 1170 1176 err_free_l1: 1171 - if (cdcfg->l1_desc) { 1172 - devm_kfree(smmu->dev, cdcfg->l1_desc); 1173 - cdcfg->l1_desc = NULL; 1177 + if (cd_table->l1_desc) { 1178 + devm_kfree(smmu->dev, cd_table->l1_desc); 1179 + cd_table->l1_desc = NULL; 1174 1180 } 1175 1181 return ret; 1176 1182 } 1177 1183 1178 - static void arm_smmu_free_cd_tables(struct arm_smmu_domain *smmu_domain) 1184 + static void arm_smmu_free_cd_tables(struct arm_smmu_master *master) 1179 1185 { 1180 1186 int i; 1181 1187 size_t size, l1size; 1182 - struct arm_smmu_device *smmu = smmu_domain->smmu; 1183 - struct arm_smmu_ctx_desc_cfg *cdcfg = &smmu_domain->s1_cfg.cdcfg; 1188 + struct arm_smmu_device *smmu = master->smmu; 1189 + struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; 1184 1190 1185 - if (cdcfg->l1_desc) { 1191 + if (cd_table->l1_desc) { 1186 1192 size = CTXDESC_L2_ENTRIES * (CTXDESC_CD_DWORDS << 3); 1187 1193 1188 - for (i = 0; i < cdcfg->num_l1_ents; i++) { 1189 - if (!cdcfg->l1_desc[i].l2ptr) 1194 + for (i = 0; i < cd_table->num_l1_ents; i++) { 1195 + if (!cd_table->l1_desc[i].l2ptr) 1190 1196 continue; 1191 1197 1192 1198 dmam_free_coherent(smmu->dev, size, 1193 - cdcfg->l1_desc[i].l2ptr, 1194 - cdcfg->l1_desc[i].l2ptr_dma); 1199 + cd_table->l1_desc[i].l2ptr, 1200 + cd_table->l1_desc[i].l2ptr_dma); 1195 1201 } 1196 - devm_kfree(smmu->dev, cdcfg->l1_desc); 1197 - cdcfg->l1_desc = NULL; 1202 + devm_kfree(smmu->dev, cd_table->l1_desc); 1203 + cd_table->l1_desc = NULL; 1198 1204 1199 - l1size = cdcfg->num_l1_ents * (CTXDESC_L1_DESC_DWORDS << 3); 1205 + l1size = cd_table->num_l1_ents * (CTXDESC_L1_DESC_DWORDS << 3); 1200 1206 } else { 1201 - l1size = cdcfg->num_l1_ents * (CTXDESC_CD_DWORDS << 3); 1207 + l1size = cd_table->num_l1_ents * (CTXDESC_CD_DWORDS << 3); 1202 1208 } 1203 1209 1204 - dmam_free_coherent(smmu->dev, l1size, cdcfg->cdtab, cdcfg->cdtab_dma); 1205 - cdcfg->cdtab_dma = 0; 1206 - cdcfg->cdtab = NULL; 1210 + dmam_free_coherent(smmu->dev, l1size, cd_table->cdtab, cd_table->cdtab_dma); 1211 + cd_table->cdtab_dma = 0; 1212 + cd_table->cdtab = NULL; 1207 1213 } 1208 1214 1209 1215 bool arm_smmu_free_asid(struct arm_smmu_ctx_desc *cd) ··· 1270 1276 u64 val = le64_to_cpu(dst[0]); 1271 1277 bool ste_live = false; 1272 1278 struct arm_smmu_device *smmu = NULL; 1273 - struct arm_smmu_s1_cfg *s1_cfg = NULL; 1279 + struct arm_smmu_ctx_desc_cfg *cd_table = NULL; 1274 1280 struct arm_smmu_s2_cfg *s2_cfg = NULL; 1275 1281 struct arm_smmu_domain *smmu_domain = NULL; 1276 1282 struct arm_smmu_cmdq_ent prefetch_cmd = { ··· 1288 1294 if (smmu_domain) { 1289 1295 switch (smmu_domain->stage) { 1290 1296 case ARM_SMMU_DOMAIN_S1: 1291 - s1_cfg = &smmu_domain->s1_cfg; 1297 + cd_table = &master->cd_table; 1292 1298 break; 1293 1299 case ARM_SMMU_DOMAIN_S2: 1294 1300 case ARM_SMMU_DOMAIN_NESTED: ··· 1319 1325 val = STRTAB_STE_0_V; 1320 1326 1321 1327 /* Bypass/fault */ 1322 - if (!smmu_domain || !(s1_cfg || s2_cfg)) { 1328 + if (!smmu_domain || !(cd_table || s2_cfg)) { 1323 1329 if (!smmu_domain && disable_bypass) 1324 1330 val |= FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_ABORT); 1325 1331 else ··· 1338 1344 return; 1339 1345 } 1340 1346 1341 - if (s1_cfg) { 1347 + if (cd_table) { 1342 1348 u64 strw = smmu->features & ARM_SMMU_FEAT_E2H ? 1343 1349 STRTAB_STE_1_STRW_EL2 : STRTAB_STE_1_STRW_NSEL1; 1344 1350 ··· 1354 1360 !master->stall_enabled) 1355 1361 dst[1] |= cpu_to_le64(STRTAB_STE_1_S1STALLD); 1356 1362 1357 - val |= (s1_cfg->cdcfg.cdtab_dma & STRTAB_STE_0_S1CTXPTR_MASK) | 1363 + val |= (cd_table->cdtab_dma & STRTAB_STE_0_S1CTXPTR_MASK) | 1358 1364 FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_S1_TRANS) | 1359 - FIELD_PREP(STRTAB_STE_0_S1CDMAX, s1_cfg->s1cdmax) | 1360 - FIELD_PREP(STRTAB_STE_0_S1FMT, s1_cfg->s1fmt); 1365 + FIELD_PREP(STRTAB_STE_0_S1CDMAX, cd_table->s1cdmax) | 1366 + FIELD_PREP(STRTAB_STE_0_S1FMT, cd_table->s1fmt); 1361 1367 } 1362 1368 1363 1369 if (s2_cfg) { ··· 1863 1869 * careful, 007. 1864 1870 */ 1865 1871 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { 1866 - arm_smmu_tlb_inv_asid(smmu, smmu_domain->s1_cfg.cd.asid); 1872 + arm_smmu_tlb_inv_asid(smmu, smmu_domain->cd.asid); 1867 1873 } else { 1868 1874 cmd.opcode = CMDQ_OP_TLBI_S12_VMALL; 1869 1875 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid; ··· 1956 1962 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { 1957 1963 cmd.opcode = smmu_domain->smmu->features & ARM_SMMU_FEAT_E2H ? 1958 1964 CMDQ_OP_TLBI_EL2_VA : CMDQ_OP_TLBI_NH_VA; 1959 - cmd.tlbi.asid = smmu_domain->s1_cfg.cd.asid; 1965 + cmd.tlbi.asid = smmu_domain->cd.asid; 1960 1966 } else { 1961 1967 cmd.opcode = CMDQ_OP_TLBI_S2_IPA; 1962 1968 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid; ··· 2061 2067 2062 2068 free_io_pgtable_ops(smmu_domain->pgtbl_ops); 2063 2069 2064 - /* Free the CD and ASID, if we allocated them */ 2070 + /* Free the ASID or VMID */ 2065 2071 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { 2066 - struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg; 2067 - 2068 2072 /* Prevent SVA from touching the CD while we're freeing it */ 2069 2073 mutex_lock(&arm_smmu_asid_lock); 2070 - if (cfg->cdcfg.cdtab) 2071 - arm_smmu_free_cd_tables(smmu_domain); 2072 - arm_smmu_free_asid(&cfg->cd); 2074 + arm_smmu_free_asid(&smmu_domain->cd); 2073 2075 mutex_unlock(&arm_smmu_asid_lock); 2074 2076 } else { 2075 2077 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; ··· 2077 2087 } 2078 2088 2079 2089 static int arm_smmu_domain_finalise_s1(struct arm_smmu_domain *smmu_domain, 2080 - struct arm_smmu_master *master, 2081 2090 struct io_pgtable_cfg *pgtbl_cfg) 2082 2091 { 2083 2092 int ret; 2084 2093 u32 asid; 2085 2094 struct arm_smmu_device *smmu = smmu_domain->smmu; 2086 - struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg; 2095 + struct arm_smmu_ctx_desc *cd = &smmu_domain->cd; 2087 2096 typeof(&pgtbl_cfg->arm_lpae_s1_cfg.tcr) tcr = &pgtbl_cfg->arm_lpae_s1_cfg.tcr; 2088 2097 2089 - refcount_set(&cfg->cd.refs, 1); 2098 + refcount_set(&cd->refs, 1); 2090 2099 2091 2100 /* Prevent SVA from modifying the ASID until it is written to the CD */ 2092 2101 mutex_lock(&arm_smmu_asid_lock); 2093 - ret = xa_alloc(&arm_smmu_asid_xa, &asid, &cfg->cd, 2102 + ret = xa_alloc(&arm_smmu_asid_xa, &asid, cd, 2094 2103 XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); 2095 2104 if (ret) 2096 2105 goto out_unlock; 2097 2106 2098 - cfg->s1cdmax = master->ssid_bits; 2099 - 2100 - smmu_domain->stall_enabled = master->stall_enabled; 2101 - 2102 - ret = arm_smmu_alloc_cd_tables(smmu_domain); 2103 - if (ret) 2104 - goto out_free_asid; 2105 - 2106 - cfg->cd.asid = (u16)asid; 2107 - cfg->cd.ttbr = pgtbl_cfg->arm_lpae_s1_cfg.ttbr; 2108 - cfg->cd.tcr = FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, tcr->tsz) | 2107 + cd->asid = (u16)asid; 2108 + cd->ttbr = pgtbl_cfg->arm_lpae_s1_cfg.ttbr; 2109 + cd->tcr = FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, tcr->tsz) | 2109 2110 FIELD_PREP(CTXDESC_CD_0_TCR_TG0, tcr->tg) | 2110 2111 FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, tcr->irgn) | 2111 2112 FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, tcr->orgn) | 2112 2113 FIELD_PREP(CTXDESC_CD_0_TCR_SH0, tcr->sh) | 2113 2114 FIELD_PREP(CTXDESC_CD_0_TCR_IPS, tcr->ips) | 2114 2115 CTXDESC_CD_0_TCR_EPD1 | CTXDESC_CD_0_AA64; 2115 - cfg->cd.mair = pgtbl_cfg->arm_lpae_s1_cfg.mair; 2116 - 2117 - /* 2118 - * Note that this will end up calling arm_smmu_sync_cd() before 2119 - * the master has been added to the devices list for this domain. 2120 - * This isn't an issue because the STE hasn't been installed yet. 2121 - */ 2122 - ret = arm_smmu_write_ctx_desc(smmu_domain, IOMMU_NO_PASID, &cfg->cd); 2123 - if (ret) 2124 - goto out_free_cd_tables; 2116 + cd->mair = pgtbl_cfg->arm_lpae_s1_cfg.mair; 2125 2117 2126 2118 mutex_unlock(&arm_smmu_asid_lock); 2127 2119 return 0; 2128 2120 2129 - out_free_cd_tables: 2130 - arm_smmu_free_cd_tables(smmu_domain); 2131 - out_free_asid: 2132 - arm_smmu_free_asid(&cfg->cd); 2133 2121 out_unlock: 2134 2122 mutex_unlock(&arm_smmu_asid_lock); 2135 2123 return ret; 2136 2124 } 2137 2125 2138 2126 static int arm_smmu_domain_finalise_s2(struct arm_smmu_domain *smmu_domain, 2139 - struct arm_smmu_master *master, 2140 2127 struct io_pgtable_cfg *pgtbl_cfg) 2141 2128 { 2142 2129 int vmid; ··· 2140 2173 return 0; 2141 2174 } 2142 2175 2143 - static int arm_smmu_domain_finalise(struct iommu_domain *domain, 2144 - struct arm_smmu_master *master) 2176 + static int arm_smmu_domain_finalise(struct iommu_domain *domain) 2145 2177 { 2146 2178 int ret; 2147 2179 unsigned long ias, oas; ··· 2148 2182 struct io_pgtable_cfg pgtbl_cfg; 2149 2183 struct io_pgtable_ops *pgtbl_ops; 2150 2184 int (*finalise_stage_fn)(struct arm_smmu_domain *, 2151 - struct arm_smmu_master *, 2152 2185 struct io_pgtable_cfg *); 2153 2186 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 2154 2187 struct arm_smmu_device *smmu = smmu_domain->smmu; ··· 2199 2234 domain->geometry.aperture_end = (1UL << pgtbl_cfg.ias) - 1; 2200 2235 domain->geometry.force_aperture = true; 2201 2236 2202 - ret = finalise_stage_fn(smmu_domain, master, &pgtbl_cfg); 2237 + ret = finalise_stage_fn(smmu_domain, &pgtbl_cfg); 2203 2238 if (ret < 0) { 2204 2239 free_io_pgtable_ops(pgtbl_ops); 2205 2240 return ret; ··· 2368 2403 master->domain = NULL; 2369 2404 master->ats_enabled = false; 2370 2405 arm_smmu_install_ste_for_dev(master); 2406 + /* 2407 + * Clearing the CD entry isn't strictly required to detach the domain 2408 + * since the table is uninstalled anyway, but it helps avoid confusion 2409 + * in the call to arm_smmu_write_ctx_desc on the next attach (which 2410 + * expects the entry to be empty). 2411 + */ 2412 + if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1 && master->cd_table.cdtab) 2413 + arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, NULL); 2371 2414 } 2372 2415 2373 2416 static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) ··· 2409 2436 2410 2437 if (!smmu_domain->smmu) { 2411 2438 smmu_domain->smmu = smmu; 2412 - ret = arm_smmu_domain_finalise(domain, master); 2413 - if (ret) { 2439 + ret = arm_smmu_domain_finalise(domain); 2440 + if (ret) 2414 2441 smmu_domain->smmu = NULL; 2415 - goto out_unlock; 2416 - } 2417 - } else if (smmu_domain->smmu != smmu) { 2442 + } else if (smmu_domain->smmu != smmu) 2418 2443 ret = -EINVAL; 2419 - goto out_unlock; 2420 - } else if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1 && 2421 - master->ssid_bits != smmu_domain->s1_cfg.s1cdmax) { 2422 - ret = -EINVAL; 2423 - goto out_unlock; 2424 - } else if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1 && 2425 - smmu_domain->stall_enabled != master->stall_enabled) { 2426 - ret = -EINVAL; 2427 - goto out_unlock; 2428 - } 2444 + 2445 + mutex_unlock(&smmu_domain->init_mutex); 2446 + if (ret) 2447 + return ret; 2429 2448 2430 2449 master->domain = smmu_domain; 2431 2450 ··· 2431 2466 if (smmu_domain->stage != ARM_SMMU_DOMAIN_BYPASS) 2432 2467 master->ats_enabled = arm_smmu_ats_supported(master); 2433 2468 2434 - arm_smmu_install_ste_for_dev(master); 2435 - 2436 2469 spin_lock_irqsave(&smmu_domain->devices_lock, flags); 2437 2470 list_add(&master->domain_head, &smmu_domain->devices); 2438 2471 spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); 2439 2472 2440 - arm_smmu_enable_ats(master); 2473 + if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { 2474 + if (!master->cd_table.cdtab) { 2475 + ret = arm_smmu_alloc_cd_tables(master); 2476 + if (ret) { 2477 + master->domain = NULL; 2478 + goto out_list_del; 2479 + } 2480 + } 2441 2481 2442 - out_unlock: 2443 - mutex_unlock(&smmu_domain->init_mutex); 2482 + /* 2483 + * Prevent SVA from concurrently modifying the CD or writing to 2484 + * the CD entry 2485 + */ 2486 + mutex_lock(&arm_smmu_asid_lock); 2487 + ret = arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, &smmu_domain->cd); 2488 + mutex_unlock(&arm_smmu_asid_lock); 2489 + if (ret) { 2490 + master->domain = NULL; 2491 + goto out_list_del; 2492 + } 2493 + } 2494 + 2495 + arm_smmu_install_ste_for_dev(master); 2496 + 2497 + arm_smmu_enable_ats(master); 2498 + return 0; 2499 + 2500 + out_list_del: 2501 + spin_lock_irqsave(&smmu_domain->devices_lock, flags); 2502 + list_del(&master->domain_head); 2503 + spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); 2504 + 2444 2505 return ret; 2445 2506 } 2446 2507 ··· 2711 2720 arm_smmu_detach_dev(master); 2712 2721 arm_smmu_disable_pasid(master); 2713 2722 arm_smmu_remove_master(master); 2723 + if (master->cd_table.cdtab) 2724 + arm_smmu_free_cd_tables(master); 2714 2725 kfree(master); 2715 2726 } 2716 2727
+8 -9
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
··· 595 595 dma_addr_t cdtab_dma; 596 596 struct arm_smmu_l1_ctx_desc *l1_desc; 597 597 unsigned int num_l1_ents; 598 - }; 599 - 600 - struct arm_smmu_s1_cfg { 601 - struct arm_smmu_ctx_desc_cfg cdcfg; 602 - struct arm_smmu_ctx_desc cd; 603 598 u8 s1fmt; 599 + /* log2 of the maximum number of CDs supported by this table */ 604 600 u8 s1cdmax; 601 + /* Whether CD entries in this table have the stall bit set. */ 602 + u8 stall_enabled:1; 605 603 }; 606 604 607 605 struct arm_smmu_s2_cfg { ··· 695 697 struct arm_smmu_domain *domain; 696 698 struct list_head domain_head; 697 699 struct arm_smmu_stream *streams; 700 + /* Locked by the iommu core using the group mutex */ 701 + struct arm_smmu_ctx_desc_cfg cd_table; 698 702 unsigned int num_streams; 699 703 bool ats_enabled; 700 704 bool stall_enabled; ··· 719 719 struct mutex init_mutex; /* Protects smmu pointer */ 720 720 721 721 struct io_pgtable_ops *pgtbl_ops; 722 - bool stall_enabled; 723 722 atomic_t nr_ats_masters; 724 723 725 724 enum arm_smmu_domain_stage stage; 726 725 union { 727 - struct arm_smmu_s1_cfg s1_cfg; 728 - struct arm_smmu_s2_cfg s2_cfg; 726 + struct arm_smmu_ctx_desc cd; 727 + struct arm_smmu_s2_cfg s2_cfg; 729 728 }; 730 729 731 730 struct iommu_domain domain; ··· 744 745 extern struct mutex arm_smmu_asid_lock; 745 746 extern struct arm_smmu_ctx_desc quiet_cd; 746 747 747 - int arm_smmu_write_ctx_desc(struct arm_smmu_domain *smmu_domain, int ssid, 748 + int arm_smmu_write_ctx_desc(struct arm_smmu_master *smmu_master, int ssid, 748 749 struct arm_smmu_ctx_desc *cd); 749 750 void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid); 750 751 void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid,
+2
drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c
··· 251 251 { .compatible = "qcom,sc7280-mss-pil" }, 252 252 { .compatible = "qcom,sc8180x-mdss" }, 253 253 { .compatible = "qcom,sc8280xp-mdss" }, 254 + { .compatible = "qcom,sdm670-mdss" }, 254 255 { .compatible = "qcom,sdm845-mdss" }, 255 256 { .compatible = "qcom,sdm845-mss-pil" }, 256 257 { .compatible = "qcom,sm6350-mdss" }, ··· 533 532 { .compatible = "qcom,sm6350-smmu-500", .data = &qcom_smmu_500_impl0_data }, 534 533 { .compatible = "qcom,sm6375-smmu-v2", .data = &qcom_smmu_v2_data }, 535 534 { .compatible = "qcom,sm6375-smmu-500", .data = &qcom_smmu_500_impl0_data }, 535 + { .compatible = "qcom,sm7150-smmu-v2", .data = &qcom_smmu_v2_data }, 536 536 { .compatible = "qcom,sm8150-smmu-500", .data = &qcom_smmu_500_impl0_data }, 537 537 { .compatible = "qcom,sm8250-smmu-500", .data = &qcom_smmu_500_impl0_data }, 538 538 { .compatible = "qcom,sm8350-smmu-500", .data = &qcom_smmu_500_impl0_data },
+41 -4
drivers/iommu/arm/arm-smmu/qcom_iommu.c
··· 332 332 return ret; 333 333 } 334 334 335 - static struct iommu_domain *qcom_iommu_domain_alloc(unsigned type) 335 + static struct iommu_domain *qcom_iommu_domain_alloc_paging(struct device *dev) 336 336 { 337 337 struct qcom_iommu_domain *qcom_domain; 338 338 339 - if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA) 340 - return NULL; 341 339 /* 342 340 * Allocate the domain and initialise some of its data structures. 343 341 * We can't really do anything meaningful until we've added a ··· 397 399 398 400 return 0; 399 401 } 402 + 403 + static int qcom_iommu_identity_attach(struct iommu_domain *identity_domain, 404 + struct device *dev) 405 + { 406 + struct iommu_domain *domain = iommu_get_domain_for_dev(dev); 407 + struct qcom_iommu_domain *qcom_domain; 408 + struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 409 + struct qcom_iommu_dev *qcom_iommu = to_iommu(dev); 410 + unsigned int i; 411 + 412 + if (domain == identity_domain || !domain) 413 + return 0; 414 + 415 + qcom_domain = to_qcom_iommu_domain(domain); 416 + if (WARN_ON(!qcom_domain->iommu)) 417 + return -EINVAL; 418 + 419 + pm_runtime_get_sync(qcom_iommu->dev); 420 + for (i = 0; i < fwspec->num_ids; i++) { 421 + struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]); 422 + 423 + /* Disable the context bank: */ 424 + iommu_writel(ctx, ARM_SMMU_CB_SCTLR, 0); 425 + 426 + ctx->domain = NULL; 427 + } 428 + pm_runtime_put_sync(qcom_iommu->dev); 429 + return 0; 430 + } 431 + 432 + static struct iommu_domain_ops qcom_iommu_identity_ops = { 433 + .attach_dev = qcom_iommu_identity_attach, 434 + }; 435 + 436 + static struct iommu_domain qcom_iommu_identity_domain = { 437 + .type = IOMMU_DOMAIN_IDENTITY, 438 + .ops = &qcom_iommu_identity_ops, 439 + }; 400 440 401 441 static int qcom_iommu_map(struct iommu_domain *domain, unsigned long iova, 402 442 phys_addr_t paddr, size_t pgsize, size_t pgcount, ··· 601 565 } 602 566 603 567 static const struct iommu_ops qcom_iommu_ops = { 568 + .identity_domain = &qcom_iommu_identity_domain, 604 569 .capable = qcom_iommu_capable, 605 - .domain_alloc = qcom_iommu_domain_alloc, 570 + .domain_alloc_paging = qcom_iommu_domain_alloc_paging, 606 571 .probe_device = qcom_iommu_probe_device, 607 572 .device_group = generic_device_group, 608 573 .of_xlate = qcom_iommu_of_xlate,
+152 -48
drivers/iommu/dma-iommu.c
··· 43 43 IOMMU_DMA_MSI_COOKIE, 44 44 }; 45 45 46 + enum iommu_dma_queue_type { 47 + IOMMU_DMA_OPTS_PER_CPU_QUEUE, 48 + IOMMU_DMA_OPTS_SINGLE_QUEUE, 49 + }; 50 + 51 + struct iommu_dma_options { 52 + enum iommu_dma_queue_type qt; 53 + size_t fq_size; 54 + unsigned int fq_timeout; 55 + }; 56 + 46 57 struct iommu_dma_cookie { 47 58 enum iommu_dma_cookie_type type; 48 59 union { 49 60 /* Full allocator for IOMMU_DMA_IOVA_COOKIE */ 50 61 struct { 51 62 struct iova_domain iovad; 52 - 53 - struct iova_fq __percpu *fq; /* Flush queue */ 63 + /* Flush queue */ 64 + union { 65 + struct iova_fq *single_fq; 66 + struct iova_fq __percpu *percpu_fq; 67 + }; 54 68 /* Number of TLB flushes that have been started */ 55 69 atomic64_t fq_flush_start_cnt; 56 70 /* Number of TLB flushes that have been finished */ ··· 81 67 82 68 /* Domain for flush queue callback; NULL if flush queue not in use */ 83 69 struct iommu_domain *fq_domain; 70 + /* Options for dma-iommu use */ 71 + struct iommu_dma_options options; 84 72 struct mutex mutex; 85 73 }; 86 74 ··· 100 84 early_param("iommu.forcedac", iommu_dma_forcedac_setup); 101 85 102 86 /* Number of entries per flush queue */ 103 - #define IOVA_FQ_SIZE 256 87 + #define IOVA_DEFAULT_FQ_SIZE 256 88 + #define IOVA_SINGLE_FQ_SIZE 32768 104 89 105 90 /* Timeout (in ms) after which entries are flushed from the queue */ 106 - #define IOVA_FQ_TIMEOUT 10 91 + #define IOVA_DEFAULT_FQ_TIMEOUT 10 92 + #define IOVA_SINGLE_FQ_TIMEOUT 1000 107 93 108 94 /* Flush queue entry for deferred flushing */ 109 95 struct iova_fq_entry { ··· 117 99 118 100 /* Per-CPU flush queue structure */ 119 101 struct iova_fq { 120 - struct iova_fq_entry entries[IOVA_FQ_SIZE]; 121 - unsigned int head, tail; 122 102 spinlock_t lock; 103 + unsigned int head, tail; 104 + unsigned int mod_mask; 105 + struct iova_fq_entry entries[]; 123 106 }; 124 107 125 108 #define fq_ring_for_each(i, fq) \ 126 - for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE) 109 + for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) & (fq)->mod_mask) 127 110 128 111 static inline bool fq_full(struct iova_fq *fq) 129 112 { 130 113 assert_spin_locked(&fq->lock); 131 - return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head); 114 + return (((fq->tail + 1) & fq->mod_mask) == fq->head); 132 115 } 133 116 134 117 static inline unsigned int fq_ring_add(struct iova_fq *fq) ··· 138 119 139 120 assert_spin_locked(&fq->lock); 140 121 141 - fq->tail = (idx + 1) % IOVA_FQ_SIZE; 122 + fq->tail = (idx + 1) & fq->mod_mask; 142 123 143 124 return idx; 144 125 } 145 126 146 - static void fq_ring_free(struct iommu_dma_cookie *cookie, struct iova_fq *fq) 127 + static void fq_ring_free_locked(struct iommu_dma_cookie *cookie, struct iova_fq *fq) 147 128 { 148 129 u64 counter = atomic64_read(&cookie->fq_flush_finish_cnt); 149 130 unsigned int idx; ··· 160 141 fq->entries[idx].iova_pfn, 161 142 fq->entries[idx].pages); 162 143 163 - fq->head = (fq->head + 1) % IOVA_FQ_SIZE; 144 + fq->head = (fq->head + 1) & fq->mod_mask; 164 145 } 146 + } 147 + 148 + static void fq_ring_free(struct iommu_dma_cookie *cookie, struct iova_fq *fq) 149 + { 150 + unsigned long flags; 151 + 152 + spin_lock_irqsave(&fq->lock, flags); 153 + fq_ring_free_locked(cookie, fq); 154 + spin_unlock_irqrestore(&fq->lock, flags); 165 155 } 166 156 167 157 static void fq_flush_iotlb(struct iommu_dma_cookie *cookie) ··· 188 160 atomic_set(&cookie->fq_timer_on, 0); 189 161 fq_flush_iotlb(cookie); 190 162 191 - for_each_possible_cpu(cpu) { 192 - unsigned long flags; 193 - struct iova_fq *fq; 194 - 195 - fq = per_cpu_ptr(cookie->fq, cpu); 196 - spin_lock_irqsave(&fq->lock, flags); 197 - fq_ring_free(cookie, fq); 198 - spin_unlock_irqrestore(&fq->lock, flags); 163 + if (cookie->options.qt == IOMMU_DMA_OPTS_SINGLE_QUEUE) { 164 + fq_ring_free(cookie, cookie->single_fq); 165 + } else { 166 + for_each_possible_cpu(cpu) 167 + fq_ring_free(cookie, per_cpu_ptr(cookie->percpu_fq, cpu)); 199 168 } 200 169 } 201 170 ··· 213 188 */ 214 189 smp_mb(); 215 190 216 - fq = raw_cpu_ptr(cookie->fq); 191 + if (cookie->options.qt == IOMMU_DMA_OPTS_SINGLE_QUEUE) 192 + fq = cookie->single_fq; 193 + else 194 + fq = raw_cpu_ptr(cookie->percpu_fq); 195 + 217 196 spin_lock_irqsave(&fq->lock, flags); 218 197 219 198 /* ··· 225 196 * flushed out on another CPU. This makes the fq_full() check below less 226 197 * likely to be true. 227 198 */ 228 - fq_ring_free(cookie, fq); 199 + fq_ring_free_locked(cookie, fq); 229 200 230 201 if (fq_full(fq)) { 231 202 fq_flush_iotlb(cookie); 232 - fq_ring_free(cookie, fq); 203 + fq_ring_free_locked(cookie, fq); 233 204 } 234 205 235 206 idx = fq_ring_add(fq); ··· 245 216 if (!atomic_read(&cookie->fq_timer_on) && 246 217 !atomic_xchg(&cookie->fq_timer_on, 1)) 247 218 mod_timer(&cookie->fq_timer, 248 - jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT)); 219 + jiffies + msecs_to_jiffies(cookie->options.fq_timeout)); 249 220 } 250 221 251 - static void iommu_dma_free_fq(struct iommu_dma_cookie *cookie) 222 + static void iommu_dma_free_fq_single(struct iova_fq *fq) 223 + { 224 + int idx; 225 + 226 + fq_ring_for_each(idx, fq) 227 + put_pages_list(&fq->entries[idx].freelist); 228 + vfree(fq); 229 + } 230 + 231 + static void iommu_dma_free_fq_percpu(struct iova_fq __percpu *percpu_fq) 252 232 { 253 233 int cpu, idx; 254 234 255 - if (!cookie->fq) 256 - return; 257 - 258 - del_timer_sync(&cookie->fq_timer); 259 235 /* The IOVAs will be torn down separately, so just free our queued pages */ 260 236 for_each_possible_cpu(cpu) { 261 - struct iova_fq *fq = per_cpu_ptr(cookie->fq, cpu); 237 + struct iova_fq *fq = per_cpu_ptr(percpu_fq, cpu); 262 238 263 239 fq_ring_for_each(idx, fq) 264 240 put_pages_list(&fq->entries[idx].freelist); 265 241 } 266 242 267 - free_percpu(cookie->fq); 243 + free_percpu(percpu_fq); 244 + } 245 + 246 + static void iommu_dma_free_fq(struct iommu_dma_cookie *cookie) 247 + { 248 + if (!cookie->fq_domain) 249 + return; 250 + 251 + del_timer_sync(&cookie->fq_timer); 252 + if (cookie->options.qt == IOMMU_DMA_OPTS_SINGLE_QUEUE) 253 + iommu_dma_free_fq_single(cookie->single_fq); 254 + else 255 + iommu_dma_free_fq_percpu(cookie->percpu_fq); 256 + } 257 + 258 + static void iommu_dma_init_one_fq(struct iova_fq *fq, size_t fq_size) 259 + { 260 + int i; 261 + 262 + fq->head = 0; 263 + fq->tail = 0; 264 + fq->mod_mask = fq_size - 1; 265 + 266 + spin_lock_init(&fq->lock); 267 + 268 + for (i = 0; i < fq_size; i++) 269 + INIT_LIST_HEAD(&fq->entries[i].freelist); 270 + } 271 + 272 + static int iommu_dma_init_fq_single(struct iommu_dma_cookie *cookie) 273 + { 274 + size_t fq_size = cookie->options.fq_size; 275 + struct iova_fq *queue; 276 + 277 + queue = vmalloc(struct_size(queue, entries, fq_size)); 278 + if (!queue) 279 + return -ENOMEM; 280 + iommu_dma_init_one_fq(queue, fq_size); 281 + cookie->single_fq = queue; 282 + 283 + return 0; 284 + } 285 + 286 + static int iommu_dma_init_fq_percpu(struct iommu_dma_cookie *cookie) 287 + { 288 + size_t fq_size = cookie->options.fq_size; 289 + struct iova_fq __percpu *queue; 290 + int cpu; 291 + 292 + queue = __alloc_percpu(struct_size(queue, entries, fq_size), 293 + __alignof__(*queue)); 294 + if (!queue) 295 + return -ENOMEM; 296 + 297 + for_each_possible_cpu(cpu) 298 + iommu_dma_init_one_fq(per_cpu_ptr(queue, cpu), fq_size); 299 + cookie->percpu_fq = queue; 300 + return 0; 268 301 } 269 302 270 303 /* sysfs updates are serialised by the mutex of the group owning @domain */ 271 304 int iommu_dma_init_fq(struct iommu_domain *domain) 272 305 { 273 306 struct iommu_dma_cookie *cookie = domain->iova_cookie; 274 - struct iova_fq __percpu *queue; 275 - int i, cpu; 307 + int rc; 276 308 277 309 if (cookie->fq_domain) 278 310 return 0; ··· 341 251 atomic64_set(&cookie->fq_flush_start_cnt, 0); 342 252 atomic64_set(&cookie->fq_flush_finish_cnt, 0); 343 253 344 - queue = alloc_percpu(struct iova_fq); 345 - if (!queue) { 254 + if (cookie->options.qt == IOMMU_DMA_OPTS_SINGLE_QUEUE) 255 + rc = iommu_dma_init_fq_single(cookie); 256 + else 257 + rc = iommu_dma_init_fq_percpu(cookie); 258 + 259 + if (rc) { 346 260 pr_warn("iova flush queue initialization failed\n"); 347 261 return -ENOMEM; 348 262 } 349 - 350 - for_each_possible_cpu(cpu) { 351 - struct iova_fq *fq = per_cpu_ptr(queue, cpu); 352 - 353 - fq->head = 0; 354 - fq->tail = 0; 355 - 356 - spin_lock_init(&fq->lock); 357 - 358 - for (i = 0; i < IOVA_FQ_SIZE; i++) 359 - INIT_LIST_HEAD(&fq->entries[i].freelist); 360 - } 361 - 362 - cookie->fq = queue; 363 263 364 264 timer_setup(&cookie->fq_timer, fq_flush_timeout, 0); 365 265 atomic_set(&cookie->fq_timer_on, 0); ··· 635 555 } 636 556 637 557 /** 558 + * iommu_dma_init_options - Initialize dma-iommu options 559 + * @options: The options to be initialized 560 + * @dev: Device the options are set for 561 + * 562 + * This allows tuning dma-iommu specific to device properties 563 + */ 564 + static void iommu_dma_init_options(struct iommu_dma_options *options, 565 + struct device *dev) 566 + { 567 + /* Shadowing IOTLB flushes do better with a single large queue */ 568 + if (dev->iommu->shadow_on_flush) { 569 + options->qt = IOMMU_DMA_OPTS_SINGLE_QUEUE; 570 + options->fq_timeout = IOVA_SINGLE_FQ_TIMEOUT; 571 + options->fq_size = IOVA_SINGLE_FQ_SIZE; 572 + } else { 573 + options->qt = IOMMU_DMA_OPTS_PER_CPU_QUEUE; 574 + options->fq_size = IOVA_DEFAULT_FQ_SIZE; 575 + options->fq_timeout = IOVA_DEFAULT_FQ_TIMEOUT; 576 + } 577 + } 578 + 579 + /** 638 580 * iommu_dma_init_domain - Initialise a DMA mapping domain 639 581 * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie() 640 582 * @base: IOVA at which the mappable address space starts ··· 715 613 ret = iova_domain_init_rcaches(iovad); 716 614 if (ret) 717 615 goto done_unlock; 616 + 617 + iommu_dma_init_options(&cookie->options, dev); 718 618 719 619 /* If the FQ fails we can simply fall back to strict mode */ 720 620 if (domain->type == IOMMU_DOMAIN_DMA_FQ &&
+40 -43
drivers/iommu/exynos-iommu.c
··· 24 24 25 25 typedef u32 sysmmu_iova_t; 26 26 typedef u32 sysmmu_pte_t; 27 + static struct iommu_domain exynos_identity_domain; 27 28 28 29 /* We do not consider super section mapping (16MB) */ 29 30 #define SECT_ORDER 20 ··· 830 829 struct exynos_iommu_owner *owner = dev_iommu_priv_get(master); 831 830 832 831 mutex_lock(&owner->rpm_lock); 833 - if (data->domain) { 832 + if (&data->domain->domain != &exynos_identity_domain) { 834 833 dev_dbg(data->sysmmu, "saving state\n"); 835 834 __sysmmu_disable(data); 836 835 } ··· 848 847 struct exynos_iommu_owner *owner = dev_iommu_priv_get(master); 849 848 850 849 mutex_lock(&owner->rpm_lock); 851 - if (data->domain) { 850 + if (&data->domain->domain != &exynos_identity_domain) { 852 851 dev_dbg(data->sysmmu, "restoring state\n"); 853 852 __sysmmu_enable(data); 854 853 } ··· 887 886 DMA_TO_DEVICE); 888 887 } 889 888 890 - static struct iommu_domain *exynos_iommu_domain_alloc(unsigned type) 889 + static struct iommu_domain *exynos_iommu_domain_alloc_paging(struct device *dev) 891 890 { 892 891 struct exynos_iommu_domain *domain; 893 892 dma_addr_t handle; ··· 895 894 896 895 /* Check if correct PTE offsets are initialized */ 897 896 BUG_ON(PG_ENT_SHIFT < 0 || !dma_dev); 898 - 899 - if (type != IOMMU_DOMAIN_DMA && type != IOMMU_DOMAIN_UNMANAGED) 900 - return NULL; 901 897 902 898 domain = kzalloc(sizeof(*domain), GFP_KERNEL); 903 899 if (!domain) ··· 978 980 kfree(domain); 979 981 } 980 982 981 - static void exynos_iommu_detach_device(struct iommu_domain *iommu_domain, 982 - struct device *dev) 983 + static int exynos_iommu_identity_attach(struct iommu_domain *identity_domain, 984 + struct device *dev) 983 985 { 984 - struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain); 985 986 struct exynos_iommu_owner *owner = dev_iommu_priv_get(dev); 986 - phys_addr_t pagetable = virt_to_phys(domain->pgtable); 987 + struct exynos_iommu_domain *domain; 988 + phys_addr_t pagetable; 987 989 struct sysmmu_drvdata *data, *next; 988 990 unsigned long flags; 989 991 990 - if (!has_sysmmu(dev) || owner->domain != iommu_domain) 991 - return; 992 + if (owner->domain == identity_domain) 993 + return 0; 994 + 995 + domain = to_exynos_domain(owner->domain); 996 + pagetable = virt_to_phys(domain->pgtable); 992 997 993 998 mutex_lock(&owner->rpm_lock); 994 999 ··· 1010 1009 list_del_init(&data->domain_node); 1011 1010 spin_unlock(&data->lock); 1012 1011 } 1013 - owner->domain = NULL; 1012 + owner->domain = identity_domain; 1014 1013 spin_unlock_irqrestore(&domain->lock, flags); 1015 1014 1016 1015 mutex_unlock(&owner->rpm_lock); 1017 1016 1018 - dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n", __func__, 1019 - &pagetable); 1017 + dev_dbg(dev, "%s: Restored IOMMU to IDENTITY from pgtable %pa\n", 1018 + __func__, &pagetable); 1019 + return 0; 1020 1020 } 1021 + 1022 + static struct iommu_domain_ops exynos_identity_ops = { 1023 + .attach_dev = exynos_iommu_identity_attach, 1024 + }; 1025 + 1026 + static struct iommu_domain exynos_identity_domain = { 1027 + .type = IOMMU_DOMAIN_IDENTITY, 1028 + .ops = &exynos_identity_ops, 1029 + }; 1021 1030 1022 1031 static int exynos_iommu_attach_device(struct iommu_domain *iommu_domain, 1023 1032 struct device *dev) ··· 1037 1026 struct sysmmu_drvdata *data; 1038 1027 phys_addr_t pagetable = virt_to_phys(domain->pgtable); 1039 1028 unsigned long flags; 1029 + int err; 1040 1030 1041 - if (!has_sysmmu(dev)) 1042 - return -ENODEV; 1043 - 1044 - if (owner->domain) 1045 - exynos_iommu_detach_device(owner->domain, dev); 1031 + err = exynos_iommu_identity_attach(&exynos_identity_domain, dev); 1032 + if (err) 1033 + return err; 1046 1034 1047 1035 mutex_lock(&owner->rpm_lock); 1048 1036 ··· 1229 1219 */ 1230 1220 static int exynos_iommu_map(struct iommu_domain *iommu_domain, 1231 1221 unsigned long l_iova, phys_addr_t paddr, size_t size, 1232 - int prot, gfp_t gfp) 1222 + size_t count, int prot, gfp_t gfp, size_t *mapped) 1233 1223 { 1234 1224 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain); 1235 1225 sysmmu_pte_t *entry; ··· 1263 1253 if (ret) 1264 1254 pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n", 1265 1255 __func__, ret, size, iova); 1256 + else 1257 + *mapped = size; 1266 1258 1267 1259 spin_unlock_irqrestore(&domain->pgtablelock, flags); 1268 1260 ··· 1286 1274 } 1287 1275 1288 1276 static size_t exynos_iommu_unmap(struct iommu_domain *iommu_domain, 1289 - unsigned long l_iova, size_t size, 1277 + unsigned long l_iova, size_t size, size_t count, 1290 1278 struct iommu_iotlb_gather *gather) 1291 1279 { 1292 1280 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain); ··· 1419 1407 return &data->iommu; 1420 1408 } 1421 1409 1422 - static void exynos_iommu_set_platform_dma(struct device *dev) 1423 - { 1424 - struct exynos_iommu_owner *owner = dev_iommu_priv_get(dev); 1425 - 1426 - if (owner->domain) { 1427 - struct iommu_group *group = iommu_group_get(dev); 1428 - 1429 - if (group) { 1430 - exynos_iommu_detach_device(owner->domain, dev); 1431 - iommu_group_put(group); 1432 - } 1433 - } 1434 - } 1435 - 1436 1410 static void exynos_iommu_release_device(struct device *dev) 1437 1411 { 1438 1412 struct exynos_iommu_owner *owner = dev_iommu_priv_get(dev); 1439 1413 struct sysmmu_drvdata *data; 1440 1414 1441 - exynos_iommu_set_platform_dma(dev); 1415 + WARN_ON(exynos_iommu_identity_attach(&exynos_identity_domain, dev)); 1442 1416 1443 1417 list_for_each_entry(data, &owner->controllers, owner_node) 1444 1418 device_link_del(data->link); ··· 1455 1457 1456 1458 INIT_LIST_HEAD(&owner->controllers); 1457 1459 mutex_init(&owner->rpm_lock); 1460 + owner->domain = &exynos_identity_domain; 1458 1461 dev_iommu_priv_set(dev, owner); 1459 1462 } 1460 1463 ··· 1470 1471 } 1471 1472 1472 1473 static const struct iommu_ops exynos_iommu_ops = { 1473 - .domain_alloc = exynos_iommu_domain_alloc, 1474 + .identity_domain = &exynos_identity_domain, 1475 + .domain_alloc_paging = exynos_iommu_domain_alloc_paging, 1474 1476 .device_group = generic_device_group, 1475 - #ifdef CONFIG_ARM 1476 - .set_platform_dma_ops = exynos_iommu_set_platform_dma, 1477 - #endif 1478 1477 .probe_device = exynos_iommu_probe_device, 1479 1478 .release_device = exynos_iommu_release_device, 1480 1479 .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE, 1481 1480 .of_xlate = exynos_iommu_of_xlate, 1482 1481 .default_domain_ops = &(const struct iommu_domain_ops) { 1483 1482 .attach_dev = exynos_iommu_attach_device, 1484 - .map = exynos_iommu_map, 1485 - .unmap = exynos_iommu_unmap, 1483 + .map_pages = exynos_iommu_map, 1484 + .unmap_pages = exynos_iommu_unmap, 1486 1485 .iova_to_phys = exynos_iommu_iova_to_phys, 1487 1486 .free = exynos_iommu_domain_free, 1488 1487 }
+38 -3
drivers/iommu/fsl_pamu_domain.c
··· 196 196 { 197 197 struct fsl_dma_domain *dma_domain; 198 198 199 + /* 200 + * FIXME: This isn't creating an unmanaged domain since the 201 + * default_domain_ops do not have any map/unmap function it doesn't meet 202 + * the requirements for __IOMMU_DOMAIN_PAGING. The only purpose seems to 203 + * allow drivers/soc/fsl/qbman/qman_portal.c to do 204 + * fsl_pamu_configure_l1_stash() 205 + */ 199 206 if (type != IOMMU_DOMAIN_UNMANAGED) 200 207 return NULL; 201 208 ··· 290 283 return ret; 291 284 } 292 285 293 - static void fsl_pamu_set_platform_dma(struct device *dev) 286 + /* 287 + * FIXME: fsl/pamu is completely broken in terms of how it works with the iommu 288 + * API. Immediately after probe the HW is left in an IDENTITY translation and 289 + * the driver provides a non-working UNMANAGED domain that it can switch over 290 + * to. However it cannot switch back to an IDENTITY translation, instead it 291 + * switches to what looks like BLOCKING. 292 + */ 293 + static int fsl_pamu_platform_attach(struct iommu_domain *platform_domain, 294 + struct device *dev) 294 295 { 295 296 struct iommu_domain *domain = iommu_get_domain_for_dev(dev); 296 - struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain); 297 + struct fsl_dma_domain *dma_domain; 297 298 const u32 *prop; 298 299 int len; 299 300 struct pci_dev *pdev = NULL; 300 301 struct pci_controller *pci_ctl; 302 + 303 + /* 304 + * Hack to keep things working as they always have, only leaving an 305 + * UNMANAGED domain makes it BLOCKING. 306 + */ 307 + if (domain == platform_domain || !domain || 308 + domain->type != IOMMU_DOMAIN_UNMANAGED) 309 + return 0; 310 + 311 + dma_domain = to_fsl_dma_domain(domain); 301 312 302 313 /* 303 314 * Use LIODN of the PCI controller while detaching a ··· 337 312 detach_device(dev, dma_domain); 338 313 else 339 314 pr_debug("missing fsl,liodn property at %pOF\n", dev->of_node); 315 + return 0; 340 316 } 317 + 318 + static struct iommu_domain_ops fsl_pamu_platform_ops = { 319 + .attach_dev = fsl_pamu_platform_attach, 320 + }; 321 + 322 + static struct iommu_domain fsl_pamu_platform_domain = { 323 + .type = IOMMU_DOMAIN_PLATFORM, 324 + .ops = &fsl_pamu_platform_ops, 325 + }; 341 326 342 327 /* Set the domain stash attribute */ 343 328 int fsl_pamu_configure_l1_stash(struct iommu_domain *domain, u32 cpu) ··· 430 395 } 431 396 432 397 static const struct iommu_ops fsl_pamu_ops = { 398 + .default_domain = &fsl_pamu_platform_domain, 433 399 .capable = fsl_pamu_capable, 434 400 .domain_alloc = fsl_pamu_domain_alloc, 435 401 .probe_device = fsl_pamu_probe_device, 436 402 .device_group = fsl_pamu_device_group, 437 - .set_platform_dma_ops = fsl_pamu_set_platform_dma, 438 403 .default_domain_ops = &(const struct iommu_domain_ops) { 439 404 .attach_dev = fsl_pamu_attach_device, 440 405 .iova_to_phys = fsl_pamu_iova_to_phys,
+174 -41
drivers/iommu/intel/debugfs.c
··· 111 111 IOMMU_REGSET_ENTRY(VCRSP), 112 112 }; 113 113 114 + static struct dentry *intel_iommu_debug; 115 + 114 116 static int iommu_regset_show(struct seq_file *m, void *unused) 115 117 { 116 118 struct dmar_drhd_unit *drhd; ··· 313 311 static inline void 314 312 dump_page_info(struct seq_file *m, unsigned long iova, u64 *path) 315 313 { 316 - seq_printf(m, "0x%013lx |\t0x%016llx\t0x%016llx\t0x%016llx\t0x%016llx\t0x%016llx\n", 317 - iova >> VTD_PAGE_SHIFT, path[5], path[4], 318 - path[3], path[2], path[1]); 314 + seq_printf(m, "0x%013lx |\t0x%016llx\t0x%016llx\t0x%016llx", 315 + iova >> VTD_PAGE_SHIFT, path[5], path[4], path[3]); 316 + if (path[2]) { 317 + seq_printf(m, "\t0x%016llx", path[2]); 318 + if (path[1]) 319 + seq_printf(m, "\t0x%016llx", path[1]); 320 + } 321 + seq_putc(m, '\n'); 319 322 } 320 323 321 324 static void pgtable_walk_level(struct seq_file *m, struct dma_pte *pde, ··· 347 340 } 348 341 } 349 342 350 - static int __show_device_domain_translation(struct device *dev, void *data) 343 + static int domain_translation_struct_show(struct seq_file *m, 344 + struct device_domain_info *info, 345 + ioasid_t pasid) 351 346 { 352 - struct dmar_domain *domain; 353 - struct seq_file *m = data; 354 - u64 path[6] = { 0 }; 347 + bool scalable, found = false; 348 + struct dmar_drhd_unit *drhd; 349 + struct intel_iommu *iommu; 350 + u16 devfn, bus, seg; 355 351 356 - domain = to_dmar_domain(iommu_get_domain_for_dev(dev)); 357 - if (!domain) 358 - return 0; 352 + bus = info->bus; 353 + devfn = info->devfn; 354 + seg = info->segment; 359 355 360 - seq_printf(m, "Device %s @0x%llx\n", dev_name(dev), 361 - (u64)virt_to_phys(domain->pgd)); 362 - seq_puts(m, "IOVA_PFN\t\tPML5E\t\t\tPML4E\t\t\tPDPE\t\t\tPDE\t\t\tPTE\n"); 356 + rcu_read_lock(); 357 + for_each_active_iommu(iommu, drhd) { 358 + struct context_entry *context; 359 + u64 pgd, path[6] = { 0 }; 360 + u32 sts, agaw; 363 361 364 - pgtable_walk_level(m, domain->pgd, domain->agaw + 2, 0, path); 365 - seq_putc(m, '\n'); 362 + if (seg != iommu->segment) 363 + continue; 366 364 367 - /* Don't iterate */ 368 - return 1; 369 - } 365 + sts = dmar_readl(iommu->reg + DMAR_GSTS_REG); 366 + if (!(sts & DMA_GSTS_TES)) { 367 + seq_printf(m, "DMA Remapping is not enabled on %s\n", 368 + iommu->name); 369 + continue; 370 + } 371 + if (dmar_readq(iommu->reg + DMAR_RTADDR_REG) & DMA_RTADDR_SMT) 372 + scalable = true; 373 + else 374 + scalable = false; 370 375 371 - static int show_device_domain_translation(struct device *dev, void *data) 372 - { 373 - struct iommu_group *group; 374 - 375 - group = iommu_group_get(dev); 376 - if (group) { 377 376 /* 378 - * The group->mutex is held across the callback, which will 379 - * block calls to iommu_attach/detach_group/device. Hence, 377 + * The iommu->lock is held across the callback, which will 378 + * block calls to domain_attach/domain_detach. Hence, 380 379 * the domain of the device will not change during traversal. 381 380 * 382 - * All devices in an iommu group share a single domain, hence 383 - * we only dump the domain of the first device. Even though, 384 - * this code still possibly races with the iommu_unmap() 381 + * Traversing page table possibly races with the iommu_unmap() 385 382 * interface. This could be solved by RCU-freeing the page 386 383 * table pages in the iommu_unmap() path. 387 384 */ 388 - iommu_group_for_each_dev(group, data, 389 - __show_device_domain_translation); 390 - iommu_group_put(group); 385 + spin_lock(&iommu->lock); 386 + 387 + context = iommu_context_addr(iommu, bus, devfn, 0); 388 + if (!context || !context_present(context)) 389 + goto iommu_unlock; 390 + 391 + if (scalable) { /* scalable mode */ 392 + struct pasid_entry *pasid_tbl, *pasid_tbl_entry; 393 + struct pasid_dir_entry *dir_tbl, *dir_entry; 394 + u16 dir_idx, tbl_idx, pgtt; 395 + u64 pasid_dir_ptr; 396 + 397 + pasid_dir_ptr = context->lo & VTD_PAGE_MASK; 398 + 399 + /* Dump specified device domain mappings with PASID. */ 400 + dir_idx = pasid >> PASID_PDE_SHIFT; 401 + tbl_idx = pasid & PASID_PTE_MASK; 402 + 403 + dir_tbl = phys_to_virt(pasid_dir_ptr); 404 + dir_entry = &dir_tbl[dir_idx]; 405 + 406 + pasid_tbl = get_pasid_table_from_pde(dir_entry); 407 + if (!pasid_tbl) 408 + goto iommu_unlock; 409 + 410 + pasid_tbl_entry = &pasid_tbl[tbl_idx]; 411 + if (!pasid_pte_is_present(pasid_tbl_entry)) 412 + goto iommu_unlock; 413 + 414 + /* 415 + * According to PASID Granular Translation Type(PGTT), 416 + * get the page table pointer. 417 + */ 418 + pgtt = (u16)(pasid_tbl_entry->val[0] & GENMASK_ULL(8, 6)) >> 6; 419 + agaw = (u8)(pasid_tbl_entry->val[0] & GENMASK_ULL(4, 2)) >> 2; 420 + 421 + switch (pgtt) { 422 + case PASID_ENTRY_PGTT_FL_ONLY: 423 + pgd = pasid_tbl_entry->val[2]; 424 + break; 425 + case PASID_ENTRY_PGTT_SL_ONLY: 426 + case PASID_ENTRY_PGTT_NESTED: 427 + pgd = pasid_tbl_entry->val[0]; 428 + break; 429 + default: 430 + goto iommu_unlock; 431 + } 432 + pgd &= VTD_PAGE_MASK; 433 + } else { /* legacy mode */ 434 + pgd = context->lo & VTD_PAGE_MASK; 435 + agaw = context->hi & 7; 436 + } 437 + 438 + seq_printf(m, "Device %04x:%02x:%02x.%x ", 439 + iommu->segment, bus, PCI_SLOT(devfn), PCI_FUNC(devfn)); 440 + 441 + if (scalable) 442 + seq_printf(m, "with pasid %x @0x%llx\n", pasid, pgd); 443 + else 444 + seq_printf(m, "@0x%llx\n", pgd); 445 + 446 + seq_printf(m, "%-17s\t%-18s\t%-18s\t%-18s\t%-18s\t%-s\n", 447 + "IOVA_PFN", "PML5E", "PML4E", "PDPE", "PDE", "PTE"); 448 + pgtable_walk_level(m, phys_to_virt(pgd), agaw + 2, 0, path); 449 + 450 + found = true; 451 + iommu_unlock: 452 + spin_unlock(&iommu->lock); 453 + if (found) 454 + break; 391 455 } 456 + rcu_read_unlock(); 392 457 393 458 return 0; 394 459 } 395 460 396 - static int domain_translation_struct_show(struct seq_file *m, void *unused) 461 + static int dev_domain_translation_struct_show(struct seq_file *m, void *unused) 397 462 { 398 - return bus_for_each_dev(&pci_bus_type, NULL, m, 399 - show_device_domain_translation); 463 + struct device_domain_info *info = (struct device_domain_info *)m->private; 464 + 465 + return domain_translation_struct_show(m, info, IOMMU_NO_PASID); 400 466 } 401 - DEFINE_SHOW_ATTRIBUTE(domain_translation_struct); 467 + DEFINE_SHOW_ATTRIBUTE(dev_domain_translation_struct); 468 + 469 + static int pasid_domain_translation_struct_show(struct seq_file *m, void *unused) 470 + { 471 + struct dev_pasid_info *dev_pasid = (struct dev_pasid_info *)m->private; 472 + struct device_domain_info *info = dev_iommu_priv_get(dev_pasid->dev); 473 + 474 + return domain_translation_struct_show(m, info, dev_pasid->pasid); 475 + } 476 + DEFINE_SHOW_ATTRIBUTE(pasid_domain_translation_struct); 402 477 403 478 static void invalidation_queue_entry_show(struct seq_file *m, 404 479 struct intel_iommu *iommu) ··· 755 666 756 667 void __init intel_iommu_debugfs_init(void) 757 668 { 758 - struct dentry *intel_iommu_debug = debugfs_create_dir("intel", 759 - iommu_debugfs_dir); 669 + intel_iommu_debug = debugfs_create_dir("intel", iommu_debugfs_dir); 760 670 761 671 debugfs_create_file("iommu_regset", 0444, intel_iommu_debug, NULL, 762 672 &iommu_regset_fops); 763 673 debugfs_create_file("dmar_translation_struct", 0444, intel_iommu_debug, 764 674 NULL, &dmar_translation_struct_fops); 765 - debugfs_create_file("domain_translation_struct", 0444, 766 - intel_iommu_debug, NULL, 767 - &domain_translation_struct_fops); 768 675 debugfs_create_file("invalidation_queue", 0444, intel_iommu_debug, 769 676 NULL, &invalidation_queue_fops); 770 677 #ifdef CONFIG_IRQ_REMAP ··· 769 684 #endif 770 685 debugfs_create_file("dmar_perf_latency", 0644, intel_iommu_debug, 771 686 NULL, &dmar_perf_latency_fops); 687 + } 688 + 689 + /* 690 + * Create a debugfs directory for each device, and then create a 691 + * debugfs file in this directory for users to dump the page table 692 + * of the default domain. e.g. 693 + * /sys/kernel/debug/iommu/intel/0000:00:01.0/domain_translation_struct 694 + */ 695 + void intel_iommu_debugfs_create_dev(struct device_domain_info *info) 696 + { 697 + info->debugfs_dentry = debugfs_create_dir(dev_name(info->dev), intel_iommu_debug); 698 + 699 + debugfs_create_file("domain_translation_struct", 0444, info->debugfs_dentry, 700 + info, &dev_domain_translation_struct_fops); 701 + } 702 + 703 + /* Remove the device debugfs directory. */ 704 + void intel_iommu_debugfs_remove_dev(struct device_domain_info *info) 705 + { 706 + debugfs_remove_recursive(info->debugfs_dentry); 707 + } 708 + 709 + /* 710 + * Create a debugfs directory per pair of {device, pasid}, then create the 711 + * corresponding debugfs file in this directory for users to dump its page 712 + * table. e.g. 713 + * /sys/kernel/debug/iommu/intel/0000:00:01.0/1/domain_translation_struct 714 + * 715 + * The debugfs only dumps the page tables whose mappings are created and 716 + * destroyed by the iommu_map/unmap() interfaces. Check the mapping type 717 + * of the domain before creating debugfs directory. 718 + */ 719 + void intel_iommu_debugfs_create_dev_pasid(struct dev_pasid_info *dev_pasid) 720 + { 721 + struct device_domain_info *info = dev_iommu_priv_get(dev_pasid->dev); 722 + char dir_name[10]; 723 + 724 + sprintf(dir_name, "%x", dev_pasid->pasid); 725 + dev_pasid->debugfs_dentry = debugfs_create_dir(dir_name, info->debugfs_dentry); 726 + 727 + debugfs_create_file("domain_translation_struct", 0444, dev_pasid->debugfs_dentry, 728 + dev_pasid, &pasid_domain_translation_struct_fops); 729 + } 730 + 731 + /* Remove the device pasid debugfs directory. */ 732 + void intel_iommu_debugfs_remove_dev_pasid(struct dev_pasid_info *dev_pasid) 733 + { 734 + debugfs_remove_recursive(dev_pasid->debugfs_dentry); 772 735 }
+13 -6
drivers/iommu/intel/iommu.c
··· 4016 4016 } 4017 4017 4018 4018 static struct iommu_domain blocking_domain = { 4019 + .type = IOMMU_DOMAIN_BLOCKED, 4019 4020 .ops = &(const struct iommu_domain_ops) { 4020 4021 .attach_dev = blocking_domain_attach_dev, 4021 - .free = intel_iommu_domain_free 4022 4022 } 4023 4023 }; 4024 4024 ··· 4028 4028 struct iommu_domain *domain; 4029 4029 4030 4030 switch (type) { 4031 - case IOMMU_DOMAIN_BLOCKED: 4032 - return &blocking_domain; 4033 4031 case IOMMU_DOMAIN_DMA: 4034 4032 case IOMMU_DOMAIN_UNMANAGED: 4035 4033 dmar_domain = alloc_domain(type); ··· 4109 4111 4110 4112 static void intel_iommu_domain_free(struct iommu_domain *domain) 4111 4113 { 4112 - if (domain != &si_domain->domain && domain != &blocking_domain) 4114 + if (domain != &si_domain->domain) 4113 4115 domain_exit(to_dmar_domain(domain)); 4114 4116 } 4115 4117 ··· 4463 4465 } 4464 4466 } 4465 4467 4468 + intel_iommu_debugfs_create_dev(info); 4469 + 4466 4470 return &iommu->iommu; 4467 4471 } 4468 4472 ··· 4474 4474 4475 4475 dmar_remove_one_dev_info(dev); 4476 4476 intel_pasid_free_table(dev); 4477 + intel_iommu_debugfs_remove_dev(info); 4477 4478 dev_iommu_priv_set(dev, NULL); 4478 4479 kfree(info); 4479 4480 set_dma_ops(dev, NULL); ··· 4719 4718 return false; 4720 4719 } 4721 4720 4722 - static void intel_iommu_iotlb_sync_map(struct iommu_domain *domain, 4723 - unsigned long iova, size_t size) 4721 + static int intel_iommu_iotlb_sync_map(struct iommu_domain *domain, 4722 + unsigned long iova, size_t size) 4724 4723 { 4725 4724 struct dmar_domain *dmar_domain = to_dmar_domain(domain); 4726 4725 unsigned long pages = aligned_nrpages(iova, size); ··· 4730 4729 4731 4730 xa_for_each(&dmar_domain->iommu_array, i, info) 4732 4731 __mapping_notify_one(info->iommu, dmar_domain, pfn, pages); 4732 + return 0; 4733 4733 } 4734 4734 4735 4735 static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid) ··· 4768 4766 spin_unlock_irqrestore(&dmar_domain->lock, flags); 4769 4767 4770 4768 domain_detach_iommu(dmar_domain, iommu); 4769 + intel_iommu_debugfs_remove_dev_pasid(dev_pasid); 4771 4770 kfree(dev_pasid); 4772 4771 out_tear_down: 4773 4772 intel_pasid_tear_down_entry(iommu, dev, pasid, false); ··· 4823 4820 spin_lock_irqsave(&dmar_domain->lock, flags); 4824 4821 list_add(&dev_pasid->link_domain, &dmar_domain->dev_pasids); 4825 4822 spin_unlock_irqrestore(&dmar_domain->lock, flags); 4823 + 4824 + if (domain->type & __IOMMU_DOMAIN_PAGING) 4825 + intel_iommu_debugfs_create_dev_pasid(dev_pasid); 4826 4826 4827 4827 return 0; 4828 4828 out_detach_iommu: ··· 4931 4925 }; 4932 4926 4933 4927 const struct iommu_ops intel_iommu_ops = { 4928 + .blocked_domain = &blocking_domain, 4934 4929 .capable = intel_iommu_capable, 4935 4930 .hw_info = intel_iommu_hw_info, 4936 4931 .domain_alloc = intel_iommu_domain_alloc,
+14
drivers/iommu/intel/iommu.h
··· 749 749 struct intel_iommu *iommu; /* IOMMU used by this device */ 750 750 struct dmar_domain *domain; /* pointer to domain */ 751 751 struct pasid_table *pasid_table; /* pasid table */ 752 + #ifdef CONFIG_INTEL_IOMMU_DEBUGFS 753 + struct dentry *debugfs_dentry; /* pointer to device directory dentry */ 754 + #endif 752 755 }; 753 756 754 757 struct dev_pasid_info { 755 758 struct list_head link_domain; /* link to domain siblings */ 756 759 struct device *dev; 757 760 ioasid_t pasid; 761 + #ifdef CONFIG_INTEL_IOMMU_DEBUGFS 762 + struct dentry *debugfs_dentry; /* pointer to pasid directory dentry */ 763 + #endif 758 764 }; 759 765 760 766 static inline void __iommu_flush_cache( ··· 941 935 942 936 #ifdef CONFIG_INTEL_IOMMU_DEBUGFS 943 937 void intel_iommu_debugfs_init(void); 938 + void intel_iommu_debugfs_create_dev(struct device_domain_info *info); 939 + void intel_iommu_debugfs_remove_dev(struct device_domain_info *info); 940 + void intel_iommu_debugfs_create_dev_pasid(struct dev_pasid_info *dev_pasid); 941 + void intel_iommu_debugfs_remove_dev_pasid(struct dev_pasid_info *dev_pasid); 944 942 #else 945 943 static inline void intel_iommu_debugfs_init(void) {} 944 + static inline void intel_iommu_debugfs_create_dev(struct device_domain_info *info) {} 945 + static inline void intel_iommu_debugfs_remove_dev(struct device_domain_info *info) {} 946 + static inline void intel_iommu_debugfs_create_dev_pasid(struct dev_pasid_info *dev_pasid) {} 947 + static inline void intel_iommu_debugfs_remove_dev_pasid(struct dev_pasid_info *dev_pasid) {} 946 948 #endif /* CONFIG_INTEL_IOMMU_DEBUGFS */ 947 949 948 950 extern const struct attribute_group *intel_iommu_groups[];
+254 -211
drivers/iommu/iommu.c
··· 37 37 #include "iommu-priv.h" 38 38 39 39 #include "iommu-sva.h" 40 - #include "iommu-priv.h" 41 40 42 41 static struct kset *iommu_group_kset; 43 42 static DEFINE_IDA(iommu_group_ida); ··· 95 96 static int iommu_bus_notifier(struct notifier_block *nb, 96 97 unsigned long action, void *data); 97 98 static void iommu_release_device(struct device *dev); 98 - static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus, 99 - unsigned type); 99 + static struct iommu_domain * 100 + __iommu_group_domain_alloc(struct iommu_group *group, unsigned int type); 100 101 static int __iommu_attach_device(struct iommu_domain *domain, 101 102 struct device *dev); 102 103 static int __iommu_attach_group(struct iommu_domain *domain, ··· 183 184 case IOMMU_DOMAIN_DMA: 184 185 case IOMMU_DOMAIN_DMA_FQ: 185 186 return "Translated"; 187 + case IOMMU_DOMAIN_PLATFORM: 188 + return "Platform"; 186 189 default: 187 190 return "Unknown"; 188 191 } ··· 291 290 spin_lock(&iommu_device_lock); 292 291 list_del(&iommu->list); 293 292 spin_unlock(&iommu_device_lock); 293 + 294 + /* Pairs with the alloc in generic_single_device_group() */ 295 + iommu_group_put(iommu->singleton_group); 296 + iommu->singleton_group = NULL; 294 297 } 295 298 EXPORT_SYMBOL_GPL(iommu_device_unregister); 296 299 ··· 409 404 ret = PTR_ERR(iommu_dev); 410 405 goto err_module_put; 411 406 } 407 + dev->iommu->iommu_dev = iommu_dev; 412 408 413 409 ret = iommu_device_link(iommu_dev, dev); 414 410 if (ret) ··· 424 418 } 425 419 dev->iommu_group = group; 426 420 427 - dev->iommu->iommu_dev = iommu_dev; 428 421 dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev); 429 422 if (ops->is_attach_deferred) 430 423 dev->iommu->attach_deferred = ops->is_attach_deferred(dev); ··· 437 432 err_module_put: 438 433 module_put(ops->owner); 439 434 err_free: 435 + dev->iommu->iommu_dev = NULL; 440 436 dev_iommu_free(dev); 441 437 return ret; 442 438 } ··· 1643 1637 EXPORT_SYMBOL_GPL(generic_device_group); 1644 1638 1645 1639 /* 1640 + * Generic device_group call-back function. It just allocates one 1641 + * iommu-group per iommu driver instance shared by every device 1642 + * probed by that iommu driver. 1643 + */ 1644 + struct iommu_group *generic_single_device_group(struct device *dev) 1645 + { 1646 + struct iommu_device *iommu = dev->iommu->iommu_dev; 1647 + 1648 + if (!iommu->singleton_group) { 1649 + struct iommu_group *group; 1650 + 1651 + group = iommu_group_alloc(); 1652 + if (IS_ERR(group)) 1653 + return group; 1654 + iommu->singleton_group = group; 1655 + } 1656 + return iommu_group_ref_get(iommu->singleton_group); 1657 + } 1658 + EXPORT_SYMBOL_GPL(generic_single_device_group); 1659 + 1660 + /* 1646 1661 * Use standard PCI bus topology, isolation features, and DMA alias quirks 1647 1662 * to find or create an IOMMU group for a device. 1648 1663 */ ··· 1744 1717 } 1745 1718 EXPORT_SYMBOL_GPL(fsl_mc_device_group); 1746 1719 1747 - static int iommu_get_def_domain_type(struct device *dev) 1748 - { 1749 - const struct iommu_ops *ops = dev_iommu_ops(dev); 1750 - 1751 - if (dev_is_pci(dev) && to_pci_dev(dev)->untrusted) 1752 - return IOMMU_DOMAIN_DMA; 1753 - 1754 - if (ops->def_domain_type) 1755 - return ops->def_domain_type(dev); 1756 - 1757 - return 0; 1758 - } 1759 - 1760 1720 static struct iommu_domain * 1761 - __iommu_group_alloc_default_domain(const struct bus_type *bus, 1762 - struct iommu_group *group, int req_type) 1721 + __iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) 1763 1722 { 1764 1723 if (group->default_domain && group->default_domain->type == req_type) 1765 1724 return group->default_domain; 1766 - return __iommu_domain_alloc(bus, req_type); 1725 + return __iommu_group_domain_alloc(group, req_type); 1726 + } 1727 + 1728 + /* 1729 + * Returns the iommu_ops for the devices in an iommu group. 1730 + * 1731 + * It is assumed that all devices in an iommu group are managed by a single 1732 + * IOMMU unit. Therefore, this returns the dev_iommu_ops of the first device 1733 + * in the group. 1734 + */ 1735 + static const struct iommu_ops *group_iommu_ops(struct iommu_group *group) 1736 + { 1737 + struct group_device *device = 1738 + list_first_entry(&group->devices, struct group_device, list); 1739 + 1740 + lockdep_assert_held(&group->mutex); 1741 + 1742 + return dev_iommu_ops(device->dev); 1767 1743 } 1768 1744 1769 1745 /* ··· 1776 1746 static struct iommu_domain * 1777 1747 iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) 1778 1748 { 1779 - const struct bus_type *bus = 1780 - list_first_entry(&group->devices, struct group_device, list) 1781 - ->dev->bus; 1749 + const struct iommu_ops *ops = group_iommu_ops(group); 1782 1750 struct iommu_domain *dom; 1783 1751 1784 1752 lockdep_assert_held(&group->mutex); 1785 1753 1754 + /* 1755 + * Allow legacy drivers to specify the domain that will be the default 1756 + * domain. This should always be either an IDENTITY/BLOCKED/PLATFORM 1757 + * domain. Do not use in new drivers. 1758 + */ 1759 + if (ops->default_domain) { 1760 + if (req_type) 1761 + return NULL; 1762 + return ops->default_domain; 1763 + } 1764 + 1786 1765 if (req_type) 1787 - return __iommu_group_alloc_default_domain(bus, group, req_type); 1766 + return __iommu_group_alloc_default_domain(group, req_type); 1788 1767 1789 1768 /* The driver gave no guidance on what type to use, try the default */ 1790 - dom = __iommu_group_alloc_default_domain(bus, group, iommu_def_domain_type); 1769 + dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type); 1791 1770 if (dom) 1792 1771 return dom; 1793 1772 1794 1773 /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */ 1795 1774 if (iommu_def_domain_type == IOMMU_DOMAIN_DMA) 1796 1775 return NULL; 1797 - dom = __iommu_group_alloc_default_domain(bus, group, IOMMU_DOMAIN_DMA); 1776 + dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA); 1798 1777 if (!dom) 1799 1778 return NULL; 1800 1779 ··· 1847 1808 return 0; 1848 1809 } 1849 1810 1850 - /* A target_type of 0 will select the best domain type and cannot fail */ 1811 + /* 1812 + * Combine the driver's chosen def_domain_type across all the devices in a 1813 + * group. Drivers must give a consistent result. 1814 + */ 1815 + static int iommu_get_def_domain_type(struct iommu_group *group, 1816 + struct device *dev, int cur_type) 1817 + { 1818 + const struct iommu_ops *ops = group_iommu_ops(group); 1819 + int type; 1820 + 1821 + if (!ops->def_domain_type) 1822 + return cur_type; 1823 + 1824 + type = ops->def_domain_type(dev); 1825 + if (!type || cur_type == type) 1826 + return cur_type; 1827 + if (!cur_type) 1828 + return type; 1829 + 1830 + dev_err_ratelimited( 1831 + dev, 1832 + "IOMMU driver error, requesting conflicting def_domain_type, %s and %s, for devices in group %u.\n", 1833 + iommu_domain_type_str(cur_type), iommu_domain_type_str(type), 1834 + group->id); 1835 + 1836 + /* 1837 + * Try to recover, drivers are allowed to force IDENITY or DMA, IDENTITY 1838 + * takes precedence. 1839 + */ 1840 + if (type == IOMMU_DOMAIN_IDENTITY) 1841 + return type; 1842 + return cur_type; 1843 + } 1844 + 1845 + /* 1846 + * A target_type of 0 will select the best domain type. 0 can be returned in 1847 + * this case meaning the global default should be used. 1848 + */ 1851 1849 static int iommu_get_default_domain_type(struct iommu_group *group, 1852 1850 int target_type) 1853 1851 { 1854 - int best_type = target_type; 1852 + struct device *untrusted = NULL; 1855 1853 struct group_device *gdev; 1856 - struct device *last_dev; 1854 + int driver_type = 0; 1857 1855 1858 1856 lockdep_assert_held(&group->mutex); 1859 1857 1860 - for_each_group_device(group, gdev) { 1861 - unsigned int type = iommu_get_def_domain_type(gdev->dev); 1862 - 1863 - if (best_type && type && best_type != type) { 1864 - if (target_type) { 1865 - dev_err_ratelimited( 1866 - gdev->dev, 1867 - "Device cannot be in %s domain\n", 1868 - iommu_domain_type_str(target_type)); 1869 - return -1; 1870 - } 1871 - 1872 - dev_warn( 1873 - gdev->dev, 1874 - "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n", 1875 - iommu_domain_type_str(type), dev_name(last_dev), 1876 - iommu_domain_type_str(best_type)); 1877 - return 0; 1878 - } 1879 - if (!best_type) 1880 - best_type = type; 1881 - last_dev = gdev->dev; 1858 + /* 1859 + * ARM32 drivers supporting CONFIG_ARM_DMA_USE_IOMMU can declare an 1860 + * identity_domain and it will automatically become their default 1861 + * domain. Later on ARM_DMA_USE_IOMMU will install its UNMANAGED domain. 1862 + * Override the selection to IDENTITY. 1863 + */ 1864 + if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) { 1865 + static_assert(!(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU) && 1866 + IS_ENABLED(CONFIG_IOMMU_DMA))); 1867 + driver_type = IOMMU_DOMAIN_IDENTITY; 1882 1868 } 1883 - return best_type; 1869 + 1870 + for_each_group_device(group, gdev) { 1871 + driver_type = iommu_get_def_domain_type(group, gdev->dev, 1872 + driver_type); 1873 + 1874 + if (dev_is_pci(gdev->dev) && to_pci_dev(gdev->dev)->untrusted) { 1875 + /* 1876 + * No ARM32 using systems will set untrusted, it cannot 1877 + * work. 1878 + */ 1879 + if (WARN_ON(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))) 1880 + return -1; 1881 + untrusted = gdev->dev; 1882 + } 1883 + } 1884 + 1885 + /* 1886 + * If the common dma ops are not selected in kconfig then we cannot use 1887 + * IOMMU_DOMAIN_DMA at all. Force IDENTITY if nothing else has been 1888 + * selected. 1889 + */ 1890 + if (!IS_ENABLED(CONFIG_IOMMU_DMA)) { 1891 + if (WARN_ON(driver_type == IOMMU_DOMAIN_DMA)) 1892 + return -1; 1893 + if (!driver_type) 1894 + driver_type = IOMMU_DOMAIN_IDENTITY; 1895 + } 1896 + 1897 + if (untrusted) { 1898 + if (driver_type && driver_type != IOMMU_DOMAIN_DMA) { 1899 + dev_err_ratelimited( 1900 + untrusted, 1901 + "Device is not trusted, but driver is overriding group %u to %s, refusing to probe.\n", 1902 + group->id, iommu_domain_type_str(driver_type)); 1903 + return -1; 1904 + } 1905 + driver_type = IOMMU_DOMAIN_DMA; 1906 + } 1907 + 1908 + if (target_type) { 1909 + if (driver_type && target_type != driver_type) 1910 + return -1; 1911 + return target_type; 1912 + } 1913 + return driver_type; 1884 1914 } 1885 1915 1886 1916 static void iommu_group_do_probe_finalize(struct device *dev) ··· 2078 1970 } 2079 1971 EXPORT_SYMBOL_GPL(iommu_set_fault_handler); 2080 1972 2081 - static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus, 2082 - unsigned type) 1973 + static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops, 1974 + struct device *dev, 1975 + unsigned int type) 2083 1976 { 2084 1977 struct iommu_domain *domain; 2085 1978 unsigned int alloc_type = type & IOMMU_DOMAIN_ALLOC_FLAGS; 2086 1979 2087 - if (bus == NULL || bus->iommu_ops == NULL) 1980 + if (alloc_type == IOMMU_DOMAIN_IDENTITY && ops->identity_domain) 1981 + return ops->identity_domain; 1982 + else if (alloc_type == IOMMU_DOMAIN_BLOCKED && ops->blocked_domain) 1983 + return ops->blocked_domain; 1984 + else if (type & __IOMMU_DOMAIN_PAGING && ops->domain_alloc_paging) 1985 + domain = ops->domain_alloc_paging(dev); 1986 + else if (ops->domain_alloc) 1987 + domain = ops->domain_alloc(alloc_type); 1988 + else 2088 1989 return NULL; 2089 1990 2090 - domain = bus->iommu_ops->domain_alloc(alloc_type); 2091 1991 if (!domain) 2092 1992 return NULL; 2093 1993 ··· 2105 1989 * may override this later 2106 1990 */ 2107 1991 if (!domain->pgsize_bitmap) 2108 - domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap; 1992 + domain->pgsize_bitmap = ops->pgsize_bitmap; 2109 1993 2110 1994 if (!domain->ops) 2111 - domain->ops = bus->iommu_ops->default_domain_ops; 1995 + domain->ops = ops->default_domain_ops; 2112 1996 2113 1997 if (iommu_is_dma_domain(domain) && iommu_get_dma_cookie(domain)) { 2114 1998 iommu_domain_free(domain); ··· 2117 2001 return domain; 2118 2002 } 2119 2003 2004 + static struct iommu_domain * 2005 + __iommu_group_domain_alloc(struct iommu_group *group, unsigned int type) 2006 + { 2007 + struct device *dev = 2008 + list_first_entry(&group->devices, struct group_device, list) 2009 + ->dev; 2010 + 2011 + return __iommu_domain_alloc(group_iommu_ops(group), dev, type); 2012 + } 2013 + 2120 2014 struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus) 2121 2015 { 2122 - return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED); 2016 + if (bus == NULL || bus->iommu_ops == NULL) 2017 + return NULL; 2018 + return __iommu_domain_alloc(bus->iommu_ops, NULL, 2019 + IOMMU_DOMAIN_UNMANAGED); 2123 2020 } 2124 2021 EXPORT_SYMBOL_GPL(iommu_domain_alloc); 2125 2022 ··· 2141 2012 if (domain->type == IOMMU_DOMAIN_SVA) 2142 2013 mmdrop(domain->mm); 2143 2014 iommu_put_dma_cookie(domain); 2144 - domain->ops->free(domain); 2015 + if (domain->ops->free) 2016 + domain->ops->free(domain); 2145 2017 } 2146 2018 EXPORT_SYMBOL_GPL(iommu_domain_free); 2147 2019 ··· 2192 2062 */ 2193 2063 int iommu_attach_device(struct iommu_domain *domain, struct device *dev) 2194 2064 { 2195 - struct iommu_group *group; 2065 + /* Caller must be a probed driver on dev */ 2066 + struct iommu_group *group = dev->iommu_group; 2196 2067 int ret; 2197 2068 2198 - group = iommu_group_get(dev); 2199 2069 if (!group) 2200 2070 return -ENODEV; 2201 2071 ··· 2212 2082 2213 2083 out_unlock: 2214 2084 mutex_unlock(&group->mutex); 2215 - iommu_group_put(group); 2216 - 2217 2085 return ret; 2218 2086 } 2219 2087 EXPORT_SYMBOL_GPL(iommu_attach_device); ··· 2226 2098 2227 2099 void iommu_detach_device(struct iommu_domain *domain, struct device *dev) 2228 2100 { 2229 - struct iommu_group *group; 2101 + /* Caller must be a probed driver on dev */ 2102 + struct iommu_group *group = dev->iommu_group; 2230 2103 2231 - group = iommu_group_get(dev); 2232 2104 if (!group) 2233 2105 return; 2234 2106 ··· 2240 2112 2241 2113 out_unlock: 2242 2114 mutex_unlock(&group->mutex); 2243 - iommu_group_put(group); 2244 2115 } 2245 2116 EXPORT_SYMBOL_GPL(iommu_detach_device); 2246 2117 2247 2118 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev) 2248 2119 { 2249 - struct iommu_domain *domain; 2250 - struct iommu_group *group; 2120 + /* Caller must be a probed driver on dev */ 2121 + struct iommu_group *group = dev->iommu_group; 2251 2122 2252 - group = iommu_group_get(dev); 2253 2123 if (!group) 2254 2124 return NULL; 2255 2125 2256 - domain = group->domain; 2257 - 2258 - iommu_group_put(group); 2259 - 2260 - return domain; 2126 + return group->domain; 2261 2127 } 2262 2128 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev); 2263 2129 ··· 2397 2275 if (group->domain == new_domain) 2398 2276 return 0; 2399 2277 2400 - /* 2401 - * New drivers should support default domains, so set_platform_dma() 2402 - * op will never be called. Otherwise the NULL domain represents some 2403 - * platform specific behavior. 2404 - */ 2405 - if (!new_domain) { 2406 - for_each_group_device(group, gdev) { 2407 - const struct iommu_ops *ops = dev_iommu_ops(gdev->dev); 2408 - 2409 - if (!WARN_ON(!ops->set_platform_dma_ops)) 2410 - ops->set_platform_dma_ops(gdev->dev); 2411 - } 2412 - group->domain = NULL; 2413 - return 0; 2414 - } 2278 + if (WARN_ON(!new_domain)) 2279 + return -EINVAL; 2415 2280 2416 2281 /* 2417 2282 * Changing the domain is done by calling attach_dev() on the new ··· 2434 2325 */ 2435 2326 last_gdev = gdev; 2436 2327 for_each_group_device(group, gdev) { 2437 - const struct iommu_ops *ops = dev_iommu_ops(gdev->dev); 2438 - 2439 2328 /* 2440 - * If set_platform_dma_ops is not present a NULL domain can 2441 - * happen only for first probe, in which case we leave 2442 - * group->domain as NULL and let release clean everything up. 2329 + * A NULL domain can happen only for first probe, in which case 2330 + * we leave group->domain as NULL and let release clean 2331 + * everything up. 2443 2332 */ 2444 2333 if (group->domain) 2445 2334 WARN_ON(__iommu_device_set_domain( 2446 2335 group, gdev->dev, group->domain, 2447 2336 IOMMU_SET_DOMAIN_MUST_SUCCEED)); 2448 - else if (ops->set_platform_dma_ops) 2449 - ops->set_platform_dma_ops(gdev->dev); 2450 2337 if (gdev == last_gdev) 2451 2338 break; 2452 2339 } ··· 2523 2418 return pgsize; 2524 2419 } 2525 2420 2526 - static int __iommu_map_pages(struct iommu_domain *domain, unsigned long iova, 2527 - phys_addr_t paddr, size_t size, int prot, 2528 - gfp_t gfp, size_t *mapped) 2529 - { 2530 - const struct iommu_domain_ops *ops = domain->ops; 2531 - size_t pgsize, count; 2532 - int ret; 2533 - 2534 - pgsize = iommu_pgsize(domain, iova, paddr, size, &count); 2535 - 2536 - pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx count %zu\n", 2537 - iova, &paddr, pgsize, count); 2538 - 2539 - if (ops->map_pages) { 2540 - ret = ops->map_pages(domain, iova, paddr, pgsize, count, prot, 2541 - gfp, mapped); 2542 - } else { 2543 - ret = ops->map(domain, iova, paddr, pgsize, prot, gfp); 2544 - *mapped = ret ? 0 : pgsize; 2545 - } 2546 - 2547 - return ret; 2548 - } 2549 - 2550 2421 static int __iommu_map(struct iommu_domain *domain, unsigned long iova, 2551 2422 phys_addr_t paddr, size_t size, int prot, gfp_t gfp) 2552 2423 { ··· 2533 2452 phys_addr_t orig_paddr = paddr; 2534 2453 int ret = 0; 2535 2454 2536 - if (unlikely(!(ops->map || ops->map_pages) || 2537 - domain->pgsize_bitmap == 0UL)) 2538 - return -ENODEV; 2539 - 2540 2455 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) 2541 2456 return -EINVAL; 2457 + 2458 + if (WARN_ON(!ops->map_pages || domain->pgsize_bitmap == 0UL)) 2459 + return -ENODEV; 2542 2460 2543 2461 /* find out the minimum page size supported */ 2544 2462 min_pagesz = 1 << __ffs(domain->pgsize_bitmap); ··· 2556 2476 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size); 2557 2477 2558 2478 while (size) { 2559 - size_t mapped = 0; 2479 + size_t pgsize, count, mapped = 0; 2560 2480 2561 - ret = __iommu_map_pages(domain, iova, paddr, size, prot, gfp, 2562 - &mapped); 2481 + pgsize = iommu_pgsize(domain, iova, paddr, size, &count); 2482 + 2483 + pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx count %zu\n", 2484 + iova, &paddr, pgsize, count); 2485 + ret = ops->map_pages(domain, iova, paddr, pgsize, count, prot, 2486 + gfp, &mapped); 2563 2487 /* 2564 2488 * Some pages may have been mapped, even if an error occurred, 2565 2489 * so we should account for those so they can be unmapped. ··· 2600 2516 return -EINVAL; 2601 2517 2602 2518 ret = __iommu_map(domain, iova, paddr, size, prot, gfp); 2603 - if (ret == 0 && ops->iotlb_sync_map) 2604 - ops->iotlb_sync_map(domain, iova, size); 2519 + if (ret == 0 && ops->iotlb_sync_map) { 2520 + ret = ops->iotlb_sync_map(domain, iova, size); 2521 + if (ret) 2522 + goto out_err; 2523 + } 2524 + 2525 + return ret; 2526 + 2527 + out_err: 2528 + /* undo mappings already done */ 2529 + iommu_unmap(domain, iova, size); 2605 2530 2606 2531 return ret; 2607 2532 } 2608 2533 EXPORT_SYMBOL_GPL(iommu_map); 2609 - 2610 - static size_t __iommu_unmap_pages(struct iommu_domain *domain, 2611 - unsigned long iova, size_t size, 2612 - struct iommu_iotlb_gather *iotlb_gather) 2613 - { 2614 - const struct iommu_domain_ops *ops = domain->ops; 2615 - size_t pgsize, count; 2616 - 2617 - pgsize = iommu_pgsize(domain, iova, iova, size, &count); 2618 - return ops->unmap_pages ? 2619 - ops->unmap_pages(domain, iova, pgsize, count, iotlb_gather) : 2620 - ops->unmap(domain, iova, pgsize, iotlb_gather); 2621 - } 2622 2534 2623 2535 static size_t __iommu_unmap(struct iommu_domain *domain, 2624 2536 unsigned long iova, size_t size, ··· 2625 2545 unsigned long orig_iova = iova; 2626 2546 unsigned int min_pagesz; 2627 2547 2628 - if (unlikely(!(ops->unmap || ops->unmap_pages) || 2629 - domain->pgsize_bitmap == 0UL)) 2548 + if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) 2630 2549 return 0; 2631 2550 2632 - if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) 2551 + if (WARN_ON(!ops->unmap_pages || domain->pgsize_bitmap == 0UL)) 2633 2552 return 0; 2634 2553 2635 2554 /* find out the minimum page size supported */ ··· 2652 2573 * or we hit an area that isn't mapped. 2653 2574 */ 2654 2575 while (unmapped < size) { 2655 - unmapped_page = __iommu_unmap_pages(domain, iova, 2656 - size - unmapped, 2657 - iotlb_gather); 2576 + size_t pgsize, count; 2577 + 2578 + pgsize = iommu_pgsize(domain, iova, iova, size - unmapped, &count); 2579 + unmapped_page = ops->unmap_pages(domain, iova, pgsize, count, iotlb_gather); 2658 2580 if (!unmapped_page) 2659 2581 break; 2660 2582 ··· 2738 2658 sg = sg_next(sg); 2739 2659 } 2740 2660 2741 - if (ops->iotlb_sync_map) 2742 - ops->iotlb_sync_map(domain, iova, mapped); 2661 + if (ops->iotlb_sync_map) { 2662 + ret = ops->iotlb_sync_map(domain, iova, mapped); 2663 + if (ret) 2664 + goto out_err; 2665 + } 2743 2666 return mapped; 2744 2667 2745 2668 out_err: ··· 3040 2957 if (req_type < 0) 3041 2958 return -EINVAL; 3042 2959 3043 - /* 3044 - * There are still some drivers which don't support default domains, so 3045 - * we ignore the failure and leave group->default_domain NULL. 3046 - * 3047 - * We assume that the iommu driver starts up the device in 3048 - * 'set_platform_dma_ops' mode if it does not support default domains. 3049 - */ 3050 2960 dom = iommu_group_alloc_default_domain(group, req_type); 3051 - if (!dom) { 3052 - /* Once in default_domain mode we never leave */ 3053 - if (group->default_domain) 3054 - return -ENODEV; 3055 - group->default_domain = NULL; 3056 - return 0; 3057 - } 2961 + if (!dom) 2962 + return -ENODEV; 3058 2963 3059 2964 if (group->default_domain == dom) 3060 2965 return 0; ··· 3185 3114 return ret ?: count; 3186 3115 } 3187 3116 3188 - static bool iommu_is_default_domain(struct iommu_group *group) 3189 - { 3190 - if (group->domain == group->default_domain) 3191 - return true; 3192 - 3193 - /* 3194 - * If the default domain was set to identity and it is still an identity 3195 - * domain then we consider this a pass. This happens because of 3196 - * amd_iommu_init_device() replacing the default idenytity domain with an 3197 - * identity domain that has a different configuration for AMDGPU. 3198 - */ 3199 - if (group->default_domain && 3200 - group->default_domain->type == IOMMU_DOMAIN_IDENTITY && 3201 - group->domain && group->domain->type == IOMMU_DOMAIN_IDENTITY) 3202 - return true; 3203 - return false; 3204 - } 3205 - 3206 3117 /** 3207 3118 * iommu_device_use_default_domain() - Device driver wants to handle device 3208 3119 * DMA through the kernel DMA API. ··· 3195 3142 */ 3196 3143 int iommu_device_use_default_domain(struct device *dev) 3197 3144 { 3198 - struct iommu_group *group = iommu_group_get(dev); 3145 + /* Caller is the driver core during the pre-probe path */ 3146 + struct iommu_group *group = dev->iommu_group; 3199 3147 int ret = 0; 3200 3148 3201 3149 if (!group) ··· 3204 3150 3205 3151 mutex_lock(&group->mutex); 3206 3152 if (group->owner_cnt) { 3207 - if (group->owner || !iommu_is_default_domain(group) || 3153 + if (group->domain != group->default_domain || group->owner || 3208 3154 !xa_empty(&group->pasid_array)) { 3209 3155 ret = -EBUSY; 3210 3156 goto unlock_out; ··· 3215 3161 3216 3162 unlock_out: 3217 3163 mutex_unlock(&group->mutex); 3218 - iommu_group_put(group); 3219 - 3220 3164 return ret; 3221 3165 } 3222 3166 ··· 3228 3176 */ 3229 3177 void iommu_device_unuse_default_domain(struct device *dev) 3230 3178 { 3231 - struct iommu_group *group = iommu_group_get(dev); 3179 + /* Caller is the driver core during the post-probe path */ 3180 + struct iommu_group *group = dev->iommu_group; 3232 3181 3233 3182 if (!group) 3234 3183 return; ··· 3239 3186 group->owner_cnt--; 3240 3187 3241 3188 mutex_unlock(&group->mutex); 3242 - iommu_group_put(group); 3243 3189 } 3244 3190 3245 3191 static int __iommu_group_alloc_blocking_domain(struct iommu_group *group) 3246 3192 { 3247 - struct group_device *dev = 3248 - list_first_entry(&group->devices, struct group_device, list); 3249 - 3250 3193 if (group->blocking_domain) 3251 3194 return 0; 3252 3195 3253 3196 group->blocking_domain = 3254 - __iommu_domain_alloc(dev->dev->bus, IOMMU_DOMAIN_BLOCKED); 3197 + __iommu_group_domain_alloc(group, IOMMU_DOMAIN_BLOCKED); 3255 3198 if (!group->blocking_domain) { 3256 3199 /* 3257 3200 * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED 3258 3201 * create an empty domain instead. 3259 3202 */ 3260 - group->blocking_domain = __iommu_domain_alloc( 3261 - dev->dev->bus, IOMMU_DOMAIN_UNMANAGED); 3203 + group->blocking_domain = __iommu_group_domain_alloc( 3204 + group, IOMMU_DOMAIN_UNMANAGED); 3262 3205 if (!group->blocking_domain) 3263 3206 return -EINVAL; 3264 3207 } ··· 3322 3273 */ 3323 3274 int iommu_device_claim_dma_owner(struct device *dev, void *owner) 3324 3275 { 3325 - struct iommu_group *group; 3276 + /* Caller must be a probed driver on dev */ 3277 + struct iommu_group *group = dev->iommu_group; 3326 3278 int ret = 0; 3327 3279 3328 3280 if (WARN_ON(!owner)) 3329 3281 return -EINVAL; 3330 3282 3331 - group = iommu_group_get(dev); 3332 3283 if (!group) 3333 3284 return -ENODEV; 3334 3285 ··· 3345 3296 ret = __iommu_take_dma_ownership(group, owner); 3346 3297 unlock_out: 3347 3298 mutex_unlock(&group->mutex); 3348 - iommu_group_put(group); 3349 - 3350 3299 return ret; 3351 3300 } 3352 3301 EXPORT_SYMBOL_GPL(iommu_device_claim_dma_owner); ··· 3382 3335 */ 3383 3336 void iommu_device_release_dma_owner(struct device *dev) 3384 3337 { 3385 - struct iommu_group *group = iommu_group_get(dev); 3338 + /* Caller must be a probed driver on dev */ 3339 + struct iommu_group *group = dev->iommu_group; 3386 3340 3387 3341 mutex_lock(&group->mutex); 3388 3342 if (group->owner_cnt > 1) ··· 3391 3343 else 3392 3344 __iommu_release_dma_ownership(group); 3393 3345 mutex_unlock(&group->mutex); 3394 - iommu_group_put(group); 3395 3346 } 3396 3347 EXPORT_SYMBOL_GPL(iommu_device_release_dma_owner); 3397 3348 ··· 3451 3404 int iommu_attach_device_pasid(struct iommu_domain *domain, 3452 3405 struct device *dev, ioasid_t pasid) 3453 3406 { 3454 - struct iommu_group *group; 3407 + /* Caller must be a probed driver on dev */ 3408 + struct iommu_group *group = dev->iommu_group; 3455 3409 void *curr; 3456 3410 int ret; 3457 3411 3458 3412 if (!domain->ops->set_dev_pasid) 3459 3413 return -EOPNOTSUPP; 3460 3414 3461 - group = iommu_group_get(dev); 3462 3415 if (!group) 3463 3416 return -ENODEV; 3464 3417 ··· 3476 3429 } 3477 3430 out_unlock: 3478 3431 mutex_unlock(&group->mutex); 3479 - iommu_group_put(group); 3480 - 3481 3432 return ret; 3482 3433 } 3483 3434 EXPORT_SYMBOL_GPL(iommu_attach_device_pasid); ··· 3492 3447 void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev, 3493 3448 ioasid_t pasid) 3494 3449 { 3495 - struct iommu_group *group = iommu_group_get(dev); 3450 + /* Caller must be a probed driver on dev */ 3451 + struct iommu_group *group = dev->iommu_group; 3496 3452 3497 3453 mutex_lock(&group->mutex); 3498 3454 __iommu_remove_group_pasid(group, pasid); 3499 3455 WARN_ON(xa_erase(&group->pasid_array, pasid) != domain); 3500 3456 mutex_unlock(&group->mutex); 3501 - 3502 - iommu_group_put(group); 3503 3457 } 3504 3458 EXPORT_SYMBOL_GPL(iommu_detach_device_pasid); 3505 3459 ··· 3520 3476 ioasid_t pasid, 3521 3477 unsigned int type) 3522 3478 { 3479 + /* Caller must be a probed driver on dev */ 3480 + struct iommu_group *group = dev->iommu_group; 3523 3481 struct iommu_domain *domain; 3524 - struct iommu_group *group; 3525 3482 3526 - group = iommu_group_get(dev); 3527 3483 if (!group) 3528 3484 return NULL; 3529 3485 ··· 3532 3488 if (type && domain && domain->type != type) 3533 3489 domain = ERR_PTR(-EBUSY); 3534 3490 xa_unlock(&group->pasid_array); 3535 - iommu_group_put(group); 3536 3491 3537 3492 return domain; 3538 3493 }
+7 -24
drivers/iommu/iommufd/selftest.c
··· 123 123 }; 124 124 }; 125 125 126 - static void mock_domain_blocking_free(struct iommu_domain *domain) 127 - { 128 - } 129 - 130 126 static int mock_domain_nop_attach(struct iommu_domain *domain, 131 127 struct device *dev) 132 128 { ··· 135 139 } 136 140 137 141 static const struct iommu_domain_ops mock_blocking_ops = { 138 - .free = mock_domain_blocking_free, 139 142 .attach_dev = mock_domain_nop_attach, 140 143 }; 141 144 ··· 251 256 for (i = 0; i < MOCK_NESTED_DOMAIN_IOTLB_NUM; i++) 252 257 mock_nested->iotlb[i] = user_cfg->iotlb; 253 258 return &mock_nested->domain; 254 - } 255 - 256 - static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type) 257 - { 258 - if (iommu_domain_type == IOMMU_DOMAIN_BLOCKED) 259 - return &mock_blocking_domain; 260 - if (iommu_domain_type == IOMMU_DOMAIN_UNMANAGED) 261 - return mock_domain_alloc_paging(NULL); 262 - return NULL; 263 259 } 264 260 265 261 static struct iommu_domain * ··· 432 446 return false; 433 447 } 434 448 435 - static void mock_domain_set_plaform_dma_ops(struct device *dev) 436 - { 437 - /* 438 - * mock doesn't setup default domains because we can't hook into the 439 - * normal probe path 440 - */ 441 - } 442 - 443 449 static struct iommu_device mock_iommu_device = { 444 450 }; 445 451 ··· 441 463 } 442 464 443 465 static const struct iommu_ops mock_ops = { 466 + /* 467 + * IOMMU_DOMAIN_BLOCKED cannot be returned from def_domain_type() 468 + * because it is zero. 469 + */ 470 + .default_domain = &mock_blocking_domain, 471 + .blocked_domain = &mock_blocking_domain, 444 472 .owner = THIS_MODULE, 445 473 .pgsize_bitmap = MOCK_IO_PAGE_SIZE, 446 474 .hw_info = mock_domain_hw_info, 447 - .domain_alloc = mock_domain_alloc, 475 + .domain_alloc_paging = mock_domain_alloc_paging, 448 476 .domain_alloc_user = mock_domain_alloc_user, 449 477 .capable = mock_domain_capable, 450 - .set_platform_dma_ops = mock_domain_set_plaform_dma_ops, 451 478 .device_group = generic_device_group, 452 479 .probe_device = mock_probe_device, 453 480 .default_domain_ops =
+65 -30
drivers/iommu/iova.c
··· 11 11 #include <linux/smp.h> 12 12 #include <linux/bitops.h> 13 13 #include <linux/cpu.h> 14 + #include <linux/workqueue.h> 14 15 15 16 /* The anchor node sits above the top of the usable address space */ 16 17 #define IOVA_ANCHOR ~0UL ··· 623 622 /* 624 623 * As kmalloc's buffer size is fixed to power of 2, 127 is chosen to 625 624 * assure size of 'iova_magazine' to be 1024 bytes, so that no memory 626 - * will be wasted. 625 + * will be wasted. Since only full magazines are inserted into the depot, 626 + * we don't need to waste PFN capacity on a separate list head either. 627 627 */ 628 628 #define IOVA_MAG_SIZE 127 629 - #define MAX_GLOBAL_MAGS 32 /* magazines per bin */ 629 + 630 + #define IOVA_DEPOT_DELAY msecs_to_jiffies(100) 630 631 631 632 struct iova_magazine { 632 - unsigned long size; 633 + union { 634 + unsigned long size; 635 + struct iova_magazine *next; 636 + }; 633 637 unsigned long pfns[IOVA_MAG_SIZE]; 634 638 }; 639 + static_assert(!(sizeof(struct iova_magazine) & (sizeof(struct iova_magazine) - 1))); 635 640 636 641 struct iova_cpu_rcache { 637 642 spinlock_t lock; ··· 647 640 648 641 struct iova_rcache { 649 642 spinlock_t lock; 650 - unsigned long depot_size; 651 - struct iova_magazine *depot[MAX_GLOBAL_MAGS]; 643 + unsigned int depot_size; 644 + struct iova_magazine *depot; 652 645 struct iova_cpu_rcache __percpu *cpu_rcaches; 646 + struct iova_domain *iovad; 647 + struct delayed_work work; 653 648 }; 654 649 655 650 static struct iova_magazine *iova_magazine_alloc(gfp_t flags) ··· 726 717 mag->pfns[mag->size++] = pfn; 727 718 } 728 719 720 + static struct iova_magazine *iova_depot_pop(struct iova_rcache *rcache) 721 + { 722 + struct iova_magazine *mag = rcache->depot; 723 + 724 + rcache->depot = mag->next; 725 + mag->size = IOVA_MAG_SIZE; 726 + rcache->depot_size--; 727 + return mag; 728 + } 729 + 730 + static void iova_depot_push(struct iova_rcache *rcache, struct iova_magazine *mag) 731 + { 732 + mag->next = rcache->depot; 733 + rcache->depot = mag; 734 + rcache->depot_size++; 735 + } 736 + 737 + static void iova_depot_work_func(struct work_struct *work) 738 + { 739 + struct iova_rcache *rcache = container_of(work, typeof(*rcache), work.work); 740 + struct iova_magazine *mag = NULL; 741 + unsigned long flags; 742 + 743 + spin_lock_irqsave(&rcache->lock, flags); 744 + if (rcache->depot_size > num_online_cpus()) 745 + mag = iova_depot_pop(rcache); 746 + spin_unlock_irqrestore(&rcache->lock, flags); 747 + 748 + if (mag) { 749 + iova_magazine_free_pfns(mag, rcache->iovad); 750 + iova_magazine_free(mag); 751 + schedule_delayed_work(&rcache->work, IOVA_DEPOT_DELAY); 752 + } 753 + } 754 + 729 755 int iova_domain_init_rcaches(struct iova_domain *iovad) 730 756 { 731 757 unsigned int cpu; ··· 778 734 779 735 rcache = &iovad->rcaches[i]; 780 736 spin_lock_init(&rcache->lock); 781 - rcache->depot_size = 0; 737 + rcache->iovad = iovad; 738 + INIT_DELAYED_WORK(&rcache->work, iova_depot_work_func); 782 739 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), 783 740 cache_line_size()); 784 741 if (!rcache->cpu_rcaches) { ··· 821 776 struct iova_rcache *rcache, 822 777 unsigned long iova_pfn) 823 778 { 824 - struct iova_magazine *mag_to_free = NULL; 825 779 struct iova_cpu_rcache *cpu_rcache; 826 780 bool can_insert = false; 827 781 unsigned long flags; ··· 838 794 839 795 if (new_mag) { 840 796 spin_lock(&rcache->lock); 841 - if (rcache->depot_size < MAX_GLOBAL_MAGS) { 842 - rcache->depot[rcache->depot_size++] = 843 - cpu_rcache->loaded; 844 - } else { 845 - mag_to_free = cpu_rcache->loaded; 846 - } 797 + iova_depot_push(rcache, cpu_rcache->loaded); 847 798 spin_unlock(&rcache->lock); 799 + schedule_delayed_work(&rcache->work, IOVA_DEPOT_DELAY); 848 800 849 801 cpu_rcache->loaded = new_mag; 850 802 can_insert = true; ··· 851 811 iova_magazine_push(cpu_rcache->loaded, iova_pfn); 852 812 853 813 spin_unlock_irqrestore(&cpu_rcache->lock, flags); 854 - 855 - if (mag_to_free) { 856 - iova_magazine_free_pfns(mag_to_free, iovad); 857 - iova_magazine_free(mag_to_free); 858 - } 859 814 860 815 return can_insert; 861 816 } ··· 889 854 has_pfn = true; 890 855 } else { 891 856 spin_lock(&rcache->lock); 892 - if (rcache->depot_size > 0) { 857 + if (rcache->depot) { 893 858 iova_magazine_free(cpu_rcache->loaded); 894 - cpu_rcache->loaded = rcache->depot[--rcache->depot_size]; 859 + cpu_rcache->loaded = iova_depot_pop(rcache); 895 860 has_pfn = true; 896 861 } 897 862 spin_unlock(&rcache->lock); ··· 930 895 struct iova_rcache *rcache; 931 896 struct iova_cpu_rcache *cpu_rcache; 932 897 unsigned int cpu; 933 - int i, j; 934 898 935 - for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) { 899 + for (int i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) { 936 900 rcache = &iovad->rcaches[i]; 937 901 if (!rcache->cpu_rcaches) 938 902 break; ··· 941 907 iova_magazine_free(cpu_rcache->prev); 942 908 } 943 909 free_percpu(rcache->cpu_rcaches); 944 - for (j = 0; j < rcache->depot_size; ++j) 945 - iova_magazine_free(rcache->depot[j]); 910 + cancel_delayed_work_sync(&rcache->work); 911 + while (rcache->depot) 912 + iova_magazine_free(iova_depot_pop(rcache)); 946 913 } 947 914 948 915 kfree(iovad->rcaches); ··· 977 942 { 978 943 struct iova_rcache *rcache; 979 944 unsigned long flags; 980 - int i, j; 981 945 982 - for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) { 946 + for (int i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) { 983 947 rcache = &iovad->rcaches[i]; 984 948 spin_lock_irqsave(&rcache->lock, flags); 985 - for (j = 0; j < rcache->depot_size; ++j) { 986 - iova_magazine_free_pfns(rcache->depot[j], iovad); 987 - iova_magazine_free(rcache->depot[j]); 949 + while (rcache->depot) { 950 + struct iova_magazine *mag = iova_depot_pop(rcache); 951 + 952 + iova_magazine_free_pfns(mag, iovad); 953 + iova_magazine_free(mag); 988 954 } 989 - rcache->depot_size = 0; 990 955 spin_unlock_irqrestore(&rcache->lock, flags); 991 956 } 992 957 }
+50 -22
drivers/iommu/ipmmu-vmsa.c
··· 64 64 struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX]; 65 65 s8 utlb_ctx[IPMMU_UTLB_MAX]; 66 66 67 - struct iommu_group *group; 68 67 struct dma_iommu_mapping *mapping; 69 68 }; 70 69 ··· 292 293 ipmmu_imuctr_write(mmu, utlb, IMUCTR_TTSEL_MMU(domain->context_id) | 293 294 IMUCTR_FLUSH | IMUCTR_MMUEN); 294 295 mmu->utlb_ctx[utlb] = domain->context_id; 296 + } 297 + 298 + /* 299 + * Disable MMU translation for the microTLB. 300 + */ 301 + static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain, 302 + unsigned int utlb) 303 + { 304 + struct ipmmu_vmsa_device *mmu = domain->mmu; 305 + 306 + ipmmu_imuctr_write(mmu, utlb, 0); 307 + mmu->utlb_ctx[utlb] = IPMMU_CTX_INVALID; 295 308 } 296 309 297 310 static void ipmmu_tlb_flush_all(void *cookie) ··· 562 551 * IOMMU Operations 563 552 */ 564 553 565 - static struct iommu_domain *ipmmu_domain_alloc(unsigned type) 554 + static struct iommu_domain *ipmmu_domain_alloc_paging(struct device *dev) 566 555 { 567 556 struct ipmmu_vmsa_domain *domain; 568 - 569 - if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA) 570 - return NULL; 571 557 572 558 domain = kzalloc(sizeof(*domain), GFP_KERNEL); 573 559 if (!domain) ··· 634 626 635 627 return 0; 636 628 } 629 + 630 + static int ipmmu_iommu_identity_attach(struct iommu_domain *identity_domain, 631 + struct device *dev) 632 + { 633 + struct iommu_domain *io_domain = iommu_get_domain_for_dev(dev); 634 + struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 635 + struct ipmmu_vmsa_domain *domain; 636 + unsigned int i; 637 + 638 + if (io_domain == identity_domain || !io_domain) 639 + return 0; 640 + 641 + domain = to_vmsa_domain(io_domain); 642 + for (i = 0; i < fwspec->num_ids; ++i) 643 + ipmmu_utlb_disable(domain, fwspec->ids[i]); 644 + 645 + /* 646 + * TODO: Optimize by disabling the context when no device is attached. 647 + */ 648 + return 0; 649 + } 650 + 651 + static struct iommu_domain_ops ipmmu_iommu_identity_ops = { 652 + .attach_dev = ipmmu_iommu_identity_attach, 653 + }; 654 + 655 + static struct iommu_domain ipmmu_iommu_identity_domain = { 656 + .type = IOMMU_DOMAIN_IDENTITY, 657 + .ops = &ipmmu_iommu_identity_ops, 658 + }; 637 659 638 660 static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova, 639 661 phys_addr_t paddr, size_t pgsize, size_t pgcount, ··· 871 833 arm_iommu_release_mapping(mmu->mapping); 872 834 } 873 835 874 - static struct iommu_group *ipmmu_find_group(struct device *dev) 875 - { 876 - struct ipmmu_vmsa_device *mmu = to_ipmmu(dev); 877 - struct iommu_group *group; 878 - 879 - if (mmu->group) 880 - return iommu_group_ref_get(mmu->group); 881 - 882 - group = iommu_group_alloc(); 883 - if (!IS_ERR(group)) 884 - mmu->group = group; 885 - 886 - return group; 887 - } 888 - 889 836 static const struct iommu_ops ipmmu_ops = { 890 - .domain_alloc = ipmmu_domain_alloc, 837 + .identity_domain = &ipmmu_iommu_identity_domain, 838 + .domain_alloc_paging = ipmmu_domain_alloc_paging, 891 839 .probe_device = ipmmu_probe_device, 892 840 .release_device = ipmmu_release_device, 893 841 .probe_finalize = ipmmu_probe_finalize, 842 + /* 843 + * FIXME: The device grouping is a fixed property of the hardware's 844 + * ability to isolate and control DMA, it should not depend on kconfig. 845 + */ 894 846 .device_group = IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA) 895 - ? generic_device_group : ipmmu_find_group, 847 + ? generic_device_group : generic_single_device_group, 896 848 .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K, 897 849 .of_xlate = ipmmu_of_xlate, 898 850 .default_domain_ops = &(const struct iommu_domain_ops) {
+24 -11
drivers/iommu/msm_iommu.c
··· 302 302 SET_M(base, ctx, 1); 303 303 } 304 304 305 - static struct iommu_domain *msm_iommu_domain_alloc(unsigned type) 305 + static struct iommu_domain *msm_iommu_domain_alloc_paging(struct device *dev) 306 306 { 307 307 struct msm_priv *priv; 308 - 309 - if (type != IOMMU_DOMAIN_UNMANAGED) 310 - return NULL; 311 308 312 309 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 313 310 if (!priv) ··· 440 443 return ret; 441 444 } 442 445 443 - static void msm_iommu_set_platform_dma(struct device *dev) 446 + static int msm_iommu_identity_attach(struct iommu_domain *identity_domain, 447 + struct device *dev) 444 448 { 445 449 struct iommu_domain *domain = iommu_get_domain_for_dev(dev); 446 - struct msm_priv *priv = to_msm_priv(domain); 450 + struct msm_priv *priv; 447 451 unsigned long flags; 448 452 struct msm_iommu_dev *iommu; 449 453 struct msm_iommu_ctx_dev *master; 450 - int ret; 454 + int ret = 0; 451 455 456 + if (domain == identity_domain || !domain) 457 + return 0; 458 + 459 + priv = to_msm_priv(domain); 452 460 free_io_pgtable_ops(priv->iop); 453 461 454 462 spin_lock_irqsave(&msm_iommu_lock, flags); ··· 470 468 } 471 469 fail: 472 470 spin_unlock_irqrestore(&msm_iommu_lock, flags); 471 + return ret; 473 472 } 473 + 474 + static struct iommu_domain_ops msm_iommu_identity_ops = { 475 + .attach_dev = msm_iommu_identity_attach, 476 + }; 477 + 478 + static struct iommu_domain msm_iommu_identity_domain = { 479 + .type = IOMMU_DOMAIN_IDENTITY, 480 + .ops = &msm_iommu_identity_ops, 481 + }; 474 482 475 483 static int msm_iommu_map(struct iommu_domain *domain, unsigned long iova, 476 484 phys_addr_t pa, size_t pgsize, size_t pgcount, ··· 498 486 return ret; 499 487 } 500 488 501 - static void msm_iommu_sync_map(struct iommu_domain *domain, unsigned long iova, 502 - size_t size) 489 + static int msm_iommu_sync_map(struct iommu_domain *domain, unsigned long iova, 490 + size_t size) 503 491 { 504 492 struct msm_priv *priv = to_msm_priv(domain); 505 493 506 494 __flush_iotlb_range(iova, size, SZ_4K, false, priv); 495 + return 0; 507 496 } 508 497 509 498 static size_t msm_iommu_unmap(struct iommu_domain *domain, unsigned long iova, ··· 688 675 } 689 676 690 677 static struct iommu_ops msm_iommu_ops = { 691 - .domain_alloc = msm_iommu_domain_alloc, 678 + .identity_domain = &msm_iommu_identity_domain, 679 + .domain_alloc_paging = msm_iommu_domain_alloc_paging, 692 680 .probe_device = msm_iommu_probe_device, 693 681 .device_group = generic_device_group, 694 - .set_platform_dma_ops = msm_iommu_set_platform_dma, 695 682 .pgsize_bitmap = MSM_IOMMU_PGSIZES, 696 683 .of_xlate = qcom_iommu_of_xlate, 697 684 .default_domain_ops = &(const struct iommu_domain_ops) {
+28 -7
drivers/iommu/mtk_iommu.c
··· 688 688 return 0; 689 689 } 690 690 691 - static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type) 691 + static struct iommu_domain *mtk_iommu_domain_alloc_paging(struct device *dev) 692 692 { 693 693 struct mtk_iommu_domain *dom; 694 - 695 - if (type != IOMMU_DOMAIN_DMA && type != IOMMU_DOMAIN_UNMANAGED) 696 - return NULL; 697 694 698 695 dom = kzalloc(sizeof(*dom), GFP_KERNEL); 699 696 if (!dom) ··· 773 776 return ret; 774 777 } 775 778 779 + static int mtk_iommu_identity_attach(struct iommu_domain *identity_domain, 780 + struct device *dev) 781 + { 782 + struct iommu_domain *domain = iommu_get_domain_for_dev(dev); 783 + struct mtk_iommu_data *data = dev_iommu_priv_get(dev); 784 + 785 + if (domain == identity_domain || !domain) 786 + return 0; 787 + 788 + mtk_iommu_config(data, dev, false, 0); 789 + return 0; 790 + } 791 + 792 + static struct iommu_domain_ops mtk_iommu_identity_ops = { 793 + .attach_dev = mtk_iommu_identity_attach, 794 + }; 795 + 796 + static struct iommu_domain mtk_iommu_identity_domain = { 797 + .type = IOMMU_DOMAIN_IDENTITY, 798 + .ops = &mtk_iommu_identity_ops, 799 + }; 800 + 776 801 static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova, 777 802 phys_addr_t paddr, size_t pgsize, size_t pgcount, 778 803 int prot, gfp_t gfp, size_t *mapped) ··· 836 817 mtk_iommu_tlb_flush_range_sync(gather->start, length, dom->bank); 837 818 } 838 819 839 - static void mtk_iommu_sync_map(struct iommu_domain *domain, unsigned long iova, 840 - size_t size) 820 + static int mtk_iommu_sync_map(struct iommu_domain *domain, unsigned long iova, 821 + size_t size) 841 822 { 842 823 struct mtk_iommu_domain *dom = to_mtk_domain(domain); 843 824 844 825 mtk_iommu_tlb_flush_range_sync(iova, size, dom->bank); 826 + return 0; 845 827 } 846 828 847 829 static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain, ··· 1015 995 } 1016 996 1017 997 static const struct iommu_ops mtk_iommu_ops = { 1018 - .domain_alloc = mtk_iommu_domain_alloc, 998 + .identity_domain = &mtk_iommu_identity_domain, 999 + .domain_alloc_paging = mtk_iommu_domain_alloc_paging, 1019 1000 .probe_device = mtk_iommu_probe_device, 1020 1001 .release_device = mtk_iommu_release_device, 1021 1002 .device_group = mtk_iommu_device_group,
+15 -13
drivers/iommu/mtk_iommu_v1.c
··· 270 270 return 0; 271 271 } 272 272 273 - static struct iommu_domain *mtk_iommu_v1_domain_alloc(unsigned type) 273 + static struct iommu_domain *mtk_iommu_v1_domain_alloc_paging(struct device *dev) 274 274 { 275 275 struct mtk_iommu_v1_domain *dom; 276 - 277 - if (type != IOMMU_DOMAIN_UNMANAGED) 278 - return NULL; 279 276 280 277 dom = kzalloc(sizeof(*dom), GFP_KERNEL); 281 278 if (!dom) ··· 316 319 return 0; 317 320 } 318 321 319 - static void mtk_iommu_v1_set_platform_dma(struct device *dev) 322 + static int mtk_iommu_v1_identity_attach(struct iommu_domain *identity_domain, 323 + struct device *dev) 320 324 { 321 325 struct mtk_iommu_v1_data *data = dev_iommu_priv_get(dev); 322 326 323 327 mtk_iommu_v1_config(data, dev, false); 328 + return 0; 324 329 } 330 + 331 + static struct iommu_domain_ops mtk_iommu_v1_identity_ops = { 332 + .attach_dev = mtk_iommu_v1_identity_attach, 333 + }; 334 + 335 + static struct iommu_domain mtk_iommu_v1_identity_domain = { 336 + .type = IOMMU_DOMAIN_IDENTITY, 337 + .ops = &mtk_iommu_v1_identity_ops, 338 + }; 325 339 326 340 static int mtk_iommu_v1_map(struct iommu_domain *domain, unsigned long iova, 327 341 phys_addr_t paddr, size_t pgsize, size_t pgcount, ··· 447 439 } 448 440 449 441 return 0; 450 - } 451 - 452 - static int mtk_iommu_v1_def_domain_type(struct device *dev) 453 - { 454 - return IOMMU_DOMAIN_UNMANAGED; 455 442 } 456 443 457 444 static struct iommu_device *mtk_iommu_v1_probe_device(struct device *dev) ··· 581 578 } 582 579 583 580 static const struct iommu_ops mtk_iommu_v1_ops = { 584 - .domain_alloc = mtk_iommu_v1_domain_alloc, 581 + .identity_domain = &mtk_iommu_v1_identity_domain, 582 + .domain_alloc_paging = mtk_iommu_v1_domain_alloc_paging, 585 583 .probe_device = mtk_iommu_v1_probe_device, 586 584 .probe_finalize = mtk_iommu_v1_probe_finalize, 587 585 .release_device = mtk_iommu_v1_release_device, 588 - .def_domain_type = mtk_iommu_v1_def_domain_type, 589 586 .device_group = generic_device_group, 590 587 .pgsize_bitmap = MT2701_IOMMU_PAGE_SIZE, 591 - .set_platform_dma_ops = mtk_iommu_v1_set_platform_dma, 592 588 .owner = THIS_MODULE, 593 589 .default_domain_ops = &(const struct iommu_domain_ops) { 594 590 .attach_dev = mtk_iommu_v1_attach_device,
+31 -38
drivers/iommu/omap-iommu.c
··· 1225 1225 platform_set_drvdata(pdev, obj); 1226 1226 1227 1227 if (omap_iommu_can_register(pdev)) { 1228 - obj->group = iommu_group_alloc(); 1229 - if (IS_ERR(obj->group)) 1230 - return PTR_ERR(obj->group); 1231 - 1232 1228 err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL, 1233 1229 obj->name); 1234 1230 if (err) 1235 - goto out_group; 1231 + return err; 1236 1232 1237 1233 err = iommu_device_register(&obj->iommu, &omap_iommu_ops, &pdev->dev); 1238 1234 if (err) 1239 1235 goto out_sysfs; 1236 + obj->has_iommu_driver = true; 1240 1237 } 1241 1238 1242 1239 pm_runtime_enable(obj->dev); ··· 1249 1252 1250 1253 out_sysfs: 1251 1254 iommu_device_sysfs_remove(&obj->iommu); 1252 - out_group: 1253 - iommu_group_put(obj->group); 1254 1255 return err; 1255 1256 } 1256 1257 ··· 1256 1261 { 1257 1262 struct omap_iommu *obj = platform_get_drvdata(pdev); 1258 1263 1259 - if (obj->group) { 1260 - iommu_group_put(obj->group); 1261 - obj->group = NULL; 1262 - 1264 + if (obj->has_iommu_driver) { 1263 1265 iommu_device_sysfs_remove(&obj->iommu); 1264 1266 iommu_device_unregister(&obj->iommu); 1265 1267 } ··· 1310 1318 } 1311 1319 1312 1320 static int omap_iommu_map(struct iommu_domain *domain, unsigned long da, 1313 - phys_addr_t pa, size_t bytes, int prot, gfp_t gfp) 1321 + phys_addr_t pa, size_t bytes, size_t count, 1322 + int prot, gfp_t gfp, size_t *mapped) 1314 1323 { 1315 1324 struct omap_iommu_domain *omap_domain = to_omap_domain(domain); 1316 1325 struct device *dev = omap_domain->dev; ··· 1349 1356 oiommu = iommu->iommu_dev; 1350 1357 iopgtable_clear_entry(oiommu, da); 1351 1358 } 1359 + } else { 1360 + *mapped = bytes; 1352 1361 } 1353 1362 1354 1363 return ret; 1355 1364 } 1356 1365 1357 1366 static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da, 1358 - size_t size, struct iommu_iotlb_gather *gather) 1367 + size_t size, size_t count, struct iommu_iotlb_gather *gather) 1359 1368 { 1360 1369 struct omap_iommu_domain *omap_domain = to_omap_domain(domain); 1361 1370 struct device *dev = omap_domain->dev; ··· 1550 1555 omap_domain->dev = NULL; 1551 1556 } 1552 1557 1553 - static void omap_iommu_set_platform_dma(struct device *dev) 1558 + static int omap_iommu_identity_attach(struct iommu_domain *identity_domain, 1559 + struct device *dev) 1554 1560 { 1555 1561 struct iommu_domain *domain = iommu_get_domain_for_dev(dev); 1556 - struct omap_iommu_domain *omap_domain = to_omap_domain(domain); 1562 + struct omap_iommu_domain *omap_domain; 1557 1563 1564 + if (domain == identity_domain || !domain) 1565 + return 0; 1566 + 1567 + omap_domain = to_omap_domain(domain); 1558 1568 spin_lock(&omap_domain->lock); 1559 1569 _omap_iommu_detach_dev(omap_domain, dev); 1560 1570 spin_unlock(&omap_domain->lock); 1571 + return 0; 1561 1572 } 1562 1573 1563 - static struct iommu_domain *omap_iommu_domain_alloc(unsigned type) 1574 + static struct iommu_domain_ops omap_iommu_identity_ops = { 1575 + .attach_dev = omap_iommu_identity_attach, 1576 + }; 1577 + 1578 + static struct iommu_domain omap_iommu_identity_domain = { 1579 + .type = IOMMU_DOMAIN_IDENTITY, 1580 + .ops = &omap_iommu_identity_ops, 1581 + }; 1582 + 1583 + static struct iommu_domain *omap_iommu_domain_alloc_paging(struct device *dev) 1564 1584 { 1565 1585 struct omap_iommu_domain *omap_domain; 1566 - 1567 - if (type != IOMMU_DOMAIN_UNMANAGED) 1568 - return NULL; 1569 1586 1570 1587 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL); 1571 1588 if (!omap_domain) ··· 1724 1717 1725 1718 } 1726 1719 1727 - static struct iommu_group *omap_iommu_device_group(struct device *dev) 1728 - { 1729 - struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev); 1730 - struct iommu_group *group = ERR_PTR(-EINVAL); 1731 - 1732 - if (!arch_data) 1733 - return ERR_PTR(-ENODEV); 1734 - 1735 - if (arch_data->iommu_dev) 1736 - group = iommu_group_ref_get(arch_data->iommu_dev->group); 1737 - 1738 - return group; 1739 - } 1740 - 1741 1720 static const struct iommu_ops omap_iommu_ops = { 1742 - .domain_alloc = omap_iommu_domain_alloc, 1721 + .identity_domain = &omap_iommu_identity_domain, 1722 + .domain_alloc_paging = omap_iommu_domain_alloc_paging, 1743 1723 .probe_device = omap_iommu_probe_device, 1744 1724 .release_device = omap_iommu_release_device, 1745 - .device_group = omap_iommu_device_group, 1746 - .set_platform_dma_ops = omap_iommu_set_platform_dma, 1725 + .device_group = generic_single_device_group, 1747 1726 .pgsize_bitmap = OMAP_IOMMU_PGSIZES, 1748 1727 .default_domain_ops = &(const struct iommu_domain_ops) { 1749 1728 .attach_dev = omap_iommu_attach_dev, 1750 - .map = omap_iommu_map, 1751 - .unmap = omap_iommu_unmap, 1729 + .map_pages = omap_iommu_map, 1730 + .unmap_pages = omap_iommu_unmap, 1752 1731 .iova_to_phys = omap_iommu_iova_to_phys, 1753 1732 .free = omap_iommu_domain_free, 1754 1733 }
+1 -1
drivers/iommu/omap-iommu.h
··· 80 80 u32 id; 81 81 82 82 struct iommu_device iommu; 83 - struct iommu_group *group; 83 + bool has_iommu_driver; 84 84 85 85 u8 pwrst; 86 86 };
+12 -47
drivers/iommu/rockchip-iommu.c
··· 113 113 struct iommu_device iommu; 114 114 struct list_head node; /* entry in rk_iommu_domain.iommus */ 115 115 struct iommu_domain *domain; /* domain to which iommu is attached */ 116 - struct iommu_group *group; 117 116 }; 118 117 119 118 struct rk_iommudata { ··· 816 817 } 817 818 818 819 static int rk_iommu_map(struct iommu_domain *domain, unsigned long _iova, 819 - phys_addr_t paddr, size_t size, int prot, gfp_t gfp) 820 + phys_addr_t paddr, size_t size, size_t count, 821 + int prot, gfp_t gfp, size_t *mapped) 820 822 { 821 823 struct rk_iommu_domain *rk_domain = to_rk_domain(domain); 822 824 unsigned long flags; ··· 850 850 paddr, size, prot); 851 851 852 852 spin_unlock_irqrestore(&rk_domain->dt_lock, flags); 853 + if (!ret) 854 + *mapped = size; 853 855 854 856 return ret; 855 857 } 856 858 857 859 static size_t rk_iommu_unmap(struct iommu_domain *domain, unsigned long _iova, 858 - size_t size, struct iommu_iotlb_gather *gather) 860 + size_t size, size_t count, struct iommu_iotlb_gather *gather) 859 861 { 860 862 struct rk_iommu_domain *rk_domain = to_rk_domain(domain); 861 863 unsigned long flags; ··· 991 989 return 0; 992 990 } 993 991 994 - static void rk_iommu_identity_free(struct iommu_domain *domain) 995 - { 996 - } 997 - 998 992 static struct iommu_domain_ops rk_identity_ops = { 999 993 .attach_dev = rk_iommu_identity_attach, 1000 - .free = rk_iommu_identity_free, 1001 994 }; 1002 995 1003 996 static struct iommu_domain rk_identity_domain = { 1004 997 .type = IOMMU_DOMAIN_IDENTITY, 1005 998 .ops = &rk_identity_ops, 1006 999 }; 1007 - 1008 - #ifdef CONFIG_ARM 1009 - static void rk_iommu_set_platform_dma(struct device *dev) 1010 - { 1011 - WARN_ON(rk_iommu_identity_attach(&rk_identity_domain, dev)); 1012 - } 1013 - #endif 1014 1000 1015 1001 static int rk_iommu_attach_device(struct iommu_domain *domain, 1016 1002 struct device *dev) ··· 1045 1055 return ret; 1046 1056 } 1047 1057 1048 - static struct iommu_domain *rk_iommu_domain_alloc(unsigned type) 1058 + static struct iommu_domain *rk_iommu_domain_alloc_paging(struct device *dev) 1049 1059 { 1050 1060 struct rk_iommu_domain *rk_domain; 1051 - 1052 - if (type == IOMMU_DOMAIN_IDENTITY) 1053 - return &rk_identity_domain; 1054 - 1055 - if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA) 1056 - return NULL; 1057 1061 1058 1062 if (!dma_dev) 1059 1063 return NULL; ··· 1139 1155 device_link_del(data->link); 1140 1156 } 1141 1157 1142 - static struct iommu_group *rk_iommu_device_group(struct device *dev) 1143 - { 1144 - struct rk_iommu *iommu; 1145 - 1146 - iommu = rk_iommu_from_dev(dev); 1147 - 1148 - return iommu_group_ref_get(iommu->group); 1149 - } 1150 - 1151 1158 static int rk_iommu_of_xlate(struct device *dev, 1152 1159 struct of_phandle_args *args) 1153 1160 { ··· 1161 1186 } 1162 1187 1163 1188 static const struct iommu_ops rk_iommu_ops = { 1164 - .domain_alloc = rk_iommu_domain_alloc, 1189 + .identity_domain = &rk_identity_domain, 1190 + .domain_alloc_paging = rk_iommu_domain_alloc_paging, 1165 1191 .probe_device = rk_iommu_probe_device, 1166 1192 .release_device = rk_iommu_release_device, 1167 - .device_group = rk_iommu_device_group, 1168 - #ifdef CONFIG_ARM 1169 - .set_platform_dma_ops = rk_iommu_set_platform_dma, 1170 - #endif 1193 + .device_group = generic_single_device_group, 1171 1194 .pgsize_bitmap = RK_IOMMU_PGSIZE_BITMAP, 1172 1195 .of_xlate = rk_iommu_of_xlate, 1173 1196 .default_domain_ops = &(const struct iommu_domain_ops) { 1174 1197 .attach_dev = rk_iommu_attach_device, 1175 - .map = rk_iommu_map, 1176 - .unmap = rk_iommu_unmap, 1198 + .map_pages = rk_iommu_map, 1199 + .unmap_pages = rk_iommu_unmap, 1177 1200 .iova_to_phys = rk_iommu_iova_to_phys, 1178 1201 .free = rk_iommu_domain_free, 1179 1202 } ··· 1253 1280 if (err) 1254 1281 return err; 1255 1282 1256 - iommu->group = iommu_group_alloc(); 1257 - if (IS_ERR(iommu->group)) { 1258 - err = PTR_ERR(iommu->group); 1259 - goto err_unprepare_clocks; 1260 - } 1261 - 1262 1283 err = iommu_device_sysfs_add(&iommu->iommu, dev, NULL, dev_name(dev)); 1263 1284 if (err) 1264 - goto err_put_group; 1285 + goto err_unprepare_clocks; 1265 1286 1266 1287 err = iommu_device_register(&iommu->iommu, &rk_iommu_ops, dev); 1267 1288 if (err) ··· 1292 1325 pm_runtime_disable(dev); 1293 1326 err_remove_sysfs: 1294 1327 iommu_device_sysfs_remove(&iommu->iommu); 1295 - err_put_group: 1296 - iommu_group_put(iommu->group); 1297 1328 err_unprepare_clocks: 1298 1329 clk_bulk_unprepare(iommu->num_clocks, iommu->clocks); 1299 1330 return err;
+389 -35
drivers/iommu/s390-iommu.c
··· 14 14 #include <linux/rcupdate.h> 15 15 #include <asm/pci_dma.h> 16 16 17 + #include "dma-iommu.h" 18 + 17 19 static const struct iommu_ops s390_iommu_ops; 20 + 21 + static struct kmem_cache *dma_region_table_cache; 22 + static struct kmem_cache *dma_page_table_cache; 23 + 24 + static u64 s390_iommu_aperture; 25 + static u32 s390_iommu_aperture_factor = 1; 18 26 19 27 struct s390_domain { 20 28 struct iommu_domain domain; 21 29 struct list_head devices; 30 + struct zpci_iommu_ctrs ctrs; 22 31 unsigned long *dma_table; 23 32 spinlock_t list_lock; 24 33 struct rcu_head rcu; 25 34 }; 35 + 36 + static inline unsigned int calc_rtx(dma_addr_t ptr) 37 + { 38 + return ((unsigned long)ptr >> ZPCI_RT_SHIFT) & ZPCI_INDEX_MASK; 39 + } 40 + 41 + static inline unsigned int calc_sx(dma_addr_t ptr) 42 + { 43 + return ((unsigned long)ptr >> ZPCI_ST_SHIFT) & ZPCI_INDEX_MASK; 44 + } 45 + 46 + static inline unsigned int calc_px(dma_addr_t ptr) 47 + { 48 + return ((unsigned long)ptr >> PAGE_SHIFT) & ZPCI_PT_MASK; 49 + } 50 + 51 + static inline void set_pt_pfaa(unsigned long *entry, phys_addr_t pfaa) 52 + { 53 + *entry &= ZPCI_PTE_FLAG_MASK; 54 + *entry |= (pfaa & ZPCI_PTE_ADDR_MASK); 55 + } 56 + 57 + static inline void set_rt_sto(unsigned long *entry, phys_addr_t sto) 58 + { 59 + *entry &= ZPCI_RTE_FLAG_MASK; 60 + *entry |= (sto & ZPCI_RTE_ADDR_MASK); 61 + *entry |= ZPCI_TABLE_TYPE_RTX; 62 + } 63 + 64 + static inline void set_st_pto(unsigned long *entry, phys_addr_t pto) 65 + { 66 + *entry &= ZPCI_STE_FLAG_MASK; 67 + *entry |= (pto & ZPCI_STE_ADDR_MASK); 68 + *entry |= ZPCI_TABLE_TYPE_SX; 69 + } 70 + 71 + static inline void validate_rt_entry(unsigned long *entry) 72 + { 73 + *entry &= ~ZPCI_TABLE_VALID_MASK; 74 + *entry &= ~ZPCI_TABLE_OFFSET_MASK; 75 + *entry |= ZPCI_TABLE_VALID; 76 + *entry |= ZPCI_TABLE_LEN_RTX; 77 + } 78 + 79 + static inline void validate_st_entry(unsigned long *entry) 80 + { 81 + *entry &= ~ZPCI_TABLE_VALID_MASK; 82 + *entry |= ZPCI_TABLE_VALID; 83 + } 84 + 85 + static inline void invalidate_pt_entry(unsigned long *entry) 86 + { 87 + WARN_ON_ONCE((*entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_INVALID); 88 + *entry &= ~ZPCI_PTE_VALID_MASK; 89 + *entry |= ZPCI_PTE_INVALID; 90 + } 91 + 92 + static inline void validate_pt_entry(unsigned long *entry) 93 + { 94 + WARN_ON_ONCE((*entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID); 95 + *entry &= ~ZPCI_PTE_VALID_MASK; 96 + *entry |= ZPCI_PTE_VALID; 97 + } 98 + 99 + static inline void entry_set_protected(unsigned long *entry) 100 + { 101 + *entry &= ~ZPCI_TABLE_PROT_MASK; 102 + *entry |= ZPCI_TABLE_PROTECTED; 103 + } 104 + 105 + static inline void entry_clr_protected(unsigned long *entry) 106 + { 107 + *entry &= ~ZPCI_TABLE_PROT_MASK; 108 + *entry |= ZPCI_TABLE_UNPROTECTED; 109 + } 110 + 111 + static inline int reg_entry_isvalid(unsigned long entry) 112 + { 113 + return (entry & ZPCI_TABLE_VALID_MASK) == ZPCI_TABLE_VALID; 114 + } 115 + 116 + static inline int pt_entry_isvalid(unsigned long entry) 117 + { 118 + return (entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID; 119 + } 120 + 121 + static inline unsigned long *get_rt_sto(unsigned long entry) 122 + { 123 + if ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_RTX) 124 + return phys_to_virt(entry & ZPCI_RTE_ADDR_MASK); 125 + else 126 + return NULL; 127 + } 128 + 129 + static inline unsigned long *get_st_pto(unsigned long entry) 130 + { 131 + if ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_SX) 132 + return phys_to_virt(entry & ZPCI_STE_ADDR_MASK); 133 + else 134 + return NULL; 135 + } 136 + 137 + static int __init dma_alloc_cpu_table_caches(void) 138 + { 139 + dma_region_table_cache = kmem_cache_create("PCI_DMA_region_tables", 140 + ZPCI_TABLE_SIZE, 141 + ZPCI_TABLE_ALIGN, 142 + 0, NULL); 143 + if (!dma_region_table_cache) 144 + return -ENOMEM; 145 + 146 + dma_page_table_cache = kmem_cache_create("PCI_DMA_page_tables", 147 + ZPCI_PT_SIZE, 148 + ZPCI_PT_ALIGN, 149 + 0, NULL); 150 + if (!dma_page_table_cache) { 151 + kmem_cache_destroy(dma_region_table_cache); 152 + return -ENOMEM; 153 + } 154 + return 0; 155 + } 156 + 157 + static unsigned long *dma_alloc_cpu_table(gfp_t gfp) 158 + { 159 + unsigned long *table, *entry; 160 + 161 + table = kmem_cache_alloc(dma_region_table_cache, gfp); 162 + if (!table) 163 + return NULL; 164 + 165 + for (entry = table; entry < table + ZPCI_TABLE_ENTRIES; entry++) 166 + *entry = ZPCI_TABLE_INVALID; 167 + return table; 168 + } 169 + 170 + static void dma_free_cpu_table(void *table) 171 + { 172 + kmem_cache_free(dma_region_table_cache, table); 173 + } 174 + 175 + static void dma_free_page_table(void *table) 176 + { 177 + kmem_cache_free(dma_page_table_cache, table); 178 + } 179 + 180 + static void dma_free_seg_table(unsigned long entry) 181 + { 182 + unsigned long *sto = get_rt_sto(entry); 183 + int sx; 184 + 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])); 188 + 189 + dma_free_cpu_table(sto); 190 + } 191 + 192 + static void dma_cleanup_tables(unsigned long *table) 193 + { 194 + int rtx; 195 + 196 + if (!table) 197 + return; 198 + 199 + for (rtx = 0; rtx < ZPCI_TABLE_ENTRIES; rtx++) 200 + if (reg_entry_isvalid(table[rtx])) 201 + dma_free_seg_table(table[rtx]); 202 + 203 + dma_free_cpu_table(table); 204 + } 205 + 206 + static unsigned long *dma_alloc_page_table(gfp_t gfp) 207 + { 208 + unsigned long *table, *entry; 209 + 210 + table = kmem_cache_alloc(dma_page_table_cache, gfp); 211 + if (!table) 212 + return NULL; 213 + 214 + for (entry = table; entry < table + ZPCI_PT_ENTRIES; entry++) 215 + *entry = ZPCI_PTE_INVALID; 216 + return table; 217 + } 218 + 219 + static unsigned long *dma_get_seg_table_origin(unsigned long *rtep, gfp_t gfp) 220 + { 221 + unsigned long old_rte, rte; 222 + unsigned long *sto; 223 + 224 + rte = READ_ONCE(*rtep); 225 + if (reg_entry_isvalid(rte)) { 226 + sto = get_rt_sto(rte); 227 + } else { 228 + sto = dma_alloc_cpu_table(gfp); 229 + if (!sto) 230 + return NULL; 231 + 232 + set_rt_sto(&rte, virt_to_phys(sto)); 233 + validate_rt_entry(&rte); 234 + entry_clr_protected(&rte); 235 + 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); 241 + } 242 + } 243 + return sto; 244 + } 245 + 246 + static unsigned long *dma_get_page_table_origin(unsigned long *step, gfp_t gfp) 247 + { 248 + unsigned long old_ste, ste; 249 + unsigned long *pto; 250 + 251 + ste = READ_ONCE(*step); 252 + if (reg_entry_isvalid(ste)) { 253 + pto = get_st_pto(ste); 254 + } else { 255 + pto = dma_alloc_page_table(gfp); 256 + if (!pto) 257 + return NULL; 258 + set_st_pto(&ste, virt_to_phys(pto)); 259 + validate_st_entry(&ste); 260 + entry_clr_protected(&ste); 261 + 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); 267 + } 268 + } 269 + return pto; 270 + } 271 + 272 + static unsigned long *dma_walk_cpu_trans(unsigned long *rto, dma_addr_t dma_addr, gfp_t gfp) 273 + { 274 + unsigned long *sto, *pto; 275 + unsigned int rtx, sx, px; 276 + 277 + rtx = calc_rtx(dma_addr); 278 + sto = dma_get_seg_table_origin(&rto[rtx], gfp); 279 + if (!sto) 280 + return NULL; 281 + 282 + sx = calc_sx(dma_addr); 283 + pto = dma_get_page_table_origin(&sto[sx], gfp); 284 + if (!pto) 285 + return NULL; 286 + 287 + px = calc_px(dma_addr); 288 + return &pto[px]; 289 + } 290 + 291 + static void dma_update_cpu_trans(unsigned long *ptep, phys_addr_t page_addr, int flags) 292 + { 293 + unsigned long pte; 294 + 295 + pte = READ_ONCE(*ptep); 296 + if (flags & ZPCI_PTE_INVALID) { 297 + invalidate_pt_entry(&pte); 298 + } else { 299 + set_pt_pfaa(&pte, page_addr); 300 + validate_pt_entry(&pte); 301 + } 302 + 303 + if (flags & ZPCI_TABLE_PROTECTED) 304 + entry_set_protected(&pte); 305 + else 306 + entry_clr_protected(&pte); 307 + 308 + xchg(ptep, pte); 309 + } 26 310 27 311 static struct s390_domain *to_s390_domain(struct iommu_domain *dom) 28 312 { ··· 315 31 316 32 static bool s390_iommu_capable(struct device *dev, enum iommu_cap cap) 317 33 { 34 + struct zpci_dev *zdev = to_zpci_dev(dev); 35 + 318 36 switch (cap) { 319 37 case IOMMU_CAP_CACHE_COHERENCY: 320 38 return true; 39 + case IOMMU_CAP_DEFERRED_FLUSH: 40 + return zdev->pft != PCI_FUNC_TYPE_ISM; 321 41 default: 322 42 return false; 323 43 } 324 44 } 325 45 326 - static struct iommu_domain *s390_domain_alloc(unsigned domain_type) 46 + static struct iommu_domain *s390_domain_alloc_paging(struct device *dev) 327 47 { 328 48 struct s390_domain *s390_domain; 329 - 330 - if (domain_type != IOMMU_DOMAIN_UNMANAGED) 331 - return NULL; 332 49 333 50 s390_domain = kzalloc(sizeof(*s390_domain), GFP_KERNEL); 334 51 if (!s390_domain) ··· 369 84 call_rcu(&s390_domain->rcu, s390_iommu_rcu_free_domain); 370 85 } 371 86 372 - static void __s390_iommu_detach_device(struct zpci_dev *zdev) 87 + static void s390_iommu_detach_device(struct iommu_domain *domain, 88 + struct device *dev) 373 89 { 374 - struct s390_domain *s390_domain = zdev->s390_domain; 90 + struct s390_domain *s390_domain = to_s390_domain(domain); 91 + struct zpci_dev *zdev = to_zpci_dev(dev); 375 92 unsigned long flags; 376 - 377 - if (!s390_domain) 378 - return; 379 93 380 94 spin_lock_irqsave(&s390_domain->list_lock, flags); 381 95 list_del_rcu(&zdev->iommu_list); ··· 402 118 return -EINVAL; 403 119 404 120 if (zdev->s390_domain) 405 - __s390_iommu_detach_device(zdev); 406 - else if (zdev->dma_table) 407 - zpci_dma_exit_device(zdev); 121 + s390_iommu_detach_device(&zdev->s390_domain->domain, dev); 408 122 409 123 cc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, 410 124 virt_to_phys(s390_domain->dma_table), &status); ··· 412 130 */ 413 131 if (cc && status != ZPCI_PCI_ST_FUNC_NOT_AVAIL) 414 132 return -EIO; 415 - zdev->dma_table = s390_domain->dma_table; 416 133 417 134 zdev->dma_table = s390_domain->dma_table; 418 135 zdev->s390_domain = s390_domain; ··· 421 140 spin_unlock_irqrestore(&s390_domain->list_lock, flags); 422 141 423 142 return 0; 424 - } 425 - 426 - static void s390_iommu_set_platform_dma(struct device *dev) 427 - { 428 - struct zpci_dev *zdev = to_zpci_dev(dev); 429 - 430 - __s390_iommu_detach_device(zdev); 431 - zpci_dma_init_device(zdev); 432 143 } 433 144 434 145 static void s390_iommu_get_resv_regions(struct device *dev, ··· 463 190 if (zdev->end_dma > ZPCI_TABLE_SIZE_RT - 1) 464 191 zdev->end_dma = ZPCI_TABLE_SIZE_RT - 1; 465 192 193 + if (zdev->tlb_refresh) 194 + dev->iommu->shadow_on_flush = 1; 195 + 466 196 return &zdev->iommu_dev; 467 197 } 468 198 ··· 478 202 * to the device, but keep it attached to other devices in the group. 479 203 */ 480 204 if (zdev) 481 - __s390_iommu_detach_device(zdev); 205 + s390_iommu_detach_device(&zdev->s390_domain->domain, dev); 206 + } 207 + 208 + static int zpci_refresh_all(struct zpci_dev *zdev) 209 + { 210 + return zpci_refresh_trans((u64)zdev->fh << 32, zdev->start_dma, 211 + zdev->end_dma - zdev->start_dma + 1); 482 212 } 483 213 484 214 static void s390_iommu_flush_iotlb_all(struct iommu_domain *domain) ··· 494 212 495 213 rcu_read_lock(); 496 214 list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) { 497 - zpci_refresh_trans((u64)zdev->fh << 32, zdev->start_dma, 498 - zdev->end_dma - zdev->start_dma + 1); 215 + atomic64_inc(&s390_domain->ctrs.global_rpcits); 216 + zpci_refresh_all(zdev); 499 217 } 500 218 rcu_read_unlock(); 501 219 } ··· 513 231 514 232 rcu_read_lock(); 515 233 list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) { 234 + atomic64_inc(&s390_domain->ctrs.sync_rpcits); 516 235 zpci_refresh_trans((u64)zdev->fh << 32, gather->start, 517 236 size); 518 237 } 519 238 rcu_read_unlock(); 520 239 } 521 240 522 - static void s390_iommu_iotlb_sync_map(struct iommu_domain *domain, 523 - unsigned long iova, size_t size) 241 + static int s390_iommu_iotlb_sync_map(struct iommu_domain *domain, 242 + unsigned long iova, size_t size) 524 243 { 525 244 struct s390_domain *s390_domain = to_s390_domain(domain); 526 245 struct zpci_dev *zdev; 246 + int ret = 0; 527 247 528 248 rcu_read_lock(); 529 249 list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) { 530 250 if (!zdev->tlb_refresh) 531 251 continue; 532 - zpci_refresh_trans((u64)zdev->fh << 32, 533 - iova, size); 252 + atomic64_inc(&s390_domain->ctrs.sync_map_rpcits); 253 + ret = zpci_refresh_trans((u64)zdev->fh << 32, 254 + iova, size); 255 + /* 256 + * let the hypervisor discover invalidated entries 257 + * allowing it to free IOVAs and unpin pages 258 + */ 259 + if (ret == -ENOMEM) { 260 + ret = zpci_refresh_all(zdev); 261 + if (ret) 262 + break; 263 + } 534 264 } 535 265 rcu_read_unlock(); 266 + 267 + return ret; 536 268 } 537 269 538 270 static int s390_iommu_validate_trans(struct s390_domain *s390_domain, ··· 626 330 if (!IS_ALIGNED(iova | paddr, pgsize)) 627 331 return -EINVAL; 628 332 629 - if (!(prot & IOMMU_READ)) 630 - return -EINVAL; 631 - 632 333 if (!(prot & IOMMU_WRITE)) 633 334 flags |= ZPCI_TABLE_PROTECTED; 634 335 635 336 rc = s390_iommu_validate_trans(s390_domain, paddr, iova, 636 - pgcount, flags, gfp); 637 - if (!rc) 337 + pgcount, flags, gfp); 338 + if (!rc) { 638 339 *mapped = size; 340 + atomic64_add(pgcount, &s390_domain->ctrs.mapped_pages); 341 + } 639 342 640 343 return rc; 641 344 } ··· 690 395 return 0; 691 396 692 397 iommu_iotlb_gather_add_range(gather, iova, size); 398 + atomic64_add(pgcount, &s390_domain->ctrs.unmapped_pages); 693 399 694 400 return size; 695 401 } 696 402 403 + static void s390_iommu_probe_finalize(struct device *dev) 404 + { 405 + iommu_setup_dma_ops(dev, 0, U64_MAX); 406 + } 407 + 408 + struct zpci_iommu_ctrs *zpci_get_iommu_ctrs(struct zpci_dev *zdev) 409 + { 410 + if (!zdev || !zdev->s390_domain) 411 + return NULL; 412 + return &zdev->s390_domain->ctrs; 413 + } 414 + 697 415 int zpci_init_iommu(struct zpci_dev *zdev) 698 416 { 417 + u64 aperture_size; 699 418 int rc = 0; 700 419 701 420 rc = iommu_device_sysfs_add(&zdev->iommu_dev, NULL, NULL, ··· 720 411 rc = iommu_device_register(&zdev->iommu_dev, &s390_iommu_ops, NULL); 721 412 if (rc) 722 413 goto out_sysfs; 414 + 415 + zdev->start_dma = PAGE_ALIGN(zdev->start_dma); 416 + aperture_size = min3(s390_iommu_aperture, 417 + ZPCI_TABLE_SIZE_RT - zdev->start_dma, 418 + zdev->end_dma - zdev->start_dma + 1); 419 + zdev->end_dma = zdev->start_dma + aperture_size - 1; 723 420 724 421 return 0; 725 422 ··· 742 427 iommu_device_sysfs_remove(&zdev->iommu_dev); 743 428 } 744 429 430 + static int __init s390_iommu_setup(char *str) 431 + { 432 + if (!strcmp(str, "strict")) { 433 + pr_warn("s390_iommu=strict deprecated; use iommu.strict=1 instead\n"); 434 + iommu_set_dma_strict(); 435 + } 436 + return 1; 437 + } 438 + 439 + __setup("s390_iommu=", s390_iommu_setup); 440 + 441 + static int __init s390_iommu_aperture_setup(char *str) 442 + { 443 + if (kstrtou32(str, 10, &s390_iommu_aperture_factor)) 444 + s390_iommu_aperture_factor = 1; 445 + return 1; 446 + } 447 + 448 + __setup("s390_iommu_aperture=", s390_iommu_aperture_setup); 449 + 450 + static int __init s390_iommu_init(void) 451 + { 452 + int rc; 453 + 454 + iommu_dma_forcedac = true; 455 + s390_iommu_aperture = (u64)virt_to_phys(high_memory); 456 + if (!s390_iommu_aperture_factor) 457 + s390_iommu_aperture = ULONG_MAX; 458 + else 459 + s390_iommu_aperture *= s390_iommu_aperture_factor; 460 + 461 + rc = dma_alloc_cpu_table_caches(); 462 + if (rc) 463 + return rc; 464 + 465 + return rc; 466 + } 467 + subsys_initcall(s390_iommu_init); 468 + 745 469 static const struct iommu_ops s390_iommu_ops = { 746 470 .capable = s390_iommu_capable, 747 - .domain_alloc = s390_domain_alloc, 471 + .domain_alloc_paging = s390_domain_alloc_paging, 748 472 .probe_device = s390_iommu_probe_device, 473 + .probe_finalize = s390_iommu_probe_finalize, 749 474 .release_device = s390_iommu_release_device, 750 475 .device_group = generic_device_group, 751 - .set_platform_dma_ops = s390_iommu_set_platform_dma, 752 476 .pgsize_bitmap = SZ_4K, 753 477 .get_resv_regions = s390_iommu_get_resv_regions, 754 478 .default_domain_ops = &(const struct iommu_domain_ops) {
+7 -29
drivers/iommu/sprd-iommu.c
··· 70 70 void __iomem *base; 71 71 struct device *dev; 72 72 struct iommu_device iommu; 73 - struct iommu_group *group; 74 73 struct clk *eb; 75 74 }; 76 75 ··· 133 134 SPRD_IOMMU_PAGE_SHIFT) * sizeof(u32); 134 135 } 135 136 136 - static struct iommu_domain *sprd_iommu_domain_alloc(unsigned int domain_type) 137 + static struct iommu_domain *sprd_iommu_domain_alloc_paging(struct device *dev) 137 138 { 138 139 struct sprd_iommu_domain *dom; 139 - 140 - if (domain_type != IOMMU_DOMAIN_DMA && domain_type != IOMMU_DOMAIN_UNMANAGED) 141 - return NULL; 142 140 143 141 dom = kzalloc(sizeof(*dom), GFP_KERNEL); 144 142 if (!dom) ··· 341 345 return size; 342 346 } 343 347 344 - static void sprd_iommu_sync_map(struct iommu_domain *domain, 345 - unsigned long iova, size_t size) 348 + static int sprd_iommu_sync_map(struct iommu_domain *domain, 349 + unsigned long iova, size_t size) 346 350 { 347 351 struct sprd_iommu_domain *dom = to_sprd_domain(domain); 348 352 unsigned int reg; ··· 354 358 355 359 /* clear IOMMU TLB buffer after page table updated */ 356 360 sprd_iommu_write(dom->sdev, reg, 0xffffffff); 361 + return 0; 357 362 } 358 363 359 364 static void sprd_iommu_sync(struct iommu_domain *domain, ··· 396 399 return &sdev->iommu; 397 400 } 398 401 399 - static struct iommu_group *sprd_iommu_device_group(struct device *dev) 400 - { 401 - struct sprd_iommu_device *sdev = dev_iommu_priv_get(dev); 402 - 403 - return iommu_group_ref_get(sdev->group); 404 - } 405 - 406 402 static int sprd_iommu_of_xlate(struct device *dev, struct of_phandle_args *args) 407 403 { 408 404 struct platform_device *pdev; ··· 411 421 412 422 413 423 static const struct iommu_ops sprd_iommu_ops = { 414 - .domain_alloc = sprd_iommu_domain_alloc, 424 + .domain_alloc_paging = sprd_iommu_domain_alloc_paging, 415 425 .probe_device = sprd_iommu_probe_device, 416 - .device_group = sprd_iommu_device_group, 426 + .device_group = generic_single_device_group, 417 427 .of_xlate = sprd_iommu_of_xlate, 418 428 .pgsize_bitmap = SPRD_IOMMU_PAGE_SIZE, 419 429 .owner = THIS_MODULE, ··· 486 496 platform_set_drvdata(pdev, sdev); 487 497 sdev->dev = dev; 488 498 489 - /* All the client devices are in the same iommu-group */ 490 - sdev->group = iommu_group_alloc(); 491 - if (IS_ERR(sdev->group)) { 492 - ret = PTR_ERR(sdev->group); 493 - goto free_page; 494 - } 495 - 496 499 ret = iommu_device_sysfs_add(&sdev->iommu, dev, NULL, dev_name(dev)); 497 500 if (ret) 498 - goto put_group; 501 + goto free_page; 499 502 500 503 ret = iommu_device_register(&sdev->iommu, &sprd_iommu_ops, dev); 501 504 if (ret) ··· 513 530 iommu_device_unregister(&sdev->iommu); 514 531 remove_sysfs: 515 532 iommu_device_sysfs_remove(&sdev->iommu); 516 - put_group: 517 - iommu_group_put(sdev->group); 518 533 free_page: 519 534 dma_free_coherent(sdev->dev, SPRD_IOMMU_PAGE_SIZE, sdev->prot_page_va, sdev->prot_page_pa); 520 535 return ret; ··· 523 542 struct sprd_iommu_device *sdev = platform_get_drvdata(pdev); 524 543 525 544 dma_free_coherent(sdev->dev, SPRD_IOMMU_PAGE_SIZE, sdev->prot_page_va, sdev->prot_page_pa); 526 - 527 - iommu_group_put(sdev->group); 528 - sdev->group = NULL; 529 545 530 546 platform_set_drvdata(pdev, NULL); 531 547 iommu_device_sysfs_remove(&sdev->iommu);
+38 -42
drivers/iommu/sun50i-iommu.c
··· 107 107 struct clk *clk; 108 108 109 109 struct iommu_domain *domain; 110 - struct iommu_group *group; 111 110 struct kmem_cache *pt_pool; 112 111 }; 113 112 ··· 401 402 spin_unlock_irqrestore(&iommu->iommu_lock, flags); 402 403 } 403 404 404 - static void sun50i_iommu_iotlb_sync_map(struct iommu_domain *domain, 405 - unsigned long iova, size_t size) 405 + static int sun50i_iommu_iotlb_sync_map(struct iommu_domain *domain, 406 + unsigned long iova, size_t size) 406 407 { 407 408 struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain); 408 409 struct sun50i_iommu *iommu = sun50i_domain->iommu; ··· 411 412 spin_lock_irqsave(&iommu->iommu_lock, flags); 412 413 sun50i_iommu_zap_range(iommu, iova, size); 413 414 spin_unlock_irqrestore(&iommu->iommu_lock, flags); 415 + 416 + return 0; 414 417 } 415 418 416 419 static void sun50i_iommu_iotlb_sync(struct iommu_domain *domain, ··· 590 589 } 591 590 592 591 static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova, 593 - phys_addr_t paddr, size_t size, int prot, gfp_t gfp) 592 + phys_addr_t paddr, size_t size, size_t count, 593 + int prot, gfp_t gfp, size_t *mapped) 594 594 { 595 595 struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain); 596 596 struct sun50i_iommu *iommu = sun50i_domain->iommu; ··· 618 616 619 617 *pte_addr = sun50i_mk_pte(paddr, prot); 620 618 sun50i_table_flush(sun50i_domain, pte_addr, 1); 619 + *mapped = size; 621 620 622 621 out: 623 622 return ret; 624 623 } 625 624 626 625 static size_t sun50i_iommu_unmap(struct iommu_domain *domain, unsigned long iova, 627 - size_t size, struct iommu_iotlb_gather *gather) 626 + size_t size, size_t count, struct iommu_iotlb_gather *gather) 628 627 { 629 628 struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain); 630 629 phys_addr_t pt_phys; ··· 670 667 sun50i_iova_get_page_offset(iova); 671 668 } 672 669 673 - static struct iommu_domain *sun50i_iommu_domain_alloc(unsigned type) 670 + static struct iommu_domain * 671 + sun50i_iommu_domain_alloc_paging(struct device *dev) 674 672 { 675 673 struct sun50i_iommu_domain *sun50i_domain; 676 - 677 - if (type != IOMMU_DOMAIN_DMA && 678 - type != IOMMU_DOMAIN_UNMANAGED) 679 - return NULL; 680 674 681 675 sun50i_domain = kzalloc(sizeof(*sun50i_domain), GFP_KERNEL); 682 676 if (!sun50i_domain) ··· 757 757 iommu->domain = NULL; 758 758 } 759 759 760 - static void sun50i_iommu_detach_device(struct iommu_domain *domain, 761 - struct device *dev) 760 + static int sun50i_iommu_identity_attach(struct iommu_domain *identity_domain, 761 + struct device *dev) 762 762 { 763 - struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain); 764 763 struct sun50i_iommu *iommu = dev_iommu_priv_get(dev); 764 + struct sun50i_iommu_domain *sun50i_domain; 765 765 766 766 dev_dbg(dev, "Detaching from IOMMU domain\n"); 767 767 768 - if (iommu->domain != domain) 769 - return; 768 + if (iommu->domain == identity_domain) 769 + return 0; 770 770 771 + sun50i_domain = to_sun50i_domain(iommu->domain); 771 772 if (refcount_dec_and_test(&sun50i_domain->refcnt)) 772 773 sun50i_iommu_detach_domain(iommu, sun50i_domain); 774 + return 0; 773 775 } 776 + 777 + static struct iommu_domain_ops sun50i_iommu_identity_ops = { 778 + .attach_dev = sun50i_iommu_identity_attach, 779 + }; 780 + 781 + static struct iommu_domain sun50i_iommu_identity_domain = { 782 + .type = IOMMU_DOMAIN_IDENTITY, 783 + .ops = &sun50i_iommu_identity_ops, 784 + }; 774 785 775 786 static int sun50i_iommu_attach_device(struct iommu_domain *domain, 776 787 struct device *dev) ··· 800 789 if (iommu->domain == domain) 801 790 return 0; 802 791 803 - if (iommu->domain) 804 - sun50i_iommu_detach_device(iommu->domain, dev); 792 + sun50i_iommu_identity_attach(&sun50i_iommu_identity_domain, dev); 805 793 806 794 sun50i_iommu_attach_domain(iommu, sun50i_domain); 807 795 ··· 818 808 return &iommu->iommu; 819 809 } 820 810 821 - static struct iommu_group *sun50i_iommu_device_group(struct device *dev) 822 - { 823 - struct sun50i_iommu *iommu = sun50i_iommu_from_dev(dev); 824 - 825 - return iommu_group_ref_get(iommu->group); 826 - } 827 - 828 811 static int sun50i_iommu_of_xlate(struct device *dev, 829 812 struct of_phandle_args *args) 830 813 { ··· 830 827 } 831 828 832 829 static const struct iommu_ops sun50i_iommu_ops = { 830 + .identity_domain = &sun50i_iommu_identity_domain, 833 831 .pgsize_bitmap = SZ_4K, 834 - .device_group = sun50i_iommu_device_group, 835 - .domain_alloc = sun50i_iommu_domain_alloc, 832 + .device_group = generic_single_device_group, 833 + .domain_alloc_paging = sun50i_iommu_domain_alloc_paging, 836 834 .of_xlate = sun50i_iommu_of_xlate, 837 835 .probe_device = sun50i_iommu_probe_device, 838 836 .default_domain_ops = &(const struct iommu_domain_ops) { ··· 842 838 .iotlb_sync_map = sun50i_iommu_iotlb_sync_map, 843 839 .iotlb_sync = sun50i_iommu_iotlb_sync, 844 840 .iova_to_phys = sun50i_iommu_iova_to_phys, 845 - .map = sun50i_iommu_map, 846 - .unmap = sun50i_iommu_unmap, 841 + .map_pages = sun50i_iommu_map, 842 + .unmap_pages = sun50i_iommu_unmap, 847 843 .free = sun50i_iommu_domain_free, 848 844 } 849 845 }; ··· 989 985 if (!iommu) 990 986 return -ENOMEM; 991 987 spin_lock_init(&iommu->iommu_lock); 988 + iommu->domain = &sun50i_iommu_identity_domain; 992 989 platform_set_drvdata(pdev, iommu); 993 990 iommu->dev = &pdev->dev; 994 991 ··· 1000 995 if (!iommu->pt_pool) 1001 996 return -ENOMEM; 1002 997 1003 - iommu->group = iommu_group_alloc(); 1004 - if (IS_ERR(iommu->group)) { 1005 - ret = PTR_ERR(iommu->group); 1006 - goto err_free_cache; 1007 - } 1008 - 1009 998 iommu->base = devm_platform_ioremap_resource(pdev, 0); 1010 999 if (IS_ERR(iommu->base)) { 1011 1000 ret = PTR_ERR(iommu->base); 1012 - goto err_free_group; 1001 + goto err_free_cache; 1013 1002 } 1014 1003 1015 1004 irq = platform_get_irq(pdev, 0); 1016 1005 if (irq < 0) { 1017 1006 ret = irq; 1018 - goto err_free_group; 1007 + goto err_free_cache; 1019 1008 } 1020 1009 1021 1010 iommu->clk = devm_clk_get(&pdev->dev, NULL); 1022 1011 if (IS_ERR(iommu->clk)) { 1023 1012 dev_err(&pdev->dev, "Couldn't get our clock.\n"); 1024 1013 ret = PTR_ERR(iommu->clk); 1025 - goto err_free_group; 1014 + goto err_free_cache; 1026 1015 } 1027 1016 1028 1017 iommu->reset = devm_reset_control_get(&pdev->dev, NULL); 1029 1018 if (IS_ERR(iommu->reset)) { 1030 1019 dev_err(&pdev->dev, "Couldn't get our reset line.\n"); 1031 1020 ret = PTR_ERR(iommu->reset); 1032 - goto err_free_group; 1021 + goto err_free_cache; 1033 1022 } 1034 1023 1035 1024 ret = iommu_device_sysfs_add(&iommu->iommu, &pdev->dev, 1036 1025 NULL, dev_name(&pdev->dev)); 1037 1026 if (ret) 1038 - goto err_free_group; 1027 + goto err_free_cache; 1039 1028 1040 1029 ret = iommu_device_register(&iommu->iommu, &sun50i_iommu_ops, &pdev->dev); 1041 1030 if (ret) ··· 1047 1048 1048 1049 err_remove_sysfs: 1049 1050 iommu_device_sysfs_remove(&iommu->iommu); 1050 - 1051 - err_free_group: 1052 - iommu_group_put(iommu->group); 1053 1051 1054 1052 err_free_cache: 1055 1053 kmem_cache_destroy(iommu->pt_pool);
-371
drivers/iommu/tegra-gart.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-only 2 - /* 3 - * IOMMU API for Graphics Address Relocation Table on Tegra20 4 - * 5 - * Copyright (c) 2010-2012, NVIDIA CORPORATION. All rights reserved. 6 - * 7 - * Author: Hiroshi DOYU <hdoyu@nvidia.com> 8 - */ 9 - 10 - #define dev_fmt(fmt) "gart: " fmt 11 - 12 - #include <linux/io.h> 13 - #include <linux/iommu.h> 14 - #include <linux/moduleparam.h> 15 - #include <linux/platform_device.h> 16 - #include <linux/slab.h> 17 - #include <linux/spinlock.h> 18 - #include <linux/vmalloc.h> 19 - 20 - #include <soc/tegra/mc.h> 21 - 22 - #define GART_REG_BASE 0x24 23 - #define GART_CONFIG (0x24 - GART_REG_BASE) 24 - #define GART_ENTRY_ADDR (0x28 - GART_REG_BASE) 25 - #define GART_ENTRY_DATA (0x2c - GART_REG_BASE) 26 - 27 - #define GART_ENTRY_PHYS_ADDR_VALID BIT(31) 28 - 29 - #define GART_PAGE_SHIFT 12 30 - #define GART_PAGE_SIZE (1 << GART_PAGE_SHIFT) 31 - #define GART_PAGE_MASK GENMASK(30, GART_PAGE_SHIFT) 32 - 33 - /* bitmap of the page sizes currently supported */ 34 - #define GART_IOMMU_PGSIZES (GART_PAGE_SIZE) 35 - 36 - struct gart_device { 37 - void __iomem *regs; 38 - u32 *savedata; 39 - unsigned long iovmm_base; /* offset to vmm_area start */ 40 - unsigned long iovmm_end; /* offset to vmm_area end */ 41 - spinlock_t pte_lock; /* for pagetable */ 42 - spinlock_t dom_lock; /* for active domain */ 43 - unsigned int active_devices; /* number of active devices */ 44 - struct iommu_domain *active_domain; /* current active domain */ 45 - struct iommu_device iommu; /* IOMMU Core handle */ 46 - struct device *dev; 47 - }; 48 - 49 - static struct gart_device *gart_handle; /* unique for a system */ 50 - 51 - static bool gart_debug; 52 - 53 - /* 54 - * Any interaction between any block on PPSB and a block on APB or AHB 55 - * must have these read-back to ensure the APB/AHB bus transaction is 56 - * complete before initiating activity on the PPSB block. 57 - */ 58 - #define FLUSH_GART_REGS(gart) readl_relaxed((gart)->regs + GART_CONFIG) 59 - 60 - #define for_each_gart_pte(gart, iova) \ 61 - for (iova = gart->iovmm_base; \ 62 - iova < gart->iovmm_end; \ 63 - iova += GART_PAGE_SIZE) 64 - 65 - static inline void gart_set_pte(struct gart_device *gart, 66 - unsigned long iova, unsigned long pte) 67 - { 68 - writel_relaxed(iova, gart->regs + GART_ENTRY_ADDR); 69 - writel_relaxed(pte, gart->regs + GART_ENTRY_DATA); 70 - } 71 - 72 - static inline unsigned long gart_read_pte(struct gart_device *gart, 73 - unsigned long iova) 74 - { 75 - unsigned long pte; 76 - 77 - writel_relaxed(iova, gart->regs + GART_ENTRY_ADDR); 78 - pte = readl_relaxed(gart->regs + GART_ENTRY_DATA); 79 - 80 - return pte; 81 - } 82 - 83 - static void do_gart_setup(struct gart_device *gart, const u32 *data) 84 - { 85 - unsigned long iova; 86 - 87 - for_each_gart_pte(gart, iova) 88 - gart_set_pte(gart, iova, data ? *(data++) : 0); 89 - 90 - writel_relaxed(1, gart->regs + GART_CONFIG); 91 - FLUSH_GART_REGS(gart); 92 - } 93 - 94 - static inline bool gart_iova_range_invalid(struct gart_device *gart, 95 - unsigned long iova, size_t bytes) 96 - { 97 - return unlikely(iova < gart->iovmm_base || bytes != GART_PAGE_SIZE || 98 - iova + bytes > gart->iovmm_end); 99 - } 100 - 101 - static inline bool gart_pte_valid(struct gart_device *gart, unsigned long iova) 102 - { 103 - return !!(gart_read_pte(gart, iova) & GART_ENTRY_PHYS_ADDR_VALID); 104 - } 105 - 106 - static int gart_iommu_attach_dev(struct iommu_domain *domain, 107 - struct device *dev) 108 - { 109 - struct gart_device *gart = gart_handle; 110 - int ret = 0; 111 - 112 - spin_lock(&gart->dom_lock); 113 - 114 - if (gart->active_domain && gart->active_domain != domain) { 115 - ret = -EINVAL; 116 - } else if (dev_iommu_priv_get(dev) != domain) { 117 - dev_iommu_priv_set(dev, domain); 118 - gart->active_domain = domain; 119 - gart->active_devices++; 120 - } 121 - 122 - spin_unlock(&gart->dom_lock); 123 - 124 - return ret; 125 - } 126 - 127 - static void gart_iommu_set_platform_dma(struct device *dev) 128 - { 129 - struct iommu_domain *domain = iommu_get_domain_for_dev(dev); 130 - struct gart_device *gart = gart_handle; 131 - 132 - spin_lock(&gart->dom_lock); 133 - 134 - if (dev_iommu_priv_get(dev) == domain) { 135 - dev_iommu_priv_set(dev, NULL); 136 - 137 - if (--gart->active_devices == 0) 138 - gart->active_domain = NULL; 139 - } 140 - 141 - spin_unlock(&gart->dom_lock); 142 - } 143 - 144 - static struct iommu_domain *gart_iommu_domain_alloc(unsigned type) 145 - { 146 - struct iommu_domain *domain; 147 - 148 - if (type != IOMMU_DOMAIN_UNMANAGED) 149 - return NULL; 150 - 151 - domain = kzalloc(sizeof(*domain), GFP_KERNEL); 152 - if (domain) { 153 - domain->geometry.aperture_start = gart_handle->iovmm_base; 154 - domain->geometry.aperture_end = gart_handle->iovmm_end - 1; 155 - domain->geometry.force_aperture = true; 156 - } 157 - 158 - return domain; 159 - } 160 - 161 - static void gart_iommu_domain_free(struct iommu_domain *domain) 162 - { 163 - WARN_ON(gart_handle->active_domain == domain); 164 - kfree(domain); 165 - } 166 - 167 - static inline int __gart_iommu_map(struct gart_device *gart, unsigned long iova, 168 - unsigned long pa) 169 - { 170 - if (unlikely(gart_debug && gart_pte_valid(gart, iova))) { 171 - dev_err(gart->dev, "Page entry is in-use\n"); 172 - return -EINVAL; 173 - } 174 - 175 - gart_set_pte(gart, iova, GART_ENTRY_PHYS_ADDR_VALID | pa); 176 - 177 - return 0; 178 - } 179 - 180 - static int gart_iommu_map(struct iommu_domain *domain, unsigned long iova, 181 - phys_addr_t pa, size_t bytes, int prot, gfp_t gfp) 182 - { 183 - struct gart_device *gart = gart_handle; 184 - int ret; 185 - 186 - if (gart_iova_range_invalid(gart, iova, bytes)) 187 - return -EINVAL; 188 - 189 - spin_lock(&gart->pte_lock); 190 - ret = __gart_iommu_map(gart, iova, (unsigned long)pa); 191 - spin_unlock(&gart->pte_lock); 192 - 193 - return ret; 194 - } 195 - 196 - static inline int __gart_iommu_unmap(struct gart_device *gart, 197 - unsigned long iova) 198 - { 199 - if (unlikely(gart_debug && !gart_pte_valid(gart, iova))) { 200 - dev_err(gart->dev, "Page entry is invalid\n"); 201 - return -EINVAL; 202 - } 203 - 204 - gart_set_pte(gart, iova, 0); 205 - 206 - return 0; 207 - } 208 - 209 - static size_t gart_iommu_unmap(struct iommu_domain *domain, unsigned long iova, 210 - size_t bytes, struct iommu_iotlb_gather *gather) 211 - { 212 - struct gart_device *gart = gart_handle; 213 - int err; 214 - 215 - if (gart_iova_range_invalid(gart, iova, bytes)) 216 - return 0; 217 - 218 - spin_lock(&gart->pte_lock); 219 - err = __gart_iommu_unmap(gart, iova); 220 - spin_unlock(&gart->pte_lock); 221 - 222 - return err ? 0 : bytes; 223 - } 224 - 225 - static phys_addr_t gart_iommu_iova_to_phys(struct iommu_domain *domain, 226 - dma_addr_t iova) 227 - { 228 - struct gart_device *gart = gart_handle; 229 - unsigned long pte; 230 - 231 - if (gart_iova_range_invalid(gart, iova, GART_PAGE_SIZE)) 232 - return -EINVAL; 233 - 234 - spin_lock(&gart->pte_lock); 235 - pte = gart_read_pte(gart, iova); 236 - spin_unlock(&gart->pte_lock); 237 - 238 - return pte & GART_PAGE_MASK; 239 - } 240 - 241 - static struct iommu_device *gart_iommu_probe_device(struct device *dev) 242 - { 243 - if (!dev_iommu_fwspec_get(dev)) 244 - return ERR_PTR(-ENODEV); 245 - 246 - return &gart_handle->iommu; 247 - } 248 - 249 - static int gart_iommu_of_xlate(struct device *dev, 250 - struct of_phandle_args *args) 251 - { 252 - return 0; 253 - } 254 - 255 - static void gart_iommu_sync_map(struct iommu_domain *domain, unsigned long iova, 256 - size_t size) 257 - { 258 - FLUSH_GART_REGS(gart_handle); 259 - } 260 - 261 - static void gart_iommu_sync(struct iommu_domain *domain, 262 - struct iommu_iotlb_gather *gather) 263 - { 264 - size_t length = gather->end - gather->start + 1; 265 - 266 - gart_iommu_sync_map(domain, gather->start, length); 267 - } 268 - 269 - static const struct iommu_ops gart_iommu_ops = { 270 - .domain_alloc = gart_iommu_domain_alloc, 271 - .probe_device = gart_iommu_probe_device, 272 - .device_group = generic_device_group, 273 - .set_platform_dma_ops = gart_iommu_set_platform_dma, 274 - .pgsize_bitmap = GART_IOMMU_PGSIZES, 275 - .of_xlate = gart_iommu_of_xlate, 276 - .default_domain_ops = &(const struct iommu_domain_ops) { 277 - .attach_dev = gart_iommu_attach_dev, 278 - .map = gart_iommu_map, 279 - .unmap = gart_iommu_unmap, 280 - .iova_to_phys = gart_iommu_iova_to_phys, 281 - .iotlb_sync_map = gart_iommu_sync_map, 282 - .iotlb_sync = gart_iommu_sync, 283 - .free = gart_iommu_domain_free, 284 - } 285 - }; 286 - 287 - int tegra_gart_suspend(struct gart_device *gart) 288 - { 289 - u32 *data = gart->savedata; 290 - unsigned long iova; 291 - 292 - /* 293 - * All GART users shall be suspended at this point. Disable 294 - * address translation to trap all GART accesses as invalid 295 - * memory accesses. 296 - */ 297 - writel_relaxed(0, gart->regs + GART_CONFIG); 298 - FLUSH_GART_REGS(gart); 299 - 300 - for_each_gart_pte(gart, iova) 301 - *(data++) = gart_read_pte(gart, iova); 302 - 303 - return 0; 304 - } 305 - 306 - int tegra_gart_resume(struct gart_device *gart) 307 - { 308 - do_gart_setup(gart, gart->savedata); 309 - 310 - return 0; 311 - } 312 - 313 - struct gart_device *tegra_gart_probe(struct device *dev, struct tegra_mc *mc) 314 - { 315 - struct gart_device *gart; 316 - struct resource *res; 317 - int err; 318 - 319 - BUILD_BUG_ON(PAGE_SHIFT != GART_PAGE_SHIFT); 320 - 321 - /* the GART memory aperture is required */ 322 - res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 1); 323 - if (!res) { 324 - dev_err(dev, "Memory aperture resource unavailable\n"); 325 - return ERR_PTR(-ENXIO); 326 - } 327 - 328 - gart = kzalloc(sizeof(*gart), GFP_KERNEL); 329 - if (!gart) 330 - return ERR_PTR(-ENOMEM); 331 - 332 - gart_handle = gart; 333 - 334 - gart->dev = dev; 335 - gart->regs = mc->regs + GART_REG_BASE; 336 - gart->iovmm_base = res->start; 337 - gart->iovmm_end = res->end + 1; 338 - spin_lock_init(&gart->pte_lock); 339 - spin_lock_init(&gart->dom_lock); 340 - 341 - do_gart_setup(gart, NULL); 342 - 343 - err = iommu_device_sysfs_add(&gart->iommu, dev, NULL, "gart"); 344 - if (err) 345 - goto free_gart; 346 - 347 - err = iommu_device_register(&gart->iommu, &gart_iommu_ops, dev); 348 - if (err) 349 - goto remove_sysfs; 350 - 351 - gart->savedata = vmalloc(resource_size(res) / GART_PAGE_SIZE * 352 - sizeof(u32)); 353 - if (!gart->savedata) { 354 - err = -ENOMEM; 355 - goto unregister_iommu; 356 - } 357 - 358 - return gart; 359 - 360 - unregister_iommu: 361 - iommu_device_unregister(&gart->iommu); 362 - remove_sysfs: 363 - iommu_device_sysfs_remove(&gart->iommu); 364 - free_gart: 365 - kfree(gart); 366 - 367 - return ERR_PTR(err); 368 - } 369 - 370 - module_param(gart_debug, bool, 0644); 371 - MODULE_PARM_DESC(gart_debug, "Enable GART debugging");
+42 -16
drivers/iommu/tegra-smmu.c
··· 272 272 clear_bit(id, smmu->asids); 273 273 } 274 274 275 - static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type) 275 + static struct iommu_domain *tegra_smmu_domain_alloc_paging(struct device *dev) 276 276 { 277 277 struct tegra_smmu_as *as; 278 - 279 - if (type != IOMMU_DOMAIN_UNMANAGED) 280 - return NULL; 281 278 282 279 as = kzalloc(sizeof(*as), GFP_KERNEL); 283 280 if (!as) ··· 508 511 return err; 509 512 } 510 513 511 - static void tegra_smmu_set_platform_dma(struct device *dev) 514 + static int tegra_smmu_identity_attach(struct iommu_domain *identity_domain, 515 + struct device *dev) 512 516 { 513 517 struct iommu_domain *domain = iommu_get_domain_for_dev(dev); 514 518 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 515 - struct tegra_smmu_as *as = to_smmu_as(domain); 516 - struct tegra_smmu *smmu = as->smmu; 519 + struct tegra_smmu_as *as; 520 + struct tegra_smmu *smmu; 517 521 unsigned int index; 518 522 519 523 if (!fwspec) 520 - return; 524 + return -ENODEV; 521 525 526 + if (domain == identity_domain || !domain) 527 + return 0; 528 + 529 + as = to_smmu_as(domain); 530 + smmu = as->smmu; 522 531 for (index = 0; index < fwspec->num_ids; index++) { 523 532 tegra_smmu_disable(smmu, fwspec->ids[index], as->id); 524 533 tegra_smmu_as_unprepare(smmu, as); 525 534 } 535 + return 0; 526 536 } 537 + 538 + static struct iommu_domain_ops tegra_smmu_identity_ops = { 539 + .attach_dev = tegra_smmu_identity_attach, 540 + }; 541 + 542 + static struct iommu_domain tegra_smmu_identity_domain = { 543 + .type = IOMMU_DOMAIN_IDENTITY, 544 + .ops = &tegra_smmu_identity_ops, 545 + }; 527 546 528 547 static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova, 529 548 u32 value) ··· 764 751 } 765 752 766 753 static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova, 767 - phys_addr_t paddr, size_t size, int prot, gfp_t gfp) 754 + phys_addr_t paddr, size_t size, size_t count, 755 + int prot, gfp_t gfp, size_t *mapped) 768 756 { 769 757 struct tegra_smmu_as *as = to_smmu_as(domain); 770 758 unsigned long flags; ··· 775 761 ret = __tegra_smmu_map(domain, iova, paddr, size, prot, gfp, &flags); 776 762 spin_unlock_irqrestore(&as->lock, flags); 777 763 764 + if (!ret) 765 + *mapped = size; 766 + 778 767 return ret; 779 768 } 780 769 781 770 static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova, 782 - size_t size, struct iommu_iotlb_gather *gather) 771 + size_t size, size_t count, struct iommu_iotlb_gather *gather) 783 772 { 784 773 struct tegra_smmu_as *as = to_smmu_as(domain); 785 774 unsigned long flags; ··· 979 962 return iommu_fwspec_add_ids(dev, &id, 1); 980 963 } 981 964 965 + static int tegra_smmu_def_domain_type(struct device *dev) 966 + { 967 + /* 968 + * FIXME: For now we want to run all translation in IDENTITY mode, due 969 + * to some device quirks. Better would be to just quirk the troubled 970 + * devices. 971 + */ 972 + return IOMMU_DOMAIN_IDENTITY; 973 + } 974 + 982 975 static const struct iommu_ops tegra_smmu_ops = { 983 - .domain_alloc = tegra_smmu_domain_alloc, 976 + .identity_domain = &tegra_smmu_identity_domain, 977 + .def_domain_type = &tegra_smmu_def_domain_type, 978 + .domain_alloc_paging = tegra_smmu_domain_alloc_paging, 984 979 .probe_device = tegra_smmu_probe_device, 985 980 .device_group = tegra_smmu_device_group, 986 - .set_platform_dma_ops = tegra_smmu_set_platform_dma, 987 981 .of_xlate = tegra_smmu_of_xlate, 988 982 .pgsize_bitmap = SZ_4K, 989 983 .default_domain_ops = &(const struct iommu_domain_ops) { 990 984 .attach_dev = tegra_smmu_attach_dev, 991 - .map = tegra_smmu_map, 992 - .unmap = tegra_smmu_unmap, 985 + .map_pages = tegra_smmu_map, 986 + .unmap_pages = tegra_smmu_unmap, 993 987 .iova_to_phys = tegra_smmu_iova_to_phys, 994 988 .free = tegra_smmu_domain_free, 995 989 } ··· 1084 1056 static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu) 1085 1057 { 1086 1058 smmu->debugfs = debugfs_create_dir("smmu", NULL); 1087 - if (!smmu->debugfs) 1088 - return; 1089 1059 1090 1060 debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu, 1091 1061 &tegra_smmu_swgroups_fops);
+2 -2
drivers/iommu/virtio-iommu.c
··· 85 85 void *writeback; 86 86 unsigned int write_offset; 87 87 unsigned int len; 88 - char buf[]; 88 + char buf[] __counted_by(len); 89 89 }; 90 90 91 91 #define VIOMMU_FAULT_RESV_MASK 0xffffff00 ··· 230 230 if (write_offset <= 0) 231 231 return -EINVAL; 232 232 233 - req = kzalloc(sizeof(*req) + len, GFP_ATOMIC); 233 + req = kzalloc(struct_size(req, buf, len), GFP_ATOMIC); 234 234 if (!req) 235 235 return -ENOMEM; 236 236
-34
drivers/memory/tegra/mc.c
··· 979 979 } 980 980 } 981 981 982 - if (IS_ENABLED(CONFIG_TEGRA_IOMMU_GART) && !mc->soc->smmu) { 983 - mc->gart = tegra_gart_probe(&pdev->dev, mc); 984 - if (IS_ERR(mc->gart)) { 985 - dev_err(&pdev->dev, "failed to probe GART: %ld\n", 986 - PTR_ERR(mc->gart)); 987 - mc->gart = NULL; 988 - } 989 - } 990 - 991 - return 0; 992 - } 993 - 994 - static int __maybe_unused tegra_mc_suspend(struct device *dev) 995 - { 996 - struct tegra_mc *mc = dev_get_drvdata(dev); 997 - 998 - if (mc->soc->ops && mc->soc->ops->suspend) 999 - return mc->soc->ops->suspend(mc); 1000 - 1001 - return 0; 1002 - } 1003 - 1004 - static int __maybe_unused tegra_mc_resume(struct device *dev) 1005 - { 1006 - struct tegra_mc *mc = dev_get_drvdata(dev); 1007 - 1008 - if (mc->soc->ops && mc->soc->ops->resume) 1009 - return mc->soc->ops->resume(mc); 1010 - 1011 982 return 0; 1012 983 } 1013 984 ··· 991 1020 icc_sync_state(dev); 992 1021 } 993 1022 994 - static const struct dev_pm_ops tegra_mc_pm_ops = { 995 - SET_SYSTEM_SLEEP_PM_OPS(tegra_mc_suspend, tegra_mc_resume) 996 - }; 997 - 998 1023 static struct platform_driver tegra_mc_driver = { 999 1024 .driver = { 1000 1025 .name = "tegra-mc", 1001 1026 .of_match_table = tegra_mc_of_match, 1002 - .pm = &tegra_mc_pm_ops, 1003 1027 .suppress_bind_attrs = true, 1004 1028 .sync_state = tegra_mc_sync_state, 1005 1029 },
-28
drivers/memory/tegra/tegra20.c
··· 688 688 return 0; 689 689 } 690 690 691 - static int tegra20_mc_suspend(struct tegra_mc *mc) 692 - { 693 - int err; 694 - 695 - if (IS_ENABLED(CONFIG_TEGRA_IOMMU_GART) && mc->gart) { 696 - err = tegra_gart_suspend(mc->gart); 697 - if (err < 0) 698 - return err; 699 - } 700 - 701 - return 0; 702 - } 703 - 704 - static int tegra20_mc_resume(struct tegra_mc *mc) 705 - { 706 - int err; 707 - 708 - if (IS_ENABLED(CONFIG_TEGRA_IOMMU_GART) && mc->gart) { 709 - err = tegra_gart_resume(mc->gart); 710 - if (err < 0) 711 - return err; 712 - } 713 - 714 - return 0; 715 - } 716 - 717 691 static irqreturn_t tegra20_mc_handle_irq(int irq, void *data) 718 692 { 719 693 struct tegra_mc *mc = data; ··· 763 789 764 790 static const struct tegra_mc_ops tegra20_mc_ops = { 765 791 .probe = tegra20_mc_probe, 766 - .suspend = tegra20_mc_suspend, 767 - .resume = tegra20_mc_resume, 768 792 .handle_irq = tegra20_mc_handle_irq, 769 793 }; 770 794
-120
include/linux/amd-iommu.h
··· 33 33 34 34 extern int amd_iommu_detect(void); 35 35 36 - /** 37 - * amd_iommu_init_device() - Init device for use with IOMMUv2 driver 38 - * @pdev: The PCI device to initialize 39 - * @pasids: Number of PASIDs to support for this device 40 - * 41 - * This function does all setup for the device pdev so that it can be 42 - * used with IOMMUv2. 43 - * Returns 0 on success or negative value on error. 44 - */ 45 - extern int amd_iommu_init_device(struct pci_dev *pdev, int pasids); 46 - 47 - /** 48 - * amd_iommu_free_device() - Free all IOMMUv2 related device resources 49 - * and disable IOMMUv2 usage for this device 50 - * @pdev: The PCI device to disable IOMMUv2 usage for' 51 - */ 52 - extern void amd_iommu_free_device(struct pci_dev *pdev); 53 - 54 - /** 55 - * amd_iommu_bind_pasid() - Bind a given task to a PASID on a device 56 - * @pdev: The PCI device to bind the task to 57 - * @pasid: The PASID on the device the task should be bound to 58 - * @task: the task to bind 59 - * 60 - * The function returns 0 on success or a negative value on error. 61 - */ 62 - extern int amd_iommu_bind_pasid(struct pci_dev *pdev, u32 pasid, 63 - struct task_struct *task); 64 - 65 - /** 66 - * amd_iommu_unbind_pasid() - Unbind a PASID from its task on 67 - * a device 68 - * @pdev: The device of the PASID 69 - * @pasid: The PASID to unbind 70 - * 71 - * When this function returns the device is no longer using the PASID 72 - * and the PASID is no longer bound to its task. 73 - */ 74 - extern void amd_iommu_unbind_pasid(struct pci_dev *pdev, u32 pasid); 75 - 76 - /** 77 - * amd_iommu_set_invalid_ppr_cb() - Register a call-back for failed 78 - * PRI requests 79 - * @pdev: The PCI device the call-back should be registered for 80 - * @cb: The call-back function 81 - * 82 - * The IOMMUv2 driver invokes this call-back when it is unable to 83 - * successfully handle a PRI request. The device driver can then decide 84 - * which PRI response the device should see. Possible return values for 85 - * the call-back are: 86 - * 87 - * - AMD_IOMMU_INV_PRI_RSP_SUCCESS - Send SUCCESS back to the device 88 - * - AMD_IOMMU_INV_PRI_RSP_INVALID - Send INVALID back to the device 89 - * - AMD_IOMMU_INV_PRI_RSP_FAIL - Send Failure back to the device, 90 - * the device is required to disable 91 - * PRI when it receives this response 92 - * 93 - * The function returns 0 on success or negative value on error. 94 - */ 95 - #define AMD_IOMMU_INV_PRI_RSP_SUCCESS 0 96 - #define AMD_IOMMU_INV_PRI_RSP_INVALID 1 97 - #define AMD_IOMMU_INV_PRI_RSP_FAIL 2 98 - 99 - typedef int (*amd_iommu_invalid_ppr_cb)(struct pci_dev *pdev, 100 - u32 pasid, 101 - unsigned long address, 102 - u16); 103 - 104 - extern int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev, 105 - amd_iommu_invalid_ppr_cb cb); 106 - 107 - #define PPR_FAULT_EXEC (1 << 1) 108 - #define PPR_FAULT_READ (1 << 2) 109 - #define PPR_FAULT_WRITE (1 << 5) 110 - #define PPR_FAULT_USER (1 << 6) 111 - #define PPR_FAULT_RSVD (1 << 7) 112 - #define PPR_FAULT_GN (1 << 8) 113 - 114 - /** 115 - * amd_iommu_device_info() - Get information about IOMMUv2 support of a 116 - * PCI device 117 - * @pdev: PCI device to query information from 118 - * @info: A pointer to an amd_iommu_device_info structure which will contain 119 - * the information about the PCI device 120 - * 121 - * Returns 0 on success, negative value on error 122 - */ 123 - 124 - #define AMD_IOMMU_DEVICE_FLAG_ATS_SUP 0x1 /* ATS feature supported */ 125 - #define AMD_IOMMU_DEVICE_FLAG_PRI_SUP 0x2 /* PRI feature supported */ 126 - #define AMD_IOMMU_DEVICE_FLAG_PASID_SUP 0x4 /* PASID context supported */ 127 - #define AMD_IOMMU_DEVICE_FLAG_EXEC_SUP 0x8 /* Device may request execution 128 - on memory pages */ 129 - #define AMD_IOMMU_DEVICE_FLAG_PRIV_SUP 0x10 /* Device may request 130 - super-user privileges */ 131 - 132 - struct amd_iommu_device_info { 133 - int max_pasids; 134 - u32 flags; 135 - }; 136 - 137 - extern int amd_iommu_device_info(struct pci_dev *pdev, 138 - struct amd_iommu_device_info *info); 139 - 140 - /** 141 - * amd_iommu_set_invalidate_ctx_cb() - Register a call-back for invalidating 142 - * a pasid context. This call-back is 143 - * invoked when the IOMMUv2 driver needs to 144 - * invalidate a PASID context, for example 145 - * because the task that is bound to that 146 - * context is about to exit. 147 - * 148 - * @pdev: The PCI device the call-back should be registered for 149 - * @cb: The call-back function 150 - */ 151 - 152 - typedef void (*amd_iommu_invalidate_ctx)(struct pci_dev *pdev, u32 pasid); 153 - 154 - extern int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev, 155 - amd_iommu_invalidate_ctx cb); 156 36 #else /* CONFIG_AMD_IOMMU */ 157 37 158 38 static inline int amd_iommu_detect(void) { return -ENODEV; }
+25 -13
include/linux/iommu.h
··· 66 66 #define __IOMMU_DOMAIN_DMA_FQ (1U << 3) /* DMA-API uses flush queue */ 67 67 68 68 #define __IOMMU_DOMAIN_SVA (1U << 4) /* Shared process address space */ 69 + #define __IOMMU_DOMAIN_PLATFORM (1U << 5) 69 70 70 71 #define __IOMMU_DOMAIN_NESTED (1U << 6) /* User-managed address space nested 71 72 on a stage-2 translation */ ··· 87 86 * invalidation. 88 87 * IOMMU_DOMAIN_SVA - DMA addresses are shared process addresses 89 88 * represented by mm_struct's. 89 + * IOMMU_DOMAIN_PLATFORM - Legacy domain for drivers that do their own 90 + * dma_api stuff. Do not use in new drivers. 90 91 */ 91 92 #define IOMMU_DOMAIN_BLOCKED (0U) 92 93 #define IOMMU_DOMAIN_IDENTITY (__IOMMU_DOMAIN_PT) ··· 99 96 __IOMMU_DOMAIN_DMA_API | \ 100 97 __IOMMU_DOMAIN_DMA_FQ) 101 98 #define IOMMU_DOMAIN_SVA (__IOMMU_DOMAIN_SVA) 99 + #define IOMMU_DOMAIN_PLATFORM (__IOMMU_DOMAIN_PLATFORM) 102 100 #define IOMMU_DOMAIN_NESTED (__IOMMU_DOMAIN_NESTED) 103 101 104 102 struct iommu_domain { ··· 344 340 * NULL while the @user_data can be optionally provided, the 345 341 * new domain must support __IOMMU_DOMAIN_PAGING. 346 342 * Upon failure, ERR_PTR must be returned. 343 + * @domain_alloc_paging: Allocate an iommu_domain that can be used for 344 + * UNMANAGED, DMA, and DMA_FQ domain types. 347 345 * @probe_device: Add device to iommu driver handling 348 346 * @release_device: Remove device from iommu driver handling 349 347 * @probe_finalize: Do final setup work after the device is added to an IOMMU 350 348 * group and attached to the groups domain 351 - * @set_platform_dma_ops: Returning control back to the platform DMA ops. This op 352 - * is to support old IOMMU drivers, new drivers should use 353 - * default domains, and the common IOMMU DMA ops. 354 349 * @device_group: find iommu group for a particular device 355 350 * @get_resv_regions: Request list of reserved regions for a device 356 351 * @of_xlate: add OF master IDs to iommu grouping ··· 368 365 * will be blocked by the hardware. 369 366 * @pgsize_bitmap: bitmap of all possible supported page sizes 370 367 * @owner: Driver module providing these ops 368 + * @identity_domain: An always available, always attachable identity 369 + * translation. 370 + * @blocked_domain: An always available, always attachable blocking 371 + * translation. 372 + * @default_domain: If not NULL this will always be set as the default domain. 373 + * This should be an IDENTITY/BLOCKED/PLATFORM domain. 374 + * Do not use in new drivers. 371 375 */ 372 376 struct iommu_ops { 373 377 bool (*capable)(struct device *dev, enum iommu_cap); ··· 385 375 struct iommu_domain *(*domain_alloc_user)( 386 376 struct device *dev, u32 flags, struct iommu_domain *parent, 387 377 const struct iommu_user_data *user_data); 378 + struct iommu_domain *(*domain_alloc_paging)(struct device *dev); 388 379 389 380 struct iommu_device *(*probe_device)(struct device *dev); 390 381 void (*release_device)(struct device *dev); 391 382 void (*probe_finalize)(struct device *dev); 392 - void (*set_platform_dma_ops)(struct device *dev); 393 383 struct iommu_group *(*device_group)(struct device *dev); 394 384 395 385 /* Request/Free a list of reserved regions for a device */ ··· 412 402 const struct iommu_domain_ops *default_domain_ops; 413 403 unsigned long pgsize_bitmap; 414 404 struct module *owner; 405 + struct iommu_domain *identity_domain; 406 + struct iommu_domain *blocked_domain; 407 + struct iommu_domain *default_domain; 415 408 }; 416 409 417 410 /** ··· 433 420 * * ENODEV - device specific errors, not able to be attached 434 421 * * <others> - treated as ENODEV by the caller. Use is discouraged 435 422 * @set_dev_pasid: set an iommu domain to a pasid of device 436 - * @map: map a physically contiguous memory region to an iommu domain 437 423 * @map_pages: map a physically contiguous set of pages of the same size to 438 424 * an iommu domain. 439 - * @unmap: unmap a physically contiguous memory region from an iommu domain 440 425 * @unmap_pages: unmap a number of pages of the same size from an iommu domain 441 426 * @flush_iotlb_all: Synchronously flush all hardware TLBs for this domain 442 427 * @iotlb_sync_map: Sync mappings created recently using @map to the hardware ··· 453 442 int (*set_dev_pasid)(struct iommu_domain *domain, struct device *dev, 454 443 ioasid_t pasid); 455 444 456 - int (*map)(struct iommu_domain *domain, unsigned long iova, 457 - phys_addr_t paddr, size_t size, int prot, gfp_t gfp); 458 445 int (*map_pages)(struct iommu_domain *domain, unsigned long iova, 459 446 phys_addr_t paddr, size_t pgsize, size_t pgcount, 460 447 int prot, gfp_t gfp, size_t *mapped); 461 - size_t (*unmap)(struct iommu_domain *domain, unsigned long iova, 462 - size_t size, struct iommu_iotlb_gather *iotlb_gather); 463 448 size_t (*unmap_pages)(struct iommu_domain *domain, unsigned long iova, 464 449 size_t pgsize, size_t pgcount, 465 450 struct iommu_iotlb_gather *iotlb_gather); 466 451 467 452 void (*flush_iotlb_all)(struct iommu_domain *domain); 468 - void (*iotlb_sync_map)(struct iommu_domain *domain, unsigned long iova, 469 - size_t size); 453 + int (*iotlb_sync_map)(struct iommu_domain *domain, unsigned long iova, 454 + size_t size); 470 455 void (*iotlb_sync)(struct iommu_domain *domain, 471 456 struct iommu_iotlb_gather *iotlb_gather); 472 457 ··· 483 476 * @list: Used by the iommu-core to keep a list of registered iommus 484 477 * @ops: iommu-ops for talking to this iommu 485 478 * @dev: struct device for sysfs handling 479 + * @singleton_group: Used internally for drivers that have only one group 486 480 * @max_pasids: number of supported PASIDs 487 481 */ 488 482 struct iommu_device { ··· 491 483 const struct iommu_ops *ops; 492 484 struct fwnode_handle *fwnode; 493 485 struct device *dev; 486 + struct iommu_group *singleton_group; 494 487 u32 max_pasids; 495 488 }; 496 489 ··· 535 526 * @attach_deferred: the dma domain attachment is deferred 536 527 * @pci_32bit_workaround: Limit DMA allocations to 32-bit IOVAs 537 528 * @require_direct: device requires IOMMU_RESV_DIRECT regions 529 + * @shadow_on_flush: IOTLB flushes are used to sync shadow tables 538 530 * 539 531 * TODO: migrate other per device data pointers under iommu_dev_data, e.g. 540 532 * struct iommu_group *iommu_group; ··· 551 541 u32 attach_deferred:1; 552 542 u32 pci_32bit_workaround:1; 553 543 u32 require_direct:1; 544 + u32 shadow_on_flush:1; 554 545 }; 555 546 556 547 int iommu_device_register(struct iommu_device *iommu, ··· 779 768 extern struct iommu_group *generic_device_group(struct device *dev); 780 769 /* FSL-MC device grouping function */ 781 770 struct iommu_group *fsl_mc_device_group(struct device *dev); 771 + extern struct iommu_group *generic_single_device_group(struct device *dev); 782 772 783 773 /** 784 774 * struct iommu_fwspec - per-device IOMMU instance data ··· 1265 1253 * Creates a mapping at @iova for the buffer described by a scatterlist 1266 1254 * stored in the given sg_table object in the provided IOMMU domain. 1267 1255 */ 1268 - static inline size_t iommu_map_sgtable(struct iommu_domain *domain, 1256 + static inline ssize_t iommu_map_sgtable(struct iommu_domain *domain, 1269 1257 unsigned long iova, struct sg_table *sgt, int prot) 1270 1258 { 1271 1259 return iommu_map_sg(domain, iova, sgt->sgl, sgt->orig_nents, prot,
-26
include/soc/tegra/mc.h
··· 96 96 97 97 struct tegra_mc; 98 98 struct tegra_smmu; 99 - struct gart_device; 100 99 101 100 #ifdef CONFIG_TEGRA_IOMMU_SMMU 102 101 struct tegra_smmu *tegra_smmu_probe(struct device *dev, ··· 112 113 113 114 static inline void tegra_smmu_remove(struct tegra_smmu *smmu) 114 115 { 115 - } 116 - #endif 117 - 118 - #ifdef CONFIG_TEGRA_IOMMU_GART 119 - struct gart_device *tegra_gart_probe(struct device *dev, struct tegra_mc *mc); 120 - int tegra_gart_suspend(struct gart_device *gart); 121 - int tegra_gart_resume(struct gart_device *gart); 122 - #else 123 - static inline struct gart_device * 124 - tegra_gart_probe(struct device *dev, struct tegra_mc *mc) 125 - { 126 - return ERR_PTR(-ENODEV); 127 - } 128 - 129 - static inline int tegra_gart_suspend(struct gart_device *gart) 130 - { 131 - return -ENODEV; 132 - } 133 - 134 - static inline int tegra_gart_resume(struct gart_device *gart) 135 - { 136 - return -ENODEV; 137 116 } 138 117 #endif 139 118 ··· 162 185 */ 163 186 int (*probe)(struct tegra_mc *mc); 164 187 void (*remove)(struct tegra_mc *mc); 165 - int (*suspend)(struct tegra_mc *mc); 166 - int (*resume)(struct tegra_mc *mc); 167 188 irqreturn_t (*handle_irq)(int irq, void *data); 168 189 int (*probe_device)(struct tegra_mc *mc, struct device *dev); 169 190 }; ··· 200 225 struct tegra_bpmp *bpmp; 201 226 struct device *dev; 202 227 struct tegra_smmu *smmu; 203 - struct gart_device *gart; 204 228 void __iomem *regs; 205 229 void __iomem *bcast_ch_regs; 206 230 void __iomem **ch_regs;