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