1 /* $NetBSD: virtioreg.h,v 1.16 2026/08/01 20:35:05 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 2010 Minoura Makoto. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* 29 * Part of the file derived from `Virtio PCI Card Specification v0.8.6 DRAFT' 30 * Appendix A. 31 */ 32 /* An interface for efficient virtio implementation. 33 * 34 * This header is BSD licensed so anyone can use the definitions 35 * to implement compatible drivers/servers. 36 * 37 * Copyright 2007, 2009, IBM Corporation 38 * All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. Neither the name of IBM nor the names of its contributors 49 * may be used to endorse or promote products derived from this software 50 * without specific prior written permission. 51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE 55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 */ 63 64 65 #ifndef _DEV_PCI_VIRTIOREG_H_ 66 #define _DEV_PCI_VIRTIOREG_H_ 67 68 #include <sys/types.h> 69 70 /* 71 * Virtio product id (all subsystems) 72 * 73 * Each device ID is marked with the version of the VirtIO specification 74 * in which the device was first described. Devices that were documented 75 * in pre-1.0 drafts also have the "0.9" version identifier. If a device 76 * has both "0.9" and "1.0", then it is a so-called "transitional device" 77 * whose behavior when using the legacy interface is described in the 78 * VirtIO 1.0 specification. If a device has only a "0.9" identifier, then 79 * it was described in the pre-1.0 drafts and subsequently dropped. 80 * 81 * Device IDs without a first-version marking have mentions in at least 82 * one version of the VirtIO specification, but no formal description in 83 * the specification. 84 */ 85 #define VIRTIO_DEVICE_ID_NETWORK 1 /* 0.9, 1.0 */ 86 #define VIRTIO_DEVICE_ID_BLOCK 2 /* 0.9, 1.0 */ 87 #define VIRTIO_DEVICE_ID_CONSOLE 3 /* 0.9, 1.0 */ 88 #define VIRTIO_DEVICE_ID_ENTROPY 4 /* 0.9, 1.0 */ 89 #define VIRTIO_DEVICE_ID_BALLOON 5 /* 0.9, 1.0 (traditional) */ 90 #define VIRTIO_DEVICE_ID_IOMEM 6 91 #define VIRTIO_DEVICE_ID_RPMSG 7 /* 0.9 */ 92 #define VIRTIO_DEVICE_ID_SCSI 8 /* 0.9, 1.0 */ 93 #define VIRTIO_DEVICE_ID_9P 9 94 #define VIRTIO_DEVICE_ID_MAC80211_WLAN 10 95 #define VIRTIO_DEVICE_ID_RPROC_SERIAL 11 96 #define VIRTIO_DEVICE_ID_CAIF 12 97 #define VIRTIO_DEVICE_ID_MEMORY_BALLOON 12 /* (new?) */ 98 #define VIRTIO_DEVICE_ID_GPU 16 /* 1.1 */ 99 #define VIRTIO_DEVICE_ID_TIMER_CLOCK 17 100 #define VIRTIO_DEVICE_ID_INPUT_DEVICE 18 /* 1.1 */ 101 #define VIRTIO_DEVICE_ID_SOCKET 19 /* 1.1 */ 102 #define VIRTIO_DEVICE_ID_CRYPTO 20 /* 1.1 */ 103 #define VIRTIO_DEVICE_ID_IOMMU 23 /* 1.2 */ 104 #define VIRTIO_DEVICE_ID_MEMORY 24 /* 1.2 */ 105 #define VIRTIO_DEVICE_ID_FILE_SYSTEM 26 /* 1.2 */ 106 #define VIRTIO_DEVICE_ID_SOUND 25 /* 1.2 */ 107 #define VIRTIO_DEVICE_ID_PMEM 27 /* 1.2 */ 108 #define VIRTIO_DEVICE_ID_RPMB 28 /* 1.2 */ 109 #define VIRTIO_DEVICE_ID_SCMI 32 /* 1.2 */ 110 #define VIRTIO_DEVICE_ID_I2C_ADAPTER 34 /* 1.2 */ 111 #define VIRTIO_DEVICE_ID_GPIO 41 /* 1.2 */ 112 113 /* common device/guest features */ 114 #define VIRTIO_F_NOTIFY_ON_EMPTY __BIT(24) /* not in 1.0 */ 115 #define VIRTIO_F_RING_INDIRECT_DESC __BIT(28) 116 #define VIRTIO_F_RING_EVENT_IDX __BIT(29) 117 #define VIRTIO_F_BAD_FEATURE __BIT(30) 118 #define VIRTIO_F_VERSION_1 __BIT(32) 119 #define VIRTIO_F_ACCESS_PLATFORM __BIT(33) 120 #define VIRTIO_F_RING_PACKED __BIT(34) 121 #define VIRTIO_F_ORDER __BIT(35) 122 #define VIRTIO_F_ORDER_PLATFORM __BIT(36) 123 #define VIRTIO_F_SR_IOV __BIT(37) 124 #define VIRTIO_F_NOTIFICATION_DATA __BIT(38) 125 #define VIRTIO_F_NOTIF_CONFIG_DATA __BIT(39) 126 #define VIRTIO_F_RING_RESET __BIT(40) 127 128 /* common device status flags */ 129 #define VIRTIO_CONFIG_DEVICE_STATUS_RESET 0 130 #define VIRTIO_CONFIG_DEVICE_STATUS_ACK 1 131 #define VIRTIO_CONFIG_DEVICE_STATUS_DRIVER 2 132 #define VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK 4 133 #define VIRTIO_CONFIG_DEVICE_STATUS_FEATURES_OK 8 134 #define VIRTIO_CONFIG_DEVICE_STATUS_DEVICE_NEEDS_RESET 64 135 #define VIRTIO_CONFIG_DEVICE_STATUS_FAILED 128 136 137 /* common ISR status flags */ 138 #define VIRTIO_CONFIG_ISR_QUEUE_INTERRUPT 1 139 #define VIRTIO_CONFIG_ISR_CONFIG_CHANGE 2 140 141 /* common device/guest features */ 142 #define VIRTIO_COMMON_FLAG_BITS \ 143 "\177\020" \ 144 "b\x24" "SR_IOV\0" \ 145 "b\x23" "ORDER_PLATFORM\0" \ 146 "b\x22" "RING_PACKED\0" \ 147 "b\x21" "ACCESS_PLATFORM\0" \ 148 "b\x20" "V1\0" \ 149 "b\x1e" "BAD_FEATURE\0" \ 150 "b\x1d" "EVENT_IDX\0" \ 151 "b\x1c" "INDIRECT_DESC\0" \ 152 "b\x18" "NOTIFY_ON_EMPTY\0" 153 154 155 /* 156 * Virtqueue 157 */ 158 159 /* marks a buffer as continuing via the next field. */ 160 #define VRING_DESC_F_NEXT 1 161 162 /* marks a buffer as write-only (otherwise read-only). */ 163 #define VRING_DESC_F_WRITE 2 164 165 /* the buffer contains a list of buffer descriptors. */ 166 #define VRING_DESC_F_INDIRECT 4 167 168 169 /* 170 * The Host uses this in used->flags to advise the Guest: don't kick me when 171 * you add a buffer. It's unreliable, so it's simply an optimization. Guest 172 * will still kick if it's out of buffers. 173 */ 174 #define VRING_USED_F_NO_NOTIFY 1 175 176 /* 177 * The Guest uses this in avail->flags to advise the Host: don't interrupt me 178 * when you consume a buffer. It's unreliable, so it's simply an 179 * optimization. 180 */ 181 #define VRING_AVAIL_F_NO_INTERRUPT 1 182 183 184 /* Virtio ring descriptors: 16 bytes. 185 * These can chain together via "next". */ 186 struct vring_desc { 187 /* Address (guest-physical). */ 188 uint64_t addr; 189 /* Length. */ 190 uint32_t len; 191 /* The flags as indicated above. */ 192 uint16_t flags; 193 /* We chain unused descriptors via this, too */ 194 uint16_t next; 195 } __packed; 196 197 struct vring_avail { 198 uint16_t flags; 199 uint16_t idx; 200 uint16_t ring[]; 201 /* trailed by uint16_t used_event when VIRTIO_F_RING_EVENT_IDX */ 202 } __packed; 203 204 /* u32 is used here for ids for padding reasons. */ 205 struct vring_used_elem { 206 /* Index of start of used descriptor chain. */ 207 uint32_t id; 208 /* Total length of the descriptor chain which was written to. */ 209 uint32_t len; 210 } __packed; 211 212 struct vring_used { 213 uint16_t flags; 214 uint16_t idx; 215 struct vring_used_elem ring[]; 216 /* trailed by uint16_t avail_event when VIRTIO_F_RING_EVENT_IDX */ 217 } __packed; 218 219 /* The standard layout for the ring is a continuous chunk of memory which 220 * looks like this. We assume num is a power of 2. 221 * 222 * struct vring { 223 * // The actual descriptors (16 bytes each) 224 * struct vring_desc desc[num]; 225 * 226 * // A ring of available descriptor heads with free-running index. 227 * __u16 avail_flags; 228 * __u16 avail_idx; 229 * __u16 available[num]; 230 * 231 * // Padding to the next align boundary. 232 * char pad[]; 233 * 234 * // A ring of used descriptor heads with free-running index. 235 * __u16 used_flags; 236 * __u16 used_idx; 237 * struct vring_used_elem used[num]; 238 * }; 239 * Note: for virtio PCI, align is 4096. 240 */ 241 242 #define VIRTIO_PAGE_SIZE (4096) 243 244 #endif /* _DEV_PCI_VIRTIOREG_H_ */ 245