1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012, Microsoft Corporation.
6 * Haiyang Zhang <haiyangz@microsoft.com>
10 * Hyper-V Synthetic Video Frame Buffer Driver
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
17 * It also solves the double mouse cursor issue of the emulated video mode.
19 * The default screen resolution is 1152x864, which may be changed by a
21 * video=hyperv_fb:<width>x<height>
22 * For example: video=hyperv_fb:1280x1024
24 * Portrait orientation is also supported:
25 * For example: video=hyperv_fb:864x1152
27 * When a Windows 10 RS5+ host is used, the virtual machine screen
28 * resolution is obtained from the host. The "video=hyperv_fb" option is
29 * not needed, but still can be used to overwrite what the host specifies.
30 * The VM resolution on the host could be set by executing the powershell
31 * "set-vmvideo" command. For example
32 * set-vmvideo -vmname name -horizontalresolution:1920 \
33 * -verticalresolution:1200 -resolutiontype single
35 * Gen 1 VMs also support direct using VM's physical memory for framebuffer.
36 * It could improve the efficiency and performance for framebuffer and VM.
37 * This requires to allocate contiguous physical memory from Linux kernel's
38 * CMA memory allocator. To enable this, supply a kernel parameter to give
39 * enough memory space to CMA allocator for framebuffer. For example:
41 * This gives 130MB memory to CMA allocator that can be allocated to
42 * framebuffer. For reference, 8K resolution (7680x4320) takes about
46 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
48 #include <linux/module.h>
49 #include <linux/kernel.h>
50 #include <linux/vmalloc.h>
51 #include <linux/init.h>
52 #include <linux/completion.h>
54 #include <linux/pci.h>
55 #include <linux/panic_notifier.h>
56 #include <linux/efi.h>
57 #include <linux/console.h>
59 #include <linux/hyperv.h>
62 /* Hyper-V Synthetic Video Protocol definitions and structures */
63 #define MAX_VMBUS_PKT_SIZE 0x4000
65 #define SYNTHVID_VERSION(major, minor) ((minor) << 16 | (major))
66 #define SYNTHVID_VERSION_WIN7 SYNTHVID_VERSION(3, 0)
67 #define SYNTHVID_VERSION_WIN8 SYNTHVID_VERSION(3, 2)
68 #define SYNTHVID_VERSION_WIN10 SYNTHVID_VERSION(3, 5)
70 #define SYNTHVID_VER_GET_MAJOR(ver) (ver & 0x0000ffff)
71 #define SYNTHVID_VER_GET_MINOR(ver) ((ver & 0xffff0000) >> 16)
73 #define SYNTHVID_DEPTH_WIN7 16
74 #define SYNTHVID_DEPTH_WIN8 32
76 #define SYNTHVID_FB_SIZE_WIN7 (4 * 1024 * 1024)
77 #define SYNTHVID_WIDTH_MAX_WIN7 1600
78 #define SYNTHVID_HEIGHT_MAX_WIN7 1200
80 #define SYNTHVID_FB_SIZE_WIN8 (8 * 1024 * 1024)
82 #define PCI_VENDOR_ID_MICROSOFT 0x1414
83 #define PCI_DEVICE_ID_HYPERV_VIDEO 0x5353
94 u32 size; /* size of message after this field */
98 enum synthvid_msg_type {
100 SYNTHVID_VERSION_REQUEST = 1,
101 SYNTHVID_VERSION_RESPONSE = 2,
102 SYNTHVID_VRAM_LOCATION = 3,
103 SYNTHVID_VRAM_LOCATION_ACK = 4,
104 SYNTHVID_SITUATION_UPDATE = 5,
105 SYNTHVID_SITUATION_UPDATE_ACK = 6,
106 SYNTHVID_POINTER_POSITION = 7,
107 SYNTHVID_POINTER_SHAPE = 8,
108 SYNTHVID_FEATURE_CHANGE = 9,
110 SYNTHVID_RESOLUTION_REQUEST = 13,
111 SYNTHVID_RESOLUTION_RESPONSE = 14,
116 #define SYNTHVID_EDID_BLOCK_SIZE 128
117 #define SYNTHVID_MAX_RESOLUTION_COUNT 64
119 struct hvd_screen_info {
124 struct synthvid_msg_hdr {
126 u32 size; /* size of this header + payload after this field*/
129 struct synthvid_version_req {
133 struct synthvid_version_resp {
136 u8 max_video_outputs;
139 struct synthvid_supported_resolution_req {
140 u8 maximum_resolution_count;
143 struct synthvid_supported_resolution_resp {
144 u8 edid_block[SYNTHVID_EDID_BLOCK_SIZE];
146 u8 default_resolution_index;
148 struct hvd_screen_info
149 supported_resolution[SYNTHVID_MAX_RESOLUTION_COUNT];
152 struct synthvid_vram_location {
154 u8 is_vram_gpa_specified;
158 struct synthvid_vram_location_ack {
162 struct video_output_situation {
171 struct synthvid_situation_update {
173 u8 video_output_count;
174 struct video_output_situation video_output[1];
177 struct synthvid_situation_update_ack {
181 struct synthvid_pointer_position {
189 #define CURSOR_MAX_X 96
190 #define CURSOR_MAX_Y 96
191 #define CURSOR_ARGB_PIXEL_SIZE 4
192 #define CURSOR_MAX_SIZE (CURSOR_MAX_X * CURSOR_MAX_Y * CURSOR_ARGB_PIXEL_SIZE)
193 #define CURSOR_COMPLETE (-1)
195 struct synthvid_pointer_shape {
198 u32 width; /* CURSOR_MAX_X at most */
199 u32 height; /* CURSOR_MAX_Y at most */
200 u32 hot_x; /* hotspot relative to upper-left of pointer image */
205 struct synthvid_feature_change {
207 u8 is_ptr_pos_needed;
208 u8 is_ptr_shape_needed;
213 s32 x1, y1; /* top left corner */
214 s32 x2, y2; /* bottom right corner, exclusive */
217 struct synthvid_dirt {
223 struct synthvid_msg {
224 struct pipe_msg_hdr pipe_hdr;
225 struct synthvid_msg_hdr vid_hdr;
227 struct synthvid_version_req ver_req;
228 struct synthvid_version_resp ver_resp;
229 struct synthvid_vram_location vram;
230 struct synthvid_vram_location_ack vram_ack;
231 struct synthvid_situation_update situ;
232 struct synthvid_situation_update_ack situ_ack;
233 struct synthvid_pointer_position ptr_pos;
234 struct synthvid_pointer_shape ptr_shape;
235 struct synthvid_feature_change feature_chg;
236 struct synthvid_dirt dirt;
237 struct synthvid_supported_resolution_req resolution_req;
238 struct synthvid_supported_resolution_resp resolution_resp;
243 /* FB driver definitions and structures */
244 #define HVFB_WIDTH 1152 /* default screen width */
245 #define HVFB_HEIGHT 864 /* default screen height */
246 #define HVFB_WIDTH_MIN 640
247 #define HVFB_HEIGHT_MIN 480
249 #define RING_BUFSIZE (256 * 1024)
250 #define VSP_TIMEOUT (10 * HZ)
251 #define HVFB_UPDATE_DELAY (HZ / 20)
252 #define HVFB_ONDEMAND_THROTTLE (HZ / 20)
255 struct fb_info *info;
256 struct resource *mem;
257 bool fb_ready; /* fb device is ready */
258 struct completion wait;
259 u32 synthvid_version;
261 struct delayed_work dwork;
263 bool update_saved; /* The value of 'update' before hibernation */
265 u32 pseudo_palette[16];
266 u8 init_buf[MAX_VMBUS_PKT_SIZE];
267 u8 recv_buf[MAX_VMBUS_PKT_SIZE];
269 /* If true, the VSC notifies the VSP on every framebuffer change */
272 /* If true, need to copy from deferred IO mem to framebuffer mem */
275 struct notifier_block hvfb_panic_nb;
277 /* Memory for deferred IO and frame buffer itself */
278 unsigned char *dio_vp;
279 unsigned char *mmio_vp;
282 /* Dirty rectangle, protected by delayed_refresh_lock */
284 bool delayed_refresh;
285 spinlock_t delayed_refresh_lock;
288 static uint screen_width = HVFB_WIDTH;
289 static uint screen_height = HVFB_HEIGHT;
290 static uint screen_depth;
291 static uint screen_fb_size;
292 static uint dio_fb_size; /* FB size for deferred IO */
294 /* Send message to Hyper-V host */
295 static inline int synthvid_send(struct hv_device *hdev,
296 struct synthvid_msg *msg)
298 static atomic64_t request_id = ATOMIC64_INIT(0);
301 msg->pipe_hdr.type = PIPE_MSG_DATA;
302 msg->pipe_hdr.size = msg->vid_hdr.size;
304 ret = vmbus_sendpacket(hdev->channel, msg,
305 msg->vid_hdr.size + sizeof(struct pipe_msg_hdr),
306 atomic64_inc_return(&request_id),
307 VM_PKT_DATA_INBAND, 0);
310 pr_err_ratelimited("Unable to send packet via vmbus; error %d\n", ret);
316 /* Send screen resolution info to host */
317 static int synthvid_send_situ(struct hv_device *hdev)
319 struct fb_info *info = hv_get_drvdata(hdev);
320 struct synthvid_msg msg;
325 memset(&msg, 0, sizeof(struct synthvid_msg));
327 msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE;
328 msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
329 sizeof(struct synthvid_situation_update);
330 msg.situ.user_ctx = 0;
331 msg.situ.video_output_count = 1;
332 msg.situ.video_output[0].active = 1;
333 msg.situ.video_output[0].vram_offset = 0;
334 msg.situ.video_output[0].depth_bits = info->var.bits_per_pixel;
335 msg.situ.video_output[0].width_pixels = info->var.xres;
336 msg.situ.video_output[0].height_pixels = info->var.yres;
337 msg.situ.video_output[0].pitch_bytes = info->fix.line_length;
339 synthvid_send(hdev, &msg);
344 /* Send mouse pointer info to host */
345 static int synthvid_send_ptr(struct hv_device *hdev)
347 struct synthvid_msg msg;
349 memset(&msg, 0, sizeof(struct synthvid_msg));
350 msg.vid_hdr.type = SYNTHVID_POINTER_POSITION;
351 msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
352 sizeof(struct synthvid_pointer_position);
353 msg.ptr_pos.is_visible = 1;
354 msg.ptr_pos.video_output = 0;
355 msg.ptr_pos.image_x = 0;
356 msg.ptr_pos.image_y = 0;
357 synthvid_send(hdev, &msg);
359 memset(&msg, 0, sizeof(struct synthvid_msg));
360 msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE;
361 msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
362 sizeof(struct synthvid_pointer_shape);
363 msg.ptr_shape.part_idx = CURSOR_COMPLETE;
364 msg.ptr_shape.is_argb = 1;
365 msg.ptr_shape.width = 1;
366 msg.ptr_shape.height = 1;
367 msg.ptr_shape.hot_x = 0;
368 msg.ptr_shape.hot_y = 0;
369 msg.ptr_shape.data[0] = 0;
370 msg.ptr_shape.data[1] = 1;
371 msg.ptr_shape.data[2] = 1;
372 msg.ptr_shape.data[3] = 1;
373 synthvid_send(hdev, &msg);
378 /* Send updated screen area (dirty rectangle) location to host */
380 synthvid_update(struct fb_info *info, int x1, int y1, int x2, int y2)
382 struct hv_device *hdev = device_to_hv_device(info->device);
383 struct synthvid_msg msg;
385 memset(&msg, 0, sizeof(struct synthvid_msg));
391 msg.vid_hdr.type = SYNTHVID_DIRT;
392 msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
393 sizeof(struct synthvid_dirt);
394 msg.dirt.video_output = 0;
395 msg.dirt.dirt_count = 1;
396 msg.dirt.rect[0].x1 = (x1 > x2) ? 0 : x1;
397 msg.dirt.rect[0].y1 = (y1 > y2) ? 0 : y1;
398 msg.dirt.rect[0].x2 =
399 (x2 < x1 || x2 > info->var.xres) ? info->var.xres : x2;
400 msg.dirt.rect[0].y2 =
401 (y2 < y1 || y2 > info->var.yres) ? info->var.yres : y2;
403 synthvid_send(hdev, &msg);
408 static void hvfb_docopy(struct hvfb_par *par,
409 unsigned long offset,
412 if (!par || !par->mmio_vp || !par->dio_vp || !par->fb_ready ||
413 size == 0 || offset >= dio_fb_size)
416 if (offset + size > dio_fb_size)
417 size = dio_fb_size - offset;
419 memcpy(par->mmio_vp + offset, par->dio_vp + offset, size);
422 /* Deferred IO callback */
423 static void synthvid_deferred_io(struct fb_info *p,
424 struct list_head *pagelist)
426 struct hvfb_par *par = p->par;
428 unsigned long start, end;
429 int y1, y2, miny, maxy;
435 * Merge dirty pages. It is possible that last page cross
436 * over the end of frame buffer row yres. This is taken care of
437 * in synthvid_update function by clamping the y2
440 list_for_each_entry(page, pagelist, lru) {
441 start = page->index << PAGE_SHIFT;
442 end = start + PAGE_SIZE - 1;
443 y1 = start / p->fix.line_length;
444 y2 = end / p->fix.line_length;
445 miny = min_t(int, miny, y1);
446 maxy = max_t(int, maxy, y2);
448 /* Copy from dio space to mmio address */
449 if (par->fb_ready && par->need_docopy)
450 hvfb_docopy(par, start, PAGE_SIZE);
453 if (par->fb_ready && par->update)
454 synthvid_update(p, 0, miny, p->var.xres, maxy + 1);
457 static struct fb_deferred_io synthvid_defio = {
459 .deferred_io = synthvid_deferred_io,
463 * Actions on received messages from host:
464 * Complete the wait event.
465 * Or, reply with screen and cursor info.
467 static void synthvid_recv_sub(struct hv_device *hdev)
469 struct fb_info *info = hv_get_drvdata(hdev);
470 struct hvfb_par *par;
471 struct synthvid_msg *msg;
477 msg = (struct synthvid_msg *)par->recv_buf;
479 /* Complete the wait event */
480 if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE ||
481 msg->vid_hdr.type == SYNTHVID_RESOLUTION_RESPONSE ||
482 msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) {
483 memcpy(par->init_buf, msg, MAX_VMBUS_PKT_SIZE);
484 complete(&par->wait);
488 /* Reply with screen and cursor info */
489 if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) {
491 synthvid_send_ptr(hdev);
492 synthvid_send_situ(hdev);
495 par->update = msg->feature_chg.is_dirt_needed;
497 schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
501 /* Receive callback for messages from the host */
502 static void synthvid_receive(void *ctx)
504 struct hv_device *hdev = ctx;
505 struct fb_info *info = hv_get_drvdata(hdev);
506 struct hvfb_par *par;
507 struct synthvid_msg *recv_buf;
516 recv_buf = (struct synthvid_msg *)par->recv_buf;
519 ret = vmbus_recvpacket(hdev->channel, recv_buf,
521 &bytes_recvd, &req_id);
522 if (bytes_recvd > 0 &&
523 recv_buf->pipe_hdr.type == PIPE_MSG_DATA)
524 synthvid_recv_sub(hdev);
525 } while (bytes_recvd > 0 && ret == 0);
528 /* Check if the ver1 version is equal or greater than ver2 */
529 static inline bool synthvid_ver_ge(u32 ver1, u32 ver2)
531 if (SYNTHVID_VER_GET_MAJOR(ver1) > SYNTHVID_VER_GET_MAJOR(ver2) ||
532 (SYNTHVID_VER_GET_MAJOR(ver1) == SYNTHVID_VER_GET_MAJOR(ver2) &&
533 SYNTHVID_VER_GET_MINOR(ver1) >= SYNTHVID_VER_GET_MINOR(ver2)))
539 /* Check synthetic video protocol version with the host */
540 static int synthvid_negotiate_ver(struct hv_device *hdev, u32 ver)
542 struct fb_info *info = hv_get_drvdata(hdev);
543 struct hvfb_par *par = info->par;
544 struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
548 memset(msg, 0, sizeof(struct synthvid_msg));
549 msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST;
550 msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
551 sizeof(struct synthvid_version_req);
552 msg->ver_req.version = ver;
553 synthvid_send(hdev, msg);
555 t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
557 pr_err("Time out on waiting version response\n");
561 if (!msg->ver_resp.is_accepted) {
566 par->synthvid_version = ver;
567 pr_info("Synthvid Version major %d, minor %d\n",
568 SYNTHVID_VER_GET_MAJOR(ver), SYNTHVID_VER_GET_MINOR(ver));
574 /* Get current resolution from the host */
575 static int synthvid_get_supported_resolution(struct hv_device *hdev)
577 struct fb_info *info = hv_get_drvdata(hdev);
578 struct hvfb_par *par = info->par;
579 struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
584 memset(msg, 0, sizeof(struct synthvid_msg));
585 msg->vid_hdr.type = SYNTHVID_RESOLUTION_REQUEST;
586 msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
587 sizeof(struct synthvid_supported_resolution_req);
589 msg->resolution_req.maximum_resolution_count =
590 SYNTHVID_MAX_RESOLUTION_COUNT;
591 synthvid_send(hdev, msg);
593 t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
595 pr_err("Time out on waiting resolution response\n");
600 if (msg->resolution_resp.resolution_count == 0) {
601 pr_err("No supported resolutions\n");
606 index = msg->resolution_resp.default_resolution_index;
607 if (index >= msg->resolution_resp.resolution_count) {
608 pr_err("Invalid resolution index: %d\n", index);
614 msg->resolution_resp.supported_resolution[index].width;
616 msg->resolution_resp.supported_resolution[index].height;
622 /* Connect to VSP (Virtual Service Provider) on host */
623 static int synthvid_connect_vsp(struct hv_device *hdev)
625 struct fb_info *info = hv_get_drvdata(hdev);
626 struct hvfb_par *par = info->par;
629 ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE,
630 NULL, 0, synthvid_receive, hdev);
632 pr_err("Unable to open vmbus channel\n");
636 /* Negotiate the protocol version with host */
637 switch (vmbus_proto_version) {
639 case VERSION_WIN10_V5:
640 ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
646 ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8);
652 ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN7);
655 ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
660 pr_err("Synthetic video device version not accepted\n");
664 if (par->synthvid_version == SYNTHVID_VERSION_WIN7)
665 screen_depth = SYNTHVID_DEPTH_WIN7;
667 screen_depth = SYNTHVID_DEPTH_WIN8;
669 if (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10)) {
670 ret = synthvid_get_supported_resolution(hdev);
672 pr_info("Failed to get supported resolution from host, use default\n");
675 screen_fb_size = hdev->channel->offermsg.offer.
676 mmio_megabytes * 1024 * 1024;
681 vmbus_close(hdev->channel);
685 /* Send VRAM and Situation messages to the host */
686 static int synthvid_send_config(struct hv_device *hdev)
688 struct fb_info *info = hv_get_drvdata(hdev);
689 struct hvfb_par *par = info->par;
690 struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
694 /* Send VRAM location */
695 memset(msg, 0, sizeof(struct synthvid_msg));
696 msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION;
697 msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
698 sizeof(struct synthvid_vram_location);
699 msg->vram.user_ctx = msg->vram.vram_gpa = par->mmio_pp;
700 msg->vram.is_vram_gpa_specified = 1;
701 synthvid_send(hdev, msg);
703 t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
705 pr_err("Time out on waiting vram location ack\n");
709 if (msg->vram_ack.user_ctx != par->mmio_pp) {
710 pr_err("Unable to set VRAM location\n");
715 /* Send pointer and situation update */
716 synthvid_send_ptr(hdev);
717 synthvid_send_situ(hdev);
725 * Delayed work callback:
726 * It is scheduled to call whenever update request is received and it has
727 * not been called in last HVFB_ONDEMAND_THROTTLE time interval.
729 static void hvfb_update_work(struct work_struct *w)
731 struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work);
732 struct fb_info *info = par->info;
737 spin_lock_irqsave(&par->delayed_refresh_lock, flags);
738 /* Reset the request flag */
739 par->delayed_refresh = false;
741 /* Store the dirty rectangle to local variables */
747 /* Clear dirty rectangle */
748 par->x1 = par->y1 = INT_MAX;
749 par->x2 = par->y2 = 0;
751 spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
753 if (x1 > info->var.xres || x2 > info->var.xres ||
754 y1 > info->var.yres || y2 > info->var.yres || x2 <= x1)
757 /* Copy the dirty rectangle to frame buffer memory */
758 if (par->need_docopy)
759 for (j = y1; j < y2; j++)
761 j * info->fix.line_length +
762 (x1 * screen_depth / 8),
763 (x2 - x1) * screen_depth / 8);
766 if (par->fb_ready && par->update)
767 synthvid_update(info, x1, y1, x2, y2);
771 * Control the on-demand refresh frequency. It schedules a delayed
772 * screen update if it has not yet.
774 static void hvfb_ondemand_refresh_throttle(struct hvfb_par *par,
775 int x1, int y1, int w, int h)
781 spin_lock_irqsave(&par->delayed_refresh_lock, flags);
783 /* Merge dirty rectangle */
784 par->x1 = min_t(int, par->x1, x1);
785 par->y1 = min_t(int, par->y1, y1);
786 par->x2 = max_t(int, par->x2, x2);
787 par->y2 = max_t(int, par->y2, y2);
789 /* Schedule a delayed screen update if not yet */
790 if (par->delayed_refresh == false) {
791 schedule_delayed_work(&par->dwork,
792 HVFB_ONDEMAND_THROTTLE);
793 par->delayed_refresh = true;
796 spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
799 static int hvfb_on_panic(struct notifier_block *nb,
800 unsigned long e, void *p)
802 struct hvfb_par *par;
803 struct fb_info *info;
805 par = container_of(nb, struct hvfb_par, hvfb_panic_nb);
806 par->synchronous_fb = true;
808 if (par->need_docopy)
809 hvfb_docopy(par, 0, dio_fb_size);
810 synthvid_update(info, 0, 0, INT_MAX, INT_MAX);
815 /* Framebuffer operation handlers */
817 static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
819 if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN ||
820 var->xres > screen_width || var->yres > screen_height ||
821 var->bits_per_pixel != screen_depth)
824 var->xres_virtual = var->xres;
825 var->yres_virtual = var->yres;
830 static int hvfb_set_par(struct fb_info *info)
832 struct hv_device *hdev = device_to_hv_device(info->device);
834 return synthvid_send_situ(hdev);
838 static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf)
840 return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset;
843 static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
844 unsigned blue, unsigned transp, struct fb_info *info)
846 u32 *pal = info->pseudo_palette;
851 pal[regno] = chan_to_field(red, &info->var.red)
852 | chan_to_field(green, &info->var.green)
853 | chan_to_field(blue, &info->var.blue)
854 | chan_to_field(transp, &info->var.transp);
859 static int hvfb_blank(int blank, struct fb_info *info)
861 return 1; /* get fb_blank to set the colormap to all black */
864 static void hvfb_cfb_fillrect(struct fb_info *p,
865 const struct fb_fillrect *rect)
867 struct hvfb_par *par = p->par;
869 cfb_fillrect(p, rect);
870 if (par->synchronous_fb)
871 synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
873 hvfb_ondemand_refresh_throttle(par, rect->dx, rect->dy,
874 rect->width, rect->height);
877 static void hvfb_cfb_copyarea(struct fb_info *p,
878 const struct fb_copyarea *area)
880 struct hvfb_par *par = p->par;
882 cfb_copyarea(p, area);
883 if (par->synchronous_fb)
884 synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
886 hvfb_ondemand_refresh_throttle(par, area->dx, area->dy,
887 area->width, area->height);
890 static void hvfb_cfb_imageblit(struct fb_info *p,
891 const struct fb_image *image)
893 struct hvfb_par *par = p->par;
895 cfb_imageblit(p, image);
896 if (par->synchronous_fb)
897 synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
899 hvfb_ondemand_refresh_throttle(par, image->dx, image->dy,
900 image->width, image->height);
903 static const struct fb_ops hvfb_ops = {
904 .owner = THIS_MODULE,
905 .fb_check_var = hvfb_check_var,
906 .fb_set_par = hvfb_set_par,
907 .fb_setcolreg = hvfb_setcolreg,
908 .fb_fillrect = hvfb_cfb_fillrect,
909 .fb_copyarea = hvfb_cfb_copyarea,
910 .fb_imageblit = hvfb_cfb_imageblit,
911 .fb_blank = hvfb_blank,
915 /* Get options from kernel paramenter "video=" */
916 static void hvfb_get_option(struct fb_info *info)
918 struct hvfb_par *par = info->par;
919 char *opt = NULL, *p;
922 if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt)
925 p = strsep(&opt, "x");
926 if (!*p || kstrtouint(p, 0, &x) ||
927 !opt || !*opt || kstrtouint(opt, 0, &y)) {
928 pr_err("Screen option is invalid: skipped\n");
932 if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN ||
933 (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10) &&
934 (x * y * screen_depth / 8 > screen_fb_size)) ||
935 (par->synthvid_version == SYNTHVID_VERSION_WIN8 &&
936 x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8) ||
937 (par->synthvid_version == SYNTHVID_VERSION_WIN7 &&
938 (x > SYNTHVID_WIDTH_MAX_WIN7 || y > SYNTHVID_HEIGHT_MAX_WIN7))) {
939 pr_err("Screen resolution option is out of range: skipped\n");
949 * Allocate enough contiguous physical memory.
950 * Return physical address if succeeded or -1 if failed.
952 static phys_addr_t hvfb_get_phymem(struct hv_device *hdev,
953 unsigned int request_size)
955 struct page *page = NULL;
956 dma_addr_t dma_handle;
958 phys_addr_t paddr = 0;
959 unsigned int order = get_order(request_size);
961 if (request_size == 0)
964 if (order < MAX_ORDER) {
965 /* Call alloc_pages if the size is less than 2^MAX_ORDER */
966 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
970 paddr = (page_to_pfn(page) << PAGE_SHIFT);
972 /* Allocate from CMA */
973 hdev->device.coherent_dma_mask = DMA_BIT_MASK(64);
975 vmem = dma_alloc_coherent(&hdev->device,
976 round_up(request_size, PAGE_SIZE),
978 GFP_KERNEL | __GFP_NOWARN);
983 paddr = virt_to_phys(vmem);
989 /* Release contiguous physical memory */
990 static void hvfb_release_phymem(struct hv_device *hdev,
991 phys_addr_t paddr, unsigned int size)
993 unsigned int order = get_order(size);
995 if (order < MAX_ORDER)
996 __free_pages(pfn_to_page(paddr >> PAGE_SHIFT), order);
998 dma_free_coherent(&hdev->device,
999 round_up(size, PAGE_SIZE),
1000 phys_to_virt(paddr),
1005 /* Get framebuffer memory from Hyper-V video pci space */
1006 static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info)
1008 struct hvfb_par *par = info->par;
1009 struct pci_dev *pdev = NULL;
1010 void __iomem *fb_virt;
1011 int gen2vm = efi_enabled(EFI_BOOT);
1012 resource_size_t pot_start, pot_end;
1016 info->apertures = alloc_apertures(1);
1017 if (!info->apertures)
1021 pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
1022 PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
1024 pr_err("Unable to find PCI Hyper-V video\n");
1028 info->apertures->ranges[0].base = pci_resource_start(pdev, 0);
1029 info->apertures->ranges[0].size = pci_resource_len(pdev, 0);
1032 * For Gen 1 VM, we can directly use the contiguous memory
1033 * from VM. If we succeed, deferred IO happens directly
1034 * on this allocated framebuffer memory, avoiding extra
1037 paddr = hvfb_get_phymem(hdev, screen_fb_size);
1038 if (paddr != (phys_addr_t) -1) {
1039 par->mmio_pp = paddr;
1040 par->mmio_vp = par->dio_vp = __va(paddr);
1042 info->fix.smem_start = paddr;
1043 info->fix.smem_len = screen_fb_size;
1044 info->screen_base = par->mmio_vp;
1045 info->screen_size = screen_fb_size;
1047 par->need_docopy = false;
1050 pr_info("Unable to allocate enough contiguous physical memory on Gen 1 VM. Using MMIO instead.\n");
1052 info->apertures->ranges[0].base = screen_info.lfb_base;
1053 info->apertures->ranges[0].size = screen_info.lfb_size;
1057 * Cannot use the contiguous physical memory.
1058 * Allocate mmio space for framebuffer.
1061 screen_width * screen_height * screen_depth / 8;
1067 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
1068 pci_resource_len(pdev, 0) < screen_fb_size) {
1069 pr_err("Resource not available or (0x%lx < 0x%lx)\n",
1070 (unsigned long) pci_resource_len(pdev, 0),
1071 (unsigned long) screen_fb_size);
1075 pot_end = pci_resource_end(pdev, 0);
1076 pot_start = pot_end - screen_fb_size + 1;
1079 ret = vmbus_allocate_mmio(&par->mem, hdev, pot_start, pot_end,
1080 screen_fb_size, 0x100000, true);
1082 pr_err("Unable to allocate framebuffer memory\n");
1087 * Map the VRAM cacheable for performance. This is also required for
1088 * VM Connect to display properly for ARM64 Linux VM, as the host also
1089 * maps the VRAM cacheable.
1091 fb_virt = ioremap_cache(par->mem->start, screen_fb_size);
1095 /* Allocate memory for deferred IO */
1096 par->dio_vp = vzalloc(round_up(dio_fb_size, PAGE_SIZE));
1097 if (par->dio_vp == NULL)
1100 /* Physical address of FB device */
1101 par->mmio_pp = par->mem->start;
1102 /* Virtual address of FB device */
1103 par->mmio_vp = (unsigned char *) fb_virt;
1105 info->fix.smem_start = par->mem->start;
1106 info->fix.smem_len = dio_fb_size;
1107 info->screen_base = par->dio_vp;
1108 info->screen_size = dio_fb_size;
1111 remove_conflicting_framebuffers(info->apertures,
1112 KBUILD_MODNAME, false);
1115 /* framebuffer is reallocated, clear screen_info to avoid misuse from kexec */
1116 screen_info.lfb_size = 0;
1117 screen_info.lfb_base = 0;
1118 screen_info.orig_video_isVGA = 0;
1128 vmbus_free_mmio(par->mem->start, screen_fb_size);
1137 /* Release the framebuffer */
1138 static void hvfb_putmem(struct hv_device *hdev, struct fb_info *info)
1140 struct hvfb_par *par = info->par;
1142 if (par->need_docopy) {
1144 iounmap(info->screen_base);
1145 vmbus_free_mmio(par->mem->start, screen_fb_size);
1147 hvfb_release_phymem(hdev, info->fix.smem_start,
1155 static int hvfb_probe(struct hv_device *hdev,
1156 const struct hv_vmbus_device_id *dev_id)
1158 struct fb_info *info;
1159 struct hvfb_par *par;
1162 info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device);
1168 par->fb_ready = false;
1169 par->need_docopy = true;
1170 init_completion(&par->wait);
1171 INIT_DELAYED_WORK(&par->dwork, hvfb_update_work);
1173 par->delayed_refresh = false;
1174 spin_lock_init(&par->delayed_refresh_lock);
1175 par->x1 = par->y1 = INT_MAX;
1176 par->x2 = par->y2 = 0;
1178 /* Connect to VSP */
1179 hv_set_drvdata(hdev, info);
1180 ret = synthvid_connect_vsp(hdev);
1182 pr_err("Unable to connect to VSP\n");
1186 hvfb_get_option(info);
1187 pr_info("Screen resolution: %dx%d, Color depth: %d, Frame buffer size: %d\n",
1188 screen_width, screen_height, screen_depth, screen_fb_size);
1190 ret = hvfb_getmem(hdev, info);
1192 pr_err("No memory for framebuffer\n");
1196 /* Set up fb_info */
1197 info->flags = FBINFO_DEFAULT;
1199 info->var.xres_virtual = info->var.xres = screen_width;
1200 info->var.yres_virtual = info->var.yres = screen_height;
1201 info->var.bits_per_pixel = screen_depth;
1203 if (info->var.bits_per_pixel == 16) {
1204 info->var.red = (struct fb_bitfield){11, 5, 0};
1205 info->var.green = (struct fb_bitfield){5, 6, 0};
1206 info->var.blue = (struct fb_bitfield){0, 5, 0};
1207 info->var.transp = (struct fb_bitfield){0, 0, 0};
1209 info->var.red = (struct fb_bitfield){16, 8, 0};
1210 info->var.green = (struct fb_bitfield){8, 8, 0};
1211 info->var.blue = (struct fb_bitfield){0, 8, 0};
1212 info->var.transp = (struct fb_bitfield){24, 8, 0};
1215 info->var.activate = FB_ACTIVATE_NOW;
1216 info->var.height = -1;
1217 info->var.width = -1;
1218 info->var.vmode = FB_VMODE_NONINTERLACED;
1220 strcpy(info->fix.id, KBUILD_MODNAME);
1221 info->fix.type = FB_TYPE_PACKED_PIXELS;
1222 info->fix.visual = FB_VISUAL_TRUECOLOR;
1223 info->fix.line_length = screen_width * screen_depth / 8;
1224 info->fix.accel = FB_ACCEL_NONE;
1226 info->fbops = &hvfb_ops;
1227 info->pseudo_palette = par->pseudo_palette;
1229 /* Initialize deferred IO */
1230 info->fbdefio = &synthvid_defio;
1231 fb_deferred_io_init(info);
1233 /* Send config to host */
1234 ret = synthvid_send_config(hdev);
1238 ret = register_framebuffer(info);
1240 pr_err("Unable to register framebuffer\n");
1244 par->fb_ready = true;
1246 par->synchronous_fb = false;
1247 par->hvfb_panic_nb.notifier_call = hvfb_on_panic;
1248 atomic_notifier_chain_register(&panic_notifier_list,
1249 &par->hvfb_panic_nb);
1254 fb_deferred_io_cleanup(info);
1255 hvfb_putmem(hdev, info);
1257 vmbus_close(hdev->channel);
1259 cancel_delayed_work_sync(&par->dwork);
1260 hv_set_drvdata(hdev, NULL);
1261 framebuffer_release(info);
1266 static int hvfb_remove(struct hv_device *hdev)
1268 struct fb_info *info = hv_get_drvdata(hdev);
1269 struct hvfb_par *par = info->par;
1271 atomic_notifier_chain_unregister(&panic_notifier_list,
1272 &par->hvfb_panic_nb);
1274 par->update = false;
1275 par->fb_ready = false;
1277 fb_deferred_io_cleanup(info);
1279 unregister_framebuffer(info);
1280 cancel_delayed_work_sync(&par->dwork);
1282 vmbus_close(hdev->channel);
1283 hv_set_drvdata(hdev, NULL);
1285 hvfb_putmem(hdev, info);
1286 framebuffer_release(info);
1291 static int hvfb_suspend(struct hv_device *hdev)
1293 struct fb_info *info = hv_get_drvdata(hdev);
1294 struct hvfb_par *par = info->par;
1298 /* 1 means do suspend */
1299 fb_set_suspend(info, 1);
1301 cancel_delayed_work_sync(&par->dwork);
1302 cancel_delayed_work_sync(&info->deferred_work);
1304 par->update_saved = par->update;
1305 par->update = false;
1306 par->fb_ready = false;
1308 vmbus_close(hdev->channel);
1315 static int hvfb_resume(struct hv_device *hdev)
1317 struct fb_info *info = hv_get_drvdata(hdev);
1318 struct hvfb_par *par = info->par;
1323 ret = synthvid_connect_vsp(hdev);
1327 ret = synthvid_send_config(hdev);
1329 vmbus_close(hdev->channel);
1333 par->fb_ready = true;
1334 par->update = par->update_saved;
1336 schedule_delayed_work(&info->deferred_work, info->fbdefio->delay);
1337 schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
1339 /* 0 means do resume */
1340 fb_set_suspend(info, 0);
1349 static const struct pci_device_id pci_stub_id_table[] = {
1351 .vendor = PCI_VENDOR_ID_MICROSOFT,
1352 .device = PCI_DEVICE_ID_HYPERV_VIDEO,
1354 { /* end of list */ }
1357 static const struct hv_vmbus_device_id id_table[] = {
1358 /* Synthetic Video Device GUID */
1363 MODULE_DEVICE_TABLE(pci, pci_stub_id_table);
1364 MODULE_DEVICE_TABLE(vmbus, id_table);
1366 static struct hv_driver hvfb_drv = {
1367 .name = KBUILD_MODNAME,
1368 .id_table = id_table,
1369 .probe = hvfb_probe,
1370 .remove = hvfb_remove,
1371 .suspend = hvfb_suspend,
1372 .resume = hvfb_resume,
1374 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1378 static int hvfb_pci_stub_probe(struct pci_dev *pdev,
1379 const struct pci_device_id *ent)
1384 static void hvfb_pci_stub_remove(struct pci_dev *pdev)
1388 static struct pci_driver hvfb_pci_stub_driver = {
1389 .name = KBUILD_MODNAME,
1390 .id_table = pci_stub_id_table,
1391 .probe = hvfb_pci_stub_probe,
1392 .remove = hvfb_pci_stub_remove,
1394 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1398 static int __init hvfb_drv_init(void)
1402 ret = vmbus_driver_register(&hvfb_drv);
1406 ret = pci_register_driver(&hvfb_pci_stub_driver);
1408 vmbus_driver_unregister(&hvfb_drv);
1415 static void __exit hvfb_drv_exit(void)
1417 pci_unregister_driver(&hvfb_pci_stub_driver);
1418 vmbus_driver_unregister(&hvfb_drv);
1421 module_init(hvfb_drv_init);
1422 module_exit(hvfb_drv_exit);
1424 MODULE_LICENSE("GPL");
1425 MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver");