2 * QEMU PowerPC PowerNV CPU Core model
4 * Copyright (c) 2016, IBM Corporation.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
20 #include "sysemu/sysemu.h"
21 #include "qapi/error.h"
22 #include "target-ppc/cpu.h"
23 #include "hw/ppc/ppc.h"
24 #include "hw/ppc/pnv.h"
25 #include "hw/ppc/pnv_core.h"
27 static void powernv_cpu_reset(void *opaque)
29 PowerPCCPU *cpu = opaque;
30 CPUState *cs = CPU(cpu);
31 CPUPPCState *env = &cpu->env;
36 * the skiboot firmware elects a primary thread to initialize the
37 * system and it can be any.
39 env->gpr[3] = PNV_FDT_ADDR;
41 env->msr |= MSR_HVB; /* Hypervisor mode */
44 static void powernv_cpu_init(PowerPCCPU *cpu, Error **errp)
46 CPUPPCState *env = &cpu->env;
48 int thread_index = 0; /* TODO: TCG supports only one thread */
49 ppc_spr_t *pir = &env->spr_cb[SPR_PIR];
51 core_pir = object_property_get_int(OBJECT(cpu), "core-pir", &error_abort);
54 * The PIR of a thread is the core PIR + the thread index. We will
55 * need to find a way to get the thread index when TCG supports
56 * more than 1. We could use the object name ?
58 pir->default_value = core_pir + thread_index;
60 /* Set time-base frequency to 512 MHz */
61 cpu_ppc_tb_init(env, PNV_TIMEBASE_FREQ);
63 qemu_register_reset(powernv_cpu_reset, cpu);
66 static void pnv_core_realize_child(Object *child, Error **errp)
68 Error *local_err = NULL;
69 CPUState *cs = CPU(child);
70 PowerPCCPU *cpu = POWERPC_CPU(cs);
72 object_property_set_bool(child, true, "realized", &local_err);
74 error_propagate(errp, local_err);
78 powernv_cpu_init(cpu, &local_err);
80 error_propagate(errp, local_err);
85 static void pnv_core_realize(DeviceState *dev, Error **errp)
87 PnvCore *pc = PNV_CORE(OBJECT(dev));
88 CPUCore *cc = CPU_CORE(OBJECT(dev));
89 PnvCoreClass *pcc = PNV_CORE_GET_CLASS(OBJECT(dev));
90 const char *typename = object_class_get_name(pcc->cpu_oc);
91 size_t size = object_type_get_instance_size(typename);
92 Error *local_err = NULL;
97 pc->threads = g_malloc0(size * cc->nr_threads);
98 for (i = 0; i < cc->nr_threads; i++) {
99 obj = pc->threads + i * size;
101 object_initialize(obj, size, typename);
103 snprintf(name, sizeof(name), "thread[%d]", i);
104 object_property_add_child(OBJECT(pc), name, obj, &local_err);
105 object_property_add_alias(obj, "core-pir", OBJECT(pc),
113 for (j = 0; j < cc->nr_threads; j++) {
114 obj = pc->threads + j * size;
116 pnv_core_realize_child(obj, &local_err);
125 obj = pc->threads + i * size;
126 object_unparent(obj);
129 error_propagate(errp, local_err);
132 static Property pnv_core_properties[] = {
133 DEFINE_PROP_UINT32("pir", PnvCore, pir, 0),
134 DEFINE_PROP_END_OF_LIST(),
137 static void pnv_core_class_init(ObjectClass *oc, void *data)
139 DeviceClass *dc = DEVICE_CLASS(oc);
140 PnvCoreClass *pcc = PNV_CORE_CLASS(oc);
142 dc->realize = pnv_core_realize;
143 dc->props = pnv_core_properties;
144 pcc->cpu_oc = cpu_class_by_name(TYPE_POWERPC_CPU, data);
147 static const TypeInfo pnv_core_info = {
148 .name = TYPE_PNV_CORE,
149 .parent = TYPE_CPU_CORE,
150 .instance_size = sizeof(PnvCore),
151 .class_size = sizeof(PnvCoreClass),
155 static const char *pnv_core_models[] = {
156 "POWER8E", "POWER8", "POWER8NVL", "POWER9"
159 static void pnv_core_register_types(void)
163 type_register_static(&pnv_core_info);
164 for (i = 0; i < ARRAY_SIZE(pnv_core_models); ++i) {
166 .parent = TYPE_PNV_CORE,
167 .instance_size = sizeof(PnvCore),
168 .class_init = pnv_core_class_init,
169 .class_data = (void *) pnv_core_models[i],
171 ti.name = pnv_core_typename(pnv_core_models[i]);
173 g_free((void *)ti.name);
177 type_init(pnv_core_register_types)
179 char *pnv_core_typename(const char *model)
181 return g_strdup_printf(TYPE_PNV_CORE "-%s", model);