2 * Virtio memory mapped device driver
4 * Copyright 2011, 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 * Registers layout (all 32-bit wide):
55 * offset d. name description
56 * ------ -- ---------------- -----------------
58 * 0x000 R MagicValue Magic value "virt"
59 * 0x004 R Version Device version (current max. 1)
60 * 0x008 R DeviceID Virtio device ID
61 * 0x00c R VendorID Virtio vendor ID
63 * 0x010 R HostFeatures Features supported by the host
64 * 0x014 W HostFeaturesSel Set of host features to access via HostFeatures
66 * 0x020 W GuestFeatures Features activated by the guest
67 * 0x024 W GuestFeaturesSel Set of activated features to set via GuestFeatures
68 * 0x028 W GuestPageSize Size of guest's memory page in bytes
70 * 0x030 W QueueSel Queue selector
71 * 0x034 R QueueNumMax Maximum size of the currently selected queue
72 * 0x038 W QueueNum Queue size for the currently selected queue
73 * 0x03c W QueueAlign Used Ring alignment for the current queue
74 * 0x040 RW QueuePFN PFN for the currently selected queue
76 * 0x050 W QueueNotify Queue notifier
77 * 0x060 R InterruptStatus Interrupt status register
78 * 0x060 W InterruptACK Interrupt acknowledge register
79 * 0x070 RW Status Device status register
81 * 0x100+ RW Device-specific configuration space
83 * Based on Virtio PCI driver by Anthony Liguori, copyright IBM Corp. 2007
85 * This work is licensed under the terms of the GNU GPL, version 2 or later.
86 * See the COPYING file in the top-level directory.
89 #define pr_fmt(fmt) "virtio-mmio: " fmt
91 #include <linux/highmem.h>
92 #include <linux/interrupt.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>
106 /* The alignment to use between consumer and producer parts of vring.
107 * Currently hardcoded to the page size. */
108 #define VIRTIO_MMIO_VRING_ALIGN PAGE_SIZE
112 #define to_virtio_mmio_device(_plat_dev) \
113 container_of(_plat_dev, struct virtio_mmio_device, vdev)
115 struct virtio_mmio_device
{
116 struct virtio_device vdev
;
117 struct platform_device
*pdev
;
120 unsigned long version
;
122 /* a list of queues so we can dispatch IRQs */
124 struct list_head virtqueues
;
127 struct virtio_mmio_vq_info
{
128 /* the actual virtqueue */
129 struct virtqueue
*vq
;
131 /* the number of entries in the queue */
134 /* the virtual address of the ring queue */
137 /* the list node for the virtqueues list */
138 struct list_head node
;
143 /* Configuration interface */
145 static u32
vm_get_features(struct virtio_device
*vdev
)
147 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
149 /* TODO: Features > 32 bits */
150 writel(0, vm_dev
->base
+ VIRTIO_MMIO_HOST_FEATURES_SEL
);
152 return readl(vm_dev
->base
+ VIRTIO_MMIO_HOST_FEATURES
);
155 static void vm_finalize_features(struct virtio_device
*vdev
)
157 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
160 /* Give virtio_ring a chance to accept features. */
161 vring_transport_features(vdev
);
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
);
170 static void vm_get(struct virtio_device
*vdev
, unsigned offset
,
171 void *buf
, unsigned len
)
173 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
177 for (i
= 0; i
< len
; i
++)
178 ptr
[i
] = readb(vm_dev
->base
+ VIRTIO_MMIO_CONFIG
+ offset
+ i
);
181 static void vm_set(struct virtio_device
*vdev
, unsigned offset
,
182 const void *buf
, unsigned len
)
184 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
188 for (i
= 0; i
< len
; i
++)
189 writeb(ptr
[i
], vm_dev
->base
+ VIRTIO_MMIO_CONFIG
+ offset
+ i
);
192 static u8
vm_get_status(struct virtio_device
*vdev
)
194 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
196 return readl(vm_dev
->base
+ VIRTIO_MMIO_STATUS
) & 0xff;
199 static void vm_set_status(struct virtio_device
*vdev
, u8 status
)
201 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
203 /* We should never be setting status to 0. */
206 writel(status
, vm_dev
->base
+ VIRTIO_MMIO_STATUS
);
209 static void vm_reset(struct virtio_device
*vdev
)
211 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
213 /* 0 status means a reset. */
214 writel(0, vm_dev
->base
+ VIRTIO_MMIO_STATUS
);
219 /* Transport interface */
221 /* the notify function used when creating a virt queue */
222 static void vm_notify(struct virtqueue
*vq
)
224 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vq
->vdev
);
226 /* We write the queue's selector into the notification register to
227 * signal the other end */
228 writel(vq
->index
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_NOTIFY
);
231 /* Notify all virtqueues on an interrupt. */
232 static irqreturn_t
vm_interrupt(int irq
, void *opaque
)
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
;
240 irqreturn_t ret
= IRQ_NONE
;
242 /* Read and acknowledge interrupts */
243 status
= readl(vm_dev
->base
+ VIRTIO_MMIO_INTERRUPT_STATUS
);
244 writel(status
, vm_dev
->base
+ VIRTIO_MMIO_INTERRUPT_ACK
);
246 if (unlikely(status
& VIRTIO_MMIO_INT_CONFIG
)
247 && vdrv
&& vdrv
->config_changed
) {
248 vdrv
->config_changed(&vm_dev
->vdev
);
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
);
264 static void vm_del_vq(struct virtqueue
*vq
)
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
;
271 spin_lock_irqsave(&vm_dev
->lock
, flags
);
272 list_del(&info
->node
);
273 spin_unlock_irqrestore(&vm_dev
->lock
, flags
);
275 vring_del_virtqueue(vq
);
277 /* Select and deactivate the queue */
278 writel(index
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_SEL
);
279 writel(0, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_PFN
);
281 size
= PAGE_ALIGN(vring_size(info
->num
, VIRTIO_MMIO_VRING_ALIGN
));
282 free_pages_exact(info
->queue
, size
);
286 static void vm_del_vqs(struct virtio_device
*vdev
)
288 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
289 struct virtqueue
*vq
, *n
;
291 list_for_each_entry_safe(vq
, n
, &vdev
->vqs
, list
)
294 free_irq(platform_get_irq(vm_dev
->pdev
, 0), vm_dev
);
299 static struct virtqueue
*vm_setup_vq(struct virtio_device
*vdev
, unsigned index
,
300 void (*callback
)(struct virtqueue
*vq
),
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
;
312 /* Select the queue we're interested in */
313 writel(index
, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_SEL
);
315 /* Queue shouldn't already be set up. */
316 if (readl(vm_dev
->base
+ VIRTIO_MMIO_QUEUE_PFN
)) {
318 goto error_available
;
321 /* Allocate and fill out our active queue description */
322 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
328 /* Allocate pages for the queue - start with a queue as big as
329 * possible (limited by maximum size allowed by device), drop down
330 * to a minimal size, just big enough to fit descriptor table
331 * and two rings (which makes it "alignment_size * 2")
333 info
->num
= readl(vm_dev
->base
+ VIRTIO_MMIO_QUEUE_NUM_MAX
);
335 /* If the device reports a 0 entry queue, we won't be able to
336 * use it to perform I/O, and vring_new_virtqueue() can't create
337 * empty queues anyway, so don't bother to set up the device.
339 if (info
->num
== 0) {
341 goto error_alloc_pages
;
345 size
= PAGE_ALIGN(vring_size(info
->num
,
346 VIRTIO_MMIO_VRING_ALIGN
));
347 /* Did the last iter shrink the queue below minimum size? */
348 if (size
< VIRTIO_MMIO_VRING_ALIGN
* 2) {
350 goto error_alloc_pages
;
353 info
->queue
= alloc_pages_exact(size
, GFP_KERNEL
| __GFP_ZERO
);
360 /* Activate the queue */
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
);
367 /* Create the vring */
368 vq
= vring_new_virtqueue(index
, info
->num
, VIRTIO_MMIO_VRING_ALIGN
, vdev
,
369 true, info
->queue
, vm_notify
, callback
, name
);
372 goto error_new_virtqueue
;
378 spin_lock_irqsave(&vm_dev
->lock
, flags
);
379 list_add(&info
->node
, &vm_dev
->virtqueues
);
380 spin_unlock_irqrestore(&vm_dev
->lock
, flags
);
385 writel(0, vm_dev
->base
+ VIRTIO_MMIO_QUEUE_PFN
);
386 free_pages_exact(info
->queue
, size
);
394 static int vm_find_vqs(struct virtio_device
*vdev
, unsigned nvqs
,
395 struct virtqueue
*vqs
[],
396 vq_callback_t
*callbacks
[],
399 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
400 unsigned int irq
= platform_get_irq(vm_dev
->pdev
, 0);
403 err
= request_irq(irq
, vm_interrupt
, IRQF_SHARED
,
404 dev_name(&vdev
->dev
), vm_dev
);
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
])) {
412 return PTR_ERR(vqs
[i
]);
419 static const char *vm_bus_name(struct virtio_device
*vdev
)
421 struct virtio_mmio_device
*vm_dev
= to_virtio_mmio_device(vdev
);
423 return vm_dev
->pdev
->name
;
426 static struct virtio_config_ops virtio_mmio_config_ops
= {
429 .get_status
= vm_get_status
,
430 .set_status
= vm_set_status
,
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
,
441 /* Platform device */
443 static int __devinit
virtio_mmio_probe(struct platform_device
*pdev
)
445 struct virtio_mmio_device
*vm_dev
;
446 struct resource
*mem
;
449 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
453 if (!devm_request_mem_region(&pdev
->dev
, mem
->start
,
454 resource_size(mem
), pdev
->name
))
457 vm_dev
= devm_kzalloc(&pdev
->dev
, sizeof(*vm_dev
), GFP_KERNEL
);
461 vm_dev
->vdev
.dev
.parent
= &pdev
->dev
;
462 vm_dev
->vdev
.config
= &virtio_mmio_config_ops
;
464 INIT_LIST_HEAD(&vm_dev
->virtqueues
);
465 spin_lock_init(&vm_dev
->lock
);
467 vm_dev
->base
= devm_ioremap(&pdev
->dev
, mem
->start
, resource_size(mem
));
468 if (vm_dev
->base
== NULL
)
471 /* Check magic value */
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
);
478 /* Check device version */
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",
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
);
489 writel(PAGE_SIZE
, vm_dev
->base
+ VIRTIO_MMIO_GUEST_PAGE_SIZE
);
491 platform_set_drvdata(pdev
, vm_dev
);
493 return register_virtio_device(&vm_dev
->vdev
);
496 static int __devexit
virtio_mmio_remove(struct platform_device
*pdev
)
498 struct virtio_mmio_device
*vm_dev
= platform_get_drvdata(pdev
);
500 unregister_virtio_device(&vm_dev
->vdev
);
507 /* Devices list parameter */
509 #if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
511 static struct device vm_cmdline_parent
= {
512 .init_name
= "virtio-mmio-cmdline",
515 static int vm_cmdline_parent_registered
;
516 static int vm_cmdline_id
;
518 static int vm_cmdline_set(const char *device
,
519 const struct kernel_param
*kp
)
522 struct resource resources
[2] = {};
524 long long int base
, size
;
526 int processed
, consumed
= 0;
527 struct platform_device
*pdev
;
529 /* Consume "size" part of the command line parameter */
530 size
= memparse(device
, &str
);
532 /* Get "@<base>:<irq>[:<id>]" chunks */
533 processed
= sscanf(str
, "@%lli:%u%n:%d%n",
534 &base
, &irq
, &consumed
,
535 &vm_cmdline_id
, &consumed
);
538 * sscanf() must processes at least 2 chunks; also there
539 * must be no extra characters after the last chunk, so
540 * str[consumed] must be '\0'
542 if (processed
< 2 || str
[consumed
])
545 resources
[0].flags
= IORESOURCE_MEM
;
546 resources
[0].start
= base
;
547 resources
[0].end
= base
+ size
- 1;
549 resources
[1].flags
= IORESOURCE_IRQ
;
550 resources
[1].start
= resources
[1].end
= irq
;
552 if (!vm_cmdline_parent_registered
) {
553 err
= device_register(&vm_cmdline_parent
);
555 pr_err("Failed to register parent device!\n");
558 vm_cmdline_parent_registered
= 1;
561 pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
563 (unsigned long long)resources
[0].start
,
564 (unsigned long long)resources
[0].end
,
565 (int)resources
[1].start
);
567 pdev
= platform_device_register_resndata(&vm_cmdline_parent
,
568 "virtio-mmio", vm_cmdline_id
++,
569 resources
, ARRAY_SIZE(resources
), NULL
, 0);
571 return PTR_ERR(pdev
);
576 static int vm_cmdline_get_device(struct device
*dev
, void *data
)
579 unsigned int len
= strlen(buffer
);
580 struct platform_device
*pdev
= to_platform_device(dev
);
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
,
590 static int vm_cmdline_get(char *buffer
, const struct kernel_param
*kp
)
593 device_for_each_child(&vm_cmdline_parent
, buffer
,
594 vm_cmdline_get_device
);
595 return strlen(buffer
) + 1;
598 static struct kernel_param_ops vm_cmdline_param_ops
= {
599 .set
= vm_cmdline_set
,
600 .get
= vm_cmdline_get
,
603 device_param_cb(device
, &vm_cmdline_param_ops
, NULL
, S_IRUSR
);
605 static int vm_unregister_cmdline_device(struct device
*dev
,
608 platform_device_unregister(to_platform_device(dev
));
613 static void vm_unregister_cmdline_devices(void)
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;
625 static void vm_unregister_cmdline_devices(void)
631 /* Platform driver */
633 static struct of_device_id virtio_mmio_match
[] = {
634 { .compatible
= "virtio,mmio", },
637 MODULE_DEVICE_TABLE(of
, virtio_mmio_match
);
639 static struct platform_driver virtio_mmio_driver
= {
640 .probe
= virtio_mmio_probe
,
641 .remove
= __devexit_p(virtio_mmio_remove
),
643 .name
= "virtio-mmio",
644 .owner
= THIS_MODULE
,
645 .of_match_table
= virtio_mmio_match
,
649 static int __init
virtio_mmio_init(void)
651 return platform_driver_register(&virtio_mmio_driver
);
654 static void __exit
virtio_mmio_exit(void)
656 platform_driver_unregister(&virtio_mmio_driver
);
657 vm_unregister_cmdline_devices();
660 module_init(virtio_mmio_init
);
661 module_exit(virtio_mmio_exit
);
663 MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
664 MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
665 MODULE_LICENSE("GPL");