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