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

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

Pull iommu updates from Joerg Roedel:

- remove the bus_set_iommu() interface which became unnecesary because
of IOMMU per-device probing

- make the dma-iommu.h header private

- Intel VT-d changes from Lu Baolu:
- Decouple PASID and PRI from SVA
- Add ESRTPS & ESIRTPS capability check
- Cleanups

- Apple DART support for the M1 Pro/MAX SOCs

- support for AMD IOMMUv2 page-tables for the DMA-API layer.

The v2 page-tables are compatible with the x86 CPU page-tables. Using
them for DMA-API prepares support for hardware-assisted IOMMU
virtualization

- support for MT6795 Helio X10 M4Us in the Mediatek IOMMU driver

- some smaller fixes and cleanups

* tag 'iommu-updates-v6.1' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu: (59 commits)
iommu/vt-d: Avoid unnecessary global DMA cache invalidation
iommu/vt-d: Avoid unnecessary global IRTE cache invalidation
iommu/vt-d: Rename cap_5lp_support to cap_fl5lp_support
iommu/vt-d: Remove pasid_set_eafe()
iommu/vt-d: Decouple PASID & PRI enabling from SVA
iommu/vt-d: Remove unnecessary SVA data accesses in page fault path
dt-bindings: iommu: arm,smmu-v3: Relax order of interrupt names
iommu: dart: Support t6000 variant
iommu/io-pgtable-dart: Add DART PTE support for t6000
iommu/io-pgtable: Add DART subpage protection support
iommu/io-pgtable: Move Apple DART support to its own file
iommu/mediatek: Add support for MT6795 Helio X10 M4Us
iommu/mediatek: Introduce new flag TF_PORT_TO_ADDR_MT8173
dt-bindings: mediatek: Add bindings for MT6795 M4U
iommu/iova: Fix module config properly
iommu/amd: Fix sparse warning
iommu/amd: Remove outdated comment
iommu/amd: Free domain ID after domain_flush_pages
iommu/amd: Free domain id in error path
iommu/virtio: Fix compile error with viommu_capable()
...

