blob: 91af412ec1d6b01f0182e1939d87ad25636bbd7e [file] [log] [blame]
Bin Mengdb7ca2e2018-10-15 02:21:00 -07001// 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 Delaunay81313352021-04-27 11:02:19 +020018#define LOG_CATEGORY UCLASS_VIRTIO
19
Bin Mengdb7ca2e2018-10-15 02:21:00 -070020#include <common.h>
Simon Glassa4bf8f62023-01-17 10:47:52 -070021#include <bootdev.h>
Bin Mengdb7ca2e2018-10-15 02:21:00 -070022#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060023#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070024#include <malloc.h>
Bin Mengdb7ca2e2018-10-15 02:21:00 -070025#include <virtio_types.h>
26#include <virtio.h>
27#include <dm/lists.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060028#include <linux/bug.h>
Bin Mengdb7ca2e2018-10-15 02:21:00 -070029
30static 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,
Sughosh Ganu081da0a2019-12-29 15:30:14 +053033 [VIRTIO_ID_RNG] = VIRTIO_RNG_DRV_NAME,
Bin Mengdb7ca2e2018-10-15 02:21:00 -070034};
35
36int virtio_get_config(struct udevice *vdev, unsigned int offset,
37 void *buf, unsigned int len)
38{
39 struct dm_virtio_ops *ops;
40
41 ops = virtio_get_ops(vdev->parent);
42
43 return ops->get_config(vdev->parent, offset, buf, len);
44}
45
46int virtio_set_config(struct udevice *vdev, unsigned int offset,
47 void *buf, unsigned int len)
48{
49 struct dm_virtio_ops *ops;
50
51 ops = virtio_get_ops(vdev->parent);
52
53 return ops->set_config(vdev->parent, offset, buf, len);
54}
55
56int virtio_generation(struct udevice *vdev, u32 *counter)
57{
58 struct dm_virtio_ops *ops;
59
60 ops = virtio_get_ops(vdev->parent);
61 if (!ops->generation)
62 return -ENOSYS;
63
64 return ops->generation(vdev->parent, counter);
65}
66
67int virtio_get_status(struct udevice *vdev, u8 *status)
68{
69 struct dm_virtio_ops *ops;
70
71 ops = virtio_get_ops(vdev->parent);
72
73 return ops->get_status(vdev->parent, status);
74}
75
76int virtio_set_status(struct udevice *vdev, u8 status)
77{
78 struct dm_virtio_ops *ops;
79
80 ops = virtio_get_ops(vdev->parent);
81
82 return ops->set_status(vdev->parent, status);
83}
84
85int virtio_reset(struct udevice *vdev)
86{
87 struct dm_virtio_ops *ops;
88
89 ops = virtio_get_ops(vdev->parent);
90
91 return ops->reset(vdev->parent);
92}
93
94int virtio_get_features(struct udevice *vdev, u64 *features)
95{
96 struct dm_virtio_ops *ops;
97
98 ops = virtio_get_ops(vdev->parent);
99
100 return ops->get_features(vdev->parent, features);
101}
102
103int virtio_set_features(struct udevice *vdev)
104{
105 struct dm_virtio_ops *ops;
106
107 ops = virtio_get_ops(vdev->parent);
108
109 return ops->set_features(vdev->parent);
110}
111
112int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
113 struct virtqueue *vqs[])
114{
115 struct dm_virtio_ops *ops;
116
117 ops = virtio_get_ops(vdev->parent);
118
119 return ops->find_vqs(vdev->parent, nvqs, vqs);
120}
121
122int virtio_del_vqs(struct udevice *vdev)
123{
124 struct dm_virtio_ops *ops;
125
126 ops = virtio_get_ops(vdev->parent);
127
128 return ops->del_vqs(vdev->parent);
129}
130
131int virtio_notify(struct udevice *vdev, struct virtqueue *vq)
132{
133 struct dm_virtio_ops *ops;
134
135 ops = virtio_get_ops(vdev->parent);
136
137 return ops->notify(vdev->parent, vq);
138}
139
140void virtio_add_status(struct udevice *vdev, u8 status)
141{
142 u8 old;
143
144 if (!virtio_get_status(vdev, &old))
145 virtio_set_status(vdev, old | status);
146}
147
148int virtio_finalize_features(struct udevice *vdev)
149{
150 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
151 u8 status;
152 int ret;
153
154 ret = virtio_set_features(vdev);
155 if (ret)
156 return ret;
157
158 if (uc_priv->legacy)
159 return 0;
160
161 virtio_add_status(vdev, VIRTIO_CONFIG_S_FEATURES_OK);
162 ret = virtio_get_status(vdev, &status);
163 if (ret)
164 return ret;
165 if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
166 debug("(%s): device refuses features %x\n", vdev->name, status);
Simon Glassd47ec042023-01-17 10:47:49 -0700167 return -EINVAL;
Bin Mengdb7ca2e2018-10-15 02:21:00 -0700168 }
169
170 return 0;
171}
172
173void virtio_driver_features_init(struct virtio_dev_priv *priv,
174 const u32 *feature,
175 u32 feature_size,
176 const u32 *feature_legacy,
177 u32 feature_legacy_size)
178{
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;
183}
184
185int virtio_init(void)
186{
Bin Mengdb7ca2e2018-10-15 02:21:00 -0700187 /* Enumerate all known virtio devices */
Michal Suchanek44322b52022-10-12 21:57:51 +0200188 return uclass_probe_all(UCLASS_VIRTIO);
Bin Mengdb7ca2e2018-10-15 02:21:00 -0700189}
190
191static int virtio_uclass_pre_probe(struct udevice *udev)
192{
193 struct dm_virtio_ops *ops;
194
195 ops = (struct dm_virtio_ops *)(udev->driver->ops);
196
197 /*
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.
200 *
201 * Only generation op is optional. All other ops are must-have.
202 */
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)
208 return -ENOENT;
209
210 return 0;
211}
212
213static int virtio_uclass_post_probe(struct udevice *udev)
214{
215 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
216 char dev_name[30], *str;
217 struct udevice *vdev;
Simon Glassddceecf2023-01-17 10:47:48 -0700218 const char *name;
Bin Mengdb7ca2e2018-10-15 02:21:00 -0700219 int ret;
220
Vincent Stehlé08dd6152021-02-14 19:39:04 +0100221 if (uc_priv->device >= VIRTIO_ID_MAX_NUM) {
Bin Mengdb7ca2e2018-10-15 02:21:00 -0700222 debug("(%s): virtio device ID %d exceeds maximum num\n",
223 udev->name, uc_priv->device);
224 return 0;
225 }
226
Simon Glassddceecf2023-01-17 10:47:48 -0700227 name = virtio_drv_name[uc_priv->device];
228 if (!name) {
Bin Mengdb7ca2e2018-10-15 02:21:00 -0700229 debug("(%s): underlying virtio device driver unavailable\n",
230 udev->name);
231 return 0;
232 }
233
Simon Glassddceecf2023-01-17 10:47:48 -0700234 snprintf(dev_name, sizeof(dev_name), "%s#%d", name, dev_seq(udev));
Bin Mengdb7ca2e2018-10-15 02:21:00 -0700235 str = strdup(dev_name);
236 if (!str)
237 return -ENOMEM;
238
Simon Glassddceecf2023-01-17 10:47:48 -0700239 ret = device_bind_driver(udev, name, str, &vdev);
Bin Mengdb7ca2e2018-10-15 02:21:00 -0700240 if (ret == -ENOENT) {
241 debug("(%s): no driver configured\n", udev->name);
242 return 0;
243 }
244 if (ret) {
245 free(str);
246 return ret;
247 }
248 device_set_name_alloced(vdev);
249
Simon Glassa4bf8f62023-01-17 10:47:52 -0700250 if (uc_priv->device == VIRTIO_ID_BLOCK) {
251 ret = bootdev_setup_for_dev(udev, name);
252 if (ret)
253 return log_msg_ret("bootdev", ret);
254 }
255
Bin Mengdb7ca2e2018-10-15 02:21:00 -0700256 INIT_LIST_HEAD(&uc_priv->vqs);
257
258 return 0;
259}
260
261static int virtio_uclass_child_post_bind(struct udevice *vdev)
262{
263 /* Acknowledge that we've seen the device */
264 virtio_add_status(vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
265
266 return 0;
267}
268
269static int virtio_uclass_child_pre_probe(struct udevice *vdev)
270{
271 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
272 u64 device_features;
273 u64 driver_features;
274 u64 driver_features_legacy;
275 int i;
276 int ret;
277
278 /*
279 * Save the real virtio device (eg: virtio-net, virtio-blk) to
280 * the transport (parent) device's uclass priv for future use.
281 */
282 uc_priv->vdev = vdev;
283
284 /*
285 * We always start by resetting the device, in case a previous driver
286 * messed it up. This also tests that code path a little.
287 */
288 ret = virtio_reset(vdev);
289 if (ret)
290 goto err;
291
292 /* We have a driver! */
293 virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER);
294
295 /* Figure out what features the device supports */
296 virtio_get_features(vdev, &device_features);
297 debug("(%s) plain device features supported %016llx\n",
298 vdev->name, device_features);
299 if (!(device_features & (1ULL << VIRTIO_F_VERSION_1)))
300 uc_priv->legacy = true;
301
302 /* Figure out what features the driver supports */
303 driver_features = 0;
304 for (i = 0; i < uc_priv->feature_table_size; i++) {
305 unsigned int f = uc_priv->feature_table[i];
306
307 WARN_ON(f >= 64);
308 driver_features |= (1ULL << f);
309 }
310
311 /* Some drivers have a separate feature table for virtio v1.0 */
312 if (uc_priv->feature_table_legacy) {
313 driver_features_legacy = 0;
314 for (i = 0; i < uc_priv->feature_table_size_legacy; i++) {
315 unsigned int f = uc_priv->feature_table_legacy[i];
316
317 WARN_ON(f >= 64);
318 driver_features_legacy |= (1ULL << f);
319 }
320 } else {
321 driver_features_legacy = driver_features;
322 }
323
324 if (uc_priv->legacy) {
325 debug("(%s): legacy virtio device\n", vdev->name);
326 uc_priv->features = driver_features_legacy & device_features;
327 } else {
328 debug("(%s): v1.0 complaint virtio device\n", vdev->name);
329 uc_priv->features = driver_features & device_features;
330 }
331
332 /* Transport features always preserved to pass to finalize_features */
333 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
334 if ((device_features & (1ULL << i)) &&
335 (i == VIRTIO_F_VERSION_1))
336 __virtio_set_bit(vdev->parent, i);
337
338 debug("(%s) final negotiated features supported %016llx\n",
339 vdev->name, uc_priv->features);
340 ret = virtio_finalize_features(vdev);
341 if (ret)
342 goto err;
343
344 return 0;
345
346err:
347 virtio_add_status(vdev, VIRTIO_CONFIG_S_FAILED);
348 return ret;
349}
350
351static int virtio_uclass_child_post_probe(struct udevice *vdev)
352{
353 /* Indicates that the driver is set up and ready to drive the device */
354 virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER_OK);
355
356 return 0;
357}
358
Simon Glassa4bf8f62023-01-17 10:47:52 -0700359static int virtio_bootdev_bind(struct udevice *dev)
360{
361 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
362
Simon Glass7e1f6a42023-01-17 10:48:08 -0700363 ucp->prio = BOOTDEVP_4_SCAN_FAST;
Simon Glassa4bf8f62023-01-17 10:47:52 -0700364
365 return 0;
366}
367
368static int virtio_bootdev_hunt(struct bootdev_hunter *info, bool show)
369{
370 int ret;
371
372 ret = uclass_probe_all(UCLASS_VIRTIO);
373 if (ret && ret != -ENOENT)
374 return log_msg_ret("vir", ret);
375
376 return 0;
377}
378
Bin Mengdb7ca2e2018-10-15 02:21:00 -0700379UCLASS_DRIVER(virtio) = {
380 .name = "virtio",
381 .id = UCLASS_VIRTIO,
382 .flags = DM_UC_FLAG_SEQ_ALIAS,
383 .pre_probe = virtio_uclass_pre_probe,
384 .post_probe = virtio_uclass_post_probe,
385 .child_post_bind = virtio_uclass_child_post_bind,
386 .child_pre_probe = virtio_uclass_child_pre_probe,
387 .child_post_probe = virtio_uclass_child_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700388 .per_device_auto = sizeof(struct virtio_dev_priv),
Bin Mengdb7ca2e2018-10-15 02:21:00 -0700389};
Simon Glassa4bf8f62023-01-17 10:47:52 -0700390
391struct bootdev_ops virtio_bootdev_ops = {
392};
393
394static const struct udevice_id virtio_bootdev_ids[] = {
395 { .compatible = "u-boot,bootdev-virtio" },
396 { }
397};
398
399U_BOOT_DRIVER(virtio_bootdev) = {
400 .name = "virtio_bootdev",
401 .id = UCLASS_BOOTDEV,
402 .ops = &virtio_bootdev_ops,
403 .bind = virtio_bootdev_bind,
404 .of_match = virtio_bootdev_ids,
405};
406
407BOOTDEV_HUNTER(virtio_bootdev_hunter) = {
Simon Glass7e1f6a42023-01-17 10:48:08 -0700408 .prio = BOOTDEVP_4_SCAN_FAST,
Simon Glassa4bf8f62023-01-17 10:47:52 -0700409 .uclass = UCLASS_VIRTIO,
410 .hunt = virtio_bootdev_hunt,
411 .drv = DM_DRIVER_REF(virtio_bootdev),
412};