1 /******************************************************************************
2  * Copyright (c) 2011 IBM Corporation
3  * All rights reserved.
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
8  *
9  * Contributors:
10  *     IBM Corporation - initial implementation
11  *****************************************************************************/
12 
13 #ifndef _LIBVIRTIO_H
14 #define _LIBVIRTIO_H
15 
16 #include <stdint.h>
17 #include <stdbool.h>
18 
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
26 
27 #define BIT(x) (1UL << (x))
28 
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)
33 
34 #define VIRTIO_TIMEOUT		        5000 /* 5 sec timeout */
35 
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 */
40 
41 /* Descriptor table entry - see Virtio Spec chapter 2.3.2 */
42 struct vring_desc {
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 */
47 };
48 
49 /* Definitions for vring_avail.flags */
50 #define VRING_AVAIL_F_NO_INTERRUPT	1
51 
52 /* Available ring - see Virtio Spec chapter 2.3.4 */
53 struct vring_avail {
54 	uint16_t flags;
55 	uint16_t idx;
56 	uint16_t ring[];
57 };
58 
59 /* Definitions for vring_used.flags */
60 #define VRING_USED_F_NO_NOTIFY		1
61 
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 */
65 };
66 
67 struct vring_used {
68 	uint16_t flags;
69 	uint16_t idx;
70 	struct vring_used_elem ring[];
71 };
72 
73 /* Structure shared with SLOF and is 16bytes */
74 struct virtio_cap {
75 	void *addr;
76 	uint8_t bar;
77 	uint8_t is_io;
78 	uint8_t cap_id;
79 };
80 
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;
90 };
91 
92 struct vqs {
93 	uint64_t id;	/* Queue ID */
94 	uint32_t size;
95 	void *buf_mem;
96 	struct vring_desc *desc;
97 	struct vring_avail *avail;
98 	struct vring_used *used;
99 };
100 
101 /* Parts of the virtqueue are aligned on a 4096 byte page boundary */
102 #define VQ_ALIGN(addr)	(((addr) + 0xfff) & ~0xfff)
103 
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);
113 extern void virtio_queue_term_vq(struct virtio_device *dev, struct vqs *vq, unsigned int id);
114 
115 extern struct virtio_device *virtio_setup_vd(void);
116 extern void virtio_reset_device(struct virtio_device *dev);
117 extern void virtio_queue_notify(struct virtio_device *dev, int queue);
118 extern void virtio_set_status(struct virtio_device *dev, int status);
119 extern void virtio_get_status(struct virtio_device *dev, int *status);
120 extern void virtio_set_qaddr(struct virtio_device *dev, int queue, unsigned long qaddr);
121 extern void virtio_set_guest_features(struct virtio_device *dev, uint64_t features);
122 extern uint64_t virtio_get_host_features(struct virtio_device *dev);
123 extern int virtio_negotiate_guest_features(struct virtio_device *dev, uint64_t features);
124 extern uint64_t virtio_get_config(struct virtio_device *dev, int offset, int size);
125 extern int __virtio_read_config(struct virtio_device *dev, void *dst,
126 				int offset, int len);
127 
128 
129 #endif /* _LIBVIRTIO_H */
130