blob: 2d81c1e7908a67b74460be9e09897c00d4cd092a [file] [log] [blame]
Igor Mammedov5e1b5d92016-06-14 16:02:06 +02001#include "qemu/osdep.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +02002#include "migration/vmstate.h"
Igor Mammedov5e1b5d92016-06-14 16:02:06 +02003#include "hw/acpi/cpu.h"
Bernhard Beschow4f70dd52023-09-08 10:42:30 +02004#include "hw/core/cpu.h"
Igor Mammedov5e1b5d92016-06-14 16:02:06 +02005#include "qapi/error.h"
Philippe Mathieu-Daudé27c91882020-09-13 21:53:47 +02006#include "qapi/qapi-events-acpi.h"
Igor Mammedov5e1b5d92016-06-14 16:02:06 +02007#include "trace.h"
Igor Mammedov27111932016-10-05 17:51:24 +02008#include "sysemu/numa.h"
Igor Mammedov5e1b5d92016-06-14 16:02:06 +02009
10#define ACPI_CPU_HOTPLUG_REG_LEN 12
11#define ACPI_CPU_SELECTOR_OFFSET_WR 0
12#define ACPI_CPU_FLAGS_OFFSET_RW 4
Igor Mammedovd2238cb2016-06-14 16:13:32 +020013#define ACPI_CPU_CMD_OFFSET_WR 5
14#define ACPI_CPU_CMD_DATA_OFFSET_RW 8
Igor Mammedove6d0c3c2019-12-09 14:09:00 +010015#define ACPI_CPU_CMD_DATA2_OFFSET_R 0
Igor Mammedovd2238cb2016-06-14 16:13:32 +020016
Igor Mammedov9cc5a902020-09-23 05:46:48 -040017#define OVMF_CPUHP_SMI_CMD 4
18
Igor Mammedovd2238cb2016-06-14 16:13:32 +020019enum {
20 CPHP_GET_NEXT_CPU_WITH_EVENT_CMD = 0,
Igor Mammedov76623d02016-04-22 19:06:36 +020021 CPHP_OST_EVENT_CMD = 1,
22 CPHP_OST_STATUS_CMD = 2,
Igor Mammedov3a61c8d2019-12-09 14:09:02 +010023 CPHP_GET_CPU_ID_CMD = 3,
Igor Mammedovd2238cb2016-06-14 16:13:32 +020024 CPHP_CMD_MAX
25};
Igor Mammedov5e1b5d92016-06-14 16:02:06 +020026
Igor Mammedov76623d02016-04-22 19:06:36 +020027static 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);
Igor Mammedov76623d02016-04-22 19:06:36 +020039 }
40 }
41 return info;
42}
43
44void acpi_cpu_ospm_status(CPUHotplugState *cpu_st, ACPIOSTInfoList ***list)
45{
Eric Blakec3033fd2021-01-13 16:10:12 -060046 ACPIOSTInfoList ***tail = list;
Igor Mammedov76623d02016-04-22 19:06:36 +020047 int i;
48
49 for (i = 0; i < cpu_st->dev_count; i++) {
Eric Blakec3033fd2021-01-13 16:10:12 -060050 QAPI_LIST_APPEND(*tail, acpi_cpu_device_status(i, &cpu_st->devs[i]));
Igor Mammedov76623d02016-04-22 19:06:36 +020051 }
52}
53
Igor Mammedov5e1b5d92016-06-14 16:02:06 +020054static uint64_t cpu_hotplug_rd(void *opaque, hwaddr addr, unsigned size)
55{
56 uint64_t val = 0;
57 CPUHotplugState *cpu_st = opaque;
58 AcpiCpuStatus *cdev;
59
60 if (cpu_st->selector >= cpu_st->dev_count) {
61 return val;
62 }
63
64 cdev = &cpu_st->devs[cpu_st->selector];
65 switch (addr) {
66 case ACPI_CPU_FLAGS_OFFSET_RW: /* pack and return is_* fields */
67 val |= cdev->cpu ? 1 : 0;
Igor Mammedovd2238cb2016-06-14 16:13:32 +020068 val |= cdev->is_inserting ? 2 : 0;
Igor Mammedov8872c252016-06-14 16:14:02 +020069 val |= cdev->is_removing ? 4 : 0;
Igor Mammedov1e6107d2020-12-07 09:07:33 -050070 val |= cdev->fw_remove ? 16 : 0;
Igor Mammedov5e1b5d92016-06-14 16:02:06 +020071 trace_cpuhp_acpi_read_flags(cpu_st->selector, val);
72 break;
Igor Mammedovd2238cb2016-06-14 16:13:32 +020073 case ACPI_CPU_CMD_DATA_OFFSET_RW:
74 switch (cpu_st->command) {
75 case CPHP_GET_NEXT_CPU_WITH_EVENT_CMD:
76 val = cpu_st->selector;
77 break;
Igor Mammedov3a61c8d2019-12-09 14:09:02 +010078 case CPHP_GET_CPU_ID_CMD:
79 val = cdev->arch_id & 0xFFFFFFFF;
80 break;
Igor Mammedovd2238cb2016-06-14 16:13:32 +020081 default:
82 break;
83 }
84 trace_cpuhp_acpi_read_cmd_data(cpu_st->selector, val);
85 break;
Igor Mammedove6d0c3c2019-12-09 14:09:00 +010086 case ACPI_CPU_CMD_DATA2_OFFSET_R:
87 switch (cpu_st->command) {
88 case CPHP_GET_NEXT_CPU_WITH_EVENT_CMD:
89 val = 0;
90 break;
Igor Mammedov3a61c8d2019-12-09 14:09:02 +010091 case CPHP_GET_CPU_ID_CMD:
92 val = cdev->arch_id >> 32;
93 break;
Igor Mammedove6d0c3c2019-12-09 14:09:00 +010094 default:
95 break;
96 }
97 trace_cpuhp_acpi_read_cmd_data2(cpu_st->selector, val);
98 break;
Igor Mammedov5e1b5d92016-06-14 16:02:06 +020099 default:
100 break;
101 }
102 return val;
103}
104
105static void cpu_hotplug_wr(void *opaque, hwaddr addr, uint64_t data,
106 unsigned int size)
107{
108 CPUHotplugState *cpu_st = opaque;
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200109 AcpiCpuStatus *cdev;
Igor Mammedov76623d02016-04-22 19:06:36 +0200110 ACPIOSTInfo *info;
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200111
112 assert(cpu_st->dev_count);
113
114 if (addr) {
115 if (cpu_st->selector >= cpu_st->dev_count) {
116 trace_cpuhp_acpi_invalid_idx_selected(cpu_st->selector);
117 return;
118 }
119 }
120
121 switch (addr) {
122 case ACPI_CPU_SELECTOR_OFFSET_WR: /* current CPU selector */
123 cpu_st->selector = data;
124 trace_cpuhp_acpi_write_idx(cpu_st->selector);
125 break;
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200126 case ACPI_CPU_FLAGS_OFFSET_RW: /* set is_* fields */
127 cdev = &cpu_st->devs[cpu_st->selector];
128 if (data & 2) { /* clear insert event */
129 cdev->is_inserting = false;
130 trace_cpuhp_acpi_clear_inserting_evt(cpu_st->selector);
Igor Mammedov8872c252016-06-14 16:14:02 +0200131 } else if (data & 4) { /* clear remove event */
132 cdev->is_removing = false;
133 trace_cpuhp_acpi_clear_remove_evt(cpu_st->selector);
134 } else if (data & 8) {
135 DeviceState *dev = NULL;
136 HotplugHandler *hotplug_ctrl = NULL;
137
Igor Mammedovc2d2a812018-08-27 13:07:10 +0200138 if (!cdev->cpu || cdev->cpu == first_cpu) {
Igor Mammedov8872c252016-06-14 16:14:02 +0200139 trace_cpuhp_acpi_ejecting_invalid_cpu(cpu_st->selector);
140 break;
141 }
142
143 trace_cpuhp_acpi_ejecting_cpu(cpu_st->selector);
144 dev = DEVICE(cdev->cpu);
145 hotplug_ctrl = qdev_get_hotplug_handler(dev);
146 hotplug_handler_unplug(hotplug_ctrl, dev, NULL);
David Hildenbrand07578b02019-02-28 13:28:47 +0100147 object_unparent(OBJECT(dev));
Igor Mammedov1e6107d2020-12-07 09:07:33 -0500148 cdev->fw_remove = false;
149 } else if (data & 16) {
150 if (!cdev->cpu || cdev->cpu == first_cpu) {
151 trace_cpuhp_acpi_fw_remove_invalid_cpu(cpu_st->selector);
152 break;
153 }
154 trace_cpuhp_acpi_fw_remove_cpu(cpu_st->selector);
155 cdev->fw_remove = true;
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200156 }
157 break;
158 case ACPI_CPU_CMD_OFFSET_WR:
159 trace_cpuhp_acpi_write_cmd(cpu_st->selector, data);
160 if (data < CPHP_CMD_MAX) {
161 cpu_st->command = data;
162 if (cpu_st->command == CPHP_GET_NEXT_CPU_WITH_EVENT_CMD) {
163 uint32_t iter = cpu_st->selector;
164
165 do {
166 cdev = &cpu_st->devs[iter];
Igor Mammedov1e6107d2020-12-07 09:07:33 -0500167 if (cdev->is_inserting || cdev->is_removing ||
168 cdev->fw_remove) {
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200169 cpu_st->selector = iter;
170 trace_cpuhp_acpi_cpu_has_events(cpu_st->selector,
Igor Mammedov8872c252016-06-14 16:14:02 +0200171 cdev->is_inserting, cdev->is_removing);
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200172 break;
173 }
174 iter = iter + 1 < cpu_st->dev_count ? iter + 1 : 0;
175 } while (iter != cpu_st->selector);
176 }
177 }
178 break;
Igor Mammedov76623d02016-04-22 19:06:36 +0200179 case ACPI_CPU_CMD_DATA_OFFSET_RW:
180 switch (cpu_st->command) {
181 case CPHP_OST_EVENT_CMD: {
182 cdev = &cpu_st->devs[cpu_st->selector];
183 cdev->ost_event = data;
184 trace_cpuhp_acpi_write_ost_ev(cpu_st->selector, cdev->ost_event);
185 break;
186 }
187 case CPHP_OST_STATUS_CMD: {
188 cdev = &cpu_st->devs[cpu_st->selector];
189 cdev->ost_status = data;
190 info = acpi_cpu_device_status(cpu_st->selector, cdev);
Peter Xu3ab72382018-08-15 21:37:37 +0800191 qapi_event_send_acpi_device_ost(info);
Igor Mammedov76623d02016-04-22 19:06:36 +0200192 qapi_free_ACPIOSTInfo(info);
193 trace_cpuhp_acpi_write_ost_status(cpu_st->selector,
194 cdev->ost_status);
195 break;
196 }
197 default:
198 break;
199 }
200 break;
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200201 default:
202 break;
203 }
204}
205
206static const MemoryRegionOps cpu_hotplug_ops = {
207 .read = cpu_hotplug_rd,
208 .write = cpu_hotplug_wr,
209 .endianness = DEVICE_LITTLE_ENDIAN,
210 .valid = {
211 .min_access_size = 1,
212 .max_access_size = 4,
213 },
214};
215
216void cpu_hotplug_hw_init(MemoryRegion *as, Object *owner,
217 CPUHotplugState *state, hwaddr base_addr)
218{
219 MachineState *machine = MACHINE(qdev_get_machine());
220 MachineClass *mc = MACHINE_GET_CLASS(machine);
Igor Mammedov80e5db32017-01-18 18:13:20 +0100221 const CPUArchIdList *id_list;
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200222 int i;
223
224 assert(mc->possible_cpu_arch_ids);
225 id_list = mc->possible_cpu_arch_ids(machine);
226 state->dev_count = id_list->len;
227 state->devs = g_new0(typeof(*state->devs), state->dev_count);
228 for (i = 0; i < id_list->len; i++) {
Igor Mammedov8aba3842017-02-09 12:08:36 +0100229 state->devs[i].cpu = CPU(id_list->cpus[i].cpu);
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200230 state->devs[i].arch_id = id_list->cpus[i].arch_id;
231 }
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200232 memory_region_init_io(&state->ctrl_reg, owner, &cpu_hotplug_ops, state,
Keqian Zhu119a2ef2020-04-13 17:15:52 +0800233 "acpi-cpu-hotplug", ACPI_CPU_HOTPLUG_REG_LEN);
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200234 memory_region_add_subregion(as, base_addr, &state->ctrl_reg);
235}
236
237static AcpiCpuStatus *get_cpu_status(CPUHotplugState *cpu_st, DeviceState *dev)
238{
239 CPUClass *k = CPU_GET_CLASS(dev);
240 uint64_t cpu_arch_id = k->get_arch_id(CPU(dev));
241 int i;
242
243 for (i = 0; i < cpu_st->dev_count; i++) {
244 if (cpu_arch_id == cpu_st->devs[i].arch_id) {
245 return &cpu_st->devs[i];
246 }
247 }
248 return NULL;
249}
250
251void acpi_cpu_plug_cb(HotplugHandler *hotplug_dev,
252 CPUHotplugState *cpu_st, DeviceState *dev, Error **errp)
253{
254 AcpiCpuStatus *cdev;
255
256 cdev = get_cpu_status(cpu_st, dev);
257 if (!cdev) {
258 return;
259 }
260
261 cdev->cpu = CPU(dev);
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200262 if (dev->hotplugged) {
263 cdev->is_inserting = true;
264 acpi_send_event(DEVICE(hotplug_dev), ACPI_CPU_HOTPLUG_STATUS);
265 }
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200266}
267
Igor Mammedov8872c252016-06-14 16:14:02 +0200268void acpi_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
269 CPUHotplugState *cpu_st,
270 DeviceState *dev, Error **errp)
271{
272 AcpiCpuStatus *cdev;
273
274 cdev = get_cpu_status(cpu_st, dev);
275 if (!cdev) {
276 return;
277 }
278
279 cdev->is_removing = true;
280 acpi_send_event(DEVICE(hotplug_dev), ACPI_CPU_HOTPLUG_STATUS);
281}
282
283void acpi_cpu_unplug_cb(CPUHotplugState *cpu_st,
284 DeviceState *dev, Error **errp)
285{
286 AcpiCpuStatus *cdev;
287
288 cdev = get_cpu_status(cpu_st, dev);
289 if (!cdev) {
290 return;
291 }
292
293 cdev->cpu = NULL;
294}
295
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200296static const VMStateDescription vmstate_cpuhp_sts = {
297 .name = "CPU hotplug device state",
298 .version_id = 1,
299 .minimum_version_id = 1,
Richard Hendersonc559ba52023-12-21 14:16:02 +1100300 .fields = (const VMStateField[]) {
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200301 VMSTATE_BOOL(is_inserting, AcpiCpuStatus),
Igor Mammedov8872c252016-06-14 16:14:02 +0200302 VMSTATE_BOOL(is_removing, AcpiCpuStatus),
Igor Mammedov76623d02016-04-22 19:06:36 +0200303 VMSTATE_UINT32(ost_event, AcpiCpuStatus),
304 VMSTATE_UINT32(ost_status, AcpiCpuStatus),
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200305 VMSTATE_END_OF_LIST()
306 }
307};
308
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200309const VMStateDescription vmstate_cpu_hotplug = {
310 .name = "CPU hotplug state",
311 .version_id = 1,
312 .minimum_version_id = 1,
Richard Hendersonc559ba52023-12-21 14:16:02 +1100313 .fields = (const VMStateField[]) {
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200314 VMSTATE_UINT32(selector, CPUHotplugState),
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200315 VMSTATE_UINT8(command, CPUHotplugState),
316 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(devs, CPUHotplugState, dev_count,
317 vmstate_cpuhp_sts, AcpiCpuStatus),
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200318 VMSTATE_END_OF_LIST()
319 }
320};
321
322#define CPU_NAME_FMT "C%.03X"
323#define CPUHP_RES_DEVICE "PRES"
324#define CPU_LOCK "CPLK"
325#define CPU_STS_METHOD "CSTA"
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200326#define CPU_SCAN_METHOD "CSCN"
327#define CPU_NOTIFY_METHOD "CTFY"
Igor Mammedov8872c252016-06-14 16:14:02 +0200328#define CPU_EJECT_METHOD "CEJ0"
Igor Mammedov76623d02016-04-22 19:06:36 +0200329#define CPU_OST_METHOD "COST"
Igor Mammedov9cc5a902020-09-23 05:46:48 -0400330#define CPU_ADDED_LIST "CNEW"
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200331
332#define CPU_ENABLED "CPEN"
333#define CPU_SELECTOR "CSEL"
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200334#define CPU_COMMAND "CCMD"
335#define CPU_DATA "CDAT"
336#define CPU_INSERT_EVENT "CINS"
Igor Mammedov8872c252016-06-14 16:14:02 +0200337#define CPU_REMOVE_EVENT "CRMV"
338#define CPU_EJECT_EVENT "CEJ0"
Igor Mammedov69dea9d2020-12-07 09:07:36 -0500339#define CPU_FW_EJECT_EVENT "CEJF"
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200340
341void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
Bernhard Beschow9a4fedc2023-09-08 10:42:28 +0200342 build_madt_cpu_fn build_madt_cpu, hwaddr io_base,
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200343 const char *res_root,
344 const char *event_handler_method)
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200345{
346 Aml *ifctx;
347 Aml *field;
348 Aml *method;
349 Aml *cpu_ctrl_dev;
350 Aml *cpus_dev;
351 Aml *zero = aml_int(0);
352 Aml *one = aml_int(1);
353 Aml *sb_scope = aml_scope("_SB");
354 MachineClass *mc = MACHINE_GET_CLASS(machine);
Igor Mammedov80e5db32017-01-18 18:13:20 +0100355 const CPUArchIdList *arch_ids = mc->possible_cpu_arch_ids(machine);
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200356 char *cphp_res_path = g_strdup_printf("%s." CPUHP_RES_DEVICE, res_root);
357
358 cpu_ctrl_dev = aml_device("%s", cphp_res_path);
359 {
360 Aml *crs;
361
362 aml_append(cpu_ctrl_dev,
363 aml_name_decl("_HID", aml_eisaid("PNP0A06")));
364 aml_append(cpu_ctrl_dev,
365 aml_name_decl("_UID", aml_string("CPU Hotplug resources")));
366 aml_append(cpu_ctrl_dev, aml_mutex(CPU_LOCK, 0));
367
368 crs = aml_resource_template();
369 aml_append(crs, aml_io(AML_DECODE16, io_base, io_base, 1,
370 ACPI_CPU_HOTPLUG_REG_LEN));
371 aml_append(cpu_ctrl_dev, aml_name_decl("_CRS", crs));
372
373 /* declare CPU hotplug MMIO region with related access fields */
374 aml_append(cpu_ctrl_dev,
375 aml_operation_region("PRST", AML_SYSTEM_IO, aml_int(io_base),
376 ACPI_CPU_HOTPLUG_REG_LEN));
377
378 field = aml_field("PRST", AML_BYTE_ACC, AML_NOLOCK,
379 AML_WRITE_AS_ZEROS);
380 aml_append(field, aml_reserved_field(ACPI_CPU_FLAGS_OFFSET_RW * 8));
381 /* 1 if enabled, read only */
382 aml_append(field, aml_named_field(CPU_ENABLED, 1));
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200383 /* (read) 1 if has a insert event. (write) 1 to clear event */
384 aml_append(field, aml_named_field(CPU_INSERT_EVENT, 1));
Igor Mammedov8872c252016-06-14 16:14:02 +0200385 /* (read) 1 if has a remove event. (write) 1 to clear event */
386 aml_append(field, aml_named_field(CPU_REMOVE_EVENT, 1));
387 /* initiates device eject, write only */
388 aml_append(field, aml_named_field(CPU_EJECT_EVENT, 1));
Igor Mammedov69dea9d2020-12-07 09:07:36 -0500389 /* tell firmware to do device eject, write only */
390 aml_append(field, aml_named_field(CPU_FW_EJECT_EVENT, 1));
391 aml_append(field, aml_reserved_field(3));
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200392 aml_append(field, aml_named_field(CPU_COMMAND, 8));
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200393 aml_append(cpu_ctrl_dev, field);
394
395 field = aml_field("PRST", AML_DWORD_ACC, AML_NOLOCK, AML_PRESERVE);
396 /* CPU selector, write only */
397 aml_append(field, aml_named_field(CPU_SELECTOR, 32));
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200398 /* flags + cmd + 2byte align */
399 aml_append(field, aml_reserved_field(4 * 8));
400 aml_append(field, aml_named_field(CPU_DATA, 32));
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200401 aml_append(cpu_ctrl_dev, field);
402
Igor Mammedov679dd1a2016-06-15 11:25:23 +0200403 if (opts.has_legacy_cphp) {
404 method = aml_method("_INI", 0, AML_SERIALIZED);
405 /* switch off legacy CPU hotplug HW and use new one,
406 * on reboot system is in new mode and writing 0
407 * in CPU_SELECTOR selects BSP, which is NOP at
408 * the time _INI is called */
409 aml_append(method, aml_store(zero, aml_name(CPU_SELECTOR)));
410 aml_append(cpu_ctrl_dev, method);
411 }
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200412 }
413 aml_append(sb_scope, cpu_ctrl_dev);
414
415 cpus_dev = aml_device("\\_SB.CPUS");
416 {
417 int i;
418 Aml *ctrl_lock = aml_name("%s.%s", cphp_res_path, CPU_LOCK);
419 Aml *cpu_selector = aml_name("%s.%s", cphp_res_path, CPU_SELECTOR);
420 Aml *is_enabled = aml_name("%s.%s", cphp_res_path, CPU_ENABLED);
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200421 Aml *cpu_cmd = aml_name("%s.%s", cphp_res_path, CPU_COMMAND);
422 Aml *cpu_data = aml_name("%s.%s", cphp_res_path, CPU_DATA);
423 Aml *ins_evt = aml_name("%s.%s", cphp_res_path, CPU_INSERT_EVENT);
Igor Mammedov8872c252016-06-14 16:14:02 +0200424 Aml *rm_evt = aml_name("%s.%s", cphp_res_path, CPU_REMOVE_EVENT);
425 Aml *ej_evt = aml_name("%s.%s", cphp_res_path, CPU_EJECT_EVENT);
Igor Mammedov69dea9d2020-12-07 09:07:36 -0500426 Aml *fw_ej_evt = aml_name("%s.%s", cphp_res_path, CPU_FW_EJECT_EVENT);
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200427
428 aml_append(cpus_dev, aml_name_decl("_HID", aml_string("ACPI0010")));
429 aml_append(cpus_dev, aml_name_decl("_CID", aml_eisaid("PNP0A05")));
430
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200431 method = aml_method(CPU_NOTIFY_METHOD, 2, AML_NOTSERIALIZED);
432 for (i = 0; i < arch_ids->len; i++) {
433 Aml *cpu = aml_name(CPU_NAME_FMT, i);
434 Aml *uid = aml_arg(0);
435 Aml *event = aml_arg(1);
436
437 ifctx = aml_if(aml_equal(uid, aml_int(i)));
438 {
439 aml_append(ifctx, aml_notify(cpu, event));
440 }
441 aml_append(method, ifctx);
442 }
443 aml_append(cpus_dev, method);
444
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200445 method = aml_method(CPU_STS_METHOD, 1, AML_SERIALIZED);
446 {
447 Aml *idx = aml_arg(0);
448 Aml *sta = aml_local(0);
449
450 aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
451 aml_append(method, aml_store(idx, cpu_selector));
452 aml_append(method, aml_store(zero, sta));
453 ifctx = aml_if(aml_equal(is_enabled, one));
454 {
455 aml_append(ifctx, aml_store(aml_int(0xF), sta));
456 }
457 aml_append(method, ifctx);
458 aml_append(method, aml_release(ctrl_lock));
459 aml_append(method, aml_return(sta));
460 }
461 aml_append(cpus_dev, method);
462
Igor Mammedov8872c252016-06-14 16:14:02 +0200463 method = aml_method(CPU_EJECT_METHOD, 1, AML_SERIALIZED);
464 {
465 Aml *idx = aml_arg(0);
466
467 aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
468 aml_append(method, aml_store(idx, cpu_selector));
Igor Mammedov69dea9d2020-12-07 09:07:36 -0500469 if (opts.fw_unplugs_cpu) {
470 aml_append(method, aml_store(one, fw_ej_evt));
471 aml_append(method, aml_store(aml_int(OVMF_CPUHP_SMI_CMD),
472 aml_name("%s", opts.smi_path)));
473 } else {
474 aml_append(method, aml_store(one, ej_evt));
475 }
Igor Mammedov8872c252016-06-14 16:14:02 +0200476 aml_append(method, aml_release(ctrl_lock));
477 }
478 aml_append(cpus_dev, method);
479
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200480 method = aml_method(CPU_SCAN_METHOD, 0, AML_SERIALIZED);
481 {
Igor Mammedov9cc5a902020-09-23 05:46:48 -0400482 const uint8_t max_cpus_per_pass = 255;
Igor Mammedov8872c252016-06-14 16:14:02 +0200483 Aml *else_ctx;
Igor Mammedov9cc5a902020-09-23 05:46:48 -0400484 Aml *while_ctx, *while_ctx2;
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200485 Aml *has_event = aml_local(0);
486 Aml *dev_chk = aml_int(1);
Igor Mammedov8872c252016-06-14 16:14:02 +0200487 Aml *eject_req = aml_int(3);
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200488 Aml *next_cpu_cmd = aml_int(CPHP_GET_NEXT_CPU_WITH_EVENT_CMD);
Igor Mammedov9cc5a902020-09-23 05:46:48 -0400489 Aml *num_added_cpus = aml_local(1);
490 Aml *cpu_idx = aml_local(2);
491 Aml *uid = aml_local(3);
492 Aml *has_job = aml_local(4);
493 Aml *new_cpus = aml_name(CPU_ADDED_LIST);
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200494
495 aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
Igor Mammedov9cc5a902020-09-23 05:46:48 -0400496
497 /*
498 * Windows versions newer than XP (including Windows 10/Windows
499 * Server 2019), do support* VarPackageOp but, it is cripled to hold
500 * the same elements number as old PackageOp.
501 * For compatibility with Windows XP (so it won't crash) use ACPI1.0
502 * PackageOp which can hold max 255 elements.
503 *
504 * use named package as old Windows don't support it in local var
505 */
506 aml_append(method, aml_name_decl(CPU_ADDED_LIST,
507 aml_package(max_cpus_per_pass)));
508
509 aml_append(method, aml_store(zero, uid));
510 aml_append(method, aml_store(one, has_job));
511 /*
512 * CPU_ADDED_LIST can hold limited number of elements, outer loop
513 * allows to process CPUs in batches which let us to handle more
514 * CPUs than CPU_ADDED_LIST can hold.
515 */
516 while_ctx2 = aml_while(aml_equal(has_job, one));
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200517 {
Igor Mammedov9cc5a902020-09-23 05:46:48 -0400518 aml_append(while_ctx2, aml_store(zero, has_job));
519
520 aml_append(while_ctx2, aml_store(one, has_event));
521 aml_append(while_ctx2, aml_store(zero, num_added_cpus));
522
523 /*
524 * Scan CPUs, till there are CPUs with events or
525 * CPU_ADDED_LIST capacity is exhausted
526 */
527 while_ctx = aml_while(aml_land(aml_equal(has_event, one),
528 aml_lless(uid, aml_int(arch_ids->len))));
529 {
530 /*
531 * clear loop exit condition, ins_evt/rm_evt checks will
532 * set it to 1 while next_cpu_cmd returns a CPU with events
533 */
534 aml_append(while_ctx, aml_store(zero, has_event));
535
536 aml_append(while_ctx, aml_store(uid, cpu_selector));
537 aml_append(while_ctx, aml_store(next_cpu_cmd, cpu_cmd));
538
539 /*
540 * wrap around case, scan is complete, exit loop.
541 * It happens since events are not cleared in scan loop,
542 * so next_cpu_cmd continues to find already processed CPUs
543 */
544 ifctx = aml_if(aml_lless(cpu_data, uid));
545 {
546 aml_append(ifctx, aml_break());
547 }
548 aml_append(while_ctx, ifctx);
549
550 /*
551 * if CPU_ADDED_LIST is full, exit inner loop and process
552 * collected CPUs
553 */
554 ifctx = aml_if(
555 aml_equal(num_added_cpus, aml_int(max_cpus_per_pass)));
556 {
557 aml_append(ifctx, aml_store(one, has_job));
558 aml_append(ifctx, aml_break());
559 }
560 aml_append(while_ctx, ifctx);
561
562 aml_append(while_ctx, aml_store(cpu_data, uid));
563 ifctx = aml_if(aml_equal(ins_evt, one));
564 {
565 /* cache added CPUs to Notify/Wakeup later */
566 aml_append(ifctx, aml_store(uid,
567 aml_index(new_cpus, num_added_cpus)));
568 aml_append(ifctx, aml_increment(num_added_cpus));
569 aml_append(ifctx, aml_store(one, has_event));
570 }
571 aml_append(while_ctx, ifctx);
572 else_ctx = aml_else();
573 ifctx = aml_if(aml_equal(rm_evt, one));
574 {
575 aml_append(ifctx,
576 aml_call2(CPU_NOTIFY_METHOD, uid, eject_req));
577 aml_append(ifctx, aml_store(one, rm_evt));
578 aml_append(ifctx, aml_store(one, has_event));
579 }
580 aml_append(else_ctx, ifctx);
581 aml_append(while_ctx, else_ctx);
582 aml_append(while_ctx, aml_increment(uid));
583 }
584 aml_append(while_ctx2, while_ctx);
585
586 /*
587 * in case FW negotiated ICH9_LPC_SMI_F_CPU_HOTPLUG_BIT,
588 * make upcall to FW, so it can pull in new CPUs before
589 * OS is notified and wakes them up
590 */
591 if (opts.smi_path) {
592 ifctx = aml_if(aml_lgreater(num_added_cpus, zero));
593 {
594 aml_append(ifctx, aml_store(aml_int(OVMF_CPUHP_SMI_CMD),
595 aml_name("%s", opts.smi_path)));
596 }
597 aml_append(while_ctx2, ifctx);
598 }
599
600 /* Notify OSPM about new CPUs and clear insert events */
601 aml_append(while_ctx2, aml_store(zero, cpu_idx));
602 while_ctx = aml_while(aml_lless(cpu_idx, num_added_cpus));
603 {
604 aml_append(while_ctx,
605 aml_store(aml_derefof(aml_index(new_cpus, cpu_idx)),
606 uid));
607 aml_append(while_ctx,
608 aml_call2(CPU_NOTIFY_METHOD, uid, dev_chk));
609 aml_append(while_ctx, aml_store(uid, aml_debug()));
610 aml_append(while_ctx, aml_store(uid, cpu_selector));
611 aml_append(while_ctx, aml_store(one, ins_evt));
612 aml_append(while_ctx, aml_increment(cpu_idx));
613 }
614 aml_append(while_ctx2, while_ctx);
615 /*
616 * If another batch is needed, then it will resume scanning
617 * exactly at -- and not after -- the last CPU that's currently
618 * in CPU_ADDED_LIST. In other words, the last CPU in
619 * CPU_ADDED_LIST is going to be re-checked. That's OK: we've
620 * just cleared the insert event for *all* CPUs in
621 * CPU_ADDED_LIST, including the last one. So the scan will
622 * simply seek past it.
623 */
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200624 }
Igor Mammedov9cc5a902020-09-23 05:46:48 -0400625 aml_append(method, while_ctx2);
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200626 aml_append(method, aml_release(ctrl_lock));
627 }
628 aml_append(cpus_dev, method);
629
Igor Mammedov76623d02016-04-22 19:06:36 +0200630 method = aml_method(CPU_OST_METHOD, 4, AML_SERIALIZED);
631 {
632 Aml *uid = aml_arg(0);
633 Aml *ev_cmd = aml_int(CPHP_OST_EVENT_CMD);
634 Aml *st_cmd = aml_int(CPHP_OST_STATUS_CMD);
635
636 aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
637 aml_append(method, aml_store(uid, cpu_selector));
638 aml_append(method, aml_store(ev_cmd, cpu_cmd));
639 aml_append(method, aml_store(aml_arg(1), cpu_data));
640 aml_append(method, aml_store(st_cmd, cpu_cmd));
641 aml_append(method, aml_store(aml_arg(2), cpu_data));
642 aml_append(method, aml_release(ctrl_lock));
643 }
644 aml_append(cpus_dev, method);
645
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200646 /* build Processor object for each processor */
647 for (i = 0; i < arch_ids->len; i++) {
648 Aml *dev;
649 Aml *uid = aml_int(i);
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200650 GArray *madt_buf = g_array_new(0, 1, 1);
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200651 int arch_id = arch_ids->cpus[i].arch_id;
652
Dr. David Alan Gilbert89cb0c02019-01-25 09:40:46 +0000653 if (opts.acpi_1_compatible && arch_id < 255) {
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200654 dev = aml_processor(i, 0, 0, CPU_NAME_FMT, i);
655 } else {
656 dev = aml_device(CPU_NAME_FMT, i);
657 aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0007")));
658 aml_append(dev, aml_name_decl("_UID", uid));
659 }
660
661 method = aml_method("_STA", 0, AML_SERIALIZED);
662 aml_append(method, aml_return(aml_call1(CPU_STS_METHOD, uid)));
663 aml_append(dev, method);
664
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200665 /* build _MAT object */
Bernhard Beschow9a4fedc2023-09-08 10:42:28 +0200666 build_madt_cpu(i, arch_ids, madt_buf, true); /* set enabled flag */
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200667 aml_append(dev, aml_name_decl("_MAT",
668 aml_buffer(madt_buf->len, (uint8_t *)madt_buf->data)));
669 g_array_free(madt_buf, true);
670
Igor Mammedovc2d2a812018-08-27 13:07:10 +0200671 if (CPU(arch_ids->cpus[i].cpu) != first_cpu) {
672 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
673 aml_append(method, aml_call1(CPU_EJECT_METHOD, uid));
674 aml_append(dev, method);
675 }
Igor Mammedov8872c252016-06-14 16:14:02 +0200676
Igor Mammedov76623d02016-04-22 19:06:36 +0200677 method = aml_method("_OST", 3, AML_SERIALIZED);
678 aml_append(method,
679 aml_call4(CPU_OST_METHOD, uid, aml_arg(0),
680 aml_arg(1), aml_arg(2))
681 );
682 aml_append(dev, method);
Igor Mammedov27111932016-10-05 17:51:24 +0200683
684 /* Linux guests discard SRAT info for non-present CPUs
685 * as a result _PXM is required for all CPUs which might
686 * be hot-plugged. For simplicity, add it for all CPUs.
687 */
Igor Mammedovea265072017-05-10 13:29:52 +0200688 if (arch_ids->cpus[i].props.has_node_id) {
689 aml_append(dev, aml_name_decl("_PXM",
690 aml_int(arch_ids->cpus[i].props.node_id)));
Igor Mammedov27111932016-10-05 17:51:24 +0200691 }
692
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200693 aml_append(cpus_dev, dev);
694 }
695 }
696 aml_append(sb_scope, cpus_dev);
697 aml_append(table, sb_scope);
698
Igor Mammedovd2238cb2016-06-14 16:13:32 +0200699 method = aml_method(event_handler_method, 0, AML_NOTSERIALIZED);
700 aml_append(method, aml_call0("\\_SB.CPUS." CPU_SCAN_METHOD));
701 aml_append(table, method);
702
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200703 g_free(cphp_res_path);
Igor Mammedov5e1b5d92016-06-14 16:02:06 +0200704}