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