spapr_cpu_core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 "qemu/osdep.h"
  10. #include "hw/cpu/core.h"
  11. #include "hw/ppc/spapr_cpu_core.h"
  12. #include "hw/qdev-properties.h"
  13. #include "migration/vmstate.h"
  14. #include "target/ppc/cpu.h"
  15. #include "hw/ppc/spapr.h"
  16. #include "qapi/error.h"
  17. #include "system/cpus.h"
  18. #include "system/kvm.h"
  19. #include "target/ppc/kvm_ppc.h"
  20. #include "hw/ppc/ppc.h"
  21. #include "target/ppc/mmu-hash64.h"
  22. #include "target/ppc/power8-pmu.h"
  23. #include "system/numa.h"
  24. #include "system/reset.h"
  25. #include "system/hw_accel.h"
  26. #include "qemu/error-report.h"
  27. static void spapr_reset_vcpu(PowerPCCPU *cpu)
  28. {
  29. CPUState *cs = CPU(cpu);
  30. CPUPPCState *env = &cpu->env;
  31. PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
  32. SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
  33. target_ulong lpcr;
  34. SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
  35. cpu_reset(cs);
  36. env->quiesced = true; /* set "RTAS stopped" state. */
  37. ppc_maybe_interrupt(env);
  38. /*
  39. * "PowerPC Processor binding to IEEE 1275" defines the initial MSR state
  40. * as 32bit (MSR_SF=0) with MSR_ME=1 and MSR_FP=1 in "8.2.1. Initial
  41. * Register Values". This can also be found in "LoPAPR 1.1" "C.9.2.1
  42. * Initial Register Values".
  43. */
  44. env->msr &= ~(1ULL << MSR_SF);
  45. env->msr |= (1ULL << MSR_ME) | (1ULL << MSR_FP);
  46. env->spr[SPR_HIOR] = 0;
  47. lpcr = env->spr[SPR_LPCR];
  48. /* Set emulated LPCR to not send interrupts to hypervisor. Note that
  49. * under KVM, the actual HW LPCR will be set differently by KVM itself,
  50. * the settings below ensure proper operations with TCG in absence of
  51. * a real hypervisor.
  52. *
  53. * Disable Power-saving mode Exit Cause exceptions for the CPU, so
  54. * we don't get spurious wakups before an RTAS start-cpu call.
  55. * For the same reason, set PSSCR_EC.
  56. */
  57. lpcr &= ~(LPCR_VPM1 | LPCR_ISL | LPCR_KBV | pcc->lpcr_pm);
  58. lpcr |= LPCR_LPES0 | LPCR_LPES1;
  59. env->spr[SPR_PSSCR] |= PSSCR_EC;
  60. ppc_store_lpcr(cpu, lpcr);
  61. /* Set a full AMOR so guest can use the AMR as it sees fit */
  62. env->spr[SPR_AMOR] = 0xffffffffffffffffull;
  63. spapr_cpu->vpa_addr = 0;
  64. spapr_cpu->slb_shadow_addr = 0;
  65. spapr_cpu->slb_shadow_size = 0;
  66. spapr_cpu->dtl_addr = 0;
  67. spapr_cpu->dtl_size = 0;
  68. spapr_caps_cpu_apply(spapr, cpu);
  69. kvm_check_mmu(cpu, &error_fatal);
  70. cpu_ppc_tb_reset(env);
  71. spapr_irq_cpu_intc_reset(spapr, cpu);
  72. }
  73. void spapr_cpu_set_entry_state(PowerPCCPU *cpu, target_ulong nip,
  74. target_ulong r1, target_ulong r3,
  75. target_ulong r4)
  76. {
  77. PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
  78. CPUPPCState *env = &cpu->env;
  79. env->nip = nip;
  80. env->gpr[1] = r1;
  81. env->gpr[3] = r3;
  82. env->gpr[4] = r4;
  83. kvmppc_set_reg_ppc_online(cpu, 1);
  84. CPU(cpu)->halted = 0;
  85. /* Enable Power-saving mode Exit Cause exceptions */
  86. ppc_store_lpcr(cpu, env->spr[SPR_LPCR] | pcc->lpcr_pm);
  87. env->quiesced = false; /* clear "RTAS stopped" state. */
  88. ppc_maybe_interrupt(env);
  89. }
  90. /*
  91. * Return the sPAPR CPU core type for @model which essentially is the CPU
  92. * model specified with -cpu cmdline option.
  93. */
  94. const char *spapr_get_cpu_core_type(const char *cpu_type)
  95. {
  96. int len = strlen(cpu_type) - strlen(POWERPC_CPU_TYPE_SUFFIX);
  97. char *core_type = g_strdup_printf(SPAPR_CPU_CORE_TYPE_NAME("%.*s"),
  98. len, cpu_type);
  99. ObjectClass *oc = object_class_by_name(core_type);
  100. g_free(core_type);
  101. if (!oc) {
  102. return NULL;
  103. }
  104. return object_class_get_name(oc);
  105. }
  106. static bool slb_shadow_needed(void *opaque)
  107. {
  108. SpaprCpuState *spapr_cpu = opaque;
  109. return spapr_cpu->slb_shadow_addr != 0;
  110. }
  111. static const VMStateDescription vmstate_spapr_cpu_slb_shadow = {
  112. .name = "spapr_cpu/vpa/slb_shadow",
  113. .version_id = 1,
  114. .minimum_version_id = 1,
  115. .needed = slb_shadow_needed,
  116. .fields = (const VMStateField[]) {
  117. VMSTATE_UINT64(slb_shadow_addr, SpaprCpuState),
  118. VMSTATE_UINT64(slb_shadow_size, SpaprCpuState),
  119. VMSTATE_END_OF_LIST()
  120. }
  121. };
  122. static bool dtl_needed(void *opaque)
  123. {
  124. SpaprCpuState *spapr_cpu = opaque;
  125. return spapr_cpu->dtl_addr != 0;
  126. }
  127. static const VMStateDescription vmstate_spapr_cpu_dtl = {
  128. .name = "spapr_cpu/vpa/dtl",
  129. .version_id = 1,
  130. .minimum_version_id = 1,
  131. .needed = dtl_needed,
  132. .fields = (const VMStateField[]) {
  133. VMSTATE_UINT64(dtl_addr, SpaprCpuState),
  134. VMSTATE_UINT64(dtl_size, SpaprCpuState),
  135. VMSTATE_END_OF_LIST()
  136. }
  137. };
  138. static bool vpa_needed(void *opaque)
  139. {
  140. SpaprCpuState *spapr_cpu = opaque;
  141. return spapr_cpu->vpa_addr != 0;
  142. }
  143. static const VMStateDescription vmstate_spapr_cpu_vpa = {
  144. .name = "spapr_cpu/vpa",
  145. .version_id = 1,
  146. .minimum_version_id = 1,
  147. .needed = vpa_needed,
  148. .fields = (const VMStateField[]) {
  149. VMSTATE_UINT64(vpa_addr, SpaprCpuState),
  150. VMSTATE_END_OF_LIST()
  151. },
  152. .subsections = (const VMStateDescription * const []) {
  153. &vmstate_spapr_cpu_slb_shadow,
  154. &vmstate_spapr_cpu_dtl,
  155. NULL
  156. }
  157. };
  158. static const VMStateDescription vmstate_spapr_cpu_state = {
  159. .name = "spapr_cpu",
  160. .version_id = 1,
  161. .minimum_version_id = 1,
  162. .fields = (const VMStateField[]) {
  163. VMSTATE_END_OF_LIST()
  164. },
  165. .subsections = (const VMStateDescription * const []) {
  166. &vmstate_spapr_cpu_vpa,
  167. NULL
  168. }
  169. };
  170. static void spapr_unrealize_vcpu(PowerPCCPU *cpu, SpaprCpuCore *sc)
  171. {
  172. CPUPPCState *env = &cpu->env;
  173. vmstate_unregister(NULL, &vmstate_spapr_cpu_state, cpu->machine_data);
  174. spapr_irq_cpu_intc_destroy(SPAPR_MACHINE(qdev_get_machine()), cpu);
  175. cpu_ppc_tb_free(env);
  176. qdev_unrealize(DEVICE(cpu));
  177. }
  178. /*
  179. * Called when CPUs are hot-plugged.
  180. */
  181. static void spapr_cpu_core_reset(DeviceState *dev)
  182. {
  183. CPUCore *cc = CPU_CORE(dev);
  184. SpaprCpuCore *sc = SPAPR_CPU_CORE(dev);
  185. int i;
  186. for (i = 0; i < cc->nr_threads; i++) {
  187. spapr_reset_vcpu(sc->threads[i]);
  188. }
  189. }
  190. /*
  191. * Called by the machine reset.
  192. */
  193. static void spapr_cpu_core_reset_handler(void *opaque)
  194. {
  195. spapr_cpu_core_reset(opaque);
  196. }
  197. static void spapr_delete_vcpu(PowerPCCPU *cpu)
  198. {
  199. SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
  200. cpu->machine_data = NULL;
  201. g_free(spapr_cpu);
  202. object_unparent(OBJECT(cpu));
  203. }
  204. static void spapr_cpu_core_unrealize(DeviceState *dev)
  205. {
  206. SpaprCpuCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
  207. CPUCore *cc = CPU_CORE(dev);
  208. int i;
  209. for (i = 0; i < cc->nr_threads; i++) {
  210. if (sc->threads[i]) {
  211. /*
  212. * Since this we can get here from the error path of
  213. * spapr_cpu_core_realize(), make sure we only unrealize
  214. * vCPUs that have already been realized.
  215. */
  216. if (qdev_is_realized(DEVICE(sc->threads[i]))) {
  217. spapr_unrealize_vcpu(sc->threads[i], sc);
  218. }
  219. spapr_delete_vcpu(sc->threads[i]);
  220. }
  221. }
  222. g_free(sc->threads);
  223. qemu_unregister_reset(spapr_cpu_core_reset_handler, sc);
  224. }
  225. static bool spapr_realize_vcpu(PowerPCCPU *cpu, SpaprMachineState *spapr,
  226. SpaprCpuCore *sc, int thread_index, Error **errp)
  227. {
  228. CPUPPCState *env = &cpu->env;
  229. CPUState *cs = CPU(cpu);
  230. if (!qdev_realize(DEVICE(cpu), NULL, errp)) {
  231. return false;
  232. }
  233. cpu_ppc_set_vhyp(cpu, PPC_VIRTUAL_HYPERVISOR(spapr));
  234. kvmppc_set_papr(cpu);
  235. env->spr_cb[SPR_PIR].default_value = cs->cpu_index;
  236. env->spr_cb[SPR_TIR].default_value = thread_index;
  237. env->spr_cb[SPR_HASHPKEYR].default_value = spapr->hashpkey_val;
  238. cpu_ppc_set_1lpar(cpu);
  239. /* Set time-base frequency to 512 MHz. vhyp must be set first. */
  240. cpu_ppc_tb_init(env, SPAPR_TIMEBASE_FREQ);
  241. if (spapr_irq_cpu_intc_create(spapr, cpu, errp) < 0) {
  242. qdev_unrealize(DEVICE(cpu));
  243. return false;
  244. }
  245. vmstate_register(NULL, cs->cpu_index, &vmstate_spapr_cpu_state,
  246. cpu->machine_data);
  247. return true;
  248. }
  249. static PowerPCCPU *spapr_create_vcpu(SpaprCpuCore *sc, int i, Error **errp)
  250. {
  251. SpaprCpuCoreClass *scc = SPAPR_CPU_CORE_GET_CLASS(sc);
  252. CPUCore *cc = CPU_CORE(sc);
  253. g_autoptr(Object) obj = NULL;
  254. g_autofree char *id = NULL;
  255. CPUState *cs;
  256. PowerPCCPU *cpu;
  257. CPUPPCState *env;
  258. obj = object_new(scc->cpu_type);
  259. cs = CPU(obj);
  260. cpu = POWERPC_CPU(obj);
  261. env = &cpu->env;
  262. /*
  263. * All CPUs start halted. CPU0 is unhalted from the machine level reset code
  264. * and the rest are explicitly started up by the guest using an RTAS call.
  265. */
  266. qdev_prop_set_bit(DEVICE(obj), "start-powered-off", true);
  267. cs->cpu_index = cc->core_id + i;
  268. if (!spapr_set_vcpu_id(cpu, cs->cpu_index, errp)) {
  269. return NULL;
  270. }
  271. env->chip_index = sc->node_id;
  272. env->core_index = cc->core_id;
  273. cpu->node_id = sc->node_id;
  274. id = g_strdup_printf("thread[%d]", i);
  275. object_property_add_child(OBJECT(sc), id, obj);
  276. cpu->machine_data = g_new0(SpaprCpuState, 1);
  277. return cpu;
  278. }
  279. static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
  280. {
  281. /* We don't use SPAPR_MACHINE() in order to exit gracefully if the user
  282. * tries to add a sPAPR CPU core to a non-pseries machine.
  283. */
  284. SpaprMachineState *spapr =
  285. (SpaprMachineState *) object_dynamic_cast(qdev_get_machine(),
  286. TYPE_SPAPR_MACHINE);
  287. SpaprCpuCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
  288. CPUCore *cc = CPU_CORE(OBJECT(dev));
  289. int i;
  290. if (!spapr) {
  291. error_setg(errp, TYPE_SPAPR_CPU_CORE " needs a pseries machine");
  292. return;
  293. }
  294. qemu_register_reset(spapr_cpu_core_reset_handler, sc);
  295. sc->threads = g_new0(PowerPCCPU *, cc->nr_threads);
  296. for (i = 0; i < cc->nr_threads; i++) {
  297. PowerPCCPU *cpu;
  298. cpu = spapr_create_vcpu(sc, i, errp);
  299. sc->threads[i] = cpu;
  300. if (cpu && cc->nr_threads > 1) {
  301. cpu->env.has_smt_siblings = true;
  302. }
  303. if (!cpu || !spapr_realize_vcpu(cpu, spapr, sc, i, errp)) {
  304. spapr_cpu_core_unrealize(dev);
  305. return;
  306. }
  307. }
  308. }
  309. static const Property spapr_cpu_core_properties[] = {
  310. DEFINE_PROP_INT32("node-id", SpaprCpuCore, node_id, CPU_UNSET_NUMA_NODE_ID),
  311. };
  312. static void spapr_cpu_core_class_init(ObjectClass *oc, void *data)
  313. {
  314. DeviceClass *dc = DEVICE_CLASS(oc);
  315. SpaprCpuCoreClass *scc = SPAPR_CPU_CORE_CLASS(oc);
  316. dc->realize = spapr_cpu_core_realize;
  317. dc->unrealize = spapr_cpu_core_unrealize;
  318. device_class_set_legacy_reset(dc, spapr_cpu_core_reset);
  319. device_class_set_props(dc, spapr_cpu_core_properties);
  320. scc->cpu_type = data;
  321. }
  322. #define DEFINE_SPAPR_CPU_CORE_TYPE(cpu_model) \
  323. { \
  324. .parent = TYPE_SPAPR_CPU_CORE, \
  325. .class_data = (void *) POWERPC_CPU_TYPE_NAME(cpu_model), \
  326. .class_init = spapr_cpu_core_class_init, \
  327. .name = SPAPR_CPU_CORE_TYPE_NAME(cpu_model), \
  328. }
  329. static const TypeInfo spapr_cpu_core_type_infos[] = {
  330. {
  331. .name = TYPE_SPAPR_CPU_CORE,
  332. .parent = TYPE_CPU_CORE,
  333. .abstract = true,
  334. .instance_size = sizeof(SpaprCpuCore),
  335. .class_size = sizeof(SpaprCpuCoreClass),
  336. },
  337. DEFINE_SPAPR_CPU_CORE_TYPE("970_v2.2"),
  338. DEFINE_SPAPR_CPU_CORE_TYPE("970mp_v1.0"),
  339. DEFINE_SPAPR_CPU_CORE_TYPE("970mp_v1.1"),
  340. DEFINE_SPAPR_CPU_CORE_TYPE("power5p_v2.1"),
  341. DEFINE_SPAPR_CPU_CORE_TYPE("power7_v2.3"),
  342. DEFINE_SPAPR_CPU_CORE_TYPE("power7p_v2.1"),
  343. DEFINE_SPAPR_CPU_CORE_TYPE("power8_v2.0"),
  344. DEFINE_SPAPR_CPU_CORE_TYPE("power8e_v2.1"),
  345. DEFINE_SPAPR_CPU_CORE_TYPE("power8nvl_v1.0"),
  346. DEFINE_SPAPR_CPU_CORE_TYPE("power9_v2.0"),
  347. DEFINE_SPAPR_CPU_CORE_TYPE("power9_v2.2"),
  348. DEFINE_SPAPR_CPU_CORE_TYPE("power10_v2.0"),
  349. DEFINE_SPAPR_CPU_CORE_TYPE("power11_v2.0"),
  350. #ifdef CONFIG_KVM
  351. DEFINE_SPAPR_CPU_CORE_TYPE("host"),
  352. #endif
  353. };
  354. DEFINE_TYPES(spapr_cpu_core_type_infos)