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