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

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

Pull IOMMU UPDATES from Joerg Roedel:

- KVM PCIe/MSI passthrough support on ARM/ARM64

- introduction of a core representation for individual hardware iommus

- support for IOMMU privileged mappings as supported by some ARM IOMMUS

- 16-bit SID support for ARM-SMMUv2

- stream table optimization for ARM-SMMUv3

- various fixes and other small improvements

* tag 'iommu-updates-v4.11' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu: (61 commits)
vfio/type1: Fix error return code in vfio_iommu_type1_attach_group()
iommu: Remove iommu_register_instance interface
iommu/exynos: Make use of iommu_device_register interface
iommu/mediatek: Make use of iommu_device_register interface
iommu/msm: Make use of iommu_device_register interface
iommu/arm-smmu: Make use of the iommu_register interface
iommu: Add iommu_device_set_fwnode() interface
iommu: Make iommu_device_link/unlink take a struct iommu_device
iommu: Add sysfs bindings for struct iommu_device
iommu: Introduce new 'struct iommu_device'
iommu: Rename struct iommu_device
iommu: Rename iommu_get_instance()
iommu: Fix static checker warning in iommu_insert_device_resv_regions
iommu: Avoid unnecessary assignment of dev->iommu_fwspec
iommu/mediatek: Remove bogus 'select' statements
iommu/dma: Remove bogus dma_supported() implementation
iommu/ipmmu-vmsa: Restrict IOMMU Domain Geometry to 32-bit address space
iommu/vt-d: Don't over-free page table directories
iommu/vt-d: Tylersburg isoch identity map check is done too late.
iommu/vt-d: Fix some macros that are incorrectly specified in intel-iommu
...

