blob: 453db0c403d807dbd0f2482f9d8dfc7c77f2f57e [file] [log] [blame]
Pawel Molledfd52e2011-10-24 14:07:03 +01001/*
2 * Virtio memory mapped device driver
3 *
4 * Copyright 2011, ARM Ltd.
5 *
6 * This module allows virtio devices to be used over a virtual, memory mapped
7 * platform device.
8 *
Pawel Moll81a054c2012-05-09 18:30:16 +01009 * The guest device(s) may be instantiated in one of three equivalent ways:
10 *
11 * 1. Static platform device in board's code, eg.:
12 *
13 * static struct platform_device v2m_virtio_device = {
14 * .name = "virtio-mmio",
15 * .id = -1,
16 * .num_resources = 2,
17 * .resource = (struct resource []) {
18 * {
19 * .start = 0x1001e000,
20 * .end = 0x1001e0ff,
21 * .flags = IORESOURCE_MEM,
22 * }, {
23 * .start = 42 + 32,
24 * .end = 42 + 32,
25 * .flags = IORESOURCE_IRQ,
26 * },
27 * }
28 * };
29 *
30 * 2. Device Tree node, eg.:
31 *
32 * virtio_block@1e000 {
33 * compatible = "virtio,mmio";
34 * reg = <0x1e000 0x100>;
35 * interrupts = <42>;
36 * }
37 *
38 * 3. Kernel module (or command line) parameter. Can be used more than once -
39 * one device will be created for each one. Syntax:
40 *
41 * [virtio_mmio.]device=<size>@<baseaddr>:<irq>[:<id>]
42 * where:
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
47 * eg.:
48 * virtio_mmio.device=0x100@0x100b0000:48 \
49 * virtio_mmio.device=1K@0x1001e000:74
50 *
51 *
52 *
Pawel Molledfd52e2011-10-24 14:07:03 +010053 * Registers layout (all 32-bit wide):
54 *
55 * offset d. name description
56 * ------ -- ---------------- -----------------
57 *
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
62 *
63 * 0x010 R HostFeatures Features supported by the host
64 * 0x014 W HostFeaturesSel Set of host features to access via HostFeatures
65 *
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
69 *
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
75 *
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
80 *
81 * 0x100+ RW Device-specific configuration space
82 *
83 * Based on Virtio PCI driver by Anthony Liguori, copyright IBM Corp. 2007
84 *
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.
87 */
88
Pawel Moll81a054c2012-05-09 18:30:16 +010089#define pr_fmt(fmt) "virtio-mmio: " fmt
90
Pawel Molledfd52e2011-10-24 14:07:03 +010091#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/* 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
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 /* a list of queues so we can dispatch IRQs */
123 spinlock_t lock;
124 struct list_head virtqueues;
125};
126
127struct virtio_mmio_vq_info {
128 /* the actual virtqueue */
129 struct virtqueue *vq;
130
131 /* the number of entries in the queue */
132 unsigned int num;
133
134 /* the index of the queue */
135 int queue_index;
136
137 /* the virtual address of the ring queue */
138 void *queue;
139
140 /* the list node for the virtqueues list */
141 struct list_head node;
142};
143
144
145
146/* Configuration interface */
147
148static u32 vm_get_features(struct virtio_device *vdev)
149{
150 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
151
152 /* TODO: Features > 32 bits */
153 writel(0, vm_dev->base + VIRTIO_MMIO_HOST_FEATURES_SEL);
154
155 return readl(vm_dev->base + VIRTIO_MMIO_HOST_FEATURES);
156}
157
158static void vm_finalize_features(struct virtio_device *vdev)
159{
160 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
161 int i;
162
163 /* Give virtio_ring a chance to accept features. */
164 vring_transport_features(vdev);
165
166 for (i = 0; i < ARRAY_SIZE(vdev->features); i++) {
Sasha Levinfe1a7fe2011-11-15 16:17:18 +0200167 writel(i, vm_dev->base + VIRTIO_MMIO_GUEST_FEATURES_SEL);
Pawel Molledfd52e2011-10-24 14:07:03 +0100168 writel(vdev->features[i],
169 vm_dev->base + VIRTIO_MMIO_GUEST_FEATURES);
170 }
171}
172
173static void vm_get(struct virtio_device *vdev, unsigned offset,
174 void *buf, unsigned len)
175{
176 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
177 u8 *ptr = buf;
178 int i;
179
180 for (i = 0; i < len; i++)
181 ptr[i] = readb(vm_dev->base + VIRTIO_MMIO_CONFIG + offset + i);
182}
183
184static void vm_set(struct virtio_device *vdev, unsigned offset,
185 const void *buf, unsigned len)
186{
187 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
188 const u8 *ptr = buf;
189 int i;
190
191 for (i = 0; i < len; i++)
192 writeb(ptr[i], vm_dev->base + VIRTIO_MMIO_CONFIG + offset + i);
193}
194
195static u8 vm_get_status(struct virtio_device *vdev)
196{
197 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
198
199 return readl(vm_dev->base + VIRTIO_MMIO_STATUS) & 0xff;
200}
201
202static void vm_set_status(struct virtio_device *vdev, u8 status)
203{
204 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
205
206 /* We should never be setting status to 0. */
207 BUG_ON(status == 0);
208
209 writel(status, vm_dev->base + VIRTIO_MMIO_STATUS);
210}
211
212static void vm_reset(struct virtio_device *vdev)
213{
214 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
215
216 /* 0 status means a reset. */
217 writel(0, vm_dev->base + VIRTIO_MMIO_STATUS);
218}
219
220
221
222/* Transport interface */
223
224/* the notify function used when creating a virt queue */
225static void vm_notify(struct virtqueue *vq)
226{
227 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
228 struct virtio_mmio_vq_info *info = vq->priv;
229
230 /* We write the queue's selector into the notification register to
231 * signal the other end */
232 writel(info->queue_index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
233}
234
235/* Notify all virtqueues on an interrupt. */
236static irqreturn_t vm_interrupt(int irq, void *opaque)
237{
238 struct virtio_mmio_device *vm_dev = opaque;
239 struct virtio_mmio_vq_info *info;
240 struct virtio_driver *vdrv = container_of(vm_dev->vdev.dev.driver,
241 struct virtio_driver, driver);
242 unsigned long status;
243 unsigned long flags;
244 irqreturn_t ret = IRQ_NONE;
245
246 /* Read and acknowledge interrupts */
247 status = readl(vm_dev->base + VIRTIO_MMIO_INTERRUPT_STATUS);
248 writel(status, vm_dev->base + VIRTIO_MMIO_INTERRUPT_ACK);
249
250 if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)
251 && vdrv && vdrv->config_changed) {
252 vdrv->config_changed(&vm_dev->vdev);
253 ret = IRQ_HANDLED;
254 }
255
256 if (likely(status & VIRTIO_MMIO_INT_VRING)) {
257 spin_lock_irqsave(&vm_dev->lock, flags);
258 list_for_each_entry(info, &vm_dev->virtqueues, node)
259 ret |= vring_interrupt(irq, info->vq);
260 spin_unlock_irqrestore(&vm_dev->lock, flags);
261 }
262
263 return ret;
264}
265
266
267
268static void vm_del_vq(struct virtqueue *vq)
269{
270 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
271 struct virtio_mmio_vq_info *info = vq->priv;
272 unsigned long flags, size;
273
274 spin_lock_irqsave(&vm_dev->lock, flags);
275 list_del(&info->node);
276 spin_unlock_irqrestore(&vm_dev->lock, flags);
277
278 vring_del_virtqueue(vq);
279
280 /* Select and deactivate the queue */
281 writel(info->queue_index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
282 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
283
284 size = PAGE_ALIGN(vring_size(info->num, VIRTIO_MMIO_VRING_ALIGN));
285 free_pages_exact(info->queue, size);
286 kfree(info);
287}
288
289static void vm_del_vqs(struct virtio_device *vdev)
290{
291 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
292 struct virtqueue *vq, *n;
293
294 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
295 vm_del_vq(vq);
296
297 free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev);
298}
299
300
301
302static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned index,
303 void (*callback)(struct virtqueue *vq),
304 const char *name)
305{
306 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
307 struct virtio_mmio_vq_info *info;
308 struct virtqueue *vq;
309 unsigned long flags, size;
310 int err;
311
312 /* Select the queue we're interested in */
313 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
314
315 /* Queue shouldn't already be set up. */
316 if (readl(vm_dev->base + VIRTIO_MMIO_QUEUE_PFN)) {
317 err = -ENOENT;
318 goto error_available;
319 }
320
321 /* Allocate and fill out our active queue description */
322 info = kmalloc(sizeof(*info), GFP_KERNEL);
323 if (!info) {
324 err = -ENOMEM;
325 goto error_kmalloc;
326 }
327 info->queue_index = index;
328
329 /* Allocate pages for the queue - start with a queue as big as
330 * possible (limited by maximum size allowed by device), drop down
331 * to a minimal size, just big enough to fit descriptor table
332 * and two rings (which makes it "alignment_size * 2")
333 */
334 info->num = readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX);
335 while (1) {
336 size = PAGE_ALIGN(vring_size(info->num,
337 VIRTIO_MMIO_VRING_ALIGN));
338 /* Already smallest possible allocation? */
339 if (size <= VIRTIO_MMIO_VRING_ALIGN * 2) {
340 err = -ENOMEM;
341 goto error_alloc_pages;
342 }
343
344 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
345 if (info->queue)
346 break;
347
348 info->num /= 2;
349 }
350
351 /* Activate the queue */
352 writel(info->num, vm_dev->base + VIRTIO_MMIO_QUEUE_NUM);
353 writel(VIRTIO_MMIO_VRING_ALIGN,
354 vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN);
355 writel(virt_to_phys(info->queue) >> PAGE_SHIFT,
356 vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
357
358 /* Create the vring */
Rusty Russell7b21e342012-01-12 15:44:42 +1030359 vq = vring_new_virtqueue(info->num, VIRTIO_MMIO_VRING_ALIGN, vdev,
360 true, info->queue, vm_notify, callback, name);
Pawel Molledfd52e2011-10-24 14:07:03 +0100361 if (!vq) {
362 err = -ENOMEM;
363 goto error_new_virtqueue;
364 }
365
366 vq->priv = info;
367 info->vq = vq;
368
369 spin_lock_irqsave(&vm_dev->lock, flags);
370 list_add(&info->node, &vm_dev->virtqueues);
371 spin_unlock_irqrestore(&vm_dev->lock, flags);
372
373 return vq;
374
375error_new_virtqueue:
376 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
377 free_pages_exact(info->queue, size);
378error_alloc_pages:
379 kfree(info);
380error_kmalloc:
381error_available:
382 return ERR_PTR(err);
383}
384
385static int vm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
386 struct virtqueue *vqs[],
387 vq_callback_t *callbacks[],
388 const char *names[])
389{
390 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
391 unsigned int irq = platform_get_irq(vm_dev->pdev, 0);
392 int i, err;
393
394 err = request_irq(irq, vm_interrupt, IRQF_SHARED,
395 dev_name(&vdev->dev), vm_dev);
396 if (err)
397 return err;
398
399 for (i = 0; i < nvqs; ++i) {
400 vqs[i] = vm_setup_vq(vdev, i, callbacks[i], names[i]);
401 if (IS_ERR(vqs[i])) {
402 vm_del_vqs(vdev);
403 return PTR_ERR(vqs[i]);
404 }
405 }
406
407 return 0;
408}
409
Rick Jones66846042011-11-14 14:17:08 +0000410static const char *vm_bus_name(struct virtio_device *vdev)
411{
412 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100413
Rick Jones66846042011-11-14 14:17:08 +0000414 return vm_dev->pdev->name;
415}
Pawel Molledfd52e2011-10-24 14:07:03 +0100416
417static struct virtio_config_ops virtio_mmio_config_ops = {
418 .get = vm_get,
419 .set = vm_set,
420 .get_status = vm_get_status,
421 .set_status = vm_set_status,
422 .reset = vm_reset,
423 .find_vqs = vm_find_vqs,
424 .del_vqs = vm_del_vqs,
425 .get_features = vm_get_features,
426 .finalize_features = vm_finalize_features,
Rick Jones66846042011-11-14 14:17:08 +0000427 .bus_name = vm_bus_name,
Pawel Molledfd52e2011-10-24 14:07:03 +0100428};
429
430
431
432/* Platform device */
433
434static int __devinit virtio_mmio_probe(struct platform_device *pdev)
435{
436 struct virtio_mmio_device *vm_dev;
437 struct resource *mem;
438 unsigned long magic;
439
440 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
441 if (!mem)
442 return -EINVAL;
443
444 if (!devm_request_mem_region(&pdev->dev, mem->start,
445 resource_size(mem), pdev->name))
446 return -EBUSY;
447
448 vm_dev = devm_kzalloc(&pdev->dev, sizeof(*vm_dev), GFP_KERNEL);
449 if (!vm_dev)
450 return -ENOMEM;
451
452 vm_dev->vdev.dev.parent = &pdev->dev;
453 vm_dev->vdev.config = &virtio_mmio_config_ops;
454 vm_dev->pdev = pdev;
455 INIT_LIST_HEAD(&vm_dev->virtqueues);
456 spin_lock_init(&vm_dev->lock);
457
458 vm_dev->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
459 if (vm_dev->base == NULL)
460 return -EFAULT;
461
462 /* Check magic value */
463 magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE);
464 if (memcmp(&magic, "virt", 4) != 0) {
465 dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
466 return -ENODEV;
467 }
468
469 /* Check device version */
470 vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION);
471 if (vm_dev->version != 1) {
472 dev_err(&pdev->dev, "Version %ld not supported!\n",
473 vm_dev->version);
474 return -ENXIO;
475 }
476
477 vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID);
478 vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
479
480 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
481
482 platform_set_drvdata(pdev, vm_dev);
483
484 return register_virtio_device(&vm_dev->vdev);
485}
486
487static int __devexit virtio_mmio_remove(struct platform_device *pdev)
488{
489 struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev);
490
491 unregister_virtio_device(&vm_dev->vdev);
492
493 return 0;
494}
495
496
497
Pawel Moll81a054c2012-05-09 18:30:16 +0100498/* Devices list parameter */
499
500#if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
501
502static struct device vm_cmdline_parent = {
503 .init_name = "virtio-mmio-cmdline",
504};
505
506static int vm_cmdline_parent_registered;
507static int vm_cmdline_id;
508
509static int vm_cmdline_set(const char *device,
510 const struct kernel_param *kp)
511{
512 int err;
513 struct resource resources[2] = {};
514 char *str;
515 long long int base;
516 int processed, consumed = 0;
517 struct platform_device *pdev;
518
519 resources[0].flags = IORESOURCE_MEM;
520 resources[1].flags = IORESOURCE_IRQ;
521
522 resources[0].end = memparse(device, &str) - 1;
523
524 processed = sscanf(str, "@%lli:%u%n:%d%n",
525 &base, &resources[1].start, &consumed,
526 &vm_cmdline_id, &consumed);
527
528 if (processed < 2 || processed > 3 || str[consumed])
529 return -EINVAL;
530
531 resources[0].start = base;
532 resources[0].end += base;
533 resources[1].end = resources[1].start;
534
535 if (!vm_cmdline_parent_registered) {
536 err = device_register(&vm_cmdline_parent);
537 if (err) {
538 pr_err("Failed to register parent device!\n");
539 return err;
540 }
541 vm_cmdline_parent_registered = 1;
542 }
543
544 pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
545 vm_cmdline_id,
546 (unsigned long long)resources[0].start,
547 (unsigned long long)resources[0].end,
548 (int)resources[1].start);
549
550 pdev = platform_device_register_resndata(&vm_cmdline_parent,
551 "virtio-mmio", vm_cmdline_id++,
552 resources, ARRAY_SIZE(resources), NULL, 0);
553 if (IS_ERR(pdev))
554 return PTR_ERR(pdev);
555
556 return 0;
557}
558
559static int vm_cmdline_get_device(struct device *dev, void *data)
560{
561 char *buffer = data;
562 unsigned int len = strlen(buffer);
563 struct platform_device *pdev = to_platform_device(dev);
564
565 snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n",
566 pdev->resource[0].end - pdev->resource[0].start + 1ULL,
567 (unsigned long long)pdev->resource[0].start,
568 (unsigned long long)pdev->resource[1].start,
569 pdev->id);
570 return 0;
571}
572
573static int vm_cmdline_get(char *buffer, const struct kernel_param *kp)
574{
575 buffer[0] = '\0';
576 device_for_each_child(&vm_cmdline_parent, buffer,
577 vm_cmdline_get_device);
578 return strlen(buffer) + 1;
579}
580
581static struct kernel_param_ops vm_cmdline_param_ops = {
582 .set = vm_cmdline_set,
583 .get = vm_cmdline_get,
584};
585
586device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR);
587
588static int vm_unregister_cmdline_device(struct device *dev,
589 void *data)
590{
591 platform_device_unregister(to_platform_device(dev));
592
593 return 0;
594}
595
596static void vm_unregister_cmdline_devices(void)
597{
598 if (vm_cmdline_parent_registered) {
599 device_for_each_child(&vm_cmdline_parent, NULL,
600 vm_unregister_cmdline_device);
601 device_unregister(&vm_cmdline_parent);
602 vm_cmdline_parent_registered = 0;
603 }
604}
605
606#else
607
608static void vm_unregister_cmdline_devices(void)
609{
610}
611
612#endif
613
Pawel Molledfd52e2011-10-24 14:07:03 +0100614/* Platform driver */
615
616static struct of_device_id virtio_mmio_match[] = {
617 { .compatible = "virtio,mmio", },
618 {},
619};
620MODULE_DEVICE_TABLE(of, virtio_mmio_match);
621
622static struct platform_driver virtio_mmio_driver = {
623 .probe = virtio_mmio_probe,
624 .remove = __devexit_p(virtio_mmio_remove),
625 .driver = {
626 .name = "virtio-mmio",
627 .owner = THIS_MODULE,
628 .of_match_table = virtio_mmio_match,
629 },
630};
631
632static int __init virtio_mmio_init(void)
633{
634 return platform_driver_register(&virtio_mmio_driver);
635}
636
637static void __exit virtio_mmio_exit(void)
638{
639 platform_driver_unregister(&virtio_mmio_driver);
Pawel Moll81a054c2012-05-09 18:30:16 +0100640 vm_unregister_cmdline_devices();
Pawel Molledfd52e2011-10-24 14:07:03 +0100641}
642
643module_init(virtio_mmio_init);
644module_exit(virtio_mmio_exit);
645
646MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
647MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
648MODULE_LICENSE("GPL");