From: Zhi Wang <[email protected]> Let guest accesses to a CXL fixed memory window reach the volatile memory of a CXL type-2 device. The device decodes the host address with its HDM decoder and accesses its memory backend at the resulting DPA.
A committed, non-interleaved decoder also maps the memory directly into the window, as for cxl-type3. Co-developed-by: Ira Weiny <[email protected]> Signed-off-by: Ira Weiny <[email protected]> Signed-off-by: Zhi Wang <[email protected]> [ clg: rebased on upstream ] Signed-off-by: Cédric Le Goater <[email protected]> [ mhonap: - third part of the original patch, renamed to cxl-type2 - add the device to cxl_mem_device() for the direct mapping, and remove its mappings on reset and exit - stubs for builds without the device ] AI-used-for: code (prototype) Signed-off-by: Manish Honap <[email protected]> --- MAINTAINERS | 2 +- hw/cxl/cxl-host.c | 20 +++++++++++++++++++ hw/mem/cxl_type2.c | 39 +++++++++++++++++++++++++++++++++++++ hw/mem/cxl_type2_stubs.c | 20 +++++++++++++++++++ hw/mem/meson.build | 1 + include/hw/cxl/cxl_device.h | 7 +++++++ 6 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 hw/mem/cxl_type2_stubs.c diff --git a/MAINTAINERS b/MAINTAINERS index 57a78bc6a9..fba3e29ee1 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3399,7 +3399,7 @@ R: Anisa Su <[email protected]> L: [email protected] S: Supported F: hw/cxl/ -F: hw/mem/cxl_type2.c +F: hw/mem/cxl_type2*.c F: hw/mem/cxl_type3.c F: include/hw/cxl/ F: qapi/cxl.json diff --git a/hw/cxl/cxl-host.c b/hw/cxl/cxl-host.c index d9c7599679..4bbbcda14d 100644 --- a/hw/cxl/cxl-host.c +++ b/hw/cxl/cxl-host.c @@ -223,6 +223,20 @@ static bool cxl_mem_device(Object *obj, CXLMemDevice *md) { CXLType3Dev *ct3d; + if (object_dynamic_cast(obj, TYPE_CXL_TYPE2)) { + CXLType2Dev *ct2d = CXL_TYPE2(obj); + + if (md) { + *md = (CXLMemDevice) { + .pdev = PCI_DEVICE(ct2d), + .cstate = &ct2d->cxl_cstate, + .hostvmem = ct2d->hostvmem, + .direct_mr = ct2d->direct_mr, + .direct_mr_fw = ct2d->direct_mr_fw, + }; + } + return true; + } if (!object_dynamic_cast(obj, TYPE_CXL_TYPE3)) { return false; } @@ -577,6 +591,9 @@ static MemTxResult cxl_read_cfmws(void *opaque, hwaddr addr, uint64_t *data, return MEMTX_ERROR; } + if (object_dynamic_cast(OBJECT(d), TYPE_CXL_TYPE2)) { + return cxl_type2_read(d, addr + fw->base, data, size, attrs); + } return cxl_type3_read(d, addr + fw->base, data, size, attrs); } @@ -593,6 +610,9 @@ static MemTxResult cxl_write_cfmws(void *opaque, hwaddr addr, return MEMTX_OK; } + if (object_dynamic_cast(OBJECT(d), TYPE_CXL_TYPE2)) { + return cxl_type2_write(d, addr + fw->base, data, size, attrs); + } return cxl_type3_write(d, addr + fw->base, data, size, attrs); } diff --git a/hw/mem/cxl_type2.c b/hw/mem/cxl_type2.c index b179d1e884..60f3f04679 100644 --- a/hw/mem/cxl_type2.c +++ b/hw/mem/cxl_type2.c @@ -48,6 +48,43 @@ static bool setup_memory(PCIDevice *pci_dev, Error **errp) return true; } +static int cxl_type2_hpa_to_as_and_dpa(CXLType2Dev *ct2d, hwaddr host_addr, + AddressSpace **as, uint64_t *dpa) +{ + MemoryRegion *vmr = host_memory_backend_get_memory(ct2d->hostvmem); + + if (!cxl_host_addr_to_dpa(&ct2d->cxl_cstate, host_addr, dpa) || + *dpa >= memory_region_size(vmr)) { + return -EINVAL; + } + *as = &ct2d->hostvmem_as; + return 0; +} + +MemTxResult cxl_type2_read(PCIDevice *d, hwaddr host_addr, uint64_t *data, + unsigned size, MemTxAttrs attrs) +{ + AddressSpace *as; + uint64_t dpa; + + if (cxl_type2_hpa_to_as_and_dpa(CXL_TYPE2(d), host_addr, &as, &dpa)) { + return MEMTX_ERROR; + } + return address_space_read(as, dpa, attrs, data, size); +} + +MemTxResult cxl_type2_write(PCIDevice *d, hwaddr host_addr, uint64_t data, + unsigned size, MemTxAttrs attrs) +{ + AddressSpace *as; + uint64_t dpa; + + if (cxl_type2_hpa_to_as_and_dpa(CXL_TYPE2(d), host_addr, &as, &dpa)) { + return MEMTX_ERROR; + } + return address_space_write(as, dpa, attrs, &data, size); +} + static void update_dvsecs(CXLType2Dev *ct2d) { CXLComponentState *cxl_cstate = &ct2d->cxl_cstate; @@ -164,6 +201,7 @@ static void cxl_type2_realize(PCIDevice *pci_dev, Error **errp) static void cxl_type2_exit(PCIDevice *pci_dev) { + cxl_fmws_direct_passthrough_remove_all(pci_dev); clean_memory(pci_dev); } @@ -172,6 +210,7 @@ static void cxl_type2_reset(DeviceState *dev) CXLType2Dev *ct2d = CXL_TYPE2(dev); CXLComponentState *cxl_cstate = &ct2d->cxl_cstate; + cxl_fmws_direct_passthrough_remove_all(PCI_DEVICE(dev)); update_dvsecs(ct2d); memcpy(PCI_DEVICE(ct2d)->wmask + range_lob(&cxl_cstate->dvsecs[PCIE_CXL_DEVICE_DVSEC]), diff --git a/hw/mem/cxl_type2_stubs.c b/hw/mem/cxl_type2_stubs.c new file mode 100644 index 0000000000..8962f55552 --- /dev/null +++ b/hw/mem/cxl_type2_stubs.c @@ -0,0 +1,20 @@ +/* + * CXL Type-2 device stubs + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "hw/cxl/cxl.h" + +MemTxResult cxl_type2_read(PCIDevice *d, hwaddr host_addr, uint64_t *data, + unsigned size, MemTxAttrs attrs) +{ + return MEMTX_ERROR; +} + +MemTxResult cxl_type2_write(PCIDevice *d, hwaddr host_addr, uint64_t data, + unsigned size, MemTxAttrs attrs) +{ + return MEMTX_ERROR; +} diff --git a/hw/mem/meson.build b/hw/mem/meson.build index 0b2c193a78..0d20588636 100644 --- a/hw/mem/meson.build +++ b/hw/mem/meson.build @@ -7,6 +7,7 @@ mem_ss.add(when: 'CONFIG_CXL_MEM_DEVICE', if_true: files('cxl_type3.c')) mem_ss.add(when: 'CONFIG_CXL_TYPE2_DEVICE', if_true: files('cxl_type2.c')) mem_ss.add(when: 'CONFIG_SP_MEM', if_true: files('sp-mem.c')) stub_ss.add(files('cxl_type3_stubs.c')) +stub_ss.add(files('cxl_type2_stubs.c')) stub_ss.add(files('memory-device-stubs.c')) system_ss.add_all(when: 'CONFIG_MEM_DEVICE', if_true: mem_ss) diff --git a/include/hw/cxl/cxl_device.h b/include/hw/cxl/cxl_device.h index 9c83421d0f..22ca2f5978 100644 --- a/include/hw/cxl/cxl_device.h +++ b/include/hw/cxl/cxl_device.h @@ -838,6 +838,8 @@ struct CXLType2Dev { HostMemoryBackend *hostvmem; /* State */ + MemoryRegion direct_mr[CXL_HDM_DECODER_COUNT]; + CXLFixedWindow *direct_mr_fw[CXL_HDM_DECODER_COUNT]; AddressSpace hostvmem_as; CXLComponentState cxl_cstate; uint8_t dvsec_wmask[PCIE_CXL_DEVICE_DVSEC_LENGTH]; @@ -846,6 +848,11 @@ struct CXLType2Dev { #define TYPE_CXL_TYPE2 "cxl-type2" OBJECT_DECLARE_SIMPLE_TYPE(CXLType2Dev, CXL_TYPE2) +MemTxResult cxl_type2_read(PCIDevice *d, hwaddr host_addr, uint64_t *data, + unsigned size, MemTxAttrs attrs); +MemTxResult cxl_type2_write(PCIDevice *d, hwaddr host_addr, uint64_t data, + unsigned size, MemTxAttrs attrs); + MemTxResult cxl_type3_read(PCIDevice *d, hwaddr host_addr, uint64_t *data, unsigned size, MemTxAttrs attrs); MemTxResult cxl_type3_write(PCIDevice *d, hwaddr host_addr, uint64_t data, -- 2.53.0