+1194 -395
+12
Documentation/ABI/testing/sysfs-kernel-iommu_groups
··· 12 12 file if the IOMMU driver has chosen to register a more 13 13 common name for the group. 14 14 Users: 15 + 16 + What: /sys/kernel/iommu_groups/reserved_regions 17 + Date: January 2017 18 + KernelVersion: v4.11 19 + Contact: Eric Auger <eric.auger@redhat.com> 20 + Description: /sys/kernel/iommu_groups/reserved_regions list IOVA 21 + regions that are reserved. Not necessarily all 22 + reserved regions are listed. This is typically used to 23 + output direct-mapped, MSI, non mappable regions. Each 24 + region is described on a single line: the 1st field is 25 + the base IOVA, the second is the end IOVA and the third 26 + field describes the type of the region.
+10
Documentation/DMA-attributes.txt
··· 143 143 where allocation failures are not a problem, and shouldn't bother the logs. 144 144 145 145 NOTE: At the moment DMA_ATTR_NO_WARN is only implemented on PowerPC. 146 + 147 + DMA_ATTR_PRIVILEGED 148 + ------------------------------ 149 + 150 + Some advanced peripherals such as remote processors and GPUs perform 151 + accesses to DMA buffers in both privileged "supervisor" and unprivileged 152 + "user" modes. This attribute is used to indicate to the DMA-mapping 153 + subsystem that the buffer is fully accessible at the elevated privilege 154 + level (and ideally inaccessible or at least read-only at the 155 + lesser-privileged levels).
+30 -30
arch/arm/mm/dma-mapping.c
··· 1171 1171 1172 1172 #ifdef CONFIG_ARM_DMA_USE_IOMMU 1173 1173 1174 + static int __dma_info_to_prot(enum dma_data_direction dir, unsigned long attrs) 1175 + { 1176 + int prot = 0; 1177 + 1178 + if (attrs & DMA_ATTR_PRIVILEGED) 1179 + prot |= IOMMU_PRIV; 1180 + 1181 + switch (dir) { 1182 + case DMA_BIDIRECTIONAL: 1183 + return prot | IOMMU_READ | IOMMU_WRITE; 1184 + case DMA_TO_DEVICE: 1185 + return prot | IOMMU_READ; 1186 + case DMA_FROM_DEVICE: 1187 + return prot | IOMMU_WRITE; 1188 + default: 1189 + return prot; 1190 + } 1191 + } 1192 + 1174 1193 /* IOMMU */ 1175 1194 1176 1195 static int extend_iommu_mapping(struct dma_iommu_mapping *mapping); ··· 1413 1394 * Create a mapping in device IO address space for specified pages 1414 1395 */ 1415 1396 static dma_addr_t 1416 - __iommu_create_mapping(struct device *dev, struct page **pages, size_t size) 1397 + __iommu_create_mapping(struct device *dev, struct page **pages, size_t size, 1398 + unsigned long attrs) 1417 1399 { 1418 1400 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev); 1419 1401 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; ··· 1439 1419 1440 1420 len = (j - i) << PAGE_SHIFT; 1441 1421 ret = iommu_map(mapping->domain, iova, phys, len, 1442 - IOMMU_READ|IOMMU_WRITE); 1422 + __dma_info_to_prot(DMA_BIDIRECTIONAL, attrs)); 1443 1423 if (ret < 0) 1444 1424 goto fail; 1445 1425 iova += len; ··· 1496 1476 } 1497 1477 1498 1478 static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp, 1499 - dma_addr_t *handle, int coherent_flag) 1479 + dma_addr_t *handle, int coherent_flag, 1480 + unsigned long attrs) 1500 1481 { 1501 1482 struct page *page; 1502 1483 void *addr; ··· 1509 1488 if (!addr) 1510 1489 return NULL; 1511 1490 1512 - *handle = __iommu_create_mapping(dev, &page, size); 1491 + *handle = __iommu_create_mapping(dev, &page, size, attrs); 1513 1492 if (*handle == DMA_ERROR_CODE) 1514 1493 goto err_mapping; 1515 1494 ··· 1543 1522 1544 1523 if (coherent_flag == COHERENT || !gfpflags_allow_blocking(gfp)) 1545 1524 return __iommu_alloc_simple(dev, size, gfp, handle, 1546 - coherent_flag); 1525 + coherent_flag, attrs); 1547 1526 1548 1527 /* 1549 1528 * Following is a work-around (a.k.a. hack) to prevent pages ··· 1558 1537 if (!pages) 1559 1538 return NULL; 1560 1539 1561 - *handle = __iommu_create_mapping(dev, pages, size); 1540 + *handle = __iommu_create_mapping(dev, pages, size, attrs); 1562 1541 if (*handle == DMA_ERROR_CODE) 1563 1542 goto err_buffer; 1564 1543 ··· 1693 1672 GFP_KERNEL); 1694 1673 } 1695 1674 1696 - static int __dma_direction_to_prot(enum dma_data_direction dir) 1697 - { 1698 - int prot; 1699 - 1700 - switch (dir) { 1701 - case DMA_BIDIRECTIONAL: 1702 - prot = IOMMU_READ | IOMMU_WRITE; 1703 - break; 1704 - case DMA_TO_DEVICE: 1705 - prot = IOMMU_READ; 1706 - break; 1707 - case DMA_FROM_DEVICE: 1708 - prot = IOMMU_WRITE; 1709 - break; 1710 - default: 1711 - prot = 0; 1712 - } 1713 - 1714 - return prot; 1715 - } 1716 - 1717 1675 /* 1718 1676 * Map a part of the scatter-gather list into contiguous io address space 1719 1677 */ ··· 1722 1722 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0) 1723 1723 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir); 1724 1724 1725 - prot = __dma_direction_to_prot(dir); 1725 + prot = __dma_info_to_prot(dir, attrs); 1726 1726 1727 1727 ret = iommu_map(mapping->domain, iova, phys, len, prot); 1728 1728 if (ret < 0) ··· 1930 1930 if (dma_addr == DMA_ERROR_CODE) 1931 1931 return dma_addr; 1932 1932 1933 - prot = __dma_direction_to_prot(dir); 1933 + prot = __dma_info_to_prot(dir, attrs); 1934 1934 1935 1935 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot); 1936 1936 if (ret < 0) ··· 2036 2036 if (dma_addr == DMA_ERROR_CODE) 2037 2037 return dma_addr; 2038 2038 2039 - prot = __dma_direction_to_prot(dir) | IOMMU_MMIO; 2039 + prot = __dma_info_to_prot(dir, attrs) | IOMMU_MMIO; 2040 2040 2041 2041 ret = iommu_map(mapping->domain, dma_addr, addr, len, prot); 2042 2042 if (ret < 0)
+3 -4
arch/arm64/mm/dma-mapping.c
··· 558 558 unsigned long attrs) 559 559 { 560 560 bool coherent = is_device_dma_coherent(dev); 561 - int ioprot = dma_direction_to_prot(DMA_BIDIRECTIONAL, coherent); 561 + int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs); 562 562 size_t iosize = size; 563 563 void *addr; 564 564 ··· 712 712 unsigned long attrs) 713 713 { 714 714 bool coherent = is_device_dma_coherent(dev); 715 - int prot = dma_direction_to_prot(dir, coherent); 715 + int prot = dma_info_to_prot(dir, coherent, attrs); 716 716 dma_addr_t dev_addr = iommu_dma_map_page(dev, page, offset, size, prot); 717 717 718 718 if (!iommu_dma_mapping_error(dev, dev_addr) && ··· 770 770 __iommu_sync_sg_for_device(dev, sgl, nelems, dir); 771 771 772 772 return iommu_dma_map_sg(dev, sgl, nelems, 773 - dma_direction_to_prot(dir, coherent)); 773 + dma_info_to_prot(dir, coherent, attrs)); 774 774 } 775 775 776 776 static void __iommu_unmap_sg_attrs(struct device *dev, ··· 799 799 .sync_sg_for_device = __iommu_sync_sg_for_device, 800 800 .map_resource = iommu_dma_map_resource, 801 801 .unmap_resource = iommu_dma_unmap_resource, 802 - .dma_supported = iommu_dma_supported, 803 802 .mapping_error = iommu_dma_mapping_error, 804 803 }; 805 804
+1 -1
drivers/acpi/arm64/iort.c
··· 536 536 if (!iort_fwnode) 537 537 return NULL; 538 538 539 - ops = iommu_get_instance(iort_fwnode); 539 + ops = iommu_ops_from_fwnode(iort_fwnode); 540 540 if (!ops) 541 541 return NULL; 542 542
+3 -2
drivers/dma/pl330.c
··· 1859 1859 * Alloc MicroCode buffer for 'chans' Channel threads. 1860 1860 * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN) 1861 1861 */ 1862 - pl330->mcode_cpu = dma_alloc_coherent(pl330->ddma.dev, 1862 + pl330->mcode_cpu = dma_alloc_attrs(pl330->ddma.dev, 1863 1863 chans * pl330->mcbufsz, 1864 - &pl330->mcode_bus, GFP_KERNEL); 1864 + &pl330->mcode_bus, GFP_KERNEL, 1865 + DMA_ATTR_PRIVILEGED); 1865 1866 if (!pl330->mcode_cpu) { 1866 1867 dev_err(pl330->ddma.dev, "%s:%d Can't allocate memory!\n", 1867 1868 __func__, __LINE__);
-3
drivers/iommu/Kconfig
··· 352 352 select IOMMU_API 353 353 select MEMORY 354 354 select MTK_SMI 355 - select COMMON_CLK_MT2701_MMSYS 356 - select COMMON_CLK_MT2701_IMGSYS 357 - select COMMON_CLK_MT2701_VDECSYS 358 355 help 359 356 Support for the M4U on certain Mediatek SoCs. M4U generation 1 HW is 360 357 Multimedia Memory Managememt Unit. This option enables remapping of
+46 -26
drivers/iommu/amd_iommu.c
··· 112 112 * Domain for untranslated devices - only allocated 113 113 * if iommu=pt passed on kernel cmd line. 114 114 */ 115 - static const struct iommu_ops amd_iommu_ops; 115 + const struct iommu_ops amd_iommu_ops; 116 116 117 117 static ATOMIC_NOTIFIER_HEAD(ppr_notifier); 118 118 int amd_iommu_max_glx_val = -1; ··· 445 445 static int iommu_init_device(struct device *dev) 446 446 { 447 447 struct iommu_dev_data *dev_data; 448 + struct amd_iommu *iommu; 448 449 int devid; 449 450 450 451 if (dev->archdata.iommu) ··· 454 453 devid = get_device_id(dev); 455 454 if (devid < 0) 456 455 return devid; 456 + 457 + iommu = amd_iommu_rlookup_table[devid]; 457 458 458 459 dev_data = find_dev_data(devid); 459 460 if (!dev_data) ··· 472 469 473 470 dev->archdata.iommu = dev_data; 474 471 475 - iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev, 476 - dev); 472 + iommu_device_link(&iommu->iommu, dev); 477 473 478 474 return 0; 479 475 } ··· 497 495 498 496 static void iommu_uninit_device(struct device *dev) 499 497 { 500 - int devid; 501 498 struct iommu_dev_data *dev_data; 499 + struct amd_iommu *iommu; 500 + int devid; 502 501 503 502 devid = get_device_id(dev); 504 503 if (devid < 0) 505 504 return; 505 + 506 + iommu = amd_iommu_rlookup_table[devid]; 506 507 507 508 dev_data = search_dev_data(devid); 508 509 if (!dev_data) ··· 514 509 if (dev_data->domain) 515 510 detach_device(dev); 516 511 517 - iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev, 518 - dev); 512 + iommu_device_unlink(&iommu->iommu, dev); 519 513 520 514 iommu_group_remove_device(dev); 521 515 ··· 3165 3161 return false; 3166 3162 } 3167 3163 3168 - static void amd_iommu_get_dm_regions(struct device *dev, 3169 - struct list_head *head) 3164 + static void amd_iommu_get_resv_regions(struct device *dev, 3165 + struct list_head *head) 3170 3166 { 3167 + struct iommu_resv_region *region; 3171 3168 struct unity_map_entry *entry; 3172 3169 int devid; 3173 3170 ··· 3177 3172 return; 3178 3173 3179 3174 list_for_each_entry(entry, &amd_iommu_unity_map, list) { 3180 - struct iommu_dm_region *region; 3175 + size_t length; 3176 + int prot = 0; 3181 3177 3182 3178 if (devid < entry->devid_start || devid > entry->devid_end) 3183 3179 continue; 3184 3180 3185 - region = kzalloc(sizeof(*region), GFP_KERNEL); 3181 + length = entry->address_end - entry->address_start; 3182 + if (entry->prot & IOMMU_PROT_IR) 3183 + prot |= IOMMU_READ; 3184 + if (entry->prot & IOMMU_PROT_IW) 3185 + prot |= IOMMU_WRITE; 3186 + 3187 + region = iommu_alloc_resv_region(entry->address_start, 3188 + length, prot, 3189 + IOMMU_RESV_DIRECT); 3186 3190 if (!region) { 3187 3191 pr_err("Out of memory allocating dm-regions for %s\n", 3188 3192 dev_name(dev)); 3189 3193 return; 3190 3194 } 3191 - 3192 - region->start = entry->address_start; 3193 - region->length = entry->address_end - entry->address_start; 3194 - if (entry->prot & IOMMU_PROT_IR) 3195 - region->prot |= IOMMU_READ; 3196 - if (entry->prot & IOMMU_PROT_IW) 3197 - region->prot |= IOMMU_WRITE; 3198 - 3199 3195 list_add_tail(&region->list, head); 3200 3196 } 3197 + 3198 + region = iommu_alloc_resv_region(MSI_RANGE_START, 3199 + MSI_RANGE_END - MSI_RANGE_START + 1, 3200 + 0, IOMMU_RESV_RESERVED); 3201 + if (!region) 3202 + return; 3203 + list_add_tail(&region->list, head); 3204 + 3205 + region = iommu_alloc_resv_region(HT_RANGE_START, 3206 + HT_RANGE_END - HT_RANGE_START + 1, 3207 + 0, IOMMU_RESV_RESERVED); 3208 + if (!region) 3209 + return; 3210 + list_add_tail(&region->list, head); 3201 3211 } 3202 3212 3203 - static void amd_iommu_put_dm_regions(struct device *dev, 3213 + static void amd_iommu_put_resv_regions(struct device *dev, 3204 3214 struct list_head *head) 3205 3215 { 3206 - struct iommu_dm_region *entry, *next; 3216 + struct iommu_resv_region *entry, *next; 3207 3217 3208 3218 list_for_each_entry_safe(entry, next, head, list) 3209 3219 kfree(entry); 3210 3220 } 3211 3221 3212 - static void amd_iommu_apply_dm_region(struct device *dev, 3222 + static void amd_iommu_apply_resv_region(struct device *dev, 3213 3223 struct iommu_domain *domain, 3214 - struct iommu_dm_region *region) 3224 + struct iommu_resv_region *region) 3215 3225 { 3216 3226 struct dma_ops_domain *dma_dom = to_dma_ops_domain(to_pdomain(domain)); 3217 3227 unsigned long start, end; ··· 3237 3217 WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL); 3238 3218 } 3239 3219 3240 - static const struct iommu_ops amd_iommu_ops = { 3220 + const struct iommu_ops amd_iommu_ops = { 3241 3221 .capable = amd_iommu_capable, 3242 3222 .domain_alloc = amd_iommu_domain_alloc, 3243 3223 .domain_free = amd_iommu_domain_free, ··· 3250 3230 .add_device = amd_iommu_add_device, 3251 3231 .remove_device = amd_iommu_remove_device, 3252 3232 .device_group = amd_iommu_device_group, 3253 - .get_dm_regions = amd_iommu_get_dm_regions, 3254 - .put_dm_regions = amd_iommu_put_dm_regions, 3255 - .apply_dm_region = amd_iommu_apply_dm_region, 3233 + .get_resv_regions = amd_iommu_get_resv_regions, 3234 + .put_resv_regions = amd_iommu_put_resv_regions, 3235 + .apply_resv_region = amd_iommu_apply_resv_region, 3256 3236 .pgsize_bitmap = AMD_IOMMU_PGSIZES, 3257 3237 }; 3258 3238
+7 -4
drivers/iommu/amd_iommu_init.c
··· 94 94 * out of it. 95 95 */ 96 96 97 + extern const struct iommu_ops amd_iommu_ops; 98 + 97 99 /* 98 100 * structure describing one IOMMU in the ACPI table. Typically followed by one 99 101 * or more ivhd_entrys. ··· 1637 1635 amd_iommu_erratum_746_workaround(iommu); 1638 1636 amd_iommu_ats_write_check_workaround(iommu); 1639 1637 1640 - iommu->iommu_dev = iommu_device_create(&iommu->dev->dev, iommu, 1641 - amd_iommu_groups, "ivhd%d", 1642 - iommu->index); 1638 + iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev, 1639 + amd_iommu_groups, "ivhd%d", iommu->index); 1640 + iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops); 1641 + iommu_device_register(&iommu->iommu); 1643 1642 1644 1643 return pci_enable_device(iommu->dev); 1645 1644 } ··· 2233 2230 */ 2234 2231 ret = check_ivrs_checksum(ivrs_base); 2235 2232 if (ret) 2236 - return ret; 2233 + goto out; 2237 2234 2238 2235 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base); 2239 2236 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
+2 -2
drivers/iommu/amd_iommu_types.h
··· 535 535 /* if one, we need to send a completion wait command */ 536 536 bool need_sync; 537 537 538 - /* IOMMU sysfs device */ 539 - struct device *iommu_dev; 538 + /* Handle for IOMMU core code */ 539 + struct iommu_device iommu; 540 540 541 541 /* 542 542 * We can't rely on the BIOS to restore all values on reinit, so we
+61 -29
drivers/iommu/arm-smmu-v3.c
··· 269 269 #define STRTAB_STE_1_SHCFG_INCOMING 1UL 270 270 #define STRTAB_STE_1_SHCFG_SHIFT 44 271 271 272 - #define STRTAB_STE_1_PRIVCFG_UNPRIV 2UL 273 - #define STRTAB_STE_1_PRIVCFG_SHIFT 48 274 - 275 272 #define STRTAB_STE_2_S2VMID_SHIFT 0 276 273 #define STRTAB_STE_2_S2VMID_MASK 0xffffUL 277 274 #define STRTAB_STE_2_VTCR_SHIFT 32 ··· 408 411 409 412 /* High-level queue structures */ 410 413 #define ARM_SMMU_POLL_TIMEOUT_US 100 414 + 415 + #define MSI_IOVA_BASE 0x8000000 416 + #define MSI_IOVA_LENGTH 0x100000 411 417 412 418 static bool disable_bypass; 413 419 module_param_named(disable_bypass, disable_bypass, bool, S_IRUGO); ··· 616 616 unsigned int sid_bits; 617 617 618 618 struct arm_smmu_strtab_cfg strtab_cfg; 619 + 620 + /* IOMMU core code handle */ 621 + struct iommu_device iommu; 619 622 }; 620 623 621 624 /* SMMU private data for each master */ ··· 1045 1042 } 1046 1043 } 1047 1044 1048 - /* Nuke the existing Config, as we're going to rewrite it */ 1049 - val &= ~(STRTAB_STE_0_CFG_MASK << STRTAB_STE_0_CFG_SHIFT); 1050 - 1051 - if (ste->valid) 1052 - val |= STRTAB_STE_0_V; 1053 - else 1054 - val &= ~STRTAB_STE_0_V; 1045 + /* Nuke the existing STE_0 value, as we're going to rewrite it */ 1046 + val = ste->valid ? STRTAB_STE_0_V : 0; 1055 1047 1056 1048 if (ste->bypass) { 1057 1049 val |= disable_bypass ? STRTAB_STE_0_CFG_ABORT ··· 1071 1073 #ifdef CONFIG_PCI_ATS 1072 1074 STRTAB_STE_1_EATS_TRANS << STRTAB_STE_1_EATS_SHIFT | 1073 1075 #endif 1074 - STRTAB_STE_1_STRW_NSEL1 << STRTAB_STE_1_STRW_SHIFT | 1075 - STRTAB_STE_1_PRIVCFG_UNPRIV << 1076 - STRTAB_STE_1_PRIVCFG_SHIFT); 1076 + STRTAB_STE_1_STRW_NSEL1 << STRTAB_STE_1_STRW_SHIFT); 1077 1077 1078 1078 if (smmu->features & ARM_SMMU_FEAT_STALLS) 1079 1079 dst[1] |= cpu_to_le64(STRTAB_STE_1_S1STALLD); ··· 1079 1083 val |= (ste->s1_cfg->cdptr_dma & STRTAB_STE_0_S1CTXPTR_MASK 1080 1084 << STRTAB_STE_0_S1CTXPTR_SHIFT) | 1081 1085 STRTAB_STE_0_CFG_S1_TRANS; 1082 - 1083 1086 } 1084 1087 1085 1088 if (ste->s2_cfg) { ··· 1367 1372 switch (cap) { 1368 1373 case IOMMU_CAP_CACHE_COHERENCY: 1369 1374 return true; 1370 - case IOMMU_CAP_INTR_REMAP: 1371 - return true; /* MSIs are just memory writes */ 1372 1375 case IOMMU_CAP_NOEXEC: 1373 1376 return true; 1374 1377 default: ··· 1788 1795 } 1789 1796 1790 1797 group = iommu_group_get_for_dev(dev); 1791 - if (!IS_ERR(group)) 1798 + if (!IS_ERR(group)) { 1792 1799 iommu_group_put(group); 1800 + iommu_device_link(&smmu->iommu, dev); 1801 + } 1793 1802 1794 1803 return PTR_ERR_OR_ZERO(group); 1795 1804 } ··· 1800 1805 { 1801 1806 struct iommu_fwspec *fwspec = dev->iommu_fwspec; 1802 1807 struct arm_smmu_master_data *master; 1808 + struct arm_smmu_device *smmu; 1803 1809 1804 1810 if (!fwspec || fwspec->ops != &arm_smmu_ops) 1805 1811 return; 1806 1812 1807 1813 master = fwspec->iommu_priv; 1814 + smmu = master->smmu; 1808 1815 if (master && master->ste.valid) 1809 1816 arm_smmu_detach_dev(dev); 1810 1817 iommu_group_remove_device(dev); 1818 + iommu_device_unlink(&smmu->iommu, dev); 1811 1819 kfree(master); 1812 1820 iommu_fwspec_free(dev); 1813 1821 } ··· 1881 1883 return iommu_fwspec_add_ids(dev, args->args, 1); 1882 1884 } 1883 1885 1886 + static void arm_smmu_get_resv_regions(struct device *dev, 1887 + struct list_head *head) 1888 + { 1889 + struct iommu_resv_region *region; 1890 + int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO; 1891 + 1892 + region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH, 1893 + prot, IOMMU_RESV_MSI); 1894 + if (!region) 1895 + return; 1896 + 1897 + list_add_tail(&region->list, head); 1898 + } 1899 + 1900 + static void arm_smmu_put_resv_regions(struct device *dev, 1901 + struct list_head *head) 1902 + { 1903 + struct iommu_resv_region *entry, *next; 1904 + 1905 + list_for_each_entry_safe(entry, next, head, list) 1906 + kfree(entry); 1907 + } 1908 + 1884 1909 static struct iommu_ops arm_smmu_ops = { 1885 1910 .capable = arm_smmu_capable, 1886 1911 .domain_alloc = arm_smmu_domain_alloc, ··· 1919 1898 .domain_get_attr = arm_smmu_domain_get_attr, 1920 1899 .domain_set_attr = arm_smmu_domain_set_attr, 1921 1900 .of_xlate = arm_smmu_of_xlate, 1901 + .get_resv_regions = arm_smmu_get_resv_regions, 1902 + .put_resv_regions = arm_smmu_put_resv_regions, 1922 1903 .pgsize_bitmap = -1UL, /* Restricted during device attach */ 1923 1904 }; 1924 1905 ··· 2006 1983 u32 size, l1size; 2007 1984 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; 2008 1985 2009 - /* 2010 - * If we can resolve everything with a single L2 table, then we 2011 - * just need a single L1 descriptor. Otherwise, calculate the L1 2012 - * size, capped to the SIDSIZE. 2013 - */ 2014 - if (smmu->sid_bits < STRTAB_SPLIT) { 2015 - size = 0; 2016 - } else { 2017 - size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3); 2018 - size = min(size, smmu->sid_bits - STRTAB_SPLIT); 2019 - } 1986 + /* Calculate the L1 size, capped to the SIDSIZE. */ 1987 + size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3); 1988 + size = min(size, smmu->sid_bits - STRTAB_SPLIT); 2020 1989 cfg->num_l1_ents = 1 << size; 2021 1990 2022 1991 size += STRTAB_SPLIT; ··· 2519 2504 smmu->ssid_bits = reg >> IDR1_SSID_SHIFT & IDR1_SSID_MASK; 2520 2505 smmu->sid_bits = reg >> IDR1_SID_SHIFT & IDR1_SID_MASK; 2521 2506 2507 + /* 2508 + * If the SMMU supports fewer bits than would fill a single L2 stream 2509 + * table, use a linear table instead. 2510 + */ 2511 + if (smmu->sid_bits <= STRTAB_SPLIT) 2512 + smmu->features &= ~ARM_SMMU_FEAT_2_LVL_STRTAB; 2513 + 2522 2514 /* IDR5 */ 2523 2515 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR5); 2524 2516 ··· 2635 2613 { 2636 2614 int irq, ret; 2637 2615 struct resource *res; 2616 + resource_size_t ioaddr; 2638 2617 struct arm_smmu_device *smmu; 2639 2618 struct device *dev = &pdev->dev; 2640 2619 bool bypass; ··· 2653 2630 dev_err(dev, "MMIO region too small (%pr)\n", res); 2654 2631 return -EINVAL; 2655 2632 } 2633 + ioaddr = res->start; 2656 2634 2657 2635 smmu->base = devm_ioremap_resource(dev, res); 2658 2636 if (IS_ERR(smmu->base)) ··· 2706 2682 return ret; 2707 2683 2708 2684 /* And we're up. Go go go! */ 2709 - iommu_register_instance(dev->fwnode, &arm_smmu_ops); 2685 + ret = iommu_device_sysfs_add(&smmu->iommu, dev, NULL, 2686 + "smmu3.%pa", &ioaddr); 2687 + if (ret) 2688 + return ret; 2689 + 2690 + iommu_device_set_ops(&smmu->iommu, &arm_smmu_ops); 2691 + iommu_device_set_fwnode(&smmu->iommu, dev->fwnode); 2692 + 2693 + ret = iommu_device_register(&smmu->iommu); 2710 2694 2711 2695 #ifdef CONFIG_PCI 2712 2696 if (pci_bus_type.iommu_ops != &arm_smmu_ops) {
+110 -25
drivers/iommu/arm-smmu.c
··· 24 24 * - v7/v8 long-descriptor format 25 25 * - Non-secure access to the SMMU 26 26 * - Context fault reporting 27 + * - Extended Stream ID (16 bit) 27 28 */ 28 29 29 30 #define pr_fmt(fmt) "arm-smmu: " fmt ··· 88 87 #define sCR0_CLIENTPD (1 << 0) 89 88 #define sCR0_GFRE (1 << 1) 90 89 #define sCR0_GFIE (1 << 2) 90 + #define sCR0_EXIDENABLE (1 << 3) 91 91 #define sCR0_GCFGFRE (1 << 4) 92 92 #define sCR0_GCFGFIE (1 << 5) 93 93 #define sCR0_USFCFG (1 << 10) ··· 128 126 #define ID0_NUMIRPT_MASK 0xff 129 127 #define ID0_NUMSIDB_SHIFT 9 130 128 #define ID0_NUMSIDB_MASK 0xf 129 + #define ID0_EXIDS (1 << 8) 131 130 #define ID0_NUMSMRG_SHIFT 0 132 131 #define ID0_NUMSMRG_MASK 0xff 133 132 ··· 172 169 #define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2)) 173 170 #define S2CR_CBNDX_SHIFT 0 174 171 #define S2CR_CBNDX_MASK 0xff 172 + #define S2CR_EXIDVALID (1 << 10) 175 173 #define S2CR_TYPE_SHIFT 16 176 174 #define S2CR_TYPE_MASK 0x3 177 175 enum arm_smmu_s2cr_type { ··· 264 260 265 261 #define TTBCR2_SEP_SHIFT 15 266 262 #define TTBCR2_SEP_UPSTREAM (0x7 << TTBCR2_SEP_SHIFT) 263 + #define TTBCR2_AS (1 << 4) 267 264 268 265 #define TTBRn_ASID_SHIFT 48 269 266 ··· 285 280 FSR_EF | FSR_PF | FSR_TF | FSR_IGN) 286 281 287 282 #define FSYNR0_WNR (1 << 4) 283 + 284 + #define MSI_IOVA_BASE 0x8000000 285 + #define MSI_IOVA_LENGTH 0x100000 288 286 289 287 static int force_stage; 290 288 module_param(force_stage, int, S_IRUGO); ··· 359 351 #define ARM_SMMU_FEAT_FMT_AARCH64_64K (1 << 9) 360 352 #define ARM_SMMU_FEAT_FMT_AARCH32_L (1 << 10) 361 353 #define ARM_SMMU_FEAT_FMT_AARCH32_S (1 << 11) 354 + #define ARM_SMMU_FEAT_EXIDS (1 << 12) 362 355 u32 features; 363 356 364 357 #define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0) ··· 389 380 unsigned int *irqs; 390 381 391 382 u32 cavium_id_base; /* Specific to Cavium */ 383 + 384 + /* IOMMU core code handle */ 385 + struct iommu_device iommu; 392 386 }; 393 387 394 388 enum arm_smmu_context_fmt { ··· 790 778 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr; 791 779 reg2 = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32; 792 780 reg2 |= TTBCR2_SEP_UPSTREAM; 781 + if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) 782 + reg2 |= TTBCR2_AS; 793 783 } 794 784 if (smmu->version > ARM_SMMU_V1) 795 785 writel_relaxed(reg2, cb_base + ARM_SMMU_CB_TTBCR2); ··· 1062 1048 struct arm_smmu_smr *smr = smmu->smrs + idx; 1063 1049 u32 reg = smr->id << SMR_ID_SHIFT | smr->mask << SMR_MASK_SHIFT; 1064 1050 1065 - if (smr->valid) 1051 + if (!(smmu->features & ARM_SMMU_FEAT_EXIDS) && smr->valid) 1066 1052 reg |= SMR_VALID; 1067 1053 writel_relaxed(reg, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_SMR(idx)); 1068 1054 } ··· 1074 1060 (s2cr->cbndx & S2CR_CBNDX_MASK) << S2CR_CBNDX_SHIFT | 1075 1061 (s2cr->privcfg & S2CR_PRIVCFG_MASK) << S2CR_PRIVCFG_SHIFT; 1076 1062 1063 + if (smmu->features & ARM_SMMU_FEAT_EXIDS && smmu->smrs && 1064 + smmu->smrs[idx].valid) 1065 + reg |= S2CR_EXIDVALID; 1077 1066 writel_relaxed(reg, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_S2CR(idx)); 1078 1067 } 1079 1068 ··· 1085 1068 arm_smmu_write_s2cr(smmu, idx); 1086 1069 if (smmu->smrs) 1087 1070 arm_smmu_write_smr(smmu, idx); 1071 + } 1072 + 1073 + /* 1074 + * The width of SMR's mask field depends on sCR0_EXIDENABLE, so this function 1075 + * should be called after sCR0 is written. 1076 + */ 1077 + static void arm_smmu_test_smr_masks(struct arm_smmu_device *smmu) 1078 + { 1079 + void __iomem *gr0_base = ARM_SMMU_GR0(smmu); 1080 + u32 smr; 1081 + 1082 + if (!smmu->smrs) 1083 + return; 1084 + 1085 + /* 1086 + * SMR.ID bits may not be preserved if the corresponding MASK 1087 + * bits are set, so check each one separately. We can reject 1088 + * masters later if they try to claim IDs outside these masks. 1089 + */ 1090 + smr = smmu->streamid_mask << SMR_ID_SHIFT; 1091 + writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0)); 1092 + smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0)); 1093 + smmu->streamid_mask = smr >> SMR_ID_SHIFT; 1094 + 1095 + smr = smmu->streamid_mask << SMR_MASK_SHIFT; 1096 + writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0)); 1097 + smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0)); 1098 + smmu->smr_mask_mask = smr >> SMR_MASK_SHIFT; 1088 1099 } 1089 1100 1090 1101 static int arm_smmu_find_sme(struct arm_smmu_device *smmu, u16 id, u16 mask) ··· 1259 1214 continue; 1260 1215 1261 1216 s2cr[idx].type = type; 1262 - s2cr[idx].privcfg = S2CR_PRIVCFG_UNPRIV; 1217 + s2cr[idx].privcfg = S2CR_PRIVCFG_DEFAULT; 1263 1218 s2cr[idx].cbndx = cbndx; 1264 1219 arm_smmu_write_s2cr(smmu, idx); 1265 1220 } ··· 1416 1371 * requests. 1417 1372 */ 1418 1373 return true; 1419 - case IOMMU_CAP_INTR_REMAP: 1420 - return true; /* MSIs are just memory writes */ 1421 1374 case IOMMU_CAP_NOEXEC: 1422 1375 return true; 1423 1376 default: ··· 1487 1444 if (ret) 1488 1445 goto out_free; 1489 1446 1447 + iommu_device_link(&smmu->iommu, dev); 1448 + 1490 1449 return 0; 1491 1450 1492 1451 out_free: ··· 1501 1456 static void arm_smmu_remove_device(struct device *dev) 1502 1457 { 1503 1458 struct iommu_fwspec *fwspec = dev->iommu_fwspec; 1459 + struct arm_smmu_master_cfg *cfg; 1460 + struct arm_smmu_device *smmu; 1461 + 1504 1462 1505 1463 if (!fwspec || fwspec->ops != &arm_smmu_ops) 1506 1464 return; 1507 1465 1466 + cfg = fwspec->iommu_priv; 1467 + smmu = cfg->smmu; 1468 + 1469 + iommu_device_unlink(&smmu->iommu, dev); 1508 1470 arm_smmu_master_free_smes(fwspec); 1509 1471 iommu_group_remove_device(dev); 1510 1472 kfree(fwspec->iommu_priv); ··· 1601 1549 return iommu_fwspec_add_ids(dev, &fwid, 1); 1602 1550 } 1603 1551 1552 + static void arm_smmu_get_resv_regions(struct device *dev, 1553 + struct list_head *head) 1554 + { 1555 + struct iommu_resv_region *region; 1556 + int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO; 1557 + 1558 + region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH, 1559 + prot, IOMMU_RESV_MSI); 1560 + if (!region) 1561 + return; 1562 + 1563 + list_add_tail(&region->list, head); 1564 + } 1565 + 1566 + static void arm_smmu_put_resv_regions(struct device *dev, 1567 + struct list_head *head) 1568 + { 1569 + struct iommu_resv_region *entry, *next; 1570 + 1571 + list_for_each_entry_safe(entry, next, head, list) 1572 + kfree(entry); 1573 + } 1574 + 1604 1575 static struct iommu_ops arm_smmu_ops = { 1605 1576 .capable = arm_smmu_capable, 1606 1577 .domain_alloc = arm_smmu_domain_alloc, ··· 1639 1564 .domain_get_attr = arm_smmu_domain_get_attr, 1640 1565 .domain_set_attr = arm_smmu_domain_set_attr, 1641 1566 .of_xlate = arm_smmu_of_xlate, 1567 + .get_resv_regions = arm_smmu_get_resv_regions, 1568 + .put_resv_regions = arm_smmu_put_resv_regions, 1642 1569 .pgsize_bitmap = -1UL, /* Restricted during device attach */ 1643 1570 }; 1644 1571 ··· 1724 1647 1725 1648 if (smmu->features & ARM_SMMU_FEAT_VMID16) 1726 1649 reg |= sCR0_VMID16EN; 1650 + 1651 + if (smmu->features & ARM_SMMU_FEAT_EXIDS) 1652 + reg |= sCR0_EXIDENABLE; 1727 1653 1728 1654 /* Push the button */ 1729 1655 __arm_smmu_tlb_sync(smmu); ··· 1815 1735 "\t(IDR0.CTTW overridden by FW configuration)\n"); 1816 1736 1817 1737 /* Max. number of entries we have for stream matching/indexing */ 1818 - size = 1 << ((id >> ID0_NUMSIDB_SHIFT) & ID0_NUMSIDB_MASK); 1738 + if (smmu->version == ARM_SMMU_V2 && id & ID0_EXIDS) { 1739 + smmu->features |= ARM_SMMU_FEAT_EXIDS; 1740 + size = 1 << 16; 1741 + } else { 1742 + size = 1 << ((id >> ID0_NUMSIDB_SHIFT) & ID0_NUMSIDB_MASK); 1743 + } 1819 1744 smmu->streamid_mask = size - 1; 1820 1745 if (id & ID0_SMS) { 1821 - u32 smr; 1822 - 1823 1746 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH; 1824 1747 size = (id >> ID0_NUMSMRG_SHIFT) & ID0_NUMSMRG_MASK; 1825 1748 if (size == 0) { ··· 1831 1748 return -ENODEV; 1832 1749 } 1833 1750 1834 - /* 1835 - * SMR.ID bits may not be preserved if the corresponding MASK 1836 - * bits are set, so check each one separately. We can reject 1837 - * masters later if they try to claim IDs outside these masks. 1838 - */ 1839 - smr = smmu->streamid_mask << SMR_ID_SHIFT; 1840 - writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0)); 1841 - smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0)); 1842 - smmu->streamid_mask = smr >> SMR_ID_SHIFT; 1843 - 1844 - smr = smmu->streamid_mask << SMR_MASK_SHIFT; 1845 - writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0)); 1846 - smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0)); 1847 - smmu->smr_mask_mask = smr >> SMR_MASK_SHIFT; 1848 - 1849 1751 /* Zero-initialised to mark as invalid */ 1850 1752 smmu->smrs = devm_kcalloc(smmu->dev, size, sizeof(*smmu->smrs), 1851 1753 GFP_KERNEL); ··· 1838 1770 return -ENOMEM; 1839 1771 1840 1772 dev_notice(smmu->dev, 1841 - "\tstream matching with %lu register groups, mask 0x%x", 1842 - size, smmu->smr_mask_mask); 1773 + "\tstream matching with %lu register groups", size); 1843 1774 } 1844 1775 /* s2cr->type == 0 means translation, so initialise explicitly */ 1845 1776 smmu->s2crs = devm_kmalloc_array(smmu->dev, size, sizeof(*smmu->s2crs), ··· 2078 2011 static int arm_smmu_device_probe(struct platform_device *pdev) 2079 2012 { 2080 2013 struct resource *res; 2014 + resource_size_t ioaddr; 2081 2015 struct arm_smmu_device *smmu; 2082 2016 struct device *dev = &pdev->dev; 2083 2017 int num_irqs, i, err; ··· 2099 2031 return err; 2100 2032 2101 2033 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2034 + ioaddr = res->start; 2102 2035 smmu->base = devm_ioremap_resource(dev, res); 2103 2036 if (IS_ERR(smmu->base)) 2104 2037 return PTR_ERR(smmu->base); ··· 2160 2091 } 2161 2092 } 2162 2093 2163 - iommu_register_instance(dev->fwnode, &arm_smmu_ops); 2094 + err = iommu_device_sysfs_add(&smmu->iommu, smmu->dev, NULL, 2095 + "smmu.%pa", &ioaddr); 2096 + if (err) { 2097 + dev_err(dev, "Failed to register iommu in sysfs\n"); 2098 + return err; 2099 + } 2100 + 2101 + iommu_device_set_ops(&smmu->iommu, &arm_smmu_ops); 2102 + iommu_device_set_fwnode(&smmu->iommu, dev->fwnode); 2103 + 2104 + err = iommu_device_register(&smmu->iommu); 2105 + if (err) { 2106 + dev_err(dev, "Failed to register iommu\n"); 2107 + return err; 2108 + } 2109 + 2164 2110 platform_set_drvdata(pdev, smmu); 2165 2111 arm_smmu_device_reset(smmu); 2112 + arm_smmu_test_smr_masks(smmu); 2166 2113 2167 2114 /* Oh, for a proper bus abstraction */ 2168 2115 if (!iommu_present(&platform_bus_type))
+141 -50
drivers/iommu/dma-iommu.c
··· 37 37 phys_addr_t phys; 38 38 }; 39 39 40 - struct iommu_dma_cookie { 41 - struct iova_domain iovad; 42 - struct list_head msi_page_list; 43 - spinlock_t msi_lock; 40 + enum iommu_dma_cookie_type { 41 + IOMMU_DMA_IOVA_COOKIE, 42 + IOMMU_DMA_MSI_COOKIE, 44 43 }; 44 + 45 + struct iommu_dma_cookie { 46 + enum iommu_dma_cookie_type type; 47 + union { 48 + /* Full allocator for IOMMU_DMA_IOVA_COOKIE */ 49 + struct iova_domain iovad; 50 + /* Trivial linear page allocator for IOMMU_DMA_MSI_COOKIE */ 51 + dma_addr_t msi_iova; 52 + }; 53 + struct list_head msi_page_list; 54 + spinlock_t msi_lock; 55 + }; 56 + 57 + static inline size_t cookie_msi_granule(struct iommu_dma_cookie *cookie) 58 + { 59 + if (cookie->type == IOMMU_DMA_IOVA_COOKIE) 60 + return cookie->iovad.granule; 61 + return PAGE_SIZE; 62 + } 45 63 46 64 static inline struct iova_domain *cookie_iovad(struct iommu_domain *domain) 47 65 { 48 - return &((struct iommu_dma_cookie *)domain->iova_cookie)->iovad; 66 + struct iommu_dma_cookie *cookie = domain->iova_cookie; 67 + 68 + if (cookie->type == IOMMU_DMA_IOVA_COOKIE) 69 + return &cookie->iovad; 70 + return NULL; 71 + } 72 + 73 + static struct iommu_dma_cookie *cookie_alloc(enum iommu_dma_cookie_type type) 74 + { 75 + struct iommu_dma_cookie *cookie; 76 + 77 + cookie = kzalloc(sizeof(*cookie), GFP_KERNEL); 78 + if (cookie) { 79 + spin_lock_init(&cookie->msi_lock); 80 + INIT_LIST_HEAD(&cookie->msi_page_list); 81 + cookie->type = type; 82 + } 83 + return cookie; 49 84 } 50 85 51 86 int iommu_dma_init(void) ··· 97 62 */ 98 63 int iommu_get_dma_cookie(struct iommu_domain *domain) 99 64 { 100 - struct iommu_dma_cookie *cookie; 101 - 102 65 if (domain->iova_cookie) 103 66 return -EEXIST; 104 67 105 - cookie = kzalloc(sizeof(*cookie), GFP_KERNEL); 106 - if (!cookie) 68 + domain->iova_cookie = cookie_alloc(IOMMU_DMA_IOVA_COOKIE); 69 + if (!domain->iova_cookie) 107 70 return -ENOMEM; 108 71 109 - spin_lock_init(&cookie->msi_lock); 110 - INIT_LIST_HEAD(&cookie->msi_page_list); 111 - domain->iova_cookie = cookie; 112 72 return 0; 113 73 } 114 74 EXPORT_SYMBOL(iommu_get_dma_cookie); 115 75 116 76 /** 77 + * iommu_get_msi_cookie - Acquire just MSI remapping resources 78 + * @domain: IOMMU domain to prepare 79 + * @base: Start address of IOVA region for MSI mappings 80 + * 81 + * Users who manage their own IOVA allocation and do not want DMA API support, 82 + * but would still like to take advantage of automatic MSI remapping, can use 83 + * this to initialise their own domain appropriately. Users should reserve a 84 + * contiguous IOVA region, starting at @base, large enough to accommodate the 85 + * number of PAGE_SIZE mappings necessary to cover every MSI doorbell address 86 + * used by the devices attached to @domain. 87 + */ 88 + int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base) 89 + { 90 + struct iommu_dma_cookie *cookie; 91 + 92 + if (domain->type != IOMMU_DOMAIN_UNMANAGED) 93 + return -EINVAL; 94 + 95 + if (domain->iova_cookie) 96 + return -EEXIST; 97 + 98 + cookie = cookie_alloc(IOMMU_DMA_MSI_COOKIE); 99 + if (!cookie) 100 + return -ENOMEM; 101 + 102 + cookie->msi_iova = base; 103 + domain->iova_cookie = cookie; 104 + return 0; 105 + } 106 + EXPORT_SYMBOL(iommu_get_msi_cookie); 107 + 108 + /** 117 109 * iommu_put_dma_cookie - Release a domain's DMA mapping resources 118 - * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie() 110 + * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie() or 111 + * iommu_get_msi_cookie() 119 112 * 120 113 * IOMMU drivers should normally call this from their domain_free callback. 121 114 */ ··· 155 92 if (!cookie) 156 93 return; 157 94 158 - if (cookie->iovad.granule) 95 + if (cookie->type == IOMMU_DMA_IOVA_COOKIE && cookie->iovad.granule) 159 96 put_iova_domain(&cookie->iovad); 160 97 161 98 list_for_each_entry_safe(msi, tmp, &cookie->msi_page_list, list) { ··· 200 137 int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base, 201 138 u64 size, struct device *dev) 202 139 { 203 - struct iova_domain *iovad = cookie_iovad(domain); 140 + struct iommu_dma_cookie *cookie = domain->iova_cookie; 141 + struct iova_domain *iovad = &cookie->iovad; 204 142 unsigned long order, base_pfn, end_pfn; 143 + bool pci = dev && dev_is_pci(dev); 205 144 206 - if (!iovad) 207 - return -ENODEV; 145 + if (!cookie || cookie->type != IOMMU_DMA_IOVA_COOKIE) 146 + return -EINVAL; 208 147 209 148 /* Use the smallest supported page size for IOVA granularity */ 210 149 order = __ffs(domain->pgsize_bitmap); ··· 226 161 end_pfn = min_t(unsigned long, end_pfn, 227 162 domain->geometry.aperture_end >> order); 228 163 } 164 + /* 165 + * PCI devices may have larger DMA masks, but still prefer allocating 166 + * within a 32-bit mask to avoid DAC addressing. Such limitations don't 167 + * apply to the typical platform device, so for those we may as well 168 + * leave the cache limit at the top of their range to save an rb_last() 169 + * traversal on every allocation. 170 + */ 171 + if (pci) 172 + end_pfn &= DMA_BIT_MASK(32) >> order; 229 173 230 - /* All we can safely do with an existing domain is enlarge it */ 174 + /* start_pfn is always nonzero for an already-initialised domain */ 231 175 if (iovad->start_pfn) { 232 176 if (1UL << order != iovad->granule || 233 - base_pfn != iovad->start_pfn || 234 - end_pfn < iovad->dma_32bit_pfn) { 177 + base_pfn != iovad->start_pfn) { 235 178 pr_warn("Incompatible range for DMA domain\n"); 236 179 return -EFAULT; 237 180 } 238 - iovad->dma_32bit_pfn = end_pfn; 181 + /* 182 + * If we have devices with different DMA masks, move the free 183 + * area cache limit down for the benefit of the smaller one. 184 + */ 185 + iovad->dma_32bit_pfn = min(end_pfn, iovad->dma_32bit_pfn); 239 186 } else { 240 187 init_iova_domain(iovad, 1UL << order, base_pfn, end_pfn); 241 - if (dev && dev_is_pci(dev)) 188 + if (pci) 242 189 iova_reserve_pci_windows(to_pci_dev(dev), iovad); 243 190 } 244 191 return 0; ··· 258 181 EXPORT_SYMBOL(iommu_dma_init_domain); 259 182 260 183 /** 261 - * dma_direction_to_prot - Translate DMA API directions to IOMMU API page flags 184 + * dma_info_to_prot - Translate DMA API directions and attributes to IOMMU API 185 + * page flags. 262 186 * @dir: Direction of DMA transfer 263 187 * @coherent: Is the DMA master cache-coherent? 188 + * @attrs: DMA attributes for the mapping 264 189 * 265 190 * Return: corresponding IOMMU API page protection flags 266 191 */ 267 - int dma_direction_to_prot(enum dma_data_direction dir, bool coherent) 192 + int dma_info_to_prot(enum dma_data_direction dir, bool coherent, 193 + unsigned long attrs) 268 194 { 269 195 int prot = coherent ? IOMMU_CACHE : 0; 196 + 197 + if (attrs & DMA_ATTR_PRIVILEGED) 198 + prot |= IOMMU_PRIV; 270 199 271 200 switch (dir) { 272 201 case DMA_BIDIRECTIONAL: ··· 287 204 } 288 205 289 206 static struct iova *__alloc_iova(struct iommu_domain *domain, size_t size, 290 - dma_addr_t dma_limit) 207 + dma_addr_t dma_limit, struct device *dev) 291 208 { 292 209 struct iova_domain *iovad = cookie_iovad(domain); 293 210 unsigned long shift = iova_shift(iovad); 294 211 unsigned long length = iova_align(iovad, size) >> shift; 212 + struct iova *iova = NULL; 295 213 296 214 if (domain->geometry.force_aperture) 297 215 dma_limit = min(dma_limit, domain->geometry.aperture_end); 216 + 217 + /* Try to get PCI devices a SAC address */ 218 + if (dma_limit > DMA_BIT_MASK(32) && dev_is_pci(dev)) 219 + iova = alloc_iova(iovad, length, DMA_BIT_MASK(32) >> shift, 220 + true); 298 221 /* 299 222 * Enforce size-alignment to be safe - there could perhaps be an 300 223 * attribute to control this per-device, or at least per-domain... 301 224 */ 302 - return alloc_iova(iovad, length, dma_limit >> shift, true); 225 + if (!iova) 226 + iova = alloc_iova(iovad, length, dma_limit >> shift, true); 227 + 228 + return iova; 303 229 } 304 230 305 231 /* The IOVA allocator knows what we mapped, so just unmap whatever that was */ ··· 461 369 if (!pages) 462 370 return NULL; 463 371 464 - iova = __alloc_iova(domain, size, dev->coherent_dma_mask); 372 + iova = __alloc_iova(domain, size, dev->coherent_dma_mask, dev); 465 373 if (!iova) 466 374 goto out_free_pages; 467 375 ··· 532 440 struct iova_domain *iovad = cookie_iovad(domain); 533 441 size_t iova_off = iova_offset(iovad, phys); 534 442 size_t len = iova_align(iovad, size + iova_off); 535 - struct iova *iova = __alloc_iova(domain, len, dma_get_mask(dev)); 443 + struct iova *iova = __alloc_iova(domain, len, dma_get_mask(dev), dev); 536 444 537 445 if (!iova) 538 446 return DMA_ERROR_CODE; ··· 690 598 prev = s; 691 599 } 692 600 693 - iova = __alloc_iova(domain, iova_len, dma_get_mask(dev)); 601 + iova = __alloc_iova(domain, iova_len, dma_get_mask(dev), dev); 694 602 if (!iova) 695 603 goto out_restore_sg; 696 604 ··· 725 633 size_t size, enum dma_data_direction dir, unsigned long attrs) 726 634 { 727 635 return __iommu_dma_map(dev, phys, size, 728 - dma_direction_to_prot(dir, false) | IOMMU_MMIO); 636 + dma_info_to_prot(dir, false, attrs) | IOMMU_MMIO); 729 637 } 730 638 731 639 void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle, 732 640 size_t size, enum dma_data_direction dir, unsigned long attrs) 733 641 { 734 642 __iommu_dma_unmap(iommu_get_domain_for_dev(dev), handle); 735 - } 736 - 737 - int iommu_dma_supported(struct device *dev, u64 mask) 738 - { 739 - /* 740 - * 'Special' IOMMUs which don't have the same addressing capability 741 - * as the CPU will have to wait until we have some way to query that 742 - * before they'll be able to use this framework. 743 - */ 744 - return 1; 745 643 } 746 644 747 645 int iommu_dma_mapping_error(struct device *dev, dma_addr_t dma_addr) ··· 744 662 { 745 663 struct iommu_dma_cookie *cookie = domain->iova_cookie; 746 664 struct iommu_dma_msi_page *msi_page; 747 - struct iova_domain *iovad = &cookie->iovad; 665 + struct iova_domain *iovad = cookie_iovad(domain); 748 666 struct iova *iova; 749 667 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO; 668 + size_t size = cookie_msi_granule(cookie); 750 669 751 - msi_addr &= ~(phys_addr_t)iova_mask(iovad); 670 + msi_addr &= ~(phys_addr_t)(size - 1); 752 671 list_for_each_entry(msi_page, &cookie->msi_page_list, list) 753 672 if (msi_page->phys == msi_addr) 754 673 return msi_page; ··· 758 675 if (!msi_page) 759 676 return NULL; 760 677 761 - iova = __alloc_iova(domain, iovad->granule, dma_get_mask(dev)); 762 - if (!iova) 763 - goto out_free_page; 764 - 765 678 msi_page->phys = msi_addr; 766 - msi_page->iova = iova_dma_addr(iovad, iova); 767 - if (iommu_map(domain, msi_page->iova, msi_addr, iovad->granule, prot)) 679 + if (iovad) { 680 + iova = __alloc_iova(domain, size, dma_get_mask(dev), dev); 681 + if (!iova) 682 + goto out_free_page; 683 + msi_page->iova = iova_dma_addr(iovad, iova); 684 + } else { 685 + msi_page->iova = cookie->msi_iova; 686 + cookie->msi_iova += size; 687 + } 688 + 689 + if (iommu_map(domain, msi_page->iova, msi_addr, size, prot)) 768 690 goto out_free_iova; 769 691 770 692 INIT_LIST_HEAD(&msi_page->list); ··· 777 689 return msi_page; 778 690 779 691 out_free_iova: 780 - __free_iova(iovad, iova); 692 + if (iovad) 693 + __free_iova(iovad, iova); 694 + else 695 + cookie->msi_iova -= size; 781 696 out_free_page: 782 697 kfree(msi_page); 783 698 return NULL; ··· 821 730 msg->data = ~0U; 822 731 } else { 823 732 msg->address_hi = upper_32_bits(msi_page->iova); 824 - msg->address_lo &= iova_mask(&cookie->iovad); 733 + msg->address_lo &= cookie_msi_granule(cookie) - 1; 825 734 msg->address_lo += lower_32_bits(msi_page->iova); 826 735 } 827 736 }
+14 -8
drivers/iommu/dmar.c
··· 74 74 static int alloc_iommu(struct dmar_drhd_unit *drhd); 75 75 static void free_iommu(struct intel_iommu *iommu); 76 76 77 + extern const struct iommu_ops intel_iommu_ops; 78 + 77 79 static void dmar_register_drhd_unit(struct dmar_drhd_unit *drhd) 78 80 { 79 81 /* ··· 1080 1078 raw_spin_lock_init(&iommu->register_lock); 1081 1079 1082 1080 if (intel_iommu_enabled) { 1083 - iommu->iommu_dev = iommu_device_create(NULL, iommu, 1084 - intel_iommu_groups, 1085 - "%s", iommu->name); 1086 - 1087 - if (IS_ERR(iommu->iommu_dev)) { 1088 - err = PTR_ERR(iommu->iommu_dev); 1081 + err = iommu_device_sysfs_add(&iommu->iommu, NULL, 1082 + intel_iommu_groups, 1083 + "%s", iommu->name); 1084 + if (err) 1089 1085 goto err_unmap; 1090 - } 1086 + 1087 + iommu_device_set_ops(&iommu->iommu, &intel_iommu_ops); 1088 + 1089 + err = iommu_device_register(&iommu->iommu); 1090 + if (err) 1091 + goto err_unmap; 1091 1092 } 1092 1093 1093 1094 drhd->iommu = iommu; ··· 1108 1103 1109 1104 static void free_iommu(struct intel_iommu *iommu) 1110 1105 { 1111 - iommu_device_destroy(iommu->iommu_dev); 1106 + iommu_device_sysfs_remove(&iommu->iommu); 1107 + iommu_device_unregister(&iommu->iommu); 1112 1108 1113 1109 if (iommu->irq) { 1114 1110 if (iommu->pr_irq) {
+47 -8
drivers/iommu/exynos-iommu.c
··· 276 276 struct list_head owner_node; /* node for owner controllers list */ 277 277 phys_addr_t pgtable; /* assigned page table structure */ 278 278 unsigned int version; /* our version */ 279 + 280 + struct iommu_device iommu; /* IOMMU core handle */ 279 281 }; 280 282 281 283 static struct exynos_iommu_domain *to_exynos_domain(struct iommu_domain *dom) ··· 383 381 { 384 382 sysmmu_pte_t *ent; 385 383 386 - dev_err(data->sysmmu, "%s FAULT occurred at %#x (page table base: %pa)\n", 387 - finfo->name, fault_addr, &data->pgtable); 384 + dev_err(data->sysmmu, "%s: %s FAULT occurred at %#x\n", 385 + dev_name(data->master), finfo->name, fault_addr); 386 + dev_dbg(data->sysmmu, "Page table base: %pa\n", &data->pgtable); 388 387 ent = section_entry(phys_to_virt(data->pgtable), fault_addr); 389 - dev_err(data->sysmmu, "\tLv1 entry: %#x\n", *ent); 388 + dev_dbg(data->sysmmu, "\tLv1 entry: %#x\n", *ent); 390 389 if (lv1ent_page(ent)) { 391 390 ent = page_entry(ent, fault_addr); 392 - dev_err(data->sysmmu, "\t Lv2 entry: %#x\n", *ent); 391 + dev_dbg(data->sysmmu, "\t Lv2 entry: %#x\n", *ent); 393 392 } 394 393 } 395 394 ··· 614 611 data->sysmmu = dev; 615 612 spin_lock_init(&data->lock); 616 613 614 + ret = iommu_device_sysfs_add(&data->iommu, &pdev->dev, NULL, 615 + dev_name(data->sysmmu)); 616 + if (ret) 617 + return ret; 618 + 619 + iommu_device_set_ops(&data->iommu, &exynos_iommu_ops); 620 + iommu_device_set_fwnode(&data->iommu, &dev->of_node->fwnode); 621 + 622 + ret = iommu_device_register(&data->iommu); 623 + if (ret) 624 + return ret; 625 + 617 626 platform_set_drvdata(pdev, data); 618 627 619 628 __sysmmu_get_version(data); ··· 642 627 } 643 628 644 629 pm_runtime_enable(dev); 645 - 646 - of_iommu_set_ops(dev->of_node, &exynos_iommu_ops); 647 630 648 631 return 0; 649 632 } ··· 756 743 DMA_TO_DEVICE); 757 744 /* For mapping page table entries we rely on dma == phys */ 758 745 BUG_ON(handle != virt_to_phys(domain->pgtable)); 746 + if (dma_mapping_error(dma_dev, handle)) 747 + goto err_lv2ent; 759 748 760 749 spin_lock_init(&domain->lock); 761 750 spin_lock_init(&domain->pgtablelock); ··· 769 754 770 755 return &domain->domain; 771 756 757 + err_lv2ent: 758 + free_pages((unsigned long)domain->lv2entcnt, 1); 772 759 err_counter: 773 760 free_pages((unsigned long)domain->pgtable, 2); 774 761 err_dma_cookie: ··· 914 897 } 915 898 916 899 if (lv1ent_fault(sent)) { 900 + dma_addr_t handle; 917 901 sysmmu_pte_t *pent; 918 902 bool need_flush_flpd_cache = lv1ent_zero(sent); 919 903 ··· 926 908 update_pte(sent, mk_lv1ent_page(virt_to_phys(pent))); 927 909 kmemleak_ignore(pent); 928 910 *pgcounter = NUM_LV2ENTRIES; 929 - dma_map_single(dma_dev, pent, LV2TABLE_SIZE, DMA_TO_DEVICE); 911 + handle = dma_map_single(dma_dev, pent, LV2TABLE_SIZE, 912 + DMA_TO_DEVICE); 913 + if (dma_mapping_error(dma_dev, handle)) { 914 + kmem_cache_free(lv2table_kmem_cache, pent); 915 + return ERR_PTR(-EADDRINUSE); 916 + } 930 917 931 918 /* 932 919 * If pre-fetched SLPD is a faulty SLPD in zero_l2_table, ··· 1254 1231 1255 1232 static void exynos_iommu_remove_device(struct device *dev) 1256 1233 { 1234 + struct exynos_iommu_owner *owner = dev->archdata.iommu; 1235 + 1257 1236 if (!has_sysmmu(dev)) 1258 1237 return; 1259 1238 1239 + if (owner->domain) { 1240 + struct iommu_group *group = iommu_group_get(dev); 1241 + 1242 + if (group) { 1243 + WARN_ON(owner->domain != 1244 + iommu_group_default_domain(group)); 1245 + exynos_iommu_detach_device(owner->domain, dev); 1246 + iommu_group_put(group); 1247 + } 1248 + } 1260 1249 iommu_group_remove_device(dev); 1261 1250 } 1262 1251 ··· 1277 1242 { 1278 1243 struct exynos_iommu_owner *owner = dev->archdata.iommu; 1279 1244 struct platform_device *sysmmu = of_find_device_by_node(spec->np); 1280 - struct sysmmu_drvdata *data; 1245 + struct sysmmu_drvdata *data, *entry; 1281 1246 1282 1247 if (!sysmmu) 1283 1248 return -ENODEV; ··· 1295 1260 mutex_init(&owner->rpm_lock); 1296 1261 dev->archdata.iommu = owner; 1297 1262 } 1263 + 1264 + list_for_each_entry(entry, &owner->controllers, owner_node) 1265 + if (entry == data) 1266 + return 0; 1298 1267 1299 1268 list_add_tail(&data->owner_node, &owner->controllers); 1300 1269 data->master = dev;
+88 -28
drivers/iommu/intel-iommu.c
··· 440 440 u64 end_address; /* reserved end address */ 441 441 struct dmar_dev_scope *devices; /* target devices */ 442 442 int devices_cnt; /* target device count */ 443 + struct iommu_resv_region *resv; /* reserved region handle */ 443 444 }; 444 445 445 446 struct dmar_atsr_unit { ··· 548 547 static DEFINE_SPINLOCK(device_domain_lock); 549 548 static LIST_HEAD(device_domain_list); 550 549 551 - static const struct iommu_ops intel_iommu_ops; 550 + const struct iommu_ops intel_iommu_ops; 552 551 553 552 static bool translation_pre_enabled(struct intel_iommu *iommu) 554 553 { ··· 1145 1144 if (!dma_pte_present(pte) || dma_pte_superpage(pte)) 1146 1145 goto next; 1147 1146 1148 - level_pfn = pfn & level_mask(level - 1); 1147 + level_pfn = pfn & level_mask(level); 1149 1148 level_pte = phys_to_virt(dma_pte_addr(pte)); 1150 1149 1151 1150 if (level > 2) ··· 3326 3325 iommu_identity_mapping |= IDENTMAP_GFX; 3327 3326 #endif 3328 3327 3328 + check_tylersburg_isoch(); 3329 + 3329 3330 if (iommu_identity_mapping) { 3330 3331 ret = si_domain_init(hw_pass_through); 3331 3332 if (ret) 3332 3333 goto free_iommu; 3333 3334 } 3334 3335 3335 - check_tylersburg_isoch(); 3336 3336 3337 3337 /* 3338 3338 * If we copied translations from a previous kernel in the kdump ··· 4248 4246 int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg) 4249 4247 { 4250 4248 struct acpi_dmar_reserved_memory *rmrr; 4249 + int prot = DMA_PTE_READ|DMA_PTE_WRITE; 4251 4250 struct dmar_rmrr_unit *rmrru; 4251 + size_t length; 4252 4252 4253 4253 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL); 4254 4254 if (!rmrru) 4255 - return -ENOMEM; 4255 + goto out; 4256 4256 4257 4257 rmrru->hdr = header; 4258 4258 rmrr = (struct acpi_dmar_reserved_memory *)header; 4259 4259 rmrru->base_address = rmrr->base_address; 4260 4260 rmrru->end_address = rmrr->end_address; 4261 + 4262 + length = rmrr->end_address - rmrr->base_address + 1; 4263 + rmrru->resv = iommu_alloc_resv_region(rmrr->base_address, length, prot, 4264 + IOMMU_RESV_DIRECT); 4265 + if (!rmrru->resv) 4266 + goto free_rmrru; 4267 + 4261 4268 rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1), 4262 4269 ((void *)rmrr) + rmrr->header.length, 4263 4270 &rmrru->devices_cnt); 4264 - if (rmrru->devices_cnt && rmrru->devices == NULL) { 4265 - kfree(rmrru); 4266 - return -ENOMEM; 4267 - } 4271 + if (rmrru->devices_cnt && rmrru->devices == NULL) 4272 + goto free_all; 4268 4273 4269 4274 list_add(&rmrru->list, &dmar_rmrr_units); 4270 4275 4271 4276 return 0; 4277 + free_all: 4278 + kfree(rmrru->resv); 4279 + free_rmrru: 4280 + kfree(rmrru); 4281 + out: 4282 + return -ENOMEM; 4272 4283 } 4273 4284 4274 4285 static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr) ··· 4495 4480 list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) { 4496 4481 list_del(&rmrru->list); 4497 4482 dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt); 4483 + kfree(rmrru->resv); 4498 4484 kfree(rmrru); 4499 4485 } 4500 4486 ··· 4869 4853 4870 4854 init_iommu_pm_ops(); 4871 4855 4872 - for_each_active_iommu(iommu, drhd) 4873 - iommu->iommu_dev = iommu_device_create(NULL, iommu, 4874 - intel_iommu_groups, 4875 - "%s", iommu->name); 4856 + for_each_active_iommu(iommu, drhd) { 4857 + iommu_device_sysfs_add(&iommu->iommu, NULL, 4858 + intel_iommu_groups, 4859 + "%s", iommu->name); 4860 + iommu_device_set_ops(&iommu->iommu, &intel_iommu_ops); 4861 + iommu_device_register(&iommu->iommu); 4862 + } 4876 4863 4877 4864 bus_set_iommu(&pci_bus_type, &intel_iommu_ops); 4878 4865 bus_register_notifier(&pci_bus_type, &device_nb); ··· 5197 5178 if (!iommu) 5198 5179 return -ENODEV; 5199 5180 5200 - iommu_device_link(iommu->iommu_dev, dev); 5181 + iommu_device_link(&iommu->iommu, dev); 5201 5182 5202 5183 group = iommu_group_get_for_dev(dev); 5203 5184 ··· 5219 5200 5220 5201 iommu_group_remove_device(dev); 5221 5202 5222 - iommu_device_unlink(iommu->iommu_dev, dev); 5203 + iommu_device_unlink(&iommu->iommu, dev); 5204 + } 5205 + 5206 + static void intel_iommu_get_resv_regions(struct device *device, 5207 + struct list_head *head) 5208 + { 5209 + struct iommu_resv_region *reg; 5210 + struct dmar_rmrr_unit *rmrr; 5211 + struct device *i_dev; 5212 + int i; 5213 + 5214 + rcu_read_lock(); 5215 + for_each_rmrr_units(rmrr) { 5216 + for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt, 5217 + i, i_dev) { 5218 + if (i_dev != device) 5219 + continue; 5220 + 5221 + list_add_tail(&rmrr->resv->list, head); 5222 + } 5223 + } 5224 + rcu_read_unlock(); 5225 + 5226 + reg = iommu_alloc_resv_region(IOAPIC_RANGE_START, 5227 + IOAPIC_RANGE_END - IOAPIC_RANGE_START + 1, 5228 + 0, IOMMU_RESV_RESERVED); 5229 + if (!reg) 5230 + return; 5231 + list_add_tail(&reg->list, head); 5232 + } 5233 + 5234 + static void intel_iommu_put_resv_regions(struct device *dev, 5235 + struct list_head *head) 5236 + { 5237 + struct iommu_resv_region *entry, *next; 5238 + 5239 + list_for_each_entry_safe(entry, next, head, list) { 5240 + if (entry->type == IOMMU_RESV_RESERVED) 5241 + kfree(entry); 5242 + } 5223 5243 } 5224 5244 5225 5245 #ifdef CONFIG_INTEL_IOMMU_SVM ··· 5390 5332 } 5391 5333 #endif /* CONFIG_INTEL_IOMMU_SVM */ 5392 5334 5393 - static const struct iommu_ops intel_iommu_ops = { 5394 - .capable = intel_iommu_capable, 5395 - .domain_alloc = intel_iommu_domain_alloc, 5396 - .domain_free = intel_iommu_domain_free, 5397 - .attach_dev = intel_iommu_attach_device, 5398 - .detach_dev = intel_iommu_detach_device, 5399 - .map = intel_iommu_map, 5400 - .unmap = intel_iommu_unmap, 5401 - .map_sg = default_iommu_map_sg, 5402 - .iova_to_phys = intel_iommu_iova_to_phys, 5403 - .add_device = intel_iommu_add_device, 5404 - .remove_device = intel_iommu_remove_device, 5405 - .device_group = pci_device_group, 5406 - .pgsize_bitmap = INTEL_IOMMU_PGSIZES, 5335 + const struct iommu_ops intel_iommu_ops = { 5336 + .capable = intel_iommu_capable, 5337 + .domain_alloc = intel_iommu_domain_alloc, 5338 + .domain_free = intel_iommu_domain_free, 5339 + .attach_dev = intel_iommu_attach_device, 5340 + .detach_dev = intel_iommu_detach_device, 5341 + .map = intel_iommu_map, 5342 + .unmap = intel_iommu_unmap, 5343 + .map_sg = default_iommu_map_sg, 5344 + .iova_to_phys = intel_iommu_iova_to_phys, 5345 + .add_device = intel_iommu_add_device, 5346 + .remove_device = intel_iommu_remove_device, 5347 + .get_resv_regions = intel_iommu_get_resv_regions, 5348 + .put_resv_regions = intel_iommu_put_resv_regions, 5349 + .device_group = pci_device_group, 5350 + .pgsize_bitmap = INTEL_IOMMU_PGSIZES, 5407 5351 }; 5408 5352 5409 5353 static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
+5 -1
drivers/iommu/io-pgtable-arm-v7s.c
··· 265 265 if (!(prot & IOMMU_MMIO)) 266 266 pte |= ARM_V7S_ATTR_TEX(1); 267 267 if (ap) { 268 - pte |= ARM_V7S_PTE_AF | ARM_V7S_PTE_AP_UNPRIV; 268 + pte |= ARM_V7S_PTE_AF; 269 + if (!(prot & IOMMU_PRIV)) 270 + pte |= ARM_V7S_PTE_AP_UNPRIV; 269 271 if (!(prot & IOMMU_WRITE)) 270 272 pte |= ARM_V7S_PTE_AP_RDONLY; 271 273 } ··· 290 288 291 289 if (!(attr & ARM_V7S_PTE_AP_RDONLY)) 292 290 prot |= IOMMU_WRITE; 291 + if (!(attr & ARM_V7S_PTE_AP_UNPRIV)) 292 + prot |= IOMMU_PRIV; 293 293 if ((attr & (ARM_V7S_TEX_MASK << ARM_V7S_TEX_SHIFT)) == 0) 294 294 prot |= IOMMU_MMIO; 295 295 else if (pte & ARM_V7S_ATTR_C)
+4 -1
drivers/iommu/io-pgtable-arm.c
··· 350 350 351 351 if (data->iop.fmt == ARM_64_LPAE_S1 || 352 352 data->iop.fmt == ARM_32_LPAE_S1) { 353 - pte = ARM_LPAE_PTE_AP_UNPRIV | ARM_LPAE_PTE_nG; 353 + pte = ARM_LPAE_PTE_nG; 354 354 355 355 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) 356 356 pte |= ARM_LPAE_PTE_AP_RDONLY; 357 + 358 + if (!(prot & IOMMU_PRIV)) 359 + pte |= ARM_LPAE_PTE_AP_UNPRIV; 357 360 358 361 if (prot & IOMMU_MMIO) 359 362 pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
+26 -35
drivers/iommu/iommu-sysfs.c
··· 50 50 postcore_initcall(iommu_dev_init); 51 51 52 52 /* 53 - * Create an IOMMU device and return a pointer to it. IOMMU specific 54 - * attributes can be provided as an attribute group, allowing a unique 55 - * namespace per IOMMU type. 53 + * Init the struct device for the IOMMU. IOMMU specific attributes can 54 + * be provided as an attribute group, allowing a unique namespace per 55 + * IOMMU type. 56 56 */ 57 - struct device *iommu_device_create(struct device *parent, void *drvdata, 58 - const struct attribute_group **groups, 59 - const char *fmt, ...) 57 + int iommu_device_sysfs_add(struct iommu_device *iommu, 58 + struct device *parent, 59 + const struct attribute_group **groups, 60 + const char *fmt, ...) 60 61 { 61 - struct device *dev; 62 62 va_list vargs; 63 63 int ret; 64 64 65 - dev = kzalloc(sizeof(*dev), GFP_KERNEL); 66 - if (!dev) 67 - return ERR_PTR(-ENOMEM); 65 + device_initialize(&iommu->dev); 68 66 69 - device_initialize(dev); 70 - 71 - dev->class = &iommu_class; 72 - dev->parent = parent; 73 - dev->groups = groups; 74 - dev_set_drvdata(dev, drvdata); 67 + iommu->dev.class = &iommu_class; 68 + iommu->dev.parent = parent; 69 + iommu->dev.groups = groups; 75 70 76 71 va_start(vargs, fmt); 77 - ret = kobject_set_name_vargs(&dev->kobj, fmt, vargs); 72 + ret = kobject_set_name_vargs(&iommu->dev.kobj, fmt, vargs); 78 73 va_end(vargs); 79 74 if (ret) 80 75 goto error; 81 76 82 - ret = device_add(dev); 77 + ret = device_add(&iommu->dev); 83 78 if (ret) 84 79 goto error; 85 80 86 - return dev; 81 + return 0; 87 82 88 83 error: 89 - put_device(dev); 90 - return ERR_PTR(ret); 84 + put_device(&iommu->dev); 85 + return ret; 91 86 } 92 87 93 - void iommu_device_destroy(struct device *dev) 88 + void iommu_device_sysfs_remove(struct iommu_device *iommu) 94 89 { 95 - if (!dev || IS_ERR(dev)) 96 - return; 97 - 98 - device_unregister(dev); 90 + device_unregister(&iommu->dev); 99 91 } 100 - 101 92 /* 102 93 * IOMMU drivers can indicate a device is managed by a given IOMMU using 103 94 * this interface. A link to the device will be created in the "devices" 104 95 * directory of the IOMMU device in sysfs and an "iommu" link will be 105 96 * created under the linked device, pointing back at the IOMMU device. 106 97 */ 107 - int iommu_device_link(struct device *dev, struct device *link) 98 + int iommu_device_link(struct iommu_device *iommu, struct device *link) 108 99 { 109 100 int ret; 110 101 111 - if (!dev || IS_ERR(dev)) 102 + if (!iommu || IS_ERR(iommu)) 112 103 return -ENODEV; 113 104 114 - ret = sysfs_add_link_to_group(&dev->kobj, "devices", 105 + ret = sysfs_add_link_to_group(&iommu->dev.kobj, "devices", 115 106 &link->kobj, dev_name(link)); 116 107 if (ret) 117 108 return ret; 118 109 119 - ret = sysfs_create_link_nowarn(&link->kobj, &dev->kobj, "iommu"); 110 + ret = sysfs_create_link_nowarn(&link->kobj, &iommu->dev.kobj, "iommu"); 120 111 if (ret) 121 - sysfs_remove_link_from_group(&dev->kobj, "devices", 112 + sysfs_remove_link_from_group(&iommu->dev.kobj, "devices", 122 113 dev_name(link)); 123 114 124 115 return ret; 125 116 } 126 117 127 - void iommu_device_unlink(struct device *dev, struct device *link) 118 + void iommu_device_unlink(struct iommu_device *iommu, struct device *link) 128 119 { 129 - if (!dev || IS_ERR(dev)) 120 + if (!iommu || IS_ERR(iommu)) 130 121 return; 131 122 132 123 sysfs_remove_link(&link->kobj, "iommu"); 133 - sysfs_remove_link_from_group(&dev->kobj, "devices", dev_name(link)); 124 + sysfs_remove_link_from_group(&iommu->dev.kobj, "devices", dev_name(link)); 134 125 }
+223 -62
drivers/iommu/iommu.c
··· 55 55 struct iommu_domain *domain; 56 56 }; 57 57 58 - struct iommu_device { 58 + struct group_device { 59 59 struct list_head list; 60 60 struct device *dev; 61 61 char *name; ··· 68 68 const char *buf, size_t count); 69 69 }; 70 70 71 + static const char * const iommu_group_resv_type_string[] = { 72 + [IOMMU_RESV_DIRECT] = "direct", 73 + [IOMMU_RESV_RESERVED] = "reserved", 74 + [IOMMU_RESV_MSI] = "msi", 75 + }; 76 + 71 77 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \ 72 78 struct iommu_group_attribute iommu_group_attr_##_name = \ 73 79 __ATTR(_name, _mode, _show, _store) ··· 82 76 container_of(_attr, struct iommu_group_attribute, attr) 83 77 #define to_iommu_group(_kobj) \ 84 78 container_of(_kobj, struct iommu_group, kobj) 79 + 80 + static LIST_HEAD(iommu_device_list); 81 + static DEFINE_SPINLOCK(iommu_device_lock); 82 + 83 + int iommu_device_register(struct iommu_device *iommu) 84 + { 85 + spin_lock(&iommu_device_lock); 86 + list_add_tail(&iommu->list, &iommu_device_list); 87 + spin_unlock(&iommu_device_lock); 88 + 89 + return 0; 90 + } 91 + 92 + void iommu_device_unregister(struct iommu_device *iommu) 93 + { 94 + spin_lock(&iommu_device_lock); 95 + list_del(&iommu->list); 96 + spin_unlock(&iommu_device_lock); 97 + } 85 98 86 99 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, 87 100 unsigned type); ··· 158 133 return sprintf(buf, "%s\n", group->name); 159 134 } 160 135 136 + /** 137 + * iommu_insert_resv_region - Insert a new region in the 138 + * list of reserved regions. 139 + * @new: new region to insert 140 + * @regions: list of regions 141 + * 142 + * The new element is sorted by address with respect to the other 143 + * regions of the same type. In case it overlaps with another 144 + * region of the same type, regions are merged. In case it 145 + * overlaps with another region of different type, regions are 146 + * not merged. 147 + */ 148 + static int iommu_insert_resv_region(struct iommu_resv_region *new, 149 + struct list_head *regions) 150 + { 151 + struct iommu_resv_region *region; 152 + phys_addr_t start = new->start; 153 + phys_addr_t end = new->start + new->length - 1; 154 + struct list_head *pos = regions->next; 155 + 156 + while (pos != regions) { 157 + struct iommu_resv_region *entry = 158 + list_entry(pos, struct iommu_resv_region, list); 159 + phys_addr_t a = entry->start; 160 + phys_addr_t b = entry->start + entry->length - 1; 161 + int type = entry->type; 162 + 163 + if (end < a) { 164 + goto insert; 165 + } else if (start > b) { 166 + pos = pos->next; 167 + } else if ((start >= a) && (end <= b)) { 168 + if (new->type == type) 169 + goto done; 170 + else 171 + pos = pos->next; 172 + } else { 173 + if (new->type == type) { 174 + phys_addr_t new_start = min(a, start); 175 + phys_addr_t new_end = max(b, end); 176 + 177 + list_del(&entry->list); 178 + entry->start = new_start; 179 + entry->length = new_end - new_start + 1; 180 + iommu_insert_resv_region(entry, regions); 181 + } else { 182 + pos = pos->next; 183 + } 184 + } 185 + } 186 + insert: 187 + region = iommu_alloc_resv_region(new->start, new->length, 188 + new->prot, new->type); 189 + if (!region) 190 + return -ENOMEM; 191 + 192 + list_add_tail(&region->list, pos); 193 + done: 194 + return 0; 195 + } 196 + 197 + static int 198 + iommu_insert_device_resv_regions(struct list_head *dev_resv_regions, 199 + struct list_head *group_resv_regions) 200 + { 201 + struct iommu_resv_region *entry; 202 + int ret = 0; 203 + 204 + list_for_each_entry(entry, dev_resv_regions, list) { 205 + ret = iommu_insert_resv_region(entry, group_resv_regions); 206 + if (ret) 207 + break; 208 + } 209 + return ret; 210 + } 211 + 212 + int iommu_get_group_resv_regions(struct iommu_group *group, 213 + struct list_head *head) 214 + { 215 + struct group_device *device; 216 + int ret = 0; 217 + 218 + mutex_lock(&group->mutex); 219 + list_for_each_entry(device, &group->devices, list) { 220 + struct list_head dev_resv_regions; 221 + 222 + INIT_LIST_HEAD(&dev_resv_regions); 223 + iommu_get_resv_regions(device->dev, &dev_resv_regions); 224 + ret = iommu_insert_device_resv_regions(&dev_resv_regions, head); 225 + iommu_put_resv_regions(device->dev, &dev_resv_regions); 226 + if (ret) 227 + break; 228 + } 229 + mutex_unlock(&group->mutex); 230 + return ret; 231 + } 232 + EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions); 233 + 234 + static ssize_t iommu_group_show_resv_regions(struct iommu_group *group, 235 + char *buf) 236 + { 237 + struct iommu_resv_region *region, *next; 238 + struct list_head group_resv_regions; 239 + char *str = buf; 240 + 241 + INIT_LIST_HEAD(&group_resv_regions); 242 + iommu_get_group_resv_regions(group, &group_resv_regions); 243 + 244 + list_for_each_entry_safe(region, next, &group_resv_regions, list) { 245 + str += sprintf(str, "0x%016llx 0x%016llx %s\n", 246 + (long long int)region->start, 247 + (long long int)(region->start + 248 + region->length - 1), 249 + iommu_group_resv_type_string[region->type]); 250 + kfree(region); 251 + } 252 + 253 + return (str - buf); 254 + } 255 + 161 256 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL); 257 + 258 + static IOMMU_GROUP_ATTR(reserved_regions, 0444, 259 + iommu_group_show_resv_regions, NULL); 162 260 163 261 static void iommu_group_release(struct kobject *kobj) 164 262 { ··· 359 211 * use the devices_kobj for reference counting. 360 212 */ 361 213 kobject_put(&group->kobj); 214 + 215 + ret = iommu_group_create_file(group, 216 + &iommu_group_attr_reserved_regions); 217 + if (ret) 218 + return ERR_PTR(ret); 362 219 363 220 pr_debug("Allocated group %d\n", group->id); 364 221 ··· 471 318 struct device *dev) 472 319 { 473 320 struct iommu_domain *domain = group->default_domain; 474 - struct iommu_dm_region *entry; 321 + struct iommu_resv_region *entry; 475 322 struct list_head mappings; 476 323 unsigned long pg_size; 477 324 int ret = 0; ··· 484 331 pg_size = 1UL << __ffs(domain->pgsize_bitmap); 485 332 INIT_LIST_HEAD(&mappings); 486 333 487 - iommu_get_dm_regions(dev, &mappings); 334 + iommu_get_resv_regions(dev, &mappings); 488 335 489 336 /* We need to consider overlapping regions for different devices */ 490 337 list_for_each_entry(entry, &mappings, list) { 491 338 dma_addr_t start, end, addr; 492 339 493 - if (domain->ops->apply_dm_region) 494 - domain->ops->apply_dm_region(dev, domain, entry); 340 + if (domain->ops->apply_resv_region) 341 + domain->ops->apply_resv_region(dev, domain, entry); 495 342 496 343 start = ALIGN(entry->start, pg_size); 497 344 end = ALIGN(entry->start + entry->length, pg_size); 345 + 346 + if (entry->type != IOMMU_RESV_DIRECT) 347 + continue; 498 348 499 349 for (addr = start; addr < end; addr += pg_size) { 500 350 phys_addr_t phys_addr; ··· 514 358 } 515 359 516 360 out: 517 - iommu_put_dm_regions(dev, &mappings); 361 + iommu_put_resv_regions(dev, &mappings); 518 362 519 363 return ret; 520 364 } ··· 530 374 int iommu_group_add_device(struct iommu_group *group, struct device *dev) 531 375 { 532 376 int ret, i = 0; 533 - struct iommu_device *device; 377 + struct group_device *device; 534 378 535 379 device = kzalloc(sizeof(*device), GFP_KERNEL); 536 380 if (!device) ··· 539 383 device->dev = dev; 540 384 541 385 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group"); 542 - if (ret) { 543 - kfree(device); 544 - return ret; 545 - } 386 + if (ret) 387 + goto err_free_device; 546 388 547 389 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj)); 548 390 rename: 549 391 if (!device->name) { 550 - sysfs_remove_link(&dev->kobj, "iommu_group"); 551 - kfree(device); 552 - return -ENOMEM; 392 + ret = -ENOMEM; 393 + goto err_remove_link; 553 394 } 554 395 555 396 ret = sysfs_create_link_nowarn(group->devices_kobj, 556 397 &dev->kobj, device->name); 557 398 if (ret) { 558 - kfree(device->name); 559 399 if (ret == -EEXIST && i >= 0) { 560 400 /* 561 401 * Account for the slim chance of collision 562 402 * and append an instance to the name. 563 403 */ 404 + kfree(device->name); 564 405 device->name = kasprintf(GFP_KERNEL, "%s.%d", 565 406 kobject_name(&dev->kobj), i++); 566 407 goto rename; 567 408 } 568 - 569 - sysfs_remove_link(&dev->kobj, "iommu_group"); 570 - kfree(device); 571 - return ret; 409 + goto err_free_name; 572 410 } 573 411 574 412 kobject_get(group->devices_kobj); ··· 574 424 mutex_lock(&group->mutex); 575 425 list_add_tail(&device->list, &group->devices); 576 426 if (group->domain) 577 - __iommu_attach_device(group->domain, dev); 427 + ret = __iommu_attach_device(group->domain, dev); 578 428 mutex_unlock(&group->mutex); 429 + if (ret) 430 + goto err_put_group; 579 431 580 432 /* Notify any listeners about change to group. */ 581 433 blocking_notifier_call_chain(&group->notifier, ··· 588 436 pr_info("Adding device %s to group %d\n", dev_name(dev), group->id); 589 437 590 438 return 0; 439 + 440 + err_put_group: 441 + mutex_lock(&group->mutex); 442 + list_del(&device->list); 443 + mutex_unlock(&group->mutex); 444 + dev->iommu_group = NULL; 445 + kobject_put(group->devices_kobj); 446 + err_free_name: 447 + kfree(device->name); 448 + err_remove_link: 449 + sysfs_remove_link(&dev->kobj, "iommu_group"); 450 + err_free_device: 451 + kfree(device); 452 + pr_err("Failed to add device %s to group %d: %d\n", dev_name(dev), group->id, ret); 453 + return ret; 591 454 } 592 455 EXPORT_SYMBOL_GPL(iommu_group_add_device); 593 456 ··· 616 449 void iommu_group_remove_device(struct device *dev) 617 450 { 618 451 struct iommu_group *group = dev->iommu_group; 619 - struct iommu_device *tmp_device, *device = NULL; 452 + struct group_device *tmp_device, *device = NULL; 620 453 621 454 pr_info("Removing device %s from group %d\n", dev_name(dev), group->id); 622 455 ··· 651 484 652 485 static int iommu_group_device_count(struct iommu_group *group) 653 486 { 654 - struct iommu_device *entry; 487 + struct group_device *entry; 655 488 int ret = 0; 656 489 657 490 list_for_each_entry(entry, &group->devices, list) ··· 674 507 static int __iommu_group_for_each_dev(struct iommu_group *group, void *data, 675 508 int (*fn)(struct device *, void *)) 676 509 { 677 - struct iommu_device *device; 510 + struct group_device *device; 678 511 int ret = 0; 679 512 680 513 list_for_each_entry(device, &group->devices, list) { ··· 1726 1559 } 1727 1560 EXPORT_SYMBOL_GPL(iommu_domain_set_attr); 1728 1561 1729 - void iommu_get_dm_regions(struct device *dev, struct list_head *list) 1562 + void iommu_get_resv_regions(struct device *dev, struct list_head *list) 1730 1563 { 1731 1564 const struct iommu_ops *ops = dev->bus->iommu_ops; 1732 1565 1733 - if (ops && ops->get_dm_regions) 1734 - ops->get_dm_regions(dev, list); 1566 + if (ops && ops->get_resv_regions) 1567 + ops->get_resv_regions(dev, list); 1735 1568 } 1736 1569 1737 - void iommu_put_dm_regions(struct device *dev, struct list_head *list) 1570 + void iommu_put_resv_regions(struct device *dev, struct list_head *list) 1738 1571 { 1739 1572 const struct iommu_ops *ops = dev->bus->iommu_ops; 1740 1573 1741 - if (ops && ops->put_dm_regions) 1742 - ops->put_dm_regions(dev, list); 1574 + if (ops && ops->put_resv_regions) 1575 + ops->put_resv_regions(dev, list); 1576 + } 1577 + 1578 + struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start, 1579 + size_t length, 1580 + int prot, int type) 1581 + { 1582 + struct iommu_resv_region *region; 1583 + 1584 + region = kzalloc(sizeof(*region), GFP_KERNEL); 1585 + if (!region) 1586 + return NULL; 1587 + 1588 + INIT_LIST_HEAD(&region->list); 1589 + region->start = start; 1590 + region->length = length; 1591 + region->prot = prot; 1592 + region->type = type; 1593 + return region; 1743 1594 } 1744 1595 1745 1596 /* Request that a device is direct mapped by the IOMMU */ ··· 1813 1628 return ret; 1814 1629 } 1815 1630 1816 - struct iommu_instance { 1817 - struct list_head list; 1818 - struct fwnode_handle *fwnode; 1819 - const struct iommu_ops *ops; 1820 - }; 1821 - static LIST_HEAD(iommu_instance_list); 1822 - static DEFINE_SPINLOCK(iommu_instance_lock); 1823 - 1824 - void iommu_register_instance(struct fwnode_handle *fwnode, 1825 - const struct iommu_ops *ops) 1631 + const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode) 1826 1632 { 1827 - struct iommu_instance *iommu = kzalloc(sizeof(*iommu), GFP_KERNEL); 1828 - 1829 - if (WARN_ON(!iommu)) 1830 - return; 1831 - 1832 - of_node_get(to_of_node(fwnode)); 1833 - INIT_LIST_HEAD(&iommu->list); 1834 - iommu->fwnode = fwnode; 1835 - iommu->ops = ops; 1836 - spin_lock(&iommu_instance_lock); 1837 - list_add_tail(&iommu->list, &iommu_instance_list); 1838 - spin_unlock(&iommu_instance_lock); 1839 - } 1840 - 1841 - const struct iommu_ops *iommu_get_instance(struct fwnode_handle *fwnode) 1842 - { 1843 - struct iommu_instance *instance; 1844 1633 const struct iommu_ops *ops = NULL; 1634 + struct iommu_device *iommu; 1845 1635 1846 - spin_lock(&iommu_instance_lock); 1847 - list_for_each_entry(instance, &iommu_instance_list, list) 1848 - if (instance->fwnode == fwnode) { 1849 - ops = instance->ops; 1636 + spin_lock(&iommu_device_lock); 1637 + list_for_each_entry(iommu, &iommu_device_list, list) 1638 + if (iommu->fwnode == fwnode) { 1639 + ops = iommu->ops; 1850 1640 break; 1851 1641 } 1852 - spin_unlock(&iommu_instance_lock); 1642 + spin_unlock(&iommu_device_lock); 1853 1643 return ops; 1854 1644 } 1855 1645 ··· 1874 1714 fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL); 1875 1715 if (!fwspec) 1876 1716 return -ENOMEM; 1717 + 1718 + dev->iommu_fwspec = fwspec; 1877 1719 } 1878 1720 1879 1721 for (i = 0; i < num_ids; i++) 1880 1722 fwspec->ids[fwspec->num_ids + i] = ids[i]; 1881 1723 1882 1724 fwspec->num_ids += num_ids; 1883 - dev->iommu_fwspec = fwspec; 1884 1725 return 0; 1885 1726 } 1886 1727 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
+11 -12
drivers/iommu/iova.c
··· 62 62 else { 63 63 struct rb_node *prev_node = rb_prev(iovad->cached32_node); 64 64 struct iova *curr_iova = 65 - container_of(iovad->cached32_node, struct iova, node); 65 + rb_entry(iovad->cached32_node, struct iova, node); 66 66 *limit_pfn = curr_iova->pfn_lo - 1; 67 67 return prev_node; 68 68 } ··· 86 86 if (!iovad->cached32_node) 87 87 return; 88 88 curr = iovad->cached32_node; 89 - cached_iova = container_of(curr, struct iova, node); 89 + cached_iova = rb_entry(curr, struct iova, node); 90 90 91 91 if (free->pfn_lo >= cached_iova->pfn_lo) { 92 92 struct rb_node *node = rb_next(&free->node); 93 - struct iova *iova = container_of(node, struct iova, node); 93 + struct iova *iova = rb_entry(node, struct iova, node); 94 94 95 95 /* only cache if it's below 32bit pfn */ 96 96 if (node && iova->pfn_lo < iovad->dma_32bit_pfn) ··· 125 125 curr = __get_cached_rbnode(iovad, &limit_pfn); 126 126 prev = curr; 127 127 while (curr) { 128 - struct iova *curr_iova = container_of(curr, struct iova, node); 128 + struct iova *curr_iova = rb_entry(curr, struct iova, node); 129 129 130 130 if (limit_pfn < curr_iova->pfn_lo) 131 131 goto move_left; ··· 171 171 172 172 /* Figure out where to put new node */ 173 173 while (*entry) { 174 - struct iova *this = container_of(*entry, 175 - struct iova, node); 174 + struct iova *this = rb_entry(*entry, struct iova, node); 176 175 parent = *entry; 177 176 178 177 if (new->pfn_lo < this->pfn_lo) ··· 200 201 struct rb_node **new = &(root->rb_node), *parent = NULL; 201 202 /* Figure out where to put new node */ 202 203 while (*new) { 203 - struct iova *this = container_of(*new, struct iova, node); 204 + struct iova *this = rb_entry(*new, struct iova, node); 204 205 205 206 parent = *new; 206 207 ··· 310 311 assert_spin_locked(&iovad->iova_rbtree_lock); 311 312 312 313 while (node) { 313 - struct iova *iova = container_of(node, struct iova, node); 314 + struct iova *iova = rb_entry(node, struct iova, node); 314 315 315 316 /* If pfn falls within iova's range, return iova */ 316 317 if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) { ··· 462 463 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); 463 464 node = rb_first(&iovad->rbroot); 464 465 while (node) { 465 - struct iova *iova = container_of(node, struct iova, node); 466 + struct iova *iova = rb_entry(node, struct iova, node); 466 467 467 468 rb_erase(node, &iovad->rbroot); 468 469 free_iova_mem(iova); ··· 476 477 __is_range_overlap(struct rb_node *node, 477 478 unsigned long pfn_lo, unsigned long pfn_hi) 478 479 { 479 - struct iova *iova = container_of(node, struct iova, node); 480 + struct iova *iova = rb_entry(node, struct iova, node); 480 481 481 482 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo)) 482 483 return 1; ··· 540 541 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); 541 542 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) { 542 543 if (__is_range_overlap(node, pfn_lo, pfn_hi)) { 543 - iova = container_of(node, struct iova, node); 544 + iova = rb_entry(node, struct iova, node); 544 545 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi); 545 546 if ((pfn_lo >= iova->pfn_lo) && 546 547 (pfn_hi <= iova->pfn_hi)) ··· 577 578 578 579 spin_lock_irqsave(&from->iova_rbtree_lock, flags); 579 580 for (node = rb_first(&from->rbroot); node; node = rb_next(node)) { 580 - struct iova *iova = container_of(node, struct iova, node); 581 + struct iova *iova = rb_entry(node, struct iova, node); 581 582 struct iova *new_iova; 582 583 583 584 new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
+2
drivers/iommu/ipmmu-vmsa.c
··· 313 313 domain->cfg.ias = 32; 314 314 domain->cfg.oas = 40; 315 315 domain->cfg.tlb = &ipmmu_gather_ops; 316 + domain->io_domain.geometry.aperture_end = DMA_BIT_MASK(32); 317 + domain->io_domain.geometry.force_aperture = true; 316 318 /* 317 319 * TODO: Add support for coherent walk through CCI with DVM and remove 318 320 * cache handling. For now, delegate it to the io-pgtable code.
+72 -1
drivers/iommu/msm_iommu.c
··· 371 371 return 0; 372 372 } 373 373 374 + /* Must be called under msm_iommu_lock */ 375 + static struct msm_iommu_dev *find_iommu_for_dev(struct device *dev) 376 + { 377 + struct msm_iommu_dev *iommu, *ret = NULL; 378 + struct msm_iommu_ctx_dev *master; 379 + 380 + list_for_each_entry(iommu, &qcom_iommu_devices, dev_node) { 381 + master = list_first_entry(&iommu->ctx_list, 382 + struct msm_iommu_ctx_dev, 383 + list); 384 + if (master->of_node == dev->of_node) { 385 + ret = iommu; 386 + break; 387 + } 388 + } 389 + 390 + return ret; 391 + } 392 + 393 + static int msm_iommu_add_device(struct device *dev) 394 + { 395 + struct msm_iommu_dev *iommu; 396 + unsigned long flags; 397 + int ret = 0; 398 + 399 + spin_lock_irqsave(&msm_iommu_lock, flags); 400 + 401 + iommu = find_iommu_for_dev(dev); 402 + if (iommu) 403 + iommu_device_link(&iommu->iommu, dev); 404 + else 405 + ret = -ENODEV; 406 + 407 + spin_unlock_irqrestore(&msm_iommu_lock, flags); 408 + 409 + return ret; 410 + } 411 + 412 + static void msm_iommu_remove_device(struct device *dev) 413 + { 414 + struct msm_iommu_dev *iommu; 415 + unsigned long flags; 416 + 417 + spin_lock_irqsave(&msm_iommu_lock, flags); 418 + 419 + iommu = find_iommu_for_dev(dev); 420 + if (iommu) 421 + iommu_device_unlink(&iommu->iommu, dev); 422 + 423 + spin_unlock_irqrestore(&msm_iommu_lock, flags); 424 + } 425 + 374 426 static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev) 375 427 { 376 428 int ret = 0; ··· 698 646 .unmap = msm_iommu_unmap, 699 647 .map_sg = default_iommu_map_sg, 700 648 .iova_to_phys = msm_iommu_iova_to_phys, 649 + .add_device = msm_iommu_add_device, 650 + .remove_device = msm_iommu_remove_device, 701 651 .pgsize_bitmap = MSM_IOMMU_PGSIZES, 702 652 .of_xlate = qcom_iommu_of_xlate, 703 653 }; ··· 707 653 static int msm_iommu_probe(struct platform_device *pdev) 708 654 { 709 655 struct resource *r; 656 + resource_size_t ioaddr; 710 657 struct msm_iommu_dev *iommu; 711 658 int ret, par, val; 712 659 ··· 751 696 ret = PTR_ERR(iommu->base); 752 697 goto fail; 753 698 } 699 + ioaddr = r->start; 754 700 755 701 iommu->irq = platform_get_irq(pdev, 0); 756 702 if (iommu->irq < 0) { ··· 793 737 } 794 738 795 739 list_add(&iommu->dev_node, &qcom_iommu_devices); 796 - of_iommu_set_ops(pdev->dev.of_node, &msm_iommu_ops); 740 + 741 + ret = iommu_device_sysfs_add(&iommu->iommu, iommu->dev, NULL, 742 + "msm-smmu.%pa", &ioaddr); 743 + if (ret) { 744 + pr_err("Could not add msm-smmu at %pa to sysfs\n", &ioaddr); 745 + goto fail; 746 + } 747 + 748 + iommu_device_set_ops(&iommu->iommu, &msm_iommu_ops); 749 + iommu_device_set_fwnode(&iommu->iommu, &pdev->dev.of_node->fwnode); 750 + 751 + ret = iommu_device_register(&iommu->iommu); 752 + if (ret) { 753 + pr_err("Could not register msm-smmu at %pa\n", &ioaddr); 754 + goto fail; 755 + } 797 756 798 757 pr_info("device mapped at %p, irq %d with %d ctx banks\n", 799 758 iommu->base, iommu->irq, iommu->ncb);
+3
drivers/iommu/msm_iommu.h
··· 19 19 #define MSM_IOMMU_H 20 20 21 21 #include <linux/interrupt.h> 22 + #include <linux/iommu.h> 22 23 #include <linux/clk.h> 23 24 24 25 /* Sharability attributes of MSM IOMMU mappings */ ··· 69 68 struct list_head dom_node; 70 69 struct list_head ctx_list; 71 70 DECLARE_BITMAP(context_map, IOMMU_MAX_CBS); 71 + 72 + struct iommu_device iommu; 72 73 }; 73 74 74 75 /**
+26 -1
drivers/iommu/mtk_iommu.c
··· 360 360 361 361 static int mtk_iommu_add_device(struct device *dev) 362 362 { 363 + struct mtk_iommu_data *data; 363 364 struct iommu_group *group; 364 365 365 366 if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops) 366 367 return -ENODEV; /* Not a iommu client device */ 368 + 369 + data = dev->iommu_fwspec->iommu_priv; 370 + iommu_device_link(&data->iommu, dev); 367 371 368 372 group = iommu_group_get_for_dev(dev); 369 373 if (IS_ERR(group)) ··· 379 375 380 376 static void mtk_iommu_remove_device(struct device *dev) 381 377 { 378 + struct mtk_iommu_data *data; 379 + 382 380 if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops) 383 381 return; 382 + 383 + data = dev->iommu_fwspec->iommu_priv; 384 + iommu_device_unlink(&data->iommu, dev); 384 385 385 386 iommu_group_remove_device(dev); 386 387 iommu_fwspec_free(dev); ··· 506 497 struct mtk_iommu_data *data; 507 498 struct device *dev = &pdev->dev; 508 499 struct resource *res; 500 + resource_size_t ioaddr; 509 501 struct component_match *match = NULL; 510 502 void *protect; 511 503 int i, larb_nr, ret; ··· 529 519 data->base = devm_ioremap_resource(dev, res); 530 520 if (IS_ERR(data->base)) 531 521 return PTR_ERR(data->base); 522 + ioaddr = res->start; 532 523 533 524 data->irq = platform_get_irq(pdev, 0); 534 525 if (data->irq < 0) ··· 578 567 if (ret) 579 568 return ret; 580 569 570 + ret = iommu_device_sysfs_add(&data->iommu, dev, NULL, 571 + "mtk-iommu.%pa", &ioaddr); 572 + if (ret) 573 + return ret; 574 + 575 + iommu_device_set_ops(&data->iommu, &mtk_iommu_ops); 576 + iommu_device_set_fwnode(&data->iommu, &pdev->dev.of_node->fwnode); 577 + 578 + ret = iommu_device_register(&data->iommu); 579 + if (ret) 580 + return ret; 581 + 581 582 if (!iommu_present(&platform_bus_type)) 582 583 bus_set_iommu(&platform_bus_type, &mtk_iommu_ops); 583 584 ··· 599 576 static int mtk_iommu_remove(struct platform_device *pdev) 600 577 { 601 578 struct mtk_iommu_data *data = platform_get_drvdata(pdev); 579 + 580 + iommu_device_sysfs_remove(&data->iommu); 581 + iommu_device_unregister(&data->iommu); 602 582 603 583 if (iommu_present(&platform_bus_type)) 604 584 bus_set_iommu(&platform_bus_type, NULL); ··· 681 655 return ret; 682 656 } 683 657 684 - of_iommu_set_ops(np, &mtk_iommu_ops); 685 658 return 0; 686 659 } 687 660
+2
drivers/iommu/mtk_iommu.h
··· 47 47 struct iommu_group *m4u_group; 48 48 struct mtk_smi_iommu smi_imu; /* SMI larb iommu info */ 49 49 bool enable_4GB; 50 + 51 + struct iommu_device iommu; 50 52 }; 51 53 52 54 static inline int compare_of(struct device *dev, void *data)
+2 -2
drivers/iommu/of_iommu.c
··· 127 127 "iommu-map-mask", &iommu_spec.np, iommu_spec.args)) 128 128 return NULL; 129 129 130 - ops = of_iommu_get_ops(iommu_spec.np); 130 + ops = iommu_ops_from_fwnode(&iommu_spec.np->fwnode); 131 131 if (!ops || !ops->of_xlate || 132 132 iommu_fwspec_init(&pdev->dev, &iommu_spec.np->fwnode, ops) || 133 133 ops->of_xlate(&pdev->dev, &iommu_spec)) ··· 157 157 "#iommu-cells", idx, 158 158 &iommu_spec)) { 159 159 np = iommu_spec.np; 160 - ops = of_iommu_get_ops(np); 160 + ops = iommu_ops_from_fwnode(&np->fwnode); 161 161 162 162 if (!ops || !ops->of_xlate || 163 163 iommu_fwspec_init(dev, &np->fwnode, ops) ||
+1
drivers/irqchip/irq-gic-v3-its.c
··· 1646 1646 1647 1647 inner_domain->parent = its_parent; 1648 1648 inner_domain->bus_token = DOMAIN_BUS_NEXUS; 1649 + inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP; 1649 1650 info->ops = &its_msi_domain_ops; 1650 1651 info->data = its; 1651 1652 inner_domain->host_data = info;
+38 -2
drivers/vfio/vfio_iommu_type1.c
··· 38 38 #include <linux/workqueue.h> 39 39 #include <linux/mdev.h> 40 40 #include <linux/notifier.h> 41 + #include <linux/dma-iommu.h> 42 + #include <linux/irqdomain.h> 41 43 42 44 #define DRIVER_VERSION "0.2" 43 45 #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>" ··· 1181 1179 return NULL; 1182 1180 } 1183 1181 1182 + static bool vfio_iommu_has_resv_msi(struct iommu_group *group, 1183 + phys_addr_t *base) 1184 + { 1185 + struct list_head group_resv_regions; 1186 + struct iommu_resv_region *region, *next; 1187 + bool ret = false; 1188 + 1189 + INIT_LIST_HEAD(&group_resv_regions); 1190 + iommu_get_group_resv_regions(group, &group_resv_regions); 1191 + list_for_each_entry(region, &group_resv_regions, list) { 1192 + if (region->type & IOMMU_RESV_MSI) { 1193 + *base = region->start; 1194 + ret = true; 1195 + goto out; 1196 + } 1197 + } 1198 + out: 1199 + list_for_each_entry_safe(region, next, &group_resv_regions, list) 1200 + kfree(region); 1201 + return ret; 1202 + } 1203 + 1184 1204 static int vfio_iommu_type1_attach_group(void *iommu_data, 1185 1205 struct iommu_group *iommu_group) 1186 1206 { ··· 1211 1187 struct vfio_domain *domain, *d; 1212 1188 struct bus_type *bus = NULL, *mdev_bus; 1213 1189 int ret; 1190 + bool resv_msi, msi_remap; 1191 + phys_addr_t resv_msi_base; 1214 1192 1215 1193 mutex_lock(&iommu->lock); 1216 1194 ··· 1282 1256 if (ret) 1283 1257 goto out_domain; 1284 1258 1259 + resv_msi = vfio_iommu_has_resv_msi(iommu_group, &resv_msi_base); 1260 + 1285 1261 INIT_LIST_HEAD(&domain->group_list); 1286 1262 list_add(&group->next, &domain->group_list); 1287 1263 1288 - if (!allow_unsafe_interrupts && 1289 - !iommu_capable(bus, IOMMU_CAP_INTR_REMAP)) { 1264 + msi_remap = resv_msi ? irq_domain_check_msi_remap() : 1265 + iommu_capable(bus, IOMMU_CAP_INTR_REMAP); 1266 + 1267 + if (!allow_unsafe_interrupts && !msi_remap) { 1290 1268 pr_warn("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n", 1291 1269 __func__); 1292 1270 ret = -EPERM; ··· 1331 1301 ret = vfio_iommu_replay(iommu, domain); 1332 1302 if (ret) 1333 1303 goto out_detach; 1304 + 1305 + if (resv_msi) { 1306 + ret = iommu_get_msi_cookie(domain->domain, resv_msi_base); 1307 + if (ret) 1308 + goto out_detach; 1309 + } 1334 1310 1335 1311 list_add(&domain->next, &iommu->domain_list); 1336 1312
+8 -2
include/linux/dma-iommu.h
··· 27 27 28 28 /* Domain management interface for IOMMU drivers */ 29 29 int iommu_get_dma_cookie(struct iommu_domain *domain); 30 + int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base); 30 31 void iommu_put_dma_cookie(struct iommu_domain *domain); 31 32 32 33 /* Setup call for arch DMA mapping code */ ··· 35 34 u64 size, struct device *dev); 36 35 37 36 /* General helpers for DMA-API <-> IOMMU-API interaction */ 38 - int dma_direction_to_prot(enum dma_data_direction dir, bool coherent); 37 + int dma_info_to_prot(enum dma_data_direction dir, bool coherent, 38 + unsigned long attrs); 39 39 40 40 /* 41 41 * These implement the bulk of the relevant DMA mapping callbacks, but require ··· 67 65 size_t size, enum dma_data_direction dir, unsigned long attrs); 68 66 void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle, 69 67 size_t size, enum dma_data_direction dir, unsigned long attrs); 70 - int iommu_dma_supported(struct device *dev, u64 mask); 71 68 int iommu_dma_mapping_error(struct device *dev, dma_addr_t dma_addr); 72 69 73 70 /* The DMA API isn't _quite_ the whole story, though... */ ··· 83 82 } 84 83 85 84 static inline int iommu_get_dma_cookie(struct iommu_domain *domain) 85 + { 86 + return -ENODEV; 87 + } 88 + 89 + static inline int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base) 86 90 { 87 91 return -ENODEV; 88 92 }
+7
include/linux/dma-mapping.h
··· 63 63 #define DMA_ATTR_NO_WARN (1UL << 8) 64 64 65 65 /* 66 + * DMA_ATTR_PRIVILEGED: used to indicate that the buffer is fully 67 + * accessible at an elevated privilege level (and ideally inaccessible or 68 + * at least read-only at lesser-privileged levels). 69 + */ 70 + #define DMA_ATTR_PRIVILEGED (1UL << 9) 71 + 72 + /* 66 73 * A dma_addr_t can hold any valid DMA or bus address for the platform. 67 74 * It can be given to a device to use as a DMA source or target. A CPU cannot 68 75 * reference a dma_addr_t directly because there may be translation between
+9 -8
include/linux/intel-iommu.h
··· 29 29 #include <linux/dma_remapping.h> 30 30 #include <linux/mmu_notifier.h> 31 31 #include <linux/list.h> 32 + #include <linux/iommu.h> 32 33 #include <asm/cacheflush.h> 33 34 #include <asm/iommu.h> 34 35 ··· 154 153 #define DMA_TLB_GLOBAL_FLUSH (((u64)1) << 60) 155 154 #define DMA_TLB_DSI_FLUSH (((u64)2) << 60) 156 155 #define DMA_TLB_PSI_FLUSH (((u64)3) << 60) 157 - #define DMA_TLB_IIRG(type) ((type >> 60) & 7) 158 - #define DMA_TLB_IAIG(val) (((val) >> 57) & 7) 156 + #define DMA_TLB_IIRG(type) ((type >> 60) & 3) 157 + #define DMA_TLB_IAIG(val) (((val) >> 57) & 3) 159 158 #define DMA_TLB_READ_DRAIN (((u64)1) << 49) 160 159 #define DMA_TLB_WRITE_DRAIN (((u64)1) << 48) 161 160 #define DMA_TLB_DID(id) (((u64)((id) & 0xffff)) << 32) ··· 165 164 166 165 /* INVALID_DESC */ 167 166 #define DMA_CCMD_INVL_GRANU_OFFSET 61 168 - #define DMA_ID_TLB_GLOBAL_FLUSH (((u64)1) << 3) 169 - #define DMA_ID_TLB_DSI_FLUSH (((u64)2) << 3) 170 - #define DMA_ID_TLB_PSI_FLUSH (((u64)3) << 3) 167 + #define DMA_ID_TLB_GLOBAL_FLUSH (((u64)1) << 4) 168 + #define DMA_ID_TLB_DSI_FLUSH (((u64)2) << 4) 169 + #define DMA_ID_TLB_PSI_FLUSH (((u64)3) << 4) 171 170 #define DMA_ID_TLB_READ_DRAIN (((u64)1) << 7) 172 171 #define DMA_ID_TLB_WRITE_DRAIN (((u64)1) << 6) 173 172 #define DMA_ID_TLB_DID(id) (((u64)((id & 0xffff) << 16))) ··· 317 316 #define QI_DEV_EIOTLB_SIZE (((u64)1) << 11) 318 317 #define QI_DEV_EIOTLB_GLOB(g) ((u64)g) 319 318 #define QI_DEV_EIOTLB_PASID(p) (((u64)p) << 32) 320 - #define QI_DEV_EIOTLB_SID(sid) ((u64)((sid) & 0xffff) << 32) 321 - #define QI_DEV_EIOTLB_QDEP(qd) (((qd) & 0x1f) << 16) 319 + #define QI_DEV_EIOTLB_SID(sid) ((u64)((sid) & 0xffff) << 16) 320 + #define QI_DEV_EIOTLB_QDEP(qd) ((u64)((qd) & 0x1f) << 4) 322 321 #define QI_DEV_EIOTLB_MAX_INVS 32 323 322 324 323 #define QI_PGRP_IDX(idx) (((u64)(idx)) << 55) ··· 440 439 struct irq_domain *ir_domain; 441 440 struct irq_domain *ir_msi_domain; 442 441 #endif 443 - struct device *iommu_dev; /* IOMMU-sysfs device */ 442 + struct iommu_device iommu; /* IOMMU core code handle */ 444 443 int node; 445 444 u32 flags; /* Software defined flags */ 446 445 };
+103 -35
include/linux/iommu.h
··· 31 31 #define IOMMU_CACHE (1 << 2) /* DMA cache coherency */ 32 32 #define IOMMU_NOEXEC (1 << 3) 33 33 #define IOMMU_MMIO (1 << 4) /* e.g. things like MSI doorbells */ 34 + /* 35 + * This is to make the IOMMU API setup privileged 36 + * mapppings accessible by the master only at higher 37 + * privileged execution level and inaccessible at 38 + * less privileged levels. 39 + */ 40 + #define IOMMU_PRIV (1 << 5) 34 41 35 42 struct iommu_ops; 36 43 struct iommu_group; ··· 124 117 DOMAIN_ATTR_MAX, 125 118 }; 126 119 120 + /* These are the possible reserved region types */ 121 + #define IOMMU_RESV_DIRECT (1 << 0) 122 + #define IOMMU_RESV_RESERVED (1 << 1) 123 + #define IOMMU_RESV_MSI (1 << 2) 124 + 127 125 /** 128 - * struct iommu_dm_region - descriptor for a direct mapped memory region 126 + * struct iommu_resv_region - descriptor for a reserved memory region 129 127 * @list: Linked list pointers 130 128 * @start: System physical start address of the region 131 129 * @length: Length of the region in bytes 132 130 * @prot: IOMMU Protection flags (READ/WRITE/...) 131 + * @type: Type of the reserved region 133 132 */ 134 - struct iommu_dm_region { 133 + struct iommu_resv_region { 135 134 struct list_head list; 136 135 phys_addr_t start; 137 136 size_t length; 138 137 int prot; 138 + int type; 139 139 }; 140 140 141 141 #ifdef CONFIG_IOMMU_API ··· 164 150 * @device_group: find iommu group for a particular device 165 151 * @domain_get_attr: Query domain attributes 166 152 * @domain_set_attr: Change domain attributes 167 - * @get_dm_regions: Request list of direct mapping requirements for a device 168 - * @put_dm_regions: Free list of direct mapping requirements for a device 169 - * @apply_dm_region: Temporary helper call-back for iova reserved ranges 153 + * @get_resv_regions: Request list of reserved regions for a device 154 + * @put_resv_regions: Free list of reserved regions for a device 155 + * @apply_resv_region: Temporary helper call-back for iova reserved ranges 170 156 * @domain_window_enable: Configure and enable a particular window for a domain 171 157 * @domain_window_disable: Disable a particular window for a domain 172 158 * @domain_set_windows: Set the number of windows for a domain ··· 198 184 int (*domain_set_attr)(struct iommu_domain *domain, 199 185 enum iommu_attr attr, void *data); 200 186 201 - /* Request/Free a list of direct mapping requirements for a device */ 202 - void (*get_dm_regions)(struct device *dev, struct list_head *list); 203 - void (*put_dm_regions)(struct device *dev, struct list_head *list); 204 - void (*apply_dm_region)(struct device *dev, struct iommu_domain *domain, 205 - struct iommu_dm_region *region); 187 + /* Request/Free a list of reserved regions for a device */ 188 + void (*get_resv_regions)(struct device *dev, struct list_head *list); 189 + void (*put_resv_regions)(struct device *dev, struct list_head *list); 190 + void (*apply_resv_region)(struct device *dev, 191 + struct iommu_domain *domain, 192 + struct iommu_resv_region *region); 206 193 207 194 /* Window handling functions */ 208 195 int (*domain_window_enable)(struct iommu_domain *domain, u32 wnd_nr, ··· 218 203 219 204 unsigned long pgsize_bitmap; 220 205 }; 206 + 207 + /** 208 + * struct iommu_device - IOMMU core representation of one IOMMU hardware 209 + * instance 210 + * @list: Used by the iommu-core to keep a list of registered iommus 211 + * @ops: iommu-ops for talking to this iommu 212 + * @dev: struct device for sysfs handling 213 + */ 214 + struct iommu_device { 215 + struct list_head list; 216 + const struct iommu_ops *ops; 217 + struct fwnode_handle *fwnode; 218 + struct device dev; 219 + }; 220 + 221 + int iommu_device_register(struct iommu_device *iommu); 222 + void iommu_device_unregister(struct iommu_device *iommu); 223 + int iommu_device_sysfs_add(struct iommu_device *iommu, 224 + struct device *parent, 225 + const struct attribute_group **groups, 226 + const char *fmt, ...) __printf(4, 5); 227 + void iommu_device_sysfs_remove(struct iommu_device *iommu); 228 + int iommu_device_link(struct iommu_device *iommu, struct device *link); 229 + void iommu_device_unlink(struct iommu_device *iommu, struct device *link); 230 + 231 + static inline void iommu_device_set_ops(struct iommu_device *iommu, 232 + const struct iommu_ops *ops) 233 + { 234 + iommu->ops = ops; 235 + } 236 + 237 + static inline void iommu_device_set_fwnode(struct iommu_device *iommu, 238 + struct fwnode_handle *fwnode) 239 + { 240 + iommu->fwnode = fwnode; 241 + } 221 242 222 243 #define IOMMU_GROUP_NOTIFY_ADD_DEVICE 1 /* Device added */ 223 244 #define IOMMU_GROUP_NOTIFY_DEL_DEVICE 2 /* Pre Device removed */ ··· 284 233 extern void iommu_set_fault_handler(struct iommu_domain *domain, 285 234 iommu_fault_handler_t handler, void *token); 286 235 287 - extern void iommu_get_dm_regions(struct device *dev, struct list_head *list); 288 - extern void iommu_put_dm_regions(struct device *dev, struct list_head *list); 236 + extern void iommu_get_resv_regions(struct device *dev, struct list_head *list); 237 + extern void iommu_put_resv_regions(struct device *dev, struct list_head *list); 289 238 extern int iommu_request_dm_for_dev(struct device *dev); 239 + extern struct iommu_resv_region * 240 + iommu_alloc_resv_region(phys_addr_t start, size_t length, int prot, int type); 241 + extern int iommu_get_group_resv_regions(struct iommu_group *group, 242 + struct list_head *head); 290 243 291 244 extern int iommu_attach_group(struct iommu_domain *domain, 292 245 struct iommu_group *group); ··· 322 267 void *data); 323 268 extern int iommu_domain_set_attr(struct iommu_domain *domain, enum iommu_attr, 324 269 void *data); 325 - struct device *iommu_device_create(struct device *parent, void *drvdata, 326 - const struct attribute_group **groups, 327 - const char *fmt, ...) __printf(4, 5); 328 - void iommu_device_destroy(struct device *dev); 329 - int iommu_device_link(struct device *dev, struct device *link); 330 - void iommu_device_unlink(struct device *dev, struct device *link); 331 270 332 271 /* Window handling function prototypes */ 333 272 extern int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr, ··· 401 352 const struct iommu_ops *ops); 402 353 void iommu_fwspec_free(struct device *dev); 403 354 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids); 404 - void iommu_register_instance(struct fwnode_handle *fwnode, 405 - const struct iommu_ops *ops); 406 - const struct iommu_ops *iommu_get_instance(struct fwnode_handle *fwnode); 355 + const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode); 407 356 408 357 #else /* CONFIG_IOMMU_API */ 409 358 410 359 struct iommu_ops {}; 411 360 struct iommu_group {}; 412 361 struct iommu_fwspec {}; 362 + struct iommu_device {}; 413 363 414 364 static inline bool iommu_present(struct bus_type *bus) 415 365 { ··· 491 443 { 492 444 } 493 445 494 - static inline void iommu_get_dm_regions(struct device *dev, 446 + static inline void iommu_get_resv_regions(struct device *dev, 495 447 struct list_head *list) 496 448 { 497 449 } 498 450 499 - static inline void iommu_put_dm_regions(struct device *dev, 451 + static inline void iommu_put_resv_regions(struct device *dev, 500 452 struct list_head *list) 501 453 { 454 + } 455 + 456 + static inline int iommu_get_group_resv_regions(struct iommu_group *group, 457 + struct list_head *head) 458 + { 459 + return -ENODEV; 502 460 } 503 461 504 462 static inline int iommu_request_dm_for_dev(struct device *dev) ··· 600 546 return -EINVAL; 601 547 } 602 548 603 - static inline struct device *iommu_device_create(struct device *parent, 604 - void *drvdata, 605 - const struct attribute_group **groups, 606 - const char *fmt, ...) 549 + static inline int iommu_device_register(struct iommu_device *iommu) 607 550 { 608 - return ERR_PTR(-ENODEV); 551 + return -ENODEV; 609 552 } 610 553 611 - static inline void iommu_device_destroy(struct device *dev) 554 + static inline void iommu_device_set_ops(struct iommu_device *iommu, 555 + const struct iommu_ops *ops) 556 + { 557 + } 558 + 559 + static inline void iommu_device_set_fwnode(struct iommu_device *iommu, 560 + struct fwnode_handle *fwnode) 561 + { 562 + } 563 + 564 + static inline void iommu_device_unregister(struct iommu_device *iommu) 565 + { 566 + } 567 + 568 + static inline int iommu_device_sysfs_add(struct iommu_device *iommu, 569 + struct device *parent, 570 + const struct attribute_group **groups, 571 + const char *fmt, ...) 572 + { 573 + return -ENODEV; 574 + } 575 + 576 + static inline void iommu_device_sysfs_remove(struct iommu_device *iommu) 612 577 { 613 578 } 614 579 ··· 657 584 return -ENODEV; 658 585 } 659 586 660 - static inline void iommu_register_instance(struct fwnode_handle *fwnode, 661 - const struct iommu_ops *ops) 662 - { 663 - } 664 - 665 587 static inline 666 - const struct iommu_ops *iommu_get_instance(struct fwnode_handle *fwnode) 588 + const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode) 667 589 { 668 590 return NULL; 669 591 }
+36
include/linux/irqdomain.h
··· 183 183 /* Irq domain is an IPI domain with single virq */ 184 184 IRQ_DOMAIN_FLAG_IPI_SINGLE = (1 << 3), 185 185 186 + /* Irq domain implements MSIs */ 187 + IRQ_DOMAIN_FLAG_MSI = (1 << 4), 188 + 189 + /* Irq domain implements MSI remapping */ 190 + IRQ_DOMAIN_FLAG_MSI_REMAP = (1 << 5), 191 + 186 192 /* 187 193 * Flags starting from IRQ_DOMAIN_FLAG_NONCORE are reserved 188 194 * for implementation specific purposes and ignored by the ··· 222 216 void *host_data); 223 217 extern struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec, 224 218 enum irq_domain_bus_token bus_token); 219 + extern bool irq_domain_check_msi_remap(void); 225 220 extern void irq_set_default_host(struct irq_domain *host); 226 221 extern int irq_domain_alloc_descs(int virq, unsigned int nr_irqs, 227 222 irq_hw_number_t hwirq, int node, ··· 453 446 { 454 447 return domain->flags & IRQ_DOMAIN_FLAG_IPI_SINGLE; 455 448 } 449 + 450 + static inline bool irq_domain_is_msi(struct irq_domain *domain) 451 + { 452 + return domain->flags & IRQ_DOMAIN_FLAG_MSI; 453 + } 454 + 455 + static inline bool irq_domain_is_msi_remap(struct irq_domain *domain) 456 + { 457 + return domain->flags & IRQ_DOMAIN_FLAG_MSI_REMAP; 458 + } 459 + 460 + extern bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain); 461 + 456 462 #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */ 457 463 static inline void irq_domain_activate_irq(struct irq_data *data) { } 458 464 static inline void irq_domain_deactivate_irq(struct irq_data *data) { } ··· 494 474 } 495 475 496 476 static inline bool irq_domain_is_ipi_single(struct irq_domain *domain) 477 + { 478 + return false; 479 + } 480 + 481 + static inline bool irq_domain_is_msi(struct irq_domain *domain) 482 + { 483 + return false; 484 + } 485 + 486 + static inline bool irq_domain_is_msi_remap(struct irq_domain *domain) 487 + { 488 + return false; 489 + } 490 + 491 + static inline bool 492 + irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain) 497 493 { 498 494 return false; 499 495 }
-11
include/linux/of_iommu.h
··· 31 31 32 32 #endif /* CONFIG_OF_IOMMU */ 33 33 34 - static inline void of_iommu_set_ops(struct device_node *np, 35 - const struct iommu_ops *ops) 36 - { 37 - iommu_register_instance(&np->fwnode, ops); 38 - } 39 - 40 - static inline const struct iommu_ops *of_iommu_get_ops(struct device_node *np) 41 - { 42 - return iommu_get_instance(&np->fwnode); 43 - } 44 - 45 34 extern struct of_device_id __iommu_of_table; 46 35 47 36 typedef int (*of_iommu_init_fn)(struct device_node *);
+39
kernel/irq/irqdomain.c
··· 278 278 EXPORT_SYMBOL_GPL(irq_find_matching_fwspec); 279 279 280 280 /** 281 + * irq_domain_check_msi_remap - Check whether all MSI irq domains implement 282 + * IRQ remapping 283 + * 284 + * Return: false if any MSI irq domain does not support IRQ remapping, 285 + * true otherwise (including if there is no MSI irq domain) 286 + */ 287 + bool irq_domain_check_msi_remap(void) 288 + { 289 + struct irq_domain *h; 290 + bool ret = true; 291 + 292 + mutex_lock(&irq_domain_mutex); 293 + list_for_each_entry(h, &irq_domain_list, link) { 294 + if (irq_domain_is_msi(h) && 295 + !irq_domain_hierarchical_is_msi_remap(h)) { 296 + ret = false; 297 + break; 298 + } 299 + } 300 + mutex_unlock(&irq_domain_mutex); 301 + return ret; 302 + } 303 + EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap); 304 + 305 + /** 281 306 * irq_set_default_host() - Set a "default" irq domain 282 307 * @domain: default domain pointer 283 308 * ··· 1432 1407 /* Hierarchy irq_domains must implement callback alloc() */ 1433 1408 if (domain->ops->alloc) 1434 1409 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY; 1410 + } 1411 + 1412 + /** 1413 + * irq_domain_hierarchical_is_msi_remap - Check if the domain or any 1414 + * parent has MSI remapping support 1415 + * @domain: domain pointer 1416 + */ 1417 + bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain) 1418 + { 1419 + for (; domain; domain = domain->parent) { 1420 + if (irq_domain_is_msi_remap(domain)) 1421 + return true; 1422 + } 1423 + return false; 1435 1424 } 1436 1425 #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */ 1437 1426 /**
+2 -2
kernel/irq/msi.c
··· 270 270 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS) 271 271 msi_domain_update_chip_ops(info); 272 272 273 - return irq_domain_create_hierarchy(parent, 0, 0, fwnode, 274 - &msi_domain_ops, info); 273 + return irq_domain_create_hierarchy(parent, IRQ_DOMAIN_FLAG_MSI, 0, 274 + fwnode, &msi_domain_ops, info); 275 275 } 276 276 277 277 int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,