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