]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
PCI: Add pci_ats_required() for CXL.cache capable devices
authorNicolin Chen <nicolinc@nvidia.com>
Thu, 21 May 2026 20:34:20 +0000 (13:34 -0700)
committerJoerg Roedel <joerg.roedel@amd.com>
Thu, 28 May 2026 07:34:41 +0000 (09:34 +0200)
Controlled by IOMMU drivers, ATS can be enabled "on demand", when a given
PASID on a device is attached to an I/O page table. This is working, even
when a device has no translation on its RID (i.e., RID is IOMMU bypassed).

However, certain PCIe devices require non-PASID ATS on their RID even when
the RID is IOMMU bypassed. Call this "ATS always on" in IOMMU term.

For example, CXL spec r4.0 notes in sec 3.2.5.13 Memory Type on CXL.cache:
 "To source requests on CXL.cache, devices need to get the Host Physical
  Address (HPA) from the Host by means of an ATS request on CXL.io."

In other words, the CXL.cache capability requires ATS; otherwise, it can't
access host physical memory.

Introduce a new pci_ats_required() helper for the IOMMU driver to scan a
PCI device and shift ATS policies between "on demand" and "always on".

Add the support for CXL.cache devices first. Pre-CXL devices will be added
in quirks.c file.

Note that pci_ats_required() validates against pci_ats_supported(), so we
ensure that untrusted devices (e.g. external ports) will not be always on.
This maintains the existing ATS security policy regarding potential side-
channel attacks via ATS.

Cc: linux-cxl@vger.kernel.org
Suggested-by: Vikram Sethi <vsethi@nvidia.com>
Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Tested-by: Nirmoy Das <nirmoyd@nvidia.com>
Acked-by: Nirmoy Das <nirmoyd@nvidia.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Acked-by: Bjorn Helgaas <bhelgaas@google.com>
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
Reviewed-by: Yi Liu <yi.l.liu@intel.com>
Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
drivers/pci/ats.c
include/linux/pci-ats.h
include/uapi/linux/pci_regs.h

index ec6c8dbdc5e9c9959e822e016ab301bf483713a5..84cd06d74fc9c7e57ee603eefddb4f2dee123a98 100644 (file)
@@ -205,6 +205,52 @@ int pci_ats_page_aligned(struct pci_dev *pdev)
        return 0;
 }
 
+/*
+ * CXL r4.0, sec 3.2.5.13 Memory Type on CXL.cache notes: to source requests on
+ * CXL.cache, devices need to get the Host Physical Address (HPA) from the Host
+ * by means of an ATS request on CXL.io.
+ *
+ * In other words, CXL.cache devices cannot access host physical memory without
+ * ATS.
+ *
+ * Check Cache_Capable instead of Cache_Enable because CXL.cache may be enabled
+ * after the caller uses this to make its ATS decision.
+ */
+static bool pci_cxl_ats_required(struct pci_dev *pdev)
+{
+       int offset;
+       u16 cap;
+
+       offset = pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_CXL,
+                                          PCI_DVSEC_CXL_DEVICE);
+       if (!offset)
+               return false;
+
+       if (pci_read_config_word(pdev, offset + PCI_DVSEC_CXL_CAP, &cap))
+               return false;
+
+       return cap & PCI_DVSEC_CXL_CACHE_CAPABLE;
+}
+
+/**
+ * pci_ats_required - Whether the PCI device requires ATS
+ * @pdev: the PCI device
+ *
+ * Returns true, if the PCI device requires ATS for basic functional operation.
+ */
+bool pci_ats_required(struct pci_dev *pdev)
+{
+       if (!pci_ats_supported(pdev))
+               return false;
+
+       /* A VF inherits its PF's requirement for ATS function */
+       if (pdev->is_virtfn)
+               pdev = pci_physfn(pdev);
+
+       return pci_cxl_ats_required(pdev);
+}
+EXPORT_SYMBOL_GPL(pci_ats_required);
+
 #ifdef CONFIG_PCI_PRI
 void pci_pri_init(struct pci_dev *pdev)
 {
index 75c6c86cf09dcbf0515a4b04c496b679718df4ba..f3723b6861294119d593b3684eea664a21b8c720 100644 (file)
@@ -12,6 +12,7 @@ int pci_prepare_ats(struct pci_dev *dev, int ps);
 void pci_disable_ats(struct pci_dev *dev);
 int pci_ats_queue_depth(struct pci_dev *dev);
 int pci_ats_page_aligned(struct pci_dev *dev);
+bool pci_ats_required(struct pci_dev *dev);
 #else /* CONFIG_PCI_ATS */
 static inline bool pci_ats_supported(struct pci_dev *d)
 { return false; }
@@ -24,6 +25,8 @@ static inline int pci_ats_queue_depth(struct pci_dev *d)
 { return -ENODEV; }
 static inline int pci_ats_page_aligned(struct pci_dev *dev)
 { return 0; }
+static inline bool pci_ats_required(struct pci_dev *dev)
+{ return false; }
 #endif /* CONFIG_PCI_ATS */
 
 #ifdef CONFIG_PCI_PRI
index 14f634ab9350d5442192162225b5e5202dbe2308..6ac45be1008b8b04a2433123f6a5b5eafe65ba31 100644 (file)
 /* CXL r4.0, 8.1.3: PCIe DVSEC for CXL Device */
 #define PCI_DVSEC_CXL_DEVICE                           0
 #define  PCI_DVSEC_CXL_CAP                             0xA
+#define   PCI_DVSEC_CXL_CACHE_CAPABLE                  _BITUL(0)
 #define   PCI_DVSEC_CXL_MEM_CAPABLE                    _BITUL(2)
 #define   PCI_DVSEC_CXL_HDM_COUNT                      __GENMASK(5, 4)
 #define  PCI_DVSEC_CXL_CTRL                            0xC