Skip to contentSkip to conversation

cxl: add support for cxl reset

RFC PATCH In review Srirangan Madhavan 4 participants 7 messages Dec 13, 2024 – Dec 24, 2024 1 message not in the archive PCI

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 cxl: add support for cxl reset

Currently, there is a lack of support for the CXL Reset method for cxl devices. This change adds the support and implements the common reset steps as laid out by the CXL Spec v3.1 Sections 9.6 & 9.7.

For devices that support CXL Reset, cache lines are disabled and WB+I is asserted. After waiting for cache invalid status, mem clr bit is asserted and finally reset is initiated. After the recommended timeout period, the reset status is checked and returned.

The patch has not been tested completely as the infrastructure is not yet in place. As this is my first kernel patch, I am looking to get the community feedback through the RFC ahead of time.

---
 drivers/pci/pci.c             | 184 ++++++++++++++++++++++++++++++++++
 include/linux/pci.h           |   2 +-
 include/uapi/linux/pci_regs.h |  25 +++++
 3 files changed, 210 insertions(+), 1 deletion(-)
drivers/pci/pci.c +184 -0
index 0b29ec6e8e5e..cf4fc027565e 100644
@@ -5034,6 +5034,12 @@ static int pci_dev_reset_slot_function(struct pci_dev *dev, bool probe)
 	return pci_reset_hotplug_slot(dev->slot->hotplug, probe);
 }
 
+static u16 cxl_device_dvsec(struct pci_dev *dev)
+{
+	return pci_find_dvsec_capability(dev, PCI_VENDOR_ID_CXL,
+					 PCI_DVSEC_CXL_DEV);
+}
+
 static u16 cxl_port_dvsec(struct pci_dev *dev)
 {
 	return pci_find_dvsec_capability(dev, PCI_VENDOR_ID_CXL,
@@ -5124,6 +5130,183 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)
 	return rc;
 }
 
