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 "exec/address-spaces.h"
15 #include "hw/acpi/acpi.h"
16 #include "hw/acpi/generic_event_device.h"
18 #include "hw/mem/pc-dimm.h"
19 #include "hw/mem/nvdimm.h"
20 #include "hw/qdev-properties.h"
21 #include "migration/vmstate.h"
22 #include "qemu/error-report.h"
23 #include "sysemu/runstate.h"
25 static const uint32_t ged_supported_events
[] = {
26 ACPI_GED_MEM_HOTPLUG_EVT
,
27 ACPI_GED_PWR_DOWN_EVT
,
28 ACPI_GED_NVDIMM_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_PWR_DOWN_EVT
:
113 aml_notify(aml_name(ACPI_POWER_BUTTON_DEVICE
),
116 case ACPI_GED_NVDIMM_HOTPLUG_EVT
:
118 aml_notify(aml_name("\\_SB.NVDR"),
123 * Please make sure all the events in ged_supported_events[]
126 g_assert_not_reached();
129 aml_append(evt
, if_ctx
);
134 error_report("Unsupported events specified");
139 /* Append _EVT method */
140 aml_append(dev
, evt
);
142 aml_append(table
, dev
);
145 /* Memory read by the GED _EVT AML dynamic method */
146 static uint64_t ged_evt_read(void *opaque
, hwaddr addr
, unsigned size
)
149 GEDState
*ged_st
= opaque
;
152 case ACPI_GED_EVT_SEL_OFFSET
:
153 /* Read the selector value and reset it */
164 /* Nothing is expected to be written to the GED memory region */
165 static void ged_evt_write(void *opaque
, hwaddr addr
, uint64_t data
,
170 static const MemoryRegionOps ged_evt_ops
= {
171 .read
= ged_evt_read
,
172 .write
= ged_evt_write
,
173 .endianness
= DEVICE_LITTLE_ENDIAN
,
175 .min_access_size
= 4,
176 .max_access_size
= 4,
180 static uint64_t ged_regs_read(void *opaque
, hwaddr addr
, unsigned size
)
185 static void ged_regs_write(void *opaque
, hwaddr addr
, uint64_t data
,
192 case ACPI_GED_REG_SLEEP_CTL
:
193 slp_typ
= (data
>> 2) & 0x07;
194 slp_en
= (data
>> 5) & 0x01;
195 if (slp_en
&& slp_typ
== 5) {
196 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN
);
199 case ACPI_GED_REG_SLEEP_STS
:
201 case ACPI_GED_REG_RESET
:
202 if (data
== ACPI_GED_RESET_VALUE
) {
203 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN
);
209 static const MemoryRegionOps ged_regs_ops
= {
210 .read
= ged_regs_read
,
211 .write
= ged_regs_write
,
212 .endianness
= DEVICE_LITTLE_ENDIAN
,
214 .min_access_size
= 1,
215 .max_access_size
= 1,
219 static void acpi_ged_device_plug_cb(HotplugHandler
*hotplug_dev
,
220 DeviceState
*dev
, Error
**errp
)
222 AcpiGedState
*s
= ACPI_GED(hotplug_dev
);
224 if (object_dynamic_cast(OBJECT(dev
), TYPE_PC_DIMM
)) {
225 if (object_dynamic_cast(OBJECT(dev
), TYPE_NVDIMM
)) {
226 nvdimm_acpi_plug_cb(hotplug_dev
, dev
);
228 acpi_memory_plug_cb(hotplug_dev
, &s
->memhp_state
, dev
, errp
);
231 error_setg(errp
, "virt: device plug request for unsupported device"
232 " type: %s", object_get_typename(OBJECT(dev
)));
236 static void acpi_ged_unplug_request_cb(HotplugHandler
*hotplug_dev
,
237 DeviceState
*dev
, Error
**errp
)
239 AcpiGedState
*s
= ACPI_GED(hotplug_dev
);
241 if ((object_dynamic_cast(OBJECT(dev
), TYPE_PC_DIMM
) &&
242 !(object_dynamic_cast(OBJECT(dev
), TYPE_NVDIMM
)))) {
243 acpi_memory_unplug_request_cb(hotplug_dev
, &s
->memhp_state
, dev
, errp
);
245 error_setg(errp
, "acpi: device unplug request for unsupported device"
246 " type: %s", object_get_typename(OBJECT(dev
)));
250 static void acpi_ged_unplug_cb(HotplugHandler
*hotplug_dev
,
251 DeviceState
*dev
, Error
**errp
)
253 AcpiGedState
*s
= ACPI_GED(hotplug_dev
);
255 if (object_dynamic_cast(OBJECT(dev
), TYPE_PC_DIMM
)) {
256 acpi_memory_unplug_cb(&s
->memhp_state
, dev
, errp
);
258 error_setg(errp
, "acpi: device unplug for unsupported device"
259 " type: %s", object_get_typename(OBJECT(dev
)));
263 static void acpi_ged_send_event(AcpiDeviceIf
*adev
, AcpiEventStatusBits ev
)
265 AcpiGedState
*s
= ACPI_GED(adev
);
266 GEDState
*ged_st
= &s
->ged_state
;
269 if (ev
& ACPI_MEMORY_HOTPLUG_STATUS
) {
270 sel
= ACPI_GED_MEM_HOTPLUG_EVT
;
271 } else if (ev
& ACPI_POWER_DOWN_STATUS
) {
272 sel
= ACPI_GED_PWR_DOWN_EVT
;
273 } else if (ev
& ACPI_NVDIMM_HOTPLUG_STATUS
) {
274 sel
= ACPI_GED_NVDIMM_HOTPLUG_EVT
;
276 /* Unknown event. Return without generating interrupt. */
277 warn_report("GED: Unsupported event %d. No irq injected", ev
);
282 * Set the GED selector field to communicate the event type.
283 * This will be read by GED aml code to select the appropriate
288 /* Trigger the event by sending an interrupt to the guest. */
289 qemu_irq_pulse(s
->irq
);
292 static Property acpi_ged_properties
[] = {
293 DEFINE_PROP_UINT32("ged-event", AcpiGedState
, ged_event_bitmap
, 0),
294 DEFINE_PROP_END_OF_LIST(),
297 static const VMStateDescription vmstate_memhp_state
= {
298 .name
= "acpi-ged/memhp",
300 .minimum_version_id
= 1,
301 .fields
= (VMStateField
[]) {
302 VMSTATE_MEMORY_HOTPLUG(memhp_state
, AcpiGedState
),
303 VMSTATE_END_OF_LIST()
307 static const VMStateDescription vmstate_ged_state
= {
308 .name
= "acpi-ged-state",
310 .minimum_version_id
= 1,
311 .fields
= (VMStateField
[]) {
312 VMSTATE_UINT32(sel
, GEDState
),
313 VMSTATE_END_OF_LIST()
317 static bool ghes_needed(void *opaque
)
319 AcpiGedState
*s
= opaque
;
320 return s
->ghes_state
.ghes_addr_le
;
323 static const VMStateDescription vmstate_ghes_state
= {
324 .name
= "acpi-ged/ghes",
326 .minimum_version_id
= 1,
327 .needed
= ghes_needed
,
328 .fields
= (VMStateField
[]) {
329 VMSTATE_STRUCT(ghes_state
, AcpiGedState
, 1,
330 vmstate_ghes_state
, AcpiGhesState
),
331 VMSTATE_END_OF_LIST()
335 static const VMStateDescription vmstate_acpi_ged
= {
338 .minimum_version_id
= 1,
339 .fields
= (VMStateField
[]) {
340 VMSTATE_STRUCT(ged_state
, AcpiGedState
, 1, vmstate_ged_state
, GEDState
),
341 VMSTATE_END_OF_LIST(),
343 .subsections
= (const VMStateDescription
* []) {
344 &vmstate_memhp_state
,
350 static void acpi_ged_initfn(Object
*obj
)
352 DeviceState
*dev
= DEVICE(obj
);
353 AcpiGedState
*s
= ACPI_GED(dev
);
354 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
355 GEDState
*ged_st
= &s
->ged_state
;
357 memory_region_init_io(&ged_st
->evt
, obj
, &ged_evt_ops
, ged_st
,
358 TYPE_ACPI_GED
, ACPI_GED_EVT_SEL_LEN
);
359 sysbus_init_mmio(sbd
, &ged_st
->evt
);
361 sysbus_init_irq(sbd
, &s
->irq
);
363 s
->memhp_state
.is_enabled
= true;
365 * GED handles memory hotplug event and acpi-mem-hotplug
366 * memory region gets initialized here. Create an exclusive
367 * container for memory hotplug IO and expose it as GED sysbus
368 * MMIO so that boards can map it separately.
370 memory_region_init(&s
->container_memhp
, OBJECT(dev
), "memhp container",
371 MEMORY_HOTPLUG_IO_LEN
);
372 sysbus_init_mmio(sbd
, &s
->container_memhp
);
373 acpi_memory_hotplug_init(&s
->container_memhp
, OBJECT(dev
),
376 memory_region_init_io(&ged_st
->regs
, obj
, &ged_regs_ops
, ged_st
,
377 TYPE_ACPI_GED
"-regs", ACPI_GED_REG_COUNT
);
378 sysbus_init_mmio(sbd
, &ged_st
->regs
);
381 static void acpi_ged_class_init(ObjectClass
*class, void *data
)
383 DeviceClass
*dc
= DEVICE_CLASS(class);
384 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(class);
385 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_CLASS(class);
387 dc
->desc
= "ACPI Generic Event Device";
388 device_class_set_props(dc
, acpi_ged_properties
);
389 dc
->vmsd
= &vmstate_acpi_ged
;
391 hc
->plug
= acpi_ged_device_plug_cb
;
392 hc
->unplug_request
= acpi_ged_unplug_request_cb
;
393 hc
->unplug
= acpi_ged_unplug_cb
;
395 adevc
->send_event
= acpi_ged_send_event
;
398 static const TypeInfo acpi_ged_info
= {
399 .name
= TYPE_ACPI_GED
,
400 .parent
= TYPE_SYS_BUS_DEVICE
,
401 .instance_size
= sizeof(AcpiGedState
),
402 .instance_init
= acpi_ged_initfn
,
403 .class_init
= acpi_ged_class_init
,
404 .interfaces
= (InterfaceInfo
[]) {
405 { TYPE_HOTPLUG_HANDLER
},
406 { TYPE_ACPI_DEVICE_IF
},
411 static void acpi_ged_register_types(void)
413 type_register_static(&acpi_ged_info
);
416 type_init(acpi_ged_register_types
)