···15811581 * If we have reason to believe the IOMMU driver missed the initial15821582 * iommu_probe_device() call for dev, replay it to get things in order.15831583 */15841584- if (!err && dev->bus && !device_iommu_mapped(dev))15841584+ if (!err && dev->bus)15851585 err = iommu_probe_device(dev);1586158615871587 /* Ignore all other errors apart from EPROBE_DEFER */
+14-25
drivers/dma/idxd/device.c
···299299 }300300}301301302302-static void __idxd_wq_set_priv_locked(struct idxd_wq *wq, int priv)303303-{304304- struct idxd_device *idxd = wq->idxd;305305- union wqcfg wqcfg;306306- unsigned int offset;307307-308308- offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PRIVL_IDX);309309- spin_lock(&idxd->dev_lock);310310- wqcfg.bits[WQCFG_PRIVL_IDX] = ioread32(idxd->reg_base + offset);311311- wqcfg.priv = priv;312312- wq->wqcfg->bits[WQCFG_PRIVL_IDX] = wqcfg.bits[WQCFG_PRIVL_IDX];313313- iowrite32(wqcfg.bits[WQCFG_PRIVL_IDX], idxd->reg_base + offset);314314- spin_unlock(&idxd->dev_lock);315315-}316316-317302static void __idxd_wq_set_pasid_locked(struct idxd_wq *wq, int pasid)318303{319304 struct idxd_device *idxd = wq->idxd;···14061421 }1407142214081423 /*14091409- * In the event that the WQ is configurable for pasid and priv bits.14101410- * For kernel wq, the driver should setup the pasid, pasid_en, and priv bit.14111411- * However, for non-kernel wq, the driver should only set the pasid_en bit for14121412- * shared wq. A dedicated wq that is not 'kernel' type will configure pasid and14241424+ * In the event that the WQ is configurable for pasid, the driver14251425+ * should setup the pasid, pasid_en bit. This is true for both kernel14261426+ * and user shared workqueues. There is no need to setup priv bit in14271427+ * that in-kernel DMA will also do user privileged requests.14281428+ * A dedicated wq that is not 'kernel' type will configure pasid and14131429 * pasid_en later on so there is no need to setup.14141430 */14151431 if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {14161416- int priv = 0;14171417-14181432 if (wq_pasid_enabled(wq)) {14191433 if (is_idxd_wq_kernel(wq) || wq_shared(wq)) {14201434 u32 pasid = wq_dedicated(wq) ? idxd->pasid : 0;···14211437 __idxd_wq_set_pasid_locked(wq, pasid);14221438 }14231439 }14241424-14251425- if (is_idxd_wq_kernel(wq))14261426- priv = 1;14271427- __idxd_wq_set_priv_locked(wq, priv);14281440 }1429144114301442 rc = 0;···15271547 spin_unlock(&idxd->dev_lock);15281548 if (rc < 0)15291549 return -ENXIO;15501550+15511551+ /*15521552+ * System PASID is preserved across device disable/enable cycle, but15531553+ * genconfig register content gets cleared during device reset. We15541554+ * need to re-enable user interrupts for kernel work queue completion15551555+ * IRQ to function.15561556+ */15571557+ if (idxd->pasid != IOMMU_PASID_INVALID)15581558+ idxd_set_user_intr(idxd, 1);1530155915311560 rc = idxd_device_evl_setup(idxd);15321561 if (rc < 0) {
+3-2
drivers/dma/idxd/dma.c
···7575 hw->xfer_size = len;7676 /*7777 * For dedicated WQ, this field is ignored and HW will use the WQCFG.priv7878- * field instead. This field should be set to 1 for kernel descriptors.7878+ * field instead. This field should be set to 0 for kernel descriptors7979+ * since kernel DMA on VT-d supports "user" privilege only.7980 */8080- hw->priv = 1;8181+ hw->priv = 0;8182 hw->completion_addr = compl;8283}8384
···550550551551static int idxd_enable_system_pasid(struct idxd_device *idxd)552552{553553- return -EOPNOTSUPP;553553+ struct pci_dev *pdev = idxd->pdev;554554+ struct device *dev = &pdev->dev;555555+ struct iommu_domain *domain;556556+ ioasid_t pasid;557557+ int ret;558558+559559+ /*560560+ * Attach a global PASID to the DMA domain so that we can use ENQCMDS561561+ * to submit work on buffers mapped by DMA API.562562+ */563563+ domain = iommu_get_domain_for_dev(dev);564564+ if (!domain)565565+ return -EPERM;566566+567567+ pasid = iommu_alloc_global_pasid(dev);568568+ if (pasid == IOMMU_PASID_INVALID)569569+ return -ENOSPC;570570+571571+ /*572572+ * DMA domain is owned by the driver, it should support all valid573573+ * types such as DMA-FQ, identity, etc.574574+ */575575+ ret = iommu_attach_device_pasid(domain, dev, pasid);576576+ if (ret) {577577+ dev_err(dev, "failed to attach device pasid %d, domain type %d",578578+ pasid, domain->type);579579+ iommu_free_global_pasid(pasid);580580+ return ret;581581+ }582582+583583+ /* Since we set user privilege for kernel DMA, enable completion IRQ */584584+ idxd_set_user_intr(idxd, 1);585585+ idxd->pasid = pasid;586586+587587+ return ret;554588}555589556590static void idxd_disable_system_pasid(struct idxd_device *idxd)557591{592592+ struct pci_dev *pdev = idxd->pdev;593593+ struct device *dev = &pdev->dev;594594+ struct iommu_domain *domain;558595559559- iommu_sva_unbind_device(idxd->sva);596596+ domain = iommu_get_domain_for_dev(dev);597597+ if (!domain)598598+ return;599599+600600+ iommu_detach_device_pasid(domain, dev, idxd->pasid);601601+ iommu_free_global_pasid(idxd->pasid);602602+603603+ idxd_set_user_intr(idxd, 0);560604 idxd->sva = NULL;605605+ idxd->pasid = IOMMU_PASID_INVALID;561606}562607563608static int idxd_enable_sva(struct pci_dev *pdev)···645600 } else {646601 set_bit(IDXD_FLAG_USER_PASID_ENABLED, &idxd->flags);647602648648- if (idxd_enable_system_pasid(idxd))649649- dev_warn(dev, "No in-kernel DMA with PASID.\n");603603+ rc = idxd_enable_system_pasid(idxd);604604+ if (rc)605605+ dev_warn(dev, "No in-kernel DMA with PASID. %d\n", rc);650606 else651607 set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);652608 }
-7
drivers/dma/idxd/sysfs.c
···948948 if (strlen(buf) > WQ_NAME_SIZE || strlen(buf) == 0)949949 return -EINVAL;950950951951- /*952952- * This is temporarily placed here until we have SVM support for953953- * dmaengine.954954- */955955- if (wq->type == IDXD_WQT_KERNEL && device_pasid_enabled(wq->idxd))956956- return -EOPNOTSUPP;957957-958951 input = kstrndup(buf, count, GFP_KERNEL);959952 if (!input)960953 return -ENOMEM;
···262262263263static void put_pasid_state_wait(struct pasid_state *pasid_state)264264{265265- refcount_dec(&pasid_state->count);266266- wait_event(pasid_state->wq, !refcount_read(&pasid_state->count));265265+ if (!refcount_dec_and_test(&pasid_state->count))266266+ wait_event(pasid_state->wq, !refcount_read(&pasid_state->count));267267 free_pasid_state(pasid_state);268268}269269···326326 continue;327327328328 put_pasid_state(pasid_state);329329+330330+ /* Clear the pasid state so that the pasid can be re-used */331331+ clear_pasid_state(dev_state, pasid_state->pasid);329332330333 /*331334 * This will call the mn_release function and
···8080 * be some overlap between use of both ASIDs, until we invalidate the8181 * TLB.8282 */8383- arm_smmu_write_ctx_desc(smmu_domain, 0, cd);8383+ arm_smmu_write_ctx_desc(smmu_domain, IOMMU_NO_PASID, cd);84848585 /* Invalidate TLB entries previously associated with that context */8686 arm_smmu_tlb_inv_asid(smmu, asid);
+15-30
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
···10591059 /*10601060 * This function handles the following cases:10611061 *10621062- * (1) Install primary CD, for normal DMA traffic (SSID = 0).10621062+ * (1) Install primary CD, for normal DMA traffic (SSID = IOMMU_NO_PASID = 0).10631063 * (2) Install a secondary CD, for SID+SSID traffic.10641064 * (3) Update ASID of a CD. Atomically write the first 64 bits of the10651065 * CD, then invalidate the old entry and mappings.···1607160716081608 sid = FIELD_GET(PRIQ_0_SID, evt[0]);16091609 ssv = FIELD_GET(PRIQ_0_SSID_V, evt[0]);16101610- ssid = ssv ? FIELD_GET(PRIQ_0_SSID, evt[0]) : 0;16101610+ ssid = ssv ? FIELD_GET(PRIQ_0_SSID, evt[0]) : IOMMU_NO_PASID;16111611 last = FIELD_GET(PRIQ_0_PRG_LAST, evt[0]);16121612 grpid = FIELD_GET(PRIQ_1_PRG_IDX, evt[1]);16131613···17481748 */17491749 *cmd = (struct arm_smmu_cmdq_ent) {17501750 .opcode = CMDQ_OP_ATC_INV,17511751- .substream_valid = !!ssid,17511751+ .substream_valid = (ssid != IOMMU_NO_PASID),17521752 .atc.ssid = ssid,17531753 };17541754···17951795 struct arm_smmu_cmdq_ent cmd;17961796 struct arm_smmu_cmdq_batch cmds;1797179717981798- arm_smmu_atc_inv_to_cmd(0, 0, 0, &cmd);17981798+ arm_smmu_atc_inv_to_cmd(IOMMU_NO_PASID, 0, 0, &cmd);1799179918001800 cmds.num = 0;18011801 for (i = 0; i < master->num_streams; i++) {···18751875 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid;18761876 arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd);18771877 }18781878- arm_smmu_atc_inv_domain(smmu_domain, 0, 0, 0);18781878+ arm_smmu_atc_inv_domain(smmu_domain, IOMMU_NO_PASID, 0, 0);18791879}1880188018811881static void __arm_smmu_tlb_inv_range(struct arm_smmu_cmdq_ent *cmd,···19681968 * Unfortunately, this can't be leaf-only since we may have19691969 * zapped an entire table.19701970 */19711971- arm_smmu_atc_inv_domain(smmu_domain, 0, iova, size);19711971+ arm_smmu_atc_inv_domain(smmu_domain, IOMMU_NO_PASID, iova, size);19721972}1973197319741974void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid,···20552055 return &smmu_domain->domain;20562056}2057205720582058-static int arm_smmu_bitmap_alloc(unsigned long *map, int span)20592059-{20602060- int idx, size = 1 << span;20612061-20622062- do {20632063- idx = find_first_zero_bit(map, size);20642064- if (idx == size)20652065- return -ENOSPC;20662066- } while (test_and_set_bit(idx, map));20672067-20682068- return idx;20692069-}20702070-20712071-static void arm_smmu_bitmap_free(unsigned long *map, int idx)20722072-{20732073- clear_bit(idx, map);20742074-}20752075-20762058static void arm_smmu_domain_free(struct iommu_domain *domain)20772059{20782060 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);···20752093 } else {20762094 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;20772095 if (cfg->vmid)20782078- arm_smmu_bitmap_free(smmu->vmid_map, cfg->vmid);20962096+ ida_free(&smmu->vmid_map, cfg->vmid);20792097 }2080209820812099 kfree(smmu_domain);···21242142 * the master has been added to the devices list for this domain.21252143 * This isn't an issue because the STE hasn't been installed yet.21262144 */21272127- ret = arm_smmu_write_ctx_desc(smmu_domain, 0, &cfg->cd);21452145+ ret = arm_smmu_write_ctx_desc(smmu_domain, IOMMU_NO_PASID, &cfg->cd);21282146 if (ret)21292147 goto out_free_cd_tables;21302148···21492167 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;21502168 typeof(&pgtbl_cfg->arm_lpae_s2_cfg.vtcr) vtcr;2151216921522152- vmid = arm_smmu_bitmap_alloc(smmu->vmid_map, smmu->vmid_bits);21702170+ /* Reserve VMID 0 for stage-2 bypass STEs */21712171+ vmid = ida_alloc_range(&smmu->vmid_map, 1, (1 << smmu->vmid_bits) - 1,21722172+ GFP_KERNEL);21532173 if (vmid < 0)21542174 return vmid;21552175···23122328 pdev = to_pci_dev(master->dev);2313232923142330 atomic_inc(&smmu_domain->nr_ats_masters);23152315- arm_smmu_atc_inv_domain(smmu_domain, 0, 0, 0);23312331+ arm_smmu_atc_inv_domain(smmu_domain, IOMMU_NO_PASID, 0, 0);23162332 if (pci_enable_ats(pdev, stu))23172333 dev_err(master->dev, "Failed to enable ATS (STU %zu)\n", stu);23182334}···30823098 reg |= STRTAB_BASE_RA;30833099 smmu->strtab_cfg.strtab_base = reg;3084310030853085- /* Allocate the first VMID for stage-2 bypass STEs */30863086- set_bit(0, smmu->vmid_map);31013101+ ida_init(&smmu->vmid_map);31023102+30873103 return 0;30883104}30893105···39073923 iommu_device_sysfs_remove(&smmu->iommu);39083924 arm_smmu_device_disable(smmu);39093925 iopf_queue_free(smmu->evtq.iopf);39263926+ ida_destroy(&smmu->vmid_map);39103927}3911392839123929static void arm_smmu_device_shutdown(struct platform_device *pdev)
+1-1
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
···670670671671#define ARM_SMMU_MAX_VMIDS (1 << 16)672672 unsigned int vmid_bits;673673- DECLARE_BITMAP(vmid_map, ARM_SMMU_MAX_VMIDS);673673+ struct ida vmid_map;674674675675 unsigned int ssid_bits;676676 unsigned int sid_bits;
+1-1
drivers/iommu/arm/arm-smmu/arm-smmu-qcom-debug.c
···33 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.44 */5566-#include <linux/of_device.h>66+#include <linux/device.h>77#include <linux/firmware/qcom/qcom_scm.h>88#include <linux/ratelimit.h>99
···2222#include <linux/init.h>2323#include <linux/mutex.h>2424#include <linux/of.h>2525-#include <linux/of_address.h>2626-#include <linux/of_device.h>2525+#include <linux/of_platform.h>2726#include <linux/platform_device.h>2827#include <linux/pm.h>2928#include <linux/pm_runtime.h>···5051 struct clk_bulk_data clks[CLK_NUM];5152 void __iomem *local_base;5253 u32 sec_id;5353- u8 num_ctxs;5454- struct qcom_iommu_ctx *ctxs[]; /* indexed by asid-1 */5454+ u8 max_asid;5555+ struct qcom_iommu_ctx *ctxs[]; /* indexed by asid */5556};56575758struct qcom_iommu_ctx {5859 struct device *dev;5960 void __iomem *base;6061 bool secure_init;6262+ bool secured_ctx;6163 u8 asid; /* asid and ctx bank # are 1:1 */6264 struct iommu_domain *domain;6365};···9494 struct qcom_iommu_dev *qcom_iommu = d->iommu;9595 if (!qcom_iommu)9696 return NULL;9797- return qcom_iommu->ctxs[asid - 1];9797+ return qcom_iommu->ctxs[asid];9898}9999100100static inline void···272272 }273273 ctx->secure_init = true;274274 }275275+276276+ /* Secured QSMMU-500/QSMMU-v2 contexts cannot be programmed */277277+ if (ctx->secured_ctx) {278278+ ctx->domain = domain;279279+ continue;280280+ }281281+282282+ /* Disable context bank before programming */283283+ iommu_writel(ctx, ARM_SMMU_CB_SCTLR, 0);284284+285285+ /* Clear context bank fault address fault status registers */286286+ iommu_writel(ctx, ARM_SMMU_CB_FAR, 0);287287+ iommu_writel(ctx, ARM_SMMU_CB_FSR, ARM_SMMU_FSR_FAULT);275288276289 /* TTBRs */277290 iommu_writeq(ctx, ARM_SMMU_CB_TTBR0,···540527 qcom_iommu = platform_get_drvdata(iommu_pdev);541528542529 /* make sure the asid specified in dt is valid, so we don't have543543- * to sanity check this elsewhere, since 'asid - 1' is used to544544- * index into qcom_iommu->ctxs:530530+ * to sanity check this elsewhere:545531 */546546- if (WARN_ON(asid < 1) ||547547- WARN_ON(asid > qcom_iommu->num_ctxs)) {532532+ if (WARN_ON(asid > qcom_iommu->max_asid) ||533533+ WARN_ON(qcom_iommu->ctxs[asid] == NULL)) {548534 put_device(&iommu_pdev->dev);549535 return -EINVAL;550536 }···629617630618static int get_asid(const struct device_node *np)631619{632632- u32 reg;620620+ u32 reg, val;621621+ int asid;633622634623 /* read the "reg" property directly to get the relative address635624 * of the context bank, and calculate the asid from that:···638625 if (of_property_read_u32_index(np, "reg", 0, ®))639626 return -ENODEV;640627641641- return reg / 0x1000; /* context banks are 0x1000 apart */628628+ /*629629+ * Context banks are 0x1000 apart but, in some cases, the ASID630630+ * number doesn't match to this logic and needs to be passed631631+ * from the DT configuration explicitly.632632+ */633633+ if (!of_property_read_u32(np, "qcom,ctx-asid", &val))634634+ asid = val;635635+ else636636+ asid = reg / 0x1000;637637+638638+ return asid;642639}643640644641static int qcom_iommu_ctx_probe(struct platform_device *pdev)···656633 struct qcom_iommu_ctx *ctx;657634 struct device *dev = &pdev->dev;658635 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev->parent);659659- struct resource *res;660636 int ret, irq;661637662638 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);···665643 ctx->dev = dev;666644 platform_set_drvdata(pdev, ctx);667645668668- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);669669- ctx->base = devm_ioremap_resource(dev, res);646646+ ctx->base = devm_platform_ioremap_resource(pdev, 0);670647 if (IS_ERR(ctx->base))671648 return PTR_ERR(ctx->base);672649673650 irq = platform_get_irq(pdev, 0);674651 if (irq < 0)675675- return -ENODEV;652652+ return irq;653653+654654+ if (of_device_is_compatible(dev->of_node, "qcom,msm-iommu-v2-sec"))655655+ ctx->secured_ctx = true;676656677657 /* clear IRQs before registering fault handler, just in case the678658 * boot-loader left us a surprise:679659 */680680- iommu_writel(ctx, ARM_SMMU_CB_FSR, iommu_readl(ctx, ARM_SMMU_CB_FSR));660660+ if (!ctx->secured_ctx)661661+ iommu_writel(ctx, ARM_SMMU_CB_FSR, iommu_readl(ctx, ARM_SMMU_CB_FSR));681662682663 ret = devm_request_irq(dev, irq,683664 qcom_iommu_fault,···702677703678 dev_dbg(dev, "found asid %u\n", ctx->asid);704679705705- qcom_iommu->ctxs[ctx->asid - 1] = ctx;680680+ qcom_iommu->ctxs[ctx->asid] = ctx;706681707682 return 0;708683}···714689715690 platform_set_drvdata(pdev, NULL);716691717717- qcom_iommu->ctxs[ctx->asid - 1] = NULL;692692+ qcom_iommu->ctxs[ctx->asid] = NULL;718693}719694720695static const struct of_device_id ctx_of_match[] = {721696 { .compatible = "qcom,msm-iommu-v1-ns" },722697 { .compatible = "qcom,msm-iommu-v1-sec" },698698+ { .compatible = "qcom,msm-iommu-v2-ns" },699699+ { .compatible = "qcom,msm-iommu-v2-sec" },723700 { /* sentinel */ }724701};725702···739712 struct device_node *child;740713741714 for_each_child_of_node(qcom_iommu->dev->of_node, child) {742742- if (of_device_is_compatible(child, "qcom,msm-iommu-v1-sec")) {715715+ if (of_device_is_compatible(child, "qcom,msm-iommu-v1-sec") ||716716+ of_device_is_compatible(child, "qcom,msm-iommu-v2-sec")) {743717 of_node_put(child);744718 return true;745719 }···764736 for_each_child_of_node(dev->of_node, child)765737 max_asid = max(max_asid, get_asid(child));766738767767- qcom_iommu = devm_kzalloc(dev, struct_size(qcom_iommu, ctxs, max_asid),739739+ qcom_iommu = devm_kzalloc(dev, struct_size(qcom_iommu, ctxs, max_asid + 1),768740 GFP_KERNEL);769741 if (!qcom_iommu)770742 return -ENOMEM;771771- qcom_iommu->num_ctxs = max_asid;743743+ qcom_iommu->max_asid = max_asid;772744 qcom_iommu->dev = dev;773745774746 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);···884856885857static const struct of_device_id qcom_iommu_of_match[] = {886858 { .compatible = "qcom,msm-iommu-v1" },859859+ { .compatible = "qcom,msm-iommu-v2" },887860 { /* sentinel */ }888861};889862
+20-6
drivers/iommu/dma-iommu.c
···660660{661661 struct iommu_dma_cookie *cookie = domain->iova_cookie;662662 struct iova_domain *iovad = &cookie->iovad;663663- unsigned long shift, iova_len, iova = 0;663663+ unsigned long shift, iova_len, iova;664664665665 if (cookie->type == IOMMU_DMA_MSI_COOKIE) {666666 cookie->msi_iova += size;···675675 if (domain->geometry.force_aperture)676676 dma_limit = min(dma_limit, (u64)domain->geometry.aperture_end);677677678678- /* Try to get PCI devices a SAC address */679679- if (dma_limit > DMA_BIT_MASK(32) && !iommu_dma_forcedac && dev_is_pci(dev))678678+ /*679679+ * Try to use all the 32-bit PCI addresses first. The original SAC vs.680680+ * DAC reasoning loses relevance with PCIe, but enough hardware and681681+ * firmware bugs are still lurking out there that it's safest not to682682+ * venture into the 64-bit space until necessary.683683+ *684684+ * If your device goes wrong after seeing the notice then likely either685685+ * its driver is not setting DMA masks accurately, the hardware has686686+ * some inherent bug in handling >32-bit addresses, or not all the687687+ * expected address bits are wired up between the device and the IOMMU.688688+ */689689+ if (dma_limit > DMA_BIT_MASK(32) && dev->iommu->pci_32bit_workaround) {680690 iova = alloc_iova_fast(iovad, iova_len,681691 DMA_BIT_MASK(32) >> shift, false);692692+ if (iova)693693+ goto done;682694683683- if (!iova)684684- iova = alloc_iova_fast(iovad, iova_len, dma_limit >> shift,685685- true);695695+ dev->iommu->pci_32bit_workaround = false;696696+ dev_notice(dev, "Using %d-bit DMA addresses\n", bits_per(dma_limit));697697+ }686698699699+ iova = alloc_iova_fast(iovad, iova_len, dma_limit >> shift, true);700700+done:687701 return (dma_addr_t)iova << shift;688702}689703
···129129 info->pasid_table = pasid_table;130130131131 if (!ecap_coherent(info->iommu->ecap))132132- clflush_cache_range(pasid_table->table, size);132132+ clflush_cache_range(pasid_table->table, (1 << order) * PAGE_SIZE);133133134134 return 0;135135}···438438 * SVA usage, device could do DMA with multiple PASIDs. It is more439439 * efficient to flush devTLB specific to the PASID.440440 */441441- if (pasid == PASID_RID2PASID)441441+ if (pasid == IOMMU_NO_PASID)442442 qi_flush_dev_iotlb(iommu, sid, pfsid, qdep, 0, 64 - VTD_PAGE_SHIFT);443443 else444444 qi_flush_dev_iotlb_pasid(iommu, sid, pfsid, pasid, qdep, 0, 64 - VTD_PAGE_SHIFT);
···2626#include "trace.h"27272828static irqreturn_t prq_event_thread(int irq, void *d);2929-static void intel_svm_drain_prq(struct device *dev, u32 pasid);3030-#define to_intel_svm_dev(handle) container_of(handle, struct intel_svm_dev, sva)31293230static DEFINE_XARRAY_ALLOC(pasid_private_array);3331static int pasid_private_add(ioasid_t pasid, void *priv)···257259 .invalidate_range = intel_invalidate_range,258260};259261260260-static DEFINE_MUTEX(pasid_mutex);261261-262262static int pasid_to_svm_sdev(struct device *dev, unsigned int pasid,263263 struct intel_svm **rsvm,264264 struct intel_svm_dev **rsdev)265265{266266 struct intel_svm_dev *sdev = NULL;267267 struct intel_svm *svm;268268-269269- /* The caller should hold the pasid_mutex lock */270270- if (WARN_ON(!mutex_is_locked(&pasid_mutex)))271271- return -EINVAL;272268273269 if (pasid == IOMMU_PASID_INVALID || pasid >= PASID_MAX)274270 return -EINVAL;···363371 return ret;364372}365373366366-/* Caller must hold pasid_mutex */367367-static int intel_svm_unbind_mm(struct device *dev, u32 pasid)374374+void intel_svm_remove_dev_pasid(struct device *dev, u32 pasid)368375{369376 struct intel_svm_dev *sdev;370377 struct intel_iommu *iommu;371378 struct intel_svm *svm;372379 struct mm_struct *mm;373373- int ret = -EINVAL;374380375381 iommu = device_to_iommu(dev, NULL, NULL);376382 if (!iommu)377377- goto out;383383+ return;378384379379- ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);380380- if (ret)381381- goto out;385385+ if (pasid_to_svm_sdev(dev, pasid, &svm, &sdev))386386+ return;382387 mm = svm->mm;383388384389 if (sdev) {385390 list_del_rcu(&sdev->list);386386- /*387387- * Flush the PASID cache and IOTLB for this device.388388- * Note that we do depend on the hardware *not* using389389- * the PASID any more. Just as we depend on other390390- * devices never using PASIDs that they have no right391391- * to use. We have a *shared* PASID table, because it's392392- * large and has to be physically contiguous. So it's393393- * hard to be as defensive as we might like.394394- */395395- intel_pasid_tear_down_entry(iommu, dev, svm->pasid, false);396396- intel_svm_drain_prq(dev, svm->pasid);397391 kfree_rcu(sdev, rcu);398392399393 if (list_empty(&svm->devs)) {···396418 kfree(svm);397419 }398420 }399399-out:400400- return ret;401421}402422403423/* Page request queue descriptor */···436460}437461438462/**439439- * intel_svm_drain_prq - Drain page requests and responses for a pasid463463+ * intel_drain_pasid_prq - Drain page requests and responses for a pasid440464 * @dev: target device441465 * @pasid: pasid for draining442466 *···450474 * described in VT-d spec CH7.10 to drain all page requests and page451475 * responses pending in the hardware.452476 */453453-static void intel_svm_drain_prq(struct device *dev, u32 pasid)477477+void intel_drain_pasid_prq(struct device *dev, u32 pasid)454478{455479 struct device_domain_info *info;456480 struct dmar_domain *domain;···496520 goto prq_retry;497521 }498522499499- /*500500- * A work in IO page fault workqueue may try to lock pasid_mutex now.501501- * Holding pasid_mutex while waiting in iopf_queue_flush_dev() for502502- * all works in the workqueue to finish may cause deadlock.503503- *504504- * It's unnecessary to hold pasid_mutex in iopf_queue_flush_dev().505505- * Unlock it to allow the works to be handled while waiting for506506- * them to finish.507507- */508508- lockdep_assert_held(&pasid_mutex);509509- mutex_unlock(&pasid_mutex);510523 iopf_queue_flush_dev(dev);511511- mutex_lock(&pasid_mutex);512524513525 /*514526 * Perform steps described in VT-d spec CH7.10 to drain page···791827 return ret;792828}793829794794-void intel_svm_remove_dev_pasid(struct device *dev, ioasid_t pasid)795795-{796796- mutex_lock(&pasid_mutex);797797- intel_svm_unbind_mm(dev, pasid);798798- mutex_unlock(&pasid_mutex);799799-}800800-801830static int intel_svm_set_dev_pasid(struct iommu_domain *domain,802831 struct device *dev, ioasid_t pasid)803832{804833 struct device_domain_info *info = dev_iommu_priv_get(dev);805834 struct intel_iommu *iommu = info->iommu;806835 struct mm_struct *mm = domain->mm;807807- int ret;808836809809- mutex_lock(&pasid_mutex);810810- ret = intel_svm_bind_mm(iommu, dev, mm);811811- mutex_unlock(&pasid_mutex);812812-813813- return ret;837837+ return intel_svm_bind_mm(iommu, dev, mm);814838}815839816840static void intel_svm_domain_free(struct iommu_domain *domain)
+10-19
drivers/iommu/iommu-sva.c
···1010#include "iommu-sva.h"11111212static DEFINE_MUTEX(iommu_sva_lock);1313-static DEFINE_IDA(iommu_global_pasid_ida);14131514/* Allocate a PASID for the mm within range (inclusive) */1616-static int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t max)1515+static int iommu_sva_alloc_pasid(struct mm_struct *mm, struct device *dev)1716{1717+ ioasid_t pasid;1818 int ret = 0;1919-2020- if (min == IOMMU_PASID_INVALID ||2121- max == IOMMU_PASID_INVALID ||2222- min == 0 || max < min)2323- return -EINVAL;24192520 if (!arch_pgtable_dma_compat(mm))2621 return -EBUSY;···2328 mutex_lock(&iommu_sva_lock);2429 /* Is a PASID already associated with this mm? */2530 if (mm_valid_pasid(mm)) {2626- if (mm->pasid < min || mm->pasid > max)3131+ if (mm->pasid >= dev->iommu->max_pasids)2732 ret = -EOVERFLOW;2833 goto out;2934 }30353131- ret = ida_alloc_range(&iommu_global_pasid_ida, min, max, GFP_KERNEL);3232- if (ret < 0)3636+ pasid = iommu_alloc_global_pasid(dev);3737+ if (pasid == IOMMU_PASID_INVALID) {3838+ ret = -ENOSPC;3339 goto out;3434-3535- mm->pasid = ret;4040+ }4141+ mm->pasid = pasid;3642 ret = 0;3743out:3844 mutex_unlock(&iommu_sva_lock);···6064{6165 struct iommu_domain *domain;6266 struct iommu_sva *handle;6363- ioasid_t max_pasids;6467 int ret;65686666- max_pasids = dev->iommu->max_pasids;6767- if (!max_pasids)6868- return ERR_PTR(-EOPNOTSUPP);6969-7069 /* Allocate mm->pasid if necessary. */7171- ret = iommu_sva_alloc_pasid(mm, 1, max_pasids - 1);7070+ ret = iommu_sva_alloc_pasid(mm, dev);7271 if (ret)7372 return ERR_PTR(ret);7473···208217 if (likely(!mm_valid_pasid(mm)))209218 return;210219211211- ida_free(&iommu_global_pasid_ida, mm->pasid);220220+ iommu_free_global_pasid(mm->pasid);212221}
-8
drivers/iommu/iommu-sysfs.c
···107107{108108 int ret;109109110110- if (!iommu || IS_ERR(iommu))111111- return -ENODEV;112112-113110 ret = sysfs_add_link_to_group(&iommu->dev->kobj, "devices",114111 &link->kobj, dev_name(link));115112 if (ret)···119122120123 return ret;121124}122122-EXPORT_SYMBOL_GPL(iommu_device_link);123125124126void iommu_device_unlink(struct iommu_device *iommu, struct device *link)125127{126126- if (!iommu || IS_ERR(iommu))127127- return;128128-129128 sysfs_remove_link(&link->kobj, "iommu");130129 sysfs_remove_link_from_group(&iommu->dev->kobj, "devices", dev_name(link));131130}132132-EXPORT_SYMBOL_GPL(iommu_device_unlink);
+296-237
drivers/iommu/iommu.c
···39394040static struct kset *iommu_group_kset;4141static DEFINE_IDA(iommu_group_ida);4242+static DEFINE_IDA(iommu_global_pasid_ida);42434344static unsigned int iommu_def_domain_type __read_mostly;4445static bool iommu_dma_strict __read_mostly = IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_STRICT);···128127 int target_type);129128static int iommu_create_device_direct_mappings(struct iommu_domain *domain,130129 struct device *dev);131131-static struct iommu_group *iommu_group_get_for_dev(struct device *dev);132130static ssize_t iommu_group_store_type(struct iommu_group *group,133131 const char *buf, size_t count);132132+static struct group_device *iommu_group_alloc_device(struct iommu_group *group,133133+ struct device *dev);134134+static void __iommu_group_free_device(struct iommu_group *group,135135+ struct group_device *grp_dev);134136135137#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \136138struct iommu_group_attribute iommu_group_attr_##_name = \···337333 return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids);338334}339335336336+/*337337+ * Init the dev->iommu and dev->iommu_group in the struct device and get the338338+ * driver probed339339+ */340340+static int iommu_init_device(struct device *dev, const struct iommu_ops *ops)341341+{342342+ struct iommu_device *iommu_dev;343343+ struct iommu_group *group;344344+ int ret;345345+346346+ if (!dev_iommu_get(dev))347347+ return -ENOMEM;348348+349349+ if (!try_module_get(ops->owner)) {350350+ ret = -EINVAL;351351+ goto err_free;352352+ }353353+354354+ iommu_dev = ops->probe_device(dev);355355+ if (IS_ERR(iommu_dev)) {356356+ ret = PTR_ERR(iommu_dev);357357+ goto err_module_put;358358+ }359359+360360+ ret = iommu_device_link(iommu_dev, dev);361361+ if (ret)362362+ goto err_release;363363+364364+ group = ops->device_group(dev);365365+ if (WARN_ON_ONCE(group == NULL))366366+ group = ERR_PTR(-EINVAL);367367+ if (IS_ERR(group)) {368368+ ret = PTR_ERR(group);369369+ goto err_unlink;370370+ }371371+ dev->iommu_group = group;372372+373373+ dev->iommu->iommu_dev = iommu_dev;374374+ dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev);375375+ if (ops->is_attach_deferred)376376+ dev->iommu->attach_deferred = ops->is_attach_deferred(dev);377377+ return 0;378378+379379+err_unlink:380380+ iommu_device_unlink(iommu_dev, dev);381381+err_release:382382+ if (ops->release_device)383383+ ops->release_device(dev);384384+err_module_put:385385+ module_put(ops->owner);386386+err_free:387387+ dev_iommu_free(dev);388388+ return ret;389389+}390390+391391+static void iommu_deinit_device(struct device *dev)392392+{393393+ struct iommu_group *group = dev->iommu_group;394394+ const struct iommu_ops *ops = dev_iommu_ops(dev);395395+396396+ lockdep_assert_held(&group->mutex);397397+398398+ iommu_device_unlink(dev->iommu->iommu_dev, dev);399399+400400+ /*401401+ * release_device() must stop using any attached domain on the device.402402+ * If there are still other devices in the group they are not effected403403+ * by this callback.404404+ *405405+ * The IOMMU driver must set the device to either an identity or406406+ * blocking translation and stop using any domain pointer, as it is407407+ * going to be freed.408408+ */409409+ if (ops->release_device)410410+ ops->release_device(dev);411411+412412+ /*413413+ * If this is the last driver to use the group then we must free the414414+ * domains before we do the module_put().415415+ */416416+ if (list_empty(&group->devices)) {417417+ if (group->default_domain) {418418+ iommu_domain_free(group->default_domain);419419+ group->default_domain = NULL;420420+ }421421+ if (group->blocking_domain) {422422+ iommu_domain_free(group->blocking_domain);423423+ group->blocking_domain = NULL;424424+ }425425+ group->domain = NULL;426426+ }427427+428428+ /* Caller must put iommu_group */429429+ dev->iommu_group = NULL;430430+ module_put(ops->owner);431431+ dev_iommu_free(dev);432432+}433433+340434static int __iommu_probe_device(struct device *dev, struct list_head *group_list)341435{342436 const struct iommu_ops *ops = dev->bus->iommu_ops;343343- struct iommu_device *iommu_dev;344437 struct iommu_group *group;345438 static DEFINE_MUTEX(iommu_probe_device_lock);439439+ struct group_device *gdev;346440 int ret;347441348442 if (!ops)···453351 * but for now enforcing a simple global ordering is fine.454352 */455353 mutex_lock(&iommu_probe_device_lock);456456- if (!dev_iommu_get(dev)) {457457- ret = -ENOMEM;458458- goto err_unlock;354354+355355+ /* Device is probed already if in a group */356356+ if (dev->iommu_group) {357357+ ret = 0;358358+ goto out_unlock;459359 }460360461461- if (!try_module_get(ops->owner)) {462462- ret = -EINVAL;463463- goto err_free;464464- }361361+ ret = iommu_init_device(dev, ops);362362+ if (ret)363363+ goto out_unlock;465364466466- iommu_dev = ops->probe_device(dev);467467- if (IS_ERR(iommu_dev)) {468468- ret = PTR_ERR(iommu_dev);469469- goto out_module_put;470470- }471471-472472- dev->iommu->iommu_dev = iommu_dev;473473- dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev);474474- if (ops->is_attach_deferred)475475- dev->iommu->attach_deferred = ops->is_attach_deferred(dev);476476-477477- group = iommu_group_get_for_dev(dev);478478- if (IS_ERR(group)) {479479- ret = PTR_ERR(group);480480- goto out_release;481481- }482482-365365+ group = dev->iommu_group;366366+ gdev = iommu_group_alloc_device(group, dev);483367 mutex_lock(&group->mutex);484484- if (group_list && !group->default_domain && list_empty(&group->entry))485485- list_add_tail(&group->entry, group_list);486486- mutex_unlock(&group->mutex);487487- iommu_group_put(group);368368+ if (IS_ERR(gdev)) {369369+ ret = PTR_ERR(gdev);370370+ goto err_put_group;371371+ }488372373373+ /*374374+ * The gdev must be in the list before calling375375+ * iommu_setup_default_domain()376376+ */377377+ list_add_tail(&gdev->list, &group->devices);378378+ WARN_ON(group->default_domain && !group->domain);379379+ if (group->default_domain)380380+ iommu_create_device_direct_mappings(group->default_domain, dev);381381+ if (group->domain) {382382+ ret = __iommu_device_set_domain(group, dev, group->domain, 0);383383+ if (ret)384384+ goto err_remove_gdev;385385+ } else if (!group->default_domain && !group_list) {386386+ ret = iommu_setup_default_domain(group, 0);387387+ if (ret)388388+ goto err_remove_gdev;389389+ } else if (!group->default_domain) {390390+ /*391391+ * With a group_list argument we defer the default_domain setup392392+ * to the caller by providing a de-duplicated list of groups393393+ * that need further setup.394394+ */395395+ if (list_empty(&group->entry))396396+ list_add_tail(&group->entry, group_list);397397+ }398398+ mutex_unlock(&group->mutex);489399 mutex_unlock(&iommu_probe_device_lock);490490- iommu_device_link(iommu_dev, dev);400400+401401+ if (dev_is_pci(dev))402402+ iommu_dma_set_pci_32bit_workaround(dev);491403492404 return 0;493405494494-out_release:495495- if (ops->release_device)496496- ops->release_device(dev);497497-498498-out_module_put:499499- module_put(ops->owner);500500-501501-err_free:502502- dev_iommu_free(dev);503503-504504-err_unlock:406406+err_remove_gdev:407407+ list_del(&gdev->list);408408+ __iommu_group_free_device(group, gdev);409409+err_put_group:410410+ iommu_deinit_device(dev);411411+ mutex_unlock(&group->mutex);412412+ iommu_group_put(group);413413+out_unlock:505414 mutex_unlock(&iommu_probe_device_lock);506415507416 return ret;···521408int iommu_probe_device(struct device *dev)522409{523410 const struct iommu_ops *ops;524524- struct iommu_group *group;525411 int ret;526412527413 ret = __iommu_probe_device(dev, NULL);528414 if (ret)529529- goto err_out;530530-531531- group = iommu_group_get(dev);532532- if (!group) {533533- ret = -ENODEV;534534- goto err_release;535535- }536536-537537- mutex_lock(&group->mutex);538538-539539- if (group->default_domain)540540- iommu_create_device_direct_mappings(group->default_domain, dev);541541-542542- if (group->domain) {543543- ret = __iommu_device_set_domain(group, dev, group->domain, 0);544544- if (ret)545545- goto err_unlock;546546- } else if (!group->default_domain) {547547- ret = iommu_setup_default_domain(group, 0);548548- if (ret)549549- goto err_unlock;550550- }551551-552552- mutex_unlock(&group->mutex);553553- iommu_group_put(group);415415+ return ret;554416555417 ops = dev_iommu_ops(dev);556418 if (ops->probe_finalize)557419 ops->probe_finalize(dev);558420559421 return 0;560560-561561-err_unlock:562562- mutex_unlock(&group->mutex);563563- iommu_group_put(group);564564-err_release:565565- iommu_release_device(dev);566566-567567-err_out:568568- return ret;569569-570422}571423572572-/*573573- * Remove a device from a group's device list and return the group device574574- * if successful.575575- */576576-static struct group_device *577577-__iommu_group_remove_device(struct iommu_group *group, struct device *dev)578578-{579579- struct group_device *device;580580-581581- lockdep_assert_held(&group->mutex);582582- for_each_group_device(group, device) {583583- if (device->dev == dev) {584584- list_del(&device->list);585585- return device;586586- }587587- }588588-589589- return NULL;590590-}591591-592592-/*593593- * Release a device from its group and decrements the iommu group reference594594- * count.595595- */596596-static void __iommu_group_release_device(struct iommu_group *group,597597- struct group_device *grp_dev)424424+static void __iommu_group_free_device(struct iommu_group *group,425425+ struct group_device *grp_dev)598426{599427 struct device *dev = grp_dev->dev;600428···544490545491 trace_remove_device_from_group(group->id, dev);546492547547- kfree(grp_dev->name);548548- kfree(grp_dev);549549- dev->iommu_group = NULL;550550- kobject_put(group->devices_kobj);551551-}552552-553553-static void iommu_release_device(struct device *dev)554554-{555555- struct iommu_group *group = dev->iommu_group;556556- struct group_device *device;557557- const struct iommu_ops *ops;558558-559559- if (!dev->iommu || !group)560560- return;561561-562562- iommu_device_unlink(dev->iommu->iommu_dev, dev);563563-564564- mutex_lock(&group->mutex);565565- device = __iommu_group_remove_device(group, dev);566566-567493 /*568568- * If the group has become empty then ownership must have been released,569569- * and the current domain must be set back to NULL or the default570570- * domain.494494+ * If the group has become empty then ownership must have been495495+ * released, and the current domain must be set back to NULL or496496+ * the default domain.571497 */572498 if (list_empty(&group->devices))573499 WARN_ON(group->owner_cnt ||574500 group->domain != group->default_domain);575501576576- /*577577- * release_device() must stop using any attached domain on the device.578578- * If there are still other devices in the group they are not effected579579- * by this callback.580580- *581581- * The IOMMU driver must set the device to either an identity or582582- * blocking translation and stop using any domain pointer, as it is583583- * going to be freed.584584- */585585- ops = dev_iommu_ops(dev);586586- if (ops->release_device)587587- ops->release_device(dev);502502+ kfree(grp_dev->name);503503+ kfree(grp_dev);504504+}505505+506506+/* Remove the iommu_group from the struct device. */507507+static void __iommu_group_remove_device(struct device *dev)508508+{509509+ struct iommu_group *group = dev->iommu_group;510510+ struct group_device *device;511511+512512+ mutex_lock(&group->mutex);513513+ for_each_group_device(group, device) {514514+ if (device->dev != dev)515515+ continue;516516+517517+ list_del(&device->list);518518+ __iommu_group_free_device(group, device);519519+ if (dev->iommu && dev->iommu->iommu_dev)520520+ iommu_deinit_device(dev);521521+ else522522+ dev->iommu_group = NULL;523523+ break;524524+ }588525 mutex_unlock(&group->mutex);589526590590- if (device)591591- __iommu_group_release_device(group, device);527527+ /*528528+ * Pairs with the get in iommu_init_device() or529529+ * iommu_group_add_device()530530+ */531531+ iommu_group_put(group);532532+}592533593593- module_put(ops->owner);594594- dev_iommu_free(dev);534534+static void iommu_release_device(struct device *dev)535535+{536536+ struct iommu_group *group = dev->iommu_group;537537+538538+ if (group)539539+ __iommu_group_remove_device(dev);540540+541541+ /* Free any fwspec if no iommu_driver was ever attached */542542+ if (dev->iommu)543543+ dev_iommu_free(dev);595544}596545597546static int __init iommu_set_def_domain_type(char *str)···855798856799 ida_free(&iommu_group_ida, group->id);857800858858- if (group->default_domain)859859- iommu_domain_free(group->default_domain);860860- if (group->blocking_domain)861861- iommu_domain_free(group->blocking_domain);801801+ /* Domains are free'd by iommu_deinit_device() */802802+ WARN_ON(group->default_domain);803803+ WARN_ON(group->blocking_domain);862804863805 kfree(group->name);864806 kfree(group);···1015959 unsigned long pg_size;1016960 int ret = 0;101796110181018- if (!iommu_is_dma_domain(domain))10191019- return 0;10201020-10211021- BUG_ON(!domain->pgsize_bitmap);10221022-10231023- pg_size = 1UL << __ffs(domain->pgsize_bitmap);962962+ pg_size = domain->pgsize_bitmap ? 1UL << __ffs(domain->pgsize_bitmap) : 0;1024963 INIT_LIST_HEAD(&mappings);964964+965965+ if (WARN_ON_ONCE(iommu_is_dma_domain(domain) && !pg_size))966966+ return -EINVAL;10259671026968 iommu_get_resv_regions(dev, &mappings);1027969···1028974 dma_addr_t start, end, addr;1029975 size_t map_size = 0;1030976977977+ if (entry->type == IOMMU_RESV_DIRECT)978978+ dev->iommu->require_direct = 1;979979+980980+ if ((entry->type != IOMMU_RESV_DIRECT &&981981+ entry->type != IOMMU_RESV_DIRECT_RELAXABLE) ||982982+ !iommu_is_dma_domain(domain))983983+ continue;984984+1031985 start = ALIGN(entry->start, pg_size);1032986 end = ALIGN(entry->start + entry->length, pg_size);10331033-10341034- if (entry->type != IOMMU_RESV_DIRECT &&10351035- entry->type != IOMMU_RESV_DIRECT_RELAXABLE)10361036- continue;10379871038988 for (addr = start; addr <= end; addr += pg_size) {1039989 phys_addr_t phys_addr;···10721014 return ret;10731015}1074101610751075-/**10761076- * iommu_group_add_device - add a device to an iommu group10771077- * @group: the group into which to add the device (reference should be held)10781078- * @dev: the device10791079- *10801080- * This function is called by an iommu driver to add a device into a10811081- * group. Adding a device increments the group reference count.10821082- */10831083-int iommu_group_add_device(struct iommu_group *group, struct device *dev)10171017+/* This is undone by __iommu_group_free_device() */10181018+static struct group_device *iommu_group_alloc_device(struct iommu_group *group,10191019+ struct device *dev)10841020{10851021 int ret, i = 0;10861022 struct group_device *device;1087102310881024 device = kzalloc(sizeof(*device), GFP_KERNEL);10891025 if (!device)10901090- return -ENOMEM;10261026+ return ERR_PTR(-ENOMEM);1091102710921028 device->dev = dev;10931029···11121060 goto err_free_name;11131061 }1114106211151115- kobject_get(group->devices_kobj);11161116-11171117- dev->iommu_group = group;11181118-11191119- mutex_lock(&group->mutex);11201120- list_add_tail(&device->list, &group->devices);11211121- mutex_unlock(&group->mutex);11221063 trace_add_device_to_group(group->id, dev);1123106411241065 dev_info(dev, "Adding to iommu group %d\n", group->id);1125106611261126- return 0;10671067+ return device;1127106811281069err_free_name:11291070 kfree(device->name);···11251080err_free_device:11261081 kfree(device);11271082 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);11281128- return ret;10831083+ return ERR_PTR(ret);10841084+}10851085+10861086+/**10871087+ * iommu_group_add_device - add a device to an iommu group10881088+ * @group: the group into which to add the device (reference should be held)10891089+ * @dev: the device10901090+ *10911091+ * This function is called by an iommu driver to add a device into a10921092+ * group. Adding a device increments the group reference count.10931093+ */10941094+int iommu_group_add_device(struct iommu_group *group, struct device *dev)10951095+{10961096+ struct group_device *gdev;10971097+10981098+ gdev = iommu_group_alloc_device(group, dev);10991099+ if (IS_ERR(gdev))11001100+ return PTR_ERR(gdev);11011101+11021102+ iommu_group_ref_get(group);11031103+ dev->iommu_group = group;11041104+11051105+ mutex_lock(&group->mutex);11061106+ list_add_tail(&gdev->list, &group->devices);11071107+ mutex_unlock(&group->mutex);11081108+ return 0;11291109}11301110EXPORT_SYMBOL_GPL(iommu_group_add_device);11311111···11641094void iommu_group_remove_device(struct device *dev)11651095{11661096 struct iommu_group *group = dev->iommu_group;11671167- struct group_device *device;1168109711691098 if (!group)11701099 return;1171110011721101 dev_info(dev, "Removing from iommu group %d\n", group->id);1173110211741174- mutex_lock(&group->mutex);11751175- device = __iommu_group_remove_device(group, dev);11761176- mutex_unlock(&group->mutex);11771177-11781178- if (device)11791179- __iommu_group_release_device(group, device);11031103+ __iommu_group_remove_device(dev);11801104}11811105EXPORT_SYMBOL_GPL(iommu_group_remove_device);11821106···17281664 return dom;17291665}1730166617311731-/**17321732- * iommu_group_get_for_dev - Find or create the IOMMU group for a device17331733- * @dev: target device17341734- *17351735- * This function is intended to be called by IOMMU drivers and extended to17361736- * support common, bus-defined algorithms when determining or creating the17371737- * IOMMU group for a device. On success, the caller will hold a reference17381738- * to the returned IOMMU group, which will already include the provided17391739- * device. The reference should be released with iommu_group_put().17401740- */17411741-static struct iommu_group *iommu_group_get_for_dev(struct device *dev)17421742-{17431743- const struct iommu_ops *ops = dev_iommu_ops(dev);17441744- struct iommu_group *group;17451745- int ret;17461746-17471747- group = iommu_group_get(dev);17481748- if (group)17491749- return group;17501750-17511751- group = ops->device_group(dev);17521752- if (WARN_ON_ONCE(group == NULL))17531753- return ERR_PTR(-EINVAL);17541754-17551755- if (IS_ERR(group))17561756- return group;17571757-17581758- ret = iommu_group_add_device(group, dev);17591759- if (ret)17601760- goto out_put_group;17611761-17621762- return group;17631763-17641764-out_put_group:17651765- iommu_group_put(group);17661766-17671767- return ERR_PTR(ret);17681768-}17691769-17701667struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)17711668{17721669 return group->default_domain;···17361711static int probe_iommu_group(struct device *dev, void *data)17371712{17381713 struct list_head *group_list = data;17391739- struct iommu_group *group;17401714 int ret;17411741-17421742- /* Device is probed already if in a group */17431743- group = iommu_group_get(dev);17441744- if (group) {17451745- iommu_group_put(group);17461746- return 0;17471747- }1748171517491716 ret = __iommu_probe_device(dev, group_list);17501717 if (ret == -ENODEV)···18131796 LIST_HEAD(group_list);18141797 int ret;1815179818161816- /*18171817- * This code-path does not allocate the default domain when18181818- * creating the iommu group, so do it after the groups are18191819- * created.18201820- */18211799 ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);18221800 if (ret)18231801 return ret;···18251813 /* Remove item from the list */18261814 list_del_init(&group->entry);1827181518161816+ /*18171817+ * We go to the trouble of deferred default domain creation so18181818+ * that the cross-group default domain type and the setup of the18191819+ * IOMMU_RESV_DIRECT will work correctly in non-hotpug scenarios.18201820+ */18281821 ret = iommu_setup_default_domain(group, 0);18291822 if (ret) {18301823 mutex_unlock(&group->mutex);···21372120 unsigned int flags)21382121{21392122 int ret;21232123+21242124+ /*21252125+ * If the device requires IOMMU_RESV_DIRECT then we cannot allow21262126+ * the blocking domain to be attached as it does not contain the21272127+ * required 1:1 mapping. This test effectively excludes the device21282128+ * being used with iommu_group_claim_dma_owner() which will block21292129+ * vfio and iommufd as well.21302130+ */21312131+ if (dev->iommu->require_direct &&21322132+ (new_domain->type == IOMMU_DOMAIN_BLOCKED ||21332133+ new_domain == group->blocking_domain)) {21342134+ dev_warn(dev,21352135+ "Firmware has requested this device have a 1:1 IOMMU mapping, rejecting configuring the device without a 1:1 mapping. Contact your platform vendor.\n");21362136+ return -EINVAL;21372137+ }2140213821412139 if (dev->iommu->attach_deferred) {21422140 if (new_domain == group->default_domain)···3235320332363204/**32373205 * iommu_group_release_dma_owner() - Release DMA ownership of a group32383238- * @dev: The device32063206+ * @group: The group32393207 *32403208 * Release the DMA ownership claimed by iommu_group_claim_dma_owner().32413209 */···3249321732503218/**32513219 * iommu_device_release_dma_owner() - Release DMA ownership of a device32523252- * @group: The device.32203220+ * @dev: The device.32533221 *32543222 * Release the DMA ownership claimed by iommu_device_claim_dma_owner().32553223 */···3432340034333401 return domain;34343402}34033403+34043404+ioasid_t iommu_alloc_global_pasid(struct device *dev)34053405+{34063406+ int ret;34073407+34083408+ /* max_pasids == 0 means that the device does not support PASID */34093409+ if (!dev->iommu->max_pasids)34103410+ return IOMMU_PASID_INVALID;34113411+34123412+ /*34133413+ * max_pasids is set up by vendor driver based on number of PASID bits34143414+ * supported but the IDA allocation is inclusive.34153415+ */34163416+ ret = ida_alloc_range(&iommu_global_pasid_ida, IOMMU_FIRST_GLOBAL_PASID,34173417+ dev->iommu->max_pasids - 1, GFP_KERNEL);34183418+ return ret < 0 ? IOMMU_PASID_INVALID : ret;34193419+}34203420+EXPORT_SYMBOL_GPL(iommu_alloc_global_pasid);34213421+34223422+void iommu_free_global_pasid(ioasid_t pasid)34233423+{34243424+ if (WARN_ON(pasid == IOMMU_PASID_INVALID))34253425+ return;34263426+34273427+ ida_free(&iommu_global_pasid_ida, pasid);34283428+}34293429+EXPORT_SYMBOL_GPL(iommu_free_global_pasid);
+11-10
drivers/iommu/ipmmu-vmsa.c
···1414#include <linux/init.h>1515#include <linux/interrupt.h>1616#include <linux/io.h>1717+#include <linux/iopoll.h>1718#include <linux/io-pgtable.h>1819#include <linux/iommu.h>1920#include <linux/of.h>2020-#include <linux/of_device.h>2121#include <linux/of_platform.h>2222+#include <linux/pci.h>2223#include <linux/platform_device.h>2324#include <linux/sizes.h>2425#include <linux/slab.h>···254253/* Wait for any pending TLB invalidations to complete */255254static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)256255{257257- unsigned int count = 0;256256+ u32 val;258257259259- while (ipmmu_ctx_read_root(domain, IMCTR) & IMCTR_FLUSH) {260260- cpu_relax();261261- if (++count == TLB_LOOP_TIMEOUT) {262262- dev_err_ratelimited(domain->mmu->dev,258258+ if (read_poll_timeout_atomic(ipmmu_ctx_read_root, val,259259+ !(val & IMCTR_FLUSH), 1, TLB_LOOP_TIMEOUT,260260+ false, domain, IMCTR))261261+ dev_err_ratelimited(domain->mmu->dev,263262 "TLB sync timed out -- MMU may be deadlocked\n");264264- return;265265- }266266- udelay(1);267267- }268263}269264270265static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)···719722 /* Check whether this SoC can use the IPMMU correctly or not */720723 if (soc_device_match(soc_denylist))721724 return false;725725+726726+ /* Check whether this device is a PCI device */727727+ if (dev_is_pci(dev))728728+ return true;722729723730 /* Check whether this device can work with the IPMMU */724731 for (i = 0; i < ARRAY_SIZE(devices_allowlist); i++) {
···159159 * If we have reason to believe the IOMMU driver missed the initial160160 * probe for dev, replay it to get things in order.161161 */162162- if (!err && dev->bus && !device_iommu_mapped(dev))162162+ if (!err && dev->bus)163163 err = iommu_probe_device(dev);164164165165 /* Ignore all other errors apart from EPROBE_DEFER */
···3232struct pci_dev;33333434extern int amd_iommu_detect(void);3535-extern int amd_iommu_init_hardware(void);36353736/**3837 * amd_iommu_init_device() - Init device for use with IOMMUv2 driver
-2
include/linux/dmar.h
···106106extern int dmar_table_init(void);107107extern int dmar_dev_scope_init(void);108108extern void dmar_register_bus_notifier(void);109109-extern int dmar_parse_dev_scope(void *start, void *end, int *cnt,110110- struct dmar_dev_scope **devices, u16 segment);111109extern void *dmar_alloc_dev_scope(void *start, void *end, int *cnt);112110extern void dmar_free_dev_scope(struct dmar_dev_scope **devices, int *cnt);113111extern int dmar_insert_dev_scope(struct dmar_pci_notify_info *info,
+15
include/linux/iommu.h
···196196 IOMMU_DEV_FEAT_IOPF,197197};198198199199+#define IOMMU_NO_PASID (0U) /* Reserved for DMA w/o PASID */200200+#define IOMMU_FIRST_GLOBAL_PASID (1U) /*starting range for allocation */199201#define IOMMU_PASID_INVALID (-1U)200202typedef unsigned int ioasid_t;201203···411409 * @priv: IOMMU Driver private data412410 * @max_pasids: number of PASIDs this device can consume413411 * @attach_deferred: the dma domain attachment is deferred412412+ * @pci_32bit_workaround: Limit DMA allocations to 32-bit IOVAs413413+ * @require_direct: device requires IOMMU_RESV_DIRECT regions414414 *415415 * TODO: migrate other per device data pointers under iommu_dev_data, e.g.416416 * struct iommu_group *iommu_group;···426422 void *priv;427423 u32 max_pasids;428424 u32 attach_deferred:1;425425+ u32 pci_32bit_workaround:1;426426+ u32 require_direct:1;429427};430428431429int iommu_device_register(struct iommu_device *iommu,···733727struct iommu_domain *734728iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid,735729 unsigned int type);730730+ioasid_t iommu_alloc_global_pasid(struct device *dev);731731+void iommu_free_global_pasid(ioasid_t pasid);736732#else /* CONFIG_IOMMU_API */737733738734struct iommu_ops {};···10961088{10971089 return NULL;10981090}10911091+10921092+static inline ioasid_t iommu_alloc_global_pasid(struct device *dev)10931093+{10941094+ return IOMMU_PASID_INVALID;10951095+}10961096+10971097+static inline void iommu_free_global_pasid(ioasid_t pasid) {}10991098#endif /* CONFIG_IOMMU_API */1100109911011100/**
+1
include/soc/mediatek/smi.h
···13131414enum iommu_atf_cmd {1515 IOMMU_ATF_CMD_CONFIG_SMI_LARB, /* For mm master to en/disable iommu */1616+ IOMMU_ATF_CMD_CONFIG_INFRA_IOMMU, /* For infra master to enable iommu */1617 IOMMU_ATF_CMD_MAX,1718};1819