xref: /openbmc/qemu/hw/mem/pc-dimm.c (revision bfb27e60)
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 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     if (!address_space_size) {
150         error_setg(errp, "memory hotplug is not enabled, "
151                          "please add maxmem option");
152         goto out;
153     }
154 
155     assert(address_space_end > address_space_start);
156     object_child_foreach(qdev_get_machine(), pc_dimm_built_list, &list);
157 
158     if (hint) {
159         new_addr = *hint;
160     } else {
161         new_addr = address_space_start;
162     }
163 
164     /* find address range that will fit new DIMM */
165     for (item = list; item; item = g_slist_next(item)) {
166         PCDIMMDevice *dimm = item->data;
167         uint64_t dimm_size = object_property_get_int(OBJECT(dimm),
168                                                      PC_DIMM_SIZE_PROP,
169                                                      errp);
170         if (errp && *errp) {
171             goto out;
172         }
173 
174         if (ranges_overlap(dimm->addr, dimm_size, new_addr, size)) {
175             if (hint) {
176                 DeviceState *d = DEVICE(dimm);
177                 error_setg(errp, "address range conflicts with '%s'", d->id);
178                 goto out;
179             }
180             new_addr = dimm->addr + dimm_size;
181         }
182     }
183     ret = new_addr;
184 
185     if (new_addr < address_space_start) {
186         error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
187                    "] at 0x%" PRIx64, new_addr, size, address_space_start);
188     } else if ((new_addr + size) > address_space_end) {
189         error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
190                    "] beyond 0x%" PRIx64, new_addr, size, address_space_end);
191     }
192 
193 out:
194     g_slist_free(list);
195     return ret;
196 }
197 
198 static Property pc_dimm_properties[] = {
199     DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0),
200     DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0),
201     DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot,
202                       PC_DIMM_UNASSIGNED_SLOT),
203     DEFINE_PROP_END_OF_LIST(),
204 };
205 
206 static void pc_dimm_get_size(Object *obj, Visitor *v, void *opaque,
207                           const char *name, Error **errp)
208 {
209     int64_t value;
210     MemoryRegion *mr;
211     PCDIMMDevice *dimm = PC_DIMM(obj);
212 
213     mr = host_memory_backend_get_memory(dimm->hostmem, errp);
214     value = memory_region_size(mr);
215 
216     visit_type_int(v, &value, name, errp);
217 }
218 
219 static void pc_dimm_check_memdev_is_busy(Object *obj, const char *name,
220                                       Object *val, Error **errp)
221 {
222     MemoryRegion *mr;
223 
224     mr = host_memory_backend_get_memory(MEMORY_BACKEND(val), errp);
225     if (memory_region_is_mapped(mr)) {
226         char *path = object_get_canonical_path_component(val);
227         error_setg(errp, "can't use already busy memdev: %s", path);
228         g_free(path);
229     } else {
230         qdev_prop_allow_set_link_before_realize(obj, name, val, errp);
231     }
232 }
233 
234 static void pc_dimm_init(Object *obj)
235 {
236     PCDIMMDevice *dimm = PC_DIMM(obj);
237 
238     object_property_add(obj, PC_DIMM_SIZE_PROP, "int", pc_dimm_get_size,
239                         NULL, NULL, NULL, &error_abort);
240     object_property_add_link(obj, PC_DIMM_MEMDEV_PROP, TYPE_MEMORY_BACKEND,
241                              (Object **)&dimm->hostmem,
242                              pc_dimm_check_memdev_is_busy,
243                              OBJ_PROP_LINK_UNREF_ON_RELEASE,
244                              &error_abort);
245 }
246 
247 static void pc_dimm_realize(DeviceState *dev, Error **errp)
248 {
249     PCDIMMDevice *dimm = PC_DIMM(dev);
250 
251     if (!dimm->hostmem) {
252         error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set");
253         return;
254     }
255     if (dimm->node >= nb_numa_nodes) {
256         error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %"
257                    PRIu32 "' which exceeds the number of numa nodes: %d",
258                    dimm->node, nb_numa_nodes);
259         return;
260     }
261 }
262 
263 static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm)
264 {
265     return host_memory_backend_get_memory(dimm->hostmem, &error_abort);
266 }
267 
268 static void pc_dimm_class_init(ObjectClass *oc, void *data)
269 {
270     DeviceClass *dc = DEVICE_CLASS(oc);
271     PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
272 
273     dc->realize = pc_dimm_realize;
274     dc->props = pc_dimm_properties;
275 
276     ddc->get_memory_region = pc_dimm_get_memory_region;
277 }
278 
279 static TypeInfo pc_dimm_info = {
280     .name          = TYPE_PC_DIMM,
281     .parent        = TYPE_DEVICE,
282     .instance_size = sizeof(PCDIMMDevice),
283     .instance_init = pc_dimm_init,
284     .class_init    = pc_dimm_class_init,
285     .class_size    = sizeof(PCDIMMDeviceClass),
286 };
287 
288 static void pc_dimm_register_types(void)
289 {
290     type_register_static(&pc_dimm_info);
291 }
292 
293 type_init(pc_dimm_register_types)
294