Build the ACPI RISC-V Quality of Service Controllers (RQSC) table for
the virt machine when CBQRI controllers are present. A capacity
controller is described by the PPTT Cache ID of the cache it backs and
a bandwidth controller by the SRAT proximity domain of its NUMA node.

Make RISCV_VIRT select RISCV_CBQRI as the table code calls into the
CBQRI devices.

Link: https://github.com/riscv-non-isa/riscv-rqsc/releases/tag/v1.0.0
Co-developed-by: Vasudevan Srinivasan <[email protected]>
Signed-off-by: Vasudevan Srinivasan <[email protected]>
Signed-off-by: Drew Fustini <[email protected]>
---
 hw/riscv/Kconfig           |   2 +-
 hw/riscv/cbqri_bandwidth.c |  13 ++
 hw/riscv/cbqri_capacity.c  |  13 ++
 hw/riscv/virt-acpi-build.c | 298 ++++++++++++++++++++++++++++++++++++++++++++-
 include/hw/riscv/cbqri.h   |  20 +++
 5 files changed, 344 insertions(+), 2 deletions(-)

diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig
index 98bdebb4b814..47dda3e52d20 100644
--- a/hw/riscv/Kconfig
+++ b/hw/riscv/Kconfig
@@ -71,7 +71,7 @@ config RISCV_VIRT
     select PLATFORM_BUS
     select ACPI
     select ACPI_PCI
-    imply RISCV_CBQRI
+    select RISCV_CBQRI
 
 config RISCV_SERVER_PLATFORM_REF
     bool
diff --git a/hw/riscv/cbqri_bandwidth.c b/hw/riscv/cbqri_bandwidth.c
index 6c33d1738d27..c55709d662f2 100644
--- a/hw/riscv/cbqri_bandwidth.c
+++ b/hw/riscv/cbqri_bandwidth.c
@@ -760,4 +760,17 @@ DeviceState *riscv_cbqri_bc_create(hwaddr addr,
     return dev;
 }
 
+void riscv_cbqri_bc_get_details(DeviceState *ds, const char *type,
+                                RqscController *rqsc)
+{
+    if (strcmp(type, TYPE_RISCV_CBQRI_BC) == 0) {
+        RiscvCbqriBandwidthState *bcs = RISCV_CBQRI_BC(ds);
+        rqsc->type = RQSC_CONTROLLER_TYPE_BANDWIDTH;
+        rqsc->mmio_base = bcs->mmio_base;
+        rqsc->rcid_count = bcs->nb_rcids;
+        rqsc->mcid_count = bcs->nb_mcids;
+        rqsc->node = bcs->node;
+    }
+}
+
 type_init(riscv_cbqri_bc_register_types)
diff --git a/hw/riscv/cbqri_capacity.c b/hw/riscv/cbqri_capacity.c
index 1585cad1d073..3ebc8a1cc26d 100644
--- a/hw/riscv/cbqri_capacity.c
+++ b/hw/riscv/cbqri_capacity.c
@@ -776,4 +776,17 @@ DeviceState *riscv_cbqri_cc_create(hwaddr addr,
     return dev;
 }
 
+void riscv_cbqri_cc_get_details(DeviceState *ds, const char *type,
+                                RqscController *rqsc)
+{
+    if (strcmp(type, TYPE_RISCV_CBQRI_CC) == 0) {
+        RiscvCbqriCapacityState *ccs = RISCV_CBQRI_CC(ds);
+        rqsc->type = RQSC_CONTROLLER_TYPE_CAPACITY;
+        rqsc->mmio_base = ccs->mmio_base;
+        rqsc->rcid_count = ccs->nb_rcids;
+        rqsc->mcid_count = ccs->nb_mcids;
+        rqsc->cache_level = ccs->cache_level;
+    }
+}
+
 type_init(riscv_cbqri_cc_register_types)
diff --git a/hw/riscv/virt-acpi-build.c b/hw/riscv/virt-acpi-build.c
index 8254f1294a72..c8a720eb568c 100644
--- a/hw/riscv/virt-acpi-build.c
+++ b/hw/riscv/virt-acpi-build.c
@@ -34,6 +34,7 @@
 #include "hw/pci-host/gpex.h"
 #include "hw/riscv/virt.h"
 #include "hw/riscv/numa.h"
+#include "hw/riscv/cbqri.h"
 #include "hw/virtio/virtio-acpi.h"
 #include "kvm/kvm_riscv.h"
 #include "migration/vmstate.h"
@@ -279,6 +280,295 @@ spcr_setup(GArray *table_data, BIOSLinker *linker, 
RISCVVirtState *s)
 /* RHCT Node[N] starts at offset 56 */
 #define RHCT_NODE_ARRAY_OFFSET 56
 
+/*
+ * RISC-V Quality of Service Controllers (RQSC) table, v1.0
+ * https://github.com/riscv-non-isa/riscv-rqsc/releases/tag/v1.0.0
+ */
+
+/* RQSC Resource Type and Resource ID Type */
+#define RQSC_RESOURCE_TYPE_CACHE                0
+#define RQSC_RESOURCE_TYPE_MEMORY               1
+#define RQSC_RESOURCE_ID_TYPE_PROCESSOR_CACHE   0
+#define RQSC_RESOURCE_ID_TYPE_MEMORY_RANGE      1
+
+#define RQSC_MAX_CONTROLLERS                    10
+
+/* PPTT nodes start after the 36-byte table header */
+#define PPTT_NODE_ARRAY_OFFSET                  36
+/* PPTT revision 3 Cache Type Structure */
+#define PPTT_NODE_TYPE_CACHE                    1
+#define PPTT_CACHE_NODE_LEN                     28
+#define PPTT_CACHE_ID_OFFSET                    24
+
+static bool virt_has_cbqri(RISCVVirtState *vs)
+{
+    BusChild *kid;
+
+    if (!vs->platform_bus_dev) {
+        return false;
+    }
+
+    QTAILQ_FOREACH(kid, &vs->platform_bus_dev->parent_bus->children, sibling) {
+        const char *type = object_get_typename(OBJECT(kid->child));
+
+        if (!strcmp(type, TYPE_RISCV_CBQRI_BC) ||
+            !strcmp(type, TYPE_RISCV_CBQRI_CC)) {
+            return true;
+        }
+    }
+
+    return false;
+}
+
+static int rqsc_cmp_mmio_base(const void *a, const void *b)
+{
+    const RqscController *x = a, *y = b;
+
+    return x->mmio_base < y->mmio_base ? -1 : x->mmio_base > y->mmio_base;
+}
+
+static unsigned int gather_cbqri_details(RISCVVirtState *vs,
+                                         RqscController rqsc[])
+{
+    BusChild *kid;
+    unsigned int count = 0;
+
+    if (!vs->platform_bus_dev) {
+        return 0;
+    }
+
+    QTAILQ_FOREACH(kid, &vs->platform_bus_dev->parent_bus->children, sibling) {
+        const char *type = object_get_typename(OBJECT(kid->child));
+        bool is_bc = !strcmp(type, TYPE_RISCV_CBQRI_BC);
+        bool is_cc = !strcmp(type, TYPE_RISCV_CBQRI_CC);
+
+        if (!is_bc && !is_cc) {
+            continue;
+        }
+
+        if (count == RQSC_MAX_CONTROLLERS) {
+            error_setg(&error_fatal, "RQSC: more than %d CBQRI controllers",
+                       RQSC_MAX_CONTROLLERS);
+        }
+
+        if (is_bc) {
+            riscv_cbqri_bc_get_details(kid->child, type, &rqsc[count]);
+        } else {
+            riscv_cbqri_cc_get_details(kid->child, type, &rqsc[count]);
+        }
+        count++;
+    }
+
+    /*
+     * The bus lists its children newest first. Order the controllers by
+     * address so that the n-th capacity controller of a cache level is
+     * the one backing the n-th cache of that level.
+     */
+    qsort(rqsc, count, sizeof(*rqsc), rqsc_cmp_mmio_base);
+
+    return count;
+}
+
+/*
+ * Return the PPTT Cache ID of one unified cache of the given level. The
+ * topology containers that hold a cache of that level are counted from 0
+ * in possible_cpus order, and idx selects the container. build_pptt()
+ * derives the ID with PPTT_CACHE_ID() from the cache type and level plus
+ * the first CPU of the container, so walk possible_cpus the way it does.
+ * Return -1 when there is no such cache and store in num_caches how many
+ * caches of that level the topology has, 0 when no smp-cache option places
+ * that level.
+ */
+static int64_t rqsc_pptt_cache_id(MachineState *ms, unsigned int level,
+                                  unsigned int idx, unsigned int *num_caches)
+{
+    const CPUArchIdList *cpus = ms->possible_cpus;
+    int64_t socket_id = -1, cluster_id = -1, core_id = -1;
+    unsigned int n, found = 0;
+    CpuTopologyLevel topo;
+
+    *num_caches = 0;
+
+    switch (level) {
+    case 2:
+        topo = machine_get_cache_topo_level(ms, CACHE_LEVEL_AND_TYPE_L2);
+        break;
+    case 3:
+        topo = machine_get_cache_topo_level(ms, CACHE_LEVEL_AND_TYPE_L3);
+        break;
+    default:
+        return -1;
+    }
+
+    for (n = 0; n < cpus->len; n++) {
+        const CpuInstanceProperties *p = &cpus->cpus[n].props;
+        bool first;
+
+        switch (topo) {
+        case CPU_TOPOLOGY_LEVEL_SOCKET:
+            first = p->socket_id != socket_id;
+            break;
+        case CPU_TOPOLOGY_LEVEL_CLUSTER:
+            first = p->socket_id != socket_id || p->cluster_id != cluster_id;
+            break;
+        case CPU_TOPOLOGY_LEVEL_CORE:
+            first = p->socket_id != socket_id ||
+                    p->cluster_id != cluster_id || p->core_id != core_id;
+            break;
+        default:
+            return -1;
+        }
+
+        if (!first) {
+            continue;
+        }
+
+        socket_id = p->socket_id;
+        cluster_id = p->cluster_id;
+        core_id = p->core_id;
+
+        if (found++ == idx) {
+            return PPTT_CACHE_ID(UNIFIED_CACHE, level, n);
+        }
+    }
+
+    *num_caches = found;
+    return -1;
+}
+
+/*
+ * Whether the PPTT at pptt_start holds a Cache Type Structure with the
+ * given Cache ID. build_pptt() does not emit every cache level for every
+ * smp-cache layout, for example an L3 cache placed at core level.
+ */
+static bool rqsc_pptt_has_cache_id(GArray *table_data, unsigned int pptt_start,
+                                   uint32_t id)
+{
+    const uint8_t *pptt = (const uint8_t *)table_data->data + pptt_start;
+    uint32_t len = ldl_le_p(pptt + 4);
+    uint32_t off = PPTT_NODE_ARRAY_OFFSET;
+
+    while (off + PPTT_CACHE_NODE_LEN <= len) {
+        uint8_t node_len = pptt[off + 1];
+
+        if (pptt[off] == PPTT_NODE_TYPE_CACHE &&
+            node_len >= PPTT_CACHE_NODE_LEN &&
+            ldl_le_p(pptt + off + PPTT_CACHE_ID_OFFSET) == id) {
+            return true;
+        }
+        if (!node_len) {
+            break;
+        }
+        off += node_len;
+    }
+
+    return false;
+}
+
+static void build_rqsc(GArray *table_data, BIOSLinker *linker,
+                       RISCVVirtState *s, unsigned int pptt_start)
+{
+    MachineState *ms = MACHINE(s);
+    RqscController rqsc[RQSC_MAX_CONTROLLERS];
+    unsigned int num, i, nr_cc[4] = {};
+    AcpiTable table = { .sig = "RQSC", .rev = 1, .oem_id = s->oem_id,
+                        .oem_table_id = s->oem_table_id };
+
+    acpi_table_begin(&table, table_data);
+
+    num = gather_cbqri_details(s, rqsc);
+
+    /* Number of QoS Controllers */
+    build_append_int_noprefix(table_data, num, 4);
+
+    for (i = 0; i < num; i++) {
+        bool is_cc = rqsc[i].type == RQSC_CONTROLLER_TYPE_CAPACITY;
+        /* A Memory resource carries 8 bytes of Bandwidth per Block */
+        unsigned int res_len = is_cc ? 20 : 28;
+        uint8_t res_type, id_type;
+        int64_t id;
+
+        if (is_cc) {
+            unsigned int level = rqsc[i].cache_level;
+            unsigned int num_caches;
+
+            res_type = RQSC_RESOURCE_TYPE_CACHE;
+            id_type = RQSC_RESOURCE_ID_TYPE_PROCESSOR_CACHE;
+            id = rqsc_pptt_cache_id(ms, level, nr_cc[level]++, &num_caches);
+            if (id < 0 && !num_caches) {
+                error_setg(&error_fatal,
+                           "CBQRI capacity controller at 0x%" PRIx64
+                           " backs an L%u cache, but no smp-cache option"
+                           " places the L%u cache",
+                           rqsc[i].mmio_base, level, level);
+            } else if (id < 0) {
+                error_setg(&error_fatal,
+                           "CBQRI capacity controller at 0x%" PRIx64
+                           " is L%u controller %u, but the smp-cache"
+                           " topology has only %u L%u cache%s",
+                           rqsc[i].mmio_base, level, nr_cc[level] - 1,
+                           num_caches, level, num_caches == 1 ? "" : "s");
+            } else if (!rqsc_pptt_has_cache_id(table_data, pptt_start, id)) {
+                error_setg(&error_fatal,
+                           "CBQRI capacity controller at 0x%" PRIx64
+                           " backs an L%u cache, but the PPTT has no L%u"
+                           " cache with ID 0x%" PRIx64,
+                           rqsc[i].mmio_base, level, level, (uint64_t)id);
+            }
+        } else {
+            res_type = RQSC_RESOURCE_TYPE_MEMORY;
+            id_type = RQSC_RESOURCE_ID_TYPE_MEMORY_RANGE;
+            id = rqsc[i].node;
+        }
+
+        /* Controller Type */
+        build_append_int_noprefix(table_data, rqsc[i].type, 1);
+        /* Reserved */
+        build_append_int_noprefix(table_data, 0, 1);
+        /* Length */
+        build_append_int_noprefix(table_data, 24 + res_len, 2);
+        /* Register Interface Address */
+        build_append_gas(table_data,
+                AML_AS_SYSTEM_MEMORY,
+                0,
+                0,
+                4,
+                rqsc[i].mmio_base);
+        /* RCID Count */
+        build_append_int_noprefix(table_data, rqsc[i].rcid_count, 2);
+        /* MCID Count */
+        build_append_int_noprefix(table_data, rqsc[i].mcid_count, 2);
+        /* Controller Flags */
+        build_append_int_noprefix(table_data, 0, 2);
+        /* Number of Resources, one per controller */
+        build_append_int_noprefix(table_data, 1, 2);
+
+        /* Resource Structure */
+        /* Resource Type */
+        build_append_int_noprefix(table_data, res_type, 1);
+        /* Reserved */
+        build_append_int_noprefix(table_data, 0, 1);
+        /* Length of Resource Structure */
+        build_append_int_noprefix(table_data, res_len, 2);
+        /* Resource Flags */
+        build_append_int_noprefix(table_data, 0, 2);
+        /* Reserved */
+        build_append_int_noprefix(table_data, 0, 1);
+        /* Resource ID Type */
+        build_append_int_noprefix(table_data, id_type, 1);
+        /* Resource ID 1: PPTT Cache ID or SRAT Proximity Domain */
+        build_append_int_noprefix(table_data, id, 8);
+        /* Resource ID 2 */
+        build_append_int_noprefix(table_data, 0, 4);
+        if (!is_cc) {
+            /* Bandwidth per Block, 0 means not provided */
+            build_append_int_noprefix(table_data, 0, 8);
+        }
+    }
+
+    acpi_table_end(linker, &table);
+}
+
 /*
  * ACPI spec, Revision 6.6
  * 5.2.37 RISC-V Hart Capabilities Table (RHCT)
@@ -879,7 +1169,7 @@ build_srat(GArray *table_data, BIOSLinker *linker, 
RISCVVirtState *vms)
 static void virt_acpi_build(RISCVVirtState *s, AcpiBuildTables *tables)
 {
     GArray *table_offsets;
-    unsigned dsdt, xsdt;
+    unsigned dsdt, xsdt, pptt_start;
     GArray *tables_blob = tables->table_data;
     MachineState *ms = MACHINE(s);
     CPUCoreCaches caches[CPU_MAX_CACHES];
@@ -913,9 +1203,15 @@ static void virt_acpi_build(RISCVVirtState *s, 
AcpiBuildTables *tables)
     }
 
     acpi_add_table(table_offsets, tables_blob);
+    pptt_start = tables_blob->len;
     build_pptt(tables_blob, tables->linker, ms, s->oem_id,
                s->oem_table_id, num_caches, caches);
 
+    if (virt_has_cbqri(s)) {
+        acpi_add_table(table_offsets, tables_blob);
+        build_rqsc(tables_blob, tables->linker, s, pptt_start);
+    }
+
     acpi_add_table(table_offsets, tables_blob);
 
     if (ms->acpi_spcr_enabled) {
diff --git a/include/hw/riscv/cbqri.h b/include/hw/riscv/cbqri.h
index 10b64613599f..bb0b961b3a43 100644
--- a/include/hw/riscv/cbqri.h
+++ b/include/hw/riscv/cbqri.h
@@ -78,4 +78,24 @@ DeviceState *riscv_cbqri_cc_create(hwaddr addr,
                                    const RiscvCbqriCapacityCaps *caps);
 DeviceState *riscv_cbqri_bc_create(hwaddr addr,
                                    const RiscvCbqriBandwidthCaps *caps);
+
+/* RQSC QoS Controller Type */
+#define RQSC_CONTROLLER_TYPE_CAPACITY   0
+#define RQSC_CONTROLLER_TYPE_BANDWIDTH  1
+
+/* What the ACPI RQSC table reports about one CBQRI controller */
+typedef struct RqscController {
+    uint8_t type;           /* RQSC_CONTROLLER_TYPE_* */
+    uint64_t mmio_base;
+    uint16_t rcid_count;
+    uint16_t mcid_count;
+    uint8_t cache_level;    /* capacity controllers */
+    uint32_t node;          /* bandwidth controllers */
+} RqscController;
+
+void riscv_cbqri_bc_get_details(DeviceState *ds, const char *type,
+                                RqscController *rqsc);
+void riscv_cbqri_cc_get_details(DeviceState *ds, const char *type,
+                                RqscController *rqsc);
+
 #endif

-- 
2.43.0


Reply via email to