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 <[email protected]>
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
488d69d008SThomas Zimmermann #include <linux/aperture.h>
49f7018c21STomi Valkeinen #include <linux/module.h>
50f7018c21STomi Valkeinen #include <linux/kernel.h>
5134a28083SOlaf Hering #include <linux/vmalloc.h>
52f7018c21STomi Valkeinen #include <linux/init.h>
53f7018c21STomi Valkeinen #include <linux/completion.h>
54f7018c21STomi Valkeinen #include <linux/fb.h>
55f7018c21STomi Valkeinen #include <linux/pci.h>
56f39650deSAndy Shevchenko #include <linux/panic_notifier.h>
57f7018c21STomi Valkeinen #include <linux/efi.h>
581ecf3020SDexuan Cui #include <linux/console.h>
59f7018c21STomi Valkeinen
60f7018c21STomi Valkeinen #include <linux/hyperv.h>
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))
66b0cce4f6SMichael Kelley /* Support for VERSION_WIN7 is removed. #define is retained for reference. */
67f7018c21STomi Valkeinen #define SYNTHVID_VERSION_WIN7 SYNTHVID_VERSION(3, 0)
68f7018c21STomi Valkeinen #define SYNTHVID_VERSION_WIN8 SYNTHVID_VERSION(3, 2)
6967e7cdb4SWei Hu #define SYNTHVID_VERSION_WIN10 SYNTHVID_VERSION(3, 5)
7067e7cdb4SWei Hu
7167e7cdb4SWei Hu #define SYNTHVID_VER_GET_MAJOR(ver) (ver & 0x0000ffff)
7267e7cdb4SWei Hu #define SYNTHVID_VER_GET_MINOR(ver) ((ver & 0xffff0000) >> 16)
73f7018c21STomi Valkeinen
74f7018c21STomi Valkeinen #define SYNTHVID_DEPTH_WIN8 32
75f7018c21STomi Valkeinen #define SYNTHVID_FB_SIZE_WIN8 (8 * 1024 * 1024)
76f7018c21STomi Valkeinen
77f7018c21STomi Valkeinen enum pipe_msg_type {
78f7018c21STomi Valkeinen PIPE_MSG_INVALID,
79f7018c21STomi Valkeinen PIPE_MSG_DATA,
80f7018c21STomi Valkeinen PIPE_MSG_MAX
81f7018c21STomi Valkeinen };
82f7018c21STomi Valkeinen
83f7018c21STomi Valkeinen struct pipe_msg_hdr {
84f7018c21STomi Valkeinen u32 type;
85f7018c21STomi Valkeinen u32 size; /* size of message after this field */
86f7018c21STomi Valkeinen } __packed;
87f7018c21STomi Valkeinen
88f7018c21STomi Valkeinen
89f7018c21STomi Valkeinen enum synthvid_msg_type {
90f7018c21STomi Valkeinen SYNTHVID_ERROR = 0,
91f7018c21STomi Valkeinen SYNTHVID_VERSION_REQUEST = 1,
92f7018c21STomi Valkeinen SYNTHVID_VERSION_RESPONSE = 2,
93f7018c21STomi Valkeinen SYNTHVID_VRAM_LOCATION = 3,
94f7018c21STomi Valkeinen SYNTHVID_VRAM_LOCATION_ACK = 4,
95f7018c21STomi Valkeinen SYNTHVID_SITUATION_UPDATE = 5,
96f7018c21STomi Valkeinen SYNTHVID_SITUATION_UPDATE_ACK = 6,
97f7018c21STomi Valkeinen SYNTHVID_POINTER_POSITION = 7,
98f7018c21STomi Valkeinen SYNTHVID_POINTER_SHAPE = 8,
99f7018c21STomi Valkeinen SYNTHVID_FEATURE_CHANGE = 9,
100f7018c21STomi Valkeinen SYNTHVID_DIRT = 10,
10167e7cdb4SWei Hu SYNTHVID_RESOLUTION_REQUEST = 13,
10267e7cdb4SWei Hu SYNTHVID_RESOLUTION_RESPONSE = 14,
103f7018c21STomi Valkeinen
10467e7cdb4SWei Hu SYNTHVID_MAX = 15
105f7018c21STomi Valkeinen };
106f7018c21STomi Valkeinen
10767e7cdb4SWei Hu #define SYNTHVID_EDID_BLOCK_SIZE 128
10867e7cdb4SWei Hu #define SYNTHVID_MAX_RESOLUTION_COUNT 64
10967e7cdb4SWei Hu
11067e7cdb4SWei Hu struct hvd_screen_info {
11167e7cdb4SWei Hu u16 width;
11267e7cdb4SWei Hu u16 height;
11367e7cdb4SWei Hu } __packed;
11467e7cdb4SWei Hu
115f7018c21STomi Valkeinen struct synthvid_msg_hdr {
116f7018c21STomi Valkeinen u32 type;
117f7018c21STomi Valkeinen u32 size; /* size of this header + payload after this field*/
118f7018c21STomi Valkeinen } __packed;
119f7018c21STomi Valkeinen
120f7018c21STomi Valkeinen struct synthvid_version_req {
121f7018c21STomi Valkeinen u32 version;
122f7018c21STomi Valkeinen } __packed;
123f7018c21STomi Valkeinen
124f7018c21STomi Valkeinen struct synthvid_version_resp {
125f7018c21STomi Valkeinen u32 version;
126f7018c21STomi Valkeinen u8 is_accepted;
127f7018c21STomi Valkeinen u8 max_video_outputs;
128f7018c21STomi Valkeinen } __packed;
129f7018c21STomi Valkeinen
13067e7cdb4SWei Hu struct synthvid_supported_resolution_req {
13167e7cdb4SWei Hu u8 maximum_resolution_count;
13267e7cdb4SWei Hu } __packed;
13367e7cdb4SWei Hu
13467e7cdb4SWei Hu struct synthvid_supported_resolution_resp {
13567e7cdb4SWei Hu u8 edid_block[SYNTHVID_EDID_BLOCK_SIZE];
13667e7cdb4SWei Hu u8 resolution_count;
13767e7cdb4SWei Hu u8 default_resolution_index;
13867e7cdb4SWei Hu u8 is_standard;
13967e7cdb4SWei Hu struct hvd_screen_info
14067e7cdb4SWei Hu supported_resolution[SYNTHVID_MAX_RESOLUTION_COUNT];
14167e7cdb4SWei Hu } __packed;
14267e7cdb4SWei Hu
143f7018c21STomi Valkeinen struct synthvid_vram_location {
144f7018c21STomi Valkeinen u64 user_ctx;
145f7018c21STomi Valkeinen u8 is_vram_gpa_specified;
146f7018c21STomi Valkeinen u64 vram_gpa;
147f7018c21STomi Valkeinen } __packed;
148f7018c21STomi Valkeinen
149f7018c21STomi Valkeinen struct synthvid_vram_location_ack {
150f7018c21STomi Valkeinen u64 user_ctx;
151f7018c21STomi Valkeinen } __packed;
152f7018c21STomi Valkeinen
153f7018c21STomi Valkeinen struct video_output_situation {
154f7018c21STomi Valkeinen u8 active;
155f7018c21STomi Valkeinen u32 vram_offset;
156f7018c21STomi Valkeinen u8 depth_bits;
157f7018c21STomi Valkeinen u32 width_pixels;
158f7018c21STomi Valkeinen u32 height_pixels;
159f7018c21STomi Valkeinen u32 pitch_bytes;
160f7018c21STomi Valkeinen } __packed;
161f7018c21STomi Valkeinen
162f7018c21STomi Valkeinen struct synthvid_situation_update {
163f7018c21STomi Valkeinen u64 user_ctx;
164f7018c21STomi Valkeinen u8 video_output_count;
165f7018c21STomi Valkeinen struct video_output_situation video_output[1];
166f7018c21STomi Valkeinen } __packed;
167f7018c21STomi Valkeinen
168f7018c21STomi Valkeinen struct synthvid_situation_update_ack {
169f7018c21STomi Valkeinen u64 user_ctx;
170f7018c21STomi Valkeinen } __packed;
171f7018c21STomi Valkeinen
172f7018c21STomi Valkeinen struct synthvid_pointer_position {
173f7018c21STomi Valkeinen u8 is_visible;
174f7018c21STomi Valkeinen u8 video_output;
175f7018c21STomi Valkeinen s32 image_x;
176f7018c21STomi Valkeinen s32 image_y;
177f7018c21STomi Valkeinen } __packed;
178f7018c21STomi Valkeinen
179f7018c21STomi Valkeinen
180f7018c21STomi Valkeinen #define CURSOR_MAX_X 96
181f7018c21STomi Valkeinen #define CURSOR_MAX_Y 96
182f7018c21STomi Valkeinen #define CURSOR_ARGB_PIXEL_SIZE 4
183f7018c21STomi Valkeinen #define CURSOR_MAX_SIZE (CURSOR_MAX_X * CURSOR_MAX_Y * CURSOR_ARGB_PIXEL_SIZE)
184f7018c21STomi Valkeinen #define CURSOR_COMPLETE (-1)
185f7018c21STomi Valkeinen
186f7018c21STomi Valkeinen struct synthvid_pointer_shape {
187f7018c21STomi Valkeinen u8 part_idx;
188f7018c21STomi Valkeinen u8 is_argb;
189f7018c21STomi Valkeinen u32 width; /* CURSOR_MAX_X at most */
190f7018c21STomi Valkeinen u32 height; /* CURSOR_MAX_Y at most */
191f7018c21STomi Valkeinen u32 hot_x; /* hotspot relative to upper-left of pointer image */
192f7018c21STomi Valkeinen u32 hot_y;
193f7018c21STomi Valkeinen u8 data[4];
194f7018c21STomi Valkeinen } __packed;
195f7018c21STomi Valkeinen
196f7018c21STomi Valkeinen struct synthvid_feature_change {
197f7018c21STomi Valkeinen u8 is_dirt_needed;
198f7018c21STomi Valkeinen u8 is_ptr_pos_needed;
199f7018c21STomi Valkeinen u8 is_ptr_shape_needed;
200f7018c21STomi Valkeinen u8 is_situ_needed;
201f7018c21STomi Valkeinen } __packed;
202f7018c21STomi Valkeinen
203f7018c21STomi Valkeinen struct rect {
204f7018c21STomi Valkeinen s32 x1, y1; /* top left corner */
205f7018c21STomi Valkeinen s32 x2, y2; /* bottom right corner, exclusive */
206f7018c21STomi Valkeinen } __packed;
207f7018c21STomi Valkeinen
208f7018c21STomi Valkeinen struct synthvid_dirt {
209f7018c21STomi Valkeinen u8 video_output;
210f7018c21STomi Valkeinen u8 dirt_count;
211f7018c21STomi Valkeinen struct rect rect[1];
212f7018c21STomi Valkeinen } __packed;
213f7018c21STomi Valkeinen
214f7018c21STomi Valkeinen struct synthvid_msg {
215f7018c21STomi Valkeinen struct pipe_msg_hdr pipe_hdr;
216f7018c21STomi Valkeinen struct synthvid_msg_hdr vid_hdr;
217f7018c21STomi Valkeinen union {
218f7018c21STomi Valkeinen struct synthvid_version_req ver_req;
219f7018c21STomi Valkeinen struct synthvid_version_resp ver_resp;
220f7018c21STomi Valkeinen struct synthvid_vram_location vram;
221f7018c21STomi Valkeinen struct synthvid_vram_location_ack vram_ack;
222f7018c21STomi Valkeinen struct synthvid_situation_update situ;
223f7018c21STomi Valkeinen struct synthvid_situation_update_ack situ_ack;
224f7018c21STomi Valkeinen struct synthvid_pointer_position ptr_pos;
225f7018c21STomi Valkeinen struct synthvid_pointer_shape ptr_shape;
226f7018c21STomi Valkeinen struct synthvid_feature_change feature_chg;
227f7018c21STomi Valkeinen struct synthvid_dirt dirt;
22867e7cdb4SWei Hu struct synthvid_supported_resolution_req resolution_req;
22967e7cdb4SWei Hu struct synthvid_supported_resolution_resp resolution_resp;
230f7018c21STomi Valkeinen };
231f7018c21STomi Valkeinen } __packed;
232f7018c21STomi Valkeinen
233f7018c21STomi Valkeinen
234f7018c21STomi Valkeinen /* FB driver definitions and structures */
235f7018c21STomi Valkeinen #define HVFB_WIDTH 1152 /* default screen width */
236f7018c21STomi Valkeinen #define HVFB_HEIGHT 864 /* default screen height */
237f7018c21STomi Valkeinen #define HVFB_WIDTH_MIN 640
238f7018c21STomi Valkeinen #define HVFB_HEIGHT_MIN 480
239f7018c21STomi Valkeinen
240f7018c21STomi Valkeinen #define RING_BUFSIZE (256 * 1024)
241f7018c21STomi Valkeinen #define VSP_TIMEOUT (10 * HZ)
242f7018c21STomi Valkeinen #define HVFB_UPDATE_DELAY (HZ / 20)
243d21987d7SWei Hu #define HVFB_ONDEMAND_THROTTLE (HZ / 20)
244f7018c21STomi Valkeinen
245f7018c21STomi Valkeinen struct hvfb_par {
246f7018c21STomi Valkeinen struct fb_info *info;
24735464483SJake Oshins struct resource *mem;
248f7018c21STomi Valkeinen bool fb_ready; /* fb device is ready */
249f7018c21STomi Valkeinen struct completion wait;
250f7018c21STomi Valkeinen u32 synthvid_version;
251f7018c21STomi Valkeinen
252f7018c21STomi Valkeinen struct delayed_work dwork;
253f7018c21STomi Valkeinen bool update;
2541ecf3020SDexuan Cui bool update_saved; /* The value of 'update' before hibernation */
255f7018c21STomi Valkeinen
256f7018c21STomi Valkeinen u32 pseudo_palette[16];
257f7018c21STomi Valkeinen u8 init_buf[MAX_VMBUS_PKT_SIZE];
258f7018c21STomi Valkeinen u8 recv_buf[MAX_VMBUS_PKT_SIZE];
2593686fe96SDexuan Cui
2603686fe96SDexuan Cui /* If true, the VSC notifies the VSP on every framebuffer change */
2613686fe96SDexuan Cui bool synchronous_fb;
2623686fe96SDexuan Cui
2633a6fb6c4SWei Hu /* If true, need to copy from deferred IO mem to framebuffer mem */
2643a6fb6c4SWei Hu bool need_docopy;
2653a6fb6c4SWei Hu
2663686fe96SDexuan Cui struct notifier_block hvfb_panic_nb;
267d21987d7SWei Hu
268d21987d7SWei Hu /* Memory for deferred IO and frame buffer itself */
269d21987d7SWei Hu unsigned char *dio_vp;
270d21987d7SWei Hu unsigned char *mmio_vp;
2713a6fb6c4SWei Hu phys_addr_t mmio_pp;
272d21987d7SWei Hu
273d21987d7SWei Hu /* Dirty rectangle, protected by delayed_refresh_lock */
274d21987d7SWei Hu int x1, y1, x2, y2;
275d21987d7SWei Hu bool delayed_refresh;
276d21987d7SWei Hu spinlock_t delayed_refresh_lock;
277f7018c21STomi Valkeinen };
278f7018c21STomi Valkeinen
279f7018c21STomi Valkeinen static uint screen_width = HVFB_WIDTH;
280f7018c21STomi Valkeinen static uint screen_height = HVFB_HEIGHT;
281f7018c21STomi Valkeinen static uint screen_depth;
282f7018c21STomi Valkeinen static uint screen_fb_size;
283d21987d7SWei Hu static uint dio_fb_size; /* FB size for deferred IO */
284f7018c21STomi Valkeinen
285*ea2f45abSSaurabh Sengar static void hvfb_putmem(struct fb_info *info);
286*ea2f45abSSaurabh Sengar
287f7018c21STomi Valkeinen /* Send message to Hyper-V host */
synthvid_send(struct hv_device * hdev,struct synthvid_msg * msg)288f7018c21STomi Valkeinen static inline int synthvid_send(struct hv_device *hdev,
289f7018c21STomi Valkeinen struct synthvid_msg *msg)
290f7018c21STomi Valkeinen {
291f7018c21STomi Valkeinen static atomic64_t request_id = ATOMIC64_INIT(0);
292f7018c21STomi Valkeinen int ret;
293f7018c21STomi Valkeinen
294f7018c21STomi Valkeinen msg->pipe_hdr.type = PIPE_MSG_DATA;
295f7018c21STomi Valkeinen msg->pipe_hdr.size = msg->vid_hdr.size;
296f7018c21STomi Valkeinen
297f7018c21STomi Valkeinen ret = vmbus_sendpacket(hdev->channel, msg,
298f7018c21STomi Valkeinen msg->vid_hdr.size + sizeof(struct pipe_msg_hdr),
299f7018c21STomi Valkeinen atomic64_inc_return(&request_id),
300f7018c21STomi Valkeinen VM_PKT_DATA_INBAND, 0);
301f7018c21STomi Valkeinen
302f7018c21STomi Valkeinen if (ret)
303aa5b7d11SMichael Kelley pr_err_ratelimited("Unable to send packet via vmbus; error %d\n", ret);
304f7018c21STomi Valkeinen
305f7018c21STomi Valkeinen return ret;
306f7018c21STomi Valkeinen }
307f7018c21STomi Valkeinen
308f7018c21STomi Valkeinen
309f7018c21STomi Valkeinen /* Send screen resolution info to host */
synthvid_send_situ(struct hv_device * hdev)310f7018c21STomi Valkeinen static int synthvid_send_situ(struct hv_device *hdev)
311f7018c21STomi Valkeinen {
312f7018c21STomi Valkeinen struct fb_info *info = hv_get_drvdata(hdev);
313f7018c21STomi Valkeinen struct synthvid_msg msg;
314f7018c21STomi Valkeinen
315f7018c21STomi Valkeinen if (!info)
316f7018c21STomi Valkeinen return -ENODEV;
317f7018c21STomi Valkeinen
318f7018c21STomi Valkeinen memset(&msg, 0, sizeof(struct synthvid_msg));
319f7018c21STomi Valkeinen
320f7018c21STomi Valkeinen msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE;
321f7018c21STomi Valkeinen msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
322f7018c21STomi Valkeinen sizeof(struct synthvid_situation_update);
323f7018c21STomi Valkeinen msg.situ.user_ctx = 0;
324f7018c21STomi Valkeinen msg.situ.video_output_count = 1;
325f7018c21STomi Valkeinen msg.situ.video_output[0].active = 1;
326f7018c21STomi Valkeinen msg.situ.video_output[0].vram_offset = 0;
327f7018c21STomi Valkeinen msg.situ.video_output[0].depth_bits = info->var.bits_per_pixel;
328f7018c21STomi Valkeinen msg.situ.video_output[0].width_pixels = info->var.xres;
329f7018c21STomi Valkeinen msg.situ.video_output[0].height_pixels = info->var.yres;
330f7018c21STomi Valkeinen msg.situ.video_output[0].pitch_bytes = info->fix.line_length;
331f7018c21STomi Valkeinen
332f7018c21STomi Valkeinen synthvid_send(hdev, &msg);
333f7018c21STomi Valkeinen
334f7018c21STomi Valkeinen return 0;
335f7018c21STomi Valkeinen }
336f7018c21STomi Valkeinen
337f7018c21STomi Valkeinen /* Send mouse pointer info to host */
synthvid_send_ptr(struct hv_device * hdev)338f7018c21STomi Valkeinen static int synthvid_send_ptr(struct hv_device *hdev)
339f7018c21STomi Valkeinen {
340f7018c21STomi Valkeinen struct synthvid_msg msg;
341f7018c21STomi Valkeinen
342f7018c21STomi Valkeinen memset(&msg, 0, sizeof(struct synthvid_msg));
343f7018c21STomi Valkeinen msg.vid_hdr.type = SYNTHVID_POINTER_POSITION;
344f7018c21STomi Valkeinen msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
345f7018c21STomi Valkeinen sizeof(struct synthvid_pointer_position);
346f7018c21STomi Valkeinen msg.ptr_pos.is_visible = 1;
347f7018c21STomi Valkeinen msg.ptr_pos.video_output = 0;
348f7018c21STomi Valkeinen msg.ptr_pos.image_x = 0;
349f7018c21STomi Valkeinen msg.ptr_pos.image_y = 0;
350f7018c21STomi Valkeinen synthvid_send(hdev, &msg);
351f7018c21STomi Valkeinen
352f7018c21STomi Valkeinen memset(&msg, 0, sizeof(struct synthvid_msg));
353f7018c21STomi Valkeinen msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE;
354f7018c21STomi Valkeinen msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
355f7018c21STomi Valkeinen sizeof(struct synthvid_pointer_shape);
356f7018c21STomi Valkeinen msg.ptr_shape.part_idx = CURSOR_COMPLETE;
357f7018c21STomi Valkeinen msg.ptr_shape.is_argb = 1;
358f7018c21STomi Valkeinen msg.ptr_shape.width = 1;
359f7018c21STomi Valkeinen msg.ptr_shape.height = 1;
360f7018c21STomi Valkeinen msg.ptr_shape.hot_x = 0;
361f7018c21STomi Valkeinen msg.ptr_shape.hot_y = 0;
362f7018c21STomi Valkeinen msg.ptr_shape.data[0] = 0;
363f7018c21STomi Valkeinen msg.ptr_shape.data[1] = 1;
364f7018c21STomi Valkeinen msg.ptr_shape.data[2] = 1;
365f7018c21STomi Valkeinen msg.ptr_shape.data[3] = 1;
366f7018c21STomi Valkeinen synthvid_send(hdev, &msg);
367f7018c21STomi Valkeinen
368f7018c21STomi Valkeinen return 0;
369f7018c21STomi Valkeinen }
370f7018c21STomi Valkeinen
371f7018c21STomi Valkeinen /* Send updated screen area (dirty rectangle) location to host */
372d21987d7SWei Hu static int
synthvid_update(struct fb_info * info,int x1,int y1,int x2,int y2)373d21987d7SWei Hu synthvid_update(struct fb_info *info, int x1, int y1, int x2, int y2)
374f7018c21STomi Valkeinen {
375f7018c21STomi Valkeinen struct hv_device *hdev = device_to_hv_device(info->device);
376f7018c21STomi Valkeinen struct synthvid_msg msg;
377f7018c21STomi Valkeinen
378f7018c21STomi Valkeinen memset(&msg, 0, sizeof(struct synthvid_msg));
379d21987d7SWei Hu if (x2 == INT_MAX)
380d21987d7SWei Hu x2 = info->var.xres;
381d21987d7SWei Hu if (y2 == INT_MAX)
382d21987d7SWei Hu y2 = info->var.yres;
383f7018c21STomi Valkeinen
384f7018c21STomi Valkeinen msg.vid_hdr.type = SYNTHVID_DIRT;
385f7018c21STomi Valkeinen msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
386f7018c21STomi Valkeinen sizeof(struct synthvid_dirt);
387f7018c21STomi Valkeinen msg.dirt.video_output = 0;
388f7018c21STomi Valkeinen msg.dirt.dirt_count = 1;
389d21987d7SWei Hu msg.dirt.rect[0].x1 = (x1 > x2) ? 0 : x1;
390d21987d7SWei Hu msg.dirt.rect[0].y1 = (y1 > y2) ? 0 : y1;
391d21987d7SWei Hu msg.dirt.rect[0].x2 =
392d21987d7SWei Hu (x2 < x1 || x2 > info->var.xres) ? info->var.xres : x2;
393d21987d7SWei Hu msg.dirt.rect[0].y2 =
394d21987d7SWei Hu (y2 < y1 || y2 > info->var.yres) ? info->var.yres : y2;
395f7018c21STomi Valkeinen
396f7018c21STomi Valkeinen synthvid_send(hdev, &msg);
397f7018c21STomi Valkeinen
398f7018c21STomi Valkeinen return 0;
399f7018c21STomi Valkeinen }
400f7018c21STomi Valkeinen
hvfb_docopy(struct hvfb_par * par,unsigned long offset,unsigned long size)401d21987d7SWei Hu static void hvfb_docopy(struct hvfb_par *par,
402d21987d7SWei Hu unsigned long offset,
403d21987d7SWei Hu unsigned long size)
404d21987d7SWei Hu {
405d21987d7SWei Hu if (!par || !par->mmio_vp || !par->dio_vp || !par->fb_ready ||
406d21987d7SWei Hu size == 0 || offset >= dio_fb_size)
407d21987d7SWei Hu return;
408d21987d7SWei Hu
409d21987d7SWei Hu if (offset + size > dio_fb_size)
410d21987d7SWei Hu size = dio_fb_size - offset;
411d21987d7SWei Hu
412d21987d7SWei Hu memcpy(par->mmio_vp + offset, par->dio_vp + offset, size);
413d21987d7SWei Hu }
414d21987d7SWei Hu
415d21987d7SWei Hu /* Deferred IO callback */
synthvid_deferred_io(struct fb_info * p,struct list_head * pagereflist)416e80eec1bSThomas Zimmermann static void synthvid_deferred_io(struct fb_info *p, struct list_head *pagereflist)
417d21987d7SWei Hu {
418d21987d7SWei Hu struct hvfb_par *par = p->par;
41956c134f7SThomas Zimmermann struct fb_deferred_io_pageref *pageref;
420d21987d7SWei Hu unsigned long start, end;
421d21987d7SWei Hu int y1, y2, miny, maxy;
422d21987d7SWei Hu
423d21987d7SWei Hu miny = INT_MAX;
424d21987d7SWei Hu maxy = 0;
425d21987d7SWei Hu
426d21987d7SWei Hu /*
427d21987d7SWei Hu * Merge dirty pages. It is possible that last page cross
428d21987d7SWei Hu * over the end of frame buffer row yres. This is taken care of
429d21987d7SWei Hu * in synthvid_update function by clamping the y2
430d21987d7SWei Hu * value to yres.
431d21987d7SWei Hu */
432e80eec1bSThomas Zimmermann list_for_each_entry(pageref, pagereflist, list) {
433e2d8b428SThomas Zimmermann start = pageref->offset;
434d21987d7SWei Hu end = start + PAGE_SIZE - 1;
435d21987d7SWei Hu y1 = start / p->fix.line_length;
436d21987d7SWei Hu y2 = end / p->fix.line_length;
437d21987d7SWei Hu miny = min_t(int, miny, y1);
438d21987d7SWei Hu maxy = max_t(int, maxy, y2);
439d21987d7SWei Hu
440d21987d7SWei Hu /* Copy from dio space to mmio address */
4413a6fb6c4SWei Hu if (par->fb_ready && par->need_docopy)
442d21987d7SWei Hu hvfb_docopy(par, start, PAGE_SIZE);
443d21987d7SWei Hu }
444d21987d7SWei Hu
445d21987d7SWei Hu if (par->fb_ready && par->update)
446d21987d7SWei Hu synthvid_update(p, 0, miny, p->var.xres, maxy + 1);
447d21987d7SWei Hu }
448d21987d7SWei Hu
449d21987d7SWei Hu static struct fb_deferred_io synthvid_defio = {
450d21987d7SWei Hu .delay = HZ / 20,
451d21987d7SWei Hu .deferred_io = synthvid_deferred_io,
452d21987d7SWei Hu };
453f7018c21STomi Valkeinen
454f7018c21STomi Valkeinen /*
455f7018c21STomi Valkeinen * Actions on received messages from host:
456f7018c21STomi Valkeinen * Complete the wait event.
457f7018c21STomi Valkeinen * Or, reply with screen and cursor info.
458f7018c21STomi Valkeinen */
synthvid_recv_sub(struct hv_device * hdev)459f7018c21STomi Valkeinen static void synthvid_recv_sub(struct hv_device *hdev)
460f7018c21STomi Valkeinen {
461f7018c21STomi Valkeinen struct fb_info *info = hv_get_drvdata(hdev);
462f7018c21STomi Valkeinen struct hvfb_par *par;
463f7018c21STomi Valkeinen struct synthvid_msg *msg;
464f7018c21STomi Valkeinen
465f7018c21STomi Valkeinen if (!info)
466f7018c21STomi Valkeinen return;
467f7018c21STomi Valkeinen
468f7018c21STomi Valkeinen par = info->par;
469f7018c21STomi Valkeinen msg = (struct synthvid_msg *)par->recv_buf;
470f7018c21STomi Valkeinen
471f7018c21STomi Valkeinen /* Complete the wait event */
472f7018c21STomi Valkeinen if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE ||
47367e7cdb4SWei Hu msg->vid_hdr.type == SYNTHVID_RESOLUTION_RESPONSE ||
474f7018c21STomi Valkeinen msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) {
475f7018c21STomi Valkeinen memcpy(par->init_buf, msg, MAX_VMBUS_PKT_SIZE);
476f7018c21STomi Valkeinen complete(&par->wait);
477f7018c21STomi Valkeinen return;
478f7018c21STomi Valkeinen }
479f7018c21STomi Valkeinen
480f7018c21STomi Valkeinen /* Reply with screen and cursor info */
481f7018c21STomi Valkeinen if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) {
482f7018c21STomi Valkeinen if (par->fb_ready) {
483f7018c21STomi Valkeinen synthvid_send_ptr(hdev);
484f7018c21STomi Valkeinen synthvid_send_situ(hdev);
485f7018c21STomi Valkeinen }
486f7018c21STomi Valkeinen
487f7018c21STomi Valkeinen par->update = msg->feature_chg.is_dirt_needed;
488f7018c21STomi Valkeinen if (par->update)
489f7018c21STomi Valkeinen schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
490f7018c21STomi Valkeinen }
491f7018c21STomi Valkeinen }
492f7018c21STomi Valkeinen
493f7018c21STomi Valkeinen /* Receive callback for messages from the host */
synthvid_receive(void * ctx)494f7018c21STomi Valkeinen static void synthvid_receive(void *ctx)
495f7018c21STomi Valkeinen {
496f7018c21STomi Valkeinen struct hv_device *hdev = ctx;
497f7018c21STomi Valkeinen struct fb_info *info = hv_get_drvdata(hdev);
498f7018c21STomi Valkeinen struct hvfb_par *par;
499f7018c21STomi Valkeinen struct synthvid_msg *recv_buf;
500f7018c21STomi Valkeinen u32 bytes_recvd;
501f7018c21STomi Valkeinen u64 req_id;
502f7018c21STomi Valkeinen int ret;
503f7018c21STomi Valkeinen
504f7018c21STomi Valkeinen if (!info)
505f7018c21STomi Valkeinen return;
506f7018c21STomi Valkeinen
507f7018c21STomi Valkeinen par = info->par;
508f7018c21STomi Valkeinen recv_buf = (struct synthvid_msg *)par->recv_buf;
509f7018c21STomi Valkeinen
510f7018c21STomi Valkeinen do {
511f7018c21STomi Valkeinen ret = vmbus_recvpacket(hdev->channel, recv_buf,
512f7018c21STomi Valkeinen MAX_VMBUS_PKT_SIZE,
513f7018c21STomi Valkeinen &bytes_recvd, &req_id);
514f7018c21STomi Valkeinen if (bytes_recvd > 0 &&
515f7018c21STomi Valkeinen recv_buf->pipe_hdr.type == PIPE_MSG_DATA)
516f7018c21STomi Valkeinen synthvid_recv_sub(hdev);
517f7018c21STomi Valkeinen } while (bytes_recvd > 0 && ret == 0);
518f7018c21STomi Valkeinen }
519f7018c21STomi Valkeinen
52067e7cdb4SWei Hu /* Check if the ver1 version is equal or greater than ver2 */
synthvid_ver_ge(u32 ver1,u32 ver2)52167e7cdb4SWei Hu static inline bool synthvid_ver_ge(u32 ver1, u32 ver2)
52267e7cdb4SWei Hu {
52367e7cdb4SWei Hu if (SYNTHVID_VER_GET_MAJOR(ver1) > SYNTHVID_VER_GET_MAJOR(ver2) ||
52467e7cdb4SWei Hu (SYNTHVID_VER_GET_MAJOR(ver1) == SYNTHVID_VER_GET_MAJOR(ver2) &&
52567e7cdb4SWei Hu SYNTHVID_VER_GET_MINOR(ver1) >= SYNTHVID_VER_GET_MINOR(ver2)))
52667e7cdb4SWei Hu return true;
52767e7cdb4SWei Hu
52867e7cdb4SWei Hu return false;
52967e7cdb4SWei Hu }
53067e7cdb4SWei Hu
531f7018c21STomi Valkeinen /* Check synthetic video protocol version with the host */
synthvid_negotiate_ver(struct hv_device * hdev,u32 ver)532f7018c21STomi Valkeinen static int synthvid_negotiate_ver(struct hv_device *hdev, u32 ver)
533f7018c21STomi Valkeinen {
534f7018c21STomi Valkeinen struct fb_info *info = hv_get_drvdata(hdev);
535f7018c21STomi Valkeinen struct hvfb_par *par = info->par;
536f7018c21STomi Valkeinen struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
5377dea97e0SNicholas Mc Guire int ret = 0;
5387dea97e0SNicholas Mc Guire unsigned long t;
539f7018c21STomi Valkeinen
540f7018c21STomi Valkeinen memset(msg, 0, sizeof(struct synthvid_msg));
541f7018c21STomi Valkeinen msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST;
542f7018c21STomi Valkeinen msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
543f7018c21STomi Valkeinen sizeof(struct synthvid_version_req);
544f7018c21STomi Valkeinen msg->ver_req.version = ver;
545f7018c21STomi Valkeinen synthvid_send(hdev, msg);
546f7018c21STomi Valkeinen
547f7018c21STomi Valkeinen t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
548f7018c21STomi Valkeinen if (!t) {
549f7018c21STomi Valkeinen pr_err("Time out on waiting version response\n");
550f7018c21STomi Valkeinen ret = -ETIMEDOUT;
551f7018c21STomi Valkeinen goto out;
552f7018c21STomi Valkeinen }
553f7018c21STomi Valkeinen if (!msg->ver_resp.is_accepted) {
554f7018c21STomi Valkeinen ret = -ENODEV;
555f7018c21STomi Valkeinen goto out;
556f7018c21STomi Valkeinen }
557f7018c21STomi Valkeinen
558f7018c21STomi Valkeinen par->synthvid_version = ver;
55967e7cdb4SWei Hu pr_info("Synthvid Version major %d, minor %d\n",
56067e7cdb4SWei Hu SYNTHVID_VER_GET_MAJOR(ver), SYNTHVID_VER_GET_MINOR(ver));
56167e7cdb4SWei Hu
56267e7cdb4SWei Hu out:
56367e7cdb4SWei Hu return ret;
56467e7cdb4SWei Hu }
56567e7cdb4SWei Hu
56667e7cdb4SWei Hu /* Get current resolution from the host */
synthvid_get_supported_resolution(struct hv_device * hdev)56767e7cdb4SWei Hu static int synthvid_get_supported_resolution(struct hv_device *hdev)
56867e7cdb4SWei Hu {
56967e7cdb4SWei Hu struct fb_info *info = hv_get_drvdata(hdev);
57067e7cdb4SWei Hu struct hvfb_par *par = info->par;
57167e7cdb4SWei Hu struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
57267e7cdb4SWei Hu int ret = 0;
57367e7cdb4SWei Hu unsigned long t;
57467e7cdb4SWei Hu u8 index;
57567e7cdb4SWei Hu
57667e7cdb4SWei Hu memset(msg, 0, sizeof(struct synthvid_msg));
57767e7cdb4SWei Hu msg->vid_hdr.type = SYNTHVID_RESOLUTION_REQUEST;
57867e7cdb4SWei Hu msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
57967e7cdb4SWei Hu sizeof(struct synthvid_supported_resolution_req);
58067e7cdb4SWei Hu
58167e7cdb4SWei Hu msg->resolution_req.maximum_resolution_count =
58267e7cdb4SWei Hu SYNTHVID_MAX_RESOLUTION_COUNT;
58367e7cdb4SWei Hu synthvid_send(hdev, msg);
58467e7cdb4SWei Hu
58567e7cdb4SWei Hu t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
58667e7cdb4SWei Hu if (!t) {
58767e7cdb4SWei Hu pr_err("Time out on waiting resolution response\n");
58867e7cdb4SWei Hu ret = -ETIMEDOUT;
58967e7cdb4SWei Hu goto out;
59067e7cdb4SWei Hu }
59167e7cdb4SWei Hu
59267e7cdb4SWei Hu if (msg->resolution_resp.resolution_count == 0) {
59367e7cdb4SWei Hu pr_err("No supported resolutions\n");
59467e7cdb4SWei Hu ret = -ENODEV;
59567e7cdb4SWei Hu goto out;
59667e7cdb4SWei Hu }
59767e7cdb4SWei Hu
59867e7cdb4SWei Hu index = msg->resolution_resp.default_resolution_index;
59967e7cdb4SWei Hu if (index >= msg->resolution_resp.resolution_count) {
60067e7cdb4SWei Hu pr_err("Invalid resolution index: %d\n", index);
60167e7cdb4SWei Hu ret = -ENODEV;
60267e7cdb4SWei Hu goto out;
60367e7cdb4SWei Hu }
60467e7cdb4SWei Hu
60567e7cdb4SWei Hu screen_width =
60667e7cdb4SWei Hu msg->resolution_resp.supported_resolution[index].width;
60767e7cdb4SWei Hu screen_height =
60867e7cdb4SWei Hu msg->resolution_resp.supported_resolution[index].height;
609f7018c21STomi Valkeinen
610f7018c21STomi Valkeinen out:
611f7018c21STomi Valkeinen return ret;
612f7018c21STomi Valkeinen }
613f7018c21STomi Valkeinen
614f7018c21STomi Valkeinen /* Connect to VSP (Virtual Service Provider) on host */
synthvid_connect_vsp(struct hv_device * hdev)615f7018c21STomi Valkeinen static int synthvid_connect_vsp(struct hv_device *hdev)
616f7018c21STomi Valkeinen {
617f7018c21STomi Valkeinen struct fb_info *info = hv_get_drvdata(hdev);
618f7018c21STomi Valkeinen struct hvfb_par *par = info->par;
619f7018c21STomi Valkeinen int ret;
620f7018c21STomi Valkeinen
621f7018c21STomi Valkeinen ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE,
622f7018c21STomi Valkeinen NULL, 0, synthvid_receive, hdev);
623f7018c21STomi Valkeinen if (ret) {
624f7018c21STomi Valkeinen pr_err("Unable to open vmbus channel\n");
625f7018c21STomi Valkeinen return ret;
626f7018c21STomi Valkeinen }
627f7018c21STomi Valkeinen
628f7018c21STomi Valkeinen /* Negotiate the protocol version with host */
62967e7cdb4SWei Hu switch (vmbus_proto_version) {
63067e7cdb4SWei Hu case VERSION_WIN10:
63167e7cdb4SWei Hu case VERSION_WIN10_V5:
63267e7cdb4SWei Hu ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
63367e7cdb4SWei Hu if (!ret)
63467e7cdb4SWei Hu break;
635df561f66SGustavo A. R. Silva fallthrough;
63667e7cdb4SWei Hu case VERSION_WIN8:
63767e7cdb4SWei Hu case VERSION_WIN8_1:
638f7018c21STomi Valkeinen ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8);
63967e7cdb4SWei Hu break;
64067e7cdb4SWei Hu default:
64167e7cdb4SWei Hu ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
64267e7cdb4SWei Hu break;
64367e7cdb4SWei Hu }
644f7018c21STomi Valkeinen
645f7018c21STomi Valkeinen if (ret) {
646f7018c21STomi Valkeinen pr_err("Synthetic video device version not accepted\n");
647f7018c21STomi Valkeinen goto error;
648f7018c21STomi Valkeinen }
649f7018c21STomi Valkeinen
650f7018c21STomi Valkeinen screen_depth = SYNTHVID_DEPTH_WIN8;
65167e7cdb4SWei Hu if (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10)) {
65267e7cdb4SWei Hu ret = synthvid_get_supported_resolution(hdev);
65367e7cdb4SWei Hu if (ret)
65467e7cdb4SWei Hu pr_info("Failed to get supported resolution from host, use default\n");
65567e7cdb4SWei Hu }
65667e7cdb4SWei Hu
657f7018c21STomi Valkeinen screen_fb_size = hdev->channel->offermsg.offer.
658f7018c21STomi Valkeinen mmio_megabytes * 1024 * 1024;
659f7018c21STomi Valkeinen
660f7018c21STomi Valkeinen return 0;
661f7018c21STomi Valkeinen
662f7018c21STomi Valkeinen error:
663f7018c21STomi Valkeinen vmbus_close(hdev->channel);
664f7018c21STomi Valkeinen return ret;
665f7018c21STomi Valkeinen }
666f7018c21STomi Valkeinen
667f7018c21STomi Valkeinen /* Send VRAM and Situation messages to the host */
synthvid_send_config(struct hv_device * hdev)668f7018c21STomi Valkeinen static int synthvid_send_config(struct hv_device *hdev)
669f7018c21STomi Valkeinen {
670f7018c21STomi Valkeinen struct fb_info *info = hv_get_drvdata(hdev);
671f7018c21STomi Valkeinen struct hvfb_par *par = info->par;
672f7018c21STomi Valkeinen struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
6737dea97e0SNicholas Mc Guire int ret = 0;
6747dea97e0SNicholas Mc Guire unsigned long t;
675f7018c21STomi Valkeinen
676f7018c21STomi Valkeinen /* Send VRAM location */
677f7018c21STomi Valkeinen memset(msg, 0, sizeof(struct synthvid_msg));
678f7018c21STomi Valkeinen msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION;
679f7018c21STomi Valkeinen msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
680f7018c21STomi Valkeinen sizeof(struct synthvid_vram_location);
681d21987d7SWei Hu msg->vram.user_ctx = msg->vram.vram_gpa = par->mmio_pp;
682f7018c21STomi Valkeinen msg->vram.is_vram_gpa_specified = 1;
683f7018c21STomi Valkeinen synthvid_send(hdev, msg);
684f7018c21STomi Valkeinen
685f7018c21STomi Valkeinen t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
686f7018c21STomi Valkeinen if (!t) {
687f7018c21STomi Valkeinen pr_err("Time out on waiting vram location ack\n");
688f7018c21STomi Valkeinen ret = -ETIMEDOUT;
689f7018c21STomi Valkeinen goto out;
690f7018c21STomi Valkeinen }
691d21987d7SWei Hu if (msg->vram_ack.user_ctx != par->mmio_pp) {
692f7018c21STomi Valkeinen pr_err("Unable to set VRAM location\n");
693f7018c21STomi Valkeinen ret = -ENODEV;
694f7018c21STomi Valkeinen goto out;
695f7018c21STomi Valkeinen }
696f7018c21STomi Valkeinen
697f7018c21STomi Valkeinen /* Send pointer and situation update */
698f7018c21STomi Valkeinen synthvid_send_ptr(hdev);
699f7018c21STomi Valkeinen synthvid_send_situ(hdev);
700f7018c21STomi Valkeinen
701f7018c21STomi Valkeinen out:
702f7018c21STomi Valkeinen return ret;
703f7018c21STomi Valkeinen }
704f7018c21STomi Valkeinen
705f7018c21STomi Valkeinen
706f7018c21STomi Valkeinen /*
707f7018c21STomi Valkeinen * Delayed work callback:
708d21987d7SWei Hu * It is scheduled to call whenever update request is received and it has
709d21987d7SWei Hu * not been called in last HVFB_ONDEMAND_THROTTLE time interval.
710f7018c21STomi Valkeinen */
hvfb_update_work(struct work_struct * w)711f7018c21STomi Valkeinen static void hvfb_update_work(struct work_struct *w)
712f7018c21STomi Valkeinen {
713f7018c21STomi Valkeinen struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work);
714f7018c21STomi Valkeinen struct fb_info *info = par->info;
715d21987d7SWei Hu unsigned long flags;
716d21987d7SWei Hu int x1, x2, y1, y2;
717d21987d7SWei Hu int j;
718f7018c21STomi Valkeinen
719d21987d7SWei Hu spin_lock_irqsave(&par->delayed_refresh_lock, flags);
720d21987d7SWei Hu /* Reset the request flag */
721d21987d7SWei Hu par->delayed_refresh = false;
722f7018c21STomi Valkeinen
723d21987d7SWei Hu /* Store the dirty rectangle to local variables */
724d21987d7SWei Hu x1 = par->x1;
725d21987d7SWei Hu x2 = par->x2;
726d21987d7SWei Hu y1 = par->y1;
727d21987d7SWei Hu y2 = par->y2;
728d21987d7SWei Hu
729d21987d7SWei Hu /* Clear dirty rectangle */
730d21987d7SWei Hu par->x1 = par->y1 = INT_MAX;
731d21987d7SWei Hu par->x2 = par->y2 = 0;
732d21987d7SWei Hu
733d21987d7SWei Hu spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
734d21987d7SWei Hu
735d21987d7SWei Hu if (x1 > info->var.xres || x2 > info->var.xres ||
736d21987d7SWei Hu y1 > info->var.yres || y2 > info->var.yres || x2 <= x1)
737d21987d7SWei Hu return;
738d21987d7SWei Hu
739d21987d7SWei Hu /* Copy the dirty rectangle to frame buffer memory */
7403a6fb6c4SWei Hu if (par->need_docopy)
7413a6fb6c4SWei Hu for (j = y1; j < y2; j++)
742d21987d7SWei Hu hvfb_docopy(par,
743d21987d7SWei Hu j * info->fix.line_length +
744d21987d7SWei Hu (x1 * screen_depth / 8),
745d21987d7SWei Hu (x2 - x1) * screen_depth / 8);
746d21987d7SWei Hu
747d21987d7SWei Hu /* Refresh */
748d21987d7SWei Hu if (par->fb_ready && par->update)
749d21987d7SWei Hu synthvid_update(info, x1, y1, x2, y2);
750d21987d7SWei Hu }
751d21987d7SWei Hu
752d21987d7SWei Hu /*
753d21987d7SWei Hu * Control the on-demand refresh frequency. It schedules a delayed
754d21987d7SWei Hu * screen update if it has not yet.
755d21987d7SWei Hu */
hvfb_ondemand_refresh_throttle(struct hvfb_par * par,int x1,int y1,int w,int h)756d21987d7SWei Hu static void hvfb_ondemand_refresh_throttle(struct hvfb_par *par,
757d21987d7SWei Hu int x1, int y1, int w, int h)
758d21987d7SWei Hu {
759d21987d7SWei Hu unsigned long flags;
760d21987d7SWei Hu int x2 = x1 + w;
761d21987d7SWei Hu int y2 = y1 + h;
762d21987d7SWei Hu
763d21987d7SWei Hu spin_lock_irqsave(&par->delayed_refresh_lock, flags);
764d21987d7SWei Hu
765d21987d7SWei Hu /* Merge dirty rectangle */
766d21987d7SWei Hu par->x1 = min_t(int, par->x1, x1);
767d21987d7SWei Hu par->y1 = min_t(int, par->y1, y1);
768d21987d7SWei Hu par->x2 = max_t(int, par->x2, x2);
769d21987d7SWei Hu par->y2 = max_t(int, par->y2, y2);
770d21987d7SWei Hu
771d21987d7SWei Hu /* Schedule a delayed screen update if not yet */
772d21987d7SWei Hu if (par->delayed_refresh == false) {
773d21987d7SWei Hu schedule_delayed_work(&par->dwork,
774d21987d7SWei Hu HVFB_ONDEMAND_THROTTLE);
775d21987d7SWei Hu par->delayed_refresh = true;
776d21987d7SWei Hu }
777d21987d7SWei Hu
778d21987d7SWei Hu spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
779f7018c21STomi Valkeinen }
780f7018c21STomi Valkeinen
hvfb_on_panic(struct notifier_block * nb,unsigned long e,void * p)7813686fe96SDexuan Cui static int hvfb_on_panic(struct notifier_block *nb,
7823686fe96SDexuan Cui unsigned long e, void *p)
7833686fe96SDexuan Cui {
7841d044ca0SGuilherme G. Piccoli struct hv_device *hdev;
7853686fe96SDexuan Cui struct hvfb_par *par;
7863686fe96SDexuan Cui struct fb_info *info;
7873686fe96SDexuan Cui
7883686fe96SDexuan Cui par = container_of(nb, struct hvfb_par, hvfb_panic_nb);
7893686fe96SDexuan Cui info = par->info;
7901d044ca0SGuilherme G. Piccoli hdev = device_to_hv_device(info->device);
7911d044ca0SGuilherme G. Piccoli
7921d044ca0SGuilherme G. Piccoli if (hv_ringbuffer_spinlock_busy(hdev->channel))
7931d044ca0SGuilherme G. Piccoli return NOTIFY_DONE;
7941d044ca0SGuilherme G. Piccoli
7951d044ca0SGuilherme G. Piccoli par->synchronous_fb = true;
7963a6fb6c4SWei Hu if (par->need_docopy)
797d21987d7SWei Hu hvfb_docopy(par, 0, dio_fb_size);
798d21987d7SWei Hu synthvid_update(info, 0, 0, INT_MAX, INT_MAX);
7993686fe96SDexuan Cui
8003686fe96SDexuan Cui return NOTIFY_DONE;
8013686fe96SDexuan Cui }
802f7018c21STomi Valkeinen
803f7018c21STomi Valkeinen /* Framebuffer operation handlers */
804f7018c21STomi Valkeinen
hvfb_check_var(struct fb_var_screeninfo * var,struct fb_info * info)805f7018c21STomi Valkeinen static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
806f7018c21STomi Valkeinen {
807f7018c21STomi Valkeinen if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN ||
808f7018c21STomi Valkeinen var->xres > screen_width || var->yres > screen_height ||
809f7018c21STomi Valkeinen var->bits_per_pixel != screen_depth)
810f7018c21STomi Valkeinen return -EINVAL;
811f7018c21STomi Valkeinen
812f7018c21STomi Valkeinen var->xres_virtual = var->xres;
813f7018c21STomi Valkeinen var->yres_virtual = var->yres;
814f7018c21STomi Valkeinen
815f7018c21STomi Valkeinen return 0;
816f7018c21STomi Valkeinen }
817f7018c21STomi Valkeinen
hvfb_set_par(struct fb_info * info)818f7018c21STomi Valkeinen static int hvfb_set_par(struct fb_info *info)
819f7018c21STomi Valkeinen {
820f7018c21STomi Valkeinen struct hv_device *hdev = device_to_hv_device(info->device);
821f7018c21STomi Valkeinen
822f7018c21STomi Valkeinen return synthvid_send_situ(hdev);
823f7018c21STomi Valkeinen }
824f7018c21STomi Valkeinen
825f7018c21STomi Valkeinen
chan_to_field(u32 chan,struct fb_bitfield * bf)826f7018c21STomi Valkeinen static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf)
827f7018c21STomi Valkeinen {
828f7018c21STomi Valkeinen return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset;
829f7018c21STomi Valkeinen }
830f7018c21STomi Valkeinen
hvfb_setcolreg(unsigned regno,unsigned red,unsigned green,unsigned blue,unsigned transp,struct fb_info * info)831f7018c21STomi Valkeinen static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
832f7018c21STomi Valkeinen unsigned blue, unsigned transp, struct fb_info *info)
833f7018c21STomi Valkeinen {
834f7018c21STomi Valkeinen u32 *pal = info->pseudo_palette;
835f7018c21STomi Valkeinen
836f7018c21STomi Valkeinen if (regno > 15)
837f7018c21STomi Valkeinen return -EINVAL;
838f7018c21STomi Valkeinen
839f7018c21STomi Valkeinen pal[regno] = chan_to_field(red, &info->var.red)
840f7018c21STomi Valkeinen | chan_to_field(green, &info->var.green)
841f7018c21STomi Valkeinen | chan_to_field(blue, &info->var.blue)
842f7018c21STomi Valkeinen | chan_to_field(transp, &info->var.transp);
843f7018c21STomi Valkeinen
844f7018c21STomi Valkeinen return 0;
845f7018c21STomi Valkeinen }
846f7018c21STomi Valkeinen
hvfb_blank(int blank,struct fb_info * info)847f7018c21STomi Valkeinen static int hvfb_blank(int blank, struct fb_info *info)
848f7018c21STomi Valkeinen {
849f7018c21STomi Valkeinen return 1; /* get fb_blank to set the colormap to all black */
850f7018c21STomi Valkeinen }
851f7018c21STomi Valkeinen
hvfb_ops_damage_range(struct fb_info * info,off_t off,size_t len)85266a749a7SThomas Zimmermann static void hvfb_ops_damage_range(struct fb_info *info, off_t off, size_t len)
8533686fe96SDexuan Cui {
85466a749a7SThomas Zimmermann /* TODO: implement damage handling */
8553686fe96SDexuan Cui }
8563686fe96SDexuan Cui
hvfb_ops_damage_area(struct fb_info * info,u32 x,u32 y,u32 width,u32 height)85766a749a7SThomas Zimmermann static void hvfb_ops_damage_area(struct fb_info *info, u32 x, u32 y, u32 width, u32 height)
8583686fe96SDexuan Cui {
85966a749a7SThomas Zimmermann struct hvfb_par *par = info->par;
8603686fe96SDexuan Cui
8613686fe96SDexuan Cui if (par->synchronous_fb)
86266a749a7SThomas Zimmermann synthvid_update(info, 0, 0, INT_MAX, INT_MAX);
863d21987d7SWei Hu else
86466a749a7SThomas Zimmermann hvfb_ondemand_refresh_throttle(par, x, y, width, height);
8653686fe96SDexuan Cui }
8663686fe96SDexuan Cui
86766a749a7SThomas Zimmermann /*
868*ea2f45abSSaurabh Sengar * fb_ops.fb_destroy is called by the last put_fb_info() call at the end
869*ea2f45abSSaurabh Sengar * of unregister_framebuffer() or fb_release(). Do any cleanup related to
870*ea2f45abSSaurabh Sengar * framebuffer here.
871*ea2f45abSSaurabh Sengar */
hvfb_destroy(struct fb_info * info)872*ea2f45abSSaurabh Sengar static void hvfb_destroy(struct fb_info *info)
873*ea2f45abSSaurabh Sengar {
874*ea2f45abSSaurabh Sengar hvfb_putmem(info);
875*ea2f45abSSaurabh Sengar framebuffer_release(info);
876*ea2f45abSSaurabh Sengar }
877*ea2f45abSSaurabh Sengar
878*ea2f45abSSaurabh Sengar /*
87966a749a7SThomas Zimmermann * TODO: GEN1 codepaths allocate from system or DMA-able memory. Fix the
88066a749a7SThomas Zimmermann * driver to use the _SYSMEM_ or _DMAMEM_ helpers in these cases.
88166a749a7SThomas Zimmermann */
88266a749a7SThomas Zimmermann FB_GEN_DEFAULT_DEFERRED_IOMEM_OPS(hvfb_ops,
88366a749a7SThomas Zimmermann hvfb_ops_damage_range,
88466a749a7SThomas Zimmermann hvfb_ops_damage_area)
8853686fe96SDexuan Cui
8868a48ac33SJani Nikula static const struct fb_ops hvfb_ops = {
887f7018c21STomi Valkeinen .owner = THIS_MODULE,
88866a749a7SThomas Zimmermann FB_DEFAULT_DEFERRED_OPS(hvfb_ops),
889f7018c21STomi Valkeinen .fb_check_var = hvfb_check_var,
890f7018c21STomi Valkeinen .fb_set_par = hvfb_set_par,
891f7018c21STomi Valkeinen .fb_setcolreg = hvfb_setcolreg,
892f7018c21STomi Valkeinen .fb_blank = hvfb_blank,
893*ea2f45abSSaurabh Sengar .fb_destroy = hvfb_destroy,
894f7018c21STomi Valkeinen };
895f7018c21STomi Valkeinen
896f7018c21STomi Valkeinen /* Get options from kernel paramenter "video=" */
hvfb_get_option(struct fb_info * info)897f7018c21STomi Valkeinen static void hvfb_get_option(struct fb_info *info)
898f7018c21STomi Valkeinen {
899f7018c21STomi Valkeinen struct hvfb_par *par = info->par;
900f7018c21STomi Valkeinen char *opt = NULL, *p;
901f7018c21STomi Valkeinen uint x = 0, y = 0;
902f7018c21STomi Valkeinen
903f7018c21STomi Valkeinen if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt)
904f7018c21STomi Valkeinen return;
905f7018c21STomi Valkeinen
906f7018c21STomi Valkeinen p = strsep(&opt, "x");
907f7018c21STomi Valkeinen if (!*p || kstrtouint(p, 0, &x) ||
908f7018c21STomi Valkeinen !opt || !*opt || kstrtouint(opt, 0, &y)) {
909f7018c21STomi Valkeinen pr_err("Screen option is invalid: skipped\n");
910f7018c21STomi Valkeinen return;
911f7018c21STomi Valkeinen }
912f7018c21STomi Valkeinen
913f7018c21STomi Valkeinen if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN ||
91467e7cdb4SWei Hu (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10) &&
9159ff5549bSMichael Kelley (x * y * screen_depth / 8 > screen_fb_size)) ||
916f7018c21STomi Valkeinen (par->synthvid_version == SYNTHVID_VERSION_WIN8 &&
917b0cce4f6SMichael Kelley x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8)) {
918f7018c21STomi Valkeinen pr_err("Screen resolution option is out of range: skipped\n");
919f7018c21STomi Valkeinen return;
920f7018c21STomi Valkeinen }
921f7018c21STomi Valkeinen
922f7018c21STomi Valkeinen screen_width = x;
923f7018c21STomi Valkeinen screen_height = y;
924f7018c21STomi Valkeinen return;
925f7018c21STomi Valkeinen }
926f7018c21STomi Valkeinen
9273a6fb6c4SWei Hu /*
9283a6fb6c4SWei Hu * Allocate enough contiguous physical memory.
9293a6fb6c4SWei Hu * Return physical address if succeeded or -1 if failed.
9303a6fb6c4SWei Hu */
hvfb_get_phymem(struct hv_device * hdev,unsigned int request_size)9313a6fb6c4SWei Hu static phys_addr_t hvfb_get_phymem(struct hv_device *hdev,
9323a6fb6c4SWei Hu unsigned int request_size)
9333a6fb6c4SWei Hu {
9343a6fb6c4SWei Hu struct page *page = NULL;
9353a6fb6c4SWei Hu dma_addr_t dma_handle;
9363a6fb6c4SWei Hu void *vmem;
9373a6fb6c4SWei Hu phys_addr_t paddr = 0;
9383a6fb6c4SWei Hu unsigned int order = get_order(request_size);
9393a6fb6c4SWei Hu
9403a6fb6c4SWei Hu if (request_size == 0)
9413a6fb6c4SWei Hu return -1;
9423a6fb6c4SWei Hu
9435e0a760bSKirill A. Shutemov if (order <= MAX_PAGE_ORDER) {
9445e0a760bSKirill A. Shutemov /* Call alloc_pages if the size is less than 2^MAX_PAGE_ORDER */
9453a6fb6c4SWei Hu page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
9463a6fb6c4SWei Hu if (!page)
9473a6fb6c4SWei Hu return -1;
9483a6fb6c4SWei Hu
9493a6fb6c4SWei Hu paddr = (page_to_pfn(page) << PAGE_SHIFT);
9503a6fb6c4SWei Hu } else {
9513a6fb6c4SWei Hu /* Allocate from CMA */
9523a6fb6c4SWei Hu hdev->device.coherent_dma_mask = DMA_BIT_MASK(64);
9533a6fb6c4SWei Hu
9543a6fb6c4SWei Hu vmem = dma_alloc_coherent(&hdev->device,
9553a6fb6c4SWei Hu round_up(request_size, PAGE_SIZE),
9563a6fb6c4SWei Hu &dma_handle,
9573a6fb6c4SWei Hu GFP_KERNEL | __GFP_NOWARN);
9583a6fb6c4SWei Hu
9593a6fb6c4SWei Hu if (!vmem)
9603a6fb6c4SWei Hu return -1;
9613a6fb6c4SWei Hu
9623a6fb6c4SWei Hu paddr = virt_to_phys(vmem);
9633a6fb6c4SWei Hu }
9643a6fb6c4SWei Hu
9653a6fb6c4SWei Hu return paddr;
9663a6fb6c4SWei Hu }
9673a6fb6c4SWei Hu
9683a6fb6c4SWei Hu /* Release contiguous physical memory */
hvfb_release_phymem(struct device * device,phys_addr_t paddr,unsigned int size)969f5e728a5SSaurabh Sengar static void hvfb_release_phymem(struct device *device,
9703a6fb6c4SWei Hu phys_addr_t paddr, unsigned int size)
9713a6fb6c4SWei Hu {
9723a6fb6c4SWei Hu unsigned int order = get_order(size);
9733a6fb6c4SWei Hu
9745e0a760bSKirill A. Shutemov if (order <= MAX_PAGE_ORDER)
9753a6fb6c4SWei Hu __free_pages(pfn_to_page(paddr >> PAGE_SHIFT), order);
9763a6fb6c4SWei Hu else
977f5e728a5SSaurabh Sengar dma_free_coherent(device,
9783a6fb6c4SWei Hu round_up(size, PAGE_SIZE),
9793a6fb6c4SWei Hu phys_to_virt(paddr),
9803a6fb6c4SWei Hu paddr);
9813a6fb6c4SWei Hu }
9823a6fb6c4SWei Hu
983f7018c21STomi Valkeinen
984f7018c21STomi Valkeinen /* Get framebuffer memory from Hyper-V video pci space */
hvfb_getmem(struct hv_device * hdev,struct fb_info * info)98535464483SJake Oshins static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info)
986f7018c21STomi Valkeinen {
987f7018c21STomi Valkeinen struct hvfb_par *par = info->par;
988f7018c21STomi Valkeinen struct pci_dev *pdev = NULL;
989f7018c21STomi Valkeinen void __iomem *fb_virt;
990f7018c21STomi Valkeinen int gen2vm = efi_enabled(EFI_BOOT);
9910aa0838cSThomas Zimmermann resource_size_t base = 0;
9920aa0838cSThomas Zimmermann resource_size_t size = 0;
9933a6fb6c4SWei Hu phys_addr_t paddr;
994f7018c21STomi Valkeinen int ret;
995f7018c21STomi Valkeinen
9963a6fb6c4SWei Hu if (!gen2vm) {
9973a6fb6c4SWei Hu pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
9983a6fb6c4SWei Hu PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
9993a6fb6c4SWei Hu if (!pdev) {
10003a6fb6c4SWei Hu pr_err("Unable to find PCI Hyper-V video\n");
10013a6fb6c4SWei Hu return -ENODEV;
10023a6fb6c4SWei Hu }
10033a6fb6c4SWei Hu
100481d23934SThomas Zimmermann base = pci_resource_start(pdev, 0);
100581d23934SThomas Zimmermann size = pci_resource_len(pdev, 0);
100630438637SMichael Kelley aperture_remove_conflicting_devices(base, size, KBUILD_MODNAME);
10073a6fb6c4SWei Hu
10083a6fb6c4SWei Hu /*
10093a6fb6c4SWei Hu * For Gen 1 VM, we can directly use the contiguous memory
10103a6fb6c4SWei Hu * from VM. If we succeed, deferred IO happens directly
10113a6fb6c4SWei Hu * on this allocated framebuffer memory, avoiding extra
10123a6fb6c4SWei Hu * memory copy.
10133a6fb6c4SWei Hu */
10143a6fb6c4SWei Hu paddr = hvfb_get_phymem(hdev, screen_fb_size);
10153a6fb6c4SWei Hu if (paddr != (phys_addr_t) -1) {
10163a6fb6c4SWei Hu par->mmio_pp = paddr;
10173a6fb6c4SWei Hu par->mmio_vp = par->dio_vp = __va(paddr);
10183a6fb6c4SWei Hu
10193a6fb6c4SWei Hu info->fix.smem_start = paddr;
10203a6fb6c4SWei Hu info->fix.smem_len = screen_fb_size;
10213a6fb6c4SWei Hu info->screen_base = par->mmio_vp;
10223a6fb6c4SWei Hu info->screen_size = screen_fb_size;
10233a6fb6c4SWei Hu
10243a6fb6c4SWei Hu par->need_docopy = false;
10253a6fb6c4SWei Hu goto getmem_done;
10263a6fb6c4SWei Hu }
10273a6fb6c4SWei Hu pr_info("Unable to allocate enough contiguous physical memory on Gen 1 VM. Using MMIO instead.\n");
102830438637SMichael Kelley } else {
102930438637SMichael Kelley aperture_remove_all_conflicting_devices(KBUILD_MODNAME);
10303a6fb6c4SWei Hu }
10313a6fb6c4SWei Hu
10323a6fb6c4SWei Hu /*
103330438637SMichael Kelley * Cannot use contiguous physical memory, so allocate MMIO space for
103430438637SMichael Kelley * the framebuffer. At this point in the function, conflicting devices
103530438637SMichael Kelley * that might have claimed the framebuffer MMIO space based on
103630438637SMichael Kelley * screen_info.lfb_base must have already been removed so that
103730438637SMichael Kelley * vmbus_allocate_mmio() does not allocate different MMIO space. If the
103830438637SMichael Kelley * kdump image were to be loaded using kexec_file_load(), the
103930438637SMichael Kelley * framebuffer location in the kdump image would be set from
104030438637SMichael Kelley * screen_info.lfb_base at the time that kdump is enabled. If the
104130438637SMichael Kelley * framebuffer has moved elsewhere, this could be the wrong location,
104230438637SMichael Kelley * causing kdump to hang when efifb (for example) loads.
10433a6fb6c4SWei Hu */
1044d21987d7SWei Hu dio_fb_size =
1045d21987d7SWei Hu screen_width * screen_height * screen_depth / 8;
1046d21987d7SWei Hu
1047c4b4d704SSaurabh Sengar ret = vmbus_allocate_mmio(&par->mem, hdev, 0, -1,
104835464483SJake Oshins screen_fb_size, 0x100000, true);
104935464483SJake Oshins if (ret != 0) {
105035464483SJake Oshins pr_err("Unable to allocate framebuffer memory\n");
105135464483SJake Oshins goto err1;
105235464483SJake Oshins }
105335464483SJake Oshins
10545f1251a4SDexuan Cui /*
10555f1251a4SDexuan Cui * Map the VRAM cacheable for performance. This is also required for
10565f1251a4SDexuan Cui * VM Connect to display properly for ARM64 Linux VM, as the host also
10575f1251a4SDexuan Cui * maps the VRAM cacheable.
10585f1251a4SDexuan Cui */
10595f1251a4SDexuan Cui fb_virt = ioremap_cache(par->mem->start, screen_fb_size);
1060f7018c21STomi Valkeinen if (!fb_virt)
1061f7018c21STomi Valkeinen goto err2;
1062f7018c21STomi Valkeinen
1063d21987d7SWei Hu /* Allocate memory for deferred IO */
1064d21987d7SWei Hu par->dio_vp = vzalloc(round_up(dio_fb_size, PAGE_SIZE));
1065d21987d7SWei Hu if (par->dio_vp == NULL)
1066d21987d7SWei Hu goto err3;
1067d21987d7SWei Hu
1068d21987d7SWei Hu /* Physical address of FB device */
1069d21987d7SWei Hu par->mmio_pp = par->mem->start;
1070d21987d7SWei Hu /* Virtual address of FB device */
1071d21987d7SWei Hu par->mmio_vp = (unsigned char *) fb_virt;
1072d21987d7SWei Hu
107335464483SJake Oshins info->fix.smem_start = par->mem->start;
1074d21987d7SWei Hu info->fix.smem_len = dio_fb_size;
1075d21987d7SWei Hu info->screen_base = par->dio_vp;
1076d21987d7SWei Hu info->screen_size = dio_fb_size;
1077f7018c21STomi Valkeinen
10783a6fb6c4SWei Hu getmem_done:
1079c25a19afSThomas Zimmermann if (!gen2vm)
1080a07b50d8SArnd Bergmann pci_dev_put(pdev);
1081f7018c21STomi Valkeinen
1082f7018c21STomi Valkeinen return 0;
1083f7018c21STomi Valkeinen
1084f7018c21STomi Valkeinen err3:
1085f7018c21STomi Valkeinen iounmap(fb_virt);
1086f7018c21STomi Valkeinen err2:
1087696ca5e8SJake Oshins vmbus_free_mmio(par->mem->start, screen_fb_size);
108835464483SJake Oshins par->mem = NULL;
1089f7018c21STomi Valkeinen err1:
1090f7018c21STomi Valkeinen if (!gen2vm)
1091f7018c21STomi Valkeinen pci_dev_put(pdev);
1092f7018c21STomi Valkeinen
1093f7018c21STomi Valkeinen return -ENOMEM;
1094f7018c21STomi Valkeinen }
1095f7018c21STomi Valkeinen
1096f7018c21STomi Valkeinen /* Release the framebuffer */
hvfb_putmem(struct fb_info * info)1097f5e728a5SSaurabh Sengar static void hvfb_putmem(struct fb_info *info)
1098f7018c21STomi Valkeinen {
1099f7018c21STomi Valkeinen struct hvfb_par *par = info->par;
1100f7018c21STomi Valkeinen
11013a6fb6c4SWei Hu if (par->need_docopy) {
1102d21987d7SWei Hu vfree(par->dio_vp);
11037241c886SMichael Kelley iounmap(par->mmio_vp);
1104696ca5e8SJake Oshins vmbus_free_mmio(par->mem->start, screen_fb_size);
11053a6fb6c4SWei Hu } else {
1106f5e728a5SSaurabh Sengar hvfb_release_phymem(info->device, info->fix.smem_start,
11073a6fb6c4SWei Hu screen_fb_size);
11083a6fb6c4SWei Hu }
11093a6fb6c4SWei Hu
111035464483SJake Oshins par->mem = NULL;
1111f7018c21STomi Valkeinen }
1112f7018c21STomi Valkeinen
1113f7018c21STomi Valkeinen
hvfb_probe(struct hv_device * hdev,const struct hv_vmbus_device_id * dev_id)1114f7018c21STomi Valkeinen static int hvfb_probe(struct hv_device *hdev,
1115f7018c21STomi Valkeinen const struct hv_vmbus_device_id *dev_id)
1116f7018c21STomi Valkeinen {
1117f7018c21STomi Valkeinen struct fb_info *info;
1118f7018c21STomi Valkeinen struct hvfb_par *par;
1119f7018c21STomi Valkeinen int ret;
1120f7018c21STomi Valkeinen
1121f7018c21STomi Valkeinen info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device);
11220adcdbcbSBartlomiej Zolnierkiewicz if (!info)
1123f7018c21STomi Valkeinen return -ENOMEM;
1124f7018c21STomi Valkeinen
1125f7018c21STomi Valkeinen par = info->par;
1126f7018c21STomi Valkeinen par->info = info;
1127f7018c21STomi Valkeinen par->fb_ready = false;
11283a6fb6c4SWei Hu par->need_docopy = true;
1129f7018c21STomi Valkeinen init_completion(&par->wait);
1130f7018c21STomi Valkeinen INIT_DELAYED_WORK(&par->dwork, hvfb_update_work);
1131f7018c21STomi Valkeinen
1132d21987d7SWei Hu par->delayed_refresh = false;
1133d21987d7SWei Hu spin_lock_init(&par->delayed_refresh_lock);
1134d21987d7SWei Hu par->x1 = par->y1 = INT_MAX;
1135d21987d7SWei Hu par->x2 = par->y2 = 0;
1136d21987d7SWei Hu
1137f7018c21STomi Valkeinen /* Connect to VSP */
1138f7018c21STomi Valkeinen hv_set_drvdata(hdev, info);
1139f7018c21STomi Valkeinen ret = synthvid_connect_vsp(hdev);
1140f7018c21STomi Valkeinen if (ret) {
1141f7018c21STomi Valkeinen pr_err("Unable to connect to VSP\n");
1142f7018c21STomi Valkeinen goto error1;
1143f7018c21STomi Valkeinen }
1144f7018c21STomi Valkeinen
114567e7cdb4SWei Hu hvfb_get_option(info);
11469ff5549bSMichael Kelley pr_info("Screen resolution: %dx%d, Color depth: %d, Frame buffer size: %d\n",
11479ff5549bSMichael Kelley screen_width, screen_height, screen_depth, screen_fb_size);
114867e7cdb4SWei Hu
114935464483SJake Oshins ret = hvfb_getmem(hdev, info);
1150f7018c21STomi Valkeinen if (ret) {
1151f7018c21STomi Valkeinen pr_err("No memory for framebuffer\n");
1152f7018c21STomi Valkeinen goto error2;
1153f7018c21STomi Valkeinen }
1154f7018c21STomi Valkeinen
1155f7018c21STomi Valkeinen /* Set up fb_info */
1156f7018c21STomi Valkeinen info->var.xres_virtual = info->var.xres = screen_width;
1157f7018c21STomi Valkeinen info->var.yres_virtual = info->var.yres = screen_height;
1158f7018c21STomi Valkeinen info->var.bits_per_pixel = screen_depth;
1159f7018c21STomi Valkeinen
1160f7018c21STomi Valkeinen if (info->var.bits_per_pixel == 16) {
1161f7018c21STomi Valkeinen info->var.red = (struct fb_bitfield){11, 5, 0};
1162f7018c21STomi Valkeinen info->var.green = (struct fb_bitfield){5, 6, 0};
1163f7018c21STomi Valkeinen info->var.blue = (struct fb_bitfield){0, 5, 0};
1164f7018c21STomi Valkeinen info->var.transp = (struct fb_bitfield){0, 0, 0};
1165f7018c21STomi Valkeinen } else {
1166f7018c21STomi Valkeinen info->var.red = (struct fb_bitfield){16, 8, 0};
1167f7018c21STomi Valkeinen info->var.green = (struct fb_bitfield){8, 8, 0};
1168f7018c21STomi Valkeinen info->var.blue = (struct fb_bitfield){0, 8, 0};
1169f7018c21STomi Valkeinen info->var.transp = (struct fb_bitfield){24, 8, 0};
1170f7018c21STomi Valkeinen }
1171f7018c21STomi Valkeinen
1172f7018c21STomi Valkeinen info->var.activate = FB_ACTIVATE_NOW;
1173f7018c21STomi Valkeinen info->var.height = -1;
1174f7018c21STomi Valkeinen info->var.width = -1;
1175f7018c21STomi Valkeinen info->var.vmode = FB_VMODE_NONINTERLACED;
1176f7018c21STomi Valkeinen
1177f7018c21STomi Valkeinen strcpy(info->fix.id, KBUILD_MODNAME);
1178f7018c21STomi Valkeinen info->fix.type = FB_TYPE_PACKED_PIXELS;
1179f7018c21STomi Valkeinen info->fix.visual = FB_VISUAL_TRUECOLOR;
1180f7018c21STomi Valkeinen info->fix.line_length = screen_width * screen_depth / 8;
1181f7018c21STomi Valkeinen info->fix.accel = FB_ACCEL_NONE;
1182f7018c21STomi Valkeinen
1183f7018c21STomi Valkeinen info->fbops = &hvfb_ops;
1184f7018c21STomi Valkeinen info->pseudo_palette = par->pseudo_palette;
1185f7018c21STomi Valkeinen
1186d21987d7SWei Hu /* Initialize deferred IO */
1187d21987d7SWei Hu info->fbdefio = &synthvid_defio;
1188d21987d7SWei Hu fb_deferred_io_init(info);
1189d21987d7SWei Hu
1190f7018c21STomi Valkeinen /* Send config to host */
1191f7018c21STomi Valkeinen ret = synthvid_send_config(hdev);
1192f7018c21STomi Valkeinen if (ret)
1193f7018c21STomi Valkeinen goto error;
1194f7018c21STomi Valkeinen
1195*ea2f45abSSaurabh Sengar ret = devm_register_framebuffer(&hdev->device, info);
1196f7018c21STomi Valkeinen if (ret) {
1197f7018c21STomi Valkeinen pr_err("Unable to register framebuffer\n");
1198f7018c21STomi Valkeinen goto error;
1199f7018c21STomi Valkeinen }
1200f7018c21STomi Valkeinen
1201f7018c21STomi Valkeinen par->fb_ready = true;
1202f7018c21STomi Valkeinen
12033686fe96SDexuan Cui par->synchronous_fb = false;
1204d786e00dSGuilherme G. Piccoli
1205d786e00dSGuilherme G. Piccoli /*
1206d786e00dSGuilherme G. Piccoli * We need to be sure this panic notifier runs _before_ the
1207d786e00dSGuilherme G. Piccoli * vmbus disconnect, so order it by priority. It must execute
1208d786e00dSGuilherme G. Piccoli * before the function hv_panic_vmbus_unload() [drivers/hv/vmbus_drv.c],
1209d786e00dSGuilherme G. Piccoli * which is almost at the end of list, with priority = INT_MIN + 1.
1210d786e00dSGuilherme G. Piccoli */
12113686fe96SDexuan Cui par->hvfb_panic_nb.notifier_call = hvfb_on_panic;
121227f22f89SChen Ni par->hvfb_panic_nb.priority = INT_MIN + 10;
12133686fe96SDexuan Cui atomic_notifier_chain_register(&panic_notifier_list,
12143686fe96SDexuan Cui &par->hvfb_panic_nb);
12153686fe96SDexuan Cui
1216f7018c21STomi Valkeinen return 0;
1217f7018c21STomi Valkeinen
1218f7018c21STomi Valkeinen error:
1219d21987d7SWei Hu fb_deferred_io_cleanup(info);
1220f5e728a5SSaurabh Sengar hvfb_putmem(info);
1221f7018c21STomi Valkeinen error2:
1222f7018c21STomi Valkeinen vmbus_close(hdev->channel);
1223f7018c21STomi Valkeinen error1:
1224f7018c21STomi Valkeinen cancel_delayed_work_sync(&par->dwork);
1225f7018c21STomi Valkeinen hv_set_drvdata(hdev, NULL);
1226f7018c21STomi Valkeinen framebuffer_release(info);
1227f7018c21STomi Valkeinen return ret;
1228f7018c21STomi Valkeinen }
1229f7018c21STomi Valkeinen
hvfb_remove(struct hv_device * hdev)123096ec2939SDawei Li static void hvfb_remove(struct hv_device *hdev)
1231f7018c21STomi Valkeinen {
1232f7018c21STomi Valkeinen struct fb_info *info = hv_get_drvdata(hdev);
1233f7018c21STomi Valkeinen struct hvfb_par *par = info->par;
1234f7018c21STomi Valkeinen
12353686fe96SDexuan Cui atomic_notifier_chain_unregister(&panic_notifier_list,
12363686fe96SDexuan Cui &par->hvfb_panic_nb);
12373686fe96SDexuan Cui
1238f7018c21STomi Valkeinen par->update = false;
1239f7018c21STomi Valkeinen par->fb_ready = false;
1240f7018c21STomi Valkeinen
1241d21987d7SWei Hu fb_deferred_io_cleanup(info);
1242d21987d7SWei Hu
1243f7018c21STomi Valkeinen cancel_delayed_work_sync(&par->dwork);
1244f7018c21STomi Valkeinen
1245f7018c21STomi Valkeinen vmbus_close(hdev->channel);
1246f7018c21STomi Valkeinen hv_set_drvdata(hdev, NULL);
1247f7018c21STomi Valkeinen }
1248f7018c21STomi Valkeinen
hvfb_suspend(struct hv_device * hdev)12491ecf3020SDexuan Cui static int hvfb_suspend(struct hv_device *hdev)
12501ecf3020SDexuan Cui {
12511ecf3020SDexuan Cui struct fb_info *info = hv_get_drvdata(hdev);
12521ecf3020SDexuan Cui struct hvfb_par *par = info->par;
12531ecf3020SDexuan Cui
12541ecf3020SDexuan Cui console_lock();
12551ecf3020SDexuan Cui
12561ecf3020SDexuan Cui /* 1 means do suspend */
12571ecf3020SDexuan Cui fb_set_suspend(info, 1);
12581ecf3020SDexuan Cui
12591ecf3020SDexuan Cui cancel_delayed_work_sync(&par->dwork);
1260382a4622SDexuan Cui cancel_delayed_work_sync(&info->deferred_work);
12611ecf3020SDexuan Cui
12621ecf3020SDexuan Cui par->update_saved = par->update;
12631ecf3020SDexuan Cui par->update = false;
12641ecf3020SDexuan Cui par->fb_ready = false;
12651ecf3020SDexuan Cui
12661ecf3020SDexuan Cui vmbus_close(hdev->channel);
12671ecf3020SDexuan Cui
12681ecf3020SDexuan Cui console_unlock();
12691ecf3020SDexuan Cui
12701ecf3020SDexuan Cui return 0;
12711ecf3020SDexuan Cui }
12721ecf3020SDexuan Cui
hvfb_resume(struct hv_device * hdev)12731ecf3020SDexuan Cui static int hvfb_resume(struct hv_device *hdev)
12741ecf3020SDexuan Cui {
12751ecf3020SDexuan Cui struct fb_info *info = hv_get_drvdata(hdev);
12761ecf3020SDexuan Cui struct hvfb_par *par = info->par;
12771ecf3020SDexuan Cui int ret;
12781ecf3020SDexuan Cui
12791ecf3020SDexuan Cui console_lock();
12801ecf3020SDexuan Cui
12811ecf3020SDexuan Cui ret = synthvid_connect_vsp(hdev);
12821ecf3020SDexuan Cui if (ret != 0)
12831ecf3020SDexuan Cui goto out;
12841ecf3020SDexuan Cui
12851ecf3020SDexuan Cui ret = synthvid_send_config(hdev);
12861ecf3020SDexuan Cui if (ret != 0) {
12871ecf3020SDexuan Cui vmbus_close(hdev->channel);
12881ecf3020SDexuan Cui goto out;
12891ecf3020SDexuan Cui }
12901ecf3020SDexuan Cui
12911ecf3020SDexuan Cui par->fb_ready = true;
12921ecf3020SDexuan Cui par->update = par->update_saved;
12931ecf3020SDexuan Cui
1294382a4622SDexuan Cui schedule_delayed_work(&info->deferred_work, info->fbdefio->delay);
12951ecf3020SDexuan Cui schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
12961ecf3020SDexuan Cui
12971ecf3020SDexuan Cui /* 0 means do resume */
12981ecf3020SDexuan Cui fb_set_suspend(info, 0);
12991ecf3020SDexuan Cui
13001ecf3020SDexuan Cui out:
13011ecf3020SDexuan Cui console_unlock();
13021ecf3020SDexuan Cui
13031ecf3020SDexuan Cui return ret;
13041ecf3020SDexuan Cui }
13051ecf3020SDexuan Cui
1306f7018c21STomi Valkeinen
13079baa3c34SBenoit Taine static const struct pci_device_id pci_stub_id_table[] = {
1308f7018c21STomi Valkeinen {
1309f7018c21STomi Valkeinen .vendor = PCI_VENDOR_ID_MICROSOFT,
1310f7018c21STomi Valkeinen .device = PCI_DEVICE_ID_HYPERV_VIDEO,
1311f7018c21STomi Valkeinen },
1312f7018c21STomi Valkeinen { /* end of list */ }
1313f7018c21STomi Valkeinen };
1314f7018c21STomi Valkeinen
1315f7018c21STomi Valkeinen static const struct hv_vmbus_device_id id_table[] = {
1316f7018c21STomi Valkeinen /* Synthetic Video Device GUID */
1317f7018c21STomi Valkeinen {HV_SYNTHVID_GUID},
1318f7018c21STomi Valkeinen {}
1319f7018c21STomi Valkeinen };
1320f7018c21STomi Valkeinen
1321f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(pci, pci_stub_id_table);
1322f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(vmbus, id_table);
1323f7018c21STomi Valkeinen
1324f7018c21STomi Valkeinen static struct hv_driver hvfb_drv = {
1325f7018c21STomi Valkeinen .name = KBUILD_MODNAME,
1326f7018c21STomi Valkeinen .id_table = id_table,
1327f7018c21STomi Valkeinen .probe = hvfb_probe,
1328f7018c21STomi Valkeinen .remove = hvfb_remove,
13291ecf3020SDexuan Cui .suspend = hvfb_suspend,
13301ecf3020SDexuan Cui .resume = hvfb_resume,
1331af0a5646SArjan van de Ven .driver = {
1332af0a5646SArjan van de Ven .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1333af0a5646SArjan van de Ven },
1334f7018c21STomi Valkeinen };
1335f7018c21STomi Valkeinen
hvfb_pci_stub_probe(struct pci_dev * pdev,const struct pci_device_id * ent)1336f7018c21STomi Valkeinen static int hvfb_pci_stub_probe(struct pci_dev *pdev,
1337f7018c21STomi Valkeinen const struct pci_device_id *ent)
1338f7018c21STomi Valkeinen {
1339f7018c21STomi Valkeinen return 0;
1340f7018c21STomi Valkeinen }
1341f7018c21STomi Valkeinen
hvfb_pci_stub_remove(struct pci_dev * pdev)1342f7018c21STomi Valkeinen static void hvfb_pci_stub_remove(struct pci_dev *pdev)
1343f7018c21STomi Valkeinen {
1344f7018c21STomi Valkeinen }
1345f7018c21STomi Valkeinen
1346f7018c21STomi Valkeinen static struct pci_driver hvfb_pci_stub_driver = {
1347f7018c21STomi Valkeinen .name = KBUILD_MODNAME,
1348f7018c21STomi Valkeinen .id_table = pci_stub_id_table,
1349f7018c21STomi Valkeinen .probe = hvfb_pci_stub_probe,
1350f7018c21STomi Valkeinen .remove = hvfb_pci_stub_remove,
1351af0a5646SArjan van de Ven .driver = {
1352af0a5646SArjan van de Ven .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1353af0a5646SArjan van de Ven }
1354f7018c21STomi Valkeinen };
1355f7018c21STomi Valkeinen
hvfb_drv_init(void)1356f7018c21STomi Valkeinen static int __init hvfb_drv_init(void)
1357f7018c21STomi Valkeinen {
1358f7018c21STomi Valkeinen int ret;
1359f7018c21STomi Valkeinen
13600ba2fa8cSThomas Zimmermann if (fb_modesetting_disabled("hyper_fb"))
13610ba2fa8cSThomas Zimmermann return -ENODEV;
13620ba2fa8cSThomas Zimmermann
1363f7018c21STomi Valkeinen ret = vmbus_driver_register(&hvfb_drv);
1364f7018c21STomi Valkeinen if (ret != 0)
1365f7018c21STomi Valkeinen return ret;
1366f7018c21STomi Valkeinen
1367f7018c21STomi Valkeinen ret = pci_register_driver(&hvfb_pci_stub_driver);
1368f7018c21STomi Valkeinen if (ret != 0) {
1369f7018c21STomi Valkeinen vmbus_driver_unregister(&hvfb_drv);
1370f7018c21STomi Valkeinen return ret;
1371f7018c21STomi Valkeinen }
1372f7018c21STomi Valkeinen
1373f7018c21STomi Valkeinen return 0;
1374f7018c21STomi Valkeinen }
1375f7018c21STomi Valkeinen
hvfb_drv_exit(void)1376f7018c21STomi Valkeinen static void __exit hvfb_drv_exit(void)
1377f7018c21STomi Valkeinen {
1378f7018c21STomi Valkeinen pci_unregister_driver(&hvfb_pci_stub_driver);
1379f7018c21STomi Valkeinen vmbus_driver_unregister(&hvfb_drv);
1380f7018c21STomi Valkeinen }
1381f7018c21STomi Valkeinen
1382f7018c21STomi Valkeinen module_init(hvfb_drv_init);
1383f7018c21STomi Valkeinen module_exit(hvfb_drv_exit);
1384f7018c21STomi Valkeinen
1385f7018c21STomi Valkeinen MODULE_LICENSE("GPL");
1386f7018c21STomi Valkeinen MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver");
1387