xref: /openbmc/qemu/hw/acpi/cpu.c (revision 24602b77f5658ae8377958c15fdef2f44affc743)
15e1b5d93SIgor Mammedov #include "qemu/osdep.h"
2d6454270SMarkus Armbruster #include "migration/vmstate.h"
35e1b5d93SIgor Mammedov #include "hw/acpi/cpu.h"
44f70dd5fSBernhard Beschow #include "hw/core/cpu.h"
55e1b5d93SIgor Mammedov #include "qapi/error.h"
627c9188fSPhilippe Mathieu-Daudé #include "qapi/qapi-events-acpi.h"
75e1b5d93SIgor Mammedov #include "trace.h"
827111931SIgor Mammedov #include "sysemu/numa.h"
95e1b5d93SIgor Mammedov 
105e1b5d93SIgor Mammedov #define ACPI_CPU_SELECTOR_OFFSET_WR 0
115e1b5d93SIgor Mammedov #define ACPI_CPU_FLAGS_OFFSET_RW 4
12d2238cb6SIgor Mammedov #define ACPI_CPU_CMD_OFFSET_WR 5
13d2238cb6SIgor Mammedov #define ACPI_CPU_CMD_DATA_OFFSET_RW 8
14e6d0c3ceSIgor Mammedov #define ACPI_CPU_CMD_DATA2_OFFSET_R 0
15d2238cb6SIgor Mammedov 
169cc5a90bSIgor Mammedov #define OVMF_CPUHP_SMI_CMD 4
179cc5a90bSIgor Mammedov 
18d2238cb6SIgor Mammedov enum {
19d2238cb6SIgor Mammedov     CPHP_GET_NEXT_CPU_WITH_EVENT_CMD = 0,
2076623d00SIgor Mammedov     CPHP_OST_EVENT_CMD = 1,
2176623d00SIgor Mammedov     CPHP_OST_STATUS_CMD = 2,
223a61c8dbSIgor Mammedov     CPHP_GET_CPU_ID_CMD = 3,
23d2238cb6SIgor Mammedov     CPHP_CMD_MAX
24d2238cb6SIgor Mammedov };
255e1b5d93SIgor Mammedov 
acpi_cpu_device_status(int idx,AcpiCpuStatus * cdev)2676623d00SIgor Mammedov static ACPIOSTInfo *acpi_cpu_device_status(int idx, AcpiCpuStatus *cdev)
2776623d00SIgor Mammedov {
2876623d00SIgor Mammedov     ACPIOSTInfo *info = g_new0(ACPIOSTInfo, 1);
2976623d00SIgor Mammedov 
3076623d00SIgor Mammedov     info->slot_type = ACPI_SLOT_TYPE_CPU;
3176623d00SIgor Mammedov     info->slot = g_strdup_printf("%d", idx);
3276623d00SIgor Mammedov     info->source = cdev->ost_event;
3376623d00SIgor Mammedov     info->status = cdev->ost_status;
3476623d00SIgor Mammedov     if (cdev->cpu) {
3576623d00SIgor Mammedov         DeviceState *dev = DEVICE(cdev->cpu);
3676623d00SIgor Mammedov         if (dev->id) {
3776623d00SIgor Mammedov             info->device = g_strdup(dev->id);
3876623d00SIgor Mammedov         }
3976623d00SIgor Mammedov     }
4076623d00SIgor Mammedov     return info;
4176623d00SIgor Mammedov }
4276623d00SIgor Mammedov 
acpi_cpu_ospm_status(CPUHotplugState * cpu_st,ACPIOSTInfoList *** list)4376623d00SIgor Mammedov void acpi_cpu_ospm_status(CPUHotplugState *cpu_st, ACPIOSTInfoList ***list)
4476623d00SIgor Mammedov {
45c3033fd3SEric Blake     ACPIOSTInfoList ***tail = list;
4676623d00SIgor Mammedov     int i;
4776623d00SIgor Mammedov 
4876623d00SIgor Mammedov     for (i = 0; i < cpu_st->dev_count; i++) {
49c3033fd3SEric Blake         QAPI_LIST_APPEND(*tail, acpi_cpu_device_status(i, &cpu_st->devs[i]));
5076623d00SIgor Mammedov     }
5176623d00SIgor Mammedov }
5276623d00SIgor Mammedov 
cpu_hotplug_rd(void * opaque,hwaddr addr,unsigned size)535e1b5d93SIgor Mammedov static uint64_t cpu_hotplug_rd(void *opaque, hwaddr addr, unsigned size)
545e1b5d93SIgor Mammedov {
555e1b5d93SIgor Mammedov     uint64_t val = 0;
565e1b5d93SIgor Mammedov     CPUHotplugState *cpu_st = opaque;
575e1b5d93SIgor Mammedov     AcpiCpuStatus *cdev;
585e1b5d93SIgor Mammedov 
595e1b5d93SIgor Mammedov     if (cpu_st->selector >= cpu_st->dev_count) {
605e1b5d93SIgor Mammedov         return val;
615e1b5d93SIgor Mammedov     }
625e1b5d93SIgor Mammedov 
635e1b5d93SIgor Mammedov     cdev = &cpu_st->devs[cpu_st->selector];
645e1b5d93SIgor Mammedov     switch (addr) {
655e1b5d93SIgor Mammedov     case ACPI_CPU_FLAGS_OFFSET_RW: /* pack and return is_* fields */
66*cb36e257SIgor Mammedov         val |= cdev->cpu ? 1 : 0;
67d2238cb6SIgor Mammedov         val |= cdev->is_inserting ? 2 : 0;
688872c25aSIgor Mammedov         val |= cdev->is_removing  ? 4 : 0;
691e6107d9SIgor Mammedov         val |= cdev->fw_remove  ? 16 : 0;
705e1b5d93SIgor Mammedov         trace_cpuhp_acpi_read_flags(cpu_st->selector, val);
715e1b5d93SIgor Mammedov         break;
72d2238cb6SIgor Mammedov     case ACPI_CPU_CMD_DATA_OFFSET_RW:
73d2238cb6SIgor Mammedov         switch (cpu_st->command) {
74d2238cb6SIgor Mammedov         case CPHP_GET_NEXT_CPU_WITH_EVENT_CMD:
75d2238cb6SIgor Mammedov            val = cpu_st->selector;
76d2238cb6SIgor Mammedov            break;
773a61c8dbSIgor Mammedov         case CPHP_GET_CPU_ID_CMD:
783a61c8dbSIgor Mammedov            val = cdev->arch_id & 0xFFFFFFFF;
793a61c8dbSIgor Mammedov            break;
80d2238cb6SIgor Mammedov         default:
81d2238cb6SIgor Mammedov            break;
82d2238cb6SIgor Mammedov         }
83d2238cb6SIgor Mammedov         trace_cpuhp_acpi_read_cmd_data(cpu_st->selector, val);
84d2238cb6SIgor Mammedov         break;
85e6d0c3ceSIgor Mammedov     case ACPI_CPU_CMD_DATA2_OFFSET_R:
86e6d0c3ceSIgor Mammedov         switch (cpu_st->command) {
87e6d0c3ceSIgor Mammedov         case CPHP_GET_NEXT_CPU_WITH_EVENT_CMD:
88e6d0c3ceSIgor Mammedov            val = 0;
89e6d0c3ceSIgor Mammedov            break;
903a61c8dbSIgor Mammedov         case CPHP_GET_CPU_ID_CMD:
913a61c8dbSIgor Mammedov            val = cdev->arch_id >> 32;
923a61c8dbSIgor Mammedov            break;
93e6d0c3ceSIgor Mammedov         default:
94e6d0c3ceSIgor Mammedov            break;
95e6d0c3ceSIgor Mammedov         }
96e6d0c3ceSIgor Mammedov         trace_cpuhp_acpi_read_cmd_data2(cpu_st->selector, val);
97e6d0c3ceSIgor Mammedov         break;
985e1b5d93SIgor Mammedov     default:
995e1b5d93SIgor Mammedov         break;
1005e1b5d93SIgor Mammedov     }
1015e1b5d93SIgor Mammedov     return val;
1025e1b5d93SIgor Mammedov }
1035e1b5d93SIgor Mammedov 
cpu_hotplug_wr(void * opaque,hwaddr addr,uint64_t data,unsigned int size)1045e1b5d93SIgor Mammedov static void cpu_hotplug_wr(void *opaque, hwaddr addr, uint64_t data,
1055e1b5d93SIgor Mammedov                            unsigned int size)
1065e1b5d93SIgor Mammedov {
1075e1b5d93SIgor Mammedov     CPUHotplugState *cpu_st = opaque;
108d2238cb6SIgor Mammedov     AcpiCpuStatus *cdev;
10976623d00SIgor Mammedov     ACPIOSTInfo *info;
1105e1b5d93SIgor Mammedov 
1115e1b5d93SIgor Mammedov     assert(cpu_st->dev_count);
1125e1b5d93SIgor Mammedov 
1135e1b5d93SIgor Mammedov     if (addr) {
1145e1b5d93SIgor Mammedov         if (cpu_st->selector >= cpu_st->dev_count) {
1155e1b5d93SIgor Mammedov             trace_cpuhp_acpi_invalid_idx_selected(cpu_st->selector);
1165e1b5d93SIgor Mammedov             return;
1175e1b5d93SIgor Mammedov         }
1185e1b5d93SIgor Mammedov     }
1195e1b5d93SIgor Mammedov 
1205e1b5d93SIgor Mammedov     switch (addr) {
1215e1b5d93SIgor Mammedov     case ACPI_CPU_SELECTOR_OFFSET_WR: /* current CPU selector */
1225e1b5d93SIgor Mammedov         cpu_st->selector = data;
1235e1b5d93SIgor Mammedov         trace_cpuhp_acpi_write_idx(cpu_st->selector);
1245e1b5d93SIgor Mammedov         break;
125d2238cb6SIgor Mammedov     case ACPI_CPU_FLAGS_OFFSET_RW: /* set is_* fields  */
126d2238cb6SIgor Mammedov         cdev = &cpu_st->devs[cpu_st->selector];
127d2238cb6SIgor Mammedov         if (data & 2) { /* clear insert event */
128d2238cb6SIgor Mammedov             cdev->is_inserting = false;
129d2238cb6SIgor Mammedov             trace_cpuhp_acpi_clear_inserting_evt(cpu_st->selector);
1308872c25aSIgor Mammedov         } else if (data & 4) { /* clear remove event */
1318872c25aSIgor Mammedov             cdev->is_removing = false;
1328872c25aSIgor Mammedov             trace_cpuhp_acpi_clear_remove_evt(cpu_st->selector);
1338872c25aSIgor Mammedov         } else if (data & 8) {
1348872c25aSIgor Mammedov             DeviceState *dev = NULL;
1358872c25aSIgor Mammedov             HotplugHandler *hotplug_ctrl = NULL;
1368872c25aSIgor Mammedov 
137c2d2a81bSIgor Mammedov             if (!cdev->cpu || cdev->cpu == first_cpu) {
1388872c25aSIgor Mammedov                 trace_cpuhp_acpi_ejecting_invalid_cpu(cpu_st->selector);
1398872c25aSIgor Mammedov                 break;
1408872c25aSIgor Mammedov             }
1418872c25aSIgor Mammedov 
1428872c25aSIgor Mammedov             trace_cpuhp_acpi_ejecting_cpu(cpu_st->selector);
1438872c25aSIgor Mammedov             dev = DEVICE(cdev->cpu);
1448872c25aSIgor Mammedov             hotplug_ctrl = qdev_get_hotplug_handler(dev);
1458872c25aSIgor Mammedov             hotplug_handler_unplug(hotplug_ctrl, dev, NULL);
14607578b0aSDavid Hildenbrand             object_unparent(OBJECT(dev));
1471e6107d9SIgor Mammedov             cdev->fw_remove = false;
1481e6107d9SIgor Mammedov         } else if (data & 16) {
1491e6107d9SIgor Mammedov             if (!cdev->cpu || cdev->cpu == first_cpu) {
1501e6107d9SIgor Mammedov                 trace_cpuhp_acpi_fw_remove_invalid_cpu(cpu_st->selector);
1511e6107d9SIgor Mammedov                 break;
1521e6107d9SIgor Mammedov             }
1531e6107d9SIgor Mammedov             trace_cpuhp_acpi_fw_remove_cpu(cpu_st->selector);
1541e6107d9SIgor Mammedov             cdev->fw_remove = true;
155d2238cb6SIgor Mammedov         }
156d2238cb6SIgor Mammedov         break;
157d2238cb6SIgor Mammedov     case ACPI_CPU_CMD_OFFSET_WR:
158d2238cb6SIgor Mammedov         trace_cpuhp_acpi_write_cmd(cpu_st->selector, data);
159d2238cb6SIgor Mammedov         if (data < CPHP_CMD_MAX) {
160d2238cb6SIgor Mammedov             cpu_st->command = data;
161d2238cb6SIgor Mammedov             if (cpu_st->command == CPHP_GET_NEXT_CPU_WITH_EVENT_CMD) {
162d2238cb6SIgor Mammedov                 uint32_t iter = cpu_st->selector;
163d2238cb6SIgor Mammedov 
164d2238cb6SIgor Mammedov                 do {
165d2238cb6SIgor Mammedov                     cdev = &cpu_st->devs[iter];
1661e6107d9SIgor Mammedov                     if (cdev->is_inserting || cdev->is_removing ||
1671e6107d9SIgor Mammedov                         cdev->fw_remove) {
168d2238cb6SIgor Mammedov                         cpu_st->selector = iter;
169d2238cb6SIgor Mammedov                         trace_cpuhp_acpi_cpu_has_events(cpu_st->selector,
1708872c25aSIgor Mammedov                             cdev->is_inserting, cdev->is_removing);
171d2238cb6SIgor Mammedov                         break;
172d2238cb6SIgor Mammedov                     }
173d2238cb6SIgor Mammedov                     iter = iter + 1 < cpu_st->dev_count ? iter + 1 : 0;
174d2238cb6SIgor Mammedov                 } while (iter != cpu_st->selector);
175d2238cb6SIgor Mammedov             }
176d2238cb6SIgor Mammedov         }
177d2238cb6SIgor Mammedov         break;
17876623d00SIgor Mammedov     case ACPI_CPU_CMD_DATA_OFFSET_RW:
17976623d00SIgor Mammedov         switch (cpu_st->command) {
18076623d00SIgor Mammedov         case CPHP_OST_EVENT_CMD: {
18176623d00SIgor Mammedov            cdev = &cpu_st->devs[cpu_st->selector];
18276623d00SIgor Mammedov            cdev->ost_event = data;
18376623d00SIgor Mammedov            trace_cpuhp_acpi_write_ost_ev(cpu_st->selector, cdev->ost_event);
18476623d00SIgor Mammedov            break;
18576623d00SIgor Mammedov         }
18676623d00SIgor Mammedov         case CPHP_OST_STATUS_CMD: {
18776623d00SIgor Mammedov            cdev = &cpu_st->devs[cpu_st->selector];
18876623d00SIgor Mammedov            cdev->ost_status = data;
18976623d00SIgor Mammedov            info = acpi_cpu_device_status(cpu_st->selector, cdev);
1903ab72385SPeter Xu            qapi_event_send_acpi_device_ost(info);
19176623d00SIgor Mammedov            qapi_free_ACPIOSTInfo(info);
19276623d00SIgor Mammedov            trace_cpuhp_acpi_write_ost_status(cpu_st->selector,
19376623d00SIgor Mammedov                                              cdev->ost_status);
19476623d00SIgor Mammedov            break;
19576623d00SIgor Mammedov         }
19676623d00SIgor Mammedov         default:
19776623d00SIgor Mammedov            break;
19876623d00SIgor Mammedov         }
19976623d00SIgor Mammedov         break;
2005e1b5d93SIgor Mammedov     default:
2015e1b5d93SIgor Mammedov         break;
2025e1b5d93SIgor Mammedov     }
2035e1b5d93SIgor Mammedov }
2045e1b5d93SIgor Mammedov 
2055e1b5d93SIgor Mammedov static const MemoryRegionOps cpu_hotplug_ops = {
2065e1b5d93SIgor Mammedov     .read = cpu_hotplug_rd,
2075e1b5d93SIgor Mammedov     .write = cpu_hotplug_wr,
2085e1b5d93SIgor Mammedov     .endianness = DEVICE_LITTLE_ENDIAN,
2095e1b5d93SIgor Mammedov     .valid = {
2105e1b5d93SIgor Mammedov         .min_access_size = 1,
2115e1b5d93SIgor Mammedov         .max_access_size = 4,
2125e1b5d93SIgor Mammedov     },
2135e1b5d93SIgor Mammedov };
2145e1b5d93SIgor Mammedov 
cpu_hotplug_hw_init(MemoryRegion * as,Object * owner,CPUHotplugState * state,hwaddr base_addr)2155e1b5d93SIgor Mammedov void cpu_hotplug_hw_init(MemoryRegion *as, Object *owner,
2165e1b5d93SIgor Mammedov                          CPUHotplugState *state, hwaddr base_addr)
2175e1b5d93SIgor Mammedov {
2185e1b5d93SIgor Mammedov     MachineState *machine = MACHINE(qdev_get_machine());
2195e1b5d93SIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(machine);
22080e5db30SIgor Mammedov     const CPUArchIdList *id_list;
2215e1b5d93SIgor Mammedov     int i;
2225e1b5d93SIgor Mammedov 
2235e1b5d93SIgor Mammedov     assert(mc->possible_cpu_arch_ids);
2245e1b5d93SIgor Mammedov     id_list = mc->possible_cpu_arch_ids(machine);
2255e1b5d93SIgor Mammedov     state->dev_count = id_list->len;
2265e1b5d93SIgor Mammedov     state->devs = g_new0(typeof(*state->devs), state->dev_count);
2275e1b5d93SIgor Mammedov     for (i = 0; i < id_list->len; i++) {
2288aba3842SIgor Mammedov         state->devs[i].cpu =  CPU(id_list->cpus[i].cpu);
2295e1b5d93SIgor Mammedov         state->devs[i].arch_id = id_list->cpus[i].arch_id;
2305e1b5d93SIgor Mammedov     }
2315e1b5d93SIgor Mammedov     memory_region_init_io(&state->ctrl_reg, owner, &cpu_hotplug_ops, state,
232119a2ef1SKeqian Zhu                           "acpi-cpu-hotplug", ACPI_CPU_HOTPLUG_REG_LEN);
2335e1b5d93SIgor Mammedov     memory_region_add_subregion(as, base_addr, &state->ctrl_reg);
2345e1b5d93SIgor Mammedov }
2355e1b5d93SIgor Mammedov 
get_cpu_status(CPUHotplugState * cpu_st,DeviceState * dev)2365e1b5d93SIgor Mammedov static AcpiCpuStatus *get_cpu_status(CPUHotplugState *cpu_st, DeviceState *dev)
2375e1b5d93SIgor Mammedov {
2385e1b5d93SIgor Mammedov     CPUClass *k = CPU_GET_CLASS(dev);
2395e1b5d93SIgor Mammedov     uint64_t cpu_arch_id = k->get_arch_id(CPU(dev));
2405e1b5d93SIgor Mammedov     int i;
2415e1b5d93SIgor Mammedov 
2425e1b5d93SIgor Mammedov     for (i = 0; i < cpu_st->dev_count; i++) {
2435e1b5d93SIgor Mammedov         if (cpu_arch_id == cpu_st->devs[i].arch_id) {
2445e1b5d93SIgor Mammedov             return &cpu_st->devs[i];
2455e1b5d93SIgor Mammedov         }
2465e1b5d93SIgor Mammedov     }
2475e1b5d93SIgor Mammedov     return NULL;
2485e1b5d93SIgor Mammedov }
2495e1b5d93SIgor Mammedov 
acpi_cpu_plug_cb(HotplugHandler * hotplug_dev,CPUHotplugState * cpu_st,DeviceState * dev,Error ** errp)2505e1b5d93SIgor Mammedov void acpi_cpu_plug_cb(HotplugHandler *hotplug_dev,
2515e1b5d93SIgor Mammedov                       CPUHotplugState *cpu_st, DeviceState *dev, Error **errp)
2525e1b5d93SIgor Mammedov {
2535e1b5d93SIgor Mammedov     AcpiCpuStatus *cdev;
2545e1b5d93SIgor Mammedov 
2555e1b5d93SIgor Mammedov     cdev = get_cpu_status(cpu_st, dev);
2565e1b5d93SIgor Mammedov     if (!cdev) {
2575e1b5d93SIgor Mammedov         return;
2585e1b5d93SIgor Mammedov     }
2595e1b5d93SIgor Mammedov 
2605e1b5d93SIgor Mammedov     cdev->cpu = CPU(dev);
261d2238cb6SIgor Mammedov     if (dev->hotplugged) {
262d2238cb6SIgor Mammedov         cdev->is_inserting = true;
263d2238cb6SIgor Mammedov         acpi_send_event(DEVICE(hotplug_dev), ACPI_CPU_HOTPLUG_STATUS);
2645e1b5d93SIgor Mammedov     }
265d2238cb6SIgor Mammedov }
266d2238cb6SIgor Mammedov 
acpi_cpu_unplug_request_cb(HotplugHandler * hotplug_dev,CPUHotplugState * cpu_st,DeviceState * dev,Error ** errp)2678872c25aSIgor Mammedov void acpi_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
2688872c25aSIgor Mammedov                                 CPUHotplugState *cpu_st,
2698872c25aSIgor Mammedov                                 DeviceState *dev, Error **errp)
2708872c25aSIgor Mammedov {
2718872c25aSIgor Mammedov     AcpiCpuStatus *cdev;
2728872c25aSIgor Mammedov 
2738872c25aSIgor Mammedov     cdev = get_cpu_status(cpu_st, dev);
2748872c25aSIgor Mammedov     if (!cdev) {
2758872c25aSIgor Mammedov         return;
2768872c25aSIgor Mammedov     }
2778872c25aSIgor Mammedov 
2788872c25aSIgor Mammedov     cdev->is_removing = true;
2798872c25aSIgor Mammedov     acpi_send_event(DEVICE(hotplug_dev), ACPI_CPU_HOTPLUG_STATUS);
2808872c25aSIgor Mammedov }
2818872c25aSIgor Mammedov 
acpi_cpu_unplug_cb(CPUHotplugState * cpu_st,DeviceState * dev,Error ** errp)2828872c25aSIgor Mammedov void acpi_cpu_unplug_cb(CPUHotplugState *cpu_st,
2838872c25aSIgor Mammedov                         DeviceState *dev, Error **errp)
2848872c25aSIgor Mammedov {
2858872c25aSIgor Mammedov     AcpiCpuStatus *cdev;
2868872c25aSIgor Mammedov 
2878872c25aSIgor Mammedov     cdev = get_cpu_status(cpu_st, dev);
2888872c25aSIgor Mammedov     if (!cdev) {
2898872c25aSIgor Mammedov         return;
2908872c25aSIgor Mammedov     }
2918872c25aSIgor Mammedov 
2928872c25aSIgor Mammedov     cdev->cpu = NULL;
2938872c25aSIgor Mammedov }
2948872c25aSIgor Mammedov 
295d2238cb6SIgor Mammedov static const VMStateDescription vmstate_cpuhp_sts = {
296d2238cb6SIgor Mammedov     .name = "CPU hotplug device state",
297d2238cb6SIgor Mammedov     .version_id = 1,
298d2238cb6SIgor Mammedov     .minimum_version_id = 1,
299c559ba57SRichard Henderson     .fields = (const VMStateField[]) {
300d2238cb6SIgor Mammedov         VMSTATE_BOOL(is_inserting, AcpiCpuStatus),
3018872c25aSIgor Mammedov         VMSTATE_BOOL(is_removing, AcpiCpuStatus),
30276623d00SIgor Mammedov         VMSTATE_UINT32(ost_event, AcpiCpuStatus),
30376623d00SIgor Mammedov         VMSTATE_UINT32(ost_status, AcpiCpuStatus),
304d2238cb6SIgor Mammedov         VMSTATE_END_OF_LIST()
305d2238cb6SIgor Mammedov     }
306d2238cb6SIgor Mammedov };
3075e1b5d93SIgor Mammedov 
3085e1b5d93SIgor Mammedov const VMStateDescription vmstate_cpu_hotplug = {
3095e1b5d93SIgor Mammedov     .name = "CPU hotplug state",
3105e1b5d93SIgor Mammedov     .version_id = 1,
3115e1b5d93SIgor Mammedov     .minimum_version_id = 1,
312c559ba57SRichard Henderson     .fields = (const VMStateField[]) {
3135e1b5d93SIgor Mammedov         VMSTATE_UINT32(selector, CPUHotplugState),
314d2238cb6SIgor Mammedov         VMSTATE_UINT8(command, CPUHotplugState),
315d2238cb6SIgor Mammedov         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(devs, CPUHotplugState, dev_count,
316d2238cb6SIgor Mammedov                                              vmstate_cpuhp_sts, AcpiCpuStatus),
3175e1b5d93SIgor Mammedov         VMSTATE_END_OF_LIST()
3185e1b5d93SIgor Mammedov     }
3195e1b5d93SIgor Mammedov };
3205e1b5d93SIgor Mammedov 
3215e1b5d93SIgor Mammedov #define CPU_NAME_FMT      "C%.03X"
3225e1b5d93SIgor Mammedov #define CPUHP_RES_DEVICE  "PRES"
3235e1b5d93SIgor Mammedov #define CPU_LOCK          "CPLK"
3245e1b5d93SIgor Mammedov #define CPU_STS_METHOD    "CSTA"
325d2238cb6SIgor Mammedov #define CPU_SCAN_METHOD   "CSCN"
326d2238cb6SIgor Mammedov #define CPU_NOTIFY_METHOD "CTFY"
3278872c25aSIgor Mammedov #define CPU_EJECT_METHOD  "CEJ0"
32876623d00SIgor Mammedov #define CPU_OST_METHOD    "COST"
3299cc5a90bSIgor Mammedov #define CPU_ADDED_LIST    "CNEW"
3305e1b5d93SIgor Mammedov 
3315e1b5d93SIgor Mammedov #define CPU_ENABLED       "CPEN"
3325e1b5d93SIgor Mammedov #define CPU_SELECTOR      "CSEL"
333d2238cb6SIgor Mammedov #define CPU_COMMAND       "CCMD"
334d2238cb6SIgor Mammedov #define CPU_DATA          "CDAT"
335d2238cb6SIgor Mammedov #define CPU_INSERT_EVENT  "CINS"
3368872c25aSIgor Mammedov #define CPU_REMOVE_EVENT  "CRMV"
3378872c25aSIgor Mammedov #define CPU_EJECT_EVENT   "CEJ0"
33869dea9d6SIgor Mammedov #define CPU_FW_EJECT_EVENT "CEJF"
3395e1b5d93SIgor Mammedov 
build_cpus_aml(Aml * table,MachineState * machine,CPUHotplugFeatures opts,build_madt_cpu_fn build_madt_cpu,hwaddr base_addr,const char * res_root,const char * event_handler_method,AmlRegionSpace rs)3405e1b5d93SIgor Mammedov void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
341efdb43b8SSalil Mehta                     build_madt_cpu_fn build_madt_cpu, hwaddr base_addr,
342d2238cb6SIgor Mammedov                     const char *res_root,
343efdb43b8SSalil Mehta                     const char *event_handler_method,
344efdb43b8SSalil Mehta                     AmlRegionSpace rs)
3455e1b5d93SIgor Mammedov {
3465e1b5d93SIgor Mammedov     Aml *ifctx;
3475e1b5d93SIgor Mammedov     Aml *field;
3485e1b5d93SIgor Mammedov     Aml *method;
3495e1b5d93SIgor Mammedov     Aml *cpu_ctrl_dev;
3505e1b5d93SIgor Mammedov     Aml *cpus_dev;
3515e1b5d93SIgor Mammedov     Aml *zero = aml_int(0);
3525e1b5d93SIgor Mammedov     Aml *one = aml_int(1);
3535e1b5d93SIgor Mammedov     Aml *sb_scope = aml_scope("_SB");
3545e1b5d93SIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(machine);
35580e5db30SIgor Mammedov     const CPUArchIdList *arch_ids = mc->possible_cpu_arch_ids(machine);
3565e1b5d93SIgor Mammedov     char *cphp_res_path = g_strdup_printf("%s." CPUHP_RES_DEVICE, res_root);
3575e1b5d93SIgor Mammedov 
3585e1b5d93SIgor Mammedov     cpu_ctrl_dev = aml_device("%s", cphp_res_path);
3595e1b5d93SIgor Mammedov     {
3605e1b5d93SIgor Mammedov         Aml *crs;
3615e1b5d93SIgor Mammedov 
3625e1b5d93SIgor Mammedov         aml_append(cpu_ctrl_dev,
3635e1b5d93SIgor Mammedov             aml_name_decl("_HID", aml_eisaid("PNP0A06")));
3645e1b5d93SIgor Mammedov         aml_append(cpu_ctrl_dev,
3655e1b5d93SIgor Mammedov             aml_name_decl("_UID", aml_string("CPU Hotplug resources")));
3665e1b5d93SIgor Mammedov         aml_append(cpu_ctrl_dev, aml_mutex(CPU_LOCK, 0));
3675e1b5d93SIgor Mammedov 
368efdb43b8SSalil Mehta         assert((rs == AML_SYSTEM_IO) || (rs == AML_SYSTEM_MEMORY));
369efdb43b8SSalil Mehta 
3705e1b5d93SIgor Mammedov         crs = aml_resource_template();
371efdb43b8SSalil Mehta         if (rs == AML_SYSTEM_IO) {
372efdb43b8SSalil Mehta             aml_append(crs, aml_io(AML_DECODE16, base_addr, base_addr, 1,
3735e1b5d93SIgor Mammedov                                ACPI_CPU_HOTPLUG_REG_LEN));
374efdb43b8SSalil Mehta         } else if (rs == AML_SYSTEM_MEMORY) {
375efdb43b8SSalil Mehta             aml_append(crs, aml_memory32_fixed(base_addr,
376efdb43b8SSalil Mehta                                ACPI_CPU_HOTPLUG_REG_LEN, AML_READ_WRITE));
377efdb43b8SSalil Mehta         }
378efdb43b8SSalil Mehta 
3795e1b5d93SIgor Mammedov         aml_append(cpu_ctrl_dev, aml_name_decl("_CRS", crs));
3805e1b5d93SIgor Mammedov 
3815e1b5d93SIgor Mammedov         /* declare CPU hotplug MMIO region with related access fields */
3825e1b5d93SIgor Mammedov         aml_append(cpu_ctrl_dev,
383efdb43b8SSalil Mehta             aml_operation_region("PRST", rs, aml_int(base_addr),
3845e1b5d93SIgor Mammedov                                  ACPI_CPU_HOTPLUG_REG_LEN));
3855e1b5d93SIgor Mammedov 
3865e1b5d93SIgor Mammedov         field = aml_field("PRST", AML_BYTE_ACC, AML_NOLOCK,
3875e1b5d93SIgor Mammedov                           AML_WRITE_AS_ZEROS);
3885e1b5d93SIgor Mammedov         aml_append(field, aml_reserved_field(ACPI_CPU_FLAGS_OFFSET_RW * 8));
3895e1b5d93SIgor Mammedov         /* 1 if enabled, read only */
3905e1b5d93SIgor Mammedov         aml_append(field, aml_named_field(CPU_ENABLED, 1));
391d2238cb6SIgor Mammedov         /* (read) 1 if has a insert event. (write) 1 to clear event */
392d2238cb6SIgor Mammedov         aml_append(field, aml_named_field(CPU_INSERT_EVENT, 1));
3938872c25aSIgor Mammedov         /* (read) 1 if has a remove event. (write) 1 to clear event */
3948872c25aSIgor Mammedov         aml_append(field, aml_named_field(CPU_REMOVE_EVENT, 1));
3958872c25aSIgor Mammedov         /* initiates device eject, write only */
3968872c25aSIgor Mammedov         aml_append(field, aml_named_field(CPU_EJECT_EVENT, 1));
39769dea9d6SIgor Mammedov         /* tell firmware to do device eject, write only */
39869dea9d6SIgor Mammedov         aml_append(field, aml_named_field(CPU_FW_EJECT_EVENT, 1));
399*cb36e257SIgor Mammedov         aml_append(field, aml_reserved_field(3));
400d2238cb6SIgor Mammedov         aml_append(field, aml_named_field(CPU_COMMAND, 8));
4015e1b5d93SIgor Mammedov         aml_append(cpu_ctrl_dev, field);
4025e1b5d93SIgor Mammedov 
4035e1b5d93SIgor Mammedov         field = aml_field("PRST", AML_DWORD_ACC, AML_NOLOCK, AML_PRESERVE);
4045e1b5d93SIgor Mammedov         /* CPU selector, write only */
4055e1b5d93SIgor Mammedov         aml_append(field, aml_named_field(CPU_SELECTOR, 32));
406d2238cb6SIgor Mammedov         /* flags + cmd + 2byte align */
407d2238cb6SIgor Mammedov         aml_append(field, aml_reserved_field(4 * 8));
408d2238cb6SIgor Mammedov         aml_append(field, aml_named_field(CPU_DATA, 32));
4095e1b5d93SIgor Mammedov         aml_append(cpu_ctrl_dev, field);
4105e1b5d93SIgor Mammedov 
411679dd1a9SIgor Mammedov         if (opts.has_legacy_cphp) {
412679dd1a9SIgor Mammedov             method = aml_method("_INI", 0, AML_SERIALIZED);
413679dd1a9SIgor Mammedov             /* switch off legacy CPU hotplug HW and use new one,
414679dd1a9SIgor Mammedov              * on reboot system is in new mode and writing 0
415679dd1a9SIgor Mammedov              * in CPU_SELECTOR selects BSP, which is NOP at
416679dd1a9SIgor Mammedov              * the time _INI is called */
417679dd1a9SIgor Mammedov             aml_append(method, aml_store(zero, aml_name(CPU_SELECTOR)));
418679dd1a9SIgor Mammedov             aml_append(cpu_ctrl_dev, method);
419679dd1a9SIgor Mammedov         }
4205e1b5d93SIgor Mammedov     }
4215e1b5d93SIgor Mammedov     aml_append(sb_scope, cpu_ctrl_dev);
4225e1b5d93SIgor Mammedov 
4235e1b5d93SIgor Mammedov     cpus_dev = aml_device("\\_SB.CPUS");
4245e1b5d93SIgor Mammedov     {
4255e1b5d93SIgor Mammedov         int i;
4265e1b5d93SIgor Mammedov         Aml *ctrl_lock = aml_name("%s.%s", cphp_res_path, CPU_LOCK);
4275e1b5d93SIgor Mammedov         Aml *cpu_selector = aml_name("%s.%s", cphp_res_path, CPU_SELECTOR);
4285e1b5d93SIgor Mammedov         Aml *is_enabled = aml_name("%s.%s", cphp_res_path, CPU_ENABLED);
429d2238cb6SIgor Mammedov         Aml *cpu_cmd = aml_name("%s.%s", cphp_res_path, CPU_COMMAND);
430d2238cb6SIgor Mammedov         Aml *cpu_data = aml_name("%s.%s", cphp_res_path, CPU_DATA);
431d2238cb6SIgor Mammedov         Aml *ins_evt = aml_name("%s.%s", cphp_res_path, CPU_INSERT_EVENT);
4328872c25aSIgor Mammedov         Aml *rm_evt = aml_name("%s.%s", cphp_res_path, CPU_REMOVE_EVENT);
4338872c25aSIgor Mammedov         Aml *ej_evt = aml_name("%s.%s", cphp_res_path, CPU_EJECT_EVENT);
43469dea9d6SIgor Mammedov         Aml *fw_ej_evt = aml_name("%s.%s", cphp_res_path, CPU_FW_EJECT_EVENT);
4355e1b5d93SIgor Mammedov 
4365e1b5d93SIgor Mammedov         aml_append(cpus_dev, aml_name_decl("_HID", aml_string("ACPI0010")));
4375e1b5d93SIgor Mammedov         aml_append(cpus_dev, aml_name_decl("_CID", aml_eisaid("PNP0A05")));
4385e1b5d93SIgor Mammedov 
439d2238cb6SIgor Mammedov         method = aml_method(CPU_NOTIFY_METHOD, 2, AML_NOTSERIALIZED);
440d2238cb6SIgor Mammedov         for (i = 0; i < arch_ids->len; i++) {
441d2238cb6SIgor Mammedov             Aml *cpu = aml_name(CPU_NAME_FMT, i);
442d2238cb6SIgor Mammedov             Aml *uid = aml_arg(0);
443d2238cb6SIgor Mammedov             Aml *event = aml_arg(1);
444d2238cb6SIgor Mammedov 
445d2238cb6SIgor Mammedov             ifctx = aml_if(aml_equal(uid, aml_int(i)));
446d2238cb6SIgor Mammedov             {
447d2238cb6SIgor Mammedov                 aml_append(ifctx, aml_notify(cpu, event));
448d2238cb6SIgor Mammedov             }
449d2238cb6SIgor Mammedov             aml_append(method, ifctx);
450d2238cb6SIgor Mammedov         }
451d2238cb6SIgor Mammedov         aml_append(cpus_dev, method);
452d2238cb6SIgor Mammedov 
4535e1b5d93SIgor Mammedov         method = aml_method(CPU_STS_METHOD, 1, AML_SERIALIZED);
4545e1b5d93SIgor Mammedov         {
4555e1b5d93SIgor Mammedov             Aml *idx = aml_arg(0);
4565e1b5d93SIgor Mammedov             Aml *sta = aml_local(0);
4575e1b5d93SIgor Mammedov 
4585e1b5d93SIgor Mammedov             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
4595e1b5d93SIgor Mammedov             aml_append(method, aml_store(idx, cpu_selector));
4605e1b5d93SIgor Mammedov             aml_append(method, aml_store(zero, sta));
461*cb36e257SIgor Mammedov             ifctx = aml_if(aml_equal(is_enabled, one));
4625e1b5d93SIgor Mammedov             {
463*cb36e257SIgor Mammedov                 aml_append(ifctx, aml_store(aml_int(0xF), sta));
4645e1b5d93SIgor Mammedov             }
4655e1b5d93SIgor Mammedov             aml_append(method, ifctx);
4665e1b5d93SIgor Mammedov             aml_append(method, aml_release(ctrl_lock));
4675e1b5d93SIgor Mammedov             aml_append(method, aml_return(sta));
4685e1b5d93SIgor Mammedov         }
4695e1b5d93SIgor Mammedov         aml_append(cpus_dev, method);
4705e1b5d93SIgor Mammedov 
4718872c25aSIgor Mammedov         method = aml_method(CPU_EJECT_METHOD, 1, AML_SERIALIZED);
4728872c25aSIgor Mammedov         {
4738872c25aSIgor Mammedov             Aml *idx = aml_arg(0);
4748872c25aSIgor Mammedov 
4758872c25aSIgor Mammedov             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
4768872c25aSIgor Mammedov             aml_append(method, aml_store(idx, cpu_selector));
47769dea9d6SIgor Mammedov             if (opts.fw_unplugs_cpu) {
47869dea9d6SIgor Mammedov                 aml_append(method, aml_store(one, fw_ej_evt));
47969dea9d6SIgor Mammedov                 aml_append(method, aml_store(aml_int(OVMF_CPUHP_SMI_CMD),
48069dea9d6SIgor Mammedov                            aml_name("%s", opts.smi_path)));
48169dea9d6SIgor Mammedov             } else {
4828872c25aSIgor Mammedov                 aml_append(method, aml_store(one, ej_evt));
48369dea9d6SIgor Mammedov             }
4848872c25aSIgor Mammedov             aml_append(method, aml_release(ctrl_lock));
4858872c25aSIgor Mammedov         }
4868872c25aSIgor Mammedov         aml_append(cpus_dev, method);
4878872c25aSIgor Mammedov 
488d2238cb6SIgor Mammedov         method = aml_method(CPU_SCAN_METHOD, 0, AML_SERIALIZED);
489d2238cb6SIgor Mammedov         {
4909cc5a90bSIgor Mammedov             const uint8_t max_cpus_per_pass = 255;
4918872c25aSIgor Mammedov             Aml *else_ctx;
4929cc5a90bSIgor Mammedov             Aml *while_ctx, *while_ctx2;
493d2238cb6SIgor Mammedov             Aml *has_event = aml_local(0);
494d2238cb6SIgor Mammedov             Aml *dev_chk = aml_int(1);
4958872c25aSIgor Mammedov             Aml *eject_req = aml_int(3);
496d2238cb6SIgor Mammedov             Aml *next_cpu_cmd = aml_int(CPHP_GET_NEXT_CPU_WITH_EVENT_CMD);
4979cc5a90bSIgor Mammedov             Aml *num_added_cpus = aml_local(1);
4989cc5a90bSIgor Mammedov             Aml *cpu_idx = aml_local(2);
4999cc5a90bSIgor Mammedov             Aml *uid = aml_local(3);
5009cc5a90bSIgor Mammedov             Aml *has_job = aml_local(4);
5019cc5a90bSIgor Mammedov             Aml *new_cpus = aml_name(CPU_ADDED_LIST);
502d2238cb6SIgor Mammedov 
503d2238cb6SIgor Mammedov             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
5049cc5a90bSIgor Mammedov 
5059cc5a90bSIgor Mammedov             /*
5069cc5a90bSIgor Mammedov              * Windows versions newer than XP (including Windows 10/Windows
5079cc5a90bSIgor Mammedov              * Server 2019), do support* VarPackageOp but, it is cripled to hold
5089cc5a90bSIgor Mammedov              * the same elements number as old PackageOp.
5099cc5a90bSIgor Mammedov              * For compatibility with Windows XP (so it won't crash) use ACPI1.0
5109cc5a90bSIgor Mammedov              * PackageOp which can hold max 255 elements.
5119cc5a90bSIgor Mammedov              *
5129cc5a90bSIgor Mammedov              * use named package as old Windows don't support it in local var
5139cc5a90bSIgor Mammedov              */
5149cc5a90bSIgor Mammedov             aml_append(method, aml_name_decl(CPU_ADDED_LIST,
5159cc5a90bSIgor Mammedov                                              aml_package(max_cpus_per_pass)));
5169cc5a90bSIgor Mammedov 
5179cc5a90bSIgor Mammedov             aml_append(method, aml_store(zero, uid));
5189cc5a90bSIgor Mammedov             aml_append(method, aml_store(one, has_job));
5199cc5a90bSIgor Mammedov             /*
5209cc5a90bSIgor Mammedov              * CPU_ADDED_LIST can hold limited number of elements, outer loop
5219cc5a90bSIgor Mammedov              * allows to process CPUs in batches which let us to handle more
5229cc5a90bSIgor Mammedov              * CPUs than CPU_ADDED_LIST can hold.
5239cc5a90bSIgor Mammedov              */
5249cc5a90bSIgor Mammedov             while_ctx2 = aml_while(aml_equal(has_job, one));
525d2238cb6SIgor Mammedov             {
5269cc5a90bSIgor Mammedov                 aml_append(while_ctx2, aml_store(zero, has_job));
5279cc5a90bSIgor Mammedov 
5289cc5a90bSIgor Mammedov                 aml_append(while_ctx2, aml_store(one, has_event));
5299cc5a90bSIgor Mammedov                 aml_append(while_ctx2, aml_store(zero, num_added_cpus));
5309cc5a90bSIgor Mammedov 
5319cc5a90bSIgor Mammedov                 /*
5329cc5a90bSIgor Mammedov                  * Scan CPUs, till there are CPUs with events or
5339cc5a90bSIgor Mammedov                  * CPU_ADDED_LIST capacity is exhausted
5349cc5a90bSIgor Mammedov                  */
5359cc5a90bSIgor Mammedov                 while_ctx = aml_while(aml_land(aml_equal(has_event, one),
5369cc5a90bSIgor Mammedov                                       aml_lless(uid, aml_int(arch_ids->len))));
5379cc5a90bSIgor Mammedov                 {
5389cc5a90bSIgor Mammedov                      /*
5399cc5a90bSIgor Mammedov                       * clear loop exit condition, ins_evt/rm_evt checks will
5409cc5a90bSIgor Mammedov                       * set it to 1 while next_cpu_cmd returns a CPU with events
5419cc5a90bSIgor Mammedov                       */
542d2238cb6SIgor Mammedov                      aml_append(while_ctx, aml_store(zero, has_event));
5439cc5a90bSIgor Mammedov 
5449cc5a90bSIgor Mammedov                      aml_append(while_ctx, aml_store(uid, cpu_selector));
545d2238cb6SIgor Mammedov                      aml_append(while_ctx, aml_store(next_cpu_cmd, cpu_cmd));
5469cc5a90bSIgor Mammedov 
5479cc5a90bSIgor Mammedov                      /*
5489cc5a90bSIgor Mammedov                       * wrap around case, scan is complete, exit loop.
5499cc5a90bSIgor Mammedov                       * It happens since events are not cleared in scan loop,
5509cc5a90bSIgor Mammedov                       * so next_cpu_cmd continues to find already processed CPUs
5519cc5a90bSIgor Mammedov                       */
5529cc5a90bSIgor Mammedov                      ifctx = aml_if(aml_lless(cpu_data, uid));
5539cc5a90bSIgor Mammedov                      {
5549cc5a90bSIgor Mammedov                          aml_append(ifctx, aml_break());
5559cc5a90bSIgor Mammedov                      }
5569cc5a90bSIgor Mammedov                      aml_append(while_ctx, ifctx);
5579cc5a90bSIgor Mammedov 
5589cc5a90bSIgor Mammedov                      /*
5599cc5a90bSIgor Mammedov                       * if CPU_ADDED_LIST is full, exit inner loop and process
5609cc5a90bSIgor Mammedov                       * collected CPUs
5619cc5a90bSIgor Mammedov                       */
5629cc5a90bSIgor Mammedov                      ifctx = aml_if(
5639cc5a90bSIgor Mammedov                          aml_equal(num_added_cpus, aml_int(max_cpus_per_pass)));
5649cc5a90bSIgor Mammedov                      {
5659cc5a90bSIgor Mammedov                          aml_append(ifctx, aml_store(one, has_job));
5669cc5a90bSIgor Mammedov                          aml_append(ifctx, aml_break());
5679cc5a90bSIgor Mammedov                      }
5689cc5a90bSIgor Mammedov                      aml_append(while_ctx, ifctx);
5699cc5a90bSIgor Mammedov 
5709cc5a90bSIgor Mammedov                      aml_append(while_ctx, aml_store(cpu_data, uid));
571d2238cb6SIgor Mammedov                      ifctx = aml_if(aml_equal(ins_evt, one));
572d2238cb6SIgor Mammedov                      {
5739cc5a90bSIgor Mammedov                          /* cache added CPUs to Notify/Wakeup later */
5749cc5a90bSIgor Mammedov                          aml_append(ifctx, aml_store(uid,
5759cc5a90bSIgor Mammedov                              aml_index(new_cpus, num_added_cpus)));
5769cc5a90bSIgor Mammedov                          aml_append(ifctx, aml_increment(num_added_cpus));
577d2238cb6SIgor Mammedov                          aml_append(ifctx, aml_store(one, has_event));
578d2238cb6SIgor Mammedov                      }
579d2238cb6SIgor Mammedov                      aml_append(while_ctx, ifctx);
5808872c25aSIgor Mammedov                      else_ctx = aml_else();
5818872c25aSIgor Mammedov                      ifctx = aml_if(aml_equal(rm_evt, one));
5828872c25aSIgor Mammedov                      {
5838872c25aSIgor Mammedov                          aml_append(ifctx,
5849cc5a90bSIgor Mammedov                              aml_call2(CPU_NOTIFY_METHOD, uid, eject_req));
5858872c25aSIgor Mammedov                          aml_append(ifctx, aml_store(one, rm_evt));
5868872c25aSIgor Mammedov                          aml_append(ifctx, aml_store(one, has_event));
5878872c25aSIgor Mammedov                      }
5888872c25aSIgor Mammedov                      aml_append(else_ctx, ifctx);
5898872c25aSIgor Mammedov                      aml_append(while_ctx, else_ctx);
5909cc5a90bSIgor Mammedov                      aml_append(while_ctx, aml_increment(uid));
591d2238cb6SIgor Mammedov                 }
5929cc5a90bSIgor Mammedov                 aml_append(while_ctx2, while_ctx);
5939cc5a90bSIgor Mammedov 
5949cc5a90bSIgor Mammedov                 /*
5959cc5a90bSIgor Mammedov                  * in case FW negotiated ICH9_LPC_SMI_F_CPU_HOTPLUG_BIT,
5969cc5a90bSIgor Mammedov                  * make upcall to FW, so it can pull in new CPUs before
5979cc5a90bSIgor Mammedov                  * OS is notified and wakes them up
5989cc5a90bSIgor Mammedov                  */
5999cc5a90bSIgor Mammedov                 if (opts.smi_path) {
6009cc5a90bSIgor Mammedov                     ifctx = aml_if(aml_lgreater(num_added_cpus, zero));
6019cc5a90bSIgor Mammedov                     {
6029cc5a90bSIgor Mammedov                         aml_append(ifctx, aml_store(aml_int(OVMF_CPUHP_SMI_CMD),
6039cc5a90bSIgor Mammedov                             aml_name("%s", opts.smi_path)));
6049cc5a90bSIgor Mammedov                     }
6059cc5a90bSIgor Mammedov                     aml_append(while_ctx2, ifctx);
6069cc5a90bSIgor Mammedov                 }
6079cc5a90bSIgor Mammedov 
6089cc5a90bSIgor Mammedov                 /* Notify OSPM about new CPUs and clear insert events */
6099cc5a90bSIgor Mammedov                 aml_append(while_ctx2, aml_store(zero, cpu_idx));
6109cc5a90bSIgor Mammedov                 while_ctx = aml_while(aml_lless(cpu_idx, num_added_cpus));
6119cc5a90bSIgor Mammedov                 {
6129cc5a90bSIgor Mammedov                     aml_append(while_ctx,
6139cc5a90bSIgor Mammedov                         aml_store(aml_derefof(aml_index(new_cpus, cpu_idx)),
6149cc5a90bSIgor Mammedov                                   uid));
6159cc5a90bSIgor Mammedov                     aml_append(while_ctx,
6169cc5a90bSIgor Mammedov                         aml_call2(CPU_NOTIFY_METHOD, uid, dev_chk));
6179cc5a90bSIgor Mammedov                     aml_append(while_ctx, aml_store(uid, aml_debug()));
6189cc5a90bSIgor Mammedov                     aml_append(while_ctx, aml_store(uid, cpu_selector));
6199cc5a90bSIgor Mammedov                     aml_append(while_ctx, aml_store(one, ins_evt));
6209cc5a90bSIgor Mammedov                     aml_append(while_ctx, aml_increment(cpu_idx));
6219cc5a90bSIgor Mammedov                 }
6229cc5a90bSIgor Mammedov                 aml_append(while_ctx2, while_ctx);
6239cc5a90bSIgor Mammedov                 /*
6249cc5a90bSIgor Mammedov                  * If another batch is needed, then it will resume scanning
6259cc5a90bSIgor Mammedov                  * exactly at -- and not after -- the last CPU that's currently
6269cc5a90bSIgor Mammedov                  * in CPU_ADDED_LIST. In other words, the last CPU in
6279cc5a90bSIgor Mammedov                  * CPU_ADDED_LIST is going to be re-checked. That's OK: we've
6289cc5a90bSIgor Mammedov                  * just cleared the insert event for *all* CPUs in
6299cc5a90bSIgor Mammedov                  * CPU_ADDED_LIST, including the last one. So the scan will
6309cc5a90bSIgor Mammedov                  * simply seek past it.
6319cc5a90bSIgor Mammedov                  */
6329cc5a90bSIgor Mammedov             }
6339cc5a90bSIgor Mammedov             aml_append(method, while_ctx2);
634d2238cb6SIgor Mammedov             aml_append(method, aml_release(ctrl_lock));
635d2238cb6SIgor Mammedov         }
636d2238cb6SIgor Mammedov         aml_append(cpus_dev, method);
637d2238cb6SIgor Mammedov 
63876623d00SIgor Mammedov         method = aml_method(CPU_OST_METHOD, 4, AML_SERIALIZED);
63976623d00SIgor Mammedov         {
64076623d00SIgor Mammedov             Aml *uid = aml_arg(0);
64176623d00SIgor Mammedov             Aml *ev_cmd = aml_int(CPHP_OST_EVENT_CMD);
64276623d00SIgor Mammedov             Aml *st_cmd = aml_int(CPHP_OST_STATUS_CMD);
64376623d00SIgor Mammedov 
64476623d00SIgor Mammedov             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
64576623d00SIgor Mammedov             aml_append(method, aml_store(uid, cpu_selector));
64676623d00SIgor Mammedov             aml_append(method, aml_store(ev_cmd, cpu_cmd));
64776623d00SIgor Mammedov             aml_append(method, aml_store(aml_arg(1), cpu_data));
64876623d00SIgor Mammedov             aml_append(method, aml_store(st_cmd, cpu_cmd));
64976623d00SIgor Mammedov             aml_append(method, aml_store(aml_arg(2), cpu_data));
65076623d00SIgor Mammedov             aml_append(method, aml_release(ctrl_lock));
65176623d00SIgor Mammedov         }
65276623d00SIgor Mammedov         aml_append(cpus_dev, method);
65376623d00SIgor Mammedov 
6545e1b5d93SIgor Mammedov         /* build Processor object for each processor */
6555e1b5d93SIgor Mammedov         for (i = 0; i < arch_ids->len; i++) {
6565e1b5d93SIgor Mammedov             Aml *dev;
6575e1b5d93SIgor Mammedov             Aml *uid = aml_int(i);
658d2238cb6SIgor Mammedov             GArray *madt_buf = g_array_new(0, 1, 1);
6595e1b5d93SIgor Mammedov             int arch_id = arch_ids->cpus[i].arch_id;
6605e1b5d93SIgor Mammedov 
66189cb0c04SDr. David Alan Gilbert             if (opts.acpi_1_compatible && arch_id < 255) {
6625e1b5d93SIgor Mammedov                 dev = aml_processor(i, 0, 0, CPU_NAME_FMT, i);
6635e1b5d93SIgor Mammedov             } else {
6645e1b5d93SIgor Mammedov                 dev = aml_device(CPU_NAME_FMT, i);
6655e1b5d93SIgor Mammedov                 aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0007")));
6665e1b5d93SIgor Mammedov                 aml_append(dev, aml_name_decl("_UID", uid));
6675e1b5d93SIgor Mammedov             }
6685e1b5d93SIgor Mammedov 
6695e1b5d93SIgor Mammedov             method = aml_method("_STA", 0, AML_SERIALIZED);
6705e1b5d93SIgor Mammedov             aml_append(method, aml_return(aml_call1(CPU_STS_METHOD, uid)));
6715e1b5d93SIgor Mammedov             aml_append(dev, method);
6725e1b5d93SIgor Mammedov 
673d2238cb6SIgor Mammedov             /* build _MAT object */
6749a4fedcfSBernhard Beschow             build_madt_cpu(i, arch_ids, madt_buf, true); /* set enabled flag */
675d2238cb6SIgor Mammedov             aml_append(dev, aml_name_decl("_MAT",
676d2238cb6SIgor Mammedov                 aml_buffer(madt_buf->len, (uint8_t *)madt_buf->data)));
677d2238cb6SIgor Mammedov             g_array_free(madt_buf, true);
678d2238cb6SIgor Mammedov 
679c2d2a81bSIgor Mammedov             if (CPU(arch_ids->cpus[i].cpu) != first_cpu) {
6808872c25aSIgor Mammedov                 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
6818872c25aSIgor Mammedov                 aml_append(method, aml_call1(CPU_EJECT_METHOD, uid));
6828872c25aSIgor Mammedov                 aml_append(dev, method);
683c2d2a81bSIgor Mammedov             }
6848872c25aSIgor Mammedov 
68576623d00SIgor Mammedov             method = aml_method("_OST", 3, AML_SERIALIZED);
68676623d00SIgor Mammedov             aml_append(method,
68776623d00SIgor Mammedov                 aml_call4(CPU_OST_METHOD, uid, aml_arg(0),
68876623d00SIgor Mammedov                           aml_arg(1), aml_arg(2))
68976623d00SIgor Mammedov             );
69076623d00SIgor Mammedov             aml_append(dev, method);
69127111931SIgor Mammedov 
69227111931SIgor Mammedov             /* Linux guests discard SRAT info for non-present CPUs
69327111931SIgor Mammedov              * as a result _PXM is required for all CPUs which might
69427111931SIgor Mammedov              * be hot-plugged. For simplicity, add it for all CPUs.
69527111931SIgor Mammedov              */
696ea265072SIgor Mammedov             if (arch_ids->cpus[i].props.has_node_id) {
697ea265072SIgor Mammedov                 aml_append(dev, aml_name_decl("_PXM",
698ea265072SIgor Mammedov                            aml_int(arch_ids->cpus[i].props.node_id)));
69927111931SIgor Mammedov             }
70027111931SIgor Mammedov 
7015e1b5d93SIgor Mammedov             aml_append(cpus_dev, dev);
7025e1b5d93SIgor Mammedov         }
7035e1b5d93SIgor Mammedov     }
7045e1b5d93SIgor Mammedov     aml_append(sb_scope, cpus_dev);
7055e1b5d93SIgor Mammedov     aml_append(table, sb_scope);
7065e1b5d93SIgor Mammedov 
707d2238cb6SIgor Mammedov     method = aml_method(event_handler_method, 0, AML_NOTSERIALIZED);
708d2238cb6SIgor Mammedov     aml_append(method, aml_call0("\\_SB.CPUS." CPU_SCAN_METHOD));
709d2238cb6SIgor Mammedov     aml_append(table, method);
710d2238cb6SIgor Mammedov 
7115e1b5d93SIgor Mammedov     g_free(cphp_res_path);
7125e1b5d93SIgor Mammedov }
713