cxl: Support Back-Invalidate
Davidlohr Bueso
maintainerv2 cover cxl: Support Back-Invalidate
Hello,
Changes from v1 (https://lore.kernel.org/all/[email protected]/):
o Merged patch 1 throughout the series.
o Updated cxl references to 4.0 (Jonathan)
o Split bi into bi_rt and bi_decoder in struct cxl_component_regs (Jonathan)
o Use port->regs instead of a new uport_regs (Jonathan, Ben, Dave)
o Simplify/rework reg map init (Ben)
o The dealloc teardown is added to the setup call for cxl_mem lifecycle. (Ben)
o Cleanup while loops for topology iteration (Ben)
This series is the initial plumbing to enable HDM-DB in Linux. The model allows Type 2 and Type 3 devices to expose their local memory to the host CPU in a coherent manner. In alignment with what was discussed at 2024 LPC type2 support session, this series takes the Type 3 memory expander approach, which is more direct. Further, afaik there is no Type 2 + BI hardware out there.
A flagship use case of Type 3 + BI is coherent shared memory, and there is currently a big gap in this regard (ie: GFAM). Another is P2P via PCIe UIO, which is also lacking today. Media Operation (4402h) for ranged sanitize/zero also trigger snoops and there are some patches out there[0]. As such this series focuses on BI enablement in terms of discovery and configuration.
The idea is for Type 3 memdevs and future Type 2 devices to make use of cxlds->bi when committing HDM decoders, such that different device coherence models can be differentiated as:
type2 hdm-d: cxlds->type == CXL_DEVTYPE_DEVMEM && cxlds->bi == false type2 hdm-db: cxlds->type == CXL_DEVTYPE_DEVMEM && cxlds->bi == true type3 hdm-h: cxlds->type == CXL_DEVTYPE_CLASSMEM && cxlds->bi == false type3 hdm-db: cxlds->type == CXL_DEVTYPE_CLASSMEM && cxlds->bi == true
Because ->bi becoming true does not depend on auto-committing upon HDM decoder enumeration (port driver), for now an already-committed BI decoder during enumeration is unsupported and the driver errors out.
o Patch 1 probes BI capabilities during register discovery, and maps BI Decoder registers.
o Patch 2 implements the BI-ID allocation (and deallocation) via a new cxl_bi_setup().
o Patch 3 introduces cxl_dpa_set_coherence() to switch between host-only and device-coherent modes.
o Patch 4 adds ABI (sysfs) for HDM-DB region creation.
o Patch 5 trivially wires cxl_bi_setup() into cxl_mem_probe().
Testing has been done with the qemu HDM-DB Type 3 counterpart (now merged).
1. HDM Decoder with BI through ad-hoc region creation.
------------------------------------------------------
# cxl list -D
[
{
"decoder":"decoder0.0",
"resource":4563402752,
"size":10737418240,
"interleave_ways":1,
"max_available_extent":10737418240,
"pmem_capable":true,
"volatile_capable":true,
"accelmem_capable":true,
"nr_targets":1
}
]Program the endpoint decoder
# echo ram > /sys/bus/cxl/devices/decoder2.0/mode # echo 1 > /sys/bus/cxl/devices/decoder2.0/bi # echo 0x40000000 > /sys/bus/cxl/devices/decoder2.0/dpa_size
Create a region in the root decoder
# echo region0 > /sys/bus/cxl/devices/decoder0.0/create_ram_bi_region [ 60.287852] cxl_core:cxl_region_can_probe:4085: cxl_region region0: config state: 0 [ 60.289161] cxl_core:cxl_bus_probe:2322: cxl_region region0: probe: -6 [ 60.290562] cxl_core:devm_cxl_add_region:2735: cxl_acpi ACPI0017:00: decoder0.0: created region0
Configure the region with the same IG, IW the root and endpoint decoders
# echo 256 > /sys/bus/cxl/devices/region0/interleave_granularity # echo 1 > /sys/bus/cxl/devices/region0/interleave_ways # echo 0x40000000 > /sys/bus/cxl/devices/region0/size
Link the endpoint decoder as a target in the region
# echo decoder2.0 > /sys/bus/cxl/devices/region0/target0 [ 107.024528] cxl_core:cxl_port_attach_region:1292: cxl region0: mem0:endpoint2 decoder2.0 add: mem0:decoder2.0 @ 0 next: none nr_eps: 1 nr_targets: 1 [ 107.027218] cxl_core:cxl_port_attach_region:1292: cxl region0: pci0000:0c:port1 decoder1.0 add: mem0:decoder2.0 @ 0 next: mem0 nr_eps: 1 nr_targets: 1 [ 107.029212] cxl_core:cxl_port_setup_targets:1610: cxl region0: pci0000:0c:port1 iw: 1 ig: 256 [ 107.030462] cxl_core:cxl_port_setup_targets:1635: cxl region0: pci0000:0c:port1 target[0] = 0000:0c:00.0 for mem0:decoder2.0 @ 0 [ 107.032175] cxl_core:cxl_calc_interleave_pos:2005: cxl_mem mem0: decoder:decoder2.0 parent:0000:0d:00.0 port:endpoint2 range:0x110000000-0x14fffffff pos:0 [ 107.034065] cxl_core:cxl_region_attach:2213: cxl decoder2.0: Test cxl_calc_interleave_pos(): success test_pos:0 cxled->pos:0
Commit the changes
# echo 1 > /sys/bus/cxl/devices/region0/commit
# cxl list -D (tooling still needs updated to show decoder target type)
[
{
"root decoders":[
{
"decoder":"decoder0.0",
"resource":4563402752,
"size":10737418240,
"interleave_ways":1,
"max_available_extent":9663676416,
"pmem_capable":true,
"volatile_capable":true,
"accelmem_capable":true,
"nr_targets":1
}
]
},
{
"port decoders":[
{
"decoder":"decoder1.0",
"resource":4563402752,
"size":1073741824,
"interleave_ways":1,
"region":"region0",
"nr_targets":1
}
]
},
{
"endpoint decoders":[
{
"decoder":"decoder2.0",
"resource":4563402752,
"size":1073741824,
"interleave_ways":1,
"region":"region0",
"dpa_resource":0,
"dpa_size":1073741824,
"mode":"ram"
}
]
}
]2. On a Type 3 HDM-DB volatile device, create one BI and one regular region.
---------------------------------------------------------------------------
# echo ram > /sys/bus/cxl/devices/decoder2.0/mode # echo 1 > /sys/bus/cxl/devices/decoder2.0/bi # echo 0x20000000 > /sys/bus/cxl/devices/decoder2.0/dpa_size # echo region0 > /sys/bus/cxl/devices/decoder0.0/create_ram_bi_region # echo 256 > /sys/bus/cxl/devices/region0/interleave_granularity # echo 1 > /sys/bus/cxl/devices/region0/interleave_ways # echo 0x20000000 > /sys/bus/cxl/devices/region0/size # echo decoder2.0 > /sys/bus/cxl/devices/region0/target0 # echo 1 > /sys/bus/cxl/devices/region0/commit
# echo ram > /sys/bus/cxl/devices/decoder2.1/mode # echo 0 > /sys/bus/cxl/devices/decoder2.1/bi (this is already the default) # echo 0x20000000 > /sys/bus/cxl/devices/decoder2.1/dpa_size # echo region1 > /sys/bus/cxl/devices/decoder0.0/create_ram_region # echo 256 > /sys/bus/cxl/devices/region1/interleave_granularity # echo 1 > /sys/bus/cxl/devices/region1/interleave_ways # echo 0x20000000 > /sys/bus/cxl/devices/region1/size # echo decoder2.1 > /sys/bus/cxl/devices/region1/target0 # echo 1 > /sys/bus/cxl/devices/region1/commit
# cxl list -D
[
{
"root decoders":[
{
"decoder":"decoder0.0",
"resource":4563402752,
"size":10737418240,
"interleave_ways":1,
"max_available_extent":9663676416,
"pmem_capable":true,
"volatile_capable":true,
"accelmem_capable":true,
"nr_targets":1
}
]
},
{
"port decoders":[
{
"decoder":"decoder1.0",
"resource":4563402752,
"size":536870912,
"interleave_ways":1,
"region":"region0",
"nr_targets":1
},
{
"decoder":"decoder1.1",
"resource":5100273664,
"size":536870912,
"interleave_ways":1,
"region":"region1",
"nr_targets":1
}
]
},
{
"endpoint decoders":[
{
"decoder":"decoder2.0",
"resource":4563402752,
"size":536870912,
"interleave_ways":1,
"region":"region0",
"dpa_resource":0,
"dpa_size":536870912,
"mode":"ram"
},
{
"decoder":"decoder2.1",
"resource":5100273664,
"size":536870912,
"interleave_ways":1,
"region":"region1",
"dpa_resource":536870912,
"dpa_size":536870912,
"mode":"ram"
}
]
}
]3. Unbind + Bind
----------------
[ 0.736421] cxl_core:cxl_bi_ctrl_endpoint:1124: cxl_pci 0000:0e:00.0: BI requests enabled
# echo mem0 > /sys/bus/cxl/drivers/cxl_mem/unbind [ 245.823941] cxl_core:cxl_bi_ctrl_endpoint:1124: cxl_pci 0000:0e:00.0: BI requests disabled [ 245.826795] cxl_core:cxl_port_commit_reap:915: cxl_port port1: reap: decoder1.1 commit_end: 0 [ 245.833687] cxl_core:cxl_port_commit_reap:915: cxl_port endpoint2: reap: decoder2.1 commit_end: 0 [ 245.835909] cxl_core:cxl_port_commit_reap:915: cxl_port port1: reap: decoder1.0 commit_end: -1 [ 245.838351] cxl_core:cxl_port_commit_reap:915: cxl_port endpoint2: reap: decoder2.0 commit_end: -1 [ 245.840773] cxl_core:cxl_detach_ep:1582: cxl_mem mem0: disconnect mem0 from port1
# echo mem0 > /sys/bus/cxl/drivers/cxl_mem/bind [ 274.069306] cxl_core:devm_cxl_enumerate_ports:1897: cxl_mem mem0: scan: iter: mem0 dport_dev: 0000:0c:01.0 parent: c [ 274.073705] cxl_core:devm_cxl_enumerate_ports:1903: cxl_mem mem0: found already registered port port1:pci0000:0c [ 274.075243] cxl_core:cxl_port_alloc:793: cxl_mem mem0: host-bridge: pci0000:0c [ 274.078191] cxl_core:cxl_cdat_get_length:487: cxl_port endpoint2: CDAT length 160 [ 274.085249] cxl_core:cxl_port_perf_data_calculate:207: cxl_port endpoint2: Failed to retrieve ep perf coordinates. [ 274.088401] cxl_core:cxl_endpoint_parse_cdat:423: cxl_port endpoint2: Failed to do perf coord calculations. [ 274.113595] cxl_core:init_hdm_decoder:1129: cxl_port endpoint2: decoder2.0: range: 0x0-0xffffffffffffffff iw: 1 ig:6 [ 274.117190] cxl_core:add_hdm_decoder:39: cxl_mem mem0: decoder2.0 added to endpoint2 [ 274.118840] cxl_core:init_hdm_decoder:1129: cxl_port endpoint2: decoder2.1: range: 0x0-0xffffffffffffffff iw: 1 ig:6 [ 274.121448] cxl_core:add_hdm_decoder:39: cxl_mem mem0: decoder2.1 added to endpoint2 [ 274.122833] cxl_core:init_hdm_decoder:1129: cxl_port endpoint2: decoder2.2: range: 0x0-0xffffffffffffffff iw: 1 ig:6 [ 274.125143] cxl_core:add_hdm_decoder:39: cxl_mem mem0: decoder2.2 added to endpoint2 [ 274.126618] cxl_core:init_hdm_decoder:1129: cxl_port endpoint2: decoder2.3: range: 0x0-0xffffffffffffffff iw: 1 ig:6 [ 274.128329] cxl_core:add_hdm_decoder:39: cxl_mem mem0: decoder2.3 added to endpoint2 [ 274.129296] cxl_core:cxl_bus_probe:2322: cxl_port endpoint2: probe: 0 [ 274.130374] cxl_core:devm_cxl_add_port:1001: cxl_mem mem0: endpoint2 added to port1 [ 274.131159] cxl_core:cxl_bi_ctrl_endpoint:1124: cxl_pci 0000:0e:00.0: BI requests enabled [ 274.131858] cxl_core:cxl_bus_probe:2322: cxl_mem mem0: probe: 0
4. Discovery of 2 BI capable devices behind a switch (w/ flit mode)
-------------------------------------------------------------------
[ 0.826785] cxl_core:cxl_probe_component_regs:102: cxl_pci 0000:10:00.0: found BI Decoder capability (0xab4) [ 0.851769] cxl_core:cxl_probe_component_regs:102: cxl_pci 0000:0f:00.0: found BI Decoder capability (0xab4) [ 0.897871] cxl_core:cxl_probe_component_regs:102: pcieport 0000:0c:00.0: found BI Decoder capability (0xab4) [ 1.092842] cxl_port:cxl_port_init_bi:134: cxl_port port2: BI RT registers not found [ 1.099965] cxl_core:cxl_probe_component_regs:96: cxl_port port2: found BI RT capability (0xaa8) [ 1.135528] cxl_core:cxl_probe_component_regs:102: pcieport 0000:0e:02.0: found BI Decoder capability (0xab4) [ 1.140404] cxl_core:cxl_probe_component_regs:102: pcieport 0000:0e:03.0: found BI Decoder capability (0xab4) [ 1.231808] cxl_core:cxl_is_bi_capable:934: cxl_pci 0000:12:00.0: No BI Decoder registers. [ 1.232783] cxl_core:cxl_probe_component_regs:102: pcieport 0000:0e:00.0: found BI Decoder capability (0xab4) [ 1.278969] cxl_port:cxl_port_init_bi:126: cxl_port endpoint5: BI Decoder registers not found [ 1.335363] cxl_core:cxl_is_bi_capable:934: cxl_pci 0000:11:00.0: No BI Decoder registers. [ 1.335988] cxl_core:cxl_probe_component_regs:102: pcieport 0000:0e:01.0: found BI Decoder capability (0xab4) [ 1.541584] cxl_core:__cxl_bi_commit:1027: pcieport 0000:0e:00.0: BI-ID commit wait took 204936us [ 1.547472] cxl_core:cxl_bi_ctrl_endpoint:1124: cxl_pci 0000:0f:00.0: BI requests enabled [ 1.797585] cxl_core:__cxl_bi_commit:1027: pcieport 0000:0e:01.0: BI-ID commit wait took 203346us [ 1.801055] cxl_core:cxl_bi_ctrl_endpoint:1124: cxl_pci 0000:10:00.0: BI requests enabled
5. Mixed Configurations (BI-capable Type 3 but DSP 68b)
-------------------------------------------------------
[ 1.475849] cxl_mem:cxl_mem_probe:160: cxl_mem mem3: BI setup failed rc=-22
# echo 1 > /sys/bus/cxl/devices/decoder5.0/bi -bash: echo: write error: Invalid argument
# echo region0 > /sys/bus/cxl/devices/decoder0.0/create_ram_bi_region # echo decoder5.0 > /sys/bus/cxl/devices/region0/target0 [ 384.895032] cxl_core:cxl_region_attach:2062: cxl region0: mem0:decoder5.0 BI not enabled on device [ 384.896300] cxl_port endpoint5: failed to attach decoder5.0 to region0: -6
6. Detect mismatch between decoder and region types
---------------------------------------------------
# echo 1 > /sys/bus/cxl/devices/decoder2.0/bi # echo region0 > /sys/bus/cxl/devices/decoder0.0/create_ram_region <- no bi ... # echo decoder2.0 > /sys/bus/cxl/devices/region0/target0 [ 24.738308] cxl_core:cxl_region_attach:2048: cxl region0: mem0:decoder2.0 type mismatch: 2 vs 3
7. Corrupted hw (commit timeout handling)
-----------------------------------------
[ 1.311323] cxl_core:__cxl_bi_get_timeout_us:953: pcieport 0000:0e:00.0: Invalid BI commit timeout scale: 9 [ 6.348610] cxl_core:__cxl_bi_commit:1027: pcieport 0000:0e:00.0: BI-ID commit timed out (5000000us) [ 6.356933] cxl_core:__cxl_bi_get_timeout_us:953: pcieport 0000:0e:00.0: Invalid BI commit timeout scale: 9 [ 11.404735] cxl_core:__cxl_bi_commit:1027: pcieport 0000:0e:00.0: BI-ID commit timed out (5000000us) [ 11.409333] cxl_mem:cxl_mem_probe:155: cxl_mem mem2: BI setup failed rc=-110 [ 11.432828] cxl_port:cxl_port_init_bi:126: cxl_port endpoint6: BI Decoder registers not found [ 11.471585] cxl_core:cxl_is_bi_capable:934: cxl_pci 0000:10:00.0: No BI Decoder registers.
Applies against 'fixes' branch (7.1-rc3) from cxl.git.
[0] https://lore.kernel.org/linux-cxl/[email protected]/
Thanks!
Davidlohr Bueso (5):
cxl: Add BI register probing and port initialization cxl/pci: Add BI topology enable/disable cxl/hdm: Add BI coherency support for endpoint decoders
cxl: Add HDM-DB region creation and sysfs interface cxl/mem: Wire up BI for Type 3 device
Documentation/ABI/testing/sysfs-bus-cxl | 39 ++- drivers/cxl/acpi.c | 2 + drivers/cxl/core/core.h | 4 + drivers/cxl/core/hdm.c | 53 +++- drivers/cxl/core/pci.c | 319 ++++++++++++++++++++++++ drivers/cxl/core/port.c | 64 ++++- drivers/cxl/core/region.c | 60 ++++- drivers/cxl/core/regs.c | 14 ++ drivers/cxl/cxl.h | 41 +++ drivers/cxl/cxlmem.h | 2 + drivers/cxl/mem.c | 4 + drivers/cxl/port.c | 89 +++++++ include/cxl/cxl.h | 8 + 13 files changed, 688 insertions(+), 11 deletions(-)
base-commit: 5d6919055dec134de3c40167a490f33c74c12581 --
2.39.5
Davidlohr Bueso
maintainerPATCH v3 1/5 cxl: Add BI register probing and port initialization
Add register probing for BI Route Table and BI Decoder capability structures in cxl_probe_component_regs(), and initialize BI registers during port probe for switch ports and endpoint ports.
Each port's probe initializes its own BI cap and the parent dport's BI Decoder. Switch DSPs are therefore mapped by the children below them (endpoint or sub-switch USP), and a root port is mapped by whichever cxl_port sits directly below it -- a switch USP, or a direct attached endpoint.
--- drivers/cxl/core/regs.c | 14 +++++++ drivers/cxl/cxl.h | 6 +++ drivers/cxl/port.c | 89 +++++++++++++++++++++++++++++++++++++++++ include/cxl/cxl.h | 6 +++ 4 files changed, 115 insertions(+)
drivers/cxl/core/regs.c +14 -0
@@ -92,6 +92,18 @@ void cxl_probe_component_regs(struct device *dev, void __iomem *base, length =CXL_RAS_CAPABILITY_LENGTH ; rmap = &map->ras;break ; +case CXL_CM_CAP_CAP_ID_BI_RT : +dev_dbg (dev,"found BI RT capability (0x%x)\n" , + offset); + length =CXL_BI_RT_CAPABILITY_LENGTH ; + rmap = &map->bi_rt; +break ; +case CXL_CM_CAP_CAP_ID_BI_DECODER : +dev_dbg (dev,"found BI Decoder capability (0x%x)\n" , + offset); + length =CXL_BI_DECODER_CAPABILITY_LENGTH ; + rmap = &map->bi_decoder; +break ;default :dev_dbg (dev,"Unknown CM cap ID: %d (0x%x)\n" , cap_id, offset);
@@ -211,6 +223,8 @@ int cxl_map_component_regs(const struct cxl_register_map *map, } mapinfo[] = { { &map->component_map.hdm_decoder, ®s->hdm_decoder }, { &map->component_map.ras, ®s->ras }, + { &map->component_map.bi_rt, ®s->bi_rt }, + { &map->component_map.bi_decoder, ®s->bi_decoder }, }; int i;
drivers/cxl/cxl.h +6 -0
@@ -42,6 +42,12 @@ extern const struct nvdimm_security_ops *cxl_security_ops;#define CXL_CM_CAP_CAP_ID_RAS 0x2#define CXL_CM_CAP_CAP_ID_HDM 0x5#define CXL_CM_CAP_CAP_HDM_VERSION 1 +#define CXL_CM_CAP_CAP_ID_BI_RT 0xB +#define CXL_CM_CAP_CAP_ID_BI_DECODER 0xC + +/* CXL 4.0 8.2.4.26 / 8.2.4.27 BI Capability Structures */ +#define CXL_BI_RT_CAPABILITY_LENGTH 0xC +#define CXL_BI_DECODER_CAPABILITY_LENGTH 0xC/* HDM decoders CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure */ #define CXL_HDM_DECODER_CAP_OFFSET 0x0
drivers/cxl/port.c +89 -0
107 lines not on this page · raw message
include/cxl/cxl.h +6 -0
@@ -34,10 +34,14 @@ struct cxl_regs {* Common set of CXL Component register block base pointers * @hdm_decoder: CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure * @ras: CXL 2.0 8.2.5.9 CXL RAS Capability Structure +* @bi_rt: CXL 4.0 8.2.4.26 CXL BI Route Table Capability Structure +* @bi_decoder: CXL 4.0 8.2.4.27 CXL BI Decoder Capability Structure */ struct_group_tagged (cxl_component_regs, component, void __iomem *hdm_decoder;void __iomem *ras; +void __iomem *bi_rt; +void __iomem *bi_decoder; );/* * Common set of CXL Device register block base pointers
@@ -80,6 +84,8 @@ struct cxl_reg_map {struct cxl_component_reg_map {struct cxl_reg_map hdm_decoder;struct cxl_reg_map ras; +struct cxl_reg_map bi_rt; +struct cxl_reg_map bi_decoder; };struct cxl_device_reg_map {
Signature · 1 line
2.39.5
Re: [PATCH v3 1/5] cxl: Add BI register probing and port initialization
Quoted 86 lines · Davidlohr Bueso
Add register probing for BI Route Table and BI Decoder capability structures in
cxl_probe_component_regs(), and initialize BI registers during port probe for switch ports and endpoint ports.Each port's probe initializes its own BI cap and the parent dport's BI Decoder. Switch DSPs are therefore mapped by the children below them (endpoint or sub-switch USP), and a root port is mapped by whichever
cxl_portsits directly below it -- a switch USP, or a direct attached endpoint.Signed-off-by Davidlohr Bueso <[email protected]>--- drivers/cxl/core/regs.c | 14 +++++++ drivers/cxl/cxl.h | 6 +++ drivers/cxl/port.c | 89 +++++++++++++++++++++++++++++++++++++++++ include/cxl/cxl.h | 6 +++ 4 files changed, 115 insertions(+)drivers/cxl/core/regs.c +14
-0@@ -92,6 +92,18 @@ void cxl_probe_component_regs(struct device *dev, void __iomem *base, length =CXL_RAS_CAPABILITY_LENGTH ; rmap = &map->ras;break ; +case CXL_CM_CAP_CAP_ID_BI_RT : +dev_dbg (dev,"found BI RT capability (0x%x)\n" , + offset); + length =CXL_BI_RT_CAPABILITY_LENGTH ; + rmap = &map->bi_rt; +break ; +case CXL_CM_CAP_CAP_ID_BI_DECODER : +dev_dbg (dev,"found BI Decoder capability (0x%x)\n" , + offset); + length =CXL_BI_DECODER_CAPABILITY_LENGTH ; + rmap = &map->bi_decoder; +break ;default :dev_dbg (dev,"Unknown CM cap ID: %d (0x%x)\n" , cap_id, offset);@@ -211,6 +223,8 @@ int cxl_map_component_regs(const struct cxl_register_map *map, } mapinfo[] = { { &map->component_map.hdm_decoder, ®s->hdm_decoder }, { &map->component_map.ras, ®s->ras }, + { &map->component_map.bi_rt, ®s->bi_rt }, + { &map->component_map.bi_decoder, ®s->bi_decoder }, }; int i;drivers/cxl/cxl.h +6
-0@@ -42,6 +42,12 @@ extern const struct nvdimm_security_ops *cxl_security_ops;#define CXL_CM_CAP_CAP_ID_RAS 0x2#define CXL_CM_CAP_CAP_ID_HDM 0x5#define CXL_CM_CAP_CAP_HDM_VERSION 1 +#define CXL_CM_CAP_CAP_ID_BI_RT 0xB +#define CXL_CM_CAP_CAP_ID_BI_DECODER 0xC + +/* CXL 4.0 8.2.4.26 / 8.2.4.27 BI Capability Structures */ +#define CXL_BI_RT_CAPABILITY_LENGTH 0xC +#define CXL_BI_DECODER_CAPABILITY_LENGTH 0xC/* HDM decoders CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure */ #define CXL_HDM_DECODER_CAP_OFFSET 0x0drivers/cxl/port.c +11
-0@@ -58,6 +58,91 @@ static int discover_region(struct device *dev, void *unused)return 0 ; } +static int cxl_dport_init_bi (struct cxl_dport *dport) +{ +struct cxl_register_map *map = &dport->reg_map; +struct device *dev = dport->dport_dev; + +/* +* Already mapped. The BI Decoder iomap persists for the dport's +* lifetime via the parent cxl_port's devm context. +*/ +if (dport->regs.bi_decoder) +return 0 ;
drivers/cxl/port.c static int discover_region(struct device *dev, void *unused) · line 81
Quoted 10 lines
drivers/cxl/port.c +10
-0+ +if (!map->component_map.bi_decoder.valid) { +dev_dbg (dev,"BI decoder registers not found\n" ); +return 0 ; + } + +if (cxl_map_component_regs (map, &dport->regs.component, +BIT (CXL_CM_CAP_CAP_ID_BI_DECODER ))) { +dev_dbg (dev,"Failed to map BI decoder capability.\n" ); +return -EINVAL ;
It looks like the return value of this function isn't being used. I haven't looked ahead in the series, but the return type should be void if it's never updated.
What initially caught my eye here was not returning the result of cxl_map_component_regs() here. It's probably better to return the result since it's the error code of the actual problem, even though it will always be -ENOMEM.
Quoted 47 lines
drivers/cxl/port.c +47
-0+ } + +return 0 ; +} + +/* +* Initialize Back-Invalidate registers for @port: map the BI Decoder on +* the upstream parent dport (root port or switch DSP), and map this +* port's own BI capability -- BI RT for a switch USP, BI Decoder for +* an endpoint. +*/ +static void cxl_port_init_bi (struct cxl_port *port) +{ +struct cxl_register_map *map = &port->reg_map; +struct cxl_dport *parent_dport = port->parent_dport; +struct device *dport_dev, *udev; +int cap_id; + +/* no upstream BI registers above host bridges or the cxl_root */ +if (!parent_dport ||is_cxl_root (parent_dport->port)) +return ; + +/* BI requires 256B Flit on the upstream link */ + udev =is_cxl_endpoint (port) ? + port->uport_dev->parent : port->uport_dev; +if (dev_is_pci (udev) && !cxl_pci_flit_256 (to_pci_dev (udev))) +return ; + + dport_dev = parent_dport->dport_dev; +if (dev_is_pci (dport_dev)) { +switch (pci_pcie_type (to_pci_dev (dport_dev))) { +case PCI_EXP_TYPE_ROOT_PORT : +case PCI_EXP_TYPE_DOWNSTREAM : +cxl_dport_init_bi (parent_dport); +break ; +default : +break ; + } + } + +if (is_cxl_endpoint (port)) { +if (port->regs.bi_decoder) +return ; +if (!map->component_map.bi_decoder.valid) { +dev_dbg (&port->dev,"BI Decoder registers not found\n" ); +return ; + }
drivers/cxl/port.c static int discover_region(struct device *dev, void *unused) · line 138
Quoted 10 lines
drivers/cxl/port.c +10
-0+ cap_id =CXL_CM_CAP_CAP_ID_BI_DECODER ; + }else { +if (port->regs.bi_rt) +return ; +if (!map->component_map.bi_rt.valid) { +dev_dbg (&port->dev,"BI RT registers not found\n" ); +return ; + } + cap_id =CXL_CM_CAP_CAP_ID_BI_RT ; + }
Nit: I overlooked that this is now mapping the port's BI capability, maybe a comment above the if statement so that readers don't just gloss over it (like myself :/).
Quoted 11 lines
drivers/cxl/port.c +7
-0+ + map->host = &port->dev; +if (cxl_map_component_regs (map, &port->regs,BIT (cap_id))) +dev_dbg (&port->dev,"Failed to map BI capability 0x%x\n" , + cap_id); +} +static int cxl_switch_port_probe (struct cxl_port *port) {/* Reset nr_dports for rebind of driver */
drivers/cxl/port.c static int cxl_switch_port_probe(struct cxl_port *port) · line 155
Quoted 5 lines
drivers/cxl/port.c +2
-0@@ -66,6 +151,8 @@ static int cxl_switch_port_probe(struct cxl_port *port)/* Cache the data early to ensure is_visible() works */ read_cdat_data (port); +cxl_port_init_bi (port); +
Do you need this to be set up before an endpoint arrives? If not, I would prefer this to be mapped with the other capabilities in cxl_port_add_dport(). That may also allow you to get rid of the below call in the endpoint probe, but I'm not sure about that...
Quoted 40 lines
drivers/cxl/port.c +2
-0return 0 ; }@@ -128,6 +215,8 @@ static int cxl_endpoint_port_probe(struct cxl_port *port)read_cdat_data (port);cxl_endpoint_parse_cdat (port); +cxl_port_init_bi (port); +get_device (&cxlmd->dev); rc =devm_add_action_or_reset (&port->dev, schedule_detach, cxlmd);if (rc)include/cxl/cxl.h +6
-0@@ -34,10 +34,14 @@ struct cxl_regs {* Common set of CXL Component register block base pointers * @hdm_decoder: CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure * @ras: CXL 2.0 8.2.5.9 CXL RAS Capability Structure +* @bi_rt: CXL 4.0 8.2.4.26 CXL BI Route Table Capability Structure +* @bi_decoder: CXL 4.0 8.2.4.27 CXL BI Decoder Capability Structure */ struct_group_tagged (cxl_component_regs, component, void __iomem *hdm_decoder;void __iomem *ras; +void __iomem *bi_rt; +void __iomem *bi_decoder; );/* * Common set of CXL Device register block base pointers @@ -80,6 +84,8 @@ struct cxl_reg_map {struct cxl_component_reg_map {struct cxl_reg_map hdm_decoder;struct cxl_reg_map ras; +struct cxl_reg_map bi_rt; +struct cxl_reg_map bi_decoder; };struct cxl_device_reg_map {
Dave Jiang
maintainerRe: [PATCH v3 1/5] cxl: Add BI register probing and port initialization
In reply to Davidlohr BuesoQuoted 72 lines · Davidlohr Bueso
Add register probing for BI Route Table and BI Decoder capability structures in
cxl_probe_component_regs(), and initialize BI registers during port probe for switch ports and endpoint ports.Each port's probe initializes its own BI cap and the parent dport's BI Decoder. Switch DSPs are therefore mapped by the children below them (endpoint or sub-switch USP), and a root port is mapped by whichever
cxl_portsits directly below it -- a switch USP, or a direct attached endpoint.Signed-off-by Davidlohr Bueso <[email protected]>--- drivers/cxl/core/regs.c | 14 +++++++ drivers/cxl/cxl.h | 6 +++ drivers/cxl/port.c | 89 +++++++++++++++++++++++++++++++++++++++++ include/cxl/cxl.h | 6 +++ 4 files changed, 115 insertions(+)drivers/cxl/core/regs.c +14
-0@@ -92,6 +92,18 @@ void cxl_probe_component_regs(struct device *dev, void __iomem *base, length =CXL_RAS_CAPABILITY_LENGTH ; rmap = &map->ras;break ; +case CXL_CM_CAP_CAP_ID_BI_RT : +dev_dbg (dev,"found BI RT capability (0x%x)\n" , + offset); + length =CXL_BI_RT_CAPABILITY_LENGTH ; + rmap = &map->bi_rt; +break ; +case CXL_CM_CAP_CAP_ID_BI_DECODER : +dev_dbg (dev,"found BI Decoder capability (0x%x)\n" , + offset); + length =CXL_BI_DECODER_CAPABILITY_LENGTH ; + rmap = &map->bi_decoder; +break ;default :dev_dbg (dev,"Unknown CM cap ID: %d (0x%x)\n" , cap_id, offset);@@ -211,6 +223,8 @@ int cxl_map_component_regs(const struct cxl_register_map *map, } mapinfo[] = { { &map->component_map.hdm_decoder, ®s->hdm_decoder }, { &map->component_map.ras, ®s->ras }, + { &map->component_map.bi_rt, ®s->bi_rt }, + { &map->component_map.bi_decoder, ®s->bi_decoder }, }; int i;drivers/cxl/cxl.h +6
-0@@ -42,6 +42,12 @@ extern const struct nvdimm_security_ops *cxl_security_ops;#define CXL_CM_CAP_CAP_ID_RAS 0x2#define CXL_CM_CAP_CAP_ID_HDM 0x5#define CXL_CM_CAP_CAP_HDM_VERSION 1 +#define CXL_CM_CAP_CAP_ID_BI_RT 0xB +#define CXL_CM_CAP_CAP_ID_BI_DECODER 0xC + +/* CXL 4.0 8.2.4.26 / 8.2.4.27 BI Capability Structures */ +#define CXL_BI_RT_CAPABILITY_LENGTH 0xC +#define CXL_BI_DECODER_CAPABILITY_LENGTH 0xC/* HDM decoders CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure */ #define CXL_HDM_DECODER_CAP_OFFSET 0x0drivers/cxl/port.c +0
-0
drivers/cxl/port.c static int discover_region(struct device *dev, void *unused) · line 61
Quoted 4 lines
drivers/cxl/port.c +1
-0@@ -58,6 +58,91 @@ static int discover_region(struct device *dev, void *unused)return 0 ; } +static int cxl_dport_init_bi (struct cxl_dport *dport)
cxl_dport_map_bi()?
Quoted 10 lines
drivers/cxl/port.c +10
-0+{ +struct cxl_register_map *map = &dport->reg_map; +struct device *dev = dport->dport_dev; + +/* +* Already mapped. The BI Decoder iomap persists for the dport's +* lifetime via the parent cxl_port's devm context. +*/ +if (dport->regs.bi_decoder) +return 0 ;
drivers/cxl/port.c static int discover_region(struct device *dev, void *unused) · line 81
Quoted 10 lines
drivers/cxl/port.c +10
-0+ +if (!map->component_map.bi_decoder.valid) { +dev_dbg (dev,"BI decoder registers not found\n" ); +return 0 ; + } + +if (cxl_map_component_regs (map, &dport->regs.component, +BIT (CXL_CM_CAP_CAP_ID_BI_DECODER ))) { +dev_dbg (dev,"Failed to map BI decoder capability.\n" ); +return -EINVAL ;
why not return the error from cxl_map_comonent_regs()?
drivers/cxl/port.c static int discover_region(struct device *dev, void *unused) · line 93
Quoted 12 lines
drivers/cxl/port.c +12
-0+ } + +return 0 ; +} + +/* +* Initialize Back-Invalidate registers for @port: map the BI Decoder on +* the upstream parent dport (root port or switch DSP), and map this +* port's own BI capability -- BI RT for a switch USP, BI Decoder for +* an endpoint. +*/ +static void cxl_port_init_bi (struct cxl_port *port)
cxl_port_map_bi()
Also, why return void? Are errors just ignored?
Quoted 4 lines
drivers/cxl/port.c +4
-0+{ +struct cxl_register_map *map = &port->reg_map; +struct cxl_dport *parent_dport = port->parent_dport; +struct device *dport_dev, *udev;
drivers/cxl/port.c static int discover_region(struct device *dev, void *unused) · line 107
Quoted 10 lines
drivers/cxl/port.c +10
-0+int cap_id; + +/* no upstream BI registers above host bridges or the cxl_root */ +if (!parent_dport ||is_cxl_root (parent_dport->port)) +return ; + +/* BI requires 256B Flit on the upstream link */ + udev =is_cxl_endpoint (port) ? + port->uport_dev->parent : port->uport_dev; +if (dev_is_pci (udev) && !cxl_pci_flit_256 (to_pci_dev (udev)))
I would split this up and do:
if (!dev_is_pci(udev))
return;
...
That way you can skip everything else if it's not PCI. Like that switch a few lines down.
drivers/cxl/port.c static int discover_region(struct device *dev, void *unused) · line 110
Quoted 3 lines
drivers/cxl/port.c +3
-0+return ; + + dport_dev = parent_dport->dport_dev;
Naming it parent_dport_dev makes it easier to keep track of what is what.
drivers/cxl/port.c static int discover_region(struct device *dev, void *unused) · line 115
Quoted 5 lines
drivers/cxl/port.c +5
-0+if (dev_is_pci (dport_dev)) { +switch (pci_pcie_type (to_pci_dev (dport_dev))) { +case PCI_EXP_TYPE_ROOT_PORT : +case PCI_EXP_TYPE_DOWNSTREAM : +cxl_dport_init_bi (parent_dport);
error return value ignored
drivers/cxl/port.c static int discover_region(struct device *dev, void *unused) · line 118
Quoted 3 lines
drivers/cxl/port.c +3
-0+break ; +default : +break ;
Should this be an error?
DJ
Quoted 76 lines
drivers/cxl/port.c +31
-0+ } + } + +if (is_cxl_endpoint (port)) { +if (port->regs.bi_decoder) +return ; +if (!map->component_map.bi_decoder.valid) { +dev_dbg (&port->dev,"BI Decoder registers not found\n" ); +return ; + } + cap_id =CXL_CM_CAP_CAP_ID_BI_DECODER ; + }else { +if (port->regs.bi_rt) +return ; +if (!map->component_map.bi_rt.valid) { +dev_dbg (&port->dev,"BI RT registers not found\n" ); +return ; + } + cap_id =CXL_CM_CAP_CAP_ID_BI_RT ; + } + + map->host = &port->dev; +if (cxl_map_component_regs (map, &port->regs,BIT (cap_id))) +dev_dbg (&port->dev,"Failed to map BI capability 0x%x\n" , + cap_id); +} +static int cxl_switch_port_probe (struct cxl_port *port) {/* Reset nr_dports for rebind of driver */ @@ -66,6 +151,8 @@ static int cxl_switch_port_probe(struct cxl_port *port)/* Cache the data early to ensure is_visible() works */ read_cdat_data (port); +cxl_port_init_bi (port); +return 0 ; }@@ -128,6 +215,8 @@ static int cxl_endpoint_port_probe(struct cxl_port *port)read_cdat_data (port);cxl_endpoint_parse_cdat (port); +cxl_port_init_bi (port); +get_device (&cxlmd->dev); rc =devm_add_action_or_reset (&port->dev, schedule_detach, cxlmd);if (rc)include/cxl/cxl.h +6
-0@@ -34,10 +34,14 @@ struct cxl_regs {* Common set of CXL Component register block base pointers * @hdm_decoder: CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure * @ras: CXL 2.0 8.2.5.9 CXL RAS Capability Structure +* @bi_rt: CXL 4.0 8.2.4.26 CXL BI Route Table Capability Structure +* @bi_decoder: CXL 4.0 8.2.4.27 CXL BI Decoder Capability Structure */ struct_group_tagged (cxl_component_regs, component, void __iomem *hdm_decoder;void __iomem *ras; +void __iomem *bi_rt; +void __iomem *bi_decoder; );/* * Common set of CXL Device register block base pointers @@ -80,6 +84,8 @@ struct cxl_reg_map {struct cxl_component_reg_map {struct cxl_reg_map hdm_decoder;struct cxl_reg_map ras; +struct cxl_reg_map bi_rt; +struct cxl_reg_map bi_decoder; };struct cxl_device_reg_map {
Davidlohr Bueso
maintainerPATCH v3 2/5 cxl/pci: Add BI topology enable/disable
In reply to Davidlohr BuesoImplement cxl_bi_setup(), which walks the CXL port topology to enable BI flows on the device and every component in the path between the device and the root port. The walk is two-pass: first to verify each component advertises BI capability, then to write BI Enable on each dport's BI Decoder Control register and on the device. On failure during the enable pass, partially-enabled dports are rolled back.
The teardown counterpart, cxl_bi_dealloc(), runs automatically on cxl_mem unbind.
cxl_bi_setup() does not touch HDM decoder configuration -- the BI bit in each decoder is programmed separately at commit time. After a successful setup the device is BI-capable but not yet using BI for decode coherence.
--- drivers/cxl/core/pci.c | 319 +++++++++++++++++++++++++++++++++++++++++ drivers/cxl/cxl.h | 28 ++++ include/cxl/cxl.h | 2 + 3 files changed, 349 insertions(+)
drivers/cxl/core/pci.c +319 -0
@@ -7,6 +7,7 @@#include <linux/pci.h> #include <linux/pci-doe.h> #include <linux/aer.h> +#include <linux/string_choices.h> #include <cxlpci.h> #include <cxlmem.h> #include <cxl.h>
@@ -926,3 +927,321 @@ int cxl_port_get_possible_dports(struct cxl_port *port)return ctx.count; } + +static bool cxl_is_bi_capable (struct pci_dev *pdev,void __iomem *bi) +{ +if (pci_pcie_type (pdev) !=PCI_EXP_TYPE_UPSTREAM && !bi) { +dev_dbg (&pdev->dev,"No BI Decoder registers.\n" ); +return false; + } + +return true; +} + +/* limit any insane timeouts from hw */ +#define CXL_BI_COMMIT_MAXTMO_US (5 * USEC_PER_SEC) + +static unsigned long __cxl_bi_get_timeout_us (struct device *dev, +unsigned int scale, +unsigned int base) +{ +static const unsigned long scale_tbl[] = { +1 ,10 ,100 ,1000 ,10000 ,100000 ,1000000 ,10000000 , + }; + +if (scale >=ARRAY_SIZE (scale_tbl)) { +dev_dbg (dev,"Invalid BI commit timeout scale: %u\n" , scale); +return CXL_BI_COMMIT_MAXTMO_US ; + } + +return scale_tbl[scale] * base; +} + +#define ___cxl_bi_commit (dev, bi, ctype) \ +do { \ + u32 status, ctrl; \ + unsigned int scale, base; \ + ktime_t tmo, now, start; \ + unsigned long poll_us, tmo_us; \ + \ + ctrl = readl(bi + CXL_BI_##ctype##_CTRL_OFFSET); \ + writel(ctrl & ~CXL_BI_##ctype##_CTRL_BI_COMMIT, \ + (bi) + CXL_BI_##ctype##_CTRL_OFFSET); \ + writel(ctrl | CXL_BI_##ctype##_CTRL_BI_COMMIT, \ + (bi) + CXL_BI_##ctype##_CTRL_OFFSET); \ + \ + status = readl((bi) + CXL_BI_##ctype##_STATUS_OFFSET); \ + scale = FIELD_GET(CXL_BI_##ctype##_STATUS_BI_COMMIT_TM_SCALE, \ + status); \ + base = FIELD_GET(CXL_BI_##ctype##_STATUS_BI_COMMIT_TM_BASE, \ + status); \ + \ +/* ... and poll */ \ + tmo_us = min_t(unsigned long, CXL_BI_COMMIT_MAXTMO_US, \ +__cxl_bi_get_timeout_us ((dev), scale, base)); \ + poll_us = tmo_us /10 ;/* arbitrary 10% of timeout */ \ + start = now =ktime_get (); \ + tmo =ktime_add_us (now, tmo_us); \ + while (!FIELD_GET(CXL_BI_##ctype##_STATUS_BI_COMMITTED, status) && \ + !FIELD_GET(CXL_BI_##ctype##_STATUS_BI_ERR_NOT_COMMITTED, \ + status)) { \ + if (ktime_after(now, tmo)) { \ + dev_dbg((dev), "BI-ID commit timed out (%luus)\n", \ + tmo_us); \ + return -ETIMEDOUT; \ + } \ + \ + fsleep(poll_us); \ + now = ktime_get(); \ + status = readl((bi) + CXL_BI_##ctype##_STATUS_OFFSET); \ + } \ + \ + if (FIELD_GET(CXL_BI_##ctype##_STATUS_BI_ERR_NOT_COMMITTED, status)) \ + return -EIO; \ + \ + dev_dbg((dev), "BI-ID commit wait took %lluus\n", \ + ktime_to_us(ktime_sub(now, start))); \ +} while (0) + +static int __cxl_bi_commit_rt (struct device *dev,void __iomem *bi) +{ +if (!bi) +return 0 ; + +if (FIELD_GET (CXL_BI_RT_CAPS_EXPLICIT_COMMIT_REQ , +readl (bi +CXL_BI_RT_CAPS_OFFSET ))) +___cxl_bi_commit (dev, bi,RT ); + +return 0 ; +} + +static int __cxl_bi_commit (struct device *dev,void __iomem *bi) +{ +if (!bi) +return -EINVAL ; + +if (!FIELD_GET (CXL_BI_DECODER_CAPS_EXPLICIT_COMMIT_REQ , +readl (bi +CXL_BI_DECODER_CAPS_OFFSET ))) +return 0 ; + +___cxl_bi_commit (dev, bi,DECODER ); +return 0 ; +} + +/* Enable or dealloc BI-ID changes in the given level of the topology. */ +static int cxl_bi_ctrl_dport (struct cxl_dport *dport,bool enable) +{ +struct pci_dev *pdev =to_pci_dev (dport->dport_dev); +void __iomem *bi = dport->regs.bi_decoder; +struct cxl_port *port = dport->port; +u32 ctrl, value; +int rc; + +guard (device)(&port->dev); + +if (!bi) +return -EINVAL ; + + ctrl =readl (bi +CXL_BI_DECODER_CTRL_OFFSET ); + +switch (pci_pcie_type (pdev)) { +case PCI_EXP_TYPE_ROOT_PORT : +if (enable) { +/* +* There is no point of failure from here on, +* BI will be enabled on the endpoint device. +*/ + port->nr_bi++; + +if (FIELD_GET (CXL_BI_DECODER_CTRL_BI_FW , ctrl)) +return 0 ; + + value = ctrl |CXL_BI_DECODER_CTRL_BI_FW ; + value &= ~CXL_BI_DECODER_CTRL_BI_ENABLE ; + }else { +if (WARN_ON_ONCE (port->nr_bi ==0 )) +return -EINVAL ; +if (--port->nr_bi >0 ) +return 0 ; + + value = ctrl & ~(CXL_BI_DECODER_CTRL_BI_FW | +CXL_BI_DECODER_CTRL_BI_ENABLE ); + } + +writel (value, bi +CXL_BI_DECODER_CTRL_OFFSET ); +return 0 ; +case PCI_EXP_TYPE_DOWNSTREAM : +if (enable) { + value = ctrl & ~CXL_BI_DECODER_CTRL_BI_FW ; + value |=CXL_BI_DECODER_CTRL_BI_ENABLE ; + }else { +if (!FIELD_GET (CXL_BI_DECODER_CTRL_BI_ENABLE , ctrl)) +return 0 ; + value = ctrl & ~(CXL_BI_DECODER_CTRL_BI_FW | +CXL_BI_DECODER_CTRL_BI_ENABLE ); + } + +writel (value, bi +CXL_BI_DECODER_CTRL_OFFSET ); + + rc =__cxl_bi_commit (dport->dport_dev, bi); +if (rc) +return rc; + +return __cxl_bi_commit_rt (&port->dev, port->regs.bi_rt); +default : +return -EINVAL ; + } +} + +static int cxl_bi_ctrl_endpoint (struct cxl_dev_state *cxlds,bool enable) +{ +struct cxl_port *endpoint = cxlds->cxlmd->endpoint; +void __iomem *bi = endpoint->regs.bi_decoder; +u32 ctrl, val; + +if (!bi) +return -EINVAL ; + + ctrl =readl (bi +CXL_BI_DECODER_CTRL_OFFSET ); + +if (enable) { +if (FIELD_GET (CXL_BI_DECODER_CTRL_BI_ENABLE , ctrl)) { +WARN_ON_ONCE (!cxlds->bi); +return 0 ; + } + val = ctrl |CXL_BI_DECODER_CTRL_BI_ENABLE ; + }else { +if (!FIELD_GET (CXL_BI_DECODER_CTRL_BI_ENABLE , ctrl)) { +WARN_ON_ONCE (cxlds->bi); +return 0 ; + } + val = ctrl & ~CXL_BI_DECODER_CTRL_BI_ENABLE ; + } + +writel (val, bi +CXL_BI_DECODER_CTRL_OFFSET ); + cxlds->bi = enable; + +dev_dbg (cxlds->dev,"BI requests %s\n" , +str_enabled_disabled (enable)); + +return 0 ; +} + +/* +* devm teardown on cxl_mem unbind. Endpoint decoders may still be +* committed here (cxl_workqueue tears them down asynchronously), but +* memory access has been quiesced. +*/ +static void cxl_bi_dealloc (void *data) +{ +struct cxl_dev_state *cxlds = data; +struct cxl_memdev *cxlmd = cxlds->cxlmd; +struct cxl_port *endpoint = cxlmd->endpoint; +struct cxl_dport *dport, *_dport; +struct cxl_port *port; + + struct cxl_port *_port __free(put_cxl_port) = +cxl_pci_find_port (to_pci_dev (cxlds->dev), &_dport); + +if (!_port || !cxlds->bi) +return ; + +if (endpoint) { + scoped_guard(rwsem_read, &cxl_rwsem.region) +cxl_bi_ctrl_endpoint (cxlds, false); + }else { +/* +* Endpoint already removed: regions torn down and +* decoders uncommitted. Endpoint BI registers are no +* longer mapped; just clear the flag. +*/ + cxlds->bi = false; + } + + port = _port; + dport = _dport; +while (!is_cxl_root (port)) { +int rc =cxl_bi_ctrl_dport (dport, false); + +/* best effort */ +if (rc) +dev_dbg (&port->dev, +"BI dport disable failed: %d\n" , rc); + + dport = port->parent_dport; + port = dport->port; + } +} + +int cxl_bi_setup (struct cxl_dev_state *cxlds) +{ +struct pci_dev *pdev =to_pci_dev (cxlds->dev); +struct cxl_port *endpoint = cxlds->cxlmd->endpoint; +struct cxl_dport *dport, *_dport, *failed; +struct cxl_port *port; +int rc; + + struct cxl_port *_port __free(put_cxl_port) = +cxl_pci_find_port (pdev, &_dport); + +if (!_port) +return -EINVAL ; + +if (!cxl_is_bi_capable (pdev, endpoint->regs.bi_decoder)) +return 0 ; + +/* walkup the topology twice, first to check, then to enable */ + port = _port; + dport = _dport; +while (!is_cxl_root (port)) { +/* check rp, dsp */ +if (!cxl_is_bi_capable (to_pci_dev (dport->dport_dev), + dport->regs.bi_decoder)) +return -EINVAL ; + +/* check usp */ +if (dev_is_pci (port->uport_dev) && +pci_pcie_type (to_pci_dev (port->uport_dev)) == +PCI_EXP_TYPE_UPSTREAM ) +if (!cxl_is_bi_capable (to_pci_dev (port->uport_dev), + port->regs.bi_rt)) +return -EINVAL ; + + dport = port->parent_dport; + port = dport->port; + } + + port = _port; + dport = _dport; +while (!is_cxl_root (port)) { + rc =cxl_bi_ctrl_dport (dport, true); +if (rc) + goto err_rollback; + + dport = port->parent_dport; + port = dport->port; + } + +/* finally, enable BI on the device */ +cxl_bi_ctrl_endpoint (cxlds, true); + +return devm_add_action_or_reset (&cxlds->cxlmd->dev, + cxl_bi_dealloc, cxlds); + +err_rollback: +/* +* Undo all dports enabled so far, including the failed one. +*/ + failed = dport; + dport = _dport; + port = _port; +while (!is_cxl_root (port)) { +cxl_bi_ctrl_dport (dport, false); +if (dport == failed) +break ; + dport = port->parent_dport; + port = dport->port; + } +return rc; +} +EXPORT_SYMBOL_NS_GPL (cxl_bi_setup,"CXL" );
drivers/cxl/cxl.h +28 -0
@@ -167,6 +167,31 @@ static inline int ways_to_eiw(unsigned int ways, u8 *eiw)#define CXL_HEADERLOG_SIZE SZ_512#define CXL_HEADERLOG_SIZE_U32 SZ_512 / sizeof(u32) +/* CXL 4.0 8.2.4.26 CXL BI Route Table Capability Structure */ +#define CXL_BI_RT_CAPS_OFFSET 0x0 +#define CXL_BI_RT_CAPS_EXPLICIT_COMMIT_REQ BIT(0) +#define CXL_BI_RT_CTRL_OFFSET 0x4 +#define CXL_BI_RT_CTRL_BI_COMMIT BIT(0) +#define CXL_BI_RT_STATUS_OFFSET 0x8 +#define CXL_BI_RT_STATUS_BI_COMMITTED BIT(0) +#define CXL_BI_RT_STATUS_BI_ERR_NOT_COMMITTED BIT(1) +#define CXL_BI_RT_STATUS_BI_COMMIT_TM_SCALE GENMASK(11, 8) +#define CXL_BI_RT_STATUS_BI_COMMIT_TM_BASE GENMASK(15, 12) + +/* CXL 4.0 8.2.4.27 CXL BI Decoder Capability Structure */ +#define CXL_BI_DECODER_CAPS_OFFSET 0x0 +#define CXL_BI_DECODER_CAPS_HDMD_CAP BIT(0) +#define CXL_BI_DECODER_CAPS_EXPLICIT_COMMIT_REQ BIT(1) +#define CXL_BI_DECODER_CTRL_OFFSET 0x4 +#define CXL_BI_DECODER_CTRL_BI_FW BIT(0) +#define CXL_BI_DECODER_CTRL_BI_ENABLE BIT(1) +#define CXL_BI_DECODER_CTRL_BI_COMMIT BIT(2) +#define CXL_BI_DECODER_STATUS_OFFSET 0x8 +#define CXL_BI_DECODER_STATUS_BI_COMMITTED BIT(0) +#define CXL_BI_DECODER_STATUS_BI_ERR_NOT_COMMITTED BIT(1) +#define CXL_BI_DECODER_STATUS_BI_COMMIT_TM_SCALE GENMASK(11, 8) +#define CXL_BI_DECODER_STATUS_BI_COMMIT_TM_BASE GENMASK(15, 12) +/* CXL 2.0 8.2.8.1 Device Capabilities Array Register */ #define CXLDEV_CAP_ARRAY_OFFSET 0x0#define CXLDEV_CAP_ARRAY_CAP_ID 0
@@ -553,6 +578,7 @@ struct cxl_dax_region {* @cdat: Cached CDAT data * @cdat_available: Should a CDAT attribute be available in sysfs * @pci_latency: Upstream latency in picoseconds +* @nr_bi: number of BI-enabled endpoints below this port * @component_reg_phys: Physical address of component register */ struct cxl_port {
@@ -578,6 +604,7 @@ struct cxl_port { } cdat;bool cdat_available;long pci_latency; +int nr_bi;resource_size_t component_reg_phys; };
@@ -906,6 +933,7 @@ void cxl_coordinates_combine(struct access_coordinate *out, struct access_coordinate *c2);bool cxl_endpoint_decoder_reset_detected (struct cxl_port *port); +int cxl_bi_setup (struct cxl_dev_state *cxlds);struct cxl_dport *devm_cxl_add_dport_by_dev (struct cxl_port *port,struct device *dport_dev);
include/cxl/cxl.h +2 -0
@@ -168,6 +168,7 @@ struct cxl_dpa_partition {* @regs: Parsed register blocks * @cxl_dvsec: Offset to the PCIe device DVSEC * @rcd: operating in RCD mode (CXL 3.0 9.11.8 CXL Devices Attached to an RCH) +* @bi: device is BI (Back-Invalidate) enabled * @media_ready: Indicate whether the device media is usable * @dpa_res: Overall DPA resource tree for the device * @part: DPA partition array
@@ -187,6 +188,7 @@ struct cxl_dev_state {struct cxl_device_regs regs;int cxl_dvsec;bool rcd; +bool bi;bool media_ready;struct resource dpa_res;struct cxl_dpa_partition part[CXL_NR_PARTITIONS_MAX ];
Signature · 1 line
2.39.5
Re: [PATCH v3 2/5] cxl/pci: Add BI topology enable/disable
Quoted 35 lines · Davidlohr Bueso
Implement
cxl_bi_setup(), which walks the CXL port topology to enable BI flows on the device and every component in the path between the device and the root port. The walk is two-pass: first to verify each component advertises BI capability, then to write BI Enable on each dport's BI Decoder Control register and on the device. On failure during the enable pass, partially-enabled dports are rolled back.The teardown counterpart,
cxl_bi_dealloc(), runs automatically oncxl_memunbind.
cxl_bi_setup()does not touch HDM decoder configuration -- the BI bit in each decoder is programmed separately at commit time. After a successful setup the device is BI-capable but not yet using BI for decode coherence.Signed-off-by Davidlohr Bueso <[email protected]>--- drivers/cxl/core/pci.c | 319 +++++++++++++++++++++++++++++++++++++++++ drivers/cxl/cxl.h | 28 ++++ include/cxl/cxl.h | 2 + 3 files changed, 349 insertions(+)drivers/cxl/core/pci.c +1
-0@@ -7,6 +7,7 @@#include <linux/pci.h> #include <linux/pci-doe.h> #include <linux/aer.h> +#include <linux/string_choices.h> #include <cxlpci.h> #include <cxlmem.h> #include <cxl.h>
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 933
Quoted 7 lines
drivers/cxl/core/pci.c +4
-0@@ -926,3 +927,321 @@ int cxl_port_get_possible_dports(struct cxl_port *port)return ctx.count; } + +static bool cxl_is_bi_capable (struct pci_dev *pdev,void __iomem *bi) +{ +if (pci_pcie_type (pdev) !=PCI_EXP_TYPE_UPSTREAM && !bi) {
I think this is wrong, but I'm not 100% sure of the intention here. My first thought was that the && should be a ||, but I'm not sure that's right. At the end of the patch in cxl_bi_setup() you call this explicitly on PCI_EXP_TYPE_UPSTREAM ports so it should fail as-is, no? So then what is this trying to accomplish?
Quoted 75 lines
drivers/cxl/core/pci.c +75
-0+dev_dbg (&pdev->dev,"No BI Decoder registers.\n" ); +return false; + } + +return true; +} + +/* limit any insane timeouts from hw */ +#define CXL_BI_COMMIT_MAXTMO_US (5 * USEC_PER_SEC) + +static unsigned long __cxl_bi_get_timeout_us (struct device *dev, +unsigned int scale, +unsigned int base) +{ +static const unsigned long scale_tbl[] = { +1 ,10 ,100 ,1000 ,10000 ,100000 ,1000000 ,10000000 , + }; + +if (scale >=ARRAY_SIZE (scale_tbl)) { +dev_dbg (dev,"Invalid BI commit timeout scale: %u\n" , scale); +return CXL_BI_COMMIT_MAXTMO_US ; + } + +return scale_tbl[scale] * base; +} + +#define ___cxl_bi_commit (dev, bi, ctype) \ +do { \ + u32 status, ctrl; \ + unsigned int scale, base; \ + ktime_t tmo, now, start; \ + unsigned long poll_us, tmo_us; \ + \ + ctrl = readl(bi + CXL_BI_##ctype##_CTRL_OFFSET); \ + writel(ctrl & ~CXL_BI_##ctype##_CTRL_BI_COMMIT, \ + (bi) + CXL_BI_##ctype##_CTRL_OFFSET); \ + writel(ctrl | CXL_BI_##ctype##_CTRL_BI_COMMIT, \ + (bi) + CXL_BI_##ctype##_CTRL_OFFSET); \ + \ + status = readl((bi) + CXL_BI_##ctype##_STATUS_OFFSET); \ + scale = FIELD_GET(CXL_BI_##ctype##_STATUS_BI_COMMIT_TM_SCALE, \ + status); \ + base = FIELD_GET(CXL_BI_##ctype##_STATUS_BI_COMMIT_TM_BASE, \ + status); \ + \ +/* ... and poll */ \ + tmo_us = min_t(unsigned long, CXL_BI_COMMIT_MAXTMO_US, \ +__cxl_bi_get_timeout_us ((dev), scale, base)); \ + poll_us = tmo_us /10 ;/* arbitrary 10% of timeout */ \ + start = now =ktime_get (); \ + tmo =ktime_add_us (now, tmo_us); \ + while (!FIELD_GET(CXL_BI_##ctype##_STATUS_BI_COMMITTED, status) && \ + !FIELD_GET(CXL_BI_##ctype##_STATUS_BI_ERR_NOT_COMMITTED, \ + status)) { \ + if (ktime_after(now, tmo)) { \ + dev_dbg((dev), "BI-ID commit timed out (%luus)\n", \ + tmo_us); \ + return -ETIMEDOUT; \ + } \ + \ + fsleep(poll_us); \ + now = ktime_get(); \ + status = readl((bi) + CXL_BI_##ctype##_STATUS_OFFSET); \ + } \ + \ + if (FIELD_GET(CXL_BI_##ctype##_STATUS_BI_ERR_NOT_COMMITTED, status)) \ + return -EIO; \ + \ + dev_dbg((dev), "BI-ID commit wait took %lluus\n", \ + ktime_to_us(ktime_sub(now, start))); \ +} while (0) + +static int __cxl_bi_commit_rt (struct device *dev,void __iomem *bi) +{ +if (!bi)
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 1018
Quoted 10 lines
drivers/cxl/core/pci.c +10
-0+return 0 ; + +if (FIELD_GET (CXL_BI_RT_CAPS_EXPLICIT_COMMIT_REQ , +readl (bi +CXL_BI_RT_CAPS_OFFSET ))) +___cxl_bi_commit (dev, bi,RT ); + +return 0 ; +} + +static int __cxl_bi_commit (struct device *dev,void __iomem *bi)
This needs a different name, probably __cxl_bi_commit_decoder(). It obviously compiles since you've tested it, but it'll definitely lead to confusion for readers later on.
Quoted 13 lines
drivers/cxl/core/pci.c +13
-0+{ +if (!bi) +return -EINVAL ; + +if (!FIELD_GET (CXL_BI_DECODER_CAPS_EXPLICIT_COMMIT_REQ , +readl (bi +CXL_BI_DECODER_CAPS_OFFSET ))) +return 0 ; + +___cxl_bi_commit (dev, bi,DECODER ); +return 0 ; +} + +/* Enable or dealloc BI-ID changes in the given level of the topology. */
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 1041
Quoted 10 lines
drivers/cxl/core/pci.c +10
-0+static int cxl_bi_ctrl_dport (struct cxl_dport *dport,bool enable) +{ +struct pci_dev *pdev =to_pci_dev (dport->dport_dev); +void __iomem *bi = dport->regs.bi_decoder; +struct cxl_port *port = dport->port; +u32 ctrl, value; +int rc; + +guard (device)(&port->dev); +
Nit: I would considers this extra space, but that's just my opinion.
Quoted 59 lines
drivers/cxl/core/pci.c +59
-0+if (!bi) +return -EINVAL ; + + ctrl =readl (bi +CXL_BI_DECODER_CTRL_OFFSET ); + +switch (pci_pcie_type (pdev)) { +case PCI_EXP_TYPE_ROOT_PORT : +if (enable) { +/* +* There is no point of failure from here on, +* BI will be enabled on the endpoint device. +*/ + port->nr_bi++; + +if (FIELD_GET (CXL_BI_DECODER_CTRL_BI_FW , ctrl)) +return 0 ; + + value = ctrl |CXL_BI_DECODER_CTRL_BI_FW ; + value &= ~CXL_BI_DECODER_CTRL_BI_ENABLE ; + }else { +if (WARN_ON_ONCE (port->nr_bi ==0 )) +return -EINVAL ; +if (--port->nr_bi >0 ) +return 0 ; + + value = ctrl & ~(CXL_BI_DECODER_CTRL_BI_FW | +CXL_BI_DECODER_CTRL_BI_ENABLE ); + } + +writel (value, bi +CXL_BI_DECODER_CTRL_OFFSET ); +return 0 ; +case PCI_EXP_TYPE_DOWNSTREAM : +if (enable) { + value = ctrl & ~CXL_BI_DECODER_CTRL_BI_FW ; + value |=CXL_BI_DECODER_CTRL_BI_ENABLE ; + }else { +if (!FIELD_GET (CXL_BI_DECODER_CTRL_BI_ENABLE , ctrl)) +return 0 ; + value = ctrl & ~(CXL_BI_DECODER_CTRL_BI_FW | +CXL_BI_DECODER_CTRL_BI_ENABLE ); + } + +writel (value, bi +CXL_BI_DECODER_CTRL_OFFSET ); + + rc =__cxl_bi_commit (dport->dport_dev, bi); +if (rc) +return rc; + +return __cxl_bi_commit_rt (&port->dev, port->regs.bi_rt); +default : +return -EINVAL ; + } +} + +static int cxl_bi_ctrl_endpoint (struct cxl_dev_state *cxlds,bool enable) +{ +struct cxl_port *endpoint = cxlds->cxlmd->endpoint; +void __iomem *bi = endpoint->regs.bi_decoder; +u32 ctrl, val;
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 1110
Quoted 10 lines
drivers/cxl/core/pci.c +10
-0+ +if (!bi) +return -EINVAL ; + + ctrl =readl (bi +CXL_BI_DECODER_CTRL_OFFSET ); + +if (enable) { +if (FIELD_GET (CXL_BI_DECODER_CTRL_BI_ENABLE , ctrl)) { +WARN_ON_ONCE (!cxlds->bi); +return 0 ;
I think it's appropriate to return an error if the WARN_ON_ONCE() is true. I think this only happens when the driver is torn down but didn't get to clear the BI registers, or if BIOS enabled BI on boot. I would argue that both of these scenarios warrant an error, unless you want to do a silent clean up of the driver state in the first case. I'm not sure how you would figure out which is which though...
Quoted 25 lines
drivers/cxl/core/pci.c +25
-0+ } + val = ctrl |CXL_BI_DECODER_CTRL_BI_ENABLE ; + }else { +if (!FIELD_GET (CXL_BI_DECODER_CTRL_BI_ENABLE , ctrl)) { +WARN_ON_ONCE (cxlds->bi); +return 0 ; + } + val = ctrl & ~CXL_BI_DECODER_CTRL_BI_ENABLE ; + } + +writel (val, bi +CXL_BI_DECODER_CTRL_OFFSET ); + cxlds->bi = enable; + +dev_dbg (cxlds->dev,"BI requests %s\n" , +str_enabled_disabled (enable)); + +return 0 ; +} + +/* +* devm teardown on cxl_mem unbind. Endpoint decoders may still be +* committed here (cxl_workqueue tears them down asynchronously), but +* memory access has been quiesced. +*/ +static void cxl_bi_dealloc (void *data)
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 1145
Quoted 10 lines
drivers/cxl/core/pci.c +10
-0+{ +struct cxl_dev_state *cxlds = data; +struct cxl_memdev *cxlmd = cxlds->cxlmd; +struct cxl_port *endpoint = cxlmd->endpoint; +struct cxl_dport *dport, *_dport; +struct cxl_port *port; + + struct cxl_port *_port __free(put_cxl_port) = +cxl_pci_find_port (to_pci_dev (cxlds->dev), &_dport); +
Now this space is extra :)
Quoted 7 lines
drivers/cxl/core/pci.c +7
-0+if (!_port || !cxlds->bi) +return ; + +if (endpoint) { + scoped_guard(rwsem_read, &cxl_rwsem.region) +cxl_bi_ctrl_endpoint (cxlds, false); + }else {
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 1162
Quoted 10 lines
drivers/cxl/core/pci.c +10
-0+/* +* Endpoint already removed: regions torn down and +* decoders uncommitted. Endpoint BI registers are no +* longer mapped; just clear the flag. +*/ + cxlds->bi = false; + } + + port = _port; + dport = _dport;
Nit: I would rename port and dport to port_iter and dport_iter since it's what they're used for.
Quoted 36 lines
drivers/cxl/core/pci.c +36
-0+while (!is_cxl_root (port)) { +int rc =cxl_bi_ctrl_dport (dport, false); + +/* best effort */ +if (rc) +dev_dbg (&port->dev, +"BI dport disable failed: %d\n" , rc); + + dport = port->parent_dport; + port = dport->port; + } +} + +int cxl_bi_setup(struct cxl_dev_state *cxlds) +{ + struct pci_dev *pdev = to_pci_dev(cxlds->dev); + struct cxl_port *endpoint = cxlds->cxlmd->endpoint; + struct cxl_dport *dport, *_dport, *failed; + struct cxl_port *port; + int rc; + + struct cxl_port *_port __free(put_cxl_port) = + cxl_pci_find_port(pdev, &_dport); + + if (!_port) + return -EINVAL; + + if (!cxl_is_bi_capable(pdev, endpoint->regs.bi_decoder)) + return 0; + + /* walkup the topology twice, first to check, then to enable */ + port = _port; + dport = _dport; + while (!is_cxl_root(port)) { + /* check rp, dsp */ + if (!cxl_is_bi_capable(to_pci_dev(dport->dport_dev),
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 1208
Quoted 10 lines
drivers/cxl/core/pci.c +10
-0+ dport->regs.bi_decoder)) +return -EINVAL ; + +/* check usp */ +if (dev_is_pci (port->uport_dev) && +pci_pcie_type (to_pci_dev (port->uport_dev)) == +PCI_EXP_TYPE_UPSTREAM ) +if (!cxl_is_bi_capable (to_pci_dev (port->uport_dev), + port->regs.bi_rt)) +return -EINVAL ;
Issue I pointed out above is here. All ports that are PCI_EXP_TYPE_UPSTREAM will fail the cxl_is_bi_capable() check.
Quoted 119 lines
drivers/cxl/core/pci.c +39
-0+ + dport = port->parent_dport; + port = dport->port; + } + + port = _port; + dport = _dport; +while (!is_cxl_root (port)) { + rc =cxl_bi_ctrl_dport (dport, true); +if (rc) + goto err_rollback; + + dport = port->parent_dport; + port = dport->port; + } + +/* finally, enable BI on the device */ +cxl_bi_ctrl_endpoint (cxlds, true); + +return devm_add_action_or_reset (&cxlds->cxlmd->dev, + cxl_bi_dealloc, cxlds); + +err_rollback: +/* +* Undo all dports enabled so far, including the failed one. +*/ + failed = dport; + dport = _dport; + port = _port; +while (!is_cxl_root (port)) { +cxl_bi_ctrl_dport (dport, false); +if (dport == failed) +break ; + dport = port->parent_dport; + port = dport->port; + } +return rc; +} +EXPORT_SYMBOL_NS_GPL (cxl_bi_setup,"CXL" );drivers/cxl/cxl.h +28
-0@@ -167,6 +167,31 @@ static inline int ways_to_eiw(unsigned int ways, u8 *eiw)#define CXL_HEADERLOG_SIZE SZ_512#define CXL_HEADERLOG_SIZE_U32 SZ_512 / sizeof(u32) +/* CXL 4.0 8.2.4.26 CXL BI Route Table Capability Structure */ +#define CXL_BI_RT_CAPS_OFFSET 0x0 +#define CXL_BI_RT_CAPS_EXPLICIT_COMMIT_REQ BIT(0) +#define CXL_BI_RT_CTRL_OFFSET 0x4 +#define CXL_BI_RT_CTRL_BI_COMMIT BIT(0) +#define CXL_BI_RT_STATUS_OFFSET 0x8 +#define CXL_BI_RT_STATUS_BI_COMMITTED BIT(0) +#define CXL_BI_RT_STATUS_BI_ERR_NOT_COMMITTED BIT(1) +#define CXL_BI_RT_STATUS_BI_COMMIT_TM_SCALE GENMASK(11, 8) +#define CXL_BI_RT_STATUS_BI_COMMIT_TM_BASE GENMASK(15, 12) + +/* CXL 4.0 8.2.4.27 CXL BI Decoder Capability Structure */ +#define CXL_BI_DECODER_CAPS_OFFSET 0x0 +#define CXL_BI_DECODER_CAPS_HDMD_CAP BIT(0) +#define CXL_BI_DECODER_CAPS_EXPLICIT_COMMIT_REQ BIT(1) +#define CXL_BI_DECODER_CTRL_OFFSET 0x4 +#define CXL_BI_DECODER_CTRL_BI_FW BIT(0) +#define CXL_BI_DECODER_CTRL_BI_ENABLE BIT(1) +#define CXL_BI_DECODER_CTRL_BI_COMMIT BIT(2) +#define CXL_BI_DECODER_STATUS_OFFSET 0x8 +#define CXL_BI_DECODER_STATUS_BI_COMMITTED BIT(0) +#define CXL_BI_DECODER_STATUS_BI_ERR_NOT_COMMITTED BIT(1) +#define CXL_BI_DECODER_STATUS_BI_COMMIT_TM_SCALE GENMASK(11, 8) +#define CXL_BI_DECODER_STATUS_BI_COMMIT_TM_BASE GENMASK(15, 12) +/* CXL 2.0 8.2.8.1 Device Capabilities Array Register */ #define CXLDEV_CAP_ARRAY_OFFSET 0x0#define CXLDEV_CAP_ARRAY_CAP_ID 0@@ -553,6 +578,7 @@ struct cxl_dax_region {* @cdat: Cached CDAT data * @cdat_available: Should a CDAT attribute be available in sysfs * @pci_latency: Upstream latency in picoseconds +* @nr_bi: number of BI-enabled endpoints below this port * @component_reg_phys: Physical address of component register */ struct cxl_port {@@ -578,6 +604,7 @@ struct cxl_port { } cdat;bool cdat_available;long pci_latency; +int nr_bi;resource_size_t component_reg_phys; };@@ -906,6 +933,7 @@ void cxl_coordinates_combine(struct access_coordinate *out, struct access_coordinate *c2);bool cxl_endpoint_decoder_reset_detected (struct cxl_port *port); +int cxl_bi_setup (struct cxl_dev_state *cxlds);struct cxl_dport *devm_cxl_add_dport_by_dev (struct cxl_port *port,struct device *dport_dev);include/cxl/cxl.h +2
-0@@ -168,6 +168,7 @@ struct cxl_dpa_partition {* @regs: Parsed register blocks * @cxl_dvsec: Offset to the PCIe device DVSEC * @rcd: operating in RCD mode (CXL 3.0 9.11.8 CXL Devices Attached to an RCH) +* @bi: device is BI (Back-Invalidate) enabled * @media_ready: Indicate whether the device media is usable * @dpa_res: Overall DPA resource tree for the device * @part: DPA partition array @@ -187,6 +188,7 @@ struct cxl_dev_state {struct cxl_device_regs regs;int cxl_dvsec;bool rcd; +bool bi;bool media_ready;struct resource dpa_res;struct cxl_dpa_partition part[CXL_NR_PARTITIONS_MAX ];
Dave Jiang
maintainerRe: [PATCH v3 2/5] cxl/pci: Add BI topology enable/disable
In reply to Davidlohr BuesoQuoted 39 lines · Davidlohr Bueso
Implement
cxl_bi_setup(), which walks the CXL port topology to enable BI flows on the device and every component in the path between the device and the root port. The walk is two-pass: first to verify each component advertises BI capability, then to write BI Enable on each dport's BI Decoder Control register and on the device. On failure during the enable pass, partially-enabled dports are rolled back.The teardown counterpart,
cxl_bi_dealloc(), runs automatically oncxl_memunbind.
cxl_bi_setup()does not touch HDM decoder configuration -- the BI bit in each decoder is programmed separately at commit time. After a successful setup the device is BI-capable but not yet using BI for decode coherence.Signed-off-by Davidlohr Bueso <[email protected]>--- drivers/cxl/core/pci.c | 319 +++++++++++++++++++++++++++++++++++++++++ drivers/cxl/cxl.h | 28 ++++ include/cxl/cxl.h | 2 + 3 files changed, 349 insertions(+)drivers/cxl/core/pci.c +2
-0@@ -7,6 +7,7 @@#include <linux/pci.h> #include <linux/pci-doe.h> #include <linux/aer.h> +#include <linux/string_choices.h> #include <cxlpci.h> #include <cxlmem.h> #include <cxl.h> @@ -926,3 +927,321 @@ int cxl_port_get_possible_dports(struct cxl_port *port)return ctx.count; } +
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 940
Quoted 10 lines
drivers/cxl/core/pci.c +10
-0+static bool cxl_is_bi_capable (struct pci_dev *pdev,void __iomem *bi) +{ +if (pci_pcie_type (pdev) !=PCI_EXP_TYPE_UPSTREAM && !bi) { +dev_dbg (&pdev->dev,"No BI Decoder registers.\n" ); +return false; + } + +return true; +} +
Maybe add a comment for this helper that that BI_RT is only for upstream ports?
Quoted 32 lines
drivers/cxl/core/pci.c +32
-0+/* limit any insane timeouts from hw */ +#define CXL_BI_COMMIT_MAXTMO_US (5 * USEC_PER_SEC) + +static unsigned long __cxl_bi_get_timeout_us (struct device *dev, +unsigned int scale, +unsigned int base) +{ +static const unsigned long scale_tbl[] = { +1 ,10 ,100 ,1000 ,10000 ,100000 ,1000000 ,10000000 , + }; + +if (scale >=ARRAY_SIZE (scale_tbl)) { +dev_dbg (dev,"Invalid BI commit timeout scale: %u\n" , scale); +return CXL_BI_COMMIT_MAXTMO_US ; + } + +return scale_tbl[scale] * base; +} + +#define ___cxl_bi_commit (dev, bi, ctype) \ +do { \ + u32 status, ctrl; \ + unsigned int scale, base; \ + ktime_t tmo, now, start; \ + unsigned long poll_us, tmo_us; \ + \ + ctrl = readl(bi + CXL_BI_##ctype##_CTRL_OFFSET); \ + writel(ctrl & ~CXL_BI_##ctype##_CTRL_BI_COMMIT, \ + (bi) + CXL_BI_##ctype##_CTRL_OFFSET); \ + writel(ctrl | CXL_BI_##ctype##_CTRL_BI_COMMIT, \ + (bi) + CXL_BI_##ctype##_CTRL_OFFSET); \ + \
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 982
Quoted 10 lines
drivers/cxl/core/pci.c +10
-0+ status = readl((bi) + CXL_BI_##ctype##_STATUS_OFFSET); \ + scale = FIELD_GET(CXL_BI_##ctype##_STATUS_BI_COMMIT_TM_SCALE, \ + status); \ + base = FIELD_GET(CXL_BI_##ctype##_STATUS_BI_COMMIT_TM_BASE, \ + status); \ + \ + /* ... and poll */ \ + tmo_us = min_t(unsigned long, CXL_BI_COMMIT_MAXTMO_US, \ +__cxl_bi_get_timeout_us ((dev), scale, base)); \ + poll_us = tmo_us /10 ;/* arbitrary 10% of timeout */ \
Any chance that poll_us end up being 0 because tmo_us is less than 10 and that creates a tight spin polling loop? Maybe poll_us = max(poll_us, 1) to deal with a 0 value? Or some sane default min poll value.
Quoted 12 lines
drivers/cxl/core/pci.c +12
-0+ start = now =ktime_get (); \ + tmo =ktime_add_us (now, tmo_us); \ + while (!FIELD_GET(CXL_BI_##ctype##_STATUS_BI_COMMITTED, status) && \ + !FIELD_GET(CXL_BI_##ctype##_STATUS_BI_ERR_NOT_COMMITTED, \ + status)) { \ + if (ktime_after(now, tmo)) { \ + dev_dbg((dev), "BI-ID commit timed out (%luus)\n", \ + tmo_us); \ + return -ETIMEDOUT; \ + } \ + \ + fsleep(poll_us); \
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 1004
Quoted 10 lines
drivers/cxl/core/pci.c +10
-0+ now =ktime_get (); \ + status = readl((bi) + CXL_BI_##ctype##_STATUS_OFFSET); \ + } \ + \ + if (FIELD_GET(CXL_BI_##ctype##_STATUS_BI_ERR_NOT_COMMITTED, status)) \ + return -EIO; \ + \ + dev_dbg((dev), "BI-ID commit wait took %lluus\n", \ + ktime_to_us(ktime_sub(now, start))); \ +} while (0)
There has to be a cleaner way to do this instead of this huge macro? I think Ben mentioned this in v1. And I don't particularly like it either.
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 1006
Quoted 2 lines
drivers/cxl/core/pci.c +2
-0+ +static int __cxl_bi_commit_rt (struct device *dev,void __iomem *bi)
I do wonder if 'struct cxl_component_regs *' would be better passed in than allowing arbitrary iomem addr.
Quoted 16 lines
drivers/cxl/core/pci.c +16
-0+{ +if (!bi) +return 0 ; + +if (FIELD_GET (CXL_BI_RT_CAPS_EXPLICIT_COMMIT_REQ , +readl (bi +CXL_BI_RT_CAPS_OFFSET ))) +___cxl_bi_commit (dev, bi,RT ); + +return 0 ; +} + +static int __cxl_bi_commit (struct device *dev,void __iomem *bi) +{ +if (!bi) +return -EINVAL ; +
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 1032
Quoted 10 lines
drivers/cxl/core/pci.c +10
-0+if (!FIELD_GET (CXL_BI_DECODER_CAPS_EXPLICIT_COMMIT_REQ , +readl (bi +CXL_BI_DECODER_CAPS_OFFSET ))) +return 0 ; + +___cxl_bi_commit (dev, bi,DECODER ); +return 0 ; +} + +/* Enable or dealloc BI-ID changes in the given level of the topology. */ +static int cxl_bi_ctrl_dport (struct cxl_dport *dport,bool enable)
I don't particularly care for the bool for toggle. Please either use an enum for readability or use wrapper functions i.e. cxl_bi_ctrl_dport_enable() / _disable().
Quoted 184 lines
drivers/cxl/core/pci.c +184
-0+{ +struct pci_dev *pdev =to_pci_dev (dport->dport_dev); +void __iomem *bi = dport->regs.bi_decoder; +struct cxl_port *port = dport->port; +u32 ctrl, value; +int rc; + +guard (device)(&port->dev); + +if (!bi) +return -EINVAL ; + + ctrl =readl (bi +CXL_BI_DECODER_CTRL_OFFSET ); + +switch (pci_pcie_type (pdev)) { +case PCI_EXP_TYPE_ROOT_PORT : +if (enable) { +/* +* There is no point of failure from here on, +* BI will be enabled on the endpoint device. +*/ + port->nr_bi++; + +if (FIELD_GET (CXL_BI_DECODER_CTRL_BI_FW , ctrl)) +return 0 ; + + value = ctrl |CXL_BI_DECODER_CTRL_BI_FW ; + value &= ~CXL_BI_DECODER_CTRL_BI_ENABLE ; + }else { +if (WARN_ON_ONCE (port->nr_bi ==0 )) +return -EINVAL ; +if (--port->nr_bi >0 ) +return 0 ; + + value = ctrl & ~(CXL_BI_DECODER_CTRL_BI_FW | +CXL_BI_DECODER_CTRL_BI_ENABLE ); + } + +writel (value, bi +CXL_BI_DECODER_CTRL_OFFSET ); +return 0 ; +case PCI_EXP_TYPE_DOWNSTREAM : +if (enable) { + value = ctrl & ~CXL_BI_DECODER_CTRL_BI_FW ; + value |=CXL_BI_DECODER_CTRL_BI_ENABLE ; + }else { +if (!FIELD_GET (CXL_BI_DECODER_CTRL_BI_ENABLE , ctrl)) +return 0 ; + value = ctrl & ~(CXL_BI_DECODER_CTRL_BI_FW | +CXL_BI_DECODER_CTRL_BI_ENABLE ); + } + +writel (value, bi +CXL_BI_DECODER_CTRL_OFFSET ); + + rc =__cxl_bi_commit (dport->dport_dev, bi); +if (rc) +return rc; + +return __cxl_bi_commit_rt (&port->dev, port->regs.bi_rt); +default : +return -EINVAL ; + } +} + +static int cxl_bi_ctrl_endpoint (struct cxl_dev_state *cxlds,bool enable) +{ +struct cxl_port *endpoint = cxlds->cxlmd->endpoint; +void __iomem *bi = endpoint->regs.bi_decoder; +u32 ctrl, val; + +if (!bi) +return -EINVAL ; + + ctrl =readl (bi +CXL_BI_DECODER_CTRL_OFFSET ); + +if (enable) { +if (FIELD_GET (CXL_BI_DECODER_CTRL_BI_ENABLE , ctrl)) { +WARN_ON_ONCE (!cxlds->bi); +return 0 ; + } + val = ctrl |CXL_BI_DECODER_CTRL_BI_ENABLE ; + }else { +if (!FIELD_GET (CXL_BI_DECODER_CTRL_BI_ENABLE , ctrl)) { +WARN_ON_ONCE (cxlds->bi); +return 0 ; + } + val = ctrl & ~CXL_BI_DECODER_CTRL_BI_ENABLE ; + } + +writel (val, bi +CXL_BI_DECODER_CTRL_OFFSET ); + cxlds->bi = enable; + +dev_dbg (cxlds->dev,"BI requests %s\n" , +str_enabled_disabled (enable)); + +return 0 ; +} + +/* +* devm teardown on cxl_mem unbind. Endpoint decoders may still be +* committed here (cxl_workqueue tears them down asynchronously), but +* memory access has been quiesced. +*/ +static void cxl_bi_dealloc (void *data) +{ +struct cxl_dev_state *cxlds = data; +struct cxl_memdev *cxlmd = cxlds->cxlmd; +struct cxl_port *endpoint = cxlmd->endpoint; +struct cxl_dport *dport, *_dport; +struct cxl_port *port; + + struct cxl_port *_port __free(put_cxl_port) = +cxl_pci_find_port (to_pci_dev (cxlds->dev), &_dport); + +if (!_port || !cxlds->bi) +return ; + +if (endpoint) { + scoped_guard(rwsem_read, &cxl_rwsem.region) +cxl_bi_ctrl_endpoint (cxlds, false); + }else { +/* +* Endpoint already removed: regions torn down and +* decoders uncommitted. Endpoint BI registers are no +* longer mapped; just clear the flag. +*/ + cxlds->bi = false; + } + + port = _port; + dport = _dport; +while (!is_cxl_root (port)) { +int rc =cxl_bi_ctrl_dport (dport, false); + +/* best effort */ +if (rc) +dev_dbg (&port->dev, +"BI dport disable failed: %d\n" , rc); + + dport = port->parent_dport; + port = dport->port; + } +} + +int cxl_bi_setup (struct cxl_dev_state *cxlds) +{ +struct pci_dev *pdev =to_pci_dev (cxlds->dev); +struct cxl_port *endpoint = cxlds->cxlmd->endpoint; +struct cxl_dport *dport, *_dport, *failed; +struct cxl_port *port; +int rc; + + struct cxl_port *_port __free(put_cxl_port) = +cxl_pci_find_port (pdev, &_dport); + +if (!_port) +return -EINVAL ; + +if (!cxl_is_bi_capable (pdev, endpoint->regs.bi_decoder)) +return 0 ; + +/* walkup the topology twice, first to check, then to enable */ + port = _port; + dport = _dport; +while (!is_cxl_root (port)) { +/* check rp, dsp */ +if (!cxl_is_bi_capable (to_pci_dev (dport->dport_dev), + dport->regs.bi_decoder)) +return -EINVAL ; + +/* check usp */ +if (dev_is_pci (port->uport_dev) && +pci_pcie_type (to_pci_dev (port->uport_dev)) == +PCI_EXP_TYPE_UPSTREAM ) +if (!cxl_is_bi_capable (to_pci_dev (port->uport_dev), + port->regs.bi_rt)) +return -EINVAL ; + + dport = port->parent_dport; + port = dport->port; + } + + port = _port; + dport = _dport; +while (!is_cxl_root (port)) {
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 1226
Quoted 10 lines
drivers/cxl/core/pci.c +10
-0+ rc =cxl_bi_ctrl_dport (dport, true); +if (rc) + goto err_rollback; + + dport = port->parent_dport; + port = dport->port; + } + +/* finally, enable BI on the device */ +cxl_bi_ctrl_endpoint (cxlds, true);
ignoring returned error. if endpoint fail to enable, do you need to rollback and skip the dealloc registration?
DJ
Quoted 100 lines
drivers/cxl/core/pci.c +21
-0+ +return devm_add_action_or_reset (&cxlds->cxlmd->dev, + cxl_bi_dealloc, cxlds); + +err_rollback: +/* +* Undo all dports enabled so far, including the failed one. +*/ + failed = dport; + dport = _dport; + port = _port; +while (!is_cxl_root (port)) { +cxl_bi_ctrl_dport (dport, false); +if (dport == failed) +break ; + dport = port->parent_dport; + port = dport->port; + } +return rc; +} +EXPORT_SYMBOL_NS_GPL (cxl_bi_setup,"CXL" );drivers/cxl/cxl.h +28
-0@@ -167,6 +167,31 @@ static inline int ways_to_eiw(unsigned int ways, u8 *eiw)#define CXL_HEADERLOG_SIZE SZ_512#define CXL_HEADERLOG_SIZE_U32 SZ_512 / sizeof(u32) +/* CXL 4.0 8.2.4.26 CXL BI Route Table Capability Structure */ +#define CXL_BI_RT_CAPS_OFFSET 0x0 +#define CXL_BI_RT_CAPS_EXPLICIT_COMMIT_REQ BIT(0) +#define CXL_BI_RT_CTRL_OFFSET 0x4 +#define CXL_BI_RT_CTRL_BI_COMMIT BIT(0) +#define CXL_BI_RT_STATUS_OFFSET 0x8 +#define CXL_BI_RT_STATUS_BI_COMMITTED BIT(0) +#define CXL_BI_RT_STATUS_BI_ERR_NOT_COMMITTED BIT(1) +#define CXL_BI_RT_STATUS_BI_COMMIT_TM_SCALE GENMASK(11, 8) +#define CXL_BI_RT_STATUS_BI_COMMIT_TM_BASE GENMASK(15, 12) + +/* CXL 4.0 8.2.4.27 CXL BI Decoder Capability Structure */ +#define CXL_BI_DECODER_CAPS_OFFSET 0x0 +#define CXL_BI_DECODER_CAPS_HDMD_CAP BIT(0) +#define CXL_BI_DECODER_CAPS_EXPLICIT_COMMIT_REQ BIT(1) +#define CXL_BI_DECODER_CTRL_OFFSET 0x4 +#define CXL_BI_DECODER_CTRL_BI_FW BIT(0) +#define CXL_BI_DECODER_CTRL_BI_ENABLE BIT(1) +#define CXL_BI_DECODER_CTRL_BI_COMMIT BIT(2) +#define CXL_BI_DECODER_STATUS_OFFSET 0x8 +#define CXL_BI_DECODER_STATUS_BI_COMMITTED BIT(0) +#define CXL_BI_DECODER_STATUS_BI_ERR_NOT_COMMITTED BIT(1) +#define CXL_BI_DECODER_STATUS_BI_COMMIT_TM_SCALE GENMASK(11, 8) +#define CXL_BI_DECODER_STATUS_BI_COMMIT_TM_BASE GENMASK(15, 12) +/* CXL 2.0 8.2.8.1 Device Capabilities Array Register */ #define CXLDEV_CAP_ARRAY_OFFSET 0x0#define CXLDEV_CAP_ARRAY_CAP_ID 0@@ -553,6 +578,7 @@ struct cxl_dax_region {* @cdat: Cached CDAT data * @cdat_available: Should a CDAT attribute be available in sysfs * @pci_latency: Upstream latency in picoseconds +* @nr_bi: number of BI-enabled endpoints below this port * @component_reg_phys: Physical address of component register */ struct cxl_port {@@ -578,6 +604,7 @@ struct cxl_port { } cdat;bool cdat_available;long pci_latency; +int nr_bi; resource_size_t component_reg_phys;> };@@ -906,6 +933,7 @@ void cxl_coordinates_combine(struct access_coordinate *out, struct access_coordinate *c2);bool cxl_endpoint_decoder_reset_detected (struct cxl_port *port); +int cxl_bi_setup (struct cxl_dev_state *cxlds);struct cxl_dport *devm_cxl_add_dport_by_dev (struct cxl_port *port,struct device *dport_dev);include/cxl/cxl.h +2
-0@@ -168,6 +168,7 @@ struct cxl_dpa_partition {* @regs: Parsed register blocks * @cxl_dvsec: Offset to the PCIe device DVSEC * @rcd: operating in RCD mode (CXL 3.0 9.11.8 CXL Devices Attached to an RCH) +* @bi: device is BI (Back-Invalidate) enabled * @media_ready: Indicate whether the device media is usable * @dpa_res: Overall DPA resource tree for the device * @part: DPA partition array @@ -187,6 +188,7 @@ struct cxl_dev_state {struct cxl_device_regs regs;int cxl_dvsec;bool rcd; +bool bi;bool media_ready;struct resource dpa_res;struct cxl_dpa_partition part[CXL_NR_PARTITIONS_MAX ];
Davidlohr Bueso
maintainerRe: [PATCH v3 2/5] cxl/pci: Add BI topology enable/disable
Quoted 15 lines · Dave Jiang
Quoted 14 lines · Davidlohr Bueso
drivers/cxl/core/pci.c +1
-0@@ -7,6 +7,7 @@#include <linux/pci.h> #include <linux/pci-doe.h> #include <linux/aer.h> +#include <linux/string_choices.h> #include <cxlpci.h> #include <cxlmem.h> #include <cxl.h> @@ -926,3 +927,321 @@ int cxl_port_get_possible_dports(struct cxl_port *port)
Quoted 15 lines
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 940
Quoted 13 lines
drivers/cxl/core/pci.c +11
-0return ctx.count; } + +static bool cxl_is_bi_capable (struct pci_dev *pdev,void __iomem *bi) +{ +if (pci_pcie_type (pdev) !=PCI_EXP_TYPE_UPSTREAM && !bi) { +dev_dbg (&pdev->dev,"No BI Decoder registers.\n" ); +return false; + } + +return true; +} +Maybe add a comment for this helper that that
BI_RTis only for upstream ports?
Ok, and I have also added a comment to cxl_is_bi_capable().
Quoted 38 lines
Quoted 38 lines
drivers/cxl/core/pci.c +37
-0+/* limit any insane timeouts from hw */ +#define CXL_BI_COMMIT_MAXTMO_US (5 * USEC_PER_SEC) + +static unsigned long __cxl_bi_get_timeout_us (struct device *dev, +unsigned int scale, +unsigned int base) +{ +static const unsigned long scale_tbl[] = { +1 ,10 ,100 ,1000 ,10000 ,100000 ,1000000 ,10000000 , + }; + +if (scale >=ARRAY_SIZE (scale_tbl)) { +dev_dbg (dev,"Invalid BI commit timeout scale: %u\n" , scale); +return CXL_BI_COMMIT_MAXTMO_US ; + } + +return scale_tbl[scale] * base; +} + +#define ___cxl_bi_commit (dev, bi, ctype) \ +do { \ + u32 status, ctrl; \ + unsigned int scale, base; \ + ktime_t tmo, now, start; \ + unsigned long poll_us, tmo_us; \ + \ + ctrl = readl(bi + CXL_BI_##ctype##_CTRL_OFFSET); \ + writel(ctrl & ~CXL_BI_##ctype##_CTRL_BI_COMMIT, \ + (bi) + CXL_BI_##ctype##_CTRL_OFFSET); \ + writel(ctrl | CXL_BI_##ctype##_CTRL_BI_COMMIT, \ + (bi) + CXL_BI_##ctype##_CTRL_OFFSET); \ + \ + status = readl((bi) + CXL_BI_##ctype##_STATUS_OFFSET); \ + scale = FIELD_GET(CXL_BI_##ctype##_STATUS_BI_COMMIT_TM_SCALE, \ + status); \ + base = FIELD_GET(CXL_BI_##ctype##_STATUS_BI_COMMIT_TM_BASE, \ + status); \
Quoted 8 lines
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 982
Quoted 5 lines
drivers/cxl/core/pci.c +5
-0+ \ +/* ... and poll */ \ + tmo_us = min_t(unsigned long, CXL_BI_COMMIT_MAXTMO_US, \ +__cxl_bi_get_timeout_us ((dev), scale, base)); \ + poll_us = tmo_us /10 ;/* arbitrary 10% of timeout */ \Any chance that
poll_usend up being 0 becausetmo_usis less than 10 and that creates a tight spin polling loop? Maybepoll_us=max(poll_us, 1)to deal with a 0 value? Or some sane default min poll value.
Right, went with max_t().
Quoted 17 lines
Quoted 17 lines
drivers/cxl/core/pci.c +16
-0+ start = now =ktime_get (); \ + tmo =ktime_add_us (now, tmo_us); \ + while (!FIELD_GET(CXL_BI_##ctype##_STATUS_BI_COMMITTED, status) && \ + !FIELD_GET(CXL_BI_##ctype##_STATUS_BI_ERR_NOT_COMMITTED, \ + status)) { \ + if (ktime_after(now, tmo)) { \ + dev_dbg((dev), "BI-ID commit timed out (%luus)\n", \ + tmo_us); \ + return -ETIMEDOUT; \ + } \ + \ + fsleep(poll_us); \ + now = ktime_get(); \ + status = readl((bi) + CXL_BI_##ctype##_STATUS_OFFSET); \ + } \ + \
Quoted 8 lines
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 1004
Quoted 6 lines
drivers/cxl/core/pci.c +6
-0+ if (FIELD_GET(CXL_BI_##ctype##_STATUS_BI_ERR_NOT_COMMITTED, status)) \ + return -EIO; \ + \ + dev_dbg((dev), "BI-ID commit wait took %lluus\n", \ + ktime_to_us(ktime_sub(now, start))); \ +} while (0)There has to be a cleaner way to do this instead of this huge macro? I think Ben mentioned this in v1. And I don't particularly like it either.
So I think Ben was more divided, but yeah I was expecting your pushback here. And while I still like the macro approach, I do agree that the above is not ideal. What I should have done is something like:
+#define DEFINE_CXL_BI_COMMIT(type, TYPE, null_rc) \
+static int __cxl_bi_commit_##type(struct device *dev, void __iomem *bi) \
+{ \
+ u32 status, ctrl; \
+ unsigned int scale, base; \
+ \
+ if (!bi) \
+ return (null_rc); \
+ \
+ if (!FIELD_GET(CXL_BI_##TYPE##_CAPS_EXPLICIT_COMMIT_REQ, \
+ readl(bi + CXL_BI_##TYPE##_CAPS_OFFSET))) \
+ return 0; \...
+}
...
+DEFINE_CXL_BI_COMMIT(rt, RT, 0);
+DEFINE_CXL_BI_COMMIT(decoder, DECODER, -EINVAL);
... and we do such things in various kernel subsystems (atomics, locking, etc.).
But I went ahead and got rid of the macro altogether, cannot say it is nicer than the above, even when trying to reduce some of the duplicated code with a wait helper.
+static int __cxl_bi_wait_commit(struct device *dev, void __iomem *status_reg,
+ u32 committed_bit, u32 err_bit,
+ unsigned int scale, unsigned int base)
+{
+ unsigned long tmo_us, poll_us; + ktime_t start; + u32 status; + int rc;
+
+ tmo_us = min_t(unsigned long, CXL_BI_COMMIT_MAXTMO_US,
+ __cxl_bi_get_timeout_us(dev, scale, base));
+ poll_us = max_t(unsigned long, tmo_us / 10, 1); /* ~10% */
+ start = ktime_get();
+
+ rc = readx_poll_timeout(readl, status_reg, status,
+ status & (committed_bit | err_bit),
+ poll_us, tmo_us);
+ if (rc) {
+ dev_dbg(dev, "BI-ID commit timed out (%luus)\n", tmo_us);
+ return rc; /* -ETIMEDOUT */
+ }
+
+ if (status & err_bit) {
+ dev_dbg(dev, "BI-ID commit rejected by hardware\n");
+ return -EIO;
+ }
+
+ dev_dbg(dev, "BI-ID commit wait took %lluus\n",
+ ktime_to_us(ktime_sub(ktime_get(), start)));
+ return 0;
+}
Then, for example __cxl_bi_commit_decoder():
+static int __cxl_bi_commit_decoder(struct device *dev, void __iomem *bi) +{
+ u32 status, ctrl;
+ unsigned int scale, base;
+
+ if (!bi)
+ return -EINVAL;
+
+ if (!FIELD_GET(CXL_BI_DECODER_CAPS_EXPLICIT_COMMIT_REQ,
+ readl(bi + CXL_BI_DECODER_CAPS_OFFSET)))
+ return 0;
+
+ ctrl = readl(bi + CXL_BI_DECODER_CTRL_OFFSET);
+ writel(ctrl & ~CXL_BI_DECODER_CTRL_BI_COMMIT,
+ bi + CXL_BI_DECODER_CTRL_OFFSET);
+ writel(ctrl | CXL_BI_DECODER_CTRL_BI_COMMIT,
+ bi + CXL_BI_DECODER_CTRL_OFFSET);
+
+ status = readl(bi + CXL_BI_DECODER_STATUS_OFFSET); + scale = FIELD_GET(CXL_BI_DECODER_STATUS_BI_COMMIT_TM_SCALE, status); + base = FIELD_GET(CXL_BI_DECODER_STATUS_BI_COMMIT_TM_BASE, status); + + return __cxl_bi_wait_commit(dev, bi + CXL_BI_DECODER_STATUS_OFFSET, + CXL_BI_DECODER_STATUS_BI_COMMITTED, + CXL_BI_DECODER_STATUS_BI_ERR_NOT_COMMITTED, + scale, base);
+}
Quoted 5 lines
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 1006
Quoted 3 lines
drivers/cxl/core/pci.c +2
-0+ +static int __cxl_bi_commit_rt (struct device *dev,void __iomem *bi)I do wonder if 'struct
cxl_component_regs*' would be better passed in than allowing arbitrary iomem addr.
Comparing how both are currently passed along (ie: ras), I am more inclined to leave it as is.
Quoted 22 lines
Quoted 22 lines
drivers/cxl/core/pci.c +21
-0+{ +if (!bi) +return 0 ; + +if (FIELD_GET (CXL_BI_RT_CAPS_EXPLICIT_COMMIT_REQ , +readl (bi +CXL_BI_RT_CAPS_OFFSET ))) +___cxl_bi_commit (dev, bi,RT ); + +return 0 ; +} + +static int __cxl_bi_commit (struct device *dev,void __iomem *bi) +{ +if (!bi) +return -EINVAL ; + +if (!FIELD_GET (CXL_BI_DECODER_CAPS_EXPLICIT_COMMIT_REQ , +readl (bi +CXL_BI_DECODER_CAPS_OFFSET ))) +return 0 ; + +___cxl_bi_commit (dev, bi,DECODER );
Quoted 8 lines
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 1032
Quoted 5 lines
drivers/cxl/core/pci.c +5
-0+return 0 ; +} + +/* Enable or dealloc BI-ID changes in the given level of the topology. */ +static int cxl_bi_ctrl_dport (struct cxl_dport *dport,bool enable)I don't particularly care for the bool for toggle. Please either use an enum for readability or use wrapper functions i.e.
cxl_bi_ctrl_dport_enable()/_disable().
Agreed.
Quoted 189 lines
Quoted 189 lines
drivers/cxl/core/pci.c +188
-0+{ +struct pci_dev *pdev =to_pci_dev (dport->dport_dev); +void __iomem *bi = dport->regs.bi_decoder; +struct cxl_port *port = dport->port; +u32 ctrl, value; +int rc; + +guard (device)(&port->dev); + +if (!bi) +return -EINVAL ; + + ctrl =readl (bi +CXL_BI_DECODER_CTRL_OFFSET ); + +switch (pci_pcie_type (pdev)) { +case PCI_EXP_TYPE_ROOT_PORT : +if (enable) { +/* +* There is no point of failure from here on, +* BI will be enabled on the endpoint device. +*/ + port->nr_bi++; + +if (FIELD_GET (CXL_BI_DECODER_CTRL_BI_FW , ctrl)) +return 0 ; + + value = ctrl |CXL_BI_DECODER_CTRL_BI_FW ; + value &= ~CXL_BI_DECODER_CTRL_BI_ENABLE ; + }else { +if (WARN_ON_ONCE (port->nr_bi ==0 )) +return -EINVAL ; +if (--port->nr_bi >0 ) +return 0 ; + + value = ctrl & ~(CXL_BI_DECODER_CTRL_BI_FW | +CXL_BI_DECODER_CTRL_BI_ENABLE ); + } + +writel (value, bi +CXL_BI_DECODER_CTRL_OFFSET ); +return 0 ; +case PCI_EXP_TYPE_DOWNSTREAM : +if (enable) { + value = ctrl & ~CXL_BI_DECODER_CTRL_BI_FW ; + value |=CXL_BI_DECODER_CTRL_BI_ENABLE ; + }else { +if (!FIELD_GET (CXL_BI_DECODER_CTRL_BI_ENABLE , ctrl)) +return 0 ; + value = ctrl & ~(CXL_BI_DECODER_CTRL_BI_FW | +CXL_BI_DECODER_CTRL_BI_ENABLE ); + } + +writel (value, bi +CXL_BI_DECODER_CTRL_OFFSET ); + + rc =__cxl_bi_commit (dport->dport_dev, bi); +if (rc) +return rc; + +return __cxl_bi_commit_rt (&port->dev, port->regs.bi_rt); +default : +return -EINVAL ; + } +} + +static int cxl_bi_ctrl_endpoint (struct cxl_dev_state *cxlds,bool enable) +{ +struct cxl_port *endpoint = cxlds->cxlmd->endpoint; +void __iomem *bi = endpoint->regs.bi_decoder; +u32 ctrl, val; + +if (!bi) +return -EINVAL ; + + ctrl =readl (bi +CXL_BI_DECODER_CTRL_OFFSET ); + +if (enable) { +if (FIELD_GET (CXL_BI_DECODER_CTRL_BI_ENABLE , ctrl)) { +WARN_ON_ONCE (!cxlds->bi); +return 0 ; + } + val = ctrl |CXL_BI_DECODER_CTRL_BI_ENABLE ; + }else { +if (!FIELD_GET (CXL_BI_DECODER_CTRL_BI_ENABLE , ctrl)) { +WARN_ON_ONCE (cxlds->bi); +return 0 ; + } + val = ctrl & ~CXL_BI_DECODER_CTRL_BI_ENABLE ; + } + +writel (val, bi +CXL_BI_DECODER_CTRL_OFFSET ); + cxlds->bi = enable; + +dev_dbg (cxlds->dev,"BI requests %s\n" , +str_enabled_disabled (enable)); + +return 0 ; +} + +/* +* devm teardown on cxl_mem unbind. Endpoint decoders may still be +* committed here (cxl_workqueue tears them down asynchronously), but +* memory access has been quiesced. +*/ +static void cxl_bi_dealloc (void *data) +{ +struct cxl_dev_state *cxlds = data; +struct cxl_memdev *cxlmd = cxlds->cxlmd; +struct cxl_port *endpoint = cxlmd->endpoint; +struct cxl_dport *dport, *_dport; +struct cxl_port *port; + + struct cxl_port *_port __free(put_cxl_port) = +cxl_pci_find_port (to_pci_dev (cxlds->dev), &_dport); + +if (!_port || !cxlds->bi) +return ; + +if (endpoint) { + scoped_guard(rwsem_read, &cxl_rwsem.region) +cxl_bi_ctrl_endpoint (cxlds, false); + }else { +/* +* Endpoint already removed: regions torn down and +* decoders uncommitted. Endpoint BI registers are no +* longer mapped; just clear the flag. +*/ + cxlds->bi = false; + } + + port = _port; + dport = _dport; +while (!is_cxl_root (port)) { +int rc =cxl_bi_ctrl_dport (dport, false); + +/* best effort */ +if (rc) +dev_dbg (&port->dev, +"BI dport disable failed: %d\n" , rc); + + dport = port->parent_dport; + port = dport->port; + } +} + +int cxl_bi_setup (struct cxl_dev_state *cxlds) +{ +struct pci_dev *pdev =to_pci_dev (cxlds->dev); +struct cxl_port *endpoint = cxlds->cxlmd->endpoint; +struct cxl_dport *dport, *_dport, *failed; +struct cxl_port *port; +int rc; + + struct cxl_port *_port __free(put_cxl_port) = +cxl_pci_find_port (pdev, &_dport); + +if (!_port) +return -EINVAL ; + +if (!cxl_is_bi_capable (pdev, endpoint->regs.bi_decoder)) +return 0 ; + +/* walkup the topology twice, first to check, then to enable */ + port = _port; + dport = _dport; +while (!is_cxl_root (port)) { +/* check rp, dsp */ +if (!cxl_is_bi_capable (to_pci_dev (dport->dport_dev), + dport->regs.bi_decoder)) +return -EINVAL ; + +/* check usp */ +if (dev_is_pci (port->uport_dev) && +pci_pcie_type (to_pci_dev (port->uport_dev)) == +PCI_EXP_TYPE_UPSTREAM ) +if (!cxl_is_bi_capable (to_pci_dev (port->uport_dev), + port->regs.bi_rt)) +return -EINVAL ; + + dport = port->parent_dport; + port = dport->port; + } + + port = _port; + dport = _dport; +while (!is_cxl_root (port)) { + rc =cxl_bi_ctrl_dport (dport, true); +if (rc) + goto err_rollback; +
Quoted 8 lines
drivers/cxl/core/pci.c int cxl_port_get_possible_dports(struct cxl_port *port) · line 1226
Quoted 6 lines
drivers/cxl/core/pci.c +6
-0+ dport = port->parent_dport; + port = dport->port; + } + +/* finally, enable BI on the device */ +cxl_bi_ctrl_endpoint (cxlds, true);ignoring returned error. if endpoint fail to enable, do you need to rollback and skip the dealloc registration?
Yep.
Thanks,
Davidlohr
Davidlohr Bueso
maintainerPATCH v3 3/5 cxl/hdm: Add BI coherency support for endpoint decoders
In reply to Davidlohr BuesoAdd cxl_dpa_set_coherence() to set an endpoint decoder's coherency type, validated against the HDM's supported coherency models and the device's BI readiness (cxlds->bi). Program the BI bit in the HDM decoder control register at commit time.
For uncommitted decoders, init_hdm_decoder() defaults target_type from supported_coherency: Type 3 devices default to HDM-DB when the HDM is DEV-only, HDM-H otherwise.
Pre-committed decoders with the BI bit set are not supported because endpoint and port enumerations are independent -- at decoder enumeration cxlds->bi is not yet known, so the topology cannot be verified.
--- drivers/cxl/core/core.h | 2 ++ drivers/cxl/core/hdm.c | 53 ++++++++++++++++++++++++++++++++++++++--- drivers/cxl/cxl.h | 6 +++++ drivers/cxl/cxlmem.h | 2 ++ 4 files changed, 60 insertions(+), 3 deletions(-)
drivers/cxl/core/core.h +2 -0
@@ -99,6 +99,8 @@ void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr,struct dentry *cxl_debugfs_create_dir (const char *dir);int cxl_dpa_set_part (struct cxl_endpoint_decoder *cxled,enum cxl_partition_mode mode); +int cxl_dpa_set_coherence (struct cxl_endpoint_decoder *cxled, +enum cxl_decoder_type type);int cxl_dpa_alloc (struct cxl_endpoint_decoder *cxled,u64 size);int cxl_dpa_free (struct cxl_endpoint_decoder *cxled);resource_size_t cxl_dpa_size (struct cxl_endpoint_decoder *cxled);
drivers/cxl/core/hdm.c +50 -3
86 lines not on this page · raw message
drivers/cxl/cxl.h +6 -0
@@ -57,6 +57,11 @@ extern const struct nvdimm_security_ops *cxl_security_ops;#define CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9)#define CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY BIT(11)#define CXL_HDM_DECODER_INTERLEAVE_16_WAY BIT(12) +#define CXL_HDM_DECODER_SUPPORTED_COHERENCY_MASK GENMASK(22, 21) +#define CXL_HDM_DECODER_COHERENCY_NONE 0x0 +#define CXL_HDM_DECODER_COHERENCY_DEV 0x1 +#define CXL_HDM_DECODER_COHERENCY_HOST 0x2 +#define CXL_HDM_DECODER_COHERENCY_BOTH 0x3#define CXL_HDM_DECODER_CTRL_OFFSET 0x4#define CXL_HDM_DECODER_ENABLE BIT(1)#define CXL_HDM_DECODER0_BASE_LOW_OFFSET (i) (0x20 * (i) + 0x10)
@@ -71,6 +76,7 @@ extern const struct nvdimm_security_ops *cxl_security_ops;#define CXL_HDM_DECODER0_CTRL_COMMITTED BIT(10)#define CXL_HDM_DECODER0_CTRL_COMMIT_ERROR BIT(11)#define CXL_HDM_DECODER0_CTRL_HOSTONLY BIT(12) +#define CXL_HDM_DECODER0_CTRL_BI BIT(13)#define CXL_HDM_DECODER0_TL_LOW (i) (0x20 * (i) + 0x24)#define CXL_HDM_DECODER0_TL_HIGH (i) (0x20 * (i) + 0x28)#define CXL_HDM_DECODER0_SKIP_LOW (i) CXL_HDM_DECODER0_TL_LOW(i)
drivers/cxl/cxlmem.h +2 -0
@@ -837,6 +837,7 @@ int cxl_mem_sanitize(struct cxl_memdev *cxlmd, u16 cmd);* @target_count: for switch decoders, max downstream port targets * @interleave_mask: interleave granularity capability, see check_interleave_cap() * @iw_cap_mask: bitmask of supported interleave ways, see check_interleave_cap() +* @supported_coherency: HDM Decoder Capability supported coherency mask * @port: mapped cxl_port, see devm_cxl_setup_hdm() */ struct cxl_hdm {
@@ -845,6 +846,7 @@ struct cxl_hdm {unsigned int target_count;unsigned int interleave_mask;unsigned long iw_cap_mask; +unsigned int supported_coherency;struct cxl_port *port; };
Signature · 1 line
2.39.5
Re: [PATCH v3 3/5] cxl/hdm: Add BI coherency support for endpoint decoders
Quoted 9 lines · Davidlohr Bueso
Add
cxl_dpa_set_coherence()to set an endpoint decoder's coherency type, validated against the HDM's supported coherency models and the device's BI readiness (cxlds->bi). Program the BI bit in the HDM decoder control register at commit time.For uncommitted decoders,
init_hdm_decoder()defaultstarget_typefromsupported_coherency: Type 3 devices default to HDM-DB when the HDM is DEV-only, HDM-H otherwise.
Quoted 7 lines
Pre-committed decoders with the BI bit set are not supported because endpoint and port enumerations are independent -- at decoder enumeration
cxlds->biis not yet known, so the topology cannot be verified.Signed-off-by Davidlohr Bueso <[email protected]>---
LGTM,
Davidlohr Bueso
maintainerPATCH v3 4/5 cxl: Add HDM-DB region creation and sysfs interface
In reply to Davidlohr BuesoA single Type 3 device can expose different parts of its memory with different coherency semantics: some ranges as HDM-H (host-only), others as HDM-DB (device-coherent with back-invalidate). Coherency models are therefore defined per memory region.
Surface this through the sysfs interface and region-creation flow:
- cap_bi (root decoder, RO): set when the underlying CFMWS window declares the BI restriction (ACPI_CEDT_CFMWS_RESTRICT_BI), propagated via a new CXL_DECODER_F_BI flag.
- create_{pmem,ram}_bi_region (root decoder, RW): create an HDM-DB region. Visible only when cap_bi is set.
- decoderX.Y/bi (endpoint decoder, RW): HDM-DB state of this decoder. Writes ('1' to set, '0' to clear) go through cxl_dpa_set_coherence() and are rejected after commit.
- regionZ/bi (region, RO): '1' if the region is HDM-DB.
--- Documentation/ABI/testing/sysfs-bus-cxl | 39 +++++++++++++-- drivers/cxl/acpi.c | 2 + drivers/cxl/core/core.h | 2 + drivers/cxl/core/port.c | 64 ++++++++++++++++++++++++- drivers/cxl/core/region.c | 60 +++++++++++++++++++++-- drivers/cxl/cxl.h | 1 + 6 files changed, 160 insertions(+), 8 deletions(-)
Documentation/ABI/testing/sysfs-bus-cxl +36 -3
@@ -297,7 +297,7 @@ Description: Each entry in the list is a dport id. -What: /sys/bus/cxl/devices/decoderX.Y/cap_{pmem,ram,type2,type3} +What: /sys/bus/cxl/devices/decoderX.Y/cap_{pmem,ram,type2,type3,bi} Date: June, 2021 KernelVersion: v5.14 Contact: [email protected]
@@ -306,8 +306,9 @@ Description: represents a fixed memory window identified by platform firmware. A fixed window may only support a subset of memory types. The 'cap_*' attributes indicate whether persistent - memory, volatile memory, accelerator memory, and / or expander - memory may be mapped behind this decoder's memory window. + memory, volatile memory, accelerator memory, expander memory, + and / or back-invalidate (HDM-DB) memory may be mapped behind + this decoder's memory window. What: /sys/bus/cxl/devices/decoderX.Y/target_type
@@ -426,6 +427,38 @@ Description: current cached value. +What: /sys/bus/cxl/devices/decoderX.Y/create_{pmem,ram}_bi_region +Date: May, 2026 +KernelVersion: v7.2 +Contact: [email protected] +Description: + (RW) Same as create_{pmem,ram}_region but creates an HDM-DB + (Back-Invalidate) region where the device manages coherency + via BISnp/BIRsp messages. Only visible when the root decoder + has the cap_bi flag set. Requires 256B Flit mode and BI + capability throughout the CXL topology. + + +What: /sys/bus/cxl/devices/decoderX.Y/bi +Date: May, 2026 +KernelVersion: v7.2 +Contact: [email protected] +Description: + (RW) HDM-DB (device-managed coherency with back-invalidate) + state of this endpoint decoder. Writing '1' configures the + decoder for HDM-DB; '0' clears it. Reads return '1' if the + decoder is configured for HDM-DB and the device's BI is + enabled. + +What: /sys/bus/cxl/devices/regionZ/bi +Date: May, 2026 +KernelVersion: v7.2 +Contact: [email protected] +Description: + (RO) Shows '1' if the region uses HDM-DB coherency model, + '0' otherwise. + + What: /sys/bus/cxl/devices/decoderX.Y/delete_region Date: May, 2022 KernelVersion: v6.0
drivers/cxl/acpi.c +2 -0
@@ -152,6 +152,8 @@ static unsigned long cfmws_to_decoder_flags(int restrictions) flags |=CXL_DECODER_F_PMEM ;if (restrictions &ACPI_CEDT_CFMWS_RESTRICT_FIXED ) flags |=CXL_DECODER_F_LOCK ; +if (restrictions &ACPI_CEDT_CFMWS_RESTRICT_BI ) + flags |=CXL_DECODER_F_BI ;return flags; }
drivers/cxl/core/core.h +2 -0
@@ -29,6 +29,8 @@ struct cxl_region_context {extern struct device_attribute dev_attr_create_pmem_region;extern struct device_attribute dev_attr_create_ram_region; +extern struct device_attribute dev_attr_create_pmem_bi_region; +extern struct device_attribute dev_attr_create_ram_bi_region;extern struct device_attribute dev_attr_delete_region;extern struct device_attribute dev_attr_region;extern const struct device_type cxl_pmem_region_type;
drivers/cxl/core/port.c +63 -1
105 lines not on this page · raw message
drivers/cxl/core/region.c +56 -4
105 lines not on this page · raw message
drivers/cxl/cxl.h +1 -0
@@ -287,6 +287,7 @@ int cxl_dport_map_rcd_linkcap(struct pci_dev *pdev, struct cxl_dport *dport);#define CXL_DECODER_F_LOCK BIT(4)#define CXL_DECODER_F_ENABLE BIT(5)#define CXL_DECODER_F_NORMALIZED_ADDRESSING BIT(6) +#define CXL_DECODER_F_BI BIT(7)#define CXL_DECODER_F_RESET_MASK (CXL_DECODER_F_ENABLE | CXL_DECODER_F_LOCK)enum cxl_decoder_type {
Signature · 1 line
2.39.5
Re: [PATCH v3 4/5] cxl: Add HDM-DB region creation and sysfs interface
Quoted 14 lines · Davidlohr Bueso
A single Type 3 device can expose different parts of its memory with different coherency semantics: some ranges as HDM-H (host-only), others as HDM-DB (device-coherent with back-invalidate). Coherency models are therefore defined per memory region.
Surface this through the sysfs interface and region-creation flow:
-
cap_bi(root decoder, RO): set when the underlying CFMWS window declares the BI restriction (ACPI_CEDT_CFMWS_RESTRICT_BI), propagated via a newCXL_DECODER_F_BIflag.- create_{pmem,ram}_bi_region (root decoder, RW): create an HDM-DB region. Visible only when
cap_biis set.
Quoted 7 lines
- decoderX.Y/bi (endpoint decoder, RW): HDM-DB state of this decoder. Writes ('1' to set, '0' to clear) go through
cxl_dpa_set_coherence()and are rejected after commit.- regionZ/bi (region, RO): '1' if the region is HDM-DB.
Signed-off-by Davidlohr Bueso <[email protected]>
LGTM,
Davidlohr Bueso
maintainerQuoted 3 lines · Davidlohr Bueso
Hello,
Changes from v1 (https://lore.kernel.org/all/[email protected]/):
Ergh, the patches that are labeled v3 are part of this series, fyi - while the cover letter says v2, sorry for the confusion (of course along with the rfc, this is actually the third iteration of this series :)
Thanks,
Davidlohr
Davidlohr Bueso
maintainerPATCH v3 5/5 cxl/mem: Wire up BI for Type 3 device
In reply to Davidlohr BuesoSetup BI flows during cxl_mem_probe() after endpoint enumeration and EDAC registration.
BI setup failure is non-fatal - the device continues to operate in HDM-H mode without back-invalidate support.
--- drivers/cxl/mem.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/cxl/mem.c +4 -0
@@ -150,6 +150,10 @@ static int cxl_mem_probe(struct device *dev)if (rc)dev_dbg (dev,"CXL memdev EDAC registration failed rc=%d\n" , rc); + rc =cxl_bi_setup (cxlds); +if (rc) +dev_dbg (dev,"BI setup failed rc=%d\n" , rc); +/* * The kernel may be operating out of CXL memory on this device, * there is no spec defined way to determine whether this device
Signature · 1 line
2.39.5
Re: [PATCH v3 5/5] cxl/mem: Wire up BI for Type 3 device
Quoted 7 lines · Davidlohr Bueso
Setup BI flows during
cxl_mem_probe()after endpoint enumeration and EDAC registration.BI setup failure is non-fatal - the device continues to operate in HDM-H mode without back-invalidate support.
Signed-off-by Davidlohr Bueso <[email protected]>
I would mention why this patch is split it somewhere (cover letter would be fine). I think the reason it's split out from 2/5 is that you need the HDM setup portions before enabling the device. You could possibly reorder the series so that the HDM set up is first then squash this into the current 2/5, but that's probably overkill.
Also, I'd change the subject from "Wire up" to "Enable" since that's what it seems this is doing.
Thanks,
Ben
Quoted 19 lines
--- drivers/cxl/mem.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/cxl/mem.c +4
-0@@ -150,6 +150,10 @@ static int cxl_mem_probe(struct device *dev)if (rc)dev_dbg (dev,"CXL memdev EDAC registration failed rc=%d\n" , rc); + rc =cxl_bi_setup (cxlds); +if (rc) +dev_dbg (dev,"BI setup failed rc=%d\n" , rc); +/* * The kernel may be operating out of CXL memory on this device, * there is no spec defined way to determine whether this device
Dave Jiang
maintainerRe: [PATCH v3 5/5] cxl/mem: Wire up BI for Type 3 device
Quoted 7 lines · Benjamin
Quoted 6 lines · Davidlohr Bueso
Setup BI flows during
cxl_mem_probe()after endpoint enumeration and EDAC registration.BI setup failure is non-fatal - the device continues to operate in HDM-H mode without back-invalidate support.
Quoted 6 lines
Quoted 1 line
Signed-off-by Davidlohr Bueso <[email protected]>I would mention why this patch is split it somewhere (cover letter would be fine). I think the reason it's split out from 2/5 is that you need the HDM setup portions before enabling the device. You could possibly reorder the series so that the HDM set up is first then squash this into the current 2/5, but that's probably overkill.
Given how small this change is and stand alone, it would make more sense to be with 2/5 so the reviewer can see how it's being used when reviewing 2/5. Also, when you need to do git bisect, it would make more sense.
DJ
Quoted 26 lines
Also, I'd change the subject from "Wire up" to "Enable" since that's what it seems this is doing.
Thanks,
BenQuoted 19 lines
--- drivers/cxl/mem.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/cxl/mem.c +4
-0@@ -150,6 +150,10 @@ static int cxl_mem_probe(struct device *dev)if (rc)dev_dbg (dev,"CXL memdev EDAC registration failed rc=%d\n" , rc); + rc =cxl_bi_setup (cxlds); +if (rc) +dev_dbg (dev,"BI setup failed rc=%d\n" , rc); +/* * The kernel may be operating out of CXL memory on this device, * there is no spec defined way to determine whether this device
Dan Williams (nvidia)
maintainerQuoted 1 line · Davidlohr Bueso
Hello,
Hi Davidlohr,
Quoted 3 lines
The idea is for Type 3 memdevs and future Type 2 devices to make use of
cxlds->biwhen committing HDM decoders, such that different device coherence models can be differentiated as:
[..]
Quoted 1 line
o Patch 4 adds ABI (sysfs) for HDM-DB region creation.
I am skeptical about needing new region creation uapi for this. A question from the example below:
[..]
Quoted 2 lines
Testing has been done with the qemu HDM-DB Type 3 counterpart (now merged).
[..]
Quoted 2 lines
2. On a Type 3 HDM-DB volatile device, create one BI and one regular region.
---------------------------------------------------------------------------
[..]
Quoted 1 line
# echo region0 > /sys/bus/cxl/devices/decoder0.0/create_ram_bi_region
[..]
Quoted 1 line
# echo region1 > /sys/bus/cxl/devices/decoder0.0/create_ram_region
...how can a single root decoder support mixed operation?
The root decoder restrictions specify the endpoint decoder requirements. There is no flexibility to entertain beyond picking the root decoder resource, right? Linux can avoid all the configuration corner cases by making the endpoint decoder inherit settings by topology configuration.
Jonathan Cameron
maintainerQuoted 23 lines · On Mon, 18 May 2026 18:52:38 -0700 "Dan Williams (nvidia)" <[email protected]>…
Quoted 1 line · Davidlohr Bueso
Hello,
Hi Davidlohr,
Quoted 3 lines
The idea is for Type 3 memdevs and future Type 2 devices to make use of
cxlds->biwhen committing HDM decoders, such that different device coherence models can be differentiated as:[..]
Quoted 1 line
o Patch 4 adds ABI (sysfs) for HDM-DB region creation.
I am skeptical about needing new region creation uapi for this. A question from the example below:
[..]
Quoted 2 lines
Testing has been done with the qemu HDM-DB Type 3 counterpart (now merged).
[..]
Quoted 2 lines
2. On a Type 3 HDM-DB volatile device, create one BI and one regular region.
---------------------------------------------------------------------------[..]
Quoted 1 line
# echo region0 > /sys/bus/cxl/devices/decoder0.0/create_ram_bi_region[..]
Quoted 9 lines
Quoted 1 line
# echo region1 > /sys/bus/cxl/devices/decoder0.0/create_ram_region...how can a single root decoder support mixed operation?
The root decoder restrictions specify the endpoint decoder requirements. There is no flexibility to entertain beyond picking the root decoder resource, right? Linux can avoid all the configuration corner cases by making the endpoint decoder inherit settings by topology configuration.
Hi Dan,
Assuming I understand the question correctly...
No idea if anyone has done it but as far as I can tell you 'could' easily enough have a single fixed memory window that supports both BI and non BI, the host implementation would just have to use the programming of the decoders in the host bridges to figure out what it needs to support.
I'm not aware of anything in the spec that says you can't implement that. Now maybe it's a non thing but if it turns up later we need to be careful not to wall ourselves into a corner from ABI point of view.
Usual reasoning of HPA space being a fairly costly / limited resource would be the reason to build this more complex hardware.
Jonathan
Dan Williams (nvidia)
maintainerJonathan Cameron wrote:
[..]
Quoted 14 lines
Quoted 5 lines
The root decoder restrictions specify the endpoint decoder requirements. There is no flexibility to entertain beyond picking the root decoder resource, right? Linux can avoid all the configuration corner cases by making the endpoint decoder inherit settings by topology configuration.
Hi Dan,
Assuming I understand the question correctly...
No idea if anyone has done it but as far as I can tell you 'could' easily enough have a single fixed memory window that supports both BI and non BI, the host implementation would just have to use the programming of the decoders in the host bridges to figure out what it needs to support.
Quoted 6 lines
I'm not aware of anything in the spec that says you can't implement that. Now maybe it's a non thing but if it turns up later we need to be careful not to wall ourselves into a corner from ABI point of view.
Usual reasoning of HPA space being a fairly costly / limited resource would be the reason to build this more complex hardware.
The BI bit in the CFMWS, unlike volatile and PMEM restrictions, has additional references by bit0 and bit1:
Bit[0]: ...(HDM-D if bit[5]=0; HDM-DB if bit[5]=1).
Bit[1]: ...If an HDM decoder that is mapped to this windows has the BI
bit set (bit[5]=1), it will result in undefined behavior.
The Bit[0] wording does not appear to leave room for optionality.
The Bit[1] wording needs fixing, but to my eyes says "host-only == host-only, do not enable BI operation in any decoder that maps this window and expect it to work".
Davidlohr Bueso
maintainerQuoted 7 lines · Dan Williams (nvidia)
The BI bit in the CFMWS, unlike volatile and PMEM restrictions, has additional references by bit0 and bit1:
Bit[0]: ...(HDM-D if bit[5]=0; HDM-DB if bit[5]=1).
Bit[1]: ...If an HDM decoder that is mapped to this windows has the BI
bit set (bit[5]=1), it will result in undefined behavior.
Quoted 5 lines
The Bit[0] wording does not appear to leave room for optionality.
The Bit[1] wording needs fixing, but to my eyes says "host-only == host-only, do not enable BI operation in any decoder that maps this window and expect it to work".
So for the qemu support, the Window restrictions will need to be updated. It used to be 0xF (bit0+bit1 enabled), which is wrong, and then bit5 was enabled when BI support was added.
I think we should just set it to host-only coherent (bit1), and then just rely on restrictions= parameter for testing device-coherent. Otherwise, as is, the kernel will always try to go for BI and fail when not passing hdm-db option to the device. The kernel should also probably complain if it sees a Window with both kinds of coherency restrictions.
Thanks,
Davidlohr
hw/cxl/cxl-host.c +1 -1
@@ -64,7 +64,7 @@ static void cxl_fixed_memory_window_config(CXLFixedMemoryWindowOptions *object,if (object->has_restrictions) { fw->restrictions = object->restrictions; }else { - fw->restrictions =0x2f ;/* No restrictions */ + fw->restrictions =0xe ;/* host-only coherent, volatile, pmem */ } fw->targets =g_malloc0_n (fw->num_targets,sizeof (*fw->targets));