Version:  2.0.40 2.2.26 2.4.37 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4

Linux/drivers/virtio/virtio_mmio.c

  1 /*
  2  * Virtio memory mapped device driver
  3  *
  4  * Copyright 2011-2014, ARM Ltd.
  5  *
  6  * This module allows virtio devices to be used over a virtual, memory mapped
  7  * platform device.
  8  *
  9  * The guest device(s) may be instantiated in one of three equivalent ways:
 10  *
 11  * 1. Static platform device in board's code, eg.:
 12  *
 13  *      static struct platform_device v2m_virtio_device = {
 14  *              .name = "virtio-mmio",
 15  *              .id = -1,
 16  *              .num_resources = 2,
 17  *              .resource = (struct resource []) {
 18  *                      {
 19  *                              .start = 0x1001e000,
 20  *                              .end = 0x1001e0ff,
 21  *                              .flags = IORESOURCE_MEM,
 22  *                      }, {
 23  *                              .start = 42 + 32,
 24  *                              .end = 42 + 32,
 25  *                              .flags = IORESOURCE_IRQ,
 26  *                      },
 27  *              }
 28  *      };
 29  *
 30  * 2. Device Tree node, eg.:
 31  *
 32  *              virtio_block@1e000 {
 33  *                      compatible = "virtio,mmio";
 34  *                      reg = <0x1e000 0x100>;
 35  *                      interrupts = <42>;
 36  *              }
 37  *
 38  * 3. Kernel module (or command line) parameter. Can be used more than once -
 39  *    one device will be created for each one. Syntax:
 40  *
 41  *              [virtio_mmio.]device=<size>@<baseaddr>:<irq>[:<id>]
 42  *    where:
 43  *              <size>     := size (can use standard suffixes like K, M or G)
 44  *              <baseaddr> := physical base address
 45  *              <irq>      := interrupt number (as passed to request_irq())
 46  *              <id>       := (optional) platform device id
 47  *    eg.:
 48  *              virtio_mmio.device=0x100@0x100b0000:48 \
 49  *                              virtio_mmio.device=1K@0x1001e000:74
 50  *
 51  *
 52  *
 53  * 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 
 59 #define pr_fmt(fmt) "virtio-mmio: " fmt
 60 
 61 #include <linux/acpi.h>
 62 #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 
 86 struct 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 
 98 struct 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 
105         /* 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 
116 static u64 vm_get_features(struct virtio_device *vdev)
117 {
118         struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
119         u64 features;
120 
121         writel(1, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL);
122         features = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES);
123         features <<= 32;
124 
125         writel(0, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL);
126         features |= readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES);
127 
128         return features;
129 }
130 
131 static int vm_finalize_features(struct virtio_device *vdev)
132 {
133         struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
134 
135         /* Give virtio_ring a chance to accept features. */
136         vring_transport_features(vdev);
137 
138         /* 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         }
144 
145         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);
152 
153         return 0;
154 }
155 
156 static 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);
160         void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
161         u8 b;
162         __le16 w;
163         __le32 l;
164 
165         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         }
196 }
197 
198 static 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);
202         void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
203         u8 b;
204         __le16 w;
205         __le32 l;
206 
207         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         }
239 }
240 
241 static 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 
251 static 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 
258 static 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 
268 static 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 */
281 static bool vm_notify(struct virtqueue *vq)
282 {
283         struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
284 
285         /* We write the queue's selector into the notification register to
286          * signal the other end */
287         writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
288         return true;
289 }
290 
291 /* Notify all virtqueues on an interrupt. */
292 static irqreturn_t vm_interrupt(int irq, void *opaque)
293 {
294         struct virtio_mmio_device *vm_dev = opaque;
295         struct virtio_mmio_vq_info *info;
296         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 
304         if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)) {
305                 virtio_config_changed(&vm_dev->vdev);
306                 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 
321 static 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;
326         unsigned int index = vq->index;
327 
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 */
335         writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
336         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         }
342 
343         size = PAGE_ALIGN(vring_size(info->num, VIRTIO_MMIO_VRING_ALIGN));
344         free_pages_exact(info->queue, size);
345         kfree(info);
346 }
347 
348 static 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 
361 static 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 
371         if (!name)
372                 return NULL;
373 
374         /* 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. */
378         if (readl(vm_dev->base + (vm_dev->version == 1 ?
379                         VIRTIO_MMIO_QUEUE_PFN : VIRTIO_MMIO_QUEUE_READY))) {
380                 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         }
390 
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);
397 
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 
407         while (1) {
408                 size = PAGE_ALIGN(vring_size(info->num,
409                                 VIRTIO_MMIO_VRING_ALIGN));
410                 /* Did the last iter shrink the queue below minimum size? */
411                 if (size < VIRTIO_MMIO_VRING_ALIGN * 2) {
412                         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 
423         /* Create the vring */
424         vq = vring_new_virtqueue(index, info->num, VIRTIO_MMIO_VRING_ALIGN, vdev,
425                                  true, info->queue, vm_notify, callback, name);
426         if (!vq) {
427                 err = -ENOMEM;
428                 goto error_new_virtqueue;
429         }
430 
431         /* 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 
458         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 
467 error_new_virtqueue:
468         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         }
474         free_pages_exact(info->queue, size);
475 error_alloc_pages:
476         kfree(info);
477 error_kmalloc:
478 error_available:
479         return ERR_PTR(err);
480 }
481 
482 static int vm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
483                        struct virtqueue *vqs[],
484                        vq_callback_t *callbacks[],
485                        const char *names[])
486 {
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 
507 static const char *vm_bus_name(struct virtio_device *vdev)
508 {
509         struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
510 
511         return vm_dev->pdev->name;
512 }
513 
514 static const struct virtio_config_ops virtio_mmio_config_ops = {
515         .get            = vm_get,
516         .set            = vm_set,
517         .generation     = vm_generation,
518         .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,
525         .bus_name       = vm_bus_name,
526 };
527 
528 
529 
530 /* Platform device */
531 
532 static int virtio_mmio_probe(struct platform_device *pdev)
533 {
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);
562         if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
563                 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);
569         if (vm_dev->version < 1 || vm_dev->version > 2) {
570                 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);
576         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         }
583         vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
584 
585         if (vm_dev->version == 1)
586                 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
587 
588         platform_set_drvdata(pdev, vm_dev);
589 
590         return register_virtio_device(&vm_dev->vdev);
591 }
592 
593 static int virtio_mmio_remove(struct platform_device *pdev)
594 {
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 
604 /* Devices list parameter */
605 
606 #if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
607 
608 static struct device vm_cmdline_parent = {
609         .init_name = "virtio-mmio-cmdline",
610 };
611 
612 static int vm_cmdline_parent_registered;
613 static int vm_cmdline_id;
614 
615 static 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;
621         long long int base, size;
622         unsigned int irq;
623         int processed, consumed = 0;
624         struct platform_device *pdev;
625 
626         /* Consume "size" part of the command line parameter */
627         size = memparse(device, &str);
628 
629         /* Get "@<base>:<irq>[:<id>]" chunks */
630         processed = sscanf(str, "@%lli:%u%n:%d%n",
631                         &base, &irq, &consumed,
632                         &vm_cmdline_id, &consumed);
633 
634         /*
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])
640                 return -EINVAL;
641 
642         resources[0].flags = IORESOURCE_MEM;
643         resources[0].start = base;
644         resources[0].end = base + size - 1;
645 
646         resources[1].flags = IORESOURCE_IRQ;
647         resources[1].start = resources[1].end = irq;
648 
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 
673 static 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 
687 static 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 
695 static const struct kernel_param_ops vm_cmdline_param_ops = {
696         .set = vm_cmdline_set,
697         .get = vm_cmdline_get,
698 };
699 
700 device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR);
701 
702 static 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 
710 static 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 
722 static void vm_unregister_cmdline_devices(void)
723 {
724 }
725 
726 #endif
727 
728 /* Platform driver */
729 
730 static struct of_device_id virtio_mmio_match[] = {
731         { .compatible = "virtio,mmio", },
732         {},
733 };
734 MODULE_DEVICE_TABLE(of, virtio_mmio_match);
735 
736 #ifdef CONFIG_ACPI
737 static const struct acpi_device_id virtio_mmio_acpi_match[] = {
738         { "LNRO0005", },
739         { }
740 };
741 MODULE_DEVICE_TABLE(acpi, virtio_mmio_acpi_match);
742 #endif
743 
744 static struct platform_driver virtio_mmio_driver = {
745         .probe          = virtio_mmio_probe,
746         .remove         = virtio_mmio_remove,
747         .driver         = {
748                 .name   = "virtio-mmio",
749                 .of_match_table = virtio_mmio_match,
750                 .acpi_match_table = ACPI_PTR(virtio_mmio_acpi_match),
751         },
752 };
753 
754 static int __init virtio_mmio_init(void)
755 {
756         return platform_driver_register(&virtio_mmio_driver);
757 }
758 
759 static void __exit virtio_mmio_exit(void)
760 {
761         platform_driver_unregister(&virtio_mmio_driver);
762         vm_unregister_cmdline_devices();
763 }
764 
765 module_init(virtio_mmio_init);
766 module_exit(virtio_mmio_exit);
767 
768 MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
769 MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
770 MODULE_LICENSE("GPL");
771 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us