]> Pileus Git - ~andy/linux/blob - drivers/virtio/virtio_mmio.c
008bf58bdaae538e5598327f3f27c40327d7e6f0
[~andy/linux] / drivers / virtio / virtio_mmio.c
1 /*
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  *
9  * 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  *
53  * 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
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 /* 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
115 struct 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
127 struct 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 virtual address of the ring queue */
135         void *queue;
136
137         /* the list node for the virtqueues list */
138         struct list_head node;
139 };
140
141
142
143 /* Configuration interface */
144
145 static u32 vm_get_features(struct virtio_device *vdev)
146 {
147         struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
148
149         /* TODO: Features > 32 bits */
150         writel(0, vm_dev->base + VIRTIO_MMIO_HOST_FEATURES_SEL);
151
152         return readl(vm_dev->base + VIRTIO_MMIO_HOST_FEATURES);
153 }
154
155 static 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         /* Give virtio_ring a chance to accept features. */
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
170 static 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
181 static 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
192 static 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
199 static 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         /* We should never be setting status to 0. */
204         BUG_ON(status == 0);
205
206         writel(status, vm_dev->base + VIRTIO_MMIO_STATUS);
207 }
208
209 static void vm_reset(struct virtio_device *vdev)
210 {
211         struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
212
213         /* 0 status means a reset. */
214         writel(0, vm_dev->base + VIRTIO_MMIO_STATUS);
215 }
216
217
218
219 /* Transport interface */
220
221 /* the notify function used when creating a virt queue */
222 static void vm_notify(struct virtqueue *vq)
223 {
224         struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
225
226         /* We write the queue's selector into the notification register to
227          * signal the other end */
228         writel(virtqueue_get_queue_index(vq), vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
229 }
230
231 /* Notify all virtqueues on an interrupt. */
232 static 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         /* Read and acknowledge interrupts */
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
264 static 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 = virtqueue_get_queue_index(vq);
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         /* 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);
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
286 static 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
299 static 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         /* Select the queue we're interested in */
310         writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
311
312         /* Queue shouldn't already be set up. */
313         if (readl(vm_dev->base + VIRTIO_MMIO_QUEUE_PFN)) {
314                 err = -ENOENT;
315                 goto error_available;
316         }
317
318         /* Allocate and fill out our active queue description */
319         info = kmalloc(sizeof(*info), GFP_KERNEL);
320         if (!info) {
321                 err = -ENOMEM;
322                 goto error_kmalloc;
323         }
324
325         /* Allocate pages for the queue - start with a queue as big as
326          * possible (limited by maximum size allowed by device), drop down
327          * to a minimal size, just big enough to fit descriptor table
328          * and two rings (which makes it "alignment_size * 2")
329          */
330         info->num = readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX);
331         while (1) {
332                 size = PAGE_ALIGN(vring_size(info->num,
333                                 VIRTIO_MMIO_VRING_ALIGN));
334                 /* Already smallest possible allocation? */
335                 if (size <= VIRTIO_MMIO_VRING_ALIGN * 2) {
336                         err = -ENOMEM;
337                         goto error_alloc_pages;
338                 }
339
340                 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
341                 if (info->queue)
342                         break;
343
344                 info->num /= 2;
345         }
346
347         /* Activate the queue */
348         writel(info->num, vm_dev->base + VIRTIO_MMIO_QUEUE_NUM);
349         writel(VIRTIO_MMIO_VRING_ALIGN,
350                         vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN);
351         writel(virt_to_phys(info->queue) >> PAGE_SHIFT,
352                         vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
353
354         /* Create the vring */
355         vq = vring_new_virtqueue(index, info->num, VIRTIO_MMIO_VRING_ALIGN, vdev,
356                                  true, info->queue, vm_notify, callback, name);
357         if (!vq) {
358                 err = -ENOMEM;
359                 goto error_new_virtqueue;
360         }
361
362         vq->priv = info;
363         info->vq = vq;
364
365         spin_lock_irqsave(&vm_dev->lock, flags);
366         list_add(&info->node, &vm_dev->virtqueues);
367         spin_unlock_irqrestore(&vm_dev->lock, flags);
368
369         return vq;
370
371 error_new_virtqueue:
372         writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
373         free_pages_exact(info->queue, size);
374 error_alloc_pages:
375         kfree(info);
376 error_kmalloc:
377 error_available:
378         return ERR_PTR(err);
379 }
380
381 static int vm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
382                        struct virtqueue *vqs[],
383                        vq_callback_t *callbacks[],
384                        const char *names[])
385 {
386         struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
387         unsigned int irq = platform_get_irq(vm_dev->pdev, 0);
388         int i, err;
389
390         err = request_irq(irq, vm_interrupt, IRQF_SHARED,
391                         dev_name(&vdev->dev), vm_dev);
392         if (err)
393                 return err;
394
395         for (i = 0; i < nvqs; ++i) {
396                 vqs[i] = vm_setup_vq(vdev, i, callbacks[i], names[i]);
397                 if (IS_ERR(vqs[i])) {
398                         vm_del_vqs(vdev);
399                         return PTR_ERR(vqs[i]);
400                 }
401         }
402
403         return 0;
404 }
405
406 static const char *vm_bus_name(struct virtio_device *vdev)
407 {
408         struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
409
410         return vm_dev->pdev->name;
411 }
412
413 static struct virtio_config_ops virtio_mmio_config_ops = {
414         .get            = vm_get,
415         .set            = vm_set,
416         .get_status     = vm_get_status,
417         .set_status     = vm_set_status,
418         .reset          = vm_reset,
419         .find_vqs       = vm_find_vqs,
420         .del_vqs        = vm_del_vqs,
421         .get_features   = vm_get_features,
422         .finalize_features = vm_finalize_features,
423         .bus_name       = vm_bus_name,
424 };
425
426
427
428 /* Platform device */
429
430 static int __devinit virtio_mmio_probe(struct platform_device *pdev)
431 {
432         struct virtio_mmio_device *vm_dev;
433         struct resource *mem;
434         unsigned long magic;
435
436         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
437         if (!mem)
438                 return -EINVAL;
439
440         if (!devm_request_mem_region(&pdev->dev, mem->start,
441                         resource_size(mem), pdev->name))
442                 return -EBUSY;
443
444         vm_dev = devm_kzalloc(&pdev->dev, sizeof(*vm_dev), GFP_KERNEL);
445         if (!vm_dev)
446                 return  -ENOMEM;
447
448         vm_dev->vdev.dev.parent = &pdev->dev;
449         vm_dev->vdev.config = &virtio_mmio_config_ops;
450         vm_dev->pdev = pdev;
451         INIT_LIST_HEAD(&vm_dev->virtqueues);
452         spin_lock_init(&vm_dev->lock);
453
454         vm_dev->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
455         if (vm_dev->base == NULL)
456                 return -EFAULT;
457
458         /* Check magic value */
459         magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE);
460         if (memcmp(&magic, "virt", 4) != 0) {
461                 dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
462                 return -ENODEV;
463         }
464
465         /* Check device version */
466         vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION);
467         if (vm_dev->version != 1) {
468                 dev_err(&pdev->dev, "Version %ld not supported!\n",
469                                 vm_dev->version);
470                 return -ENXIO;
471         }
472
473         vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID);
474         vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
475
476         writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
477
478         platform_set_drvdata(pdev, vm_dev);
479
480         return register_virtio_device(&vm_dev->vdev);
481 }
482
483 static int __devexit virtio_mmio_remove(struct platform_device *pdev)
484 {
485         struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev);
486
487         unregister_virtio_device(&vm_dev->vdev);
488
489         return 0;
490 }
491
492
493
494 /* Devices list parameter */
495
496 #if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
497
498 static struct device vm_cmdline_parent = {
499         .init_name = "virtio-mmio-cmdline",
500 };
501
502 static int vm_cmdline_parent_registered;
503 static int vm_cmdline_id;
504
505 static int vm_cmdline_set(const char *device,
506                 const struct kernel_param *kp)
507 {
508         int err;
509         struct resource resources[2] = {};
510         char *str;
511         long long int base;
512         int processed, consumed = 0;
513         struct platform_device *pdev;
514
515         resources[0].flags = IORESOURCE_MEM;
516         resources[1].flags = IORESOURCE_IRQ;
517
518         resources[0].end = memparse(device, &str) - 1;
519
520         processed = sscanf(str, "@%lli:%u%n:%d%n",
521                         &base, &resources[1].start, &consumed,
522                         &vm_cmdline_id, &consumed);
523
524         if (processed < 2 || processed > 3 || str[consumed])
525                 return -EINVAL;
526
527         resources[0].start = base;
528         resources[0].end += base;
529         resources[1].end = resources[1].start;
530
531         if (!vm_cmdline_parent_registered) {
532                 err = device_register(&vm_cmdline_parent);
533                 if (err) {
534                         pr_err("Failed to register parent device!\n");
535                         return err;
536                 }
537                 vm_cmdline_parent_registered = 1;
538         }
539
540         pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
541                        vm_cmdline_id,
542                        (unsigned long long)resources[0].start,
543                        (unsigned long long)resources[0].end,
544                        (int)resources[1].start);
545
546         pdev = platform_device_register_resndata(&vm_cmdline_parent,
547                         "virtio-mmio", vm_cmdline_id++,
548                         resources, ARRAY_SIZE(resources), NULL, 0);
549         if (IS_ERR(pdev))
550                 return PTR_ERR(pdev);
551
552         return 0;
553 }
554
555 static int vm_cmdline_get_device(struct device *dev, void *data)
556 {
557         char *buffer = data;
558         unsigned int len = strlen(buffer);
559         struct platform_device *pdev = to_platform_device(dev);
560
561         snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n",
562                         pdev->resource[0].end - pdev->resource[0].start + 1ULL,
563                         (unsigned long long)pdev->resource[0].start,
564                         (unsigned long long)pdev->resource[1].start,
565                         pdev->id);
566         return 0;
567 }
568
569 static int vm_cmdline_get(char *buffer, const struct kernel_param *kp)
570 {
571         buffer[0] = '\0';
572         device_for_each_child(&vm_cmdline_parent, buffer,
573                         vm_cmdline_get_device);
574         return strlen(buffer) + 1;
575 }
576
577 static struct kernel_param_ops vm_cmdline_param_ops = {
578         .set = vm_cmdline_set,
579         .get = vm_cmdline_get,
580 };
581
582 device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR);
583
584 static int vm_unregister_cmdline_device(struct device *dev,
585                 void *data)
586 {
587         platform_device_unregister(to_platform_device(dev));
588
589         return 0;
590 }
591
592 static void vm_unregister_cmdline_devices(void)
593 {
594         if (vm_cmdline_parent_registered) {
595                 device_for_each_child(&vm_cmdline_parent, NULL,
596                                 vm_unregister_cmdline_device);
597                 device_unregister(&vm_cmdline_parent);
598                 vm_cmdline_parent_registered = 0;
599         }
600 }
601
602 #else
603
604 static void vm_unregister_cmdline_devices(void)
605 {
606 }
607
608 #endif
609
610 /* Platform driver */
611
612 static struct of_device_id virtio_mmio_match[] = {
613         { .compatible = "virtio,mmio", },
614         {},
615 };
616 MODULE_DEVICE_TABLE(of, virtio_mmio_match);
617
618 static struct platform_driver virtio_mmio_driver = {
619         .probe          = virtio_mmio_probe,
620         .remove         = __devexit_p(virtio_mmio_remove),
621         .driver         = {
622                 .name   = "virtio-mmio",
623                 .owner  = THIS_MODULE,
624                 .of_match_table = virtio_mmio_match,
625         },
626 };
627
628 static int __init virtio_mmio_init(void)
629 {
630         return platform_driver_register(&virtio_mmio_driver);
631 }
632
633 static void __exit virtio_mmio_exit(void)
634 {
635         platform_driver_unregister(&virtio_mmio_driver);
636         vm_unregister_cmdline_devices();
637 }
638
639 module_init(virtio_mmio_init);
640 module_exit(virtio_mmio_exit);
641
642 MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
643 MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
644 MODULE_LICENSE("GPL");