mic_virtio: robust feature array size calculation
[linux-block.git] / drivers / virtio / virtio_mmio.c
CommitLineData
edfd52e6
PM
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 *
81a054ce
PM
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 *
edfd52e6
PM
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
0d34cc2d 78 * 0x064 W InterruptACK Interrupt acknowledge register
edfd52e6
PM
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
81a054ce
PM
89#define pr_fmt(fmt) "virtio-mmio: " fmt
90
edfd52e6
PM
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
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
edfd52e6
PM
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
145static 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
155static void vm_finalize_features(struct virtio_device *vdev)
156{
157 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
edfd52e6
PM
158
159 /* Give virtio_ring a chance to accept features. */
160 vring_transport_features(vdev);
161
e16e12be
MT
162 writel(0, vm_dev->base + VIRTIO_MMIO_GUEST_FEATURES_SEL);
163 writel(vdev->features, vm_dev->base + VIRTIO_MMIO_GUEST_FEATURES);
edfd52e6
PM
164}
165
166static void vm_get(struct virtio_device *vdev, unsigned offset,
167 void *buf, unsigned len)
168{
169 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
170 u8 *ptr = buf;
171 int i;
172
173 for (i = 0; i < len; i++)
174 ptr[i] = readb(vm_dev->base + VIRTIO_MMIO_CONFIG + offset + i);
175}
176
177static void vm_set(struct virtio_device *vdev, unsigned offset,
178 const void *buf, unsigned len)
179{
180 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
181 const u8 *ptr = buf;
182 int i;
183
184 for (i = 0; i < len; i++)
185 writeb(ptr[i], vm_dev->base + VIRTIO_MMIO_CONFIG + offset + i);
186}
187
188static u8 vm_get_status(struct virtio_device *vdev)
189{
190 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
191
192 return readl(vm_dev->base + VIRTIO_MMIO_STATUS) & 0xff;
193}
194
195static void vm_set_status(struct virtio_device *vdev, u8 status)
196{
197 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
198
199 /* We should never be setting status to 0. */
200 BUG_ON(status == 0);
201
202 writel(status, vm_dev->base + VIRTIO_MMIO_STATUS);
203}
204
205static void vm_reset(struct virtio_device *vdev)
206{
207 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
208
209 /* 0 status means a reset. */
210 writel(0, vm_dev->base + VIRTIO_MMIO_STATUS);
211}
212
213
214
215/* Transport interface */
216
217/* the notify function used when creating a virt queue */
46f9c2b9 218static bool vm_notify(struct virtqueue *vq)
edfd52e6
PM
219{
220 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
edfd52e6
PM
221
222 /* We write the queue's selector into the notification register to
223 * signal the other end */
06ca287d 224 writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
46f9c2b9 225 return true;
edfd52e6
PM
226}
227
228/* Notify all virtqueues on an interrupt. */
229static irqreturn_t vm_interrupt(int irq, void *opaque)
230{
231 struct virtio_mmio_device *vm_dev = opaque;
232 struct virtio_mmio_vq_info *info;
edfd52e6
PM
233 unsigned long status;
234 unsigned long flags;
235 irqreturn_t ret = IRQ_NONE;
236
237 /* Read and acknowledge interrupts */
238 status = readl(vm_dev->base + VIRTIO_MMIO_INTERRUPT_STATUS);
239 writel(status, vm_dev->base + VIRTIO_MMIO_INTERRUPT_ACK);
240
016c98c6
MT
241 if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)) {
242 virtio_config_changed(&vm_dev->vdev);
edfd52e6
PM
243 ret = IRQ_HANDLED;
244 }
245
246 if (likely(status & VIRTIO_MMIO_INT_VRING)) {
247 spin_lock_irqsave(&vm_dev->lock, flags);
248 list_for_each_entry(info, &vm_dev->virtqueues, node)
249 ret |= vring_interrupt(irq, info->vq);
250 spin_unlock_irqrestore(&vm_dev->lock, flags);
251 }
252
253 return ret;
254}
255
256
257
258static void vm_del_vq(struct virtqueue *vq)
259{
260 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
261 struct virtio_mmio_vq_info *info = vq->priv;
262 unsigned long flags, size;
06ca287d 263 unsigned int index = vq->index;
edfd52e6
PM
264
265 spin_lock_irqsave(&vm_dev->lock, flags);
266 list_del(&info->node);
267 spin_unlock_irqrestore(&vm_dev->lock, flags);
268
269 vring_del_virtqueue(vq);
270
271 /* Select and deactivate the queue */
17bb6d40 272 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
edfd52e6
PM
273 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
274
275 size = PAGE_ALIGN(vring_size(info->num, VIRTIO_MMIO_VRING_ALIGN));
276 free_pages_exact(info->queue, size);
277 kfree(info);
278}
279
280static void vm_del_vqs(struct virtio_device *vdev)
281{
282 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
283 struct virtqueue *vq, *n;
284
285 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
286 vm_del_vq(vq);
287
288 free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev);
289}
290
291
292
293static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned index,
294 void (*callback)(struct virtqueue *vq),
295 const char *name)
296{
297 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
298 struct virtio_mmio_vq_info *info;
299 struct virtqueue *vq;
300 unsigned long flags, size;
301 int err;
302
6457f126
MT
303 if (!name)
304 return NULL;
305
edfd52e6
PM
306 /* Select the queue we're interested in */
307 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
308
309 /* Queue shouldn't already be set up. */
310 if (readl(vm_dev->base + VIRTIO_MMIO_QUEUE_PFN)) {
311 err = -ENOENT;
312 goto error_available;
313 }
314
315 /* Allocate and fill out our active queue description */
316 info = kmalloc(sizeof(*info), GFP_KERNEL);
317 if (!info) {
318 err = -ENOMEM;
319 goto error_kmalloc;
320 }
edfd52e6
PM
321
322 /* Allocate pages for the queue - start with a queue as big as
323 * possible (limited by maximum size allowed by device), drop down
324 * to a minimal size, just big enough to fit descriptor table
325 * and two rings (which makes it "alignment_size * 2")
326 */
327 info->num = readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX);
d78b519f
BF
328
329 /* If the device reports a 0 entry queue, we won't be able to
330 * use it to perform I/O, and vring_new_virtqueue() can't create
331 * empty queues anyway, so don't bother to set up the device.
332 */
333 if (info->num == 0) {
334 err = -ENOENT;
335 goto error_alloc_pages;
336 }
337
edfd52e6
PM
338 while (1) {
339 size = PAGE_ALIGN(vring_size(info->num,
340 VIRTIO_MMIO_VRING_ALIGN));
3850d29f
BF
341 /* Did the last iter shrink the queue below minimum size? */
342 if (size < VIRTIO_MMIO_VRING_ALIGN * 2) {
edfd52e6
PM
343 err = -ENOMEM;
344 goto error_alloc_pages;
345 }
346
347 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
348 if (info->queue)
349 break;
350
351 info->num /= 2;
352 }
353
354 /* Activate the queue */
355 writel(info->num, vm_dev->base + VIRTIO_MMIO_QUEUE_NUM);
356 writel(VIRTIO_MMIO_VRING_ALIGN,
357 vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN);
358 writel(virt_to_phys(info->queue) >> PAGE_SHIFT,
359 vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
360
361 /* Create the vring */
17bb6d40 362 vq = vring_new_virtqueue(index, info->num, VIRTIO_MMIO_VRING_ALIGN, vdev,
7b21e34f 363 true, info->queue, vm_notify, callback, name);
edfd52e6
PM
364 if (!vq) {
365 err = -ENOMEM;
366 goto error_new_virtqueue;
367 }
368
369 vq->priv = info;
370 info->vq = vq;
371
372 spin_lock_irqsave(&vm_dev->lock, flags);
373 list_add(&info->node, &vm_dev->virtqueues);
374 spin_unlock_irqrestore(&vm_dev->lock, flags);
375
376 return vq;
377
378error_new_virtqueue:
379 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
380 free_pages_exact(info->queue, size);
381error_alloc_pages:
382 kfree(info);
383error_kmalloc:
384error_available:
385 return ERR_PTR(err);
386}
387
388static int vm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
389 struct virtqueue *vqs[],
390 vq_callback_t *callbacks[],
391 const char *names[])
392{
393 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
394 unsigned int irq = platform_get_irq(vm_dev->pdev, 0);
395 int i, err;
396
397 err = request_irq(irq, vm_interrupt, IRQF_SHARED,
398 dev_name(&vdev->dev), vm_dev);
399 if (err)
400 return err;
401
402 for (i = 0; i < nvqs; ++i) {
403 vqs[i] = vm_setup_vq(vdev, i, callbacks[i], names[i]);
404 if (IS_ERR(vqs[i])) {
405 vm_del_vqs(vdev);
406 return PTR_ERR(vqs[i]);
407 }
408 }
409
410 return 0;
411}
412
66846048
RJ
413static const char *vm_bus_name(struct virtio_device *vdev)
414{
415 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
edfd52e6 416
66846048
RJ
417 return vm_dev->pdev->name;
418}
edfd52e6 419
93503932 420static const struct virtio_config_ops virtio_mmio_config_ops = {
edfd52e6
PM
421 .get = vm_get,
422 .set = vm_set,
423 .get_status = vm_get_status,
424 .set_status = vm_set_status,
425 .reset = vm_reset,
426 .find_vqs = vm_find_vqs,
427 .del_vqs = vm_del_vqs,
428 .get_features = vm_get_features,
429 .finalize_features = vm_finalize_features,
66846048 430 .bus_name = vm_bus_name,
edfd52e6
PM
431};
432
433
434
435/* Platform device */
436
8590dbc7 437static int virtio_mmio_probe(struct platform_device *pdev)
edfd52e6
PM
438{
439 struct virtio_mmio_device *vm_dev;
440 struct resource *mem;
441 unsigned long magic;
442
443 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
444 if (!mem)
445 return -EINVAL;
446
447 if (!devm_request_mem_region(&pdev->dev, mem->start,
448 resource_size(mem), pdev->name))
449 return -EBUSY;
450
451 vm_dev = devm_kzalloc(&pdev->dev, sizeof(*vm_dev), GFP_KERNEL);
452 if (!vm_dev)
453 return -ENOMEM;
454
455 vm_dev->vdev.dev.parent = &pdev->dev;
456 vm_dev->vdev.config = &virtio_mmio_config_ops;
457 vm_dev->pdev = pdev;
458 INIT_LIST_HEAD(&vm_dev->virtqueues);
459 spin_lock_init(&vm_dev->lock);
460
461 vm_dev->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
462 if (vm_dev->base == NULL)
463 return -EFAULT;
464
465 /* Check magic value */
466 magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE);
4ae85370 467 if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
edfd52e6
PM
468 dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
469 return -ENODEV;
470 }
471
472 /* Check device version */
473 vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION);
474 if (vm_dev->version != 1) {
475 dev_err(&pdev->dev, "Version %ld not supported!\n",
476 vm_dev->version);
477 return -ENXIO;
478 }
479
480 vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID);
481 vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
482
483 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
484
485 platform_set_drvdata(pdev, vm_dev);
486
487 return register_virtio_device(&vm_dev->vdev);
488}
489
8590dbc7 490static int virtio_mmio_remove(struct platform_device *pdev)
edfd52e6
PM
491{
492 struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev);
493
494 unregister_virtio_device(&vm_dev->vdev);
495
496 return 0;
497}
498
499
500
81a054ce
PM
501/* Devices list parameter */
502
503#if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
504
505static struct device vm_cmdline_parent = {
506 .init_name = "virtio-mmio-cmdline",
507};
508
509static int vm_cmdline_parent_registered;
510static int vm_cmdline_id;
511
512static int vm_cmdline_set(const char *device,
513 const struct kernel_param *kp)
514{
515 int err;
516 struct resource resources[2] = {};
517 char *str;
40f9938c
PM
518 long long int base, size;
519 unsigned int irq;
81a054ce
PM
520 int processed, consumed = 0;
521 struct platform_device *pdev;
522
40f9938c
PM
523 /* Consume "size" part of the command line parameter */
524 size = memparse(device, &str);
81a054ce 525
40f9938c 526 /* Get "@<base>:<irq>[:<id>]" chunks */
81a054ce 527 processed = sscanf(str, "@%lli:%u%n:%d%n",
40f9938c 528 &base, &irq, &consumed,
81a054ce
PM
529 &vm_cmdline_id, &consumed);
530
40f9938c
PM
531 /*
532 * sscanf() must processes at least 2 chunks; also there
533 * must be no extra characters after the last chunk, so
534 * str[consumed] must be '\0'
535 */
536 if (processed < 2 || str[consumed])
81a054ce
PM
537 return -EINVAL;
538
40f9938c 539 resources[0].flags = IORESOURCE_MEM;
81a054ce 540 resources[0].start = base;
40f9938c
PM
541 resources[0].end = base + size - 1;
542
543 resources[1].flags = IORESOURCE_IRQ;
544 resources[1].start = resources[1].end = irq;
81a054ce
PM
545
546 if (!vm_cmdline_parent_registered) {
547 err = device_register(&vm_cmdline_parent);
548 if (err) {
549 pr_err("Failed to register parent device!\n");
550 return err;
551 }
552 vm_cmdline_parent_registered = 1;
553 }
554
555 pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
556 vm_cmdline_id,
557 (unsigned long long)resources[0].start,
558 (unsigned long long)resources[0].end,
559 (int)resources[1].start);
560
561 pdev = platform_device_register_resndata(&vm_cmdline_parent,
562 "virtio-mmio", vm_cmdline_id++,
563 resources, ARRAY_SIZE(resources), NULL, 0);
564 if (IS_ERR(pdev))
565 return PTR_ERR(pdev);
566
567 return 0;
568}
569
570static int vm_cmdline_get_device(struct device *dev, void *data)
571{
572 char *buffer = data;
573 unsigned int len = strlen(buffer);
574 struct platform_device *pdev = to_platform_device(dev);
575
576 snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n",
577 pdev->resource[0].end - pdev->resource[0].start + 1ULL,
578 (unsigned long long)pdev->resource[0].start,
579 (unsigned long long)pdev->resource[1].start,
580 pdev->id);
581 return 0;
582}
583
584static int vm_cmdline_get(char *buffer, const struct kernel_param *kp)
585{
586 buffer[0] = '\0';
587 device_for_each_child(&vm_cmdline_parent, buffer,
588 vm_cmdline_get_device);
589 return strlen(buffer) + 1;
590}
591
592static struct kernel_param_ops vm_cmdline_param_ops = {
593 .set = vm_cmdline_set,
594 .get = vm_cmdline_get,
595};
596
597device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR);
598
599static int vm_unregister_cmdline_device(struct device *dev,
600 void *data)
601{
602 platform_device_unregister(to_platform_device(dev));
603
604 return 0;
605}
606
607static void vm_unregister_cmdline_devices(void)
608{
609 if (vm_cmdline_parent_registered) {
610 device_for_each_child(&vm_cmdline_parent, NULL,
611 vm_unregister_cmdline_device);
612 device_unregister(&vm_cmdline_parent);
613 vm_cmdline_parent_registered = 0;
614 }
615}
616
617#else
618
619static void vm_unregister_cmdline_devices(void)
620{
621}
622
623#endif
624
edfd52e6
PM
625/* Platform driver */
626
627static struct of_device_id virtio_mmio_match[] = {
628 { .compatible = "virtio,mmio", },
629 {},
630};
631MODULE_DEVICE_TABLE(of, virtio_mmio_match);
632
633static struct platform_driver virtio_mmio_driver = {
634 .probe = virtio_mmio_probe,
8590dbc7 635 .remove = virtio_mmio_remove,
edfd52e6
PM
636 .driver = {
637 .name = "virtio-mmio",
638 .owner = THIS_MODULE,
639 .of_match_table = virtio_mmio_match,
640 },
641};
642
643static int __init virtio_mmio_init(void)
644{
645 return platform_driver_register(&virtio_mmio_driver);
646}
647
648static void __exit virtio_mmio_exit(void)
649{
650 platform_driver_unregister(&virtio_mmio_driver);
81a054ce 651 vm_unregister_cmdline_devices();
edfd52e6
PM
652}
653
654module_init(virtio_mmio_init);
655module_exit(virtio_mmio_exit);
656
657MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
658MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
659MODULE_LICENSE("GPL");