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