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