From nobody Sat Nov 29 10:48:41 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1764284306; cv=none; d=zohomail.com; s=zohoarc; b=GhZD4Vo0cf4H6Z5CQJJemOfk4KkBAzHhajpwPRBiGLlH+vi5GgJo9eb4QCxJxoPqoVRJtKmDNPeL2KME3K0dLF+3FaW+32rd6e6/t4Q/4NBb8vAchzF1R0pB6I0EjF4JFvNal26Rl/NEx1FdLD8nQLIAKgliNji6QEZkGoD0vic= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1764284306; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=2+75VMdILjkKzsi0XiHtZYtbHRdb23XmWJbMhKLtBMc=; b=hTUdVHtAqtP94hb0U55dH1g43C+A99W3TwXB+2brpjN80oMf8EdD0gT17JYbs5bY5pCDvT2C4+tzZyySJQqdtodHyLT0Cau+Ah+M/9NBh9VxxJwJW/cQBH4Pps0XtdR+mZJfFkpDW+4qWpUZxZcFyzW64PI7EX+cka5VZAi0awo= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1764284306723918.0836428607884; Thu, 27 Nov 2025 14:58:26 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vOkwN-0004GK-SG; Thu, 27 Nov 2025 17:58:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vOkwM-0004Ew-5j for qemu-devel@nongnu.org; Thu, 27 Nov 2025 17:58:18 -0500 Received: from frasgout.his.huawei.com ([185.176.79.56]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vOkwK-0001oK-8y for qemu-devel@nongnu.org; Thu, 27 Nov 2025 17:58:17 -0500 Received: from mail.maildlp.com (unknown [172.18.186.31]) by frasgout.his.huawei.com (SkyGuard) with ESMTPS id 4dHWz94vzKzHnGcc; Fri, 28 Nov 2025 06:57:25 +0800 (CST) Received: from dubpeml500005.china.huawei.com (unknown [7.214.145.207]) by mail.maildlp.com (Postfix) with ESMTPS id 0814F1400D4; Fri, 28 Nov 2025 06:58:15 +0800 (CST) Received: from a2303103017.china.huawei.com (10.47.72.83) by dubpeml500005.china.huawei.com (7.214.145.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Thu, 27 Nov 2025 22:58:14 +0000 To: CC: , , , , , , , , , Subject: [RFC PATCH 5/7] hw/cxl: Add performant direct mapping for extents Date: Thu, 27 Nov 2025 22:55:23 +0000 Message-ID: <20251127225526.700-6-alireza.sanaee@huawei.com> X-Mailer: git-send-email 2.51.0.windows.2 In-Reply-To: <20251127225526.700-1-alireza.sanaee@huawei.com> References: <20251127225526.700-1-alireza.sanaee@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.47.72.83] X-ClientProxiedBy: lhrpeml100012.china.huawei.com (7.191.174.184) To dubpeml500005.china.huawei.com (7.214.145.207) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=185.176.79.56; envelope-from=alireza.sanaee@huawei.com; helo=frasgout.his.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Alireza Sanaee From: Alireza Sanaee via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1764284309376019200 Content-Type: text/plain; charset="utf-8" Add alias direct mapping into the fixed memory window. Signed-off-by: Alireza Sanaee --- hw/cxl/cxl-host.c | 4 ++ hw/cxl/cxl-mailbox-utils.c | 73 +++++++++++++++++++++++++++++-------- hw/mem/cxl_type3.c | 6 ++- include/hw/cxl/cxl_device.h | 25 +++++++++++-- 4 files changed, 87 insertions(+), 21 deletions(-) diff --git a/hw/cxl/cxl-host.c b/hw/cxl/cxl-host.c index 7c8fde4646..3b327be68c 100644 --- a/hw/cxl/cxl-host.c +++ b/hw/cxl/cxl-host.c @@ -362,10 +362,14 @@ static int cxl_fmws_direct_passthrough(Object *obj, v= oid *opaque) fw =3D CXL_FMW(obj); =20 if (!cfmws_is_not_interleaved(fw, state->decoder_base)) { + ct3d->direct_mr_enabled =3D false; return 0; } + ct3d->direct_mr_enabled =3D true; =20 ct3d->dc.fw =3D fw; + ct3d->dc.dc_decoder_window.base =3D state->decoder_base; + ct3d->dc.dc_decoder_window.size =3D state->decoder_size; =20 if (state->commit) { MemoryRegion *mr =3D NULL; diff --git a/hw/cxl/cxl-mailbox-utils.c b/hw/cxl/cxl-mailbox-utils.c index b785553225..15114a5314 100644 --- a/hw/cxl/cxl-mailbox-utils.c +++ b/hw/cxl/cxl-mailbox-utils.c @@ -2846,13 +2846,19 @@ void cxl_insert_extent_to_extent_list(CXLDCExtentLi= st *list, uint64_t len, uint8_t *tag, uint16_t shared_seq, - int rid) + int rid, + uint64_t offset) { CXLDCExtent *extent; =20 extent =3D g_new0(CXLDCExtent, 1); extent->start_dpa =3D dpa; extent->len =3D len; + extent->offset =3D offset; + extent->shared_seq =3D shared_seq; + extent->hm =3D host_mem; + extent->fw =3D fw; + extent->rid =3D rid; if (tag) { memcpy(extent->tag, tag, 0x10); } @@ -2880,14 +2886,15 @@ CXLDCExtentGroup *cxl_insert_extent_to_extent_group= (CXLDCExtentGroup *group, uint64_t len, uint8_t *tag, uint16_t shared_seq, - int rid) + int rid, + uint64_t offset) { if (!group) { group =3D g_new0(CXLDCExtentGroup, 1); QTAILQ_INIT(&group->list); } cxl_insert_extent_to_extent_list(&group->list, host_mem, fw, dpa, len, - tag, shared_seq, rid); + tag, shared_seq, rid, offset); return group; } =20 @@ -2985,7 +2992,8 @@ static bool cxl_extent_find_extent_detail(CXLDCExtent= GroupList *list, uint8_t *tag, HostMemoryBackend **hmb, struct CXLFixedWindow **fw, - int *rid) + int *rid, + uint64_t *offset) { CXLDCExtent *ent, *ent_next; CXLDCExtentGroup *group =3D QTAILQ_FIRST(list); @@ -2996,6 +3004,7 @@ static bool cxl_extent_find_extent_detail(CXLDCExtent= GroupList *list, *hmb =3D ent->hm; memcpy(tag, ent->tag, 0x10); *rid =3D ent->rid; + *offset =3D ent->offset; return true; } } @@ -3057,7 +3066,7 @@ static CXLRetCode cmd_dcd_add_dyn_cap_rsp(const struc= t cxl_cmd *cmd, HostMemoryBackend *hmb_dc; uint8_t tag[0x10]; uint32_t i, num; - uint64_t dpa, len; + uint64_t dpa, len, offset; int rid; CXLRetCode ret; =20 @@ -3100,16 +3109,25 @@ static CXLRetCode cmd_dcd_add_dyn_cap_rsp(const str= uct cxl_cmd *cmd, len =3D in->updated_entries[i].len; =20 if (ct3d->dc.total_capacity_cmd) { + int mr_idx =3D ct3d->dc.cur_direct_region_idx; found =3D cxl_extent_find_extent_detail( - &ct3d->dc.extents_pending, dpa, len, tag, &hmb_dc, &fw, &r= id); + &ct3d->dc.extents_pending, dpa, len, tag, &hmb_dc, &fw, &r= id, + &offset); =20 /* * This only occurs when host accepts an extent where device d= oes * not know anything about it. */ if (!found) { - qemu_log("Could not find the extent detail for DPA 0x%" PR= Ix64 - " LEN 0x%" PRIx64 "\n", + qemu_log("could not find the extent detail for dpa 0x%" PR= Ix64 + " len 0x%" PRIx64 "\n", + dpa, len); + return CXL_MBOX_INVALID_PA; + } + + if (!hmb_dc) { + qemu_log("Mapping host memory backend for dpa 0x%" PRIx64 + " len 0x%" PRIx64 "\n", dpa, len); return CXL_MBOX_INVALID_PA; } @@ -3123,6 +3141,7 @@ static CXLRetCode cmd_dcd_add_dyn_cap_rsp(const struc= t cxl_cmd *cmd, } =20 mr =3D host_memory_backend_get_memory(hmb_dc); + if (!mr) { qemu_log("Could not get memory region from host memory " "backend\n"); @@ -3132,11 +3151,32 @@ static CXLRetCode cmd_dcd_add_dyn_cap_rsp(const str= uct cxl_cmd *cmd, memory_region_set_nonvolatile(mr, false); memory_region_set_enabled(mr, true); host_memory_backend_set_mapped(hmb_dc, true); + + if (ct3d->direct_mr_enabled) { + g_autofree char *direct_mapping_name =3D + g_strdup_printf("cxl-direct-mapping-%d", mr_idx); + int region_offset =3D dpa - ct3d->dc.regions[rid].base; + MemoryRegion *dr_dc_mr =3D &ct3d->dc.dc_direct_mr[mr_idx]; + memory_region_init_alias(dr_dc_mr, OBJECT(ct3d), + direct_mapping_name, mr, region_o= ffset, + ct3d->dc.dc_decoder_window.size); + memory_region_add_subregion(&fw->mr, + ct3d->dc.dc_decoder_window.bas= e - + fw->base + offset, + dr_dc_mr); + /* + * for now assuming 4 extents and 4 direct mapping memory + * regions. + */ + ct3d->dc.cur_direct_region_idx =3D + (ct3d->dc.cur_direct_region_idx + 1) % 4; + } + cxl_insert_extent_to_extent_list(extent_list, hmb_dc, fw, dpa,= len, - NULL, 0, rid); + tag, 0, rid, offset); } else { cxl_insert_extent_to_extent_list(extent_list, NULL, NULL, dpa,= len, - NULL, 0, -1); + NULL, 0, -1, -1); } =20 ct3d->dc.total_extent_count +=3D 1; @@ -3167,7 +3207,7 @@ static uint32_t copy_extent_list(CXLDCExtentList *dst, QTAILQ_FOREACH(ent, src, node) { cxl_insert_extent_to_extent_list(dst, ent->hm, ent->fw, ent->start= _dpa, ent->len, ent->tag, ent->shared_s= eq, - ent->rid); + ent->rid, ent->offset); cnt++; } return cnt; @@ -3223,15 +3263,15 @@ static CXLRetCode cxl_dc_extent_release_dry_run(CXL= Type3Dev *ct3d, if (len1) { cxl_insert_extent_to_extent_list(updated_list, NUL= L, NULL, ent_start_d= pa, - len1, NULL, 0, - ent->rid); + len1, ent->tag, 0, + ent->rid, ent->of= fset); cnt_delta++; } if (len2) { cxl_insert_extent_to_extent_list(updated_list, NUL= L, NULL, dpa + len, - len2, NULL, 0, - ent->rid); + len2, ent->tag, 0, + ent->rid, ent->of= fset); cnt_delta++; } =20 @@ -3706,7 +3746,8 @@ static CXLRetCode cmd_fm_initiate_dc_add(const struct= cxl_cmd *cmd, group =3D cxl_insert_extent_to_extent_group(group, NULL, N= ULL, ext->start_dpa, ext->len, ext->t= ag, - ext->shared_seq,= 0); + ext->shared_seq,= 0, + -1); } =20 cxl_extent_group_list_insert_tail(&ct3d->dc.extents_pending, g= roup); diff --git a/hw/mem/cxl_type3.c b/hw/mem/cxl_type3.c index ef1c1cbaef..e3093f63a3 100644 --- a/hw/mem/cxl_type3.c +++ b/hw/mem/cxl_type3.c @@ -2139,7 +2139,8 @@ static void qmp_cxl_process_dynamic_capacity_prescrip= tive(const char *path, extents[i].len, extents[i].tag, extents[i].share= d_seq, - rid); + rid, + offset); } else { group =3D cxl_insert_extent_to_extent_group(group, dcd->dc.host_dc, @@ -2148,7 +2149,8 @@ static void qmp_cxl_process_dynamic_capacity_prescrip= tive(const char *path, extents[i].len, extents[i].tag, extents[i].share= d_seq, - rid); + rid, + offset); } } =20 diff --git a/include/hw/cxl/cxl_device.h b/include/hw/cxl/cxl_device.h index fe0c44e8d7..1a521df881 100644 --- a/include/hw/cxl/cxl_device.h +++ b/include/hw/cxl/cxl_device.h @@ -524,6 +524,7 @@ typedef struct CXLDCExtent { uint16_t shared_seq; uint8_t rsvd[0x6]; int rid; + uint64_t offset; =20 QTAILQ_ENTRY(CXLDCExtent) node; } CXLDCExtent; @@ -589,6 +590,7 @@ struct CXLType3Dev { =20 /* State */ MemoryRegion direct_mr[CXL_HDM_DECODER_COUNT]; + bool direct_mr_enabled; AddressSpace hostvmem_as; AddressSpace hostpmem_as; CXLComponentState cxl_cstate; @@ -633,6 +635,14 @@ struct CXLType3Dev { HostMemoryBackend *host_dc; AddressSpace host_dc_as; struct CXLFixedWindow *fw; + int cur_direct_region_idx; + /* + * dc_decoder_window represents the CXL Decoder Window + */ + struct decoder_window { + hwaddr base; + hwaddr size; + } dc_decoder_window; /* * total_capacity is equivalent to the dynamic capability * memory region size. @@ -647,6 +657,11 @@ struct CXLType3Dev { =20 uint8_t num_regions; /* 0-8 regions */ CXLDCRegion regions[DCD_MAX_NUM_REGION]; + /* + * Assume 4 now but many possible, each region is one alias an ext= ent + * to allow performance translation in KVM. + */ + MemoryRegion dc_direct_mr[4]; } dc; =20 struct CXLSanitizeInfo *media_op_sanitize; @@ -720,8 +735,11 @@ void cxl_insert_extent_to_extent_list(CXLDCExtentList = *list, HostMemoryBackend *hm, struct CXLFixedWindow *fw, uint64_t dpa, - uint64_t len, uint8_t *tag, - uint16_t shared_seq, int rid); + uint64_t len, + uint8_t *tag, + uint16_t shared_seq, + int rid, + uint64_t offset); bool test_any_bits_set(const unsigned long *addr, unsigned long nr, unsigned long size); bool cxl_extents_contains_dpa_range(CXLDCExtentList *list, @@ -733,7 +751,8 @@ CXLDCExtentGroup *cxl_insert_extent_to_extent_group(CXL= DCExtentGroup *group, uint64_t len, uint8_t *tag, uint16_t shared_seq, - int rid); + int rid, + uint64_t offset); void cxl_extent_group_list_insert_tail(CXLDCExtentGroupList *list, CXLDCExtentGroup *group); uint32_t cxl_extent_group_list_delete_front(CXLDCExtentGroupList *list); --=20 2.43.0