hw/riscv/virt: Add CXL support to the RISC-V virt machine
v2 cover hw/riscv/virt: Add CXL support to the RISC-V virt machine
This series adds CXL (Compute Express Link) support to the RISC-V virt machine in QEMU, following the same approach as the ARM virt machine.
Prerequisite
------------
This series depends on Alireza Sanaee's v8 series [1]:
[1/3] hw/cxl: Use HPA in cxl_cfmws_find_device() rather than offset
in window
[2/3] hw/cxl: Allow cxl_cfmws_find_device() to filter on whether
interleaved paths are accepted
[3/3] hw/cxl: Add a performant (and correct) path for the non
interleaved cases
Patch [1/3] is already in the tree. Patches [2/3] and [3/3] have been tested and verified functional on RISC-V QEMU with CXL enabled. They are not included in this posting; please apply them before this series.
Series overview
---------------
[1/4] (hw/riscv/virt): Wires up the core CXL machine support -- Kconfig selects, CXLState, PCIBus pointer (pci_bus), cxl_machine_init(), CXL host register region, FMW mapping, and machine_done hooks.
[2/4] (hw/riscv/virt-acpi-build): Adds ACPI0017 (CXLM) to the DSDT with a _DEP on every ACPI0016 CXL host bridge device. The _DEP ensures the OS defers ACPI0017 enumeration until acpi_pci_root has attached all CXL host bridges, fixing a probe-ordering race that leaves the CXL port topology incomplete. A corresponding kernel change to call acpi_dev_clear_dependencies() in acpi_pci_root_add() is required on the Linux side. That kernel patch has merged:
https://lore.kernel.org/linux-pci/[email protected]/
[3/4] (hw/riscv/virt, gpex): Reserves the top 256 MiB of the 32-bit MMIO window for CXL host bridges. CXL component-register BARs are 64-bit non-prefetchable; per PCI-to-PCI Bridge Architecture Specification Rev 1.2 §3.2.5.8-10, only the prefetchable window can be 64-bit, so Linux places these BARs in the 32-bit non-prefetchable bridge window. Without this reservation PCI0 consumes the entire 1 GiB 32-bit range and CXL bridges get an empty _CRS, preventing BAR assignment.
[4/4] (tests/qtest): Adds a RISC-V bios-tables test for the CXL variant. Expected AML golden files (DSDT.cxl, CEDT.cxl) are generated and included.
Changes from v1
---------------
- hw/riscv/virt: PCIBus *bus renamed to PCIBus *pci_bus (Jonathan).
- hw/riscv/virt: Dropped outer if (s->pci_bus) guard around cxl_hook_up_pxb_registers(); the function already handles a NULL bus internally (Jonathan).
- hw/riscv/virt-acpi-build: All s->bus references updated to
s->pci_bus; iasl -d decompiled DSDT fragment added to commit
message.
- hw/riscv/virt,gpex: Commit message expanded with PCI-to-PCI Bridge
Spec §3.2.5.8/9/10 citations (Jonathan). - Original patch 4 ("Map committed HDM decoder ranges as RAM for direct DMA") dropped; superseded by Alireza Sanaee's v8 series [1], which correctly handles the interleaved/non-interleaved split and supports KVM.
- New patch 4: RISC-V ACPI bios-tables test for CXL, with golden AML files generated and included.
[1] https://lore.kernel.org/qemu-devel/[email protected]/
Test
---------------
Tested on the RISC-V virt machine with EDK2 firmware and a buildroot guest carrying the kernel patches above.
QEMU invocation (CXL-relevant options shown; EDK2 pflash, -bios, -kernel, -append and the virtio-blk rootfs are as usual):
qemu-system-riscv64 \
-M virt,aia=aplic-imsic,acpi=on,cxl=on \
-cpu rv64 -smp 2 -m 4G,maxmem=8G,slots=8 \
-object memory-backend-ram,id=vmem0,share=on,size=4G \
-device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1 \
-device cxl-rp,port=0,bus=cxl.1,id=rp0,chassis=0,slot=2 \
-device cxl-type3,bus=rp0,volatile-memdev=vmem0,id=cxl-vmem0 \
-M cxl-fmw.0.targets.0=cxl.1,cxl-fmw.0.size=4G \
... # EDK2 pflash + -bios fw_dynamic.bin + -kernel Image
# + virtio-blk rootfs + -append "root=/dev/vda ..."Verification (compare the two free -h outputs: total system memory grows by 4 GiB after onlining):
# free -h
# cxl list
# cxl enable-memdev mem0
# cxl create-region -m -t ram -d decoder0.0 -w 1 mem0 -s 4G
# daxctl online-memory dax0.0
# free -hChen Pei (4):
hw/riscv/virt: Add CXL support to the RISC-V virt machine hw/riscv/virt-acpi-build: Add _DEP to ACPI0017 for CXL host bridge
dependency
hw/riscv/virt,gpex: Provide 32-bit MMIO window for CXL host bridges tests/qtest: Add RISC-V ACPI bios tables test for CXL
hw/pci-host/gpex-acpi.c | 36 +++++++++- hw/riscv/Kconfig | 2 + hw/riscv/virt-acpi-build.c | 52 ++++++++++++++ hw/riscv/virt.c | 98 +++++++++++++++++++++++--- include/hw/pci-host/gpex.h | 1 + include/hw/riscv/virt.h | 3 + tests/data/acpi/riscv64/virt/CEDT.cxl | Bin 0 -> 108 bytes tests/data/acpi/riscv64/virt/DSDT.cxl | Bin 0 -> 6212 bytes tests/qtest/bios-tables-test.c | 54 ++++++++++++++ 9 files changed, 234 insertions(+), 12 deletions(-) create mode 100644 tests/data/acpi/riscv64/virt/CEDT.cxl create mode 100644 tests/data/acpi/riscv64/virt/DSDT.cxl
Signature · 1 line
2.50.1
PATCH v2 1/4 hw/riscv/virt: Add CXL support to the RISC-V virt machine
Enable CXL support on the RISC-V virt machine following the same approach used by the ARM virt machine:
- Add PXB and ACPI_CXL Kconfig selections
- Add CXLState and PCIBus pointer to RISCVVirtState
- Register CXL machine properties via cxl_machine_init()
- Create CXL host register region above the PCIe high MMIO region
- Call cxl_hook_up_pxb_registers() and cxl_fmws_link_targets() at machine_done time
- Map Fixed Memory Windows above the CXL host register region
- Add ACPI0017 device in DSDT and build CEDT table in virt-acpi-build.c
--- hw/riscv/Kconfig | 2 ++ hw/riscv/virt-acpi-build.c | 20 +++++++++++++++++++ hw/riscv/virt.c | 40 ++++++++++++++++++++++++++++++++++++++ include/hw/riscv/virt.h | 3 +++ 4 files changed, 65 insertions(+)
hw/riscv/Kconfig +2 -0
@@ -66,8 +66,10 @@ config RISCV_VIRT select VIRTIO_MMIO select FW_CFG_DMA select PLATFORM_BUS + select PXB select ACPI select ACPI_PCI + select ACPI_CXL config SHAKTI_C bool
hw/riscv/virt-acpi-build.c +20 -0
@@ -29,12 +29,16 @@#include "hw/acpi/aml-build.h" #include "hw/acpi/pci.h" #include "hw/acpi/utils.h" +#include "hw/acpi/cxl.h" #include "hw/intc/riscv_aclint.h" #include "hw/nvram/fw_cfg_acpi.h" #include "hw/pci-host/gpex.h" +#include "hw/pci/pci_bus.h" #include "hw/riscv/virt.h" #include "hw/riscv/numa.h" #include "hw/virtio/virtio-acpi.h" +#include "hw/cxl/cxl.h" +#include "hw/cxl/cxl_host.h" #include "kvm/kvm_riscv.h" #include "migration/vmstate.h" #include "qapi/error.h"
@@ -503,6 +507,17 @@ static void build_dsdt(GArray *table_data,acpi_dsdt_add_gpex_host (scope,PCIE_IRQ +VIRT_IRQCHIP_NUM_SOURCES *2 ); } +if (s->cxl_devices_state.is_enabled) { +Aml *cxl_dev =aml_device ("CXLM" ); +aml_append (cxl_dev,aml_name_decl ("_HID" ,aml_string ("ACPI0017" ))); +Aml *method =aml_method ("_STA" ,0 ,AML_NOTSERIALIZED ); +aml_append (method,aml_return (aml_int (0x0B ))); +aml_append (cxl_dev, method); +build_cxl_dsm_method (cxl_dev); + +aml_append (scope, cxl_dev); + } +aml_append (dsdt, scope);/* copy AML table into ACPI tables blob and patch header there */
@@ -910,6 +925,11 @@ static void virt_acpi_build(RISCVVirtState *s, AcpiBuildTables *tables) s->oem_table_id); } +if (s->cxl_devices_state.is_enabled) { +cxl_build_cedt (table_offsets, tables_blob, tables->linker, + s->oem_id, s->oem_table_id, &s->cxl_devices_state); + } +if (ms->numa_state->num_nodes >0 ) {acpi_add_table (table_offsets, tables_blob);build_srat (tables_blob, tables->linker, s);
hw/riscv/virt.c +40 -0
@@ -55,6 +55,8 @@#include "hw/pci/pci.h" #include "hw/pci-host/gpex.h" #include "hw/display/ramfb.h" +#include "hw/cxl/cxl.h" +#include "hw/cxl/cxl_host.h" #include "hw/acpi/aml-build.h" #include "qapi/qapi-visit-common.h" #include "hw/virtio/virtio-iommu.h"
@@ -1259,9 +1261,27 @@ static inline DeviceState *gpex_pcie_init(MemoryRegion *sys_mem, }GPEX_HOST (dev)->gpex_cfg.bus =PCI_HOST_BRIDGE (dev)->bus; + s->pci_bus =PCI_HOST_BRIDGE (dev)->bus;return dev; } +static void create_cxl_host_reg_region (RISCVVirtState *s) +{ +MemoryRegion *sysmem =get_system_memory (); +MemoryRegion *mr = &s->cxl_devices_state.host_mr; +hwaddr base; + +if (!s->cxl_devices_state.is_enabled) { +return ; + } + + base = virt_high_pcie_memmap.base + virt_high_pcie_memmap.size; + base =ROUND_UP (base,64 * KiB); + +memory_region_init (mr,OBJECT (s),"cxl_host_reg" ,64 * KiB *16 ); +memory_region_add_subregion (sysmem, base, mr); +} +static FWCfgState *create_fw_cfg (const MachineState *ms,hwaddr base) {FWCfgState *fw_cfg;
@@ -1426,6 +1446,13 @@ static void virt_machine_done(Notifier *notifier, void *data) machine_done);MachineState *machine =MACHINE (s);hwaddr start_addr = s->memmap[VIRT_DRAM ].base; + +cxl_hook_up_pxb_registers (s->pci_bus, &s->cxl_devices_state, + &error_fatal); + +if (s->cxl_devices_state.is_enabled) { +cxl_fmws_link_targets (&error_fatal); + }hwaddr firmware_end_addr;vaddr kernel_start_addr;const char *firmware_name =riscv_default_firmware_name (&s->soc[0 ]);
@@ -1663,6 +1690,17 @@ static void virt_machine_init(MachineState *machine)ROUND_UP (virt_high_pcie_memmap.base, virt_high_pcie_memmap.size); } +create_cxl_host_reg_region (s); + +if (s->cxl_devices_state.is_enabled) { +hwaddr cxl_base = virt_high_pcie_memmap.base + + virt_high_pcie_memmap.size; + cxl_base +=memory_region_size (&s->cxl_devices_state.host_mr); + cxl_base =ROUND_UP (cxl_base,256 * MiB); +cxl_fmws_set_memmap (cxl_base,UINT64_MAX ); +cxl_fmws_update_mmio (); + } +/* register system main memory (actual RAM) */ memory_region_add_subregion (system_memory, s->memmap[VIRT_DRAM ].base, machine->ram);
@@ -1769,6 +1807,8 @@ static void virt_machine_instance_init(Object *obj) s->oem_table_id =g_strndup (ACPI_BUILD_APPNAME8 ,8 ); s->acpi =ON_OFF_AUTO_AUTO ; s->iommu_sys =ON_OFF_AUTO_AUTO ; + +cxl_machine_init (obj, &s->cxl_devices_state); }static char *virt_get_aia_guests (Object *obj,Error **errp)
include/hw/riscv/virt.h +3 -0
@@ -24,6 +24,7 @@#include "hw/core/sysbus.h" #include "hw/block/flash.h" #include "hw/intc/riscv_imsic.h" +#include "hw/cxl/cxl.h" #define VIRT_CPUS_MAX_BITS 9#define VIRT_CPUS_MAX (1 << VIRT_CPUS_MAX_BITS)
@@ -64,6 +65,8 @@ struct RISCVVirtState {struct GPEXHost *gpex_host;OnOffAuto iommu_sys;uint16_t pci_iommu_bdf; +CXLState cxl_devices_state; +PCIBus *pci_bus; };enum {
Signature · 1 line
2.50.1
PATCH v2 2/4 hw/riscv/virt-acpi-build: Add _DEP to ACPI0017 for CXL host bridge dependency
In reply to Chen PeiOn RISC-V QEMU virt platform with CXL enabled, the probe ordering of acpi_pci_root (ACPI0016) and cxl_acpi (ACPI0017) is not guaranteed. If cxl_acpi probes before acpi_pci_root has attached the CXL host bridges, the CXL port topology will be incomplete because to_cxl_host_bridge() silently skips devices whose PCI root is not yet ready.
Add a _DEP object to the ACPI0017 device in the DSDT, declaring its dependency on the ACPI0016 CXL host bridge devices. This tells the OS to defer ACPI0017 enumeration until all ACPI0016 devices have been attached by acpi_pci_root.
This requires a corresponding kernel change to call acpi_dev_clear_dependencies() in acpi_pci_root_add().
The resulting DSDT fragment (iasl -d output) for a single CXL host bridge at bus 0x01 looks like:
Device (CXLM)
{
Name (_HID, "ACPI0017")
Name (_DEP, Package (0x01)
{
\_SB.PC01
})
Method (_STA, 0, NotSerialized)
{
Return (0x0B)
}
...
}--- hw/riscv/virt-acpi-build.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
hw/riscv/virt-acpi-build.c +32 -0
@@ -510,6 +510,38 @@ static void build_dsdt(GArray *table_data,if (s->cxl_devices_state.is_enabled) {Aml *cxl_dev =aml_device ("CXLM" );aml_append (cxl_dev,aml_name_decl ("_HID" ,aml_string ("ACPI0017" ))); + +/* +* Declare a _DEP on every ACPI0016 CXL host bridge so the OS +* defers ACPI0017 enumeration until acpi_pci_root has attached +* the CXL host bridges. Without this, cxl_acpi may probe before +* to_cxl_host_bridge() can resolve the PCI root and the CXL +* port topology comes up empty. +*/ +if (s->pci_bus) { +PCIBus *bus; +uint32_t num_cxl_hbs =0 ; + + QLIST_FOREACH(bus, &s->pci_bus->child, sibling) { +if (pci_bus_is_root (bus) &&pci_bus_is_cxl (bus)) { + num_cxl_hbs++; + } + } + +if (num_cxl_hbs >0 ) { +Aml *dep_pkg =aml_package (num_cxl_hbs); + + QLIST_FOREACH(bus, &s->pci_bus->child, sibling) { +if (pci_bus_is_root (bus) &&pci_bus_is_cxl (bus)) { +aml_append (dep_pkg, +aml_name ("\\_SB.PC%.02X" , +pci_bus_num (bus))); + } + } +aml_append (cxl_dev,aml_name_decl ("_DEP" , dep_pkg)); + } + } +Aml *method =aml_method ("_STA" ,0 ,AML_NOTSERIALIZED );aml_append (method,aml_return (aml_int (0x0B )));aml_append (cxl_dev, method);
Signature · 1 line
2.50.1
PATCH v2 4/4 tests/qtest: Add RISC-V ACPI bios tables test for CXL
In reply to Chen PeiAdd test_acpi_riscv64_virt_tcg_cxl() to verify that enabling CXL on the RISC-V virt machine produces correct ACPI tables, including the ACPI0017 CXLM device with _DEP in the DSDT and the CEDT table.
The test boots with cxl=on, one pxb-cxl bus (bus_nr=12), a CXL root port, a cxl-type3 persistent memory device, and a fixed memory window, mirroring the existing x86 q35 CXL test pattern.
Since pxb-cxl is a root bus, using -cdrom causes QEMU to auto-plug the cdrom drive into pxb-cxl, triggering "Only PCI/PCIe bridges can be plugged into pxb-cxl". The ISO is instead attached explicitly via a virtio-scsi-pci controller on pcie.0, following the same approach as test_acpi_aarch64_virt_tcg_pxb().
--- tests/data/acpi/riscv64/virt/CEDT.cxl | Bin 0 -> 108 bytes tests/data/acpi/riscv64/virt/DSDT.cxl | Bin 0 -> 6212 bytes tests/qtest/bios-tables-test.c | 54 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 tests/data/acpi/riscv64/virt/CEDT.cxl create mode 100644 tests/data/acpi/riscv64/virt/DSDT.cxl
tests/data/acpi/riscv64/virt/CEDT.cxl new file binary +0 -0
tests/data/acpi/riscv64/virt/DSDT.cxl new file binary +0 -0
tests/qtest/bios-tables-test.c +54 -0
66 lines not on this page · raw message
Signature · 1 line
2.50.1
PATCH v2 3/4 hw/riscv/virt,gpex: Provide 32-bit MMIO window for CXL host bridges
In reply to Chen PeiCXL component register BAR (BAR0 on CXL Root Port and Type3 device) and the CXL device register BAR (BAR2 on Type3 device) are declared as 64-bit non-prefetchable memory. Per the PCI-to-PCI Bridge Architecture Specification Rev 1.2 (PCI-SIG, 2003):
- §3.2.5.8 (Memory Base/Limit): the non-prefetchable window covers only 32-bit addresses (AD[31:20]); the Type 1 header defines no upper-32-bit extension for it.
- §3.2.5.9 (Prefetchable Memory Base/Limit): the bottom 4 bits encode 64-bit support (01h), but this applies exclusively to the *prefetchable* window.
- §3.2.5.10 (Prefetchable Base/Limit Upper 32 Bits): optional registers for AD[63:32] of the prefetchable range only.
The architecture therefore allows a 64-bit window only when it is also prefetchable; there is no 64-bit non-prefetchable form. PCIe inherits this Type 1 header layout unchanged. Linux thus places 64-bit non-prefetchable BARs in the 32-bit non-prefetchable bridge window, which requires the bridge to own enough address space below 4 GiB.
On RISC-V virt the 32-bit PCIe MMIO range (1 GiB at 0x40000000) is currently consumed entirely by PCI0, so CXL host bridges (ACPI0016) have no non-prefetchable window and Linux fails to assign these BARs. Marking the BARs prefetchable would work around it, but the CXL component registers have read/write side effects and are not prefetchable per the PCIe specification.
Reserve the top 256 MiB of the 32-bit MMIO window exclusively for CXL host bridges:
- Shrink PCI0's mmio32 window by 256 MiB in virt.c so that UEFI's PciHostBridgeDxe and the ACPI _CRS for PCI0 never claim that range
- Store the reserved range in a new gpex_cfg.cxl_mmio32 field
- In gpex-acpi.c, emit the cxl_mmio32 range as the Memory resource in the CXL host bridge _CRS instead of re-using build_crs() (which returns an empty set when UEFI has not assigned resources yet)
- Reduce the FDT 'ranges' for PCI0 by the same 256 MiB so that UEFI firmware driven by device-tree also respects the reservation
--- hw/pci-host/gpex-acpi.c | 36 +++++++++++++++++++++-- hw/riscv/virt.c | 58 +++++++++++++++++++++++++++++++------- include/hw/pci-host/gpex.h | 1 + 3 files changed, 83 insertions(+), 12 deletions(-)
hw/pci-host/gpex-acpi.c +34 -2
@@ -158,9 +158,41 @@ void acpi_dsdt_add_gpex(Aml *scope, struct GPEXConfig *cfg)* Resources defined for PXBs are composed of the following parts: * 1. The resources the pci-bridge/pcie-root-port need. * 2. The resources the devices behind pxb need. +* +* For CXL host bridges on platforms where UEFI (driven by +* FDT 'ranges') does not assign PCI resources for the CXL +* root bridge before ACPI table construction, build_crs() +* would return an empty resource set. When the platform +* has reserved a dedicated MMIO window for CXL host bridges +* (cfg->cxl_mmio32), emit that window as a static _CRS +* instead. The platform is responsible for shrinking PCI0's +* mmio32 window so the two do not overlap. */ - crs =build_crs (PCI_HOST_BRIDGE (BUS (bus)->parent), &crs_range_set, - cfg->pio.base,0 ,0 ,0 ); +if (is_cxl && cfg->cxl_mmio32.size) { +uint64_t cxl_base = cfg->cxl_mmio32.base; +uint64_t cxl_size = cfg->cxl_mmio32.size; + + crs =aml_resource_template (); + +/* 32-bit MMIO range for CXL devices */ +aml_append (crs, +aml_dword_memory (AML_POS_DECODE ,AML_MIN_FIXED , +AML_MAX_FIXED ,AML_NON_CACHEABLE , +AML_READ_WRITE ,0 , + cxl_base, + cxl_base + cxl_size -1 , +0 , cxl_size)); + +/* Bus number range */ +aml_append (crs, +aml_word_bus_number (AML_MIN_FIXED ,AML_MAX_FIXED , +AML_POS_DECODE ,0 , + bus_num, bus_num +15 , +0 ,16 )); + }else { + crs =build_crs (PCI_HOST_BRIDGE (BUS (bus)->parent), + &crs_range_set, cfg->pio.base,0 ,0 ,0 ); + }aml_append (dev,aml_name_decl ("_CRS" , crs));if (is_cxl) {
hw/riscv/virt.c +48 -10
76 lines not on this page · raw message
include/hw/pci-host/gpex.h +1 -0
@@ -43,6 +43,7 @@ struct GPEXConfig {MemMapEntry mmio32;MemMapEntry mmio64;MemMapEntry pio; +MemMapEntry cxl_mmio32;int irq;PCIBus *bus;bool pci_native_hotplug;
Signature · 1 line
2.50.1
Hi,
Quoted 145 lines · Chen Pei
Enable CXL support on the RISC-V virt machine following the same approach used by the ARM virt machine:
- Add PXB andACPI_CXLKconfigselections
- Add CXLState and PCIBus pointer to RISCVVirtState
- Register CXL machine properties viacxl_machine_init()
- Create CXL host register region above the PCIe high MMIO region
- Callcxl_hook_up_pxb_registers()andcxl_fmws_link_targets()atmachine_donetime
- Map Fixed Memory Windows above the CXL host register region
- Add ACPI0017 device in DSDT and build CEDT table invirt-acpi-build.cSigned-off-by Chen Pei <[email protected]>--- hw/riscv/Kconfig | 2 ++ hw/riscv/virt-acpi-build.c | 20 +++++++++++++++++++ hw/riscv/virt.c | 40 ++++++++++++++++++++++++++++++++++++++ include/hw/riscv/virt.h | 3 +++ 4 files changed, 65 insertions(+)hw/riscv/Kconfig +2
-0@@ -66,8 +66,10 @@ config RISCV_VIRT select VIRTIO_MMIO select FW_CFG_DMA select PLATFORM_BUS + select PXB select ACPI select ACPI_PCI + select ACPI_CXL config SHAKTI_C boolhw/riscv/virt-acpi-build.c +20
-0@@ -29,12 +29,16 @@#include "hw/acpi/aml-build.h" #include "hw/acpi/pci.h" #include "hw/acpi/utils.h" +#include "hw/acpi/cxl.h" #include "hw/intc/riscv_aclint.h" #include "hw/nvram/fw_cfg_acpi.h" #include "hw/pci-host/gpex.h" +#include "hw/pci/pci_bus.h" #include "hw/riscv/virt.h" #include "hw/riscv/numa.h" #include "hw/virtio/virtio-acpi.h" +#include "hw/cxl/cxl.h" +#include "hw/cxl/cxl_host.h" #include "kvm/kvm_riscv.h" #include "migration/vmstate.h" #include "qapi/error.h" @@ -503,6 +507,17 @@ static void build_dsdt(GArray *table_data,acpi_dsdt_add_gpex_host (scope,PCIE_IRQ +VIRT_IRQCHIP_NUM_SOURCES *2 ); } +if (s->cxl_devices_state.is_enabled) { +Aml *cxl_dev =aml_device ("CXLM" ); +aml_append (cxl_dev,aml_name_decl ("_HID" ,aml_string ("ACPI0017" ))); +Aml *method =aml_method ("_STA" ,0 ,AML_NOTSERIALIZED ); +aml_append (method,aml_return (aml_int (0x0B ))); +aml_append (cxl_dev, method); +build_cxl_dsm_method (cxl_dev); + +aml_append (scope, cxl_dev); + } +aml_append (dsdt, scope);/* copy AML table into ACPI tables blob and patch header there */ @@ -910,6 +925,11 @@ static void virt_acpi_build(RISCVVirtState *s, AcpiBuildTables *tables) s->oem_table_id); } +if (s->cxl_devices_state.is_enabled) { +cxl_build_cedt (table_offsets, tables_blob, tables->linker, + s->oem_id, s->oem_table_id, &s->cxl_devices_state); + } +if (ms->numa_state->num_nodes >0 ) {acpi_add_table (table_offsets, tables_blob);build_srat (tables_blob, tables->linker, s);hw/riscv/virt.c +28
-0@@ -55,6 +55,8 @@#include "hw/pci/pci.h" #include "hw/pci-host/gpex.h" #include "hw/display/ramfb.h" +#include "hw/cxl/cxl.h" +#include "hw/cxl/cxl_host.h" #include "hw/acpi/aml-build.h" #include "qapi/qapi-visit-common.h" #include "hw/virtio/virtio-iommu.h" @@ -1259,9 +1261,27 @@ static inline DeviceState *gpex_pcie_init(MemoryRegion *sys_mem, }GPEX_HOST (dev)->gpex_cfg.bus =PCI_HOST_BRIDGE (dev)->bus; + s->pci_bus =PCI_HOST_BRIDGE (dev)->bus;return dev; } +static void create_cxl_host_reg_region (RISCVVirtState *s) +{ +MemoryRegion *sysmem =get_system_memory (); +MemoryRegion *mr = &s->cxl_devices_state.host_mr; +hwaddr base; + +if (!s->cxl_devices_state.is_enabled) { +return ; + } + + base = virt_high_pcie_memmap.base + virt_high_pcie_memmap.size; + base =ROUND_UP (base,64 * KiB); + +memory_region_init (mr,OBJECT (s),"cxl_host_reg" ,64 * KiB *16 ); +memory_region_add_subregion (sysmem, base, mr); +} +static FWCfgState *create_fw_cfg (const MachineState *ms,hwaddr base) {FWCfgState *fw_cfg;@@ -1426,6 +1446,13 @@ static void virt_machine_done(Notifier *notifier, void *data) machine_done);MachineState *machine =MACHINE (s);hwaddr start_addr = s->memmap[VIRT_DRAM ].base; + +cxl_hook_up_pxb_registers (s->pci_bus, &s->cxl_devices_state, + &error_fatal); + +if (s->cxl_devices_state.is_enabled) { +cxl_fmws_link_targets (&error_fatal); + }hwaddr firmware_end_addr;vaddr kernel_start_addr;const char *firmware_name =riscv_default_firmware_name (&s->soc[0 ]);@@ -1663,6 +1690,17 @@ static void virt_machine_init(MachineState *machine)ROUND_UP (virt_high_pcie_memmap.base, virt_high_pcie_memmap.size); } +create_cxl_host_reg_region (s);
hw/riscv/virt.c static void virt_machine_init(MachineState *machine) · line 1703
Quoted 10 lines
hw/riscv/virt.c +10
-0+ +if (s->cxl_devices_state.is_enabled) { +hwaddr cxl_base = virt_high_pcie_memmap.base + + virt_high_pcie_memmap.size; + cxl_base +=memory_region_size (&s->cxl_devices_state.host_mr); + cxl_base =ROUND_UP (cxl_base,256 * MiB); +cxl_fmws_set_memmap (cxl_base,UINT64_MAX ); +cxl_fmws_update_mmio (); + } +
create_cxl_host_reg_region() is doing the same check:
if (!s->cxl_devices_state.is_enabled) {
(...)
Maybe we could squash these update_mmio() lines in the same helper to have everything CXL related in the same place. The helper would need to be renamed to something more appropriate (e.g. cxl_host_state_init() since we're at init time).
Patch LGTM otherwise. Thanks,
Daniel
Quoted 33 lines
hw/riscv/virt.c +2
-0/* register system main memory (actual RAM) */ memory_region_add_subregion (system_memory, s->memmap[VIRT_DRAM ].base, machine->ram);@@ -1769,6 +1807,8 @@ static void virt_machine_instance_init(Object *obj) s->oem_table_id =g_strndup (ACPI_BUILD_APPNAME8 ,8 ); s->acpi =ON_OFF_AUTO_AUTO ; s->iommu_sys =ON_OFF_AUTO_AUTO ; + +cxl_machine_init (obj, &s->cxl_devices_state); }static char *virt_get_aia_guests (Object *obj,Error **errp)include/hw/riscv/virt.h +3
-0@@ -24,6 +24,7 @@#include "hw/core/sysbus.h" #include "hw/block/flash.h" #include "hw/intc/riscv_imsic.h" +#include "hw/cxl/cxl.h" #define VIRT_CPUS_MAX_BITS 9#define VIRT_CPUS_MAX (1 << VIRT_CPUS_MAX_BITS)@@ -64,6 +65,8 @@ struct RISCVVirtState {struct GPEXHost *gpex_host;OnOffAuto iommu_sys;uint16_t pci_iommu_bdf; +CXLState cxl_devices_state; +PCIBus *pci_bus; };enum {
Re: [PATCH v2 3/4] hw/riscv/virt,gpex: Provide 32-bit MMIO window for CXL host bridges
In reply to Chen PeiQuoted 132 lines · Chen Pei
CXL component register BAR (BAR0 on CXL Root Port and Type3 device) and the CXL device register BAR (BAR2 on Type3 device) are declared as 64-bit non-prefetchable memory. Per the PCI-to-PCI Bridge Architecture Specification Rev 1.2 (PCI-SIG, 2003):
- §3.2.5.8 (Memory Base/Limit): the non-prefetchable window covers only 32-bit addresses (AD[31:20]); the Type 1 header defines no upper-32-bit extension for it.
- §3.2.5.9 (Prefetchable Memory Base/Limit): the bottom 4 bits encode 64-bit support (01h), but this applies exclusively to the *prefetchable* window.
- §3.2.5.10 (Prefetchable Base/Limit Upper 32 Bits): optional registers for AD[63:32] of the prefetchable range only.The architecture therefore allows a 64-bit window only when it is also prefetchable; there is no 64-bit non-prefetchable form. PCIe inherits this Type 1 header layout unchanged. Linux thus places 64-bit non-prefetchable BARs in the 32-bit non-prefetchable bridge window, which requires the bridge to own enough address space below 4 GiB.
On RISC-V virt the 32-bit PCIe MMIO range (1 GiB at
0x40000000) is currently consumed entirely by PCI0, so CXL host bridges (ACPI0016) have no non-prefetchable window and Linux fails to assign these BARs. Marking the BARs prefetchable would work around it, but the CXL component registers have read/write side effects and are not prefetchable per the PCIe specification.Reserve the top 256 MiB of the 32-bit MMIO window exclusively for CXL host bridges:
- Shrink PCI0's mmio32 window by 256 MiB invirt.cso that UEFI's PciHostBridgeDxe and the ACPI_CRSfor PCI0 never claim that range
- Store the reserved range in a new gpex_cfg.cxl_mmio32 field
- Ingpex-acpi.c, emit thecxl_mmio32range as the Memory resource in the CXL host bridge_CRSinstead of re-usingbuild_crs()(which returns an empty set when UEFI has not assigned resources yet)
- Reduce the FDT 'ranges' for PCI0 by the same 256 MiB so that UEFI firmware driven by device-tree also respects the reservationSigned-off-by Chen Pei <[email protected]>--- hw/pci-host/gpex-acpi.c | 36 +++++++++++++++++++++-- hw/riscv/virt.c | 58 +++++++++++++++++++++++++++++++------- include/hw/pci-host/gpex.h | 1 + 3 files changed, 83 insertions(+), 12 deletions(-)hw/pci-host/gpex-acpi.c +34
-2@@ -158,9 +158,41 @@ void acpi_dsdt_add_gpex(Aml *scope, struct GPEXConfig *cfg)* Resources defined for PXBs are composed of the following parts: * 1. The resources the pci-bridge/pcie-root-port need. * 2. The resources the devices behind pxb need. +* +* For CXL host bridges on platforms where UEFI (driven by +* FDT 'ranges') does not assign PCI resources for the CXL +* root bridge before ACPI table construction, build_crs() +* would return an empty resource set. When the platform +* has reserved a dedicated MMIO window for CXL host bridges +* (cfg->cxl_mmio32), emit that window as a static _CRS +* instead. The platform is responsible for shrinking PCI0's +* mmio32 window so the two do not overlap. */ - crs =build_crs (PCI_HOST_BRIDGE (BUS (bus)->parent), &crs_range_set, - cfg->pio.base,0 ,0 ,0 ); +if (is_cxl && cfg->cxl_mmio32.size) { +uint64_t cxl_base = cfg->cxl_mmio32.base; +uint64_t cxl_size = cfg->cxl_mmio32.size; + + crs =aml_resource_template (); + +/* 32-bit MMIO range for CXL devices */ +aml_append (crs, +aml_dword_memory (AML_POS_DECODE ,AML_MIN_FIXED , +AML_MAX_FIXED ,AML_NON_CACHEABLE , +AML_READ_WRITE ,0 , + cxl_base, + cxl_base + cxl_size -1 , +0 , cxl_size)); + +/* Bus number range */ +aml_append (crs, +aml_word_bus_number (AML_MIN_FIXED ,AML_MAX_FIXED , +AML_POS_DECODE ,0 , + bus_num, bus_num +15 , +0 ,16 )); + }else { + crs =build_crs (PCI_HOST_BRIDGE (BUS (bus)->parent), + &crs_range_set, cfg->pio.base,0 ,0 ,0 ); + }aml_append (dev,aml_name_decl ("_CRS" , crs));if (is_cxl) {hw/riscv/virt.c +15
-9@@ -113,6 +113,9 @@ static const MemMapEntry virt_memmap[] = {/* PCIe high mmio for RV64, size is fixed but base depends on top of RAM */ #define VIRT64_HIGH_PCIE_MMIO_SIZE (16 * GiB) +/* 32-bit MMIO range carved out of VIRT_PCIE_MMIO for CXL host bridges */ +#define VIRT_CXL_MMIO32_SIZE (256 * MiB) +static MemMapEntry virt_high_pcie_memmap;#define VIRT_FLASH_SECTOR_SIZE (256 * KiB)@@ -890,15 +893,28 @@ static void create_fdt_pcie(RISCVVirtState *s, }qemu_fdt_setprop_sized_cells (ms->fdt, name,"reg" ,2 , s->memmap[VIRT_PCIE_ECAM ].base,2 , s->memmap[VIRT_PCIE_ECAM ].size); -qemu_fdt_setprop_sized_cells (ms->fdt, name,"ranges" , -1 ,FDT_PCI_RANGE_IOPORT ,2 ,0 , -2 , s->memmap[VIRT_PCIE_PIO ].base,2 , s->memmap[VIRT_PCIE_PIO ].size, -1 ,FDT_PCI_RANGE_MMIO , -2 , s->memmap[VIRT_PCIE_MMIO ].base, -2 , s->memmap[VIRT_PCIE_MMIO ].base,2 , s->memmap[VIRT_PCIE_MMIO ].size, -1 ,FDT_PCI_RANGE_MMIO_64BIT , -2 , virt_high_pcie_memmap.base, -2 , virt_high_pcie_memmap.base,2 , virt_high_pcie_memmap.size); + { +/* +* When CXL is enabled, reserve the last 256 MiB of the 32-bit +* MMIO window for CXL host bridges and exclude it from the main +* PCIe host bridge's FDT 'ranges' so UEFI's PciHostBridgeDxe +* does not allocate that range to PCI0. The CXL host bridge +* _CRS declares this range independently. +*/ +hwaddr mmio32_size = s->memmap[VIRT_PCIE_MMIO ].size; +if (s->cxl_devices_state.is_enabled) { + mmio32_size -=VIRT_CXL_MMIO32_SIZE ; + }
hw/riscv/virt.c static void create_fdt_pcie(RISCVVirtState *s, · line 917
Quoted 10 lines
hw/riscv/virt.c +10
-0+qemu_fdt_setprop_sized_cells (ms->fdt, name,"ranges" , +1 ,FDT_PCI_RANGE_IOPORT ,2 ,0 , +2 , s->memmap[VIRT_PCIE_PIO ].base,2 , s->memmap[VIRT_PCIE_PIO ].size, +1 ,FDT_PCI_RANGE_MMIO , +2 , s->memmap[VIRT_PCIE_MMIO ].base, +2 , s->memmap[VIRT_PCIE_MMIO ].base,2 , mmio32_size, +1 ,FDT_PCI_RANGE_MMIO_64BIT , +2 , virt_high_pcie_memmap.base, +2 , virt_high_pcie_memmap.base,2 , virt_high_pcie_memmap.size); + }
We can get rid of the braces - it's just adding an extra identation level while not doing anything special. 'mmio32_size' can be declared at the start of create_fdt_pcie().
Thanks,
Daniel
Quoted 46 lines
hw/riscv/virt.c +23
-1if (virt_is_iommu_sys_enabled (s)) { qemu_fdt_setprop_cells(ms->fdt, name, "iommu-map",@@ -1728,7 +1744,29 @@ static void virt_machine_init(MachineState *machine) qdev_get_gpio_in(virtio_irqchip, VIRTIO_IRQ + i)); } -gpex_pcie_init (system_memory, pcie_irqchip, s); +DeviceState *pcie_dev =gpex_pcie_init (system_memory, pcie_irqchip, s); + +/* +* If CXL is enabled, reserve the last 256 MiB of the 32-bit MMIO +* window for CXL host bridges so the bridge non-prefetchable window +* can hold CXL device BARs (component registers and similar 64-bit +* non-prefetchable BARs that need a < 4 GiB address). +* +* - Shrink PCI0's mmio32 advertised in the ACPI _CRS by the same +* 256 MiB so the two ranges do not overlap (the FDT 'ranges' +* shrink happens in create_fdt_pcie()). +* - Store the reserved range in cxl_mmio32 so gpex-acpi.c can emit +* a correct _CRS for the CXL host bridge (ACPI0016). +*/ +if (s->cxl_devices_state.is_enabled) { +GPEXHost *gpex =GPEX_HOST (pcie_dev); + gpex->gpex_cfg.cxl_mmio32.size =VIRT_CXL_MMIO32_SIZE ; + gpex->gpex_cfg.cxl_mmio32.base = + s->memmap[VIRT_PCIE_MMIO ].base + + s->memmap[VIRT_PCIE_MMIO ].size -VIRT_CXL_MMIO32_SIZE ; +/* Shrink PCI0's advertised 32-bit MMIO window to exclude CXL range */ + gpex->gpex_cfg.mmio32.size -=VIRT_CXL_MMIO32_SIZE ; + }create_platform_bus (s, mmio_irqchip);include/hw/pci-host/gpex.h +1
-0@@ -43,6 +43,7 @@ struct GPEXConfig {MemMapEntry mmio32;MemMapEntry mmio64;MemMapEntry pio; +MemMapEntry cxl_mmio32;int irq;PCIBus *bus;bool pci_native_hotplug;
Hi Daniel,
Quoted 9 lines · Daniel Henrique Barboza
create_cxl_host_reg_region()is doing the same check:if (!s->cxl_devices_state.is_enabled) { (...)Maybe we could squash these
update_mmio()lines in the same helper to have everything CXL related in the same place. The helper would need to be renamed to something more appropriate (e.g.cxl_host_state_init()since we're at init time).
Thanks for the review. You are right, the x86 pc_memory_init() path keeps the CXL host register region creation and the FMW address programming inside a single 'if (is_enabled)' block, and there is no reason for RISC-V to split them. I'll fold the cxl_fmws_set_memmap() and cxl_fmws_update_mmio() calls into create_cxl_host_reg_region(), rename the helper to cxl_host_state_init(), and drop the duplicate guard in virt_machine_init() in v3.
Thanks,
Pei
Re: [PATCH v2 3/4] hw/riscv/virt,gpex: Provide 32-bit MMIO window for CXL host bridges
In reply to Daniel Henrique BarbozaHi Daniel,
Quoted 3 lines · Daniel Henrique Barboza
We can get rid of the braces - it's just adding an extra identation level while not doing anything special. '
mmio32_size' can be declared at the start ofcreate_fdt_pcie().
Makes sense. I'll move mmio32_size to the top of create_fdt_pcie() and remove the extra braces in v3.
Thanks,
Pei
Re: [PATCH v2 2/4] hw/riscv/virt-acpi-build: Add _DEP to ACPI0017 for CXL host bridge dependency
In reply to Chen PeiQuoted 8 lines · Chen Pei
On RISC-V QEMU virt platform with CXL enabled, the probe ordering of
acpi_pci_root(ACPI0016) andcxl_acpi(ACPI0017) is not guaranteed. Ifcxl_acpiprobes beforeacpi_pci_roothas attached the CXL host bridges, the CXL port topology will be incomplete becauseto_cxl_host_bridge()silently skips devices whose PCI root is not yet ready.
Quoted 7 lines
Add a
_DEPobject to the ACPI0017 device in the DSDT, declaring its dependency on the ACPI0016 CXL host bridge devices. This tells the OS to defer ACPI0017 enumeration until all ACPI0016 devices have been attached byacpi_pci_root.This requires a corresponding kernel change to call
acpi_dev_clear_dependencies()inacpi_pci_root_add().
What happens to kernels that don't have this change?
Alistair
Quoted 70 lines
The resulting DSDT fragment (iasl -d output) for a single CXL host bridge at bus
0x01looks like:Device (CXLM) { Name (_HID, "ACPI0017") Name (_DEP, Package (0x01) { \_SB.PC01 }) Method (_STA, 0, NotSerialized) { Return (0x0B) } ... }Signed-off-by Chen Pei <[email protected]>--- hw/riscv/virt-acpi-build.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)hw/riscv/virt-acpi-build.c +32
-1@@ -510,6 +510,38 @@ static void build_dsdt(GArray *table_data,if (s->cxl_devices_state.is_enabled) {Aml *cxl_dev =aml_device ("CXLM" );aml_append (cxl_dev,aml_name_decl ("_HID" ,aml_string ("ACPI0017" ))); + +/* +* Declare a _DEP on every ACPI0016 CXL host bridge so the OS +* defers ACPI0017 enumeration until acpi_pci_root has attached +* the CXL host bridges. Without this, cxl_acpi may probe before +* to_cxl_host_bridge() can resolve the PCI root and the CXL +* port topology comes up empty. +*/ +if (s->pci_bus) { +PCIBus *bus; +uint32_t num_cxl_hbs =0 ; + + QLIST_FOREACH(bus, &s->pci_bus->child, sibling) { +if (pci_bus_is_root (bus) &&pci_bus_is_cxl (bus)) { + num_cxl_hbs++; + } + } + +if (num_cxl_hbs >0 ) { +Aml *dep_pkg =aml_package (num_cxl_hbs); + + QLIST_FOREACH(bus, &s->pci_bus->child, sibling) { +if (pci_bus_is_root (bus) &&pci_bus_is_cxl (bus)) { +aml_append (dep_pkg, +aml_name ("\\_SB.PC%.02X" , +pci_bus_num (bus))); + } + } +aml_append (cxl_dev,aml_name_decl ("_DEP" , dep_pkg)); + } + } +Aml *method =aml_method ("_STA" ,0 ,AML_NOTSERIALIZED );aml_append (method,aml_return (aml_int (0x0B )));aml_append (cxl_dev, method); --2.50.1
Re: [PATCH v2 4/4] tests/qtest: Add RISC-V ACPI bios tables test for CXL
In reply to Chen PeiQuoted 9 lines · Chen Pei
Add
test_acpi_riscv64_virt_tcg_cxl()to verify that enabling CXL on the RISC-V virt machine produces correct ACPI tables, including the ACPI0017 CXLM device with_DEPin the DSDT and the CEDT table.The test boots with cxl=on, one pxb-cxl bus (bus_nr=12), a CXL root port, a cxl-type3 persistent memory device, and a fixed memory window, mirroring the existing x86 q35 CXL test pattern.
Quoted 7 lines
Since pxb-cxl is a root bus, using -cdrom causes QEMU to auto-plug the cdrom drive into pxb-cxl, triggering "Only PCI/PCIe bridges can be plugged into pxb-cxl". The ISO is instead attached explicitly via a virtio-scsi-pci controller on pcie.0, following the same approach as
test_acpi_aarch64_virt_tcg_pxb().Signed-off-by Chen Pei <[email protected]>
Alistair
Quoted 138 lines
--- tests/data/acpi/riscv64/virt/CEDT.cxl | Bin 0 -> 108 bytes tests/data/acpi/riscv64/virt/DSDT.cxl | Bin 0 -> 6212 bytes tests/qtest/bios-tables-test.c | 54 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 tests/data/acpi/riscv64/virt/CEDT.cxl create mode 100644 tests/data/acpi/riscv64/virt/DSDT.cxltests/data/acpi/riscv64/virt/CEDT.cxl new file binary +0
-0tests/data/acpi/riscv64/virt/DSDT.cxl new file binary +0
-0tests/qtest/bios-tables-test.c +54
-1@@ -2194,6 +2194,56 @@ static void test_acpi_riscv64_virt_tcg(void)free_test_data (&data); } +#ifdef CONFIG_POSIX +static void test_acpi_riscv64_virt_tcg_cxl (void ) +{ +gchar *tmp_path =g_dir_make_tmp ("qemu-test-cxl.XXXXXX" ,NULL ); +gchar *params; + +test_data data = { + .machine ="virt" , + .arch ="riscv64" , + .tcg_only = true, + .uefi_fl1 ="pc-bios/edk2-riscv-code.fd" , + .uefi_fl2 ="pc-bios/edk2-riscv-vars.fd" , + .ram_start =0x80000000ULL , + .scan_len =128ULL * MiB, + .variant =".cxl" , + }; + +/* +* While using -cdrom, the cdrom would auto-plug into pxb-cxl because +* its bus is also a root bus, triggering "Only PCI/PCIe bridges can be +* plugged into pxb-cxl". Attach the ISO explicitly to a scsi controller +* on pcie.0 instead, following the same pattern as +* test_acpi_aarch64_virt_tcg_pxb(). +*/ + params =g_strdup_printf ("-cpu rva22s64" +" -machine cxl=on" +" -device pcie-root-port,chassis=1,id=pci.1,bus=pcie.0" +" -device virtio-scsi-pci,id=scsi0,bus=pci.1" +" -drive file=tests/data/uefi-boot-images/" +"bios-tables-test.riscv64.iso.qcow2," +"if=none,media=cdrom,id=drive-scsi0-0-0-1,readonly=on" +" -device scsi-cd,bus=scsi0.0,scsi-id=0," +"drive=drive-scsi0-0-0-1,id=scsi0-0-0-1,bootindex=1" +" -object memory-backend-file,id=cxl-mem1,mem-path=%s,size=256M" +" -object memory-backend-file,id=lsa1,mem-path=%s,size=256M" +" -device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1" +" -device cxl-rp,port=0,bus=cxl.1,id=rp1,chassis=0,slot=2" +" -device cxl-type3,bus=rp1,persistent-memdev=cxl-mem1,lsa=lsa1" +" -M cxl-fmw.0.targets.0=cxl.1,cxl-fmw.0.size=4G," +"cxl-fmw.0.interleave-granularity=8k" , + tmp_path, tmp_path); +test_acpi_one (params, &data); + +g_free (params); +g_assert (g_rmdir (tmp_path) ==0 ); +g_free (tmp_path); +free_test_data (&data); +} +#endif /* CONFIG_POSIX */ +static void test_acpi_aarch64_virt_tcg (void ) {test_data data = {@@ -2888,6 +2938,10 @@ int main(int argc, char *argv[]) test_acpi_riscv64_virt_tcg_numamem);qtest_add_func ("acpi/virt/acpispcr" , test_acpi_riscv64_virt_tcg_acpi_spcr); +#ifdef CONFIG_POSIX +qtest_add_func ("acpi/virt/cxl" , + test_acpi_riscv64_virt_tcg_cxl); +#endif } }else if (strcmp (arch,"loongarch64" ) ==0 ) {if (has_tcg &&qtest_has_machine ("virt" )) { --2.50.1
Re: [PATCH v2 2/4] hw/riscv/virt-acpi-build: Add _DEP to ACPI0017 for CXL host bridge dependency
In reply to Alistair FrancisHi Alistair,
Quoted 1 line · Alistair Francis
What happens to kernels that don't have this change?
Good question. The behavior depends on how old the kernel is:
- Kernels that support _DEP processing but lack the acpi_dev_clear_dependencies() call in acpi_pci_root_add(): the ACPI0017 (CXLM) device will remain deferred indefinitely because its declared dependency on ACPI0016 is never cleared. CXL memory would be unusable on such kernels with this QEMU.
- Very old kernels that ignore _DEP entirely: the behavior is the same as v1 — the probe-ordering race between acpi_pci_root and cxl_acpi remains, but nothing is worse than before.
The kernel-side fix has already merged:
https://lore.kernel.org/linux-pci/[email protected]/
So this is effectively a "new QEMU requires a sufficiently new kernel for CXL to work" situation, similar to other QEMU features that depend on kernel support.
Thanks,
Pei
Re: [PATCH v2 2/4] hw/riscv/virt-acpi-build: Add _DEP to ACPI0017 for CXL host bridge dependency
Quoted 18 lines · Chen Pei
Hi Alistair,
Quoted 1 line · Alistair Francis
What happens to kernels that don't have this change?
Good question. The behavior depends on how old the kernel is:
- Kernels that support
_DEPprocessing but lack theacpi_dev_clear_dependencies()call inacpi_pci_root_add(): the ACPI0017 (CXLM) device will remain deferred indefinitely because its declared dependency on ACPI0016 is never cleared. CXL memory would be unusable on such kernels with this QEMU.- Very old kernels that ignore
_DEPentirely: the behavior is the same as v1 — the probe-ordering race betweenacpi_pci_rootandcxl_acpiremains, but nothing is worse than before.
Quoted 6 lines
The kernel-side fix has already merged:
https://lore.kernel.org/linux-pci/[email protected]/So this is effectively a "new QEMU requires a sufficiently new kernel for CXL to work" situation, similar to other QEMU features that depend on kernel support.
That's fine, we can say you need a new kernel for this new feature to work, as long as it doesn't break existing PCIe for old kernels
Alistair
Quoted 3 lines
Thanks,
Pei
Re: [PATCH v2 3/4] hw/riscv/virt,gpex: Provide 32-bit MMIO window for CXL host bridges
In reply to Chen PeiHi Chen Pei,
hw/pci-host/gpex-acpi.c void acpi_dsdt_add_gpex(Aml *scope, struct GPEXConfig *cfg) · line 184
Quoted 11 lines · Chen Pei
hw/pci-host/gpex-acpi.c +11
-0+if (is_cxl && cfg->cxl_mmio32.size) { +uint64_t cxl_base = cfg->cxl_mmio32.base; +uint64_t cxl_size = cfg->cxl_mmio32.size; + crs =aml_resource_template (); +/* 32-bit MMIO range for CXL devices */ +aml_append (crs, +aml_dword_memory (AML_POS_DECODE ,AML_MIN_FIXED , +AML_MAX_FIXED ,AML_NON_CACHEABLE , +AML_READ_WRITE ,0 , + cxl_base, cxl_base + cxl_size -1 , +0 , cxl_size));
cfg->cxl_mmio32 is a single range on the main GPEX host, but this block runs once per CXL host bridge in the QLIST_FOREACH(bus, &bus->child) loop, so with more than one pxb-cxl every ACPI0016 gets a _CRS that declares the *same* [cxl_base, cxl_base + 256 MiB) window.
I realise build_crs() returns nothing usable for these bridges at ACPI-build time (the BARs aren't assigned yet, per your commit message), which is why the static path exists -- but it hands that one shared cfg->cxl_mmio32 to every CXL bridge.
I checked the generated DSDT for a two-pxb-cxl riscv64 config (cxl.1 at bus_nr=12, cxl.2 at bus_nr=200): both ACPI0016 host bridges (PC0C and PCC8) come up with the identical producer window
DWordMemory (ResourceProducer, PosDecode, MinFixed, MaxFixed, ...)
0x70000000, // Range Minimum
0x7FFFFFFF, // Range Maximum
0x10000000, // Lengthwhile PCI0 is correctly shrunk to 0x40000000-0x6FFFFFFF. That 256 MiB window is the only memory range either ACPI0016 advertises, so every BAR behind the two bridges has to come from it; and since two host bridges can't both decode the same range, at most one of them can actually claim it. Multiple pxb-cxl is a supported topology in QEMU generally. Partitioning the window per bridge may be one way out, but whether that's the right model is your call -- is supporting more than one pxb-cxl in scope for this series, or is a single CXL host bridge the intended target for now? If the latter, a note to that effect in the commit message would help.
One smaller thing about the hand-built template: the bus range is hard-coded to [bus_num, bus_num + 15], so with cxl.1 at bus_nr=12 and cxl.2 at bus_nr=16 the two _CRS come out as [0x0C,0x1B] and [0x10,0x1F], which overlap on buses 0x10-0x1B. The real width depends on how many buses the pxb-cxl spans, which isn't known at ACPI-build time (pxb-cxl only carries a bus_nr). build_crs() is no drop-in here either: with the static path disabled I saw the range collapse to a single bus [bus_num, bus_num], since no subordinate buses are enumerated yet -- so it under-sizes rather than over-sizes.
Both really trace back to the same spot: the static branch synthesises the bridge's window and bus range from machine-global constants (cfg->cxl_mmio32 and the fixed +15) rather than anything per-bridge, so neither scales past one pxb-cxl. Going static is fair enough given build_crs() has nothing to offer at build time -- as above, it supplies neither the window nor a usable bus range -- so the real question is just where the per-bridge window size and bus span should come from.
Many thanks,
Junjie
Re: [PATCH v2 3/4] hw/riscv/virt,gpex: Provide 32-bit MMIO window for CXL host bridges
Hi Junjie,
Thanks for the thorough review.
Quoted 4 lines
cfg->cxl_mmio32is a single range on the main GPEX host, but this block runs once per CXL host bridge in theQLIST_FOREACH(bus, &bus->child)loop, so with more than one pxb-cxl every ACPI0016 gets a_CRSthat declares the *same* [cxl_base,cxl_base+ 256 MiB) window.
You're right. The static path synthesises the bridge window from the machine-global cfg->cxl_mmio32 rather than per-bridge state, so it does not scale past one pxb-cxl.
Quoted 3 lines
is supporting more than one pxb-cxl in scope for this series, or is a single CXL host bridge the intended target for now? If the latter, a note to that effect in the commit message would help.
For this series the intended target is a single CXL host bridge, which matches the bios-tables test case (one pxb-cxl at bus_nr=12). I'll add a note to the commit message in v3.
Quoted 4 lines
One smaller thing about the hand-built template: the bus range is hard-coded to [
bus_num,bus_num+ 15], so with cxl.1 at bus_nr=12 and cxl.2 at bus_nr=16 the two_CRScome out as [0x0C,0x1B] and [0x10,0x1F], which overlap on buses 0x10-0x1B.
This is a related symptom of the same root cause: pxb-cxl only carries a bus_nr and the actual subordinate bus count is not known at ACPI-build time, so neither build_crs() nor a static constant can produce a correct span for the multi-bridge case. The +15 heuristic is borrowed from the ARM virt machine's gpex handling where a single pxb is the norm.
Quoted 4 lines
Both really trace back to the same spot: the static branch synthesises the bridge's window and bus range from machine-global constants (
cfg->cxl_mmio32and the fixed +15) rather than anything per-bridge, so neither scales past one pxb-cxl.
Agreed. I'll add a TODO comment at the static branch in gpex-acpi.c to flag this for future work, and document the single-bridge limitation in the commit message as noted above.
Thanks,
Pei
Re: [PATCH v2 3/4] hw/riscv/virt,gpex: Provide 32-bit MMIO window for CXL host bridges
In reply to Chen PeiQuoted 50 lines · On Thu, 18 Jun 2026 17:38:25 +0800 Chen Pei <[email protected]> wrote:
CXL component register BAR (BAR0 on CXL Root Port and Type3 device) and the CXL device register BAR (BAR2 on Type3 device) are declared as 64-bit non-prefetchable memory. Per the PCI-to-PCI Bridge Architecture Specification Rev 1.2 (PCI-SIG, 2003):
- §3.2.5.8 (Memory Base/Limit): the non-prefetchable window covers only 32-bit addresses (AD[31:20]); the Type 1 header defines no upper-32-bit extension for it.
- §3.2.5.9 (Prefetchable Memory Base/Limit): the bottom 4 bits encode 64-bit support (01h), but this applies exclusively to the *prefetchable* window.
- §3.2.5.10 (Prefetchable Base/Limit Upper 32 Bits): optional registers for AD[63:32] of the prefetchable range only.The architecture therefore allows a 64-bit window only when it is also prefetchable; there is no 64-bit non-prefetchable form. PCIe inherits this Type 1 header layout unchanged. Linux thus places 64-bit non-prefetchable BARs in the 32-bit non-prefetchable bridge window, which requires the bridge to own enough address space below 4 GiB.
On RISC-V virt the 32-bit PCIe MMIO range (1 GiB at
0x40000000) is currently consumed entirely by PCI0, so CXL host bridges (ACPI0016) have no non-prefetchable window and Linux fails to assign these BARs. Marking the BARs prefetchable would work around it, but the CXL component registers have read/write side effects and are not prefetchable per the PCIe specification.Reserve the top 256 MiB of the 32-bit MMIO window exclusively for CXL host bridges:
- Shrink PCI0's mmio32 window by 256 MiB invirt.cso that UEFI's PciHostBridgeDxe and the ACPI_CRSfor PCI0 never claim that range
- Store the reserved range in a new gpex_cfg.cxl_mmio32 field
- Ingpex-acpi.c, emit thecxl_mmio32range as the Memory resource in the CXL host bridge_CRSinstead of re-usingbuild_crs()(which returns an empty set when UEFI has not assigned resources yet)
- Reduce the FDT 'ranges' for PCI0 by the same 256 MiB so that UEFI firmware driven by device-tree also respects the reservationSigned-off-by Chen Pei <[email protected]>--- hw/pci-host/gpex-acpi.c | 36 +++++++++++++++++++++-- hw/riscv/virt.c | 58 +++++++++++++++++++++++++++++++------- include/hw/pci-host/gpex.h | 1 + 3 files changed, 83 insertions(+), 12 deletions(-)hw/pci-host/gpex-acpi.c +0
-0
hw/pci-host/gpex-acpi.c void acpi_dsdt_add_gpex(Aml *scope, struct GPEXConfig *cfg) · line 164
Quoted 7 lines
hw/pci-host/gpex-acpi.c +4
-0@@ -158,9 +158,41 @@ void acpi_dsdt_add_gpex(Aml *scope, struct GPEXConfig *cfg)* Resources defined for PXBs are composed of the following parts: * 1. The resources the pci-bridge/pcie-root-port need. * 2. The resources the devices behind pxb need. +* +* For CXL host bridges on platforms where UEFI (driven by +* FDT 'ranges') does not assign PCI resources for the CXL +* root bridge before ACPI table construction, build_crs()
I kind of dislike (basically riscv specific hack) in generic code. well, correct way to tackle this is
1: fix UEFI to perform required initialization.
hw/pci-host/gpex-acpi.c void acpi_dsdt_add_gpex(Aml *scope, struct GPEXConfig *cfg) · line 169
Quoted 5 lines
hw/pci-host/gpex-acpi.c +5
-0+* would return an empty resource set. When the platform +* has reserved a dedicated MMIO window for CXL host bridges +* (cfg->cxl_mmio32), emit that window as a static _CRS +* instead. The platform is responsible for shrinking PCI0's +* mmio32 window so the two do not overlap.
if you have to put a hack into QEMU, make it work with current build_crs() and localize 'proper simulated initialization' in riscv codebase (i.e. do not spread it to common codebase). But before doing it see (#1 note).
Quoted 126 lines
hw/pci-host/gpex-acpi.c +25
-2*/ - crs =build_crs (PCI_HOST_BRIDGE (BUS (bus)->parent), &crs_range_set, - cfg->pio.base,0 ,0 ,0 ); +if (is_cxl && cfg->cxl_mmio32.size) { +uint64_t cxl_base = cfg->cxl_mmio32.base; +uint64_t cxl_size = cfg->cxl_mmio32.size; + + crs =aml_resource_template (); + +/* 32-bit MMIO range for CXL devices */ +aml_append (crs, +aml_dword_memory (AML_POS_DECODE ,AML_MIN_FIXED , +AML_MAX_FIXED ,AML_NON_CACHEABLE , +AML_READ_WRITE ,0 , + cxl_base, + cxl_base + cxl_size -1 , +0 , cxl_size)); + +/* Bus number range */ +aml_append (crs, +aml_word_bus_number (AML_MIN_FIXED ,AML_MAX_FIXED , +AML_POS_DECODE ,0 , + bus_num, bus_num +15 , +0 ,16 )); + }else { + crs =build_crs (PCI_HOST_BRIDGE (BUS (bus)->parent), + &crs_range_set, cfg->pio.base,0 ,0 ,0 ); + }aml_append (dev,aml_name_decl ("_CRS" , crs));if (is_cxl) {hw/riscv/virt.c +48
-10@@ -113,6 +113,9 @@ static const MemMapEntry virt_memmap[] = {/* PCIe high mmio for RV64, size is fixed but base depends on top of RAM */ #define VIRT64_HIGH_PCIE_MMIO_SIZE (16 * GiB) +/* 32-bit MMIO range carved out of VIRT_PCIE_MMIO for CXL host bridges */ +#define VIRT_CXL_MMIO32_SIZE (256 * MiB) +static MemMapEntry virt_high_pcie_memmap;#define VIRT_FLASH_SECTOR_SIZE (256 * KiB)@@ -890,15 +893,28 @@ static void create_fdt_pcie(RISCVVirtState *s, }qemu_fdt_setprop_sized_cells (ms->fdt, name,"reg" ,2 , s->memmap[VIRT_PCIE_ECAM ].base,2 , s->memmap[VIRT_PCIE_ECAM ].size); -qemu_fdt_setprop_sized_cells (ms->fdt, name,"ranges" , -1 ,FDT_PCI_RANGE_IOPORT ,2 ,0 , -2 , s->memmap[VIRT_PCIE_PIO ].base,2 , s->memmap[VIRT_PCIE_PIO ].size, -1 ,FDT_PCI_RANGE_MMIO , -2 , s->memmap[VIRT_PCIE_MMIO ].base, -2 , s->memmap[VIRT_PCIE_MMIO ].base,2 , s->memmap[VIRT_PCIE_MMIO ].size, -1 ,FDT_PCI_RANGE_MMIO_64BIT , -2 , virt_high_pcie_memmap.base, -2 , virt_high_pcie_memmap.base,2 , virt_high_pcie_memmap.size); + { +/* +* When CXL is enabled, reserve the last 256 MiB of the 32-bit +* MMIO window for CXL host bridges and exclude it from the main +* PCIe host bridge's FDT 'ranges' so UEFI's PciHostBridgeDxe +* does not allocate that range to PCI0. The CXL host bridge +* _CRS declares this range independently. +*/ +hwaddr mmio32_size = s->memmap[VIRT_PCIE_MMIO ].size; +if (s->cxl_devices_state.is_enabled) { + mmio32_size -=VIRT_CXL_MMIO32_SIZE ; + } +qemu_fdt_setprop_sized_cells (ms->fdt, name,"ranges" , +1 ,FDT_PCI_RANGE_IOPORT ,2 ,0 , +2 , s->memmap[VIRT_PCIE_PIO ].base,2 , s->memmap[VIRT_PCIE_PIO ].size, +1 ,FDT_PCI_RANGE_MMIO , +2 , s->memmap[VIRT_PCIE_MMIO ].base, +2 , s->memmap[VIRT_PCIE_MMIO ].base,2 , mmio32_size, +1 ,FDT_PCI_RANGE_MMIO_64BIT , +2 , virt_high_pcie_memmap.base, +2 , virt_high_pcie_memmap.base,2 , virt_high_pcie_memmap.size); + }if (virt_is_iommu_sys_enabled (s)) { qemu_fdt_setprop_cells(ms->fdt, name, "iommu-map",@@ -1728,7 +1744,29 @@ static void virt_machine_init(MachineState *machine) qdev_get_gpio_in(virtio_irqchip, VIRTIO_IRQ + i)); } -gpex_pcie_init (system_memory, pcie_irqchip, s); +DeviceState *pcie_dev =gpex_pcie_init (system_memory, pcie_irqchip, s); + +/* +* If CXL is enabled, reserve the last 256 MiB of the 32-bit MMIO +* window for CXL host bridges so the bridge non-prefetchable window +* can hold CXL device BARs (component registers and similar 64-bit +* non-prefetchable BARs that need a < 4 GiB address). +* +* - Shrink PCI0's mmio32 advertised in the ACPI _CRS by the same +* 256 MiB so the two ranges do not overlap (the FDT 'ranges' +* shrink happens in create_fdt_pcie()). +* - Store the reserved range in cxl_mmio32 so gpex-acpi.c can emit +* a correct _CRS for the CXL host bridge (ACPI0016). +*/ +if (s->cxl_devices_state.is_enabled) { +GPEXHost *gpex =GPEX_HOST (pcie_dev); + gpex->gpex_cfg.cxl_mmio32.size =VIRT_CXL_MMIO32_SIZE ; + gpex->gpex_cfg.cxl_mmio32.base = + s->memmap[VIRT_PCIE_MMIO ].base + + s->memmap[VIRT_PCIE_MMIO ].size -VIRT_CXL_MMIO32_SIZE ; +/* Shrink PCI0's advertised 32-bit MMIO window to exclude CXL range */ + gpex->gpex_cfg.mmio32.size -=VIRT_CXL_MMIO32_SIZE ; + }create_platform_bus (s, mmio_irqchip);include/hw/pci-host/gpex.h +1
-0@@ -43,6 +43,7 @@ struct GPEXConfig {MemMapEntry mmio32;MemMapEntry mmio64;MemMapEntry pio; +MemMapEntry cxl_mmio32;int irq;PCIBus *bus;bool pci_native_hotplug;
Re: [PATCH v2 3/4] hw/riscv/virt,gpex: Provide 32-bit MMIO window for CXL host bridges
Hi Igor,
Thanks for the review.
Quoted 3 lines · On Mon, 27 Jul 2026 11:06:15 +0200 Igor Mammedov <[email protected]> wrote:
I kind of dislike (basically riscv specific hack) in generic code. well, correct way to tackle this is
1: fix UEFI to perform required initialization.
I agree this is not a clean solution, and I acknowledge the concern about putting riscv-specific behavior into the shared gpex code.
I did try the "fix UEFI" direction first. With the current EDK2 (edk2-stable202602, the same FdtPciHostBridgeLib/PciBusDxe used by both aarch64 and riscv virt), I booted a riscv virt machine with pxb-cxl and dumped the ECAM config space after the firmware finished enumeration:
- pxb-cxl (1b36:000b) on bus 0: class 0x0600 (host bridge),
header type 0, memory/prefetchable window registers all zero;
- cxl-rp on bus 12: memory window base > limit (empty, reset value);
- cxl-type3 on bus 13: entire config space reads 0xffffffff, i.e. the firmware never enumerated behind the CXL host bridge.
The root cause is that PciBusDxe only recurses into devices for which IS_PCI_BRIDGE() is true (header layout 1), while pxb-cxl presents header type 0 and class 0x0600, so the firmware neither assigns it a secondary bus number nor any MMIO window. FdtPciHostBridgeLib likewise only consumes the single "pci-host-ecam-generic" node describing PCI0 and has no knowledge of the expander bridge. As a result build_crs() genuinely returns an empty set on riscv today - this is not a timing artifact.
Quoted 3 lines
if you have to put a hack into QEMU, make it work with current
build_crs()and localize 'proper simulated initialization' in riscv codebase (i.e. do not spread it to common codebase). But before doing it see (#1 not=
e).
Understood. Since #1 (a UEFI that performs the initialization) is not available on riscv at the moment, the static window is currently the only path that makes CXL host bridge _CRS usable here.
For what it is worth, this is consistent with how CXL is being handled on the ARM side. The sbsa-ref enablement (Yuquan Wang, RFC up to v7, "hw/arm/sbsa-ref: Support CXL Host Bridge & CFMW" [1], plus the matching edk2-platforms RFC [2]) deliberately carves out exclusive static MMIO32/MMIO64 and bus-number ranges for the CXL host bridge and builds static ACPI tables, explicitly noting that it "doesn't need to communicate CXL contents via DT to edk2". Jonathan Cameron's arm/virt approach instead relies on EDK2 performing the enumeration and QEMU reading the result back via build_crs(), but that depends on a recent EDK2 with expander-bridge support that we do not have on riscv yet; his earlier DT-binding RFC for expander bridges [3] also notes that on the DT path QEMU has to allocate the windows itself.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://op-lists.linaro.org/archives/list/[email protected]/thread/EPPN4R6KLGKYLWPOZUBWJG5HCXAAO3SQ/
[3] https://lore.kernel.org/all/[email protected]/
Given that, my tentative plan for v3 is to keep the static-window approach for now but address your objection by localizing it: drop the cxl_mmio32 field and the is_cxl branch from the common gpex code, and instead simulate the initialization in riscv virt code so that the existing build_crs() path produces the correct _CRS (i.e. have riscv pre-populate the bridge window registers / range set the way a firmware would), keeping the common codebase untouched.
Before I rework it that way, I would appreciate any suggestions you may have on this approach.
Thanks,
Pei