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