xref: /openbmc/qemu/hw/mem/memory-device.c (revision 759bac67)
1 /*
2  * Memory Device Interface
3  *
4  * Copyright ProfitBricks GmbH 2012
5  * Copyright (C) 2014 Red Hat Inc
6  * Copyright (c) 2018 Red Hat Inc
7  *
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.
10  */
11 
12 #include "qemu/osdep.h"
13 #include "qemu/error-report.h"
14 #include "hw/mem/memory-device.h"
15 #include "qapi/error.h"
16 #include "hw/boards.h"
17 #include "qemu/range.h"
18 #include "hw/virtio/vhost.h"
19 #include "sysemu/kvm.h"
20 #include "exec/address-spaces.h"
21 #include "trace.h"
22 
23 static gint memory_device_addr_sort(gconstpointer a, gconstpointer b)
24 {
25     const MemoryDeviceState *md_a = MEMORY_DEVICE(a);
26     const MemoryDeviceState *md_b = MEMORY_DEVICE(b);
27     const MemoryDeviceClass *mdc_a = MEMORY_DEVICE_GET_CLASS(a);
28     const MemoryDeviceClass *mdc_b = MEMORY_DEVICE_GET_CLASS(b);
29     const uint64_t addr_a = mdc_a->get_addr(md_a);
30     const uint64_t addr_b = mdc_b->get_addr(md_b);
31 
32     if (addr_a > addr_b) {
33         return 1;
34     } else if (addr_a < addr_b) {
35         return -1;
36     }
37     return 0;
38 }
39 
40 static int memory_device_build_list(Object *obj, void *opaque)
41 {
42     GSList **list = opaque;
43 
44     if (object_dynamic_cast(obj, TYPE_MEMORY_DEVICE)) {
45         DeviceState *dev = DEVICE(obj);
46         if (dev->realized) { /* only realized memory devices matter */
47             *list = g_slist_insert_sorted(*list, dev, memory_device_addr_sort);
48         }
49     }
50 
51     object_child_foreach(obj, memory_device_build_list, opaque);
52     return 0;
53 }
54 
55 static unsigned int memory_device_get_memslots(MemoryDeviceState *md)
56 {
57     const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(md);
58 
59     if (mdc->get_memslots) {
60         return mdc->get_memslots(md);
61     }
62     return 1;
63 }
64 
65 static void memory_device_check_addable(MachineState *ms, MemoryDeviceState *md,
66                                         MemoryRegion *mr, Error **errp)
67 {
68     const uint64_t used_region_size = ms->device_memory->used_region_size;
69     const uint64_t size = memory_region_size(mr);
70     const unsigned int required_memslots = memory_device_get_memslots(md);
71 
72     /* we will need memory slots for kvm and vhost */
73     if (kvm_enabled() && kvm_get_free_memslots() < required_memslots) {
74         error_setg(errp, "hypervisor has not enough free memory slots left");
75         return;
76     }
77     if (vhost_get_free_memslots() < required_memslots) {
78         error_setg(errp, "a used vhost backend has not enough free memory slots left");
79         return;
80     }
81 
82     /* will we exceed the total amount of memory specified */
83     if (used_region_size + size < used_region_size ||
84         used_region_size + size > ms->maxram_size - ms->ram_size) {
85         error_setg(errp, "not enough space, currently 0x%" PRIx64
86                    " in use of total space for memory devices 0x" RAM_ADDR_FMT,
87                    used_region_size, ms->maxram_size - ms->ram_size);
88         return;
89     }
90 
91 }
92 
93 static uint64_t memory_device_get_free_addr(MachineState *ms,
94                                             const uint64_t *hint,
95                                             uint64_t align, uint64_t size,
96                                             Error **errp)
97 {
98     GSList *list = NULL, *item;
99     Range as, new = range_empty;
100 
101     range_init_nofail(&as, ms->device_memory->base,
102                       memory_region_size(&ms->device_memory->mr));
103 
104     /* start of address space indicates the maximum alignment we expect */
105     if (!QEMU_IS_ALIGNED(range_lob(&as), align)) {
106         warn_report("the alignment (0x%" PRIx64 ") exceeds the expected"
107                     " maximum alignment, memory will get fragmented and not"
108                     " all 'maxmem' might be usable for memory devices.",
109                     align);
110     }
111 
112     if (hint && !QEMU_IS_ALIGNED(*hint, align)) {
113         error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes",
114                    align);
115         return 0;
116     }
117 
118     if (!QEMU_IS_ALIGNED(size, align)) {
119         error_setg(errp, "backend memory size must be multiple of 0x%"
120                    PRIx64, align);
121         return 0;
122     }
123 
124     if (hint) {
125         if (range_init(&new, *hint, size) || !range_contains_range(&as, &new)) {
126             error_setg(errp, "can't add memory device [0x%" PRIx64 ":0x%" PRIx64
127                        "], usable range for memory devices [0x%" PRIx64 ":0x%"
128                        PRIx64 "]", *hint, size, range_lob(&as),
129                        range_size(&as));
130             return 0;
131         }
132     } else {
133         if (range_init(&new, QEMU_ALIGN_UP(range_lob(&as), align), size)) {
134             error_setg(errp, "can't add memory device, device too big");
135             return 0;
136         }
137     }
138 
139     /* find address range that will fit new memory device */
140     object_child_foreach(OBJECT(ms), memory_device_build_list, &list);
141     for (item = list; item; item = g_slist_next(item)) {
142         const MemoryDeviceState *md = item->data;
143         const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(OBJECT(md));
144         uint64_t next_addr;
145         Range tmp;
146 
147         range_init_nofail(&tmp, mdc->get_addr(md),
148                           memory_device_get_region_size(md, &error_abort));
149 
150         if (range_overlaps_range(&tmp, &new)) {
151             if (hint) {
152                 const DeviceState *d = DEVICE(md);
153                 error_setg(errp, "address range conflicts with memory device"
154                            " id='%s'", d->id ? d->id : "(unnamed)");
155                 goto out;
156             }
157 
158             next_addr = QEMU_ALIGN_UP(range_upb(&tmp) + 1, align);
159             if (!next_addr || range_init(&new, next_addr, range_size(&new))) {
160                 range_make_empty(&new);
161                 break;
162             }
163         } else if (range_lob(&tmp) > range_upb(&new)) {
164             break;
165         }
166     }
167 
168     if (!range_contains_range(&as, &new)) {
169         error_setg(errp, "could not find position in guest address space for "
170                    "memory device - memory fragmented due to alignments");
171     }
172 out:
173     g_slist_free(list);
174     return range_lob(&new);
175 }
176 
177 MemoryDeviceInfoList *qmp_memory_device_list(void)
178 {
179     GSList *devices = NULL, *item;
180     MemoryDeviceInfoList *list = NULL, **tail = &list;
181 
182     object_child_foreach(qdev_get_machine(), memory_device_build_list,
183                          &devices);
184 
185     for (item = devices; item; item = g_slist_next(item)) {
186         const MemoryDeviceState *md = MEMORY_DEVICE(item->data);
187         const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(item->data);
188         MemoryDeviceInfo *info = g_new0(MemoryDeviceInfo, 1);
189 
190         mdc->fill_device_info(md, info);
191 
192         QAPI_LIST_APPEND(tail, info);
193     }
194 
195     g_slist_free(devices);
196 
197     return list;
198 }
199 
200 static int memory_device_plugged_size(Object *obj, void *opaque)
201 {
202     uint64_t *size = opaque;
203 
204     if (object_dynamic_cast(obj, TYPE_MEMORY_DEVICE)) {
205         const DeviceState *dev = DEVICE(obj);
206         const MemoryDeviceState *md = MEMORY_DEVICE(obj);
207         const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(obj);
208 
209         if (dev->realized) {
210             *size += mdc->get_plugged_size(md, &error_abort);
211         }
212     }
213 
214     object_child_foreach(obj, memory_device_plugged_size, opaque);
215     return 0;
216 }
217 
218 uint64_t get_plugged_memory_size(void)
219 {
220     uint64_t size = 0;
221 
222     memory_device_plugged_size(qdev_get_machine(), &size);
223 
224     return size;
225 }
226 
227 void memory_device_pre_plug(MemoryDeviceState *md, MachineState *ms,
228                             const uint64_t *legacy_align, Error **errp)
229 {
230     const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(md);
231     Error *local_err = NULL;
232     uint64_t addr, align = 0;
233     MemoryRegion *mr;
234 
235     if (!ms->device_memory) {
236         error_setg(errp, "the configuration is not prepared for memory devices"
237                          " (e.g., for memory hotplug), consider specifying the"
238                          " maxmem option");
239         return;
240     }
241 
242     mr = mdc->get_memory_region(md, &local_err);
243     if (local_err) {
244         goto out;
245     }
246 
247     memory_device_check_addable(ms, md, mr, &local_err);
248     if (local_err) {
249         goto out;
250     }
251 
252     if (legacy_align) {
253         align = *legacy_align;
254     } else {
255         if (mdc->get_min_alignment) {
256             align = mdc->get_min_alignment(md);
257         }
258         align = MAX(align, memory_region_get_alignment(mr));
259     }
260     addr = mdc->get_addr(md);
261     addr = memory_device_get_free_addr(ms, !addr ? NULL : &addr, align,
262                                        memory_region_size(mr), &local_err);
263     if (local_err) {
264         goto out;
265     }
266     mdc->set_addr(md, addr, &local_err);
267     if (!local_err) {
268         trace_memory_device_pre_plug(DEVICE(md)->id ? DEVICE(md)->id : "",
269                                      addr);
270     }
271 out:
272     error_propagate(errp, local_err);
273 }
274 
275 void memory_device_plug(MemoryDeviceState *md, MachineState *ms)
276 {
277     const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(md);
278     const uint64_t addr = mdc->get_addr(md);
279     MemoryRegion *mr;
280 
281     /*
282      * We expect that a previous call to memory_device_pre_plug() succeeded, so
283      * it can't fail at this point.
284      */
285     mr = mdc->get_memory_region(md, &error_abort);
286     g_assert(ms->device_memory);
287 
288     ms->device_memory->used_region_size += memory_region_size(mr);
289     memory_region_add_subregion(&ms->device_memory->mr,
290                                 addr - ms->device_memory->base, mr);
291     trace_memory_device_plug(DEVICE(md)->id ? DEVICE(md)->id : "", addr);
292 }
293 
294 void memory_device_unplug(MemoryDeviceState *md, MachineState *ms)
295 {
296     const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(md);
297     MemoryRegion *mr;
298 
299     /*
300      * We expect that a previous call to memory_device_pre_plug() succeeded, so
301      * it can't fail at this point.
302      */
303     mr = mdc->get_memory_region(md, &error_abort);
304     g_assert(ms->device_memory);
305 
306     memory_region_del_subregion(&ms->device_memory->mr, mr);
307     ms->device_memory->used_region_size -= memory_region_size(mr);
308     trace_memory_device_unplug(DEVICE(md)->id ? DEVICE(md)->id : "",
309                                mdc->get_addr(md));
310 }
311 
312 uint64_t memory_device_get_region_size(const MemoryDeviceState *md,
313                                        Error **errp)
314 {
315     const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(md);
316     MemoryRegion *mr;
317 
318     /* dropping const here is fine as we don't touch the memory region */
319     mr = mdc->get_memory_region((MemoryDeviceState *)md, errp);
320     if (!mr) {
321         return 0;
322     }
323 
324     return memory_region_size(mr);
325 }
326 
327 void machine_memory_devices_init(MachineState *ms, hwaddr base, uint64_t size)
328 {
329     g_assert(size);
330     g_assert(!ms->device_memory);
331     ms->device_memory = g_new0(DeviceMemoryState, 1);
332     ms->device_memory->base = base;
333 
334     memory_region_init(&ms->device_memory->mr, OBJECT(ms), "device-memory",
335                        size);
336     memory_region_add_subregion(get_system_memory(), ms->device_memory->base,
337                                 &ms->device_memory->mr);
338 }
339 
340 static const TypeInfo memory_device_info = {
341     .name          = TYPE_MEMORY_DEVICE,
342     .parent        = TYPE_INTERFACE,
343     .class_size = sizeof(MemoryDeviceClass),
344 };
345 
346 static void memory_device_register_types(void)
347 {
348     type_register_static(&memory_device_info);
349 }
350 
351 type_init(memory_device_register_types)
352