]>
Commit | Line | Data |
---|---|---|
8fb49b4c BM |
1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
2 | /* | |
3 | * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi> | |
4 | * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com> | |
5 | * | |
6 | * VirtIO is a virtualization standard for network and disk device drivers | |
7 | * where just the guest's device driver "knows" it is running in a virtual | |
8 | * environment, and cooperates with the hypervisor. This enables guests to | |
9 | * get high performance network and disk operations, and gives most of the | |
10 | * performance benefits of paravirtualization. In the U-Boot case, the guest | |
11 | * is U-Boot itself, while the virtual environment are normally QEMU targets | |
12 | * like ARM, RISC-V and x86. | |
13 | * | |
14 | * See http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf for | |
15 | * the VirtIO specification v1.0. | |
16 | * | |
17 | * This file is largely based on Linux kernel virtio_*.h files | |
18 | */ | |
19 | ||
20 | #ifndef __VIRTIO_H__ | |
21 | #define __VIRTIO_H__ | |
22 | ||
d8bf49fa | 23 | #include <virtio_types.h> |
3be9f399 | 24 | #include <dm/device.h> |
cd93d625 | 25 | #include <linux/bitops.h> |
eb41d8a1 | 26 | #include <linux/bug.h> |
8fb49b4c BM |
27 | #define VIRTIO_ID_NET 1 /* virtio net */ |
28 | #define VIRTIO_ID_BLOCK 2 /* virtio block */ | |
03018ea8 SG |
29 | #define VIRTIO_ID_RNG 4 /* virtio rng */ |
30 | #define VIRTIO_ID_MAX_NUM 5 | |
8fb49b4c BM |
31 | |
32 | #define VIRTIO_NET_DRV_NAME "virtio-net" | |
33 | #define VIRTIO_BLK_DRV_NAME "virtio-blk" | |
03018ea8 | 34 | #define VIRTIO_RNG_DRV_NAME "virtio-rng" |
8fb49b4c BM |
35 | |
36 | /* Status byte for guest to report progress, and synchronize features */ | |
37 | ||
38 | /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */ | |
39 | #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1 | |
40 | /* We have found a driver for the device */ | |
41 | #define VIRTIO_CONFIG_S_DRIVER 2 | |
42 | /* Driver has used its parts of the config, and is happy */ | |
43 | #define VIRTIO_CONFIG_S_DRIVER_OK 4 | |
44 | /* Driver has finished configuring features */ | |
45 | #define VIRTIO_CONFIG_S_FEATURES_OK 8 | |
46 | /* Device entered invalid state, driver must reset it */ | |
47 | #define VIRTIO_CONFIG_S_NEEDS_RESET 0x40 | |
48 | /* We've given up on this device */ | |
49 | #define VIRTIO_CONFIG_S_FAILED 0x80 | |
50 | ||
51 | /* | |
52 | * Virtio feature bits VIRTIO_TRANSPORT_F_START through VIRTIO_TRANSPORT_F_END | |
53 | * are reserved for the transport being used (eg: virtio_ring, virtio_pci etc.), | |
54 | * the rest are per-device feature bits. | |
55 | */ | |
56 | #define VIRTIO_TRANSPORT_F_START 28 | |
57 | #define VIRTIO_TRANSPORT_F_END 38 | |
58 | ||
59 | #ifndef VIRTIO_CONFIG_NO_LEGACY | |
60 | /* | |
61 | * Do we get callbacks when the ring is completely used, | |
62 | * even if we've suppressed them? | |
63 | */ | |
64 | #define VIRTIO_F_NOTIFY_ON_EMPTY 24 | |
65 | ||
66 | /* Can the device handle any descriptor layout? */ | |
67 | #define VIRTIO_F_ANY_LAYOUT 27 | |
68 | #endif /* VIRTIO_CONFIG_NO_LEGACY */ | |
69 | ||
70 | /* v1.0 compliant */ | |
71 | #define VIRTIO_F_VERSION_1 32 | |
72 | ||
73 | /* | |
74 | * If clear - device has the IOMMU bypass quirk feature. | |
75 | * If set - use platform tools to detect the IOMMU. | |
76 | * | |
77 | * Note the reverse polarity (compared to most other features), | |
78 | * this is for compatibility with legacy systems. | |
79 | */ | |
80 | #define VIRTIO_F_IOMMU_PLATFORM 33 | |
81 | ||
82 | /* Does the device support Single Root I/O Virtualization? */ | |
83 | #define VIRTIO_F_SR_IOV 37 | |
84 | ||
85 | /** | |
86 | * virtio scatter-gather struct | |
87 | * | |
88 | * @addr: sg buffer address | |
89 | * @lengh: sg buffer length | |
90 | */ | |
91 | struct virtio_sg { | |
92 | void *addr; | |
93 | size_t length; | |
94 | }; | |
95 | ||
96 | struct virtqueue; | |
97 | ||
98 | /* virtio bus operations */ | |
99 | struct dm_virtio_ops { | |
100 | /** | |
101 | * get_config() - read the value of a configuration field | |
102 | * | |
103 | * @vdev: the real virtio device | |
104 | * @offset: the offset of the configuration field | |
105 | * @buf: the buffer to write the field value into | |
106 | * @len: the length of the buffer | |
107 | * @return 0 if OK, -ve on error | |
108 | */ | |
109 | int (*get_config)(struct udevice *vdev, unsigned int offset, | |
110 | void *buf, unsigned int len); | |
111 | /** | |
112 | * set_config() - write the value of a configuration field | |
113 | * | |
114 | * @vdev: the real virtio device | |
115 | * @offset: the offset of the configuration field | |
116 | * @buf: the buffer to read the field value from | |
117 | * @len: the length of the buffer | |
118 | * @return 0 if OK, -ve on error | |
119 | */ | |
120 | int (*set_config)(struct udevice *vdev, unsigned int offset, | |
121 | const void *buf, unsigned int len); | |
122 | /** | |
123 | * generation() - config generation counter | |
124 | * | |
125 | * @vdev: the real virtio device | |
126 | * @counter: the returned config generation counter | |
127 | * @return 0 if OK, -ve on error | |
128 | */ | |
129 | int (*generation)(struct udevice *vdev, u32 *counter); | |
130 | /** | |
131 | * get_status() - read the status byte | |
132 | * | |
133 | * @vdev: the real virtio device | |
134 | * @status: the returned status byte | |
135 | * @return 0 if OK, -ve on error | |
136 | */ | |
137 | int (*get_status)(struct udevice *vdev, u8 *status); | |
138 | /** | |
139 | * set_status() - write the status byte | |
140 | * | |
141 | * @vdev: the real virtio device | |
142 | * @status: the new status byte | |
143 | * @return 0 if OK, -ve on error | |
144 | */ | |
145 | int (*set_status)(struct udevice *vdev, u8 status); | |
146 | /** | |
147 | * reset() - reset the device | |
148 | * | |
149 | * @vdev: the real virtio device | |
150 | * @return 0 if OK, -ve on error | |
151 | */ | |
152 | int (*reset)(struct udevice *vdev); | |
153 | /** | |
154 | * get_features() - get the array of feature bits for this device | |
155 | * | |
156 | * @vdev: the real virtio device | |
157 | * @features: the first 32 feature bits (all we currently need) | |
158 | * @return 0 if OK, -ve on error | |
159 | */ | |
160 | int (*get_features)(struct udevice *vdev, u64 *features); | |
161 | /** | |
162 | * set_features() - confirm what device features we'll be using | |
163 | * | |
164 | * @vdev: the real virtio device | |
165 | * @return 0 if OK, -ve on error | |
166 | */ | |
167 | int (*set_features)(struct udevice *vdev); | |
168 | /** | |
169 | * find_vqs() - find virtqueues and instantiate them | |
170 | * | |
171 | * @vdev: the real virtio device | |
172 | * @nvqs: the number of virtqueues to find | |
173 | * @vqs: on success, includes new virtqueues | |
174 | * @return 0 if OK, -ve on error | |
175 | */ | |
176 | int (*find_vqs)(struct udevice *vdev, unsigned int nvqs, | |
177 | struct virtqueue *vqs[]); | |
178 | /** | |
179 | * del_vqs() - free virtqueues found by find_vqs() | |
180 | * | |
181 | * @vdev: the real virtio device | |
182 | * @return 0 if OK, -ve on error | |
183 | */ | |
184 | int (*del_vqs)(struct udevice *vdev); | |
185 | /** | |
186 | * notify() - notify the device to process the queue | |
187 | * | |
188 | * @vdev: the real virtio device | |
189 | * @vq: virtqueue to process | |
190 | * @return 0 if OK, -ve on error | |
191 | */ | |
192 | int (*notify)(struct udevice *vdev, struct virtqueue *vq); | |
193 | }; | |
194 | ||
195 | /* Get access to a virtio bus' operations */ | |
196 | #define virtio_get_ops(dev) ((struct dm_virtio_ops *)(dev)->driver->ops) | |
197 | ||
198 | /** | |
199 | * virtio uclass per device private data | |
200 | * | |
201 | * @vqs: virtualqueue for the virtio device | |
202 | * @vdev: the real virtio device underneath | |
203 | * @legacy: is it a legacy device? | |
204 | * @device: virtio device ID | |
205 | * @vendor: virtio vendor ID | |
206 | * @features: negotiated supported features | |
207 | * @feature_table: an array of feature supported by the driver | |
208 | * @feature_table_size: number of entries in the feature table array | |
209 | * @feature_table_legacy: same as feature_table but working in legacy mode | |
210 | * @feature_table_size_legacy: number of entries in feature table legacy array | |
211 | */ | |
212 | struct virtio_dev_priv { | |
213 | struct list_head vqs; | |
214 | struct udevice *vdev; | |
215 | bool legacy; | |
216 | u32 device; | |
217 | u32 vendor; | |
218 | u64 features; | |
219 | const u32 *feature_table; | |
220 | u32 feature_table_size; | |
221 | const u32 *feature_table_legacy; | |
222 | u32 feature_table_size_legacy; | |
223 | }; | |
224 | ||
225 | /** | |
226 | * virtio_get_config() - read the value of a configuration field | |
227 | * | |
228 | * @vdev: the real virtio device | |
229 | * @offset: the offset of the configuration field | |
230 | * @buf: the buffer to write the field value into | |
231 | * @len: the length of the buffer | |
185f812c | 232 | * Return: 0 if OK, -ve on error |
8fb49b4c BM |
233 | */ |
234 | int virtio_get_config(struct udevice *vdev, unsigned int offset, | |
235 | void *buf, unsigned int len); | |
236 | ||
237 | /** | |
238 | * virtio_set_config() - write the value of a configuration field | |
239 | * | |
240 | * @vdev: the real virtio device | |
241 | * @offset: the offset of the configuration field | |
242 | * @buf: the buffer to read the field value from | |
243 | * @len: the length of the buffer | |
185f812c | 244 | * Return: 0 if OK, -ve on error |
8fb49b4c BM |
245 | */ |
246 | int virtio_set_config(struct udevice *vdev, unsigned int offset, | |
247 | void *buf, unsigned int len); | |
248 | ||
249 | /** | |
250 | * virtio_generation() - config generation counter | |
251 | * | |
252 | * @vdev: the real virtio device | |
253 | * @counter: the returned config generation counter | |
185f812c | 254 | * Return: 0 if OK, -ve on error |
8fb49b4c BM |
255 | */ |
256 | int virtio_generation(struct udevice *vdev, u32 *counter); | |
257 | ||
258 | /** | |
259 | * virtio_get_status() - read the status byte | |
260 | * | |
261 | * @vdev: the real virtio device | |
262 | * @status: the returned status byte | |
185f812c | 263 | * Return: 0 if OK, -ve on error |
8fb49b4c BM |
264 | */ |
265 | int virtio_get_status(struct udevice *vdev, u8 *status); | |
266 | ||
267 | /** | |
268 | * virtio_set_status() - write the status byte | |
269 | * | |
270 | * @vdev: the real virtio device | |
271 | * @status: the new status byte | |
185f812c | 272 | * Return: 0 if OK, -ve on error |
8fb49b4c BM |
273 | */ |
274 | int virtio_set_status(struct udevice *vdev, u8 status); | |
275 | ||
276 | /** | |
277 | * virtio_reset() - reset the device | |
278 | * | |
279 | * @vdev: the real virtio device | |
185f812c | 280 | * Return: 0 if OK, -ve on error |
8fb49b4c BM |
281 | */ |
282 | int virtio_reset(struct udevice *vdev); | |
283 | ||
284 | /** | |
285 | * virtio_get_features() - get the array of feature bits for this device | |
286 | * | |
287 | * @vdev: the real virtio device | |
288 | * @features: the first 32 feature bits (all we currently need) | |
185f812c | 289 | * Return: 0 if OK, -ve on error |
8fb49b4c BM |
290 | */ |
291 | int virtio_get_features(struct udevice *vdev, u64 *features); | |
292 | ||
293 | /** | |
294 | * virtio_set_features() - confirm what device features we'll be using | |
295 | * | |
296 | * @vdev: the real virtio device | |
185f812c | 297 | * Return: 0 if OK, -ve on error |
8fb49b4c BM |
298 | */ |
299 | int virtio_set_features(struct udevice *vdev); | |
300 | ||
301 | /** | |
302 | * virtio_find_vqs() - find virtqueues and instantiate them | |
303 | * | |
304 | * @vdev: the real virtio device | |
305 | * @nvqs: the number of virtqueues to find | |
306 | * @vqs: on success, includes new virtqueues | |
185f812c | 307 | * Return: 0 if OK, -ve on error |
8fb49b4c BM |
308 | */ |
309 | int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs, | |
310 | struct virtqueue *vqs[]); | |
311 | ||
312 | /** | |
313 | * virtio_del_vqs() - free virtqueues found by find_vqs() | |
314 | * | |
315 | * @vdev: the real virtio device | |
185f812c | 316 | * Return: 0 if OK, -ve on error |
8fb49b4c BM |
317 | */ |
318 | int virtio_del_vqs(struct udevice *vdev); | |
319 | ||
320 | /** | |
321 | * virtio_notify() - notify the device to process the queue | |
322 | * | |
323 | * @vdev: the real virtio device | |
324 | * @vq: virtqueue to process | |
185f812c | 325 | * Return: 0 if OK, -ve on error |
8fb49b4c BM |
326 | */ |
327 | int virtio_notify(struct udevice *vdev, struct virtqueue *vq); | |
328 | ||
329 | /** | |
330 | * virtio_add_status() - helper to set a new status code to the device | |
331 | * | |
332 | * @vdev: the real virtio device | |
333 | * @status: new status code to be added | |
334 | */ | |
335 | void virtio_add_status(struct udevice *vdev, u8 status); | |
336 | ||
337 | /** | |
338 | * virtio_finalize_features() - helper to finalize features | |
339 | * | |
340 | * @vdev: the real virtio device | |
185f812c | 341 | * Return: 0 if OK, -ve on error |
8fb49b4c BM |
342 | */ |
343 | int virtio_finalize_features(struct udevice *vdev); | |
344 | ||
345 | /** | |
346 | * virtio_driver_features_init() - initialize driver supported features | |
347 | * | |
348 | * This fills in the virtio device parent per child private data with the given | |
349 | * information, which contains driver supported features and legacy features. | |
350 | * | |
351 | * This API should be called in the virtio device driver's bind method, so that | |
352 | * later virtio transport uclass driver can utilize the driver supplied features | |
353 | * to negotiate with the device on the final supported features. | |
354 | * | |
355 | * @priv: virtio uclass per device private data | |
356 | * @feature: an array of feature supported by the driver | |
357 | * @feature_size: number of entries in the feature table array | |
358 | * @feature_legacy: same as feature_table but working in legacy mode | |
359 | * @feature_legacy_size:number of entries in feature table legacy array | |
360 | */ | |
361 | void virtio_driver_features_init(struct virtio_dev_priv *priv, | |
362 | const u32 *feature, | |
363 | u32 feature_size, | |
364 | const u32 *feature_legacy, | |
365 | u32 feature_legacy_size); | |
366 | ||
367 | /** | |
368 | * virtio_init() - helper to enumerate all known virtio devices | |
369 | * | |
185f812c | 370 | * Return: 0 if OK, -ve on error |
8fb49b4c BM |
371 | */ |
372 | int virtio_init(void); | |
373 | ||
374 | static inline u16 __virtio16_to_cpu(bool little_endian, __virtio16 val) | |
375 | { | |
376 | if (little_endian) | |
377 | return le16_to_cpu((__force __le16)val); | |
378 | else | |
379 | return be16_to_cpu((__force __be16)val); | |
380 | } | |
381 | ||
382 | static inline __virtio16 __cpu_to_virtio16(bool little_endian, u16 val) | |
383 | { | |
384 | if (little_endian) | |
385 | return (__force __virtio16)cpu_to_le16(val); | |
386 | else | |
387 | return (__force __virtio16)cpu_to_be16(val); | |
388 | } | |
389 | ||
390 | static inline u32 __virtio32_to_cpu(bool little_endian, __virtio32 val) | |
391 | { | |
392 | if (little_endian) | |
393 | return le32_to_cpu((__force __le32)val); | |
394 | else | |
395 | return be32_to_cpu((__force __be32)val); | |
396 | } | |
397 | ||
398 | static inline __virtio32 __cpu_to_virtio32(bool little_endian, u32 val) | |
399 | { | |
400 | if (little_endian) | |
401 | return (__force __virtio32)cpu_to_le32(val); | |
402 | else | |
403 | return (__force __virtio32)cpu_to_be32(val); | |
404 | } | |
405 | ||
406 | static inline u64 __virtio64_to_cpu(bool little_endian, __virtio64 val) | |
407 | { | |
408 | if (little_endian) | |
409 | return le64_to_cpu((__force __le64)val); | |
410 | else | |
411 | return be64_to_cpu((__force __be64)val); | |
412 | } | |
413 | ||
414 | static inline __virtio64 __cpu_to_virtio64(bool little_endian, u64 val) | |
415 | { | |
416 | if (little_endian) | |
417 | return (__force __virtio64)cpu_to_le64(val); | |
418 | else | |
419 | return (__force __virtio64)cpu_to_be64(val); | |
420 | } | |
421 | ||
422 | /** | |
423 | * __virtio_test_bit - helper to test feature bits | |
424 | * | |
425 | * For use by transports. Devices should normally use virtio_has_feature, | |
426 | * which includes more checks. | |
427 | * | |
428 | * @udev: the transport device | |
429 | * @fbit: the feature bit | |
430 | */ | |
431 | static inline bool __virtio_test_bit(struct udevice *udev, unsigned int fbit) | |
432 | { | |
433 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); | |
434 | ||
435 | /* Did you forget to fix assumptions on max features? */ | |
436 | if (__builtin_constant_p(fbit)) | |
437 | BUILD_BUG_ON(fbit >= 64); | |
438 | else | |
439 | WARN_ON(fbit >= 64); | |
440 | ||
441 | return uc_priv->features & BIT_ULL(fbit); | |
442 | } | |
443 | ||
444 | /** | |
445 | * __virtio_set_bit - helper to set feature bits | |
446 | * | |
447 | * For use by transports. | |
448 | * | |
449 | * @udev: the transport device | |
450 | * @fbit: the feature bit | |
451 | */ | |
452 | static inline void __virtio_set_bit(struct udevice *udev, unsigned int fbit) | |
453 | { | |
454 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); | |
455 | ||
456 | /* Did you forget to fix assumptions on max features? */ | |
457 | if (__builtin_constant_p(fbit)) | |
458 | BUILD_BUG_ON(fbit >= 64); | |
459 | else | |
460 | WARN_ON(fbit >= 64); | |
461 | ||
462 | uc_priv->features |= BIT_ULL(fbit); | |
463 | } | |
464 | ||
465 | /** | |
466 | * __virtio_clear_bit - helper to clear feature bits | |
467 | * | |
468 | * For use by transports. | |
469 | * | |
470 | * @vdev: the transport device | |
471 | * @fbit: the feature bit | |
472 | */ | |
473 | static inline void __virtio_clear_bit(struct udevice *udev, unsigned int fbit) | |
474 | { | |
475 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); | |
476 | ||
477 | /* Did you forget to fix assumptions on max features? */ | |
478 | if (__builtin_constant_p(fbit)) | |
479 | BUILD_BUG_ON(fbit >= 64); | |
480 | else | |
481 | WARN_ON(fbit >= 64); | |
482 | ||
483 | uc_priv->features &= ~BIT_ULL(fbit); | |
484 | } | |
485 | ||
486 | /** | |
487 | * virtio_has_feature - helper to determine if this device has this feature | |
488 | * | |
489 | * Note this API is only usable after the virtio device driver's bind phase, | |
490 | * as the feature has been negotiated between the device and the driver. | |
491 | * | |
492 | * @vdev: the virtio device | |
493 | * @fbit: the feature bit | |
494 | */ | |
495 | static inline bool virtio_has_feature(struct udevice *vdev, unsigned int fbit) | |
496 | { | |
73466df3 | 497 | if (!(dev_get_flags(vdev) & DM_FLAG_BOUND)) |
8fb49b4c BM |
498 | WARN_ON(true); |
499 | ||
500 | return __virtio_test_bit(vdev->parent, fbit); | |
501 | } | |
502 | ||
503 | static inline bool virtio_legacy_is_little_endian(void) | |
504 | { | |
505 | #ifdef __LITTLE_ENDIAN | |
506 | return true; | |
507 | #else | |
508 | return false; | |
509 | #endif | |
510 | } | |
511 | ||
512 | static inline bool virtio_is_little_endian(struct udevice *vdev) | |
513 | { | |
514 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent); | |
515 | ||
516 | return !uc_priv->legacy || virtio_legacy_is_little_endian(); | |
517 | } | |
518 | ||
519 | /* Memory accessors */ | |
520 | static inline u16 virtio16_to_cpu(struct udevice *vdev, __virtio16 val) | |
521 | { | |
522 | return __virtio16_to_cpu(virtio_is_little_endian(vdev), val); | |
523 | } | |
524 | ||
525 | static inline __virtio16 cpu_to_virtio16(struct udevice *vdev, u16 val) | |
526 | { | |
527 | return __cpu_to_virtio16(virtio_is_little_endian(vdev), val); | |
528 | } | |
529 | ||
530 | static inline u32 virtio32_to_cpu(struct udevice *vdev, __virtio32 val) | |
531 | { | |
532 | return __virtio32_to_cpu(virtio_is_little_endian(vdev), val); | |
533 | } | |
534 | ||
535 | static inline __virtio32 cpu_to_virtio32(struct udevice *vdev, u32 val) | |
536 | { | |
537 | return __cpu_to_virtio32(virtio_is_little_endian(vdev), val); | |
538 | } | |
539 | ||
540 | static inline u64 virtio64_to_cpu(struct udevice *vdev, __virtio64 val) | |
541 | { | |
542 | return __virtio64_to_cpu(virtio_is_little_endian(vdev), val); | |
543 | } | |
544 | ||
545 | static inline __virtio64 cpu_to_virtio64(struct udevice *vdev, u64 val) | |
546 | { | |
547 | return __cpu_to_virtio64(virtio_is_little_endian(vdev), val); | |
548 | } | |
549 | ||
550 | /* Read @count fields, @bytes each */ | |
551 | static inline void __virtio_cread_many(struct udevice *vdev, | |
552 | unsigned int offset, | |
553 | void *buf, size_t count, size_t bytes) | |
554 | { | |
555 | u32 old, gen; | |
556 | int i; | |
557 | ||
558 | /* no need to check return value as generation can be optional */ | |
559 | virtio_generation(vdev, &gen); | |
560 | do { | |
561 | old = gen; | |
562 | ||
563 | for (i = 0; i < count; i++) | |
564 | virtio_get_config(vdev, offset + bytes * i, | |
565 | buf + i * bytes, bytes); | |
566 | ||
567 | virtio_generation(vdev, &gen); | |
568 | } while (gen != old); | |
569 | } | |
570 | ||
571 | static inline void virtio_cread_bytes(struct udevice *vdev, | |
572 | unsigned int offset, | |
573 | void *buf, size_t len) | |
574 | { | |
575 | __virtio_cread_many(vdev, offset, buf, len, 1); | |
576 | } | |
577 | ||
578 | static inline u8 virtio_cread8(struct udevice *vdev, unsigned int offset) | |
579 | { | |
580 | u8 ret; | |
581 | ||
582 | virtio_get_config(vdev, offset, &ret, sizeof(ret)); | |
583 | return ret; | |
584 | } | |
585 | ||
586 | static inline void virtio_cwrite8(struct udevice *vdev, | |
587 | unsigned int offset, u8 val) | |
588 | { | |
589 | virtio_set_config(vdev, offset, &val, sizeof(val)); | |
590 | } | |
591 | ||
592 | static inline u16 virtio_cread16(struct udevice *vdev, | |
593 | unsigned int offset) | |
594 | { | |
595 | u16 ret; | |
596 | ||
597 | virtio_get_config(vdev, offset, &ret, sizeof(ret)); | |
598 | return virtio16_to_cpu(vdev, (__force __virtio16)ret); | |
599 | } | |
600 | ||
601 | static inline void virtio_cwrite16(struct udevice *vdev, | |
602 | unsigned int offset, u16 val) | |
603 | { | |
604 | val = (__force u16)cpu_to_virtio16(vdev, val); | |
605 | virtio_set_config(vdev, offset, &val, sizeof(val)); | |
606 | } | |
607 | ||
608 | static inline u32 virtio_cread32(struct udevice *vdev, | |
609 | unsigned int offset) | |
610 | { | |
611 | u32 ret; | |
612 | ||
613 | virtio_get_config(vdev, offset, &ret, sizeof(ret)); | |
614 | return virtio32_to_cpu(vdev, (__force __virtio32)ret); | |
615 | } | |
616 | ||
617 | static inline void virtio_cwrite32(struct udevice *vdev, | |
618 | unsigned int offset, u32 val) | |
619 | { | |
620 | val = (__force u32)cpu_to_virtio32(vdev, val); | |
621 | virtio_set_config(vdev, offset, &val, sizeof(val)); | |
622 | } | |
623 | ||
624 | static inline u64 virtio_cread64(struct udevice *vdev, | |
625 | unsigned int offset) | |
626 | { | |
627 | u64 ret; | |
628 | ||
629 | __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret)); | |
630 | return virtio64_to_cpu(vdev, (__force __virtio64)ret); | |
631 | } | |
632 | ||
633 | static inline void virtio_cwrite64(struct udevice *vdev, | |
634 | unsigned int offset, u64 val) | |
635 | { | |
636 | val = (__force u64)cpu_to_virtio64(vdev, val); | |
637 | virtio_set_config(vdev, offset, &val, sizeof(val)); | |
638 | } | |
639 | ||
640 | /* Config space read accessor */ | |
641 | #define virtio_cread(vdev, structname, member, ptr) \ | |
642 | do { \ | |
643 | /* Must match the member's type, and be integer */ \ | |
644 | if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \ | |
645 | (*ptr) = 1; \ | |
646 | \ | |
647 | switch (sizeof(*ptr)) { \ | |
648 | case 1: \ | |
649 | *(ptr) = virtio_cread8(vdev, \ | |
650 | offsetof(structname, member)); \ | |
651 | break; \ | |
652 | case 2: \ | |
653 | *(ptr) = virtio_cread16(vdev, \ | |
654 | offsetof(structname, member)); \ | |
655 | break; \ | |
656 | case 4: \ | |
657 | *(ptr) = virtio_cread32(vdev, \ | |
658 | offsetof(structname, member)); \ | |
659 | break; \ | |
660 | case 8: \ | |
661 | *(ptr) = virtio_cread64(vdev, \ | |
662 | offsetof(structname, member)); \ | |
663 | break; \ | |
664 | default: \ | |
665 | WARN_ON(true); \ | |
666 | } \ | |
667 | } while (0) | |
668 | ||
669 | /* Config space write accessor */ | |
670 | #define virtio_cwrite(vdev, structname, member, ptr) \ | |
671 | do { \ | |
672 | /* Must match the member's type, and be integer */ \ | |
673 | if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \ | |
674 | WARN_ON((*ptr) == 1); \ | |
675 | \ | |
676 | switch (sizeof(*ptr)) { \ | |
677 | case 1: \ | |
678 | virtio_cwrite8(vdev, \ | |
679 | offsetof(structname, member), \ | |
680 | *(ptr)); \ | |
681 | break; \ | |
682 | case 2: \ | |
683 | virtio_cwrite16(vdev, \ | |
684 | offsetof(structname, member), \ | |
685 | *(ptr)); \ | |
686 | break; \ | |
687 | case 4: \ | |
688 | virtio_cwrite32(vdev, \ | |
689 | offsetof(structname, member), \ | |
690 | *(ptr)); \ | |
691 | break; \ | |
692 | case 8: \ | |
693 | virtio_cwrite64(vdev, \ | |
694 | offsetof(structname, member), \ | |
695 | *(ptr)); \ | |
696 | break; \ | |
697 | default: \ | |
698 | WARN_ON(true); \ | |
699 | } \ | |
700 | } while (0) | |
701 | ||
702 | /* Conditional config space accessors */ | |
703 | #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \ | |
704 | ({ \ | |
705 | int _r = 0; \ | |
706 | if (!virtio_has_feature(vdev, fbit)) \ | |
707 | _r = -ENOENT; \ | |
708 | else \ | |
709 | virtio_cread(vdev, structname, member, ptr); \ | |
710 | _r; \ | |
711 | }) | |
712 | ||
713 | #endif /* __VIRTIO_H__ */ |