Thomas Gleixner | 43aa313 | 2019-05-29 07:17:54 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012, Microsoft Corporation. |
| 4 | * |
| 5 | * Author: |
| 6 | * Haiyang Zhang <haiyangz@microsoft.com> |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Hyper-V Synthetic Video Frame Buffer Driver |
| 11 | * |
| 12 | * This is the driver for the Hyper-V Synthetic Video, which supports |
| 13 | * screen resolution up to Full HD 1920x1080 with 32 bit color on Windows |
| 14 | * Server 2012, and 1600x1200 with 16 bit color on Windows Server 2008 R2 |
| 15 | * or earlier. |
| 16 | * |
| 17 | * It also solves the double mouse cursor issue of the emulated video mode. |
| 18 | * |
| 19 | * The default screen resolution is 1152x864, which may be changed by a |
| 20 | * kernel parameter: |
| 21 | * video=hyperv_fb:<width>x<height> |
| 22 | * For example: video=hyperv_fb:1280x1024 |
| 23 | * |
| 24 | * Portrait orientation is also supported: |
| 25 | * For example: video=hyperv_fb:864x1152 |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 26 | * |
| 27 | * When a Windows 10 RS5+ host is used, the virtual machine screen |
| 28 | * resolution is obtained from the host. The "video=hyperv_fb" option is |
| 29 | * not needed, but still can be used to overwrite what the host specifies. |
| 30 | * The VM resolution on the host could be set by executing the powershell |
| 31 | * "set-vmvideo" command. For example |
| 32 | * set-vmvideo -vmname name -horizontalresolution:1920 \ |
| 33 | * -verticalresolution:1200 -resolutiontype single |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 34 | * |
| 35 | * Gen 1 VMs also support direct using VM's physical memory for framebuffer. |
| 36 | * It could improve the efficiency and performance for framebuffer and VM. |
| 37 | * This requires to allocate contiguous physical memory from Linux kernel's |
| 38 | * CMA memory allocator. To enable this, supply a kernel parameter to give |
| 39 | * enough memory space to CMA allocator for framebuffer. For example: |
| 40 | * cma=130m |
| 41 | * This gives 130MB memory to CMA allocator that can be allocated to |
| 42 | * framebuffer. For reference, 8K resolution (7680x4320) takes about |
| 43 | * 127MB memory. |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 44 | */ |
| 45 | |
| 46 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 47 | |
| 48 | #include <linux/module.h> |
| 49 | #include <linux/kernel.h> |
Olaf Hering | 34a2808 | 2020-11-06 19:39:41 +0100 | [diff] [blame] | 50 | #include <linux/vmalloc.h> |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 51 | #include <linux/init.h> |
| 52 | #include <linux/completion.h> |
| 53 | #include <linux/fb.h> |
| 54 | #include <linux/pci.h> |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 55 | #include <linux/efi.h> |
Dexuan Cui | 1ecf302 | 2019-09-11 23:34:10 +0000 | [diff] [blame] | 56 | #include <linux/console.h> |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 57 | |
| 58 | #include <linux/hyperv.h> |
| 59 | |
| 60 | |
| 61 | /* Hyper-V Synthetic Video Protocol definitions and structures */ |
| 62 | #define MAX_VMBUS_PKT_SIZE 0x4000 |
| 63 | |
| 64 | #define SYNTHVID_VERSION(major, minor) ((minor) << 16 | (major)) |
| 65 | #define SYNTHVID_VERSION_WIN7 SYNTHVID_VERSION(3, 0) |
| 66 | #define SYNTHVID_VERSION_WIN8 SYNTHVID_VERSION(3, 2) |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 67 | #define SYNTHVID_VERSION_WIN10 SYNTHVID_VERSION(3, 5) |
| 68 | |
| 69 | #define SYNTHVID_VER_GET_MAJOR(ver) (ver & 0x0000ffff) |
| 70 | #define SYNTHVID_VER_GET_MINOR(ver) ((ver & 0xffff0000) >> 16) |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 71 | |
| 72 | #define SYNTHVID_DEPTH_WIN7 16 |
| 73 | #define SYNTHVID_DEPTH_WIN8 32 |
| 74 | |
| 75 | #define SYNTHVID_FB_SIZE_WIN7 (4 * 1024 * 1024) |
| 76 | #define SYNTHVID_WIDTH_MAX_WIN7 1600 |
| 77 | #define SYNTHVID_HEIGHT_MAX_WIN7 1200 |
| 78 | |
| 79 | #define SYNTHVID_FB_SIZE_WIN8 (8 * 1024 * 1024) |
| 80 | |
| 81 | #define PCI_VENDOR_ID_MICROSOFT 0x1414 |
| 82 | #define PCI_DEVICE_ID_HYPERV_VIDEO 0x5353 |
| 83 | |
| 84 | |
| 85 | enum pipe_msg_type { |
| 86 | PIPE_MSG_INVALID, |
| 87 | PIPE_MSG_DATA, |
| 88 | PIPE_MSG_MAX |
| 89 | }; |
| 90 | |
| 91 | struct pipe_msg_hdr { |
| 92 | u32 type; |
| 93 | u32 size; /* size of message after this field */ |
| 94 | } __packed; |
| 95 | |
| 96 | |
| 97 | enum synthvid_msg_type { |
| 98 | SYNTHVID_ERROR = 0, |
| 99 | SYNTHVID_VERSION_REQUEST = 1, |
| 100 | SYNTHVID_VERSION_RESPONSE = 2, |
| 101 | SYNTHVID_VRAM_LOCATION = 3, |
| 102 | SYNTHVID_VRAM_LOCATION_ACK = 4, |
| 103 | SYNTHVID_SITUATION_UPDATE = 5, |
| 104 | SYNTHVID_SITUATION_UPDATE_ACK = 6, |
| 105 | SYNTHVID_POINTER_POSITION = 7, |
| 106 | SYNTHVID_POINTER_SHAPE = 8, |
| 107 | SYNTHVID_FEATURE_CHANGE = 9, |
| 108 | SYNTHVID_DIRT = 10, |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 109 | SYNTHVID_RESOLUTION_REQUEST = 13, |
| 110 | SYNTHVID_RESOLUTION_RESPONSE = 14, |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 111 | |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 112 | SYNTHVID_MAX = 15 |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 115 | #define SYNTHVID_EDID_BLOCK_SIZE 128 |
| 116 | #define SYNTHVID_MAX_RESOLUTION_COUNT 64 |
| 117 | |
| 118 | struct hvd_screen_info { |
| 119 | u16 width; |
| 120 | u16 height; |
| 121 | } __packed; |
| 122 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 123 | struct synthvid_msg_hdr { |
| 124 | u32 type; |
| 125 | u32 size; /* size of this header + payload after this field*/ |
| 126 | } __packed; |
| 127 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 128 | struct synthvid_version_req { |
| 129 | u32 version; |
| 130 | } __packed; |
| 131 | |
| 132 | struct synthvid_version_resp { |
| 133 | u32 version; |
| 134 | u8 is_accepted; |
| 135 | u8 max_video_outputs; |
| 136 | } __packed; |
| 137 | |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 138 | struct synthvid_supported_resolution_req { |
| 139 | u8 maximum_resolution_count; |
| 140 | } __packed; |
| 141 | |
| 142 | struct synthvid_supported_resolution_resp { |
| 143 | u8 edid_block[SYNTHVID_EDID_BLOCK_SIZE]; |
| 144 | u8 resolution_count; |
| 145 | u8 default_resolution_index; |
| 146 | u8 is_standard; |
| 147 | struct hvd_screen_info |
| 148 | supported_resolution[SYNTHVID_MAX_RESOLUTION_COUNT]; |
| 149 | } __packed; |
| 150 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 151 | struct synthvid_vram_location { |
| 152 | u64 user_ctx; |
| 153 | u8 is_vram_gpa_specified; |
| 154 | u64 vram_gpa; |
| 155 | } __packed; |
| 156 | |
| 157 | struct synthvid_vram_location_ack { |
| 158 | u64 user_ctx; |
| 159 | } __packed; |
| 160 | |
| 161 | struct video_output_situation { |
| 162 | u8 active; |
| 163 | u32 vram_offset; |
| 164 | u8 depth_bits; |
| 165 | u32 width_pixels; |
| 166 | u32 height_pixels; |
| 167 | u32 pitch_bytes; |
| 168 | } __packed; |
| 169 | |
| 170 | struct synthvid_situation_update { |
| 171 | u64 user_ctx; |
| 172 | u8 video_output_count; |
| 173 | struct video_output_situation video_output[1]; |
| 174 | } __packed; |
| 175 | |
| 176 | struct synthvid_situation_update_ack { |
| 177 | u64 user_ctx; |
| 178 | } __packed; |
| 179 | |
| 180 | struct synthvid_pointer_position { |
| 181 | u8 is_visible; |
| 182 | u8 video_output; |
| 183 | s32 image_x; |
| 184 | s32 image_y; |
| 185 | } __packed; |
| 186 | |
| 187 | |
| 188 | #define CURSOR_MAX_X 96 |
| 189 | #define CURSOR_MAX_Y 96 |
| 190 | #define CURSOR_ARGB_PIXEL_SIZE 4 |
| 191 | #define CURSOR_MAX_SIZE (CURSOR_MAX_X * CURSOR_MAX_Y * CURSOR_ARGB_PIXEL_SIZE) |
| 192 | #define CURSOR_COMPLETE (-1) |
| 193 | |
| 194 | struct synthvid_pointer_shape { |
| 195 | u8 part_idx; |
| 196 | u8 is_argb; |
| 197 | u32 width; /* CURSOR_MAX_X at most */ |
| 198 | u32 height; /* CURSOR_MAX_Y at most */ |
| 199 | u32 hot_x; /* hotspot relative to upper-left of pointer image */ |
| 200 | u32 hot_y; |
| 201 | u8 data[4]; |
| 202 | } __packed; |
| 203 | |
| 204 | struct synthvid_feature_change { |
| 205 | u8 is_dirt_needed; |
| 206 | u8 is_ptr_pos_needed; |
| 207 | u8 is_ptr_shape_needed; |
| 208 | u8 is_situ_needed; |
| 209 | } __packed; |
| 210 | |
| 211 | struct rect { |
| 212 | s32 x1, y1; /* top left corner */ |
| 213 | s32 x2, y2; /* bottom right corner, exclusive */ |
| 214 | } __packed; |
| 215 | |
| 216 | struct synthvid_dirt { |
| 217 | u8 video_output; |
| 218 | u8 dirt_count; |
| 219 | struct rect rect[1]; |
| 220 | } __packed; |
| 221 | |
| 222 | struct synthvid_msg { |
| 223 | struct pipe_msg_hdr pipe_hdr; |
| 224 | struct synthvid_msg_hdr vid_hdr; |
| 225 | union { |
| 226 | struct synthvid_version_req ver_req; |
| 227 | struct synthvid_version_resp ver_resp; |
| 228 | struct synthvid_vram_location vram; |
| 229 | struct synthvid_vram_location_ack vram_ack; |
| 230 | struct synthvid_situation_update situ; |
| 231 | struct synthvid_situation_update_ack situ_ack; |
| 232 | struct synthvid_pointer_position ptr_pos; |
| 233 | struct synthvid_pointer_shape ptr_shape; |
| 234 | struct synthvid_feature_change feature_chg; |
| 235 | struct synthvid_dirt dirt; |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 236 | struct synthvid_supported_resolution_req resolution_req; |
| 237 | struct synthvid_supported_resolution_resp resolution_resp; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 238 | }; |
| 239 | } __packed; |
| 240 | |
| 241 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 242 | /* FB driver definitions and structures */ |
| 243 | #define HVFB_WIDTH 1152 /* default screen width */ |
| 244 | #define HVFB_HEIGHT 864 /* default screen height */ |
| 245 | #define HVFB_WIDTH_MIN 640 |
| 246 | #define HVFB_HEIGHT_MIN 480 |
| 247 | |
| 248 | #define RING_BUFSIZE (256 * 1024) |
| 249 | #define VSP_TIMEOUT (10 * HZ) |
| 250 | #define HVFB_UPDATE_DELAY (HZ / 20) |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 251 | #define HVFB_ONDEMAND_THROTTLE (HZ / 20) |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 252 | |
| 253 | struct hvfb_par { |
| 254 | struct fb_info *info; |
Jake Oshins | 3546448 | 2015-08-05 00:52:37 -0700 | [diff] [blame] | 255 | struct resource *mem; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 256 | bool fb_ready; /* fb device is ready */ |
| 257 | struct completion wait; |
| 258 | u32 synthvid_version; |
| 259 | |
| 260 | struct delayed_work dwork; |
| 261 | bool update; |
Dexuan Cui | 1ecf302 | 2019-09-11 23:34:10 +0000 | [diff] [blame] | 262 | bool update_saved; /* The value of 'update' before hibernation */ |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 263 | |
| 264 | u32 pseudo_palette[16]; |
| 265 | u8 init_buf[MAX_VMBUS_PKT_SIZE]; |
| 266 | u8 recv_buf[MAX_VMBUS_PKT_SIZE]; |
Dexuan Cui | 3686fe9 | 2014-08-01 04:27:54 -0700 | [diff] [blame] | 267 | |
| 268 | /* If true, the VSC notifies the VSP on every framebuffer change */ |
| 269 | bool synchronous_fb; |
| 270 | |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 271 | /* If true, need to copy from deferred IO mem to framebuffer mem */ |
| 272 | bool need_docopy; |
| 273 | |
Dexuan Cui | 3686fe9 | 2014-08-01 04:27:54 -0700 | [diff] [blame] | 274 | struct notifier_block hvfb_panic_nb; |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 275 | |
| 276 | /* Memory for deferred IO and frame buffer itself */ |
| 277 | unsigned char *dio_vp; |
| 278 | unsigned char *mmio_vp; |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 279 | phys_addr_t mmio_pp; |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 280 | |
| 281 | /* Dirty rectangle, protected by delayed_refresh_lock */ |
| 282 | int x1, y1, x2, y2; |
| 283 | bool delayed_refresh; |
| 284 | spinlock_t delayed_refresh_lock; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | static uint screen_width = HVFB_WIDTH; |
| 288 | static uint screen_height = HVFB_HEIGHT; |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 289 | static uint screen_width_max = HVFB_WIDTH; |
| 290 | static uint screen_height_max = HVFB_HEIGHT; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 291 | static uint screen_depth; |
| 292 | static uint screen_fb_size; |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 293 | static uint dio_fb_size; /* FB size for deferred IO */ |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 294 | |
| 295 | /* Send message to Hyper-V host */ |
| 296 | static inline int synthvid_send(struct hv_device *hdev, |
| 297 | struct synthvid_msg *msg) |
| 298 | { |
| 299 | static atomic64_t request_id = ATOMIC64_INIT(0); |
| 300 | int ret; |
| 301 | |
| 302 | msg->pipe_hdr.type = PIPE_MSG_DATA; |
| 303 | msg->pipe_hdr.size = msg->vid_hdr.size; |
| 304 | |
| 305 | ret = vmbus_sendpacket(hdev->channel, msg, |
| 306 | msg->vid_hdr.size + sizeof(struct pipe_msg_hdr), |
| 307 | atomic64_inc_return(&request_id), |
| 308 | VM_PKT_DATA_INBAND, 0); |
| 309 | |
| 310 | if (ret) |
| 311 | pr_err("Unable to send packet via vmbus\n"); |
| 312 | |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | /* Send screen resolution info to host */ |
| 318 | static int synthvid_send_situ(struct hv_device *hdev) |
| 319 | { |
| 320 | struct fb_info *info = hv_get_drvdata(hdev); |
| 321 | struct synthvid_msg msg; |
| 322 | |
| 323 | if (!info) |
| 324 | return -ENODEV; |
| 325 | |
| 326 | memset(&msg, 0, sizeof(struct synthvid_msg)); |
| 327 | |
| 328 | msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE; |
| 329 | msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) + |
| 330 | sizeof(struct synthvid_situation_update); |
| 331 | msg.situ.user_ctx = 0; |
| 332 | msg.situ.video_output_count = 1; |
| 333 | msg.situ.video_output[0].active = 1; |
| 334 | msg.situ.video_output[0].vram_offset = 0; |
| 335 | msg.situ.video_output[0].depth_bits = info->var.bits_per_pixel; |
| 336 | msg.situ.video_output[0].width_pixels = info->var.xres; |
| 337 | msg.situ.video_output[0].height_pixels = info->var.yres; |
| 338 | msg.situ.video_output[0].pitch_bytes = info->fix.line_length; |
| 339 | |
| 340 | synthvid_send(hdev, &msg); |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | /* Send mouse pointer info to host */ |
| 346 | static int synthvid_send_ptr(struct hv_device *hdev) |
| 347 | { |
| 348 | struct synthvid_msg msg; |
| 349 | |
| 350 | memset(&msg, 0, sizeof(struct synthvid_msg)); |
| 351 | msg.vid_hdr.type = SYNTHVID_POINTER_POSITION; |
| 352 | msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) + |
| 353 | sizeof(struct synthvid_pointer_position); |
| 354 | msg.ptr_pos.is_visible = 1; |
| 355 | msg.ptr_pos.video_output = 0; |
| 356 | msg.ptr_pos.image_x = 0; |
| 357 | msg.ptr_pos.image_y = 0; |
| 358 | synthvid_send(hdev, &msg); |
| 359 | |
| 360 | memset(&msg, 0, sizeof(struct synthvid_msg)); |
| 361 | msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE; |
| 362 | msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) + |
| 363 | sizeof(struct synthvid_pointer_shape); |
| 364 | msg.ptr_shape.part_idx = CURSOR_COMPLETE; |
| 365 | msg.ptr_shape.is_argb = 1; |
| 366 | msg.ptr_shape.width = 1; |
| 367 | msg.ptr_shape.height = 1; |
| 368 | msg.ptr_shape.hot_x = 0; |
| 369 | msg.ptr_shape.hot_y = 0; |
| 370 | msg.ptr_shape.data[0] = 0; |
| 371 | msg.ptr_shape.data[1] = 1; |
| 372 | msg.ptr_shape.data[2] = 1; |
| 373 | msg.ptr_shape.data[3] = 1; |
| 374 | synthvid_send(hdev, &msg); |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | /* Send updated screen area (dirty rectangle) location to host */ |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 380 | static int |
| 381 | synthvid_update(struct fb_info *info, int x1, int y1, int x2, int y2) |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 382 | { |
| 383 | struct hv_device *hdev = device_to_hv_device(info->device); |
| 384 | struct synthvid_msg msg; |
| 385 | |
| 386 | memset(&msg, 0, sizeof(struct synthvid_msg)); |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 387 | if (x2 == INT_MAX) |
| 388 | x2 = info->var.xres; |
| 389 | if (y2 == INT_MAX) |
| 390 | y2 = info->var.yres; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 391 | |
| 392 | msg.vid_hdr.type = SYNTHVID_DIRT; |
| 393 | msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) + |
| 394 | sizeof(struct synthvid_dirt); |
| 395 | msg.dirt.video_output = 0; |
| 396 | msg.dirt.dirt_count = 1; |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 397 | msg.dirt.rect[0].x1 = (x1 > x2) ? 0 : x1; |
| 398 | msg.dirt.rect[0].y1 = (y1 > y2) ? 0 : y1; |
| 399 | msg.dirt.rect[0].x2 = |
| 400 | (x2 < x1 || x2 > info->var.xres) ? info->var.xres : x2; |
| 401 | msg.dirt.rect[0].y2 = |
| 402 | (y2 < y1 || y2 > info->var.yres) ? info->var.yres : y2; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 403 | |
| 404 | synthvid_send(hdev, &msg); |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 409 | static void hvfb_docopy(struct hvfb_par *par, |
| 410 | unsigned long offset, |
| 411 | unsigned long size) |
| 412 | { |
| 413 | if (!par || !par->mmio_vp || !par->dio_vp || !par->fb_ready || |
| 414 | size == 0 || offset >= dio_fb_size) |
| 415 | return; |
| 416 | |
| 417 | if (offset + size > dio_fb_size) |
| 418 | size = dio_fb_size - offset; |
| 419 | |
| 420 | memcpy(par->mmio_vp + offset, par->dio_vp + offset, size); |
| 421 | } |
| 422 | |
| 423 | /* Deferred IO callback */ |
| 424 | static void synthvid_deferred_io(struct fb_info *p, |
| 425 | struct list_head *pagelist) |
| 426 | { |
| 427 | struct hvfb_par *par = p->par; |
| 428 | struct page *page; |
| 429 | unsigned long start, end; |
| 430 | int y1, y2, miny, maxy; |
| 431 | |
| 432 | miny = INT_MAX; |
| 433 | maxy = 0; |
| 434 | |
| 435 | /* |
| 436 | * Merge dirty pages. It is possible that last page cross |
| 437 | * over the end of frame buffer row yres. This is taken care of |
| 438 | * in synthvid_update function by clamping the y2 |
| 439 | * value to yres. |
| 440 | */ |
| 441 | list_for_each_entry(page, pagelist, lru) { |
| 442 | start = page->index << PAGE_SHIFT; |
| 443 | end = start + PAGE_SIZE - 1; |
| 444 | y1 = start / p->fix.line_length; |
| 445 | y2 = end / p->fix.line_length; |
| 446 | miny = min_t(int, miny, y1); |
| 447 | maxy = max_t(int, maxy, y2); |
| 448 | |
| 449 | /* Copy from dio space to mmio address */ |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 450 | if (par->fb_ready && par->need_docopy) |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 451 | hvfb_docopy(par, start, PAGE_SIZE); |
| 452 | } |
| 453 | |
| 454 | if (par->fb_ready && par->update) |
| 455 | synthvid_update(p, 0, miny, p->var.xres, maxy + 1); |
| 456 | } |
| 457 | |
| 458 | static struct fb_deferred_io synthvid_defio = { |
| 459 | .delay = HZ / 20, |
| 460 | .deferred_io = synthvid_deferred_io, |
| 461 | }; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 462 | |
| 463 | /* |
| 464 | * Actions on received messages from host: |
| 465 | * Complete the wait event. |
| 466 | * Or, reply with screen and cursor info. |
| 467 | */ |
| 468 | static void synthvid_recv_sub(struct hv_device *hdev) |
| 469 | { |
| 470 | struct fb_info *info = hv_get_drvdata(hdev); |
| 471 | struct hvfb_par *par; |
| 472 | struct synthvid_msg *msg; |
| 473 | |
| 474 | if (!info) |
| 475 | return; |
| 476 | |
| 477 | par = info->par; |
| 478 | msg = (struct synthvid_msg *)par->recv_buf; |
| 479 | |
| 480 | /* Complete the wait event */ |
| 481 | if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE || |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 482 | msg->vid_hdr.type == SYNTHVID_RESOLUTION_RESPONSE || |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 483 | msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) { |
| 484 | memcpy(par->init_buf, msg, MAX_VMBUS_PKT_SIZE); |
| 485 | complete(&par->wait); |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | /* Reply with screen and cursor info */ |
| 490 | if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) { |
| 491 | if (par->fb_ready) { |
| 492 | synthvid_send_ptr(hdev); |
| 493 | synthvid_send_situ(hdev); |
| 494 | } |
| 495 | |
| 496 | par->update = msg->feature_chg.is_dirt_needed; |
| 497 | if (par->update) |
| 498 | schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /* Receive callback for messages from the host */ |
| 503 | static void synthvid_receive(void *ctx) |
| 504 | { |
| 505 | struct hv_device *hdev = ctx; |
| 506 | struct fb_info *info = hv_get_drvdata(hdev); |
| 507 | struct hvfb_par *par; |
| 508 | struct synthvid_msg *recv_buf; |
| 509 | u32 bytes_recvd; |
| 510 | u64 req_id; |
| 511 | int ret; |
| 512 | |
| 513 | if (!info) |
| 514 | return; |
| 515 | |
| 516 | par = info->par; |
| 517 | recv_buf = (struct synthvid_msg *)par->recv_buf; |
| 518 | |
| 519 | do { |
| 520 | ret = vmbus_recvpacket(hdev->channel, recv_buf, |
| 521 | MAX_VMBUS_PKT_SIZE, |
| 522 | &bytes_recvd, &req_id); |
| 523 | if (bytes_recvd > 0 && |
| 524 | recv_buf->pipe_hdr.type == PIPE_MSG_DATA) |
| 525 | synthvid_recv_sub(hdev); |
| 526 | } while (bytes_recvd > 0 && ret == 0); |
| 527 | } |
| 528 | |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 529 | /* Check if the ver1 version is equal or greater than ver2 */ |
| 530 | static inline bool synthvid_ver_ge(u32 ver1, u32 ver2) |
| 531 | { |
| 532 | if (SYNTHVID_VER_GET_MAJOR(ver1) > SYNTHVID_VER_GET_MAJOR(ver2) || |
| 533 | (SYNTHVID_VER_GET_MAJOR(ver1) == SYNTHVID_VER_GET_MAJOR(ver2) && |
| 534 | SYNTHVID_VER_GET_MINOR(ver1) >= SYNTHVID_VER_GET_MINOR(ver2))) |
| 535 | return true; |
| 536 | |
| 537 | return false; |
| 538 | } |
| 539 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 540 | /* Check synthetic video protocol version with the host */ |
| 541 | static int synthvid_negotiate_ver(struct hv_device *hdev, u32 ver) |
| 542 | { |
| 543 | struct fb_info *info = hv_get_drvdata(hdev); |
| 544 | struct hvfb_par *par = info->par; |
| 545 | struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf; |
Nicholas Mc Guire | 7dea97e | 2015-01-29 11:24:16 +0100 | [diff] [blame] | 546 | int ret = 0; |
| 547 | unsigned long t; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 548 | |
| 549 | memset(msg, 0, sizeof(struct synthvid_msg)); |
| 550 | msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST; |
| 551 | msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) + |
| 552 | sizeof(struct synthvid_version_req); |
| 553 | msg->ver_req.version = ver; |
| 554 | synthvid_send(hdev, msg); |
| 555 | |
| 556 | t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT); |
| 557 | if (!t) { |
| 558 | pr_err("Time out on waiting version response\n"); |
| 559 | ret = -ETIMEDOUT; |
| 560 | goto out; |
| 561 | } |
| 562 | if (!msg->ver_resp.is_accepted) { |
| 563 | ret = -ENODEV; |
| 564 | goto out; |
| 565 | } |
| 566 | |
| 567 | par->synthvid_version = ver; |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 568 | pr_info("Synthvid Version major %d, minor %d\n", |
| 569 | SYNTHVID_VER_GET_MAJOR(ver), SYNTHVID_VER_GET_MINOR(ver)); |
| 570 | |
| 571 | out: |
| 572 | return ret; |
| 573 | } |
| 574 | |
| 575 | /* Get current resolution from the host */ |
| 576 | static int synthvid_get_supported_resolution(struct hv_device *hdev) |
| 577 | { |
| 578 | struct fb_info *info = hv_get_drvdata(hdev); |
| 579 | struct hvfb_par *par = info->par; |
| 580 | struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf; |
| 581 | int ret = 0; |
| 582 | unsigned long t; |
| 583 | u8 index; |
| 584 | int i; |
| 585 | |
| 586 | memset(msg, 0, sizeof(struct synthvid_msg)); |
| 587 | msg->vid_hdr.type = SYNTHVID_RESOLUTION_REQUEST; |
| 588 | msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) + |
| 589 | sizeof(struct synthvid_supported_resolution_req); |
| 590 | |
| 591 | msg->resolution_req.maximum_resolution_count = |
| 592 | SYNTHVID_MAX_RESOLUTION_COUNT; |
| 593 | synthvid_send(hdev, msg); |
| 594 | |
| 595 | t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT); |
| 596 | if (!t) { |
| 597 | pr_err("Time out on waiting resolution response\n"); |
Colin Ian King | 366c5aa | 2019-11-14 17:27:20 +0000 | [diff] [blame] | 598 | ret = -ETIMEDOUT; |
| 599 | goto out; |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | if (msg->resolution_resp.resolution_count == 0) { |
| 603 | pr_err("No supported resolutions\n"); |
| 604 | ret = -ENODEV; |
| 605 | goto out; |
| 606 | } |
| 607 | |
| 608 | index = msg->resolution_resp.default_resolution_index; |
| 609 | if (index >= msg->resolution_resp.resolution_count) { |
| 610 | pr_err("Invalid resolution index: %d\n", index); |
| 611 | ret = -ENODEV; |
| 612 | goto out; |
| 613 | } |
| 614 | |
| 615 | for (i = 0; i < msg->resolution_resp.resolution_count; i++) { |
| 616 | screen_width_max = max_t(unsigned int, screen_width_max, |
| 617 | msg->resolution_resp.supported_resolution[i].width); |
| 618 | screen_height_max = max_t(unsigned int, screen_height_max, |
| 619 | msg->resolution_resp.supported_resolution[i].height); |
| 620 | } |
| 621 | |
| 622 | screen_width = |
| 623 | msg->resolution_resp.supported_resolution[index].width; |
| 624 | screen_height = |
| 625 | msg->resolution_resp.supported_resolution[index].height; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 626 | |
| 627 | out: |
| 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | /* Connect to VSP (Virtual Service Provider) on host */ |
| 632 | static int synthvid_connect_vsp(struct hv_device *hdev) |
| 633 | { |
| 634 | struct fb_info *info = hv_get_drvdata(hdev); |
| 635 | struct hvfb_par *par = info->par; |
| 636 | int ret; |
| 637 | |
| 638 | ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE, |
| 639 | NULL, 0, synthvid_receive, hdev); |
| 640 | if (ret) { |
| 641 | pr_err("Unable to open vmbus channel\n"); |
| 642 | return ret; |
| 643 | } |
| 644 | |
| 645 | /* Negotiate the protocol version with host */ |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 646 | switch (vmbus_proto_version) { |
| 647 | case VERSION_WIN10: |
| 648 | case VERSION_WIN10_V5: |
| 649 | ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10); |
| 650 | if (!ret) |
| 651 | break; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 652 | fallthrough; |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 653 | case VERSION_WIN8: |
| 654 | case VERSION_WIN8_1: |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 655 | ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8); |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 656 | if (!ret) |
| 657 | break; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 658 | fallthrough; |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 659 | case VERSION_WS2008: |
| 660 | case VERSION_WIN7: |
| 661 | ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN7); |
| 662 | break; |
| 663 | default: |
| 664 | ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10); |
| 665 | break; |
| 666 | } |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 667 | |
| 668 | if (ret) { |
| 669 | pr_err("Synthetic video device version not accepted\n"); |
| 670 | goto error; |
| 671 | } |
| 672 | |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 673 | if (par->synthvid_version == SYNTHVID_VERSION_WIN7) |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 674 | screen_depth = SYNTHVID_DEPTH_WIN7; |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 675 | else |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 676 | screen_depth = SYNTHVID_DEPTH_WIN8; |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 677 | |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 678 | if (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10)) { |
| 679 | ret = synthvid_get_supported_resolution(hdev); |
| 680 | if (ret) |
| 681 | pr_info("Failed to get supported resolution from host, use default\n"); |
| 682 | } |
| 683 | |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 684 | screen_fb_size = hdev->channel->offermsg.offer. |
| 685 | mmio_megabytes * 1024 * 1024; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 686 | |
| 687 | return 0; |
| 688 | |
| 689 | error: |
| 690 | vmbus_close(hdev->channel); |
| 691 | return ret; |
| 692 | } |
| 693 | |
| 694 | /* Send VRAM and Situation messages to the host */ |
| 695 | static int synthvid_send_config(struct hv_device *hdev) |
| 696 | { |
| 697 | struct fb_info *info = hv_get_drvdata(hdev); |
| 698 | struct hvfb_par *par = info->par; |
| 699 | struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf; |
Nicholas Mc Guire | 7dea97e | 2015-01-29 11:24:16 +0100 | [diff] [blame] | 700 | int ret = 0; |
| 701 | unsigned long t; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 702 | |
| 703 | /* Send VRAM location */ |
| 704 | memset(msg, 0, sizeof(struct synthvid_msg)); |
| 705 | msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION; |
| 706 | msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) + |
| 707 | sizeof(struct synthvid_vram_location); |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 708 | msg->vram.user_ctx = msg->vram.vram_gpa = par->mmio_pp; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 709 | msg->vram.is_vram_gpa_specified = 1; |
| 710 | synthvid_send(hdev, msg); |
| 711 | |
| 712 | t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT); |
| 713 | if (!t) { |
| 714 | pr_err("Time out on waiting vram location ack\n"); |
| 715 | ret = -ETIMEDOUT; |
| 716 | goto out; |
| 717 | } |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 718 | if (msg->vram_ack.user_ctx != par->mmio_pp) { |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 719 | pr_err("Unable to set VRAM location\n"); |
| 720 | ret = -ENODEV; |
| 721 | goto out; |
| 722 | } |
| 723 | |
| 724 | /* Send pointer and situation update */ |
| 725 | synthvid_send_ptr(hdev); |
| 726 | synthvid_send_situ(hdev); |
| 727 | |
| 728 | out: |
| 729 | return ret; |
| 730 | } |
| 731 | |
| 732 | |
| 733 | /* |
| 734 | * Delayed work callback: |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 735 | * It is scheduled to call whenever update request is received and it has |
| 736 | * not been called in last HVFB_ONDEMAND_THROTTLE time interval. |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 737 | */ |
| 738 | static void hvfb_update_work(struct work_struct *w) |
| 739 | { |
| 740 | struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work); |
| 741 | struct fb_info *info = par->info; |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 742 | unsigned long flags; |
| 743 | int x1, x2, y1, y2; |
| 744 | int j; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 745 | |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 746 | spin_lock_irqsave(&par->delayed_refresh_lock, flags); |
| 747 | /* Reset the request flag */ |
| 748 | par->delayed_refresh = false; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 749 | |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 750 | /* Store the dirty rectangle to local variables */ |
| 751 | x1 = par->x1; |
| 752 | x2 = par->x2; |
| 753 | y1 = par->y1; |
| 754 | y2 = par->y2; |
| 755 | |
| 756 | /* Clear dirty rectangle */ |
| 757 | par->x1 = par->y1 = INT_MAX; |
| 758 | par->x2 = par->y2 = 0; |
| 759 | |
| 760 | spin_unlock_irqrestore(&par->delayed_refresh_lock, flags); |
| 761 | |
| 762 | if (x1 > info->var.xres || x2 > info->var.xres || |
| 763 | y1 > info->var.yres || y2 > info->var.yres || x2 <= x1) |
| 764 | return; |
| 765 | |
| 766 | /* Copy the dirty rectangle to frame buffer memory */ |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 767 | if (par->need_docopy) |
| 768 | for (j = y1; j < y2; j++) |
| 769 | hvfb_docopy(par, |
| 770 | j * info->fix.line_length + |
| 771 | (x1 * screen_depth / 8), |
| 772 | (x2 - x1) * screen_depth / 8); |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 773 | |
| 774 | /* Refresh */ |
| 775 | if (par->fb_ready && par->update) |
| 776 | synthvid_update(info, x1, y1, x2, y2); |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * Control the on-demand refresh frequency. It schedules a delayed |
| 781 | * screen update if it has not yet. |
| 782 | */ |
| 783 | static void hvfb_ondemand_refresh_throttle(struct hvfb_par *par, |
| 784 | int x1, int y1, int w, int h) |
| 785 | { |
| 786 | unsigned long flags; |
| 787 | int x2 = x1 + w; |
| 788 | int y2 = y1 + h; |
| 789 | |
| 790 | spin_lock_irqsave(&par->delayed_refresh_lock, flags); |
| 791 | |
| 792 | /* Merge dirty rectangle */ |
| 793 | par->x1 = min_t(int, par->x1, x1); |
| 794 | par->y1 = min_t(int, par->y1, y1); |
| 795 | par->x2 = max_t(int, par->x2, x2); |
| 796 | par->y2 = max_t(int, par->y2, y2); |
| 797 | |
| 798 | /* Schedule a delayed screen update if not yet */ |
| 799 | if (par->delayed_refresh == false) { |
| 800 | schedule_delayed_work(&par->dwork, |
| 801 | HVFB_ONDEMAND_THROTTLE); |
| 802 | par->delayed_refresh = true; |
| 803 | } |
| 804 | |
| 805 | spin_unlock_irqrestore(&par->delayed_refresh_lock, flags); |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Dexuan Cui | 3686fe9 | 2014-08-01 04:27:54 -0700 | [diff] [blame] | 808 | static int hvfb_on_panic(struct notifier_block *nb, |
| 809 | unsigned long e, void *p) |
| 810 | { |
| 811 | struct hvfb_par *par; |
| 812 | struct fb_info *info; |
| 813 | |
| 814 | par = container_of(nb, struct hvfb_par, hvfb_panic_nb); |
| 815 | par->synchronous_fb = true; |
| 816 | info = par->info; |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 817 | if (par->need_docopy) |
| 818 | hvfb_docopy(par, 0, dio_fb_size); |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 819 | synthvid_update(info, 0, 0, INT_MAX, INT_MAX); |
Dexuan Cui | 3686fe9 | 2014-08-01 04:27:54 -0700 | [diff] [blame] | 820 | |
| 821 | return NOTIFY_DONE; |
| 822 | } |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 823 | |
| 824 | /* Framebuffer operation handlers */ |
| 825 | |
| 826 | static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info) |
| 827 | { |
| 828 | if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN || |
| 829 | var->xres > screen_width || var->yres > screen_height || |
| 830 | var->bits_per_pixel != screen_depth) |
| 831 | return -EINVAL; |
| 832 | |
| 833 | var->xres_virtual = var->xres; |
| 834 | var->yres_virtual = var->yres; |
| 835 | |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | static int hvfb_set_par(struct fb_info *info) |
| 840 | { |
| 841 | struct hv_device *hdev = device_to_hv_device(info->device); |
| 842 | |
| 843 | return synthvid_send_situ(hdev); |
| 844 | } |
| 845 | |
| 846 | |
| 847 | static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf) |
| 848 | { |
| 849 | return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset; |
| 850 | } |
| 851 | |
| 852 | static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green, |
| 853 | unsigned blue, unsigned transp, struct fb_info *info) |
| 854 | { |
| 855 | u32 *pal = info->pseudo_palette; |
| 856 | |
| 857 | if (regno > 15) |
| 858 | return -EINVAL; |
| 859 | |
| 860 | pal[regno] = chan_to_field(red, &info->var.red) |
| 861 | | chan_to_field(green, &info->var.green) |
| 862 | | chan_to_field(blue, &info->var.blue) |
| 863 | | chan_to_field(transp, &info->var.transp); |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
Gerd Hoffmann | 9b5254b | 2013-10-02 13:55:11 +0200 | [diff] [blame] | 868 | static int hvfb_blank(int blank, struct fb_info *info) |
| 869 | { |
| 870 | return 1; /* get fb_blank to set the colormap to all black */ |
| 871 | } |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 872 | |
Dexuan Cui | 3686fe9 | 2014-08-01 04:27:54 -0700 | [diff] [blame] | 873 | static void hvfb_cfb_fillrect(struct fb_info *p, |
| 874 | const struct fb_fillrect *rect) |
| 875 | { |
| 876 | struct hvfb_par *par = p->par; |
| 877 | |
| 878 | cfb_fillrect(p, rect); |
| 879 | if (par->synchronous_fb) |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 880 | synthvid_update(p, 0, 0, INT_MAX, INT_MAX); |
| 881 | else |
| 882 | hvfb_ondemand_refresh_throttle(par, rect->dx, rect->dy, |
| 883 | rect->width, rect->height); |
Dexuan Cui | 3686fe9 | 2014-08-01 04:27:54 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | static void hvfb_cfb_copyarea(struct fb_info *p, |
| 887 | const struct fb_copyarea *area) |
| 888 | { |
| 889 | struct hvfb_par *par = p->par; |
| 890 | |
| 891 | cfb_copyarea(p, area); |
| 892 | if (par->synchronous_fb) |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 893 | synthvid_update(p, 0, 0, INT_MAX, INT_MAX); |
| 894 | else |
| 895 | hvfb_ondemand_refresh_throttle(par, area->dx, area->dy, |
| 896 | area->width, area->height); |
Dexuan Cui | 3686fe9 | 2014-08-01 04:27:54 -0700 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | static void hvfb_cfb_imageblit(struct fb_info *p, |
| 900 | const struct fb_image *image) |
| 901 | { |
| 902 | struct hvfb_par *par = p->par; |
| 903 | |
| 904 | cfb_imageblit(p, image); |
| 905 | if (par->synchronous_fb) |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 906 | synthvid_update(p, 0, 0, INT_MAX, INT_MAX); |
| 907 | else |
| 908 | hvfb_ondemand_refresh_throttle(par, image->dx, image->dy, |
| 909 | image->width, image->height); |
Dexuan Cui | 3686fe9 | 2014-08-01 04:27:54 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Jani Nikula | 8a48ac33 | 2019-12-03 18:38:50 +0200 | [diff] [blame] | 912 | static const struct fb_ops hvfb_ops = { |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 913 | .owner = THIS_MODULE, |
| 914 | .fb_check_var = hvfb_check_var, |
| 915 | .fb_set_par = hvfb_set_par, |
| 916 | .fb_setcolreg = hvfb_setcolreg, |
Dexuan Cui | 3686fe9 | 2014-08-01 04:27:54 -0700 | [diff] [blame] | 917 | .fb_fillrect = hvfb_cfb_fillrect, |
| 918 | .fb_copyarea = hvfb_cfb_copyarea, |
| 919 | .fb_imageblit = hvfb_cfb_imageblit, |
Gerd Hoffmann | 9b5254b | 2013-10-02 13:55:11 +0200 | [diff] [blame] | 920 | .fb_blank = hvfb_blank, |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 921 | }; |
| 922 | |
| 923 | |
| 924 | /* Get options from kernel paramenter "video=" */ |
| 925 | static void hvfb_get_option(struct fb_info *info) |
| 926 | { |
| 927 | struct hvfb_par *par = info->par; |
| 928 | char *opt = NULL, *p; |
| 929 | uint x = 0, y = 0; |
| 930 | |
| 931 | if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt) |
| 932 | return; |
| 933 | |
| 934 | p = strsep(&opt, "x"); |
| 935 | if (!*p || kstrtouint(p, 0, &x) || |
| 936 | !opt || !*opt || kstrtouint(opt, 0, &y)) { |
| 937 | pr_err("Screen option is invalid: skipped\n"); |
| 938 | return; |
| 939 | } |
| 940 | |
| 941 | if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN || |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 942 | (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10) && |
| 943 | (x > screen_width_max || y > screen_height_max)) || |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 944 | (par->synthvid_version == SYNTHVID_VERSION_WIN8 && |
| 945 | x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8) || |
| 946 | (par->synthvid_version == SYNTHVID_VERSION_WIN7 && |
| 947 | (x > SYNTHVID_WIDTH_MAX_WIN7 || y > SYNTHVID_HEIGHT_MAX_WIN7))) { |
| 948 | pr_err("Screen resolution option is out of range: skipped\n"); |
| 949 | return; |
| 950 | } |
| 951 | |
| 952 | screen_width = x; |
| 953 | screen_height = y; |
| 954 | return; |
| 955 | } |
| 956 | |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 957 | /* |
| 958 | * Allocate enough contiguous physical memory. |
| 959 | * Return physical address if succeeded or -1 if failed. |
| 960 | */ |
| 961 | static phys_addr_t hvfb_get_phymem(struct hv_device *hdev, |
| 962 | unsigned int request_size) |
| 963 | { |
| 964 | struct page *page = NULL; |
| 965 | dma_addr_t dma_handle; |
| 966 | void *vmem; |
| 967 | phys_addr_t paddr = 0; |
| 968 | unsigned int order = get_order(request_size); |
| 969 | |
| 970 | if (request_size == 0) |
| 971 | return -1; |
| 972 | |
| 973 | if (order < MAX_ORDER) { |
| 974 | /* Call alloc_pages if the size is less than 2^MAX_ORDER */ |
| 975 | page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order); |
| 976 | if (!page) |
| 977 | return -1; |
| 978 | |
| 979 | paddr = (page_to_pfn(page) << PAGE_SHIFT); |
| 980 | } else { |
| 981 | /* Allocate from CMA */ |
| 982 | hdev->device.coherent_dma_mask = DMA_BIT_MASK(64); |
| 983 | |
| 984 | vmem = dma_alloc_coherent(&hdev->device, |
| 985 | round_up(request_size, PAGE_SIZE), |
| 986 | &dma_handle, |
| 987 | GFP_KERNEL | __GFP_NOWARN); |
| 988 | |
| 989 | if (!vmem) |
| 990 | return -1; |
| 991 | |
| 992 | paddr = virt_to_phys(vmem); |
| 993 | } |
| 994 | |
| 995 | return paddr; |
| 996 | } |
| 997 | |
| 998 | /* Release contiguous physical memory */ |
| 999 | static void hvfb_release_phymem(struct hv_device *hdev, |
| 1000 | phys_addr_t paddr, unsigned int size) |
| 1001 | { |
| 1002 | unsigned int order = get_order(size); |
| 1003 | |
| 1004 | if (order < MAX_ORDER) |
| 1005 | __free_pages(pfn_to_page(paddr >> PAGE_SHIFT), order); |
| 1006 | else |
| 1007 | dma_free_coherent(&hdev->device, |
| 1008 | round_up(size, PAGE_SIZE), |
| 1009 | phys_to_virt(paddr), |
| 1010 | paddr); |
| 1011 | } |
| 1012 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1013 | |
| 1014 | /* Get framebuffer memory from Hyper-V video pci space */ |
Jake Oshins | 3546448 | 2015-08-05 00:52:37 -0700 | [diff] [blame] | 1015 | static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info) |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1016 | { |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 1017 | struct hvfb_par *par = info->par; |
| 1018 | struct pci_dev *pdev = NULL; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1019 | void __iomem *fb_virt; |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 1020 | int gen2vm = efi_enabled(EFI_BOOT); |
Jake Oshins | 3546448 | 2015-08-05 00:52:37 -0700 | [diff] [blame] | 1021 | resource_size_t pot_start, pot_end; |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 1022 | phys_addr_t paddr; |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 1023 | int ret; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1024 | |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 1025 | info->apertures = alloc_apertures(1); |
| 1026 | if (!info->apertures) |
| 1027 | return -ENOMEM; |
| 1028 | |
| 1029 | if (!gen2vm) { |
| 1030 | pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT, |
| 1031 | PCI_DEVICE_ID_HYPERV_VIDEO, NULL); |
| 1032 | if (!pdev) { |
| 1033 | pr_err("Unable to find PCI Hyper-V video\n"); |
| 1034 | kfree(info->apertures); |
| 1035 | return -ENODEV; |
| 1036 | } |
| 1037 | |
| 1038 | info->apertures->ranges[0].base = pci_resource_start(pdev, 0); |
| 1039 | info->apertures->ranges[0].size = pci_resource_len(pdev, 0); |
| 1040 | |
| 1041 | /* |
| 1042 | * For Gen 1 VM, we can directly use the contiguous memory |
| 1043 | * from VM. If we succeed, deferred IO happens directly |
| 1044 | * on this allocated framebuffer memory, avoiding extra |
| 1045 | * memory copy. |
| 1046 | */ |
| 1047 | paddr = hvfb_get_phymem(hdev, screen_fb_size); |
| 1048 | if (paddr != (phys_addr_t) -1) { |
| 1049 | par->mmio_pp = paddr; |
| 1050 | par->mmio_vp = par->dio_vp = __va(paddr); |
| 1051 | |
| 1052 | info->fix.smem_start = paddr; |
| 1053 | info->fix.smem_len = screen_fb_size; |
| 1054 | info->screen_base = par->mmio_vp; |
| 1055 | info->screen_size = screen_fb_size; |
| 1056 | |
| 1057 | par->need_docopy = false; |
| 1058 | goto getmem_done; |
| 1059 | } |
| 1060 | pr_info("Unable to allocate enough contiguous physical memory on Gen 1 VM. Using MMIO instead.\n"); |
| 1061 | } else { |
| 1062 | info->apertures->ranges[0].base = screen_info.lfb_base; |
| 1063 | info->apertures->ranges[0].size = screen_info.lfb_size; |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * Cannot use the contiguous physical memory. |
| 1068 | * Allocate mmio space for framebuffer. |
| 1069 | */ |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 1070 | dio_fb_size = |
| 1071 | screen_width * screen_height * screen_depth / 8; |
| 1072 | |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 1073 | if (gen2vm) { |
Jake Oshins | 3546448 | 2015-08-05 00:52:37 -0700 | [diff] [blame] | 1074 | pot_start = 0; |
| 1075 | pot_end = -1; |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 1076 | } else { |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 1077 | if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) || |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 1078 | pci_resource_len(pdev, 0) < screen_fb_size) { |
| 1079 | pr_err("Resource not available or (0x%lx < 0x%lx)\n", |
| 1080 | (unsigned long) pci_resource_len(pdev, 0), |
| 1081 | (unsigned long) screen_fb_size); |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 1082 | goto err1; |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 1083 | } |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 1084 | |
Jake Oshins | 3546448 | 2015-08-05 00:52:37 -0700 | [diff] [blame] | 1085 | pot_end = pci_resource_end(pdev, 0); |
| 1086 | pot_start = pot_end - screen_fb_size + 1; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
Jake Oshins | 3546448 | 2015-08-05 00:52:37 -0700 | [diff] [blame] | 1089 | ret = vmbus_allocate_mmio(&par->mem, hdev, pot_start, pot_end, |
| 1090 | screen_fb_size, 0x100000, true); |
| 1091 | if (ret != 0) { |
| 1092 | pr_err("Unable to allocate framebuffer memory\n"); |
| 1093 | goto err1; |
| 1094 | } |
| 1095 | |
| 1096 | fb_virt = ioremap(par->mem->start, screen_fb_size); |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1097 | if (!fb_virt) |
| 1098 | goto err2; |
| 1099 | |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 1100 | /* Allocate memory for deferred IO */ |
| 1101 | par->dio_vp = vzalloc(round_up(dio_fb_size, PAGE_SIZE)); |
| 1102 | if (par->dio_vp == NULL) |
| 1103 | goto err3; |
| 1104 | |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 1105 | /* Physical address of FB device */ |
| 1106 | par->mmio_pp = par->mem->start; |
| 1107 | /* Virtual address of FB device */ |
| 1108 | par->mmio_vp = (unsigned char *) fb_virt; |
| 1109 | |
Jake Oshins | 3546448 | 2015-08-05 00:52:37 -0700 | [diff] [blame] | 1110 | info->fix.smem_start = par->mem->start; |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 1111 | info->fix.smem_len = dio_fb_size; |
| 1112 | info->screen_base = par->dio_vp; |
| 1113 | info->screen_size = dio_fb_size; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1114 | |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 1115 | getmem_done: |
| 1116 | remove_conflicting_framebuffers(info->apertures, |
| 1117 | KBUILD_MODNAME, false); |
Kairui Song | 3cb73bc | 2020-10-14 17:24:29 +0800 | [diff] [blame] | 1118 | |
| 1119 | if (gen2vm) { |
| 1120 | /* framebuffer is reallocated, clear screen_info to avoid misuse from kexec */ |
| 1121 | screen_info.lfb_size = 0; |
| 1122 | screen_info.lfb_base = 0; |
| 1123 | screen_info.orig_video_isVGA = 0; |
| 1124 | } else { |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 1125 | pci_dev_put(pdev); |
Kairui Song | 3cb73bc | 2020-10-14 17:24:29 +0800 | [diff] [blame] | 1126 | } |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 1127 | kfree(info->apertures); |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 1128 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1129 | return 0; |
| 1130 | |
| 1131 | err3: |
| 1132 | iounmap(fb_virt); |
| 1133 | err2: |
Jake Oshins | 696ca5e | 2016-04-05 10:22:52 -0700 | [diff] [blame] | 1134 | vmbus_free_mmio(par->mem->start, screen_fb_size); |
Jake Oshins | 3546448 | 2015-08-05 00:52:37 -0700 | [diff] [blame] | 1135 | par->mem = NULL; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1136 | err1: |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 1137 | if (!gen2vm) |
| 1138 | pci_dev_put(pdev); |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 1139 | kfree(info->apertures); |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 1140 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1141 | return -ENOMEM; |
| 1142 | } |
| 1143 | |
| 1144 | /* Release the framebuffer */ |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 1145 | static void hvfb_putmem(struct hv_device *hdev, struct fb_info *info) |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1146 | { |
Gerd Hoffmann | 9069fd5 | 2014-02-26 11:51:24 +0100 | [diff] [blame] | 1147 | struct hvfb_par *par = info->par; |
| 1148 | |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 1149 | if (par->need_docopy) { |
| 1150 | vfree(par->dio_vp); |
| 1151 | iounmap(info->screen_base); |
| 1152 | vmbus_free_mmio(par->mem->start, screen_fb_size); |
| 1153 | } else { |
| 1154 | hvfb_release_phymem(hdev, info->fix.smem_start, |
| 1155 | screen_fb_size); |
| 1156 | } |
| 1157 | |
Jake Oshins | 3546448 | 2015-08-05 00:52:37 -0700 | [diff] [blame] | 1158 | par->mem = NULL; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | |
| 1162 | static int hvfb_probe(struct hv_device *hdev, |
| 1163 | const struct hv_vmbus_device_id *dev_id) |
| 1164 | { |
| 1165 | struct fb_info *info; |
| 1166 | struct hvfb_par *par; |
| 1167 | int ret; |
| 1168 | |
| 1169 | info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device); |
Bartlomiej Zolnierkiewicz | 0adcdbc | 2019-06-28 12:30:08 +0200 | [diff] [blame] | 1170 | if (!info) |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1171 | return -ENOMEM; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1172 | |
| 1173 | par = info->par; |
| 1174 | par->info = info; |
| 1175 | par->fb_ready = false; |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 1176 | par->need_docopy = true; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1177 | init_completion(&par->wait); |
| 1178 | INIT_DELAYED_WORK(&par->dwork, hvfb_update_work); |
| 1179 | |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 1180 | par->delayed_refresh = false; |
| 1181 | spin_lock_init(&par->delayed_refresh_lock); |
| 1182 | par->x1 = par->y1 = INT_MAX; |
| 1183 | par->x2 = par->y2 = 0; |
| 1184 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1185 | /* Connect to VSP */ |
| 1186 | hv_set_drvdata(hdev, info); |
| 1187 | ret = synthvid_connect_vsp(hdev); |
| 1188 | if (ret) { |
| 1189 | pr_err("Unable to connect to VSP\n"); |
| 1190 | goto error1; |
| 1191 | } |
| 1192 | |
Wei Hu | 67e7cdb | 2019-09-05 09:11:53 +0000 | [diff] [blame] | 1193 | hvfb_get_option(info); |
| 1194 | pr_info("Screen resolution: %dx%d, Color depth: %d\n", |
| 1195 | screen_width, screen_height, screen_depth); |
| 1196 | |
Jake Oshins | 3546448 | 2015-08-05 00:52:37 -0700 | [diff] [blame] | 1197 | ret = hvfb_getmem(hdev, info); |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1198 | if (ret) { |
| 1199 | pr_err("No memory for framebuffer\n"); |
| 1200 | goto error2; |
| 1201 | } |
| 1202 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1203 | /* Set up fb_info */ |
| 1204 | info->flags = FBINFO_DEFAULT; |
| 1205 | |
| 1206 | info->var.xres_virtual = info->var.xres = screen_width; |
| 1207 | info->var.yres_virtual = info->var.yres = screen_height; |
| 1208 | info->var.bits_per_pixel = screen_depth; |
| 1209 | |
| 1210 | if (info->var.bits_per_pixel == 16) { |
| 1211 | info->var.red = (struct fb_bitfield){11, 5, 0}; |
| 1212 | info->var.green = (struct fb_bitfield){5, 6, 0}; |
| 1213 | info->var.blue = (struct fb_bitfield){0, 5, 0}; |
| 1214 | info->var.transp = (struct fb_bitfield){0, 0, 0}; |
| 1215 | } else { |
| 1216 | info->var.red = (struct fb_bitfield){16, 8, 0}; |
| 1217 | info->var.green = (struct fb_bitfield){8, 8, 0}; |
| 1218 | info->var.blue = (struct fb_bitfield){0, 8, 0}; |
| 1219 | info->var.transp = (struct fb_bitfield){24, 8, 0}; |
| 1220 | } |
| 1221 | |
| 1222 | info->var.activate = FB_ACTIVATE_NOW; |
| 1223 | info->var.height = -1; |
| 1224 | info->var.width = -1; |
| 1225 | info->var.vmode = FB_VMODE_NONINTERLACED; |
| 1226 | |
| 1227 | strcpy(info->fix.id, KBUILD_MODNAME); |
| 1228 | info->fix.type = FB_TYPE_PACKED_PIXELS; |
| 1229 | info->fix.visual = FB_VISUAL_TRUECOLOR; |
| 1230 | info->fix.line_length = screen_width * screen_depth / 8; |
| 1231 | info->fix.accel = FB_ACCEL_NONE; |
| 1232 | |
| 1233 | info->fbops = &hvfb_ops; |
| 1234 | info->pseudo_palette = par->pseudo_palette; |
| 1235 | |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 1236 | /* Initialize deferred IO */ |
| 1237 | info->fbdefio = &synthvid_defio; |
| 1238 | fb_deferred_io_init(info); |
| 1239 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1240 | /* Send config to host */ |
| 1241 | ret = synthvid_send_config(hdev); |
| 1242 | if (ret) |
| 1243 | goto error; |
| 1244 | |
| 1245 | ret = register_framebuffer(info); |
| 1246 | if (ret) { |
| 1247 | pr_err("Unable to register framebuffer\n"); |
| 1248 | goto error; |
| 1249 | } |
| 1250 | |
| 1251 | par->fb_ready = true; |
| 1252 | |
Dexuan Cui | 3686fe9 | 2014-08-01 04:27:54 -0700 | [diff] [blame] | 1253 | par->synchronous_fb = false; |
| 1254 | par->hvfb_panic_nb.notifier_call = hvfb_on_panic; |
| 1255 | atomic_notifier_chain_register(&panic_notifier_list, |
| 1256 | &par->hvfb_panic_nb); |
| 1257 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1258 | return 0; |
| 1259 | |
| 1260 | error: |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 1261 | fb_deferred_io_cleanup(info); |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 1262 | hvfb_putmem(hdev, info); |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1263 | error2: |
| 1264 | vmbus_close(hdev->channel); |
| 1265 | error1: |
| 1266 | cancel_delayed_work_sync(&par->dwork); |
| 1267 | hv_set_drvdata(hdev, NULL); |
| 1268 | framebuffer_release(info); |
| 1269 | return ret; |
| 1270 | } |
| 1271 | |
| 1272 | |
| 1273 | static int hvfb_remove(struct hv_device *hdev) |
| 1274 | { |
| 1275 | struct fb_info *info = hv_get_drvdata(hdev); |
| 1276 | struct hvfb_par *par = info->par; |
| 1277 | |
Dexuan Cui | 3686fe9 | 2014-08-01 04:27:54 -0700 | [diff] [blame] | 1278 | atomic_notifier_chain_unregister(&panic_notifier_list, |
| 1279 | &par->hvfb_panic_nb); |
| 1280 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1281 | par->update = false; |
| 1282 | par->fb_ready = false; |
| 1283 | |
Wei Hu | d21987d | 2019-09-18 06:03:20 +0000 | [diff] [blame] | 1284 | fb_deferred_io_cleanup(info); |
| 1285 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1286 | unregister_framebuffer(info); |
| 1287 | cancel_delayed_work_sync(&par->dwork); |
| 1288 | |
| 1289 | vmbus_close(hdev->channel); |
| 1290 | hv_set_drvdata(hdev, NULL); |
| 1291 | |
Wei Hu | 3a6fb6c | 2019-12-09 15:57:49 +0800 | [diff] [blame] | 1292 | hvfb_putmem(hdev, info); |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1293 | framebuffer_release(info); |
| 1294 | |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
Dexuan Cui | 1ecf302 | 2019-09-11 23:34:10 +0000 | [diff] [blame] | 1298 | static int hvfb_suspend(struct hv_device *hdev) |
| 1299 | { |
| 1300 | struct fb_info *info = hv_get_drvdata(hdev); |
| 1301 | struct hvfb_par *par = info->par; |
| 1302 | |
| 1303 | console_lock(); |
| 1304 | |
| 1305 | /* 1 means do suspend */ |
| 1306 | fb_set_suspend(info, 1); |
| 1307 | |
| 1308 | cancel_delayed_work_sync(&par->dwork); |
Dexuan Cui | 382a462 | 2020-01-06 14:41:51 -0800 | [diff] [blame] | 1309 | cancel_delayed_work_sync(&info->deferred_work); |
Dexuan Cui | 1ecf302 | 2019-09-11 23:34:10 +0000 | [diff] [blame] | 1310 | |
| 1311 | par->update_saved = par->update; |
| 1312 | par->update = false; |
| 1313 | par->fb_ready = false; |
| 1314 | |
| 1315 | vmbus_close(hdev->channel); |
| 1316 | |
| 1317 | console_unlock(); |
| 1318 | |
| 1319 | return 0; |
| 1320 | } |
| 1321 | |
| 1322 | static int hvfb_resume(struct hv_device *hdev) |
| 1323 | { |
| 1324 | struct fb_info *info = hv_get_drvdata(hdev); |
| 1325 | struct hvfb_par *par = info->par; |
| 1326 | int ret; |
| 1327 | |
| 1328 | console_lock(); |
| 1329 | |
| 1330 | ret = synthvid_connect_vsp(hdev); |
| 1331 | if (ret != 0) |
| 1332 | goto out; |
| 1333 | |
| 1334 | ret = synthvid_send_config(hdev); |
| 1335 | if (ret != 0) { |
| 1336 | vmbus_close(hdev->channel); |
| 1337 | goto out; |
| 1338 | } |
| 1339 | |
| 1340 | par->fb_ready = true; |
| 1341 | par->update = par->update_saved; |
| 1342 | |
Dexuan Cui | 382a462 | 2020-01-06 14:41:51 -0800 | [diff] [blame] | 1343 | schedule_delayed_work(&info->deferred_work, info->fbdefio->delay); |
Dexuan Cui | 1ecf302 | 2019-09-11 23:34:10 +0000 | [diff] [blame] | 1344 | schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY); |
| 1345 | |
| 1346 | /* 0 means do resume */ |
| 1347 | fb_set_suspend(info, 0); |
| 1348 | |
| 1349 | out: |
| 1350 | console_unlock(); |
| 1351 | |
| 1352 | return ret; |
| 1353 | } |
| 1354 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1355 | |
Benoit Taine | 9baa3c3 | 2014-08-08 15:56:03 +0200 | [diff] [blame] | 1356 | static const struct pci_device_id pci_stub_id_table[] = { |
Gerd Hoffmann | 7ad9684 | 2013-10-02 13:55:10 +0200 | [diff] [blame] | 1357 | { |
| 1358 | .vendor = PCI_VENDOR_ID_MICROSOFT, |
| 1359 | .device = PCI_DEVICE_ID_HYPERV_VIDEO, |
| 1360 | }, |
| 1361 | { /* end of list */ } |
| 1362 | }; |
| 1363 | |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1364 | static const struct hv_vmbus_device_id id_table[] = { |
| 1365 | /* Synthetic Video Device GUID */ |
| 1366 | {HV_SYNTHVID_GUID}, |
| 1367 | {} |
| 1368 | }; |
| 1369 | |
Gerd Hoffmann | 7ad9684 | 2013-10-02 13:55:10 +0200 | [diff] [blame] | 1370 | MODULE_DEVICE_TABLE(pci, pci_stub_id_table); |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1371 | MODULE_DEVICE_TABLE(vmbus, id_table); |
| 1372 | |
| 1373 | static struct hv_driver hvfb_drv = { |
| 1374 | .name = KBUILD_MODNAME, |
| 1375 | .id_table = id_table, |
| 1376 | .probe = hvfb_probe, |
| 1377 | .remove = hvfb_remove, |
Dexuan Cui | 1ecf302 | 2019-09-11 23:34:10 +0000 | [diff] [blame] | 1378 | .suspend = hvfb_suspend, |
| 1379 | .resume = hvfb_resume, |
Arjan van de Ven | af0a564 | 2018-06-05 13:37:49 -0700 | [diff] [blame] | 1380 | .driver = { |
| 1381 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
| 1382 | }, |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1383 | }; |
| 1384 | |
Gerd Hoffmann | 7ad9684 | 2013-10-02 13:55:10 +0200 | [diff] [blame] | 1385 | static int hvfb_pci_stub_probe(struct pci_dev *pdev, |
| 1386 | const struct pci_device_id *ent) |
| 1387 | { |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
| 1391 | static void hvfb_pci_stub_remove(struct pci_dev *pdev) |
| 1392 | { |
| 1393 | } |
| 1394 | |
| 1395 | static struct pci_driver hvfb_pci_stub_driver = { |
| 1396 | .name = KBUILD_MODNAME, |
| 1397 | .id_table = pci_stub_id_table, |
| 1398 | .probe = hvfb_pci_stub_probe, |
| 1399 | .remove = hvfb_pci_stub_remove, |
Arjan van de Ven | af0a564 | 2018-06-05 13:37:49 -0700 | [diff] [blame] | 1400 | .driver = { |
| 1401 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
| 1402 | } |
Gerd Hoffmann | 7ad9684 | 2013-10-02 13:55:10 +0200 | [diff] [blame] | 1403 | }; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1404 | |
| 1405 | static int __init hvfb_drv_init(void) |
| 1406 | { |
Gerd Hoffmann | 7ad9684 | 2013-10-02 13:55:10 +0200 | [diff] [blame] | 1407 | int ret; |
| 1408 | |
| 1409 | ret = vmbus_driver_register(&hvfb_drv); |
| 1410 | if (ret != 0) |
| 1411 | return ret; |
| 1412 | |
| 1413 | ret = pci_register_driver(&hvfb_pci_stub_driver); |
| 1414 | if (ret != 0) { |
| 1415 | vmbus_driver_unregister(&hvfb_drv); |
| 1416 | return ret; |
| 1417 | } |
| 1418 | |
| 1419 | return 0; |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | static void __exit hvfb_drv_exit(void) |
| 1423 | { |
Gerd Hoffmann | 7ad9684 | 2013-10-02 13:55:10 +0200 | [diff] [blame] | 1424 | pci_unregister_driver(&hvfb_pci_stub_driver); |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1425 | vmbus_driver_unregister(&hvfb_drv); |
| 1426 | } |
| 1427 | |
| 1428 | module_init(hvfb_drv_init); |
| 1429 | module_exit(hvfb_drv_exit); |
| 1430 | |
| 1431 | MODULE_LICENSE("GPL"); |
Haiyang Zhang | 68a2d20b | 2013-04-29 15:05:42 -0700 | [diff] [blame] | 1432 | MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver"); |