blob: c63d0efa947b8fb63e740f4cf92ff7facc5b032a [file] [log] [blame]
Pawel Molledfd52e2011-10-24 14:07:03 +01001/*
2 * Virtio memory mapped device driver
3 *
4 * Copyright 2011, ARM Ltd.
5 *
6 * This module allows virtio devices to be used over a virtual, memory mapped
7 * platform device.
8 *
Pawel Moll81a054c2012-05-09 18:30:16 +01009 * The guest device(s) may be instantiated in one of three equivalent ways:
10 *
11 * 1. Static platform device in board's code, eg.:
12 *
13 * static struct platform_device v2m_virtio_device = {
14 * .name = "virtio-mmio",
15 * .id = -1,
16 * .num_resources = 2,
17 * .resource = (struct resource []) {
18 * {
19 * .start = 0x1001e000,
20 * .end = 0x1001e0ff,
21 * .flags = IORESOURCE_MEM,
22 * }, {
23 * .start = 42 + 32,
24 * .end = 42 + 32,
25 * .flags = IORESOURCE_IRQ,
26 * },
27 * }
28 * };
29 *
30 * 2. Device Tree node, eg.:
31 *
32 * virtio_block@1e000 {
33 * compatible = "virtio,mmio";
34 * reg = <0x1e000 0x100>;
35 * interrupts = <42>;
36 * }
37 *
38 * 3. Kernel module (or command line) parameter. Can be used more than once -
39 * one device will be created for each one. Syntax:
40 *
41 * [virtio_mmio.]device=<size>@<baseaddr>:<irq>[:<id>]
42 * where:
43 * <size> := size (can use standard suffixes like K, M or G)
44 * <baseaddr> := physical base address
45 * <irq> := interrupt number (as passed to request_irq())
46 * <id> := (optional) platform device id
47 * eg.:
48 * virtio_mmio.device=0x100@0x100b0000:48 \
49 * virtio_mmio.device=1K@0x1001e000:74
50 *
51 *
52 *
Pawel Molledfd52e2011-10-24 14:07:03 +010053 * Registers layout (all 32-bit wide):
54 *
55 * offset d. name description
56 * ------ -- ---------------- -----------------
57 *
58 * 0x000 R MagicValue Magic value "virt"
59 * 0x004 R Version Device version (current max. 1)
60 * 0x008 R DeviceID Virtio device ID
61 * 0x00c R VendorID Virtio vendor ID
62 *
63 * 0x010 R HostFeatures Features supported by the host
64 * 0x014 W HostFeaturesSel Set of host features to access via HostFeatures
65 *
66 * 0x020 W GuestFeatures Features activated by the guest
67 * 0x024 W GuestFeaturesSel Set of activated features to set via GuestFeatures
68 * 0x028 W GuestPageSize Size of guest's memory page in bytes
69 *
70 * 0x030 W QueueSel Queue selector
71 * 0x034 R QueueNumMax Maximum size of the currently selected queue
72 * 0x038 W QueueNum Queue size for the currently selected queue
73 * 0x03c W QueueAlign Used Ring alignment for the current queue
74 * 0x040 RW QueuePFN PFN for the currently selected queue
75 *
76 * 0x050 W QueueNotify Queue notifier
77 * 0x060 R InterruptStatus Interrupt status register
Ryota Ozaki0d34cc22013-02-04 10:35:30 +103078 * 0x064 W InterruptACK Interrupt acknowledge register
Pawel Molledfd52e2011-10-24 14:07:03 +010079 * 0x070 RW Status Device status register
80 *
81 * 0x100+ RW Device-specific configuration space
82 *
83 * Based on Virtio PCI driver by Anthony Liguori, copyright IBM Corp. 2007
84 *
85 * This work is licensed under the terms of the GNU GPL, version 2 or later.
86 * See the COPYING file in the top-level directory.
87 */
88
Pawel Moll81a054c2012-05-09 18:30:16 +010089#define pr_fmt(fmt) "virtio-mmio: " fmt
90
Pawel Molledfd52e2011-10-24 14:07:03 +010091#include <linux/highmem.h>
92#include <linux/interrupt.h>
93#include <linux/io.h>
94#include <linux/list.h>
95#include <linux/module.h>
96#include <linux/platform_device.h>
97#include <linux/slab.h>
98#include <linux/spinlock.h>
99#include <linux/virtio.h>
100#include <linux/virtio_config.h>
101#include <linux/virtio_mmio.h>
102#include <linux/virtio_ring.h>
103
104
105
106/* The alignment to use between consumer and producer parts of vring.
107 * Currently hardcoded to the page size. */
108#define VIRTIO_MMIO_VRING_ALIGN PAGE_SIZE
109
110
111
112#define to_virtio_mmio_device(_plat_dev) \
113 container_of(_plat_dev, struct virtio_mmio_device, vdev)
114
115struct virtio_mmio_device {
116 struct virtio_device vdev;
117 struct platform_device *pdev;
118
119 void __iomem *base;
120 unsigned long version;
121
122 /* a list of queues so we can dispatch IRQs */
123 spinlock_t lock;
124 struct list_head virtqueues;
125};
126
127struct virtio_mmio_vq_info {
128 /* the actual virtqueue */
129 struct virtqueue *vq;
130
131 /* the number of entries in the queue */
132 unsigned int num;
133
Pawel Molledfd52e2011-10-24 14:07:03 +0100134 /* 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
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200145static u64 vm_get_features(struct virtio_device *vdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100146{
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);
Pawel Molledfd52e2011-10-24 14:07:03 +0100158
159 /* Give virtio_ring a chance to accept features. */
160 vring_transport_features(vdev);
161
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200162 writel(0, vm_dev->base + VIRTIO_MMIO_GUEST_FEATURES_SEL);
163 writel(vdev->features, vm_dev->base + VIRTIO_MMIO_GUEST_FEATURES);
Pawel Molledfd52e2011-10-24 14:07:03 +0100164}
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 */
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030218static bool vm_notify(struct virtqueue *vq)
Pawel Molledfd52e2011-10-24 14:07:03 +0100219{
220 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100221
222 /* We write the queue's selector into the notification register to
223 * signal the other end */
Rusty Russell06ca2872012-10-16 23:56:14 +1030224 writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030225 return true;
Pawel Molledfd52e2011-10-24 14:07:03 +0100226}
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;
Pawel Molledfd52e2011-10-24 14:07:03 +0100233 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
Michael S. Tsirkin016c98c2014-10-14 10:40:34 +1030241 if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)) {
242 virtio_config_changed(&vm_dev->vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100243 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;
Rusty Russell06ca2872012-10-16 23:56:14 +1030263 unsigned int index = vq->index;
Pawel Molledfd52e2011-10-24 14:07:03 +0100264
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 */
Jason Wang17bb6d42012-08-28 13:54:13 +0200272 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
Pawel Molledfd52e2011-10-24 14:07:03 +0100273 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
Michael S. Tsirkin6457f122012-09-05 21:47:45 +0300303 if (!name)
304 return NULL;
305
Pawel Molledfd52e2011-10-24 14:07:03 +0100306 /* 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 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100321
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);
Brian Foleyd78b5192012-09-24 14:33:42 +0100328
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
Pawel Molledfd52e2011-10-24 14:07:03 +0100338 while (1) {
339 size = PAGE_ALIGN(vring_size(info->num,
340 VIRTIO_MMIO_VRING_ALIGN));
Brian Foley3850d292012-09-24 14:33:41 +0100341 /* Did the last iter shrink the queue below minimum size? */
342 if (size < VIRTIO_MMIO_VRING_ALIGN * 2) {
Pawel Molledfd52e2011-10-24 14:07:03 +0100343 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 */
Jason Wang17bb6d42012-08-28 13:54:13 +0200362 vq = vring_new_virtqueue(index, info->num, VIRTIO_MMIO_VRING_ALIGN, vdev,
Rusty Russell7b21e342012-01-12 15:44:42 +1030363 true, info->queue, vm_notify, callback, name);
Pawel Molledfd52e2011-10-24 14:07:03 +0100364 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
Rick Jones66846042011-11-14 14:17:08 +0000413static const char *vm_bus_name(struct virtio_device *vdev)
414{
415 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100416
Rick Jones66846042011-11-14 14:17:08 +0000417 return vm_dev->pdev->name;
418}
Pawel Molledfd52e2011-10-24 14:07:03 +0100419
Stephen Hemminger93503932013-02-10 15:57:38 +1030420static const struct virtio_config_ops virtio_mmio_config_ops = {
Pawel Molledfd52e2011-10-24 14:07:03 +0100421 .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,
Rick Jones66846042011-11-14 14:17:08 +0000430 .bus_name = vm_bus_name,
Pawel Molledfd52e2011-10-24 14:07:03 +0100431};
432
433
434
435/* Platform device */
436
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800437static int virtio_mmio_probe(struct platform_device *pdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100438{
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);
Marc Zyngier4ae85372013-11-05 21:21:28 +1030467 if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
Pawel Molledfd52e2011-10-24 14:07:03 +0100468 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
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800490static int virtio_mmio_remove(struct platform_device *pdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100491{
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
Pawel Moll81a054c2012-05-09 18:30:16 +0100501/* 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;
Pawel Moll40f99382012-11-22 12:30:24 +1030518 long long int base, size;
519 unsigned int irq;
Pawel Moll81a054c2012-05-09 18:30:16 +0100520 int processed, consumed = 0;
521 struct platform_device *pdev;
522
Pawel Moll40f99382012-11-22 12:30:24 +1030523 /* Consume "size" part of the command line parameter */
524 size = memparse(device, &str);
Pawel Moll81a054c2012-05-09 18:30:16 +0100525
Pawel Moll40f99382012-11-22 12:30:24 +1030526 /* Get "@<base>:<irq>[:<id>]" chunks */
Pawel Moll81a054c2012-05-09 18:30:16 +0100527 processed = sscanf(str, "@%lli:%u%n:%d%n",
Pawel Moll40f99382012-11-22 12:30:24 +1030528 &base, &irq, &consumed,
Pawel Moll81a054c2012-05-09 18:30:16 +0100529 &vm_cmdline_id, &consumed);
530
Pawel Moll40f99382012-11-22 12:30:24 +1030531 /*
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])
Pawel Moll81a054c2012-05-09 18:30:16 +0100537 return -EINVAL;
538
Pawel Moll40f99382012-11-22 12:30:24 +1030539 resources[0].flags = IORESOURCE_MEM;
Pawel Moll81a054c2012-05-09 18:30:16 +0100540 resources[0].start = base;
Pawel Moll40f99382012-11-22 12:30:24 +1030541 resources[0].end = base + size - 1;
542
543 resources[1].flags = IORESOURCE_IRQ;
544 resources[1].start = resources[1].end = irq;
Pawel Moll81a054c2012-05-09 18:30:16 +0100545
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
Pawel Molledfd52e2011-10-24 14:07:03 +0100625/* 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,
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800635 .remove = virtio_mmio_remove,
Pawel Molledfd52e2011-10-24 14:07:03 +0100636 .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);
Pawel Moll81a054c2012-05-09 18:30:16 +0100651 vm_unregister_cmdline_devices();
Pawel Molledfd52e2011-10-24 14:07:03 +0100652}
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");