2 * sPAPR CPU core device, acts as container of CPU thread devices.
4 * Copyright (C) 2016 Bharata B Rao <bharata@linux.vnet.ibm.com>
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.
9 #include "hw/cpu/core.h"
10 #include "hw/ppc/spapr_cpu_core.h"
11 #include "target/ppc/cpu.h"
12 #include "hw/ppc/spapr.h"
13 #include "hw/boards.h"
14 #include "qapi/error.h"
15 #include "sysemu/cpus.h"
16 #include "sysemu/kvm.h"
17 #include "target/ppc/kvm_ppc.h"
18 #include "hw/ppc/ppc.h"
19 #include "target/ppc/mmu-hash64.h"
20 #include "sysemu/numa.h"
21 #include "qemu/error-report.h"
23 void spapr_cpu_parse_features(sPAPRMachineState
*spapr
)
26 * Backwards compatibility hack:
28 * CPUs had a "compat=" property which didn't make sense for
29 * anything except pseries. It was replaced by "max-cpu-compat"
30 * machine option. This supports old command lines like
31 * -cpu POWER8,compat=power7
32 * By stripping the compat option and applying it to the machine
33 * before passing it on to the cpu level parser.
37 gchar
*compat_str
= NULL
;
39 inpieces
= g_strsplit(MACHINE(spapr
)->cpu_model
, ",", 0);
41 /* inpieces[0] is the actual model string */
45 if (g_str_has_prefix(inpieces
[i
], "compat=")) {
46 /* in case of multiple compat= options */
48 compat_str
= inpieces
[i
];
54 /* Excise compat options from list */
55 inpieces
[j
] = inpieces
[i
];
59 char *val
= compat_str
+ strlen("compat=");
60 gchar
*newprops
= g_strjoinv(",", inpieces
);
62 object_property_set_str(OBJECT(spapr
), val
, "max-cpu-compat",
65 ppc_cpu_parse_features(newprops
);
68 ppc_cpu_parse_features(MACHINE(spapr
)->cpu_model
);
74 static void spapr_cpu_reset(void *opaque
)
76 sPAPRMachineState
*spapr
= SPAPR_MACHINE(qdev_get_machine());
77 PowerPCCPU
*cpu
= opaque
;
78 CPUState
*cs
= CPU(cpu
);
79 CPUPPCState
*env
= &cpu
->env
;
83 /* All CPUs start halted. CPU0 is unhalted from the machine level
84 * reset code and the rest are explicitly started up by the guest
85 * using an RTAS call */
88 env
->spr
[SPR_HIOR
] = 0;
91 * This is a hack for the benefit of KVM PR - it abuses the SDR1
92 * slot in kvm_sregs to communicate the userspace address of the
96 env
->spr
[SPR_SDR1
] = (target_ulong
)(uintptr_t)spapr
->htab
97 | (spapr
->htab_shift
- 18);
98 if (kvmppc_put_books_sregs(cpu
) < 0) {
99 error_report("Unable to update SDR1 in KVM");
105 static void spapr_cpu_destroy(PowerPCCPU
*cpu
)
107 qemu_unregister_reset(spapr_cpu_reset
, cpu
);
110 static void spapr_cpu_init(sPAPRMachineState
*spapr
, PowerPCCPU
*cpu
,
113 CPUPPCState
*env
= &cpu
->env
;
115 /* Set time-base frequency to 512 MHz */
116 cpu_ppc_tb_init(env
, SPAPR_TIMEBASE_FREQ
);
118 /* Enable PAPR mode in TCG or KVM */
119 cpu_ppc_set_papr(cpu
, PPC_VIRTUAL_HYPERVISOR(spapr
));
121 qemu_register_reset(spapr_cpu_reset
, cpu
);
122 spapr_cpu_reset(cpu
);
126 * Return the sPAPR CPU core type for @model which essentially is the CPU
127 * model specified with -cpu cmdline option.
129 char *spapr_get_cpu_core_type(const char *model
)
132 gchar
**model_pieces
= g_strsplit(model
, ",", 2);
134 core_type
= g_strdup_printf("%s-%s", model_pieces
[0], TYPE_SPAPR_CPU_CORE
);
136 /* Check whether it exists or whether we have to look up an alias name */
137 if (!object_class_by_name(core_type
)) {
138 const char *realmodel
;
142 realmodel
= ppc_cpu_lookup_alias(model_pieces
[0]);
144 core_type
= spapr_get_cpu_core_type(realmodel
);
148 g_strfreev(model_pieces
);
152 static void spapr_cpu_core_unrealizefn(DeviceState
*dev
, Error
**errp
)
154 sPAPRCPUCore
*sc
= SPAPR_CPU_CORE(OBJECT(dev
));
155 sPAPRCPUCoreClass
*scc
= SPAPR_CPU_CORE_GET_CLASS(OBJECT(dev
));
156 const char *typename
= object_class_get_name(scc
->cpu_class
);
157 size_t size
= object_type_get_instance_size(typename
);
158 CPUCore
*cc
= CPU_CORE(dev
);
161 for (i
= 0; i
< cc
->nr_threads
; i
++) {
162 void *obj
= sc
->threads
+ i
* size
;
163 DeviceState
*dev
= DEVICE(obj
);
164 CPUState
*cs
= CPU(dev
);
165 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
167 spapr_cpu_destroy(cpu
);
168 object_unparent(cpu
->intc
);
170 object_unparent(obj
);
175 static void spapr_cpu_core_realize_child(Object
*child
, Error
**errp
)
177 Error
*local_err
= NULL
;
178 sPAPRMachineState
*spapr
= SPAPR_MACHINE(qdev_get_machine());
179 CPUState
*cs
= CPU(child
);
180 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
183 object_property_set_bool(child
, true, "realized", &local_err
);
188 spapr_cpu_init(spapr
, cpu
, &local_err
);
193 obj
= object_new(spapr
->icp_type
);
194 object_property_add_child(child
, "icp", obj
, &error_abort
);
196 object_property_add_const_link(obj
, ICP_PROP_XICS
, OBJECT(spapr
),
198 object_property_add_const_link(obj
, ICP_PROP_CPU
, child
, &error_abort
);
199 object_property_set_bool(obj
, true, "realized", &local_err
);
207 object_unparent(obj
);
209 error_propagate(errp
, local_err
);
212 static void spapr_cpu_core_realize(DeviceState
*dev
, Error
**errp
)
214 sPAPRCPUCore
*sc
= SPAPR_CPU_CORE(OBJECT(dev
));
215 sPAPRCPUCoreClass
*scc
= SPAPR_CPU_CORE_GET_CLASS(OBJECT(dev
));
216 CPUCore
*cc
= CPU_CORE(OBJECT(dev
));
217 const char *typename
= object_class_get_name(scc
->cpu_class
);
218 size_t size
= object_type_get_instance_size(typename
);
219 Error
*local_err
= NULL
;
223 sc
->threads
= g_malloc0(size
* cc
->nr_threads
);
224 for (i
= 0; i
< cc
->nr_threads
; i
++) {
229 obj
= sc
->threads
+ i
* size
;
231 object_initialize(obj
, size
, typename
);
233 cpu
= POWERPC_CPU(cs
);
234 cs
->cpu_index
= cc
->core_id
+ i
;
236 /* Set NUMA node for the threads belonged to core */
237 cpu
->node_id
= sc
->node_id
;
239 snprintf(id
, sizeof(id
), "thread[%d]", i
);
240 object_property_add_child(OBJECT(sc
), id
, obj
, &local_err
);
247 for (j
= 0; j
< cc
->nr_threads
; j
++) {
248 obj
= sc
->threads
+ j
* size
;
250 spapr_cpu_core_realize_child(obj
, &local_err
);
259 obj
= sc
->threads
+ i
* size
;
260 object_unparent(obj
);
263 error_propagate(errp
, local_err
);
266 static const char *spapr_core_models
[] = {
298 static Property spapr_cpu_core_properties
[] = {
299 DEFINE_PROP_INT32("node-id", sPAPRCPUCore
, node_id
, CPU_UNSET_NUMA_NODE_ID
),
300 DEFINE_PROP_END_OF_LIST()
303 void spapr_cpu_core_class_init(ObjectClass
*oc
, void *data
)
305 DeviceClass
*dc
= DEVICE_CLASS(oc
);
306 sPAPRCPUCoreClass
*scc
= SPAPR_CPU_CORE_CLASS(oc
);
308 dc
->realize
= spapr_cpu_core_realize
;
309 dc
->unrealize
= spapr_cpu_core_unrealizefn
;
310 dc
->props
= spapr_cpu_core_properties
;
311 scc
->cpu_class
= cpu_class_by_name(TYPE_POWERPC_CPU
, data
);
312 g_assert(scc
->cpu_class
);
315 static const TypeInfo spapr_cpu_core_type_info
= {
316 .name
= TYPE_SPAPR_CPU_CORE
,
317 .parent
= TYPE_CPU_CORE
,
319 .instance_size
= sizeof(sPAPRCPUCore
),
320 .class_size
= sizeof(sPAPRCPUCoreClass
),
323 static void spapr_cpu_core_register_types(void)
327 type_register_static(&spapr_cpu_core_type_info
);
329 for (i
= 0; i
< ARRAY_SIZE(spapr_core_models
); i
++) {
330 TypeInfo type_info
= {
331 .parent
= TYPE_SPAPR_CPU_CORE
,
332 .instance_size
= sizeof(sPAPRCPUCore
),
333 .class_init
= spapr_cpu_core_class_init
,
334 .class_data
= (void *) spapr_core_models
[i
],
337 type_info
.name
= g_strdup_printf("%s-" TYPE_SPAPR_CPU_CORE
,
338 spapr_core_models
[i
]);
339 type_register(&type_info
);
340 g_free((void *)type_info
.name
);
344 type_init(spapr_cpu_core_register_types
)