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