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