1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Virtio memory mapped device driver
4 *
5 * Copyright 2011-2014, ARM Ltd.
6 *
7 * This module allows virtio devices to be used over a virtual, memory mapped
8 * platform device.
9 *
10 * The guest device(s) may be instantiated in one of three equivalent ways:
11 *
12 * 1. Static platform device in board's code, eg.:
13 *
14 * static struct platform_device v2m_virtio_device = {
15 * .name = "virtio-mmio",
16 * .id = -1,
17 * .num_resources = 2,
18 * .resource = (struct resource []) {
19 * {
20 * .start = 0x1001e000,
21 * .end = 0x1001e0ff,
22 * .flags = IORESOURCE_MEM,
23 * }, {
24 * .start = 42 + 32,
25 * .end = 42 + 32,
26 * .flags = IORESOURCE_IRQ,
27 * },
28 * }
29 * };
30 *
31 * 2. Device Tree node, eg.:
32 *
33 * virtio_block@1e000 {
34 * compatible = "virtio,mmio";
35 * reg = <0x1e000 0x100>;
36 * interrupts = <42>;
37 * }
38 *
39 * 3. Kernel module (or command line) parameter. Can be used more than once -
40 * one device will be created for each one. Syntax:
41 *
42 * [virtio_mmio.]device=<size>@<baseaddr>:<irq>[:<id>]
43 * where:
44 * <size> := size (can use standard suffixes like K, M or G)
45 * <baseaddr> := physical base address
46 * <irq> := interrupt number (as passed to request_irq())
47 * <id> := (optional) platform device id
48 * eg.:
49 * virtio_mmio.device=0x100@0x100b0000:48 \
50 * virtio_mmio.device=1K@0x1001e000:74
51 *
52 * Based on Virtio PCI driver by Anthony Liguori, copyright IBM Corp. 2007
53 */
54
55 #define pr_fmt(fmt) "virtio-mmio: " fmt
56
57 #include <linux/acpi.h>
58 #include <linux/delay.h>
59 #include <linux/dma-mapping.h>
60 #include <linux/highmem.h>
61 #include <linux/interrupt.h>
62 #include <linux/io.h>
63 #include <linux/list.h>
64 #include <linux/module.h>
65 #include <linux/of.h>
66 #include <linux/platform_device.h>
67 #include <linux/pm.h>
68 #include <linux/slab.h>
69 #include <linux/virtio.h>
70 #include <linux/virtio_config.h>
71 #include <uapi/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
85 struct virtio_mmio_device {
86 struct virtio_device vdev;
87 struct platform_device *pdev;
88
89 void __iomem *base;
90 unsigned long version;
91
92 /* True if enable_irq_wake() succeeded for the shared IRQ. */
93 bool wake_irq_enabled;
94 };
95
96 /* Configuration interface */
97
vm_get_features(struct virtio_device * vdev)98 static u64 vm_get_features(struct virtio_device *vdev)
99 {
100 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
101 u64 features;
102
103 writel(1, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL);
104 features = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES);
105 features <<= 32;
106
107 writel(0, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL);
108 features |= readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES);
109
110 return features;
111 }
112
vm_finalize_features(struct virtio_device * vdev)113 static int vm_finalize_features(struct virtio_device *vdev)
114 {
115 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
116
117 /* Give virtio_ring a chance to accept features. */
118 vring_transport_features(vdev);
119
120 /* Make sure there are no mixed devices */
121 if (vm_dev->version >= 2 &&
122 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
123 dev_err(&vdev->dev, "New virtio-mmio devices (version >= 2) must provide VIRTIO_F_VERSION_1 feature!\n");
124 return -EINVAL;
125 }
126
127 writel(1, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL);
128 writel((u32)(vdev->features >> 32),
129 vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES);
130
131 writel(0, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL);
132 writel((u32)vdev->features,
133 vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES);
134
135 return 0;
136 }
137
vm_get(struct virtio_device * vdev,unsigned int offset,void * buf,unsigned int len)138 static void vm_get(struct virtio_device *vdev, unsigned int offset,
139 void *buf, unsigned int len)
140 {
141 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
142 void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
143 u8 b;
144 __le16 w;
145 __le32 l;
146
147 if (vm_dev->version == 1) {
148 u8 *ptr = buf;
149 int i;
150
151 for (i = 0; i < len; i++)
152 ptr[i] = readb(base + offset + i);
153 return;
154 }
155
156 switch (len) {
157 case 1:
158 b = readb(base + offset);
159 memcpy(buf, &b, sizeof b);
160 break;
161 case 2:
162 w = cpu_to_le16(readw(base + offset));
163 memcpy(buf, &w, sizeof w);
164 break;
165 case 4:
166 l = cpu_to_le32(readl(base + offset));
167 memcpy(buf, &l, sizeof l);
168 break;
169 case 8:
170 l = cpu_to_le32(readl(base + offset));
171 memcpy(buf, &l, sizeof l);
172 l = cpu_to_le32(ioread32(base + offset + sizeof l));
173 memcpy(buf + sizeof l, &l, sizeof l);
174 break;
175 default:
176 BUG();
177 }
178 }
179
vm_set(struct virtio_device * vdev,unsigned int offset,const void * buf,unsigned int len)180 static void vm_set(struct virtio_device *vdev, unsigned int offset,
181 const void *buf, unsigned int len)
182 {
183 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
184 void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
185 u8 b;
186 __le16 w;
187 __le32 l;
188
189 if (vm_dev->version == 1) {
190 const u8 *ptr = buf;
191 int i;
192
193 for (i = 0; i < len; i++)
194 writeb(ptr[i], base + offset + i);
195
196 return;
197 }
198
199 switch (len) {
200 case 1:
201 memcpy(&b, buf, sizeof b);
202 writeb(b, base + offset);
203 break;
204 case 2:
205 memcpy(&w, buf, sizeof w);
206 writew(le16_to_cpu(w), base + offset);
207 break;
208 case 4:
209 memcpy(&l, buf, sizeof l);
210 writel(le32_to_cpu(l), base + offset);
211 break;
212 case 8:
213 memcpy(&l, buf, sizeof l);
214 writel(le32_to_cpu(l), base + offset);
215 memcpy(&l, buf + sizeof l, sizeof l);
216 writel(le32_to_cpu(l), base + offset + sizeof l);
217 break;
218 default:
219 BUG();
220 }
221 }
222
vm_generation(struct virtio_device * vdev)223 static u32 vm_generation(struct virtio_device *vdev)
224 {
225 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
226
227 if (vm_dev->version == 1)
228 return 0;
229 else
230 return readl(vm_dev->base + VIRTIO_MMIO_CONFIG_GENERATION);
231 }
232
vm_get_status(struct virtio_device * vdev)233 static u8 vm_get_status(struct virtio_device *vdev)
234 {
235 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
236
237 return readl(vm_dev->base + VIRTIO_MMIO_STATUS) & 0xff;
238 }
239
vm_set_status(struct virtio_device * vdev,u8 status)240 static void vm_set_status(struct virtio_device *vdev, u8 status)
241 {
242 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
243
244 /* We should never be setting status to 0. */
245 BUG_ON(status == 0);
246
247 /*
248 * Per memory-barriers.txt, wmb() is not needed to guarantee
249 * that the cache coherent memory writes have completed
250 * before writing to the MMIO region.
251 */
252 writel(status, vm_dev->base + VIRTIO_MMIO_STATUS);
253 }
254
vm_reset(struct virtio_device * vdev)255 static void vm_reset(struct virtio_device *vdev)
256 {
257 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
258
259 /* 0 status means a reset. */
260 writel(0, vm_dev->base + VIRTIO_MMIO_STATUS);
261
262 if (vm_dev->version >= 3) {
263 /* Wait for reset to complete. */
264 while (vm_get_status(vdev))
265 fsleep(1000);
266 }
267 }
268
269
270
271 /* Transport interface */
272
273 /* the notify function used when creating a virt queue */
vm_notify(struct virtqueue * vq)274 static bool vm_notify(struct virtqueue *vq)
275 {
276 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
277
278 /* We write the queue's selector into the notification register to
279 * signal the other end */
280 writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
281 return true;
282 }
283
vm_notify_with_data(struct virtqueue * vq)284 static bool vm_notify_with_data(struct virtqueue *vq)
285 {
286 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
287 u32 data = vring_notification_data(vq);
288
289 writel(data, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
290
291 return true;
292 }
293
294 /* Notify all virtqueues on an interrupt. */
vm_interrupt(int irq,void * opaque)295 static irqreturn_t vm_interrupt(int irq, void *opaque)
296 {
297 struct virtio_mmio_device *vm_dev = opaque;
298 struct virtqueue *vq;
299 unsigned long status;
300 irqreturn_t ret = IRQ_NONE;
301
302 /* Read and acknowledge interrupts */
303 status = readl(vm_dev->base + VIRTIO_MMIO_INTERRUPT_STATUS);
304 writel(status, vm_dev->base + VIRTIO_MMIO_INTERRUPT_ACK);
305
306 if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)) {
307 virtio_config_changed(&vm_dev->vdev);
308 ret = IRQ_HANDLED;
309 }
310
311 if (likely(status & VIRTIO_MMIO_INT_VRING)) {
312 virtio_device_for_each_vq(&vm_dev->vdev, vq)
313 ret |= vring_interrupt(irq, vq);
314 }
315
316 return ret;
317 }
318
319
320
vm_del_vq(struct virtqueue * vq)321 static void vm_del_vq(struct virtqueue *vq)
322 {
323 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
324 unsigned int index = vq->index;
325
326 /* Select and deactivate the queue */
327 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
328 if (vm_dev->version == 1) {
329 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
330 } else {
331 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
332 WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY));
333 }
334
335 vring_del_virtqueue(vq);
336 }
337
vm_del_vqs(struct virtio_device * vdev)338 static void vm_del_vqs(struct virtio_device *vdev)
339 {
340 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
341 struct virtqueue *vq, *n;
342 int irq = platform_get_irq(vm_dev->pdev, 0);
343
344 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
345 vm_del_vq(vq);
346
347 if (vm_dev->wake_irq_enabled) {
348 disable_irq_wake(irq);
349 vm_dev->wake_irq_enabled = false;
350 }
351
352 free_irq(irq, vm_dev);
353 }
354
vm_synchronize_cbs(struct virtio_device * vdev)355 static void vm_synchronize_cbs(struct virtio_device *vdev)
356 {
357 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
358
359 synchronize_irq(platform_get_irq(vm_dev->pdev, 0));
360 }
361
vm_setup_vq(struct virtio_device * vdev,unsigned int index,void (* callback)(struct virtqueue * vq),const char * name,bool ctx)362 static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned int index,
363 void (*callback)(struct virtqueue *vq),
364 const char *name, bool ctx)
365 {
366 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
367 bool (*notify)(struct virtqueue *vq);
368 struct virtqueue *vq;
369 unsigned int num;
370 int err;
371
372 if (__virtio_test_bit(vdev, VIRTIO_F_NOTIFICATION_DATA))
373 notify = vm_notify_with_data;
374 else
375 notify = vm_notify;
376
377 if (!name)
378 return NULL;
379
380 /* Select the queue we're interested in */
381 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
382
383 /* Queue shouldn't already be set up. */
384 if (readl(vm_dev->base + (vm_dev->version == 1 ?
385 VIRTIO_MMIO_QUEUE_PFN : VIRTIO_MMIO_QUEUE_READY))) {
386 err = -ENOENT;
387 goto error_available;
388 }
389
390 num = readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX);
391 if (num == 0) {
392 err = -ENOENT;
393 goto error_new_virtqueue;
394 }
395
396 /* Create the vring */
397 vq = vring_create_virtqueue(index, num, VIRTIO_MMIO_VRING_ALIGN, vdev,
398 true, true, ctx, notify, callback, name);
399 if (!vq) {
400 err = -ENOMEM;
401 goto error_new_virtqueue;
402 }
403
404 vq->num_max = num;
405
406 /* Activate the queue */
407 writel(virtqueue_get_vring_size(vq), vm_dev->base + VIRTIO_MMIO_QUEUE_NUM);
408 if (vm_dev->version == 1) {
409 u64 q_pfn = virtqueue_get_desc_addr(vq) >> PAGE_SHIFT;
410
411 /*
412 * virtio-mmio v1 uses a 32bit QUEUE PFN. If we have something
413 * that doesn't fit in 32bit, fail the setup rather than
414 * pretending to be successful.
415 */
416 if (q_pfn >> 32) {
417 dev_err(&vdev->dev,
418 "platform bug: legacy virtio-mmio must not be used with RAM above 0x%llxGB\n",
419 0x1ULL << (32 + PAGE_SHIFT - 30));
420 err = -E2BIG;
421 goto error_bad_pfn;
422 }
423
424 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN);
425 writel(q_pfn, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
426 } else {
427 u64 addr;
428
429 addr = virtqueue_get_desc_addr(vq);
430 writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_LOW);
431 writel((u32)(addr >> 32),
432 vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_HIGH);
433
434 addr = virtqueue_get_avail_addr(vq);
435 writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_LOW);
436 writel((u32)(addr >> 32),
437 vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_HIGH);
438
439 addr = virtqueue_get_used_addr(vq);
440 writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_USED_LOW);
441 writel((u32)(addr >> 32),
442 vm_dev->base + VIRTIO_MMIO_QUEUE_USED_HIGH);
443
444 writel(1, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
445 }
446
447 return vq;
448
449 error_bad_pfn:
450 vring_del_virtqueue(vq);
451 error_new_virtqueue:
452 if (vm_dev->version == 1) {
453 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
454 } else {
455 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
456 WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY));
457 }
458 error_available:
459 return ERR_PTR(err);
460 }
461
vm_find_vqs(struct virtio_device * vdev,unsigned int nvqs,struct virtqueue * vqs[],struct virtqueue_info vqs_info[],struct irq_affinity * desc)462 static int vm_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
463 struct virtqueue *vqs[],
464 struct virtqueue_info vqs_info[],
465 struct irq_affinity *desc)
466 {
467 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
468 int irq = platform_get_irq(vm_dev->pdev, 0);
469 int i, err, queue_idx = 0;
470
471 if (irq < 0)
472 return irq;
473
474 err = request_irq(irq, vm_interrupt, IRQF_SHARED,
475 dev_name(&vdev->dev), vm_dev);
476 if (err)
477 return err;
478
479 if (of_property_read_bool(vm_dev->pdev->dev.of_node, "wakeup-source") &&
480 !enable_irq_wake(irq))
481 vm_dev->wake_irq_enabled = true;
482
483 for (i = 0; i < nvqs; ++i) {
484 struct virtqueue_info *vqi = &vqs_info[i];
485
486 if (!vqi->name) {
487 vqs[i] = NULL;
488 continue;
489 }
490
491 vqs[i] = vm_setup_vq(vdev, queue_idx++, vqi->callback,
492 vqi->name, vqi->ctx);
493 if (IS_ERR(vqs[i])) {
494 vm_del_vqs(vdev);
495 return PTR_ERR(vqs[i]);
496 }
497 }
498
499 return 0;
500 }
501
vm_bus_name(struct virtio_device * vdev)502 static const char *vm_bus_name(struct virtio_device *vdev)
503 {
504 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
505
506 return vm_dev->pdev->name;
507 }
508
vm_get_shm_region(struct virtio_device * vdev,struct virtio_shm_region * region,u8 id)509 static bool vm_get_shm_region(struct virtio_device *vdev,
510 struct virtio_shm_region *region, u8 id)
511 {
512 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
513 u64 len, addr;
514
515 /* Select the region we're interested in */
516 writel(id, vm_dev->base + VIRTIO_MMIO_SHM_SEL);
517
518 /* Read the region size */
519 len = (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_LEN_LOW);
520 len |= (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_LEN_HIGH) << 32;
521
522 region->len = len;
523
524 /* Check if region length is -1. If that's the case, the shared memory
525 * region does not exist and there is no need to proceed further.
526 */
527 if (len == ~(u64)0)
528 return false;
529
530 /* Read the region base address */
531 addr = (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_BASE_LOW);
532 addr |= (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_BASE_HIGH) << 32;
533
534 region->addr = addr;
535
536 return true;
537 }
538
539 static const struct virtio_config_ops virtio_mmio_config_ops = {
540 .get = vm_get,
541 .set = vm_set,
542 .generation = vm_generation,
543 .get_status = vm_get_status,
544 .set_status = vm_set_status,
545 .reset = vm_reset,
546 .find_vqs = vm_find_vqs,
547 .del_vqs = vm_del_vqs,
548 .get_features = vm_get_features,
549 .finalize_features = vm_finalize_features,
550 .bus_name = vm_bus_name,
551 .get_shm_region = vm_get_shm_region,
552 .synchronize_cbs = vm_synchronize_cbs,
553 };
554
555 #ifdef CONFIG_PM_SLEEP
virtio_mmio_freeze(struct device * dev)556 static int virtio_mmio_freeze(struct device *dev)
557 {
558 struct virtio_mmio_device *vm_dev = dev_get_drvdata(dev);
559
560 return virtio_device_freeze(&vm_dev->vdev);
561 }
562
virtio_mmio_restore(struct device * dev)563 static int virtio_mmio_restore(struct device *dev)
564 {
565 struct virtio_mmio_device *vm_dev = dev_get_drvdata(dev);
566
567 if (vm_dev->version == 1)
568 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
569
570 return virtio_device_restore(&vm_dev->vdev);
571 }
572
573 static const struct dev_pm_ops virtio_mmio_pm_ops = {
574 SET_SYSTEM_SLEEP_PM_OPS(virtio_mmio_freeze, virtio_mmio_restore)
575 };
576 #endif
577
virtio_mmio_release_dev(struct device * _d)578 static void virtio_mmio_release_dev(struct device *_d)
579 {
580 struct virtio_device *vdev =
581 container_of(_d, struct virtio_device, dev);
582 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
583
584 kfree(vm_dev);
585 }
586
587 /* Platform device */
588
virtio_mmio_probe(struct platform_device * pdev)589 static int virtio_mmio_probe(struct platform_device *pdev)
590 {
591 struct virtio_mmio_device *vm_dev;
592 unsigned long magic;
593 int rc;
594
595 vm_dev = kzalloc_obj(*vm_dev);
596 if (!vm_dev)
597 return -ENOMEM;
598
599 vm_dev->vdev.dev.parent = &pdev->dev;
600 vm_dev->vdev.dev.release = virtio_mmio_release_dev;
601 vm_dev->vdev.config = &virtio_mmio_config_ops;
602 vm_dev->pdev = pdev;
603
604 vm_dev->base = devm_platform_ioremap_resource(pdev, 0);
605 if (IS_ERR(vm_dev->base)) {
606 rc = PTR_ERR(vm_dev->base);
607 goto free_vm_dev;
608 }
609
610 /* Check magic value */
611 magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE);
612 if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
613 dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
614 rc = -ENODEV;
615 goto free_vm_dev;
616 }
617
618 /* Check device version */
619 vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION);
620 if (vm_dev->version < 1 || vm_dev->version > 3) {
621 dev_err(&pdev->dev, "Version %ld not supported!\n",
622 vm_dev->version);
623 rc = -ENXIO;
624 goto free_vm_dev;
625 }
626
627 vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID);
628 if (vm_dev->vdev.id.device == 0) {
629 /*
630 * virtio-mmio device with an ID 0 is a (dummy) placeholder
631 * with no function. End probing now with no error reported.
632 */
633 rc = -ENODEV;
634 goto free_vm_dev;
635 }
636 vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
637
638 if (vm_dev->version == 1) {
639 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
640
641 rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
642 /*
643 * In the legacy case, ensure our coherently-allocated virtio
644 * ring will be at an address expressable as a 32-bit PFN.
645 */
646 if (!rc)
647 dma_set_coherent_mask(&pdev->dev,
648 DMA_BIT_MASK(32 + PAGE_SHIFT));
649 } else {
650 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
651 }
652 if (rc)
653 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
654 if (rc)
655 dev_warn(&pdev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
656
657 platform_set_drvdata(pdev, vm_dev);
658
659 rc = register_virtio_device(&vm_dev->vdev);
660 if (rc)
661 put_device(&vm_dev->vdev.dev);
662
663 return rc;
664
665 free_vm_dev:
666 kfree(vm_dev);
667 return rc;
668 }
669
virtio_mmio_remove(struct platform_device * pdev)670 static void virtio_mmio_remove(struct platform_device *pdev)
671 {
672 struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev);
673 unregister_virtio_device(&vm_dev->vdev);
674 }
675
676
677
678 /* Devices list parameter */
679
680 #if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
681
682 static struct device *vm_cmdline_parent;
683
684 static int vm_cmdline_parent_registered;
685 static int vm_cmdline_id;
686
vm_cmdline_set(const char * device,const struct kernel_param * kp)687 static int vm_cmdline_set(const char *device,
688 const struct kernel_param *kp)
689 {
690 struct resource resources[2] = {};
691 char *str;
692 long long base, size;
693 unsigned int irq;
694 int processed, consumed = 0;
695 struct platform_device *pdev;
696
697 /* Consume "size" part of the command line parameter */
698 size = memparse(device, &str);
699
700 /* Get "@<base>:<irq>[:<id>]" chunks */
701 processed = sscanf(str, "@%lli:%u%n:%d%n",
702 &base, &irq, &consumed,
703 &vm_cmdline_id, &consumed);
704
705 /*
706 * sscanf() must process at least 2 chunks; also there
707 * must be no extra characters after the last chunk, so
708 * str[consumed] must be '\0'
709 */
710 if (processed < 2 || str[consumed] || irq == 0)
711 return -EINVAL;
712
713 resources[0].flags = IORESOURCE_MEM;
714 resources[0].start = base;
715 resources[0].end = base + size - 1;
716
717 resources[1].flags = IORESOURCE_IRQ;
718 resources[1].start = resources[1].end = irq;
719
720 if (!vm_cmdline_parent_registered) {
721 vm_cmdline_parent = __root_device_register("virtio-mmio-cmdline", NULL);
722 if (IS_ERR(vm_cmdline_parent)) {
723 pr_err("Failed to register parent device!\n");
724 return PTR_ERR(vm_cmdline_parent);
725 }
726 vm_cmdline_parent_registered = 1;
727 }
728
729 pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
730 vm_cmdline_id,
731 (unsigned long long)resources[0].start,
732 (unsigned long long)resources[0].end,
733 (int)resources[1].start);
734
735 pdev = platform_device_register_resndata(vm_cmdline_parent,
736 "virtio-mmio", vm_cmdline_id++,
737 resources, ARRAY_SIZE(resources), NULL, 0);
738
739 return PTR_ERR_OR_ZERO(pdev);
740 }
741
vm_cmdline_get_device(struct device * dev,void * data)742 static int vm_cmdline_get_device(struct device *dev, void *data)
743 {
744 char *buffer = data;
745 unsigned int len = strlen(buffer);
746 struct platform_device *pdev = to_platform_device(dev);
747
748 snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n",
749 pdev->resource[0].end - pdev->resource[0].start + 1ULL,
750 (unsigned long long)pdev->resource[0].start,
751 (unsigned long long)pdev->resource[1].start,
752 pdev->id);
753 return 0;
754 }
755
vm_cmdline_get(char * buffer,const struct kernel_param * kp)756 static int vm_cmdline_get(char *buffer, const struct kernel_param *kp)
757 {
758 buffer[0] = '\0';
759
760 if (vm_cmdline_parent_registered) {
761 device_for_each_child(vm_cmdline_parent, buffer,
762 vm_cmdline_get_device);
763 }
764
765 return strlen(buffer) + 1;
766 }
767
768 static const struct kernel_param_ops vm_cmdline_param_ops = {
769 .set = vm_cmdline_set,
770 .get = vm_cmdline_get,
771 };
772
773 device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR);
774
vm_unregister_cmdline_device(struct device * dev,void * data)775 static int vm_unregister_cmdline_device(struct device *dev,
776 void *data)
777 {
778 platform_device_unregister(to_platform_device(dev));
779
780 return 0;
781 }
782
vm_unregister_cmdline_devices(void)783 static void vm_unregister_cmdline_devices(void)
784 {
785 if (vm_cmdline_parent_registered) {
786 device_for_each_child(vm_cmdline_parent, NULL,
787 vm_unregister_cmdline_device);
788 root_device_unregister(vm_cmdline_parent);
789 vm_cmdline_parent_registered = 0;
790 }
791 }
792
793 #else
794
vm_unregister_cmdline_devices(void)795 static void vm_unregister_cmdline_devices(void)
796 {
797 }
798
799 #endif
800
801 /* Platform driver */
802
803 static const struct of_device_id virtio_mmio_match[] = {
804 { .compatible = "virtio,mmio", },
805 {},
806 };
807 MODULE_DEVICE_TABLE(of, virtio_mmio_match);
808
809 #ifdef CONFIG_ACPI
810 static const struct acpi_device_id virtio_mmio_acpi_match[] = {
811 { "LNRO0005", },
812 { }
813 };
814 MODULE_DEVICE_TABLE(acpi, virtio_mmio_acpi_match);
815 #endif
816
817 static struct platform_driver virtio_mmio_driver = {
818 .probe = virtio_mmio_probe,
819 .remove = virtio_mmio_remove,
820 .driver = {
821 .name = "virtio-mmio",
822 .of_match_table = virtio_mmio_match,
823 .acpi_match_table = ACPI_PTR(virtio_mmio_acpi_match),
824 #ifdef CONFIG_PM_SLEEP
825 .pm = &virtio_mmio_pm_ops,
826 #endif
827 },
828 };
829
virtio_mmio_init(void)830 static int __init virtio_mmio_init(void)
831 {
832 return platform_driver_register(&virtio_mmio_driver);
833 }
834
virtio_mmio_exit(void)835 static void __exit virtio_mmio_exit(void)
836 {
837 platform_driver_unregister(&virtio_mmio_driver);
838 vm_unregister_cmdline_devices();
839 }
840
841 module_init(virtio_mmio_init);
842 module_exit(virtio_mmio_exit);
843
844 MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
845 MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
846 MODULE_LICENSE("GPL");
847