xref: /linux/drivers/video/fbdev/hyperv_fb.c (revision 27f22f897095b09df32bf689b63624d23b0c8ebc)
143aa3132SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f7018c21STomi Valkeinen /*
3f7018c21STomi Valkeinen  * Copyright (c) 2012, Microsoft Corporation.
4f7018c21STomi Valkeinen  *
5f7018c21STomi Valkeinen  * Author:
6f7018c21STomi Valkeinen  *   Haiyang Zhang <haiyangz@microsoft.com>
7f7018c21STomi Valkeinen  */
8f7018c21STomi Valkeinen 
9f7018c21STomi Valkeinen /*
10f7018c21STomi Valkeinen  * Hyper-V Synthetic Video Frame Buffer Driver
11f7018c21STomi Valkeinen  *
12f7018c21STomi Valkeinen  * This is the driver for the Hyper-V Synthetic Video, which supports
13f7018c21STomi Valkeinen  * screen resolution up to Full HD 1920x1080 with 32 bit color on Windows
14f7018c21STomi Valkeinen  * Server 2012, and 1600x1200 with 16 bit color on Windows Server 2008 R2
15f7018c21STomi Valkeinen  * or earlier.
16f7018c21STomi Valkeinen  *
17f7018c21STomi Valkeinen  * It also solves the double mouse cursor issue of the emulated video mode.
18f7018c21STomi Valkeinen  *
19f7018c21STomi Valkeinen  * The default screen resolution is 1152x864, which may be changed by a
20f7018c21STomi Valkeinen  * kernel parameter:
21f7018c21STomi Valkeinen  *     video=hyperv_fb:<width>x<height>
22f7018c21STomi Valkeinen  *     For example: video=hyperv_fb:1280x1024
23f7018c21STomi Valkeinen  *
24f7018c21STomi Valkeinen  * Portrait orientation is also supported:
25f7018c21STomi Valkeinen  *     For example: video=hyperv_fb:864x1152
2667e7cdb4SWei Hu  *
2767e7cdb4SWei Hu  * When a Windows 10 RS5+ host is used, the virtual machine screen
2867e7cdb4SWei Hu  * resolution is obtained from the host. The "video=hyperv_fb" option is
2967e7cdb4SWei Hu  * not needed, but still can be used to overwrite what the host specifies.
3067e7cdb4SWei Hu  * The VM resolution on the host could be set by executing the powershell
3167e7cdb4SWei Hu  * "set-vmvideo" command. For example
3267e7cdb4SWei Hu  *     set-vmvideo -vmname name -horizontalresolution:1920 \
3367e7cdb4SWei Hu  * -verticalresolution:1200 -resolutiontype single
343a6fb6c4SWei Hu  *
353a6fb6c4SWei Hu  * Gen 1 VMs also support direct using VM's physical memory for framebuffer.
363a6fb6c4SWei Hu  * It could improve the efficiency and performance for framebuffer and VM.
373a6fb6c4SWei Hu  * This requires to allocate contiguous physical memory from Linux kernel's
383a6fb6c4SWei Hu  * CMA memory allocator. To enable this, supply a kernel parameter to give
393a6fb6c4SWei Hu  * enough memory space to CMA allocator for framebuffer. For example:
403a6fb6c4SWei Hu  *    cma=130m
413a6fb6c4SWei Hu  * This gives 130MB memory to CMA allocator that can be allocated to
423a6fb6c4SWei Hu  * framebuffer. For reference, 8K resolution (7680x4320) takes about
433a6fb6c4SWei Hu  * 127MB memory.
44f7018c21STomi Valkeinen  */
45f7018c21STomi Valkeinen 
46f7018c21STomi Valkeinen #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47f7018c21STomi Valkeinen 
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 
285f7018c21STomi Valkeinen /* Send message to Hyper-V host */
286f7018c21STomi Valkeinen static inline int synthvid_send(struct hv_device *hdev,
287f7018c21STomi Valkeinen 				struct synthvid_msg *msg)
288f7018c21STomi Valkeinen {
289f7018c21STomi Valkeinen 	static atomic64_t request_id = ATOMIC64_INIT(0);
290f7018c21STomi Valkeinen 	int ret;
291f7018c21STomi Valkeinen 
292f7018c21STomi Valkeinen 	msg->pipe_hdr.type = PIPE_MSG_DATA;
293f7018c21STomi Valkeinen 	msg->pipe_hdr.size = msg->vid_hdr.size;
294f7018c21STomi Valkeinen 
295f7018c21STomi Valkeinen 	ret = vmbus_sendpacket(hdev->channel, msg,
296f7018c21STomi Valkeinen 			       msg->vid_hdr.size + sizeof(struct pipe_msg_hdr),
297f7018c21STomi Valkeinen 			       atomic64_inc_return(&request_id),
298f7018c21STomi Valkeinen 			       VM_PKT_DATA_INBAND, 0);
299f7018c21STomi Valkeinen 
300f7018c21STomi Valkeinen 	if (ret)
301aa5b7d11SMichael Kelley 		pr_err_ratelimited("Unable to send packet via vmbus; error %d\n", ret);
302f7018c21STomi Valkeinen 
303f7018c21STomi Valkeinen 	return ret;
304f7018c21STomi Valkeinen }
305f7018c21STomi Valkeinen 
306f7018c21STomi Valkeinen 
307f7018c21STomi Valkeinen /* Send screen resolution info to host */
308f7018c21STomi Valkeinen static int synthvid_send_situ(struct hv_device *hdev)
309f7018c21STomi Valkeinen {
310f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
311f7018c21STomi Valkeinen 	struct synthvid_msg msg;
312f7018c21STomi Valkeinen 
313f7018c21STomi Valkeinen 	if (!info)
314f7018c21STomi Valkeinen 		return -ENODEV;
315f7018c21STomi Valkeinen 
316f7018c21STomi Valkeinen 	memset(&msg, 0, sizeof(struct synthvid_msg));
317f7018c21STomi Valkeinen 
318f7018c21STomi Valkeinen 	msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE;
319f7018c21STomi Valkeinen 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
320f7018c21STomi Valkeinen 		sizeof(struct synthvid_situation_update);
321f7018c21STomi Valkeinen 	msg.situ.user_ctx = 0;
322f7018c21STomi Valkeinen 	msg.situ.video_output_count = 1;
323f7018c21STomi Valkeinen 	msg.situ.video_output[0].active = 1;
324f7018c21STomi Valkeinen 	msg.situ.video_output[0].vram_offset = 0;
325f7018c21STomi Valkeinen 	msg.situ.video_output[0].depth_bits = info->var.bits_per_pixel;
326f7018c21STomi Valkeinen 	msg.situ.video_output[0].width_pixels = info->var.xres;
327f7018c21STomi Valkeinen 	msg.situ.video_output[0].height_pixels = info->var.yres;
328f7018c21STomi Valkeinen 	msg.situ.video_output[0].pitch_bytes = info->fix.line_length;
329f7018c21STomi Valkeinen 
330f7018c21STomi Valkeinen 	synthvid_send(hdev, &msg);
331f7018c21STomi Valkeinen 
332f7018c21STomi Valkeinen 	return 0;
333f7018c21STomi Valkeinen }
334f7018c21STomi Valkeinen 
335f7018c21STomi Valkeinen /* Send mouse pointer info to host */
336f7018c21STomi Valkeinen static int synthvid_send_ptr(struct hv_device *hdev)
337f7018c21STomi Valkeinen {
338f7018c21STomi Valkeinen 	struct synthvid_msg msg;
339f7018c21STomi Valkeinen 
340f7018c21STomi Valkeinen 	memset(&msg, 0, sizeof(struct synthvid_msg));
341f7018c21STomi Valkeinen 	msg.vid_hdr.type = SYNTHVID_POINTER_POSITION;
342f7018c21STomi Valkeinen 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
343f7018c21STomi Valkeinen 		sizeof(struct synthvid_pointer_position);
344f7018c21STomi Valkeinen 	msg.ptr_pos.is_visible = 1;
345f7018c21STomi Valkeinen 	msg.ptr_pos.video_output = 0;
346f7018c21STomi Valkeinen 	msg.ptr_pos.image_x = 0;
347f7018c21STomi Valkeinen 	msg.ptr_pos.image_y = 0;
348f7018c21STomi Valkeinen 	synthvid_send(hdev, &msg);
349f7018c21STomi Valkeinen 
350f7018c21STomi Valkeinen 	memset(&msg, 0, sizeof(struct synthvid_msg));
351f7018c21STomi Valkeinen 	msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE;
352f7018c21STomi Valkeinen 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
353f7018c21STomi Valkeinen 		sizeof(struct synthvid_pointer_shape);
354f7018c21STomi Valkeinen 	msg.ptr_shape.part_idx = CURSOR_COMPLETE;
355f7018c21STomi Valkeinen 	msg.ptr_shape.is_argb = 1;
356f7018c21STomi Valkeinen 	msg.ptr_shape.width = 1;
357f7018c21STomi Valkeinen 	msg.ptr_shape.height = 1;
358f7018c21STomi Valkeinen 	msg.ptr_shape.hot_x = 0;
359f7018c21STomi Valkeinen 	msg.ptr_shape.hot_y = 0;
360f7018c21STomi Valkeinen 	msg.ptr_shape.data[0] = 0;
361f7018c21STomi Valkeinen 	msg.ptr_shape.data[1] = 1;
362f7018c21STomi Valkeinen 	msg.ptr_shape.data[2] = 1;
363f7018c21STomi Valkeinen 	msg.ptr_shape.data[3] = 1;
364f7018c21STomi Valkeinen 	synthvid_send(hdev, &msg);
365f7018c21STomi Valkeinen 
366f7018c21STomi Valkeinen 	return 0;
367f7018c21STomi Valkeinen }
368f7018c21STomi Valkeinen 
369f7018c21STomi Valkeinen /* Send updated screen area (dirty rectangle) location to host */
370d21987d7SWei Hu static int
371d21987d7SWei Hu synthvid_update(struct fb_info *info, int x1, int y1, int x2, int y2)
372f7018c21STomi Valkeinen {
373f7018c21STomi Valkeinen 	struct hv_device *hdev = device_to_hv_device(info->device);
374f7018c21STomi Valkeinen 	struct synthvid_msg msg;
375f7018c21STomi Valkeinen 
376f7018c21STomi Valkeinen 	memset(&msg, 0, sizeof(struct synthvid_msg));
377d21987d7SWei Hu 	if (x2 == INT_MAX)
378d21987d7SWei Hu 		x2 = info->var.xres;
379d21987d7SWei Hu 	if (y2 == INT_MAX)
380d21987d7SWei Hu 		y2 = info->var.yres;
381f7018c21STomi Valkeinen 
382f7018c21STomi Valkeinen 	msg.vid_hdr.type = SYNTHVID_DIRT;
383f7018c21STomi Valkeinen 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
384f7018c21STomi Valkeinen 		sizeof(struct synthvid_dirt);
385f7018c21STomi Valkeinen 	msg.dirt.video_output = 0;
386f7018c21STomi Valkeinen 	msg.dirt.dirt_count = 1;
387d21987d7SWei Hu 	msg.dirt.rect[0].x1 = (x1 > x2) ? 0 : x1;
388d21987d7SWei Hu 	msg.dirt.rect[0].y1 = (y1 > y2) ? 0 : y1;
389d21987d7SWei Hu 	msg.dirt.rect[0].x2 =
390d21987d7SWei Hu 		(x2 < x1 || x2 > info->var.xres) ? info->var.xres : x2;
391d21987d7SWei Hu 	msg.dirt.rect[0].y2 =
392d21987d7SWei Hu 		(y2 < y1 || y2 > info->var.yres) ? info->var.yres : y2;
393f7018c21STomi Valkeinen 
394f7018c21STomi Valkeinen 	synthvid_send(hdev, &msg);
395f7018c21STomi Valkeinen 
396f7018c21STomi Valkeinen 	return 0;
397f7018c21STomi Valkeinen }
398f7018c21STomi Valkeinen 
399d21987d7SWei Hu static void hvfb_docopy(struct hvfb_par *par,
400d21987d7SWei Hu 			unsigned long offset,
401d21987d7SWei Hu 			unsigned long size)
402d21987d7SWei Hu {
403d21987d7SWei Hu 	if (!par || !par->mmio_vp || !par->dio_vp || !par->fb_ready ||
404d21987d7SWei Hu 	    size == 0 || offset >= dio_fb_size)
405d21987d7SWei Hu 		return;
406d21987d7SWei Hu 
407d21987d7SWei Hu 	if (offset + size > dio_fb_size)
408d21987d7SWei Hu 		size = dio_fb_size - offset;
409d21987d7SWei Hu 
410d21987d7SWei Hu 	memcpy(par->mmio_vp + offset, par->dio_vp + offset, size);
411d21987d7SWei Hu }
412d21987d7SWei Hu 
413d21987d7SWei Hu /* Deferred IO callback */
414e80eec1bSThomas Zimmermann static void synthvid_deferred_io(struct fb_info *p, struct list_head *pagereflist)
415d21987d7SWei Hu {
416d21987d7SWei Hu 	struct hvfb_par *par = p->par;
41756c134f7SThomas Zimmermann 	struct fb_deferred_io_pageref *pageref;
418d21987d7SWei Hu 	unsigned long start, end;
419d21987d7SWei Hu 	int y1, y2, miny, maxy;
420d21987d7SWei Hu 
421d21987d7SWei Hu 	miny = INT_MAX;
422d21987d7SWei Hu 	maxy = 0;
423d21987d7SWei Hu 
424d21987d7SWei Hu 	/*
425d21987d7SWei Hu 	 * Merge dirty pages. It is possible that last page cross
426d21987d7SWei Hu 	 * over the end of frame buffer row yres. This is taken care of
427d21987d7SWei Hu 	 * in synthvid_update function by clamping the y2
428d21987d7SWei Hu 	 * value to yres.
429d21987d7SWei Hu 	 */
430e80eec1bSThomas Zimmermann 	list_for_each_entry(pageref, pagereflist, list) {
431e2d8b428SThomas Zimmermann 		start = pageref->offset;
432d21987d7SWei Hu 		end = start + PAGE_SIZE - 1;
433d21987d7SWei Hu 		y1 = start / p->fix.line_length;
434d21987d7SWei Hu 		y2 = end / p->fix.line_length;
435d21987d7SWei Hu 		miny = min_t(int, miny, y1);
436d21987d7SWei Hu 		maxy = max_t(int, maxy, y2);
437d21987d7SWei Hu 
438d21987d7SWei Hu 		/* Copy from dio space to mmio address */
4393a6fb6c4SWei Hu 		if (par->fb_ready && par->need_docopy)
440d21987d7SWei Hu 			hvfb_docopy(par, start, PAGE_SIZE);
441d21987d7SWei Hu 	}
442d21987d7SWei Hu 
443d21987d7SWei Hu 	if (par->fb_ready && par->update)
444d21987d7SWei Hu 		synthvid_update(p, 0, miny, p->var.xres, maxy + 1);
445d21987d7SWei Hu }
446d21987d7SWei Hu 
447d21987d7SWei Hu static struct fb_deferred_io synthvid_defio = {
448d21987d7SWei Hu 	.delay		= HZ / 20,
449d21987d7SWei Hu 	.deferred_io	= synthvid_deferred_io,
450d21987d7SWei Hu };
451f7018c21STomi Valkeinen 
452f7018c21STomi Valkeinen /*
453f7018c21STomi Valkeinen  * Actions on received messages from host:
454f7018c21STomi Valkeinen  * Complete the wait event.
455f7018c21STomi Valkeinen  * Or, reply with screen and cursor info.
456f7018c21STomi Valkeinen  */
457f7018c21STomi Valkeinen static void synthvid_recv_sub(struct hv_device *hdev)
458f7018c21STomi Valkeinen {
459f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
460f7018c21STomi Valkeinen 	struct hvfb_par *par;
461f7018c21STomi Valkeinen 	struct synthvid_msg *msg;
462f7018c21STomi Valkeinen 
463f7018c21STomi Valkeinen 	if (!info)
464f7018c21STomi Valkeinen 		return;
465f7018c21STomi Valkeinen 
466f7018c21STomi Valkeinen 	par = info->par;
467f7018c21STomi Valkeinen 	msg = (struct synthvid_msg *)par->recv_buf;
468f7018c21STomi Valkeinen 
469f7018c21STomi Valkeinen 	/* Complete the wait event */
470f7018c21STomi Valkeinen 	if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE ||
47167e7cdb4SWei Hu 	    msg->vid_hdr.type == SYNTHVID_RESOLUTION_RESPONSE ||
472f7018c21STomi Valkeinen 	    msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) {
473f7018c21STomi Valkeinen 		memcpy(par->init_buf, msg, MAX_VMBUS_PKT_SIZE);
474f7018c21STomi Valkeinen 		complete(&par->wait);
475f7018c21STomi Valkeinen 		return;
476f7018c21STomi Valkeinen 	}
477f7018c21STomi Valkeinen 
478f7018c21STomi Valkeinen 	/* Reply with screen and cursor info */
479f7018c21STomi Valkeinen 	if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) {
480f7018c21STomi Valkeinen 		if (par->fb_ready) {
481f7018c21STomi Valkeinen 			synthvid_send_ptr(hdev);
482f7018c21STomi Valkeinen 			synthvid_send_situ(hdev);
483f7018c21STomi Valkeinen 		}
484f7018c21STomi Valkeinen 
485f7018c21STomi Valkeinen 		par->update = msg->feature_chg.is_dirt_needed;
486f7018c21STomi Valkeinen 		if (par->update)
487f7018c21STomi Valkeinen 			schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
488f7018c21STomi Valkeinen 	}
489f7018c21STomi Valkeinen }
490f7018c21STomi Valkeinen 
491f7018c21STomi Valkeinen /* Receive callback for messages from the host */
492f7018c21STomi Valkeinen static void synthvid_receive(void *ctx)
493f7018c21STomi Valkeinen {
494f7018c21STomi Valkeinen 	struct hv_device *hdev = ctx;
495f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
496f7018c21STomi Valkeinen 	struct hvfb_par *par;
497f7018c21STomi Valkeinen 	struct synthvid_msg *recv_buf;
498f7018c21STomi Valkeinen 	u32 bytes_recvd;
499f7018c21STomi Valkeinen 	u64 req_id;
500f7018c21STomi Valkeinen 	int ret;
501f7018c21STomi Valkeinen 
502f7018c21STomi Valkeinen 	if (!info)
503f7018c21STomi Valkeinen 		return;
504f7018c21STomi Valkeinen 
505f7018c21STomi Valkeinen 	par = info->par;
506f7018c21STomi Valkeinen 	recv_buf = (struct synthvid_msg *)par->recv_buf;
507f7018c21STomi Valkeinen 
508f7018c21STomi Valkeinen 	do {
509f7018c21STomi Valkeinen 		ret = vmbus_recvpacket(hdev->channel, recv_buf,
510f7018c21STomi Valkeinen 				       MAX_VMBUS_PKT_SIZE,
511f7018c21STomi Valkeinen 				       &bytes_recvd, &req_id);
512f7018c21STomi Valkeinen 		if (bytes_recvd > 0 &&
513f7018c21STomi Valkeinen 		    recv_buf->pipe_hdr.type == PIPE_MSG_DATA)
514f7018c21STomi Valkeinen 			synthvid_recv_sub(hdev);
515f7018c21STomi Valkeinen 	} while (bytes_recvd > 0 && ret == 0);
516f7018c21STomi Valkeinen }
517f7018c21STomi Valkeinen 
51867e7cdb4SWei Hu /* Check if the ver1 version is equal or greater than ver2 */
51967e7cdb4SWei Hu static inline bool synthvid_ver_ge(u32 ver1, u32 ver2)
52067e7cdb4SWei Hu {
52167e7cdb4SWei Hu 	if (SYNTHVID_VER_GET_MAJOR(ver1) > SYNTHVID_VER_GET_MAJOR(ver2) ||
52267e7cdb4SWei Hu 	    (SYNTHVID_VER_GET_MAJOR(ver1) == SYNTHVID_VER_GET_MAJOR(ver2) &&
52367e7cdb4SWei Hu 	     SYNTHVID_VER_GET_MINOR(ver1) >= SYNTHVID_VER_GET_MINOR(ver2)))
52467e7cdb4SWei Hu 		return true;
52567e7cdb4SWei Hu 
52667e7cdb4SWei Hu 	return false;
52767e7cdb4SWei Hu }
52867e7cdb4SWei Hu 
529f7018c21STomi Valkeinen /* Check synthetic video protocol version with the host */
530f7018c21STomi Valkeinen static int synthvid_negotiate_ver(struct hv_device *hdev, u32 ver)
531f7018c21STomi Valkeinen {
532f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
533f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
534f7018c21STomi Valkeinen 	struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
5357dea97e0SNicholas Mc Guire 	int ret = 0;
5367dea97e0SNicholas Mc Guire 	unsigned long t;
537f7018c21STomi Valkeinen 
538f7018c21STomi Valkeinen 	memset(msg, 0, sizeof(struct synthvid_msg));
539f7018c21STomi Valkeinen 	msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST;
540f7018c21STomi Valkeinen 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
541f7018c21STomi Valkeinen 		sizeof(struct synthvid_version_req);
542f7018c21STomi Valkeinen 	msg->ver_req.version = ver;
543f7018c21STomi Valkeinen 	synthvid_send(hdev, msg);
544f7018c21STomi Valkeinen 
545f7018c21STomi Valkeinen 	t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
546f7018c21STomi Valkeinen 	if (!t) {
547f7018c21STomi Valkeinen 		pr_err("Time out on waiting version response\n");
548f7018c21STomi Valkeinen 		ret = -ETIMEDOUT;
549f7018c21STomi Valkeinen 		goto out;
550f7018c21STomi Valkeinen 	}
551f7018c21STomi Valkeinen 	if (!msg->ver_resp.is_accepted) {
552f7018c21STomi Valkeinen 		ret = -ENODEV;
553f7018c21STomi Valkeinen 		goto out;
554f7018c21STomi Valkeinen 	}
555f7018c21STomi Valkeinen 
556f7018c21STomi Valkeinen 	par->synthvid_version = ver;
55767e7cdb4SWei Hu 	pr_info("Synthvid Version major %d, minor %d\n",
55867e7cdb4SWei Hu 		SYNTHVID_VER_GET_MAJOR(ver), SYNTHVID_VER_GET_MINOR(ver));
55967e7cdb4SWei Hu 
56067e7cdb4SWei Hu out:
56167e7cdb4SWei Hu 	return ret;
56267e7cdb4SWei Hu }
56367e7cdb4SWei Hu 
56467e7cdb4SWei Hu /* Get current resolution from the host */
56567e7cdb4SWei Hu static int synthvid_get_supported_resolution(struct hv_device *hdev)
56667e7cdb4SWei Hu {
56767e7cdb4SWei Hu 	struct fb_info *info = hv_get_drvdata(hdev);
56867e7cdb4SWei Hu 	struct hvfb_par *par = info->par;
56967e7cdb4SWei Hu 	struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
57067e7cdb4SWei Hu 	int ret = 0;
57167e7cdb4SWei Hu 	unsigned long t;
57267e7cdb4SWei Hu 	u8 index;
57367e7cdb4SWei Hu 
57467e7cdb4SWei Hu 	memset(msg, 0, sizeof(struct synthvid_msg));
57567e7cdb4SWei Hu 	msg->vid_hdr.type = SYNTHVID_RESOLUTION_REQUEST;
57667e7cdb4SWei Hu 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
57767e7cdb4SWei Hu 		sizeof(struct synthvid_supported_resolution_req);
57867e7cdb4SWei Hu 
57967e7cdb4SWei Hu 	msg->resolution_req.maximum_resolution_count =
58067e7cdb4SWei Hu 		SYNTHVID_MAX_RESOLUTION_COUNT;
58167e7cdb4SWei Hu 	synthvid_send(hdev, msg);
58267e7cdb4SWei Hu 
58367e7cdb4SWei Hu 	t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
58467e7cdb4SWei Hu 	if (!t) {
58567e7cdb4SWei Hu 		pr_err("Time out on waiting resolution response\n");
58667e7cdb4SWei Hu 		ret = -ETIMEDOUT;
58767e7cdb4SWei Hu 		goto out;
58867e7cdb4SWei Hu 	}
58967e7cdb4SWei Hu 
59067e7cdb4SWei Hu 	if (msg->resolution_resp.resolution_count == 0) {
59167e7cdb4SWei Hu 		pr_err("No supported resolutions\n");
59267e7cdb4SWei Hu 		ret = -ENODEV;
59367e7cdb4SWei Hu 		goto out;
59467e7cdb4SWei Hu 	}
59567e7cdb4SWei Hu 
59667e7cdb4SWei Hu 	index = msg->resolution_resp.default_resolution_index;
59767e7cdb4SWei Hu 	if (index >= msg->resolution_resp.resolution_count) {
59867e7cdb4SWei Hu 		pr_err("Invalid resolution index: %d\n", index);
59967e7cdb4SWei Hu 		ret = -ENODEV;
60067e7cdb4SWei Hu 		goto out;
60167e7cdb4SWei Hu 	}
60267e7cdb4SWei Hu 
60367e7cdb4SWei Hu 	screen_width =
60467e7cdb4SWei Hu 		msg->resolution_resp.supported_resolution[index].width;
60567e7cdb4SWei Hu 	screen_height =
60667e7cdb4SWei Hu 		msg->resolution_resp.supported_resolution[index].height;
607f7018c21STomi Valkeinen 
608f7018c21STomi Valkeinen out:
609f7018c21STomi Valkeinen 	return ret;
610f7018c21STomi Valkeinen }
611f7018c21STomi Valkeinen 
612f7018c21STomi Valkeinen /* Connect to VSP (Virtual Service Provider) on host */
613f7018c21STomi Valkeinen static int synthvid_connect_vsp(struct hv_device *hdev)
614f7018c21STomi Valkeinen {
615f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
616f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
617f7018c21STomi Valkeinen 	int ret;
618f7018c21STomi Valkeinen 
619f7018c21STomi Valkeinen 	ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE,
620f7018c21STomi Valkeinen 			 NULL, 0, synthvid_receive, hdev);
621f7018c21STomi Valkeinen 	if (ret) {
622f7018c21STomi Valkeinen 		pr_err("Unable to open vmbus channel\n");
623f7018c21STomi Valkeinen 		return ret;
624f7018c21STomi Valkeinen 	}
625f7018c21STomi Valkeinen 
626f7018c21STomi Valkeinen 	/* Negotiate the protocol version with host */
62767e7cdb4SWei Hu 	switch (vmbus_proto_version) {
62867e7cdb4SWei Hu 	case VERSION_WIN10:
62967e7cdb4SWei Hu 	case VERSION_WIN10_V5:
63067e7cdb4SWei Hu 		ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
63167e7cdb4SWei Hu 		if (!ret)
63267e7cdb4SWei Hu 			break;
633df561f66SGustavo A. R. Silva 		fallthrough;
63467e7cdb4SWei Hu 	case VERSION_WIN8:
63567e7cdb4SWei Hu 	case VERSION_WIN8_1:
636f7018c21STomi Valkeinen 		ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8);
63767e7cdb4SWei Hu 		break;
63867e7cdb4SWei Hu 	default:
63967e7cdb4SWei Hu 		ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
64067e7cdb4SWei Hu 		break;
64167e7cdb4SWei Hu 	}
642f7018c21STomi Valkeinen 
643f7018c21STomi Valkeinen 	if (ret) {
644f7018c21STomi Valkeinen 		pr_err("Synthetic video device version not accepted\n");
645f7018c21STomi Valkeinen 		goto error;
646f7018c21STomi Valkeinen 	}
647f7018c21STomi Valkeinen 
648f7018c21STomi Valkeinen 	screen_depth = SYNTHVID_DEPTH_WIN8;
64967e7cdb4SWei Hu 	if (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10)) {
65067e7cdb4SWei Hu 		ret = synthvid_get_supported_resolution(hdev);
65167e7cdb4SWei Hu 		if (ret)
65267e7cdb4SWei Hu 			pr_info("Failed to get supported resolution from host, use default\n");
65367e7cdb4SWei Hu 	}
65467e7cdb4SWei Hu 
655f7018c21STomi Valkeinen 	screen_fb_size = hdev->channel->offermsg.offer.
656f7018c21STomi Valkeinen 				mmio_megabytes * 1024 * 1024;
657f7018c21STomi Valkeinen 
658f7018c21STomi Valkeinen 	return 0;
659f7018c21STomi Valkeinen 
660f7018c21STomi Valkeinen error:
661f7018c21STomi Valkeinen 	vmbus_close(hdev->channel);
662f7018c21STomi Valkeinen 	return ret;
663f7018c21STomi Valkeinen }
664f7018c21STomi Valkeinen 
665f7018c21STomi Valkeinen /* Send VRAM and Situation messages to the host */
666f7018c21STomi Valkeinen static int synthvid_send_config(struct hv_device *hdev)
667f7018c21STomi Valkeinen {
668f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
669f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
670f7018c21STomi Valkeinen 	struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
6717dea97e0SNicholas Mc Guire 	int ret = 0;
6727dea97e0SNicholas Mc Guire 	unsigned long t;
673f7018c21STomi Valkeinen 
674f7018c21STomi Valkeinen 	/* Send VRAM location */
675f7018c21STomi Valkeinen 	memset(msg, 0, sizeof(struct synthvid_msg));
676f7018c21STomi Valkeinen 	msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION;
677f7018c21STomi Valkeinen 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
678f7018c21STomi Valkeinen 		sizeof(struct synthvid_vram_location);
679d21987d7SWei Hu 	msg->vram.user_ctx = msg->vram.vram_gpa = par->mmio_pp;
680f7018c21STomi Valkeinen 	msg->vram.is_vram_gpa_specified = 1;
681f7018c21STomi Valkeinen 	synthvid_send(hdev, msg);
682f7018c21STomi Valkeinen 
683f7018c21STomi Valkeinen 	t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
684f7018c21STomi Valkeinen 	if (!t) {
685f7018c21STomi Valkeinen 		pr_err("Time out on waiting vram location ack\n");
686f7018c21STomi Valkeinen 		ret = -ETIMEDOUT;
687f7018c21STomi Valkeinen 		goto out;
688f7018c21STomi Valkeinen 	}
689d21987d7SWei Hu 	if (msg->vram_ack.user_ctx != par->mmio_pp) {
690f7018c21STomi Valkeinen 		pr_err("Unable to set VRAM location\n");
691f7018c21STomi Valkeinen 		ret = -ENODEV;
692f7018c21STomi Valkeinen 		goto out;
693f7018c21STomi Valkeinen 	}
694f7018c21STomi Valkeinen 
695f7018c21STomi Valkeinen 	/* Send pointer and situation update */
696f7018c21STomi Valkeinen 	synthvid_send_ptr(hdev);
697f7018c21STomi Valkeinen 	synthvid_send_situ(hdev);
698f7018c21STomi Valkeinen 
699f7018c21STomi Valkeinen out:
700f7018c21STomi Valkeinen 	return ret;
701f7018c21STomi Valkeinen }
702f7018c21STomi Valkeinen 
703f7018c21STomi Valkeinen 
704f7018c21STomi Valkeinen /*
705f7018c21STomi Valkeinen  * Delayed work callback:
706d21987d7SWei Hu  * It is scheduled to call whenever update request is received and it has
707d21987d7SWei Hu  * not been called in last HVFB_ONDEMAND_THROTTLE time interval.
708f7018c21STomi Valkeinen  */
709f7018c21STomi Valkeinen static void hvfb_update_work(struct work_struct *w)
710f7018c21STomi Valkeinen {
711f7018c21STomi Valkeinen 	struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work);
712f7018c21STomi Valkeinen 	struct fb_info *info = par->info;
713d21987d7SWei Hu 	unsigned long flags;
714d21987d7SWei Hu 	int x1, x2, y1, y2;
715d21987d7SWei Hu 	int j;
716f7018c21STomi Valkeinen 
717d21987d7SWei Hu 	spin_lock_irqsave(&par->delayed_refresh_lock, flags);
718d21987d7SWei Hu 	/* Reset the request flag */
719d21987d7SWei Hu 	par->delayed_refresh = false;
720f7018c21STomi Valkeinen 
721d21987d7SWei Hu 	/* Store the dirty rectangle to local variables */
722d21987d7SWei Hu 	x1 = par->x1;
723d21987d7SWei Hu 	x2 = par->x2;
724d21987d7SWei Hu 	y1 = par->y1;
725d21987d7SWei Hu 	y2 = par->y2;
726d21987d7SWei Hu 
727d21987d7SWei Hu 	/* Clear dirty rectangle */
728d21987d7SWei Hu 	par->x1 = par->y1 = INT_MAX;
729d21987d7SWei Hu 	par->x2 = par->y2 = 0;
730d21987d7SWei Hu 
731d21987d7SWei Hu 	spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
732d21987d7SWei Hu 
733d21987d7SWei Hu 	if (x1 > info->var.xres || x2 > info->var.xres ||
734d21987d7SWei Hu 	    y1 > info->var.yres || y2 > info->var.yres || x2 <= x1)
735d21987d7SWei Hu 		return;
736d21987d7SWei Hu 
737d21987d7SWei Hu 	/* Copy the dirty rectangle to frame buffer memory */
7383a6fb6c4SWei Hu 	if (par->need_docopy)
7393a6fb6c4SWei Hu 		for (j = y1; j < y2; j++)
740d21987d7SWei Hu 			hvfb_docopy(par,
741d21987d7SWei Hu 				    j * info->fix.line_length +
742d21987d7SWei Hu 				    (x1 * screen_depth / 8),
743d21987d7SWei Hu 				    (x2 - x1) * screen_depth / 8);
744d21987d7SWei Hu 
745d21987d7SWei Hu 	/* Refresh */
746d21987d7SWei Hu 	if (par->fb_ready && par->update)
747d21987d7SWei Hu 		synthvid_update(info, x1, y1, x2, y2);
748d21987d7SWei Hu }
749d21987d7SWei Hu 
750d21987d7SWei Hu /*
751d21987d7SWei Hu  * Control the on-demand refresh frequency. It schedules a delayed
752d21987d7SWei Hu  * screen update if it has not yet.
753d21987d7SWei Hu  */
754d21987d7SWei Hu static void hvfb_ondemand_refresh_throttle(struct hvfb_par *par,
755d21987d7SWei Hu 					   int x1, int y1, int w, int h)
756d21987d7SWei Hu {
757d21987d7SWei Hu 	unsigned long flags;
758d21987d7SWei Hu 	int x2 = x1 + w;
759d21987d7SWei Hu 	int y2 = y1 + h;
760d21987d7SWei Hu 
761d21987d7SWei Hu 	spin_lock_irqsave(&par->delayed_refresh_lock, flags);
762d21987d7SWei Hu 
763d21987d7SWei Hu 	/* Merge dirty rectangle */
764d21987d7SWei Hu 	par->x1 = min_t(int, par->x1, x1);
765d21987d7SWei Hu 	par->y1 = min_t(int, par->y1, y1);
766d21987d7SWei Hu 	par->x2 = max_t(int, par->x2, x2);
767d21987d7SWei Hu 	par->y2 = max_t(int, par->y2, y2);
768d21987d7SWei Hu 
769d21987d7SWei Hu 	/* Schedule a delayed screen update if not yet */
770d21987d7SWei Hu 	if (par->delayed_refresh == false) {
771d21987d7SWei Hu 		schedule_delayed_work(&par->dwork,
772d21987d7SWei Hu 				      HVFB_ONDEMAND_THROTTLE);
773d21987d7SWei Hu 		par->delayed_refresh = true;
774d21987d7SWei Hu 	}
775d21987d7SWei Hu 
776d21987d7SWei Hu 	spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
777f7018c21STomi Valkeinen }
778f7018c21STomi Valkeinen 
7793686fe96SDexuan Cui static int hvfb_on_panic(struct notifier_block *nb,
7803686fe96SDexuan Cui 			 unsigned long e, void *p)
7813686fe96SDexuan Cui {
7821d044ca0SGuilherme G. Piccoli 	struct hv_device *hdev;
7833686fe96SDexuan Cui 	struct hvfb_par *par;
7843686fe96SDexuan Cui 	struct fb_info *info;
7853686fe96SDexuan Cui 
7863686fe96SDexuan Cui 	par = container_of(nb, struct hvfb_par, hvfb_panic_nb);
7873686fe96SDexuan Cui 	info = par->info;
7881d044ca0SGuilherme G. Piccoli 	hdev = device_to_hv_device(info->device);
7891d044ca0SGuilherme G. Piccoli 
7901d044ca0SGuilherme G. Piccoli 	if (hv_ringbuffer_spinlock_busy(hdev->channel))
7911d044ca0SGuilherme G. Piccoli 		return NOTIFY_DONE;
7921d044ca0SGuilherme G. Piccoli 
7931d044ca0SGuilherme G. Piccoli 	par->synchronous_fb = true;
7943a6fb6c4SWei Hu 	if (par->need_docopy)
795d21987d7SWei Hu 		hvfb_docopy(par, 0, dio_fb_size);
796d21987d7SWei Hu 	synthvid_update(info, 0, 0, INT_MAX, INT_MAX);
7973686fe96SDexuan Cui 
7983686fe96SDexuan Cui 	return NOTIFY_DONE;
7993686fe96SDexuan Cui }
800f7018c21STomi Valkeinen 
801f7018c21STomi Valkeinen /* Framebuffer operation handlers */
802f7018c21STomi Valkeinen 
803f7018c21STomi Valkeinen static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
804f7018c21STomi Valkeinen {
805f7018c21STomi Valkeinen 	if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN ||
806f7018c21STomi Valkeinen 	    var->xres > screen_width || var->yres >  screen_height ||
807f7018c21STomi Valkeinen 	    var->bits_per_pixel != screen_depth)
808f7018c21STomi Valkeinen 		return -EINVAL;
809f7018c21STomi Valkeinen 
810f7018c21STomi Valkeinen 	var->xres_virtual = var->xres;
811f7018c21STomi Valkeinen 	var->yres_virtual = var->yres;
812f7018c21STomi Valkeinen 
813f7018c21STomi Valkeinen 	return 0;
814f7018c21STomi Valkeinen }
815f7018c21STomi Valkeinen 
816f7018c21STomi Valkeinen static int hvfb_set_par(struct fb_info *info)
817f7018c21STomi Valkeinen {
818f7018c21STomi Valkeinen 	struct hv_device *hdev = device_to_hv_device(info->device);
819f7018c21STomi Valkeinen 
820f7018c21STomi Valkeinen 	return synthvid_send_situ(hdev);
821f7018c21STomi Valkeinen }
822f7018c21STomi Valkeinen 
823f7018c21STomi Valkeinen 
824f7018c21STomi Valkeinen static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf)
825f7018c21STomi Valkeinen {
826f7018c21STomi Valkeinen 	return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset;
827f7018c21STomi Valkeinen }
828f7018c21STomi Valkeinen 
829f7018c21STomi Valkeinen static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
830f7018c21STomi Valkeinen 			  unsigned blue, unsigned transp, struct fb_info *info)
831f7018c21STomi Valkeinen {
832f7018c21STomi Valkeinen 	u32 *pal = info->pseudo_palette;
833f7018c21STomi Valkeinen 
834f7018c21STomi Valkeinen 	if (regno > 15)
835f7018c21STomi Valkeinen 		return -EINVAL;
836f7018c21STomi Valkeinen 
837f7018c21STomi Valkeinen 	pal[regno] = chan_to_field(red, &info->var.red)
838f7018c21STomi Valkeinen 		| chan_to_field(green, &info->var.green)
839f7018c21STomi Valkeinen 		| chan_to_field(blue, &info->var.blue)
840f7018c21STomi Valkeinen 		| chan_to_field(transp, &info->var.transp);
841f7018c21STomi Valkeinen 
842f7018c21STomi Valkeinen 	return 0;
843f7018c21STomi Valkeinen }
844f7018c21STomi Valkeinen 
845f7018c21STomi Valkeinen static int hvfb_blank(int blank, struct fb_info *info)
846f7018c21STomi Valkeinen {
847f7018c21STomi Valkeinen 	return 1;	/* get fb_blank to set the colormap to all black */
848f7018c21STomi Valkeinen }
849f7018c21STomi Valkeinen 
85066a749a7SThomas Zimmermann static void hvfb_ops_damage_range(struct fb_info *info, off_t off, size_t len)
8513686fe96SDexuan Cui {
85266a749a7SThomas Zimmermann 	/* TODO: implement damage handling */
8533686fe96SDexuan Cui }
8543686fe96SDexuan Cui 
85566a749a7SThomas Zimmermann static void hvfb_ops_damage_area(struct fb_info *info, u32 x, u32 y, u32 width, u32 height)
8563686fe96SDexuan Cui {
85766a749a7SThomas Zimmermann 	struct hvfb_par *par = info->par;
8583686fe96SDexuan Cui 
8593686fe96SDexuan Cui 	if (par->synchronous_fb)
86066a749a7SThomas Zimmermann 		synthvid_update(info, 0, 0, INT_MAX, INT_MAX);
861d21987d7SWei Hu 	else
86266a749a7SThomas Zimmermann 		hvfb_ondemand_refresh_throttle(par, x, y, width, height);
8633686fe96SDexuan Cui }
8643686fe96SDexuan Cui 
86566a749a7SThomas Zimmermann /*
86666a749a7SThomas Zimmermann  * TODO: GEN1 codepaths allocate from system or DMA-able memory. Fix the
86766a749a7SThomas Zimmermann  *       driver to use the _SYSMEM_ or _DMAMEM_ helpers in these cases.
86866a749a7SThomas Zimmermann  */
86966a749a7SThomas Zimmermann FB_GEN_DEFAULT_DEFERRED_IOMEM_OPS(hvfb_ops,
87066a749a7SThomas Zimmermann 				  hvfb_ops_damage_range,
87166a749a7SThomas Zimmermann 				  hvfb_ops_damage_area)
8723686fe96SDexuan Cui 
8738a48ac33SJani Nikula static const struct fb_ops hvfb_ops = {
874f7018c21STomi Valkeinen 	.owner = THIS_MODULE,
87566a749a7SThomas Zimmermann 	FB_DEFAULT_DEFERRED_OPS(hvfb_ops),
876f7018c21STomi Valkeinen 	.fb_check_var = hvfb_check_var,
877f7018c21STomi Valkeinen 	.fb_set_par = hvfb_set_par,
878f7018c21STomi Valkeinen 	.fb_setcolreg = hvfb_setcolreg,
879f7018c21STomi Valkeinen 	.fb_blank = hvfb_blank,
880f7018c21STomi Valkeinen };
881f7018c21STomi Valkeinen 
882f7018c21STomi Valkeinen /* Get options from kernel paramenter "video=" */
883f7018c21STomi Valkeinen static void hvfb_get_option(struct fb_info *info)
884f7018c21STomi Valkeinen {
885f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
886f7018c21STomi Valkeinen 	char *opt = NULL, *p;
887f7018c21STomi Valkeinen 	uint x = 0, y = 0;
888f7018c21STomi Valkeinen 
889f7018c21STomi Valkeinen 	if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt)
890f7018c21STomi Valkeinen 		return;
891f7018c21STomi Valkeinen 
892f7018c21STomi Valkeinen 	p = strsep(&opt, "x");
893f7018c21STomi Valkeinen 	if (!*p || kstrtouint(p, 0, &x) ||
894f7018c21STomi Valkeinen 	    !opt || !*opt || kstrtouint(opt, 0, &y)) {
895f7018c21STomi Valkeinen 		pr_err("Screen option is invalid: skipped\n");
896f7018c21STomi Valkeinen 		return;
897f7018c21STomi Valkeinen 	}
898f7018c21STomi Valkeinen 
899f7018c21STomi Valkeinen 	if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN ||
90067e7cdb4SWei Hu 	    (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10) &&
9019ff5549bSMichael Kelley 	    (x * y * screen_depth / 8 > screen_fb_size)) ||
902f7018c21STomi Valkeinen 	    (par->synthvid_version == SYNTHVID_VERSION_WIN8 &&
903b0cce4f6SMichael Kelley 	     x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8)) {
904f7018c21STomi Valkeinen 		pr_err("Screen resolution option is out of range: skipped\n");
905f7018c21STomi Valkeinen 		return;
906f7018c21STomi Valkeinen 	}
907f7018c21STomi Valkeinen 
908f7018c21STomi Valkeinen 	screen_width = x;
909f7018c21STomi Valkeinen 	screen_height = y;
910f7018c21STomi Valkeinen 	return;
911f7018c21STomi Valkeinen }
912f7018c21STomi Valkeinen 
9133a6fb6c4SWei Hu /*
9143a6fb6c4SWei Hu  * Allocate enough contiguous physical memory.
9153a6fb6c4SWei Hu  * Return physical address if succeeded or -1 if failed.
9163a6fb6c4SWei Hu  */
9173a6fb6c4SWei Hu static phys_addr_t hvfb_get_phymem(struct hv_device *hdev,
9183a6fb6c4SWei Hu 				   unsigned int request_size)
9193a6fb6c4SWei Hu {
9203a6fb6c4SWei Hu 	struct page *page = NULL;
9213a6fb6c4SWei Hu 	dma_addr_t dma_handle;
9223a6fb6c4SWei Hu 	void *vmem;
9233a6fb6c4SWei Hu 	phys_addr_t paddr = 0;
9243a6fb6c4SWei Hu 	unsigned int order = get_order(request_size);
9253a6fb6c4SWei Hu 
9263a6fb6c4SWei Hu 	if (request_size == 0)
9273a6fb6c4SWei Hu 		return -1;
9283a6fb6c4SWei Hu 
9295e0a760bSKirill A. Shutemov 	if (order <= MAX_PAGE_ORDER) {
9305e0a760bSKirill A. Shutemov 		/* Call alloc_pages if the size is less than 2^MAX_PAGE_ORDER */
9313a6fb6c4SWei Hu 		page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
9323a6fb6c4SWei Hu 		if (!page)
9333a6fb6c4SWei Hu 			return -1;
9343a6fb6c4SWei Hu 
9353a6fb6c4SWei Hu 		paddr = (page_to_pfn(page) << PAGE_SHIFT);
9363a6fb6c4SWei Hu 	} else {
9373a6fb6c4SWei Hu 		/* Allocate from CMA */
9383a6fb6c4SWei Hu 		hdev->device.coherent_dma_mask = DMA_BIT_MASK(64);
9393a6fb6c4SWei Hu 
9403a6fb6c4SWei Hu 		vmem = dma_alloc_coherent(&hdev->device,
9413a6fb6c4SWei Hu 					  round_up(request_size, PAGE_SIZE),
9423a6fb6c4SWei Hu 					  &dma_handle,
9433a6fb6c4SWei Hu 					  GFP_KERNEL | __GFP_NOWARN);
9443a6fb6c4SWei Hu 
9453a6fb6c4SWei Hu 		if (!vmem)
9463a6fb6c4SWei Hu 			return -1;
9473a6fb6c4SWei Hu 
9483a6fb6c4SWei Hu 		paddr = virt_to_phys(vmem);
9493a6fb6c4SWei Hu 	}
9503a6fb6c4SWei Hu 
9513a6fb6c4SWei Hu 	return paddr;
9523a6fb6c4SWei Hu }
9533a6fb6c4SWei Hu 
9543a6fb6c4SWei Hu /* Release contiguous physical memory */
9553a6fb6c4SWei Hu static void hvfb_release_phymem(struct hv_device *hdev,
9563a6fb6c4SWei Hu 				phys_addr_t paddr, unsigned int size)
9573a6fb6c4SWei Hu {
9583a6fb6c4SWei Hu 	unsigned int order = get_order(size);
9593a6fb6c4SWei Hu 
9605e0a760bSKirill A. Shutemov 	if (order <= MAX_PAGE_ORDER)
9613a6fb6c4SWei Hu 		__free_pages(pfn_to_page(paddr >> PAGE_SHIFT), order);
9623a6fb6c4SWei Hu 	else
9633a6fb6c4SWei Hu 		dma_free_coherent(&hdev->device,
9643a6fb6c4SWei Hu 				  round_up(size, PAGE_SIZE),
9653a6fb6c4SWei Hu 				  phys_to_virt(paddr),
9663a6fb6c4SWei Hu 				  paddr);
9673a6fb6c4SWei Hu }
9683a6fb6c4SWei Hu 
969f7018c21STomi Valkeinen 
970f7018c21STomi Valkeinen /* Get framebuffer memory from Hyper-V video pci space */
97135464483SJake Oshins static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info)
972f7018c21STomi Valkeinen {
973f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
974f7018c21STomi Valkeinen 	struct pci_dev *pdev  = NULL;
975f7018c21STomi Valkeinen 	void __iomem *fb_virt;
976f7018c21STomi Valkeinen 	int gen2vm = efi_enabled(EFI_BOOT);
9770aa0838cSThomas Zimmermann 	resource_size_t base = 0;
9780aa0838cSThomas Zimmermann 	resource_size_t size = 0;
9793a6fb6c4SWei Hu 	phys_addr_t paddr;
980f7018c21STomi Valkeinen 	int ret;
981f7018c21STomi Valkeinen 
9823a6fb6c4SWei Hu 	if (!gen2vm) {
9833a6fb6c4SWei Hu 		pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
9843a6fb6c4SWei Hu 			PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
9853a6fb6c4SWei Hu 		if (!pdev) {
9863a6fb6c4SWei Hu 			pr_err("Unable to find PCI Hyper-V video\n");
9873a6fb6c4SWei Hu 			return -ENODEV;
9883a6fb6c4SWei Hu 		}
9893a6fb6c4SWei Hu 
99081d23934SThomas Zimmermann 		base = pci_resource_start(pdev, 0);
99181d23934SThomas Zimmermann 		size = pci_resource_len(pdev, 0);
9923a6fb6c4SWei Hu 
9933a6fb6c4SWei Hu 		/*
9943a6fb6c4SWei Hu 		 * For Gen 1 VM, we can directly use the contiguous memory
9953a6fb6c4SWei Hu 		 * from VM. If we succeed, deferred IO happens directly
9963a6fb6c4SWei Hu 		 * on this allocated framebuffer memory, avoiding extra
9973a6fb6c4SWei Hu 		 * memory copy.
9983a6fb6c4SWei Hu 		 */
9993a6fb6c4SWei Hu 		paddr = hvfb_get_phymem(hdev, screen_fb_size);
10003a6fb6c4SWei Hu 		if (paddr != (phys_addr_t) -1) {
10013a6fb6c4SWei Hu 			par->mmio_pp = paddr;
10023a6fb6c4SWei Hu 			par->mmio_vp = par->dio_vp = __va(paddr);
10033a6fb6c4SWei Hu 
10043a6fb6c4SWei Hu 			info->fix.smem_start = paddr;
10053a6fb6c4SWei Hu 			info->fix.smem_len = screen_fb_size;
10063a6fb6c4SWei Hu 			info->screen_base = par->mmio_vp;
10073a6fb6c4SWei Hu 			info->screen_size = screen_fb_size;
10083a6fb6c4SWei Hu 
10093a6fb6c4SWei Hu 			par->need_docopy = false;
10103a6fb6c4SWei Hu 			goto getmem_done;
10113a6fb6c4SWei Hu 		}
10123a6fb6c4SWei Hu 		pr_info("Unable to allocate enough contiguous physical memory on Gen 1 VM. Using MMIO instead.\n");
10133a6fb6c4SWei Hu 	}
10143a6fb6c4SWei Hu 
10153a6fb6c4SWei Hu 	/*
10163a6fb6c4SWei Hu 	 * Cannot use the contiguous physical memory.
10173a6fb6c4SWei Hu 	 * Allocate mmio space for framebuffer.
10183a6fb6c4SWei Hu 	 */
1019d21987d7SWei Hu 	dio_fb_size =
1020d21987d7SWei Hu 		screen_width * screen_height * screen_depth / 8;
1021d21987d7SWei Hu 
1022c4b4d704SSaurabh Sengar 	ret = vmbus_allocate_mmio(&par->mem, hdev, 0, -1,
102335464483SJake Oshins 				  screen_fb_size, 0x100000, true);
102435464483SJake Oshins 	if (ret != 0) {
102535464483SJake Oshins 		pr_err("Unable to allocate framebuffer memory\n");
102635464483SJake Oshins 		goto err1;
102735464483SJake Oshins 	}
102835464483SJake Oshins 
10295f1251a4SDexuan Cui 	/*
10305f1251a4SDexuan Cui 	 * Map the VRAM cacheable for performance. This is also required for
10315f1251a4SDexuan Cui 	 * VM Connect to display properly for ARM64 Linux VM, as the host also
10325f1251a4SDexuan Cui 	 * maps the VRAM cacheable.
10335f1251a4SDexuan Cui 	 */
10345f1251a4SDexuan Cui 	fb_virt = ioremap_cache(par->mem->start, screen_fb_size);
1035f7018c21STomi Valkeinen 	if (!fb_virt)
1036f7018c21STomi Valkeinen 		goto err2;
1037f7018c21STomi Valkeinen 
1038d21987d7SWei Hu 	/* Allocate memory for deferred IO */
1039d21987d7SWei Hu 	par->dio_vp = vzalloc(round_up(dio_fb_size, PAGE_SIZE));
1040d21987d7SWei Hu 	if (par->dio_vp == NULL)
1041d21987d7SWei Hu 		goto err3;
1042d21987d7SWei Hu 
1043d21987d7SWei Hu 	/* Physical address of FB device */
1044d21987d7SWei Hu 	par->mmio_pp = par->mem->start;
1045d21987d7SWei Hu 	/* Virtual address of FB device */
1046d21987d7SWei Hu 	par->mmio_vp = (unsigned char *) fb_virt;
1047d21987d7SWei Hu 
104835464483SJake Oshins 	info->fix.smem_start = par->mem->start;
1049d21987d7SWei Hu 	info->fix.smem_len = dio_fb_size;
1050d21987d7SWei Hu 	info->screen_base = par->dio_vp;
1051d21987d7SWei Hu 	info->screen_size = dio_fb_size;
1052f7018c21STomi Valkeinen 
10533a6fb6c4SWei Hu getmem_done:
10540aa0838cSThomas Zimmermann 	if (base && size)
10555fbcc670SDaniel Vetter 		aperture_remove_conflicting_devices(base, size, KBUILD_MODNAME);
10560aa0838cSThomas Zimmermann 	else
10570aa0838cSThomas Zimmermann 		aperture_remove_all_conflicting_devices(KBUILD_MODNAME);
10583cb73bc3SKairui Song 
1059c25a19afSThomas Zimmermann 	if (!gen2vm)
1060a07b50d8SArnd Bergmann 		pci_dev_put(pdev);
1061f7018c21STomi Valkeinen 
1062f7018c21STomi Valkeinen 	return 0;
1063f7018c21STomi Valkeinen 
1064f7018c21STomi Valkeinen err3:
1065f7018c21STomi Valkeinen 	iounmap(fb_virt);
1066f7018c21STomi Valkeinen err2:
1067696ca5e8SJake Oshins 	vmbus_free_mmio(par->mem->start, screen_fb_size);
106835464483SJake Oshins 	par->mem = NULL;
1069f7018c21STomi Valkeinen err1:
1070f7018c21STomi Valkeinen 	if (!gen2vm)
1071f7018c21STomi Valkeinen 		pci_dev_put(pdev);
1072f7018c21STomi Valkeinen 
1073f7018c21STomi Valkeinen 	return -ENOMEM;
1074f7018c21STomi Valkeinen }
1075f7018c21STomi Valkeinen 
1076f7018c21STomi Valkeinen /* Release the framebuffer */
10773a6fb6c4SWei Hu static void hvfb_putmem(struct hv_device *hdev, struct fb_info *info)
1078f7018c21STomi Valkeinen {
1079f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
1080f7018c21STomi Valkeinen 
10813a6fb6c4SWei Hu 	if (par->need_docopy) {
1082d21987d7SWei Hu 		vfree(par->dio_vp);
1083f7018c21STomi Valkeinen 		iounmap(info->screen_base);
1084696ca5e8SJake Oshins 		vmbus_free_mmio(par->mem->start, screen_fb_size);
10853a6fb6c4SWei Hu 	} else {
10863a6fb6c4SWei Hu 		hvfb_release_phymem(hdev, info->fix.smem_start,
10873a6fb6c4SWei Hu 				    screen_fb_size);
10883a6fb6c4SWei Hu 	}
10893a6fb6c4SWei Hu 
109035464483SJake Oshins 	par->mem = NULL;
1091f7018c21STomi Valkeinen }
1092f7018c21STomi Valkeinen 
1093f7018c21STomi Valkeinen 
1094f7018c21STomi Valkeinen static int hvfb_probe(struct hv_device *hdev,
1095f7018c21STomi Valkeinen 		      const struct hv_vmbus_device_id *dev_id)
1096f7018c21STomi Valkeinen {
1097f7018c21STomi Valkeinen 	struct fb_info *info;
1098f7018c21STomi Valkeinen 	struct hvfb_par *par;
1099f7018c21STomi Valkeinen 	int ret;
1100f7018c21STomi Valkeinen 
1101f7018c21STomi Valkeinen 	info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device);
11020adcdbcbSBartlomiej Zolnierkiewicz 	if (!info)
1103f7018c21STomi Valkeinen 		return -ENOMEM;
1104f7018c21STomi Valkeinen 
1105f7018c21STomi Valkeinen 	par = info->par;
1106f7018c21STomi Valkeinen 	par->info = info;
1107f7018c21STomi Valkeinen 	par->fb_ready = false;
11083a6fb6c4SWei Hu 	par->need_docopy = true;
1109f7018c21STomi Valkeinen 	init_completion(&par->wait);
1110f7018c21STomi Valkeinen 	INIT_DELAYED_WORK(&par->dwork, hvfb_update_work);
1111f7018c21STomi Valkeinen 
1112d21987d7SWei Hu 	par->delayed_refresh = false;
1113d21987d7SWei Hu 	spin_lock_init(&par->delayed_refresh_lock);
1114d21987d7SWei Hu 	par->x1 = par->y1 = INT_MAX;
1115d21987d7SWei Hu 	par->x2 = par->y2 = 0;
1116d21987d7SWei Hu 
1117f7018c21STomi Valkeinen 	/* Connect to VSP */
1118f7018c21STomi Valkeinen 	hv_set_drvdata(hdev, info);
1119f7018c21STomi Valkeinen 	ret = synthvid_connect_vsp(hdev);
1120f7018c21STomi Valkeinen 	if (ret) {
1121f7018c21STomi Valkeinen 		pr_err("Unable to connect to VSP\n");
1122f7018c21STomi Valkeinen 		goto error1;
1123f7018c21STomi Valkeinen 	}
1124f7018c21STomi Valkeinen 
112567e7cdb4SWei Hu 	hvfb_get_option(info);
11269ff5549bSMichael Kelley 	pr_info("Screen resolution: %dx%d, Color depth: %d, Frame buffer size: %d\n",
11279ff5549bSMichael Kelley 		screen_width, screen_height, screen_depth, screen_fb_size);
112867e7cdb4SWei Hu 
112935464483SJake Oshins 	ret = hvfb_getmem(hdev, info);
1130f7018c21STomi Valkeinen 	if (ret) {
1131f7018c21STomi Valkeinen 		pr_err("No memory for framebuffer\n");
1132f7018c21STomi Valkeinen 		goto error2;
1133f7018c21STomi Valkeinen 	}
1134f7018c21STomi Valkeinen 
1135f7018c21STomi Valkeinen 	/* Set up fb_info */
1136f7018c21STomi Valkeinen 	info->var.xres_virtual = info->var.xres = screen_width;
1137f7018c21STomi Valkeinen 	info->var.yres_virtual = info->var.yres = screen_height;
1138f7018c21STomi Valkeinen 	info->var.bits_per_pixel = screen_depth;
1139f7018c21STomi Valkeinen 
1140f7018c21STomi Valkeinen 	if (info->var.bits_per_pixel == 16) {
1141f7018c21STomi Valkeinen 		info->var.red = (struct fb_bitfield){11, 5, 0};
1142f7018c21STomi Valkeinen 		info->var.green = (struct fb_bitfield){5, 6, 0};
1143f7018c21STomi Valkeinen 		info->var.blue = (struct fb_bitfield){0, 5, 0};
1144f7018c21STomi Valkeinen 		info->var.transp = (struct fb_bitfield){0, 0, 0};
1145f7018c21STomi Valkeinen 	} else {
1146f7018c21STomi Valkeinen 		info->var.red = (struct fb_bitfield){16, 8, 0};
1147f7018c21STomi Valkeinen 		info->var.green = (struct fb_bitfield){8, 8, 0};
1148f7018c21STomi Valkeinen 		info->var.blue = (struct fb_bitfield){0, 8, 0};
1149f7018c21STomi Valkeinen 		info->var.transp = (struct fb_bitfield){24, 8, 0};
1150f7018c21STomi Valkeinen 	}
1151f7018c21STomi Valkeinen 
1152f7018c21STomi Valkeinen 	info->var.activate = FB_ACTIVATE_NOW;
1153f7018c21STomi Valkeinen 	info->var.height = -1;
1154f7018c21STomi Valkeinen 	info->var.width = -1;
1155f7018c21STomi Valkeinen 	info->var.vmode = FB_VMODE_NONINTERLACED;
1156f7018c21STomi Valkeinen 
1157f7018c21STomi Valkeinen 	strcpy(info->fix.id, KBUILD_MODNAME);
1158f7018c21STomi Valkeinen 	info->fix.type = FB_TYPE_PACKED_PIXELS;
1159f7018c21STomi Valkeinen 	info->fix.visual = FB_VISUAL_TRUECOLOR;
1160f7018c21STomi Valkeinen 	info->fix.line_length = screen_width * screen_depth / 8;
1161f7018c21STomi Valkeinen 	info->fix.accel = FB_ACCEL_NONE;
1162f7018c21STomi Valkeinen 
1163f7018c21STomi Valkeinen 	info->fbops = &hvfb_ops;
1164f7018c21STomi Valkeinen 	info->pseudo_palette = par->pseudo_palette;
1165f7018c21STomi Valkeinen 
1166d21987d7SWei Hu 	/* Initialize deferred IO */
1167d21987d7SWei Hu 	info->fbdefio = &synthvid_defio;
1168d21987d7SWei Hu 	fb_deferred_io_init(info);
1169d21987d7SWei Hu 
1170f7018c21STomi Valkeinen 	/* Send config to host */
1171f7018c21STomi Valkeinen 	ret = synthvid_send_config(hdev);
1172f7018c21STomi Valkeinen 	if (ret)
1173f7018c21STomi Valkeinen 		goto error;
1174f7018c21STomi Valkeinen 
1175f7018c21STomi Valkeinen 	ret = register_framebuffer(info);
1176f7018c21STomi Valkeinen 	if (ret) {
1177f7018c21STomi Valkeinen 		pr_err("Unable to register framebuffer\n");
1178f7018c21STomi Valkeinen 		goto error;
1179f7018c21STomi Valkeinen 	}
1180f7018c21STomi Valkeinen 
1181f7018c21STomi Valkeinen 	par->fb_ready = true;
1182f7018c21STomi Valkeinen 
11833686fe96SDexuan Cui 	par->synchronous_fb = false;
1184d786e00dSGuilherme G. Piccoli 
1185d786e00dSGuilherme G. Piccoli 	/*
1186d786e00dSGuilherme G. Piccoli 	 * We need to be sure this panic notifier runs _before_ the
1187d786e00dSGuilherme G. Piccoli 	 * vmbus disconnect, so order it by priority. It must execute
1188d786e00dSGuilherme G. Piccoli 	 * before the function hv_panic_vmbus_unload() [drivers/hv/vmbus_drv.c],
1189d786e00dSGuilherme G. Piccoli 	 * which is almost at the end of list, with priority = INT_MIN + 1.
1190d786e00dSGuilherme G. Piccoli 	 */
11913686fe96SDexuan Cui 	par->hvfb_panic_nb.notifier_call = hvfb_on_panic;
1192*27f22f89SChen Ni 	par->hvfb_panic_nb.priority = INT_MIN + 10;
11933686fe96SDexuan Cui 	atomic_notifier_chain_register(&panic_notifier_list,
11943686fe96SDexuan Cui 				       &par->hvfb_panic_nb);
11953686fe96SDexuan Cui 
1196f7018c21STomi Valkeinen 	return 0;
1197f7018c21STomi Valkeinen 
1198f7018c21STomi Valkeinen error:
1199d21987d7SWei Hu 	fb_deferred_io_cleanup(info);
12003a6fb6c4SWei Hu 	hvfb_putmem(hdev, info);
1201f7018c21STomi Valkeinen error2:
1202f7018c21STomi Valkeinen 	vmbus_close(hdev->channel);
1203f7018c21STomi Valkeinen error1:
1204f7018c21STomi Valkeinen 	cancel_delayed_work_sync(&par->dwork);
1205f7018c21STomi Valkeinen 	hv_set_drvdata(hdev, NULL);
1206f7018c21STomi Valkeinen 	framebuffer_release(info);
1207f7018c21STomi Valkeinen 	return ret;
1208f7018c21STomi Valkeinen }
1209f7018c21STomi Valkeinen 
121096ec2939SDawei Li static void hvfb_remove(struct hv_device *hdev)
1211f7018c21STomi Valkeinen {
1212f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
1213f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
1214f7018c21STomi Valkeinen 
12153686fe96SDexuan Cui 	atomic_notifier_chain_unregister(&panic_notifier_list,
12163686fe96SDexuan Cui 					 &par->hvfb_panic_nb);
12173686fe96SDexuan Cui 
1218f7018c21STomi Valkeinen 	par->update = false;
1219f7018c21STomi Valkeinen 	par->fb_ready = false;
1220f7018c21STomi Valkeinen 
1221d21987d7SWei Hu 	fb_deferred_io_cleanup(info);
1222d21987d7SWei Hu 
1223f7018c21STomi Valkeinen 	unregister_framebuffer(info);
1224f7018c21STomi Valkeinen 	cancel_delayed_work_sync(&par->dwork);
1225f7018c21STomi Valkeinen 
1226f7018c21STomi Valkeinen 	vmbus_close(hdev->channel);
1227f7018c21STomi Valkeinen 	hv_set_drvdata(hdev, NULL);
1228f7018c21STomi Valkeinen 
12293a6fb6c4SWei Hu 	hvfb_putmem(hdev, info);
1230f7018c21STomi Valkeinen 	framebuffer_release(info);
1231f7018c21STomi Valkeinen }
1232f7018c21STomi Valkeinen 
12331ecf3020SDexuan Cui static int hvfb_suspend(struct hv_device *hdev)
12341ecf3020SDexuan Cui {
12351ecf3020SDexuan Cui 	struct fb_info *info = hv_get_drvdata(hdev);
12361ecf3020SDexuan Cui 	struct hvfb_par *par = info->par;
12371ecf3020SDexuan Cui 
12381ecf3020SDexuan Cui 	console_lock();
12391ecf3020SDexuan Cui 
12401ecf3020SDexuan Cui 	/* 1 means do suspend */
12411ecf3020SDexuan Cui 	fb_set_suspend(info, 1);
12421ecf3020SDexuan Cui 
12431ecf3020SDexuan Cui 	cancel_delayed_work_sync(&par->dwork);
1244382a4622SDexuan Cui 	cancel_delayed_work_sync(&info->deferred_work);
12451ecf3020SDexuan Cui 
12461ecf3020SDexuan Cui 	par->update_saved = par->update;
12471ecf3020SDexuan Cui 	par->update = false;
12481ecf3020SDexuan Cui 	par->fb_ready = false;
12491ecf3020SDexuan Cui 
12501ecf3020SDexuan Cui 	vmbus_close(hdev->channel);
12511ecf3020SDexuan Cui 
12521ecf3020SDexuan Cui 	console_unlock();
12531ecf3020SDexuan Cui 
12541ecf3020SDexuan Cui 	return 0;
12551ecf3020SDexuan Cui }
12561ecf3020SDexuan Cui 
12571ecf3020SDexuan Cui static int hvfb_resume(struct hv_device *hdev)
12581ecf3020SDexuan Cui {
12591ecf3020SDexuan Cui 	struct fb_info *info = hv_get_drvdata(hdev);
12601ecf3020SDexuan Cui 	struct hvfb_par *par = info->par;
12611ecf3020SDexuan Cui 	int ret;
12621ecf3020SDexuan Cui 
12631ecf3020SDexuan Cui 	console_lock();
12641ecf3020SDexuan Cui 
12651ecf3020SDexuan Cui 	ret = synthvid_connect_vsp(hdev);
12661ecf3020SDexuan Cui 	if (ret != 0)
12671ecf3020SDexuan Cui 		goto out;
12681ecf3020SDexuan Cui 
12691ecf3020SDexuan Cui 	ret = synthvid_send_config(hdev);
12701ecf3020SDexuan Cui 	if (ret != 0) {
12711ecf3020SDexuan Cui 		vmbus_close(hdev->channel);
12721ecf3020SDexuan Cui 		goto out;
12731ecf3020SDexuan Cui 	}
12741ecf3020SDexuan Cui 
12751ecf3020SDexuan Cui 	par->fb_ready = true;
12761ecf3020SDexuan Cui 	par->update = par->update_saved;
12771ecf3020SDexuan Cui 
1278382a4622SDexuan Cui 	schedule_delayed_work(&info->deferred_work, info->fbdefio->delay);
12791ecf3020SDexuan Cui 	schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
12801ecf3020SDexuan Cui 
12811ecf3020SDexuan Cui 	/* 0 means do resume */
12821ecf3020SDexuan Cui 	fb_set_suspend(info, 0);
12831ecf3020SDexuan Cui 
12841ecf3020SDexuan Cui out:
12851ecf3020SDexuan Cui 	console_unlock();
12861ecf3020SDexuan Cui 
12871ecf3020SDexuan Cui 	return ret;
12881ecf3020SDexuan Cui }
12891ecf3020SDexuan Cui 
1290f7018c21STomi Valkeinen 
12919baa3c34SBenoit Taine static const struct pci_device_id pci_stub_id_table[] = {
1292f7018c21STomi Valkeinen 	{
1293f7018c21STomi Valkeinen 		.vendor      = PCI_VENDOR_ID_MICROSOFT,
1294f7018c21STomi Valkeinen 		.device      = PCI_DEVICE_ID_HYPERV_VIDEO,
1295f7018c21STomi Valkeinen 	},
1296f7018c21STomi Valkeinen 	{ /* end of list */ }
1297f7018c21STomi Valkeinen };
1298f7018c21STomi Valkeinen 
1299f7018c21STomi Valkeinen static const struct hv_vmbus_device_id id_table[] = {
1300f7018c21STomi Valkeinen 	/* Synthetic Video Device GUID */
1301f7018c21STomi Valkeinen 	{HV_SYNTHVID_GUID},
1302f7018c21STomi Valkeinen 	{}
1303f7018c21STomi Valkeinen };
1304f7018c21STomi Valkeinen 
1305f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(pci, pci_stub_id_table);
1306f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(vmbus, id_table);
1307f7018c21STomi Valkeinen 
1308f7018c21STomi Valkeinen static struct hv_driver hvfb_drv = {
1309f7018c21STomi Valkeinen 	.name = KBUILD_MODNAME,
1310f7018c21STomi Valkeinen 	.id_table = id_table,
1311f7018c21STomi Valkeinen 	.probe = hvfb_probe,
1312f7018c21STomi Valkeinen 	.remove = hvfb_remove,
13131ecf3020SDexuan Cui 	.suspend = hvfb_suspend,
13141ecf3020SDexuan Cui 	.resume = hvfb_resume,
1315af0a5646SArjan van de Ven 	.driver = {
1316af0a5646SArjan van de Ven 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1317af0a5646SArjan van de Ven 	},
1318f7018c21STomi Valkeinen };
1319f7018c21STomi Valkeinen 
1320f7018c21STomi Valkeinen static int hvfb_pci_stub_probe(struct pci_dev *pdev,
1321f7018c21STomi Valkeinen 			       const struct pci_device_id *ent)
1322f7018c21STomi Valkeinen {
1323f7018c21STomi Valkeinen 	return 0;
1324f7018c21STomi Valkeinen }
1325f7018c21STomi Valkeinen 
1326f7018c21STomi Valkeinen static void hvfb_pci_stub_remove(struct pci_dev *pdev)
1327f7018c21STomi Valkeinen {
1328f7018c21STomi Valkeinen }
1329f7018c21STomi Valkeinen 
1330f7018c21STomi Valkeinen static struct pci_driver hvfb_pci_stub_driver = {
1331f7018c21STomi Valkeinen 	.name =		KBUILD_MODNAME,
1332f7018c21STomi Valkeinen 	.id_table =	pci_stub_id_table,
1333f7018c21STomi Valkeinen 	.probe =	hvfb_pci_stub_probe,
1334f7018c21STomi Valkeinen 	.remove =	hvfb_pci_stub_remove,
1335af0a5646SArjan van de Ven 	.driver = {
1336af0a5646SArjan van de Ven 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1337af0a5646SArjan van de Ven 	}
1338f7018c21STomi Valkeinen };
1339f7018c21STomi Valkeinen 
1340f7018c21STomi Valkeinen static int __init hvfb_drv_init(void)
1341f7018c21STomi Valkeinen {
1342f7018c21STomi Valkeinen 	int ret;
1343f7018c21STomi Valkeinen 
13440ba2fa8cSThomas Zimmermann 	if (fb_modesetting_disabled("hyper_fb"))
13450ba2fa8cSThomas Zimmermann 		return -ENODEV;
13460ba2fa8cSThomas Zimmermann 
1347f7018c21STomi Valkeinen 	ret = vmbus_driver_register(&hvfb_drv);
1348f7018c21STomi Valkeinen 	if (ret != 0)
1349f7018c21STomi Valkeinen 		return ret;
1350f7018c21STomi Valkeinen 
1351f7018c21STomi Valkeinen 	ret = pci_register_driver(&hvfb_pci_stub_driver);
1352f7018c21STomi Valkeinen 	if (ret != 0) {
1353f7018c21STomi Valkeinen 		vmbus_driver_unregister(&hvfb_drv);
1354f7018c21STomi Valkeinen 		return ret;
1355f7018c21STomi Valkeinen 	}
1356f7018c21STomi Valkeinen 
1357f7018c21STomi Valkeinen 	return 0;
1358f7018c21STomi Valkeinen }
1359f7018c21STomi Valkeinen 
1360f7018c21STomi Valkeinen static void __exit hvfb_drv_exit(void)
1361f7018c21STomi Valkeinen {
1362f7018c21STomi Valkeinen 	pci_unregister_driver(&hvfb_pci_stub_driver);
1363f7018c21STomi Valkeinen 	vmbus_driver_unregister(&hvfb_drv);
1364f7018c21STomi Valkeinen }
1365f7018c21STomi Valkeinen 
1366f7018c21STomi Valkeinen module_init(hvfb_drv_init);
1367f7018c21STomi Valkeinen module_exit(hvfb_drv_exit);
1368f7018c21STomi Valkeinen 
1369f7018c21STomi Valkeinen MODULE_LICENSE("GPL");
1370f7018c21STomi Valkeinen MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver");
1371