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