+static int cxl_reset_prepare(struct pci_dev *dev, u16 dvsec)
+{
+	u16 reg, val, cap;
+	int rc;
+	u32 timeout_us = 100, timeout_tot_us = 10000;
+
+	/*
+	 * Wait for any pending transactions.
+	 * Assuming this does cxl.io stuff.
+	 */
+	if (!pci_wait_for_pending_transaction(dev))
+		pci_err(dev, "timed out waiting for pending transaction; performing cxl reset anyway\n");
+
+	/*
+	 * Disable caching and then write back and invalidate lines.
+	 */
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCAP,
+				  &cap);
+	if (rc)
+		return rc;
+
+	if (!(cap & PCI_DVSEC_CXL_DEVCAP_CACHE_CAPABLE))
+		return 0;
+
+	/*
+	 * Disable cache.
+	 * WB and invalidate cahce if capability is advertised.
+	 */
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+				  &reg);
+	if (rc)
+		return rc;
+	val = reg | PCI_DVSEC_CXL_DEVCTL2_DISABLE_CACHING;
+
+	if (cap & PCI_DVSEC_CXL_DEVCAP_CACHE_WB_INVALIDATE)
+		val = reg | PCI_DVSEC_CXL_DEVCTL2_INIT_CACHE_WB_INVALIDATE;
+	pci_write_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+			      val);
+
+	/*
+	 * From Section 9.6: "Software may leverage the cache size reported in
+	 * the DVSEC CXL Capability2 register to compute a suitable timeout
+	 * value".
+	 * Given there is no conversion factor for cache size -> timeout,
+	 * setting timer for default 10ms.
+	 */
+	do {
+		if (timeout_tot_us < 0)
+			return -ETIMEDOUT;
+		usleep_range(timeout_us, timeout_us+1);
+		timeout_tot_us -= timeout_us;
+		rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+					  &reg);
+		if (rc)
+			return rc;
+	} while (!(reg & PCI_DVSEC_CXL_DEVSTATUS2_CACHE_INVALID));
+
+	return 0;
+}
+
+/**
+ * cxl_reset_init - initiate a cxl reset
+ * @dev: device to reset
+ *
+ * Initiate a cxl reset.
+ */
+static int cxl_reset_init(struct pci_dev *dev, u16 dvsec)
+{
+	u16 reg, val;
+	u32 timeout_ms;
+	int rc;
+	u32 reset_timeouts_ms[] = {10, 100, 1000, 10000, 100000};
+
+	/*
+	 * Check if CXL Reset MEM CLR is supported.
+	 * TODO: Add check to do MEM CLR only if requested.
+	 */
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCAP,
+				  &reg);
+	if (rc)
+		return rc;
+
+	if (reg & PCI_DVSEC_CXL_DEVCAP_CXL_RST_MEM_CLR) {
+		rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+					  &reg);
+		if (rc)
+			return rc;
+
+		val = reg | PCI_DVSEC_CXL_DEVCTL2_CXL_RST_MEM_CLR_ENABLE;
+		pci_write_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+				      val);
+	}
+
+	/*
+	 * Read timeout value
+	 */
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCAP,
+				  &reg);
+	if (rc)
+		return rc;
+	timeout_ms = reset_timeouts_ms[FIELD_GET(PCI_DVSEC_CXL_DEVCAP_CXL_RST_TIMEOUT_MASK, reg)];
+
+	/*
+	 * Write reset config
+	 */
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+				  &reg);
+	if (rc)
+		return rc;
+
+	val = reg | PCI_DVSEC_CXL_DEVCTL2_CXL_INIT_RST;
+	pci_write_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+			      val);
+
+	/*
+	 *  Wait till timeout and then check reset status is complete.
+	 */
+	msleep(timeout_ms);
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVSTATUS2,
+				  &reg);
+	if (rc)
+		return rc;
+	if (reg & PCI_DVSEC_CXL_DEVSTATUS2_RST_ERR ||
+	    ~reg & PCI_DVSEC_CXL_DEVSTATUS2_RST_COMPLETE)
+		return -ETIMEDOUT;
+
+	/*
+	 * Revert cashing disable.
+	 */
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+				  &reg);
+	if (rc)
+		return rc;
+	val = (reg & (~PCI_DVSEC_CXL_DEVCTL2_DISABLE_CACHING));
+	pci_write_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+			      val);
+
+	return 0;
+}
+
+/**
+ * cxl_reset - initiate a cxl reset
+ * @dev: device to reset
+ * @probe: if true, return 0 if device can be reset this way
+ *
+ * Initiate a cxl reset on @dev.
+ */
+static int cxl_reset(struct pci_dev *dev, bool probe)
+{
+	u16 dvsec, reg;
+	int rc;
+
+	dvsec = cxl_device_dvsec(dev);
+	if (!dvsec)
+		return -ENOTTY;
+
+	/*
+	 * Check if CXL Reset is supported.
+	 */
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCAP,
+				  &reg);
+	if (rc)
+		return -ENOTTY;
+
+	if (~(reg & PCI_DVSEC_CXL_DEVCAP_CXL_RST))
+		return -ENOTTY;
+
+	if (probe)
+		return 0;
+
+	rc = cxl_reset_prepare(dev, dvsec);
+	if (rc)
+		return rc;
+
+	return cxl_reset_init(dev, dvsec);
+}
+
 void pci_dev_lock(struct pci_dev *dev)
 {
 	/* block PM suspend, driver probe, etc. */
@@ -5210,6 +5393,7 @@ static const struct pci_reset_fn_method pci_reset_fn_methods[] = {
 	{ pci_dev_acpi_reset, .name = "acpi" },
 	{ pcie_reset_flr, .name = "flr" },
 	{ pci_af_flr, .name = "af_flr" },
+	{ cxl_reset, .name = "cxl_reset" },
 	{ pci_pm_reset, .name = "pm" },
 	{ pci_reset_bus_function, .name = "bus" },
 	{ cxl_reset_bus_function, .name = "cxl_bus" },
include/linux/pci.h +1 -1
index db9b47ce3eef..60bacca8e379 100644
@@ -51,7 +51,7 @@
 			       PCI_STATUS_PARITY)
 
 /* Number of reset methods used in pci_reset_fn_methods array in pci.c */
-#define PCI_NUM_RESET_METHODS 8
+#define PCI_NUM_RESET_METHODS 9
 
 #define PCI_RESET_PROBE		true
 #define PCI_RESET_DO_RESET	false
include/uapi/linux/pci_regs.h +25 -0
index 1601c7ed5fab..ec5b408a0789 100644
@@ -1209,6 +1209,31 @@
 #define PCI_DOE_DATA_OBJECT_DISC_RSP_3_NEXT_INDEX	0xff000000
 
 /* Compute Express Link (CXL r3.1, sec 8.1.5) */
+#define PCI_DVSEC_CXL_DEV				0
+#define PCI_DVSEC_CXL_DEVCAP				0x0a
+#define PCI_DVSEC_CXL_DEVCAP_CACHE_CAPABLE		0x00000001
+#define PCI_DVSEC_CXL_DEVCAP_CACHE_WB_INVALIDATE	0x00000040
+#define PCI_DVSEC_CXL_DEVCAP_CXL_RST			0x00000080
+#define PCI_DVSEC_CXL_DEVCAP_CXL_RST_TIMEOUT_IND	0x8
+#define PCI_DVSEC_CXL_DEVCAP_CXL_RST_TIMEOUT_MASK	0x00000700
+#define PCI_DVSEC_CXL_DEVCAP_CXL_RST_MEM_CLR		0x00000800
+#define PCI_DVSEC_CXL_DEVCTL				0x0c
+#define PCI_DVSEC_CXL_DEVCTL2				0x10
+#define PCI_DVSEC_CXL_DEVCTL2_DISABLE_CACHING		0x1
+#define PCI_DVSEC_CXL_DEVCTL2_INIT_CACHE_WB_INVALIDATE	0x2
+#define PCI_DVSEC_CXL_DEVCTL2_CXL_INIT_RST		0x4
+#define PCI_DVSEC_CXL_DEVCTL2_CXL_RST_MEM_CLR_ENABLE	0x8
+#define PCI_DVSEC_CXL_DEVSTATUS2			0x12
+#define PCI_DVSEC_CXL_DEVSTATUS2_CACHE_INVALID		0x1
+#define PCI_DVSEC_CXL_DEVSTATUS2_RST_COMPLETE		0x2
+#define PCI_DVSEC_CXL_DEVSTATUS2_RST_ERR		0x4
+#define PCI_DVSEC_CXL_DEVCAP2				0x16
+#define PCI_DVSEC_CXL_DEVCAP2_CACHE_SIZE_UNIT		0x0000000F
+#define PCI_DVSEC_CXL_DEVCAP2_CACHE_SIZE_UNIT_0		0x0
+#define PCI_DVSEC_CXL_DEVCAP2_CACHE_SIZE_UNIT_1		0x40
+#define PCI_DVSEC_CXL_DEVCAP2_CACHE_SIZE_UNIT_2		0x400
+#define PCI_DVSEC_CXL_DEVCAP2_CACHE_SIZE(x)		(((x) & 0x0000FF00) >> 8)
+
 #define PCI_DVSEC_CXL_PORT				3
 #define PCI_DVSEC_CXL_PORT_CTL				0x0c
 #define PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR		0x00000001
Signature · 1 line
2.25.1
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Srirangan,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on pci/next]
[also build test WARNING on pci/for-linus cxl/next linus/master v6.13-rc2 next-20241213] [cannot apply to cxl/pending]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Srirangan-Madhavan/cxl-add-support-for-cxl-reset/20241213-154303
base:   https://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git next
patch link:    https://lore.kernel.org/r/20241213074143.374-1-smadhavan%40nvidia.com
patch subject: [RFC PATCH v1] cxl: add support for cxl reset
config: s390-randconfig-002-20241214 (https://download.01.org/0day-ci/archive/20241214/[email protected]/config)
compiler: s390-linux-gcc (GCC) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241214/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/pci/pci.c:5197: warning: Function parameter or struct member 'dvsec' not described in 'cxl_reset_init'

vim +5197 drivers/pci/pci.c

5189
5190 /**
5191 * cxl_reset_init - initiate a cxl reset
5192 * @dev: device to reset
5193 *
5194 * Initiate a cxl reset.
5195 */
5196 static int cxl_reset_init(struct pci_dev *dev, u16 dvsec)

> 5197	{

5198 u16 reg, val;
5199 u32 timeout_ms;
5200 int rc;
5201 u32 reset_timeouts_ms[] = {10, 100, 1000, 10000, 100000};
5202
5203 /*
5204 * Check if CXL Reset MEM CLR is supported.
5205 * TODO: Add check to do MEM CLR only if requested.
5206 */
5207 rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCAP,
5208 &reg);
5209 if (rc)
5210 return rc;
5211
5212 if (reg & PCI_DVSEC_CXL_DEVCAP_CXL_RST_MEM_CLR) {
5213 rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
5214 &reg);
5215 if (rc)
5216 return rc;
5217
5218 val = reg | PCI_DVSEC_CXL_DEVCTL2_CXL_RST_MEM_CLR_ENABLE;
5219 pci_write_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
5220 val);
5221 }
5222
5223 /*
5224 * Read timeout value
5225 */
5226 rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCAP,
5227 &reg);
5228 if (rc)
5229 return rc;
5230 timeout_ms = reset_timeouts_ms[FIELD_GET(PCI_DVSEC_CXL_DEVCAP_CXL_RST_TIMEOUT_MASK, reg)];
5231
5232 /*
5233 * Write reset config
5234 */
5235 rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
5236 &reg);
5237 if (rc)
5238 return rc;
5239
5240 val = reg | PCI_DVSEC_CXL_DEVCTL2_CXL_INIT_RST;
5241 pci_write_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
5242 val);
5243
5244 /*
5245 * Wait till timeout and then check reset status is complete.
5246 */
5247 msleep(timeout_ms);
5248 rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVSTATUS2,
5249 &reg);
5250 if (rc)
5251 return rc;
5252 if (reg & PCI_DVSEC_CXL_DEVSTATUS2_RST_ERR ||
5253 ~reg & PCI_DVSEC_CXL_DEVSTATUS2_RST_COMPLETE)
5254 return -ETIMEDOUT;
5255
5256 /*
5257 * Revert cashing disable.
5258 */
5259 rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
5260 &reg);
5261 if (rc)
5262 return rc;
5263 val = (reg & (~PCI_DVSEC_CXL_DEVCTL2_DISABLE_CACHING));
5264 pci_write_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
5265 val);
5266
5267 return 0;
5268 }
5269

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to Srirangan Madhavan
Quoted 3 lines · Srirangan Madhavan

Currently, there is a lack of support for the CXL Reset method for cxl devices. This change adds the support and implements the common reset steps as laid out by the CXL Spec v3.1 Sections 9.6 & 9.7.

I'm not sure what the use case for this support is. Do you have a use case in mind?

What happens if there are current cxl regions mapped to the device being reset? I don't think it is enough to flush the caches. Section 9.7 talks about system software requirements for the HDMs. How are those requirements met with this patch?

I think some more detail on the use case and how this functionality is to be used will clear up some of these questions.

Ira

Quoted 279 lines

For devices that support CXL Reset, cache lines are disabled and WB+I is asserted. After waiting for cache invalid status, mem clr bit is asserted and finally reset is initiated. After the recommended timeout period, the reset status is checked and returned.

The patch has not been tested completely as the infrastructure is not yet in place. As this is my first kernel patch, I am looking to get the community feedback through the RFC ahead of time.

---
 drivers/pci/pci.c             | 184 ++++++++++++++++++++++++++++++++++
 include/linux/pci.h           |   2 +-
 include/uapi/linux/pci_regs.h |  25 +++++
 3 files changed, 210 insertions(+), 1 deletion(-)
drivers/pci/pci.c +184 -0
index 0b29ec6e8e5e..cf4fc027565e 100644
@@ -5034,6 +5034,12 @@ static int pci_dev_reset_slot_function(struct pci_dev *dev, bool probe)
 	return pci_reset_hotplug_slot(dev->slot->hotplug, probe);
 }
 
+static u16 cxl_device_dvsec(struct pci_dev *dev)
+{
+	return pci_find_dvsec_capability(dev, PCI_VENDOR_ID_CXL,
+					 PCI_DVSEC_CXL_DEV);
+}
+
 static u16 cxl_port_dvsec(struct pci_dev *dev)
 {
 	return pci_find_dvsec_capability(dev, PCI_VENDOR_ID_CXL,
@@ -5124,6 +5130,183 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)
 	return rc;
 }
 
+static int cxl_reset_prepare(struct pci_dev *dev, u16 dvsec)
+{
+	u16 reg, val, cap;
+	int rc;
+	u32 timeout_us = 100, timeout_tot_us = 10000;
+
+	/*
+	 * Wait for any pending transactions.
+	 * Assuming this does cxl.io stuff.
+	 */
+	if (!pci_wait_for_pending_transaction(dev))
+		pci_err(dev, "timed out waiting for pending transaction; performing cxl reset anyway\n");
+
+	/*
+	 * Disable caching and then write back and invalidate lines.
+	 */
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCAP,
+				  &cap);
+	if (rc)
+		return rc;
+
+	if (!(cap & PCI_DVSEC_CXL_DEVCAP_CACHE_CAPABLE))
+		return 0;
+
+	/*
+	 * Disable cache.
+	 * WB and invalidate cahce if capability is advertised.
+	 */
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+				  &reg);
+	if (rc)
+		return rc;
+	val = reg | PCI_DVSEC_CXL_DEVCTL2_DISABLE_CACHING;
+
+	if (cap & PCI_DVSEC_CXL_DEVCAP_CACHE_WB_INVALIDATE)
+		val = reg | PCI_DVSEC_CXL_DEVCTL2_INIT_CACHE_WB_INVALIDATE;
+	pci_write_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+			      val);
+
+	/*
+	 * From Section 9.6: "Software may leverage the cache size reported in
+	 * the DVSEC CXL Capability2 register to compute a suitable timeout
+	 * value".
+	 * Given there is no conversion factor for cache size -> timeout,
+	 * setting timer for default 10ms.
+	 */
+	do {
+		if (timeout_tot_us < 0)
+			return -ETIMEDOUT;
+		usleep_range(timeout_us, timeout_us+1);
+		timeout_tot_us -= timeout_us;
+		rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+					  &reg);
+		if (rc)
+			return rc;
+	} while (!(reg & PCI_DVSEC_CXL_DEVSTATUS2_CACHE_INVALID));
+
+	return 0;
+}
+
+/**
+ * cxl_reset_init - initiate a cxl reset
+ * @dev: device to reset
+ *
+ * Initiate a cxl reset.
+ */
+static int cxl_reset_init(struct pci_dev *dev, u16 dvsec)
+{
+	u16 reg, val;
+	u32 timeout_ms;
+	int rc;
+	u32 reset_timeouts_ms[] = {10, 100, 1000, 10000, 100000};
+
+	/*
+	 * Check if CXL Reset MEM CLR is supported.
+	 * TODO: Add check to do MEM CLR only if requested.
+	 */
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCAP,
+				  &reg);
+	if (rc)
+		return rc;
+
+	if (reg & PCI_DVSEC_CXL_DEVCAP_CXL_RST_MEM_CLR) {
+		rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+					  &reg);
+		if (rc)
+			return rc;
+
+		val = reg | PCI_DVSEC_CXL_DEVCTL2_CXL_RST_MEM_CLR_ENABLE;
+		pci_write_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+				      val);
+	}
+
+	/*
+	 * Read timeout value
+	 */
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCAP,
+				  &reg);
+	if (rc)
+		return rc;
+	timeout_ms = reset_timeouts_ms[FIELD_GET(PCI_DVSEC_CXL_DEVCAP_CXL_RST_TIMEOUT_MASK, reg)];
+
+	/*
+	 * Write reset config
+	 */
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+				  &reg);
+	if (rc)
+		return rc;
+
+	val = reg | PCI_DVSEC_CXL_DEVCTL2_CXL_INIT_RST;
+	pci_write_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+			      val);
+
+	/*
+	 *  Wait till timeout and then check reset status is complete.
+	 */
+	msleep(timeout_ms);
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVSTATUS2,
+				  &reg);
+	if (rc)
+		return rc;
+	if (reg & PCI_DVSEC_CXL_DEVSTATUS2_RST_ERR ||
+	    ~reg & PCI_DVSEC_CXL_DEVSTATUS2_RST_COMPLETE)
+		return -ETIMEDOUT;
+
+	/*
+	 * Revert cashing disable.
+	 */
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+				  &reg);
+	if (rc)
+		return rc;
+	val = (reg & (~PCI_DVSEC_CXL_DEVCTL2_DISABLE_CACHING));
+	pci_write_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCTL2,
+			      val);
+
+	return 0;
+}
+
+/**
+ * cxl_reset - initiate a cxl reset
+ * @dev: device to reset
+ * @probe: if true, return 0 if device can be reset this way
+ *
+ * Initiate a cxl reset on @dev.
+ */
+static int cxl_reset(struct pci_dev *dev, bool probe)
+{
+	u16 dvsec, reg;
+	int rc;
+
+	dvsec = cxl_device_dvsec(dev);
+	if (!dvsec)
+		return -ENOTTY;
+
+	/*
+	 * Check if CXL Reset is supported.
+	 */
+	rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_DEVCAP,
+				  &reg);
+	if (rc)
+		return -ENOTTY;
+
+	if (~(reg & PCI_DVSEC_CXL_DEVCAP_CXL_RST))
+		return -ENOTTY;
+
+	if (probe)
+		return 0;
+
+	rc = cxl_reset_prepare(dev, dvsec);
+	if (rc)
+		return rc;
+
+	return cxl_reset_init(dev, dvsec);
+}
+
 void pci_dev_lock(struct pci_dev *dev)
 {
 	/* block PM suspend, driver probe, etc. */
@@ -5210,6 +5393,7 @@ static const struct pci_reset_fn_method pci_reset_fn_methods[] = {
 	{ pci_dev_acpi_reset, .name = "acpi" },
 	{ pcie_reset_flr, .name = "flr" },
 	{ pci_af_flr, .name = "af_flr" },
+	{ cxl_reset, .name = "cxl_reset" },
 	{ pci_pm_reset, .name = "pm" },
 	{ pci_reset_bus_function, .name = "bus" },
 	{ cxl_reset_bus_function, .name = "cxl_bus" },
include/linux/pci.h +1 -1
index db9b47ce3eef..60bacca8e379 100644
@@ -51,7 +51,7 @@
 			       PCI_STATUS_PARITY)
 
 /* Number of reset methods used in pci_reset_fn_methods array in pci.c */
-#define PCI_NUM_RESET_METHODS 8
+#define PCI_NUM_RESET_METHODS 9
 
 #define PCI_RESET_PROBE		true
 #define PCI_RESET_DO_RESET	false
include/uapi/linux/pci_regs.h +25 -0
index 1601c7ed5fab..ec5b408a0789 100644
@@ -1209,6 +1209,31 @@
 #define PCI_DOE_DATA_OBJECT_DISC_RSP_3_NEXT_INDEX	0xff000000
 
 /* Compute Express Link (CXL r3.1, sec 8.1.5) */
+#define PCI_DVSEC_CXL_DEV				0
+#define PCI_DVSEC_CXL_DEVCAP				0x0a
+#define PCI_DVSEC_CXL_DEVCAP_CACHE_CAPABLE		0x00000001
+#define PCI_DVSEC_CXL_DEVCAP_CACHE_WB_INVALIDATE	0x00000040
+#define PCI_DVSEC_CXL_DEVCAP_CXL_RST			0x00000080
+#define PCI_DVSEC_CXL_DEVCAP_CXL_RST_TIMEOUT_IND	0x8
+#define PCI_DVSEC_CXL_DEVCAP_CXL_RST_TIMEOUT_MASK	0x00000700
+#define PCI_DVSEC_CXL_DEVCAP_CXL_RST_MEM_CLR		0x00000800
+#define PCI_DVSEC_CXL_DEVCTL				0x0c
+#define PCI_DVSEC_CXL_DEVCTL2				0x10
+#define PCI_DVSEC_CXL_DEVCTL2_DISABLE_CACHING		0x1
+#define PCI_DVSEC_CXL_DEVCTL2_INIT_CACHE_WB_INVALIDATE	0x2
+#define PCI_DVSEC_CXL_DEVCTL2_CXL_INIT_RST		0x4
+#define PCI_DVSEC_CXL_DEVCTL2_CXL_RST_MEM_CLR_ENABLE	0x8
+#define PCI_DVSEC_CXL_DEVSTATUS2			0x12
+#define PCI_DVSEC_CXL_DEVSTATUS2_CACHE_INVALID		0x1
+#define PCI_DVSEC_CXL_DEVSTATUS2_RST_COMPLETE		0x2
+#define PCI_DVSEC_CXL_DEVSTATUS2_RST_ERR		0x4
+#define PCI_DVSEC_CXL_DEVCAP2				0x16
+#define PCI_DVSEC_CXL_DEVCAP2_CACHE_SIZE_UNIT		0x0000000F
+#define PCI_DVSEC_CXL_DEVCAP2_CACHE_SIZE_UNIT_0		0x0
+#define PCI_DVSEC_CXL_DEVCAP2_CACHE_SIZE_UNIT_1		0x40
+#define PCI_DVSEC_CXL_DEVCAP2_CACHE_SIZE_UNIT_2		0x400
+#define PCI_DVSEC_CXL_DEVCAP2_CACHE_SIZE(x)		(((x) & 0x0000FF00) >> 8)
+
 #define PCI_DVSEC_CXL_PORT				3
 #define PCI_DVSEC_CXL_PORT_CTL				0x0c
 #define PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR		0x00000001
Signature · 1 line
2.25.1
#3 lore raw

External email: Use caution opening links or attachments

Hello Ira!

Thank you so much for your comments. I’ve added my response below. Apologies for the slight delay.

Quoted 7 lines
Quoted 3 lines · Srirangan Madhavan

Currently, there is a lack of support for the CXL Reset method for cxl devices. This change adds the support and implements the common reset steps as laid out by the CXL Spec v3.1 Sections 9.6 & 9.7.

I'm not sure what the use case for this support is. Do you have a use case in mind?

I think that this change would be broadly useful due to few interrelated reasons. Without the change, even if devices are capable of CXL reset, users will not be able to access the method. Considering the changes to support type 2 devices are being added now, there will be more such devices. Adding the support would enable cases where more fine grained reset is required than bus reset methods/conventional reset. And FLR does not affect the CXL.cache/CXL.mem protocols. Spec also mentions cases like FM uses CXL reset for rebinding, error recovery. So I think that adding this change would be useful for all devices with the capability.

Quoted 4 lines

What happens if there are current cxl regions mapped to the device being reset? I don't think it is enough to flush the caches. Section 9.7 talks about system software requirements for the HDMs. How are those requirements met with this patch?

Sorry for not covering that. Your & other maintainer input here would be much appreciated. Since “system software” here is not very specifc, my proposal here is not very specific as well.

Considering different platforms might have specific operations to effectively clear the regions, some type of optional helper routines in the CXL core would be appropriate. This way if required, a Type 2 device driver for example can choose to use it if required.

Thank you.

Regards,
Srirangan.

A message that is not in the archive (sent off-list or not yet received): PH7PR12MB796800828DEC6E60D7C7C0F1C3072@PH7PR12MB7968.namprd12.prod.outlook.com

#5 lore raw
Quoted 12 lines · Srirangan Madhavan

External email: Use caution opening links or attachments

Hello Ira!

Thank you so much for your comments. I’ve added my response below. Apologies for the slight delay.

Quoted 4 lines
Quoted 3 lines · Srirangan Madhavan

Currently, there is a lack of support for the CXL Reset method for cxl devices. This change adds the support and implements the common reset steps as laid out by the CXL Spec v3.1 Sections 9.6 & 9.7.

Quoted 8 lines
Quoted 2 lines

I'm not sure what the use case for this support is. Do you have a use case in mind?

I think that this change would be broadly useful due to few interrelated reasons. Without the change, even if devices are capable of CXL reset, users will not be able to access the method. Considering the changes to support type 2 devices are being added now, there will be more such devices.

I see use cases for type 2 for sure.

Quoted 12 lines

Adding the support would enable cases where more fine grained reset is required than bus reset methods/conventional reset. And FLR does not affect the CXL.cache/CXL.mem protocols. Spec also mentions cases like FM uses CXL reset for rebinding, error recovery. So I think that adding this change would be useful for all devices with the capability.

Quoted 4 lines

What happens if there are current cxl regions mapped to the device being reset? I don't think it is enough to flush the caches. Section 9.7 talks about system software requirements for the HDMs. How are those requirements met with this patch?

Quoted 8 lines

Sorry for not covering that. Your & other maintainer input here would be much appreciated. Since “system software” here is not very specifc, my proposal here is not very specific as well.

Considering different platforms might have specific operations to effectively clear the regions, some type of optional helper routines in the CXL core would be appropriate. This way if required, a Type 2 device driver for example can choose to use it if required.

There are a couple of ways to go here. For memory devices I think this is on the user. But I don't think that is a primary use case as I'm still unclear of why a user would need to do any reset of the memory device.

For type 2 devices I think the use case will be device dependant. The specific driver in that case will need to tear down their regions (with or without user space coordination) and then issue the reset.

All that said I think this patch belongs with a series which implements some device support. Or is more general for type 2. Which is what Alejandro (cc'ed) is working on.

https://lore.kernel.org/linux-cxl/[email protected]/

Ira

#6 lore raw
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

What happens if there are current cxl regions mapped to the device being reset? I don't think it is enough to flush the caches. Section 9.7 talks about system software requirements for the HDMs. How are those requirements met with this patch?

Considering different platforms might have specific operations to effectively clear the regions, some type of optional helper routines in the CXL core would be appropriate. This way if required, a Type 2 device driver for example can choose to use it if required.

Quoted 7 lines

There are a couple of ways to go here. For memory devices I think this is on the user. But I don't think that is a primary use case as I'm still unclear of why a user would need to do any reset of the memory device.

For type 2 devices I think the use case will be device dependant. The specific driver in that case will need to tear down their regions (with or without user space coordination) and then issue the reset.

That makes sense. So in either case, based on this logic, I am assuming it is out of scope for this patch to remove the the memory regions.

Quoted 3 lines

All that said I think this patch belongs with a series which implements some device support. Or is more general for type 2. Which is what Alejandro (cc'ed) is working on.

Thanks for the feedback.
@Alejandro, if you have any thoughts on this, kindly let me know. I can proceed based on that.

Regards,
Srirangan.

#7 lore raw
Quoted 18 lines · Srirangan Madhavan
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

What happens if there are current cxl regions mapped to the device being reset? I don't think it is enough to flush the caches. Section 9.7 talks about system software requirements for the HDMs. How are those requirements met with this patch?

Considering different platforms might have specific operations to effectively clear the regions, some type of optional helper routines in the CXL core would be appropriate. This way if required, a Type 2 device driver for example can choose to use it if required.

Quoted 7 lines

There are a couple of ways to go here. For memory devices I think this is on the user. But I don't think that is a primary use case as I'm still unclear of why a user would need to do any reset of the memory device.

For type 2 devices I think the use case will be device dependant. The specific driver in that case will need to tear down their regions (with or without user space coordination) and then issue the reset.

That makes sense. So in either case, based on this logic, I am assuming it is out of scope for this patch to remove the the memory regions.

Quoted 6 lines
Quoted 3 lines

All that said I think this patch belongs with a series which implements some device support. Or is more general for type 2. Which is what Alejandro (cc'ed) is working on.

Thanks for the feedback.
@Alejandro, if you have any thoughts on this, kindly let me know. I can proceed based on that.

Sorry for the late reply.

I thought about this some time ago and shared my concerns about current CXL error management which should lead to a CXL reset in some situations.

Apart from that not being there yet, how to do the reset has the problem of the restoration of device HDM registers. I think the kernel CXL core should keep that information before the reset, and restore them back when the reset is done. Other possibility is to just unroll the HDM which implies involving all CXL switches in the path plus the CXL Root Complex. I do not know if there are other considerations for supporting restoration. For a, I hope, near future, CXL.cache should be included here, but it is not clear yet how to support it, so even less clear for a reset.

Of course, CXL errors can be more problematic than with PCIe, and a reset could imply the system (some cores) being stuck, so all this is quite tricky.

In the specific case I'm adding along with the generic CXL Type2 support, if the system is stable after a detected CXL error, the device HDM should be configured after the reset, by implicitly some code linked to the reset triggered by the driver, what could be quite similar to the initial CXL probe, but if restoration is an option, the CXL core keeping the same ranges and somehow giving that back to the device.

I hope this helps somehow.

Thank you

Alejandro

Quoted 4 lines

Regards,
Srirangan.