3 * Copyright (c) 2018 Intel Corporation
4 * Copyright (c) 2019 Huawei Technologies R & D (UK) Ltd
5 * Written by Samuel Ortiz, Shameer Kolothum
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2 or later, as published by the Free Software Foundation.
12 #include "qemu/osdep.h"
13 #include "qapi/error.h"
14 #include "hw/acpi/acpi.h"
15 #include "hw/acpi/generic_event_device.h"
17 #include "hw/mem/pc-dimm.h"
18 #include "hw/mem/nvdimm.h"
19 #include "hw/qdev-properties.h"
20 #include "migration/vmstate.h"
21 #include "qemu/error-report.h"
22 #include "sysemu/runstate.h"
24 static const uint32_t ged_supported_events
[] = {
25 ACPI_GED_MEM_HOTPLUG_EVT
,
26 ACPI_GED_PWR_DOWN_EVT
,
27 ACPI_GED_NVDIMM_HOTPLUG_EVT
,
28 ACPI_GED_CPU_HOTPLUG_EVT
,
32 * The ACPI Generic Event Device (GED) is a hardware-reduced specific
33 * device[ACPI v6.1 Section 5.6.9] that handles all platform events,
34 * including the hotplug ones. Platforms need to specify their own
35 * GED Event bitmap to describe what kind of events they want to support
36 * through GED. This routine uses a single interrupt for the GED device,
37 * relying on IO memory region to communicate the type of device
38 * affected by the interrupt. This way, we can support up to 32 events
39 * with a unique interrupt.
41 void build_ged_aml(Aml
*table
, const char *name
, HotplugHandler
*hotplug_dev
,
42 uint32_t ged_irq
, AmlRegionSpace rs
, hwaddr ged_base
)
44 AcpiGedState
*s
= ACPI_GED(hotplug_dev
);
45 Aml
*crs
= aml_resource_template();
47 Aml
*dev
= aml_device("%s", name
);
48 Aml
*evt_sel
= aml_local(0);
49 Aml
*esel
= aml_name(AML_GED_EVT_SEL
);
52 aml_append(crs
, aml_interrupt(AML_CONSUMER
, AML_EDGE
, AML_ACTIVE_HIGH
,
53 AML_EXCLUSIVE
, &ged_irq
, 1));
55 aml_append(dev
, aml_name_decl("_HID", aml_string("ACPI0013")));
56 aml_append(dev
, aml_name_decl("_UID", aml_string(GED_DEVICE
)));
57 aml_append(dev
, aml_name_decl("_CRS", crs
));
59 /* Append IO region */
60 aml_append(dev
, aml_operation_region(AML_GED_EVT_REG
, rs
,
61 aml_int(ged_base
+ ACPI_GED_EVT_SEL_OFFSET
),
62 ACPI_GED_EVT_SEL_LEN
));
63 field
= aml_field(AML_GED_EVT_REG
, AML_DWORD_ACC
, AML_NOLOCK
,
65 aml_append(field
, aml_named_field(AML_GED_EVT_SEL
,
66 ACPI_GED_EVT_SEL_LEN
* BITS_PER_BYTE
));
67 aml_append(dev
, field
);
70 * For each GED event we:
71 * - Add a conditional block for each event, inside a loop.
72 * - Call a method for each supported GED event type.
74 * The resulting ASL code looks like:
77 * If ((Local0 & One) == One)
82 * If ((Local0 & 0x2) == 0x2)
88 evt
= aml_method("_EVT", 1, AML_SERIALIZED
);
92 uint32_t ged_events
= ctpop32(s
->ged_event_bitmap
);
95 aml_append(evt
, aml_store(esel
, evt_sel
));
97 for (i
= 0; i
< ARRAY_SIZE(ged_supported_events
) && ged_events
; i
++) {
98 uint32_t event
= s
->ged_event_bitmap
& ged_supported_events
[i
];
104 if_ctx
= aml_if(aml_equal(aml_and(evt_sel
, aml_int(event
), NULL
),
107 case ACPI_GED_MEM_HOTPLUG_EVT
:
108 aml_append(if_ctx
, aml_call0(MEMORY_DEVICES_CONTAINER
"."
109 MEMORY_SLOT_SCAN_METHOD
));
111 case ACPI_GED_CPU_HOTPLUG_EVT
:
112 aml_append(if_ctx
, aml_call0(AML_GED_EVT_CPU_SCAN_METHOD
));
114 case ACPI_GED_PWR_DOWN_EVT
:
116 aml_notify(aml_name(ACPI_POWER_BUTTON_DEVICE
),
119 case ACPI_GED_NVDIMM_HOTPLUG_EVT
:
121 aml_notify(aml_name("\\_SB.NVDR"),
126 * Please make sure all the events in ged_supported_events[]
129 g_assert_not_reached();
132 aml_append(evt
, if_ctx
);
137 error_report("Unsupported events specified");
142 /* Append _EVT method */
143 aml_append(dev
, evt
);
145 aml_append(table
, dev
);
148 void acpi_dsdt_add_power_button(Aml
*scope
)
150 Aml
*dev
= aml_device(ACPI_POWER_BUTTON_DEVICE
);
151 aml_append(dev
, aml_name_decl("_HID", aml_string("PNP0C0C")));
152 aml_append(dev
, aml_name_decl("_UID", aml_int(0)));
153 aml_append(scope
, dev
);
156 /* Memory read by the GED _EVT AML dynamic method */
157 static uint64_t ged_evt_read(void *opaque
, hwaddr addr
, unsigned size
)
160 GEDState
*ged_st
= opaque
;
163 case ACPI_GED_EVT_SEL_OFFSET
:
164 /* Read the selector value and reset it */
175 /* Nothing is expected to be written to the GED memory region */
176 static void ged_evt_write(void *opaque
, hwaddr addr
, uint64_t data
,
181 static const MemoryRegionOps ged_evt_ops
= {
182 .read
= ged_evt_read
,
183 .write
= ged_evt_write
,
184 .endianness
= DEVICE_LITTLE_ENDIAN
,
186 .min_access_size
= 4,
187 .max_access_size
= 4,
191 static uint64_t ged_regs_read(void *opaque
, hwaddr addr
, unsigned size
)
196 static void ged_regs_write(void *opaque
, hwaddr addr
, uint64_t data
,
203 case ACPI_GED_REG_SLEEP_CTL
:
204 slp_typ
= (data
>> ACPI_GED_SLP_TYP_POS
) & ACPI_GED_SLP_TYP_MASK
;
205 slp_en
= !!(data
& ACPI_GED_SLP_EN
);
206 if (slp_en
&& slp_typ
== ACPI_GED_SLP_TYP_S5
) {
207 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN
);
210 case ACPI_GED_REG_SLEEP_STS
:
212 case ACPI_GED_REG_RESET
:
213 if (data
== ACPI_GED_RESET_VALUE
) {
214 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
220 static const MemoryRegionOps ged_regs_ops
= {
221 .read
= ged_regs_read
,
222 .write
= ged_regs_write
,
223 .endianness
= DEVICE_LITTLE_ENDIAN
,
225 .min_access_size
= 1,
226 .max_access_size
= 1,
230 static void acpi_ged_device_plug_cb(HotplugHandler
*hotplug_dev
,
231 DeviceState
*dev
, Error
**errp
)
233 AcpiGedState
*s
= ACPI_GED(hotplug_dev
);
235 if (object_dynamic_cast(OBJECT(dev
), TYPE_PC_DIMM
)) {
236 if (object_dynamic_cast(OBJECT(dev
), TYPE_NVDIMM
)) {
237 nvdimm_acpi_plug_cb(hotplug_dev
, dev
);
239 acpi_memory_plug_cb(hotplug_dev
, &s
->memhp_state
, dev
, errp
);
241 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_CPU
)) {
242 acpi_cpu_plug_cb(hotplug_dev
, &s
->cpuhp_state
, dev
, errp
);
244 error_setg(errp
, "virt: device plug request for unsupported device"
245 " type: %s", object_get_typename(OBJECT(dev
)));
249 static void acpi_ged_unplug_request_cb(HotplugHandler
*hotplug_dev
,
250 DeviceState
*dev
, Error
**errp
)
252 AcpiGedState
*s
= ACPI_GED(hotplug_dev
);
254 if ((object_dynamic_cast(OBJECT(dev
), TYPE_PC_DIMM
) &&
255 !(object_dynamic_cast(OBJECT(dev
), TYPE_NVDIMM
)))) {
256 acpi_memory_unplug_request_cb(hotplug_dev
, &s
->memhp_state
, dev
, errp
);
257 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_CPU
)) {
258 acpi_cpu_unplug_request_cb(hotplug_dev
, &s
->cpuhp_state
, dev
, errp
);
260 error_setg(errp
, "acpi: device unplug request for unsupported device"
261 " type: %s", object_get_typename(OBJECT(dev
)));
265 static void acpi_ged_unplug_cb(HotplugHandler
*hotplug_dev
,
266 DeviceState
*dev
, Error
**errp
)
268 AcpiGedState
*s
= ACPI_GED(hotplug_dev
);
270 if (object_dynamic_cast(OBJECT(dev
), TYPE_PC_DIMM
)) {
271 acpi_memory_unplug_cb(&s
->memhp_state
, dev
, errp
);
272 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_CPU
)) {
273 acpi_cpu_unplug_cb(&s
->cpuhp_state
, dev
, errp
);
275 error_setg(errp
, "acpi: device unplug for unsupported device"
276 " type: %s", object_get_typename(OBJECT(dev
)));
280 static void acpi_ged_ospm_status(AcpiDeviceIf
*adev
, ACPIOSTInfoList
***list
)
282 AcpiGedState
*s
= ACPI_GED(adev
);
284 acpi_memory_ospm_status(&s
->memhp_state
, list
);
285 acpi_cpu_ospm_status(&s
->cpuhp_state
, list
);
288 static void acpi_ged_send_event(AcpiDeviceIf
*adev
, AcpiEventStatusBits ev
)
290 AcpiGedState
*s
= ACPI_GED(adev
);
291 GEDState
*ged_st
= &s
->ged_state
;
294 if (ev
& ACPI_MEMORY_HOTPLUG_STATUS
) {
295 sel
= ACPI_GED_MEM_HOTPLUG_EVT
;
296 } else if (ev
& ACPI_POWER_DOWN_STATUS
) {
297 sel
= ACPI_GED_PWR_DOWN_EVT
;
298 } else if (ev
& ACPI_NVDIMM_HOTPLUG_STATUS
) {
299 sel
= ACPI_GED_NVDIMM_HOTPLUG_EVT
;
300 } else if (ev
& ACPI_CPU_HOTPLUG_STATUS
) {
301 sel
= ACPI_GED_CPU_HOTPLUG_EVT
;
303 /* Unknown event. Return without generating interrupt. */
304 warn_report("GED: Unsupported event %d. No irq injected", ev
);
309 * Set the GED selector field to communicate the event type.
310 * This will be read by GED aml code to select the appropriate
315 /* Trigger the event by sending an interrupt to the guest. */
316 qemu_irq_pulse(s
->irq
);
319 static Property acpi_ged_properties
[] = {
320 DEFINE_PROP_UINT32("ged-event", AcpiGedState
, ged_event_bitmap
, 0),
321 DEFINE_PROP_END_OF_LIST(),
324 static const VMStateDescription vmstate_memhp_state
= {
325 .name
= "acpi-ged/memhp",
327 .minimum_version_id
= 1,
328 .fields
= (const VMStateField
[]) {
329 VMSTATE_MEMORY_HOTPLUG(memhp_state
, AcpiGedState
),
330 VMSTATE_END_OF_LIST()
334 static const VMStateDescription vmstate_ged_state
= {
335 .name
= "acpi-ged-state",
337 .minimum_version_id
= 1,
338 .fields
= (const VMStateField
[]) {
339 VMSTATE_UINT32(sel
, GEDState
),
340 VMSTATE_END_OF_LIST()
344 static const VMStateDescription vmstate_ghes
= {
347 .minimum_version_id
= 1,
348 .fields
= (const VMStateField
[]) {
349 VMSTATE_UINT64(ghes_addr_le
, AcpiGhesState
),
350 VMSTATE_END_OF_LIST()
354 static bool ghes_needed(void *opaque
)
356 AcpiGedState
*s
= opaque
;
357 return s
->ghes_state
.ghes_addr_le
;
360 static const VMStateDescription vmstate_ghes_state
= {
361 .name
= "acpi-ged/ghes",
363 .minimum_version_id
= 1,
364 .needed
= ghes_needed
,
365 .fields
= (const VMStateField
[]) {
366 VMSTATE_STRUCT(ghes_state
, AcpiGedState
, 1,
367 vmstate_ghes
, AcpiGhesState
),
368 VMSTATE_END_OF_LIST()
372 static const VMStateDescription vmstate_acpi_ged
= {
375 .minimum_version_id
= 1,
376 .fields
= (const VMStateField
[]) {
377 VMSTATE_STRUCT(ged_state
, AcpiGedState
, 1, vmstate_ged_state
, GEDState
),
378 VMSTATE_END_OF_LIST(),
380 .subsections
= (const VMStateDescription
* const []) {
381 &vmstate_memhp_state
,
387 static void acpi_ged_realize(DeviceState
*dev
, Error
**errp
)
389 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
390 AcpiGedState
*s
= ACPI_GED(dev
);
394 ged_events
= ctpop32(s
->ged_event_bitmap
);
396 for (i
= 0; i
< ARRAY_SIZE(ged_supported_events
) && ged_events
; i
++) {
397 uint32_t event
= s
->ged_event_bitmap
& ged_supported_events
[i
];
404 case ACPI_GED_CPU_HOTPLUG_EVT
:
405 /* initialize CPU Hotplug related regions */
406 memory_region_init(&s
->container_cpuhp
, OBJECT(dev
),
408 ACPI_CPU_HOTPLUG_REG_LEN
);
409 sysbus_init_mmio(sbd
, &s
->container_cpuhp
);
410 cpu_hotplug_hw_init(&s
->container_cpuhp
, OBJECT(dev
),
418 error_report("Unsupported events specified");
423 static void acpi_ged_initfn(Object
*obj
)
425 DeviceState
*dev
= DEVICE(obj
);
426 AcpiGedState
*s
= ACPI_GED(dev
);
427 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
428 GEDState
*ged_st
= &s
->ged_state
;
430 memory_region_init_io(&ged_st
->evt
, obj
, &ged_evt_ops
, ged_st
,
431 TYPE_ACPI_GED
, ACPI_GED_EVT_SEL_LEN
);
432 sysbus_init_mmio(sbd
, &ged_st
->evt
);
434 sysbus_init_irq(sbd
, &s
->irq
);
436 s
->memhp_state
.is_enabled
= true;
438 * GED handles memory hotplug event and acpi-mem-hotplug
439 * memory region gets initialized here. Create an exclusive
440 * container for memory hotplug IO and expose it as GED sysbus
441 * MMIO so that boards can map it separately.
443 memory_region_init(&s
->container_memhp
, OBJECT(dev
), "memhp container",
444 MEMORY_HOTPLUG_IO_LEN
);
445 sysbus_init_mmio(sbd
, &s
->container_memhp
);
446 acpi_memory_hotplug_init(&s
->container_memhp
, OBJECT(dev
),
449 memory_region_init_io(&ged_st
->regs
, obj
, &ged_regs_ops
, ged_st
,
450 TYPE_ACPI_GED
"-regs", ACPI_GED_REG_COUNT
);
451 sysbus_init_mmio(sbd
, &ged_st
->regs
);
454 static void acpi_ged_class_init(ObjectClass
*class, void *data
)
456 DeviceClass
*dc
= DEVICE_CLASS(class);
457 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(class);
458 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_CLASS(class);
460 dc
->desc
= "ACPI Generic Event Device";
461 device_class_set_props(dc
, acpi_ged_properties
);
462 dc
->vmsd
= &vmstate_acpi_ged
;
463 dc
->realize
= acpi_ged_realize
;
465 hc
->plug
= acpi_ged_device_plug_cb
;
466 hc
->unplug_request
= acpi_ged_unplug_request_cb
;
467 hc
->unplug
= acpi_ged_unplug_cb
;
469 adevc
->ospm_status
= acpi_ged_ospm_status
;
470 adevc
->send_event
= acpi_ged_send_event
;
473 static const TypeInfo acpi_ged_info
= {
474 .name
= TYPE_ACPI_GED
,
475 .parent
= TYPE_SYS_BUS_DEVICE
,
476 .instance_size
= sizeof(AcpiGedState
),
477 .instance_init
= acpi_ged_initfn
,
478 .class_init
= acpi_ged_class_init
,
479 .interfaces
= (InterfaceInfo
[]) {
480 { TYPE_HOTPLUG_HANDLER
},
481 { TYPE_ACPI_DEVICE_IF
},
486 static void acpi_ged_register_types(void)
488 type_register_static(&acpi_ged_info
);
491 type_init(acpi_ged_register_types
)