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