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