Skip to contentSkip to conversation

hw/cxl: CXL emulation cleanups and minor fixes for upstream

PATCH Superseded Jonathan Cameron 3 participants 12 messages Jan 12, 2023 – Jan 31, 2023

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover hw/cxl: CXL emulation cleanups and minor fixes for upstream

V2:
- Various minor issues found by Philippe, see individual patches. Note that the const_le64() patch matches changes in a set of Philippe's that was never applied. Philippe may send an update of that series before this merges. If that occurs, drop "qemu/bswap: Add const_le64()"
- Picked up tags.

V1 Cover letter.

A small collection of misc fixes and tidying up pulled out from various series. I've pulled this to the top of my queue of CXL related work as they stand fine on their own and it will reduce the noise in the larger patch sets if these go upstream first.

Gregory's patches were posted as part of his work on adding volatile support.
https://lore.kernel.org/linux-cxl/[email protected]/
https://lore.kernel.org/linux-cxl/[email protected]/
I might propose this for upstream inclusion this cycle, but testing is currently limited by lack of suitable kernel support.

Ira's patches were part of his event injection series.
https://lore.kernel.org/linux-cxl/[email protected]/
Intent is to propose for upstream the rest of that series shortly after some minor changes from earlier review.

My three patches have not previously been posted.

For the curious, the current state of QEMU CXL emulation that we are working through the backlog wrt to final cleanup before proposing for upstreaming can be found at.

https://gitlab.com/jic23/qemu/-/commits/cxl-2023-01-11

Gregory Price (2):
hw/cxl: set cxl-type3 device type to PCI_CLASS_MEMORY_CXL hw/cxl: Add CXL_CAPACITY_MULTIPLIER definition

Ira Weiny (3):
qemu/bswap: Add const_le64()
qemu/uuid: Add UUID static initializer hw/cxl/mailbox: Use new UUID network order define for cel_uuid

Jonathan Cameron (3):
hw/mem/cxl_type3: Improve error handling in realize() hw/pci-bridge/cxl_downstream: Fix type naming mismatch hw/i386/acpi: Drop duplicate _UID entry for CXL root bridge

 hw/cxl/cxl-device-utils.c      |  2 +-
 hw/cxl/cxl-mailbox-utils.c     | 28 +++++++++++++++-------------
 hw/i386/acpi-build.c           |  1 -
 hw/mem/cxl_type3.c             | 15 +++++++++++----
 hw/pci-bridge/cxl_downstream.c |  2 +-
 include/hw/cxl/cxl_device.h    |  2 +-
 include/qemu/bswap.h           | 12 +++++++++++-
 include/qemu/uuid.h            | 12 ++++++++++++
 8 files changed, 52 insertions(+), 22 deletions(-)
Signature · 1 line
2.37.2
#1 lore raw

PATCH v2 1/8 hw/mem/cxl_type3: Improve error handling in realize()

msix_init_exclusive_bar() can fail, so if it does cleanup the address space.

---
 hw/mem/cxl_type3.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
hw/mem/cxl_type3.c +10 -2
index dae4fd89ca..252822bd82 100644
@@ -401,7 +401,7 @@ static void ct3_realize(PCIDevice *pci_dev, Error **errp)
     MemoryRegion *mr = &regs->component_registers;
     uint8_t *pci_conf = pci_dev->config;
     unsigned short msix_num = 1;
-    int i;
+    int i, rc;
 
     if (!cxl_setup_memory(ct3d, errp)) {
         return;
@@ -438,7 +438,10 @@ static void ct3_realize(PCIDevice *pci_dev, Error **errp)
                      &ct3d->cxl_dstate.device_registers);
 
     /* MSI(-X) Initailization */
-    msix_init_exclusive_bar(pci_dev, msix_num, 4, NULL);
+    rc = msix_init_exclusive_bar(pci_dev, msix_num, 4, NULL);
+    if (rc) {
+        goto err_address_space_free;
+    }
     for (i = 0; i < msix_num; i++) {
         msix_vector_use(pci_dev, i);
     }
@@ -450,6 +453,11 @@ static void ct3_realize(PCIDevice *pci_dev, Error **errp)
     cxl_cstate->cdat.free_cdat_table = ct3_free_cdat_table;
     cxl_cstate->cdat.private = ct3d;
     cxl_doe_cdat_init(cxl_cstate, errp);
+    return;
+
+err_address_space_free:
+    address_space_destroy(&ct3d->hostmem_as);
+    return;
 }
 
 static void ct3_exit(PCIDevice *pci_dev)
