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