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