cpu.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. #include "qemu/osdep.h"
  2. #include "migration/vmstate.h"
  3. #include "hw/acpi/cpu.h"
  4. #include "hw/core/cpu.h"
  5. #include "qapi/error.h"
  6. #include "qapi/qapi-events-acpi.h"
  7. #include "trace.h"
  8. #include "system/numa.h"
  9. #define ACPI_CPU_SELECTOR_OFFSET_WR 0
  10. #define ACPI_CPU_FLAGS_OFFSET_RW 4
  11. #define ACPI_CPU_CMD_OFFSET_WR 5
  12. #define ACPI_CPU_CMD_DATA_OFFSET_RW 8
  13. #define ACPI_CPU_CMD_DATA2_OFFSET_R 0
  14. #define OVMF_CPUHP_SMI_CMD 4
  15. enum {
  16. CPHP_GET_NEXT_CPU_WITH_EVENT_CMD = 0,
  17. CPHP_OST_EVENT_CMD = 1,
  18. CPHP_OST_STATUS_CMD = 2,
  19. CPHP_GET_CPU_ID_CMD = 3,
  20. CPHP_CMD_MAX
  21. };
  22. static ACPIOSTInfo *acpi_cpu_device_status(int idx, AcpiCpuStatus *cdev)
  23. {
  24. ACPIOSTInfo *info = g_new0(ACPIOSTInfo, 1);
  25. info->slot_type = ACPI_SLOT_TYPE_CPU;
  26. info->slot = g_strdup_printf("%d", idx);
  27. info->source = cdev->ost_event;
  28. info->status = cdev->ost_status;
  29. if (cdev->cpu) {
  30. DeviceState *dev = DEVICE(cdev->cpu);
  31. if (dev->id) {
  32. info->device = g_strdup(dev->id);
  33. }
  34. }
  35. return info;
  36. }
  37. void acpi_cpu_ospm_status(CPUHotplugState *cpu_st, ACPIOSTInfoList ***list)
  38. {
  39. ACPIOSTInfoList ***tail = list;
  40. int i;
  41. for (i = 0; i < cpu_st->dev_count; i++) {
  42. QAPI_LIST_APPEND(*tail, acpi_cpu_device_status(i, &cpu_st->devs[i]));
  43. }
  44. }
  45. static uint64_t cpu_hotplug_rd(void *opaque, hwaddr addr, unsigned size)
  46. {
  47. uint64_t val = 0;
  48. CPUHotplugState *cpu_st = opaque;
  49. AcpiCpuStatus *cdev;
  50. if (cpu_st->selector >= cpu_st->dev_count) {
  51. return val;
  52. }
  53. cdev = &cpu_st->devs[cpu_st->selector];
  54. switch (addr) {
  55. case ACPI_CPU_FLAGS_OFFSET_RW: /* pack and return is_* fields */
  56. val |= cdev->cpu ? 1 : 0;
  57. val |= cdev->is_inserting ? 2 : 0;
  58. val |= cdev->is_removing ? 4 : 0;
  59. val |= cdev->fw_remove ? 16 : 0;
  60. trace_cpuhp_acpi_read_flags(cpu_st->selector, val);
  61. break;
  62. case ACPI_CPU_CMD_DATA_OFFSET_RW:
  63. switch (cpu_st->command) {
  64. case CPHP_GET_NEXT_CPU_WITH_EVENT_CMD:
  65. val = cpu_st->selector;
  66. break;
  67. case CPHP_GET_CPU_ID_CMD:
  68. val = cdev->arch_id & 0xFFFFFFFF;
  69. break;
  70. default:
  71. break;
  72. }
  73. trace_cpuhp_acpi_read_cmd_data(cpu_st->selector, val);
  74. break;
  75. case ACPI_CPU_CMD_DATA2_OFFSET_R:
  76. switch (cpu_st->command) {
  77. case CPHP_GET_NEXT_CPU_WITH_EVENT_CMD:
  78. val = 0;
  79. break;
  80. case CPHP_GET_CPU_ID_CMD:
  81. val = cdev->arch_id >> 32;
  82. break;
  83. default:
  84. break;
  85. }
  86. trace_cpuhp_acpi_read_cmd_data2(cpu_st->selector, val);
  87. break;
  88. default:
  89. break;
  90. }
  91. return val;
  92. }
  93. static void cpu_hotplug_wr(void *opaque, hwaddr addr, uint64_t data,
  94. unsigned int size)
  95. {
  96. CPUHotplugState *cpu_st = opaque;
  97. AcpiCpuStatus *cdev;
  98. ACPIOSTInfo *info;
  99. assert(cpu_st->dev_count);
  100. if (addr) {
  101. if (cpu_st->selector >= cpu_st->dev_count) {
  102. trace_cpuhp_acpi_invalid_idx_selected(cpu_st->selector);
  103. return;
  104. }
  105. }
  106. switch (addr) {
  107. case ACPI_CPU_SELECTOR_OFFSET_WR: /* current CPU selector */
  108. cpu_st->selector = data;
  109. trace_cpuhp_acpi_write_idx(cpu_st->selector);
  110. break;
  111. case ACPI_CPU_FLAGS_OFFSET_RW: /* set is_* fields */
  112. cdev = &cpu_st->devs[cpu_st->selector];
  113. if (data & 2) { /* clear insert event */
  114. cdev->is_inserting = false;
  115. trace_cpuhp_acpi_clear_inserting_evt(cpu_st->selector);
  116. } else if (data & 4) { /* clear remove event */
  117. cdev->is_removing = false;
  118. trace_cpuhp_acpi_clear_remove_evt(cpu_st->selector);
  119. } else if (data & 8) {
  120. DeviceState *dev = NULL;
  121. HotplugHandler *hotplug_ctrl = NULL;
  122. if (!cdev->cpu || cdev->cpu == first_cpu) {
  123. trace_cpuhp_acpi_ejecting_invalid_cpu(cpu_st->selector);
  124. break;
  125. }
  126. trace_cpuhp_acpi_ejecting_cpu(cpu_st->selector);
  127. dev = DEVICE(cdev->cpu);
  128. hotplug_ctrl = qdev_get_hotplug_handler(dev);
  129. hotplug_handler_unplug(hotplug_ctrl, dev, NULL);
  130. object_unparent(OBJECT(dev));
  131. cdev->fw_remove = false;
  132. } else if (data & 16) {
  133. if (!cdev->cpu || cdev->cpu == first_cpu) {
  134. trace_cpuhp_acpi_fw_remove_invalid_cpu(cpu_st->selector);
  135. break;
  136. }
  137. trace_cpuhp_acpi_fw_remove_cpu(cpu_st->selector);
  138. cdev->fw_remove = true;
  139. }
  140. break;
  141. case ACPI_CPU_CMD_OFFSET_WR:
  142. trace_cpuhp_acpi_write_cmd(cpu_st->selector, data);
  143. if (data < CPHP_CMD_MAX) {
  144. cpu_st->command = data;
  145. if (cpu_st->command == CPHP_GET_NEXT_CPU_WITH_EVENT_CMD) {
  146. uint32_t iter = cpu_st->selector;
  147. do {
  148. cdev = &cpu_st->devs[iter];
  149. if (cdev->is_inserting || cdev->is_removing ||
  150. cdev->fw_remove) {
  151. cpu_st->selector = iter;
  152. trace_cpuhp_acpi_cpu_has_events(cpu_st->selector,
  153. cdev->is_inserting, cdev->is_removing);
  154. break;
  155. }
  156. iter = iter + 1 < cpu_st->dev_count ? iter + 1 : 0;
  157. } while (iter != cpu_st->selector);
  158. }
  159. }
  160. break;
  161. case ACPI_CPU_CMD_DATA_OFFSET_RW:
  162. switch (cpu_st->command) {
  163. case CPHP_OST_EVENT_CMD: {
  164. cdev = &cpu_st->devs[cpu_st->selector];
  165. cdev->ost_event = data;
  166. trace_cpuhp_acpi_write_ost_ev(cpu_st->selector, cdev->ost_event);
  167. break;
  168. }
  169. case CPHP_OST_STATUS_CMD: {
  170. cdev = &cpu_st->devs[cpu_st->selector];
  171. cdev->ost_status = data;
  172. info = acpi_cpu_device_status(cpu_st->selector, cdev);
  173. qapi_event_send_acpi_device_ost(info);
  174. qapi_free_ACPIOSTInfo(info);
  175. trace_cpuhp_acpi_write_ost_status(cpu_st->selector,
  176. cdev->ost_status);
  177. break;
  178. }
  179. default:
  180. break;
  181. }
  182. break;
  183. default:
  184. break;
  185. }
  186. }
  187. static const MemoryRegionOps cpu_hotplug_ops = {
  188. .read = cpu_hotplug_rd,
  189. .write = cpu_hotplug_wr,
  190. .endianness = DEVICE_LITTLE_ENDIAN,
  191. .valid = {
  192. .min_access_size = 1,
  193. .max_access_size = 4,
  194. },
  195. };
  196. void cpu_hotplug_hw_init(MemoryRegion *as, Object *owner,
  197. CPUHotplugState *state, hwaddr base_addr)
  198. {
  199. MachineState *machine = MACHINE(qdev_get_machine());
  200. MachineClass *mc = MACHINE_GET_CLASS(machine);
  201. const CPUArchIdList *id_list;
  202. int i;
  203. assert(mc->possible_cpu_arch_ids);
  204. id_list = mc->possible_cpu_arch_ids(machine);
  205. state->dev_count = id_list->len;
  206. state->devs = g_new0(typeof(*state->devs), state->dev_count);
  207. for (i = 0; i < id_list->len; i++) {
  208. state->devs[i].cpu = CPU(id_list->cpus[i].cpu);
  209. state->devs[i].arch_id = id_list->cpus[i].arch_id;
  210. }
  211. memory_region_init_io(&state->ctrl_reg, owner, &cpu_hotplug_ops, state,
  212. "acpi-cpu-hotplug", ACPI_CPU_HOTPLUG_REG_LEN);
  213. memory_region_add_subregion(as, base_addr, &state->ctrl_reg);
  214. }
  215. static AcpiCpuStatus *get_cpu_status(CPUHotplugState *cpu_st, DeviceState *dev)
  216. {
  217. CPUClass *k = CPU_GET_CLASS(dev);
  218. uint64_t cpu_arch_id = k->get_arch_id(CPU(dev));
  219. int i;
  220. for (i = 0; i < cpu_st->dev_count; i++) {
  221. if (cpu_arch_id == cpu_st->devs[i].arch_id) {
  222. return &cpu_st->devs[i];
  223. }
  224. }
  225. return NULL;
  226. }
  227. void acpi_cpu_plug_cb(HotplugHandler *hotplug_dev,
  228. CPUHotplugState *cpu_st, DeviceState *dev, Error **errp)
  229. {
  230. AcpiCpuStatus *cdev;
  231. cdev = get_cpu_status(cpu_st, dev);
  232. if (!cdev) {
  233. return;
  234. }
  235. cdev->cpu = CPU(dev);
  236. if (dev->hotplugged) {
  237. cdev->is_inserting = true;
  238. acpi_send_event(DEVICE(hotplug_dev), ACPI_CPU_HOTPLUG_STATUS);
  239. }
  240. }
  241. void acpi_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
  242. CPUHotplugState *cpu_st,
  243. DeviceState *dev, Error **errp)
  244. {
  245. AcpiCpuStatus *cdev;
  246. cdev = get_cpu_status(cpu_st, dev);
  247. if (!cdev) {
  248. return;
  249. }
  250. cdev->is_removing = true;
  251. acpi_send_event(DEVICE(hotplug_dev), ACPI_CPU_HOTPLUG_STATUS);
  252. }
  253. void acpi_cpu_unplug_cb(CPUHotplugState *cpu_st,
  254. DeviceState *dev, Error **errp)
  255. {
  256. AcpiCpuStatus *cdev;
  257. cdev = get_cpu_status(cpu_st, dev);
  258. if (!cdev) {
  259. return;
  260. }
  261. cdev->cpu = NULL;
  262. }
  263. static const VMStateDescription vmstate_cpuhp_sts = {
  264. .name = "CPU hotplug device state",
  265. .version_id = 1,
  266. .minimum_version_id = 1,
  267. .fields = (const VMStateField[]) {
  268. VMSTATE_BOOL(is_inserting, AcpiCpuStatus),
  269. VMSTATE_BOOL(is_removing, AcpiCpuStatus),
  270. VMSTATE_UINT32(ost_event, AcpiCpuStatus),
  271. VMSTATE_UINT32(ost_status, AcpiCpuStatus),
  272. VMSTATE_END_OF_LIST()
  273. }
  274. };
  275. const VMStateDescription vmstate_cpu_hotplug = {
  276. .name = "CPU hotplug state",
  277. .version_id = 1,
  278. .minimum_version_id = 1,
  279. .fields = (const VMStateField[]) {
  280. VMSTATE_UINT32(selector, CPUHotplugState),
  281. VMSTATE_UINT8(command, CPUHotplugState),
  282. VMSTATE_STRUCT_VARRAY_POINTER_UINT32(devs, CPUHotplugState, dev_count,
  283. vmstate_cpuhp_sts, AcpiCpuStatus),
  284. VMSTATE_END_OF_LIST()
  285. }
  286. };
  287. #define CPU_NAME_FMT "C%.03X"
  288. #define CPUHP_RES_DEVICE "PRES"
  289. #define CPU_LOCK "CPLK"
  290. #define CPU_STS_METHOD "CSTA"
  291. #define CPU_SCAN_METHOD "CSCN"
  292. #define CPU_NOTIFY_METHOD "CTFY"
  293. #define CPU_EJECT_METHOD "CEJ0"
  294. #define CPU_OST_METHOD "COST"
  295. #define CPU_ADDED_LIST "CNEW"
  296. #define CPU_EJ_LIST "CEJL"
  297. #define CPU_ENABLED "CPEN"
  298. #define CPU_SELECTOR "CSEL"
  299. #define CPU_COMMAND "CCMD"
  300. #define CPU_DATA "CDAT"
  301. #define CPU_INSERT_EVENT "CINS"
  302. #define CPU_REMOVE_EVENT "CRMV"
  303. #define CPU_EJECT_EVENT "CEJ0"
  304. #define CPU_FW_EJECT_EVENT "CEJF"
  305. void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
  306. build_madt_cpu_fn build_madt_cpu, hwaddr base_addr,
  307. const char *res_root,
  308. const char *event_handler_method,
  309. AmlRegionSpace rs)
  310. {
  311. Aml *ifctx;
  312. Aml *field;
  313. Aml *method;
  314. Aml *cpu_ctrl_dev;
  315. Aml *cpus_dev;
  316. Aml *zero = aml_int(0);
  317. Aml *one = aml_int(1);
  318. Aml *sb_scope = aml_scope("_SB");
  319. MachineClass *mc = MACHINE_GET_CLASS(machine);
  320. const CPUArchIdList *arch_ids = mc->possible_cpu_arch_ids(machine);
  321. char *cphp_res_path = g_strdup_printf("%s." CPUHP_RES_DEVICE, res_root);
  322. cpu_ctrl_dev = aml_device("%s", cphp_res_path);
  323. {
  324. Aml *crs;
  325. aml_append(cpu_ctrl_dev,
  326. aml_name_decl("_HID", aml_eisaid("PNP0A06")));
  327. aml_append(cpu_ctrl_dev,
  328. aml_name_decl("_UID", aml_string("CPU Hotplug resources")));
  329. aml_append(cpu_ctrl_dev, aml_mutex(CPU_LOCK, 0));
  330. assert((rs == AML_SYSTEM_IO) || (rs == AML_SYSTEM_MEMORY));
  331. crs = aml_resource_template();
  332. if (rs == AML_SYSTEM_IO) {
  333. aml_append(crs, aml_io(AML_DECODE16, base_addr, base_addr, 1,
  334. ACPI_CPU_HOTPLUG_REG_LEN));
  335. } else if (rs == AML_SYSTEM_MEMORY) {
  336. aml_append(crs, aml_memory32_fixed(base_addr,
  337. ACPI_CPU_HOTPLUG_REG_LEN, AML_READ_WRITE));
  338. }
  339. aml_append(cpu_ctrl_dev, aml_name_decl("_CRS", crs));
  340. /* declare CPU hotplug MMIO region with related access fields */
  341. aml_append(cpu_ctrl_dev,
  342. aml_operation_region("PRST", rs, aml_int(base_addr),
  343. ACPI_CPU_HOTPLUG_REG_LEN));
  344. field = aml_field("PRST", AML_BYTE_ACC, AML_NOLOCK,
  345. AML_WRITE_AS_ZEROS);
  346. aml_append(field, aml_reserved_field(ACPI_CPU_FLAGS_OFFSET_RW * 8));
  347. /* 1 if enabled, read only */
  348. aml_append(field, aml_named_field(CPU_ENABLED, 1));
  349. /* (read) 1 if has a insert event. (write) 1 to clear event */
  350. aml_append(field, aml_named_field(CPU_INSERT_EVENT, 1));
  351. /* (read) 1 if has a remove event. (write) 1 to clear event */
  352. aml_append(field, aml_named_field(CPU_REMOVE_EVENT, 1));
  353. /* initiates device eject, write only */
  354. aml_append(field, aml_named_field(CPU_EJECT_EVENT, 1));
  355. /* tell firmware to do device eject, write only */
  356. aml_append(field, aml_named_field(CPU_FW_EJECT_EVENT, 1));
  357. aml_append(field, aml_reserved_field(3));
  358. aml_append(field, aml_named_field(CPU_COMMAND, 8));
  359. aml_append(cpu_ctrl_dev, field);
  360. field = aml_field("PRST", AML_DWORD_ACC, AML_NOLOCK, AML_PRESERVE);
  361. /* CPU selector, write only */
  362. aml_append(field, aml_named_field(CPU_SELECTOR, 32));
  363. /* flags + cmd + 2byte align */
  364. aml_append(field, aml_reserved_field(4 * 8));
  365. aml_append(field, aml_named_field(CPU_DATA, 32));
  366. aml_append(cpu_ctrl_dev, field);
  367. if (opts.has_legacy_cphp) {
  368. method = aml_method("_INI", 0, AML_SERIALIZED);
  369. /* switch off legacy CPU hotplug HW and use new one,
  370. * on reboot system is in new mode and writing 0
  371. * in CPU_SELECTOR selects BSP, which is NOP at
  372. * the time _INI is called */
  373. aml_append(method, aml_store(zero, aml_name(CPU_SELECTOR)));
  374. aml_append(cpu_ctrl_dev, method);
  375. }
  376. }
  377. aml_append(sb_scope, cpu_ctrl_dev);
  378. cpus_dev = aml_device("\\_SB.CPUS");
  379. {
  380. int i;
  381. Aml *ctrl_lock = aml_name("%s.%s", cphp_res_path, CPU_LOCK);
  382. Aml *cpu_selector = aml_name("%s.%s", cphp_res_path, CPU_SELECTOR);
  383. Aml *is_enabled = aml_name("%s.%s", cphp_res_path, CPU_ENABLED);
  384. Aml *cpu_cmd = aml_name("%s.%s", cphp_res_path, CPU_COMMAND);
  385. Aml *cpu_data = aml_name("%s.%s", cphp_res_path, CPU_DATA);
  386. Aml *ins_evt = aml_name("%s.%s", cphp_res_path, CPU_INSERT_EVENT);
  387. Aml *rm_evt = aml_name("%s.%s", cphp_res_path, CPU_REMOVE_EVENT);
  388. Aml *ej_evt = aml_name("%s.%s", cphp_res_path, CPU_EJECT_EVENT);
  389. Aml *fw_ej_evt = aml_name("%s.%s", cphp_res_path, CPU_FW_EJECT_EVENT);
  390. aml_append(cpus_dev, aml_name_decl("_HID", aml_string("ACPI0010")));
  391. aml_append(cpus_dev, aml_name_decl("_CID", aml_eisaid("PNP0A05")));
  392. method = aml_method(CPU_NOTIFY_METHOD, 2, AML_NOTSERIALIZED);
  393. for (i = 0; i < arch_ids->len; i++) {
  394. Aml *cpu = aml_name(CPU_NAME_FMT, i);
  395. Aml *uid = aml_arg(0);
  396. Aml *event = aml_arg(1);
  397. ifctx = aml_if(aml_equal(uid, aml_int(i)));
  398. {
  399. aml_append(ifctx, aml_notify(cpu, event));
  400. }
  401. aml_append(method, ifctx);
  402. }
  403. aml_append(cpus_dev, method);
  404. method = aml_method(CPU_STS_METHOD, 1, AML_SERIALIZED);
  405. {
  406. Aml *idx = aml_arg(0);
  407. Aml *sta = aml_local(0);
  408. aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
  409. aml_append(method, aml_store(idx, cpu_selector));
  410. aml_append(method, aml_store(zero, sta));
  411. ifctx = aml_if(aml_equal(is_enabled, one));
  412. {
  413. aml_append(ifctx, aml_store(aml_int(0xF), sta));
  414. }
  415. aml_append(method, ifctx);
  416. aml_append(method, aml_release(ctrl_lock));
  417. aml_append(method, aml_return(sta));
  418. }
  419. aml_append(cpus_dev, method);
  420. method = aml_method(CPU_EJECT_METHOD, 1, AML_SERIALIZED);
  421. {
  422. Aml *idx = aml_arg(0);
  423. aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
  424. aml_append(method, aml_store(idx, cpu_selector));
  425. if (opts.fw_unplugs_cpu) {
  426. aml_append(method, aml_store(one, fw_ej_evt));
  427. aml_append(method, aml_store(aml_int(OVMF_CPUHP_SMI_CMD),
  428. aml_name("%s", opts.smi_path)));
  429. } else {
  430. aml_append(method, aml_store(one, ej_evt));
  431. }
  432. aml_append(method, aml_release(ctrl_lock));
  433. }
  434. aml_append(cpus_dev, method);
  435. method = aml_method(CPU_SCAN_METHOD, 0, AML_SERIALIZED);
  436. {
  437. const uint8_t max_cpus_per_pass = 255;
  438. Aml *while_ctx, *while_ctx2;
  439. Aml *has_event = aml_local(0);
  440. Aml *dev_chk = aml_int(1);
  441. Aml *eject_req = aml_int(3);
  442. Aml *next_cpu_cmd = aml_int(CPHP_GET_NEXT_CPU_WITH_EVENT_CMD);
  443. Aml *num_added_cpus = aml_local(1);
  444. Aml *cpu_idx = aml_local(2);
  445. Aml *uid = aml_local(3);
  446. Aml *has_job = aml_local(4);
  447. Aml *new_cpus = aml_name(CPU_ADDED_LIST);
  448. Aml *ej_cpus = aml_name(CPU_EJ_LIST);
  449. Aml *num_ej_cpus = aml_local(5);
  450. aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
  451. /*
  452. * Windows versions newer than XP (including Windows 10/Windows
  453. * Server 2019), do support* VarPackageOp but, it is cripled to hold
  454. * the same elements number as old PackageOp.
  455. * For compatibility with Windows XP (so it won't crash) use ACPI1.0
  456. * PackageOp which can hold max 255 elements.
  457. *
  458. * use named package as old Windows don't support it in local var
  459. */
  460. aml_append(method, aml_name_decl(CPU_ADDED_LIST,
  461. aml_package(max_cpus_per_pass)));
  462. aml_append(method, aml_name_decl(CPU_EJ_LIST,
  463. aml_package(max_cpus_per_pass)));
  464. aml_append(method, aml_store(zero, uid));
  465. aml_append(method, aml_store(one, has_job));
  466. /*
  467. * CPU_ADDED_LIST can hold limited number of elements, outer loop
  468. * allows to process CPUs in batches which let us to handle more
  469. * CPUs than CPU_ADDED_LIST can hold.
  470. */
  471. while_ctx2 = aml_while(aml_equal(has_job, one));
  472. {
  473. aml_append(while_ctx2, aml_store(zero, has_job));
  474. aml_append(while_ctx2, aml_store(one, has_event));
  475. aml_append(while_ctx2, aml_store(zero, num_added_cpus));
  476. aml_append(while_ctx2, aml_store(zero, num_ej_cpus));
  477. /*
  478. * Scan CPUs, till there are CPUs with events or
  479. * CPU_ADDED_LIST capacity is exhausted
  480. */
  481. while_ctx = aml_while(aml_land(aml_equal(has_event, one),
  482. aml_lless(uid, aml_int(arch_ids->len))));
  483. {
  484. /*
  485. * clear loop exit condition, ins_evt/rm_evt checks will
  486. * set it to 1 while next_cpu_cmd returns a CPU with events
  487. */
  488. aml_append(while_ctx, aml_store(zero, has_event));
  489. aml_append(while_ctx, aml_store(uid, cpu_selector));
  490. aml_append(while_ctx, aml_store(next_cpu_cmd, cpu_cmd));
  491. /*
  492. * wrap around case, scan is complete, exit loop.
  493. * It happens since events are not cleared in scan loop,
  494. * so next_cpu_cmd continues to find already processed CPUs
  495. */
  496. ifctx = aml_if(aml_lless(cpu_data, uid));
  497. {
  498. aml_append(ifctx, aml_break());
  499. }
  500. aml_append(while_ctx, ifctx);
  501. /*
  502. * if CPU_ADDED_LIST is full, exit inner loop and process
  503. * collected CPUs
  504. */
  505. ifctx = aml_if(aml_lor(
  506. aml_equal(num_added_cpus, aml_int(max_cpus_per_pass)),
  507. aml_equal(num_ej_cpus, aml_int(max_cpus_per_pass))
  508. ));
  509. {
  510. aml_append(ifctx, aml_store(one, has_job));
  511. aml_append(ifctx, aml_break());
  512. }
  513. aml_append(while_ctx, ifctx);
  514. aml_append(while_ctx, aml_store(cpu_data, uid));
  515. ifctx = aml_if(aml_equal(ins_evt, one));
  516. {
  517. /* cache added CPUs to Notify/Wakeup later */
  518. aml_append(ifctx, aml_store(uid,
  519. aml_index(new_cpus, num_added_cpus)));
  520. aml_append(ifctx, aml_increment(num_added_cpus));
  521. aml_append(ifctx, aml_store(one, has_event));
  522. }
  523. aml_append(while_ctx, ifctx);
  524. ifctx = aml_if(aml_equal(rm_evt, one));
  525. {
  526. /* cache to be removed CPUs to Notify later */
  527. aml_append(ifctx, aml_store(uid,
  528. aml_index(ej_cpus, num_ej_cpus)));
  529. aml_append(ifctx, aml_increment(num_ej_cpus));
  530. aml_append(ifctx, aml_store(one, has_event));
  531. }
  532. aml_append(while_ctx, ifctx);
  533. aml_append(while_ctx, aml_increment(uid));
  534. }
  535. aml_append(while_ctx2, while_ctx);
  536. /*
  537. * in case FW negotiated ICH9_LPC_SMI_F_CPU_HOTPLUG_BIT,
  538. * make upcall to FW, so it can pull in new CPUs before
  539. * OS is notified and wakes them up
  540. */
  541. if (opts.smi_path) {
  542. ifctx = aml_if(aml_lgreater(num_added_cpus, zero));
  543. {
  544. aml_append(ifctx, aml_store(aml_int(OVMF_CPUHP_SMI_CMD),
  545. aml_name("%s", opts.smi_path)));
  546. }
  547. aml_append(while_ctx2, ifctx);
  548. }
  549. /* Notify OSPM about new CPUs and clear insert events */
  550. aml_append(while_ctx2, aml_store(zero, cpu_idx));
  551. while_ctx = aml_while(aml_lless(cpu_idx, num_added_cpus));
  552. {
  553. aml_append(while_ctx,
  554. aml_store(aml_derefof(aml_index(new_cpus, cpu_idx)),
  555. uid));
  556. aml_append(while_ctx,
  557. aml_call2(CPU_NOTIFY_METHOD, uid, dev_chk));
  558. aml_append(while_ctx, aml_store(uid, aml_debug()));
  559. aml_append(while_ctx, aml_store(uid, cpu_selector));
  560. aml_append(while_ctx, aml_store(one, ins_evt));
  561. aml_append(while_ctx, aml_increment(cpu_idx));
  562. }
  563. aml_append(while_ctx2, while_ctx);
  564. /*
  565. * Notify OSPM about to be removed CPUs and clear remove flag
  566. */
  567. aml_append(while_ctx2, aml_store(zero, cpu_idx));
  568. while_ctx = aml_while(aml_lless(cpu_idx, num_ej_cpus));
  569. {
  570. aml_append(while_ctx,
  571. aml_store(aml_derefof(aml_index(ej_cpus, cpu_idx)),
  572. uid));
  573. aml_append(while_ctx,
  574. aml_call2(CPU_NOTIFY_METHOD, uid, eject_req));
  575. aml_append(while_ctx, aml_store(uid, cpu_selector));
  576. aml_append(while_ctx, aml_store(one, rm_evt));
  577. aml_append(while_ctx, aml_increment(cpu_idx));
  578. }
  579. aml_append(while_ctx2, while_ctx);
  580. /*
  581. * If another batch is needed, then it will resume scanning
  582. * exactly at -- and not after -- the last CPU that's currently
  583. * in CPU_ADDED_LIST. In other words, the last CPU in
  584. * CPU_ADDED_LIST is going to be re-checked. That's OK: we've
  585. * just cleared the insert event for *all* CPUs in
  586. * CPU_ADDED_LIST, including the last one. So the scan will
  587. * simply seek past it.
  588. */
  589. }
  590. aml_append(method, while_ctx2);
  591. aml_append(method, aml_release(ctrl_lock));
  592. }
  593. aml_append(cpus_dev, method);
  594. method = aml_method(CPU_OST_METHOD, 4, AML_SERIALIZED);
  595. {
  596. Aml *uid = aml_arg(0);
  597. Aml *ev_cmd = aml_int(CPHP_OST_EVENT_CMD);
  598. Aml *st_cmd = aml_int(CPHP_OST_STATUS_CMD);
  599. aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
  600. aml_append(method, aml_store(uid, cpu_selector));
  601. aml_append(method, aml_store(ev_cmd, cpu_cmd));
  602. aml_append(method, aml_store(aml_arg(1), cpu_data));
  603. aml_append(method, aml_store(st_cmd, cpu_cmd));
  604. aml_append(method, aml_store(aml_arg(2), cpu_data));
  605. aml_append(method, aml_release(ctrl_lock));
  606. }
  607. aml_append(cpus_dev, method);
  608. /* build Processor object for each processor */
  609. for (i = 0; i < arch_ids->len; i++) {
  610. Aml *dev;
  611. Aml *uid = aml_int(i);
  612. GArray *madt_buf = g_array_new(0, 1, 1);
  613. int arch_id = arch_ids->cpus[i].arch_id;
  614. if (opts.acpi_1_compatible && arch_id < 255) {
  615. dev = aml_processor(i, 0, 0, CPU_NAME_FMT, i);
  616. } else {
  617. dev = aml_device(CPU_NAME_FMT, i);
  618. aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0007")));
  619. aml_append(dev, aml_name_decl("_UID", uid));
  620. }
  621. method = aml_method("_STA", 0, AML_SERIALIZED);
  622. aml_append(method, aml_return(aml_call1(CPU_STS_METHOD, uid)));
  623. aml_append(dev, method);
  624. /* build _MAT object */
  625. build_madt_cpu(i, arch_ids, madt_buf, true); /* set enabled flag */
  626. aml_append(dev, aml_name_decl("_MAT",
  627. aml_buffer(madt_buf->len, (uint8_t *)madt_buf->data)));
  628. g_array_free(madt_buf, true);
  629. if (CPU(arch_ids->cpus[i].cpu) != first_cpu) {
  630. method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
  631. aml_append(method, aml_call1(CPU_EJECT_METHOD, uid));
  632. aml_append(dev, method);
  633. }
  634. method = aml_method("_OST", 3, AML_SERIALIZED);
  635. aml_append(method,
  636. aml_call4(CPU_OST_METHOD, uid, aml_arg(0),
  637. aml_arg(1), aml_arg(2))
  638. );
  639. aml_append(dev, method);
  640. /* Linux guests discard SRAT info for non-present CPUs
  641. * as a result _PXM is required for all CPUs which might
  642. * be hot-plugged. For simplicity, add it for all CPUs.
  643. */
  644. if (arch_ids->cpus[i].props.has_node_id) {
  645. aml_append(dev, aml_name_decl("_PXM",
  646. aml_int(arch_ids->cpus[i].props.node_id)));
  647. }
  648. aml_append(cpus_dev, dev);
  649. }
  650. }
  651. aml_append(sb_scope, cpus_dev);
  652. aml_append(table, sb_scope);
  653. method = aml_method(event_handler_method, 0, AML_NOTSERIALIZED);
  654. aml_append(method, aml_call0("\\_SB.CPUS." CPU_SCAN_METHOD));
  655. aml_append(table, method);
  656. g_free(cphp_res_path);
  657. }