blob: 7a5e60dea6c59267ef48efa42ac2091346616b57 [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
Pawel Molledfd52e2011-10-24 14:07:03 +010061#include <linux/highmem.h>
62#include <linux/interrupt.h>
63#include <linux/io.h>
64#include <linux/list.h>
65#include <linux/module.h>
66#include <linux/platform_device.h>
67#include <linux/slab.h>
68#include <linux/spinlock.h>
69#include <linux/virtio.h>
70#include <linux/virtio_config.h>
71#include <linux/virtio_mmio.h>
72#include <linux/virtio_ring.h>
73
74
75
76/* The alignment to use between consumer and producer parts of vring.
77 * Currently hardcoded to the page size. */
78#define VIRTIO_MMIO_VRING_ALIGN PAGE_SIZE
79
80
81
82#define to_virtio_mmio_device(_plat_dev) \
83 container_of(_plat_dev, struct virtio_mmio_device, vdev)
84
85struct virtio_mmio_device {
86 struct virtio_device vdev;
87 struct platform_device *pdev;
88
89 void __iomem *base;
90 unsigned long version;
91
92 /* a list of queues so we can dispatch IRQs */
93 spinlock_t lock;
94 struct list_head virtqueues;
95};
96
97struct virtio_mmio_vq_info {
98 /* the actual virtqueue */
99 struct virtqueue *vq;
100
101 /* the number of entries in the queue */
102 unsigned int num;
103
Pawel Molledfd52e2011-10-24 14:07:03 +0100104 /* the virtual address of the ring queue */
105 void *queue;
106
107 /* the list node for the virtqueues list */
108 struct list_head node;
109};
110
111
112
113/* Configuration interface */
114
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200115static u64 vm_get_features(struct virtio_device *vdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100116{
117 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Pawel Moll1862ee22015-01-23 14:45:55 +1030118 u64 features;
Pawel Molledfd52e2011-10-24 14:07:03 +0100119
Pawel Moll1862ee22015-01-23 14:45:55 +1030120 writel(1, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL);
121 features = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES);
122 features <<= 32;
Pawel Molledfd52e2011-10-24 14:07:03 +0100123
Pawel Moll1862ee22015-01-23 14:45:55 +1030124 writel(0, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL);
125 features |= readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES);
126
127 return features;
Pawel Molledfd52e2011-10-24 14:07:03 +0100128}
129
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200130static int vm_finalize_features(struct virtio_device *vdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100131{
132 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100133
134 /* Give virtio_ring a chance to accept features. */
135 vring_transport_features(vdev);
136
Pawel Moll1862ee22015-01-23 14:45:55 +1030137 /* Make sure there is are no mixed devices */
138 if (vm_dev->version == 2 &&
139 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
140 dev_err(&vdev->dev, "New virtio-mmio devices (version 2) must provide VIRTIO_F_VERSION_1 feature!\n");
141 return -EINVAL;
142 }
Michael S. Tsirkin93d389f2014-11-27 13:45:58 +0200143
Pawel Moll1862ee22015-01-23 14:45:55 +1030144 writel(1, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL);
145 writel((u32)(vdev->features >> 32),
146 vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES);
147
148 writel(0, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL);
149 writel((u32)vdev->features,
150 vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES);
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200151
152 return 0;
Pawel Molledfd52e2011-10-24 14:07:03 +0100153}
154
155static void vm_get(struct virtio_device *vdev, unsigned offset,
156 void *buf, unsigned len)
157{
158 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Michael S. Tsirkin704a0b52015-03-17 12:11:30 +1030159 void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
160 u8 b;
161 __le16 w;
162 __le32 l;
Pawel Molledfd52e2011-10-24 14:07:03 +0100163
Michael S. Tsirkin704a0b52015-03-17 12:11:30 +1030164 if (vm_dev->version == 1) {
165 u8 *ptr = buf;
166 int i;
167
168 for (i = 0; i < len; i++)
169 ptr[i] = readb(base + offset + i);
170 return;
171 }
172
173 switch (len) {
174 case 1:
175 b = readb(base + offset);
176 memcpy(buf, &b, sizeof b);
177 break;
178 case 2:
179 w = cpu_to_le16(readw(base + offset));
180 memcpy(buf, &w, sizeof w);
181 break;
182 case 4:
183 l = cpu_to_le32(readl(base + offset));
184 memcpy(buf, &l, sizeof l);
185 break;
186 case 8:
187 l = cpu_to_le32(readl(base + offset));
188 memcpy(buf, &l, sizeof l);
189 l = cpu_to_le32(ioread32(base + offset + sizeof l));
190 memcpy(buf + sizeof l, &l, sizeof l);
191 break;
192 default:
193 BUG();
194 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100195}
196
197static void vm_set(struct virtio_device *vdev, unsigned offset,
198 const void *buf, unsigned len)
199{
200 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Michael S. Tsirkin704a0b52015-03-17 12:11:30 +1030201 void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
202 u8 b;
203 __le16 w;
204 __le32 l;
Pawel Molledfd52e2011-10-24 14:07:03 +0100205
Michael S. Tsirkin704a0b52015-03-17 12:11:30 +1030206 if (vm_dev->version == 1) {
207 const u8 *ptr = buf;
208 int i;
209
210 for (i = 0; i < len; i++)
211 writeb(ptr[i], base + offset + i);
212
213 return;
214 }
215
216 switch (len) {
217 case 1:
218 memcpy(&b, buf, sizeof b);
219 writeb(b, base + offset);
220 break;
221 case 2:
222 memcpy(&w, buf, sizeof w);
223 writew(le16_to_cpu(w), base + offset);
224 break;
225 case 4:
226 memcpy(&l, buf, sizeof l);
227 writel(le32_to_cpu(l), base + offset);
228 break;
229 case 8:
230 memcpy(&l, buf, sizeof l);
231 writel(le32_to_cpu(l), base + offset);
232 memcpy(&l, buf + sizeof l, sizeof l);
233 writel(le32_to_cpu(l), base + offset + sizeof l);
234 break;
235 default:
236 BUG();
237 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100238}
239
Michael S. Tsirkin87e7bf12015-03-12 12:56:43 +1030240static u32 vm_generation(struct virtio_device *vdev)
241{
242 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
243
244 if (vm_dev->version == 1)
245 return 0;
246 else
247 return readl(vm_dev->base + VIRTIO_MMIO_CONFIG_GENERATION);
248}
249
Pawel Molledfd52e2011-10-24 14:07:03 +0100250static u8 vm_get_status(struct virtio_device *vdev)
251{
252 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
253
254 return readl(vm_dev->base + VIRTIO_MMIO_STATUS) & 0xff;
255}
256
257static void vm_set_status(struct virtio_device *vdev, u8 status)
258{
259 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
260
261 /* We should never be setting status to 0. */
262 BUG_ON(status == 0);
263
264 writel(status, vm_dev->base + VIRTIO_MMIO_STATUS);
265}
266
267static void vm_reset(struct virtio_device *vdev)
268{
269 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
270
271 /* 0 status means a reset. */
272 writel(0, vm_dev->base + VIRTIO_MMIO_STATUS);
273}
274
275
276
277/* Transport interface */
278
279/* the notify function used when creating a virt queue */
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030280static bool vm_notify(struct virtqueue *vq)
Pawel Molledfd52e2011-10-24 14:07:03 +0100281{
282 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100283
284 /* We write the queue's selector into the notification register to
285 * signal the other end */
Rusty Russell06ca2872012-10-16 23:56:14 +1030286 writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030287 return true;
Pawel Molledfd52e2011-10-24 14:07:03 +0100288}
289
290/* Notify all virtqueues on an interrupt. */
291static irqreturn_t vm_interrupt(int irq, void *opaque)
292{
293 struct virtio_mmio_device *vm_dev = opaque;
294 struct virtio_mmio_vq_info *info;
Pawel Molledfd52e2011-10-24 14:07:03 +0100295 unsigned long status;
296 unsigned long flags;
297 irqreturn_t ret = IRQ_NONE;
298
299 /* Read and acknowledge interrupts */
300 status = readl(vm_dev->base + VIRTIO_MMIO_INTERRUPT_STATUS);
301 writel(status, vm_dev->base + VIRTIO_MMIO_INTERRUPT_ACK);
302
Michael S. Tsirkin016c98c2014-10-14 10:40:34 +1030303 if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)) {
304 virtio_config_changed(&vm_dev->vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100305 ret = IRQ_HANDLED;
306 }
307
308 if (likely(status & VIRTIO_MMIO_INT_VRING)) {
309 spin_lock_irqsave(&vm_dev->lock, flags);
310 list_for_each_entry(info, &vm_dev->virtqueues, node)
311 ret |= vring_interrupt(irq, info->vq);
312 spin_unlock_irqrestore(&vm_dev->lock, flags);
313 }
314
315 return ret;
316}
317
318
319
320static void vm_del_vq(struct virtqueue *vq)
321{
322 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
323 struct virtio_mmio_vq_info *info = vq->priv;
324 unsigned long flags, size;
Rusty Russell06ca2872012-10-16 23:56:14 +1030325 unsigned int index = vq->index;
Pawel Molledfd52e2011-10-24 14:07:03 +0100326
327 spin_lock_irqsave(&vm_dev->lock, flags);
328 list_del(&info->node);
329 spin_unlock_irqrestore(&vm_dev->lock, flags);
330
331 vring_del_virtqueue(vq);
332
333 /* Select and deactivate the queue */
Jason Wang17bb6d42012-08-28 13:54:13 +0200334 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
Pawel Moll1862ee22015-01-23 14:45:55 +1030335 if (vm_dev->version == 1) {
336 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
337 } else {
338 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
339 WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY));
340 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100341
342 size = PAGE_ALIGN(vring_size(info->num, VIRTIO_MMIO_VRING_ALIGN));
343 free_pages_exact(info->queue, size);
344 kfree(info);
345}
346
347static void vm_del_vqs(struct virtio_device *vdev)
348{
349 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
350 struct virtqueue *vq, *n;
351
352 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
353 vm_del_vq(vq);
354
355 free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev);
356}
357
358
359
360static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned index,
361 void (*callback)(struct virtqueue *vq),
362 const char *name)
363{
364 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
365 struct virtio_mmio_vq_info *info;
366 struct virtqueue *vq;
367 unsigned long flags, size;
368 int err;
369
Michael S. Tsirkin6457f1262012-09-05 21:47:45 +0300370 if (!name)
371 return NULL;
372
Pawel Molledfd52e2011-10-24 14:07:03 +0100373 /* Select the queue we're interested in */
374 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
375
376 /* Queue shouldn't already be set up. */
Pawel Moll1862ee22015-01-23 14:45:55 +1030377 if (readl(vm_dev->base + (vm_dev->version == 1 ?
378 VIRTIO_MMIO_QUEUE_PFN : VIRTIO_MMIO_QUEUE_READY))) {
Pawel Molledfd52e2011-10-24 14:07:03 +0100379 err = -ENOENT;
380 goto error_available;
381 }
382
383 /* Allocate and fill out our active queue description */
384 info = kmalloc(sizeof(*info), GFP_KERNEL);
385 if (!info) {
386 err = -ENOMEM;
387 goto error_kmalloc;
388 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100389
390 /* Allocate pages for the queue - start with a queue as big as
391 * possible (limited by maximum size allowed by device), drop down
392 * to a minimal size, just big enough to fit descriptor table
393 * and two rings (which makes it "alignment_size * 2")
394 */
395 info->num = readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX);
Brian Foleyd78b5192012-09-24 14:33:42 +0100396
397 /* If the device reports a 0 entry queue, we won't be able to
398 * use it to perform I/O, and vring_new_virtqueue() can't create
399 * empty queues anyway, so don't bother to set up the device.
400 */
401 if (info->num == 0) {
402 err = -ENOENT;
403 goto error_alloc_pages;
404 }
405
Pawel Molledfd52e2011-10-24 14:07:03 +0100406 while (1) {
407 size = PAGE_ALIGN(vring_size(info->num,
408 VIRTIO_MMIO_VRING_ALIGN));
Brian Foley3850d292012-09-24 14:33:41 +0100409 /* Did the last iter shrink the queue below minimum size? */
410 if (size < VIRTIO_MMIO_VRING_ALIGN * 2) {
Pawel Molledfd52e2011-10-24 14:07:03 +0100411 err = -ENOMEM;
412 goto error_alloc_pages;
413 }
414
415 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
416 if (info->queue)
417 break;
418
419 info->num /= 2;
420 }
421
Pawel Molledfd52e2011-10-24 14:07:03 +0100422 /* Create the vring */
Jason Wang17bb6d42012-08-28 13:54:13 +0200423 vq = vring_new_virtqueue(index, info->num, VIRTIO_MMIO_VRING_ALIGN, vdev,
Rusty Russell7b21e342012-01-12 15:44:42 +1030424 true, info->queue, vm_notify, callback, name);
Pawel Molledfd52e2011-10-24 14:07:03 +0100425 if (!vq) {
426 err = -ENOMEM;
427 goto error_new_virtqueue;
428 }
429
Pawel Moll1862ee22015-01-23 14:45:55 +1030430 /* Activate the queue */
431 writel(info->num, vm_dev->base + VIRTIO_MMIO_QUEUE_NUM);
432 if (vm_dev->version == 1) {
433 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN);
434 writel(virt_to_phys(info->queue) >> PAGE_SHIFT,
435 vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
436 } else {
437 u64 addr;
438
439 addr = virt_to_phys(info->queue);
440 writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_LOW);
441 writel((u32)(addr >> 32),
442 vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_HIGH);
443
444 addr = virt_to_phys(virtqueue_get_avail(vq));
445 writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_LOW);
446 writel((u32)(addr >> 32),
447 vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_HIGH);
448
449 addr = virt_to_phys(virtqueue_get_used(vq));
450 writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_USED_LOW);
451 writel((u32)(addr >> 32),
452 vm_dev->base + VIRTIO_MMIO_QUEUE_USED_HIGH);
453
454 writel(1, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
455 }
456
Pawel Molledfd52e2011-10-24 14:07:03 +0100457 vq->priv = info;
458 info->vq = vq;
459
460 spin_lock_irqsave(&vm_dev->lock, flags);
461 list_add(&info->node, &vm_dev->virtqueues);
462 spin_unlock_irqrestore(&vm_dev->lock, flags);
463
464 return vq;
465
466error_new_virtqueue:
Pawel Moll1862ee22015-01-23 14:45:55 +1030467 if (vm_dev->version == 1) {
468 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
469 } else {
470 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
471 WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY));
472 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100473 free_pages_exact(info->queue, size);
474error_alloc_pages:
475 kfree(info);
476error_kmalloc:
477error_available:
478 return ERR_PTR(err);
479}
480
481static int vm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
482 struct virtqueue *vqs[],
483 vq_callback_t *callbacks[],
484 const char *names[])
485{
486 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
487 unsigned int irq = platform_get_irq(vm_dev->pdev, 0);
488 int i, err;
489
490 err = request_irq(irq, vm_interrupt, IRQF_SHARED,
491 dev_name(&vdev->dev), vm_dev);
492 if (err)
493 return err;
494
495 for (i = 0; i < nvqs; ++i) {
496 vqs[i] = vm_setup_vq(vdev, i, callbacks[i], names[i]);
497 if (IS_ERR(vqs[i])) {
498 vm_del_vqs(vdev);
499 return PTR_ERR(vqs[i]);
500 }
501 }
502
503 return 0;
504}
505
Rick Jones66846042011-11-14 14:17:08 +0000506static const char *vm_bus_name(struct virtio_device *vdev)
507{
508 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100509
Rick Jones66846042011-11-14 14:17:08 +0000510 return vm_dev->pdev->name;
511}
Pawel Molledfd52e2011-10-24 14:07:03 +0100512
Stephen Hemminger93503932013-02-10 15:57:38 +1030513static const struct virtio_config_ops virtio_mmio_config_ops = {
Pawel Molledfd52e2011-10-24 14:07:03 +0100514 .get = vm_get,
515 .set = vm_set,
Michael S. Tsirkin87e7bf12015-03-12 12:56:43 +1030516 .generation = vm_generation,
Pawel Molledfd52e2011-10-24 14:07:03 +0100517 .get_status = vm_get_status,
518 .set_status = vm_set_status,
519 .reset = vm_reset,
520 .find_vqs = vm_find_vqs,
521 .del_vqs = vm_del_vqs,
522 .get_features = vm_get_features,
523 .finalize_features = vm_finalize_features,
Rick Jones66846042011-11-14 14:17:08 +0000524 .bus_name = vm_bus_name,
Pawel Molledfd52e2011-10-24 14:07:03 +0100525};
526
527
528
529/* Platform device */
530
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800531static int virtio_mmio_probe(struct platform_device *pdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100532{
533 struct virtio_mmio_device *vm_dev;
534 struct resource *mem;
535 unsigned long magic;
536
537 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
538 if (!mem)
539 return -EINVAL;
540
541 if (!devm_request_mem_region(&pdev->dev, mem->start,
542 resource_size(mem), pdev->name))
543 return -EBUSY;
544
545 vm_dev = devm_kzalloc(&pdev->dev, sizeof(*vm_dev), GFP_KERNEL);
546 if (!vm_dev)
547 return -ENOMEM;
548
549 vm_dev->vdev.dev.parent = &pdev->dev;
550 vm_dev->vdev.config = &virtio_mmio_config_ops;
551 vm_dev->pdev = pdev;
552 INIT_LIST_HEAD(&vm_dev->virtqueues);
553 spin_lock_init(&vm_dev->lock);
554
555 vm_dev->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
556 if (vm_dev->base == NULL)
557 return -EFAULT;
558
559 /* Check magic value */
560 magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE);
Marc Zyngier4ae85372013-11-05 21:21:28 +1030561 if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
Pawel Molledfd52e2011-10-24 14:07:03 +0100562 dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
563 return -ENODEV;
564 }
565
566 /* Check device version */
567 vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION);
Pawel Moll1862ee22015-01-23 14:45:55 +1030568 if (vm_dev->version < 1 || vm_dev->version > 2) {
Pawel Molledfd52e2011-10-24 14:07:03 +0100569 dev_err(&pdev->dev, "Version %ld not supported!\n",
570 vm_dev->version);
571 return -ENXIO;
572 }
573
574 vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID);
Pawel Moll1862ee22015-01-23 14:45:55 +1030575 if (vm_dev->vdev.id.device == 0) {
576 /*
577 * virtio-mmio device with an ID 0 is a (dummy) placeholder
578 * with no function. End probing now with no error reported.
579 */
580 return -ENODEV;
581 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100582 vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
583
Pawel Moll1862ee22015-01-23 14:45:55 +1030584 if (vm_dev->version == 1)
585 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
Pawel Molledfd52e2011-10-24 14:07:03 +0100586
587 platform_set_drvdata(pdev, vm_dev);
588
589 return register_virtio_device(&vm_dev->vdev);
590}
591
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800592static int virtio_mmio_remove(struct platform_device *pdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100593{
594 struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev);
595
596 unregister_virtio_device(&vm_dev->vdev);
597
598 return 0;
599}
600
601
602
Pawel Moll81a054c2012-05-09 18:30:16 +0100603/* Devices list parameter */
604
605#if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
606
607static struct device vm_cmdline_parent = {
608 .init_name = "virtio-mmio-cmdline",
609};
610
611static int vm_cmdline_parent_registered;
612static int vm_cmdline_id;
613
614static int vm_cmdline_set(const char *device,
615 const struct kernel_param *kp)
616{
617 int err;
618 struct resource resources[2] = {};
619 char *str;
Pawel Moll40f99382012-11-22 12:30:24 +1030620 long long int base, size;
621 unsigned int irq;
Pawel Moll81a054c2012-05-09 18:30:16 +0100622 int processed, consumed = 0;
623 struct platform_device *pdev;
624
Pawel Moll40f99382012-11-22 12:30:24 +1030625 /* Consume "size" part of the command line parameter */
626 size = memparse(device, &str);
Pawel Moll81a054c2012-05-09 18:30:16 +0100627
Pawel Moll40f99382012-11-22 12:30:24 +1030628 /* Get "@<base>:<irq>[:<id>]" chunks */
Pawel Moll81a054c2012-05-09 18:30:16 +0100629 processed = sscanf(str, "@%lli:%u%n:%d%n",
Pawel Moll40f99382012-11-22 12:30:24 +1030630 &base, &irq, &consumed,
Pawel Moll81a054c2012-05-09 18:30:16 +0100631 &vm_cmdline_id, &consumed);
632
Pawel Moll40f99382012-11-22 12:30:24 +1030633 /*
634 * sscanf() must processes at least 2 chunks; also there
635 * must be no extra characters after the last chunk, so
636 * str[consumed] must be '\0'
637 */
638 if (processed < 2 || str[consumed])
Pawel Moll81a054c2012-05-09 18:30:16 +0100639 return -EINVAL;
640
Pawel Moll40f99382012-11-22 12:30:24 +1030641 resources[0].flags = IORESOURCE_MEM;
Pawel Moll81a054c2012-05-09 18:30:16 +0100642 resources[0].start = base;
Pawel Moll40f99382012-11-22 12:30:24 +1030643 resources[0].end = base + size - 1;
644
645 resources[1].flags = IORESOURCE_IRQ;
646 resources[1].start = resources[1].end = irq;
Pawel Moll81a054c2012-05-09 18:30:16 +0100647
648 if (!vm_cmdline_parent_registered) {
649 err = device_register(&vm_cmdline_parent);
650 if (err) {
651 pr_err("Failed to register parent device!\n");
652 return err;
653 }
654 vm_cmdline_parent_registered = 1;
655 }
656
657 pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
658 vm_cmdline_id,
659 (unsigned long long)resources[0].start,
660 (unsigned long long)resources[0].end,
661 (int)resources[1].start);
662
663 pdev = platform_device_register_resndata(&vm_cmdline_parent,
664 "virtio-mmio", vm_cmdline_id++,
665 resources, ARRAY_SIZE(resources), NULL, 0);
666 if (IS_ERR(pdev))
667 return PTR_ERR(pdev);
668
669 return 0;
670}
671
672static int vm_cmdline_get_device(struct device *dev, void *data)
673{
674 char *buffer = data;
675 unsigned int len = strlen(buffer);
676 struct platform_device *pdev = to_platform_device(dev);
677
678 snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n",
679 pdev->resource[0].end - pdev->resource[0].start + 1ULL,
680 (unsigned long long)pdev->resource[0].start,
681 (unsigned long long)pdev->resource[1].start,
682 pdev->id);
683 return 0;
684}
685
686static int vm_cmdline_get(char *buffer, const struct kernel_param *kp)
687{
688 buffer[0] = '\0';
689 device_for_each_child(&vm_cmdline_parent, buffer,
690 vm_cmdline_get_device);
691 return strlen(buffer) + 1;
692}
693
694static struct kernel_param_ops vm_cmdline_param_ops = {
695 .set = vm_cmdline_set,
696 .get = vm_cmdline_get,
697};
698
699device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR);
700
701static int vm_unregister_cmdline_device(struct device *dev,
702 void *data)
703{
704 platform_device_unregister(to_platform_device(dev));
705
706 return 0;
707}
708
709static void vm_unregister_cmdline_devices(void)
710{
711 if (vm_cmdline_parent_registered) {
712 device_for_each_child(&vm_cmdline_parent, NULL,
713 vm_unregister_cmdline_device);
714 device_unregister(&vm_cmdline_parent);
715 vm_cmdline_parent_registered = 0;
716 }
717}
718
719#else
720
721static void vm_unregister_cmdline_devices(void)
722{
723}
724
725#endif
726
Pawel Molledfd52e2011-10-24 14:07:03 +0100727/* Platform driver */
728
729static struct of_device_id virtio_mmio_match[] = {
730 { .compatible = "virtio,mmio", },
731 {},
732};
733MODULE_DEVICE_TABLE(of, virtio_mmio_match);
734
735static struct platform_driver virtio_mmio_driver = {
736 .probe = virtio_mmio_probe,
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800737 .remove = virtio_mmio_remove,
Pawel Molledfd52e2011-10-24 14:07:03 +0100738 .driver = {
739 .name = "virtio-mmio",
Pawel Molledfd52e2011-10-24 14:07:03 +0100740 .of_match_table = virtio_mmio_match,
741 },
742};
743
744static int __init virtio_mmio_init(void)
745{
746 return platform_driver_register(&virtio_mmio_driver);
747}
748
749static void __exit virtio_mmio_exit(void)
750{
751 platform_driver_unregister(&virtio_mmio_driver);
Pawel Moll81a054c2012-05-09 18:30:16 +0100752 vm_unregister_cmdline_devices();
Pawel Molledfd52e2011-10-24 14:07:03 +0100753}
754
755module_init(virtio_mmio_init);
756module_exit(virtio_mmio_exit);
757
758MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
759MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
760MODULE_LICENSE("GPL");