4 #include "types.h" // u64
5 #include "memmap.h" // PAGE_SIZE
8 #define PAGE_MASK (PAGE_SIZE-1)
10 #define virt_to_phys(v) (unsigned long)(v)
11 #define phys_to_virt(p) (void*)(p)
12 /* Compiler barrier is enough as an x86 CPU does not reorder reads or writes */
13 #define smp_rmb() barrier()
14 #define smp_wmb() barrier()
16 /* Status byte for guest to report progress, and synchronize features. */
17 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
18 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
19 /* We have found a driver for the device. */
20 #define VIRTIO_CONFIG_S_DRIVER 2
21 /* Driver has used its parts of the config, and is happy */
22 #define VIRTIO_CONFIG_S_DRIVER_OK 4
23 /* We've given up on this device. */
24 #define VIRTIO_CONFIG_S_FAILED 0x80
26 #define MAX_QUEUE_NUM (128)
28 #define VRING_DESC_F_NEXT 1
29 #define VRING_DESC_F_WRITE 2
31 #define VRING_AVAIL_F_NO_INTERRUPT 1
33 #define VRING_USED_F_NO_NOTIFY 1
50 struct vring_used_elem
60 struct vring_used_elem ring[];
65 struct vring_desc *desc;
66 struct vring_avail *avail;
67 struct vring_used *used;
70 #define vring_size(num) \
71 (((((sizeof(struct vring_desc) * num) + \
72 (sizeof(struct vring_avail) + sizeof(u16) * num)) \
73 + PAGE_MASK) & ~PAGE_MASK) + \
74 (sizeof(struct vring_used) + sizeof(struct vring_used_elem) * num))
76 typedef unsigned char virtio_queue_t[vring_size(MAX_QUEUE_NUM)];
78 struct vring_virtqueue {
83 u16 vdata[MAX_QUEUE_NUM];
93 static inline void vring_init(struct vring *vr,
94 unsigned int num, unsigned char *queue)
102 /* physical address of desc must be page aligned */
104 pa = virt_to_phys(queue);
105 pa = (pa + PAGE_MASK) & ~PAGE_MASK;
106 vr->desc = phys_to_virt(pa);
108 vr->avail = (struct vring_avail *)&vr->desc[num];
109 /* disable interrupts */
110 vr->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
112 /* physical address of used must be page aligned */
114 pa = virt_to_phys(&vr->avail->ring[num]);
115 pa = (pa + PAGE_MASK) & ~PAGE_MASK;
116 vr->used = phys_to_virt(pa);
118 for (i = 0; i < num - 1; i++)
119 vr->desc[i].next = i + 1;
120 vr->desc[i].next = 0;
123 int vring_more_used(struct vring_virtqueue *vq);
124 void vring_detach(struct vring_virtqueue *vq, unsigned int head);
125 int vring_get_buf(struct vring_virtqueue *vq, unsigned int *len);
126 void vring_add_buf(struct vring_virtqueue *vq, struct vring_list list[],
127 unsigned int out, unsigned int in,
128 int index, int num_added);
129 void vring_kick(unsigned int ioaddr, struct vring_virtqueue *vq, int num_added);
131 #endif /* _VIRTIO_RING_H_ */