From: Li Chen <me@linux.beauty>
To: Jonathan Cameron <jonathan.cameron@huawei.com>,
Fan Ni <fan.ni@samsung.com>
Cc: qemu-devel@nongnu.org, Li Chen <me@linux.beauty>
Subject: [PATCH 1/3] cxl/type3: expose vmem mapping for fixed windows
Date: Tue, 17 Mar 2026 11:33:01 +0800 [thread overview]
Message-ID: <20260317033304.3185291-2-me@linux.beauty> (raw)
In-Reply-To: <20260317033304.3185291-1-me@linux.beauty>
CXL fixed memory windows may map linearly to a Type-3 volatile memdev.
Add a helper that checks whether a host physical range is backed by the
volatile memory backend and returns the backing MemoryRegion and offset.
This is used by KVM to alias fixed windows to RAM when possible.
Signed-off-by: Li Chen <me@linux.beauty>
---
hw/mem/cxl_type3.c | 52 +++++++++++++++++++++++++++++++++++++
include/hw/cxl/cxl_device.h | 3 +++
2 files changed, 55 insertions(+)
diff --git a/hw/mem/cxl_type3.c b/hw/mem/cxl_type3.c
index 4739239da3..bf79db6082 100644
--- a/hw/mem/cxl_type3.c
+++ b/hw/mem/cxl_type3.c
@@ -1314,6 +1314,58 @@ MemTxResult cxl_type3_write(PCIDevice *d, hwaddr host_addr, uint64_t data,
return address_space_write(as, dpa_offset, attrs, &data, size);
}
+bool cxl_type3_get_window_vmem_mapping(CXLType3Dev *ct3d, hwaddr host_base,
+ hwaddr size, MemoryRegion **mr,
+ hwaddr *offset, Error **errp)
+{
+ MemoryRegion *vmr;
+ uint64_t dpa_start, dpa_end;
+ uint64_t vmr_size;
+ hwaddr host_end;
+
+ if (!size) {
+ error_setg(errp, "window size must be non-zero");
+ return false;
+ }
+
+ host_end = host_base + size - 1;
+ if (host_end < host_base) {
+ error_setg(errp, "window range overflows");
+ return false;
+ }
+
+ if (!ct3d->hostvmem) {
+ error_setg(errp, "no volatile-memdev configured");
+ return false;
+ }
+ vmr = host_memory_backend_get_memory(ct3d->hostvmem);
+ if (!vmr) {
+ error_setg(errp, "volatile-memdev has no backing memory region");
+ return false;
+ }
+
+ if (!cxl_type3_dpa(ct3d, host_base, &dpa_start) ||
+ !cxl_type3_dpa(ct3d, host_end, &dpa_end)) {
+ error_setg(errp, "failed to translate HPA to DPA");
+ return false;
+ }
+
+ if (dpa_end != dpa_start + size - 1) {
+ error_setg(errp, "window is not linearly mapped to DPA");
+ return false;
+ }
+
+ vmr_size = memory_region_size(vmr);
+ if (dpa_start >= vmr_size || size > vmr_size - dpa_start) {
+ error_setg(errp, "window maps outside volatile-memdev");
+ return false;
+ }
+
+ *mr = vmr;
+ *offset = dpa_start;
+ return true;
+}
+
static void ct3d_reset(DeviceState *dev)
{
CXLType3Dev *ct3d = CXL_TYPE3(dev);
diff --git a/include/hw/cxl/cxl_device.h b/include/hw/cxl/cxl_device.h
index 393f312217..5e3da17c10 100644
--- a/include/hw/cxl/cxl_device.h
+++ b/include/hw/cxl/cxl_device.h
@@ -827,6 +827,9 @@ 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,
unsigned size, MemTxAttrs attrs);
+bool cxl_type3_get_window_vmem_mapping(CXLType3Dev *ct3d, hwaddr host_base,
+ hwaddr size, MemoryRegion **mr,
+ hwaddr *offset, Error **errp);
uint64_t cxl_device_get_timestamp(CXLDeviceState *cxlds);
--
2.52.0
next prev parent reply other threads:[~2026-03-17 3:34 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-17 3:33 [PATCH 0/3] cxl: avoid KVM internal error for fixed memory windows Li Chen
2026-03-17 3:33 ` Li Chen [this message]
2026-03-17 3:33 ` [PATCH 2/3] cxl: alias fixed memory windows to RAM under KVM Li Chen
2026-03-17 3:33 ` [PATCH 3/3] cxl: update fixed window mappings on decoder programming Li Chen
2026-03-17 16:57 ` [PATCH 0/3] cxl: avoid KVM internal error for fixed memory windows Jonathan Cameron via qemu development
2026-03-18 15:01 ` Alireza Sanaee via qemu development
2026-03-22 5:46 ` Li Chen
2026-03-23 9:51 ` Alireza Sanaee via qemu development
2026-03-22 5:49 ` Li Chen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260317033304.3185291-2-me@linux.beauty \
--to=me@linux.beauty \
--cc=fan.ni@samsung.com \
--cc=jonathan.cameron@huawei.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox