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