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