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