xref: /openbmc/qemu/include/hw/mem/memory-device.h (revision 4a9b31b8)
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 "qom/object.h"
17 #include "hw/qdev.h"
18 
19 #define TYPE_MEMORY_DEVICE "memory-device"
20 
21 #define MEMORY_DEVICE_CLASS(klass) \
22      OBJECT_CLASS_CHECK(MemoryDeviceClass, (klass), TYPE_MEMORY_DEVICE)
23 #define MEMORY_DEVICE_GET_CLASS(obj) \
24     OBJECT_GET_CLASS(MemoryDeviceClass, (obj), TYPE_MEMORY_DEVICE)
25 #define MEMORY_DEVICE(obj) \
26      INTERFACE_CHECK(MemoryDeviceState, (obj), TYPE_MEMORY_DEVICE)
27 
28 typedef struct MemoryDeviceState {
29     Object parent_obj;
30 } MemoryDeviceState;
31 
32 /**
33  * MemoryDeviceClass:
34  *
35  * All memory devices need to implement TYPE_MEMORY_DEVICE as an interface.
36  *
37  * A memory device is a device that owns a memory region which is
38  * mapped into guest physical address space at a certain address. The
39  * address in guest physical memory can either be specified explicitly
40  * or get assigned automatically.
41  *
42  * Conceptually, memory devices only span one memory region. If multiple
43  * successive memory regions are used, a covering memory region has to
44  * be provided. Scattered memory regions are not supported for single
45  * devices.
46  */
47 typedef struct MemoryDeviceClass {
48     /* private */
49     InterfaceClass parent_class;
50 
51     /*
52      * Return the address of the memory device in guest physical memory.
53      *
54      * Called when (un)plugging a memory device or when iterating over
55      * all memory devices mapped into guest physical address space.
56      *
57      * If "0" is returned, no address has been specified by the user and
58      * no address has been assigned to this memory device yet.
59      */
60     uint64_t (*get_addr)(const MemoryDeviceState *md);
61 
62     /*
63      * Set the address of the memory device in guest physical memory.
64      *
65      * Called when plugging the memory device to configure the determined
66      * address in guest physical memory.
67      */
68     void (*set_addr)(MemoryDeviceState *md, uint64_t addr, Error **errp);
69 
70     /*
71      * Return the amount of memory provided by the memory device currently
72      * usable ("plugged") by the VM.
73      *
74      * Called when calculating the total amount of ram available to the
75      * VM (e.g. to report memory stats to the user).
76      *
77      * This is helpful for devices that dynamically manage the amount of
78      * memory accessible by the guest via the reserved memory region. For
79      * most devices, this corresponds to the size of the memory region.
80      */
81     uint64_t (*get_plugged_size)(const MemoryDeviceState *md, Error **errp);
82 
83     /*
84      * Return the memory region of the memory device.
85      *
86      * Called when (un)plugging the memory device, to (un)map the
87      * memory region in guest physical memory, but also to detect the
88      * required alignment during address assignment or when the size of the
89      * memory region is required.
90      */
91     MemoryRegion *(*get_memory_region)(MemoryDeviceState *md, Error **errp);
92 
93     /*
94      * Translate the memory device into #MemoryDeviceInfo.
95      */
96     void (*fill_device_info)(const MemoryDeviceState *md,
97                              MemoryDeviceInfo *info);
98 } MemoryDeviceClass;
99 
100 MemoryDeviceInfoList *qmp_memory_device_list(void);
101 uint64_t get_plugged_memory_size(void);
102 void memory_device_pre_plug(MemoryDeviceState *md, MachineState *ms,
103                             const uint64_t *legacy_align, Error **errp);
104 void memory_device_plug(MemoryDeviceState *md, MachineState *ms);
105 void memory_device_unplug(MemoryDeviceState *md, MachineState *ms);
106 uint64_t memory_device_get_region_size(const MemoryDeviceState *md,
107                                        Error **errp);
108 
109 #endif
110