blob: a46a4a29e9295f7225bfbc631642e0e0a81f92d6 [file] [log] [blame]
Thomas Gleixnerf33f5fe2019-05-22 09:51:24 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Pawel Molledfd52e2011-10-24 14:07:03 +01002/*
3 * Virtio memory mapped device driver
4 *
Pawel Moll1862ee22015-01-23 14:45:55 +10305 * Copyright 2011-2014, ARM Ltd.
Pawel Molledfd52e2011-10-24 14:07:03 +01006 *
7 * This module allows virtio devices to be used over a virtual, memory mapped
8 * platform device.
9 *
Pawel Moll81a054c2012-05-09 18:30:16 +010010 * 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 *
Pawel Molledfd52e2011-10-24 14:07:03 +010052 * Based on Virtio PCI driver by Anthony Liguori, copyright IBM Corp. 2007
Pawel Molledfd52e2011-10-24 14:07:03 +010053 */
54
Pawel Moll81a054c2012-05-09 18:30:16 +010055#define pr_fmt(fmt) "virtio-mmio: " fmt
56
Graeme Gregory38c4ab82015-07-28 10:44:02 +010057#include <linux/acpi.h>
Robin Murphyf7f66342017-01-10 17:51:17 +000058#include <linux/dma-mapping.h>
Pawel Molledfd52e2011-10-24 14:07:03 +010059#include <linux/highmem.h>
60#include <linux/interrupt.h>
61#include <linux/io.h>
62#include <linux/list.h>
63#include <linux/module.h>
Rob Herringc5111a52023-04-05 15:27:21 -050064#include <linux/of.h>
Pawel Molledfd52e2011-10-24 14:07:03 +010065#include <linux/platform_device.h>
Stephan Gerholded7ac372022-06-21 13:06:20 +020066#include <linux/pm.h>
Pawel Molledfd52e2011-10-24 14:07:03 +010067#include <linux/slab.h>
68#include <linux/spinlock.h>
69#include <linux/virtio.h>
70#include <linux/virtio_config.h>
Michael S. Tsirkin51be7a92017-01-12 23:36:32 +020071#include <uapi/linux/virtio_mmio.h>
Pawel Molledfd52e2011-10-24 14:07:03 +010072#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
85struct virtio_mmio_device {
86 struct virtio_device vdev;
87 struct platform_device *pdev;
88
89 void __iomem *base;
90 unsigned long version;
91
92 /* a list of queues so we can dispatch IRQs */
93 spinlock_t lock;
94 struct list_head virtqueues;
95};
96
97struct virtio_mmio_vq_info {
98 /* the actual virtqueue */
99 struct virtqueue *vq;
100
Pawel Molledfd52e2011-10-24 14:07:03 +0100101 /* the list node for the virtqueues list */
102 struct list_head node;
103};
104
105
106
107/* Configuration interface */
108
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200109static u64 vm_get_features(struct virtio_device *vdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100110{
111 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Pawel Moll1862ee22015-01-23 14:45:55 +1030112 u64 features;
Pawel Molledfd52e2011-10-24 14:07:03 +0100113
Pawel Moll1862ee22015-01-23 14:45:55 +1030114 writel(1, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL);
115 features = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES);
116 features <<= 32;
Pawel Molledfd52e2011-10-24 14:07:03 +0100117
Pawel Moll1862ee22015-01-23 14:45:55 +1030118 writel(0, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL);
119 features |= readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES);
120
121 return features;
Pawel Molledfd52e2011-10-24 14:07:03 +0100122}
123
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200124static int vm_finalize_features(struct virtio_device *vdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100125{
126 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100127
128 /* Give virtio_ring a chance to accept features. */
129 vring_transport_features(vdev);
130
Xianting Tian0c4aeb42021-02-06 07:46:59 -0500131 /* Make sure there are no mixed devices */
Pawel Moll1862ee22015-01-23 14:45:55 +1030132 if (vm_dev->version == 2 &&
133 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
134 dev_err(&vdev->dev, "New virtio-mmio devices (version 2) must provide VIRTIO_F_VERSION_1 feature!\n");
135 return -EINVAL;
136 }
Michael S. Tsirkin93d389f2014-11-27 13:45:58 +0200137
Pawel Moll1862ee22015-01-23 14:45:55 +1030138 writel(1, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL);
139 writel((u32)(vdev->features >> 32),
140 vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES);
141
142 writel(0, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL);
143 writel((u32)vdev->features,
144 vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES);
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200145
146 return 0;
Pawel Molledfd52e2011-10-24 14:07:03 +0100147}
148
Solomon Tan31532342022-04-18 02:54:35 +0000149static void vm_get(struct virtio_device *vdev, unsigned int offset,
150 void *buf, unsigned int len)
Pawel Molledfd52e2011-10-24 14:07:03 +0100151{
152 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Michael S. Tsirkin704a0b52015-03-17 12:11:30 +1030153 void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
154 u8 b;
155 __le16 w;
156 __le32 l;
Pawel Molledfd52e2011-10-24 14:07:03 +0100157
Michael S. Tsirkin704a0b52015-03-17 12:11:30 +1030158 if (vm_dev->version == 1) {
159 u8 *ptr = buf;
160 int i;
161
162 for (i = 0; i < len; i++)
163 ptr[i] = readb(base + offset + i);
164 return;
165 }
166
167 switch (len) {
168 case 1:
169 b = readb(base + offset);
170 memcpy(buf, &b, sizeof b);
171 break;
172 case 2:
173 w = cpu_to_le16(readw(base + offset));
174 memcpy(buf, &w, sizeof w);
175 break;
176 case 4:
177 l = cpu_to_le32(readl(base + offset));
178 memcpy(buf, &l, sizeof l);
179 break;
180 case 8:
181 l = cpu_to_le32(readl(base + offset));
182 memcpy(buf, &l, sizeof l);
183 l = cpu_to_le32(ioread32(base + offset + sizeof l));
184 memcpy(buf + sizeof l, &l, sizeof l);
185 break;
186 default:
187 BUG();
188 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100189}
190
Solomon Tan31532342022-04-18 02:54:35 +0000191static void vm_set(struct virtio_device *vdev, unsigned int offset,
192 const void *buf, unsigned int len)
Pawel Molledfd52e2011-10-24 14:07:03 +0100193{
194 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Michael S. Tsirkin704a0b52015-03-17 12:11:30 +1030195 void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
196 u8 b;
197 __le16 w;
198 __le32 l;
Pawel Molledfd52e2011-10-24 14:07:03 +0100199
Michael S. Tsirkin704a0b52015-03-17 12:11:30 +1030200 if (vm_dev->version == 1) {
201 const u8 *ptr = buf;
202 int i;
203
204 for (i = 0; i < len; i++)
205 writeb(ptr[i], base + offset + i);
206
207 return;
208 }
209
210 switch (len) {
211 case 1:
212 memcpy(&b, buf, sizeof b);
213 writeb(b, base + offset);
214 break;
215 case 2:
216 memcpy(&w, buf, sizeof w);
217 writew(le16_to_cpu(w), base + offset);
218 break;
219 case 4:
220 memcpy(&l, buf, sizeof l);
221 writel(le32_to_cpu(l), base + offset);
222 break;
223 case 8:
224 memcpy(&l, buf, sizeof l);
225 writel(le32_to_cpu(l), base + offset);
226 memcpy(&l, buf + sizeof l, sizeof l);
227 writel(le32_to_cpu(l), base + offset + sizeof l);
228 break;
229 default:
230 BUG();
231 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100232}
233
Michael S. Tsirkin87e7bf12015-03-12 12:56:43 +1030234static u32 vm_generation(struct virtio_device *vdev)
235{
236 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
237
238 if (vm_dev->version == 1)
239 return 0;
240 else
241 return readl(vm_dev->base + VIRTIO_MMIO_CONFIG_GENERATION);
242}
243
Pawel Molledfd52e2011-10-24 14:07:03 +0100244static u8 vm_get_status(struct virtio_device *vdev)
245{
246 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
247
248 return readl(vm_dev->base + VIRTIO_MMIO_STATUS) & 0xff;
249}
250
251static void vm_set_status(struct virtio_device *vdev, u8 status)
252{
253 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
254
255 /* We should never be setting status to 0. */
256 BUG_ON(status == 0);
257
Jason Wang8b4ec692022-05-27 14:01:19 +0800258 /*
259 * Per memory-barriers.txt, wmb() is not needed to guarantee
Bo Liuacb0055e2022-06-08 23:11:06 -0400260 * that the cache coherent memory writes have completed
Jason Wang8b4ec692022-05-27 14:01:19 +0800261 * before writing to the MMIO region.
262 */
Pawel Molledfd52e2011-10-24 14:07:03 +0100263 writel(status, vm_dev->base + VIRTIO_MMIO_STATUS);
264}
265
266static void vm_reset(struct virtio_device *vdev)
267{
268 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
269
270 /* 0 status means a reset. */
271 writel(0, vm_dev->base + VIRTIO_MMIO_STATUS);
272}
273
274
275
276/* Transport interface */
277
278/* the notify function used when creating a virt queue */
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030279static bool vm_notify(struct virtqueue *vq)
Pawel Molledfd52e2011-10-24 14:07:03 +0100280{
281 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100282
283 /* We write the queue's selector into the notification register to
284 * signal the other end */
Rusty Russell06ca2872012-10-16 23:56:14 +1030285 writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030286 return true;
Pawel Molledfd52e2011-10-24 14:07:03 +0100287}
288
Viktor Prutyanovaf8ecec2023-04-13 11:18:54 +0300289static bool vm_notify_with_data(struct virtqueue *vq)
290{
291 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
292 u32 data = vring_notification_data(vq);
293
294 writel(data, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
295
296 return true;
297}
298
Pawel Molledfd52e2011-10-24 14:07:03 +0100299/* Notify all virtqueues on an interrupt. */
300static irqreturn_t vm_interrupt(int irq, void *opaque)
301{
302 struct virtio_mmio_device *vm_dev = opaque;
303 struct virtio_mmio_vq_info *info;
Pawel Molledfd52e2011-10-24 14:07:03 +0100304 unsigned long status;
305 unsigned long flags;
306 irqreturn_t ret = IRQ_NONE;
307
308 /* Read and acknowledge interrupts */
309 status = readl(vm_dev->base + VIRTIO_MMIO_INTERRUPT_STATUS);
310 writel(status, vm_dev->base + VIRTIO_MMIO_INTERRUPT_ACK);
311
Michael S. Tsirkin016c98c2014-10-14 10:40:34 +1030312 if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)) {
313 virtio_config_changed(&vm_dev->vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100314 ret = IRQ_HANDLED;
315 }
316
317 if (likely(status & VIRTIO_MMIO_INT_VRING)) {
318 spin_lock_irqsave(&vm_dev->lock, flags);
319 list_for_each_entry(info, &vm_dev->virtqueues, node)
320 ret |= vring_interrupt(irq, info->vq);
321 spin_unlock_irqrestore(&vm_dev->lock, flags);
322 }
323
324 return ret;
325}
326
327
328
329static void vm_del_vq(struct virtqueue *vq)
330{
331 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
332 struct virtio_mmio_vq_info *info = vq->priv;
Andy Lutomirskib4211132016-02-02 21:46:38 -0800333 unsigned long flags;
Rusty Russell06ca2872012-10-16 23:56:14 +1030334 unsigned int index = vq->index;
Pawel Molledfd52e2011-10-24 14:07:03 +0100335
336 spin_lock_irqsave(&vm_dev->lock, flags);
337 list_del(&info->node);
338 spin_unlock_irqrestore(&vm_dev->lock, flags);
339
Pawel Molledfd52e2011-10-24 14:07:03 +0100340 /* Select and deactivate the queue */
Jason Wang17bb6d42012-08-28 13:54:13 +0200341 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
Pawel Moll1862ee22015-01-23 14:45:55 +1030342 if (vm_dev->version == 1) {
343 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
344 } else {
345 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
346 WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY));
347 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100348
Andy Lutomirskib4211132016-02-02 21:46:38 -0800349 vring_del_virtqueue(vq);
350
Pawel Molledfd52e2011-10-24 14:07:03 +0100351 kfree(info);
352}
353
354static void vm_del_vqs(struct virtio_device *vdev)
355{
356 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
357 struct virtqueue *vq, *n;
358
359 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
360 vm_del_vq(vq);
361
362 free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev);
363}
364
Jason Wang9e9b2892022-05-27 14:01:16 +0800365static void vm_synchronize_cbs(struct virtio_device *vdev)
366{
367 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
368
369 synchronize_irq(platform_get_irq(vm_dev->pdev, 0));
370}
371
Solomon Tan31532342022-04-18 02:54:35 +0000372static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned int index,
Pawel Molledfd52e2011-10-24 14:07:03 +0100373 void (*callback)(struct virtqueue *vq),
Michael S. Tsirkinc62f61b2022-08-16 01:36:35 -0400374 const char *name, bool ctx)
Pawel Molledfd52e2011-10-24 14:07:03 +0100375{
376 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Viktor Prutyanovaf8ecec2023-04-13 11:18:54 +0300377 bool (*notify)(struct virtqueue *vq);
Pawel Molledfd52e2011-10-24 14:07:03 +0100378 struct virtio_mmio_vq_info *info;
379 struct virtqueue *vq;
Andy Lutomirskib4211132016-02-02 21:46:38 -0800380 unsigned long flags;
381 unsigned int num;
Pawel Molledfd52e2011-10-24 14:07:03 +0100382 int err;
383
Viktor Prutyanovaf8ecec2023-04-13 11:18:54 +0300384 if (__virtio_test_bit(vdev, VIRTIO_F_NOTIFICATION_DATA))
385 notify = vm_notify_with_data;
386 else
387 notify = vm_notify;
388
Michael S. Tsirkin6457f122012-09-05 21:47:45 +0300389 if (!name)
390 return NULL;
391
Pawel Molledfd52e2011-10-24 14:07:03 +0100392 /* Select the queue we're interested in */
393 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
394
395 /* Queue shouldn't already be set up. */
Pawel Moll1862ee22015-01-23 14:45:55 +1030396 if (readl(vm_dev->base + (vm_dev->version == 1 ?
397 VIRTIO_MMIO_QUEUE_PFN : VIRTIO_MMIO_QUEUE_READY))) {
Pawel Molledfd52e2011-10-24 14:07:03 +0100398 err = -ENOENT;
399 goto error_available;
400 }
401
402 /* Allocate and fill out our active queue description */
403 info = kmalloc(sizeof(*info), GFP_KERNEL);
404 if (!info) {
405 err = -ENOMEM;
406 goto error_kmalloc;
407 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100408
Andy Lutomirskib4211132016-02-02 21:46:38 -0800409 num = readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX);
410 if (num == 0) {
Brian Foleyd78b5192012-09-24 14:33:42 +0100411 err = -ENOENT;
Andy Lutomirskib4211132016-02-02 21:46:38 -0800412 goto error_new_virtqueue;
Pawel Molledfd52e2011-10-24 14:07:03 +0100413 }
414
Pawel Molledfd52e2011-10-24 14:07:03 +0100415 /* Create the vring */
Michael S. Tsirkinc62f61b2022-08-16 01:36:35 -0400416 vq = vring_create_virtqueue(index, num, VIRTIO_MMIO_VRING_ALIGN, vdev,
Viktor Prutyanovaf8ecec2023-04-13 11:18:54 +0300417 true, true, ctx, notify, callback, name);
Pawel Molledfd52e2011-10-24 14:07:03 +0100418 if (!vq) {
419 err = -ENOMEM;
420 goto error_new_virtqueue;
421 }
422
Xuan Zhuoda802962022-08-01 14:38:21 +0800423 vq->num_max = num;
424
Pawel Moll1862ee22015-01-23 14:45:55 +1030425 /* Activate the queue */
Andy Lutomirskib4211132016-02-02 21:46:38 -0800426 writel(virtqueue_get_vring_size(vq), vm_dev->base + VIRTIO_MMIO_QUEUE_NUM);
Pawel Moll1862ee22015-01-23 14:45:55 +1030427 if (vm_dev->version == 1) {
Suzuki K Poulose3fc92a92018-07-18 10:18:44 +0100428 u64 q_pfn = virtqueue_get_desc_addr(vq) >> PAGE_SHIFT;
429
430 /*
431 * virtio-mmio v1 uses a 32bit QUEUE PFN. If we have something
432 * that doesn't fit in 32bit, fail the setup rather than
433 * pretending to be successful.
434 */
435 if (q_pfn >> 32) {
436 dev_err(&vdev->dev,
437 "platform bug: legacy virtio-mmio must not be used with RAM above 0x%llxGB\n",
438 0x1ULL << (32 + PAGE_SHIFT - 30));
439 err = -E2BIG;
440 goto error_bad_pfn;
441 }
442
Pawel Moll1862ee22015-01-23 14:45:55 +1030443 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN);
Suzuki K Poulose3fc92a92018-07-18 10:18:44 +0100444 writel(q_pfn, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
Pawel Moll1862ee22015-01-23 14:45:55 +1030445 } else {
446 u64 addr;
447
Andy Lutomirskib4211132016-02-02 21:46:38 -0800448 addr = virtqueue_get_desc_addr(vq);
Pawel Moll1862ee22015-01-23 14:45:55 +1030449 writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_LOW);
450 writel((u32)(addr >> 32),
451 vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_HIGH);
452
Andy Lutomirskib4211132016-02-02 21:46:38 -0800453 addr = virtqueue_get_avail_addr(vq);
Pawel Moll1862ee22015-01-23 14:45:55 +1030454 writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_LOW);
455 writel((u32)(addr >> 32),
456 vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_HIGH);
457
Andy Lutomirskib4211132016-02-02 21:46:38 -0800458 addr = virtqueue_get_used_addr(vq);
Pawel Moll1862ee22015-01-23 14:45:55 +1030459 writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_USED_LOW);
460 writel((u32)(addr >> 32),
461 vm_dev->base + VIRTIO_MMIO_QUEUE_USED_HIGH);
462
463 writel(1, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
464 }
465
Pawel Molledfd52e2011-10-24 14:07:03 +0100466 vq->priv = info;
467 info->vq = vq;
468
469 spin_lock_irqsave(&vm_dev->lock, flags);
470 list_add(&info->node, &vm_dev->virtqueues);
471 spin_unlock_irqrestore(&vm_dev->lock, flags);
472
473 return vq;
474
Suzuki K Poulose3fc92a92018-07-18 10:18:44 +0100475error_bad_pfn:
476 vring_del_virtqueue(vq);
Pawel Molledfd52e2011-10-24 14:07:03 +0100477error_new_virtqueue:
Pawel Moll1862ee22015-01-23 14:45:55 +1030478 if (vm_dev->version == 1) {
479 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
480 } else {
481 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
482 WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY));
483 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100484 kfree(info);
485error_kmalloc:
486error_available:
487 return ERR_PTR(err);
488}
489
Solomon Tan31532342022-04-18 02:54:35 +0000490static int vm_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
Pawel Molledfd52e2011-10-24 14:07:03 +0100491 struct virtqueue *vqs[],
492 vq_callback_t *callbacks[],
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +0100493 const char * const names[],
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +0200494 const bool *ctx,
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +0100495 struct irq_affinity *desc)
Pawel Molledfd52e2011-10-24 14:07:03 +0100496{
497 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Ihor Matushchak5e663f02019-07-02 17:48:18 +0300498 int irq = platform_get_irq(vm_dev->pdev, 0);
Wei Wanga2299892018-12-28 10:26:26 +0800499 int i, err, queue_idx = 0;
Pawel Molledfd52e2011-10-24 14:07:03 +0100500
Markus Elfring0c35c672020-04-05 19:14:10 +0200501 if (irq < 0)
Ihor Matushchak5e663f02019-07-02 17:48:18 +0300502 return irq;
Ihor Matushchak5e663f02019-07-02 17:48:18 +0300503
Pawel Molledfd52e2011-10-24 14:07:03 +0100504 err = request_irq(irq, vm_interrupt, IRQF_SHARED,
505 dev_name(&vdev->dev), vm_dev);
506 if (err)
507 return err;
508
Minghao Xue02213272022-06-10 16:58:27 +0800509 if (of_property_read_bool(vm_dev->pdev->dev.of_node, "wakeup-source"))
510 enable_irq_wake(irq);
511
Pawel Molledfd52e2011-10-24 14:07:03 +0100512 for (i = 0; i < nvqs; ++i) {
Wei Wanga2299892018-12-28 10:26:26 +0800513 if (!names[i]) {
514 vqs[i] = NULL;
515 continue;
516 }
517
518 vqs[i] = vm_setup_vq(vdev, queue_idx++, callbacks[i], names[i],
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +0200519 ctx ? ctx[i] : false);
Pawel Molledfd52e2011-10-24 14:07:03 +0100520 if (IS_ERR(vqs[i])) {
521 vm_del_vqs(vdev);
522 return PTR_ERR(vqs[i]);
523 }
524 }
525
526 return 0;
527}
528
Rick Jones66846042011-11-14 14:17:08 +0000529static const char *vm_bus_name(struct virtio_device *vdev)
530{
531 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100532
Rick Jones66846042011-11-14 14:17:08 +0000533 return vm_dev->pdev->name;
534}
Pawel Molledfd52e2011-10-24 14:07:03 +0100535
Sebastien Boeuf38e89542020-08-19 18:19:43 -0400536static bool vm_get_shm_region(struct virtio_device *vdev,
537 struct virtio_shm_region *region, u8 id)
538{
539 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
540 u64 len, addr;
541
542 /* Select the region we're interested in */
543 writel(id, vm_dev->base + VIRTIO_MMIO_SHM_SEL);
544
545 /* Read the region size */
546 len = (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_LEN_LOW);
547 len |= (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_LEN_HIGH) << 32;
548
549 region->len = len;
550
551 /* Check if region length is -1. If that's the case, the shared memory
552 * region does not exist and there is no need to proceed further.
553 */
554 if (len == ~(u64)0)
555 return false;
556
557 /* Read the region base address */
558 addr = (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_BASE_LOW);
559 addr |= (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_BASE_HIGH) << 32;
560
561 region->addr = addr;
562
563 return true;
564}
565
Stephen Hemminger93503932013-02-10 15:57:38 +1030566static const struct virtio_config_ops virtio_mmio_config_ops = {
Pawel Molledfd52e2011-10-24 14:07:03 +0100567 .get = vm_get,
568 .set = vm_set,
Michael S. Tsirkin87e7bf12015-03-12 12:56:43 +1030569 .generation = vm_generation,
Pawel Molledfd52e2011-10-24 14:07:03 +0100570 .get_status = vm_get_status,
571 .set_status = vm_set_status,
572 .reset = vm_reset,
573 .find_vqs = vm_find_vqs,
574 .del_vqs = vm_del_vqs,
575 .get_features = vm_get_features,
576 .finalize_features = vm_finalize_features,
Rick Jones66846042011-11-14 14:17:08 +0000577 .bus_name = vm_bus_name,
Sebastien Boeuf38e89542020-08-19 18:19:43 -0400578 .get_shm_region = vm_get_shm_region,
Jason Wang9e9b2892022-05-27 14:01:16 +0800579 .synchronize_cbs = vm_synchronize_cbs,
Pawel Molledfd52e2011-10-24 14:07:03 +0100580};
581
Stephan Gerholded7ac372022-06-21 13:06:20 +0200582#ifdef CONFIG_PM_SLEEP
583static int virtio_mmio_freeze(struct device *dev)
584{
585 struct virtio_mmio_device *vm_dev = dev_get_drvdata(dev);
586
587 return virtio_device_freeze(&vm_dev->vdev);
588}
589
590static int virtio_mmio_restore(struct device *dev)
591{
592 struct virtio_mmio_device *vm_dev = dev_get_drvdata(dev);
593
Stephan Gerholde0c2ce82022-06-21 13:06:21 +0200594 if (vm_dev->version == 1)
595 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
596
Stephan Gerholded7ac372022-06-21 13:06:20 +0200597 return virtio_device_restore(&vm_dev->vdev);
598}
599
600static const struct dev_pm_ops virtio_mmio_pm_ops = {
601 SET_SYSTEM_SLEEP_PM_OPS(virtio_mmio_freeze, virtio_mmio_restore)
602};
603#endif
Pawel Molledfd52e2011-10-24 14:07:03 +0100604
weiping zhang7eb781b2017-12-06 21:59:16 +0800605static void virtio_mmio_release_dev(struct device *_d)
606{
607 struct virtio_device *vdev =
608 container_of(_d, struct virtio_device, dev);
Tang Binda98b542021-02-22 13:57:24 +0800609 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
weiping zhang7eb781b2017-12-06 21:59:16 +0800610 struct platform_device *pdev = vm_dev->pdev;
611
612 devm_kfree(&pdev->dev, vm_dev);
613}
Pawel Molledfd52e2011-10-24 14:07:03 +0100614
615/* Platform device */
616
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800617static int virtio_mmio_probe(struct platform_device *pdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100618{
619 struct virtio_mmio_device *vm_dev;
Pawel Molledfd52e2011-10-24 14:07:03 +0100620 unsigned long magic;
Robin Murphyf7f66342017-01-10 17:51:17 +0000621 int rc;
Pawel Molledfd52e2011-10-24 14:07:03 +0100622
Pawel Molledfd52e2011-10-24 14:07:03 +0100623 vm_dev = devm_kzalloc(&pdev->dev, sizeof(*vm_dev), GFP_KERNEL);
Mark Rutlandc2e90802017-12-12 13:45:50 +0000624 if (!vm_dev)
625 return -ENOMEM;
Pawel Molledfd52e2011-10-24 14:07:03 +0100626
627 vm_dev->vdev.dev.parent = &pdev->dev;
weiping zhang7eb781b2017-12-06 21:59:16 +0800628 vm_dev->vdev.dev.release = virtio_mmio_release_dev;
Pawel Molledfd52e2011-10-24 14:07:03 +0100629 vm_dev->vdev.config = &virtio_mmio_config_ops;
630 vm_dev->pdev = pdev;
631 INIT_LIST_HEAD(&vm_dev->virtqueues);
632 spin_lock_init(&vm_dev->lock);
633
Yangtao Lic64eb622019-12-22 19:08:39 +0000634 vm_dev->base = devm_platform_ioremap_resource(pdev, 0);
635 if (IS_ERR(vm_dev->base))
636 return PTR_ERR(vm_dev->base);
Pawel Molledfd52e2011-10-24 14:07:03 +0100637
638 /* Check magic value */
639 magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE);
Marc Zyngier4ae85372013-11-05 21:21:28 +1030640 if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
Pawel Molledfd52e2011-10-24 14:07:03 +0100641 dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
Mark Rutlandc2e90802017-12-12 13:45:50 +0000642 return -ENODEV;
Pawel Molledfd52e2011-10-24 14:07:03 +0100643 }
644
645 /* Check device version */
646 vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION);
Pawel Moll1862ee22015-01-23 14:45:55 +1030647 if (vm_dev->version < 1 || vm_dev->version > 2) {
Pawel Molledfd52e2011-10-24 14:07:03 +0100648 dev_err(&pdev->dev, "Version %ld not supported!\n",
649 vm_dev->version);
Mark Rutlandc2e90802017-12-12 13:45:50 +0000650 return -ENXIO;
Pawel Molledfd52e2011-10-24 14:07:03 +0100651 }
652
653 vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID);
Pawel Moll1862ee22015-01-23 14:45:55 +1030654 if (vm_dev->vdev.id.device == 0) {
655 /*
656 * virtio-mmio device with an ID 0 is a (dummy) placeholder
657 * with no function. End probing now with no error reported.
658 */
Mark Rutlandc2e90802017-12-12 13:45:50 +0000659 return -ENODEV;
Pawel Moll1862ee22015-01-23 14:45:55 +1030660 }
Pawel Molledfd52e2011-10-24 14:07:03 +0100661 vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
662
Robin Murphyf7f66342017-01-10 17:51:17 +0000663 if (vm_dev->version == 1) {
Pawel Moll1862ee22015-01-23 14:45:55 +1030664 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
Pawel Molledfd52e2011-10-24 14:07:03 +0100665
Robin Murphyf7f66342017-01-10 17:51:17 +0000666 rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
667 /*
668 * In the legacy case, ensure our coherently-allocated virtio
669 * ring will be at an address expressable as a 32-bit PFN.
670 */
671 if (!rc)
672 dma_set_coherent_mask(&pdev->dev,
673 DMA_BIT_MASK(32 + PAGE_SHIFT));
674 } else {
675 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
676 }
677 if (rc)
678 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
679 if (rc)
680 dev_warn(&pdev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
681
Pawel Molledfd52e2011-10-24 14:07:03 +0100682 platform_set_drvdata(pdev, vm_dev);
683
weiping zhang7eb781b2017-12-06 21:59:16 +0800684 rc = register_virtio_device(&vm_dev->vdev);
Mark Rutlandc2e90802017-12-12 13:45:50 +0000685 if (rc)
weiping zhang7eb781b2017-12-06 21:59:16 +0800686 put_device(&vm_dev->vdev.dev);
Mark Rutlandc2e90802017-12-12 13:45:50 +0000687
weiping zhang7eb781b2017-12-06 21:59:16 +0800688 return rc;
Pawel Molledfd52e2011-10-24 14:07:03 +0100689}
690
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800691static int virtio_mmio_remove(struct platform_device *pdev)
Pawel Molledfd52e2011-10-24 14:07:03 +0100692{
693 struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev);
Pawel Molledfd52e2011-10-24 14:07:03 +0100694 unregister_virtio_device(&vm_dev->vdev);
695
696 return 0;
697}
698
699
700
Pawel Moll81a054c2012-05-09 18:30:16 +0100701/* Devices list parameter */
702
703#if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
704
705static struct device vm_cmdline_parent = {
706 .init_name = "virtio-mmio-cmdline",
707};
708
709static int vm_cmdline_parent_registered;
710static int vm_cmdline_id;
711
712static int vm_cmdline_set(const char *device,
713 const struct kernel_param *kp)
714{
715 int err;
716 struct resource resources[2] = {};
717 char *str;
Solomon Tan0619eda2022-04-18 02:56:24 +0000718 long long base, size;
Pawel Moll40f99382012-11-22 12:30:24 +1030719 unsigned int irq;
Pawel Moll81a054c2012-05-09 18:30:16 +0100720 int processed, consumed = 0;
721 struct platform_device *pdev;
722
Pawel Moll40f99382012-11-22 12:30:24 +1030723 /* Consume "size" part of the command line parameter */
724 size = memparse(device, &str);
Pawel Moll81a054c2012-05-09 18:30:16 +0100725
Pawel Moll40f99382012-11-22 12:30:24 +1030726 /* Get "@<base>:<irq>[:<id>]" chunks */
Pawel Moll81a054c2012-05-09 18:30:16 +0100727 processed = sscanf(str, "@%lli:%u%n:%d%n",
Pawel Moll40f99382012-11-22 12:30:24 +1030728 &base, &irq, &consumed,
Pawel Moll81a054c2012-05-09 18:30:16 +0100729 &vm_cmdline_id, &consumed);
730
Pawel Moll40f99382012-11-22 12:30:24 +1030731 /*
Bjorn Helgaas62ca18a2020-07-01 15:53:15 -0500732 * sscanf() must process at least 2 chunks; also there
Pawel Moll40f99382012-11-22 12:30:24 +1030733 * must be no extra characters after the last chunk, so
734 * str[consumed] must be '\0'
735 */
Bjorn Helgaas62ca18a2020-07-01 15:53:15 -0500736 if (processed < 2 || str[consumed] || irq == 0)
Pawel Moll81a054c2012-05-09 18:30:16 +0100737 return -EINVAL;
738
Pawel Moll40f99382012-11-22 12:30:24 +1030739 resources[0].flags = IORESOURCE_MEM;
Pawel Moll81a054c2012-05-09 18:30:16 +0100740 resources[0].start = base;
Pawel Moll40f99382012-11-22 12:30:24 +1030741 resources[0].end = base + size - 1;
742
743 resources[1].flags = IORESOURCE_IRQ;
744 resources[1].start = resources[1].end = irq;
Pawel Moll81a054c2012-05-09 18:30:16 +0100745
746 if (!vm_cmdline_parent_registered) {
747 err = device_register(&vm_cmdline_parent);
748 if (err) {
chengkaitaoa58a7f92022-06-02 08:55:42 +0800749 put_device(&vm_cmdline_parent);
Pawel Moll81a054c2012-05-09 18:30:16 +0100750 pr_err("Failed to register parent device!\n");
751 return err;
752 }
753 vm_cmdline_parent_registered = 1;
754 }
755
756 pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
757 vm_cmdline_id,
758 (unsigned long long)resources[0].start,
759 (unsigned long long)resources[0].end,
760 (int)resources[1].start);
761
762 pdev = platform_device_register_resndata(&vm_cmdline_parent,
763 "virtio-mmio", vm_cmdline_id++,
764 resources, ARRAY_SIZE(resources), NULL, 0);
Pawel Moll81a054c2012-05-09 18:30:16 +0100765
Vasyl Gomonovychc2c9f9b2017-11-28 16:15:47 +0100766 return PTR_ERR_OR_ZERO(pdev);
Pawel Moll81a054c2012-05-09 18:30:16 +0100767}
768
769static int vm_cmdline_get_device(struct device *dev, void *data)
770{
771 char *buffer = data;
772 unsigned int len = strlen(buffer);
773 struct platform_device *pdev = to_platform_device(dev);
774
775 snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n",
776 pdev->resource[0].end - pdev->resource[0].start + 1ULL,
777 (unsigned long long)pdev->resource[0].start,
778 (unsigned long long)pdev->resource[1].start,
779 pdev->id);
780 return 0;
781}
782
783static int vm_cmdline_get(char *buffer, const struct kernel_param *kp)
784{
785 buffer[0] = '\0';
786 device_for_each_child(&vm_cmdline_parent, buffer,
787 vm_cmdline_get_device);
788 return strlen(buffer) + 1;
789}
790
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930791static const struct kernel_param_ops vm_cmdline_param_ops = {
Pawel Moll81a054c2012-05-09 18:30:16 +0100792 .set = vm_cmdline_set,
793 .get = vm_cmdline_get,
794};
795
796device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR);
797
798static int vm_unregister_cmdline_device(struct device *dev,
799 void *data)
800{
801 platform_device_unregister(to_platform_device(dev));
802
803 return 0;
804}
805
806static void vm_unregister_cmdline_devices(void)
807{
808 if (vm_cmdline_parent_registered) {
809 device_for_each_child(&vm_cmdline_parent, NULL,
810 vm_unregister_cmdline_device);
811 device_unregister(&vm_cmdline_parent);
812 vm_cmdline_parent_registered = 0;
813 }
814}
815
816#else
817
818static void vm_unregister_cmdline_devices(void)
819{
820}
821
822#endif
823
Pawel Molledfd52e2011-10-24 14:07:03 +0100824/* Platform driver */
825
Arvind Yadav31919142017-06-19 11:44:41 +0530826static const struct of_device_id virtio_mmio_match[] = {
Pawel Molledfd52e2011-10-24 14:07:03 +0100827 { .compatible = "virtio,mmio", },
828 {},
829};
830MODULE_DEVICE_TABLE(of, virtio_mmio_match);
831
Graeme Gregory38c4ab82015-07-28 10:44:02 +0100832#ifdef CONFIG_ACPI
833static const struct acpi_device_id virtio_mmio_acpi_match[] = {
834 { "LNRO0005", },
835 { }
836};
837MODULE_DEVICE_TABLE(acpi, virtio_mmio_acpi_match);
838#endif
839
Pawel Molledfd52e2011-10-24 14:07:03 +0100840static struct platform_driver virtio_mmio_driver = {
841 .probe = virtio_mmio_probe,
Greg Kroah-Hartman8590dbc2012-12-21 13:05:30 -0800842 .remove = virtio_mmio_remove,
Pawel Molledfd52e2011-10-24 14:07:03 +0100843 .driver = {
844 .name = "virtio-mmio",
Pawel Molledfd52e2011-10-24 14:07:03 +0100845 .of_match_table = virtio_mmio_match,
Graeme Gregory38c4ab82015-07-28 10:44:02 +0100846 .acpi_match_table = ACPI_PTR(virtio_mmio_acpi_match),
Stephan Gerholded7ac372022-06-21 13:06:20 +0200847#ifdef CONFIG_PM_SLEEP
848 .pm = &virtio_mmio_pm_ops,
849#endif
Pawel Molledfd52e2011-10-24 14:07:03 +0100850 },
851};
852
853static int __init virtio_mmio_init(void)
854{
855 return platform_driver_register(&virtio_mmio_driver);
856}
857
858static void __exit virtio_mmio_exit(void)
859{
860 platform_driver_unregister(&virtio_mmio_driver);
Pawel Moll81a054c2012-05-09 18:30:16 +0100861 vm_unregister_cmdline_devices();
Pawel Molledfd52e2011-10-24 14:07:03 +0100862}
863
864module_init(virtio_mmio_init);
865module_exit(virtio_mmio_exit);
866
867MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
868MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
869MODULE_LICENSE("GPL");