Commit | Line | Data |
---|---|---|
f33f5fe2 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
edfd52e6 PM |
2 | /* |
3 | * Virtio memory mapped device driver | |
4 | * | |
1862ee22 | 5 | * Copyright 2011-2014, ARM Ltd. |
edfd52e6 PM |
6 | * |
7 | * This module allows virtio devices to be used over a virtual, memory mapped | |
8 | * platform device. | |
9 | * | |
81a054ce PM |
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 | * | |
edfd52e6 | 52 | * Based on Virtio PCI driver by Anthony Liguori, copyright IBM Corp. 2007 |
edfd52e6 PM |
53 | */ |
54 | ||
81a054ce PM |
55 | #define pr_fmt(fmt) "virtio-mmio: " fmt |
56 | ||
38c4ab8e | 57 | #include <linux/acpi.h> |
f7f6634d | 58 | #include <linux/dma-mapping.h> |
edfd52e6 PM |
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> | |
c5111a5b | 64 | #include <linux/of.h> |
edfd52e6 | 65 | #include <linux/platform_device.h> |
ed7ac37f | 66 | #include <linux/pm.h> |
edfd52e6 PM |
67 | #include <linux/slab.h> |
68 | #include <linux/spinlock.h> | |
69 | #include <linux/virtio.h> | |
70 | #include <linux/virtio_config.h> | |
51be7a9a | 71 | #include <uapi/linux/virtio_mmio.h> |
edfd52e6 PM |
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 | /* a list of queues so we can dispatch IRQs */ | |
93 | spinlock_t lock; | |
94 | struct list_head virtqueues; | |
95 | }; | |
96 | ||
97 | struct virtio_mmio_vq_info { | |
98 | /* the actual virtqueue */ | |
99 | struct virtqueue *vq; | |
100 | ||
edfd52e6 PM |
101 | /* the list node for the virtqueues list */ |
102 | struct list_head node; | |
103 | }; | |
104 | ||
105 | ||
106 | ||
107 | /* Configuration interface */ | |
108 | ||
d0254773 | 109 | static u64 vm_get_features(struct virtio_device *vdev) |
edfd52e6 PM |
110 | { |
111 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); | |
1862ee22 PM |
112 | u64 features; |
113 | ||
114 | writel(1, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL); | |
115 | features = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES); | |
116 | features <<= 32; | |
edfd52e6 | 117 | |
1862ee22 PM |
118 | writel(0, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL); |
119 | features |= readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES); | |
edfd52e6 | 120 | |
1862ee22 | 121 | return features; |
edfd52e6 PM |
122 | } |
123 | ||
5c609a5e | 124 | static int vm_finalize_features(struct virtio_device *vdev) |
edfd52e6 PM |
125 | { |
126 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); | |
edfd52e6 PM |
127 | |
128 | /* Give virtio_ring a chance to accept features. */ | |
129 | vring_transport_features(vdev); | |
130 | ||
0c4aeb4b | 131 | /* Make sure there are no mixed devices */ |
1862ee22 PM |
132 | 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 | } | |
137 | ||
138 | writel(1, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL); | |
139 | writel((u32)(vdev->features >> 32), | |
140 | vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES); | |
93d389f8 | 141 | |
1862ee22 PM |
142 | writel(0, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL); |
143 | writel((u32)vdev->features, | |
144 | vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES); | |
5c609a5e MT |
145 | |
146 | return 0; | |
edfd52e6 PM |
147 | } |
148 | ||
31532340 ST |
149 | static void vm_get(struct virtio_device *vdev, unsigned int offset, |
150 | void *buf, unsigned int len) | |
edfd52e6 PM |
151 | { |
152 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); | |
704a0b5f MT |
153 | void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG; |
154 | u8 b; | |
155 | __le16 w; | |
156 | __le32 l; | |
157 | ||
158 | 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 | } | |
edfd52e6 | 166 | |
704a0b5f MT |
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 | } | |
edfd52e6 PM |
189 | } |
190 | ||
31532340 ST |
191 | static void vm_set(struct virtio_device *vdev, unsigned int offset, |
192 | const void *buf, unsigned int len) | |
edfd52e6 PM |
193 | { |
194 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); | |
704a0b5f MT |
195 | void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG; |
196 | u8 b; | |
197 | __le16 w; | |
198 | __le32 l; | |
199 | ||
200 | 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); | |
edfd52e6 | 206 | |
704a0b5f MT |
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 | } | |
edfd52e6 PM |
232 | } |
233 | ||
87e7bf14 MT |
234 | static 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 | ||
edfd52e6 PM |
244 | static 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 | ||
251 | static 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 | ||
8b4ec69d JW |
258 | /* |
259 | * Per memory-barriers.txt, wmb() is not needed to guarantee | |
acb0055e | 260 | * that the cache coherent memory writes have completed |
8b4ec69d JW |
261 | * before writing to the MMIO region. |
262 | */ | |
edfd52e6 PM |
263 | writel(status, vm_dev->base + VIRTIO_MMIO_STATUS); |
264 | } | |
265 | ||
266 | static 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 */ | |
46f9c2b9 | 279 | static bool vm_notify(struct virtqueue *vq) |
edfd52e6 PM |
280 | { |
281 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev); | |
edfd52e6 PM |
282 | |
283 | /* We write the queue's selector into the notification register to | |
284 | * signal the other end */ | |
06ca287d | 285 | writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY); |
46f9c2b9 | 286 | return true; |
edfd52e6 PM |
287 | } |
288 | ||
af8ececd VP |
289 | static 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 | ||
edfd52e6 PM |
299 | /* Notify all virtqueues on an interrupt. */ |
300 | static irqreturn_t vm_interrupt(int irq, void *opaque) | |
301 | { | |
302 | struct virtio_mmio_device *vm_dev = opaque; | |
303 | struct virtio_mmio_vq_info *info; | |
edfd52e6 PM |
304 | 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 | ||
016c98c6 MT |
312 | if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)) { |
313 | virtio_config_changed(&vm_dev->vdev); | |
edfd52e6 PM |
314 | 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 | ||
329 | static 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; | |
b4211138 | 333 | unsigned long flags; |
06ca287d | 334 | unsigned int index = vq->index; |
edfd52e6 PM |
335 | |
336 | spin_lock_irqsave(&vm_dev->lock, flags); | |
337 | list_del(&info->node); | |
338 | spin_unlock_irqrestore(&vm_dev->lock, flags); | |
339 | ||
edfd52e6 | 340 | /* Select and deactivate the queue */ |
17bb6d40 | 341 | writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL); |
1862ee22 PM |
342 | 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 | } | |
edfd52e6 | 348 | |
b4211138 AL |
349 | vring_del_virtqueue(vq); |
350 | ||
edfd52e6 PM |
351 | kfree(info); |
352 | } | |
353 | ||
354 | static 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 | ||
9e9b2893 JW |
365 | static 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 | ||
31532340 | 372 | static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned int index, |
edfd52e6 | 373 | void (*callback)(struct virtqueue *vq), |
c62f61b5 | 374 | const char *name, bool ctx) |
edfd52e6 PM |
375 | { |
376 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); | |
af8ececd | 377 | bool (*notify)(struct virtqueue *vq); |
edfd52e6 PM |
378 | struct virtio_mmio_vq_info *info; |
379 | struct virtqueue *vq; | |
b4211138 AL |
380 | unsigned long flags; |
381 | unsigned int num; | |
edfd52e6 PM |
382 | int err; |
383 | ||
af8ececd VP |
384 | if (__virtio_test_bit(vdev, VIRTIO_F_NOTIFICATION_DATA)) |
385 | notify = vm_notify_with_data; | |
386 | else | |
387 | notify = vm_notify; | |
388 | ||
6457f126 MT |
389 | if (!name) |
390 | return NULL; | |
391 | ||
edfd52e6 PM |
392 | /* 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. */ | |
1862ee22 PM |
396 | if (readl(vm_dev->base + (vm_dev->version == 1 ? |
397 | VIRTIO_MMIO_QUEUE_PFN : VIRTIO_MMIO_QUEUE_READY))) { | |
edfd52e6 PM |
398 | 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 | } | |
edfd52e6 | 408 | |
b4211138 AL |
409 | num = readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX); |
410 | if (num == 0) { | |
d78b519f | 411 | err = -ENOENT; |
b4211138 | 412 | goto error_new_virtqueue; |
edfd52e6 PM |
413 | } |
414 | ||
edfd52e6 | 415 | /* Create the vring */ |
c62f61b5 | 416 | vq = vring_create_virtqueue(index, num, VIRTIO_MMIO_VRING_ALIGN, vdev, |
af8ececd | 417 | true, true, ctx, notify, callback, name); |
edfd52e6 PM |
418 | if (!vq) { |
419 | err = -ENOMEM; | |
420 | goto error_new_virtqueue; | |
421 | } | |
422 | ||
da802961 XZ |
423 | vq->num_max = num; |
424 | ||
1862ee22 | 425 | /* Activate the queue */ |
b4211138 | 426 | writel(virtqueue_get_vring_size(vq), vm_dev->base + VIRTIO_MMIO_QUEUE_NUM); |
1862ee22 | 427 | if (vm_dev->version == 1) { |
3fc92a96 SP |
428 | 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 | ||
1862ee22 | 443 | writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN); |
3fc92a96 | 444 | writel(q_pfn, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN); |
1862ee22 PM |
445 | } else { |
446 | u64 addr; | |
447 | ||
b4211138 | 448 | addr = virtqueue_get_desc_addr(vq); |
1862ee22 PM |
449 | 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 | ||
b4211138 | 453 | addr = virtqueue_get_avail_addr(vq); |
1862ee22 PM |
454 | 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 | ||
b4211138 | 458 | addr = virtqueue_get_used_addr(vq); |
1862ee22 PM |
459 | 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 | ||
edfd52e6 PM |
466 | 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 | ||
3fc92a96 SP |
475 | error_bad_pfn: |
476 | vring_del_virtqueue(vq); | |
edfd52e6 | 477 | error_new_virtqueue: |
1862ee22 PM |
478 | 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 | } | |
edfd52e6 PM |
484 | kfree(info); |
485 | error_kmalloc: | |
486 | error_available: | |
487 | return ERR_PTR(err); | |
488 | } | |
489 | ||
31532340 | 490 | static int vm_find_vqs(struct virtio_device *vdev, unsigned int nvqs, |
edfd52e6 PM |
491 | struct virtqueue *vqs[], |
492 | vq_callback_t *callbacks[], | |
fb5e31d9 | 493 | const char * const names[], |
f94682dd | 494 | const bool *ctx, |
fb5e31d9 | 495 | struct irq_affinity *desc) |
edfd52e6 PM |
496 | { |
497 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); | |
5e663f04 | 498 | int irq = platform_get_irq(vm_dev->pdev, 0); |
a229989d | 499 | int i, err, queue_idx = 0; |
edfd52e6 | 500 | |
0c35c674 | 501 | if (irq < 0) |
5e663f04 | 502 | return irq; |
5e663f04 | 503 | |
edfd52e6 PM |
504 | err = request_irq(irq, vm_interrupt, IRQF_SHARED, |
505 | dev_name(&vdev->dev), vm_dev); | |
506 | if (err) | |
507 | return err; | |
508 | ||
02213273 MX |
509 | if (of_property_read_bool(vm_dev->pdev->dev.of_node, "wakeup-source")) |
510 | enable_irq_wake(irq); | |
511 | ||
edfd52e6 | 512 | for (i = 0; i < nvqs; ++i) { |
a229989d WW |
513 | if (!names[i]) { |
514 | vqs[i] = NULL; | |
515 | continue; | |
516 | } | |
517 | ||
518 | vqs[i] = vm_setup_vq(vdev, queue_idx++, callbacks[i], names[i], | |
f94682dd | 519 | ctx ? ctx[i] : false); |
edfd52e6 PM |
520 | if (IS_ERR(vqs[i])) { |
521 | vm_del_vqs(vdev); | |
522 | return PTR_ERR(vqs[i]); | |
523 | } | |
524 | } | |
525 | ||
526 | return 0; | |
527 | } | |
528 | ||
66846048 RJ |
529 | static const char *vm_bus_name(struct virtio_device *vdev) |
530 | { | |
531 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); | |
edfd52e6 | 532 | |
66846048 RJ |
533 | return vm_dev->pdev->name; |
534 | } | |
edfd52e6 | 535 | |
38e89548 SB |
536 | static 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 | ||
93503932 | 566 | static const struct virtio_config_ops virtio_mmio_config_ops = { |
edfd52e6 PM |
567 | .get = vm_get, |
568 | .set = vm_set, | |
87e7bf14 | 569 | .generation = vm_generation, |
edfd52e6 PM |
570 | .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, | |
66846048 | 577 | .bus_name = vm_bus_name, |
38e89548 | 578 | .get_shm_region = vm_get_shm_region, |
9e9b2893 | 579 | .synchronize_cbs = vm_synchronize_cbs, |
edfd52e6 PM |
580 | }; |
581 | ||
ed7ac37f SG |
582 | #ifdef CONFIG_PM_SLEEP |
583 | static 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 | ||
590 | static int virtio_mmio_restore(struct device *dev) | |
591 | { | |
592 | struct virtio_mmio_device *vm_dev = dev_get_drvdata(dev); | |
593 | ||
e0c2ce82 SG |
594 | if (vm_dev->version == 1) |
595 | writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE); | |
596 | ||
ed7ac37f SG |
597 | return virtio_device_restore(&vm_dev->vdev); |
598 | } | |
599 | ||
600 | static const struct dev_pm_ops virtio_mmio_pm_ops = { | |
601 | SET_SYSTEM_SLEEP_PM_OPS(virtio_mmio_freeze, virtio_mmio_restore) | |
602 | }; | |
603 | #endif | |
edfd52e6 | 604 | |
7eb781b1 | 605 | static void virtio_mmio_release_dev(struct device *_d) |
606 | { | |
607 | struct virtio_device *vdev = | |
608 | container_of(_d, struct virtio_device, dev); | |
da98b54d | 609 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); |
7eb781b1 | 610 | |
55c91fed | 611 | kfree(vm_dev); |
7eb781b1 | 612 | } |
edfd52e6 PM |
613 | |
614 | /* Platform device */ | |
615 | ||
8590dbc7 | 616 | static int virtio_mmio_probe(struct platform_device *pdev) |
edfd52e6 PM |
617 | { |
618 | struct virtio_mmio_device *vm_dev; | |
edfd52e6 | 619 | unsigned long magic; |
f7f6634d | 620 | int rc; |
edfd52e6 | 621 | |
55c91fed | 622 | vm_dev = kzalloc(sizeof(*vm_dev), GFP_KERNEL); |
c2e90800 MR |
623 | if (!vm_dev) |
624 | return -ENOMEM; | |
edfd52e6 PM |
625 | |
626 | vm_dev->vdev.dev.parent = &pdev->dev; | |
7eb781b1 | 627 | vm_dev->vdev.dev.release = virtio_mmio_release_dev; |
edfd52e6 PM |
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 | ||
c64eb62c | 633 | vm_dev->base = devm_platform_ioremap_resource(pdev, 0); |
fab7f259 MH |
634 | if (IS_ERR(vm_dev->base)) { |
635 | rc = PTR_ERR(vm_dev->base); | |
636 | goto free_vm_dev; | |
637 | } | |
edfd52e6 PM |
638 | |
639 | /* Check magic value */ | |
640 | magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE); | |
4ae85370 | 641 | if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) { |
edfd52e6 | 642 | dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic); |
fab7f259 MH |
643 | rc = -ENODEV; |
644 | goto free_vm_dev; | |
edfd52e6 PM |
645 | } |
646 | ||
647 | /* Check device version */ | |
648 | vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION); | |
1862ee22 | 649 | if (vm_dev->version < 1 || vm_dev->version > 2) { |
edfd52e6 PM |
650 | dev_err(&pdev->dev, "Version %ld not supported!\n", |
651 | vm_dev->version); | |
fab7f259 MH |
652 | rc = -ENXIO; |
653 | goto free_vm_dev; | |
edfd52e6 PM |
654 | } |
655 | ||
656 | vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID); | |
1862ee22 PM |
657 | if (vm_dev->vdev.id.device == 0) { |
658 | /* | |
659 | * virtio-mmio device with an ID 0 is a (dummy) placeholder | |
660 | * with no function. End probing now with no error reported. | |
661 | */ | |
fab7f259 MH |
662 | rc = -ENODEV; |
663 | goto free_vm_dev; | |
1862ee22 | 664 | } |
edfd52e6 PM |
665 | vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID); |
666 | ||
f7f6634d | 667 | if (vm_dev->version == 1) { |
1862ee22 | 668 | writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE); |
edfd52e6 | 669 | |
f7f6634d RM |
670 | rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)); |
671 | /* | |
672 | * In the legacy case, ensure our coherently-allocated virtio | |
673 | * ring will be at an address expressable as a 32-bit PFN. | |
674 | */ | |
675 | if (!rc) | |
676 | dma_set_coherent_mask(&pdev->dev, | |
677 | DMA_BIT_MASK(32 + PAGE_SHIFT)); | |
678 | } else { | |
679 | rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); | |
680 | } | |
681 | if (rc) | |
682 | rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); | |
683 | if (rc) | |
684 | dev_warn(&pdev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n"); | |
685 | ||
edfd52e6 PM |
686 | platform_set_drvdata(pdev, vm_dev); |
687 | ||
7eb781b1 | 688 | rc = register_virtio_device(&vm_dev->vdev); |
c2e90800 | 689 | if (rc) |
7eb781b1 | 690 | put_device(&vm_dev->vdev.dev); |
c2e90800 | 691 | |
7eb781b1 | 692 | return rc; |
fab7f259 MH |
693 | |
694 | free_vm_dev: | |
695 | kfree(vm_dev); | |
696 | return rc; | |
edfd52e6 PM |
697 | } |
698 | ||
8590dbc7 | 699 | static int virtio_mmio_remove(struct platform_device *pdev) |
edfd52e6 PM |
700 | { |
701 | struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev); | |
edfd52e6 PM |
702 | unregister_virtio_device(&vm_dev->vdev); |
703 | ||
704 | return 0; | |
705 | } | |
706 | ||
707 | ||
708 | ||
81a054ce PM |
709 | /* Devices list parameter */ |
710 | ||
711 | #if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES) | |
712 | ||
713 | static struct device vm_cmdline_parent = { | |
714 | .init_name = "virtio-mmio-cmdline", | |
715 | }; | |
716 | ||
717 | static int vm_cmdline_parent_registered; | |
718 | static int vm_cmdline_id; | |
719 | ||
720 | static int vm_cmdline_set(const char *device, | |
721 | const struct kernel_param *kp) | |
722 | { | |
723 | int err; | |
724 | struct resource resources[2] = {}; | |
725 | char *str; | |
0619eda8 | 726 | long long base, size; |
40f9938c | 727 | unsigned int irq; |
81a054ce PM |
728 | int processed, consumed = 0; |
729 | struct platform_device *pdev; | |
730 | ||
40f9938c PM |
731 | /* Consume "size" part of the command line parameter */ |
732 | size = memparse(device, &str); | |
81a054ce | 733 | |
40f9938c | 734 | /* Get "@<base>:<irq>[:<id>]" chunks */ |
81a054ce | 735 | processed = sscanf(str, "@%lli:%u%n:%d%n", |
40f9938c | 736 | &base, &irq, &consumed, |
81a054ce PM |
737 | &vm_cmdline_id, &consumed); |
738 | ||
40f9938c | 739 | /* |
62ca18a0 | 740 | * sscanf() must process at least 2 chunks; also there |
40f9938c PM |
741 | * must be no extra characters after the last chunk, so |
742 | * str[consumed] must be '\0' | |
743 | */ | |
62ca18a0 | 744 | if (processed < 2 || str[consumed] || irq == 0) |
81a054ce PM |
745 | return -EINVAL; |
746 | ||
40f9938c | 747 | resources[0].flags = IORESOURCE_MEM; |
81a054ce | 748 | resources[0].start = base; |
40f9938c PM |
749 | resources[0].end = base + size - 1; |
750 | ||
751 | resources[1].flags = IORESOURCE_IRQ; | |
752 | resources[1].start = resources[1].end = irq; | |
81a054ce PM |
753 | |
754 | if (!vm_cmdline_parent_registered) { | |
755 | err = device_register(&vm_cmdline_parent); | |
756 | if (err) { | |
a58a7f97 | 757 | put_device(&vm_cmdline_parent); |
81a054ce PM |
758 | pr_err("Failed to register parent device!\n"); |
759 | return err; | |
760 | } | |
761 | vm_cmdline_parent_registered = 1; | |
762 | } | |
763 | ||
764 | pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n", | |
765 | vm_cmdline_id, | |
766 | (unsigned long long)resources[0].start, | |
767 | (unsigned long long)resources[0].end, | |
768 | (int)resources[1].start); | |
769 | ||
770 | pdev = platform_device_register_resndata(&vm_cmdline_parent, | |
771 | "virtio-mmio", vm_cmdline_id++, | |
772 | resources, ARRAY_SIZE(resources), NULL, 0); | |
81a054ce | 773 | |
c2c9f9bc | 774 | return PTR_ERR_OR_ZERO(pdev); |
81a054ce PM |
775 | } |
776 | ||
777 | static int vm_cmdline_get_device(struct device *dev, void *data) | |
778 | { | |
779 | char *buffer = data; | |
780 | unsigned int len = strlen(buffer); | |
781 | struct platform_device *pdev = to_platform_device(dev); | |
782 | ||
783 | snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n", | |
784 | pdev->resource[0].end - pdev->resource[0].start + 1ULL, | |
785 | (unsigned long long)pdev->resource[0].start, | |
786 | (unsigned long long)pdev->resource[1].start, | |
787 | pdev->id); | |
788 | return 0; | |
789 | } | |
790 | ||
791 | static int vm_cmdline_get(char *buffer, const struct kernel_param *kp) | |
792 | { | |
793 | buffer[0] = '\0'; | |
794 | device_for_each_child(&vm_cmdline_parent, buffer, | |
795 | vm_cmdline_get_device); | |
796 | return strlen(buffer) + 1; | |
797 | } | |
798 | ||
9c27847d | 799 | static const struct kernel_param_ops vm_cmdline_param_ops = { |
81a054ce PM |
800 | .set = vm_cmdline_set, |
801 | .get = vm_cmdline_get, | |
802 | }; | |
803 | ||
804 | device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR); | |
805 | ||
806 | static int vm_unregister_cmdline_device(struct device *dev, | |
807 | void *data) | |
808 | { | |
809 | platform_device_unregister(to_platform_device(dev)); | |
810 | ||
811 | return 0; | |
812 | } | |
813 | ||
814 | static void vm_unregister_cmdline_devices(void) | |
815 | { | |
816 | if (vm_cmdline_parent_registered) { | |
817 | device_for_each_child(&vm_cmdline_parent, NULL, | |
818 | vm_unregister_cmdline_device); | |
819 | device_unregister(&vm_cmdline_parent); | |
820 | vm_cmdline_parent_registered = 0; | |
821 | } | |
822 | } | |
823 | ||
824 | #else | |
825 | ||
826 | static void vm_unregister_cmdline_devices(void) | |
827 | { | |
828 | } | |
829 | ||
830 | #endif | |
831 | ||
edfd52e6 PM |
832 | /* Platform driver */ |
833 | ||
31919140 | 834 | static const struct of_device_id virtio_mmio_match[] = { |
edfd52e6 PM |
835 | { .compatible = "virtio,mmio", }, |
836 | {}, | |
837 | }; | |
838 | MODULE_DEVICE_TABLE(of, virtio_mmio_match); | |
839 | ||
38c4ab8e GG |
840 | #ifdef CONFIG_ACPI |
841 | static const struct acpi_device_id virtio_mmio_acpi_match[] = { | |
842 | { "LNRO0005", }, | |
843 | { } | |
844 | }; | |
845 | MODULE_DEVICE_TABLE(acpi, virtio_mmio_acpi_match); | |
846 | #endif | |
847 | ||
edfd52e6 PM |
848 | static struct platform_driver virtio_mmio_driver = { |
849 | .probe = virtio_mmio_probe, | |
8590dbc7 | 850 | .remove = virtio_mmio_remove, |
edfd52e6 PM |
851 | .driver = { |
852 | .name = "virtio-mmio", | |
edfd52e6 | 853 | .of_match_table = virtio_mmio_match, |
38c4ab8e | 854 | .acpi_match_table = ACPI_PTR(virtio_mmio_acpi_match), |
ed7ac37f SG |
855 | #ifdef CONFIG_PM_SLEEP |
856 | .pm = &virtio_mmio_pm_ops, | |
857 | #endif | |
edfd52e6 PM |
858 | }, |
859 | }; | |
860 | ||
861 | static int __init virtio_mmio_init(void) | |
862 | { | |
863 | return platform_driver_register(&virtio_mmio_driver); | |
864 | } | |
865 | ||
866 | static void __exit virtio_mmio_exit(void) | |
867 | { | |
868 | platform_driver_unregister(&virtio_mmio_driver); | |
81a054ce | 869 | vm_unregister_cmdline_devices(); |
edfd52e6 PM |
870 | } |
871 | ||
872 | module_init(virtio_mmio_init); | |
873 | module_exit(virtio_mmio_exit); | |
874 | ||
875 | MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>"); | |
876 | MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices"); | |
877 | MODULE_LICENSE("GPL"); |