1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Virtio memory mapped device driver 4 * 5 * Copyright 2011-2014, ARM Ltd. 6 * 7 * This module allows virtio devices to be used over a virtual, memory mapped 8 * platform device. 9 * 10 * The guest device(s) may be instantiated in one of three equivalent ways: 11 * 12 * 1. Static platform device in board's code, eg.: 13 * 14 * static struct platform_device v2m_virtio_device = { 15 * .name = "virtio-mmio", 16 * .id = -1, 17 * .num_resources = 2, 18 * .resource = (struct resource []) { 19 * { 20 * .start = 0x1001e000, 21 * .end = 0x1001e0ff, 22 * .flags = IORESOURCE_MEM, 23 * }, { 24 * .start = 42 + 32, 25 * .end = 42 + 32, 26 * .flags = IORESOURCE_IRQ, 27 * }, 28 * } 29 * }; 30 * 31 * 2. Device Tree node, eg.: 32 * 33 * virtio_block@1e000 { 34 * compatible = "virtio,mmio"; 35 * reg = <0x1e000 0x100>; 36 * interrupts = <42>; 37 * } 38 * 39 * 3. Kernel module (or command line) parameter. Can be used more than once - 40 * one device will be created for each one. Syntax: 41 * 42 * [virtio_mmio.]device=<size>@<baseaddr>:<irq>[:<id>] 43 * where: 44 * <size> := size (can use standard suffixes like K, M or G) 45 * <baseaddr> := physical base address 46 * <irq> := interrupt number (as passed to request_irq()) 47 * <id> := (optional) platform device id 48 * eg.: 49 * virtio_mmio.device=0x100@0x100b0000:48 \ 50 * virtio_mmio.device=1K@0x1001e000:74 51 * 52 * Based on Virtio PCI driver by Anthony Liguori, copyright IBM Corp. 2007 53 */ 54 55 #define pr_fmt(fmt) "virtio-mmio: " fmt 56 57 #include <linux/acpi.h> 58 #include <linux/dma-mapping.h> 59 #include <linux/highmem.h> 60 #include <linux/interrupt.h> 61 #include <linux/io.h> 62 #include <linux/list.h> 63 #include <linux/module.h> 64 #include <linux/of.h> 65 #include <linux/platform_device.h> 66 #include <linux/pm.h> 67 #include <linux/slab.h> 68 #include <linux/virtio.h> 69 #include <linux/virtio_config.h> 70 #include <uapi/linux/virtio_mmio.h> 71 #include <linux/virtio_ring.h> 72 73 74 75 /* The alignment to use between consumer and producer parts of vring. 76 * Currently hardcoded to the page size. */ 77 #define VIRTIO_MMIO_VRING_ALIGN PAGE_SIZE 78 79 80 81 #define to_virtio_mmio_device(_plat_dev) \ 82 container_of(_plat_dev, struct virtio_mmio_device, vdev) 83 84 struct virtio_mmio_device { 85 struct virtio_device vdev; 86 struct platform_device *pdev; 87 88 void __iomem *base; 89 unsigned long version; 90 }; 91 92 /* Configuration interface */ 93 94 static u64 vm_get_features(struct virtio_device *vdev) 95 { 96 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); 97 u64 features; 98 99 writel(1, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL); 100 features = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES); 101 features <<= 32; 102 103 writel(0, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL); 104 features |= readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES); 105 106 return features; 107 } 108 109 static int vm_finalize_features(struct virtio_device *vdev) 110 { 111 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); 112 113 /* Give virtio_ring a chance to accept features. */ 114 vring_transport_features(vdev); 115 116 /* Make sure there are no mixed devices */ 117 if (vm_dev->version == 2 && 118 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) { 119 dev_err(&vdev->dev, "New virtio-mmio devices (version 2) must provide VIRTIO_F_VERSION_1 feature!\n"); 120 return -EINVAL; 121 } 122 123 writel(1, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL); 124 writel((u32)(vdev->features >> 32), 125 vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES); 126 127 writel(0, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL); 128 writel((u32)vdev->features, 129 vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES); 130 131 return 0; 132 } 133 134 static void vm_get(struct virtio_device *vdev, unsigned int offset, 135 void *buf, unsigned int len) 136 { 137 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); 138 void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG; 139 u8 b; 140 __le16 w; 141 __le32 l; 142 143 if (vm_dev->version == 1) { 144 u8 *ptr = buf; 145 int i; 146 147 for (i = 0; i < len; i++) 148 ptr[i] = readb(base + offset + i); 149 return; 150 } 151 152 switch (len) { 153 case 1: 154 b = readb(base + offset); 155 memcpy(buf, &b, sizeof b); 156 break; 157 case 2: 158 w = cpu_to_le16(readw(base + offset)); 159 memcpy(buf, &w, sizeof w); 160 break; 161 case 4: 162 l = cpu_to_le32(readl(base + offset)); 163 memcpy(buf, &l, sizeof l); 164 break; 165 case 8: 166 l = cpu_to_le32(readl(base + offset)); 167 memcpy(buf, &l, sizeof l); 168 l = cpu_to_le32(ioread32(base + offset + sizeof l)); 169 memcpy(buf + sizeof l, &l, sizeof l); 170 break; 171 default: 172 BUG(); 173 } 174 } 175 176 static void vm_set(struct virtio_device *vdev, unsigned int offset, 177 const void *buf, unsigned int len) 178 { 179 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); 180 void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG; 181 u8 b; 182 __le16 w; 183 __le32 l; 184 185 if (vm_dev->version == 1) { 186 const u8 *ptr = buf; 187 int i; 188 189 for (i = 0; i < len; i++) 190 writeb(ptr[i], base + offset + i); 191 192 return; 193 } 194 195 switch (len) { 196 case 1: 197 memcpy(&b, buf, sizeof b); 198 writeb(b, base + offset); 199 break; 200 case 2: 201 memcpy(&w, buf, sizeof w); 202 writew(le16_to_cpu(w), base + offset); 203 break; 204 case 4: 205 memcpy(&l, buf, sizeof l); 206 writel(le32_to_cpu(l), base + offset); 207 break; 208 case 8: 209 memcpy(&l, buf, sizeof l); 210 writel(le32_to_cpu(l), base + offset); 211 memcpy(&l, buf + sizeof l, sizeof l); 212 writel(le32_to_cpu(l), base + offset + sizeof l); 213 break; 214 default: 215 BUG(); 216 } 217 } 218 219 static u32 vm_generation(struct virtio_device *vdev) 220 { 221 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); 222 223 if (vm_dev->version == 1) 224 return 0; 225 else 226 return readl(vm_dev->base + VIRTIO_MMIO_CONFIG_GENERATION); 227 } 228 229 static u8 vm_get_status(struct virtio_device *vdev) 230 { 231 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); 232 233 return readl(vm_dev->base + VIRTIO_MMIO_STATUS) & 0xff; 234 } 235 236 static void vm_set_status(struct virtio_device *vdev, u8 status) 237 { 238 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); 239 240 /* We should never be setting status to 0. */ 241 BUG_ON(status == 0); 242 243 /* 244 * Per memory-barriers.txt, wmb() is not needed to guarantee 245 * that the cache coherent memory writes have completed 246 * before writing to the MMIO region. 247 */ 248 writel(status, vm_dev->base + VIRTIO_MMIO_STATUS); 249 } 250 251 static void vm_reset(struct virtio_device *vdev) 252 { 253 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); 254 255 /* 0 status means a reset. */ 256 writel(0, vm_dev->base + VIRTIO_MMIO_STATUS); 257 } 258 259 260 261 /* Transport interface */ 262 263 /* the notify function used when creating a virt queue */ 264 static bool vm_notify(struct virtqueue *vq) 265 { 266 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev); 267 268 /* We write the queue's selector into the notification register to 269 * signal the other end */ 270 writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY); 271 return true; 272 } 273 274 static bool vm_notify_with_data(struct virtqueue *vq) 275 { 276 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev); 277 u32 data = vring_notification_data(vq); 278 279 writel(data, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY); 280 281 return true; 282 } 283 284 /* Notify all virtqueues on an interrupt. */ 285 static irqreturn_t vm_interrupt(int irq, void *opaque) 286 { 287 struct virtio_mmio_device *vm_dev = opaque; 288 struct virtqueue *vq; 289 unsigned long status; 290 irqreturn_t ret = IRQ_NONE; 291 292 /* Read and acknowledge interrupts */ 293 status = readl(vm_dev->base + VIRTIO_MMIO_INTERRUPT_STATUS); 294 writel(status, vm_dev->base + VIRTIO_MMIO_INTERRUPT_ACK); 295 296 if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)) { 297 virtio_config_changed(&vm_dev->vdev); 298 ret = IRQ_HANDLED; 299 } 300 301 if (likely(status & VIRTIO_MMIO_INT_VRING)) { 302 virtio_device_for_each_vq(&vm_dev->vdev, vq) 303 ret |= vring_interrupt(irq, vq); 304 } 305 306 return ret; 307 } 308 309 310 311 static void vm_del_vq(struct virtqueue *vq) 312 { 313 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev); 314 unsigned int index = vq->index; 315 316 /* Select and deactivate the queue */ 317 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL); 318 if (vm_dev->version == 1) { 319 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN); 320 } else { 321 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY); 322 WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY)); 323 } 324 325 vring_del_virtqueue(vq); 326 } 327 328 static void vm_del_vqs(struct virtio_device *vdev) 329 { 330 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); 331 struct virtqueue *vq, *n; 332 333 list_for_each_entry_safe(vq, n, &vdev->vqs, list) 334 vm_del_vq(vq); 335 336 free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev); 337 } 338 339 static void vm_synchronize_cbs(struct virtio_device *vdev) 340 { 341 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); 342 343 synchronize_irq(platform_get_irq(vm_dev->pdev, 0)); 344 } 345 346 static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned int index, 347 void (*callback)(struct virtqueue *vq), 348 const char *name, bool ctx) 349 { 350 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); 351 bool (*notify)(struct virtqueue *vq); 352 struct virtqueue *vq; 353 unsigned int num; 354 int err; 355 356 if (__virtio_test_bit(vdev, VIRTIO_F_NOTIFICATION_DATA)) 357 notify = vm_notify_with_data; 358 else 359 notify = vm_notify; 360 361 if (!name) 362 return NULL; 363 364 /* Select the queue we're interested in */ 365 writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL); 366 367 /* Queue shouldn't already be set up. */ 368 if (readl(vm_dev->base + (vm_dev->version == 1 ? 369 VIRTIO_MMIO_QUEUE_PFN : VIRTIO_MMIO_QUEUE_READY))) { 370 err = -ENOENT; 371 goto error_available; 372 } 373 374 num = readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX); 375 if (num == 0) { 376 err = -ENOENT; 377 goto error_new_virtqueue; 378 } 379 380 /* Create the vring */ 381 vq = vring_create_virtqueue(index, num, VIRTIO_MMIO_VRING_ALIGN, vdev, 382 true, true, ctx, notify, callback, name); 383 if (!vq) { 384 err = -ENOMEM; 385 goto error_new_virtqueue; 386 } 387 388 vq->num_max = num; 389 390 /* Activate the queue */ 391 writel(virtqueue_get_vring_size(vq), vm_dev->base + VIRTIO_MMIO_QUEUE_NUM); 392 if (vm_dev->version == 1) { 393 u64 q_pfn = virtqueue_get_desc_addr(vq) >> PAGE_SHIFT; 394 395 /* 396 * virtio-mmio v1 uses a 32bit QUEUE PFN. If we have something 397 * that doesn't fit in 32bit, fail the setup rather than 398 * pretending to be successful. 399 */ 400 if (q_pfn >> 32) { 401 dev_err(&vdev->dev, 402 "platform bug: legacy virtio-mmio must not be used with RAM above 0x%llxGB\n", 403 0x1ULL << (32 + PAGE_SHIFT - 30)); 404 err = -E2BIG; 405 goto error_bad_pfn; 406 } 407 408 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN); 409 writel(q_pfn, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN); 410 } else { 411 u64 addr; 412 413 addr = virtqueue_get_desc_addr(vq); 414 writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_LOW); 415 writel((u32)(addr >> 32), 416 vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_HIGH); 417 418 addr = virtqueue_get_avail_addr(vq); 419 writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_LOW); 420 writel((u32)(addr >> 32), 421 vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_HIGH); 422 423 addr = virtqueue_get_used_addr(vq); 424 writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_USED_LOW); 425 writel((u32)(addr >> 32), 426 vm_dev->base + VIRTIO_MMIO_QUEUE_USED_HIGH); 427 428 writel(1, vm_dev->base + VIRTIO_MMIO_QUEUE_READY); 429 } 430 431 return vq; 432 433 error_bad_pfn: 434 vring_del_virtqueue(vq); 435 error_new_virtqueue: 436 if (vm_dev->version == 1) { 437 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN); 438 } else { 439 writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY); 440 WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY)); 441 } 442 error_available: 443 return ERR_PTR(err); 444 } 445 446 static int vm_find_vqs(struct virtio_device *vdev, unsigned int nvqs, 447 struct virtqueue *vqs[], 448 struct virtqueue_info vqs_info[], 449 struct irq_affinity *desc) 450 { 451 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); 452 int irq = platform_get_irq(vm_dev->pdev, 0); 453 int i, err, queue_idx = 0; 454 455 if (irq < 0) 456 return irq; 457 458 err = request_irq(irq, vm_interrupt, IRQF_SHARED, 459 dev_name(&vdev->dev), vm_dev); 460 if (err) 461 return err; 462 463 if (of_property_read_bool(vm_dev->pdev->dev.of_node, "wakeup-source")) 464 enable_irq_wake(irq); 465 466 for (i = 0; i < nvqs; ++i) { 467 struct virtqueue_info *vqi = &vqs_info[i]; 468 469 if (!vqi->name) { 470 vqs[i] = NULL; 471 continue; 472 } 473 474 vqs[i] = vm_setup_vq(vdev, queue_idx++, vqi->callback, 475 vqi->name, vqi->ctx); 476 if (IS_ERR(vqs[i])) { 477 vm_del_vqs(vdev); 478 return PTR_ERR(vqs[i]); 479 } 480 } 481 482 return 0; 483 } 484 485 static const char *vm_bus_name(struct virtio_device *vdev) 486 { 487 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); 488 489 return vm_dev->pdev->name; 490 } 491 492 static bool vm_get_shm_region(struct virtio_device *vdev, 493 struct virtio_shm_region *region, u8 id) 494 { 495 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); 496 u64 len, addr; 497 498 /* Select the region we're interested in */ 499 writel(id, vm_dev->base + VIRTIO_MMIO_SHM_SEL); 500 501 /* Read the region size */ 502 len = (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_LEN_LOW); 503 len |= (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_LEN_HIGH) << 32; 504 505 region->len = len; 506 507 /* Check if region length is -1. If that's the case, the shared memory 508 * region does not exist and there is no need to proceed further. 509 */ 510 if (len == ~(u64)0) 511 return false; 512 513 /* Read the region base address */ 514 addr = (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_BASE_LOW); 515 addr |= (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_BASE_HIGH) << 32; 516 517 region->addr = addr; 518 519 return true; 520 } 521 522 static const struct virtio_config_ops virtio_mmio_config_ops = { 523 .get = vm_get, 524 .set = vm_set, 525 .generation = vm_generation, 526 .get_status = vm_get_status, 527 .set_status = vm_set_status, 528 .reset = vm_reset, 529 .find_vqs = vm_find_vqs, 530 .del_vqs = vm_del_vqs, 531 .get_features = vm_get_features, 532 .finalize_features = vm_finalize_features, 533 .bus_name = vm_bus_name, 534 .get_shm_region = vm_get_shm_region, 535 .synchronize_cbs = vm_synchronize_cbs, 536 }; 537 538 #ifdef CONFIG_PM_SLEEP 539 static int virtio_mmio_freeze(struct device *dev) 540 { 541 struct virtio_mmio_device *vm_dev = dev_get_drvdata(dev); 542 543 return virtio_device_freeze(&vm_dev->vdev); 544 } 545 546 static int virtio_mmio_restore(struct device *dev) 547 { 548 struct virtio_mmio_device *vm_dev = dev_get_drvdata(dev); 549 550 if (vm_dev->version == 1) 551 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE); 552 553 return virtio_device_restore(&vm_dev->vdev); 554 } 555 556 static const struct dev_pm_ops virtio_mmio_pm_ops = { 557 SET_SYSTEM_SLEEP_PM_OPS(virtio_mmio_freeze, virtio_mmio_restore) 558 }; 559 #endif 560 561 static void virtio_mmio_release_dev(struct device *_d) 562 { 563 struct virtio_device *vdev = 564 container_of(_d, struct virtio_device, dev); 565 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); 566 567 kfree(vm_dev); 568 } 569 570 /* Platform device */ 571 572 static int virtio_mmio_probe(struct platform_device *pdev) 573 { 574 struct virtio_mmio_device *vm_dev; 575 unsigned long magic; 576 int rc; 577 578 vm_dev = kzalloc(sizeof(*vm_dev), GFP_KERNEL); 579 if (!vm_dev) 580 return -ENOMEM; 581 582 vm_dev->vdev.dev.parent = &pdev->dev; 583 vm_dev->vdev.dev.release = virtio_mmio_release_dev; 584 vm_dev->vdev.config = &virtio_mmio_config_ops; 585 vm_dev->pdev = pdev; 586 587 vm_dev->base = devm_platform_ioremap_resource(pdev, 0); 588 if (IS_ERR(vm_dev->base)) { 589 rc = PTR_ERR(vm_dev->base); 590 goto free_vm_dev; 591 } 592 593 /* Check magic value */ 594 magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE); 595 if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) { 596 dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic); 597 rc = -ENODEV; 598 goto free_vm_dev; 599 } 600 601 /* Check device version */ 602 vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION); 603 if (vm_dev->version < 1 || vm_dev->version > 2) { 604 dev_err(&pdev->dev, "Version %ld not supported!\n", 605 vm_dev->version); 606 rc = -ENXIO; 607 goto free_vm_dev; 608 } 609 610 vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID); 611 if (vm_dev->vdev.id.device == 0) { 612 /* 613 * virtio-mmio device with an ID 0 is a (dummy) placeholder 614 * with no function. End probing now with no error reported. 615 */ 616 rc = -ENODEV; 617 goto free_vm_dev; 618 } 619 vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID); 620 621 if (vm_dev->version == 1) { 622 writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE); 623 624 rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)); 625 /* 626 * In the legacy case, ensure our coherently-allocated virtio 627 * ring will be at an address expressable as a 32-bit PFN. 628 */ 629 if (!rc) 630 dma_set_coherent_mask(&pdev->dev, 631 DMA_BIT_MASK(32 + PAGE_SHIFT)); 632 } else { 633 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 634 } 635 if (rc) 636 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 637 if (rc) 638 dev_warn(&pdev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n"); 639 640 platform_set_drvdata(pdev, vm_dev); 641 642 rc = register_virtio_device(&vm_dev->vdev); 643 if (rc) 644 put_device(&vm_dev->vdev.dev); 645 646 return rc; 647 648 free_vm_dev: 649 kfree(vm_dev); 650 return rc; 651 } 652 653 static void virtio_mmio_remove(struct platform_device *pdev) 654 { 655 struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev); 656 unregister_virtio_device(&vm_dev->vdev); 657 } 658 659 660 661 /* Devices list parameter */ 662 663 #if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES) 664 665 static struct device vm_cmdline_parent = { 666 .init_name = "virtio-mmio-cmdline", 667 }; 668 669 static int vm_cmdline_parent_registered; 670 static int vm_cmdline_id; 671 672 static int vm_cmdline_set(const char *device, 673 const struct kernel_param *kp) 674 { 675 int err; 676 struct resource resources[2] = {}; 677 char *str; 678 long long base, size; 679 unsigned int irq; 680 int processed, consumed = 0; 681 struct platform_device *pdev; 682 683 /* Consume "size" part of the command line parameter */ 684 size = memparse(device, &str); 685 686 /* Get "@<base>:<irq>[:<id>]" chunks */ 687 processed = sscanf(str, "@%lli:%u%n:%d%n", 688 &base, &irq, &consumed, 689 &vm_cmdline_id, &consumed); 690 691 /* 692 * sscanf() must process at least 2 chunks; also there 693 * must be no extra characters after the last chunk, so 694 * str[consumed] must be '\0' 695 */ 696 if (processed < 2 || str[consumed] || irq == 0) 697 return -EINVAL; 698 699 resources[0].flags = IORESOURCE_MEM; 700 resources[0].start = base; 701 resources[0].end = base + size - 1; 702 703 resources[1].flags = IORESOURCE_IRQ; 704 resources[1].start = resources[1].end = irq; 705 706 if (!vm_cmdline_parent_registered) { 707 err = device_register(&vm_cmdline_parent); 708 if (err) { 709 put_device(&vm_cmdline_parent); 710 pr_err("Failed to register parent device!\n"); 711 return err; 712 } 713 vm_cmdline_parent_registered = 1; 714 } 715 716 pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n", 717 vm_cmdline_id, 718 (unsigned long long)resources[0].start, 719 (unsigned long long)resources[0].end, 720 (int)resources[1].start); 721 722 pdev = platform_device_register_resndata(&vm_cmdline_parent, 723 "virtio-mmio", vm_cmdline_id++, 724 resources, ARRAY_SIZE(resources), NULL, 0); 725 726 return PTR_ERR_OR_ZERO(pdev); 727 } 728 729 static int vm_cmdline_get_device(struct device *dev, void *data) 730 { 731 char *buffer = data; 732 unsigned int len = strlen(buffer); 733 struct platform_device *pdev = to_platform_device(dev); 734 735 snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n", 736 pdev->resource[0].end - pdev->resource[0].start + 1ULL, 737 (unsigned long long)pdev->resource[0].start, 738 (unsigned long long)pdev->resource[1].start, 739 pdev->id); 740 return 0; 741 } 742 743 static int vm_cmdline_get(char *buffer, const struct kernel_param *kp) 744 { 745 buffer[0] = '\0'; 746 device_for_each_child(&vm_cmdline_parent, buffer, 747 vm_cmdline_get_device); 748 return strlen(buffer) + 1; 749 } 750 751 static const struct kernel_param_ops vm_cmdline_param_ops = { 752 .set = vm_cmdline_set, 753 .get = vm_cmdline_get, 754 }; 755 756 device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR); 757 758 static int vm_unregister_cmdline_device(struct device *dev, 759 void *data) 760 { 761 platform_device_unregister(to_platform_device(dev)); 762 763 return 0; 764 } 765 766 static void vm_unregister_cmdline_devices(void) 767 { 768 if (vm_cmdline_parent_registered) { 769 device_for_each_child(&vm_cmdline_parent, NULL, 770 vm_unregister_cmdline_device); 771 device_unregister(&vm_cmdline_parent); 772 vm_cmdline_parent_registered = 0; 773 } 774 } 775 776 #else 777 778 static void vm_unregister_cmdline_devices(void) 779 { 780 } 781 782 #endif 783 784 /* Platform driver */ 785 786 static const struct of_device_id virtio_mmio_match[] = { 787 { .compatible = "virtio,mmio", }, 788 {}, 789 }; 790 MODULE_DEVICE_TABLE(of, virtio_mmio_match); 791 792 #ifdef CONFIG_ACPI 793 static const struct acpi_device_id virtio_mmio_acpi_match[] = { 794 { "LNRO0005", }, 795 { } 796 }; 797 MODULE_DEVICE_TABLE(acpi, virtio_mmio_acpi_match); 798 #endif 799 800 static struct platform_driver virtio_mmio_driver = { 801 .probe = virtio_mmio_probe, 802 .remove = virtio_mmio_remove, 803 .driver = { 804 .name = "virtio-mmio", 805 .of_match_table = virtio_mmio_match, 806 .acpi_match_table = ACPI_PTR(virtio_mmio_acpi_match), 807 #ifdef CONFIG_PM_SLEEP 808 .pm = &virtio_mmio_pm_ops, 809 #endif 810 }, 811 }; 812 813 static int __init virtio_mmio_init(void) 814 { 815 return platform_driver_register(&virtio_mmio_driver); 816 } 817 818 static void __exit virtio_mmio_exit(void) 819 { 820 platform_driver_unregister(&virtio_mmio_driver); 821 vm_unregister_cmdline_devices(); 822 } 823 824 module_init(virtio_mmio_init); 825 module_exit(virtio_mmio_exit); 826 827 MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>"); 828 MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices"); 829 MODULE_LICENSE("GPL"); 830