]> git.proxmox.com Git - mirror_qemu.git/blob - hw/acpi/memory_hotplug.c
pc: memhp: enable nvdimm device hotplug
[mirror_qemu.git] / hw / acpi / memory_hotplug.c
1 #include "qemu/osdep.h"
2 #include "hw/acpi/memory_hotplug.h"
3 #include "hw/acpi/pc-hotplug.h"
4 #include "hw/mem/pc-dimm.h"
5 #include "hw/mem/nvdimm.h"
6 #include "hw/boards.h"
7 #include "hw/qdev-core.h"
8 #include "trace.h"
9 #include "qapi-event.h"
10
11 static ACPIOSTInfo *acpi_memory_device_status(int slot, MemStatus *mdev)
12 {
13 ACPIOSTInfo *info = g_new0(ACPIOSTInfo, 1);
14
15 info->slot_type = ACPI_SLOT_TYPE_DIMM;
16 info->slot = g_strdup_printf("%d", slot);
17 info->source = mdev->ost_event;
18 info->status = mdev->ost_status;
19 if (mdev->dimm) {
20 DeviceState *dev = DEVICE(mdev->dimm);
21 if (dev->id) {
22 info->device = g_strdup(dev->id);
23 info->has_device = true;
24 }
25 }
26 return info;
27 }
28
29 void acpi_memory_ospm_status(MemHotplugState *mem_st, ACPIOSTInfoList ***list)
30 {
31 int i;
32
33 for (i = 0; i < mem_st->dev_count; i++) {
34 ACPIOSTInfoList *elem = g_new0(ACPIOSTInfoList, 1);
35 elem->value = acpi_memory_device_status(i, &mem_st->devs[i]);
36 elem->next = NULL;
37 **list = elem;
38 *list = &elem->next;
39 }
40 }
41
42 static uint64_t acpi_memory_hotplug_read(void *opaque, hwaddr addr,
43 unsigned int size)
44 {
45 uint32_t val = 0;
46 MemHotplugState *mem_st = opaque;
47 MemStatus *mdev;
48 Object *o;
49
50 if (mem_st->selector >= mem_st->dev_count) {
51 trace_mhp_acpi_invalid_slot_selected(mem_st->selector);
52 return 0;
53 }
54
55 mdev = &mem_st->devs[mem_st->selector];
56 o = OBJECT(mdev->dimm);
57 switch (addr) {
58 case 0x0: /* Lo part of phys address where DIMM is mapped */
59 val = o ? object_property_get_int(o, PC_DIMM_ADDR_PROP, NULL) : 0;
60 trace_mhp_acpi_read_addr_lo(mem_st->selector, val);
61 break;
62 case 0x4: /* Hi part of phys address where DIMM is mapped */
63 val = o ? object_property_get_int(o, PC_DIMM_ADDR_PROP, NULL) >> 32 : 0;
64 trace_mhp_acpi_read_addr_hi(mem_st->selector, val);
65 break;
66 case 0x8: /* Lo part of DIMM size */
67 val = o ? object_property_get_int(o, PC_DIMM_SIZE_PROP, NULL) : 0;
68 trace_mhp_acpi_read_size_lo(mem_st->selector, val);
69 break;
70 case 0xc: /* Hi part of DIMM size */
71 val = o ? object_property_get_int(o, PC_DIMM_SIZE_PROP, NULL) >> 32 : 0;
72 trace_mhp_acpi_read_size_hi(mem_st->selector, val);
73 break;
74 case 0x10: /* node proximity for _PXM method */
75 val = o ? object_property_get_int(o, PC_DIMM_NODE_PROP, NULL) : 0;
76 trace_mhp_acpi_read_pxm(mem_st->selector, val);
77 break;
78 case 0x14: /* pack and return is_* fields */
79 val |= mdev->is_enabled ? 1 : 0;
80 val |= mdev->is_inserting ? 2 : 0;
81 val |= mdev->is_removing ? 4 : 0;
82 trace_mhp_acpi_read_flags(mem_st->selector, val);
83 break;
84 default:
85 val = ~0;
86 break;
87 }
88 return val;
89 }
90
91 static void acpi_memory_hotplug_write(void *opaque, hwaddr addr, uint64_t data,
92 unsigned int size)
93 {
94 MemHotplugState *mem_st = opaque;
95 MemStatus *mdev;
96 ACPIOSTInfo *info;
97 DeviceState *dev = NULL;
98 HotplugHandler *hotplug_ctrl = NULL;
99 Error *local_err = NULL;
100
101 if (!mem_st->dev_count) {
102 return;
103 }
104
105 if (addr) {
106 if (mem_st->selector >= mem_st->dev_count) {
107 trace_mhp_acpi_invalid_slot_selected(mem_st->selector);
108 return;
109 }
110 }
111
112 switch (addr) {
113 case 0x0: /* DIMM slot selector */
114 mem_st->selector = data;
115 trace_mhp_acpi_write_slot(mem_st->selector);
116 break;
117 case 0x4: /* _OST event */
118 mdev = &mem_st->devs[mem_st->selector];
119 if (data == 1) {
120 /* TODO: handle device insert OST event */
121 } else if (data == 3) {
122 /* TODO: handle device remove OST event */
123 }
124 mdev->ost_event = data;
125 trace_mhp_acpi_write_ost_ev(mem_st->selector, mdev->ost_event);
126 break;
127 case 0x8: /* _OST status */
128 mdev = &mem_st->devs[mem_st->selector];
129 mdev->ost_status = data;
130 trace_mhp_acpi_write_ost_status(mem_st->selector, mdev->ost_status);
131 /* TODO: implement memory removal on guest signal */
132
133 info = acpi_memory_device_status(mem_st->selector, mdev);
134 qapi_event_send_acpi_device_ost(info, &error_abort);
135 qapi_free_ACPIOSTInfo(info);
136 break;
137 case 0x14: /* set is_* fields */
138 mdev = &mem_st->devs[mem_st->selector];
139 if (data & 2) { /* clear insert event */
140 mdev->is_inserting = false;
141 trace_mhp_acpi_clear_insert_evt(mem_st->selector);
142 } else if (data & 4) {
143 mdev->is_removing = false;
144 trace_mhp_acpi_clear_remove_evt(mem_st->selector);
145 } else if (data & 8) {
146 if (!mdev->is_enabled) {
147 trace_mhp_acpi_ejecting_invalid_slot(mem_st->selector);
148 break;
149 }
150
151 dev = DEVICE(mdev->dimm);
152 hotplug_ctrl = qdev_get_hotplug_handler(dev);
153 /* call pc-dimm unplug cb */
154 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
155 if (local_err) {
156 trace_mhp_acpi_pc_dimm_delete_failed(mem_st->selector);
157 qapi_event_send_mem_unplug_error(dev->id,
158 error_get_pretty(local_err),
159 &error_abort);
160 error_free(local_err);
161 break;
162 }
163 trace_mhp_acpi_pc_dimm_deleted(mem_st->selector);
164 }
165 break;
166 default:
167 break;
168 }
169
170 }
171 static const MemoryRegionOps acpi_memory_hotplug_ops = {
172 .read = acpi_memory_hotplug_read,
173 .write = acpi_memory_hotplug_write,
174 .endianness = DEVICE_LITTLE_ENDIAN,
175 .valid = {
176 .min_access_size = 1,
177 .max_access_size = 4,
178 },
179 };
180
181 void acpi_memory_hotplug_init(MemoryRegion *as, Object *owner,
182 MemHotplugState *state)
183 {
184 MachineState *machine = MACHINE(qdev_get_machine());
185
186 state->dev_count = machine->ram_slots;
187 if (!state->dev_count) {
188 return;
189 }
190
191 state->devs = g_malloc0(sizeof(*state->devs) * state->dev_count);
192 memory_region_init_io(&state->io, owner, &acpi_memory_hotplug_ops, state,
193 "acpi-mem-hotplug", ACPI_MEMORY_HOTPLUG_IO_LEN);
194 memory_region_add_subregion(as, ACPI_MEMORY_HOTPLUG_BASE, &state->io);
195 }
196
197 /**
198 * acpi_memory_slot_status:
199 * @mem_st: memory hotplug state
200 * @dev: device
201 * @errp: set in case of an error
202 *
203 * Obtain a single memory slot status.
204 *
205 * This function will be called by memory unplug request cb and unplug cb.
206 */
207 static MemStatus *
208 acpi_memory_slot_status(MemHotplugState *mem_st,
209 DeviceState *dev, Error **errp)
210 {
211 Error *local_err = NULL;
212 int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
213 &local_err);
214
215 if (local_err) {
216 error_propagate(errp, local_err);
217 return NULL;
218 }
219
220 if (slot >= mem_st->dev_count) {
221 char *dev_path = object_get_canonical_path(OBJECT(dev));
222 error_setg(errp, "acpi_memory_slot_status: "
223 "device [%s] returned invalid memory slot[%d]",
224 dev_path, slot);
225 g_free(dev_path);
226 return NULL;
227 }
228
229 return &mem_st->devs[slot];
230 }
231
232 void acpi_memory_plug_cb(HotplugHandler *hotplug_dev, MemHotplugState *mem_st,
233 DeviceState *dev, Error **errp)
234 {
235 MemStatus *mdev;
236 AcpiEventStatusBits event;
237 bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
238
239 mdev = acpi_memory_slot_status(mem_st, dev, errp);
240 if (!mdev) {
241 return;
242 }
243
244 mdev->dimm = dev;
245
246 /*
247 * do not set is_enabled and is_inserting if the slot is plugged with
248 * a nvdimm device to stop OSPM inquires memory region from the slot.
249 */
250 if (is_nvdimm) {
251 event = ACPI_NVDIMM_HOTPLUG_STATUS;
252 } else {
253 mdev->is_enabled = true;
254 event = ACPI_MEMORY_HOTPLUG_STATUS;
255 }
256
257 if (dev->hotplugged) {
258 if (!is_nvdimm) {
259 mdev->is_inserting = true;
260 }
261 acpi_send_event(DEVICE(hotplug_dev), event);
262 }
263 }
264
265 void acpi_memory_unplug_request_cb(HotplugHandler *hotplug_dev,
266 MemHotplugState *mem_st,
267 DeviceState *dev, Error **errp)
268 {
269 MemStatus *mdev;
270
271 mdev = acpi_memory_slot_status(mem_st, dev, errp);
272 if (!mdev) {
273 return;
274 }
275
276 /* nvdimm device hot unplug is not supported yet. */
277 assert(!object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM));
278 mdev->is_removing = true;
279 acpi_send_event(DEVICE(hotplug_dev), ACPI_MEMORY_HOTPLUG_STATUS);
280 }
281
282 void acpi_memory_unplug_cb(MemHotplugState *mem_st,
283 DeviceState *dev, Error **errp)
284 {
285 MemStatus *mdev;
286
287 mdev = acpi_memory_slot_status(mem_st, dev, errp);
288 if (!mdev) {
289 return;
290 }
291
292 /* nvdimm device hot unplug is not supported yet. */
293 assert(!object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM));
294 mdev->is_enabled = false;
295 mdev->dimm = NULL;
296 }
297
298 static const VMStateDescription vmstate_memhp_sts = {
299 .name = "memory hotplug device state",
300 .version_id = 1,
301 .minimum_version_id = 1,
302 .minimum_version_id_old = 1,
303 .fields = (VMStateField[]) {
304 VMSTATE_BOOL(is_enabled, MemStatus),
305 VMSTATE_BOOL(is_inserting, MemStatus),
306 VMSTATE_UINT32(ost_event, MemStatus),
307 VMSTATE_UINT32(ost_status, MemStatus),
308 VMSTATE_END_OF_LIST()
309 }
310 };
311
312 const VMStateDescription vmstate_memory_hotplug = {
313 .name = "memory hotplug state",
314 .version_id = 1,
315 .minimum_version_id = 1,
316 .minimum_version_id_old = 1,
317 .fields = (VMStateField[]) {
318 VMSTATE_UINT32(selector, MemHotplugState),
319 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(devs, MemHotplugState, dev_count,
320 vmstate_memhp_sts, MemStatus),
321 VMSTATE_END_OF_LIST()
322 }
323 };