]>
Commit | Line | Data |
---|---|---|
10b7e74b VL |
1 | /* |
2 | * Dimm device for Memory Hotplug | |
3 | * | |
4 | * Copyright ProfitBricks GmbH 2012 | |
5 | * Copyright (C) 2014 Red Hat Inc | |
6 | * | |
7 | * This library is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2 of the License, or (at your option) any later version. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
18 | * License along with this library; if not, see <http://www.gnu.org/licenses/> | |
19 | */ | |
20 | ||
0430891c | 21 | #include "qemu/osdep.h" |
10b7e74b | 22 | #include "hw/mem/pc-dimm.h" |
6388e18d | 23 | #include "hw/mem/nvdimm.h" |
2cc0e2e8 | 24 | #include "hw/mem/memory-device.h" |
da34e65c | 25 | #include "qapi/error.h" |
10b7e74b VL |
26 | #include "qemu/config-file.h" |
27 | #include "qapi/visitor.h" | |
0b312571 | 28 | #include "qemu/range.h" |
e35704ba | 29 | #include "sysemu/numa.h" |
43bbb49e BR |
30 | #include "sysemu/kvm.h" |
31 | #include "trace.h" | |
3fad8788 | 32 | #include "hw/virtio/vhost.h" |
9967c949 | 33 | |
37153450 BR |
34 | typedef struct pc_dimms_capacity { |
35 | uint64_t size; | |
36 | Error **errp; | |
37 | } pc_dimms_capacity; | |
38 | ||
43bbb49e | 39 | void pc_dimm_memory_plug(DeviceState *dev, MemoryHotplugState *hpms, |
acc7fa17 | 40 | uint64_t align, Error **errp) |
43bbb49e BR |
41 | { |
42 | int slot; | |
43 | MachineState *machine = MACHINE(qdev_get_machine()); | |
44 | PCDIMMDevice *dimm = PC_DIMM(dev); | |
8df1426e XG |
45 | PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm); |
46 | MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm); | |
43bbb49e BR |
47 | Error *local_err = NULL; |
48 | uint64_t existing_dimms_capacity = 0; | |
acc7fa17 | 49 | MemoryRegion *mr; |
43bbb49e BR |
50 | uint64_t addr; |
51 | ||
acc7fa17 DH |
52 | mr = ddc->get_memory_region(dimm, &local_err); |
53 | if (local_err) { | |
54 | goto out; | |
55 | } | |
56 | ||
9ed442b8 MAL |
57 | addr = object_property_get_uint(OBJECT(dimm), |
58 | PC_DIMM_ADDR_PROP, &local_err); | |
43bbb49e BR |
59 | if (local_err) { |
60 | goto out; | |
61 | } | |
62 | ||
63 | addr = pc_dimm_get_free_addr(hpms->base, | |
64 | memory_region_size(&hpms->mr), | |
d6a9b0b8 | 65 | !addr ? NULL : &addr, align, |
43bbb49e BR |
66 | memory_region_size(mr), &local_err); |
67 | if (local_err) { | |
68 | goto out; | |
69 | } | |
70 | ||
71 | existing_dimms_capacity = pc_existing_dimms_capacity(&local_err); | |
72 | if (local_err) { | |
73 | goto out; | |
74 | } | |
75 | ||
76 | if (existing_dimms_capacity + memory_region_size(mr) > | |
77 | machine->maxram_size - machine->ram_size) { | |
78 | error_setg(&local_err, "not enough space, currently 0x%" PRIx64 | |
79 | " in use of total hot pluggable 0x" RAM_ADDR_FMT, | |
80 | existing_dimms_capacity, | |
81 | machine->maxram_size - machine->ram_size); | |
82 | goto out; | |
83 | } | |
84 | ||
9ed442b8 | 85 | object_property_set_uint(OBJECT(dev), addr, PC_DIMM_ADDR_PROP, &local_err); |
43bbb49e BR |
86 | if (local_err) { |
87 | goto out; | |
88 | } | |
89 | trace_mhp_pc_dimm_assigned_address(addr); | |
90 | ||
91 | slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP, &local_err); | |
92 | if (local_err) { | |
93 | goto out; | |
94 | } | |
95 | ||
96 | slot = pc_dimm_get_free_slot(slot == PC_DIMM_UNASSIGNED_SLOT ? NULL : &slot, | |
97 | machine->ram_slots, &local_err); | |
98 | if (local_err) { | |
99 | goto out; | |
100 | } | |
101 | object_property_set_int(OBJECT(dev), slot, PC_DIMM_SLOT_PROP, &local_err); | |
102 | if (local_err) { | |
103 | goto out; | |
104 | } | |
105 | trace_mhp_pc_dimm_assigned_slot(slot); | |
106 | ||
107 | if (kvm_enabled() && !kvm_has_free_slot(machine)) { | |
108 | error_setg(&local_err, "hypervisor has no free memory slots left"); | |
109 | goto out; | |
110 | } | |
111 | ||
3fad8788 IM |
112 | if (!vhost_has_free_slot()) { |
113 | error_setg(&local_err, "a used vhost backend has no free" | |
114 | " memory slots left"); | |
115 | goto out; | |
116 | } | |
117 | ||
43bbb49e | 118 | memory_region_add_subregion(&hpms->mr, addr - hpms->base, mr); |
8df1426e | 119 | vmstate_register_ram(vmstate_mr, dev); |
43bbb49e BR |
120 | |
121 | out: | |
122 | error_propagate(errp, local_err); | |
123 | } | |
124 | ||
acc7fa17 | 125 | void pc_dimm_memory_unplug(DeviceState *dev, MemoryHotplugState *hpms) |
43bbb49e | 126 | { |
fa9ea81d | 127 | PCDIMMDevice *dimm = PC_DIMM(dev); |
8df1426e XG |
128 | PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm); |
129 | MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm); | |
acc7fa17 | 130 | MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort); |
fa9ea81d | 131 | |
43bbb49e | 132 | memory_region_del_subregion(&hpms->mr, mr); |
8df1426e | 133 | vmstate_unregister_ram(vmstate_mr, dev); |
43bbb49e BR |
134 | } |
135 | ||
37153450 | 136 | static int pc_existing_dimms_capacity_internal(Object *obj, void *opaque) |
9967c949 | 137 | { |
37153450 BR |
138 | pc_dimms_capacity *cap = opaque; |
139 | uint64_t *size = &cap->size; | |
9967c949 BR |
140 | |
141 | if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { | |
142 | DeviceState *dev = DEVICE(obj); | |
143 | ||
144 | if (dev->realized) { | |
b053ef61 | 145 | (*size) += object_property_get_uint(obj, PC_DIMM_SIZE_PROP, |
37153450 | 146 | cap->errp); |
9967c949 BR |
147 | } |
148 | ||
37153450 | 149 | if (cap->errp && *cap->errp) { |
9967c949 BR |
150 | return 1; |
151 | } | |
152 | } | |
37153450 | 153 | object_child_foreach(obj, pc_existing_dimms_capacity_internal, opaque); |
9967c949 BR |
154 | return 0; |
155 | } | |
0b312571 | 156 | |
37153450 BR |
157 | uint64_t pc_existing_dimms_capacity(Error **errp) |
158 | { | |
159 | pc_dimms_capacity cap; | |
160 | ||
161 | cap.size = 0; | |
162 | cap.errp = errp; | |
163 | ||
164 | pc_existing_dimms_capacity_internal(qdev_get_machine(), &cap); | |
165 | return cap.size; | |
166 | } | |
167 | ||
0cd03d89 IM |
168 | static int pc_dimm_slot2bitmap(Object *obj, void *opaque) |
169 | { | |
170 | unsigned long *bitmap = opaque; | |
171 | ||
172 | if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { | |
173 | DeviceState *dev = DEVICE(obj); | |
174 | if (dev->realized) { /* count only realized DIMMs */ | |
175 | PCDIMMDevice *d = PC_DIMM(obj); | |
176 | set_bit(d->slot, bitmap); | |
177 | } | |
178 | } | |
179 | ||
180 | object_child_foreach(obj, pc_dimm_slot2bitmap, opaque); | |
181 | return 0; | |
182 | } | |
183 | ||
184 | int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp) | |
185 | { | |
186 | unsigned long *bitmap = bitmap_new(max_slots); | |
187 | int slot = 0; | |
188 | ||
189 | object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap, bitmap); | |
190 | ||
191 | /* check if requested slot is not occupied */ | |
192 | if (hint) { | |
193 | if (*hint >= max_slots) { | |
194 | error_setg(errp, "invalid slot# %d, should be less than %d", | |
195 | *hint, max_slots); | |
196 | } else if (!test_bit(*hint, bitmap)) { | |
197 | slot = *hint; | |
198 | } else { | |
199 | error_setg(errp, "slot %d is busy", *hint); | |
200 | } | |
201 | goto out; | |
202 | } | |
203 | ||
204 | /* search for free slot */ | |
205 | slot = find_first_zero_bit(bitmap, max_slots); | |
206 | if (slot == max_slots) { | |
207 | error_setg(errp, "no free slots available"); | |
208 | } | |
209 | out: | |
210 | g_free(bitmap); | |
211 | return slot; | |
212 | } | |
213 | ||
0b312571 IM |
214 | static gint pc_dimm_addr_sort(gconstpointer a, gconstpointer b) |
215 | { | |
216 | PCDIMMDevice *x = PC_DIMM(a); | |
217 | PCDIMMDevice *y = PC_DIMM(b); | |
218 | Int128 diff = int128_sub(int128_make64(x->addr), int128_make64(y->addr)); | |
219 | ||
220 | if (int128_lt(diff, int128_zero())) { | |
221 | return -1; | |
222 | } else if (int128_gt(diff, int128_zero())) { | |
223 | return 1; | |
224 | } | |
225 | return 0; | |
226 | } | |
227 | ||
228 | static int pc_dimm_built_list(Object *obj, void *opaque) | |
229 | { | |
230 | GSList **list = opaque; | |
231 | ||
232 | if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { | |
233 | DeviceState *dev = DEVICE(obj); | |
234 | if (dev->realized) { /* only realized DIMMs matter */ | |
235 | *list = g_slist_insert_sorted(*list, dev, pc_dimm_addr_sort); | |
236 | } | |
237 | } | |
238 | ||
239 | object_child_foreach(obj, pc_dimm_built_list, opaque); | |
240 | return 0; | |
241 | } | |
242 | ||
243 | uint64_t pc_dimm_get_free_addr(uint64_t address_space_start, | |
244 | uint64_t address_space_size, | |
d6a9b0b8 MT |
245 | uint64_t *hint, uint64_t align, uint64_t size, |
246 | Error **errp) | |
0b312571 IM |
247 | { |
248 | GSList *list = NULL, *item; | |
249 | uint64_t new_addr, ret = 0; | |
250 | uint64_t address_space_end = address_space_start + address_space_size; | |
251 | ||
0c0de1b6 | 252 | g_assert(QEMU_ALIGN_UP(address_space_start, align) == address_space_start); |
0c0de1b6 | 253 | |
9b79a76c IM |
254 | if (!address_space_size) { |
255 | error_setg(errp, "memory hotplug is not enabled, " | |
256 | "please add maxmem option"); | |
257 | goto out; | |
258 | } | |
259 | ||
92a37a04 IM |
260 | if (hint && QEMU_ALIGN_UP(*hint, align) != *hint) { |
261 | error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes", | |
262 | align); | |
263 | goto out; | |
264 | } | |
265 | ||
266 | if (QEMU_ALIGN_UP(size, align) != size) { | |
267 | error_setg(errp, "backend memory size must be multiple of 0x%" | |
268 | PRIx64, align); | |
269 | goto out; | |
270 | } | |
271 | ||
9b79a76c | 272 | assert(address_space_end > address_space_start); |
0b312571 IM |
273 | object_child_foreach(qdev_get_machine(), pc_dimm_built_list, &list); |
274 | ||
275 | if (hint) { | |
276 | new_addr = *hint; | |
277 | } else { | |
278 | new_addr = address_space_start; | |
279 | } | |
280 | ||
281 | /* find address range that will fit new DIMM */ | |
282 | for (item = list; item; item = g_slist_next(item)) { | |
283 | PCDIMMDevice *dimm = item->data; | |
b053ef61 MAL |
284 | uint64_t dimm_size = object_property_get_uint(OBJECT(dimm), |
285 | PC_DIMM_SIZE_PROP, | |
286 | errp); | |
0b312571 IM |
287 | if (errp && *errp) { |
288 | goto out; | |
289 | } | |
290 | ||
d6a9b0b8 | 291 | if (ranges_overlap(dimm->addr, dimm_size, new_addr, size)) { |
0b312571 IM |
292 | if (hint) { |
293 | DeviceState *d = DEVICE(dimm); | |
294 | error_setg(errp, "address range conflicts with '%s'", d->id); | |
295 | goto out; | |
296 | } | |
d6a9b0b8 | 297 | new_addr = QEMU_ALIGN_UP(dimm->addr + dimm_size, align); |
0b312571 IM |
298 | } |
299 | } | |
300 | ret = new_addr; | |
301 | ||
302 | if (new_addr < address_space_start) { | |
303 | error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64 | |
304 | "] at 0x%" PRIx64, new_addr, size, address_space_start); | |
305 | } else if ((new_addr + size) > address_space_end) { | |
306 | error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64 | |
307 | "] beyond 0x%" PRIx64, new_addr, size, address_space_end); | |
308 | } | |
309 | ||
310 | out: | |
311 | g_slist_free(list); | |
312 | return ret; | |
313 | } | |
10b7e74b VL |
314 | |
315 | static Property pc_dimm_properties[] = { | |
316 | DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0), | |
317 | DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0), | |
318 | DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot, | |
319 | PC_DIMM_UNASSIGNED_SLOT), | |
2de7e268 FZ |
320 | DEFINE_PROP_LINK(PC_DIMM_MEMDEV_PROP, PCDIMMDevice, hostmem, |
321 | TYPE_MEMORY_BACKEND, HostMemoryBackend *), | |
10b7e74b VL |
322 | DEFINE_PROP_END_OF_LIST(), |
323 | }; | |
324 | ||
d7bce999 EB |
325 | static void pc_dimm_get_size(Object *obj, Visitor *v, const char *name, |
326 | void *opaque, Error **errp) | |
10b7e74b | 327 | { |
b053ef61 | 328 | uint64_t value; |
10b7e74b VL |
329 | MemoryRegion *mr; |
330 | PCDIMMDevice *dimm = PC_DIMM(obj); | |
3c3e88a8 | 331 | PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(obj); |
10b7e74b | 332 | |
04790978 TH |
333 | mr = ddc->get_memory_region(dimm, errp); |
334 | if (!mr) { | |
335 | return; | |
336 | } | |
10b7e74b VL |
337 | value = memory_region_size(mr); |
338 | ||
b053ef61 | 339 | visit_type_uint64(v, name, &value, errp); |
10b7e74b VL |
340 | } |
341 | ||
342 | static void pc_dimm_init(Object *obj) | |
343 | { | |
b053ef61 | 344 | object_property_add(obj, PC_DIMM_SIZE_PROP, "uint64", pc_dimm_get_size, |
10b7e74b | 345 | NULL, NULL, NULL, &error_abort); |
10b7e74b VL |
346 | } |
347 | ||
348 | static void pc_dimm_realize(DeviceState *dev, Error **errp) | |
349 | { | |
350 | PCDIMMDevice *dimm = PC_DIMM(dev); | |
9f318f8f | 351 | PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm); |
10b7e74b VL |
352 | |
353 | if (!dimm->hostmem) { | |
354 | error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set"); | |
355 | return; | |
2de7e268 FZ |
356 | } else if (host_memory_backend_is_mapped(dimm->hostmem)) { |
357 | char *path = object_get_canonical_path_component(OBJECT(dimm->hostmem)); | |
358 | error_setg(errp, "can't use already busy memdev: %s", path); | |
359 | g_free(path); | |
360 | return; | |
10b7e74b | 361 | } |
32532f21 BR |
362 | if (((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) || |
363 | (!nb_numa_nodes && dimm->node)) { | |
988eba0f MT |
364 | error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %" |
365 | PRIu32 "' which exceeds the number of numa nodes: %d", | |
32532f21 | 366 | dimm->node, nb_numa_nodes ? nb_numa_nodes : 1); |
cfe0ffd0 HT |
367 | return; |
368 | } | |
9f318f8f XG |
369 | |
370 | if (ddc->realize) { | |
371 | ddc->realize(dimm, errp); | |
372 | } | |
2aece63c XG |
373 | |
374 | host_memory_backend_set_mapped(dimm->hostmem, true); | |
375 | } | |
376 | ||
377 | static void pc_dimm_unrealize(DeviceState *dev, Error **errp) | |
378 | { | |
379 | PCDIMMDevice *dimm = PC_DIMM(dev); | |
380 | ||
381 | host_memory_backend_set_mapped(dimm->hostmem, false); | |
10b7e74b VL |
382 | } |
383 | ||
04790978 | 384 | static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm, Error **errp) |
10b7e74b | 385 | { |
04790978 TH |
386 | if (!dimm->hostmem) { |
387 | error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property must be set"); | |
388 | return NULL; | |
389 | } | |
390 | ||
391 | return host_memory_backend_get_memory(dimm->hostmem, errp); | |
10b7e74b VL |
392 | } |
393 | ||
8df1426e XG |
394 | static MemoryRegion *pc_dimm_get_vmstate_memory_region(PCDIMMDevice *dimm) |
395 | { | |
396 | return host_memory_backend_get_memory(dimm->hostmem, &error_abort); | |
397 | } | |
398 | ||
2cc0e2e8 DH |
399 | static uint64_t pc_dimm_md_get_addr(const MemoryDeviceState *md) |
400 | { | |
401 | const PCDIMMDevice *dimm = PC_DIMM(md); | |
402 | ||
403 | return dimm->addr; | |
404 | } | |
405 | ||
406 | static uint64_t pc_dimm_md_get_region_size(const MemoryDeviceState *md) | |
407 | { | |
408 | /* dropping const here is fine as we don't touch the memory region */ | |
409 | PCDIMMDevice *dimm = PC_DIMM(md); | |
410 | const PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(md); | |
411 | MemoryRegion *mr; | |
412 | ||
413 | mr = ddc->get_memory_region(dimm, &error_abort); | |
414 | if (!mr) { | |
415 | return 0; | |
416 | } | |
417 | ||
418 | return memory_region_size(mr); | |
419 | } | |
420 | ||
421 | static void pc_dimm_md_fill_device_info(const MemoryDeviceState *md, | |
422 | MemoryDeviceInfo *info) | |
423 | { | |
424 | PCDIMMDeviceInfo *di = g_new0(PCDIMMDeviceInfo, 1); | |
425 | const DeviceClass *dc = DEVICE_GET_CLASS(md); | |
426 | const PCDIMMDevice *dimm = PC_DIMM(md); | |
427 | const DeviceState *dev = DEVICE(md); | |
428 | ||
429 | if (dev->id) { | |
430 | di->has_id = true; | |
431 | di->id = g_strdup(dev->id); | |
432 | } | |
433 | di->hotplugged = dev->hotplugged; | |
434 | di->hotpluggable = dc->hotpluggable; | |
435 | di->addr = dimm->addr; | |
436 | di->slot = dimm->slot; | |
437 | di->node = dimm->node; | |
438 | di->size = object_property_get_uint(OBJECT(dimm), PC_DIMM_SIZE_PROP, | |
439 | NULL); | |
440 | di->memdev = object_get_canonical_path(OBJECT(dimm->hostmem)); | |
441 | ||
442 | if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) { | |
443 | info->u.nvdimm.data = di; | |
444 | info->type = MEMORY_DEVICE_INFO_KIND_NVDIMM; | |
445 | } else { | |
446 | info->u.dimm.data = di; | |
447 | info->type = MEMORY_DEVICE_INFO_KIND_DIMM; | |
448 | } | |
449 | } | |
450 | ||
10b7e74b VL |
451 | static void pc_dimm_class_init(ObjectClass *oc, void *data) |
452 | { | |
453 | DeviceClass *dc = DEVICE_CLASS(oc); | |
454 | PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc); | |
2cc0e2e8 | 455 | MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(oc); |
10b7e74b VL |
456 | |
457 | dc->realize = pc_dimm_realize; | |
2aece63c | 458 | dc->unrealize = pc_dimm_unrealize; |
10b7e74b | 459 | dc->props = pc_dimm_properties; |
bdd09778 | 460 | dc->desc = "DIMM memory module"; |
10b7e74b VL |
461 | |
462 | ddc->get_memory_region = pc_dimm_get_memory_region; | |
8df1426e | 463 | ddc->get_vmstate_memory_region = pc_dimm_get_vmstate_memory_region; |
2cc0e2e8 DH |
464 | |
465 | mdc->get_addr = pc_dimm_md_get_addr; | |
466 | /* for a dimm plugged_size == region_size */ | |
467 | mdc->get_plugged_size = pc_dimm_md_get_region_size; | |
468 | mdc->get_region_size = pc_dimm_md_get_region_size; | |
469 | mdc->fill_device_info = pc_dimm_md_fill_device_info; | |
10b7e74b VL |
470 | } |
471 | ||
472 | static TypeInfo pc_dimm_info = { | |
473 | .name = TYPE_PC_DIMM, | |
474 | .parent = TYPE_DEVICE, | |
475 | .instance_size = sizeof(PCDIMMDevice), | |
476 | .instance_init = pc_dimm_init, | |
477 | .class_init = pc_dimm_class_init, | |
478 | .class_size = sizeof(PCDIMMDeviceClass), | |
2cc0e2e8 DH |
479 | .interfaces = (InterfaceInfo[]) { |
480 | { TYPE_MEMORY_DEVICE }, | |
481 | { } | |
482 | }, | |
10b7e74b VL |
483 | }; |
484 | ||
485 | static void pc_dimm_register_types(void) | |
486 | { | |
487 | type_register_static(&pc_dimm_info); | |
488 | } | |
489 | ||
490 | type_init(pc_dimm_register_types) |