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"
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
,
31 * The ACPI Generic Event Device (GED) is a hardware-reduced specific
32 * device[ACPI v6.1 Section 5.6.9] that handles all platform events,
33 * including the hotplug ones. Platforms need to specify their own
34 * GED Event bitmap to describe what kind of events they want to support
35 * through GED. This routine uses a single interrupt for the GED device,
36 * relying on IO memory region to communicate the type of device
37 * affected by the interrupt. This way, we can support up to 32 events
38 * with a unique interrupt.
40 void build_ged_aml(Aml
*table
, const char *name
, HotplugHandler
*hotplug_dev
,
41 uint32_t ged_irq
, AmlRegionSpace rs
, hwaddr ged_base
)
43 AcpiGedState
*s
= ACPI_GED(hotplug_dev
);
44 Aml
*crs
= aml_resource_template();
46 Aml
*dev
= aml_device("%s", name
);
47 Aml
*evt_sel
= aml_local(0);
48 Aml
*esel
= aml_name(AML_GED_EVT_SEL
);
51 aml_append(crs
, aml_interrupt(AML_CONSUMER
, AML_EDGE
, AML_ACTIVE_HIGH
,
52 AML_EXCLUSIVE
, &ged_irq
, 1));
54 aml_append(dev
, aml_name_decl("_HID", aml_string("ACPI0013")));
55 aml_append(dev
, aml_name_decl("_UID", aml_string(GED_DEVICE
)));
56 aml_append(dev
, aml_name_decl("_CRS", crs
));
58 /* Append IO region */
59 aml_append(dev
, aml_operation_region(AML_GED_EVT_REG
, rs
,
60 aml_int(ged_base
+ ACPI_GED_EVT_SEL_OFFSET
),
61 ACPI_GED_EVT_SEL_LEN
));
62 field
= aml_field(AML_GED_EVT_REG
, AML_DWORD_ACC
, AML_NOLOCK
,
64 aml_append(field
, aml_named_field(AML_GED_EVT_SEL
,
65 ACPI_GED_EVT_SEL_LEN
* BITS_PER_BYTE
));
66 aml_append(dev
, field
);
69 * For each GED event we:
70 * - Add a conditional block for each event, inside a loop.
71 * - Call a method for each supported GED event type.
73 * The resulting ASL code looks like:
76 * If ((Local0 & One) == One)
81 * If ((Local0 & 0x2) == 0x2)
87 evt
= aml_method("_EVT", 1, AML_SERIALIZED
);
91 uint32_t ged_events
= ctpop32(s
->ged_event_bitmap
);
94 aml_append(evt
, aml_store(esel
, evt_sel
));
96 for (i
= 0; i
< ARRAY_SIZE(ged_supported_events
) && ged_events
; i
++) {
97 uint32_t event
= s
->ged_event_bitmap
& ged_supported_events
[i
];
103 if_ctx
= aml_if(aml_equal(aml_and(evt_sel
, aml_int(event
), NULL
),
106 case ACPI_GED_MEM_HOTPLUG_EVT
:
107 aml_append(if_ctx
, aml_call0(MEMORY_DEVICES_CONTAINER
"."
108 MEMORY_SLOT_SCAN_METHOD
));
110 case ACPI_GED_PWR_DOWN_EVT
:
112 aml_notify(aml_name(ACPI_POWER_BUTTON_DEVICE
),
115 case ACPI_GED_NVDIMM_HOTPLUG_EVT
:
117 aml_notify(aml_name("\\_SB.NVDR"),
122 * Please make sure all the events in ged_supported_events[]
125 g_assert_not_reached();
128 aml_append(evt
, if_ctx
);
133 error_report("Unsupported events specified");
138 /* Append _EVT method */
139 aml_append(dev
, evt
);
141 aml_append(table
, dev
);
144 /* Memory read by the GED _EVT AML dynamic method */
145 static uint64_t ged_evt_read(void *opaque
, hwaddr addr
, unsigned size
)
148 GEDState
*ged_st
= opaque
;
151 case ACPI_GED_EVT_SEL_OFFSET
:
152 /* Read the selector value and reset it */
163 /* Nothing is expected to be written to the GED memory region */
164 static void ged_evt_write(void *opaque
, hwaddr addr
, uint64_t data
,
169 static const MemoryRegionOps ged_evt_ops
= {
170 .read
= ged_evt_read
,
171 .write
= ged_evt_write
,
172 .endianness
= DEVICE_LITTLE_ENDIAN
,
174 .min_access_size
= 4,
175 .max_access_size
= 4,
179 static void acpi_ged_device_plug_cb(HotplugHandler
*hotplug_dev
,
180 DeviceState
*dev
, Error
**errp
)
182 AcpiGedState
*s
= ACPI_GED(hotplug_dev
);
184 if (object_dynamic_cast(OBJECT(dev
), TYPE_PC_DIMM
)) {
185 if (object_dynamic_cast(OBJECT(dev
), TYPE_NVDIMM
)) {
186 nvdimm_acpi_plug_cb(hotplug_dev
, dev
);
188 acpi_memory_plug_cb(hotplug_dev
, &s
->memhp_state
, dev
, errp
);
191 error_setg(errp
, "virt: device plug request for unsupported device"
192 " type: %s", object_get_typename(OBJECT(dev
)));
196 static void acpi_ged_unplug_request_cb(HotplugHandler
*hotplug_dev
,
197 DeviceState
*dev
, Error
**errp
)
199 AcpiGedState
*s
= ACPI_GED(hotplug_dev
);
201 if ((object_dynamic_cast(OBJECT(dev
), TYPE_PC_DIMM
) &&
202 !(object_dynamic_cast(OBJECT(dev
), TYPE_NVDIMM
)))) {
203 acpi_memory_unplug_request_cb(hotplug_dev
, &s
->memhp_state
, dev
, errp
);
205 error_setg(errp
, "acpi: device unplug request for unsupported device"
206 " type: %s", object_get_typename(OBJECT(dev
)));
210 static void acpi_ged_unplug_cb(HotplugHandler
*hotplug_dev
,
211 DeviceState
*dev
, Error
**errp
)
213 AcpiGedState
*s
= ACPI_GED(hotplug_dev
);
215 if (object_dynamic_cast(OBJECT(dev
), TYPE_PC_DIMM
)) {
216 acpi_memory_unplug_cb(&s
->memhp_state
, dev
, errp
);
218 error_setg(errp
, "acpi: device unplug for unsupported device"
219 " type: %s", object_get_typename(OBJECT(dev
)));
223 static void acpi_ged_send_event(AcpiDeviceIf
*adev
, AcpiEventStatusBits ev
)
225 AcpiGedState
*s
= ACPI_GED(adev
);
226 GEDState
*ged_st
= &s
->ged_state
;
229 if (ev
& ACPI_MEMORY_HOTPLUG_STATUS
) {
230 sel
= ACPI_GED_MEM_HOTPLUG_EVT
;
231 } else if (ev
& ACPI_POWER_DOWN_STATUS
) {
232 sel
= ACPI_GED_PWR_DOWN_EVT
;
233 } else if (ev
& ACPI_NVDIMM_HOTPLUG_STATUS
) {
234 sel
= ACPI_GED_NVDIMM_HOTPLUG_EVT
;
236 /* Unknown event. Return without generating interrupt. */
237 warn_report("GED: Unsupported event %d. No irq injected", ev
);
242 * Set the GED selector field to communicate the event type.
243 * This will be read by GED aml code to select the appropriate
248 /* Trigger the event by sending an interrupt to the guest. */
249 qemu_irq_pulse(s
->irq
);
252 static Property acpi_ged_properties
[] = {
253 DEFINE_PROP_UINT32("ged-event", AcpiGedState
, ged_event_bitmap
, 0),
254 DEFINE_PROP_END_OF_LIST(),
257 static const VMStateDescription vmstate_memhp_state
= {
258 .name
= "acpi-ged/memhp",
260 .minimum_version_id
= 1,
261 .fields
= (VMStateField
[]) {
262 VMSTATE_MEMORY_HOTPLUG(memhp_state
, AcpiGedState
),
263 VMSTATE_END_OF_LIST()
267 static const VMStateDescription vmstate_ged_state
= {
268 .name
= "acpi-ged-state",
270 .minimum_version_id
= 1,
271 .fields
= (VMStateField
[]) {
272 VMSTATE_UINT32(sel
, GEDState
),
273 VMSTATE_END_OF_LIST()
277 static bool ghes_needed(void *opaque
)
279 AcpiGedState
*s
= opaque
;
280 return s
->ghes_state
.ghes_addr_le
;
283 static const VMStateDescription vmstate_ghes_state
= {
284 .name
= "acpi-ged/ghes",
286 .minimum_version_id
= 1,
287 .needed
= ghes_needed
,
288 .fields
= (VMStateField
[]) {
289 VMSTATE_STRUCT(ghes_state
, AcpiGedState
, 1,
290 vmstate_ghes_state
, AcpiGhesState
),
291 VMSTATE_END_OF_LIST()
295 static const VMStateDescription vmstate_acpi_ged
= {
298 .minimum_version_id
= 1,
299 .fields
= (VMStateField
[]) {
300 VMSTATE_STRUCT(ged_state
, AcpiGedState
, 1, vmstate_ged_state
, GEDState
),
301 VMSTATE_END_OF_LIST(),
303 .subsections
= (const VMStateDescription
* []) {
304 &vmstate_memhp_state
,
310 static void acpi_ged_initfn(Object
*obj
)
312 DeviceState
*dev
= DEVICE(obj
);
313 AcpiGedState
*s
= ACPI_GED(dev
);
314 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
315 GEDState
*ged_st
= &s
->ged_state
;
317 memory_region_init_io(&ged_st
->evt
, obj
, &ged_evt_ops
, ged_st
,
318 TYPE_ACPI_GED
, ACPI_GED_EVT_SEL_LEN
);
319 sysbus_init_mmio(sbd
, &ged_st
->evt
);
321 sysbus_init_irq(sbd
, &s
->irq
);
323 s
->memhp_state
.is_enabled
= true;
325 * GED handles memory hotplug event and acpi-mem-hotplug
326 * memory region gets initialized here. Create an exclusive
327 * container for memory hotplug IO and expose it as GED sysbus
328 * MMIO so that boards can map it separately.
330 memory_region_init(&s
->container_memhp
, OBJECT(dev
), "memhp container",
331 MEMORY_HOTPLUG_IO_LEN
);
332 sysbus_init_mmio(sbd
, &s
->container_memhp
);
333 acpi_memory_hotplug_init(&s
->container_memhp
, OBJECT(dev
),
337 static void acpi_ged_class_init(ObjectClass
*class, void *data
)
339 DeviceClass
*dc
= DEVICE_CLASS(class);
340 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(class);
341 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_CLASS(class);
343 dc
->desc
= "ACPI Generic Event Device";
344 device_class_set_props(dc
, acpi_ged_properties
);
345 dc
->vmsd
= &vmstate_acpi_ged
;
347 hc
->plug
= acpi_ged_device_plug_cb
;
348 hc
->unplug_request
= acpi_ged_unplug_request_cb
;
349 hc
->unplug
= acpi_ged_unplug_cb
;
351 adevc
->send_event
= acpi_ged_send_event
;
354 static const TypeInfo acpi_ged_info
= {
355 .name
= TYPE_ACPI_GED
,
356 .parent
= TYPE_SYS_BUS_DEVICE
,
357 .instance_size
= sizeof(AcpiGedState
),
358 .instance_init
= acpi_ged_initfn
,
359 .class_init
= acpi_ged_class_init
,
360 .interfaces
= (InterfaceInfo
[]) {
361 { TYPE_HOTPLUG_HANDLER
},
362 { TYPE_ACPI_DEVICE_IF
},
367 static void acpi_ged_register_types(void)
369 type_register_static(&acpi_ged_info
);
372 type_init(acpi_ged_register_types
)