]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/virtio_config.h
seccomp: Move speculation migitation control to arch code
[mirror_ubuntu-artful-kernel.git] / include / linux / virtio_config.h
CommitLineData
ec3d41c4
RR
1#ifndef _LINUX_VIRTIO_CONFIG_H
2#define _LINUX_VIRTIO_CONFIG_H
b4f68be6 3
d2a7ddda 4#include <linux/err.h>
187f1882 5#include <linux/bug.h>
72e61eb4 6#include <linux/virtio.h>
eef960a0 7#include <linux/virtio_byteorder.h>
607ca46e 8#include <uapi/linux/virtio_config.h>
ec3d41c4 9
fb5e31d9
CH
10struct irq_affinity;
11
ec3d41c4
RR
12/**
13 * virtio_config_ops - operations for configuring a virtio device
a586d4f6 14 * @get: read the value of a configuration field
ec3d41c4 15 * vdev: the virtio_device
a586d4f6 16 * offset: the offset of the configuration field
ec3d41c4 17 * buf: the buffer to write the field value into.
a586d4f6 18 * len: the length of the buffer
a586d4f6 19 * @set: write the value of a configuration field
ec3d41c4 20 * vdev: the virtio_device
a586d4f6 21 * offset: the offset of the configuration field
ec3d41c4 22 * buf: the buffer to read the field value from.
a586d4f6 23 * len: the length of the buffer
d71de9ec
MT
24 * @generation: config generation counter
25 * vdev: the virtio_device
26 * Returns the config generation counter
ec3d41c4
RR
27 * @get_status: read the status byte
28 * vdev: the virtio_device
29 * Returns the status byte
30 * @set_status: write the status byte
31 * vdev: the virtio_device
32 * status: the new status byte
6e5aa7ef
RR
33 * @reset: reset the device
34 * vdev: the virtio device
35 * After this, status and feature negotiation must be done again
e6af578c
MT
36 * Device must not be reset from its vq/config callbacks, or in
37 * parallel with being added/removed.
d2a7ddda 38 * @find_vqs: find virtqueues and instantiate them.
ec3d41c4 39 * vdev: the virtio_device
d2a7ddda
MT
40 * nvqs: the number of virtqueues to find
41 * vqs: on success, includes new virtqueues
42 * callbacks: array of callbacks, for each virtqueue
6457f126 43 * include a NULL entry for vqs that do not need a callback
d2a7ddda 44 * names: array of virtqueue names (mainly for debugging)
6457f126 45 * include a NULL entry for vqs unused by driver
d2a7ddda
MT
46 * Returns 0 on success or error status
47 * @del_vqs: free virtqueues found by find_vqs().
c45a6816
RR
48 * @get_features: get the array of feature bits for this device.
49 * vdev: the virtio_device
50 * Returns the first 32 feature bits (all we currently need).
c624896e 51 * @finalize_features: confirm what device features we'll be using.
c45a6816 52 * vdev: the virtio_device
c624896e
RR
53 * This gives the final feature bits for the device: it can change
54 * the dev->feature bits if it wants.
5c609a5e 55 * Returns 0 on success or error status
66846048
RJ
56 * @bus_name: return the bus name associated with the device
57 * vdev: the virtio_device
58 * This returns a pointer to the bus name a la pci_name from which
59 * the caller can then copy.
75a0a52b 60 * @set_vq_affinity: set the affinity for a virtqueue.
bbaba479 61 * @get_vq_affinity: get the affinity for a virtqueue (optional).
ec3d41c4 62 */
d2a7ddda 63typedef void vq_callback_t(struct virtqueue *);
1842f23c 64struct virtio_config_ops {
a586d4f6 65 void (*get)(struct virtio_device *vdev, unsigned offset,
ec3d41c4 66 void *buf, unsigned len);
a586d4f6 67 void (*set)(struct virtio_device *vdev, unsigned offset,
ec3d41c4 68 const void *buf, unsigned len);
d71de9ec 69 u32 (*generation)(struct virtio_device *vdev);
ec3d41c4
RR
70 u8 (*get_status)(struct virtio_device *vdev);
71 void (*set_status)(struct virtio_device *vdev, u8 status);
6e5aa7ef 72 void (*reset)(struct virtio_device *vdev);
d2a7ddda 73 int (*find_vqs)(struct virtio_device *, unsigned nvqs,
fb5e31d9 74 struct virtqueue *vqs[], vq_callback_t *callbacks[],
f94682dd
MT
75 const char * const names[], const bool *ctx,
76 struct irq_affinity *desc);
d2a7ddda 77 void (*del_vqs)(struct virtio_device *);
d0254773 78 u64 (*get_features)(struct virtio_device *vdev);
5c609a5e 79 int (*finalize_features)(struct virtio_device *vdev);
66846048 80 const char *(*bus_name)(struct virtio_device *vdev);
75a0a52b 81 int (*set_vq_affinity)(struct virtqueue *vq, int cpu);
bbaba479
CH
82 const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev,
83 int index);
ec3d41c4
RR
84};
85
c45a6816
RR
86/* If driver didn't advertise the feature, it will never appear. */
87void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
88 unsigned int fbit);
89
90/**
d4024af5
MT
91 * __virtio_test_bit - helper to test feature bits. For use by transports.
92 * Devices should normally use virtio_has_feature,
93 * which includes more checks.
c45a6816
RR
94 * @vdev: the device
95 * @fbit: the feature bit
96 */
d4024af5
MT
97static inline bool __virtio_test_bit(const struct virtio_device *vdev,
98 unsigned int fbit)
99{
100 /* Did you forget to fix assumptions on max features? */
101 if (__builtin_constant_p(fbit))
d0254773 102 BUILD_BUG_ON(fbit >= 64);
d4024af5 103 else
d0254773 104 BUG_ON(fbit >= 64);
d4024af5 105
d0254773 106 return vdev->features & BIT_ULL(fbit);
d4024af5
MT
107}
108
109/**
110 * __virtio_set_bit - helper to set feature bits. For use by transports.
111 * @vdev: the device
112 * @fbit: the feature bit
113 */
114static inline void __virtio_set_bit(struct virtio_device *vdev,
115 unsigned int fbit)
116{
117 /* Did you forget to fix assumptions on max features? */
118 if (__builtin_constant_p(fbit))
d0254773 119 BUILD_BUG_ON(fbit >= 64);
d4024af5 120 else
d0254773 121 BUG_ON(fbit >= 64);
d4024af5 122
d0254773 123 vdev->features |= BIT_ULL(fbit);
d4024af5
MT
124}
125
126/**
127 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
128 * @vdev: the device
129 * @fbit: the feature bit
130 */
131static inline void __virtio_clear_bit(struct virtio_device *vdev,
c45a6816
RR
132 unsigned int fbit)
133{
134 /* Did you forget to fix assumptions on max features? */
1765e3a4 135 if (__builtin_constant_p(fbit))
d0254773 136 BUILD_BUG_ON(fbit >= 64);
1765e3a4 137 else
d0254773 138 BUG_ON(fbit >= 64);
c45a6816 139
d0254773 140 vdev->features &= ~BIT_ULL(fbit);
d4024af5
MT
141}
142
143/**
144 * virtio_has_feature - helper to determine if this device has this feature.
145 * @vdev: the device
146 * @fbit: the feature bit
147 */
148static inline bool virtio_has_feature(const struct virtio_device *vdev,
149 unsigned int fbit)
150{
ee006b35
MM
151 if (fbit < VIRTIO_TRANSPORT_F_START)
152 virtio_check_driver_offered_feature(vdev, fbit);
153
d4024af5 154 return __virtio_test_bit(vdev, fbit);
c45a6816
RR
155}
156
1a937693
MT
157/**
158 * virtio_has_iommu_quirk - determine whether this device has the iommu quirk
159 * @vdev: the device
160 */
161static inline bool virtio_has_iommu_quirk(const struct virtio_device *vdev)
162{
163 /*
164 * Note the reverse polarity of the quirk feature (compared to most
165 * other features), this is for compatibility with legacy systems.
166 */
167 return !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
168}
169
d2a7ddda
MT
170static inline
171struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
172 vq_callback_t *c, const char *n)
173{
174 vq_callback_t *callbacks[] = { c };
175 const char *names[] = { n };
176 struct virtqueue *vq;
f94682dd
MT
177 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL,
178 NULL);
d2a7ddda
MT
179 if (err < 0)
180 return ERR_PTR(err);
181 return vq;
182}
66846048 183
9b2bbdb2
MT
184static inline
185int virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
186 struct virtqueue *vqs[], vq_callback_t *callbacks[],
187 const char * const names[],
188 struct irq_affinity *desc)
189{
f94682dd
MT
190 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, NULL, desc);
191}
192
193static inline
194int virtio_find_vqs_ctx(struct virtio_device *vdev, unsigned nvqs,
195 struct virtqueue *vqs[], vq_callback_t *callbacks[],
196 const char * const names[], const bool *ctx,
197 struct irq_affinity *desc)
198{
199 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, ctx,
200 desc);
9b2bbdb2
MT
201}
202
3569db59
MT
203/**
204 * virtio_device_ready - enable vq use in probe function
205 * @vdev: the device
206 *
207 * Driver must call this to use vqs in the probe function.
208 *
209 * Note: vqs are enabled automatically after probe returns.
210 */
211static inline
212void virtio_device_ready(struct virtio_device *dev)
213{
214 unsigned status = dev->config->get_status(dev);
215
216 BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
217 dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
218}
219
66846048
RJ
220static inline
221const char *virtio_bus_name(struct virtio_device *vdev)
222{
223 if (!vdev->config->bus_name)
224 return "virtio";
225 return vdev->config->bus_name(vdev);
226}
227
75a0a52b
JW
228/**
229 * virtqueue_set_affinity - setting affinity for a virtqueue
230 * @vq: the virtqueue
231 * @cpu: the cpu no.
232 *
233 * Pay attention the function are best-effort: the affinity hint may not be set
234 * due to config support, irq type and sharing.
235 *
236 */
237static inline
238int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
239{
240 struct virtio_device *vdev = vq->vdev;
241 if (vdev->config->set_vq_affinity)
242 return vdev->config->set_vq_affinity(vq, cpu);
243 return 0;
244}
245
cf561f0d
GK
246static inline bool virtio_is_little_endian(struct virtio_device *vdev)
247{
7d824109
GK
248 return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
249 virtio_legacy_is_little_endian();
cf561f0d
GK
250}
251
eef960a0
MT
252/* Memory accessors */
253static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
254{
cf561f0d 255 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
eef960a0
MT
256}
257
258static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
259{
cf561f0d 260 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
eef960a0
MT
261}
262
263static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
264{
cf561f0d 265 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
eef960a0
MT
266}
267
268static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
269{
cf561f0d 270 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
eef960a0
MT
271}
272
273static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
274{
cf561f0d 275 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
eef960a0
MT
276}
277
278static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
279{
cf561f0d 280 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
eef960a0
MT
281}
282
0b90d062
RR
283/* Config space accessors. */
284#define virtio_cread(vdev, structname, member, ptr) \
285 do { \
286 /* Must match the member's type, and be integer */ \
287 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
288 (*ptr) = 1; \
289 \
290 switch (sizeof(*ptr)) { \
291 case 1: \
292 *(ptr) = virtio_cread8(vdev, \
293 offsetof(structname, member)); \
294 break; \
295 case 2: \
296 *(ptr) = virtio_cread16(vdev, \
297 offsetof(structname, member)); \
298 break; \
299 case 4: \
300 *(ptr) = virtio_cread32(vdev, \
301 offsetof(structname, member)); \
302 break; \
303 case 8: \
304 *(ptr) = virtio_cread64(vdev, \
305 offsetof(structname, member)); \
306 break; \
307 default: \
308 BUG(); \
309 } \
310 } while(0)
311
312/* Config space accessors. */
313#define virtio_cwrite(vdev, structname, member, ptr) \
314 do { \
315 /* Must match the member's type, and be integer */ \
316 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
317 BUG_ON((*ptr) == 1); \
318 \
319 switch (sizeof(*ptr)) { \
320 case 1: \
321 virtio_cwrite8(vdev, \
322 offsetof(structname, member), \
323 *(ptr)); \
324 break; \
325 case 2: \
326 virtio_cwrite16(vdev, \
327 offsetof(structname, member), \
328 *(ptr)); \
329 break; \
330 case 4: \
331 virtio_cwrite32(vdev, \
332 offsetof(structname, member), \
333 *(ptr)); \
334 break; \
335 case 8: \
336 virtio_cwrite64(vdev, \
337 offsetof(structname, member), \
338 *(ptr)); \
339 break; \
340 default: \
341 BUG(); \
342 } \
343 } while(0)
344
d71de9ec
MT
345/* Read @count fields, @bytes each. */
346static inline void __virtio_cread_many(struct virtio_device *vdev,
347 unsigned int offset,
348 void *buf, size_t count, size_t bytes)
349{
350 u32 old, gen = vdev->config->generation ?
351 vdev->config->generation(vdev) : 0;
352 int i;
353
354 do {
355 old = gen;
356
357 for (i = 0; i < count; i++)
358 vdev->config->get(vdev, offset + bytes * i,
359 buf + i * bytes, bytes);
360
361 gen = vdev->config->generation ?
362 vdev->config->generation(vdev) : 0;
363 } while (gen != old);
364}
365
0b90d062
RR
366static inline void virtio_cread_bytes(struct virtio_device *vdev,
367 unsigned int offset,
368 void *buf, size_t len)
369{
d71de9ec 370 __virtio_cread_many(vdev, offset, buf, len, 1);
0b90d062
RR
371}
372
caa0e2d0
MT
373static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
374{
375 u8 ret;
376 vdev->config->get(vdev, offset, &ret, sizeof(ret));
377 return ret;
378}
379
0b90d062
RR
380static inline void virtio_cwrite8(struct virtio_device *vdev,
381 unsigned int offset, u8 val)
382{
383 vdev->config->set(vdev, offset, &val, sizeof(val));
384}
385
386static inline u16 virtio_cread16(struct virtio_device *vdev,
387 unsigned int offset)
388{
389 u16 ret;
390 vdev->config->get(vdev, offset, &ret, sizeof(ret));
92e6d743 391 return virtio16_to_cpu(vdev, (__force __virtio16)ret);
0b90d062
RR
392}
393
394static inline void virtio_cwrite16(struct virtio_device *vdev,
395 unsigned int offset, u16 val)
396{
92e6d743 397 val = (__force u16)cpu_to_virtio16(vdev, val);
0b90d062
RR
398 vdev->config->set(vdev, offset, &val, sizeof(val));
399}
400
401static inline u32 virtio_cread32(struct virtio_device *vdev,
402 unsigned int offset)
403{
404 u32 ret;
405 vdev->config->get(vdev, offset, &ret, sizeof(ret));
92e6d743 406 return virtio32_to_cpu(vdev, (__force __virtio32)ret);
0b90d062
RR
407}
408
409static inline void virtio_cwrite32(struct virtio_device *vdev,
410 unsigned int offset, u32 val)
411{
92e6d743 412 val = (__force u32)cpu_to_virtio32(vdev, val);
0b90d062
RR
413 vdev->config->set(vdev, offset, &val, sizeof(val));
414}
415
416static inline u64 virtio_cread64(struct virtio_device *vdev,
417 unsigned int offset)
418{
419 u64 ret;
d71de9ec 420 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
92e6d743 421 return virtio64_to_cpu(vdev, (__force __virtio64)ret);
0b90d062
RR
422}
423
424static inline void virtio_cwrite64(struct virtio_device *vdev,
425 unsigned int offset, u64 val)
426{
92e6d743 427 val = (__force u64)cpu_to_virtio64(vdev, val);
0b90d062
RR
428 vdev->config->set(vdev, offset, &val, sizeof(val));
429}
430
431/* Conditional config space accessors. */
432#define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
433 ({ \
434 int _r = 0; \
435 if (!virtio_has_feature(vdev, fbit)) \
436 _r = -ENOENT; \
437 else \
438 virtio_cread((vdev), structname, member, ptr); \
439 _r; \
440 })
75a0a52b 441
ec3d41c4 442#endif /* _LINUX_VIRTIO_CONFIG_H */