2 * Virtio memory mapped device driver
4 * Copyright 2011-2014, ARM Ltd.
6 * This module allows virtio devices to be used over a virtual, memory mapped
9 * The guest device(s) may be instantiated in one of three equivalent ways:
11 * 1. Static platform device in board's code, eg.:
13 * static struct platform_device v2m_virtio_device = {
14 * .name = "virtio-mmio",
17 * .resource = (struct resource []) {
19 * .start = 0x1001e000,
21 * .flags = IORESOURCE_MEM,
25 * .flags = IORESOURCE_IRQ,
30 * 2. Device Tree node, eg.:
32 * virtio_block@1e000 {
33 * compatible = "virtio,mmio";
34 * reg = <0x1e000 0x100>;
38 * 3. Kernel module (or command line) parameter. Can be used more than once -
39 * one device will be created for each one. Syntax:
41 * [virtio_mmio.]device=<size>@<baseaddr>:<irq>[:<id>]
43 * <size> := size (can use standard suffixes like K, M or G)
44 * <baseaddr> := physical base address
45 * <irq> := interrupt number (as passed to request_irq())
46 * <id> := (optional) platform device id
48 * virtio_mmio.device=0x100@0x100b0000:48 \
49 * virtio_mmio.device=1K@0x1001e000:74
53 * Based on Virtio PCI driver by Anthony Liguori, copyright IBM Corp. 2007
55 * This work is licensed under the terms of the GNU GPL, version 2 or later.
56 * See the COPYING file in the top-level directory.
59 #define pr_fmt(fmt) "virtio-mmio: " fmt
61 #include <linux/acpi.h>
62 #include <linux/highmem.h>
63 #include <linux/interrupt.h>
65 #include <linux/list.h>
66 #include <linux/module.h>
67 #include <linux/platform_device.h>
68 #include <linux/slab.h>
69 #include <linux/spinlock.h>
70 #include <linux/virtio.h>
71 #include <linux/virtio_config.h>
72 #include <linux/virtio_mmio.h>
73 #include <linux/virtio_ring.h>
77 /* The alignment to use between consumer and producer parts of vring.
78 * Currently hardcoded to the page size. */
79 #define VIRTIO_MMIO_VRING_ALIGN PAGE_SIZE
83 #define to_virtio_mmio_device(_plat_dev) \
84 container_of(_plat_dev, struct virtio_mmio_device, vdev)
86 struct virtio_mmio_device
{
87 struct virtio_device vdev
;
88 struct platform_device
*pdev
;
91 unsigned long version
;
93 /* a list of queues so we can dispatch IRQs */
95 struct list_head virtqueues
;
98 struct virtio_mmio_vq_info
{
99 /* the actual virtqueue */
100 struct virtqueue
*vq
;
102 /* the number of entries in the queue */
105 /* the virtual address of the ring queue */
108 /* the list node for the virtqueues list */
109 struct list_head node
;
114 /* Configuration interface */
116 static u64
vm_get_features(struct virtio_device
*vdev
)
118 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
121 writel(1, vm_dev
->base
+ VIRTIO_MMIO_DEVICE_FEATURES_SEL
);
122 features
= readl(vm_dev
->base
+ VIRTIO_MMIO_DEVICE_FEATURES
);
125 writel(0, vm_dev
->base
+ VIRTIO_MMIO_DEVICE_FEATURES_SEL
);
126 features
|= readl(vm_dev
->base
+ VIRTIO_MMIO_DEVICE_FEATURES
);
131 static int vm_finalize_features(struct virtio_device
*vdev
)
133 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
135 /* Give virtio_ring a chance to accept features. */
136 vring_transport_features(vdev
);
138 /* Make sure there is are no mixed devices */
139 if (vm_dev
->version
== 2 &&
140 !__virtio_test_bit(vdev
, VIRTIO_F_VERSION_1
)) {
141 dev_err(&vdev
->dev
, "New virtio-mmio devices (version 2) must provide VIRTIO_F_VERSION_1 feature!\n");
145 writel(1, vm_dev
->base
+ VIRTIO_MMIO_DRIVER_FEATURES_SEL
);
146 writel((u32
)(vdev
->features
>> 32),
147 vm_dev
->base
+ VIRTIO_MMIO_DRIVER_FEATURES
);
149 writel(0, vm_dev
->base
+ VIRTIO_MMIO_DRIVER_FEATURES_SEL
);
150 writel((u32
)vdev
->features
,
151 vm_dev
->base
+ VIRTIO_MMIO_DRIVER_FEATURES
);
156 static void vm_get(struct virtio_device
*vdev
, unsigned offset
,
157 void *buf
, unsigned len
)
159 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
160 void __iomem
*base
= vm_dev
->base
+ VIRTIO_MMIO_CONFIG
;
165 if (vm_dev
->version
== 1) {
169 for (i
= 0; i
< len
; i
++)
170 ptr
[i
] = readb(base
+ offset
+ i
);
176 b
= readb(base
+ offset
);
177 memcpy(buf
, &b
, sizeof b
);
180 w
= cpu_to_le16(readw(base
+ offset
));
181 memcpy(buf
, &w
, sizeof w
);
184 l
= cpu_to_le32(readl(base
+ offset
));
185 memcpy(buf
, &l
, sizeof l
);
188 l
= cpu_to_le32(readl(base
+ offset
));
189 memcpy(buf
, &l
, sizeof l
);
190 l
= cpu_to_le32(ioread32(base
+ offset
+ sizeof l
));
191 memcpy(buf
+ sizeof l
, &l
, sizeof l
);
198 static void vm_set(struct virtio_device
*vdev
, unsigned offset
,
199 const void *buf
, unsigned len
)
201 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
202 void __iomem
*base
= vm_dev
->base
+ VIRTIO_MMIO_CONFIG
;
207 if (vm_dev
->version
== 1) {
211 for (i
= 0; i
< len
; i
++)
212 writeb(ptr
[i
], base
+ offset
+ i
);
219 memcpy(&b
, buf
, sizeof b
);
220 writeb(b
, base
+ offset
);
223 memcpy(&w
, buf
, sizeof w
);
224 writew(le16_to_cpu(w
), base
+ offset
);
227 memcpy(&l
, buf
, sizeof l
);
228 writel(le32_to_cpu(l
), base
+ offset
);
231 memcpy(&l
, buf
, sizeof l
);
232 writel(le32_to_cpu(l
), base
+ offset
);
233 memcpy(&l
, buf
+ sizeof l
, sizeof l
);
234 writel(le32_to_cpu(l
), base
+ offset
+ sizeof l
);
241 static u32
vm_generation(struct virtio_device
*vdev
)
243 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
245 if (vm_dev
->version
== 1)
248 return readl(vm_dev
->base
+ VIRTIO_MMIO_CONFIG_GENERATION
);
251 static u8
vm_get_status(struct virtio_device
*vdev
)
253 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
255 return readl(vm_dev
->base
+ VIRTIO_MMIO_STATUS
) & 0xff;
258 static void vm_set_status(struct virtio_device
*vdev
, u8 status
)
260 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
262 /* We should never be setting status to 0. */
265 writel(status
, vm_dev
->base
+ VIRTIO_MMIO_STATUS
);
268 static void vm_reset(struct virtio_device
*vdev
)
270 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
272 /* 0 status means a reset. */
273 writel(0, vm_dev
->base
+ VIRTIO_MMIO_STATUS
);
278 /* Transport interface */
280 /* the notify function used when creating a virt queue */
281 static bool vm_notify(struct virtqueue
*vq
)
283 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vq
->vdev
);
285 /* We write the queue's selector into the notification register to
286 * signal the other end */
287 writel(vq
->index
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_NOTIFY
);
291 /* Notify all virtqueues on an interrupt. */
292 static irqreturn_t
vm_interrupt(int irq
, void *opaque
)
294 struct virtio_mmio_device
*vm_dev
= opaque
;
295 struct virtio_mmio_vq_info
*info
;
296 unsigned long status
;
298 irqreturn_t ret
= IRQ_NONE
;
300 /* Read and acknowledge interrupts */
301 status
= readl(vm_dev
->base
+ VIRTIO_MMIO_INTERRUPT_STATUS
);
302 writel(status
, vm_dev
->base
+ VIRTIO_MMIO_INTERRUPT_ACK
);
304 if (unlikely(status
& VIRTIO_MMIO_INT_CONFIG
)) {
305 virtio_config_changed(&vm_dev
->vdev
);
309 if (likely(status
& VIRTIO_MMIO_INT_VRING
)) {
310 spin_lock_irqsave(&vm_dev
->lock
, flags
);
311 list_for_each_entry(info
, &vm_dev
->virtqueues
, node
)
312 ret
|= vring_interrupt(irq
, info
->vq
);
313 spin_unlock_irqrestore(&vm_dev
->lock
, flags
);
321 static void vm_del_vq(struct virtqueue
*vq
)
323 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vq
->vdev
);
324 struct virtio_mmio_vq_info
*info
= vq
->priv
;
325 unsigned long flags
, size
;
326 unsigned int index
= vq
->index
;
328 spin_lock_irqsave(&vm_dev
->lock
, flags
);
329 list_del(&info
->node
);
330 spin_unlock_irqrestore(&vm_dev
->lock
, flags
);
332 vring_del_virtqueue(vq
);
334 /* Select and deactivate the queue */
335 writel(index
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_SEL
);
336 if (vm_dev
->version
== 1) {
337 writel(0, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_PFN
);
339 writel(0, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_READY
);
340 WARN_ON(readl(vm_dev
->base
+ VIRTIO_MMIO_QUEUE_READY
));
343 size
= PAGE_ALIGN(vring_size(info
->num
, VIRTIO_MMIO_VRING_ALIGN
));
344 free_pages_exact(info
->queue
, size
);
348 static void vm_del_vqs(struct virtio_device
*vdev
)
350 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
351 struct virtqueue
*vq
, *n
;
353 list_for_each_entry_safe(vq
, n
, &vdev
->vqs
, list
)
356 free_irq(platform_get_irq(vm_dev
->pdev
, 0), vm_dev
);
361 static struct virtqueue
*vm_setup_vq(struct virtio_device
*vdev
, unsigned index
,
362 void (*callback
)(struct virtqueue
*vq
),
365 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
366 struct virtio_mmio_vq_info
*info
;
367 struct virtqueue
*vq
;
368 unsigned long flags
, size
;
374 /* Select the queue we're interested in */
375 writel(index
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_SEL
);
377 /* Queue shouldn't already be set up. */
378 if (readl(vm_dev
->base
+ (vm_dev
->version
== 1 ?
379 VIRTIO_MMIO_QUEUE_PFN
: VIRTIO_MMIO_QUEUE_READY
))) {
381 goto error_available
;
384 /* Allocate and fill out our active queue description */
385 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
391 /* Allocate pages for the queue - start with a queue as big as
392 * possible (limited by maximum size allowed by device), drop down
393 * to a minimal size, just big enough to fit descriptor table
394 * and two rings (which makes it "alignment_size * 2")
396 info
->num
= readl(vm_dev
->base
+ VIRTIO_MMIO_QUEUE_NUM_MAX
);
398 /* If the device reports a 0 entry queue, we won't be able to
399 * use it to perform I/O, and vring_new_virtqueue() can't create
400 * empty queues anyway, so don't bother to set up the device.
402 if (info
->num
== 0) {
404 goto error_alloc_pages
;
408 size
= PAGE_ALIGN(vring_size(info
->num
,
409 VIRTIO_MMIO_VRING_ALIGN
));
410 /* Did the last iter shrink the queue below minimum size? */
411 if (size
< VIRTIO_MMIO_VRING_ALIGN
* 2) {
413 goto error_alloc_pages
;
416 info
->queue
= alloc_pages_exact(size
, GFP_KERNEL
| __GFP_ZERO
);
423 /* Create the vring */
424 vq
= vring_new_virtqueue(index
, info
->num
, VIRTIO_MMIO_VRING_ALIGN
, vdev
,
425 true, info
->queue
, vm_notify
, callback
, name
);
428 goto error_new_virtqueue
;
431 /* Activate the queue */
432 writel(info
->num
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_NUM
);
433 if (vm_dev
->version
== 1) {
434 writel(PAGE_SIZE
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_ALIGN
);
435 writel(virt_to_phys(info
->queue
) >> PAGE_SHIFT
,
436 vm_dev
->base
+ VIRTIO_MMIO_QUEUE_PFN
);
440 addr
= virt_to_phys(info
->queue
);
441 writel((u32
)addr
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_DESC_LOW
);
442 writel((u32
)(addr
>> 32),
443 vm_dev
->base
+ VIRTIO_MMIO_QUEUE_DESC_HIGH
);
445 addr
= virt_to_phys(virtqueue_get_avail(vq
));
446 writel((u32
)addr
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_AVAIL_LOW
);
447 writel((u32
)(addr
>> 32),
448 vm_dev
->base
+ VIRTIO_MMIO_QUEUE_AVAIL_HIGH
);
450 addr
= virt_to_phys(virtqueue_get_used(vq
));
451 writel((u32
)addr
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_USED_LOW
);
452 writel((u32
)(addr
>> 32),
453 vm_dev
->base
+ VIRTIO_MMIO_QUEUE_USED_HIGH
);
455 writel(1, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_READY
);
461 spin_lock_irqsave(&vm_dev
->lock
, flags
);
462 list_add(&info
->node
, &vm_dev
->virtqueues
);
463 spin_unlock_irqrestore(&vm_dev
->lock
, flags
);
468 if (vm_dev
->version
== 1) {
469 writel(0, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_PFN
);
471 writel(0, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_READY
);
472 WARN_ON(readl(vm_dev
->base
+ VIRTIO_MMIO_QUEUE_READY
));
474 free_pages_exact(info
->queue
, size
);
482 static int vm_find_vqs(struct virtio_device
*vdev
, unsigned nvqs
,
483 struct virtqueue
*vqs
[],
484 vq_callback_t
*callbacks
[],
487 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
488 unsigned int irq
= platform_get_irq(vm_dev
->pdev
, 0);
491 err
= request_irq(irq
, vm_interrupt
, IRQF_SHARED
,
492 dev_name(&vdev
->dev
), vm_dev
);
496 for (i
= 0; i
< nvqs
; ++i
) {
497 vqs
[i
] = vm_setup_vq(vdev
, i
, callbacks
[i
], names
[i
]);
498 if (IS_ERR(vqs
[i
])) {
500 return PTR_ERR(vqs
[i
]);
507 static const char *vm_bus_name(struct virtio_device
*vdev
)
509 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
511 return vm_dev
->pdev
->name
;
514 static const struct virtio_config_ops virtio_mmio_config_ops
= {
517 .generation
= vm_generation
,
518 .get_status
= vm_get_status
,
519 .set_status
= vm_set_status
,
521 .find_vqs
= vm_find_vqs
,
522 .del_vqs
= vm_del_vqs
,
523 .get_features
= vm_get_features
,
524 .finalize_features
= vm_finalize_features
,
525 .bus_name
= vm_bus_name
,
530 /* Platform device */
532 static int virtio_mmio_probe(struct platform_device
*pdev
)
534 struct virtio_mmio_device
*vm_dev
;
535 struct resource
*mem
;
538 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
542 if (!devm_request_mem_region(&pdev
->dev
, mem
->start
,
543 resource_size(mem
), pdev
->name
))
546 vm_dev
= devm_kzalloc(&pdev
->dev
, sizeof(*vm_dev
), GFP_KERNEL
);
550 vm_dev
->vdev
.dev
.parent
= &pdev
->dev
;
551 vm_dev
->vdev
.config
= &virtio_mmio_config_ops
;
553 INIT_LIST_HEAD(&vm_dev
->virtqueues
);
554 spin_lock_init(&vm_dev
->lock
);
556 vm_dev
->base
= devm_ioremap(&pdev
->dev
, mem
->start
, resource_size(mem
));
557 if (vm_dev
->base
== NULL
)
560 /* Check magic value */
561 magic
= readl(vm_dev
->base
+ VIRTIO_MMIO_MAGIC_VALUE
);
562 if (magic
!= ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
563 dev_warn(&pdev
->dev
, "Wrong magic value 0x%08lx!\n", magic
);
567 /* Check device version */
568 vm_dev
->version
= readl(vm_dev
->base
+ VIRTIO_MMIO_VERSION
);
569 if (vm_dev
->version
< 1 || vm_dev
->version
> 2) {
570 dev_err(&pdev
->dev
, "Version %ld not supported!\n",
575 vm_dev
->vdev
.id
.device
= readl(vm_dev
->base
+ VIRTIO_MMIO_DEVICE_ID
);
576 if (vm_dev
->vdev
.id
.device
== 0) {
578 * virtio-mmio device with an ID 0 is a (dummy) placeholder
579 * with no function. End probing now with no error reported.
583 vm_dev
->vdev
.id
.vendor
= readl(vm_dev
->base
+ VIRTIO_MMIO_VENDOR_ID
);
585 if (vm_dev
->version
== 1)
586 writel(PAGE_SIZE
, vm_dev
->base
+ VIRTIO_MMIO_GUEST_PAGE_SIZE
);
588 platform_set_drvdata(pdev
, vm_dev
);
590 return register_virtio_device(&vm_dev
->vdev
);
593 static int virtio_mmio_remove(struct platform_device
*pdev
)
595 struct virtio_mmio_device
*vm_dev
= platform_get_drvdata(pdev
);
597 unregister_virtio_device(&vm_dev
->vdev
);
604 /* Devices list parameter */
606 #if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
608 static struct device vm_cmdline_parent
= {
609 .init_name
= "virtio-mmio-cmdline",
612 static int vm_cmdline_parent_registered
;
613 static int vm_cmdline_id
;
615 static int vm_cmdline_set(const char *device
,
616 const struct kernel_param
*kp
)
619 struct resource resources
[2] = {};
621 long long int base
, size
;
623 int processed
, consumed
= 0;
624 struct platform_device
*pdev
;
626 /* Consume "size" part of the command line parameter */
627 size
= memparse(device
, &str
);
629 /* Get "@<base>:<irq>[:<id>]" chunks */
630 processed
= sscanf(str
, "@%lli:%u%n:%d%n",
631 &base
, &irq
, &consumed
,
632 &vm_cmdline_id
, &consumed
);
635 * sscanf() must processes at least 2 chunks; also there
636 * must be no extra characters after the last chunk, so
637 * str[consumed] must be '\0'
639 if (processed
< 2 || str
[consumed
])
642 resources
[0].flags
= IORESOURCE_MEM
;
643 resources
[0].start
= base
;
644 resources
[0].end
= base
+ size
- 1;
646 resources
[1].flags
= IORESOURCE_IRQ
;
647 resources
[1].start
= resources
[1].end
= irq
;
649 if (!vm_cmdline_parent_registered
) {
650 err
= device_register(&vm_cmdline_parent
);
652 pr_err("Failed to register parent device!\n");
655 vm_cmdline_parent_registered
= 1;
658 pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
660 (unsigned long long)resources
[0].start
,
661 (unsigned long long)resources
[0].end
,
662 (int)resources
[1].start
);
664 pdev
= platform_device_register_resndata(&vm_cmdline_parent
,
665 "virtio-mmio", vm_cmdline_id
++,
666 resources
, ARRAY_SIZE(resources
), NULL
, 0);
668 return PTR_ERR(pdev
);
673 static int vm_cmdline_get_device(struct device
*dev
, void *data
)
676 unsigned int len
= strlen(buffer
);
677 struct platform_device
*pdev
= to_platform_device(dev
);
679 snprintf(buffer
+ len
, PAGE_SIZE
- len
, "0x%llx@0x%llx:%llu:%d\n",
680 pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1ULL,
681 (unsigned long long)pdev
->resource
[0].start
,
682 (unsigned long long)pdev
->resource
[1].start
,
687 static int vm_cmdline_get(char *buffer
, const struct kernel_param
*kp
)
690 device_for_each_child(&vm_cmdline_parent
, buffer
,
691 vm_cmdline_get_device
);
692 return strlen(buffer
) + 1;
695 static const struct kernel_param_ops vm_cmdline_param_ops
= {
696 .set
= vm_cmdline_set
,
697 .get
= vm_cmdline_get
,
700 device_param_cb(device
, &vm_cmdline_param_ops
, NULL
, S_IRUSR
);
702 static int vm_unregister_cmdline_device(struct device
*dev
,
705 platform_device_unregister(to_platform_device(dev
));
710 static void vm_unregister_cmdline_devices(void)
712 if (vm_cmdline_parent_registered
) {
713 device_for_each_child(&vm_cmdline_parent
, NULL
,
714 vm_unregister_cmdline_device
);
715 device_unregister(&vm_cmdline_parent
);
716 vm_cmdline_parent_registered
= 0;
722 static void vm_unregister_cmdline_devices(void)
728 /* Platform driver */
730 static struct of_device_id virtio_mmio_match
[] = {
731 { .compatible
= "virtio,mmio", },
734 MODULE_DEVICE_TABLE(of
, virtio_mmio_match
);
737 static const struct acpi_device_id virtio_mmio_acpi_match
[] = {
741 MODULE_DEVICE_TABLE(acpi
, virtio_mmio_acpi_match
);
744 static struct platform_driver virtio_mmio_driver
= {
745 .probe
= virtio_mmio_probe
,
746 .remove
= virtio_mmio_remove
,
748 .name
= "virtio-mmio",
749 .of_match_table
= virtio_mmio_match
,
750 .acpi_match_table
= ACPI_PTR(virtio_mmio_acpi_match
),
754 static int __init
virtio_mmio_init(void)
756 return platform_driver_register(&virtio_mmio_driver
);
759 static void __exit
virtio_mmio_exit(void)
761 platform_driver_unregister(&virtio_mmio_driver
);
762 vm_unregister_cmdline_devices();
765 module_init(virtio_mmio_init
);
766 module_exit(virtio_mmio_exit
);
768 MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
769 MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
770 MODULE_LICENSE("GPL");