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 typedef struct pc_dimms_capacity
{
35 void pc_dimm_memory_plug(DeviceState
*dev
, MachineState
*machine
,
36 uint64_t align
, Error
**errp
)
39 PCDIMMDevice
*dimm
= PC_DIMM(dev
);
40 PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(dimm
);
41 MemoryRegion
*vmstate_mr
= ddc
->get_vmstate_memory_region(dimm
);
42 Error
*local_err
= NULL
;
46 mr
= ddc
->get_memory_region(dimm
, &local_err
);
51 addr
= object_property_get_uint(OBJECT(dimm
),
52 PC_DIMM_ADDR_PROP
, &local_err
);
57 addr
= memory_device_get_free_addr(machine
, !addr
? NULL
: &addr
, align
,
58 memory_region_size(mr
), &local_err
);
63 object_property_set_uint(OBJECT(dev
), addr
, PC_DIMM_ADDR_PROP
, &local_err
);
67 trace_mhp_pc_dimm_assigned_address(addr
);
69 slot
= object_property_get_int(OBJECT(dev
), PC_DIMM_SLOT_PROP
, &local_err
);
74 slot
= pc_dimm_get_free_slot(slot
== PC_DIMM_UNASSIGNED_SLOT
? NULL
: &slot
,
75 machine
->ram_slots
, &local_err
);
79 object_property_set_int(OBJECT(dev
), slot
, PC_DIMM_SLOT_PROP
, &local_err
);
83 trace_mhp_pc_dimm_assigned_slot(slot
);
85 memory_device_plug_region(machine
, mr
, addr
);
86 vmstate_register_ram(vmstate_mr
, dev
);
89 error_propagate(errp
, local_err
);
92 void pc_dimm_memory_unplug(DeviceState
*dev
, MachineState
*machine
)
94 PCDIMMDevice
*dimm
= PC_DIMM(dev
);
95 PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(dimm
);
96 MemoryRegion
*vmstate_mr
= ddc
->get_vmstate_memory_region(dimm
);
97 MemoryRegion
*mr
= ddc
->get_memory_region(dimm
, &error_abort
);
99 memory_device_unplug_region(machine
, mr
);
100 vmstate_unregister_ram(vmstate_mr
, dev
);
103 static int pc_dimm_slot2bitmap(Object
*obj
, void *opaque
)
105 unsigned long *bitmap
= opaque
;
107 if (object_dynamic_cast(obj
, TYPE_PC_DIMM
)) {
108 DeviceState
*dev
= DEVICE(obj
);
109 if (dev
->realized
) { /* count only realized DIMMs */
110 PCDIMMDevice
*d
= PC_DIMM(obj
);
111 set_bit(d
->slot
, bitmap
);
115 object_child_foreach(obj
, pc_dimm_slot2bitmap
, opaque
);
119 int pc_dimm_get_free_slot(const int *hint
, int max_slots
, Error
**errp
)
121 unsigned long *bitmap
;
124 if (max_slots
<= 0) {
125 error_setg(errp
, "no slots where allocated, please specify "
126 "the 'slots' option");
130 bitmap
= bitmap_new(max_slots
);
131 object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap
, bitmap
);
133 /* check if requested slot is not occupied */
135 if (*hint
>= max_slots
) {
136 error_setg(errp
, "invalid slot# %d, should be less than %d",
138 } else if (!test_bit(*hint
, bitmap
)) {
141 error_setg(errp
, "slot %d is busy", *hint
);
146 /* search for free slot */
147 slot
= find_first_zero_bit(bitmap
, max_slots
);
148 if (slot
== max_slots
) {
149 error_setg(errp
, "no free slots available");
156 static Property pc_dimm_properties
[] = {
157 DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP
, PCDIMMDevice
, addr
, 0),
158 DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP
, PCDIMMDevice
, node
, 0),
159 DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP
, PCDIMMDevice
, slot
,
160 PC_DIMM_UNASSIGNED_SLOT
),
161 DEFINE_PROP_LINK(PC_DIMM_MEMDEV_PROP
, PCDIMMDevice
, hostmem
,
162 TYPE_MEMORY_BACKEND
, HostMemoryBackend
*),
163 DEFINE_PROP_END_OF_LIST(),
166 static void pc_dimm_get_size(Object
*obj
, Visitor
*v
, const char *name
,
167 void *opaque
, Error
**errp
)
171 PCDIMMDevice
*dimm
= PC_DIMM(obj
);
172 PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(obj
);
174 mr
= ddc
->get_memory_region(dimm
, errp
);
178 value
= memory_region_size(mr
);
180 visit_type_uint64(v
, name
, &value
, errp
);
183 static void pc_dimm_init(Object
*obj
)
185 object_property_add(obj
, PC_DIMM_SIZE_PROP
, "uint64", pc_dimm_get_size
,
186 NULL
, NULL
, NULL
, &error_abort
);
189 static void pc_dimm_realize(DeviceState
*dev
, Error
**errp
)
191 PCDIMMDevice
*dimm
= PC_DIMM(dev
);
192 PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(dimm
);
194 if (!dimm
->hostmem
) {
195 error_setg(errp
, "'" PC_DIMM_MEMDEV_PROP
"' property is not set");
197 } else if (host_memory_backend_is_mapped(dimm
->hostmem
)) {
198 char *path
= object_get_canonical_path_component(OBJECT(dimm
->hostmem
));
199 error_setg(errp
, "can't use already busy memdev: %s", path
);
203 if (((nb_numa_nodes
> 0) && (dimm
->node
>= nb_numa_nodes
)) ||
204 (!nb_numa_nodes
&& dimm
->node
)) {
205 error_setg(errp
, "'DIMM property " PC_DIMM_NODE_PROP
" has value %"
206 PRIu32
"' which exceeds the number of numa nodes: %d",
207 dimm
->node
, nb_numa_nodes
? nb_numa_nodes
: 1);
212 ddc
->realize(dimm
, errp
);
215 host_memory_backend_set_mapped(dimm
->hostmem
, true);
218 static void pc_dimm_unrealize(DeviceState
*dev
, Error
**errp
)
220 PCDIMMDevice
*dimm
= PC_DIMM(dev
);
222 host_memory_backend_set_mapped(dimm
->hostmem
, false);
225 static MemoryRegion
*pc_dimm_get_memory_region(PCDIMMDevice
*dimm
, Error
**errp
)
227 if (!dimm
->hostmem
) {
228 error_setg(errp
, "'" PC_DIMM_MEMDEV_PROP
"' property must be set");
232 return host_memory_backend_get_memory(dimm
->hostmem
, errp
);
235 static MemoryRegion
*pc_dimm_get_vmstate_memory_region(PCDIMMDevice
*dimm
)
237 return host_memory_backend_get_memory(dimm
->hostmem
, &error_abort
);
240 static uint64_t pc_dimm_md_get_addr(const MemoryDeviceState
*md
)
242 const PCDIMMDevice
*dimm
= PC_DIMM(md
);
247 static uint64_t pc_dimm_md_get_region_size(const MemoryDeviceState
*md
)
249 /* dropping const here is fine as we don't touch the memory region */
250 PCDIMMDevice
*dimm
= PC_DIMM(md
);
251 const PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(md
);
254 mr
= ddc
->get_memory_region(dimm
, &error_abort
);
259 return memory_region_size(mr
);
262 static void pc_dimm_md_fill_device_info(const MemoryDeviceState
*md
,
263 MemoryDeviceInfo
*info
)
265 PCDIMMDeviceInfo
*di
= g_new0(PCDIMMDeviceInfo
, 1);
266 const DeviceClass
*dc
= DEVICE_GET_CLASS(md
);
267 const PCDIMMDevice
*dimm
= PC_DIMM(md
);
268 const DeviceState
*dev
= DEVICE(md
);
272 di
->id
= g_strdup(dev
->id
);
274 di
->hotplugged
= dev
->hotplugged
;
275 di
->hotpluggable
= dc
->hotpluggable
;
276 di
->addr
= dimm
->addr
;
277 di
->slot
= dimm
->slot
;
278 di
->node
= dimm
->node
;
279 di
->size
= object_property_get_uint(OBJECT(dimm
), PC_DIMM_SIZE_PROP
,
281 di
->memdev
= object_get_canonical_path(OBJECT(dimm
->hostmem
));
283 if (object_dynamic_cast(OBJECT(dev
), TYPE_NVDIMM
)) {
284 info
->u
.nvdimm
.data
= di
;
285 info
->type
= MEMORY_DEVICE_INFO_KIND_NVDIMM
;
287 info
->u
.dimm
.data
= di
;
288 info
->type
= MEMORY_DEVICE_INFO_KIND_DIMM
;
292 static void pc_dimm_class_init(ObjectClass
*oc
, void *data
)
294 DeviceClass
*dc
= DEVICE_CLASS(oc
);
295 PCDIMMDeviceClass
*ddc
= PC_DIMM_CLASS(oc
);
296 MemoryDeviceClass
*mdc
= MEMORY_DEVICE_CLASS(oc
);
298 dc
->realize
= pc_dimm_realize
;
299 dc
->unrealize
= pc_dimm_unrealize
;
300 dc
->props
= pc_dimm_properties
;
301 dc
->desc
= "DIMM memory module";
303 ddc
->get_memory_region
= pc_dimm_get_memory_region
;
304 ddc
->get_vmstate_memory_region
= pc_dimm_get_vmstate_memory_region
;
306 mdc
->get_addr
= pc_dimm_md_get_addr
;
307 /* for a dimm plugged_size == region_size */
308 mdc
->get_plugged_size
= pc_dimm_md_get_region_size
;
309 mdc
->get_region_size
= pc_dimm_md_get_region_size
;
310 mdc
->fill_device_info
= pc_dimm_md_fill_device_info
;
313 static TypeInfo pc_dimm_info
= {
314 .name
= TYPE_PC_DIMM
,
315 .parent
= TYPE_DEVICE
,
316 .instance_size
= sizeof(PCDIMMDevice
),
317 .instance_init
= pc_dimm_init
,
318 .class_init
= pc_dimm_class_init
,
319 .class_size
= sizeof(PCDIMMDeviceClass
),
320 .interfaces
= (InterfaceInfo
[]) {
321 { TYPE_MEMORY_DEVICE
},
326 static void pc_dimm_register_types(void)
328 type_register_static(&pc_dimm_info
);
331 type_init(pc_dimm_register_types
)