tools/virtio: fix build for 3.8
[platform/adaptation/renesas_rcar/renesas_kernel.git] / tools / virtio / linux / virtio.h
1 #ifndef LINUX_VIRTIO_H
2 #define LINUX_VIRTIO_H
3
4 #include <stdbool.h>
5 #include <stdlib.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <assert.h>
10
11 #include <linux/types.h>
12 #include <errno.h>
13
14 typedef unsigned long long dma_addr_t;
15
16 struct scatterlist {
17         unsigned long   page_link;
18         unsigned int    offset;
19         unsigned int    length;
20         dma_addr_t      dma_address;
21 };
22
23 struct page {
24         unsigned long long dummy;
25 };
26
27 #define BUG_ON(__BUG_ON_cond) assert(!(__BUG_ON_cond))
28
29 /* Physical == Virtual */
30 #define virt_to_phys(p) ((unsigned long)p)
31 #define phys_to_virt(a) ((void *)(unsigned long)(a))
32 /* Page address: Virtual / 4K */
33 #define virt_to_page(p) ((struct page*)((virt_to_phys(p) / 4096) * \
34                                         sizeof(struct page)))
35 #define offset_in_page(p) (((unsigned long)p) % 4096)
36 #define sg_phys(sg) ((sg->page_link & ~0x3) / sizeof(struct page) * 4096 + \
37                      sg->offset)
38 static inline void sg_mark_end(struct scatterlist *sg)
39 {
40         /*
41          * Set termination bit, clear potential chain bit
42          */
43         sg->page_link |= 0x02;
44         sg->page_link &= ~0x01;
45 }
46 static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
47 {
48         memset(sgl, 0, sizeof(*sgl) * nents);
49         sg_mark_end(&sgl[nents - 1]);
50 }
51 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
52 {
53         unsigned long page_link = sg->page_link & 0x3;
54
55         /*
56          * In order for the low bit stealing approach to work, pages
57          * must be aligned at a 32-bit boundary as a minimum.
58          */
59         BUG_ON((unsigned long) page & 0x03);
60         sg->page_link = page_link | (unsigned long) page;
61 }
62
63 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
64                                unsigned int len, unsigned int offset)
65 {
66         sg_assign_page(sg, page);
67         sg->offset = offset;
68         sg->length = len;
69 }
70
71 static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
72                               unsigned int buflen)
73 {
74         sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
75 }
76
77 static inline void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
78 {
79         sg_init_table(sg, 1);
80         sg_set_buf(sg, buf, buflen);
81 }
82
83 typedef __u16 u16;
84
85 typedef enum {
86         GFP_KERNEL,
87         GFP_ATOMIC,
88         __GFP_HIGHMEM,
89         __GFP_HIGH
90 } gfp_t;
91 typedef enum {
92         IRQ_NONE,
93         IRQ_HANDLED
94 } irqreturn_t;
95
96 static inline void *kmalloc(size_t s, gfp_t gfp)
97 {
98         return malloc(s);
99 }
100
101 static inline void kfree(void *p)
102 {
103         free(p);
104 }
105
106 #define container_of(ptr, type, member) ({                      \
107         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
108         (type *)( (char *)__mptr - offsetof(type,member) );})
109
110 #define uninitialized_var(x) x = x
111
112 # ifndef likely
113 #  define likely(x)     (__builtin_expect(!!(x), 1))
114 # endif
115 # ifndef unlikely
116 #  define unlikely(x)   (__builtin_expect(!!(x), 0))
117 # endif
118
119 #define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
120 #ifdef DEBUG
121 #define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
122 #else
123 #define pr_debug(format, ...) do {} while (0)
124 #endif
125 #define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
126 #define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
127
128 /* TODO: empty stubs for now. Broken but enough for virtio_ring.c */
129 #define list_add_tail(a, b) do {} while (0)
130 #define list_del(a) do {} while (0)
131
132 #define BIT_WORD(nr)            ((nr) / BITS_PER_LONG)
133 #define BITS_PER_BYTE           8
134 #define BITS_PER_LONG (sizeof(long) * BITS_PER_BYTE)
135 #define BIT_MASK(nr)            (1UL << ((nr) % BITS_PER_LONG))
136 /* TODO: Not atomic as it should be:
137  * we don't use this for anything important. */
138 static inline void clear_bit(int nr, volatile unsigned long *addr)
139 {
140         unsigned long mask = BIT_MASK(nr);
141         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
142
143         *p &= ~mask;
144 }
145
146 static inline int test_bit(int nr, const volatile unsigned long *addr)
147 {
148         return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
149 }
150
151 /* The only feature we care to support */
152 #define virtio_has_feature(dev, feature) \
153         test_bit((feature), (dev)->features)
154 /* end of stubs */
155
156 struct virtio_device {
157         void *dev;
158         unsigned long features[1];
159 };
160
161 struct virtqueue {
162         /* TODO: commented as list macros are empty stubs for now.
163          * Broken but enough for virtio_ring.c
164          * struct list_head list; */
165         void (*callback)(struct virtqueue *vq);
166         const char *name;
167         struct virtio_device *vdev;
168         unsigned int index;
169         unsigned int num_free;
170         void *priv;
171 };
172
173 #define EXPORT_SYMBOL_GPL(__EXPORT_SYMBOL_GPL_name) \
174         void __EXPORT_SYMBOL_GPL##__EXPORT_SYMBOL_GPL_name() { \
175 }
176 #define MODULE_LICENSE(__MODULE_LICENSE_value) \
177         const char *__MODULE_LICENSE_name = __MODULE_LICENSE_value
178
179 #define CONFIG_SMP
180
181 #if defined(__i386__) || defined(__x86_64__)
182 #define barrier() asm volatile("" ::: "memory")
183 #define mb() __sync_synchronize()
184
185 #define smp_mb()        mb()
186 # define smp_rmb()      barrier()
187 # define smp_wmb()      barrier()
188 /* Weak barriers should be used. If not - it's a bug */
189 # define rmb()  abort()
190 # define wmb()  abort()
191 #else
192 #error Please fill in barrier macros
193 #endif
194
195 /* Interfaces exported by virtio_ring. */
196 int virtqueue_add_buf(struct virtqueue *vq,
197                       struct scatterlist sg[],
198                       unsigned int out_num,
199                       unsigned int in_num,
200                       void *data,
201                       gfp_t gfp);
202
203 void virtqueue_kick(struct virtqueue *vq);
204
205 void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
206
207 void virtqueue_disable_cb(struct virtqueue *vq);
208
209 bool virtqueue_enable_cb(struct virtqueue *vq);
210 bool virtqueue_enable_cb_delayed(struct virtqueue *vq);
211
212 void *virtqueue_detach_unused_buf(struct virtqueue *vq);
213 struct virtqueue *vring_new_virtqueue(unsigned int index,
214                                       unsigned int num,
215                                       unsigned int vring_align,
216                                       struct virtio_device *vdev,
217                                       bool weak_barriers,
218                                       void *pages,
219                                       void (*notify)(struct virtqueue *vq),
220                                       void (*callback)(struct virtqueue *vq),
221                                       const char *name);
222 void vring_del_virtqueue(struct virtqueue *vq);
223
224 #endif