qmp: add query-memory-devices command
[qemu.git] / hw / mem / pc-dimm.c
1 /*
2 * Dimm device for Memory Hotplug
3 *
4 * Copyright ProfitBricks GmbH 2012
5 * Copyright (C) 2014 Red Hat Inc
6 *
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.
11 *
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.
16 *
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/>
19 */
20
21 #include "hw/mem/pc-dimm.h"
22 #include "qemu/config-file.h"
23 #include "qapi/visitor.h"
24 #include "qemu/range.h"
25
26 int qmp_pc_dimm_device_list(Object *obj, void *opaque)
27 {
28 MemoryDeviceInfoList ***prev = opaque;
29
30 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
31 DeviceState *dev = DEVICE(obj);
32
33 if (dev->realized) {
34 MemoryDeviceInfoList *elem = g_new0(MemoryDeviceInfoList, 1);
35 MemoryDeviceInfo *info = g_new0(MemoryDeviceInfo, 1);
36 PCDIMMDeviceInfo *di = g_new0(PCDIMMDeviceInfo, 1);
37 DeviceClass *dc = DEVICE_GET_CLASS(obj);
38 PCDIMMDevice *dimm = PC_DIMM(obj);
39
40 if (dev->id) {
41 di->has_id = true;
42 di->id = g_strdup(dev->id);
43 }
44 di->hotplugged = dev->hotplugged;
45 di->hotpluggable = dc->hotpluggable;
46 di->addr = dimm->addr;
47 di->slot = dimm->slot;
48 di->node = dimm->node;
49 di->size = object_property_get_int(OBJECT(dimm), PC_DIMM_SIZE_PROP,
50 NULL);
51 di->memdev = object_get_canonical_path(OBJECT(dimm->hostmem));
52
53 info->dimm = di;
54 elem->value = info;
55 elem->next = NULL;
56 **prev = elem;
57 *prev = &elem->next;
58 }
59 }
60
61 object_child_foreach(obj, qmp_pc_dimm_device_list, opaque);
62 return 0;
63 }
64
65 static int pc_dimm_slot2bitmap(Object *obj, void *opaque)
66 {
67 unsigned long *bitmap = opaque;
68
69 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
70 DeviceState *dev = DEVICE(obj);
71 if (dev->realized) { /* count only realized DIMMs */
72 PCDIMMDevice *d = PC_DIMM(obj);
73 set_bit(d->slot, bitmap);
74 }
75 }
76
77 object_child_foreach(obj, pc_dimm_slot2bitmap, opaque);
78 return 0;
79 }
80
81 int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp)
82 {
83 unsigned long *bitmap = bitmap_new(max_slots);
84 int slot = 0;
85
86 object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap, bitmap);
87
88 /* check if requested slot is not occupied */
89 if (hint) {
90 if (*hint >= max_slots) {
91 error_setg(errp, "invalid slot# %d, should be less than %d",
92 *hint, max_slots);
93 } else if (!test_bit(*hint, bitmap)) {
94 slot = *hint;
95 } else {
96 error_setg(errp, "slot %d is busy", *hint);
97 }
98 goto out;
99 }
100
101 /* search for free slot */
102 slot = find_first_zero_bit(bitmap, max_slots);
103 if (slot == max_slots) {
104 error_setg(errp, "no free slots available");
105 }
106 out:
107 g_free(bitmap);
108 return slot;
109 }
110
111 static gint pc_dimm_addr_sort(gconstpointer a, gconstpointer b)
112 {
113 PCDIMMDevice *x = PC_DIMM(a);
114 PCDIMMDevice *y = PC_DIMM(b);
115 Int128 diff = int128_sub(int128_make64(x->addr), int128_make64(y->addr));
116
117 if (int128_lt(diff, int128_zero())) {
118 return -1;
119 } else if (int128_gt(diff, int128_zero())) {
120 return 1;
121 }
122 return 0;
123 }
124
125 static int pc_dimm_built_list(Object *obj, void *opaque)
126 {
127 GSList **list = opaque;
128
129 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
130 DeviceState *dev = DEVICE(obj);
131 if (dev->realized) { /* only realized DIMMs matter */
132 *list = g_slist_insert_sorted(*list, dev, pc_dimm_addr_sort);
133 }
134 }
135
136 object_child_foreach(obj, pc_dimm_built_list, opaque);
137 return 0;
138 }
139
140 uint64_t pc_dimm_get_free_addr(uint64_t address_space_start,
141 uint64_t address_space_size,
142 uint64_t *hint, uint64_t size,
143 Error **errp)
144 {
145 GSList *list = NULL, *item;
146 uint64_t new_addr, ret = 0;
147 uint64_t address_space_end = address_space_start + address_space_size;
148
149 assert(address_space_end > address_space_size);
150 object_child_foreach(qdev_get_machine(), pc_dimm_built_list, &list);
151
152 if (hint) {
153 new_addr = *hint;
154 } else {
155 new_addr = address_space_start;
156 }
157
158 /* find address range that will fit new DIMM */
159 for (item = list; item; item = g_slist_next(item)) {
160 PCDIMMDevice *dimm = item->data;
161 uint64_t dimm_size = object_property_get_int(OBJECT(dimm),
162 PC_DIMM_SIZE_PROP,
163 errp);
164 if (errp && *errp) {
165 goto out;
166 }
167
168 if (ranges_overlap(dimm->addr, dimm_size, new_addr, size)) {
169 if (hint) {
170 DeviceState *d = DEVICE(dimm);
171 error_setg(errp, "address range conflicts with '%s'", d->id);
172 goto out;
173 }
174 new_addr = dimm->addr + dimm_size;
175 }
176 }
177 ret = new_addr;
178
179 if (new_addr < address_space_start) {
180 error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
181 "] at 0x%" PRIx64, new_addr, size, address_space_start);
182 } else if ((new_addr + size) > address_space_end) {
183 error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
184 "] beyond 0x%" PRIx64, new_addr, size, address_space_end);
185 }
186
187 out:
188 g_slist_free(list);
189 return ret;
190 }
191
192 static Property pc_dimm_properties[] = {
193 DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0),
194 DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0),
195 DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot,
196 PC_DIMM_UNASSIGNED_SLOT),
197 DEFINE_PROP_END_OF_LIST(),
198 };
199
200 static void pc_dimm_get_size(Object *obj, Visitor *v, void *opaque,
201 const char *name, Error **errp)
202 {
203 int64_t value;
204 MemoryRegion *mr;
205 PCDIMMDevice *dimm = PC_DIMM(obj);
206
207 mr = host_memory_backend_get_memory(dimm->hostmem, errp);
208 value = memory_region_size(mr);
209
210 visit_type_int(v, &value, name, errp);
211 }
212
213 static void pc_dimm_check_memdev_is_busy(Object *obj, const char *name,
214 Object *val, Error **errp)
215 {
216 MemoryRegion *mr;
217
218 mr = host_memory_backend_get_memory(MEMORY_BACKEND(val), errp);
219 if (memory_region_is_mapped(mr)) {
220 char *path = object_get_canonical_path_component(val);
221 error_setg(errp, "can't use already busy memdev: %s", path);
222 g_free(path);
223 } else {
224 qdev_prop_allow_set_link_before_realize(obj, name, val, errp);
225 }
226 }
227
228 static void pc_dimm_init(Object *obj)
229 {
230 PCDIMMDevice *dimm = PC_DIMM(obj);
231
232 object_property_add(obj, PC_DIMM_SIZE_PROP, "int", pc_dimm_get_size,
233 NULL, NULL, NULL, &error_abort);
234 object_property_add_link(obj, PC_DIMM_MEMDEV_PROP, TYPE_MEMORY_BACKEND,
235 (Object **)&dimm->hostmem,
236 pc_dimm_check_memdev_is_busy,
237 OBJ_PROP_LINK_UNREF_ON_RELEASE,
238 &error_abort);
239 }
240
241 static void pc_dimm_realize(DeviceState *dev, Error **errp)
242 {
243 PCDIMMDevice *dimm = PC_DIMM(dev);
244
245 if (!dimm->hostmem) {
246 error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set");
247 return;
248 }
249 }
250
251 static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm)
252 {
253 return host_memory_backend_get_memory(dimm->hostmem, &error_abort);
254 }
255
256 static void pc_dimm_class_init(ObjectClass *oc, void *data)
257 {
258 DeviceClass *dc = DEVICE_CLASS(oc);
259 PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
260
261 dc->realize = pc_dimm_realize;
262 dc->props = pc_dimm_properties;
263
264 ddc->get_memory_region = pc_dimm_get_memory_region;
265 }
266
267 static TypeInfo pc_dimm_info = {
268 .name = TYPE_PC_DIMM,
269 .parent = TYPE_DEVICE,
270 .instance_size = sizeof(PCDIMMDevice),
271 .instance_init = pc_dimm_init,
272 .class_init = pc_dimm_class_init,
273 .class_size = sizeof(PCDIMMDeviceClass),
274 };
275
276 static void pc_dimm_register_types(void)
277 {
278 type_register_static(&pc_dimm_info);
279 }
280
281 type_init(pc_dimm_register_types)