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