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