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

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

Pull iommu updates from Joerg Roedel:
"Core changes:
- Fix race conditions in device probe path
- Retire IOMMU bus_ops
- Support for passing custom allocators to page table drivers
- Clean up Kconfig around IOMMU_SVA
- Support for sharing SVA domains with all devices bound to a mm
- Firmware data parsing cleanup
- Tracing improvements for iommu-dma code
- Some smaller fixes and cleanups

ARM-SMMU drivers:
- Device-tree binding updates:
- Add additional compatible strings for Qualcomm SoCs
- Document Adreno clocks for Qualcomm's SM8350 SoC
- SMMUv2:
- Implement support for the ->domain_alloc_paging() callback
- Ensure Secure context is restored following suspend of Qualcomm
SMMU implementation
- SMMUv3:
- Disable stalling mode for the "quiet" context descriptor
- Minor refactoring and driver cleanups

Intel VT-d driver:
- Cleanup and refactoring

AMD IOMMU driver:
- Improve IO TLB invalidation logic
- Small cleanups and improvements

Rockchip IOMMU driver:
- DT binding update to add Rockchip RK3588

Apple DART driver:
- Apple M1 USB4/Thunderbolt DART support
- Cleanups

Virtio IOMMU driver:
- Add support for iotlb_sync_map
- Enable deferred IO TLB flushes"

* tag 'iommu-updates-v6.8' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu: (66 commits)
iommu: Don't reserve 0-length IOVA region
iommu/vt-d: Move inline helpers to header files
iommu/vt-d: Remove unused vcmd interfaces
iommu/vt-d: Remove unused parameter of intel_pasid_setup_pass_through()
iommu/vt-d: Refactor device_to_iommu() to retrieve iommu directly
iommu/sva: Fix memory leak in iommu_sva_bind_device()
dt-bindings: iommu: rockchip: Add Rockchip RK3588
iommu/dma: Trace bounce buffer usage when mapping buffers
iommu/arm-smmu: Convert to domain_alloc_paging()
iommu/arm-smmu: Pass arm_smmu_domain to internal functions
iommu/arm-smmu: Implement IOMMU_DOMAIN_BLOCKED
iommu/arm-smmu: Convert to a global static identity domain
iommu/arm-smmu: Reorganize arm_smmu_domain_add_master()
iommu/arm-smmu-v3: Remove ARM_SMMU_DOMAIN_NESTED
iommu/arm-smmu-v3: Master cannot be NULL in arm_smmu_write_strtab_ent()
iommu/arm-smmu-v3: Add a type for the STE
iommu/arm-smmu-v3: disable stall for quiet_cd
iommu/qcom: restore IOMMU state if needed
iommu/arm-smmu-qcom: Add QCM2290 MDSS compatible
iommu/arm-smmu-qcom: Add missing GMU entry to match table
...

