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
36 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38 #include <linux/module.h>
39 #include <linux/kernel.h>
40 #include <linux/init.h>
41 #include <linux/completion.h>
43 #include <linux/pci.h>
44 #include <linux/efi.h>
45 #include <linux/console.h>
47 #include <linux/hyperv.h>
50 /* Hyper-V Synthetic Video Protocol definitions and structures */
51 #define MAX_VMBUS_PKT_SIZE 0x4000
53 #define SYNTHVID_VERSION(major, minor) ((minor) << 16 | (major))
54 #define SYNTHVID_VERSION_WIN7 SYNTHVID_VERSION(3, 0)
55 #define SYNTHVID_VERSION_WIN8 SYNTHVID_VERSION(3, 2)
56 #define SYNTHVID_VERSION_WIN10 SYNTHVID_VERSION(3, 5)
58 #define SYNTHVID_VER_GET_MAJOR(ver) (ver & 0x0000ffff)
59 #define SYNTHVID_VER_GET_MINOR(ver) ((ver & 0xffff0000) >> 16)
61 #define SYNTHVID_DEPTH_WIN7 16
62 #define SYNTHVID_DEPTH_WIN8 32
64 #define SYNTHVID_FB_SIZE_WIN7 (4 * 1024 * 1024)
65 #define SYNTHVID_WIDTH_MAX_WIN7 1600
66 #define SYNTHVID_HEIGHT_MAX_WIN7 1200
68 #define SYNTHVID_FB_SIZE_WIN8 (8 * 1024 * 1024)
70 #define PCI_VENDOR_ID_MICROSOFT 0x1414
71 #define PCI_DEVICE_ID_HYPERV_VIDEO 0x5353
82 u32 size; /* size of message after this field */
86 enum synthvid_msg_type {
88 SYNTHVID_VERSION_REQUEST = 1,
89 SYNTHVID_VERSION_RESPONSE = 2,
90 SYNTHVID_VRAM_LOCATION = 3,
91 SYNTHVID_VRAM_LOCATION_ACK = 4,
92 SYNTHVID_SITUATION_UPDATE = 5,
93 SYNTHVID_SITUATION_UPDATE_ACK = 6,
94 SYNTHVID_POINTER_POSITION = 7,
95 SYNTHVID_POINTER_SHAPE = 8,
96 SYNTHVID_FEATURE_CHANGE = 9,
98 SYNTHVID_RESOLUTION_REQUEST = 13,
99 SYNTHVID_RESOLUTION_RESPONSE = 14,
104 #define SYNTHVID_EDID_BLOCK_SIZE 128
105 #define SYNTHVID_MAX_RESOLUTION_COUNT 64
107 struct hvd_screen_info {
112 struct synthvid_msg_hdr {
114 u32 size; /* size of this header + payload after this field*/
117 struct synthvid_version_req {
121 struct synthvid_version_resp {
124 u8 max_video_outputs;
127 struct synthvid_supported_resolution_req {
128 u8 maximum_resolution_count;
131 struct synthvid_supported_resolution_resp {
132 u8 edid_block[SYNTHVID_EDID_BLOCK_SIZE];
134 u8 default_resolution_index;
136 struct hvd_screen_info
137 supported_resolution[SYNTHVID_MAX_RESOLUTION_COUNT];
140 struct synthvid_vram_location {
142 u8 is_vram_gpa_specified;
146 struct synthvid_vram_location_ack {
150 struct video_output_situation {
159 struct synthvid_situation_update {
161 u8 video_output_count;
162 struct video_output_situation video_output[1];
165 struct synthvid_situation_update_ack {
169 struct synthvid_pointer_position {
177 #define CURSOR_MAX_X 96
178 #define CURSOR_MAX_Y 96
179 #define CURSOR_ARGB_PIXEL_SIZE 4
180 #define CURSOR_MAX_SIZE (CURSOR_MAX_X * CURSOR_MAX_Y * CURSOR_ARGB_PIXEL_SIZE)
181 #define CURSOR_COMPLETE (-1)
183 struct synthvid_pointer_shape {
186 u32 width; /* CURSOR_MAX_X at most */
187 u32 height; /* CURSOR_MAX_Y at most */
188 u32 hot_x; /* hotspot relative to upper-left of pointer image */
193 struct synthvid_feature_change {
195 u8 is_ptr_pos_needed;
196 u8 is_ptr_shape_needed;
201 s32 x1, y1; /* top left corner */
202 s32 x2, y2; /* bottom right corner, exclusive */
205 struct synthvid_dirt {
211 struct synthvid_msg {
212 struct pipe_msg_hdr pipe_hdr;
213 struct synthvid_msg_hdr vid_hdr;
215 struct synthvid_version_req ver_req;
216 struct synthvid_version_resp ver_resp;
217 struct synthvid_vram_location vram;
218 struct synthvid_vram_location_ack vram_ack;
219 struct synthvid_situation_update situ;
220 struct synthvid_situation_update_ack situ_ack;
221 struct synthvid_pointer_position ptr_pos;
222 struct synthvid_pointer_shape ptr_shape;
223 struct synthvid_feature_change feature_chg;
224 struct synthvid_dirt dirt;
225 struct synthvid_supported_resolution_req resolution_req;
226 struct synthvid_supported_resolution_resp resolution_resp;
232 /* FB driver definitions and structures */
233 #define HVFB_WIDTH 1152 /* default screen width */
234 #define HVFB_HEIGHT 864 /* default screen height */
235 #define HVFB_WIDTH_MIN 640
236 #define HVFB_HEIGHT_MIN 480
238 #define RING_BUFSIZE (256 * 1024)
239 #define VSP_TIMEOUT (10 * HZ)
240 #define HVFB_UPDATE_DELAY (HZ / 20)
241 #define HVFB_ONDEMAND_THROTTLE (HZ / 20)
244 struct fb_info *info;
245 struct resource *mem;
246 bool fb_ready; /* fb device is ready */
247 struct completion wait;
248 u32 synthvid_version;
250 struct delayed_work dwork;
252 bool update_saved; /* The value of 'update' before hibernation */
254 u32 pseudo_palette[16];
255 u8 init_buf[MAX_VMBUS_PKT_SIZE];
256 u8 recv_buf[MAX_VMBUS_PKT_SIZE];
258 /* If true, the VSC notifies the VSP on every framebuffer change */
261 struct notifier_block hvfb_panic_nb;
263 /* Memory for deferred IO and frame buffer itself */
264 unsigned char *dio_vp;
265 unsigned char *mmio_vp;
266 unsigned long mmio_pp;
268 /* Dirty rectangle, protected by delayed_refresh_lock */
270 bool delayed_refresh;
271 spinlock_t delayed_refresh_lock;
274 static uint screen_width = HVFB_WIDTH;
275 static uint screen_height = HVFB_HEIGHT;
276 static uint screen_width_max = HVFB_WIDTH;
277 static uint screen_height_max = HVFB_HEIGHT;
278 static uint screen_depth;
279 static uint screen_fb_size;
280 static uint dio_fb_size; /* FB size for deferred IO */
282 /* Send message to Hyper-V host */
283 static inline int synthvid_send(struct hv_device *hdev,
284 struct synthvid_msg *msg)
286 static atomic64_t request_id = ATOMIC64_INIT(0);
289 msg->pipe_hdr.type = PIPE_MSG_DATA;
290 msg->pipe_hdr.size = msg->vid_hdr.size;
292 ret = vmbus_sendpacket(hdev->channel, msg,
293 msg->vid_hdr.size + sizeof(struct pipe_msg_hdr),
294 atomic64_inc_return(&request_id),
295 VM_PKT_DATA_INBAND, 0);
298 pr_err("Unable to send packet via vmbus\n");
304 /* Send screen resolution info to host */
305 static int synthvid_send_situ(struct hv_device *hdev)
307 struct fb_info *info = hv_get_drvdata(hdev);
308 struct synthvid_msg msg;
313 memset(&msg, 0, sizeof(struct synthvid_msg));
315 msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE;
316 msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
317 sizeof(struct synthvid_situation_update);
318 msg.situ.user_ctx = 0;
319 msg.situ.video_output_count = 1;
320 msg.situ.video_output[0].active = 1;
321 msg.situ.video_output[0].vram_offset = 0;
322 msg.situ.video_output[0].depth_bits = info->var.bits_per_pixel;
323 msg.situ.video_output[0].width_pixels = info->var.xres;
324 msg.situ.video_output[0].height_pixels = info->var.yres;
325 msg.situ.video_output[0].pitch_bytes = info->fix.line_length;
327 synthvid_send(hdev, &msg);
332 /* Send mouse pointer info to host */
333 static int synthvid_send_ptr(struct hv_device *hdev)
335 struct synthvid_msg msg;
337 memset(&msg, 0, sizeof(struct synthvid_msg));
338 msg.vid_hdr.type = SYNTHVID_POINTER_POSITION;
339 msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
340 sizeof(struct synthvid_pointer_position);
341 msg.ptr_pos.is_visible = 1;
342 msg.ptr_pos.video_output = 0;
343 msg.ptr_pos.image_x = 0;
344 msg.ptr_pos.image_y = 0;
345 synthvid_send(hdev, &msg);
347 memset(&msg, 0, sizeof(struct synthvid_msg));
348 msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE;
349 msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
350 sizeof(struct synthvid_pointer_shape);
351 msg.ptr_shape.part_idx = CURSOR_COMPLETE;
352 msg.ptr_shape.is_argb = 1;
353 msg.ptr_shape.width = 1;
354 msg.ptr_shape.height = 1;
355 msg.ptr_shape.hot_x = 0;
356 msg.ptr_shape.hot_y = 0;
357 msg.ptr_shape.data[0] = 0;
358 msg.ptr_shape.data[1] = 1;
359 msg.ptr_shape.data[2] = 1;
360 msg.ptr_shape.data[3] = 1;
361 synthvid_send(hdev, &msg);
366 /* Send updated screen area (dirty rectangle) location to host */
368 synthvid_update(struct fb_info *info, int x1, int y1, int x2, int y2)
370 struct hv_device *hdev = device_to_hv_device(info->device);
371 struct synthvid_msg msg;
373 memset(&msg, 0, sizeof(struct synthvid_msg));
379 msg.vid_hdr.type = SYNTHVID_DIRT;
380 msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
381 sizeof(struct synthvid_dirt);
382 msg.dirt.video_output = 0;
383 msg.dirt.dirt_count = 1;
384 msg.dirt.rect[0].x1 = (x1 > x2) ? 0 : x1;
385 msg.dirt.rect[0].y1 = (y1 > y2) ? 0 : y1;
386 msg.dirt.rect[0].x2 =
387 (x2 < x1 || x2 > info->var.xres) ? info->var.xres : x2;
388 msg.dirt.rect[0].y2 =
389 (y2 < y1 || y2 > info->var.yres) ? info->var.yres : y2;
391 synthvid_send(hdev, &msg);
396 static void hvfb_docopy(struct hvfb_par *par,
397 unsigned long offset,
400 if (!par || !par->mmio_vp || !par->dio_vp || !par->fb_ready ||
401 size == 0 || offset >= dio_fb_size)
404 if (offset + size > dio_fb_size)
405 size = dio_fb_size - offset;
407 memcpy(par->mmio_vp + offset, par->dio_vp + offset, size);
410 /* Deferred IO callback */
411 static void synthvid_deferred_io(struct fb_info *p,
412 struct list_head *pagelist)
414 struct hvfb_par *par = p->par;
416 unsigned long start, end;
417 int y1, y2, miny, maxy;
423 * Merge dirty pages. It is possible that last page cross
424 * over the end of frame buffer row yres. This is taken care of
425 * in synthvid_update function by clamping the y2
428 list_for_each_entry(page, pagelist, lru) {
429 start = page->index << PAGE_SHIFT;
430 end = start + PAGE_SIZE - 1;
431 y1 = start / p->fix.line_length;
432 y2 = end / p->fix.line_length;
433 miny = min_t(int, miny, y1);
434 maxy = max_t(int, maxy, y2);
436 /* Copy from dio space to mmio address */
438 hvfb_docopy(par, start, PAGE_SIZE);
441 if (par->fb_ready && par->update)
442 synthvid_update(p, 0, miny, p->var.xres, maxy + 1);
445 static struct fb_deferred_io synthvid_defio = {
447 .deferred_io = synthvid_deferred_io,
451 * Actions on received messages from host:
452 * Complete the wait event.
453 * Or, reply with screen and cursor info.
455 static void synthvid_recv_sub(struct hv_device *hdev)
457 struct fb_info *info = hv_get_drvdata(hdev);
458 struct hvfb_par *par;
459 struct synthvid_msg *msg;
465 msg = (struct synthvid_msg *)par->recv_buf;
467 /* Complete the wait event */
468 if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE ||
469 msg->vid_hdr.type == SYNTHVID_RESOLUTION_RESPONSE ||
470 msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) {
471 memcpy(par->init_buf, msg, MAX_VMBUS_PKT_SIZE);
472 complete(&par->wait);
476 /* Reply with screen and cursor info */
477 if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) {
479 synthvid_send_ptr(hdev);
480 synthvid_send_situ(hdev);
483 par->update = msg->feature_chg.is_dirt_needed;
485 schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
489 /* Receive callback for messages from the host */
490 static void synthvid_receive(void *ctx)
492 struct hv_device *hdev = ctx;
493 struct fb_info *info = hv_get_drvdata(hdev);
494 struct hvfb_par *par;
495 struct synthvid_msg *recv_buf;
504 recv_buf = (struct synthvid_msg *)par->recv_buf;
507 ret = vmbus_recvpacket(hdev->channel, recv_buf,
509 &bytes_recvd, &req_id);
510 if (bytes_recvd > 0 &&
511 recv_buf->pipe_hdr.type == PIPE_MSG_DATA)
512 synthvid_recv_sub(hdev);
513 } while (bytes_recvd > 0 && ret == 0);
516 /* Check if the ver1 version is equal or greater than ver2 */
517 static inline bool synthvid_ver_ge(u32 ver1, u32 ver2)
519 if (SYNTHVID_VER_GET_MAJOR(ver1) > SYNTHVID_VER_GET_MAJOR(ver2) ||
520 (SYNTHVID_VER_GET_MAJOR(ver1) == SYNTHVID_VER_GET_MAJOR(ver2) &&
521 SYNTHVID_VER_GET_MINOR(ver1) >= SYNTHVID_VER_GET_MINOR(ver2)))
527 /* Check synthetic video protocol version with the host */
528 static int synthvid_negotiate_ver(struct hv_device *hdev, u32 ver)
530 struct fb_info *info = hv_get_drvdata(hdev);
531 struct hvfb_par *par = info->par;
532 struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
536 memset(msg, 0, sizeof(struct synthvid_msg));
537 msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST;
538 msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
539 sizeof(struct synthvid_version_req);
540 msg->ver_req.version = ver;
541 synthvid_send(hdev, msg);
543 t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
545 pr_err("Time out on waiting version response\n");
549 if (!msg->ver_resp.is_accepted) {
554 par->synthvid_version = ver;
555 pr_info("Synthvid Version major %d, minor %d\n",
556 SYNTHVID_VER_GET_MAJOR(ver), SYNTHVID_VER_GET_MINOR(ver));
562 /* Get current resolution from the host */
563 static int synthvid_get_supported_resolution(struct hv_device *hdev)
565 struct fb_info *info = hv_get_drvdata(hdev);
566 struct hvfb_par *par = info->par;
567 struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
573 memset(msg, 0, sizeof(struct synthvid_msg));
574 msg->vid_hdr.type = SYNTHVID_RESOLUTION_REQUEST;
575 msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
576 sizeof(struct synthvid_supported_resolution_req);
578 msg->resolution_req.maximum_resolution_count =
579 SYNTHVID_MAX_RESOLUTION_COUNT;
580 synthvid_send(hdev, msg);
582 t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
584 pr_err("Time out on waiting resolution response\n");
589 if (msg->resolution_resp.resolution_count == 0) {
590 pr_err("No supported resolutions\n");
595 index = msg->resolution_resp.default_resolution_index;
596 if (index >= msg->resolution_resp.resolution_count) {
597 pr_err("Invalid resolution index: %d\n", index);
602 for (i = 0; i < msg->resolution_resp.resolution_count; i++) {
603 screen_width_max = max_t(unsigned int, screen_width_max,
604 msg->resolution_resp.supported_resolution[i].width);
605 screen_height_max = max_t(unsigned int, screen_height_max,
606 msg->resolution_resp.supported_resolution[i].height);
610 msg->resolution_resp.supported_resolution[index].width;
612 msg->resolution_resp.supported_resolution[index].height;
618 /* Connect to VSP (Virtual Service Provider) on host */
619 static int synthvid_connect_vsp(struct hv_device *hdev)
621 struct fb_info *info = hv_get_drvdata(hdev);
622 struct hvfb_par *par = info->par;
625 ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE,
626 NULL, 0, synthvid_receive, hdev);
628 pr_err("Unable to open vmbus channel\n");
632 /* Negotiate the protocol version with host */
633 switch (vmbus_proto_version) {
635 case VERSION_WIN10_V5:
636 ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
642 ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8);
648 ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN7);
651 ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
656 pr_err("Synthetic video device version not accepted\n");
660 if (par->synthvid_version == SYNTHVID_VERSION_WIN7)
661 screen_depth = SYNTHVID_DEPTH_WIN7;
663 screen_depth = SYNTHVID_DEPTH_WIN8;
665 if (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10)) {
666 ret = synthvid_get_supported_resolution(hdev);
668 pr_info("Failed to get supported resolution from host, use default\n");
671 screen_fb_size = hdev->channel->offermsg.offer.
672 mmio_megabytes * 1024 * 1024;
677 vmbus_close(hdev->channel);
681 /* Send VRAM and Situation messages to the host */
682 static int synthvid_send_config(struct hv_device *hdev)
684 struct fb_info *info = hv_get_drvdata(hdev);
685 struct hvfb_par *par = info->par;
686 struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
690 /* Send VRAM location */
691 memset(msg, 0, sizeof(struct synthvid_msg));
692 msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION;
693 msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
694 sizeof(struct synthvid_vram_location);
695 msg->vram.user_ctx = msg->vram.vram_gpa = par->mmio_pp;
696 msg->vram.is_vram_gpa_specified = 1;
697 synthvid_send(hdev, msg);
699 t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
701 pr_err("Time out on waiting vram location ack\n");
705 if (msg->vram_ack.user_ctx != par->mmio_pp) {
706 pr_err("Unable to set VRAM location\n");
711 /* Send pointer and situation update */
712 synthvid_send_ptr(hdev);
713 synthvid_send_situ(hdev);
721 * Delayed work callback:
722 * It is scheduled to call whenever update request is received and it has
723 * not been called in last HVFB_ONDEMAND_THROTTLE time interval.
725 static void hvfb_update_work(struct work_struct *w)
727 struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work);
728 struct fb_info *info = par->info;
733 spin_lock_irqsave(&par->delayed_refresh_lock, flags);
734 /* Reset the request flag */
735 par->delayed_refresh = false;
737 /* Store the dirty rectangle to local variables */
743 /* Clear dirty rectangle */
744 par->x1 = par->y1 = INT_MAX;
745 par->x2 = par->y2 = 0;
747 spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
749 if (x1 > info->var.xres || x2 > info->var.xres ||
750 y1 > info->var.yres || y2 > info->var.yres || x2 <= x1)
753 /* Copy the dirty rectangle to frame buffer memory */
754 for (j = y1; j < y2; j++) {
756 j * info->fix.line_length +
757 (x1 * screen_depth / 8),
758 (x2 - x1) * screen_depth / 8);
762 if (par->fb_ready && par->update)
763 synthvid_update(info, x1, y1, x2, y2);
767 * Control the on-demand refresh frequency. It schedules a delayed
768 * screen update if it has not yet.
770 static void hvfb_ondemand_refresh_throttle(struct hvfb_par *par,
771 int x1, int y1, int w, int h)
777 spin_lock_irqsave(&par->delayed_refresh_lock, flags);
779 /* Merge dirty rectangle */
780 par->x1 = min_t(int, par->x1, x1);
781 par->y1 = min_t(int, par->y1, y1);
782 par->x2 = max_t(int, par->x2, x2);
783 par->y2 = max_t(int, par->y2, y2);
785 /* Schedule a delayed screen update if not yet */
786 if (par->delayed_refresh == false) {
787 schedule_delayed_work(&par->dwork,
788 HVFB_ONDEMAND_THROTTLE);
789 par->delayed_refresh = true;
792 spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
795 static int hvfb_on_panic(struct notifier_block *nb,
796 unsigned long e, void *p)
798 struct hvfb_par *par;
799 struct fb_info *info;
801 par = container_of(nb, struct hvfb_par, hvfb_panic_nb);
802 par->synchronous_fb = true;
804 hvfb_docopy(par, 0, dio_fb_size);
805 synthvid_update(info, 0, 0, INT_MAX, INT_MAX);
810 /* Framebuffer operation handlers */
812 static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
814 if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN ||
815 var->xres > screen_width || var->yres > screen_height ||
816 var->bits_per_pixel != screen_depth)
819 var->xres_virtual = var->xres;
820 var->yres_virtual = var->yres;
825 static int hvfb_set_par(struct fb_info *info)
827 struct hv_device *hdev = device_to_hv_device(info->device);
829 return synthvid_send_situ(hdev);
833 static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf)
835 return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset;
838 static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
839 unsigned blue, unsigned transp, struct fb_info *info)
841 u32 *pal = info->pseudo_palette;
846 pal[regno] = chan_to_field(red, &info->var.red)
847 | chan_to_field(green, &info->var.green)
848 | chan_to_field(blue, &info->var.blue)
849 | chan_to_field(transp, &info->var.transp);
854 static int hvfb_blank(int blank, struct fb_info *info)
856 return 1; /* get fb_blank to set the colormap to all black */
859 static void hvfb_cfb_fillrect(struct fb_info *p,
860 const struct fb_fillrect *rect)
862 struct hvfb_par *par = p->par;
864 cfb_fillrect(p, rect);
865 if (par->synchronous_fb)
866 synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
868 hvfb_ondemand_refresh_throttle(par, rect->dx, rect->dy,
869 rect->width, rect->height);
872 static void hvfb_cfb_copyarea(struct fb_info *p,
873 const struct fb_copyarea *area)
875 struct hvfb_par *par = p->par;
877 cfb_copyarea(p, area);
878 if (par->synchronous_fb)
879 synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
881 hvfb_ondemand_refresh_throttle(par, area->dx, area->dy,
882 area->width, area->height);
885 static void hvfb_cfb_imageblit(struct fb_info *p,
886 const struct fb_image *image)
888 struct hvfb_par *par = p->par;
890 cfb_imageblit(p, image);
891 if (par->synchronous_fb)
892 synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
894 hvfb_ondemand_refresh_throttle(par, image->dx, image->dy,
895 image->width, image->height);
898 static struct fb_ops hvfb_ops = {
899 .owner = THIS_MODULE,
900 .fb_check_var = hvfb_check_var,
901 .fb_set_par = hvfb_set_par,
902 .fb_setcolreg = hvfb_setcolreg,
903 .fb_fillrect = hvfb_cfb_fillrect,
904 .fb_copyarea = hvfb_cfb_copyarea,
905 .fb_imageblit = hvfb_cfb_imageblit,
906 .fb_blank = hvfb_blank,
910 /* Get options from kernel paramenter "video=" */
911 static void hvfb_get_option(struct fb_info *info)
913 struct hvfb_par *par = info->par;
914 char *opt = NULL, *p;
917 if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt)
920 p = strsep(&opt, "x");
921 if (!*p || kstrtouint(p, 0, &x) ||
922 !opt || !*opt || kstrtouint(opt, 0, &y)) {
923 pr_err("Screen option is invalid: skipped\n");
927 if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN ||
928 (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10) &&
929 (x > screen_width_max || y > screen_height_max)) ||
930 (par->synthvid_version == SYNTHVID_VERSION_WIN8 &&
931 x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8) ||
932 (par->synthvid_version == SYNTHVID_VERSION_WIN7 &&
933 (x > SYNTHVID_WIDTH_MAX_WIN7 || y > SYNTHVID_HEIGHT_MAX_WIN7))) {
934 pr_err("Screen resolution option is out of range: skipped\n");
944 /* Get framebuffer memory from Hyper-V video pci space */
945 static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info)
947 struct hvfb_par *par = info->par;
948 struct pci_dev *pdev = NULL;
949 void __iomem *fb_virt;
950 int gen2vm = efi_enabled(EFI_BOOT);
951 resource_size_t pot_start, pot_end;
955 screen_width * screen_height * screen_depth / 8;
961 pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
962 PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
964 pr_err("Unable to find PCI Hyper-V video\n");
968 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
969 pci_resource_len(pdev, 0) < screen_fb_size) {
970 pr_err("Resource not available or (0x%lx < 0x%lx)\n",
971 (unsigned long) pci_resource_len(pdev, 0),
972 (unsigned long) screen_fb_size);
976 pot_end = pci_resource_end(pdev, 0);
977 pot_start = pot_end - screen_fb_size + 1;
980 ret = vmbus_allocate_mmio(&par->mem, hdev, pot_start, pot_end,
981 screen_fb_size, 0x100000, true);
983 pr_err("Unable to allocate framebuffer memory\n");
987 fb_virt = ioremap(par->mem->start, screen_fb_size);
991 /* Allocate memory for deferred IO */
992 par->dio_vp = vzalloc(round_up(dio_fb_size, PAGE_SIZE));
993 if (par->dio_vp == NULL)
996 info->apertures = alloc_apertures(1);
997 if (!info->apertures)
1001 info->apertures->ranges[0].base = screen_info.lfb_base;
1002 info->apertures->ranges[0].size = screen_info.lfb_size;
1003 remove_conflicting_framebuffers(info->apertures,
1004 KBUILD_MODNAME, false);
1006 info->apertures->ranges[0].base = pci_resource_start(pdev, 0);
1007 info->apertures->ranges[0].size = pci_resource_len(pdev, 0);
1010 /* Physical address of FB device */
1011 par->mmio_pp = par->mem->start;
1012 /* Virtual address of FB device */
1013 par->mmio_vp = (unsigned char *) fb_virt;
1015 info->fix.smem_start = par->mem->start;
1016 info->fix.smem_len = dio_fb_size;
1017 info->screen_base = par->dio_vp;
1018 info->screen_size = dio_fb_size;
1030 vmbus_free_mmio(par->mem->start, screen_fb_size);
1039 /* Release the framebuffer */
1040 static void hvfb_putmem(struct fb_info *info)
1042 struct hvfb_par *par = info->par;
1045 iounmap(info->screen_base);
1046 vmbus_free_mmio(par->mem->start, screen_fb_size);
1051 static int hvfb_probe(struct hv_device *hdev,
1052 const struct hv_vmbus_device_id *dev_id)
1054 struct fb_info *info;
1055 struct hvfb_par *par;
1058 info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device);
1064 par->fb_ready = false;
1065 init_completion(&par->wait);
1066 INIT_DELAYED_WORK(&par->dwork, hvfb_update_work);
1068 par->delayed_refresh = false;
1069 spin_lock_init(&par->delayed_refresh_lock);
1070 par->x1 = par->y1 = INT_MAX;
1071 par->x2 = par->y2 = 0;
1073 /* Connect to VSP */
1074 hv_set_drvdata(hdev, info);
1075 ret = synthvid_connect_vsp(hdev);
1077 pr_err("Unable to connect to VSP\n");
1081 hvfb_get_option(info);
1082 pr_info("Screen resolution: %dx%d, Color depth: %d\n",
1083 screen_width, screen_height, screen_depth);
1085 ret = hvfb_getmem(hdev, info);
1087 pr_err("No memory for framebuffer\n");
1091 /* Set up fb_info */
1092 info->flags = FBINFO_DEFAULT;
1094 info->var.xres_virtual = info->var.xres = screen_width;
1095 info->var.yres_virtual = info->var.yres = screen_height;
1096 info->var.bits_per_pixel = screen_depth;
1098 if (info->var.bits_per_pixel == 16) {
1099 info->var.red = (struct fb_bitfield){11, 5, 0};
1100 info->var.green = (struct fb_bitfield){5, 6, 0};
1101 info->var.blue = (struct fb_bitfield){0, 5, 0};
1102 info->var.transp = (struct fb_bitfield){0, 0, 0};
1104 info->var.red = (struct fb_bitfield){16, 8, 0};
1105 info->var.green = (struct fb_bitfield){8, 8, 0};
1106 info->var.blue = (struct fb_bitfield){0, 8, 0};
1107 info->var.transp = (struct fb_bitfield){24, 8, 0};
1110 info->var.activate = FB_ACTIVATE_NOW;
1111 info->var.height = -1;
1112 info->var.width = -1;
1113 info->var.vmode = FB_VMODE_NONINTERLACED;
1115 strcpy(info->fix.id, KBUILD_MODNAME);
1116 info->fix.type = FB_TYPE_PACKED_PIXELS;
1117 info->fix.visual = FB_VISUAL_TRUECOLOR;
1118 info->fix.line_length = screen_width * screen_depth / 8;
1119 info->fix.accel = FB_ACCEL_NONE;
1121 info->fbops = &hvfb_ops;
1122 info->pseudo_palette = par->pseudo_palette;
1124 /* Initialize deferred IO */
1125 info->fbdefio = &synthvid_defio;
1126 fb_deferred_io_init(info);
1128 /* Send config to host */
1129 ret = synthvid_send_config(hdev);
1133 ret = register_framebuffer(info);
1135 pr_err("Unable to register framebuffer\n");
1139 par->fb_ready = true;
1141 par->synchronous_fb = false;
1142 par->hvfb_panic_nb.notifier_call = hvfb_on_panic;
1143 atomic_notifier_chain_register(&panic_notifier_list,
1144 &par->hvfb_panic_nb);
1149 fb_deferred_io_cleanup(info);
1152 vmbus_close(hdev->channel);
1154 cancel_delayed_work_sync(&par->dwork);
1155 hv_set_drvdata(hdev, NULL);
1156 framebuffer_release(info);
1161 static int hvfb_remove(struct hv_device *hdev)
1163 struct fb_info *info = hv_get_drvdata(hdev);
1164 struct hvfb_par *par = info->par;
1166 atomic_notifier_chain_unregister(&panic_notifier_list,
1167 &par->hvfb_panic_nb);
1169 par->update = false;
1170 par->fb_ready = false;
1172 fb_deferred_io_cleanup(info);
1174 unregister_framebuffer(info);
1175 cancel_delayed_work_sync(&par->dwork);
1177 vmbus_close(hdev->channel);
1178 hv_set_drvdata(hdev, NULL);
1181 framebuffer_release(info);
1186 static int hvfb_suspend(struct hv_device *hdev)
1188 struct fb_info *info = hv_get_drvdata(hdev);
1189 struct hvfb_par *par = info->par;
1193 /* 1 means do suspend */
1194 fb_set_suspend(info, 1);
1196 cancel_delayed_work_sync(&par->dwork);
1198 par->update_saved = par->update;
1199 par->update = false;
1200 par->fb_ready = false;
1202 vmbus_close(hdev->channel);
1209 static int hvfb_resume(struct hv_device *hdev)
1211 struct fb_info *info = hv_get_drvdata(hdev);
1212 struct hvfb_par *par = info->par;
1217 ret = synthvid_connect_vsp(hdev);
1221 ret = synthvid_send_config(hdev);
1223 vmbus_close(hdev->channel);
1227 par->fb_ready = true;
1228 par->update = par->update_saved;
1230 schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
1232 /* 0 means do resume */
1233 fb_set_suspend(info, 0);
1242 static const struct pci_device_id pci_stub_id_table[] = {
1244 .vendor = PCI_VENDOR_ID_MICROSOFT,
1245 .device = PCI_DEVICE_ID_HYPERV_VIDEO,
1247 { /* end of list */ }
1250 static const struct hv_vmbus_device_id id_table[] = {
1251 /* Synthetic Video Device GUID */
1256 MODULE_DEVICE_TABLE(pci, pci_stub_id_table);
1257 MODULE_DEVICE_TABLE(vmbus, id_table);
1259 static struct hv_driver hvfb_drv = {
1260 .name = KBUILD_MODNAME,
1261 .id_table = id_table,
1262 .probe = hvfb_probe,
1263 .remove = hvfb_remove,
1264 .suspend = hvfb_suspend,
1265 .resume = hvfb_resume,
1267 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1271 static int hvfb_pci_stub_probe(struct pci_dev *pdev,
1272 const struct pci_device_id *ent)
1277 static void hvfb_pci_stub_remove(struct pci_dev *pdev)
1281 static struct pci_driver hvfb_pci_stub_driver = {
1282 .name = KBUILD_MODNAME,
1283 .id_table = pci_stub_id_table,
1284 .probe = hvfb_pci_stub_probe,
1285 .remove = hvfb_pci_stub_remove,
1287 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1291 static int __init hvfb_drv_init(void)
1295 ret = vmbus_driver_register(&hvfb_drv);
1299 ret = pci_register_driver(&hvfb_pci_stub_driver);
1301 vmbus_driver_unregister(&hvfb_drv);
1308 static void __exit hvfb_drv_exit(void)
1310 pci_unregister_driver(&hvfb_pci_stub_driver);
1311 vmbus_driver_unregister(&hvfb_drv);
1314 module_init(hvfb_drv_init);
1315 module_exit(hvfb_drv_exit);
1317 MODULE_LICENSE("GPL");
1318 MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver");