Skip to contentSkip to conversation

CXL: Add a loadable module for address translation

PATCH Superseded [email protected] 4 participants 14 messages Aug 4, 2025 – Aug 29, 2025

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

cover CXL: Add a loadable module for address translation

From: Alison Schofield <[email protected]>

This set doesn't have a base commit. It is based on cxl/next plus the Poison by Region Offset patchset [1], plus the fix to Limit XOR map application based on host bridge ways [2] which this test module exposed.

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

This series refactors CXL address translation code to support testing and adds a dedicated test module for validation of the translation calculations.

The work is presented in 3 patches:
1. Extracts the core translation logic into standalone, testable functions.
2. Provides access to XOR interleave calculations without exposing internal CXIMS structures to test modules.
3. Adds the test module that validates both Host to Device, and Device to Host address translations.

Accessing the core functions in the test module was implemented by adding the needed core/region functions to cxl_core_exports.c and adding a similar 'cxl_acpi_exports.c' to access the cxl/acpi.c funcs.

The companion CXL Unit Test script will be posted separately.

Alison Schofield (3):
cxl/region: Refactor address translation funcs for testing cxl/acpi: Make the XOR calculations available for testing cxl/test: Add cxl_translate module for address translation testing

 drivers/cxl/acpi.c                     |  51 ++++-
 drivers/cxl/core/region.c              | 145 +++++++-----
 drivers/cxl/cxl.h                      |   3 +
 include/linux/acpi.h                   |   7 +
 tools/testing/cxl/Kbuild               |   1 +
 tools/testing/cxl/cxl_acpi_exports.c   |  13 ++
 tools/testing/cxl/cxl_core_exports.c   |  11 +
 tools/testing/cxl/test/Kbuild          |   2 +
 tools/testing/cxl/test/cxl_translate.c | 298 +++++++++++++++++++++++++
 9 files changed, 467 insertions(+), 64 deletions(-)
 create mode 100644 tools/testing/cxl/cxl_acpi_exports.c
 create mode 100644 tools/testing/cxl/test/cxl_translate.c
Signature · 1 line
2.37.3
author maintainer
#1 lore raw

PATCH 1/3 cxl/region: Refactor address translation funcs for testing

From: Alison Schofield <[email protected]>

In preparation for adding a test module that exercises the address translation calculations, extract the core calculations into stand- alone functions that operate on base parameters without dependencies on struct cxl_region.

This refactoring enables unit testing of the address translation logic with controlled inputs, while maintaining identical functionality in the existing code paths.

The moved code has only one change. In the new cxl_calculate_position() eiw_to_ways(eiw, &ways) replaces the prior usage of p->interleave_ways, since the new function cannot depend upon struct cxl_region_params.

---
 drivers/cxl/core/region.c | 145 +++++++++++++++++++++++---------------
 drivers/cxl/cxl.h         |   3 +
 2 files changed, 90 insertions(+), 58 deletions(-)
drivers/cxl/core/region.c +87 -58
index 281a31df686f..ccee5049d700 100644
@@ -2929,13 +2929,91 @@ static bool has_spa_to_hpa(struct cxl_root_decoder *cxlrd)
 	return cxlrd->ops && cxlrd->ops->spa_to_hpa;
 }
 
