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