]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
iommu/vt-d: Move PRI enablement in probe path
authorLu Baolu <baolu.lu@linux.intel.com>
Mon, 10 Mar 2025 02:47:48 +0000 (10:47 +0800)
committerJoerg Roedel <jroedel@suse.de>
Mon, 10 Mar 2025 08:31:04 +0000 (09:31 +0100)
Update PRI enablement to use the new method, similar to the amd iommu
driver. Enable PRI in the device probe path and disable it when the device
is released. PRI is enabled throughout the device's iommu lifecycle. The
infrastructure for the iommu subsystem to handle iopf requests is created
during iopf enablement and released during iopf disablement.  All invalid
page requests from the device are automatically handled by the iommu
subsystem if iopf is not enabled. Add iopf_refcount to track the iopf
enablement.

Convert the return type of intel_iommu_disable_iopf() to void, as there
is no way to handle a failure when disabling this feature.  Make
intel_iommu_enable/disable_iopf() helpers global, as they will be used
beyond the current file in the subsequent patch.

The iopf_refcount is not protected by any lock. This is acceptable, as
there is no concurrent access to it in the current code. The following
patch will address this by moving it to the domain attach/detach paths,
which are protected by the iommu group mutex.

Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Tested-by: Zhangfei Gao <zhangfei.gao@linaro.org>
Link: https://lore.kernel.org/r/20250228092631.3425464-6-baolu.lu@linux.intel.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>
drivers/iommu/intel/iommu.c
drivers/iommu/intel/iommu.h
drivers/iommu/intel/pasid.c
drivers/iommu/intel/prq.c

index 83928038b9cb1f01236662cc41e7d97207dea7ec..9ed8bdb3e9da6ac945d6e39b72385995fef9a7d4 100644 (file)
@@ -1197,6 +1197,37 @@ static void iommu_disable_pci_ats(struct device_domain_info *info)
        info->ats_enabled = 0;
 }
 
+static void iommu_enable_pci_pri(struct device_domain_info *info)
+{
+       struct pci_dev *pdev;
+
+       if (!info->ats_enabled || !info->pri_supported)
+               return;
+
+       pdev = to_pci_dev(info->dev);
+       /* PASID is required in PRG Response Message. */
+       if (info->pasid_enabled && !pci_prg_resp_pasid_required(pdev))
+               return;
+
+       if (pci_reset_pri(pdev))
+               return;
+
+       if (!pci_enable_pri(pdev, PRQ_DEPTH))
+               info->pri_enabled = 1;
+}
+
+static void iommu_disable_pci_pri(struct device_domain_info *info)
+{
+       if (!info->pri_enabled)
+               return;
+
+       if (WARN_ON(info->iopf_refcount))
+               iopf_queue_remove_device(info->iommu->iopf_queue, info->dev);
+
+       pci_disable_pri(to_pci_dev(info->dev));
+       info->pri_enabled = 0;
+}
+
 static void intel_flush_iotlb_all(struct iommu_domain *domain)
 {
        cache_tag_flush_all(to_dmar_domain(domain));
@@ -3756,6 +3787,7 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev)
 
        if (sm_supported(iommu))
                iommu_enable_pci_ats(info);
+       iommu_enable_pci_pri(info);
 
        return &iommu->iommu;
 free_table:
@@ -3773,6 +3805,7 @@ static void intel_iommu_release_device(struct device *dev)
        struct device_domain_info *info = dev_iommu_priv_get(dev);
        struct intel_iommu *iommu = info->iommu;
 
+       iommu_disable_pci_pri(info);
        iommu_disable_pci_ats(info);
 
        if (info->pasid_enabled) {
@@ -3861,116 +3894,41 @@ static struct iommu_group *intel_iommu_device_group(struct device *dev)
        return generic_device_group(dev);
 }
 
-static int context_flip_pri(struct device_domain_info *info, bool enable)
-{
-       struct intel_iommu *iommu = info->iommu;
-       u8 bus = info->bus, devfn = info->devfn;
-       struct context_entry *context;
-       u16 did;
-
-       spin_lock(&iommu->lock);
-       if (context_copied(iommu, bus, devfn)) {
-               spin_unlock(&iommu->lock);
-               return -EINVAL;
-       }
-
-       context = iommu_context_addr(iommu, bus, devfn, false);
-       if (!context || !context_present(context)) {
-               spin_unlock(&iommu->lock);
-               return -ENODEV;
-       }
-       did = context_domain_id(context);
-
-       if (enable)
-               context_set_sm_pre(context);
-       else
-               context_clear_sm_pre(context);
-
-       if (!ecap_coherent(iommu->ecap))
-               clflush_cache_range(context, sizeof(*context));
-       intel_context_flush_present(info, context, did, true);
-       spin_unlock(&iommu->lock);
-
-       return 0;
-}
-
-static int intel_iommu_enable_iopf(struct device *dev)
+int intel_iommu_enable_iopf(struct device *dev)
 {
-       struct pci_dev *pdev = dev_is_pci(dev) ? to_pci_dev(dev) : NULL;
        struct device_domain_info *info = dev_iommu_priv_get(dev);
-       struct intel_iommu *iommu;
+       struct intel_iommu *iommu = info->iommu;
        int ret;
 
-       if (!pdev || !info || !info->ats_enabled || !info->pri_supported)
+       if (!info->pri_enabled)
                return -ENODEV;
 
-       if (info->pri_enabled)
-               return -EBUSY;
-
-       iommu = info->iommu;
-       if (!iommu)
-               return -EINVAL;
-
-       /* PASID is required in PRG Response Message. */
-       if (info->pasid_enabled && !pci_prg_resp_pasid_required(pdev))
-               return -EINVAL;
-
-       ret = pci_reset_pri(pdev);
-       if (ret)
-               return ret;
+       if (info->iopf_refcount) {
+               info->iopf_refcount++;
+               return 0;
+       }
 
        ret = iopf_queue_add_device(iommu->iopf_queue, dev);
        if (ret)
                return ret;
 
-       ret = context_flip_pri(info, true);
-       if (ret)
-               goto err_remove_device;
-
-       ret = pci_enable_pri(pdev, PRQ_DEPTH);
-       if (ret)
-               goto err_clear_pri;
-
-       info->pri_enabled = 1;
+       info->iopf_refcount = 1;
 
        return 0;
-err_clear_pri:
-       context_flip_pri(info, false);
-err_remove_device:
-       iopf_queue_remove_device(iommu->iopf_queue, dev);
-
-       return ret;
 }
 
