CXL: Add a loadable module for address translation
Alison Schofield
maintainerv3 cover CXL: Add a loadable module for address translation
Changes in v3:
- Rebase on latest cxl/next
This set is posted for review based on cxl/next because it depends on SPA->DPA translation work only in cxl/next (the poison by region offset set). When cxl/next becomes v6.18-rc1, I'll rebase and resend.
- Replace __mock_exports with EXPORT_SYMBOL_GPL_FOR_MODULES()
- Replace (u64)GENMASK() w GENMASK_ULL() (DaveJ)
- Refactor cxled lookup in cxl_dpa_to_hpa() (DaveJ)
- Move struct cxl_cxims_data to cxl.h (DaveJ)
Below is new functionality in v3:
- Add and EXPORT cxl_validate_translation_parameter().
This consolidates param validation in region.c and exporting it enables testing.
- Add a test of cxl_validate_translation_parameter() to test module.
- Add a random test of the exported CXL Driver translation functions.
Changes in v2:
- Rebase on cxl/next
This set has dependencies on patches only in cxl/next.
- Make the new helpers static by default, exportable for tests (Dan)
- Restore useful dev_dbg() message for invalid position (Jonathan)
- Create and free cxims data struct in test module (Dan)
- Make some test module comments kdoc's (Jonathan)
- Move a block comment to new helper (Jonathan)
- Remove useless check for ULLONG_MAX (Jonathan)
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 also with a standalone testable function.
3. Adds the test module that validates both Host to Device, and Device to Host address translations.
The CXL Driver functions are made available to the test module using EXPORT_SYMBOL_GPL_FOR_MODULES() for cxl_translate only.
The companion CXL Unit Test script is 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 | 41 ++- drivers/cxl/core/region.c | 204 ++++++++---- drivers/cxl/cxl.h | 19 ++ tools/testing/cxl/test/Kbuild | 1 + tools/testing/cxl/test/cxl_translate.c | 445 +++++++++++++++++++++++++ 5 files changed, 638 insertions(+), 72 deletions(-) create mode 100644 tools/testing/cxl/test/cxl_translate.c
base-commit: c10b92358bdf87959c9f0b9b7efa8ed946e1fb48
Signature · 1 line
2.37.3
Alison Schofield
maintainerPATCH v3 1/3 cxl/region: Refactor address translation funcs for testing
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.
Perform additional parameter validation to protect against a test module sending bad parameters. Export the validation function, as well as the three core translation functions for use by test module cxl_translate only.
This refactoring enables unit testing of the address translation logic with controlled inputs, while preserving identical functionality in the existing code paths.
--- drivers/cxl/core/region.c | 204 ++++++++++++++++++++++++++------------ drivers/cxl/cxl.h | 6 ++ 2 files changed, 149 insertions(+), 61 deletions(-)
drivers/cxl/core/region.c +143 -61
@@ -2934,28 +2934,119 @@ static bool has_spa_to_hpa(struct cxl_root_decoder *cxlrd)return cxlrd->ops && cxlrd->ops->spa_to_hpa; } -u64 cxl_dpa_to_hpa (struct cxl_region *cxlr,const struct cxl_memdev *cxlmd, -u64 dpa) +#define CXL_POS_ZERO 0 +/** +* cxl_validate_translation_params +* @eiw: encoded interleave ways +* @eig: encoded interleave granularity +* @pos: position in interleave +* +* Callers pass CXL_POS_ZERO when no position parameter needs validating. +* +* Returns: 0 on success, -EINVAL on first invalid parameter +*/ +int cxl_validate_translation_params (u8 eiw,u16 eig,int pos) { -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 ; -u16 eig =0 ; -u8 eiw =0 ; -int pos; - -for (int i =0 ; i < p->nr_targets; i++) { - cxled = p->targets[i]; -if (cxlmd ==cxled_to_memdev (cxled)) -break ; +int ways, gran; + +if (eiw_to_ways (eiw, &ways)) { +pr_debug ("%s: invalid eiw=%u\n" , __func__, eiw); +return -EINVAL ; + } +if (eig_to_granularity (eig, &gran)) { +pr_debug ("%s: invalid eig=%u\n" , __func__, eig); +return -EINVAL ; + } +if (pos <0 || pos >= ways) { +pr_debug ("%s: invalid pos=%d for ways=%u\n" , __func__, pos, + ways); +return -EINVAL ; } -if (!cxled || cxlmd !=cxled_to_memdev (cxled)) + +return 0 ; +} +EXPORT_SYMBOL_GPL_FOR_MODULES (cxl_validate_translation_params,"cxl_translate" ); + +u64 cxl_calculate_dpa_offset (u64 hpa_offset,u8 eiw,u16 eig) +{ +u64 dpa_offset, bits_lower, bits_upper, temp; +int ret; + + ret =cxl_validate_translation_params (eiw, eig,CXL_POS_ZERO ); +if (ret)return ULLONG_MAX ; - pos = cxled->pos; -ways_to_eiw (p->interleave_ways, &eiw); -granularity_to_eig (p->interleave_granularity, &eig); +/* +* 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 &= ~GENMASK_ULL (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; +} +EXPORT_SYMBOL_GPL_FOR_MODULES (cxl_calculate_dpa_offset,"cxl_translate" ); + +int cxl_calculate_position (u64 hpa_offset,u8 eiw,u16 eig) +{ +unsigned int ways =0 ; +u64 shifted, rem; +int pos, ret; + + ret =cxl_validate_translation_params (eiw, eig,CXL_POS_ZERO ); +if (ret) +return ret; + +if (!eiw) +/* position is 0 if no interleaving */ +return 0 ; + +/* +* 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; +} +EXPORT_SYMBOL_GPL_FOR_MODULES (cxl_calculate_position,"cxl_translate" ); + +u64 cxl_calculate_hpa_offset (u64 dpa_offset,int pos,u8 eiw,u16 eig) +{ +u64 mask_upper, hpa_offset, bits_upper; +int ret; + + ret =cxl_validate_translation_params (eiw, eig, pos); +if (ret) +return ULLONG_MAX ;/* * The device position in the region interleave set was removed
@@ -2967,9 +3058,6 @@ u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,* 8.2.4.19.13 Implementation Note: Device Decode Logic */ -/* Remove the dpa base */ - dpa_offset = dpa -cxl_dpa_resource_start (cxled); - mask_upper =GENMASK_ULL (51 , eig +8 );if (eiw <8 ) {
@@ -2984,6 +3072,37 @@ u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,/* The lower bits remain unchanged */ hpa_offset |= dpa_offset &GENMASK_ULL (eig +7 ,0 ); +return hpa_offset; +} +EXPORT_SYMBOL_GPL_FOR_MODULES (cxl_calculate_hpa_offset,"cxl_translate" ); + +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); +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; + +for (int i =0 ; i < p->nr_targets; i++) { +if (cxlmd ==cxled_to_memdev (p->targets[i])) { + cxled = p->targets[i]; +break ; + } + } +if (!cxled) +return ULLONG_MAX ; + + pos = cxled->pos; +ways_to_eiw (p->interleave_ways, &eiw); +granularity_to_eig (p->interleave_granularity, &eig); + + dpa_offset = dpa -cxl_dpa_resource_start (cxled); + 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;
@@ -3016,8 +3135,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;
@@ -3039,50 +3156,15 @@ 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; - } + + pos =cxl_calculate_position (hpa_offset, eiw, eig);if (pos <0 || pos >= p->nr_targets) {dev_dbg (&cxlr->dev,"Invalid position %d for %d targets\n" , 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);/* Look-up and return the result: a memdev and a DPA */ for (int i =0 ; i < p->nr_targets; i++) {
drivers/cxl/cxl.h +6 -0
@@ -736,6 +736,12 @@ static inline bool is_cxl_root(struct cxl_port *port)return port->uport_dev == port->dev.parent; } +/* Address translation functions exported to cxl_translate test module only */ +int cxl_validate_translation_params (u8 eiw,u16 eig,int pos); +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
Alison Schofield
maintainerPATCH v3 2/3 cxl/acpi: Make the XOR calculations available for testing
In reply to Alison SchofieldIn preparation for adding a test module that can exercise the address translation functions performed by the CXL Driver, refactor the XOR implementation like this:
- Extract the core calculation into a standalone helper function,
- Export the new function for use by test module cxl_translate only,
- Enhance the parameter validation since this new function will be called from a test module with no guarantee of valid parameters,
- Move the define of struct cxl_cxims_data to cxl.h so the test module can build xormaps.
--- drivers/cxl/acpi.c | 41 ++++++++++++++++++++++++++++++----------- drivers/cxl/cxl.h | 13 +++++++++++++ 2 files changed, 43 insertions(+), 11 deletions(-)
drivers/cxl/acpi.c +30 -11
@@ -11,25 +11,36 @@#include "cxlpci.h" #include "cxl.h" -struct cxl_cxims_data { -int nr_maps; - u64 xormaps[] __counted_by(nr_maps); -}; -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) +#define HBIW_TO_NR_MAPS_SIZE (CXL_DECODER_MAX_INTERLEAVE + 1) +static const int hbiw_to_nr_maps[HBIW_TO_NR_MAPS_SIZE ] = { + [1 ] =0 , [2 ] =1 , [3 ] =0 , [4 ] =2 , [6 ] =1 , [8 ] =3 , [12 ] =2 , [16 ] =4 +}; + +static const int valid_hbiw[] = {1 ,2 ,3 ,4 ,6 ,8 ,12 ,16 }; + +u64 cxl_do_xormap_calc (struct cxl_cxims_data *cximsd,u64 addr,int hbiw) { -struct cxl_cxims_data *cximsd = cxlrd->platform_data; -int hbiw = cxlrd->cxlsd.nr_targets; +int nr_maps_to_apply =-1 ;u64 val;int pos; -/* No xormaps for host bridge interleave ways of 1 or 3 */ -if (hbiw ==1 || hbiw ==3 ) -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
@@ -60,6 +71,14 @@ static u64 cxl_apply_xor_maps(struct cxl_root_decoder *cxlrd, u64 addr)return addr; } +EXPORT_SYMBOL_GPL_FOR_MODULES (cxl_do_xormap_calc,"cxl_translate" ); + +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;
drivers/cxl/cxl.h +13 -0
@@ -741,6 +741,19 @@ int cxl_validate_translation_params(u8 eiw, u16 eig, int pos);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); +struct cxl_cxims_data { +int nr_maps; + u64 xormaps[] __counted_by(nr_maps); +}; + +#if IS_ENABLED (CONFIG_CXL_ACPI ) +u64 cxl_do_xormap_calc (struct cxl_cxims_data *cximsd,u64 addr,int hbiw); +#else +static inline u64 cxl_do_xormap_calc (struct cxl_cxims_data *cximsd,u64 addr,int hbiw) +{ +return ULLONG_MAX ; +} +#endif int cxl_num_decoders_committed (struct cxl_port *port);bool is_cxl_port (const struct device *dev);
Signature · 1 line
2.37.3
Alison Schofield
maintainerPATCH v3 3/3 cxl/test: Add cxl_translate module for address translation testing
In reply to Alison SchofieldAdd 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.
Two types of testing are provided:
1. Parameterized test vectors:
Test vectors are passed as module parameters in the format:
"dpa pos r_eiw r_eig hb_ways math expected_spa".
Round-trip validation is performed:
- Translate a DPA and position to a SPA
- Verify the result matches expected SPA
- Translate that SPA back to a DPA and position
- Verify round-trip consistency
2. Internal validation testing:
When no test vectors are provided, the module performs validation of the translation functions by checking parameter boundaries and running 10,000 iterations of randomly generated valid parameters to exercise the core calculation functions.
The module uses the CXL Driver translation functions through symbols exported exclusively for cxl_translate.
--- tools/testing/cxl/test/Kbuild | 1 + tools/testing/cxl/test/cxl_translate.c | 445 +++++++++++++++++++++++++ 2 files changed, 446 insertions(+) create mode 100644 tools/testing/cxl/test/cxl_translate.c
tools/testing/cxl/test/Kbuild +1 -0
@@ -4,6 +4,7 @@ ccflags-y := -I$(srctree)/drivers/cxl/ -I$(srctree)/drivers/cxl/core 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
tools/testing/cxl/test/cxl_translate.c new file +445 -0
445 lines not on this page · raw message
Signature · 1 line
2.37.3
Re: [PATCH v3 2/3] cxl/acpi: Make the XOR calculations available for testing
In reply to Alison SchofieldQuoted 4 lines · On Wed, 17 Sep 2025 17:10:31 -0700 Alison Schofield…
In preparation for adding a test module that can exercise the address translation functions performed by the CXL Driver, refactor the XOR implementation like this:
Quoted 8 lines
- Extract the core calculation into a standalone helper function,
- Export the new function for use by test modulecxl_translateonly,
- Enhance the parameter validation since this new function will be called from a test module with no guarantee of valid parameters,
- Move the define of structcxl_cxims_datatocxl.hso the test module can build xormaps.Signed-off-by Alison Schofield <[email protected]>
Seems fine to me
Re: [PATCH v3 1/3] cxl/region: Refactor address translation funcs for testing
In reply to Alison SchofieldQuoted 10 lines · Alison Schofield
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.Perform additional parameter validation to protect against a test module sending bad parameters. Export the validation function, as well as the three core translation functions for use by test module
cxl_translateonly.
Quoted 6 lines
This refactoring enables unit testing of the address translation logic with controlled inputs, while preserving identical functionality in the existing code paths.
Reviewed-by Jonathan Cameron <[email protected]>Signed-off-by Alison Schofield <[email protected]>
Quoted 275 lines
--- drivers/cxl/core/region.c | 204 ++++++++++++++++++++++++++------------ drivers/cxl/cxl.h | 6 ++ 2 files changed, 149 insertions(+), 61 deletions(-)drivers/cxl/core/region.c +143
-61@@ -2934,28 +2934,119 @@ static bool has_spa_to_hpa(struct cxl_root_decoder *cxlrd)return cxlrd->ops && cxlrd->ops->spa_to_hpa; } -u64 cxl_dpa_to_hpa (struct cxl_region *cxlr,const struct cxl_memdev *cxlmd, -u64 dpa) +#define CXL_POS_ZERO 0 +/** +* cxl_validate_translation_params +* @eiw: encoded interleave ways +* @eig: encoded interleave granularity +* @pos: position in interleave +* +* Callers pass CXL_POS_ZERO when no position parameter needs validating. +* +* Returns: 0 on success, -EINVAL on first invalid parameter +*/ +int cxl_validate_translation_params (u8 eiw,u16 eig,int pos) { -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 ; -u16 eig =0 ; -u8 eiw =0 ; -int pos; - -for (int i =0 ; i < p->nr_targets; i++) { - cxled = p->targets[i]; -if (cxlmd ==cxled_to_memdev (cxled)) -break ; +int ways, gran; + +if (eiw_to_ways (eiw, &ways)) { +pr_debug ("%s: invalid eiw=%u\n" , __func__, eiw); +return -EINVAL ; + } +if (eig_to_granularity (eig, &gran)) { +pr_debug ("%s: invalid eig=%u\n" , __func__, eig); +return -EINVAL ; + } +if (pos <0 || pos >= ways) { +pr_debug ("%s: invalid pos=%d for ways=%u\n" , __func__, pos, + ways); +return -EINVAL ; } -if (!cxled || cxlmd !=cxled_to_memdev (cxled)) + +return 0 ; +} +EXPORT_SYMBOL_GPL_FOR_MODULES (cxl_validate_translation_params,"cxl_translate" ); + +u64 cxl_calculate_dpa_offset (u64 hpa_offset,u8 eiw,u16 eig) +{ +u64 dpa_offset, bits_lower, bits_upper, temp; +int ret; + + ret =cxl_validate_translation_params (eiw, eig,CXL_POS_ZERO ); +if (ret)return ULLONG_MAX ; - pos = cxled->pos; -ways_to_eiw (p->interleave_ways, &eiw); -granularity_to_eig (p->interleave_granularity, &eig); +/* +* 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 &= ~GENMASK_ULL (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; +} +EXPORT_SYMBOL_GPL_FOR_MODULES (cxl_calculate_dpa_offset,"cxl_translate" ); + +int cxl_calculate_position (u64 hpa_offset,u8 eiw,u16 eig) +{ +unsigned int ways =0 ; +u64 shifted, rem; +int pos, ret; + + ret =cxl_validate_translation_params (eiw, eig,CXL_POS_ZERO ); +if (ret) +return ret; + +if (!eiw) +/* position is 0 if no interleaving */ +return 0 ; + +/* +* 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; +} +EXPORT_SYMBOL_GPL_FOR_MODULES (cxl_calculate_position,"cxl_translate" ); + +u64 cxl_calculate_hpa_offset (u64 dpa_offset,int pos,u8 eiw,u16 eig) +{ +u64 mask_upper, hpa_offset, bits_upper; +int ret; + + ret =cxl_validate_translation_params (eiw, eig, pos); +if (ret) +return ULLONG_MAX ;/* * The device position in the region interleave set was removed @@ -2967,9 +3058,6 @@ u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,* 8.2.4.19.13 Implementation Note: Device Decode Logic */ -/* Remove the dpa base */ - dpa_offset = dpa -cxl_dpa_resource_start (cxled); - mask_upper =GENMASK_ULL (51 , eig +8 );if (eiw <8 ) {@@ -2984,6 +3072,37 @@ u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,/* The lower bits remain unchanged */ hpa_offset |= dpa_offset &GENMASK_ULL (eig +7 ,0 ); +return hpa_offset; +} +EXPORT_SYMBOL_GPL_FOR_MODULES (cxl_calculate_hpa_offset,"cxl_translate" ); + +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); +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; + +for (int i =0 ; i < p->nr_targets; i++) { +if (cxlmd ==cxled_to_memdev (p->targets[i])) { + cxled = p->targets[i]; +break ; + } + } +if (!cxled) +return ULLONG_MAX ; + + pos = cxled->pos; +ways_to_eiw (p->interleave_ways, &eiw); +granularity_to_eig (p->interleave_granularity, &eig); + + dpa_offset = dpa -cxl_dpa_resource_start (cxled); + 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;@@ -3016,8 +3135,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;@@ -3039,50 +3156,15 @@ 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; - } + + pos =cxl_calculate_position (hpa_offset, eiw, eig);if (pos <0 || pos >= p->nr_targets) {dev_dbg (&cxlr->dev,"Invalid position %d for %d targets\n" , 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);/* Look-up and return the result: a memdev and a DPA */ for (int i =0 ; i < p->nr_targets; i++) {drivers/cxl/cxl.h +6
-0@@ -736,6 +736,12 @@ static inline bool is_cxl_root(struct cxl_port *port)return port->uport_dev == port->dev.parent; } +/* Address translation functions exported to cxl_translate test module only */ +int cxl_validate_translation_params (u8 eiw,u16 eig,int pos); +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);
Re: [PATCH v3 2/3] cxl/acpi: Make the XOR calculations available for testing
In reply to Alison SchofieldQuoted 4 lines · Alison Schofield
In preparation for adding a test module that can exercise the address translation functions performed by the CXL Driver, refactor the XOR implementation like this:
Quoted 8 lines
- Extract the core calculation into a standalone helper function,
- Export the new function for use by test modulecxl_translateonly,
- Enhance the parameter validation since this new function will be called from a test module with no guarantee of valid parameters,
- Move the define of structcxl_cxims_datatocxl.hso the test module can build xormaps.Signed-off-by Alison Schofield <[email protected]>
Quoted 96 lines
--- drivers/cxl/acpi.c | 41 ++++++++++++++++++++++++++++++----------- drivers/cxl/cxl.h | 13 +++++++++++++ 2 files changed, 43 insertions(+), 11 deletions(-)drivers/cxl/acpi.c +30
-11@@ -11,25 +11,36 @@#include "cxlpci.h" #include "cxl.h" -struct cxl_cxims_data { -int nr_maps; - u64 xormaps[] __counted_by(nr_maps); -}; -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) +#define HBIW_TO_NR_MAPS_SIZE (CXL_DECODER_MAX_INTERLEAVE + 1) +static const int hbiw_to_nr_maps[HBIW_TO_NR_MAPS_SIZE ] = { + [1 ] =0 , [2 ] =1 , [3 ] =0 , [4 ] =2 , [6 ] =1 , [8 ] =3 , [12 ] =2 , [16 ] =4 +}; + +static const int valid_hbiw[] = {1 ,2 ,3 ,4 ,6 ,8 ,12 ,16 }; + +u64 cxl_do_xormap_calc (struct cxl_cxims_data *cximsd,u64 addr,int hbiw) { -struct cxl_cxims_data *cximsd = cxlrd->platform_data; -int hbiw = cxlrd->cxlsd.nr_targets; +int nr_maps_to_apply =-1 ;u64 val;int pos; -/* No xormaps for host bridge interleave ways of 1 or 3 */ -if (hbiw ==1 || hbiw ==3 ) -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 @@ -60,6 +71,14 @@ static u64 cxl_apply_xor_maps(struct cxl_root_decoder *cxlrd, u64 addr)return addr; } +EXPORT_SYMBOL_GPL_FOR_MODULES (cxl_do_xormap_calc,"cxl_translate" ); + +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;drivers/cxl/cxl.h +13
-0@@ -741,6 +741,19 @@ int cxl_validate_translation_params(u8 eiw, u16 eig, int pos);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); +struct cxl_cxims_data { +int nr_maps; + u64 xormaps[] __counted_by(nr_maps); +}; + +#if IS_ENABLED (CONFIG_CXL_ACPI ) +u64 cxl_do_xormap_calc (struct cxl_cxims_data *cximsd,u64 addr,int hbiw); +#else +static inline u64 cxl_do_xormap_calc (struct cxl_cxims_data *cximsd,u64 addr,int hbiw) +{ +return ULLONG_MAX ; +} +#endif int cxl_num_decoders_committed (struct cxl_port *port);bool is_cxl_port (const struct device *dev);