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 "sysemu/kvm.h" 17 #include "target/ppc/kvm_ppc.h" 18 #include "hw/ppc/ppc.h" 19 #include "target/ppc/mmu-hash64.h" 20 #include "sysemu/numa.h" 21 #include "sysemu/hw_accel.h" 22 #include "qemu/error-report.h" 23 24 static void spapr_cpu_reset(void *opaque) 25 { 26 PowerPCCPU *cpu = opaque; 27 CPUState *cs = CPU(cpu); 28 CPUPPCState *env = &cpu->env; 29 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 30 31 cpu_reset(cs); 32 33 /* All CPUs start halted. CPU0 is unhalted from the machine level 34 * reset code and the rest are explicitly started up by the guest 35 * using an RTAS call */ 36 cs->halted = 1; 37 38 env->spr[SPR_HIOR] = 0; 39 40 /* Disable Power-saving mode Exit Cause exceptions for the CPU. 41 * This can cause issues when rebooting the guest if a secondary 42 * is awaken */ 43 if (cs != first_cpu) { 44 env->spr[SPR_LPCR] &= ~pcc->lpcr_pm; 45 } 46 } 47 48 static void spapr_cpu_destroy(PowerPCCPU *cpu) 49 { 50 qemu_unregister_reset(spapr_cpu_reset, cpu); 51 } 52 53 static void spapr_cpu_init(sPAPRMachineState *spapr, PowerPCCPU *cpu, 54 Error **errp) 55 { 56 CPUPPCState *env = &cpu->env; 57 58 /* Set time-base frequency to 512 MHz */ 59 cpu_ppc_tb_init(env, SPAPR_TIMEBASE_FREQ); 60 61 /* Enable PAPR mode in TCG or KVM */ 62 cpu_ppc_set_papr(cpu, PPC_VIRTUAL_HYPERVISOR(spapr)); 63 64 qemu_register_reset(spapr_cpu_reset, cpu); 65 spapr_cpu_reset(cpu); 66 } 67 68 /* 69 * Return the sPAPR CPU core type for @model which essentially is the CPU 70 * model specified with -cpu cmdline option. 71 */ 72 const char *spapr_get_cpu_core_type(const char *cpu_type) 73 { 74 int len = strlen(cpu_type) - strlen(POWERPC_CPU_TYPE_SUFFIX); 75 char *core_type = g_strdup_printf(SPAPR_CPU_CORE_TYPE_NAME("%.*s"), 76 len, cpu_type); 77 ObjectClass *oc = object_class_by_name(core_type); 78 79 g_free(core_type); 80 if (!oc) { 81 return NULL; 82 } 83 84 return object_class_get_name(oc); 85 } 86 87 static void spapr_cpu_core_unrealizefn(DeviceState *dev, Error **errp) 88 { 89 sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev)); 90 CPUCore *cc = CPU_CORE(dev); 91 int i; 92 93 for (i = 0; i < cc->nr_threads; i++) { 94 Object *obj = OBJECT(sc->threads[i]); 95 DeviceState *dev = DEVICE(obj); 96 CPUState *cs = CPU(dev); 97 PowerPCCPU *cpu = POWERPC_CPU(cs); 98 99 spapr_cpu_destroy(cpu); 100 object_unparent(cpu->intc); 101 cpu_remove_sync(cs); 102 object_unparent(obj); 103 } 104 g_free(sc->threads); 105 } 106 107 static void spapr_cpu_core_realize_child(Object *child, 108 sPAPRMachineState *spapr, Error **errp) 109 { 110 Error *local_err = NULL; 111 CPUState *cs = CPU(child); 112 PowerPCCPU *cpu = POWERPC_CPU(cs); 113 114 object_property_set_bool(child, true, "realized", &local_err); 115 if (local_err) { 116 goto error; 117 } 118 119 spapr_cpu_init(spapr, cpu, &local_err); 120 if (local_err) { 121 goto error; 122 } 123 124 cpu->intc = icp_create(child, spapr->icp_type, XICS_FABRIC(spapr), 125 &local_err); 126 if (local_err) { 127 goto error; 128 } 129 130 return; 131 132 error: 133 error_propagate(errp, local_err); 134 } 135 136 static void spapr_cpu_core_realize(DeviceState *dev, Error **errp) 137 { 138 /* We don't use SPAPR_MACHINE() in order to exit gracefully if the user 139 * tries to add a sPAPR CPU core to a non-pseries machine. 140 */ 141 sPAPRMachineState *spapr = 142 (sPAPRMachineState *) object_dynamic_cast(qdev_get_machine(), 143 TYPE_SPAPR_MACHINE); 144 sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev)); 145 sPAPRCPUCoreClass *scc = SPAPR_CPU_CORE_GET_CLASS(OBJECT(dev)); 146 CPUCore *cc = CPU_CORE(OBJECT(dev)); 147 Error *local_err = NULL; 148 Object *obj; 149 int i, j; 150 151 if (!spapr) { 152 error_setg(errp, TYPE_SPAPR_CPU_CORE " needs a pseries machine"); 153 return; 154 } 155 156 sc->threads = g_new(PowerPCCPU *, cc->nr_threads); 157 for (i = 0; i < cc->nr_threads; i++) { 158 char id[32]; 159 CPUState *cs; 160 PowerPCCPU *cpu; 161 162 obj = object_new(scc->cpu_type); 163 164 cs = CPU(obj); 165 cpu = sc->threads[i] = POWERPC_CPU(obj); 166 cs->cpu_index = cc->core_id + i; 167 cpu->vcpu_id = (cc->core_id * spapr->vsmt / smp_threads) + i; 168 if (kvm_enabled() && !kvm_vcpu_id_is_valid(cpu->vcpu_id)) { 169 error_setg(&local_err, "Can't create CPU with id %d in KVM", 170 cpu->vcpu_id); 171 error_append_hint(&local_err, "Adjust the number of cpus to %d " 172 "or try to raise the number of threads per core\n", 173 cpu->vcpu_id * smp_threads / spapr->vsmt); 174 goto err; 175 } 176 177 178 /* Set NUMA node for the threads belonged to core */ 179 cpu->node_id = sc->node_id; 180 181 snprintf(id, sizeof(id), "thread[%d]", i); 182 object_property_add_child(OBJECT(sc), id, obj, &local_err); 183 if (local_err) { 184 goto err; 185 } 186 object_unref(obj); 187 } 188 189 for (j = 0; j < cc->nr_threads; j++) { 190 obj = OBJECT(sc->threads[j]); 191 192 spapr_cpu_core_realize_child(obj, spapr, &local_err); 193 if (local_err) { 194 goto err; 195 } 196 } 197 return; 198 199 err: 200 while (--i >= 0) { 201 obj = OBJECT(sc->threads[i]); 202 object_unparent(obj); 203 } 204 g_free(sc->threads); 205 error_propagate(errp, local_err); 206 } 207 208 static Property spapr_cpu_core_properties[] = { 209 DEFINE_PROP_INT32("node-id", sPAPRCPUCore, node_id, CPU_UNSET_NUMA_NODE_ID), 210 DEFINE_PROP_END_OF_LIST() 211 }; 212 213 static void spapr_cpu_core_class_init(ObjectClass *oc, void *data) 214 { 215 DeviceClass *dc = DEVICE_CLASS(oc); 216 sPAPRCPUCoreClass *scc = SPAPR_CPU_CORE_CLASS(oc); 217 218 dc->realize = spapr_cpu_core_realize; 219 dc->unrealize = spapr_cpu_core_unrealizefn; 220 dc->props = spapr_cpu_core_properties; 221 scc->cpu_type = data; 222 } 223 224 #define DEFINE_SPAPR_CPU_CORE_TYPE(cpu_model) \ 225 { \ 226 .parent = TYPE_SPAPR_CPU_CORE, \ 227 .class_data = (void *) POWERPC_CPU_TYPE_NAME(cpu_model), \ 228 .class_init = spapr_cpu_core_class_init, \ 229 .name = SPAPR_CPU_CORE_TYPE_NAME(cpu_model), \ 230 } 231 232 static const TypeInfo spapr_cpu_core_type_infos[] = { 233 { 234 .name = TYPE_SPAPR_CPU_CORE, 235 .parent = TYPE_CPU_CORE, 236 .abstract = true, 237 .instance_size = sizeof(sPAPRCPUCore), 238 .class_size = sizeof(sPAPRCPUCoreClass), 239 }, 240 DEFINE_SPAPR_CPU_CORE_TYPE("970_v2.2"), 241 DEFINE_SPAPR_CPU_CORE_TYPE("970mp_v1.0"), 242 DEFINE_SPAPR_CPU_CORE_TYPE("970mp_v1.1"), 243 DEFINE_SPAPR_CPU_CORE_TYPE("power5+_v2.1"), 244 DEFINE_SPAPR_CPU_CORE_TYPE("power7_v2.3"), 245 DEFINE_SPAPR_CPU_CORE_TYPE("power7+_v2.1"), 246 DEFINE_SPAPR_CPU_CORE_TYPE("power8_v2.0"), 247 DEFINE_SPAPR_CPU_CORE_TYPE("power8e_v2.1"), 248 DEFINE_SPAPR_CPU_CORE_TYPE("power8nvl_v1.0"), 249 DEFINE_SPAPR_CPU_CORE_TYPE("power9_v1.0"), 250 DEFINE_SPAPR_CPU_CORE_TYPE("power9_v2.0"), 251 #ifdef CONFIG_KVM 252 DEFINE_SPAPR_CPU_CORE_TYPE("host"), 253 #endif 254 }; 255 256 DEFINE_TYPES(spapr_cpu_core_type_infos) 257