xref: /openbmc/qemu/hw/mem/pc-dimm.c (revision 2266d443)
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 "hw/mem/memory-device.h"
25 #include "qapi/error.h"
26 #include "qapi/visitor.h"
27 #include "sysemu/numa.h"
28 #include "trace.h"
29 
30 static int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp);
31 
32 void pc_dimm_plug(DeviceState *dev, MachineState *machine, uint64_t align,
33                   Error **errp)
34 {
35     int slot;
36     PCDIMMDevice *dimm = PC_DIMM(dev);
37     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
38     MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm,
39                                                               &error_abort);
40     MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
41     Error *local_err = NULL;
42     uint64_t addr;
43 
44     addr = object_property_get_uint(OBJECT(dimm),
45                                     PC_DIMM_ADDR_PROP, &local_err);
46     if (local_err) {
47         goto out;
48     }
49 
50     addr = memory_device_get_free_addr(machine, !addr ? NULL : &addr, align,
51                                        memory_region_size(mr), &local_err);
52     if (local_err) {
53         goto out;
54     }
55 
56     object_property_set_uint(OBJECT(dev), addr, PC_DIMM_ADDR_PROP, &local_err);
57     if (local_err) {
58         goto out;
59     }
60     trace_mhp_pc_dimm_assigned_address(addr);
61 
62     slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP, &local_err);
63     if (local_err) {
64         goto out;
65     }
66 
67     slot = pc_dimm_get_free_slot(slot == PC_DIMM_UNASSIGNED_SLOT ? NULL : &slot,
68                                  machine->ram_slots, &local_err);
69     if (local_err) {
70         goto out;
71     }
72     object_property_set_int(OBJECT(dev), slot, PC_DIMM_SLOT_PROP, &local_err);
73     if (local_err) {
74         goto out;
75     }
76     trace_mhp_pc_dimm_assigned_slot(slot);
77 
78     memory_device_plug_region(machine, mr, addr);
79     vmstate_register_ram(vmstate_mr, dev);
80 
81 out:
82     error_propagate(errp, local_err);
83 }
84 
85 void pc_dimm_unplug(DeviceState *dev, MachineState *machine)
86 {
87     PCDIMMDevice *dimm = PC_DIMM(dev);
88     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
89     MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm,
90                                                               &error_abort);
91     MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
92 
93     memory_device_unplug_region(machine, mr);
94     vmstate_unregister_ram(vmstate_mr, dev);
95 }
96 
97 static int pc_dimm_slot2bitmap(Object *obj, void *opaque)
98 {
99     unsigned long *bitmap = opaque;
100 
101     if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
102         DeviceState *dev = DEVICE(obj);
103         if (dev->realized) { /* count only realized DIMMs */
104             PCDIMMDevice *d = PC_DIMM(obj);
105             set_bit(d->slot, bitmap);
106         }
107     }
108 
109     object_child_foreach(obj, pc_dimm_slot2bitmap, opaque);
110     return 0;
111 }
112 
113 static int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp)
114 {
115     unsigned long *bitmap;
116     int slot = 0;
117 
118     if (max_slots <= 0) {
119         error_setg(errp, "no slots where allocated, please specify "
120                    "the 'slots' option");
121         return slot;
122     }
123 
124     bitmap = bitmap_new(max_slots);
125     object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap, bitmap);
126 
127     /* check if requested slot is not occupied */
128     if (hint) {
129         if (*hint >= max_slots) {
130             error_setg(errp, "invalid slot# %d, should be less than %d",
131                        *hint, max_slots);
132         } else if (!test_bit(*hint, bitmap)) {
133             slot = *hint;
134         } else {
135             error_setg(errp, "slot %d is busy", *hint);
136         }
137         goto out;
138     }
139 
140     /* search for free slot */
141     slot = find_first_zero_bit(bitmap, max_slots);
142     if (slot == max_slots) {
143         error_setg(errp, "no free slots available");
144     }
145 out:
146     g_free(bitmap);
147     return slot;
148 }
149 
150 static Property pc_dimm_properties[] = {
151     DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0),
152     DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0),
153     DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot,
154                       PC_DIMM_UNASSIGNED_SLOT),
155     DEFINE_PROP_LINK(PC_DIMM_MEMDEV_PROP, PCDIMMDevice, hostmem,
156                      TYPE_MEMORY_BACKEND, HostMemoryBackend *),
157     DEFINE_PROP_END_OF_LIST(),
158 };
159 
160 static void pc_dimm_get_size(Object *obj, Visitor *v, const char *name,
161                              void *opaque, Error **errp)
162 {
163     uint64_t value;
164     MemoryRegion *mr;
165     PCDIMMDevice *dimm = PC_DIMM(obj);
166     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(obj);
167 
168     mr = ddc->get_memory_region(dimm, errp);
169     if (!mr) {
170         return;
171     }
172     value = memory_region_size(mr);
173 
174     visit_type_uint64(v, name, &value, errp);
175 }
176 
177 static void pc_dimm_init(Object *obj)
178 {
179     object_property_add(obj, PC_DIMM_SIZE_PROP, "uint64", pc_dimm_get_size,
180                         NULL, NULL, NULL, &error_abort);
181 }
182 
183 static void pc_dimm_realize(DeviceState *dev, Error **errp)
184 {
185     PCDIMMDevice *dimm = PC_DIMM(dev);
186     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
187 
188     if (!dimm->hostmem) {
189         error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set");
190         return;
191     } else if (host_memory_backend_is_mapped(dimm->hostmem)) {
192         char *path = object_get_canonical_path_component(OBJECT(dimm->hostmem));
193         error_setg(errp, "can't use already busy memdev: %s", path);
194         g_free(path);
195         return;
196     }
197     if (((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) ||
198         (!nb_numa_nodes && dimm->node)) {
199         error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %"
200                    PRIu32 "' which exceeds the number of numa nodes: %d",
201                    dimm->node, nb_numa_nodes ? nb_numa_nodes : 1);
202         return;
203     }
204 
205     if (ddc->realize) {
206         ddc->realize(dimm, errp);
207     }
208 
209     host_memory_backend_set_mapped(dimm->hostmem, true);
210 }
211 
212 static void pc_dimm_unrealize(DeviceState *dev, Error **errp)
213 {
214     PCDIMMDevice *dimm = PC_DIMM(dev);
215 
216     host_memory_backend_set_mapped(dimm->hostmem, false);
217 }
218 
219 static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm, Error **errp)
220 {
221     if (!dimm->hostmem) {
222         error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property must be set");
223         return NULL;
224     }
225 
226     return host_memory_backend_get_memory(dimm->hostmem);
227 }
228 
229 static uint64_t pc_dimm_md_get_addr(const MemoryDeviceState *md)
230 {
231     const PCDIMMDevice *dimm = PC_DIMM(md);
232 
233     return dimm->addr;
234 }
235 
236 static uint64_t pc_dimm_md_get_region_size(const MemoryDeviceState *md)
237 {
238     /* dropping const here is fine as we don't touch the memory region */
239     PCDIMMDevice *dimm = PC_DIMM(md);
240     const PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(md);
241     MemoryRegion *mr;
242 
243     mr = ddc->get_memory_region(dimm, &error_abort);
244     if (!mr) {
245         return 0;
246     }
247 
248     return memory_region_size(mr);
249 }
250 
251 static void pc_dimm_md_fill_device_info(const MemoryDeviceState *md,
252                                         MemoryDeviceInfo *info)
253 {
254     PCDIMMDeviceInfo *di = g_new0(PCDIMMDeviceInfo, 1);
255     const DeviceClass *dc = DEVICE_GET_CLASS(md);
256     const PCDIMMDevice *dimm = PC_DIMM(md);
257     const DeviceState *dev = DEVICE(md);
258 
259     if (dev->id) {
260         di->has_id = true;
261         di->id = g_strdup(dev->id);
262     }
263     di->hotplugged = dev->hotplugged;
264     di->hotpluggable = dc->hotpluggable;
265     di->addr = dimm->addr;
266     di->slot = dimm->slot;
267     di->node = dimm->node;
268     di->size = object_property_get_uint(OBJECT(dimm), PC_DIMM_SIZE_PROP,
269                                         NULL);
270     di->memdev = object_get_canonical_path(OBJECT(dimm->hostmem));
271 
272     if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
273         info->u.nvdimm.data = di;
274         info->type = MEMORY_DEVICE_INFO_KIND_NVDIMM;
275     } else {
276         info->u.dimm.data = di;
277         info->type = MEMORY_DEVICE_INFO_KIND_DIMM;
278     }
279 }
280 
281 static void pc_dimm_class_init(ObjectClass *oc, void *data)
282 {
283     DeviceClass *dc = DEVICE_CLASS(oc);
284     PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
285     MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(oc);
286 
287     dc->realize = pc_dimm_realize;
288     dc->unrealize = pc_dimm_unrealize;
289     dc->props = pc_dimm_properties;
290     dc->desc = "DIMM memory module";
291 
292     ddc->get_memory_region = pc_dimm_get_memory_region;
293     ddc->get_vmstate_memory_region = pc_dimm_get_memory_region;
294 
295     mdc->get_addr = pc_dimm_md_get_addr;
296     /* for a dimm plugged_size == region_size */
297     mdc->get_plugged_size = pc_dimm_md_get_region_size;
298     mdc->get_region_size = pc_dimm_md_get_region_size;
299     mdc->fill_device_info = pc_dimm_md_fill_device_info;
300 }
301 
302 static TypeInfo pc_dimm_info = {
303     .name          = TYPE_PC_DIMM,
304     .parent        = TYPE_DEVICE,
305     .instance_size = sizeof(PCDIMMDevice),
306     .instance_init = pc_dimm_init,
307     .class_init    = pc_dimm_class_init,
308     .class_size    = sizeof(PCDIMMDeviceClass),
309     .interfaces = (InterfaceInfo[]) {
310         { TYPE_MEMORY_DEVICE },
311         { }
312     },
313 };
314 
315 static void pc_dimm_register_types(void)
316 {
317     type_register_static(&pc_dimm_info);
318 }
319 
320 type_init(pc_dimm_register_types)
321