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