1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2012, Microsoft Corporation. 4 * 5 * Author: 6 * Haiyang Zhang <haiyangz@microsoft.com> 7 */ 8 9 /* 10 * Hyper-V Synthetic Video Frame Buffer Driver 11 * 12 * This is the driver for the Hyper-V Synthetic Video, which supports 13 * screen resolution up to Full HD 1920x1080 with 32 bit color on Windows 14 * Server 2012, and 1600x1200 with 16 bit color on Windows Server 2008 R2 15 * or earlier. 16 * 17 * It also solves the double mouse cursor issue of the emulated video mode. 18 * 19 * The default screen resolution is 1152x864, which may be changed by a 20 * kernel parameter: 21 * video=hyperv_fb:<width>x<height> 22 * For example: video=hyperv_fb:1280x1024 23 * 24 * Portrait orientation is also supported: 25 * For example: video=hyperv_fb:864x1152 26 */ 27 28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 29 30 #include <linux/module.h> 31 #include <linux/kernel.h> 32 #include <linux/init.h> 33 #include <linux/completion.h> 34 #include <linux/fb.h> 35 #include <linux/pci.h> 36 #include <linux/efi.h> 37 38 #include <linux/hyperv.h> 39 40 41 /* Hyper-V Synthetic Video Protocol definitions and structures */ 42 #define MAX_VMBUS_PKT_SIZE 0x4000 43 44 #define SYNTHVID_VERSION(major, minor) ((minor) << 16 | (major)) 45 #define SYNTHVID_VERSION_WIN7 SYNTHVID_VERSION(3, 0) 46 #define SYNTHVID_VERSION_WIN8 SYNTHVID_VERSION(3, 2) 47 48 #define SYNTHVID_DEPTH_WIN7 16 49 #define SYNTHVID_DEPTH_WIN8 32 50 51 #define SYNTHVID_FB_SIZE_WIN7 (4 * 1024 * 1024) 52 #define SYNTHVID_WIDTH_MAX_WIN7 1600 53 #define SYNTHVID_HEIGHT_MAX_WIN7 1200 54 55 #define SYNTHVID_FB_SIZE_WIN8 (8 * 1024 * 1024) 56 57 #define PCI_VENDOR_ID_MICROSOFT 0x1414 58 #define PCI_DEVICE_ID_HYPERV_VIDEO 0x5353 59 60 61 enum pipe_msg_type { 62 PIPE_MSG_INVALID, 63 PIPE_MSG_DATA, 64 PIPE_MSG_MAX 65 }; 66 67 struct pipe_msg_hdr { 68 u32 type; 69 u32 size; /* size of message after this field */ 70 } __packed; 71 72 73 enum synthvid_msg_type { 74 SYNTHVID_ERROR = 0, 75 SYNTHVID_VERSION_REQUEST = 1, 76 SYNTHVID_VERSION_RESPONSE = 2, 77 SYNTHVID_VRAM_LOCATION = 3, 78 SYNTHVID_VRAM_LOCATION_ACK = 4, 79 SYNTHVID_SITUATION_UPDATE = 5, 80 SYNTHVID_SITUATION_UPDATE_ACK = 6, 81 SYNTHVID_POINTER_POSITION = 7, 82 SYNTHVID_POINTER_SHAPE = 8, 83 SYNTHVID_FEATURE_CHANGE = 9, 84 SYNTHVID_DIRT = 10, 85 86 SYNTHVID_MAX = 11 87 }; 88 89 struct synthvid_msg_hdr { 90 u32 type; 91 u32 size; /* size of this header + payload after this field*/ 92 } __packed; 93 94 95 struct synthvid_version_req { 96 u32 version; 97 } __packed; 98 99 struct synthvid_version_resp { 100 u32 version; 101 u8 is_accepted; 102 u8 max_video_outputs; 103 } __packed; 104 105 struct synthvid_vram_location { 106 u64 user_ctx; 107 u8 is_vram_gpa_specified; 108 u64 vram_gpa; 109 } __packed; 110 111 struct synthvid_vram_location_ack { 112 u64 user_ctx; 113 } __packed; 114 115 struct video_output_situation { 116 u8 active; 117 u32 vram_offset; 118 u8 depth_bits; 119 u32 width_pixels; 120 u32 height_pixels; 121 u32 pitch_bytes; 122 } __packed; 123 124 struct synthvid_situation_update { 125 u64 user_ctx; 126 u8 video_output_count; 127 struct video_output_situation video_output[1]; 128 } __packed; 129 130 struct synthvid_situation_update_ack { 131 u64 user_ctx; 132 } __packed; 133 134 struct synthvid_pointer_position { 135 u8 is_visible; 136 u8 video_output; 137 s32 image_x; 138 s32 image_y; 139 } __packed; 140 141 142 #define CURSOR_MAX_X 96 143 #define CURSOR_MAX_Y 96 144 #define CURSOR_ARGB_PIXEL_SIZE 4 145 #define CURSOR_MAX_SIZE (CURSOR_MAX_X * CURSOR_MAX_Y * CURSOR_ARGB_PIXEL_SIZE) 146 #define CURSOR_COMPLETE (-1) 147 148 struct synthvid_pointer_shape { 149 u8 part_idx; 150 u8 is_argb; 151 u32 width; /* CURSOR_MAX_X at most */ 152 u32 height; /* CURSOR_MAX_Y at most */ 153 u32 hot_x; /* hotspot relative to upper-left of pointer image */ 154 u32 hot_y; 155 u8 data[4]; 156 } __packed; 157 158 struct synthvid_feature_change { 159 u8 is_dirt_needed; 160 u8 is_ptr_pos_needed; 161 u8 is_ptr_shape_needed; 162 u8 is_situ_needed; 163 } __packed; 164 165 struct rect { 166 s32 x1, y1; /* top left corner */ 167 s32 x2, y2; /* bottom right corner, exclusive */ 168 } __packed; 169 170 struct synthvid_dirt { 171 u8 video_output; 172 u8 dirt_count; 173 struct rect rect[1]; 174 } __packed; 175 176 struct synthvid_msg { 177 struct pipe_msg_hdr pipe_hdr; 178 struct synthvid_msg_hdr vid_hdr; 179 union { 180 struct synthvid_version_req ver_req; 181 struct synthvid_version_resp ver_resp; 182 struct synthvid_vram_location vram; 183 struct synthvid_vram_location_ack vram_ack; 184 struct synthvid_situation_update situ; 185 struct synthvid_situation_update_ack situ_ack; 186 struct synthvid_pointer_position ptr_pos; 187 struct synthvid_pointer_shape ptr_shape; 188 struct synthvid_feature_change feature_chg; 189 struct synthvid_dirt dirt; 190 }; 191 } __packed; 192 193 194 195 /* FB driver definitions and structures */ 196 #define HVFB_WIDTH 1152 /* default screen width */ 197 #define HVFB_HEIGHT 864 /* default screen height */ 198 #define HVFB_WIDTH_MIN 640 199 #define HVFB_HEIGHT_MIN 480 200 201 #define RING_BUFSIZE (256 * 1024) 202 #define VSP_TIMEOUT (10 * HZ) 203 #define HVFB_UPDATE_DELAY (HZ / 20) 204 205 struct hvfb_par { 206 struct fb_info *info; 207 struct resource *mem; 208 bool fb_ready; /* fb device is ready */ 209 struct completion wait; 210 u32 synthvid_version; 211 212 struct delayed_work dwork; 213 bool update; 214 215 u32 pseudo_palette[16]; 216 u8 init_buf[MAX_VMBUS_PKT_SIZE]; 217 u8 recv_buf[MAX_VMBUS_PKT_SIZE]; 218 219 /* If true, the VSC notifies the VSP on every framebuffer change */ 220 bool synchronous_fb; 221 222 struct notifier_block hvfb_panic_nb; 223 }; 224 225 static uint screen_width = HVFB_WIDTH; 226 static uint screen_height = HVFB_HEIGHT; 227 static uint screen_depth; 228 static uint screen_fb_size; 229 230 /* Send message to Hyper-V host */ 231 static inline int synthvid_send(struct hv_device *hdev, 232 struct synthvid_msg *msg) 233 { 234 static atomic64_t request_id = ATOMIC64_INIT(0); 235 int ret; 236 237 msg->pipe_hdr.type = PIPE_MSG_DATA; 238 msg->pipe_hdr.size = msg->vid_hdr.size; 239 240 ret = vmbus_sendpacket(hdev->channel, msg, 241 msg->vid_hdr.size + sizeof(struct pipe_msg_hdr), 242 atomic64_inc_return(&request_id), 243 VM_PKT_DATA_INBAND, 0); 244 245 if (ret) 246 pr_err("Unable to send packet via vmbus\n"); 247 248 return ret; 249 } 250 251 252 /* Send screen resolution info to host */ 253 static int synthvid_send_situ(struct hv_device *hdev) 254 { 255 struct fb_info *info = hv_get_drvdata(hdev); 256 struct synthvid_msg msg; 257 258 if (!info) 259 return -ENODEV; 260 261 memset(&msg, 0, sizeof(struct synthvid_msg)); 262 263 msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE; 264 msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) + 265 sizeof(struct synthvid_situation_update); 266 msg.situ.user_ctx = 0; 267 msg.situ.video_output_count = 1; 268 msg.situ.video_output[0].active = 1; 269 msg.situ.video_output[0].vram_offset = 0; 270 msg.situ.video_output[0].depth_bits = info->var.bits_per_pixel; 271 msg.situ.video_output[0].width_pixels = info->var.xres; 272 msg.situ.video_output[0].height_pixels = info->var.yres; 273 msg.situ.video_output[0].pitch_bytes = info->fix.line_length; 274 275 synthvid_send(hdev, &msg); 276 277 return 0; 278 } 279 280 /* Send mouse pointer info to host */ 281 static int synthvid_send_ptr(struct hv_device *hdev) 282 { 283 struct synthvid_msg msg; 284 285 memset(&msg, 0, sizeof(struct synthvid_msg)); 286 msg.vid_hdr.type = SYNTHVID_POINTER_POSITION; 287 msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) + 288 sizeof(struct synthvid_pointer_position); 289 msg.ptr_pos.is_visible = 1; 290 msg.ptr_pos.video_output = 0; 291 msg.ptr_pos.image_x = 0; 292 msg.ptr_pos.image_y = 0; 293 synthvid_send(hdev, &msg); 294 295 memset(&msg, 0, sizeof(struct synthvid_msg)); 296 msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE; 297 msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) + 298 sizeof(struct synthvid_pointer_shape); 299 msg.ptr_shape.part_idx = CURSOR_COMPLETE; 300 msg.ptr_shape.is_argb = 1; 301 msg.ptr_shape.width = 1; 302 msg.ptr_shape.height = 1; 303 msg.ptr_shape.hot_x = 0; 304 msg.ptr_shape.hot_y = 0; 305 msg.ptr_shape.data[0] = 0; 306 msg.ptr_shape.data[1] = 1; 307 msg.ptr_shape.data[2] = 1; 308 msg.ptr_shape.data[3] = 1; 309 synthvid_send(hdev, &msg); 310 311 return 0; 312 } 313 314 /* Send updated screen area (dirty rectangle) location to host */ 315 static int synthvid_update(struct fb_info *info) 316 { 317 struct hv_device *hdev = device_to_hv_device(info->device); 318 struct synthvid_msg msg; 319 320 memset(&msg, 0, sizeof(struct synthvid_msg)); 321 322 msg.vid_hdr.type = SYNTHVID_DIRT; 323 msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) + 324 sizeof(struct synthvid_dirt); 325 msg.dirt.video_output = 0; 326 msg.dirt.dirt_count = 1; 327 msg.dirt.rect[0].x1 = 0; 328 msg.dirt.rect[0].y1 = 0; 329 msg.dirt.rect[0].x2 = info->var.xres; 330 msg.dirt.rect[0].y2 = info->var.yres; 331 332 synthvid_send(hdev, &msg); 333 334 return 0; 335 } 336 337 338 /* 339 * Actions on received messages from host: 340 * Complete the wait event. 341 * Or, reply with screen and cursor info. 342 */ 343 static void synthvid_recv_sub(struct hv_device *hdev) 344 { 345 struct fb_info *info = hv_get_drvdata(hdev); 346 struct hvfb_par *par; 347 struct synthvid_msg *msg; 348 349 if (!info) 350 return; 351 352 par = info->par; 353 msg = (struct synthvid_msg *)par->recv_buf; 354 355 /* Complete the wait event */ 356 if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE || 357 msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) { 358 memcpy(par->init_buf, msg, MAX_VMBUS_PKT_SIZE); 359 complete(&par->wait); 360 return; 361 } 362 363 /* Reply with screen and cursor info */ 364 if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) { 365 if (par->fb_ready) { 366 synthvid_send_ptr(hdev); 367 synthvid_send_situ(hdev); 368 } 369 370 par->update = msg->feature_chg.is_dirt_needed; 371 if (par->update) 372 schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY); 373 } 374 } 375 376 /* Receive callback for messages from the host */ 377 static void synthvid_receive(void *ctx) 378 { 379 struct hv_device *hdev = ctx; 380 struct fb_info *info = hv_get_drvdata(hdev); 381 struct hvfb_par *par; 382 struct synthvid_msg *recv_buf; 383 u32 bytes_recvd; 384 u64 req_id; 385 int ret; 386 387 if (!info) 388 return; 389 390 par = info->par; 391 recv_buf = (struct synthvid_msg *)par->recv_buf; 392 393 do { 394 ret = vmbus_recvpacket(hdev->channel, recv_buf, 395 MAX_VMBUS_PKT_SIZE, 396 &bytes_recvd, &req_id); 397 if (bytes_recvd > 0 && 398 recv_buf->pipe_hdr.type == PIPE_MSG_DATA) 399 synthvid_recv_sub(hdev); 400 } while (bytes_recvd > 0 && ret == 0); 401 } 402 403 /* Check synthetic video protocol version with the host */ 404 static int synthvid_negotiate_ver(struct hv_device *hdev, u32 ver) 405 { 406 struct fb_info *info = hv_get_drvdata(hdev); 407 struct hvfb_par *par = info->par; 408 struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf; 409 int ret = 0; 410 unsigned long t; 411 412 memset(msg, 0, sizeof(struct synthvid_msg)); 413 msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST; 414 msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) + 415 sizeof(struct synthvid_version_req); 416 msg->ver_req.version = ver; 417 synthvid_send(hdev, msg); 418 419 t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT); 420 if (!t) { 421 pr_err("Time out on waiting version response\n"); 422 ret = -ETIMEDOUT; 423 goto out; 424 } 425 if (!msg->ver_resp.is_accepted) { 426 ret = -ENODEV; 427 goto out; 428 } 429 430 par->synthvid_version = ver; 431 432 out: 433 return ret; 434 } 435 436 /* Connect to VSP (Virtual Service Provider) on host */ 437 static int synthvid_connect_vsp(struct hv_device *hdev) 438 { 439 struct fb_info *info = hv_get_drvdata(hdev); 440 struct hvfb_par *par = info->par; 441 int ret; 442 443 ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE, 444 NULL, 0, synthvid_receive, hdev); 445 if (ret) { 446 pr_err("Unable to open vmbus channel\n"); 447 return ret; 448 } 449 450 /* Negotiate the protocol version with host */ 451 if (vmbus_proto_version == VERSION_WS2008 || 452 vmbus_proto_version == VERSION_WIN7) 453 ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN7); 454 else 455 ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8); 456 457 if (ret) { 458 pr_err("Synthetic video device version not accepted\n"); 459 goto error; 460 } 461 462 if (par->synthvid_version == SYNTHVID_VERSION_WIN7) 463 screen_depth = SYNTHVID_DEPTH_WIN7; 464 else 465 screen_depth = SYNTHVID_DEPTH_WIN8; 466 467 screen_fb_size = hdev->channel->offermsg.offer. 468 mmio_megabytes * 1024 * 1024; 469 470 return 0; 471 472 error: 473 vmbus_close(hdev->channel); 474 return ret; 475 } 476 477 /* Send VRAM and Situation messages to the host */ 478 static int synthvid_send_config(struct hv_device *hdev) 479 { 480 struct fb_info *info = hv_get_drvdata(hdev); 481 struct hvfb_par *par = info->par; 482 struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf; 483 int ret = 0; 484 unsigned long t; 485 486 /* Send VRAM location */ 487 memset(msg, 0, sizeof(struct synthvid_msg)); 488 msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION; 489 msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) + 490 sizeof(struct synthvid_vram_location); 491 msg->vram.user_ctx = msg->vram.vram_gpa = info->fix.smem_start; 492 msg->vram.is_vram_gpa_specified = 1; 493 synthvid_send(hdev, msg); 494 495 t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT); 496 if (!t) { 497 pr_err("Time out on waiting vram location ack\n"); 498 ret = -ETIMEDOUT; 499 goto out; 500 } 501 if (msg->vram_ack.user_ctx != info->fix.smem_start) { 502 pr_err("Unable to set VRAM location\n"); 503 ret = -ENODEV; 504 goto out; 505 } 506 507 /* Send pointer and situation update */ 508 synthvid_send_ptr(hdev); 509 synthvid_send_situ(hdev); 510 511 out: 512 return ret; 513 } 514 515 516 /* 517 * Delayed work callback: 518 * It is called at HVFB_UPDATE_DELAY or longer time interval to process 519 * screen updates. It is re-scheduled if further update is necessary. 520 */ 521 static void hvfb_update_work(struct work_struct *w) 522 { 523 struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work); 524 struct fb_info *info = par->info; 525 526 if (par->fb_ready) 527 synthvid_update(info); 528 529 if (par->update) 530 schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY); 531 } 532 533 static int hvfb_on_panic(struct notifier_block *nb, 534 unsigned long e, void *p) 535 { 536 struct hvfb_par *par; 537 struct fb_info *info; 538 539 par = container_of(nb, struct hvfb_par, hvfb_panic_nb); 540 par->synchronous_fb = true; 541 info = par->info; 542 synthvid_update(info); 543 544 return NOTIFY_DONE; 545 } 546 547 /* Framebuffer operation handlers */ 548 549 static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info) 550 { 551 if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN || 552 var->xres > screen_width || var->yres > screen_height || 553 var->bits_per_pixel != screen_depth) 554 return -EINVAL; 555 556 var->xres_virtual = var->xres; 557 var->yres_virtual = var->yres; 558 559 return 0; 560 } 561 562 static int hvfb_set_par(struct fb_info *info) 563 { 564 struct hv_device *hdev = device_to_hv_device(info->device); 565 566 return synthvid_send_situ(hdev); 567 } 568 569 570 static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf) 571 { 572 return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset; 573 } 574 575 static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green, 576 unsigned blue, unsigned transp, struct fb_info *info) 577 { 578 u32 *pal = info->pseudo_palette; 579 580 if (regno > 15) 581 return -EINVAL; 582 583 pal[regno] = chan_to_field(red, &info->var.red) 584 | chan_to_field(green, &info->var.green) 585 | chan_to_field(blue, &info->var.blue) 586 | chan_to_field(transp, &info->var.transp); 587 588 return 0; 589 } 590 591 static int hvfb_blank(int blank, struct fb_info *info) 592 { 593 return 1; /* get fb_blank to set the colormap to all black */ 594 } 595 596 static void hvfb_cfb_fillrect(struct fb_info *p, 597 const struct fb_fillrect *rect) 598 { 599 struct hvfb_par *par = p->par; 600 601 cfb_fillrect(p, rect); 602 if (par->synchronous_fb) 603 synthvid_update(p); 604 } 605 606 static void hvfb_cfb_copyarea(struct fb_info *p, 607 const struct fb_copyarea *area) 608 { 609 struct hvfb_par *par = p->par; 610 611 cfb_copyarea(p, area); 612 if (par->synchronous_fb) 613 synthvid_update(p); 614 } 615 616 static void hvfb_cfb_imageblit(struct fb_info *p, 617 const struct fb_image *image) 618 { 619 struct hvfb_par *par = p->par; 620 621 cfb_imageblit(p, image); 622 if (par->synchronous_fb) 623 synthvid_update(p); 624 } 625 626 static struct fb_ops hvfb_ops = { 627 .owner = THIS_MODULE, 628 .fb_check_var = hvfb_check_var, 629 .fb_set_par = hvfb_set_par, 630 .fb_setcolreg = hvfb_setcolreg, 631 .fb_fillrect = hvfb_cfb_fillrect, 632 .fb_copyarea = hvfb_cfb_copyarea, 633 .fb_imageblit = hvfb_cfb_imageblit, 634 .fb_blank = hvfb_blank, 635 }; 636 637 638 /* Get options from kernel paramenter "video=" */ 639 static void hvfb_get_option(struct fb_info *info) 640 { 641 struct hvfb_par *par = info->par; 642 char *opt = NULL, *p; 643 uint x = 0, y = 0; 644 645 if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt) 646 return; 647 648 p = strsep(&opt, "x"); 649 if (!*p || kstrtouint(p, 0, &x) || 650 !opt || !*opt || kstrtouint(opt, 0, &y)) { 651 pr_err("Screen option is invalid: skipped\n"); 652 return; 653 } 654 655 if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN || 656 (par->synthvid_version == SYNTHVID_VERSION_WIN8 && 657 x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8) || 658 (par->synthvid_version == SYNTHVID_VERSION_WIN7 && 659 (x > SYNTHVID_WIDTH_MAX_WIN7 || y > SYNTHVID_HEIGHT_MAX_WIN7))) { 660 pr_err("Screen resolution option is out of range: skipped\n"); 661 return; 662 } 663 664 screen_width = x; 665 screen_height = y; 666 return; 667 } 668 669 670 /* Get framebuffer memory from Hyper-V video pci space */ 671 static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info) 672 { 673 struct hvfb_par *par = info->par; 674 struct pci_dev *pdev = NULL; 675 void __iomem *fb_virt; 676 int gen2vm = efi_enabled(EFI_BOOT); 677 resource_size_t pot_start, pot_end; 678 int ret; 679 680 if (gen2vm) { 681 pot_start = 0; 682 pot_end = -1; 683 } else { 684 pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT, 685 PCI_DEVICE_ID_HYPERV_VIDEO, NULL); 686 if (!pdev) { 687 pr_err("Unable to find PCI Hyper-V video\n"); 688 return -ENODEV; 689 } 690 691 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) || 692 pci_resource_len(pdev, 0) < screen_fb_size) 693 goto err1; 694 695 pot_end = pci_resource_end(pdev, 0); 696 pot_start = pot_end - screen_fb_size + 1; 697 } 698 699 ret = vmbus_allocate_mmio(&par->mem, hdev, pot_start, pot_end, 700 screen_fb_size, 0x100000, true); 701 if (ret != 0) { 702 pr_err("Unable to allocate framebuffer memory\n"); 703 goto err1; 704 } 705 706 fb_virt = ioremap(par->mem->start, screen_fb_size); 707 if (!fb_virt) 708 goto err2; 709 710 info->apertures = alloc_apertures(1); 711 if (!info->apertures) 712 goto err3; 713 714 if (gen2vm) { 715 info->apertures->ranges[0].base = screen_info.lfb_base; 716 info->apertures->ranges[0].size = screen_info.lfb_size; 717 remove_conflicting_framebuffers(info->apertures, 718 KBUILD_MODNAME, false); 719 } else { 720 info->apertures->ranges[0].base = pci_resource_start(pdev, 0); 721 info->apertures->ranges[0].size = pci_resource_len(pdev, 0); 722 } 723 724 info->fix.smem_start = par->mem->start; 725 info->fix.smem_len = screen_fb_size; 726 info->screen_base = fb_virt; 727 info->screen_size = screen_fb_size; 728 729 if (!gen2vm) 730 pci_dev_put(pdev); 731 732 return 0; 733 734 err3: 735 iounmap(fb_virt); 736 err2: 737 vmbus_free_mmio(par->mem->start, screen_fb_size); 738 par->mem = NULL; 739 err1: 740 if (!gen2vm) 741 pci_dev_put(pdev); 742 743 return -ENOMEM; 744 } 745 746 /* Release the framebuffer */ 747 static void hvfb_putmem(struct fb_info *info) 748 { 749 struct hvfb_par *par = info->par; 750 751 iounmap(info->screen_base); 752 vmbus_free_mmio(par->mem->start, screen_fb_size); 753 par->mem = NULL; 754 } 755 756 757 static int hvfb_probe(struct hv_device *hdev, 758 const struct hv_vmbus_device_id *dev_id) 759 { 760 struct fb_info *info; 761 struct hvfb_par *par; 762 int ret; 763 764 info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device); 765 if (!info) { 766 pr_err("No memory for framebuffer info\n"); 767 return -ENOMEM; 768 } 769 770 par = info->par; 771 par->info = info; 772 par->fb_ready = false; 773 init_completion(&par->wait); 774 INIT_DELAYED_WORK(&par->dwork, hvfb_update_work); 775 776 /* Connect to VSP */ 777 hv_set_drvdata(hdev, info); 778 ret = synthvid_connect_vsp(hdev); 779 if (ret) { 780 pr_err("Unable to connect to VSP\n"); 781 goto error1; 782 } 783 784 ret = hvfb_getmem(hdev, info); 785 if (ret) { 786 pr_err("No memory for framebuffer\n"); 787 goto error2; 788 } 789 790 hvfb_get_option(info); 791 pr_info("Screen resolution: %dx%d, Color depth: %d\n", 792 screen_width, screen_height, screen_depth); 793 794 795 /* Set up fb_info */ 796 info->flags = FBINFO_DEFAULT; 797 798 info->var.xres_virtual = info->var.xres = screen_width; 799 info->var.yres_virtual = info->var.yres = screen_height; 800 info->var.bits_per_pixel = screen_depth; 801 802 if (info->var.bits_per_pixel == 16) { 803 info->var.red = (struct fb_bitfield){11, 5, 0}; 804 info->var.green = (struct fb_bitfield){5, 6, 0}; 805 info->var.blue = (struct fb_bitfield){0, 5, 0}; 806 info->var.transp = (struct fb_bitfield){0, 0, 0}; 807 } else { 808 info->var.red = (struct fb_bitfield){16, 8, 0}; 809 info->var.green = (struct fb_bitfield){8, 8, 0}; 810 info->var.blue = (struct fb_bitfield){0, 8, 0}; 811 info->var.transp = (struct fb_bitfield){24, 8, 0}; 812 } 813 814 info->var.activate = FB_ACTIVATE_NOW; 815 info->var.height = -1; 816 info->var.width = -1; 817 info->var.vmode = FB_VMODE_NONINTERLACED; 818 819 strcpy(info->fix.id, KBUILD_MODNAME); 820 info->fix.type = FB_TYPE_PACKED_PIXELS; 821 info->fix.visual = FB_VISUAL_TRUECOLOR; 822 info->fix.line_length = screen_width * screen_depth / 8; 823 info->fix.accel = FB_ACCEL_NONE; 824 825 info->fbops = &hvfb_ops; 826 info->pseudo_palette = par->pseudo_palette; 827 828 /* Send config to host */ 829 ret = synthvid_send_config(hdev); 830 if (ret) 831 goto error; 832 833 ret = register_framebuffer(info); 834 if (ret) { 835 pr_err("Unable to register framebuffer\n"); 836 goto error; 837 } 838 839 par->fb_ready = true; 840 841 par->synchronous_fb = false; 842 par->hvfb_panic_nb.notifier_call = hvfb_on_panic; 843 atomic_notifier_chain_register(&panic_notifier_list, 844 &par->hvfb_panic_nb); 845 846 return 0; 847 848 error: 849 hvfb_putmem(info); 850 error2: 851 vmbus_close(hdev->channel); 852 error1: 853 cancel_delayed_work_sync(&par->dwork); 854 hv_set_drvdata(hdev, NULL); 855 framebuffer_release(info); 856 return ret; 857 } 858 859 860 static int hvfb_remove(struct hv_device *hdev) 861 { 862 struct fb_info *info = hv_get_drvdata(hdev); 863 struct hvfb_par *par = info->par; 864 865 atomic_notifier_chain_unregister(&panic_notifier_list, 866 &par->hvfb_panic_nb); 867 868 par->update = false; 869 par->fb_ready = false; 870 871 unregister_framebuffer(info); 872 cancel_delayed_work_sync(&par->dwork); 873 874 vmbus_close(hdev->channel); 875 hv_set_drvdata(hdev, NULL); 876 877 hvfb_putmem(info); 878 framebuffer_release(info); 879 880 return 0; 881 } 882 883 884 static const struct pci_device_id pci_stub_id_table[] = { 885 { 886 .vendor = PCI_VENDOR_ID_MICROSOFT, 887 .device = PCI_DEVICE_ID_HYPERV_VIDEO, 888 }, 889 { /* end of list */ } 890 }; 891 892 static const struct hv_vmbus_device_id id_table[] = { 893 /* Synthetic Video Device GUID */ 894 {HV_SYNTHVID_GUID}, 895 {} 896 }; 897 898 MODULE_DEVICE_TABLE(pci, pci_stub_id_table); 899 MODULE_DEVICE_TABLE(vmbus, id_table); 900 901 static struct hv_driver hvfb_drv = { 902 .name = KBUILD_MODNAME, 903 .id_table = id_table, 904 .probe = hvfb_probe, 905 .remove = hvfb_remove, 906 .driver = { 907 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 908 }, 909 }; 910 911 static int hvfb_pci_stub_probe(struct pci_dev *pdev, 912 const struct pci_device_id *ent) 913 { 914 return 0; 915 } 916 917 static void hvfb_pci_stub_remove(struct pci_dev *pdev) 918 { 919 } 920 921 static struct pci_driver hvfb_pci_stub_driver = { 922 .name = KBUILD_MODNAME, 923 .id_table = pci_stub_id_table, 924 .probe = hvfb_pci_stub_probe, 925 .remove = hvfb_pci_stub_remove, 926 .driver = { 927 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 928 } 929 }; 930 931 static int __init hvfb_drv_init(void) 932 { 933 int ret; 934 935 ret = vmbus_driver_register(&hvfb_drv); 936 if (ret != 0) 937 return ret; 938 939 ret = pci_register_driver(&hvfb_pci_stub_driver); 940 if (ret != 0) { 941 vmbus_driver_unregister(&hvfb_drv); 942 return ret; 943 } 944 945 return 0; 946 } 947 948 static void __exit hvfb_drv_exit(void) 949 { 950 pci_unregister_driver(&hvfb_pci_stub_driver); 951 vmbus_driver_unregister(&hvfb_drv); 952 } 953 954 module_init(hvfb_drv_init); 955 module_exit(hvfb_drv_exit); 956 957 MODULE_LICENSE("GPL"); 958 MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver"); 959