diff mbox

[RFC,v0,6/9] cpu: Introduce CPU core device

Message ID 1449728144-6223-7-git-send-email-bharata@linux.vnet.ibm.com
State New
Headers show

Commit Message

Bharata B Rao Dec. 10, 2015, 6:15 a.m. UTC
CPU core device is a container of CPU thread devices. Core device links
to the backend socket object. All the cores within a socket defined
in the topology specification will link to the same socket object.
CPU hotplug is performed in the granularity of CPU core device.
When hotplugged, CPU core creates CPU thread devices.

Signed-off-by: Bharata B Rao <bharata@linux.vnet.ibm.com>
---
 hw/cpu/Makefile.objs  |  2 +-
 hw/cpu/core.c         | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/cpu/core.h | 28 +++++++++++++++
 3 files changed, 127 insertions(+), 1 deletion(-)
 create mode 100644 hw/cpu/core.c
 create mode 100644 include/hw/cpu/core.h
diff mbox

Patch

diff --git a/hw/cpu/Makefile.objs b/hw/cpu/Makefile.objs
index 93d1226..de5c313 100644
--- a/hw/cpu/Makefile.objs
+++ b/hw/cpu/Makefile.objs
@@ -2,5 +2,5 @@  obj-$(CONFIG_ARM11MPCORE) += arm11mpcore.o
 obj-$(CONFIG_REALVIEW) += realview_mpcore.o
 obj-$(CONFIG_A9MPCORE) += a9mpcore.o
 obj-$(CONFIG_A15MPCORE) += a15mpcore.o
-obj-y += socket.o
+obj-y += socket.o core.o
 
diff --git a/hw/cpu/core.c b/hw/cpu/core.c
new file mode 100644
index 0000000..d14bd77
--- /dev/null
+++ b/hw/cpu/core.c
@@ -0,0 +1,98 @@ 
+/*
+ * CPU core device, acts as container of CPU thread devices.
+ *
+ * Copyright (C) 2015 Bharata B Rao <bharata@linux.vnet.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#include "hw/cpu/core.h"
+#include "hw/boards.h"
+#include <sysemu/cpus.h>
+#include "qemu/error-report.h"
+
+static int cpu_core_realize_child(Object *child, void *opaque)
+{
+    Error **errp = opaque;
+
+    object_property_set_bool(child, true, "realized", errp);
+    if (*errp) {
+        return 1;
+    }
+
+    return 0;
+}
+
+static void cpu_core_realize(DeviceState *dev, Error **errp)
+{
+    CPUCore *core = CPU_CORE(OBJECT(dev));
+
+    if (!core->socket) {
+        error_setg(errp, "'" CPU_CORE_SOCKET_PROP "' property is not set");
+        return;
+    }
+    object_child_foreach(OBJECT(dev), cpu_core_realize_child, errp);
+}
+
+static void cpu_core_class_init(ObjectClass *oc, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(oc);
+
+    dc->realize = cpu_core_realize;
+}
+
+static void cpu_core_check_socket_is_full(Object *obj, const char *name,
+                                          Object *val, Error **errp)
+{
+    CPUSocket *socket = CPU_SOCKET(val);
+
+    if (socket->nr_cores == smp_cores) {
+        char *path = object_get_canonical_path_component(val);
+        error_setg(errp, "Socket already full: %s", path);
+        g_free(path);
+    } else {
+        socket->nr_cores++;
+        qdev_prop_allow_set_link_before_realize(obj, name, val, errp);
+    }
+}
+
+static void cpu_core_instance_init(Object *obj)
+{
+    int i;
+    CPUState *cpu;
+    MachineState *machine = MACHINE(qdev_get_machine());
+    CPUCore *core = CPU_CORE(obj);
+
+    object_property_add_link(obj, CPU_CORE_SOCKET_PROP, TYPE_CPU_SOCKET,
+                             (Object **)&core->socket,
+                             cpu_core_check_socket_is_full,
+                             OBJ_PROP_LINK_UNREF_ON_RELEASE,
+                             &error_abort);
+
+    /* Create as many CPU threads as specified in the topology */
+    for (i = 0; i < smp_threads; i++) {
+        cpu = cpu_generic_init(machine->cpu_type, machine->cpu_model);
+        if (!cpu) {
+            error_report("Unable to find CPU definition: %s\n",
+                          machine->cpu_model);
+            exit(EXIT_FAILURE);
+        }
+        object_property_add_child(obj, "thread[*]", OBJECT(cpu), &error_abort);
+        object_unref(OBJECT(cpu));
+    }
+}
+
+static const TypeInfo cpu_core_type_info = {
+    .name = TYPE_CPU_CORE,
+    .parent = TYPE_DEVICE,
+    .class_init = cpu_core_class_init,
+    .instance_init = cpu_core_instance_init,
+    .instance_size = sizeof(CPUCore),
+};
+
+static void cpu_core_register_types(void)
+{
+    type_register_static(&cpu_core_type_info);
+}
+
+type_init(cpu_core_register_types)
diff --git a/include/hw/cpu/core.h b/include/hw/cpu/core.h
new file mode 100644
index 0000000..0314098
--- /dev/null
+++ b/include/hw/cpu/core.h
@@ -0,0 +1,28 @@ 
+/*
+ * CPU core device.
+ *
+ * Copyright (C) 2015 Bharata B Rao <bharata@linux.vnet.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#ifndef HW_CPU_CORE_H
+#define HW_CPU_CORE_H
+
+#include "hw/qdev.h"
+#include "hw/cpu/socket.h"
+
+#define TYPE_CPU_CORE "cpu-core"
+#define CPU_CORE(obj) \
+    OBJECT_CHECK(CPUCore, (obj), TYPE_CPU_CORE)
+
+#define CPU_CORE_SOCKET_PROP "socket"
+
+typedef struct CPUCore {
+    /*< private >*/
+    DeviceState parent_obj;
+    /*< public >*/
+    CPUSocket *socket;
+} CPUCore;
+
+#endif