xref: /openbmc/linux/drivers/video/fbdev/hyperv_fb.c (revision b830f94f)
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 
28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/completion.h>
34 #include <linux/fb.h>
35 #include <linux/pci.h>
36 #include <linux/efi.h>
37 
38 #include <linux/hyperv.h>
39 
40 
41 /* Hyper-V Synthetic Video Protocol definitions and structures */
42 #define MAX_VMBUS_PKT_SIZE 0x4000
43 
44 #define SYNTHVID_VERSION(major, minor) ((minor) << 16 | (major))
45 #define SYNTHVID_VERSION_WIN7 SYNTHVID_VERSION(3, 0)
46 #define SYNTHVID_VERSION_WIN8 SYNTHVID_VERSION(3, 2)
47 
48 #define SYNTHVID_DEPTH_WIN7 16
49 #define SYNTHVID_DEPTH_WIN8 32
50 
51 #define SYNTHVID_FB_SIZE_WIN7 (4 * 1024 * 1024)
52 #define SYNTHVID_WIDTH_MAX_WIN7 1600
53 #define SYNTHVID_HEIGHT_MAX_WIN7 1200
54 
55 #define SYNTHVID_FB_SIZE_WIN8 (8 * 1024 * 1024)
56 
57 #define PCI_VENDOR_ID_MICROSOFT 0x1414
58 #define PCI_DEVICE_ID_HYPERV_VIDEO 0x5353
59 
60 
61 enum pipe_msg_type {
62 	PIPE_MSG_INVALID,
63 	PIPE_MSG_DATA,
64 	PIPE_MSG_MAX
65 };
66 
67 struct pipe_msg_hdr {
68 	u32 type;
69 	u32 size; /* size of message after this field */
70 } __packed;
71 
72 
73 enum synthvid_msg_type {
74 	SYNTHVID_ERROR			= 0,
75 	SYNTHVID_VERSION_REQUEST	= 1,
76 	SYNTHVID_VERSION_RESPONSE	= 2,
77 	SYNTHVID_VRAM_LOCATION		= 3,
78 	SYNTHVID_VRAM_LOCATION_ACK	= 4,
79 	SYNTHVID_SITUATION_UPDATE	= 5,
80 	SYNTHVID_SITUATION_UPDATE_ACK	= 6,
81 	SYNTHVID_POINTER_POSITION	= 7,
82 	SYNTHVID_POINTER_SHAPE		= 8,
83 	SYNTHVID_FEATURE_CHANGE		= 9,
84 	SYNTHVID_DIRT			= 10,
85 
86 	SYNTHVID_MAX			= 11
87 };
88 
89 struct synthvid_msg_hdr {
90 	u32 type;
91 	u32 size;  /* size of this header + payload after this field*/
92 } __packed;
93 
94 
95 struct synthvid_version_req {
96 	u32 version;
97 } __packed;
98 
99 struct synthvid_version_resp {
100 	u32 version;
101 	u8 is_accepted;
102 	u8 max_video_outputs;
103 } __packed;
104 
105 struct synthvid_vram_location {
106 	u64 user_ctx;
107 	u8 is_vram_gpa_specified;
108 	u64 vram_gpa;
109 } __packed;
110 
111 struct synthvid_vram_location_ack {
112 	u64 user_ctx;
113 } __packed;
114 
115 struct video_output_situation {
116 	u8 active;
117 	u32 vram_offset;
118 	u8 depth_bits;
119 	u32 width_pixels;
120 	u32 height_pixels;
121 	u32 pitch_bytes;
122 } __packed;
123 
124 struct synthvid_situation_update {
125 	u64 user_ctx;
126 	u8 video_output_count;
127 	struct video_output_situation video_output[1];
128 } __packed;
129 
130 struct synthvid_situation_update_ack {
131 	u64 user_ctx;
132 } __packed;
133 
134 struct synthvid_pointer_position {
135 	u8 is_visible;
136 	u8 video_output;
137 	s32 image_x;
138 	s32 image_y;
139 } __packed;
140 
141 
142 #define CURSOR_MAX_X 96
143 #define CURSOR_MAX_Y 96
144 #define CURSOR_ARGB_PIXEL_SIZE 4
145 #define CURSOR_MAX_SIZE (CURSOR_MAX_X * CURSOR_MAX_Y * CURSOR_ARGB_PIXEL_SIZE)
146 #define CURSOR_COMPLETE (-1)
147 
148 struct synthvid_pointer_shape {
149 	u8 part_idx;
150 	u8 is_argb;
151 	u32 width; /* CURSOR_MAX_X at most */
152 	u32 height; /* CURSOR_MAX_Y at most */
153 	u32 hot_x; /* hotspot relative to upper-left of pointer image */
154 	u32 hot_y;
155 	u8 data[4];
156 } __packed;
157 
158 struct synthvid_feature_change {
159 	u8 is_dirt_needed;
160 	u8 is_ptr_pos_needed;
161 	u8 is_ptr_shape_needed;
162 	u8 is_situ_needed;
163 } __packed;
164 
165 struct rect {
166 	s32 x1, y1; /* top left corner */
167 	s32 x2, y2; /* bottom right corner, exclusive */
168 } __packed;
169 
170 struct synthvid_dirt {
171 	u8 video_output;
172 	u8 dirt_count;
173 	struct rect rect[1];
174 } __packed;
175 
176 struct synthvid_msg {
177 	struct pipe_msg_hdr pipe_hdr;
178 	struct synthvid_msg_hdr vid_hdr;
179 	union {
180 		struct synthvid_version_req ver_req;
181 		struct synthvid_version_resp ver_resp;
182 		struct synthvid_vram_location vram;
183 		struct synthvid_vram_location_ack vram_ack;
184 		struct synthvid_situation_update situ;
185 		struct synthvid_situation_update_ack situ_ack;
186 		struct synthvid_pointer_position ptr_pos;
187 		struct synthvid_pointer_shape ptr_shape;
188 		struct synthvid_feature_change feature_chg;
189 		struct synthvid_dirt dirt;
190 	};
191 } __packed;
192 
193 
194 
195 /* FB driver definitions and structures */
196 #define HVFB_WIDTH 1152 /* default screen width */
197 #define HVFB_HEIGHT 864 /* default screen height */
198 #define HVFB_WIDTH_MIN 640
199 #define HVFB_HEIGHT_MIN 480
200 
201 #define RING_BUFSIZE (256 * 1024)
202 #define VSP_TIMEOUT (10 * HZ)
203 #define HVFB_UPDATE_DELAY (HZ / 20)
204 
205 struct hvfb_par {
206 	struct fb_info *info;
207 	struct resource *mem;
208 	bool fb_ready; /* fb device is ready */
209 	struct completion wait;
210 	u32 synthvid_version;
211 
212 	struct delayed_work dwork;
213 	bool update;
214 
215 	u32 pseudo_palette[16];
216 	u8 init_buf[MAX_VMBUS_PKT_SIZE];
217 	u8 recv_buf[MAX_VMBUS_PKT_SIZE];
218 
219 	/* If true, the VSC notifies the VSP on every framebuffer change */
220 	bool synchronous_fb;
221 
222 	struct notifier_block hvfb_panic_nb;
223 };
224 
225 static uint screen_width = HVFB_WIDTH;
226 static uint screen_height = HVFB_HEIGHT;
227 static uint screen_depth;
228 static uint screen_fb_size;
229 
230 /* Send message to Hyper-V host */
231 static inline int synthvid_send(struct hv_device *hdev,
232 				struct synthvid_msg *msg)
233 {
234 	static atomic64_t request_id = ATOMIC64_INIT(0);
235 	int ret;
236 
237 	msg->pipe_hdr.type = PIPE_MSG_DATA;
238 	msg->pipe_hdr.size = msg->vid_hdr.size;
239 
240 	ret = vmbus_sendpacket(hdev->channel, msg,
241 			       msg->vid_hdr.size + sizeof(struct pipe_msg_hdr),
242 			       atomic64_inc_return(&request_id),
243 			       VM_PKT_DATA_INBAND, 0);
244 
245 	if (ret)
246 		pr_err("Unable to send packet via vmbus\n");
247 
248 	return ret;
249 }
250 
251 
252 /* Send screen resolution info to host */
253 static int synthvid_send_situ(struct hv_device *hdev)
254 {
255 	struct fb_info *info = hv_get_drvdata(hdev);
256 	struct synthvid_msg msg;
257 
258 	if (!info)
259 		return -ENODEV;
260 
261 	memset(&msg, 0, sizeof(struct synthvid_msg));
262 
263 	msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE;
264 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
265 		sizeof(struct synthvid_situation_update);
266 	msg.situ.user_ctx = 0;
267 	msg.situ.video_output_count = 1;
268 	msg.situ.video_output[0].active = 1;
269 	msg.situ.video_output[0].vram_offset = 0;
270 	msg.situ.video_output[0].depth_bits = info->var.bits_per_pixel;
271 	msg.situ.video_output[0].width_pixels = info->var.xres;
272 	msg.situ.video_output[0].height_pixels = info->var.yres;
273 	msg.situ.video_output[0].pitch_bytes = info->fix.line_length;
274 
275 	synthvid_send(hdev, &msg);
276 
277 	return 0;
278 }
279 
280 /* Send mouse pointer info to host */
281 static int synthvid_send_ptr(struct hv_device *hdev)
282 {
283 	struct synthvid_msg msg;
284 
285 	memset(&msg, 0, sizeof(struct synthvid_msg));
286 	msg.vid_hdr.type = SYNTHVID_POINTER_POSITION;
287 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
288 		sizeof(struct synthvid_pointer_position);
289 	msg.ptr_pos.is_visible = 1;
290 	msg.ptr_pos.video_output = 0;
291 	msg.ptr_pos.image_x = 0;
292 	msg.ptr_pos.image_y = 0;
293 	synthvid_send(hdev, &msg);
294 
295 	memset(&msg, 0, sizeof(struct synthvid_msg));
296 	msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE;
297 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
298 		sizeof(struct synthvid_pointer_shape);
299 	msg.ptr_shape.part_idx = CURSOR_COMPLETE;
300 	msg.ptr_shape.is_argb = 1;
301 	msg.ptr_shape.width = 1;
302 	msg.ptr_shape.height = 1;
303 	msg.ptr_shape.hot_x = 0;
304 	msg.ptr_shape.hot_y = 0;
305 	msg.ptr_shape.data[0] = 0;
306 	msg.ptr_shape.data[1] = 1;
307 	msg.ptr_shape.data[2] = 1;
308 	msg.ptr_shape.data[3] = 1;
309 	synthvid_send(hdev, &msg);
310 
311 	return 0;
312 }
313 
314 /* Send updated screen area (dirty rectangle) location to host */
315 static int synthvid_update(struct fb_info *info)
316 {
317 	struct hv_device *hdev = device_to_hv_device(info->device);
318 	struct synthvid_msg msg;
319 
320 	memset(&msg, 0, sizeof(struct synthvid_msg));
321 
322 	msg.vid_hdr.type = SYNTHVID_DIRT;
323 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
324 		sizeof(struct synthvid_dirt);
325 	msg.dirt.video_output = 0;
326 	msg.dirt.dirt_count = 1;
327 	msg.dirt.rect[0].x1 = 0;
328 	msg.dirt.rect[0].y1 = 0;
329 	msg.dirt.rect[0].x2 = info->var.xres;
330 	msg.dirt.rect[0].y2 = info->var.yres;
331 
332 	synthvid_send(hdev, &msg);
333 
334 	return 0;
335 }
336 
337 
338 /*
339  * Actions on received messages from host:
340  * Complete the wait event.
341  * Or, reply with screen and cursor info.
342  */
343 static void synthvid_recv_sub(struct hv_device *hdev)
344 {
345 	struct fb_info *info = hv_get_drvdata(hdev);
346 	struct hvfb_par *par;
347 	struct synthvid_msg *msg;
348 
349 	if (!info)
350 		return;
351 
352 	par = info->par;
353 	msg = (struct synthvid_msg *)par->recv_buf;
354 
355 	/* Complete the wait event */
356 	if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE ||
357 	    msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) {
358 		memcpy(par->init_buf, msg, MAX_VMBUS_PKT_SIZE);
359 		complete(&par->wait);
360 		return;
361 	}
362 
363 	/* Reply with screen and cursor info */
364 	if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) {
365 		if (par->fb_ready) {
366 			synthvid_send_ptr(hdev);
367 			synthvid_send_situ(hdev);
368 		}
369 
370 		par->update = msg->feature_chg.is_dirt_needed;
371 		if (par->update)
372 			schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
373 	}
374 }
375 
376 /* Receive callback for messages from the host */
377 static void synthvid_receive(void *ctx)
378 {
379 	struct hv_device *hdev = ctx;
380 	struct fb_info *info = hv_get_drvdata(hdev);
381 	struct hvfb_par *par;
382 	struct synthvid_msg *recv_buf;
383 	u32 bytes_recvd;
384 	u64 req_id;
385 	int ret;
386 
387 	if (!info)
388 		return;
389 
390 	par = info->par;
391 	recv_buf = (struct synthvid_msg *)par->recv_buf;
392 
393 	do {
394 		ret = vmbus_recvpacket(hdev->channel, recv_buf,
395 				       MAX_VMBUS_PKT_SIZE,
396 				       &bytes_recvd, &req_id);
397 		if (bytes_recvd > 0 &&
398 		    recv_buf->pipe_hdr.type == PIPE_MSG_DATA)
399 			synthvid_recv_sub(hdev);
400 	} while (bytes_recvd > 0 && ret == 0);
401 }
402 
403 /* Check synthetic video protocol version with the host */
404 static int synthvid_negotiate_ver(struct hv_device *hdev, u32 ver)
405 {
406 	struct fb_info *info = hv_get_drvdata(hdev);
407 	struct hvfb_par *par = info->par;
408 	struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
409 	int ret = 0;
410 	unsigned long t;
411 
412 	memset(msg, 0, sizeof(struct synthvid_msg));
413 	msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST;
414 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
415 		sizeof(struct synthvid_version_req);
416 	msg->ver_req.version = ver;
417 	synthvid_send(hdev, msg);
418 
419 	t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
420 	if (!t) {
421 		pr_err("Time out on waiting version response\n");
422 		ret = -ETIMEDOUT;
423 		goto out;
424 	}
425 	if (!msg->ver_resp.is_accepted) {
426 		ret = -ENODEV;
427 		goto out;
428 	}
429 
430 	par->synthvid_version = ver;
431 
432 out:
433 	return ret;
434 }
435 
436 /* Connect to VSP (Virtual Service Provider) on host */
437 static int synthvid_connect_vsp(struct hv_device *hdev)
438 {
439 	struct fb_info *info = hv_get_drvdata(hdev);
440 	struct hvfb_par *par = info->par;
441 	int ret;
442 
443 	ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE,
444 			 NULL, 0, synthvid_receive, hdev);
445 	if (ret) {
446 		pr_err("Unable to open vmbus channel\n");
447 		return ret;
448 	}
449 
450 	/* Negotiate the protocol version with host */
451 	if (vmbus_proto_version == VERSION_WS2008 ||
452 	    vmbus_proto_version == VERSION_WIN7)
453 		ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN7);
454 	else
455 		ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8);
456 
457 	if (ret) {
458 		pr_err("Synthetic video device version not accepted\n");
459 		goto error;
460 	}
461 
462 	if (par->synthvid_version == SYNTHVID_VERSION_WIN7)
463 		screen_depth = SYNTHVID_DEPTH_WIN7;
464 	else
465 		screen_depth = SYNTHVID_DEPTH_WIN8;
466 
467 	screen_fb_size = hdev->channel->offermsg.offer.
468 				mmio_megabytes * 1024 * 1024;
469 
470 	return 0;
471 
472 error:
473 	vmbus_close(hdev->channel);
474 	return ret;
475 }
476 
477 /* Send VRAM and Situation messages to the host */
478 static int synthvid_send_config(struct hv_device *hdev)
479 {
480 	struct fb_info *info = hv_get_drvdata(hdev);
481 	struct hvfb_par *par = info->par;
482 	struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
483 	int ret = 0;
484 	unsigned long t;
485 
486 	/* Send VRAM location */
487 	memset(msg, 0, sizeof(struct synthvid_msg));
488 	msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION;
489 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
490 		sizeof(struct synthvid_vram_location);
491 	msg->vram.user_ctx = msg->vram.vram_gpa = info->fix.smem_start;
492 	msg->vram.is_vram_gpa_specified = 1;
493 	synthvid_send(hdev, msg);
494 
495 	t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
496 	if (!t) {
497 		pr_err("Time out on waiting vram location ack\n");
498 		ret = -ETIMEDOUT;
499 		goto out;
500 	}
501 	if (msg->vram_ack.user_ctx != info->fix.smem_start) {
502 		pr_err("Unable to set VRAM location\n");
503 		ret = -ENODEV;
504 		goto out;
505 	}
506 
507 	/* Send pointer and situation update */
508 	synthvid_send_ptr(hdev);
509 	synthvid_send_situ(hdev);
510 
511 out:
512 	return ret;
513 }
514 
515 
516 /*
517  * Delayed work callback:
518  * It is called at HVFB_UPDATE_DELAY or longer time interval to process
519  * screen updates. It is re-scheduled if further update is necessary.
520  */
521 static void hvfb_update_work(struct work_struct *w)
522 {
523 	struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work);
524 	struct fb_info *info = par->info;
525 
526 	if (par->fb_ready)
527 		synthvid_update(info);
528 
529 	if (par->update)
530 		schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
531 }
532 
533 static int hvfb_on_panic(struct notifier_block *nb,
534 			 unsigned long e, void *p)
535 {
536 	struct hvfb_par *par;
537 	struct fb_info *info;
538 
539 	par = container_of(nb, struct hvfb_par, hvfb_panic_nb);
540 	par->synchronous_fb = true;
541 	info = par->info;
542 	synthvid_update(info);
543 
544 	return NOTIFY_DONE;
545 }
546 
547 /* Framebuffer operation handlers */
548 
549 static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
550 {
551 	if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN ||
552 	    var->xres > screen_width || var->yres >  screen_height ||
553 	    var->bits_per_pixel != screen_depth)
554 		return -EINVAL;
555 
556 	var->xres_virtual = var->xres;
557 	var->yres_virtual = var->yres;
558 
559 	return 0;
560 }
561 
562 static int hvfb_set_par(struct fb_info *info)
563 {
564 	struct hv_device *hdev = device_to_hv_device(info->device);
565 
566 	return synthvid_send_situ(hdev);
567 }
568 
569 
570 static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf)
571 {
572 	return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset;
573 }
574 
575 static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
576 			  unsigned blue, unsigned transp, struct fb_info *info)
577 {
578 	u32 *pal = info->pseudo_palette;
579 
580 	if (regno > 15)
581 		return -EINVAL;
582 
583 	pal[regno] = chan_to_field(red, &info->var.red)
584 		| chan_to_field(green, &info->var.green)
585 		| chan_to_field(blue, &info->var.blue)
586 		| chan_to_field(transp, &info->var.transp);
587 
588 	return 0;
589 }
590 
591 static int hvfb_blank(int blank, struct fb_info *info)
592 {
593 	return 1;	/* get fb_blank to set the colormap to all black */
594 }
595 
596 static void hvfb_cfb_fillrect(struct fb_info *p,
597 			      const struct fb_fillrect *rect)
598 {
599 	struct hvfb_par *par = p->par;
600 
601 	cfb_fillrect(p, rect);
602 	if (par->synchronous_fb)
603 		synthvid_update(p);
604 }
605 
606 static void hvfb_cfb_copyarea(struct fb_info *p,
607 			      const struct fb_copyarea *area)
608 {
609 	struct hvfb_par *par = p->par;
610 
611 	cfb_copyarea(p, area);
612 	if (par->synchronous_fb)
613 		synthvid_update(p);
614 }
615 
616 static void hvfb_cfb_imageblit(struct fb_info *p,
617 			       const struct fb_image *image)
618 {
619 	struct hvfb_par *par = p->par;
620 
621 	cfb_imageblit(p, image);
622 	if (par->synchronous_fb)
623 		synthvid_update(p);
624 }
625 
626 static struct fb_ops hvfb_ops = {
627 	.owner = THIS_MODULE,
628 	.fb_check_var = hvfb_check_var,
629 	.fb_set_par = hvfb_set_par,
630 	.fb_setcolreg = hvfb_setcolreg,
631 	.fb_fillrect = hvfb_cfb_fillrect,
632 	.fb_copyarea = hvfb_cfb_copyarea,
633 	.fb_imageblit = hvfb_cfb_imageblit,
634 	.fb_blank = hvfb_blank,
635 };
636 
637 
638 /* Get options from kernel paramenter "video=" */
639 static void hvfb_get_option(struct fb_info *info)
640 {
641 	struct hvfb_par *par = info->par;
642 	char *opt = NULL, *p;
643 	uint x = 0, y = 0;
644 
645 	if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt)
646 		return;
647 
648 	p = strsep(&opt, "x");
649 	if (!*p || kstrtouint(p, 0, &x) ||
650 	    !opt || !*opt || kstrtouint(opt, 0, &y)) {
651 		pr_err("Screen option is invalid: skipped\n");
652 		return;
653 	}
654 
655 	if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN ||
656 	    (par->synthvid_version == SYNTHVID_VERSION_WIN8 &&
657 	     x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8) ||
658 	    (par->synthvid_version == SYNTHVID_VERSION_WIN7 &&
659 	     (x > SYNTHVID_WIDTH_MAX_WIN7 || y > SYNTHVID_HEIGHT_MAX_WIN7))) {
660 		pr_err("Screen resolution option is out of range: skipped\n");
661 		return;
662 	}
663 
664 	screen_width = x;
665 	screen_height = y;
666 	return;
667 }
668 
669 
670 /* Get framebuffer memory from Hyper-V video pci space */
671 static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info)
672 {
673 	struct hvfb_par *par = info->par;
674 	struct pci_dev *pdev  = NULL;
675 	void __iomem *fb_virt;
676 	int gen2vm = efi_enabled(EFI_BOOT);
677 	resource_size_t pot_start, pot_end;
678 	int ret;
679 
680 	if (gen2vm) {
681 		pot_start = 0;
682 		pot_end = -1;
683 	} else {
684 		pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
685 			      PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
686 		if (!pdev) {
687 			pr_err("Unable to find PCI Hyper-V video\n");
688 			return -ENODEV;
689 		}
690 
691 		if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
692 		    pci_resource_len(pdev, 0) < screen_fb_size)
693 			goto err1;
694 
695 		pot_end = pci_resource_end(pdev, 0);
696 		pot_start = pot_end - screen_fb_size + 1;
697 	}
698 
699 	ret = vmbus_allocate_mmio(&par->mem, hdev, pot_start, pot_end,
700 				  screen_fb_size, 0x100000, true);
701 	if (ret != 0) {
702 		pr_err("Unable to allocate framebuffer memory\n");
703 		goto err1;
704 	}
705 
706 	fb_virt = ioremap(par->mem->start, screen_fb_size);
707 	if (!fb_virt)
708 		goto err2;
709 
710 	info->apertures = alloc_apertures(1);
711 	if (!info->apertures)
712 		goto err3;
713 
714 	if (gen2vm) {
715 		info->apertures->ranges[0].base = screen_info.lfb_base;
716 		info->apertures->ranges[0].size = screen_info.lfb_size;
717 		remove_conflicting_framebuffers(info->apertures,
718 						KBUILD_MODNAME, false);
719 	} else {
720 		info->apertures->ranges[0].base = pci_resource_start(pdev, 0);
721 		info->apertures->ranges[0].size = pci_resource_len(pdev, 0);
722 	}
723 
724 	info->fix.smem_start = par->mem->start;
725 	info->fix.smem_len = screen_fb_size;
726 	info->screen_base = fb_virt;
727 	info->screen_size = screen_fb_size;
728 
729 	if (!gen2vm)
730 		pci_dev_put(pdev);
731 
732 	return 0;
733 
734 err3:
735 	iounmap(fb_virt);
736 err2:
737 	vmbus_free_mmio(par->mem->start, screen_fb_size);
738 	par->mem = NULL;
739 err1:
740 	if (!gen2vm)
741 		pci_dev_put(pdev);
742 
743 	return -ENOMEM;
744 }
745 
746 /* Release the framebuffer */
747 static void hvfb_putmem(struct fb_info *info)
748 {
749 	struct hvfb_par *par = info->par;
750 
751 	iounmap(info->screen_base);
752 	vmbus_free_mmio(par->mem->start, screen_fb_size);
753 	par->mem = NULL;
754 }
755 
756 
757 static int hvfb_probe(struct hv_device *hdev,
758 		      const struct hv_vmbus_device_id *dev_id)
759 {
760 	struct fb_info *info;
761 	struct hvfb_par *par;
762 	int ret;
763 
764 	info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device);
765 	if (!info)
766 		return -ENOMEM;
767 
768 	par = info->par;
769 	par->info = info;
770 	par->fb_ready = false;
771 	init_completion(&par->wait);
772 	INIT_DELAYED_WORK(&par->dwork, hvfb_update_work);
773 
774 	/* Connect to VSP */
775 	hv_set_drvdata(hdev, info);
776 	ret = synthvid_connect_vsp(hdev);
777 	if (ret) {
778 		pr_err("Unable to connect to VSP\n");
779 		goto error1;
780 	}
781 
782 	ret = hvfb_getmem(hdev, info);
783 	if (ret) {
784 		pr_err("No memory for framebuffer\n");
785 		goto error2;
786 	}
787 
788 	hvfb_get_option(info);
789 	pr_info("Screen resolution: %dx%d, Color depth: %d\n",
790 		screen_width, screen_height, screen_depth);
791 
792 
793 	/* Set up fb_info */
794 	info->flags = FBINFO_DEFAULT;
795 
796 	info->var.xres_virtual = info->var.xres = screen_width;
797 	info->var.yres_virtual = info->var.yres = screen_height;
798 	info->var.bits_per_pixel = screen_depth;
799 
800 	if (info->var.bits_per_pixel == 16) {
801 		info->var.red = (struct fb_bitfield){11, 5, 0};
802 		info->var.green = (struct fb_bitfield){5, 6, 0};
803 		info->var.blue = (struct fb_bitfield){0, 5, 0};
804 		info->var.transp = (struct fb_bitfield){0, 0, 0};
805 	} else {
806 		info->var.red = (struct fb_bitfield){16, 8, 0};
807 		info->var.green = (struct fb_bitfield){8, 8, 0};
808 		info->var.blue = (struct fb_bitfield){0, 8, 0};
809 		info->var.transp = (struct fb_bitfield){24, 8, 0};
810 	}
811 
812 	info->var.activate = FB_ACTIVATE_NOW;
813 	info->var.height = -1;
814 	info->var.width = -1;
815 	info->var.vmode = FB_VMODE_NONINTERLACED;
816 
817 	strcpy(info->fix.id, KBUILD_MODNAME);
818 	info->fix.type = FB_TYPE_PACKED_PIXELS;
819 	info->fix.visual = FB_VISUAL_TRUECOLOR;
820 	info->fix.line_length = screen_width * screen_depth / 8;
821 	info->fix.accel = FB_ACCEL_NONE;
822 
823 	info->fbops = &hvfb_ops;
824 	info->pseudo_palette = par->pseudo_palette;
825 
826 	/* Send config to host */
827 	ret = synthvid_send_config(hdev);
828 	if (ret)
829 		goto error;
830 
831 	ret = register_framebuffer(info);
832 	if (ret) {
833 		pr_err("Unable to register framebuffer\n");
834 		goto error;
835 	}
836 
837 	par->fb_ready = true;
838 
839 	par->synchronous_fb = false;
840 	par->hvfb_panic_nb.notifier_call = hvfb_on_panic;
841 	atomic_notifier_chain_register(&panic_notifier_list,
842 				       &par->hvfb_panic_nb);
843 
844 	return 0;
845 
846 error:
847 	hvfb_putmem(info);
848 error2:
849 	vmbus_close(hdev->channel);
850 error1:
851 	cancel_delayed_work_sync(&par->dwork);
852 	hv_set_drvdata(hdev, NULL);
853 	framebuffer_release(info);
854 	return ret;
855 }
856 
857 
858 static int hvfb_remove(struct hv_device *hdev)
859 {
860 	struct fb_info *info = hv_get_drvdata(hdev);
861 	struct hvfb_par *par = info->par;
862 
863 	atomic_notifier_chain_unregister(&panic_notifier_list,
864 					 &par->hvfb_panic_nb);
865 
866 	par->update = false;
867 	par->fb_ready = false;
868 
869 	unregister_framebuffer(info);
870 	cancel_delayed_work_sync(&par->dwork);
871 
872 	vmbus_close(hdev->channel);
873 	hv_set_drvdata(hdev, NULL);
874 
875 	hvfb_putmem(info);
876 	framebuffer_release(info);
877 
878 	return 0;
879 }
880 
881 
882 static const struct pci_device_id pci_stub_id_table[] = {
883 	{
884 		.vendor      = PCI_VENDOR_ID_MICROSOFT,
885 		.device      = PCI_DEVICE_ID_HYPERV_VIDEO,
886 	},
887 	{ /* end of list */ }
888 };
889 
890 static const struct hv_vmbus_device_id id_table[] = {
891 	/* Synthetic Video Device GUID */
892 	{HV_SYNTHVID_GUID},
893 	{}
894 };
895 
896 MODULE_DEVICE_TABLE(pci, pci_stub_id_table);
897 MODULE_DEVICE_TABLE(vmbus, id_table);
898 
899 static struct hv_driver hvfb_drv = {
900 	.name = KBUILD_MODNAME,
901 	.id_table = id_table,
902 	.probe = hvfb_probe,
903 	.remove = hvfb_remove,
904 	.driver = {
905 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
906 	},
907 };
908 
909 static int hvfb_pci_stub_probe(struct pci_dev *pdev,
910 			       const struct pci_device_id *ent)
911 {
912 	return 0;
913 }
914 
915 static void hvfb_pci_stub_remove(struct pci_dev *pdev)
916 {
917 }
918 
919 static struct pci_driver hvfb_pci_stub_driver = {
920 	.name =		KBUILD_MODNAME,
921 	.id_table =	pci_stub_id_table,
922 	.probe =	hvfb_pci_stub_probe,
923 	.remove =	hvfb_pci_stub_remove,
924 	.driver = {
925 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
926 	}
927 };
928 
929 static int __init hvfb_drv_init(void)
930 {
931 	int ret;
932 
933 	ret = vmbus_driver_register(&hvfb_drv);
934 	if (ret != 0)
935 		return ret;
936 
937 	ret = pci_register_driver(&hvfb_pci_stub_driver);
938 	if (ret != 0) {
939 		vmbus_driver_unregister(&hvfb_drv);
940 		return ret;
941 	}
942 
943 	return 0;
944 }
945 
946 static void __exit hvfb_drv_exit(void)
947 {
948 	pci_unregister_driver(&hvfb_pci_stub_driver);
949 	vmbus_driver_unregister(&hvfb_drv);
950 }
951 
952 module_init(hvfb_drv_init);
953 module_exit(hvfb_drv_exit);
954 
955 MODULE_LICENSE("GPL");
956 MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver");
957