xref: /openbmc/qemu/hw/cpu/core.c (revision ebe15582)
1 /*
2  * CPU core abstract device
3  *
4  * Copyright (C) 2016 Bharata B Rao <bharata@linux.vnet.ibm.com>
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "hw/cpu/core.h"
12 #include "qapi/visitor.h"
13 #include "qemu/module.h"
14 #include "qapi/error.h"
15 #include "sysemu/cpus.h"
16 #include "hw/boards.h"
17 
18 static void core_prop_get_core_id(Object *obj, Visitor *v, const char *name,
19                                   void *opaque, Error **errp)
20 {
21     CPUCore *core = CPU_CORE(obj);
22     int64_t value = core->core_id;
23 
24     visit_type_int(v, name, &value, errp);
25 }
26 
27 static void core_prop_set_core_id(Object *obj, Visitor *v, const char *name,
28                                   void *opaque, Error **errp)
29 {
30     CPUCore *core = CPU_CORE(obj);
31     Error *local_err = NULL;
32     int64_t value;
33 
34     visit_type_int(v, name, &value, &local_err);
35     if (local_err) {
36         error_propagate(errp, local_err);
37         return;
38     }
39 
40     if (value < 0) {
41         error_setg(errp, "Invalid core id %"PRId64, value);
42         return;
43     }
44 
45     core->core_id = value;
46 }
47 
48 static void core_prop_get_nr_threads(Object *obj, Visitor *v, const char *name,
49                                      void *opaque, Error **errp)
50 {
51     CPUCore *core = CPU_CORE(obj);
52     int64_t value = core->nr_threads;
53 
54     visit_type_int(v, name, &value, errp);
55 }
56 
57 static void core_prop_set_nr_threads(Object *obj, Visitor *v, const char *name,
58                                      void *opaque, Error **errp)
59 {
60     CPUCore *core = CPU_CORE(obj);
61     Error *local_err = NULL;
62     int64_t value;
63 
64     visit_type_int(v, name, &value, &local_err);
65     if (local_err) {
66         error_propagate(errp, local_err);
67         return;
68     }
69 
70     core->nr_threads = value;
71 }
72 
73 static void cpu_core_instance_init(Object *obj)
74 {
75     MachineState *ms = MACHINE(qdev_get_machine());
76     CPUCore *core = CPU_CORE(obj);
77 
78     object_property_add(obj, "core-id", "int", core_prop_get_core_id,
79                         core_prop_set_core_id, NULL, NULL, NULL);
80     object_property_add(obj, "nr-threads", "int", core_prop_get_nr_threads,
81                         core_prop_set_nr_threads, NULL, NULL, NULL);
82     core->nr_threads = ms->smp.threads;
83 }
84 
85 static void cpu_core_class_init(ObjectClass *oc, void *data)
86 {
87     DeviceClass *dc = DEVICE_CLASS(oc);
88 
89     set_bit(DEVICE_CATEGORY_CPU, dc->categories);
90 }
91 
92 static const TypeInfo cpu_core_type_info = {
93     .name = TYPE_CPU_CORE,
94     .parent = TYPE_DEVICE,
95     .abstract = true,
96     .class_init = cpu_core_class_init,
97     .instance_size = sizeof(CPUCore),
98     .instance_init = cpu_core_instance_init,
99 };
100 
101 static void cpu_core_register_types(void)
102 {
103     type_register_static(&cpu_core_type_info);
104 }
105 
106 type_init(cpu_core_register_types)
107