Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2012, Microsoft Corporation.
0004  *
0005  * Author:
0006  *   Haiyang Zhang <haiyangz@microsoft.com>
0007  */
0008 
0009 /*
0010  * Hyper-V Synthetic Video Frame Buffer Driver
0011  *
0012  * This is the driver for the Hyper-V Synthetic Video, which supports
0013  * screen resolution up to Full HD 1920x1080 with 32 bit color on Windows
0014  * Server 2012, and 1600x1200 with 16 bit color on Windows Server 2008 R2
0015  * or earlier.
0016  *
0017  * It also solves the double mouse cursor issue of the emulated video mode.
0018  *
0019  * The default screen resolution is 1152x864, which may be changed by a
0020  * kernel parameter:
0021  *     video=hyperv_fb:<width>x<height>
0022  *     For example: video=hyperv_fb:1280x1024
0023  *
0024  * Portrait orientation is also supported:
0025  *     For example: video=hyperv_fb:864x1152
0026  *
0027  * When a Windows 10 RS5+ host is used, the virtual machine screen
0028  * resolution is obtained from the host. The "video=hyperv_fb" option is
0029  * not needed, but still can be used to overwrite what the host specifies.
0030  * The VM resolution on the host could be set by executing the powershell
0031  * "set-vmvideo" command. For example
0032  *     set-vmvideo -vmname name -horizontalresolution:1920 \
0033  * -verticalresolution:1200 -resolutiontype single
0034  *
0035  * Gen 1 VMs also support direct using VM's physical memory for framebuffer.
0036  * It could improve the efficiency and performance for framebuffer and VM.
0037  * This requires to allocate contiguous physical memory from Linux kernel's
0038  * CMA memory allocator. To enable this, supply a kernel parameter to give
0039  * enough memory space to CMA allocator for framebuffer. For example:
0040  *    cma=130m
0041  * This gives 130MB memory to CMA allocator that can be allocated to
0042  * framebuffer. For reference, 8K resolution (7680x4320) takes about
0043  * 127MB memory.
0044  */
0045 
0046 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0047 
0048 #include <linux/module.h>
0049 #include <linux/kernel.h>
0050 #include <linux/vmalloc.h>
0051 #include <linux/init.h>
0052 #include <linux/completion.h>
0053 #include <linux/fb.h>
0054 #include <linux/pci.h>
0055 #include <linux/panic_notifier.h>
0056 #include <linux/efi.h>
0057 #include <linux/console.h>
0058 
0059 #include <linux/hyperv.h>
0060 
0061 
0062 /* Hyper-V Synthetic Video Protocol definitions and structures */
0063 #define MAX_VMBUS_PKT_SIZE 0x4000
0064 
0065 #define SYNTHVID_VERSION(major, minor) ((minor) << 16 | (major))
0066 /* Support for VERSION_WIN7 is removed. #define is retained for reference. */
0067 #define SYNTHVID_VERSION_WIN7 SYNTHVID_VERSION(3, 0)
0068 #define SYNTHVID_VERSION_WIN8 SYNTHVID_VERSION(3, 2)
0069 #define SYNTHVID_VERSION_WIN10 SYNTHVID_VERSION(3, 5)
0070 
0071 #define SYNTHVID_VER_GET_MAJOR(ver) (ver & 0x0000ffff)
0072 #define SYNTHVID_VER_GET_MINOR(ver) ((ver & 0xffff0000) >> 16)
0073 
0074 #define SYNTHVID_DEPTH_WIN8 32
0075 #define SYNTHVID_FB_SIZE_WIN8 (8 * 1024 * 1024)
0076 
0077 enum pipe_msg_type {
0078     PIPE_MSG_INVALID,
0079     PIPE_MSG_DATA,
0080     PIPE_MSG_MAX
0081 };
0082 
0083 struct pipe_msg_hdr {
0084     u32 type;
0085     u32 size; /* size of message after this field */
0086 } __packed;
0087 
0088 
0089 enum synthvid_msg_type {
0090     SYNTHVID_ERROR          = 0,
0091     SYNTHVID_VERSION_REQUEST    = 1,
0092     SYNTHVID_VERSION_RESPONSE   = 2,
0093     SYNTHVID_VRAM_LOCATION      = 3,
0094     SYNTHVID_VRAM_LOCATION_ACK  = 4,
0095     SYNTHVID_SITUATION_UPDATE   = 5,
0096     SYNTHVID_SITUATION_UPDATE_ACK   = 6,
0097     SYNTHVID_POINTER_POSITION   = 7,
0098     SYNTHVID_POINTER_SHAPE      = 8,
0099     SYNTHVID_FEATURE_CHANGE     = 9,
0100     SYNTHVID_DIRT           = 10,
0101     SYNTHVID_RESOLUTION_REQUEST = 13,
0102     SYNTHVID_RESOLUTION_RESPONSE    = 14,
0103 
0104     SYNTHVID_MAX            = 15
0105 };
0106 
0107 #define     SYNTHVID_EDID_BLOCK_SIZE    128
0108 #define     SYNTHVID_MAX_RESOLUTION_COUNT   64
0109 
0110 struct hvd_screen_info {
0111     u16 width;
0112     u16 height;
0113 } __packed;
0114 
0115 struct synthvid_msg_hdr {
0116     u32 type;
0117     u32 size;  /* size of this header + payload after this field*/
0118 } __packed;
0119 
0120 struct synthvid_version_req {
0121     u32 version;
0122 } __packed;
0123 
0124 struct synthvid_version_resp {
0125     u32 version;
0126     u8 is_accepted;
0127     u8 max_video_outputs;
0128 } __packed;
0129 
0130 struct synthvid_supported_resolution_req {
0131     u8 maximum_resolution_count;
0132 } __packed;
0133 
0134 struct synthvid_supported_resolution_resp {
0135     u8 edid_block[SYNTHVID_EDID_BLOCK_SIZE];
0136     u8 resolution_count;
0137     u8 default_resolution_index;
0138     u8 is_standard;
0139     struct hvd_screen_info
0140         supported_resolution[SYNTHVID_MAX_RESOLUTION_COUNT];
0141 } __packed;
0142 
0143 struct synthvid_vram_location {
0144     u64 user_ctx;
0145     u8 is_vram_gpa_specified;
0146     u64 vram_gpa;
0147 } __packed;
0148 
0149 struct synthvid_vram_location_ack {
0150     u64 user_ctx;
0151 } __packed;
0152 
0153 struct video_output_situation {
0154     u8 active;
0155     u32 vram_offset;
0156     u8 depth_bits;
0157     u32 width_pixels;
0158     u32 height_pixels;
0159     u32 pitch_bytes;
0160 } __packed;
0161 
0162 struct synthvid_situation_update {
0163     u64 user_ctx;
0164     u8 video_output_count;
0165     struct video_output_situation video_output[1];
0166 } __packed;
0167 
0168 struct synthvid_situation_update_ack {
0169     u64 user_ctx;
0170 } __packed;
0171 
0172 struct synthvid_pointer_position {
0173     u8 is_visible;
0174     u8 video_output;
0175     s32 image_x;
0176     s32 image_y;
0177 } __packed;
0178 
0179 
0180 #define CURSOR_MAX_X 96
0181 #define CURSOR_MAX_Y 96
0182 #define CURSOR_ARGB_PIXEL_SIZE 4
0183 #define CURSOR_MAX_SIZE (CURSOR_MAX_X * CURSOR_MAX_Y * CURSOR_ARGB_PIXEL_SIZE)
0184 #define CURSOR_COMPLETE (-1)
0185 
0186 struct synthvid_pointer_shape {
0187     u8 part_idx;
0188     u8 is_argb;
0189     u32 width; /* CURSOR_MAX_X at most */
0190     u32 height; /* CURSOR_MAX_Y at most */
0191     u32 hot_x; /* hotspot relative to upper-left of pointer image */
0192     u32 hot_y;
0193     u8 data[4];
0194 } __packed;
0195 
0196 struct synthvid_feature_change {
0197     u8 is_dirt_needed;
0198     u8 is_ptr_pos_needed;
0199     u8 is_ptr_shape_needed;
0200     u8 is_situ_needed;
0201 } __packed;
0202 
0203 struct rect {
0204     s32 x1, y1; /* top left corner */
0205     s32 x2, y2; /* bottom right corner, exclusive */
0206 } __packed;
0207 
0208 struct synthvid_dirt {
0209     u8 video_output;
0210     u8 dirt_count;
0211     struct rect rect[1];
0212 } __packed;
0213 
0214 struct synthvid_msg {
0215     struct pipe_msg_hdr pipe_hdr;
0216     struct synthvid_msg_hdr vid_hdr;
0217     union {
0218         struct synthvid_version_req ver_req;
0219         struct synthvid_version_resp ver_resp;
0220         struct synthvid_vram_location vram;
0221         struct synthvid_vram_location_ack vram_ack;
0222         struct synthvid_situation_update situ;
0223         struct synthvid_situation_update_ack situ_ack;
0224         struct synthvid_pointer_position ptr_pos;
0225         struct synthvid_pointer_shape ptr_shape;
0226         struct synthvid_feature_change feature_chg;
0227         struct synthvid_dirt dirt;
0228         struct synthvid_supported_resolution_req resolution_req;
0229         struct synthvid_supported_resolution_resp resolution_resp;
0230     };
0231 } __packed;
0232 
0233 
0234 /* FB driver definitions and structures */
0235 #define HVFB_WIDTH 1152 /* default screen width */
0236 #define HVFB_HEIGHT 864 /* default screen height */
0237 #define HVFB_WIDTH_MIN 640
0238 #define HVFB_HEIGHT_MIN 480
0239 
0240 #define RING_BUFSIZE (256 * 1024)
0241 #define VSP_TIMEOUT (10 * HZ)
0242 #define HVFB_UPDATE_DELAY (HZ / 20)
0243 #define HVFB_ONDEMAND_THROTTLE (HZ / 20)
0244 
0245 struct hvfb_par {
0246     struct fb_info *info;
0247     struct resource *mem;
0248     bool fb_ready; /* fb device is ready */
0249     struct completion wait;
0250     u32 synthvid_version;
0251 
0252     struct delayed_work dwork;
0253     bool update;
0254     bool update_saved; /* The value of 'update' before hibernation */
0255 
0256     u32 pseudo_palette[16];
0257     u8 init_buf[MAX_VMBUS_PKT_SIZE];
0258     u8 recv_buf[MAX_VMBUS_PKT_SIZE];
0259 
0260     /* If true, the VSC notifies the VSP on every framebuffer change */
0261     bool synchronous_fb;
0262 
0263     /* If true, need to copy from deferred IO mem to framebuffer mem */
0264     bool need_docopy;
0265 
0266     struct notifier_block hvfb_panic_nb;
0267 
0268     /* Memory for deferred IO and frame buffer itself */
0269     unsigned char *dio_vp;
0270     unsigned char *mmio_vp;
0271     phys_addr_t mmio_pp;
0272 
0273     /* Dirty rectangle, protected by delayed_refresh_lock */
0274     int x1, y1, x2, y2;
0275     bool delayed_refresh;
0276     spinlock_t delayed_refresh_lock;
0277 };
0278 
0279 static uint screen_width = HVFB_WIDTH;
0280 static uint screen_height = HVFB_HEIGHT;
0281 static uint screen_depth;
0282 static uint screen_fb_size;
0283 static uint dio_fb_size; /* FB size for deferred IO */
0284 
0285 /* Send message to Hyper-V host */
0286 static inline int synthvid_send(struct hv_device *hdev,
0287                 struct synthvid_msg *msg)
0288 {
0289     static atomic64_t request_id = ATOMIC64_INIT(0);
0290     int ret;
0291 
0292     msg->pipe_hdr.type = PIPE_MSG_DATA;
0293     msg->pipe_hdr.size = msg->vid_hdr.size;
0294 
0295     ret = vmbus_sendpacket(hdev->channel, msg,
0296                    msg->vid_hdr.size + sizeof(struct pipe_msg_hdr),
0297                    atomic64_inc_return(&request_id),
0298                    VM_PKT_DATA_INBAND, 0);
0299 
0300     if (ret)
0301         pr_err_ratelimited("Unable to send packet via vmbus; error %d\n", ret);
0302 
0303     return ret;
0304 }
0305 
0306 
0307 /* Send screen resolution info to host */
0308 static int synthvid_send_situ(struct hv_device *hdev)
0309 {
0310     struct fb_info *info = hv_get_drvdata(hdev);
0311     struct synthvid_msg msg;
0312 
0313     if (!info)
0314         return -ENODEV;
0315 
0316     memset(&msg, 0, sizeof(struct synthvid_msg));
0317 
0318     msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE;
0319     msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
0320         sizeof(struct synthvid_situation_update);
0321     msg.situ.user_ctx = 0;
0322     msg.situ.video_output_count = 1;
0323     msg.situ.video_output[0].active = 1;
0324     msg.situ.video_output[0].vram_offset = 0;
0325     msg.situ.video_output[0].depth_bits = info->var.bits_per_pixel;
0326     msg.situ.video_output[0].width_pixels = info->var.xres;
0327     msg.situ.video_output[0].height_pixels = info->var.yres;
0328     msg.situ.video_output[0].pitch_bytes = info->fix.line_length;
0329 
0330     synthvid_send(hdev, &msg);
0331 
0332     return 0;
0333 }
0334 
0335 /* Send mouse pointer info to host */
0336 static int synthvid_send_ptr(struct hv_device *hdev)
0337 {
0338     struct synthvid_msg msg;
0339 
0340     memset(&msg, 0, sizeof(struct synthvid_msg));
0341     msg.vid_hdr.type = SYNTHVID_POINTER_POSITION;
0342     msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
0343         sizeof(struct synthvid_pointer_position);
0344     msg.ptr_pos.is_visible = 1;
0345     msg.ptr_pos.video_output = 0;
0346     msg.ptr_pos.image_x = 0;
0347     msg.ptr_pos.image_y = 0;
0348     synthvid_send(hdev, &msg);
0349 
0350     memset(&msg, 0, sizeof(struct synthvid_msg));
0351     msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE;
0352     msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
0353         sizeof(struct synthvid_pointer_shape);
0354     msg.ptr_shape.part_idx = CURSOR_COMPLETE;
0355     msg.ptr_shape.is_argb = 1;
0356     msg.ptr_shape.width = 1;
0357     msg.ptr_shape.height = 1;
0358     msg.ptr_shape.hot_x = 0;
0359     msg.ptr_shape.hot_y = 0;
0360     msg.ptr_shape.data[0] = 0;
0361     msg.ptr_shape.data[1] = 1;
0362     msg.ptr_shape.data[2] = 1;
0363     msg.ptr_shape.data[3] = 1;
0364     synthvid_send(hdev, &msg);
0365 
0366     return 0;
0367 }
0368 
0369 /* Send updated screen area (dirty rectangle) location to host */
0370 static int
0371 synthvid_update(struct fb_info *info, int x1, int y1, int x2, int y2)
0372 {
0373     struct hv_device *hdev = device_to_hv_device(info->device);
0374     struct synthvid_msg msg;
0375 
0376     memset(&msg, 0, sizeof(struct synthvid_msg));
0377     if (x2 == INT_MAX)
0378         x2 = info->var.xres;
0379     if (y2 == INT_MAX)
0380         y2 = info->var.yres;
0381 
0382     msg.vid_hdr.type = SYNTHVID_DIRT;
0383     msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
0384         sizeof(struct synthvid_dirt);
0385     msg.dirt.video_output = 0;
0386     msg.dirt.dirt_count = 1;
0387     msg.dirt.rect[0].x1 = (x1 > x2) ? 0 : x1;
0388     msg.dirt.rect[0].y1 = (y1 > y2) ? 0 : y1;
0389     msg.dirt.rect[0].x2 =
0390         (x2 < x1 || x2 > info->var.xres) ? info->var.xres : x2;
0391     msg.dirt.rect[0].y2 =
0392         (y2 < y1 || y2 > info->var.yres) ? info->var.yres : y2;
0393 
0394     synthvid_send(hdev, &msg);
0395 
0396     return 0;
0397 }
0398 
0399 static void hvfb_docopy(struct hvfb_par *par,
0400             unsigned long offset,
0401             unsigned long size)
0402 {
0403     if (!par || !par->mmio_vp || !par->dio_vp || !par->fb_ready ||
0404         size == 0 || offset >= dio_fb_size)
0405         return;
0406 
0407     if (offset + size > dio_fb_size)
0408         size = dio_fb_size - offset;
0409 
0410     memcpy(par->mmio_vp + offset, par->dio_vp + offset, size);
0411 }
0412 
0413 /* Deferred IO callback */
0414 static void synthvid_deferred_io(struct fb_info *p, struct list_head *pagereflist)
0415 {
0416     struct hvfb_par *par = p->par;
0417     struct fb_deferred_io_pageref *pageref;
0418     unsigned long start, end;
0419     int y1, y2, miny, maxy;
0420 
0421     miny = INT_MAX;
0422     maxy = 0;
0423 
0424     /*
0425      * Merge dirty pages. It is possible that last page cross
0426      * over the end of frame buffer row yres. This is taken care of
0427      * in synthvid_update function by clamping the y2
0428      * value to yres.
0429      */
0430     list_for_each_entry(pageref, pagereflist, list) {
0431         start = pageref->offset;
0432         end = start + PAGE_SIZE - 1;
0433         y1 = start / p->fix.line_length;
0434         y2 = end / p->fix.line_length;
0435         miny = min_t(int, miny, y1);
0436         maxy = max_t(int, maxy, y2);
0437 
0438         /* Copy from dio space to mmio address */
0439         if (par->fb_ready && par->need_docopy)
0440             hvfb_docopy(par, start, PAGE_SIZE);
0441     }
0442 
0443     if (par->fb_ready && par->update)
0444         synthvid_update(p, 0, miny, p->var.xres, maxy + 1);
0445 }
0446 
0447 static struct fb_deferred_io synthvid_defio = {
0448     .delay      = HZ / 20,
0449     .deferred_io    = synthvid_deferred_io,
0450 };
0451 
0452 /*
0453  * Actions on received messages from host:
0454  * Complete the wait event.
0455  * Or, reply with screen and cursor info.
0456  */
0457 static void synthvid_recv_sub(struct hv_device *hdev)
0458 {
0459     struct fb_info *info = hv_get_drvdata(hdev);
0460     struct hvfb_par *par;
0461     struct synthvid_msg *msg;
0462 
0463     if (!info)
0464         return;
0465 
0466     par = info->par;
0467     msg = (struct synthvid_msg *)par->recv_buf;
0468 
0469     /* Complete the wait event */
0470     if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE ||
0471         msg->vid_hdr.type == SYNTHVID_RESOLUTION_RESPONSE ||
0472         msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) {
0473         memcpy(par->init_buf, msg, MAX_VMBUS_PKT_SIZE);
0474         complete(&par->wait);
0475         return;
0476     }
0477 
0478     /* Reply with screen and cursor info */
0479     if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) {
0480         if (par->fb_ready) {
0481             synthvid_send_ptr(hdev);
0482             synthvid_send_situ(hdev);
0483         }
0484 
0485         par->update = msg->feature_chg.is_dirt_needed;
0486         if (par->update)
0487             schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
0488     }
0489 }
0490 
0491 /* Receive callback for messages from the host */
0492 static void synthvid_receive(void *ctx)
0493 {
0494     struct hv_device *hdev = ctx;
0495     struct fb_info *info = hv_get_drvdata(hdev);
0496     struct hvfb_par *par;
0497     struct synthvid_msg *recv_buf;
0498     u32 bytes_recvd;
0499     u64 req_id;
0500     int ret;
0501 
0502     if (!info)
0503         return;
0504 
0505     par = info->par;
0506     recv_buf = (struct synthvid_msg *)par->recv_buf;
0507 
0508     do {
0509         ret = vmbus_recvpacket(hdev->channel, recv_buf,
0510                        MAX_VMBUS_PKT_SIZE,
0511                        &bytes_recvd, &req_id);
0512         if (bytes_recvd > 0 &&
0513             recv_buf->pipe_hdr.type == PIPE_MSG_DATA)
0514             synthvid_recv_sub(hdev);
0515     } while (bytes_recvd > 0 && ret == 0);
0516 }
0517 
0518 /* Check if the ver1 version is equal or greater than ver2 */
0519 static inline bool synthvid_ver_ge(u32 ver1, u32 ver2)
0520 {
0521     if (SYNTHVID_VER_GET_MAJOR(ver1) > SYNTHVID_VER_GET_MAJOR(ver2) ||
0522         (SYNTHVID_VER_GET_MAJOR(ver1) == SYNTHVID_VER_GET_MAJOR(ver2) &&
0523          SYNTHVID_VER_GET_MINOR(ver1) >= SYNTHVID_VER_GET_MINOR(ver2)))
0524         return true;
0525 
0526     return false;
0527 }
0528 
0529 /* Check synthetic video protocol version with the host */
0530 static int synthvid_negotiate_ver(struct hv_device *hdev, u32 ver)
0531 {
0532     struct fb_info *info = hv_get_drvdata(hdev);
0533     struct hvfb_par *par = info->par;
0534     struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
0535     int ret = 0;
0536     unsigned long t;
0537 
0538     memset(msg, 0, sizeof(struct synthvid_msg));
0539     msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST;
0540     msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
0541         sizeof(struct synthvid_version_req);
0542     msg->ver_req.version = ver;
0543     synthvid_send(hdev, msg);
0544 
0545     t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
0546     if (!t) {
0547         pr_err("Time out on waiting version response\n");
0548         ret = -ETIMEDOUT;
0549         goto out;
0550     }
0551     if (!msg->ver_resp.is_accepted) {
0552         ret = -ENODEV;
0553         goto out;
0554     }
0555 
0556     par->synthvid_version = ver;
0557     pr_info("Synthvid Version major %d, minor %d\n",
0558         SYNTHVID_VER_GET_MAJOR(ver), SYNTHVID_VER_GET_MINOR(ver));
0559 
0560 out:
0561     return ret;
0562 }
0563 
0564 /* Get current resolution from the host */
0565 static int synthvid_get_supported_resolution(struct hv_device *hdev)
0566 {
0567     struct fb_info *info = hv_get_drvdata(hdev);
0568     struct hvfb_par *par = info->par;
0569     struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
0570     int ret = 0;
0571     unsigned long t;
0572     u8 index;
0573 
0574     memset(msg, 0, sizeof(struct synthvid_msg));
0575     msg->vid_hdr.type = SYNTHVID_RESOLUTION_REQUEST;
0576     msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
0577         sizeof(struct synthvid_supported_resolution_req);
0578 
0579     msg->resolution_req.maximum_resolution_count =
0580         SYNTHVID_MAX_RESOLUTION_COUNT;
0581     synthvid_send(hdev, msg);
0582 
0583     t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
0584     if (!t) {
0585         pr_err("Time out on waiting resolution response\n");
0586         ret = -ETIMEDOUT;
0587         goto out;
0588     }
0589 
0590     if (msg->resolution_resp.resolution_count == 0) {
0591         pr_err("No supported resolutions\n");
0592         ret = -ENODEV;
0593         goto out;
0594     }
0595 
0596     index = msg->resolution_resp.default_resolution_index;
0597     if (index >= msg->resolution_resp.resolution_count) {
0598         pr_err("Invalid resolution index: %d\n", index);
0599         ret = -ENODEV;
0600         goto out;
0601     }
0602 
0603     screen_width =
0604         msg->resolution_resp.supported_resolution[index].width;
0605     screen_height =
0606         msg->resolution_resp.supported_resolution[index].height;
0607 
0608 out:
0609     return ret;
0610 }
0611 
0612 /* Connect to VSP (Virtual Service Provider) on host */
0613 static int synthvid_connect_vsp(struct hv_device *hdev)
0614 {
0615     struct fb_info *info = hv_get_drvdata(hdev);
0616     struct hvfb_par *par = info->par;
0617     int ret;
0618 
0619     ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE,
0620              NULL, 0, synthvid_receive, hdev);
0621     if (ret) {
0622         pr_err("Unable to open vmbus channel\n");
0623         return ret;
0624     }
0625 
0626     /* Negotiate the protocol version with host */
0627     switch (vmbus_proto_version) {
0628     case VERSION_WIN10:
0629     case VERSION_WIN10_V5:
0630         ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
0631         if (!ret)
0632             break;
0633         fallthrough;
0634     case VERSION_WIN8:
0635     case VERSION_WIN8_1:
0636         ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8);
0637         break;
0638     default:
0639         ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
0640         break;
0641     }
0642 
0643     if (ret) {
0644         pr_err("Synthetic video device version not accepted\n");
0645         goto error;
0646     }
0647 
0648     screen_depth = SYNTHVID_DEPTH_WIN8;
0649     if (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10)) {
0650         ret = synthvid_get_supported_resolution(hdev);
0651         if (ret)
0652             pr_info("Failed to get supported resolution from host, use default\n");
0653     }
0654 
0655     screen_fb_size = hdev->channel->offermsg.offer.
0656                 mmio_megabytes * 1024 * 1024;
0657 
0658     return 0;
0659 
0660 error:
0661     vmbus_close(hdev->channel);
0662     return ret;
0663 }
0664 
0665 /* Send VRAM and Situation messages to the host */
0666 static int synthvid_send_config(struct hv_device *hdev)
0667 {
0668     struct fb_info *info = hv_get_drvdata(hdev);
0669     struct hvfb_par *par = info->par;
0670     struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
0671     int ret = 0;
0672     unsigned long t;
0673 
0674     /* Send VRAM location */
0675     memset(msg, 0, sizeof(struct synthvid_msg));
0676     msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION;
0677     msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
0678         sizeof(struct synthvid_vram_location);
0679     msg->vram.user_ctx = msg->vram.vram_gpa = par->mmio_pp;
0680     msg->vram.is_vram_gpa_specified = 1;
0681     synthvid_send(hdev, msg);
0682 
0683     t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
0684     if (!t) {
0685         pr_err("Time out on waiting vram location ack\n");
0686         ret = -ETIMEDOUT;
0687         goto out;
0688     }
0689     if (msg->vram_ack.user_ctx != par->mmio_pp) {
0690         pr_err("Unable to set VRAM location\n");
0691         ret = -ENODEV;
0692         goto out;
0693     }
0694 
0695     /* Send pointer and situation update */
0696     synthvid_send_ptr(hdev);
0697     synthvid_send_situ(hdev);
0698 
0699 out:
0700     return ret;
0701 }
0702 
0703 
0704 /*
0705  * Delayed work callback:
0706  * It is scheduled to call whenever update request is received and it has
0707  * not been called in last HVFB_ONDEMAND_THROTTLE time interval.
0708  */
0709 static void hvfb_update_work(struct work_struct *w)
0710 {
0711     struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work);
0712     struct fb_info *info = par->info;
0713     unsigned long flags;
0714     int x1, x2, y1, y2;
0715     int j;
0716 
0717     spin_lock_irqsave(&par->delayed_refresh_lock, flags);
0718     /* Reset the request flag */
0719     par->delayed_refresh = false;
0720 
0721     /* Store the dirty rectangle to local variables */
0722     x1 = par->x1;
0723     x2 = par->x2;
0724     y1 = par->y1;
0725     y2 = par->y2;
0726 
0727     /* Clear dirty rectangle */
0728     par->x1 = par->y1 = INT_MAX;
0729     par->x2 = par->y2 = 0;
0730 
0731     spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
0732 
0733     if (x1 > info->var.xres || x2 > info->var.xres ||
0734         y1 > info->var.yres || y2 > info->var.yres || x2 <= x1)
0735         return;
0736 
0737     /* Copy the dirty rectangle to frame buffer memory */
0738     if (par->need_docopy)
0739         for (j = y1; j < y2; j++)
0740             hvfb_docopy(par,
0741                     j * info->fix.line_length +
0742                     (x1 * screen_depth / 8),
0743                     (x2 - x1) * screen_depth / 8);
0744 
0745     /* Refresh */
0746     if (par->fb_ready && par->update)
0747         synthvid_update(info, x1, y1, x2, y2);
0748 }
0749 
0750 /*
0751  * Control the on-demand refresh frequency. It schedules a delayed
0752  * screen update if it has not yet.
0753  */
0754 static void hvfb_ondemand_refresh_throttle(struct hvfb_par *par,
0755                        int x1, int y1, int w, int h)
0756 {
0757     unsigned long flags;
0758     int x2 = x1 + w;
0759     int y2 = y1 + h;
0760 
0761     spin_lock_irqsave(&par->delayed_refresh_lock, flags);
0762 
0763     /* Merge dirty rectangle */
0764     par->x1 = min_t(int, par->x1, x1);
0765     par->y1 = min_t(int, par->y1, y1);
0766     par->x2 = max_t(int, par->x2, x2);
0767     par->y2 = max_t(int, par->y2, y2);
0768 
0769     /* Schedule a delayed screen update if not yet */
0770     if (par->delayed_refresh == false) {
0771         schedule_delayed_work(&par->dwork,
0772                       HVFB_ONDEMAND_THROTTLE);
0773         par->delayed_refresh = true;
0774     }
0775 
0776     spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
0777 }
0778 
0779 static int hvfb_on_panic(struct notifier_block *nb,
0780              unsigned long e, void *p)
0781 {
0782     struct hvfb_par *par;
0783     struct fb_info *info;
0784 
0785     par = container_of(nb, struct hvfb_par, hvfb_panic_nb);
0786     par->synchronous_fb = true;
0787     info = par->info;
0788     if (par->need_docopy)
0789         hvfb_docopy(par, 0, dio_fb_size);
0790     synthvid_update(info, 0, 0, INT_MAX, INT_MAX);
0791 
0792     return NOTIFY_DONE;
0793 }
0794 
0795 /* Framebuffer operation handlers */
0796 
0797 static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
0798 {
0799     if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN ||
0800         var->xres > screen_width || var->yres >  screen_height ||
0801         var->bits_per_pixel != screen_depth)
0802         return -EINVAL;
0803 
0804     var->xres_virtual = var->xres;
0805     var->yres_virtual = var->yres;
0806 
0807     return 0;
0808 }
0809 
0810 static int hvfb_set_par(struct fb_info *info)
0811 {
0812     struct hv_device *hdev = device_to_hv_device(info->device);
0813 
0814     return synthvid_send_situ(hdev);
0815 }
0816 
0817 
0818 static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf)
0819 {
0820     return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset;
0821 }
0822 
0823 static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
0824               unsigned blue, unsigned transp, struct fb_info *info)
0825 {
0826     u32 *pal = info->pseudo_palette;
0827 
0828     if (regno > 15)
0829         return -EINVAL;
0830 
0831     pal[regno] = chan_to_field(red, &info->var.red)
0832         | chan_to_field(green, &info->var.green)
0833         | chan_to_field(blue, &info->var.blue)
0834         | chan_to_field(transp, &info->var.transp);
0835 
0836     return 0;
0837 }
0838 
0839 static int hvfb_blank(int blank, struct fb_info *info)
0840 {
0841     return 1;   /* get fb_blank to set the colormap to all black */
0842 }
0843 
0844 static void hvfb_cfb_fillrect(struct fb_info *p,
0845                   const struct fb_fillrect *rect)
0846 {
0847     struct hvfb_par *par = p->par;
0848 
0849     cfb_fillrect(p, rect);
0850     if (par->synchronous_fb)
0851         synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
0852     else
0853         hvfb_ondemand_refresh_throttle(par, rect->dx, rect->dy,
0854                            rect->width, rect->height);
0855 }
0856 
0857 static void hvfb_cfb_copyarea(struct fb_info *p,
0858                   const struct fb_copyarea *area)
0859 {
0860     struct hvfb_par *par = p->par;
0861 
0862     cfb_copyarea(p, area);
0863     if (par->synchronous_fb)
0864         synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
0865     else
0866         hvfb_ondemand_refresh_throttle(par, area->dx, area->dy,
0867                            area->width, area->height);
0868 }
0869 
0870 static void hvfb_cfb_imageblit(struct fb_info *p,
0871                    const struct fb_image *image)
0872 {
0873     struct hvfb_par *par = p->par;
0874 
0875     cfb_imageblit(p, image);
0876     if (par->synchronous_fb)
0877         synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
0878     else
0879         hvfb_ondemand_refresh_throttle(par, image->dx, image->dy,
0880                            image->width, image->height);
0881 }
0882 
0883 static const struct fb_ops hvfb_ops = {
0884     .owner = THIS_MODULE,
0885     .fb_check_var = hvfb_check_var,
0886     .fb_set_par = hvfb_set_par,
0887     .fb_setcolreg = hvfb_setcolreg,
0888     .fb_fillrect = hvfb_cfb_fillrect,
0889     .fb_copyarea = hvfb_cfb_copyarea,
0890     .fb_imageblit = hvfb_cfb_imageblit,
0891     .fb_blank = hvfb_blank,
0892     .fb_mmap = fb_deferred_io_mmap,
0893 };
0894 
0895 
0896 /* Get options from kernel paramenter "video=" */
0897 static void hvfb_get_option(struct fb_info *info)
0898 {
0899     struct hvfb_par *par = info->par;
0900     char *opt = NULL, *p;
0901     uint x = 0, y = 0;
0902 
0903     if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt)
0904         return;
0905 
0906     p = strsep(&opt, "x");
0907     if (!*p || kstrtouint(p, 0, &x) ||
0908         !opt || !*opt || kstrtouint(opt, 0, &y)) {
0909         pr_err("Screen option is invalid: skipped\n");
0910         return;
0911     }
0912 
0913     if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN ||
0914         (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10) &&
0915         (x * y * screen_depth / 8 > screen_fb_size)) ||
0916         (par->synthvid_version == SYNTHVID_VERSION_WIN8 &&
0917          x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8)) {
0918         pr_err("Screen resolution option is out of range: skipped\n");
0919         return;
0920     }
0921 
0922     screen_width = x;
0923     screen_height = y;
0924     return;
0925 }
0926 
0927 /*
0928  * Allocate enough contiguous physical memory.
0929  * Return physical address if succeeded or -1 if failed.
0930  */
0931 static phys_addr_t hvfb_get_phymem(struct hv_device *hdev,
0932                    unsigned int request_size)
0933 {
0934     struct page *page = NULL;
0935     dma_addr_t dma_handle;
0936     void *vmem;
0937     phys_addr_t paddr = 0;
0938     unsigned int order = get_order(request_size);
0939 
0940     if (request_size == 0)
0941         return -1;
0942 
0943     if (order < MAX_ORDER) {
0944         /* Call alloc_pages if the size is less than 2^MAX_ORDER */
0945         page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
0946         if (!page)
0947             return -1;
0948 
0949         paddr = (page_to_pfn(page) << PAGE_SHIFT);
0950     } else {
0951         /* Allocate from CMA */
0952         hdev->device.coherent_dma_mask = DMA_BIT_MASK(64);
0953 
0954         vmem = dma_alloc_coherent(&hdev->device,
0955                       round_up(request_size, PAGE_SIZE),
0956                       &dma_handle,
0957                       GFP_KERNEL | __GFP_NOWARN);
0958 
0959         if (!vmem)
0960             return -1;
0961 
0962         paddr = virt_to_phys(vmem);
0963     }
0964 
0965     return paddr;
0966 }
0967 
0968 /* Release contiguous physical memory */
0969 static void hvfb_release_phymem(struct hv_device *hdev,
0970                 phys_addr_t paddr, unsigned int size)
0971 {
0972     unsigned int order = get_order(size);
0973 
0974     if (order < MAX_ORDER)
0975         __free_pages(pfn_to_page(paddr >> PAGE_SHIFT), order);
0976     else
0977         dma_free_coherent(&hdev->device,
0978                   round_up(size, PAGE_SIZE),
0979                   phys_to_virt(paddr),
0980                   paddr);
0981 }
0982 
0983 
0984 /* Get framebuffer memory from Hyper-V video pci space */
0985 static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info)
0986 {
0987     struct hvfb_par *par = info->par;
0988     struct pci_dev *pdev  = NULL;
0989     void __iomem *fb_virt;
0990     int gen2vm = efi_enabled(EFI_BOOT);
0991     phys_addr_t paddr;
0992     int ret;
0993 
0994     info->apertures = alloc_apertures(1);
0995     if (!info->apertures)
0996         return -ENOMEM;
0997 
0998     if (!gen2vm) {
0999         pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
1000             PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
1001         if (!pdev) {
1002             pr_err("Unable to find PCI Hyper-V video\n");
1003             return -ENODEV;
1004         }
1005 
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          * For Gen 1 VM, we can directly use the contiguous memory
1011          * from VM. If we succeed, deferred IO happens directly
1012          * on this allocated framebuffer memory, avoiding extra
1013          * memory copy.
1014          */
1015         paddr = hvfb_get_phymem(hdev, screen_fb_size);
1016         if (paddr != (phys_addr_t) -1) {
1017             par->mmio_pp = paddr;
1018             par->mmio_vp = par->dio_vp = __va(paddr);
1019 
1020             info->fix.smem_start = paddr;
1021             info->fix.smem_len = screen_fb_size;
1022             info->screen_base = par->mmio_vp;
1023             info->screen_size = screen_fb_size;
1024 
1025             par->need_docopy = false;
1026             goto getmem_done;
1027         }
1028         pr_info("Unable to allocate enough contiguous physical memory on Gen 1 VM. Using MMIO instead.\n");
1029     } else {
1030         info->apertures->ranges[0].base = screen_info.lfb_base;
1031         info->apertures->ranges[0].size = screen_info.lfb_size;
1032     }
1033 
1034     /*
1035      * Cannot use the contiguous physical memory.
1036      * Allocate mmio space for framebuffer.
1037      */
1038     dio_fb_size =
1039         screen_width * screen_height * screen_depth / 8;
1040 
1041     ret = vmbus_allocate_mmio(&par->mem, hdev, 0, -1,
1042                   screen_fb_size, 0x100000, true);
1043     if (ret != 0) {
1044         pr_err("Unable to allocate framebuffer memory\n");
1045         goto err1;
1046     }
1047 
1048     /*
1049      * Map the VRAM cacheable for performance. This is also required for
1050      * VM Connect to display properly for ARM64 Linux VM, as the host also
1051      * maps the VRAM cacheable.
1052      */
1053     fb_virt = ioremap_cache(par->mem->start, screen_fb_size);
1054     if (!fb_virt)
1055         goto err2;
1056 
1057     /* Allocate memory for deferred IO */
1058     par->dio_vp = vzalloc(round_up(dio_fb_size, PAGE_SIZE));
1059     if (par->dio_vp == NULL)
1060         goto err3;
1061 
1062     /* Physical address of FB device */
1063     par->mmio_pp = par->mem->start;
1064     /* Virtual address of FB device */
1065     par->mmio_vp = (unsigned char *) fb_virt;
1066 
1067     info->fix.smem_start = par->mem->start;
1068     info->fix.smem_len = dio_fb_size;
1069     info->screen_base = par->dio_vp;
1070     info->screen_size = dio_fb_size;
1071 
1072 getmem_done:
1073     remove_conflicting_framebuffers(info->apertures,
1074                     KBUILD_MODNAME, false);
1075 
1076     if (gen2vm) {
1077         /* framebuffer is reallocated, clear screen_info to avoid misuse from kexec */
1078         screen_info.lfb_size = 0;
1079         screen_info.lfb_base = 0;
1080         screen_info.orig_video_isVGA = 0;
1081     } else {
1082         pci_dev_put(pdev);
1083     }
1084 
1085     return 0;
1086 
1087 err3:
1088     iounmap(fb_virt);
1089 err2:
1090     vmbus_free_mmio(par->mem->start, screen_fb_size);
1091     par->mem = NULL;
1092 err1:
1093     if (!gen2vm)
1094         pci_dev_put(pdev);
1095 
1096     return -ENOMEM;
1097 }
1098 
1099 /* Release the framebuffer */
1100 static void hvfb_putmem(struct hv_device *hdev, struct fb_info *info)
1101 {
1102     struct hvfb_par *par = info->par;
1103 
1104     if (par->need_docopy) {
1105         vfree(par->dio_vp);
1106         iounmap(info->screen_base);
1107         vmbus_free_mmio(par->mem->start, screen_fb_size);
1108     } else {
1109         hvfb_release_phymem(hdev, info->fix.smem_start,
1110                     screen_fb_size);
1111     }
1112 
1113     par->mem = NULL;
1114 }
1115 
1116 
1117 static int hvfb_probe(struct hv_device *hdev,
1118               const struct hv_vmbus_device_id *dev_id)
1119 {
1120     struct fb_info *info;
1121     struct hvfb_par *par;
1122     int ret;
1123 
1124     info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device);
1125     if (!info)
1126         return -ENOMEM;
1127 
1128     par = info->par;
1129     par->info = info;
1130     par->fb_ready = false;
1131     par->need_docopy = true;
1132     init_completion(&par->wait);
1133     INIT_DELAYED_WORK(&par->dwork, hvfb_update_work);
1134 
1135     par->delayed_refresh = false;
1136     spin_lock_init(&par->delayed_refresh_lock);
1137     par->x1 = par->y1 = INT_MAX;
1138     par->x2 = par->y2 = 0;
1139 
1140     /* Connect to VSP */
1141     hv_set_drvdata(hdev, info);
1142     ret = synthvid_connect_vsp(hdev);
1143     if (ret) {
1144         pr_err("Unable to connect to VSP\n");
1145         goto error1;
1146     }
1147 
1148     hvfb_get_option(info);
1149     pr_info("Screen resolution: %dx%d, Color depth: %d, Frame buffer size: %d\n",
1150         screen_width, screen_height, screen_depth, screen_fb_size);
1151 
1152     ret = hvfb_getmem(hdev, info);
1153     if (ret) {
1154         pr_err("No memory for framebuffer\n");
1155         goto error2;
1156     }
1157 
1158     /* Set up fb_info */
1159     info->flags = FBINFO_DEFAULT;
1160 
1161     info->var.xres_virtual = info->var.xres = screen_width;
1162     info->var.yres_virtual = info->var.yres = screen_height;
1163     info->var.bits_per_pixel = screen_depth;
1164 
1165     if (info->var.bits_per_pixel == 16) {
1166         info->var.red = (struct fb_bitfield){11, 5, 0};
1167         info->var.green = (struct fb_bitfield){5, 6, 0};
1168         info->var.blue = (struct fb_bitfield){0, 5, 0};
1169         info->var.transp = (struct fb_bitfield){0, 0, 0};
1170     } else {
1171         info->var.red = (struct fb_bitfield){16, 8, 0};
1172         info->var.green = (struct fb_bitfield){8, 8, 0};
1173         info->var.blue = (struct fb_bitfield){0, 8, 0};
1174         info->var.transp = (struct fb_bitfield){24, 8, 0};
1175     }
1176 
1177     info->var.activate = FB_ACTIVATE_NOW;
1178     info->var.height = -1;
1179     info->var.width = -1;
1180     info->var.vmode = FB_VMODE_NONINTERLACED;
1181 
1182     strcpy(info->fix.id, KBUILD_MODNAME);
1183     info->fix.type = FB_TYPE_PACKED_PIXELS;
1184     info->fix.visual = FB_VISUAL_TRUECOLOR;
1185     info->fix.line_length = screen_width * screen_depth / 8;
1186     info->fix.accel = FB_ACCEL_NONE;
1187 
1188     info->fbops = &hvfb_ops;
1189     info->pseudo_palette = par->pseudo_palette;
1190 
1191     /* Initialize deferred IO */
1192     info->fbdefio = &synthvid_defio;
1193     fb_deferred_io_init(info);
1194 
1195     /* Send config to host */
1196     ret = synthvid_send_config(hdev);
1197     if (ret)
1198         goto error;
1199 
1200     ret = register_framebuffer(info);
1201     if (ret) {
1202         pr_err("Unable to register framebuffer\n");
1203         goto error;
1204     }
1205 
1206     par->fb_ready = true;
1207 
1208     par->synchronous_fb = false;
1209     par->hvfb_panic_nb.notifier_call = hvfb_on_panic;
1210     atomic_notifier_chain_register(&panic_notifier_list,
1211                        &par->hvfb_panic_nb);
1212 
1213     return 0;
1214 
1215 error:
1216     fb_deferred_io_cleanup(info);
1217     hvfb_putmem(hdev, info);
1218 error2:
1219     vmbus_close(hdev->channel);
1220 error1:
1221     cancel_delayed_work_sync(&par->dwork);
1222     hv_set_drvdata(hdev, NULL);
1223     framebuffer_release(info);
1224     return ret;
1225 }
1226 
1227 
1228 static int hvfb_remove(struct hv_device *hdev)
1229 {
1230     struct fb_info *info = hv_get_drvdata(hdev);
1231     struct hvfb_par *par = info->par;
1232 
1233     atomic_notifier_chain_unregister(&panic_notifier_list,
1234                      &par->hvfb_panic_nb);
1235 
1236     par->update = false;
1237     par->fb_ready = false;
1238 
1239     fb_deferred_io_cleanup(info);
1240 
1241     unregister_framebuffer(info);
1242     cancel_delayed_work_sync(&par->dwork);
1243 
1244     vmbus_close(hdev->channel);
1245     hv_set_drvdata(hdev, NULL);
1246 
1247     hvfb_putmem(hdev, info);
1248     framebuffer_release(info);
1249 
1250     return 0;
1251 }
1252 
1253 static int hvfb_suspend(struct hv_device *hdev)
1254 {
1255     struct fb_info *info = hv_get_drvdata(hdev);
1256     struct hvfb_par *par = info->par;
1257 
1258     console_lock();
1259 
1260     /* 1 means do suspend */
1261     fb_set_suspend(info, 1);
1262 
1263     cancel_delayed_work_sync(&par->dwork);
1264     cancel_delayed_work_sync(&info->deferred_work);
1265 
1266     par->update_saved = par->update;
1267     par->update = false;
1268     par->fb_ready = false;
1269 
1270     vmbus_close(hdev->channel);
1271 
1272     console_unlock();
1273 
1274     return 0;
1275 }
1276 
1277 static int hvfb_resume(struct hv_device *hdev)
1278 {
1279     struct fb_info *info = hv_get_drvdata(hdev);
1280     struct hvfb_par *par = info->par;
1281     int ret;
1282 
1283     console_lock();
1284 
1285     ret = synthvid_connect_vsp(hdev);
1286     if (ret != 0)
1287         goto out;
1288 
1289     ret = synthvid_send_config(hdev);
1290     if (ret != 0) {
1291         vmbus_close(hdev->channel);
1292         goto out;
1293     }
1294 
1295     par->fb_ready = true;
1296     par->update = par->update_saved;
1297 
1298     schedule_delayed_work(&info->deferred_work, info->fbdefio->delay);
1299     schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
1300 
1301     /* 0 means do resume */
1302     fb_set_suspend(info, 0);
1303 
1304 out:
1305     console_unlock();
1306 
1307     return ret;
1308 }
1309 
1310 
1311 static const struct pci_device_id pci_stub_id_table[] = {
1312     {
1313         .vendor      = PCI_VENDOR_ID_MICROSOFT,
1314         .device      = PCI_DEVICE_ID_HYPERV_VIDEO,
1315     },
1316     { /* end of list */ }
1317 };
1318 
1319 static const struct hv_vmbus_device_id id_table[] = {
1320     /* Synthetic Video Device GUID */
1321     {HV_SYNTHVID_GUID},
1322     {}
1323 };
1324 
1325 MODULE_DEVICE_TABLE(pci, pci_stub_id_table);
1326 MODULE_DEVICE_TABLE(vmbus, id_table);
1327 
1328 static struct hv_driver hvfb_drv = {
1329     .name = KBUILD_MODNAME,
1330     .id_table = id_table,
1331     .probe = hvfb_probe,
1332     .remove = hvfb_remove,
1333     .suspend = hvfb_suspend,
1334     .resume = hvfb_resume,
1335     .driver = {
1336         .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1337     },
1338 };
1339 
1340 static int hvfb_pci_stub_probe(struct pci_dev *pdev,
1341                    const struct pci_device_id *ent)
1342 {
1343     return 0;
1344 }
1345 
1346 static void hvfb_pci_stub_remove(struct pci_dev *pdev)
1347 {
1348 }
1349 
1350 static struct pci_driver hvfb_pci_stub_driver = {
1351     .name =     KBUILD_MODNAME,
1352     .id_table = pci_stub_id_table,
1353     .probe =    hvfb_pci_stub_probe,
1354     .remove =   hvfb_pci_stub_remove,
1355     .driver = {
1356         .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1357     }
1358 };
1359 
1360 static int __init hvfb_drv_init(void)
1361 {
1362     int ret;
1363 
1364     ret = vmbus_driver_register(&hvfb_drv);
1365     if (ret != 0)
1366         return ret;
1367 
1368     ret = pci_register_driver(&hvfb_pci_stub_driver);
1369     if (ret != 0) {
1370         vmbus_driver_unregister(&hvfb_drv);
1371         return ret;
1372     }
1373 
1374     return 0;
1375 }
1376 
1377 static void __exit hvfb_drv_exit(void)
1378 {
1379     pci_unregister_driver(&hvfb_pci_stub_driver);
1380     vmbus_driver_unregister(&hvfb_drv);
1381 }
1382 
1383 module_init(hvfb_drv_init);
1384 module_exit(hvfb_drv_exit);
1385 
1386 MODULE_LICENSE("GPL");
1387 MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver");