1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
4 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
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
25 #include <virtio_types.h>
28 #include <linux/bug.h>
30 static const char *const virtio_drv_name[VIRTIO_ID_MAX_NUM] = {
31 [VIRTIO_ID_NET] = VIRTIO_NET_DRV_NAME,
32 [VIRTIO_ID_BLOCK] = VIRTIO_BLK_DRV_NAME,
33 [VIRTIO_ID_RNG] = VIRTIO_RNG_DRV_NAME,
36 int virtio_get_config(struct udevice *vdev, unsigned int offset,
37 void *buf, unsigned int len)
39 struct dm_virtio_ops *ops;
41 ops = virtio_get_ops(vdev->parent);
43 return ops->get_config(vdev->parent, offset, buf, len);
46 int virtio_set_config(struct udevice *vdev, unsigned int offset,
47 void *buf, unsigned int len)
49 struct dm_virtio_ops *ops;
51 ops = virtio_get_ops(vdev->parent);
53 return ops->set_config(vdev->parent, offset, buf, len);
56 int virtio_generation(struct udevice *vdev, u32 *counter)
58 struct dm_virtio_ops *ops;
60 ops = virtio_get_ops(vdev->parent);
64 return ops->generation(vdev->parent, counter);
67 int virtio_get_status(struct udevice *vdev, u8 *status)
69 struct dm_virtio_ops *ops;
71 ops = virtio_get_ops(vdev->parent);
73 return ops->get_status(vdev->parent, status);
76 int virtio_set_status(struct udevice *vdev, u8 status)
78 struct dm_virtio_ops *ops;
80 ops = virtio_get_ops(vdev->parent);
82 return ops->set_status(vdev->parent, status);
85 int virtio_reset(struct udevice *vdev)
87 struct dm_virtio_ops *ops;
89 ops = virtio_get_ops(vdev->parent);
91 return ops->reset(vdev->parent);
94 int virtio_get_features(struct udevice *vdev, u64 *features)
96 struct dm_virtio_ops *ops;
98 ops = virtio_get_ops(vdev->parent);
100 return ops->get_features(vdev->parent, features);
103 int virtio_set_features(struct udevice *vdev)
105 struct dm_virtio_ops *ops;
107 ops = virtio_get_ops(vdev->parent);
109 return ops->set_features(vdev->parent);
112 int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
113 struct virtqueue *vqs[])
115 struct dm_virtio_ops *ops;
117 ops = virtio_get_ops(vdev->parent);
119 return ops->find_vqs(vdev->parent, nvqs, vqs);
122 int virtio_del_vqs(struct udevice *vdev)
124 struct dm_virtio_ops *ops;
126 ops = virtio_get_ops(vdev->parent);
128 return ops->del_vqs(vdev->parent);
131 int virtio_notify(struct udevice *vdev, struct virtqueue *vq)
133 struct dm_virtio_ops *ops;
135 ops = virtio_get_ops(vdev->parent);
137 return ops->notify(vdev->parent, vq);
140 void virtio_add_status(struct udevice *vdev, u8 status)
144 if (!virtio_get_status(vdev, &old))
145 virtio_set_status(vdev, old | status);
148 int virtio_finalize_features(struct udevice *vdev)
150 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
154 ret = virtio_set_features(vdev);
161 virtio_add_status(vdev, VIRTIO_CONFIG_S_FEATURES_OK);
162 ret = virtio_get_status(vdev, &status);
165 if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
166 debug("(%s): device refuses features %x\n", vdev->name, status);
173 void virtio_driver_features_init(struct virtio_dev_priv *priv,
176 const u32 *feature_legacy,
177 u32 feature_legacy_size)
179 priv->feature_table = feature;
180 priv->feature_table_size = feature_size;
181 priv->feature_table_legacy = feature_legacy;
182 priv->feature_table_size_legacy = feature_legacy_size;
185 int virtio_init(void)
187 /* Enumerate all known virtio devices */
188 return uclass_probe_all(UCLASS_VIRTIO);
191 static int virtio_uclass_pre_probe(struct udevice *udev)
193 struct dm_virtio_ops *ops;
195 ops = (struct dm_virtio_ops *)(udev->driver->ops);
198 * Check virtio transport driver ops here so that we don't need
199 * check these ops each time when the virtio_xxx APIs are called.
201 * Only generation op is optional. All other ops are must-have.
203 if (!ops->get_config || !ops->set_config ||
204 !ops->get_status || !ops->set_status ||
205 !ops->get_features || !ops->set_features ||
206 !ops->find_vqs || !ops->del_vqs ||
207 !ops->reset || !ops->notify)
213 static int virtio_uclass_post_probe(struct udevice *udev)
215 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
216 char dev_name[30], *str;
217 struct udevice *vdev;
221 if (uc_priv->device >= VIRTIO_ID_MAX_NUM) {
222 debug("(%s): virtio device ID %d exceeds maximum num\n",
223 udev->name, uc_priv->device);
227 name = virtio_drv_name[uc_priv->device];
229 debug("(%s): underlying virtio device driver unavailable\n",
234 snprintf(dev_name, sizeof(dev_name), "%s#%d", name, dev_seq(udev));
235 str = strdup(dev_name);
239 ret = device_bind_driver(udev, name, str, &vdev);
240 if (ret == -ENOENT) {
241 debug("(%s): %s driver not configured\n", udev->name, name);
248 device_set_name_alloced(vdev);
250 if (uc_priv->device == VIRTIO_ID_BLOCK && !IS_ENABLED(CONFIG_SANDBOX)) {
251 ret = bootdev_setup_for_sibling_blk(vdev, "virtio_bootdev");
253 return log_msg_ret("bootdev", ret);
256 INIT_LIST_HEAD(&uc_priv->vqs);
261 static int virtio_uclass_child_post_bind(struct udevice *vdev)
263 /* Acknowledge that we've seen the device */
264 virtio_add_status(vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
269 static int virtio_uclass_child_pre_probe(struct udevice *vdev)
271 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
274 u64 driver_features_legacy;
278 /* bootdevs are not virtio devices */
279 if (device_get_uclass_id(vdev) == UCLASS_BOOTDEV)
283 * Save the real virtio device (eg: virtio-net, virtio-blk) to
284 * the transport (parent) device's uclass priv for future use.
286 uc_priv->vdev = vdev;
289 * We always start by resetting the device, in case a previous driver
290 * messed it up. This also tests that code path a little.
292 ret = virtio_reset(vdev);
296 /* We have a driver! */
297 virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER);
299 /* Figure out what features the device supports */
300 virtio_get_features(vdev, &device_features);
301 debug("(%s) plain device features supported %016llx\n",
302 vdev->name, device_features);
303 if (!(device_features & (1ULL << VIRTIO_F_VERSION_1)))
304 uc_priv->legacy = true;
306 /* Figure out what features the driver supports */
308 for (i = 0; i < uc_priv->feature_table_size; i++) {
309 unsigned int f = uc_priv->feature_table[i];
312 driver_features |= (1ULL << f);
315 /* Some drivers have a separate feature table for virtio v1.0 */
316 if (uc_priv->feature_table_legacy) {
317 driver_features_legacy = 0;
318 for (i = 0; i < uc_priv->feature_table_size_legacy; i++) {
319 unsigned int f = uc_priv->feature_table_legacy[i];
322 driver_features_legacy |= (1ULL << f);
325 driver_features_legacy = driver_features;
328 if (uc_priv->legacy) {
329 debug("(%s): legacy virtio device\n", vdev->name);
330 uc_priv->features = driver_features_legacy & device_features;
332 debug("(%s): v1.0 complaint virtio device\n", vdev->name);
333 uc_priv->features = driver_features & device_features;
336 /* Transport features always preserved to pass to finalize_features */
337 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
338 if ((device_features & (1ULL << i)) &&
339 (i == VIRTIO_F_VERSION_1 || i == VIRTIO_F_IOMMU_PLATFORM))
340 __virtio_set_bit(vdev->parent, i);
342 debug("(%s) final negotiated features supported %016llx\n",
343 vdev->name, uc_priv->features);
344 ret = virtio_finalize_features(vdev);
351 virtio_add_status(vdev, VIRTIO_CONFIG_S_FAILED);
355 static int virtio_uclass_child_post_probe(struct udevice *vdev)
357 /* Indicates that the driver is set up and ready to drive the device */
358 virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER_OK);
363 static int virtio_bootdev_bind(struct udevice *dev)
365 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
367 ucp->prio = BOOTDEVP_4_SCAN_FAST;
372 static int virtio_bootdev_hunt(struct bootdev_hunter *info, bool show)
376 if (IS_ENABLED(CONFIG_PCI)) {
377 ret = uclass_probe_all(UCLASS_PCI);
378 if (ret && ret != -ENOENT)
379 return log_msg_ret("pci", ret);
382 ret = uclass_probe_all(UCLASS_VIRTIO);
383 if (ret && ret != -ENOENT)
384 return log_msg_ret("vir", ret);
389 UCLASS_DRIVER(virtio) = {
392 .flags = DM_UC_FLAG_SEQ_ALIAS,
393 .pre_probe = virtio_uclass_pre_probe,
394 .post_probe = virtio_uclass_post_probe,
395 .child_post_bind = virtio_uclass_child_post_bind,
396 .child_pre_probe = virtio_uclass_child_pre_probe,
397 .child_post_probe = virtio_uclass_child_post_probe,
398 .per_device_auto = sizeof(struct virtio_dev_priv),
401 struct bootdev_ops virtio_bootdev_ops = {
404 static const struct udevice_id virtio_bootdev_ids[] = {
405 { .compatible = "u-boot,bootdev-virtio" },
409 U_BOOT_DRIVER(virtio_bootdev) = {
410 .name = "virtio_bootdev",
411 .id = UCLASS_BOOTDEV,
412 .ops = &virtio_bootdev_ops,
413 .bind = virtio_bootdev_bind,
414 .of_match = virtio_bootdev_ids,
417 BOOTDEV_HUNTER(virtio_bootdev_hunter) = {
418 .prio = BOOTDEVP_4_SCAN_FAST,
419 .uclass = UCLASS_VIRTIO,
420 .hunt = virtio_bootdev_hunt,
421 .drv = DM_DRIVER_REF(virtio_bootdev),