Bin Meng | db7ca2e | 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 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 18 | #define LOG_CATEGORY UCLASS_VIRTIO |
| 19 | |
Simon Glass | a4bf8f6 | 2023-01-17 10:47:52 -0700 | [diff] [blame] | 20 | #include <bootdev.h> |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 21 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 22 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 23 | #include <malloc.h> |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 24 | #include <virtio_types.h> |
| 25 | #include <virtio.h> |
| 26 | #include <dm/lists.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 27 | #include <linux/bug.h> |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 28 | |
| 29 | static const char *const virtio_drv_name[VIRTIO_ID_MAX_NUM] = { |
| 30 | [VIRTIO_ID_NET] = VIRTIO_NET_DRV_NAME, |
| 31 | [VIRTIO_ID_BLOCK] = VIRTIO_BLK_DRV_NAME, |
Sughosh Ganu | 081da0a | 2019-12-29 15:30:14 +0530 | [diff] [blame] | 32 | [VIRTIO_ID_RNG] = VIRTIO_RNG_DRV_NAME, |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | int virtio_get_config(struct udevice *vdev, unsigned int offset, |
| 36 | void *buf, unsigned int len) |
| 37 | { |
| 38 | struct dm_virtio_ops *ops; |
| 39 | |
| 40 | ops = virtio_get_ops(vdev->parent); |
| 41 | |
| 42 | return ops->get_config(vdev->parent, offset, buf, len); |
| 43 | } |
| 44 | |
| 45 | int virtio_set_config(struct udevice *vdev, unsigned int offset, |
| 46 | void *buf, unsigned int len) |
| 47 | { |
| 48 | struct dm_virtio_ops *ops; |
| 49 | |
| 50 | ops = virtio_get_ops(vdev->parent); |
| 51 | |
| 52 | return ops->set_config(vdev->parent, offset, buf, len); |
| 53 | } |
| 54 | |
| 55 | int virtio_generation(struct udevice *vdev, u32 *counter) |
| 56 | { |
| 57 | struct dm_virtio_ops *ops; |
| 58 | |
| 59 | ops = virtio_get_ops(vdev->parent); |
| 60 | if (!ops->generation) |
| 61 | return -ENOSYS; |
| 62 | |
| 63 | return ops->generation(vdev->parent, counter); |
| 64 | } |
| 65 | |
| 66 | int virtio_get_status(struct udevice *vdev, u8 *status) |
| 67 | { |
| 68 | struct dm_virtio_ops *ops; |
| 69 | |
| 70 | ops = virtio_get_ops(vdev->parent); |
| 71 | |
| 72 | return ops->get_status(vdev->parent, status); |
| 73 | } |
| 74 | |
| 75 | int virtio_set_status(struct udevice *vdev, u8 status) |
| 76 | { |
| 77 | struct dm_virtio_ops *ops; |
| 78 | |
| 79 | ops = virtio_get_ops(vdev->parent); |
| 80 | |
| 81 | return ops->set_status(vdev->parent, status); |
| 82 | } |
| 83 | |
| 84 | int virtio_reset(struct udevice *vdev) |
| 85 | { |
| 86 | struct dm_virtio_ops *ops; |
| 87 | |
| 88 | ops = virtio_get_ops(vdev->parent); |
| 89 | |
| 90 | return ops->reset(vdev->parent); |
| 91 | } |
| 92 | |
| 93 | int virtio_get_features(struct udevice *vdev, u64 *features) |
| 94 | { |
| 95 | struct dm_virtio_ops *ops; |
| 96 | |
| 97 | ops = virtio_get_ops(vdev->parent); |
| 98 | |
| 99 | return ops->get_features(vdev->parent, features); |
| 100 | } |
| 101 | |
| 102 | int virtio_set_features(struct udevice *vdev) |
| 103 | { |
| 104 | struct dm_virtio_ops *ops; |
| 105 | |
| 106 | ops = virtio_get_ops(vdev->parent); |
| 107 | |
| 108 | return ops->set_features(vdev->parent); |
| 109 | } |
| 110 | |
| 111 | int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs, |
| 112 | struct virtqueue *vqs[]) |
| 113 | { |
| 114 | struct dm_virtio_ops *ops; |
| 115 | |
| 116 | ops = virtio_get_ops(vdev->parent); |
| 117 | |
| 118 | return ops->find_vqs(vdev->parent, nvqs, vqs); |
| 119 | } |
| 120 | |
| 121 | int virtio_del_vqs(struct udevice *vdev) |
| 122 | { |
| 123 | struct dm_virtio_ops *ops; |
| 124 | |
| 125 | ops = virtio_get_ops(vdev->parent); |
| 126 | |
| 127 | return ops->del_vqs(vdev->parent); |
| 128 | } |
| 129 | |
| 130 | int virtio_notify(struct udevice *vdev, struct virtqueue *vq) |
| 131 | { |
| 132 | struct dm_virtio_ops *ops; |
| 133 | |
| 134 | ops = virtio_get_ops(vdev->parent); |
| 135 | |
| 136 | return ops->notify(vdev->parent, vq); |
| 137 | } |
| 138 | |
| 139 | void virtio_add_status(struct udevice *vdev, u8 status) |
| 140 | { |
| 141 | u8 old; |
| 142 | |
| 143 | if (!virtio_get_status(vdev, &old)) |
| 144 | virtio_set_status(vdev, old | status); |
| 145 | } |
| 146 | |
| 147 | int virtio_finalize_features(struct udevice *vdev) |
| 148 | { |
| 149 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent); |
| 150 | u8 status; |
| 151 | int ret; |
| 152 | |
| 153 | ret = virtio_set_features(vdev); |
| 154 | if (ret) |
| 155 | return ret; |
| 156 | |
| 157 | if (uc_priv->legacy) |
| 158 | return 0; |
| 159 | |
| 160 | virtio_add_status(vdev, VIRTIO_CONFIG_S_FEATURES_OK); |
| 161 | ret = virtio_get_status(vdev, &status); |
| 162 | if (ret) |
| 163 | return ret; |
| 164 | if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) { |
| 165 | debug("(%s): device refuses features %x\n", vdev->name, status); |
Simon Glass | d47ec04 | 2023-01-17 10:47:49 -0700 | [diff] [blame] | 166 | return -EINVAL; |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | void virtio_driver_features_init(struct virtio_dev_priv *priv, |
| 173 | const u32 *feature, |
| 174 | u32 feature_size, |
| 175 | const u32 *feature_legacy, |
| 176 | u32 feature_legacy_size) |
| 177 | { |
| 178 | priv->feature_table = feature; |
| 179 | priv->feature_table_size = feature_size; |
| 180 | priv->feature_table_legacy = feature_legacy; |
| 181 | priv->feature_table_size_legacy = feature_legacy_size; |
| 182 | } |
| 183 | |
| 184 | int virtio_init(void) |
| 185 | { |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 186 | /* Enumerate all known virtio devices */ |
Michal Suchanek | 44322b5 | 2022-10-12 21:57:51 +0200 | [diff] [blame] | 187 | return uclass_probe_all(UCLASS_VIRTIO); |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static int virtio_uclass_pre_probe(struct udevice *udev) |
| 191 | { |
| 192 | struct dm_virtio_ops *ops; |
| 193 | |
| 194 | ops = (struct dm_virtio_ops *)(udev->driver->ops); |
| 195 | |
| 196 | /* |
| 197 | * Check virtio transport driver ops here so that we don't need |
| 198 | * check these ops each time when the virtio_xxx APIs are called. |
| 199 | * |
| 200 | * Only generation op is optional. All other ops are must-have. |
| 201 | */ |
| 202 | if (!ops->get_config || !ops->set_config || |
| 203 | !ops->get_status || !ops->set_status || |
| 204 | !ops->get_features || !ops->set_features || |
| 205 | !ops->find_vqs || !ops->del_vqs || |
| 206 | !ops->reset || !ops->notify) |
| 207 | return -ENOENT; |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int virtio_uclass_post_probe(struct udevice *udev) |
| 213 | { |
| 214 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); |
| 215 | char dev_name[30], *str; |
| 216 | struct udevice *vdev; |
Simon Glass | ddceecf | 2023-01-17 10:47:48 -0700 | [diff] [blame] | 217 | const char *name; |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 218 | int ret; |
| 219 | |
Vincent Stehlé | 08dd615 | 2021-02-14 19:39:04 +0100 | [diff] [blame] | 220 | if (uc_priv->device >= VIRTIO_ID_MAX_NUM) { |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 221 | debug("(%s): virtio device ID %d exceeds maximum num\n", |
| 222 | udev->name, uc_priv->device); |
| 223 | return 0; |
| 224 | } |
| 225 | |
Simon Glass | ddceecf | 2023-01-17 10:47:48 -0700 | [diff] [blame] | 226 | name = virtio_drv_name[uc_priv->device]; |
| 227 | if (!name) { |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 228 | debug("(%s): underlying virtio device driver unavailable\n", |
| 229 | udev->name); |
| 230 | return 0; |
| 231 | } |
| 232 | |
Simon Glass | ddceecf | 2023-01-17 10:47:48 -0700 | [diff] [blame] | 233 | snprintf(dev_name, sizeof(dev_name), "%s#%d", name, dev_seq(udev)); |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 234 | str = strdup(dev_name); |
| 235 | if (!str) |
| 236 | return -ENOMEM; |
| 237 | |
Simon Glass | ddceecf | 2023-01-17 10:47:48 -0700 | [diff] [blame] | 238 | ret = device_bind_driver(udev, name, str, &vdev); |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 239 | if (ret == -ENOENT) { |
Heinrich Schuchardt | b27c27a | 2023-07-26 17:43:40 +0200 | [diff] [blame] | 240 | debug("(%s): %s driver not configured\n", udev->name, name); |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | if (ret) { |
| 244 | free(str); |
| 245 | return ret; |
| 246 | } |
| 247 | device_set_name_alloced(vdev); |
| 248 | |
Simon Glass | 2bf7734 | 2023-01-28 15:00:20 -0700 | [diff] [blame] | 249 | if (uc_priv->device == VIRTIO_ID_BLOCK && !IS_ENABLED(CONFIG_SANDBOX)) { |
Simon Glass | b1d581d | 2023-07-30 11:15:14 -0600 | [diff] [blame] | 250 | ret = bootdev_setup_for_sibling_blk(vdev, "virtio_bootdev"); |
Simon Glass | a4bf8f6 | 2023-01-17 10:47:52 -0700 | [diff] [blame] | 251 | if (ret) |
| 252 | return log_msg_ret("bootdev", ret); |
| 253 | } |
| 254 | |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 255 | INIT_LIST_HEAD(&uc_priv->vqs); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int virtio_uclass_child_post_bind(struct udevice *vdev) |
| 261 | { |
| 262 | /* Acknowledge that we've seen the device */ |
| 263 | virtio_add_status(vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE); |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int virtio_uclass_child_pre_probe(struct udevice *vdev) |
| 269 | { |
| 270 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent); |
| 271 | u64 device_features; |
| 272 | u64 driver_features; |
| 273 | u64 driver_features_legacy; |
| 274 | int i; |
| 275 | int ret; |
| 276 | |
Simon Glass | 2bf7734 | 2023-01-28 15:00:20 -0700 | [diff] [blame] | 277 | /* bootdevs are not virtio devices */ |
| 278 | if (device_get_uclass_id(vdev) == UCLASS_BOOTDEV) |
| 279 | return 0; |
| 280 | |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 281 | /* |
| 282 | * Save the real virtio device (eg: virtio-net, virtio-blk) to |
| 283 | * the transport (parent) device's uclass priv for future use. |
| 284 | */ |
| 285 | uc_priv->vdev = vdev; |
| 286 | |
| 287 | /* |
| 288 | * We always start by resetting the device, in case a previous driver |
| 289 | * messed it up. This also tests that code path a little. |
| 290 | */ |
| 291 | ret = virtio_reset(vdev); |
| 292 | if (ret) |
| 293 | goto err; |
| 294 | |
| 295 | /* We have a driver! */ |
| 296 | virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER); |
| 297 | |
| 298 | /* Figure out what features the device supports */ |
| 299 | virtio_get_features(vdev, &device_features); |
| 300 | debug("(%s) plain device features supported %016llx\n", |
| 301 | vdev->name, device_features); |
| 302 | if (!(device_features & (1ULL << VIRTIO_F_VERSION_1))) |
| 303 | uc_priv->legacy = true; |
| 304 | |
| 305 | /* Figure out what features the driver supports */ |
| 306 | driver_features = 0; |
| 307 | for (i = 0; i < uc_priv->feature_table_size; i++) { |
| 308 | unsigned int f = uc_priv->feature_table[i]; |
| 309 | |
| 310 | WARN_ON(f >= 64); |
| 311 | driver_features |= (1ULL << f); |
| 312 | } |
| 313 | |
| 314 | /* Some drivers have a separate feature table for virtio v1.0 */ |
| 315 | if (uc_priv->feature_table_legacy) { |
| 316 | driver_features_legacy = 0; |
| 317 | for (i = 0; i < uc_priv->feature_table_size_legacy; i++) { |
| 318 | unsigned int f = uc_priv->feature_table_legacy[i]; |
| 319 | |
| 320 | WARN_ON(f >= 64); |
| 321 | driver_features_legacy |= (1ULL << f); |
| 322 | } |
| 323 | } else { |
| 324 | driver_features_legacy = driver_features; |
| 325 | } |
| 326 | |
| 327 | if (uc_priv->legacy) { |
| 328 | debug("(%s): legacy virtio device\n", vdev->name); |
| 329 | uc_priv->features = driver_features_legacy & device_features; |
| 330 | } else { |
| 331 | debug("(%s): v1.0 complaint virtio device\n", vdev->name); |
| 332 | uc_priv->features = driver_features & device_features; |
| 333 | } |
| 334 | |
| 335 | /* Transport features always preserved to pass to finalize_features */ |
| 336 | for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) |
| 337 | if ((device_features & (1ULL << i)) && |
Will Deacon | 3dcaf6d | 2023-03-29 22:24:55 +0800 | [diff] [blame] | 338 | (i == VIRTIO_F_VERSION_1 || i == VIRTIO_F_IOMMU_PLATFORM)) |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 339 | __virtio_set_bit(vdev->parent, i); |
| 340 | |
| 341 | debug("(%s) final negotiated features supported %016llx\n", |
| 342 | vdev->name, uc_priv->features); |
| 343 | ret = virtio_finalize_features(vdev); |
| 344 | if (ret) |
| 345 | goto err; |
| 346 | |
| 347 | return 0; |
| 348 | |
| 349 | err: |
| 350 | virtio_add_status(vdev, VIRTIO_CONFIG_S_FAILED); |
| 351 | return ret; |
| 352 | } |
| 353 | |
| 354 | static int virtio_uclass_child_post_probe(struct udevice *vdev) |
| 355 | { |
| 356 | /* Indicates that the driver is set up and ready to drive the device */ |
| 357 | virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER_OK); |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
Simon Glass | a4bf8f6 | 2023-01-17 10:47:52 -0700 | [diff] [blame] | 362 | static int virtio_bootdev_bind(struct udevice *dev) |
| 363 | { |
| 364 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 365 | |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 366 | ucp->prio = BOOTDEVP_4_SCAN_FAST; |
Simon Glass | a4bf8f6 | 2023-01-17 10:47:52 -0700 | [diff] [blame] | 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static int virtio_bootdev_hunt(struct bootdev_hunter *info, bool show) |
| 372 | { |
| 373 | int ret; |
| 374 | |
Simon Glass | 83009b3 | 2023-04-24 13:49:45 +1200 | [diff] [blame] | 375 | if (IS_ENABLED(CONFIG_PCI)) { |
| 376 | ret = uclass_probe_all(UCLASS_PCI); |
| 377 | if (ret && ret != -ENOENT) |
| 378 | return log_msg_ret("pci", ret); |
| 379 | } |
| 380 | |
Simon Glass | a4bf8f6 | 2023-01-17 10:47:52 -0700 | [diff] [blame] | 381 | ret = uclass_probe_all(UCLASS_VIRTIO); |
| 382 | if (ret && ret != -ENOENT) |
| 383 | return log_msg_ret("vir", ret); |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 388 | UCLASS_DRIVER(virtio) = { |
| 389 | .name = "virtio", |
| 390 | .id = UCLASS_VIRTIO, |
| 391 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 392 | .pre_probe = virtio_uclass_pre_probe, |
| 393 | .post_probe = virtio_uclass_post_probe, |
| 394 | .child_post_bind = virtio_uclass_child_post_bind, |
| 395 | .child_pre_probe = virtio_uclass_child_pre_probe, |
| 396 | .child_post_probe = virtio_uclass_child_post_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 397 | .per_device_auto = sizeof(struct virtio_dev_priv), |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 398 | }; |
Simon Glass | a4bf8f6 | 2023-01-17 10:47:52 -0700 | [diff] [blame] | 399 | |
| 400 | struct bootdev_ops virtio_bootdev_ops = { |
| 401 | }; |
| 402 | |
| 403 | static const struct udevice_id virtio_bootdev_ids[] = { |
| 404 | { .compatible = "u-boot,bootdev-virtio" }, |
| 405 | { } |
| 406 | }; |
| 407 | |
| 408 | U_BOOT_DRIVER(virtio_bootdev) = { |
| 409 | .name = "virtio_bootdev", |
| 410 | .id = UCLASS_BOOTDEV, |
| 411 | .ops = &virtio_bootdev_ops, |
| 412 | .bind = virtio_bootdev_bind, |
| 413 | .of_match = virtio_bootdev_ids, |
| 414 | }; |
| 415 | |
| 416 | BOOTDEV_HUNTER(virtio_bootdev_hunter) = { |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 417 | .prio = BOOTDEVP_4_SCAN_FAST, |
Simon Glass | a4bf8f6 | 2023-01-17 10:47:52 -0700 | [diff] [blame] | 418 | .uclass = UCLASS_VIRTIO, |
| 419 | .hunt = virtio_bootdev_hunt, |
| 420 | .drv = DM_DRIVER_REF(virtio_bootdev), |
| 421 | }; |