xref: /openbmc/qemu/include/hw/mem/memory-device.h (revision 520e210c)
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 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 typedef struct MemoryDeviceClass {
46     /* private */
47     InterfaceClass parent_class;
48 
49     /*
50      * Return the address of the memory device in guest physical memory.
51      *
52      * Called when (un)plugging a memory device or when iterating over
53      * all memory devices mapped into guest physical address space.
54      *
55      * If "0" is returned, no address has been specified by the user and
56      * no address has been assigned to this memory device yet.
57      */
58     uint64_t (*get_addr)(const MemoryDeviceState *md);
59 
60     /*
61      * Set the address of the memory device in guest physical memory.
62      *
63      * Called when plugging the memory device to configure the determined
64      * address in guest physical memory.
65      */
66     void (*set_addr)(MemoryDeviceState *md, uint64_t addr, Error **errp);
67 
68     /*
69      * Return the amount of memory provided by the memory device currently
70      * usable ("plugged") by the VM.
71      *
72      * Called when calculating the total amount of ram available to the
73      * VM (e.g. to report memory stats to the user).
74      *
75      * This is helpful for devices that dynamically manage the amount of
76      * memory accessible by the guest via the reserved memory region. For
77      * most devices, this corresponds to the size of the memory region.
78      */
79     uint64_t (*get_plugged_size)(const MemoryDeviceState *md, Error **errp);
80 
81     /*
82      * Return the memory region of the memory device.
83      *
84      * Called when (un)plugging the memory device, to (un)map the
85      * memory region in guest physical memory, but also to detect the
86      * required alignment during address assignment or when the size of the
87      * memory region is required.
88      */
89     MemoryRegion *(*get_memory_region)(MemoryDeviceState *md, Error **errp);
90 
91     /*
92      * Translate the memory device into #MemoryDeviceInfo.
93      */
94     void (*fill_device_info)(const MemoryDeviceState *md,
95                              MemoryDeviceInfo *info);
96 } MemoryDeviceClass;
97 
98 MemoryDeviceInfoList *qmp_memory_device_list(void);
99 uint64_t get_plugged_memory_size(void);
100 void memory_device_pre_plug(MemoryDeviceState *md, MachineState *ms,
101                             const uint64_t *legacy_align, Error **errp);
102 void memory_device_plug(MemoryDeviceState *md, MachineState *ms);
103 void memory_device_unplug(MemoryDeviceState *md, MachineState *ms);
104 uint64_t memory_device_get_region_size(const MemoryDeviceState *md,
105                                        Error **errp);
106 
107 #endif
108