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 list node for the virtqueues list */
103 struct list_head node
;
108 /* Configuration interface */
110 static u64
vm_get_features(struct virtio_device
*vdev
)
112 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
115 writel(1, vm_dev
->base
+ VIRTIO_MMIO_DEVICE_FEATURES_SEL
);
116 features
= readl(vm_dev
->base
+ VIRTIO_MMIO_DEVICE_FEATURES
);
119 writel(0, vm_dev
->base
+ VIRTIO_MMIO_DEVICE_FEATURES_SEL
);
120 features
|= readl(vm_dev
->base
+ VIRTIO_MMIO_DEVICE_FEATURES
);
125 static int vm_finalize_features(struct virtio_device
*vdev
)
127 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
129 /* Give virtio_ring a chance to accept features. */
130 vring_transport_features(vdev
);
132 /* Make sure there is are no mixed devices */
133 if (vm_dev
->version
== 2 &&
134 !__virtio_test_bit(vdev
, VIRTIO_F_VERSION_1
)) {
135 dev_err(&vdev
->dev
, "New virtio-mmio devices (version 2) must provide VIRTIO_F_VERSION_1 feature!\n");
139 writel(1, vm_dev
->base
+ VIRTIO_MMIO_DRIVER_FEATURES_SEL
);
140 writel((u32
)(vdev
->features
>> 32),
141 vm_dev
->base
+ VIRTIO_MMIO_DRIVER_FEATURES
);
143 writel(0, vm_dev
->base
+ VIRTIO_MMIO_DRIVER_FEATURES_SEL
);
144 writel((u32
)vdev
->features
,
145 vm_dev
->base
+ VIRTIO_MMIO_DRIVER_FEATURES
);
150 static void vm_get(struct virtio_device
*vdev
, unsigned offset
,
151 void *buf
, unsigned len
)
153 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
154 void __iomem
*base
= vm_dev
->base
+ VIRTIO_MMIO_CONFIG
;
159 if (vm_dev
->version
== 1) {
163 for (i
= 0; i
< len
; i
++)
164 ptr
[i
] = readb(base
+ offset
+ i
);
170 b
= readb(base
+ offset
);
171 memcpy(buf
, &b
, sizeof b
);
174 w
= cpu_to_le16(readw(base
+ offset
));
175 memcpy(buf
, &w
, sizeof w
);
178 l
= cpu_to_le32(readl(base
+ offset
));
179 memcpy(buf
, &l
, sizeof l
);
182 l
= cpu_to_le32(readl(base
+ offset
));
183 memcpy(buf
, &l
, sizeof l
);
184 l
= cpu_to_le32(ioread32(base
+ offset
+ sizeof l
));
185 memcpy(buf
+ sizeof l
, &l
, sizeof l
);
192 static void vm_set(struct virtio_device
*vdev
, unsigned offset
,
193 const void *buf
, unsigned len
)
195 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
196 void __iomem
*base
= vm_dev
->base
+ VIRTIO_MMIO_CONFIG
;
201 if (vm_dev
->version
== 1) {
205 for (i
= 0; i
< len
; i
++)
206 writeb(ptr
[i
], base
+ offset
+ i
);
213 memcpy(&b
, buf
, sizeof b
);
214 writeb(b
, base
+ offset
);
217 memcpy(&w
, buf
, sizeof w
);
218 writew(le16_to_cpu(w
), base
+ offset
);
221 memcpy(&l
, buf
, sizeof l
);
222 writel(le32_to_cpu(l
), base
+ offset
);
225 memcpy(&l
, buf
, sizeof l
);
226 writel(le32_to_cpu(l
), base
+ offset
);
227 memcpy(&l
, buf
+ sizeof l
, sizeof l
);
228 writel(le32_to_cpu(l
), base
+ offset
+ sizeof l
);
235 static u32
vm_generation(struct virtio_device
*vdev
)
237 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
239 if (vm_dev
->version
== 1)
242 return readl(vm_dev
->base
+ VIRTIO_MMIO_CONFIG_GENERATION
);
245 static u8
vm_get_status(struct virtio_device
*vdev
)
247 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
249 return readl(vm_dev
->base
+ VIRTIO_MMIO_STATUS
) & 0xff;
252 static void vm_set_status(struct virtio_device
*vdev
, u8 status
)
254 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
256 /* We should never be setting status to 0. */
259 writel(status
, vm_dev
->base
+ VIRTIO_MMIO_STATUS
);
262 static void vm_reset(struct virtio_device
*vdev
)
264 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
266 /* 0 status means a reset. */
267 writel(0, vm_dev
->base
+ VIRTIO_MMIO_STATUS
);
272 /* Transport interface */
274 /* the notify function used when creating a virt queue */
275 static bool vm_notify(struct virtqueue
*vq
)
277 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vq
->vdev
);
279 /* We write the queue's selector into the notification register to
280 * signal the other end */
281 writel(vq
->index
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_NOTIFY
);
285 /* Notify all virtqueues on an interrupt. */
286 static irqreturn_t
vm_interrupt(int irq
, void *opaque
)
288 struct virtio_mmio_device
*vm_dev
= opaque
;
289 struct virtio_mmio_vq_info
*info
;
290 unsigned long status
;
292 irqreturn_t ret
= IRQ_NONE
;
294 /* Read and acknowledge interrupts */
295 status
= readl(vm_dev
->base
+ VIRTIO_MMIO_INTERRUPT_STATUS
);
296 writel(status
, vm_dev
->base
+ VIRTIO_MMIO_INTERRUPT_ACK
);
298 if (unlikely(status
& VIRTIO_MMIO_INT_CONFIG
)) {
299 virtio_config_changed(&vm_dev
->vdev
);
303 if (likely(status
& VIRTIO_MMIO_INT_VRING
)) {
304 spin_lock_irqsave(&vm_dev
->lock
, flags
);
305 list_for_each_entry(info
, &vm_dev
->virtqueues
, node
)
306 ret
|= vring_interrupt(irq
, info
->vq
);
307 spin_unlock_irqrestore(&vm_dev
->lock
, flags
);
315 static void vm_del_vq(struct virtqueue
*vq
)
317 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vq
->vdev
);
318 struct virtio_mmio_vq_info
*info
= vq
->priv
;
320 unsigned int index
= vq
->index
;
322 spin_lock_irqsave(&vm_dev
->lock
, flags
);
323 list_del(&info
->node
);
324 spin_unlock_irqrestore(&vm_dev
->lock
, flags
);
326 /* Select and deactivate the queue */
327 writel(index
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_SEL
);
328 if (vm_dev
->version
== 1) {
329 writel(0, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_PFN
);
331 writel(0, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_READY
);
332 WARN_ON(readl(vm_dev
->base
+ VIRTIO_MMIO_QUEUE_READY
));
335 vring_del_virtqueue(vq
);
340 static void vm_del_vqs(struct virtio_device
*vdev
)
342 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
343 struct virtqueue
*vq
, *n
;
345 list_for_each_entry_safe(vq
, n
, &vdev
->vqs
, list
)
348 free_irq(platform_get_irq(vm_dev
->pdev
, 0), vm_dev
);
351 static struct virtqueue
*vm_setup_vq(struct virtio_device
*vdev
, unsigned index
,
352 void (*callback
)(struct virtqueue
*vq
),
355 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
356 struct virtio_mmio_vq_info
*info
;
357 struct virtqueue
*vq
;
365 /* Select the queue we're interested in */
366 writel(index
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_SEL
);
368 /* Queue shouldn't already be set up. */
369 if (readl(vm_dev
->base
+ (vm_dev
->version
== 1 ?
370 VIRTIO_MMIO_QUEUE_PFN
: VIRTIO_MMIO_QUEUE_READY
))) {
372 goto error_available
;
375 /* Allocate and fill out our active queue description */
376 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
382 num
= readl(vm_dev
->base
+ VIRTIO_MMIO_QUEUE_NUM_MAX
);
385 goto error_new_virtqueue
;
388 /* Create the vring */
389 vq
= vring_create_virtqueue(index
, num
, VIRTIO_MMIO_VRING_ALIGN
, vdev
,
390 true, true, vm_notify
, callback
, name
);
393 goto error_new_virtqueue
;
396 /* Activate the queue */
397 writel(virtqueue_get_vring_size(vq
), vm_dev
->base
+ VIRTIO_MMIO_QUEUE_NUM
);
398 if (vm_dev
->version
== 1) {
399 writel(PAGE_SIZE
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_ALIGN
);
400 writel(virtqueue_get_desc_addr(vq
) >> PAGE_SHIFT
,
401 vm_dev
->base
+ VIRTIO_MMIO_QUEUE_PFN
);
405 addr
= virtqueue_get_desc_addr(vq
);
406 writel((u32
)addr
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_DESC_LOW
);
407 writel((u32
)(addr
>> 32),
408 vm_dev
->base
+ VIRTIO_MMIO_QUEUE_DESC_HIGH
);
410 addr
= virtqueue_get_avail_addr(vq
);
411 writel((u32
)addr
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_AVAIL_LOW
);
412 writel((u32
)(addr
>> 32),
413 vm_dev
->base
+ VIRTIO_MMIO_QUEUE_AVAIL_HIGH
);
415 addr
= virtqueue_get_used_addr(vq
);
416 writel((u32
)addr
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_USED_LOW
);
417 writel((u32
)(addr
>> 32),
418 vm_dev
->base
+ VIRTIO_MMIO_QUEUE_USED_HIGH
);
420 writel(1, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_READY
);
426 spin_lock_irqsave(&vm_dev
->lock
, flags
);
427 list_add(&info
->node
, &vm_dev
->virtqueues
);
428 spin_unlock_irqrestore(&vm_dev
->lock
, flags
);
433 if (vm_dev
->version
== 1) {
434 writel(0, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_PFN
);
436 writel(0, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_READY
);
437 WARN_ON(readl(vm_dev
->base
+ VIRTIO_MMIO_QUEUE_READY
));
445 static int vm_find_vqs(struct virtio_device
*vdev
, unsigned nvqs
,
446 struct virtqueue
*vqs
[],
447 vq_callback_t
*callbacks
[],
448 const char * const names
[])
450 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
451 unsigned int irq
= platform_get_irq(vm_dev
->pdev
, 0);
454 err
= request_irq(irq
, vm_interrupt
, IRQF_SHARED
,
455 dev_name(&vdev
->dev
), vm_dev
);
459 for (i
= 0; i
< nvqs
; ++i
) {
460 vqs
[i
] = vm_setup_vq(vdev
, i
, callbacks
[i
], names
[i
]);
461 if (IS_ERR(vqs
[i
])) {
463 return PTR_ERR(vqs
[i
]);
470 static const char *vm_bus_name(struct virtio_device
*vdev
)
472 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
474 return vm_dev
->pdev
->name
;
477 static const struct virtio_config_ops virtio_mmio_config_ops
= {
480 .generation
= vm_generation
,
481 .get_status
= vm_get_status
,
482 .set_status
= vm_set_status
,
484 .find_vqs
= vm_find_vqs
,
485 .del_vqs
= vm_del_vqs
,
486 .get_features
= vm_get_features
,
487 .finalize_features
= vm_finalize_features
,
488 .bus_name
= vm_bus_name
,
492 static void virtio_mmio_release_dev_empty(struct device
*_d
) {}
494 /* Platform device */
496 static int virtio_mmio_probe(struct platform_device
*pdev
)
498 struct virtio_mmio_device
*vm_dev
;
499 struct resource
*mem
;
502 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
506 if (!devm_request_mem_region(&pdev
->dev
, mem
->start
,
507 resource_size(mem
), pdev
->name
))
510 vm_dev
= devm_kzalloc(&pdev
->dev
, sizeof(*vm_dev
), GFP_KERNEL
);
514 vm_dev
->vdev
.dev
.parent
= &pdev
->dev
;
515 vm_dev
->vdev
.dev
.release
= virtio_mmio_release_dev_empty
;
516 vm_dev
->vdev
.config
= &virtio_mmio_config_ops
;
518 INIT_LIST_HEAD(&vm_dev
->virtqueues
);
519 spin_lock_init(&vm_dev
->lock
);
521 vm_dev
->base
= devm_ioremap(&pdev
->dev
, mem
->start
, resource_size(mem
));
522 if (vm_dev
->base
== NULL
)
525 /* Check magic value */
526 magic
= readl(vm_dev
->base
+ VIRTIO_MMIO_MAGIC_VALUE
);
527 if (magic
!= ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
528 dev_warn(&pdev
->dev
, "Wrong magic value 0x%08lx!\n", magic
);
532 /* Check device version */
533 vm_dev
->version
= readl(vm_dev
->base
+ VIRTIO_MMIO_VERSION
);
534 if (vm_dev
->version
< 1 || vm_dev
->version
> 2) {
535 dev_err(&pdev
->dev
, "Version %ld not supported!\n",
540 vm_dev
->vdev
.id
.device
= readl(vm_dev
->base
+ VIRTIO_MMIO_DEVICE_ID
);
541 if (vm_dev
->vdev
.id
.device
== 0) {
543 * virtio-mmio device with an ID 0 is a (dummy) placeholder
544 * with no function. End probing now with no error reported.
548 vm_dev
->vdev
.id
.vendor
= readl(vm_dev
->base
+ VIRTIO_MMIO_VENDOR_ID
);
550 if (vm_dev
->version
== 1)
551 writel(PAGE_SIZE
, vm_dev
->base
+ VIRTIO_MMIO_GUEST_PAGE_SIZE
);
553 platform_set_drvdata(pdev
, vm_dev
);
555 return register_virtio_device(&vm_dev
->vdev
);
558 static int virtio_mmio_remove(struct platform_device
*pdev
)
560 struct virtio_mmio_device
*vm_dev
= platform_get_drvdata(pdev
);
562 unregister_virtio_device(&vm_dev
->vdev
);
569 /* Devices list parameter */
571 #if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
573 static struct device vm_cmdline_parent
= {
574 .init_name
= "virtio-mmio-cmdline",
577 static int vm_cmdline_parent_registered
;
578 static int vm_cmdline_id
;
580 static int vm_cmdline_set(const char *device
,
581 const struct kernel_param
*kp
)
584 struct resource resources
[2] = {};
586 long long int base
, size
;
588 int processed
, consumed
= 0;
589 struct platform_device
*pdev
;
591 /* Consume "size" part of the command line parameter */
592 size
= memparse(device
, &str
);
594 /* Get "@<base>:<irq>[:<id>]" chunks */
595 processed
= sscanf(str
, "@%lli:%u%n:%d%n",
596 &base
, &irq
, &consumed
,
597 &vm_cmdline_id
, &consumed
);
600 * sscanf() must processes at least 2 chunks; also there
601 * must be no extra characters after the last chunk, so
602 * str[consumed] must be '\0'
604 if (processed
< 2 || str
[consumed
])
607 resources
[0].flags
= IORESOURCE_MEM
;
608 resources
[0].start
= base
;
609 resources
[0].end
= base
+ size
- 1;
611 resources
[1].flags
= IORESOURCE_IRQ
;
612 resources
[1].start
= resources
[1].end
= irq
;
614 if (!vm_cmdline_parent_registered
) {
615 err
= device_register(&vm_cmdline_parent
);
617 pr_err("Failed to register parent device!\n");
620 vm_cmdline_parent_registered
= 1;
623 pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
625 (unsigned long long)resources
[0].start
,
626 (unsigned long long)resources
[0].end
,
627 (int)resources
[1].start
);
629 pdev
= platform_device_register_resndata(&vm_cmdline_parent
,
630 "virtio-mmio", vm_cmdline_id
++,
631 resources
, ARRAY_SIZE(resources
), NULL
, 0);
633 return PTR_ERR(pdev
);
638 static int vm_cmdline_get_device(struct device
*dev
, void *data
)
641 unsigned int len
= strlen(buffer
);
642 struct platform_device
*pdev
= to_platform_device(dev
);
644 snprintf(buffer
+ len
, PAGE_SIZE
- len
, "0x%llx@0x%llx:%llu:%d\n",
645 pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1ULL,
646 (unsigned long long)pdev
->resource
[0].start
,
647 (unsigned long long)pdev
->resource
[1].start
,
652 static int vm_cmdline_get(char *buffer
, const struct kernel_param
*kp
)
655 device_for_each_child(&vm_cmdline_parent
, buffer
,
656 vm_cmdline_get_device
);
657 return strlen(buffer
) + 1;
660 static const struct kernel_param_ops vm_cmdline_param_ops
= {
661 .set
= vm_cmdline_set
,
662 .get
= vm_cmdline_get
,
665 device_param_cb(device
, &vm_cmdline_param_ops
, NULL
, S_IRUSR
);
667 static int vm_unregister_cmdline_device(struct device
*dev
,
670 platform_device_unregister(to_platform_device(dev
));
675 static void vm_unregister_cmdline_devices(void)
677 if (vm_cmdline_parent_registered
) {
678 device_for_each_child(&vm_cmdline_parent
, NULL
,
679 vm_unregister_cmdline_device
);
680 device_unregister(&vm_cmdline_parent
);
681 vm_cmdline_parent_registered
= 0;
687 static void vm_unregister_cmdline_devices(void)
693 /* Platform driver */
695 static struct of_device_id virtio_mmio_match
[] = {
696 { .compatible
= "virtio,mmio", },
699 MODULE_DEVICE_TABLE(of
, virtio_mmio_match
);
702 static const struct acpi_device_id virtio_mmio_acpi_match
[] = {
706 MODULE_DEVICE_TABLE(acpi
, virtio_mmio_acpi_match
);
709 static struct platform_driver virtio_mmio_driver
= {
710 .probe
= virtio_mmio_probe
,
711 .remove
= virtio_mmio_remove
,
713 .name
= "virtio-mmio",
714 .of_match_table
= virtio_mmio_match
,
715 .acpi_match_table
= ACPI_PTR(virtio_mmio_acpi_match
),
719 static int __init
virtio_mmio_init(void)
721 return platform_driver_register(&virtio_mmio_driver
);
724 static void __exit
virtio_mmio_exit(void)
726 platform_driver_unregister(&virtio_mmio_driver
);
727 vm_unregister_cmdline_devices();
730 module_init(virtio_mmio_init
);
731 module_exit(virtio_mmio_exit
);
733 MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
734 MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
735 MODULE_LICENSE("GPL");