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 "qemu/log.h" 23 #include "target/ppc/cpu.h" 24 #include "hw/ppc/ppc.h" 25 #include "hw/ppc/pnv.h" 26 #include "hw/ppc/pnv_core.h" 27 #include "hw/ppc/pnv_xscom.h" 28 #include "hw/ppc/xics.h" 29 30 static void powernv_cpu_reset(void *opaque) 31 { 32 PowerPCCPU *cpu = opaque; 33 CPUState *cs = CPU(cpu); 34 CPUPPCState *env = &cpu->env; 35 36 cpu_reset(cs); 37 38 /* 39 * the skiboot firmware elects a primary thread to initialize the 40 * system and it can be any. 41 */ 42 env->gpr[3] = PNV_FDT_ADDR; 43 env->nip = 0x10; 44 env->msr |= MSR_HVB; /* Hypervisor mode */ 45 } 46 47 static void powernv_cpu_init(PowerPCCPU *cpu, Error **errp) 48 { 49 CPUPPCState *env = &cpu->env; 50 int core_pir; 51 int thread_index = 0; /* TODO: TCG supports only one thread */ 52 ppc_spr_t *pir = &env->spr_cb[SPR_PIR]; 53 54 core_pir = object_property_get_int(OBJECT(cpu), "core-pir", &error_abort); 55 56 /* 57 * The PIR of a thread is the core PIR + the thread index. We will 58 * need to find a way to get the thread index when TCG supports 59 * more than 1. We could use the object name ? 60 */ 61 pir->default_value = core_pir + thread_index; 62 63 /* Set time-base frequency to 512 MHz */ 64 cpu_ppc_tb_init(env, PNV_TIMEBASE_FREQ); 65 66 qemu_register_reset(powernv_cpu_reset, cpu); 67 } 68 69 /* 70 * These values are read by the PowerNV HW monitors under Linux 71 */ 72 #define PNV_XSCOM_EX_DTS_RESULT0 0x50000 73 #define PNV_XSCOM_EX_DTS_RESULT1 0x50001 74 75 static uint64_t pnv_core_xscom_read(void *opaque, hwaddr addr, 76 unsigned int width) 77 { 78 uint32_t offset = addr >> 3; 79 uint64_t val = 0; 80 81 /* The result should be 38 C */ 82 switch (offset) { 83 case PNV_XSCOM_EX_DTS_RESULT0: 84 val = 0x26f024f023f0000ull; 85 break; 86 case PNV_XSCOM_EX_DTS_RESULT1: 87 val = 0x24f000000000000ull; 88 break; 89 default: 90 qemu_log_mask(LOG_UNIMP, "Warning: reading reg=0x%" HWADDR_PRIx, 91 addr); 92 } 93 94 return val; 95 } 96 97 static void pnv_core_xscom_write(void *opaque, hwaddr addr, uint64_t val, 98 unsigned int width) 99 { 100 qemu_log_mask(LOG_UNIMP, "Warning: writing to reg=0x%" HWADDR_PRIx, 101 addr); 102 } 103 104 static const MemoryRegionOps pnv_core_xscom_ops = { 105 .read = pnv_core_xscom_read, 106 .write = pnv_core_xscom_write, 107 .valid.min_access_size = 8, 108 .valid.max_access_size = 8, 109 .impl.min_access_size = 8, 110 .impl.max_access_size = 8, 111 .endianness = DEVICE_BIG_ENDIAN, 112 }; 113 114 static void pnv_core_realize_child(Object *child, XICSFabric *xi, Error **errp) 115 { 116 Error *local_err = NULL; 117 CPUState *cs = CPU(child); 118 PowerPCCPU *cpu = POWERPC_CPU(cs); 119 Object *obj; 120 121 obj = object_new(TYPE_PNV_ICP); 122 object_property_add_child(OBJECT(cpu), "icp", obj, &error_abort); 123 object_unref(obj); 124 object_property_add_const_link(obj, ICP_PROP_XICS, OBJECT(xi), 125 &error_abort); 126 object_property_set_bool(obj, true, "realized", &local_err); 127 if (local_err) { 128 error_propagate(errp, local_err); 129 return; 130 } 131 132 object_property_set_bool(child, true, "realized", &local_err); 133 if (local_err) { 134 object_unparent(obj); 135 error_propagate(errp, local_err); 136 return; 137 } 138 139 powernv_cpu_init(cpu, &local_err); 140 if (local_err) { 141 object_unparent(obj); 142 error_propagate(errp, local_err); 143 return; 144 } 145 146 xics_cpu_setup(xi, cpu, ICP(obj)); 147 } 148 149 static void pnv_core_realize(DeviceState *dev, Error **errp) 150 { 151 PnvCore *pc = PNV_CORE(OBJECT(dev)); 152 CPUCore *cc = CPU_CORE(OBJECT(dev)); 153 PnvCoreClass *pcc = PNV_CORE_GET_CLASS(OBJECT(dev)); 154 const char *typename = object_class_get_name(pcc->cpu_oc); 155 size_t size = object_type_get_instance_size(typename); 156 Error *local_err = NULL; 157 void *obj; 158 int i, j; 159 char name[32]; 160 Object *xi; 161 162 xi = object_property_get_link(OBJECT(dev), "xics", &local_err); 163 if (!xi) { 164 error_setg(errp, "%s: required link 'xics' not found: %s", 165 __func__, error_get_pretty(local_err)); 166 return; 167 } 168 169 pc->threads = g_malloc0(size * cc->nr_threads); 170 for (i = 0; i < cc->nr_threads; i++) { 171 obj = pc->threads + i * size; 172 173 object_initialize(obj, size, typename); 174 175 snprintf(name, sizeof(name), "thread[%d]", i); 176 object_property_add_child(OBJECT(pc), name, obj, &local_err); 177 object_property_add_alias(obj, "core-pir", OBJECT(pc), 178 "pir", &local_err); 179 if (local_err) { 180 goto err; 181 } 182 object_unref(obj); 183 } 184 185 for (j = 0; j < cc->nr_threads; j++) { 186 obj = pc->threads + j * size; 187 188 pnv_core_realize_child(obj, XICS_FABRIC(xi), &local_err); 189 if (local_err) { 190 goto err; 191 } 192 } 193 194 snprintf(name, sizeof(name), "xscom-core.%d", cc->core_id); 195 pnv_xscom_region_init(&pc->xscom_regs, OBJECT(dev), &pnv_core_xscom_ops, 196 pc, name, PNV_XSCOM_EX_CORE_SIZE); 197 return; 198 199 err: 200 while (--i >= 0) { 201 obj = pc->threads + i * size; 202 object_unparent(obj); 203 } 204 g_free(pc->threads); 205 error_propagate(errp, local_err); 206 } 207 208 static Property pnv_core_properties[] = { 209 DEFINE_PROP_UINT32("pir", PnvCore, pir, 0), 210 DEFINE_PROP_END_OF_LIST(), 211 }; 212 213 static void pnv_core_class_init(ObjectClass *oc, void *data) 214 { 215 DeviceClass *dc = DEVICE_CLASS(oc); 216 PnvCoreClass *pcc = PNV_CORE_CLASS(oc); 217 218 dc->realize = pnv_core_realize; 219 dc->props = pnv_core_properties; 220 pcc->cpu_oc = cpu_class_by_name(TYPE_POWERPC_CPU, data); 221 } 222 223 static const TypeInfo pnv_core_info = { 224 .name = TYPE_PNV_CORE, 225 .parent = TYPE_CPU_CORE, 226 .instance_size = sizeof(PnvCore), 227 .class_size = sizeof(PnvCoreClass), 228 .abstract = true, 229 }; 230 231 static const char *pnv_core_models[] = { 232 "POWER8E", "POWER8", "POWER8NVL", "POWER9" 233 }; 234 235 static void pnv_core_register_types(void) 236 { 237 int i ; 238 239 type_register_static(&pnv_core_info); 240 for (i = 0; i < ARRAY_SIZE(pnv_core_models); ++i) { 241 TypeInfo ti = { 242 .parent = TYPE_PNV_CORE, 243 .instance_size = sizeof(PnvCore), 244 .class_init = pnv_core_class_init, 245 .class_data = (void *) pnv_core_models[i], 246 }; 247 ti.name = pnv_core_typename(pnv_core_models[i]); 248 type_register(&ti); 249 g_free((void *)ti.name); 250 } 251 } 252 253 type_init(pnv_core_register_types) 254 255 char *pnv_core_typename(const char *model) 256 { 257 return g_strdup_printf(TYPE_PNV_CORE "-%s", model); 258 } 259