Signature · 1 line
2.37.2
#2 lore raw

PATCH v2 2/8 hw/pci-bridge/cxl_downstream: Fix type naming mismatch

In reply to Jonathan Cameron

Fix capitalization difference between struct name and typedef.

---
 hw/pci-bridge/cxl_downstream.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/pci-bridge/cxl_downstream.c +1 -1
index 3d4e6b59cd..54f507318f 100644
@@ -15,7 +15,7 @@
 #include "hw/pci/pcie_port.h"
 #include "qapi/error.h"
 
-typedef struct CXLDownStreamPort {
+typedef struct CXLDownstreamPort {
     /*< private >*/
     PCIESlot parent_obj;
 
Signature · 1 line
2.37.2
#3 lore raw

PATCH v2 3/8 hw/cxl: set cxl-type3 device type to PCI_CLASS_MEMORY_CXL

In reply to Jonathan Cameron

From: Gregory Price <[email protected]>

Current code sets to STORAGE_EXPRESS and then overrides it.

---
 hw/mem/cxl_type3.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/mem/cxl_type3.c +1 -2
index 252822bd82..217a5e639b 100644
@@ -408,7 +408,6 @@ static void ct3_realize(PCIDevice *pci_dev, Error **errp)
     }
 
     pci_config_set_prog_interface(pci_conf, 0x10);
-    pci_config_set_class(pci_conf, PCI_CLASS_MEMORY_CXL);
 
     pcie_endpoint_cap_init(pci_dev, 0x80);
     if (ct3d->sn != UI64_NULL) {
@@ -627,7 +626,7 @@ static void ct3_class_init(ObjectClass *oc, void *data)
 
     pc->realize = ct3_realize;
     pc->exit = ct3_exit;
-    pc->class_id = PCI_CLASS_STORAGE_EXPRESS;
+    pc->class_id = PCI_CLASS_MEMORY_CXL;
     pc->vendor_id = PCI_VENDOR_ID_INTEL;
     pc->device_id = 0xd93; /* LVF for now */
     pc->revision = 1;
Signature · 1 line
2.37.2
#4 lore raw

PATCH v2 4/8 hw/cxl: Add CXL_CAPACITY_MULTIPLIER definition

In reply to Jonathan Cameron

From: Gregory Price <[email protected]>

Remove usage of magic numbers when accessing capacity fields and replace with CXL_CAPACITY_MULTIPLIER, matching the kernel definition.

---
v2:
Change to 256 * MiB and include qemu/units.h (Philippe Mathieu-Daudé)

 hw/cxl/cxl-mailbox-utils.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
hw/cxl/cxl-mailbox-utils.c +9 -6
index bc1bb18844..3f67b665f5 100644
@@ -12,8 +12,11 @@
 #include "hw/pci/pci.h"
 #include "qemu/cutils.h"
 #include "qemu/log.h"
+#include "qemu/units.h"
 #include "qemu/uuid.h"
 
+#define CXL_CAPACITY_MULTIPLIER   (256 * MiB)
+
 /*
  * How to add a new command, example. The command set FOO, with cmd BAR.
  *  1. Add the command set and cmd to the enum.
@@ -138,7 +141,7 @@ static ret_code cmd_firmware_update_get_info(struct cxl_cmd *cmd,
     } QEMU_PACKED *fw_info;
     QEMU_BUILD_BUG_ON(sizeof(*fw_info) != 0x50);
 
-    if (cxl_dstate->pmem_size < (256 << 20)) {
+    if (cxl_dstate->pmem_size < CXL_CAPACITY_MULTIPLIER) {
         return CXL_MBOX_INTERNAL_ERROR;
     }
 
@@ -283,7 +286,7 @@ static ret_code cmd_identify_memory_device(struct cxl_cmd *cmd,
     CXLType3Class *cvc = CXL_TYPE3_GET_CLASS(ct3d);
     uint64_t size = cxl_dstate->pmem_size;
 
-    if (!QEMU_IS_ALIGNED(size, 256 << 20)) {
+    if (!QEMU_IS_ALIGNED(size, CXL_CAPACITY_MULTIPLIER)) {
         return CXL_MBOX_INTERNAL_ERROR;
     }
 
@@ -293,8 +296,8 @@ static ret_code cmd_identify_memory_device(struct cxl_cmd *cmd,
     /* PMEM only */
     snprintf(id->fw_revision, 0x10, "BWFW VERSION %02d", 0);
 
-    id->total_capacity = size / (256 << 20);
-    id->persistent_capacity = size / (256 << 20);
+    id->total_capacity = size / CXL_CAPACITY_MULTIPLIER;
+    id->persistent_capacity = size / CXL_CAPACITY_MULTIPLIER;
     id->lsa_size = cvc->get_lsa_size(ct3d);
 
     *len = sizeof(*id);
@@ -314,14 +317,14 @@ static ret_code cmd_ccls_get_partition_info(struct cxl_cmd *cmd,
     QEMU_BUILD_BUG_ON(sizeof(*part_info) != 0x20);
     uint64_t size = cxl_dstate->pmem_size;
 
-    if (!QEMU_IS_ALIGNED(size, 256 << 20)) {
+    if (!QEMU_IS_ALIGNED(size, CXL_CAPACITY_MULTIPLIER)) {
         return CXL_MBOX_INTERNAL_ERROR;
     }
 
     /* PMEM only */
     part_info->active_vmem = 0;
     part_info->next_vmem = 0;
-    part_info->active_pmem = size / (256 << 20);
+    part_info->active_pmem = size / CXL_CAPACITY_MULTIPLIER;
     part_info->next_pmem = 0;
 
     *len = sizeof(*part_info);
Signature · 1 line
2.37.2
Reviewed-by on v2 4/8
#5 lore raw

Re: [PATCH v2 4/8] hw/cxl: Add CXL_CAPACITY_MULTIPLIER definition

Quoted 21 lines · Jonathan Cameron

From: Gregory Price <[email protected]>

Remove usage of magic numbers when accessing capacity fields and replace with CXL_CAPACITY_MULTIPLIER, matching the kernel definition.

---
v2:
Change to 256 * MiB and include qemu/units.h (Philippe Mathieu-Daudé)

  hw/cxl/cxl-mailbox-utils.c | 15 +++++++++------
  1 file changed, 9 insertions(+), 6 deletions(-)
hw/cxl/cxl-mailbox-utils.c +0 -0
index bc1bb18844..3f67b665f5 100644

hw/cxl/cxl-mailbox-utils.c line 18

Quoted 7 lines
hw/cxl/cxl-mailbox-utils.c +2 -0
@@ -12,8 +12,11 @@
  #include "hw/pci/pci.h"
  #include "qemu/cutils.h"
  #include "qemu/log.h"
+#include "qemu/units.h"
  #include "qemu/uuid.h"
 
+#define CXL_CAPACITY_MULTIPLIER   (256 * MiB)

Thanks, appreciated.

#6 lore raw

PATCH v2 5/8 hw/i386/acpi: Drop duplicate _UID entry for CXL root bridge

In reply to Jonathan Cameron

Noticed as this prevents iASL disasembling the DSDT table.

---
 hw/i386/acpi-build.c | 1 -
 1 file changed, 1 deletion(-)
hw/i386/acpi-build.c +0 -1
index 127c4e2d50..a584b62ae2 100644
@@ -1482,7 +1482,6 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
                 aml_append(pkg, aml_eisaid("PNP0A03"));
                 aml_append(dev, aml_name_decl("_CID", pkg));
                 aml_append(dev, aml_name_decl("_ADR", aml_int(0)));
-                aml_append(dev, aml_name_decl("_UID", aml_int(bus_num)));
                 build_cxl_osc_method(dev);
             } else if (pci_bus_is_express(bus)) {
                 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
Signature · 1 line
2.37.2
#7 lore raw

Re: [PATCH v2 5/8] hw/i386/acpi: Drop duplicate _UID entry for CXL root bridge

Quoted 4 lines · Jonathan Cameron

Noticed as this prevents iASL disasembling the DSDT table.

This needed but it looks like you did not bother updating expected files for the tests. Did you run make check?

Quoted 18 lines
---
 hw/i386/acpi-build.c | 1 -
 1 file changed, 1 deletion(-)
hw/i386/acpi-build.c +0 -1
index 127c4e2d50..a584b62ae2 100644
@@ -1482,7 +1482,6 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
                 aml_append(pkg, aml_eisaid("PNP0A03"));
                 aml_append(dev, aml_name_decl("_CID", pkg));
                 aml_append(dev, aml_name_decl("_ADR", aml_int(0)));
-                aml_append(dev, aml_name_decl("_UID", aml_int(bus_num)));
                 build_cxl_osc_method(dev);
             } else if (pci_bus_is_express(bus)) {
                 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
Signature · 1 line
2.37.2
#8 lore raw

Re: [PATCH v2 5/8] hw/i386/acpi: Drop duplicate _UID entry for CXL root bridge

Quoted 8 lines · On Fri, 27 Jan 2023 21:38:20 -0500 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 4 lines · Jonathan Cameron

Noticed as this prevents iASL disasembling the DSDT table.

This needed but it looks like you did not bother updating expected files for the tests. Did you run make check?

Sorry, that was sloppy of me.

v3 with table update dance for q35/DSDT.cxl sent out.

(I failed to send this email yesterday).

Quoted 20 lines
Quoted 19 lines
---
 hw/i386/acpi-build.c | 1 -
 1 file changed, 1 deletion(-)
hw/i386/acpi-build.c +0 -1
index 127c4e2d50..a584b62ae2 100644
@@ -1482,7 +1482,6 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
                 aml_append(pkg, aml_eisaid("PNP0A03"));
                 aml_append(dev, aml_name_decl("_CID", pkg));
                 aml_append(dev, aml_name_decl("_ADR", aml_int(0)));
-                aml_append(dev, aml_name_decl("_UID", aml_int(bus_num)));
                 build_cxl_osc_method(dev);
             } else if (pci_bus_is_express(bus)) {
                 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
Signature · 1 line
2.37.2  
#9 lore raw

PATCH v2 6/8 qemu/bswap: Add const_le64()

In reply to Jonathan Cameron

From: Ira Weiny <[email protected]>

Gcc requires constant versions of cpu_to_le* calls.

Add a 64 bit version.

---
v2: Update comment (Philippe)

 include/qemu/bswap.h | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
include/qemu/bswap.h +11 -1
index 346d05f2aa..c5cb9686c5 100644
@@ -182,11 +182,20 @@ CPU_CONVERT(le, 32, uint32_t)
 CPU_CONVERT(le, 64, uint64_t)
 
 /*
- * Same as cpu_to_le{16,32}, except that gcc will figure the result is
+ * Same as cpu_to_le{16,32,64}, except that gcc will figure the result is
  * a compile-time constant if you pass in a constant.  So this can be
  * used to initialize static variables.
  */
 #if HOST_BIG_ENDIAN
+# define const_le64(_x)                          \
+    ((((_x) & 0x00000000000000ffU) << 56) |      \
+     (((_x) & 0x000000000000ff00U) << 40) |      \
+     (((_x) & 0x0000000000ff0000U) << 24) |      \
+     (((_x) & 0x00000000ff000000U) <<  8) |      \
+     (((_x) & 0x000000ff00000000U) >>  8) |      \
+     (((_x) & 0x0000ff0000000000U) >> 24) |      \
+     (((_x) & 0x00ff000000000000U) >> 40) |      \
+     (((_x) & 0xff00000000000000U) >> 56))
 # define const_le32(_x)                          \
     ((((_x) & 0x000000ffU) << 24) |              \
      (((_x) & 0x0000ff00U) <<  8) |              \
@@ -196,6 +205,7 @@ CPU_CONVERT(le, 64, uint64_t)
     ((((_x) & 0x00ff) << 8) |                    \
      (((_x) & 0xff00) >> 8))
 #else
+# define const_le64(_x) (_x)
 # define const_le32(_x) (_x)
 # define const_le16(_x) (_x)
 #endif
Signature · 1 line
2.37.2
#10 lore raw

PATCH v2 7/8 qemu/uuid: Add UUID static initializer

In reply to Jonathan Cameron

From: Ira Weiny <[email protected]>

UUID's are defined as network byte order fields. No static initializer was available for UUID's in their standard big endian format.

Define a big endian initializer for UUIDs.

---
 include/qemu/uuid.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/qemu/uuid.h +12 -0
index 9925febfa5..dc40ee1fc9 100644
@@ -61,6 +61,18 @@ typedef struct {
     (clock_seq_hi_and_reserved), (clock_seq_low), (node0), (node1), (node2),\
     (node3), (node4), (node5) }
 
+/* Normal (network byte order) UUID */
+#define UUID(time_low, time_mid, time_hi_and_version,                    \
+  clock_seq_hi_and_reserved, clock_seq_low, node0, node1, node2,         \
+  node3, node4, node5)                                                   \
+  { ((time_low) >> 24) & 0xff, ((time_low) >> 16) & 0xff,                \
+    ((time_low) >> 8) & 0xff, (time_low) & 0xff,                         \
+    ((time_mid) >> 8) & 0xff, (time_mid) & 0xff,                         \
+    ((time_hi_and_version) >> 8) & 0xff, (time_hi_and_version) & 0xff,   \
+    (clock_seq_hi_and_reserved), (clock_seq_low),                        \
+    (node0), (node1), (node2), (node3), (node4), (node5)                 \
+  }
+
 #define UUID_FMT "%02hhx%02hhx%02hhx%02hhx-" \
                  "%02hhx%02hhx-%02hhx%02hhx-" \
                  "%02hhx%02hhx-" \
Signature · 1 line
2.37.2
#11 lore raw

PATCH v2 8/8 hw/cxl/mailbox: Use new UUID network order define for cel_uuid

In reply to Jonathan Cameron

From: Ira Weiny <[email protected]>

The cel_uuid was programatically generated previously because there was no static initializer for network order UUIDs.

Use the new network order initializer for cel_uuid. Adjust cxl_initialize_mailbox() because it can't fail now.

Update specification reference.

---
v2:
Make it const (Philippe)

 hw/cxl/cxl-device-utils.c   |  2 +-
 hw/cxl/cxl-mailbox-utils.c  | 13 ++++++-------
 include/hw/cxl/cxl_device.h |  2 +-
 3 files changed, 8 insertions(+), 9 deletions(-)
hw/cxl/cxl-device-utils.c +1 -1
index 83ce7a8270..4c5e88aaf5 100644
@@ -267,5 +267,5 @@ void cxl_device_register_init_common(CXLDeviceState *cxl_dstate)
     cxl_device_cap_init(cxl_dstate, MEMORY_DEVICE, 0x4000);
     memdev_reg_init_common(cxl_dstate);
 
-    assert(cxl_initialize_mailbox(cxl_dstate) == 0);
+    cxl_initialize_mailbox(cxl_dstate);
 }
hw/cxl/cxl-mailbox-utils.c +6 -7
index 3f67b665f5..206e04a4b8 100644
@@ -193,7 +193,11 @@ static ret_code cmd_timestamp_set(struct cxl_cmd *cmd,
     return CXL_MBOX_SUCCESS;
 }
 
-static QemuUUID cel_uuid;
+/* CXL 3.0 8.2.9.5.2.1 Command Effects Log (CEL) */
+static const QemuUUID cel_uuid = {
+    .data = UUID(0x0da9c0b5, 0xbf41, 0x4b78, 0x8f, 0x79,
+                 0x96, 0xb1, 0x62, 0x3b, 0x3f, 0x17)
+};
 
 /* 8.2.9.4.1 */
 static ret_code cmd_logs_get_supported(struct cxl_cmd *cmd,
@@ -458,11 +462,8 @@ void cxl_process_mailbox(CXLDeviceState *cxl_dstate)
                      DOORBELL, 0);
 }
 
-int cxl_initialize_mailbox(CXLDeviceState *cxl_dstate)
+void cxl_initialize_mailbox(CXLDeviceState *cxl_dstate)
 {
-    /* CXL 2.0: Table 169 Get Supported Logs Log Entry */
-    const char *cel_uuidstr = "0da9c0b5-bf41-4b78-8f79-96b1623b3f17";
-
     for (int set = 0; set < 256; set++) {
         for (int cmd = 0; cmd < 256; cmd++) {
             if (cxl_cmd_set[set][cmd].handler) {
@@ -476,6 +477,4 @@ int cxl_initialize_mailbox(CXLDeviceState *cxl_dstate)
             }
         }
     }
-
-    return qemu_uuid_parse(cel_uuidstr, &cel_uuid);
 }
include/hw/cxl/cxl_device.h +1 -1
index 250adf18b2..7e5ad65c1d 100644
@@ -170,7 +170,7 @@ CXL_DEVICE_CAPABILITY_HEADER_REGISTER(MEMORY_DEVICE,
                                       CXL_DEVICE_CAP_HDR1_OFFSET +
                                           CXL_DEVICE_CAP_REG_SIZE * 2)
 
-int cxl_initialize_mailbox(CXLDeviceState *cxl_dstate);
+void cxl_initialize_mailbox(CXLDeviceState *cxl_dstate);
 void cxl_process_mailbox(CXLDeviceState *cxl_dstate);
 
 #define cxl_device_cap_init(dstate, reg, cap_id)                           \
Signature · 1 line
2.37.2