]>
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" |
da34e65c | 23 | #include "qapi/error.h" |
10b7e74b VL |
24 | #include "qemu/config-file.h" |
25 | #include "qapi/visitor.h" | |
0b312571 | 26 | #include "qemu/range.h" |
e35704ba | 27 | #include "sysemu/numa.h" |
43bbb49e BR |
28 | #include "sysemu/kvm.h" |
29 | #include "trace.h" | |
3fad8788 | 30 | #include "hw/virtio/vhost.h" |
9967c949 | 31 | |
37153450 BR |
32 | typedef struct pc_dimms_capacity { |
33 | uint64_t size; | |
34 | Error **errp; | |
35 | } pc_dimms_capacity; | |
36 | ||
43bbb49e | 37 | void pc_dimm_memory_plug(DeviceState *dev, MemoryHotplugState *hpms, |
d6a9b0b8 | 38 | MemoryRegion *mr, uint64_t align, Error **errp) |
43bbb49e BR |
39 | { |
40 | int slot; | |
41 | MachineState *machine = MACHINE(qdev_get_machine()); | |
42 | PCDIMMDevice *dimm = PC_DIMM(dev); | |
43 | Error *local_err = NULL; | |
44 | uint64_t existing_dimms_capacity = 0; | |
45 | uint64_t addr; | |
46 | ||
47 | addr = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP, &local_err); | |
48 | if (local_err) { | |
49 | goto out; | |
50 | } | |
51 | ||
52 | addr = pc_dimm_get_free_addr(hpms->base, | |
53 | memory_region_size(&hpms->mr), | |
d6a9b0b8 | 54 | !addr ? NULL : &addr, align, |
43bbb49e BR |
55 | memory_region_size(mr), &local_err); |
56 | if (local_err) { | |
57 | goto out; | |
58 | } | |
59 | ||
60 | existing_dimms_capacity = pc_existing_dimms_capacity(&local_err); | |
61 | if (local_err) { | |
62 | goto out; | |
63 | } | |
64 | ||
65 | if (existing_dimms_capacity + memory_region_size(mr) > | |
66 | machine->maxram_size - machine->ram_size) { | |
67 | error_setg(&local_err, "not enough space, currently 0x%" PRIx64 | |
68 | " in use of total hot pluggable 0x" RAM_ADDR_FMT, | |
69 | existing_dimms_capacity, | |
70 | machine->maxram_size - machine->ram_size); | |
71 | goto out; | |
72 | } | |
73 | ||
74 | object_property_set_int(OBJECT(dev), addr, PC_DIMM_ADDR_PROP, &local_err); | |
75 | if (local_err) { | |
76 | goto out; | |
77 | } | |
78 | trace_mhp_pc_dimm_assigned_address(addr); | |
79 | ||
80 | slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP, &local_err); | |
81 | if (local_err) { | |
82 | goto out; | |
83 | } | |
84 | ||
85 | slot = pc_dimm_get_free_slot(slot == PC_DIMM_UNASSIGNED_SLOT ? NULL : &slot, | |
86 | machine->ram_slots, &local_err); | |
87 | if (local_err) { | |
88 | goto out; | |
89 | } | |
90 | object_property_set_int(OBJECT(dev), slot, PC_DIMM_SLOT_PROP, &local_err); | |
91 | if (local_err) { | |
92 | goto out; | |
93 | } | |
94 | trace_mhp_pc_dimm_assigned_slot(slot); | |
95 | ||
96 | if (kvm_enabled() && !kvm_has_free_slot(machine)) { | |
97 | error_setg(&local_err, "hypervisor has no free memory slots left"); | |
98 | goto out; | |
99 | } | |
100 | ||
3fad8788 IM |
101 | if (!vhost_has_free_slot()) { |
102 | error_setg(&local_err, "a used vhost backend has no free" | |
103 | " memory slots left"); | |
104 | goto out; | |
105 | } | |
106 | ||
43bbb49e BR |
107 | memory_region_add_subregion(&hpms->mr, addr - hpms->base, mr); |
108 | vmstate_register_ram(mr, dev); | |
fa9ea81d | 109 | numa_set_mem_node_id(addr, memory_region_size(mr), dimm->node); |
43bbb49e BR |
110 | |
111 | out: | |
112 | error_propagate(errp, local_err); | |
113 | } | |
114 | ||
115 | void pc_dimm_memory_unplug(DeviceState *dev, MemoryHotplugState *hpms, | |
116 | MemoryRegion *mr) | |
117 | { | |
fa9ea81d BR |
118 | PCDIMMDevice *dimm = PC_DIMM(dev); |
119 | ||
120 | numa_unset_mem_node_id(dimm->addr, memory_region_size(mr), dimm->node); | |
43bbb49e BR |
121 | memory_region_del_subregion(&hpms->mr, mr); |
122 | vmstate_unregister_ram(mr, dev); | |
123 | } | |
124 | ||
37153450 | 125 | static int pc_existing_dimms_capacity_internal(Object *obj, void *opaque) |
9967c949 | 126 | { |
37153450 BR |
127 | pc_dimms_capacity *cap = opaque; |
128 | uint64_t *size = &cap->size; | |
9967c949 BR |
129 | |
130 | if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { | |
131 | DeviceState *dev = DEVICE(obj); | |
132 | ||
133 | if (dev->realized) { | |
134 | (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP, | |
37153450 | 135 | cap->errp); |
9967c949 BR |
136 | } |
137 | ||
37153450 | 138 | if (cap->errp && *cap->errp) { |
9967c949 BR |
139 | return 1; |
140 | } | |
141 | } | |
37153450 | 142 | object_child_foreach(obj, pc_existing_dimms_capacity_internal, opaque); |
9967c949 BR |
143 | return 0; |
144 | } | |
0b312571 | 145 | |
37153450 BR |
146 | uint64_t pc_existing_dimms_capacity(Error **errp) |
147 | { | |
148 | pc_dimms_capacity cap; | |
149 | ||
150 | cap.size = 0; | |
151 | cap.errp = errp; | |
152 | ||
153 | pc_existing_dimms_capacity_internal(qdev_get_machine(), &cap); | |
154 | return cap.size; | |
155 | } | |
156 | ||
6f2e2730 IM |
157 | int qmp_pc_dimm_device_list(Object *obj, void *opaque) |
158 | { | |
159 | MemoryDeviceInfoList ***prev = opaque; | |
160 | ||
161 | if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { | |
162 | DeviceState *dev = DEVICE(obj); | |
163 | ||
164 | if (dev->realized) { | |
165 | MemoryDeviceInfoList *elem = g_new0(MemoryDeviceInfoList, 1); | |
166 | MemoryDeviceInfo *info = g_new0(MemoryDeviceInfo, 1); | |
167 | PCDIMMDeviceInfo *di = g_new0(PCDIMMDeviceInfo, 1); | |
168 | DeviceClass *dc = DEVICE_GET_CLASS(obj); | |
169 | PCDIMMDevice *dimm = PC_DIMM(obj); | |
170 | ||
171 | if (dev->id) { | |
172 | di->has_id = true; | |
173 | di->id = g_strdup(dev->id); | |
174 | } | |
175 | di->hotplugged = dev->hotplugged; | |
176 | di->hotpluggable = dc->hotpluggable; | |
177 | di->addr = dimm->addr; | |
178 | di->slot = dimm->slot; | |
179 | di->node = dimm->node; | |
180 | di->size = object_property_get_int(OBJECT(dimm), PC_DIMM_SIZE_PROP, | |
181 | NULL); | |
182 | di->memdev = object_get_canonical_path(OBJECT(dimm->hostmem)); | |
183 | ||
32bafa8f | 184 | info->u.dimm.data = di; |
6f2e2730 IM |
185 | elem->value = info; |
186 | elem->next = NULL; | |
187 | **prev = elem; | |
188 | *prev = &elem->next; | |
189 | } | |
190 | } | |
191 | ||
192 | object_child_foreach(obj, qmp_pc_dimm_device_list, opaque); | |
193 | return 0; | |
194 | } | |
195 | ||
0cd03d89 IM |
196 | static int pc_dimm_slot2bitmap(Object *obj, void *opaque) |
197 | { | |
198 | unsigned long *bitmap = opaque; | |
199 | ||
200 | if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { | |
201 | DeviceState *dev = DEVICE(obj); | |
202 | if (dev->realized) { /* count only realized DIMMs */ | |
203 | PCDIMMDevice *d = PC_DIMM(obj); | |
204 | set_bit(d->slot, bitmap); | |
205 | } | |
206 | } | |
207 | ||
208 | object_child_foreach(obj, pc_dimm_slot2bitmap, opaque); | |
209 | return 0; | |
210 | } | |
211 | ||
212 | int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp) | |
213 | { | |
214 | unsigned long *bitmap = bitmap_new(max_slots); | |
215 | int slot = 0; | |
216 | ||
217 | object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap, bitmap); | |
218 | ||
219 | /* check if requested slot is not occupied */ | |
220 | if (hint) { | |
221 | if (*hint >= max_slots) { | |
222 | error_setg(errp, "invalid slot# %d, should be less than %d", | |
223 | *hint, max_slots); | |
224 | } else if (!test_bit(*hint, bitmap)) { | |
225 | slot = *hint; | |
226 | } else { | |
227 | error_setg(errp, "slot %d is busy", *hint); | |
228 | } | |
229 | goto out; | |
230 | } | |
231 | ||
232 | /* search for free slot */ | |
233 | slot = find_first_zero_bit(bitmap, max_slots); | |
234 | if (slot == max_slots) { | |
235 | error_setg(errp, "no free slots available"); | |
236 | } | |
237 | out: | |
238 | g_free(bitmap); | |
239 | return slot; | |
240 | } | |
241 | ||
0b312571 IM |
242 | static gint pc_dimm_addr_sort(gconstpointer a, gconstpointer b) |
243 | { | |
244 | PCDIMMDevice *x = PC_DIMM(a); | |
245 | PCDIMMDevice *y = PC_DIMM(b); | |
246 | Int128 diff = int128_sub(int128_make64(x->addr), int128_make64(y->addr)); | |
247 | ||
248 | if (int128_lt(diff, int128_zero())) { | |
249 | return -1; | |
250 | } else if (int128_gt(diff, int128_zero())) { | |
251 | return 1; | |
252 | } | |
253 | return 0; | |
254 | } | |
255 | ||
256 | static int pc_dimm_built_list(Object *obj, void *opaque) | |
257 | { | |
258 | GSList **list = opaque; | |
259 | ||
260 | if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { | |
261 | DeviceState *dev = DEVICE(obj); | |
262 | if (dev->realized) { /* only realized DIMMs matter */ | |
263 | *list = g_slist_insert_sorted(*list, dev, pc_dimm_addr_sort); | |
264 | } | |
265 | } | |
266 | ||
267 | object_child_foreach(obj, pc_dimm_built_list, opaque); | |
268 | return 0; | |
269 | } | |
270 | ||
271 | uint64_t pc_dimm_get_free_addr(uint64_t address_space_start, | |
272 | uint64_t address_space_size, | |
d6a9b0b8 MT |
273 | uint64_t *hint, uint64_t align, uint64_t size, |
274 | Error **errp) | |
0b312571 IM |
275 | { |
276 | GSList *list = NULL, *item; | |
277 | uint64_t new_addr, ret = 0; | |
278 | uint64_t address_space_end = address_space_start + address_space_size; | |
279 | ||
0c0de1b6 | 280 | g_assert(QEMU_ALIGN_UP(address_space_start, align) == address_space_start); |
0c0de1b6 | 281 | |
9b79a76c IM |
282 | if (!address_space_size) { |
283 | error_setg(errp, "memory hotplug is not enabled, " | |
284 | "please add maxmem option"); | |
285 | goto out; | |
286 | } | |
287 | ||
92a37a04 IM |
288 | if (hint && QEMU_ALIGN_UP(*hint, align) != *hint) { |
289 | error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes", | |
290 | align); | |
291 | goto out; | |
292 | } | |
293 | ||
294 | if (QEMU_ALIGN_UP(size, align) != size) { | |
295 | error_setg(errp, "backend memory size must be multiple of 0x%" | |
296 | PRIx64, align); | |
297 | goto out; | |
298 | } | |
299 | ||
9b79a76c | 300 | assert(address_space_end > address_space_start); |
0b312571 IM |
301 | object_child_foreach(qdev_get_machine(), pc_dimm_built_list, &list); |
302 | ||
303 | if (hint) { | |
304 | new_addr = *hint; | |
305 | } else { | |
306 | new_addr = address_space_start; | |
307 | } | |
308 | ||
309 | /* find address range that will fit new DIMM */ | |
310 | for (item = list; item; item = g_slist_next(item)) { | |
311 | PCDIMMDevice *dimm = item->data; | |
312 | uint64_t dimm_size = object_property_get_int(OBJECT(dimm), | |
313 | PC_DIMM_SIZE_PROP, | |
314 | errp); | |
315 | if (errp && *errp) { | |
316 | goto out; | |
317 | } | |
318 | ||
d6a9b0b8 | 319 | if (ranges_overlap(dimm->addr, dimm_size, new_addr, size)) { |
0b312571 IM |
320 | if (hint) { |
321 | DeviceState *d = DEVICE(dimm); | |
322 | error_setg(errp, "address range conflicts with '%s'", d->id); | |
323 | goto out; | |
324 | } | |
d6a9b0b8 | 325 | new_addr = QEMU_ALIGN_UP(dimm->addr + dimm_size, align); |
0b312571 IM |
326 | } |
327 | } | |
328 | ret = new_addr; | |
329 | ||
330 | if (new_addr < address_space_start) { | |
331 | error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64 | |
332 | "] at 0x%" PRIx64, new_addr, size, address_space_start); | |
333 | } else if ((new_addr + size) > address_space_end) { | |
334 | error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64 | |
335 | "] beyond 0x%" PRIx64, new_addr, size, address_space_end); | |
336 | } | |
337 | ||
338 | out: | |
339 | g_slist_free(list); | |
340 | return ret; | |
341 | } | |
10b7e74b VL |
342 | |
343 | static Property pc_dimm_properties[] = { | |
344 | DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0), | |
345 | DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0), | |
346 | DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot, | |
347 | PC_DIMM_UNASSIGNED_SLOT), | |
348 | DEFINE_PROP_END_OF_LIST(), | |
349 | }; | |
350 | ||
d7bce999 EB |
351 | static void pc_dimm_get_size(Object *obj, Visitor *v, const char *name, |
352 | void *opaque, Error **errp) | |
10b7e74b VL |
353 | { |
354 | int64_t value; | |
355 | MemoryRegion *mr; | |
356 | PCDIMMDevice *dimm = PC_DIMM(obj); | |
357 | ||
358 | mr = host_memory_backend_get_memory(dimm->hostmem, errp); | |
359 | value = memory_region_size(mr); | |
360 | ||
51e72bc1 | 361 | visit_type_int(v, name, &value, errp); |
10b7e74b VL |
362 | } |
363 | ||
7bb5d6ad IM |
364 | static void pc_dimm_check_memdev_is_busy(Object *obj, const char *name, |
365 | Object *val, Error **errp) | |
366 | { | |
367 | MemoryRegion *mr; | |
c9f4b77a | 368 | Error *local_err = NULL; |
7bb5d6ad | 369 | |
c9f4b77a IM |
370 | mr = host_memory_backend_get_memory(MEMORY_BACKEND(val), &local_err); |
371 | if (local_err) { | |
372 | goto out; | |
373 | } | |
7bb5d6ad IM |
374 | if (memory_region_is_mapped(mr)) { |
375 | char *path = object_get_canonical_path_component(val); | |
c9f4b77a | 376 | error_setg(&local_err, "can't use already busy memdev: %s", path); |
7bb5d6ad IM |
377 | g_free(path); |
378 | } else { | |
c9f4b77a | 379 | qdev_prop_allow_set_link_before_realize(obj, name, val, &local_err); |
7bb5d6ad | 380 | } |
c9f4b77a IM |
381 | |
382 | out: | |
383 | error_propagate(errp, local_err); | |
7bb5d6ad IM |
384 | } |
385 | ||
10b7e74b VL |
386 | static void pc_dimm_init(Object *obj) |
387 | { | |
388 | PCDIMMDevice *dimm = PC_DIMM(obj); | |
389 | ||
390 | object_property_add(obj, PC_DIMM_SIZE_PROP, "int", pc_dimm_get_size, | |
391 | NULL, NULL, NULL, &error_abort); | |
392 | object_property_add_link(obj, PC_DIMM_MEMDEV_PROP, TYPE_MEMORY_BACKEND, | |
393 | (Object **)&dimm->hostmem, | |
7bb5d6ad | 394 | pc_dimm_check_memdev_is_busy, |
10b7e74b VL |
395 | OBJ_PROP_LINK_UNREF_ON_RELEASE, |
396 | &error_abort); | |
397 | } | |
398 | ||
399 | static void pc_dimm_realize(DeviceState *dev, Error **errp) | |
400 | { | |
401 | PCDIMMDevice *dimm = PC_DIMM(dev); | |
402 | ||
403 | if (!dimm->hostmem) { | |
404 | error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set"); | |
405 | return; | |
406 | } | |
32532f21 BR |
407 | if (((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) || |
408 | (!nb_numa_nodes && dimm->node)) { | |
988eba0f MT |
409 | error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %" |
410 | PRIu32 "' which exceeds the number of numa nodes: %d", | |
32532f21 | 411 | dimm->node, nb_numa_nodes ? nb_numa_nodes : 1); |
cfe0ffd0 HT |
412 | return; |
413 | } | |
10b7e74b VL |
414 | } |
415 | ||
416 | static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm) | |
417 | { | |
418 | return host_memory_backend_get_memory(dimm->hostmem, &error_abort); | |
419 | } | |
420 | ||
421 | static void pc_dimm_class_init(ObjectClass *oc, void *data) | |
422 | { | |
423 | DeviceClass *dc = DEVICE_CLASS(oc); | |
424 | PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc); | |
425 | ||
426 | dc->realize = pc_dimm_realize; | |
427 | dc->props = pc_dimm_properties; | |
bdd09778 | 428 | dc->desc = "DIMM memory module"; |
10b7e74b VL |
429 | |
430 | ddc->get_memory_region = pc_dimm_get_memory_region; | |
431 | } | |
432 | ||
433 | static TypeInfo pc_dimm_info = { | |
434 | .name = TYPE_PC_DIMM, | |
435 | .parent = TYPE_DEVICE, | |
436 | .instance_size = sizeof(PCDIMMDevice), | |
437 | .instance_init = pc_dimm_init, | |
438 | .class_init = pc_dimm_class_init, | |
439 | .class_size = sizeof(PCDIMMDeviceClass), | |
440 | }; | |
441 | ||
442 | static void pc_dimm_register_types(void) | |
443 | { | |
444 | type_register_static(&pc_dimm_info); | |
445 | } | |
446 | ||
447 | type_init(pc_dimm_register_types) |