target-i386/kvm: cache the return value of kvm_enable_x2apic()
[qemu.git] / hw / acpi / cpu.c
blob902f5c90a80cfe20cfcf44750b09fce0664fc067
1 #include "qemu/osdep.h"
2 #include "hw/boards.h"
3 #include "hw/acpi/cpu.h"
4 #include "qapi/error.h"
5 #include "qapi-event.h"
6 #include "trace.h"
7 #include "sysemu/numa.h"
9 #define ACPI_CPU_HOTPLUG_REG_LEN 12
10 #define ACPI_CPU_SELECTOR_OFFSET_WR 0
11 #define ACPI_CPU_FLAGS_OFFSET_RW 4
12 #define ACPI_CPU_CMD_OFFSET_WR 5
13 #define ACPI_CPU_CMD_DATA_OFFSET_RW 8
15 enum {
16 CPHP_GET_NEXT_CPU_WITH_EVENT_CMD = 0,
17 CPHP_OST_EVENT_CMD = 1,
18 CPHP_OST_STATUS_CMD = 2,
19 CPHP_CMD_MAX
22 static ACPIOSTInfo *acpi_cpu_device_status(int idx, AcpiCpuStatus *cdev)
24 ACPIOSTInfo *info = g_new0(ACPIOSTInfo, 1);
26 info->slot_type = ACPI_SLOT_TYPE_CPU;
27 info->slot = g_strdup_printf("%d", idx);
28 info->source = cdev->ost_event;
29 info->status = cdev->ost_status;
30 if (cdev->cpu) {
31 DeviceState *dev = DEVICE(cdev->cpu);
32 if (dev->id) {
33 info->device = g_strdup(dev->id);
34 info->has_device = true;
37 return info;
40 void acpi_cpu_ospm_status(CPUHotplugState *cpu_st, ACPIOSTInfoList ***list)
42 int i;
44 for (i = 0; i < cpu_st->dev_count; i++) {
45 ACPIOSTInfoList *elem = g_new0(ACPIOSTInfoList, 1);
46 elem->value = acpi_cpu_device_status(i, &cpu_st->devs[i]);
47 elem->next = NULL;
48 **list = elem;
49 *list = &elem->next;
53 static uint64_t cpu_hotplug_rd(void *opaque, hwaddr addr, unsigned size)
55 uint64_t val = 0;
56 CPUHotplugState *cpu_st = opaque;
57 AcpiCpuStatus *cdev;
59 if (cpu_st->selector >= cpu_st->dev_count) {
60 return val;
63 cdev = &cpu_st->devs[cpu_st->selector];
64 switch (addr) {
65 case ACPI_CPU_FLAGS_OFFSET_RW: /* pack and return is_* fields */
66 val |= cdev->cpu ? 1 : 0;
67 val |= cdev->is_inserting ? 2 : 0;
68 val |= cdev->is_removing ? 4 : 0;
69 trace_cpuhp_acpi_read_flags(cpu_st->selector, val);
70 break;
71 case ACPI_CPU_CMD_DATA_OFFSET_RW:
72 switch (cpu_st->command) {
73 case CPHP_GET_NEXT_CPU_WITH_EVENT_CMD:
74 val = cpu_st->selector;
75 break;
76 default:
77 break;
79 trace_cpuhp_acpi_read_cmd_data(cpu_st->selector, val);
80 break;
81 default:
82 break;
84 return val;
87 static void cpu_hotplug_wr(void *opaque, hwaddr addr, uint64_t data,
88 unsigned int size)
90 CPUHotplugState *cpu_st = opaque;
91 AcpiCpuStatus *cdev;
92 ACPIOSTInfo *info;
94 assert(cpu_st->dev_count);
96 if (addr) {
97 if (cpu_st->selector >= cpu_st->dev_count) {
98 trace_cpuhp_acpi_invalid_idx_selected(cpu_st->selector);
99 return;
103 switch (addr) {
104 case ACPI_CPU_SELECTOR_OFFSET_WR: /* current CPU selector */
105 cpu_st->selector = data;
106 trace_cpuhp_acpi_write_idx(cpu_st->selector);
107 break;
108 case ACPI_CPU_FLAGS_OFFSET_RW: /* set is_* fields */
109 cdev = &cpu_st->devs[cpu_st->selector];
110 if (data & 2) { /* clear insert event */
111 cdev->is_inserting = false;
112 trace_cpuhp_acpi_clear_inserting_evt(cpu_st->selector);
113 } else if (data & 4) { /* clear remove event */
114 cdev->is_removing = false;
115 trace_cpuhp_acpi_clear_remove_evt(cpu_st->selector);
116 } else if (data & 8) {
117 DeviceState *dev = NULL;
118 HotplugHandler *hotplug_ctrl = NULL;
120 if (!cdev->cpu) {
121 trace_cpuhp_acpi_ejecting_invalid_cpu(cpu_st->selector);
122 break;
125 trace_cpuhp_acpi_ejecting_cpu(cpu_st->selector);
126 dev = DEVICE(cdev->cpu);
127 hotplug_ctrl = qdev_get_hotplug_handler(dev);
128 hotplug_handler_unplug(hotplug_ctrl, dev, NULL);
130 break;
131 case ACPI_CPU_CMD_OFFSET_WR:
132 trace_cpuhp_acpi_write_cmd(cpu_st->selector, data);
133 if (data < CPHP_CMD_MAX) {
134 cpu_st->command = data;
135 if (cpu_st->command == CPHP_GET_NEXT_CPU_WITH_EVENT_CMD) {
136 uint32_t iter = cpu_st->selector;
138 do {
139 cdev = &cpu_st->devs[iter];
140 if (cdev->is_inserting || cdev->is_removing) {
141 cpu_st->selector = iter;
142 trace_cpuhp_acpi_cpu_has_events(cpu_st->selector,
143 cdev->is_inserting, cdev->is_removing);
144 break;
146 iter = iter + 1 < cpu_st->dev_count ? iter + 1 : 0;
147 } while (iter != cpu_st->selector);
150 break;
151 case ACPI_CPU_CMD_DATA_OFFSET_RW:
152 switch (cpu_st->command) {
153 case CPHP_OST_EVENT_CMD: {
154 cdev = &cpu_st->devs[cpu_st->selector];
155 cdev->ost_event = data;
156 trace_cpuhp_acpi_write_ost_ev(cpu_st->selector, cdev->ost_event);
157 break;
159 case CPHP_OST_STATUS_CMD: {
160 cdev = &cpu_st->devs[cpu_st->selector];
161 cdev->ost_status = data;
162 info = acpi_cpu_device_status(cpu_st->selector, cdev);
163 qapi_event_send_acpi_device_ost(info, &error_abort);
164 qapi_free_ACPIOSTInfo(info);
165 trace_cpuhp_acpi_write_ost_status(cpu_st->selector,
166 cdev->ost_status);
167 break;
169 default:
170 break;
172 break;
173 default:
174 break;
178 static const MemoryRegionOps cpu_hotplug_ops = {
179 .read = cpu_hotplug_rd,
180 .write = cpu_hotplug_wr,
181 .endianness = DEVICE_LITTLE_ENDIAN,
182 .valid = {
183 .min_access_size = 1,
184 .max_access_size = 4,
188 void cpu_hotplug_hw_init(MemoryRegion *as, Object *owner,
189 CPUHotplugState *state, hwaddr base_addr)
191 MachineState *machine = MACHINE(qdev_get_machine());
192 MachineClass *mc = MACHINE_GET_CLASS(machine);
193 CPUArchIdList *id_list;
194 int i;
196 assert(mc->possible_cpu_arch_ids);
197 id_list = mc->possible_cpu_arch_ids(machine);
198 state->dev_count = id_list->len;
199 state->devs = g_new0(typeof(*state->devs), state->dev_count);
200 for (i = 0; i < id_list->len; i++) {
201 state->devs[i].cpu = id_list->cpus[i].cpu;
202 state->devs[i].arch_id = id_list->cpus[i].arch_id;
204 g_free(id_list);
205 memory_region_init_io(&state->ctrl_reg, owner, &cpu_hotplug_ops, state,
206 "acpi-mem-hotplug", ACPI_CPU_HOTPLUG_REG_LEN);
207 memory_region_add_subregion(as, base_addr, &state->ctrl_reg);
210 static AcpiCpuStatus *get_cpu_status(CPUHotplugState *cpu_st, DeviceState *dev)
212 CPUClass *k = CPU_GET_CLASS(dev);
213 uint64_t cpu_arch_id = k->get_arch_id(CPU(dev));
214 int i;
216 for (i = 0; i < cpu_st->dev_count; i++) {
217 if (cpu_arch_id == cpu_st->devs[i].arch_id) {
218 return &cpu_st->devs[i];
221 return NULL;
224 void acpi_cpu_plug_cb(HotplugHandler *hotplug_dev,
225 CPUHotplugState *cpu_st, DeviceState *dev, Error **errp)
227 AcpiCpuStatus *cdev;
229 cdev = get_cpu_status(cpu_st, dev);
230 if (!cdev) {
231 return;
234 cdev->cpu = CPU(dev);
235 if (dev->hotplugged) {
236 cdev->is_inserting = true;
237 acpi_send_event(DEVICE(hotplug_dev), ACPI_CPU_HOTPLUG_STATUS);
241 void acpi_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
242 CPUHotplugState *cpu_st,
243 DeviceState *dev, Error **errp)
245 AcpiCpuStatus *cdev;
247 cdev = get_cpu_status(cpu_st, dev);
248 if (!cdev) {
249 return;
252 cdev->is_removing = true;
253 acpi_send_event(DEVICE(hotplug_dev), ACPI_CPU_HOTPLUG_STATUS);
256 void acpi_cpu_unplug_cb(CPUHotplugState *cpu_st,
257 DeviceState *dev, Error **errp)
259 AcpiCpuStatus *cdev;
261 cdev = get_cpu_status(cpu_st, dev);
262 if (!cdev) {
263 return;
266 cdev->cpu = NULL;
269 static const VMStateDescription vmstate_cpuhp_sts = {
270 .name = "CPU hotplug device state",
271 .version_id = 1,
272 .minimum_version_id = 1,
273 .minimum_version_id_old = 1,
274 .fields = (VMStateField[]) {
275 VMSTATE_BOOL(is_inserting, AcpiCpuStatus),
276 VMSTATE_BOOL(is_removing, AcpiCpuStatus),
277 VMSTATE_UINT32(ost_event, AcpiCpuStatus),
278 VMSTATE_UINT32(ost_status, AcpiCpuStatus),
279 VMSTATE_END_OF_LIST()
283 const VMStateDescription vmstate_cpu_hotplug = {
284 .name = "CPU hotplug state",
285 .version_id = 1,
286 .minimum_version_id = 1,
287 .minimum_version_id_old = 1,
288 .fields = (VMStateField[]) {
289 VMSTATE_UINT32(selector, CPUHotplugState),
290 VMSTATE_UINT8(command, CPUHotplugState),
291 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(devs, CPUHotplugState, dev_count,
292 vmstate_cpuhp_sts, AcpiCpuStatus),
293 VMSTATE_END_OF_LIST()
297 #define CPU_NAME_FMT "C%.03X"
298 #define CPUHP_RES_DEVICE "PRES"
299 #define CPU_LOCK "CPLK"
300 #define CPU_STS_METHOD "CSTA"
301 #define CPU_SCAN_METHOD "CSCN"
302 #define CPU_NOTIFY_METHOD "CTFY"
303 #define CPU_EJECT_METHOD "CEJ0"
304 #define CPU_OST_METHOD "COST"
306 #define CPU_ENABLED "CPEN"
307 #define CPU_SELECTOR "CSEL"
308 #define CPU_COMMAND "CCMD"
309 #define CPU_DATA "CDAT"
310 #define CPU_INSERT_EVENT "CINS"
311 #define CPU_REMOVE_EVENT "CRMV"
312 #define CPU_EJECT_EVENT "CEJ0"
314 void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
315 hwaddr io_base,
316 const char *res_root,
317 const char *event_handler_method)
319 Aml *ifctx;
320 Aml *field;
321 Aml *method;
322 Aml *cpu_ctrl_dev;
323 Aml *cpus_dev;
324 Aml *zero = aml_int(0);
325 Aml *one = aml_int(1);
326 Aml *sb_scope = aml_scope("_SB");
327 MachineClass *mc = MACHINE_GET_CLASS(machine);
328 CPUArchIdList *arch_ids = mc->possible_cpu_arch_ids(machine);
329 char *cphp_res_path = g_strdup_printf("%s." CPUHP_RES_DEVICE, res_root);
330 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, NULL);
331 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
332 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
334 cpu_ctrl_dev = aml_device("%s", cphp_res_path);
336 Aml *crs;
338 aml_append(cpu_ctrl_dev,
339 aml_name_decl("_HID", aml_eisaid("PNP0A06")));
340 aml_append(cpu_ctrl_dev,
341 aml_name_decl("_UID", aml_string("CPU Hotplug resources")));
342 aml_append(cpu_ctrl_dev, aml_mutex(CPU_LOCK, 0));
344 crs = aml_resource_template();
345 aml_append(crs, aml_io(AML_DECODE16, io_base, io_base, 1,
346 ACPI_CPU_HOTPLUG_REG_LEN));
347 aml_append(cpu_ctrl_dev, aml_name_decl("_CRS", crs));
349 /* declare CPU hotplug MMIO region with related access fields */
350 aml_append(cpu_ctrl_dev,
351 aml_operation_region("PRST", AML_SYSTEM_IO, aml_int(io_base),
352 ACPI_CPU_HOTPLUG_REG_LEN));
354 field = aml_field("PRST", AML_BYTE_ACC, AML_NOLOCK,
355 AML_WRITE_AS_ZEROS);
356 aml_append(field, aml_reserved_field(ACPI_CPU_FLAGS_OFFSET_RW * 8));
357 /* 1 if enabled, read only */
358 aml_append(field, aml_named_field(CPU_ENABLED, 1));
359 /* (read) 1 if has a insert event. (write) 1 to clear event */
360 aml_append(field, aml_named_field(CPU_INSERT_EVENT, 1));
361 /* (read) 1 if has a remove event. (write) 1 to clear event */
362 aml_append(field, aml_named_field(CPU_REMOVE_EVENT, 1));
363 /* initiates device eject, write only */
364 aml_append(field, aml_named_field(CPU_EJECT_EVENT, 1));
365 aml_append(field, aml_reserved_field(4));
366 aml_append(field, aml_named_field(CPU_COMMAND, 8));
367 aml_append(cpu_ctrl_dev, field);
369 field = aml_field("PRST", AML_DWORD_ACC, AML_NOLOCK, AML_PRESERVE);
370 /* CPU selector, write only */
371 aml_append(field, aml_named_field(CPU_SELECTOR, 32));
372 /* flags + cmd + 2byte align */
373 aml_append(field, aml_reserved_field(4 * 8));
374 aml_append(field, aml_named_field(CPU_DATA, 32));
375 aml_append(cpu_ctrl_dev, field);
377 if (opts.has_legacy_cphp) {
378 method = aml_method("_INI", 0, AML_SERIALIZED);
379 /* switch off legacy CPU hotplug HW and use new one,
380 * on reboot system is in new mode and writing 0
381 * in CPU_SELECTOR selects BSP, which is NOP at
382 * the time _INI is called */
383 aml_append(method, aml_store(zero, aml_name(CPU_SELECTOR)));
384 aml_append(cpu_ctrl_dev, method);
387 aml_append(sb_scope, cpu_ctrl_dev);
389 cpus_dev = aml_device("\\_SB.CPUS");
391 int i;
392 Aml *ctrl_lock = aml_name("%s.%s", cphp_res_path, CPU_LOCK);
393 Aml *cpu_selector = aml_name("%s.%s", cphp_res_path, CPU_SELECTOR);
394 Aml *is_enabled = aml_name("%s.%s", cphp_res_path, CPU_ENABLED);
395 Aml *cpu_cmd = aml_name("%s.%s", cphp_res_path, CPU_COMMAND);
396 Aml *cpu_data = aml_name("%s.%s", cphp_res_path, CPU_DATA);
397 Aml *ins_evt = aml_name("%s.%s", cphp_res_path, CPU_INSERT_EVENT);
398 Aml *rm_evt = aml_name("%s.%s", cphp_res_path, CPU_REMOVE_EVENT);
399 Aml *ej_evt = aml_name("%s.%s", cphp_res_path, CPU_EJECT_EVENT);
401 aml_append(cpus_dev, aml_name_decl("_HID", aml_string("ACPI0010")));
402 aml_append(cpus_dev, aml_name_decl("_CID", aml_eisaid("PNP0A05")));
404 method = aml_method(CPU_NOTIFY_METHOD, 2, AML_NOTSERIALIZED);
405 for (i = 0; i < arch_ids->len; i++) {
406 Aml *cpu = aml_name(CPU_NAME_FMT, i);
407 Aml *uid = aml_arg(0);
408 Aml *event = aml_arg(1);
410 ifctx = aml_if(aml_equal(uid, aml_int(i)));
412 aml_append(ifctx, aml_notify(cpu, event));
414 aml_append(method, ifctx);
416 aml_append(cpus_dev, method);
418 method = aml_method(CPU_STS_METHOD, 1, AML_SERIALIZED);
420 Aml *idx = aml_arg(0);
421 Aml *sta = aml_local(0);
423 aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
424 aml_append(method, aml_store(idx, cpu_selector));
425 aml_append(method, aml_store(zero, sta));
426 ifctx = aml_if(aml_equal(is_enabled, one));
428 aml_append(ifctx, aml_store(aml_int(0xF), sta));
430 aml_append(method, ifctx);
431 aml_append(method, aml_release(ctrl_lock));
432 aml_append(method, aml_return(sta));
434 aml_append(cpus_dev, method);
436 method = aml_method(CPU_EJECT_METHOD, 1, AML_SERIALIZED);
438 Aml *idx = aml_arg(0);
440 aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
441 aml_append(method, aml_store(idx, cpu_selector));
442 aml_append(method, aml_store(one, ej_evt));
443 aml_append(method, aml_release(ctrl_lock));
445 aml_append(cpus_dev, method);
447 method = aml_method(CPU_SCAN_METHOD, 0, AML_SERIALIZED);
449 Aml *else_ctx;
450 Aml *while_ctx;
451 Aml *has_event = aml_local(0);
452 Aml *dev_chk = aml_int(1);
453 Aml *eject_req = aml_int(3);
454 Aml *next_cpu_cmd = aml_int(CPHP_GET_NEXT_CPU_WITH_EVENT_CMD);
456 aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
457 aml_append(method, aml_store(one, has_event));
458 while_ctx = aml_while(aml_equal(has_event, one));
460 /* clear loop exit condition, ins_evt/rm_evt checks
461 * will set it to 1 while next_cpu_cmd returns a CPU
462 * with events */
463 aml_append(while_ctx, aml_store(zero, has_event));
464 aml_append(while_ctx, aml_store(next_cpu_cmd, cpu_cmd));
465 ifctx = aml_if(aml_equal(ins_evt, one));
467 aml_append(ifctx,
468 aml_call2(CPU_NOTIFY_METHOD, cpu_data, dev_chk));
469 aml_append(ifctx, aml_store(one, ins_evt));
470 aml_append(ifctx, aml_store(one, has_event));
472 aml_append(while_ctx, ifctx);
473 else_ctx = aml_else();
474 ifctx = aml_if(aml_equal(rm_evt, one));
476 aml_append(ifctx,
477 aml_call2(CPU_NOTIFY_METHOD, cpu_data, eject_req));
478 aml_append(ifctx, aml_store(one, rm_evt));
479 aml_append(ifctx, aml_store(one, has_event));
481 aml_append(else_ctx, ifctx);
482 aml_append(while_ctx, else_ctx);
484 aml_append(method, while_ctx);
485 aml_append(method, aml_release(ctrl_lock));
487 aml_append(cpus_dev, method);
489 method = aml_method(CPU_OST_METHOD, 4, AML_SERIALIZED);
491 Aml *uid = aml_arg(0);
492 Aml *ev_cmd = aml_int(CPHP_OST_EVENT_CMD);
493 Aml *st_cmd = aml_int(CPHP_OST_STATUS_CMD);
495 aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
496 aml_append(method, aml_store(uid, cpu_selector));
497 aml_append(method, aml_store(ev_cmd, cpu_cmd));
498 aml_append(method, aml_store(aml_arg(1), cpu_data));
499 aml_append(method, aml_store(st_cmd, cpu_cmd));
500 aml_append(method, aml_store(aml_arg(2), cpu_data));
501 aml_append(method, aml_release(ctrl_lock));
503 aml_append(cpus_dev, method);
505 /* build Processor object for each processor */
506 for (i = 0; i < arch_ids->len; i++) {
507 int j;
508 Aml *dev;
509 Aml *uid = aml_int(i);
510 GArray *madt_buf = g_array_new(0, 1, 1);
511 int arch_id = arch_ids->cpus[i].arch_id;
513 if (opts.apci_1_compatible && arch_id < 255) {
514 dev = aml_processor(i, 0, 0, CPU_NAME_FMT, i);
515 } else {
516 dev = aml_device(CPU_NAME_FMT, i);
517 aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0007")));
518 aml_append(dev, aml_name_decl("_UID", uid));
521 method = aml_method("_STA", 0, AML_SERIALIZED);
522 aml_append(method, aml_return(aml_call1(CPU_STS_METHOD, uid)));
523 aml_append(dev, method);
525 /* build _MAT object */
526 assert(adevc && adevc->madt_cpu);
527 adevc->madt_cpu(adev, i, arch_ids, madt_buf);
528 switch (madt_buf->data[0]) {
529 case ACPI_APIC_PROCESSOR: {
530 AcpiMadtProcessorApic *apic = (void *)madt_buf->data;
531 apic->flags = cpu_to_le32(1);
532 break;
534 default:
535 assert(0);
537 aml_append(dev, aml_name_decl("_MAT",
538 aml_buffer(madt_buf->len, (uint8_t *)madt_buf->data)));
539 g_array_free(madt_buf, true);
541 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
542 aml_append(method, aml_call1(CPU_EJECT_METHOD, uid));
543 aml_append(dev, method);
545 method = aml_method("_OST", 3, AML_SERIALIZED);
546 aml_append(method,
547 aml_call4(CPU_OST_METHOD, uid, aml_arg(0),
548 aml_arg(1), aml_arg(2))
550 aml_append(dev, method);
552 /* Linux guests discard SRAT info for non-present CPUs
553 * as a result _PXM is required for all CPUs which might
554 * be hot-plugged. For simplicity, add it for all CPUs.
556 j = numa_get_node_for_cpu(i);
557 if (j < nb_numa_nodes) {
558 aml_append(dev, aml_name_decl("_PXM", aml_int(j)));
561 aml_append(cpus_dev, dev);
564 aml_append(sb_scope, cpus_dev);
565 aml_append(table, sb_scope);
567 method = aml_method(event_handler_method, 0, AML_NOTSERIALIZED);
568 aml_append(method, aml_call0("\\_SB.CPUS." CPU_SCAN_METHOD));
569 aml_append(table, method);
571 g_free(cphp_res_path);
572 g_free(arch_ids);