1
2#ifndef _LINUX_VIRTIO_CONFIG_H
3#define _LINUX_VIRTIO_CONFIG_H
4
5#include <linux/err.h>
6#include <linux/bug.h>
7#include <linux/virtio.h>
8#include <linux/virtio_byteorder.h>
9#include <uapi/linux/virtio_config.h>
10
11struct irq_affinity;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64typedef void vq_callback_t(struct virtqueue *);
65struct virtio_config_ops {
66 void (*get)(struct virtio_device *vdev, unsigned offset,
67 void *buf, unsigned len);
68 void (*set)(struct virtio_device *vdev, unsigned offset,
69 const void *buf, unsigned len);
70 u32 (*generation)(struct virtio_device *vdev);
71 u8 (*get_status)(struct virtio_device *vdev);
72 void (*set_status)(struct virtio_device *vdev, u8 status);
73 void (*reset)(struct virtio_device *vdev);
74 int (*find_vqs)(struct virtio_device *, unsigned nvqs,
75 struct virtqueue *vqs[], vq_callback_t *callbacks[],
76 const char * const names[], const bool *ctx,
77 struct irq_affinity *desc);
78 void (*del_vqs)(struct virtio_device *);
79 u64 (*get_features)(struct virtio_device *vdev);
80 int (*finalize_features)(struct virtio_device *vdev);
81 const char *(*bus_name)(struct virtio_device *vdev);
82 int (*set_vq_affinity)(struct virtqueue *vq, int cpu);
83 const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev,
84 int index);
85};
86
87
88void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
89 unsigned int fbit);
90
91
92
93
94
95
96
97
98static inline bool __virtio_test_bit(const struct virtio_device *vdev,
99 unsigned int fbit)
100{
101
102 if (__builtin_constant_p(fbit))
103 BUILD_BUG_ON(fbit >= 64);
104 else
105 BUG_ON(fbit >= 64);
106
107 return vdev->features & BIT_ULL(fbit);
108}
109
110
111
112
113
114
115static inline void __virtio_set_bit(struct virtio_device *vdev,
116 unsigned int fbit)
117{
118
119 if (__builtin_constant_p(fbit))
120 BUILD_BUG_ON(fbit >= 64);
121 else
122 BUG_ON(fbit >= 64);
123
124 vdev->features |= BIT_ULL(fbit);
125}
126
127
128
129
130
131
132static inline void __virtio_clear_bit(struct virtio_device *vdev,
133 unsigned int fbit)
134{
135
136 if (__builtin_constant_p(fbit))
137 BUILD_BUG_ON(fbit >= 64);
138 else
139 BUG_ON(fbit >= 64);
140
141 vdev->features &= ~BIT_ULL(fbit);
142}
143
144
145
146
147
148
149static inline bool virtio_has_feature(const struct virtio_device *vdev,
150 unsigned int fbit)
151{
152 if (fbit < VIRTIO_TRANSPORT_F_START)
153 virtio_check_driver_offered_feature(vdev, fbit);
154
155 return __virtio_test_bit(vdev, fbit);
156}
157
158
159
160
161
162static inline bool virtio_has_iommu_quirk(const struct virtio_device *vdev)
163{
164
165
166
167
168 return !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
169}
170
171static inline
172struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
173 vq_callback_t *c, const char *n)
174{
175 vq_callback_t *callbacks[] = { c };
176 const char *names[] = { n };
177 struct virtqueue *vq;
178 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL,
179 NULL);
180 if (err < 0)
181 return ERR_PTR(err);
182 return vq;
183}
184
185static inline
186int virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
187 struct virtqueue *vqs[], vq_callback_t *callbacks[],
188 const char * const names[],
189 struct irq_affinity *desc)
190{
191 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, NULL, desc);
192}
193
194static inline
195int virtio_find_vqs_ctx(struct virtio_device *vdev, unsigned nvqs,
196 struct virtqueue *vqs[], vq_callback_t *callbacks[],
197 const char * const names[], const bool *ctx,
198 struct irq_affinity *desc)
199{
200 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, ctx,
201 desc);
202}
203
204
205
206
207
208
209
210
211
212static inline
213void virtio_device_ready(struct virtio_device *dev)
214{
215 unsigned status = dev->config->get_status(dev);
216
217 BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
218 dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
219}
220
221static inline
222const char *virtio_bus_name(struct virtio_device *vdev)
223{
224 if (!vdev->config->bus_name)
225 return "virtio";
226 return vdev->config->bus_name(vdev);
227}
228
229
230
231
232
233
234
235
236
237
238static inline
239int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
240{
241 struct virtio_device *vdev = vq->vdev;
242 if (vdev->config->set_vq_affinity)
243 return vdev->config->set_vq_affinity(vq, cpu);
244 return 0;
245}
246
247static inline bool virtio_is_little_endian(struct virtio_device *vdev)
248{
249 return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
250 virtio_legacy_is_little_endian();
251}
252
253
254static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
255{
256 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
257}
258
259static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
260{
261 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
262}
263
264static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
265{
266 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
267}
268
269static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
270{
271 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
272}
273
274static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
275{
276 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
277}
278
279static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
280{
281 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
282}
283
284
285#define virtio_cread(vdev, structname, member, ptr) \
286 do { \
287 \
288 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
289 (*ptr) = 1; \
290 \
291 switch (sizeof(*ptr)) { \
292 case 1: \
293 *(ptr) = virtio_cread8(vdev, \
294 offsetof(structname, member)); \
295 break; \
296 case 2: \
297 *(ptr) = virtio_cread16(vdev, \
298 offsetof(structname, member)); \
299 break; \
300 case 4: \
301 *(ptr) = virtio_cread32(vdev, \
302 offsetof(structname, member)); \
303 break; \
304 case 8: \
305 *(ptr) = virtio_cread64(vdev, \
306 offsetof(structname, member)); \
307 break; \
308 default: \
309 BUG(); \
310 } \
311 } while(0)
312
313
314#define virtio_cwrite(vdev, structname, member, ptr) \
315 do { \
316 \
317 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
318 BUG_ON((*ptr) == 1); \
319 \
320 switch (sizeof(*ptr)) { \
321 case 1: \
322 virtio_cwrite8(vdev, \
323 offsetof(structname, member), \
324 *(ptr)); \
325 break; \
326 case 2: \
327 virtio_cwrite16(vdev, \
328 offsetof(structname, member), \
329 *(ptr)); \
330 break; \
331 case 4: \
332 virtio_cwrite32(vdev, \
333 offsetof(structname, member), \
334 *(ptr)); \
335 break; \
336 case 8: \
337 virtio_cwrite64(vdev, \
338 offsetof(structname, member), \
339 *(ptr)); \
340 break; \
341 default: \
342 BUG(); \
343 } \
344 } while(0)
345
346
347static inline void __virtio_cread_many(struct virtio_device *vdev,
348 unsigned int offset,
349 void *buf, size_t count, size_t bytes)
350{
351 u32 old, gen = vdev->config->generation ?
352 vdev->config->generation(vdev) : 0;
353 int i;
354
355 do {
356 old = gen;
357
358 for (i = 0; i < count; i++)
359 vdev->config->get(vdev, offset + bytes * i,
360 buf + i * bytes, bytes);
361
362 gen = vdev->config->generation ?
363 vdev->config->generation(vdev) : 0;
364 } while (gen != old);
365}
366
367static inline void virtio_cread_bytes(struct virtio_device *vdev,
368 unsigned int offset,
369 void *buf, size_t len)
370{
371 __virtio_cread_many(vdev, offset, buf, len, 1);
372}
373
374static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
375{
376 u8 ret;
377 vdev->config->get(vdev, offset, &ret, sizeof(ret));
378 return ret;
379}
380
381static inline void virtio_cwrite8(struct virtio_device *vdev,
382 unsigned int offset, u8 val)
383{
384 vdev->config->set(vdev, offset, &val, sizeof(val));
385}
386
387static inline u16 virtio_cread16(struct virtio_device *vdev,
388 unsigned int offset)
389{
390 u16 ret;
391 vdev->config->get(vdev, offset, &ret, sizeof(ret));
392 return virtio16_to_cpu(vdev, (__force __virtio16)ret);
393}
394
395static inline void virtio_cwrite16(struct virtio_device *vdev,
396 unsigned int offset, u16 val)
397{
398 val = (__force u16)cpu_to_virtio16(vdev, val);
399 vdev->config->set(vdev, offset, &val, sizeof(val));
400}
401
402static inline u32 virtio_cread32(struct virtio_device *vdev,
403 unsigned int offset)
404{
405 u32 ret;
406 vdev->config->get(vdev, offset, &ret, sizeof(ret));
407 return virtio32_to_cpu(vdev, (__force __virtio32)ret);
408}
409
410static inline void virtio_cwrite32(struct virtio_device *vdev,
411 unsigned int offset, u32 val)
412{
413 val = (__force u32)cpu_to_virtio32(vdev, val);
414 vdev->config->set(vdev, offset, &val, sizeof(val));
415}
416
417static inline u64 virtio_cread64(struct virtio_device *vdev,
418 unsigned int offset)
419{
420 u64 ret;
421 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
422 return virtio64_to_cpu(vdev, (__force __virtio64)ret);
423}
424
425static inline void virtio_cwrite64(struct virtio_device *vdev,
426 unsigned int offset, u64 val)
427{
428 val = (__force u64)cpu_to_virtio64(vdev, val);
429 vdev->config->set(vdev, offset, &val, sizeof(val));
430}
431
432
433#define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
434 ({ \
435 int _r = 0; \
436 if (!virtio_has_feature(vdev, fbit)) \
437 _r = -ENOENT; \
438 else \
439 virtio_cread((vdev), structname, member, ptr); \
440 _r; \
441 })
442
443#endif
444