]> git.proxmox.com Git - mirror_qemu.git/blame - hw/mem/pc-dimm.c
Merge remote-tracking branch 'remotes/awilliam/tags/vfio-update-20150302.0' into...
[mirror_qemu.git] / hw / mem / pc-dimm.c
CommitLineData
10b7e74b
VL
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"
0b312571 24#include "qemu/range.h"
e35704ba 25#include "sysemu/numa.h"
9967c949 26
37153450
BR
27typedef struct pc_dimms_capacity {
28 uint64_t size;
29 Error **errp;
30} pc_dimms_capacity;
31
32static int pc_existing_dimms_capacity_internal(Object *obj, void *opaque)
9967c949 33{
37153450
BR
34 pc_dimms_capacity *cap = opaque;
35 uint64_t *size = &cap->size;
9967c949
BR
36
37 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
38 DeviceState *dev = DEVICE(obj);
39
40 if (dev->realized) {
41 (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
37153450 42 cap->errp);
9967c949
BR
43 }
44
37153450 45 if (cap->errp && *cap->errp) {
9967c949
BR
46 return 1;
47 }
48 }
37153450 49 object_child_foreach(obj, pc_existing_dimms_capacity_internal, opaque);
9967c949
BR
50 return 0;
51}
0b312571 52
37153450
BR
53uint64_t pc_existing_dimms_capacity(Error **errp)
54{
55 pc_dimms_capacity cap;
56
57 cap.size = 0;
58 cap.errp = errp;
59
60 pc_existing_dimms_capacity_internal(qdev_get_machine(), &cap);
61 return cap.size;
62}
63
6f2e2730
IM
64int qmp_pc_dimm_device_list(Object *obj, void *opaque)
65{
66 MemoryDeviceInfoList ***prev = opaque;
67
68 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
69 DeviceState *dev = DEVICE(obj);
70
71 if (dev->realized) {
72 MemoryDeviceInfoList *elem = g_new0(MemoryDeviceInfoList, 1);
73 MemoryDeviceInfo *info = g_new0(MemoryDeviceInfo, 1);
74 PCDIMMDeviceInfo *di = g_new0(PCDIMMDeviceInfo, 1);
75 DeviceClass *dc = DEVICE_GET_CLASS(obj);
76 PCDIMMDevice *dimm = PC_DIMM(obj);
77
78 if (dev->id) {
79 di->has_id = true;
80 di->id = g_strdup(dev->id);
81 }
82 di->hotplugged = dev->hotplugged;
83 di->hotpluggable = dc->hotpluggable;
84 di->addr = dimm->addr;
85 di->slot = dimm->slot;
86 di->node = dimm->node;
87 di->size = object_property_get_int(OBJECT(dimm), PC_DIMM_SIZE_PROP,
88 NULL);
89 di->memdev = object_get_canonical_path(OBJECT(dimm->hostmem));
90
91 info->dimm = di;
92 elem->value = info;
93 elem->next = NULL;
94 **prev = elem;
95 *prev = &elem->next;
96 }
97 }
98
99 object_child_foreach(obj, qmp_pc_dimm_device_list, opaque);
100 return 0;
101}
102
0cd03d89
IM
103static int pc_dimm_slot2bitmap(Object *obj, void *opaque)
104{
105 unsigned long *bitmap = opaque;
106
107 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
108 DeviceState *dev = DEVICE(obj);
109 if (dev->realized) { /* count only realized DIMMs */
110 PCDIMMDevice *d = PC_DIMM(obj);
111 set_bit(d->slot, bitmap);
112 }
113 }
114
115 object_child_foreach(obj, pc_dimm_slot2bitmap, opaque);
116 return 0;
117}
118
119int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp)
120{
121 unsigned long *bitmap = bitmap_new(max_slots);
122 int slot = 0;
123
124 object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap, bitmap);
125
126 /* check if requested slot is not occupied */
127 if (hint) {
128 if (*hint >= max_slots) {
129 error_setg(errp, "invalid slot# %d, should be less than %d",
130 *hint, max_slots);
131 } else if (!test_bit(*hint, bitmap)) {
132 slot = *hint;
133 } else {
134 error_setg(errp, "slot %d is busy", *hint);
135 }
136 goto out;
137 }
138
139 /* search for free slot */
140 slot = find_first_zero_bit(bitmap, max_slots);
141 if (slot == max_slots) {
142 error_setg(errp, "no free slots available");
143 }
144out:
145 g_free(bitmap);
146 return slot;
147}
148
0b312571
IM
149static gint pc_dimm_addr_sort(gconstpointer a, gconstpointer b)
150{
151 PCDIMMDevice *x = PC_DIMM(a);
152 PCDIMMDevice *y = PC_DIMM(b);
153 Int128 diff = int128_sub(int128_make64(x->addr), int128_make64(y->addr));
154
155 if (int128_lt(diff, int128_zero())) {
156 return -1;
157 } else if (int128_gt(diff, int128_zero())) {
158 return 1;
159 }
160 return 0;
161}
162
163static int pc_dimm_built_list(Object *obj, void *opaque)
164{
165 GSList **list = opaque;
166
167 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
168 DeviceState *dev = DEVICE(obj);
169 if (dev->realized) { /* only realized DIMMs matter */
170 *list = g_slist_insert_sorted(*list, dev, pc_dimm_addr_sort);
171 }
172 }
173
174 object_child_foreach(obj, pc_dimm_built_list, opaque);
175 return 0;
176}
177
178uint64_t pc_dimm_get_free_addr(uint64_t address_space_start,
179 uint64_t address_space_size,
92a37a04 180 uint64_t *hint, uint64_t align, uint64_t size,
0b312571
IM
181 Error **errp)
182{
183 GSList *list = NULL, *item;
184 uint64_t new_addr, ret = 0;
185 uint64_t address_space_end = address_space_start + address_space_size;
186
0c0de1b6
IM
187 g_assert(QEMU_ALIGN_UP(address_space_start, align) == address_space_start);
188 g_assert(QEMU_ALIGN_UP(address_space_size, align) == address_space_size);
189
9b79a76c
IM
190 if (!address_space_size) {
191 error_setg(errp, "memory hotplug is not enabled, "
192 "please add maxmem option");
193 goto out;
194 }
195
92a37a04
IM
196 if (hint && QEMU_ALIGN_UP(*hint, align) != *hint) {
197 error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes",
198 align);
199 goto out;
200 }
201
202 if (QEMU_ALIGN_UP(size, align) != size) {
203 error_setg(errp, "backend memory size must be multiple of 0x%"
204 PRIx64, align);
205 goto out;
206 }
207
9b79a76c 208 assert(address_space_end > address_space_start);
0b312571
IM
209 object_child_foreach(qdev_get_machine(), pc_dimm_built_list, &list);
210
211 if (hint) {
212 new_addr = *hint;
213 } else {
214 new_addr = address_space_start;
215 }
216
217 /* find address range that will fit new DIMM */
218 for (item = list; item; item = g_slist_next(item)) {
219 PCDIMMDevice *dimm = item->data;
220 uint64_t dimm_size = object_property_get_int(OBJECT(dimm),
221 PC_DIMM_SIZE_PROP,
222 errp);
223 if (errp && *errp) {
224 goto out;
225 }
226
227 if (ranges_overlap(dimm->addr, dimm_size, new_addr, size)) {
228 if (hint) {
229 DeviceState *d = DEVICE(dimm);
230 error_setg(errp, "address range conflicts with '%s'", d->id);
231 goto out;
232 }
0c0de1b6 233 new_addr = QEMU_ALIGN_UP(dimm->addr + dimm_size, align);
0b312571
IM
234 }
235 }
236 ret = new_addr;
237
238 if (new_addr < address_space_start) {
239 error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
240 "] at 0x%" PRIx64, new_addr, size, address_space_start);
241 } else if ((new_addr + size) > address_space_end) {
242 error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
243 "] beyond 0x%" PRIx64, new_addr, size, address_space_end);
244 }
245
246out:
247 g_slist_free(list);
248 return ret;
249}
10b7e74b
VL
250
251static Property pc_dimm_properties[] = {
252 DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0),
253 DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0),
254 DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot,
255 PC_DIMM_UNASSIGNED_SLOT),
256 DEFINE_PROP_END_OF_LIST(),
257};
258
259static void pc_dimm_get_size(Object *obj, Visitor *v, void *opaque,
260 const char *name, Error **errp)
261{
262 int64_t value;
263 MemoryRegion *mr;
264 PCDIMMDevice *dimm = PC_DIMM(obj);
265
266 mr = host_memory_backend_get_memory(dimm->hostmem, errp);
267 value = memory_region_size(mr);
268
269 visit_type_int(v, &value, name, errp);
270}
271
7bb5d6ad
IM
272static void pc_dimm_check_memdev_is_busy(Object *obj, const char *name,
273 Object *val, Error **errp)
274{
275 MemoryRegion *mr;
276
277 mr = host_memory_backend_get_memory(MEMORY_BACKEND(val), errp);
278 if (memory_region_is_mapped(mr)) {
279 char *path = object_get_canonical_path_component(val);
280 error_setg(errp, "can't use already busy memdev: %s", path);
281 g_free(path);
282 } else {
283 qdev_prop_allow_set_link_before_realize(obj, name, val, errp);
284 }
285}
286
10b7e74b
VL
287static void pc_dimm_init(Object *obj)
288{
289 PCDIMMDevice *dimm = PC_DIMM(obj);
290
291 object_property_add(obj, PC_DIMM_SIZE_PROP, "int", pc_dimm_get_size,
292 NULL, NULL, NULL, &error_abort);
293 object_property_add_link(obj, PC_DIMM_MEMDEV_PROP, TYPE_MEMORY_BACKEND,
294 (Object **)&dimm->hostmem,
7bb5d6ad 295 pc_dimm_check_memdev_is_busy,
10b7e74b
VL
296 OBJ_PROP_LINK_UNREF_ON_RELEASE,
297 &error_abort);
298}
299
300static void pc_dimm_realize(DeviceState *dev, Error **errp)
301{
302 PCDIMMDevice *dimm = PC_DIMM(dev);
303
304 if (!dimm->hostmem) {
305 error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set");
306 return;
307 }
fc50ff06 308 if ((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) {
988eba0f
MT
309 error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %"
310 PRIu32 "' which exceeds the number of numa nodes: %d",
311 dimm->node, nb_numa_nodes);
cfe0ffd0
HT
312 return;
313 }
10b7e74b
VL
314}
315
316static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm)
317{
318 return host_memory_backend_get_memory(dimm->hostmem, &error_abort);
319}
320
321static void pc_dimm_class_init(ObjectClass *oc, void *data)
322{
323 DeviceClass *dc = DEVICE_CLASS(oc);
324 PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
325
326 dc->realize = pc_dimm_realize;
327 dc->props = pc_dimm_properties;
328
329 ddc->get_memory_region = pc_dimm_get_memory_region;
330}
331
332static TypeInfo pc_dimm_info = {
333 .name = TYPE_PC_DIMM,
334 .parent = TYPE_DEVICE,
335 .instance_size = sizeof(PCDIMMDevice),
336 .instance_init = pc_dimm_init,
337 .class_init = pc_dimm_class_init,
338 .class_size = sizeof(PCDIMMDeviceClass),
339};
340
341static void pc_dimm_register_types(void)
342{
343 type_register_static(&pc_dimm_info);
344}
345
346type_init(pc_dimm_register_types)