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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
|
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-02-25 GuEe-GUI the first version
*/
#include <rtthread.h>
#define DBG_TAG "virtio.mmio"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#include <mmu.h>
#include <drivers/ofw.h>
#include <drivers/pic.h>
#include <drivers/virtio.h>
#include <drivers/platform.h>
#include <drivers/core/bus.h>
#define VIRTIO_MMIO_MAGIC 0x000 /* <RO> Magic value */
#define VIRTIO_MMIO_VERSION 0x004 /* <RO> Device version number */
#define VIRTIO_MMIO_DEVICE_ID 0x008 /* <RO> Virtio Subsystem Device ID */
#define VIRTIO_MMIO_VENDOR_ID 0x00c /* <RO> Virtio Subsystem Vendor ID */
#define VIRTIO_MMIO_DEVICE_FEATURES 0x010 /* <RO> Flags representing features the device supports */
#define VIRTIO_MMIO_DEVICE_FEATURES_SEL 0x014 /* <WO> Device (host) features word selection. */
#define VIRTIO_MMIO_DRIVER_FEATURES 0x020 /* <WO> Device features understood and activated by the driver */
#define VIRTIO_MMIO_DRIVER_FEATURES_SEL 0x024 /* <WO> Activated (guest) features word selection */
#define VIRTIO_MMIO_GUEST_PAGE_SIZE 0x028 /* <WO> Guest page size, this value should be a power of 2 */
#define VIRTIO_MMIO_QUEUE_SEL 0x030 /* <WO> Virtual queue index */
#define VIRTIO_MMIO_QUEUE_NUM_MAX 0x034 /* <RO> Maximum virtual queue size */
#define VIRTIO_MMIO_QUEUE_NUM 0x038 /* <WO> Virtual queue size */
#define VIRTIO_MMIO_QUEUE_ALIGN 0x03c /* <WO> Used Ring alignment in the virtual queue */
#define VIRTIO_MMIO_QUEUE_PFN 0x040 /* <RW> Guest physical page number of the virtual queue */
#define VIRTIO_MMIO_QUEUE_READY 0x044 /* <RW> Virtual queue ready bit */
#define VIRTIO_MMIO_QUEUE_NOTIFY 0x050 /* <WO> Queue notifier */
#define VIRTIO_MMIO_INTERRUPT_STATUS 0x060 /* <RO> Interrupt status */
#define VIRTIO_MMIO_INTERRUPT_ACK 0x064 /* <WO> Interrupt acknowledge */
#define VIRTIO_MMIO_STATUS 0x070 /* <RW> Device status */
#define VIRTIO_MMIO_QUEUE_DESC_LOW 0x080 /* <WO> Virtual queue's descriptor Area 64 bit long physical address */
#define VIRTIO_MMIO_QUEUE_DESC_HIGH 0x084 /* <WO> */
#define VIRTIO_MMIO_QUEUE_AVAIL_LOW 0x090 /* <WO> Virtual queue's available Ring (Driver Area) 64 bit long physical address */
#define VIRTIO_MMIO_QUEUE_AVAIL_HIGH 0x094 /* <WO> */
#define VIRTIO_MMIO_QUEUE_USED_LOW 0x0a0 /* <WO> Virtual queue's used Ring (Device Area) 64 bit long physical address */
#define VIRTIO_MMIO_QUEUE_USED_HIGH 0x0a4 /* <WO> */
#define VIRTIO_MMIO_SHM_SEL 0x0ac /* <WO> Shared memory region id */
#define VIRTIO_MMIO_SHM_LEN_LOW 0x0b0 /* <RO> Shared memory region length, 64 bits in two halves */
#define VIRTIO_MMIO_SHM_LEN_HIGH 0x0b4 /* <RO> */
#define VIRTIO_MMIO_SHM_BASE_LOW 0x0b8 /* <RO> Shared memory region base address, 64 bits in two halves */
#define VIRTIO_MMIO_SHM_BASE_HIGH 0x0bc /* <RO> */
#define VIRTIO_MMIO_CONFIG_GENERATION 0x0fc /* <RO> Configuration atomicity value */
#define VIRTIO_MMIO_CONFIG 0x100 /* <RO> Configuration space */
#define VIRTIO_MMIO_INT_VIRTQ (1 << 0) /* The device has used a buffer in at least one of the active virtual queues */
#define VIRTIO_MMIO_INT_CONFIG (1 << 1) /* The configuration of the device has changed */
#define VIRTIO_MMIO_VIRTQ_PAGE_SHIFT ARCH_PAGE_SHIFT
#define VIRTIO_MMIO_VIRTQ_PAGE_SZIE (1 << VIRTIO_MMIO_VIRTQ_PAGE_SHIFT)
#define VIRTIO_MMIO_VIRTQ_ALIGN_SHIFT VIRTIO_MMIO_VIRTQ_PAGE_SHIFT
#define VIRTIO_MMIO_VIRTQ_ALIGN (1 << VIRTIO_MMIO_VIRTQ_ALIGN_SHIFT)
struct virtio_mmio
{
struct rt_virtio_device parent;
void *base;
int irq;
rt_ubase_t version;
struct rt_spinlock spinlock;
};
#define raw_to_virtio_mmio(raw) rt_container_of(raw, struct virtio_mmio, parent)
rt_inline rt_uint64_t virtio_mmio_read64(struct virtio_mmio *vio, int offset)
{
return rt_cpu_to_le64(HWREG64(vio->base + offset));
}
rt_inline void virtio_mmio_write64(struct virtio_mmio *vio, int offset, rt_uint64_t value)
{
HWREG64(vio->base + offset) = rt_le64_to_cpu(value);
}
rt_inline rt_uint32_t virtio_mmio_read32(struct virtio_mmio *vio, int offset)
{
return rt_cpu_to_le32(HWREG32(vio->base + offset));
}
rt_inline void virtio_mmio_write32(struct virtio_mmio *vio, int offset, rt_uint32_t value)
{
HWREG32(vio->base + offset) = rt_le32_to_cpu(value);
}
rt_inline rt_uint16_t virtio_mmio_read16(struct virtio_mmio *vio, int offset)
{
return rt_cpu_to_le16(HWREG16(vio->base + offset));
}
rt_inline void virtio_mmio_write16(struct virtio_mmio *vio, int offset, rt_uint16_t value)
{
HWREG16(vio->base + offset) = rt_le16_to_cpu(value);
}
rt_inline rt_uint8_t virtio_mmio_read8(struct virtio_mmio *vio, int offset)
{
return HWREG8(vio->base + offset);
}
rt_inline void virtio_mmio_write8(struct virtio_mmio *vio, int offset, rt_uint8_t value)
{
HWREG8(vio->base + offset) = value;
}
static void virtio_mmio_isr(int irqno, void *param)
{
rt_uint32_t status;
struct virtio_mmio *vio = param;
/* Read interrupts */
status = virtio_mmio_read32(vio, VIRTIO_MMIO_INTERRUPT_STATUS);
/* Acknowledge interrupts */
virtio_mmio_write32(vio, VIRTIO_MMIO_INTERRUPT_ACK, status);
if (status & VIRTIO_MMIO_INT_CONFIG)
{
rt_virtio_device_config_changed(&vio->parent);
}
if (status & VIRTIO_MMIO_INT_VIRTQ)
{
struct rt_virtqueue *vq;
rt_ubase_t level = rt_spin_lock_irqsave(&vio->spinlock);
rt_list_for_each_entry(vq, &vio->parent.vq_node, list)
{
rt_virtqueue_isr(irqno, vq);
}
rt_spin_unlock_irqrestore(&vio->spinlock, level);
}
}
static rt_bool_t virtio_mmio_notify(struct rt_virtqueue *vq)
{
struct virtio_mmio *vio = raw_to_virtio_mmio(vq->vdev);
virtio_mmio_write32(vio, VIRTIO_MMIO_QUEUE_NOTIFY, vq->index);
return RT_TRUE;
}
static rt_err_t virtio_mmio_read_shm_region(struct virtio_mmio *vio,
struct rt_virtio_shm_region *shm)
{
virtio_mmio_write32(vio, VIRTIO_MMIO_SHM_SEL, shm->id);
shm->size = virtio_mmio_read32(vio, VIRTIO_MMIO_SHM_LEN_HIGH);
shm->size <<= 32;
shm->size |= virtio_mmio_read32(vio, VIRTIO_MMIO_SHM_LEN_LOW);
if (shm->size == ~(rt_uint64_t)0UL)
{
return -RT_EEMPTY;
}
shm->base = virtio_mmio_read32(vio, VIRTIO_MMIO_SHM_BASE_HIGH);
shm->base <<= 32;
shm->base |= virtio_mmio_read32(vio, VIRTIO_MMIO_SHM_BASE_LOW);
return RT_EOK;
}
static rt_err_t virtio_mmio_get_status(struct rt_virtio_device *vdev,
rt_uint8_t *out_status)
{
struct virtio_mmio *vio = raw_to_virtio_mmio(vdev);
*out_status = virtio_mmio_read32(vio, VIRTIO_MMIO_STATUS);
return RT_EOK;
}
static rt_err_t virtio_mmio_set_status(struct rt_virtio_device *vdev,
rt_uint8_t status)
{
struct virtio_mmio *vio = raw_to_virtio_mmio(vdev);
RT_ASSERT(status != 0);
virtio_mmio_write32(vio, VIRTIO_MMIO_STATUS, status);
return RT_EOK;
}
static rt_err_t virtio_mmio_get_features(struct rt_virtio_device *vdev,
rt_uint64_t *out_features)
{
rt_uint64_t features;
struct virtio_mmio *vio = raw_to_virtio_mmio(vdev);
virtio_mmio_write32(vio, VIRTIO_MMIO_DEVICE_FEATURES_SEL, 1);
features = virtio_mmio_read32(vio, VIRTIO_MMIO_DEVICE_FEATURES);
features <<= 32;
virtio_mmio_write32(vio, VIRTIO_MMIO_DEVICE_FEATURES_SEL, 0);
features |= virtio_mmio_read32(vio, VIRTIO_MMIO_DEVICE_FEATURES);
*out_features = features;
return RT_EOK;
}
static rt_err_t virtio_mmio_set_features(struct rt_virtio_device *vdev)
{
struct virtio_mmio *vio = raw_to_virtio_mmio(vdev);
/* Make sure there are no mixed devices */
if (vio->version == 2 && !(vdev->features & RT_BIT(VIRTIO_F_VERSION_1)))
{
LOG_E("%s devices (version 2) must provide VIRTIO_F_VERSION_1 feature",
rt_dm_dev_get_name(&vdev->parent));
return -RT_EINVAL;
}
virtio_mmio_write32(vio, VIRTIO_MMIO_DRIVER_FEATURES_SEL, 1);
virtio_mmio_write32(vio, VIRTIO_MMIO_DRIVER_FEATURES,
(rt_uint32_t)(vdev->features >> 32));
virtio_mmio_write32(vio, VIRTIO_MMIO_DRIVER_FEATURES_SEL, 0);
virtio_mmio_write32(vio, VIRTIO_MMIO_DRIVER_FEATURES,
(rt_uint32_t)vdev->features);
return RT_EOK;
}
static rt_err_t virtio_mmio_get_config(struct rt_virtio_device *vdev,
rt_uint32_t offset, void *dst, int length)
{
struct virtio_mmio *vio = raw_to_virtio_mmio(vdev);
offset += VIRTIO_MMIO_CONFIG;
if (vio->version == 1)
{
for (int i = 0; i < length; ++i)
{
((rt_uint8_t *)dst)[i] = virtio_mmio_read8(vio, offset + i);
}
return RT_EOK;
}
switch (length)
{
case 4:
*(rt_uint32_t *)dst = virtio_mmio_read32(vio, offset);
break;
case 1:
*(rt_uint8_t *)dst = virtio_mmio_read8(vio, offset);
break;
case 2:
*(rt_uint16_t *)dst = virtio_mmio_read16(vio, offset);
break;
case 8:
*(rt_uint64_t *)dst = virtio_mmio_read64(vio, offset);
break;
default:
return -RT_EINVAL;
}
return RT_EOK;
}
static rt_err_t virtio_mmio_set_config(struct rt_virtio_device *vdev,
rt_uint32_t offset, const void *src, int length)
{
struct virtio_mmio *vio = raw_to_virtio_mmio(vdev);
offset += VIRTIO_MMIO_CONFIG;
if (vio->version == 1)
{
for (int i = 0; i < length; ++i)
{
virtio_mmio_write8(vio, offset + i, ((rt_uint8_t *)src)[i]);
}
return RT_EOK;
}
switch (length)
{
case 4:
virtio_mmio_write32(vio, offset, *(rt_uint32_t *)src);
break;
case 1:
virtio_mmio_write8(vio, offset, *(rt_uint8_t *)src);
break;
case 2:
virtio_mmio_write16(vio, offset, *(rt_uint16_t *)src);
break;
case 8:
virtio_mmio_write64(vio, offset, *(rt_uint64_t *)src);
break;
default:
return -RT_EINVAL;
}
return RT_EOK;
}
static struct rt_virtqueue *virtio_mmio_install_vq(struct rt_virtio_device *vdev,
int index, const char *name, rt_virtqueue_callback cb,
struct rt_virtqueue_formula *formula)
{
rt_uint32_t num_max;
struct rt_virtqueue *vq;
struct virtio_mmio *vio = raw_to_virtio_mmio(vdev);
virtio_mmio_write32(vio, VIRTIO_MMIO_QUEUE_SEL, index);
if (virtio_mmio_read32(vio, vio->version == 1 ?
VIRTIO_MMIO_QUEUE_PFN : VIRTIO_MMIO_QUEUE_READY))
{
return RT_NULL;
}
num_max = virtio_mmio_read32(vio, VIRTIO_MMIO_QUEUE_NUM_MAX);
if (num_max == 0)
{
LOG_E("%s.virtqueue[%d](%s) num_max is zero",
rt_dm_dev_get_name(&vdev->parent), index, name);
return RT_NULL;
}
vq = rt_virtqueue_create(vdev, name, index, num_max, VIRTIO_MMIO_VIRTQ_ALIGN,
virtio_mmio_notify, cb, formula);
if (!vq)
{
return vq;
}
vq->num_max = num_max;
/* Activate the queue */
virtio_mmio_write32(vio, VIRTIO_MMIO_QUEUE_NUM, rt_virtqueue_get_virtq_size(vq));
if (vio->version == 1)
{
rt_uint32_t pfn = (rt_ubase_t)rt_kmem_v2p((void *)rt_virtqueue_get_desc_addr(vq));
pfn >>= VIRTIO_MMIO_VIRTQ_ALIGN_SHIFT;
virtio_mmio_write32(vio, VIRTIO_MMIO_QUEUE_ALIGN, VIRTIO_MMIO_VIRTQ_ALIGN);
virtio_mmio_write32(vio, VIRTIO_MMIO_QUEUE_PFN, pfn);
}
else
{
rt_uint64_t page;
page = (rt_ubase_t)rt_kmem_v2p((void *)rt_virtqueue_get_desc_addr(vq));
virtio_mmio_write32(vio, VIRTIO_MMIO_QUEUE_DESC_LOW, (rt_uint32_t)page);
virtio_mmio_write32(vio, VIRTIO_MMIO_QUEUE_DESC_HIGH, (rt_uint32_t)(page >> 32));
page = (rt_ubase_t)rt_kmem_v2p((void *)rt_virtqueue_get_avail_addr(vq));
virtio_mmio_write32(vio, VIRTIO_MMIO_QUEUE_AVAIL_LOW, (rt_uint32_t)page);
virtio_mmio_write32(vio, VIRTIO_MMIO_QUEUE_AVAIL_HIGH, (rt_uint32_t)(page >> 32));
page = (rt_ubase_t)rt_kmem_v2p((void *)rt_virtqueue_get_used_addr(vq));
virtio_mmio_write32(vio, VIRTIO_MMIO_QUEUE_USED_LOW, (rt_uint32_t)page);
virtio_mmio_write32(vio, VIRTIO_MMIO_QUEUE_USED_HIGH, (rt_uint32_t)(page >> 32));
virtio_mmio_write32(vio, VIRTIO_MMIO_QUEUE_READY, 1);
}
return vq;
}
static rt_err_t virtio_mmio_install_vqs(struct rt_virtio_device *vdev, int vqs_nr,
struct rt_virtqueue *vqs[], const char *names[], rt_virtqueue_callback cbs[])
{
struct virtio_mmio *vio = raw_to_virtio_mmio(vdev);
for (int i = 0, vq_idx = 0; i < vqs_nr; ++i)
{
if (!names[i])
{
vqs[i] = RT_NULL;
continue;
}
vqs[i] = virtio_mmio_install_vq(vdev, vq_idx++, names[i], cbs[i], RT_NULL);
if (!vqs[i])
{
rt_virtio_virtqueue_release(vdev);
return -RT_ERROR;
}
}
rt_hw_interrupt_install(vio->irq, virtio_mmio_isr, vio,
rt_dm_dev_get_name(&vdev->parent));
rt_hw_interrupt_umask(vio->irq);
return RT_EOK;
}
static rt_err_t virtio_mmio_release_vqs(struct rt_virtio_device *vdev)
{
struct rt_virtqueue *vq, *vq_next;
struct virtio_mmio *vio = raw_to_virtio_mmio(vdev);
rt_hw_interrupt_mask(vio->irq);
rt_pic_detach_irq(vio->irq, vio);
rt_list_for_each_entry_safe(vq, vq_next, &vio->parent.vq_node, list)
{
virtio_mmio_write32(vio, VIRTIO_MMIO_QUEUE_SEL, vq->index);
if (vio->version == 1)
{
virtio_mmio_write32(vio, VIRTIO_MMIO_QUEUE_PFN, RT_NULL);
}
else
{
rt_uint32_t status;
virtio_mmio_write32(vio, VIRTIO_MMIO_QUEUE_READY, RT_NULL);
status = virtio_mmio_read32(vio, VIRTIO_MMIO_QUEUE_READY);
if (!status)
{
LOG_W("%s.virtqueue[%s] VIRTIO_MMIO_QUEUE_READY = %d",
rt_dm_dev_get_name(&vq->vdev->parent), vq->name, status);
}
}
rt_virtqueue_delete(&vio->parent, vq);
}
return RT_EOK;
}
static rt_err_t virtio_mmio_control_vqs(struct rt_virtio_device *vdev,
rt_uint32_t cfg, void *data)
{
rt_err_t err = RT_EOK;
struct virtio_mmio *vio = raw_to_virtio_mmio(vdev);
switch (cfg)
{
case RT_VIRTQUEUE_CTL_IRQ_AFFINITY:
err = rt_pic_irq_set_affinity(vio->irq, (rt_bitmap_t *)data);
break;
case RT_VIRTQUEUE_CTL_READ_SHM_REGION:
if (!data)
{
err = -RT_EINVAL;
break;
}
err = virtio_mmio_read_shm_region(vio, data);
break;
default:
err = -RT_ENOSYS;
break;
}
return err;
}
static rt_err_t virtio_mmio_generation(struct rt_virtio_device *vdev,
rt_uint32_t *out_counter)
{
struct virtio_mmio *vio = raw_to_virtio_mmio(vdev);
if (vio->version == 1)
{
*out_counter = 0;
}
else
{
*out_counter = virtio_mmio_read32(vio, VIRTIO_MMIO_CONFIG_GENERATION);
}
return RT_EOK;
}
static rt_err_t virtio_mmio_reset(struct rt_virtio_device *vdev)
{
struct virtio_mmio *vio = raw_to_virtio_mmio(vdev);
virtio_mmio_write32(vio, VIRTIO_MMIO_STATUS, 0);
return RT_EOK;
}
static const struct rt_virtio_transport virtio_mmio_trans =
{
.get_status = virtio_mmio_get_status,
.set_status = virtio_mmio_set_status,
.get_features = virtio_mmio_get_features,
.set_features = virtio_mmio_set_features,
.get_config = virtio_mmio_get_config,
.set_config = virtio_mmio_set_config,
.install_vqs = virtio_mmio_install_vqs,
.release_vqs = virtio_mmio_release_vqs,
.control_vqs = virtio_mmio_control_vqs,
.generation = virtio_mmio_generation,
.reset = virtio_mmio_reset,
};
static rt_err_t virtio_mmio_probe(struct rt_platform_device *pdev)
{
rt_err_t err = RT_EOK;
rt_uint32_t magic;
struct rt_virtio_device *vdev;
struct rt_device *dev = &pdev->parent;
struct virtio_mmio *vio = rt_calloc(1, sizeof(*vio));
if (!vio)
{
return -RT_ENOMEM;
}
vio->base = rt_dm_dev_iomap(dev, 0);
if (!vio->base)
{
err = -RT_EIO;
goto _fail;
}
vio->irq = rt_dm_dev_get_irq(dev, 0);
if (vio->irq < 0)
{
err = vio->irq;
goto _fail;
}
magic = virtio_mmio_read32(vio, VIRTIO_MMIO_MAGIC);
/* 0x74726976 (a Little Endian equivalent of the "virt" string). */
if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24))
{
err = -RT_EINVAL;
LOG_E("Invalid magic: %x", magic);
goto _fail;
}
vio->version = virtio_mmio_read32(vio, VIRTIO_MMIO_VERSION);
/* Only support version 1~2 */
if (vio->version < 1 || vio->version > 2)
{
err = -RT_ENOSYS;
LOG_E("Not support version: %d", vio->version);
goto _fail;
}
vdev = &vio->parent;
vdev->id.device = virtio_mmio_read32(vio, VIRTIO_MMIO_DEVICE_ID);
if (vdev->id.device == VIRTIO_DEVICE_ID_INVALID)
{
err = -RT_EEMPTY;
goto _fail;
}
vdev->id.vendor = virtio_mmio_read32(vio, VIRTIO_MMIO_VENDOR_ID);
if (vio->version == 1)
{
virtio_mmio_write32(vio, VIRTIO_MMIO_GUEST_PAGE_SIZE, VIRTIO_MMIO_VIRTQ_PAGE_SZIE);
}
rt_spin_lock_init(&vio->spinlock);
vdev->trans = &virtio_mmio_trans;
vdev->parent.ofw_node = dev->ofw_node;
if ((err = rt_virtio_device_register(vdev)))
{
goto _fail;
}
dev->user_data = vio;
return RT_EOK;
_fail:
if (vio->base)
{
rt_iounmap(vio->base);
}
rt_free(vio);
return err;
}
static rt_err_t virtio_mmio_remove(struct rt_platform_device *pdev)
{
struct virtio_mmio *vio = pdev->parent.user_data;
rt_hw_interrupt_mask(vio->irq);
rt_pic_detach_irq(vio->irq, vio);
rt_bus_remove_device(&vio->parent.parent);
rt_iounmap(vio->base);
rt_free(vio);
return RT_EOK;
}
static const struct rt_ofw_node_id virtio_mmio_ofw_ids[] =
{
{ .compatible = "virtio,mmio" },
{ /* sentinel */ }
};
static struct rt_platform_driver virtio_mmio_driver =
{
.name = "virtio-mmio",
.ids = virtio_mmio_ofw_ids,
.probe = virtio_mmio_probe,
.remove = virtio_mmio_remove,
};
RT_PLATFORM_DRIVER_EXPORT(virtio_mmio_driver);
|