1 /* $OpenBSD: virtioreg.h,v 1.6 2024/07/26 07:55:23 sf 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_ANY_LAYOUT (1ULL<<27) 89 #define VIRTIO_F_RING_INDIRECT_DESC (1ULL<<28) 90 #define VIRTIO_F_RING_EVENT_IDX (1ULL<<29) 91 #define VIRTIO_F_BAD_FEATURE (1ULL<<30) 92 #define VIRTIO_F_VERSION_1 (1ULL<<32) 93 #define VIRTIO_F_ACCESS_PLATFORM (1ULL<<33) 94 #define VIRTIO_F_RING_PACKED (1ULL<<34) 95 #define VIRTIO_F_IN_ORDER (1ULL<<35) 96 #define VIRTIO_F_ORDER_PLATFORM (1ULL<<36) 97 #define VIRTIO_F_SR_IOV (1ULL<<37) 98 #define VIRTIO_F_NOTIFICATION_DATA (1ULL<<38) 99 #define VIRTIO_F_NOTIF_CONFIG_DATA (1ULL<<39) 100 #define VIRTIO_F_RING_RESET (1ULL<<40) 101 102 /* device status bits */ 103 #define VIRTIO_CONFIG_DEVICE_STATUS_RESET 0 104 #define VIRTIO_CONFIG_DEVICE_STATUS_ACK 1 105 #define VIRTIO_CONFIG_DEVICE_STATUS_DRIVER 2 106 #define VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK 4 107 #define VIRTIO_CONFIG_DEVICE_STATUS_FEATURES_OK 8 108 #define VIRTIO_CONFIG_DEVICE_STATUS_DEVICE_NEEDS_RESET 64 109 #define VIRTIO_CONFIG_DEVICE_STATUS_FAILED 128 110 111 /* Virtqueue */ 112 /* This marks a buffer as continuing via the next field. */ 113 #define VRING_DESC_F_NEXT 1 114 /* This marks a buffer as write-only (otherwise read-only). */ 115 #define VRING_DESC_F_WRITE 2 116 /* This means the buffer contains a list of buffer descriptors. */ 117 #define VRING_DESC_F_INDIRECT 4 118 119 /* The Host uses this in used->flags to advise the Guest: don't kick me 120 * when you add a buffer. It's unreliable, so it's simply an 121 * optimization. Guest will still kick if it's out of buffers. */ 122 #define VRING_USED_F_NO_NOTIFY 1 123 /* The Guest uses this in avail->flags to advise the Host: don't 124 * interrupt me when you consume a buffer. It's unreliable, so it's 125 * simply an optimization. */ 126 #define VRING_AVAIL_F_NO_INTERRUPT 1 127 128 129 /* The standard layout for the ring is a continuous chunk of memory which 130 * looks like this. We assume num is a power of 2. 131 * 132 * struct vring { 133 * // The actual descriptors (16 bytes each) 134 * struct vring_desc desc[num]; 135 * 136 * // A ring of available descriptor heads with free-running index. 137 * __u16 avail_flags; 138 * __u16 avail_idx; 139 * __u16 available[num]; 140 * __u16 used_event_idx 141 * 142 * // Padding to the next align boundary. 143 * char pad[]; 144 * 145 * // A ring of used descriptor heads with free-running index. 146 * __u16 used_flags; 147 * __u16 used_idx; 148 * struct vring_used_elem used[num]; 149 * __u16 avail_event_idx; 150 * }; 151 * Note: for virtio PCI, align is 4096. 152 */ 153 154 /* Virtio ring descriptors: 16 bytes. 155 * These can chain together via "next". */ 156 struct vring_desc { 157 /* Address (guest-physical). */ 158 uint64_t addr; 159 /* Length. */ 160 uint32_t len; 161 /* The flags as indicated above. */ 162 uint16_t flags; 163 /* We chain unused descriptors via this, too */ 164 uint16_t next; 165 } __packed; 166 167 struct vring_avail { 168 uint16_t flags; 169 uint16_t idx; 170 uint16_t ring[0]; 171 } __packed; 172 173 /* u32 is used here for ids for padding reasons. */ 174 struct vring_used_elem { 175 /* Index of start of used descriptor chain. */ 176 uint32_t id; 177 /* Total length of the descriptor chain which was written to. */ 178 uint32_t len; 179 } __packed; 180 181 struct vring_used { 182 uint16_t flags; 183 uint16_t idx; 184 struct vring_used_elem ring[0]; 185 } __packed; 186 187 /* 188 * We publish the used event index at the end of the available ring, and vice 189 * versa. They are at the end for backwards compatibility. 190 */ 191 #define VQ_USED_EVENT(vq) (*(uint16_t*)(&(vq)->vq_avail->ring[(vq)->vq_num])) 192 #define VQ_AVAIL_EVENT(vq) (*(uint16_t*)(&(vq)->vq_used->ring[(vq)->vq_num])) 193 194 #define VIRTIO_PAGE_SIZE (4096) 195 196 #endif /* _DEV_PV_VIRTIOREG_H_ */ 197