1 // SPDX-License-Identifier: GPL-2.0+
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.
14 * See http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf for
15 * the VirtIO specification v1.0.
18 #define LOG_CATEGORY UCLASS_VIRTIO
24 #include <virtio_types.h>
27 #include <linux/bug.h>
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,
32 [VIRTIO_ID_RNG] = VIRTIO_RNG_DRV_NAME,
35 int virtio_get_config(struct udevice *vdev, unsigned int offset,
36 void *buf, unsigned int len)
38 struct dm_virtio_ops *ops;
40 ops = virtio_get_ops(vdev->parent);
42 return ops->get_config(vdev->parent, offset, buf, len);
45 int virtio_set_config(struct udevice *vdev, unsigned int offset,
46 void *buf, unsigned int len)
48 struct dm_virtio_ops *ops;
50 ops = virtio_get_ops(vdev->parent);
52 return ops->set_config(vdev->parent, offset, buf, len);
55 int virtio_generation(struct udevice *vdev, u32 *counter)
57 struct dm_virtio_ops *ops;
59 ops = virtio_get_ops(vdev->parent);
63 return ops->generation(vdev->parent, counter);
66 int virtio_get_status(struct udevice *vdev, u8 *status)
68 struct dm_virtio_ops *ops;
70 ops = virtio_get_ops(vdev->parent);
72 return ops->get_status(vdev->parent, status);
75 int virtio_set_status(struct udevice *vdev, u8 status)
77 struct dm_virtio_ops *ops;
79 ops = virtio_get_ops(vdev->parent);
81 return ops->set_status(vdev->parent, status);
84 int virtio_reset(struct udevice *vdev)
86 struct dm_virtio_ops *ops;
88 ops = virtio_get_ops(vdev->parent);
90 return ops->reset(vdev->parent);
93 int virtio_get_features(struct udevice *vdev, u64 *features)
95 struct dm_virtio_ops *ops;
97 ops = virtio_get_ops(vdev->parent);
99 return ops->get_features(vdev->parent, features);
102 int virtio_set_features(struct udevice *vdev)
104 struct dm_virtio_ops *ops;
106 ops = virtio_get_ops(vdev->parent);
108 return ops->set_features(vdev->parent);
111 int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
112 struct virtqueue *vqs[])
114 struct dm_virtio_ops *ops;
116 ops = virtio_get_ops(vdev->parent);
118 return ops->find_vqs(vdev->parent, nvqs, vqs);
121 int virtio_del_vqs(struct udevice *vdev)
123 struct dm_virtio_ops *ops;
125 ops = virtio_get_ops(vdev->parent);
127 return ops->del_vqs(vdev->parent);
130 int virtio_notify(struct udevice *vdev, struct virtqueue *vq)
132 struct dm_virtio_ops *ops;
134 ops = virtio_get_ops(vdev->parent);
136 return ops->notify(vdev->parent, vq);
139 void virtio_add_status(struct udevice *vdev, u8 status)
143 if (!virtio_get_status(vdev, &old))
144 virtio_set_status(vdev, old | status);
147 int virtio_finalize_features(struct udevice *vdev)
149 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
153 ret = virtio_set_features(vdev);
160 virtio_add_status(vdev, VIRTIO_CONFIG_S_FEATURES_OK);
161 ret = virtio_get_status(vdev, &status);
164 if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
165 debug("(%s): device refuses features %x\n", vdev->name, status);
172 void virtio_driver_features_init(struct virtio_dev_priv *priv,
175 const u32 *feature_legacy,
176 u32 feature_legacy_size)
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;
184 int virtio_init(void)
186 /* Enumerate all known virtio devices */
187 return uclass_probe_all(UCLASS_VIRTIO);
190 static int virtio_uclass_pre_probe(struct udevice *udev)
192 struct dm_virtio_ops *ops;
194 ops = (struct dm_virtio_ops *)(udev->driver->ops);
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.
200 * Only generation op is optional. All other ops are must-have.
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)
212 static int virtio_uclass_post_probe(struct udevice *udev)
214 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
215 char dev_name[30], *str;
216 struct udevice *vdev;
220 if (uc_priv->device >= VIRTIO_ID_MAX_NUM) {
221 debug("(%s): virtio device ID %d exceeds maximum num\n",
222 udev->name, uc_priv->device);
226 name = virtio_drv_name[uc_priv->device];
228 debug("(%s): underlying virtio device driver unavailable\n",
233 snprintf(dev_name, sizeof(dev_name), "%s#%d", name, dev_seq(udev));
234 str = strdup(dev_name);
238 ret = device_bind_driver(udev, name, str, &vdev);
239 if (ret == -ENOENT) {
240 debug("(%s): %s driver not configured\n", udev->name, name);
247 device_set_name_alloced(vdev);
249 if (uc_priv->device == VIRTIO_ID_BLOCK && !IS_ENABLED(CONFIG_SANDBOX)) {
250 ret = bootdev_setup_for_sibling_blk(vdev, "virtio_bootdev");
252 return log_msg_ret("bootdev", ret);
255 INIT_LIST_HEAD(&uc_priv->vqs);
260 static int virtio_uclass_child_post_bind(struct udevice *vdev)
262 /* Acknowledge that we've seen the device */
263 virtio_add_status(vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
268 static int virtio_uclass_child_pre_probe(struct udevice *vdev)
270 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
273 u64 driver_features_legacy;
277 /* bootdevs are not virtio devices */
278 if (device_get_uclass_id(vdev) == UCLASS_BOOTDEV)
282 * Save the real virtio device (eg: virtio-net, virtio-blk) to
283 * the transport (parent) device's uclass priv for future use.
285 uc_priv->vdev = vdev;
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.
291 ret = virtio_reset(vdev);
295 /* We have a driver! */
296 virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER);
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;
305 /* Figure out what features the driver supports */
307 for (i = 0; i < uc_priv->feature_table_size; i++) {
308 unsigned int f = uc_priv->feature_table[i];
311 driver_features |= (1ULL << f);
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];
321 driver_features_legacy |= (1ULL << f);
324 driver_features_legacy = driver_features;
327 if (uc_priv->legacy) {
328 debug("(%s): legacy virtio device\n", vdev->name);
329 uc_priv->features = driver_features_legacy & device_features;
331 debug("(%s): v1.0 complaint virtio device\n", vdev->name);
332 uc_priv->features = driver_features & device_features;
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 || i == VIRTIO_F_IOMMU_PLATFORM))
339 __virtio_set_bit(vdev->parent, i);
341 debug("(%s) final negotiated features supported %016llx\n",
342 vdev->name, uc_priv->features);
343 ret = virtio_finalize_features(vdev);
350 virtio_add_status(vdev, VIRTIO_CONFIG_S_FAILED);
354 static int virtio_uclass_child_post_probe(struct udevice *vdev)
356 /* Indicates that the driver is set up and ready to drive the device */
357 virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER_OK);
362 static int virtio_bootdev_bind(struct udevice *dev)
364 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
366 ucp->prio = BOOTDEVP_4_SCAN_FAST;
371 static int virtio_bootdev_hunt(struct bootdev_hunter *info, bool show)
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);
381 ret = uclass_probe_all(UCLASS_VIRTIO);
382 if (ret && ret != -ENOENT)
383 return log_msg_ret("vir", ret);
388 UCLASS_DRIVER(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,
397 .per_device_auto = sizeof(struct virtio_dev_priv),
400 struct bootdev_ops virtio_bootdev_ops = {
403 static const struct udevice_id virtio_bootdev_ids[] = {
404 { .compatible = "u-boot,bootdev-virtio" },
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,
416 BOOTDEV_HUNTER(virtio_bootdev_hunter) = {
417 .prio = BOOTDEVP_4_SCAN_FAST,
418 .uclass = UCLASS_VIRTIO,
419 .hunt = virtio_bootdev_hunt,
420 .drv = DM_DRIVER_REF(virtio_bootdev),