11 #include <linux/types.h>
14 typedef unsigned long long dma_addr_t
;
17 unsigned long page_link
;
20 dma_addr_t dma_address
;
24 unsigned long long dummy
;
27 #define BUG_ON(__BUG_ON_cond) assert(!(__BUG_ON_cond))
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) * \
35 #define offset_in_page(p) (((unsigned long)p) % 4096)
36 #define sg_phys(sg) ((sg->page_link & ~0x3) / sizeof(struct page) * 4096 + \
38 static inline void sg_mark_end(struct scatterlist
*sg
)
41 * Set termination bit, clear potential chain bit
43 sg
->page_link
|= 0x02;
44 sg
->page_link
&= ~0x01;
46 static inline void sg_init_table(struct scatterlist
*sgl
, unsigned int nents
)
48 memset(sgl
, 0, sizeof(*sgl
) * nents
);
49 sg_mark_end(&sgl
[nents
- 1]);
51 static inline void sg_assign_page(struct scatterlist
*sg
, struct page
*page
)
53 unsigned long page_link
= sg
->page_link
& 0x3;
56 * In order for the low bit stealing approach to work, pages
57 * must be aligned at a 32-bit boundary as a minimum.
59 BUG_ON((unsigned long) page
& 0x03);
60 sg
->page_link
= page_link
| (unsigned long) page
;
63 static inline void sg_set_page(struct scatterlist
*sg
, struct page
*page
,
64 unsigned int len
, unsigned int offset
)
66 sg_assign_page(sg
, page
);
71 static inline void sg_set_buf(struct scatterlist
*sg
, const void *buf
,
74 sg_set_page(sg
, virt_to_page(buf
), buflen
, offset_in_page(buf
));
77 static inline void sg_init_one(struct scatterlist
*sg
, const void *buf
, unsigned int buflen
)
80 sg_set_buf(sg
, buf
, buflen
);
94 static inline void *kmalloc(size_t s
, gfp_t gfp
)
99 static inline void kfree(void *p
)
104 #define container_of(ptr, type, member) ({ \
105 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
106 (type *)( (char *)__mptr - offsetof(type,member) );})
108 #define uninitialized_var(x) x = x
111 # define likely(x) (__builtin_expect(!!(x), 1))
114 # define unlikely(x) (__builtin_expect(!!(x), 0))
117 #define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
119 #define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
121 #define pr_debug(format, ...) do {} while (0)
123 #define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
124 #define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
126 /* TODO: empty stubs for now. Broken but enough for virtio_ring.c */
127 #define list_add_tail(a, b) do {} while (0)
128 #define list_del(a) do {} while (0)
130 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
131 #define BITS_PER_BYTE 8
132 #define BITS_PER_LONG (sizeof(long) * BITS_PER_BYTE)
133 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
134 /* TODO: Not atomic as it should be:
135 * we don't use this for anything important. */
136 static inline void clear_bit(int nr
, volatile unsigned long *addr
)
138 unsigned long mask
= BIT_MASK(nr
);
139 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
144 static inline int test_bit(int nr
, const volatile unsigned long *addr
)
146 return 1UL & (addr
[BIT_WORD(nr
)] >> (nr
& (BITS_PER_LONG
-1)));
149 /* The only feature we care to support */
150 #define virtio_has_feature(dev, feature) \
151 test_bit((feature), (dev)->features)
154 struct virtio_device
{
156 unsigned long features
[1];
160 /* TODO: commented as list macros are empty stubs for now.
161 * Broken but enough for virtio_ring.c
162 * struct list_head list; */
163 void (*callback
)(struct virtqueue
*vq
);
165 struct virtio_device
*vdev
;
169 #define EXPORT_SYMBOL_GPL(__EXPORT_SYMBOL_GPL_name) \
170 void __EXPORT_SYMBOL_GPL##__EXPORT_SYMBOL_GPL_name() { \
172 #define MODULE_LICENSE(__MODULE_LICENSE_value) \
173 const char *__MODULE_LICENSE_name = __MODULE_LICENSE_value
177 #if defined(__i386__) || defined(__x86_64__)
178 #define barrier() asm volatile("" ::: "memory")
179 #define mb() __sync_synchronize()
181 #define smp_mb() mb()
182 # define smp_rmb() barrier()
183 # define smp_wmb() barrier()
185 #error Please fill in barrier macros
188 /* Interfaces exported by virtio_ring. */
189 int virtqueue_add_buf(struct virtqueue
*vq
,
190 struct scatterlist sg
[],
191 unsigned int out_num
,
196 void virtqueue_kick(struct virtqueue
*vq
);
198 void *virtqueue_get_buf(struct virtqueue
*vq
, unsigned int *len
);
200 void virtqueue_disable_cb(struct virtqueue
*vq
);
202 bool virtqueue_enable_cb(struct virtqueue
*vq
);
204 void *virtqueue_detach_unused_buf(struct virtqueue
*vq
);
205 struct virtqueue
*vring_new_virtqueue(unsigned int num
,
206 unsigned int vring_align
,
207 struct virtio_device
*vdev
,
210 void (*notify
)(struct virtqueue
*vq
),
211 void (*callback
)(struct virtqueue
*vq
),
213 void vring_del_virtqueue(struct virtqueue
*vq
);