blob: ad1bcc79904f50680b993ef430ce96e7f476b64b [file] [log] [blame]
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +02001/*
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"
Cédric Le Goater24ece072016-10-22 11:46:41 +020022#include "qemu/log.h"
Thomas Huthfcf5ef22016-10-11 08:56:52 +020023#include "target/ppc/cpu.h"
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +020024#include "hw/ppc/ppc.h"
25#include "hw/ppc/pnv.h"
26#include "hw/ppc/pnv_core.h"
Cédric Le Goaterec575aa2016-11-07 19:03:02 +010027#include "hw/ppc/pnv_xscom.h"
Cédric Le Goater960fbd22017-04-03 09:46:03 +020028#include "hw/ppc/xics.h"
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +020029
Igor Mammedov35bdb9d2017-10-09 21:51:08 +020030static const char *pnv_core_cpu_typename(PnvCore *pc)
31{
32 const char *core_type = object_class_get_name(object_get_class(OBJECT(pc)));
33 int len = strlen(core_type) - strlen(PNV_CORE_TYPE_SUFFIX);
34 char *s = g_strdup_printf(POWERPC_CPU_TYPE_NAME("%.*s"), len, core_type);
35 const char *cpu_type = object_class_get_name(object_class_by_name(s));
36 g_free(s);
37 return cpu_type;
38}
39
Cédric Le Goaterb168a132017-12-15 14:56:01 +010040static void pnv_cpu_reset(void *opaque)
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +020041{
42 PowerPCCPU *cpu = opaque;
43 CPUState *cs = CPU(cpu);
44 CPUPPCState *env = &cpu->env;
45
46 cpu_reset(cs);
47
48 /*
49 * the skiboot firmware elects a primary thread to initialize the
50 * system and it can be any.
51 */
52 env->gpr[3] = PNV_FDT_ADDR;
53 env->nip = 0x10;
54 env->msr |= MSR_HVB; /* Hypervisor mode */
55}
56
Cédric Le Goater24ece072016-10-22 11:46:41 +020057/*
58 * These values are read by the PowerNV HW monitors under Linux
59 */
60#define PNV_XSCOM_EX_DTS_RESULT0 0x50000
61#define PNV_XSCOM_EX_DTS_RESULT1 0x50001
62
63static uint64_t pnv_core_xscom_read(void *opaque, hwaddr addr,
64 unsigned int width)
65{
66 uint32_t offset = addr >> 3;
67 uint64_t val = 0;
68
69 /* The result should be 38 C */
70 switch (offset) {
71 case PNV_XSCOM_EX_DTS_RESULT0:
72 val = 0x26f024f023f0000ull;
73 break;
74 case PNV_XSCOM_EX_DTS_RESULT1:
75 val = 0x24f000000000000ull;
76 break;
77 default:
Philippe Mathieu-Daudéc7e71a12018-06-08 13:15:33 +010078 qemu_log_mask(LOG_UNIMP, "Warning: reading reg=0x%" HWADDR_PRIx "\n",
Cédric Le Goater24ece072016-10-22 11:46:41 +020079 addr);
80 }
81
82 return val;
83}
84
85static void pnv_core_xscom_write(void *opaque, hwaddr addr, uint64_t val,
86 unsigned int width)
87{
Philippe Mathieu-Daudéc7e71a12018-06-08 13:15:33 +010088 qemu_log_mask(LOG_UNIMP, "Warning: writing to reg=0x%" HWADDR_PRIx "\n",
Cédric Le Goater24ece072016-10-22 11:46:41 +020089 addr);
90}
91
92static const MemoryRegionOps pnv_core_xscom_ops = {
93 .read = pnv_core_xscom_read,
94 .write = pnv_core_xscom_write,
95 .valid.min_access_size = 8,
96 .valid.max_access_size = 8,
97 .impl.min_access_size = 8,
98 .impl.max_access_size = 8,
99 .endianness = DEVICE_BIG_ENDIAN,
100};
101
Cédric Le Goaterd35aefa2018-06-15 17:25:33 +0200102static void pnv_realize_vcpu(PowerPCCPU *cpu, PnvChip *chip, Error **errp)
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200103{
David Gibson3a247522018-06-13 13:34:36 +1000104 CPUPPCState *env = &cpu->env;
105 int core_pir;
106 int thread_index = 0; /* TODO: TCG supports only one thread */
107 ppc_spr_t *pir = &env->spr_cb[SPR_PIR];
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200108 Error *local_err = NULL;
Cédric Le Goaterd35aefa2018-06-15 17:25:33 +0200109 PnvChipClass *pcc = PNV_CHIP_GET_CLASS(chip);
Cédric Le Goater960fbd22017-04-03 09:46:03 +0200110
David Gibson3a247522018-06-13 13:34:36 +1000111 object_property_set_bool(OBJECT(cpu), true, "realized", &local_err);
Cédric Le Goater960fbd22017-04-03 09:46:03 +0200112 if (local_err) {
113 error_propagate(errp, local_err);
114 return;
115 }
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200116
Cédric Le Goaterd35aefa2018-06-15 17:25:33 +0200117 cpu->intc = pcc->intc_create(chip, OBJECT(cpu), &local_err);
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200118 if (local_err) {
119 error_propagate(errp, local_err);
120 return;
121 }
122
David Gibson3a247522018-06-13 13:34:36 +1000123 core_pir = object_property_get_uint(OBJECT(cpu), "core-pir", &error_abort);
124
125 /*
126 * The PIR of a thread is the core PIR + the thread index. We will
127 * need to find a way to get the thread index when TCG supports
128 * more than 1. We could use the object name ?
129 */
130 pir->default_value = core_pir + thread_index;
131
132 /* Set time-base frequency to 512 MHz */
133 cpu_ppc_tb_init(env, PNV_TIMEBASE_FREQ);
134
135 qemu_register_reset(pnv_cpu_reset, cpu);
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200136}
137
138static void pnv_core_realize(DeviceState *dev, Error **errp)
139{
140 PnvCore *pc = PNV_CORE(OBJECT(dev));
141 CPUCore *cc = CPU_CORE(OBJECT(dev));
Igor Mammedov35bdb9d2017-10-09 21:51:08 +0200142 const char *typename = pnv_core_cpu_typename(pc);
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200143 Error *local_err = NULL;
144 void *obj;
145 int i, j;
146 char name[32];
Cédric Le Goaterd35aefa2018-06-15 17:25:33 +0200147 Object *chip;
Cédric Le Goater960fbd22017-04-03 09:46:03 +0200148
Cédric Le Goaterd35aefa2018-06-15 17:25:33 +0200149 chip = object_property_get_link(OBJECT(dev), "chip", &local_err);
150 if (!chip) {
Markus Armbruster4b576642018-10-17 10:26:25 +0200151 error_propagate_prepend(errp, local_err,
152 "required link 'chip' not found: ");
Cédric Le Goater56f68432018-06-26 16:22:14 +0200153 return;
Cédric Le Goater960fbd22017-04-03 09:46:03 +0200154 }
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200155
David Gibson08304a82018-06-13 11:57:37 +1000156 pc->threads = g_new(PowerPCCPU *, cc->nr_threads);
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200157 for (i = 0; i < cc->nr_threads; i++) {
David Gibson08304a82018-06-13 11:57:37 +1000158 obj = object_new(typename);
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200159
David Gibson08304a82018-06-13 11:57:37 +1000160 pc->threads[i] = POWERPC_CPU(obj);
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200161
162 snprintf(name, sizeof(name), "thread[%d]", i);
David Gibson937c2142018-06-13 11:55:31 +1000163 object_property_add_child(OBJECT(pc), name, obj, &error_abort);
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200164 object_property_add_alias(obj, "core-pir", OBJECT(pc),
David Gibson937c2142018-06-13 11:55:31 +1000165 "pir", &error_abort);
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200166 object_unref(obj);
167 }
168
169 for (j = 0; j < cc->nr_threads; j++) {
Cédric Le Goaterd35aefa2018-06-15 17:25:33 +0200170 pnv_realize_vcpu(pc->threads[j], PNV_CHIP(chip), &local_err);
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200171 if (local_err) {
172 goto err;
173 }
174 }
Cédric Le Goater24ece072016-10-22 11:46:41 +0200175
176 snprintf(name, sizeof(name), "xscom-core.%d", cc->core_id);
177 pnv_xscom_region_init(&pc->xscom_regs, OBJECT(dev), &pnv_core_xscom_ops,
Cédric Le Goaterc0358512018-01-15 19:04:04 +0100178 pc, name, PNV_XSCOM_EX_SIZE);
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200179 return;
180
181err:
182 while (--i >= 0) {
David Gibson08304a82018-06-13 11:57:37 +1000183 obj = OBJECT(pc->threads[i]);
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200184 object_unparent(obj);
185 }
186 g_free(pc->threads);
187 error_propagate(errp, local_err);
188}
189
David Gibson5e22e292018-06-13 12:08:42 +1000190static void pnv_unrealize_vcpu(PowerPCCPU *cpu)
191{
192 qemu_unregister_reset(pnv_cpu_reset, cpu);
193 object_unparent(cpu->intc);
194 cpu_remove_sync(CPU(cpu));
195 object_unparent(OBJECT(cpu));
196}
197
198static void pnv_core_unrealize(DeviceState *dev, Error **errp)
199{
200 PnvCore *pc = PNV_CORE(dev);
201 CPUCore *cc = CPU_CORE(dev);
202 int i;
203
204 for (i = 0; i < cc->nr_threads; i++) {
205 pnv_unrealize_vcpu(pc->threads[i]);
206 }
207 g_free(pc->threads);
208}
209
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200210static Property pnv_core_properties[] = {
211 DEFINE_PROP_UINT32("pir", PnvCore, pir, 0),
212 DEFINE_PROP_END_OF_LIST(),
213};
214
215static void pnv_core_class_init(ObjectClass *oc, void *data)
216{
217 DeviceClass *dc = DEVICE_CLASS(oc);
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200218
219 dc->realize = pnv_core_realize;
David Gibson5e22e292018-06-13 12:08:42 +1000220 dc->unrealize = pnv_core_unrealize;
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200221 dc->props = pnv_core_properties;
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200222}
223
Igor Mammedov7383af12017-10-09 21:51:09 +0200224#define DEFINE_PNV_CORE_TYPE(cpu_model) \
225 { \
226 .parent = TYPE_PNV_CORE, \
227 .name = PNV_CORE_TYPE_NAME(cpu_model), \
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200228 }
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200229
Igor Mammedov7383af12017-10-09 21:51:09 +0200230static const TypeInfo pnv_core_infos[] = {
231 {
232 .name = TYPE_PNV_CORE,
233 .parent = TYPE_CPU_CORE,
234 .instance_size = sizeof(PnvCore),
235 .class_size = sizeof(PnvCoreClass),
236 .class_init = pnv_core_class_init,
237 .abstract = true,
238 },
239 DEFINE_PNV_CORE_TYPE("power8e_v2.1"),
240 DEFINE_PNV_CORE_TYPE("power8_v2.0"),
241 DEFINE_PNV_CORE_TYPE("power8nvl_v1.0"),
242 DEFINE_PNV_CORE_TYPE("power9_v2.0"),
243};
Cédric Le Goaterd2fd9612016-10-22 11:46:39 +0200244
Igor Mammedov7383af12017-10-09 21:51:09 +0200245DEFINE_TYPES(pnv_core_infos)