xref: /openbmc/linux/drivers/video/fbdev/hyperv_fb.c (revision 465191d6)
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  * 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
34  *
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:
40  *    cma=130m
41  * This gives 130MB memory to CMA allocator that can be allocated to
42  * framebuffer. For reference, 8K resolution (7680x4320) takes about
43  * 127MB memory.
44  */
45 
46 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47 
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>
53 #include <linux/fb.h>
54 #include <linux/pci.h>
55 #include <linux/panic_notifier.h>
56 #include <linux/efi.h>
57 #include <linux/console.h>
58 
59 #include <linux/hyperv.h>
60 
61 
62 /* Hyper-V Synthetic Video Protocol definitions and structures */
63 #define MAX_VMBUS_PKT_SIZE 0x4000
64 
65 #define SYNTHVID_VERSION(major, minor) ((minor) << 16 | (major))
66 /* Support for VERSION_WIN7 is removed. #define is retained for reference. */
67 #define SYNTHVID_VERSION_WIN7 SYNTHVID_VERSION(3, 0)
68 #define SYNTHVID_VERSION_WIN8 SYNTHVID_VERSION(3, 2)
69 #define SYNTHVID_VERSION_WIN10 SYNTHVID_VERSION(3, 5)
70 
71 #define SYNTHVID_VER_GET_MAJOR(ver) (ver & 0x0000ffff)
72 #define SYNTHVID_VER_GET_MINOR(ver) ((ver & 0xffff0000) >> 16)
73 
74 #define SYNTHVID_DEPTH_WIN8 32
75 #define SYNTHVID_FB_SIZE_WIN8 (8 * 1024 * 1024)
76 
77 #define PCI_VENDOR_ID_MICROSOFT 0x1414
78 #define PCI_DEVICE_ID_HYPERV_VIDEO 0x5353
79 
80 
81 enum pipe_msg_type {
82 	PIPE_MSG_INVALID,
83 	PIPE_MSG_DATA,
84 	PIPE_MSG_MAX
85 };
86 
87 struct pipe_msg_hdr {
88 	u32 type;
89 	u32 size; /* size of message after this field */
90 } __packed;
91 
92 
93 enum synthvid_msg_type {
94 	SYNTHVID_ERROR			= 0,
95 	SYNTHVID_VERSION_REQUEST	= 1,
96 	SYNTHVID_VERSION_RESPONSE	= 2,
97 	SYNTHVID_VRAM_LOCATION		= 3,
98 	SYNTHVID_VRAM_LOCATION_ACK	= 4,
99 	SYNTHVID_SITUATION_UPDATE	= 5,
100 	SYNTHVID_SITUATION_UPDATE_ACK	= 6,
101 	SYNTHVID_POINTER_POSITION	= 7,
102 	SYNTHVID_POINTER_SHAPE		= 8,
103 	SYNTHVID_FEATURE_CHANGE		= 9,
104 	SYNTHVID_DIRT			= 10,
105 	SYNTHVID_RESOLUTION_REQUEST	= 13,
106 	SYNTHVID_RESOLUTION_RESPONSE	= 14,
107 
108 	SYNTHVID_MAX			= 15
109 };
110 
111 #define		SYNTHVID_EDID_BLOCK_SIZE	128
112 #define		SYNTHVID_MAX_RESOLUTION_COUNT	64
113 
114 struct hvd_screen_info {
115 	u16 width;
116 	u16 height;
117 } __packed;
118 
119 struct synthvid_msg_hdr {
120 	u32 type;
121 	u32 size;  /* size of this header + payload after this field*/
122 } __packed;
123 
124 struct synthvid_version_req {
125 	u32 version;
126 } __packed;
127 
128 struct synthvid_version_resp {
129 	u32 version;
130 	u8 is_accepted;
131 	u8 max_video_outputs;
132 } __packed;
133 
134 struct synthvid_supported_resolution_req {
135 	u8 maximum_resolution_count;
136 } __packed;
137 
138 struct synthvid_supported_resolution_resp {
139 	u8 edid_block[SYNTHVID_EDID_BLOCK_SIZE];
140 	u8 resolution_count;
141 	u8 default_resolution_index;
142 	u8 is_standard;
143 	struct hvd_screen_info
144 		supported_resolution[SYNTHVID_MAX_RESOLUTION_COUNT];
145 } __packed;
146 
147 struct synthvid_vram_location {
148 	u64 user_ctx;
149 	u8 is_vram_gpa_specified;
150 	u64 vram_gpa;
151 } __packed;
152 
153 struct synthvid_vram_location_ack {
154 	u64 user_ctx;
155 } __packed;
156 
157 struct video_output_situation {
158 	u8 active;
159 	u32 vram_offset;
160 	u8 depth_bits;
161 	u32 width_pixels;
162 	u32 height_pixels;
163 	u32 pitch_bytes;
164 } __packed;
165 
166 struct synthvid_situation_update {
167 	u64 user_ctx;
168 	u8 video_output_count;
169 	struct video_output_situation video_output[1];
170 } __packed;
171 
172 struct synthvid_situation_update_ack {
173 	u64 user_ctx;
174 } __packed;
175 
176 struct synthvid_pointer_position {
177 	u8 is_visible;
178 	u8 video_output;
179 	s32 image_x;
180 	s32 image_y;
181 } __packed;
182 
183 
184 #define CURSOR_MAX_X 96
185 #define CURSOR_MAX_Y 96
186 #define CURSOR_ARGB_PIXEL_SIZE 4
187 #define CURSOR_MAX_SIZE (CURSOR_MAX_X * CURSOR_MAX_Y * CURSOR_ARGB_PIXEL_SIZE)
188 #define CURSOR_COMPLETE (-1)
189 
190 struct synthvid_pointer_shape {
191 	u8 part_idx;
192 	u8 is_argb;
193 	u32 width; /* CURSOR_MAX_X at most */
194 	u32 height; /* CURSOR_MAX_Y at most */
195 	u32 hot_x; /* hotspot relative to upper-left of pointer image */
196 	u32 hot_y;
197 	u8 data[4];
198 } __packed;
199 
200 struct synthvid_feature_change {
201 	u8 is_dirt_needed;
202 	u8 is_ptr_pos_needed;
203 	u8 is_ptr_shape_needed;
204 	u8 is_situ_needed;
205 } __packed;
206 
207 struct rect {
208 	s32 x1, y1; /* top left corner */
209 	s32 x2, y2; /* bottom right corner, exclusive */
210 } __packed;
211 
212 struct synthvid_dirt {
213 	u8 video_output;
214 	u8 dirt_count;
215 	struct rect rect[1];
216 } __packed;
217 
218 struct synthvid_msg {
219 	struct pipe_msg_hdr pipe_hdr;
220 	struct synthvid_msg_hdr vid_hdr;
221 	union {
222 		struct synthvid_version_req ver_req;
223 		struct synthvid_version_resp ver_resp;
224 		struct synthvid_vram_location vram;
225 		struct synthvid_vram_location_ack vram_ack;
226 		struct synthvid_situation_update situ;
227 		struct synthvid_situation_update_ack situ_ack;
228 		struct synthvid_pointer_position ptr_pos;
229 		struct synthvid_pointer_shape ptr_shape;
230 		struct synthvid_feature_change feature_chg;
231 		struct synthvid_dirt dirt;
232 		struct synthvid_supported_resolution_req resolution_req;
233 		struct synthvid_supported_resolution_resp resolution_resp;
234 	};
235 } __packed;
236 
237 
238 /* FB driver definitions and structures */
239 #define HVFB_WIDTH 1152 /* default screen width */
240 #define HVFB_HEIGHT 864 /* default screen height */
241 #define HVFB_WIDTH_MIN 640
242 #define HVFB_HEIGHT_MIN 480
243 
244 #define RING_BUFSIZE (256 * 1024)
245 #define VSP_TIMEOUT (10 * HZ)
246 #define HVFB_UPDATE_DELAY (HZ / 20)
247 #define HVFB_ONDEMAND_THROTTLE (HZ / 20)
248 
249 struct hvfb_par {
250 	struct fb_info *info;
251 	struct resource *mem;
252 	bool fb_ready; /* fb device is ready */
253 	struct completion wait;
254 	u32 synthvid_version;
255 
256 	struct delayed_work dwork;
257 	bool update;
258 	bool update_saved; /* The value of 'update' before hibernation */
259 
260 	u32 pseudo_palette[16];
261 	u8 init_buf[MAX_VMBUS_PKT_SIZE];
262 	u8 recv_buf[MAX_VMBUS_PKT_SIZE];
263 
264 	/* If true, the VSC notifies the VSP on every framebuffer change */
265 	bool synchronous_fb;
266 
267 	/* If true, need to copy from deferred IO mem to framebuffer mem */
268 	bool need_docopy;
269 
270 	struct notifier_block hvfb_panic_nb;
271 
272 	/* Memory for deferred IO and frame buffer itself */
273 	unsigned char *dio_vp;
274 	unsigned char *mmio_vp;
275 	phys_addr_t mmio_pp;
276 
277 	/* Dirty rectangle, protected by delayed_refresh_lock */
278 	int x1, y1, x2, y2;
279 	bool delayed_refresh;
280 	spinlock_t delayed_refresh_lock;
281 };
282 
283 static uint screen_width = HVFB_WIDTH;
284 static uint screen_height = HVFB_HEIGHT;
285 static uint screen_depth;
286 static uint screen_fb_size;
287 static uint dio_fb_size; /* FB size for deferred IO */
288 
289 /* Send message to Hyper-V host */
290 static inline int synthvid_send(struct hv_device *hdev,
291 				struct synthvid_msg *msg)
292 {
293 	static atomic64_t request_id = ATOMIC64_INIT(0);
294 	int ret;
295 
296 	msg->pipe_hdr.type = PIPE_MSG_DATA;
297 	msg->pipe_hdr.size = msg->vid_hdr.size;
298 
299 	ret = vmbus_sendpacket(hdev->channel, msg,
300 			       msg->vid_hdr.size + sizeof(struct pipe_msg_hdr),
301 			       atomic64_inc_return(&request_id),
302 			       VM_PKT_DATA_INBAND, 0);
303 
304 	if (ret)
305 		pr_err_ratelimited("Unable to send packet via vmbus; error %d\n", ret);
306 
307 	return ret;
308 }
309 
310 
311 /* Send screen resolution info to host */
312 static int synthvid_send_situ(struct hv_device *hdev)
313 {
314 	struct fb_info *info = hv_get_drvdata(hdev);
315 	struct synthvid_msg msg;
316 
317 	if (!info)
318 		return -ENODEV;
319 
320 	memset(&msg, 0, sizeof(struct synthvid_msg));
321 
322 	msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE;
323 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
324 		sizeof(struct synthvid_situation_update);
325 	msg.situ.user_ctx = 0;
326 	msg.situ.video_output_count = 1;
327 	msg.situ.video_output[0].active = 1;
328 	msg.situ.video_output[0].vram_offset = 0;
329 	msg.situ.video_output[0].depth_bits = info->var.bits_per_pixel;
330 	msg.situ.video_output[0].width_pixels = info->var.xres;
331 	msg.situ.video_output[0].height_pixels = info->var.yres;
332 	msg.situ.video_output[0].pitch_bytes = info->fix.line_length;
333 
334 	synthvid_send(hdev, &msg);
335 
336 	return 0;
337 }
338 
339 /* Send mouse pointer info to host */
340 static int synthvid_send_ptr(struct hv_device *hdev)
341 {
342 	struct synthvid_msg msg;
343 
344 	memset(&msg, 0, sizeof(struct synthvid_msg));
345 	msg.vid_hdr.type = SYNTHVID_POINTER_POSITION;
346 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
347 		sizeof(struct synthvid_pointer_position);
348 	msg.ptr_pos.is_visible = 1;
349 	msg.ptr_pos.video_output = 0;
350 	msg.ptr_pos.image_x = 0;
351 	msg.ptr_pos.image_y = 0;
352 	synthvid_send(hdev, &msg);
353 
354 	memset(&msg, 0, sizeof(struct synthvid_msg));
355 	msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE;
356 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
357 		sizeof(struct synthvid_pointer_shape);
358 	msg.ptr_shape.part_idx = CURSOR_COMPLETE;
359 	msg.ptr_shape.is_argb = 1;
360 	msg.ptr_shape.width = 1;
361 	msg.ptr_shape.height = 1;
362 	msg.ptr_shape.hot_x = 0;
363 	msg.ptr_shape.hot_y = 0;
364 	msg.ptr_shape.data[0] = 0;
365 	msg.ptr_shape.data[1] = 1;
366 	msg.ptr_shape.data[2] = 1;
367 	msg.ptr_shape.data[3] = 1;
368 	synthvid_send(hdev, &msg);
369 
370 	return 0;
371 }
372 
373 /* Send updated screen area (dirty rectangle) location to host */
374 static int
375 synthvid_update(struct fb_info *info, int x1, int y1, int x2, int y2)
376 {
377 	struct hv_device *hdev = device_to_hv_device(info->device);
378 	struct synthvid_msg msg;
379 
380 	memset(&msg, 0, sizeof(struct synthvid_msg));
381 	if (x2 == INT_MAX)
382 		x2 = info->var.xres;
383 	if (y2 == INT_MAX)
384 		y2 = info->var.yres;
385 
386 	msg.vid_hdr.type = SYNTHVID_DIRT;
387 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
388 		sizeof(struct synthvid_dirt);
389 	msg.dirt.video_output = 0;
390 	msg.dirt.dirt_count = 1;
391 	msg.dirt.rect[0].x1 = (x1 > x2) ? 0 : x1;
392 	msg.dirt.rect[0].y1 = (y1 > y2) ? 0 : y1;
393 	msg.dirt.rect[0].x2 =
394 		(x2 < x1 || x2 > info->var.xres) ? info->var.xres : x2;
395 	msg.dirt.rect[0].y2 =
396 		(y2 < y1 || y2 > info->var.yres) ? info->var.yres : y2;
397 
398 	synthvid_send(hdev, &msg);
399 
400 	return 0;
401 }
402 
403 static void hvfb_docopy(struct hvfb_par *par,
404 			unsigned long offset,
405 			unsigned long size)
406 {
407 	if (!par || !par->mmio_vp || !par->dio_vp || !par->fb_ready ||
408 	    size == 0 || offset >= dio_fb_size)
409 		return;
410 
411 	if (offset + size > dio_fb_size)
412 		size = dio_fb_size - offset;
413 
414 	memcpy(par->mmio_vp + offset, par->dio_vp + offset, size);
415 }
416 
417 /* Deferred IO callback */
418 static void synthvid_deferred_io(struct fb_info *p, struct list_head *pagereflist)
419 {
420 	struct hvfb_par *par = p->par;
421 	struct fb_deferred_io_pageref *pageref;
422 	unsigned long start, end;
423 	int y1, y2, miny, maxy;
424 
425 	miny = INT_MAX;
426 	maxy = 0;
427 
428 	/*
429 	 * Merge dirty pages. It is possible that last page cross
430 	 * over the end of frame buffer row yres. This is taken care of
431 	 * in synthvid_update function by clamping the y2
432 	 * value to yres.
433 	 */
434 	list_for_each_entry(pageref, pagereflist, list) {
435 		start = pageref->offset;
436 		end = start + PAGE_SIZE - 1;
437 		y1 = start / p->fix.line_length;
438 		y2 = end / p->fix.line_length;
439 		miny = min_t(int, miny, y1);
440 		maxy = max_t(int, maxy, y2);
441 
442 		/* Copy from dio space to mmio address */
443 		if (par->fb_ready && par->need_docopy)
444 			hvfb_docopy(par, start, PAGE_SIZE);
445 	}
446 
447 	if (par->fb_ready && par->update)
448 		synthvid_update(p, 0, miny, p->var.xres, maxy + 1);
449 }
450 
451 static struct fb_deferred_io synthvid_defio = {
452 	.delay		= HZ / 20,
453 	.deferred_io	= synthvid_deferred_io,
454 };
455 
456 /*
457  * Actions on received messages from host:
458  * Complete the wait event.
459  * Or, reply with screen and cursor info.
460  */
461 static void synthvid_recv_sub(struct hv_device *hdev)
462 {
463 	struct fb_info *info = hv_get_drvdata(hdev);
464 	struct hvfb_par *par;
465 	struct synthvid_msg *msg;
466 
467 	if (!info)
468 		return;
469 
470 	par = info->par;
471 	msg = (struct synthvid_msg *)par->recv_buf;
472 
473 	/* Complete the wait event */
474 	if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE ||
475 	    msg->vid_hdr.type == SYNTHVID_RESOLUTION_RESPONSE ||
476 	    msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) {
477 		memcpy(par->init_buf, msg, MAX_VMBUS_PKT_SIZE);
478 		complete(&par->wait);
479 		return;
480 	}
481 
482 	/* Reply with screen and cursor info */
483 	if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) {
484 		if (par->fb_ready) {
485 			synthvid_send_ptr(hdev);
486 			synthvid_send_situ(hdev);
487 		}
488 
489 		par->update = msg->feature_chg.is_dirt_needed;
490 		if (par->update)
491 			schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
492 	}
493 }
494 
495 /* Receive callback for messages from the host */
496 static void synthvid_receive(void *ctx)
497 {
498 	struct hv_device *hdev = ctx;
499 	struct fb_info *info = hv_get_drvdata(hdev);
500 	struct hvfb_par *par;
501 	struct synthvid_msg *recv_buf;
502 	u32 bytes_recvd;
503 	u64 req_id;
504 	int ret;
505 
506 	if (!info)
507 		return;
508 
509 	par = info->par;
510 	recv_buf = (struct synthvid_msg *)par->recv_buf;
511 
512 	do {
513 		ret = vmbus_recvpacket(hdev->channel, recv_buf,
514 				       MAX_VMBUS_PKT_SIZE,
515 				       &bytes_recvd, &req_id);
516 		if (bytes_recvd > 0 &&
517 		    recv_buf->pipe_hdr.type == PIPE_MSG_DATA)
518 			synthvid_recv_sub(hdev);
519 	} while (bytes_recvd > 0 && ret == 0);
520 }
521 
522 /* Check if the ver1 version is equal or greater than ver2 */
523 static inline bool synthvid_ver_ge(u32 ver1, u32 ver2)
524 {
525 	if (SYNTHVID_VER_GET_MAJOR(ver1) > SYNTHVID_VER_GET_MAJOR(ver2) ||
526 	    (SYNTHVID_VER_GET_MAJOR(ver1) == SYNTHVID_VER_GET_MAJOR(ver2) &&
527 	     SYNTHVID_VER_GET_MINOR(ver1) >= SYNTHVID_VER_GET_MINOR(ver2)))
528 		return true;
529 
530 	return false;
531 }
532 
533 /* Check synthetic video protocol version with the host */
534 static int synthvid_negotiate_ver(struct hv_device *hdev, u32 ver)
535 {
536 	struct fb_info *info = hv_get_drvdata(hdev);
537 	struct hvfb_par *par = info->par;
538 	struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
539 	int ret = 0;
540 	unsigned long t;
541 
542 	memset(msg, 0, sizeof(struct synthvid_msg));
543 	msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST;
544 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
545 		sizeof(struct synthvid_version_req);
546 	msg->ver_req.version = ver;
547 	synthvid_send(hdev, msg);
548 
549 	t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
550 	if (!t) {
551 		pr_err("Time out on waiting version response\n");
552 		ret = -ETIMEDOUT;
553 		goto out;
554 	}
555 	if (!msg->ver_resp.is_accepted) {
556 		ret = -ENODEV;
557 		goto out;
558 	}
559 
560 	par->synthvid_version = ver;
561 	pr_info("Synthvid Version major %d, minor %d\n",
562 		SYNTHVID_VER_GET_MAJOR(ver), SYNTHVID_VER_GET_MINOR(ver));
563 
564 out:
565 	return ret;
566 }
567 
568 /* Get current resolution from the host */
569 static int synthvid_get_supported_resolution(struct hv_device *hdev)
570 {
571 	struct fb_info *info = hv_get_drvdata(hdev);
572 	struct hvfb_par *par = info->par;
573 	struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
574 	int ret = 0;
575 	unsigned long t;
576 	u8 index;
577 
578 	memset(msg, 0, sizeof(struct synthvid_msg));
579 	msg->vid_hdr.type = SYNTHVID_RESOLUTION_REQUEST;
580 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
581 		sizeof(struct synthvid_supported_resolution_req);
582 
583 	msg->resolution_req.maximum_resolution_count =
584 		SYNTHVID_MAX_RESOLUTION_COUNT;
585 	synthvid_send(hdev, msg);
586 
587 	t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
588 	if (!t) {
589 		pr_err("Time out on waiting resolution response\n");
590 		ret = -ETIMEDOUT;
591 		goto out;
592 	}
593 
594 	if (msg->resolution_resp.resolution_count == 0) {
595 		pr_err("No supported resolutions\n");
596 		ret = -ENODEV;
597 		goto out;
598 	}
599 
600 	index = msg->resolution_resp.default_resolution_index;
601 	if (index >= msg->resolution_resp.resolution_count) {
602 		pr_err("Invalid resolution index: %d\n", index);
603 		ret = -ENODEV;
604 		goto out;
605 	}
606 
607 	screen_width =
608 		msg->resolution_resp.supported_resolution[index].width;
609 	screen_height =
610 		msg->resolution_resp.supported_resolution[index].height;
611 
612 out:
613 	return ret;
614 }
615 
616 /* Connect to VSP (Virtual Service Provider) on host */
617 static int synthvid_connect_vsp(struct hv_device *hdev)
618 {
619 	struct fb_info *info = hv_get_drvdata(hdev);
620 	struct hvfb_par *par = info->par;
621 	int ret;
622 
623 	ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE,
624 			 NULL, 0, synthvid_receive, hdev);
625 	if (ret) {
626 		pr_err("Unable to open vmbus channel\n");
627 		return ret;
628 	}
629 
630 	/* Negotiate the protocol version with host */
631 	switch (vmbus_proto_version) {
632 	case VERSION_WIN10:
633 	case VERSION_WIN10_V5:
634 		ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
635 		if (!ret)
636 			break;
637 		fallthrough;
638 	case VERSION_WIN8:
639 	case VERSION_WIN8_1:
640 		ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8);
641 		break;
642 	default:
643 		ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
644 		break;
645 	}
646 
647 	if (ret) {
648 		pr_err("Synthetic video device version not accepted\n");
649 		goto error;
650 	}
651 
652 	screen_depth = SYNTHVID_DEPTH_WIN8;
653 	if (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10)) {
654 		ret = synthvid_get_supported_resolution(hdev);
655 		if (ret)
656 			pr_info("Failed to get supported resolution from host, use default\n");
657 	}
658 
659 	screen_fb_size = hdev->channel->offermsg.offer.
660 				mmio_megabytes * 1024 * 1024;
661 
662 	return 0;
663 
664 error:
665 	vmbus_close(hdev->channel);
666 	return ret;
667 }
668 
669 /* Send VRAM and Situation messages to the host */
670 static int synthvid_send_config(struct hv_device *hdev)
671 {
672 	struct fb_info *info = hv_get_drvdata(hdev);
673 	struct hvfb_par *par = info->par;
674 	struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
675 	int ret = 0;
676 	unsigned long t;
677 
678 	/* Send VRAM location */
679 	memset(msg, 0, sizeof(struct synthvid_msg));
680 	msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION;
681 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
682 		sizeof(struct synthvid_vram_location);
683 	msg->vram.user_ctx = msg->vram.vram_gpa = par->mmio_pp;
684 	msg->vram.is_vram_gpa_specified = 1;
685 	synthvid_send(hdev, msg);
686 
687 	t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
688 	if (!t) {
689 		pr_err("Time out on waiting vram location ack\n");
690 		ret = -ETIMEDOUT;
691 		goto out;
692 	}
693 	if (msg->vram_ack.user_ctx != par->mmio_pp) {
694 		pr_err("Unable to set VRAM location\n");
695 		ret = -ENODEV;
696 		goto out;
697 	}
698 
699 	/* Send pointer and situation update */
700 	synthvid_send_ptr(hdev);
701 	synthvid_send_situ(hdev);
702 
703 out:
704 	return ret;
705 }
706 
707 
708 /*
709  * Delayed work callback:
710  * It is scheduled to call whenever update request is received and it has
711  * not been called in last HVFB_ONDEMAND_THROTTLE time interval.
712  */
713 static void hvfb_update_work(struct work_struct *w)
714 {
715 	struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work);
716 	struct fb_info *info = par->info;
717 	unsigned long flags;
718 	int x1, x2, y1, y2;
719 	int j;
720 
721 	spin_lock_irqsave(&par->delayed_refresh_lock, flags);
722 	/* Reset the request flag */
723 	par->delayed_refresh = false;
724 
725 	/* Store the dirty rectangle to local variables */
726 	x1 = par->x1;
727 	x2 = par->x2;
728 	y1 = par->y1;
729 	y2 = par->y2;
730 
731 	/* Clear dirty rectangle */
732 	par->x1 = par->y1 = INT_MAX;
733 	par->x2 = par->y2 = 0;
734 
735 	spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
736 
737 	if (x1 > info->var.xres || x2 > info->var.xres ||
738 	    y1 > info->var.yres || y2 > info->var.yres || x2 <= x1)
739 		return;
740 
741 	/* Copy the dirty rectangle to frame buffer memory */
742 	if (par->need_docopy)
743 		for (j = y1; j < y2; j++)
744 			hvfb_docopy(par,
745 				    j * info->fix.line_length +
746 				    (x1 * screen_depth / 8),
747 				    (x2 - x1) * screen_depth / 8);
748 
749 	/* Refresh */
750 	if (par->fb_ready && par->update)
751 		synthvid_update(info, x1, y1, x2, y2);
752 }
753 
754 /*
755  * Control the on-demand refresh frequency. It schedules a delayed
756  * screen update if it has not yet.
757  */
758 static void hvfb_ondemand_refresh_throttle(struct hvfb_par *par,
759 					   int x1, int y1, int w, int h)
760 {
761 	unsigned long flags;
762 	int x2 = x1 + w;
763 	int y2 = y1 + h;
764 
765 	spin_lock_irqsave(&par->delayed_refresh_lock, flags);
766 
767 	/* Merge dirty rectangle */
768 	par->x1 = min_t(int, par->x1, x1);
769 	par->y1 = min_t(int, par->y1, y1);
770 	par->x2 = max_t(int, par->x2, x2);
771 	par->y2 = max_t(int, par->y2, y2);
772 
773 	/* Schedule a delayed screen update if not yet */
774 	if (par->delayed_refresh == false) {
775 		schedule_delayed_work(&par->dwork,
776 				      HVFB_ONDEMAND_THROTTLE);
777 		par->delayed_refresh = true;
778 	}
779 
780 	spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
781 }
782 
783 static int hvfb_on_panic(struct notifier_block *nb,
784 			 unsigned long e, void *p)
785 {
786 	struct hvfb_par *par;
787 	struct fb_info *info;
788 
789 	par = container_of(nb, struct hvfb_par, hvfb_panic_nb);
790 	par->synchronous_fb = true;
791 	info = par->info;
792 	if (par->need_docopy)
793 		hvfb_docopy(par, 0, dio_fb_size);
794 	synthvid_update(info, 0, 0, INT_MAX, INT_MAX);
795 
796 	return NOTIFY_DONE;
797 }
798 
799 /* Framebuffer operation handlers */
800 
801 static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
802 {
803 	if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN ||
804 	    var->xres > screen_width || var->yres >  screen_height ||
805 	    var->bits_per_pixel != screen_depth)
806 		return -EINVAL;
807 
808 	var->xres_virtual = var->xres;
809 	var->yres_virtual = var->yres;
810 
811 	return 0;
812 }
813 
814 static int hvfb_set_par(struct fb_info *info)
815 {
816 	struct hv_device *hdev = device_to_hv_device(info->device);
817 
818 	return synthvid_send_situ(hdev);
819 }
820 
821 
822 static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf)
823 {
824 	return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset;
825 }
826 
827 static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
828 			  unsigned blue, unsigned transp, struct fb_info *info)
829 {
830 	u32 *pal = info->pseudo_palette;
831 
832 	if (regno > 15)
833 		return -EINVAL;
834 
835 	pal[regno] = chan_to_field(red, &info->var.red)
836 		| chan_to_field(green, &info->var.green)
837 		| chan_to_field(blue, &info->var.blue)
838 		| chan_to_field(transp, &info->var.transp);
839 
840 	return 0;
841 }
842 
843 static int hvfb_blank(int blank, struct fb_info *info)
844 {
845 	return 1;	/* get fb_blank to set the colormap to all black */
846 }
847 
848 static void hvfb_cfb_fillrect(struct fb_info *p,
849 			      const struct fb_fillrect *rect)
850 {
851 	struct hvfb_par *par = p->par;
852 
853 	cfb_fillrect(p, rect);
854 	if (par->synchronous_fb)
855 		synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
856 	else
857 		hvfb_ondemand_refresh_throttle(par, rect->dx, rect->dy,
858 					       rect->width, rect->height);
859 }
860 
861 static void hvfb_cfb_copyarea(struct fb_info *p,
862 			      const struct fb_copyarea *area)
863 {
864 	struct hvfb_par *par = p->par;
865 
866 	cfb_copyarea(p, area);
867 	if (par->synchronous_fb)
868 		synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
869 	else
870 		hvfb_ondemand_refresh_throttle(par, area->dx, area->dy,
871 					       area->width, area->height);
872 }
873 
874 static void hvfb_cfb_imageblit(struct fb_info *p,
875 			       const struct fb_image *image)
876 {
877 	struct hvfb_par *par = p->par;
878 
879 	cfb_imageblit(p, image);
880 	if (par->synchronous_fb)
881 		synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
882 	else
883 		hvfb_ondemand_refresh_throttle(par, image->dx, image->dy,
884 					       image->width, image->height);
885 }
886 
887 static const struct fb_ops hvfb_ops = {
888 	.owner = THIS_MODULE,
889 	.fb_check_var = hvfb_check_var,
890 	.fb_set_par = hvfb_set_par,
891 	.fb_setcolreg = hvfb_setcolreg,
892 	.fb_fillrect = hvfb_cfb_fillrect,
893 	.fb_copyarea = hvfb_cfb_copyarea,
894 	.fb_imageblit = hvfb_cfb_imageblit,
895 	.fb_blank = hvfb_blank,
896 	.fb_mmap = fb_deferred_io_mmap,
897 };
898 
899 
900 /* Get options from kernel paramenter "video=" */
901 static void hvfb_get_option(struct fb_info *info)
902 {
903 	struct hvfb_par *par = info->par;
904 	char *opt = NULL, *p;
905 	uint x = 0, y = 0;
906 
907 	if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt)
908 		return;
909 
910 	p = strsep(&opt, "x");
911 	if (!*p || kstrtouint(p, 0, &x) ||
912 	    !opt || !*opt || kstrtouint(opt, 0, &y)) {
913 		pr_err("Screen option is invalid: skipped\n");
914 		return;
915 	}
916 
917 	if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN ||
918 	    (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10) &&
919 	    (x * y * screen_depth / 8 > screen_fb_size)) ||
920 	    (par->synthvid_version == SYNTHVID_VERSION_WIN8 &&
921 	     x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8)) {
922 		pr_err("Screen resolution option is out of range: skipped\n");
923 		return;
924 	}
925 
926 	screen_width = x;
927 	screen_height = y;
928 	return;
929 }
930 
931 /*
932  * Allocate enough contiguous physical memory.
933  * Return physical address if succeeded or -1 if failed.
934  */
935 static phys_addr_t hvfb_get_phymem(struct hv_device *hdev,
936 				   unsigned int request_size)
937 {
938 	struct page *page = NULL;
939 	dma_addr_t dma_handle;
940 	void *vmem;
941 	phys_addr_t paddr = 0;
942 	unsigned int order = get_order(request_size);
943 
944 	if (request_size == 0)
945 		return -1;
946 
947 	if (order < MAX_ORDER) {
948 		/* Call alloc_pages if the size is less than 2^MAX_ORDER */
949 		page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
950 		if (!page)
951 			return -1;
952 
953 		paddr = (page_to_pfn(page) << PAGE_SHIFT);
954 	} else {
955 		/* Allocate from CMA */
956 		hdev->device.coherent_dma_mask = DMA_BIT_MASK(64);
957 
958 		vmem = dma_alloc_coherent(&hdev->device,
959 					  round_up(request_size, PAGE_SIZE),
960 					  &dma_handle,
961 					  GFP_KERNEL | __GFP_NOWARN);
962 
963 		if (!vmem)
964 			return -1;
965 
966 		paddr = virt_to_phys(vmem);
967 	}
968 
969 	return paddr;
970 }
971 
972 /* Release contiguous physical memory */
973 static void hvfb_release_phymem(struct hv_device *hdev,
974 				phys_addr_t paddr, unsigned int size)
975 {
976 	unsigned int order = get_order(size);
977 
978 	if (order < MAX_ORDER)
979 		__free_pages(pfn_to_page(paddr >> PAGE_SHIFT), order);
980 	else
981 		dma_free_coherent(&hdev->device,
982 				  round_up(size, PAGE_SIZE),
983 				  phys_to_virt(paddr),
984 				  paddr);
985 }
986 
987 
988 /* Get framebuffer memory from Hyper-V video pci space */
989 static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info)
990 {
991 	struct hvfb_par *par = info->par;
992 	struct pci_dev *pdev  = NULL;
993 	void __iomem *fb_virt;
994 	int gen2vm = efi_enabled(EFI_BOOT);
995 	phys_addr_t paddr;
996 	int ret;
997 
998 	info->apertures = alloc_apertures(1);
999 	if (!info->apertures)
1000 		return -ENOMEM;
1001 
1002 	if (!gen2vm) {
1003 		pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
1004 			PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
1005 		if (!pdev) {
1006 			pr_err("Unable to find PCI Hyper-V video\n");
1007 			return -ENODEV;
1008 		}
1009 
1010 		info->apertures->ranges[0].base = pci_resource_start(pdev, 0);
1011 		info->apertures->ranges[0].size = pci_resource_len(pdev, 0);
1012 
1013 		/*
1014 		 * For Gen 1 VM, we can directly use the contiguous memory
1015 		 * from VM. If we succeed, deferred IO happens directly
1016 		 * on this allocated framebuffer memory, avoiding extra
1017 		 * memory copy.
1018 		 */
1019 		paddr = hvfb_get_phymem(hdev, screen_fb_size);
1020 		if (paddr != (phys_addr_t) -1) {
1021 			par->mmio_pp = paddr;
1022 			par->mmio_vp = par->dio_vp = __va(paddr);
1023 
1024 			info->fix.smem_start = paddr;
1025 			info->fix.smem_len = screen_fb_size;
1026 			info->screen_base = par->mmio_vp;
1027 			info->screen_size = screen_fb_size;
1028 
1029 			par->need_docopy = false;
1030 			goto getmem_done;
1031 		}
1032 		pr_info("Unable to allocate enough contiguous physical memory on Gen 1 VM. Using MMIO instead.\n");
1033 	} else {
1034 		info->apertures->ranges[0].base = screen_info.lfb_base;
1035 		info->apertures->ranges[0].size = screen_info.lfb_size;
1036 	}
1037 
1038 	/*
1039 	 * Cannot use the contiguous physical memory.
1040 	 * Allocate mmio space for framebuffer.
1041 	 */
1042 	dio_fb_size =
1043 		screen_width * screen_height * screen_depth / 8;
1044 
1045 	ret = vmbus_allocate_mmio(&par->mem, hdev, 0, -1,
1046 				  screen_fb_size, 0x100000, true);
1047 	if (ret != 0) {
1048 		pr_err("Unable to allocate framebuffer memory\n");
1049 		goto err1;
1050 	}
1051 
1052 	/*
1053 	 * Map the VRAM cacheable for performance. This is also required for
1054 	 * VM Connect to display properly for ARM64 Linux VM, as the host also
1055 	 * maps the VRAM cacheable.
1056 	 */
1057 	fb_virt = ioremap_cache(par->mem->start, screen_fb_size);
1058 	if (!fb_virt)
1059 		goto err2;
1060 
1061 	/* Allocate memory for deferred IO */
1062 	par->dio_vp = vzalloc(round_up(dio_fb_size, PAGE_SIZE));
1063 	if (par->dio_vp == NULL)
1064 		goto err3;
1065 
1066 	/* Physical address of FB device */
1067 	par->mmio_pp = par->mem->start;
1068 	/* Virtual address of FB device */
1069 	par->mmio_vp = (unsigned char *) fb_virt;
1070 
1071 	info->fix.smem_start = par->mem->start;
1072 	info->fix.smem_len = dio_fb_size;
1073 	info->screen_base = par->dio_vp;
1074 	info->screen_size = dio_fb_size;
1075 
1076 getmem_done:
1077 	remove_conflicting_framebuffers(info->apertures,
1078 					KBUILD_MODNAME, false);
1079 
1080 	if (gen2vm) {
1081 		/* framebuffer is reallocated, clear screen_info to avoid misuse from kexec */
1082 		screen_info.lfb_size = 0;
1083 		screen_info.lfb_base = 0;
1084 		screen_info.orig_video_isVGA = 0;
1085 	} else {
1086 		pci_dev_put(pdev);
1087 	}
1088 
1089 	return 0;
1090 
1091 err3:
1092 	iounmap(fb_virt);
1093 err2:
1094 	vmbus_free_mmio(par->mem->start, screen_fb_size);
1095 	par->mem = NULL;
1096 err1:
1097 	if (!gen2vm)
1098 		pci_dev_put(pdev);
1099 
1100 	return -ENOMEM;
1101 }
1102 
1103 /* Release the framebuffer */
1104 static void hvfb_putmem(struct hv_device *hdev, struct fb_info *info)
1105 {
1106 	struct hvfb_par *par = info->par;
1107 
1108 	if (par->need_docopy) {
1109 		vfree(par->dio_vp);
1110 		iounmap(info->screen_base);
1111 		vmbus_free_mmio(par->mem->start, screen_fb_size);
1112 	} else {
1113 		hvfb_release_phymem(hdev, info->fix.smem_start,
1114 				    screen_fb_size);
1115 	}
1116 
1117 	par->mem = NULL;
1118 }
1119 
1120 
1121 static int hvfb_probe(struct hv_device *hdev,
1122 		      const struct hv_vmbus_device_id *dev_id)
1123 {
1124 	struct fb_info *info;
1125 	struct hvfb_par *par;
1126 	int ret;
1127 
1128 	info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device);
1129 	if (!info)
1130 		return -ENOMEM;
1131 
1132 	par = info->par;
1133 	par->info = info;
1134 	par->fb_ready = false;
1135 	par->need_docopy = true;
1136 	init_completion(&par->wait);
1137 	INIT_DELAYED_WORK(&par->dwork, hvfb_update_work);
1138 
1139 	par->delayed_refresh = false;
1140 	spin_lock_init(&par->delayed_refresh_lock);
1141 	par->x1 = par->y1 = INT_MAX;
1142 	par->x2 = par->y2 = 0;
1143 
1144 	/* Connect to VSP */
1145 	hv_set_drvdata(hdev, info);
1146 	ret = synthvid_connect_vsp(hdev);
1147 	if (ret) {
1148 		pr_err("Unable to connect to VSP\n");
1149 		goto error1;
1150 	}
1151 
1152 	hvfb_get_option(info);
1153 	pr_info("Screen resolution: %dx%d, Color depth: %d, Frame buffer size: %d\n",
1154 		screen_width, screen_height, screen_depth, screen_fb_size);
1155 
1156 	ret = hvfb_getmem(hdev, info);
1157 	if (ret) {
1158 		pr_err("No memory for framebuffer\n");
1159 		goto error2;
1160 	}
1161 
1162 	/* Set up fb_info */
1163 	info->flags = FBINFO_DEFAULT;
1164 
1165 	info->var.xres_virtual = info->var.xres = screen_width;
1166 	info->var.yres_virtual = info->var.yres = screen_height;
1167 	info->var.bits_per_pixel = screen_depth;
1168 
1169 	if (info->var.bits_per_pixel == 16) {
1170 		info->var.red = (struct fb_bitfield){11, 5, 0};
1171 		info->var.green = (struct fb_bitfield){5, 6, 0};
1172 		info->var.blue = (struct fb_bitfield){0, 5, 0};
1173 		info->var.transp = (struct fb_bitfield){0, 0, 0};
1174 	} else {
1175 		info->var.red = (struct fb_bitfield){16, 8, 0};
1176 		info->var.green = (struct fb_bitfield){8, 8, 0};
1177 		info->var.blue = (struct fb_bitfield){0, 8, 0};
1178 		info->var.transp = (struct fb_bitfield){24, 8, 0};
1179 	}
1180 
1181 	info->var.activate = FB_ACTIVATE_NOW;
1182 	info->var.height = -1;
1183 	info->var.width = -1;
1184 	info->var.vmode = FB_VMODE_NONINTERLACED;
1185 
1186 	strcpy(info->fix.id, KBUILD_MODNAME);
1187 	info->fix.type = FB_TYPE_PACKED_PIXELS;
1188 	info->fix.visual = FB_VISUAL_TRUECOLOR;
1189 	info->fix.line_length = screen_width * screen_depth / 8;
1190 	info->fix.accel = FB_ACCEL_NONE;
1191 
1192 	info->fbops = &hvfb_ops;
1193 	info->pseudo_palette = par->pseudo_palette;
1194 
1195 	/* Initialize deferred IO */
1196 	info->fbdefio = &synthvid_defio;
1197 	fb_deferred_io_init(info);
1198 
1199 	/* Send config to host */
1200 	ret = synthvid_send_config(hdev);
1201 	if (ret)
1202 		goto error;
1203 
1204 	ret = register_framebuffer(info);
1205 	if (ret) {
1206 		pr_err("Unable to register framebuffer\n");
1207 		goto error;
1208 	}
1209 
1210 	par->fb_ready = true;
1211 
1212 	par->synchronous_fb = false;
1213 	par->hvfb_panic_nb.notifier_call = hvfb_on_panic;
1214 	atomic_notifier_chain_register(&panic_notifier_list,
1215 				       &par->hvfb_panic_nb);
1216 
1217 	return 0;
1218 
1219 error:
1220 	fb_deferred_io_cleanup(info);
1221 	hvfb_putmem(hdev, info);
1222 error2:
1223 	vmbus_close(hdev->channel);
1224 error1:
1225 	cancel_delayed_work_sync(&par->dwork);
1226 	hv_set_drvdata(hdev, NULL);
1227 	framebuffer_release(info);
1228 	return ret;
1229 }
1230 
1231 
1232 static int hvfb_remove(struct hv_device *hdev)
1233 {
1234 	struct fb_info *info = hv_get_drvdata(hdev);
1235 	struct hvfb_par *par = info->par;
1236 
1237 	atomic_notifier_chain_unregister(&panic_notifier_list,
1238 					 &par->hvfb_panic_nb);
1239 
1240 	par->update = false;
1241 	par->fb_ready = false;
1242 
1243 	fb_deferred_io_cleanup(info);
1244 
1245 	unregister_framebuffer(info);
1246 	cancel_delayed_work_sync(&par->dwork);
1247 
1248 	vmbus_close(hdev->channel);
1249 	hv_set_drvdata(hdev, NULL);
1250 
1251 	hvfb_putmem(hdev, info);
1252 	framebuffer_release(info);
1253 
1254 	return 0;
1255 }
1256 
1257 static int hvfb_suspend(struct hv_device *hdev)
1258 {
1259 	struct fb_info *info = hv_get_drvdata(hdev);
1260 	struct hvfb_par *par = info->par;
1261 
1262 	console_lock();
1263 
1264 	/* 1 means do suspend */
1265 	fb_set_suspend(info, 1);
1266 
1267 	cancel_delayed_work_sync(&par->dwork);
1268 	cancel_delayed_work_sync(&info->deferred_work);
1269 
1270 	par->update_saved = par->update;
1271 	par->update = false;
1272 	par->fb_ready = false;
1273 
1274 	vmbus_close(hdev->channel);
1275 
1276 	console_unlock();
1277 
1278 	return 0;
1279 }
1280 
1281 static int hvfb_resume(struct hv_device *hdev)
1282 {
1283 	struct fb_info *info = hv_get_drvdata(hdev);
1284 	struct hvfb_par *par = info->par;
1285 	int ret;
1286 
1287 	console_lock();
1288 
1289 	ret = synthvid_connect_vsp(hdev);
1290 	if (ret != 0)
1291 		goto out;
1292 
1293 	ret = synthvid_send_config(hdev);
1294 	if (ret != 0) {
1295 		vmbus_close(hdev->channel);
1296 		goto out;
1297 	}
1298 
1299 	par->fb_ready = true;
1300 	par->update = par->update_saved;
1301 
1302 	schedule_delayed_work(&info->deferred_work, info->fbdefio->delay);
1303 	schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
1304 
1305 	/* 0 means do resume */
1306 	fb_set_suspend(info, 0);
1307 
1308 out:
1309 	console_unlock();
1310 
1311 	return ret;
1312 }
1313 
1314 
1315 static const struct pci_device_id pci_stub_id_table[] = {
1316 	{
1317 		.vendor      = PCI_VENDOR_ID_MICROSOFT,
1318 		.device      = PCI_DEVICE_ID_HYPERV_VIDEO,
1319 	},
1320 	{ /* end of list */ }
1321 };
1322 
1323 static const struct hv_vmbus_device_id id_table[] = {
1324 	/* Synthetic Video Device GUID */
1325 	{HV_SYNTHVID_GUID},
1326 	{}
1327 };
1328 
1329 MODULE_DEVICE_TABLE(pci, pci_stub_id_table);
1330 MODULE_DEVICE_TABLE(vmbus, id_table);
1331 
1332 static struct hv_driver hvfb_drv = {
1333 	.name = KBUILD_MODNAME,
1334 	.id_table = id_table,
1335 	.probe = hvfb_probe,
1336 	.remove = hvfb_remove,
1337 	.suspend = hvfb_suspend,
1338 	.resume = hvfb_resume,
1339 	.driver = {
1340 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1341 	},
1342 };
1343 
1344 static int hvfb_pci_stub_probe(struct pci_dev *pdev,
1345 			       const struct pci_device_id *ent)
1346 {
1347 	return 0;
1348 }
1349 
1350 static void hvfb_pci_stub_remove(struct pci_dev *pdev)
1351 {
1352 }
1353 
1354 static struct pci_driver hvfb_pci_stub_driver = {
1355 	.name =		KBUILD_MODNAME,
1356 	.id_table =	pci_stub_id_table,
1357 	.probe =	hvfb_pci_stub_probe,
1358 	.remove =	hvfb_pci_stub_remove,
1359 	.driver = {
1360 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1361 	}
1362 };
1363 
1364 static int __init hvfb_drv_init(void)
1365 {
1366 	int ret;
1367 
1368 	ret = vmbus_driver_register(&hvfb_drv);
1369 	if (ret != 0)
1370 		return ret;
1371 
1372 	ret = pci_register_driver(&hvfb_pci_stub_driver);
1373 	if (ret != 0) {
1374 		vmbus_driver_unregister(&hvfb_drv);
1375 		return ret;
1376 	}
1377 
1378 	return 0;
1379 }
1380 
1381 static void __exit hvfb_drv_exit(void)
1382 {
1383 	pci_unregister_driver(&hvfb_pci_stub_driver);
1384 	vmbus_driver_unregister(&hvfb_drv);
1385 }
1386 
1387 module_init(hvfb_drv_init);
1388 module_exit(hvfb_drv_exit);
1389 
1390 MODULE_LICENSE("GPL");
1391 MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver");
1392