143aa3132SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2f7018c21STomi Valkeinen /* 3f7018c21STomi Valkeinen * Copyright (c) 2012, Microsoft Corporation. 4f7018c21STomi Valkeinen * 5f7018c21STomi Valkeinen * Author: 6f7018c21STomi Valkeinen * Haiyang Zhang <haiyangz@microsoft.com> 7f7018c21STomi Valkeinen */ 8f7018c21STomi Valkeinen 9f7018c21STomi Valkeinen /* 10f7018c21STomi Valkeinen * Hyper-V Synthetic Video Frame Buffer Driver 11f7018c21STomi Valkeinen * 12f7018c21STomi Valkeinen * This is the driver for the Hyper-V Synthetic Video, which supports 13f7018c21STomi Valkeinen * screen resolution up to Full HD 1920x1080 with 32 bit color on Windows 14f7018c21STomi Valkeinen * Server 2012, and 1600x1200 with 16 bit color on Windows Server 2008 R2 15f7018c21STomi Valkeinen * or earlier. 16f7018c21STomi Valkeinen * 17f7018c21STomi Valkeinen * It also solves the double mouse cursor issue of the emulated video mode. 18f7018c21STomi Valkeinen * 19f7018c21STomi Valkeinen * The default screen resolution is 1152x864, which may be changed by a 20f7018c21STomi Valkeinen * kernel parameter: 21f7018c21STomi Valkeinen * video=hyperv_fb:<width>x<height> 22f7018c21STomi Valkeinen * For example: video=hyperv_fb:1280x1024 23f7018c21STomi Valkeinen * 24f7018c21STomi Valkeinen * Portrait orientation is also supported: 25f7018c21STomi Valkeinen * For example: video=hyperv_fb:864x1152 2667e7cdb4SWei Hu * 2767e7cdb4SWei Hu * When a Windows 10 RS5+ host is used, the virtual machine screen 2867e7cdb4SWei Hu * resolution is obtained from the host. The "video=hyperv_fb" option is 2967e7cdb4SWei Hu * not needed, but still can be used to overwrite what the host specifies. 3067e7cdb4SWei Hu * The VM resolution on the host could be set by executing the powershell 3167e7cdb4SWei Hu * "set-vmvideo" command. For example 3267e7cdb4SWei Hu * set-vmvideo -vmname name -horizontalresolution:1920 \ 3367e7cdb4SWei Hu * -verticalresolution:1200 -resolutiontype single 343a6fb6c4SWei Hu * 353a6fb6c4SWei Hu * Gen 1 VMs also support direct using VM's physical memory for framebuffer. 363a6fb6c4SWei Hu * It could improve the efficiency and performance for framebuffer and VM. 373a6fb6c4SWei Hu * This requires to allocate contiguous physical memory from Linux kernel's 383a6fb6c4SWei Hu * CMA memory allocator. To enable this, supply a kernel parameter to give 393a6fb6c4SWei Hu * enough memory space to CMA allocator for framebuffer. For example: 403a6fb6c4SWei Hu * cma=130m 413a6fb6c4SWei Hu * This gives 130MB memory to CMA allocator that can be allocated to 423a6fb6c4SWei Hu * framebuffer. For reference, 8K resolution (7680x4320) takes about 433a6fb6c4SWei Hu * 127MB memory. 44f7018c21STomi Valkeinen */ 45f7018c21STomi Valkeinen 46f7018c21STomi Valkeinen #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 47f7018c21STomi Valkeinen 48f7018c21STomi Valkeinen #include <linux/module.h> 49f7018c21STomi Valkeinen #include <linux/kernel.h> 5034a28083SOlaf Hering #include <linux/vmalloc.h> 51f7018c21STomi Valkeinen #include <linux/init.h> 52f7018c21STomi Valkeinen #include <linux/completion.h> 53f7018c21STomi Valkeinen #include <linux/fb.h> 54f7018c21STomi Valkeinen #include <linux/pci.h> 55f39650deSAndy Shevchenko #include <linux/panic_notifier.h> 56f7018c21STomi Valkeinen #include <linux/efi.h> 571ecf3020SDexuan Cui #include <linux/console.h> 58f7018c21STomi Valkeinen 59f7018c21STomi Valkeinen #include <linux/hyperv.h> 60f7018c21STomi Valkeinen 61f7018c21STomi Valkeinen 62f7018c21STomi Valkeinen /* Hyper-V Synthetic Video Protocol definitions and structures */ 63f7018c21STomi Valkeinen #define MAX_VMBUS_PKT_SIZE 0x4000 64f7018c21STomi Valkeinen 65f7018c21STomi Valkeinen #define SYNTHVID_VERSION(major, minor) ((minor) << 16 | (major)) 66f7018c21STomi Valkeinen #define SYNTHVID_VERSION_WIN7 SYNTHVID_VERSION(3, 0) 67f7018c21STomi Valkeinen #define SYNTHVID_VERSION_WIN8 SYNTHVID_VERSION(3, 2) 6867e7cdb4SWei Hu #define SYNTHVID_VERSION_WIN10 SYNTHVID_VERSION(3, 5) 6967e7cdb4SWei Hu 7067e7cdb4SWei Hu #define SYNTHVID_VER_GET_MAJOR(ver) (ver & 0x0000ffff) 7167e7cdb4SWei Hu #define SYNTHVID_VER_GET_MINOR(ver) ((ver & 0xffff0000) >> 16) 72f7018c21STomi Valkeinen 73f7018c21STomi Valkeinen #define SYNTHVID_DEPTH_WIN7 16 74f7018c21STomi Valkeinen #define SYNTHVID_DEPTH_WIN8 32 75f7018c21STomi Valkeinen 76f7018c21STomi Valkeinen #define SYNTHVID_FB_SIZE_WIN7 (4 * 1024 * 1024) 77f7018c21STomi Valkeinen #define SYNTHVID_WIDTH_MAX_WIN7 1600 78f7018c21STomi Valkeinen #define SYNTHVID_HEIGHT_MAX_WIN7 1200 79f7018c21STomi Valkeinen 80f7018c21STomi Valkeinen #define SYNTHVID_FB_SIZE_WIN8 (8 * 1024 * 1024) 81f7018c21STomi Valkeinen 82f7018c21STomi Valkeinen #define PCI_VENDOR_ID_MICROSOFT 0x1414 83f7018c21STomi Valkeinen #define PCI_DEVICE_ID_HYPERV_VIDEO 0x5353 84f7018c21STomi Valkeinen 85f7018c21STomi Valkeinen 86f7018c21STomi Valkeinen enum pipe_msg_type { 87f7018c21STomi Valkeinen PIPE_MSG_INVALID, 88f7018c21STomi Valkeinen PIPE_MSG_DATA, 89f7018c21STomi Valkeinen PIPE_MSG_MAX 90f7018c21STomi Valkeinen }; 91f7018c21STomi Valkeinen 92f7018c21STomi Valkeinen struct pipe_msg_hdr { 93f7018c21STomi Valkeinen u32 type; 94f7018c21STomi Valkeinen u32 size; /* size of message after this field */ 95f7018c21STomi Valkeinen } __packed; 96f7018c21STomi Valkeinen 97f7018c21STomi Valkeinen 98f7018c21STomi Valkeinen enum synthvid_msg_type { 99f7018c21STomi Valkeinen SYNTHVID_ERROR = 0, 100f7018c21STomi Valkeinen SYNTHVID_VERSION_REQUEST = 1, 101f7018c21STomi Valkeinen SYNTHVID_VERSION_RESPONSE = 2, 102f7018c21STomi Valkeinen SYNTHVID_VRAM_LOCATION = 3, 103f7018c21STomi Valkeinen SYNTHVID_VRAM_LOCATION_ACK = 4, 104f7018c21STomi Valkeinen SYNTHVID_SITUATION_UPDATE = 5, 105f7018c21STomi Valkeinen SYNTHVID_SITUATION_UPDATE_ACK = 6, 106f7018c21STomi Valkeinen SYNTHVID_POINTER_POSITION = 7, 107f7018c21STomi Valkeinen SYNTHVID_POINTER_SHAPE = 8, 108f7018c21STomi Valkeinen SYNTHVID_FEATURE_CHANGE = 9, 109f7018c21STomi Valkeinen SYNTHVID_DIRT = 10, 11067e7cdb4SWei Hu SYNTHVID_RESOLUTION_REQUEST = 13, 11167e7cdb4SWei Hu SYNTHVID_RESOLUTION_RESPONSE = 14, 112f7018c21STomi Valkeinen 11367e7cdb4SWei Hu SYNTHVID_MAX = 15 114f7018c21STomi Valkeinen }; 115f7018c21STomi Valkeinen 11667e7cdb4SWei Hu #define SYNTHVID_EDID_BLOCK_SIZE 128 11767e7cdb4SWei Hu #define SYNTHVID_MAX_RESOLUTION_COUNT 64 11867e7cdb4SWei Hu 11967e7cdb4SWei Hu struct hvd_screen_info { 12067e7cdb4SWei Hu u16 width; 12167e7cdb4SWei Hu u16 height; 12267e7cdb4SWei Hu } __packed; 12367e7cdb4SWei Hu 124f7018c21STomi Valkeinen struct synthvid_msg_hdr { 125f7018c21STomi Valkeinen u32 type; 126f7018c21STomi Valkeinen u32 size; /* size of this header + payload after this field*/ 127f7018c21STomi Valkeinen } __packed; 128f7018c21STomi Valkeinen 129f7018c21STomi Valkeinen struct synthvid_version_req { 130f7018c21STomi Valkeinen u32 version; 131f7018c21STomi Valkeinen } __packed; 132f7018c21STomi Valkeinen 133f7018c21STomi Valkeinen struct synthvid_version_resp { 134f7018c21STomi Valkeinen u32 version; 135f7018c21STomi Valkeinen u8 is_accepted; 136f7018c21STomi Valkeinen u8 max_video_outputs; 137f7018c21STomi Valkeinen } __packed; 138f7018c21STomi Valkeinen 13967e7cdb4SWei Hu struct synthvid_supported_resolution_req { 14067e7cdb4SWei Hu u8 maximum_resolution_count; 14167e7cdb4SWei Hu } __packed; 14267e7cdb4SWei Hu 14367e7cdb4SWei Hu struct synthvid_supported_resolution_resp { 14467e7cdb4SWei Hu u8 edid_block[SYNTHVID_EDID_BLOCK_SIZE]; 14567e7cdb4SWei Hu u8 resolution_count; 14667e7cdb4SWei Hu u8 default_resolution_index; 14767e7cdb4SWei Hu u8 is_standard; 14867e7cdb4SWei Hu struct hvd_screen_info 14967e7cdb4SWei Hu supported_resolution[SYNTHVID_MAX_RESOLUTION_COUNT]; 15067e7cdb4SWei Hu } __packed; 15167e7cdb4SWei Hu 152f7018c21STomi Valkeinen struct synthvid_vram_location { 153f7018c21STomi Valkeinen u64 user_ctx; 154f7018c21STomi Valkeinen u8 is_vram_gpa_specified; 155f7018c21STomi Valkeinen u64 vram_gpa; 156f7018c21STomi Valkeinen } __packed; 157f7018c21STomi Valkeinen 158f7018c21STomi Valkeinen struct synthvid_vram_location_ack { 159f7018c21STomi Valkeinen u64 user_ctx; 160f7018c21STomi Valkeinen } __packed; 161f7018c21STomi Valkeinen 162f7018c21STomi Valkeinen struct video_output_situation { 163f7018c21STomi Valkeinen u8 active; 164f7018c21STomi Valkeinen u32 vram_offset; 165f7018c21STomi Valkeinen u8 depth_bits; 166f7018c21STomi Valkeinen u32 width_pixels; 167f7018c21STomi Valkeinen u32 height_pixels; 168f7018c21STomi Valkeinen u32 pitch_bytes; 169f7018c21STomi Valkeinen } __packed; 170f7018c21STomi Valkeinen 171f7018c21STomi Valkeinen struct synthvid_situation_update { 172f7018c21STomi Valkeinen u64 user_ctx; 173f7018c21STomi Valkeinen u8 video_output_count; 174f7018c21STomi Valkeinen struct video_output_situation video_output[1]; 175f7018c21STomi Valkeinen } __packed; 176f7018c21STomi Valkeinen 177f7018c21STomi Valkeinen struct synthvid_situation_update_ack { 178f7018c21STomi Valkeinen u64 user_ctx; 179f7018c21STomi Valkeinen } __packed; 180f7018c21STomi Valkeinen 181f7018c21STomi Valkeinen struct synthvid_pointer_position { 182f7018c21STomi Valkeinen u8 is_visible; 183f7018c21STomi Valkeinen u8 video_output; 184f7018c21STomi Valkeinen s32 image_x; 185f7018c21STomi Valkeinen s32 image_y; 186f7018c21STomi Valkeinen } __packed; 187f7018c21STomi Valkeinen 188f7018c21STomi Valkeinen 189f7018c21STomi Valkeinen #define CURSOR_MAX_X 96 190f7018c21STomi Valkeinen #define CURSOR_MAX_Y 96 191f7018c21STomi Valkeinen #define CURSOR_ARGB_PIXEL_SIZE 4 192f7018c21STomi Valkeinen #define CURSOR_MAX_SIZE (CURSOR_MAX_X * CURSOR_MAX_Y * CURSOR_ARGB_PIXEL_SIZE) 193f7018c21STomi Valkeinen #define CURSOR_COMPLETE (-1) 194f7018c21STomi Valkeinen 195f7018c21STomi Valkeinen struct synthvid_pointer_shape { 196f7018c21STomi Valkeinen u8 part_idx; 197f7018c21STomi Valkeinen u8 is_argb; 198f7018c21STomi Valkeinen u32 width; /* CURSOR_MAX_X at most */ 199f7018c21STomi Valkeinen u32 height; /* CURSOR_MAX_Y at most */ 200f7018c21STomi Valkeinen u32 hot_x; /* hotspot relative to upper-left of pointer image */ 201f7018c21STomi Valkeinen u32 hot_y; 202f7018c21STomi Valkeinen u8 data[4]; 203f7018c21STomi Valkeinen } __packed; 204f7018c21STomi Valkeinen 205f7018c21STomi Valkeinen struct synthvid_feature_change { 206f7018c21STomi Valkeinen u8 is_dirt_needed; 207f7018c21STomi Valkeinen u8 is_ptr_pos_needed; 208f7018c21STomi Valkeinen u8 is_ptr_shape_needed; 209f7018c21STomi Valkeinen u8 is_situ_needed; 210f7018c21STomi Valkeinen } __packed; 211f7018c21STomi Valkeinen 212f7018c21STomi Valkeinen struct rect { 213f7018c21STomi Valkeinen s32 x1, y1; /* top left corner */ 214f7018c21STomi Valkeinen s32 x2, y2; /* bottom right corner, exclusive */ 215f7018c21STomi Valkeinen } __packed; 216f7018c21STomi Valkeinen 217f7018c21STomi Valkeinen struct synthvid_dirt { 218f7018c21STomi Valkeinen u8 video_output; 219f7018c21STomi Valkeinen u8 dirt_count; 220f7018c21STomi Valkeinen struct rect rect[1]; 221f7018c21STomi Valkeinen } __packed; 222f7018c21STomi Valkeinen 223f7018c21STomi Valkeinen struct synthvid_msg { 224f7018c21STomi Valkeinen struct pipe_msg_hdr pipe_hdr; 225f7018c21STomi Valkeinen struct synthvid_msg_hdr vid_hdr; 226f7018c21STomi Valkeinen union { 227f7018c21STomi Valkeinen struct synthvid_version_req ver_req; 228f7018c21STomi Valkeinen struct synthvid_version_resp ver_resp; 229f7018c21STomi Valkeinen struct synthvid_vram_location vram; 230f7018c21STomi Valkeinen struct synthvid_vram_location_ack vram_ack; 231f7018c21STomi Valkeinen struct synthvid_situation_update situ; 232f7018c21STomi Valkeinen struct synthvid_situation_update_ack situ_ack; 233f7018c21STomi Valkeinen struct synthvid_pointer_position ptr_pos; 234f7018c21STomi Valkeinen struct synthvid_pointer_shape ptr_shape; 235f7018c21STomi Valkeinen struct synthvid_feature_change feature_chg; 236f7018c21STomi Valkeinen struct synthvid_dirt dirt; 23767e7cdb4SWei Hu struct synthvid_supported_resolution_req resolution_req; 23867e7cdb4SWei Hu struct synthvid_supported_resolution_resp resolution_resp; 239f7018c21STomi Valkeinen }; 240f7018c21STomi Valkeinen } __packed; 241f7018c21STomi Valkeinen 242f7018c21STomi Valkeinen 243f7018c21STomi Valkeinen /* FB driver definitions and structures */ 244f7018c21STomi Valkeinen #define HVFB_WIDTH 1152 /* default screen width */ 245f7018c21STomi Valkeinen #define HVFB_HEIGHT 864 /* default screen height */ 246f7018c21STomi Valkeinen #define HVFB_WIDTH_MIN 640 247f7018c21STomi Valkeinen #define HVFB_HEIGHT_MIN 480 248f7018c21STomi Valkeinen 249f7018c21STomi Valkeinen #define RING_BUFSIZE (256 * 1024) 250f7018c21STomi Valkeinen #define VSP_TIMEOUT (10 * HZ) 251f7018c21STomi Valkeinen #define HVFB_UPDATE_DELAY (HZ / 20) 252d21987d7SWei Hu #define HVFB_ONDEMAND_THROTTLE (HZ / 20) 253f7018c21STomi Valkeinen 254f7018c21STomi Valkeinen struct hvfb_par { 255f7018c21STomi Valkeinen struct fb_info *info; 25635464483SJake Oshins struct resource *mem; 257f7018c21STomi Valkeinen bool fb_ready; /* fb device is ready */ 258f7018c21STomi Valkeinen struct completion wait; 259f7018c21STomi Valkeinen u32 synthvid_version; 260f7018c21STomi Valkeinen 261f7018c21STomi Valkeinen struct delayed_work dwork; 262f7018c21STomi Valkeinen bool update; 2631ecf3020SDexuan Cui bool update_saved; /* The value of 'update' before hibernation */ 264f7018c21STomi Valkeinen 265f7018c21STomi Valkeinen u32 pseudo_palette[16]; 266f7018c21STomi Valkeinen u8 init_buf[MAX_VMBUS_PKT_SIZE]; 267f7018c21STomi Valkeinen u8 recv_buf[MAX_VMBUS_PKT_SIZE]; 2683686fe96SDexuan Cui 2693686fe96SDexuan Cui /* If true, the VSC notifies the VSP on every framebuffer change */ 2703686fe96SDexuan Cui bool synchronous_fb; 2713686fe96SDexuan Cui 2723a6fb6c4SWei Hu /* If true, need to copy from deferred IO mem to framebuffer mem */ 2733a6fb6c4SWei Hu bool need_docopy; 2743a6fb6c4SWei Hu 2753686fe96SDexuan Cui struct notifier_block hvfb_panic_nb; 276d21987d7SWei Hu 277d21987d7SWei Hu /* Memory for deferred IO and frame buffer itself */ 278d21987d7SWei Hu unsigned char *dio_vp; 279d21987d7SWei Hu unsigned char *mmio_vp; 2803a6fb6c4SWei Hu phys_addr_t mmio_pp; 281d21987d7SWei Hu 282d21987d7SWei Hu /* Dirty rectangle, protected by delayed_refresh_lock */ 283d21987d7SWei Hu int x1, y1, x2, y2; 284d21987d7SWei Hu bool delayed_refresh; 285d21987d7SWei Hu spinlock_t delayed_refresh_lock; 286f7018c21STomi Valkeinen }; 287f7018c21STomi Valkeinen 288f7018c21STomi Valkeinen static uint screen_width = HVFB_WIDTH; 289f7018c21STomi Valkeinen static uint screen_height = HVFB_HEIGHT; 290f7018c21STomi Valkeinen static uint screen_depth; 291f7018c21STomi Valkeinen static uint screen_fb_size; 292d21987d7SWei Hu static uint dio_fb_size; /* FB size for deferred IO */ 293f7018c21STomi Valkeinen 294f7018c21STomi Valkeinen /* Send message to Hyper-V host */ 295f7018c21STomi Valkeinen static inline int synthvid_send(struct hv_device *hdev, 296f7018c21STomi Valkeinen struct synthvid_msg *msg) 297f7018c21STomi Valkeinen { 298f7018c21STomi Valkeinen static atomic64_t request_id = ATOMIC64_INIT(0); 299f7018c21STomi Valkeinen int ret; 300f7018c21STomi Valkeinen 301f7018c21STomi Valkeinen msg->pipe_hdr.type = PIPE_MSG_DATA; 302f7018c21STomi Valkeinen msg->pipe_hdr.size = msg->vid_hdr.size; 303f7018c21STomi Valkeinen 304f7018c21STomi Valkeinen ret = vmbus_sendpacket(hdev->channel, msg, 305f7018c21STomi Valkeinen msg->vid_hdr.size + sizeof(struct pipe_msg_hdr), 306f7018c21STomi Valkeinen atomic64_inc_return(&request_id), 307f7018c21STomi Valkeinen VM_PKT_DATA_INBAND, 0); 308f7018c21STomi Valkeinen 309f7018c21STomi Valkeinen if (ret) 310aa5b7d11SMichael Kelley pr_err_ratelimited("Unable to send packet via vmbus; error %d\n", ret); 311f7018c21STomi Valkeinen 312f7018c21STomi Valkeinen return ret; 313f7018c21STomi Valkeinen } 314f7018c21STomi Valkeinen 315f7018c21STomi Valkeinen 316f7018c21STomi Valkeinen /* Send screen resolution info to host */ 317f7018c21STomi Valkeinen static int synthvid_send_situ(struct hv_device *hdev) 318f7018c21STomi Valkeinen { 319f7018c21STomi Valkeinen struct fb_info *info = hv_get_drvdata(hdev); 320f7018c21STomi Valkeinen struct synthvid_msg msg; 321f7018c21STomi Valkeinen 322f7018c21STomi Valkeinen if (!info) 323f7018c21STomi Valkeinen return -ENODEV; 324f7018c21STomi Valkeinen 325f7018c21STomi Valkeinen memset(&msg, 0, sizeof(struct synthvid_msg)); 326f7018c21STomi Valkeinen 327f7018c21STomi Valkeinen msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE; 328f7018c21STomi Valkeinen msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) + 329f7018c21STomi Valkeinen sizeof(struct synthvid_situation_update); 330f7018c21STomi Valkeinen msg.situ.user_ctx = 0; 331f7018c21STomi Valkeinen msg.situ.video_output_count = 1; 332f7018c21STomi Valkeinen msg.situ.video_output[0].active = 1; 333f7018c21STomi Valkeinen msg.situ.video_output[0].vram_offset = 0; 334f7018c21STomi Valkeinen msg.situ.video_output[0].depth_bits = info->var.bits_per_pixel; 335f7018c21STomi Valkeinen msg.situ.video_output[0].width_pixels = info->var.xres; 336f7018c21STomi Valkeinen msg.situ.video_output[0].height_pixels = info->var.yres; 337f7018c21STomi Valkeinen msg.situ.video_output[0].pitch_bytes = info->fix.line_length; 338f7018c21STomi Valkeinen 339f7018c21STomi Valkeinen synthvid_send(hdev, &msg); 340f7018c21STomi Valkeinen 341f7018c21STomi Valkeinen return 0; 342f7018c21STomi Valkeinen } 343f7018c21STomi Valkeinen 344f7018c21STomi Valkeinen /* Send mouse pointer info to host */ 345f7018c21STomi Valkeinen static int synthvid_send_ptr(struct hv_device *hdev) 346f7018c21STomi Valkeinen { 347f7018c21STomi Valkeinen struct synthvid_msg msg; 348f7018c21STomi Valkeinen 349f7018c21STomi Valkeinen memset(&msg, 0, sizeof(struct synthvid_msg)); 350f7018c21STomi Valkeinen msg.vid_hdr.type = SYNTHVID_POINTER_POSITION; 351f7018c21STomi Valkeinen msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) + 352f7018c21STomi Valkeinen sizeof(struct synthvid_pointer_position); 353f7018c21STomi Valkeinen msg.ptr_pos.is_visible = 1; 354f7018c21STomi Valkeinen msg.ptr_pos.video_output = 0; 355f7018c21STomi Valkeinen msg.ptr_pos.image_x = 0; 356f7018c21STomi Valkeinen msg.ptr_pos.image_y = 0; 357f7018c21STomi Valkeinen synthvid_send(hdev, &msg); 358f7018c21STomi Valkeinen 359f7018c21STomi Valkeinen memset(&msg, 0, sizeof(struct synthvid_msg)); 360f7018c21STomi Valkeinen msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE; 361f7018c21STomi Valkeinen msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) + 362f7018c21STomi Valkeinen sizeof(struct synthvid_pointer_shape); 363f7018c21STomi Valkeinen msg.ptr_shape.part_idx = CURSOR_COMPLETE; 364f7018c21STomi Valkeinen msg.ptr_shape.is_argb = 1; 365f7018c21STomi Valkeinen msg.ptr_shape.width = 1; 366f7018c21STomi Valkeinen msg.ptr_shape.height = 1; 367f7018c21STomi Valkeinen msg.ptr_shape.hot_x = 0; 368f7018c21STomi Valkeinen msg.ptr_shape.hot_y = 0; 369f7018c21STomi Valkeinen msg.ptr_shape.data[0] = 0; 370f7018c21STomi Valkeinen msg.ptr_shape.data[1] = 1; 371f7018c21STomi Valkeinen msg.ptr_shape.data[2] = 1; 372f7018c21STomi Valkeinen msg.ptr_shape.data[3] = 1; 373f7018c21STomi Valkeinen synthvid_send(hdev, &msg); 374f7018c21STomi Valkeinen 375f7018c21STomi Valkeinen return 0; 376f7018c21STomi Valkeinen } 377f7018c21STomi Valkeinen 378f7018c21STomi Valkeinen /* Send updated screen area (dirty rectangle) location to host */ 379d21987d7SWei Hu static int 380d21987d7SWei Hu synthvid_update(struct fb_info *info, int x1, int y1, int x2, int y2) 381f7018c21STomi Valkeinen { 382f7018c21STomi Valkeinen struct hv_device *hdev = device_to_hv_device(info->device); 383f7018c21STomi Valkeinen struct synthvid_msg msg; 384f7018c21STomi Valkeinen 385f7018c21STomi Valkeinen memset(&msg, 0, sizeof(struct synthvid_msg)); 386d21987d7SWei Hu if (x2 == INT_MAX) 387d21987d7SWei Hu x2 = info->var.xres; 388d21987d7SWei Hu if (y2 == INT_MAX) 389d21987d7SWei Hu y2 = info->var.yres; 390f7018c21STomi Valkeinen 391f7018c21STomi Valkeinen msg.vid_hdr.type = SYNTHVID_DIRT; 392f7018c21STomi Valkeinen msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) + 393f7018c21STomi Valkeinen sizeof(struct synthvid_dirt); 394f7018c21STomi Valkeinen msg.dirt.video_output = 0; 395f7018c21STomi Valkeinen msg.dirt.dirt_count = 1; 396d21987d7SWei Hu msg.dirt.rect[0].x1 = (x1 > x2) ? 0 : x1; 397d21987d7SWei Hu msg.dirt.rect[0].y1 = (y1 > y2) ? 0 : y1; 398d21987d7SWei Hu msg.dirt.rect[0].x2 = 399d21987d7SWei Hu (x2 < x1 || x2 > info->var.xres) ? info->var.xres : x2; 400d21987d7SWei Hu msg.dirt.rect[0].y2 = 401d21987d7SWei Hu (y2 < y1 || y2 > info->var.yres) ? info->var.yres : y2; 402f7018c21STomi Valkeinen 403f7018c21STomi Valkeinen synthvid_send(hdev, &msg); 404f7018c21STomi Valkeinen 405f7018c21STomi Valkeinen return 0; 406f7018c21STomi Valkeinen } 407f7018c21STomi Valkeinen 408d21987d7SWei Hu static void hvfb_docopy(struct hvfb_par *par, 409d21987d7SWei Hu unsigned long offset, 410d21987d7SWei Hu unsigned long size) 411d21987d7SWei Hu { 412d21987d7SWei Hu if (!par || !par->mmio_vp || !par->dio_vp || !par->fb_ready || 413d21987d7SWei Hu size == 0 || offset >= dio_fb_size) 414d21987d7SWei Hu return; 415d21987d7SWei Hu 416d21987d7SWei Hu if (offset + size > dio_fb_size) 417d21987d7SWei Hu size = dio_fb_size - offset; 418d21987d7SWei Hu 419d21987d7SWei Hu memcpy(par->mmio_vp + offset, par->dio_vp + offset, size); 420d21987d7SWei Hu } 421d21987d7SWei Hu 422d21987d7SWei Hu /* Deferred IO callback */ 423d21987d7SWei Hu static void synthvid_deferred_io(struct fb_info *p, 424d21987d7SWei Hu struct list_head *pagelist) 425d21987d7SWei Hu { 426d21987d7SWei Hu struct hvfb_par *par = p->par; 427d21987d7SWei Hu struct page *page; 428d21987d7SWei Hu unsigned long start, end; 429d21987d7SWei Hu int y1, y2, miny, maxy; 430d21987d7SWei Hu 431d21987d7SWei Hu miny = INT_MAX; 432d21987d7SWei Hu maxy = 0; 433d21987d7SWei Hu 434d21987d7SWei Hu /* 435d21987d7SWei Hu * Merge dirty pages. It is possible that last page cross 436d21987d7SWei Hu * over the end of frame buffer row yres. This is taken care of 437d21987d7SWei Hu * in synthvid_update function by clamping the y2 438d21987d7SWei Hu * value to yres. 439d21987d7SWei Hu */ 440d21987d7SWei Hu list_for_each_entry(page, pagelist, lru) { 441d21987d7SWei Hu start = page->index << PAGE_SHIFT; 442d21987d7SWei Hu end = start + PAGE_SIZE - 1; 443d21987d7SWei Hu y1 = start / p->fix.line_length; 444d21987d7SWei Hu y2 = end / p->fix.line_length; 445d21987d7SWei Hu miny = min_t(int, miny, y1); 446d21987d7SWei Hu maxy = max_t(int, maxy, y2); 447d21987d7SWei Hu 448d21987d7SWei Hu /* Copy from dio space to mmio address */ 4493a6fb6c4SWei Hu if (par->fb_ready && par->need_docopy) 450d21987d7SWei Hu hvfb_docopy(par, start, PAGE_SIZE); 451d21987d7SWei Hu } 452d21987d7SWei Hu 453d21987d7SWei Hu if (par->fb_ready && par->update) 454d21987d7SWei Hu synthvid_update(p, 0, miny, p->var.xres, maxy + 1); 455d21987d7SWei Hu } 456d21987d7SWei Hu 457d21987d7SWei Hu static struct fb_deferred_io synthvid_defio = { 458d21987d7SWei Hu .delay = HZ / 20, 459d21987d7SWei Hu .deferred_io = synthvid_deferred_io, 460d21987d7SWei Hu }; 461f7018c21STomi Valkeinen 462f7018c21STomi Valkeinen /* 463f7018c21STomi Valkeinen * Actions on received messages from host: 464f7018c21STomi Valkeinen * Complete the wait event. 465f7018c21STomi Valkeinen * Or, reply with screen and cursor info. 466f7018c21STomi Valkeinen */ 467f7018c21STomi Valkeinen static void synthvid_recv_sub(struct hv_device *hdev) 468f7018c21STomi Valkeinen { 469f7018c21STomi Valkeinen struct fb_info *info = hv_get_drvdata(hdev); 470f7018c21STomi Valkeinen struct hvfb_par *par; 471f7018c21STomi Valkeinen struct synthvid_msg *msg; 472f7018c21STomi Valkeinen 473f7018c21STomi Valkeinen if (!info) 474f7018c21STomi Valkeinen return; 475f7018c21STomi Valkeinen 476f7018c21STomi Valkeinen par = info->par; 477f7018c21STomi Valkeinen msg = (struct synthvid_msg *)par->recv_buf; 478f7018c21STomi Valkeinen 479f7018c21STomi Valkeinen /* Complete the wait event */ 480f7018c21STomi Valkeinen if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE || 48167e7cdb4SWei Hu msg->vid_hdr.type == SYNTHVID_RESOLUTION_RESPONSE || 482f7018c21STomi Valkeinen msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) { 483f7018c21STomi Valkeinen memcpy(par->init_buf, msg, MAX_VMBUS_PKT_SIZE); 484f7018c21STomi Valkeinen complete(&par->wait); 485f7018c21STomi Valkeinen return; 486f7018c21STomi Valkeinen } 487f7018c21STomi Valkeinen 488f7018c21STomi Valkeinen /* Reply with screen and cursor info */ 489f7018c21STomi Valkeinen if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) { 490f7018c21STomi Valkeinen if (par->fb_ready) { 491f7018c21STomi Valkeinen synthvid_send_ptr(hdev); 492f7018c21STomi Valkeinen synthvid_send_situ(hdev); 493f7018c21STomi Valkeinen } 494f7018c21STomi Valkeinen 495f7018c21STomi Valkeinen par->update = msg->feature_chg.is_dirt_needed; 496f7018c21STomi Valkeinen if (par->update) 497f7018c21STomi Valkeinen schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY); 498f7018c21STomi Valkeinen } 499f7018c21STomi Valkeinen } 500f7018c21STomi Valkeinen 501f7018c21STomi Valkeinen /* Receive callback for messages from the host */ 502f7018c21STomi Valkeinen static void synthvid_receive(void *ctx) 503f7018c21STomi Valkeinen { 504f7018c21STomi Valkeinen struct hv_device *hdev = ctx; 505f7018c21STomi Valkeinen struct fb_info *info = hv_get_drvdata(hdev); 506f7018c21STomi Valkeinen struct hvfb_par *par; 507f7018c21STomi Valkeinen struct synthvid_msg *recv_buf; 508f7018c21STomi Valkeinen u32 bytes_recvd; 509f7018c21STomi Valkeinen u64 req_id; 510f7018c21STomi Valkeinen int ret; 511f7018c21STomi Valkeinen 512f7018c21STomi Valkeinen if (!info) 513f7018c21STomi Valkeinen return; 514f7018c21STomi Valkeinen 515f7018c21STomi Valkeinen par = info->par; 516f7018c21STomi Valkeinen recv_buf = (struct synthvid_msg *)par->recv_buf; 517f7018c21STomi Valkeinen 518f7018c21STomi Valkeinen do { 519f7018c21STomi Valkeinen ret = vmbus_recvpacket(hdev->channel, recv_buf, 520f7018c21STomi Valkeinen MAX_VMBUS_PKT_SIZE, 521f7018c21STomi Valkeinen &bytes_recvd, &req_id); 522f7018c21STomi Valkeinen if (bytes_recvd > 0 && 523f7018c21STomi Valkeinen recv_buf->pipe_hdr.type == PIPE_MSG_DATA) 524f7018c21STomi Valkeinen synthvid_recv_sub(hdev); 525f7018c21STomi Valkeinen } while (bytes_recvd > 0 && ret == 0); 526f7018c21STomi Valkeinen } 527f7018c21STomi Valkeinen 52867e7cdb4SWei Hu /* Check if the ver1 version is equal or greater than ver2 */ 52967e7cdb4SWei Hu static inline bool synthvid_ver_ge(u32 ver1, u32 ver2) 53067e7cdb4SWei Hu { 53167e7cdb4SWei Hu if (SYNTHVID_VER_GET_MAJOR(ver1) > SYNTHVID_VER_GET_MAJOR(ver2) || 53267e7cdb4SWei Hu (SYNTHVID_VER_GET_MAJOR(ver1) == SYNTHVID_VER_GET_MAJOR(ver2) && 53367e7cdb4SWei Hu SYNTHVID_VER_GET_MINOR(ver1) >= SYNTHVID_VER_GET_MINOR(ver2))) 53467e7cdb4SWei Hu return true; 53567e7cdb4SWei Hu 53667e7cdb4SWei Hu return false; 53767e7cdb4SWei Hu } 53867e7cdb4SWei Hu 539f7018c21STomi Valkeinen /* Check synthetic video protocol version with the host */ 540f7018c21STomi Valkeinen static int synthvid_negotiate_ver(struct hv_device *hdev, u32 ver) 541f7018c21STomi Valkeinen { 542f7018c21STomi Valkeinen struct fb_info *info = hv_get_drvdata(hdev); 543f7018c21STomi Valkeinen struct hvfb_par *par = info->par; 544f7018c21STomi Valkeinen struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf; 5457dea97e0SNicholas Mc Guire int ret = 0; 5467dea97e0SNicholas Mc Guire unsigned long t; 547f7018c21STomi Valkeinen 548f7018c21STomi Valkeinen memset(msg, 0, sizeof(struct synthvid_msg)); 549f7018c21STomi Valkeinen msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST; 550f7018c21STomi Valkeinen msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) + 551f7018c21STomi Valkeinen sizeof(struct synthvid_version_req); 552f7018c21STomi Valkeinen msg->ver_req.version = ver; 553f7018c21STomi Valkeinen synthvid_send(hdev, msg); 554f7018c21STomi Valkeinen 555f7018c21STomi Valkeinen t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT); 556f7018c21STomi Valkeinen if (!t) { 557f7018c21STomi Valkeinen pr_err("Time out on waiting version response\n"); 558f7018c21STomi Valkeinen ret = -ETIMEDOUT; 559f7018c21STomi Valkeinen goto out; 560f7018c21STomi Valkeinen } 561f7018c21STomi Valkeinen if (!msg->ver_resp.is_accepted) { 562f7018c21STomi Valkeinen ret = -ENODEV; 563f7018c21STomi Valkeinen goto out; 564f7018c21STomi Valkeinen } 565f7018c21STomi Valkeinen 566f7018c21STomi Valkeinen par->synthvid_version = ver; 56767e7cdb4SWei Hu pr_info("Synthvid Version major %d, minor %d\n", 56867e7cdb4SWei Hu SYNTHVID_VER_GET_MAJOR(ver), SYNTHVID_VER_GET_MINOR(ver)); 56967e7cdb4SWei Hu 57067e7cdb4SWei Hu out: 57167e7cdb4SWei Hu return ret; 57267e7cdb4SWei Hu } 57367e7cdb4SWei Hu 57467e7cdb4SWei Hu /* Get current resolution from the host */ 57567e7cdb4SWei Hu static int synthvid_get_supported_resolution(struct hv_device *hdev) 57667e7cdb4SWei Hu { 57767e7cdb4SWei Hu struct fb_info *info = hv_get_drvdata(hdev); 57867e7cdb4SWei Hu struct hvfb_par *par = info->par; 57967e7cdb4SWei Hu struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf; 58067e7cdb4SWei Hu int ret = 0; 58167e7cdb4SWei Hu unsigned long t; 58267e7cdb4SWei Hu u8 index; 58367e7cdb4SWei Hu 58467e7cdb4SWei Hu memset(msg, 0, sizeof(struct synthvid_msg)); 58567e7cdb4SWei Hu msg->vid_hdr.type = SYNTHVID_RESOLUTION_REQUEST; 58667e7cdb4SWei Hu msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) + 58767e7cdb4SWei Hu sizeof(struct synthvid_supported_resolution_req); 58867e7cdb4SWei Hu 58967e7cdb4SWei Hu msg->resolution_req.maximum_resolution_count = 59067e7cdb4SWei Hu SYNTHVID_MAX_RESOLUTION_COUNT; 59167e7cdb4SWei Hu synthvid_send(hdev, msg); 59267e7cdb4SWei Hu 59367e7cdb4SWei Hu t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT); 59467e7cdb4SWei Hu if (!t) { 59567e7cdb4SWei Hu pr_err("Time out on waiting resolution response\n"); 59667e7cdb4SWei Hu ret = -ETIMEDOUT; 59767e7cdb4SWei Hu goto out; 59867e7cdb4SWei Hu } 59967e7cdb4SWei Hu 60067e7cdb4SWei Hu if (msg->resolution_resp.resolution_count == 0) { 60167e7cdb4SWei Hu pr_err("No supported resolutions\n"); 60267e7cdb4SWei Hu ret = -ENODEV; 60367e7cdb4SWei Hu goto out; 60467e7cdb4SWei Hu } 60567e7cdb4SWei Hu 60667e7cdb4SWei Hu index = msg->resolution_resp.default_resolution_index; 60767e7cdb4SWei Hu if (index >= msg->resolution_resp.resolution_count) { 60867e7cdb4SWei Hu pr_err("Invalid resolution index: %d\n", index); 60967e7cdb4SWei Hu ret = -ENODEV; 61067e7cdb4SWei Hu goto out; 61167e7cdb4SWei Hu } 61267e7cdb4SWei Hu 61367e7cdb4SWei Hu screen_width = 61467e7cdb4SWei Hu msg->resolution_resp.supported_resolution[index].width; 61567e7cdb4SWei Hu screen_height = 61667e7cdb4SWei Hu msg->resolution_resp.supported_resolution[index].height; 617f7018c21STomi Valkeinen 618f7018c21STomi Valkeinen out: 619f7018c21STomi Valkeinen return ret; 620f7018c21STomi Valkeinen } 621f7018c21STomi Valkeinen 622f7018c21STomi Valkeinen /* Connect to VSP (Virtual Service Provider) on host */ 623f7018c21STomi Valkeinen static int synthvid_connect_vsp(struct hv_device *hdev) 624f7018c21STomi Valkeinen { 625f7018c21STomi Valkeinen struct fb_info *info = hv_get_drvdata(hdev); 626f7018c21STomi Valkeinen struct hvfb_par *par = info->par; 627f7018c21STomi Valkeinen int ret; 628f7018c21STomi Valkeinen 629f7018c21STomi Valkeinen ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE, 630f7018c21STomi Valkeinen NULL, 0, synthvid_receive, hdev); 631f7018c21STomi Valkeinen if (ret) { 632f7018c21STomi Valkeinen pr_err("Unable to open vmbus channel\n"); 633f7018c21STomi Valkeinen return ret; 634f7018c21STomi Valkeinen } 635f7018c21STomi Valkeinen 636f7018c21STomi Valkeinen /* Negotiate the protocol version with host */ 63767e7cdb4SWei Hu switch (vmbus_proto_version) { 63867e7cdb4SWei Hu case VERSION_WIN10: 63967e7cdb4SWei Hu case VERSION_WIN10_V5: 64067e7cdb4SWei Hu ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10); 64167e7cdb4SWei Hu if (!ret) 64267e7cdb4SWei Hu break; 643df561f66SGustavo A. R. Silva fallthrough; 64467e7cdb4SWei Hu case VERSION_WIN8: 64567e7cdb4SWei Hu case VERSION_WIN8_1: 646f7018c21STomi Valkeinen ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8); 64767e7cdb4SWei Hu if (!ret) 64867e7cdb4SWei Hu break; 649df561f66SGustavo A. R. Silva fallthrough; 65067e7cdb4SWei Hu case VERSION_WS2008: 65167e7cdb4SWei Hu case VERSION_WIN7: 65267e7cdb4SWei Hu ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN7); 65367e7cdb4SWei Hu break; 65467e7cdb4SWei Hu default: 65567e7cdb4SWei Hu ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10); 65667e7cdb4SWei Hu break; 65767e7cdb4SWei Hu } 658f7018c21STomi Valkeinen 659f7018c21STomi Valkeinen if (ret) { 660f7018c21STomi Valkeinen pr_err("Synthetic video device version not accepted\n"); 661f7018c21STomi Valkeinen goto error; 662f7018c21STomi Valkeinen } 663f7018c21STomi Valkeinen 664f7018c21STomi Valkeinen if (par->synthvid_version == SYNTHVID_VERSION_WIN7) 665f7018c21STomi Valkeinen screen_depth = SYNTHVID_DEPTH_WIN7; 666f7018c21STomi Valkeinen else 667f7018c21STomi Valkeinen screen_depth = SYNTHVID_DEPTH_WIN8; 668f7018c21STomi Valkeinen 66967e7cdb4SWei Hu if (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10)) { 67067e7cdb4SWei Hu ret = synthvid_get_supported_resolution(hdev); 67167e7cdb4SWei Hu if (ret) 67267e7cdb4SWei Hu pr_info("Failed to get supported resolution from host, use default\n"); 67367e7cdb4SWei Hu } 67467e7cdb4SWei Hu 675f7018c21STomi Valkeinen screen_fb_size = hdev->channel->offermsg.offer. 676f7018c21STomi Valkeinen mmio_megabytes * 1024 * 1024; 677f7018c21STomi Valkeinen 678f7018c21STomi Valkeinen return 0; 679f7018c21STomi Valkeinen 680f7018c21STomi Valkeinen error: 681f7018c21STomi Valkeinen vmbus_close(hdev->channel); 682f7018c21STomi Valkeinen return ret; 683f7018c21STomi Valkeinen } 684f7018c21STomi Valkeinen 685f7018c21STomi Valkeinen /* Send VRAM and Situation messages to the host */ 686f7018c21STomi Valkeinen static int synthvid_send_config(struct hv_device *hdev) 687f7018c21STomi Valkeinen { 688f7018c21STomi Valkeinen struct fb_info *info = hv_get_drvdata(hdev); 689f7018c21STomi Valkeinen struct hvfb_par *par = info->par; 690f7018c21STomi Valkeinen struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf; 6917dea97e0SNicholas Mc Guire int ret = 0; 6927dea97e0SNicholas Mc Guire unsigned long t; 693f7018c21STomi Valkeinen 694f7018c21STomi Valkeinen /* Send VRAM location */ 695f7018c21STomi Valkeinen memset(msg, 0, sizeof(struct synthvid_msg)); 696f7018c21STomi Valkeinen msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION; 697f7018c21STomi Valkeinen msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) + 698f7018c21STomi Valkeinen sizeof(struct synthvid_vram_location); 699d21987d7SWei Hu msg->vram.user_ctx = msg->vram.vram_gpa = par->mmio_pp; 700f7018c21STomi Valkeinen msg->vram.is_vram_gpa_specified = 1; 701f7018c21STomi Valkeinen synthvid_send(hdev, msg); 702f7018c21STomi Valkeinen 703f7018c21STomi Valkeinen t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT); 704f7018c21STomi Valkeinen if (!t) { 705f7018c21STomi Valkeinen pr_err("Time out on waiting vram location ack\n"); 706f7018c21STomi Valkeinen ret = -ETIMEDOUT; 707f7018c21STomi Valkeinen goto out; 708f7018c21STomi Valkeinen } 709d21987d7SWei Hu if (msg->vram_ack.user_ctx != par->mmio_pp) { 710f7018c21STomi Valkeinen pr_err("Unable to set VRAM location\n"); 711f7018c21STomi Valkeinen ret = -ENODEV; 712f7018c21STomi Valkeinen goto out; 713f7018c21STomi Valkeinen } 714f7018c21STomi Valkeinen 715f7018c21STomi Valkeinen /* Send pointer and situation update */ 716f7018c21STomi Valkeinen synthvid_send_ptr(hdev); 717f7018c21STomi Valkeinen synthvid_send_situ(hdev); 718f7018c21STomi Valkeinen 719f7018c21STomi Valkeinen out: 720f7018c21STomi Valkeinen return ret; 721f7018c21STomi Valkeinen } 722f7018c21STomi Valkeinen 723f7018c21STomi Valkeinen 724f7018c21STomi Valkeinen /* 725f7018c21STomi Valkeinen * Delayed work callback: 726d21987d7SWei Hu * It is scheduled to call whenever update request is received and it has 727d21987d7SWei Hu * not been called in last HVFB_ONDEMAND_THROTTLE time interval. 728f7018c21STomi Valkeinen */ 729f7018c21STomi Valkeinen static void hvfb_update_work(struct work_struct *w) 730f7018c21STomi Valkeinen { 731f7018c21STomi Valkeinen struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work); 732f7018c21STomi Valkeinen struct fb_info *info = par->info; 733d21987d7SWei Hu unsigned long flags; 734d21987d7SWei Hu int x1, x2, y1, y2; 735d21987d7SWei Hu int j; 736f7018c21STomi Valkeinen 737d21987d7SWei Hu spin_lock_irqsave(&par->delayed_refresh_lock, flags); 738d21987d7SWei Hu /* Reset the request flag */ 739d21987d7SWei Hu par->delayed_refresh = false; 740f7018c21STomi Valkeinen 741d21987d7SWei Hu /* Store the dirty rectangle to local variables */ 742d21987d7SWei Hu x1 = par->x1; 743d21987d7SWei Hu x2 = par->x2; 744d21987d7SWei Hu y1 = par->y1; 745d21987d7SWei Hu y2 = par->y2; 746d21987d7SWei Hu 747d21987d7SWei Hu /* Clear dirty rectangle */ 748d21987d7SWei Hu par->x1 = par->y1 = INT_MAX; 749d21987d7SWei Hu par->x2 = par->y2 = 0; 750d21987d7SWei Hu 751d21987d7SWei Hu spin_unlock_irqrestore(&par->delayed_refresh_lock, flags); 752d21987d7SWei Hu 753d21987d7SWei Hu if (x1 > info->var.xres || x2 > info->var.xres || 754d21987d7SWei Hu y1 > info->var.yres || y2 > info->var.yres || x2 <= x1) 755d21987d7SWei Hu return; 756d21987d7SWei Hu 757d21987d7SWei Hu /* Copy the dirty rectangle to frame buffer memory */ 7583a6fb6c4SWei Hu if (par->need_docopy) 7593a6fb6c4SWei Hu for (j = y1; j < y2; j++) 760d21987d7SWei Hu hvfb_docopy(par, 761d21987d7SWei Hu j * info->fix.line_length + 762d21987d7SWei Hu (x1 * screen_depth / 8), 763d21987d7SWei Hu (x2 - x1) * screen_depth / 8); 764d21987d7SWei Hu 765d21987d7SWei Hu /* Refresh */ 766d21987d7SWei Hu if (par->fb_ready && par->update) 767d21987d7SWei Hu synthvid_update(info, x1, y1, x2, y2); 768d21987d7SWei Hu } 769d21987d7SWei Hu 770d21987d7SWei Hu /* 771d21987d7SWei Hu * Control the on-demand refresh frequency. It schedules a delayed 772d21987d7SWei Hu * screen update if it has not yet. 773d21987d7SWei Hu */ 774d21987d7SWei Hu static void hvfb_ondemand_refresh_throttle(struct hvfb_par *par, 775d21987d7SWei Hu int x1, int y1, int w, int h) 776d21987d7SWei Hu { 777d21987d7SWei Hu unsigned long flags; 778d21987d7SWei Hu int x2 = x1 + w; 779d21987d7SWei Hu int y2 = y1 + h; 780d21987d7SWei Hu 781d21987d7SWei Hu spin_lock_irqsave(&par->delayed_refresh_lock, flags); 782d21987d7SWei Hu 783d21987d7SWei Hu /* Merge dirty rectangle */ 784d21987d7SWei Hu par->x1 = min_t(int, par->x1, x1); 785d21987d7SWei Hu par->y1 = min_t(int, par->y1, y1); 786d21987d7SWei Hu par->x2 = max_t(int, par->x2, x2); 787d21987d7SWei Hu par->y2 = max_t(int, par->y2, y2); 788d21987d7SWei Hu 789d21987d7SWei Hu /* Schedule a delayed screen update if not yet */ 790d21987d7SWei Hu if (par->delayed_refresh == false) { 791d21987d7SWei Hu schedule_delayed_work(&par->dwork, 792d21987d7SWei Hu HVFB_ONDEMAND_THROTTLE); 793d21987d7SWei Hu par->delayed_refresh = true; 794d21987d7SWei Hu } 795d21987d7SWei Hu 796d21987d7SWei Hu spin_unlock_irqrestore(&par->delayed_refresh_lock, flags); 797f7018c21STomi Valkeinen } 798f7018c21STomi Valkeinen 7993686fe96SDexuan Cui static int hvfb_on_panic(struct notifier_block *nb, 8003686fe96SDexuan Cui unsigned long e, void *p) 8013686fe96SDexuan Cui { 8023686fe96SDexuan Cui struct hvfb_par *par; 8033686fe96SDexuan Cui struct fb_info *info; 8043686fe96SDexuan Cui 8053686fe96SDexuan Cui par = container_of(nb, struct hvfb_par, hvfb_panic_nb); 8063686fe96SDexuan Cui par->synchronous_fb = true; 8073686fe96SDexuan Cui info = par->info; 8083a6fb6c4SWei Hu if (par->need_docopy) 809d21987d7SWei Hu hvfb_docopy(par, 0, dio_fb_size); 810d21987d7SWei Hu synthvid_update(info, 0, 0, INT_MAX, INT_MAX); 8113686fe96SDexuan Cui 8123686fe96SDexuan Cui return NOTIFY_DONE; 8133686fe96SDexuan Cui } 814f7018c21STomi Valkeinen 815f7018c21STomi Valkeinen /* Framebuffer operation handlers */ 816f7018c21STomi Valkeinen 817f7018c21STomi Valkeinen static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info) 818f7018c21STomi Valkeinen { 819f7018c21STomi Valkeinen if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN || 820f7018c21STomi Valkeinen var->xres > screen_width || var->yres > screen_height || 821f7018c21STomi Valkeinen var->bits_per_pixel != screen_depth) 822f7018c21STomi Valkeinen return -EINVAL; 823f7018c21STomi Valkeinen 824f7018c21STomi Valkeinen var->xres_virtual = var->xres; 825f7018c21STomi Valkeinen var->yres_virtual = var->yres; 826f7018c21STomi Valkeinen 827f7018c21STomi Valkeinen return 0; 828f7018c21STomi Valkeinen } 829f7018c21STomi Valkeinen 830f7018c21STomi Valkeinen static int hvfb_set_par(struct fb_info *info) 831f7018c21STomi Valkeinen { 832f7018c21STomi Valkeinen struct hv_device *hdev = device_to_hv_device(info->device); 833f7018c21STomi Valkeinen 834f7018c21STomi Valkeinen return synthvid_send_situ(hdev); 835f7018c21STomi Valkeinen } 836f7018c21STomi Valkeinen 837f7018c21STomi Valkeinen 838f7018c21STomi Valkeinen static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf) 839f7018c21STomi Valkeinen { 840f7018c21STomi Valkeinen return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset; 841f7018c21STomi Valkeinen } 842f7018c21STomi Valkeinen 843f7018c21STomi Valkeinen static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green, 844f7018c21STomi Valkeinen unsigned blue, unsigned transp, struct fb_info *info) 845f7018c21STomi Valkeinen { 846f7018c21STomi Valkeinen u32 *pal = info->pseudo_palette; 847f7018c21STomi Valkeinen 848f7018c21STomi Valkeinen if (regno > 15) 849f7018c21STomi Valkeinen return -EINVAL; 850f7018c21STomi Valkeinen 851f7018c21STomi Valkeinen pal[regno] = chan_to_field(red, &info->var.red) 852f7018c21STomi Valkeinen | chan_to_field(green, &info->var.green) 853f7018c21STomi Valkeinen | chan_to_field(blue, &info->var.blue) 854f7018c21STomi Valkeinen | chan_to_field(transp, &info->var.transp); 855f7018c21STomi Valkeinen 856f7018c21STomi Valkeinen return 0; 857f7018c21STomi Valkeinen } 858f7018c21STomi Valkeinen 859f7018c21STomi Valkeinen static int hvfb_blank(int blank, struct fb_info *info) 860f7018c21STomi Valkeinen { 861f7018c21STomi Valkeinen return 1; /* get fb_blank to set the colormap to all black */ 862f7018c21STomi Valkeinen } 863f7018c21STomi Valkeinen 8643686fe96SDexuan Cui static void hvfb_cfb_fillrect(struct fb_info *p, 8653686fe96SDexuan Cui const struct fb_fillrect *rect) 8663686fe96SDexuan Cui { 8673686fe96SDexuan Cui struct hvfb_par *par = p->par; 8683686fe96SDexuan Cui 8693686fe96SDexuan Cui cfb_fillrect(p, rect); 8703686fe96SDexuan Cui if (par->synchronous_fb) 871d21987d7SWei Hu synthvid_update(p, 0, 0, INT_MAX, INT_MAX); 872d21987d7SWei Hu else 873d21987d7SWei Hu hvfb_ondemand_refresh_throttle(par, rect->dx, rect->dy, 874d21987d7SWei Hu rect->width, rect->height); 8753686fe96SDexuan Cui } 8763686fe96SDexuan Cui 8773686fe96SDexuan Cui static void hvfb_cfb_copyarea(struct fb_info *p, 8783686fe96SDexuan Cui const struct fb_copyarea *area) 8793686fe96SDexuan Cui { 8803686fe96SDexuan Cui struct hvfb_par *par = p->par; 8813686fe96SDexuan Cui 8823686fe96SDexuan Cui cfb_copyarea(p, area); 8833686fe96SDexuan Cui if (par->synchronous_fb) 884d21987d7SWei Hu synthvid_update(p, 0, 0, INT_MAX, INT_MAX); 885d21987d7SWei Hu else 886d21987d7SWei Hu hvfb_ondemand_refresh_throttle(par, area->dx, area->dy, 887d21987d7SWei Hu area->width, area->height); 8883686fe96SDexuan Cui } 8893686fe96SDexuan Cui 8903686fe96SDexuan Cui static void hvfb_cfb_imageblit(struct fb_info *p, 8913686fe96SDexuan Cui const struct fb_image *image) 8923686fe96SDexuan Cui { 8933686fe96SDexuan Cui struct hvfb_par *par = p->par; 8943686fe96SDexuan Cui 8953686fe96SDexuan Cui cfb_imageblit(p, image); 8963686fe96SDexuan Cui if (par->synchronous_fb) 897d21987d7SWei Hu synthvid_update(p, 0, 0, INT_MAX, INT_MAX); 898d21987d7SWei Hu else 899d21987d7SWei Hu hvfb_ondemand_refresh_throttle(par, image->dx, image->dy, 900d21987d7SWei Hu image->width, image->height); 9013686fe96SDexuan Cui } 9023686fe96SDexuan Cui 9038a48ac33SJani Nikula static const struct fb_ops hvfb_ops = { 904f7018c21STomi Valkeinen .owner = THIS_MODULE, 905f7018c21STomi Valkeinen .fb_check_var = hvfb_check_var, 906f7018c21STomi Valkeinen .fb_set_par = hvfb_set_par, 907f7018c21STomi Valkeinen .fb_setcolreg = hvfb_setcolreg, 9083686fe96SDexuan Cui .fb_fillrect = hvfb_cfb_fillrect, 9093686fe96SDexuan Cui .fb_copyarea = hvfb_cfb_copyarea, 9103686fe96SDexuan Cui .fb_imageblit = hvfb_cfb_imageblit, 911f7018c21STomi Valkeinen .fb_blank = hvfb_blank, 912f7018c21STomi Valkeinen }; 913f7018c21STomi Valkeinen 914f7018c21STomi Valkeinen 915f7018c21STomi Valkeinen /* Get options from kernel paramenter "video=" */ 916f7018c21STomi Valkeinen static void hvfb_get_option(struct fb_info *info) 917f7018c21STomi Valkeinen { 918f7018c21STomi Valkeinen struct hvfb_par *par = info->par; 919f7018c21STomi Valkeinen char *opt = NULL, *p; 920f7018c21STomi Valkeinen uint x = 0, y = 0; 921f7018c21STomi Valkeinen 922f7018c21STomi Valkeinen if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt) 923f7018c21STomi Valkeinen return; 924f7018c21STomi Valkeinen 925f7018c21STomi Valkeinen p = strsep(&opt, "x"); 926f7018c21STomi Valkeinen if (!*p || kstrtouint(p, 0, &x) || 927f7018c21STomi Valkeinen !opt || !*opt || kstrtouint(opt, 0, &y)) { 928f7018c21STomi Valkeinen pr_err("Screen option is invalid: skipped\n"); 929f7018c21STomi Valkeinen return; 930f7018c21STomi Valkeinen } 931f7018c21STomi Valkeinen 932f7018c21STomi Valkeinen if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN || 93367e7cdb4SWei Hu (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10) && 934*9ff5549bSMichael Kelley (x * y * screen_depth / 8 > screen_fb_size)) || 935f7018c21STomi Valkeinen (par->synthvid_version == SYNTHVID_VERSION_WIN8 && 936f7018c21STomi Valkeinen x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8) || 937f7018c21STomi Valkeinen (par->synthvid_version == SYNTHVID_VERSION_WIN7 && 938f7018c21STomi Valkeinen (x > SYNTHVID_WIDTH_MAX_WIN7 || y > SYNTHVID_HEIGHT_MAX_WIN7))) { 939f7018c21STomi Valkeinen pr_err("Screen resolution option is out of range: skipped\n"); 940f7018c21STomi Valkeinen return; 941f7018c21STomi Valkeinen } 942f7018c21STomi Valkeinen 943f7018c21STomi Valkeinen screen_width = x; 944f7018c21STomi Valkeinen screen_height = y; 945f7018c21STomi Valkeinen return; 946f7018c21STomi Valkeinen } 947f7018c21STomi Valkeinen 9483a6fb6c4SWei Hu /* 9493a6fb6c4SWei Hu * Allocate enough contiguous physical memory. 9503a6fb6c4SWei Hu * Return physical address if succeeded or -1 if failed. 9513a6fb6c4SWei Hu */ 9523a6fb6c4SWei Hu static phys_addr_t hvfb_get_phymem(struct hv_device *hdev, 9533a6fb6c4SWei Hu unsigned int request_size) 9543a6fb6c4SWei Hu { 9553a6fb6c4SWei Hu struct page *page = NULL; 9563a6fb6c4SWei Hu dma_addr_t dma_handle; 9573a6fb6c4SWei Hu void *vmem; 9583a6fb6c4SWei Hu phys_addr_t paddr = 0; 9593a6fb6c4SWei Hu unsigned int order = get_order(request_size); 9603a6fb6c4SWei Hu 9613a6fb6c4SWei Hu if (request_size == 0) 9623a6fb6c4SWei Hu return -1; 9633a6fb6c4SWei Hu 9643a6fb6c4SWei Hu if (order < MAX_ORDER) { 9653a6fb6c4SWei Hu /* Call alloc_pages if the size is less than 2^MAX_ORDER */ 9663a6fb6c4SWei Hu page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order); 9673a6fb6c4SWei Hu if (!page) 9683a6fb6c4SWei Hu return -1; 9693a6fb6c4SWei Hu 9703a6fb6c4SWei Hu paddr = (page_to_pfn(page) << PAGE_SHIFT); 9713a6fb6c4SWei Hu } else { 9723a6fb6c4SWei Hu /* Allocate from CMA */ 9733a6fb6c4SWei Hu hdev->device.coherent_dma_mask = DMA_BIT_MASK(64); 9743a6fb6c4SWei Hu 9753a6fb6c4SWei Hu vmem = dma_alloc_coherent(&hdev->device, 9763a6fb6c4SWei Hu round_up(request_size, PAGE_SIZE), 9773a6fb6c4SWei Hu &dma_handle, 9783a6fb6c4SWei Hu GFP_KERNEL | __GFP_NOWARN); 9793a6fb6c4SWei Hu 9803a6fb6c4SWei Hu if (!vmem) 9813a6fb6c4SWei Hu return -1; 9823a6fb6c4SWei Hu 9833a6fb6c4SWei Hu paddr = virt_to_phys(vmem); 9843a6fb6c4SWei Hu } 9853a6fb6c4SWei Hu 9863a6fb6c4SWei Hu return paddr; 9873a6fb6c4SWei Hu } 9883a6fb6c4SWei Hu 9893a6fb6c4SWei Hu /* Release contiguous physical memory */ 9903a6fb6c4SWei Hu static void hvfb_release_phymem(struct hv_device *hdev, 9913a6fb6c4SWei Hu phys_addr_t paddr, unsigned int size) 9923a6fb6c4SWei Hu { 9933a6fb6c4SWei Hu unsigned int order = get_order(size); 9943a6fb6c4SWei Hu 9953a6fb6c4SWei Hu if (order < MAX_ORDER) 9963a6fb6c4SWei Hu __free_pages(pfn_to_page(paddr >> PAGE_SHIFT), order); 9973a6fb6c4SWei Hu else 9983a6fb6c4SWei Hu dma_free_coherent(&hdev->device, 9993a6fb6c4SWei Hu round_up(size, PAGE_SIZE), 10003a6fb6c4SWei Hu phys_to_virt(paddr), 10013a6fb6c4SWei Hu paddr); 10023a6fb6c4SWei Hu } 10033a6fb6c4SWei Hu 1004f7018c21STomi Valkeinen 1005f7018c21STomi Valkeinen /* Get framebuffer memory from Hyper-V video pci space */ 100635464483SJake Oshins static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info) 1007f7018c21STomi Valkeinen { 1008f7018c21STomi Valkeinen struct hvfb_par *par = info->par; 1009f7018c21STomi Valkeinen struct pci_dev *pdev = NULL; 1010f7018c21STomi Valkeinen void __iomem *fb_virt; 1011f7018c21STomi Valkeinen int gen2vm = efi_enabled(EFI_BOOT); 101235464483SJake Oshins resource_size_t pot_start, pot_end; 10133a6fb6c4SWei Hu phys_addr_t paddr; 1014f7018c21STomi Valkeinen int ret; 1015f7018c21STomi Valkeinen 10163a6fb6c4SWei Hu info->apertures = alloc_apertures(1); 10173a6fb6c4SWei Hu if (!info->apertures) 10183a6fb6c4SWei Hu return -ENOMEM; 10193a6fb6c4SWei Hu 10203a6fb6c4SWei Hu if (!gen2vm) { 10213a6fb6c4SWei Hu pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT, 10223a6fb6c4SWei Hu PCI_DEVICE_ID_HYPERV_VIDEO, NULL); 10233a6fb6c4SWei Hu if (!pdev) { 10243a6fb6c4SWei Hu pr_err("Unable to find PCI Hyper-V video\n"); 10253a6fb6c4SWei Hu return -ENODEV; 10263a6fb6c4SWei Hu } 10273a6fb6c4SWei Hu 10283a6fb6c4SWei Hu info->apertures->ranges[0].base = pci_resource_start(pdev, 0); 10293a6fb6c4SWei Hu info->apertures->ranges[0].size = pci_resource_len(pdev, 0); 10303a6fb6c4SWei Hu 10313a6fb6c4SWei Hu /* 10323a6fb6c4SWei Hu * For Gen 1 VM, we can directly use the contiguous memory 10333a6fb6c4SWei Hu * from VM. If we succeed, deferred IO happens directly 10343a6fb6c4SWei Hu * on this allocated framebuffer memory, avoiding extra 10353a6fb6c4SWei Hu * memory copy. 10363a6fb6c4SWei Hu */ 10373a6fb6c4SWei Hu paddr = hvfb_get_phymem(hdev, screen_fb_size); 10383a6fb6c4SWei Hu if (paddr != (phys_addr_t) -1) { 10393a6fb6c4SWei Hu par->mmio_pp = paddr; 10403a6fb6c4SWei Hu par->mmio_vp = par->dio_vp = __va(paddr); 10413a6fb6c4SWei Hu 10423a6fb6c4SWei Hu info->fix.smem_start = paddr; 10433a6fb6c4SWei Hu info->fix.smem_len = screen_fb_size; 10443a6fb6c4SWei Hu info->screen_base = par->mmio_vp; 10453a6fb6c4SWei Hu info->screen_size = screen_fb_size; 10463a6fb6c4SWei Hu 10473a6fb6c4SWei Hu par->need_docopy = false; 10483a6fb6c4SWei Hu goto getmem_done; 10493a6fb6c4SWei Hu } 10503a6fb6c4SWei Hu pr_info("Unable to allocate enough contiguous physical memory on Gen 1 VM. Using MMIO instead.\n"); 10513a6fb6c4SWei Hu } else { 10523a6fb6c4SWei Hu info->apertures->ranges[0].base = screen_info.lfb_base; 10533a6fb6c4SWei Hu info->apertures->ranges[0].size = screen_info.lfb_size; 10543a6fb6c4SWei Hu } 10553a6fb6c4SWei Hu 10563a6fb6c4SWei Hu /* 10573a6fb6c4SWei Hu * Cannot use the contiguous physical memory. 10583a6fb6c4SWei Hu * Allocate mmio space for framebuffer. 10593a6fb6c4SWei Hu */ 1060d21987d7SWei Hu dio_fb_size = 1061d21987d7SWei Hu screen_width * screen_height * screen_depth / 8; 1062d21987d7SWei Hu 1063f7018c21STomi Valkeinen if (gen2vm) { 106435464483SJake Oshins pot_start = 0; 106535464483SJake Oshins pot_end = -1; 1066f7018c21STomi Valkeinen } else { 1067f7018c21STomi Valkeinen if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) || 106867e7cdb4SWei Hu pci_resource_len(pdev, 0) < screen_fb_size) { 106967e7cdb4SWei Hu pr_err("Resource not available or (0x%lx < 0x%lx)\n", 107067e7cdb4SWei Hu (unsigned long) pci_resource_len(pdev, 0), 107167e7cdb4SWei Hu (unsigned long) screen_fb_size); 1072f7018c21STomi Valkeinen goto err1; 107367e7cdb4SWei Hu } 1074f7018c21STomi Valkeinen 107535464483SJake Oshins pot_end = pci_resource_end(pdev, 0); 107635464483SJake Oshins pot_start = pot_end - screen_fb_size + 1; 1077f7018c21STomi Valkeinen } 1078f7018c21STomi Valkeinen 107935464483SJake Oshins ret = vmbus_allocate_mmio(&par->mem, hdev, pot_start, pot_end, 108035464483SJake Oshins screen_fb_size, 0x100000, true); 108135464483SJake Oshins if (ret != 0) { 108235464483SJake Oshins pr_err("Unable to allocate framebuffer memory\n"); 108335464483SJake Oshins goto err1; 108435464483SJake Oshins } 108535464483SJake Oshins 10865f1251a4SDexuan Cui /* 10875f1251a4SDexuan Cui * Map the VRAM cacheable for performance. This is also required for 10885f1251a4SDexuan Cui * VM Connect to display properly for ARM64 Linux VM, as the host also 10895f1251a4SDexuan Cui * maps the VRAM cacheable. 10905f1251a4SDexuan Cui */ 10915f1251a4SDexuan Cui fb_virt = ioremap_cache(par->mem->start, screen_fb_size); 1092f7018c21STomi Valkeinen if (!fb_virt) 1093f7018c21STomi Valkeinen goto err2; 1094f7018c21STomi Valkeinen 1095d21987d7SWei Hu /* Allocate memory for deferred IO */ 1096d21987d7SWei Hu par->dio_vp = vzalloc(round_up(dio_fb_size, PAGE_SIZE)); 1097d21987d7SWei Hu if (par->dio_vp == NULL) 1098d21987d7SWei Hu goto err3; 1099d21987d7SWei Hu 1100d21987d7SWei Hu /* Physical address of FB device */ 1101d21987d7SWei Hu par->mmio_pp = par->mem->start; 1102d21987d7SWei Hu /* Virtual address of FB device */ 1103d21987d7SWei Hu par->mmio_vp = (unsigned char *) fb_virt; 1104d21987d7SWei Hu 110535464483SJake Oshins info->fix.smem_start = par->mem->start; 1106d21987d7SWei Hu info->fix.smem_len = dio_fb_size; 1107d21987d7SWei Hu info->screen_base = par->dio_vp; 1108d21987d7SWei Hu info->screen_size = dio_fb_size; 1109f7018c21STomi Valkeinen 11103a6fb6c4SWei Hu getmem_done: 11113a6fb6c4SWei Hu remove_conflicting_framebuffers(info->apertures, 11123a6fb6c4SWei Hu KBUILD_MODNAME, false); 11133cb73bc3SKairui Song 11143cb73bc3SKairui Song if (gen2vm) { 11153cb73bc3SKairui Song /* framebuffer is reallocated, clear screen_info to avoid misuse from kexec */ 11163cb73bc3SKairui Song screen_info.lfb_size = 0; 11173cb73bc3SKairui Song screen_info.lfb_base = 0; 11183cb73bc3SKairui Song screen_info.orig_video_isVGA = 0; 11193cb73bc3SKairui Song } else { 1120f7018c21STomi Valkeinen pci_dev_put(pdev); 11213cb73bc3SKairui Song } 1122f7018c21STomi Valkeinen 1123f7018c21STomi Valkeinen return 0; 1124f7018c21STomi Valkeinen 1125f7018c21STomi Valkeinen err3: 1126f7018c21STomi Valkeinen iounmap(fb_virt); 1127f7018c21STomi Valkeinen err2: 1128696ca5e8SJake Oshins vmbus_free_mmio(par->mem->start, screen_fb_size); 112935464483SJake Oshins par->mem = NULL; 1130f7018c21STomi Valkeinen err1: 1131f7018c21STomi Valkeinen if (!gen2vm) 1132f7018c21STomi Valkeinen pci_dev_put(pdev); 1133f7018c21STomi Valkeinen 1134f7018c21STomi Valkeinen return -ENOMEM; 1135f7018c21STomi Valkeinen } 1136f7018c21STomi Valkeinen 1137f7018c21STomi Valkeinen /* Release the framebuffer */ 11383a6fb6c4SWei Hu static void hvfb_putmem(struct hv_device *hdev, struct fb_info *info) 1139f7018c21STomi Valkeinen { 1140f7018c21STomi Valkeinen struct hvfb_par *par = info->par; 1141f7018c21STomi Valkeinen 11423a6fb6c4SWei Hu if (par->need_docopy) { 1143d21987d7SWei Hu vfree(par->dio_vp); 1144f7018c21STomi Valkeinen iounmap(info->screen_base); 1145696ca5e8SJake Oshins vmbus_free_mmio(par->mem->start, screen_fb_size); 11463a6fb6c4SWei Hu } else { 11473a6fb6c4SWei Hu hvfb_release_phymem(hdev, info->fix.smem_start, 11483a6fb6c4SWei Hu screen_fb_size); 11493a6fb6c4SWei Hu } 11503a6fb6c4SWei Hu 115135464483SJake Oshins par->mem = NULL; 1152f7018c21STomi Valkeinen } 1153f7018c21STomi Valkeinen 1154f7018c21STomi Valkeinen 1155f7018c21STomi Valkeinen static int hvfb_probe(struct hv_device *hdev, 1156f7018c21STomi Valkeinen const struct hv_vmbus_device_id *dev_id) 1157f7018c21STomi Valkeinen { 1158f7018c21STomi Valkeinen struct fb_info *info; 1159f7018c21STomi Valkeinen struct hvfb_par *par; 1160f7018c21STomi Valkeinen int ret; 1161f7018c21STomi Valkeinen 1162f7018c21STomi Valkeinen info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device); 11630adcdbcbSBartlomiej Zolnierkiewicz if (!info) 1164f7018c21STomi Valkeinen return -ENOMEM; 1165f7018c21STomi Valkeinen 1166f7018c21STomi Valkeinen par = info->par; 1167f7018c21STomi Valkeinen par->info = info; 1168f7018c21STomi Valkeinen par->fb_ready = false; 11693a6fb6c4SWei Hu par->need_docopy = true; 1170f7018c21STomi Valkeinen init_completion(&par->wait); 1171f7018c21STomi Valkeinen INIT_DELAYED_WORK(&par->dwork, hvfb_update_work); 1172f7018c21STomi Valkeinen 1173d21987d7SWei Hu par->delayed_refresh = false; 1174d21987d7SWei Hu spin_lock_init(&par->delayed_refresh_lock); 1175d21987d7SWei Hu par->x1 = par->y1 = INT_MAX; 1176d21987d7SWei Hu par->x2 = par->y2 = 0; 1177d21987d7SWei Hu 1178f7018c21STomi Valkeinen /* Connect to VSP */ 1179f7018c21STomi Valkeinen hv_set_drvdata(hdev, info); 1180f7018c21STomi Valkeinen ret = synthvid_connect_vsp(hdev); 1181f7018c21STomi Valkeinen if (ret) { 1182f7018c21STomi Valkeinen pr_err("Unable to connect to VSP\n"); 1183f7018c21STomi Valkeinen goto error1; 1184f7018c21STomi Valkeinen } 1185f7018c21STomi Valkeinen 118667e7cdb4SWei Hu hvfb_get_option(info); 1187*9ff5549bSMichael Kelley pr_info("Screen resolution: %dx%d, Color depth: %d, Frame buffer size: %d\n", 1188*9ff5549bSMichael Kelley screen_width, screen_height, screen_depth, screen_fb_size); 118967e7cdb4SWei Hu 119035464483SJake Oshins ret = hvfb_getmem(hdev, info); 1191f7018c21STomi Valkeinen if (ret) { 1192f7018c21STomi Valkeinen pr_err("No memory for framebuffer\n"); 1193f7018c21STomi Valkeinen goto error2; 1194f7018c21STomi Valkeinen } 1195f7018c21STomi Valkeinen 1196f7018c21STomi Valkeinen /* Set up fb_info */ 1197f7018c21STomi Valkeinen info->flags = FBINFO_DEFAULT; 1198f7018c21STomi Valkeinen 1199f7018c21STomi Valkeinen info->var.xres_virtual = info->var.xres = screen_width; 1200f7018c21STomi Valkeinen info->var.yres_virtual = info->var.yres = screen_height; 1201f7018c21STomi Valkeinen info->var.bits_per_pixel = screen_depth; 1202f7018c21STomi Valkeinen 1203f7018c21STomi Valkeinen if (info->var.bits_per_pixel == 16) { 1204f7018c21STomi Valkeinen info->var.red = (struct fb_bitfield){11, 5, 0}; 1205f7018c21STomi Valkeinen info->var.green = (struct fb_bitfield){5, 6, 0}; 1206f7018c21STomi Valkeinen info->var.blue = (struct fb_bitfield){0, 5, 0}; 1207f7018c21STomi Valkeinen info->var.transp = (struct fb_bitfield){0, 0, 0}; 1208f7018c21STomi Valkeinen } else { 1209f7018c21STomi Valkeinen info->var.red = (struct fb_bitfield){16, 8, 0}; 1210f7018c21STomi Valkeinen info->var.green = (struct fb_bitfield){8, 8, 0}; 1211f7018c21STomi Valkeinen info->var.blue = (struct fb_bitfield){0, 8, 0}; 1212f7018c21STomi Valkeinen info->var.transp = (struct fb_bitfield){24, 8, 0}; 1213f7018c21STomi Valkeinen } 1214f7018c21STomi Valkeinen 1215f7018c21STomi Valkeinen info->var.activate = FB_ACTIVATE_NOW; 1216f7018c21STomi Valkeinen info->var.height = -1; 1217f7018c21STomi Valkeinen info->var.width = -1; 1218f7018c21STomi Valkeinen info->var.vmode = FB_VMODE_NONINTERLACED; 1219f7018c21STomi Valkeinen 1220f7018c21STomi Valkeinen strcpy(info->fix.id, KBUILD_MODNAME); 1221f7018c21STomi Valkeinen info->fix.type = FB_TYPE_PACKED_PIXELS; 1222f7018c21STomi Valkeinen info->fix.visual = FB_VISUAL_TRUECOLOR; 1223f7018c21STomi Valkeinen info->fix.line_length = screen_width * screen_depth / 8; 1224f7018c21STomi Valkeinen info->fix.accel = FB_ACCEL_NONE; 1225f7018c21STomi Valkeinen 1226f7018c21STomi Valkeinen info->fbops = &hvfb_ops; 1227f7018c21STomi Valkeinen info->pseudo_palette = par->pseudo_palette; 1228f7018c21STomi Valkeinen 1229d21987d7SWei Hu /* Initialize deferred IO */ 1230d21987d7SWei Hu info->fbdefio = &synthvid_defio; 1231d21987d7SWei Hu fb_deferred_io_init(info); 1232d21987d7SWei Hu 1233f7018c21STomi Valkeinen /* Send config to host */ 1234f7018c21STomi Valkeinen ret = synthvid_send_config(hdev); 1235f7018c21STomi Valkeinen if (ret) 1236f7018c21STomi Valkeinen goto error; 1237f7018c21STomi Valkeinen 1238f7018c21STomi Valkeinen ret = register_framebuffer(info); 1239f7018c21STomi Valkeinen if (ret) { 1240f7018c21STomi Valkeinen pr_err("Unable to register framebuffer\n"); 1241f7018c21STomi Valkeinen goto error; 1242f7018c21STomi Valkeinen } 1243f7018c21STomi Valkeinen 1244f7018c21STomi Valkeinen par->fb_ready = true; 1245f7018c21STomi Valkeinen 12463686fe96SDexuan Cui par->synchronous_fb = false; 12473686fe96SDexuan Cui par->hvfb_panic_nb.notifier_call = hvfb_on_panic; 12483686fe96SDexuan Cui atomic_notifier_chain_register(&panic_notifier_list, 12493686fe96SDexuan Cui &par->hvfb_panic_nb); 12503686fe96SDexuan Cui 1251f7018c21STomi Valkeinen return 0; 1252f7018c21STomi Valkeinen 1253f7018c21STomi Valkeinen error: 1254d21987d7SWei Hu fb_deferred_io_cleanup(info); 12553a6fb6c4SWei Hu hvfb_putmem(hdev, info); 1256f7018c21STomi Valkeinen error2: 1257f7018c21STomi Valkeinen vmbus_close(hdev->channel); 1258f7018c21STomi Valkeinen error1: 1259f7018c21STomi Valkeinen cancel_delayed_work_sync(&par->dwork); 1260f7018c21STomi Valkeinen hv_set_drvdata(hdev, NULL); 1261f7018c21STomi Valkeinen framebuffer_release(info); 1262f7018c21STomi Valkeinen return ret; 1263f7018c21STomi Valkeinen } 1264f7018c21STomi Valkeinen 1265f7018c21STomi Valkeinen 1266f7018c21STomi Valkeinen static int hvfb_remove(struct hv_device *hdev) 1267f7018c21STomi Valkeinen { 1268f7018c21STomi Valkeinen struct fb_info *info = hv_get_drvdata(hdev); 1269f7018c21STomi Valkeinen struct hvfb_par *par = info->par; 1270f7018c21STomi Valkeinen 12713686fe96SDexuan Cui atomic_notifier_chain_unregister(&panic_notifier_list, 12723686fe96SDexuan Cui &par->hvfb_panic_nb); 12733686fe96SDexuan Cui 1274f7018c21STomi Valkeinen par->update = false; 1275f7018c21STomi Valkeinen par->fb_ready = false; 1276f7018c21STomi Valkeinen 1277d21987d7SWei Hu fb_deferred_io_cleanup(info); 1278d21987d7SWei Hu 1279f7018c21STomi Valkeinen unregister_framebuffer(info); 1280f7018c21STomi Valkeinen cancel_delayed_work_sync(&par->dwork); 1281f7018c21STomi Valkeinen 1282f7018c21STomi Valkeinen vmbus_close(hdev->channel); 1283f7018c21STomi Valkeinen hv_set_drvdata(hdev, NULL); 1284f7018c21STomi Valkeinen 12853a6fb6c4SWei Hu hvfb_putmem(hdev, info); 1286f7018c21STomi Valkeinen framebuffer_release(info); 1287f7018c21STomi Valkeinen 1288f7018c21STomi Valkeinen return 0; 1289f7018c21STomi Valkeinen } 1290f7018c21STomi Valkeinen 12911ecf3020SDexuan Cui static int hvfb_suspend(struct hv_device *hdev) 12921ecf3020SDexuan Cui { 12931ecf3020SDexuan Cui struct fb_info *info = hv_get_drvdata(hdev); 12941ecf3020SDexuan Cui struct hvfb_par *par = info->par; 12951ecf3020SDexuan Cui 12961ecf3020SDexuan Cui console_lock(); 12971ecf3020SDexuan Cui 12981ecf3020SDexuan Cui /* 1 means do suspend */ 12991ecf3020SDexuan Cui fb_set_suspend(info, 1); 13001ecf3020SDexuan Cui 13011ecf3020SDexuan Cui cancel_delayed_work_sync(&par->dwork); 1302382a4622SDexuan Cui cancel_delayed_work_sync(&info->deferred_work); 13031ecf3020SDexuan Cui 13041ecf3020SDexuan Cui par->update_saved = par->update; 13051ecf3020SDexuan Cui par->update = false; 13061ecf3020SDexuan Cui par->fb_ready = false; 13071ecf3020SDexuan Cui 13081ecf3020SDexuan Cui vmbus_close(hdev->channel); 13091ecf3020SDexuan Cui 13101ecf3020SDexuan Cui console_unlock(); 13111ecf3020SDexuan Cui 13121ecf3020SDexuan Cui return 0; 13131ecf3020SDexuan Cui } 13141ecf3020SDexuan Cui 13151ecf3020SDexuan Cui static int hvfb_resume(struct hv_device *hdev) 13161ecf3020SDexuan Cui { 13171ecf3020SDexuan Cui struct fb_info *info = hv_get_drvdata(hdev); 13181ecf3020SDexuan Cui struct hvfb_par *par = info->par; 13191ecf3020SDexuan Cui int ret; 13201ecf3020SDexuan Cui 13211ecf3020SDexuan Cui console_lock(); 13221ecf3020SDexuan Cui 13231ecf3020SDexuan Cui ret = synthvid_connect_vsp(hdev); 13241ecf3020SDexuan Cui if (ret != 0) 13251ecf3020SDexuan Cui goto out; 13261ecf3020SDexuan Cui 13271ecf3020SDexuan Cui ret = synthvid_send_config(hdev); 13281ecf3020SDexuan Cui if (ret != 0) { 13291ecf3020SDexuan Cui vmbus_close(hdev->channel); 13301ecf3020SDexuan Cui goto out; 13311ecf3020SDexuan Cui } 13321ecf3020SDexuan Cui 13331ecf3020SDexuan Cui par->fb_ready = true; 13341ecf3020SDexuan Cui par->update = par->update_saved; 13351ecf3020SDexuan Cui 1336382a4622SDexuan Cui schedule_delayed_work(&info->deferred_work, info->fbdefio->delay); 13371ecf3020SDexuan Cui schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY); 13381ecf3020SDexuan Cui 13391ecf3020SDexuan Cui /* 0 means do resume */ 13401ecf3020SDexuan Cui fb_set_suspend(info, 0); 13411ecf3020SDexuan Cui 13421ecf3020SDexuan Cui out: 13431ecf3020SDexuan Cui console_unlock(); 13441ecf3020SDexuan Cui 13451ecf3020SDexuan Cui return ret; 13461ecf3020SDexuan Cui } 13471ecf3020SDexuan Cui 1348f7018c21STomi Valkeinen 13499baa3c34SBenoit Taine static const struct pci_device_id pci_stub_id_table[] = { 1350f7018c21STomi Valkeinen { 1351f7018c21STomi Valkeinen .vendor = PCI_VENDOR_ID_MICROSOFT, 1352f7018c21STomi Valkeinen .device = PCI_DEVICE_ID_HYPERV_VIDEO, 1353f7018c21STomi Valkeinen }, 1354f7018c21STomi Valkeinen { /* end of list */ } 1355f7018c21STomi Valkeinen }; 1356f7018c21STomi Valkeinen 1357f7018c21STomi Valkeinen static const struct hv_vmbus_device_id id_table[] = { 1358f7018c21STomi Valkeinen /* Synthetic Video Device GUID */ 1359f7018c21STomi Valkeinen {HV_SYNTHVID_GUID}, 1360f7018c21STomi Valkeinen {} 1361f7018c21STomi Valkeinen }; 1362f7018c21STomi Valkeinen 1363f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(pci, pci_stub_id_table); 1364f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(vmbus, id_table); 1365f7018c21STomi Valkeinen 1366f7018c21STomi Valkeinen static struct hv_driver hvfb_drv = { 1367f7018c21STomi Valkeinen .name = KBUILD_MODNAME, 1368f7018c21STomi Valkeinen .id_table = id_table, 1369f7018c21STomi Valkeinen .probe = hvfb_probe, 1370f7018c21STomi Valkeinen .remove = hvfb_remove, 13711ecf3020SDexuan Cui .suspend = hvfb_suspend, 13721ecf3020SDexuan Cui .resume = hvfb_resume, 1373af0a5646SArjan van de Ven .driver = { 1374af0a5646SArjan van de Ven .probe_type = PROBE_PREFER_ASYNCHRONOUS, 1375af0a5646SArjan van de Ven }, 1376f7018c21STomi Valkeinen }; 1377f7018c21STomi Valkeinen 1378f7018c21STomi Valkeinen static int hvfb_pci_stub_probe(struct pci_dev *pdev, 1379f7018c21STomi Valkeinen const struct pci_device_id *ent) 1380f7018c21STomi Valkeinen { 1381f7018c21STomi Valkeinen return 0; 1382f7018c21STomi Valkeinen } 1383f7018c21STomi Valkeinen 1384f7018c21STomi Valkeinen static void hvfb_pci_stub_remove(struct pci_dev *pdev) 1385f7018c21STomi Valkeinen { 1386f7018c21STomi Valkeinen } 1387f7018c21STomi Valkeinen 1388f7018c21STomi Valkeinen static struct pci_driver hvfb_pci_stub_driver = { 1389f7018c21STomi Valkeinen .name = KBUILD_MODNAME, 1390f7018c21STomi Valkeinen .id_table = pci_stub_id_table, 1391f7018c21STomi Valkeinen .probe = hvfb_pci_stub_probe, 1392f7018c21STomi Valkeinen .remove = hvfb_pci_stub_remove, 1393af0a5646SArjan van de Ven .driver = { 1394af0a5646SArjan van de Ven .probe_type = PROBE_PREFER_ASYNCHRONOUS, 1395af0a5646SArjan van de Ven } 1396f7018c21STomi Valkeinen }; 1397f7018c21STomi Valkeinen 1398f7018c21STomi Valkeinen static int __init hvfb_drv_init(void) 1399f7018c21STomi Valkeinen { 1400f7018c21STomi Valkeinen int ret; 1401f7018c21STomi Valkeinen 1402f7018c21STomi Valkeinen ret = vmbus_driver_register(&hvfb_drv); 1403f7018c21STomi Valkeinen if (ret != 0) 1404f7018c21STomi Valkeinen return ret; 1405f7018c21STomi Valkeinen 1406f7018c21STomi Valkeinen ret = pci_register_driver(&hvfb_pci_stub_driver); 1407f7018c21STomi Valkeinen if (ret != 0) { 1408f7018c21STomi Valkeinen vmbus_driver_unregister(&hvfb_drv); 1409f7018c21STomi Valkeinen return ret; 1410f7018c21STomi Valkeinen } 1411f7018c21STomi Valkeinen 1412f7018c21STomi Valkeinen return 0; 1413f7018c21STomi Valkeinen } 1414f7018c21STomi Valkeinen 1415f7018c21STomi Valkeinen static void __exit hvfb_drv_exit(void) 1416f7018c21STomi Valkeinen { 1417f7018c21STomi Valkeinen pci_unregister_driver(&hvfb_pci_stub_driver); 1418f7018c21STomi Valkeinen vmbus_driver_unregister(&hvfb_drv); 1419f7018c21STomi Valkeinen } 1420f7018c21STomi Valkeinen 1421f7018c21STomi Valkeinen module_init(hvfb_drv_init); 1422f7018c21STomi Valkeinen module_exit(hvfb_drv_exit); 1423f7018c21STomi Valkeinen 1424f7018c21STomi Valkeinen MODULE_LICENSE("GPL"); 1425f7018c21STomi Valkeinen MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver"); 1426