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 | |
| 18 | #include <common.h> |
| 19 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame^] | 20 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 21 | #include <malloc.h> |
Bin Meng | 8fb49b4 | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 22 | #include <virtio_types.h> |
| 23 | #include <virtio.h> |
| 24 | #include <dm/lists.h> |
| 25 | |
| 26 | static const char *const virtio_drv_name[VIRTIO_ID_MAX_NUM] = { |
| 27 | [VIRTIO_ID_NET] = VIRTIO_NET_DRV_NAME, |
| 28 | [VIRTIO_ID_BLOCK] = VIRTIO_BLK_DRV_NAME, |
Sughosh Ganu | 03018ea | 2019-12-29 15:30:14 +0530 | [diff] [blame] | 29 | [VIRTIO_ID_RNG] = VIRTIO_RNG_DRV_NAME, |
Bin Meng | 8fb49b4 | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | int virtio_get_config(struct udevice *vdev, unsigned int offset, |
| 33 | void *buf, unsigned int len) |
| 34 | { |
| 35 | struct dm_virtio_ops *ops; |
| 36 | |
| 37 | ops = virtio_get_ops(vdev->parent); |
| 38 | |
| 39 | return ops->get_config(vdev->parent, offset, buf, len); |
| 40 | } |
| 41 | |
| 42 | int virtio_set_config(struct udevice *vdev, unsigned int offset, |
| 43 | void *buf, unsigned int len) |
| 44 | { |
| 45 | struct dm_virtio_ops *ops; |
| 46 | |
| 47 | ops = virtio_get_ops(vdev->parent); |
| 48 | |
| 49 | return ops->set_config(vdev->parent, offset, buf, len); |
| 50 | } |
| 51 | |
| 52 | int virtio_generation(struct udevice *vdev, u32 *counter) |
| 53 | { |
| 54 | struct dm_virtio_ops *ops; |
| 55 | |
| 56 | ops = virtio_get_ops(vdev->parent); |
| 57 | if (!ops->generation) |
| 58 | return -ENOSYS; |
| 59 | |
| 60 | return ops->generation(vdev->parent, counter); |
| 61 | } |
| 62 | |
| 63 | int virtio_get_status(struct udevice *vdev, u8 *status) |
| 64 | { |
| 65 | struct dm_virtio_ops *ops; |
| 66 | |
| 67 | ops = virtio_get_ops(vdev->parent); |
| 68 | |
| 69 | return ops->get_status(vdev->parent, status); |
| 70 | } |
| 71 | |
| 72 | int virtio_set_status(struct udevice *vdev, u8 status) |
| 73 | { |
| 74 | struct dm_virtio_ops *ops; |
| 75 | |
| 76 | ops = virtio_get_ops(vdev->parent); |
| 77 | |
| 78 | return ops->set_status(vdev->parent, status); |
| 79 | } |
| 80 | |
| 81 | int virtio_reset(struct udevice *vdev) |
| 82 | { |
| 83 | struct dm_virtio_ops *ops; |
| 84 | |
| 85 | ops = virtio_get_ops(vdev->parent); |
| 86 | |
| 87 | return ops->reset(vdev->parent); |
| 88 | } |
| 89 | |
| 90 | int virtio_get_features(struct udevice *vdev, u64 *features) |
| 91 | { |
| 92 | struct dm_virtio_ops *ops; |
| 93 | |
| 94 | ops = virtio_get_ops(vdev->parent); |
| 95 | |
| 96 | return ops->get_features(vdev->parent, features); |
| 97 | } |
| 98 | |
| 99 | int virtio_set_features(struct udevice *vdev) |
| 100 | { |
| 101 | struct dm_virtio_ops *ops; |
| 102 | |
| 103 | ops = virtio_get_ops(vdev->parent); |
| 104 | |
| 105 | return ops->set_features(vdev->parent); |
| 106 | } |
| 107 | |
| 108 | int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs, |
| 109 | struct virtqueue *vqs[]) |
| 110 | { |
| 111 | struct dm_virtio_ops *ops; |
| 112 | |
| 113 | ops = virtio_get_ops(vdev->parent); |
| 114 | |
| 115 | return ops->find_vqs(vdev->parent, nvqs, vqs); |
| 116 | } |
| 117 | |
| 118 | int virtio_del_vqs(struct udevice *vdev) |
| 119 | { |
| 120 | struct dm_virtio_ops *ops; |
| 121 | |
| 122 | ops = virtio_get_ops(vdev->parent); |
| 123 | |
| 124 | return ops->del_vqs(vdev->parent); |
| 125 | } |
| 126 | |
| 127 | int virtio_notify(struct udevice *vdev, struct virtqueue *vq) |
| 128 | { |
| 129 | struct dm_virtio_ops *ops; |
| 130 | |
| 131 | ops = virtio_get_ops(vdev->parent); |
| 132 | |
| 133 | return ops->notify(vdev->parent, vq); |
| 134 | } |
| 135 | |
| 136 | void virtio_add_status(struct udevice *vdev, u8 status) |
| 137 | { |
| 138 | u8 old; |
| 139 | |
| 140 | if (!virtio_get_status(vdev, &old)) |
| 141 | virtio_set_status(vdev, old | status); |
| 142 | } |
| 143 | |
| 144 | int virtio_finalize_features(struct udevice *vdev) |
| 145 | { |
| 146 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent); |
| 147 | u8 status; |
| 148 | int ret; |
| 149 | |
| 150 | ret = virtio_set_features(vdev); |
| 151 | if (ret) |
| 152 | return ret; |
| 153 | |
| 154 | if (uc_priv->legacy) |
| 155 | return 0; |
| 156 | |
| 157 | virtio_add_status(vdev, VIRTIO_CONFIG_S_FEATURES_OK); |
| 158 | ret = virtio_get_status(vdev, &status); |
| 159 | if (ret) |
| 160 | return ret; |
| 161 | if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) { |
| 162 | debug("(%s): device refuses features %x\n", vdev->name, status); |
| 163 | return -ENODEV; |
| 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | void virtio_driver_features_init(struct virtio_dev_priv *priv, |
| 170 | const u32 *feature, |
| 171 | u32 feature_size, |
| 172 | const u32 *feature_legacy, |
| 173 | u32 feature_legacy_size) |
| 174 | { |
| 175 | priv->feature_table = feature; |
| 176 | priv->feature_table_size = feature_size; |
| 177 | priv->feature_table_legacy = feature_legacy; |
| 178 | priv->feature_table_size_legacy = feature_legacy_size; |
| 179 | } |
| 180 | |
| 181 | int virtio_init(void) |
| 182 | { |
| 183 | struct udevice *bus; |
| 184 | int ret; |
| 185 | |
| 186 | /* Enumerate all known virtio devices */ |
| 187 | ret = uclass_first_device(UCLASS_VIRTIO, &bus); |
| 188 | if (ret) |
| 189 | return ret; |
| 190 | |
| 191 | while (bus) { |
| 192 | ret = uclass_next_device(&bus); |
| 193 | if (ret) |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | static int virtio_uclass_pre_probe(struct udevice *udev) |
| 201 | { |
| 202 | struct dm_virtio_ops *ops; |
| 203 | |
| 204 | ops = (struct dm_virtio_ops *)(udev->driver->ops); |
| 205 | |
| 206 | /* |
| 207 | * Check virtio transport driver ops here so that we don't need |
| 208 | * check these ops each time when the virtio_xxx APIs are called. |
| 209 | * |
| 210 | * Only generation op is optional. All other ops are must-have. |
| 211 | */ |
| 212 | if (!ops->get_config || !ops->set_config || |
| 213 | !ops->get_status || !ops->set_status || |
| 214 | !ops->get_features || !ops->set_features || |
| 215 | !ops->find_vqs || !ops->del_vqs || |
| 216 | !ops->reset || !ops->notify) |
| 217 | return -ENOENT; |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static int virtio_uclass_post_probe(struct udevice *udev) |
| 223 | { |
| 224 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); |
| 225 | char dev_name[30], *str; |
| 226 | struct udevice *vdev; |
| 227 | int ret; |
| 228 | |
| 229 | if (uc_priv->device > VIRTIO_ID_MAX_NUM) { |
| 230 | debug("(%s): virtio device ID %d exceeds maximum num\n", |
| 231 | udev->name, uc_priv->device); |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | if (!virtio_drv_name[uc_priv->device]) { |
| 236 | debug("(%s): underlying virtio device driver unavailable\n", |
| 237 | udev->name); |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | snprintf(dev_name, sizeof(dev_name), "%s#%d", |
| 242 | virtio_drv_name[uc_priv->device], udev->seq); |
| 243 | str = strdup(dev_name); |
| 244 | if (!str) |
| 245 | return -ENOMEM; |
| 246 | |
| 247 | ret = device_bind_driver(udev, virtio_drv_name[uc_priv->device], |
| 248 | str, &vdev); |
| 249 | if (ret == -ENOENT) { |
| 250 | debug("(%s): no driver configured\n", udev->name); |
| 251 | return 0; |
| 252 | } |
| 253 | if (ret) { |
| 254 | free(str); |
| 255 | return ret; |
| 256 | } |
| 257 | device_set_name_alloced(vdev); |
| 258 | |
| 259 | INIT_LIST_HEAD(&uc_priv->vqs); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static int virtio_uclass_child_post_bind(struct udevice *vdev) |
| 265 | { |
| 266 | /* Acknowledge that we've seen the device */ |
| 267 | virtio_add_status(vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE); |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static int virtio_uclass_child_pre_probe(struct udevice *vdev) |
| 273 | { |
| 274 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent); |
| 275 | u64 device_features; |
| 276 | u64 driver_features; |
| 277 | u64 driver_features_legacy; |
| 278 | int i; |
| 279 | int ret; |
| 280 | |
| 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)) && |
| 338 | (i == VIRTIO_F_VERSION_1)) |
| 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 | |
| 362 | UCLASS_DRIVER(virtio) = { |
| 363 | .name = "virtio", |
| 364 | .id = UCLASS_VIRTIO, |
| 365 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 366 | .pre_probe = virtio_uclass_pre_probe, |
| 367 | .post_probe = virtio_uclass_post_probe, |
| 368 | .child_post_bind = virtio_uclass_child_post_bind, |
| 369 | .child_pre_probe = virtio_uclass_child_pre_probe, |
| 370 | .child_post_probe = virtio_uclass_child_post_probe, |
| 371 | .per_device_auto_alloc_size = sizeof(struct virtio_dev_priv), |
| 372 | }; |