[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[RFC 12/41] hw/core/topo: Add helpers to traverse the CPU topology tree
From: |
Zhao Liu |
Subject: |
[RFC 12/41] hw/core/topo: Add helpers to traverse the CPU topology tree |
Date: |
Thu, 30 Nov 2023 22:41:34 +0800 |
From: Zhao Liu <zhao1.liu@intel.com>
The topology devices will be organized as a topology tree. Each topology
device may have many topology children with lower topology level.
Add the helpers to traverse the CPU topology tree.
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
hw/core/cpu-topo.c | 41 ++++++++++++++++++++++++++++++++++++++
include/hw/core/cpu-topo.h | 13 ++++++++++++
2 files changed, 54 insertions(+)
diff --git a/hw/core/cpu-topo.c b/hw/core/cpu-topo.c
index cba2dc747e74..687a4cc566ec 100644
--- a/hw/core/cpu-topo.c
+++ b/hw/core/cpu-topo.c
@@ -318,3 +318,44 @@ static void cpu_topo_register_types(void)
}
type_init(cpu_topo_register_types)
+
+static int do_cpu_topo_child_foreach(CPUTopoState *topo,
+ unsigned long *levels,
+ topo_fn fn, void *opaque,
+ bool recurse)
+{
+ CPUTopoState *child;
+ int ret = TOPO_FOREACH_CONTINUE;
+
+ QTAILQ_FOREACH(child, &topo->children, sibling) {
+ if (!levels || (levels && test_bit(CPU_TOPO_LEVEL(child), levels))) {
+ ret = fn(child, opaque);
+ if (ret == TOPO_FOREACH_END || ret == TOPO_FOREACH_ERR) {
+ break;
+ } else if (ret == TOPO_FOREACH_SIBLING) {
+ continue;
+ }
+ }
+
+ if (recurse) {
+ ret = do_cpu_topo_child_foreach(child, levels, fn, opaque,
recurse);
+ if (ret != TOPO_FOREACH_CONTINUE) {
+ break;
+ }
+ }
+ }
+ return ret;
+}
+
+int cpu_topo_child_foreach(CPUTopoState *topo, unsigned long *levels,
+ topo_fn fn, void *opaque)
+{
+ return do_cpu_topo_child_foreach(topo, levels, fn, opaque, false);
+}
+
+int cpu_topo_child_foreach_recursive(CPUTopoState *topo,
+ unsigned long *levels,
+ topo_fn fn, void *opaque)
+{
+ return do_cpu_topo_child_foreach(topo, levels, fn, opaque, true);
+}
diff --git a/include/hw/core/cpu-topo.h b/include/hw/core/cpu-topo.h
index 1ffdb0be6d38..453bacbb558b 100644
--- a/include/hw/core/cpu-topo.h
+++ b/include/hw/core/cpu-topo.h
@@ -90,4 +90,17 @@ struct CPUTopoState {
#define CPU_TOPO_LEVEL(topo) (CPU_TOPO_GET_CLASS(topo)->level)
+#define TOPO_FOREACH_SIBLING 2
+#define TOPO_FOREACH_END 1
+#define TOPO_FOREACH_CONTINUE 0
+#define TOPO_FOREACH_ERR -1
+
+typedef int (*topo_fn)(CPUTopoState *topo, void *opaque);
+
+int cpu_topo_child_foreach(CPUTopoState *topo, unsigned long *levels,
+ topo_fn fn, void *opaque);
+int cpu_topo_child_foreach_recursive(CPUTopoState *topo,
+ unsigned long *levels,
+ topo_fn fn, void *opaque);
+
#endif /* CPU_TOPO_H */
--
2.34.1
- [RFC 29/41] hw/core/slot: Statistics topology information in CPU slot, (continued)
- [RFC 29/41] hw/core/slot: Statistics topology information in CPU slot, Zhao Liu, 2023/11/30
- [RFC 31/41] hw/machine: Plug cpu-slot into machine to maintain topology tree, Zhao Liu, 2023/11/30
- [RFC 32/41] hw/machine: Build smp topology tree from -smp, Zhao Liu, 2023/11/30
- [RFC 33/41] hw/machine: Validate smp topology tree without -smp, Zhao Liu, 2023/11/30
- [RFC 39/41] hw/i386: Add the interface to search parent for QOM topology, Zhao Liu, 2023/11/30
- [RFC 05/41] qdev: Set device parent and id after setting properties, Zhao Liu, 2023/11/30
- [RFC 06/41] qdev: Introduce user-child interface to collect devices from -device, Zhao Liu, 2023/11/30
- [RFC 08/41] hw/core/topo: Introduce CPU topology device abstraction, Zhao Liu, 2023/11/30
- [RFC 16/41] PPC/ppc-core: Limit plugged-threads and nr-threads to be equal, Zhao Liu, 2023/11/30
- [RFC 22/41] hw/cpu/cluster: Convert cpu-cluster from general device to topology device, Zhao Liu, 2023/11/30
- [RFC 12/41] hw/core/topo: Add helpers to traverse the CPU topology tree,
Zhao Liu <=
- [RFC 14/41] PPC/ppc-core: Offload core-id to PPC specific core abstarction, Zhao Liu, 2023/11/30
- [RFC 17/41] hw/cpu/core: Convert cpu-core from general device to topology device, Zhao Liu, 2023/11/30
- [RFC 19/41] hw/cpu/cluster: Wrap TCG related ops and props into CONFIG_TCG, Zhao Liu, 2023/11/30
- [RFC 41/41] hw/i386: Cleanup non-QOM topology support, Zhao Liu, 2023/11/30
- [RFC 02/41] qdev: Allow qdev_device_add() to add specific category device, Zhao Liu, 2023/11/30
- [RFC 38/41] hw/i386: Wrap apic id and topology sub ids assigning as helpers, Zhao Liu, 2023/11/30
- [RFC 37/41] hw/i386: Allow i386 to create new CPUs from QOM topology, Zhao Liu, 2023/11/30