+u64 cxl_calculate_dpa_offset(u64 hpa_offset, u8 eiw, u16 eig)
+{
+	u64 dpa_offset, bits_lower, bits_upper, temp;
+
+	/*
+	 * DPA offset: CXL Spec 3.2 Section 8.2.4.20.13
+	 * Lower bits [IG+7:0] pass through unchanged
+	 * (eiw < 8)
+	 *	Per spec: DPAOffset[51:IG+8] = (HPAOffset[51:IG+IW+8] >> IW)
+	 *	Clear the position bits to isolate upper section, then
+	 *	reverse the left shift by eiw that occurred during DPA->HPA
+	 * (eiw >= 8)
+	 *	Per spec: DPAOffset[51:IG+8] = HPAOffset[51:IG+IW] / 3
+	 *	Extract upper bits from the correct bit range and divide by 3
+	 *	to recover the original DPA upper bits
+	 */
+	bits_lower = hpa_offset & GENMASK_ULL(eig + 7, 0);
+	if (eiw < 8) {
+		temp = hpa_offset &= ~((u64)GENMASK(eig + eiw + 8 - 1, 0));
+		dpa_offset = temp >> eiw;
+	} else {
+		bits_upper = div64_u64(hpa_offset >> (eig + eiw), 3);
+		dpa_offset = bits_upper << (eig + 8);
+	}
+	dpa_offset |= bits_lower;
+
+	return dpa_offset;
+}
+
+int cxl_calculate_position(u64 hpa_offset, u8 eiw, u16 eig)
+{
+	unsigned int ways = 0;
+	u64 shifted, rem;
+	int pos;
+
+	/*
+	 * Interleave position: CXL Spec 3.2 Section 8.2.4.20.13
+	 * eiw < 8
+	 *	Position is in the IW bits at HPA_OFFSET[IG+8+IW-1:IG+8].
+	 *	Per spec "remove IW bits starting with bit position IG+8"
+	 * eiw >= 8
+	 *	Position is not explicitly stored in HPA_OFFSET bits. It is
+	 *	derived from the modulo operation of the upper bits using
+	 *	the total number of interleave ways.
+	 */
+	if (eiw < 8) {
+		pos = (hpa_offset >> (eig + 8)) & GENMASK(eiw - 1, 0);
+	} else {
+		shifted = hpa_offset >> (eig + 8);
+		eiw_to_ways(eiw, &ways);
+		div64_u64_rem(shifted, ways, &rem);
+		pos = rem;
+	}
+
+	return pos;
+}
+
+u64 cxl_calculate_hpa_offset(u64 dpa_offset, int pos, u8 eiw, u16 eig)
+{
+	u64 mask_upper, hpa_offset, bits_upper;
+
+	mask_upper = GENMASK_ULL(51, eig + 8);
+
+	if (eiw < 8) {
+		hpa_offset = (dpa_offset & mask_upper) << eiw;
+		hpa_offset |= pos << (eig + 8);
+	} else {
+		bits_upper = (dpa_offset & mask_upper) >> (eig + 8);
+		bits_upper = bits_upper * 3;
+		hpa_offset = ((bits_upper << (eiw - 8)) + pos) << (eig + 8);
+	}
+
+	/* The lower bits remain unchanged */
+	hpa_offset |= dpa_offset & GENMASK_ULL(eig + 7, 0);
+
+	return hpa_offset;
+}
+
 u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
 		   u64 dpa)
 {
 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
-	u64 dpa_offset, hpa_offset, bits_upper, mask_upper, hpa;
 	struct cxl_region_params *p = &cxlr->params;
 	struct cxl_endpoint_decoder *cxled = NULL;
+	u64 dpa_offset, hpa_offset, hpa;
 	u16 eig = 0;
 	u8 eiw = 0;
 	int pos;
@@ -2965,19 +3043,8 @@ u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
 	/* Remove the dpa base */
 	dpa_offset = dpa - cxl_dpa_resource_start(cxled);
 
-	mask_upper = GENMASK_ULL(51, eig + 8);
-
-	if (eiw < 8) {
-		hpa_offset = (dpa_offset & mask_upper) << eiw;
-		hpa_offset |= pos << (eig + 8);
-	} else {
-		bits_upper = (dpa_offset & mask_upper) >> (eig + 8);
-		bits_upper = bits_upper * 3;
-		hpa_offset = ((bits_upper << (eiw - 8)) + pos) << (eig + 8);
-	}
-
-	/* The lower bits remain unchanged */
-	hpa_offset |= dpa_offset & GENMASK_ULL(eig + 7, 0);
+	/* this chunk was moved, maybe move comment too */
+	hpa_offset = cxl_calculate_hpa_offset(dpa_offset, pos, eiw, eig);
 
 	/* Apply the hpa_offset to the region base address */
 	hpa = hpa_offset + p->res->start + p->cache_size;
@@ -3011,8 +3078,6 @@ static int region_offset_to_dpa_result(struct cxl_region *cxlr, u64 offset,
 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
 	struct cxl_endpoint_decoder *cxled;
 	u64 hpa, hpa_offset, dpa_offset;
-	u64 bits_upper, bits_lower;
-	u64 shifted, rem, temp;
 	u16 eig = 0;
 	u8 eiw = 0;
 	int pos;
@@ -3034,50 +3099,14 @@ static int region_offset_to_dpa_result(struct cxl_region *cxlr, u64 offset,
 	} else {
 		hpa_offset = offset;
 	}
-	/*
-	 * Interleave position: CXL Spec 3.2 Section 8.2.4.20.13
-	 * eiw < 8
-	 *	Position is in the IW bits at HPA_OFFSET[IG+8+IW-1:IG+8].
-	 *	Per spec "remove IW bits starting with bit position IG+8"
-	 * eiw >= 8
-	 *	Position is not explicitly stored in HPA_OFFSET bits. It is
-	 *	derived from the modulo operation of the upper bits using
-	 *	the total number of interleave ways.
-	 */
-	if (eiw < 8) {
-		pos = (hpa_offset >> (eig + 8)) & GENMASK(eiw - 1, 0);
-	} else {
-		shifted = hpa_offset >> (eig + 8);
-		div64_u64_rem(shifted, p->interleave_ways, &rem);
-		pos = rem;
-	}
-	if (pos < 0 || pos >= p->nr_targets) {
-		dev_dbg(&cxlr->dev, "Invalid position %d for %d targets\n",
-			pos, p->nr_targets);
+
+	pos = cxl_calculate_position(hpa_offset, eiw, eig);
+	if (pos < 0 || pos >= p->nr_targets)
 		return -ENXIO;
-	}
 
-	/*
-	 * DPA offset: CXL Spec 3.2 Section 8.2.4.20.13
-	 * Lower bits [IG+7:0] pass through unchanged
-	 * (eiw < 8)
-	 *	Per spec: DPAOffset[51:IG+8] = (HPAOffset[51:IG+IW+8] >> IW)
-	 *	Clear the position bits to isolate upper section, then
-	 *	reverse the left shift by eiw that occurred during DPA->HPA
-	 * (eiw >= 8)
-	 *	Per spec: DPAOffset[51:IG+8] = HPAOffset[51:IG+IW] / 3
-	 *	Extract upper bits from the correct bit range and divide by 3
-	 *	to recover the original DPA upper bits
-	 */
-	bits_lower = hpa_offset & GENMASK_ULL(eig + 7, 0);
-	if (eiw < 8) {
-		temp = hpa_offset &= ~((u64)GENMASK(eig + eiw + 8 - 1, 0));
-		dpa_offset = temp >> eiw;
-	} else {
-		bits_upper = div64_u64(hpa_offset >> (eig + eiw), 3);
-		dpa_offset = bits_upper << (eig + 8);
-	}
-	dpa_offset |= bits_lower;
+	dpa_offset = cxl_calculate_dpa_offset(hpa_offset, eiw, eig);
+	if (dpa_offset == ULLONG_MAX)
+		return -ENXIO;
 
 	/* Look-up and return the result: a memdev and a DPA */
 	for (int i = 0; i < p->nr_targets; i++) {
drivers/cxl/cxl.h +3 -0
index f87628a22852..151a8ce698c5 100644
@@ -731,6 +731,9 @@ static inline bool is_cxl_root(struct cxl_port *port)
 	return port->uport_dev == port->dev.parent;
 }
 
+u64 cxl_calculate_hpa_offset(u64 dpa_offset, int pos, u8 eiw, u16 eig);
+u64 cxl_calculate_dpa_offset(u64 hpa_offset, u8 eiw, u16 eig);
+int cxl_calculate_position(u64 hpa_offset, u8 eiw, u16 eig);
 int cxl_num_decoders_committed(struct cxl_port *port);
 bool is_cxl_port(const struct device *dev);
 struct cxl_port *to_cxl_port(const struct device *dev);
Signature · 1 line
2.37.3
#2 lore raw

Re: [PATCH 1/3] cxl/region: Refactor address translation funcs for testing

Quoted 11 lines · On Mon, 4 Aug 2025 01:52:39 -0700 [email protected] wrote:

From: Alison Schofield <[email protected]>

In preparation for adding a test module that exercises the address translation calculations, extract the core calculations into stand- alone functions that operate on base parameters without dependencies on struct cxl_region.

This refactoring enables unit testing of the address translation logic with controlled inputs, while maintaining identical functionality in the existing code paths.

Quoted 5 lines

The moved code has only one change. In the new cxl_calculate_position() eiw_to_ways(eiw, &ways) replaces the prior usage of p->interleave_ways, since the new function cannot depend upon struct cxl_region_params.

A few comments. In general looks fine to me.

Quoted 19 lines

u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,

drivers/cxl/core/region.c +1 -5
 		   u64 dpa)
 {
 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
-	u64 dpa_offset, hpa_offset, bits_upper, mask_upper, hpa;
 	struct cxl_region_params *p = &cxlr->params;
 	struct cxl_endpoint_decoder *cxled = NULL;
+	u64 dpa_offset, hpa_offset, hpa;
 	u16 eig = 0;
 	u8 eiw = 0;
 	int pos;
@@ -2965,19 +3043,8 @@ u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
 	/* Remove the dpa base */
 	dpa_offset = dpa - cxl_dpa_resource_start(cxled);
 
-	mask_upper = GENMASK_ULL(51, eig + 8);
-
-	if (eiw < 8) {
-		hpa_offset = (dpa_offset & mask_upper) << eiw;

drivers/cxl/core/region.c u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd, · line 3046

Quoted 10 lines
drivers/cxl/core/region.c +1 -9
-		hpa_offset |= pos << (eig + 8);
-	} else {
-		bits_upper = (dpa_offset & mask_upper) >> (eig + 8);
-		bits_upper = bits_upper * 3;
-		hpa_offset = ((bits_upper << (eiw - 8)) + pos) << (eig + 8);
-	}
-
-	/* The lower bits remain unchanged */
-	hpa_offset |= dpa_offset & GENMASK_ULL(eig + 7, 0);
+	/* this chunk was moved, maybe move comment too */

No idea what that refers to!

Quoted 27 lines
drivers/cxl/core/region.c +1 -12
+	hpa_offset = cxl_calculate_hpa_offset(dpa_offset, pos, eiw, eig);
 
 	/* Apply the hpa_offset to the region base address */
 	hpa = hpa_offset + p->res->start + p->cache_size;
@@ -3011,8 +3078,6 @@ static int region_offset_to_dpa_result(struct cxl_region *cxlr, u64 offset,
 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
 	struct cxl_endpoint_decoder *cxled;
 	u64 hpa, hpa_offset, dpa_offset;
-	u64 bits_upper, bits_lower;
-	u64 shifted, rem, temp;
 	u16 eig = 0;
 	u8 eiw = 0;
 	int pos;
@@ -3034,50 +3099,14 @@ static int region_offset_to_dpa_result(struct cxl_region *cxlr, u64 offset,
 	} else {
 		hpa_offset = offset;
 	}
-	/*
-	 * Interleave position: CXL Spec 3.2 Section 8.2.4.20.13
-	 * eiw < 8
-	 *	Position is in the IW bits at HPA_OFFSET[IG+8+IW-1:IG+8].
-	 *	Per spec "remove IW bits starting with bit position IG+8"
-	 * eiw >= 8
-	 *	Position is not explicitly stored in HPA_OFFSET bits. It is
-	 *	derived from the modulo operation of the upper bits using
-	 *	the total number of interleave ways.
-	 */

drivers/cxl/core/region.c static int region_offset_to_dpa_result(struct cxl_region *cxlr, u64 offset, · line 3056

Quoted 10 lines
drivers/cxl/core/region.c +0 -10
-	if (eiw < 8) {
-		pos = (hpa_offset >> (eig + 8)) & GENMASK(eiw - 1, 0);
-	} else {
-		shifted = hpa_offset >> (eig + 8);
-		div64_u64_rem(shifted, p->interleave_ways, &rem);
-		pos = rem;
-	}
-	if (pos < 0 || pos >= p->nr_targets) {
-		dev_dbg(&cxlr->dev, "Invalid position %d for %d targets\n",
-			pos, p->nr_targets);

Bit of a shame to loose this debug print if we hit this condition.

Quoted 19 lines
drivers/cxl/core/region.c +3 -14
+
+	pos = cxl_calculate_position(hpa_offset, eiw, eig);
+	if (pos < 0 || pos >= p->nr_targets)
 		return -ENXIO;
-	}
 
-	/*
-	 * DPA offset: CXL Spec 3.2 Section 8.2.4.20.13
-	 * Lower bits [IG+7:0] pass through unchanged
-	 * (eiw < 8)
-	 *	Per spec: DPAOffset[51:IG+8] = (HPAOffset[51:IG+IW+8] >> IW)
-	 *	Clear the position bits to isolate upper section, then
-	 *	reverse the left shift by eiw that occurred during DPA->HPA
-	 * (eiw >= 8)
-	 *	Per spec: DPAOffset[51:IG+8] = HPAOffset[51:IG+IW] / 3
-	 *	Extract upper bits from the correct bit range and divide by 3
-	 *	to recover the original DPA upper bits
-	 */
-	bits_lower = hpa_offset & GENMASK_ULL(eig + 7, 0);

drivers/cxl/core/region.c static int region_offset_to_dpa_result(struct cxl_region *cxlr, u64 offset, · line 3108

Quoted 10 lines
drivers/cxl/core/region.c +2 -8
-	if (eiw < 8) {
-		temp = hpa_offset &= ~((u64)GENMASK(eig + eiw + 8 - 1, 0));
-		dpa_offset = temp >> eiw;
-	} else {
-		bits_upper = div64_u64(hpa_offset >> (eig + eiw), 3);
-		dpa_offset = bits_upper << (eig + 8);
-	}
-	dpa_offset |= bits_lower;
+	dpa_offset = cxl_calculate_dpa_offset(hpa_offset, eiw, eig);
+	if (dpa_offset == ULLONG_MAX)

How do we get ULLONG_MAX? Not obvious that cxl_calculate_dpa_offset() can generate that.

Quoted 1 line
drivers/cxl/core/region.c +1 -0
+		return -ENXIO;
author maintainer
#3 lore raw

Re: [PATCH 1/3] cxl/region: Refactor address translation funcs for testing

Quoted 14 lines · Jonathan Cameron
Quoted 11 lines · On Mon, 4 Aug 2025 01:52:39 -0700 [email protected] wrote:

From: Alison Schofield <[email protected]>

In preparation for adding a test module that exercises the address translation calculations, extract the core calculations into stand- alone functions that operate on base parameters without dependencies on struct cxl_region.

This refactoring enables unit testing of the address translation logic with controlled inputs, while maintaining identical functionality in the existing code paths.

Quoted 7 lines
Quoted 5 lines

The moved code has only one change. In the new cxl_calculate_position() eiw_to_ways(eiw, &ways) replaces the prior usage of p->interleave_ways, since the new function cannot depend upon struct cxl_region_params.

A few comments. In general looks fine to me.

Thanks for reviewing -

snip

Quoted 4 lines
Quoted 4 lines
drivers/cxl/core/region.c +0 -0
@@ -2965,19 +3043,8 @@ u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
 	/* Remove the dpa base */
 	dpa_offset = dpa - cxl_dpa_resource_start(cxled);
 
Quoted 16 lines
Quoted 4 lines
drivers/cxl/core/region.c +0 -4
-	mask_upper = GENMASK_ULL(51, eig + 8);
-
-	if (eiw < 8) {
-		hpa_offset = (dpa_offset & mask_upper) << eiw;

drivers/cxl/core/region.c u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd, · line 3046

Quoted 10 lines
drivers/cxl/core/region.c +1 -9
-		hpa_offset |= pos << (eig + 8);
-	} else {
-		bits_upper = (dpa_offset & mask_upper) >> (eig + 8);
-		bits_upper = bits_upper * 3;
-		hpa_offset = ((bits_upper << (eiw - 8)) + pos) << (eig + 8);
-	}
-
-	/* The lower bits remain unchanged */
-	hpa_offset |= dpa_offset & GENMASK_ULL(eig + 7, 0);
+	/* this chunk was moved, maybe move comment too */

No idea what that refers to!

You caught me talking to myself and you caught me not listening to myself too! That comment refers to a block comment that is cut off (from the top) of this diff. I intended to move it into the new helper as it describes the algorithm of dpa->hpa work. I've moved it in v2.

Quoted 1 line

snip

Quoted 6 lines

drivers/cxl/core/region.c static int region_offset_to_dpa_result(struct cxl_region *cxlr, u64 offset, · line 3056

Quoted 4 lines
drivers/cxl/core/region.c +0 -4
-	}
-	if (pos < 0 || pos >= p->nr_targets) {
-		dev_dbg(&cxlr->dev, "Invalid position %d for %d targets\n",
-			pos, p->nr_targets);

Bit of a shame to loose this debug print if we hit this condition.

Yes, that was not good. I've restored it below in v2.

Quoted 6 lines
Quoted 6 lines
drivers/cxl/core/region.c +3 -1
+
+	pos = cxl_calculate_position(hpa_offset, eiw, eig);
+	if (pos < 0 || pos >= p->nr_targets)
 		return -ENXIO;
-	}

snip

Quoted 8 lines

drivers/cxl/core/region.c static int region_offset_to_dpa_result(struct cxl_region *cxlr, u64 offset, · line 3108

Quoted 5 lines
drivers/cxl/core/region.c +2 -3
-		dpa_offset = bits_upper << (eig + 8);
-	}
-	dpa_offset |= bits_lower;
+	dpa_offset = cxl_calculate_dpa_offset(hpa_offset, eiw, eig);
+	if (dpa_offset == ULLONG_MAX)

How do we get ULLONG_MAX? Not obvious that cxl_calculate_dpa_offset() can generate that.

No, that shouldn't be a check here. Thanks for the catch.

Quoted 5 lines
Quoted 2 lines
drivers/cxl/core/region.c +1 -0
+		return -ENXIO;
maintainer
#4 lore raw

Re: [PATCH 1/3] cxl/region: Refactor address translation funcs for testing

In reply to [email protected]
Quoted 30 lines · On 8/4/25 1:52 AM, [email protected] wrote:

From: Alison Schofield <[email protected]>

In preparation for adding a test module that exercises the address translation calculations, extract the core calculations into stand- alone functions that operate on base parameters without dependencies on struct cxl_region.

This refactoring enables unit testing of the address translation logic with controlled inputs, while maintaining identical functionality in the existing code paths.

The moved code has only one change. In the new cxl_calculate_position() eiw_to_ways(eiw, &ways) replaces the prior usage of p->interleave_ways, since the new function cannot depend upon struct cxl_region_params.

---
 drivers/cxl/core/region.c | 145 +++++++++++++++++++++++---------------
 drivers/cxl/cxl.h         |   3 +
 2 files changed, 90 insertions(+), 58 deletions(-)
drivers/cxl/core/region.c +1 -0
index 281a31df686f..ccee5049d700 100644
@@ -2929,13 +2929,91 @@ static bool has_spa_to_hpa(struct cxl_root_decoder *cxlrd)
 	return cxlrd->ops && cxlrd->ops->spa_to_hpa;
 }
 
+u64 cxl_calculate_dpa_offset(u64 hpa_offset, u8 eiw, u16 eig)

drivers/cxl/core/region.c static bool has_spa_to_hpa(struct cxl_root_decoder *cxlrd) · line 2942

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+{
+	u64 dpa_offset, bits_lower, bits_upper, temp;
+
+	/*
+	 * DPA offset: CXL Spec 3.2 Section 8.2.4.20.13
+	 * Lower bits [IG+7:0] pass through unchanged
+	 * (eiw < 8)
+	 *	Per spec: DPAOffset[51:IG+8] = (HPAOffset[51:IG+IW+8] >> IW)
+	 *	Clear the position bits to isolate upper section, then
+	 *	reverse the left shift by eiw that occurred during DPA->HPA

May be easier to read if listing the steps instead of continued sentence:

	 *	Per spec: DPAOffset[51:IG+8] = (HPAOffset[51:IG+IW+8] >> IW)
	 *	1. Clear the position bits to isolate upper section.
	 *	2. Reverse the left shift by eiw that occurred during DPA->HPA

drivers/cxl/core/region.c static bool has_spa_to_hpa(struct cxl_root_decoder *cxlrd) · line 2946

Quoted 4 lines
drivers/cxl/core/region.c +4 -0
+	 * (eiw >= 8)
+	 *	Per spec: DPAOffset[51:IG+8] = HPAOffset[51:IG+IW] / 3
+	 *	Extract upper bits from the correct bit range and divide by 3
+	 *	to recover the original DPA upper bits

same here as above

DJ

Quoted 176 lines
drivers/cxl/core/region.c +72 -58
+	 */
+	bits_lower = hpa_offset & GENMASK_ULL(eig + 7, 0);
+	if (eiw < 8) {
+		temp = hpa_offset &= ~((u64)GENMASK(eig + eiw + 8 - 1, 0));
+		dpa_offset = temp >> eiw;
+	} else {
+		bits_upper = div64_u64(hpa_offset >> (eig + eiw), 3);
+		dpa_offset = bits_upper << (eig + 8);
+	}
+	dpa_offset |= bits_lower;
+
+	return dpa_offset;
+}
+
+int cxl_calculate_position(u64 hpa_offset, u8 eiw, u16 eig)
+{
+	unsigned int ways = 0;
+	u64 shifted, rem;
+	int pos;
+
+	/*
+	 * Interleave position: CXL Spec 3.2 Section 8.2.4.20.13
+	 * eiw < 8
+	 *	Position is in the IW bits at HPA_OFFSET[IG+8+IW-1:IG+8].
+	 *	Per spec "remove IW bits starting with bit position IG+8"
+	 * eiw >= 8
+	 *	Position is not explicitly stored in HPA_OFFSET bits. It is
+	 *	derived from the modulo operation of the upper bits using
+	 *	the total number of interleave ways.
+	 */
+	if (eiw < 8) {
+		pos = (hpa_offset >> (eig + 8)) & GENMASK(eiw - 1, 0);
+	} else {
+		shifted = hpa_offset >> (eig + 8);
+		eiw_to_ways(eiw, &ways);
+		div64_u64_rem(shifted, ways, &rem);
+		pos = rem;
+	}
+
+	return pos;
+}
+
+u64 cxl_calculate_hpa_offset(u64 dpa_offset, int pos, u8 eiw, u16 eig)
+{
+	u64 mask_upper, hpa_offset, bits_upper;
+
+	mask_upper = GENMASK_ULL(51, eig + 8);
+
+	if (eiw < 8) {
+		hpa_offset = (dpa_offset & mask_upper) << eiw;
+		hpa_offset |= pos << (eig + 8);
+	} else {
+		bits_upper = (dpa_offset & mask_upper) >> (eig + 8);
+		bits_upper = bits_upper * 3;
+		hpa_offset = ((bits_upper << (eiw - 8)) + pos) << (eig + 8);
+	}
+
+	/* The lower bits remain unchanged */
+	hpa_offset |= dpa_offset & GENMASK_ULL(eig + 7, 0);
+
+	return hpa_offset;
+}
+
 u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
 		   u64 dpa)
 {
 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
-	u64 dpa_offset, hpa_offset, bits_upper, mask_upper, hpa;
 	struct cxl_region_params *p = &cxlr->params;
 	struct cxl_endpoint_decoder *cxled = NULL;
+	u64 dpa_offset, hpa_offset, hpa;
 	u16 eig = 0;
 	u8 eiw = 0;
 	int pos;
@@ -2965,19 +3043,8 @@ u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
 	/* Remove the dpa base */
 	dpa_offset = dpa - cxl_dpa_resource_start(cxled);
 
-	mask_upper = GENMASK_ULL(51, eig + 8);
-
-	if (eiw < 8) {
-		hpa_offset = (dpa_offset & mask_upper) << eiw;
-		hpa_offset |= pos << (eig + 8);
-	} else {
-		bits_upper = (dpa_offset & mask_upper) >> (eig + 8);
-		bits_upper = bits_upper * 3;
-		hpa_offset = ((bits_upper << (eiw - 8)) + pos) << (eig + 8);
-	}
-
-	/* The lower bits remain unchanged */
-	hpa_offset |= dpa_offset & GENMASK_ULL(eig + 7, 0);
+	/* this chunk was moved, maybe move comment too */
+	hpa_offset = cxl_calculate_hpa_offset(dpa_offset, pos, eiw, eig);
 
 	/* Apply the hpa_offset to the region base address */
 	hpa = hpa_offset + p->res->start + p->cache_size;
@@ -3011,8 +3078,6 @@ static int region_offset_to_dpa_result(struct cxl_region *cxlr, u64 offset,
 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
 	struct cxl_endpoint_decoder *cxled;
 	u64 hpa, hpa_offset, dpa_offset;
-	u64 bits_upper, bits_lower;
-	u64 shifted, rem, temp;
 	u16 eig = 0;
 	u8 eiw = 0;
 	int pos;
@@ -3034,50 +3099,14 @@ static int region_offset_to_dpa_result(struct cxl_region *cxlr, u64 offset,
 	} else {
 		hpa_offset = offset;
 	}
-	/*
-	 * Interleave position: CXL Spec 3.2 Section 8.2.4.20.13
-	 * eiw < 8
-	 *	Position is in the IW bits at HPA_OFFSET[IG+8+IW-1:IG+8].
-	 *	Per spec "remove IW bits starting with bit position IG+8"
-	 * eiw >= 8
-	 *	Position is not explicitly stored in HPA_OFFSET bits. It is
-	 *	derived from the modulo operation of the upper bits using
-	 *	the total number of interleave ways.
-	 */
-	if (eiw < 8) {
-		pos = (hpa_offset >> (eig + 8)) & GENMASK(eiw - 1, 0);
-	} else {
-		shifted = hpa_offset >> (eig + 8);
-		div64_u64_rem(shifted, p->interleave_ways, &rem);
-		pos = rem;
-	}
-	if (pos < 0 || pos >= p->nr_targets) {
-		dev_dbg(&cxlr->dev, "Invalid position %d for %d targets\n",
-			pos, p->nr_targets);
+
+	pos = cxl_calculate_position(hpa_offset, eiw, eig);
+	if (pos < 0 || pos >= p->nr_targets)
 		return -ENXIO;
-	}
 
-	/*
-	 * DPA offset: CXL Spec 3.2 Section 8.2.4.20.13
-	 * Lower bits [IG+7:0] pass through unchanged
-	 * (eiw < 8)
-	 *	Per spec: DPAOffset[51:IG+8] = (HPAOffset[51:IG+IW+8] >> IW)
-	 *	Clear the position bits to isolate upper section, then
-	 *	reverse the left shift by eiw that occurred during DPA->HPA
-	 * (eiw >= 8)
-	 *	Per spec: DPAOffset[51:IG+8] = HPAOffset[51:IG+IW] / 3
-	 *	Extract upper bits from the correct bit range and divide by 3
-	 *	to recover the original DPA upper bits
-	 */
-	bits_lower = hpa_offset & GENMASK_ULL(eig + 7, 0);
-	if (eiw < 8) {
-		temp = hpa_offset &= ~((u64)GENMASK(eig + eiw + 8 - 1, 0));
-		dpa_offset = temp >> eiw;
-	} else {
-		bits_upper = div64_u64(hpa_offset >> (eig + eiw), 3);
-		dpa_offset = bits_upper << (eig + 8);
-	}
-	dpa_offset |= bits_lower;
+	dpa_offset = cxl_calculate_dpa_offset(hpa_offset, eiw, eig);
+	if (dpa_offset == ULLONG_MAX)
+		return -ENXIO;
 
 	/* Look-up and return the result: a memdev and a DPA */
 	for (int i = 0; i < p->nr_targets; i++) {
drivers/cxl/cxl.h +3 -0
index f87628a22852..151a8ce698c5 100644
@@ -731,6 +731,9 @@ static inline bool is_cxl_root(struct cxl_port *port)
 	return port->uport_dev == port->dev.parent;
 }
 
+u64 cxl_calculate_hpa_offset(u64 dpa_offset, int pos, u8 eiw, u16 eig);
+u64 cxl_calculate_dpa_offset(u64 hpa_offset, u8 eiw, u16 eig);
+int cxl_calculate_position(u64 hpa_offset, u8 eiw, u16 eig);
 int cxl_num_decoders_committed(struct cxl_port *port);
 bool is_cxl_port(const struct device *dev);
 struct cxl_port *to_cxl_port(const struct device *dev);
author maintainer
#5 lore raw

Re: [PATCH 1/3] cxl/region: Refactor address translation funcs for testing

Quoted 50 lines · Dave Jiang
Quoted 30 lines · On 8/4/25 1:52 AM, [email protected] wrote:

From: Alison Schofield <[email protected]>

In preparation for adding a test module that exercises the address translation calculations, extract the core calculations into stand- alone functions that operate on base parameters without dependencies on struct cxl_region.

This refactoring enables unit testing of the address translation logic with controlled inputs, while maintaining identical functionality in the existing code paths.

The moved code has only one change. In the new cxl_calculate_position() eiw_to_ways(eiw, &ways) replaces the prior usage of p->interleave_ways, since the new function cannot depend upon struct cxl_region_params.

---
 drivers/cxl/core/region.c | 145 +++++++++++++++++++++++---------------
 drivers/cxl/cxl.h         |   3 +
 2 files changed, 90 insertions(+), 58 deletions(-)
drivers/cxl/core/region.c +1 -0
index 281a31df686f..ccee5049d700 100644
@@ -2929,13 +2929,91 @@ static bool has_spa_to_hpa(struct cxl_root_decoder *cxlrd)
 	return cxlrd->ops && cxlrd->ops->spa_to_hpa;
 }
 
+u64 cxl_calculate_dpa_offset(u64 hpa_offset, u8 eiw, u16 eig)

drivers/cxl/core/region.c static bool has_spa_to_hpa(struct cxl_root_decoder *cxlrd) · line 2942

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+{
+	u64 dpa_offset, bits_lower, bits_upper, temp;
+
+	/*
+	 * DPA offset: CXL Spec 3.2 Section 8.2.4.20.13
+	 * Lower bits [IG+7:0] pass through unchanged
+	 * (eiw < 8)
+	 *	Per spec: DPAOffset[51:IG+8] = (HPAOffset[51:IG+IW+8] >> IW)
+	 *	Clear the position bits to isolate upper section, then
+	 *	reverse the left shift by eiw that occurred during DPA->HPA

May be easier to read if listing the steps instead of continued sentence:

	 *	Per spec: DPAOffset[51:IG+8] = (HPAOffset[51:IG+IW+8] >> IW)
	 *	1. Clear the position bits to isolate upper section.
	 *	2. Reverse the left shift by eiw that occurred during DPA->HPA
Quoted 6 lines

drivers/cxl/core/region.c static bool has_spa_to_hpa(struct cxl_root_decoder *cxlrd) · line 2946

Quoted 4 lines
drivers/cxl/core/region.c +4 -0
+	 * (eiw >= 8)
+	 *	Per spec: DPAOffset[51:IG+8] = HPAOffset[51:IG+IW] / 3
+	 *	Extract upper bits from the correct bit range and divide by 3
+	 *	to recover the original DPA upper bits

same here as above

I didn't pick this up in v2 because it is in moved code, not new code, so I didn't want to pollute the diff. (Granted this diff is hard to read already).

I'll keep this in mind if this needs touching in future revisions.

Quoted 3 lines

DJ

author maintainer
#6 lore raw

PATCH 2/3 cxl/acpi: Make the XOR calculations available for testing

In reply to [email protected]

From: Alison Schofield <[email protected]>

In preparation for adding a test module that can exercise the address translation functions performed on XOR configured regions, the XOR function needs to be refactored and the ability to create a CXIMS provided.

Refactor the XOR function by extracting the core calculation into a standalone function. Enhance the parameter validation since this new function will be called from the test module where the parameters may not be guaranteed valid.

To allow the test module to create a CXIMS without exposing the full cxl_cxims_data structure, add new functions that can create and free a CXIMS.

---
 drivers/cxl/acpi.c   | 51 ++++++++++++++++++++++++++++++++++++++------
 include/linux/acpi.h |  7 ++++++
 2 files changed, 52 insertions(+), 6 deletions(-)
drivers/cxl/acpi.c +45 -6
index 547ed116a16f..86ca0466a95b 100644
@@ -22,6 +22,7 @@ struct cxl_cxims_data {
  * translation is based on the Host Bridge interleave ways of the CXL Window.
  * CXL Spec Secion 9.18.1.4 and Table 9-22.
  */
+static const int valid_hbiw[] = { 1, 2, 3, 4, 6, 8, 12, 16 };
 static const int hbiw_to_nr_maps[] = {
 	[1] = 0, [2] = 1, [3] = 0, [4] = 2, [6] = 1, [8] = 3, [12] = 2, [16] = 4
 };
@@ -30,16 +31,47 @@ static const guid_t acpi_cxl_qtg_id_guid =
 	GUID_INIT(0xF365F9A6, 0xA7DE, 0x4071,
 		  0xA6, 0x6A, 0xB4, 0x0C, 0x0B, 0x4F, 0x8E, 0x52);
 
-static u64 cxl_apply_xor_maps(struct cxl_root_decoder *cxlrd, u64 addr)
+struct cxl_cxims_data *cxl_create_cxims_data(int nr_maps, u64 *xormaps)
 {
-	int nr_maps_to_apply = hbiw_to_nr_maps[cxlrd->cxlsd.nr_targets];
-	struct cxl_cxims_data *cximsd = cxlrd->platform_data;
+	struct cxl_cxims_data *cximsd;
+
+	if (nr_maps <= 0 || !xormaps)
+		return NULL;
+
+	cximsd = kzalloc(struct_size(cximsd, xormaps, nr_maps), GFP_KERNEL);
+	if (!cximsd)
+		return NULL;
+
+	memcpy(cximsd->xormaps, xormaps, nr_maps * sizeof(*cximsd->xormaps));
+	cximsd->nr_maps = nr_maps;
+
+	return cximsd;
+}
+
+void cxl_free_cxims_data(struct cxl_cxims_data *cximsd)
+{
+	kfree(cximsd);
+}
+
+u64 cxl_do_xormap_calc(struct cxl_cxims_data *cximsd, u64 addr, int hbiw)
+{
+	int nr_maps_to_apply = -1;
 	u64 val;
 	int pos;
 
-	/* No xormaps for host bridge interleave ways of 1 or 3 */
-	if (!nr_maps_to_apply)
-		return addr;
+	/*
+	 * Strictly validate hbiw since this function is used for testing and
+	 * that nullifies any expectation of trusted parameters from the CXL
+	 * Region Driver.
+	 */
+	for (int i = 0; i < ARRAY_SIZE(valid_hbiw); i++) {
+		if (valid_hbiw[i] == hbiw) {
+			nr_maps_to_apply = hbiw_to_nr_maps[hbiw];
+			break;
+		}
+	}
+	if (nr_maps_to_apply == -1 || nr_maps_to_apply > cximsd->nr_maps)
+		return ULLONG_MAX;
 
 	/*
 	 * In regions using XOR interleave arithmetic the CXL HPA may not
@@ -71,6 +103,13 @@ static u64 cxl_apply_xor_maps(struct cxl_root_decoder *cxlrd, u64 addr)
 	return addr;
 }
 
+static u64 cxl_apply_xor_maps(struct cxl_root_decoder *cxlrd, u64 addr)
+{
+	struct cxl_cxims_data *cximsd = cxlrd->platform_data;
+
+	return cxl_do_xormap_calc(cximsd, addr, cxlrd->cxlsd.nr_targets);
+}
+
 struct cxl_cxims_context {
 	struct device *dev;
 	struct cxl_root_decoder *cxlrd;
include/linux/acpi.h +7 -0
index f102c0fe3431..854578c6009f 100644
@@ -1616,4 +1616,11 @@ static inline bool acpi_node_backed_by_real_pxm(int nid)
 }
 #endif
 
+#if IS_ENABLED(CONFIG_CXL_ACPI)
+struct cxl_cxims_data;
+void cxl_free_cxims_data(struct cxl_cxims_data *cximsd);
+struct cxl_cxims_data *cxl_create_cxims_data(int nr_maps, u64 *xormaps);
+u64 cxl_do_xormap_calc(struct cxl_cxims_data *cximsd, u64 addr, int hbiw);
+#endif
+
 #endif	/*_LINUX_ACPI_H*/
Signature · 1 line
2.37.3
Reviewed-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] cxl/acpi: Make the XOR calculations available for testing

Quoted 12 lines · On Mon, 4 Aug 2025 01:52:40 -0700 [email protected] wrote:

From: Alison Schofield <[email protected]>

In preparation for adding a test module that can exercise the address translation functions performed on XOR configured regions, the XOR function needs to be refactored and the ability to create a CXIMS provided.

Refactor the XOR function by extracting the core calculation into a standalone function. Enhance the parameter validation since this new function will be called from the test module where the parameters may not be guaranteed valid.

Quoted 5 lines

To allow the test module to create a CXIMS without exposing the full cxl_cxims_data structure, add new functions that can create and free a CXIMS.

Hi Alison,

I'm not particularly keen on code that is just there to create opaque stuff for tests that may or may not be built but I guess it's not huge so fair enough.

author maintainer
#8 lore raw

Re: [PATCH 2/3] cxl/acpi: Make the XOR calculations available for testing

Quoted 19 lines · Jonathan Cameron
Quoted 16 lines · On Mon, 4 Aug 2025 01:52:40 -0700 [email protected] wrote:

From: Alison Schofield <[email protected]>

In preparation for adding a test module that can exercise the address translation functions performed on XOR configured regions, the XOR function needs to be refactored and the ability to create a CXIMS provided.

Refactor the XOR function by extracting the core calculation into a standalone function. Enhance the parameter validation since this new function will be called from the test module where the parameters may not be guaranteed valid.

To allow the test module to create a CXIMS without exposing the full cxl_cxims_data structure, add new functions that can create and free a CXIMS.

Quoted 6 lines
Quoted 1 line

Hi Alison,

I'm not particularly keen on code that is just there to create opaque stuff for tests that may or may not be built but I guess it's not huge so fair enough.

Patch 3 is the test module that uses this, and the CXL unit test that uses the test module is on the list for review too.

Thanks for all the reviews!

Quoted 2 lines
#9 lore raw

Re: [PATCH 2/3] cxl/acpi: Make the XOR calculations available for testing

In reply to [email protected]
Quoted 17 lines · alison.schofield@ wrote:

From: Alison Schofield <[email protected]>

In preparation for adding a test module that can exercise the address translation functions performed on XOR configured regions, the XOR function needs to be refactored and the ability to create a CXIMS provided.

Refactor the XOR function by extracting the core calculation into a standalone function. Enhance the parameter validation since this new function will be called from the test module where the parameters may not be guaranteed valid.

To allow the test module to create a CXIMS without exposing the full cxl_cxims_data structure, add new functions that can create and free a CXIMS.

[..]

include/linux/acpi.h static inline bool acpi_node_backed_by_real_pxm(int nid) · line 1621

Quoted 11 lines
include/linux/acpi.h +3 -0
index f102c0fe3431..854578c6009f 100644
@@ -1616,4 +1616,11 @@ static inline bool acpi_node_backed_by_real_pxm(int nid)
 }
 #endif
 
+#if IS_ENABLED(CONFIG_CXL_ACPI)
+struct cxl_cxims_data;
+void cxl_free_cxims_data(struct cxl_cxims_data *cximsd);

This just wraps kfree(), call kfree() directly.

include/linux/acpi.h static inline bool acpi_node_backed_by_real_pxm(int nid) · line 1622

Quoted 1 line
include/linux/acpi.h +1 -0
+struct cxl_cxims_data *cxl_create_cxims_data(int nr_maps, u64 *xormaps);

The only caller of this is cxl_test, it can just be copied there, no need for the core to carry it.

include/linux/acpi.h static inline bool acpi_node_backed_by_real_pxm(int nid) · line 1623

Quoted 1 line
include/linux/acpi.h +1 -0
+u64 cxl_do_xormap_calc(struct cxl_cxims_data *cximsd, u64 addr, int hbiw);

This function should be static outside of test builds. See __mock as an example of optionally marking the function as not static. Move the header declaration to tools/testing/cxl/ so that most readers can just assume this function is static.

That said, __mock is currently used to optionally mark a function as __weak, so maybe this wants a __mock_export for that purpose.

author maintainer
#10 lore raw

Re: [PATCH 2/3] cxl/acpi: Make the XOR calculations available for testing

Quoted 27 lines · Dan Williams
Quoted 17 lines · alison.schofield@ wrote:

From: Alison Schofield <[email protected]>

In preparation for adding a test module that can exercise the address translation functions performed on XOR configured regions, the XOR function needs to be refactored and the ability to create a CXIMS provided.

Refactor the XOR function by extracting the core calculation into a standalone function. Enhance the parameter validation since this new function will be called from the test module where the parameters may not be guaranteed valid.

To allow the test module to create a CXIMS without exposing the full cxl_cxims_data structure, add new functions that can create and free a CXIMS.

[..]

Quoted 8 lines
include/linux/acpi.h +0 -0
index f102c0fe3431..854578c6009f 100644
@@ -1616,4 +1616,11 @@ static inline bool acpi_node_backed_by_real_pxm(int nid)
 }
 #endif
 
Quoted 5 lines

include/linux/acpi.h static inline bool acpi_node_backed_by_real_pxm(int nid) · line 1621

Quoted 3 lines
include/linux/acpi.h +3 -0
+#if IS_ENABLED(CONFIG_CXL_ACPI)
+struct cxl_cxims_data;
+void cxl_free_cxims_data(struct cxl_cxims_data *cximsd);

This just wraps kfree(), call kfree() directly.

Done.

Quoted 6 lines

include/linux/acpi.h static inline bool acpi_node_backed_by_real_pxm(int nid) · line 1622

Quoted 2 lines
include/linux/acpi.h +1 -0
+struct cxl_cxims_data *cxl_create_cxims_data(int nr_maps, u64 *xormaps);

The only caller of this is cxl_test, it can just be copied there, no need for the core to carry it.

Done. To make that work I moved "struct cxl_cxims_data" definition from cxl/acpi.c to include/linux/acpi.h for module to access.

Quoted 9 lines

include/linux/acpi.h static inline bool acpi_node_backed_by_real_pxm(int nid) · line 1623

Quoted 1 line
include/linux/acpi.h +1 -0
+u64 cxl_do_xormap_calc(struct cxl_cxims_data *cximsd, u64 addr, int hbiw);

This function should be static outside of test builds. See __mock as an example of optionally marking the function as not static. Move the header declaration to tools/testing/cxl/ so that most readers can just assume this function is static.

That said, __mock is currently used to optionally mark a function as __weak, so maybe this wants a __mock_export for that purpose.

Added a __mock_export and used on all 4 funcs, with a new header tools/testing/cxl/cxl_test.h. Please take a look in v2.

author maintainer
#11 lore raw

PATCH 3/3 cxl/test: Add cxl_translate module for address translation testing

In reply to [email protected]

From: Alison Schofield <[email protected]>

Add a loadable test module that validates CXL address translation calculations using parameterized test vectors. The module tests both host to device and device to host address translations for Modulo and XOR interleave arithmetic.

Test vectors are provided as module parameters in the format:
"dpa pos r_eiw r_eig hb_ways math expected_spa"

The module performs round-trip validation:
1. Translate a DPA and position to a SPA
2. Verify the result matches expected SPA
3. Translate that SPA back to a DPA and position
4. Verify round-trip consistency

The module accesses the refactored translation functions through the exports made available only to CXL test modules.

---
 tools/testing/cxl/Kbuild               |   1 +
 tools/testing/cxl/cxl_acpi_exports.c   |  13 ++
 tools/testing/cxl/cxl_core_exports.c   |  11 +
 tools/testing/cxl/test/Kbuild          |   2 +
 tools/testing/cxl/test/cxl_translate.c | 298 +++++++++++++++++++++++++
 5 files changed, 325 insertions(+)
 create mode 100644 tools/testing/cxl/cxl_acpi_exports.c
 create mode 100644 tools/testing/cxl/test/cxl_translate.c
tools/testing/cxl/Kbuild +1 -0
index d07f14cb7aa4..8b16fbb44858 100644
@@ -29,6 +29,7 @@ cxl_acpi-y := $(CXL_SRC)/acpi.o
 cxl_acpi-y += mock_acpi.o
 cxl_acpi-y += config_check.o
 cxl_acpi-y += cxl_acpi_test.o
+cxl_acpi-y += cxl_acpi_exports.o
 
 obj-m += cxl_pmem.o
 
tools/testing/cxl/cxl_acpi_exports.c new file +13 -0
new file mode 100644
index 000000000000..ea00772ce74d
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright(c) 2022 Intel Corporation. All rights reserved. */
+
+#include <linux/acpi.h>
+
+/*
+ * Exporting of cxl_acpi (acpi.o) symbols that are only used by
+ * the test module cxl_translate.
+ */
+
+EXPORT_SYMBOL_NS_GPL(cxl_create_cxims_data, "CXL");
+EXPORT_SYMBOL_NS_GPL(cxl_free_cxims_data, "CXL");
+EXPORT_SYMBOL_NS_GPL(cxl_do_xormap_calc, "CXL");
tools/testing/cxl/cxl_core_exports.c +11 -0
index f088792a8925..30c9284c26a5 100644
@@ -5,3 +5,14 @@
 
 /* Exporting of cxl_core symbols that are only used by cxl_test */
 EXPORT_SYMBOL_NS_GPL(cxl_num_decoders_committed, "CXL");
+
+/*
+ * Exporting of cxl_core symbols used only by the cxl_translate module to test
+ * the CXL Region Drivers's address translation calculations.
+ *
+ * See tools/testing/cxl/cxl_translate.c
+ * See the CXL unit test cxl-translate.sh for usage
+ */
+EXPORT_SYMBOL_NS_GPL(cxl_calculate_hpa_offset, "CXL");
+EXPORT_SYMBOL_NS_GPL(cxl_calculate_dpa_offset, "CXL");
+EXPORT_SYMBOL_NS_GPL(cxl_calculate_position, "CXL");
tools/testing/cxl/test/Kbuild +2 -0
index 6b1927897856..d55973e61fdd 100644
@@ -5,6 +5,8 @@ obj-m += cxl_test.o
 obj-m += cxl_mock.o
 obj-m += cxl_mock_mem.o
 
+obj-m += cxl_translate.o
+
 cxl_test-y := cxl.o
 cxl_mock-y := mock.o
 cxl_mock_mem-y := mem.o
tools/testing/cxl/test/cxl_translate.c new file +298 -0

298 lines not on this page · raw message

Signature · 1 line
2.37.3
Reviewed-by on v1 3/3
#12 lore raw

Re: [PATCH 3/3] cxl/test: Add cxl_translate module for address translation testing

Quoted 16 lines · On Mon, 4 Aug 2025 01:52:41 -0700 [email protected] wrote:

From: Alison Schofield <[email protected]>

Add a loadable test module that validates CXL address translation calculations using parameterized test vectors. The module tests both host to device and device to host address translations for Modulo and XOR interleave arithmetic.

Test vectors are provided as module parameters in the format:
"dpa pos r_eiw r_eig hb_ways math expected_spa"

The module performs round-trip validation:
1. Translate a DPA and position to a SPA
2. Verify the result matches expected SPA
3. Translate that SPA back to a DPA and position
4. Verify round-trip consistency

Quoted 4 lines

The module accesses the refactored translation functions through the exports made available only to CXL test modules.

Nice little test. Trivial comments inline.

Quoted 112 lines
---
 tools/testing/cxl/Kbuild               |   1 +
 tools/testing/cxl/cxl_acpi_exports.c   |  13 ++
 tools/testing/cxl/cxl_core_exports.c   |  11 +
 tools/testing/cxl/test/Kbuild          |   2 +
 tools/testing/cxl/test/cxl_translate.c | 298 +++++++++++++++++++++++++
 5 files changed, 325 insertions(+)
 create mode 100644 tools/testing/cxl/cxl_acpi_exports.c
 create mode 100644 tools/testing/cxl/test/cxl_translate.c
tools/testing/cxl/Kbuild +1 -0
index d07f14cb7aa4..8b16fbb44858 100644
@@ -29,6 +29,7 @@ cxl_acpi-y := $(CXL_SRC)/acpi.o
 cxl_acpi-y += mock_acpi.o
 cxl_acpi-y += config_check.o
 cxl_acpi-y += cxl_acpi_test.o
+cxl_acpi-y += cxl_acpi_exports.o
 
 obj-m += cxl_pmem.o
 
tools/testing/cxl/cxl_acpi_exports.c new file +13 -0
new file mode 100644
index 000000000000..ea00772ce74d
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright(c) 2022 Intel Corporation. All rights reserved. */
+
+#include <linux/acpi.h>
+
+/*
+ * Exporting of cxl_acpi (acpi.o) symbols that are only used by
+ * the test module cxl_translate.
+ */
+
+EXPORT_SYMBOL_NS_GPL(cxl_create_cxims_data, "CXL");
+EXPORT_SYMBOL_NS_GPL(cxl_free_cxims_data, "CXL");
+EXPORT_SYMBOL_NS_GPL(cxl_do_xormap_calc, "CXL");
tools/testing/cxl/cxl_core_exports.c +11 -0
index f088792a8925..30c9284c26a5 100644
@@ -5,3 +5,14 @@
 
 /* Exporting of cxl_core symbols that are only used by cxl_test */
 EXPORT_SYMBOL_NS_GPL(cxl_num_decoders_committed, "CXL");
+
+/*
+ * Exporting of cxl_core symbols used only by the cxl_translate module to test
+ * the CXL Region Drivers's address translation calculations.
+ *
+ * See tools/testing/cxl/cxl_translate.c
+ * See the CXL unit test cxl-translate.sh for usage
+ */
+EXPORT_SYMBOL_NS_GPL(cxl_calculate_hpa_offset, "CXL");
+EXPORT_SYMBOL_NS_GPL(cxl_calculate_dpa_offset, "CXL");
+EXPORT_SYMBOL_NS_GPL(cxl_calculate_position, "CXL");
tools/testing/cxl/test/Kbuild +2 -0
index 6b1927897856..d55973e61fdd 100644
@@ -5,6 +5,8 @@ obj-m += cxl_test.o
 obj-m += cxl_mock.o
 obj-m += cxl_mock_mem.o
 
+obj-m += cxl_translate.o
+
 cxl_test-y := cxl.o
 cxl_mock-y := mock.o
 cxl_mock_mem-y := mem.o
tools/testing/cxl/test/cxl_translate.c new file +33 -0
new file mode 100644
index 000000000000..b163ebc4a82b
@@ -0,0 +1,298 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright(c) 2025 Intel Corporation. All rights reserved.
+
+#include <linux/moduleparam.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/acpi.h>
+#include <cxlmem.h>
+
+/* Maximum number of test vectors and entry length */
+#define MAX_TABLE_ENTRIES 128
+#define MAX_ENTRY_LEN 128
+
+/* Expected number of parameters in each test vector */
+#define EXPECTED_PARAMS 7
+
+/* Module parameters for test vectors */
+static char *table[MAX_TABLE_ENTRIES];
+static int table_num;
+
+/* Interleave Arithmetic */
+#define MODULO_MATH 0
+#define XOR_MATH 1
+
+/*
+ * XOR mapping configuration
+ * The test data sets all use the same set of xormaps. When additional
+ * data sets arrive for validation, this static setup will need to
+ * be changed to accept xormaps as additional parameters.
+ */
+struct cxl_cxims_data *cximsd;

tools/testing/cxl/test/cxl_translate.c line 43

Quoted 10 lines
tools/testing/cxl/test/cxl_translate.c new file +10 -0
+static u64 xormap_list[] = {
+	0x2020900,
+	0x4041200,
+	0x1010400,
+	0x800,
+};
+
+static int nr_maps = ARRAY_SIZE(xormap_list);
+
+/*

Might as well make this kernel-doc even if nothing is going to build these docs currently. You can probably still point the scripts at the file and to check docs are complete etc.

Quoted 17 lines
tools/testing/cxl/test/cxl_translate.c new file +17 -0
+ * to_hpa - translate a DPA offset and position to HPA offset
+ *
+ * @dpa_offset: device physical address offset
+ * @pos: devices position in interleave
+ * @r_eiw: region encoded interleave ways
+ * @r_eig: region encoded interleave granularity
+ * @hb_ways: host bridge interleave ways
+ * @math: interleave arithmetic (MODULO_MATH or XOR_MATH)
+ *
+ * Returns: host physical address offset
+ */
+static u64 to_hpa(u64 dpa_offset, int pos, u8 r_eiw, u16 r_eig, u8 hb_ways,
+		  u8 math)
+{
+	u64 hpa_offset;
+
+	/* Calculate base HPA offset from DPA and position */

tools/testing/cxl/test/cxl_translate.c line 70

Quoted 10 lines
tools/testing/cxl/test/cxl_translate.c new file +10 -0
+	hpa_offset = cxl_calculate_hpa_offset(dpa_offset, pos, r_eiw, r_eig);
+
+	/* Apply XOR mapping if specified */
+	if (math == XOR_MATH)
+		hpa_offset = cxl_do_xormap_calc(cximsd, hpa_offset, hb_ways);
+
+	return hpa_offset;
+}
+
+/*

Likewise on kernel-doc /**

Quoted 12 lines
tools/testing/cxl/test/cxl_translate.c new file +12 -0
+ * to_dpa - Convert HPA offset to DPA offset
+ *
+ * @hpa_offset: host physical address offset
+ * @r_eiw: region encoded interleave ways
+ * @r_eig: region encoded interleave granularity
+ * @hb_ways: host bridge interleave ways
+ * @math: interleave arithmetic (MODULO_MATH or XOR_MATH)
+ *
+ * Returns: device physical address offset
+ */
+static u64 to_dpa(u64 hpa_offset, u8 r_eiw, u16 r_eig, u8 hb_ways, u8 math)
+{

tools/testing/cxl/test/cxl_translate.c line 92

Quoted 10 lines
tools/testing/cxl/test/cxl_translate.c new file +10 -0
+	u64 offset = hpa_offset;
+
+	/* Reverse XOR mapping if specified */
+	if (math == XOR_MATH)
+		offset = cxl_do_xormap_calc(cximsd, hpa_offset, hb_ways);
+
+	return cxl_calculate_dpa_offset(offset, r_eiw, r_eig);
+}
+
+/**

Not sure why this one was special.

Quoted 21 lines
tools/testing/cxl/test/cxl_translate.c new file +21 -0
+ * to_pos - Convert HPA offset to interleave position
+ *
+ * @hpa_offset: host physical address offset
+ * @r_eiw: region encoded interleave ways
+ * @r_eig: region encoded interleave granularity
+ * @hb_ways: host bridge interleave ways
+ * @math: interleave arithmetic (MODULO_MATH or XOR_MATH)
+ *
+ * Returns: devices position in region interleave
+ */
+static u64 to_pos(u64 hpa_offset, u8 r_eiw, u16 r_eig, u8 hb_ways, u8 math)
+{
+	u64 offset = hpa_offset;
+
+	/* Reverse XOR mapping if specified */
+	if (math == XOR_MATH)
+		offset = cxl_do_xormap_calc(cximsd, hpa_offset, hb_ways);
+
+	return cxl_calculate_position(offset, r_eiw, r_eig);
+}
+
author maintainer
#13 lore raw

Re: [PATCH 3/3] cxl/test: Add cxl_translate module for address translation testing

Quoted 19 lines · Jonathan Cameron
Quoted 16 lines · On Mon, 4 Aug 2025 01:52:41 -0700 [email protected] wrote:

From: Alison Schofield <[email protected]>

Add a loadable test module that validates CXL address translation calculations using parameterized test vectors. The module tests both host to device and device to host address translations for Modulo and XOR interleave arithmetic.

Test vectors are provided as module parameters in the format:
"dpa pos r_eiw r_eig hb_ways math expected_spa"

The module performs round-trip validation:
1. Translate a DPA and position to a SPA
2. Verify the result matches expected SPA
3. Translate that SPA back to a DPA and position
4. Verify round-trip consistency

Quoted 7 lines
Quoted 4 lines

The module accesses the refactored translation functions through the exports made available only to CXL test modules.

Nice little test. Trivial comments inline.

:)

snip

Quoted 4 lines

Might as well make this kernel-doc even if nothing is going to build these docs currently. You can probably still point the scripts at the file and to check docs are complete etc.

Will do for all of these and tidy'd up the format too.

Quoted 14 lines
Quoted 14 lines

+ * to_hpa - translate a DPA offset and position to HPA offset
+ *
+ * @dpa_offset: device physical address offset
+ * @pos: devices position in interleave
+ * @r_eiw: region encoded interleave ways
+ * @r_eig: region encoded interleave granularity
+ * @hb_ways: host bridge interleave ways
+ * @math: interleave arithmetic (MODULO_MATH or XOR_MATH)
+ *
+ * Returns: host physical address offset
+ */

tools/testing/cxl/test/cxl_translate.c new file +2 -0
+static u64 to_hpa(u64 dpa_offset, int pos, u8 r_eiw, u16 r_eig, u8 hb_ways,
+		  u8 math)

snip