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 "qapi/error.h"
24 #include "qemu/config-file.h"
25 #include "qapi/visitor.h"
26 #include "qemu/range.h"
27 #include "sysemu/numa.h"
28 #include "sysemu/kvm.h"
30 #include "hw/virtio/vhost.h"
32 typedef struct pc_dimms_capacity
{
37 void pc_dimm_memory_plug(DeviceState
*dev
, MemoryHotplugState
*hpms
,
38 MemoryRegion
*mr
, uint64_t align
, Error
**errp
)
41 MachineState
*machine
= MACHINE(qdev_get_machine());
42 PCDIMMDevice
*dimm
= PC_DIMM(dev
);
43 PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(dimm
);
44 MemoryRegion
*vmstate_mr
= ddc
->get_vmstate_memory_region(dimm
);
45 Error
*local_err
= NULL
;
46 uint64_t existing_dimms_capacity
= 0;
49 addr
= object_property_get_uint(OBJECT(dimm
),
50 PC_DIMM_ADDR_PROP
, &local_err
);
55 addr
= pc_dimm_get_free_addr(hpms
->base
,
56 memory_region_size(&hpms
->mr
),
57 !addr
? NULL
: &addr
, align
,
58 memory_region_size(mr
), &local_err
);
63 existing_dimms_capacity
= pc_existing_dimms_capacity(&local_err
);
68 if (existing_dimms_capacity
+ memory_region_size(mr
) >
69 machine
->maxram_size
- machine
->ram_size
) {
70 error_setg(&local_err
, "not enough space, currently 0x%" PRIx64
71 " in use of total hot pluggable 0x" RAM_ADDR_FMT
,
72 existing_dimms_capacity
,
73 machine
->maxram_size
- machine
->ram_size
);
77 object_property_set_uint(OBJECT(dev
), addr
, PC_DIMM_ADDR_PROP
, &local_err
);
81 trace_mhp_pc_dimm_assigned_address(addr
);
83 slot
= object_property_get_int(OBJECT(dev
), PC_DIMM_SLOT_PROP
, &local_err
);
88 slot
= pc_dimm_get_free_slot(slot
== PC_DIMM_UNASSIGNED_SLOT
? NULL
: &slot
,
89 machine
->ram_slots
, &local_err
);
93 object_property_set_int(OBJECT(dev
), slot
, PC_DIMM_SLOT_PROP
, &local_err
);
97 trace_mhp_pc_dimm_assigned_slot(slot
);
99 if (kvm_enabled() && !kvm_has_free_slot(machine
)) {
100 error_setg(&local_err
, "hypervisor has no free memory slots left");
104 if (!vhost_has_free_slot()) {
105 error_setg(&local_err
, "a used vhost backend has no free"
106 " memory slots left");
110 memory_region_add_subregion(&hpms
->mr
, addr
- hpms
->base
, mr
);
111 vmstate_register_ram(vmstate_mr
, dev
);
112 numa_set_mem_node_id(addr
, memory_region_size(mr
), dimm
->node
);
115 error_propagate(errp
, local_err
);
118 void pc_dimm_memory_unplug(DeviceState
*dev
, MemoryHotplugState
*hpms
,
121 PCDIMMDevice
*dimm
= PC_DIMM(dev
);
122 PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(dimm
);
123 MemoryRegion
*vmstate_mr
= ddc
->get_vmstate_memory_region(dimm
);
125 numa_unset_mem_node_id(dimm
->addr
, memory_region_size(mr
), dimm
->node
);
126 memory_region_del_subregion(&hpms
->mr
, mr
);
127 vmstate_unregister_ram(vmstate_mr
, dev
);
130 static int pc_existing_dimms_capacity_internal(Object
*obj
, void *opaque
)
132 pc_dimms_capacity
*cap
= opaque
;
133 uint64_t *size
= &cap
->size
;
135 if (object_dynamic_cast(obj
, TYPE_PC_DIMM
)) {
136 DeviceState
*dev
= DEVICE(obj
);
139 (*size
) += object_property_get_uint(obj
, PC_DIMM_SIZE_PROP
,
143 if (cap
->errp
&& *cap
->errp
) {
147 object_child_foreach(obj
, pc_existing_dimms_capacity_internal
, opaque
);
151 uint64_t pc_existing_dimms_capacity(Error
**errp
)
153 pc_dimms_capacity cap
;
158 pc_existing_dimms_capacity_internal(qdev_get_machine(), &cap
);
162 int qmp_pc_dimm_device_list(Object
*obj
, void *opaque
)
164 MemoryDeviceInfoList
***prev
= opaque
;
166 if (object_dynamic_cast(obj
, TYPE_PC_DIMM
)) {
167 DeviceState
*dev
= DEVICE(obj
);
170 MemoryDeviceInfoList
*elem
= g_new0(MemoryDeviceInfoList
, 1);
171 MemoryDeviceInfo
*info
= g_new0(MemoryDeviceInfo
, 1);
172 PCDIMMDeviceInfo
*di
= g_new0(PCDIMMDeviceInfo
, 1);
173 DeviceClass
*dc
= DEVICE_GET_CLASS(obj
);
174 PCDIMMDevice
*dimm
= PC_DIMM(obj
);
178 di
->id
= g_strdup(dev
->id
);
180 di
->hotplugged
= dev
->hotplugged
;
181 di
->hotpluggable
= dc
->hotpluggable
;
182 di
->addr
= dimm
->addr
;
183 di
->slot
= dimm
->slot
;
184 di
->node
= dimm
->node
;
185 di
->size
= object_property_get_uint(OBJECT(dimm
), PC_DIMM_SIZE_PROP
,
187 di
->memdev
= object_get_canonical_path(OBJECT(dimm
->hostmem
));
189 info
->u
.dimm
.data
= di
;
197 object_child_foreach(obj
, qmp_pc_dimm_device_list
, opaque
);
201 static int pc_dimm_slot2bitmap(Object
*obj
, void *opaque
)
203 unsigned long *bitmap
= opaque
;
205 if (object_dynamic_cast(obj
, TYPE_PC_DIMM
)) {
206 DeviceState
*dev
= DEVICE(obj
);
207 if (dev
->realized
) { /* count only realized DIMMs */
208 PCDIMMDevice
*d
= PC_DIMM(obj
);
209 set_bit(d
->slot
, bitmap
);
213 object_child_foreach(obj
, pc_dimm_slot2bitmap
, opaque
);
217 int pc_dimm_get_free_slot(const int *hint
, int max_slots
, Error
**errp
)
219 unsigned long *bitmap
= bitmap_new(max_slots
);
222 object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap
, bitmap
);
224 /* check if requested slot is not occupied */
226 if (*hint
>= max_slots
) {
227 error_setg(errp
, "invalid slot# %d, should be less than %d",
229 } else if (!test_bit(*hint
, bitmap
)) {
232 error_setg(errp
, "slot %d is busy", *hint
);
237 /* search for free slot */
238 slot
= find_first_zero_bit(bitmap
, max_slots
);
239 if (slot
== max_slots
) {
240 error_setg(errp
, "no free slots available");
247 static gint
pc_dimm_addr_sort(gconstpointer a
, gconstpointer b
)
249 PCDIMMDevice
*x
= PC_DIMM(a
);
250 PCDIMMDevice
*y
= PC_DIMM(b
);
251 Int128 diff
= int128_sub(int128_make64(x
->addr
), int128_make64(y
->addr
));
253 if (int128_lt(diff
, int128_zero())) {
255 } else if (int128_gt(diff
, int128_zero())) {
261 static int pc_dimm_built_list(Object
*obj
, void *opaque
)
263 GSList
**list
= opaque
;
265 if (object_dynamic_cast(obj
, TYPE_PC_DIMM
)) {
266 DeviceState
*dev
= DEVICE(obj
);
267 if (dev
->realized
) { /* only realized DIMMs matter */
268 *list
= g_slist_insert_sorted(*list
, dev
, pc_dimm_addr_sort
);
272 object_child_foreach(obj
, pc_dimm_built_list
, opaque
);
276 uint64_t pc_dimm_get_free_addr(uint64_t address_space_start
,
277 uint64_t address_space_size
,
278 uint64_t *hint
, uint64_t align
, uint64_t size
,
281 GSList
*list
= NULL
, *item
;
282 uint64_t new_addr
, ret
= 0;
283 uint64_t address_space_end
= address_space_start
+ address_space_size
;
285 g_assert(QEMU_ALIGN_UP(address_space_start
, align
) == address_space_start
);
287 if (!address_space_size
) {
288 error_setg(errp
, "memory hotplug is not enabled, "
289 "please add maxmem option");
293 if (hint
&& QEMU_ALIGN_UP(*hint
, align
) != *hint
) {
294 error_setg(errp
, "address must be aligned to 0x%" PRIx64
" bytes",
299 if (QEMU_ALIGN_UP(size
, align
) != size
) {
300 error_setg(errp
, "backend memory size must be multiple of 0x%"
305 assert(address_space_end
> address_space_start
);
306 object_child_foreach(qdev_get_machine(), pc_dimm_built_list
, &list
);
311 new_addr
= address_space_start
;
314 /* find address range that will fit new DIMM */
315 for (item
= list
; item
; item
= g_slist_next(item
)) {
316 PCDIMMDevice
*dimm
= item
->data
;
317 uint64_t dimm_size
= object_property_get_uint(OBJECT(dimm
),
324 if (ranges_overlap(dimm
->addr
, dimm_size
, new_addr
, size
)) {
326 DeviceState
*d
= DEVICE(dimm
);
327 error_setg(errp
, "address range conflicts with '%s'", d
->id
);
330 new_addr
= QEMU_ALIGN_UP(dimm
->addr
+ dimm_size
, align
);
335 if (new_addr
< address_space_start
) {
336 error_setg(errp
, "can't add memory [0x%" PRIx64
":0x%" PRIx64
337 "] at 0x%" PRIx64
, new_addr
, size
, address_space_start
);
338 } else if ((new_addr
+ size
) > address_space_end
) {
339 error_setg(errp
, "can't add memory [0x%" PRIx64
":0x%" PRIx64
340 "] beyond 0x%" PRIx64
, new_addr
, size
, address_space_end
);
348 static Property pc_dimm_properties
[] = {
349 DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP
, PCDIMMDevice
, addr
, 0),
350 DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP
, PCDIMMDevice
, node
, 0),
351 DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP
, PCDIMMDevice
, slot
,
352 PC_DIMM_UNASSIGNED_SLOT
),
353 DEFINE_PROP_LINK(PC_DIMM_MEMDEV_PROP
, PCDIMMDevice
, hostmem
,
354 TYPE_MEMORY_BACKEND
, HostMemoryBackend
*),
355 DEFINE_PROP_END_OF_LIST(),
358 static void pc_dimm_get_size(Object
*obj
, Visitor
*v
, const char *name
,
359 void *opaque
, Error
**errp
)
363 PCDIMMDevice
*dimm
= PC_DIMM(obj
);
364 PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(obj
);
366 mr
= ddc
->get_memory_region(dimm
, errp
);
370 value
= memory_region_size(mr
);
372 visit_type_uint64(v
, name
, &value
, errp
);
375 static void pc_dimm_init(Object
*obj
)
377 object_property_add(obj
, PC_DIMM_SIZE_PROP
, "uint64", pc_dimm_get_size
,
378 NULL
, NULL
, NULL
, &error_abort
);
381 static void pc_dimm_realize(DeviceState
*dev
, Error
**errp
)
383 PCDIMMDevice
*dimm
= PC_DIMM(dev
);
384 PCDIMMDeviceClass
*ddc
= PC_DIMM_GET_CLASS(dimm
);
386 if (!dimm
->hostmem
) {
387 error_setg(errp
, "'" PC_DIMM_MEMDEV_PROP
"' property is not set");
389 } else if (host_memory_backend_is_mapped(dimm
->hostmem
)) {
390 char *path
= object_get_canonical_path_component(OBJECT(dimm
->hostmem
));
391 error_setg(errp
, "can't use already busy memdev: %s", path
);
395 if (((nb_numa_nodes
> 0) && (dimm
->node
>= nb_numa_nodes
)) ||
396 (!nb_numa_nodes
&& dimm
->node
)) {
397 error_setg(errp
, "'DIMM property " PC_DIMM_NODE_PROP
" has value %"
398 PRIu32
"' which exceeds the number of numa nodes: %d",
399 dimm
->node
, nb_numa_nodes
? nb_numa_nodes
: 1);
404 ddc
->realize(dimm
, errp
);
407 host_memory_backend_set_mapped(dimm
->hostmem
, true);
410 static void pc_dimm_unrealize(DeviceState
*dev
, Error
**errp
)
412 PCDIMMDevice
*dimm
= PC_DIMM(dev
);
414 host_memory_backend_set_mapped(dimm
->hostmem
, false);
417 static MemoryRegion
*pc_dimm_get_memory_region(PCDIMMDevice
*dimm
, Error
**errp
)
419 if (!dimm
->hostmem
) {
420 error_setg(errp
, "'" PC_DIMM_MEMDEV_PROP
"' property must be set");
424 return host_memory_backend_get_memory(dimm
->hostmem
, errp
);
427 static MemoryRegion
*pc_dimm_get_vmstate_memory_region(PCDIMMDevice
*dimm
)
429 return host_memory_backend_get_memory(dimm
->hostmem
, &error_abort
);
432 static void pc_dimm_class_init(ObjectClass
*oc
, void *data
)
434 DeviceClass
*dc
= DEVICE_CLASS(oc
);
435 PCDIMMDeviceClass
*ddc
= PC_DIMM_CLASS(oc
);
437 dc
->realize
= pc_dimm_realize
;
438 dc
->unrealize
= pc_dimm_unrealize
;
439 dc
->props
= pc_dimm_properties
;
440 dc
->desc
= "DIMM memory module";
442 ddc
->get_memory_region
= pc_dimm_get_memory_region
;
443 ddc
->get_vmstate_memory_region
= pc_dimm_get_vmstate_memory_region
;
446 static TypeInfo pc_dimm_info
= {
447 .name
= TYPE_PC_DIMM
,
448 .parent
= TYPE_DEVICE
,
449 .instance_size
= sizeof(PCDIMMDevice
),
450 .instance_init
= pc_dimm_init
,
451 .class_init
= pc_dimm_class_init
,
452 .class_size
= sizeof(PCDIMMDeviceClass
),
455 static void pc_dimm_register_types(void)
457 type_register_static(&pc_dimm_info
);
460 type_init(pc_dimm_register_types
)