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#define pr_fmt(fmt) "virtio-mmio: " fmt
90
91#include <linux/highmem.h>
92#include <linux/interrupt.h>
93#include <linux/io.h>
94#include <linux/list.h>
95#include <linux/module.h>
96#include <linux/platform_device.h>
97#include <linux/slab.h>
98#include <linux/spinlock.h>
99#include <linux/virtio.h>
100#include <linux/virtio_config.h>
101#include <linux/virtio_mmio.h>
102#include <linux/virtio_ring.h>
103
104
105
106
107
108#define VIRTIO_MMIO_VRING_ALIGN PAGE_SIZE
109
110
111
112#define to_virtio_mmio_device(_plat_dev) \
113 container_of(_plat_dev, struct virtio_mmio_device, vdev)
114
115struct virtio_mmio_device {
116 struct virtio_device vdev;
117 struct platform_device *pdev;
118
119 void __iomem *base;
120 unsigned long version;
121
122
123 spinlock_t lock;
124 struct list_head virtqueues;
125};
126
127struct virtio_mmio_vq_info {
128
129 struct virtqueue *vq;
130
131
132 unsigned int num;
133
134
135 void *queue;
136
137
138 struct list_head node;
139};
140
141
142
143
144
145static u32 vm_get_features(struct virtio_device *vdev)
146{
147 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
148
149
150 writel(0, vm_dev->base + VIRTIO_MMIO_HOST_FEATURES_SEL);
151
152 return readl(vm_dev->base + VIRTIO_MMIO_HOST_FEATURES);
153}
154
155static void vm_finalize_features(struct virtio_device *vdev)
156{
157 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
158 int i;
159
160
161 vring_transport_features(vdev);
162
163 for (i = 0; i < ARRAY_SIZE(vdev->features); i++) {
164 writel(i, vm_dev->base + VIRTIO_MMIO_GUEST_FEATURES_SEL);
165 writel(vdev->features[i],
166 vm_dev->base + VIRTIO_MMIO_GUEST_FEATURES);
167 }
168}
169
170static void vm_get(struct virtio_device *vdev, unsigned offset,
171 void *buf, unsigned len)
172{
173 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
174 u8 *ptr = buf;
175 int i;
176
177 for (i = 0; i < len; i++)
178 ptr[i] = readb(vm_dev->base + VIRTIO_MMIO_CONFIG + offset + i);
179}
180
181static void vm_set(struct virtio_device *vdev, unsigned offset,
182 const void *buf, unsigned len)
183{
184 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
185 const u8 *ptr = buf;
186 int i;
187
188 for (i = 0; i < len; i++)
189 writeb(ptr[i], vm_dev->base + VIRTIO_MMIO_CONFIG + offset + i);
190}
191
192static u8 vm_get_status(struct virtio_device *vdev)
193{
194 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
195
196 return readl(vm_dev->base + VIRTIO_MMIO_STATUS) & 0xff;
197}
198
199static void vm_set_status(struct virtio_device *vdev, u8 status)
200{
201 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
202
203
204 BUG_ON(status == 0);
205
206 writel(status, vm_dev->base + VIRTIO_MMIO_STATUS);
207}
208
209static void vm_reset(struct virtio_device *vdev)
210{
211 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
212
213
214 writel(0, vm_dev->base + VIRTIO_MMIO_STATUS);
215}
216
217
218
219
220
221
222static void vm_notify(struct virtqueue *vq)
223{
224 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
225
226
227
228 writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
229}
230
231
232static irqreturn_t vm_interrupt(int irq, void *opaque)
233{
234 struct virtio_mmio_device *vm_dev = opaque;
235 struct virtio_mmio_vq_info *info;
236 struct virtio_driver *vdrv = container_of(vm_dev->vdev.dev.driver,
237 struct virtio_driver, driver);
238 unsigned long status;
239 unsigned long flags;
240 irqreturn_t ret = IRQ_NONE;
241
242
243 status = readl(vm_dev->base + VIRTIO_MMIO_INTERRUPT_STATUS);
244 writel(status, vm_dev->base + VIRTIO_MMIO_INTERRUPT_ACK);
245
246 if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)
247 && vdrv && vdrv->config_changed) {
248 vdrv->config_changed(&vm_dev->vdev);
249 ret = IRQ_HANDLED;
250 }
251
252 if (likely(status & VIRTIO_MMIO_INT_VRING)) {
253 spin_lock_irqsave(&vm_dev->lock, flags);
254 list_for_each_entry(info, &vm_dev->virtqueues, node)
255 ret |= vring_interrupt(irq, info->vq);
256 spin_unlock_irqrestore(&vm_dev->lock, flags);
257 }
258
259 return ret;
260}
261
262
263
264static void vm_del_vq(struct virtqueue *vq)
265{
266 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
267 struct virtio_mmio_vq_info *info = vq->priv;
268 unsigned long flags, size;
269 unsigned int index = vq->index;
270
271 spin_lock_irqsave(&vm_dev->lock, flags);
272 list_del(&info->node);
273 spin_unlock_irqrestore(&vm_dev->lock, flags);
274
275 vring_del_virtqueue(vq);
276
277
278 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
279 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
280
281 size = PAGE_ALIGN(vring_size(info->num, VIRTIO_MMIO_VRING_ALIGN));
282 free_pages_exact(info->queue, size);
283 kfree(info);
284}
285
286static void vm_del_vqs(struct virtio_device *vdev)
287{
288 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
289 struct virtqueue *vq, *n;
290
291 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
292 vm_del_vq(vq);
293
294 free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev);
295}
296
297
298
299static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned index,
300 void (*callback)(struct virtqueue *vq),
301 const char *name)
302{
303 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
304 struct virtio_mmio_vq_info *info;
305 struct virtqueue *vq;
306 unsigned long flags, size;
307 int err;
308
309 if (!name)
310 return NULL;
311
312
313 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
314
315
316 if (readl(vm_dev->base + VIRTIO_MMIO_QUEUE_PFN)) {
317 err = -ENOENT;
318 goto error_available;
319 }
320
321
322 info = kmalloc(sizeof(*info), GFP_KERNEL);
323 if (!info) {
324 err = -ENOMEM;
325 goto error_kmalloc;
326 }
327
328
329
330
331
332
333 info->num = readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX);
334
335
336
337
338
339 if (info->num == 0) {
340 err = -ENOENT;
341 goto error_alloc_pages;
342 }
343
344 while (1) {
345 size = PAGE_ALIGN(vring_size(info->num,
346 VIRTIO_MMIO_VRING_ALIGN));
347
348 if (size < VIRTIO_MMIO_VRING_ALIGN * 2) {
349 err = -ENOMEM;
350 goto error_alloc_pages;
351 }
352
353 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
354 if (info->queue)
355 break;
356
357 info->num /= 2;
358 }
359
360
361 writel(info->num, vm_dev->base + VIRTIO_MMIO_QUEUE_NUM);
362 writel(VIRTIO_MMIO_VRING_ALIGN,
363 vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN);
364 writel(virt_to_phys(info->queue) >> PAGE_SHIFT,
365 vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
366
367
368 vq = vring_new_virtqueue(index, info->num, VIRTIO_MMIO_VRING_ALIGN, vdev,
369 true, info->queue, vm_notify, callback, name);
370 if (!vq) {
371 err = -ENOMEM;
372 goto error_new_virtqueue;
373 }
374
375 vq->priv = info;
376 info->vq = vq;
377
378 spin_lock_irqsave(&vm_dev->lock, flags);
379 list_add(&info->node, &vm_dev->virtqueues);
380 spin_unlock_irqrestore(&vm_dev->lock, flags);
381
382 return vq;
383
384error_new_virtqueue:
385 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
386 free_pages_exact(info->queue, size);
387error_alloc_pages:
388 kfree(info);
389error_kmalloc:
390error_available:
391 return ERR_PTR(err);
392}
393
394static int vm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
395 struct virtqueue *vqs[],
396 vq_callback_t *callbacks[],
397 const char *names[])
398{
399 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
400 unsigned int irq = platform_get_irq(vm_dev->pdev, 0);
401 int i, err;
402
403 err = request_irq(irq, vm_interrupt, IRQF_SHARED,
404 dev_name(&vdev->dev), vm_dev);
405 if (err)
406 return err;
407
408 for (i = 0; i < nvqs; ++i) {
409 vqs[i] = vm_setup_vq(vdev, i, callbacks[i], names[i]);
410 if (IS_ERR(vqs[i])) {
411 vm_del_vqs(vdev);
412 return PTR_ERR(vqs[i]);
413 }
414 }
415
416 return 0;
417}
418
419static const char *vm_bus_name(struct virtio_device *vdev)
420{
421 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
422
423 return vm_dev->pdev->name;
424}
425
426static const struct virtio_config_ops virtio_mmio_config_ops = {
427 .get = vm_get,
428 .set = vm_set,
429 .get_status = vm_get_status,
430 .set_status = vm_set_status,
431 .reset = vm_reset,
432 .find_vqs = vm_find_vqs,
433 .del_vqs = vm_del_vqs,
434 .get_features = vm_get_features,
435 .finalize_features = vm_finalize_features,
436 .bus_name = vm_bus_name,
437};
438
439
440
441
442
443static int virtio_mmio_probe(struct platform_device *pdev)
444{
445 struct virtio_mmio_device *vm_dev;
446 struct resource *mem;
447 unsigned long magic;
448
449 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
450 if (!mem)
451 return -EINVAL;
452
453 if (!devm_request_mem_region(&pdev->dev, mem->start,
454 resource_size(mem), pdev->name))
455 return -EBUSY;
456
457 vm_dev = devm_kzalloc(&pdev->dev, sizeof(*vm_dev), GFP_KERNEL);
458 if (!vm_dev)
459 return -ENOMEM;
460
461 vm_dev->vdev.dev.parent = &pdev->dev;
462 vm_dev->vdev.config = &virtio_mmio_config_ops;
463 vm_dev->pdev = pdev;
464 INIT_LIST_HEAD(&vm_dev->virtqueues);
465 spin_lock_init(&vm_dev->lock);
466
467 vm_dev->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
468 if (vm_dev->base == NULL)
469 return -EFAULT;
470
471
472 magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE);
473 if (memcmp(&magic, "virt", 4) != 0) {
474 dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
475 return -ENODEV;
476 }
477
478
479 vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION);
480 if (vm_dev->version != 1) {
481 dev_err(&pdev->dev, "Version %ld not supported!\n",
482 vm_dev->version);
483 return -ENXIO;
484 }
485
486 vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID);
487 vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
488
489 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
490
491 platform_set_drvdata(pdev, vm_dev);
492
493 return register_virtio_device(&vm_dev->vdev);
494}
495
496static int virtio_mmio_remove(struct platform_device *pdev)
497{
498 struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev);
499
500 unregister_virtio_device(&vm_dev->vdev);
501
502 return 0;
503}
504
505
506
507
508
509#if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
510
511static struct device vm_cmdline_parent = {
512 .init_name = "virtio-mmio-cmdline",
513};
514
515static int vm_cmdline_parent_registered;
516static int vm_cmdline_id;
517
518static int vm_cmdline_set(const char *device,
519 const struct kernel_param *kp)
520{
521 int err;
522 struct resource resources[2] = {};
523 char *str;
524 long long int base, size;
525 unsigned int irq;
526 int processed, consumed = 0;
527 struct platform_device *pdev;
528
529
530 size = memparse(device, &str);
531
532
533 processed = sscanf(str, "@%lli:%u%n:%d%n",
534 &base, &irq, &consumed,
535 &vm_cmdline_id, &consumed);
536
537
538
539
540
541
542 if (processed < 2 || str[consumed])
543 return -EINVAL;
544
545 resources[0].flags = IORESOURCE_MEM;
546 resources[0].start = base;
547 resources[0].end = base + size - 1;
548
549 resources[1].flags = IORESOURCE_IRQ;
550 resources[1].start = resources[1].end = irq;
551
552 if (!vm_cmdline_parent_registered) {
553 err = device_register(&vm_cmdline_parent);
554 if (err) {
555 pr_err("Failed to register parent device!\n");
556 return err;
557 }
558 vm_cmdline_parent_registered = 1;
559 }
560
561 pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
562 vm_cmdline_id,
563 (unsigned long long)resources[0].start,
564 (unsigned long long)resources[0].end,
565 (int)resources[1].start);
566
567 pdev = platform_device_register_resndata(&vm_cmdline_parent,
568 "virtio-mmio", vm_cmdline_id++,
569 resources, ARRAY_SIZE(resources), NULL, 0);
570 if (IS_ERR(pdev))
571 return PTR_ERR(pdev);
572
573 return 0;
574}
575
576static int vm_cmdline_get_device(struct device *dev, void *data)
577{
578 char *buffer = data;
579 unsigned int len = strlen(buffer);
580 struct platform_device *pdev = to_platform_device(dev);
581
582 snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n",
583 pdev->resource[0].end - pdev->resource[0].start + 1ULL,
584 (unsigned long long)pdev->resource[0].start,
585 (unsigned long long)pdev->resource[1].start,
586 pdev->id);
587 return 0;
588}
589
590static int vm_cmdline_get(char *buffer, const struct kernel_param *kp)
591{
592 buffer[0] = '\0';
593 device_for_each_child(&vm_cmdline_parent, buffer,
594 vm_cmdline_get_device);
595 return strlen(buffer) + 1;
596}
597
598static struct kernel_param_ops vm_cmdline_param_ops = {
599 .set = vm_cmdline_set,
600 .get = vm_cmdline_get,
601};
602
603device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR);
604
605static int vm_unregister_cmdline_device(struct device *dev,
606 void *data)
607{
608 platform_device_unregister(to_platform_device(dev));
609
610 return 0;
611}
612
613static void vm_unregister_cmdline_devices(void)
614{
615 if (vm_cmdline_parent_registered) {
616 device_for_each_child(&vm_cmdline_parent, NULL,
617 vm_unregister_cmdline_device);
618 device_unregister(&vm_cmdline_parent);
619 vm_cmdline_parent_registered = 0;
620 }
621}
622
623#else
624
625static void vm_unregister_cmdline_devices(void)
626{
627}
628
629#endif
630
631
632
633static struct of_device_id virtio_mmio_match[] = {
634 { .compatible = "virtio,mmio", },
635 {},
636};
637MODULE_DEVICE_TABLE(of, virtio_mmio_match);
638
639static struct platform_driver virtio_mmio_driver = {
640 .probe = virtio_mmio_probe,
641 .remove = virtio_mmio_remove,
642 .driver = {
643 .name = "virtio-mmio",
644 .owner = THIS_MODULE,
645 .of_match_table = virtio_mmio_match,
646 },
647};
648
649static int __init virtio_mmio_init(void)
650{
651 return platform_driver_register(&virtio_mmio_driver);
652}
653
654static void __exit virtio_mmio_exit(void)
655{
656 platform_driver_unregister(&virtio_mmio_driver);
657 vm_unregister_cmdline_devices();
658}
659
660module_init(virtio_mmio_init);
661module_exit(virtio_mmio_exit);
662
663MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
664MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
665MODULE_LICENSE("GPL");
666