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 static int pc_dimm_get_free_slot(const int *hint
, int max_slots
, Error
**errp
);
32 void pc_dimm_pre_plug(PCDIMMDevice
*dimm
, MachineState
*machine
,
33 const uint64_t *legacy_align
, Error
**errp
)
35 Error
*local_err
= NULL
;
39 slot
= object_property_get_int(OBJECT(dimm
), PC_DIMM_SLOT_PROP
,
41 slot
= pc_dimm_get_free_slot(slot
== PC_DIMM_UNASSIGNED_SLOT
? NULL
: &slot
,
42 machine
->ram_slots
, &local_err
);
46 object_property_set_int(OBJECT(dimm
), slot
, PC_DIMM_SLOT_PROP
,
48 trace_mhp_pc_dimm_assigned_slot(slot
);
50 memory_device_pre_plug(MEMORY_DEVICE(dimm
), machine
, legacy_align
,
56 addr
= object_property_get_uint(OBJECT(dimm
), PC_DIMM_ADDR_PROP
,
58 trace_mhp_pc_dimm_assigned_address(addr
);
60 error_propagate(errp
, local_err
);
63 void pc_dimm_plug(PCDIMMDevice
*dimm
, MachineState
*machine
, Error
**errp
)
65 PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(dimm
);
66 MemoryRegion
*vmstate_mr
= ddc
->get_vmstate_memory_region(dimm
,
69 memory_device_plug(MEMORY_DEVICE(dimm
), machine
);
70 vmstate_register_ram(vmstate_mr
, DEVICE(dimm
));
73 void pc_dimm_unplug(PCDIMMDevice
*dimm
, MachineState
*machine
)
75 PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(dimm
);
76 MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(dimm
);
77 MemoryRegion
*vmstate_mr
= ddc
->get_vmstate_memory_region(dimm
,
79 MemoryRegion
*mr
= mdc
->get_memory_region(MEMORY_DEVICE(dimm
),
82 memory_device_unplug_region(machine
, mr
);
83 vmstate_unregister_ram(vmstate_mr
, DEVICE(dimm
));
86 static int pc_dimm_slot2bitmap(Object
*obj
, void *opaque
)
88 unsigned long *bitmap
= opaque
;
90 if (object_dynamic_cast(obj
, TYPE_PC_DIMM
)) {
91 DeviceState
*dev
= DEVICE(obj
);
92 if (dev
->realized
) { /* count only realized DIMMs */
93 PCDIMMDevice
*d
= PC_DIMM(obj
);
94 set_bit(d
->slot
, bitmap
);
98 object_child_foreach(obj
, pc_dimm_slot2bitmap
, opaque
);
102 static int pc_dimm_get_free_slot(const int *hint
, int max_slots
, Error
**errp
)
104 unsigned long *bitmap
;
107 if (max_slots
<= 0) {
108 error_setg(errp
, "no slots where allocated, please specify "
109 "the 'slots' option");
113 bitmap
= bitmap_new(max_slots
);
114 object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap
, bitmap
);
116 /* check if requested slot is not occupied */
118 if (*hint
>= max_slots
) {
119 error_setg(errp
, "invalid slot# %d, should be less than %d",
121 } else if (!test_bit(*hint
, bitmap
)) {
124 error_setg(errp
, "slot %d is busy", *hint
);
129 /* search for free slot */
130 slot
= find_first_zero_bit(bitmap
, max_slots
);
131 if (slot
== max_slots
) {
132 error_setg(errp
, "no free slots available");
139 static Property pc_dimm_properties
[] = {
140 DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP
, PCDIMMDevice
, addr
, 0),
141 DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP
, PCDIMMDevice
, node
, 0),
142 DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP
, PCDIMMDevice
, slot
,
143 PC_DIMM_UNASSIGNED_SLOT
),
144 DEFINE_PROP_LINK(PC_DIMM_MEMDEV_PROP
, PCDIMMDevice
, hostmem
,
145 TYPE_MEMORY_BACKEND
, HostMemoryBackend
*),
146 DEFINE_PROP_END_OF_LIST(),
149 static void pc_dimm_get_size(Object
*obj
, Visitor
*v
, const char *name
,
150 void *opaque
, Error
**errp
)
152 Error
*local_err
= NULL
;
155 value
= memory_device_get_region_size(MEMORY_DEVICE(obj
), &local_err
);
157 error_propagate(errp
, local_err
);
161 visit_type_uint64(v
, name
, &value
, errp
);
164 static void pc_dimm_init(Object
*obj
)
166 object_property_add(obj
, PC_DIMM_SIZE_PROP
, "uint64", pc_dimm_get_size
,
167 NULL
, NULL
, NULL
, &error_abort
);
170 static void pc_dimm_realize(DeviceState
*dev
, Error
**errp
)
172 PCDIMMDevice
*dimm
= PC_DIMM(dev
);
173 PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(dimm
);
175 if (!dimm
->hostmem
) {
176 error_setg(errp
, "'" PC_DIMM_MEMDEV_PROP
"' property is not set");
178 } else if (host_memory_backend_is_mapped(dimm
->hostmem
)) {
179 char *path
= object_get_canonical_path_component(OBJECT(dimm
->hostmem
));
180 error_setg(errp
, "can't use already busy memdev: %s", path
);
184 if (((nb_numa_nodes
> 0) && (dimm
->node
>= nb_numa_nodes
)) ||
185 (!nb_numa_nodes
&& dimm
->node
)) {
186 error_setg(errp
, "'DIMM property " PC_DIMM_NODE_PROP
" has value %"
187 PRIu32
"' which exceeds the number of numa nodes: %d",
188 dimm
->node
, nb_numa_nodes
? nb_numa_nodes
: 1);
193 ddc
->realize(dimm
, errp
);
196 host_memory_backend_set_mapped(dimm
->hostmem
, true);
199 static void pc_dimm_unrealize(DeviceState
*dev
, Error
**errp
)
201 PCDIMMDevice
*dimm
= PC_DIMM(dev
);
203 host_memory_backend_set_mapped(dimm
->hostmem
, false);
206 static MemoryRegion
*pc_dimm_get_memory_region(PCDIMMDevice
*dimm
, Error
**errp
)
208 if (!dimm
->hostmem
) {
209 error_setg(errp
, "'" PC_DIMM_MEMDEV_PROP
"' property must be set");
213 return host_memory_backend_get_memory(dimm
->hostmem
);
216 static uint64_t pc_dimm_md_get_addr(const MemoryDeviceState
*md
)
218 const PCDIMMDevice
*dimm
= PC_DIMM(md
);
223 static void pc_dimm_md_set_addr(MemoryDeviceState
*md
, uint64_t addr
,
226 object_property_set_uint(OBJECT(md
), addr
, PC_DIMM_ADDR_PROP
, errp
);
229 static MemoryRegion
*pc_dimm_md_get_memory_region(MemoryDeviceState
*md
,
232 return pc_dimm_get_memory_region(PC_DIMM(md
), errp
);
235 static void pc_dimm_md_fill_device_info(const MemoryDeviceState
*md
,
236 MemoryDeviceInfo
*info
)
238 PCDIMMDeviceInfo
*di
= g_new0(PCDIMMDeviceInfo
, 1);
239 const DeviceClass
*dc
= DEVICE_GET_CLASS(md
);
240 const PCDIMMDevice
*dimm
= PC_DIMM(md
);
241 const DeviceState
*dev
= DEVICE(md
);
245 di
->id
= g_strdup(dev
->id
);
247 di
->hotplugged
= dev
->hotplugged
;
248 di
->hotpluggable
= dc
->hotpluggable
;
249 di
->addr
= dimm
->addr
;
250 di
->slot
= dimm
->slot
;
251 di
->node
= dimm
->node
;
252 di
->size
= object_property_get_uint(OBJECT(dimm
), PC_DIMM_SIZE_PROP
,
254 di
->memdev
= object_get_canonical_path(OBJECT(dimm
->hostmem
));
256 if (object_dynamic_cast(OBJECT(dev
), TYPE_NVDIMM
)) {
257 info
->u
.nvdimm
.data
= di
;
258 info
->type
= MEMORY_DEVICE_INFO_KIND_NVDIMM
;
260 info
->u
.dimm
.data
= di
;
261 info
->type
= MEMORY_DEVICE_INFO_KIND_DIMM
;
265 static void pc_dimm_class_init(ObjectClass
*oc
, void *data
)
267 DeviceClass
*dc
= DEVICE_CLASS(oc
);
268 PCDIMMDeviceClass
*ddc
= PC_DIMM_CLASS(oc
);
269 MemoryDeviceClass
*mdc
= MEMORY_DEVICE_CLASS(oc
);
271 dc
->realize
= pc_dimm_realize
;
272 dc
->unrealize
= pc_dimm_unrealize
;
273 dc
->props
= pc_dimm_properties
;
274 dc
->desc
= "DIMM memory module";
276 ddc
->get_vmstate_memory_region
= pc_dimm_get_memory_region
;
278 mdc
->get_addr
= pc_dimm_md_get_addr
;
279 mdc
->set_addr
= pc_dimm_md_set_addr
;
280 /* for a dimm plugged_size == region_size */
281 mdc
->get_plugged_size
= memory_device_get_region_size
;
282 mdc
->get_memory_region
= pc_dimm_md_get_memory_region
;
283 mdc
->fill_device_info
= pc_dimm_md_fill_device_info
;
286 static TypeInfo pc_dimm_info
= {
287 .name
= TYPE_PC_DIMM
,
288 .parent
= TYPE_DEVICE
,
289 .instance_size
= sizeof(PCDIMMDevice
),
290 .instance_init
= pc_dimm_init
,
291 .class_init
= pc_dimm_class_init
,
292 .class_size
= sizeof(PCDIMMDeviceClass
),
293 .interfaces
= (InterfaceInfo
[]) {
294 { TYPE_MEMORY_DEVICE
},
299 static void pc_dimm_register_types(void)
301 type_register_static(&pc_dimm_info
);
304 type_init(pc_dimm_register_types
)