xref: /openbmc/qemu/hw/cpu/core.c (revision 37677d7d)
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 
17 static void core_prop_get_core_id(Object *obj, Visitor *v, const char *name,
18                                   void *opaque, Error **errp)
19 {
20     CPUCore *core = CPU_CORE(obj);
21     int64_t value = core->core_id;
22 
23     visit_type_int(v, name, &value, errp);
24 }
25 
26 static void core_prop_set_core_id(Object *obj, Visitor *v, const char *name,
27                                   void *opaque, Error **errp)
28 {
29     CPUCore *core = CPU_CORE(obj);
30     Error *local_err = NULL;
31     int64_t value;
32 
33     visit_type_int(v, name, &value, &local_err);
34     if (local_err) {
35         error_propagate(errp, local_err);
36         return;
37     }
38 
39     if (value < 0) {
40         error_setg(errp, "Invalid core id %"PRId64, value);
41         return;
42     }
43 
44     core->core_id = value;
45 }
46 
47 static void core_prop_get_nr_threads(Object *obj, Visitor *v, const char *name,
48                                      void *opaque, Error **errp)
49 {
50     CPUCore *core = CPU_CORE(obj);
51     int64_t value = core->nr_threads;
52 
53     visit_type_int(v, name, &value, errp);
54 }
55 
56 static void core_prop_set_nr_threads(Object *obj, Visitor *v, const char *name,
57                                      void *opaque, Error **errp)
58 {
59     CPUCore *core = CPU_CORE(obj);
60     Error *local_err = NULL;
61     int64_t value;
62 
63     visit_type_int(v, name, &value, &local_err);
64     if (local_err) {
65         error_propagate(errp, local_err);
66         return;
67     }
68 
69     core->nr_threads = value;
70 }
71 
72 static void cpu_core_instance_init(Object *obj)
73 {
74     CPUCore *core = CPU_CORE(obj);
75 
76     object_property_add(obj, "core-id", "int", core_prop_get_core_id,
77                         core_prop_set_core_id, NULL, NULL, NULL);
78     object_property_add(obj, "nr-threads", "int", core_prop_get_nr_threads,
79                         core_prop_set_nr_threads, NULL, NULL, NULL);
80     core->nr_threads = smp_threads;
81 }
82 
83 static void cpu_core_class_init(ObjectClass *oc, void *data)
84 {
85     DeviceClass *dc = DEVICE_CLASS(oc);
86 
87     set_bit(DEVICE_CATEGORY_CPU, dc->categories);
88 }
89 
90 static const TypeInfo cpu_core_type_info = {
91     .name = TYPE_CPU_CORE,
92     .parent = TYPE_DEVICE,
93     .abstract = true,
94     .class_init = cpu_core_class_init,
95     .instance_size = sizeof(CPUCore),
96     .instance_init = cpu_core_instance_init,
97 };
98 
99 static void cpu_core_register_types(void)
100 {
101     type_register_static(&cpu_core_type_info);
102 }
103 
104 type_init(cpu_core_register_types)
105