xref: /openbmc/qemu/hw/mem/pc-dimm.c (revision a37eaa53)
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 
113 out:
114     error_propagate(errp, local_err);
115 }
116 
117 void pc_dimm_memory_unplug(DeviceState *dev, MemoryHotplugState *hpms,
118                            MemoryRegion *mr)
119 {
120     PCDIMMDevice *dimm = PC_DIMM(dev);
121     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
122     MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm);
123 
124     memory_region_del_subregion(&hpms->mr, mr);
125     vmstate_unregister_ram(vmstate_mr, dev);
126 }
127 
128 static int pc_existing_dimms_capacity_internal(Object *obj, void *opaque)
129 {
130     pc_dimms_capacity *cap = opaque;
131     uint64_t *size = &cap->size;
132 
133     if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
134         DeviceState *dev = DEVICE(obj);
135 
136         if (dev->realized) {
137             (*size) += object_property_get_uint(obj, PC_DIMM_SIZE_PROP,
138                 cap->errp);
139         }
140 
141         if (cap->errp && *cap->errp) {
142             return 1;
143         }
144     }
145     object_child_foreach(obj, pc_existing_dimms_capacity_internal, opaque);
146     return 0;
147 }
148 
149 uint64_t pc_existing_dimms_capacity(Error **errp)
150 {
151     pc_dimms_capacity cap;
152 
153     cap.size = 0;
154     cap.errp = errp;
155 
156     pc_existing_dimms_capacity_internal(qdev_get_machine(), &cap);
157     return cap.size;
158 }
159 
160 uint64_t get_plugged_memory_size(void)
161 {
162     return pc_existing_dimms_capacity(&error_abort);
163 }
164 
165 int qmp_pc_dimm_device_list(Object *obj, void *opaque)
166 {
167     MemoryDeviceInfoList ***prev = opaque;
168 
169     if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
170         DeviceState *dev = DEVICE(obj);
171 
172         if (dev->realized) {
173             MemoryDeviceInfoList *elem = g_new0(MemoryDeviceInfoList, 1);
174             MemoryDeviceInfo *info = g_new0(MemoryDeviceInfo, 1);
175             PCDIMMDeviceInfo *di = g_new0(PCDIMMDeviceInfo, 1);
176             DeviceClass *dc = DEVICE_GET_CLASS(obj);
177             PCDIMMDevice *dimm = PC_DIMM(obj);
178 
179             if (dev->id) {
180                 di->has_id = true;
181                 di->id = g_strdup(dev->id);
182             }
183             di->hotplugged = dev->hotplugged;
184             di->hotpluggable = dc->hotpluggable;
185             di->addr = dimm->addr;
186             di->slot = dimm->slot;
187             di->node = dimm->node;
188             di->size = object_property_get_uint(OBJECT(dimm), PC_DIMM_SIZE_PROP,
189                                                 NULL);
190             di->memdev = object_get_canonical_path(OBJECT(dimm->hostmem));
191 
192             info->u.dimm.data = di;
193             elem->value = info;
194             elem->next = NULL;
195             **prev = elem;
196             *prev = &elem->next;
197         }
198     }
199 
200     object_child_foreach(obj, qmp_pc_dimm_device_list, opaque);
201     return 0;
202 }
203 
204 static int pc_dimm_slot2bitmap(Object *obj, void *opaque)
205 {
206     unsigned long *bitmap = opaque;
207 
208     if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
209         DeviceState *dev = DEVICE(obj);
210         if (dev->realized) { /* count only realized DIMMs */
211             PCDIMMDevice *d = PC_DIMM(obj);
212             set_bit(d->slot, bitmap);
213         }
214     }
215 
216     object_child_foreach(obj, pc_dimm_slot2bitmap, opaque);
217     return 0;
218 }
219 
220 int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp)
221 {
222     unsigned long *bitmap = bitmap_new(max_slots);
223     int slot = 0;
224 
225     object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap, bitmap);
226 
227     /* check if requested slot is not occupied */
228     if (hint) {
229         if (*hint >= max_slots) {
230             error_setg(errp, "invalid slot# %d, should be less than %d",
231                        *hint, max_slots);
232         } else if (!test_bit(*hint, bitmap)) {
233             slot = *hint;
234         } else {
235             error_setg(errp, "slot %d is busy", *hint);
236         }
237         goto out;
238     }
239 
240     /* search for free slot */
241     slot = find_first_zero_bit(bitmap, max_slots);
242     if (slot == max_slots) {
243         error_setg(errp, "no free slots available");
244     }
245 out:
246     g_free(bitmap);
247     return slot;
248 }
249 
250 static gint pc_dimm_addr_sort(gconstpointer a, gconstpointer b)
251 {
252     PCDIMMDevice *x = PC_DIMM(a);
253     PCDIMMDevice *y = PC_DIMM(b);
254     Int128 diff = int128_sub(int128_make64(x->addr), int128_make64(y->addr));
255 
256     if (int128_lt(diff, int128_zero())) {
257         return -1;
258     } else if (int128_gt(diff, int128_zero())) {
259         return 1;
260     }
261     return 0;
262 }
263 
264 static int pc_dimm_built_list(Object *obj, void *opaque)
265 {
266     GSList **list = opaque;
267 
268     if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
269         DeviceState *dev = DEVICE(obj);
270         if (dev->realized) { /* only realized DIMMs matter */
271             *list = g_slist_insert_sorted(*list, dev, pc_dimm_addr_sort);
272         }
273     }
274 
275     object_child_foreach(obj, pc_dimm_built_list, opaque);
276     return 0;
277 }
278 
279 uint64_t pc_dimm_get_free_addr(uint64_t address_space_start,
280                                uint64_t address_space_size,
281                                uint64_t *hint, uint64_t align, uint64_t size,
282                                Error **errp)
283 {
284     GSList *list = NULL, *item;
285     uint64_t new_addr, ret = 0;
286     uint64_t address_space_end = address_space_start + address_space_size;
287 
288     g_assert(QEMU_ALIGN_UP(address_space_start, align) == address_space_start);
289 
290     if (!address_space_size) {
291         error_setg(errp, "memory hotplug is not enabled, "
292                          "please add maxmem option");
293         goto out;
294     }
295 
296     if (hint && QEMU_ALIGN_UP(*hint, align) != *hint) {
297         error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes",
298                    align);
299         goto out;
300     }
301 
302     if (QEMU_ALIGN_UP(size, align) != size) {
303         error_setg(errp, "backend memory size must be multiple of 0x%"
304                    PRIx64, align);
305         goto out;
306     }
307 
308     assert(address_space_end > address_space_start);
309     object_child_foreach(qdev_get_machine(), pc_dimm_built_list, &list);
310 
311     if (hint) {
312         new_addr = *hint;
313     } else {
314         new_addr = address_space_start;
315     }
316 
317     /* find address range that will fit new DIMM */
318     for (item = list; item; item = g_slist_next(item)) {
319         PCDIMMDevice *dimm = item->data;
320         uint64_t dimm_size = object_property_get_uint(OBJECT(dimm),
321                                                       PC_DIMM_SIZE_PROP,
322                                                       errp);
323         if (errp && *errp) {
324             goto out;
325         }
326 
327         if (ranges_overlap(dimm->addr, dimm_size, new_addr, size)) {
328             if (hint) {
329                 DeviceState *d = DEVICE(dimm);
330                 error_setg(errp, "address range conflicts with '%s'", d->id);
331                 goto out;
332             }
333             new_addr = QEMU_ALIGN_UP(dimm->addr + dimm_size, align);
334         }
335     }
336     ret = new_addr;
337 
338     if (new_addr < address_space_start) {
339         error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
340                    "] at 0x%" PRIx64, new_addr, size, address_space_start);
341     } else if ((new_addr + size) > address_space_end) {
342         error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
343                    "] beyond 0x%" PRIx64, new_addr, size, address_space_end);
344     }
345 
346 out:
347     g_slist_free(list);
348     return ret;
349 }
350 
351 static Property pc_dimm_properties[] = {
352     DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0),
353     DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0),
354     DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot,
355                       PC_DIMM_UNASSIGNED_SLOT),
356     DEFINE_PROP_LINK(PC_DIMM_MEMDEV_PROP, PCDIMMDevice, hostmem,
357                      TYPE_MEMORY_BACKEND, HostMemoryBackend *),
358     DEFINE_PROP_END_OF_LIST(),
359 };
360 
361 static void pc_dimm_get_size(Object *obj, Visitor *v, const char *name,
362                              void *opaque, Error **errp)
363 {
364     uint64_t value;
365     MemoryRegion *mr;
366     PCDIMMDevice *dimm = PC_DIMM(obj);
367     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(obj);
368 
369     mr = ddc->get_memory_region(dimm, errp);
370     if (!mr) {
371         return;
372     }
373     value = memory_region_size(mr);
374 
375     visit_type_uint64(v, name, &value, errp);
376 }
377 
378 static void pc_dimm_init(Object *obj)
379 {
380     object_property_add(obj, PC_DIMM_SIZE_PROP, "uint64", pc_dimm_get_size,
381                         NULL, NULL, NULL, &error_abort);
382 }
383 
384 static void pc_dimm_realize(DeviceState *dev, Error **errp)
385 {
386     PCDIMMDevice *dimm = PC_DIMM(dev);
387     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
388 
389     if (!dimm->hostmem) {
390         error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set");
391         return;
392     } else if (host_memory_backend_is_mapped(dimm->hostmem)) {
393         char *path = object_get_canonical_path_component(OBJECT(dimm->hostmem));
394         error_setg(errp, "can't use already busy memdev: %s", path);
395         g_free(path);
396         return;
397     }
398     if (((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) ||
399         (!nb_numa_nodes && dimm->node)) {
400         error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %"
401                    PRIu32 "' which exceeds the number of numa nodes: %d",
402                    dimm->node, nb_numa_nodes ? nb_numa_nodes : 1);
403         return;
404     }
405 
406     if (ddc->realize) {
407         ddc->realize(dimm, errp);
408     }
409 
410     host_memory_backend_set_mapped(dimm->hostmem, true);
411 }
412 
413 static void pc_dimm_unrealize(DeviceState *dev, Error **errp)
414 {
415     PCDIMMDevice *dimm = PC_DIMM(dev);
416 
417     host_memory_backend_set_mapped(dimm->hostmem, false);
418 }
419 
420 static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm, Error **errp)
421 {
422     if (!dimm->hostmem) {
423         error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property must be set");
424         return NULL;
425     }
426 
427     return host_memory_backend_get_memory(dimm->hostmem, errp);
428 }
429 
430 static MemoryRegion *pc_dimm_get_vmstate_memory_region(PCDIMMDevice *dimm)
431 {
432     return host_memory_backend_get_memory(dimm->hostmem, &error_abort);
433 }
434 
435 static void pc_dimm_class_init(ObjectClass *oc, void *data)
436 {
437     DeviceClass *dc = DEVICE_CLASS(oc);
438     PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
439 
440     dc->realize = pc_dimm_realize;
441     dc->unrealize = pc_dimm_unrealize;
442     dc->props = pc_dimm_properties;
443     dc->desc = "DIMM memory module";
444 
445     ddc->get_memory_region = pc_dimm_get_memory_region;
446     ddc->get_vmstate_memory_region = pc_dimm_get_vmstate_memory_region;
447 }
448 
449 static TypeInfo pc_dimm_info = {
450     .name          = TYPE_PC_DIMM,
451     .parent        = TYPE_DEVICE,
452     .instance_size = sizeof(PCDIMMDevice),
453     .instance_init = pc_dimm_init,
454     .class_init    = pc_dimm_class_init,
455     .class_size    = sizeof(PCDIMMDeviceClass),
456 };
457 
458 static void pc_dimm_register_types(void)
459 {
460     type_register_static(&pc_dimm_info);
461 }
462 
463 type_init(pc_dimm_register_types)
464