xref: /openbmc/qemu/hw/mem/pc-dimm.c (revision dd9fe29c)
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"
24 #include "qemu/range.h"
25 
26 typedef struct pc_dimms_capacity {
27      uint64_t size;
28      Error    **errp;
29 } pc_dimms_capacity;
30 
31 static int pc_existing_dimms_capacity_internal(Object *obj, void *opaque)
32 {
33     pc_dimms_capacity *cap = opaque;
34     uint64_t *size = &cap->size;
35 
36     if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
37         DeviceState *dev = DEVICE(obj);
38 
39         if (dev->realized) {
40             (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
41                 cap->errp);
42         }
43 
44         if (cap->errp && *cap->errp) {
45             return 1;
46         }
47     }
48     object_child_foreach(obj, pc_existing_dimms_capacity_internal, opaque);
49     return 0;
50 }
51 
52 uint64_t pc_existing_dimms_capacity(Error **errp)
53 {
54     pc_dimms_capacity cap;
55 
56     cap.size = 0;
57     cap.errp = errp;
58 
59     pc_existing_dimms_capacity_internal(qdev_get_machine(), &cap);
60     return cap.size;
61 }
62 
63 int qmp_pc_dimm_device_list(Object *obj, void *opaque)
64 {
65     MemoryDeviceInfoList ***prev = opaque;
66 
67     if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
68         DeviceState *dev = DEVICE(obj);
69 
70         if (dev->realized) {
71             MemoryDeviceInfoList *elem = g_new0(MemoryDeviceInfoList, 1);
72             MemoryDeviceInfo *info = g_new0(MemoryDeviceInfo, 1);
73             PCDIMMDeviceInfo *di = g_new0(PCDIMMDeviceInfo, 1);
74             DeviceClass *dc = DEVICE_GET_CLASS(obj);
75             PCDIMMDevice *dimm = PC_DIMM(obj);
76 
77             if (dev->id) {
78                 di->has_id = true;
79                 di->id = g_strdup(dev->id);
80             }
81             di->hotplugged = dev->hotplugged;
82             di->hotpluggable = dc->hotpluggable;
83             di->addr = dimm->addr;
84             di->slot = dimm->slot;
85             di->node = dimm->node;
86             di->size = object_property_get_int(OBJECT(dimm), PC_DIMM_SIZE_PROP,
87                                                NULL);
88             di->memdev = object_get_canonical_path(OBJECT(dimm->hostmem));
89 
90             info->dimm = di;
91             elem->value = info;
92             elem->next = NULL;
93             **prev = elem;
94             *prev = &elem->next;
95         }
96     }
97 
98     object_child_foreach(obj, qmp_pc_dimm_device_list, opaque);
99     return 0;
100 }
101 
102 static int pc_dimm_slot2bitmap(Object *obj, void *opaque)
103 {
104     unsigned long *bitmap = opaque;
105 
106     if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
107         DeviceState *dev = DEVICE(obj);
108         if (dev->realized) { /* count only realized DIMMs */
109             PCDIMMDevice *d = PC_DIMM(obj);
110             set_bit(d->slot, bitmap);
111         }
112     }
113 
114     object_child_foreach(obj, pc_dimm_slot2bitmap, opaque);
115     return 0;
116 }
117 
118 int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp)
119 {
120     unsigned long *bitmap = bitmap_new(max_slots);
121     int slot = 0;
122 
123     object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap, bitmap);
124 
125     /* check if requested slot is not occupied */
126     if (hint) {
127         if (*hint >= max_slots) {
128             error_setg(errp, "invalid slot# %d, should be less than %d",
129                        *hint, max_slots);
130         } else if (!test_bit(*hint, bitmap)) {
131             slot = *hint;
132         } else {
133             error_setg(errp, "slot %d is busy", *hint);
134         }
135         goto out;
136     }
137 
138     /* search for free slot */
139     slot = find_first_zero_bit(bitmap, max_slots);
140     if (slot == max_slots) {
141         error_setg(errp, "no free slots available");
142     }
143 out:
144     g_free(bitmap);
145     return slot;
146 }
147 
148 static gint pc_dimm_addr_sort(gconstpointer a, gconstpointer b)
149 {
150     PCDIMMDevice *x = PC_DIMM(a);
151     PCDIMMDevice *y = PC_DIMM(b);
152     Int128 diff = int128_sub(int128_make64(x->addr), int128_make64(y->addr));
153 
154     if (int128_lt(diff, int128_zero())) {
155         return -1;
156     } else if (int128_gt(diff, int128_zero())) {
157         return 1;
158     }
159     return 0;
160 }
161 
162 static int pc_dimm_built_list(Object *obj, void *opaque)
163 {
164     GSList **list = opaque;
165 
166     if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
167         DeviceState *dev = DEVICE(obj);
168         if (dev->realized) { /* only realized DIMMs matter */
169             *list = g_slist_insert_sorted(*list, dev, pc_dimm_addr_sort);
170         }
171     }
172 
173     object_child_foreach(obj, pc_dimm_built_list, opaque);
174     return 0;
175 }
176 
177 uint64_t pc_dimm_get_free_addr(uint64_t address_space_start,
178                                uint64_t address_space_size,
179                                uint64_t *hint, uint64_t align, uint64_t size,
180                                Error **errp)
181 {
182     GSList *list = NULL, *item;
183     uint64_t new_addr, ret = 0;
184     uint64_t address_space_end = address_space_start + address_space_size;
185 
186     g_assert(QEMU_ALIGN_UP(address_space_start, align) == address_space_start);
187     g_assert(QEMU_ALIGN_UP(address_space_size, align) == address_space_size);
188 
189     if (!address_space_size) {
190         error_setg(errp, "memory hotplug is not enabled, "
191                          "please add maxmem option");
192         goto out;
193     }
194 
195     if (hint && QEMU_ALIGN_UP(*hint, align) != *hint) {
196         error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes",
197                    align);
198         goto out;
199     }
200 
201     if (QEMU_ALIGN_UP(size, align) != size) {
202         error_setg(errp, "backend memory size must be multiple of 0x%"
203                    PRIx64, align);
204         goto out;
205     }
206 
207     assert(address_space_end > address_space_start);
208     object_child_foreach(qdev_get_machine(), pc_dimm_built_list, &list);
209 
210     if (hint) {
211         new_addr = *hint;
212     } else {
213         new_addr = address_space_start;
214     }
215 
216     /* find address range that will fit new DIMM */
217     for (item = list; item; item = g_slist_next(item)) {
218         PCDIMMDevice *dimm = item->data;
219         uint64_t dimm_size = object_property_get_int(OBJECT(dimm),
220                                                      PC_DIMM_SIZE_PROP,
221                                                      errp);
222         if (errp && *errp) {
223             goto out;
224         }
225 
226         if (ranges_overlap(dimm->addr, dimm_size, new_addr, size)) {
227             if (hint) {
228                 DeviceState *d = DEVICE(dimm);
229                 error_setg(errp, "address range conflicts with '%s'", d->id);
230                 goto out;
231             }
232             new_addr = QEMU_ALIGN_UP(dimm->addr + dimm_size, align);
233         }
234     }
235     ret = new_addr;
236 
237     if (new_addr < address_space_start) {
238         error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
239                    "] at 0x%" PRIx64, new_addr, size, address_space_start);
240     } else if ((new_addr + size) > address_space_end) {
241         error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
242                    "] beyond 0x%" PRIx64, new_addr, size, address_space_end);
243     }
244 
245 out:
246     g_slist_free(list);
247     return ret;
248 }
249 
250 static Property pc_dimm_properties[] = {
251     DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0),
252     DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0),
253     DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot,
254                       PC_DIMM_UNASSIGNED_SLOT),
255     DEFINE_PROP_END_OF_LIST(),
256 };
257 
258 static void pc_dimm_get_size(Object *obj, Visitor *v, void *opaque,
259                           const char *name, Error **errp)
260 {
261     int64_t value;
262     MemoryRegion *mr;
263     PCDIMMDevice *dimm = PC_DIMM(obj);
264 
265     mr = host_memory_backend_get_memory(dimm->hostmem, errp);
266     value = memory_region_size(mr);
267 
268     visit_type_int(v, &value, name, errp);
269 }
270 
271 static void pc_dimm_check_memdev_is_busy(Object *obj, const char *name,
272                                       Object *val, Error **errp)
273 {
274     MemoryRegion *mr;
275 
276     mr = host_memory_backend_get_memory(MEMORY_BACKEND(val), errp);
277     if (memory_region_is_mapped(mr)) {
278         char *path = object_get_canonical_path_component(val);
279         error_setg(errp, "can't use already busy memdev: %s", path);
280         g_free(path);
281     } else {
282         qdev_prop_allow_set_link_before_realize(obj, name, val, errp);
283     }
284 }
285 
286 static void pc_dimm_init(Object *obj)
287 {
288     PCDIMMDevice *dimm = PC_DIMM(obj);
289 
290     object_property_add(obj, PC_DIMM_SIZE_PROP, "int", pc_dimm_get_size,
291                         NULL, NULL, NULL, &error_abort);
292     object_property_add_link(obj, PC_DIMM_MEMDEV_PROP, TYPE_MEMORY_BACKEND,
293                              (Object **)&dimm->hostmem,
294                              pc_dimm_check_memdev_is_busy,
295                              OBJ_PROP_LINK_UNREF_ON_RELEASE,
296                              &error_abort);
297 }
298 
299 static void pc_dimm_realize(DeviceState *dev, Error **errp)
300 {
301     PCDIMMDevice *dimm = PC_DIMM(dev);
302 
303     if (!dimm->hostmem) {
304         error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set");
305         return;
306     }
307     if ((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) {
308         error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %"
309                    PRIu32 "' which exceeds the number of numa nodes: %d",
310                    dimm->node, nb_numa_nodes);
311         return;
312     }
313 }
314 
315 static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm)
316 {
317     return host_memory_backend_get_memory(dimm->hostmem, &error_abort);
318 }
319 
320 static void pc_dimm_class_init(ObjectClass *oc, void *data)
321 {
322     DeviceClass *dc = DEVICE_CLASS(oc);
323     PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
324 
325     dc->realize = pc_dimm_realize;
326     dc->props = pc_dimm_properties;
327 
328     ddc->get_memory_region = pc_dimm_get_memory_region;
329 }
330 
331 static TypeInfo pc_dimm_info = {
332     .name          = TYPE_PC_DIMM,
333     .parent        = TYPE_DEVICE,
334     .instance_size = sizeof(PCDIMMDevice),
335     .instance_init = pc_dimm_init,
336     .class_init    = pc_dimm_class_init,
337     .class_size    = sizeof(PCDIMMDeviceClass),
338 };
339 
340 static void pc_dimm_register_types(void)
341 {
342     type_register_static(&pc_dimm_info);
343 }
344 
345 type_init(pc_dimm_register_types)
346