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