1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Mediated virtual PCI display host device driver 4 * 5 * Emulate enough of qemu stdvga to make bochs-drm.ko happy. That is 6 * basically the vram memory bar and the bochs dispi interface vbe 7 * registers in the mmio register bar. Specifically it does *not* 8 * include any legacy vga stuff. Device looks a lot like "qemu -device 9 * secondary-vga". 10 * 11 * (c) Gerd Hoffmann <kraxel@redhat.com> 12 * 13 * based on mtty driver which is: 14 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. 15 * Author: Neo Jia <cjia@nvidia.com> 16 * Kirti Wankhede <kwankhede@nvidia.com> 17 * 18 * This program is free software; you can redistribute it and/or modify 19 * it under the terms of the GNU General Public License version 2 as 20 * published by the Free Software Foundation. 21 */ 22 #include <linux/init.h> 23 #include <linux/module.h> 24 #include <linux/device.h> 25 #include <linux/kernel.h> 26 #include <linux/slab.h> 27 #include <linux/vmalloc.h> 28 #include <linux/cdev.h> 29 #include <linux/vfio.h> 30 #include <linux/iommu.h> 31 #include <linux/sysfs.h> 32 #include <linux/mdev.h> 33 #include <linux/pci.h> 34 #include <linux/dma-buf.h> 35 #include <linux/highmem.h> 36 #include <drm/drm_fourcc.h> 37 #include <drm/drm_rect.h> 38 #include <drm/drm_modeset_lock.h> 39 #include <drm/drm_property.h> 40 #include <drm/drm_plane.h> 41 42 43 #define VBE_DISPI_INDEX_ID 0x0 44 #define VBE_DISPI_INDEX_XRES 0x1 45 #define VBE_DISPI_INDEX_YRES 0x2 46 #define VBE_DISPI_INDEX_BPP 0x3 47 #define VBE_DISPI_INDEX_ENABLE 0x4 48 #define VBE_DISPI_INDEX_BANK 0x5 49 #define VBE_DISPI_INDEX_VIRT_WIDTH 0x6 50 #define VBE_DISPI_INDEX_VIRT_HEIGHT 0x7 51 #define VBE_DISPI_INDEX_X_OFFSET 0x8 52 #define VBE_DISPI_INDEX_Y_OFFSET 0x9 53 #define VBE_DISPI_INDEX_VIDEO_MEMORY_64K 0xa 54 #define VBE_DISPI_INDEX_COUNT 0xb 55 56 #define VBE_DISPI_ID0 0xB0C0 57 #define VBE_DISPI_ID1 0xB0C1 58 #define VBE_DISPI_ID2 0xB0C2 59 #define VBE_DISPI_ID3 0xB0C3 60 #define VBE_DISPI_ID4 0xB0C4 61 #define VBE_DISPI_ID5 0xB0C5 62 63 #define VBE_DISPI_DISABLED 0x00 64 #define VBE_DISPI_ENABLED 0x01 65 #define VBE_DISPI_GETCAPS 0x02 66 #define VBE_DISPI_8BIT_DAC 0x20 67 #define VBE_DISPI_LFB_ENABLED 0x40 68 #define VBE_DISPI_NOCLEARMEM 0x80 69 70 71 #define MBOCHS_NAME "mbochs" 72 #define MBOCHS_CLASS_NAME "mbochs" 73 74 #define MBOCHS_CONFIG_SPACE_SIZE 0xff 75 #define MBOCHS_MMIO_BAR_OFFSET PAGE_SIZE 76 #define MBOCHS_MMIO_BAR_SIZE PAGE_SIZE 77 #define MBOCHS_MEMORY_BAR_OFFSET (MBOCHS_MMIO_BAR_OFFSET + \ 78 MBOCHS_MMIO_BAR_SIZE) 79 80 #define STORE_LE16(addr, val) (*(u16 *)addr = val) 81 #define STORE_LE32(addr, val) (*(u32 *)addr = val) 82 83 84 MODULE_LICENSE("GPL v2"); 85 86 static int max_mbytes = 256; 87 module_param_named(count, max_mbytes, int, 0444); 88 MODULE_PARM_DESC(mem, "megabytes available to " MBOCHS_NAME " devices"); 89 90 91 #define MBOCHS_TYPE_1 "small" 92 #define MBOCHS_TYPE_2 "medium" 93 #define MBOCHS_TYPE_3 "large" 94 95 static const struct mbochs_type { 96 const char *name; 97 u32 mbytes; 98 } mbochs_types[] = { 99 { 100 .name = MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_1, 101 .mbytes = 4, 102 }, { 103 .name = MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_2, 104 .mbytes = 16, 105 }, { 106 .name = MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_3, 107 .mbytes = 64, 108 }, 109 }; 110 111 112 static dev_t mbochs_devt; 113 static struct class *mbochs_class; 114 static struct cdev mbochs_cdev; 115 static struct device mbochs_dev; 116 static int mbochs_used_mbytes; 117 118 struct mbochs_mode { 119 u32 drm_format; 120 u32 bytepp; 121 u32 width; 122 u32 height; 123 u32 stride; 124 u32 __pad; 125 u64 offset; 126 u64 size; 127 }; 128 129 struct mbochs_dmabuf { 130 struct mbochs_mode mode; 131 u32 id; 132 struct page **pages; 133 pgoff_t pagecount; 134 struct dma_buf *buf; 135 struct mdev_state *mdev_state; 136 struct list_head next; 137 bool unlinked; 138 }; 139 140 /* State of each mdev device */ 141 struct mdev_state { 142 u8 *vconfig; 143 u64 bar_mask[3]; 144 u32 memory_bar_mask; 145 struct mutex ops_lock; 146 struct mdev_device *mdev; 147 struct vfio_device_info dev_info; 148 149 const struct mbochs_type *type; 150 u16 vbe[VBE_DISPI_INDEX_COUNT]; 151 u64 memsize; 152 struct page **pages; 153 pgoff_t pagecount; 154 155 struct list_head dmabufs; 156 u32 active_id; 157 u32 next_id; 158 }; 159 160 static const char *vbe_name_list[VBE_DISPI_INDEX_COUNT] = { 161 [VBE_DISPI_INDEX_ID] = "id", 162 [VBE_DISPI_INDEX_XRES] = "xres", 163 [VBE_DISPI_INDEX_YRES] = "yres", 164 [VBE_DISPI_INDEX_BPP] = "bpp", 165 [VBE_DISPI_INDEX_ENABLE] = "enable", 166 [VBE_DISPI_INDEX_BANK] = "bank", 167 [VBE_DISPI_INDEX_VIRT_WIDTH] = "virt-width", 168 [VBE_DISPI_INDEX_VIRT_HEIGHT] = "virt-height", 169 [VBE_DISPI_INDEX_X_OFFSET] = "x-offset", 170 [VBE_DISPI_INDEX_Y_OFFSET] = "y-offset", 171 [VBE_DISPI_INDEX_VIDEO_MEMORY_64K] = "video-mem", 172 }; 173 174 static const char *vbe_name(u32 index) 175 { 176 if (index < ARRAY_SIZE(vbe_name_list)) 177 return vbe_name_list[index]; 178 return "(invalid)"; 179 } 180 181 static struct page *__mbochs_get_page(struct mdev_state *mdev_state, 182 pgoff_t pgoff); 183 static struct page *mbochs_get_page(struct mdev_state *mdev_state, 184 pgoff_t pgoff); 185 186 static const struct mbochs_type *mbochs_find_type(struct kobject *kobj) 187 { 188 int i; 189 190 for (i = 0; i < ARRAY_SIZE(mbochs_types); i++) 191 if (strcmp(mbochs_types[i].name, kobj->name) == 0) 192 return mbochs_types + i; 193 return NULL; 194 } 195 196 static void mbochs_create_config_space(struct mdev_state *mdev_state) 197 { 198 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_VENDOR_ID], 199 0x1234); 200 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_DEVICE_ID], 201 0x1111); 202 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_VENDOR_ID], 203 PCI_SUBVENDOR_ID_REDHAT_QUMRANET); 204 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_ID], 205 PCI_SUBDEVICE_ID_QEMU); 206 207 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_COMMAND], 208 PCI_COMMAND_IO | PCI_COMMAND_MEMORY); 209 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_CLASS_DEVICE], 210 PCI_CLASS_DISPLAY_OTHER); 211 mdev_state->vconfig[PCI_CLASS_REVISION] = 0x01; 212 213 STORE_LE32((u32 *) &mdev_state->vconfig[PCI_BASE_ADDRESS_0], 214 PCI_BASE_ADDRESS_SPACE_MEMORY | 215 PCI_BASE_ADDRESS_MEM_TYPE_32 | 216 PCI_BASE_ADDRESS_MEM_PREFETCH); 217 mdev_state->bar_mask[0] = ~(mdev_state->memsize) + 1; 218 219 STORE_LE32((u32 *) &mdev_state->vconfig[PCI_BASE_ADDRESS_2], 220 PCI_BASE_ADDRESS_SPACE_MEMORY | 221 PCI_BASE_ADDRESS_MEM_TYPE_32); 222 mdev_state->bar_mask[2] = ~(MBOCHS_MMIO_BAR_SIZE) + 1; 223 } 224 225 static int mbochs_check_framebuffer(struct mdev_state *mdev_state, 226 struct mbochs_mode *mode) 227 { 228 struct device *dev = mdev_dev(mdev_state->mdev); 229 u16 *vbe = mdev_state->vbe; 230 u32 virt_width; 231 232 WARN_ON(!mutex_is_locked(&mdev_state->ops_lock)); 233 234 if (!(vbe[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED)) 235 goto nofb; 236 237 memset(mode, 0, sizeof(*mode)); 238 switch (vbe[VBE_DISPI_INDEX_BPP]) { 239 case 32: 240 mode->drm_format = DRM_FORMAT_XRGB8888; 241 mode->bytepp = 4; 242 break; 243 default: 244 dev_info_ratelimited(dev, "%s: bpp %d not supported\n", 245 __func__, vbe[VBE_DISPI_INDEX_BPP]); 246 goto nofb; 247 } 248 249 mode->width = vbe[VBE_DISPI_INDEX_XRES]; 250 mode->height = vbe[VBE_DISPI_INDEX_YRES]; 251 virt_width = vbe[VBE_DISPI_INDEX_VIRT_WIDTH]; 252 if (virt_width < mode->width) 253 virt_width = mode->width; 254 mode->stride = virt_width * mode->bytepp; 255 mode->size = (u64)mode->stride * mode->height; 256 mode->offset = ((u64)vbe[VBE_DISPI_INDEX_X_OFFSET] * mode->bytepp + 257 (u64)vbe[VBE_DISPI_INDEX_Y_OFFSET] * mode->stride); 258 259 if (mode->width < 64 || mode->height < 64) { 260 dev_info_ratelimited(dev, "%s: invalid resolution %dx%d\n", 261 __func__, mode->width, mode->height); 262 goto nofb; 263 } 264 if (mode->offset + mode->size > mdev_state->memsize) { 265 dev_info_ratelimited(dev, "%s: framebuffer memory overflow\n", 266 __func__); 267 goto nofb; 268 } 269 270 return 0; 271 272 nofb: 273 memset(mode, 0, sizeof(*mode)); 274 return -EINVAL; 275 } 276 277 static bool mbochs_modes_equal(struct mbochs_mode *mode1, 278 struct mbochs_mode *mode2) 279 { 280 return memcmp(mode1, mode2, sizeof(struct mbochs_mode)) == 0; 281 } 282 283 static void handle_pci_cfg_write(struct mdev_state *mdev_state, u16 offset, 284 char *buf, u32 count) 285 { 286 struct device *dev = mdev_dev(mdev_state->mdev); 287 int index = (offset - PCI_BASE_ADDRESS_0) / 0x04; 288 u32 cfg_addr; 289 290 switch (offset) { 291 case PCI_BASE_ADDRESS_0: 292 case PCI_BASE_ADDRESS_2: 293 cfg_addr = *(u32 *)buf; 294 295 if (cfg_addr == 0xffffffff) { 296 cfg_addr = (cfg_addr & mdev_state->bar_mask[index]); 297 } else { 298 cfg_addr &= PCI_BASE_ADDRESS_MEM_MASK; 299 if (cfg_addr) 300 dev_info(dev, "BAR #%d @ 0x%x\n", 301 index, cfg_addr); 302 } 303 304 cfg_addr |= (mdev_state->vconfig[offset] & 305 ~PCI_BASE_ADDRESS_MEM_MASK); 306 STORE_LE32(&mdev_state->vconfig[offset], cfg_addr); 307 break; 308 } 309 } 310 311 static void handle_mmio_write(struct mdev_state *mdev_state, u16 offset, 312 char *buf, u32 count) 313 { 314 struct device *dev = mdev_dev(mdev_state->mdev); 315 int index; 316 u16 reg16; 317 318 switch (offset) { 319 case 0x400 ... 0x41f: /* vga ioports remapped */ 320 goto unhandled; 321 case 0x500 ... 0x515: /* bochs dispi interface */ 322 if (count != 2) 323 goto unhandled; 324 index = (offset - 0x500) / 2; 325 reg16 = *(u16 *)buf; 326 if (index < ARRAY_SIZE(mdev_state->vbe)) 327 mdev_state->vbe[index] = reg16; 328 dev_dbg(dev, "%s: vbe write %d = %d (%s)\n", 329 __func__, index, reg16, vbe_name(index)); 330 break; 331 case 0x600 ... 0x607: /* qemu extended regs */ 332 goto unhandled; 333 default: 334 unhandled: 335 dev_dbg(dev, "%s: @0x%03x, count %d (unhandled)\n", 336 __func__, offset, count); 337 break; 338 } 339 } 340 341 static void handle_mmio_read(struct mdev_state *mdev_state, u16 offset, 342 char *buf, u32 count) 343 { 344 struct device *dev = mdev_dev(mdev_state->mdev); 345 u16 reg16 = 0; 346 int index; 347 348 switch (offset) { 349 case 0x500 ... 0x515: /* bochs dispi interface */ 350 if (count != 2) 351 goto unhandled; 352 index = (offset - 0x500) / 2; 353 if (index < ARRAY_SIZE(mdev_state->vbe)) 354 reg16 = mdev_state->vbe[index]; 355 dev_dbg(dev, "%s: vbe read %d = %d (%s)\n", 356 __func__, index, reg16, vbe_name(index)); 357 *(u16 *)buf = reg16; 358 break; 359 default: 360 unhandled: 361 dev_dbg(dev, "%s: @0x%03x, count %d (unhandled)\n", 362 __func__, offset, count); 363 memset(buf, 0, count); 364 break; 365 } 366 } 367 368 static ssize_t mdev_access(struct mdev_device *mdev, char *buf, size_t count, 369 loff_t pos, bool is_write) 370 { 371 struct mdev_state *mdev_state = mdev_get_drvdata(mdev); 372 struct device *dev = mdev_dev(mdev); 373 struct page *pg; 374 loff_t poff; 375 char *map; 376 int ret = 0; 377 378 mutex_lock(&mdev_state->ops_lock); 379 380 if (pos < MBOCHS_CONFIG_SPACE_SIZE) { 381 if (is_write) 382 handle_pci_cfg_write(mdev_state, pos, buf, count); 383 else 384 memcpy(buf, (mdev_state->vconfig + pos), count); 385 386 } else if (pos >= MBOCHS_MMIO_BAR_OFFSET && 387 pos + count <= MBOCHS_MEMORY_BAR_OFFSET) { 388 pos -= MBOCHS_MMIO_BAR_OFFSET; 389 if (is_write) 390 handle_mmio_write(mdev_state, pos, buf, count); 391 else 392 handle_mmio_read(mdev_state, pos, buf, count); 393 394 } else if (pos >= MBOCHS_MEMORY_BAR_OFFSET && 395 pos + count <= 396 MBOCHS_MEMORY_BAR_OFFSET + mdev_state->memsize) { 397 pos -= MBOCHS_MMIO_BAR_OFFSET; 398 poff = pos & ~PAGE_MASK; 399 pg = __mbochs_get_page(mdev_state, pos >> PAGE_SHIFT); 400 map = kmap(pg); 401 if (is_write) 402 memcpy(map + poff, buf, count); 403 else 404 memcpy(buf, map + poff, count); 405 kunmap(pg); 406 put_page(pg); 407 408 } else { 409 dev_dbg(dev, "%s: %s @0x%llx (unhandled)\n", 410 __func__, is_write ? "WR" : "RD", pos); 411 ret = -1; 412 goto accessfailed; 413 } 414 415 ret = count; 416 417 418 accessfailed: 419 mutex_unlock(&mdev_state->ops_lock); 420 421 return ret; 422 } 423 424 static int mbochs_reset(struct mdev_device *mdev) 425 { 426 struct mdev_state *mdev_state = mdev_get_drvdata(mdev); 427 u32 size64k = mdev_state->memsize / (64 * 1024); 428 int i; 429 430 for (i = 0; i < ARRAY_SIZE(mdev_state->vbe); i++) 431 mdev_state->vbe[i] = 0; 432 mdev_state->vbe[VBE_DISPI_INDEX_ID] = VBE_DISPI_ID5; 433 mdev_state->vbe[VBE_DISPI_INDEX_VIDEO_MEMORY_64K] = size64k; 434 return 0; 435 } 436 437 static int mbochs_create(struct kobject *kobj, struct mdev_device *mdev) 438 { 439 const struct mbochs_type *type = mbochs_find_type(kobj); 440 struct device *dev = mdev_dev(mdev); 441 struct mdev_state *mdev_state; 442 443 if (!type) 444 type = &mbochs_types[0]; 445 if (type->mbytes + mbochs_used_mbytes > max_mbytes) 446 return -ENOMEM; 447 448 mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL); 449 if (mdev_state == NULL) 450 return -ENOMEM; 451 452 mdev_state->vconfig = kzalloc(MBOCHS_CONFIG_SPACE_SIZE, GFP_KERNEL); 453 if (mdev_state->vconfig == NULL) 454 goto err_mem; 455 456 mdev_state->memsize = type->mbytes * 1024 * 1024; 457 mdev_state->pagecount = mdev_state->memsize >> PAGE_SHIFT; 458 mdev_state->pages = kcalloc(mdev_state->pagecount, 459 sizeof(struct page *), 460 GFP_KERNEL); 461 if (!mdev_state->pages) 462 goto err_mem; 463 464 dev_info(dev, "%s: %s, %d MB, %ld pages\n", __func__, 465 kobj->name, type->mbytes, mdev_state->pagecount); 466 467 mutex_init(&mdev_state->ops_lock); 468 mdev_state->mdev = mdev; 469 mdev_set_drvdata(mdev, mdev_state); 470 INIT_LIST_HEAD(&mdev_state->dmabufs); 471 mdev_state->next_id = 1; 472 473 mdev_state->type = type; 474 mbochs_create_config_space(mdev_state); 475 mbochs_reset(mdev); 476 477 mbochs_used_mbytes += type->mbytes; 478 return 0; 479 480 err_mem: 481 kfree(mdev_state->vconfig); 482 kfree(mdev_state); 483 return -ENOMEM; 484 } 485 486 static int mbochs_remove(struct mdev_device *mdev) 487 { 488 struct mdev_state *mdev_state = mdev_get_drvdata(mdev); 489 490 mbochs_used_mbytes -= mdev_state->type->mbytes; 491 mdev_set_drvdata(mdev, NULL); 492 kfree(mdev_state->pages); 493 kfree(mdev_state->vconfig); 494 kfree(mdev_state); 495 return 0; 496 } 497 498 static ssize_t mbochs_read(struct mdev_device *mdev, char __user *buf, 499 size_t count, loff_t *ppos) 500 { 501 unsigned int done = 0; 502 int ret; 503 504 while (count) { 505 size_t filled; 506 507 if (count >= 4 && !(*ppos % 4)) { 508 u32 val; 509 510 ret = mdev_access(mdev, (char *)&val, sizeof(val), 511 *ppos, false); 512 if (ret <= 0) 513 goto read_err; 514 515 if (copy_to_user(buf, &val, sizeof(val))) 516 goto read_err; 517 518 filled = 4; 519 } else if (count >= 2 && !(*ppos % 2)) { 520 u16 val; 521 522 ret = mdev_access(mdev, (char *)&val, sizeof(val), 523 *ppos, false); 524 if (ret <= 0) 525 goto read_err; 526 527 if (copy_to_user(buf, &val, sizeof(val))) 528 goto read_err; 529 530 filled = 2; 531 } else { 532 u8 val; 533 534 ret = mdev_access(mdev, (char *)&val, sizeof(val), 535 *ppos, false); 536 if (ret <= 0) 537 goto read_err; 538 539 if (copy_to_user(buf, &val, sizeof(val))) 540 goto read_err; 541 542 filled = 1; 543 } 544 545 count -= filled; 546 done += filled; 547 *ppos += filled; 548 buf += filled; 549 } 550 551 return done; 552 553 read_err: 554 return -EFAULT; 555 } 556 557 static ssize_t mbochs_write(struct mdev_device *mdev, const char __user *buf, 558 size_t count, loff_t *ppos) 559 { 560 unsigned int done = 0; 561 int ret; 562 563 while (count) { 564 size_t filled; 565 566 if (count >= 4 && !(*ppos % 4)) { 567 u32 val; 568 569 if (copy_from_user(&val, buf, sizeof(val))) 570 goto write_err; 571 572 ret = mdev_access(mdev, (char *)&val, sizeof(val), 573 *ppos, true); 574 if (ret <= 0) 575 goto write_err; 576 577 filled = 4; 578 } else if (count >= 2 && !(*ppos % 2)) { 579 u16 val; 580 581 if (copy_from_user(&val, buf, sizeof(val))) 582 goto write_err; 583 584 ret = mdev_access(mdev, (char *)&val, sizeof(val), 585 *ppos, true); 586 if (ret <= 0) 587 goto write_err; 588 589 filled = 2; 590 } else { 591 u8 val; 592 593 if (copy_from_user(&val, buf, sizeof(val))) 594 goto write_err; 595 596 ret = mdev_access(mdev, (char *)&val, sizeof(val), 597 *ppos, true); 598 if (ret <= 0) 599 goto write_err; 600 601 filled = 1; 602 } 603 count -= filled; 604 done += filled; 605 *ppos += filled; 606 buf += filled; 607 } 608 609 return done; 610 write_err: 611 return -EFAULT; 612 } 613 614 static struct page *__mbochs_get_page(struct mdev_state *mdev_state, 615 pgoff_t pgoff) 616 { 617 WARN_ON(!mutex_is_locked(&mdev_state->ops_lock)); 618 619 if (!mdev_state->pages[pgoff]) { 620 mdev_state->pages[pgoff] = 621 alloc_pages(GFP_HIGHUSER | __GFP_ZERO, 0); 622 if (!mdev_state->pages[pgoff]) 623 return NULL; 624 } 625 626 get_page(mdev_state->pages[pgoff]); 627 return mdev_state->pages[pgoff]; 628 } 629 630 static struct page *mbochs_get_page(struct mdev_state *mdev_state, 631 pgoff_t pgoff) 632 { 633 struct page *page; 634 635 if (WARN_ON(pgoff >= mdev_state->pagecount)) 636 return NULL; 637 638 mutex_lock(&mdev_state->ops_lock); 639 page = __mbochs_get_page(mdev_state, pgoff); 640 mutex_unlock(&mdev_state->ops_lock); 641 642 return page; 643 } 644 645 static void mbochs_put_pages(struct mdev_state *mdev_state) 646 { 647 struct device *dev = mdev_dev(mdev_state->mdev); 648 int i, count = 0; 649 650 WARN_ON(!mutex_is_locked(&mdev_state->ops_lock)); 651 652 for (i = 0; i < mdev_state->pagecount; i++) { 653 if (!mdev_state->pages[i]) 654 continue; 655 put_page(mdev_state->pages[i]); 656 mdev_state->pages[i] = NULL; 657 count++; 658 } 659 dev_dbg(dev, "%s: %d pages released\n", __func__, count); 660 } 661 662 static vm_fault_t mbochs_region_vm_fault(struct vm_fault *vmf) 663 { 664 struct vm_area_struct *vma = vmf->vma; 665 struct mdev_state *mdev_state = vma->vm_private_data; 666 pgoff_t page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT; 667 668 if (page_offset >= mdev_state->pagecount) 669 return VM_FAULT_SIGBUS; 670 671 vmf->page = mbochs_get_page(mdev_state, page_offset); 672 if (!vmf->page) 673 return VM_FAULT_SIGBUS; 674 675 return 0; 676 } 677 678 static const struct vm_operations_struct mbochs_region_vm_ops = { 679 .fault = mbochs_region_vm_fault, 680 }; 681 682 static int mbochs_mmap(struct mdev_device *mdev, struct vm_area_struct *vma) 683 { 684 struct mdev_state *mdev_state = mdev_get_drvdata(mdev); 685 686 if (vma->vm_pgoff != MBOCHS_MEMORY_BAR_OFFSET >> PAGE_SHIFT) 687 return -EINVAL; 688 if (vma->vm_end < vma->vm_start) 689 return -EINVAL; 690 if (vma->vm_end - vma->vm_start > mdev_state->memsize) 691 return -EINVAL; 692 if ((vma->vm_flags & VM_SHARED) == 0) 693 return -EINVAL; 694 695 vma->vm_ops = &mbochs_region_vm_ops; 696 vma->vm_private_data = mdev_state; 697 return 0; 698 } 699 700 static vm_fault_t mbochs_dmabuf_vm_fault(struct vm_fault *vmf) 701 { 702 struct vm_area_struct *vma = vmf->vma; 703 struct mbochs_dmabuf *dmabuf = vma->vm_private_data; 704 705 if (WARN_ON(vmf->pgoff >= dmabuf->pagecount)) 706 return VM_FAULT_SIGBUS; 707 708 vmf->page = dmabuf->pages[vmf->pgoff]; 709 get_page(vmf->page); 710 return 0; 711 } 712 713 static const struct vm_operations_struct mbochs_dmabuf_vm_ops = { 714 .fault = mbochs_dmabuf_vm_fault, 715 }; 716 717 static int mbochs_mmap_dmabuf(struct dma_buf *buf, struct vm_area_struct *vma) 718 { 719 struct mbochs_dmabuf *dmabuf = buf->priv; 720 struct device *dev = mdev_dev(dmabuf->mdev_state->mdev); 721 722 dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id); 723 724 if ((vma->vm_flags & VM_SHARED) == 0) 725 return -EINVAL; 726 727 vma->vm_ops = &mbochs_dmabuf_vm_ops; 728 vma->vm_private_data = dmabuf; 729 return 0; 730 } 731 732 static void mbochs_print_dmabuf(struct mbochs_dmabuf *dmabuf, 733 const char *prefix) 734 { 735 struct device *dev = mdev_dev(dmabuf->mdev_state->mdev); 736 u32 fourcc = dmabuf->mode.drm_format; 737 738 dev_dbg(dev, "%s/%d: %c%c%c%c, %dx%d, stride %d, off 0x%llx, size 0x%llx, pages %ld\n", 739 prefix, dmabuf->id, 740 fourcc ? ((fourcc >> 0) & 0xff) : '-', 741 fourcc ? ((fourcc >> 8) & 0xff) : '-', 742 fourcc ? ((fourcc >> 16) & 0xff) : '-', 743 fourcc ? ((fourcc >> 24) & 0xff) : '-', 744 dmabuf->mode.width, dmabuf->mode.height, dmabuf->mode.stride, 745 dmabuf->mode.offset, dmabuf->mode.size, dmabuf->pagecount); 746 } 747 748 static struct sg_table *mbochs_map_dmabuf(struct dma_buf_attachment *at, 749 enum dma_data_direction direction) 750 { 751 struct mbochs_dmabuf *dmabuf = at->dmabuf->priv; 752 struct device *dev = mdev_dev(dmabuf->mdev_state->mdev); 753 struct sg_table *sg; 754 755 dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id); 756 757 sg = kzalloc(sizeof(*sg), GFP_KERNEL); 758 if (!sg) 759 goto err1; 760 if (sg_alloc_table_from_pages(sg, dmabuf->pages, dmabuf->pagecount, 761 0, dmabuf->mode.size, GFP_KERNEL) < 0) 762 goto err2; 763 if (!dma_map_sg(at->dev, sg->sgl, sg->nents, direction)) 764 goto err3; 765 766 return sg; 767 768 err3: 769 sg_free_table(sg); 770 err2: 771 kfree(sg); 772 err1: 773 return ERR_PTR(-ENOMEM); 774 } 775 776 static void mbochs_unmap_dmabuf(struct dma_buf_attachment *at, 777 struct sg_table *sg, 778 enum dma_data_direction direction) 779 { 780 struct mbochs_dmabuf *dmabuf = at->dmabuf->priv; 781 struct device *dev = mdev_dev(dmabuf->mdev_state->mdev); 782 783 dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id); 784 785 sg_free_table(sg); 786 kfree(sg); 787 } 788 789 static void mbochs_release_dmabuf(struct dma_buf *buf) 790 { 791 struct mbochs_dmabuf *dmabuf = buf->priv; 792 struct mdev_state *mdev_state = dmabuf->mdev_state; 793 struct device *dev = mdev_dev(mdev_state->mdev); 794 pgoff_t pg; 795 796 dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id); 797 798 for (pg = 0; pg < dmabuf->pagecount; pg++) 799 put_page(dmabuf->pages[pg]); 800 801 mutex_lock(&mdev_state->ops_lock); 802 dmabuf->buf = NULL; 803 if (dmabuf->unlinked) 804 kfree(dmabuf); 805 mutex_unlock(&mdev_state->ops_lock); 806 } 807 808 static void *mbochs_kmap_dmabuf(struct dma_buf *buf, unsigned long page_num) 809 { 810 struct mbochs_dmabuf *dmabuf = buf->priv; 811 struct page *page = dmabuf->pages[page_num]; 812 813 return kmap(page); 814 } 815 816 static void mbochs_kunmap_dmabuf(struct dma_buf *buf, unsigned long page_num, 817 void *vaddr) 818 { 819 kunmap(vaddr); 820 } 821 822 static struct dma_buf_ops mbochs_dmabuf_ops = { 823 .map_dma_buf = mbochs_map_dmabuf, 824 .unmap_dma_buf = mbochs_unmap_dmabuf, 825 .release = mbochs_release_dmabuf, 826 .map = mbochs_kmap_dmabuf, 827 .unmap = mbochs_kunmap_dmabuf, 828 .mmap = mbochs_mmap_dmabuf, 829 }; 830 831 static struct mbochs_dmabuf *mbochs_dmabuf_alloc(struct mdev_state *mdev_state, 832 struct mbochs_mode *mode) 833 { 834 struct mbochs_dmabuf *dmabuf; 835 pgoff_t page_offset, pg; 836 837 WARN_ON(!mutex_is_locked(&mdev_state->ops_lock)); 838 839 dmabuf = kzalloc(sizeof(struct mbochs_dmabuf), GFP_KERNEL); 840 if (!dmabuf) 841 return NULL; 842 843 dmabuf->mode = *mode; 844 dmabuf->id = mdev_state->next_id++; 845 dmabuf->pagecount = DIV_ROUND_UP(mode->size, PAGE_SIZE); 846 dmabuf->pages = kcalloc(dmabuf->pagecount, sizeof(struct page *), 847 GFP_KERNEL); 848 if (!dmabuf->pages) 849 goto err_free_dmabuf; 850 851 page_offset = dmabuf->mode.offset >> PAGE_SHIFT; 852 for (pg = 0; pg < dmabuf->pagecount; pg++) { 853 dmabuf->pages[pg] = __mbochs_get_page(mdev_state, 854 page_offset + pg); 855 if (!dmabuf->pages[pg]) 856 goto err_free_pages; 857 } 858 859 dmabuf->mdev_state = mdev_state; 860 list_add(&dmabuf->next, &mdev_state->dmabufs); 861 862 mbochs_print_dmabuf(dmabuf, __func__); 863 return dmabuf; 864 865 err_free_pages: 866 while (pg > 0) 867 put_page(dmabuf->pages[--pg]); 868 kfree(dmabuf->pages); 869 err_free_dmabuf: 870 kfree(dmabuf); 871 return NULL; 872 } 873 874 static struct mbochs_dmabuf * 875 mbochs_dmabuf_find_by_mode(struct mdev_state *mdev_state, 876 struct mbochs_mode *mode) 877 { 878 struct mbochs_dmabuf *dmabuf; 879 880 WARN_ON(!mutex_is_locked(&mdev_state->ops_lock)); 881 882 list_for_each_entry(dmabuf, &mdev_state->dmabufs, next) 883 if (mbochs_modes_equal(&dmabuf->mode, mode)) 884 return dmabuf; 885 886 return NULL; 887 } 888 889 static struct mbochs_dmabuf * 890 mbochs_dmabuf_find_by_id(struct mdev_state *mdev_state, u32 id) 891 { 892 struct mbochs_dmabuf *dmabuf; 893 894 WARN_ON(!mutex_is_locked(&mdev_state->ops_lock)); 895 896 list_for_each_entry(dmabuf, &mdev_state->dmabufs, next) 897 if (dmabuf->id == id) 898 return dmabuf; 899 900 return NULL; 901 } 902 903 static int mbochs_dmabuf_export(struct mbochs_dmabuf *dmabuf) 904 { 905 struct mdev_state *mdev_state = dmabuf->mdev_state; 906 struct device *dev = mdev_dev(mdev_state->mdev); 907 DEFINE_DMA_BUF_EXPORT_INFO(exp_info); 908 struct dma_buf *buf; 909 910 WARN_ON(!mutex_is_locked(&mdev_state->ops_lock)); 911 912 if (!IS_ALIGNED(dmabuf->mode.offset, PAGE_SIZE)) { 913 dev_info_ratelimited(dev, "%s: framebuffer not page-aligned\n", 914 __func__); 915 return -EINVAL; 916 } 917 918 exp_info.ops = &mbochs_dmabuf_ops; 919 exp_info.size = dmabuf->mode.size; 920 exp_info.priv = dmabuf; 921 922 buf = dma_buf_export(&exp_info); 923 if (IS_ERR(buf)) { 924 dev_info_ratelimited(dev, "%s: dma_buf_export failed: %ld\n", 925 __func__, PTR_ERR(buf)); 926 return PTR_ERR(buf); 927 } 928 929 dmabuf->buf = buf; 930 dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id); 931 return 0; 932 } 933 934 static int mbochs_get_region_info(struct mdev_device *mdev, 935 struct vfio_region_info *region_info, 936 u16 *cap_type_id, void **cap_type) 937 { 938 struct mdev_state *mdev_state; 939 940 mdev_state = mdev_get_drvdata(mdev); 941 if (!mdev_state) 942 return -EINVAL; 943 944 if (region_info->index >= VFIO_PCI_NUM_REGIONS) 945 return -EINVAL; 946 947 switch (region_info->index) { 948 case VFIO_PCI_CONFIG_REGION_INDEX: 949 region_info->offset = 0; 950 region_info->size = MBOCHS_CONFIG_SPACE_SIZE; 951 region_info->flags = (VFIO_REGION_INFO_FLAG_READ | 952 VFIO_REGION_INFO_FLAG_WRITE); 953 break; 954 case VFIO_PCI_BAR0_REGION_INDEX: 955 region_info->offset = MBOCHS_MEMORY_BAR_OFFSET; 956 region_info->size = mdev_state->memsize; 957 region_info->flags = (VFIO_REGION_INFO_FLAG_READ | 958 VFIO_REGION_INFO_FLAG_WRITE | 959 VFIO_REGION_INFO_FLAG_MMAP); 960 break; 961 case VFIO_PCI_BAR2_REGION_INDEX: 962 region_info->offset = MBOCHS_MMIO_BAR_OFFSET; 963 region_info->size = MBOCHS_MMIO_BAR_SIZE; 964 region_info->flags = (VFIO_REGION_INFO_FLAG_READ | 965 VFIO_REGION_INFO_FLAG_WRITE); 966 break; 967 default: 968 region_info->size = 0; 969 region_info->offset = 0; 970 region_info->flags = 0; 971 } 972 973 return 0; 974 } 975 976 static int mbochs_get_irq_info(struct mdev_device *mdev, 977 struct vfio_irq_info *irq_info) 978 { 979 irq_info->count = 0; 980 return 0; 981 } 982 983 static int mbochs_get_device_info(struct mdev_device *mdev, 984 struct vfio_device_info *dev_info) 985 { 986 dev_info->flags = VFIO_DEVICE_FLAGS_PCI; 987 dev_info->num_regions = VFIO_PCI_NUM_REGIONS; 988 dev_info->num_irqs = VFIO_PCI_NUM_IRQS; 989 return 0; 990 } 991 992 static int mbochs_query_gfx_plane(struct mdev_device *mdev, 993 struct vfio_device_gfx_plane_info *plane) 994 { 995 struct mdev_state *mdev_state = mdev_get_drvdata(mdev); 996 struct device *dev = mdev_dev(mdev); 997 struct mbochs_dmabuf *dmabuf; 998 struct mbochs_mode mode; 999 int ret; 1000 1001 if (plane->flags & VFIO_GFX_PLANE_TYPE_PROBE) { 1002 if (plane->flags == (VFIO_GFX_PLANE_TYPE_PROBE | 1003 VFIO_GFX_PLANE_TYPE_DMABUF)) 1004 return 0; 1005 return -EINVAL; 1006 } 1007 1008 if (plane->flags != VFIO_GFX_PLANE_TYPE_DMABUF) 1009 return -EINVAL; 1010 1011 plane->drm_format_mod = 0; 1012 plane->x_pos = 0; 1013 plane->y_pos = 0; 1014 plane->x_hot = 0; 1015 plane->y_hot = 0; 1016 1017 mutex_lock(&mdev_state->ops_lock); 1018 1019 ret = -EINVAL; 1020 if (plane->drm_plane_type == DRM_PLANE_TYPE_PRIMARY) 1021 ret = mbochs_check_framebuffer(mdev_state, &mode); 1022 if (ret < 0) { 1023 plane->drm_format = 0; 1024 plane->width = 0; 1025 plane->height = 0; 1026 plane->stride = 0; 1027 plane->size = 0; 1028 plane->dmabuf_id = 0; 1029 goto done; 1030 } 1031 1032 dmabuf = mbochs_dmabuf_find_by_mode(mdev_state, &mode); 1033 if (!dmabuf) 1034 mbochs_dmabuf_alloc(mdev_state, &mode); 1035 if (!dmabuf) { 1036 mutex_unlock(&mdev_state->ops_lock); 1037 return -ENOMEM; 1038 } 1039 1040 plane->drm_format = dmabuf->mode.drm_format; 1041 plane->width = dmabuf->mode.width; 1042 plane->height = dmabuf->mode.height; 1043 plane->stride = dmabuf->mode.stride; 1044 plane->size = dmabuf->mode.size; 1045 plane->dmabuf_id = dmabuf->id; 1046 1047 done: 1048 if (plane->drm_plane_type == DRM_PLANE_TYPE_PRIMARY && 1049 mdev_state->active_id != plane->dmabuf_id) { 1050 dev_dbg(dev, "%s: primary: %d => %d\n", __func__, 1051 mdev_state->active_id, plane->dmabuf_id); 1052 mdev_state->active_id = plane->dmabuf_id; 1053 } 1054 mutex_unlock(&mdev_state->ops_lock); 1055 return 0; 1056 } 1057 1058 static int mbochs_get_gfx_dmabuf(struct mdev_device *mdev, 1059 u32 id) 1060 { 1061 struct mdev_state *mdev_state = mdev_get_drvdata(mdev); 1062 struct mbochs_dmabuf *dmabuf; 1063 1064 mutex_lock(&mdev_state->ops_lock); 1065 1066 dmabuf = mbochs_dmabuf_find_by_id(mdev_state, id); 1067 if (!dmabuf) { 1068 mutex_unlock(&mdev_state->ops_lock); 1069 return -ENOENT; 1070 } 1071 1072 if (!dmabuf->buf) 1073 mbochs_dmabuf_export(dmabuf); 1074 1075 mutex_unlock(&mdev_state->ops_lock); 1076 1077 if (!dmabuf->buf) 1078 return -EINVAL; 1079 1080 return dma_buf_fd(dmabuf->buf, 0); 1081 } 1082 1083 static long mbochs_ioctl(struct mdev_device *mdev, unsigned int cmd, 1084 unsigned long arg) 1085 { 1086 int ret = 0; 1087 unsigned long minsz; 1088 struct mdev_state *mdev_state; 1089 1090 mdev_state = mdev_get_drvdata(mdev); 1091 1092 switch (cmd) { 1093 case VFIO_DEVICE_GET_INFO: 1094 { 1095 struct vfio_device_info info; 1096 1097 minsz = offsetofend(struct vfio_device_info, num_irqs); 1098 1099 if (copy_from_user(&info, (void __user *)arg, minsz)) 1100 return -EFAULT; 1101 1102 if (info.argsz < minsz) 1103 return -EINVAL; 1104 1105 ret = mbochs_get_device_info(mdev, &info); 1106 if (ret) 1107 return ret; 1108 1109 memcpy(&mdev_state->dev_info, &info, sizeof(info)); 1110 1111 if (copy_to_user((void __user *)arg, &info, minsz)) 1112 return -EFAULT; 1113 1114 return 0; 1115 } 1116 case VFIO_DEVICE_GET_REGION_INFO: 1117 { 1118 struct vfio_region_info info; 1119 u16 cap_type_id = 0; 1120 void *cap_type = NULL; 1121 1122 minsz = offsetofend(struct vfio_region_info, offset); 1123 1124 if (copy_from_user(&info, (void __user *)arg, minsz)) 1125 return -EFAULT; 1126 1127 if (info.argsz < minsz) 1128 return -EINVAL; 1129 1130 ret = mbochs_get_region_info(mdev, &info, &cap_type_id, 1131 &cap_type); 1132 if (ret) 1133 return ret; 1134 1135 if (copy_to_user((void __user *)arg, &info, minsz)) 1136 return -EFAULT; 1137 1138 return 0; 1139 } 1140 1141 case VFIO_DEVICE_GET_IRQ_INFO: 1142 { 1143 struct vfio_irq_info info; 1144 1145 minsz = offsetofend(struct vfio_irq_info, count); 1146 1147 if (copy_from_user(&info, (void __user *)arg, minsz)) 1148 return -EFAULT; 1149 1150 if ((info.argsz < minsz) || 1151 (info.index >= mdev_state->dev_info.num_irqs)) 1152 return -EINVAL; 1153 1154 ret = mbochs_get_irq_info(mdev, &info); 1155 if (ret) 1156 return ret; 1157 1158 if (copy_to_user((void __user *)arg, &info, minsz)) 1159 return -EFAULT; 1160 1161 return 0; 1162 } 1163 1164 case VFIO_DEVICE_QUERY_GFX_PLANE: 1165 { 1166 struct vfio_device_gfx_plane_info plane; 1167 1168 minsz = offsetofend(struct vfio_device_gfx_plane_info, 1169 region_index); 1170 1171 if (copy_from_user(&plane, (void __user *)arg, minsz)) 1172 return -EFAULT; 1173 1174 if (plane.argsz < minsz) 1175 return -EINVAL; 1176 1177 ret = mbochs_query_gfx_plane(mdev, &plane); 1178 if (ret) 1179 return ret; 1180 1181 if (copy_to_user((void __user *)arg, &plane, minsz)) 1182 return -EFAULT; 1183 1184 return 0; 1185 } 1186 1187 case VFIO_DEVICE_GET_GFX_DMABUF: 1188 { 1189 u32 dmabuf_id; 1190 1191 if (get_user(dmabuf_id, (__u32 __user *)arg)) 1192 return -EFAULT; 1193 1194 return mbochs_get_gfx_dmabuf(mdev, dmabuf_id); 1195 } 1196 1197 case VFIO_DEVICE_SET_IRQS: 1198 return -EINVAL; 1199 1200 case VFIO_DEVICE_RESET: 1201 return mbochs_reset(mdev); 1202 } 1203 return -ENOTTY; 1204 } 1205 1206 static int mbochs_open(struct mdev_device *mdev) 1207 { 1208 if (!try_module_get(THIS_MODULE)) 1209 return -ENODEV; 1210 1211 return 0; 1212 } 1213 1214 static void mbochs_close(struct mdev_device *mdev) 1215 { 1216 struct mdev_state *mdev_state = mdev_get_drvdata(mdev); 1217 struct mbochs_dmabuf *dmabuf, *tmp; 1218 1219 mutex_lock(&mdev_state->ops_lock); 1220 1221 list_for_each_entry_safe(dmabuf, tmp, &mdev_state->dmabufs, next) { 1222 list_del(&dmabuf->next); 1223 if (dmabuf->buf) { 1224 /* free in mbochs_release_dmabuf() */ 1225 dmabuf->unlinked = true; 1226 } else { 1227 kfree(dmabuf); 1228 } 1229 } 1230 mbochs_put_pages(mdev_state); 1231 1232 mutex_unlock(&mdev_state->ops_lock); 1233 module_put(THIS_MODULE); 1234 } 1235 1236 static ssize_t 1237 memory_show(struct device *dev, struct device_attribute *attr, 1238 char *buf) 1239 { 1240 struct mdev_device *mdev = mdev_from_dev(dev); 1241 struct mdev_state *mdev_state = mdev_get_drvdata(mdev); 1242 1243 return sprintf(buf, "%d MB\n", mdev_state->type->mbytes); 1244 } 1245 static DEVICE_ATTR_RO(memory); 1246 1247 static struct attribute *mdev_dev_attrs[] = { 1248 &dev_attr_memory.attr, 1249 NULL, 1250 }; 1251 1252 static const struct attribute_group mdev_dev_group = { 1253 .name = "vendor", 1254 .attrs = mdev_dev_attrs, 1255 }; 1256 1257 const struct attribute_group *mdev_dev_groups[] = { 1258 &mdev_dev_group, 1259 NULL, 1260 }; 1261 1262 static ssize_t 1263 name_show(struct kobject *kobj, struct device *dev, char *buf) 1264 { 1265 return sprintf(buf, "%s\n", kobj->name); 1266 } 1267 MDEV_TYPE_ATTR_RO(name); 1268 1269 static ssize_t 1270 description_show(struct kobject *kobj, struct device *dev, char *buf) 1271 { 1272 const struct mbochs_type *type = mbochs_find_type(kobj); 1273 1274 return sprintf(buf, "virtual display, %d MB video memory\n", 1275 type ? type->mbytes : 0); 1276 } 1277 MDEV_TYPE_ATTR_RO(description); 1278 1279 static ssize_t 1280 available_instances_show(struct kobject *kobj, struct device *dev, char *buf) 1281 { 1282 const struct mbochs_type *type = mbochs_find_type(kobj); 1283 int count = (max_mbytes - mbochs_used_mbytes) / type->mbytes; 1284 1285 return sprintf(buf, "%d\n", count); 1286 } 1287 MDEV_TYPE_ATTR_RO(available_instances); 1288 1289 static ssize_t device_api_show(struct kobject *kobj, struct device *dev, 1290 char *buf) 1291 { 1292 return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING); 1293 } 1294 MDEV_TYPE_ATTR_RO(device_api); 1295 1296 static struct attribute *mdev_types_attrs[] = { 1297 &mdev_type_attr_name.attr, 1298 &mdev_type_attr_description.attr, 1299 &mdev_type_attr_device_api.attr, 1300 &mdev_type_attr_available_instances.attr, 1301 NULL, 1302 }; 1303 1304 static struct attribute_group mdev_type_group1 = { 1305 .name = MBOCHS_TYPE_1, 1306 .attrs = mdev_types_attrs, 1307 }; 1308 1309 static struct attribute_group mdev_type_group2 = { 1310 .name = MBOCHS_TYPE_2, 1311 .attrs = mdev_types_attrs, 1312 }; 1313 1314 static struct attribute_group mdev_type_group3 = { 1315 .name = MBOCHS_TYPE_3, 1316 .attrs = mdev_types_attrs, 1317 }; 1318 1319 static struct attribute_group *mdev_type_groups[] = { 1320 &mdev_type_group1, 1321 &mdev_type_group2, 1322 &mdev_type_group3, 1323 NULL, 1324 }; 1325 1326 static const struct mdev_parent_ops mdev_fops = { 1327 .owner = THIS_MODULE, 1328 .mdev_attr_groups = mdev_dev_groups, 1329 .supported_type_groups = mdev_type_groups, 1330 .create = mbochs_create, 1331 .remove = mbochs_remove, 1332 .open = mbochs_open, 1333 .release = mbochs_close, 1334 .read = mbochs_read, 1335 .write = mbochs_write, 1336 .ioctl = mbochs_ioctl, 1337 .mmap = mbochs_mmap, 1338 }; 1339 1340 static const struct file_operations vd_fops = { 1341 .owner = THIS_MODULE, 1342 }; 1343 1344 static void mbochs_device_release(struct device *dev) 1345 { 1346 /* nothing */ 1347 } 1348 1349 static int __init mbochs_dev_init(void) 1350 { 1351 int ret = 0; 1352 1353 ret = alloc_chrdev_region(&mbochs_devt, 0, MINORMASK, MBOCHS_NAME); 1354 if (ret < 0) { 1355 pr_err("Error: failed to register mbochs_dev, err: %d\n", ret); 1356 return ret; 1357 } 1358 cdev_init(&mbochs_cdev, &vd_fops); 1359 cdev_add(&mbochs_cdev, mbochs_devt, MINORMASK); 1360 pr_info("%s: major %d\n", __func__, MAJOR(mbochs_devt)); 1361 1362 mbochs_class = class_create(THIS_MODULE, MBOCHS_CLASS_NAME); 1363 if (IS_ERR(mbochs_class)) { 1364 pr_err("Error: failed to register mbochs_dev class\n"); 1365 ret = PTR_ERR(mbochs_class); 1366 goto failed1; 1367 } 1368 mbochs_dev.class = mbochs_class; 1369 mbochs_dev.release = mbochs_device_release; 1370 dev_set_name(&mbochs_dev, "%s", MBOCHS_NAME); 1371 1372 ret = device_register(&mbochs_dev); 1373 if (ret) 1374 goto failed2; 1375 1376 ret = mdev_register_device(&mbochs_dev, &mdev_fops); 1377 if (ret) 1378 goto failed3; 1379 1380 return 0; 1381 1382 failed3: 1383 device_unregister(&mbochs_dev); 1384 failed2: 1385 class_destroy(mbochs_class); 1386 failed1: 1387 cdev_del(&mbochs_cdev); 1388 unregister_chrdev_region(mbochs_devt, MINORMASK); 1389 return ret; 1390 } 1391 1392 static void __exit mbochs_dev_exit(void) 1393 { 1394 mbochs_dev.bus = NULL; 1395 mdev_unregister_device(&mbochs_dev); 1396 1397 device_unregister(&mbochs_dev); 1398 cdev_del(&mbochs_cdev); 1399 unregister_chrdev_region(mbochs_devt, MINORMASK); 1400 class_destroy(mbochs_class); 1401 mbochs_class = NULL; 1402 } 1403 1404 module_init(mbochs_dev_init) 1405 module_exit(mbochs_dev_exit) 1406