acpi: add hardware implementation for memory hot unplug
[qemu/kevin.git] / hw / acpi / memory_hotplug.c
blob35bbfeb1d81edb744a78d19a8ad226468f40114a
1 #include "hw/acpi/memory_hotplug.h"
2 #include "hw/acpi/pc-hotplug.h"
3 #include "hw/mem/pc-dimm.h"
4 #include "hw/boards.h"
5 #include "hw/qdev-core.h"
6 #include "trace.h"
7 #include "qapi-event.h"
9 static ACPIOSTInfo *acpi_memory_device_status(int slot, MemStatus *mdev)
11 ACPIOSTInfo *info = g_new0(ACPIOSTInfo, 1);
13 info->slot_type = ACPI_SLOT_TYPE_DIMM;
14 info->slot = g_strdup_printf("%d", slot);
15 info->source = mdev->ost_event;
16 info->status = mdev->ost_status;
17 if (mdev->dimm) {
18 DeviceState *dev = DEVICE(mdev->dimm);
19 if (dev->id) {
20 info->device = g_strdup(dev->id);
21 info->has_device = true;
24 return info;
27 void acpi_memory_ospm_status(MemHotplugState *mem_st, ACPIOSTInfoList ***list)
29 int i;
31 for (i = 0; i < mem_st->dev_count; i++) {
32 ACPIOSTInfoList *elem = g_new0(ACPIOSTInfoList, 1);
33 elem->value = acpi_memory_device_status(i, &mem_st->devs[i]);
34 elem->next = NULL;
35 **list = elem;
36 *list = &elem->next;
40 static uint64_t acpi_memory_hotplug_read(void *opaque, hwaddr addr,
41 unsigned int size)
43 uint32_t val = 0;
44 MemHotplugState *mem_st = opaque;
45 MemStatus *mdev;
46 Object *o;
48 if (mem_st->selector >= mem_st->dev_count) {
49 trace_mhp_acpi_invalid_slot_selected(mem_st->selector);
50 return 0;
53 mdev = &mem_st->devs[mem_st->selector];
54 o = OBJECT(mdev->dimm);
55 switch (addr) {
56 case 0x0: /* Lo part of phys address where DIMM is mapped */
57 val = o ? object_property_get_int(o, PC_DIMM_ADDR_PROP, NULL) : 0;
58 trace_mhp_acpi_read_addr_lo(mem_st->selector, val);
59 break;
60 case 0x4: /* Hi part of phys address where DIMM is mapped */
61 val = o ? object_property_get_int(o, PC_DIMM_ADDR_PROP, NULL) >> 32 : 0;
62 trace_mhp_acpi_read_addr_hi(mem_st->selector, val);
63 break;
64 case 0x8: /* Lo part of DIMM size */
65 val = o ? object_property_get_int(o, PC_DIMM_SIZE_PROP, NULL) : 0;
66 trace_mhp_acpi_read_size_lo(mem_st->selector, val);
67 break;
68 case 0xc: /* Hi part of DIMM size */
69 val = o ? object_property_get_int(o, PC_DIMM_SIZE_PROP, NULL) >> 32 : 0;
70 trace_mhp_acpi_read_size_hi(mem_st->selector, val);
71 break;
72 case 0x10: /* node proximity for _PXM method */
73 val = o ? object_property_get_int(o, PC_DIMM_NODE_PROP, NULL) : 0;
74 trace_mhp_acpi_read_pxm(mem_st->selector, val);
75 break;
76 case 0x14: /* pack and return is_* fields */
77 val |= mdev->is_enabled ? 1 : 0;
78 val |= mdev->is_inserting ? 2 : 0;
79 val |= mdev->is_removing ? 4 : 0;
80 trace_mhp_acpi_read_flags(mem_st->selector, val);
81 break;
82 default:
83 val = ~0;
84 break;
86 return val;
89 static void acpi_memory_hotplug_write(void *opaque, hwaddr addr, uint64_t data,
90 unsigned int size)
92 MemHotplugState *mem_st = opaque;
93 MemStatus *mdev;
94 ACPIOSTInfo *info;
95 DeviceState *dev = NULL;
96 HotplugHandler *hotplug_ctrl = NULL;
98 if (!mem_st->dev_count) {
99 return;
102 if (addr) {
103 if (mem_st->selector >= mem_st->dev_count) {
104 trace_mhp_acpi_invalid_slot_selected(mem_st->selector);
105 return;
109 switch (addr) {
110 case 0x0: /* DIMM slot selector */
111 mem_st->selector = data;
112 trace_mhp_acpi_write_slot(mem_st->selector);
113 break;
114 case 0x4: /* _OST event */
115 mdev = &mem_st->devs[mem_st->selector];
116 if (data == 1) {
117 /* TODO: handle device insert OST event */
118 } else if (data == 3) {
119 /* TODO: handle device remove OST event */
121 mdev->ost_event = data;
122 trace_mhp_acpi_write_ost_ev(mem_st->selector, mdev->ost_event);
123 break;
124 case 0x8: /* _OST status */
125 mdev = &mem_st->devs[mem_st->selector];
126 mdev->ost_status = data;
127 trace_mhp_acpi_write_ost_status(mem_st->selector, mdev->ost_status);
128 /* TODO: implement memory removal on guest signal */
130 info = acpi_memory_device_status(mem_st->selector, mdev);
131 qapi_event_send_acpi_device_ost(info, &error_abort);
132 qapi_free_ACPIOSTInfo(info);
133 break;
134 case 0x14: /* set is_* fields */
135 mdev = &mem_st->devs[mem_st->selector];
136 if (data & 2) { /* clear insert event */
137 mdev->is_inserting = false;
138 trace_mhp_acpi_clear_insert_evt(mem_st->selector);
139 } else if (data & 4) {
140 mdev->is_removing = false;
141 trace_mhp_acpi_clear_remove_evt(mem_st->selector);
142 } else if (data & 8) {
143 if (!mdev->is_enabled) {
144 trace_mhp_acpi_ejecting_invalid_slot(mem_st->selector);
145 break;
148 dev = DEVICE(mdev->dimm);
149 hotplug_ctrl = qdev_get_hotplug_handler(dev);
150 /* call pc-dimm unplug cb */
151 hotplug_handler_unplug(hotplug_ctrl, dev, NULL);
152 trace_mhp_acpi_pc_dimm_deleted(mem_st->selector);
154 break;
155 default:
156 break;
160 static const MemoryRegionOps acpi_memory_hotplug_ops = {
161 .read = acpi_memory_hotplug_read,
162 .write = acpi_memory_hotplug_write,
163 .endianness = DEVICE_LITTLE_ENDIAN,
164 .valid = {
165 .min_access_size = 1,
166 .max_access_size = 4,
170 void acpi_memory_hotplug_init(MemoryRegion *as, Object *owner,
171 MemHotplugState *state)
173 MachineState *machine = MACHINE(qdev_get_machine());
175 state->dev_count = machine->ram_slots;
176 if (!state->dev_count) {
177 return;
180 state->devs = g_malloc0(sizeof(*state->devs) * state->dev_count);
181 memory_region_init_io(&state->io, owner, &acpi_memory_hotplug_ops, state,
182 "acpi-mem-hotplug", ACPI_MEMORY_HOTPLUG_IO_LEN);
183 memory_region_add_subregion(as, ACPI_MEMORY_HOTPLUG_BASE, &state->io);
187 * acpi_memory_slot_status:
188 * @mem_st: memory hotplug state
189 * @dev: device
190 * @errp: set in case of an error
192 * Obtain a single memory slot status.
194 * This function will be called by memory unplug request cb and unplug cb.
196 static MemStatus *
197 acpi_memory_slot_status(MemHotplugState *mem_st,
198 DeviceState *dev, Error **errp)
200 Error *local_err = NULL;
201 int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
202 &local_err);
204 if (local_err) {
205 error_propagate(errp, local_err);
206 return NULL;
209 if (slot >= mem_st->dev_count) {
210 char *dev_path = object_get_canonical_path(OBJECT(dev));
211 error_setg(errp, "acpi_memory_slot_status: "
212 "device [%s] returned invalid memory slot[%d]",
213 dev_path, slot);
214 g_free(dev_path);
215 return NULL;
218 return &mem_st->devs[slot];
221 void acpi_memory_plug_cb(ACPIREGS *ar, qemu_irq irq, MemHotplugState *mem_st,
222 DeviceState *dev, Error **errp)
224 MemStatus *mdev;
226 mdev = acpi_memory_slot_status(mem_st, dev, errp);
227 if (!mdev) {
228 return;
231 mdev->dimm = dev;
232 mdev->is_enabled = true;
233 mdev->is_inserting = true;
235 /* do ACPI magic */
236 ar->gpe.sts[0] |= ACPI_MEMORY_HOTPLUG_STATUS;
237 acpi_update_sci(ar, irq);
238 return;
241 void acpi_memory_unplug_request_cb(ACPIREGS *ar, qemu_irq irq,
242 MemHotplugState *mem_st,
243 DeviceState *dev, Error **errp)
245 MemStatus *mdev;
247 mdev = acpi_memory_slot_status(mem_st, dev, errp);
248 if (!mdev) {
249 return;
252 mdev->is_removing = true;
254 /* Do ACPI magic */
255 ar->gpe.sts[0] |= ACPI_MEMORY_HOTPLUG_STATUS;
256 acpi_update_sci(ar, irq);
259 void acpi_memory_unplug_cb(MemHotplugState *mem_st,
260 DeviceState *dev, Error **errp)
262 MemStatus *mdev;
264 mdev = acpi_memory_slot_status(mem_st, dev, errp);
265 if (!mdev) {
266 return;
269 mdev->is_enabled = false;
270 mdev->dimm = NULL;
273 static const VMStateDescription vmstate_memhp_sts = {
274 .name = "memory hotplug device state",
275 .version_id = 1,
276 .minimum_version_id = 1,
277 .minimum_version_id_old = 1,
278 .fields = (VMStateField[]) {
279 VMSTATE_BOOL(is_enabled, MemStatus),
280 VMSTATE_BOOL(is_inserting, MemStatus),
281 VMSTATE_UINT32(ost_event, MemStatus),
282 VMSTATE_UINT32(ost_status, MemStatus),
283 VMSTATE_END_OF_LIST()
287 const VMStateDescription vmstate_memory_hotplug = {
288 .name = "memory hotplug state",
289 .version_id = 1,
290 .minimum_version_id = 1,
291 .minimum_version_id_old = 1,
292 .fields = (VMStateField[]) {
293 VMSTATE_UINT32(selector, MemHotplugState),
294 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(devs, MemHotplugState, dev_count,
295 vmstate_memhp_sts, MemStatus),
296 VMSTATE_END_OF_LIST()