virtio_ring.h 2.9 KB
Newer Older
1 2 3
#ifndef _LINUX_VIRTIO_RING_H
#define _LINUX_VIRTIO_RING_H

4
#include <asm/barrier.h>
5
#include <linux/irqreturn.h>
6 7
#include <uapi/linux/virtio_ring.h>

8 9 10 11 12 13 14
/*
 * Barriers in virtio are tricky.  Non-SMP virtio guests can't assume
 * they're not on an SMP host system, so they need to assume real
 * barriers.  Non-SMP virtio hosts could skip the barriers, but does
 * anyone care?
 *
 * For virtio_pci on SMP, we don't need to order with respect to MMIO
15
 * accesses through relaxed memory I/O windows, so virt_mb() et al are
16 17 18 19 20 21 22 23 24 25 26
 * sufficient.
 *
 * For using virtio to talk to real devices (eg. other heterogeneous
 * CPUs) we do need real barriers.  In theory, we could be using both
 * kinds of virtio, so it's a runtime decision, and the branch is
 * actually quite cheap.
 */

static inline void virtio_mb(bool weak_barriers)
{
	if (weak_barriers)
27
		virt_mb();
28 29 30 31 32 33 34
	else
		mb();
}

static inline void virtio_rmb(bool weak_barriers)
{
	if (weak_barriers)
35
		virt_rmb();
36 37 38 39 40 41 42
	else
		rmb();
}

static inline void virtio_wmb(bool weak_barriers)
{
	if (weak_barriers)
43
		virt_wmb();
44 45 46 47
	else
		wmb();
}

48 49 50 51 52 53 54 55 56 57 58
static inline void virtio_store_mb(bool weak_barriers,
				   __virtio16 *p, __virtio16 v)
{
	if (weak_barriers) {
		virt_store_mb(*p, v);
	} else {
		WRITE_ONCE(*p, v);
		mb();
	}
}

59 60 61
struct virtio_device;
struct virtqueue;

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
/*
 * Creates a virtqueue and allocates the descriptor ring.  If
 * may_reduce_num is set, then this may allocate a smaller ring than
 * expected.  The caller should query virtqueue_get_ring_size to learn
 * the actual size of the ring.
 */
struct virtqueue *vring_create_virtqueue(unsigned int index,
					 unsigned int num,
					 unsigned int vring_align,
					 struct virtio_device *vdev,
					 bool weak_barriers,
					 bool may_reduce_num,
					 bool (*notify)(struct virtqueue *vq),
					 void (*callback)(struct virtqueue *vq),
					 const char *name);

/* Creates a virtqueue with a custom layout. */
struct virtqueue *__vring_new_virtqueue(unsigned int index,
					struct vring vring,
					struct virtio_device *vdev,
					bool weak_barriers,
					bool (*notify)(struct virtqueue *),
					void (*callback)(struct virtqueue *),
					const char *name);

/*
 * Creates a virtqueue with a standard layout but a caller-allocated
 * ring.
 */
91 92
struct virtqueue *vring_new_virtqueue(unsigned int index,
				      unsigned int num,
93
				      unsigned int vring_align,
94
				      struct virtio_device *vdev,
95
				      bool weak_barriers,
96
				      void *pages,
97
				      bool (*notify)(struct virtqueue *vq),
98 99
				      void (*callback)(struct virtqueue *vq),
				      const char *name);
100 101 102 103 104

/*
 * Destroys a virtqueue.  If created with vring_create_virtqueue, this
 * also frees the ring.
 */
105
void vring_del_virtqueue(struct virtqueue *vq);
106

107 108
/* Filter out transport-specific feature bits. */
void vring_transport_features(struct virtio_device *vdev);
109 110 111

irqreturn_t vring_interrupt(int irq, void *_vq);
#endif /* _LINUX_VIRTIO_RING_H */