memory-device.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #ifndef MEMORY_DEVICE_H
  13. #define MEMORY_DEVICE_H
  14. #include "hw/qdev-core.h"
  15. #include "qapi/qapi-types-machine.h"
  16. #include "qom/object.h"
  17. #define TYPE_MEMORY_DEVICE "memory-device"
  18. typedef struct MemoryDeviceClass MemoryDeviceClass;
  19. DECLARE_CLASS_CHECKERS(MemoryDeviceClass, MEMORY_DEVICE,
  20. TYPE_MEMORY_DEVICE)
  21. #define MEMORY_DEVICE(obj) \
  22. INTERFACE_CHECK(MemoryDeviceState, (obj), TYPE_MEMORY_DEVICE)
  23. typedef struct MemoryDeviceState MemoryDeviceState;
  24. /**
  25. * MemoryDeviceClass:
  26. *
  27. * All memory devices need to implement TYPE_MEMORY_DEVICE as an interface.
  28. *
  29. * A memory device is a device that owns a memory region which is
  30. * mapped into guest physical address space at a certain address. The
  31. * address in guest physical memory can either be specified explicitly
  32. * or get assigned automatically.
  33. *
  34. * Some memory device might not own a memory region in certain device
  35. * configurations. Such devices can logically get (un)plugged, however,
  36. * empty memory devices are mostly ignored by the memory device code.
  37. *
  38. * Conceptually, memory devices only span one memory region. If multiple
  39. * successive memory regions are used, a covering memory region has to
  40. * be provided. Scattered memory regions are not supported for single
  41. * devices.
  42. *
  43. * The device memory region returned via @get_memory_region may either be a
  44. * single RAM memory region or a memory region container with subregions
  45. * that are RAM memory regions or aliases to RAM memory regions. Other
  46. * memory regions or subregions are not supported.
  47. *
  48. * If the device memory region returned via @get_memory_region is a
  49. * memory region container, it's supported to dynamically (un)map subregions
  50. * as long as the number of memslots returned by @get_memslots() won't
  51. * be exceeded and as long as all memory regions are of the same kind (e.g.,
  52. * all RAM or all ROM).
  53. */
  54. struct MemoryDeviceClass {
  55. /* private */
  56. InterfaceClass parent_class;
  57. /*
  58. * Return the address of the memory device in guest physical memory.
  59. *
  60. * Called when (un)plugging a memory device or when iterating over
  61. * all memory devices mapped into guest physical address space.
  62. *
  63. * If "0" is returned, no address has been specified by the user and
  64. * no address has been assigned to this memory device yet.
  65. */
  66. uint64_t (*get_addr)(const MemoryDeviceState *md);
  67. /*
  68. * Set the address of the memory device in guest physical memory.
  69. *
  70. * Called when plugging the memory device to configure the determined
  71. * address in guest physical memory.
  72. */
  73. void (*set_addr)(MemoryDeviceState *md, uint64_t addr, Error **errp);
  74. /*
  75. * Return the amount of memory provided by the memory device currently
  76. * usable ("plugged") by the VM.
  77. *
  78. * Called when calculating the total amount of ram available to the
  79. * VM (e.g. to report memory stats to the user).
  80. *
  81. * This is helpful for devices that dynamically manage the amount of
  82. * memory accessible by the guest via the reserved memory region. For
  83. * most devices, this corresponds to the size of the memory region.
  84. */
  85. uint64_t (*get_plugged_size)(const MemoryDeviceState *md, Error **errp);
  86. /*
  87. * Return the memory region of the memory device. If the device is
  88. * completely empty, returns NULL without an error.
  89. *
  90. * Called when (un)plugging the memory device, to (un)map the
  91. * memory region in guest physical memory, but also to detect the
  92. * required alignment during address assignment or when the size of the
  93. * memory region is required.
  94. */
  95. MemoryRegion *(*get_memory_region)(MemoryDeviceState *md, Error **errp);
  96. /*
  97. * Optional: Instruct the memory device to decide how many memory slots
  98. * it requires, not exceeding the given limit.
  99. *
  100. * Called exactly once when pre-plugging the memory device, before
  101. * querying the number of memslots using @get_memslots the first time.
  102. */
  103. void (*decide_memslots)(MemoryDeviceState *md, unsigned int limit);
  104. /*
  105. * Optional for memory devices that require only a single memslot,
  106. * required for all other memory devices: Return the number of memslots
  107. * (distinct RAM memory regions in the device memory region) that are
  108. * required by the device.
  109. *
  110. * If this function is not implemented, the assumption is "1".
  111. *
  112. * Called when (un)plugging the memory device, to check if the requirements
  113. * can be satisfied, and to do proper accounting.
  114. */
  115. unsigned int (*get_memslots)(MemoryDeviceState *md);
  116. /*
  117. * Optional: Return the desired minimum alignment of the device in guest
  118. * physical address space. The final alignment is computed based on this
  119. * alignment and the alignment requirements of the memory region.
  120. *
  121. * Called when plugging the memory device to detect the required alignment
  122. * during address assignment.
  123. */
  124. uint64_t (*get_min_alignment)(const MemoryDeviceState *md);
  125. /*
  126. * Translate the memory device into #MemoryDeviceInfo.
  127. */
  128. void (*fill_device_info)(const MemoryDeviceState *md,
  129. MemoryDeviceInfo *info);
  130. };
  131. /*
  132. * Traditionally, KVM/vhost in many setups supported 509 memslots, whereby
  133. * 253 memslots were "reserved" for boot memory and other devices (such
  134. * as PCI BARs, which can get mapped dynamically) and 256 memslots were
  135. * dedicated for DIMMs. These magic numbers worked reliably in the past.
  136. *
  137. * Further, using many memslots can negatively affect performance, so setting
  138. * the soft-limit of memslots used by memory devices to the traditional
  139. * DIMM limit of 256 sounds reasonable.
  140. *
  141. * If we have less than 509 memslots, we will instruct memory devices that
  142. * support automatically deciding how many memslots to use to only use a single
  143. * one.
  144. *
  145. * Hotplugging vhost devices with at least 509 memslots is not expected to
  146. * cause problems, not even when devices automatically decided how many memslots
  147. * to use.
  148. */
  149. #define MEMORY_DEVICES_SOFT_MEMSLOT_LIMIT 256
  150. #define MEMORY_DEVICES_SAFE_MAX_MEMSLOTS 509
  151. MemoryDeviceInfoList *qmp_memory_device_list(void);
  152. uint64_t get_plugged_memory_size(void);
  153. unsigned int memory_devices_get_reserved_memslots(void);
  154. bool memory_devices_memslot_auto_decision_active(void);
  155. void memory_device_pre_plug(MemoryDeviceState *md, MachineState *ms,
  156. Error **errp);
  157. void memory_device_plug(MemoryDeviceState *md, MachineState *ms);
  158. void memory_device_unplug(MemoryDeviceState *md, MachineState *ms);
  159. uint64_t memory_device_get_region_size(const MemoryDeviceState *md,
  160. Error **errp);
  161. #endif