xref: /openbmc/qemu/hw/remote/memory.c (revision 62a4db55)
1 /*
2  * Memory manager for remote device
3  *
4  * Copyright © 2018, 2021 Oracle and/or its affiliates.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  *
9  */
10 
11 #include "qemu/osdep.h"
12 #include "qemu-common.h"
13 
14 #include "hw/remote/memory.h"
15 #include "exec/ram_addr.h"
16 #include "qapi/error.h"
17 
18 static void remote_sysmem_reset(void)
19 {
20     MemoryRegion *sysmem, *subregion, *next;
21 
22     sysmem = get_system_memory();
23 
24     QTAILQ_FOREACH_SAFE(subregion, &sysmem->subregions, subregions_link, next) {
25         if (subregion->ram) {
26             memory_region_del_subregion(sysmem, subregion);
27             object_unparent(OBJECT(subregion));
28         }
29     }
30 }
31 
32 void remote_sysmem_reconfig(MPQemuMsg *msg, Error **errp)
33 {
34     ERRP_GUARD();
35     SyncSysmemMsg *sysmem_info = &msg->data.sync_sysmem;
36     MemoryRegion *sysmem, *subregion;
37     static unsigned int suffix;
38     int region;
39 
40     sysmem = get_system_memory();
41 
42     remote_sysmem_reset();
43 
44     for (region = 0; region < msg->num_fds; region++, suffix++) {
45         g_autofree char *name = g_strdup_printf("remote-mem-%u", suffix);
46         subregion = g_new(MemoryRegion, 1);
47         memory_region_init_ram_from_fd(subregion, NULL,
48                                        name, sysmem_info->sizes[region],
49                                        RAM_SHARED, msg->fds[region],
50                                        sysmem_info->offsets[region],
51                                        errp);
52 
53         if (*errp) {
54             g_free(subregion);
55             remote_sysmem_reset();
56             return;
57         }
58 
59         memory_region_add_subregion(sysmem, sysmem_info->gpas[region],
60                                     subregion);
61 
62     }
63 }
64