blob: 1c6ae98710a0138e0211a3646105640a27711682 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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
61enum pipe_msg_type {
62 PIPE_MSG_INVALID,
63 PIPE_MSG_DATA,
64 PIPE_MSG_MAX
65};
66
67struct pipe_msg_hdr {
68 u32 type;
69 u32 size; /* size of message after this field */
70} __packed;
71
72
73enum 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
89struct synthvid_msg_hdr {
90 u32 type;
91 u32 size; /* size of this header + payload after this field*/
92} __packed;
93
94
95struct synthvid_version_req {
96 u32 version;
97} __packed;
98
99struct synthvid_version_resp {
100 u32 version;
101 u8 is_accepted;
102 u8 max_video_outputs;
103} __packed;
104
105struct synthvid_vram_location {
106 u64 user_ctx;
107 u8 is_vram_gpa_specified;
108 u64 vram_gpa;
109} __packed;
110
111struct synthvid_vram_location_ack {
112 u64 user_ctx;
113} __packed;
114
115struct 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
124struct synthvid_situation_update {
125 u64 user_ctx;
126 u8 video_output_count;
127 struct video_output_situation video_output[1];
128} __packed;
129
130struct synthvid_situation_update_ack {
131 u64 user_ctx;
132} __packed;
133
134struct 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
148struct 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
158struct 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
165struct rect {
166 s32 x1, y1; /* top left corner */
167 s32 x2, y2; /* bottom right corner, exclusive */
168} __packed;
169
170struct synthvid_dirt {
171 u8 video_output;
172 u8 dirt_count;
173 struct rect rect[1];
174} __packed;
175
176struct 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
205struct 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
225static uint screen_width = HVFB_WIDTH;
226static uint screen_height = HVFB_HEIGHT;
227static uint screen_depth;
228static uint screen_fb_size;
229
230/* Send message to Hyper-V host */
231static 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 */
253static 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 */
281static 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 */
315static 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 */
343static 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 */
377static 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 */
404static 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
432out:
433 return ret;
434}
435
436/* Connect to VSP (Virtual Service Provider) on host */
437static 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
472error:
473 vmbus_close(hdev->channel);
474 return ret;
475}
476
477/* Send VRAM and Situation messages to the host */
478static 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
511out:
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 */
521static 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
533static 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
549static 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
562static 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
570static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf)
571{
572 return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset;
573}
574
575static 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
591static 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
596static 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
606static 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
616static 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
626static 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=" */
639static 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 */
671static 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 /*
707 * Map the VRAM cacheable for performance.
708 */
709 fb_virt = ioremap_wc(par->mem->start, screen_fb_size);
710 if (!fb_virt)
711 goto err2;
712
713 info->apertures = alloc_apertures(1);
714 if (!info->apertures)
715 goto err3;
716
717 if (gen2vm) {
718 info->apertures->ranges[0].base = screen_info.lfb_base;
719 info->apertures->ranges[0].size = screen_info.lfb_size;
720 remove_conflicting_framebuffers(info->apertures,
721 KBUILD_MODNAME, false);
722 } else {
723 info->apertures->ranges[0].base = pci_resource_start(pdev, 0);
724 info->apertures->ranges[0].size = pci_resource_len(pdev, 0);
725 }
726
727 info->fix.smem_start = par->mem->start;
728 info->fix.smem_len = screen_fb_size;
729 info->screen_base = fb_virt;
730 info->screen_size = screen_fb_size;
731
732 if (!gen2vm)
733 pci_dev_put(pdev);
734
735 return 0;
736
737err3:
738 iounmap(fb_virt);
739err2:
740 vmbus_free_mmio(par->mem->start, screen_fb_size);
741 par->mem = NULL;
742err1:
743 if (!gen2vm)
744 pci_dev_put(pdev);
745
746 return -ENOMEM;
747}
748
749/* Release the framebuffer */
750static void hvfb_putmem(struct fb_info *info)
751{
752 struct hvfb_par *par = info->par;
753
754 iounmap(info->screen_base);
755 vmbus_free_mmio(par->mem->start, screen_fb_size);
756 par->mem = NULL;
757}
758
759
760static int hvfb_probe(struct hv_device *hdev,
761 const struct hv_vmbus_device_id *dev_id)
762{
763 struct fb_info *info;
764 struct hvfb_par *par;
765 int ret;
766
767 info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device);
768 if (!info)
769 return -ENOMEM;
770
771 par = info->par;
772 par->info = info;
773 par->fb_ready = false;
774 init_completion(&par->wait);
775 INIT_DELAYED_WORK(&par->dwork, hvfb_update_work);
776
777 /* Connect to VSP */
778 hv_set_drvdata(hdev, info);
779 ret = synthvid_connect_vsp(hdev);
780 if (ret) {
781 pr_err("Unable to connect to VSP\n");
782 goto error1;
783 }
784
785 ret = hvfb_getmem(hdev, info);
786 if (ret) {
787 pr_err("No memory for framebuffer\n");
788 goto error2;
789 }
790
791 hvfb_get_option(info);
792 pr_info("Screen resolution: %dx%d, Color depth: %d\n",
793 screen_width, screen_height, screen_depth);
794
795
796 /* Set up fb_info */
797 info->flags = FBINFO_DEFAULT;
798
799 info->var.xres_virtual = info->var.xres = screen_width;
800 info->var.yres_virtual = info->var.yres = screen_height;
801 info->var.bits_per_pixel = screen_depth;
802
803 if (info->var.bits_per_pixel == 16) {
804 info->var.red = (struct fb_bitfield){11, 5, 0};
805 info->var.green = (struct fb_bitfield){5, 6, 0};
806 info->var.blue = (struct fb_bitfield){0, 5, 0};
807 info->var.transp = (struct fb_bitfield){0, 0, 0};
808 } else {
809 info->var.red = (struct fb_bitfield){16, 8, 0};
810 info->var.green = (struct fb_bitfield){8, 8, 0};
811 info->var.blue = (struct fb_bitfield){0, 8, 0};
812 info->var.transp = (struct fb_bitfield){24, 8, 0};
813 }
814
815 info->var.activate = FB_ACTIVATE_NOW;
816 info->var.height = -1;
817 info->var.width = -1;
818 info->var.vmode = FB_VMODE_NONINTERLACED;
819
820 strcpy(info->fix.id, KBUILD_MODNAME);
821 info->fix.type = FB_TYPE_PACKED_PIXELS;
822 info->fix.visual = FB_VISUAL_TRUECOLOR;
823 info->fix.line_length = screen_width * screen_depth / 8;
824 info->fix.accel = FB_ACCEL_NONE;
825
826 info->fbops = &hvfb_ops;
827 info->pseudo_palette = par->pseudo_palette;
828
829 /* Send config to host */
830 ret = synthvid_send_config(hdev);
831 if (ret)
832 goto error;
833
834 ret = register_framebuffer(info);
835 if (ret) {
836 pr_err("Unable to register framebuffer\n");
837 goto error;
838 }
839
840 par->fb_ready = true;
841
842 par->synchronous_fb = false;
843 par->hvfb_panic_nb.notifier_call = hvfb_on_panic;
844 atomic_notifier_chain_register(&panic_notifier_list,
845 &par->hvfb_panic_nb);
846
847 return 0;
848
849error:
850 hvfb_putmem(info);
851error2:
852 vmbus_close(hdev->channel);
853error1:
854 cancel_delayed_work_sync(&par->dwork);
855 hv_set_drvdata(hdev, NULL);
856 framebuffer_release(info);
857 return ret;
858}
859
860
861static int hvfb_remove(struct hv_device *hdev)
862{
863 struct fb_info *info = hv_get_drvdata(hdev);
864 struct hvfb_par *par = info->par;
865
866 atomic_notifier_chain_unregister(&panic_notifier_list,
867 &par->hvfb_panic_nb);
868
869 par->update = false;
870 par->fb_ready = false;
871
872 unregister_framebuffer(info);
873 cancel_delayed_work_sync(&par->dwork);
874
875 vmbus_close(hdev->channel);
876 hv_set_drvdata(hdev, NULL);
877
878 hvfb_putmem(info);
879 framebuffer_release(info);
880
881 return 0;
882}
883
884
885static const struct pci_device_id pci_stub_id_table[] = {
886 {
887 .vendor = PCI_VENDOR_ID_MICROSOFT,
888 .device = PCI_DEVICE_ID_HYPERV_VIDEO,
889 },
890 { /* end of list */ }
891};
892
893static const struct hv_vmbus_device_id id_table[] = {
894 /* Synthetic Video Device GUID */
895 {HV_SYNTHVID_GUID},
896 {}
897};
898
899MODULE_DEVICE_TABLE(pci, pci_stub_id_table);
900MODULE_DEVICE_TABLE(vmbus, id_table);
901
902static struct hv_driver hvfb_drv = {
903 .name = KBUILD_MODNAME,
904 .id_table = id_table,
905 .probe = hvfb_probe,
906 .remove = hvfb_remove,
907 .driver = {
908 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
909 },
910};
911
912static int hvfb_pci_stub_probe(struct pci_dev *pdev,
913 const struct pci_device_id *ent)
914{
915 return 0;
916}
917
918static void hvfb_pci_stub_remove(struct pci_dev *pdev)
919{
920}
921
922static struct pci_driver hvfb_pci_stub_driver = {
923 .name = KBUILD_MODNAME,
924 .id_table = pci_stub_id_table,
925 .probe = hvfb_pci_stub_probe,
926 .remove = hvfb_pci_stub_remove,
927 .driver = {
928 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
929 }
930};
931
932static int __init hvfb_drv_init(void)
933{
934 int ret;
935
936 ret = vmbus_driver_register(&hvfb_drv);
937 if (ret != 0)
938 return ret;
939
940 ret = pci_register_driver(&hvfb_pci_stub_driver);
941 if (ret != 0) {
942 vmbus_driver_unregister(&hvfb_drv);
943 return ret;
944 }
945
946 return 0;
947}
948
949static void __exit hvfb_drv_exit(void)
950{
951 pci_unregister_driver(&hvfb_pci_stub_driver);
952 vmbus_driver_unregister(&hvfb_drv);
953}
954
955module_init(hvfb_drv_init);
956module_exit(hvfb_drv_exit);
957
958MODULE_LICENSE("GPL");
959MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver");