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