From: Manish Honap <[email protected]> fmw-index, 0 by default, selects the fixed memory window that maps the device memory. The window must hold the whole volatile backend, allow device-coherent memory, and have the device's pxb-cxl as its only target. The backend size must be a nonzero multiple of 256 MiB.
The range is registered with the pxb-cxl at realize, one cxl-type2 per pxb-cxl. The pxb-cxl commits its decoder for the range only at reset, so a hotplugged device would find it uncommitted, and an unplugged one could not be plugged back. The device is therefore not hotpluggable. AI-used-for: code (prototype) Signed-off-by: Manish Honap <[email protected]> --- hw/mem/cxl_type2.c | 78 +++++++++++++++++++++++++ hw/pci-bridge/pci_expander_bridge_cxl.c | 5 ++ include/hw/cxl/cxl_device.h | 4 ++ include/hw/pci/pci_bridge.h | 1 + 4 files changed, 88 insertions(+) diff --git a/hw/mem/cxl_type2.c b/hw/mem/cxl_type2.c index ab89d332dc..86fcc1a94a 100644 --- a/hw/mem/cxl_type2.c +++ b/hw/mem/cxl_type2.c @@ -7,9 +7,11 @@ */ #include "qemu/osdep.h" +#include "qemu/units.h" #include "qapi/error.h" #include "hw/core/qdev-properties.h" #include "hw/cxl/cxl.h" +#include "hw/cxl/cxl_host.h" #include "hw/pci/pci.h" #include "hw/pci/pci_bridge.h" #include "hw/pci/pcie_port.h" @@ -30,6 +32,7 @@ static void clean_memory(PCIDevice *pci_dev) */ static bool cxl_type2_check_topology(PCIDevice *pci_dev, Error **errp) { + CXLType2Dev *ct2d = CXL_TYPE2(pci_dev); PCIDevice *rp = pci_get_bus(pci_dev)->parent_dev; PCIDevice *host = rp ? pci_get_bus(rp)->parent_dev : NULL; @@ -44,9 +47,59 @@ static bool cxl_type2_check_topology(PCIDevice *pci_dev, Error **errp) error_setg(errp, "cxl-type2 must be at slot 0, function 0"); return false; } + + ct2d->pxb = PXB_CXL_DEV(host); + ct2d->root_port = PCIE_PORT(rp)->port; + return true; +} + +/* fw->target_hbs is set only once machine creation is done */ +static bool cxl_type2_resolve_fmw(CXLType2Dev *ct2d, Error **errp) +{ + MemoryRegion *mr = host_memory_backend_get_memory(ct2d->hostvmem); + GSList *list = cxl_fmws_get_all_sorted(); + CXLFixedWindow *fw = NULL; + unsigned int matches = 0; + bool ambiguous = false; + Object *target; + + for (GSList *iter = list; iter; iter = iter->next) { + if (CXL_FMW(iter->data)->index == ct2d->fmw_index) { + fw = CXL_FMW(iter->data); + matches++; + } + } + g_slist_free(list); + + if (matches != 1) { + error_setg(errp, "cxl-type2 fmw-index %u matched %u CFMWS windows", + ct2d->fmw_index, matches); + return false; + } + if (fw->num_targets != 1) { + error_setg(errp, "cxl-type2 requires a single-target CFMWS"); + return false; + } + if (!(fw->restrictions & CXL_FMW_DEVICE_COHERENT)) { + error_setg(errp, "cxl-type2 CFMWS must allow device-coherent memory"); + return false; + } + if (memory_region_size(mr) > fw->size) { + error_setg(errp, "cxl-type2 volatile-memdev does not fit in its CFMWS"); + return false; + } + target = object_resolve_path_type(fw->targets[0], TYPE_PXB_CXL_DEV, + &ambiguous); + if (!target || ambiguous || target != OBJECT(ct2d->pxb)) { + error_setg(errp, "cxl-type2 CFMWS does not target its pxb-cxl"); + return false; + } + + ct2d->fmw = fw; return true; } + static bool setup_memory(PCIDevice *pci_dev, Error **errp) { CXLType2Dev *ct2d = CXL_TYPE2(pci_dev); @@ -66,6 +119,12 @@ static bool setup_memory(PCIDevice *pci_dev, Error **errp) object_get_canonical_path_component(OBJECT(ct2d->hostvmem))); return false; } + if (!memory_region_size(vmr) || + !QEMU_IS_ALIGNED(memory_region_size(vmr), 256 * MiB)) { + error_setg(errp, "volatile-memdev size must be a nonzero multiple of " + "256 MiB"); + return false; + } memory_region_set_nonvolatile(vmr, false); memory_region_set_enabled(vmr, true); host_memory_backend_set_mapped(ct2d->hostvmem, true); @@ -215,18 +274,34 @@ static void setup_cxl_regs(PCIDevice *pci_dev) static void cxl_type2_realize(PCIDevice *pci_dev, Error **errp) { + CXLType2Dev *ct2d = CXL_TYPE2(pci_dev); + uint64_t size; + if (!cxl_type2_check_topology(pci_dev, errp) || !setup_memory(pci_dev, errp)) { return; } + if (!cxl_type2_resolve_fmw(ct2d, errp)) { + clean_memory(pci_dev); + return; + } + size = memory_region_size(host_memory_backend_get_memory(ct2d->hostvmem)); pcie_endpoint_cap_init(pci_dev, 0x80); setup_cxl_regs(pci_dev); + + if (!pxb_cxl_register_firmware_hdm(ct2d->pxb, ct2d->fmw->base, size, + ct2d->root_port, errp)) { + clean_memory(pci_dev); + } } static void cxl_type2_exit(PCIDevice *pci_dev) { + CXLType2Dev *ct2d = CXL_TYPE2(pci_dev); + + pxb_cxl_unregister_firmware_hdm(ct2d->pxb); cxl_fmws_direct_passthrough_remove_all(pci_dev); clean_memory(pci_dev); } @@ -249,6 +324,7 @@ static void cxl_type2_reset(DeviceState *dev) static const Property cxl_type2_props[] = { DEFINE_PROP_LINK("volatile-memdev", CXLType2Dev, hostvmem, TYPE_MEMORY_BACKEND, HostMemoryBackend *), + DEFINE_PROP_UINT32("fmw-index", CXLType2Dev, fmw_index, 0), }; static const VMStateDescription cxl_type2_vmsd = { @@ -271,6 +347,8 @@ static void cxl_type2_class_init(ObjectClass *oc, const void *data) pc->revision = 1; set_bit(DEVICE_CATEGORY_MISC, dc->categories); + /* pxb-cxl commits its decoder for the device only at reset */ + dc->hotpluggable = false; dc->desc = "CXL Type-2 device"; dc->vmsd = &cxl_type2_vmsd; device_class_set_legacy_reset(dc, cxl_type2_reset); diff --git a/hw/pci-bridge/pci_expander_bridge_cxl.c b/hw/pci-bridge/pci_expander_bridge_cxl.c index 01fd479a9e..9b0444ce53 100644 --- a/hw/pci-bridge/pci_expander_bridge_cxl.c +++ b/hw/pci-bridge/pci_expander_bridge_cxl.c @@ -119,6 +119,11 @@ bool pxb_cxl_register_firmware_hdm(PXBCXLDev *pxb, uint64_t base, target, errp); } +void pxb_cxl_unregister_firmware_hdm(PXBCXLDev *pxb) +{ + pxb->firmware_hdm = (CXLFirmwareHDM) { 0 }; +} + static void pxb_cxl_dev_reset(DeviceState *dev) { PXBCXLDev *pxb = PXB_CXL_DEV(dev); diff --git a/include/hw/cxl/cxl_device.h b/include/hw/cxl/cxl_device.h index 22ca2f5978..b4a33de6e1 100644 --- a/include/hw/cxl/cxl_device.h +++ b/include/hw/cxl/cxl_device.h @@ -836,8 +836,12 @@ struct CXLType2Dev { /* Properties */ HostMemoryBackend *hostvmem; + uint32_t fmw_index; /* State */ + struct PXBCXLDev *pxb; + uint8_t root_port; + CXLFixedWindow *fmw; MemoryRegion direct_mr[CXL_HDM_DECODER_COUNT]; CXLFixedWindow *direct_mr_fw[CXL_HDM_DECODER_COUNT]; AddressSpace hostvmem_as; diff --git a/include/hw/pci/pci_bridge.h b/include/hw/pci/pci_bridge.h index d0227a4c38..106a3109c8 100644 --- a/include/hw/pci/pci_bridge.h +++ b/include/hw/pci/pci_bridge.h @@ -126,6 +126,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(PXBCXLDev, PXB_CXL_DEV) bool pxb_cxl_register_firmware_hdm(PXBCXLDev *pxb, uint64_t base, uint64_t size, uint8_t target, Error **errp); +void pxb_cxl_unregister_firmware_hdm(PXBCXLDev *pxb); int pci_bridge_ssvid_init(PCIDevice *dev, uint8_t offset, uint16_t svid, uint16_t ssid, -- 2.53.0
