xref: /openbmc/qemu/include/hw/mem/memory-device.h (revision 766aa0a6)
1 /*
2  * Memory Device Interface
3  *
4  * Copyright (c) 2018 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 or later.
10  * See the COPYING file in the top-level directory.
11  */
12 
13 #ifndef MEMORY_DEVICE_H
14 #define MEMORY_DEVICE_H
15 
16 #include "hw/qdev-core.h"
17 #include "qapi/qapi-types-machine.h"
18 #include "qom/object.h"
19 
20 #define TYPE_MEMORY_DEVICE "memory-device"
21 
22 typedef struct MemoryDeviceClass MemoryDeviceClass;
23 DECLARE_CLASS_CHECKERS(MemoryDeviceClass, MEMORY_DEVICE,
24                        TYPE_MEMORY_DEVICE)
25 #define MEMORY_DEVICE(obj) \
26      INTERFACE_CHECK(MemoryDeviceState, (obj), TYPE_MEMORY_DEVICE)
27 
28 typedef struct MemoryDeviceState MemoryDeviceState;
29 
30 /**
31  * MemoryDeviceClass:
32  *
33  * All memory devices need to implement TYPE_MEMORY_DEVICE as an interface.
34  *
35  * A memory device is a device that owns a memory region which is
36  * mapped into guest physical address space at a certain address. The
37  * address in guest physical memory can either be specified explicitly
38  * or get assigned automatically.
39  *
40  * Conceptually, memory devices only span one memory region. If multiple
41  * successive memory regions are used, a covering memory region has to
42  * be provided. Scattered memory regions are not supported for single
43  * devices.
44  *
45  * The device memory region returned via @get_memory_region may either be a
46  * single RAM memory region or a memory region container with subregions
47  * that are RAM memory regions or aliases to RAM memory regions. Other
48  * memory regions or subregions are not supported.
49  *
50  * If the device memory region returned via @get_memory_region is a
51  * memory region container, it's supported to dynamically (un)map subregions
52  * as long as the number of memslots returned by @get_memslots() won't
53  * be exceeded and as long as all memory regions are of the same kind (e.g.,
54  * all RAM or all ROM).
55  */
56 struct MemoryDeviceClass {
57     /* private */
58     InterfaceClass parent_class;
59 
60     /*
61      * Return the address of the memory device in guest physical memory.
62      *
63      * Called when (un)plugging a memory device or when iterating over
64      * all memory devices mapped into guest physical address space.
65      *
66      * If "0" is returned, no address has been specified by the user and
67      * no address has been assigned to this memory device yet.
68      */
69     uint64_t (*get_addr)(const MemoryDeviceState *md);
70 
71     /*
72      * Set the address of the memory device in guest physical memory.
73      *
74      * Called when plugging the memory device to configure the determined
75      * address in guest physical memory.
76      */
77     void (*set_addr)(MemoryDeviceState *md, uint64_t addr, Error **errp);
78 
79     /*
80      * Return the amount of memory provided by the memory device currently
81      * usable ("plugged") by the VM.
82      *
83      * Called when calculating the total amount of ram available to the
84      * VM (e.g. to report memory stats to the user).
85      *
86      * This is helpful for devices that dynamically manage the amount of
87      * memory accessible by the guest via the reserved memory region. For
88      * most devices, this corresponds to the size of the memory region.
89      */
90     uint64_t (*get_plugged_size)(const MemoryDeviceState *md, Error **errp);
91 
92     /*
93      * Return the memory region of the memory device.
94      *
95      * Called when (un)plugging the memory device, to (un)map the
96      * memory region in guest physical memory, but also to detect the
97      * required alignment during address assignment or when the size of the
98      * memory region is required.
99      */
100     MemoryRegion *(*get_memory_region)(MemoryDeviceState *md, Error **errp);
101 
102     /*
103      * Optional for memory devices that require only a single memslot,
104      * required for all other memory devices: Return the number of memslots
105      * (distinct RAM memory regions in the device memory region) that are
106      * required by the device.
107      *
108      * If this function is not implemented, the assumption is "1".
109      *
110      * Called when (un)plugging the memory device, to check if the requirements
111      * can be satisfied, and to do proper accounting.
112      */
113     unsigned int (*get_memslots)(MemoryDeviceState *md);
114 
115     /*
116      * Optional: Return the desired minimum alignment of the device in guest
117      * physical address space. The final alignment is computed based on this
118      * alignment and the alignment requirements of the memory region.
119      *
120      * Called when plugging the memory device to detect the required alignment
121      * during address assignment.
122      */
123     uint64_t (*get_min_alignment)(const MemoryDeviceState *md);
124 
125     /*
126      * Translate the memory device into #MemoryDeviceInfo.
127      */
128     void (*fill_device_info)(const MemoryDeviceState *md,
129                              MemoryDeviceInfo *info);
130 };
131 
132 MemoryDeviceInfoList *qmp_memory_device_list(void);
133 uint64_t get_plugged_memory_size(void);
134 unsigned int memory_devices_get_reserved_memslots(void);
135 void memory_device_pre_plug(MemoryDeviceState *md, MachineState *ms,
136                             const uint64_t *legacy_align, Error **errp);
137 void memory_device_plug(MemoryDeviceState *md, MachineState *ms);
138 void memory_device_unplug(MemoryDeviceState *md, MachineState *ms);
139 uint64_t memory_device_get_region_size(const MemoryDeviceState *md,
140                                        Error **errp);
141 
142 #endif
143