Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Virtio memory mapped device driver |
| 3 | * |
Pawel Moll | 1862ee2 | 2015-01-23 14:45:55 +1030 | [diff] [blame] | 4 | * Copyright 2011-2014, ARM Ltd. |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 5 | * |
| 6 | * This module allows virtio devices to be used over a virtual, memory mapped |
| 7 | * platform device. |
| 8 | * |
Pawel Moll | 81a054c | 2012-05-09 18:30:16 +0100 | [diff] [blame] | 9 | * The guest device(s) may be instantiated in one of three equivalent ways: |
| 10 | * |
| 11 | * 1. Static platform device in board's code, eg.: |
| 12 | * |
| 13 | * static struct platform_device v2m_virtio_device = { |
| 14 | * .name = "virtio-mmio", |
| 15 | * .id = -1, |
| 16 | * .num_resources = 2, |
| 17 | * .resource = (struct resource []) { |
| 18 | * { |
| 19 | * .start = 0x1001e000, |
| 20 | * .end = 0x1001e0ff, |
| 21 | * .flags = IORESOURCE_MEM, |
| 22 | * }, { |
| 23 | * .start = 42 + 32, |
| 24 | * .end = 42 + 32, |
| 25 | * .flags = IORESOURCE_IRQ, |
| 26 | * }, |
| 27 | * } |
| 28 | * }; |
| 29 | * |
| 30 | * 2. Device Tree node, eg.: |
| 31 | * |
| 32 | * virtio_block@1e000 { |
| 33 | * compatible = "virtio,mmio"; |
| 34 | * reg = <0x1e000 0x100>; |
| 35 | * interrupts = <42>; |
| 36 | * } |
| 37 | * |
| 38 | * 3. Kernel module (or command line) parameter. Can be used more than once - |
| 39 | * one device will be created for each one. Syntax: |
| 40 | * |
| 41 | * [virtio_mmio.]device=<size>@<baseaddr>:<irq>[:<id>] |
| 42 | * where: |
| 43 | * <size> := size (can use standard suffixes like K, M or G) |
| 44 | * <baseaddr> := physical base address |
| 45 | * <irq> := interrupt number (as passed to request_irq()) |
| 46 | * <id> := (optional) platform device id |
| 47 | * eg.: |
| 48 | * virtio_mmio.device=0x100@0x100b0000:48 \ |
| 49 | * virtio_mmio.device=1K@0x1001e000:74 |
| 50 | * |
| 51 | * |
| 52 | * |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 53 | * Based on Virtio PCI driver by Anthony Liguori, copyright IBM Corp. 2007 |
| 54 | * |
| 55 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 56 | * See the COPYING file in the top-level directory. |
| 57 | */ |
| 58 | |
Pawel Moll | 81a054c | 2012-05-09 18:30:16 +0100 | [diff] [blame] | 59 | #define pr_fmt(fmt) "virtio-mmio: " fmt |
| 60 | |
Graeme Gregory | 38c4ab8 | 2015-07-28 10:44:02 +0100 | [diff] [blame] | 61 | #include <linux/acpi.h> |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 62 | #include <linux/highmem.h> |
| 63 | #include <linux/interrupt.h> |
| 64 | #include <linux/io.h> |
| 65 | #include <linux/list.h> |
| 66 | #include <linux/module.h> |
| 67 | #include <linux/platform_device.h> |
| 68 | #include <linux/slab.h> |
| 69 | #include <linux/spinlock.h> |
| 70 | #include <linux/virtio.h> |
| 71 | #include <linux/virtio_config.h> |
| 72 | #include <linux/virtio_mmio.h> |
| 73 | #include <linux/virtio_ring.h> |
| 74 | |
| 75 | |
| 76 | |
| 77 | /* The alignment to use between consumer and producer parts of vring. |
| 78 | * Currently hardcoded to the page size. */ |
| 79 | #define VIRTIO_MMIO_VRING_ALIGN PAGE_SIZE |
| 80 | |
| 81 | |
| 82 | |
| 83 | #define to_virtio_mmio_device(_plat_dev) \ |
| 84 | container_of(_plat_dev, struct virtio_mmio_device, vdev) |
| 85 | |
| 86 | struct virtio_mmio_device { |
| 87 | struct virtio_device vdev; |
| 88 | struct platform_device *pdev; |
| 89 | |
| 90 | void __iomem *base; |
| 91 | unsigned long version; |
| 92 | |
| 93 | /* a list of queues so we can dispatch IRQs */ |
| 94 | spinlock_t lock; |
| 95 | struct list_head virtqueues; |
| 96 | }; |
| 97 | |
| 98 | struct virtio_mmio_vq_info { |
| 99 | /* the actual virtqueue */ |
| 100 | struct virtqueue *vq; |
| 101 | |
| 102 | /* the number of entries in the queue */ |
| 103 | unsigned int num; |
| 104 | |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 105 | /* the virtual address of the ring queue */ |
| 106 | void *queue; |
| 107 | |
| 108 | /* the list node for the virtqueues list */ |
| 109 | struct list_head node; |
| 110 | }; |
| 111 | |
| 112 | |
| 113 | |
| 114 | /* Configuration interface */ |
| 115 | |
Michael S. Tsirkin | d025477 | 2014-10-07 16:39:43 +0200 | [diff] [blame] | 116 | static u64 vm_get_features(struct virtio_device *vdev) |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 117 | { |
| 118 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); |
Pawel Moll | 1862ee2 | 2015-01-23 14:45:55 +1030 | [diff] [blame] | 119 | u64 features; |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 120 | |
Pawel Moll | 1862ee2 | 2015-01-23 14:45:55 +1030 | [diff] [blame] | 121 | writel(1, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL); |
| 122 | features = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES); |
| 123 | features <<= 32; |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 124 | |
Pawel Moll | 1862ee2 | 2015-01-23 14:45:55 +1030 | [diff] [blame] | 125 | writel(0, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL); |
| 126 | features |= readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES); |
| 127 | |
| 128 | return features; |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Michael S. Tsirkin | 5c609a5 | 2014-12-04 20:20:27 +0200 | [diff] [blame] | 131 | static int vm_finalize_features(struct virtio_device *vdev) |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 132 | { |
| 133 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 134 | |
| 135 | /* Give virtio_ring a chance to accept features. */ |
| 136 | vring_transport_features(vdev); |
| 137 | |
Pawel Moll | 1862ee2 | 2015-01-23 14:45:55 +1030 | [diff] [blame] | 138 | /* Make sure there is are no mixed devices */ |
| 139 | if (vm_dev->version == 2 && |
| 140 | !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) { |
| 141 | dev_err(&vdev->dev, "New virtio-mmio devices (version 2) must provide VIRTIO_F_VERSION_1 feature!\n"); |
| 142 | return -EINVAL; |
| 143 | } |
Michael S. Tsirkin | 93d389f | 2014-11-27 13:45:58 +0200 | [diff] [blame] | 144 | |
Pawel Moll | 1862ee2 | 2015-01-23 14:45:55 +1030 | [diff] [blame] | 145 | writel(1, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL); |
| 146 | writel((u32)(vdev->features >> 32), |
| 147 | vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES); |
| 148 | |
| 149 | writel(0, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL); |
| 150 | writel((u32)vdev->features, |
| 151 | vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES); |
Michael S. Tsirkin | 5c609a5 | 2014-12-04 20:20:27 +0200 | [diff] [blame] | 152 | |
| 153 | return 0; |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static void vm_get(struct virtio_device *vdev, unsigned offset, |
| 157 | void *buf, unsigned len) |
| 158 | { |
| 159 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); |
Michael S. Tsirkin | 704a0b5 | 2015-03-17 12:11:30 +1030 | [diff] [blame] | 160 | void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG; |
| 161 | u8 b; |
| 162 | __le16 w; |
| 163 | __le32 l; |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 164 | |
Michael S. Tsirkin | 704a0b5 | 2015-03-17 12:11:30 +1030 | [diff] [blame] | 165 | if (vm_dev->version == 1) { |
| 166 | u8 *ptr = buf; |
| 167 | int i; |
| 168 | |
| 169 | for (i = 0; i < len; i++) |
| 170 | ptr[i] = readb(base + offset + i); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | switch (len) { |
| 175 | case 1: |
| 176 | b = readb(base + offset); |
| 177 | memcpy(buf, &b, sizeof b); |
| 178 | break; |
| 179 | case 2: |
| 180 | w = cpu_to_le16(readw(base + offset)); |
| 181 | memcpy(buf, &w, sizeof w); |
| 182 | break; |
| 183 | case 4: |
| 184 | l = cpu_to_le32(readl(base + offset)); |
| 185 | memcpy(buf, &l, sizeof l); |
| 186 | break; |
| 187 | case 8: |
| 188 | l = cpu_to_le32(readl(base + offset)); |
| 189 | memcpy(buf, &l, sizeof l); |
| 190 | l = cpu_to_le32(ioread32(base + offset + sizeof l)); |
| 191 | memcpy(buf + sizeof l, &l, sizeof l); |
| 192 | break; |
| 193 | default: |
| 194 | BUG(); |
| 195 | } |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void vm_set(struct virtio_device *vdev, unsigned offset, |
| 199 | const void *buf, unsigned len) |
| 200 | { |
| 201 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); |
Michael S. Tsirkin | 704a0b5 | 2015-03-17 12:11:30 +1030 | [diff] [blame] | 202 | void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG; |
| 203 | u8 b; |
| 204 | __le16 w; |
| 205 | __le32 l; |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 206 | |
Michael S. Tsirkin | 704a0b5 | 2015-03-17 12:11:30 +1030 | [diff] [blame] | 207 | if (vm_dev->version == 1) { |
| 208 | const u8 *ptr = buf; |
| 209 | int i; |
| 210 | |
| 211 | for (i = 0; i < len; i++) |
| 212 | writeb(ptr[i], base + offset + i); |
| 213 | |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | switch (len) { |
| 218 | case 1: |
| 219 | memcpy(&b, buf, sizeof b); |
| 220 | writeb(b, base + offset); |
| 221 | break; |
| 222 | case 2: |
| 223 | memcpy(&w, buf, sizeof w); |
| 224 | writew(le16_to_cpu(w), base + offset); |
| 225 | break; |
| 226 | case 4: |
| 227 | memcpy(&l, buf, sizeof l); |
| 228 | writel(le32_to_cpu(l), base + offset); |
| 229 | break; |
| 230 | case 8: |
| 231 | memcpy(&l, buf, sizeof l); |
| 232 | writel(le32_to_cpu(l), base + offset); |
| 233 | memcpy(&l, buf + sizeof l, sizeof l); |
| 234 | writel(le32_to_cpu(l), base + offset + sizeof l); |
| 235 | break; |
| 236 | default: |
| 237 | BUG(); |
| 238 | } |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Michael S. Tsirkin | 87e7bf1 | 2015-03-12 12:56:43 +1030 | [diff] [blame] | 241 | static u32 vm_generation(struct virtio_device *vdev) |
| 242 | { |
| 243 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); |
| 244 | |
| 245 | if (vm_dev->version == 1) |
| 246 | return 0; |
| 247 | else |
| 248 | return readl(vm_dev->base + VIRTIO_MMIO_CONFIG_GENERATION); |
| 249 | } |
| 250 | |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 251 | static u8 vm_get_status(struct virtio_device *vdev) |
| 252 | { |
| 253 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); |
| 254 | |
| 255 | return readl(vm_dev->base + VIRTIO_MMIO_STATUS) & 0xff; |
| 256 | } |
| 257 | |
| 258 | static void vm_set_status(struct virtio_device *vdev, u8 status) |
| 259 | { |
| 260 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); |
| 261 | |
| 262 | /* We should never be setting status to 0. */ |
| 263 | BUG_ON(status == 0); |
| 264 | |
| 265 | writel(status, vm_dev->base + VIRTIO_MMIO_STATUS); |
| 266 | } |
| 267 | |
| 268 | static void vm_reset(struct virtio_device *vdev) |
| 269 | { |
| 270 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); |
| 271 | |
| 272 | /* 0 status means a reset. */ |
| 273 | writel(0, vm_dev->base + VIRTIO_MMIO_STATUS); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | |
| 278 | /* Transport interface */ |
| 279 | |
| 280 | /* the notify function used when creating a virt queue */ |
Heinz Graalfs | 46f9c2b | 2013-10-29 09:38:50 +1030 | [diff] [blame] | 281 | static bool vm_notify(struct virtqueue *vq) |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 282 | { |
| 283 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev); |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 284 | |
| 285 | /* We write the queue's selector into the notification register to |
| 286 | * signal the other end */ |
Rusty Russell | 06ca287 | 2012-10-16 23:56:14 +1030 | [diff] [blame] | 287 | writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY); |
Heinz Graalfs | 46f9c2b | 2013-10-29 09:38:50 +1030 | [diff] [blame] | 288 | return true; |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* Notify all virtqueues on an interrupt. */ |
| 292 | static irqreturn_t vm_interrupt(int irq, void *opaque) |
| 293 | { |
| 294 | struct virtio_mmio_device *vm_dev = opaque; |
| 295 | struct virtio_mmio_vq_info *info; |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 296 | unsigned long status; |
| 297 | unsigned long flags; |
| 298 | irqreturn_t ret = IRQ_NONE; |
| 299 | |
| 300 | /* Read and acknowledge interrupts */ |
| 301 | status = readl(vm_dev->base + VIRTIO_MMIO_INTERRUPT_STATUS); |
| 302 | writel(status, vm_dev->base + VIRTIO_MMIO_INTERRUPT_ACK); |
| 303 | |
Michael S. Tsirkin | 016c98c | 2014-10-14 10:40:34 +1030 | [diff] [blame] | 304 | if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)) { |
| 305 | virtio_config_changed(&vm_dev->vdev); |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 306 | ret = IRQ_HANDLED; |
| 307 | } |
| 308 | |
| 309 | if (likely(status & VIRTIO_MMIO_INT_VRING)) { |
| 310 | spin_lock_irqsave(&vm_dev->lock, flags); |
| 311 | list_for_each_entry(info, &vm_dev->virtqueues, node) |
| 312 | ret |= vring_interrupt(irq, info->vq); |
| 313 | spin_unlock_irqrestore(&vm_dev->lock, flags); |
| 314 | } |
| 315 | |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | |
| 320 | |
| 321 | static void vm_del_vq(struct virtqueue *vq) |
| 322 | { |
| 323 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev); |
| 324 | struct virtio_mmio_vq_info *info = vq->priv; |
| 325 | unsigned long flags, size; |
Rusty Russell | 06ca287 | 2012-10-16 23:56:14 +1030 | [diff] [blame] | 326 | unsigned int index = vq->index; |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 327 | |
| 328 | spin_lock_irqsave(&vm_dev->lock, flags); |
| 329 | list_del(&info->node); |
| 330 | spin_unlock_irqrestore(&vm_dev->lock, flags); |
| 331 | |
| 332 | vring_del_virtqueue(vq); |
| 333 | |
| 334 | /* Select and deactivate the queue */ |
Jason Wang | 17bb6d4 | 2012-08-28 13:54:13 +0200 | [diff] [blame] | 335 | writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL); |
Pawel Moll | 1862ee2 | 2015-01-23 14:45:55 +1030 | [diff] [blame] | 336 | if (vm_dev->version == 1) { |
| 337 | writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN); |
| 338 | } else { |
| 339 | writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY); |
| 340 | WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY)); |
| 341 | } |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 342 | |
| 343 | size = PAGE_ALIGN(vring_size(info->num, VIRTIO_MMIO_VRING_ALIGN)); |
| 344 | free_pages_exact(info->queue, size); |
| 345 | kfree(info); |
| 346 | } |
| 347 | |
| 348 | static void vm_del_vqs(struct virtio_device *vdev) |
| 349 | { |
| 350 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); |
| 351 | struct virtqueue *vq, *n; |
| 352 | |
| 353 | list_for_each_entry_safe(vq, n, &vdev->vqs, list) |
| 354 | vm_del_vq(vq); |
| 355 | |
| 356 | free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev); |
| 357 | } |
| 358 | |
| 359 | |
| 360 | |
| 361 | static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned index, |
| 362 | void (*callback)(struct virtqueue *vq), |
| 363 | const char *name) |
| 364 | { |
| 365 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); |
| 366 | struct virtio_mmio_vq_info *info; |
| 367 | struct virtqueue *vq; |
| 368 | unsigned long flags, size; |
| 369 | int err; |
| 370 | |
Michael S. Tsirkin | 6457f126 | 2012-09-05 21:47:45 +0300 | [diff] [blame] | 371 | if (!name) |
| 372 | return NULL; |
| 373 | |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 374 | /* Select the queue we're interested in */ |
| 375 | writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL); |
| 376 | |
| 377 | /* Queue shouldn't already be set up. */ |
Pawel Moll | 1862ee2 | 2015-01-23 14:45:55 +1030 | [diff] [blame] | 378 | if (readl(vm_dev->base + (vm_dev->version == 1 ? |
| 379 | VIRTIO_MMIO_QUEUE_PFN : VIRTIO_MMIO_QUEUE_READY))) { |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 380 | err = -ENOENT; |
| 381 | goto error_available; |
| 382 | } |
| 383 | |
| 384 | /* Allocate and fill out our active queue description */ |
| 385 | info = kmalloc(sizeof(*info), GFP_KERNEL); |
| 386 | if (!info) { |
| 387 | err = -ENOMEM; |
| 388 | goto error_kmalloc; |
| 389 | } |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 390 | |
| 391 | /* Allocate pages for the queue - start with a queue as big as |
| 392 | * possible (limited by maximum size allowed by device), drop down |
| 393 | * to a minimal size, just big enough to fit descriptor table |
| 394 | * and two rings (which makes it "alignment_size * 2") |
| 395 | */ |
| 396 | info->num = readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX); |
Brian Foley | d78b519 | 2012-09-24 14:33:42 +0100 | [diff] [blame] | 397 | |
| 398 | /* If the device reports a 0 entry queue, we won't be able to |
| 399 | * use it to perform I/O, and vring_new_virtqueue() can't create |
| 400 | * empty queues anyway, so don't bother to set up the device. |
| 401 | */ |
| 402 | if (info->num == 0) { |
| 403 | err = -ENOENT; |
| 404 | goto error_alloc_pages; |
| 405 | } |
| 406 | |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 407 | while (1) { |
| 408 | size = PAGE_ALIGN(vring_size(info->num, |
| 409 | VIRTIO_MMIO_VRING_ALIGN)); |
Brian Foley | 3850d29 | 2012-09-24 14:33:41 +0100 | [diff] [blame] | 410 | /* Did the last iter shrink the queue below minimum size? */ |
| 411 | if (size < VIRTIO_MMIO_VRING_ALIGN * 2) { |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 412 | err = -ENOMEM; |
| 413 | goto error_alloc_pages; |
| 414 | } |
| 415 | |
| 416 | info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); |
| 417 | if (info->queue) |
| 418 | break; |
| 419 | |
| 420 | info->num /= 2; |
| 421 | } |
| 422 | |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 423 | /* Create the vring */ |
Jason Wang | 17bb6d4 | 2012-08-28 13:54:13 +0200 | [diff] [blame] | 424 | vq = vring_new_virtqueue(index, info->num, VIRTIO_MMIO_VRING_ALIGN, vdev, |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 425 | true, info->queue, vm_notify, callback, name); |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 426 | if (!vq) { |
| 427 | err = -ENOMEM; |
| 428 | goto error_new_virtqueue; |
| 429 | } |
| 430 | |
Pawel Moll | 1862ee2 | 2015-01-23 14:45:55 +1030 | [diff] [blame] | 431 | /* Activate the queue */ |
| 432 | writel(info->num, vm_dev->base + VIRTIO_MMIO_QUEUE_NUM); |
| 433 | if (vm_dev->version == 1) { |
| 434 | writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN); |
| 435 | writel(virt_to_phys(info->queue) >> PAGE_SHIFT, |
| 436 | vm_dev->base + VIRTIO_MMIO_QUEUE_PFN); |
| 437 | } else { |
| 438 | u64 addr; |
| 439 | |
| 440 | addr = virt_to_phys(info->queue); |
| 441 | writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_LOW); |
| 442 | writel((u32)(addr >> 32), |
| 443 | vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_HIGH); |
| 444 | |
| 445 | addr = virt_to_phys(virtqueue_get_avail(vq)); |
| 446 | writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_LOW); |
| 447 | writel((u32)(addr >> 32), |
| 448 | vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_HIGH); |
| 449 | |
| 450 | addr = virt_to_phys(virtqueue_get_used(vq)); |
| 451 | writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_USED_LOW); |
| 452 | writel((u32)(addr >> 32), |
| 453 | vm_dev->base + VIRTIO_MMIO_QUEUE_USED_HIGH); |
| 454 | |
| 455 | writel(1, vm_dev->base + VIRTIO_MMIO_QUEUE_READY); |
| 456 | } |
| 457 | |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 458 | vq->priv = info; |
| 459 | info->vq = vq; |
| 460 | |
| 461 | spin_lock_irqsave(&vm_dev->lock, flags); |
| 462 | list_add(&info->node, &vm_dev->virtqueues); |
| 463 | spin_unlock_irqrestore(&vm_dev->lock, flags); |
| 464 | |
| 465 | return vq; |
| 466 | |
| 467 | error_new_virtqueue: |
Pawel Moll | 1862ee2 | 2015-01-23 14:45:55 +1030 | [diff] [blame] | 468 | if (vm_dev->version == 1) { |
| 469 | writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN); |
| 470 | } else { |
| 471 | writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY); |
| 472 | WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY)); |
| 473 | } |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 474 | free_pages_exact(info->queue, size); |
| 475 | error_alloc_pages: |
| 476 | kfree(info); |
| 477 | error_kmalloc: |
| 478 | error_available: |
| 479 | return ERR_PTR(err); |
| 480 | } |
| 481 | |
| 482 | static int vm_find_vqs(struct virtio_device *vdev, unsigned nvqs, |
| 483 | struct virtqueue *vqs[], |
| 484 | vq_callback_t *callbacks[], |
Stefan Hajnoczi | f7ad26f | 2015-12-17 16:53:43 +0800 | [diff] [blame] | 485 | const char * const names[]) |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 486 | { |
| 487 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); |
| 488 | unsigned int irq = platform_get_irq(vm_dev->pdev, 0); |
| 489 | int i, err; |
| 490 | |
| 491 | err = request_irq(irq, vm_interrupt, IRQF_SHARED, |
| 492 | dev_name(&vdev->dev), vm_dev); |
| 493 | if (err) |
| 494 | return err; |
| 495 | |
| 496 | for (i = 0; i < nvqs; ++i) { |
| 497 | vqs[i] = vm_setup_vq(vdev, i, callbacks[i], names[i]); |
| 498 | if (IS_ERR(vqs[i])) { |
| 499 | vm_del_vqs(vdev); |
| 500 | return PTR_ERR(vqs[i]); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
Rick Jones | 6684604 | 2011-11-14 14:17:08 +0000 | [diff] [blame] | 507 | static const char *vm_bus_name(struct virtio_device *vdev) |
| 508 | { |
| 509 | struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 510 | |
Rick Jones | 6684604 | 2011-11-14 14:17:08 +0000 | [diff] [blame] | 511 | return vm_dev->pdev->name; |
| 512 | } |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 513 | |
Stephen Hemminger | 9350393 | 2013-02-10 15:57:38 +1030 | [diff] [blame] | 514 | static const struct virtio_config_ops virtio_mmio_config_ops = { |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 515 | .get = vm_get, |
| 516 | .set = vm_set, |
Michael S. Tsirkin | 87e7bf1 | 2015-03-12 12:56:43 +1030 | [diff] [blame] | 517 | .generation = vm_generation, |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 518 | .get_status = vm_get_status, |
| 519 | .set_status = vm_set_status, |
| 520 | .reset = vm_reset, |
| 521 | .find_vqs = vm_find_vqs, |
| 522 | .del_vqs = vm_del_vqs, |
| 523 | .get_features = vm_get_features, |
| 524 | .finalize_features = vm_finalize_features, |
Rick Jones | 6684604 | 2011-11-14 14:17:08 +0000 | [diff] [blame] | 525 | .bus_name = vm_bus_name, |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 526 | }; |
| 527 | |
| 528 | |
| 529 | |
| 530 | /* Platform device */ |
| 531 | |
Greg Kroah-Hartman | 8590dbc | 2012-12-21 13:05:30 -0800 | [diff] [blame] | 532 | static int virtio_mmio_probe(struct platform_device *pdev) |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 533 | { |
| 534 | struct virtio_mmio_device *vm_dev; |
| 535 | struct resource *mem; |
| 536 | unsigned long magic; |
| 537 | |
| 538 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 539 | if (!mem) |
| 540 | return -EINVAL; |
| 541 | |
| 542 | if (!devm_request_mem_region(&pdev->dev, mem->start, |
| 543 | resource_size(mem), pdev->name)) |
| 544 | return -EBUSY; |
| 545 | |
| 546 | vm_dev = devm_kzalloc(&pdev->dev, sizeof(*vm_dev), GFP_KERNEL); |
| 547 | if (!vm_dev) |
| 548 | return -ENOMEM; |
| 549 | |
| 550 | vm_dev->vdev.dev.parent = &pdev->dev; |
| 551 | vm_dev->vdev.config = &virtio_mmio_config_ops; |
| 552 | vm_dev->pdev = pdev; |
| 553 | INIT_LIST_HEAD(&vm_dev->virtqueues); |
| 554 | spin_lock_init(&vm_dev->lock); |
| 555 | |
| 556 | vm_dev->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem)); |
| 557 | if (vm_dev->base == NULL) |
| 558 | return -EFAULT; |
| 559 | |
| 560 | /* Check magic value */ |
| 561 | magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE); |
Marc Zyngier | 4ae8537 | 2013-11-05 21:21:28 +1030 | [diff] [blame] | 562 | if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) { |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 563 | dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic); |
| 564 | return -ENODEV; |
| 565 | } |
| 566 | |
| 567 | /* Check device version */ |
| 568 | vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION); |
Pawel Moll | 1862ee2 | 2015-01-23 14:45:55 +1030 | [diff] [blame] | 569 | if (vm_dev->version < 1 || vm_dev->version > 2) { |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 570 | dev_err(&pdev->dev, "Version %ld not supported!\n", |
| 571 | vm_dev->version); |
| 572 | return -ENXIO; |
| 573 | } |
| 574 | |
| 575 | vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID); |
Pawel Moll | 1862ee2 | 2015-01-23 14:45:55 +1030 | [diff] [blame] | 576 | if (vm_dev->vdev.id.device == 0) { |
| 577 | /* |
| 578 | * virtio-mmio device with an ID 0 is a (dummy) placeholder |
| 579 | * with no function. End probing now with no error reported. |
| 580 | */ |
| 581 | return -ENODEV; |
| 582 | } |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 583 | vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID); |
| 584 | |
Pawel Moll | 1862ee2 | 2015-01-23 14:45:55 +1030 | [diff] [blame] | 585 | if (vm_dev->version == 1) |
| 586 | writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE); |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 587 | |
| 588 | platform_set_drvdata(pdev, vm_dev); |
| 589 | |
| 590 | return register_virtio_device(&vm_dev->vdev); |
| 591 | } |
| 592 | |
Greg Kroah-Hartman | 8590dbc | 2012-12-21 13:05:30 -0800 | [diff] [blame] | 593 | static int virtio_mmio_remove(struct platform_device *pdev) |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 594 | { |
| 595 | struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev); |
| 596 | |
| 597 | unregister_virtio_device(&vm_dev->vdev); |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | |
| 603 | |
Pawel Moll | 81a054c | 2012-05-09 18:30:16 +0100 | [diff] [blame] | 604 | /* Devices list parameter */ |
| 605 | |
| 606 | #if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES) |
| 607 | |
| 608 | static struct device vm_cmdline_parent = { |
| 609 | .init_name = "virtio-mmio-cmdline", |
| 610 | }; |
| 611 | |
| 612 | static int vm_cmdline_parent_registered; |
| 613 | static int vm_cmdline_id; |
| 614 | |
| 615 | static int vm_cmdline_set(const char *device, |
| 616 | const struct kernel_param *kp) |
| 617 | { |
| 618 | int err; |
| 619 | struct resource resources[2] = {}; |
| 620 | char *str; |
Pawel Moll | 40f9938 | 2012-11-22 12:30:24 +1030 | [diff] [blame] | 621 | long long int base, size; |
| 622 | unsigned int irq; |
Pawel Moll | 81a054c | 2012-05-09 18:30:16 +0100 | [diff] [blame] | 623 | int processed, consumed = 0; |
| 624 | struct platform_device *pdev; |
| 625 | |
Pawel Moll | 40f9938 | 2012-11-22 12:30:24 +1030 | [diff] [blame] | 626 | /* Consume "size" part of the command line parameter */ |
| 627 | size = memparse(device, &str); |
Pawel Moll | 81a054c | 2012-05-09 18:30:16 +0100 | [diff] [blame] | 628 | |
Pawel Moll | 40f9938 | 2012-11-22 12:30:24 +1030 | [diff] [blame] | 629 | /* Get "@<base>:<irq>[:<id>]" chunks */ |
Pawel Moll | 81a054c | 2012-05-09 18:30:16 +0100 | [diff] [blame] | 630 | processed = sscanf(str, "@%lli:%u%n:%d%n", |
Pawel Moll | 40f9938 | 2012-11-22 12:30:24 +1030 | [diff] [blame] | 631 | &base, &irq, &consumed, |
Pawel Moll | 81a054c | 2012-05-09 18:30:16 +0100 | [diff] [blame] | 632 | &vm_cmdline_id, &consumed); |
| 633 | |
Pawel Moll | 40f9938 | 2012-11-22 12:30:24 +1030 | [diff] [blame] | 634 | /* |
| 635 | * sscanf() must processes at least 2 chunks; also there |
| 636 | * must be no extra characters after the last chunk, so |
| 637 | * str[consumed] must be '\0' |
| 638 | */ |
| 639 | if (processed < 2 || str[consumed]) |
Pawel Moll | 81a054c | 2012-05-09 18:30:16 +0100 | [diff] [blame] | 640 | return -EINVAL; |
| 641 | |
Pawel Moll | 40f9938 | 2012-11-22 12:30:24 +1030 | [diff] [blame] | 642 | resources[0].flags = IORESOURCE_MEM; |
Pawel Moll | 81a054c | 2012-05-09 18:30:16 +0100 | [diff] [blame] | 643 | resources[0].start = base; |
Pawel Moll | 40f9938 | 2012-11-22 12:30:24 +1030 | [diff] [blame] | 644 | resources[0].end = base + size - 1; |
| 645 | |
| 646 | resources[1].flags = IORESOURCE_IRQ; |
| 647 | resources[1].start = resources[1].end = irq; |
Pawel Moll | 81a054c | 2012-05-09 18:30:16 +0100 | [diff] [blame] | 648 | |
| 649 | if (!vm_cmdline_parent_registered) { |
| 650 | err = device_register(&vm_cmdline_parent); |
| 651 | if (err) { |
| 652 | pr_err("Failed to register parent device!\n"); |
| 653 | return err; |
| 654 | } |
| 655 | vm_cmdline_parent_registered = 1; |
| 656 | } |
| 657 | |
| 658 | pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n", |
| 659 | vm_cmdline_id, |
| 660 | (unsigned long long)resources[0].start, |
| 661 | (unsigned long long)resources[0].end, |
| 662 | (int)resources[1].start); |
| 663 | |
| 664 | pdev = platform_device_register_resndata(&vm_cmdline_parent, |
| 665 | "virtio-mmio", vm_cmdline_id++, |
| 666 | resources, ARRAY_SIZE(resources), NULL, 0); |
| 667 | if (IS_ERR(pdev)) |
| 668 | return PTR_ERR(pdev); |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static int vm_cmdline_get_device(struct device *dev, void *data) |
| 674 | { |
| 675 | char *buffer = data; |
| 676 | unsigned int len = strlen(buffer); |
| 677 | struct platform_device *pdev = to_platform_device(dev); |
| 678 | |
| 679 | snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n", |
| 680 | pdev->resource[0].end - pdev->resource[0].start + 1ULL, |
| 681 | (unsigned long long)pdev->resource[0].start, |
| 682 | (unsigned long long)pdev->resource[1].start, |
| 683 | pdev->id); |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | static int vm_cmdline_get(char *buffer, const struct kernel_param *kp) |
| 688 | { |
| 689 | buffer[0] = '\0'; |
| 690 | device_for_each_child(&vm_cmdline_parent, buffer, |
| 691 | vm_cmdline_get_device); |
| 692 | return strlen(buffer) + 1; |
| 693 | } |
| 694 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 695 | static const struct kernel_param_ops vm_cmdline_param_ops = { |
Pawel Moll | 81a054c | 2012-05-09 18:30:16 +0100 | [diff] [blame] | 696 | .set = vm_cmdline_set, |
| 697 | .get = vm_cmdline_get, |
| 698 | }; |
| 699 | |
| 700 | device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR); |
| 701 | |
| 702 | static int vm_unregister_cmdline_device(struct device *dev, |
| 703 | void *data) |
| 704 | { |
| 705 | platform_device_unregister(to_platform_device(dev)); |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | static void vm_unregister_cmdline_devices(void) |
| 711 | { |
| 712 | if (vm_cmdline_parent_registered) { |
| 713 | device_for_each_child(&vm_cmdline_parent, NULL, |
| 714 | vm_unregister_cmdline_device); |
| 715 | device_unregister(&vm_cmdline_parent); |
| 716 | vm_cmdline_parent_registered = 0; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | #else |
| 721 | |
| 722 | static void vm_unregister_cmdline_devices(void) |
| 723 | { |
| 724 | } |
| 725 | |
| 726 | #endif |
| 727 | |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 728 | /* Platform driver */ |
| 729 | |
| 730 | static struct of_device_id virtio_mmio_match[] = { |
| 731 | { .compatible = "virtio,mmio", }, |
| 732 | {}, |
| 733 | }; |
| 734 | MODULE_DEVICE_TABLE(of, virtio_mmio_match); |
| 735 | |
Graeme Gregory | 38c4ab8 | 2015-07-28 10:44:02 +0100 | [diff] [blame] | 736 | #ifdef CONFIG_ACPI |
| 737 | static const struct acpi_device_id virtio_mmio_acpi_match[] = { |
| 738 | { "LNRO0005", }, |
| 739 | { } |
| 740 | }; |
| 741 | MODULE_DEVICE_TABLE(acpi, virtio_mmio_acpi_match); |
| 742 | #endif |
| 743 | |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 744 | static struct platform_driver virtio_mmio_driver = { |
| 745 | .probe = virtio_mmio_probe, |
Greg Kroah-Hartman | 8590dbc | 2012-12-21 13:05:30 -0800 | [diff] [blame] | 746 | .remove = virtio_mmio_remove, |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 747 | .driver = { |
| 748 | .name = "virtio-mmio", |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 749 | .of_match_table = virtio_mmio_match, |
Graeme Gregory | 38c4ab8 | 2015-07-28 10:44:02 +0100 | [diff] [blame] | 750 | .acpi_match_table = ACPI_PTR(virtio_mmio_acpi_match), |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 751 | }, |
| 752 | }; |
| 753 | |
| 754 | static int __init virtio_mmio_init(void) |
| 755 | { |
| 756 | return platform_driver_register(&virtio_mmio_driver); |
| 757 | } |
| 758 | |
| 759 | static void __exit virtio_mmio_exit(void) |
| 760 | { |
| 761 | platform_driver_unregister(&virtio_mmio_driver); |
Pawel Moll | 81a054c | 2012-05-09 18:30:16 +0100 | [diff] [blame] | 762 | vm_unregister_cmdline_devices(); |
Pawel Moll | edfd52e | 2011-10-24 14:07:03 +0100 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | module_init(virtio_mmio_init); |
| 766 | module_exit(virtio_mmio_exit); |
| 767 | |
| 768 | MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>"); |
| 769 | MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices"); |
| 770 | MODULE_LICENSE("GPL"); |