xref: /openbmc/qemu/include/hw/virtio/virtio-mem.h (revision c009a311)
1 /*
2  * Virtio MEM device
3  *
4  * Copyright (C) 2020 Red Hat, Inc.
5  *
6  * Authors:
7  *  David Hildenbrand <david@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.
10  * See the COPYING file in the top-level directory.
11  */
12 
13 #ifndef HW_VIRTIO_MEM_H
14 #define HW_VIRTIO_MEM_H
15 
16 #include "standard-headers/linux/virtio_mem.h"
17 #include "hw/resettable.h"
18 #include "hw/virtio/virtio.h"
19 #include "qapi/qapi-types-misc.h"
20 #include "sysemu/hostmem.h"
21 #include "qom/object.h"
22 
23 #define TYPE_VIRTIO_MEM "virtio-mem"
24 
25 OBJECT_DECLARE_TYPE(VirtIOMEM, VirtIOMEMClass,
26                     VIRTIO_MEM)
27 
28 #define VIRTIO_MEM_MEMDEV_PROP "memdev"
29 #define VIRTIO_MEM_NODE_PROP "node"
30 #define VIRTIO_MEM_SIZE_PROP "size"
31 #define VIRTIO_MEM_REQUESTED_SIZE_PROP "requested-size"
32 #define VIRTIO_MEM_BLOCK_SIZE_PROP "block-size"
33 #define VIRTIO_MEM_ADDR_PROP "memaddr"
34 #define VIRTIO_MEM_UNPLUGGED_INACCESSIBLE_PROP "unplugged-inaccessible"
35 #define VIRTIO_MEM_EARLY_MIGRATION_PROP "x-early-migration"
36 #define VIRTIO_MEM_PREALLOC_PROP "prealloc"
37 #define VIRTIO_MEM_DYNAMIC_MEMSLOTS_PROP "dynamic-memslots"
38 
39 struct VirtIOMEM {
40     VirtIODevice parent_obj;
41 
42     /* guest -> host request queue */
43     VirtQueue *vq;
44 
45     /* bitmap used to track unplugged memory */
46     int32_t bitmap_size;
47     unsigned long *bitmap;
48 
49     /*
50      * With "dynamic-memslots=on": Device memory region in which we dynamically
51      * map the memslots.
52      */
53     MemoryRegion *mr;
54 
55     /*
56      * With "dynamic-memslots=on": The individual memslots (aliases into the
57      * memory backend).
58      */
59     MemoryRegion *memslots;
60 
61     /* With "dynamic-memslots=on": The total number of memslots. */
62     uint16_t nb_memslots;
63 
64     /*
65      * With "dynamic-memslots=on": Size of one memslot (the size of the
66      * last one can differ).
67      */
68     uint64_t memslot_size;
69 
70     /* Assigned memory backend with the RAM memory region. */
71     HostMemoryBackend *memdev;
72 
73     /* NUMA node */
74     uint32_t node;
75 
76     /* assigned address of the region in guest physical memory */
77     uint64_t addr;
78 
79     /* usable region size (<= region_size) */
80     uint64_t usable_region_size;
81 
82     /* actual size (how much the guest plugged) */
83     uint64_t size;
84 
85     /* requested size */
86     uint64_t requested_size;
87 
88     /* block size and alignment */
89     uint64_t block_size;
90 
91     /*
92      * Whether we indicate VIRTIO_MEM_F_UNPLUGGED_INACCESSIBLE to the guest.
93      * For !x86 targets this will always be "on" and consequently indicate
94      * VIRTIO_MEM_F_UNPLUGGED_INACCESSIBLE.
95      */
96     OnOffAuto unplugged_inaccessible;
97 
98     /* whether to prealloc memory when plugging new blocks */
99     bool prealloc;
100 
101     /*
102      * Whether we migrate properties that are immutable while migration is
103      * active early, before state of other devices and especially, before
104      * migrating any RAM content.
105      */
106     bool early_migration;
107 
108     /*
109      * Whether we dynamically map (multiple, if possible) memslots instead of
110      * statically mapping the whole RAM memory region.
111      */
112     bool dynamic_memslots;
113 
114     /* notifiers to notify when "size" changes */
115     NotifierList size_change_notifiers;
116 
117     /* listeners to notify on plug/unplug activity. */
118     QLIST_HEAD(, RamDiscardListener) rdl_list;
119 
120     /* State of the resettable container */
121     ResettableState reset_state;
122 };
123 
124 struct VirtIOMEMClass {
125     /* private */
126     VirtIODevice parent;
127 
128     /* public */
129     void (*fill_device_info)(const VirtIOMEM *vmen, VirtioMEMDeviceInfo *vi);
130     MemoryRegion *(*get_memory_region)(VirtIOMEM *vmem, Error **errp);
131     void (*decide_memslots)(VirtIOMEM *vmem, unsigned int limit);
132     unsigned int (*get_memslots)(VirtIOMEM *vmem);
133     void (*add_size_change_notifier)(VirtIOMEM *vmem, Notifier *notifier);
134     void (*remove_size_change_notifier)(VirtIOMEM *vmem, Notifier *notifier);
135     void (*unplug_request_check)(VirtIOMEM *vmem, Error **errp);
136 };
137 
138 #endif
139