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 | |
| 18 | #include <common.h> |
| 19 | #include <dm.h> |
| 20 | #include <virtio_types.h> |
| 21 | #include <virtio.h> |
| 22 | #include <dm/lists.h> |
| 23 | |
| 24 | static const char *const virtio_drv_name[VIRTIO_ID_MAX_NUM] = { |
| 25 | [VIRTIO_ID_NET] = VIRTIO_NET_DRV_NAME, |
| 26 | [VIRTIO_ID_BLOCK] = VIRTIO_BLK_DRV_NAME, |
Sughosh Ganu | 081da0a | 2019-12-29 15:30:14 +0530 | [diff] [blame] | 27 | [VIRTIO_ID_RNG] = VIRTIO_RNG_DRV_NAME, |
Bin Meng | db7ca2e | 2018-10-15 02:21:00 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | int virtio_get_config(struct udevice *vdev, unsigned int offset, |
| 31 | void *buf, unsigned int len) |
| 32 | { |
| 33 | struct dm_virtio_ops *ops; |
| 34 | |
| 35 | ops = virtio_get_ops(vdev->parent); |
| 36 | |
| 37 | return ops->get_config(vdev->parent, offset, buf, len); |
| 38 | } |
| 39 | |
| 40 | int virtio_set_config(struct udevice *vdev, unsigned int offset, |
| 41 | void *buf, unsigned int len) |
| 42 | { |
| 43 | struct dm_virtio_ops *ops; |
| 44 | |
| 45 | ops = virtio_get_ops(vdev->parent); |
| 46 | |
| 47 | return ops->set_config(vdev->parent, offset, buf, len); |
| 48 | } |
| 49 | |
| 50 | int virtio_generation(struct udevice *vdev, u32 *counter) |
| 51 | { |
| 52 | struct dm_virtio_ops *ops; |
| 53 | |
| 54 | ops = virtio_get_ops(vdev->parent); |
| 55 | if (!ops->generation) |
| 56 | return -ENOSYS; |
| 57 | |
| 58 | return ops->generation(vdev->parent, counter); |
| 59 | } |
| 60 | |
| 61 | int virtio_get_status(struct udevice *vdev, u8 *status) |
| 62 | { |
| 63 | struct dm_virtio_ops *ops; |
| 64 | |
| 65 | ops = virtio_get_ops(vdev->parent); |
| 66 | |
| 67 | return ops->get_status(vdev->parent, status); |
| 68 | } |
| 69 | |
| 70 | int virtio_set_status(struct udevice *vdev, u8 status) |
| 71 | { |
| 72 | struct dm_virtio_ops *ops; |
| 73 | |
| 74 | ops = virtio_get_ops(vdev->parent); |
| 75 | |
| 76 | return ops->set_status(vdev->parent, status); |
| 77 | } |
| 78 | |
| 79 | int virtio_reset(struct udevice *vdev) |
| 80 | { |
| 81 | struct dm_virtio_ops *ops; |
| 82 | |
| 83 | ops = virtio_get_ops(vdev->parent); |
| 84 | |
| 85 | return ops->reset(vdev->parent); |
| 86 | } |
| 87 | |
| 88 | int virtio_get_features(struct udevice *vdev, u64 *features) |
| 89 | { |
| 90 | struct dm_virtio_ops *ops; |
| 91 | |
| 92 | ops = virtio_get_ops(vdev->parent); |
| 93 | |
| 94 | return ops->get_features(vdev->parent, features); |
| 95 | } |
| 96 | |
| 97 | int virtio_set_features(struct udevice *vdev) |
| 98 | { |
| 99 | struct dm_virtio_ops *ops; |
| 100 | |
| 101 | ops = virtio_get_ops(vdev->parent); |
| 102 | |
| 103 | return ops->set_features(vdev->parent); |
| 104 | } |
| 105 | |
| 106 | int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs, |
| 107 | struct virtqueue *vqs[]) |
| 108 | { |
| 109 | struct dm_virtio_ops *ops; |
| 110 | |
| 111 | ops = virtio_get_ops(vdev->parent); |
| 112 | |
| 113 | return ops->find_vqs(vdev->parent, nvqs, vqs); |
| 114 | } |
| 115 | |
| 116 | int virtio_del_vqs(struct udevice *vdev) |
| 117 | { |
| 118 | struct dm_virtio_ops *ops; |
| 119 | |
| 120 | ops = virtio_get_ops(vdev->parent); |
| 121 | |
| 122 | return ops->del_vqs(vdev->parent); |
| 123 | } |
| 124 | |
| 125 | int virtio_notify(struct udevice *vdev, struct virtqueue *vq) |
| 126 | { |
| 127 | struct dm_virtio_ops *ops; |
| 128 | |
| 129 | ops = virtio_get_ops(vdev->parent); |
| 130 | |
| 131 | return ops->notify(vdev->parent, vq); |
| 132 | } |
| 133 | |
| 134 | void virtio_add_status(struct udevice *vdev, u8 status) |
| 135 | { |
| 136 | u8 old; |
| 137 | |
| 138 | if (!virtio_get_status(vdev, &old)) |
| 139 | virtio_set_status(vdev, old | status); |
| 140 | } |
| 141 | |
| 142 | int virtio_finalize_features(struct udevice *vdev) |
| 143 | { |
| 144 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent); |
| 145 | u8 status; |
| 146 | int ret; |
| 147 | |
| 148 | ret = virtio_set_features(vdev); |
| 149 | if (ret) |
| 150 | return ret; |
| 151 | |
| 152 | if (uc_priv->legacy) |
| 153 | return 0; |
| 154 | |
| 155 | virtio_add_status(vdev, VIRTIO_CONFIG_S_FEATURES_OK); |
| 156 | ret = virtio_get_status(vdev, &status); |
| 157 | if (ret) |
| 158 | return ret; |
| 159 | if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) { |
| 160 | debug("(%s): device refuses features %x\n", vdev->name, status); |
| 161 | return -ENODEV; |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | void virtio_driver_features_init(struct virtio_dev_priv *priv, |
| 168 | const u32 *feature, |
| 169 | u32 feature_size, |
| 170 | const u32 *feature_legacy, |
| 171 | u32 feature_legacy_size) |
| 172 | { |
| 173 | priv->feature_table = feature; |
| 174 | priv->feature_table_size = feature_size; |
| 175 | priv->feature_table_legacy = feature_legacy; |
| 176 | priv->feature_table_size_legacy = feature_legacy_size; |
| 177 | } |
| 178 | |
| 179 | int virtio_init(void) |
| 180 | { |
| 181 | struct udevice *bus; |
| 182 | int ret; |
| 183 | |
| 184 | /* Enumerate all known virtio devices */ |
| 185 | ret = uclass_first_device(UCLASS_VIRTIO, &bus); |
| 186 | if (ret) |
| 187 | return ret; |
| 188 | |
| 189 | while (bus) { |
| 190 | ret = uclass_next_device(&bus); |
| 191 | if (ret) |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | static int virtio_uclass_pre_probe(struct udevice *udev) |
| 199 | { |
| 200 | struct dm_virtio_ops *ops; |
| 201 | |
| 202 | ops = (struct dm_virtio_ops *)(udev->driver->ops); |
| 203 | |
| 204 | /* |
| 205 | * Check virtio transport driver ops here so that we don't need |
| 206 | * check these ops each time when the virtio_xxx APIs are called. |
| 207 | * |
| 208 | * Only generation op is optional. All other ops are must-have. |
| 209 | */ |
| 210 | if (!ops->get_config || !ops->set_config || |
| 211 | !ops->get_status || !ops->set_status || |
| 212 | !ops->get_features || !ops->set_features || |
| 213 | !ops->find_vqs || !ops->del_vqs || |
| 214 | !ops->reset || !ops->notify) |
| 215 | return -ENOENT; |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int virtio_uclass_post_probe(struct udevice *udev) |
| 221 | { |
| 222 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); |
| 223 | char dev_name[30], *str; |
| 224 | struct udevice *vdev; |
| 225 | int ret; |
| 226 | |
| 227 | if (uc_priv->device > VIRTIO_ID_MAX_NUM) { |
| 228 | debug("(%s): virtio device ID %d exceeds maximum num\n", |
| 229 | udev->name, uc_priv->device); |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | if (!virtio_drv_name[uc_priv->device]) { |
| 234 | debug("(%s): underlying virtio device driver unavailable\n", |
| 235 | udev->name); |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | snprintf(dev_name, sizeof(dev_name), "%s#%d", |
| 240 | virtio_drv_name[uc_priv->device], udev->seq); |
| 241 | str = strdup(dev_name); |
| 242 | if (!str) |
| 243 | return -ENOMEM; |
| 244 | |
| 245 | ret = device_bind_driver(udev, virtio_drv_name[uc_priv->device], |
| 246 | str, &vdev); |
| 247 | if (ret == -ENOENT) { |
| 248 | debug("(%s): no driver configured\n", udev->name); |
| 249 | return 0; |
| 250 | } |
| 251 | if (ret) { |
| 252 | free(str); |
| 253 | return ret; |
| 254 | } |
| 255 | device_set_name_alloced(vdev); |
| 256 | |
| 257 | INIT_LIST_HEAD(&uc_priv->vqs); |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static int virtio_uclass_child_post_bind(struct udevice *vdev) |
| 263 | { |
| 264 | /* Acknowledge that we've seen the device */ |
| 265 | virtio_add_status(vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int virtio_uclass_child_pre_probe(struct udevice *vdev) |
| 271 | { |
| 272 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent); |
| 273 | u64 device_features; |
| 274 | u64 driver_features; |
| 275 | u64 driver_features_legacy; |
| 276 | int i; |
| 277 | int ret; |
| 278 | |
| 279 | /* |
| 280 | * Save the real virtio device (eg: virtio-net, virtio-blk) to |
| 281 | * the transport (parent) device's uclass priv for future use. |
| 282 | */ |
| 283 | uc_priv->vdev = vdev; |
| 284 | |
| 285 | /* |
| 286 | * We always start by resetting the device, in case a previous driver |
| 287 | * messed it up. This also tests that code path a little. |
| 288 | */ |
| 289 | ret = virtio_reset(vdev); |
| 290 | if (ret) |
| 291 | goto err; |
| 292 | |
| 293 | /* We have a driver! */ |
| 294 | virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER); |
| 295 | |
| 296 | /* Figure out what features the device supports */ |
| 297 | virtio_get_features(vdev, &device_features); |
| 298 | debug("(%s) plain device features supported %016llx\n", |
| 299 | vdev->name, device_features); |
| 300 | if (!(device_features & (1ULL << VIRTIO_F_VERSION_1))) |
| 301 | uc_priv->legacy = true; |
| 302 | |
| 303 | /* Figure out what features the driver supports */ |
| 304 | driver_features = 0; |
| 305 | for (i = 0; i < uc_priv->feature_table_size; i++) { |
| 306 | unsigned int f = uc_priv->feature_table[i]; |
| 307 | |
| 308 | WARN_ON(f >= 64); |
| 309 | driver_features |= (1ULL << f); |
| 310 | } |
| 311 | |
| 312 | /* Some drivers have a separate feature table for virtio v1.0 */ |
| 313 | if (uc_priv->feature_table_legacy) { |
| 314 | driver_features_legacy = 0; |
| 315 | for (i = 0; i < uc_priv->feature_table_size_legacy; i++) { |
| 316 | unsigned int f = uc_priv->feature_table_legacy[i]; |
| 317 | |
| 318 | WARN_ON(f >= 64); |
| 319 | driver_features_legacy |= (1ULL << f); |
| 320 | } |
| 321 | } else { |
| 322 | driver_features_legacy = driver_features; |
| 323 | } |
| 324 | |
| 325 | if (uc_priv->legacy) { |
| 326 | debug("(%s): legacy virtio device\n", vdev->name); |
| 327 | uc_priv->features = driver_features_legacy & device_features; |
| 328 | } else { |
| 329 | debug("(%s): v1.0 complaint virtio device\n", vdev->name); |
| 330 | uc_priv->features = driver_features & device_features; |
| 331 | } |
| 332 | |
| 333 | /* Transport features always preserved to pass to finalize_features */ |
| 334 | for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) |
| 335 | if ((device_features & (1ULL << i)) && |
| 336 | (i == VIRTIO_F_VERSION_1)) |
| 337 | __virtio_set_bit(vdev->parent, i); |
| 338 | |
| 339 | debug("(%s) final negotiated features supported %016llx\n", |
| 340 | vdev->name, uc_priv->features); |
| 341 | ret = virtio_finalize_features(vdev); |
| 342 | if (ret) |
| 343 | goto err; |
| 344 | |
| 345 | return 0; |
| 346 | |
| 347 | err: |
| 348 | virtio_add_status(vdev, VIRTIO_CONFIG_S_FAILED); |
| 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | static int virtio_uclass_child_post_probe(struct udevice *vdev) |
| 353 | { |
| 354 | /* Indicates that the driver is set up and ready to drive the device */ |
| 355 | virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER_OK); |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | UCLASS_DRIVER(virtio) = { |
| 361 | .name = "virtio", |
| 362 | .id = UCLASS_VIRTIO, |
| 363 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 364 | .pre_probe = virtio_uclass_pre_probe, |
| 365 | .post_probe = virtio_uclass_post_probe, |
| 366 | .child_post_bind = virtio_uclass_child_post_bind, |
| 367 | .child_pre_probe = virtio_uclass_child_pre_probe, |
| 368 | .child_post_probe = virtio_uclass_child_post_probe, |
| 369 | .per_device_auto_alloc_size = sizeof(struct virtio_dev_priv), |
| 370 | }; |