]> Git Repo - qemu.git/blob - hw/ppc/pnv_core.c
ppc/pnv: add a PnvCore object
[qemu.git] / hw / ppc / pnv_core.c
1 /*
2  * QEMU PowerPC PowerNV CPU Core model
3  *
4  * Copyright (c) 2016, IBM Corporation.
5  *
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.
10  *
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.
15  *
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/>.
18  */
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"
26
27 static void powernv_cpu_reset(void *opaque)
28 {
29     PowerPCCPU *cpu = opaque;
30     CPUState *cs = CPU(cpu);
31     CPUPPCState *env = &cpu->env;
32
33     cpu_reset(cs);
34
35     /*
36      * the skiboot firmware elects a primary thread to initialize the
37      * system and it can be any.
38      */
39     env->gpr[3] = PNV_FDT_ADDR;
40     env->nip = 0x10;
41     env->msr |= MSR_HVB; /* Hypervisor mode */
42 }
43
44 static void powernv_cpu_init(PowerPCCPU *cpu, Error **errp)
45 {
46     CPUPPCState *env = &cpu->env;
47     int core_pir;
48     int thread_index = 0; /* TODO: TCG supports only one thread */
49     ppc_spr_t *pir = &env->spr_cb[SPR_PIR];
50
51     core_pir = object_property_get_int(OBJECT(cpu), "core-pir", &error_abort);
52
53     /*
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 ?
57      */
58     pir->default_value = core_pir + thread_index;
59
60     /* Set time-base frequency to 512 MHz */
61     cpu_ppc_tb_init(env, PNV_TIMEBASE_FREQ);
62
63     qemu_register_reset(powernv_cpu_reset, cpu);
64 }
65
66 static void pnv_core_realize_child(Object *child, Error **errp)
67 {
68     Error *local_err = NULL;
69     CPUState *cs = CPU(child);
70     PowerPCCPU *cpu = POWERPC_CPU(cs);
71
72     object_property_set_bool(child, true, "realized", &local_err);
73     if (local_err) {
74         error_propagate(errp, local_err);
75         return;
76     }
77
78     powernv_cpu_init(cpu, &local_err);
79     if (local_err) {
80         error_propagate(errp, local_err);
81         return;
82     }
83 }
84
85 static void pnv_core_realize(DeviceState *dev, Error **errp)
86 {
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;
93     void *obj;
94     int i, j;
95     char name[32];
96
97     pc->threads = g_malloc0(size * cc->nr_threads);
98     for (i = 0; i < cc->nr_threads; i++) {
99         obj = pc->threads + i * size;
100
101         object_initialize(obj, size, typename);
102
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),
106                                   "pir", &local_err);
107         if (local_err) {
108             goto err;
109         }
110         object_unref(obj);
111     }
112
113     for (j = 0; j < cc->nr_threads; j++) {
114         obj = pc->threads + j * size;
115
116         pnv_core_realize_child(obj, &local_err);
117         if (local_err) {
118             goto err;
119         }
120     }
121     return;
122
123 err:
124     while (--i >= 0) {
125         obj = pc->threads + i * size;
126         object_unparent(obj);
127     }
128     g_free(pc->threads);
129     error_propagate(errp, local_err);
130 }
131
132 static Property pnv_core_properties[] = {
133     DEFINE_PROP_UINT32("pir", PnvCore, pir, 0),
134     DEFINE_PROP_END_OF_LIST(),
135 };
136
137 static void pnv_core_class_init(ObjectClass *oc, void *data)
138 {
139     DeviceClass *dc = DEVICE_CLASS(oc);
140     PnvCoreClass *pcc = PNV_CORE_CLASS(oc);
141
142     dc->realize = pnv_core_realize;
143     dc->props = pnv_core_properties;
144     pcc->cpu_oc = cpu_class_by_name(TYPE_POWERPC_CPU, data);
145 }
146
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),
152     .abstract       = true,
153 };
154
155 static const char *pnv_core_models[] = {
156     "POWER8E", "POWER8", "POWER8NVL", "POWER9"
157 };
158
159 static void pnv_core_register_types(void)
160 {
161     int i ;
162
163     type_register_static(&pnv_core_info);
164     for (i = 0; i < ARRAY_SIZE(pnv_core_models); ++i) {
165         TypeInfo ti = {
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],
170         };
171         ti.name = pnv_core_typename(pnv_core_models[i]);
172         type_register(&ti);
173         g_free((void *)ti.name);
174     }
175 }
176
177 type_init(pnv_core_register_types)
178
179 char *pnv_core_typename(const char *model)
180 {
181     return g_strdup_printf(TYPE_PNV_CORE "-%s", model);
182 }
This page took 0.033713 seconds and 4 git commands to generate.