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"
30 void pc_dimm_plug(DeviceState
*dev
, MachineState
*machine
, uint64_t align
,
34 PCDIMMDevice
*dimm
= PC_DIMM(dev
);
35 PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(dimm
);
36 MemoryRegion
*vmstate_mr
= ddc
->get_vmstate_memory_region(dimm
);
37 Error
*local_err
= NULL
;
41 mr
= ddc
->get_memory_region(dimm
, &local_err
);
46 addr
= object_property_get_uint(OBJECT(dimm
),
47 PC_DIMM_ADDR_PROP
, &local_err
);
52 addr
= memory_device_get_free_addr(machine
, !addr
? NULL
: &addr
, align
,
53 memory_region_size(mr
), &local_err
);
58 object_property_set_uint(OBJECT(dev
), addr
, PC_DIMM_ADDR_PROP
, &local_err
);
62 trace_mhp_pc_dimm_assigned_address(addr
);
64 slot
= object_property_get_int(OBJECT(dev
), PC_DIMM_SLOT_PROP
, &local_err
);
69 slot
= pc_dimm_get_free_slot(slot
== PC_DIMM_UNASSIGNED_SLOT
? NULL
: &slot
,
70 machine
->ram_slots
, &local_err
);
74 object_property_set_int(OBJECT(dev
), slot
, PC_DIMM_SLOT_PROP
, &local_err
);
78 trace_mhp_pc_dimm_assigned_slot(slot
);
80 memory_device_plug_region(machine
, mr
, addr
);
81 vmstate_register_ram(vmstate_mr
, dev
);
84 error_propagate(errp
, local_err
);
87 void pc_dimm_unplug(DeviceState
*dev
, MachineState
*machine
)
89 PCDIMMDevice
*dimm
= PC_DIMM(dev
);
90 PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(dimm
);
91 MemoryRegion
*vmstate_mr
= ddc
->get_vmstate_memory_region(dimm
);
92 MemoryRegion
*mr
= ddc
->get_memory_region(dimm
, &error_abort
);
94 memory_device_unplug_region(machine
, mr
);
95 vmstate_unregister_ram(vmstate_mr
, dev
);
98 static int pc_dimm_slot2bitmap(Object
*obj
, void *opaque
)
100 unsigned long *bitmap
= opaque
;
102 if (object_dynamic_cast(obj
, TYPE_PC_DIMM
)) {
103 DeviceState
*dev
= DEVICE(obj
);
104 if (dev
->realized
) { /* count only realized DIMMs */
105 PCDIMMDevice
*d
= PC_DIMM(obj
);
106 set_bit(d
->slot
, bitmap
);
110 object_child_foreach(obj
, pc_dimm_slot2bitmap
, opaque
);
114 int pc_dimm_get_free_slot(const int *hint
, int max_slots
, Error
**errp
)
116 unsigned long *bitmap
;
119 if (max_slots
<= 0) {
120 error_setg(errp
, "no slots where allocated, please specify "
121 "the 'slots' option");
125 bitmap
= bitmap_new(max_slots
);
126 object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap
, bitmap
);
128 /* check if requested slot is not occupied */
130 if (*hint
>= max_slots
) {
131 error_setg(errp
, "invalid slot# %d, should be less than %d",
133 } else if (!test_bit(*hint
, bitmap
)) {
136 error_setg(errp
, "slot %d is busy", *hint
);
141 /* search for free slot */
142 slot
= find_first_zero_bit(bitmap
, max_slots
);
143 if (slot
== max_slots
) {
144 error_setg(errp
, "no free slots available");
151 static Property pc_dimm_properties
[] = {
152 DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP
, PCDIMMDevice
, addr
, 0),
153 DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP
, PCDIMMDevice
, node
, 0),
154 DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP
, PCDIMMDevice
, slot
,
155 PC_DIMM_UNASSIGNED_SLOT
),
156 DEFINE_PROP_LINK(PC_DIMM_MEMDEV_PROP
, PCDIMMDevice
, hostmem
,
157 TYPE_MEMORY_BACKEND
, HostMemoryBackend
*),
158 DEFINE_PROP_END_OF_LIST(),
161 static void pc_dimm_get_size(Object
*obj
, Visitor
*v
, const char *name
,
162 void *opaque
, Error
**errp
)
166 PCDIMMDevice
*dimm
= PC_DIMM(obj
);
167 PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(obj
);
169 mr
= ddc
->get_memory_region(dimm
, errp
);
173 value
= memory_region_size(mr
);
175 visit_type_uint64(v
, name
, &value
, errp
);
178 static void pc_dimm_init(Object
*obj
)
180 object_property_add(obj
, PC_DIMM_SIZE_PROP
, "uint64", pc_dimm_get_size
,
181 NULL
, NULL
, NULL
, &error_abort
);
184 static void pc_dimm_realize(DeviceState
*dev
, Error
**errp
)
186 PCDIMMDevice
*dimm
= PC_DIMM(dev
);
187 PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(dimm
);
189 if (!dimm
->hostmem
) {
190 error_setg(errp
, "'" PC_DIMM_MEMDEV_PROP
"' property is not set");
192 } else if (host_memory_backend_is_mapped(dimm
->hostmem
)) {
193 char *path
= object_get_canonical_path_component(OBJECT(dimm
->hostmem
));
194 error_setg(errp
, "can't use already busy memdev: %s", path
);
198 if (((nb_numa_nodes
> 0) && (dimm
->node
>= nb_numa_nodes
)) ||
199 (!nb_numa_nodes
&& dimm
->node
)) {
200 error_setg(errp
, "'DIMM property " PC_DIMM_NODE_PROP
" has value %"
201 PRIu32
"' which exceeds the number of numa nodes: %d",
202 dimm
->node
, nb_numa_nodes
? nb_numa_nodes
: 1);
207 ddc
->realize(dimm
, errp
);
210 host_memory_backend_set_mapped(dimm
->hostmem
, true);
213 static void pc_dimm_unrealize(DeviceState
*dev
, Error
**errp
)
215 PCDIMMDevice
*dimm
= PC_DIMM(dev
);
217 host_memory_backend_set_mapped(dimm
->hostmem
, false);
220 static MemoryRegion
*pc_dimm_get_memory_region(PCDIMMDevice
*dimm
, Error
**errp
)
222 if (!dimm
->hostmem
) {
223 error_setg(errp
, "'" PC_DIMM_MEMDEV_PROP
"' property must be set");
227 return host_memory_backend_get_memory(dimm
->hostmem
, errp
);
230 static MemoryRegion
*pc_dimm_get_vmstate_memory_region(PCDIMMDevice
*dimm
)
232 return host_memory_backend_get_memory(dimm
->hostmem
, &error_abort
);
235 static uint64_t pc_dimm_md_get_addr(const MemoryDeviceState
*md
)
237 const PCDIMMDevice
*dimm
= PC_DIMM(md
);
242 static uint64_t pc_dimm_md_get_region_size(const MemoryDeviceState
*md
)
244 /* dropping const here is fine as we don't touch the memory region */
245 PCDIMMDevice
*dimm
= PC_DIMM(md
);
246 const PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(md
);
249 mr
= ddc
->get_memory_region(dimm
, &error_abort
);
254 return memory_region_size(mr
);
257 static void pc_dimm_md_fill_device_info(const MemoryDeviceState
*md
,
258 MemoryDeviceInfo
*info
)
260 PCDIMMDeviceInfo
*di
= g_new0(PCDIMMDeviceInfo
, 1);
261 const DeviceClass
*dc
= DEVICE_GET_CLASS(md
);
262 const PCDIMMDevice
*dimm
= PC_DIMM(md
);
263 const DeviceState
*dev
= DEVICE(md
);
267 di
->id
= g_strdup(dev
->id
);
269 di
->hotplugged
= dev
->hotplugged
;
270 di
->hotpluggable
= dc
->hotpluggable
;
271 di
->addr
= dimm
->addr
;
272 di
->slot
= dimm
->slot
;
273 di
->node
= dimm
->node
;
274 di
->size
= object_property_get_uint(OBJECT(dimm
), PC_DIMM_SIZE_PROP
,
276 di
->memdev
= object_get_canonical_path(OBJECT(dimm
->hostmem
));
278 if (object_dynamic_cast(OBJECT(dev
), TYPE_NVDIMM
)) {
279 info
->u
.nvdimm
.data
= di
;
280 info
->type
= MEMORY_DEVICE_INFO_KIND_NVDIMM
;
282 info
->u
.dimm
.data
= di
;
283 info
->type
= MEMORY_DEVICE_INFO_KIND_DIMM
;
287 static void pc_dimm_class_init(ObjectClass
*oc
, void *data
)
289 DeviceClass
*dc
= DEVICE_CLASS(oc
);
290 PCDIMMDeviceClass
*ddc
= PC_DIMM_CLASS(oc
);
291 MemoryDeviceClass
*mdc
= MEMORY_DEVICE_CLASS(oc
);
293 dc
->realize
= pc_dimm_realize
;
294 dc
->unrealize
= pc_dimm_unrealize
;
295 dc
->props
= pc_dimm_properties
;
296 dc
->desc
= "DIMM memory module";
298 ddc
->get_memory_region
= pc_dimm_get_memory_region
;
299 ddc
->get_vmstate_memory_region
= pc_dimm_get_vmstate_memory_region
;
301 mdc
->get_addr
= pc_dimm_md_get_addr
;
302 /* for a dimm plugged_size == region_size */
303 mdc
->get_plugged_size
= pc_dimm_md_get_region_size
;
304 mdc
->get_region_size
= pc_dimm_md_get_region_size
;
305 mdc
->fill_device_info
= pc_dimm_md_fill_device_info
;
308 static TypeInfo pc_dimm_info
= {
309 .name
= TYPE_PC_DIMM
,
310 .parent
= TYPE_DEVICE
,
311 .instance_size
= sizeof(PCDIMMDevice
),
312 .instance_init
= pc_dimm_init
,
313 .class_init
= pc_dimm_class_init
,
314 .class_size
= sizeof(PCDIMMDeviceClass
),
315 .interfaces
= (InterfaceInfo
[]) {
316 { TYPE_MEMORY_DEVICE
},
321 static void pc_dimm_register_types(void)
323 type_register_static(&pc_dimm_info
);
326 type_init(pc_dimm_register_types
)