2 * Memory Device Interface
4 * Copyright ProfitBricks GmbH 2012
5 * Copyright (C) 2014 Red Hat Inc
6 * Copyright (c) 2018 Red Hat Inc
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "qemu/error-report.h"
14 #include "hw/mem/memory-device.h"
15 #include "qapi/error.h"
16 #include "hw/boards.h"
17 #include "qemu/range.h"
18 #include "hw/virtio/vhost.h"
19 #include "sysemu/kvm.h"
22 static gint
memory_device_addr_sort(gconstpointer a
, gconstpointer b
)
24 const MemoryDeviceState
*md_a
= MEMORY_DEVICE(a
);
25 const MemoryDeviceState
*md_b
= MEMORY_DEVICE(b
);
26 const MemoryDeviceClass
*mdc_a
= MEMORY_DEVICE_GET_CLASS(a
);
27 const MemoryDeviceClass
*mdc_b
= MEMORY_DEVICE_GET_CLASS(b
);
28 const uint64_t addr_a
= mdc_a
->get_addr(md_a
);
29 const uint64_t addr_b
= mdc_b
->get_addr(md_b
);
31 if (addr_a
> addr_b
) {
33 } else if (addr_a
< addr_b
) {
39 static int memory_device_build_list(Object
*obj
, void *opaque
)
41 GSList
**list
= opaque
;
43 if (object_dynamic_cast(obj
, TYPE_MEMORY_DEVICE
)) {
44 DeviceState
*dev
= DEVICE(obj
);
45 if (dev
->realized
) { /* only realized memory devices matter */
46 *list
= g_slist_insert_sorted(*list
, dev
, memory_device_addr_sort
);
50 object_child_foreach(obj
, memory_device_build_list
, opaque
);
54 static int memory_device_used_region_size(Object
*obj
, void *opaque
)
56 uint64_t *size
= opaque
;
58 if (object_dynamic_cast(obj
, TYPE_MEMORY_DEVICE
)) {
59 const DeviceState
*dev
= DEVICE(obj
);
60 const MemoryDeviceState
*md
= MEMORY_DEVICE(obj
);
63 *size
+= memory_device_get_region_size(md
, &error_abort
);
67 object_child_foreach(obj
, memory_device_used_region_size
, opaque
);
71 static void memory_device_check_addable(MachineState
*ms
, uint64_t size
,
74 uint64_t used_region_size
= 0;
76 /* we will need a new memory slot for kvm and vhost */
77 if (kvm_enabled() && !kvm_has_free_slot(ms
)) {
78 error_setg(errp
, "hypervisor has no free memory slots left");
81 if (!vhost_has_free_slot()) {
82 error_setg(errp
, "a used vhost backend has no free memory slots left");
86 /* will we exceed the total amount of memory specified */
87 memory_device_used_region_size(OBJECT(ms
), &used_region_size
);
88 if (used_region_size
+ size
< used_region_size
||
89 used_region_size
+ size
> ms
->maxram_size
- ms
->ram_size
) {
90 error_setg(errp
, "not enough space, currently 0x%" PRIx64
91 " in use of total space for memory devices 0x" RAM_ADDR_FMT
,
92 used_region_size
, ms
->maxram_size
- ms
->ram_size
);
98 static uint64_t memory_device_get_free_addr(MachineState
*ms
,
100 uint64_t align
, uint64_t size
,
104 GSList
*list
= NULL
, *item
;
105 Range as
, new = range_empty
;
107 if (!ms
->device_memory
) {
108 error_setg(errp
, "memory devices (e.g. for memory hotplug) are not "
109 "supported by the machine");
113 if (!memory_region_size(&ms
->device_memory
->mr
)) {
114 error_setg(errp
, "memory devices (e.g. for memory hotplug) are not "
115 "enabled, please specify the maxmem option");
118 range_init_nofail(&as
, ms
->device_memory
->base
,
119 memory_region_size(&ms
->device_memory
->mr
));
121 /* start of address space indicates the maximum alignment we expect */
122 if (!QEMU_IS_ALIGNED(range_lob(&as
), align
)) {
123 warn_report("the alignment (0x%" PRIx64
") exceeds the expected"
124 " maximum alignment, memory will get fragmented and not"
125 " all 'maxmem' might be usable for memory devices.",
129 memory_device_check_addable(ms
, size
, &err
);
131 error_propagate(errp
, err
);
135 if (hint
&& !QEMU_IS_ALIGNED(*hint
, align
)) {
136 error_setg(errp
, "address must be aligned to 0x%" PRIx64
" bytes",
141 if (!QEMU_IS_ALIGNED(size
, align
)) {
142 error_setg(errp
, "backend memory size must be multiple of 0x%"
148 if (range_init(&new, *hint
, size
) || !range_contains_range(&as
, &new)) {
149 error_setg(errp
, "can't add memory device [0x%" PRIx64
":0x%" PRIx64
150 "], usable range for memory devices [0x%" PRIx64
":0x%"
151 PRIx64
"]", *hint
, size
, range_lob(&as
),
156 if (range_init(&new, QEMU_ALIGN_UP(range_lob(&as
), align
), size
)) {
157 error_setg(errp
, "can't add memory device, device too big");
162 /* find address range that will fit new memory device */
163 object_child_foreach(OBJECT(ms
), memory_device_build_list
, &list
);
164 for (item
= list
; item
; item
= g_slist_next(item
)) {
165 const MemoryDeviceState
*md
= item
->data
;
166 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(OBJECT(md
));
170 range_init_nofail(&tmp
, mdc
->get_addr(md
),
171 memory_device_get_region_size(md
, &error_abort
));
173 if (range_overlaps_range(&tmp
, &new)) {
175 const DeviceState
*d
= DEVICE(md
);
176 error_setg(errp
, "address range conflicts with memory device"
177 " id='%s'", d
->id
? d
->id
: "(unnamed)");
181 next_addr
= QEMU_ALIGN_UP(range_upb(&tmp
) + 1, align
);
182 if (!next_addr
|| range_init(&new, next_addr
, range_size(&new))) {
183 range_make_empty(&new);
186 } else if (range_lob(&tmp
) > range_upb(&new)) {
191 if (!range_contains_range(&as
, &new)) {
192 error_setg(errp
, "could not find position in guest address space for "
193 "memory device - memory fragmented due to alignments");
197 return range_lob(&new);
200 MemoryDeviceInfoList
*qmp_memory_device_list(void)
202 GSList
*devices
= NULL
, *item
;
203 MemoryDeviceInfoList
*list
= NULL
, **tail
= &list
;
205 object_child_foreach(qdev_get_machine(), memory_device_build_list
,
208 for (item
= devices
; item
; item
= g_slist_next(item
)) {
209 const MemoryDeviceState
*md
= MEMORY_DEVICE(item
->data
);
210 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(item
->data
);
211 MemoryDeviceInfo
*info
= g_new0(MemoryDeviceInfo
, 1);
213 mdc
->fill_device_info(md
, info
);
215 QAPI_LIST_APPEND(tail
, info
);
218 g_slist_free(devices
);
223 static int memory_device_plugged_size(Object
*obj
, void *opaque
)
225 uint64_t *size
= opaque
;
227 if (object_dynamic_cast(obj
, TYPE_MEMORY_DEVICE
)) {
228 const DeviceState
*dev
= DEVICE(obj
);
229 const MemoryDeviceState
*md
= MEMORY_DEVICE(obj
);
230 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(obj
);
233 *size
+= mdc
->get_plugged_size(md
, &error_abort
);
237 object_child_foreach(obj
, memory_device_plugged_size
, opaque
);
241 uint64_t get_plugged_memory_size(void)
245 memory_device_plugged_size(qdev_get_machine(), &size
);
250 void memory_device_pre_plug(MemoryDeviceState
*md
, MachineState
*ms
,
251 const uint64_t *legacy_align
, Error
**errp
)
253 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(md
);
254 Error
*local_err
= NULL
;
255 uint64_t addr
, align
= 0;
258 mr
= mdc
->get_memory_region(md
, &local_err
);
264 align
= *legacy_align
;
266 if (mdc
->get_min_alignment
) {
267 align
= mdc
->get_min_alignment(md
);
269 align
= MAX(align
, memory_region_get_alignment(mr
));
271 addr
= mdc
->get_addr(md
);
272 addr
= memory_device_get_free_addr(ms
, !addr
? NULL
: &addr
, align
,
273 memory_region_size(mr
), &local_err
);
277 mdc
->set_addr(md
, addr
, &local_err
);
279 trace_memory_device_pre_plug(DEVICE(md
)->id
? DEVICE(md
)->id
: "",
283 error_propagate(errp
, local_err
);
286 void memory_device_plug(MemoryDeviceState
*md
, MachineState
*ms
)
288 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(md
);
289 const uint64_t addr
= mdc
->get_addr(md
);
293 * We expect that a previous call to memory_device_pre_plug() succeeded, so
294 * it can't fail at this point.
296 mr
= mdc
->get_memory_region(md
, &error_abort
);
297 g_assert(ms
->device_memory
);
299 memory_region_add_subregion(&ms
->device_memory
->mr
,
300 addr
- ms
->device_memory
->base
, mr
);
301 trace_memory_device_plug(DEVICE(md
)->id
? DEVICE(md
)->id
: "", addr
);
304 void memory_device_unplug(MemoryDeviceState
*md
, MachineState
*ms
)
306 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(md
);
310 * We expect that a previous call to memory_device_pre_plug() succeeded, so
311 * it can't fail at this point.
313 mr
= mdc
->get_memory_region(md
, &error_abort
);
314 g_assert(ms
->device_memory
);
316 memory_region_del_subregion(&ms
->device_memory
->mr
, mr
);
317 trace_memory_device_unplug(DEVICE(md
)->id
? DEVICE(md
)->id
: "",
321 uint64_t memory_device_get_region_size(const MemoryDeviceState
*md
,
324 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(md
);
327 /* dropping const here is fine as we don't touch the memory region */
328 mr
= mdc
->get_memory_region((MemoryDeviceState
*)md
, errp
);
333 return memory_region_size(mr
);
336 static const TypeInfo memory_device_info
= {
337 .name
= TYPE_MEMORY_DEVICE
,
338 .parent
= TYPE_INTERFACE
,
339 .class_size
= sizeof(MemoryDeviceClass
),
342 static void memory_device_register_types(void)
344 type_register_static(&memory_device_info
);
347 type_init(memory_device_register_types
)