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