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