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