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