1 /* 2 * sPAPR CPU core device, acts as container of CPU thread devices. 3 * 4 * Copyright (C) 2016 Bharata B Rao <bharata@linux.vnet.ibm.com> 5 * 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. 8 */ 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 "target-ppc/kvm_ppc.h" 17 #include "hw/ppc/ppc.h" 18 #include "target-ppc/mmu-hash64.h" 19 #include "sysemu/numa.h" 20 21 static void spapr_cpu_reset(void *opaque) 22 { 23 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); 24 PowerPCCPU *cpu = opaque; 25 CPUState *cs = CPU(cpu); 26 CPUPPCState *env = &cpu->env; 27 28 cpu_reset(cs); 29 30 /* All CPUs start halted. CPU0 is unhalted from the machine level 31 * reset code and the rest are explicitly started up by the guest 32 * using an RTAS call */ 33 cs->halted = 1; 34 35 env->spr[SPR_HIOR] = 0; 36 37 ppc_hash64_set_external_hpt(cpu, spapr->htab, spapr->htab_shift, 38 &error_fatal); 39 } 40 41 static void spapr_cpu_destroy(PowerPCCPU *cpu) 42 { 43 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); 44 45 xics_cpu_destroy(spapr->xics, cpu); 46 qemu_unregister_reset(spapr_cpu_reset, cpu); 47 } 48 49 void spapr_cpu_init(sPAPRMachineState *spapr, PowerPCCPU *cpu, Error **errp) 50 { 51 CPUPPCState *env = &cpu->env; 52 CPUState *cs = CPU(cpu); 53 int i; 54 55 /* Set time-base frequency to 512 MHz */ 56 cpu_ppc_tb_init(env, SPAPR_TIMEBASE_FREQ); 57 58 /* Enable PAPR mode in TCG or KVM */ 59 cpu_ppc_set_papr(cpu); 60 61 if (cpu->max_compat) { 62 Error *local_err = NULL; 63 64 ppc_set_compat(cpu, cpu->max_compat, &local_err); 65 if (local_err) { 66 error_propagate(errp, local_err); 67 return; 68 } 69 } 70 71 /* Set NUMA node for the added CPUs */ 72 for (i = 0; i < nb_numa_nodes; i++) { 73 if (test_bit(cs->cpu_index, numa_info[i].node_cpu)) { 74 cs->numa_node = i; 75 break; 76 } 77 } 78 79 xics_cpu_setup(spapr->xics, cpu); 80 81 qemu_register_reset(spapr_cpu_reset, cpu); 82 spapr_cpu_reset(cpu); 83 } 84 85 /* 86 * Return the sPAPR CPU core type for @model which essentially is the CPU 87 * model specified with -cpu cmdline option. 88 */ 89 char *spapr_get_cpu_core_type(const char *model) 90 { 91 char *core_type; 92 gchar **model_pieces = g_strsplit(model, ",", 2); 93 94 core_type = g_strdup_printf("%s-%s", model_pieces[0], TYPE_SPAPR_CPU_CORE); 95 g_strfreev(model_pieces); 96 return core_type; 97 } 98 99 static void spapr_core_release(DeviceState *dev, void *opaque) 100 { 101 sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev)); 102 const char *typename = object_class_get_name(sc->cpu_class); 103 size_t size = object_type_get_instance_size(typename); 104 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); 105 CPUCore *cc = CPU_CORE(dev); 106 int i; 107 108 for (i = 0; i < cc->nr_threads; i++) { 109 void *obj = sc->threads + i * size; 110 DeviceState *dev = DEVICE(obj); 111 CPUState *cs = CPU(dev); 112 PowerPCCPU *cpu = POWERPC_CPU(cs); 113 114 spapr_cpu_destroy(cpu); 115 cpu_remove_sync(cs); 116 object_unparent(obj); 117 } 118 119 spapr->cores[cc->core_id / smp_threads] = NULL; 120 121 g_free(sc->threads); 122 object_unparent(OBJECT(dev)); 123 } 124 125 void spapr_core_unplug(HotplugHandler *hotplug_dev, DeviceState *dev, 126 Error **errp) 127 { 128 CPUCore *cc = CPU_CORE(dev); 129 int smt = kvmppc_smt_threads(); 130 int index = cc->core_id / smp_threads; 131 sPAPRDRConnector *drc = 132 spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index * smt); 133 sPAPRDRConnectorClass *drck; 134 Error *local_err = NULL; 135 136 if (index == 0) { 137 error_setg(errp, "Boot CPU core may not be unplugged"); 138 return; 139 } 140 141 g_assert(drc); 142 143 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 144 drck->detach(drc, dev, spapr_core_release, NULL, &local_err); 145 if (local_err) { 146 error_propagate(errp, local_err); 147 return; 148 } 149 150 spapr_hotplug_req_remove_by_index(drc); 151 } 152 153 void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev, 154 Error **errp) 155 { 156 sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(OBJECT(hotplug_dev)); 157 sPAPRMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev)); 158 sPAPRCPUCore *core = SPAPR_CPU_CORE(OBJECT(dev)); 159 CPUCore *cc = CPU_CORE(dev); 160 CPUState *cs = CPU(core->threads); 161 sPAPRDRConnector *drc; 162 sPAPRDRConnectorClass *drck; 163 Error *local_err = NULL; 164 void *fdt = NULL; 165 int fdt_offset = 0; 166 int index = cc->core_id / smp_threads; 167 int smt = kvmppc_smt_threads(); 168 169 drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index * smt); 170 spapr->cores[index] = OBJECT(dev); 171 172 if (!smc->dr_cpu_enabled) { 173 /* 174 * This is a cold plugged CPU core but the machine doesn't support 175 * DR. So skip the hotplug path ensuring that the core is brought 176 * up online with out an associated DR connector. 177 */ 178 return; 179 } 180 181 g_assert(drc); 182 183 /* 184 * Setup CPU DT entries only for hotplugged CPUs. For boot time or 185 * coldplugged CPUs DT entries are setup in spapr_finalize_fdt(). 186 */ 187 if (dev->hotplugged) { 188 fdt = spapr_populate_hotplug_cpu_dt(cs, &fdt_offset, spapr); 189 } 190 191 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 192 drck->attach(drc, dev, fdt, fdt_offset, !dev->hotplugged, &local_err); 193 if (local_err) { 194 g_free(fdt); 195 spapr->cores[index] = NULL; 196 error_propagate(errp, local_err); 197 return; 198 } 199 200 if (dev->hotplugged) { 201 /* 202 * Send hotplug notification interrupt to the guest only in case 203 * of hotplugged CPUs. 204 */ 205 spapr_hotplug_req_add_by_index(drc); 206 } else { 207 /* 208 * Set the right DRC states for cold plugged CPU. 209 */ 210 drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_USABLE); 211 drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_UNISOLATED); 212 } 213 } 214 215 void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev, 216 Error **errp) 217 { 218 MachineState *machine = MACHINE(OBJECT(hotplug_dev)); 219 sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(OBJECT(hotplug_dev)); 220 sPAPRMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev)); 221 int spapr_max_cores = max_cpus / smp_threads; 222 int index; 223 Error *local_err = NULL; 224 CPUCore *cc = CPU_CORE(dev); 225 char *base_core_type = spapr_get_cpu_core_type(machine->cpu_model); 226 const char *type = object_get_typename(OBJECT(dev)); 227 228 if (strcmp(base_core_type, type)) { 229 error_setg(&local_err, "CPU core type should be %s", base_core_type); 230 goto out; 231 } 232 233 if (!smc->dr_cpu_enabled && dev->hotplugged) { 234 error_setg(&local_err, "CPU hotplug not supported for this machine"); 235 goto out; 236 } 237 238 if (cc->nr_threads != smp_threads) { 239 error_setg(&local_err, "threads must be %d", smp_threads); 240 goto out; 241 } 242 243 if (cc->core_id % smp_threads) { 244 error_setg(&local_err, "invalid core id %d\n", cc->core_id); 245 goto out; 246 } 247 248 index = cc->core_id / smp_threads; 249 if (index < 0 || index >= spapr_max_cores) { 250 error_setg(&local_err, "core id %d out of range", cc->core_id); 251 goto out; 252 } 253 254 if (spapr->cores[index]) { 255 error_setg(&local_err, "core %d already populated", cc->core_id); 256 goto out; 257 } 258 259 out: 260 g_free(base_core_type); 261 error_propagate(errp, local_err); 262 } 263 264 static void spapr_cpu_core_realize_child(Object *child, Error **errp) 265 { 266 Error *local_err = NULL; 267 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); 268 CPUState *cs = CPU(child); 269 PowerPCCPU *cpu = POWERPC_CPU(cs); 270 271 object_property_set_bool(child, true, "realized", &local_err); 272 if (local_err) { 273 error_propagate(errp, local_err); 274 return; 275 } 276 277 spapr_cpu_init(spapr, cpu, &local_err); 278 if (local_err) { 279 error_propagate(errp, local_err); 280 return; 281 } 282 } 283 284 static void spapr_cpu_core_realize(DeviceState *dev, Error **errp) 285 { 286 sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev)); 287 CPUCore *cc = CPU_CORE(OBJECT(dev)); 288 const char *typename = object_class_get_name(sc->cpu_class); 289 size_t size = object_type_get_instance_size(typename); 290 Error *local_err = NULL; 291 void *obj; 292 int i, j; 293 294 sc->threads = g_malloc0(size * cc->nr_threads); 295 for (i = 0; i < cc->nr_threads; i++) { 296 char id[32]; 297 CPUState *cs; 298 299 obj = sc->threads + i * size; 300 301 object_initialize(obj, size, typename); 302 cs = CPU(obj); 303 cs->cpu_index = cc->core_id + i; 304 snprintf(id, sizeof(id), "thread[%d]", i); 305 object_property_add_child(OBJECT(sc), id, obj, &local_err); 306 if (local_err) { 307 goto err; 308 } 309 object_unref(obj); 310 } 311 312 for (j = 0; j < cc->nr_threads; j++) { 313 obj = sc->threads + j * size; 314 315 spapr_cpu_core_realize_child(obj, &local_err); 316 if (local_err) { 317 goto err; 318 } 319 } 320 return; 321 322 err: 323 while (--i >= 0) { 324 obj = sc->threads + i * size; 325 object_unparent(obj); 326 } 327 g_free(sc->threads); 328 error_propagate(errp, local_err); 329 } 330 331 static void spapr_cpu_core_class_init(ObjectClass *oc, void *data) 332 { 333 DeviceClass *dc = DEVICE_CLASS(oc); 334 dc->realize = spapr_cpu_core_realize; 335 } 336 337 /* 338 * instance_init routines from different flavours of sPAPR CPU cores. 339 */ 340 #define SPAPR_CPU_CORE_INITFN(_type, _fname) \ 341 static void glue(glue(spapr_cpu_core_, _fname), _initfn(Object *obj)) \ 342 { \ 343 sPAPRCPUCore *core = SPAPR_CPU_CORE(obj); \ 344 char *name = g_strdup_printf("%s-" TYPE_POWERPC_CPU, stringify(_type)); \ 345 ObjectClass *oc = object_class_by_name(name); \ 346 g_assert(oc); \ 347 g_free((void *)name); \ 348 core->cpu_class = oc; \ 349 } 350 351 SPAPR_CPU_CORE_INITFN(970mp_v1.0, 970MP_v10); 352 SPAPR_CPU_CORE_INITFN(970mp_v1.1, 970MP_v11); 353 SPAPR_CPU_CORE_INITFN(970_v2.2, 970); 354 SPAPR_CPU_CORE_INITFN(POWER5+_v2.1, POWER5plus); 355 SPAPR_CPU_CORE_INITFN(POWER7_v2.3, POWER7); 356 SPAPR_CPU_CORE_INITFN(POWER7+_v2.1, POWER7plus); 357 SPAPR_CPU_CORE_INITFN(POWER8_v2.0, POWER8); 358 SPAPR_CPU_CORE_INITFN(POWER8E_v2.1, POWER8E); 359 SPAPR_CPU_CORE_INITFN(POWER8NVL_v1.0, POWER8NVL); 360 361 typedef struct SPAPRCoreInfo { 362 const char *name; 363 void (*initfn)(Object *obj); 364 } SPAPRCoreInfo; 365 366 static const SPAPRCoreInfo spapr_cores[] = { 367 /* 970 and aliaes */ 368 { .name = "970_v2.2", .initfn = spapr_cpu_core_970_initfn }, 369 { .name = "970", .initfn = spapr_cpu_core_970_initfn }, 370 371 /* 970MP variants and aliases */ 372 { .name = "970MP_v1.0", .initfn = spapr_cpu_core_970MP_v10_initfn }, 373 { .name = "970mp_v1.0", .initfn = spapr_cpu_core_970MP_v10_initfn }, 374 { .name = "970MP_v1.1", .initfn = spapr_cpu_core_970MP_v11_initfn }, 375 { .name = "970mp_v1.1", .initfn = spapr_cpu_core_970MP_v11_initfn }, 376 { .name = "970mp", .initfn = spapr_cpu_core_970MP_v11_initfn }, 377 378 /* POWER5 and aliases */ 379 { .name = "POWER5+_v2.1", .initfn = spapr_cpu_core_POWER5plus_initfn }, 380 { .name = "POWER5+", .initfn = spapr_cpu_core_POWER5plus_initfn }, 381 382 /* POWER7 and aliases */ 383 { .name = "POWER7_v2.3", .initfn = spapr_cpu_core_POWER7_initfn }, 384 { .name = "POWER7", .initfn = spapr_cpu_core_POWER7_initfn }, 385 386 /* POWER7+ and aliases */ 387 { .name = "POWER7+_v2.1", .initfn = spapr_cpu_core_POWER7plus_initfn }, 388 { .name = "POWER7+", .initfn = spapr_cpu_core_POWER7plus_initfn }, 389 390 /* POWER8 and aliases */ 391 { .name = "POWER8_v2.0", .initfn = spapr_cpu_core_POWER8_initfn }, 392 { .name = "POWER8", .initfn = spapr_cpu_core_POWER8_initfn }, 393 { .name = "power8", .initfn = spapr_cpu_core_POWER8_initfn }, 394 395 /* POWER8E and aliases */ 396 { .name = "POWER8E_v2.1", .initfn = spapr_cpu_core_POWER8E_initfn }, 397 { .name = "POWER8E", .initfn = spapr_cpu_core_POWER8E_initfn }, 398 399 /* POWER8NVL and aliases */ 400 { .name = "POWER8NVL_v1.0", .initfn = spapr_cpu_core_POWER8NVL_initfn }, 401 { .name = "POWER8NVL", .initfn = spapr_cpu_core_POWER8NVL_initfn }, 402 403 { .name = NULL } 404 }; 405 406 static void spapr_cpu_core_register(const SPAPRCoreInfo *info) 407 { 408 TypeInfo type_info = { 409 .parent = TYPE_SPAPR_CPU_CORE, 410 .instance_size = sizeof(sPAPRCPUCore), 411 .instance_init = info->initfn, 412 }; 413 414 type_info.name = g_strdup_printf("%s-" TYPE_SPAPR_CPU_CORE, info->name); 415 type_register(&type_info); 416 g_free((void *)type_info.name); 417 } 418 419 static const TypeInfo spapr_cpu_core_type_info = { 420 .name = TYPE_SPAPR_CPU_CORE, 421 .parent = TYPE_CPU_CORE, 422 .abstract = true, 423 .instance_size = sizeof(sPAPRCPUCore), 424 .class_init = spapr_cpu_core_class_init, 425 }; 426 427 static void spapr_cpu_core_register_types(void) 428 { 429 const SPAPRCoreInfo *info = spapr_cores; 430 431 type_register_static(&spapr_cpu_core_type_info); 432 while (info->name) { 433 spapr_cpu_core_register(info); 434 info++; 435 } 436 } 437 438 type_init(spapr_cpu_core_register_types) 439