xref: /linux/drivers/video/fbdev/hyperv_fb.c (revision a07b50d80ab621f4f18d429068a43cffec26691f)
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>
51f1b215fdSThomas Zimmermann #include <linux/screen_info.h>
5234a28083SOlaf Hering #include <linux/vmalloc.h>
53f7018c21STomi Valkeinen #include <linux/init.h>
54f7018c21STomi Valkeinen #include <linux/completion.h>
55f7018c21STomi Valkeinen #include <linux/fb.h>
56f7018c21STomi Valkeinen #include <linux/pci.h>
57f39650deSAndy Shevchenko #include <linux/panic_notifier.h>
58f7018c21STomi Valkeinen #include <linux/efi.h>
591ecf3020SDexuan Cui #include <linux/console.h>
60f7018c21STomi Valkeinen 
61f7018c21STomi Valkeinen #include <linux/hyperv.h>
62f7018c21STomi Valkeinen 
63f7018c21STomi Valkeinen /* Hyper-V Synthetic Video Protocol definitions and structures */
64f7018c21STomi Valkeinen #define MAX_VMBUS_PKT_SIZE 0x4000
65f7018c21STomi Valkeinen 
66f7018c21STomi Valkeinen #define SYNTHVID_VERSION(major, minor) ((minor) << 16 | (major))
67b0cce4f6SMichael Kelley /* Support for VERSION_WIN7 is removed. #define is retained for reference. */
68f7018c21STomi Valkeinen #define SYNTHVID_VERSION_WIN7 SYNTHVID_VERSION(3, 0)
69f7018c21STomi Valkeinen #define SYNTHVID_VERSION_WIN8 SYNTHVID_VERSION(3, 2)
7067e7cdb4SWei Hu #define SYNTHVID_VERSION_WIN10 SYNTHVID_VERSION(3, 5)
7167e7cdb4SWei Hu 
7267e7cdb4SWei Hu #define SYNTHVID_VER_GET_MAJOR(ver) (ver & 0x0000ffff)
7367e7cdb4SWei Hu #define SYNTHVID_VER_GET_MINOR(ver) ((ver & 0xffff0000) >> 16)
74f7018c21STomi Valkeinen 
75f7018c21STomi Valkeinen #define SYNTHVID_DEPTH_WIN8 32
76f7018c21STomi Valkeinen #define SYNTHVID_FB_SIZE_WIN8 (8 * 1024 * 1024)
77f7018c21STomi Valkeinen 
78f7018c21STomi Valkeinen enum pipe_msg_type {
79f7018c21STomi Valkeinen 	PIPE_MSG_INVALID,
80f7018c21STomi Valkeinen 	PIPE_MSG_DATA,
81f7018c21STomi Valkeinen 	PIPE_MSG_MAX
82f7018c21STomi Valkeinen };
83f7018c21STomi Valkeinen 
84f7018c21STomi Valkeinen struct pipe_msg_hdr {
85f7018c21STomi Valkeinen 	u32 type;
86f7018c21STomi Valkeinen 	u32 size; /* size of message after this field */
87f7018c21STomi Valkeinen } __packed;
88f7018c21STomi Valkeinen 
89f7018c21STomi Valkeinen 
90f7018c21STomi Valkeinen enum synthvid_msg_type {
91f7018c21STomi Valkeinen 	SYNTHVID_ERROR			= 0,
92f7018c21STomi Valkeinen 	SYNTHVID_VERSION_REQUEST	= 1,
93f7018c21STomi Valkeinen 	SYNTHVID_VERSION_RESPONSE	= 2,
94f7018c21STomi Valkeinen 	SYNTHVID_VRAM_LOCATION		= 3,
95f7018c21STomi Valkeinen 	SYNTHVID_VRAM_LOCATION_ACK	= 4,
96f7018c21STomi Valkeinen 	SYNTHVID_SITUATION_UPDATE	= 5,
97f7018c21STomi Valkeinen 	SYNTHVID_SITUATION_UPDATE_ACK	= 6,
98f7018c21STomi Valkeinen 	SYNTHVID_POINTER_POSITION	= 7,
99f7018c21STomi Valkeinen 	SYNTHVID_POINTER_SHAPE		= 8,
100f7018c21STomi Valkeinen 	SYNTHVID_FEATURE_CHANGE		= 9,
101f7018c21STomi Valkeinen 	SYNTHVID_DIRT			= 10,
10267e7cdb4SWei Hu 	SYNTHVID_RESOLUTION_REQUEST	= 13,
10367e7cdb4SWei Hu 	SYNTHVID_RESOLUTION_RESPONSE	= 14,
104f7018c21STomi Valkeinen 
10567e7cdb4SWei Hu 	SYNTHVID_MAX			= 15
106f7018c21STomi Valkeinen };
107f7018c21STomi Valkeinen 
10867e7cdb4SWei Hu #define		SYNTHVID_EDID_BLOCK_SIZE	128
10967e7cdb4SWei Hu #define		SYNTHVID_MAX_RESOLUTION_COUNT	64
11067e7cdb4SWei Hu 
11167e7cdb4SWei Hu struct hvd_screen_info {
11267e7cdb4SWei Hu 	u16 width;
11367e7cdb4SWei Hu 	u16 height;
11467e7cdb4SWei Hu } __packed;
11567e7cdb4SWei Hu 
116f7018c21STomi Valkeinen struct synthvid_msg_hdr {
117f7018c21STomi Valkeinen 	u32 type;
118f7018c21STomi Valkeinen 	u32 size;  /* size of this header + payload after this field*/
119f7018c21STomi Valkeinen } __packed;
120f7018c21STomi Valkeinen 
121f7018c21STomi Valkeinen struct synthvid_version_req {
122f7018c21STomi Valkeinen 	u32 version;
123f7018c21STomi Valkeinen } __packed;
124f7018c21STomi Valkeinen 
125f7018c21STomi Valkeinen struct synthvid_version_resp {
126f7018c21STomi Valkeinen 	u32 version;
127f7018c21STomi Valkeinen 	u8 is_accepted;
128f7018c21STomi Valkeinen 	u8 max_video_outputs;
129f7018c21STomi Valkeinen } __packed;
130f7018c21STomi Valkeinen 
13167e7cdb4SWei Hu struct synthvid_supported_resolution_req {
13267e7cdb4SWei Hu 	u8 maximum_resolution_count;
13367e7cdb4SWei Hu } __packed;
13467e7cdb4SWei Hu 
13567e7cdb4SWei Hu struct synthvid_supported_resolution_resp {
13667e7cdb4SWei Hu 	u8 edid_block[SYNTHVID_EDID_BLOCK_SIZE];
13767e7cdb4SWei Hu 	u8 resolution_count;
13867e7cdb4SWei Hu 	u8 default_resolution_index;
13967e7cdb4SWei Hu 	u8 is_standard;
14067e7cdb4SWei Hu 	struct hvd_screen_info
14167e7cdb4SWei Hu 		supported_resolution[SYNTHVID_MAX_RESOLUTION_COUNT];
14267e7cdb4SWei Hu } __packed;
14367e7cdb4SWei Hu 
144f7018c21STomi Valkeinen struct synthvid_vram_location {
145f7018c21STomi Valkeinen 	u64 user_ctx;
146f7018c21STomi Valkeinen 	u8 is_vram_gpa_specified;
147f7018c21STomi Valkeinen 	u64 vram_gpa;
148f7018c21STomi Valkeinen } __packed;
149f7018c21STomi Valkeinen 
150f7018c21STomi Valkeinen struct synthvid_vram_location_ack {
151f7018c21STomi Valkeinen 	u64 user_ctx;
152f7018c21STomi Valkeinen } __packed;
153f7018c21STomi Valkeinen 
154f7018c21STomi Valkeinen struct video_output_situation {
155f7018c21STomi Valkeinen 	u8 active;
156f7018c21STomi Valkeinen 	u32 vram_offset;
157f7018c21STomi Valkeinen 	u8 depth_bits;
158f7018c21STomi Valkeinen 	u32 width_pixels;
159f7018c21STomi Valkeinen 	u32 height_pixels;
160f7018c21STomi Valkeinen 	u32 pitch_bytes;
161f7018c21STomi Valkeinen } __packed;
162f7018c21STomi Valkeinen 
163f7018c21STomi Valkeinen struct synthvid_situation_update {
164f7018c21STomi Valkeinen 	u64 user_ctx;
165f7018c21STomi Valkeinen 	u8 video_output_count;
166f7018c21STomi Valkeinen 	struct video_output_situation video_output[1];
167f7018c21STomi Valkeinen } __packed;
168f7018c21STomi Valkeinen 
169f7018c21STomi Valkeinen struct synthvid_situation_update_ack {
170f7018c21STomi Valkeinen 	u64 user_ctx;
171f7018c21STomi Valkeinen } __packed;
172f7018c21STomi Valkeinen 
173f7018c21STomi Valkeinen struct synthvid_pointer_position {
174f7018c21STomi Valkeinen 	u8 is_visible;
175f7018c21STomi Valkeinen 	u8 video_output;
176f7018c21STomi Valkeinen 	s32 image_x;
177f7018c21STomi Valkeinen 	s32 image_y;
178f7018c21STomi Valkeinen } __packed;
179f7018c21STomi Valkeinen 
180f7018c21STomi Valkeinen 
181f7018c21STomi Valkeinen #define CURSOR_MAX_X 96
182f7018c21STomi Valkeinen #define CURSOR_MAX_Y 96
183f7018c21STomi Valkeinen #define CURSOR_ARGB_PIXEL_SIZE 4
184f7018c21STomi Valkeinen #define CURSOR_MAX_SIZE (CURSOR_MAX_X * CURSOR_MAX_Y * CURSOR_ARGB_PIXEL_SIZE)
185f7018c21STomi Valkeinen #define CURSOR_COMPLETE (-1)
186f7018c21STomi Valkeinen 
187f7018c21STomi Valkeinen struct synthvid_pointer_shape {
188f7018c21STomi Valkeinen 	u8 part_idx;
189f7018c21STomi Valkeinen 	u8 is_argb;
190f7018c21STomi Valkeinen 	u32 width; /* CURSOR_MAX_X at most */
191f7018c21STomi Valkeinen 	u32 height; /* CURSOR_MAX_Y at most */
192f7018c21STomi Valkeinen 	u32 hot_x; /* hotspot relative to upper-left of pointer image */
193f7018c21STomi Valkeinen 	u32 hot_y;
194f7018c21STomi Valkeinen 	u8 data[4];
195f7018c21STomi Valkeinen } __packed;
196f7018c21STomi Valkeinen 
197f7018c21STomi Valkeinen struct synthvid_feature_change {
198f7018c21STomi Valkeinen 	u8 is_dirt_needed;
199f7018c21STomi Valkeinen 	u8 is_ptr_pos_needed;
200f7018c21STomi Valkeinen 	u8 is_ptr_shape_needed;
201f7018c21STomi Valkeinen 	u8 is_situ_needed;
202f7018c21STomi Valkeinen } __packed;
203f7018c21STomi Valkeinen 
204f7018c21STomi Valkeinen struct rect {
205f7018c21STomi Valkeinen 	s32 x1, y1; /* top left corner */
206f7018c21STomi Valkeinen 	s32 x2, y2; /* bottom right corner, exclusive */
207f7018c21STomi Valkeinen } __packed;
208f7018c21STomi Valkeinen 
209f7018c21STomi Valkeinen struct synthvid_dirt {
210f7018c21STomi Valkeinen 	u8 video_output;
211f7018c21STomi Valkeinen 	u8 dirt_count;
212f7018c21STomi Valkeinen 	struct rect rect[1];
213f7018c21STomi Valkeinen } __packed;
214f7018c21STomi Valkeinen 
215f7018c21STomi Valkeinen struct synthvid_msg {
216f7018c21STomi Valkeinen 	struct pipe_msg_hdr pipe_hdr;
217f7018c21STomi Valkeinen 	struct synthvid_msg_hdr vid_hdr;
218f7018c21STomi Valkeinen 	union {
219f7018c21STomi Valkeinen 		struct synthvid_version_req ver_req;
220f7018c21STomi Valkeinen 		struct synthvid_version_resp ver_resp;
221f7018c21STomi Valkeinen 		struct synthvid_vram_location vram;
222f7018c21STomi Valkeinen 		struct synthvid_vram_location_ack vram_ack;
223f7018c21STomi Valkeinen 		struct synthvid_situation_update situ;
224f7018c21STomi Valkeinen 		struct synthvid_situation_update_ack situ_ack;
225f7018c21STomi Valkeinen 		struct synthvid_pointer_position ptr_pos;
226f7018c21STomi Valkeinen 		struct synthvid_pointer_shape ptr_shape;
227f7018c21STomi Valkeinen 		struct synthvid_feature_change feature_chg;
228f7018c21STomi Valkeinen 		struct synthvid_dirt dirt;
22967e7cdb4SWei Hu 		struct synthvid_supported_resolution_req resolution_req;
23067e7cdb4SWei Hu 		struct synthvid_supported_resolution_resp resolution_resp;
231f7018c21STomi Valkeinen 	};
232f7018c21STomi Valkeinen } __packed;
233f7018c21STomi Valkeinen 
234f7018c21STomi Valkeinen 
235f7018c21STomi Valkeinen /* FB driver definitions and structures */
236f7018c21STomi Valkeinen #define HVFB_WIDTH 1152 /* default screen width */
237f7018c21STomi Valkeinen #define HVFB_HEIGHT 864 /* default screen height */
238f7018c21STomi Valkeinen #define HVFB_WIDTH_MIN 640
239f7018c21STomi Valkeinen #define HVFB_HEIGHT_MIN 480
240f7018c21STomi Valkeinen 
241f7018c21STomi Valkeinen #define RING_BUFSIZE (256 * 1024)
242f7018c21STomi Valkeinen #define VSP_TIMEOUT (10 * HZ)
243f7018c21STomi Valkeinen #define HVFB_UPDATE_DELAY (HZ / 20)
244d21987d7SWei Hu #define HVFB_ONDEMAND_THROTTLE (HZ / 20)
245f7018c21STomi Valkeinen 
246f7018c21STomi Valkeinen struct hvfb_par {
247f7018c21STomi Valkeinen 	struct fb_info *info;
24835464483SJake Oshins 	struct resource *mem;
249f7018c21STomi Valkeinen 	bool fb_ready; /* fb device is ready */
250f7018c21STomi Valkeinen 	struct completion wait;
251f7018c21STomi Valkeinen 	u32 synthvid_version;
252f7018c21STomi Valkeinen 
253f7018c21STomi Valkeinen 	struct delayed_work dwork;
254f7018c21STomi Valkeinen 	bool update;
2551ecf3020SDexuan Cui 	bool update_saved; /* The value of 'update' before hibernation */
256f7018c21STomi Valkeinen 
257f7018c21STomi Valkeinen 	u32 pseudo_palette[16];
258f7018c21STomi Valkeinen 	u8 init_buf[MAX_VMBUS_PKT_SIZE];
259f7018c21STomi Valkeinen 	u8 recv_buf[MAX_VMBUS_PKT_SIZE];
2603686fe96SDexuan Cui 
2613686fe96SDexuan Cui 	/* If true, the VSC notifies the VSP on every framebuffer change */
2623686fe96SDexuan Cui 	bool synchronous_fb;
2633686fe96SDexuan Cui 
2643a6fb6c4SWei Hu 	/* If true, need to copy from deferred IO mem to framebuffer mem */
2653a6fb6c4SWei Hu 	bool need_docopy;
2663a6fb6c4SWei Hu 
2673686fe96SDexuan Cui 	struct notifier_block hvfb_panic_nb;
268d21987d7SWei Hu 
269d21987d7SWei Hu 	/* Memory for deferred IO and frame buffer itself */
270d21987d7SWei Hu 	unsigned char *dio_vp;
271d21987d7SWei Hu 	unsigned char *mmio_vp;
2723a6fb6c4SWei Hu 	phys_addr_t mmio_pp;
273d21987d7SWei Hu 
274d21987d7SWei Hu 	/* Dirty rectangle, protected by delayed_refresh_lock */
275d21987d7SWei Hu 	int x1, y1, x2, y2;
276d21987d7SWei Hu 	bool delayed_refresh;
277d21987d7SWei Hu 	spinlock_t delayed_refresh_lock;
278f7018c21STomi Valkeinen };
279f7018c21STomi Valkeinen 
280f7018c21STomi Valkeinen static uint screen_width = HVFB_WIDTH;
281f7018c21STomi Valkeinen static uint screen_height = HVFB_HEIGHT;
282f7018c21STomi Valkeinen static uint screen_depth;
283f7018c21STomi Valkeinen static uint screen_fb_size;
284d21987d7SWei Hu static uint dio_fb_size; /* FB size for deferred IO */
285f7018c21STomi Valkeinen 
286f7018c21STomi Valkeinen /* Send message to Hyper-V host */
287f7018c21STomi Valkeinen static inline int synthvid_send(struct hv_device *hdev,
288f7018c21STomi Valkeinen 				struct synthvid_msg *msg)
289f7018c21STomi Valkeinen {
290f7018c21STomi Valkeinen 	static atomic64_t request_id = ATOMIC64_INIT(0);
291f7018c21STomi Valkeinen 	int ret;
292f7018c21STomi Valkeinen 
293f7018c21STomi Valkeinen 	msg->pipe_hdr.type = PIPE_MSG_DATA;
294f7018c21STomi Valkeinen 	msg->pipe_hdr.size = msg->vid_hdr.size;
295f7018c21STomi Valkeinen 
296f7018c21STomi Valkeinen 	ret = vmbus_sendpacket(hdev->channel, msg,
297f7018c21STomi Valkeinen 			       msg->vid_hdr.size + sizeof(struct pipe_msg_hdr),
298f7018c21STomi Valkeinen 			       atomic64_inc_return(&request_id),
299f7018c21STomi Valkeinen 			       VM_PKT_DATA_INBAND, 0);
300f7018c21STomi Valkeinen 
301f7018c21STomi Valkeinen 	if (ret)
302aa5b7d11SMichael Kelley 		pr_err_ratelimited("Unable to send packet via vmbus; error %d\n", ret);
303f7018c21STomi Valkeinen 
304f7018c21STomi Valkeinen 	return ret;
305f7018c21STomi Valkeinen }
306f7018c21STomi Valkeinen 
307f7018c21STomi Valkeinen 
308f7018c21STomi Valkeinen /* Send screen resolution info to host */
309f7018c21STomi Valkeinen static int synthvid_send_situ(struct hv_device *hdev)
310f7018c21STomi Valkeinen {
311f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
312f7018c21STomi Valkeinen 	struct synthvid_msg msg;
313f7018c21STomi Valkeinen 
314f7018c21STomi Valkeinen 	if (!info)
315f7018c21STomi Valkeinen 		return -ENODEV;
316f7018c21STomi Valkeinen 
317f7018c21STomi Valkeinen 	memset(&msg, 0, sizeof(struct synthvid_msg));
318f7018c21STomi Valkeinen 
319f7018c21STomi Valkeinen 	msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE;
320f7018c21STomi Valkeinen 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
321f7018c21STomi Valkeinen 		sizeof(struct synthvid_situation_update);
322f7018c21STomi Valkeinen 	msg.situ.user_ctx = 0;
323f7018c21STomi Valkeinen 	msg.situ.video_output_count = 1;
324f7018c21STomi Valkeinen 	msg.situ.video_output[0].active = 1;
325f7018c21STomi Valkeinen 	msg.situ.video_output[0].vram_offset = 0;
326f7018c21STomi Valkeinen 	msg.situ.video_output[0].depth_bits = info->var.bits_per_pixel;
327f7018c21STomi Valkeinen 	msg.situ.video_output[0].width_pixels = info->var.xres;
328f7018c21STomi Valkeinen 	msg.situ.video_output[0].height_pixels = info->var.yres;
329f7018c21STomi Valkeinen 	msg.situ.video_output[0].pitch_bytes = info->fix.line_length;
330f7018c21STomi Valkeinen 
331f7018c21STomi Valkeinen 	synthvid_send(hdev, &msg);
332f7018c21STomi Valkeinen 
333f7018c21STomi Valkeinen 	return 0;
334f7018c21STomi Valkeinen }
335f7018c21STomi Valkeinen 
336f7018c21STomi Valkeinen /* Send mouse pointer info to host */
337f7018c21STomi Valkeinen static int synthvid_send_ptr(struct hv_device *hdev)
338f7018c21STomi Valkeinen {
339f7018c21STomi Valkeinen 	struct synthvid_msg msg;
340f7018c21STomi Valkeinen 
341f7018c21STomi Valkeinen 	memset(&msg, 0, sizeof(struct synthvid_msg));
342f7018c21STomi Valkeinen 	msg.vid_hdr.type = SYNTHVID_POINTER_POSITION;
343f7018c21STomi Valkeinen 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
344f7018c21STomi Valkeinen 		sizeof(struct synthvid_pointer_position);
345f7018c21STomi Valkeinen 	msg.ptr_pos.is_visible = 1;
346f7018c21STomi Valkeinen 	msg.ptr_pos.video_output = 0;
347f7018c21STomi Valkeinen 	msg.ptr_pos.image_x = 0;
348f7018c21STomi Valkeinen 	msg.ptr_pos.image_y = 0;
349f7018c21STomi Valkeinen 	synthvid_send(hdev, &msg);
350f7018c21STomi Valkeinen 
351f7018c21STomi Valkeinen 	memset(&msg, 0, sizeof(struct synthvid_msg));
352f7018c21STomi Valkeinen 	msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE;
353f7018c21STomi Valkeinen 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
354f7018c21STomi Valkeinen 		sizeof(struct synthvid_pointer_shape);
355f7018c21STomi Valkeinen 	msg.ptr_shape.part_idx = CURSOR_COMPLETE;
356f7018c21STomi Valkeinen 	msg.ptr_shape.is_argb = 1;
357f7018c21STomi Valkeinen 	msg.ptr_shape.width = 1;
358f7018c21STomi Valkeinen 	msg.ptr_shape.height = 1;
359f7018c21STomi Valkeinen 	msg.ptr_shape.hot_x = 0;
360f7018c21STomi Valkeinen 	msg.ptr_shape.hot_y = 0;
361f7018c21STomi Valkeinen 	msg.ptr_shape.data[0] = 0;
362f7018c21STomi Valkeinen 	msg.ptr_shape.data[1] = 1;
363f7018c21STomi Valkeinen 	msg.ptr_shape.data[2] = 1;
364f7018c21STomi Valkeinen 	msg.ptr_shape.data[3] = 1;
365f7018c21STomi Valkeinen 	synthvid_send(hdev, &msg);
366f7018c21STomi Valkeinen 
367f7018c21STomi Valkeinen 	return 0;
368f7018c21STomi Valkeinen }
369f7018c21STomi Valkeinen 
370f7018c21STomi Valkeinen /* Send updated screen area (dirty rectangle) location to host */
371d21987d7SWei Hu static int
372d21987d7SWei Hu synthvid_update(struct fb_info *info, int x1, int y1, int x2, int y2)
373f7018c21STomi Valkeinen {
374f7018c21STomi Valkeinen 	struct hv_device *hdev = device_to_hv_device(info->device);
375f7018c21STomi Valkeinen 	struct synthvid_msg msg;
376f7018c21STomi Valkeinen 
377f7018c21STomi Valkeinen 	memset(&msg, 0, sizeof(struct synthvid_msg));
378d21987d7SWei Hu 	if (x2 == INT_MAX)
379d21987d7SWei Hu 		x2 = info->var.xres;
380d21987d7SWei Hu 	if (y2 == INT_MAX)
381d21987d7SWei Hu 		y2 = info->var.yres;
382f7018c21STomi Valkeinen 
383f7018c21STomi Valkeinen 	msg.vid_hdr.type = SYNTHVID_DIRT;
384f7018c21STomi Valkeinen 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
385f7018c21STomi Valkeinen 		sizeof(struct synthvid_dirt);
386f7018c21STomi Valkeinen 	msg.dirt.video_output = 0;
387f7018c21STomi Valkeinen 	msg.dirt.dirt_count = 1;
388d21987d7SWei Hu 	msg.dirt.rect[0].x1 = (x1 > x2) ? 0 : x1;
389d21987d7SWei Hu 	msg.dirt.rect[0].y1 = (y1 > y2) ? 0 : y1;
390d21987d7SWei Hu 	msg.dirt.rect[0].x2 =
391d21987d7SWei Hu 		(x2 < x1 || x2 > info->var.xres) ? info->var.xres : x2;
392d21987d7SWei Hu 	msg.dirt.rect[0].y2 =
393d21987d7SWei Hu 		(y2 < y1 || y2 > info->var.yres) ? info->var.yres : y2;
394f7018c21STomi Valkeinen 
395f7018c21STomi Valkeinen 	synthvid_send(hdev, &msg);
396f7018c21STomi Valkeinen 
397f7018c21STomi Valkeinen 	return 0;
398f7018c21STomi Valkeinen }
399f7018c21STomi Valkeinen 
400d21987d7SWei Hu static void hvfb_docopy(struct hvfb_par *par,
401d21987d7SWei Hu 			unsigned long offset,
402d21987d7SWei Hu 			unsigned long size)
403d21987d7SWei Hu {
404d21987d7SWei Hu 	if (!par || !par->mmio_vp || !par->dio_vp || !par->fb_ready ||
405d21987d7SWei Hu 	    size == 0 || offset >= dio_fb_size)
406d21987d7SWei Hu 		return;
407d21987d7SWei Hu 
408d21987d7SWei Hu 	if (offset + size > dio_fb_size)
409d21987d7SWei Hu 		size = dio_fb_size - offset;
410d21987d7SWei Hu 
411d21987d7SWei Hu 	memcpy(par->mmio_vp + offset, par->dio_vp + offset, size);
412d21987d7SWei Hu }
413d21987d7SWei Hu 
414d21987d7SWei Hu /* Deferred IO callback */
415e80eec1bSThomas Zimmermann static void synthvid_deferred_io(struct fb_info *p, struct list_head *pagereflist)
416d21987d7SWei Hu {
417d21987d7SWei Hu 	struct hvfb_par *par = p->par;
41856c134f7SThomas Zimmermann 	struct fb_deferred_io_pageref *pageref;
419d21987d7SWei Hu 	unsigned long start, end;
420d21987d7SWei Hu 	int y1, y2, miny, maxy;
421d21987d7SWei Hu 
422d21987d7SWei Hu 	miny = INT_MAX;
423d21987d7SWei Hu 	maxy = 0;
424d21987d7SWei Hu 
425d21987d7SWei Hu 	/*
426d21987d7SWei Hu 	 * Merge dirty pages. It is possible that last page cross
427d21987d7SWei Hu 	 * over the end of frame buffer row yres. This is taken care of
428d21987d7SWei Hu 	 * in synthvid_update function by clamping the y2
429d21987d7SWei Hu 	 * value to yres.
430d21987d7SWei Hu 	 */
431e80eec1bSThomas Zimmermann 	list_for_each_entry(pageref, pagereflist, list) {
432e2d8b428SThomas Zimmermann 		start = pageref->offset;
433d21987d7SWei Hu 		end = start + PAGE_SIZE - 1;
434d21987d7SWei Hu 		y1 = start / p->fix.line_length;
435d21987d7SWei Hu 		y2 = end / p->fix.line_length;
436d21987d7SWei Hu 		miny = min_t(int, miny, y1);
437d21987d7SWei Hu 		maxy = max_t(int, maxy, y2);
438d21987d7SWei Hu 
439d21987d7SWei Hu 		/* Copy from dio space to mmio address */
4403a6fb6c4SWei Hu 		if (par->fb_ready && par->need_docopy)
441d21987d7SWei Hu 			hvfb_docopy(par, start, PAGE_SIZE);
442d21987d7SWei Hu 	}
443d21987d7SWei Hu 
444d21987d7SWei Hu 	if (par->fb_ready && par->update)
445d21987d7SWei Hu 		synthvid_update(p, 0, miny, p->var.xres, maxy + 1);
446d21987d7SWei Hu }
447d21987d7SWei Hu 
448d21987d7SWei Hu static struct fb_deferred_io synthvid_defio = {
449d21987d7SWei Hu 	.delay		= HZ / 20,
450d21987d7SWei Hu 	.deferred_io	= synthvid_deferred_io,
451d21987d7SWei Hu };
452f7018c21STomi Valkeinen 
453f7018c21STomi Valkeinen /*
454f7018c21STomi Valkeinen  * Actions on received messages from host:
455f7018c21STomi Valkeinen  * Complete the wait event.
456f7018c21STomi Valkeinen  * Or, reply with screen and cursor info.
457f7018c21STomi Valkeinen  */
458f7018c21STomi Valkeinen static void synthvid_recv_sub(struct hv_device *hdev)
459f7018c21STomi Valkeinen {
460f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
461f7018c21STomi Valkeinen 	struct hvfb_par *par;
462f7018c21STomi Valkeinen 	struct synthvid_msg *msg;
463f7018c21STomi Valkeinen 
464f7018c21STomi Valkeinen 	if (!info)
465f7018c21STomi Valkeinen 		return;
466f7018c21STomi Valkeinen 
467f7018c21STomi Valkeinen 	par = info->par;
468f7018c21STomi Valkeinen 	msg = (struct synthvid_msg *)par->recv_buf;
469f7018c21STomi Valkeinen 
470f7018c21STomi Valkeinen 	/* Complete the wait event */
471f7018c21STomi Valkeinen 	if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE ||
47267e7cdb4SWei Hu 	    msg->vid_hdr.type == SYNTHVID_RESOLUTION_RESPONSE ||
473f7018c21STomi Valkeinen 	    msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) {
474f7018c21STomi Valkeinen 		memcpy(par->init_buf, msg, MAX_VMBUS_PKT_SIZE);
475f7018c21STomi Valkeinen 		complete(&par->wait);
476f7018c21STomi Valkeinen 		return;
477f7018c21STomi Valkeinen 	}
478f7018c21STomi Valkeinen 
479f7018c21STomi Valkeinen 	/* Reply with screen and cursor info */
480f7018c21STomi Valkeinen 	if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) {
481f7018c21STomi Valkeinen 		if (par->fb_ready) {
482f7018c21STomi Valkeinen 			synthvid_send_ptr(hdev);
483f7018c21STomi Valkeinen 			synthvid_send_situ(hdev);
484f7018c21STomi Valkeinen 		}
485f7018c21STomi Valkeinen 
486f7018c21STomi Valkeinen 		par->update = msg->feature_chg.is_dirt_needed;
487f7018c21STomi Valkeinen 		if (par->update)
488f7018c21STomi Valkeinen 			schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
489f7018c21STomi Valkeinen 	}
490f7018c21STomi Valkeinen }
491f7018c21STomi Valkeinen 
492f7018c21STomi Valkeinen /* Receive callback for messages from the host */
493f7018c21STomi Valkeinen static void synthvid_receive(void *ctx)
494f7018c21STomi Valkeinen {
495f7018c21STomi Valkeinen 	struct hv_device *hdev = ctx;
496f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
497f7018c21STomi Valkeinen 	struct hvfb_par *par;
498f7018c21STomi Valkeinen 	struct synthvid_msg *recv_buf;
499f7018c21STomi Valkeinen 	u32 bytes_recvd;
500f7018c21STomi Valkeinen 	u64 req_id;
501f7018c21STomi Valkeinen 	int ret;
502f7018c21STomi Valkeinen 
503f7018c21STomi Valkeinen 	if (!info)
504f7018c21STomi Valkeinen 		return;
505f7018c21STomi Valkeinen 
506f7018c21STomi Valkeinen 	par = info->par;
507f7018c21STomi Valkeinen 	recv_buf = (struct synthvid_msg *)par->recv_buf;
508f7018c21STomi Valkeinen 
509f7018c21STomi Valkeinen 	do {
510f7018c21STomi Valkeinen 		ret = vmbus_recvpacket(hdev->channel, recv_buf,
511f7018c21STomi Valkeinen 				       MAX_VMBUS_PKT_SIZE,
512f7018c21STomi Valkeinen 				       &bytes_recvd, &req_id);
513f7018c21STomi Valkeinen 		if (bytes_recvd > 0 &&
514f7018c21STomi Valkeinen 		    recv_buf->pipe_hdr.type == PIPE_MSG_DATA)
515f7018c21STomi Valkeinen 			synthvid_recv_sub(hdev);
516f7018c21STomi Valkeinen 	} while (bytes_recvd > 0 && ret == 0);
517f7018c21STomi Valkeinen }
518f7018c21STomi Valkeinen 
51967e7cdb4SWei Hu /* Check if the ver1 version is equal or greater than ver2 */
52067e7cdb4SWei Hu static inline bool synthvid_ver_ge(u32 ver1, u32 ver2)
52167e7cdb4SWei Hu {
52267e7cdb4SWei Hu 	if (SYNTHVID_VER_GET_MAJOR(ver1) > SYNTHVID_VER_GET_MAJOR(ver2) ||
52367e7cdb4SWei Hu 	    (SYNTHVID_VER_GET_MAJOR(ver1) == SYNTHVID_VER_GET_MAJOR(ver2) &&
52467e7cdb4SWei Hu 	     SYNTHVID_VER_GET_MINOR(ver1) >= SYNTHVID_VER_GET_MINOR(ver2)))
52567e7cdb4SWei Hu 		return true;
52667e7cdb4SWei Hu 
52767e7cdb4SWei Hu 	return false;
52867e7cdb4SWei Hu }
52967e7cdb4SWei Hu 
530f7018c21STomi Valkeinen /* Check synthetic video protocol version with the host */
531f7018c21STomi Valkeinen static int synthvid_negotiate_ver(struct hv_device *hdev, u32 ver)
532f7018c21STomi Valkeinen {
533f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
534f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
535f7018c21STomi Valkeinen 	struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
5367dea97e0SNicholas Mc Guire 	int ret = 0;
5377dea97e0SNicholas Mc Guire 	unsigned long t;
538f7018c21STomi Valkeinen 
539f7018c21STomi Valkeinen 	memset(msg, 0, sizeof(struct synthvid_msg));
540f7018c21STomi Valkeinen 	msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST;
541f7018c21STomi Valkeinen 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
542f7018c21STomi Valkeinen 		sizeof(struct synthvid_version_req);
543f7018c21STomi Valkeinen 	msg->ver_req.version = ver;
544f7018c21STomi Valkeinen 	synthvid_send(hdev, msg);
545f7018c21STomi Valkeinen 
546f7018c21STomi Valkeinen 	t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
547f7018c21STomi Valkeinen 	if (!t) {
548f7018c21STomi Valkeinen 		pr_err("Time out on waiting version response\n");
549f7018c21STomi Valkeinen 		ret = -ETIMEDOUT;
550f7018c21STomi Valkeinen 		goto out;
551f7018c21STomi Valkeinen 	}
552f7018c21STomi Valkeinen 	if (!msg->ver_resp.is_accepted) {
553f7018c21STomi Valkeinen 		ret = -ENODEV;
554f7018c21STomi Valkeinen 		goto out;
555f7018c21STomi Valkeinen 	}
556f7018c21STomi Valkeinen 
557f7018c21STomi Valkeinen 	par->synthvid_version = ver;
55867e7cdb4SWei Hu 	pr_info("Synthvid Version major %d, minor %d\n",
55967e7cdb4SWei Hu 		SYNTHVID_VER_GET_MAJOR(ver), SYNTHVID_VER_GET_MINOR(ver));
56067e7cdb4SWei Hu 
56167e7cdb4SWei Hu out:
56267e7cdb4SWei Hu 	return ret;
56367e7cdb4SWei Hu }
56467e7cdb4SWei Hu 
56567e7cdb4SWei Hu /* Get current resolution from the host */
56667e7cdb4SWei Hu static int synthvid_get_supported_resolution(struct hv_device *hdev)
56767e7cdb4SWei Hu {
56867e7cdb4SWei Hu 	struct fb_info *info = hv_get_drvdata(hdev);
56967e7cdb4SWei Hu 	struct hvfb_par *par = info->par;
57067e7cdb4SWei Hu 	struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
57167e7cdb4SWei Hu 	int ret = 0;
57267e7cdb4SWei Hu 	unsigned long t;
57367e7cdb4SWei Hu 	u8 index;
57467e7cdb4SWei Hu 
57567e7cdb4SWei Hu 	memset(msg, 0, sizeof(struct synthvid_msg));
57667e7cdb4SWei Hu 	msg->vid_hdr.type = SYNTHVID_RESOLUTION_REQUEST;
57767e7cdb4SWei Hu 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
57867e7cdb4SWei Hu 		sizeof(struct synthvid_supported_resolution_req);
57967e7cdb4SWei Hu 
58067e7cdb4SWei Hu 	msg->resolution_req.maximum_resolution_count =
58167e7cdb4SWei Hu 		SYNTHVID_MAX_RESOLUTION_COUNT;
58267e7cdb4SWei Hu 	synthvid_send(hdev, msg);
58367e7cdb4SWei Hu 
58467e7cdb4SWei Hu 	t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
58567e7cdb4SWei Hu 	if (!t) {
58667e7cdb4SWei Hu 		pr_err("Time out on waiting resolution response\n");
58767e7cdb4SWei Hu 		ret = -ETIMEDOUT;
58867e7cdb4SWei Hu 		goto out;
58967e7cdb4SWei Hu 	}
59067e7cdb4SWei Hu 
59167e7cdb4SWei Hu 	if (msg->resolution_resp.resolution_count == 0) {
59267e7cdb4SWei Hu 		pr_err("No supported resolutions\n");
59367e7cdb4SWei Hu 		ret = -ENODEV;
59467e7cdb4SWei Hu 		goto out;
59567e7cdb4SWei Hu 	}
59667e7cdb4SWei Hu 
59767e7cdb4SWei Hu 	index = msg->resolution_resp.default_resolution_index;
59867e7cdb4SWei Hu 	if (index >= msg->resolution_resp.resolution_count) {
59967e7cdb4SWei Hu 		pr_err("Invalid resolution index: %d\n", index);
60067e7cdb4SWei Hu 		ret = -ENODEV;
60167e7cdb4SWei Hu 		goto out;
60267e7cdb4SWei Hu 	}
60367e7cdb4SWei Hu 
60467e7cdb4SWei Hu 	screen_width =
60567e7cdb4SWei Hu 		msg->resolution_resp.supported_resolution[index].width;
60667e7cdb4SWei Hu 	screen_height =
60767e7cdb4SWei Hu 		msg->resolution_resp.supported_resolution[index].height;
608f7018c21STomi Valkeinen 
609f7018c21STomi Valkeinen out:
610f7018c21STomi Valkeinen 	return ret;
611f7018c21STomi Valkeinen }
612f7018c21STomi Valkeinen 
613f7018c21STomi Valkeinen /* Connect to VSP (Virtual Service Provider) on host */
614f7018c21STomi Valkeinen static int synthvid_connect_vsp(struct hv_device *hdev)
615f7018c21STomi Valkeinen {
616f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
617f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
618f7018c21STomi Valkeinen 	int ret;
619f7018c21STomi Valkeinen 
620f7018c21STomi Valkeinen 	ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE,
621f7018c21STomi Valkeinen 			 NULL, 0, synthvid_receive, hdev);
622f7018c21STomi Valkeinen 	if (ret) {
623f7018c21STomi Valkeinen 		pr_err("Unable to open vmbus channel\n");
624f7018c21STomi Valkeinen 		return ret;
625f7018c21STomi Valkeinen 	}
626f7018c21STomi Valkeinen 
627f7018c21STomi Valkeinen 	/* Negotiate the protocol version with host */
62867e7cdb4SWei Hu 	switch (vmbus_proto_version) {
62967e7cdb4SWei Hu 	case VERSION_WIN10:
63067e7cdb4SWei Hu 	case VERSION_WIN10_V5:
63167e7cdb4SWei Hu 		ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
63267e7cdb4SWei Hu 		if (!ret)
63367e7cdb4SWei Hu 			break;
634df561f66SGustavo A. R. Silva 		fallthrough;
63567e7cdb4SWei Hu 	case VERSION_WIN8:
63667e7cdb4SWei Hu 	case VERSION_WIN8_1:
637f7018c21STomi Valkeinen 		ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8);
63867e7cdb4SWei Hu 		break;
63967e7cdb4SWei Hu 	default:
64067e7cdb4SWei Hu 		ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
64167e7cdb4SWei Hu 		break;
64267e7cdb4SWei Hu 	}
643f7018c21STomi Valkeinen 
644f7018c21STomi Valkeinen 	if (ret) {
645f7018c21STomi Valkeinen 		pr_err("Synthetic video device version not accepted\n");
646f7018c21STomi Valkeinen 		goto error;
647f7018c21STomi Valkeinen 	}
648f7018c21STomi Valkeinen 
649f7018c21STomi Valkeinen 	screen_depth = SYNTHVID_DEPTH_WIN8;
65067e7cdb4SWei Hu 	if (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10)) {
65167e7cdb4SWei Hu 		ret = synthvid_get_supported_resolution(hdev);
65267e7cdb4SWei Hu 		if (ret)
65367e7cdb4SWei Hu 			pr_info("Failed to get supported resolution from host, use default\n");
65467e7cdb4SWei Hu 	}
65567e7cdb4SWei Hu 
656f7018c21STomi Valkeinen 	screen_fb_size = hdev->channel->offermsg.offer.
657f7018c21STomi Valkeinen 				mmio_megabytes * 1024 * 1024;
658f7018c21STomi Valkeinen 
659f7018c21STomi Valkeinen 	return 0;
660f7018c21STomi Valkeinen 
661f7018c21STomi Valkeinen error:
662f7018c21STomi Valkeinen 	vmbus_close(hdev->channel);
663f7018c21STomi Valkeinen 	return ret;
664f7018c21STomi Valkeinen }
665f7018c21STomi Valkeinen 
666f7018c21STomi Valkeinen /* Send VRAM and Situation messages to the host */
667f7018c21STomi Valkeinen static int synthvid_send_config(struct hv_device *hdev)
668f7018c21STomi Valkeinen {
669f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
670f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
671f7018c21STomi Valkeinen 	struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
6727dea97e0SNicholas Mc Guire 	int ret = 0;
6737dea97e0SNicholas Mc Guire 	unsigned long t;
674f7018c21STomi Valkeinen 
675f7018c21STomi Valkeinen 	/* Send VRAM location */
676f7018c21STomi Valkeinen 	memset(msg, 0, sizeof(struct synthvid_msg));
677f7018c21STomi Valkeinen 	msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION;
678f7018c21STomi Valkeinen 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
679f7018c21STomi Valkeinen 		sizeof(struct synthvid_vram_location);
680d21987d7SWei Hu 	msg->vram.user_ctx = msg->vram.vram_gpa = par->mmio_pp;
681f7018c21STomi Valkeinen 	msg->vram.is_vram_gpa_specified = 1;
682f7018c21STomi Valkeinen 	synthvid_send(hdev, msg);
683f7018c21STomi Valkeinen 
684f7018c21STomi Valkeinen 	t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
685f7018c21STomi Valkeinen 	if (!t) {
686f7018c21STomi Valkeinen 		pr_err("Time out on waiting vram location ack\n");
687f7018c21STomi Valkeinen 		ret = -ETIMEDOUT;
688f7018c21STomi Valkeinen 		goto out;
689f7018c21STomi Valkeinen 	}
690d21987d7SWei Hu 	if (msg->vram_ack.user_ctx != par->mmio_pp) {
691f7018c21STomi Valkeinen 		pr_err("Unable to set VRAM location\n");
692f7018c21STomi Valkeinen 		ret = -ENODEV;
693f7018c21STomi Valkeinen 		goto out;
694f7018c21STomi Valkeinen 	}
695f7018c21STomi Valkeinen 
696f7018c21STomi Valkeinen 	/* Send pointer and situation update */
697f7018c21STomi Valkeinen 	synthvid_send_ptr(hdev);
698f7018c21STomi Valkeinen 	synthvid_send_situ(hdev);
699f7018c21STomi Valkeinen 
700f7018c21STomi Valkeinen out:
701f7018c21STomi Valkeinen 	return ret;
702f7018c21STomi Valkeinen }
703f7018c21STomi Valkeinen 
704f7018c21STomi Valkeinen 
705f7018c21STomi Valkeinen /*
706f7018c21STomi Valkeinen  * Delayed work callback:
707d21987d7SWei Hu  * It is scheduled to call whenever update request is received and it has
708d21987d7SWei Hu  * not been called in last HVFB_ONDEMAND_THROTTLE time interval.
709f7018c21STomi Valkeinen  */
710f7018c21STomi Valkeinen static void hvfb_update_work(struct work_struct *w)
711f7018c21STomi Valkeinen {
712f7018c21STomi Valkeinen 	struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work);
713f7018c21STomi Valkeinen 	struct fb_info *info = par->info;
714d21987d7SWei Hu 	unsigned long flags;
715d21987d7SWei Hu 	int x1, x2, y1, y2;
716d21987d7SWei Hu 	int j;
717f7018c21STomi Valkeinen 
718d21987d7SWei Hu 	spin_lock_irqsave(&par->delayed_refresh_lock, flags);
719d21987d7SWei Hu 	/* Reset the request flag */
720d21987d7SWei Hu 	par->delayed_refresh = false;
721f7018c21STomi Valkeinen 
722d21987d7SWei Hu 	/* Store the dirty rectangle to local variables */
723d21987d7SWei Hu 	x1 = par->x1;
724d21987d7SWei Hu 	x2 = par->x2;
725d21987d7SWei Hu 	y1 = par->y1;
726d21987d7SWei Hu 	y2 = par->y2;
727d21987d7SWei Hu 
728d21987d7SWei Hu 	/* Clear dirty rectangle */
729d21987d7SWei Hu 	par->x1 = par->y1 = INT_MAX;
730d21987d7SWei Hu 	par->x2 = par->y2 = 0;
731d21987d7SWei Hu 
732d21987d7SWei Hu 	spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
733d21987d7SWei Hu 
734d21987d7SWei Hu 	if (x1 > info->var.xres || x2 > info->var.xres ||
735d21987d7SWei Hu 	    y1 > info->var.yres || y2 > info->var.yres || x2 <= x1)
736d21987d7SWei Hu 		return;
737d21987d7SWei Hu 
738d21987d7SWei Hu 	/* Copy the dirty rectangle to frame buffer memory */
7393a6fb6c4SWei Hu 	if (par->need_docopy)
7403a6fb6c4SWei Hu 		for (j = y1; j < y2; j++)
741d21987d7SWei Hu 			hvfb_docopy(par,
742d21987d7SWei Hu 				    j * info->fix.line_length +
743d21987d7SWei Hu 				    (x1 * screen_depth / 8),
744d21987d7SWei Hu 				    (x2 - x1) * screen_depth / 8);
745d21987d7SWei Hu 
746d21987d7SWei Hu 	/* Refresh */
747d21987d7SWei Hu 	if (par->fb_ready && par->update)
748d21987d7SWei Hu 		synthvid_update(info, x1, y1, x2, y2);
749d21987d7SWei Hu }
750d21987d7SWei Hu 
751d21987d7SWei Hu /*
752d21987d7SWei Hu  * Control the on-demand refresh frequency. It schedules a delayed
753d21987d7SWei Hu  * screen update if it has not yet.
754d21987d7SWei Hu  */
755d21987d7SWei Hu static void hvfb_ondemand_refresh_throttle(struct hvfb_par *par,
756d21987d7SWei Hu 					   int x1, int y1, int w, int h)
757d21987d7SWei Hu {
758d21987d7SWei Hu 	unsigned long flags;
759d21987d7SWei Hu 	int x2 = x1 + w;
760d21987d7SWei Hu 	int y2 = y1 + h;
761d21987d7SWei Hu 
762d21987d7SWei Hu 	spin_lock_irqsave(&par->delayed_refresh_lock, flags);
763d21987d7SWei Hu 
764d21987d7SWei Hu 	/* Merge dirty rectangle */
765d21987d7SWei Hu 	par->x1 = min_t(int, par->x1, x1);
766d21987d7SWei Hu 	par->y1 = min_t(int, par->y1, y1);
767d21987d7SWei Hu 	par->x2 = max_t(int, par->x2, x2);
768d21987d7SWei Hu 	par->y2 = max_t(int, par->y2, y2);
769d21987d7SWei Hu 
770d21987d7SWei Hu 	/* Schedule a delayed screen update if not yet */
771d21987d7SWei Hu 	if (par->delayed_refresh == false) {
772d21987d7SWei Hu 		schedule_delayed_work(&par->dwork,
773d21987d7SWei Hu 				      HVFB_ONDEMAND_THROTTLE);
774d21987d7SWei Hu 		par->delayed_refresh = true;
775d21987d7SWei Hu 	}
776d21987d7SWei Hu 
777d21987d7SWei Hu 	spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
778f7018c21STomi Valkeinen }
779f7018c21STomi Valkeinen 
7803686fe96SDexuan Cui static int hvfb_on_panic(struct notifier_block *nb,
7813686fe96SDexuan Cui 			 unsigned long e, void *p)
7823686fe96SDexuan Cui {
7831d044ca0SGuilherme G. Piccoli 	struct hv_device *hdev;
7843686fe96SDexuan Cui 	struct hvfb_par *par;
7853686fe96SDexuan Cui 	struct fb_info *info;
7863686fe96SDexuan Cui 
7873686fe96SDexuan Cui 	par = container_of(nb, struct hvfb_par, hvfb_panic_nb);
7883686fe96SDexuan Cui 	info = par->info;
7891d044ca0SGuilherme G. Piccoli 	hdev = device_to_hv_device(info->device);
7901d044ca0SGuilherme G. Piccoli 
7911d044ca0SGuilherme G. Piccoli 	if (hv_ringbuffer_spinlock_busy(hdev->channel))
7921d044ca0SGuilherme G. Piccoli 		return NOTIFY_DONE;
7931d044ca0SGuilherme G. Piccoli 
7941d044ca0SGuilherme G. Piccoli 	par->synchronous_fb = true;
7953a6fb6c4SWei Hu 	if (par->need_docopy)
796d21987d7SWei Hu 		hvfb_docopy(par, 0, dio_fb_size);
797d21987d7SWei Hu 	synthvid_update(info, 0, 0, INT_MAX, INT_MAX);
7983686fe96SDexuan Cui 
7993686fe96SDexuan Cui 	return NOTIFY_DONE;
8003686fe96SDexuan Cui }
801f7018c21STomi Valkeinen 
802f7018c21STomi Valkeinen /* Framebuffer operation handlers */
803f7018c21STomi Valkeinen 
804f7018c21STomi Valkeinen static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
805f7018c21STomi Valkeinen {
806f7018c21STomi Valkeinen 	if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN ||
807f7018c21STomi Valkeinen 	    var->xres > screen_width || var->yres >  screen_height ||
808f7018c21STomi Valkeinen 	    var->bits_per_pixel != screen_depth)
809f7018c21STomi Valkeinen 		return -EINVAL;
810f7018c21STomi Valkeinen 
811f7018c21STomi Valkeinen 	var->xres_virtual = var->xres;
812f7018c21STomi Valkeinen 	var->yres_virtual = var->yres;
813f7018c21STomi Valkeinen 
814f7018c21STomi Valkeinen 	return 0;
815f7018c21STomi Valkeinen }
816f7018c21STomi Valkeinen 
817f7018c21STomi Valkeinen static int hvfb_set_par(struct fb_info *info)
818f7018c21STomi Valkeinen {
819f7018c21STomi Valkeinen 	struct hv_device *hdev = device_to_hv_device(info->device);
820f7018c21STomi Valkeinen 
821f7018c21STomi Valkeinen 	return synthvid_send_situ(hdev);
822f7018c21STomi Valkeinen }
823f7018c21STomi Valkeinen 
824f7018c21STomi Valkeinen 
825f7018c21STomi Valkeinen static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf)
826f7018c21STomi Valkeinen {
827f7018c21STomi Valkeinen 	return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset;
828f7018c21STomi Valkeinen }
829f7018c21STomi Valkeinen 
830f7018c21STomi Valkeinen static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
831f7018c21STomi Valkeinen 			  unsigned blue, unsigned transp, struct fb_info *info)
832f7018c21STomi Valkeinen {
833f7018c21STomi Valkeinen 	u32 *pal = info->pseudo_palette;
834f7018c21STomi Valkeinen 
835f7018c21STomi Valkeinen 	if (regno > 15)
836f7018c21STomi Valkeinen 		return -EINVAL;
837f7018c21STomi Valkeinen 
838f7018c21STomi Valkeinen 	pal[regno] = chan_to_field(red, &info->var.red)
839f7018c21STomi Valkeinen 		| chan_to_field(green, &info->var.green)
840f7018c21STomi Valkeinen 		| chan_to_field(blue, &info->var.blue)
841f7018c21STomi Valkeinen 		| chan_to_field(transp, &info->var.transp);
842f7018c21STomi Valkeinen 
843f7018c21STomi Valkeinen 	return 0;
844f7018c21STomi Valkeinen }
845f7018c21STomi Valkeinen 
846f7018c21STomi Valkeinen static int hvfb_blank(int blank, struct fb_info *info)
847f7018c21STomi Valkeinen {
848f7018c21STomi Valkeinen 	return 1;	/* get fb_blank to set the colormap to all black */
849f7018c21STomi Valkeinen }
850f7018c21STomi Valkeinen 
8513686fe96SDexuan Cui static void hvfb_cfb_fillrect(struct fb_info *p,
8523686fe96SDexuan Cui 			      const struct fb_fillrect *rect)
8533686fe96SDexuan Cui {
8543686fe96SDexuan Cui 	struct hvfb_par *par = p->par;
8553686fe96SDexuan Cui 
8563686fe96SDexuan Cui 	cfb_fillrect(p, rect);
8573686fe96SDexuan Cui 	if (par->synchronous_fb)
858d21987d7SWei Hu 		synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
859d21987d7SWei Hu 	else
860d21987d7SWei Hu 		hvfb_ondemand_refresh_throttle(par, rect->dx, rect->dy,
861d21987d7SWei Hu 					       rect->width, rect->height);
8623686fe96SDexuan Cui }
8633686fe96SDexuan Cui 
8643686fe96SDexuan Cui static void hvfb_cfb_copyarea(struct fb_info *p,
8653686fe96SDexuan Cui 			      const struct fb_copyarea *area)
8663686fe96SDexuan Cui {
8673686fe96SDexuan Cui 	struct hvfb_par *par = p->par;
8683686fe96SDexuan Cui 
8693686fe96SDexuan Cui 	cfb_copyarea(p, area);
8703686fe96SDexuan Cui 	if (par->synchronous_fb)
871d21987d7SWei Hu 		synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
872d21987d7SWei Hu 	else
873d21987d7SWei Hu 		hvfb_ondemand_refresh_throttle(par, area->dx, area->dy,
874d21987d7SWei Hu 					       area->width, area->height);
8753686fe96SDexuan Cui }
8763686fe96SDexuan Cui 
8773686fe96SDexuan Cui static void hvfb_cfb_imageblit(struct fb_info *p,
8783686fe96SDexuan Cui 			       const struct fb_image *image)
8793686fe96SDexuan Cui {
8803686fe96SDexuan Cui 	struct hvfb_par *par = p->par;
8813686fe96SDexuan Cui 
8823686fe96SDexuan Cui 	cfb_imageblit(p, image);
8833686fe96SDexuan Cui 	if (par->synchronous_fb)
884d21987d7SWei Hu 		synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
885d21987d7SWei Hu 	else
886d21987d7SWei Hu 		hvfb_ondemand_refresh_throttle(par, image->dx, image->dy,
887d21987d7SWei Hu 					       image->width, image->height);
8883686fe96SDexuan Cui }
8893686fe96SDexuan Cui 
8908a48ac33SJani Nikula static const struct fb_ops hvfb_ops = {
891f7018c21STomi Valkeinen 	.owner = THIS_MODULE,
892f7018c21STomi Valkeinen 	.fb_check_var = hvfb_check_var,
893f7018c21STomi Valkeinen 	.fb_set_par = hvfb_set_par,
894f7018c21STomi Valkeinen 	.fb_setcolreg = hvfb_setcolreg,
8953686fe96SDexuan Cui 	.fb_fillrect = hvfb_cfb_fillrect,
8963686fe96SDexuan Cui 	.fb_copyarea = hvfb_cfb_copyarea,
8973686fe96SDexuan Cui 	.fb_imageblit = hvfb_cfb_imageblit,
898f7018c21STomi Valkeinen 	.fb_blank = hvfb_blank,
89959055851SThomas Zimmermann 	.fb_mmap = fb_deferred_io_mmap,
900f7018c21STomi Valkeinen };
901f7018c21STomi Valkeinen 
902f7018c21STomi Valkeinen 
903f7018c21STomi Valkeinen /* Get options from kernel paramenter "video=" */
904f7018c21STomi Valkeinen static void hvfb_get_option(struct fb_info *info)
905f7018c21STomi Valkeinen {
906f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
907f7018c21STomi Valkeinen 	char *opt = NULL, *p;
908f7018c21STomi Valkeinen 	uint x = 0, y = 0;
909f7018c21STomi Valkeinen 
910f7018c21STomi Valkeinen 	if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt)
911f7018c21STomi Valkeinen 		return;
912f7018c21STomi Valkeinen 
913f7018c21STomi Valkeinen 	p = strsep(&opt, "x");
914f7018c21STomi Valkeinen 	if (!*p || kstrtouint(p, 0, &x) ||
915f7018c21STomi Valkeinen 	    !opt || !*opt || kstrtouint(opt, 0, &y)) {
916f7018c21STomi Valkeinen 		pr_err("Screen option is invalid: skipped\n");
917f7018c21STomi Valkeinen 		return;
918f7018c21STomi Valkeinen 	}
919f7018c21STomi Valkeinen 
920f7018c21STomi Valkeinen 	if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN ||
92167e7cdb4SWei Hu 	    (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10) &&
9229ff5549bSMichael Kelley 	    (x * y * screen_depth / 8 > screen_fb_size)) ||
923f7018c21STomi Valkeinen 	    (par->synthvid_version == SYNTHVID_VERSION_WIN8 &&
924b0cce4f6SMichael Kelley 	     x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8)) {
925f7018c21STomi Valkeinen 		pr_err("Screen resolution option is out of range: skipped\n");
926f7018c21STomi Valkeinen 		return;
927f7018c21STomi Valkeinen 	}
928f7018c21STomi Valkeinen 
929f7018c21STomi Valkeinen 	screen_width = x;
930f7018c21STomi Valkeinen 	screen_height = y;
931f7018c21STomi Valkeinen 	return;
932f7018c21STomi Valkeinen }
933f7018c21STomi Valkeinen 
9343a6fb6c4SWei Hu /*
9353a6fb6c4SWei Hu  * Allocate enough contiguous physical memory.
9363a6fb6c4SWei Hu  * Return physical address if succeeded or -1 if failed.
9373a6fb6c4SWei Hu  */
9383a6fb6c4SWei Hu static phys_addr_t hvfb_get_phymem(struct hv_device *hdev,
9393a6fb6c4SWei Hu 				   unsigned int request_size)
9403a6fb6c4SWei Hu {
9413a6fb6c4SWei Hu 	struct page *page = NULL;
9423a6fb6c4SWei Hu 	dma_addr_t dma_handle;
9433a6fb6c4SWei Hu 	void *vmem;
9443a6fb6c4SWei Hu 	phys_addr_t paddr = 0;
9453a6fb6c4SWei Hu 	unsigned int order = get_order(request_size);
9463a6fb6c4SWei Hu 
9473a6fb6c4SWei Hu 	if (request_size == 0)
9483a6fb6c4SWei Hu 		return -1;
9493a6fb6c4SWei Hu 
95023baf831SKirill A. Shutemov 	if (order <= MAX_ORDER) {
9513a6fb6c4SWei Hu 		/* Call alloc_pages if the size is less than 2^MAX_ORDER */
9523a6fb6c4SWei Hu 		page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
9533a6fb6c4SWei Hu 		if (!page)
9543a6fb6c4SWei Hu 			return -1;
9553a6fb6c4SWei Hu 
9563a6fb6c4SWei Hu 		paddr = (page_to_pfn(page) << PAGE_SHIFT);
9573a6fb6c4SWei Hu 	} else {
9583a6fb6c4SWei Hu 		/* Allocate from CMA */
9593a6fb6c4SWei Hu 		hdev->device.coherent_dma_mask = DMA_BIT_MASK(64);
9603a6fb6c4SWei Hu 
9613a6fb6c4SWei Hu 		vmem = dma_alloc_coherent(&hdev->device,
9623a6fb6c4SWei Hu 					  round_up(request_size, PAGE_SIZE),
9633a6fb6c4SWei Hu 					  &dma_handle,
9643a6fb6c4SWei Hu 					  GFP_KERNEL | __GFP_NOWARN);
9653a6fb6c4SWei Hu 
9663a6fb6c4SWei Hu 		if (!vmem)
9673a6fb6c4SWei Hu 			return -1;
9683a6fb6c4SWei Hu 
9693a6fb6c4SWei Hu 		paddr = virt_to_phys(vmem);
9703a6fb6c4SWei Hu 	}
9713a6fb6c4SWei Hu 
9723a6fb6c4SWei Hu 	return paddr;
9733a6fb6c4SWei Hu }
9743a6fb6c4SWei Hu 
9753a6fb6c4SWei Hu /* Release contiguous physical memory */
9763a6fb6c4SWei Hu static void hvfb_release_phymem(struct hv_device *hdev,
9773a6fb6c4SWei Hu 				phys_addr_t paddr, unsigned int size)
9783a6fb6c4SWei Hu {
9793a6fb6c4SWei Hu 	unsigned int order = get_order(size);
9803a6fb6c4SWei Hu 
98123baf831SKirill A. Shutemov 	if (order <= MAX_ORDER)
9823a6fb6c4SWei Hu 		__free_pages(pfn_to_page(paddr >> PAGE_SHIFT), order);
9833a6fb6c4SWei Hu 	else
9843a6fb6c4SWei Hu 		dma_free_coherent(&hdev->device,
9853a6fb6c4SWei Hu 				  round_up(size, PAGE_SIZE),
9863a6fb6c4SWei Hu 				  phys_to_virt(paddr),
9873a6fb6c4SWei Hu 				  paddr);
9883a6fb6c4SWei Hu }
9893a6fb6c4SWei Hu 
990f7018c21STomi Valkeinen 
991f7018c21STomi Valkeinen /* Get framebuffer memory from Hyper-V video pci space */
99235464483SJake Oshins static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info)
993f7018c21STomi Valkeinen {
994f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
995f7018c21STomi Valkeinen 	struct pci_dev *pdev  = NULL;
996f7018c21STomi Valkeinen 	void __iomem *fb_virt;
997f7018c21STomi Valkeinen 	int gen2vm = efi_enabled(EFI_BOOT);
99881d23934SThomas Zimmermann 	resource_size_t base, size;
9993a6fb6c4SWei Hu 	phys_addr_t paddr;
1000f7018c21STomi Valkeinen 	int ret;
1001f7018c21STomi Valkeinen 
10023a6fb6c4SWei Hu 	if (!gen2vm) {
10033a6fb6c4SWei Hu 		pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
10043a6fb6c4SWei Hu 			PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
10053a6fb6c4SWei Hu 		if (!pdev) {
10063a6fb6c4SWei Hu 			pr_err("Unable to find PCI Hyper-V video\n");
10073a6fb6c4SWei Hu 			return -ENODEV;
10083a6fb6c4SWei Hu 		}
10093a6fb6c4SWei Hu 
101081d23934SThomas Zimmermann 		base = pci_resource_start(pdev, 0);
101181d23934SThomas Zimmermann 		size = pci_resource_len(pdev, 0);
10123a6fb6c4SWei Hu 
10133a6fb6c4SWei Hu 		/*
10143a6fb6c4SWei Hu 		 * For Gen 1 VM, we can directly use the contiguous memory
10153a6fb6c4SWei Hu 		 * from VM. If we succeed, deferred IO happens directly
10163a6fb6c4SWei Hu 		 * on this allocated framebuffer memory, avoiding extra
10173a6fb6c4SWei Hu 		 * memory copy.
10183a6fb6c4SWei Hu 		 */
10193a6fb6c4SWei Hu 		paddr = hvfb_get_phymem(hdev, screen_fb_size);
10203a6fb6c4SWei Hu 		if (paddr != (phys_addr_t) -1) {
10213a6fb6c4SWei Hu 			par->mmio_pp = paddr;
10223a6fb6c4SWei Hu 			par->mmio_vp = par->dio_vp = __va(paddr);
10233a6fb6c4SWei Hu 
10243a6fb6c4SWei Hu 			info->fix.smem_start = paddr;
10253a6fb6c4SWei Hu 			info->fix.smem_len = screen_fb_size;
10263a6fb6c4SWei Hu 			info->screen_base = par->mmio_vp;
10273a6fb6c4SWei Hu 			info->screen_size = screen_fb_size;
10283a6fb6c4SWei Hu 
10293a6fb6c4SWei Hu 			par->need_docopy = false;
10303a6fb6c4SWei Hu 			goto getmem_done;
10313a6fb6c4SWei Hu 		}
10323a6fb6c4SWei Hu 		pr_info("Unable to allocate enough contiguous physical memory on Gen 1 VM. Using MMIO instead.\n");
1033*a07b50d8SArnd Bergmann 	} else if (IS_ENABLED(CONFIG_SYSFB)) {
103481d23934SThomas Zimmermann 		base = screen_info.lfb_base;
103581d23934SThomas Zimmermann 		size = screen_info.lfb_size;
10363a6fb6c4SWei Hu 	}
10373a6fb6c4SWei Hu 
10383a6fb6c4SWei Hu 	/*
10393a6fb6c4SWei Hu 	 * Cannot use the contiguous physical memory.
10403a6fb6c4SWei Hu 	 * Allocate mmio space for framebuffer.
10413a6fb6c4SWei Hu 	 */
1042d21987d7SWei Hu 	dio_fb_size =
1043d21987d7SWei Hu 		screen_width * screen_height * screen_depth / 8;
1044d21987d7SWei Hu 
1045c4b4d704SSaurabh Sengar 	ret = vmbus_allocate_mmio(&par->mem, hdev, 0, -1,
104635464483SJake Oshins 				  screen_fb_size, 0x100000, true);
104735464483SJake Oshins 	if (ret != 0) {
104835464483SJake Oshins 		pr_err("Unable to allocate framebuffer memory\n");
104935464483SJake Oshins 		goto err1;
105035464483SJake Oshins 	}
105135464483SJake Oshins 
10525f1251a4SDexuan Cui 	/*
10535f1251a4SDexuan Cui 	 * Map the VRAM cacheable for performance. This is also required for
10545f1251a4SDexuan Cui 	 * VM Connect to display properly for ARM64 Linux VM, as the host also
10555f1251a4SDexuan Cui 	 * maps the VRAM cacheable.
10565f1251a4SDexuan Cui 	 */
10575f1251a4SDexuan Cui 	fb_virt = ioremap_cache(par->mem->start, screen_fb_size);
1058f7018c21STomi Valkeinen 	if (!fb_virt)
1059f7018c21STomi Valkeinen 		goto err2;
1060f7018c21STomi Valkeinen 
1061d21987d7SWei Hu 	/* Allocate memory for deferred IO */
1062d21987d7SWei Hu 	par->dio_vp = vzalloc(round_up(dio_fb_size, PAGE_SIZE));
1063d21987d7SWei Hu 	if (par->dio_vp == NULL)
1064d21987d7SWei Hu 		goto err3;
1065d21987d7SWei Hu 
1066d21987d7SWei Hu 	/* Physical address of FB device */
1067d21987d7SWei Hu 	par->mmio_pp = par->mem->start;
1068d21987d7SWei Hu 	/* Virtual address of FB device */
1069d21987d7SWei Hu 	par->mmio_vp = (unsigned char *) fb_virt;
1070d21987d7SWei Hu 
107135464483SJake Oshins 	info->fix.smem_start = par->mem->start;
1072d21987d7SWei Hu 	info->fix.smem_len = dio_fb_size;
1073d21987d7SWei Hu 	info->screen_base = par->dio_vp;
1074d21987d7SWei Hu 	info->screen_size = dio_fb_size;
1075f7018c21STomi Valkeinen 
10763a6fb6c4SWei Hu getmem_done:
10775fbcc670SDaniel Vetter 	aperture_remove_conflicting_devices(base, size, KBUILD_MODNAME);
10783cb73bc3SKairui Song 
1079*a07b50d8SArnd Bergmann 	if (!gen2vm) {
1080*a07b50d8SArnd Bergmann 		pci_dev_put(pdev);
1081*a07b50d8SArnd Bergmann 	} else if (IS_ENABLED(CONFIG_SYSFB)) {
10823cb73bc3SKairui Song 		/* framebuffer is reallocated, clear screen_info to avoid misuse from kexec */
10833cb73bc3SKairui Song 		screen_info.lfb_size = 0;
10843cb73bc3SKairui Song 		screen_info.lfb_base = 0;
10853cb73bc3SKairui Song 		screen_info.orig_video_isVGA = 0;
10863cb73bc3SKairui Song 	}
1087f7018c21STomi Valkeinen 
1088f7018c21STomi Valkeinen 	return 0;
1089f7018c21STomi Valkeinen 
1090f7018c21STomi Valkeinen err3:
1091f7018c21STomi Valkeinen 	iounmap(fb_virt);
1092f7018c21STomi Valkeinen err2:
1093696ca5e8SJake Oshins 	vmbus_free_mmio(par->mem->start, screen_fb_size);
109435464483SJake Oshins 	par->mem = NULL;
1095f7018c21STomi Valkeinen err1:
1096f7018c21STomi Valkeinen 	if (!gen2vm)
1097f7018c21STomi Valkeinen 		pci_dev_put(pdev);
1098f7018c21STomi Valkeinen 
1099f7018c21STomi Valkeinen 	return -ENOMEM;
1100f7018c21STomi Valkeinen }
1101f7018c21STomi Valkeinen 
1102f7018c21STomi Valkeinen /* Release the framebuffer */
11033a6fb6c4SWei Hu static void hvfb_putmem(struct hv_device *hdev, struct fb_info *info)
1104f7018c21STomi Valkeinen {
1105f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
1106f7018c21STomi Valkeinen 
11073a6fb6c4SWei Hu 	if (par->need_docopy) {
1108d21987d7SWei Hu 		vfree(par->dio_vp);
1109f7018c21STomi Valkeinen 		iounmap(info->screen_base);
1110696ca5e8SJake Oshins 		vmbus_free_mmio(par->mem->start, screen_fb_size);
11113a6fb6c4SWei Hu 	} else {
11123a6fb6c4SWei Hu 		hvfb_release_phymem(hdev, info->fix.smem_start,
11133a6fb6c4SWei Hu 				    screen_fb_size);
11143a6fb6c4SWei Hu 	}
11153a6fb6c4SWei Hu 
111635464483SJake Oshins 	par->mem = NULL;
1117f7018c21STomi Valkeinen }
1118f7018c21STomi Valkeinen 
1119f7018c21STomi Valkeinen 
1120f7018c21STomi Valkeinen static int hvfb_probe(struct hv_device *hdev,
1121f7018c21STomi Valkeinen 		      const struct hv_vmbus_device_id *dev_id)
1122f7018c21STomi Valkeinen {
1123f7018c21STomi Valkeinen 	struct fb_info *info;
1124f7018c21STomi Valkeinen 	struct hvfb_par *par;
1125f7018c21STomi Valkeinen 	int ret;
1126f7018c21STomi Valkeinen 
1127f7018c21STomi Valkeinen 	info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device);
11280adcdbcbSBartlomiej Zolnierkiewicz 	if (!info)
1129f7018c21STomi Valkeinen 		return -ENOMEM;
1130f7018c21STomi Valkeinen 
1131f7018c21STomi Valkeinen 	par = info->par;
1132f7018c21STomi Valkeinen 	par->info = info;
1133f7018c21STomi Valkeinen 	par->fb_ready = false;
11343a6fb6c4SWei Hu 	par->need_docopy = true;
1135f7018c21STomi Valkeinen 	init_completion(&par->wait);
1136f7018c21STomi Valkeinen 	INIT_DELAYED_WORK(&par->dwork, hvfb_update_work);
1137f7018c21STomi Valkeinen 
1138d21987d7SWei Hu 	par->delayed_refresh = false;
1139d21987d7SWei Hu 	spin_lock_init(&par->delayed_refresh_lock);
1140d21987d7SWei Hu 	par->x1 = par->y1 = INT_MAX;
1141d21987d7SWei Hu 	par->x2 = par->y2 = 0;
1142d21987d7SWei Hu 
1143f7018c21STomi Valkeinen 	/* Connect to VSP */
1144f7018c21STomi Valkeinen 	hv_set_drvdata(hdev, info);
1145f7018c21STomi Valkeinen 	ret = synthvid_connect_vsp(hdev);
1146f7018c21STomi Valkeinen 	if (ret) {
1147f7018c21STomi Valkeinen 		pr_err("Unable to connect to VSP\n");
1148f7018c21STomi Valkeinen 		goto error1;
1149f7018c21STomi Valkeinen 	}
1150f7018c21STomi Valkeinen 
115167e7cdb4SWei Hu 	hvfb_get_option(info);
11529ff5549bSMichael Kelley 	pr_info("Screen resolution: %dx%d, Color depth: %d, Frame buffer size: %d\n",
11539ff5549bSMichael Kelley 		screen_width, screen_height, screen_depth, screen_fb_size);
115467e7cdb4SWei Hu 
115535464483SJake Oshins 	ret = hvfb_getmem(hdev, info);
1156f7018c21STomi Valkeinen 	if (ret) {
1157f7018c21STomi Valkeinen 		pr_err("No memory for framebuffer\n");
1158f7018c21STomi Valkeinen 		goto error2;
1159f7018c21STomi Valkeinen 	}
1160f7018c21STomi Valkeinen 
1161f7018c21STomi Valkeinen 	/* Set up fb_info */
1162f7018c21STomi Valkeinen 	info->var.xres_virtual = info->var.xres = screen_width;
1163f7018c21STomi Valkeinen 	info->var.yres_virtual = info->var.yres = screen_height;
1164f7018c21STomi Valkeinen 	info->var.bits_per_pixel = screen_depth;
1165f7018c21STomi Valkeinen 
1166f7018c21STomi Valkeinen 	if (info->var.bits_per_pixel == 16) {
1167f7018c21STomi Valkeinen 		info->var.red = (struct fb_bitfield){11, 5, 0};
1168f7018c21STomi Valkeinen 		info->var.green = (struct fb_bitfield){5, 6, 0};
1169f7018c21STomi Valkeinen 		info->var.blue = (struct fb_bitfield){0, 5, 0};
1170f7018c21STomi Valkeinen 		info->var.transp = (struct fb_bitfield){0, 0, 0};
1171f7018c21STomi Valkeinen 	} else {
1172f7018c21STomi Valkeinen 		info->var.red = (struct fb_bitfield){16, 8, 0};
1173f7018c21STomi Valkeinen 		info->var.green = (struct fb_bitfield){8, 8, 0};
1174f7018c21STomi Valkeinen 		info->var.blue = (struct fb_bitfield){0, 8, 0};
1175f7018c21STomi Valkeinen 		info->var.transp = (struct fb_bitfield){24, 8, 0};
1176f7018c21STomi Valkeinen 	}
1177f7018c21STomi Valkeinen 
1178f7018c21STomi Valkeinen 	info->var.activate = FB_ACTIVATE_NOW;
1179f7018c21STomi Valkeinen 	info->var.height = -1;
1180f7018c21STomi Valkeinen 	info->var.width = -1;
1181f7018c21STomi Valkeinen 	info->var.vmode = FB_VMODE_NONINTERLACED;
1182f7018c21STomi Valkeinen 
1183f7018c21STomi Valkeinen 	strcpy(info->fix.id, KBUILD_MODNAME);
1184f7018c21STomi Valkeinen 	info->fix.type = FB_TYPE_PACKED_PIXELS;
1185f7018c21STomi Valkeinen 	info->fix.visual = FB_VISUAL_TRUECOLOR;
1186f7018c21STomi Valkeinen 	info->fix.line_length = screen_width * screen_depth / 8;
1187f7018c21STomi Valkeinen 	info->fix.accel = FB_ACCEL_NONE;
1188f7018c21STomi Valkeinen 
1189f7018c21STomi Valkeinen 	info->fbops = &hvfb_ops;
1190f7018c21STomi Valkeinen 	info->pseudo_palette = par->pseudo_palette;
1191f7018c21STomi Valkeinen 
1192d21987d7SWei Hu 	/* Initialize deferred IO */
1193d21987d7SWei Hu 	info->fbdefio = &synthvid_defio;
1194d21987d7SWei Hu 	fb_deferred_io_init(info);
1195d21987d7SWei Hu 
1196f7018c21STomi Valkeinen 	/* Send config to host */
1197f7018c21STomi Valkeinen 	ret = synthvid_send_config(hdev);
1198f7018c21STomi Valkeinen 	if (ret)
1199f7018c21STomi Valkeinen 		goto error;
1200f7018c21STomi Valkeinen 
1201f7018c21STomi Valkeinen 	ret = register_framebuffer(info);
1202f7018c21STomi Valkeinen 	if (ret) {
1203f7018c21STomi Valkeinen 		pr_err("Unable to register framebuffer\n");
1204f7018c21STomi Valkeinen 		goto error;
1205f7018c21STomi Valkeinen 	}
1206f7018c21STomi Valkeinen 
1207f7018c21STomi Valkeinen 	par->fb_ready = true;
1208f7018c21STomi Valkeinen 
12093686fe96SDexuan Cui 	par->synchronous_fb = false;
1210d786e00dSGuilherme G. Piccoli 
1211d786e00dSGuilherme G. Piccoli 	/*
1212d786e00dSGuilherme G. Piccoli 	 * We need to be sure this panic notifier runs _before_ the
1213d786e00dSGuilherme G. Piccoli 	 * vmbus disconnect, so order it by priority. It must execute
1214d786e00dSGuilherme G. Piccoli 	 * before the function hv_panic_vmbus_unload() [drivers/hv/vmbus_drv.c],
1215d786e00dSGuilherme G. Piccoli 	 * which is almost at the end of list, with priority = INT_MIN + 1.
1216d786e00dSGuilherme G. Piccoli 	 */
12173686fe96SDexuan Cui 	par->hvfb_panic_nb.notifier_call = hvfb_on_panic;
1218d786e00dSGuilherme G. Piccoli 	par->hvfb_panic_nb.priority = INT_MIN + 10,
12193686fe96SDexuan Cui 	atomic_notifier_chain_register(&panic_notifier_list,
12203686fe96SDexuan Cui 				       &par->hvfb_panic_nb);
12213686fe96SDexuan Cui 
1222f7018c21STomi Valkeinen 	return 0;
1223f7018c21STomi Valkeinen 
1224f7018c21STomi Valkeinen error:
1225d21987d7SWei Hu 	fb_deferred_io_cleanup(info);
12263a6fb6c4SWei Hu 	hvfb_putmem(hdev, info);
1227f7018c21STomi Valkeinen error2:
1228f7018c21STomi Valkeinen 	vmbus_close(hdev->channel);
1229f7018c21STomi Valkeinen error1:
1230f7018c21STomi Valkeinen 	cancel_delayed_work_sync(&par->dwork);
1231f7018c21STomi Valkeinen 	hv_set_drvdata(hdev, NULL);
1232f7018c21STomi Valkeinen 	framebuffer_release(info);
1233f7018c21STomi Valkeinen 	return ret;
1234f7018c21STomi Valkeinen }
1235f7018c21STomi Valkeinen 
123696ec2939SDawei Li static void hvfb_remove(struct hv_device *hdev)
1237f7018c21STomi Valkeinen {
1238f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
1239f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
1240f7018c21STomi Valkeinen 
12413686fe96SDexuan Cui 	atomic_notifier_chain_unregister(&panic_notifier_list,
12423686fe96SDexuan Cui 					 &par->hvfb_panic_nb);
12433686fe96SDexuan Cui 
1244f7018c21STomi Valkeinen 	par->update = false;
1245f7018c21STomi Valkeinen 	par->fb_ready = false;
1246f7018c21STomi Valkeinen 
1247d21987d7SWei Hu 	fb_deferred_io_cleanup(info);
1248d21987d7SWei Hu 
1249f7018c21STomi Valkeinen 	unregister_framebuffer(info);
1250f7018c21STomi Valkeinen 	cancel_delayed_work_sync(&par->dwork);
1251f7018c21STomi Valkeinen 
1252f7018c21STomi Valkeinen 	vmbus_close(hdev->channel);
1253f7018c21STomi Valkeinen 	hv_set_drvdata(hdev, NULL);
1254f7018c21STomi Valkeinen 
12553a6fb6c4SWei Hu 	hvfb_putmem(hdev, info);
1256f7018c21STomi Valkeinen 	framebuffer_release(info);
1257f7018c21STomi Valkeinen }
1258f7018c21STomi Valkeinen 
12591ecf3020SDexuan Cui static int hvfb_suspend(struct hv_device *hdev)
12601ecf3020SDexuan Cui {
12611ecf3020SDexuan Cui 	struct fb_info *info = hv_get_drvdata(hdev);
12621ecf3020SDexuan Cui 	struct hvfb_par *par = info->par;
12631ecf3020SDexuan Cui 
12641ecf3020SDexuan Cui 	console_lock();
12651ecf3020SDexuan Cui 
12661ecf3020SDexuan Cui 	/* 1 means do suspend */
12671ecf3020SDexuan Cui 	fb_set_suspend(info, 1);
12681ecf3020SDexuan Cui 
12691ecf3020SDexuan Cui 	cancel_delayed_work_sync(&par->dwork);
1270382a4622SDexuan Cui 	cancel_delayed_work_sync(&info->deferred_work);
12711ecf3020SDexuan Cui 
12721ecf3020SDexuan Cui 	par->update_saved = par->update;
12731ecf3020SDexuan Cui 	par->update = false;
12741ecf3020SDexuan Cui 	par->fb_ready = false;
12751ecf3020SDexuan Cui 
12761ecf3020SDexuan Cui 	vmbus_close(hdev->channel);
12771ecf3020SDexuan Cui 
12781ecf3020SDexuan Cui 	console_unlock();
12791ecf3020SDexuan Cui 
12801ecf3020SDexuan Cui 	return 0;
12811ecf3020SDexuan Cui }
12821ecf3020SDexuan Cui 
12831ecf3020SDexuan Cui static int hvfb_resume(struct hv_device *hdev)
12841ecf3020SDexuan Cui {
12851ecf3020SDexuan Cui 	struct fb_info *info = hv_get_drvdata(hdev);
12861ecf3020SDexuan Cui 	struct hvfb_par *par = info->par;
12871ecf3020SDexuan Cui 	int ret;
12881ecf3020SDexuan Cui 
12891ecf3020SDexuan Cui 	console_lock();
12901ecf3020SDexuan Cui 
12911ecf3020SDexuan Cui 	ret = synthvid_connect_vsp(hdev);
12921ecf3020SDexuan Cui 	if (ret != 0)
12931ecf3020SDexuan Cui 		goto out;
12941ecf3020SDexuan Cui 
12951ecf3020SDexuan Cui 	ret = synthvid_send_config(hdev);
12961ecf3020SDexuan Cui 	if (ret != 0) {
12971ecf3020SDexuan Cui 		vmbus_close(hdev->channel);
12981ecf3020SDexuan Cui 		goto out;
12991ecf3020SDexuan Cui 	}
13001ecf3020SDexuan Cui 
13011ecf3020SDexuan Cui 	par->fb_ready = true;
13021ecf3020SDexuan Cui 	par->update = par->update_saved;
13031ecf3020SDexuan Cui 
1304382a4622SDexuan Cui 	schedule_delayed_work(&info->deferred_work, info->fbdefio->delay);
13051ecf3020SDexuan Cui 	schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
13061ecf3020SDexuan Cui 
13071ecf3020SDexuan Cui 	/* 0 means do resume */
13081ecf3020SDexuan Cui 	fb_set_suspend(info, 0);
13091ecf3020SDexuan Cui 
13101ecf3020SDexuan Cui out:
13111ecf3020SDexuan Cui 	console_unlock();
13121ecf3020SDexuan Cui 
13131ecf3020SDexuan Cui 	return ret;
13141ecf3020SDexuan Cui }
13151ecf3020SDexuan Cui 
1316f7018c21STomi Valkeinen 
13179baa3c34SBenoit Taine static const struct pci_device_id pci_stub_id_table[] = {
1318f7018c21STomi Valkeinen 	{
1319f7018c21STomi Valkeinen 		.vendor      = PCI_VENDOR_ID_MICROSOFT,
1320f7018c21STomi Valkeinen 		.device      = PCI_DEVICE_ID_HYPERV_VIDEO,
1321f7018c21STomi Valkeinen 	},
1322f7018c21STomi Valkeinen 	{ /* end of list */ }
1323f7018c21STomi Valkeinen };
1324f7018c21STomi Valkeinen 
1325f7018c21STomi Valkeinen static const struct hv_vmbus_device_id id_table[] = {
1326f7018c21STomi Valkeinen 	/* Synthetic Video Device GUID */
1327f7018c21STomi Valkeinen 	{HV_SYNTHVID_GUID},
1328f7018c21STomi Valkeinen 	{}
1329f7018c21STomi Valkeinen };
1330f7018c21STomi Valkeinen 
1331f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(pci, pci_stub_id_table);
1332f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(vmbus, id_table);
1333f7018c21STomi Valkeinen 
1334f7018c21STomi Valkeinen static struct hv_driver hvfb_drv = {
1335f7018c21STomi Valkeinen 	.name = KBUILD_MODNAME,
1336f7018c21STomi Valkeinen 	.id_table = id_table,
1337f7018c21STomi Valkeinen 	.probe = hvfb_probe,
1338f7018c21STomi Valkeinen 	.remove = hvfb_remove,
13391ecf3020SDexuan Cui 	.suspend = hvfb_suspend,
13401ecf3020SDexuan Cui 	.resume = hvfb_resume,
1341af0a5646SArjan van de Ven 	.driver = {
1342af0a5646SArjan van de Ven 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1343af0a5646SArjan van de Ven 	},
1344f7018c21STomi Valkeinen };
1345f7018c21STomi Valkeinen 
1346f7018c21STomi Valkeinen static int hvfb_pci_stub_probe(struct pci_dev *pdev,
1347f7018c21STomi Valkeinen 			       const struct pci_device_id *ent)
1348f7018c21STomi Valkeinen {
1349f7018c21STomi Valkeinen 	return 0;
1350f7018c21STomi Valkeinen }
1351f7018c21STomi Valkeinen 
1352f7018c21STomi Valkeinen static void hvfb_pci_stub_remove(struct pci_dev *pdev)
1353f7018c21STomi Valkeinen {
1354f7018c21STomi Valkeinen }
1355f7018c21STomi Valkeinen 
1356f7018c21STomi Valkeinen static struct pci_driver hvfb_pci_stub_driver = {
1357f7018c21STomi Valkeinen 	.name =		KBUILD_MODNAME,
1358f7018c21STomi Valkeinen 	.id_table =	pci_stub_id_table,
1359f7018c21STomi Valkeinen 	.probe =	hvfb_pci_stub_probe,
1360f7018c21STomi Valkeinen 	.remove =	hvfb_pci_stub_remove,
1361af0a5646SArjan van de Ven 	.driver = {
1362af0a5646SArjan van de Ven 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1363af0a5646SArjan van de Ven 	}
1364f7018c21STomi Valkeinen };
1365f7018c21STomi Valkeinen 
1366f7018c21STomi Valkeinen static int __init hvfb_drv_init(void)
1367f7018c21STomi Valkeinen {
1368f7018c21STomi Valkeinen 	int ret;
1369f7018c21STomi Valkeinen 
13700ba2fa8cSThomas Zimmermann 	if (fb_modesetting_disabled("hyper_fb"))
13710ba2fa8cSThomas Zimmermann 		return -ENODEV;
13720ba2fa8cSThomas Zimmermann 
1373f7018c21STomi Valkeinen 	ret = vmbus_driver_register(&hvfb_drv);
1374f7018c21STomi Valkeinen 	if (ret != 0)
1375f7018c21STomi Valkeinen 		return ret;
1376f7018c21STomi Valkeinen 
1377f7018c21STomi Valkeinen 	ret = pci_register_driver(&hvfb_pci_stub_driver);
1378f7018c21STomi Valkeinen 	if (ret != 0) {
1379f7018c21STomi Valkeinen 		vmbus_driver_unregister(&hvfb_drv);
1380f7018c21STomi Valkeinen 		return ret;
1381f7018c21STomi Valkeinen 	}
1382f7018c21STomi Valkeinen 
1383f7018c21STomi Valkeinen 	return 0;
1384f7018c21STomi Valkeinen }
1385f7018c21STomi Valkeinen 
1386f7018c21STomi Valkeinen static void __exit hvfb_drv_exit(void)
1387f7018c21STomi Valkeinen {
1388f7018c21STomi Valkeinen 	pci_unregister_driver(&hvfb_pci_stub_driver);
1389f7018c21STomi Valkeinen 	vmbus_driver_unregister(&hvfb_drv);
1390f7018c21STomi Valkeinen }
1391f7018c21STomi Valkeinen 
1392f7018c21STomi Valkeinen module_init(hvfb_drv_init);
1393f7018c21STomi Valkeinen module_exit(hvfb_drv_exit);
1394f7018c21STomi Valkeinen 
1395f7018c21STomi Valkeinen MODULE_LICENSE("GPL");
1396f7018c21STomi Valkeinen MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver");
1397