-static int intel_iommu_disable_iopf(struct device *dev)
+void intel_iommu_disable_iopf(struct device *dev)
 {
        struct device_domain_info *info = dev_iommu_priv_get(dev);
        struct intel_iommu *iommu = info->iommu;
 
-       if (!info->pri_enabled)
-               return -EINVAL;
+       if (WARN_ON(!info->pri_enabled || !info->iopf_refcount))
+               return;
 
-       /* Disable new PRI reception: */
-       context_flip_pri(info, false);
+       if (--info->iopf_refcount)
+               return;
 
-       /*
-        * Remove device from fault queue and acknowledge all outstanding
-        * PRQs to the device:
-        */
        iopf_queue_remove_device(iommu->iopf_queue, dev);
-
-       /*
-        * PCIe spec states that by clearing PRI enable bit, the Page
-        * Request Interface will not issue new page requests, but has
-        * outstanding page requests that have been transmitted or are
-        * queued for transmission. This is supposed to be called after
-        * the device driver has stopped DMA, all PASIDs have been
-        * unbound and the outstanding PRQs have been drained.
-        */
-       pci_disable_pri(to_pci_dev(dev));
-       info->pri_enabled = 0;
-
-       return 0;
 }
 
 static int
@@ -3993,7 +3951,8 @@ intel_iommu_dev_disable_feat(struct device *dev, enum iommu_dev_features feat)
 {
        switch (feat) {
        case IOMMU_DEV_FEAT_IOPF:
-               return intel_iommu_disable_iopf(dev);
+               intel_iommu_disable_iopf(dev);
+               return 0;
 
        case IOMMU_DEV_FEAT_SVA:
                return 0;
index dd980808998da9fb78b9f0236a0e0935b67bd4f2..42b4e500989b248f2f0bbed58402dec395896040 100644 (file)
@@ -774,6 +774,7 @@ struct device_domain_info {
        u8 ats_enabled:1;
        u8 dtlb_extra_inval:1;  /* Quirk for devices need extra flush */
        u8 ats_qdep;
+       unsigned int iopf_refcount;
        struct device *dev; /* it's NULL for PCIe-to-PCI bridge */
        struct intel_iommu *iommu; /* IOMMU used by this device */
        struct dmar_domain *domain; /* pointer to domain */
@@ -1295,6 +1296,9 @@ void intel_iommu_page_response(struct device *dev, struct iopf_fault *evt,
                               struct iommu_page_response *msg);
 void intel_iommu_drain_pasid_prq(struct device *dev, u32 pasid);
 
+int intel_iommu_enable_iopf(struct device *dev);
+void intel_iommu_disable_iopf(struct device *dev);
+
 #ifdef CONFIG_INTEL_IOMMU_SVM
 void intel_svm_check(struct intel_iommu *iommu);
 struct iommu_domain *intel_svm_domain_alloc(struct device *dev,
index fb59a7d35958f50350b7c70fcf3dc572ecee0e16..c2742e256552ac6700ae430d613984bb977342a8 100644 (file)
@@ -992,6 +992,8 @@ static int context_entry_set_pasid_table(struct context_entry *context,
                context_set_sm_dte(context);
        if (info->pasid_supported)
                context_set_pasid(context);
+       if (info->pri_supported)
+               context_set_sm_pre(context);
 
        context_set_fault_enable(context);
        context_set_present(context);
index 064194399b38bb74ee11b0519cbdb00bd61f1ec0..5b6a64d968502f5342d4aa182a0a0f5035ebe207 100644 (file)
@@ -67,7 +67,7 @@ void intel_iommu_drain_pasid_prq(struct device *dev, u32 pasid)
        u16 sid, did;
 
        info = dev_iommu_priv_get(dev);
-       if (!info->pri_enabled)
+       if (!info->iopf_refcount)
                return;
 
        iommu = info->iommu;