1 /* $OpenBSD: virtioreg.h,v 1.5 2023/04/20 19:28:31 jcs Exp $ */ 2 /* $NetBSD: virtioreg.h,v 1.1 2011/10/30 12:12:21 hannken Exp $ */ 3 4 /* 5 * Copyright (c) 2012 Stefan Fritsch. 6 * Copyright (c) 2010 Minoura Makoto. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 /* 31 * Part of the file derived from `Virtio PCI Card Specification v0.8.6 DRAFT' 32 * Appendix A. 33 */ 34 /* An interface for efficient virtio implementation. 35 * 36 * This header is BSD licensed so anyone can use the definitions 37 * to implement compatible drivers/servers. 38 * 39 * Copyright 2007, 2009, IBM Corporation 40 * All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of IBM nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 */ 65 66 67 #ifndef _DEV_PV_VIRTIOREG_H_ 68 #define _DEV_PV_VIRTIOREG_H_ 69 70 #include <sys/types.h> 71 72 /* Virtio product id (subsystem) */ 73 #define PCI_PRODUCT_VIRTIO_NETWORK 1 74 #define PCI_PRODUCT_VIRTIO_BLOCK 2 75 #define PCI_PRODUCT_VIRTIO_CONSOLE 3 76 #define PCI_PRODUCT_VIRTIO_ENTROPY 4 77 #define PCI_PRODUCT_VIRTIO_BALLOON 5 78 #define PCI_PRODUCT_VIRTIO_IOMEM 6 79 #define PCI_PRODUCT_VIRTIO_RPMSG 7 80 #define PCI_PRODUCT_VIRTIO_SCSI 8 81 #define PCI_PRODUCT_VIRTIO_9P 9 82 #define PCI_PRODUCT_VIRTIO_MAC80211 10 83 #define PCI_PRODUCT_VIRTIO_GPU 16 84 #define PCI_PRODUCT_VIRTIO_VMMCI 65535 /* private id */ 85 86 /* device-independent feature bits */ 87 #define VIRTIO_F_NOTIFY_ON_EMPTY (1ULL<<24) 88 #define VIRTIO_F_RING_INDIRECT_DESC (1ULL<<28) 89 #define VIRTIO_F_RING_EVENT_IDX (1ULL<<29) 90 #define VIRTIO_F_BAD_FEATURE (1ULL<<30) 91 #define VIRTIO_F_VERSION_1 (1ULL<<32) 92 93 /* device status bits */ 94 #define VIRTIO_CONFIG_DEVICE_STATUS_RESET 0 95 #define VIRTIO_CONFIG_DEVICE_STATUS_ACK 1 96 #define VIRTIO_CONFIG_DEVICE_STATUS_DRIVER 2 97 #define VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK 4 98 #define VIRTIO_CONFIG_DEVICE_STATUS_FEATURES_OK 8 99 #define VIRTIO_CONFIG_DEVICE_STATUS_DEVICE_NEEDS_RESET 64 100 #define VIRTIO_CONFIG_DEVICE_STATUS_FAILED 128 101 102 /* Virtqueue */ 103 /* This marks a buffer as continuing via the next field. */ 104 #define VRING_DESC_F_NEXT 1 105 /* This marks a buffer as write-only (otherwise read-only). */ 106 #define VRING_DESC_F_WRITE 2 107 /* This means the buffer contains a list of buffer descriptors. */ 108 #define VRING_DESC_F_INDIRECT 4 109 110 /* The Host uses this in used->flags to advise the Guest: don't kick me 111 * when you add a buffer. It's unreliable, so it's simply an 112 * optimization. Guest will still kick if it's out of buffers. */ 113 #define VRING_USED_F_NO_NOTIFY 1 114 /* The Guest uses this in avail->flags to advise the Host: don't 115 * interrupt me when you consume a buffer. It's unreliable, so it's 116 * simply an optimization. */ 117 #define VRING_AVAIL_F_NO_INTERRUPT 1 118 119 120 /* The standard layout for the ring is a continuous chunk of memory which 121 * looks like this. We assume num is a power of 2. 122 * 123 * struct vring { 124 * // The actual descriptors (16 bytes each) 125 * struct vring_desc desc[num]; 126 * 127 * // A ring of available descriptor heads with free-running index. 128 * __u16 avail_flags; 129 * __u16 avail_idx; 130 * __u16 available[num]; 131 * __u16 used_event_idx 132 * 133 * // Padding to the next align boundary. 134 * char pad[]; 135 * 136 * // A ring of used descriptor heads with free-running index. 137 * __u16 used_flags; 138 * __u16 used_idx; 139 * struct vring_used_elem used[num]; 140 * __u16 avail_event_idx; 141 * }; 142 * Note: for virtio PCI, align is 4096. 143 */ 144 145 /* Virtio ring descriptors: 16 bytes. 146 * These can chain together via "next". */ 147 struct vring_desc { 148 /* Address (guest-physical). */ 149 uint64_t addr; 150 /* Length. */ 151 uint32_t len; 152 /* The flags as indicated above. */ 153 uint16_t flags; 154 /* We chain unused descriptors via this, too */ 155 uint16_t next; 156 } __packed; 157 158 struct vring_avail { 159 uint16_t flags; 160 uint16_t idx; 161 uint16_t ring[0]; 162 } __packed; 163 164 /* u32 is used here for ids for padding reasons. */ 165 struct vring_used_elem { 166 /* Index of start of used descriptor chain. */ 167 uint32_t id; 168 /* Total length of the descriptor chain which was written to. */ 169 uint32_t len; 170 } __packed; 171 172 struct vring_used { 173 uint16_t flags; 174 uint16_t idx; 175 struct vring_used_elem ring[0]; 176 } __packed; 177 178 /* 179 * We publish the used event index at the end of the available ring, and vice 180 * versa. They are at the end for backwards compatibility. 181 */ 182 #define VQ_USED_EVENT(vq) (*(uint16_t*)(&(vq)->vq_avail->ring[(vq)->vq_num])) 183 #define VQ_AVAIL_EVENT(vq) (*(uint16_t*)(&(vq)->vq_used->ring[(vq)->vq_num])) 184 185 #define VIRTIO_PAGE_SIZE (4096) 186 187 #endif /* _DEV_PV_VIRTIOREG_H_ */ 188