···16831683 By default, super page will be supported if Intel IOMMU16841684 has the capability. With this option, super page will16851685 not be supported.16861686- ecs_off [Default Off]16871687- By default, extended context tables will be supported if16881688- the hardware advertises that it has support both for the16891689- extended tables themselves, and also PASID support. With16901690- this option set, extended tables will not be used even16911691- on hardware which claims to support them.16861686+ sm_off [Default Off]16871687+ By default, scalable mode will be supported if the16881688+ hardware advertises that it has support for the scalable16891689+ mode translation. With this option set, scalable mode16901690+ will not be used even on hardware which claims to support16911691+ it.16921692 tboot_noforce [Default Off]16931693 Do not force the Intel IOMMU enabled under tboot.16941694 By default, tboot will force Intel IOMMU on, which
···1717 "arm,mmu-401"1818 "arm,mmu-500"1919 "cavium,smmu-v2"2020+ "qcom,smmu-v2"20212122 depending on the particular implementation and/or the2223 version of the architecture implemented.2424+2525+ Qcom SoCs must contain, as below, SoC-specific compatibles2626+ along with "qcom,smmu-v2":2727+ "qcom,msm8996-smmu-v2", "qcom,smmu-v2",2828+ "qcom,sdm845-smmu-v2", "qcom,smmu-v2".2929+3030+ Qcom SoCs implementing "arm,mmu-500" must also include,3131+ as below, SoC-specific compatibles:3232+ "qcom,sdm845-smmu-500", "arm,mmu-500"23332434- reg : Base address and size of the SMMU.2535···8070 property is not valid for SMMUs using stream indexing,8171 or using stream matching with #iommu-cells = <2>, and8272 may be ignored if present in such cases.7373+7474+- clock-names: List of the names of clocks input to the device. The7575+ required list depends on particular implementation and7676+ is as follows:7777+ - for "qcom,smmu-v2":7878+ - "bus": clock required for downstream bus access and7979+ for the smmu ptw,8080+ - "iface": clock required to access smmu's registers8181+ through the TCU's programming interface.8282+ - unspecified for other implementations.8383+8484+- clocks: Specifiers for all clocks listed in the clock-names property,8585+ as per generic clock bindings.8686+8787+- power-domains: Specifiers for power domains required to be powered on for8888+ the SMMU to operate, as per generic power domain bindings.83898490** Deprecated properties:8591···163137 iommu-map = <0 &smmu3 0 0x400>;164138 ...165139 };140140+141141+ /* Qcom's arm,smmu-v2 implementation */142142+ smmu4: iommu@d00000 {143143+ compatible = "qcom,msm8996-smmu-v2", "qcom,smmu-v2";144144+ reg = <0xd00000 0x10000>;145145+146146+ #global-interrupts = <1>;147147+ interrupts = <GIC_SPI 73 IRQ_TYPE_LEVEL_HIGH>,148148+ <GIC_SPI 320 IRQ_TYPE_LEVEL_HIGH>,149149+ <GIC_SPI 321 IRQ_TYPE_LEVEL_HIGH>;150150+ #iommu-cells = <1>;151151+ power-domains = <&mmcc MDSS_GDSC>;152152+153153+ clocks = <&mmcc SMMU_MDP_AXI_CLK>,154154+ <&mmcc SMMU_MDP_AHB_CLK>;155155+ clock-names = "bus", "iface";156156+ };
···1414 - "renesas,ipmmu-r8a7743" for the R8A7743 (RZ/G1M) IPMMU.1515 - "renesas,ipmmu-r8a7744" for the R8A7744 (RZ/G1N) IPMMU.1616 - "renesas,ipmmu-r8a7745" for the R8A7745 (RZ/G1E) IPMMU.1717+ - "renesas,ipmmu-r8a774a1" for the R8A774A1 (RZ/G2M) IPMMU.1818+ - "renesas,ipmmu-r8a774c0" for the R8A774C0 (RZ/G2E) IPMMU.1719 - "renesas,ipmmu-r8a7790" for the R8A7790 (R-Car H2) IPMMU.1820 - "renesas,ipmmu-r8a7791" for the R8A7791 (R-Car M2-W) IPMMU.1921 - "renesas,ipmmu-r8a7793" for the R8A7793 (R-Car M2-N) IPMMU.
···10861086 if (!device_is_registered(dev))10871087 return -ENOENT;1088108810891089- if (dev->iommu_group) {10891089+ if (device_iommu_mapped(dev)) {10901090 pr_debug("%s: Skipping device %s with iommu group %d\n",10911091 __func__, dev_name(dev),10921092 iommu_group_id(dev->iommu_group));···11291129 * and we needn't detach them from the associated11301130 * IOMMU groups11311131 */11321132- if (!dev->iommu_group) {11321132+ if (!device_iommu_mapped(dev)) {11331133 pr_debug("iommu_tce: skipping device %s with no tbl\n",11341134 dev_name(dev));11351135 return;···11481148 case BUS_NOTIFY_ADD_DEVICE:11491149 return iommu_add_device(dev);11501150 case BUS_NOTIFY_DEL_DEVICE:11511151- if (dev->iommu_group)11511151+ if (device_iommu_mapped(dev))11521152 iommu_del_device(dev);11531153 return 0;11541154 default:
···779779static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)780780{781781 struct acpi_iort_node *iommu;782782- struct iommu_fwspec *fwspec = dev->iommu_fwspec;782782+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);783783784784 iommu = iort_get_iort_node(fwspec->iommu_fwnode);785785···794794 return NULL;795795}796796797797-static inline const struct iommu_ops *iort_fwspec_iommu_ops(798798- struct iommu_fwspec *fwspec)797797+static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)799798{799799+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);800800+800801 return (fwspec && fwspec->ops) ? fwspec->ops : NULL;801802}802803···806805{807806 int err = 0;808807809809- if (ops->add_device && dev->bus && !dev->iommu_group)810810- err = ops->add_device(dev);808808+ if (dev->bus && !device_iommu_mapped(dev))809809+ err = iommu_probe_device(dev);811810812811 return err;813812}···825824 */826825int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)827826{827827+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);828828 struct acpi_iort_its_group *its;829829 struct acpi_iort_node *iommu_node, *its_node = NULL;830830 int i, resv = 0;···843841 * a given PCI or named component may map IDs to.844842 */845843846846- for (i = 0; i < dev->iommu_fwspec->num_ids; i++) {844844+ for (i = 0; i < fwspec->num_ids; i++) {847845 its_node = iort_node_map_id(iommu_node,848848- dev->iommu_fwspec->ids[i],846846+ fwspec->ids[i],849847 NULL, IORT_MSI_TYPE);850848 if (its_node)851849 break;···876874 return (resv == its->its_count) ? resv : -ENODEV;877875}878876#else879879-static inline const struct iommu_ops *iort_fwspec_iommu_ops(880880- struct iommu_fwspec *fwspec)877877+static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev);881878{ return NULL; }882879static inline int iort_add_device_replay(const struct iommu_ops *ops,883880 struct device *dev)···10461045 * If we already translated the fwspec there10471046 * is nothing left to do, return the iommu_ops.10481047 */10491049- ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);10481048+ ops = iort_fwspec_iommu_ops(dev);10501049 if (ops)10511050 return ops;10521051···10851084 * add_device callback for dev, replay it to get things in order.10861085 */10871086 if (!err) {10881088- ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);10871087+ ops = iort_fwspec_iommu_ops(dev);10891088 err = iort_add_device_replay(ops, dev);10901089 }10911090
+1-1
drivers/dma/sh/rcar-dmac.c
···18091809 * level we can't disable it selectively, so ignore channel 0 for now if18101810 * the device is part of an IOMMU group.18111811 */18121812- if (pdev->dev.iommu_group) {18121812+ if (device_iommu_mapped(&pdev->dev)) {18131813 dmac->n_channels--;18141814 channels_offset = 1;18151815 }
···291291}292292293293/*294294- * 0: readable295295- * 1: writable296296- * 2-6: reserved297297- * 7: super page298298- * 8-10: available299299- * 11: snoop behavior300300- * 12-63: Host physcial address301301- */302302-struct dma_pte {303303- u64 val;304304-};305305-306306-static inline void dma_clear_pte(struct dma_pte *pte)307307-{308308- pte->val = 0;309309-}310310-311311-static inline u64 dma_pte_addr(struct dma_pte *pte)312312-{313313-#ifdef CONFIG_64BIT314314- return pte->val & VTD_PAGE_MASK;315315-#else316316- /* Must have a full atomic 64-bit read */317317- return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;318318-#endif319319-}320320-321321-static inline bool dma_pte_present(struct dma_pte *pte)322322-{323323- return (pte->val & 3) != 0;324324-}325325-326326-static inline bool dma_pte_superpage(struct dma_pte *pte)327327-{328328- return (pte->val & DMA_PTE_LARGE_PAGE);329329-}330330-331331-static inline int first_pte_in_page(struct dma_pte *pte)332332-{333333- return !((unsigned long)pte & ~VTD_PAGE_MASK);334334-}335335-336336-/*337294 * This domain is a statically identity mapping domain.338295 * 1. This domain creats a static 1:1 mapping to all usable memory.339296 * 2. It maps to each iommu if successful.···362405static int dmar_forcedac;363406static int intel_iommu_strict;364407static int intel_iommu_superpage = 1;365365-static int intel_iommu_ecs = 1;366366-static int intel_iommu_pasid28;408408+static int intel_iommu_sm = 1;367409static int iommu_identity_mapping;368410369411#define IDENTMAP_ALL 1370412#define IDENTMAP_GFX 2371413#define IDENTMAP_AZALIA 4372414373373-/* Broadwell and Skylake have broken ECS support — normal so-called "second374374- * level" translation of DMA requests-without-PASID doesn't actually happen375375- * unless you also set the NESTE bit in an extended context-entry. Which of376376- * course means that SVM doesn't work because it's trying to do nested377377- * translation of the physical addresses it finds in the process page tables,378378- * through the IOVA->phys mapping found in the "second level" page tables.379379- *380380- * The VT-d specification was retroactively changed to change the definition381381- * of the capability bits and pretend that Broadwell/Skylake never happened...382382- * but unfortunately the wrong bit was changed. It's ECS which is broken, but383383- * for some reason it was the PASID capability bit which was redefined (from384384- * bit 28 on BDW/SKL to bit 40 in future).385385- *386386- * So our test for ECS needs to eschew those implementations which set the old387387- * PASID capabiity bit 28, since those are the ones on which ECS is broken.388388- * Unless we are working around the 'pasid28' limitations, that is, by putting389389- * the device into passthrough mode for normal DMA and thus masking the bug.390390- */391391-#define ecs_enabled(iommu) (intel_iommu_ecs && ecap_ecs(iommu->ecap) && \392392- (intel_iommu_pasid28 || !ecap_broken_pasid(iommu->ecap)))393393-/* PASID support is thus enabled if ECS is enabled and *either* of the old394394- * or new capability bits are set. */395395-#define pasid_enabled(iommu) (ecs_enabled(iommu) && \396396- (ecap_pasid(iommu->ecap) || ecap_broken_pasid(iommu->ecap)))415415+#define sm_supported(iommu) (intel_iommu_sm && ecap_smts((iommu)->ecap))416416+#define pasid_supported(iommu) (sm_supported(iommu) && \417417+ ecap_pasid((iommu)->ecap))397418398419int intel_iommu_gfx_mapped;399420EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);···382447383448/*384449 * Iterate over elements in device_domain_list and call the specified385385- * callback @fn against each element. This helper should only be used386386- * in the context where the device_domain_lock has already been holden.450450+ * callback @fn against each element.387451 */388452int for_each_device_domain(int (*fn)(struct device_domain_info *info,389453 void *data), void *data)390454{391455 int ret = 0;456456+ unsigned long flags;392457 struct device_domain_info *info;393458394394- assert_spin_locked(&device_domain_lock);459459+ spin_lock_irqsave(&device_domain_lock, flags);395460 list_for_each_entry(info, &device_domain_list, global) {396461 ret = fn(info, data);397397- if (ret)462462+ if (ret) {463463+ spin_unlock_irqrestore(&device_domain_lock, flags);398464 return ret;465465+ }399466 }467467+ spin_unlock_irqrestore(&device_domain_lock, flags);400468401469 return 0;402470}···454516 } else if (!strncmp(str, "sp_off", 6)) {455517 pr_info("Disable supported super page\n");456518 intel_iommu_superpage = 0;457457- } else if (!strncmp(str, "ecs_off", 7)) {458458- printk(KERN_INFO459459- "Intel-IOMMU: disable extended context table support\n");460460- intel_iommu_ecs = 0;461461- } else if (!strncmp(str, "pasid28", 7)) {462462- printk(KERN_INFO463463- "Intel-IOMMU: enable pre-production PASID support\n");464464- intel_iommu_pasid28 = 1;465465- iommu_identity_mapping |= IDENTMAP_GFX;519519+ } else if (!strncmp(str, "sm_off", 6)) {520520+ pr_info("Intel-IOMMU: disable scalable mode support\n");521521+ intel_iommu_sm = 0;466522 } else if (!strncmp(str, "tboot_noforce", 13)) {467523 printk(KERN_INFO468524 "Intel-IOMMU: not forcing on after tboot. This could expose security risk for tboot\n");···703771 u64 *entry;704772705773 entry = &root->lo;706706- if (ecs_enabled(iommu)) {774774+ if (sm_supported(iommu)) {707775 if (devfn >= 0x80) {708776 devfn -= 0x80;709777 entry = &root->hi;···845913 if (context)846914 free_pgtable_page(context);847915848848- if (!ecs_enabled(iommu))916916+ if (!sm_supported(iommu))849917 continue;850918851919 context = iommu_context_addr(iommu, i, 0x80, 0);···11971265 unsigned long flag;1198126611991267 addr = virt_to_phys(iommu->root_entry);12001200- if (ecs_enabled(iommu))12011201- addr |= DMA_RTADDR_RTT;12681268+ if (sm_supported(iommu))12691269+ addr |= DMA_RTADDR_SMT;1202127012031271 raw_spin_lock_irqsave(&iommu->register_lock, flag);12041272 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr);···12121280 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);12131281}1214128212151215-static void iommu_flush_write_buffer(struct intel_iommu *iommu)12831283+void iommu_flush_write_buffer(struct intel_iommu *iommu)12161284{12171285 u32 val;12181286 unsigned long flag;···16231691 */16241692 set_bit(0, iommu->domain_ids);1625169316941694+ /*16951695+ * Vt-d spec rev3.0 (section 6.2.3.1) requires that each pasid16961696+ * entry for first-level or pass-through translation modes should16971697+ * be programmed with a domain id different from those used for16981698+ * second-level or nested translation. We reserve a domain id for16991699+ * this purpose.17001700+ */17011701+ if (sm_supported(iommu))17021702+ set_bit(FLPT_DEFAULT_DID, iommu->domain_ids);17031703+16261704 return 0;16271705}16281706···16971755 free_context_table(iommu);1698175616991757#ifdef CONFIG_INTEL_IOMMU_SVM17001700- if (pasid_enabled(iommu)) {17581758+ if (pasid_supported(iommu)) {17011759 if (ecap_prs(iommu->ecap))17021760 intel_svm_finish_prq(iommu);17031703- intel_svm_exit(iommu);17041761 }17051762#endif17061763}···19191978 free_domain_mem(domain);19201979}1921198019811981+/*19821982+ * Get the PASID directory size for scalable mode context entry.19831983+ * Value of X in the PDTS field of a scalable mode context entry19841984+ * indicates PASID directory with 2^(X + 7) entries.19851985+ */19861986+static inline unsigned long context_get_sm_pds(struct pasid_table *table)19871987+{19881988+ int pds, max_pde;19891989+19901990+ max_pde = table->max_pasid >> PASID_PDE_SHIFT;19911991+ pds = find_first_bit((unsigned long *)&max_pde, MAX_NR_PASID_BITS);19921992+ if (pds < 7)19931993+ return 0;19941994+19951995+ return pds - 7;19961996+}19971997+19981998+/*19991999+ * Set the RID_PASID field of a scalable mode context entry. The20002000+ * IOMMU hardware will use the PASID value set in this field for20012001+ * DMA translations of DMA requests without PASID.20022002+ */20032003+static inline void20042004+context_set_sm_rid2pasid(struct context_entry *context, unsigned long pasid)20052005+{20062006+ context->hi |= pasid & ((1 << 20) - 1);20072007+ context->hi |= (1 << 20);20082008+}20092009+20102010+/*20112011+ * Set the DTE(Device-TLB Enable) field of a scalable mode context20122012+ * entry.20132013+ */20142014+static inline void context_set_sm_dte(struct context_entry *context)20152015+{20162016+ context->lo |= (1 << 2);20172017+}20182018+20192019+/*20202020+ * Set the PRE(Page Request Enable) field of a scalable mode context20212021+ * entry.20222022+ */20232023+static inline void context_set_sm_pre(struct context_entry *context)20242024+{20252025+ context->lo |= (1 << 4);20262026+}20272027+20282028+/* Convert value to context PASID directory size field coding. */20292029+#define context_pdts(pds) (((pds) & 0x7) << 9)20302030+19222031static int domain_context_mapping_one(struct dmar_domain *domain,19232032 struct intel_iommu *iommu,20332033+ struct pasid_table *table,19242034 u8 bus, u8 devfn)19252035{19262036 u16 did = domain->iommu_did[iommu->seq_id];···19791987 struct device_domain_info *info = NULL;19801988 struct context_entry *context;19811989 unsigned long flags;19821982- struct dma_pte *pgd;19831983- int ret, agaw;19901990+ int ret;1984199119851992 WARN_ON(did == 0);19861993···20252034 }20262035 }2027203620282028- pgd = domain->pgd;20292029-20302037 context_clear_entry(context);20312031- context_set_domain_id(context, did);2032203820332033- /*20342034- * Skip top levels of page tables for iommu which has less agaw20352035- * than default. Unnecessary for PT mode.20362036- */20372037- if (translation != CONTEXT_TT_PASS_THROUGH) {20382038- for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {20392039- ret = -ENOMEM;20402040- pgd = phys_to_virt(dma_pte_addr(pgd));20412041- if (!dma_pte_present(pgd))20422042- goto out_unlock;20432043- }20392039+ if (sm_supported(iommu)) {20402040+ unsigned long pds;2044204120422042+ WARN_ON(!table);20432043+20442044+ /* Setup the PASID DIR pointer: */20452045+ pds = context_get_sm_pds(table);20462046+ context->lo = (u64)virt_to_phys(table->table) |20472047+ context_pdts(pds);20482048+20492049+ /* Setup the RID_PASID field: */20502050+ context_set_sm_rid2pasid(context, PASID_RID2PASID);20512051+20522052+ /*20532053+ * Setup the Device-TLB enable bit and Page request20542054+ * Enable bit:20552055+ */20452056 info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);20462057 if (info && info->ats_supported)20472047- translation = CONTEXT_TT_DEV_IOTLB;20482048- else20492049- translation = CONTEXT_TT_MULTI_LEVEL;20502050-20512051- context_set_address_root(context, virt_to_phys(pgd));20522052- context_set_address_width(context, iommu->agaw);20582058+ context_set_sm_dte(context);20592059+ if (info && info->pri_supported)20602060+ context_set_sm_pre(context);20532061 } else {20542054- /*20552055- * In pass through mode, AW must be programmed to20562056- * indicate the largest AGAW value supported by20572057- * hardware. And ASR is ignored by hardware.20582058- */20592059- context_set_address_width(context, iommu->msagaw);20622062+ struct dma_pte *pgd = domain->pgd;20632063+ int agaw;20642064+20652065+ context_set_domain_id(context, did);20662066+ context_set_translation_type(context, translation);20672067+20682068+ if (translation != CONTEXT_TT_PASS_THROUGH) {20692069+ /*20702070+ * Skip top levels of page tables for iommu which has20712071+ * less agaw than default. Unnecessary for PT mode.20722072+ */20732073+ for (agaw = domain->agaw; agaw > iommu->agaw; agaw--) {20742074+ ret = -ENOMEM;20752075+ pgd = phys_to_virt(dma_pte_addr(pgd));20762076+ if (!dma_pte_present(pgd))20772077+ goto out_unlock;20782078+ }20792079+20802080+ info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);20812081+ if (info && info->ats_supported)20822082+ translation = CONTEXT_TT_DEV_IOTLB;20832083+ else20842084+ translation = CONTEXT_TT_MULTI_LEVEL;20852085+20862086+ context_set_address_root(context, virt_to_phys(pgd));20872087+ context_set_address_width(context, agaw);20882088+ } else {20892089+ /*20902090+ * In pass through mode, AW must be programmed to20912091+ * indicate the largest AGAW value supported by20922092+ * hardware. And ASR is ignored by hardware.20932093+ */20942094+ context_set_address_width(context, iommu->msagaw);20952095+ }20602096 }2061209720622062- context_set_translation_type(context, translation);20632098 context_set_fault_enable(context);20642099 context_set_present(context);20652100 domain_flush_cache(domain, context, sizeof(*context));···21192102struct domain_context_mapping_data {21202103 struct dmar_domain *domain;21212104 struct intel_iommu *iommu;21052105+ struct pasid_table *table;21222106};2123210721242108static int domain_context_mapping_cb(struct pci_dev *pdev,···21282110 struct domain_context_mapping_data *data = opaque;2129211121302112 return domain_context_mapping_one(data->domain, data->iommu,21312131- PCI_BUS_NUM(alias), alias & 0xff);21132113+ data->table, PCI_BUS_NUM(alias),21142114+ alias & 0xff);21322115}2133211621342117static int21352118domain_context_mapping(struct dmar_domain *domain, struct device *dev)21362119{21202120+ struct domain_context_mapping_data data;21212121+ struct pasid_table *table;21372122 struct intel_iommu *iommu;21382123 u8 bus, devfn;21392139- struct domain_context_mapping_data data;2140212421412125 iommu = device_to_iommu(dev, &bus, &devfn);21422126 if (!iommu)21432127 return -ENODEV;2144212821292129+ table = intel_pasid_get_table(dev);21302130+21452131 if (!dev_is_pci(dev))21462146- return domain_context_mapping_one(domain, iommu, bus, devfn);21322132+ return domain_context_mapping_one(domain, iommu, table,21332133+ bus, devfn);2147213421482135 data.domain = domain;21492136 data.iommu = iommu;21372137+ data.table = table;2150213821512139 return pci_for_each_dma_alias(to_pci_dev(dev),21522140 &domain_context_mapping_cb, &data);···24882464 dmar_find_matched_atsr_unit(pdev))24892465 info->ats_supported = 1;2490246624912491- if (ecs_enabled(iommu)) {24922492- if (pasid_enabled(iommu)) {24672467+ if (sm_supported(iommu)) {24682468+ if (pasid_supported(iommu)) {24932469 int features = pci_pasid_features(pdev);24942470 if (features >= 0)24952471 info->pasid_supported = features | 1;···25352511 list_add(&info->global, &device_domain_list);25362512 if (dev)25372513 dev->archdata.iommu = info;25142514+ spin_unlock_irqrestore(&device_domain_lock, flags);2538251525392539- if (dev && dev_is_pci(dev) && info->pasid_supported) {25162516+ /* PASID table is mandatory for a PCI device in scalable mode. */25172517+ if (dev && dev_is_pci(dev) && sm_supported(iommu)) {25402518 ret = intel_pasid_alloc_table(dev);25412519 if (ret) {25422542- pr_warn("No pasid table for %s, pasid disabled\n",25432543- dev_name(dev));25442544- info->pasid_supported = 0;25202520+ pr_err("PASID table allocation for %s failed\n",25212521+ dev_name(dev));25222522+ dmar_remove_one_dev_info(domain, dev);25232523+ return NULL;25242524+ }25252525+25262526+ /* Setup the PASID entry for requests without PASID: */25272527+ spin_lock(&iommu->lock);25282528+ if (hw_pass_through && domain_type_is_si(domain))25292529+ ret = intel_pasid_setup_pass_through(iommu, domain,25302530+ dev, PASID_RID2PASID);25312531+ else25322532+ ret = intel_pasid_setup_second_level(iommu, domain,25332533+ dev, PASID_RID2PASID);25342534+ spin_unlock(&iommu->lock);25352535+ if (ret) {25362536+ pr_err("Setup RID2PASID for %s failed\n",25372537+ dev_name(dev));25382538+ dmar_remove_one_dev_info(domain, dev);25392539+ return NULL;25452540 }25462541 }25472547- spin_unlock_irqrestore(&device_domain_lock, flags);2548254225492543 if (dev && domain_context_mapping(domain, dev)) {25502544 pr_err("Domain context map for %s failed\n", dev_name(dev));···33193277 * We need to ensure the system pasid table is no bigger33203278 * than the smallest supported.33213279 */33223322- if (pasid_enabled(iommu)) {32803280+ if (pasid_supported(iommu)) {33233281 u32 temp = 2 << ecap_pss(iommu->ecap);3324328233253283 intel_pasid_max_id = min_t(u32, temp,···33803338 if (!ecap_pass_through(iommu->ecap))33813339 hw_pass_through = 0;33823340#ifdef CONFIG_INTEL_IOMMU_SVM33833383- if (pasid_enabled(iommu))33413341+ if (pasid_supported(iommu))33843342 intel_svm_init(iommu);33853343#endif33863344 }···34843442 iommu_flush_write_buffer(iommu);3485344334863444#ifdef CONFIG_INTEL_IOMMU_SVM34873487- if (pasid_enabled(iommu) && ecap_prs(iommu->ecap)) {34453445+ if (pasid_supported(iommu) && ecap_prs(iommu->ecap)) {34883446 ret = intel_svm_enable_prq(iommu);34893447 if (ret)34903448 goto free_iommu;···43734331 goto out;4374433243754333#ifdef CONFIG_INTEL_IOMMU_SVM43764376- if (pasid_enabled(iommu))43344334+ if (pasid_supported(iommu))43774335 intel_svm_init(iommu);43784336#endif43794337···43904348 iommu_flush_write_buffer(iommu);4391434943924350#ifdef CONFIG_INTEL_IOMMU_SVM43934393- if (pasid_enabled(iommu) && ecap_prs(iommu->ecap)) {43514351+ if (pasid_supported(iommu) && ecap_prs(iommu->ecap)) {43944352 ret = intel_svm_enable_prq(iommu);43954353 if (ret)43964354 goto disable_iommu;···49254883 iommu = info->iommu;4926488449274885 if (info->dev) {48864886+ if (dev_is_pci(info->dev) && sm_supported(iommu))48874887+ intel_pasid_tear_down_entry(iommu, info->dev,48884888+ PASID_RID2PASID);48894889+49284890 iommu_disable_dev_iotlb(info);49294891 domain_context_clear(iommu, info->dev);49304892 intel_pasid_free_table(info->dev);···52565210}5257521152585212#ifdef CONFIG_INTEL_IOMMU_SVM52595259-#define MAX_NR_PASID_BITS (20)52605260-static inline unsigned long intel_iommu_get_pts(struct device *dev)52615261-{52625262- int pts, max_pasid;52635263-52645264- max_pasid = intel_pasid_get_dev_max_id(dev);52655265- pts = find_first_bit((unsigned long *)&max_pasid, MAX_NR_PASID_BITS);52665266- if (pts < 5)52675267- return 0;52685268-52695269- return pts - 5;52705270-}52715271-52725213int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct intel_svm_dev *sdev)52735214{52745215 struct device_domain_info *info;···52875254 sdev->sid = PCI_DEVID(info->bus, info->devfn);5288525552895256 if (!(ctx_lo & CONTEXT_PASIDE)) {52905290- if (iommu->pasid_state_table)52915291- context[1].hi = (u64)virt_to_phys(iommu->pasid_state_table);52925292- context[1].lo = (u64)virt_to_phys(info->pasid_table->table) |52935293- intel_iommu_get_pts(sdev->dev);52945294-52955295- wmb();52965296- /* CONTEXT_TT_MULTI_LEVEL and CONTEXT_TT_DEV_IOTLB are both52975297- * extended to permit requests-with-PASID if the PASIDE bit52985298- * is set. which makes sense. For CONTEXT_TT_PASS_THROUGH,52995299- * however, the PASIDE bit is ignored and requests-with-PASID53005300- * are unconditionally blocked. Which makes less sense.53015301- * So convert from CONTEXT_TT_PASS_THROUGH to one of the new53025302- * "guest mode" translation types depending on whether ATS53035303- * is available or not. Annoyingly, we can't use the new53045304- * modes *unless* PASIDE is set. */53055305- if ((ctx_lo & CONTEXT_TT_MASK) == (CONTEXT_TT_PASS_THROUGH << 2)) {53065306- ctx_lo &= ~CONTEXT_TT_MASK;53075307- if (info->ats_supported)53085308- ctx_lo |= CONTEXT_TT_PT_PASID_DEV_IOTLB << 2;53095309- else53105310- ctx_lo |= CONTEXT_TT_PT_PASID << 2;53115311- }53125257 ctx_lo |= CONTEXT_PASIDE;53135313- if (iommu->pasid_state_table)53145314- ctx_lo |= CONTEXT_DINVE;53155315- if (info->pri_supported)53165316- ctx_lo |= CONTEXT_PRS;53175258 context[0].lo = ctx_lo;53185259 wmb();53195260 iommu->flush.flush_context(iommu, sdev->did, sdev->sid,
+433-16
drivers/iommu/intel-pasid.c
···991010#define pr_fmt(fmt) "DMAR: " fmt11111212+#include <linux/bitops.h>1313+#include <linux/cpufeature.h>1214#include <linux/dmar.h>1315#include <linux/intel-iommu.h>1416#include <linux/iommu.h>···125123 struct pasid_table *pasid_table;126124 struct pasid_table_opaque data;127125 struct page *pages;128128- size_t size, count;126126+ int max_pasid = 0;129127 int ret, order;128128+ int size;130129130130+ might_sleep();131131 info = dev->archdata.iommu;132132- if (WARN_ON(!info || !dev_is_pci(dev) ||133133- !info->pasid_supported || info->pasid_table))132132+ if (WARN_ON(!info || !dev_is_pci(dev) || info->pasid_table))134133 return -EINVAL;135134136135 /* DMA alias device already has a pasid table, use it: */···141138 if (ret)142139 goto attach_out;143140144144- pasid_table = kzalloc(sizeof(*pasid_table), GFP_ATOMIC);141141+ pasid_table = kzalloc(sizeof(*pasid_table), GFP_KERNEL);145142 if (!pasid_table)146143 return -ENOMEM;147144 INIT_LIST_HEAD(&pasid_table->dev);148145149149- size = sizeof(struct pasid_entry);150150- count = min_t(int, pci_max_pasids(to_pci_dev(dev)), intel_pasid_max_id);151151- order = get_order(size * count);146146+ if (info->pasid_supported)147147+ max_pasid = min_t(int, pci_max_pasids(to_pci_dev(dev)),148148+ intel_pasid_max_id);149149+150150+ size = max_pasid >> (PASID_PDE_SHIFT - 3);151151+ order = size ? get_order(size) : 0;152152 pages = alloc_pages_node(info->iommu->node,153153- GFP_ATOMIC | __GFP_ZERO,154154- order);153153+ GFP_KERNEL | __GFP_ZERO, order);155154 if (!pages)156155 return -ENOMEM;157156158157 pasid_table->table = page_address(pages);159158 pasid_table->order = order;160160- pasid_table->max_pasid = count;159159+ pasid_table->max_pasid = 1 << (order + PAGE_SHIFT + 3);161160162161attach_out:163162 device_attach_pasid_table(info, pasid_table);···167162 return 0;168163}169164165165+/* Get PRESENT bit of a PASID directory entry. */166166+static inline bool167167+pasid_pde_is_present(struct pasid_dir_entry *pde)168168+{169169+ return READ_ONCE(pde->val) & PASID_PTE_PRESENT;170170+}171171+172172+/* Get PASID table from a PASID directory entry. */173173+static inline struct pasid_entry *174174+get_pasid_table_from_pde(struct pasid_dir_entry *pde)175175+{176176+ if (!pasid_pde_is_present(pde))177177+ return NULL;178178+179179+ return phys_to_virt(READ_ONCE(pde->val) & PDE_PFN_MASK);180180+}181181+170182void intel_pasid_free_table(struct device *dev)171183{172184 struct device_domain_info *info;173185 struct pasid_table *pasid_table;186186+ struct pasid_dir_entry *dir;187187+ struct pasid_entry *table;188188+ int i, max_pde;174189175190 info = dev->archdata.iommu;176176- if (!info || !dev_is_pci(dev) ||177177- !info->pasid_supported || !info->pasid_table)191191+ if (!info || !dev_is_pci(dev) || !info->pasid_table)178192 return;179193180194 pasid_table = info->pasid_table;···201177202178 if (!list_empty(&pasid_table->dev))203179 return;180180+181181+ /* Free scalable mode PASID directory tables: */182182+ dir = pasid_table->table;183183+ max_pde = pasid_table->max_pasid >> PASID_PDE_SHIFT;184184+ for (i = 0; i < max_pde; i++) {185185+ table = get_pasid_table_from_pde(&dir[i]);186186+ free_pgtable_page(table);187187+ }204188205189 free_pages((unsigned long)pasid_table->table, pasid_table->order);206190 kfree(pasid_table);···238206239207struct pasid_entry *intel_pasid_get_entry(struct device *dev, int pasid)240208{209209+ struct device_domain_info *info;241210 struct pasid_table *pasid_table;211211+ struct pasid_dir_entry *dir;242212 struct pasid_entry *entries;213213+ int dir_index, index;243214244215 pasid_table = intel_pasid_get_table(dev);245216 if (WARN_ON(!pasid_table || pasid < 0 ||246217 pasid >= intel_pasid_get_dev_max_id(dev)))247218 return NULL;248219249249- entries = pasid_table->table;220220+ dir = pasid_table->table;221221+ info = dev->archdata.iommu;222222+ dir_index = pasid >> PASID_PDE_SHIFT;223223+ index = pasid & PASID_PTE_MASK;250224251251- return &entries[pasid];225225+ spin_lock(&pasid_lock);226226+ entries = get_pasid_table_from_pde(&dir[dir_index]);227227+ if (!entries) {228228+ entries = alloc_pgtable_page(info->iommu->node);229229+ if (!entries) {230230+ spin_unlock(&pasid_lock);231231+ return NULL;232232+ }233233+234234+ WRITE_ONCE(dir[dir_index].val,235235+ (u64)virt_to_phys(entries) | PASID_PTE_PRESENT);236236+ }237237+ spin_unlock(&pasid_lock);238238+239239+ return &entries[index];252240}253241254242/*···276224 */277225static inline void pasid_clear_entry(struct pasid_entry *pe)278226{279279- WRITE_ONCE(pe->val, 0);227227+ WRITE_ONCE(pe->val[0], 0);228228+ WRITE_ONCE(pe->val[1], 0);229229+ WRITE_ONCE(pe->val[2], 0);230230+ WRITE_ONCE(pe->val[3], 0);231231+ WRITE_ONCE(pe->val[4], 0);232232+ WRITE_ONCE(pe->val[5], 0);233233+ WRITE_ONCE(pe->val[6], 0);234234+ WRITE_ONCE(pe->val[7], 0);280235}281236282282-void intel_pasid_clear_entry(struct device *dev, int pasid)237237+static void intel_pasid_clear_entry(struct device *dev, int pasid)283238{284239 struct pasid_entry *pe;285240···295236 return;296237297238 pasid_clear_entry(pe);239239+}240240+241241+static inline void pasid_set_bits(u64 *ptr, u64 mask, u64 bits)242242+{243243+ u64 old;244244+245245+ old = READ_ONCE(*ptr);246246+ WRITE_ONCE(*ptr, (old & ~mask) | bits);247247+}248248+249249+/*250250+ * Setup the DID(Domain Identifier) field (Bit 64~79) of scalable mode251251+ * PASID entry.252252+ */253253+static inline void254254+pasid_set_domain_id(struct pasid_entry *pe, u64 value)255255+{256256+ pasid_set_bits(&pe->val[1], GENMASK_ULL(15, 0), value);257257+}258258+259259+/*260260+ * Get domain ID value of a scalable mode PASID entry.261261+ */262262+static inline u16263263+pasid_get_domain_id(struct pasid_entry *pe)264264+{265265+ return (u16)(READ_ONCE(pe->val[1]) & GENMASK_ULL(15, 0));266266+}267267+268268+/*269269+ * Setup the SLPTPTR(Second Level Page Table Pointer) field (Bit 12~63)270270+ * of a scalable mode PASID entry.271271+ */272272+static inline void273273+pasid_set_slptr(struct pasid_entry *pe, u64 value)274274+{275275+ pasid_set_bits(&pe->val[0], VTD_PAGE_MASK, value);276276+}277277+278278+/*279279+ * Setup the AW(Address Width) field (Bit 2~4) of a scalable mode PASID280280+ * entry.281281+ */282282+static inline void283283+pasid_set_address_width(struct pasid_entry *pe, u64 value)284284+{285285+ pasid_set_bits(&pe->val[0], GENMASK_ULL(4, 2), value << 2);286286+}287287+288288+/*289289+ * Setup the PGTT(PASID Granular Translation Type) field (Bit 6~8)290290+ * of a scalable mode PASID entry.291291+ */292292+static inline void293293+pasid_set_translation_type(struct pasid_entry *pe, u64 value)294294+{295295+ pasid_set_bits(&pe->val[0], GENMASK_ULL(8, 6), value << 6);296296+}297297+298298+/*299299+ * Enable fault processing by clearing the FPD(Fault Processing300300+ * Disable) field (Bit 1) of a scalable mode PASID entry.301301+ */302302+static inline void pasid_set_fault_enable(struct pasid_entry *pe)303303+{304304+ pasid_set_bits(&pe->val[0], 1 << 1, 0);305305+}306306+307307+/*308308+ * Setup the SRE(Supervisor Request Enable) field (Bit 128) of a309309+ * scalable mode PASID entry.310310+ */311311+static inline void pasid_set_sre(struct pasid_entry *pe)312312+{313313+ pasid_set_bits(&pe->val[2], 1 << 0, 1);314314+}315315+316316+/*317317+ * Setup the P(Present) field (Bit 0) of a scalable mode PASID318318+ * entry.319319+ */320320+static inline void pasid_set_present(struct pasid_entry *pe)321321+{322322+ pasid_set_bits(&pe->val[0], 1 << 0, 1);323323+}324324+325325+/*326326+ * Setup Page Walk Snoop bit (Bit 87) of a scalable mode PASID327327+ * entry.328328+ */329329+static inline void pasid_set_page_snoop(struct pasid_entry *pe, bool value)330330+{331331+ pasid_set_bits(&pe->val[1], 1 << 23, value);332332+}333333+334334+/*335335+ * Setup the First Level Page table Pointer field (Bit 140~191)336336+ * of a scalable mode PASID entry.337337+ */338338+static inline void339339+pasid_set_flptr(struct pasid_entry *pe, u64 value)340340+{341341+ pasid_set_bits(&pe->val[2], VTD_PAGE_MASK, value);342342+}343343+344344+/*345345+ * Setup the First Level Paging Mode field (Bit 130~131) of a346346+ * scalable mode PASID entry.347347+ */348348+static inline void349349+pasid_set_flpm(struct pasid_entry *pe, u64 value)350350+{351351+ pasid_set_bits(&pe->val[2], GENMASK_ULL(3, 2), value << 2);352352+}353353+354354+static void355355+pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu,356356+ u16 did, int pasid)357357+{358358+ struct qi_desc desc;359359+360360+ desc.qw0 = QI_PC_DID(did) | QI_PC_PASID_SEL | QI_PC_PASID(pasid);361361+ desc.qw1 = 0;362362+ desc.qw2 = 0;363363+ desc.qw3 = 0;364364+365365+ qi_submit_sync(&desc, iommu);366366+}367367+368368+static void369369+iotlb_invalidation_with_pasid(struct intel_iommu *iommu, u16 did, u32 pasid)370370+{371371+ struct qi_desc desc;372372+373373+ desc.qw0 = QI_EIOTLB_PASID(pasid) | QI_EIOTLB_DID(did) |374374+ QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | QI_EIOTLB_TYPE;375375+ desc.qw1 = 0;376376+ desc.qw2 = 0;377377+ desc.qw3 = 0;378378+379379+ qi_submit_sync(&desc, iommu);380380+}381381+382382+static void383383+devtlb_invalidation_with_pasid(struct intel_iommu *iommu,384384+ struct device *dev, int pasid)385385+{386386+ struct device_domain_info *info;387387+ u16 sid, qdep, pfsid;388388+389389+ info = dev->archdata.iommu;390390+ if (!info || !info->ats_enabled)391391+ return;392392+393393+ sid = info->bus << 8 | info->devfn;394394+ qdep = info->ats_qdep;395395+ pfsid = info->pfsid;396396+397397+ qi_flush_dev_iotlb(iommu, sid, pfsid, qdep, 0, 64 - VTD_PAGE_SHIFT);398398+}399399+400400+void intel_pasid_tear_down_entry(struct intel_iommu *iommu,401401+ struct device *dev, int pasid)402402+{403403+ struct pasid_entry *pte;404404+ u16 did;405405+406406+ pte = intel_pasid_get_entry(dev, pasid);407407+ if (WARN_ON(!pte))408408+ return;409409+410410+ intel_pasid_clear_entry(dev, pasid);411411+ did = pasid_get_domain_id(pte);412412+413413+ if (!ecap_coherent(iommu->ecap))414414+ clflush_cache_range(pte, sizeof(*pte));415415+416416+ pasid_cache_invalidation_with_pasid(iommu, did, pasid);417417+ iotlb_invalidation_with_pasid(iommu, did, pasid);418418+419419+ /* Device IOTLB doesn't need to be flushed in caching mode. */420420+ if (!cap_caching_mode(iommu->cap))421421+ devtlb_invalidation_with_pasid(iommu, dev, pasid);422422+}423423+424424+/*425425+ * Set up the scalable mode pasid table entry for first only426426+ * translation type.427427+ */428428+int intel_pasid_setup_first_level(struct intel_iommu *iommu,429429+ struct device *dev, pgd_t *pgd,430430+ int pasid, u16 did, int flags)431431+{432432+ struct pasid_entry *pte;433433+434434+ if (!ecap_flts(iommu->ecap)) {435435+ pr_err("No first level translation support on %s\n",436436+ iommu->name);437437+ return -EINVAL;438438+ }439439+440440+ pte = intel_pasid_get_entry(dev, pasid);441441+ if (WARN_ON(!pte))442442+ return -EINVAL;443443+444444+ pasid_clear_entry(pte);445445+446446+ /* Setup the first level page table pointer: */447447+ pasid_set_flptr(pte, (u64)__pa(pgd));448448+ if (flags & PASID_FLAG_SUPERVISOR_MODE) {449449+ if (!ecap_srs(iommu->ecap)) {450450+ pr_err("No supervisor request support on %s\n",451451+ iommu->name);452452+ return -EINVAL;453453+ }454454+ pasid_set_sre(pte);455455+ }456456+457457+#ifdef CONFIG_X86458458+ if (cpu_feature_enabled(X86_FEATURE_LA57))459459+ pasid_set_flpm(pte, 1);460460+#endif /* CONFIG_X86 */461461+462462+ pasid_set_domain_id(pte, did);463463+ pasid_set_address_width(pte, iommu->agaw);464464+ pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));465465+466466+ /* Setup Present and PASID Granular Transfer Type: */467467+ pasid_set_translation_type(pte, 1);468468+ pasid_set_present(pte);469469+470470+ if (!ecap_coherent(iommu->ecap))471471+ clflush_cache_range(pte, sizeof(*pte));472472+473473+ if (cap_caching_mode(iommu->cap)) {474474+ pasid_cache_invalidation_with_pasid(iommu, did, pasid);475475+ iotlb_invalidation_with_pasid(iommu, did, pasid);476476+ } else {477477+ iommu_flush_write_buffer(iommu);478478+ }479479+480480+ return 0;481481+}482482+483483+/*484484+ * Set up the scalable mode pasid entry for second only translation type.485485+ */486486+int intel_pasid_setup_second_level(struct intel_iommu *iommu,487487+ struct dmar_domain *domain,488488+ struct device *dev, int pasid)489489+{490490+ struct pasid_entry *pte;491491+ struct dma_pte *pgd;492492+ u64 pgd_val;493493+ int agaw;494494+ u16 did;495495+496496+ /*497497+ * If hardware advertises no support for second level498498+ * translation, return directly.499499+ */500500+ if (!ecap_slts(iommu->ecap)) {501501+ pr_err("No second level translation support on %s\n",502502+ iommu->name);503503+ return -EINVAL;504504+ }505505+506506+ /*507507+ * Skip top levels of page tables for iommu which has less agaw508508+ * than default. Unnecessary for PT mode.509509+ */510510+ pgd = domain->pgd;511511+ for (agaw = domain->agaw; agaw > iommu->agaw; agaw--) {512512+ pgd = phys_to_virt(dma_pte_addr(pgd));513513+ if (!dma_pte_present(pgd)) {514514+ dev_err(dev, "Invalid domain page table\n");515515+ return -EINVAL;516516+ }517517+ }518518+519519+ pgd_val = virt_to_phys(pgd);520520+ did = domain->iommu_did[iommu->seq_id];521521+522522+ pte = intel_pasid_get_entry(dev, pasid);523523+ if (!pte) {524524+ dev_err(dev, "Failed to get pasid entry of PASID %d\n", pasid);525525+ return -ENODEV;526526+ }527527+528528+ pasid_clear_entry(pte);529529+ pasid_set_domain_id(pte, did);530530+ pasid_set_slptr(pte, pgd_val);531531+ pasid_set_address_width(pte, agaw);532532+ pasid_set_translation_type(pte, 2);533533+ pasid_set_fault_enable(pte);534534+ pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));535535+536536+ /*537537+ * Since it is a second level only translation setup, we should538538+ * set SRE bit as well (addresses are expected to be GPAs).539539+ */540540+ pasid_set_sre(pte);541541+ pasid_set_present(pte);542542+543543+ if (!ecap_coherent(iommu->ecap))544544+ clflush_cache_range(pte, sizeof(*pte));545545+546546+ if (cap_caching_mode(iommu->cap)) {547547+ pasid_cache_invalidation_with_pasid(iommu, did, pasid);548548+ iotlb_invalidation_with_pasid(iommu, did, pasid);549549+ } else {550550+ iommu_flush_write_buffer(iommu);551551+ }552552+553553+ return 0;554554+}555555+556556+/*557557+ * Set up the scalable mode pasid entry for passthrough translation type.558558+ */559559+int intel_pasid_setup_pass_through(struct intel_iommu *iommu,560560+ struct dmar_domain *domain,561561+ struct device *dev, int pasid)562562+{563563+ u16 did = FLPT_DEFAULT_DID;564564+ struct pasid_entry *pte;565565+566566+ pte = intel_pasid_get_entry(dev, pasid);567567+ if (!pte) {568568+ dev_err(dev, "Failed to get pasid entry of PASID %d\n", pasid);569569+ return -ENODEV;570570+ }571571+572572+ pasid_clear_entry(pte);573573+ pasid_set_domain_id(pte, did);574574+ pasid_set_address_width(pte, iommu->agaw);575575+ pasid_set_translation_type(pte, 4);576576+ pasid_set_fault_enable(pte);577577+ pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));578578+579579+ /*580580+ * We should set SRE bit as well since the addresses are expected581581+ * to be GPAs.582582+ */583583+ pasid_set_sre(pte);584584+ pasid_set_present(pte);585585+586586+ if (!ecap_coherent(iommu->ecap))587587+ clflush_cache_range(pte, sizeof(*pte));588588+589589+ if (cap_caching_mode(iommu->cap)) {590590+ pasid_cache_invalidation_with_pasid(iommu, did, pasid);591591+ iotlb_invalidation_with_pasid(iommu, did, pasid);592592+ } else {593593+ iommu_flush_write_buffer(iommu);594594+ }595595+596596+ return 0;298597}
+37-3
drivers/iommu/intel-pasid.h
···1010#ifndef __INTEL_PASID_H1111#define __INTEL_PASID_H12121313+#define PASID_RID2PASID 0x01314#define PASID_MIN 0x11414-#define PASID_MAX 0x200001515+#define PASID_MAX 0x1000001616+#define PASID_PTE_MASK 0x3F1717+#define PASID_PTE_PRESENT 11818+#define PDE_PFN_MASK PAGE_MASK1919+#define PASID_PDE_SHIFT 62020+#define MAX_NR_PASID_BITS 202121+2222+/*2323+ * Domain ID reserved for pasid entries programmed for first-level2424+ * only and pass-through transfer modes.2525+ */2626+#define FLPT_DEFAULT_DID 12727+2828+/*2929+ * The SUPERVISOR_MODE flag indicates a first level translation which3030+ * can be used for access to kernel addresses. It is valid only for3131+ * access to the kernel's static 1:1 mapping of physical memory — not3232+ * to vmalloc or even module mappings.3333+ */3434+#define PASID_FLAG_SUPERVISOR_MODE BIT(0)3535+3636+struct pasid_dir_entry {3737+ u64 val;3838+};15391640struct pasid_entry {1717- u64 val;4141+ u64 val[8];1842};19432044/* The representative of a PASID table */···5834struct pasid_table *intel_pasid_get_table(struct device *dev);5935int intel_pasid_get_dev_max_id(struct device *dev);6036struct pasid_entry *intel_pasid_get_entry(struct device *dev, int pasid);6161-void intel_pasid_clear_entry(struct device *dev, int pasid);3737+int intel_pasid_setup_first_level(struct intel_iommu *iommu,3838+ struct device *dev, pgd_t *pgd,3939+ int pasid, u16 did, int flags);4040+int intel_pasid_setup_second_level(struct intel_iommu *iommu,4141+ struct dmar_domain *domain,4242+ struct device *dev, int pasid);4343+int intel_pasid_setup_pass_through(struct intel_iommu *iommu,4444+ struct dmar_domain *domain,4545+ struct device *dev, int pasid);4646+void intel_pasid_tear_down_entry(struct intel_iommu *iommu,4747+ struct device *dev, int pasid);62486349#endif /* __INTEL_PASID_H */
+61-110
drivers/iommu/intel-svm.c
···29293030#include "intel-pasid.h"31313232-#define PASID_ENTRY_P BIT_ULL(0)3333-#define PASID_ENTRY_FLPM_5LP BIT_ULL(9)3434-#define PASID_ENTRY_SRE BIT_ULL(11)3535-3632static irqreturn_t prq_event_thread(int irq, void *d);3737-3838-struct pasid_state_entry {3939- u64 val;4040-};41334234int intel_svm_init(struct intel_iommu *iommu)4335{4444- struct page *pages;4545- int order;4646-4736 if (cpu_feature_enabled(X86_FEATURE_GBPAGES) &&4837 !cap_fl1gp_support(iommu->cap))4938 return -EINVAL;···4051 if (cpu_feature_enabled(X86_FEATURE_LA57) &&4152 !cap_5lp_support(iommu->cap))4253 return -EINVAL;4343-4444- /* Start at 2 because it's defined as 2^(1+PSS) */4545- iommu->pasid_max = 2 << ecap_pss(iommu->ecap);4646-4747- /* Eventually I'm promised we will get a multi-level PASID table4848- * and it won't have to be physically contiguous. Until then,4949- * limit the size because 8MiB contiguous allocations can be hard5050- * to come by. The limit of 0x20000, which is 1MiB for each of5151- * the PASID and PASID-state tables, is somewhat arbitrary. */5252- if (iommu->pasid_max > 0x20000)5353- iommu->pasid_max = 0x20000;5454-5555- order = get_order(sizeof(struct pasid_entry) * iommu->pasid_max);5656- if (ecap_dis(iommu->ecap)) {5757- /* Just making it explicit... */5858- BUILD_BUG_ON(sizeof(struct pasid_entry) != sizeof(struct pasid_state_entry));5959- pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);6060- if (pages)6161- iommu->pasid_state_table = page_address(pages);6262- else6363- pr_warn("IOMMU: %s: Failed to allocate PASID state table\n",6464- iommu->name);6565- }6666-6767- return 0;6868-}6969-7070-int intel_svm_exit(struct intel_iommu *iommu)7171-{7272- int order = get_order(sizeof(struct pasid_entry) * iommu->pasid_max);7373-7474- if (iommu->pasid_state_table) {7575- free_pages((unsigned long)iommu->pasid_state_table, order);7676- iommu->pasid_state_table = NULL;7777- }78547955 return 0;8056}···117163 * because that's the only option the hardware gives us. Despite118164 * the fact that they are actually only accessible through one. */119165 if (gl)120120- desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |121121- QI_EIOTLB_GRAN(QI_GRAN_ALL_ALL) | QI_EIOTLB_TYPE;166166+ desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |167167+ QI_EIOTLB_DID(sdev->did) |168168+ QI_EIOTLB_GRAN(QI_GRAN_ALL_ALL) |169169+ QI_EIOTLB_TYPE;122170 else123123- desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |124124- QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | QI_EIOTLB_TYPE;125125- desc.high = 0;171171+ desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |172172+ QI_EIOTLB_DID(sdev->did) |173173+ QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |174174+ QI_EIOTLB_TYPE;175175+ desc.qw1 = 0;126176 } else {127177 int mask = ilog2(__roundup_pow_of_two(pages));128178129129- desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |130130- QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) | QI_EIOTLB_TYPE;131131- desc.high = QI_EIOTLB_ADDR(address) | QI_EIOTLB_GL(gl) |132132- QI_EIOTLB_IH(ih) | QI_EIOTLB_AM(mask);179179+ desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |180180+ QI_EIOTLB_DID(sdev->did) |181181+ QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) |182182+ QI_EIOTLB_TYPE;183183+ desc.qw1 = QI_EIOTLB_ADDR(address) |184184+ QI_EIOTLB_GL(gl) |185185+ QI_EIOTLB_IH(ih) |186186+ QI_EIOTLB_AM(mask);133187 }188188+ desc.qw2 = 0;189189+ desc.qw3 = 0;134190 qi_submit_sync(&desc, svm->iommu);135191136192 if (sdev->dev_iotlb) {137137- desc.low = QI_DEV_EIOTLB_PASID(svm->pasid) | QI_DEV_EIOTLB_SID(sdev->sid) |138138- QI_DEV_EIOTLB_QDEP(sdev->qdep) | QI_DEIOTLB_TYPE;193193+ desc.qw0 = QI_DEV_EIOTLB_PASID(svm->pasid) |194194+ QI_DEV_EIOTLB_SID(sdev->sid) |195195+ QI_DEV_EIOTLB_QDEP(sdev->qdep) |196196+ QI_DEIOTLB_TYPE;139197 if (pages == -1) {140140- desc.high = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) | QI_DEV_EIOTLB_SIZE;198198+ desc.qw1 = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) |199199+ QI_DEV_EIOTLB_SIZE;141200 } else if (pages > 1) {142201 /* The least significant zero bit indicates the size. So,143202 * for example, an "address" value of 0x12345f000 will···158191 unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT);159192 unsigned long mask = __rounddown_pow_of_two(address ^ last);160193161161- desc.high = QI_DEV_EIOTLB_ADDR((address & ~mask) | (mask - 1)) | QI_DEV_EIOTLB_SIZE;194194+ desc.qw1 = QI_DEV_EIOTLB_ADDR((address & ~mask) |195195+ (mask - 1)) | QI_DEV_EIOTLB_SIZE;162196 } else {163163- desc.high = QI_DEV_EIOTLB_ADDR(address);197197+ desc.qw1 = QI_DEV_EIOTLB_ADDR(address);164198 }199199+ desc.qw2 = 0;200200+ desc.qw3 = 0;165201 qi_submit_sync(&desc, svm->iommu);166202 }167203}···173203 unsigned long pages, int ih, int gl)174204{175205 struct intel_svm_dev *sdev;176176-177177- /* Try deferred invalidate if available */178178- if (svm->iommu->pasid_state_table &&179179- !cmpxchg64(&svm->iommu->pasid_state_table[svm->pasid].val, 0, 1ULL << 63))180180- return;181206182207 rcu_read_lock();183208 list_for_each_entry_rcu(sdev, &svm->devs, list)···199234 (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0, 0);200235}201236202202-203203-static void intel_flush_pasid_dev(struct intel_svm *svm, struct intel_svm_dev *sdev, int pasid)204204-{205205- struct qi_desc desc;206206-207207- desc.high = 0;208208- desc.low = QI_PC_TYPE | QI_PC_DID(sdev->did) | QI_PC_PASID_SEL | QI_PC_PASID(pasid);209209-210210- qi_submit_sync(&desc, svm->iommu);211211-}212212-213237static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)214238{215239 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);···218264 */219265 rcu_read_lock();220266 list_for_each_entry_rcu(sdev, &svm->devs, list) {221221- intel_pasid_clear_entry(sdev->dev, svm->pasid);222222- intel_flush_pasid_dev(svm, sdev, svm->pasid);267267+ intel_pasid_tear_down_entry(svm->iommu, sdev->dev, svm->pasid);223268 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm);224269 }225270 rcu_read_unlock();···237284int intel_svm_bind_mm(struct device *dev, int *pasid, int flags, struct svm_dev_ops *ops)238285{239286 struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);240240- struct pasid_entry *entry;241287 struct intel_svm_dev *sdev;242288 struct intel_svm *svm = NULL;243289 struct mm_struct *mm = NULL;244244- u64 pasid_entry_val;245290 int pasid_max;246291 int ret;247292···348397 kfree(sdev);349398 goto out;350399 }351351- pasid_entry_val = (u64)__pa(mm->pgd) | PASID_ENTRY_P;352352- } else353353- pasid_entry_val = (u64)__pa(init_mm.pgd) |354354- PASID_ENTRY_P | PASID_ENTRY_SRE;355355- if (cpu_feature_enabled(X86_FEATURE_LA57))356356- pasid_entry_val |= PASID_ENTRY_FLPM_5LP;400400+ }357401358358- entry = intel_pasid_get_entry(dev, svm->pasid);359359- entry->val = pasid_entry_val;360360-361361- wmb();362362-363363- /*364364- * Flush PASID cache when a PASID table entry becomes365365- * present.366366- */367367- if (cap_caching_mode(iommu->cap))368368- intel_flush_pasid_dev(svm, sdev, svm->pasid);402402+ spin_lock(&iommu->lock);403403+ ret = intel_pasid_setup_first_level(iommu, dev,404404+ mm ? mm->pgd : init_mm.pgd,405405+ svm->pasid, FLPT_DEFAULT_DID,406406+ mm ? 0 : PASID_FLAG_SUPERVISOR_MODE);407407+ spin_unlock(&iommu->lock);408408+ if (ret) {409409+ if (mm)410410+ mmu_notifier_unregister(&svm->notifier, mm);411411+ intel_pasid_free_id(svm->pasid);412412+ kfree(svm);413413+ kfree(sdev);414414+ goto out;415415+ }369416370417 list_add_tail(&svm->list, &global_svm_list);371418 }···409460 * to use. We have a *shared* PASID table, because it's410461 * large and has to be physically contiguous. So it's411462 * hard to be as defensive as we might like. */412412- intel_flush_pasid_dev(svm, sdev, svm->pasid);463463+ intel_pasid_tear_down_entry(iommu, dev, svm->pasid);413464 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm);414465 kfree_rcu(sdev, rcu);415415- intel_pasid_clear_entry(dev, svm->pasid);416466417467 if (list_empty(&svm->devs)) {418468 intel_pasid_free_id(svm->pasid);···619671 no_pasid:620672 if (req->lpig) {621673 /* Page Group Response */622622- resp.low = QI_PGRP_PASID(req->pasid) |674674+ resp.qw0 = QI_PGRP_PASID(req->pasid) |623675 QI_PGRP_DID((req->bus << 8) | req->devfn) |624676 QI_PGRP_PASID_P(req->pasid_present) |625677 QI_PGRP_RESP_TYPE;626626- resp.high = QI_PGRP_IDX(req->prg_index) |627627- QI_PGRP_PRIV(req->private) | QI_PGRP_RESP_CODE(result);628628-629629- qi_submit_sync(&resp, iommu);678678+ resp.qw1 = QI_PGRP_IDX(req->prg_index) |679679+ QI_PGRP_PRIV(req->private) |680680+ QI_PGRP_RESP_CODE(result);630681 } else if (req->srr) {631682 /* Page Stream Response */632632- resp.low = QI_PSTRM_IDX(req->prg_index) |633633- QI_PSTRM_PRIV(req->private) | QI_PSTRM_BUS(req->bus) |634634- QI_PSTRM_PASID(req->pasid) | QI_PSTRM_RESP_TYPE;635635- resp.high = QI_PSTRM_ADDR(address) | QI_PSTRM_DEVFN(req->devfn) |683683+ resp.qw0 = QI_PSTRM_IDX(req->prg_index) |684684+ QI_PSTRM_PRIV(req->private) |685685+ QI_PSTRM_BUS(req->bus) |686686+ QI_PSTRM_PASID(req->pasid) |687687+ QI_PSTRM_RESP_TYPE;688688+ resp.qw1 = QI_PSTRM_ADDR(address) |689689+ QI_PSTRM_DEVFN(req->devfn) |636690 QI_PSTRM_RESP_CODE(result);637637-638638- qi_submit_sync(&resp, iommu);639691 }692692+ resp.qw2 = 0;693693+ resp.qw3 = 0;694694+ qi_submit_sync(&resp, iommu);640695641696 head = (head + sizeof(*req)) & PRQ_RING_MASK;642697 }
···11/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.22 *33+ * Author: Stepan Moskovchenko <stepanm@codeaurora.org>44+ *35 * This program is free software; you can redistribute it and/or modify46 * it under the terms of the GNU General Public License version 2 and57 * only version 2 as published by the Free Software Foundation.···19172018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt2119#include <linux/kernel.h>2222-#include <linux/module.h>2020+#include <linux/init.h>2321#include <linux/platform_device.h>2422#include <linux/errno.h>2523#include <linux/io.h>···863861864862 return ret;865863}866866-867867-static void __exit msm_iommu_driver_exit(void)868868-{869869- platform_driver_unregister(&msm_iommu_driver);870870-}871871-872864subsys_initcall(msm_iommu_driver_init);873873-module_exit(msm_iommu_driver_exit);874865875875-MODULE_LICENSE("GPL v2");876876-MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");
+14-11
drivers/iommu/mtk_iommu.c
···113113 struct iommu_domain domain;114114};115115116116-static struct iommu_ops mtk_iommu_ops;116116+static const struct iommu_ops mtk_iommu_ops;117117118118static LIST_HEAD(m4ulist); /* List all the M4U HWs */119119···244244{245245 struct mtk_smi_larb_iommu *larb_mmu;246246 unsigned int larbid, portid;247247- struct iommu_fwspec *fwspec = dev->iommu_fwspec;247247+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);248248 int i;249249250250 for (i = 0; i < fwspec->num_ids; ++i) {···336336 struct device *dev)337337{338338 struct mtk_iommu_domain *dom = to_mtk_domain(domain);339339- struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv;339339+ struct mtk_iommu_data *data = dev_iommu_fwspec_get(dev)->iommu_priv;340340341341 if (!data)342342 return -ENODEV;···355355static void mtk_iommu_detach_device(struct iommu_domain *domain,356356 struct device *dev)357357{358358- struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv;358358+ struct mtk_iommu_data *data = dev_iommu_fwspec_get(dev)->iommu_priv;359359360360 if (!data)361361 return;···417417418418static int mtk_iommu_add_device(struct device *dev)419419{420420+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);420421 struct mtk_iommu_data *data;421422 struct iommu_group *group;422423423423- if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops)424424+ if (!fwspec || fwspec->ops != &mtk_iommu_ops)424425 return -ENODEV; /* Not a iommu client device */425426426426- data = dev->iommu_fwspec->iommu_priv;427427+ data = fwspec->iommu_priv;427428 iommu_device_link(&data->iommu, dev);428429429430 group = iommu_group_get_for_dev(dev);···437436438437static void mtk_iommu_remove_device(struct device *dev)439438{439439+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);440440 struct mtk_iommu_data *data;441441442442- if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops)442442+ if (!fwspec || fwspec->ops != &mtk_iommu_ops)443443 return;444444445445- data = dev->iommu_fwspec->iommu_priv;445445+ data = fwspec->iommu_priv;446446 iommu_device_unlink(&data->iommu, dev);447447448448 iommu_group_remove_device(dev);···470468471469static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)472470{471471+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);473472 struct platform_device *m4updev;474473475474 if (args->args_count != 1) {···479476 return -EINVAL;480477 }481478482482- if (!dev->iommu_fwspec->iommu_priv) {479479+ if (!fwspec->iommu_priv) {483480 /* Get the m4u device */484481 m4updev = of_find_device_by_node(args->np);485482 if (WARN_ON(!m4updev))486483 return -EINVAL;487484488488- dev->iommu_fwspec->iommu_priv = platform_get_drvdata(m4updev);485485+ fwspec->iommu_priv = platform_get_drvdata(m4updev);489486 }490487491488 return iommu_fwspec_add_ids(dev, args->args, 1);492489}493490494494-static struct iommu_ops mtk_iommu_ops = {491491+static const struct iommu_ops mtk_iommu_ops = {495492 .domain_alloc = mtk_iommu_domain_alloc,496493 .domain_free = mtk_iommu_domain_free,497494 .attach_dev = mtk_iommu_attach_device,
+21-26
drivers/iommu/mtk_iommu_v1.c
···11/*22+ * IOMMU API for MTK architected m4u v1 implementations33+ *24 * Copyright (c) 2015-2016 MediaTek Inc.35 * Author: Honghui Zhang <honghui.zhang@mediatek.com>46 *···3735#include <linux/spinlock.h>3836#include <asm/barrier.h>3937#include <asm/dma-iommu.h>4040-#include <linux/module.h>3838+#include <linux/init.h>4139#include <dt-bindings/memory/mt2701-larb-port.h>4240#include <soc/mediatek/smi.h>4341#include "mtk_iommu.h"···208206{209207 struct mtk_smi_larb_iommu *larb_mmu;210208 unsigned int larbid, portid;211211- struct iommu_fwspec *fwspec = dev->iommu_fwspec;209209+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);212210 int i;213211214212 for (i = 0; i < fwspec->num_ids; ++i) {···273271 struct device *dev)274272{275273 struct mtk_iommu_domain *dom = to_mtk_domain(domain);276276- struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv;274274+ struct mtk_iommu_data *data = dev_iommu_fwspec_get(dev)->iommu_priv;277275 int ret;278276279277 if (!data)···295293static void mtk_iommu_detach_device(struct iommu_domain *domain,296294 struct device *dev)297295{298298- struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv;296296+ struct mtk_iommu_data *data = dev_iommu_fwspec_get(dev)->iommu_priv;299297300298 if (!data)301299 return;···364362 return pa;365363}366364367367-static struct iommu_ops mtk_iommu_ops;365365+static const struct iommu_ops mtk_iommu_ops;368366369367/*370368 * MTK generation one iommu HW only support one iommu domain, and all the client···373371static int mtk_iommu_create_mapping(struct device *dev,374372 struct of_phandle_args *args)375373{374374+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);376375 struct mtk_iommu_data *data;377376 struct platform_device *m4updev;378377 struct dma_iommu_mapping *mtk_mapping;···386383 return -EINVAL;387384 }388385389389- if (!dev->iommu_fwspec) {386386+ if (!fwspec) {390387 ret = iommu_fwspec_init(dev, &args->np->fwnode, &mtk_iommu_ops);391388 if (ret)392389 return ret;393393- } else if (dev->iommu_fwspec->ops != &mtk_iommu_ops) {390390+ fwspec = dev_iommu_fwspec_get(dev);391391+ } else if (dev_iommu_fwspec_get(dev)->ops != &mtk_iommu_ops) {394392 return -EINVAL;395393 }396394397397- if (!dev->iommu_fwspec->iommu_priv) {395395+ if (!fwspec->iommu_priv) {398396 /* Get the m4u device */399397 m4updev = of_find_device_by_node(args->np);400398 if (WARN_ON(!m4updev))401399 return -EINVAL;402400403403- dev->iommu_fwspec->iommu_priv = platform_get_drvdata(m4updev);401401+ fwspec->iommu_priv = platform_get_drvdata(m4updev);404402 }405403406404 ret = iommu_fwspec_add_ids(dev, args->args, 1);407405 if (ret)408406 return ret;409407410410- data = dev->iommu_fwspec->iommu_priv;408408+ data = fwspec->iommu_priv;411409 m4udev = data->dev;412410 mtk_mapping = m4udev->archdata.iommu;413411 if (!mtk_mapping) {···426422427423static int mtk_iommu_add_device(struct device *dev)428424{425425+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);429426 struct dma_iommu_mapping *mtk_mapping;430427 struct of_phandle_args iommu_spec;431428 struct of_phandle_iterator it;···445440 of_node_put(iommu_spec.np);446441 }447442448448- if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops)443443+ if (!fwspec || fwspec->ops != &mtk_iommu_ops)449444 return -ENODEV; /* Not a iommu client device */450445451446 /*···463458 if (err)464459 return err;465460466466- data = dev->iommu_fwspec->iommu_priv;461461+ data = fwspec->iommu_priv;467462 mtk_mapping = data->dev->archdata.iommu;468463 err = arm_iommu_attach_device(dev, mtk_mapping);469464 if (err) {···476471477472static void mtk_iommu_remove_device(struct device *dev)478473{474474+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);479475 struct mtk_iommu_data *data;480476481481- if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops)477477+ if (!fwspec || fwspec->ops != &mtk_iommu_ops)482478 return;483479484484- data = dev->iommu_fwspec->iommu_priv;480480+ data = fwspec->iommu_priv;485481 iommu_device_unlink(&data->iommu, dev);486482487483 iommu_group_remove_device(dev);···530524 return 0;531525}532526533533-static struct iommu_ops mtk_iommu_ops = {527527+static const struct iommu_ops mtk_iommu_ops = {534528 .domain_alloc = mtk_iommu_domain_alloc,535529 .domain_free = mtk_iommu_domain_free,536530 .attach_dev = mtk_iommu_attach_device,···710704{711705 return platform_driver_register(&mtk_iommu_driver);712706}713713-714714-static void __exit m4u_exit(void)715715-{716716- return platform_driver_unregister(&mtk_iommu_driver);717717-}718718-719707subsys_initcall(m4u_init);720720-module_exit(m4u_exit);721721-722722-MODULE_DESCRIPTION("IOMMU API for MTK architected m4u v1 implementations");723723-MODULE_AUTHOR("Honghui Zhang <honghui.zhang@mediatek.com>");724724-MODULE_LICENSE("GPL v2");
+10-6
drivers/iommu/of_iommu.c
···164164 struct device_node *master_np)165165{166166 const struct iommu_ops *ops = NULL;167167- struct iommu_fwspec *fwspec = dev->iommu_fwspec;167167+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);168168 int err = NO_IOMMU;169169170170 if (!master_np)···208208 }209209 }210210211211+211212 /*212213 * Two success conditions can be represented by non-negative err here:213214 * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons214215 * 0 : we found an IOMMU, and dev->fwspec is initialised appropriately215216 * <0 : any actual error216217 */217217- if (!err)218218- ops = dev->iommu_fwspec->ops;218218+ if (!err) {219219+ /* The fwspec pointer changed, read it again */220220+ fwspec = dev_iommu_fwspec_get(dev);221221+ ops = fwspec->ops;222222+ }219223 /*220224 * If we have reason to believe the IOMMU driver missed the initial221221- * add_device callback for dev, replay it to get things in order.225225+ * probe for dev, replay it to get things in order.222226 */223223- if (ops && ops->add_device && dev->bus && !dev->iommu_group)224224- err = ops->add_device(dev);227227+ if (dev->bus && !device_iommu_mapped(dev))228228+ err = iommu_probe_device(dev);225229226230 /* Ignore all other errors apart from EPROBE_DEFER */227231 if (err == -EPROBE_DEFER) {
···11/*22+ * IOMMU API for Rockchip33+ *44+ * Module Authors: Simon Xue <xxm@rock-chips.com>55+ * Daniel Kurtz <djkurtz@chromium.org>66+ *27 * This program is free software; you can redistribute it and/or modify38 * it under the terms of the GNU General Public License version 2 as49 * published by the Free Software Foundation.···2217#include <linux/iopoll.h>2318#include <linux/list.h>2419#include <linux/mm.h>2525-#include <linux/module.h>2020+#include <linux/init.h>2621#include <linux/of.h>2722#include <linux/of_iommu.h>2823#include <linux/of_platform.h>···12861281 { .compatible = "rockchip,iommu" },12871282 { /* sentinel */ }12881283};12891289-MODULE_DEVICE_TABLE(of, rk_iommu_dt_ids);1290128412911285static struct platform_driver rk_iommu_driver = {12921286 .probe = rk_iommu_probe,···13031299 return platform_driver_register(&rk_iommu_driver);13041300}13051301subsys_initcall(rk_iommu_init);13061306-13071307-MODULE_DESCRIPTION("IOMMU API for Rockchip");13081308-MODULE_AUTHOR("Simon Xue <xxm@rock-chips.com> and Daniel Kurtz <djkurtz@chromium.org>");13091309-MODULE_ALIAS("platform:rockchip-iommu");13101310-MODULE_LICENSE("GPL v2");
+7-30
drivers/iommu/tegra-gart.c
···33 *44 * Copyright (c) 2010-2012, NVIDIA CORPORATION. All rights reserved.55 *66+ * Author: Hiroshi DOYU <hdoyu@nvidia.com>77+ *68 * This program is free software; you can redistribute it and/or modify it79 * under the terms and conditions of the GNU General Public License,810 * version 2, as published by the Free Software Foundation.···21192220#define pr_fmt(fmt) "%s(): " fmt, __func__23212424-#include <linux/module.h>2222+#include <linux/init.h>2323+#include <linux/moduleparam.h>2524#include <linux/platform_device.h>2625#include <linux/spinlock.h>2726#include <linux/slab.h>···481478 return 0;482479}483480484484-static int tegra_gart_remove(struct platform_device *pdev)485485-{486486- struct gart_device *gart = platform_get_drvdata(pdev);487487-488488- iommu_device_unregister(&gart->iommu);489489- iommu_device_sysfs_remove(&gart->iommu);490490-491491- writel(0, gart->regs + GART_CONFIG);492492- if (gart->savedata)493493- vfree(gart->savedata);494494- gart_handle = NULL;495495- return 0;496496-}497497-498481static const struct dev_pm_ops tegra_gart_pm_ops = {499482 .suspend = tegra_gart_suspend,500483 .resume = tegra_gart_resume,···490501 { .compatible = "nvidia,tegra20-gart", },491502 { },492503};493493-MODULE_DEVICE_TABLE(of, tegra_gart_of_match);494504495505static struct platform_driver tegra_gart_driver = {496506 .probe = tegra_gart_probe,497497- .remove = tegra_gart_remove,498507 .driver = {499508 .name = "tegra-gart",500509 .pm = &tegra_gart_pm_ops,501510 .of_match_table = tegra_gart_of_match,511511+ .suppress_bind_attrs = true,502512 },503513};504514505505-static int tegra_gart_init(void)515515+static int __init tegra_gart_init(void)506516{507517 return platform_driver_register(&tegra_gart_driver);508518}509509-510510-static void __exit tegra_gart_exit(void)511511-{512512- platform_driver_unregister(&tegra_gart_driver);513513-}514514-515519subsys_initcall(tegra_gart_init);516516-module_exit(tegra_gart_exit);517517-module_param(gart_debug, bool, 0644);518520521521+module_param(gart_debug, bool, 0644);519522MODULE_PARM_DESC(gart_debug, "Enable GART debugging");520520-MODULE_DESCRIPTION("IOMMU API for GART in Tegra20");521521-MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");522522-MODULE_ALIAS("platform:tegra-gart");523523-MODULE_LICENSE("GPL v2");
···244244 * an iommu. Doing anything when there is no iommu is definitely245245 * unsafe...246246 */247247- if (!(xhci->quirks & XHCI_ZERO_64B_REGS) || !dev->iommu_group)247247+ if (!(xhci->quirks & XHCI_ZERO_64B_REGS) || !device_iommu_mapped(dev))248248 return;249249250250 xhci_info(xhci, "Zeroing 64bit base registers, expecting fault\n");
+2-31
drivers/vfio/vfio_iommu_type1.c
···978978 return ret;979979}980980981981-/*982982- * Turns out AMD IOMMU has a page table bug where it won't map large pages983983- * to a region that previously mapped smaller pages. This should be fixed984984- * soon, so this is just a temporary workaround to break mappings down into985985- * PAGE_SIZE. Better to map smaller pages than nothing.986986- */987987-static int map_try_harder(struct vfio_domain *domain, dma_addr_t iova,988988- unsigned long pfn, long npage, int prot)989989-{990990- long i;991991- int ret = 0;992992-993993- for (i = 0; i < npage; i++, pfn++, iova += PAGE_SIZE) {994994- ret = iommu_map(domain->domain, iova,995995- (phys_addr_t)pfn << PAGE_SHIFT,996996- PAGE_SIZE, prot | domain->prot);997997- if (ret)998998- break;999999- }10001000-10011001- for (; i < npage && i > 0; i--, iova -= PAGE_SIZE)10021002- iommu_unmap(domain->domain, iova, PAGE_SIZE);10031003-10041004- return ret;10051005-}10061006-1007981static int vfio_iommu_map(struct vfio_iommu *iommu, dma_addr_t iova,1008982 unsigned long pfn, long npage, int prot)1009983{···9871013 list_for_each_entry(d, &iommu->domain_list, next) {9881014 ret = iommu_map(d->domain, iova, (phys_addr_t)pfn << PAGE_SHIFT,9891015 npage << PAGE_SHIFT, prot | d->prot);990990- if (ret) {991991- if (ret != -EBUSY ||992992- map_try_harder(d, iova, pfn, npage, prot))993993- goto unwind;994994- }10161016+ if (ret)10171017+ goto unwind;99510189961019 cond_resched();9971020 }
+10
include/linux/device.h
···10581058 return container_of(kobj, struct device, kobj);10591059}1060106010611061+/**10621062+ * device_iommu_mapped - Returns true when the device DMA is translated10631063+ * by an IOMMU10641064+ * @dev: Device to perform the check on10651065+ */10661066+static inline bool device_iommu_mapped(struct device *dev)10671067+{10681068+ return (dev->iommu_group != NULL);10691069+}10701070+10611071/* Get the wakeup routines, which depend on struct device */10621072#include <linux/pm_wakeup.h>10631073
···2626#include <linux/iova.h>2727#include <linux/io.h>2828#include <linux/idr.h>2929-#include <linux/dma_remapping.h>3029#include <linux/mmu_notifier.h>3130#include <linux/list.h>3231#include <linux/iommu.h>···3637#include <asm/iommu.h>37383839/*4040+ * VT-d hardware uses 4KiB page size regardless of host page size.4141+ */4242+#define VTD_PAGE_SHIFT (12)4343+#define VTD_PAGE_SIZE (1UL << VTD_PAGE_SHIFT)4444+#define VTD_PAGE_MASK (((u64)-1) << VTD_PAGE_SHIFT)4545+#define VTD_PAGE_ALIGN(addr) (((addr) + VTD_PAGE_SIZE - 1) & VTD_PAGE_MASK)4646+4747+#define VTD_STRIDE_SHIFT (9)4848+#define VTD_STRIDE_MASK (((u64)-1) << VTD_STRIDE_SHIFT)4949+5050+#define DMA_PTE_READ (1)5151+#define DMA_PTE_WRITE (2)5252+#define DMA_PTE_LARGE_PAGE (1 << 7)5353+#define DMA_PTE_SNP (1 << 11)5454+5555+#define CONTEXT_TT_MULTI_LEVEL 05656+#define CONTEXT_TT_DEV_IOTLB 15757+#define CONTEXT_TT_PASS_THROUGH 25858+#define CONTEXT_PASIDE BIT_ULL(3)5959+6060+/*3961 * Intel IOMMU register specification per version 1.0 public spec.4062 */4141-4263#define DMAR_VER_REG 0x0 /* Arch version supported by this IOMMU */4364#define DMAR_CAP_REG 0x8 /* Hardware supported capabilities */4465#define DMAR_ECAP_REG 0x10 /* Extended capabilities supported */···170151 * Extended Capability Register171152 */172153154154+#define ecap_smpwc(e) (((e) >> 48) & 0x1)155155+#define ecap_flts(e) (((e) >> 47) & 0x1)156156+#define ecap_slts(e) (((e) >> 46) & 0x1)157157+#define ecap_smts(e) (((e) >> 43) & 0x1)173158#define ecap_dit(e) ((e >> 41) & 0x1)174159#define ecap_pasid(e) ((e >> 40) & 0x1)175160#define ecap_pss(e) ((e >> 35) & 0x1f)···252229253230/* DMA_RTADDR_REG */254231#define DMA_RTADDR_RTT (((u64)1) << 11)232232+#define DMA_RTADDR_SMT (((u64)1) << 10)255233256234/* CCMD_REG */257235#define DMA_CCMD_ICC (((u64)1) << 63)···398374#define QI_GRAN_NONG_PASID 2399375#define QI_GRAN_PSI_PASID 3400376377377+#define qi_shift(iommu) (DMAR_IQ_SHIFT + !!ecap_smts((iommu)->ecap))378378+401379struct qi_desc {402402- u64 low, high;380380+ u64 qw0;381381+ u64 qw1;382382+ u64 qw2;383383+ u64 qw3;403384};404385405386struct q_inval {406387 raw_spinlock_t q_lock;407407- struct qi_desc *desc; /* invalidation queue */388388+ void *desc; /* invalidation queue */408389 int *desc_status; /* desc status */409390 int free_head; /* first free entry */410391 int free_tail; /* last free entry */···541512 struct iommu_flush flush;542513#endif543514#ifdef CONFIG_INTEL_IOMMU_SVM544544- /* These are large and need to be contiguous, so we allocate just545545- * one for now. We'll maybe want to rethink that if we truly give546546- * devices away to userspace processes (e.g. for DPDK) and don't547547- * want to trust that userspace will use *only* the PASID it was548548- * told to. But while it's all driver-arbitrated, we're fine. */549549- struct pasid_state_entry *pasid_state_table;550515 struct page_req_dsc *prq;551516 unsigned char prq_name[16]; /* Name for PRQ interrupt */552552- u32 pasid_max;553517#endif554518 struct q_inval *qi; /* Queued invalidation info */555519 u32 *iommu_state; /* Store iommu states between suspend and resume.*/···585563 clflush_cache_range(addr, size);586564}587565566566+/*567567+ * 0: readable568568+ * 1: writable569569+ * 2-6: reserved570570+ * 7: super page571571+ * 8-10: available572572+ * 11: snoop behavior573573+ * 12-63: Host physcial address574574+ */575575+struct dma_pte {576576+ u64 val;577577+};578578+579579+static inline void dma_clear_pte(struct dma_pte *pte)580580+{581581+ pte->val = 0;582582+}583583+584584+static inline u64 dma_pte_addr(struct dma_pte *pte)585585+{586586+#ifdef CONFIG_64BIT587587+ return pte->val & VTD_PAGE_MASK;588588+#else589589+ /* Must have a full atomic 64-bit read */590590+ return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;591591+#endif592592+}593593+594594+static inline bool dma_pte_present(struct dma_pte *pte)595595+{596596+ return (pte->val & 3) != 0;597597+}598598+599599+static inline bool dma_pte_superpage(struct dma_pte *pte)600600+{601601+ return (pte->val & DMA_PTE_LARGE_PAGE);602602+}603603+604604+static inline int first_pte_in_page(struct dma_pte *pte)605605+{606606+ return !((unsigned long)pte & ~VTD_PAGE_MASK);607607+}608608+588609extern struct dmar_drhd_unit * dmar_find_matched_drhd_unit(struct pci_dev *dev);589610extern int dmar_find_matched_atsr_unit(struct pci_dev *dev);590611···652587struct intel_iommu *domain_get_iommu(struct dmar_domain *domain);653588int for_each_device_domain(int (*fn)(struct device_domain_info *info,654589 void *data), void *data);590590+void iommu_flush_write_buffer(struct intel_iommu *iommu);655591656592#ifdef CONFIG_INTEL_IOMMU_SVM657593int intel_svm_init(struct intel_iommu *iommu);658658-int intel_svm_exit(struct intel_iommu *iommu);659594extern int intel_svm_enable_prq(struct intel_iommu *iommu);660595extern int intel_svm_finish_prq(struct intel_iommu *iommu);661596···696631bool context_present(struct context_entry *context);697632struct context_entry *iommu_context_addr(struct intel_iommu *iommu, u8 bus,698633 u8 devfn, int alloc);634634+635635+#ifdef CONFIG_INTEL_IOMMU636636+extern int iommu_calculate_agaw(struct intel_iommu *iommu);637637+extern int iommu_calculate_max_sagaw(struct intel_iommu *iommu);638638+extern int dmar_disabled;639639+extern int intel_iommu_enabled;640640+extern int intel_iommu_tboot_noforce;641641+#else642642+static inline int iommu_calculate_agaw(struct intel_iommu *iommu)643643+{644644+ return 0;645645+}646646+static inline int iommu_calculate_max_sagaw(struct intel_iommu *iommu)647647+{648648+ return 0;649649+}650650+#define dmar_disabled (1)651651+#define intel_iommu_enabled (0)652652+#endif699653700654#endif
+16-2
include/linux/iommu.h
···168168 * @map: map a physically contiguous memory region to an iommu domain169169 * @unmap: unmap a physically contiguous memory region from an iommu domain170170 * @flush_tlb_all: Synchronously flush all hardware TLBs for this domain171171- * @tlb_range_add: Add a given iova range to the flush queue for this domain172172- * @tlb_sync: Flush all queued ranges from the hardware TLBs and empty flush171171+ * @iotlb_range_add: Add a given iova range to the flush queue for this domain172172+ * @iotlb_sync: Flush all queued ranges from the hardware TLBs and empty flush173173 * queue174174 * @iova_to_phys: translate iova to physical address175175 * @add_device: add device to iommu grouping···397397void iommu_fwspec_free(struct device *dev);398398int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids);399399const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode);400400+401401+static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev)402402+{403403+ return dev->iommu_fwspec;404404+}405405+406406+static inline void dev_iommu_fwspec_set(struct device *dev,407407+ struct iommu_fwspec *fwspec)408408+{409409+ dev->iommu_fwspec = fwspec;410410+}411411+412412+int iommu_probe_device(struct device *dev);413413+void iommu_release_device(struct device *dev);400414401415#else /* CONFIG_IOMMU_API */402416