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"
20 #include "exec/address-spaces.h"
23 static gint
memory_device_addr_sort(gconstpointer a
, gconstpointer b
)
25 const MemoryDeviceState
*md_a
= MEMORY_DEVICE(a
);
26 const MemoryDeviceState
*md_b
= MEMORY_DEVICE(b
);
27 const MemoryDeviceClass
*mdc_a
= MEMORY_DEVICE_GET_CLASS(a
);
28 const MemoryDeviceClass
*mdc_b
= MEMORY_DEVICE_GET_CLASS(b
);
29 const uint64_t addr_a
= mdc_a
->get_addr(md_a
);
30 const uint64_t addr_b
= mdc_b
->get_addr(md_b
);
32 if (addr_a
> addr_b
) {
34 } else if (addr_a
< addr_b
) {
40 static int memory_device_build_list(Object
*obj
, void *opaque
)
42 GSList
**list
= opaque
;
44 if (object_dynamic_cast(obj
, TYPE_MEMORY_DEVICE
)) {
45 DeviceState
*dev
= DEVICE(obj
);
46 if (dev
->realized
) { /* only realized memory devices matter */
47 *list
= g_slist_insert_sorted(*list
, dev
, memory_device_addr_sort
);
51 object_child_foreach(obj
, memory_device_build_list
, opaque
);
55 static void memory_device_check_addable(MachineState
*ms
, MemoryRegion
*mr
,
58 const uint64_t used_region_size
= ms
->device_memory
->used_region_size
;
59 const uint64_t size
= memory_region_size(mr
);
61 /* we will need a new memory slot for kvm and vhost */
62 if (kvm_enabled() && !kvm_has_free_slot(ms
)) {
63 error_setg(errp
, "hypervisor has no free memory slots left");
66 if (!vhost_has_free_slot()) {
67 error_setg(errp
, "a used vhost backend has no free memory slots left");
71 /* will we exceed the total amount of memory specified */
72 if (used_region_size
+ size
< used_region_size
||
73 used_region_size
+ size
> ms
->maxram_size
- ms
->ram_size
) {
74 error_setg(errp
, "not enough space, currently 0x%" PRIx64
75 " in use of total space for memory devices 0x" RAM_ADDR_FMT
,
76 used_region_size
, ms
->maxram_size
- ms
->ram_size
);
82 static uint64_t memory_device_get_free_addr(MachineState
*ms
,
84 uint64_t align
, uint64_t size
,
87 GSList
*list
= NULL
, *item
;
88 Range as
, new = range_empty
;
90 range_init_nofail(&as
, ms
->device_memory
->base
,
91 memory_region_size(&ms
->device_memory
->mr
));
93 /* start of address space indicates the maximum alignment we expect */
94 if (!QEMU_IS_ALIGNED(range_lob(&as
), align
)) {
95 warn_report("the alignment (0x%" PRIx64
") exceeds the expected"
96 " maximum alignment, memory will get fragmented and not"
97 " all 'maxmem' might be usable for memory devices.",
101 if (hint
&& !QEMU_IS_ALIGNED(*hint
, align
)) {
102 error_setg(errp
, "address must be aligned to 0x%" PRIx64
" bytes",
107 if (!QEMU_IS_ALIGNED(size
, align
)) {
108 error_setg(errp
, "backend memory size must be multiple of 0x%"
114 if (range_init(&new, *hint
, size
) || !range_contains_range(&as
, &new)) {
115 error_setg(errp
, "can't add memory device [0x%" PRIx64
":0x%" PRIx64
116 "], usable range for memory devices [0x%" PRIx64
":0x%"
117 PRIx64
"]", *hint
, size
, range_lob(&as
),
122 if (range_init(&new, QEMU_ALIGN_UP(range_lob(&as
), align
), size
)) {
123 error_setg(errp
, "can't add memory device, device too big");
128 /* find address range that will fit new memory device */
129 object_child_foreach(OBJECT(ms
), memory_device_build_list
, &list
);
130 for (item
= list
; item
; item
= g_slist_next(item
)) {
131 const MemoryDeviceState
*md
= item
->data
;
132 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(OBJECT(md
));
136 range_init_nofail(&tmp
, mdc
->get_addr(md
),
137 memory_device_get_region_size(md
, &error_abort
));
139 if (range_overlaps_range(&tmp
, &new)) {
141 const DeviceState
*d
= DEVICE(md
);
142 error_setg(errp
, "address range conflicts with memory device"
143 " id='%s'", d
->id
? d
->id
: "(unnamed)");
147 next_addr
= QEMU_ALIGN_UP(range_upb(&tmp
) + 1, align
);
148 if (!next_addr
|| range_init(&new, next_addr
, range_size(&new))) {
149 range_make_empty(&new);
152 } else if (range_lob(&tmp
) > range_upb(&new)) {
157 if (!range_contains_range(&as
, &new)) {
158 error_setg(errp
, "could not find position in guest address space for "
159 "memory device - memory fragmented due to alignments");
163 return range_lob(&new);
166 MemoryDeviceInfoList
*qmp_memory_device_list(void)
168 GSList
*devices
= NULL
, *item
;
169 MemoryDeviceInfoList
*list
= NULL
, **tail
= &list
;
171 object_child_foreach(qdev_get_machine(), memory_device_build_list
,
174 for (item
= devices
; item
; item
= g_slist_next(item
)) {
175 const MemoryDeviceState
*md
= MEMORY_DEVICE(item
->data
);
176 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(item
->data
);
177 MemoryDeviceInfo
*info
= g_new0(MemoryDeviceInfo
, 1);
179 mdc
->fill_device_info(md
, info
);
181 QAPI_LIST_APPEND(tail
, info
);
184 g_slist_free(devices
);
189 static int memory_device_plugged_size(Object
*obj
, void *opaque
)
191 uint64_t *size
= opaque
;
193 if (object_dynamic_cast(obj
, TYPE_MEMORY_DEVICE
)) {
194 const DeviceState
*dev
= DEVICE(obj
);
195 const MemoryDeviceState
*md
= MEMORY_DEVICE(obj
);
196 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(obj
);
199 *size
+= mdc
->get_plugged_size(md
, &error_abort
);
203 object_child_foreach(obj
, memory_device_plugged_size
, opaque
);
207 uint64_t get_plugged_memory_size(void)
211 memory_device_plugged_size(qdev_get_machine(), &size
);
216 void memory_device_pre_plug(MemoryDeviceState
*md
, MachineState
*ms
,
217 const uint64_t *legacy_align
, Error
**errp
)
219 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(md
);
220 Error
*local_err
= NULL
;
221 uint64_t addr
, align
= 0;
224 if (!ms
->device_memory
) {
225 error_setg(errp
, "the configuration is not prepared for memory devices"
226 " (e.g., for memory hotplug), consider specifying the"
231 mr
= mdc
->get_memory_region(md
, &local_err
);
236 memory_device_check_addable(ms
, mr
, &local_err
);
242 align
= *legacy_align
;
244 if (mdc
->get_min_alignment
) {
245 align
= mdc
->get_min_alignment(md
);
247 align
= MAX(align
, memory_region_get_alignment(mr
));
249 addr
= mdc
->get_addr(md
);
250 addr
= memory_device_get_free_addr(ms
, !addr
? NULL
: &addr
, align
,
251 memory_region_size(mr
), &local_err
);
255 mdc
->set_addr(md
, addr
, &local_err
);
257 trace_memory_device_pre_plug(DEVICE(md
)->id
? DEVICE(md
)->id
: "",
261 error_propagate(errp
, local_err
);
264 void memory_device_plug(MemoryDeviceState
*md
, MachineState
*ms
)
266 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(md
);
267 const uint64_t addr
= mdc
->get_addr(md
);
271 * We expect that a previous call to memory_device_pre_plug() succeeded, so
272 * it can't fail at this point.
274 mr
= mdc
->get_memory_region(md
, &error_abort
);
275 g_assert(ms
->device_memory
);
277 ms
->device_memory
->used_region_size
+= memory_region_size(mr
);
278 memory_region_add_subregion(&ms
->device_memory
->mr
,
279 addr
- ms
->device_memory
->base
, mr
);
280 trace_memory_device_plug(DEVICE(md
)->id
? DEVICE(md
)->id
: "", addr
);
283 void memory_device_unplug(MemoryDeviceState
*md
, MachineState
*ms
)
285 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(md
);
289 * We expect that a previous call to memory_device_pre_plug() succeeded, so
290 * it can't fail at this point.
292 mr
= mdc
->get_memory_region(md
, &error_abort
);
293 g_assert(ms
->device_memory
);
295 memory_region_del_subregion(&ms
->device_memory
->mr
, mr
);
296 ms
->device_memory
->used_region_size
-= memory_region_size(mr
);
297 trace_memory_device_unplug(DEVICE(md
)->id
? DEVICE(md
)->id
: "",
301 uint64_t memory_device_get_region_size(const MemoryDeviceState
*md
,
304 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(md
);
307 /* dropping const here is fine as we don't touch the memory region */
308 mr
= mdc
->get_memory_region((MemoryDeviceState
*)md
, errp
);
313 return memory_region_size(mr
);
316 void machine_memory_devices_init(MachineState
*ms
, hwaddr base
, uint64_t size
)
319 g_assert(!ms
->device_memory
);
320 ms
->device_memory
= g_new0(DeviceMemoryState
, 1);
321 ms
->device_memory
->base
= base
;
323 memory_region_init(&ms
->device_memory
->mr
, OBJECT(ms
), "device-memory",
325 memory_region_add_subregion(get_system_memory(), ms
->device_memory
->base
,
326 &ms
->device_memory
->mr
);
329 static const TypeInfo memory_device_info
= {
330 .name
= TYPE_MEMORY_DEVICE
,
331 .parent
= TYPE_INTERFACE
,
332 .class_size
= sizeof(MemoryDeviceClass
),
335 static void memory_device_register_types(void)
337 type_register_static(&memory_device_info
);
340 type_init(memory_device_register_types
)