blob: 4944f0faf9d44ecfa7dedb28ddcef013c6a6e5ef [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"
25
Igor Mammedov6f2e2732014-06-16 19:12:25 +020026int 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
Igor Mammedov0cd03d82014-06-02 15:25:14 +020065static 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
81int 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 }
106out:
107 g_free(bitmap);
108 return slot;
109}
110
Igor Mammedov0b312572014-06-02 15:25:13 +0200111static 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
125static 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
140uint64_t pc_dimm_get_free_addr(uint64_t address_space_start,
141 uint64_t address_space_size,
Igor Mammedov92a37a02014-10-31 16:38:36 +0000142 uint64_t *hint, uint64_t align, uint64_t size,
Igor Mammedov0b312572014-06-02 15:25:13 +0200143 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
Igor Mammedov9b79a762014-06-30 12:43:29 +0200149 if (!address_space_size) {
150 error_setg(errp, "memory hotplug is not enabled, "
151 "please add maxmem option");
152 goto out;
153 }
154
Igor Mammedov92a37a02014-10-31 16:38:36 +0000155 if (hint && QEMU_ALIGN_UP(*hint, align) != *hint) {
156 error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes",
157 align);
158 goto out;
159 }
160
161 if (QEMU_ALIGN_UP(size, align) != size) {
162 error_setg(errp, "backend memory size must be multiple of 0x%"
163 PRIx64, align);
164 goto out;
165 }
166
Igor Mammedov9b79a762014-06-30 12:43:29 +0200167 assert(address_space_end > address_space_start);
Igor Mammedov0b312572014-06-02 15:25:13 +0200168 object_child_foreach(qdev_get_machine(), pc_dimm_built_list, &list);
169
170 if (hint) {
171 new_addr = *hint;
172 } else {
173 new_addr = address_space_start;
174 }
175
176 /* find address range that will fit new DIMM */
177 for (item = list; item; item = g_slist_next(item)) {
178 PCDIMMDevice *dimm = item->data;
179 uint64_t dimm_size = object_property_get_int(OBJECT(dimm),
180 PC_DIMM_SIZE_PROP,
181 errp);
182 if (errp && *errp) {
183 goto out;
184 }
185
186 if (ranges_overlap(dimm->addr, dimm_size, new_addr, size)) {
187 if (hint) {
188 DeviceState *d = DEVICE(dimm);
189 error_setg(errp, "address range conflicts with '%s'", d->id);
190 goto out;
191 }
192 new_addr = dimm->addr + dimm_size;
193 }
194 }
195 ret = new_addr;
196
197 if (new_addr < address_space_start) {
198 error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
199 "] at 0x%" PRIx64, new_addr, size, address_space_start);
200 } else if ((new_addr + size) > address_space_end) {
201 error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
202 "] beyond 0x%" PRIx64, new_addr, size, address_space_end);
203 }
204
205out:
206 g_slist_free(list);
207 return ret;
208}
Vasilis Liaskovitis10b7e742014-06-02 15:25:05 +0200209
210static Property pc_dimm_properties[] = {
211 DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0),
212 DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0),
213 DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot,
214 PC_DIMM_UNASSIGNED_SLOT),
215 DEFINE_PROP_END_OF_LIST(),
216};
217
218static void pc_dimm_get_size(Object *obj, Visitor *v, void *opaque,
219 const char *name, Error **errp)
220{
221 int64_t value;
222 MemoryRegion *mr;
223 PCDIMMDevice *dimm = PC_DIMM(obj);
224
225 mr = host_memory_backend_get_memory(dimm->hostmem, errp);
226 value = memory_region_size(mr);
227
228 visit_type_int(v, &value, name, errp);
229}
230
Igor Mammedov7bb5d6a2014-06-02 15:25:07 +0200231static void pc_dimm_check_memdev_is_busy(Object *obj, const char *name,
232 Object *val, Error **errp)
233{
234 MemoryRegion *mr;
235
236 mr = host_memory_backend_get_memory(MEMORY_BACKEND(val), errp);
237 if (memory_region_is_mapped(mr)) {
238 char *path = object_get_canonical_path_component(val);
239 error_setg(errp, "can't use already busy memdev: %s", path);
240 g_free(path);
241 } else {
242 qdev_prop_allow_set_link_before_realize(obj, name, val, errp);
243 }
244}
245
Vasilis Liaskovitis10b7e742014-06-02 15:25:05 +0200246static void pc_dimm_init(Object *obj)
247{
248 PCDIMMDevice *dimm = PC_DIMM(obj);
249
250 object_property_add(obj, PC_DIMM_SIZE_PROP, "int", pc_dimm_get_size,
251 NULL, NULL, NULL, &error_abort);
252 object_property_add_link(obj, PC_DIMM_MEMDEV_PROP, TYPE_MEMORY_BACKEND,
253 (Object **)&dimm->hostmem,
Igor Mammedov7bb5d6a2014-06-02 15:25:07 +0200254 pc_dimm_check_memdev_is_busy,
Vasilis Liaskovitis10b7e742014-06-02 15:25:05 +0200255 OBJ_PROP_LINK_UNREF_ON_RELEASE,
256 &error_abort);
257}
258
259static void pc_dimm_realize(DeviceState *dev, Error **errp)
260{
261 PCDIMMDevice *dimm = PC_DIMM(dev);
262
263 if (!dimm->hostmem) {
264 error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set");
265 return;
266 }
zhanghailiangfc50ff02014-09-26 17:16:12 +0800267 if ((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) {
Michael S. Tsirkin988eba02014-08-04 14:21:59 +0200268 error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %"
269 PRIu32 "' which exceeds the number of numa nodes: %d",
270 dimm->node, nb_numa_nodes);
Hu Taocfe0ffd2014-08-04 16:16:08 +0800271 return;
272 }
Vasilis Liaskovitis10b7e742014-06-02 15:25:05 +0200273}
274
275static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm)
276{
277 return host_memory_backend_get_memory(dimm->hostmem, &error_abort);
278}
279
280static void pc_dimm_class_init(ObjectClass *oc, void *data)
281{
282 DeviceClass *dc = DEVICE_CLASS(oc);
283 PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
284
285 dc->realize = pc_dimm_realize;
286 dc->props = pc_dimm_properties;
287
288 ddc->get_memory_region = pc_dimm_get_memory_region;
289}
290
291static TypeInfo pc_dimm_info = {
292 .name = TYPE_PC_DIMM,
293 .parent = TYPE_DEVICE,
294 .instance_size = sizeof(PCDIMMDevice),
295 .instance_init = pc_dimm_init,
296 .class_init = pc_dimm_class_init,
297 .class_size = sizeof(PCDIMMDeviceClass),
298};
299
300static void pc_dimm_register_types(void)
301{
302 type_register_static(&pc_dimm_info);
303}
304
305type_init(pc_dimm_register_types)