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