kernel/virtio.h
96 lines
1//2// virtio device definitions.3// for both the mmio interface, and virtio descriptors.4// only tested with qemu.5//6// the virtio spec:7// https://docs.oasis-open.org/virtio/virtio/v1.1/virtio-v1.1.pdf8//910// virtio mmio control registers, mapped starting at 0x10001000.11// from qemu virtio_mmio.h12#define VIRTIO_MMIO_MAGIC_VALUE 0x000 // 0x7472697613#define VIRTIO_MMIO_VERSION 0x004 // version; should be 214#define VIRTIO_MMIO_DEVICE_ID 0x008 // device type; 1 is net, 2 is disk15#define VIRTIO_MMIO_VENDOR_ID 0x00c // 0x554d455116#define VIRTIO_MMIO_DEVICE_FEATURES 0x01017#define VIRTIO_MMIO_DRIVER_FEATURES 0x02018#define VIRTIO_MMIO_QUEUE_SEL 0x030 // select queue, write-only19#define VIRTIO_MMIO_QUEUE_NUM_MAX 0x034 // max size of current queue, read-only20#define VIRTIO_MMIO_QUEUE_NUM 0x038 // size of current queue, write-only21#define VIRTIO_MMIO_QUEUE_READY 0x044 // ready bit22#define VIRTIO_MMIO_QUEUE_NOTIFY 0x050 // write-only23#define VIRTIO_MMIO_INTERRUPT_STATUS 0x060 // read-only24#define VIRTIO_MMIO_INTERRUPT_ACK 0x064 // write-only25#define VIRTIO_MMIO_STATUS 0x070 // read/write26#define VIRTIO_MMIO_QUEUE_DESC_LOW 0x080 // physical address for descriptor table, write-only27#define VIRTIO_MMIO_QUEUE_DESC_HIGH 0x08428#define VIRTIO_MMIO_DRIVER_DESC_LOW 0x090 // physical address for available ring, write-only29#define VIRTIO_MMIO_DRIVER_DESC_HIGH 0x09430#define VIRTIO_MMIO_DEVICE_DESC_LOW 0x0a0 // physical address for used ring, write-only31#define VIRTIO_MMIO_DEVICE_DESC_HIGH 0x0a43233// status register bits, from qemu virtio_config.h34#define VIRTIO_CONFIG_S_ACKNOWLEDGE 135#define VIRTIO_CONFIG_S_DRIVER 236#define VIRTIO_CONFIG_S_DRIVER_OK 437#define VIRTIO_CONFIG_S_FEATURES_OK 83839// device feature bits40#define VIRTIO_BLK_F_RO 5 /* Disk is read-only */41#define VIRTIO_BLK_F_SCSI 7 /* Supports scsi command passthru */42#define VIRTIO_BLK_F_CONFIG_WCE 11 /* Writeback mode available in config */43#define VIRTIO_BLK_F_MQ 12 /* support more than one vq */44#define VIRTIO_F_ANY_LAYOUT 2745#define VIRTIO_RING_F_INDIRECT_DESC 2846#define VIRTIO_RING_F_EVENT_IDX 294748// this many virtio descriptors.49// must be a power of two.50#define NUM 85152// a single descriptor, from the spec.53struct virtq_desc {54 uint64 addr;55 uint32 len;56 uint16 flags;57 uint16 next;58};59#define VRING_DESC_F_NEXT 1 // chained with another descriptor60#define VRING_DESC_F_WRITE 2 // device writes (vs read)6162// the (entire) avail ring, from the spec.63struct virtq_avail {64 uint16 flags; // always zero65 uint16 idx; // driver will write ring[idx] next66 uint16 ring[NUM]; // descriptor numbers of chain heads67 uint16 unused;68};6970// one entry in the "used" ring, with which the71// device tells the driver about completed requests.72struct virtq_used_elem {73 uint32 id; // index of start of completed descriptor chain74 uint32 len;75};7677struct virtq_used {78 uint16 flags; // always zero79 uint16 idx; // device increments when it adds a ring[] entry80 struct virtq_used_elem ring[NUM];81};8283// these are specific to virtio block devices, e.g. disks,84// described in Section 5.2 of the spec.8586#define VIRTIO_BLK_T_IN 0 // read the disk87#define VIRTIO_BLK_T_OUT 1 // write the disk8889// the format of the first descriptor in a disk request.90// to be followed by two more descriptors containing91// the block, and a one-byte status.92struct virtio_blk_req {93 uint32 type; // VIRTIO_BLK_T_IN or ..._OUT94 uint32 reserved;95 uint64 sector;96};97