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

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

Pull iommu updates for from Joerg Roedel:
"This time the IOMMU updates are mostly cleanups or fixes. No big new
features or drivers this time. In particular the changes include:

- Bigger cleanup of the Domain<->IOMMU data structures and the code
that manages them in the Intel VT-d driver. This makes the code
easier to understand and maintain, and also easier to keep the data
structures in sync. It is also a preparation step to make use of
default domains from the IOMMU core in the Intel VT-d driver.

- Fixes for a couple of DMA-API misuses in ARM IOMMU drivers, namely
in the ARM and Tegra SMMU drivers.

- Fix for a potential buffer overflow in the OMAP iommu driver's
debug code

- A couple of smaller fixes and cleanups in various drivers

- One small new feature: Report domain-id usage in the Intel VT-d
driver to easier detect bugs where these are leaked"

* tag 'iommu-updates-v4.3' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu: (83 commits)
iommu/vt-d: Really use upper context table when necessary
x86/vt-d: Fix documentation of DRHD
iommu/fsl: Really fix init section(s) content
iommu/io-pgtable-arm: Unmap and free table when overwriting with block
iommu/io-pgtable-arm: Move init-fn declarations to io-pgtable.h
iommu/msm: Use BUG_ON instead of if () BUG()
iommu/vt-d: Access iomem correctly
iommu/vt-d: Make two functions static
iommu/vt-d: Use BUG_ON instead of if () BUG()
iommu/vt-d: Return false instead of 0 in irq_remapping_cap()
iommu/amd: Use BUG_ON instead of if () BUG()
iommu/amd: Make a symbol static
iommu/amd: Simplify allocation in irq_remapping_alloc()
iommu/tegra-smmu: Parameterize number of TLB lines
iommu/tegra-smmu: Factor out tegra_smmu_set_pde()
iommu/tegra-smmu: Extract tegra_smmu_pte_get_use()
iommu/tegra-smmu: Use __GFP_ZERO to allocate zeroed pages
iommu/tegra-smmu: Remove PageReserved manipulation
iommu/tegra-smmu: Convert to use DMA API
iommu/tegra-smmu: smmu_flush_ptc() wants device addresses
...