+1279 -1043
+1
Documentation/devicetree/bindings/iommu/apple,dart.yaml
··· 24 24 compatible: 25 25 enum: 26 26 - apple,t8103-dart 27 + - apple,t8103-usb4-dart 27 28 - apple,t8110-dart 28 29 - apple,t6000-dart 29 30
+74 -3
Documentation/devicetree/bindings/iommu/arm,smmu.yaml
··· 56 56 - qcom,sm8350-smmu-500 57 57 - qcom,sm8450-smmu-500 58 58 - qcom,sm8550-smmu-500 59 + - qcom,sm8650-smmu-500 60 + - qcom,x1e80100-smmu-500 59 61 - const: qcom,smmu-500 60 62 - const: arm,mmu-500 61 63 ··· 91 89 - qcom,sm8150-smmu-500 92 90 - qcom,sm8250-smmu-500 93 91 - qcom,sm8350-smmu-500 92 + - qcom,sm8450-smmu-500 93 + - qcom,sm8550-smmu-500 94 94 - const: qcom,adreno-smmu 95 95 - const: qcom,smmu-500 96 96 - const: arm,mmu-500 ··· 438 434 compatible: 439 435 items: 440 436 - enum: 437 + - qcom,sm8350-smmu-500 438 + - const: qcom,adreno-smmu 439 + - const: qcom,smmu-500 440 + - const: arm,mmu-500 441 + then: 442 + properties: 443 + clock-names: 444 + items: 445 + - const: bus 446 + - const: iface 447 + - const: ahb 448 + - const: hlos1_vote_gpu_smmu 449 + - const: cx_gmu 450 + - const: hub_cx_int 451 + - const: hub_aon 452 + clocks: 453 + minItems: 7 454 + maxItems: 7 455 + 456 + - if: 457 + properties: 458 + compatible: 459 + items: 460 + - enum: 441 461 - qcom,sm6115-smmu-500 442 462 - qcom,sm6125-smmu-500 443 463 - const: qcom,adreno-smmu ··· 480 452 - description: GPU memory bus clock 481 453 - description: Voter clock required for HLOS SMMU access 482 454 - description: Interface clock required for register access 455 + 456 + - if: 457 + properties: 458 + compatible: 459 + const: qcom,sm8450-smmu-500 460 + then: 461 + properties: 462 + clock-names: 463 + items: 464 + - const: gmu 465 + - const: hub 466 + - const: hlos 467 + - const: bus 468 + - const: iface 469 + - const: ahb 470 + 471 + clocks: 472 + items: 473 + - description: GMU clock 474 + - description: GPU HUB clock 475 + - description: HLOS vote clock 476 + - description: GPU memory bus clock 477 + - description: GPU SNoC bus clock 478 + - description: GPU AHB clock 479 + 480 + - if: 481 + properties: 482 + compatible: 483 + const: qcom,sm8550-smmu-500 484 + then: 485 + properties: 486 + clock-names: 487 + items: 488 + - const: hlos 489 + - const: bus 490 + - const: iface 491 + - const: ahb 492 + 493 + clocks: 494 + items: 495 + - description: HLOS vote clock 496 + - description: GPU memory bus clock 497 + - description: GPU SNoC bus clock 498 + - description: GPU AHB clock 483 499 484 500 # Disallow clocks for all other platforms with specific compatibles 485 501 - if: ··· 544 472 - qcom,sdx65-smmu-500 545 473 - qcom,sm6350-smmu-500 546 474 - qcom,sm6375-smmu-500 547 - - qcom,sm8350-smmu-500 548 - - qcom,sm8450-smmu-500 549 - - qcom,sm8550-smmu-500 475 + - qcom,sm8650-smmu-500 476 + - qcom,x1e80100-smmu-500 550 477 then: 551 478 properties: 552 479 clock-names: false
+8 -3
Documentation/devicetree/bindings/iommu/rockchip,iommu.yaml
··· 19 19 20 20 properties: 21 21 compatible: 22 - enum: 23 - - rockchip,iommu 24 - - rockchip,rk3568-iommu 22 + oneOf: 23 + - enum: 24 + - rockchip,iommu 25 + - rockchip,rk3568-iommu 26 + - items: 27 + - enum: 28 + - rockchip,rk3588-iommu 29 + - const: rockchip,rk3568-iommu 25 30 26 31 reg: 27 32 items:
+5
arch/Kconfig
··· 301 301 config ARCH_HAS_CPU_FINALIZE_INIT 302 302 bool 303 303 304 + # The architecture has a per-task state that includes the mm's PASID 305 + config ARCH_HAS_CPU_PASID 306 + bool 307 + select IOMMU_MM_DATA 308 + 304 309 config HAVE_ARCH_THREAD_STRUCT_WHITELIST 305 310 bool 306 311 help
+1 -1
arch/arc/mm/dma.c
··· 91 91 * Plug in direct dma map ops. 92 92 */ 93 93 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, 94 - const struct iommu_ops *iommu, bool coherent) 94 + bool coherent) 95 95 { 96 96 /* 97 97 * IOC hardware snoops all DMA traffic keeping the caches consistent
+1 -1
arch/arm/mm/dma-mapping-nommu.c
··· 34 34 } 35 35 36 36 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, 37 - const struct iommu_ops *iommu, bool coherent) 37 + bool coherent) 38 38 { 39 39 if (IS_ENABLED(CONFIG_CPU_V7M)) { 40 40 /*
+5 -5
arch/arm/mm/dma-mapping.c
··· 1710 1710 EXPORT_SYMBOL_GPL(arm_iommu_detach_device); 1711 1711 1712 1712 static void arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size, 1713 - const struct iommu_ops *iommu, bool coherent) 1713 + bool coherent) 1714 1714 { 1715 1715 struct dma_iommu_mapping *mapping; 1716 1716 ··· 1745 1745 #else 1746 1746 1747 1747 static void arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size, 1748 - const struct iommu_ops *iommu, bool coherent) 1748 + bool coherent) 1749 1749 { 1750 1750 } 1751 1751 ··· 1754 1754 #endif /* CONFIG_ARM_DMA_USE_IOMMU */ 1755 1755 1756 1756 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, 1757 - const struct iommu_ops *iommu, bool coherent) 1757 + bool coherent) 1758 1758 { 1759 1759 /* 1760 1760 * Due to legacy code that sets the ->dma_coherent flag from a bus ··· 1773 1773 if (dev->dma_ops) 1774 1774 return; 1775 1775 1776 - if (iommu) 1777 - arm_setup_iommu_dma_ops(dev, dma_base, size, iommu, coherent); 1776 + if (device_iommu_mapped(dev)) 1777 + arm_setup_iommu_dma_ops(dev, dma_base, size, coherent); 1778 1778 1779 1779 xen_setup_dma_ops(dev); 1780 1780 dev->archdata.dma_ops_setup = true;
+2 -2
arch/arm64/mm/dma-mapping.c
··· 47 47 #endif 48 48 49 49 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, 50 - const struct iommu_ops *iommu, bool coherent) 50 + bool coherent) 51 51 { 52 52 int cls = cache_line_size_of_cpu(); 53 53 ··· 58 58 ARCH_DMA_MINALIGN, cls); 59 59 60 60 dev->dma_coherent = coherent; 61 - if (iommu) 61 + if (device_iommu_mapped(dev)) 62 62 iommu_setup_dma_ops(dev, dma_base, dma_base + size - 1); 63 63 64 64 xen_setup_dma_ops(dev);
+1 -1
arch/mips/mm/dma-noncoherent.c
··· 138 138 139 139 #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS 140 140 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, 141 - const struct iommu_ops *iommu, bool coherent) 141 + bool coherent) 142 142 { 143 143 dev->dma_coherent = coherent; 144 144 }
+1 -1
arch/riscv/mm/dma-noncoherent.c
··· 129 129 } 130 130 131 131 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, 132 - const struct iommu_ops *iommu, bool coherent) 132 + bool coherent) 133 133 { 134 134 WARN_TAINT(!coherent && riscv_cbom_block_size > ARCH_DMA_MINALIGN, 135 135 TAINT_CPU_OUT_OF_SPEC,
+1
arch/x86/Kconfig
··· 72 72 select ARCH_HAS_CACHE_LINE_SIZE 73 73 select ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION 74 74 select ARCH_HAS_CPU_FINALIZE_INIT 75 + select ARCH_HAS_CPU_PASID if IOMMU_SVA 75 76 select ARCH_HAS_CURRENT_STACK_POINTER 76 77 select ARCH_HAS_DEBUG_VIRTUAL 77 78 select ARCH_HAS_DEBUG_VM_PGTABLE if !X86_PAE
+2 -2
arch/x86/kernel/traps.c
··· 566 566 */ 567 567 static bool try_fixup_enqcmd_gp(void) 568 568 { 569 - #ifdef CONFIG_IOMMU_SVA 569 + #ifdef CONFIG_ARCH_HAS_CPU_PASID 570 570 u32 pasid; 571 571 572 572 /* ··· 592 592 if (!mm_valid_pasid(current->mm)) 593 593 return false; 594 594 595 - pasid = current->mm->pasid; 595 + pasid = mm_get_enqcmd_pasid(current->mm); 596 596 597 597 /* 598 598 * Did this thread already have its PASID activated?
+18 -14
drivers/acpi/scan.c
··· 1561 1561 return fwspec ? fwspec->ops : NULL; 1562 1562 } 1563 1563 1564 - static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev, 1565 - const u32 *id_in) 1564 + static int acpi_iommu_configure_id(struct device *dev, const u32 *id_in) 1566 1565 { 1567 1566 int err; 1568 1567 const struct iommu_ops *ops; ··· 1575 1576 ops = acpi_iommu_fwspec_ops(dev); 1576 1577 if (ops) { 1577 1578 mutex_unlock(&iommu_probe_device_lock); 1578 - return ops; 1579 + return 0; 1579 1580 } 1580 1581 1581 1582 err = iort_iommu_configure_id(dev, id_in); ··· 1592 1593 1593 1594 /* Ignore all other errors apart from EPROBE_DEFER */ 1594 1595 if (err == -EPROBE_DEFER) { 1595 - return ERR_PTR(err); 1596 + return err; 1596 1597 } else if (err) { 1597 1598 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err); 1598 - return NULL; 1599 + return -ENODEV; 1599 1600 } 1600 - return acpi_iommu_fwspec_ops(dev); 1601 + if (!acpi_iommu_fwspec_ops(dev)) 1602 + return -ENODEV; 1603 + return 0; 1601 1604 } 1602 1605 1603 1606 #else /* !CONFIG_IOMMU_API */ ··· 1611 1610 return -ENODEV; 1612 1611 } 1613 1612 1614 - static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev, 1615 - const u32 *id_in) 1613 + static int acpi_iommu_configure_id(struct device *dev, const u32 *id_in) 1616 1614 { 1617 - return NULL; 1615 + return -ENODEV; 1618 1616 } 1619 1617 1620 1618 #endif /* !CONFIG_IOMMU_API */ ··· 1627 1627 int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr, 1628 1628 const u32 *input_id) 1629 1629 { 1630 - const struct iommu_ops *iommu; 1630 + int ret; 1631 1631 1632 1632 if (attr == DEV_DMA_NOT_SUPPORTED) { 1633 1633 set_dma_ops(dev, &dma_dummy_ops); ··· 1636 1636 1637 1637 acpi_arch_dma_setup(dev); 1638 1638 1639 - iommu = acpi_iommu_configure_id(dev, input_id); 1640 - if (PTR_ERR(iommu) == -EPROBE_DEFER) 1639 + ret = acpi_iommu_configure_id(dev, input_id); 1640 + if (ret == -EPROBE_DEFER) 1641 1641 return -EPROBE_DEFER; 1642 1642 1643 - arch_setup_dma_ops(dev, 0, U64_MAX, 1644 - iommu, attr == DEV_DMA_COHERENT); 1643 + /* 1644 + * Historically this routine doesn't fail driver probing due to errors 1645 + * in acpi_iommu_configure_id() 1646 + */ 1647 + 1648 + arch_setup_dma_ops(dev, 0, U64_MAX, attr == DEV_DMA_COHERENT); 1645 1649 1646 1650 return 0; 1647 1651 }
+3 -5
drivers/dma/tegra186-gpc-dma.c
··· 1348 1348 static int tegra_dma_probe(struct platform_device *pdev) 1349 1349 { 1350 1350 const struct tegra_dma_chip_data *cdata = NULL; 1351 - struct iommu_fwspec *iommu_spec; 1352 - unsigned int stream_id, i; 1351 + unsigned int i; 1352 + u32 stream_id; 1353 1353 struct tegra_dma *tdma; 1354 1354 int ret; 1355 1355 ··· 1378 1378 1379 1379 tdma->dma_dev.dev = &pdev->dev; 1380 1380 1381 - iommu_spec = dev_iommu_fwspec_get(&pdev->dev); 1382 - if (!iommu_spec) { 1381 + if (!tegra_dev_iommu_get_stream_id(&pdev->dev, &stream_id)) { 1383 1382 dev_err(&pdev->dev, "Missing iommu stream-id\n"); 1384 1383 return -EINVAL; 1385 1384 } 1386 - stream_id = iommu_spec->ids[0] & 0xffff; 1387 1385 1388 1386 ret = device_property_read_u32(&pdev->dev, "dma-channel-mask", 1389 1387 &tdma->chan_mask);
+2 -7
drivers/gpu/drm/nouveau/nvkm/subdev/ltc/gp10b.c
··· 28 28 gp10b_ltc_init(struct nvkm_ltc *ltc) 29 29 { 30 30 struct nvkm_device *device = ltc->subdev.device; 31 - struct iommu_fwspec *spec; 31 + u32 sid; 32 32 33 33 nvkm_wr32(device, 0x17e27c, ltc->ltc_nr); 34 34 nvkm_wr32(device, 0x17e000, ltc->ltc_nr); 35 35 nvkm_wr32(device, 0x100800, ltc->ltc_nr); 36 36 37 - spec = dev_iommu_fwspec_get(device->dev); 38 - if (spec) { 39 - u32 sid = spec->ids[0] & 0xffff; 40 - 41 - /* stream ID */ 37 + if (tegra_dev_iommu_get_stream_id(device->dev, &sid)) 42 38 nvkm_wr32(device, 0x160000, sid << 2); 43 - } 44 39 } 45 40 46 41 static const struct nvkm_ltc_func
+1 -1
drivers/hv/hv_common.c
··· 488 488 * Hyper-V does not offer a vIOMMU in the guest 489 489 * VM, so pass 0/NULL for the IOMMU settings 490 490 */ 491 - arch_setup_dma_ops(dev, 0, 0, NULL, coherent); 491 + arch_setup_dma_ops(dev, 0, 0, coherent); 492 492 } 493 493 EXPORT_SYMBOL_GPL(hv_setup_dma_ops); 494 494
+1
drivers/iommu/Kconfig
··· 160 160 161 161 # Shared Virtual Addressing 162 162 config IOMMU_SVA 163 + select IOMMU_MM_DATA 163 164 bool 164 165 165 166 config FSL_PAMU
+7 -1
drivers/iommu/amd/amd_iommu.h
··· 53 53 void amd_iommu_pdev_disable_cap_pri(struct pci_dev *pdev); 54 54 55 55 int amd_iommu_flush_page(struct iommu_domain *dom, u32 pasid, u64 address); 56 + /* 57 + * This function flushes all internal caches of 58 + * the IOMMU used by this driver. 59 + */ 60 + void amd_iommu_flush_all_caches(struct amd_iommu *iommu); 56 61 void amd_iommu_update_and_flush_device_table(struct protection_domain *domain); 57 62 void amd_iommu_domain_update(struct protection_domain *domain); 58 63 void amd_iommu_domain_flush_complete(struct protection_domain *domain); 59 - void amd_iommu_domain_flush_tlb_pde(struct protection_domain *domain); 64 + void amd_iommu_domain_flush_pages(struct protection_domain *domain, 65 + u64 address, size_t size); 60 66 int amd_iommu_flush_tlb(struct iommu_domain *dom, u32 pasid); 61 67 int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, u32 pasid, 62 68 unsigned long cr3);
-6
drivers/iommu/amd/amd_iommu_types.h
··· 902 902 extern u64 amd_iommu_efr; 903 903 extern u64 amd_iommu_efr2; 904 904 905 - /* 906 - * This function flushes all internal caches of 907 - * the IOMMU used by this driver. 908 - */ 909 - void iommu_flush_all_caches(struct amd_iommu *iommu); 910 - 911 905 static inline int get_ioapic_devid(int id) 912 906 { 913 907 struct devid_map *entry;
+4 -4
drivers/iommu/amd/init.c
··· 2223 2223 init_device_table_dma(pci_seg); 2224 2224 2225 2225 for_each_iommu(iommu) 2226 - iommu_flush_all_caches(iommu); 2226 + amd_iommu_flush_all_caches(iommu); 2227 2227 2228 2228 print_iommu_info(); 2229 2229 ··· 2773 2773 iommu_enable_xt(iommu); 2774 2774 iommu_enable_irtcachedis(iommu); 2775 2775 iommu_enable(iommu); 2776 - iommu_flush_all_caches(iommu); 2776 + amd_iommu_flush_all_caches(iommu); 2777 2777 } 2778 2778 2779 2779 /* ··· 2829 2829 iommu_enable_xt(iommu); 2830 2830 iommu_enable_irtcachedis(iommu); 2831 2831 iommu_set_device_table(iommu); 2832 - iommu_flush_all_caches(iommu); 2832 + amd_iommu_flush_all_caches(iommu); 2833 2833 } 2834 2834 } 2835 2835 } ··· 3293 3293 uninit_device_table_dma(pci_seg); 3294 3294 3295 3295 for_each_iommu(iommu) 3296 - iommu_flush_all_caches(iommu); 3296 + amd_iommu_flush_all_caches(iommu); 3297 3297 } 3298 3298 } 3299 3299 return ret;
+3 -2
drivers/iommu/amd/io_pgtable.c
··· 369 369 bool updated = false; 370 370 u64 __pte, *pte; 371 371 int ret, i, count; 372 + size_t size = pgcount << __ffs(pgsize); 373 + unsigned long o_iova = iova; 372 374 373 375 BUG_ON(!IS_ALIGNED(iova, pgsize)); 374 376 BUG_ON(!IS_ALIGNED(paddr, pgsize)); ··· 426 424 * Updates and flushing already happened in 427 425 * increase_address_space(). 428 426 */ 429 - amd_iommu_domain_flush_tlb_pde(dom); 430 - amd_iommu_domain_flush_complete(dom); 427 + amd_iommu_domain_flush_pages(dom, o_iova, size); 431 428 spin_unlock_irqrestore(&dom->lock, flags); 432 429 } 433 430
+2 -8
drivers/iommu/amd/io_pgtable_v2.c
··· 244 244 unsigned long mapped_size = 0; 245 245 unsigned long o_iova = iova; 246 246 size_t size = pgcount << __ffs(pgsize); 247 - int count = 0; 248 247 int ret = 0; 249 248 bool updated = false; 250 249 ··· 264 265 265 266 *pte = set_pte_attr(paddr, map_size, prot); 266 267 267 - count++; 268 268 iova += map_size; 269 269 paddr += map_size; 270 270 mapped_size += map_size; 271 271 } 272 272 273 273 out: 274 - if (updated) { 275 - if (count > 1) 276 - amd_iommu_flush_tlb(&pdom->domain, 0); 277 - else 278 - amd_iommu_flush_page(&pdom->domain, 0, o_iova); 279 - } 274 + if (updated) 275 + amd_iommu_domain_flush_pages(pdom, o_iova, size); 280 276 281 277 if (mapped) 282 278 *mapped += mapped_size;
+90 -99
drivers/iommu/amd/iommu.c
··· 64 64 LIST_HEAD(acpihid_map); 65 65 66 66 const struct iommu_ops amd_iommu_ops; 67 - const struct iommu_dirty_ops amd_dirty_ops; 67 + static const struct iommu_dirty_ops amd_dirty_ops; 68 68 69 69 int amd_iommu_max_glx_val = -1; 70 70 ··· 84 84 * Helper functions 85 85 * 86 86 ****************************************************************************/ 87 + 88 + static inline bool pdom_is_v2_pgtbl_mode(struct protection_domain *pdom) 89 + { 90 + return (pdom && (pdom->flags & PD_IOMMUV2_MASK)); 91 + } 87 92 88 93 static inline int get_acpihid_device_id(struct device *dev, 89 94 struct acpihid_map_entry **entry) ··· 555 550 556 551 if (dev_data->domain) 557 552 detach_device(dev); 558 - 559 - dev_iommu_priv_set(dev, NULL); 560 553 561 554 /* 562 555 * We keep dev_data around for unplugged devices and reuse it when the ··· 1127 1124 } 1128 1125 1129 1126 static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address, 1130 - size_t size, u16 domid, int pde) 1127 + size_t size, u16 domid, 1128 + ioasid_t pasid, bool gn) 1131 1129 { 1132 1130 u64 inv_address = build_inv_address(address, size); 1133 1131 1134 1132 memset(cmd, 0, sizeof(*cmd)); 1133 + 1135 1134 cmd->data[1] |= domid; 1136 1135 cmd->data[2] = lower_32_bits(inv_address); 1137 1136 cmd->data[3] = upper_32_bits(inv_address); 1137 + /* PDE bit - we want to flush everything, not only the PTEs */ 1138 + cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK; 1139 + if (gn) { 1140 + cmd->data[0] |= pasid; 1141 + cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK; 1142 + } 1138 1143 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES); 1139 - if (pde) /* PDE bit - we want to flush everything, not only the PTEs */ 1140 - cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK; 1141 1144 } 1142 1145 1143 1146 static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep, 1144 - u64 address, size_t size) 1147 + u64 address, size_t size, 1148 + ioasid_t pasid, bool gn) 1145 1149 { 1146 1150 u64 inv_address = build_inv_address(address, size); 1147 1151 1148 1152 memset(cmd, 0, sizeof(*cmd)); 1153 + 1149 1154 cmd->data[0] = devid; 1150 1155 cmd->data[0] |= (qdep & 0xff) << 24; 1151 1156 cmd->data[1] = devid; 1152 1157 cmd->data[2] = lower_32_bits(inv_address); 1153 1158 cmd->data[3] = upper_32_bits(inv_address); 1154 - CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES); 1155 - } 1159 + if (gn) { 1160 + cmd->data[0] |= ((pasid >> 8) & 0xff) << 16; 1161 + cmd->data[1] |= (pasid & 0xff) << 16; 1162 + cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK; 1163 + } 1156 1164 1157 - static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, u32 pasid, 1158 - u64 address, bool size) 1159 - { 1160 - memset(cmd, 0, sizeof(*cmd)); 1161 - 1162 - address &= ~(0xfffULL); 1163 - 1164 - cmd->data[0] = pasid; 1165 - cmd->data[1] = domid; 1166 - cmd->data[2] = lower_32_bits(address); 1167 - cmd->data[3] = upper_32_bits(address); 1168 - cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK; 1169 - cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK; 1170 - if (size) 1171 - cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; 1172 - CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES); 1173 - } 1174 - 1175 - static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, u32 pasid, 1176 - int qdep, u64 address, bool size) 1177 - { 1178 - memset(cmd, 0, sizeof(*cmd)); 1179 - 1180 - address &= ~(0xfffULL); 1181 - 1182 - cmd->data[0] = devid; 1183 - cmd->data[0] |= ((pasid >> 8) & 0xff) << 16; 1184 - cmd->data[0] |= (qdep & 0xff) << 24; 1185 - cmd->data[1] = devid; 1186 - cmd->data[1] |= (pasid & 0xff) << 16; 1187 - cmd->data[2] = lower_32_bits(address); 1188 - cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK; 1189 - cmd->data[3] = upper_32_bits(address); 1190 - if (size) 1191 - cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; 1192 1165 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES); 1193 1166 } 1194 1167 ··· 1320 1341 for (dom_id = 0; dom_id <= last_bdf; ++dom_id) { 1321 1342 struct iommu_cmd cmd; 1322 1343 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1323 - dom_id, 1); 1344 + dom_id, IOMMU_NO_PASID, false); 1324 1345 iommu_queue_command(iommu, &cmd); 1325 1346 } 1326 1347 ··· 1332 1353 struct iommu_cmd cmd; 1333 1354 1334 1355 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1335 - dom_id, 1); 1356 + dom_id, IOMMU_NO_PASID, false); 1336 1357 iommu_queue_command(iommu, &cmd); 1337 1358 1338 1359 iommu_completion_wait(iommu); ··· 1371 1392 iommu_completion_wait(iommu); 1372 1393 } 1373 1394 1374 - void iommu_flush_all_caches(struct amd_iommu *iommu) 1395 + void amd_iommu_flush_all_caches(struct amd_iommu *iommu) 1375 1396 { 1376 1397 if (check_feature(FEATURE_IA)) { 1377 1398 amd_iommu_flush_all(iommu); ··· 1385 1406 /* 1386 1407 * Command send function for flushing on-device TLB 1387 1408 */ 1388 - static int device_flush_iotlb(struct iommu_dev_data *dev_data, 1389 - u64 address, size_t size) 1409 + static int device_flush_iotlb(struct iommu_dev_data *dev_data, u64 address, 1410 + size_t size, ioasid_t pasid, bool gn) 1390 1411 { 1391 1412 struct amd_iommu *iommu; 1392 1413 struct iommu_cmd cmd; ··· 1397 1418 if (!iommu) 1398 1419 return -EINVAL; 1399 1420 1400 - build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size); 1421 + build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, 1422 + size, pasid, gn); 1401 1423 1402 1424 return iommu_queue_command(iommu, &cmd); 1403 1425 } ··· 1444 1464 return ret; 1445 1465 } 1446 1466 1447 - if (dev_data->ats_enabled) 1448 - ret = device_flush_iotlb(dev_data, 0, ~0UL); 1467 + if (dev_data->ats_enabled) { 1468 + /* Invalidate the entire contents of an IOTLB */ 1469 + ret = device_flush_iotlb(dev_data, 0, ~0UL, 1470 + IOMMU_NO_PASID, false); 1471 + } 1449 1472 1450 1473 return ret; 1451 1474 } ··· 1459 1476 * page. Otherwise it flushes the whole TLB of the IOMMU. 1460 1477 */ 1461 1478 static void __domain_flush_pages(struct protection_domain *domain, 1462 - u64 address, size_t size, int pde) 1479 + u64 address, size_t size) 1463 1480 { 1464 1481 struct iommu_dev_data *dev_data; 1465 1482 struct iommu_cmd cmd; 1466 1483 int ret = 0, i; 1484 + ioasid_t pasid = IOMMU_NO_PASID; 1485 + bool gn = false; 1467 1486 1468 - build_inv_iommu_pages(&cmd, address, size, domain->id, pde); 1487 + if (pdom_is_v2_pgtbl_mode(domain)) 1488 + gn = true; 1489 + 1490 + build_inv_iommu_pages(&cmd, address, size, domain->id, pasid, gn); 1469 1491 1470 1492 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) { 1471 1493 if (!domain->dev_iommu[i]) ··· 1488 1500 if (!dev_data->ats_enabled) 1489 1501 continue; 1490 1502 1491 - ret |= device_flush_iotlb(dev_data, address, size); 1503 + ret |= device_flush_iotlb(dev_data, address, size, pasid, gn); 1492 1504 } 1493 1505 1494 1506 WARN_ON(ret); 1495 1507 } 1496 1508 1497 - static void domain_flush_pages(struct protection_domain *domain, 1498 - u64 address, size_t size, int pde) 1509 + void amd_iommu_domain_flush_pages(struct protection_domain *domain, 1510 + u64 address, size_t size) 1499 1511 { 1500 1512 if (likely(!amd_iommu_np_cache)) { 1501 - __domain_flush_pages(domain, address, size, pde); 1513 + __domain_flush_pages(domain, address, size); 1514 + 1515 + /* Wait until IOMMU TLB and all device IOTLB flushes are complete */ 1516 + amd_iommu_domain_flush_complete(domain); 1517 + 1502 1518 return; 1503 1519 } 1504 1520 ··· 1535 1543 1536 1544 flush_size = 1ul << min_alignment; 1537 1545 1538 - __domain_flush_pages(domain, address, flush_size, pde); 1546 + __domain_flush_pages(domain, address, flush_size); 1539 1547 address += flush_size; 1540 1548 size -= flush_size; 1541 1549 } 1550 + 1551 + /* Wait until IOMMU TLB and all device IOTLB flushes are complete */ 1552 + amd_iommu_domain_flush_complete(domain); 1542 1553 } 1543 1554 1544 1555 /* Flush the whole IO/TLB for a given protection domain - including PDE */ 1545 - void amd_iommu_domain_flush_tlb_pde(struct protection_domain *domain) 1556 + static void amd_iommu_domain_flush_all(struct protection_domain *domain) 1546 1557 { 1547 - domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1); 1558 + amd_iommu_domain_flush_pages(domain, 0, 1559 + CMD_INV_IOMMU_ALL_PAGES_ADDRESS); 1548 1560 } 1549 1561 1550 1562 void amd_iommu_domain_flush_complete(struct protection_domain *domain) ··· 1575 1579 unsigned long flags; 1576 1580 1577 1581 spin_lock_irqsave(&domain->lock, flags); 1578 - domain_flush_pages(domain, iova, size, 1); 1579 - amd_iommu_domain_flush_complete(domain); 1582 + amd_iommu_domain_flush_pages(domain, iova, size); 1580 1583 spin_unlock_irqrestore(&domain->lock, flags); 1581 1584 } 1582 1585 } ··· 1853 1858 /* Flush the DTE entry */ 1854 1859 device_flush_dte(dev_data); 1855 1860 1856 - /* Flush IOTLB */ 1857 - amd_iommu_domain_flush_tlb_pde(domain); 1858 - 1859 - /* Wait for the flushes to finish */ 1860 - amd_iommu_domain_flush_complete(domain); 1861 + /* Flush IOTLB and wait for the flushes to finish */ 1862 + amd_iommu_domain_flush_all(domain); 1861 1863 1862 1864 /* decrease reference counters - needs to happen after the flushes */ 1863 1865 domain->dev_iommu[iommu->index] -= 1; ··· 1887 1895 pdev_enable_caps(to_pci_dev(dev)); 1888 1896 1889 1897 do_attach(dev_data, domain); 1890 - 1891 - /* 1892 - * We might boot into a crash-kernel here. The crashed kernel 1893 - * left the caches in the IOMMU dirty. So we have to flush 1894 - * here to evict all dirty stuff. 1895 - */ 1896 - amd_iommu_domain_flush_tlb_pde(domain); 1897 - 1898 - amd_iommu_domain_flush_complete(domain); 1899 1898 1900 1899 out: 1901 1900 spin_unlock(&dev_data->lock); ··· 2031 2048 amd_iommu_update_and_flush_device_table(domain); 2032 2049 2033 2050 /* Flush domain TLB(s) and wait for completion */ 2034 - amd_iommu_domain_flush_tlb_pde(domain); 2035 - amd_iommu_domain_flush_complete(domain); 2051 + amd_iommu_domain_flush_all(domain); 2036 2052 } 2037 2053 2038 2054 /***************************************************************************** ··· 2464 2482 } 2465 2483 2466 2484 /* Flush IOTLB to mark IOPTE dirty on the next translation(s) */ 2467 - if (domain_flush) { 2468 - amd_iommu_domain_flush_tlb_pde(pdomain); 2469 - amd_iommu_domain_flush_complete(pdomain); 2470 - } 2485 + if (domain_flush) 2486 + amd_iommu_domain_flush_all(pdomain); 2487 + 2471 2488 pdomain->dirty_tracking = enable; 2472 2489 spin_unlock_irqrestore(&pdomain->lock, flags); 2473 2490 ··· 2569 2588 unsigned long flags; 2570 2589 2571 2590 spin_lock_irqsave(&dom->lock, flags); 2572 - amd_iommu_domain_flush_tlb_pde(dom); 2573 - amd_iommu_domain_flush_complete(dom); 2591 + amd_iommu_domain_flush_all(dom); 2574 2592 spin_unlock_irqrestore(&dom->lock, flags); 2575 2593 } 2576 2594 ··· 2580 2600 unsigned long flags; 2581 2601 2582 2602 spin_lock_irqsave(&dom->lock, flags); 2583 - domain_flush_pages(dom, gather->start, gather->end - gather->start + 1, 1); 2584 - amd_iommu_domain_flush_complete(dom); 2603 + amd_iommu_domain_flush_pages(dom, gather->start, 2604 + gather->end - gather->start + 1); 2585 2605 spin_unlock_irqrestore(&dom->lock, flags); 2586 2606 } 2587 2607 ··· 2615 2635 return true; 2616 2636 } 2617 2637 2618 - const struct iommu_dirty_ops amd_dirty_ops = { 2638 + static const struct iommu_dirty_ops amd_dirty_ops = { 2619 2639 .set_dirty_tracking = amd_iommu_set_dirty_tracking, 2620 2640 .read_and_clear_dirty = amd_iommu_read_and_clear_dirty, 2621 2641 }; ··· 2646 2666 }; 2647 2667 2648 2668 static int __flush_pasid(struct protection_domain *domain, u32 pasid, 2649 - u64 address, bool size) 2669 + u64 address, size_t size) 2650 2670 { 2651 2671 struct iommu_dev_data *dev_data; 2652 2672 struct iommu_cmd cmd; ··· 2655 2675 if (!(domain->flags & PD_IOMMUV2_MASK)) 2656 2676 return -EINVAL; 2657 2677 2658 - build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size); 2678 + build_inv_iommu_pages(&cmd, address, size, domain->id, pasid, true); 2659 2679 2660 2680 /* 2661 2681 * IOMMU TLB needs to be flushed before Device TLB to ··· 2689 2709 iommu = rlookup_amd_iommu(dev_data->dev); 2690 2710 if (!iommu) 2691 2711 continue; 2692 - build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid, 2693 - qdep, address, size); 2712 + build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, 2713 + address, size, pasid, true); 2694 2714 2695 2715 ret = iommu_queue_command(iommu, &cmd); 2696 2716 if (ret != 0) ··· 2710 2730 static int __amd_iommu_flush_page(struct protection_domain *domain, u32 pasid, 2711 2731 u64 address) 2712 2732 { 2713 - return __flush_pasid(domain, pasid, address, false); 2733 + return __flush_pasid(domain, pasid, address, PAGE_SIZE); 2714 2734 } 2715 2735 2716 2736 int amd_iommu_flush_page(struct iommu_domain *dom, u32 pasid, ··· 2729 2749 2730 2750 static int __amd_iommu_flush_tlb(struct protection_domain *domain, u32 pasid) 2731 2751 { 2732 - return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 2733 - true); 2752 + return __flush_pasid(domain, pasid, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS); 2734 2753 } 2735 2754 2736 2755 int amd_iommu_flush_tlb(struct iommu_domain *dom, u32 pasid) ··· 3090 3111 return index; 3091 3112 } 3092 3113 3093 - static int modify_irte_ga(struct amd_iommu *iommu, u16 devid, int index, 3094 - struct irte_ga *irte) 3114 + static int __modify_irte_ga(struct amd_iommu *iommu, u16 devid, int index, 3115 + struct irte_ga *irte) 3095 3116 { 3096 3117 struct irq_remap_table *table; 3097 3118 struct irte_ga *entry; ··· 3117 3138 WARN_ON(!try_cmpxchg128(&entry->irte, &old, irte->irte)); 3118 3139 3119 3140 raw_spin_unlock_irqrestore(&table->lock, flags); 3141 + 3142 + return 0; 3143 + } 3144 + 3145 + static int modify_irte_ga(struct amd_iommu *iommu, u16 devid, int index, 3146 + struct irte_ga *irte) 3147 + { 3148 + bool ret; 3149 + 3150 + ret = __modify_irte_ga(iommu, devid, index, irte); 3151 + if (ret) 3152 + return ret; 3120 3153 3121 3154 iommu_flush_irt_and_complete(iommu, devid); 3122 3155 ··· 3813 3822 } 3814 3823 entry->lo.fields_vapic.is_run = is_run; 3815 3824 3816 - return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid, 3817 - ir_data->irq_2_irte.index, entry); 3825 + return __modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid, 3826 + ir_data->irq_2_irte.index, entry); 3818 3827 } 3819 3828 EXPORT_SYMBOL(amd_iommu_update_ga); 3820 3829 #endif
+35 -5
drivers/iommu/apple-dart.c
··· 81 81 #define DART_T8020_TCR_BYPASS_DAPF BIT(12) 82 82 83 83 #define DART_T8020_TTBR 0x200 84 + #define DART_T8020_USB4_TTBR 0x400 84 85 #define DART_T8020_TTBR_VALID BIT(31) 85 86 #define DART_T8020_TTBR_ADDR_FIELD_SHIFT 0 86 87 #define DART_T8020_TTBR_SHIFT 12 ··· 369 368 u32 command) 370 369 { 371 370 unsigned long flags; 372 - int ret; 371 + int ret, i; 373 372 u32 command_reg; 374 373 375 374 spin_lock_irqsave(&stream_map->dart->lock, flags); 376 375 377 - writel(stream_map->sidmap[0], stream_map->dart->regs + DART_T8020_STREAM_SELECT); 376 + for (i = 0; i < BITS_TO_U32(stream_map->dart->num_streams); i++) 377 + writel(stream_map->sidmap[i], 378 + stream_map->dart->regs + DART_T8020_STREAM_SELECT + 4 * i); 378 379 writel(command, stream_map->dart->regs + DART_T8020_STREAM_COMMAND); 379 380 380 381 ret = readl_poll_timeout_atomic( ··· 743 740 { 744 741 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev); 745 742 746 - dev_iommu_priv_set(dev, NULL); 747 743 kfree(cfg); 748 744 } 749 745 ··· 910 908 911 909 ret = apple_dart_merge_master_cfg(group_master_cfg, cfg); 912 910 if (ret) { 913 - dev_err(dev, "Failed to merge DART IOMMU grups.\n"); 911 + dev_err(dev, "Failed to merge DART IOMMU groups.\n"); 914 912 iommu_group_put(group); 915 913 res = ERR_PTR(ret); 916 914 goto out; ··· 1217 1215 .ttbr_shift = DART_T8020_TTBR_SHIFT, 1218 1216 .ttbr_count = 4, 1219 1217 }; 1218 + 1219 + static const struct apple_dart_hw apple_dart_hw_t8103_usb4 = { 1220 + .type = DART_T8020, 1221 + .irq_handler = apple_dart_t8020_irq, 1222 + .invalidate_tlb = apple_dart_t8020_hw_invalidate_tlb, 1223 + .oas = 36, 1224 + .fmt = APPLE_DART, 1225 + .max_sid_count = 64, 1226 + 1227 + .enable_streams = DART_T8020_STREAMS_ENABLE, 1228 + .lock = DART_T8020_CONFIG, 1229 + .lock_bit = DART_T8020_CONFIG_LOCK, 1230 + 1231 + .error = DART_T8020_ERROR, 1232 + 1233 + .tcr = DART_T8020_TCR, 1234 + .tcr_enabled = DART_T8020_TCR_TRANSLATE_ENABLE, 1235 + .tcr_disabled = 0, 1236 + .tcr_bypass = 0, 1237 + 1238 + .ttbr = DART_T8020_USB4_TTBR, 1239 + .ttbr_valid = DART_T8020_TTBR_VALID, 1240 + .ttbr_addr_field_shift = DART_T8020_TTBR_ADDR_FIELD_SHIFT, 1241 + .ttbr_shift = DART_T8020_TTBR_SHIFT, 1242 + .ttbr_count = 4, 1243 + }; 1244 + 1220 1245 static const struct apple_dart_hw apple_dart_hw_t6000 = { 1221 1246 .type = DART_T6000, 1222 1247 .irq_handler = apple_dart_t8020_irq, ··· 1301 1272 unsigned int sid, idx; 1302 1273 1303 1274 for (sid = 0; sid < dart->num_streams; sid++) { 1304 - dart->save_tcr[sid] = readl_relaxed(dart->regs + DART_TCR(dart, sid)); 1275 + dart->save_tcr[sid] = readl(dart->regs + DART_TCR(dart, sid)); 1305 1276 for (idx = 0; idx < dart->hw->ttbr_count; idx++) 1306 1277 dart->save_ttbr[sid][idx] = 1307 1278 readl(dart->regs + DART_TTBR(dart, sid, idx)); ··· 1336 1307 1337 1308 static const struct of_device_id apple_dart_of_match[] = { 1338 1309 { .compatible = "apple,t8103-dart", .data = &apple_dart_hw_t8103 }, 1310 + { .compatible = "apple,t8103-usb4-dart", .data = &apple_dart_hw_t8103_usb4 }, 1339 1311 { .compatible = "apple,t8110-dart", .data = &apple_dart_hw_t8110 }, 1340 1312 { .compatible = "apple,t6000-dart", .data = &apple_dart_hw_t6000 }, 1341 1313 {},
+15 -8
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c
··· 246 246 smmu_domain); 247 247 } 248 248 249 - arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, start, size); 249 + arm_smmu_atc_inv_domain(smmu_domain, mm_get_enqcmd_pasid(mm), start, 250 + size); 250 251 } 251 252 252 253 static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) ··· 265 264 * DMA may still be running. Keep the cd valid to avoid C_BAD_CD events, 266 265 * but disable translation. 267 266 */ 268 - arm_smmu_update_ctx_desc_devices(smmu_domain, mm->pasid, &quiet_cd); 267 + arm_smmu_update_ctx_desc_devices(smmu_domain, mm_get_enqcmd_pasid(mm), 268 + &quiet_cd); 269 269 270 270 arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_mn->cd->asid); 271 - arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, 0, 0); 271 + arm_smmu_atc_inv_domain(smmu_domain, mm_get_enqcmd_pasid(mm), 0, 0); 272 272 273 273 smmu_mn->cleared = true; 274 274 mutex_unlock(&sva_lock); ··· 327 325 328 326 spin_lock_irqsave(&smmu_domain->devices_lock, flags); 329 327 list_for_each_entry(master, &smmu_domain->devices, domain_head) { 330 - ret = arm_smmu_write_ctx_desc(master, mm->pasid, cd); 328 + ret = arm_smmu_write_ctx_desc(master, mm_get_enqcmd_pasid(mm), 329 + cd); 331 330 if (ret) { 332 - list_for_each_entry_from_reverse(master, &smmu_domain->devices, domain_head) 333 - arm_smmu_write_ctx_desc(master, mm->pasid, NULL); 331 + list_for_each_entry_from_reverse( 332 + master, &smmu_domain->devices, domain_head) 333 + arm_smmu_write_ctx_desc( 334 + master, mm_get_enqcmd_pasid(mm), NULL); 334 335 break; 335 336 } 336 337 } ··· 363 358 364 359 list_del(&smmu_mn->list); 365 360 366 - arm_smmu_update_ctx_desc_devices(smmu_domain, mm->pasid, NULL); 361 + arm_smmu_update_ctx_desc_devices(smmu_domain, mm_get_enqcmd_pasid(mm), 362 + NULL); 367 363 368 364 /* 369 365 * If we went through clear(), we've already invalidated, and no ··· 372 366 */ 373 367 if (!smmu_mn->cleared) { 374 368 arm_smmu_tlb_inv_asid(smmu_domain->smmu, cd->asid); 375 - arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, 0, 0); 369 + arm_smmu_atc_inv_domain(smmu_domain, mm_get_enqcmd_pasid(mm), 0, 370 + 0); 376 371 } 377 372 378 373 /* Frees smmu_mn */
+35 -44
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
··· 1063 1063 bool cd_live; 1064 1064 __le64 *cdptr; 1065 1065 struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; 1066 + struct arm_smmu_device *smmu = master->smmu; 1066 1067 1067 1068 if (WARN_ON(ssid >= (1 << cd_table->s1cdmax))) 1068 1069 return -E2BIG; ··· 1078 1077 if (!cd) { /* (5) */ 1079 1078 val = 0; 1080 1079 } else if (cd == &quiet_cd) { /* (4) */ 1080 + if (!(smmu->features & ARM_SMMU_FEAT_STALL_FORCE)) 1081 + val &= ~(CTXDESC_CD_0_S | CTXDESC_CD_0_R); 1081 1082 val |= CTXDESC_CD_0_TCR_EPD0; 1082 1083 } else if (cd_live) { /* (3) */ 1083 1084 val &= ~CTXDESC_CD_0_ASID; ··· 1252 1249 } 1253 1250 1254 1251 static void arm_smmu_write_strtab_ent(struct arm_smmu_master *master, u32 sid, 1255 - __le64 *dst) 1252 + struct arm_smmu_ste *dst) 1256 1253 { 1257 1254 /* 1258 1255 * This is hideously complicated, but we only really care about ··· 1270 1267 * 2. Write everything apart from dword 0, sync, write dword 0, sync 1271 1268 * 3. Update Config, sync 1272 1269 */ 1273 - u64 val = le64_to_cpu(dst[0]); 1270 + u64 val = le64_to_cpu(dst->data[0]); 1274 1271 bool ste_live = false; 1275 - struct arm_smmu_device *smmu = NULL; 1272 + struct arm_smmu_device *smmu = master->smmu; 1276 1273 struct arm_smmu_ctx_desc_cfg *cd_table = NULL; 1277 1274 struct arm_smmu_s2_cfg *s2_cfg = NULL; 1278 - struct arm_smmu_domain *smmu_domain = NULL; 1275 + struct arm_smmu_domain *smmu_domain = master->domain; 1279 1276 struct arm_smmu_cmdq_ent prefetch_cmd = { 1280 1277 .opcode = CMDQ_OP_PREFETCH_CFG, 1281 1278 .prefetch = { ··· 1283 1280 }, 1284 1281 }; 1285 1282 1286 - if (master) { 1287 - smmu_domain = master->domain; 1288 - smmu = master->smmu; 1289 - } 1290 - 1291 1283 if (smmu_domain) { 1292 1284 switch (smmu_domain->stage) { 1293 1285 case ARM_SMMU_DOMAIN_S1: 1294 1286 cd_table = &master->cd_table; 1295 1287 break; 1296 1288 case ARM_SMMU_DOMAIN_S2: 1297 - case ARM_SMMU_DOMAIN_NESTED: 1298 1289 s2_cfg = &smmu_domain->s2_cfg; 1299 1290 break; 1300 1291 default: ··· 1322 1325 else 1323 1326 val |= FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_BYPASS); 1324 1327 1325 - dst[0] = cpu_to_le64(val); 1326 - dst[1] = cpu_to_le64(FIELD_PREP(STRTAB_STE_1_SHCFG, 1328 + dst->data[0] = cpu_to_le64(val); 1329 + dst->data[1] = cpu_to_le64(FIELD_PREP(STRTAB_STE_1_SHCFG, 1327 1330 STRTAB_STE_1_SHCFG_INCOMING)); 1328 - dst[2] = 0; /* Nuke the VMID */ 1331 + dst->data[2] = 0; /* Nuke the VMID */ 1329 1332 /* 1330 1333 * The SMMU can perform negative caching, so we must sync 1331 1334 * the STE regardless of whether the old value was live. ··· 1340 1343 STRTAB_STE_1_STRW_EL2 : STRTAB_STE_1_STRW_NSEL1; 1341 1344 1342 1345 BUG_ON(ste_live); 1343 - dst[1] = cpu_to_le64( 1346 + dst->data[1] = cpu_to_le64( 1344 1347 FIELD_PREP(STRTAB_STE_1_S1DSS, STRTAB_STE_1_S1DSS_SSID0) | 1345 1348 FIELD_PREP(STRTAB_STE_1_S1CIR, STRTAB_STE_1_S1C_CACHE_WBRA) | 1346 1349 FIELD_PREP(STRTAB_STE_1_S1COR, STRTAB_STE_1_S1C_CACHE_WBRA) | ··· 1349 1352 1350 1353 if (smmu->features & ARM_SMMU_FEAT_STALLS && 1351 1354 !master->stall_enabled) 1352 - dst[1] |= cpu_to_le64(STRTAB_STE_1_S1STALLD); 1355 + dst->data[1] |= cpu_to_le64(STRTAB_STE_1_S1STALLD); 1353 1356 1354 1357 val |= (cd_table->cdtab_dma & STRTAB_STE_0_S1CTXPTR_MASK) | 1355 1358 FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_S1_TRANS) | ··· 1359 1362 1360 1363 if (s2_cfg) { 1361 1364 BUG_ON(ste_live); 1362 - dst[2] = cpu_to_le64( 1365 + dst->data[2] = cpu_to_le64( 1363 1366 FIELD_PREP(STRTAB_STE_2_S2VMID, s2_cfg->vmid) | 1364 1367 FIELD_PREP(STRTAB_STE_2_VTCR, s2_cfg->vtcr) | 1365 1368 #ifdef __BIG_ENDIAN ··· 1368 1371 STRTAB_STE_2_S2PTW | STRTAB_STE_2_S2AA64 | 1369 1372 STRTAB_STE_2_S2R); 1370 1373 1371 - dst[3] = cpu_to_le64(s2_cfg->vttbr & STRTAB_STE_3_S2TTB_MASK); 1374 + dst->data[3] = cpu_to_le64(s2_cfg->vttbr & STRTAB_STE_3_S2TTB_MASK); 1372 1375 1373 1376 val |= FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_S2_TRANS); 1374 1377 } 1375 1378 1376 1379 if (master->ats_enabled) 1377 - dst[1] |= cpu_to_le64(FIELD_PREP(STRTAB_STE_1_EATS, 1380 + dst->data[1] |= cpu_to_le64(FIELD_PREP(STRTAB_STE_1_EATS, 1378 1381 STRTAB_STE_1_EATS_TRANS)); 1379 1382 1380 1383 arm_smmu_sync_ste_for_sid(smmu, sid); 1381 1384 /* See comment in arm_smmu_write_ctx_desc() */ 1382 - WRITE_ONCE(dst[0], cpu_to_le64(val)); 1385 + WRITE_ONCE(dst->data[0], cpu_to_le64(val)); 1383 1386 arm_smmu_sync_ste_for_sid(smmu, sid); 1384 1387 1385 1388 /* It's likely that we'll want to use the new STE soon */ ··· 1387 1390 arm_smmu_cmdq_issue_cmd(smmu, &prefetch_cmd); 1388 1391 } 1389 1392 1390 - static void arm_smmu_init_bypass_stes(__le64 *strtab, unsigned int nent, bool force) 1393 + static void arm_smmu_init_bypass_stes(struct arm_smmu_ste *strtab, 1394 + unsigned int nent, bool force) 1391 1395 { 1392 1396 unsigned int i; 1393 1397 u64 val = STRTAB_STE_0_V; ··· 1399 1401 val |= FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_BYPASS); 1400 1402 1401 1403 for (i = 0; i < nent; ++i) { 1402 - strtab[0] = cpu_to_le64(val); 1403 - strtab[1] = cpu_to_le64(FIELD_PREP(STRTAB_STE_1_SHCFG, 1404 - STRTAB_STE_1_SHCFG_INCOMING)); 1405 - strtab[2] = 0; 1406 - strtab += STRTAB_STE_DWORDS; 1404 + strtab->data[0] = cpu_to_le64(val); 1405 + strtab->data[1] = cpu_to_le64(FIELD_PREP( 1406 + STRTAB_STE_1_SHCFG, STRTAB_STE_1_SHCFG_INCOMING)); 1407 + strtab->data[2] = 0; 1408 + strtab++; 1407 1409 } 1408 1410 } 1409 1411 ··· 2169 2171 fmt = ARM_64_LPAE_S1; 2170 2172 finalise_stage_fn = arm_smmu_domain_finalise_s1; 2171 2173 break; 2172 - case ARM_SMMU_DOMAIN_NESTED: 2173 2174 case ARM_SMMU_DOMAIN_S2: 2174 2175 ias = smmu->ias; 2175 2176 oas = smmu->oas; ··· 2206 2209 return 0; 2207 2210 } 2208 2211 2209 - static __le64 *arm_smmu_get_step_for_sid(struct arm_smmu_device *smmu, u32 sid) 2212 + static struct arm_smmu_ste * 2213 + arm_smmu_get_step_for_sid(struct arm_smmu_device *smmu, u32 sid) 2210 2214 { 2211 - __le64 *step; 2212 2215 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; 2213 2216 2214 2217 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) { 2215 - struct arm_smmu_strtab_l1_desc *l1_desc; 2216 - int idx; 2218 + unsigned int idx1, idx2; 2217 2219 2218 2220 /* Two-level walk */ 2219 - idx = (sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS; 2220 - l1_desc = &cfg->l1_desc[idx]; 2221 - idx = (sid & ((1 << STRTAB_SPLIT) - 1)) * STRTAB_STE_DWORDS; 2222 - step = &l1_desc->l2ptr[idx]; 2221 + idx1 = (sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS; 2222 + idx2 = sid & ((1 << STRTAB_SPLIT) - 1); 2223 + return &cfg->l1_desc[idx1].l2ptr[idx2]; 2223 2224 } else { 2224 2225 /* Simple linear lookup */ 2225 - step = &cfg->strtab[sid * STRTAB_STE_DWORDS]; 2226 + return (struct arm_smmu_ste *)&cfg 2227 + ->strtab[sid * STRTAB_STE_DWORDS]; 2226 2228 } 2227 - 2228 - return step; 2229 2229 } 2230 2230 2231 2231 static void arm_smmu_install_ste_for_dev(struct arm_smmu_master *master) ··· 2232 2238 2233 2239 for (i = 0; i < master->num_streams; ++i) { 2234 2240 u32 sid = master->streams[i].id; 2235 - __le64 *step = arm_smmu_get_step_for_sid(smmu, sid); 2241 + struct arm_smmu_ste *step = 2242 + arm_smmu_get_step_for_sid(smmu, sid); 2236 2243 2237 2244 /* Bridged PCI devices may end up with duplicated IDs */ 2238 2245 for (j = 0; j < i; j++) ··· 2644 2649 struct arm_smmu_master *master; 2645 2650 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 2646 2651 2647 - if (!fwspec || fwspec->ops != &arm_smmu_ops) 2648 - return ERR_PTR(-ENODEV); 2649 - 2650 2652 if (WARN_ON_ONCE(dev_iommu_priv_get(dev))) 2651 2653 return ERR_PTR(-EBUSY); 2652 2654 ··· 2690 2698 2691 2699 err_free_master: 2692 2700 kfree(master); 2693 - dev_iommu_priv_set(dev, NULL); 2694 2701 return ERR_PTR(ret); 2695 2702 } 2696 2703 ··· 2733 2742 if (smmu_domain->smmu) 2734 2743 ret = -EPERM; 2735 2744 else 2736 - smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED; 2745 + smmu_domain->stage = ARM_SMMU_DOMAIN_S2; 2737 2746 mutex_unlock(&smmu_domain->init_mutex); 2738 2747 2739 2748 return ret; ··· 3760 3769 iort_get_rmr_sids(dev_fwnode(smmu->dev), &rmr_list); 3761 3770 3762 3771 list_for_each_entry(e, &rmr_list, list) { 3763 - __le64 *step; 3772 + struct arm_smmu_ste *step; 3764 3773 struct iommu_iort_rmr_data *rmr; 3765 3774 int ret, i; 3766 3775
+6 -2
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
··· 206 206 #define STRTAB_L1_DESC_L2PTR_MASK GENMASK_ULL(51, 6) 207 207 208 208 #define STRTAB_STE_DWORDS 8 209 + 210 + struct arm_smmu_ste { 211 + __le64 data[STRTAB_STE_DWORDS]; 212 + }; 213 + 209 214 #define STRTAB_STE_0_V (1UL << 0) 210 215 #define STRTAB_STE_0_CFG GENMASK_ULL(3, 1) 211 216 #define STRTAB_STE_0_CFG_ABORT 0 ··· 576 571 struct arm_smmu_strtab_l1_desc { 577 572 u8 span; 578 573 579 - __le64 *l2ptr; 574 + struct arm_smmu_ste *l2ptr; 580 575 dma_addr_t l2ptr_dma; 581 576 }; 582 577 ··· 715 710 enum arm_smmu_domain_stage { 716 711 ARM_SMMU_DOMAIN_S1 = 0, 717 712 ARM_SMMU_DOMAIN_S2, 718 - ARM_SMMU_DOMAIN_NESTED, 719 713 ARM_SMMU_DOMAIN_BYPASS, 720 714 }; 721 715
+2
drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c
··· 243 243 244 244 static const struct of_device_id qcom_smmu_client_of_match[] __maybe_unused = { 245 245 { .compatible = "qcom,adreno" }, 246 + { .compatible = "qcom,adreno-gmu" }, 246 247 { .compatible = "qcom,mdp4" }, 247 248 { .compatible = "qcom,mdss" }, 249 + { .compatible = "qcom,qcm2290-mdss" }, 248 250 { .compatible = "qcom,sc7180-mdss" }, 249 251 { .compatible = "qcom,sc7180-mss-pil" }, 250 252 { .compatible = "qcom,sc7280-mdss" },
+103 -63
drivers/iommu/arm/arm-smmu/arm-smmu.c
··· 82 82 pm_runtime_put_autosuspend(smmu->dev); 83 83 } 84 84 85 + static void arm_smmu_rpm_use_autosuspend(struct arm_smmu_device *smmu) 86 + { 87 + /* 88 + * Setup an autosuspend delay to avoid bouncing runpm state. 89 + * Otherwise, if a driver for a suspended consumer device 90 + * unmaps buffers, it will runpm resume/suspend for each one. 91 + * 92 + * For example, when used by a GPU device, when an application 93 + * or game exits, it can trigger unmapping 100s or 1000s of 94 + * buffers. With a runpm cycle for each buffer, that adds up 95 + * to 5-10sec worth of reprogramming the context bank, while 96 + * the system appears to be locked up to the user. 97 + */ 98 + pm_runtime_set_autosuspend_delay(smmu->dev, 20); 99 + pm_runtime_use_autosuspend(smmu->dev); 100 + } 101 + 85 102 static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom) 86 103 { 87 104 return container_of(dom, struct arm_smmu_domain, domain); ··· 409 392 { 410 393 u32 fsr, fsynr, cbfrsynra; 411 394 unsigned long iova; 412 - struct iommu_domain *domain = dev; 413 - struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 395 + struct arm_smmu_domain *smmu_domain = dev; 414 396 struct arm_smmu_device *smmu = smmu_domain->smmu; 415 397 int idx = smmu_domain->cfg.cbndx; 416 398 int ret; ··· 422 406 iova = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_FAR); 423 407 cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(idx)); 424 408 425 - ret = report_iommu_fault(domain, NULL, iova, 409 + ret = report_iommu_fault(&smmu_domain->domain, NULL, iova, 426 410 fsynr & ARM_SMMU_FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ); 427 411 428 412 if (ret == -ENOSYS) ··· 623 607 return __arm_smmu_alloc_bitmap(smmu->context_map, start, smmu->num_context_banks); 624 608 } 625 609 626 - static int arm_smmu_init_domain_context(struct iommu_domain *domain, 610 + static int arm_smmu_init_domain_context(struct arm_smmu_domain *smmu_domain, 627 611 struct arm_smmu_device *smmu, 628 612 struct device *dev) 629 613 { ··· 632 616 struct io_pgtable_ops *pgtbl_ops; 633 617 struct io_pgtable_cfg pgtbl_cfg; 634 618 enum io_pgtable_fmt fmt; 635 - struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 619 + struct iommu_domain *domain = &smmu_domain->domain; 636 620 struct arm_smmu_cfg *cfg = &smmu_domain->cfg; 637 621 irqreturn_t (*context_fault)(int irq, void *dev); 638 622 639 623 mutex_lock(&smmu_domain->init_mutex); 640 624 if (smmu_domain->smmu) 641 625 goto out_unlock; 642 - 643 - if (domain->type == IOMMU_DOMAIN_IDENTITY) { 644 - smmu_domain->stage = ARM_SMMU_DOMAIN_BYPASS; 645 - smmu_domain->smmu = smmu; 646 - goto out_unlock; 647 - } 648 626 649 627 /* 650 628 * Mapping the requested stage onto what we support is surprisingly ··· 806 796 else 807 797 context_fault = arm_smmu_context_fault; 808 798 809 - ret = devm_request_irq(smmu->dev, irq, context_fault, 810 - IRQF_SHARED, "arm-smmu-context-fault", domain); 799 + ret = devm_request_irq(smmu->dev, irq, context_fault, IRQF_SHARED, 800 + "arm-smmu-context-fault", smmu_domain); 811 801 if (ret < 0) { 812 802 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n", 813 803 cfg->irptndx, irq); ··· 828 818 return ret; 829 819 } 830 820 831 - static void arm_smmu_destroy_domain_context(struct iommu_domain *domain) 821 + static void arm_smmu_destroy_domain_context(struct arm_smmu_domain *smmu_domain) 832 822 { 833 - struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 834 823 struct arm_smmu_device *smmu = smmu_domain->smmu; 835 824 struct arm_smmu_cfg *cfg = &smmu_domain->cfg; 836 825 int ret, irq; 837 826 838 - if (!smmu || domain->type == IOMMU_DOMAIN_IDENTITY) 827 + if (!smmu) 839 828 return; 840 829 841 830 ret = arm_smmu_rpm_get(smmu); ··· 850 841 851 842 if (cfg->irptndx != ARM_SMMU_INVALID_IRPTNDX) { 852 843 irq = smmu->irqs[cfg->irptndx]; 853 - devm_free_irq(smmu->dev, irq, domain); 844 + devm_free_irq(smmu->dev, irq, smmu_domain); 854 845 } 855 846 856 847 free_io_pgtable_ops(smmu_domain->pgtbl_ops); ··· 859 850 arm_smmu_rpm_put(smmu); 860 851 } 861 852 862 - static struct iommu_domain *arm_smmu_domain_alloc(unsigned type) 853 + static struct iommu_domain *arm_smmu_domain_alloc_paging(struct device *dev) 863 854 { 864 855 struct arm_smmu_domain *smmu_domain; 865 856 866 - if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_IDENTITY) { 867 - if (using_legacy_binding || type != IOMMU_DOMAIN_DMA) 868 - return NULL; 869 - } 870 857 /* 871 858 * Allocate the domain and initialise some of its data structures. 872 859 * We can't really do anything meaningful until we've added a ··· 875 870 mutex_init(&smmu_domain->init_mutex); 876 871 spin_lock_init(&smmu_domain->cb_lock); 877 872 873 + if (dev) { 874 + struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev); 875 + 876 + if (arm_smmu_init_domain_context(smmu_domain, cfg->smmu, dev)) { 877 + kfree(smmu_domain); 878 + return NULL; 879 + } 880 + } 881 + 878 882 return &smmu_domain->domain; 879 883 } 880 884 ··· 895 881 * Free the domain resources. We assume that all devices have 896 882 * already been detached. 897 883 */ 898 - arm_smmu_destroy_domain_context(domain); 884 + arm_smmu_destroy_domain_context(smmu_domain); 899 885 kfree(smmu_domain); 900 886 } 901 887 ··· 1095 1081 mutex_unlock(&smmu->stream_map_mutex); 1096 1082 } 1097 1083 1098 - static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain, 1099 - struct arm_smmu_master_cfg *cfg, 1100 - struct iommu_fwspec *fwspec) 1084 + static void arm_smmu_master_install_s2crs(struct arm_smmu_master_cfg *cfg, 1085 + enum arm_smmu_s2cr_type type, 1086 + u8 cbndx, struct iommu_fwspec *fwspec) 1101 1087 { 1102 - struct arm_smmu_device *smmu = smmu_domain->smmu; 1088 + struct arm_smmu_device *smmu = cfg->smmu; 1103 1089 struct arm_smmu_s2cr *s2cr = smmu->s2crs; 1104 - u8 cbndx = smmu_domain->cfg.cbndx; 1105 - enum arm_smmu_s2cr_type type; 1106 1090 int i, idx; 1107 - 1108 - if (smmu_domain->stage == ARM_SMMU_DOMAIN_BYPASS) 1109 - type = S2CR_TYPE_BYPASS; 1110 - else 1111 - type = S2CR_TYPE_TRANS; 1112 1091 1113 1092 for_each_cfg_sme(cfg, fwspec, i, idx) { 1114 1093 if (type == s2cr[idx].type && cbndx == s2cr[idx].cbndx) ··· 1112 1105 s2cr[idx].cbndx = cbndx; 1113 1106 arm_smmu_write_s2cr(smmu, idx); 1114 1107 } 1115 - return 0; 1116 1108 } 1117 1109 1118 1110 static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) ··· 1121 1115 struct arm_smmu_master_cfg *cfg; 1122 1116 struct arm_smmu_device *smmu; 1123 1117 int ret; 1124 - 1125 - if (!fwspec || fwspec->ops != &arm_smmu_ops) { 1126 - dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n"); 1127 - return -ENXIO; 1128 - } 1129 1118 1130 1119 /* 1131 1120 * FIXME: The arch/arm DMA API code tries to attach devices to its own ··· 1140 1139 return ret; 1141 1140 1142 1141 /* Ensure that the domain is finalised */ 1143 - ret = arm_smmu_init_domain_context(domain, smmu, dev); 1142 + ret = arm_smmu_init_domain_context(smmu_domain, smmu, dev); 1144 1143 if (ret < 0) 1145 1144 goto rpm_put; 1146 1145 ··· 1154 1153 } 1155 1154 1156 1155 /* Looks ok, so add the device to the domain */ 1157 - ret = arm_smmu_domain_add_master(smmu_domain, cfg, fwspec); 1158 - 1159 - /* 1160 - * Setup an autosuspend delay to avoid bouncing runpm state. 1161 - * Otherwise, if a driver for a suspended consumer device 1162 - * unmaps buffers, it will runpm resume/suspend for each one. 1163 - * 1164 - * For example, when used by a GPU device, when an application 1165 - * or game exits, it can trigger unmapping 100s or 1000s of 1166 - * buffers. With a runpm cycle for each buffer, that adds up 1167 - * to 5-10sec worth of reprogramming the context bank, while 1168 - * the system appears to be locked up to the user. 1169 - */ 1170 - pm_runtime_set_autosuspend_delay(smmu->dev, 20); 1171 - pm_runtime_use_autosuspend(smmu->dev); 1172 - 1156 + arm_smmu_master_install_s2crs(cfg, S2CR_TYPE_TRANS, 1157 + smmu_domain->cfg.cbndx, fwspec); 1158 + arm_smmu_rpm_use_autosuspend(smmu); 1173 1159 rpm_put: 1174 1160 arm_smmu_rpm_put(smmu); 1175 1161 return ret; 1176 1162 } 1163 + 1164 + static int arm_smmu_attach_dev_type(struct device *dev, 1165 + enum arm_smmu_s2cr_type type) 1166 + { 1167 + struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev); 1168 + struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 1169 + struct arm_smmu_device *smmu; 1170 + int ret; 1171 + 1172 + if (!cfg) 1173 + return -ENODEV; 1174 + smmu = cfg->smmu; 1175 + 1176 + ret = arm_smmu_rpm_get(smmu); 1177 + if (ret < 0) 1178 + return ret; 1179 + 1180 + arm_smmu_master_install_s2crs(cfg, type, 0, fwspec); 1181 + arm_smmu_rpm_use_autosuspend(smmu); 1182 + arm_smmu_rpm_put(smmu); 1183 + return 0; 1184 + } 1185 + 1186 + static int arm_smmu_attach_dev_identity(struct iommu_domain *domain, 1187 + struct device *dev) 1188 + { 1189 + return arm_smmu_attach_dev_type(dev, S2CR_TYPE_BYPASS); 1190 + } 1191 + 1192 + static const struct iommu_domain_ops arm_smmu_identity_ops = { 1193 + .attach_dev = arm_smmu_attach_dev_identity, 1194 + }; 1195 + 1196 + static struct iommu_domain arm_smmu_identity_domain = { 1197 + .type = IOMMU_DOMAIN_IDENTITY, 1198 + .ops = &arm_smmu_identity_ops, 1199 + }; 1200 + 1201 + static int arm_smmu_attach_dev_blocked(struct iommu_domain *domain, 1202 + struct device *dev) 1203 + { 1204 + return arm_smmu_attach_dev_type(dev, S2CR_TYPE_FAULT); 1205 + } 1206 + 1207 + static const struct iommu_domain_ops arm_smmu_blocked_ops = { 1208 + .attach_dev = arm_smmu_attach_dev_blocked, 1209 + }; 1210 + 1211 + static struct iommu_domain arm_smmu_blocked_domain = { 1212 + .type = IOMMU_DOMAIN_BLOCKED, 1213 + .ops = &arm_smmu_blocked_ops, 1214 + }; 1177 1215 1178 1216 static int arm_smmu_map_pages(struct iommu_domain *domain, unsigned long iova, 1179 1217 phys_addr_t paddr, size_t pgsize, size_t pgcount, ··· 1397 1357 fwspec = dev_iommu_fwspec_get(dev); 1398 1358 if (ret) 1399 1359 goto out_free; 1400 - } else if (fwspec && fwspec->ops == &arm_smmu_ops) { 1401 - smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode); 1402 1360 } else { 1403 - return ERR_PTR(-ENODEV); 1361 + smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode); 1404 1362 } 1405 1363 1406 1364 ret = -EINVAL; ··· 1465 1427 1466 1428 arm_smmu_rpm_put(cfg->smmu); 1467 1429 1468 - dev_iommu_priv_set(dev, NULL); 1469 1430 kfree(cfg); 1470 1431 } 1471 1432 ··· 1597 1560 } 1598 1561 1599 1562 static struct iommu_ops arm_smmu_ops = { 1563 + .identity_domain = &arm_smmu_identity_domain, 1564 + .blocked_domain = &arm_smmu_blocked_domain, 1600 1565 .capable = arm_smmu_capable, 1601 - .domain_alloc = arm_smmu_domain_alloc, 1566 + .domain_alloc_paging = arm_smmu_domain_alloc_paging, 1602 1567 .probe_device = arm_smmu_probe_device, 1603 1568 .release_device = arm_smmu_release_device, 1604 1569 .probe_finalize = arm_smmu_probe_finalize, ··· 2200 2161 return err; 2201 2162 } 2202 2163 2203 - err = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev); 2164 + err = iommu_device_register(&smmu->iommu, &arm_smmu_ops, 2165 + using_legacy_binding ? NULL : dev); 2204 2166 if (err) { 2205 2167 dev_err(dev, "Failed to register iommu\n"); 2206 2168 iommu_device_sysfs_remove(&smmu->iommu);
-1
drivers/iommu/arm/arm-smmu/arm-smmu.h
··· 361 361 ARM_SMMU_DOMAIN_S1 = 0, 362 362 ARM_SMMU_DOMAIN_S2, 363 363 ARM_SMMU_DOMAIN_NESTED, 364 - ARM_SMMU_DOMAIN_BYPASS, 365 364 }; 366 365 367 366 struct arm_smmu_domain {
+12 -14
drivers/iommu/arm/arm-smmu/qcom_iommu.c
··· 79 79 80 80 static const struct iommu_ops qcom_iommu_ops; 81 81 82 - static struct qcom_iommu_dev * to_iommu(struct device *dev) 83 - { 84 - struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 85 - 86 - if (!fwspec || fwspec->ops != &qcom_iommu_ops) 87 - return NULL; 88 - 89 - return dev_iommu_priv_get(dev); 90 - } 91 - 92 82 static struct qcom_iommu_ctx * to_ctx(struct qcom_iommu_domain *d, unsigned asid) 93 83 { 94 84 struct qcom_iommu_dev *qcom_iommu = d->iommu; ··· 362 372 363 373 static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct device *dev) 364 374 { 365 - struct qcom_iommu_dev *qcom_iommu = to_iommu(dev); 375 + struct qcom_iommu_dev *qcom_iommu = dev_iommu_priv_get(dev); 366 376 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain); 367 377 int ret; 368 378 ··· 394 404 struct iommu_domain *domain = iommu_get_domain_for_dev(dev); 395 405 struct qcom_iommu_domain *qcom_domain; 396 406 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 397 - struct qcom_iommu_dev *qcom_iommu = to_iommu(dev); 407 + struct qcom_iommu_dev *qcom_iommu = dev_iommu_priv_get(dev); 398 408 unsigned int i; 399 409 400 410 if (domain == identity_domain || !domain) ··· 525 535 526 536 static struct iommu_device *qcom_iommu_probe_device(struct device *dev) 527 537 { 528 - struct qcom_iommu_dev *qcom_iommu = to_iommu(dev); 538 + struct qcom_iommu_dev *qcom_iommu = dev_iommu_priv_get(dev); 529 539 struct device_link *link; 530 540 531 541 if (!qcom_iommu) ··· 890 900 static int __maybe_unused qcom_iommu_resume(struct device *dev) 891 901 { 892 902 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev); 903 + int ret; 893 904 894 - return clk_bulk_prepare_enable(CLK_NUM, qcom_iommu->clks); 905 + ret = clk_bulk_prepare_enable(CLK_NUM, qcom_iommu->clks); 906 + if (ret < 0) 907 + return ret; 908 + 909 + if (dev->pm_domain) 910 + return qcom_scm_restore_sec_cfg(qcom_iommu->sec_id, 0); 911 + 912 + return ret; 895 913 } 896 914 897 915 static int __maybe_unused qcom_iommu_suspend(struct device *dev)
+3
drivers/iommu/dma-iommu.c
··· 29 29 #include <linux/spinlock.h> 30 30 #include <linux/swiotlb.h> 31 31 #include <linux/vmalloc.h> 32 + #include <trace/events/swiotlb.h> 32 33 33 34 #include "dma-iommu.h" 34 35 ··· 1156 1155 dev_warn_once(dev, "DMA bounce buffers are inactive, unable to map unaligned transaction.\n"); 1157 1156 return DMA_MAPPING_ERROR; 1158 1157 } 1158 + 1159 + trace_swiotlb_bounced(dev, phys, size); 1159 1160 1160 1161 aligned_size = iova_align(iovad, size); 1161 1162 phys = swiotlb_tbl_map_single(dev, phys, size, aligned_size,
-3
drivers/iommu/intel/debugfs.c
··· 106 106 IOMMU_REGSET_ENTRY(MTRR_PHYSMASK8), 107 107 IOMMU_REGSET_ENTRY(MTRR_PHYSBASE9), 108 108 IOMMU_REGSET_ENTRY(MTRR_PHYSMASK9), 109 - IOMMU_REGSET_ENTRY(VCCAP), 110 - IOMMU_REGSET_ENTRY(VCMD), 111 - IOMMU_REGSET_ENTRY(VCRSP), 112 109 }; 113 110 114 111 static struct dentry *intel_iommu_debug;
+24 -217
drivers/iommu/intel/iommu.c
··· 46 46 47 47 #define DEFAULT_DOMAIN_ADDRESS_WIDTH 57 48 48 49 - #define MAX_AGAW_WIDTH 64 50 - #define MAX_AGAW_PFN_WIDTH (MAX_AGAW_WIDTH - VTD_PAGE_SHIFT) 51 - 52 49 #define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << ((gaw) - VTD_PAGE_SHIFT)) - 1) 53 50 #define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << (gaw)) - 1) 54 51 ··· 59 62 #define IOVA_START_PFN (1) 60 63 61 64 #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT) 62 - 63 - /* page table handling */ 64 - #define LEVEL_STRIDE (9) 65 - #define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1) 66 - 67 - static inline int agaw_to_level(int agaw) 68 - { 69 - return agaw + 2; 70 - } 71 - 72 - static inline int agaw_to_width(int agaw) 73 - { 74 - return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH); 75 - } 76 - 77 - static inline int width_to_agaw(int width) 78 - { 79 - return DIV_ROUND_UP(width - 30, LEVEL_STRIDE); 80 - } 81 - 82 - static inline unsigned int level_to_offset_bits(int level) 83 - { 84 - return (level - 1) * LEVEL_STRIDE; 85 - } 86 - 87 - static inline int pfn_level_offset(u64 pfn, int level) 88 - { 89 - return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK; 90 - } 91 - 92 - static inline u64 level_mask(int level) 93 - { 94 - return -1ULL << level_to_offset_bits(level); 95 - } 96 - 97 - static inline u64 level_size(int level) 98 - { 99 - return 1ULL << level_to_offset_bits(level); 100 - } 101 - 102 - static inline u64 align_to_level(u64 pfn, int level) 103 - { 104 - return (pfn + level_size(level) - 1) & level_mask(level); 105 - } 106 - 107 - static inline unsigned long lvl_to_nr_pages(unsigned int lvl) 108 - { 109 - return 1UL << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH); 110 - } 111 - 112 - /* VT-d pages must always be _smaller_ than MM pages. Otherwise things 113 - are never going to work. */ 114 - static inline unsigned long mm_to_dma_pfn_start(unsigned long mm_pfn) 115 - { 116 - return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT); 117 - } 118 - static inline unsigned long mm_to_dma_pfn_end(unsigned long mm_pfn) 119 - { 120 - return ((mm_pfn + 1) << (PAGE_SHIFT - VTD_PAGE_SHIFT)) - 1; 121 - } 122 - static inline unsigned long page_to_dma_pfn(struct page *pg) 123 - { 124 - return mm_to_dma_pfn_start(page_to_pfn(pg)); 125 - } 126 - static inline unsigned long virt_to_dma_pfn(void *p) 127 - { 128 - return page_to_dma_pfn(virt_to_page(p)); 129 - } 130 65 131 66 static void __init check_tylersburg_isoch(void); 132 67 static int rwbf_quirk; ··· 95 166 return 0; 96 167 97 168 return re->hi & VTD_PAGE_MASK; 98 - } 99 - 100 - static inline void context_set_present(struct context_entry *context) 101 - { 102 - context->lo |= 1; 103 - } 104 - 105 - static inline void context_set_fault_enable(struct context_entry *context) 106 - { 107 - context->lo &= (((u64)-1) << 2) | 1; 108 - } 109 - 110 - static inline void context_set_translation_type(struct context_entry *context, 111 - unsigned long value) 112 - { 113 - context->lo &= (((u64)-1) << 4) | 3; 114 - context->lo |= (value & 3) << 2; 115 - } 116 - 117 - static inline void context_set_address_root(struct context_entry *context, 118 - unsigned long value) 119 - { 120 - context->lo &= ~VTD_PAGE_MASK; 121 - context->lo |= value & VTD_PAGE_MASK; 122 - } 123 - 124 - static inline void context_set_address_width(struct context_entry *context, 125 - unsigned long value) 126 - { 127 - context->hi |= value & 7; 128 - } 129 - 130 - static inline void context_set_domain_id(struct context_entry *context, 131 - unsigned long value) 132 - { 133 - context->hi |= (value & ((1 << 16) - 1)) << 8; 134 - } 135 - 136 - static inline void context_set_pasid(struct context_entry *context) 137 - { 138 - context->lo |= CONTEXT_PASIDE; 139 - } 140 - 141 - static inline int context_domain_id(struct context_entry *c) 142 - { 143 - return((c->hi >> 8) & 0xffff); 144 - } 145 - 146 - static inline void context_clear_entry(struct context_entry *context) 147 - { 148 - context->lo = 0; 149 - context->hi = 0; 150 - } 151 - 152 - static inline bool context_copied(struct intel_iommu *iommu, u8 bus, u8 devfn) 153 - { 154 - if (!iommu->copied_tables) 155 - return false; 156 - 157 - return test_bit(((long)bus << 8) | devfn, iommu->copied_tables); 158 - } 159 - 160 - static inline void 161 - set_context_copied(struct intel_iommu *iommu, u8 bus, u8 devfn) 162 - { 163 - set_bit(((long)bus << 8) | devfn, iommu->copied_tables); 164 - } 165 - 166 - static inline void 167 - clear_context_copied(struct intel_iommu *iommu, u8 bus, u8 devfn) 168 - { 169 - clear_bit(((long)bus << 8) | devfn, iommu->copied_tables); 170 169 } 171 170 172 171 /* ··· 240 383 free_page((unsigned long)vaddr); 241 384 } 242 385 243 - static inline int domain_type_is_si(struct dmar_domain *domain) 386 + static int domain_type_is_si(struct dmar_domain *domain) 244 387 { 245 388 return domain->domain.type == IOMMU_DOMAIN_IDENTITY; 246 389 } 247 390 248 - static inline int domain_pfn_supported(struct dmar_domain *domain, 249 - unsigned long pfn) 391 + static int domain_pfn_supported(struct dmar_domain *domain, unsigned long pfn) 250 392 { 251 393 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT; 252 394 ··· 307 451 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH); 308 452 } 309 453 310 - static inline bool iommu_paging_structure_coherency(struct intel_iommu *iommu) 454 + static bool iommu_paging_structure_coherency(struct intel_iommu *iommu) 311 455 { 312 456 return sm_supported(iommu) ? 313 457 ecap_smpwc(iommu->ecap) : ecap_coherent(iommu->ecap); ··· 559 703 return false; 560 704 } 561 705 562 - struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn) 706 + static struct intel_iommu *device_lookup_iommu(struct device *dev, u8 *bus, u8 *devfn) 563 707 { 564 708 struct dmar_drhd_unit *drhd = NULL; 565 709 struct pci_dev *pdev = NULL; ··· 1430 1574 } 1431 1575 1432 1576 /* Notification for newly created mappings */ 1433 - static inline void __mapping_notify_one(struct intel_iommu *iommu, 1434 - struct dmar_domain *domain, 1435 - unsigned long pfn, unsigned int pages) 1577 + static void __mapping_notify_one(struct intel_iommu *iommu, struct dmar_domain *domain, 1578 + unsigned long pfn, unsigned int pages) 1436 1579 { 1437 1580 /* 1438 1581 * It's a non-present to present mapping. Only flush if caching mode ··· 1698 1843 spin_unlock(&iommu->lock); 1699 1844 } 1700 1845 1701 - static inline int guestwidth_to_adjustwidth(int gaw) 1846 + static int guestwidth_to_adjustwidth(int gaw) 1702 1847 { 1703 1848 int agaw; 1704 1849 int r = (gaw - 12) % 9; ··· 1732 1877 * Value of X in the PDTS field of a scalable mode context entry 1733 1878 * indicates PASID directory with 2^(X + 7) entries. 1734 1879 */ 1735 - static inline unsigned long context_get_sm_pds(struct pasid_table *table) 1880 + static unsigned long context_get_sm_pds(struct pasid_table *table) 1736 1881 { 1737 1882 unsigned long pds, max_pde; 1738 1883 ··· 1743 1888 1744 1889 return pds - 7; 1745 1890 } 1746 - 1747 - /* 1748 - * Set the RID_PASID field of a scalable mode context entry. The 1749 - * IOMMU hardware will use the PASID value set in this field for 1750 - * DMA translations of DMA requests without PASID. 1751 - */ 1752 - static inline void 1753 - context_set_sm_rid2pasid(struct context_entry *context, unsigned long pasid) 1754 - { 1755 - context->hi |= pasid & ((1 << 20) - 1); 1756 - } 1757 - 1758 - /* 1759 - * Set the DTE(Device-TLB Enable) field of a scalable mode context 1760 - * entry. 1761 - */ 1762 - static inline void context_set_sm_dte(struct context_entry *context) 1763 - { 1764 - context->lo |= BIT_ULL(2); 1765 - } 1766 - 1767 - /* 1768 - * Set the PRE(Page Request Enable) field of a scalable mode context 1769 - * entry. 1770 - */ 1771 - static inline void context_set_sm_pre(struct context_entry *context) 1772 - { 1773 - context->lo |= BIT_ULL(4); 1774 - } 1775 - 1776 - /* Convert value to context PASID directory size field coding. */ 1777 - #define context_pdts(pds) (((pds) & 0x7) << 9) 1778 1891 1779 1892 static int domain_context_mapping_one(struct dmar_domain *domain, 1780 1893 struct intel_iommu *iommu, ··· 1904 2081 static int 1905 2082 domain_context_mapping(struct dmar_domain *domain, struct device *dev) 1906 2083 { 2084 + struct device_domain_info *info = dev_iommu_priv_get(dev); 1907 2085 struct domain_context_mapping_data data; 2086 + struct intel_iommu *iommu = info->iommu; 2087 + u8 bus = info->bus, devfn = info->devfn; 1908 2088 struct pasid_table *table; 1909 - struct intel_iommu *iommu; 1910 - u8 bus, devfn; 1911 - 1912 - iommu = device_to_iommu(dev, &bus, &devfn); 1913 - if (!iommu) 1914 - return -ENODEV; 1915 2089 1916 2090 table = intel_pasid_get_table(dev); 1917 2091 ··· 1925 2105 } 1926 2106 1927 2107 /* Returns a number of VTD pages, but aligned to MM page size */ 1928 - static inline unsigned long aligned_nrpages(unsigned long host_addr, 1929 - size_t size) 2108 + static unsigned long aligned_nrpages(unsigned long host_addr, size_t size) 1930 2109 { 1931 2110 host_addr &= ~PAGE_MASK; 1932 2111 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT; 1933 2112 } 1934 2113 1935 2114 /* Return largest possible superpage level for a given mapping */ 1936 - static inline int hardware_largepage_caps(struct dmar_domain *domain, 1937 - unsigned long iov_pfn, 1938 - unsigned long phy_pfn, 1939 - unsigned long pages) 2115 + static int hardware_largepage_caps(struct dmar_domain *domain, unsigned long iov_pfn, 2116 + unsigned long phy_pfn, unsigned long pages) 1940 2117 { 1941 2118 int support, level = 1; 1942 2119 unsigned long pfnmerge; ··· 2266 2449 struct device *dev) 2267 2450 { 2268 2451 struct device_domain_info *info = dev_iommu_priv_get(dev); 2269 - struct intel_iommu *iommu; 2452 + struct intel_iommu *iommu = info->iommu; 2270 2453 unsigned long flags; 2271 - u8 bus, devfn; 2272 2454 int ret; 2273 - 2274 - iommu = device_to_iommu(dev, &bus, &devfn); 2275 - if (!iommu) 2276 - return -ENODEV; 2277 2455 2278 2456 ret = domain_attach_iommu(domain, iommu); 2279 2457 if (ret) ··· 2282 2470 if (sm_supported(iommu) && !dev_is_real_dma_subdevice(dev)) { 2283 2471 /* Setup the PASID entry for requests without PASID: */ 2284 2472 if (hw_pass_through && domain_type_is_si(domain)) 2285 - ret = intel_pasid_setup_pass_through(iommu, domain, 2473 + ret = intel_pasid_setup_pass_through(iommu, 2286 2474 dev, IOMMU_NO_PASID); 2287 2475 else if (domain->use_first_level) 2288 2476 ret = domain_setup_first_level(iommu, domain, dev, ··· 3427 3615 up_write(&dmar_global_lock); 3428 3616 } 3429 3617 3430 - static inline struct intel_iommu *dev_to_intel_iommu(struct device *dev) 3618 + static struct intel_iommu *dev_to_intel_iommu(struct device *dev) 3431 3619 { 3432 3620 struct iommu_device *iommu_dev = dev_to_iommu_device(dev); 3433 3621 ··· 3506 3694 NULL, 3507 3695 }; 3508 3696 3509 - static inline bool has_external_pci(void) 3697 + static bool has_external_pci(void) 3510 3698 { 3511 3699 struct pci_dev *pdev = NULL; 3512 3700 ··· 3931 4119 int prepare_domain_attach_device(struct iommu_domain *domain, 3932 4120 struct device *dev) 3933 4121 { 4122 + struct device_domain_info *info = dev_iommu_priv_get(dev); 3934 4123 struct dmar_domain *dmar_domain = to_dmar_domain(domain); 3935 - struct intel_iommu *iommu; 4124 + struct intel_iommu *iommu = info->iommu; 3936 4125 int addr_width; 3937 - 3938 - iommu = device_to_iommu(dev, NULL, NULL); 3939 - if (!iommu) 3940 - return -ENODEV; 3941 4126 3942 4127 if (dmar_domain->force_snooping && !ecap_sc_support(iommu->ecap)) 3943 4128 return -EINVAL; ··· 4212 4403 u8 bus, devfn; 4213 4404 int ret; 4214 4405 4215 - iommu = device_to_iommu(dev, &bus, &devfn); 4406 + iommu = device_lookup_iommu(dev, &bus, &devfn); 4216 4407 if (!iommu || !iommu->iommu.ops) 4217 4408 return ERR_PTR(-ENODEV); 4218 4409 ··· 4270 4461 ret = intel_pasid_alloc_table(dev); 4271 4462 if (ret) { 4272 4463 dev_err(dev, "PASID table allocation failed\n"); 4273 - dev_iommu_priv_set(dev, NULL); 4274 4464 kfree(info); 4275 4465 return ERR_PTR(ret); 4276 4466 } ··· 4287 4479 dmar_remove_one_dev_info(dev); 4288 4480 intel_pasid_free_table(dev); 4289 4481 intel_iommu_debugfs_remove_dev(info); 4290 - dev_iommu_priv_set(dev, NULL); 4291 4482 kfree(info); 4292 4483 set_dma_ops(dev, NULL); 4293 4484 } ··· 4546 4739 4547 4740 static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid) 4548 4741 { 4549 - struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL); 4742 + struct device_domain_info *info = dev_iommu_priv_get(dev); 4550 4743 struct dev_pasid_info *curr, *dev_pasid = NULL; 4744 + struct intel_iommu *iommu = info->iommu; 4551 4745 struct dmar_domain *dmar_domain; 4552 4746 struct iommu_domain *domain; 4553 4747 unsigned long flags; ··· 4619 4811 goto out_free; 4620 4812 4621 4813 if (domain_type_is_si(dmar_domain)) 4622 - ret = intel_pasid_setup_pass_through(iommu, dmar_domain, 4623 - dev, pasid); 4814 + ret = intel_pasid_setup_pass_through(iommu, dev, pasid); 4624 4815 else if (dmar_domain->use_first_level) 4625 4816 ret = domain_setup_first_level(iommu, dmar_domain, 4626 4817 dev, pasid);
+175 -4
drivers/iommu/intel/iommu.h
··· 140 140 #define DMAR_ECEO_REG 0x408 141 141 #define DMAR_ECRSP_REG 0x410 142 142 #define DMAR_ECCAP_REG 0x430 143 - #define DMAR_VCCAP_REG 0xe30 /* Virtual command capability register */ 144 - #define DMAR_VCMD_REG 0xe00 /* Virtual command register */ 145 - #define DMAR_VCRSP_REG 0xe10 /* Virtual command response register */ 146 143 147 144 #define DMAR_IQER_REG_IQEI(reg) FIELD_GET(GENMASK_ULL(3, 0), reg) 148 145 #define DMAR_IQER_REG_ITESID(reg) FIELD_GET(GENMASK_ULL(47, 32), reg) ··· 851 854 return (context->lo & 1); 852 855 } 853 856 857 + #define LEVEL_STRIDE (9) 858 + #define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1) 859 + #define MAX_AGAW_WIDTH (64) 860 + #define MAX_AGAW_PFN_WIDTH (MAX_AGAW_WIDTH - VTD_PAGE_SHIFT) 861 + 862 + static inline int agaw_to_level(int agaw) 863 + { 864 + return agaw + 2; 865 + } 866 + 867 + static inline int agaw_to_width(int agaw) 868 + { 869 + return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH); 870 + } 871 + 872 + static inline int width_to_agaw(int width) 873 + { 874 + return DIV_ROUND_UP(width - 30, LEVEL_STRIDE); 875 + } 876 + 877 + static inline unsigned int level_to_offset_bits(int level) 878 + { 879 + return (level - 1) * LEVEL_STRIDE; 880 + } 881 + 882 + static inline int pfn_level_offset(u64 pfn, int level) 883 + { 884 + return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK; 885 + } 886 + 887 + static inline u64 level_mask(int level) 888 + { 889 + return -1ULL << level_to_offset_bits(level); 890 + } 891 + 892 + static inline u64 level_size(int level) 893 + { 894 + return 1ULL << level_to_offset_bits(level); 895 + } 896 + 897 + static inline u64 align_to_level(u64 pfn, int level) 898 + { 899 + return (pfn + level_size(level) - 1) & level_mask(level); 900 + } 901 + 902 + static inline unsigned long lvl_to_nr_pages(unsigned int lvl) 903 + { 904 + return 1UL << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH); 905 + } 906 + 907 + /* VT-d pages must always be _smaller_ than MM pages. Otherwise things 908 + are never going to work. */ 909 + static inline unsigned long mm_to_dma_pfn_start(unsigned long mm_pfn) 910 + { 911 + return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT); 912 + } 913 + static inline unsigned long mm_to_dma_pfn_end(unsigned long mm_pfn) 914 + { 915 + return ((mm_pfn + 1) << (PAGE_SHIFT - VTD_PAGE_SHIFT)) - 1; 916 + } 917 + static inline unsigned long page_to_dma_pfn(struct page *pg) 918 + { 919 + return mm_to_dma_pfn_start(page_to_pfn(pg)); 920 + } 921 + static inline unsigned long virt_to_dma_pfn(void *p) 922 + { 923 + return page_to_dma_pfn(virt_to_page(p)); 924 + } 925 + 926 + static inline void context_set_present(struct context_entry *context) 927 + { 928 + context->lo |= 1; 929 + } 930 + 931 + static inline void context_set_fault_enable(struct context_entry *context) 932 + { 933 + context->lo &= (((u64)-1) << 2) | 1; 934 + } 935 + 936 + static inline void context_set_translation_type(struct context_entry *context, 937 + unsigned long value) 938 + { 939 + context->lo &= (((u64)-1) << 4) | 3; 940 + context->lo |= (value & 3) << 2; 941 + } 942 + 943 + static inline void context_set_address_root(struct context_entry *context, 944 + unsigned long value) 945 + { 946 + context->lo &= ~VTD_PAGE_MASK; 947 + context->lo |= value & VTD_PAGE_MASK; 948 + } 949 + 950 + static inline void context_set_address_width(struct context_entry *context, 951 + unsigned long value) 952 + { 953 + context->hi |= value & 7; 954 + } 955 + 956 + static inline void context_set_domain_id(struct context_entry *context, 957 + unsigned long value) 958 + { 959 + context->hi |= (value & ((1 << 16) - 1)) << 8; 960 + } 961 + 962 + static inline void context_set_pasid(struct context_entry *context) 963 + { 964 + context->lo |= CONTEXT_PASIDE; 965 + } 966 + 967 + static inline int context_domain_id(struct context_entry *c) 968 + { 969 + return((c->hi >> 8) & 0xffff); 970 + } 971 + 972 + static inline void context_clear_entry(struct context_entry *context) 973 + { 974 + context->lo = 0; 975 + context->hi = 0; 976 + } 977 + 978 + #ifdef CONFIG_INTEL_IOMMU 979 + static inline bool context_copied(struct intel_iommu *iommu, u8 bus, u8 devfn) 980 + { 981 + if (!iommu->copied_tables) 982 + return false; 983 + 984 + return test_bit(((long)bus << 8) | devfn, iommu->copied_tables); 985 + } 986 + 987 + static inline void 988 + set_context_copied(struct intel_iommu *iommu, u8 bus, u8 devfn) 989 + { 990 + set_bit(((long)bus << 8) | devfn, iommu->copied_tables); 991 + } 992 + 993 + static inline void 994 + clear_context_copied(struct intel_iommu *iommu, u8 bus, u8 devfn) 995 + { 996 + clear_bit(((long)bus << 8) | devfn, iommu->copied_tables); 997 + } 998 + #endif /* CONFIG_INTEL_IOMMU */ 999 + 1000 + /* 1001 + * Set the RID_PASID field of a scalable mode context entry. The 1002 + * IOMMU hardware will use the PASID value set in this field for 1003 + * DMA translations of DMA requests without PASID. 1004 + */ 1005 + static inline void 1006 + context_set_sm_rid2pasid(struct context_entry *context, unsigned long pasid) 1007 + { 1008 + context->hi |= pasid & ((1 << 20) - 1); 1009 + } 1010 + 1011 + /* 1012 + * Set the DTE(Device-TLB Enable) field of a scalable mode context 1013 + * entry. 1014 + */ 1015 + static inline void context_set_sm_dte(struct context_entry *context) 1016 + { 1017 + context->lo |= BIT_ULL(2); 1018 + } 1019 + 1020 + /* 1021 + * Set the PRE(Page Request Enable) field of a scalable mode context 1022 + * entry. 1023 + */ 1024 + static inline void context_set_sm_pre(struct context_entry *context) 1025 + { 1026 + context->lo |= BIT_ULL(4); 1027 + } 1028 + 1029 + /* Convert value to context PASID directory size field coding. */ 1030 + #define context_pdts(pds) (((pds) & 0x7) << 9) 1031 + 854 1032 struct dmar_drhd_unit *dmar_find_matched_drhd_unit(struct pci_dev *dev); 855 1033 856 1034 int dmar_enable_qi(struct intel_iommu *iommu); ··· 1072 900 void *alloc_pgtable_page(int node, gfp_t gfp); 1073 901 void free_pgtable_page(void *vaddr); 1074 902 void iommu_flush_write_buffer(struct intel_iommu *iommu); 1075 - struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn); 1076 903 struct iommu_domain *intel_nested_domain_alloc(struct iommu_domain *parent, 1077 904 const struct iommu_user_data *user_data); 1078 905
+3 -271
drivers/iommu/intel/pasid.c
··· 26 26 */ 27 27 u32 intel_pasid_max_id = PASID_MAX; 28 28 29 - int vcmd_alloc_pasid(struct intel_iommu *iommu, u32 *pasid) 30 - { 31 - unsigned long flags; 32 - u8 status_code; 33 - int ret = 0; 34 - u64 res; 35 - 36 - raw_spin_lock_irqsave(&iommu->register_lock, flags); 37 - dmar_writeq(iommu->reg + DMAR_VCMD_REG, VCMD_CMD_ALLOC); 38 - IOMMU_WAIT_OP(iommu, DMAR_VCRSP_REG, dmar_readq, 39 - !(res & VCMD_VRSP_IP), res); 40 - raw_spin_unlock_irqrestore(&iommu->register_lock, flags); 41 - 42 - status_code = VCMD_VRSP_SC(res); 43 - switch (status_code) { 44 - case VCMD_VRSP_SC_SUCCESS: 45 - *pasid = VCMD_VRSP_RESULT_PASID(res); 46 - break; 47 - case VCMD_VRSP_SC_NO_PASID_AVAIL: 48 - pr_info("IOMMU: %s: No PASID available\n", iommu->name); 49 - ret = -ENOSPC; 50 - break; 51 - default: 52 - ret = -ENODEV; 53 - pr_warn("IOMMU: %s: Unexpected error code %d\n", 54 - iommu->name, status_code); 55 - } 56 - 57 - return ret; 58 - } 59 - 60 - void vcmd_free_pasid(struct intel_iommu *iommu, u32 pasid) 61 - { 62 - unsigned long flags; 63 - u8 status_code; 64 - u64 res; 65 - 66 - raw_spin_lock_irqsave(&iommu->register_lock, flags); 67 - dmar_writeq(iommu->reg + DMAR_VCMD_REG, 68 - VCMD_CMD_OPERAND(pasid) | VCMD_CMD_FREE); 69 - IOMMU_WAIT_OP(iommu, DMAR_VCRSP_REG, dmar_readq, 70 - !(res & VCMD_VRSP_IP), res); 71 - raw_spin_unlock_irqrestore(&iommu->register_lock, flags); 72 - 73 - status_code = VCMD_VRSP_SC(res); 74 - switch (status_code) { 75 - case VCMD_VRSP_SC_SUCCESS: 76 - break; 77 - case VCMD_VRSP_SC_INVALID_PASID: 78 - pr_info("IOMMU: %s: Invalid PASID\n", iommu->name); 79 - break; 80 - default: 81 - pr_warn("IOMMU: %s: Unexpected error code %d\n", 82 - iommu->name, status_code); 83 - } 84 - } 85 - 86 29 /* 87 30 * Per device pasid table management: 88 31 */ ··· 173 230 /* 174 231 * Interfaces for PASID table entry manipulation: 175 232 */ 176 - static inline void pasid_clear_entry(struct pasid_entry *pe) 177 - { 178 - WRITE_ONCE(pe->val[0], 0); 179 - WRITE_ONCE(pe->val[1], 0); 180 - WRITE_ONCE(pe->val[2], 0); 181 - WRITE_ONCE(pe->val[3], 0); 182 - WRITE_ONCE(pe->val[4], 0); 183 - WRITE_ONCE(pe->val[5], 0); 184 - WRITE_ONCE(pe->val[6], 0); 185 - WRITE_ONCE(pe->val[7], 0); 186 - } 187 - 188 - static inline void pasid_clear_entry_with_fpd(struct pasid_entry *pe) 189 - { 190 - WRITE_ONCE(pe->val[0], PASID_PTE_FPD); 191 - WRITE_ONCE(pe->val[1], 0); 192 - WRITE_ONCE(pe->val[2], 0); 193 - WRITE_ONCE(pe->val[3], 0); 194 - WRITE_ONCE(pe->val[4], 0); 195 - WRITE_ONCE(pe->val[5], 0); 196 - WRITE_ONCE(pe->val[6], 0); 197 - WRITE_ONCE(pe->val[7], 0); 198 - } 199 - 200 233 static void 201 234 intel_pasid_clear_entry(struct device *dev, u32 pasid, bool fault_ignore) 202 235 { ··· 186 267 pasid_clear_entry_with_fpd(pe); 187 268 else 188 269 pasid_clear_entry(pe); 189 - } 190 - 191 - static inline void pasid_set_bits(u64 *ptr, u64 mask, u64 bits) 192 - { 193 - u64 old; 194 - 195 - old = READ_ONCE(*ptr); 196 - WRITE_ONCE(*ptr, (old & ~mask) | bits); 197 - } 198 - 199 - static inline u64 pasid_get_bits(u64 *ptr) 200 - { 201 - return READ_ONCE(*ptr); 202 - } 203 - 204 - /* 205 - * Setup the DID(Domain Identifier) field (Bit 64~79) of scalable mode 206 - * PASID entry. 207 - */ 208 - static inline void 209 - pasid_set_domain_id(struct pasid_entry *pe, u64 value) 210 - { 211 - pasid_set_bits(&pe->val[1], GENMASK_ULL(15, 0), value); 212 - } 213 - 214 - /* 215 - * Get domain ID value of a scalable mode PASID entry. 216 - */ 217 - static inline u16 218 - pasid_get_domain_id(struct pasid_entry *pe) 219 - { 220 - return (u16)(READ_ONCE(pe->val[1]) & GENMASK_ULL(15, 0)); 221 - } 222 - 223 - /* 224 - * Setup the SLPTPTR(Second Level Page Table Pointer) field (Bit 12~63) 225 - * of a scalable mode PASID entry. 226 - */ 227 - static inline void 228 - pasid_set_slptr(struct pasid_entry *pe, u64 value) 229 - { 230 - pasid_set_bits(&pe->val[0], VTD_PAGE_MASK, value); 231 - } 232 - 233 - /* 234 - * Setup the AW(Address Width) field (Bit 2~4) of a scalable mode PASID 235 - * entry. 236 - */ 237 - static inline void 238 - pasid_set_address_width(struct pasid_entry *pe, u64 value) 239 - { 240 - pasid_set_bits(&pe->val[0], GENMASK_ULL(4, 2), value << 2); 241 - } 242 - 243 - /* 244 - * Setup the PGTT(PASID Granular Translation Type) field (Bit 6~8) 245 - * of a scalable mode PASID entry. 246 - */ 247 - static inline void 248 - pasid_set_translation_type(struct pasid_entry *pe, u64 value) 249 - { 250 - pasid_set_bits(&pe->val[0], GENMASK_ULL(8, 6), value << 6); 251 - } 252 - 253 - /* 254 - * Enable fault processing by clearing the FPD(Fault Processing 255 - * Disable) field (Bit 1) of a scalable mode PASID entry. 256 - */ 257 - static inline void pasid_set_fault_enable(struct pasid_entry *pe) 258 - { 259 - pasid_set_bits(&pe->val[0], 1 << 1, 0); 260 - } 261 - 262 - /* 263 - * Enable second level A/D bits by setting the SLADE (Second Level 264 - * Access Dirty Enable) field (Bit 9) of a scalable mode PASID 265 - * entry. 266 - */ 267 - static inline void pasid_set_ssade(struct pasid_entry *pe) 268 - { 269 - pasid_set_bits(&pe->val[0], 1 << 9, 1 << 9); 270 - } 271 - 272 - /* 273 - * Disable second level A/D bits by clearing the SLADE (Second Level 274 - * Access Dirty Enable) field (Bit 9) of a scalable mode PASID 275 - * entry. 276 - */ 277 - static inline void pasid_clear_ssade(struct pasid_entry *pe) 278 - { 279 - pasid_set_bits(&pe->val[0], 1 << 9, 0); 280 - } 281 - 282 - /* 283 - * Checks if second level A/D bits specifically the SLADE (Second Level 284 - * Access Dirty Enable) field (Bit 9) of a scalable mode PASID 285 - * entry is set. 286 - */ 287 - static inline bool pasid_get_ssade(struct pasid_entry *pe) 288 - { 289 - return pasid_get_bits(&pe->val[0]) & (1 << 9); 290 - } 291 - 292 - /* 293 - * Setup the SRE(Supervisor Request Enable) field (Bit 128) of a 294 - * scalable mode PASID entry. 295 - */ 296 - static inline void pasid_set_sre(struct pasid_entry *pe) 297 - { 298 - pasid_set_bits(&pe->val[2], 1 << 0, 1); 299 - } 300 - 301 - /* 302 - * Setup the WPE(Write Protect Enable) field (Bit 132) of a 303 - * scalable mode PASID entry. 304 - */ 305 - static inline void pasid_set_wpe(struct pasid_entry *pe) 306 - { 307 - pasid_set_bits(&pe->val[2], 1 << 4, 1 << 4); 308 - } 309 - 310 - /* 311 - * Setup the P(Present) field (Bit 0) of a scalable mode PASID 312 - * entry. 313 - */ 314 - static inline void pasid_set_present(struct pasid_entry *pe) 315 - { 316 - pasid_set_bits(&pe->val[0], 1 << 0, 1); 317 - } 318 - 319 - /* 320 - * Setup Page Walk Snoop bit (Bit 87) of a scalable mode PASID 321 - * entry. 322 - */ 323 - static inline void pasid_set_page_snoop(struct pasid_entry *pe, bool value) 324 - { 325 - pasid_set_bits(&pe->val[1], 1 << 23, value << 23); 326 - } 327 - 328 - /* 329 - * Setup No Execute Enable bit (Bit 133) of a scalable mode PASID 330 - * entry. It is required when XD bit of the first level page table 331 - * entry is about to be set. 332 - */ 333 - static inline void pasid_set_nxe(struct pasid_entry *pe) 334 - { 335 - pasid_set_bits(&pe->val[2], 1 << 5, 1 << 5); 336 - } 337 - 338 - /* 339 - * Setup the Page Snoop (PGSNP) field (Bit 88) of a scalable mode 340 - * PASID entry. 341 - */ 342 - static inline void 343 - pasid_set_pgsnp(struct pasid_entry *pe) 344 - { 345 - pasid_set_bits(&pe->val[1], 1ULL << 24, 1ULL << 24); 346 - } 347 - 348 - /* 349 - * Setup the First Level Page table Pointer field (Bit 140~191) 350 - * of a scalable mode PASID entry. 351 - */ 352 - static inline void 353 - pasid_set_flptr(struct pasid_entry *pe, u64 value) 354 - { 355 - pasid_set_bits(&pe->val[2], VTD_PAGE_MASK, value); 356 - } 357 - 358 - /* 359 - * Setup the First Level Paging Mode field (Bit 130~131) of a 360 - * scalable mode PASID entry. 361 - */ 362 - static inline void 363 - pasid_set_flpm(struct pasid_entry *pe, u64 value) 364 - { 365 - pasid_set_bits(&pe->val[2], GENMASK_ULL(3, 2), value << 2); 366 - } 367 - 368 - /* 369 - * Setup the Extended Access Flag Enable (EAFE) field (Bit 135) 370 - * of a scalable mode PASID entry. 371 - */ 372 - static inline void pasid_set_eafe(struct pasid_entry *pe) 373 - { 374 - pasid_set_bits(&pe->val[2], 1 << 7, 1 << 7); 375 270 } 376 271 377 272 static void ··· 346 613 * Skip top levels of page tables for iommu which has less agaw 347 614 * than default. Unnecessary for PT mode. 348 615 */ 349 - static inline int iommu_skip_agaw(struct dmar_domain *domain, 350 - struct intel_iommu *iommu, 351 - struct dma_pte **pgd) 616 + static int iommu_skip_agaw(struct dmar_domain *domain, 617 + struct intel_iommu *iommu, 618 + struct dma_pte **pgd) 352 619 { 353 620 int agaw; 354 621 ··· 500 767 * Set up the scalable mode pasid entry for passthrough translation type. 501 768 */ 502 769 int intel_pasid_setup_pass_through(struct intel_iommu *iommu, 503 - struct dmar_domain *domain, 504 770 struct device *dev, u32 pasid) 505 771 { 506 772 u16 did = FLPT_DEFAULT_DID;
+210 -13
drivers/iommu/intel/pasid.h
··· 22 22 #define is_pasid_enabled(entry) (((entry)->lo >> 3) & 0x1) 23 23 #define get_pasid_dir_size(entry) (1 << ((((entry)->lo >> 9) & 0x7) + 7)) 24 24 25 - /* Virtual command interface for enlightened pasid management. */ 26 - #define VCMD_CMD_ALLOC 0x1 27 - #define VCMD_CMD_FREE 0x2 28 - #define VCMD_VRSP_IP 0x1 29 - #define VCMD_VRSP_SC(e) (((e) & 0xff) >> 1) 30 - #define VCMD_VRSP_SC_SUCCESS 0 31 - #define VCMD_VRSP_SC_NO_PASID_AVAIL 16 32 - #define VCMD_VRSP_SC_INVALID_PASID 16 33 - #define VCMD_VRSP_RESULT_PASID(e) (((e) >> 16) & 0xfffff) 34 - #define VCMD_CMD_OPERAND(e) ((e) << 16) 35 25 /* 36 26 * Domain ID reserved for pasid entries programmed for first-level 37 27 * only and pass-through transfer modes. ··· 86 96 return (u16)((READ_ONCE(pte->val[0]) >> 6) & 0x7); 87 97 } 88 98 99 + static inline void pasid_clear_entry(struct pasid_entry *pe) 100 + { 101 + WRITE_ONCE(pe->val[0], 0); 102 + WRITE_ONCE(pe->val[1], 0); 103 + WRITE_ONCE(pe->val[2], 0); 104 + WRITE_ONCE(pe->val[3], 0); 105 + WRITE_ONCE(pe->val[4], 0); 106 + WRITE_ONCE(pe->val[5], 0); 107 + WRITE_ONCE(pe->val[6], 0); 108 + WRITE_ONCE(pe->val[7], 0); 109 + } 110 + 111 + static inline void pasid_clear_entry_with_fpd(struct pasid_entry *pe) 112 + { 113 + WRITE_ONCE(pe->val[0], PASID_PTE_FPD); 114 + WRITE_ONCE(pe->val[1], 0); 115 + WRITE_ONCE(pe->val[2], 0); 116 + WRITE_ONCE(pe->val[3], 0); 117 + WRITE_ONCE(pe->val[4], 0); 118 + WRITE_ONCE(pe->val[5], 0); 119 + WRITE_ONCE(pe->val[6], 0); 120 + WRITE_ONCE(pe->val[7], 0); 121 + } 122 + 123 + static inline void pasid_set_bits(u64 *ptr, u64 mask, u64 bits) 124 + { 125 + u64 old; 126 + 127 + old = READ_ONCE(*ptr); 128 + WRITE_ONCE(*ptr, (old & ~mask) | bits); 129 + } 130 + 131 + static inline u64 pasid_get_bits(u64 *ptr) 132 + { 133 + return READ_ONCE(*ptr); 134 + } 135 + 136 + /* 137 + * Setup the DID(Domain Identifier) field (Bit 64~79) of scalable mode 138 + * PASID entry. 139 + */ 140 + static inline void 141 + pasid_set_domain_id(struct pasid_entry *pe, u64 value) 142 + { 143 + pasid_set_bits(&pe->val[1], GENMASK_ULL(15, 0), value); 144 + } 145 + 146 + /* 147 + * Get domain ID value of a scalable mode PASID entry. 148 + */ 149 + static inline u16 150 + pasid_get_domain_id(struct pasid_entry *pe) 151 + { 152 + return (u16)(READ_ONCE(pe->val[1]) & GENMASK_ULL(15, 0)); 153 + } 154 + 155 + /* 156 + * Setup the SLPTPTR(Second Level Page Table Pointer) field (Bit 12~63) 157 + * of a scalable mode PASID entry. 158 + */ 159 + static inline void 160 + pasid_set_slptr(struct pasid_entry *pe, u64 value) 161 + { 162 + pasid_set_bits(&pe->val[0], VTD_PAGE_MASK, value); 163 + } 164 + 165 + /* 166 + * Setup the AW(Address Width) field (Bit 2~4) of a scalable mode PASID 167 + * entry. 168 + */ 169 + static inline void 170 + pasid_set_address_width(struct pasid_entry *pe, u64 value) 171 + { 172 + pasid_set_bits(&pe->val[0], GENMASK_ULL(4, 2), value << 2); 173 + } 174 + 175 + /* 176 + * Setup the PGTT(PASID Granular Translation Type) field (Bit 6~8) 177 + * of a scalable mode PASID entry. 178 + */ 179 + static inline void 180 + pasid_set_translation_type(struct pasid_entry *pe, u64 value) 181 + { 182 + pasid_set_bits(&pe->val[0], GENMASK_ULL(8, 6), value << 6); 183 + } 184 + 185 + /* 186 + * Enable fault processing by clearing the FPD(Fault Processing 187 + * Disable) field (Bit 1) of a scalable mode PASID entry. 188 + */ 189 + static inline void pasid_set_fault_enable(struct pasid_entry *pe) 190 + { 191 + pasid_set_bits(&pe->val[0], 1 << 1, 0); 192 + } 193 + 194 + /* 195 + * Enable second level A/D bits by setting the SLADE (Second Level 196 + * Access Dirty Enable) field (Bit 9) of a scalable mode PASID 197 + * entry. 198 + */ 199 + static inline void pasid_set_ssade(struct pasid_entry *pe) 200 + { 201 + pasid_set_bits(&pe->val[0], 1 << 9, 1 << 9); 202 + } 203 + 204 + /* 205 + * Disable second level A/D bits by clearing the SLADE (Second Level 206 + * Access Dirty Enable) field (Bit 9) of a scalable mode PASID 207 + * entry. 208 + */ 209 + static inline void pasid_clear_ssade(struct pasid_entry *pe) 210 + { 211 + pasid_set_bits(&pe->val[0], 1 << 9, 0); 212 + } 213 + 214 + /* 215 + * Checks if second level A/D bits specifically the SLADE (Second Level 216 + * Access Dirty Enable) field (Bit 9) of a scalable mode PASID 217 + * entry is set. 218 + */ 219 + static inline bool pasid_get_ssade(struct pasid_entry *pe) 220 + { 221 + return pasid_get_bits(&pe->val[0]) & (1 << 9); 222 + } 223 + 224 + /* 225 + * Setup the SRE(Supervisor Request Enable) field (Bit 128) of a 226 + * scalable mode PASID entry. 227 + */ 228 + static inline void pasid_set_sre(struct pasid_entry *pe) 229 + { 230 + pasid_set_bits(&pe->val[2], 1 << 0, 1); 231 + } 232 + 233 + /* 234 + * Setup the WPE(Write Protect Enable) field (Bit 132) of a 235 + * scalable mode PASID entry. 236 + */ 237 + static inline void pasid_set_wpe(struct pasid_entry *pe) 238 + { 239 + pasid_set_bits(&pe->val[2], 1 << 4, 1 << 4); 240 + } 241 + 242 + /* 243 + * Setup the P(Present) field (Bit 0) of a scalable mode PASID 244 + * entry. 245 + */ 246 + static inline void pasid_set_present(struct pasid_entry *pe) 247 + { 248 + pasid_set_bits(&pe->val[0], 1 << 0, 1); 249 + } 250 + 251 + /* 252 + * Setup Page Walk Snoop bit (Bit 87) of a scalable mode PASID 253 + * entry. 254 + */ 255 + static inline void pasid_set_page_snoop(struct pasid_entry *pe, bool value) 256 + { 257 + pasid_set_bits(&pe->val[1], 1 << 23, value << 23); 258 + } 259 + 260 + /* 261 + * Setup No Execute Enable bit (Bit 133) of a scalable mode PASID 262 + * entry. It is required when XD bit of the first level page table 263 + * entry is about to be set. 264 + */ 265 + static inline void pasid_set_nxe(struct pasid_entry *pe) 266 + { 267 + pasid_set_bits(&pe->val[2], 1 << 5, 1 << 5); 268 + } 269 + 270 + /* 271 + * Setup the Page Snoop (PGSNP) field (Bit 88) of a scalable mode 272 + * PASID entry. 273 + */ 274 + static inline void 275 + pasid_set_pgsnp(struct pasid_entry *pe) 276 + { 277 + pasid_set_bits(&pe->val[1], 1ULL << 24, 1ULL << 24); 278 + } 279 + 280 + /* 281 + * Setup the First Level Page table Pointer field (Bit 140~191) 282 + * of a scalable mode PASID entry. 283 + */ 284 + static inline void 285 + pasid_set_flptr(struct pasid_entry *pe, u64 value) 286 + { 287 + pasid_set_bits(&pe->val[2], VTD_PAGE_MASK, value); 288 + } 289 + 290 + /* 291 + * Setup the First Level Paging Mode field (Bit 130~131) of a 292 + * scalable mode PASID entry. 293 + */ 294 + static inline void 295 + pasid_set_flpm(struct pasid_entry *pe, u64 value) 296 + { 297 + pasid_set_bits(&pe->val[2], GENMASK_ULL(3, 2), value << 2); 298 + } 299 + 300 + /* 301 + * Setup the Extended Access Flag Enable (EAFE) field (Bit 135) 302 + * of a scalable mode PASID entry. 303 + */ 304 + static inline void pasid_set_eafe(struct pasid_entry *pe) 305 + { 306 + pasid_set_bits(&pe->val[2], 1 << 7, 1 << 7); 307 + } 308 + 89 309 extern unsigned int intel_pasid_max_id; 90 310 int intel_pasid_alloc_table(struct device *dev); 91 311 void intel_pasid_free_table(struct device *dev); ··· 311 111 struct device *dev, u32 pasid, 312 112 bool enabled); 313 113 int intel_pasid_setup_pass_through(struct intel_iommu *iommu, 314 - struct dmar_domain *domain, 315 114 struct device *dev, u32 pasid); 316 115 int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev, 317 116 u32 pasid, struct dmar_domain *domain); 318 117 void intel_pasid_tear_down_entry(struct intel_iommu *iommu, 319 118 struct device *dev, u32 pasid, 320 119 bool fault_ignore); 321 - int vcmd_alloc_pasid(struct intel_iommu *iommu, u32 *pasid); 322 - void vcmd_free_pasid(struct intel_iommu *iommu, u32 pasid); 323 120 void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu, 324 121 struct device *dev, u32 pasid); 325 122 #endif /* __INTEL_PASID_H */
+10 -24
drivers/iommu/intel/svm.c
··· 316 316 } 317 317 318 318 static int intel_svm_bind_mm(struct intel_iommu *iommu, struct device *dev, 319 - struct mm_struct *mm) 319 + struct iommu_domain *domain, ioasid_t pasid) 320 320 { 321 321 struct device_domain_info *info = dev_iommu_priv_get(dev); 322 + struct mm_struct *mm = domain->mm; 322 323 struct intel_svm_dev *sdev; 323 324 struct intel_svm *svm; 324 325 unsigned long sflags; 325 326 int ret = 0; 326 327 327 - svm = pasid_private_find(mm->pasid); 328 + svm = pasid_private_find(pasid); 328 329 if (!svm) { 329 330 svm = kzalloc(sizeof(*svm), GFP_KERNEL); 330 331 if (!svm) 331 332 return -ENOMEM; 332 333 333 - svm->pasid = mm->pasid; 334 + svm->pasid = pasid; 334 335 svm->mm = mm; 335 336 INIT_LIST_HEAD_RCU(&svm->devs); 336 337 ··· 369 368 370 369 /* Setup the pasid table: */ 371 370 sflags = cpu_feature_enabled(X86_FEATURE_LA57) ? PASID_FLAG_FL5LP : 0; 372 - ret = intel_pasid_setup_first_level(iommu, dev, mm->pgd, mm->pasid, 371 + ret = intel_pasid_setup_first_level(iommu, dev, mm->pgd, pasid, 373 372 FLPT_DEFAULT_DID, sflags); 374 373 if (ret) 375 374 goto free_sdev; ··· 383 382 free_svm: 384 383 if (list_empty(&svm->devs)) { 385 384 mmu_notifier_unregister(&svm->notifier, mm); 386 - pasid_private_remove(mm->pasid); 385 + pasid_private_remove(pasid); 387 386 kfree(svm); 388 387 } 389 388 ··· 393 392 void intel_svm_remove_dev_pasid(struct device *dev, u32 pasid) 394 393 { 395 394 struct intel_svm_dev *sdev; 396 - struct intel_iommu *iommu; 397 395 struct intel_svm *svm; 398 396 struct mm_struct *mm; 399 - 400 - iommu = device_to_iommu(dev, NULL, NULL); 401 - if (!iommu) 402 - return; 403 397 404 398 if (pasid_to_svm_sdev(dev, pasid, &svm, &sdev)) 405 399 return; ··· 746 750 struct iommu_fault_event *evt, 747 751 struct iommu_page_response *msg) 748 752 { 753 + struct device_domain_info *info = dev_iommu_priv_get(dev); 754 + struct intel_iommu *iommu = info->iommu; 755 + u8 bus = info->bus, devfn = info->devfn; 749 756 struct iommu_fault_page_request *prm; 750 - struct intel_iommu *iommu; 751 757 bool private_present; 752 758 bool pasid_present; 753 759 bool last_page; 754 - u8 bus, devfn; 755 760 int ret = 0; 756 761 u16 sid; 757 - 758 - if (!dev || !dev_is_pci(dev)) 759 - return -ENODEV; 760 - 761 - iommu = device_to_iommu(dev, &bus, &devfn); 762 - if (!iommu) 763 - return -ENODEV; 764 - 765 - if (!msg || !evt) 766 - return -EINVAL; 767 762 768 763 prm = &evt->fault.prm; 769 764 sid = PCI_DEVID(bus, devfn); ··· 809 822 { 810 823 struct device_domain_info *info = dev_iommu_priv_get(dev); 811 824 struct intel_iommu *iommu = info->iommu; 812 - struct mm_struct *mm = domain->mm; 813 825 814 - return intel_svm_bind_mm(iommu, dev, mm); 826 + return intel_svm_bind_mm(iommu, dev, domain, pasid); 815 827 } 816 828 817 829 static void intel_svm_domain_free(struct iommu_domain *domain)
+39 -16
drivers/iommu/io-pgtable-arm.c
··· 188 188 } 189 189 190 190 static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp, 191 - struct io_pgtable_cfg *cfg) 191 + struct io_pgtable_cfg *cfg, 192 + void *cookie) 192 193 { 193 194 struct device *dev = cfg->iommu_dev; 194 195 int order = get_order(size); 195 - struct page *p; 196 196 dma_addr_t dma; 197 197 void *pages; 198 198 199 199 VM_BUG_ON((gfp & __GFP_HIGHMEM)); 200 - p = alloc_pages_node(dev_to_node(dev), gfp | __GFP_ZERO, order); 201 - if (!p) 200 + 201 + if (cfg->alloc) { 202 + pages = cfg->alloc(cookie, size, gfp); 203 + } else { 204 + struct page *p; 205 + 206 + p = alloc_pages_node(dev_to_node(dev), gfp | __GFP_ZERO, order); 207 + pages = p ? page_address(p) : NULL; 208 + } 209 + 210 + if (!pages) 202 211 return NULL; 203 212 204 - pages = page_address(p); 205 213 if (!cfg->coherent_walk) { 206 214 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE); 207 215 if (dma_mapping_error(dev, dma)) ··· 228 220 out_unmap: 229 221 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); 230 222 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); 223 + 231 224 out_free: 232 - __free_pages(p, order); 225 + if (cfg->free) 226 + cfg->free(cookie, pages, size); 227 + else 228 + free_pages((unsigned long)pages, order); 229 + 233 230 return NULL; 234 231 } 235 232 236 233 static void __arm_lpae_free_pages(void *pages, size_t size, 237 - struct io_pgtable_cfg *cfg) 234 + struct io_pgtable_cfg *cfg, 235 + void *cookie) 238 236 { 239 237 if (!cfg->coherent_walk) 240 238 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages), 241 239 size, DMA_TO_DEVICE); 242 - free_pages((unsigned long)pages, get_order(size)); 240 + 241 + if (cfg->free) 242 + cfg->free(cookie, pages, size); 243 + else 244 + free_pages((unsigned long)pages, get_order(size)); 243 245 } 244 246 245 247 static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, int num_entries, ··· 391 373 /* Grab a pointer to the next level */ 392 374 pte = READ_ONCE(*ptep); 393 375 if (!pte) { 394 - cptep = __arm_lpae_alloc_pages(tblsz, gfp, cfg); 376 + cptep = __arm_lpae_alloc_pages(tblsz, gfp, cfg, data->iop.cookie); 395 377 if (!cptep) 396 378 return -ENOMEM; 397 379 398 380 pte = arm_lpae_install_table(cptep, ptep, 0, data); 399 381 if (pte) 400 - __arm_lpae_free_pages(cptep, tblsz, cfg); 382 + __arm_lpae_free_pages(cptep, tblsz, cfg, data->iop.cookie); 401 383 } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) { 402 384 __arm_lpae_sync_pte(ptep, 1, cfg); 403 385 } ··· 542 524 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); 543 525 } 544 526 545 - __arm_lpae_free_pages(start, table_size, &data->iop.cfg); 527 + __arm_lpae_free_pages(start, table_size, &data->iop.cfg, data->iop.cookie); 546 528 } 547 529 548 530 static void arm_lpae_free_pgtable(struct io_pgtable *iop) ··· 570 552 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 571 553 return 0; 572 554 573 - tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg); 555 + tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg, data->iop.cookie); 574 556 if (!tablep) 575 557 return 0; /* Bytes unmapped */ 576 558 ··· 593 575 594 576 pte = arm_lpae_install_table(tablep, ptep, blk_pte, data); 595 577 if (pte != blk_pte) { 596 - __arm_lpae_free_pages(tablep, tablesz, cfg); 578 + __arm_lpae_free_pages(tablep, tablesz, cfg, data->iop.cookie); 597 579 /* 598 580 * We may race against someone unmapping another part of this 599 581 * block, but anything else is invalid. We can't misinterpret ··· 900 882 901 883 /* Looking good; allocate a pgd */ 902 884 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), 903 - GFP_KERNEL, cfg); 885 + GFP_KERNEL, cfg, cookie); 904 886 if (!data->pgd) 905 887 goto out_free_data; 906 888 ··· 1002 984 1003 985 /* Allocate pgd pages */ 1004 986 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), 1005 - GFP_KERNEL, cfg); 987 + GFP_KERNEL, cfg, cookie); 1006 988 if (!data->pgd) 1007 989 goto out_free_data; 1008 990 ··· 1077 1059 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)); 1078 1060 1079 1061 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL, 1080 - cfg); 1062 + cfg, cookie); 1081 1063 if (!data->pgd) 1082 1064 goto out_free_data; 1083 1065 ··· 1098 1080 } 1099 1081 1100 1082 struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { 1083 + .caps = IO_PGTABLE_CAP_CUSTOM_ALLOCATOR, 1101 1084 .alloc = arm_64_lpae_alloc_pgtable_s1, 1102 1085 .free = arm_lpae_free_pgtable, 1103 1086 }; 1104 1087 1105 1088 struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = { 1089 + .caps = IO_PGTABLE_CAP_CUSTOM_ALLOCATOR, 1106 1090 .alloc = arm_64_lpae_alloc_pgtable_s2, 1107 1091 .free = arm_lpae_free_pgtable, 1108 1092 }; 1109 1093 1110 1094 struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = { 1095 + .caps = IO_PGTABLE_CAP_CUSTOM_ALLOCATOR, 1111 1096 .alloc = arm_32_lpae_alloc_pgtable_s1, 1112 1097 .free = arm_lpae_free_pgtable, 1113 1098 }; 1114 1099 1115 1100 struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = { 1101 + .caps = IO_PGTABLE_CAP_CUSTOM_ALLOCATOR, 1116 1102 .alloc = arm_32_lpae_alloc_pgtable_s2, 1117 1103 .free = arm_lpae_free_pgtable, 1118 1104 }; 1119 1105 1120 1106 struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = { 1107 + .caps = IO_PGTABLE_CAP_CUSTOM_ALLOCATOR, 1121 1108 .alloc = arm_mali_lpae_alloc_pgtable, 1122 1109 .free = arm_lpae_free_pgtable, 1123 1110 };
+23
drivers/iommu/io-pgtable.c
··· 34 34 #endif 35 35 }; 36 36 37 + static int check_custom_allocator(enum io_pgtable_fmt fmt, 38 + struct io_pgtable_cfg *cfg) 39 + { 40 + /* No custom allocator, no need to check the format. */ 41 + if (!cfg->alloc && !cfg->free) 42 + return 0; 43 + 44 + /* When passing a custom allocator, both the alloc and free 45 + * functions should be provided. 46 + */ 47 + if (!cfg->alloc || !cfg->free) 48 + return -EINVAL; 49 + 50 + /* Make sure the format supports custom allocators. */ 51 + if (io_pgtable_init_table[fmt]->caps & IO_PGTABLE_CAP_CUSTOM_ALLOCATOR) 52 + return 0; 53 + 54 + return -EINVAL; 55 + } 56 + 37 57 struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt, 38 58 struct io_pgtable_cfg *cfg, 39 59 void *cookie) ··· 62 42 const struct io_pgtable_init_fns *fns; 63 43 64 44 if (fmt >= IO_PGTABLE_NUM_FMTS) 45 + return NULL; 46 + 47 + if (check_custom_allocator(fmt, cfg)) 65 48 return NULL; 66 49 67 50 fns = io_pgtable_init_table[fmt];
+59 -42
drivers/iommu/iommu-sva.c
··· 12 12 static DEFINE_MUTEX(iommu_sva_lock); 13 13 14 14 /* Allocate a PASID for the mm within range (inclusive) */ 15 - static int iommu_sva_alloc_pasid(struct mm_struct *mm, struct device *dev) 15 + static struct iommu_mm_data *iommu_alloc_mm_data(struct mm_struct *mm, struct device *dev) 16 16 { 17 + struct iommu_mm_data *iommu_mm; 17 18 ioasid_t pasid; 18 - int ret = 0; 19 + 20 + lockdep_assert_held(&iommu_sva_lock); 19 21 20 22 if (!arch_pgtable_dma_compat(mm)) 21 - return -EBUSY; 23 + return ERR_PTR(-EBUSY); 22 24 23 - mutex_lock(&iommu_sva_lock); 25 + iommu_mm = mm->iommu_mm; 24 26 /* Is a PASID already associated with this mm? */ 25 - if (mm_valid_pasid(mm)) { 26 - if (mm->pasid >= dev->iommu->max_pasids) 27 - ret = -EOVERFLOW; 28 - goto out; 27 + if (iommu_mm) { 28 + if (iommu_mm->pasid >= dev->iommu->max_pasids) 29 + return ERR_PTR(-EOVERFLOW); 30 + return iommu_mm; 29 31 } 32 + 33 + iommu_mm = kzalloc(sizeof(struct iommu_mm_data), GFP_KERNEL); 34 + if (!iommu_mm) 35 + return ERR_PTR(-ENOMEM); 30 36 31 37 pasid = iommu_alloc_global_pasid(dev); 32 38 if (pasid == IOMMU_PASID_INVALID) { 33 - ret = -ENOSPC; 34 - goto out; 39 + kfree(iommu_mm); 40 + return ERR_PTR(-ENOSPC); 35 41 } 36 - mm->pasid = pasid; 37 - ret = 0; 38 - out: 39 - mutex_unlock(&iommu_sva_lock); 40 - return ret; 42 + iommu_mm->pasid = pasid; 43 + INIT_LIST_HEAD(&iommu_mm->sva_domains); 44 + /* 45 + * Make sure the write to mm->iommu_mm is not reordered in front of 46 + * initialization to iommu_mm fields. If it does, readers may see a 47 + * valid iommu_mm with uninitialized values. 48 + */ 49 + smp_store_release(&mm->iommu_mm, iommu_mm); 50 + return iommu_mm; 41 51 } 42 52 43 53 /** ··· 68 58 */ 69 59 struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm) 70 60 { 61 + struct iommu_mm_data *iommu_mm; 71 62 struct iommu_domain *domain; 72 63 struct iommu_sva *handle; 73 64 int ret; 74 65 75 - /* Allocate mm->pasid if necessary. */ 76 - ret = iommu_sva_alloc_pasid(mm, dev); 77 - if (ret) 78 - return ERR_PTR(ret); 79 - 80 - handle = kzalloc(sizeof(*handle), GFP_KERNEL); 81 - if (!handle) 82 - return ERR_PTR(-ENOMEM); 83 - 84 66 mutex_lock(&iommu_sva_lock); 85 - /* Search for an existing domain. */ 86 - domain = iommu_get_domain_for_dev_pasid(dev, mm->pasid, 87 - IOMMU_DOMAIN_SVA); 88 - if (IS_ERR(domain)) { 89 - ret = PTR_ERR(domain); 67 + 68 + /* Allocate mm->pasid if necessary. */ 69 + iommu_mm = iommu_alloc_mm_data(mm, dev); 70 + if (IS_ERR(iommu_mm)) { 71 + ret = PTR_ERR(iommu_mm); 90 72 goto out_unlock; 91 73 } 92 74 93 - if (domain) { 94 - domain->users++; 95 - goto out; 75 + handle = kzalloc(sizeof(*handle), GFP_KERNEL); 76 + if (!handle) { 77 + ret = -ENOMEM; 78 + goto out_unlock; 79 + } 80 + 81 + /* Search for an existing domain. */ 82 + list_for_each_entry(domain, &mm->iommu_mm->sva_domains, next) { 83 + ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid); 84 + if (!ret) { 85 + domain->users++; 86 + goto out; 87 + } 96 88 } 97 89 98 90 /* Allocate a new domain and set it on device pasid. */ 99 91 domain = iommu_sva_domain_alloc(dev, mm); 100 92 if (!domain) { 101 93 ret = -ENOMEM; 102 - goto out_unlock; 94 + goto out_free_handle; 103 95 } 104 96 105 - ret = iommu_attach_device_pasid(domain, dev, mm->pasid); 97 + ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid); 106 98 if (ret) 107 99 goto out_free_domain; 108 100 domain->users = 1; 101 + list_add(&domain->next, &mm->iommu_mm->sva_domains); 102 + 109 103 out: 110 104 mutex_unlock(&iommu_sva_lock); 111 105 handle->dev = dev; 112 106 handle->domain = domain; 113 - 114 107 return handle; 115 108 116 109 out_free_domain: 117 110 iommu_domain_free(domain); 111 + out_free_handle: 112 + kfree(handle); 118 113 out_unlock: 119 114 mutex_unlock(&iommu_sva_lock); 120 - kfree(handle); 121 - 122 115 return ERR_PTR(ret); 123 116 } 124 117 EXPORT_SYMBOL_GPL(iommu_sva_bind_device); ··· 137 124 void iommu_sva_unbind_device(struct iommu_sva *handle) 138 125 { 139 126 struct iommu_domain *domain = handle->domain; 140 - ioasid_t pasid = domain->mm->pasid; 127 + struct iommu_mm_data *iommu_mm = domain->mm->iommu_mm; 141 128 struct device *dev = handle->dev; 142 129 143 130 mutex_lock(&iommu_sva_lock); 131 + iommu_detach_device_pasid(domain, dev, iommu_mm->pasid); 144 132 if (--domain->users == 0) { 145 - iommu_detach_device_pasid(domain, dev, pasid); 133 + list_del(&domain->next); 146 134 iommu_domain_free(domain); 147 135 } 148 136 mutex_unlock(&iommu_sva_lock); ··· 155 141 { 156 142 struct iommu_domain *domain = handle->domain; 157 143 158 - return domain->mm->pasid; 144 + return mm_get_enqcmd_pasid(domain->mm); 159 145 } 160 146 EXPORT_SYMBOL_GPL(iommu_sva_get_pasid); 161 147 ··· 219 205 220 206 void mm_pasid_drop(struct mm_struct *mm) 221 207 { 222 - if (likely(!mm_valid_pasid(mm))) 208 + struct iommu_mm_data *iommu_mm = mm->iommu_mm; 209 + 210 + if (!iommu_mm) 223 211 return; 224 212 225 - iommu_free_global_pasid(mm->pasid); 213 + iommu_free_global_pasid(iommu_mm->pasid); 214 + kfree(iommu_mm); 226 215 }
+107 -47
drivers/iommu/iommu.c
··· 148 148 static LIST_HEAD(iommu_device_list); 149 149 static DEFINE_SPINLOCK(iommu_device_lock); 150 150 151 - static struct bus_type * const iommu_buses[] = { 151 + static const struct bus_type * const iommu_buses[] = { 152 152 &platform_bus_type, 153 153 #ifdef CONFIG_PCI 154 154 &pci_bus_type, ··· 257 257 /* We need to be able to take module references appropriately */ 258 258 if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner)) 259 259 return -EINVAL; 260 - /* 261 - * Temporarily enforce global restriction to a single driver. This was 262 - * already the de-facto behaviour, since any possible combination of 263 - * existing drivers would compete for at least the PCI or platform bus. 264 - */ 265 - if (iommu_buses[0]->iommu_ops && iommu_buses[0]->iommu_ops != ops) 266 - return -EBUSY; 267 260 268 261 iommu->ops = ops; 269 262 if (hwdev) ··· 266 273 list_add_tail(&iommu->list, &iommu_device_list); 267 274 spin_unlock(&iommu_device_lock); 268 275 269 - for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++) { 270 - iommu_buses[i]->iommu_ops = ops; 276 + for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++) 271 277 err = bus_iommu_probe(iommu_buses[i]); 272 - } 273 278 if (err) 274 279 iommu_device_unregister(iommu); 275 280 return err; ··· 320 329 list_add_tail(&iommu->list, &iommu_device_list); 321 330 spin_unlock(&iommu_device_lock); 322 331 323 - bus->iommu_ops = ops; 324 332 err = bus_iommu_probe(bus); 325 333 if (err) { 326 334 iommu_device_unregister_bus(iommu, bus, nb); ··· 333 343 static struct dev_iommu *dev_iommu_get(struct device *dev) 334 344 { 335 345 struct dev_iommu *param = dev->iommu; 346 + 347 + lockdep_assert_held(&iommu_probe_device_lock); 336 348 337 349 if (param) 338 350 return param; ··· 360 368 kfree(param); 361 369 } 362 370 371 + /* 372 + * Internal equivalent of device_iommu_mapped() for when we care that a device 373 + * actually has API ops, and don't want false positives from VFIO-only groups. 374 + */ 375 + static bool dev_has_iommu(struct device *dev) 376 + { 377 + return dev->iommu && dev->iommu->iommu_dev; 378 + } 379 + 363 380 static u32 dev_iommu_get_max_pasids(struct device *dev) 364 381 { 365 382 u32 max_pasids = 0, bits = 0; ··· 386 385 387 386 return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids); 388 387 } 388 + 389 + void dev_iommu_priv_set(struct device *dev, void *priv) 390 + { 391 + /* FSL_PAMU does something weird */ 392 + if (!IS_ENABLED(CONFIG_FSL_PAMU)) 393 + lockdep_assert_held(&iommu_probe_device_lock); 394 + dev->iommu->priv = priv; 395 + } 396 + EXPORT_SYMBOL_GPL(dev_iommu_priv_set); 389 397 390 398 /* 391 399 * Init the dev->iommu and dev->iommu_group in the struct device and get the ··· 499 489 500 490 static int __iommu_probe_device(struct device *dev, struct list_head *group_list) 501 491 { 502 - const struct iommu_ops *ops = dev->bus->iommu_ops; 492 + const struct iommu_ops *ops; 493 + struct iommu_fwspec *fwspec; 503 494 struct iommu_group *group; 504 495 struct group_device *gdev; 505 496 int ret; 497 + 498 + /* 499 + * For FDT-based systems and ACPI IORT/VIOT, drivers register IOMMU 500 + * instances with non-NULL fwnodes, and client devices should have been 501 + * identified with a fwspec by this point. Otherwise, we can currently 502 + * assume that only one of Intel, AMD, s390, PAMU or legacy SMMUv2 can 503 + * be present, and that any of their registered instances has suitable 504 + * ops for probing, and thus cheekily co-opt the same mechanism. 505 + */ 506 + fwspec = dev_iommu_fwspec_get(dev); 507 + if (fwspec && fwspec->ops) 508 + ops = fwspec->ops; 509 + else 510 + ops = iommu_ops_from_fwnode(NULL); 506 511 507 512 if (!ops) 508 513 return -ENODEV; ··· 643 618 644 619 list_del(&device->list); 645 620 __iommu_group_free_device(group, device); 646 - if (dev->iommu && dev->iommu->iommu_dev) 621 + if (dev_has_iommu(dev)) 647 622 iommu_deinit_device(dev); 648 623 else 649 624 dev->iommu_group = NULL; ··· 842 817 * Non-API groups still expose reserved_regions in sysfs, 843 818 * so filter out calls that get here that way. 844 819 */ 845 - if (!device->dev->iommu) 820 + if (!dev_has_iommu(device->dev)) 846 821 break; 847 822 848 823 INIT_LIST_HEAD(&dev_resv_regions); ··· 1247 1222 __iommu_group_remove_device(dev); 1248 1223 } 1249 1224 EXPORT_SYMBOL_GPL(iommu_group_remove_device); 1225 + 1226 + static struct device *iommu_group_first_dev(struct iommu_group *group) 1227 + { 1228 + lockdep_assert_held(&group->mutex); 1229 + return list_first_entry(&group->devices, struct group_device, list)->dev; 1230 + } 1250 1231 1251 1232 /** 1252 1233 * iommu_group_for_each_dev - iterate over each device in the group ··· 1782 1751 } 1783 1752 1784 1753 /* 1785 - * Returns the iommu_ops for the devices in an iommu group. 1786 - * 1787 - * It is assumed that all devices in an iommu group are managed by a single 1788 - * IOMMU unit. Therefore, this returns the dev_iommu_ops of the first device 1789 - * in the group. 1790 - */ 1791 - static const struct iommu_ops *group_iommu_ops(struct iommu_group *group) 1792 - { 1793 - struct group_device *device = 1794 - list_first_entry(&group->devices, struct group_device, list); 1795 - 1796 - lockdep_assert_held(&group->mutex); 1797 - 1798 - return dev_iommu_ops(device->dev); 1799 - } 1800 - 1801 - /* 1802 1754 * req_type of 0 means "auto" which means to select a domain based on 1803 1755 * iommu_def_domain_type or what the driver actually supports. 1804 1756 */ 1805 1757 static struct iommu_domain * 1806 1758 iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) 1807 1759 { 1808 - const struct iommu_ops *ops = group_iommu_ops(group); 1760 + const struct iommu_ops *ops = dev_iommu_ops(iommu_group_first_dev(group)); 1809 1761 struct iommu_domain *dom; 1810 1762 1811 1763 lockdep_assert_held(&group->mutex); ··· 1868 1854 static int iommu_get_def_domain_type(struct iommu_group *group, 1869 1855 struct device *dev, int cur_type) 1870 1856 { 1871 - const struct iommu_ops *ops = group_iommu_ops(group); 1857 + const struct iommu_ops *ops = dev_iommu_ops(dev); 1872 1858 int type; 1873 1859 1874 1860 if (!ops->def_domain_type) ··· 2017 2003 return 0; 2018 2004 } 2019 2005 2006 + /** 2007 + * iommu_present() - make platform-specific assumptions about an IOMMU 2008 + * @bus: bus to check 2009 + * 2010 + * Do not use this function. You want device_iommu_mapped() instead. 2011 + * 2012 + * Return: true if some IOMMU is present and aware of devices on the given bus; 2013 + * in general it may not be the only IOMMU, and it may not have anything to do 2014 + * with whatever device you are ultimately interested in. 2015 + */ 2020 2016 bool iommu_present(const struct bus_type *bus) 2021 2017 { 2022 - return bus->iommu_ops != NULL; 2018 + bool ret = false; 2019 + 2020 + for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) { 2021 + if (iommu_buses[i] == bus) { 2022 + spin_lock(&iommu_device_lock); 2023 + ret = !list_empty(&iommu_device_list); 2024 + spin_unlock(&iommu_device_lock); 2025 + } 2026 + } 2027 + return ret; 2023 2028 } 2024 2029 EXPORT_SYMBOL_GPL(iommu_present); 2025 2030 ··· 2054 2021 { 2055 2022 const struct iommu_ops *ops; 2056 2023 2057 - if (!dev->iommu || !dev->iommu->iommu_dev) 2024 + if (!dev_has_iommu(dev)) 2058 2025 return false; 2059 2026 2060 2027 ops = dev_iommu_ops(dev); ··· 2140 2107 return ERR_PTR(-ENOMEM); 2141 2108 2142 2109 domain->type = type; 2110 + domain->owner = ops; 2143 2111 /* 2144 2112 * If not already set, assume all sizes by default; the driver 2145 2113 * may override this later ··· 2166 2132 static struct iommu_domain * 2167 2133 __iommu_group_domain_alloc(struct iommu_group *group, unsigned int type) 2168 2134 { 2169 - struct device *dev = 2170 - list_first_entry(&group->devices, struct group_device, list) 2171 - ->dev; 2135 + struct device *dev = iommu_group_first_dev(group); 2172 2136 2173 - return __iommu_domain_alloc(group_iommu_ops(group), dev, type); 2137 + return __iommu_domain_alloc(dev_iommu_ops(dev), dev, type); 2138 + } 2139 + 2140 + static int __iommu_domain_alloc_dev(struct device *dev, void *data) 2141 + { 2142 + const struct iommu_ops **ops = data; 2143 + 2144 + if (!dev_has_iommu(dev)) 2145 + return 0; 2146 + 2147 + if (WARN_ONCE(*ops && *ops != dev_iommu_ops(dev), 2148 + "Multiple IOMMU drivers present for bus %s, which the public IOMMU API can't fully support yet. You will still need to disable one or more for this to work, sorry!\n", 2149 + dev_bus_name(dev))) 2150 + return -EBUSY; 2151 + 2152 + *ops = dev_iommu_ops(dev); 2153 + return 0; 2174 2154 } 2175 2155 2176 2156 struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus) 2177 2157 { 2158 + const struct iommu_ops *ops = NULL; 2159 + int err = bus_for_each_dev(bus, NULL, &ops, __iommu_domain_alloc_dev); 2178 2160 struct iommu_domain *domain; 2179 2161 2180 - if (bus == NULL || bus->iommu_ops == NULL) 2162 + if (err || !ops) 2181 2163 return NULL; 2182 - domain = __iommu_domain_alloc(bus->iommu_ops, NULL, 2183 - IOMMU_DOMAIN_UNMANAGED); 2164 + 2165 + domain = __iommu_domain_alloc(ops, NULL, IOMMU_DOMAIN_UNMANAGED); 2184 2166 if (IS_ERR(domain)) 2185 2167 return NULL; 2186 2168 return domain; ··· 2334 2284 static int __iommu_attach_group(struct iommu_domain *domain, 2335 2285 struct iommu_group *group) 2336 2286 { 2287 + struct device *dev; 2288 + 2337 2289 if (group->domain && group->domain != group->default_domain && 2338 2290 group->domain != group->blocking_domain) 2339 2291 return -EBUSY; 2292 + 2293 + dev = iommu_group_first_dev(group); 2294 + if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner) 2295 + return -EINVAL; 2340 2296 2341 2297 return __iommu_group_set_domain(group, domain); 2342 2298 } ··· 3060 3004 */ 3061 3005 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat) 3062 3006 { 3063 - if (dev->iommu && dev->iommu->iommu_dev) { 3064 - const struct iommu_ops *ops = dev->iommu->iommu_dev->ops; 3007 + if (dev_has_iommu(dev)) { 3008 + const struct iommu_ops *ops = dev_iommu_ops(dev); 3065 3009 3066 3010 if (ops->dev_enable_feat) 3067 3011 return ops->dev_enable_feat(dev, feat); ··· 3076 3020 */ 3077 3021 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat) 3078 3022 { 3079 - if (dev->iommu && dev->iommu->iommu_dev) { 3080 - const struct iommu_ops *ops = dev->iommu->iommu_dev->ops; 3023 + if (dev_has_iommu(dev)) { 3024 + const struct iommu_ops *ops = dev_iommu_ops(dev); 3081 3025 3082 3026 if (ops->dev_disable_feat) 3083 3027 return ops->dev_disable_feat(dev, feat); ··· 3537 3481 if (!group) 3538 3482 return -ENODEV; 3539 3483 3484 + if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner) 3485 + return -EINVAL; 3486 + 3540 3487 mutex_lock(&group->mutex); 3541 3488 curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL); 3542 3489 if (curr) { ··· 3628 3569 domain->type = IOMMU_DOMAIN_SVA; 3629 3570 mmgrab(mm); 3630 3571 domain->mm = mm; 3572 + domain->owner = ops; 3631 3573 domain->iopf_handler = iommu_sva_handle_iopf; 3632 3574 domain->fault_data = mm; 3633 3575
+2
drivers/iommu/iommufd/hw_pagetable.c
··· 135 135 hwpt->domain = NULL; 136 136 goto out_abort; 137 137 } 138 + hwpt->domain->owner = ops; 138 139 } else { 139 140 hwpt->domain = iommu_domain_alloc(idev->dev->bus); 140 141 if (!hwpt->domain) { ··· 234 233 hwpt->domain = NULL; 235 234 goto out_abort; 236 235 } 236 + hwpt->domain->owner = ops; 237 237 238 238 if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) { 239 239 rc = -EINVAL;
+1 -6
drivers/iommu/mtk_iommu.c
··· 863 863 static struct iommu_device *mtk_iommu_probe_device(struct device *dev) 864 864 { 865 865 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 866 - struct mtk_iommu_data *data; 866 + struct mtk_iommu_data *data = dev_iommu_priv_get(dev); 867 867 struct device_link *link; 868 868 struct device *larbdev; 869 869 unsigned int larbid, larbidx, i; 870 - 871 - if (!fwspec || fwspec->ops != &mtk_iommu_ops) 872 - return ERR_PTR(-ENODEV); /* Not a iommu client device */ 873 - 874 - data = dev_iommu_priv_get(dev); 875 870 876 871 if (!MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) 877 872 return &data->iommu;
-3
drivers/iommu/mtk_iommu_v1.c
··· 481 481 idx++; 482 482 } 483 483 484 - if (!fwspec || fwspec->ops != &mtk_iommu_v1_ops) 485 - return ERR_PTR(-ENODEV); /* Not a iommu client device */ 486 - 487 484 data = dev_iommu_priv_get(dev); 488 485 489 486 /* Link the consumer device with the smi-larb device(supplier) */
+33 -38
drivers/iommu/of_iommu.c
··· 17 17 #include <linux/slab.h> 18 18 #include <linux/fsl/mc.h> 19 19 20 - #define NO_IOMMU 1 21 - 22 20 static int of_iommu_xlate(struct device *dev, 23 21 struct of_phandle_args *iommu_spec) 24 22 { ··· 27 29 ops = iommu_ops_from_fwnode(fwnode); 28 30 if ((ops && !ops->of_xlate) || 29 31 !of_device_is_available(iommu_spec->np)) 30 - return NO_IOMMU; 32 + return -ENODEV; 31 33 32 34 ret = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops); 33 35 if (ret) ··· 59 61 "iommu-map-mask", &iommu_spec.np, 60 62 iommu_spec.args); 61 63 if (err) 62 - return err == -ENODEV ? NO_IOMMU : err; 64 + return err; 63 65 64 66 err = of_iommu_xlate(dev, &iommu_spec); 65 67 of_node_put(iommu_spec.np); ··· 70 72 struct device *dev) 71 73 { 72 74 struct of_phandle_args iommu_spec; 73 - int err = NO_IOMMU, idx = 0; 75 + int err = -ENODEV, idx = 0; 74 76 75 77 while (!of_parse_phandle_with_args(master_np, "iommus", 76 78 "#iommu-cells", ··· 105 107 of_iommu_configure_dev(master_np, dev); 106 108 } 107 109 108 - const struct iommu_ops *of_iommu_configure(struct device *dev, 109 - struct device_node *master_np, 110 - const u32 *id) 110 + /* 111 + * Returns: 112 + * 0 on success, an iommu was configured 113 + * -ENODEV if the device does not have any IOMMU 114 + * -EPROBEDEFER if probing should be tried again 115 + * -errno fatal errors 116 + */ 117 + int of_iommu_configure(struct device *dev, struct device_node *master_np, 118 + const u32 *id) 111 119 { 112 - const struct iommu_ops *ops = NULL; 113 120 struct iommu_fwspec *fwspec; 114 - int err = NO_IOMMU; 121 + int err; 115 122 116 123 if (!master_np) 117 - return NULL; 124 + return -ENODEV; 118 125 119 126 /* Serialise to make dev->iommu stable under our potential fwspec */ 120 127 mutex_lock(&iommu_probe_device_lock); ··· 127 124 if (fwspec) { 128 125 if (fwspec->ops) { 129 126 mutex_unlock(&iommu_probe_device_lock); 130 - return fwspec->ops; 127 + return 0; 131 128 } 132 129 /* In the deferred case, start again from scratch */ 133 130 iommu_fwspec_free(dev); ··· 150 147 } else { 151 148 err = of_iommu_configure_device(master_np, dev, id); 152 149 } 153 - 154 - /* 155 - * Two success conditions can be represented by non-negative err here: 156 - * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons 157 - * 0 : we found an IOMMU, and dev->fwspec is initialised appropriately 158 - * <0 : any actual error 159 - */ 160 - if (!err) { 161 - /* The fwspec pointer changed, read it again */ 162 - fwspec = dev_iommu_fwspec_get(dev); 163 - ops = fwspec->ops; 164 - } 165 150 mutex_unlock(&iommu_probe_device_lock); 166 151 167 - /* 168 - * If we have reason to believe the IOMMU driver missed the initial 169 - * probe for dev, replay it to get things in order. 170 - */ 171 - if (!err && dev->bus) 172 - err = iommu_probe_device(dev); 152 + if (err == -ENODEV || err == -EPROBE_DEFER) 153 + return err; 154 + if (err) 155 + goto err_log; 173 156 174 - /* Ignore all other errors apart from EPROBE_DEFER */ 175 - if (err == -EPROBE_DEFER) { 176 - ops = ERR_PTR(err); 177 - } else if (err < 0) { 178 - dev_dbg(dev, "Adding to IOMMU failed: %d\n", err); 179 - ops = NULL; 180 - } 157 + err = iommu_probe_device(dev); 158 + if (err) 159 + goto err_log; 160 + return 0; 181 161 182 - return ops; 162 + err_log: 163 + dev_dbg(dev, "Adding to IOMMU failed: %pe\n", ERR_PTR(err)); 164 + return err; 183 165 } 184 166 185 167 static enum iommu_resv_type __maybe_unused ··· 248 260 phys_addr_t iova; 249 261 size_t length; 250 262 263 + if (of_dma_is_coherent(dev->of_node)) 264 + prot |= IOMMU_CACHE; 265 + 251 266 maps = of_translate_dma_region(np, maps, &iova, &length); 267 + if (length == 0) { 268 + dev_warn(dev, "Cannot reserve IOVA region of 0 size\n"); 269 + continue; 270 + } 252 271 type = iommu_resv_region_get_type(dev, &phys, iova, length); 253 272 254 273 region = iommu_alloc_resv_region(iova, length, prot, type,
-1
drivers/iommu/omap-iommu.c
··· 1719 1719 if (!dev->of_node || !arch_data) 1720 1720 return; 1721 1721 1722 - dev_iommu_priv_set(dev, NULL); 1723 1722 kfree(arch_data); 1724 1723 1725 1724 }
+1 -7
drivers/iommu/sprd-iommu.c
··· 385 385 386 386 static struct iommu_device *sprd_iommu_probe_device(struct device *dev) 387 387 { 388 - struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 389 - struct sprd_iommu_device *sdev; 390 - 391 - if (!fwspec || fwspec->ops != &sprd_iommu_ops) 392 - return ERR_PTR(-ENODEV); 393 - 394 - sdev = dev_iommu_priv_get(dev); 388 + struct sprd_iommu_device *sdev = dev_iommu_priv_get(dev); 395 389 396 390 return &sdev->iommu; 397 391 }
+32 -4
drivers/iommu/virtio-iommu.c
··· 843 843 .flags = cpu_to_le32(flags), 844 844 }; 845 845 846 - ret = viommu_send_req_sync(vdomain->viommu, &map, sizeof(map)); 846 + ret = viommu_add_req(vdomain->viommu, &map, sizeof(map)); 847 847 if (ret) { 848 848 viommu_del_mappings(vdomain, iova, end); 849 849 return ret; ··· 912 912 viommu_sync_req(vdomain->viommu); 913 913 } 914 914 915 + static int viommu_iotlb_sync_map(struct iommu_domain *domain, 916 + unsigned long iova, size_t size) 917 + { 918 + struct viommu_domain *vdomain = to_viommu_domain(domain); 919 + 920 + /* 921 + * May be called before the viommu is initialized including 922 + * while creating direct mapping 923 + */ 924 + if (!vdomain->nr_endpoints) 925 + return 0; 926 + return viommu_sync_req(vdomain->viommu); 927 + } 928 + 929 + static void viommu_flush_iotlb_all(struct iommu_domain *domain) 930 + { 931 + struct viommu_domain *vdomain = to_viommu_domain(domain); 932 + 933 + /* 934 + * May be called before the viommu is initialized including 935 + * while creating direct mapping 936 + */ 937 + if (!vdomain->nr_endpoints) 938 + return; 939 + viommu_sync_req(vdomain->viommu); 940 + } 941 + 915 942 static void viommu_get_resv_regions(struct device *dev, struct list_head *head) 916 943 { 917 944 struct iommu_resv_region *entry, *new_entry, *msi = NULL; ··· 995 968 struct viommu_endpoint *vdev; 996 969 struct viommu_dev *viommu = NULL; 997 970 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 998 - 999 - if (!fwspec || fwspec->ops != &viommu_ops) 1000 - return ERR_PTR(-ENODEV); 1001 971 1002 972 viommu = viommu_get_by_fwnode(fwspec->iommu_fwnode); 1003 973 if (!viommu) ··· 1061 1037 switch (cap) { 1062 1038 case IOMMU_CAP_CACHE_COHERENCY: 1063 1039 return true; 1040 + case IOMMU_CAP_DEFERRED_FLUSH: 1041 + return true; 1064 1042 default: 1065 1043 return false; 1066 1044 } ··· 1083 1057 .map_pages = viommu_map_pages, 1084 1058 .unmap_pages = viommu_unmap_pages, 1085 1059 .iova_to_phys = viommu_iova_to_phys, 1060 + .flush_iotlb_all = viommu_flush_iotlb_all, 1086 1061 .iotlb_sync = viommu_iotlb_sync, 1062 + .iotlb_sync_map = viommu_iotlb_sync_map, 1087 1063 .free = viommu_domain_free, 1088 1064 } 1089 1065 };
+8 -6
drivers/memory/tegra/tegra186.c
··· 114 114 static int tegra186_mc_probe_device(struct tegra_mc *mc, struct device *dev) 115 115 { 116 116 #if IS_ENABLED(CONFIG_IOMMU_API) 117 - struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 118 117 struct of_phandle_args args; 119 118 unsigned int i, index = 0; 119 + u32 sid; 120 + 121 + if (!tegra_dev_iommu_get_stream_id(dev, &sid)) 122 + return 0; 120 123 121 124 while (!of_parse_phandle_with_args(dev->of_node, "interconnects", "#interconnect-cells", 122 125 index, &args)) { ··· 127 124 for (i = 0; i < mc->soc->num_clients; i++) { 128 125 const struct tegra_mc_client *client = &mc->soc->clients[i]; 129 126 130 - if (client->id == args.args[0]) { 131 - u32 sid = fwspec->ids[0] & MC_SID_STREAMID_OVERRIDE_MASK; 132 - 133 - tegra186_mc_client_sid_override(mc, client, sid); 134 - } 127 + if (client->id == args.args[0]) 128 + tegra186_mc_client_sid_override( 129 + mc, client, 130 + sid & MC_SID_STREAMID_OVERRIDE_MASK); 135 131 } 136 132 } 137 133
+16 -8
drivers/of/device.c
··· 93 93 int of_dma_configure_id(struct device *dev, struct device_node *np, 94 94 bool force_dma, const u32 *id) 95 95 { 96 - const struct iommu_ops *iommu; 97 96 const struct bus_dma_region *map = NULL; 98 97 struct device_node *bus_np; 99 98 u64 dma_start = 0; 100 99 u64 mask, end, size = 0; 101 100 bool coherent; 101 + int iommu_ret; 102 102 int ret; 103 103 104 104 if (np == dev->of_node) ··· 181 181 dev_dbg(dev, "device is%sdma coherent\n", 182 182 coherent ? " " : " not "); 183 183 184 - iommu = of_iommu_configure(dev, np, id); 185 - if (PTR_ERR(iommu) == -EPROBE_DEFER) { 184 + iommu_ret = of_iommu_configure(dev, np, id); 185 + if (iommu_ret == -EPROBE_DEFER) { 186 186 /* Don't touch range map if it wasn't set from a valid dma-ranges */ 187 187 if (!ret) 188 188 dev->dma_range_map = NULL; 189 189 kfree(map); 190 190 return -EPROBE_DEFER; 191 - } 191 + } else if (iommu_ret == -ENODEV) { 192 + dev_dbg(dev, "device is not behind an iommu\n"); 193 + } else if (iommu_ret) { 194 + dev_err(dev, "iommu configuration for device failed with %pe\n", 195 + ERR_PTR(iommu_ret)); 192 196 193 - dev_dbg(dev, "device is%sbehind an iommu\n", 194 - iommu ? " " : " not "); 197 + /* 198 + * Historically this routine doesn't fail driver probing 199 + * due to errors in of_iommu_configure() 200 + */ 201 + } else 202 + dev_dbg(dev, "device is behind an iommu\n"); 195 203 196 - arch_setup_dma_ops(dev, dma_start, size, iommu, coherent); 204 + arch_setup_dma_ops(dev, dma_start, size, coherent); 197 205 198 - if (!iommu) 206 + if (iommu_ret) 199 207 of_dma_set_restricted_buffer(dev, np); 200 208 201 209 return 0;
+2
include/acpi/acpi_bus.h
··· 719 719 720 720 /* helper */ 721 721 722 + struct iommu_ops; 723 + 722 724 bool acpi_dma_supported(const struct acpi_device *adev); 723 725 enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev); 724 726 int acpi_iommu_fwspec_init(struct device *dev, u32 id,
-1
include/linux/device.h
··· 42 42 struct subsys_private; 43 43 struct device_node; 44 44 struct fwnode_handle; 45 - struct iommu_ops; 46 45 struct iommu_group; 47 46 struct dev_pin_info; 48 47 struct dev_iommu;
-5
include/linux/device/bus.h
··· 62 62 * this bus. 63 63 * @pm: Power management operations of this bus, callback the specific 64 64 * device driver's pm-ops. 65 - * @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU 66 - * driver implementations to a bus and allow the driver to do 67 - * bus-specific setup 68 65 * @need_parent_lock: When probing or removing a device on this bus, the 69 66 * device core should lock the device's parent. 70 67 * ··· 100 103 void (*dma_cleanup)(struct device *dev); 101 104 102 105 const struct dev_pm_ops *pm; 103 - 104 - const struct iommu_ops *iommu_ops; 105 106 106 107 bool need_parent_lock; 107 108 };
+3 -2
include/linux/dma-map-ops.h
··· 11 11 #include <linux/slab.h> 12 12 13 13 struct cma; 14 + struct iommu_ops; 14 15 15 16 /* 16 17 * Values for struct dma_map_ops.flags: ··· 427 426 428 427 #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS 429 428 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, 430 - const struct iommu_ops *iommu, bool coherent); 429 + bool coherent); 431 430 #else 432 431 static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base, 433 - u64 size, const struct iommu_ops *iommu, bool coherent) 432 + u64 size, bool coherent) 434 433 { 435 434 } 436 435 #endif /* CONFIG_ARCH_HAS_SETUP_DMA_OPS */
+34
include/linux/io-pgtable.h
··· 100 100 const struct iommu_flush_ops *tlb; 101 101 struct device *iommu_dev; 102 102 103 + /** 104 + * @alloc: Custom page allocator. 105 + * 106 + * Optional hook used to allocate page tables. If this function is NULL, 107 + * @free must be NULL too. 108 + * 109 + * Memory returned should be zeroed and suitable for dma_map_single() and 110 + * virt_to_phys(). 111 + * 112 + * Not all formats support custom page allocators. Before considering 113 + * passing a non-NULL value, make sure the chosen page format supports 114 + * this feature. 115 + */ 116 + void *(*alloc)(void *cookie, size_t size, gfp_t gfp); 117 + 118 + /** 119 + * @free: Custom page de-allocator. 120 + * 121 + * Optional hook used to free page tables allocated with the @alloc 122 + * hook. Must be non-NULL if @alloc is not NULL, must be NULL 123 + * otherwise. 124 + */ 125 + void (*free)(void *cookie, void *pages, size_t size); 126 + 103 127 /* Low-level data specific to the table format */ 104 128 union { 105 129 struct { ··· 266 242 } 267 243 268 244 /** 245 + * enum io_pgtable_caps - IO page table backend capabilities. 246 + */ 247 + enum io_pgtable_caps { 248 + /** @IO_PGTABLE_CAP_CUSTOM_ALLOCATOR: Backend accepts custom page table allocators. */ 249 + IO_PGTABLE_CAP_CUSTOM_ALLOCATOR = BIT(0), 250 + }; 251 + 252 + /** 269 253 * struct io_pgtable_init_fns - Alloc/free a set of page tables for a 270 254 * particular format. 271 255 * 272 256 * @alloc: Allocate a set of page tables described by cfg. 273 257 * @free: Free the page tables associated with iop. 258 + * @caps: Combination of @io_pgtable_caps flags encoding the backend capabilities. 274 259 */ 275 260 struct io_pgtable_init_fns { 276 261 struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie); 277 262 void (*free)(struct io_pgtable *iop); 263 + u32 caps; 278 264 }; 279 265 280 266 extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns;
+39 -8
include/linux/iommu.h
··· 106 106 unsigned type; 107 107 const struct iommu_domain_ops *ops; 108 108 const struct iommu_dirty_ops *dirty_ops; 109 - 109 + const struct iommu_ops *owner; /* Whose domain_alloc we came from */ 110 110 unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */ 111 111 struct iommu_domain_geometry geometry; 112 112 struct iommu_dma_cookie *iova_cookie; ··· 121 121 struct { /* IOMMU_DOMAIN_SVA */ 122 122 struct mm_struct *mm; 123 123 int users; 124 + /* 125 + * Next iommu_domain in mm->iommu_mm->sva-domains list 126 + * protected by iommu_sva_lock. 127 + */ 128 + struct list_head next; 124 129 }; 125 130 }; 126 131 }; ··· 817 812 struct iommu_domain *domain; 818 813 }; 819 814 815 + struct iommu_mm_data { 816 + u32 pasid; 817 + struct list_head sva_domains; 818 + }; 819 + 820 820 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode, 821 821 const struct iommu_ops *ops); 822 822 void iommu_fwspec_free(struct device *dev); ··· 850 840 return NULL; 851 841 } 852 842 853 - static inline void dev_iommu_priv_set(struct device *dev, void *priv) 854 - { 855 - dev->iommu->priv = priv; 856 - } 843 + void dev_iommu_priv_set(struct device *dev, void *priv); 857 844 858 845 extern struct mutex iommu_probe_device_lock; 859 846 int iommu_probe_device(struct device *dev); ··· 1344 1337 return false; 1345 1338 } 1346 1339 1347 - #ifdef CONFIG_IOMMU_SVA 1340 + #ifdef CONFIG_IOMMU_MM_DATA 1348 1341 static inline void mm_pasid_init(struct mm_struct *mm) 1349 1342 { 1350 - mm->pasid = IOMMU_PASID_INVALID; 1343 + /* 1344 + * During dup_mm(), a new mm will be memcpy'd from an old one and that makes 1345 + * the new mm and the old one point to a same iommu_mm instance. When either 1346 + * one of the two mms gets released, the iommu_mm instance is freed, leaving 1347 + * the other mm running into a use-after-free/double-free problem. To avoid 1348 + * the problem, zeroing the iommu_mm pointer of a new mm is needed here. 1349 + */ 1350 + mm->iommu_mm = NULL; 1351 1351 } 1352 + 1352 1353 static inline bool mm_valid_pasid(struct mm_struct *mm) 1353 1354 { 1354 - return mm->pasid != IOMMU_PASID_INVALID; 1355 + return READ_ONCE(mm->iommu_mm); 1355 1356 } 1357 + 1358 + static inline u32 mm_get_enqcmd_pasid(struct mm_struct *mm) 1359 + { 1360 + struct iommu_mm_data *iommu_mm = READ_ONCE(mm->iommu_mm); 1361 + 1362 + if (!iommu_mm) 1363 + return IOMMU_PASID_INVALID; 1364 + return iommu_mm->pasid; 1365 + } 1366 + 1356 1367 void mm_pasid_drop(struct mm_struct *mm); 1357 1368 struct iommu_sva *iommu_sva_bind_device(struct device *dev, 1358 1369 struct mm_struct *mm); ··· 1393 1368 } 1394 1369 static inline void mm_pasid_init(struct mm_struct *mm) {} 1395 1370 static inline bool mm_valid_pasid(struct mm_struct *mm) { return false; } 1371 + 1372 + static inline u32 mm_get_enqcmd_pasid(struct mm_struct *mm) 1373 + { 1374 + return IOMMU_PASID_INVALID; 1375 + } 1376 + 1396 1377 static inline void mm_pasid_drop(struct mm_struct *mm) {} 1397 1378 #endif /* CONFIG_IOMMU_SVA */ 1398 1379
+3 -2
include/linux/mm_types.h
··· 730 730 #endif 731 731 732 732 struct kioctx_table; 733 + struct iommu_mm_data; 733 734 struct mm_struct { 734 735 struct { 735 736 /* ··· 942 941 #endif 943 942 struct work_struct async_put_work; 944 943 945 - #ifdef CONFIG_IOMMU_SVA 946 - u32 pasid; 944 + #ifdef CONFIG_IOMMU_MM_DATA 945 + struct iommu_mm_data *iommu_mm; 947 946 #endif 948 947 #ifdef CONFIG_KSM 949 948 /*
+6 -7
include/linux/of_iommu.h
··· 8 8 9 9 #ifdef CONFIG_OF_IOMMU 10 10 11 - extern const struct iommu_ops *of_iommu_configure(struct device *dev, 12 - struct device_node *master_np, 13 - const u32 *id); 11 + extern int of_iommu_configure(struct device *dev, struct device_node *master_np, 12 + const u32 *id); 14 13 15 14 extern void of_iommu_get_resv_regions(struct device *dev, 16 15 struct list_head *list); 17 16 18 17 #else 19 18 20 - static inline const struct iommu_ops *of_iommu_configure(struct device *dev, 21 - struct device_node *master_np, 22 - const u32 *id) 19 + static inline int of_iommu_configure(struct device *dev, 20 + struct device_node *master_np, 21 + const u32 *id) 23 22 { 24 - return NULL; 23 + return -ENODEV; 25 24 } 26 25 27 26 static inline void of_iommu_get_resv_regions(struct device *dev,
+1 -1
include/linux/sched.h
··· 957 957 /* Recursion prevention for eventfd_signal() */ 958 958 unsigned in_eventfd:1; 959 959 #endif 960 - #ifdef CONFIG_IOMMU_SVA 960 + #ifdef CONFIG_ARCH_HAS_CPU_PASID 961 961 unsigned pasid_activated:1; 962 962 #endif 963 963 #ifdef CONFIG_CPU_SUP_INTEL
+1 -1
kernel/fork.c
··· 1173 1173 tsk->use_memdelay = 0; 1174 1174 #endif 1175 1175 1176 - #ifdef CONFIG_IOMMU_SVA 1176 + #ifdef CONFIG_ARCH_HAS_CPU_PASID 1177 1177 tsk->pasid_activated = 0; 1178 1178 #endif 1179 1179
+3
mm/Kconfig
··· 1258 1258 bool 1259 1259 depends on !STACK_GROWSUP 1260 1260 1261 + config IOMMU_MM_DATA 1262 + bool 1263 + 1261 1264 source "mm/damon/Kconfig" 1262 1265 1263 1266 endmenu
-3
mm/init-mm.c
··· 44 44 #endif 45 45 .user_ns = &init_user_ns, 46 46 .cpu_bitmap = CPU_BITS_NONE, 47 - #ifdef CONFIG_IOMMU_SVA 48 - .pasid = IOMMU_PASID_INVALID, 49 - #endif 50 47 INIT_MM_CONTEXT(init_mm) 51 48 }; 52 49