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