1
/******************************************************************************
2
* Copyright (c) 2011 IBM Corporation
4
* This program and the accompanying materials
5
* are made available under the terms of the BSD License
6
* which accompanies this distribution, and is available at
7
* http://www.opensource.org/licenses/bsd-license.php
10
* IBM Corporation - initial implementation
11
*****************************************************************************/
19
/* Device status bits */
20
#define VIRTIO_STAT_ACKNOWLEDGE 1
21
#define VIRTIO_STAT_DRIVER 2
22
#define VIRTIO_STAT_DRIVER_OK 4
23
#define VIRTIO_STAT_FEATURES_OK 8
24
#define VIRTIO_STAT_NEEDS_RESET 64
25
#define VIRTIO_STAT_FAILED 128
27
#define BIT(x) (1UL << (x))
29
/* VIRTIO 1.0 Device independent feature bits */
30
#define VIRTIO_F_RING_INDIRECT_DESC BIT(28)
31
#define VIRTIO_F_RING_EVENT_IDX BIT(29)
32
#define VIRTIO_F_VERSION_1 BIT(32)
34
#define VIRTIO_TIMEOUT 5000 /* 5 sec timeout */
36
/* Definitions for vring_desc.flags */
37
#define VRING_DESC_F_NEXT 1 /* buffer continues via the next field */
38
#define VRING_DESC_F_WRITE 2 /* buffer is write-only (otherwise read-only) */
39
#define VRING_DESC_F_INDIRECT 4 /* buffer contains a list of buffer descriptors */
41
/* Descriptor table entry - see Virtio Spec chapter 2.3.2 */
43
uint64_t addr; /* Address (guest-physical) */
44
uint32_t len; /* Length */
45
uint16_t flags; /* The flags as indicated above */
46
uint16_t next; /* Next field if flags & NEXT */
49
/* Definitions for vring_avail.flags */
50
#define VRING_AVAIL_F_NO_INTERRUPT 1
52
/* Available ring - see Virtio Spec chapter 2.3.4 */
59
/* Definitions for vring_used.flags */
60
#define VRING_USED_F_NO_NOTIFY 1
62
struct vring_used_elem {
63
uint32_t id; /* Index of start of used descriptor chain */
64
uint32_t len; /* Total length of the descriptor chain which was used */
70
struct vring_used_elem ring[];
73
/* Structure shared with SLOF and is 16bytes */
81
struct virtio_device {
82
uint32_t is_modern; /* Indicates whether to use virtio 1.0 */
83
struct virtio_cap legacy;
84
struct virtio_cap common;
85
struct virtio_cap notify;
86
struct virtio_cap isr;
87
struct virtio_cap device;
88
struct virtio_cap pci;
89
uint32_t notify_off_mul;
93
uint64_t id; /* Queue ID */
96
struct vring_desc *desc;
97
struct vring_avail *avail;
98
struct vring_used *used;
101
/* Parts of the virtqueue are aligned on a 4096 byte page boundary */
102
#define VQ_ALIGN(addr) (((addr) + 0xfff) & ~0xfff)
104
extern unsigned long virtio_vring_size(unsigned int qsize);
105
extern unsigned int virtio_get_qsize(struct virtio_device *dev, int queue);
106
extern struct vring_desc *virtio_get_vring_desc(struct virtio_device *dev, int queue);
107
extern struct vring_avail *virtio_get_vring_avail(struct virtio_device *dev, int queue);
108
extern struct vring_used *virtio_get_vring_used(struct virtio_device *dev, int queue);
109
extern void virtio_fill_desc(struct vring_desc *desc, bool is_modern,
110
uint64_t addr, uint32_t len,
111
uint16_t flags, uint16_t next);
112
extern int virtio_queue_init_vq(struct virtio_device *dev, struct vqs *vq, unsigned int id);
114
extern struct virtio_device *virtio_setup_vd(void);
115
extern void virtio_reset_device(struct virtio_device *dev);
116
extern void virtio_queue_notify(struct virtio_device *dev, int queue);
117
extern void virtio_set_status(struct virtio_device *dev, int status);
118
extern void virtio_get_status(struct virtio_device *dev, int *status);
119
extern void virtio_set_qaddr(struct virtio_device *dev, int queue, unsigned long qaddr);
120
extern void virtio_set_guest_features(struct virtio_device *dev, uint64_t features);
121
extern uint64_t virtio_get_host_features(struct virtio_device *dev);
122
extern int virtio_negotiate_guest_features(struct virtio_device *dev, uint64_t features);
123
extern uint64_t virtio_get_config(struct virtio_device *dev, int offset, int size);
124
extern int __virtio_read_config(struct virtio_device *dev, void *dst,
125
int offset, int len);
128
#endif /* _LIBVIRTIO_H */