+949 -981
+6
Documentation/devicetree/bindings/iommu/arm,smmu.txt
··· 43 43 44 44 ** System MMU optional properties: 45 45 46 + - dma-coherent : Present if page table walks made by the SMMU are 47 + cache coherent with the CPU. 48 + 49 + NOTE: this only applies to the SMMU itself, not 50 + masters connected upstream of the SMMU. 51 + 46 52 - calxeda,smmu-secure-config-access : Enable proper handling of buggy 47 53 implementations that always use secure access to 48 54 SMMU configuration registers. In this case non-secure
+6
Documentation/devicetree/bindings/iommu/ti,omap-iommu.txt
··· 8 8 - ti,hwmods : Name of the hwmod associated with the IOMMU instance 9 9 - reg : Address space for the configuration registers 10 10 - interrupts : Interrupt specifier for the IOMMU instance 11 + - #iommu-cells : Should be 0. OMAP IOMMUs are all "single-master" devices, 12 + and needs no additional data in the pargs specifier. Please 13 + also refer to the generic bindings document for more info 14 + on this property, 15 + Documentation/devicetree/bindings/iommu/iommu.txt 11 16 12 17 Optional properties: 13 18 - ti,#tlb-entries : Number of entries in the translation look-aside buffer. ··· 23 18 Example: 24 19 /* OMAP3 ISP MMU */ 25 20 mmu_isp: mmu@480bd400 { 21 + #iommu-cells = <0>; 26 22 compatible = "ti,omap2-iommu"; 27 23 reg = <0x480bd400 0x80>; 28 24 interrupts = <24>;
+2 -1
drivers/iommu/Kconfig
··· 23 23 config IOMMU_IO_PGTABLE_LPAE 24 24 bool "ARMv7/v8 Long Descriptor Format" 25 25 select IOMMU_IO_PGTABLE 26 - depends on ARM || ARM64 || COMPILE_TEST 26 + # SWIOTLB guarantees a dma_to_phys() implementation 27 + depends on ARM || ARM64 || (COMPILE_TEST && SWIOTLB) 27 28 help 28 29 Enable support for the ARM long descriptor pagetable format. 29 30 This allocator supports 4K/2M/1G, 16K/32M and 64K/512M page
+8 -13
drivers/iommu/amd_iommu.c
··· 1835 1835 free_gcr3_tbl_level2(domain->gcr3_tbl); 1836 1836 else if (domain->glx == 1) 1837 1837 free_gcr3_tbl_level1(domain->gcr3_tbl); 1838 - else if (domain->glx != 0) 1839 - BUG(); 1838 + else 1839 + BUG_ON(domain->glx != 0); 1840 1840 1841 1841 free_page((unsigned long)domain->gcr3_tbl); 1842 1842 } ··· 3947 3947 if (ret < 0) 3948 3948 return ret; 3949 3949 3950 - ret = -ENOMEM; 3951 - data = kzalloc(sizeof(*data), GFP_KERNEL); 3952 - if (!data) 3953 - goto out_free_parent; 3954 - 3955 3950 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) { 3956 3951 if (get_irq_table(devid, true)) 3957 3952 index = info->ioapic_pin; ··· 3957 3962 } 3958 3963 if (index < 0) { 3959 3964 pr_warn("Failed to allocate IRTE\n"); 3960 - kfree(data); 3961 3965 goto out_free_parent; 3962 3966 } 3963 3967 ··· 3968 3974 goto out_free_data; 3969 3975 } 3970 3976 3971 - if (i > 0) { 3972 - data = kzalloc(sizeof(*data), GFP_KERNEL); 3973 - if (!data) 3974 - goto out_free_data; 3975 - } 3977 + ret = -ENOMEM; 3978 + data = kzalloc(sizeof(*data), GFP_KERNEL); 3979 + if (!data) 3980 + goto out_free_data; 3981 + 3976 3982 irq_data->hwirq = (devid << 16) + i; 3977 3983 irq_data->chip_data = data; 3978 3984 irq_data->chip = &amd_ir_chip; 3979 3985 irq_remapping_prepare_irte(data, cfg, info, devid, index, i); 3980 3986 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT); 3981 3987 } 3988 + 3982 3989 return 0; 3983 3990 3984 3991 out_free_data:
+1 -1
drivers/iommu/amd_iommu_init.c
··· 154 154 u32 amd_iommu_max_pasid __read_mostly = ~0; 155 155 156 156 bool amd_iommu_v2_present __read_mostly; 157 - bool amd_iommu_pc_present __read_mostly; 157 + static bool amd_iommu_pc_present __read_mostly; 158 158 159 159 bool amd_iommu_force_isolation __read_mostly; 160 160
+2 -2
drivers/iommu/amd_iommu_v2.c
··· 356 356 free_pasid_states_level2(dev_state->states); 357 357 else if (dev_state->pasid_levels == 1) 358 358 free_pasid_states_level1(dev_state->states); 359 - else if (dev_state->pasid_levels != 0) 360 - BUG(); 359 + else 360 + BUG_ON(dev_state->pasid_levels != 0); 361 361 362 362 free_page((unsigned long)dev_state->states); 363 363 }
+27 -39
drivers/iommu/arm-smmu-v3.c
··· 118 118 119 119 #define ARM_SMMU_IRQ_CTRL 0x50 120 120 #define IRQ_CTRL_EVTQ_IRQEN (1 << 2) 121 + #define IRQ_CTRL_PRIQ_IRQEN (1 << 1) 121 122 #define IRQ_CTRL_GERROR_IRQEN (1 << 0) 122 123 123 124 #define ARM_SMMU_IRQ_CTRLACK 0x54 ··· 174 173 #define ARM_SMMU_PRIQ_IRQ_CFG2 0xdc 175 174 176 175 /* Common MSI config fields */ 177 - #define MSI_CFG0_SH_SHIFT 60 178 - #define MSI_CFG0_SH_NSH (0UL << MSI_CFG0_SH_SHIFT) 179 - #define MSI_CFG0_SH_OSH (2UL << MSI_CFG0_SH_SHIFT) 180 - #define MSI_CFG0_SH_ISH (3UL << MSI_CFG0_SH_SHIFT) 181 - #define MSI_CFG0_MEMATTR_SHIFT 56 182 - #define MSI_CFG0_MEMATTR_DEVICE_nGnRE (0x1 << MSI_CFG0_MEMATTR_SHIFT) 183 176 #define MSI_CFG0_ADDR_SHIFT 2 184 177 #define MSI_CFG0_ADDR_MASK 0x3fffffffffffUL 178 + #define MSI_CFG2_SH_SHIFT 4 179 + #define MSI_CFG2_SH_NSH (0UL << MSI_CFG2_SH_SHIFT) 180 + #define MSI_CFG2_SH_OSH (2UL << MSI_CFG2_SH_SHIFT) 181 + #define MSI_CFG2_SH_ISH (3UL << MSI_CFG2_SH_SHIFT) 182 + #define MSI_CFG2_MEMATTR_SHIFT 0 183 + #define MSI_CFG2_MEMATTR_DEVICE_nGnRE (0x1 << MSI_CFG2_MEMATTR_SHIFT) 185 184 186 185 #define Q_IDX(q, p) ((p) & ((1 << (q)->max_n_shift) - 1)) 187 186 #define Q_WRP(q, p) ((p) & (1 << (q)->max_n_shift)) ··· 1331 1330 arm_smmu_cmdq_issue_cmd(smmu, &cmd); 1332 1331 } 1333 1332 1334 - static void arm_smmu_flush_pgtable(void *addr, size_t size, void *cookie) 1335 - { 1336 - struct arm_smmu_domain *smmu_domain = cookie; 1337 - struct arm_smmu_device *smmu = smmu_domain->smmu; 1338 - unsigned long offset = (unsigned long)addr & ~PAGE_MASK; 1339 - 1340 - if (smmu->features & ARM_SMMU_FEAT_COHERENCY) { 1341 - dsb(ishst); 1342 - } else { 1343 - dma_addr_t dma_addr; 1344 - struct device *dev = smmu->dev; 1345 - 1346 - dma_addr = dma_map_page(dev, virt_to_page(addr), offset, size, 1347 - DMA_TO_DEVICE); 1348 - 1349 - if (dma_mapping_error(dev, dma_addr)) 1350 - dev_err(dev, "failed to flush pgtable at %p\n", addr); 1351 - else 1352 - dma_unmap_page(dev, dma_addr, size, DMA_TO_DEVICE); 1353 - } 1354 - } 1355 - 1356 1333 static struct iommu_gather_ops arm_smmu_gather_ops = { 1357 1334 .tlb_flush_all = arm_smmu_tlb_inv_context, 1358 1335 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync, 1359 1336 .tlb_sync = arm_smmu_tlb_sync, 1360 - .flush_pgtable = arm_smmu_flush_pgtable, 1361 1337 }; 1362 1338 1363 1339 /* IOMMU API */ ··· 1509 1531 .ias = ias, 1510 1532 .oas = oas, 1511 1533 .tlb = &arm_smmu_gather_ops, 1534 + .iommu_dev = smmu->dev, 1512 1535 }; 1513 1536 1514 1537 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain); ··· 2032 2053 int ret; 2033 2054 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; 2034 2055 2035 - /* Calculate the L1 size, capped to the SIDSIZE */ 2036 - size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3); 2037 - size = min(size, smmu->sid_bits - STRTAB_SPLIT); 2056 + /* 2057 + * If we can resolve everything with a single L2 table, then we 2058 + * just need a single L1 descriptor. Otherwise, calculate the L1 2059 + * size, capped to the SIDSIZE. 2060 + */ 2061 + if (smmu->sid_bits < STRTAB_SPLIT) { 2062 + size = 0; 2063 + } else { 2064 + size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3); 2065 + size = min(size, smmu->sid_bits - STRTAB_SPLIT); 2066 + } 2038 2067 cfg->num_l1_ents = 1 << size; 2039 2068 2040 2069 size += STRTAB_SPLIT; ··· 2185 2198 static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu) 2186 2199 { 2187 2200 int ret, irq; 2201 + u32 irqen_flags = IRQ_CTRL_EVTQ_IRQEN | IRQ_CTRL_GERROR_IRQEN; 2188 2202 2189 2203 /* Disable IRQs first */ 2190 2204 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_IRQ_CTRL, ··· 2240 2252 if (IS_ERR_VALUE(ret)) 2241 2253 dev_warn(smmu->dev, 2242 2254 "failed to enable priq irq\n"); 2255 + else 2256 + irqen_flags |= IRQ_CTRL_PRIQ_IRQEN; 2243 2257 } 2244 2258 } 2245 2259 2246 2260 /* Enable interrupt generation on the SMMU */ 2247 - ret = arm_smmu_write_reg_sync(smmu, 2248 - IRQ_CTRL_EVTQ_IRQEN | 2249 - IRQ_CTRL_GERROR_IRQEN, 2261 + ret = arm_smmu_write_reg_sync(smmu, irqen_flags, 2250 2262 ARM_SMMU_IRQ_CTRL, ARM_SMMU_IRQ_CTRLACK); 2251 2263 if (ret) 2252 2264 dev_warn(smmu->dev, "failed to enable irqs\n"); ··· 2528 2540 case IDR5_OAS_44_BIT: 2529 2541 smmu->oas = 44; 2530 2542 break; 2543 + default: 2544 + dev_info(smmu->dev, 2545 + "unknown output address size. Truncating to 48-bit\n"); 2546 + /* Fallthrough */ 2531 2547 case IDR5_OAS_48_BIT: 2532 2548 smmu->oas = 48; 2533 - break; 2534 - default: 2535 - dev_err(smmu->dev, "unknown output address size!\n"); 2536 - return -ENXIO; 2537 2549 } 2538 2550 2539 2551 /* Set the DMA mask for our table walker */
+18 -27
drivers/iommu/arm-smmu.c
··· 37 37 #include <linux/iopoll.h> 38 38 #include <linux/module.h> 39 39 #include <linux/of.h> 40 + #include <linux/of_address.h> 40 41 #include <linux/pci.h> 41 42 #include <linux/platform_device.h> 42 43 #include <linux/slab.h> ··· 608 607 } 609 608 } 610 609 611 - static void arm_smmu_flush_pgtable(void *addr, size_t size, void *cookie) 612 - { 613 - struct arm_smmu_domain *smmu_domain = cookie; 614 - struct arm_smmu_device *smmu = smmu_domain->smmu; 615 - unsigned long offset = (unsigned long)addr & ~PAGE_MASK; 616 - 617 - 618 - /* Ensure new page tables are visible to the hardware walker */ 619 - if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) { 620 - dsb(ishst); 621 - } else { 622 - /* 623 - * If the SMMU can't walk tables in the CPU caches, treat them 624 - * like non-coherent DMA since we need to flush the new entries 625 - * all the way out to memory. There's no possibility of 626 - * recursion here as the SMMU table walker will not be wired 627 - * through another SMMU. 628 - */ 629 - dma_map_page(smmu->dev, virt_to_page(addr), offset, size, 630 - DMA_TO_DEVICE); 631 - } 632 - } 633 - 634 610 static struct iommu_gather_ops arm_smmu_gather_ops = { 635 611 .tlb_flush_all = arm_smmu_tlb_inv_context, 636 612 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync, 637 613 .tlb_sync = arm_smmu_tlb_sync, 638 - .flush_pgtable = arm_smmu_flush_pgtable, 639 614 }; 640 615 641 616 static irqreturn_t arm_smmu_context_fault(int irq, void *dev) ··· 875 898 .ias = ias, 876 899 .oas = oas, 877 900 .tlb = &arm_smmu_gather_ops, 901 + .iommu_dev = smmu->dev, 878 902 }; 879 903 880 904 smmu_domain->smmu = smmu; ··· 1510 1532 unsigned long size; 1511 1533 void __iomem *gr0_base = ARM_SMMU_GR0(smmu); 1512 1534 u32 id; 1535 + bool cttw_dt, cttw_reg; 1513 1536 1514 1537 dev_notice(smmu->dev, "probing hardware configuration...\n"); 1515 1538 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version); ··· 1550 1571 dev_notice(smmu->dev, "\taddress translation ops\n"); 1551 1572 } 1552 1573 1553 - if (id & ID0_CTTW) { 1574 + /* 1575 + * In order for DMA API calls to work properly, we must defer to what 1576 + * the DT says about coherency, regardless of what the hardware claims. 1577 + * Fortunately, this also opens up a workaround for systems where the 1578 + * ID register value has ended up configured incorrectly. 1579 + */ 1580 + cttw_dt = of_dma_is_coherent(smmu->dev->of_node); 1581 + cttw_reg = !!(id & ID0_CTTW); 1582 + if (cttw_dt) 1554 1583 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK; 1555 - dev_notice(smmu->dev, "\tcoherent table walk\n"); 1556 - } 1584 + if (cttw_dt || cttw_reg) 1585 + dev_notice(smmu->dev, "\t%scoherent table walk\n", 1586 + cttw_dt ? "" : "non-"); 1587 + if (cttw_dt != cttw_reg) 1588 + dev_notice(smmu->dev, 1589 + "\t(IDR0.CTTW overridden by dma-coherent property)\n"); 1557 1590 1558 1591 if (id & ID0_SMS) { 1559 1592 u32 smr, sid, mask;
+1 -1
drivers/iommu/dmar.c
··· 1068 1068 if (intel_iommu_enabled) 1069 1069 iommu->iommu_dev = iommu_device_create(NULL, iommu, 1070 1070 intel_iommu_groups, 1071 - iommu->name); 1071 + "%s", iommu->name); 1072 1072 1073 1073 return 0; 1074 1074
+13 -13
drivers/iommu/fsl_pamu.c
··· 41 41 42 42 static struct paace *ppaact; 43 43 static struct paace *spaact; 44 - static struct ome *omt __initdata; 45 44 46 45 /* 47 46 * Table for matching compatible strings, for device tree ··· 49 50 * SOCs. For the older SOCs "fsl,qoriq-device-config-1.0" 50 51 * string would be used. 51 52 */ 52 - static const struct of_device_id guts_device_ids[] __initconst = { 53 + static const struct of_device_id guts_device_ids[] = { 53 54 { .compatible = "fsl,qoriq-device-config-1.0", }, 54 55 { .compatible = "fsl,qoriq-device-config-2.0", }, 55 56 {} ··· 598 599 * Memory accesses to QMAN and BMAN private memory need not be coherent, so 599 600 * clear the PAACE entry coherency attribute for them. 600 601 */ 601 - static void __init setup_qbman_paace(struct paace *ppaace, int paace_type) 602 + static void setup_qbman_paace(struct paace *ppaace, int paace_type) 602 603 { 603 604 switch (paace_type) { 604 605 case QMAN_PAACE: ··· 628 629 * this table to translate device transaction to appropriate corenet 629 630 * transaction. 630 631 */ 631 - static void __init setup_omt(struct ome *omt) 632 + static void setup_omt(struct ome *omt) 632 633 { 633 634 struct ome *ome; 634 635 ··· 665 666 * Get the maximum number of PAACT table entries 666 667 * and subwindows supported by PAMU 667 668 */ 668 - static void __init get_pamu_cap_values(unsigned long pamu_reg_base) 669 + static void get_pamu_cap_values(unsigned long pamu_reg_base) 669 670 { 670 671 u32 pc_val; 671 672 ··· 675 676 } 676 677 677 678 /* Setup PAMU registers pointing to PAACT, SPAACT and OMT */ 678 - static int __init setup_one_pamu(unsigned long pamu_reg_base, unsigned long pamu_reg_size, 679 - phys_addr_t ppaact_phys, phys_addr_t spaact_phys, 680 - phys_addr_t omt_phys) 679 + static int setup_one_pamu(unsigned long pamu_reg_base, unsigned long pamu_reg_size, 680 + phys_addr_t ppaact_phys, phys_addr_t spaact_phys, 681 + phys_addr_t omt_phys) 681 682 { 682 683 u32 *pc; 683 684 struct pamu_mmap_regs *pamu_regs; ··· 719 720 } 720 721 721 722 /* Enable all device LIODNS */ 722 - static void __init setup_liodns(void) 723 + static void setup_liodns(void) 723 724 { 724 725 int i, len; 725 726 struct paace *ppaace; ··· 845 846 /* 846 847 * Create a coherence subdomain for a given memory block. 847 848 */ 848 - static int __init create_csd(phys_addr_t phys, size_t size, u32 csd_port_id) 849 + static int create_csd(phys_addr_t phys, size_t size, u32 csd_port_id) 849 850 { 850 851 struct device_node *np; 851 852 const __be32 *iprop; ··· 987 988 static const struct { 988 989 u32 svr; 989 990 u32 port_id; 990 - } port_id_map[] __initconst = { 991 + } port_id_map[] = { 991 992 {(SVR_P2040 << 8) | 0x10, 0xFF000000}, /* P2040 1.0 */ 992 993 {(SVR_P2040 << 8) | 0x11, 0xFF000000}, /* P2040 1.1 */ 993 994 {(SVR_P2041 << 8) | 0x10, 0xFF000000}, /* P2041 1.0 */ ··· 1005 1006 1006 1007 #define SVR_SECURITY 0x80000 /* The Security (E) bit */ 1007 1008 1008 - static int __init fsl_pamu_probe(struct platform_device *pdev) 1009 + static int fsl_pamu_probe(struct platform_device *pdev) 1009 1010 { 1010 1011 struct device *dev = &pdev->dev; 1011 1012 void __iomem *pamu_regs = NULL; ··· 1021 1022 int irq; 1022 1023 phys_addr_t ppaact_phys; 1023 1024 phys_addr_t spaact_phys; 1025 + struct ome *omt; 1024 1026 phys_addr_t omt_phys; 1025 1027 size_t mem_size = 0; 1026 1028 unsigned int order = 0; ··· 1200 1200 return ret; 1201 1201 } 1202 1202 1203 - static struct platform_driver fsl_of_pamu_driver __initdata = { 1203 + static struct platform_driver fsl_of_pamu_driver = { 1204 1204 .driver = { 1205 1205 .name = "fsl-of-pamu", 1206 1206 },
+337 -389
drivers/iommu/intel-iommu.c
··· 364 364 static struct dmar_domain *si_domain; 365 365 static int hw_pass_through = 1; 366 366 367 - /* domain represents a virtual machine, more than one devices 367 + /* 368 + * Domain represents a virtual machine, more than one devices 368 369 * across iommus may be owned in one domain, e.g. kvm guest. 369 370 */ 370 371 #define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 0) ··· 373 372 /* si_domain contains mulitple devices */ 374 373 #define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1) 375 374 375 + #define for_each_domain_iommu(idx, domain) \ 376 + for (idx = 0; idx < g_num_of_iommus; idx++) \ 377 + if (domain->iommu_refcnt[idx]) 378 + 376 379 struct dmar_domain { 377 - int id; /* domain id */ 378 380 int nid; /* node id */ 379 - DECLARE_BITMAP(iommu_bmp, DMAR_UNITS_SUPPORTED); 380 - /* bitmap of iommus this domain uses*/ 381 + 382 + unsigned iommu_refcnt[DMAR_UNITS_SUPPORTED]; 383 + /* Refcount of devices per iommu */ 384 + 385 + 386 + u16 iommu_did[DMAR_UNITS_SUPPORTED]; 387 + /* Domain ids per IOMMU. Use u16 since 388 + * domain ids are 16 bit wide according 389 + * to VT-d spec, section 9.3 */ 381 390 382 391 struct list_head devices; /* all devices' list */ 383 392 struct iova_domain iovad; /* iova's that belong to this domain */ ··· 406 395 int iommu_superpage;/* Level of superpages supported: 407 396 0 == 4KiB (no superpages), 1 == 2MiB, 408 397 2 == 1GiB, 3 == 512GiB, 4 == 1TiB */ 409 - spinlock_t iommu_lock; /* protect iommu set in domain */ 410 398 u64 max_addr; /* maximum mapped address */ 411 399 412 400 struct iommu_domain domain; /* generic domain data structure for ··· 475 465 476 466 static void domain_exit(struct dmar_domain *domain); 477 467 static void domain_remove_dev_info(struct dmar_domain *domain); 478 - static void domain_remove_one_dev_info(struct dmar_domain *domain, 479 - struct device *dev); 480 - static void iommu_detach_dependent_devices(struct intel_iommu *iommu, 481 - struct device *dev); 468 + static void dmar_remove_one_dev_info(struct dmar_domain *domain, 469 + struct device *dev); 470 + static void __dmar_remove_one_dev_info(struct device_domain_info *info); 471 + static void domain_context_clear(struct intel_iommu *iommu, 472 + struct device *dev); 482 473 static int domain_detach_iommu(struct dmar_domain *domain, 483 474 struct intel_iommu *iommu); 484 475 ··· 579 568 static struct kmem_cache *iommu_domain_cache; 580 569 static struct kmem_cache *iommu_devinfo_cache; 581 570 571 + static struct dmar_domain* get_iommu_domain(struct intel_iommu *iommu, u16 did) 572 + { 573 + struct dmar_domain **domains; 574 + int idx = did >> 8; 575 + 576 + domains = iommu->domains[idx]; 577 + if (!domains) 578 + return NULL; 579 + 580 + return domains[did & 0xff]; 581 + } 582 + 583 + static void set_iommu_domain(struct intel_iommu *iommu, u16 did, 584 + struct dmar_domain *domain) 585 + { 586 + struct dmar_domain **domains; 587 + int idx = did >> 8; 588 + 589 + if (!iommu->domains[idx]) { 590 + size_t size = 256 * sizeof(struct dmar_domain *); 591 + iommu->domains[idx] = kzalloc(size, GFP_ATOMIC); 592 + } 593 + 594 + domains = iommu->domains[idx]; 595 + if (WARN_ON(!domains)) 596 + return; 597 + else 598 + domains[did & 0xff] = domain; 599 + } 600 + 582 601 static inline void *alloc_pgtable_page(int node) 583 602 { 584 603 struct page *page; ··· 648 607 static inline int domain_type_is_vm(struct dmar_domain *domain) 649 608 { 650 609 return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE; 610 + } 611 + 612 + static inline int domain_type_is_si(struct dmar_domain *domain) 613 + { 614 + return domain->flags & DOMAIN_FLAG_STATIC_IDENTITY; 651 615 } 652 616 653 617 static inline int domain_type_is_vm_or_si(struct dmar_domain *domain) ··· 709 663 710 664 /* si_domain and vm domain should not get here. */ 711 665 BUG_ON(domain_type_is_vm_or_si(domain)); 712 - iommu_id = find_first_bit(domain->iommu_bmp, g_num_of_iommus); 666 + for_each_domain_iommu(iommu_id, domain) 667 + break; 668 + 713 669 if (iommu_id < 0 || iommu_id >= g_num_of_iommus) 714 670 return NULL; 715 671 ··· 727 679 728 680 domain->iommu_coherency = 1; 729 681 730 - for_each_set_bit(i, domain->iommu_bmp, g_num_of_iommus) { 682 + for_each_domain_iommu(i, domain) { 731 683 found = true; 732 684 if (!ecap_coherent(g_iommus[i]->ecap)) { 733 685 domain->iommu_coherency = 0; ··· 807 759 struct context_entry *context; 808 760 u64 *entry; 809 761 762 + entry = &root->lo; 810 763 if (ecs_enabled(iommu)) { 811 764 if (devfn >= 0x80) { 812 765 devfn -= 0x80; ··· 815 766 } 816 767 devfn *= 2; 817 768 } 818 - entry = &root->lo; 819 769 if (*entry & 1) 820 770 context = phys_to_virt(*entry & VTD_PAGE_MASK); 821 771 else { ··· 1214 1166 /* We can't just free the pages because the IOMMU may still be walking 1215 1167 the page tables, and may have cached the intermediate levels. The 1216 1168 pages can only be freed after the IOTLB flush has been done. */ 1217 - struct page *domain_unmap(struct dmar_domain *domain, 1218 - unsigned long start_pfn, 1219 - unsigned long last_pfn) 1169 + static struct page *domain_unmap(struct dmar_domain *domain, 1170 + unsigned long start_pfn, 1171 + unsigned long last_pfn) 1220 1172 { 1221 1173 struct page *freelist = NULL; 1222 1174 ··· 1240 1192 return freelist; 1241 1193 } 1242 1194 1243 - void dma_free_pagelist(struct page *freelist) 1195 + static void dma_free_pagelist(struct page *freelist) 1244 1196 { 1245 1197 struct page *pg; 1246 1198 ··· 1408 1360 u8 bus, u8 devfn) 1409 1361 { 1410 1362 bool found = false; 1411 - unsigned long flags; 1412 1363 struct device_domain_info *info; 1413 1364 struct pci_dev *pdev; 1365 + 1366 + assert_spin_locked(&device_domain_lock); 1414 1367 1415 1368 if (!ecap_dev_iotlb_support(iommu->ecap)) 1416 1369 return NULL; ··· 1419 1370 if (!iommu->qi) 1420 1371 return NULL; 1421 1372 1422 - spin_lock_irqsave(&device_domain_lock, flags); 1423 1373 list_for_each_entry(info, &domain->devices, link) 1424 1374 if (info->iommu == iommu && info->bus == bus && 1425 1375 info->devfn == devfn) { 1426 1376 found = true; 1427 1377 break; 1428 1378 } 1429 - spin_unlock_irqrestore(&device_domain_lock, flags); 1430 1379 1431 1380 if (!found || !info->dev || !dev_is_pci(info->dev)) 1432 1381 return NULL; ··· 1483 1436 spin_unlock_irqrestore(&device_domain_lock, flags); 1484 1437 } 1485 1438 1486 - static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did, 1487 - unsigned long pfn, unsigned int pages, int ih, int map) 1439 + static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, 1440 + struct dmar_domain *domain, 1441 + unsigned long pfn, unsigned int pages, 1442 + int ih, int map) 1488 1443 { 1489 1444 unsigned int mask = ilog2(__roundup_pow_of_two(pages)); 1490 1445 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT; 1446 + u16 did = domain->iommu_did[iommu->seq_id]; 1491 1447 1492 1448 BUG_ON(pages == 0); 1493 1449 ··· 1514 1464 * flush. However, device IOTLB doesn't need to be flushed in this case. 1515 1465 */ 1516 1466 if (!cap_caching_mode(iommu->cap) || !map) 1517 - iommu_flush_dev_iotlb(iommu->domains[did], addr, mask); 1467 + iommu_flush_dev_iotlb(get_iommu_domain(iommu, did), 1468 + addr, mask); 1518 1469 } 1519 1470 1520 1471 static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu) ··· 1570 1519 1571 1520 static int iommu_init_domains(struct intel_iommu *iommu) 1572 1521 { 1573 - unsigned long ndomains; 1574 - unsigned long nlongs; 1522 + u32 ndomains, nlongs; 1523 + size_t size; 1575 1524 1576 1525 ndomains = cap_ndoms(iommu->cap); 1577 - pr_debug("%s: Number of Domains supported <%ld>\n", 1526 + pr_debug("%s: Number of Domains supported <%d>\n", 1578 1527 iommu->name, ndomains); 1579 1528 nlongs = BITS_TO_LONGS(ndomains); 1580 1529 1581 1530 spin_lock_init(&iommu->lock); 1582 1531 1583 - /* TBD: there might be 64K domains, 1584 - * consider other allocation for future chip 1585 - */ 1586 1532 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL); 1587 1533 if (!iommu->domain_ids) { 1588 1534 pr_err("%s: Allocating domain id array failed\n", 1589 1535 iommu->name); 1590 1536 return -ENOMEM; 1591 1537 } 1592 - iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *), 1593 - GFP_KERNEL); 1594 - if (!iommu->domains) { 1538 + 1539 + size = ((ndomains >> 8) + 1) * sizeof(struct dmar_domain **); 1540 + iommu->domains = kzalloc(size, GFP_KERNEL); 1541 + 1542 + if (iommu->domains) { 1543 + size = 256 * sizeof(struct dmar_domain *); 1544 + iommu->domains[0] = kzalloc(size, GFP_KERNEL); 1545 + } 1546 + 1547 + if (!iommu->domains || !iommu->domains[0]) { 1595 1548 pr_err("%s: Allocating domain array failed\n", 1596 1549 iommu->name); 1597 1550 kfree(iommu->domain_ids); 1551 + kfree(iommu->domains); 1598 1552 iommu->domain_ids = NULL; 1553 + iommu->domains = NULL; 1599 1554 return -ENOMEM; 1600 1555 } 1601 1556 1557 + 1558 + 1602 1559 /* 1603 - * if Caching mode is set, then invalid translations are tagged 1604 - * with domainid 0. Hence we need to pre-allocate it. 1560 + * If Caching mode is set, then invalid translations are tagged 1561 + * with domain-id 0, hence we need to pre-allocate it. We also 1562 + * use domain-id 0 as a marker for non-allocated domain-id, so 1563 + * make sure it is not used for a real domain. 1605 1564 */ 1606 - if (cap_caching_mode(iommu->cap)) 1607 - set_bit(0, iommu->domain_ids); 1565 + set_bit(0, iommu->domain_ids); 1566 + 1608 1567 return 0; 1609 1568 } 1610 1569 1611 1570 static void disable_dmar_iommu(struct intel_iommu *iommu) 1612 1571 { 1613 - struct dmar_domain *domain; 1614 - int i; 1572 + struct device_domain_info *info, *tmp; 1573 + unsigned long flags; 1615 1574 1616 - if ((iommu->domains) && (iommu->domain_ids)) { 1617 - for_each_set_bit(i, iommu->domain_ids, cap_ndoms(iommu->cap)) { 1618 - /* 1619 - * Domain id 0 is reserved for invalid translation 1620 - * if hardware supports caching mode. 1621 - */ 1622 - if (cap_caching_mode(iommu->cap) && i == 0) 1623 - continue; 1575 + if (!iommu->domains || !iommu->domain_ids) 1576 + return; 1624 1577 1625 - domain = iommu->domains[i]; 1626 - clear_bit(i, iommu->domain_ids); 1627 - if (domain_detach_iommu(domain, iommu) == 0 && 1628 - !domain_type_is_vm(domain)) 1629 - domain_exit(domain); 1630 - } 1578 + spin_lock_irqsave(&device_domain_lock, flags); 1579 + list_for_each_entry_safe(info, tmp, &device_domain_list, global) { 1580 + struct dmar_domain *domain; 1581 + 1582 + if (info->iommu != iommu) 1583 + continue; 1584 + 1585 + if (!info->dev || !info->domain) 1586 + continue; 1587 + 1588 + domain = info->domain; 1589 + 1590 + dmar_remove_one_dev_info(domain, info->dev); 1591 + 1592 + if (!domain_type_is_vm_or_si(domain)) 1593 + domain_exit(domain); 1631 1594 } 1595 + spin_unlock_irqrestore(&device_domain_lock, flags); 1632 1596 1633 1597 if (iommu->gcmd & DMA_GCMD_TE) 1634 1598 iommu_disable_translation(iommu); ··· 1652 1586 static void free_dmar_iommu(struct intel_iommu *iommu) 1653 1587 { 1654 1588 if ((iommu->domains) && (iommu->domain_ids)) { 1589 + int elems = (cap_ndoms(iommu->cap) >> 8) + 1; 1590 + int i; 1591 + 1592 + for (i = 0; i < elems; i++) 1593 + kfree(iommu->domains[i]); 1655 1594 kfree(iommu->domains); 1656 1595 kfree(iommu->domain_ids); 1657 1596 iommu->domains = NULL; ··· 1671 1600 1672 1601 static struct dmar_domain *alloc_domain(int flags) 1673 1602 { 1674 - /* domain id for virtual machine, it won't be set in context */ 1675 - static atomic_t vm_domid = ATOMIC_INIT(0); 1676 1603 struct dmar_domain *domain; 1677 1604 1678 1605 domain = alloc_domain_mem(); ··· 1680 1611 memset(domain, 0, sizeof(*domain)); 1681 1612 domain->nid = -1; 1682 1613 domain->flags = flags; 1683 - spin_lock_init(&domain->iommu_lock); 1684 1614 INIT_LIST_HEAD(&domain->devices); 1685 - if (flags & DOMAIN_FLAG_VIRTUAL_MACHINE) 1686 - domain->id = atomic_inc_return(&vm_domid); 1687 1615 1688 1616 return domain; 1689 1617 } 1690 1618 1691 - static int __iommu_attach_domain(struct dmar_domain *domain, 1692 - struct intel_iommu *iommu) 1693 - { 1694 - int num; 1695 - unsigned long ndomains; 1696 - 1697 - ndomains = cap_ndoms(iommu->cap); 1698 - num = find_first_zero_bit(iommu->domain_ids, ndomains); 1699 - if (num < ndomains) { 1700 - set_bit(num, iommu->domain_ids); 1701 - iommu->domains[num] = domain; 1702 - } else { 1703 - num = -ENOSPC; 1704 - } 1705 - 1706 - return num; 1707 - } 1708 - 1709 - static int iommu_attach_domain(struct dmar_domain *domain, 1619 + /* Must be called with iommu->lock */ 1620 + static int domain_attach_iommu(struct dmar_domain *domain, 1710 1621 struct intel_iommu *iommu) 1711 1622 { 1712 - int num; 1713 - unsigned long flags; 1714 - 1715 - spin_lock_irqsave(&iommu->lock, flags); 1716 - num = __iommu_attach_domain(domain, iommu); 1717 - spin_unlock_irqrestore(&iommu->lock, flags); 1718 - if (num < 0) 1719 - pr_err("%s: No free domain ids\n", iommu->name); 1720 - 1721 - return num; 1722 - } 1723 - 1724 - static int iommu_attach_vm_domain(struct dmar_domain *domain, 1725 - struct intel_iommu *iommu) 1726 - { 1727 - int num; 1728 1623 unsigned long ndomains; 1624 + int num; 1729 1625 1730 - ndomains = cap_ndoms(iommu->cap); 1731 - for_each_set_bit(num, iommu->domain_ids, ndomains) 1732 - if (iommu->domains[num] == domain) 1733 - return num; 1626 + assert_spin_locked(&device_domain_lock); 1627 + assert_spin_locked(&iommu->lock); 1734 1628 1735 - return __iommu_attach_domain(domain, iommu); 1736 - } 1737 - 1738 - static void iommu_detach_domain(struct dmar_domain *domain, 1739 - struct intel_iommu *iommu) 1740 - { 1741 - unsigned long flags; 1742 - int num, ndomains; 1743 - 1744 - spin_lock_irqsave(&iommu->lock, flags); 1745 - if (domain_type_is_vm_or_si(domain)) { 1629 + domain->iommu_refcnt[iommu->seq_id] += 1; 1630 + domain->iommu_count += 1; 1631 + if (domain->iommu_refcnt[iommu->seq_id] == 1) { 1746 1632 ndomains = cap_ndoms(iommu->cap); 1747 - for_each_set_bit(num, iommu->domain_ids, ndomains) { 1748 - if (iommu->domains[num] == domain) { 1749 - clear_bit(num, iommu->domain_ids); 1750 - iommu->domains[num] = NULL; 1751 - break; 1752 - } 1633 + num = find_first_zero_bit(iommu->domain_ids, ndomains); 1634 + 1635 + if (num >= ndomains) { 1636 + pr_err("%s: No free domain ids\n", iommu->name); 1637 + domain->iommu_refcnt[iommu->seq_id] -= 1; 1638 + domain->iommu_count -= 1; 1639 + return -ENOSPC; 1753 1640 } 1754 - } else { 1755 - clear_bit(domain->id, iommu->domain_ids); 1756 - iommu->domains[domain->id] = NULL; 1757 - } 1758 - spin_unlock_irqrestore(&iommu->lock, flags); 1759 - } 1760 1641 1761 - static void domain_attach_iommu(struct dmar_domain *domain, 1762 - struct intel_iommu *iommu) 1763 - { 1764 - unsigned long flags; 1642 + set_bit(num, iommu->domain_ids); 1643 + set_iommu_domain(iommu, num, domain); 1765 1644 1766 - spin_lock_irqsave(&domain->iommu_lock, flags); 1767 - if (!test_and_set_bit(iommu->seq_id, domain->iommu_bmp)) { 1768 - domain->iommu_count++; 1769 - if (domain->iommu_count == 1) 1770 - domain->nid = iommu->node; 1645 + domain->iommu_did[iommu->seq_id] = num; 1646 + domain->nid = iommu->node; 1647 + 1771 1648 domain_update_iommu_cap(domain); 1772 1649 } 1773 - spin_unlock_irqrestore(&domain->iommu_lock, flags); 1650 + 1651 + return 0; 1774 1652 } 1775 1653 1776 1654 static int domain_detach_iommu(struct dmar_domain *domain, 1777 1655 struct intel_iommu *iommu) 1778 1656 { 1779 - unsigned long flags; 1780 - int count = INT_MAX; 1657 + int num, count = INT_MAX; 1781 1658 1782 - spin_lock_irqsave(&domain->iommu_lock, flags); 1783 - if (test_and_clear_bit(iommu->seq_id, domain->iommu_bmp)) { 1784 - count = --domain->iommu_count; 1659 + assert_spin_locked(&device_domain_lock); 1660 + assert_spin_locked(&iommu->lock); 1661 + 1662 + domain->iommu_refcnt[iommu->seq_id] -= 1; 1663 + count = --domain->iommu_count; 1664 + if (domain->iommu_refcnt[iommu->seq_id] == 0) { 1665 + num = domain->iommu_did[iommu->seq_id]; 1666 + clear_bit(num, iommu->domain_ids); 1667 + set_iommu_domain(iommu, num, NULL); 1668 + 1785 1669 domain_update_iommu_cap(domain); 1670 + domain->iommu_did[iommu->seq_id] = 0; 1786 1671 } 1787 - spin_unlock_irqrestore(&domain->iommu_lock, flags); 1788 1672 1789 1673 return count; 1790 1674 } ··· 1804 1782 return agaw; 1805 1783 } 1806 1784 1807 - static int domain_init(struct dmar_domain *domain, int guest_width) 1785 + static int domain_init(struct dmar_domain *domain, struct intel_iommu *iommu, 1786 + int guest_width) 1808 1787 { 1809 - struct intel_iommu *iommu; 1810 1788 int adjust_width, agaw; 1811 1789 unsigned long sagaw; 1812 1790 ··· 1815 1793 domain_reserve_special_ranges(domain); 1816 1794 1817 1795 /* calculate AGAW */ 1818 - iommu = domain_get_iommu(domain); 1819 1796 if (guest_width > cap_mgaw(iommu->cap)) 1820 1797 guest_width = cap_mgaw(iommu->cap); 1821 1798 domain->gaw = guest_width; ··· 1857 1836 1858 1837 static void domain_exit(struct dmar_domain *domain) 1859 1838 { 1860 - struct dmar_drhd_unit *drhd; 1861 - struct intel_iommu *iommu; 1862 1839 struct page *freelist = NULL; 1863 1840 1864 1841 /* Domain 0 is reserved, so dont process it */ ··· 1867 1848 if (!intel_iommu_strict) 1868 1849 flush_unmaps_timeout(0); 1869 1850 1870 - /* remove associated devices */ 1851 + /* Remove associated devices and clear attached or cached domains */ 1852 + rcu_read_lock(); 1871 1853 domain_remove_dev_info(domain); 1854 + rcu_read_unlock(); 1872 1855 1873 1856 /* destroy iovas */ 1874 1857 put_iova_domain(&domain->iovad); 1875 1858 1876 1859 freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw)); 1877 - 1878 - /* clear attached or cached domains */ 1879 - rcu_read_lock(); 1880 - for_each_active_iommu(iommu, drhd) 1881 - if (domain_type_is_vm(domain) || 1882 - test_bit(iommu->seq_id, domain->iommu_bmp)) 1883 - iommu_detach_domain(domain, iommu); 1884 - rcu_read_unlock(); 1885 1860 1886 1861 dma_free_pagelist(freelist); 1887 1862 ··· 1884 1871 1885 1872 static int domain_context_mapping_one(struct dmar_domain *domain, 1886 1873 struct intel_iommu *iommu, 1887 - u8 bus, u8 devfn, int translation) 1874 + u8 bus, u8 devfn) 1888 1875 { 1876 + u16 did = domain->iommu_did[iommu->seq_id]; 1877 + int translation = CONTEXT_TT_MULTI_LEVEL; 1878 + struct device_domain_info *info = NULL; 1889 1879 struct context_entry *context; 1890 1880 unsigned long flags; 1891 1881 struct dma_pte *pgd; 1892 - int id; 1893 - int agaw; 1894 - struct device_domain_info *info = NULL; 1882 + int ret, agaw; 1883 + 1884 + WARN_ON(did == 0); 1885 + 1886 + if (hw_pass_through && domain_type_is_si(domain)) 1887 + translation = CONTEXT_TT_PASS_THROUGH; 1895 1888 1896 1889 pr_debug("Set context mapping for %02x:%02x.%d\n", 1897 1890 bus, PCI_SLOT(devfn), PCI_FUNC(devfn)); 1898 1891 1899 1892 BUG_ON(!domain->pgd); 1900 - BUG_ON(translation != CONTEXT_TT_PASS_THROUGH && 1901 - translation != CONTEXT_TT_MULTI_LEVEL); 1902 1893 1903 - spin_lock_irqsave(&iommu->lock, flags); 1894 + spin_lock_irqsave(&device_domain_lock, flags); 1895 + spin_lock(&iommu->lock); 1896 + 1897 + ret = -ENOMEM; 1904 1898 context = iommu_context_addr(iommu, bus, devfn, 1); 1905 - spin_unlock_irqrestore(&iommu->lock, flags); 1906 1899 if (!context) 1907 - return -ENOMEM; 1908 - spin_lock_irqsave(&iommu->lock, flags); 1909 - if (context_present(context)) { 1910 - spin_unlock_irqrestore(&iommu->lock, flags); 1911 - return 0; 1912 - } 1900 + goto out_unlock; 1913 1901 1914 - context_clear_entry(context); 1902 + ret = 0; 1903 + if (context_present(context)) 1904 + goto out_unlock; 1915 1905 1916 - id = domain->id; 1917 1906 pgd = domain->pgd; 1918 1907 1919 - if (domain_type_is_vm_or_si(domain)) { 1920 - if (domain_type_is_vm(domain)) { 1921 - id = iommu_attach_vm_domain(domain, iommu); 1922 - if (id < 0) { 1923 - spin_unlock_irqrestore(&iommu->lock, flags); 1924 - pr_err("%s: No free domain ids\n", iommu->name); 1925 - return -EFAULT; 1926 - } 1927 - } 1908 + context_clear_entry(context); 1909 + context_set_domain_id(context, did); 1928 1910 1929 - /* Skip top levels of page tables for 1930 - * iommu which has less agaw than default. 1931 - * Unnecessary for PT mode. 1932 - */ 1933 - if (translation != CONTEXT_TT_PASS_THROUGH) { 1934 - for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) { 1935 - pgd = phys_to_virt(dma_pte_addr(pgd)); 1936 - if (!dma_pte_present(pgd)) { 1937 - spin_unlock_irqrestore(&iommu->lock, flags); 1938 - return -ENOMEM; 1939 - } 1940 - } 1941 - } 1942 - } 1943 - 1944 - context_set_domain_id(context, id); 1945 - 1911 + /* 1912 + * Skip top levels of page tables for iommu which has less agaw 1913 + * than default. Unnecessary for PT mode. 1914 + */ 1946 1915 if (translation != CONTEXT_TT_PASS_THROUGH) { 1916 + for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) { 1917 + ret = -ENOMEM; 1918 + pgd = phys_to_virt(dma_pte_addr(pgd)); 1919 + if (!dma_pte_present(pgd)) 1920 + goto out_unlock; 1921 + } 1922 + 1947 1923 info = iommu_support_dev_iotlb(domain, iommu, bus, devfn); 1948 1924 translation = info ? CONTEXT_TT_DEV_IOTLB : 1949 1925 CONTEXT_TT_MULTI_LEVEL; 1950 - } 1951 - /* 1952 - * In pass through mode, AW must be programmed to indicate the largest 1953 - * AGAW value supported by hardware. And ASR is ignored by hardware. 1954 - */ 1955 - if (unlikely(translation == CONTEXT_TT_PASS_THROUGH)) 1956 - context_set_address_width(context, iommu->msagaw); 1957 - else { 1926 + 1958 1927 context_set_address_root(context, virt_to_phys(pgd)); 1959 1928 context_set_address_width(context, iommu->agaw); 1929 + } else { 1930 + /* 1931 + * In pass through mode, AW must be programmed to 1932 + * indicate the largest AGAW value supported by 1933 + * hardware. And ASR is ignored by hardware. 1934 + */ 1935 + context_set_address_width(context, iommu->msagaw); 1960 1936 } 1961 1937 1962 1938 context_set_translation_type(context, translation); ··· 1964 1962 (((u16)bus) << 8) | devfn, 1965 1963 DMA_CCMD_MASK_NOBIT, 1966 1964 DMA_CCMD_DEVICE_INVL); 1967 - iommu->flush.flush_iotlb(iommu, id, 0, 0, DMA_TLB_DSI_FLUSH); 1965 + iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH); 1968 1966 } else { 1969 1967 iommu_flush_write_buffer(iommu); 1970 1968 } 1971 1969 iommu_enable_dev_iotlb(info); 1972 - spin_unlock_irqrestore(&iommu->lock, flags); 1973 1970 1974 - domain_attach_iommu(domain, iommu); 1971 + ret = 0; 1972 + 1973 + out_unlock: 1974 + spin_unlock(&iommu->lock); 1975 + spin_unlock_irqrestore(&device_domain_lock, flags); 1975 1976 1976 1977 return 0; 1977 1978 } ··· 1982 1977 struct domain_context_mapping_data { 1983 1978 struct dmar_domain *domain; 1984 1979 struct intel_iommu *iommu; 1985 - int translation; 1986 1980 }; 1987 1981 1988 1982 static int domain_context_mapping_cb(struct pci_dev *pdev, ··· 1990 1986 struct domain_context_mapping_data *data = opaque; 1991 1987 1992 1988 return domain_context_mapping_one(data->domain, data->iommu, 1993 - PCI_BUS_NUM(alias), alias & 0xff, 1994 - data->translation); 1989 + PCI_BUS_NUM(alias), alias & 0xff); 1995 1990 } 1996 1991 1997 1992 static int 1998 - domain_context_mapping(struct dmar_domain *domain, struct device *dev, 1999 - int translation) 1993 + domain_context_mapping(struct dmar_domain *domain, struct device *dev) 2000 1994 { 2001 1995 struct intel_iommu *iommu; 2002 1996 u8 bus, devfn; ··· 2005 2003 return -ENODEV; 2006 2004 2007 2005 if (!dev_is_pci(dev)) 2008 - return domain_context_mapping_one(domain, iommu, bus, devfn, 2009 - translation); 2006 + return domain_context_mapping_one(domain, iommu, bus, devfn); 2010 2007 2011 2008 data.domain = domain; 2012 2009 data.iommu = iommu; 2013 - data.translation = translation; 2014 2010 2015 2011 return pci_for_each_dma_alias(to_pci_dev(dev), 2016 2012 &domain_context_mapping_cb, &data); ··· 2194 2194 return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot); 2195 2195 } 2196 2196 2197 - static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn) 2197 + static void domain_context_clear_one(struct intel_iommu *iommu, u8 bus, u8 devfn) 2198 2198 { 2199 2199 if (!iommu) 2200 2200 return; ··· 2220 2220 unsigned long flags; 2221 2221 2222 2222 spin_lock_irqsave(&device_domain_lock, flags); 2223 - list_for_each_entry_safe(info, tmp, &domain->devices, link) { 2224 - unlink_domain_info(info); 2225 - spin_unlock_irqrestore(&device_domain_lock, flags); 2226 - 2227 - iommu_disable_dev_iotlb(info); 2228 - iommu_detach_dev(info->iommu, info->bus, info->devfn); 2229 - 2230 - if (domain_type_is_vm(domain)) { 2231 - iommu_detach_dependent_devices(info->iommu, info->dev); 2232 - domain_detach_iommu(domain, info->iommu); 2233 - } 2234 - 2235 - free_devinfo_mem(info); 2236 - spin_lock_irqsave(&device_domain_lock, flags); 2237 - } 2223 + list_for_each_entry_safe(info, tmp, &domain->devices, link) 2224 + __dmar_remove_one_dev_info(info); 2238 2225 spin_unlock_irqrestore(&device_domain_lock, flags); 2239 2226 } 2240 2227 ··· 2253 2266 return NULL; 2254 2267 } 2255 2268 2256 - static struct dmar_domain *dmar_insert_dev_info(struct intel_iommu *iommu, 2257 - int bus, int devfn, 2258 - struct device *dev, 2259 - struct dmar_domain *domain) 2269 + static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu, 2270 + int bus, int devfn, 2271 + struct device *dev, 2272 + struct dmar_domain *domain) 2260 2273 { 2261 2274 struct dmar_domain *found = NULL; 2262 2275 struct device_domain_info *info; 2263 2276 unsigned long flags; 2277 + int ret; 2264 2278 2265 2279 info = alloc_devinfo_mem(); 2266 2280 if (!info) ··· 2278 2290 spin_lock_irqsave(&device_domain_lock, flags); 2279 2291 if (dev) 2280 2292 found = find_domain(dev); 2281 - else { 2293 + 2294 + if (!found) { 2282 2295 struct device_domain_info *info2; 2283 2296 info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn); 2284 - if (info2) 2285 - found = info2->domain; 2297 + if (info2) { 2298 + found = info2->domain; 2299 + info2->dev = dev; 2300 + } 2286 2301 } 2302 + 2287 2303 if (found) { 2288 2304 spin_unlock_irqrestore(&device_domain_lock, flags); 2289 2305 free_devinfo_mem(info); ··· 2295 2303 return found; 2296 2304 } 2297 2305 2306 + spin_lock(&iommu->lock); 2307 + ret = domain_attach_iommu(domain, iommu); 2308 + spin_unlock(&iommu->lock); 2309 + 2310 + if (ret) { 2311 + spin_unlock_irqrestore(&device_domain_lock, flags); 2312 + return NULL; 2313 + } 2314 + 2298 2315 list_add(&info->link, &domain->devices); 2299 2316 list_add(&info->global, &device_domain_list); 2300 2317 if (dev) 2301 2318 dev->archdata.iommu = info; 2302 2319 spin_unlock_irqrestore(&device_domain_lock, flags); 2320 + 2321 + if (dev && domain_context_mapping(domain, dev)) { 2322 + pr_err("Domain context map for %s failed\n", dev_name(dev)); 2323 + dmar_remove_one_dev_info(domain, dev); 2324 + return NULL; 2325 + } 2303 2326 2304 2327 return domain; 2305 2328 } ··· 2328 2321 /* domain is initialized */ 2329 2322 static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw) 2330 2323 { 2324 + struct device_domain_info *info = NULL; 2331 2325 struct dmar_domain *domain, *tmp; 2332 2326 struct intel_iommu *iommu; 2333 - struct device_domain_info *info; 2334 - u16 dma_alias; 2327 + u16 req_id, dma_alias; 2335 2328 unsigned long flags; 2336 2329 u8 bus, devfn; 2337 2330 ··· 2342 2335 iommu = device_to_iommu(dev, &bus, &devfn); 2343 2336 if (!iommu) 2344 2337 return NULL; 2338 + 2339 + req_id = ((u16)bus << 8) | devfn; 2345 2340 2346 2341 if (dev_is_pci(dev)) { 2347 2342 struct pci_dev *pdev = to_pci_dev(dev); ··· 2369 2360 domain = alloc_domain(0); 2370 2361 if (!domain) 2371 2362 return NULL; 2372 - domain->id = iommu_attach_domain(domain, iommu); 2373 - if (domain->id < 0) { 2374 - free_domain_mem(domain); 2375 - return NULL; 2376 - } 2377 - domain_attach_iommu(domain, iommu); 2378 - if (domain_init(domain, gaw)) { 2363 + if (domain_init(domain, iommu, gaw)) { 2379 2364 domain_exit(domain); 2380 2365 return NULL; 2381 2366 } 2382 2367 2383 2368 /* register PCI DMA alias device */ 2384 - if (dev_is_pci(dev)) { 2385 - tmp = dmar_insert_dev_info(iommu, PCI_BUS_NUM(dma_alias), 2386 - dma_alias & 0xff, NULL, domain); 2369 + if (req_id != dma_alias && dev_is_pci(dev)) { 2370 + tmp = dmar_insert_one_dev_info(iommu, PCI_BUS_NUM(dma_alias), 2371 + dma_alias & 0xff, NULL, domain); 2387 2372 2388 2373 if (!tmp || tmp != domain) { 2389 2374 domain_exit(domain); ··· 2389 2386 } 2390 2387 2391 2388 found_domain: 2392 - tmp = dmar_insert_dev_info(iommu, bus, devfn, dev, domain); 2389 + tmp = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain); 2393 2390 2394 2391 if (!tmp || tmp != domain) { 2395 2392 domain_exit(domain); ··· 2417 2414 return -ENOMEM; 2418 2415 } 2419 2416 2420 - pr_debug("Mapping reserved region %llx-%llx for domain %d\n", 2421 - start, end, domain->id); 2417 + pr_debug("Mapping reserved region %llx-%llx\n", start, end); 2422 2418 /* 2423 2419 * RMRR range might have overlap with physical memory range, 2424 2420 * clear it first ··· 2478 2476 if (ret) 2479 2477 goto error; 2480 2478 2481 - /* context entry init */ 2482 - ret = domain_context_mapping(domain, dev, CONTEXT_TT_MULTI_LEVEL); 2483 - if (ret) 2484 - goto error; 2485 - 2486 2479 return 0; 2487 2480 2488 2481 error: ··· 2523 2526 2524 2527 static int __init si_domain_init(int hw) 2525 2528 { 2526 - struct dmar_drhd_unit *drhd; 2527 - struct intel_iommu *iommu; 2528 2529 int nid, ret = 0; 2529 - bool first = true; 2530 2530 2531 2531 si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY); 2532 2532 if (!si_domain) 2533 2533 return -EFAULT; 2534 - 2535 - for_each_active_iommu(iommu, drhd) { 2536 - ret = iommu_attach_domain(si_domain, iommu); 2537 - if (ret < 0) { 2538 - domain_exit(si_domain); 2539 - return -EFAULT; 2540 - } else if (first) { 2541 - si_domain->id = ret; 2542 - first = false; 2543 - } else if (si_domain->id != ret) { 2544 - domain_exit(si_domain); 2545 - return -EFAULT; 2546 - } 2547 - domain_attach_iommu(si_domain, iommu); 2548 - } 2549 2534 2550 2535 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) { 2551 2536 domain_exit(si_domain); 2552 2537 return -EFAULT; 2553 2538 } 2554 2539 2555 - pr_debug("Identity mapping domain is domain %d\n", 2556 - si_domain->id); 2540 + pr_debug("Identity mapping domain allocated\n"); 2557 2541 2558 2542 if (hw) 2559 2543 return 0; ··· 2568 2590 return 0; 2569 2591 } 2570 2592 2571 - static int domain_add_dev_info(struct dmar_domain *domain, 2572 - struct device *dev, int translation) 2593 + static int domain_add_dev_info(struct dmar_domain *domain, struct device *dev) 2573 2594 { 2574 2595 struct dmar_domain *ndomain; 2575 2596 struct intel_iommu *iommu; 2576 2597 u8 bus, devfn; 2577 - int ret; 2578 2598 2579 2599 iommu = device_to_iommu(dev, &bus, &devfn); 2580 2600 if (!iommu) 2581 2601 return -ENODEV; 2582 2602 2583 - ndomain = dmar_insert_dev_info(iommu, bus, devfn, dev, domain); 2603 + ndomain = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain); 2584 2604 if (ndomain != domain) 2585 2605 return -EBUSY; 2586 - 2587 - ret = domain_context_mapping(domain, dev, translation); 2588 - if (ret) { 2589 - domain_remove_one_dev_info(domain, dev); 2590 - return ret; 2591 - } 2592 2606 2593 2607 return 0; 2594 2608 } ··· 2721 2751 if (!iommu_should_identity_map(dev, 1)) 2722 2752 return 0; 2723 2753 2724 - ret = domain_add_dev_info(si_domain, dev, 2725 - hw ? CONTEXT_TT_PASS_THROUGH : 2726 - CONTEXT_TT_MULTI_LEVEL); 2754 + ret = domain_add_dev_info(si_domain, dev); 2727 2755 if (!ret) 2728 2756 pr_info("%s identity mapping for device %s\n", 2729 2757 hw ? "Hardware" : "Software", dev_name(dev)); ··· 2807 2839 } 2808 2840 2809 2841 static int copy_context_table(struct intel_iommu *iommu, 2810 - struct root_entry *old_re, 2842 + struct root_entry __iomem *old_re, 2811 2843 struct context_entry **tbl, 2812 2844 int bus, bool ext) 2813 2845 { 2814 - struct context_entry *old_ce = NULL, *new_ce = NULL, ce; 2815 2846 int tbl_idx, pos = 0, idx, devfn, ret = 0, did; 2847 + struct context_entry __iomem *old_ce = NULL; 2848 + struct context_entry *new_ce = NULL, ce; 2849 + struct root_entry re; 2816 2850 phys_addr_t old_ce_phys; 2817 2851 2818 2852 tbl_idx = ext ? bus * 2 : bus; 2853 + memcpy_fromio(&re, old_re, sizeof(re)); 2819 2854 2820 2855 for (devfn = 0; devfn < 256; devfn++) { 2821 2856 /* First calculate the correct index */ ··· 2838 2867 2839 2868 ret = 0; 2840 2869 if (devfn < 0x80) 2841 - old_ce_phys = root_entry_lctp(old_re); 2870 + old_ce_phys = root_entry_lctp(&re); 2842 2871 else 2843 - old_ce_phys = root_entry_uctp(old_re); 2872 + old_ce_phys = root_entry_uctp(&re); 2844 2873 2845 2874 if (!old_ce_phys) { 2846 2875 if (ext && devfn == 0) { ··· 2865 2894 } 2866 2895 2867 2896 /* Now copy the context entry */ 2868 - ce = old_ce[idx]; 2897 + memcpy_fromio(&ce, old_ce + idx, sizeof(ce)); 2869 2898 2870 2899 if (!__context_present(&ce)) 2871 2900 continue; ··· 2909 2938 2910 2939 static int copy_translation_tables(struct intel_iommu *iommu) 2911 2940 { 2941 + struct root_entry __iomem *old_rt; 2912 2942 struct context_entry **ctxt_tbls; 2913 - struct root_entry *old_rt; 2914 2943 phys_addr_t old_rt_phys; 2915 2944 int ctxt_table_entries; 2916 2945 unsigned long flags; ··· 3240 3269 static struct dmar_domain *__get_valid_domain_for_dev(struct device *dev) 3241 3270 { 3242 3271 struct dmar_domain *domain; 3243 - int ret; 3244 3272 3245 3273 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH); 3246 3274 if (!domain) { 3247 3275 pr_err("Allocating domain for %s failed\n", 3248 3276 dev_name(dev)); 3249 3277 return NULL; 3250 - } 3251 - 3252 - /* make sure context mapping is ok */ 3253 - if (unlikely(!domain_context_mapped(dev))) { 3254 - ret = domain_context_mapping(domain, dev, CONTEXT_TT_MULTI_LEVEL); 3255 - if (ret) { 3256 - pr_err("Domain context map for %s failed\n", 3257 - dev_name(dev)); 3258 - return NULL; 3259 - } 3260 3278 } 3261 3279 3262 3280 return domain; ··· 3283 3323 * 32 bit DMA is removed from si_domain and fall back 3284 3324 * to non-identity mapping. 3285 3325 */ 3286 - domain_remove_one_dev_info(si_domain, dev); 3326 + dmar_remove_one_dev_info(si_domain, dev); 3287 3327 pr_info("32bit %s uses non-identity mapping\n", 3288 3328 dev_name(dev)); 3289 3329 return 0; ··· 3295 3335 */ 3296 3336 if (iommu_should_identity_map(dev, 0)) { 3297 3337 int ret; 3298 - ret = domain_add_dev_info(si_domain, dev, 3299 - hw_pass_through ? 3300 - CONTEXT_TT_PASS_THROUGH : 3301 - CONTEXT_TT_MULTI_LEVEL); 3338 + ret = domain_add_dev_info(si_domain, dev); 3302 3339 if (!ret) { 3303 3340 pr_info("64bit %s uses identity mapping\n", 3304 3341 dev_name(dev)); ··· 3356 3399 3357 3400 /* it's a non-present to present mapping. Only flush if caching mode */ 3358 3401 if (cap_caching_mode(iommu->cap)) 3359 - iommu_flush_iotlb_psi(iommu, domain->id, mm_to_dma_pfn(iova->pfn_lo), size, 0, 1); 3402 + iommu_flush_iotlb_psi(iommu, domain, 3403 + mm_to_dma_pfn(iova->pfn_lo), 3404 + size, 0, 1); 3360 3405 else 3361 3406 iommu_flush_write_buffer(iommu); 3362 3407 ··· 3409 3450 3410 3451 /* On real hardware multiple invalidations are expensive */ 3411 3452 if (cap_caching_mode(iommu->cap)) 3412 - iommu_flush_iotlb_psi(iommu, domain->id, 3453 + iommu_flush_iotlb_psi(iommu, domain, 3413 3454 iova->pfn_lo, iova_size(iova), 3414 3455 !deferred_flush[i].freelist[j], 0); 3415 3456 else { ··· 3493 3534 freelist = domain_unmap(domain, start_pfn, last_pfn); 3494 3535 3495 3536 if (intel_iommu_strict) { 3496 - iommu_flush_iotlb_psi(iommu, domain->id, start_pfn, 3537 + iommu_flush_iotlb_psi(iommu, domain, start_pfn, 3497 3538 last_pfn - start_pfn + 1, !freelist, 0); 3498 3539 /* free iova */ 3499 3540 __free_iova(&domain->iovad, iova); ··· 3651 3692 3652 3693 /* it's a non-present to present mapping. Only flush if caching mode */ 3653 3694 if (cap_caching_mode(iommu->cap)) 3654 - iommu_flush_iotlb_psi(iommu, domain->id, start_vpfn, size, 0, 1); 3695 + iommu_flush_iotlb_psi(iommu, domain, start_vpfn, size, 0, 1); 3655 3696 else 3656 3697 iommu_flush_write_buffer(iommu); 3657 3698 ··· 4128 4169 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); 4129 4170 iommu_enable_translation(iommu); 4130 4171 4131 - if (si_domain) { 4132 - ret = iommu_attach_domain(si_domain, iommu); 4133 - if (ret < 0 || si_domain->id != ret) 4134 - goto disable_iommu; 4135 - domain_attach_iommu(si_domain, iommu); 4136 - } 4137 - 4138 4172 iommu_disable_protect_mem_regions(iommu); 4139 4173 return 0; 4140 4174 ··· 4289 4337 if (!domain) 4290 4338 return 0; 4291 4339 4292 - down_read(&dmar_global_lock); 4293 - domain_remove_one_dev_info(domain, dev); 4340 + dmar_remove_one_dev_info(domain, dev); 4294 4341 if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices)) 4295 4342 domain_exit(domain); 4296 - up_read(&dmar_global_lock); 4297 4343 4298 4344 return 0; 4299 4345 } ··· 4348 4398 4349 4399 rcu_read_lock(); 4350 4400 for_each_active_iommu(iommu, drhd) 4351 - iommu_flush_iotlb_psi(iommu, si_domain->id, 4401 + iommu_flush_iotlb_psi(iommu, si_domain, 4352 4402 iova->pfn_lo, iova_size(iova), 4353 4403 !freelist, 0); 4354 4404 rcu_read_unlock(); ··· 4407 4457 } 4408 4458 static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL); 4409 4459 4460 + static ssize_t intel_iommu_show_ndoms(struct device *dev, 4461 + struct device_attribute *attr, 4462 + char *buf) 4463 + { 4464 + struct intel_iommu *iommu = dev_get_drvdata(dev); 4465 + return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap)); 4466 + } 4467 + static DEVICE_ATTR(domains_supported, S_IRUGO, intel_iommu_show_ndoms, NULL); 4468 + 4469 + static ssize_t intel_iommu_show_ndoms_used(struct device *dev, 4470 + struct device_attribute *attr, 4471 + char *buf) 4472 + { 4473 + struct intel_iommu *iommu = dev_get_drvdata(dev); 4474 + return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids, 4475 + cap_ndoms(iommu->cap))); 4476 + } 4477 + static DEVICE_ATTR(domains_used, S_IRUGO, intel_iommu_show_ndoms_used, NULL); 4478 + 4410 4479 static struct attribute *intel_iommu_attrs[] = { 4411 4480 &dev_attr_version.attr, 4412 4481 &dev_attr_address.attr, 4413 4482 &dev_attr_cap.attr, 4414 4483 &dev_attr_ecap.attr, 4484 + &dev_attr_domains_supported.attr, 4485 + &dev_attr_domains_used.attr, 4415 4486 NULL, 4416 4487 }; 4417 4488 ··· 4512 4541 for_each_active_iommu(iommu, drhd) 4513 4542 iommu->iommu_dev = iommu_device_create(NULL, iommu, 4514 4543 intel_iommu_groups, 4515 - iommu->name); 4544 + "%s", iommu->name); 4516 4545 4517 4546 bus_set_iommu(&pci_bus_type, &intel_iommu_ops); 4518 4547 bus_register_notifier(&pci_bus_type, &device_nb); ··· 4532 4561 return ret; 4533 4562 } 4534 4563 4535 - static int iommu_detach_dev_cb(struct pci_dev *pdev, u16 alias, void *opaque) 4564 + static int domain_context_clear_one_cb(struct pci_dev *pdev, u16 alias, void *opaque) 4536 4565 { 4537 4566 struct intel_iommu *iommu = opaque; 4538 4567 4539 - iommu_detach_dev(iommu, PCI_BUS_NUM(alias), alias & 0xff); 4568 + domain_context_clear_one(iommu, PCI_BUS_NUM(alias), alias & 0xff); 4540 4569 return 0; 4541 4570 } 4542 4571 ··· 4546 4575 * devices, unbinding the driver from any one of them will possibly leave 4547 4576 * the others unable to operate. 4548 4577 */ 4549 - static void iommu_detach_dependent_devices(struct intel_iommu *iommu, 4550 - struct device *dev) 4578 + static void domain_context_clear(struct intel_iommu *iommu, struct device *dev) 4551 4579 { 4552 4580 if (!iommu || !dev || !dev_is_pci(dev)) 4553 4581 return; 4554 4582 4555 - pci_for_each_dma_alias(to_pci_dev(dev), &iommu_detach_dev_cb, iommu); 4583 + pci_for_each_dma_alias(to_pci_dev(dev), &domain_context_clear_one_cb, iommu); 4556 4584 } 4557 4585 4558 - static void domain_remove_one_dev_info(struct dmar_domain *domain, 4559 - struct device *dev) 4586 + static void __dmar_remove_one_dev_info(struct device_domain_info *info) 4560 4587 { 4561 - struct device_domain_info *info, *tmp; 4562 4588 struct intel_iommu *iommu; 4563 4589 unsigned long flags; 4564 - bool found = false; 4565 - u8 bus, devfn; 4566 4590 4567 - iommu = device_to_iommu(dev, &bus, &devfn); 4568 - if (!iommu) 4591 + assert_spin_locked(&device_domain_lock); 4592 + 4593 + if (WARN_ON(!info)) 4569 4594 return; 4570 4595 4596 + iommu = info->iommu; 4597 + 4598 + if (info->dev) { 4599 + iommu_disable_dev_iotlb(info); 4600 + domain_context_clear(iommu, info->dev); 4601 + } 4602 + 4603 + unlink_domain_info(info); 4604 + 4605 + spin_lock_irqsave(&iommu->lock, flags); 4606 + domain_detach_iommu(info->domain, iommu); 4607 + spin_unlock_irqrestore(&iommu->lock, flags); 4608 + 4609 + free_devinfo_mem(info); 4610 + } 4611 + 4612 + static void dmar_remove_one_dev_info(struct dmar_domain *domain, 4613 + struct device *dev) 4614 + { 4615 + struct device_domain_info *info; 4616 + unsigned long flags; 4617 + 4571 4618 spin_lock_irqsave(&device_domain_lock, flags); 4572 - list_for_each_entry_safe(info, tmp, &domain->devices, link) { 4573 - if (info->iommu == iommu && info->bus == bus && 4574 - info->devfn == devfn) { 4575 - unlink_domain_info(info); 4576 - spin_unlock_irqrestore(&device_domain_lock, flags); 4577 - 4578 - iommu_disable_dev_iotlb(info); 4579 - iommu_detach_dev(iommu, info->bus, info->devfn); 4580 - iommu_detach_dependent_devices(iommu, dev); 4581 - free_devinfo_mem(info); 4582 - 4583 - spin_lock_irqsave(&device_domain_lock, flags); 4584 - 4585 - if (found) 4586 - break; 4587 - else 4588 - continue; 4589 - } 4590 - 4591 - /* if there is no other devices under the same iommu 4592 - * owned by this domain, clear this iommu in iommu_bmp 4593 - * update iommu count and coherency 4594 - */ 4595 - if (info->iommu == iommu) 4596 - found = true; 4597 - } 4598 - 4619 + info = dev->archdata.iommu; 4620 + __dmar_remove_one_dev_info(info); 4599 4621 spin_unlock_irqrestore(&device_domain_lock, flags); 4600 - 4601 - if (found == 0) { 4602 - domain_detach_iommu(domain, iommu); 4603 - if (!domain_type_is_vm_or_si(domain)) 4604 - iommu_detach_domain(domain, iommu); 4605 - } 4606 4622 } 4607 4623 4608 4624 static int md_domain_init(struct dmar_domain *domain, int guest_width) ··· 4670 4712 4671 4713 old_domain = find_domain(dev); 4672 4714 if (old_domain) { 4673 - if (domain_type_is_vm_or_si(dmar_domain)) 4674 - domain_remove_one_dev_info(old_domain, dev); 4675 - else 4676 - domain_remove_dev_info(old_domain); 4715 + rcu_read_lock(); 4716 + dmar_remove_one_dev_info(old_domain, dev); 4717 + rcu_read_unlock(); 4677 4718 4678 4719 if (!domain_type_is_vm_or_si(old_domain) && 4679 4720 list_empty(&old_domain->devices)) ··· 4712 4755 dmar_domain->agaw--; 4713 4756 } 4714 4757 4715 - return domain_add_dev_info(dmar_domain, dev, CONTEXT_TT_MULTI_LEVEL); 4758 + return domain_add_dev_info(dmar_domain, dev); 4716 4759 } 4717 4760 4718 4761 static void intel_iommu_detach_device(struct iommu_domain *domain, 4719 4762 struct device *dev) 4720 4763 { 4721 - domain_remove_one_dev_info(to_dmar_domain(domain), dev); 4764 + dmar_remove_one_dev_info(to_dmar_domain(domain), dev); 4722 4765 } 4723 4766 4724 4767 static int intel_iommu_map(struct iommu_domain *domain, ··· 4767 4810 struct intel_iommu *iommu; 4768 4811 unsigned long start_pfn, last_pfn; 4769 4812 unsigned int npages; 4770 - int iommu_id, num, ndomains, level = 0; 4813 + int iommu_id, level = 0; 4771 4814 4772 4815 /* Cope with horrid API which requires us to unmap more than the 4773 4816 size argument if it happens to be a large-page mapping. */ 4774 - if (!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level)) 4775 - BUG(); 4817 + BUG_ON(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level)); 4776 4818 4777 4819 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level)) 4778 4820 size = VTD_PAGE_SIZE << level_to_offset_bits(level); ··· 4783 4827 4784 4828 npages = last_pfn - start_pfn + 1; 4785 4829 4786 - for_each_set_bit(iommu_id, dmar_domain->iommu_bmp, g_num_of_iommus) { 4787 - iommu = g_iommus[iommu_id]; 4830 + for_each_domain_iommu(iommu_id, dmar_domain) { 4831 + iommu = g_iommus[iommu_id]; 4788 4832 4789 - /* 4790 - * find bit position of dmar_domain 4791 - */ 4792 - ndomains = cap_ndoms(iommu->cap); 4793 - for_each_set_bit(num, iommu->domain_ids, ndomains) { 4794 - if (iommu->domains[num] == dmar_domain) 4795 - iommu_flush_iotlb_psi(iommu, num, start_pfn, 4796 - npages, !freelist, 0); 4797 - } 4798 - 4833 + iommu_flush_iotlb_psi(g_iommus[iommu_id], dmar_domain, 4834 + start_pfn, npages, !freelist, 0); 4799 4835 } 4800 4836 4801 4837 dma_free_pagelist(freelist);
+4 -2
drivers/iommu/intel_irq_remapping.c
··· 384 384 385 385 static int iommu_load_old_irte(struct intel_iommu *iommu) 386 386 { 387 - struct irte *old_ir_table; 387 + struct irte __iomem *old_ir_table; 388 388 phys_addr_t irt_phys; 389 389 unsigned int i; 390 390 size_t size; ··· 413 413 return -ENOMEM; 414 414 415 415 /* Copy data over */ 416 - memcpy(iommu->ir_table->base, old_ir_table, size); 416 + memcpy_fromio(iommu->ir_table->base, old_ir_table, size); 417 417 418 418 __iommu_flush_cache(iommu, iommu->ir_table->base, size); 419 419 ··· 425 425 if (iommu->ir_table->base[i].present) 426 426 bitmap_set(iommu->ir_table->bitmap, i, 1); 427 427 } 428 + 429 + iounmap(old_ir_table); 428 430 429 431 return 0; 430 432 }
+109 -34
drivers/iommu/io-pgtable-arm.c
··· 26 26 #include <linux/slab.h> 27 27 #include <linux/types.h> 28 28 29 + #include <asm/barrier.h> 30 + 29 31 #include "io-pgtable.h" 30 32 31 33 #define ARM_LPAE_MAX_ADDR_BITS 48 ··· 202 200 203 201 static bool selftest_running = false; 204 202 203 + static dma_addr_t __arm_lpae_dma_addr(struct device *dev, void *pages) 204 + { 205 + return phys_to_dma(dev, virt_to_phys(pages)); 206 + } 207 + 208 + static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp, 209 + struct io_pgtable_cfg *cfg) 210 + { 211 + struct device *dev = cfg->iommu_dev; 212 + dma_addr_t dma; 213 + void *pages = alloc_pages_exact(size, gfp | __GFP_ZERO); 214 + 215 + if (!pages) 216 + return NULL; 217 + 218 + if (!selftest_running) { 219 + dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE); 220 + if (dma_mapping_error(dev, dma)) 221 + goto out_free; 222 + /* 223 + * We depend on the IOMMU being able to work with any physical 224 + * address directly, so if the DMA layer suggests it can't by 225 + * giving us back some translation, that bodes very badly... 226 + */ 227 + if (dma != __arm_lpae_dma_addr(dev, pages)) 228 + goto out_unmap; 229 + } 230 + 231 + return pages; 232 + 233 + out_unmap: 234 + dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); 235 + dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); 236 + out_free: 237 + free_pages_exact(pages, size); 238 + return NULL; 239 + } 240 + 241 + static void __arm_lpae_free_pages(void *pages, size_t size, 242 + struct io_pgtable_cfg *cfg) 243 + { 244 + struct device *dev = cfg->iommu_dev; 245 + 246 + if (!selftest_running) 247 + dma_unmap_single(dev, __arm_lpae_dma_addr(dev, pages), 248 + size, DMA_TO_DEVICE); 249 + free_pages_exact(pages, size); 250 + } 251 + 252 + static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte, 253 + struct io_pgtable_cfg *cfg) 254 + { 255 + struct device *dev = cfg->iommu_dev; 256 + 257 + *ptep = pte; 258 + 259 + if (!selftest_running) 260 + dma_sync_single_for_device(dev, __arm_lpae_dma_addr(dev, ptep), 261 + sizeof(pte), DMA_TO_DEVICE); 262 + } 263 + 264 + static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 265 + unsigned long iova, size_t size, int lvl, 266 + arm_lpae_iopte *ptep); 267 + 205 268 static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 206 269 unsigned long iova, phys_addr_t paddr, 207 270 arm_lpae_iopte prot, int lvl, 208 271 arm_lpae_iopte *ptep) 209 272 { 210 273 arm_lpae_iopte pte = prot; 274 + struct io_pgtable_cfg *cfg = &data->iop.cfg; 211 275 212 - /* We require an unmap first */ 213 276 if (iopte_leaf(*ptep, lvl)) { 277 + /* We require an unmap first */ 214 278 WARN_ON(!selftest_running); 215 279 return -EEXIST; 280 + } else if (iopte_type(*ptep, lvl) == ARM_LPAE_PTE_TYPE_TABLE) { 281 + /* 282 + * We need to unmap and free the old table before 283 + * overwriting it with a block entry. 284 + */ 285 + arm_lpae_iopte *tblp; 286 + size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 287 + 288 + tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data); 289 + if (WARN_ON(__arm_lpae_unmap(data, iova, sz, lvl, tblp) != sz)) 290 + return -EINVAL; 216 291 } 217 292 218 - if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS) 293 + if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) 219 294 pte |= ARM_LPAE_PTE_NS; 220 295 221 296 if (lvl == ARM_LPAE_MAX_LEVELS - 1) ··· 303 224 pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS; 304 225 pte |= pfn_to_iopte(paddr >> data->pg_shift, data); 305 226 306 - *ptep = pte; 307 - data->iop.cfg.tlb->flush_pgtable(ptep, sizeof(*ptep), data->iop.cookie); 227 + __arm_lpae_set_pte(ptep, pte, cfg); 308 228 return 0; 309 229 } 310 230 ··· 312 234 int lvl, arm_lpae_iopte *ptep) 313 235 { 314 236 arm_lpae_iopte *cptep, pte; 315 - void *cookie = data->iop.cookie; 316 237 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); 238 + struct io_pgtable_cfg *cfg = &data->iop.cfg; 317 239 318 240 /* Find our entry at the current level */ 319 241 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 320 242 321 243 /* If we can install a leaf entry at this level, then do so */ 322 - if (size == block_size && (size & data->iop.cfg.pgsize_bitmap)) 244 + if (size == block_size && (size & cfg->pgsize_bitmap)) 323 245 return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep); 324 246 325 247 /* We can't allocate tables at the final level */ ··· 329 251 /* Grab a pointer to the next level */ 330 252 pte = *ptep; 331 253 if (!pte) { 332 - cptep = alloc_pages_exact(1UL << data->pg_shift, 333 - GFP_ATOMIC | __GFP_ZERO); 254 + cptep = __arm_lpae_alloc_pages(1UL << data->pg_shift, 255 + GFP_ATOMIC, cfg); 334 256 if (!cptep) 335 257 return -ENOMEM; 336 258 337 - data->iop.cfg.tlb->flush_pgtable(cptep, 1UL << data->pg_shift, 338 - cookie); 339 259 pte = __pa(cptep) | ARM_LPAE_PTE_TYPE_TABLE; 340 - if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS) 260 + if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) 341 261 pte |= ARM_LPAE_PTE_NSTABLE; 342 - *ptep = pte; 343 - data->iop.cfg.tlb->flush_pgtable(ptep, sizeof(*ptep), cookie); 262 + __arm_lpae_set_pte(ptep, pte, cfg); 344 263 } else { 345 264 cptep = iopte_deref(pte, data); 346 265 } ··· 384 309 { 385 310 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 386 311 arm_lpae_iopte *ptep = data->pgd; 387 - int lvl = ARM_LPAE_START_LVL(data); 312 + int ret, lvl = ARM_LPAE_START_LVL(data); 388 313 arm_lpae_iopte prot; 389 314 390 315 /* If no access, then nothing to do */ ··· 392 317 return 0; 393 318 394 319 prot = arm_lpae_prot_to_pte(data, iommu_prot); 395 - return __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep); 320 + ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep); 321 + /* 322 + * Synchronise all PTE updates for the new mapping before there's 323 + * a chance for anything to kick off a table walk for the new iova. 324 + */ 325 + wmb(); 326 + 327 + return ret; 396 328 } 397 329 398 330 static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, ··· 429 347 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); 430 348 } 431 349 432 - free_pages_exact(start, table_size); 350 + __arm_lpae_free_pages(start, table_size, &data->iop.cfg); 433 351 } 434 352 435 353 static void arm_lpae_free_pgtable(struct io_pgtable *iop) ··· 448 366 unsigned long blk_start, blk_end; 449 367 phys_addr_t blk_paddr; 450 368 arm_lpae_iopte table = 0; 451 - void *cookie = data->iop.cookie; 452 - const struct iommu_gather_ops *tlb = data->iop.cfg.tlb; 369 + struct io_pgtable_cfg *cfg = &data->iop.cfg; 453 370 454 371 blk_start = iova & ~(blk_size - 1); 455 372 blk_end = blk_start + blk_size; ··· 474 393 } 475 394 } 476 395 477 - *ptep = table; 478 - tlb->flush_pgtable(ptep, sizeof(*ptep), cookie); 396 + __arm_lpae_set_pte(ptep, table, cfg); 479 397 iova &= ~(blk_size - 1); 480 - tlb->tlb_add_flush(iova, blk_size, true, cookie); 398 + cfg->tlb->tlb_add_flush(iova, blk_size, true, data->iop.cookie); 481 399 return size; 482 400 } 483 401 ··· 498 418 499 419 /* If the size matches this level, we're in the right place */ 500 420 if (size == blk_size) { 501 - *ptep = 0; 502 - tlb->flush_pgtable(ptep, sizeof(*ptep), cookie); 421 + __arm_lpae_set_pte(ptep, 0, &data->iop.cfg); 503 422 504 423 if (!iopte_leaf(pte, lvl)) { 505 424 /* Also flush any partial walks */ 506 425 tlb->tlb_add_flush(iova, size, false, cookie); 507 - tlb->tlb_sync(data->iop.cookie); 426 + tlb->tlb_sync(cookie); 508 427 ptep = iopte_deref(pte, data); 509 428 __arm_lpae_free_pgtable(data, lvl + 1, ptep); 510 429 } else { ··· 719 640 cfg->arm_lpae_s1_cfg.mair[1] = 0; 720 641 721 642 /* Looking good; allocate a pgd */ 722 - data->pgd = alloc_pages_exact(data->pgd_size, GFP_KERNEL | __GFP_ZERO); 643 + data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg); 723 644 if (!data->pgd) 724 645 goto out_free_data; 725 646 726 - cfg->tlb->flush_pgtable(data->pgd, data->pgd_size, cookie); 647 + /* Ensure the empty pgd is visible before any actual TTBR write */ 648 + wmb(); 727 649 728 650 /* TTBRs */ 729 651 cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd); ··· 808 728 cfg->arm_lpae_s2_cfg.vtcr = reg; 809 729 810 730 /* Allocate pgd pages */ 811 - data->pgd = alloc_pages_exact(data->pgd_size, GFP_KERNEL | __GFP_ZERO); 731 + data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg); 812 732 if (!data->pgd) 813 733 goto out_free_data; 814 734 815 - cfg->tlb->flush_pgtable(data->pgd, data->pgd_size, cookie); 735 + /* Ensure the empty pgd is visible before any actual TTBR write */ 736 + wmb(); 816 737 817 738 /* VTTBR */ 818 739 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); ··· 899 818 WARN_ON(cookie != cfg_cookie); 900 819 } 901 820 902 - static void dummy_flush_pgtable(void *ptr, size_t size, void *cookie) 903 - { 904 - WARN_ON(cookie != cfg_cookie); 905 - } 906 - 907 821 static struct iommu_gather_ops dummy_tlb_ops __initdata = { 908 822 .tlb_flush_all = dummy_tlb_flush_all, 909 823 .tlb_add_flush = dummy_tlb_add_flush, 910 824 .tlb_sync = dummy_tlb_sync, 911 - .flush_pgtable = dummy_flush_pgtable, 912 825 }; 913 826 914 827 static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
-5
drivers/iommu/io-pgtable.c
··· 24 24 25 25 #include "io-pgtable.h" 26 26 27 - extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns; 28 - extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns; 29 - extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns; 30 - extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns; 31 - 32 27 static const struct io_pgtable_init_fns * 33 28 io_pgtable_init_table[IO_PGTABLE_NUM_FMTS] = 34 29 {
+11 -3
drivers/iommu/io-pgtable.h
··· 17 17 * 18 18 * @tlb_flush_all: Synchronously invalidate the entire TLB context. 19 19 * @tlb_add_flush: Queue up a TLB invalidation for a virtual address range. 20 - * @tlb_sync: Ensure any queue TLB invalidation has taken effect. 21 - * @flush_pgtable: Ensure page table updates are visible to the IOMMU. 20 + * @tlb_sync: Ensure any queued TLB invalidation has taken effect, and 21 + * any corresponding page table updates are visible to the 22 + * IOMMU. 22 23 * 23 24 * Note that these can all be called in atomic context and must therefore 24 25 * not block. ··· 29 28 void (*tlb_add_flush)(unsigned long iova, size_t size, bool leaf, 30 29 void *cookie); 31 30 void (*tlb_sync)(void *cookie); 32 - void (*flush_pgtable)(void *ptr, size_t size, void *cookie); 33 31 }; 34 32 35 33 /** ··· 41 41 * @ias: Input address (iova) size, in bits. 42 42 * @oas: Output address (paddr) size, in bits. 43 43 * @tlb: TLB management callbacks for this set of tables. 44 + * @iommu_dev: The device representing the DMA configuration for the 45 + * page table walker. 44 46 */ 45 47 struct io_pgtable_cfg { 46 48 #define IO_PGTABLE_QUIRK_ARM_NS (1 << 0) /* Set NS bit in PTEs */ ··· 51 49 unsigned int ias; 52 50 unsigned int oas; 53 51 const struct iommu_gather_ops *tlb; 52 + struct device *iommu_dev; 54 53 55 54 /* Low-level data specific to the table format */ 56 55 union { ··· 142 139 struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie); 143 140 void (*free)(struct io_pgtable *iop); 144 141 }; 142 + 143 + extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns; 144 + extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns; 145 + extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns; 146 + extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns; 145 147 146 148 #endif /* __IO_PGTABLE_H */
+5 -14
drivers/iommu/ipmmu-vmsa.c
··· 283 283 /* The hardware doesn't support selective TLB flush. */ 284 284 } 285 285 286 - static void ipmmu_flush_pgtable(void *ptr, size_t size, void *cookie) 287 - { 288 - unsigned long offset = (unsigned long)ptr & ~PAGE_MASK; 289 - struct ipmmu_vmsa_domain *domain = cookie; 290 - 291 - /* 292 - * TODO: Add support for coherent walk through CCI with DVM and remove 293 - * cache handling. 294 - */ 295 - dma_map_page(domain->mmu->dev, virt_to_page(ptr), offset, size, 296 - DMA_TO_DEVICE); 297 - } 298 - 299 286 static struct iommu_gather_ops ipmmu_gather_ops = { 300 287 .tlb_flush_all = ipmmu_tlb_flush_all, 301 288 .tlb_add_flush = ipmmu_tlb_add_flush, 302 289 .tlb_sync = ipmmu_tlb_flush_all, 303 - .flush_pgtable = ipmmu_flush_pgtable, 304 290 }; 305 291 306 292 /* ----------------------------------------------------------------------------- ··· 313 327 domain->cfg.ias = 32; 314 328 domain->cfg.oas = 40; 315 329 domain->cfg.tlb = &ipmmu_gather_ops; 330 + /* 331 + * TODO: Add support for coherent walk through CCI with DVM and remove 332 + * cache handling. For now, delegate it to the io-pgtable code. 333 + */ 334 + domain->cfg.iommu_dev = domain->mmu->dev; 316 335 317 336 domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg, 318 337 domain);
+1 -1
drivers/iommu/irq_remapping.c
··· 84 84 bool irq_remapping_cap(enum irq_remap_cap cap) 85 85 { 86 86 if (!remap_ops || disable_irq_post) 87 - return 0; 87 + return false; 88 88 89 89 return (remap_ops->capability & (1 << cap)); 90 90 }
+2 -2
drivers/iommu/msm_iommu.c
··· 106 106 #endif 107 107 108 108 list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) { 109 - if (!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent) 110 - BUG(); 109 + 110 + BUG_ON(!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent); 111 111 112 112 iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent); 113 113 BUG_ON(!iommu_drvdata);
+5 -3
drivers/iommu/of_iommu.c
··· 141 141 struct iommu_ops *ops = NULL; 142 142 int idx = 0; 143 143 144 - if (dev_is_pci(dev)) { 145 - dev_err(dev, "IOMMU is currently not supported for PCI\n"); 144 + /* 145 + * We can't do much for PCI devices without knowing how 146 + * device IDs are wired up from the PCI bus to the IOMMU. 147 + */ 148 + if (dev_is_pci(dev)) 146 149 return NULL; 147 - } 148 150 149 151 /* 150 152 * We don't currently walk up the tree looking for a parent IOMMU.
+111 -20
drivers/iommu/omap-iommu-debug.c
··· 14 14 #include <linux/io.h> 15 15 #include <linux/slab.h> 16 16 #include <linux/uaccess.h> 17 + #include <linux/pm_runtime.h> 17 18 #include <linux/debugfs.h> 18 19 #include <linux/platform_data/iommu-omap.h> 19 20 ··· 28 27 static inline bool is_omap_iommu_detached(struct omap_iommu *obj) 29 28 { 30 29 return !obj->domain; 30 + } 31 + 32 + #define pr_reg(name) \ 33 + do { \ 34 + ssize_t bytes; \ 35 + const char *str = "%20s: %08x\n"; \ 36 + const int maxcol = 32; \ 37 + bytes = snprintf(p, maxcol, str, __stringify(name), \ 38 + iommu_read_reg(obj, MMU_##name)); \ 39 + p += bytes; \ 40 + len -= bytes; \ 41 + if (len < maxcol) \ 42 + goto out; \ 43 + } while (0) 44 + 45 + static ssize_t 46 + omap2_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len) 47 + { 48 + char *p = buf; 49 + 50 + pr_reg(REVISION); 51 + pr_reg(IRQSTATUS); 52 + pr_reg(IRQENABLE); 53 + pr_reg(WALKING_ST); 54 + pr_reg(CNTL); 55 + pr_reg(FAULT_AD); 56 + pr_reg(TTB); 57 + pr_reg(LOCK); 58 + pr_reg(LD_TLB); 59 + pr_reg(CAM); 60 + pr_reg(RAM); 61 + pr_reg(GFLUSH); 62 + pr_reg(FLUSH_ENTRY); 63 + pr_reg(READ_CAM); 64 + pr_reg(READ_RAM); 65 + pr_reg(EMU_FAULT_AD); 66 + out: 67 + return p - buf; 68 + } 69 + 70 + static ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, 71 + ssize_t bytes) 72 + { 73 + if (!obj || !buf) 74 + return -EINVAL; 75 + 76 + pm_runtime_get_sync(obj->dev); 77 + 78 + bytes = omap2_iommu_dump_ctx(obj, buf, bytes); 79 + 80 + pm_runtime_put_sync(obj->dev); 81 + 82 + return bytes; 31 83 } 32 84 33 85 static ssize_t debug_read_regs(struct file *file, char __user *userbuf, ··· 109 55 return bytes; 110 56 } 111 57 112 - static ssize_t debug_read_tlb(struct file *file, char __user *userbuf, 113 - size_t count, loff_t *ppos) 58 + static int 59 + __dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num) 114 60 { 115 - struct omap_iommu *obj = file->private_data; 116 - char *p, *buf; 117 - ssize_t bytes, rest; 61 + int i; 62 + struct iotlb_lock saved; 63 + struct cr_regs tmp; 64 + struct cr_regs *p = crs; 65 + 66 + pm_runtime_get_sync(obj->dev); 67 + iotlb_lock_get(obj, &saved); 68 + 69 + for_each_iotlb_cr(obj, num, i, tmp) { 70 + if (!iotlb_cr_valid(&tmp)) 71 + continue; 72 + *p++ = tmp; 73 + } 74 + 75 + iotlb_lock_set(obj, &saved); 76 + pm_runtime_put_sync(obj->dev); 77 + 78 + return p - crs; 79 + } 80 + 81 + static ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr, 82 + struct seq_file *s) 83 + { 84 + return seq_printf(s, "%08x %08x %01x\n", cr->cam, cr->ram, 85 + (cr->cam & MMU_CAM_P) ? 1 : 0); 86 + } 87 + 88 + static size_t omap_dump_tlb_entries(struct omap_iommu *obj, struct seq_file *s) 89 + { 90 + int i, num; 91 + struct cr_regs *cr; 92 + 93 + num = obj->nr_tlb_entries; 94 + 95 + cr = kcalloc(num, sizeof(*cr), GFP_KERNEL); 96 + if (!cr) 97 + return 0; 98 + 99 + num = __dump_tlb_entries(obj, cr, num); 100 + for (i = 0; i < num; i++) 101 + iotlb_dump_cr(obj, cr + i, s); 102 + kfree(cr); 103 + 104 + return 0; 105 + } 106 + 107 + static int debug_read_tlb(struct seq_file *s, void *data) 108 + { 109 + struct omap_iommu *obj = s->private; 118 110 119 111 if (is_omap_iommu_detached(obj)) 120 112 return -EPERM; 121 113 122 - buf = kmalloc(count, GFP_KERNEL); 123 - if (!buf) 124 - return -ENOMEM; 125 - p = buf; 126 - 127 114 mutex_lock(&iommu_debug_lock); 128 115 129 - p += sprintf(p, "%8s %8s\n", "cam:", "ram:"); 130 - p += sprintf(p, "-----------------------------------------\n"); 131 - rest = count - (p - buf); 132 - p += omap_dump_tlb_entries(obj, p, rest); 133 - 134 - bytes = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf); 116 + seq_printf(s, "%8s %8s\n", "cam:", "ram:"); 117 + seq_puts(s, "-----------------------------------------\n"); 118 + omap_dump_tlb_entries(obj, s); 135 119 136 120 mutex_unlock(&iommu_debug_lock); 137 - kfree(buf); 138 121 139 - return bytes; 122 + return 0; 140 123 } 141 124 142 125 static void dump_ioptable(struct seq_file *s) ··· 245 154 .open = simple_open, \ 246 155 .read = debug_read_##name, \ 247 156 .llseek = generic_file_llseek, \ 248 - }; 157 + } 249 158 250 159 DEBUG_FOPS_RO(regs); 251 - DEBUG_FOPS_RO(tlb); 160 + DEBUG_SEQ_FOPS_RO(tlb); 252 161 DEBUG_SEQ_FOPS_RO(pagetable); 253 162 254 163 #define __DEBUG_ADD_FILE(attr, mode) \
+17 -181
drivers/iommu/omap-iommu.c
··· 12 12 */ 13 13 14 14 #include <linux/err.h> 15 - #include <linux/module.h> 16 15 #include <linux/slab.h> 17 16 #include <linux/interrupt.h> 18 17 #include <linux/ioport.h> ··· 36 37 37 38 #define to_iommu(dev) \ 38 39 ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev))) 39 - 40 - #define for_each_iotlb_cr(obj, n, __i, cr) \ 41 - for (__i = 0; \ 42 - (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \ 43 - __i++) 44 40 45 41 /* bitmap of the page sizes currently supported */ 46 42 #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M) ··· 65 71 #define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT) 66 72 #define MMU_LOCK_VICT(x) \ 67 73 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT) 68 - 69 - struct iotlb_lock { 70 - short base; 71 - short vict; 72 - }; 73 74 74 75 static struct platform_driver omap_iommu_driver; 75 76 static struct kmem_cache *iopte_cachep; ··· 202 213 /* 203 214 * TLB operations 204 215 */ 205 - static inline int iotlb_cr_valid(struct cr_regs *cr) 206 - { 207 - if (!cr) 208 - return -EINVAL; 209 - 210 - return cr->cam & MMU_CAM_V; 211 - } 212 - 213 216 static u32 iotlb_cr_to_virt(struct cr_regs *cr) 214 217 { 215 218 u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK; ··· 241 260 return status; 242 261 } 243 262 244 - static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l) 263 + void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l) 245 264 { 246 265 u32 val; 247 266 ··· 249 268 250 269 l->base = MMU_LOCK_BASE(val); 251 270 l->vict = MMU_LOCK_VICT(val); 252 - 253 271 } 254 272 255 - static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l) 273 + void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l) 256 274 { 257 275 u32 val; 258 276 ··· 277 297 } 278 298 279 299 /* only used in iotlb iteration for-loop */ 280 - static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n) 300 + struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n) 281 301 { 282 302 struct cr_regs cr; 283 303 struct iotlb_lock l; ··· 447 467 448 468 pm_runtime_put_sync(obj->dev); 449 469 } 450 - 451 - #ifdef CONFIG_OMAP_IOMMU_DEBUG 452 - 453 - #define pr_reg(name) \ 454 - do { \ 455 - ssize_t bytes; \ 456 - const char *str = "%20s: %08x\n"; \ 457 - const int maxcol = 32; \ 458 - bytes = snprintf(p, maxcol, str, __stringify(name), \ 459 - iommu_read_reg(obj, MMU_##name)); \ 460 - p += bytes; \ 461 - len -= bytes; \ 462 - if (len < maxcol) \ 463 - goto out; \ 464 - } while (0) 465 - 466 - static ssize_t 467 - omap2_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len) 468 - { 469 - char *p = buf; 470 - 471 - pr_reg(REVISION); 472 - pr_reg(IRQSTATUS); 473 - pr_reg(IRQENABLE); 474 - pr_reg(WALKING_ST); 475 - pr_reg(CNTL); 476 - pr_reg(FAULT_AD); 477 - pr_reg(TTB); 478 - pr_reg(LOCK); 479 - pr_reg(LD_TLB); 480 - pr_reg(CAM); 481 - pr_reg(RAM); 482 - pr_reg(GFLUSH); 483 - pr_reg(FLUSH_ENTRY); 484 - pr_reg(READ_CAM); 485 - pr_reg(READ_RAM); 486 - pr_reg(EMU_FAULT_AD); 487 - out: 488 - return p - buf; 489 - } 490 - 491 - ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes) 492 - { 493 - if (!obj || !buf) 494 - return -EINVAL; 495 - 496 - pm_runtime_get_sync(obj->dev); 497 - 498 - bytes = omap2_iommu_dump_ctx(obj, buf, bytes); 499 - 500 - pm_runtime_put_sync(obj->dev); 501 - 502 - return bytes; 503 - } 504 - 505 - static int 506 - __dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num) 507 - { 508 - int i; 509 - struct iotlb_lock saved; 510 - struct cr_regs tmp; 511 - struct cr_regs *p = crs; 512 - 513 - pm_runtime_get_sync(obj->dev); 514 - iotlb_lock_get(obj, &saved); 515 - 516 - for_each_iotlb_cr(obj, num, i, tmp) { 517 - if (!iotlb_cr_valid(&tmp)) 518 - continue; 519 - *p++ = tmp; 520 - } 521 - 522 - iotlb_lock_set(obj, &saved); 523 - pm_runtime_put_sync(obj->dev); 524 - 525 - return p - crs; 526 - } 527 - 528 - /** 529 - * iotlb_dump_cr - Dump an iommu tlb entry into buf 530 - * @obj: target iommu 531 - * @cr: contents of cam and ram register 532 - * @buf: output buffer 533 - **/ 534 - static ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr, 535 - char *buf) 536 - { 537 - char *p = buf; 538 - 539 - /* FIXME: Need more detail analysis of cam/ram */ 540 - p += sprintf(p, "%08x %08x %01x\n", cr->cam, cr->ram, 541 - (cr->cam & MMU_CAM_P) ? 1 : 0); 542 - 543 - return p - buf; 544 - } 545 - 546 - /** 547 - * omap_dump_tlb_entries - dump cr arrays to given buffer 548 - * @obj: target iommu 549 - * @buf: output buffer 550 - **/ 551 - size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes) 552 - { 553 - int i, num; 554 - struct cr_regs *cr; 555 - char *p = buf; 556 - 557 - num = bytes / sizeof(*cr); 558 - num = min(obj->nr_tlb_entries, num); 559 - 560 - cr = kcalloc(num, sizeof(*cr), GFP_KERNEL); 561 - if (!cr) 562 - return 0; 563 - 564 - num = __dump_tlb_entries(obj, cr, num); 565 - for (i = 0; i < num; i++) 566 - p += iotlb_dump_cr(obj, cr + i, p); 567 - kfree(cr); 568 - 569 - return p - buf; 570 - } 571 - 572 - #endif /* CONFIG_OMAP_IOMMU_DEBUG */ 573 470 574 471 /* 575 472 * H/W pagetable operations ··· 787 930 788 931 if (!iopgd_is_table(*iopgd)) { 789 932 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n", 790 - obj->name, errs, da, iopgd, *iopgd); 933 + obj->name, errs, da, iopgd, *iopgd); 791 934 return IRQ_NONE; 792 935 } 793 936 794 937 iopte = iopte_offset(iopgd, da); 795 938 796 939 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n", 797 - obj->name, errs, da, iopgd, *iopgd, iopte, *iopte); 940 + obj->name, errs, da, iopgd, *iopgd, iopte, *iopte); 798 941 799 942 return IRQ_NONE; 800 943 } ··· 820 963 struct device *dev; 821 964 struct omap_iommu *obj; 822 965 823 - dev = driver_find_device(&omap_iommu_driver.driver, NULL, 824 - (void *)name, 825 - device_match_by_alias); 966 + dev = driver_find_device(&omap_iommu_driver.driver, NULL, (void *)name, 967 + device_match_by_alias); 826 968 if (!dev) 827 969 return ERR_PTR(-ENODEV); 828 970 ··· 945 1089 { .compatible = "ti,dra7-iommu" }, 946 1090 {}, 947 1091 }; 948 - MODULE_DEVICE_TABLE(of, omap_iommu_of_match); 949 1092 950 1093 static struct platform_driver omap_iommu_driver = { 951 1094 .probe = omap_iommu_probe, ··· 976 1121 } 977 1122 978 1123 static int omap_iommu_map(struct iommu_domain *domain, unsigned long da, 979 - phys_addr_t pa, size_t bytes, int prot) 1124 + phys_addr_t pa, size_t bytes, int prot) 980 1125 { 981 1126 struct omap_iommu_domain *omap_domain = to_omap_domain(domain); 982 1127 struct omap_iommu *oiommu = omap_domain->iommu_dev; ··· 1003 1148 } 1004 1149 1005 1150 static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da, 1006 - size_t size) 1151 + size_t size) 1007 1152 { 1008 1153 struct omap_iommu_domain *omap_domain = to_omap_domain(domain); 1009 1154 struct omap_iommu *oiommu = omap_domain->iommu_dev; ··· 1054 1199 } 1055 1200 1056 1201 static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain, 1057 - struct device *dev) 1202 + struct device *dev) 1058 1203 { 1059 1204 struct omap_iommu *oiommu = dev_to_omap_iommu(dev); 1060 1205 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu; ··· 1075 1220 } 1076 1221 1077 1222 static void omap_iommu_detach_dev(struct iommu_domain *domain, 1078 - struct device *dev) 1223 + struct device *dev) 1079 1224 { 1080 1225 struct omap_iommu_domain *omap_domain = to_omap_domain(domain); 1081 1226 ··· 1092 1237 return NULL; 1093 1238 1094 1239 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL); 1095 - if (!omap_domain) { 1096 - pr_err("kzalloc failed\n"); 1240 + if (!omap_domain) 1097 1241 goto out; 1098 - } 1099 1242 1100 1243 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL); 1101 - if (!omap_domain->pgtable) { 1102 - pr_err("kzalloc failed\n"); 1244 + if (!omap_domain->pgtable) 1103 1245 goto fail_nomem; 1104 - } 1105 1246 1106 1247 /* 1107 1248 * should never fail, but please keep this around to ensure ··· 1136 1285 } 1137 1286 1138 1287 static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain, 1139 - dma_addr_t da) 1288 + dma_addr_t da) 1140 1289 { 1141 1290 struct omap_iommu_domain *omap_domain = to_omap_domain(domain); 1142 1291 struct omap_iommu *oiommu = omap_domain->iommu_dev; ··· 1153 1302 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK); 1154 1303 else 1155 1304 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte, 1156 - (unsigned long long)da); 1305 + (unsigned long long)da); 1157 1306 } else { 1158 1307 if (iopgd_is_section(*pgd)) 1159 1308 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK); ··· 1161 1310 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK); 1162 1311 else 1163 1312 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd, 1164 - (unsigned long long)da); 1313 + (unsigned long long)da); 1165 1314 } 1166 1315 1167 1316 return ret; ··· 1256 1405 1257 1406 return platform_driver_register(&omap_iommu_driver); 1258 1407 } 1259 - /* must be ready before omap3isp is probed */ 1260 1408 subsys_initcall(omap_iommu_init); 1261 - 1262 - static void __exit omap_iommu_exit(void) 1263 - { 1264 - kmem_cache_destroy(iopte_cachep); 1265 - 1266 - platform_driver_unregister(&omap_iommu_driver); 1267 - 1268 - omap_iommu_debugfs_exit(); 1269 - } 1270 - module_exit(omap_iommu_exit); 1271 - 1272 - MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives"); 1273 - MODULE_ALIAS("platform:omap-iommu"); 1274 - MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi"); 1275 - MODULE_LICENSE("GPL v2"); 1409 + /* must be ready before omap3isp is probed */
+41 -38
drivers/iommu/omap-iommu.h
··· 13 13 #ifndef _OMAP_IOMMU_H 14 14 #define _OMAP_IOMMU_H 15 15 16 + #include <linux/bitops.h> 17 + 18 + #define for_each_iotlb_cr(obj, n, __i, cr) \ 19 + for (__i = 0; \ 20 + (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \ 21 + __i++) 22 + 16 23 struct iotlb_entry { 17 24 u32 da; 18 25 u32 pa; 19 26 u32 pgsz, prsvd, valid; 20 - union { 21 - u16 ap; 22 - struct { 23 - u32 endian, elsz, mixed; 24 - }; 25 - }; 27 + u32 endian, elsz, mixed; 26 28 }; 27 29 28 30 struct omap_iommu { ··· 51 49 }; 52 50 53 51 struct cr_regs { 54 - union { 55 - struct { 56 - u16 cam_l; 57 - u16 cam_h; 58 - }; 59 - u32 cam; 60 - }; 61 - union { 62 - struct { 63 - u16 ram_l; 64 - u16 ram_h; 65 - }; 66 - u32 ram; 67 - }; 52 + u32 cam; 53 + u32 ram; 54 + }; 55 + 56 + struct iotlb_lock { 57 + short base; 58 + short vict; 68 59 }; 69 60 70 61 /** ··· 98 103 * MMU Register bit definitions 99 104 */ 100 105 /* IRQSTATUS & IRQENABLE */ 101 - #define MMU_IRQ_MULTIHITFAULT (1 << 4) 102 - #define MMU_IRQ_TABLEWALKFAULT (1 << 3) 103 - #define MMU_IRQ_EMUMISS (1 << 2) 104 - #define MMU_IRQ_TRANSLATIONFAULT (1 << 1) 105 - #define MMU_IRQ_TLBMISS (1 << 0) 106 + #define MMU_IRQ_MULTIHITFAULT BIT(4) 107 + #define MMU_IRQ_TABLEWALKFAULT BIT(3) 108 + #define MMU_IRQ_EMUMISS BIT(2) 109 + #define MMU_IRQ_TRANSLATIONFAULT BIT(1) 110 + #define MMU_IRQ_TLBMISS BIT(0) 106 111 107 112 #define __MMU_IRQ_FAULT \ 108 113 (MMU_IRQ_MULTIHITFAULT | MMU_IRQ_EMUMISS | MMU_IRQ_TRANSLATIONFAULT) ··· 114 119 /* MMU_CNTL */ 115 120 #define MMU_CNTL_SHIFT 1 116 121 #define MMU_CNTL_MASK (7 << MMU_CNTL_SHIFT) 117 - #define MMU_CNTL_EML_TLB (1 << 3) 118 - #define MMU_CNTL_TWL_EN (1 << 2) 119 - #define MMU_CNTL_MMU_EN (1 << 1) 122 + #define MMU_CNTL_EML_TLB BIT(3) 123 + #define MMU_CNTL_TWL_EN BIT(2) 124 + #define MMU_CNTL_MMU_EN BIT(1) 120 125 121 126 /* CAM */ 122 127 #define MMU_CAM_VATAG_SHIFT 12 123 128 #define MMU_CAM_VATAG_MASK \ 124 129 ((~0UL >> MMU_CAM_VATAG_SHIFT) << MMU_CAM_VATAG_SHIFT) 125 - #define MMU_CAM_P (1 << 3) 126 - #define MMU_CAM_V (1 << 2) 130 + #define MMU_CAM_P BIT(3) 131 + #define MMU_CAM_V BIT(2) 127 132 #define MMU_CAM_PGSZ_MASK 3 128 133 #define MMU_CAM_PGSZ_1M (0 << 0) 129 134 #define MMU_CAM_PGSZ_64K (1 << 0) ··· 136 141 ((~0UL >> MMU_RAM_PADDR_SHIFT) << MMU_RAM_PADDR_SHIFT) 137 142 138 143 #define MMU_RAM_ENDIAN_SHIFT 9 139 - #define MMU_RAM_ENDIAN_MASK (1 << MMU_RAM_ENDIAN_SHIFT) 144 + #define MMU_RAM_ENDIAN_MASK BIT(MMU_RAM_ENDIAN_SHIFT) 140 145 #define MMU_RAM_ENDIAN_LITTLE (0 << MMU_RAM_ENDIAN_SHIFT) 141 - #define MMU_RAM_ENDIAN_BIG (1 << MMU_RAM_ENDIAN_SHIFT) 146 + #define MMU_RAM_ENDIAN_BIG BIT(MMU_RAM_ENDIAN_SHIFT) 142 147 143 148 #define MMU_RAM_ELSZ_SHIFT 7 144 149 #define MMU_RAM_ELSZ_MASK (3 << MMU_RAM_ELSZ_SHIFT) ··· 147 152 #define MMU_RAM_ELSZ_32 (2 << MMU_RAM_ELSZ_SHIFT) 148 153 #define MMU_RAM_ELSZ_NONE (3 << MMU_RAM_ELSZ_SHIFT) 149 154 #define MMU_RAM_MIXED_SHIFT 6 150 - #define MMU_RAM_MIXED_MASK (1 << MMU_RAM_MIXED_SHIFT) 155 + #define MMU_RAM_MIXED_MASK BIT(MMU_RAM_MIXED_SHIFT) 151 156 #define MMU_RAM_MIXED MMU_RAM_MIXED_MASK 152 157 153 158 #define MMU_GP_REG_BUS_ERR_BACK_EN 0x1 ··· 185 190 /* 186 191 * global functions 187 192 */ 188 - #ifdef CONFIG_OMAP_IOMMU_DEBUG 189 - extern ssize_t 190 - omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len); 191 - extern size_t 192 - omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t len); 193 193 194 + struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n); 195 + void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l); 196 + void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l); 197 + 198 + #ifdef CONFIG_OMAP_IOMMU_DEBUG 194 199 void omap_iommu_debugfs_init(void); 195 200 void omap_iommu_debugfs_exit(void); 196 201 ··· 215 220 static inline void iommu_write_reg(struct omap_iommu *obj, u32 val, size_t offs) 216 221 { 217 222 __raw_writel(val, obj->regbase + offs); 223 + } 224 + 225 + static inline int iotlb_cr_valid(struct cr_regs *cr) 226 + { 227 + if (!cr) 228 + return -EINVAL; 229 + 230 + return cr->cam & MMU_CAM_V; 218 231 } 219 232 220 233 #endif /* _OMAP_IOMMU_H */
+17 -10
drivers/iommu/omap-iopgtable.h
··· 10 10 * published by the Free Software Foundation. 11 11 */ 12 12 13 + #ifndef _OMAP_IOPGTABLE_H 14 + #define _OMAP_IOPGTABLE_H 15 + 16 + #include <linux/bitops.h> 17 + 13 18 /* 14 19 * "L2 table" address mask and size definitions. 15 20 */ 16 21 #define IOPGD_SHIFT 20 17 - #define IOPGD_SIZE (1UL << IOPGD_SHIFT) 22 + #define IOPGD_SIZE BIT(IOPGD_SHIFT) 18 23 #define IOPGD_MASK (~(IOPGD_SIZE - 1)) 19 24 20 25 /* 21 26 * "section" address mask and size definitions. 22 27 */ 23 28 #define IOSECTION_SHIFT 20 24 - #define IOSECTION_SIZE (1UL << IOSECTION_SHIFT) 29 + #define IOSECTION_SIZE BIT(IOSECTION_SHIFT) 25 30 #define IOSECTION_MASK (~(IOSECTION_SIZE - 1)) 26 31 27 32 /* 28 33 * "supersection" address mask and size definitions. 29 34 */ 30 35 #define IOSUPER_SHIFT 24 31 - #define IOSUPER_SIZE (1UL << IOSUPER_SHIFT) 36 + #define IOSUPER_SIZE BIT(IOSUPER_SHIFT) 32 37 #define IOSUPER_MASK (~(IOSUPER_SIZE - 1)) 33 38 34 39 #define PTRS_PER_IOPGD (1UL << (32 - IOPGD_SHIFT)) ··· 43 38 * "small page" address mask and size definitions. 44 39 */ 45 40 #define IOPTE_SHIFT 12 46 - #define IOPTE_SIZE (1UL << IOPTE_SHIFT) 41 + #define IOPTE_SIZE BIT(IOPTE_SHIFT) 47 42 #define IOPTE_MASK (~(IOPTE_SIZE - 1)) 48 43 49 44 /* 50 45 * "large page" address mask and size definitions. 51 46 */ 52 47 #define IOLARGE_SHIFT 16 53 - #define IOLARGE_SIZE (1UL << IOLARGE_SHIFT) 48 + #define IOLARGE_SIZE BIT(IOLARGE_SHIFT) 54 49 #define IOLARGE_MASK (~(IOLARGE_SIZE - 1)) 55 50 56 51 #define PTRS_PER_IOPTE (1UL << (IOPGD_SHIFT - IOPTE_SHIFT)) ··· 74 69 /* 75 70 * some descriptor attributes. 76 71 */ 77 - #define IOPGD_TABLE (1 << 0) 78 - #define IOPGD_SECTION (2 << 0) 79 - #define IOPGD_SUPER (1 << 18 | 2 << 0) 72 + #define IOPGD_TABLE (1) 73 + #define IOPGD_SECTION (2) 74 + #define IOPGD_SUPER (BIT(18) | IOPGD_SECTION) 80 75 81 76 #define iopgd_is_table(x) (((x) & 3) == IOPGD_TABLE) 82 77 #define iopgd_is_section(x) (((x) & (1 << 18 | 3)) == IOPGD_SECTION) 83 78 #define iopgd_is_super(x) (((x) & (1 << 18 | 3)) == IOPGD_SUPER) 84 79 85 - #define IOPTE_SMALL (2 << 0) 86 - #define IOPTE_LARGE (1 << 0) 80 + #define IOPTE_SMALL (2) 81 + #define IOPTE_LARGE (1) 87 82 88 83 #define iopte_is_small(x) (((x) & 2) == IOPTE_SMALL) 89 84 #define iopte_is_large(x) (((x) & 3) == IOPTE_LARGE) ··· 98 93 /* to find an entry in the second-level page table. */ 99 94 #define iopte_index(da) (((da) >> IOPTE_SHIFT) & (PTRS_PER_IOPTE - 1)) 100 95 #define iopte_offset(iopgd, da) (iopgd_page_vaddr(iopgd) + iopte_index(da)) 96 + 97 + #endif /* _OMAP_IOPGTABLE_H */
+200 -110
drivers/iommu/tegra-smmu.c
··· 27 27 const struct tegra_smmu_soc *soc; 28 28 29 29 unsigned long pfn_mask; 30 + unsigned long tlb_mask; 30 31 31 32 unsigned long *asids; 32 33 struct mutex lock; ··· 41 40 struct iommu_domain domain; 42 41 struct tegra_smmu *smmu; 43 42 unsigned int use_count; 44 - struct page *count; 43 + u32 *count; 44 + struct page **pts; 45 45 struct page *pd; 46 + dma_addr_t pd_dma; 46 47 unsigned id; 47 48 u32 attr; 48 49 }; ··· 71 68 #define SMMU_TLB_CONFIG 0x14 72 69 #define SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29) 73 70 #define SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28) 74 - #define SMMU_TLB_CONFIG_ACTIVE_LINES(x) ((x) & 0x3f) 71 + #define SMMU_TLB_CONFIG_ACTIVE_LINES(smmu) \ 72 + ((smmu)->soc->num_tlb_lines & (smmu)->tlb_mask) 75 73 76 74 #define SMMU_PTC_CONFIG 0x18 77 75 #define SMMU_PTC_CONFIG_ENABLE (1 << 29) ··· 83 79 #define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f) 84 80 85 81 #define SMMU_PTB_DATA 0x020 86 - #define SMMU_PTB_DATA_VALUE(page, attr) (page_to_phys(page) >> 12 | (attr)) 82 + #define SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr)) 87 83 88 - #define SMMU_MK_PDE(page, attr) (page_to_phys(page) >> SMMU_PTE_SHIFT | (attr)) 84 + #define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr)) 89 85 90 86 #define SMMU_TLB_FLUSH 0x030 91 87 #define SMMU_TLB_FLUSH_VA_MATCH_ALL (0 << 0) ··· 138 134 #define SMMU_PTE_ATTR (SMMU_PTE_READABLE | SMMU_PTE_WRITABLE | \ 139 135 SMMU_PTE_NONSECURE) 140 136 141 - static inline void smmu_flush_ptc(struct tegra_smmu *smmu, struct page *page, 137 + static unsigned int iova_pd_index(unsigned long iova) 138 + { 139 + return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1); 140 + } 141 + 142 + static unsigned int iova_pt_index(unsigned long iova) 143 + { 144 + return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1); 145 + } 146 + 147 + static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr) 148 + { 149 + addr >>= 12; 150 + return (addr & smmu->pfn_mask) == addr; 151 + } 152 + 153 + static dma_addr_t smmu_pde_to_dma(u32 pde) 154 + { 155 + return pde << 12; 156 + } 157 + 158 + static void smmu_flush_ptc_all(struct tegra_smmu *smmu) 159 + { 160 + smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH); 161 + } 162 + 163 + static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma, 142 164 unsigned long offset) 143 165 { 144 - phys_addr_t phys = page ? page_to_phys(page) : 0; 145 166 u32 value; 146 167 147 - if (page) { 148 - offset &= ~(smmu->mc->soc->atom_size - 1); 168 + offset &= ~(smmu->mc->soc->atom_size - 1); 149 169 150 - if (smmu->mc->soc->num_address_bits > 32) { 151 - #ifdef CONFIG_PHYS_ADDR_T_64BIT 152 - value = (phys >> 32) & SMMU_PTC_FLUSH_HI_MASK; 170 + if (smmu->mc->soc->num_address_bits > 32) { 171 + #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT 172 + value = (dma >> 32) & SMMU_PTC_FLUSH_HI_MASK; 153 173 #else 154 - value = 0; 174 + value = 0; 155 175 #endif 156 - smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI); 157 - } 158 - 159 - value = (phys + offset) | SMMU_PTC_FLUSH_TYPE_ADR; 160 - } else { 161 - value = SMMU_PTC_FLUSH_TYPE_ALL; 176 + smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI); 162 177 } 163 178 179 + value = (dma + offset) | SMMU_PTC_FLUSH_TYPE_ADR; 164 180 smmu_writel(smmu, value, SMMU_PTC_FLUSH); 165 181 } 166 182 ··· 260 236 static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type) 261 237 { 262 238 struct tegra_smmu_as *as; 263 - unsigned int i; 264 - uint32_t *pd; 265 239 266 240 if (type != IOMMU_DOMAIN_UNMANAGED) 267 241 return NULL; ··· 270 248 271 249 as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE; 272 250 273 - as->pd = alloc_page(GFP_KERNEL | __GFP_DMA); 251 + as->pd = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO); 274 252 if (!as->pd) { 275 253 kfree(as); 276 254 return NULL; 277 255 } 278 256 279 - as->count = alloc_page(GFP_KERNEL); 257 + as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL); 280 258 if (!as->count) { 281 259 __free_page(as->pd); 282 260 kfree(as); 283 261 return NULL; 284 262 } 285 263 286 - /* clear PDEs */ 287 - pd = page_address(as->pd); 288 - SetPageReserved(as->pd); 289 - 290 - for (i = 0; i < SMMU_NUM_PDE; i++) 291 - pd[i] = 0; 292 - 293 - /* clear PDE usage counters */ 294 - pd = page_address(as->count); 295 - SetPageReserved(as->count); 296 - 297 - for (i = 0; i < SMMU_NUM_PDE; i++) 298 - pd[i] = 0; 264 + as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL); 265 + if (!as->pts) { 266 + kfree(as->count); 267 + __free_page(as->pd); 268 + kfree(as); 269 + return NULL; 270 + } 299 271 300 272 /* setup aperture */ 301 273 as->domain.geometry.aperture_start = 0; ··· 304 288 struct tegra_smmu_as *as = to_smmu_as(domain); 305 289 306 290 /* TODO: free page directory and page tables */ 307 - ClearPageReserved(as->pd); 308 291 309 292 kfree(as); 310 293 } ··· 391 376 return 0; 392 377 } 393 378 379 + as->pd_dma = dma_map_page(smmu->dev, as->pd, 0, SMMU_SIZE_PD, 380 + DMA_TO_DEVICE); 381 + if (dma_mapping_error(smmu->dev, as->pd_dma)) 382 + return -ENOMEM; 383 + 384 + /* We can't handle 64-bit DMA addresses */ 385 + if (!smmu_dma_addr_valid(smmu, as->pd_dma)) { 386 + err = -ENOMEM; 387 + goto err_unmap; 388 + } 389 + 394 390 err = tegra_smmu_alloc_asid(smmu, &as->id); 395 391 if (err < 0) 396 - return err; 392 + goto err_unmap; 397 393 398 - smmu->soc->ops->flush_dcache(as->pd, 0, SMMU_SIZE_PD); 399 - smmu_flush_ptc(smmu, as->pd, 0); 394 + smmu_flush_ptc(smmu, as->pd_dma, 0); 400 395 smmu_flush_tlb_asid(smmu, as->id); 401 396 402 397 smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID); 403 - value = SMMU_PTB_DATA_VALUE(as->pd, as->attr); 398 + value = SMMU_PTB_DATA_VALUE(as->pd_dma, as->attr); 404 399 smmu_writel(smmu, value, SMMU_PTB_DATA); 405 400 smmu_flush(smmu); 406 401 ··· 418 393 as->use_count++; 419 394 420 395 return 0; 396 + 397 + err_unmap: 398 + dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE); 399 + return err; 421 400 } 422 401 423 402 static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu, ··· 431 402 return; 432 403 433 404 tegra_smmu_free_asid(smmu, as->id); 405 + 406 + dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE); 407 + 434 408 as->smmu = NULL; 435 409 } 436 410 ··· 497 465 } 498 466 } 499 467 500 - static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova, 501 - struct page **pagep) 468 + static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova, 469 + u32 value) 502 470 { 503 - u32 *pd = page_address(as->pd), *pt, *count; 504 - u32 pde = (iova >> SMMU_PDE_SHIFT) & 0x3ff; 505 - u32 pte = (iova >> SMMU_PTE_SHIFT) & 0x3ff; 471 + unsigned int pd_index = iova_pd_index(iova); 506 472 struct tegra_smmu *smmu = as->smmu; 507 - struct page *page; 508 - unsigned int i; 473 + u32 *pd = page_address(as->pd); 474 + unsigned long offset = pd_index * sizeof(*pd); 509 475 510 - if (pd[pde] == 0) { 511 - page = alloc_page(GFP_KERNEL | __GFP_DMA); 476 + /* Set the page directory entry first */ 477 + pd[pd_index] = value; 478 + 479 + /* The flush the page directory entry from caches */ 480 + dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset, 481 + sizeof(*pd), DMA_TO_DEVICE); 482 + 483 + /* And flush the iommu */ 484 + smmu_flush_ptc(smmu, as->pd_dma, offset); 485 + smmu_flush_tlb_section(smmu, as->id, iova); 486 + smmu_flush(smmu); 487 + } 488 + 489 + static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova) 490 + { 491 + u32 *pt = page_address(pt_page); 492 + 493 + return pt + iova_pt_index(iova); 494 + } 495 + 496 + static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova, 497 + dma_addr_t *dmap) 498 + { 499 + unsigned int pd_index = iova_pd_index(iova); 500 + struct page *pt_page; 501 + u32 *pd; 502 + 503 + pt_page = as->pts[pd_index]; 504 + if (!pt_page) 505 + return NULL; 506 + 507 + pd = page_address(as->pd); 508 + *dmap = smmu_pde_to_dma(pd[pd_index]); 509 + 510 + return tegra_smmu_pte_offset(pt_page, iova); 511 + } 512 + 513 + static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova, 514 + dma_addr_t *dmap) 515 + { 516 + unsigned int pde = iova_pd_index(iova); 517 + struct tegra_smmu *smmu = as->smmu; 518 + 519 + if (!as->pts[pde]) { 520 + struct page *page; 521 + dma_addr_t dma; 522 + 523 + page = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO); 512 524 if (!page) 513 525 return NULL; 514 526 515 - pt = page_address(page); 516 - SetPageReserved(page); 527 + dma = dma_map_page(smmu->dev, page, 0, SMMU_SIZE_PT, 528 + DMA_TO_DEVICE); 529 + if (dma_mapping_error(smmu->dev, dma)) { 530 + __free_page(page); 531 + return NULL; 532 + } 517 533 518 - for (i = 0; i < SMMU_NUM_PTE; i++) 519 - pt[i] = 0; 534 + if (!smmu_dma_addr_valid(smmu, dma)) { 535 + dma_unmap_page(smmu->dev, dma, SMMU_SIZE_PT, 536 + DMA_TO_DEVICE); 537 + __free_page(page); 538 + return NULL; 539 + } 520 540 521 - smmu->soc->ops->flush_dcache(page, 0, SMMU_SIZE_PT); 541 + as->pts[pde] = page; 522 542 523 - pd[pde] = SMMU_MK_PDE(page, SMMU_PDE_ATTR | SMMU_PDE_NEXT); 543 + tegra_smmu_set_pde(as, iova, SMMU_MK_PDE(dma, SMMU_PDE_ATTR | 544 + SMMU_PDE_NEXT)); 524 545 525 - smmu->soc->ops->flush_dcache(as->pd, pde << 2, 4); 526 - smmu_flush_ptc(smmu, as->pd, pde << 2); 527 - smmu_flush_tlb_section(smmu, as->id, iova); 528 - smmu_flush(smmu); 546 + *dmap = dma; 529 547 } else { 530 - page = pfn_to_page(pd[pde] & smmu->pfn_mask); 531 - pt = page_address(page); 548 + u32 *pd = page_address(as->pd); 549 + 550 + *dmap = smmu_pde_to_dma(pd[pde]); 532 551 } 533 552 534 - *pagep = page; 535 - 536 - /* Keep track of entries in this page table. */ 537 - count = page_address(as->count); 538 - if (pt[pte] == 0) 539 - count[pde]++; 540 - 541 - return &pt[pte]; 553 + return tegra_smmu_pte_offset(as->pts[pde], iova); 542 554 } 543 555 544 - static void as_put_pte(struct tegra_smmu_as *as, dma_addr_t iova) 556 + static void tegra_smmu_pte_get_use(struct tegra_smmu_as *as, unsigned long iova) 545 557 { 546 - u32 pde = (iova >> SMMU_PDE_SHIFT) & 0x3ff; 547 - u32 pte = (iova >> SMMU_PTE_SHIFT) & 0x3ff; 548 - u32 *count = page_address(as->count); 549 - u32 *pd = page_address(as->pd), *pt; 550 - struct page *page; 558 + unsigned int pd_index = iova_pd_index(iova); 551 559 552 - page = pfn_to_page(pd[pde] & as->smmu->pfn_mask); 553 - pt = page_address(page); 560 + as->count[pd_index]++; 561 + } 562 + 563 + static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova) 564 + { 565 + unsigned int pde = iova_pd_index(iova); 566 + struct page *page = as->pts[pde]; 554 567 555 568 /* 556 569 * When no entries in this page table are used anymore, return the 557 570 * memory page to the system. 558 571 */ 559 - if (pt[pte] != 0) { 560 - if (--count[pde] == 0) { 561 - ClearPageReserved(page); 562 - __free_page(page); 563 - pd[pde] = 0; 564 - } 572 + if (--as->count[pde] == 0) { 573 + struct tegra_smmu *smmu = as->smmu; 574 + u32 *pd = page_address(as->pd); 575 + dma_addr_t pte_dma = smmu_pde_to_dma(pd[pde]); 565 576 566 - pt[pte] = 0; 577 + tegra_smmu_set_pde(as, iova, 0); 578 + 579 + dma_unmap_page(smmu->dev, pte_dma, SMMU_SIZE_PT, DMA_TO_DEVICE); 580 + __free_page(page); 581 + as->pts[pde] = NULL; 567 582 } 583 + } 584 + 585 + static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova, 586 + u32 *pte, dma_addr_t pte_dma, u32 val) 587 + { 588 + struct tegra_smmu *smmu = as->smmu; 589 + unsigned long offset = offset_in_page(pte); 590 + 591 + *pte = val; 592 + 593 + dma_sync_single_range_for_device(smmu->dev, pte_dma, offset, 594 + 4, DMA_TO_DEVICE); 595 + smmu_flush_ptc(smmu, pte_dma, offset); 596 + smmu_flush_tlb_group(smmu, as->id, iova); 597 + smmu_flush(smmu); 568 598 } 569 599 570 600 static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova, 571 601 phys_addr_t paddr, size_t size, int prot) 572 602 { 573 603 struct tegra_smmu_as *as = to_smmu_as(domain); 574 - struct tegra_smmu *smmu = as->smmu; 575 - unsigned long offset; 576 - struct page *page; 604 + dma_addr_t pte_dma; 577 605 u32 *pte; 578 606 579 - pte = as_get_pte(as, iova, &page); 607 + pte = as_get_pte(as, iova, &pte_dma); 580 608 if (!pte) 581 609 return -ENOMEM; 582 610 583 - *pte = __phys_to_pfn(paddr) | SMMU_PTE_ATTR; 584 - offset = offset_in_page(pte); 611 + /* If we aren't overwriting a pre-existing entry, increment use */ 612 + if (*pte == 0) 613 + tegra_smmu_pte_get_use(as, iova); 585 614 586 - smmu->soc->ops->flush_dcache(page, offset, 4); 587 - smmu_flush_ptc(smmu, page, offset); 588 - smmu_flush_tlb_group(smmu, as->id, iova); 589 - smmu_flush(smmu); 615 + tegra_smmu_set_pte(as, iova, pte, pte_dma, 616 + __phys_to_pfn(paddr) | SMMU_PTE_ATTR); 590 617 591 618 return 0; 592 619 } ··· 654 563 size_t size) 655 564 { 656 565 struct tegra_smmu_as *as = to_smmu_as(domain); 657 - struct tegra_smmu *smmu = as->smmu; 658 - unsigned long offset; 659 - struct page *page; 566 + dma_addr_t pte_dma; 660 567 u32 *pte; 661 568 662 - pte = as_get_pte(as, iova, &page); 663 - if (!pte) 569 + pte = tegra_smmu_pte_lookup(as, iova, &pte_dma); 570 + if (!pte || !*pte) 664 571 return 0; 665 572 666 - offset = offset_in_page(pte); 667 - as_put_pte(as, iova); 668 - 669 - smmu->soc->ops->flush_dcache(page, offset, 4); 670 - smmu_flush_ptc(smmu, page, offset); 671 - smmu_flush_tlb_group(smmu, as->id, iova); 672 - smmu_flush(smmu); 573 + tegra_smmu_set_pte(as, iova, pte, pte_dma, 0); 574 + tegra_smmu_pte_put_use(as, iova); 673 575 674 576 return size; 675 577 } ··· 671 587 dma_addr_t iova) 672 588 { 673 589 struct tegra_smmu_as *as = to_smmu_as(domain); 674 - struct page *page; 675 590 unsigned long pfn; 591 + dma_addr_t pte_dma; 676 592 u32 *pte; 677 593 678 - pte = as_get_pte(as, iova, &page); 594 + pte = tegra_smmu_pte_lookup(as, iova, &pte_dma); 595 + if (!pte || !*pte) 596 + return 0; 597 + 679 598 pfn = *pte & as->smmu->pfn_mask; 680 599 681 600 return PFN_PHYS(pfn); ··· 903 816 smmu->pfn_mask = BIT_MASK(mc->soc->num_address_bits - PAGE_SHIFT) - 1; 904 817 dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n", 905 818 mc->soc->num_address_bits, smmu->pfn_mask); 819 + smmu->tlb_mask = (smmu->soc->num_tlb_lines << 1) - 1; 820 + dev_dbg(dev, "TLB lines: %u, mask: %#lx\n", smmu->soc->num_tlb_lines, 821 + smmu->tlb_mask); 906 822 907 823 value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f); 908 824 ··· 915 825 smmu_writel(smmu, value, SMMU_PTC_CONFIG); 916 826 917 827 value = SMMU_TLB_CONFIG_HIT_UNDER_MISS | 918 - SMMU_TLB_CONFIG_ACTIVE_LINES(0x20); 828 + SMMU_TLB_CONFIG_ACTIVE_LINES(smmu); 919 829 920 830 if (soc->supports_round_robin_arbitration) 921 831 value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION; 922 832 923 833 smmu_writel(smmu, value, SMMU_TLB_CONFIG); 924 834 925 - smmu_flush_ptc(smmu, NULL, 0); 835 + smmu_flush_ptc_all(smmu); 926 836 smmu_flush_tlb(smmu); 927 837 smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG); 928 838 smmu_flush(smmu);
+1 -17
drivers/memory/tegra/tegra114.c
··· 9 9 #include <linux/of.h> 10 10 #include <linux/mm.h> 11 11 12 - #include <asm/cacheflush.h> 13 - 14 12 #include <dt-bindings/memory/tegra114-mc.h> 15 13 16 14 #include "mc.h" ··· 912 914 { .name = "tsec", .swgroup = TEGRA_SWGROUP_TSEC, .reg = 0x294 }, 913 915 }; 914 916 915 - static void tegra114_flush_dcache(struct page *page, unsigned long offset, 916 - size_t size) 917 - { 918 - phys_addr_t phys = page_to_phys(page) + offset; 919 - void *virt = page_address(page) + offset; 920 - 921 - __cpuc_flush_dcache_area(virt, size); 922 - outer_flush_range(phys, phys + size); 923 - } 924 - 925 - static const struct tegra_smmu_ops tegra114_smmu_ops = { 926 - .flush_dcache = tegra114_flush_dcache, 927 - }; 928 - 929 917 static const struct tegra_smmu_soc tegra114_smmu_soc = { 930 918 .clients = tegra114_mc_clients, 931 919 .num_clients = ARRAY_SIZE(tegra114_mc_clients), ··· 919 935 .num_swgroups = ARRAY_SIZE(tegra114_swgroups), 920 936 .supports_round_robin_arbitration = false, 921 937 .supports_request_limit = false, 938 + .num_tlb_lines = 32, 922 939 .num_asids = 4, 923 - .ops = &tegra114_smmu_ops, 924 940 }; 925 941 926 942 const struct tegra_mc_soc tegra114_mc_soc = {
+1 -30
drivers/memory/tegra/tegra124.c
··· 9 9 #include <linux/of.h> 10 10 #include <linux/mm.h> 11 11 12 - #include <asm/cacheflush.h> 13 - 14 12 #include <dt-bindings/memory/tegra124-mc.h> 15 13 16 14 #include "mc.h" ··· 1000 1002 }; 1001 1003 1002 1004 #ifdef CONFIG_ARCH_TEGRA_124_SOC 1003 - static void tegra124_flush_dcache(struct page *page, unsigned long offset, 1004 - size_t size) 1005 - { 1006 - phys_addr_t phys = page_to_phys(page) + offset; 1007 - void *virt = page_address(page) + offset; 1008 - 1009 - __cpuc_flush_dcache_area(virt, size); 1010 - outer_flush_range(phys, phys + size); 1011 - } 1012 - 1013 - static const struct tegra_smmu_ops tegra124_smmu_ops = { 1014 - .flush_dcache = tegra124_flush_dcache, 1015 - }; 1016 - 1017 1005 static const struct tegra_smmu_soc tegra124_smmu_soc = { 1018 1006 .clients = tegra124_mc_clients, 1019 1007 .num_clients = ARRAY_SIZE(tegra124_mc_clients), ··· 1008 1024 .supports_round_robin_arbitration = true, 1009 1025 .supports_request_limit = true, 1010 1026 .num_asids = 128, 1011 - .ops = &tegra124_smmu_ops, 1012 1027 }; 1013 1028 1014 1029 const struct tegra_mc_soc tegra124_mc_soc = { ··· 1023 1040 #endif /* CONFIG_ARCH_TEGRA_124_SOC */ 1024 1041 1025 1042 #ifdef CONFIG_ARCH_TEGRA_132_SOC 1026 - static void tegra132_flush_dcache(struct page *page, unsigned long offset, 1027 - size_t size) 1028 - { 1029 - void *virt = page_address(page) + offset; 1030 - 1031 - __flush_dcache_area(virt, size); 1032 - } 1033 - 1034 - static const struct tegra_smmu_ops tegra132_smmu_ops = { 1035 - .flush_dcache = tegra132_flush_dcache, 1036 - }; 1037 - 1038 1043 static const struct tegra_smmu_soc tegra132_smmu_soc = { 1039 1044 .clients = tegra124_mc_clients, 1040 1045 .num_clients = ARRAY_SIZE(tegra124_mc_clients), ··· 1030 1059 .num_swgroups = ARRAY_SIZE(tegra124_swgroups), 1031 1060 .supports_round_robin_arbitration = true, 1032 1061 .supports_request_limit = true, 1062 + .num_tlb_lines = 32, 1033 1063 .num_asids = 128, 1034 - .ops = &tegra132_smmu_ops, 1035 1064 }; 1036 1065 1037 1066 const struct tegra_mc_soc tegra132_mc_soc = {
+1 -17
drivers/memory/tegra/tegra30.c
··· 9 9 #include <linux/of.h> 10 10 #include <linux/mm.h> 11 11 12 - #include <asm/cacheflush.h> 13 - 14 12 #include <dt-bindings/memory/tegra30-mc.h> 15 13 16 14 #include "mc.h" ··· 934 936 { .name = "isp", .swgroup = TEGRA_SWGROUP_ISP, .reg = 0x258 }, 935 937 }; 936 938 937 - static void tegra30_flush_dcache(struct page *page, unsigned long offset, 938 - size_t size) 939 - { 940 - phys_addr_t phys = page_to_phys(page) + offset; 941 - void *virt = page_address(page) + offset; 942 - 943 - __cpuc_flush_dcache_area(virt, size); 944 - outer_flush_range(phys, phys + size); 945 - } 946 - 947 - static const struct tegra_smmu_ops tegra30_smmu_ops = { 948 - .flush_dcache = tegra30_flush_dcache, 949 - }; 950 - 951 939 static const struct tegra_smmu_soc tegra30_smmu_soc = { 952 940 .clients = tegra30_mc_clients, 953 941 .num_clients = ARRAY_SIZE(tegra30_mc_clients), ··· 941 957 .num_swgroups = ARRAY_SIZE(tegra30_swgroups), 942 958 .supports_round_robin_arbitration = false, 943 959 .supports_request_limit = false, 960 + .num_tlb_lines = 16, 944 961 .num_asids = 4, 945 - .ops = &tegra30_smmu_ops, 946 962 }; 947 963 948 964 const struct tegra_mc_soc tegra30_mc_soc = {
+1 -1
include/linux/intel-iommu.h
··· 344 344 345 345 #ifdef CONFIG_INTEL_IOMMU 346 346 unsigned long *domain_ids; /* bitmap of domains */ 347 - struct dmar_domain **domains; /* ptr to domains */ 347 + struct dmar_domain ***domains; /* ptr to domains */ 348 348 spinlock_t lock; /* protect context, domain ids */ 349 349 struct root_entry *root_entry; /* virtual address */ 350 350
+1 -7
include/soc/tegra/mc.h
··· 51 51 unsigned int reg; 52 52 }; 53 53 54 - struct tegra_smmu_ops { 55 - void (*flush_dcache)(struct page *page, unsigned long offset, 56 - size_t size); 57 - }; 58 - 59 54 struct tegra_smmu_soc { 60 55 const struct tegra_mc_client *clients; 61 56 unsigned int num_clients; ··· 61 66 bool supports_round_robin_arbitration; 62 67 bool supports_request_limit; 63 68 69 + unsigned int num_tlb_lines; 64 70 unsigned int num_asids; 65 - 66 - const struct tegra_smmu_ops *ops; 67 71 }; 68 72 69 73 struct tegra_mc;