+1501 -902
+2
Documentation/admin-guide/kernel-parameters.txt
··· 321 321 force_enable - Force enable the IOMMU on platforms known 322 322 to be buggy with IOMMU enabled. Use this 323 323 option with care. 324 + pgtbl_v1 - Use v1 page table for DMA-API (Default). 325 + pgtbl_v2 - Use v2 page table for DMA-API. 324 326 325 327 amd_iommu_dump= [HW,X86-64] 326 328 Enable AMD IOMMU driver option to dump the ACPI table
+5 -10
Documentation/devicetree/bindings/iommu/arm,smmu-v3.yaml
··· 39 39 any others. 40 40 - minItems: 1 41 41 items: 42 - - enum: 43 - - eventq # Event Queue not empty 44 - - gerror # Global Error activated 45 - - const: gerror 46 - - enum: 47 - - cmdq-sync # CMD_SYNC complete 48 - - priq # PRI Queue not empty 49 - - enum: 50 - - cmdq-sync 51 - - priq 42 + enum: 43 + - eventq # Event Queue not empty 44 + - gerror # Global Error activated 45 + - cmdq-sync # CMD_SYNC complete 46 + - priq # PRI Queue not empty 52 47 53 48 '#iommu-cells': 54 49 const: 1
+4
Documentation/devicetree/bindings/iommu/mediatek,iommu.yaml
··· 73 73 - mediatek,mt2701-m4u # generation one 74 74 - mediatek,mt2712-m4u # generation two 75 75 - mediatek,mt6779-m4u # generation two 76 + - mediatek,mt6795-m4u # generation two 76 77 - mediatek,mt8167-m4u # generation two 77 78 - mediatek,mt8173-m4u # generation two 78 79 - mediatek,mt8183-m4u # generation two ··· 125 124 dt-binding/memory/mt2701-larb-port.h for mt2701 and mt7623, 126 125 dt-binding/memory/mt2712-larb-port.h for mt2712, 127 126 dt-binding/memory/mt6779-larb-port.h for mt6779, 127 + dt-binding/memory/mt6795-larb-port.h for mt6795, 128 128 dt-binding/memory/mt8167-larb-port.h for mt8167, 129 129 dt-binding/memory/mt8173-larb-port.h for mt8173, 130 130 dt-binding/memory/mt8183-larb-port.h for mt8183, ··· 150 148 enum: 151 149 - mediatek,mt2701-m4u 152 150 - mediatek,mt2712-m4u 151 + - mediatek,mt6795-m4u 153 152 - mediatek,mt8173-m4u 154 153 - mediatek,mt8186-iommu-mm 155 154 - mediatek,mt8192-m4u ··· 180 177 contains: 181 178 enum: 182 179 - mediatek,mt2712-m4u 180 + - mediatek,mt6795-m4u 183 181 - mediatek,mt8173-m4u 184 182 185 183 then:
+2 -1
MAINTAINERS
··· 1915 1915 F: drivers/i2c/busses/i2c-pasemi-core.c 1916 1916 F: drivers/i2c/busses/i2c-pasemi-platform.c 1917 1917 F: drivers/iommu/apple-dart.c 1918 + F: drivers/iommu/io-pgtable-dart.c 1918 1919 F: drivers/irqchip/irq-apple-aic.c 1919 1920 F: drivers/mailbox/apple-mailbox.c 1920 1921 F: drivers/nvme/host/apple.c ··· 10690 10689 S: Maintained 10691 10690 T: git git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu.git 10692 10691 F: drivers/iommu/dma-iommu.c 10692 + F: drivers/iommu/dma-iommu.h 10693 10693 F: drivers/iommu/iova.c 10694 - F: include/linux/dma-iommu.h 10695 10694 F: include/linux/iova.h 10696 10695 10697 10696 IOMMU SUBSYSTEM
-1
arch/arm64/Kconfig
··· 209 209 select HAVE_KPROBES 210 210 select HAVE_KRETPROBES 211 211 select HAVE_GENERIC_VDSO 212 - select IOMMU_DMA if IOMMU_SUPPORT 213 212 select IRQ_DOMAIN 214 213 select IRQ_FORCED_THREADING 215 214 select KASAN_VMALLOC if KASAN
+1 -1
arch/arm64/mm/dma-mapping.c
··· 7 7 #include <linux/gfp.h> 8 8 #include <linux/cache.h> 9 9 #include <linux/dma-map-ops.h> 10 - #include <linux/dma-iommu.h> 10 + #include <linux/iommu.h> 11 11 #include <xen/xen.h> 12 12 13 13 #include <asm/cacheflush.h>
-1
drivers/acpi/viot.c
··· 19 19 #define pr_fmt(fmt) "ACPI: VIOT: " fmt 20 20 21 21 #include <linux/acpi_viot.h> 22 - #include <linux/dma-iommu.h> 23 22 #include <linux/fwnode.h> 24 23 #include <linux/iommu.h> 25 24 #include <linux/list.h>
-1
drivers/gpu/drm/exynos/exynos_drm_dma.c
··· 4 4 // Author: Inki Dae <inki.dae@samsung.com> 5 5 // Author: Andrzej Hajda <a.hajda@samsung.com> 6 6 7 - #include <linux/dma-iommu.h> 8 7 #include <linux/dma-map-ops.h> 9 8 #include <linux/iommu.h> 10 9 #include <linux/platform_device.h>
+13 -3
drivers/iommu/Kconfig
··· 67 67 68 68 If unsure, say N here. 69 69 70 + config IOMMU_IO_PGTABLE_DART 71 + bool "Apple DART Formats" 72 + select IOMMU_IO_PGTABLE 73 + depends on ARM64 || (COMPILE_TEST && !GENERIC_ATOMIC64) 74 + help 75 + Enable support for the Apple DART pagetable formats. These include 76 + the t8020 and t6000/t8110 DART formats used in Apple M1/M2 family 77 + SoCs. 78 + 79 + If unsure, say N here. 80 + 70 81 endmenu 71 82 72 83 config IOMMU_DEBUGFS ··· 148 137 149 138 # IOMMU-agnostic DMA-mapping layer 150 139 config IOMMU_DMA 151 - bool 140 + def_bool ARM64 || IA64 || X86 152 141 select DMA_OPS 153 142 select IOMMU_API 154 143 select IOMMU_IOVA ··· 305 294 tristate "Apple DART IOMMU Support" 306 295 depends on ARCH_APPLE || (COMPILE_TEST && !GENERIC_ATOMIC64) 307 296 select IOMMU_API 308 - select IOMMU_IO_PGTABLE_LPAE 297 + select IOMMU_IO_PGTABLE_DART 309 298 default ARCH_APPLE 310 299 help 311 300 Support for Apple DART (Device Address Resolution Table) IOMMUs ··· 487 476 depends on VIRTIO 488 477 depends on (ARM64 || X86) 489 478 select IOMMU_API 490 - select IOMMU_DMA 491 479 select INTERVAL_TREE 492 480 select ACPI_VIOT if ACPI 493 481 help
+1
drivers/iommu/Makefile
··· 8 8 obj-$(CONFIG_IOMMU_IO_PGTABLE) += io-pgtable.o 9 9 obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o 10 10 obj-$(CONFIG_IOMMU_IO_PGTABLE_LPAE) += io-pgtable-arm.o 11 + obj-$(CONFIG_IOMMU_IO_PGTABLE_DART) += io-pgtable-dart.o 11 12 obj-$(CONFIG_IOASID) += ioasid.o 12 13 obj-$(CONFIG_IOMMU_IOVA) += iova.o 13 14 obj-$(CONFIG_OF_IOMMU) += of_iommu.o
-1
drivers/iommu/amd/Kconfig
··· 9 9 select PCI_PASID 10 10 select IOMMU_API 11 11 select IOMMU_IOVA 12 - select IOMMU_DMA 13 12 select IOMMU_IO_PGTABLE 14 13 depends on X86_64 && PCI && ACPI && HAVE_CMPXCHG_DOUBLE 15 14 help
+1 -1
drivers/iommu/amd/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 - obj-$(CONFIG_AMD_IOMMU) += iommu.o init.o quirks.o io_pgtable.o 2 + obj-$(CONFIG_AMD_IOMMU) += iommu.o init.o quirks.o io_pgtable.o io_pgtable_v2.o 3 3 obj-$(CONFIG_AMD_IOMMU_DEBUGFS) += debugfs.o 4 4 obj-$(CONFIG_AMD_IOMMU_V2) += iommu_v2.o
-1
drivers/iommu/amd/amd_iommu.h
··· 18 18 extern int amd_iommu_init_devices(void); 19 19 extern void amd_iommu_uninit_devices(void); 20 20 extern void amd_iommu_init_notifier(void); 21 - extern int amd_iommu_init_api(void); 22 21 extern void amd_iommu_set_rlookup_table(struct amd_iommu *iommu, u16 devid); 23 22 24 23 #ifdef CONFIG_AMD_IOMMU_DEBUGFS
+9 -1
drivers/iommu/amd/amd_iommu_types.h
··· 94 94 #define FEATURE_HE (1ULL<<8) 95 95 #define FEATURE_PC (1ULL<<9) 96 96 #define FEATURE_GAM_VAPIC (1ULL<<21) 97 + #define FEATURE_GIOSUP (1ULL<<48) 97 98 #define FEATURE_EPHSUP (1ULL<<50) 98 99 #define FEATURE_SNP (1ULL<<63) 99 100 ··· 277 276 * 512GB Pages are not supported due to a hardware bug 278 277 */ 279 278 #define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38)) 279 + /* 4K, 2MB, 1G page sizes are supported */ 280 + #define AMD_IOMMU_PGSIZES_V2 (PAGE_SIZE | (1ULL << 21) | (1ULL << 30)) 280 281 281 282 /* Bit value definition for dte irq remapping fields*/ 282 283 #define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6) ··· 379 376 #define DTE_FLAG_IW (1ULL << 62) 380 377 381 378 #define DTE_FLAG_IOTLB (1ULL << 32) 379 + #define DTE_FLAG_GIOV (1ULL << 54) 382 380 #define DTE_FLAG_GV (1ULL << 55) 383 381 #define DTE_FLAG_MASK (0x3ffULL << 32) 384 382 #define DTE_GLX_SHIFT (56) ··· 438 434 #define PD_PASSTHROUGH_MASK (1UL << 2) /* domain has no page 439 435 translation */ 440 436 #define PD_IOMMUV2_MASK (1UL << 3) /* domain has gcr3 table */ 437 + #define PD_GIOV_MASK (1UL << 4) /* domain enable GIOV support */ 441 438 442 439 extern bool amd_iommu_dump; 443 440 #define DUMP_printk(format, arg...) \ ··· 460 455 461 456 /* Interrupt remapping feature used? */ 462 457 extern bool amd_iommu_irq_remap; 458 + 459 + extern const struct iommu_ops amd_iommu_ops; 463 460 464 461 /* IVRS indicates that pre-boot remapping was enabled */ 465 462 extern bool amdr_ivrs_remap_support; ··· 533 526 struct io_pgtable iop; 534 527 int mode; 535 528 u64 *root; 536 - atomic64_t pt_root; /* pgtable root and pgtable mode */ 529 + atomic64_t pt_root; /* pgtable root and pgtable mode */ 530 + u64 *pgd; /* v2 pgtable pgd pointer */ 537 531 }; 538 532 539 533 /*
+32 -15
drivers/iommu/amd/init.c
··· 95 95 * out of it. 96 96 */ 97 97 98 - extern const struct iommu_ops amd_iommu_ops; 99 - 100 98 /* 101 99 * structure describing one IOMMU in the ACPI table. Typically followed by one 102 100 * or more ivhd_entrys. ··· 2066 2068 2067 2069 init_iommu_perf_ctr(iommu); 2068 2070 2071 + if (amd_iommu_pgtable == AMD_IOMMU_V2) { 2072 + if (!iommu_feature(iommu, FEATURE_GIOSUP) || 2073 + !iommu_feature(iommu, FEATURE_GT)) { 2074 + pr_warn("Cannot enable v2 page table for DMA-API. Fallback to v1.\n"); 2075 + amd_iommu_pgtable = AMD_IOMMU_V1; 2076 + } else if (iommu_default_passthrough()) { 2077 + pr_warn("V2 page table doesn't support passthrough mode. Fallback to v1.\n"); 2078 + amd_iommu_pgtable = AMD_IOMMU_V1; 2079 + } 2080 + } 2081 + 2069 2082 if (is_rd890_iommu(iommu->dev)) { 2070 2083 int i, j; 2071 2084 ··· 2155 2146 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE) 2156 2147 pr_info("X2APIC enabled\n"); 2157 2148 } 2149 + if (amd_iommu_pgtable == AMD_IOMMU_V2) 2150 + pr_info("V2 page table enabled\n"); 2158 2151 } 2159 2152 2160 2153 static int __init amd_iommu_init_pci(void) ··· 2179 2168 /* 2180 2169 * Order is important here to make sure any unity map requirements are 2181 2170 * fulfilled. The unity mappings are created and written to the device 2182 - * table during the amd_iommu_init_api() call. 2171 + * table during the iommu_init_pci() call. 2183 2172 * 2184 2173 * After that we call init_device_table_dma() to make sure any 2185 2174 * uninitialized DTE will block DMA, and in the end we flush the caches 2186 2175 * of all IOMMUs to make sure the changes to the device table are 2187 2176 * active. 2188 2177 */ 2189 - ret = amd_iommu_init_api(); 2190 - if (ret) { 2191 - pr_err("IOMMU: Failed to initialize IOMMU-API interface (error=%d)!\n", 2192 - ret); 2193 - goto out; 2194 - } 2195 - 2196 2178 for_each_pci_segment(pci_seg) 2197 2179 init_device_table_dma(pci_seg); 2198 2180 ··· 3370 3366 3371 3367 static int __init parse_amd_iommu_options(char *str) 3372 3368 { 3373 - for (; *str; ++str) { 3369 + if (!str) 3370 + return -EINVAL; 3371 + 3372 + while (*str) { 3374 3373 if (strncmp(str, "fullflush", 9) == 0) { 3375 3374 pr_warn("amd_iommu=fullflush deprecated; use iommu.strict=1 instead\n"); 3376 3375 iommu_set_dma_strict(); 3377 - } 3378 - if (strncmp(str, "force_enable", 12) == 0) 3376 + } else if (strncmp(str, "force_enable", 12) == 0) { 3379 3377 amd_iommu_force_enable = true; 3380 - if (strncmp(str, "off", 3) == 0) 3378 + } else if (strncmp(str, "off", 3) == 0) { 3381 3379 amd_iommu_disabled = true; 3382 - if (strncmp(str, "force_isolation", 15) == 0) 3380 + } else if (strncmp(str, "force_isolation", 15) == 0) { 3383 3381 amd_iommu_force_isolation = true; 3382 + } else if (strncmp(str, "pgtbl_v1", 8) == 0) { 3383 + amd_iommu_pgtable = AMD_IOMMU_V1; 3384 + } else if (strncmp(str, "pgtbl_v2", 8) == 0) { 3385 + amd_iommu_pgtable = AMD_IOMMU_V2; 3386 + } else { 3387 + pr_notice("Unknown option - '%s'\n", str); 3388 + } 3389 + 3390 + str += strcspn(str, ","); 3391 + while (*str == ',') 3392 + str++; 3384 3393 } 3385 3394 3386 3395 return 1;
+43 -33
drivers/iommu/amd/io_pgtable.c
··· 360 360 * supporting all features of AMD IOMMU page tables like level skipping 361 361 * and full 64 bit address spaces. 362 362 */ 363 - static int iommu_v1_map_page(struct io_pgtable_ops *ops, unsigned long iova, 364 - phys_addr_t paddr, size_t size, int prot, gfp_t gfp) 363 + static int iommu_v1_map_pages(struct io_pgtable_ops *ops, unsigned long iova, 364 + phys_addr_t paddr, size_t pgsize, size_t pgcount, 365 + int prot, gfp_t gfp, size_t *mapped) 365 366 { 366 367 struct protection_domain *dom = io_pgtable_ops_to_domain(ops); 367 368 LIST_HEAD(freelist); ··· 370 369 u64 __pte, *pte; 371 370 int ret, i, count; 372 371 373 - BUG_ON(!IS_ALIGNED(iova, size)); 374 - BUG_ON(!IS_ALIGNED(paddr, size)); 372 + BUG_ON(!IS_ALIGNED(iova, pgsize)); 373 + BUG_ON(!IS_ALIGNED(paddr, pgsize)); 375 374 376 375 ret = -EINVAL; 377 376 if (!(prot & IOMMU_PROT_MASK)) 378 377 goto out; 379 378 380 - count = PAGE_SIZE_PTE_COUNT(size); 381 - pte = alloc_pte(dom, iova, size, NULL, gfp, &updated); 379 + while (pgcount > 0) { 380 + count = PAGE_SIZE_PTE_COUNT(pgsize); 381 + pte = alloc_pte(dom, iova, pgsize, NULL, gfp, &updated); 382 382 383 - ret = -ENOMEM; 384 - if (!pte) 385 - goto out; 383 + ret = -ENOMEM; 384 + if (!pte) 385 + goto out; 386 386 387 - for (i = 0; i < count; ++i) 388 - free_clear_pte(&pte[i], pte[i], &freelist); 387 + for (i = 0; i < count; ++i) 388 + free_clear_pte(&pte[i], pte[i], &freelist); 389 389 390 - if (!list_empty(&freelist)) 391 - updated = true; 390 + if (!list_empty(&freelist)) 391 + updated = true; 392 392 393 - if (count > 1) { 394 - __pte = PAGE_SIZE_PTE(__sme_set(paddr), size); 395 - __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_PR | IOMMU_PTE_FC; 396 - } else 397 - __pte = __sme_set(paddr) | IOMMU_PTE_PR | IOMMU_PTE_FC; 393 + if (count > 1) { 394 + __pte = PAGE_SIZE_PTE(__sme_set(paddr), pgsize); 395 + __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_PR | IOMMU_PTE_FC; 396 + } else 397 + __pte = __sme_set(paddr) | IOMMU_PTE_PR | IOMMU_PTE_FC; 398 398 399 - if (prot & IOMMU_PROT_IR) 400 - __pte |= IOMMU_PTE_IR; 401 - if (prot & IOMMU_PROT_IW) 402 - __pte |= IOMMU_PTE_IW; 399 + if (prot & IOMMU_PROT_IR) 400 + __pte |= IOMMU_PTE_IR; 401 + if (prot & IOMMU_PROT_IW) 402 + __pte |= IOMMU_PTE_IW; 403 403 404 - for (i = 0; i < count; ++i) 405 - pte[i] = __pte; 404 + for (i = 0; i < count; ++i) 405 + pte[i] = __pte; 406 + 407 + iova += pgsize; 408 + paddr += pgsize; 409 + pgcount--; 410 + if (mapped) 411 + *mapped += pgsize; 412 + } 406 413 407 414 ret = 0; 408 415 ··· 435 426 return ret; 436 427 } 437 428 438 - static unsigned long iommu_v1_unmap_page(struct io_pgtable_ops *ops, 439 - unsigned long iova, 440 - size_t size, 441 - struct iommu_iotlb_gather *gather) 429 + static unsigned long iommu_v1_unmap_pages(struct io_pgtable_ops *ops, 430 + unsigned long iova, 431 + size_t pgsize, size_t pgcount, 432 + struct iommu_iotlb_gather *gather) 442 433 { 443 434 struct amd_io_pgtable *pgtable = io_pgtable_ops_to_data(ops); 444 435 unsigned long long unmapped; 445 436 unsigned long unmap_size; 446 437 u64 *pte; 438 + size_t size = pgcount << __ffs(pgsize); 447 439 448 - BUG_ON(!is_power_of_2(size)); 440 + BUG_ON(!is_power_of_2(pgsize)); 449 441 450 442 unmapped = 0; 451 443 ··· 458 448 count = PAGE_SIZE_PTE_COUNT(unmap_size); 459 449 for (i = 0; i < count; i++) 460 450 pte[i] = 0ULL; 451 + } else { 452 + return unmapped; 461 453 } 462 454 463 455 iova = (iova & ~(unmap_size - 1)) + unmap_size; 464 456 unmapped += unmap_size; 465 457 } 466 - 467 - BUG_ON(unmapped && !is_power_of_2(unmapped)); 468 458 469 459 return unmapped; 470 460 } ··· 524 514 cfg->oas = IOMMU_OUT_ADDR_BIT_SIZE, 525 515 cfg->tlb = &v1_flush_ops; 526 516 527 - pgtable->iop.ops.map = iommu_v1_map_page; 528 - pgtable->iop.ops.unmap = iommu_v1_unmap_page; 517 + pgtable->iop.ops.map_pages = iommu_v1_map_pages; 518 + pgtable->iop.ops.unmap_pages = iommu_v1_unmap_pages; 529 519 pgtable->iop.ops.iova_to_phys = iommu_v1_iova_to_phys; 530 520 531 521 return &pgtable->iop;
+415
drivers/iommu/amd/io_pgtable_v2.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * CPU-agnostic AMD IO page table v2 allocator. 4 + * 5 + * Copyright (C) 2022 Advanced Micro Devices, Inc. 6 + * Author: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> 7 + * Author: Vasant Hegde <vasant.hegde@amd.com> 8 + */ 9 + 10 + #define pr_fmt(fmt) "AMD-Vi: " fmt 11 + #define dev_fmt(fmt) pr_fmt(fmt) 12 + 13 + #include <linux/bitops.h> 14 + #include <linux/io-pgtable.h> 15 + #include <linux/kernel.h> 16 + 17 + #include <asm/barrier.h> 18 + 19 + #include "amd_iommu_types.h" 20 + #include "amd_iommu.h" 21 + 22 + #define IOMMU_PAGE_PRESENT BIT_ULL(0) /* Is present */ 23 + #define IOMMU_PAGE_RW BIT_ULL(1) /* Writeable */ 24 + #define IOMMU_PAGE_USER BIT_ULL(2) /* Userspace addressable */ 25 + #define IOMMU_PAGE_PWT BIT_ULL(3) /* Page write through */ 26 + #define IOMMU_PAGE_PCD BIT_ULL(4) /* Page cache disabled */ 27 + #define IOMMU_PAGE_ACCESS BIT_ULL(5) /* Was accessed (updated by IOMMU) */ 28 + #define IOMMU_PAGE_DIRTY BIT_ULL(6) /* Was written to (updated by IOMMU) */ 29 + #define IOMMU_PAGE_PSE BIT_ULL(7) /* Page Size Extensions */ 30 + #define IOMMU_PAGE_NX BIT_ULL(63) /* No execute */ 31 + 32 + #define MAX_PTRS_PER_PAGE 512 33 + 34 + #define IOMMU_PAGE_SIZE_2M BIT_ULL(21) 35 + #define IOMMU_PAGE_SIZE_1G BIT_ULL(30) 36 + 37 + 38 + static inline int get_pgtable_level(void) 39 + { 40 + /* 5 level page table is not supported */ 41 + return PAGE_MODE_4_LEVEL; 42 + } 43 + 44 + static inline bool is_large_pte(u64 pte) 45 + { 46 + return (pte & IOMMU_PAGE_PSE); 47 + } 48 + 49 + static inline void *alloc_pgtable_page(void) 50 + { 51 + return (void *)get_zeroed_page(GFP_KERNEL); 52 + } 53 + 54 + static inline u64 set_pgtable_attr(u64 *page) 55 + { 56 + u64 prot; 57 + 58 + prot = IOMMU_PAGE_PRESENT | IOMMU_PAGE_RW | IOMMU_PAGE_USER; 59 + prot |= IOMMU_PAGE_ACCESS | IOMMU_PAGE_DIRTY; 60 + 61 + return (iommu_virt_to_phys(page) | prot); 62 + } 63 + 64 + static inline void *get_pgtable_pte(u64 pte) 65 + { 66 + return iommu_phys_to_virt(pte & PM_ADDR_MASK); 67 + } 68 + 69 + static u64 set_pte_attr(u64 paddr, u64 pg_size, int prot) 70 + { 71 + u64 pte; 72 + 73 + pte = __sme_set(paddr & PM_ADDR_MASK); 74 + pte |= IOMMU_PAGE_PRESENT | IOMMU_PAGE_USER; 75 + pte |= IOMMU_PAGE_ACCESS | IOMMU_PAGE_DIRTY; 76 + 77 + if (prot & IOMMU_PROT_IW) 78 + pte |= IOMMU_PAGE_RW; 79 + 80 + /* Large page */ 81 + if (pg_size == IOMMU_PAGE_SIZE_1G || pg_size == IOMMU_PAGE_SIZE_2M) 82 + pte |= IOMMU_PAGE_PSE; 83 + 84 + return pte; 85 + } 86 + 87 + static inline u64 get_alloc_page_size(u64 size) 88 + { 89 + if (size >= IOMMU_PAGE_SIZE_1G) 90 + return IOMMU_PAGE_SIZE_1G; 91 + 92 + if (size >= IOMMU_PAGE_SIZE_2M) 93 + return IOMMU_PAGE_SIZE_2M; 94 + 95 + return PAGE_SIZE; 96 + } 97 + 98 + static inline int page_size_to_level(u64 pg_size) 99 + { 100 + if (pg_size == IOMMU_PAGE_SIZE_1G) 101 + return PAGE_MODE_3_LEVEL; 102 + if (pg_size == IOMMU_PAGE_SIZE_2M) 103 + return PAGE_MODE_2_LEVEL; 104 + 105 + return PAGE_MODE_1_LEVEL; 106 + } 107 + 108 + static inline void free_pgtable_page(u64 *pt) 109 + { 110 + free_page((unsigned long)pt); 111 + } 112 + 113 + static void free_pgtable(u64 *pt, int level) 114 + { 115 + u64 *p; 116 + int i; 117 + 118 + for (i = 0; i < MAX_PTRS_PER_PAGE; i++) { 119 + /* PTE present? */ 120 + if (!IOMMU_PTE_PRESENT(pt[i])) 121 + continue; 122 + 123 + if (is_large_pte(pt[i])) 124 + continue; 125 + 126 + /* 127 + * Free the next level. No need to look at l1 tables here since 128 + * they can only contain leaf PTEs; just free them directly. 129 + */ 130 + p = get_pgtable_pte(pt[i]); 131 + if (level > 2) 132 + free_pgtable(p, level - 1); 133 + else 134 + free_pgtable_page(p); 135 + } 136 + 137 + free_pgtable_page(pt); 138 + } 139 + 140 + /* Allocate page table */ 141 + static u64 *v2_alloc_pte(u64 *pgd, unsigned long iova, 142 + unsigned long pg_size, bool *updated) 143 + { 144 + u64 *pte, *page; 145 + int level, end_level; 146 + 147 + level = get_pgtable_level() - 1; 148 + end_level = page_size_to_level(pg_size); 149 + pte = &pgd[PM_LEVEL_INDEX(level, iova)]; 150 + iova = PAGE_SIZE_ALIGN(iova, PAGE_SIZE); 151 + 152 + while (level >= end_level) { 153 + u64 __pte, __npte; 154 + 155 + __pte = *pte; 156 + 157 + if (IOMMU_PTE_PRESENT(__pte) && is_large_pte(__pte)) { 158 + /* Unmap large pte */ 159 + cmpxchg64(pte, *pte, 0ULL); 160 + *updated = true; 161 + continue; 162 + } 163 + 164 + if (!IOMMU_PTE_PRESENT(__pte)) { 165 + page = alloc_pgtable_page(); 166 + if (!page) 167 + return NULL; 168 + 169 + __npte = set_pgtable_attr(page); 170 + /* pte could have been changed somewhere. */ 171 + if (cmpxchg64(pte, __pte, __npte) != __pte) 172 + free_pgtable_page(page); 173 + else if (IOMMU_PTE_PRESENT(__pte)) 174 + *updated = true; 175 + 176 + continue; 177 + } 178 + 179 + level -= 1; 180 + pte = get_pgtable_pte(__pte); 181 + pte = &pte[PM_LEVEL_INDEX(level, iova)]; 182 + } 183 + 184 + /* Tear down existing pte entries */ 185 + if (IOMMU_PTE_PRESENT(*pte)) { 186 + u64 *__pte; 187 + 188 + *updated = true; 189 + __pte = get_pgtable_pte(*pte); 190 + cmpxchg64(pte, *pte, 0ULL); 191 + if (pg_size == IOMMU_PAGE_SIZE_1G) 192 + free_pgtable(__pte, end_level - 1); 193 + else if (pg_size == IOMMU_PAGE_SIZE_2M) 194 + free_pgtable_page(__pte); 195 + } 196 + 197 + return pte; 198 + } 199 + 200 + /* 201 + * This function checks if there is a PTE for a given dma address. 202 + * If there is one, it returns the pointer to it. 203 + */ 204 + static u64 *fetch_pte(struct amd_io_pgtable *pgtable, 205 + unsigned long iova, unsigned long *page_size) 206 + { 207 + u64 *pte; 208 + int level; 209 + 210 + level = get_pgtable_level() - 1; 211 + pte = &pgtable->pgd[PM_LEVEL_INDEX(level, iova)]; 212 + /* Default page size is 4K */ 213 + *page_size = PAGE_SIZE; 214 + 215 + while (level) { 216 + /* Not present */ 217 + if (!IOMMU_PTE_PRESENT(*pte)) 218 + return NULL; 219 + 220 + /* Walk to the next level */ 221 + pte = get_pgtable_pte(*pte); 222 + pte = &pte[PM_LEVEL_INDEX(level - 1, iova)]; 223 + 224 + /* Large page */ 225 + if (is_large_pte(*pte)) { 226 + if (level == PAGE_MODE_3_LEVEL) 227 + *page_size = IOMMU_PAGE_SIZE_1G; 228 + else if (level == PAGE_MODE_2_LEVEL) 229 + *page_size = IOMMU_PAGE_SIZE_2M; 230 + else 231 + return NULL; /* Wrongly set PSE bit in PTE */ 232 + 233 + break; 234 + } 235 + 236 + level -= 1; 237 + } 238 + 239 + return pte; 240 + } 241 + 242 + static int iommu_v2_map_pages(struct io_pgtable_ops *ops, unsigned long iova, 243 + phys_addr_t paddr, size_t pgsize, size_t pgcount, 244 + int prot, gfp_t gfp, size_t *mapped) 245 + { 246 + struct protection_domain *pdom = io_pgtable_ops_to_domain(ops); 247 + struct io_pgtable_cfg *cfg = &pdom->iop.iop.cfg; 248 + u64 *pte; 249 + unsigned long map_size; 250 + unsigned long mapped_size = 0; 251 + unsigned long o_iova = iova; 252 + size_t size = pgcount << __ffs(pgsize); 253 + int count = 0; 254 + int ret = 0; 255 + bool updated = false; 256 + 257 + if (WARN_ON(!pgsize || (pgsize & cfg->pgsize_bitmap) != pgsize) || !pgcount) 258 + return -EINVAL; 259 + 260 + if (!(prot & IOMMU_PROT_MASK)) 261 + return -EINVAL; 262 + 263 + while (mapped_size < size) { 264 + map_size = get_alloc_page_size(pgsize); 265 + pte = v2_alloc_pte(pdom->iop.pgd, iova, map_size, &updated); 266 + if (!pte) { 267 + ret = -EINVAL; 268 + goto out; 269 + } 270 + 271 + *pte = set_pte_attr(paddr, map_size, prot); 272 + 273 + count++; 274 + iova += map_size; 275 + paddr += map_size; 276 + mapped_size += map_size; 277 + } 278 + 279 + out: 280 + if (updated) { 281 + if (count > 1) 282 + amd_iommu_flush_tlb(&pdom->domain, 0); 283 + else 284 + amd_iommu_flush_page(&pdom->domain, 0, o_iova); 285 + } 286 + 287 + if (mapped) 288 + *mapped += mapped_size; 289 + 290 + return ret; 291 + } 292 + 293 + static unsigned long iommu_v2_unmap_pages(struct io_pgtable_ops *ops, 294 + unsigned long iova, 295 + size_t pgsize, size_t pgcount, 296 + struct iommu_iotlb_gather *gather) 297 + { 298 + struct amd_io_pgtable *pgtable = io_pgtable_ops_to_data(ops); 299 + struct io_pgtable_cfg *cfg = &pgtable->iop.cfg; 300 + unsigned long unmap_size; 301 + unsigned long unmapped = 0; 302 + size_t size = pgcount << __ffs(pgsize); 303 + u64 *pte; 304 + 305 + if (WARN_ON(!pgsize || (pgsize & cfg->pgsize_bitmap) != pgsize || !pgcount)) 306 + return 0; 307 + 308 + while (unmapped < size) { 309 + pte = fetch_pte(pgtable, iova, &unmap_size); 310 + if (!pte) 311 + return unmapped; 312 + 313 + *pte = 0ULL; 314 + 315 + iova = (iova & ~(unmap_size - 1)) + unmap_size; 316 + unmapped += unmap_size; 317 + } 318 + 319 + return unmapped; 320 + } 321 + 322 + static phys_addr_t iommu_v2_iova_to_phys(struct io_pgtable_ops *ops, unsigned long iova) 323 + { 324 + struct amd_io_pgtable *pgtable = io_pgtable_ops_to_data(ops); 325 + unsigned long offset_mask, pte_pgsize; 326 + u64 *pte, __pte; 327 + 328 + pte = fetch_pte(pgtable, iova, &pte_pgsize); 329 + if (!pte || !IOMMU_PTE_PRESENT(*pte)) 330 + return 0; 331 + 332 + offset_mask = pte_pgsize - 1; 333 + __pte = __sme_clr(*pte & PM_ADDR_MASK); 334 + 335 + return (__pte & ~offset_mask) | (iova & offset_mask); 336 + } 337 + 338 + /* 339 + * ---------------------------------------------------- 340 + */ 341 + static void v2_tlb_flush_all(void *cookie) 342 + { 343 + } 344 + 345 + static void v2_tlb_flush_walk(unsigned long iova, size_t size, 346 + size_t granule, void *cookie) 347 + { 348 + } 349 + 350 + static void v2_tlb_add_page(struct iommu_iotlb_gather *gather, 351 + unsigned long iova, size_t granule, 352 + void *cookie) 353 + { 354 + } 355 + 356 + static const struct iommu_flush_ops v2_flush_ops = { 357 + .tlb_flush_all = v2_tlb_flush_all, 358 + .tlb_flush_walk = v2_tlb_flush_walk, 359 + .tlb_add_page = v2_tlb_add_page, 360 + }; 361 + 362 + static void v2_free_pgtable(struct io_pgtable *iop) 363 + { 364 + struct protection_domain *pdom; 365 + struct amd_io_pgtable *pgtable = container_of(iop, struct amd_io_pgtable, iop); 366 + 367 + pdom = container_of(pgtable, struct protection_domain, iop); 368 + if (!(pdom->flags & PD_IOMMUV2_MASK)) 369 + return; 370 + 371 + /* 372 + * Make changes visible to IOMMUs. No need to clear gcr3 entry 373 + * as gcr3 table is already freed. 374 + */ 375 + amd_iommu_domain_update(pdom); 376 + 377 + /* Free page table */ 378 + free_pgtable(pgtable->pgd, get_pgtable_level()); 379 + } 380 + 381 + static struct io_pgtable *v2_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) 382 + { 383 + struct amd_io_pgtable *pgtable = io_pgtable_cfg_to_data(cfg); 384 + struct protection_domain *pdom = (struct protection_domain *)cookie; 385 + int ret; 386 + 387 + pgtable->pgd = alloc_pgtable_page(); 388 + if (!pgtable->pgd) 389 + return NULL; 390 + 391 + ret = amd_iommu_domain_set_gcr3(&pdom->domain, 0, iommu_virt_to_phys(pgtable->pgd)); 392 + if (ret) 393 + goto err_free_pgd; 394 + 395 + pgtable->iop.ops.map_pages = iommu_v2_map_pages; 396 + pgtable->iop.ops.unmap_pages = iommu_v2_unmap_pages; 397 + pgtable->iop.ops.iova_to_phys = iommu_v2_iova_to_phys; 398 + 399 + cfg->pgsize_bitmap = AMD_IOMMU_PGSIZES_V2, 400 + cfg->ias = IOMMU_IN_ADDR_BIT_SIZE, 401 + cfg->oas = IOMMU_OUT_ADDR_BIT_SIZE, 402 + cfg->tlb = &v2_flush_ops; 403 + 404 + return &pgtable->iop; 405 + 406 + err_free_pgd: 407 + free_pgtable_page(pgtable->pgd); 408 + 409 + return NULL; 410 + } 411 + 412 + struct io_pgtable_init_fns io_pgtable_amd_iommu_v2_init_fns = { 413 + .alloc = v2_alloc_pgtable, 414 + .free = v2_free_pgtable, 415 + };
+97 -67
drivers/iommu/amd/iommu.c
··· 11 11 #include <linux/ratelimit.h> 12 12 #include <linux/pci.h> 13 13 #include <linux/acpi.h> 14 - #include <linux/amba/bus.h> 15 - #include <linux/platform_device.h> 16 14 #include <linux/pci-ats.h> 17 15 #include <linux/bitmap.h> 18 16 #include <linux/slab.h> ··· 18 20 #include <linux/scatterlist.h> 19 21 #include <linux/dma-map-ops.h> 20 22 #include <linux/dma-direct.h> 21 - #include <linux/dma-iommu.h> 22 23 #include <linux/iommu-helper.h> 23 24 #include <linux/delay.h> 24 25 #include <linux/amd-iommu.h> ··· 39 42 #include <asm/dma.h> 40 43 41 44 #include "amd_iommu.h" 45 + #include "../dma-iommu.h" 42 46 #include "../irq_remapping.h" 43 47 44 48 #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28)) ··· 64 66 LIST_HEAD(hpet_map); 65 67 LIST_HEAD(acpihid_map); 66 68 67 - /* 68 - * Domain for untranslated devices - only allocated 69 - * if iommu=pt passed on kernel cmd line. 70 - */ 71 69 const struct iommu_ops amd_iommu_ops; 72 70 73 71 static ATOMIC_NOTIFIER_HEAD(ppr_notifier); ··· 79 85 struct kmem_cache *amd_iommu_irq_cache; 80 86 81 87 static void detach_device(struct device *dev); 88 + static int domain_enable_v2(struct protection_domain *domain, int pasids); 82 89 83 90 /**************************************************************************** 84 91 * ··· 1592 1597 1593 1598 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C; 1594 1599 flags |= tmp; 1600 + 1601 + if (domain->flags & PD_GIOV_MASK) 1602 + pte_root |= DTE_FLAG_GIOV; 1595 1603 } 1596 1604 1597 1605 flags &= ~DEV_DOMID_MASK; ··· 1648 1650 domain->dev_iommu[iommu->index] += 1; 1649 1651 domain->dev_cnt += 1; 1650 1652 1653 + /* Override supported page sizes */ 1654 + if (domain->flags & PD_GIOV_MASK) 1655 + domain->domain.pgsize_bitmap = AMD_IOMMU_PGSIZES_V2; 1656 + 1651 1657 /* Update device table */ 1652 1658 set_dte_entry(iommu, dev_data->devid, domain, 1653 1659 ats, dev_data->iommu_v2); ··· 1696 1694 pci_disable_pasid(pdev); 1697 1695 } 1698 1696 1699 - static int pdev_iommuv2_enable(struct pci_dev *pdev) 1697 + static int pdev_pri_ats_enable(struct pci_dev *pdev) 1700 1698 { 1701 1699 int ret; 1702 1700 ··· 1759 1757 struct iommu_domain *def_domain = iommu_get_dma_domain(dev); 1760 1758 1761 1759 ret = -EINVAL; 1762 - if (def_domain->type != IOMMU_DOMAIN_IDENTITY) 1760 + 1761 + /* 1762 + * In case of using AMD_IOMMU_V1 page table mode and the device 1763 + * is enabling for PPR/ATS support (using v2 table), 1764 + * we need to make sure that the domain type is identity map. 1765 + */ 1766 + if ((amd_iommu_pgtable == AMD_IOMMU_V1) && 1767 + def_domain->type != IOMMU_DOMAIN_IDENTITY) { 1763 1768 goto out; 1769 + } 1764 1770 1765 1771 if (dev_data->iommu_v2) { 1766 - if (pdev_iommuv2_enable(pdev) != 0) 1772 + if (pdev_pri_ats_enable(pdev) != 0) 1767 1773 goto out; 1768 1774 1769 1775 dev_data->ats.enabled = true; ··· 1862 1852 if (!iommu) 1863 1853 return ERR_PTR(-ENODEV); 1864 1854 1855 + /* Not registered yet? */ 1856 + if (!iommu->iommu.ops) 1857 + return ERR_PTR(-ENODEV); 1858 + 1865 1859 if (dev_iommu_priv_get(dev)) 1866 1860 return &iommu->iommu; 1867 1861 ··· 1952 1938 amd_iommu_domain_flush_complete(domain); 1953 1939 } 1954 1940 1955 - int __init amd_iommu_init_api(void) 1956 - { 1957 - int err; 1958 - 1959 - err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops); 1960 - if (err) 1961 - return err; 1962 - #ifdef CONFIG_ARM_AMBA 1963 - err = bus_set_iommu(&amba_bustype, &amd_iommu_ops); 1964 - if (err) 1965 - return err; 1966 - #endif 1967 - err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops); 1968 - if (err) 1969 - return err; 1970 - 1971 - return 0; 1972 - } 1973 - 1974 1941 /***************************************************************************** 1975 1942 * 1976 1943 * The following functions belong to the exported interface of AMD IOMMU ··· 1984 1989 if (!domain) 1985 1990 return; 1986 1991 1987 - if (domain->id) 1988 - domain_id_free(domain->id); 1989 - 1990 1992 if (domain->iop.pgtbl_cfg.tlb) 1991 1993 free_io_pgtable_ops(&domain->iop.iop.ops); 1994 + 1995 + if (domain->id) 1996 + domain_id_free(domain->id); 1992 1997 1993 1998 kfree(domain); 1994 1999 } ··· 2007 2012 2008 2013 if (mode != PAGE_MODE_NONE) { 2009 2014 pt_root = (void *)get_zeroed_page(GFP_KERNEL); 2010 - if (!pt_root) 2015 + if (!pt_root) { 2016 + domain_id_free(domain->id); 2011 2017 return -ENOMEM; 2018 + } 2012 2019 } 2013 2020 2014 2021 amd_iommu_domain_set_pgtable(domain, pt_root, mode); 2022 + 2023 + return 0; 2024 + } 2025 + 2026 + static int protection_domain_init_v2(struct protection_domain *domain) 2027 + { 2028 + spin_lock_init(&domain->lock); 2029 + domain->id = domain_id_alloc(); 2030 + if (!domain->id) 2031 + return -ENOMEM; 2032 + INIT_LIST_HEAD(&domain->dev_list); 2033 + 2034 + domain->flags |= PD_GIOV_MASK; 2035 + 2036 + if (domain_enable_v2(domain, 1)) { 2037 + domain_id_free(domain->id); 2038 + return -ENOMEM; 2039 + } 2015 2040 2016 2041 return 0; 2017 2042 } ··· 2063 2048 case AMD_IOMMU_V1: 2064 2049 ret = protection_domain_init_v1(domain, mode); 2065 2050 break; 2051 + case AMD_IOMMU_V2: 2052 + ret = protection_domain_init_v2(domain); 2053 + break; 2066 2054 default: 2067 2055 ret = -EINVAL; 2068 2056 } ··· 2074 2056 goto out_err; 2075 2057 2076 2058 pgtbl_ops = alloc_io_pgtable_ops(pgtable, &domain->iop.pgtbl_cfg, domain); 2077 - if (!pgtbl_ops) 2059 + if (!pgtbl_ops) { 2060 + domain_id_free(domain->id); 2078 2061 goto out_err; 2062 + } 2079 2063 2080 2064 return domain; 2081 2065 out_err: ··· 2195 2175 struct protection_domain *domain = to_pdomain(dom); 2196 2176 struct io_pgtable_ops *ops = &domain->iop.iop.ops; 2197 2177 2198 - if (ops->map) 2178 + if (ops->map_pages) 2199 2179 domain_flush_np_cache(domain, iova, size); 2200 2180 } 2201 2181 2202 - static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova, 2203 - phys_addr_t paddr, size_t page_size, int iommu_prot, 2204 - gfp_t gfp) 2182 + static int amd_iommu_map_pages(struct iommu_domain *dom, unsigned long iova, 2183 + phys_addr_t paddr, size_t pgsize, size_t pgcount, 2184 + int iommu_prot, gfp_t gfp, size_t *mapped) 2205 2185 { 2206 2186 struct protection_domain *domain = to_pdomain(dom); 2207 2187 struct io_pgtable_ops *ops = &domain->iop.iop.ops; ··· 2217 2197 if (iommu_prot & IOMMU_WRITE) 2218 2198 prot |= IOMMU_PROT_IW; 2219 2199 2220 - if (ops->map) 2221 - ret = ops->map(ops, iova, paddr, page_size, prot, gfp); 2200 + if (ops->map_pages) { 2201 + ret = ops->map_pages(ops, iova, paddr, pgsize, 2202 + pgcount, prot, gfp, mapped); 2203 + } 2222 2204 2223 2205 return ret; 2224 2206 } ··· 2246 2224 iommu_iotlb_gather_add_range(gather, iova, size); 2247 2225 } 2248 2226 2249 - static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova, 2250 - size_t page_size, 2251 - struct iommu_iotlb_gather *gather) 2227 + static size_t amd_iommu_unmap_pages(struct iommu_domain *dom, unsigned long iova, 2228 + size_t pgsize, size_t pgcount, 2229 + struct iommu_iotlb_gather *gather) 2252 2230 { 2253 2231 struct protection_domain *domain = to_pdomain(dom); 2254 2232 struct io_pgtable_ops *ops = &domain->iop.iop.ops; ··· 2258 2236 (domain->iop.mode == PAGE_MODE_NONE)) 2259 2237 return 0; 2260 2238 2261 - r = (ops->unmap) ? ops->unmap(ops, iova, page_size, gather) : 0; 2239 + r = (ops->unmap_pages) ? ops->unmap_pages(ops, iova, pgsize, pgcount, NULL) : 0; 2262 2240 2263 - amd_iommu_iotlb_gather_add_page(dom, gather, iova, page_size); 2241 + if (r) 2242 + amd_iommu_iotlb_gather_add_page(dom, gather, iova, r); 2264 2243 2265 2244 return r; 2266 2245 } ··· 2275 2252 return ops->iova_to_phys(ops, iova); 2276 2253 } 2277 2254 2278 - static bool amd_iommu_capable(enum iommu_cap cap) 2255 + static bool amd_iommu_capable(struct device *dev, enum iommu_cap cap) 2279 2256 { 2280 2257 switch (cap) { 2281 2258 case IOMMU_CAP_CACHE_COHERENCY: ··· 2423 2400 .default_domain_ops = &(const struct iommu_domain_ops) { 2424 2401 .attach_dev = amd_iommu_attach_device, 2425 2402 .detach_dev = amd_iommu_detach_device, 2426 - .map = amd_iommu_map, 2427 - .unmap = amd_iommu_unmap, 2403 + .map_pages = amd_iommu_map_pages, 2404 + .unmap_pages = amd_iommu_unmap_pages, 2428 2405 .iotlb_sync_map = amd_iommu_iotlb_sync_map, 2429 2406 .iova_to_phys = amd_iommu_iova_to_phys, 2430 2407 .flush_iotlb_all = amd_iommu_flush_iotlb_all, ··· 2471 2448 } 2472 2449 EXPORT_SYMBOL(amd_iommu_domain_direct_map); 2473 2450 2474 - int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids) 2451 + /* Note: This function expects iommu_domain->lock to be held prior calling the function. */ 2452 + static int domain_enable_v2(struct protection_domain *domain, int pasids) 2475 2453 { 2476 - struct protection_domain *domain = to_pdomain(dom); 2477 - unsigned long flags; 2478 - int levels, ret; 2454 + int levels; 2479 2455 2480 2456 /* Number of GCR3 table levels required */ 2481 2457 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9) ··· 2483 2461 if (levels > amd_iommu_max_glx_val) 2484 2462 return -EINVAL; 2485 2463 2486 - spin_lock_irqsave(&domain->lock, flags); 2464 + domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC); 2465 + if (domain->gcr3_tbl == NULL) 2466 + return -ENOMEM; 2467 + 2468 + domain->glx = levels; 2469 + domain->flags |= PD_IOMMUV2_MASK; 2470 + 2471 + amd_iommu_domain_update(domain); 2472 + 2473 + return 0; 2474 + } 2475 + 2476 + int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids) 2477 + { 2478 + struct protection_domain *pdom = to_pdomain(dom); 2479 + unsigned long flags; 2480 + int ret; 2481 + 2482 + spin_lock_irqsave(&pdom->lock, flags); 2487 2483 2488 2484 /* 2489 2485 * Save us all sanity checks whether devices already in the ··· 2509 2469 * devices attached when it is switched into IOMMUv2 mode. 2510 2470 */ 2511 2471 ret = -EBUSY; 2512 - if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK) 2472 + if (pdom->dev_cnt > 0 || pdom->flags & PD_IOMMUV2_MASK) 2513 2473 goto out; 2514 2474 2515 - ret = -ENOMEM; 2516 - domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC); 2517 - if (domain->gcr3_tbl == NULL) 2518 - goto out; 2519 - 2520 - domain->glx = levels; 2521 - domain->flags |= PD_IOMMUV2_MASK; 2522 - 2523 - amd_iommu_domain_update(domain); 2524 - 2525 - ret = 0; 2475 + if (!pdom->gcr3_tbl) 2476 + ret = domain_enable_v2(pdom, pasids); 2526 2477 2527 2478 out: 2528 - spin_unlock_irqrestore(&domain->lock, flags); 2529 - 2479 + spin_unlock_irqrestore(&pdom->lock, flags); 2530 2480 return ret; 2531 2481 } 2532 2482 EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
+24 -33
drivers/iommu/apple-dart.c
··· 15 15 #include <linux/bitfield.h> 16 16 #include <linux/clk.h> 17 17 #include <linux/dev_printk.h> 18 - #include <linux/dma-iommu.h> 19 18 #include <linux/dma-mapping.h> 20 19 #include <linux/err.h> 21 20 #include <linux/interrupt.h> ··· 31 32 #include <linux/slab.h> 32 33 #include <linux/swab.h> 33 34 #include <linux/types.h> 35 + 36 + #include "dma-iommu.h" 34 37 35 38 #define DART_MAX_STREAMS 16 36 39 #define DART_MAX_TTBR 4 ··· 82 81 #define DART_TTBR_VALID BIT(31) 83 82 #define DART_TTBR_SHIFT 12 84 83 84 + struct apple_dart_hw { 85 + u32 oas; 86 + enum io_pgtable_fmt fmt; 87 + }; 88 + 85 89 /* 86 90 * Private structure associated with each DART device. 87 91 * 88 92 * @dev: device struct 93 + * @hw: SoC-specific hardware data 89 94 * @regs: mapped MMIO region 90 95 * @irq: interrupt number, can be shared with other DARTs 91 96 * @clks: clocks associated with this DART ··· 105 98 */ 106 99 struct apple_dart { 107 100 struct device *dev; 101 + const struct apple_dart_hw *hw; 108 102 109 103 void __iomem *regs; 110 104 ··· 429 421 pgtbl_cfg = (struct io_pgtable_cfg){ 430 422 .pgsize_bitmap = dart->pgsize, 431 423 .ias = 32, 432 - .oas = 36, 424 + .oas = dart->hw->oas, 433 425 .coherent_walk = 1, 434 426 .iommu_dev = dart->dev, 435 427 }; 436 428 437 429 dart_domain->pgtbl_ops = 438 - alloc_io_pgtable_ops(APPLE_DART, &pgtbl_cfg, domain); 430 + alloc_io_pgtable_ops(dart->hw->fmt, &pgtbl_cfg, domain); 439 431 if (!dart_domain->pgtbl_ops) { 440 432 ret = -ENOMEM; 441 433 goto done; ··· 828 820 return IRQ_HANDLED; 829 821 } 830 822 831 - static int apple_dart_set_bus_ops(const struct iommu_ops *ops) 832 - { 833 - int ret; 834 - 835 - if (!iommu_present(&platform_bus_type)) { 836 - ret = bus_set_iommu(&platform_bus_type, ops); 837 - if (ret) 838 - return ret; 839 - } 840 - #ifdef CONFIG_PCI 841 - if (!iommu_present(&pci_bus_type)) { 842 - ret = bus_set_iommu(&pci_bus_type, ops); 843 - if (ret) { 844 - bus_set_iommu(&platform_bus_type, NULL); 845 - return ret; 846 - } 847 - } 848 - #endif 849 - return 0; 850 - } 851 - 852 823 static int apple_dart_probe(struct platform_device *pdev) 853 824 { 854 825 int ret; ··· 841 854 return -ENOMEM; 842 855 843 856 dart->dev = dev; 857 + dart->hw = of_device_get_match_data(dev); 844 858 spin_lock_init(&dart->lock); 845 859 846 860 dart->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res); ··· 883 895 884 896 platform_set_drvdata(pdev, dart); 885 897 886 - ret = apple_dart_set_bus_ops(&apple_dart_iommu_ops); 887 - if (ret) 888 - goto err_free_irq; 889 - 890 898 ret = iommu_device_sysfs_add(&dart->iommu, dev, NULL, "apple-dart.%s", 891 899 dev_name(&pdev->dev)); 892 900 if (ret) 893 - goto err_remove_bus_ops; 901 + goto err_free_irq; 894 902 895 903 ret = iommu_device_register(&dart->iommu, &apple_dart_iommu_ops, dev); 896 904 if (ret) ··· 900 916 901 917 err_sysfs_remove: 902 918 iommu_device_sysfs_remove(&dart->iommu); 903 - err_remove_bus_ops: 904 - apple_dart_set_bus_ops(NULL); 905 919 err_free_irq: 906 920 free_irq(dart->irq, dart); 907 921 err_clk_disable: ··· 914 932 915 933 apple_dart_hw_reset(dart); 916 934 free_irq(dart->irq, dart); 917 - apple_dart_set_bus_ops(NULL); 918 935 919 936 iommu_device_unregister(&dart->iommu); 920 937 iommu_device_sysfs_remove(&dart->iommu); ··· 923 942 return 0; 924 943 } 925 944 945 + static const struct apple_dart_hw apple_dart_hw_t8103 = { 946 + .oas = 36, 947 + .fmt = APPLE_DART, 948 + }; 949 + static const struct apple_dart_hw apple_dart_hw_t6000 = { 950 + .oas = 42, 951 + .fmt = APPLE_DART2, 952 + }; 953 + 926 954 static const struct of_device_id apple_dart_of_match[] = { 927 - { .compatible = "apple,t8103-dart", .data = NULL }, 955 + { .compatible = "apple,t8103-dart", .data = &apple_dart_hw_t8103 }, 956 + { .compatible = "apple,t6000-dart", .data = &apple_dart_hw_t6000 }, 928 957 {}, 929 958 }; 930 959 MODULE_DEVICE_TABLE(of, apple_dart_of_match);
+8 -54
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
··· 14 14 #include <linux/bitops.h> 15 15 #include <linux/crash_dump.h> 16 16 #include <linux/delay.h> 17 - #include <linux/dma-iommu.h> 18 17 #include <linux/err.h> 19 18 #include <linux/interrupt.h> 20 19 #include <linux/io-pgtable.h> ··· 27 28 #include <linux/pci-ats.h> 28 29 #include <linux/platform_device.h> 29 30 30 - #include <linux/amba/bus.h> 31 - 32 31 #include "arm-smmu-v3.h" 32 + #include "../../dma-iommu.h" 33 33 #include "../../iommu-sva-lib.h" 34 34 35 35 static bool disable_bypass = true; ··· 1990 1992 }; 1991 1993 1992 1994 /* IOMMU API */ 1993 - static bool arm_smmu_capable(enum iommu_cap cap) 1995 + static bool arm_smmu_capable(struct device *dev, enum iommu_cap cap) 1994 1996 { 1997 + struct arm_smmu_master *master = dev_iommu_priv_get(dev); 1998 + 1995 1999 switch (cap) { 1996 2000 case IOMMU_CAP_CACHE_COHERENCY: 1997 - return true; 2001 + /* Assume that a coherent TCU implies coherent TBUs */ 2002 + return master->smmu->features & ARM_SMMU_FEAT_COHERENCY; 1998 2003 case IOMMU_CAP_NOEXEC: 1999 2004 return true; 2000 2005 default: ··· 3695 3694 return SZ_128K; 3696 3695 } 3697 3696 3698 - static int arm_smmu_set_bus_ops(struct iommu_ops *ops) 3699 - { 3700 - int err; 3701 - 3702 - #ifdef CONFIG_PCI 3703 - if (pci_bus_type.iommu_ops != ops) { 3704 - err = bus_set_iommu(&pci_bus_type, ops); 3705 - if (err) 3706 - return err; 3707 - } 3708 - #endif 3709 - #ifdef CONFIG_ARM_AMBA 3710 - if (amba_bustype.iommu_ops != ops) { 3711 - err = bus_set_iommu(&amba_bustype, ops); 3712 - if (err) 3713 - goto err_reset_pci_ops; 3714 - } 3715 - #endif 3716 - if (platform_bus_type.iommu_ops != ops) { 3717 - err = bus_set_iommu(&platform_bus_type, ops); 3718 - if (err) 3719 - goto err_reset_amba_ops; 3720 - } 3721 - 3722 - return 0; 3723 - 3724 - err_reset_amba_ops: 3725 - #ifdef CONFIG_ARM_AMBA 3726 - bus_set_iommu(&amba_bustype, NULL); 3727 - #endif 3728 - err_reset_pci_ops: __maybe_unused; 3729 - #ifdef CONFIG_PCI 3730 - bus_set_iommu(&pci_bus_type, NULL); 3731 - #endif 3732 - return err; 3733 - } 3734 - 3735 3697 static void __iomem *arm_smmu_ioremap(struct device *dev, resource_size_t start, 3736 3698 resource_size_t size) 3737 3699 { ··· 3833 3869 ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev); 3834 3870 if (ret) { 3835 3871 dev_err(dev, "Failed to register iommu\n"); 3836 - goto err_sysfs_remove; 3872 + iommu_device_sysfs_remove(&smmu->iommu); 3873 + return ret; 3837 3874 } 3838 3875 3839 - ret = arm_smmu_set_bus_ops(&arm_smmu_ops); 3840 - if (ret) 3841 - goto err_unregister_device; 3842 - 3843 3876 return 0; 3844 - 3845 - err_unregister_device: 3846 - iommu_device_unregister(&smmu->iommu); 3847 - err_sysfs_remove: 3848 - iommu_device_sysfs_remove(&smmu->iommu); 3849 - return ret; 3850 3877 } 3851 3878 3852 3879 static int arm_smmu_device_remove(struct platform_device *pdev) 3853 3880 { 3854 3881 struct arm_smmu_device *smmu = platform_get_drvdata(pdev); 3855 3882 3856 - arm_smmu_set_bus_ops(NULL); 3857 3883 iommu_device_unregister(&smmu->iommu); 3858 3884 iommu_device_sysfs_remove(&smmu->iommu); 3859 3885 arm_smmu_device_disable(smmu);
+8 -89
drivers/iommu/arm/arm-smmu/arm-smmu.c
··· 21 21 #include <linux/acpi_iort.h> 22 22 #include <linux/bitfield.h> 23 23 #include <linux/delay.h> 24 - #include <linux/dma-iommu.h> 25 24 #include <linux/dma-mapping.h> 26 25 #include <linux/err.h> 27 26 #include <linux/interrupt.h> ··· 36 37 #include <linux/ratelimit.h> 37 38 #include <linux/slab.h> 38 39 39 - #include <linux/amba/bus.h> 40 40 #include <linux/fsl/mc.h> 41 41 42 42 #include "arm-smmu.h" 43 + #include "../../dma-iommu.h" 43 44 44 45 /* 45 46 * Apparently, some Qualcomm arm64 platforms which appear to expose their SMMU ··· 92 93 static struct iommu_ops arm_smmu_ops; 93 94 94 95 #ifdef CONFIG_ARM_SMMU_LEGACY_DT_BINDINGS 95 - static int arm_smmu_bus_init(struct iommu_ops *ops); 96 - 97 96 static struct device_node *dev_get_dev_node(struct device *dev) 98 97 { 99 98 if (dev_is_pci(dev)) { ··· 177 180 kfree(sids); 178 181 return err; 179 182 } 180 - 181 - /* 182 - * With the legacy DT binding in play, we have no guarantees about 183 - * probe order, but then we're also not doing default domains, so we can 184 - * delay setting bus ops until we're sure every possible SMMU is ready, 185 - * and that way ensure that no probe_device() calls get missed. 186 - */ 187 - static int arm_smmu_legacy_bus_init(void) 188 - { 189 - if (using_legacy_binding) 190 - return arm_smmu_bus_init(&arm_smmu_ops); 191 - return 0; 192 - } 193 - device_initcall_sync(arm_smmu_legacy_bus_init); 194 183 #else 195 184 static int arm_smmu_register_legacy_master(struct device *dev, 196 185 struct arm_smmu_device **smmu) ··· 1313 1330 return ops->iova_to_phys(ops, iova); 1314 1331 } 1315 1332 1316 - static bool arm_smmu_capable(enum iommu_cap cap) 1333 + static bool arm_smmu_capable(struct device *dev, enum iommu_cap cap) 1317 1334 { 1335 + struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev); 1336 + 1318 1337 switch (cap) { 1319 1338 case IOMMU_CAP_CACHE_COHERENCY: 1320 - /* 1321 - * Return true here as the SMMU can always send out coherent 1322 - * requests. 1323 - */ 1324 - return true; 1339 + /* Assume that a coherent TCU implies coherent TBUs */ 1340 + return cfg->smmu->features & ARM_SMMU_FEAT_COHERENT_WALK; 1325 1341 case IOMMU_CAP_NOEXEC: 1326 1342 return true; 1327 1343 default: ··· 1998 2016 return 0; 1999 2017 } 2000 2018 2001 - static int arm_smmu_bus_init(struct iommu_ops *ops) 2002 - { 2003 - int err; 2004 - 2005 - /* Oh, for a proper bus abstraction */ 2006 - if (!iommu_present(&platform_bus_type)) { 2007 - err = bus_set_iommu(&platform_bus_type, ops); 2008 - if (err) 2009 - return err; 2010 - } 2011 - #ifdef CONFIG_ARM_AMBA 2012 - if (!iommu_present(&amba_bustype)) { 2013 - err = bus_set_iommu(&amba_bustype, ops); 2014 - if (err) 2015 - goto err_reset_platform_ops; 2016 - } 2017 - #endif 2018 - #ifdef CONFIG_PCI 2019 - if (!iommu_present(&pci_bus_type)) { 2020 - err = bus_set_iommu(&pci_bus_type, ops); 2021 - if (err) 2022 - goto err_reset_amba_ops; 2023 - } 2024 - #endif 2025 - #ifdef CONFIG_FSL_MC_BUS 2026 - if (!iommu_present(&fsl_mc_bus_type)) { 2027 - err = bus_set_iommu(&fsl_mc_bus_type, ops); 2028 - if (err) 2029 - goto err_reset_pci_ops; 2030 - } 2031 - #endif 2032 - return 0; 2033 - 2034 - err_reset_pci_ops: __maybe_unused; 2035 - #ifdef CONFIG_PCI 2036 - bus_set_iommu(&pci_bus_type, NULL); 2037 - #endif 2038 - err_reset_amba_ops: __maybe_unused; 2039 - #ifdef CONFIG_ARM_AMBA 2040 - bus_set_iommu(&amba_bustype, NULL); 2041 - #endif 2042 - err_reset_platform_ops: __maybe_unused; 2043 - bus_set_iommu(&platform_bus_type, NULL); 2044 - return err; 2045 - } 2046 - 2047 2019 static void arm_smmu_rmr_install_bypass_smr(struct arm_smmu_device *smmu) 2048 2020 { 2049 2021 struct list_head rmr_list; ··· 2162 2226 err = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev); 2163 2227 if (err) { 2164 2228 dev_err(dev, "Failed to register iommu\n"); 2165 - goto err_sysfs_remove; 2229 + iommu_device_sysfs_remove(&smmu->iommu); 2230 + return err; 2166 2231 } 2167 2232 2168 2233 platform_set_drvdata(pdev, smmu); ··· 2185 2248 pm_runtime_enable(dev); 2186 2249 } 2187 2250 2188 - /* 2189 - * For ACPI and generic DT bindings, an SMMU will be probed before 2190 - * any device which might need it, so we want the bus ops in place 2191 - * ready to handle default domain setup as soon as any SMMU exists. 2192 - */ 2193 - if (!using_legacy_binding) { 2194 - err = arm_smmu_bus_init(&arm_smmu_ops); 2195 - if (err) 2196 - goto err_unregister_device; 2197 - } 2198 - 2199 2251 return 0; 2200 - 2201 - err_unregister_device: 2202 - iommu_device_unregister(&smmu->iommu); 2203 - err_sysfs_remove: 2204 - iommu_device_sysfs_remove(&smmu->iommu); 2205 - return err; 2206 2252 } 2207 2253 2208 2254 static int arm_smmu_device_remove(struct platform_device *pdev) ··· 2198 2278 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS)) 2199 2279 dev_notice(&pdev->dev, "disabling translation\n"); 2200 2280 2201 - arm_smmu_bus_init(NULL); 2202 2281 iommu_device_unregister(&smmu->iommu); 2203 2282 iommu_device_sysfs_remove(&smmu->iommu); 2204 2283
+1 -5
drivers/iommu/arm/arm-smmu/qcom_iommu.c
··· 493 493 return ret; 494 494 } 495 495 496 - static bool qcom_iommu_capable(enum iommu_cap cap) 496 + static bool qcom_iommu_capable(struct device *dev, enum iommu_cap cap) 497 497 { 498 498 switch (cap) { 499 499 case IOMMU_CAP_CACHE_COHERENCY: ··· 837 837 goto err_pm_disable; 838 838 } 839 839 840 - bus_set_iommu(&platform_bus_type, &qcom_iommu_ops); 841 - 842 840 if (qcom_iommu->local_base) { 843 841 pm_runtime_get_sync(dev); 844 842 writel_relaxed(0xffffffff, qcom_iommu->local_base + SMMU_INTR_SEL_NS); ··· 853 855 static int qcom_iommu_device_remove(struct platform_device *pdev) 854 856 { 855 857 struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev); 856 - 857 - bus_set_iommu(&platform_bus_type, NULL); 858 858 859 859 pm_runtime_force_suspend(&pdev->dev); 860 860 platform_set_drvdata(pdev, NULL);
+15 -3
drivers/iommu/dma-iommu.c
··· 13 13 #include <linux/crash_dump.h> 14 14 #include <linux/device.h> 15 15 #include <linux/dma-direct.h> 16 - #include <linux/dma-iommu.h> 17 16 #include <linux/dma-map-ops.h> 18 17 #include <linux/gfp.h> 19 18 #include <linux/huge_mm.h> ··· 28 29 #include <linux/spinlock.h> 29 30 #include <linux/swiotlb.h> 30 31 #include <linux/vmalloc.h> 32 + 33 + #include "dma-iommu.h" 31 34 32 35 struct iommu_dma_msi_page { 33 36 struct list_head list; ··· 1634 1633 return NULL; 1635 1634 } 1636 1635 1636 + /** 1637 + * iommu_dma_prepare_msi() - Map the MSI page in the IOMMU domain 1638 + * @desc: MSI descriptor, will store the MSI page 1639 + * @msi_addr: MSI target address to be mapped 1640 + * 1641 + * Return: 0 on success or negative error code if the mapping failed. 1642 + */ 1637 1643 int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr) 1638 1644 { 1639 1645 struct device *dev = msi_desc_to_dev(desc); ··· 1669 1661 return 0; 1670 1662 } 1671 1663 1672 - void iommu_dma_compose_msi_msg(struct msi_desc *desc, 1673 - struct msi_msg *msg) 1664 + /** 1665 + * iommu_dma_compose_msi_msg() - Apply translation to an MSI message 1666 + * @desc: MSI descriptor prepared by iommu_dma_prepare_msi() 1667 + * @msg: MSI message containing target physical address 1668 + */ 1669 + void iommu_dma_compose_msi_msg(struct msi_desc *desc, struct msi_msg *msg) 1674 1670 { 1675 1671 struct device *dev = msi_desc_to_dev(desc); 1676 1672 const struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
+42
drivers/iommu/dma-iommu.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + /* 3 + * Copyright (C) 2014-2015 ARM Ltd. 4 + */ 5 + #ifndef __DMA_IOMMU_H 6 + #define __DMA_IOMMU_H 7 + 8 + #include <linux/iommu.h> 9 + 10 + #ifdef CONFIG_IOMMU_DMA 11 + 12 + int iommu_get_dma_cookie(struct iommu_domain *domain); 13 + void iommu_put_dma_cookie(struct iommu_domain *domain); 14 + 15 + int iommu_dma_init_fq(struct iommu_domain *domain); 16 + 17 + void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list); 18 + 19 + extern bool iommu_dma_forcedac; 20 + 21 + #else /* CONFIG_IOMMU_DMA */ 22 + 23 + static inline int iommu_dma_init_fq(struct iommu_domain *domain) 24 + { 25 + return -EINVAL; 26 + } 27 + 28 + static inline int iommu_get_dma_cookie(struct iommu_domain *domain) 29 + { 30 + return -ENODEV; 31 + } 32 + 33 + static inline void iommu_put_dma_cookie(struct iommu_domain *domain) 34 + { 35 + } 36 + 37 + static inline void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list) 38 + { 39 + } 40 + 41 + #endif /* CONFIG_IOMMU_DMA */ 42 + #endif /* __DMA_IOMMU_H */
-9
drivers/iommu/exynos-iommu.c
··· 1446 1446 goto err_zero_lv2; 1447 1447 } 1448 1448 1449 - ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops); 1450 - if (ret) { 1451 - pr_err("%s: Failed to register exynos-iommu driver.\n", 1452 - __func__); 1453 - goto err_set_iommu; 1454 - } 1455 - 1456 1449 return 0; 1457 - err_set_iommu: 1458 - kmem_cache_free(lv2table_kmem_cache, zero_lv2_table); 1459 1450 err_zero_lv2: 1460 1451 platform_driver_unregister(&exynos_sysmmu_driver); 1461 1452 err_reg_driver:
+1 -5
drivers/iommu/fsl_pamu_domain.c
··· 178 178 return iova; 179 179 } 180 180 181 - static bool fsl_pamu_capable(enum iommu_cap cap) 181 + static bool fsl_pamu_capable(struct device *dev, enum iommu_cap cap) 182 182 { 183 183 return cap == IOMMU_CAP_CACHE_COHERENCY; 184 184 } ··· 476 476 if (ret) { 477 477 iommu_device_sysfs_remove(&pamu_iommu); 478 478 pr_err("Can't register iommu device\n"); 479 - return ret; 480 479 } 481 - 482 - bus_set_iommu(&platform_bus_type, &fsl_pamu_ops); 483 - bus_set_iommu(&pci_bus_type, &fsl_pamu_ops); 484 480 485 481 return ret; 486 482 }
+2 -4
drivers/iommu/intel/Kconfig
··· 19 19 select DMAR_TABLE 20 20 select SWIOTLB 21 21 select IOASID 22 - select IOMMU_DMA 23 22 select PCI_ATS 23 + select PCI_PRI 24 + select PCI_PASID 24 25 help 25 26 DMA remapping (DMAR) devices support enables independent address 26 27 translations for Direct Memory Access (DMA) from devices. ··· 49 48 config INTEL_IOMMU_SVM 50 49 bool "Support for Shared Virtual Memory with Intel IOMMU" 51 50 depends on X86_64 52 - select PCI_PASID 53 - select PCI_PRI 54 51 select MMU_NOTIFIER 55 - select IOASID 56 52 select IOMMU_SVA 57 53 help 58 54 Shared Virtual Memory (SVM) provides a facility for devices
+2 -2
drivers/iommu/intel/cap_audit.c
··· 37 37 MINIMAL_FEATURE_IOMMU(b, ecap, ECAP_MHMV_MASK); 38 38 MINIMAL_FEATURE_IOMMU(b, ecap, ECAP_IRO_MASK); 39 39 40 - CHECK_FEATURE_MISMATCH(a, b, cap, 5lp_support, CAP_FL5LP_MASK); 40 + CHECK_FEATURE_MISMATCH(a, b, cap, fl5lp_support, CAP_FL5LP_MASK); 41 41 CHECK_FEATURE_MISMATCH(a, b, cap, fl1gp_support, CAP_FL1GP_MASK); 42 42 CHECK_FEATURE_MISMATCH(a, b, cap, read_drain, CAP_RD_MASK); 43 43 CHECK_FEATURE_MISMATCH(a, b, cap, write_drain, CAP_WD_MASK); ··· 84 84 goto out; 85 85 } 86 86 87 - CHECK_FEATURE_MISMATCH_HOTPLUG(iommu, cap, 5lp_support, CAP_FL5LP_MASK); 87 + CHECK_FEATURE_MISMATCH_HOTPLUG(iommu, cap, fl5lp_support, CAP_FL5LP_MASK); 88 88 CHECK_FEATURE_MISMATCH_HOTPLUG(iommu, cap, fl1gp_support, CAP_FL1GP_MASK); 89 89 CHECK_FEATURE_MISMATCH_HOTPLUG(iommu, cap, read_drain, CAP_RD_MASK); 90 90 CHECK_FEATURE_MISMATCH_HOTPLUG(iommu, cap, write_drain, CAP_WD_MASK);
+27 -68
drivers/iommu/intel/iommu.c
··· 15 15 16 16 #include <linux/crash_dump.h> 17 17 #include <linux/dma-direct.h> 18 - #include <linux/dma-iommu.h> 19 18 #include <linux/dmi.h> 20 19 #include <linux/intel-svm.h> 21 20 #include <linux/memory.h> ··· 25 26 #include <linux/tboot.h> 26 27 27 28 #include "iommu.h" 29 + #include "../dma-iommu.h" 28 30 #include "../irq_remapping.h" 29 31 #include "../iommu-sva-lib.h" 30 32 #include "pasid.h" ··· 197 197 unsigned long value) 198 198 { 199 199 context->hi |= (value & ((1 << 16) - 1)) << 8; 200 + } 201 + 202 + static inline void context_set_pasid(struct context_entry *context) 203 + { 204 + context->lo |= CONTEXT_PASIDE; 200 205 } 201 206 202 207 static inline int context_domain_id(struct context_entry *c) ··· 404 399 { 405 400 unsigned long fl_sagaw, sl_sagaw; 406 401 407 - fl_sagaw = BIT(2) | (cap_5lp_support(iommu->cap) ? BIT(3) : 0); 402 + fl_sagaw = BIT(2) | (cap_fl5lp_support(iommu->cap) ? BIT(3) : 0); 408 403 sl_sagaw = cap_sagaw(iommu->cap); 409 404 410 405 /* Second level only. */ ··· 1239 1234 1240 1235 raw_spin_unlock_irqrestore(&iommu->register_lock, flag); 1241 1236 1237 + /* 1238 + * Hardware invalidates all DMA remapping hardware translation 1239 + * caches as part of SRTP flow. 1240 + */ 1241 + if (cap_esrtps(iommu->cap)) 1242 + return; 1243 + 1242 1244 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL); 1243 1245 if (sm_supported(iommu)) 1244 1246 qi_flush_pasid_cache(iommu, 0, QI_PC_GLOBAL, 0); ··· 1362 1350 } 1363 1351 1364 1352 static struct device_domain_info * 1365 - iommu_support_dev_iotlb(struct dmar_domain *domain, struct intel_iommu *iommu, 1366 - u8 bus, u8 devfn) 1353 + domain_lookup_dev_info(struct dmar_domain *domain, 1354 + struct intel_iommu *iommu, u8 bus, u8 devfn) 1367 1355 { 1368 1356 struct device_domain_info *info; 1369 1357 unsigned long flags; 1370 - 1371 - if (!iommu->qi) 1372 - return NULL; 1373 1358 1374 1359 spin_lock_irqsave(&domain->lock, flags); 1375 1360 list_for_each_entry(info, &domain->devices, link) { 1376 1361 if (info->iommu == iommu && info->bus == bus && 1377 1362 info->devfn == devfn) { 1378 1363 spin_unlock_irqrestore(&domain->lock, flags); 1379 - return info->ats_supported ? info : NULL; 1364 + return info; 1380 1365 } 1381 1366 } 1382 1367 spin_unlock_irqrestore(&domain->lock, flags); ··· 1398 1389 spin_unlock_irqrestore(&domain->lock, flags); 1399 1390 } 1400 1391 1401 - static void iommu_enable_dev_iotlb(struct device_domain_info *info) 1392 + static void iommu_enable_pci_caps(struct device_domain_info *info) 1402 1393 { 1403 1394 struct pci_dev *pdev; 1404 1395 ··· 1421 1412 info->pfsid = pci_dev_id(pf_pdev); 1422 1413 } 1423 1414 1424 - #ifdef CONFIG_INTEL_IOMMU_SVM 1425 1415 /* The PCIe spec, in its wisdom, declares that the behaviour of 1426 1416 the device if you enable PASID support after ATS support is 1427 1417 undefined. So always enable PASID support on devices which ··· 1433 1425 (info->pasid_enabled ? pci_prg_resp_pasid_required(pdev) : 1) && 1434 1426 !pci_reset_pri(pdev) && !pci_enable_pri(pdev, PRQ_DEPTH)) 1435 1427 info->pri_enabled = 1; 1436 - #endif 1428 + 1437 1429 if (info->ats_supported && pci_ats_page_aligned(pdev) && 1438 1430 !pci_enable_ats(pdev, VTD_PAGE_SHIFT)) { 1439 1431 info->ats_enabled = 1; ··· 1456 1448 info->ats_enabled = 0; 1457 1449 domain_update_iotlb(info->domain); 1458 1450 } 1459 - #ifdef CONFIG_INTEL_IOMMU_SVM 1451 + 1460 1452 if (info->pri_enabled) { 1461 1453 pci_disable_pri(pdev); 1462 1454 info->pri_enabled = 0; 1463 1455 } 1456 + 1464 1457 if (info->pasid_enabled) { 1465 1458 pci_disable_pasid(pdev); 1466 1459 info->pasid_enabled = 0; 1467 1460 } 1468 - #endif 1469 1461 } 1470 1462 1471 1463 static void __iommu_flush_dev_iotlb(struct device_domain_info *info, ··· 1915 1907 u8 bus, u8 devfn) 1916 1908 { 1917 1909 struct device_domain_info *info = 1918 - iommu_support_dev_iotlb(domain, iommu, bus, devfn); 1910 + domain_lookup_dev_info(domain, iommu, bus, devfn); 1919 1911 u16 did = domain_id_iommu(domain, iommu); 1920 1912 int translation = CONTEXT_TT_MULTI_LEVEL; 1921 1913 struct context_entry *context; ··· 1988 1980 context_set_sm_dte(context); 1989 1981 if (info && info->pri_supported) 1990 1982 context_set_sm_pre(context); 1983 + if (info && info->pasid_supported) 1984 + context_set_pasid(context); 1991 1985 } else { 1992 1986 struct dma_pte *pgd = domain->pgd; 1993 1987 int agaw; ··· 2047 2037 } else { 2048 2038 iommu_flush_write_buffer(iommu); 2049 2039 } 2050 - iommu_enable_dev_iotlb(info); 2040 + iommu_enable_pci_caps(info); 2051 2041 2052 2042 ret = 0; 2053 2043 ··· 3906 3896 continue; 3907 3897 } 3908 3898 3909 - pn->dev->bus->iommu_ops = &intel_iommu_ops; 3910 3899 ret = iommu_probe_device(pn->dev); 3911 3900 if (ret) 3912 3901 break; ··· 4038 4029 } 4039 4030 up_read(&dmar_global_lock); 4040 4031 4041 - bus_set_iommu(&pci_bus_type, &intel_iommu_ops); 4042 4032 if (si_domain && !hw_pass_through) 4043 4033 register_memory_notifier(&intel_iommu_memory_nb); 4044 4034 ··· 4445 4437 return true; 4446 4438 } 4447 4439 4448 - static bool intel_iommu_capable(enum iommu_cap cap) 4440 + static bool intel_iommu_capable(struct device *dev, enum iommu_cap cap) 4449 4441 { 4450 4442 if (cap == IOMMU_CAP_CACHE_COHERENCY) 4451 4443 return true; ··· 4465 4457 u8 bus, devfn; 4466 4458 4467 4459 iommu = device_to_iommu(dev, &bus, &devfn); 4468 - if (!iommu) 4460 + if (!iommu || !iommu->iommu.ops) 4469 4461 return ERR_PTR(-ENODEV); 4470 4462 4471 4463 info = kzalloc(sizeof(*info), GFP_KERNEL); ··· 4582 4574 list_add_tail(&reg->list, head); 4583 4575 } 4584 4576 4585 - int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct device *dev) 4586 - { 4587 - struct device_domain_info *info = dev_iommu_priv_get(dev); 4588 - struct context_entry *context; 4589 - struct dmar_domain *domain; 4590 - u64 ctx_lo; 4591 - int ret; 4592 - 4593 - domain = info->domain; 4594 - if (!domain) 4595 - return -EINVAL; 4596 - 4597 - spin_lock(&iommu->lock); 4598 - ret = -EINVAL; 4599 - if (!info->pasid_supported) 4600 - goto out; 4601 - 4602 - context = iommu_context_addr(iommu, info->bus, info->devfn, 0); 4603 - if (WARN_ON(!context)) 4604 - goto out; 4605 - 4606 - ctx_lo = context[0].lo; 4607 - 4608 - if (!(ctx_lo & CONTEXT_PASIDE)) { 4609 - ctx_lo |= CONTEXT_PASIDE; 4610 - context[0].lo = ctx_lo; 4611 - wmb(); 4612 - iommu->flush.flush_context(iommu, 4613 - domain_id_iommu(domain, iommu), 4614 - PCI_DEVID(info->bus, info->devfn), 4615 - DMA_CCMD_MASK_NOBIT, 4616 - DMA_CCMD_DEVICE_INVL); 4617 - } 4618 - 4619 - /* Enable PASID support in the device, if it wasn't already */ 4620 - if (!info->pasid_enabled) 4621 - iommu_enable_dev_iotlb(info); 4622 - 4623 - ret = 0; 4624 - 4625 - out: 4626 - spin_unlock(&iommu->lock); 4627 - 4628 - return ret; 4629 - } 4630 - 4631 4577 static struct iommu_group *intel_iommu_device_group(struct device *dev) 4632 4578 { 4633 4579 if (dev_is_pci(dev)) ··· 4603 4641 return -EINVAL; 4604 4642 4605 4643 if (!(iommu->flags & VTD_FLAG_SVM_CAPABLE)) 4606 - return -ENODEV; 4607 - 4608 - if (intel_iommu_enable_pasid(iommu, dev)) 4609 4644 return -ENODEV; 4610 4645 4611 4646 if (!info->pasid_enabled || !info->pri_enabled || !info->ats_enabled)
+4 -3
drivers/iommu/intel/iommu.h
··· 146 146 /* 147 147 * Decoding Capability Register 148 148 */ 149 - #define cap_5lp_support(c) (((c) >> 60) & 1) 149 + #define cap_esrtps(c) (((c) >> 63) & 1) 150 + #define cap_esirtps(c) (((c) >> 62) & 1) 151 + #define cap_fl5lp_support(c) (((c) >> 60) & 1) 150 152 #define cap_pi_support(c) (((c) >> 59) & 1) 151 153 #define cap_fl1gp_support(c) (((c) >> 56) & 1) 152 154 #define cap_read_drain(c) (((c) >> 55) & 1) ··· 588 586 #ifdef CONFIG_INTEL_IOMMU_SVM 589 587 struct page_req_dsc *prq; 590 588 unsigned char prq_name[16]; /* Name for PRQ interrupt */ 589 + unsigned long prq_seq_number; 591 590 struct completion prq_complete; 592 591 struct ioasid_allocator_ops pasid_allocator; /* Custom allocator for PASIDs */ 593 592 #endif ··· 744 741 void *alloc_pgtable_page(int node); 745 742 void free_pgtable_page(void *vaddr); 746 743 void iommu_flush_write_buffer(struct intel_iommu *iommu); 747 - int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct device *dev); 748 744 struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn); 749 745 750 746 #ifdef CONFIG_INTEL_IOMMU_SVM ··· 763 761 struct device *dev; 764 762 struct intel_iommu *iommu; 765 763 struct iommu_sva sva; 766 - unsigned long prq_seq_number; 767 764 u32 pasid; 768 765 int users; 769 766 u16 did;
+4 -2
drivers/iommu/intel/irq_remapping.c
··· 494 494 * Global invalidation of interrupt entry cache to make sure the 495 495 * hardware uses the new irq remapping table. 496 496 */ 497 - qi_global_iec(iommu); 497 + if (!cap_esirtps(iommu->cap)) 498 + qi_global_iec(iommu); 498 499 } 499 500 500 501 static void iommu_enable_irq_remapping(struct intel_iommu *iommu) ··· 681 680 * global invalidation of interrupt entry cache before disabling 682 681 * interrupt-remapping. 683 682 */ 684 - qi_global_iec(iommu); 683 + if (!cap_esirtps(iommu->cap)) 684 + qi_global_iec(iommu); 685 685 686 686 raw_spin_lock_irqsave(&iommu->register_lock, flags); 687 687
+1 -11
drivers/iommu/intel/pasid.c
··· 392 392 pasid_set_bits(&pe->val[2], GENMASK_ULL(3, 2), value << 2); 393 393 } 394 394 395 - /* 396 - * Setup the Extended Access Flag Enable (EAFE) field (Bit 135) 397 - * of a scalable mode PASID entry. 398 - */ 399 - static inline void 400 - pasid_set_eafe(struct pasid_entry *pe) 401 - { 402 - pasid_set_bits(&pe->val[2], 1 << 7, 1 << 7); 403 - } 404 - 405 395 static void 406 396 pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu, 407 397 u16 did, u32 pasid) ··· 519 529 } 520 530 } 521 531 522 - if ((flags & PASID_FLAG_FL5LP) && !cap_5lp_support(iommu->cap)) { 532 + if ((flags & PASID_FLAG_FL5LP) && !cap_fl5lp_support(iommu->cap)) { 523 533 pr_err("No 5-level paging support for first-level on %s\n", 524 534 iommu->name); 525 535 return -EINVAL;
+8 -54
drivers/iommu/intel/svm.c
··· 49 49 } 50 50 51 51 static struct intel_svm_dev * 52 - svm_lookup_device_by_sid(struct intel_svm *svm, u16 sid) 53 - { 54 - struct intel_svm_dev *sdev = NULL, *t; 55 - 56 - rcu_read_lock(); 57 - list_for_each_entry_rcu(t, &svm->devs, list) { 58 - if (t->sid == sid) { 59 - sdev = t; 60 - break; 61 - } 62 - } 63 - rcu_read_unlock(); 64 - 65 - return sdev; 66 - } 67 - 68 - static struct intel_svm_dev * 69 52 svm_lookup_device_by_dev(struct intel_svm *svm, struct device *dev) 70 53 { 71 54 struct intel_svm_dev *sdev = NULL, *t; ··· 164 181 } 165 182 166 183 if (cpu_feature_enabled(X86_FEATURE_LA57) && 167 - !cap_5lp_support(iommu->cap)) { 184 + !cap_fl5lp_support(iommu->cap)) { 168 185 pr_err("%s SVM disabled, incompatible paging mode\n", 169 186 iommu->name); 170 187 return; ··· 689 706 690 707 static irqreturn_t prq_event_thread(int irq, void *d) 691 708 { 692 - struct intel_svm_dev *sdev = NULL; 693 709 struct intel_iommu *iommu = d; 694 - struct intel_svm *svm = NULL; 695 710 struct page_req_dsc *req; 696 711 int head, tail, handled; 712 + struct pci_dev *pdev; 697 713 u64 address; 698 714 699 715 /* ··· 712 730 pr_err("IOMMU: %s: Page request without PASID\n", 713 731 iommu->name); 714 732 bad_req: 715 - svm = NULL; 716 - sdev = NULL; 717 733 handle_bad_prq_event(iommu, req, QI_RESP_INVALID); 718 734 goto prq_advance; 719 735 } ··· 738 758 if (unlikely(req->lpig && !req->rd_req && !req->wr_req)) 739 759 goto prq_advance; 740 760 741 - if (!svm || svm->pasid != req->pasid) { 742 - /* 743 - * It can't go away, because the driver is not permitted 744 - * to unbind the mm while any page faults are outstanding. 745 - */ 746 - svm = pasid_private_find(req->pasid); 747 - if (IS_ERR_OR_NULL(svm) || (svm->flags & SVM_FLAG_SUPERVISOR_MODE)) 748 - goto bad_req; 749 - } 750 - 751 - if (!sdev || sdev->sid != req->rid) { 752 - sdev = svm_lookup_device_by_sid(svm, req->rid); 753 - if (!sdev) 754 - goto bad_req; 755 - } 756 - 757 - sdev->prq_seq_number++; 758 - 761 + pdev = pci_get_domain_bus_and_slot(iommu->segment, 762 + PCI_BUS_NUM(req->rid), 763 + req->rid & 0xff); 759 764 /* 760 765 * If prq is to be handled outside iommu driver via receiver of 761 766 * the fault notifiers, we skip the page response here. 762 767 */ 763 - if (intel_svm_prq_report(iommu, sdev->dev, req)) 768 + if (!pdev || intel_svm_prq_report(iommu, &pdev->dev, req)) 764 769 handle_bad_prq_event(iommu, req, QI_RESP_INVALID); 765 770 766 - trace_prq_report(iommu, sdev->dev, req->qw_0, req->qw_1, 771 + trace_prq_report(iommu, &pdev->dev, req->qw_0, req->qw_1, 767 772 req->priv_data[0], req->priv_data[1], 768 - sdev->prq_seq_number); 773 + iommu->prq_seq_number++); 769 774 prq_advance: 770 775 head = (head + sizeof(*req)) & PRQ_RING_MASK; 771 776 } ··· 846 881 struct iommu_page_response *msg) 847 882 { 848 883 struct iommu_fault_page_request *prm; 849 - struct intel_svm_dev *sdev = NULL; 850 - struct intel_svm *svm = NULL; 851 884 struct intel_iommu *iommu; 852 885 bool private_present; 853 886 bool pasid_present; ··· 864 901 if (!msg || !evt) 865 902 return -EINVAL; 866 903 867 - mutex_lock(&pasid_mutex); 868 - 869 904 prm = &evt->fault.prm; 870 905 sid = PCI_DEVID(bus, devfn); 871 906 pasid_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; ··· 877 916 878 917 if (prm->pasid == 0 || prm->pasid >= PASID_MAX) { 879 918 ret = -EINVAL; 880 - goto out; 881 - } 882 - 883 - ret = pasid_to_svm_sdev(dev, prm->pasid, &svm, &sdev); 884 - if (ret || !sdev) { 885 - ret = -ENODEV; 886 919 goto out; 887 920 } 888 921 ··· 909 954 qi_submit_sync(iommu, &desc, 1, 0); 910 955 } 911 956 out: 912 - mutex_unlock(&pasid_mutex); 913 957 return ret; 914 958 }
+6 -65
drivers/iommu/io-pgtable-arm.c
··· 130 130 #define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL 131 131 #define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL 132 132 133 - #define APPLE_DART_PTE_PROT_NO_WRITE (1<<7) 134 - #define APPLE_DART_PTE_PROT_NO_READ (1<<8) 135 - 136 133 /* IOPTE accessors */ 137 134 #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d)) 138 135 ··· 197 200 void *pages; 198 201 199 202 VM_BUG_ON((gfp & __GFP_HIGHMEM)); 200 - p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE, 201 - gfp | __GFP_ZERO, order); 203 + p = alloc_pages_node(dev_to_node(dev), gfp | __GFP_ZERO, order); 202 204 if (!p) 203 205 return NULL; 204 206 ··· 401 405 int prot) 402 406 { 403 407 arm_lpae_iopte pte; 404 - 405 - if (data->iop.fmt == APPLE_DART) { 406 - pte = 0; 407 - if (!(prot & IOMMU_WRITE)) 408 - pte |= APPLE_DART_PTE_PROT_NO_WRITE; 409 - if (!(prot & IOMMU_READ)) 410 - pte |= APPLE_DART_PTE_PROT_NO_READ; 411 - return pte; 412 - } 413 408 414 409 if (data->iop.fmt == ARM_64_LPAE_S1 || 415 410 data->iop.fmt == ARM_32_LPAE_S1) { ··· 1094 1107 return NULL; 1095 1108 } 1096 1109 1097 - static struct io_pgtable * 1098 - apple_dart_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) 1099 - { 1100 - struct arm_lpae_io_pgtable *data; 1101 - int i; 1102 - 1103 - if (cfg->oas > 36) 1104 - return NULL; 1105 - 1106 - data = arm_lpae_alloc_pgtable(cfg); 1107 - if (!data) 1108 - return NULL; 1109 - 1110 - /* 1111 - * The table format itself always uses two levels, but the total VA 1112 - * space is mapped by four separate tables, making the MMIO registers 1113 - * an effective "level 1". For simplicity, though, we treat this 1114 - * equivalently to LPAE stage 2 concatenation at level 2, with the 1115 - * additional TTBRs each just pointing at consecutive pages. 1116 - */ 1117 - if (data->start_level < 1) 1118 - goto out_free_data; 1119 - if (data->start_level == 1 && data->pgd_bits > 2) 1120 - goto out_free_data; 1121 - if (data->start_level > 1) 1122 - data->pgd_bits = 0; 1123 - data->start_level = 2; 1124 - cfg->apple_dart_cfg.n_ttbrs = 1 << data->pgd_bits; 1125 - data->pgd_bits += data->bits_per_level; 1126 - 1127 - data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL, 1128 - cfg); 1129 - if (!data->pgd) 1130 - goto out_free_data; 1131 - 1132 - for (i = 0; i < cfg->apple_dart_cfg.n_ttbrs; ++i) 1133 - cfg->apple_dart_cfg.ttbr[i] = 1134 - virt_to_phys(data->pgd + i * ARM_LPAE_GRANULE(data)); 1135 - 1136 - return &data->iop; 1137 - 1138 - out_free_data: 1139 - kfree(data); 1140 - return NULL; 1141 - } 1142 - 1143 1110 struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { 1144 1111 .alloc = arm_64_lpae_alloc_pgtable_s1, 1145 1112 .free = arm_lpae_free_pgtable, ··· 1116 1175 1117 1176 struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = { 1118 1177 .alloc = arm_mali_lpae_alloc_pgtable, 1119 - .free = arm_lpae_free_pgtable, 1120 - }; 1121 - 1122 - struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns = { 1123 - .alloc = apple_dart_alloc_pgtable, 1124 1178 .free = arm_lpae_free_pgtable, 1125 1179 }; 1126 1180 ··· 1279 1343 }; 1280 1344 1281 1345 int i, j, pass = 0, fail = 0; 1346 + struct device dev; 1282 1347 struct io_pgtable_cfg cfg = { 1283 1348 .tlb = &dummy_tlb_ops, 1284 1349 .oas = 48, 1285 1350 .coherent_walk = true, 1351 + .iommu_dev = &dev, 1286 1352 }; 1353 + 1354 + /* __arm_lpae_alloc_pages() merely needs dev_to_node() to work */ 1355 + set_dev_node(&dev, NUMA_NO_NODE); 1287 1356 1288 1357 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { 1289 1358 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
+469
drivers/iommu/io-pgtable-dart.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Apple DART page table allocator. 4 + * 5 + * Copyright (C) 2022 The Asahi Linux Contributors 6 + * 7 + * Based on io-pgtable-arm. 8 + * 9 + * Copyright (C) 2014 ARM Limited 10 + * 11 + * Author: Will Deacon <will.deacon@arm.com> 12 + */ 13 + 14 + #define pr_fmt(fmt) "dart io-pgtable: " fmt 15 + 16 + #include <linux/atomic.h> 17 + #include <linux/bitfield.h> 18 + #include <linux/bitops.h> 19 + #include <linux/io-pgtable.h> 20 + #include <linux/kernel.h> 21 + #include <linux/sizes.h> 22 + #include <linux/slab.h> 23 + #include <linux/types.h> 24 + 25 + #include <asm/barrier.h> 26 + 27 + #define DART1_MAX_ADDR_BITS 36 28 + 29 + #define DART_MAX_TABLES 4 30 + #define DART_LEVELS 2 31 + 32 + /* Struct accessors */ 33 + #define io_pgtable_to_data(x) \ 34 + container_of((x), struct dart_io_pgtable, iop) 35 + 36 + #define io_pgtable_ops_to_data(x) \ 37 + io_pgtable_to_data(io_pgtable_ops_to_pgtable(x)) 38 + 39 + #define DART_GRANULE(d) \ 40 + (sizeof(dart_iopte) << (d)->bits_per_level) 41 + #define DART_PTES_PER_TABLE(d) \ 42 + (DART_GRANULE(d) >> ilog2(sizeof(dart_iopte))) 43 + 44 + #define APPLE_DART_PTE_SUBPAGE_START GENMASK_ULL(63, 52) 45 + #define APPLE_DART_PTE_SUBPAGE_END GENMASK_ULL(51, 40) 46 + 47 + #define APPLE_DART1_PADDR_MASK GENMASK_ULL(35, 12) 48 + #define APPLE_DART2_PADDR_MASK GENMASK_ULL(37, 10) 49 + #define APPLE_DART2_PADDR_SHIFT (4) 50 + 51 + /* Apple DART1 protection bits */ 52 + #define APPLE_DART1_PTE_PROT_NO_READ BIT(8) 53 + #define APPLE_DART1_PTE_PROT_NO_WRITE BIT(7) 54 + #define APPLE_DART1_PTE_PROT_SP_DIS BIT(1) 55 + 56 + /* Apple DART2 protection bits */ 57 + #define APPLE_DART2_PTE_PROT_NO_READ BIT(3) 58 + #define APPLE_DART2_PTE_PROT_NO_WRITE BIT(2) 59 + #define APPLE_DART2_PTE_PROT_NO_CACHE BIT(1) 60 + 61 + /* marks PTE as valid */ 62 + #define APPLE_DART_PTE_VALID BIT(0) 63 + 64 + /* IOPTE accessors */ 65 + #define iopte_deref(pte, d) __va(iopte_to_paddr(pte, d)) 66 + 67 + struct dart_io_pgtable { 68 + struct io_pgtable iop; 69 + 70 + int tbl_bits; 71 + int bits_per_level; 72 + 73 + void *pgd[DART_MAX_TABLES]; 74 + }; 75 + 76 + typedef u64 dart_iopte; 77 + 78 + 79 + static dart_iopte paddr_to_iopte(phys_addr_t paddr, 80 + struct dart_io_pgtable *data) 81 + { 82 + dart_iopte pte; 83 + 84 + if (data->iop.fmt == APPLE_DART) 85 + return paddr & APPLE_DART1_PADDR_MASK; 86 + 87 + /* format is APPLE_DART2 */ 88 + pte = paddr >> APPLE_DART2_PADDR_SHIFT; 89 + pte &= APPLE_DART2_PADDR_MASK; 90 + 91 + return pte; 92 + } 93 + 94 + static phys_addr_t iopte_to_paddr(dart_iopte pte, 95 + struct dart_io_pgtable *data) 96 + { 97 + u64 paddr; 98 + 99 + if (data->iop.fmt == APPLE_DART) 100 + return pte & APPLE_DART1_PADDR_MASK; 101 + 102 + /* format is APPLE_DART2 */ 103 + paddr = pte & APPLE_DART2_PADDR_MASK; 104 + paddr <<= APPLE_DART2_PADDR_SHIFT; 105 + 106 + return paddr; 107 + } 108 + 109 + static void *__dart_alloc_pages(size_t size, gfp_t gfp, 110 + struct io_pgtable_cfg *cfg) 111 + { 112 + int order = get_order(size); 113 + struct page *p; 114 + 115 + VM_BUG_ON((gfp & __GFP_HIGHMEM)); 116 + p = alloc_pages(gfp | __GFP_ZERO, order); 117 + if (!p) 118 + return NULL; 119 + 120 + return page_address(p); 121 + } 122 + 123 + static int dart_init_pte(struct dart_io_pgtable *data, 124 + unsigned long iova, phys_addr_t paddr, 125 + dart_iopte prot, int num_entries, 126 + dart_iopte *ptep) 127 + { 128 + int i; 129 + dart_iopte pte = prot; 130 + size_t sz = data->iop.cfg.pgsize_bitmap; 131 + 132 + for (i = 0; i < num_entries; i++) 133 + if (ptep[i] & APPLE_DART_PTE_VALID) { 134 + /* We require an unmap first */ 135 + WARN_ON(ptep[i] & APPLE_DART_PTE_VALID); 136 + return -EEXIST; 137 + } 138 + 139 + /* subpage protection: always allow access to the entire page */ 140 + pte |= FIELD_PREP(APPLE_DART_PTE_SUBPAGE_START, 0); 141 + pte |= FIELD_PREP(APPLE_DART_PTE_SUBPAGE_END, 0xfff); 142 + 143 + pte |= APPLE_DART1_PTE_PROT_SP_DIS; 144 + pte |= APPLE_DART_PTE_VALID; 145 + 146 + for (i = 0; i < num_entries; i++) 147 + ptep[i] = pte | paddr_to_iopte(paddr + i * sz, data); 148 + 149 + return 0; 150 + } 151 + 152 + static dart_iopte dart_install_table(dart_iopte *table, 153 + dart_iopte *ptep, 154 + dart_iopte curr, 155 + struct dart_io_pgtable *data) 156 + { 157 + dart_iopte old, new; 158 + 159 + new = paddr_to_iopte(__pa(table), data) | APPLE_DART_PTE_VALID; 160 + 161 + /* 162 + * Ensure the table itself is visible before its PTE can be. 163 + * Whilst we could get away with cmpxchg64_release below, this 164 + * doesn't have any ordering semantics when !CONFIG_SMP. 165 + */ 166 + dma_wmb(); 167 + 168 + old = cmpxchg64_relaxed(ptep, curr, new); 169 + 170 + return old; 171 + } 172 + 173 + static int dart_get_table(struct dart_io_pgtable *data, unsigned long iova) 174 + { 175 + return (iova >> (3 * data->bits_per_level + ilog2(sizeof(dart_iopte)))) & 176 + ((1 << data->tbl_bits) - 1); 177 + } 178 + 179 + static int dart_get_l1_index(struct dart_io_pgtable *data, unsigned long iova) 180 + { 181 + 182 + return (iova >> (2 * data->bits_per_level + ilog2(sizeof(dart_iopte)))) & 183 + ((1 << data->bits_per_level) - 1); 184 + } 185 + 186 + static int dart_get_l2_index(struct dart_io_pgtable *data, unsigned long iova) 187 + { 188 + 189 + return (iova >> (data->bits_per_level + ilog2(sizeof(dart_iopte)))) & 190 + ((1 << data->bits_per_level) - 1); 191 + } 192 + 193 + static dart_iopte *dart_get_l2(struct dart_io_pgtable *data, unsigned long iova) 194 + { 195 + dart_iopte pte, *ptep; 196 + int tbl = dart_get_table(data, iova); 197 + 198 + ptep = data->pgd[tbl]; 199 + if (!ptep) 200 + return NULL; 201 + 202 + ptep += dart_get_l1_index(data, iova); 203 + pte = READ_ONCE(*ptep); 204 + 205 + /* Valid entry? */ 206 + if (!pte) 207 + return NULL; 208 + 209 + /* Deref to get level 2 table */ 210 + return iopte_deref(pte, data); 211 + } 212 + 213 + static dart_iopte dart_prot_to_pte(struct dart_io_pgtable *data, 214 + int prot) 215 + { 216 + dart_iopte pte = 0; 217 + 218 + if (data->iop.fmt == APPLE_DART) { 219 + if (!(prot & IOMMU_WRITE)) 220 + pte |= APPLE_DART1_PTE_PROT_NO_WRITE; 221 + if (!(prot & IOMMU_READ)) 222 + pte |= APPLE_DART1_PTE_PROT_NO_READ; 223 + } 224 + if (data->iop.fmt == APPLE_DART2) { 225 + if (!(prot & IOMMU_WRITE)) 226 + pte |= APPLE_DART2_PTE_PROT_NO_WRITE; 227 + if (!(prot & IOMMU_READ)) 228 + pte |= APPLE_DART2_PTE_PROT_NO_READ; 229 + if (!(prot & IOMMU_CACHE)) 230 + pte |= APPLE_DART2_PTE_PROT_NO_CACHE; 231 + } 232 + 233 + return pte; 234 + } 235 + 236 + static int dart_map_pages(struct io_pgtable_ops *ops, unsigned long iova, 237 + phys_addr_t paddr, size_t pgsize, size_t pgcount, 238 + int iommu_prot, gfp_t gfp, size_t *mapped) 239 + { 240 + struct dart_io_pgtable *data = io_pgtable_ops_to_data(ops); 241 + struct io_pgtable_cfg *cfg = &data->iop.cfg; 242 + size_t tblsz = DART_GRANULE(data); 243 + int ret = 0, tbl, num_entries, max_entries, map_idx_start; 244 + dart_iopte pte, *cptep, *ptep; 245 + dart_iopte prot; 246 + 247 + if (WARN_ON(pgsize != cfg->pgsize_bitmap)) 248 + return -EINVAL; 249 + 250 + if (WARN_ON(paddr >> cfg->oas)) 251 + return -ERANGE; 252 + 253 + /* If no access, then nothing to do */ 254 + if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) 255 + return 0; 256 + 257 + tbl = dart_get_table(data, iova); 258 + 259 + ptep = data->pgd[tbl]; 260 + ptep += dart_get_l1_index(data, iova); 261 + pte = READ_ONCE(*ptep); 262 + 263 + /* no L2 table present */ 264 + if (!pte) { 265 + cptep = __dart_alloc_pages(tblsz, gfp, cfg); 266 + if (!cptep) 267 + return -ENOMEM; 268 + 269 + pte = dart_install_table(cptep, ptep, 0, data); 270 + if (pte) 271 + free_pages((unsigned long)cptep, get_order(tblsz)); 272 + 273 + /* L2 table is present (now) */ 274 + pte = READ_ONCE(*ptep); 275 + } 276 + 277 + ptep = iopte_deref(pte, data); 278 + 279 + /* install a leaf entries into L2 table */ 280 + prot = dart_prot_to_pte(data, iommu_prot); 281 + map_idx_start = dart_get_l2_index(data, iova); 282 + max_entries = DART_PTES_PER_TABLE(data) - map_idx_start; 283 + num_entries = min_t(int, pgcount, max_entries); 284 + ptep += map_idx_start; 285 + ret = dart_init_pte(data, iova, paddr, prot, num_entries, ptep); 286 + if (!ret && mapped) 287 + *mapped += num_entries * pgsize; 288 + 289 + /* 290 + * Synchronise all PTE updates for the new mapping before there's 291 + * a chance for anything to kick off a table walk for the new iova. 292 + */ 293 + wmb(); 294 + 295 + return ret; 296 + } 297 + 298 + static size_t dart_unmap_pages(struct io_pgtable_ops *ops, unsigned long iova, 299 + size_t pgsize, size_t pgcount, 300 + struct iommu_iotlb_gather *gather) 301 + { 302 + struct dart_io_pgtable *data = io_pgtable_ops_to_data(ops); 303 + struct io_pgtable_cfg *cfg = &data->iop.cfg; 304 + int i = 0, num_entries, max_entries, unmap_idx_start; 305 + dart_iopte pte, *ptep; 306 + 307 + if (WARN_ON(pgsize != cfg->pgsize_bitmap || !pgcount)) 308 + return 0; 309 + 310 + ptep = dart_get_l2(data, iova); 311 + 312 + /* Valid L2 IOPTE pointer? */ 313 + if (WARN_ON(!ptep)) 314 + return 0; 315 + 316 + unmap_idx_start = dart_get_l2_index(data, iova); 317 + ptep += unmap_idx_start; 318 + 319 + max_entries = DART_PTES_PER_TABLE(data) - unmap_idx_start; 320 + num_entries = min_t(int, pgcount, max_entries); 321 + 322 + while (i < num_entries) { 323 + pte = READ_ONCE(*ptep); 324 + if (WARN_ON(!pte)) 325 + break; 326 + 327 + /* clear pte */ 328 + *ptep = 0; 329 + 330 + if (!iommu_iotlb_gather_queued(gather)) 331 + io_pgtable_tlb_add_page(&data->iop, gather, 332 + iova + i * pgsize, pgsize); 333 + 334 + ptep++; 335 + i++; 336 + } 337 + 338 + return i * pgsize; 339 + } 340 + 341 + static phys_addr_t dart_iova_to_phys(struct io_pgtable_ops *ops, 342 + unsigned long iova) 343 + { 344 + struct dart_io_pgtable *data = io_pgtable_ops_to_data(ops); 345 + dart_iopte pte, *ptep; 346 + 347 + ptep = dart_get_l2(data, iova); 348 + 349 + /* Valid L2 IOPTE pointer? */ 350 + if (!ptep) 351 + return 0; 352 + 353 + ptep += dart_get_l2_index(data, iova); 354 + 355 + pte = READ_ONCE(*ptep); 356 + /* Found translation */ 357 + if (pte) { 358 + iova &= (data->iop.cfg.pgsize_bitmap - 1); 359 + return iopte_to_paddr(pte, data) | iova; 360 + } 361 + 362 + /* Ran out of page tables to walk */ 363 + return 0; 364 + } 365 + 366 + static struct dart_io_pgtable * 367 + dart_alloc_pgtable(struct io_pgtable_cfg *cfg) 368 + { 369 + struct dart_io_pgtable *data; 370 + int tbl_bits, bits_per_level, va_bits, pg_shift; 371 + 372 + pg_shift = __ffs(cfg->pgsize_bitmap); 373 + bits_per_level = pg_shift - ilog2(sizeof(dart_iopte)); 374 + 375 + va_bits = cfg->ias - pg_shift; 376 + 377 + tbl_bits = max_t(int, 0, va_bits - (bits_per_level * DART_LEVELS)); 378 + if ((1 << tbl_bits) > DART_MAX_TABLES) 379 + return NULL; 380 + 381 + data = kzalloc(sizeof(*data), GFP_KERNEL); 382 + if (!data) 383 + return NULL; 384 + 385 + data->tbl_bits = tbl_bits; 386 + data->bits_per_level = bits_per_level; 387 + 388 + data->iop.ops = (struct io_pgtable_ops) { 389 + .map_pages = dart_map_pages, 390 + .unmap_pages = dart_unmap_pages, 391 + .iova_to_phys = dart_iova_to_phys, 392 + }; 393 + 394 + return data; 395 + } 396 + 397 + static struct io_pgtable * 398 + apple_dart_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) 399 + { 400 + struct dart_io_pgtable *data; 401 + int i; 402 + 403 + if (!cfg->coherent_walk) 404 + return NULL; 405 + 406 + if (cfg->oas != 36 && cfg->oas != 42) 407 + return NULL; 408 + 409 + if (cfg->ias > cfg->oas) 410 + return NULL; 411 + 412 + if (!(cfg->pgsize_bitmap == SZ_4K || cfg->pgsize_bitmap == SZ_16K)) 413 + return NULL; 414 + 415 + data = dart_alloc_pgtable(cfg); 416 + if (!data) 417 + return NULL; 418 + 419 + cfg->apple_dart_cfg.n_ttbrs = 1 << data->tbl_bits; 420 + 421 + for (i = 0; i < cfg->apple_dart_cfg.n_ttbrs; ++i) { 422 + data->pgd[i] = __dart_alloc_pages(DART_GRANULE(data), GFP_KERNEL, 423 + cfg); 424 + if (!data->pgd[i]) 425 + goto out_free_data; 426 + cfg->apple_dart_cfg.ttbr[i] = virt_to_phys(data->pgd[i]); 427 + } 428 + 429 + return &data->iop; 430 + 431 + out_free_data: 432 + while (--i >= 0) 433 + free_pages((unsigned long)data->pgd[i], 434 + get_order(DART_GRANULE(data))); 435 + kfree(data); 436 + return NULL; 437 + } 438 + 439 + static void apple_dart_free_pgtable(struct io_pgtable *iop) 440 + { 441 + struct dart_io_pgtable *data = io_pgtable_to_data(iop); 442 + dart_iopte *ptep, *end; 443 + int i; 444 + 445 + for (i = 0; i < (1 << data->tbl_bits) && data->pgd[i]; ++i) { 446 + ptep = data->pgd[i]; 447 + end = (void *)ptep + DART_GRANULE(data); 448 + 449 + while (ptep != end) { 450 + dart_iopte pte = *ptep++; 451 + 452 + if (pte) { 453 + unsigned long page = 454 + (unsigned long)iopte_deref(pte, data); 455 + 456 + free_pages(page, get_order(DART_GRANULE(data))); 457 + } 458 + } 459 + free_pages((unsigned long)data->pgd[i], 460 + get_order(DART_GRANULE(data))); 461 + } 462 + 463 + kfree(data); 464 + } 465 + 466 + struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns = { 467 + .alloc = apple_dart_alloc_pgtable, 468 + .free = apple_dart_free_pgtable, 469 + };
+4
drivers/iommu/io-pgtable.c
··· 20 20 [ARM_64_LPAE_S1] = &io_pgtable_arm_64_lpae_s1_init_fns, 21 21 [ARM_64_LPAE_S2] = &io_pgtable_arm_64_lpae_s2_init_fns, 22 22 [ARM_MALI_LPAE] = &io_pgtable_arm_mali_lpae_init_fns, 23 + #endif 24 + #ifdef CONFIG_IOMMU_IO_PGTABLE_DART 23 25 [APPLE_DART] = &io_pgtable_apple_dart_init_fns, 26 + [APPLE_DART2] = &io_pgtable_apple_dart_init_fns, 24 27 #endif 25 28 #ifdef CONFIG_IOMMU_IO_PGTABLE_ARMV7S 26 29 [ARM_V7S] = &io_pgtable_arm_v7s_init_fns, 27 30 #endif 28 31 #ifdef CONFIG_AMD_IOMMU 29 32 [AMD_IOMMU_V1] = &io_pgtable_amd_iommu_v1_init_fns, 33 + [AMD_IOMMU_V2] = &io_pgtable_amd_iommu_v2_init_fns, 30 34 #endif 31 35 }; 32 36
+64 -90
drivers/iommu/iommu.c
··· 6 6 7 7 #define pr_fmt(fmt) "iommu: " fmt 8 8 9 + #include <linux/amba/bus.h> 9 10 #include <linux/device.h> 10 - #include <linux/dma-iommu.h> 11 11 #include <linux/kernel.h> 12 12 #include <linux/bits.h> 13 13 #include <linux/bug.h> ··· 16 16 #include <linux/export.h> 17 17 #include <linux/slab.h> 18 18 #include <linux/errno.h> 19 + #include <linux/host1x_context_bus.h> 19 20 #include <linux/iommu.h> 20 21 #include <linux/idr.h> 21 22 #include <linux/err.h> 22 23 #include <linux/pci.h> 23 24 #include <linux/bitops.h> 25 + #include <linux/platform_device.h> 24 26 #include <linux/property.h> 25 27 #include <linux/fsl/mc.h> 26 28 #include <linux/module.h> 27 29 #include <linux/cc_platform.h> 28 30 #include <trace/events/iommu.h> 31 + 32 + #include "dma-iommu.h" 29 33 30 34 static struct kset *iommu_group_kset; 31 35 static DEFINE_IDA(iommu_group_ida); ··· 79 75 #define IOMMU_CMD_LINE_DMA_API BIT(0) 80 76 #define IOMMU_CMD_LINE_STRICT BIT(1) 81 77 78 + static int iommu_bus_notifier(struct notifier_block *nb, 79 + unsigned long action, void *data); 82 80 static int iommu_alloc_default_domain(struct iommu_group *group, 83 81 struct device *dev); 84 82 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, ··· 109 103 static LIST_HEAD(iommu_device_list); 110 104 static DEFINE_SPINLOCK(iommu_device_lock); 111 105 106 + static struct bus_type * const iommu_buses[] = { 107 + &platform_bus_type, 108 + #ifdef CONFIG_PCI 109 + &pci_bus_type, 110 + #endif 111 + #ifdef CONFIG_ARM_AMBA 112 + &amba_bustype, 113 + #endif 114 + #ifdef CONFIG_FSL_MC_BUS 115 + &fsl_mc_bus_type, 116 + #endif 117 + #ifdef CONFIG_TEGRA_HOST1X_CONTEXT_BUS 118 + &host1x_context_device_bus_type, 119 + #endif 120 + }; 121 + 112 122 /* 113 123 * Use a function instead of an array here because the domain-type is a 114 124 * bit-field, so an array would waste memory. ··· 148 126 149 127 static int __init iommu_subsys_init(void) 150 128 { 129 + struct notifier_block *nb; 130 + 151 131 if (!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API)) { 152 132 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH)) 153 133 iommu_set_default_passthrough(false); ··· 176 152 (iommu_cmd_line & IOMMU_CMD_LINE_STRICT) ? 177 153 "(set via kernel command line)" : ""); 178 154 155 + nb = kcalloc(ARRAY_SIZE(iommu_buses), sizeof(*nb), GFP_KERNEL); 156 + if (!nb) 157 + return -ENOMEM; 158 + 159 + for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) { 160 + nb[i].notifier_call = iommu_bus_notifier; 161 + bus_register_notifier(iommu_buses[i], &nb[i]); 162 + } 163 + 179 164 return 0; 180 165 } 181 166 subsys_initcall(iommu_subsys_init); 167 + 168 + static int remove_iommu_group(struct device *dev, void *data) 169 + { 170 + if (dev->iommu && dev->iommu->iommu_dev == data) 171 + iommu_release_device(dev); 172 + 173 + return 0; 174 + } 182 175 183 176 /** 184 177 * iommu_device_register() - Register an IOMMU hardware instance ··· 208 167 int iommu_device_register(struct iommu_device *iommu, 209 168 const struct iommu_ops *ops, struct device *hwdev) 210 169 { 170 + int err = 0; 171 + 211 172 /* We need to be able to take module references appropriately */ 212 173 if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner)) 213 174 return -EINVAL; 175 + /* 176 + * Temporarily enforce global restriction to a single driver. This was 177 + * already the de-facto behaviour, since any possible combination of 178 + * existing drivers would compete for at least the PCI or platform bus. 179 + */ 180 + if (iommu_buses[0]->iommu_ops && iommu_buses[0]->iommu_ops != ops) 181 + return -EBUSY; 214 182 215 183 iommu->ops = ops; 216 184 if (hwdev) 217 - iommu->fwnode = hwdev->fwnode; 185 + iommu->fwnode = dev_fwnode(hwdev); 218 186 219 187 spin_lock(&iommu_device_lock); 220 188 list_add_tail(&iommu->list, &iommu_device_list); 221 189 spin_unlock(&iommu_device_lock); 222 - return 0; 190 + 191 + for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++) { 192 + iommu_buses[i]->iommu_ops = ops; 193 + err = bus_iommu_probe(iommu_buses[i]); 194 + } 195 + if (err) 196 + iommu_device_unregister(iommu); 197 + return err; 223 198 } 224 199 EXPORT_SYMBOL_GPL(iommu_device_register); 225 200 226 201 void iommu_device_unregister(struct iommu_device *iommu) 227 202 { 203 + for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) 204 + bus_for_each_dev(iommu_buses[i], NULL, iommu, remove_iommu_group); 205 + 228 206 spin_lock(&iommu_device_lock); 229 207 list_del(&iommu->list); 230 208 spin_unlock(&iommu_device_lock); ··· 714 654 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype, 715 655 NULL, "%d", group->id); 716 656 if (ret) { 717 - ida_free(&iommu_group_ida, group->id); 718 657 kobject_put(&group->kobj); 719 658 return ERR_PTR(ret); 720 659 } ··· 1671 1612 return ret; 1672 1613 } 1673 1614 1674 - static int remove_iommu_group(struct device *dev, void *data) 1675 - { 1676 - iommu_release_device(dev); 1677 - 1678 - return 0; 1679 - } 1680 - 1681 1615 static int iommu_bus_notifier(struct notifier_block *nb, 1682 1616 unsigned long action, void *data) 1683 1617 { ··· 1827 1775 return ret; 1828 1776 } 1829 1777 1830 - static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops) 1831 - { 1832 - struct notifier_block *nb; 1833 - int err; 1834 - 1835 - nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL); 1836 - if (!nb) 1837 - return -ENOMEM; 1838 - 1839 - nb->notifier_call = iommu_bus_notifier; 1840 - 1841 - err = bus_register_notifier(bus, nb); 1842 - if (err) 1843 - goto out_free; 1844 - 1845 - err = bus_iommu_probe(bus); 1846 - if (err) 1847 - goto out_err; 1848 - 1849 - 1850 - return 0; 1851 - 1852 - out_err: 1853 - /* Clean up */ 1854 - bus_for_each_dev(bus, NULL, NULL, remove_iommu_group); 1855 - bus_unregister_notifier(bus, nb); 1856 - 1857 - out_free: 1858 - kfree(nb); 1859 - 1860 - return err; 1861 - } 1862 - 1863 - /** 1864 - * bus_set_iommu - set iommu-callbacks for the bus 1865 - * @bus: bus. 1866 - * @ops: the callbacks provided by the iommu-driver 1867 - * 1868 - * This function is called by an iommu driver to set the iommu methods 1869 - * used for a particular bus. Drivers for devices on that bus can use 1870 - * the iommu-api after these ops are registered. 1871 - * This special function is needed because IOMMUs are usually devices on 1872 - * the bus itself, so the iommu drivers are not initialized when the bus 1873 - * is set up. With this function the iommu-driver can set the iommu-ops 1874 - * afterwards. 1875 - */ 1876 - int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops) 1877 - { 1878 - int err; 1879 - 1880 - if (ops == NULL) { 1881 - bus->iommu_ops = NULL; 1882 - return 0; 1883 - } 1884 - 1885 - if (bus->iommu_ops != NULL) 1886 - return -EBUSY; 1887 - 1888 - bus->iommu_ops = ops; 1889 - 1890 - /* Do IOMMU specific setup for this bus-type */ 1891 - err = iommu_bus_init(bus, ops); 1892 - if (err) 1893 - bus->iommu_ops = NULL; 1894 - 1895 - return err; 1896 - } 1897 - EXPORT_SYMBOL_GPL(bus_set_iommu); 1898 - 1899 1778 bool iommu_present(struct bus_type *bus) 1900 1779 { 1901 1780 return bus->iommu_ops != NULL; ··· 1852 1869 if (!ops->capable) 1853 1870 return false; 1854 1871 1855 - return ops->capable(cap); 1872 + return ops->capable(dev, cap); 1856 1873 } 1857 1874 EXPORT_SYMBOL_GPL(device_iommu_capable); 1858 - 1859 - bool iommu_capable(struct bus_type *bus, enum iommu_cap cap) 1860 - { 1861 - if (!bus->iommu_ops || !bus->iommu_ops->capable) 1862 - return false; 1863 - 1864 - return bus->iommu_ops->capable(cap); 1865 - } 1866 - EXPORT_SYMBOL_GPL(iommu_capable); 1867 1875 1868 1876 /** 1869 1877 * iommu_set_fault_handler() - set a fault handler for an iommu domain
+3 -10
drivers/iommu/iova.c
··· 661 661 unsigned long flags; 662 662 int i; 663 663 664 - if (!mag) 665 - return; 666 - 667 664 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); 668 665 669 666 for (i = 0 ; i < mag->size; ++i) { ··· 680 683 681 684 static bool iova_magazine_full(struct iova_magazine *mag) 682 685 { 683 - return (mag && mag->size == IOVA_MAG_SIZE); 686 + return mag->size == IOVA_MAG_SIZE; 684 687 } 685 688 686 689 static bool iova_magazine_empty(struct iova_magazine *mag) 687 690 { 688 - return (!mag || mag->size == 0); 691 + return mag->size == 0; 689 692 } 690 693 691 694 static unsigned long iova_magazine_pop(struct iova_magazine *mag, ··· 693 696 { 694 697 int i; 695 698 unsigned long pfn; 696 - 697 - BUG_ON(iova_magazine_empty(mag)); 698 699 699 700 /* Only fall back to the rbtree if we have no suitable pfns at all */ 700 701 for (i = mag->size - 1; mag->pfns[i] > limit_pfn; i--) ··· 708 713 709 714 static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn) 710 715 { 711 - BUG_ON(iova_magazine_full(mag)); 712 - 713 716 mag->pfns[mag->size++] = pfn; 714 717 } 715 718 ··· 875 882 { 876 883 unsigned int log_size = order_base_2(size); 877 884 878 - if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE || !iovad->rcaches) 885 + if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE) 879 886 return 0; 880 887 881 888 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);
+1 -34
drivers/iommu/ipmmu-vmsa.c
··· 1090 1090 ret = iommu_device_register(&mmu->iommu, &ipmmu_ops, &pdev->dev); 1091 1091 if (ret) 1092 1092 return ret; 1093 - 1094 - #if defined(CONFIG_IOMMU_DMA) 1095 - if (!iommu_present(&platform_bus_type)) 1096 - bus_set_iommu(&platform_bus_type, &ipmmu_ops); 1097 - #endif 1098 1093 } 1099 1094 1100 1095 /* ··· 1163 1168 .probe = ipmmu_probe, 1164 1169 .remove = ipmmu_remove, 1165 1170 }; 1166 - 1167 - static int __init ipmmu_init(void) 1168 - { 1169 - struct device_node *np; 1170 - static bool setup_done; 1171 - int ret; 1172 - 1173 - if (setup_done) 1174 - return 0; 1175 - 1176 - np = of_find_matching_node(NULL, ipmmu_of_ids); 1177 - if (!np) 1178 - return 0; 1179 - 1180 - of_node_put(np); 1181 - 1182 - ret = platform_driver_register(&ipmmu_driver); 1183 - if (ret < 0) 1184 - return ret; 1185 - 1186 - #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA) 1187 - if (!iommu_present(&platform_bus_type)) 1188 - bus_set_iommu(&platform_bus_type, &ipmmu_ops); 1189 - #endif 1190 - 1191 - setup_done = true; 1192 - return 0; 1193 - } 1194 - subsys_initcall(ipmmu_init); 1171 + builtin_platform_driver(ipmmu_driver);
-2
drivers/iommu/msm_iommu.c
··· 792 792 goto fail; 793 793 } 794 794 795 - bus_set_iommu(&platform_bus_type, &msm_iommu_ops); 796 - 797 795 pr_info("device mapped at %p, irq %d with %d ctx banks\n", 798 796 iommu->base, iommu->irq, iommu->ncb); 799 797
+20 -25
drivers/iommu/mtk_iommu.c
··· 138 138 #define PM_CLK_AO BIT(15) 139 139 #define IFA_IOMMU_PCIE_SUPPORT BIT(16) 140 140 #define PGTABLE_PA_35_EN BIT(17) 141 + #define TF_PORT_TO_ADDR_MT8173 BIT(18) 141 142 142 143 #define MTK_IOMMU_HAS_FLAG_MASK(pdata, _x, mask) \ 143 144 ((((pdata)->flags) & (mask)) == (_x)) ··· 158 157 enum mtk_iommu_plat { 159 158 M4U_MT2712, 160 159 M4U_MT6779, 160 + M4U_MT6795, 161 161 M4U_MT8167, 162 162 M4U_MT8173, 163 163 M4U_MT8183, ··· 957 955 * Global control settings are in bank0. May re-init these global registers 958 956 * since no sure if there is bank0 consumers. 959 957 */ 960 - if (data->plat_data->m4u_plat == M4U_MT8173) { 958 + if (MTK_IOMMU_HAS_FLAG(data->plat_data, TF_PORT_TO_ADDR_MT8173)) { 961 959 regval = F_MMU_PREFETCH_RT_REPLACE_MOD | 962 960 F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173; 963 961 } else { ··· 1245 1243 data->hw_list = &data->hw_list_head; 1246 1244 } 1247 1245 1248 - if (!iommu_present(&platform_bus_type)) { 1249 - ret = bus_set_iommu(&platform_bus_type, &mtk_iommu_ops); 1250 - if (ret) 1251 - goto out_list_del; 1252 - } 1253 - 1254 1246 if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) { 1255 1247 ret = component_master_add_with_match(dev, &mtk_iommu_com_ops, match); 1256 1248 if (ret) 1257 - goto out_bus_set_null; 1258 - } else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA) && 1259 - MTK_IOMMU_HAS_FLAG(data->plat_data, IFA_IOMMU_PCIE_SUPPORT)) { 1260 - #ifdef CONFIG_PCI 1261 - if (!iommu_present(&pci_bus_type)) { 1262 - ret = bus_set_iommu(&pci_bus_type, &mtk_iommu_ops); 1263 - if (ret) /* PCIe fail don't affect platform_bus. */ 1264 - goto out_list_del; 1265 - } 1266 - #endif 1249 + goto out_list_del; 1267 1250 } 1268 1251 return ret; 1269 1252 1270 - out_bus_set_null: 1271 - bus_set_iommu(&platform_bus_type, NULL); 1272 1253 out_list_del: 1273 1254 list_del(&data->list); 1274 1255 iommu_device_unregister(&data->iommu); ··· 1279 1294 if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) { 1280 1295 device_link_remove(data->smicomm_dev, &pdev->dev); 1281 1296 component_master_del(&pdev->dev, &mtk_iommu_com_ops); 1282 - } else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA) && 1283 - MTK_IOMMU_HAS_FLAG(data->plat_data, IFA_IOMMU_PCIE_SUPPORT)) { 1284 - #ifdef CONFIG_PCI 1285 - bus_set_iommu(&pci_bus_type, NULL); 1286 - #endif 1287 1297 } 1288 1298 pm_runtime_disable(&pdev->dev); 1289 1299 for (i = 0; i < data->plat_data->banks_num; i++) { ··· 1393 1413 .larbid_remap = {{0}, {1}, {2}, {3}, {5}, {7, 8}, {10}, {9}}, 1394 1414 }; 1395 1415 1416 + static const struct mtk_iommu_plat_data mt6795_data = { 1417 + .m4u_plat = M4U_MT6795, 1418 + .flags = HAS_4GB_MODE | HAS_BCLK | RESET_AXI | 1419 + HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM | 1420 + TF_PORT_TO_ADDR_MT8173, 1421 + .inv_sel_reg = REG_MMU_INV_SEL_GEN1, 1422 + .banks_num = 1, 1423 + .banks_enable = {true}, 1424 + .iova_region = single_domain, 1425 + .iova_region_nr = ARRAY_SIZE(single_domain), 1426 + .larbid_remap = {{0}, {1}, {2}, {3}, {4}}, /* Linear mapping. */ 1427 + }; 1428 + 1396 1429 static const struct mtk_iommu_plat_data mt8167_data = { 1397 1430 .m4u_plat = M4U_MT8167, 1398 1431 .flags = RESET_AXI | HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM, ··· 1420 1427 static const struct mtk_iommu_plat_data mt8173_data = { 1421 1428 .m4u_plat = M4U_MT8173, 1422 1429 .flags = HAS_4GB_MODE | HAS_BCLK | RESET_AXI | 1423 - HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM, 1430 + HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM | 1431 + TF_PORT_TO_ADDR_MT8173, 1424 1432 .inv_sel_reg = REG_MMU_INV_SEL_GEN1, 1425 1433 .banks_num = 1, 1426 1434 .banks_enable = {true}, ··· 1518 1524 static const struct of_device_id mtk_iommu_of_ids[] = { 1519 1525 { .compatible = "mediatek,mt2712-m4u", .data = &mt2712_data}, 1520 1526 { .compatible = "mediatek,mt6779-m4u", .data = &mt6779_data}, 1527 + { .compatible = "mediatek,mt6795-m4u", .data = &mt6795_data}, 1521 1528 { .compatible = "mediatek,mt8167-m4u", .data = &mt8167_data}, 1522 1529 { .compatible = "mediatek,mt8173-m4u", .data = &mt8173_data}, 1523 1530 { .compatible = "mediatek,mt8183-m4u", .data = &mt8183_data},
+1 -12
drivers/iommu/mtk_iommu_v1.c
··· 691 691 if (ret) 692 692 goto out_sysfs_remove; 693 693 694 - if (!iommu_present(&platform_bus_type)) { 695 - ret = bus_set_iommu(&platform_bus_type, &mtk_iommu_v1_ops); 696 - if (ret) 697 - goto out_dev_unreg; 698 - } 699 - 700 694 ret = component_master_add_with_match(dev, &mtk_iommu_v1_com_ops, match); 701 695 if (ret) 702 - goto out_bus_set_null; 696 + goto out_dev_unreg; 703 697 return ret; 704 698 705 - out_bus_set_null: 706 - bus_set_iommu(&platform_bus_type, NULL); 707 699 out_dev_unreg: 708 700 iommu_device_unregister(&data->iommu); 709 701 out_sysfs_remove: ··· 709 717 710 718 iommu_device_sysfs_remove(&data->iommu); 711 719 iommu_device_unregister(&data->iommu); 712 - 713 - if (iommu_present(&platform_bus_type)) 714 - bus_set_iommu(&platform_bus_type, NULL); 715 720 716 721 clk_disable_unprepare(data->bclk); 717 722 devm_free_irq(&pdev->dev, data->irq, data);
+3 -3
drivers/iommu/omap-iommu-debug.c
··· 32 32 ssize_t bytes; \ 33 33 const char *str = "%20s: %08x\n"; \ 34 34 const int maxcol = 32; \ 35 - bytes = snprintf(p, maxcol, str, __stringify(name), \ 35 + if (len < maxcol) \ 36 + goto out; \ 37 + bytes = scnprintf(p, maxcol, str, __stringify(name), \ 36 38 iommu_read_reg(obj, MMU_##name)); \ 37 39 p += bytes; \ 38 40 len -= bytes; \ 39 - if (len < maxcol) \ 40 - goto out; \ 41 41 } while (0) 42 42 43 43 static ssize_t
-6
drivers/iommu/omap-iommu.c
··· 1776 1776 goto fail_driver; 1777 1777 } 1778 1778 1779 - ret = bus_set_iommu(&platform_bus_type, &omap_iommu_ops); 1780 - if (ret) 1781 - goto fail_bus; 1782 - 1783 1779 return 0; 1784 1780 1785 - fail_bus: 1786 - platform_driver_unregister(&omap_iommu_driver); 1787 1781 fail_driver: 1788 1782 kmem_cache_destroy(iopte_cachep); 1789 1783 return ret;
-2
drivers/iommu/rockchip-iommu.c
··· 1300 1300 if (!dma_dev) 1301 1301 dma_dev = &pdev->dev; 1302 1302 1303 - bus_set_iommu(&platform_bus_type, &rk_iommu_ops); 1304 - 1305 1303 pm_runtime_enable(dev); 1306 1304 1307 1305 for (i = 0; i < iommu->num_irq; i++) {
+7 -8
drivers/iommu/s390-iommu.c
··· 39 39 return container_of(dom, struct s390_domain, domain); 40 40 } 41 41 42 - static bool s390_iommu_capable(enum iommu_cap cap) 42 + static bool s390_iommu_capable(struct device *dev, enum iommu_cap cap) 43 43 { 44 44 switch (cap) { 45 45 case IOMMU_CAP_CACHE_COHERENCY: ··· 185 185 186 186 static struct iommu_device *s390_iommu_probe_device(struct device *dev) 187 187 { 188 - struct zpci_dev *zdev = to_zpci_dev(dev); 188 + struct zpci_dev *zdev; 189 + 190 + if (!dev_is_pci(dev)) 191 + return ERR_PTR(-ENODEV); 192 + 193 + zdev = to_zpci_dev(dev); 189 194 190 195 return &zdev->iommu_dev; 191 196 } ··· 390 385 .free = s390_domain_free, 391 386 } 392 387 }; 393 - 394 - static int __init s390_iommu_init(void) 395 - { 396 - return bus_set_iommu(&pci_bus_type, &s390_iommu_ops); 397 - } 398 - subsys_initcall(s390_iommu_init);
-5
drivers/iommu/sprd-iommu.c
··· 496 496 if (ret) 497 497 goto remove_sysfs; 498 498 499 - if (!iommu_present(&platform_bus_type)) 500 - bus_set_iommu(&platform_bus_type, &sprd_iommu_ops); 501 - 502 499 ret = sprd_iommu_clk_enable(sdev); 503 500 if (ret) 504 501 goto unregister_iommu; ··· 530 533 531 534 iommu_group_put(sdev->group); 532 535 sdev->group = NULL; 533 - 534 - bus_set_iommu(&platform_bus_type, NULL); 535 536 536 537 platform_set_drvdata(pdev, NULL); 537 538 iommu_device_sysfs_remove(&sdev->iommu);
-2
drivers/iommu/sun50i-iommu.c
··· 965 965 if (ret < 0) 966 966 goto err_unregister; 967 967 968 - bus_set_iommu(&platform_bus_type, &sun50i_iommu_ops); 969 - 970 968 return 0; 971 969 972 970 err_unregister:
+6 -23
drivers/iommu/tegra-smmu.c
··· 1083 1083 1084 1084 /* 1085 1085 * This is a bit of a hack. Ideally we'd want to simply return this 1086 - * value. However the IOMMU registration process will attempt to add 1087 - * all devices to the IOMMU when bus_set_iommu() is called. In order 1086 + * value. However iommu_device_register() will attempt to add 1087 + * all devices to the IOMMU before we get that far. In order 1088 1088 * not to rely on global variables to track the IOMMU instance, we 1089 1089 * set it here so that it can be looked up from the .probe_device() 1090 1090 * callback via the IOMMU device's .drvdata field. ··· 1138 1138 return ERR_PTR(err); 1139 1139 1140 1140 err = iommu_device_register(&smmu->iommu, &tegra_smmu_ops, dev); 1141 - if (err) 1142 - goto remove_sysfs; 1143 - 1144 - err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops); 1145 - if (err < 0) 1146 - goto unregister; 1147 - 1148 - #ifdef CONFIG_PCI 1149 - err = bus_set_iommu(&pci_bus_type, &tegra_smmu_ops); 1150 - if (err < 0) 1151 - goto unset_platform_bus; 1152 - #endif 1141 + if (err) { 1142 + iommu_device_sysfs_remove(&smmu->iommu); 1143 + return ERR_PTR(err); 1144 + } 1153 1145 1154 1146 if (IS_ENABLED(CONFIG_DEBUG_FS)) 1155 1147 tegra_smmu_debugfs_init(smmu); 1156 1148 1157 1149 return smmu; 1158 - 1159 - unset_platform_bus: __maybe_unused; 1160 - bus_set_iommu(&platform_bus_type, NULL); 1161 - unregister: 1162 - iommu_device_unregister(&smmu->iommu); 1163 - remove_sysfs: 1164 - iommu_device_sysfs_remove(&smmu->iommu); 1165 - 1166 - return ERR_PTR(err); 1167 1150 } 1168 1151 1169 1152 void tegra_smmu_remove(struct tegra_smmu *smmu)
+4 -28
drivers/iommu/virtio-iommu.c
··· 7 7 8 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 9 10 - #include <linux/amba/bus.h> 11 10 #include <linux/delay.h> 12 - #include <linux/dma-iommu.h> 13 11 #include <linux/dma-map-ops.h> 14 12 #include <linux/freezer.h> 15 13 #include <linux/interval_tree.h> ··· 15 17 #include <linux/module.h> 16 18 #include <linux/of_platform.h> 17 19 #include <linux/pci.h> 18 - #include <linux/platform_device.h> 19 20 #include <linux/virtio.h> 20 21 #include <linux/virtio_config.h> 21 22 #include <linux/virtio_ids.h> 22 23 #include <linux/wait.h> 23 24 24 25 #include <uapi/linux/virtio_iommu.h> 26 + 27 + #include "dma-iommu.h" 25 28 26 29 #define MSI_IOVA_BASE 0x8000000 27 30 #define MSI_IOVA_LENGTH 0x100000 ··· 924 925 925 926 static int viommu_match_node(struct device *dev, const void *data) 926 927 { 927 - return dev->parent->fwnode == data; 928 + return device_match_fwnode(dev->parent, data); 928 929 } 929 930 930 931 static struct viommu_dev *viommu_get_by_fwnode(struct fwnode_handle *fwnode) ··· 1005 1006 return iommu_fwspec_add_ids(dev, args->args, 1); 1006 1007 } 1007 1008 1008 - static bool viommu_capable(enum iommu_cap cap) 1009 + static bool viommu_capable(struct device *dev, enum iommu_cap cap) 1009 1010 { 1010 1011 switch (cap) { 1011 1012 case IOMMU_CAP_CACHE_COHERENCY: ··· 1155 1156 1156 1157 iommu_device_register(&viommu->iommu, &viommu_ops, parent_dev); 1157 1158 1158 - #ifdef CONFIG_PCI 1159 - if (pci_bus_type.iommu_ops != &viommu_ops) { 1160 - ret = bus_set_iommu(&pci_bus_type, &viommu_ops); 1161 - if (ret) 1162 - goto err_unregister; 1163 - } 1164 - #endif 1165 - #ifdef CONFIG_ARM_AMBA 1166 - if (amba_bustype.iommu_ops != &viommu_ops) { 1167 - ret = bus_set_iommu(&amba_bustype, &viommu_ops); 1168 - if (ret) 1169 - goto err_unregister; 1170 - } 1171 - #endif 1172 - if (platform_bus_type.iommu_ops != &viommu_ops) { 1173 - ret = bus_set_iommu(&platform_bus_type, &viommu_ops); 1174 - if (ret) 1175 - goto err_unregister; 1176 - } 1177 - 1178 1159 vdev->priv = viommu; 1179 1160 1180 1161 dev_info(dev, "input address: %u bits\n", ··· 1163 1184 1164 1185 return 0; 1165 1186 1166 - err_unregister: 1167 - iommu_device_sysfs_remove(&viommu->iommu); 1168 - iommu_device_unregister(&viommu->iommu); 1169 1187 err_free_vqs: 1170 1188 vdev->config->del_vqs(vdev); 1171 1189
+1 -1
drivers/irqchip/irq-gic-v2m.c
··· 13 13 #define pr_fmt(fmt) "GICv2m: " fmt 14 14 15 15 #include <linux/acpi.h> 16 - #include <linux/dma-iommu.h> 16 + #include <linux/iommu.h> 17 17 #include <linux/irq.h> 18 18 #include <linux/irqdomain.h> 19 19 #include <linux/kernel.h>
+1 -1
drivers/irqchip/irq-gic-v3-its.c
··· 11 11 #include <linux/cpu.h> 12 12 #include <linux/crash_dump.h> 13 13 #include <linux/delay.h> 14 - #include <linux/dma-iommu.h> 15 14 #include <linux/efi.h> 16 15 #include <linux/interrupt.h> 16 + #include <linux/iommu.h> 17 17 #include <linux/iopoll.h> 18 18 #include <linux/irqdomain.h> 19 19 #include <linux/list.h>
+1 -1
drivers/irqchip/irq-gic-v3-mbi.c
··· 6 6 7 7 #define pr_fmt(fmt) "GICv3: " fmt 8 8 9 - #include <linux/dma-iommu.h> 9 + #include <linux/iommu.h> 10 10 #include <linux/irq.h> 11 11 #include <linux/irqdomain.h> 12 12 #include <linux/kernel.h>
+1 -1
drivers/irqchip/irq-ls-scfg-msi.c
··· 11 11 #include <linux/module.h> 12 12 #include <linux/msi.h> 13 13 #include <linux/interrupt.h> 14 + #include <linux/iommu.h> 14 15 #include <linux/irq.h> 15 16 #include <linux/irqchip/chained_irq.h> 16 17 #include <linux/irqdomain.h> ··· 19 18 #include <linux/of_pci.h> 20 19 #include <linux/of_platform.h> 21 20 #include <linux/spinlock.h> 22 - #include <linux/dma-iommu.h> 23 21 24 22 #define MSI_IRQS_PER_MSIR 32 25 23 #define MSI_MSIR_OFFSET 4
-1
drivers/vfio/vfio_iommu_type1.c
··· 37 37 #include <linux/vfio.h> 38 38 #include <linux/workqueue.h> 39 39 #include <linux/notifier.h> 40 - #include <linux/dma-iommu.h> 41 40 #include <linux/irqdomain.h> 42 41 #include "vfio.h" 43 42
+95
include/dt-bindings/memory/mt6795-larb-port.h
··· 1 + /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */ 2 + /* 3 + * Copyright (c) 2022 Collabora Ltd. 4 + * Author: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> 5 + */ 6 + 7 + #ifndef _DT_BINDINGS_MEMORY_MT6795_LARB_PORT_H_ 8 + #define _DT_BINDINGS_MEMORY_MT6795_LARB_PORT_H_ 9 + 10 + #include <dt-bindings/memory/mtk-memory-port.h> 11 + 12 + #define M4U_LARB0_ID 0 13 + #define M4U_LARB1_ID 1 14 + #define M4U_LARB2_ID 2 15 + #define M4U_LARB3_ID 3 16 + #define M4U_LARB4_ID 4 17 + 18 + /* larb0 */ 19 + #define M4U_PORT_DISP_OVL0 MTK_M4U_ID(M4U_LARB0_ID, 0) 20 + #define M4U_PORT_DISP_RDMA0 MTK_M4U_ID(M4U_LARB0_ID, 1) 21 + #define M4U_PORT_DISP_RDMA1 MTK_M4U_ID(M4U_LARB0_ID, 2) 22 + #define M4U_PORT_DISP_WDMA0 MTK_M4U_ID(M4U_LARB0_ID, 3) 23 + #define M4U_PORT_DISP_OVL1 MTK_M4U_ID(M4U_LARB0_ID, 4) 24 + #define M4U_PORT_DISP_RDMA2 MTK_M4U_ID(M4U_LARB0_ID, 5) 25 + #define M4U_PORT_DISP_WDMA1 MTK_M4U_ID(M4U_LARB0_ID, 6) 26 + #define M4U_PORT_DISP_OD_R MTK_M4U_ID(M4U_LARB0_ID, 7) 27 + #define M4U_PORT_DISP_OD_W MTK_M4U_ID(M4U_LARB0_ID, 8) 28 + #define M4U_PORT_MDP_RDMA0 MTK_M4U_ID(M4U_LARB0_ID, 9) 29 + #define M4U_PORT_MDP_RDMA1 MTK_M4U_ID(M4U_LARB0_ID, 10) 30 + #define M4U_PORT_MDP_WDMA MTK_M4U_ID(M4U_LARB0_ID, 11) 31 + #define M4U_PORT_MDP_WROT0 MTK_M4U_ID(M4U_LARB0_ID, 12) 32 + #define M4U_PORT_MDP_WROT1 MTK_M4U_ID(M4U_LARB0_ID, 13) 33 + 34 + /* larb1 */ 35 + #define M4U_PORT_VDEC_MC MTK_M4U_ID(M4U_LARB1_ID, 0) 36 + #define M4U_PORT_VDEC_PP MTK_M4U_ID(M4U_LARB1_ID, 1) 37 + #define M4U_PORT_VDEC_UFO MTK_M4U_ID(M4U_LARB1_ID, 2) 38 + #define M4U_PORT_VDEC_VLD MTK_M4U_ID(M4U_LARB1_ID, 3) 39 + #define M4U_PORT_VDEC_VLD2 MTK_M4U_ID(M4U_LARB1_ID, 4) 40 + #define M4U_PORT_VDEC_AVC_MV MTK_M4U_ID(M4U_LARB1_ID, 5) 41 + #define M4U_PORT_VDEC_PRED_RD MTK_M4U_ID(M4U_LARB1_ID, 6) 42 + #define M4U_PORT_VDEC_PRED_WR MTK_M4U_ID(M4U_LARB1_ID, 7) 43 + #define M4U_PORT_VDEC_PPWRAP MTK_M4U_ID(M4U_LARB1_ID, 8) 44 + 45 + /* larb2 */ 46 + #define M4U_PORT_CAM_IMGO MTK_M4U_ID(M4U_LARB2_ID, 0) 47 + #define M4U_PORT_CAM_RRZO MTK_M4U_ID(M4U_LARB2_ID, 1) 48 + #define M4U_PORT_CAM_AAO MTK_M4U_ID(M4U_LARB2_ID, 2) 49 + #define M4U_PORT_CAM_LCSO MTK_M4U_ID(M4U_LARB2_ID, 3) 50 + #define M4U_PORT_CAM_ESFKO MTK_M4U_ID(M4U_LARB2_ID, 4) 51 + #define M4U_PORT_CAM_IMGO_S MTK_M4U_ID(M4U_LARB2_ID, 5) 52 + #define M4U_PORT_CAM_LSCI MTK_M4U_ID(M4U_LARB2_ID, 6) 53 + #define M4U_PORT_CAM_LSCI_D MTK_M4U_ID(M4U_LARB2_ID, 7) 54 + #define M4U_PORT_CAM_BPCI MTK_M4U_ID(M4U_LARB2_ID, 8) 55 + #define M4U_PORT_CAM_BPCI_D MTK_M4U_ID(M4U_LARB2_ID, 9) 56 + #define M4U_PORT_CAM_UFDI MTK_M4U_ID(M4U_LARB2_ID, 10) 57 + #define M4U_PORT_CAM_IMGI MTK_M4U_ID(M4U_LARB2_ID, 11) 58 + #define M4U_PORT_CAM_IMG2O MTK_M4U_ID(M4U_LARB2_ID, 12) 59 + #define M4U_PORT_CAM_IMG3O MTK_M4U_ID(M4U_LARB2_ID, 13) 60 + #define M4U_PORT_CAM_VIPI MTK_M4U_ID(M4U_LARB2_ID, 14) 61 + #define M4U_PORT_CAM_VIP2I MTK_M4U_ID(M4U_LARB2_ID, 15) 62 + #define M4U_PORT_CAM_VIP3I MTK_M4U_ID(M4U_LARB2_ID, 16) 63 + #define M4U_PORT_CAM_LCEI MTK_M4U_ID(M4U_LARB2_ID, 17) 64 + #define M4U_PORT_CAM_RB MTK_M4U_ID(M4U_LARB2_ID, 18) 65 + #define M4U_PORT_CAM_RP MTK_M4U_ID(M4U_LARB2_ID, 19) 66 + #define M4U_PORT_CAM_WR MTK_M4U_ID(M4U_LARB2_ID, 20) 67 + 68 + /* larb3 */ 69 + #define M4U_PORT_VENC_RCPU MTK_M4U_ID(M4U_LARB3_ID, 0) 70 + #define M4U_PORT_VENC_REC MTK_M4U_ID(M4U_LARB3_ID, 1) 71 + #define M4U_PORT_VENC_BSDMA MTK_M4U_ID(M4U_LARB3_ID, 2) 72 + #define M4U_PORT_VENC_SV_COMV MTK_M4U_ID(M4U_LARB3_ID, 3) 73 + #define M4U_PORT_VENC_RD_COMV MTK_M4U_ID(M4U_LARB3_ID, 4) 74 + #define M4U_PORT_JPGENC_BSDMA MTK_M4U_ID(M4U_LARB3_ID, 5) 75 + #define M4U_PORT_REMDC_SDMA MTK_M4U_ID(M4U_LARB3_ID, 6) 76 + #define M4U_PORT_REMDC_BSDMA MTK_M4U_ID(M4U_LARB3_ID, 7) 77 + #define M4U_PORT_JPGENC_RDMA MTK_M4U_ID(M4U_LARB3_ID, 8) 78 + #define M4U_PORT_JPGENC_SDMA MTK_M4U_ID(M4U_LARB3_ID, 9) 79 + #define M4U_PORT_JPGDEC_WDMA MTK_M4U_ID(M4U_LARB3_ID, 10) 80 + #define M4U_PORT_JPGDEC_BSDMA MTK_M4U_ID(M4U_LARB3_ID, 11) 81 + #define M4U_PORT_VENC_CUR_LUMA MTK_M4U_ID(M4U_LARB3_ID, 12) 82 + #define M4U_PORT_VENC_CUR_CHROMA MTK_M4U_ID(M4U_LARB3_ID, 13) 83 + #define M4U_PORT_VENC_REF_LUMA MTK_M4U_ID(M4U_LARB3_ID, 14) 84 + #define M4U_PORT_VENC_REF_CHROMA MTK_M4U_ID(M4U_LARB3_ID, 15) 85 + #define M4U_PORT_REMDC_WDMA MTK_M4U_ID(M4U_LARB3_ID, 16) 86 + #define M4U_PORT_VENC_NBM_RDMA MTK_M4U_ID(M4U_LARB3_ID, 17) 87 + #define M4U_PORT_VENC_NBM_WDMA MTK_M4U_ID(M4U_LARB3_ID, 18) 88 + 89 + /* larb4 */ 90 + #define M4U_PORT_MJC_MV_RD MTK_M4U_ID(M4U_LARB4_ID, 0) 91 + #define M4U_PORT_MJC_MV_WR MTK_M4U_ID(M4U_LARB4_ID, 1) 92 + #define M4U_PORT_MJC_DMA_RD MTK_M4U_ID(M4U_LARB4_ID, 2) 93 + #define M4U_PORT_MJC_DMA_WR MTK_M4U_ID(M4U_LARB4_ID, 3) 94 + 95 + #endif
-93
include/linux/dma-iommu.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0-only */ 2 - /* 3 - * Copyright (C) 2014-2015 ARM Ltd. 4 - */ 5 - #ifndef __DMA_IOMMU_H 6 - #define __DMA_IOMMU_H 7 - 8 - #include <linux/errno.h> 9 - #include <linux/types.h> 10 - 11 - #ifdef CONFIG_IOMMU_DMA 12 - #include <linux/dma-mapping.h> 13 - #include <linux/iommu.h> 14 - #include <linux/msi.h> 15 - 16 - /* Domain management interface for IOMMU drivers */ 17 - int iommu_get_dma_cookie(struct iommu_domain *domain); 18 - int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base); 19 - void iommu_put_dma_cookie(struct iommu_domain *domain); 20 - 21 - /* Setup call for arch DMA mapping code */ 22 - void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit); 23 - int iommu_dma_init_fq(struct iommu_domain *domain); 24 - 25 - /* The DMA API isn't _quite_ the whole story, though... */ 26 - /* 27 - * iommu_dma_prepare_msi() - Map the MSI page in the IOMMU device 28 - * 29 - * The MSI page will be stored in @desc. 30 - * 31 - * Return: 0 on success otherwise an error describing the failure. 32 - */ 33 - int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr); 34 - 35 - /* Update the MSI message if required. */ 36 - void iommu_dma_compose_msi_msg(struct msi_desc *desc, 37 - struct msi_msg *msg); 38 - 39 - void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list); 40 - 41 - void iommu_dma_free_cpu_cached_iovas(unsigned int cpu, 42 - struct iommu_domain *domain); 43 - 44 - extern bool iommu_dma_forcedac; 45 - 46 - #else /* CONFIG_IOMMU_DMA */ 47 - 48 - struct iommu_domain; 49 - struct msi_desc; 50 - struct msi_msg; 51 - struct device; 52 - 53 - static inline void iommu_setup_dma_ops(struct device *dev, u64 dma_base, 54 - u64 dma_limit) 55 - { 56 - } 57 - 58 - static inline int iommu_dma_init_fq(struct iommu_domain *domain) 59 - { 60 - return -EINVAL; 61 - } 62 - 63 - static inline int iommu_get_dma_cookie(struct iommu_domain *domain) 64 - { 65 - return -ENODEV; 66 - } 67 - 68 - static inline int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base) 69 - { 70 - return -ENODEV; 71 - } 72 - 73 - static inline void iommu_put_dma_cookie(struct iommu_domain *domain) 74 - { 75 - } 76 - 77 - static inline int iommu_dma_prepare_msi(struct msi_desc *desc, 78 - phys_addr_t msi_addr) 79 - { 80 - return 0; 81 - } 82 - 83 - static inline void iommu_dma_compose_msi_msg(struct msi_desc *desc, 84 - struct msi_msg *msg) 85 - { 86 - } 87 - 88 - static inline void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list) 89 - { 90 - } 91 - 92 - #endif /* CONFIG_IOMMU_DMA */ 93 - #endif /* __DMA_IOMMU_H */
+3
include/linux/io-pgtable.h
··· 16 16 ARM_V7S, 17 17 ARM_MALI_LPAE, 18 18 AMD_IOMMU_V1, 19 + AMD_IOMMU_V2, 19 20 APPLE_DART, 21 + APPLE_DART2, 20 22 IO_PGTABLE_NUM_FMTS, 21 23 }; 22 24 ··· 262 260 extern struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns; 263 261 extern struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns; 264 262 extern struct io_pgtable_init_fns io_pgtable_amd_iommu_v1_init_fns; 263 + extern struct io_pgtable_init_fns io_pgtable_amd_iommu_v2_init_fns; 265 264 extern struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns; 266 265 267 266 #endif /* __IO_PGTABLE_H */
+38 -9
include/linux/iommu.h
··· 212 212 * @of_xlate: add OF master IDs to iommu grouping 213 213 * @is_attach_deferred: Check if domain attach should be deferred from iommu 214 214 * driver init to device driver init (default no) 215 - * @dev_has/enable/disable_feat: per device entries to check/enable/disable 215 + * @dev_enable/disable_feat: per device entries to enable/disable 216 216 * iommu specific features. 217 217 * @sva_bind: Bind process address space to device 218 218 * @sva_unbind: Unbind process address space from device ··· 227 227 * @owner: Driver module providing these ops 228 228 */ 229 229 struct iommu_ops { 230 - bool (*capable)(enum iommu_cap); 230 + bool (*capable)(struct device *dev, enum iommu_cap); 231 231 232 232 /* Domain allocation and freeing by the iommu driver */ 233 233 struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type); ··· 416 416 return dev->iommu->iommu_dev->ops; 417 417 } 418 418 419 - extern int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops); 420 419 extern int bus_iommu_probe(struct bus_type *bus); 421 420 extern bool iommu_present(struct bus_type *bus); 422 421 extern bool device_iommu_capable(struct device *dev, enum iommu_cap cap); 423 - extern bool iommu_capable(struct bus_type *bus, enum iommu_cap cap); 424 422 extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus); 425 423 extern struct iommu_group *iommu_group_get_by_id(int id); 426 424 extern void iommu_domain_free(struct iommu_domain *domain); ··· 691 693 } 692 694 693 695 static inline bool device_iommu_capable(struct device *dev, enum iommu_cap cap) 694 - { 695 - return false; 696 - } 697 - 698 - static inline bool iommu_capable(struct bus_type *bus, enum iommu_cap cap) 699 696 { 700 697 return false; 701 698 } ··· 1062 1069 #else 1063 1070 static inline void iommu_debugfs_setup(void) {} 1064 1071 #endif 1072 + 1073 + #ifdef CONFIG_IOMMU_DMA 1074 + #include <linux/msi.h> 1075 + 1076 + /* Setup call for arch DMA mapping code */ 1077 + void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit); 1078 + 1079 + int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base); 1080 + 1081 + int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr); 1082 + void iommu_dma_compose_msi_msg(struct msi_desc *desc, struct msi_msg *msg); 1083 + 1084 + #else /* CONFIG_IOMMU_DMA */ 1085 + 1086 + struct msi_desc; 1087 + struct msi_msg; 1088 + 1089 + static inline void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit) 1090 + { 1091 + } 1092 + 1093 + static inline int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base) 1094 + { 1095 + return -ENODEV; 1096 + } 1097 + 1098 + static inline int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr) 1099 + { 1100 + return 0; 1101 + } 1102 + 1103 + static inline void iommu_dma_compose_msi_msg(struct msi_desc *desc, struct msi_msg *msg) 1104 + { 1105 + } 1106 + 1107 + #endif /* CONFIG_IOMMU_DMA */ 1065 1108 1066 1109 #endif /* __LINUX_IOMMU_H */
+1 -1
include/linux/iova.h
··· 75 75 return iova >> iova_shift(iovad); 76 76 } 77 77 78 - #if IS_ENABLED(CONFIG_IOMMU_IOVA) 78 + #if IS_REACHABLE(CONFIG_IOMMU_IOVA) 79 79 int iova_cache_get(void); 80 80 void iova_cache_put(void); 81 81