tests: Restore check-qdict unit test
[qemu/armbru.git] / hw / mem / pc-dimm.c
blobfb6bcaedc4747d882c1526a1cb6dbe7c01f0564f
1 /*
2 * Dimm device for Memory Hotplug
4 * Copyright ProfitBricks GmbH 2012
5 * Copyright (C) 2014 Red Hat Inc
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.
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.
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/>
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"
30 static int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp);
32 void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine,
33 const uint64_t *legacy_align, Error **errp)
35 PCDIMMDevice *dimm = PC_DIMM(dev);
36 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
37 Error *local_err = NULL;
38 MemoryRegion *mr;
39 uint64_t addr, align;
40 int slot;
42 slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
43 &error_abort);
44 slot = pc_dimm_get_free_slot(slot == PC_DIMM_UNASSIGNED_SLOT ? NULL : &slot,
45 machine->ram_slots, &local_err);
46 if (local_err) {
47 goto out;
49 object_property_set_int(OBJECT(dev), slot, PC_DIMM_SLOT_PROP, &error_abort);
50 trace_mhp_pc_dimm_assigned_slot(slot);
52 mr = ddc->get_memory_region(dimm, &local_err);
53 if (local_err) {
54 goto out;
57 align = legacy_align ? *legacy_align : memory_region_get_alignment(mr);
58 addr = object_property_get_uint(OBJECT(dev), PC_DIMM_ADDR_PROP,
59 &error_abort);
60 addr = memory_device_get_free_addr(machine, !addr ? NULL : &addr, align,
61 memory_region_size(mr), &local_err);
62 if (local_err) {
63 goto out;
65 trace_mhp_pc_dimm_assigned_address(addr);
66 object_property_set_uint(OBJECT(dev), addr, PC_DIMM_ADDR_PROP,
67 &error_abort);
68 out:
69 error_propagate(errp, local_err);
72 void pc_dimm_plug(DeviceState *dev, MachineState *machine, Error **errp)
74 PCDIMMDevice *dimm = PC_DIMM(dev);
75 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
76 MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm,
77 &error_abort);
78 MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
79 uint64_t addr;
81 addr = object_property_get_uint(OBJECT(dev), PC_DIMM_ADDR_PROP,
82 &error_abort);
84 memory_device_plug_region(machine, mr, addr);
85 vmstate_register_ram(vmstate_mr, dev);
88 void pc_dimm_unplug(DeviceState *dev, MachineState *machine)
90 PCDIMMDevice *dimm = PC_DIMM(dev);
91 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
92 MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm,
93 &error_abort);
94 MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
96 memory_device_unplug_region(machine, mr);
97 vmstate_unregister_ram(vmstate_mr, dev);
100 static int pc_dimm_slot2bitmap(Object *obj, void *opaque)
102 unsigned long *bitmap = opaque;
104 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
105 DeviceState *dev = DEVICE(obj);
106 if (dev->realized) { /* count only realized DIMMs */
107 PCDIMMDevice *d = PC_DIMM(obj);
108 set_bit(d->slot, bitmap);
112 object_child_foreach(obj, pc_dimm_slot2bitmap, opaque);
113 return 0;
116 static int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp)
118 unsigned long *bitmap;
119 int slot = 0;
121 if (max_slots <= 0) {
122 error_setg(errp, "no slots where allocated, please specify "
123 "the 'slots' option");
124 return slot;
127 bitmap = bitmap_new(max_slots);
128 object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap, bitmap);
130 /* check if requested slot is not occupied */
131 if (hint) {
132 if (*hint >= max_slots) {
133 error_setg(errp, "invalid slot# %d, should be less than %d",
134 *hint, max_slots);
135 } else if (!test_bit(*hint, bitmap)) {
136 slot = *hint;
137 } else {
138 error_setg(errp, "slot %d is busy", *hint);
140 goto out;
143 /* search for free slot */
144 slot = find_first_zero_bit(bitmap, max_slots);
145 if (slot == max_slots) {
146 error_setg(errp, "no free slots available");
148 out:
149 g_free(bitmap);
150 return slot;
153 static Property pc_dimm_properties[] = {
154 DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0),
155 DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0),
156 DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot,
157 PC_DIMM_UNASSIGNED_SLOT),
158 DEFINE_PROP_LINK(PC_DIMM_MEMDEV_PROP, PCDIMMDevice, hostmem,
159 TYPE_MEMORY_BACKEND, HostMemoryBackend *),
160 DEFINE_PROP_END_OF_LIST(),
163 static void pc_dimm_get_size(Object *obj, Visitor *v, const char *name,
164 void *opaque, Error **errp)
166 uint64_t value;
167 MemoryRegion *mr;
168 PCDIMMDevice *dimm = PC_DIMM(obj);
169 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(obj);
171 mr = ddc->get_memory_region(dimm, errp);
172 if (!mr) {
173 return;
175 value = memory_region_size(mr);
177 visit_type_uint64(v, name, &value, errp);
180 static void pc_dimm_init(Object *obj)
182 object_property_add(obj, PC_DIMM_SIZE_PROP, "uint64", pc_dimm_get_size,
183 NULL, NULL, NULL, &error_abort);
186 static void pc_dimm_realize(DeviceState *dev, Error **errp)
188 PCDIMMDevice *dimm = PC_DIMM(dev);
189 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
191 if (!dimm->hostmem) {
192 error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set");
193 return;
194 } else if (host_memory_backend_is_mapped(dimm->hostmem)) {
195 char *path = object_get_canonical_path_component(OBJECT(dimm->hostmem));
196 error_setg(errp, "can't use already busy memdev: %s", path);
197 g_free(path);
198 return;
200 if (((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) ||
201 (!nb_numa_nodes && dimm->node)) {
202 error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %"
203 PRIu32 "' which exceeds the number of numa nodes: %d",
204 dimm->node, nb_numa_nodes ? nb_numa_nodes : 1);
205 return;
208 if (ddc->realize) {
209 ddc->realize(dimm, errp);
212 host_memory_backend_set_mapped(dimm->hostmem, true);
215 static void pc_dimm_unrealize(DeviceState *dev, Error **errp)
217 PCDIMMDevice *dimm = PC_DIMM(dev);
219 host_memory_backend_set_mapped(dimm->hostmem, false);
222 static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm, Error **errp)
224 if (!dimm->hostmem) {
225 error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property must be set");
226 return NULL;
229 return host_memory_backend_get_memory(dimm->hostmem);
232 static uint64_t pc_dimm_md_get_addr(const MemoryDeviceState *md)
234 const PCDIMMDevice *dimm = PC_DIMM(md);
236 return dimm->addr;
239 static uint64_t pc_dimm_md_get_region_size(const MemoryDeviceState *md)
241 /* dropping const here is fine as we don't touch the memory region */
242 PCDIMMDevice *dimm = PC_DIMM(md);
243 const PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(md);
244 MemoryRegion *mr;
246 mr = ddc->get_memory_region(dimm, &error_abort);
247 if (!mr) {
248 return 0;
251 return memory_region_size(mr);
254 static void pc_dimm_md_fill_device_info(const MemoryDeviceState *md,
255 MemoryDeviceInfo *info)
257 PCDIMMDeviceInfo *di = g_new0(PCDIMMDeviceInfo, 1);
258 const DeviceClass *dc = DEVICE_GET_CLASS(md);
259 const PCDIMMDevice *dimm = PC_DIMM(md);
260 const DeviceState *dev = DEVICE(md);
262 if (dev->id) {
263 di->has_id = true;
264 di->id = g_strdup(dev->id);
266 di->hotplugged = dev->hotplugged;
267 di->hotpluggable = dc->hotpluggable;
268 di->addr = dimm->addr;
269 di->slot = dimm->slot;
270 di->node = dimm->node;
271 di->size = object_property_get_uint(OBJECT(dimm), PC_DIMM_SIZE_PROP,
272 NULL);
273 di->memdev = object_get_canonical_path(OBJECT(dimm->hostmem));
275 if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
276 info->u.nvdimm.data = di;
277 info->type = MEMORY_DEVICE_INFO_KIND_NVDIMM;
278 } else {
279 info->u.dimm.data = di;
280 info->type = MEMORY_DEVICE_INFO_KIND_DIMM;
284 static void pc_dimm_class_init(ObjectClass *oc, void *data)
286 DeviceClass *dc = DEVICE_CLASS(oc);
287 PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
288 MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(oc);
290 dc->realize = pc_dimm_realize;
291 dc->unrealize = pc_dimm_unrealize;
292 dc->props = pc_dimm_properties;
293 dc->desc = "DIMM memory module";
295 ddc->get_memory_region = pc_dimm_get_memory_region;
296 ddc->get_vmstate_memory_region = pc_dimm_get_memory_region;
298 mdc->get_addr = pc_dimm_md_get_addr;
299 /* for a dimm plugged_size == region_size */
300 mdc->get_plugged_size = pc_dimm_md_get_region_size;
301 mdc->get_region_size = pc_dimm_md_get_region_size;
302 mdc->fill_device_info = pc_dimm_md_fill_device_info;
305 static TypeInfo pc_dimm_info = {
306 .name = TYPE_PC_DIMM,
307 .parent = TYPE_DEVICE,
308 .instance_size = sizeof(PCDIMMDevice),
309 .instance_init = pc_dimm_init,
310 .class_init = pc_dimm_class_init,
311 .class_size = sizeof(PCDIMMDeviceClass),
312 .interfaces = (InterfaceInfo[]) {
313 { TYPE_MEMORY_DEVICE },
318 static void pc_dimm_register_types(void)
320 type_register_static(&pc_dimm_info);
323 type_init(pc_dimm_register_types)