1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
* Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
*
* VirtIO is a virtualization standard for network and disk device drivers
* where just the guest's device driver "knows" it is running in a virtual
* environment, and cooperates with the hypervisor. This enables guests to
* get high performance network and disk operations, and gives most of the
* performance benefits of paravirtualization. In the U-Boot case, the guest
* is U-Boot itself, while the virtual environment are normally QEMU targets
* like ARM, RISC-V and x86.
*
* See http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf for
* the VirtIO specification v1.0.
*/
#define LOG_CATEGORY UCLASS_VIRTIO
#include <common.h>
#include <bootdev.h>
#include <dm.h>
#include <log.h>
#include <malloc.h>
#include <virtio_types.h>
#include <virtio.h>
#include <dm/lists.h>
#include <linux/bug.h>
static const char *const virtio_drv_name[VIRTIO_ID_MAX_NUM] = {
[VIRTIO_ID_NET] = VIRTIO_NET_DRV_NAME,
[VIRTIO_ID_BLOCK] = VIRTIO_BLK_DRV_NAME,
[VIRTIO_ID_RNG] = VIRTIO_RNG_DRV_NAME,
};
int virtio_get_config(struct udevice *vdev, unsigned int offset,
void *buf, unsigned int len)
{
struct dm_virtio_ops *ops;
ops = virtio_get_ops(vdev->parent);
return ops->get_config(vdev->parent, offset, buf, len);
}
int virtio_set_config(struct udevice *vdev, unsigned int offset,
void *buf, unsigned int len)
{
struct dm_virtio_ops *ops;
ops = virtio_get_ops(vdev->parent);
return ops->set_config(vdev->parent, offset, buf, len);
}
int virtio_generation(struct udevice *vdev, u32 *counter)
{
struct dm_virtio_ops *ops;
ops = virtio_get_ops(vdev->parent);
if (!ops->generation)
return -ENOSYS;
return ops->generation(vdev->parent, counter);
}
int virtio_get_status(struct udevice *vdev, u8 *status)
{
struct dm_virtio_ops *ops;
ops = virtio_get_ops(vdev->parent);
return ops->get_status(vdev->parent, status);
}
int virtio_set_status(struct udevice *vdev, u8 status)
{
struct dm_virtio_ops *ops;
ops = virtio_get_ops(vdev->parent);
return ops->set_status(vdev->parent, status);
}
int virtio_reset(struct udevice *vdev)
{
struct dm_virtio_ops *ops;
ops = virtio_get_ops(vdev->parent);
return ops->reset(vdev->parent);
}
int virtio_get_features(struct udevice *vdev, u64 *features)
{
struct dm_virtio_ops *ops;
ops = virtio_get_ops(vdev->parent);
return ops->get_features(vdev->parent, features);
}
int virtio_set_features(struct udevice *vdev)
{
struct dm_virtio_ops *ops;
ops = virtio_get_ops(vdev->parent);
return ops->set_features(vdev->parent);
}
int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
struct virtqueue *vqs[])
{
struct dm_virtio_ops *ops;
ops = virtio_get_ops(vdev->parent);
return ops->find_vqs(vdev->parent, nvqs, vqs);
}
int virtio_del_vqs(struct udevice *vdev)
{
struct dm_virtio_ops *ops;
ops = virtio_get_ops(vdev->parent);
return ops->del_vqs(vdev->parent);
}
int virtio_notify(struct udevice *vdev, struct virtqueue *vq)
{
struct dm_virtio_ops *ops;
ops = virtio_get_ops(vdev->parent);
return ops->notify(vdev->parent, vq);
}
void virtio_add_status(struct udevice *vdev, u8 status)
{
u8 old;
if (!virtio_get_status(vdev, &old))
virtio_set_status(vdev, old | status);
}
int virtio_finalize_features(struct udevice *vdev)
{
struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
u8 status;
int ret;
ret = virtio_set_features(vdev);
if (ret)
return ret;
if (uc_priv->legacy)
return 0;
virtio_add_status(vdev, VIRTIO_CONFIG_S_FEATURES_OK);
ret = virtio_get_status(vdev, &status);
if (ret)
return ret;
if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
debug("(%s): device refuses features %x\n", vdev->name, status);
return -EINVAL;
}
return 0;
}
void virtio_driver_features_init(struct virtio_dev_priv *priv,
const u32 *feature,
u32 feature_size,
const u32 *feature_legacy,
u32 feature_legacy_size)
{
priv->feature_table = feature;
priv->feature_table_size = feature_size;
priv->feature_table_legacy = feature_legacy;
priv->feature_table_size_legacy = feature_legacy_size;
}
int virtio_init(void)
{
/* Enumerate all known virtio devices */
return uclass_probe_all(UCLASS_VIRTIO);
}
static int virtio_uclass_pre_probe(struct udevice *udev)
{
struct dm_virtio_ops *ops;
ops = (struct dm_virtio_ops *)(udev->driver->ops);
/*
* Check virtio transport driver ops here so that we don't need
* check these ops each time when the virtio_xxx APIs are called.
*
* Only generation op is optional. All other ops are must-have.
*/
if (!ops->get_config || !ops->set_config ||
!ops->get_status || !ops->set_status ||
!ops->get_features || !ops->set_features ||
!ops->find_vqs || !ops->del_vqs ||
!ops->reset || !ops->notify)
return -ENOENT;
return 0;
}
static int virtio_uclass_post_probe(struct udevice *udev)
{
struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
char dev_name[30], *str;
struct udevice *vdev;
const char *name;
int ret;
if (uc_priv->device >= VIRTIO_ID_MAX_NUM) {
debug("(%s): virtio device ID %d exceeds maximum num\n",
udev->name, uc_priv->device);
return 0;
}
name = virtio_drv_name[uc_priv->device];
if (!name) {
debug("(%s): underlying virtio device driver unavailable\n",
udev->name);
return 0;
}
snprintf(dev_name, sizeof(dev_name), "%s#%d", name, dev_seq(udev));
str = strdup(dev_name);
if (!str)
return -ENOMEM;
ret = device_bind_driver(udev, name, str, &vdev);
if (ret == -ENOENT) {
debug("(%s): %s driver not configured\n", udev->name, name);
return 0;
}
if (ret) {
free(str);
return ret;
}
device_set_name_alloced(vdev);
if (uc_priv->device == VIRTIO_ID_BLOCK && !IS_ENABLED(CONFIG_SANDBOX)) {
ret = bootdev_setup_sibling_blk(vdev, "virtio_bootdev");
if (ret)
return log_msg_ret("bootdev", ret);
}
INIT_LIST_HEAD(&uc_priv->vqs);
return 0;
}
static int virtio_uclass_child_post_bind(struct udevice *vdev)
{
/* Acknowledge that we've seen the device */
virtio_add_status(vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
return 0;
}
static int virtio_uclass_child_pre_probe(struct udevice *vdev)
{
struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
u64 device_features;
u64 driver_features;
u64 driver_features_legacy;
int i;
int ret;
/* bootdevs are not virtio devices */
if (device_get_uclass_id(vdev) == UCLASS_BOOTDEV)
return 0;
/*
* Save the real virtio device (eg: virtio-net, virtio-blk) to
* the transport (parent) device's uclass priv for future use.
*/
uc_priv->vdev = vdev;
/*
* We always start by resetting the device, in case a previous driver
* messed it up. This also tests that code path a little.
*/
ret = virtio_reset(vdev);
if (ret)
goto err;
/* We have a driver! */
virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER);
/* Figure out what features the device supports */
virtio_get_features(vdev, &device_features);
debug("(%s) plain device features supported %016llx\n",
vdev->name, device_features);
if (!(device_features & (1ULL << VIRTIO_F_VERSION_1)))
uc_priv->legacy = true;
/* Figure out what features the driver supports */
driver_features = 0;
for (i = 0; i < uc_priv->feature_table_size; i++) {
unsigned int f = uc_priv->feature_table[i];
WARN_ON(f >= 64);
driver_features |= (1ULL << f);
}
/* Some drivers have a separate feature table for virtio v1.0 */
if (uc_priv->feature_table_legacy) {
driver_features_legacy = 0;
for (i = 0; i < uc_priv->feature_table_size_legacy; i++) {
unsigned int f = uc_priv->feature_table_legacy[i];
WARN_ON(f >= 64);
driver_features_legacy |= (1ULL << f);
}
} else {
driver_features_legacy = driver_features;
}
if (uc_priv->legacy) {
debug("(%s): legacy virtio device\n", vdev->name);
uc_priv->features = driver_features_legacy & device_features;
} else {
debug("(%s): v1.0 complaint virtio device\n", vdev->name);
uc_priv->features = driver_features & device_features;
}
/* Transport features always preserved to pass to finalize_features */
for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
if ((device_features & (1ULL << i)) &&
(i == VIRTIO_F_VERSION_1 || i == VIRTIO_F_IOMMU_PLATFORM))
__virtio_set_bit(vdev->parent, i);
debug("(%s) final negotiated features supported %016llx\n",
vdev->name, uc_priv->features);
ret = virtio_finalize_features(vdev);
if (ret)
goto err;
return 0;
err:
virtio_add_status(vdev, VIRTIO_CONFIG_S_FAILED);
return ret;
}
static int virtio_uclass_child_post_probe(struct udevice *vdev)
{
/* Indicates that the driver is set up and ready to drive the device */
virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER_OK);
return 0;
}
static int virtio_bootdev_bind(struct udevice *dev)
{
struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
ucp->prio = BOOTDEVP_4_SCAN_FAST;
return 0;
}
static int virtio_bootdev_hunt(struct bootdev_hunter *info, bool show)
{
int ret;
if (IS_ENABLED(CONFIG_PCI)) {
ret = uclass_probe_all(UCLASS_PCI);
if (ret && ret != -ENOENT)
return log_msg_ret("pci", ret);
}
ret = uclass_probe_all(UCLASS_VIRTIO);
if (ret && ret != -ENOENT)
return log_msg_ret("vir", ret);
return 0;
}
UCLASS_DRIVER(virtio) = {
.name = "virtio",
.id = UCLASS_VIRTIO,
.flags = DM_UC_FLAG_SEQ_ALIAS,
.pre_probe = virtio_uclass_pre_probe,
.post_probe = virtio_uclass_post_probe,
.child_post_bind = virtio_uclass_child_post_bind,
.child_pre_probe = virtio_uclass_child_pre_probe,
.child_post_probe = virtio_uclass_child_post_probe,
.per_device_auto = sizeof(struct virtio_dev_priv),
};
struct bootdev_ops virtio_bootdev_ops = {
};
static const struct udevice_id virtio_bootdev_ids[] = {
{ .compatible = "u-boot,bootdev-virtio" },
{ }
};
U_BOOT_DRIVER(virtio_bootdev) = {
.name = "virtio_bootdev",
.id = UCLASS_BOOTDEV,
.ops = &virtio_bootdev_ops,
.bind = virtio_bootdev_bind,
.of_match = virtio_bootdev_ids,
};
BOOTDEV_HUNTER(virtio_bootdev_hunter) = {
.prio = BOOTDEVP_4_SCAN_FAST,
.uclass = UCLASS_VIRTIO,
.hunt = virtio_bootdev_hunt,
.drv = DM_DRIVER_REF(virtio_bootdev),
};
|