blob: e70633d29f04f6470385056d349aff2f025ee434 [file] [log] [blame]
Vasilis Liaskovitis10b7e742014-06-02 15:25:05 +02001/*
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"
Igor Mammedov0b312572014-06-02 15:25:13 +020024#include "qemu/range.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020025#include "sysemu/numa.h"
Bharata B Rao9967c942015-01-27 09:35:01 +053026
Bharata B Rao37153452015-01-27 09:35:02 +053027typedef 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)
Bharata B Rao9967c942015-01-27 09:35:01 +053033{
Bharata B Rao37153452015-01-27 09:35:02 +053034 pc_dimms_capacity *cap = opaque;
35 uint64_t *size = &cap->size;
Bharata B Rao9967c942015-01-27 09:35:01 +053036
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,
Bharata B Rao37153452015-01-27 09:35:02 +053042 cap->errp);
Bharata B Rao9967c942015-01-27 09:35:01 +053043 }
44
Bharata B Rao37153452015-01-27 09:35:02 +053045 if (cap->errp && *cap->errp) {
Bharata B Rao9967c942015-01-27 09:35:01 +053046 return 1;
47 }
48 }
Bharata B Rao37153452015-01-27 09:35:02 +053049 object_child_foreach(obj, pc_existing_dimms_capacity_internal, opaque);
Bharata B Rao9967c942015-01-27 09:35:01 +053050 return 0;
51}
Igor Mammedov0b312572014-06-02 15:25:13 +020052
Bharata B Rao37153452015-01-27 09:35:02 +053053uint64_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
Igor Mammedov6f2e2732014-06-16 19:12:25 +020064int 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
zhanghailiang87a45cf2014-11-17 13:11:08 +0800103ram_addr_t get_current_ram_size(void)
104{
105 MemoryDeviceInfoList *info_list = NULL;
106 MemoryDeviceInfoList **prev = &info_list;
107 MemoryDeviceInfoList *info;
108 ram_addr_t size = ram_size;
109
110 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
111 for (info = info_list; info; info = info->next) {
112 MemoryDeviceInfo *value = info->value;
113
114 if (value) {
115 switch (value->kind) {
116 case MEMORY_DEVICE_INFO_KIND_DIMM:
117 size += value->dimm->size;
118 break;
119 default:
120 break;
121 }
122 }
123 }
124 qapi_free_MemoryDeviceInfoList(info_list);
125
126 return size;
127}
128
Igor Mammedov0cd03d82014-06-02 15:25:14 +0200129static int pc_dimm_slot2bitmap(Object *obj, void *opaque)
130{
131 unsigned long *bitmap = opaque;
132
133 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
134 DeviceState *dev = DEVICE(obj);
135 if (dev->realized) { /* count only realized DIMMs */
136 PCDIMMDevice *d = PC_DIMM(obj);
137 set_bit(d->slot, bitmap);
138 }
139 }
140
141 object_child_foreach(obj, pc_dimm_slot2bitmap, opaque);
142 return 0;
143}
144
145int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp)
146{
147 unsigned long *bitmap = bitmap_new(max_slots);
148 int slot = 0;
149
150 object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap, bitmap);
151
152 /* check if requested slot is not occupied */
153 if (hint) {
154 if (*hint >= max_slots) {
155 error_setg(errp, "invalid slot# %d, should be less than %d",
156 *hint, max_slots);
157 } else if (!test_bit(*hint, bitmap)) {
158 slot = *hint;
159 } else {
160 error_setg(errp, "slot %d is busy", *hint);
161 }
162 goto out;
163 }
164
165 /* search for free slot */
166 slot = find_first_zero_bit(bitmap, max_slots);
167 if (slot == max_slots) {
168 error_setg(errp, "no free slots available");
169 }
170out:
171 g_free(bitmap);
172 return slot;
173}
174
Igor Mammedov0b312572014-06-02 15:25:13 +0200175static gint pc_dimm_addr_sort(gconstpointer a, gconstpointer b)
176{
177 PCDIMMDevice *x = PC_DIMM(a);
178 PCDIMMDevice *y = PC_DIMM(b);
179 Int128 diff = int128_sub(int128_make64(x->addr), int128_make64(y->addr));
180
181 if (int128_lt(diff, int128_zero())) {
182 return -1;
183 } else if (int128_gt(diff, int128_zero())) {
184 return 1;
185 }
186 return 0;
187}
188
189static int pc_dimm_built_list(Object *obj, void *opaque)
190{
191 GSList **list = opaque;
192
193 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
194 DeviceState *dev = DEVICE(obj);
195 if (dev->realized) { /* only realized DIMMs matter */
196 *list = g_slist_insert_sorted(*list, dev, pc_dimm_addr_sort);
197 }
198 }
199
200 object_child_foreach(obj, pc_dimm_built_list, opaque);
201 return 0;
202}
203
204uint64_t pc_dimm_get_free_addr(uint64_t address_space_start,
205 uint64_t address_space_size,
Igor Mammedov92a37a02014-10-31 16:38:36 +0000206 uint64_t *hint, uint64_t align, uint64_t size,
Igor Mammedov0b312572014-06-02 15:25:13 +0200207 Error **errp)
208{
209 GSList *list = NULL, *item;
210 uint64_t new_addr, ret = 0;
211 uint64_t address_space_end = address_space_start + address_space_size;
212
Igor Mammedov0c0de1b2014-10-31 16:38:40 +0000213 g_assert(QEMU_ALIGN_UP(address_space_start, align) == address_space_start);
Igor Mammedov0c0de1b2014-10-31 16:38:40 +0000214
Igor Mammedov9b79a762014-06-30 12:43:29 +0200215 if (!address_space_size) {
216 error_setg(errp, "memory hotplug is not enabled, "
217 "please add maxmem option");
218 goto out;
219 }
220
Igor Mammedov92a37a02014-10-31 16:38:36 +0000221 if (hint && QEMU_ALIGN_UP(*hint, align) != *hint) {
222 error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes",
223 align);
224 goto out;
225 }
226
227 if (QEMU_ALIGN_UP(size, align) != size) {
228 error_setg(errp, "backend memory size must be multiple of 0x%"
229 PRIx64, align);
230 goto out;
231 }
232
Igor Mammedov9b79a762014-06-30 12:43:29 +0200233 assert(address_space_end > address_space_start);
Igor Mammedov0b312572014-06-02 15:25:13 +0200234 object_child_foreach(qdev_get_machine(), pc_dimm_built_list, &list);
235
236 if (hint) {
237 new_addr = *hint;
238 } else {
239 new_addr = address_space_start;
240 }
241
242 /* find address range that will fit new DIMM */
243 for (item = list; item; item = g_slist_next(item)) {
244 PCDIMMDevice *dimm = item->data;
245 uint64_t dimm_size = object_property_get_int(OBJECT(dimm),
246 PC_DIMM_SIZE_PROP,
247 errp);
248 if (errp && *errp) {
249 goto out;
250 }
251
252 if (ranges_overlap(dimm->addr, dimm_size, new_addr, size)) {
253 if (hint) {
254 DeviceState *d = DEVICE(dimm);
255 error_setg(errp, "address range conflicts with '%s'", d->id);
256 goto out;
257 }
Igor Mammedov0c0de1b2014-10-31 16:38:40 +0000258 new_addr = QEMU_ALIGN_UP(dimm->addr + dimm_size, align);
Igor Mammedov0b312572014-06-02 15:25:13 +0200259 }
260 }
261 ret = new_addr;
262
263 if (new_addr < address_space_start) {
264 error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
265 "] at 0x%" PRIx64, new_addr, size, address_space_start);
266 } else if ((new_addr + size) > address_space_end) {
267 error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
268 "] beyond 0x%" PRIx64, new_addr, size, address_space_end);
269 }
270
271out:
272 g_slist_free(list);
273 return ret;
274}
Vasilis Liaskovitis10b7e742014-06-02 15:25:05 +0200275
276static Property pc_dimm_properties[] = {
277 DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0),
278 DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0),
279 DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot,
280 PC_DIMM_UNASSIGNED_SLOT),
281 DEFINE_PROP_END_OF_LIST(),
282};
283
284static void pc_dimm_get_size(Object *obj, Visitor *v, void *opaque,
285 const char *name, Error **errp)
286{
287 int64_t value;
288 MemoryRegion *mr;
289 PCDIMMDevice *dimm = PC_DIMM(obj);
290
291 mr = host_memory_backend_get_memory(dimm->hostmem, errp);
292 value = memory_region_size(mr);
293
294 visit_type_int(v, &value, name, errp);
295}
296
Igor Mammedov7bb5d6a2014-06-02 15:25:07 +0200297static void pc_dimm_check_memdev_is_busy(Object *obj, const char *name,
298 Object *val, Error **errp)
299{
300 MemoryRegion *mr;
301
302 mr = host_memory_backend_get_memory(MEMORY_BACKEND(val), errp);
303 if (memory_region_is_mapped(mr)) {
304 char *path = object_get_canonical_path_component(val);
305 error_setg(errp, "can't use already busy memdev: %s", path);
306 g_free(path);
307 } else {
308 qdev_prop_allow_set_link_before_realize(obj, name, val, errp);
309 }
310}
311
Vasilis Liaskovitis10b7e742014-06-02 15:25:05 +0200312static void pc_dimm_init(Object *obj)
313{
314 PCDIMMDevice *dimm = PC_DIMM(obj);
315
316 object_property_add(obj, PC_DIMM_SIZE_PROP, "int", pc_dimm_get_size,
317 NULL, NULL, NULL, &error_abort);
318 object_property_add_link(obj, PC_DIMM_MEMDEV_PROP, TYPE_MEMORY_BACKEND,
319 (Object **)&dimm->hostmem,
Igor Mammedov7bb5d6a2014-06-02 15:25:07 +0200320 pc_dimm_check_memdev_is_busy,
Vasilis Liaskovitis10b7e742014-06-02 15:25:05 +0200321 OBJ_PROP_LINK_UNREF_ON_RELEASE,
322 &error_abort);
323}
324
325static void pc_dimm_realize(DeviceState *dev, Error **errp)
326{
327 PCDIMMDevice *dimm = PC_DIMM(dev);
328
329 if (!dimm->hostmem) {
330 error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set");
331 return;
332 }
zhanghailiangfc50ff02014-09-26 17:16:12 +0800333 if ((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) {
Michael S. Tsirkin988eba02014-08-04 14:21:59 +0200334 error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %"
335 PRIu32 "' which exceeds the number of numa nodes: %d",
336 dimm->node, nb_numa_nodes);
Hu Taocfe0ffd2014-08-04 16:16:08 +0800337 return;
338 }
Vasilis Liaskovitis10b7e742014-06-02 15:25:05 +0200339}
340
341static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm)
342{
343 return host_memory_backend_get_memory(dimm->hostmem, &error_abort);
344}
345
346static void pc_dimm_class_init(ObjectClass *oc, void *data)
347{
348 DeviceClass *dc = DEVICE_CLASS(oc);
349 PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
350
351 dc->realize = pc_dimm_realize;
352 dc->props = pc_dimm_properties;
Paulo Vitalbdd09772015-03-10 12:25:51 +0100353 dc->desc = "DIMM memory module";
Vasilis Liaskovitis10b7e742014-06-02 15:25:05 +0200354
355 ddc->get_memory_region = pc_dimm_get_memory_region;
356}
357
358static TypeInfo pc_dimm_info = {
359 .name = TYPE_PC_DIMM,
360 .parent = TYPE_DEVICE,
361 .instance_size = sizeof(PCDIMMDevice),
362 .instance_init = pc_dimm_init,
363 .class_init = pc_dimm_class_init,
364 .class_size = sizeof(PCDIMMDeviceClass),
365};
366
367static void pc_dimm_register_types(void)
368{
369 type_register_static(&pc_dimm_info);
370}
371
372type_init(pc_dimm_register_types)