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