xref: /openbmc/qemu/hw/remote/memory.c (revision 4921d0a7)
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 
13 #include "hw/remote/memory.h"
14 #include "exec/ram_addr.h"
15 #include "qapi/error.h"
16 
17 static void remote_sysmem_reset(void)
18 {
19     MemoryRegion *sysmem, *subregion, *next;
20 
21     sysmem = get_system_memory();
22 
23     QTAILQ_FOREACH_SAFE(subregion, &sysmem->subregions, subregions_link, next) {
24         if (subregion->ram) {
25             memory_region_del_subregion(sysmem, subregion);
26             object_unparent(OBJECT(subregion));
27         }
28     }
29 }
30 
31 void remote_sysmem_reconfig(MPQemuMsg *msg, Error **errp)
32 {
33     ERRP_GUARD();
34     SyncSysmemMsg *sysmem_info = &msg->data.sync_sysmem;
35     MemoryRegion *sysmem, *subregion;
36     static unsigned int suffix;
37     int region;
38 
39     sysmem = get_system_memory();
40 
41     remote_sysmem_reset();
42 
43     for (region = 0; region < msg->num_fds; region++, suffix++) {
44         g_autofree char *name = g_strdup_printf("remote-mem-%u", suffix);
45         subregion = g_new(MemoryRegion, 1);
46         memory_region_init_ram_from_fd(subregion, NULL,
47                                        name, sysmem_info->sizes[region],
48                                        RAM_SHARED, msg->fds[region],
49                                        sysmem_info->offsets[region],
50                                        errp);
51 
52         if (*errp) {
53             g_free(subregion);
54             remote_sysmem_reset();
55             return;
56         }
57 
58         memory_region_add_subregion(sysmem, sysmem_info->gpas[region],
59                                     subregion);
60 
61     }
62 }
63