blob: 745c6ee1bb3eab1259522d2833b350aa33ceb1f7 [file] [log] [blame]
Pawel Molledfd52e2011-10-24 14:07:03 +01001/*
2 * Virtio memory mapped device driver
3 *
Pawel Moll1862ee22015-01-23 14:45:55 +10304 * Copyright 2011-2014, ARM Ltd.
Pawel Molledfd52e2011-10-24 14:07:03 +01005 *
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 * Based on Virtio PCI driver by Anthony Liguori, copyright IBM Corp. 2007
54 *
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.
57 */
58
Pawel Moll81a054c2012-05-09 18:30:16 +010059#define pr_fmt(fmt) "virtio-mmio: " fmt
60
Graeme Gregory38c4ab82015-07-28 10:44:02 +010061#include <linux/acpi.h>
Pawel Molledfd52e2011-10-24 14:07:03 +010062#include <linux/highmem.h>
63#include <linux/interrupt.h>
64#include <linux/io.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>
74
75
76
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
80
81
82
83#define to_virtio_mmio_device(_plat_dev) \
84 container_of(_plat_dev, struct virtio_mmio_device, vdev)
85
86struct virtio_mmio_device {
87 struct virtio_device vdev;
88 struct platform_device *pdev;
89
90 void __iomem *base;
91 unsigned long version;
92
93 /* a list of queues so we can dispatch IRQs */
94 spinlock_t lock;
95 struct list_head virtqueues;
96};
97
98struct virtio_mmio_vq_info {
99 /* the actual virtqueue */
100 struct virtqueue *vq;
101
102 /* the number of entries in the queue */
103 unsigned int num;
104
Pawel Molledfd52e2011-10-24 14:07:03 +0100105 /* the virtual address of the ring queue */
106 void *queue;
107
108 /* the list node for the virtqueues list */
109 struct list_head node;
110};
111
112
113
114/* Configuration interface */
115
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200116static u64 vm_get_features(struct virtio_device *vdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100117{
118 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Pawel Moll1862ee22015-01-23 14:45:55 +1030119 u64 features;
Pawel Molledfd52e2011-10-24 14:07:03 +0100120
Pawel Moll1862ee22015-01-23 14:45:55 +1030121 writel(1, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL);
122 features = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES);
123 features <<= 32;
Pawel Molledfd52e2011-10-24 14:07:03 +0100124
Pawel Moll1862ee22015-01-23 14:45:55 +1030125 writel(0, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL);
126 features |= readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES);
127
128 return features;
Pawel Molledfd52e2011-10-24 14:07:03 +0100129}
130
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200131static int vm_finalize_features(struct virtio_device *vdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100132{
133 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100134
135 /* Give virtio_ring a chance to accept features. */
136 vring_transport_features(vdev);
137
Pawel Moll1862ee22015-01-23 14:45:55 +1030138 /* Make sure there is are no mixed devices */
139 if (vm_dev->version == 2 &&
140 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
141 dev_err(&vdev->dev, "New virtio-mmio devices (version 2) must provide VIRTIO_F_VERSION_1 feature!\n");
142 return -EINVAL;
143 }
Michael S. Tsirkin93d389f2014-11-27 13:45:58 +0200144
Pawel Moll1862ee22015-01-23 14:45:55 +1030145 writel(1, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL);
146 writel((u32)(vdev->features >> 32),
147 vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES);
148
149 writel(0, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL);
150 writel((u32)vdev->features,
151 vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES);
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200152
153 return 0;
Pawel Molledfd52e2011-10-24 14:07:03 +0100154}
155
156static void vm_get(struct virtio_device *vdev, unsigned offset,
157 void *buf, unsigned len)
158{
159 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Michael S. Tsirkin704a0b52015-03-17 12:11:30 +1030160 void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
161 u8 b;
162 __le16 w;
163 __le32 l;
Pawel Molledfd52e2011-10-24 14:07:03 +0100164
Michael S. Tsirkin704a0b52015-03-17 12:11:30 +1030165 if (vm_dev->version == 1) {
166 u8 *ptr = buf;
167 int i;
168
169 for (i = 0; i < len; i++)
170 ptr[i] = readb(base + offset + i);
171 return;
172 }
173
174 switch (len) {
175 case 1:
176 b = readb(base + offset);
177 memcpy(buf, &b, sizeof b);
178 break;
179 case 2:
180 w = cpu_to_le16(readw(base + offset));
181 memcpy(buf, &w, sizeof w);
182 break;
183 case 4:
184 l = cpu_to_le32(readl(base + offset));
185 memcpy(buf, &l, sizeof l);
186 break;
187 case 8:
188 l = cpu_to_le32(readl(base + offset));
189 memcpy(buf, &l, sizeof l);
190 l = cpu_to_le32(ioread32(base + offset + sizeof l));
191 memcpy(buf + sizeof l, &l, sizeof l);
192 break;
193 default:
194 BUG();
195 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100196}
197
198static void vm_set(struct virtio_device *vdev, unsigned offset,
199 const void *buf, unsigned len)
200{
201 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Michael S. Tsirkin704a0b52015-03-17 12:11:30 +1030202 void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
203 u8 b;
204 __le16 w;
205 __le32 l;
Pawel Molledfd52e2011-10-24 14:07:03 +0100206
Michael S. Tsirkin704a0b52015-03-17 12:11:30 +1030207 if (vm_dev->version == 1) {
208 const u8 *ptr = buf;
209 int i;
210
211 for (i = 0; i < len; i++)
212 writeb(ptr[i], base + offset + i);
213
214 return;
215 }
216
217 switch (len) {
218 case 1:
219 memcpy(&b, buf, sizeof b);
220 writeb(b, base + offset);
221 break;
222 case 2:
223 memcpy(&w, buf, sizeof w);
224 writew(le16_to_cpu(w), base + offset);
225 break;
226 case 4:
227 memcpy(&l, buf, sizeof l);
228 writel(le32_to_cpu(l), base + offset);
229 break;
230 case 8:
231 memcpy(&l, buf, sizeof l);
232 writel(le32_to_cpu(l), base + offset);
233 memcpy(&l, buf + sizeof l, sizeof l);
234 writel(le32_to_cpu(l), base + offset + sizeof l);
235 break;
236 default:
237 BUG();
238 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100239}
240
Michael S. Tsirkin87e7bf12015-03-12 12:56:43 +1030241static u32 vm_generation(struct virtio_device *vdev)
242{
243 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
244
245 if (vm_dev->version == 1)
246 return 0;
247 else
248 return readl(vm_dev->base + VIRTIO_MMIO_CONFIG_GENERATION);
249}
250
Pawel Molledfd52e2011-10-24 14:07:03 +0100251static u8 vm_get_status(struct virtio_device *vdev)
252{
253 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
254
255 return readl(vm_dev->base + VIRTIO_MMIO_STATUS) & 0xff;
256}
257
258static void vm_set_status(struct virtio_device *vdev, u8 status)
259{
260 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
261
262 /* We should never be setting status to 0. */
263 BUG_ON(status == 0);
264
265 writel(status, vm_dev->base + VIRTIO_MMIO_STATUS);
266}
267
268static void vm_reset(struct virtio_device *vdev)
269{
270 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
271
272 /* 0 status means a reset. */
273 writel(0, vm_dev->base + VIRTIO_MMIO_STATUS);
274}
275
276
277
278/* Transport interface */
279
280/* the notify function used when creating a virt queue */
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030281static bool vm_notify(struct virtqueue *vq)
Pawel Molledfd52e2011-10-24 14:07:03 +0100282{
283 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100284
285 /* We write the queue's selector into the notification register to
286 * signal the other end */
Rusty Russell06ca2872012-10-16 23:56:14 +1030287 writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030288 return true;
Pawel Molledfd52e2011-10-24 14:07:03 +0100289}
290
291/* Notify all virtqueues on an interrupt. */
292static irqreturn_t vm_interrupt(int irq, void *opaque)
293{
294 struct virtio_mmio_device *vm_dev = opaque;
295 struct virtio_mmio_vq_info *info;
Pawel Molledfd52e2011-10-24 14:07:03 +0100296 unsigned long status;
297 unsigned long flags;
298 irqreturn_t ret = IRQ_NONE;
299
300 /* Read and acknowledge interrupts */
301 status = readl(vm_dev->base + VIRTIO_MMIO_INTERRUPT_STATUS);
302 writel(status, vm_dev->base + VIRTIO_MMIO_INTERRUPT_ACK);
303
Michael S. Tsirkin016c98c2014-10-14 10:40:34 +1030304 if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)) {
305 virtio_config_changed(&vm_dev->vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100306 ret = IRQ_HANDLED;
307 }
308
309 if (likely(status & VIRTIO_MMIO_INT_VRING)) {
310 spin_lock_irqsave(&vm_dev->lock, flags);
311 list_for_each_entry(info, &vm_dev->virtqueues, node)
312 ret |= vring_interrupt(irq, info->vq);
313 spin_unlock_irqrestore(&vm_dev->lock, flags);
314 }
315
316 return ret;
317}
318
319
320
321static void vm_del_vq(struct virtqueue *vq)
322{
323 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
324 struct virtio_mmio_vq_info *info = vq->priv;
325 unsigned long flags, size;
Rusty Russell06ca2872012-10-16 23:56:14 +1030326 unsigned int index = vq->index;
Pawel Molledfd52e2011-10-24 14:07:03 +0100327
328 spin_lock_irqsave(&vm_dev->lock, flags);
329 list_del(&info->node);
330 spin_unlock_irqrestore(&vm_dev->lock, flags);
331
332 vring_del_virtqueue(vq);
333
334 /* Select and deactivate the queue */
Jason Wang17bb6d42012-08-28 13:54:13 +0200335 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
Pawel Moll1862ee22015-01-23 14:45:55 +1030336 if (vm_dev->version == 1) {
337 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
338 } else {
339 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
340 WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY));
341 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100342
343 size = PAGE_ALIGN(vring_size(info->num, VIRTIO_MMIO_VRING_ALIGN));
344 free_pages_exact(info->queue, size);
345 kfree(info);
346}
347
348static void vm_del_vqs(struct virtio_device *vdev)
349{
350 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
351 struct virtqueue *vq, *n;
352
353 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
354 vm_del_vq(vq);
355
356 free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev);
357}
358
359
360
361static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned index,
362 void (*callback)(struct virtqueue *vq),
363 const char *name)
364{
365 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
366 struct virtio_mmio_vq_info *info;
367 struct virtqueue *vq;
368 unsigned long flags, size;
369 int err;
370
Michael S. Tsirkin6457f1262012-09-05 21:47:45 +0300371 if (!name)
372 return NULL;
373
Pawel Molledfd52e2011-10-24 14:07:03 +0100374 /* Select the queue we're interested in */
375 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
376
377 /* Queue shouldn't already be set up. */
Pawel Moll1862ee22015-01-23 14:45:55 +1030378 if (readl(vm_dev->base + (vm_dev->version == 1 ?
379 VIRTIO_MMIO_QUEUE_PFN : VIRTIO_MMIO_QUEUE_READY))) {
Pawel Molledfd52e2011-10-24 14:07:03 +0100380 err = -ENOENT;
381 goto error_available;
382 }
383
384 /* Allocate and fill out our active queue description */
385 info = kmalloc(sizeof(*info), GFP_KERNEL);
386 if (!info) {
387 err = -ENOMEM;
388 goto error_kmalloc;
389 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100390
391 /* Allocate pages for the queue - start with a queue as big as
392 * possible (limited by maximum size allowed by device), drop down
393 * to a minimal size, just big enough to fit descriptor table
394 * and two rings (which makes it "alignment_size * 2")
395 */
396 info->num = readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX);
Brian Foleyd78b5192012-09-24 14:33:42 +0100397
398 /* If the device reports a 0 entry queue, we won't be able to
399 * use it to perform I/O, and vring_new_virtqueue() can't create
400 * empty queues anyway, so don't bother to set up the device.
401 */
402 if (info->num == 0) {
403 err = -ENOENT;
404 goto error_alloc_pages;
405 }
406
Pawel Molledfd52e2011-10-24 14:07:03 +0100407 while (1) {
408 size = PAGE_ALIGN(vring_size(info->num,
409 VIRTIO_MMIO_VRING_ALIGN));
Brian Foley3850d292012-09-24 14:33:41 +0100410 /* Did the last iter shrink the queue below minimum size? */
411 if (size < VIRTIO_MMIO_VRING_ALIGN * 2) {
Pawel Molledfd52e2011-10-24 14:07:03 +0100412 err = -ENOMEM;
413 goto error_alloc_pages;
414 }
415
416 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
417 if (info->queue)
418 break;
419
420 info->num /= 2;
421 }
422
Pawel Molledfd52e2011-10-24 14:07:03 +0100423 /* Create the vring */
Jason Wang17bb6d42012-08-28 13:54:13 +0200424 vq = vring_new_virtqueue(index, info->num, VIRTIO_MMIO_VRING_ALIGN, vdev,
Rusty Russell7b21e342012-01-12 15:44:42 +1030425 true, info->queue, vm_notify, callback, name);
Pawel Molledfd52e2011-10-24 14:07:03 +0100426 if (!vq) {
427 err = -ENOMEM;
428 goto error_new_virtqueue;
429 }
430
Pawel Moll1862ee22015-01-23 14:45:55 +1030431 /* Activate the queue */
432 writel(info->num, vm_dev->base + VIRTIO_MMIO_QUEUE_NUM);
433 if (vm_dev->version == 1) {
434 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN);
435 writel(virt_to_phys(info->queue) >> PAGE_SHIFT,
436 vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
437 } else {
438 u64 addr;
439
440 addr = virt_to_phys(info->queue);
441 writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_LOW);
442 writel((u32)(addr >> 32),
443 vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_HIGH);
444
445 addr = virt_to_phys(virtqueue_get_avail(vq));
446 writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_LOW);
447 writel((u32)(addr >> 32),
448 vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_HIGH);
449
450 addr = virt_to_phys(virtqueue_get_used(vq));
451 writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_USED_LOW);
452 writel((u32)(addr >> 32),
453 vm_dev->base + VIRTIO_MMIO_QUEUE_USED_HIGH);
454
455 writel(1, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
456 }
457
Pawel Molledfd52e2011-10-24 14:07:03 +0100458 vq->priv = info;
459 info->vq = vq;
460
461 spin_lock_irqsave(&vm_dev->lock, flags);
462 list_add(&info->node, &vm_dev->virtqueues);
463 spin_unlock_irqrestore(&vm_dev->lock, flags);
464
465 return vq;
466
467error_new_virtqueue:
Pawel Moll1862ee22015-01-23 14:45:55 +1030468 if (vm_dev->version == 1) {
469 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
470 } else {
471 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
472 WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY));
473 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100474 free_pages_exact(info->queue, size);
475error_alloc_pages:
476 kfree(info);
477error_kmalloc:
478error_available:
479 return ERR_PTR(err);
480}
481
482static int vm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
483 struct virtqueue *vqs[],
484 vq_callback_t *callbacks[],
Stefan Hajnoczif7ad26f2015-12-17 16:53:43 +0800485 const char * const names[])
Pawel Molledfd52e2011-10-24 14:07:03 +0100486{
487 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
488 unsigned int irq = platform_get_irq(vm_dev->pdev, 0);
489 int i, err;
490
491 err = request_irq(irq, vm_interrupt, IRQF_SHARED,
492 dev_name(&vdev->dev), vm_dev);
493 if (err)
494 return err;
495
496 for (i = 0; i < nvqs; ++i) {
497 vqs[i] = vm_setup_vq(vdev, i, callbacks[i], names[i]);
498 if (IS_ERR(vqs[i])) {
499 vm_del_vqs(vdev);
500 return PTR_ERR(vqs[i]);
501 }
502 }
503
504 return 0;
505}
506
Rick Jones66846042011-11-14 14:17:08 +0000507static const char *vm_bus_name(struct virtio_device *vdev)
508{
509 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100510
Rick Jones66846042011-11-14 14:17:08 +0000511 return vm_dev->pdev->name;
512}
Pawel Molledfd52e2011-10-24 14:07:03 +0100513
Stephen Hemminger93503932013-02-10 15:57:38 +1030514static const struct virtio_config_ops virtio_mmio_config_ops = {
Pawel Molledfd52e2011-10-24 14:07:03 +0100515 .get = vm_get,
516 .set = vm_set,
Michael S. Tsirkin87e7bf12015-03-12 12:56:43 +1030517 .generation = vm_generation,
Pawel Molledfd52e2011-10-24 14:07:03 +0100518 .get_status = vm_get_status,
519 .set_status = vm_set_status,
520 .reset = vm_reset,
521 .find_vqs = vm_find_vqs,
522 .del_vqs = vm_del_vqs,
523 .get_features = vm_get_features,
524 .finalize_features = vm_finalize_features,
Rick Jones66846042011-11-14 14:17:08 +0000525 .bus_name = vm_bus_name,
Pawel Molledfd52e2011-10-24 14:07:03 +0100526};
527
528
529
530/* Platform device */
531
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800532static int virtio_mmio_probe(struct platform_device *pdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100533{
534 struct virtio_mmio_device *vm_dev;
535 struct resource *mem;
536 unsigned long magic;
537
538 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
539 if (!mem)
540 return -EINVAL;
541
542 if (!devm_request_mem_region(&pdev->dev, mem->start,
543 resource_size(mem), pdev->name))
544 return -EBUSY;
545
546 vm_dev = devm_kzalloc(&pdev->dev, sizeof(*vm_dev), GFP_KERNEL);
547 if (!vm_dev)
548 return -ENOMEM;
549
550 vm_dev->vdev.dev.parent = &pdev->dev;
551 vm_dev->vdev.config = &virtio_mmio_config_ops;
552 vm_dev->pdev = pdev;
553 INIT_LIST_HEAD(&vm_dev->virtqueues);
554 spin_lock_init(&vm_dev->lock);
555
556 vm_dev->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
557 if (vm_dev->base == NULL)
558 return -EFAULT;
559
560 /* Check magic value */
561 magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE);
Marc Zyngier4ae85372013-11-05 21:21:28 +1030562 if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
Pawel Molledfd52e2011-10-24 14:07:03 +0100563 dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
564 return -ENODEV;
565 }
566
567 /* Check device version */
568 vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION);
Pawel Moll1862ee22015-01-23 14:45:55 +1030569 if (vm_dev->version < 1 || vm_dev->version > 2) {
Pawel Molledfd52e2011-10-24 14:07:03 +0100570 dev_err(&pdev->dev, "Version %ld not supported!\n",
571 vm_dev->version);
572 return -ENXIO;
573 }
574
575 vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID);
Pawel Moll1862ee22015-01-23 14:45:55 +1030576 if (vm_dev->vdev.id.device == 0) {
577 /*
578 * virtio-mmio device with an ID 0 is a (dummy) placeholder
579 * with no function. End probing now with no error reported.
580 */
581 return -ENODEV;
582 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100583 vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
584
Pawel Moll1862ee22015-01-23 14:45:55 +1030585 if (vm_dev->version == 1)
586 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
Pawel Molledfd52e2011-10-24 14:07:03 +0100587
588 platform_set_drvdata(pdev, vm_dev);
589
590 return register_virtio_device(&vm_dev->vdev);
591}
592
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800593static int virtio_mmio_remove(struct platform_device *pdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100594{
595 struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev);
596
597 unregister_virtio_device(&vm_dev->vdev);
598
599 return 0;
600}
601
602
603
Pawel Moll81a054c2012-05-09 18:30:16 +0100604/* Devices list parameter */
605
606#if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
607
608static struct device vm_cmdline_parent = {
609 .init_name = "virtio-mmio-cmdline",
610};
611
612static int vm_cmdline_parent_registered;
613static int vm_cmdline_id;
614
615static int vm_cmdline_set(const char *device,
616 const struct kernel_param *kp)
617{
618 int err;
619 struct resource resources[2] = {};
620 char *str;
Pawel Moll40f99382012-11-22 12:30:24 +1030621 long long int base, size;
622 unsigned int irq;
Pawel Moll81a054c2012-05-09 18:30:16 +0100623 int processed, consumed = 0;
624 struct platform_device *pdev;
625
Pawel Moll40f99382012-11-22 12:30:24 +1030626 /* Consume "size" part of the command line parameter */
627 size = memparse(device, &str);
Pawel Moll81a054c2012-05-09 18:30:16 +0100628
Pawel Moll40f99382012-11-22 12:30:24 +1030629 /* Get "@<base>:<irq>[:<id>]" chunks */
Pawel Moll81a054c2012-05-09 18:30:16 +0100630 processed = sscanf(str, "@%lli:%u%n:%d%n",
Pawel Moll40f99382012-11-22 12:30:24 +1030631 &base, &irq, &consumed,
Pawel Moll81a054c2012-05-09 18:30:16 +0100632 &vm_cmdline_id, &consumed);
633
Pawel Moll40f99382012-11-22 12:30:24 +1030634 /*
635 * sscanf() must processes at least 2 chunks; also there
636 * must be no extra characters after the last chunk, so
637 * str[consumed] must be '\0'
638 */
639 if (processed < 2 || str[consumed])
Pawel Moll81a054c2012-05-09 18:30:16 +0100640 return -EINVAL;
641
Pawel Moll40f99382012-11-22 12:30:24 +1030642 resources[0].flags = IORESOURCE_MEM;
Pawel Moll81a054c2012-05-09 18:30:16 +0100643 resources[0].start = base;
Pawel Moll40f99382012-11-22 12:30:24 +1030644 resources[0].end = base + size - 1;
645
646 resources[1].flags = IORESOURCE_IRQ;
647 resources[1].start = resources[1].end = irq;
Pawel Moll81a054c2012-05-09 18:30:16 +0100648
649 if (!vm_cmdline_parent_registered) {
650 err = device_register(&vm_cmdline_parent);
651 if (err) {
652 pr_err("Failed to register parent device!\n");
653 return err;
654 }
655 vm_cmdline_parent_registered = 1;
656 }
657
658 pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
659 vm_cmdline_id,
660 (unsigned long long)resources[0].start,
661 (unsigned long long)resources[0].end,
662 (int)resources[1].start);
663
664 pdev = platform_device_register_resndata(&vm_cmdline_parent,
665 "virtio-mmio", vm_cmdline_id++,
666 resources, ARRAY_SIZE(resources), NULL, 0);
667 if (IS_ERR(pdev))
668 return PTR_ERR(pdev);
669
670 return 0;
671}
672
673static int vm_cmdline_get_device(struct device *dev, void *data)
674{
675 char *buffer = data;
676 unsigned int len = strlen(buffer);
677 struct platform_device *pdev = to_platform_device(dev);
678
679 snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n",
680 pdev->resource[0].end - pdev->resource[0].start + 1ULL,
681 (unsigned long long)pdev->resource[0].start,
682 (unsigned long long)pdev->resource[1].start,
683 pdev->id);
684 return 0;
685}
686
687static int vm_cmdline_get(char *buffer, const struct kernel_param *kp)
688{
689 buffer[0] = '\0';
690 device_for_each_child(&vm_cmdline_parent, buffer,
691 vm_cmdline_get_device);
692 return strlen(buffer) + 1;
693}
694
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930695static const struct kernel_param_ops vm_cmdline_param_ops = {
Pawel Moll81a054c2012-05-09 18:30:16 +0100696 .set = vm_cmdline_set,
697 .get = vm_cmdline_get,
698};
699
700device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR);
701
702static int vm_unregister_cmdline_device(struct device *dev,
703 void *data)
704{
705 platform_device_unregister(to_platform_device(dev));
706
707 return 0;
708}
709
710static void vm_unregister_cmdline_devices(void)
711{
712 if (vm_cmdline_parent_registered) {
713 device_for_each_child(&vm_cmdline_parent, NULL,
714 vm_unregister_cmdline_device);
715 device_unregister(&vm_cmdline_parent);
716 vm_cmdline_parent_registered = 0;
717 }
718}
719
720#else
721
722static void vm_unregister_cmdline_devices(void)
723{
724}
725
726#endif
727
Pawel Molledfd52e2011-10-24 14:07:03 +0100728/* Platform driver */
729
730static struct of_device_id virtio_mmio_match[] = {
731 { .compatible = "virtio,mmio", },
732 {},
733};
734MODULE_DEVICE_TABLE(of, virtio_mmio_match);
735
Graeme Gregory38c4ab82015-07-28 10:44:02 +0100736#ifdef CONFIG_ACPI
737static const struct acpi_device_id virtio_mmio_acpi_match[] = {
738 { "LNRO0005", },
739 { }
740};
741MODULE_DEVICE_TABLE(acpi, virtio_mmio_acpi_match);
742#endif
743
Pawel Molledfd52e2011-10-24 14:07:03 +0100744static struct platform_driver virtio_mmio_driver = {
745 .probe = virtio_mmio_probe,
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800746 .remove = virtio_mmio_remove,
Pawel Molledfd52e2011-10-24 14:07:03 +0100747 .driver = {
748 .name = "virtio-mmio",
Pawel Molledfd52e2011-10-24 14:07:03 +0100749 .of_match_table = virtio_mmio_match,
Graeme Gregory38c4ab82015-07-28 10:44:02 +0100750 .acpi_match_table = ACPI_PTR(virtio_mmio_acpi_match),
Pawel Molledfd52e2011-10-24 14:07:03 +0100751 },
752};
753
754static int __init virtio_mmio_init(void)
755{
756 return platform_driver_register(&virtio_mmio_driver);
757}
758
759static void __exit virtio_mmio_exit(void)
760{
761 platform_driver_unregister(&virtio_mmio_driver);
Pawel Moll81a054c2012-05-09 18:30:16 +0100762 vm_unregister_cmdline_devices();
Pawel Molledfd52e2011-10-24 14:07:03 +0100763}
764
765module_init(virtio_mmio_init);
766module_exit(virtio_mmio_exit);
767
768MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
769MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
770MODULE_LICENSE("GPL");