virtio_mem.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. /*
  3. * Virtio Mem Device
  4. *
  5. * Copyright Red Hat, Inc. 2020
  6. *
  7. * Authors:
  8. * David Hildenbrand <david@redhat.com>
  9. *
  10. * This header is BSD licensed so anyone can use the definitions
  11. * to implement compatible drivers/servers:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of IBM nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IBM OR
  28. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  31. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  32. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  34. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. #ifndef _LINUX_VIRTIO_MEM_H
  38. #define _LINUX_VIRTIO_MEM_H
  39. #include <linux/types.h>
  40. #include <linux/virtio_types.h>
  41. #include <linux/virtio_ids.h>
  42. #include <linux/virtio_config.h>
  43. /*
  44. * Each virtio-mem device manages a dedicated region in physical address
  45. * space. Each device can belong to a single NUMA node, multiple devices
  46. * for a single NUMA node are possible. A virtio-mem device is like a
  47. * "resizable DIMM" consisting of small memory blocks that can be plugged
  48. * or unplugged. The device driver is responsible for (un)plugging memory
  49. * blocks on demand.
  50. *
  51. * Virtio-mem devices can only operate on their assigned memory region in
  52. * order to (un)plug memory. A device cannot (un)plug memory belonging to
  53. * other devices.
  54. *
  55. * The "region_size" corresponds to the maximum amount of memory that can
  56. * be provided by a device. The "size" corresponds to the amount of memory
  57. * that is currently plugged. "requested_size" corresponds to a request
  58. * from the device to the device driver to (un)plug blocks. The
  59. * device driver should try to (un)plug blocks in order to reach the
  60. * "requested_size". It is impossible to plug more memory than requested.
  61. *
  62. * The "usable_region_size" represents the memory region that can actually
  63. * be used to (un)plug memory. It is always at least as big as the
  64. * "requested_size" and will grow dynamically. It will only shrink when
  65. * explicitly triggered (VIRTIO_MEM_REQ_UNPLUG).
  66. *
  67. * There are no guarantees what will happen if unplugged memory is
  68. * read/written. In general, unplugged memory should not be touched, because
  69. * the resulting action is undefined. There is one exception: without
  70. * VIRTIO_MEM_F_UNPLUGGED_INACCESSIBLE, unplugged memory inside the usable
  71. * region can be read, to simplify creation of memory dumps.
  72. *
  73. * It can happen that the device cannot process a request, because it is
  74. * busy. The device driver has to retry later.
  75. *
  76. * Usually, during system resets all memory will get unplugged, so the
  77. * device driver can start with a clean state. However, in specific
  78. * scenarios (if the device is busy) it can happen that the device still
  79. * has memory plugged. The device driver can request to unplug all memory
  80. * (VIRTIO_MEM_REQ_UNPLUG) - which might take a while to succeed if the
  81. * device is busy.
  82. */
  83. /* --- virtio-mem: feature bits --- */
  84. /* node_id is an ACPI PXM and is valid */
  85. #define VIRTIO_MEM_F_ACPI_PXM 0
  86. /* unplugged memory must not be accessed */
  87. #define VIRTIO_MEM_F_UNPLUGGED_INACCESSIBLE 1
  88. /* --- virtio-mem: guest -> host requests --- */
  89. /* request to plug memory blocks */
  90. #define VIRTIO_MEM_REQ_PLUG 0
  91. /* request to unplug memory blocks */
  92. #define VIRTIO_MEM_REQ_UNPLUG 1
  93. /* request to unplug all blocks and shrink the usable size */
  94. #define VIRTIO_MEM_REQ_UNPLUG_ALL 2
  95. /* request information about the plugged state of memory blocks */
  96. #define VIRTIO_MEM_REQ_STATE 3
  97. struct virtio_mem_req_plug {
  98. __virtio64 addr;
  99. __virtio16 nb_blocks;
  100. __virtio16 padding[3];
  101. };
  102. struct virtio_mem_req_unplug {
  103. __virtio64 addr;
  104. __virtio16 nb_blocks;
  105. __virtio16 padding[3];
  106. };
  107. struct virtio_mem_req_state {
  108. __virtio64 addr;
  109. __virtio16 nb_blocks;
  110. __virtio16 padding[3];
  111. };
  112. struct virtio_mem_req {
  113. __virtio16 type;
  114. __virtio16 padding[3];
  115. union {
  116. struct virtio_mem_req_plug plug;
  117. struct virtio_mem_req_unplug unplug;
  118. struct virtio_mem_req_state state;
  119. } u;
  120. };
  121. /* --- virtio-mem: host -> guest response --- */
  122. /*
  123. * Request processed successfully, applicable for
  124. * - VIRTIO_MEM_REQ_PLUG
  125. * - VIRTIO_MEM_REQ_UNPLUG
  126. * - VIRTIO_MEM_REQ_UNPLUG_ALL
  127. * - VIRTIO_MEM_REQ_STATE
  128. */
  129. #define VIRTIO_MEM_RESP_ACK 0
  130. /*
  131. * Request denied - e.g. trying to plug more than requested, applicable for
  132. * - VIRTIO_MEM_REQ_PLUG
  133. */
  134. #define VIRTIO_MEM_RESP_NACK 1
  135. /*
  136. * Request cannot be processed right now, try again later, applicable for
  137. * - VIRTIO_MEM_REQ_PLUG
  138. * - VIRTIO_MEM_REQ_UNPLUG
  139. * - VIRTIO_MEM_REQ_UNPLUG_ALL
  140. */
  141. #define VIRTIO_MEM_RESP_BUSY 2
  142. /*
  143. * Error in request (e.g. addresses/alignment), applicable for
  144. * - VIRTIO_MEM_REQ_PLUG
  145. * - VIRTIO_MEM_REQ_UNPLUG
  146. * - VIRTIO_MEM_REQ_STATE
  147. */
  148. #define VIRTIO_MEM_RESP_ERROR 3
  149. /* State of memory blocks is "plugged" */
  150. #define VIRTIO_MEM_STATE_PLUGGED 0
  151. /* State of memory blocks is "unplugged" */
  152. #define VIRTIO_MEM_STATE_UNPLUGGED 1
  153. /* State of memory blocks is "mixed" */
  154. #define VIRTIO_MEM_STATE_MIXED 2
  155. struct virtio_mem_resp_state {
  156. __virtio16 state;
  157. };
  158. struct virtio_mem_resp {
  159. __virtio16 type;
  160. __virtio16 padding[3];
  161. union {
  162. struct virtio_mem_resp_state state;
  163. } u;
  164. };
  165. /* --- virtio-mem: configuration --- */
  166. struct virtio_mem_config {
  167. /* Block size and alignment. Cannot change. */
  168. __le64 block_size;
  169. /* Valid with VIRTIO_MEM_F_ACPI_PXM. Cannot change. */
  170. __le16 node_id;
  171. __u8 padding[6];
  172. /* Start address of the memory region. Cannot change. */
  173. __le64 addr;
  174. /* Region size (maximum). Cannot change. */
  175. __le64 region_size;
  176. /*
  177. * Currently usable region size. Can grow up to region_size. Can
  178. * shrink due to VIRTIO_MEM_REQ_UNPLUG_ALL (in which case no config
  179. * update will be sent).
  180. */
  181. __le64 usable_region_size;
  182. /*
  183. * Currently used size. Changes due to plug/unplug requests, but no
  184. * config updates will be sent.
  185. */
  186. __le64 plugged_size;
  187. /* Requested size. New plug requests cannot exceed it. Can change. */
  188. __le64 requested_size;
  189. };
  190. #endif /* _LINUX_VIRTIO_MEM_H */