blob: aec1daee9ada3c3de69911fecb1341923713acae [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. Tsirkin93d389f2014-11-27 13:45:58 +0200162 /* Make sure we don't have any features > 32 bits! */
163 BUG_ON((u32)vdev->features != vdev->features);
164
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200165 writel(0, vm_dev->base + VIRTIO_MMIO_GUEST_FEATURES_SEL);
166 writel(vdev->features, vm_dev->base + VIRTIO_MMIO_GUEST_FEATURES);
Pawel Molledfd52e2011-10-24 14:07:03 +0100167}
168
169static void vm_get(struct virtio_device *vdev, unsigned offset,
170 void *buf, unsigned len)
171{
172 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
173 u8 *ptr = buf;
174 int i;
175
176 for (i = 0; i < len; i++)
177 ptr[i] = readb(vm_dev->base + VIRTIO_MMIO_CONFIG + offset + i);
178}
179
180static void vm_set(struct virtio_device *vdev, unsigned offset,
181 const void *buf, unsigned len)
182{
183 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
184 const u8 *ptr = buf;
185 int i;
186
187 for (i = 0; i < len; i++)
188 writeb(ptr[i], vm_dev->base + VIRTIO_MMIO_CONFIG + offset + i);
189}
190
191static u8 vm_get_status(struct virtio_device *vdev)
192{
193 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
194
195 return readl(vm_dev->base + VIRTIO_MMIO_STATUS) & 0xff;
196}
197
198static void vm_set_status(struct virtio_device *vdev, u8 status)
199{
200 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
201
202 /* We should never be setting status to 0. */
203 BUG_ON(status == 0);
204
205 writel(status, vm_dev->base + VIRTIO_MMIO_STATUS);
206}
207
208static void vm_reset(struct virtio_device *vdev)
209{
210 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
211
212 /* 0 status means a reset. */
213 writel(0, vm_dev->base + VIRTIO_MMIO_STATUS);
214}
215
216
217
218/* Transport interface */
219
220/* the notify function used when creating a virt queue */
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030221static bool vm_notify(struct virtqueue *vq)
Pawel Molledfd52e2011-10-24 14:07:03 +0100222{
223 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100224
225 /* We write the queue's selector into the notification register to
226 * signal the other end */
Rusty Russell06ca2872012-10-16 23:56:14 +1030227 writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030228 return true;
Pawel Molledfd52e2011-10-24 14:07:03 +0100229}
230
231/* Notify all virtqueues on an interrupt. */
232static irqreturn_t vm_interrupt(int irq, void *opaque)
233{
234 struct virtio_mmio_device *vm_dev = opaque;
235 struct virtio_mmio_vq_info *info;
Pawel Molledfd52e2011-10-24 14:07:03 +0100236 unsigned long status;
237 unsigned long flags;
238 irqreturn_t ret = IRQ_NONE;
239
240 /* Read and acknowledge interrupts */
241 status = readl(vm_dev->base + VIRTIO_MMIO_INTERRUPT_STATUS);
242 writel(status, vm_dev->base + VIRTIO_MMIO_INTERRUPT_ACK);
243
Michael S. Tsirkin016c98c2014-10-14 10:40:34 +1030244 if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)) {
245 virtio_config_changed(&vm_dev->vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100246 ret = IRQ_HANDLED;
247 }
248
249 if (likely(status & VIRTIO_MMIO_INT_VRING)) {
250 spin_lock_irqsave(&vm_dev->lock, flags);
251 list_for_each_entry(info, &vm_dev->virtqueues, node)
252 ret |= vring_interrupt(irq, info->vq);
253 spin_unlock_irqrestore(&vm_dev->lock, flags);
254 }
255
256 return ret;
257}
258
259
260
261static void vm_del_vq(struct virtqueue *vq)
262{
263 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
264 struct virtio_mmio_vq_info *info = vq->priv;
265 unsigned long flags, size;
Rusty Russell06ca2872012-10-16 23:56:14 +1030266 unsigned int index = vq->index;
Pawel Molledfd52e2011-10-24 14:07:03 +0100267
268 spin_lock_irqsave(&vm_dev->lock, flags);
269 list_del(&info->node);
270 spin_unlock_irqrestore(&vm_dev->lock, flags);
271
272 vring_del_virtqueue(vq);
273
274 /* Select and deactivate the queue */
Jason Wang17bb6d42012-08-28 13:54:13 +0200275 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
Pawel Molledfd52e2011-10-24 14:07:03 +0100276 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
277
278 size = PAGE_ALIGN(vring_size(info->num, VIRTIO_MMIO_VRING_ALIGN));
279 free_pages_exact(info->queue, size);
280 kfree(info);
281}
282
283static void vm_del_vqs(struct virtio_device *vdev)
284{
285 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
286 struct virtqueue *vq, *n;
287
288 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
289 vm_del_vq(vq);
290
291 free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev);
292}
293
294
295
296static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned index,
297 void (*callback)(struct virtqueue *vq),
298 const char *name)
299{
300 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
301 struct virtio_mmio_vq_info *info;
302 struct virtqueue *vq;
303 unsigned long flags, size;
304 int err;
305
Michael S. Tsirkin6457f122012-09-05 21:47:45 +0300306 if (!name)
307 return NULL;
308
Pawel Molledfd52e2011-10-24 14:07:03 +0100309 /* Select the queue we're interested in */
310 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
311
312 /* Queue shouldn't already be set up. */
313 if (readl(vm_dev->base + VIRTIO_MMIO_QUEUE_PFN)) {
314 err = -ENOENT;
315 goto error_available;
316 }
317
318 /* Allocate and fill out our active queue description */
319 info = kmalloc(sizeof(*info), GFP_KERNEL);
320 if (!info) {
321 err = -ENOMEM;
322 goto error_kmalloc;
323 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100324
325 /* Allocate pages for the queue - start with a queue as big as
326 * possible (limited by maximum size allowed by device), drop down
327 * to a minimal size, just big enough to fit descriptor table
328 * and two rings (which makes it "alignment_size * 2")
329 */
330 info->num = readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX);
Brian Foleyd78b5192012-09-24 14:33:42 +0100331
332 /* If the device reports a 0 entry queue, we won't be able to
333 * use it to perform I/O, and vring_new_virtqueue() can't create
334 * empty queues anyway, so don't bother to set up the device.
335 */
336 if (info->num == 0) {
337 err = -ENOENT;
338 goto error_alloc_pages;
339 }
340
Pawel Molledfd52e2011-10-24 14:07:03 +0100341 while (1) {
342 size = PAGE_ALIGN(vring_size(info->num,
343 VIRTIO_MMIO_VRING_ALIGN));
Brian Foley3850d292012-09-24 14:33:41 +0100344 /* Did the last iter shrink the queue below minimum size? */
345 if (size < VIRTIO_MMIO_VRING_ALIGN * 2) {
Pawel Molledfd52e2011-10-24 14:07:03 +0100346 err = -ENOMEM;
347 goto error_alloc_pages;
348 }
349
350 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
351 if (info->queue)
352 break;
353
354 info->num /= 2;
355 }
356
357 /* Activate the queue */
358 writel(info->num, vm_dev->base + VIRTIO_MMIO_QUEUE_NUM);
359 writel(VIRTIO_MMIO_VRING_ALIGN,
360 vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN);
361 writel(virt_to_phys(info->queue) >> PAGE_SHIFT,
362 vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
363
364 /* Create the vring */
Jason Wang17bb6d42012-08-28 13:54:13 +0200365 vq = vring_new_virtqueue(index, info->num, VIRTIO_MMIO_VRING_ALIGN, vdev,
Rusty Russell7b21e342012-01-12 15:44:42 +1030366 true, info->queue, vm_notify, callback, name);
Pawel Molledfd52e2011-10-24 14:07:03 +0100367 if (!vq) {
368 err = -ENOMEM;
369 goto error_new_virtqueue;
370 }
371
372 vq->priv = info;
373 info->vq = vq;
374
375 spin_lock_irqsave(&vm_dev->lock, flags);
376 list_add(&info->node, &vm_dev->virtqueues);
377 spin_unlock_irqrestore(&vm_dev->lock, flags);
378
379 return vq;
380
381error_new_virtqueue:
382 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
383 free_pages_exact(info->queue, size);
384error_alloc_pages:
385 kfree(info);
386error_kmalloc:
387error_available:
388 return ERR_PTR(err);
389}
390
391static int vm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
392 struct virtqueue *vqs[],
393 vq_callback_t *callbacks[],
394 const char *names[])
395{
396 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
397 unsigned int irq = platform_get_irq(vm_dev->pdev, 0);
398 int i, err;
399
400 err = request_irq(irq, vm_interrupt, IRQF_SHARED,
401 dev_name(&vdev->dev), vm_dev);
402 if (err)
403 return err;
404
405 for (i = 0; i < nvqs; ++i) {
406 vqs[i] = vm_setup_vq(vdev, i, callbacks[i], names[i]);
407 if (IS_ERR(vqs[i])) {
408 vm_del_vqs(vdev);
409 return PTR_ERR(vqs[i]);
410 }
411 }
412
413 return 0;
414}
415
Rick Jones66846042011-11-14 14:17:08 +0000416static const char *vm_bus_name(struct virtio_device *vdev)
417{
418 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100419
Rick Jones66846042011-11-14 14:17:08 +0000420 return vm_dev->pdev->name;
421}
Pawel Molledfd52e2011-10-24 14:07:03 +0100422
Stephen Hemminger93503932013-02-10 15:57:38 +1030423static const struct virtio_config_ops virtio_mmio_config_ops = {
Pawel Molledfd52e2011-10-24 14:07:03 +0100424 .get = vm_get,
425 .set = vm_set,
426 .get_status = vm_get_status,
427 .set_status = vm_set_status,
428 .reset = vm_reset,
429 .find_vqs = vm_find_vqs,
430 .del_vqs = vm_del_vqs,
431 .get_features = vm_get_features,
432 .finalize_features = vm_finalize_features,
Rick Jones66846042011-11-14 14:17:08 +0000433 .bus_name = vm_bus_name,
Pawel Molledfd52e2011-10-24 14:07:03 +0100434};
435
436
437
438/* Platform device */
439
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800440static int virtio_mmio_probe(struct platform_device *pdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100441{
442 struct virtio_mmio_device *vm_dev;
443 struct resource *mem;
444 unsigned long magic;
445
446 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
447 if (!mem)
448 return -EINVAL;
449
450 if (!devm_request_mem_region(&pdev->dev, mem->start,
451 resource_size(mem), pdev->name))
452 return -EBUSY;
453
454 vm_dev = devm_kzalloc(&pdev->dev, sizeof(*vm_dev), GFP_KERNEL);
455 if (!vm_dev)
456 return -ENOMEM;
457
458 vm_dev->vdev.dev.parent = &pdev->dev;
459 vm_dev->vdev.config = &virtio_mmio_config_ops;
460 vm_dev->pdev = pdev;
461 INIT_LIST_HEAD(&vm_dev->virtqueues);
462 spin_lock_init(&vm_dev->lock);
463
464 vm_dev->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
465 if (vm_dev->base == NULL)
466 return -EFAULT;
467
468 /* Check magic value */
469 magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE);
Marc Zyngier4ae85372013-11-05 21:21:28 +1030470 if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
Pawel Molledfd52e2011-10-24 14:07:03 +0100471 dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
472 return -ENODEV;
473 }
474
475 /* Check device version */
476 vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION);
477 if (vm_dev->version != 1) {
478 dev_err(&pdev->dev, "Version %ld not supported!\n",
479 vm_dev->version);
480 return -ENXIO;
481 }
482
483 vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID);
484 vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
485
486 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
487
488 platform_set_drvdata(pdev, vm_dev);
489
490 return register_virtio_device(&vm_dev->vdev);
491}
492
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800493static int virtio_mmio_remove(struct platform_device *pdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100494{
495 struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev);
496
497 unregister_virtio_device(&vm_dev->vdev);
498
499 return 0;
500}
501
502
503
Pawel Moll81a054c2012-05-09 18:30:16 +0100504/* Devices list parameter */
505
506#if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
507
508static struct device vm_cmdline_parent = {
509 .init_name = "virtio-mmio-cmdline",
510};
511
512static int vm_cmdline_parent_registered;
513static int vm_cmdline_id;
514
515static int vm_cmdline_set(const char *device,
516 const struct kernel_param *kp)
517{
518 int err;
519 struct resource resources[2] = {};
520 char *str;
Pawel Moll40f99382012-11-22 12:30:24 +1030521 long long int base, size;
522 unsigned int irq;
Pawel Moll81a054c2012-05-09 18:30:16 +0100523 int processed, consumed = 0;
524 struct platform_device *pdev;
525
Pawel Moll40f99382012-11-22 12:30:24 +1030526 /* Consume "size" part of the command line parameter */
527 size = memparse(device, &str);
Pawel Moll81a054c2012-05-09 18:30:16 +0100528
Pawel Moll40f99382012-11-22 12:30:24 +1030529 /* Get "@<base>:<irq>[:<id>]" chunks */
Pawel Moll81a054c2012-05-09 18:30:16 +0100530 processed = sscanf(str, "@%lli:%u%n:%d%n",
Pawel Moll40f99382012-11-22 12:30:24 +1030531 &base, &irq, &consumed,
Pawel Moll81a054c2012-05-09 18:30:16 +0100532 &vm_cmdline_id, &consumed);
533
Pawel Moll40f99382012-11-22 12:30:24 +1030534 /*
535 * sscanf() must processes at least 2 chunks; also there
536 * must be no extra characters after the last chunk, so
537 * str[consumed] must be '\0'
538 */
539 if (processed < 2 || str[consumed])
Pawel Moll81a054c2012-05-09 18:30:16 +0100540 return -EINVAL;
541
Pawel Moll40f99382012-11-22 12:30:24 +1030542 resources[0].flags = IORESOURCE_MEM;
Pawel Moll81a054c2012-05-09 18:30:16 +0100543 resources[0].start = base;
Pawel Moll40f99382012-11-22 12:30:24 +1030544 resources[0].end = base + size - 1;
545
546 resources[1].flags = IORESOURCE_IRQ;
547 resources[1].start = resources[1].end = irq;
Pawel Moll81a054c2012-05-09 18:30:16 +0100548
549 if (!vm_cmdline_parent_registered) {
550 err = device_register(&vm_cmdline_parent);
551 if (err) {
552 pr_err("Failed to register parent device!\n");
553 return err;
554 }
555 vm_cmdline_parent_registered = 1;
556 }
557
558 pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
559 vm_cmdline_id,
560 (unsigned long long)resources[0].start,
561 (unsigned long long)resources[0].end,
562 (int)resources[1].start);
563
564 pdev = platform_device_register_resndata(&vm_cmdline_parent,
565 "virtio-mmio", vm_cmdline_id++,
566 resources, ARRAY_SIZE(resources), NULL, 0);
567 if (IS_ERR(pdev))
568 return PTR_ERR(pdev);
569
570 return 0;
571}
572
573static int vm_cmdline_get_device(struct device *dev, void *data)
574{
575 char *buffer = data;
576 unsigned int len = strlen(buffer);
577 struct platform_device *pdev = to_platform_device(dev);
578
579 snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n",
580 pdev->resource[0].end - pdev->resource[0].start + 1ULL,
581 (unsigned long long)pdev->resource[0].start,
582 (unsigned long long)pdev->resource[1].start,
583 pdev->id);
584 return 0;
585}
586
587static int vm_cmdline_get(char *buffer, const struct kernel_param *kp)
588{
589 buffer[0] = '\0';
590 device_for_each_child(&vm_cmdline_parent, buffer,
591 vm_cmdline_get_device);
592 return strlen(buffer) + 1;
593}
594
595static struct kernel_param_ops vm_cmdline_param_ops = {
596 .set = vm_cmdline_set,
597 .get = vm_cmdline_get,
598};
599
600device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR);
601
602static int vm_unregister_cmdline_device(struct device *dev,
603 void *data)
604{
605 platform_device_unregister(to_platform_device(dev));
606
607 return 0;
608}
609
610static void vm_unregister_cmdline_devices(void)
611{
612 if (vm_cmdline_parent_registered) {
613 device_for_each_child(&vm_cmdline_parent, NULL,
614 vm_unregister_cmdline_device);
615 device_unregister(&vm_cmdline_parent);
616 vm_cmdline_parent_registered = 0;
617 }
618}
619
620#else
621
622static void vm_unregister_cmdline_devices(void)
623{
624}
625
626#endif
627
Pawel Molledfd52e2011-10-24 14:07:03 +0100628/* Platform driver */
629
630static struct of_device_id virtio_mmio_match[] = {
631 { .compatible = "virtio,mmio", },
632 {},
633};
634MODULE_DEVICE_TABLE(of, virtio_mmio_match);
635
636static struct platform_driver virtio_mmio_driver = {
637 .probe = virtio_mmio_probe,
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800638 .remove = virtio_mmio_remove,
Pawel Molledfd52e2011-10-24 14:07:03 +0100639 .driver = {
640 .name = "virtio-mmio",
641 .owner = THIS_MODULE,
642 .of_match_table = virtio_mmio_match,
643 },
644};
645
646static int __init virtio_mmio_init(void)
647{
648 return platform_driver_register(&virtio_mmio_driver);
649}
650
651static void __exit virtio_mmio_exit(void)
652{
653 platform_driver_unregister(&virtio_mmio_driver);
Pawel Moll81a054c2012-05-09 18:30:16 +0100654 vm_unregister_cmdline_devices();
Pawel Molledfd52e2011-10-24 14:07:03 +0100655}
656
657module_init(virtio_mmio_init);
658module_exit(virtio_mmio_exit);
659
660MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
661MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
662MODULE_LICENSE("GPL");