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 "hw/mem/memory-device.h"
15 #include "qapi/error.h"
16 #include "hw/boards.h"
17 #include "qemu/range.h"
19 static gint
memory_device_addr_sort(gconstpointer a
, gconstpointer b
)
21 const MemoryDeviceState
*md_a
= MEMORY_DEVICE(a
);
22 const MemoryDeviceState
*md_b
= MEMORY_DEVICE(b
);
23 const MemoryDeviceClass
*mdc_a
= MEMORY_DEVICE_GET_CLASS(a
);
24 const MemoryDeviceClass
*mdc_b
= MEMORY_DEVICE_GET_CLASS(b
);
25 const uint64_t addr_a
= mdc_a
->get_addr(md_a
);
26 const uint64_t addr_b
= mdc_b
->get_addr(md_b
);
28 if (addr_a
> addr_b
) {
30 } else if (addr_a
< addr_b
) {
36 static int memory_device_build_list(Object
*obj
, void *opaque
)
38 GSList
**list
= opaque
;
40 if (object_dynamic_cast(obj
, TYPE_MEMORY_DEVICE
)) {
41 DeviceState
*dev
= DEVICE(obj
);
42 if (dev
->realized
) { /* only realized memory devices matter */
43 *list
= g_slist_insert_sorted(*list
, dev
, memory_device_addr_sort
);
47 object_child_foreach(obj
, memory_device_build_list
, opaque
);
51 MemoryDeviceInfoList
*qmp_memory_device_list(void)
53 GSList
*devices
= NULL
, *item
;
54 MemoryDeviceInfoList
*list
= NULL
, *prev
= NULL
;
56 object_child_foreach(qdev_get_machine(), memory_device_build_list
,
59 for (item
= devices
; item
; item
= g_slist_next(item
)) {
60 const MemoryDeviceState
*md
= MEMORY_DEVICE(item
->data
);
61 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(item
->data
);
62 MemoryDeviceInfoList
*elem
= g_new0(MemoryDeviceInfoList
, 1);
63 MemoryDeviceInfo
*info
= g_new0(MemoryDeviceInfo
, 1);
65 mdc
->fill_device_info(md
, info
);
77 g_slist_free(devices
);
82 static int memory_device_plugged_size(Object
*obj
, void *opaque
)
84 uint64_t *size
= opaque
;
86 if (object_dynamic_cast(obj
, TYPE_MEMORY_DEVICE
)) {
87 const DeviceState
*dev
= DEVICE(obj
);
88 const MemoryDeviceState
*md
= MEMORY_DEVICE(obj
);
89 const MemoryDeviceClass
*mdc
= MEMORY_DEVICE_GET_CLASS(obj
);
92 *size
+= mdc
->get_plugged_size(md
);
96 object_child_foreach(obj
, memory_device_plugged_size
, opaque
);
100 uint64_t get_plugged_memory_size(void)
104 memory_device_plugged_size(qdev_get_machine(), &size
);
109 static const TypeInfo memory_device_info
= {
110 .name
= TYPE_MEMORY_DEVICE
,
111 .parent
= TYPE_INTERFACE
,
112 .class_size
= sizeof(MemoryDeviceClass
),
115 static void memory_device_register_types(void)
117 type_register_static(&memory_device_info
);
120 type_init(memory_device_register_types
)