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
12 #include "hw/boards.h"
13 #include "hw/cpu/core.h"
14 #include "qapi/error.h"
15 #include "qapi/visitor.h"
16
core_prop_get_core_id(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)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
core_prop_set_core_id(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)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 int64_t value;
31
32 if (!visit_type_int(v, name, &value, errp)) {
33 return;
34 }
35
36 if (value < 0) {
37 error_setg(errp, "Invalid core id %"PRId64, value);
38 return;
39 }
40
41 core->core_id = value;
42 }
43
core_prop_get_nr_threads(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)44 static void core_prop_get_nr_threads(Object *obj, Visitor *v, const char *name,
45 void *opaque, Error **errp)
46 {
47 CPUCore *core = CPU_CORE(obj);
48 int64_t value = core->nr_threads;
49
50 visit_type_int(v, name, &value, errp);
51 }
52
core_prop_set_nr_threads(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)53 static void core_prop_set_nr_threads(Object *obj, Visitor *v, const char *name,
54 void *opaque, Error **errp)
55 {
56 CPUCore *core = CPU_CORE(obj);
57 int64_t value;
58
59 if (!visit_type_int(v, name, &value, errp)) {
60 return;
61 }
62
63 core->nr_threads = value;
64 }
65
cpu_core_instance_init(Object * obj)66 static void cpu_core_instance_init(Object *obj)
67 {
68 CPUCore *core = CPU_CORE(obj);
69
70 /*
71 * Only '-device something-cpu-core,help' can get us there before
72 * the machine has been created. We don't care to set nr_threads
73 * in this case since it isn't used afterwards.
74 */
75 if (current_machine) {
76 core->nr_threads = current_machine->smp.threads;
77 }
78 }
79
cpu_core_class_init(ObjectClass * oc,void * data)80 static void cpu_core_class_init(ObjectClass *oc, void *data)
81 {
82 DeviceClass *dc = DEVICE_CLASS(oc);
83
84 set_bit(DEVICE_CATEGORY_CPU, dc->categories);
85 object_class_property_add(oc, "core-id", "int", core_prop_get_core_id,
86 core_prop_set_core_id, NULL, NULL);
87 object_class_property_add(oc, "nr-threads", "int", core_prop_get_nr_threads,
88 core_prop_set_nr_threads, NULL, NULL);
89 }
90
91 static const TypeInfo cpu_core_type_info = {
92 .name = TYPE_CPU_CORE,
93 .parent = TYPE_DEVICE,
94 .abstract = true,
95 .class_init = cpu_core_class_init,
96 .instance_size = sizeof(CPUCore),
97 .instance_init = cpu_core_instance_init,
98 };
99
cpu_core_register_types(void)100 static void cpu_core_register_types(void)
101 {
102 type_register_static(&cpu_core_type_info);
103 }
104
105 type_init(cpu_core_register_types)
106