xref: /linux/drivers/video/fbdev/hyperv_fb.c (revision 5f1251a48c17b54939d7477305e39679a565382c)
143aa3132SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f7018c21STomi Valkeinen /*
3f7018c21STomi Valkeinen  * Copyright (c) 2012, Microsoft Corporation.
4f7018c21STomi Valkeinen  *
5f7018c21STomi Valkeinen  * Author:
6f7018c21STomi Valkeinen  *   Haiyang Zhang <haiyangz@microsoft.com>
7f7018c21STomi Valkeinen  */
8f7018c21STomi Valkeinen 
9f7018c21STomi Valkeinen /*
10f7018c21STomi Valkeinen  * Hyper-V Synthetic Video Frame Buffer Driver
11f7018c21STomi Valkeinen  *
12f7018c21STomi Valkeinen  * This is the driver for the Hyper-V Synthetic Video, which supports
13f7018c21STomi Valkeinen  * screen resolution up to Full HD 1920x1080 with 32 bit color on Windows
14f7018c21STomi Valkeinen  * Server 2012, and 1600x1200 with 16 bit color on Windows Server 2008 R2
15f7018c21STomi Valkeinen  * or earlier.
16f7018c21STomi Valkeinen  *
17f7018c21STomi Valkeinen  * It also solves the double mouse cursor issue of the emulated video mode.
18f7018c21STomi Valkeinen  *
19f7018c21STomi Valkeinen  * The default screen resolution is 1152x864, which may be changed by a
20f7018c21STomi Valkeinen  * kernel parameter:
21f7018c21STomi Valkeinen  *     video=hyperv_fb:<width>x<height>
22f7018c21STomi Valkeinen  *     For example: video=hyperv_fb:1280x1024
23f7018c21STomi Valkeinen  *
24f7018c21STomi Valkeinen  * Portrait orientation is also supported:
25f7018c21STomi Valkeinen  *     For example: video=hyperv_fb:864x1152
2667e7cdb4SWei Hu  *
2767e7cdb4SWei Hu  * When a Windows 10 RS5+ host is used, the virtual machine screen
2867e7cdb4SWei Hu  * resolution is obtained from the host. The "video=hyperv_fb" option is
2967e7cdb4SWei Hu  * not needed, but still can be used to overwrite what the host specifies.
3067e7cdb4SWei Hu  * The VM resolution on the host could be set by executing the powershell
3167e7cdb4SWei Hu  * "set-vmvideo" command. For example
3267e7cdb4SWei Hu  *     set-vmvideo -vmname name -horizontalresolution:1920 \
3367e7cdb4SWei Hu  * -verticalresolution:1200 -resolutiontype single
343a6fb6c4SWei Hu  *
353a6fb6c4SWei Hu  * Gen 1 VMs also support direct using VM's physical memory for framebuffer.
363a6fb6c4SWei Hu  * It could improve the efficiency and performance for framebuffer and VM.
373a6fb6c4SWei Hu  * This requires to allocate contiguous physical memory from Linux kernel's
383a6fb6c4SWei Hu  * CMA memory allocator. To enable this, supply a kernel parameter to give
393a6fb6c4SWei Hu  * enough memory space to CMA allocator for framebuffer. For example:
403a6fb6c4SWei Hu  *    cma=130m
413a6fb6c4SWei Hu  * This gives 130MB memory to CMA allocator that can be allocated to
423a6fb6c4SWei Hu  * framebuffer. For reference, 8K resolution (7680x4320) takes about
433a6fb6c4SWei Hu  * 127MB memory.
44f7018c21STomi Valkeinen  */
45f7018c21STomi Valkeinen 
46f7018c21STomi Valkeinen #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47f7018c21STomi Valkeinen 
48f7018c21STomi Valkeinen #include <linux/module.h>
49f7018c21STomi Valkeinen #include <linux/kernel.h>
50f7018c21STomi Valkeinen #include <linux/init.h>
51f7018c21STomi Valkeinen #include <linux/completion.h>
52f7018c21STomi Valkeinen #include <linux/fb.h>
53f7018c21STomi Valkeinen #include <linux/pci.h>
54f7018c21STomi Valkeinen #include <linux/efi.h>
551ecf3020SDexuan Cui #include <linux/console.h>
56f7018c21STomi Valkeinen 
57f7018c21STomi Valkeinen #include <linux/hyperv.h>
58f7018c21STomi Valkeinen 
59f7018c21STomi Valkeinen 
60f7018c21STomi Valkeinen /* Hyper-V Synthetic Video Protocol definitions and structures */
61f7018c21STomi Valkeinen #define MAX_VMBUS_PKT_SIZE 0x4000
62f7018c21STomi Valkeinen 
63f7018c21STomi Valkeinen #define SYNTHVID_VERSION(major, minor) ((minor) << 16 | (major))
64f7018c21STomi Valkeinen #define SYNTHVID_VERSION_WIN7 SYNTHVID_VERSION(3, 0)
65f7018c21STomi Valkeinen #define SYNTHVID_VERSION_WIN8 SYNTHVID_VERSION(3, 2)
6667e7cdb4SWei Hu #define SYNTHVID_VERSION_WIN10 SYNTHVID_VERSION(3, 5)
6767e7cdb4SWei Hu 
6867e7cdb4SWei Hu #define SYNTHVID_VER_GET_MAJOR(ver) (ver & 0x0000ffff)
6967e7cdb4SWei Hu #define SYNTHVID_VER_GET_MINOR(ver) ((ver & 0xffff0000) >> 16)
70f7018c21STomi Valkeinen 
71f7018c21STomi Valkeinen #define SYNTHVID_DEPTH_WIN7 16
72f7018c21STomi Valkeinen #define SYNTHVID_DEPTH_WIN8 32
73f7018c21STomi Valkeinen 
74f7018c21STomi Valkeinen #define SYNTHVID_FB_SIZE_WIN7 (4 * 1024 * 1024)
75f7018c21STomi Valkeinen #define SYNTHVID_WIDTH_MAX_WIN7 1600
76f7018c21STomi Valkeinen #define SYNTHVID_HEIGHT_MAX_WIN7 1200
77f7018c21STomi Valkeinen 
78f7018c21STomi Valkeinen #define SYNTHVID_FB_SIZE_WIN8 (8 * 1024 * 1024)
79f7018c21STomi Valkeinen 
80f7018c21STomi Valkeinen #define PCI_VENDOR_ID_MICROSOFT 0x1414
81f7018c21STomi Valkeinen #define PCI_DEVICE_ID_HYPERV_VIDEO 0x5353
82f7018c21STomi Valkeinen 
83f7018c21STomi Valkeinen 
84f7018c21STomi Valkeinen enum pipe_msg_type {
85f7018c21STomi Valkeinen 	PIPE_MSG_INVALID,
86f7018c21STomi Valkeinen 	PIPE_MSG_DATA,
87f7018c21STomi Valkeinen 	PIPE_MSG_MAX
88f7018c21STomi Valkeinen };
89f7018c21STomi Valkeinen 
90f7018c21STomi Valkeinen struct pipe_msg_hdr {
91f7018c21STomi Valkeinen 	u32 type;
92f7018c21STomi Valkeinen 	u32 size; /* size of message after this field */
93f7018c21STomi Valkeinen } __packed;
94f7018c21STomi Valkeinen 
95f7018c21STomi Valkeinen 
96f7018c21STomi Valkeinen enum synthvid_msg_type {
97f7018c21STomi Valkeinen 	SYNTHVID_ERROR			= 0,
98f7018c21STomi Valkeinen 	SYNTHVID_VERSION_REQUEST	= 1,
99f7018c21STomi Valkeinen 	SYNTHVID_VERSION_RESPONSE	= 2,
100f7018c21STomi Valkeinen 	SYNTHVID_VRAM_LOCATION		= 3,
101f7018c21STomi Valkeinen 	SYNTHVID_VRAM_LOCATION_ACK	= 4,
102f7018c21STomi Valkeinen 	SYNTHVID_SITUATION_UPDATE	= 5,
103f7018c21STomi Valkeinen 	SYNTHVID_SITUATION_UPDATE_ACK	= 6,
104f7018c21STomi Valkeinen 	SYNTHVID_POINTER_POSITION	= 7,
105f7018c21STomi Valkeinen 	SYNTHVID_POINTER_SHAPE		= 8,
106f7018c21STomi Valkeinen 	SYNTHVID_FEATURE_CHANGE		= 9,
107f7018c21STomi Valkeinen 	SYNTHVID_DIRT			= 10,
10867e7cdb4SWei Hu 	SYNTHVID_RESOLUTION_REQUEST	= 13,
10967e7cdb4SWei Hu 	SYNTHVID_RESOLUTION_RESPONSE	= 14,
110f7018c21STomi Valkeinen 
11167e7cdb4SWei Hu 	SYNTHVID_MAX			= 15
112f7018c21STomi Valkeinen };
113f7018c21STomi Valkeinen 
11467e7cdb4SWei Hu #define		SYNTHVID_EDID_BLOCK_SIZE	128
11567e7cdb4SWei Hu #define		SYNTHVID_MAX_RESOLUTION_COUNT	64
11667e7cdb4SWei Hu 
11767e7cdb4SWei Hu struct hvd_screen_info {
11867e7cdb4SWei Hu 	u16 width;
11967e7cdb4SWei Hu 	u16 height;
12067e7cdb4SWei Hu } __packed;
12167e7cdb4SWei Hu 
122f7018c21STomi Valkeinen struct synthvid_msg_hdr {
123f7018c21STomi Valkeinen 	u32 type;
124f7018c21STomi Valkeinen 	u32 size;  /* size of this header + payload after this field*/
125f7018c21STomi Valkeinen } __packed;
126f7018c21STomi Valkeinen 
127f7018c21STomi Valkeinen struct synthvid_version_req {
128f7018c21STomi Valkeinen 	u32 version;
129f7018c21STomi Valkeinen } __packed;
130f7018c21STomi Valkeinen 
131f7018c21STomi Valkeinen struct synthvid_version_resp {
132f7018c21STomi Valkeinen 	u32 version;
133f7018c21STomi Valkeinen 	u8 is_accepted;
134f7018c21STomi Valkeinen 	u8 max_video_outputs;
135f7018c21STomi Valkeinen } __packed;
136f7018c21STomi Valkeinen 
13767e7cdb4SWei Hu struct synthvid_supported_resolution_req {
13867e7cdb4SWei Hu 	u8 maximum_resolution_count;
13967e7cdb4SWei Hu } __packed;
14067e7cdb4SWei Hu 
14167e7cdb4SWei Hu struct synthvid_supported_resolution_resp {
14267e7cdb4SWei Hu 	u8 edid_block[SYNTHVID_EDID_BLOCK_SIZE];
14367e7cdb4SWei Hu 	u8 resolution_count;
14467e7cdb4SWei Hu 	u8 default_resolution_index;
14567e7cdb4SWei Hu 	u8 is_standard;
14667e7cdb4SWei Hu 	struct hvd_screen_info
14767e7cdb4SWei Hu 		supported_resolution[SYNTHVID_MAX_RESOLUTION_COUNT];
14867e7cdb4SWei Hu } __packed;
14967e7cdb4SWei Hu 
150f7018c21STomi Valkeinen struct synthvid_vram_location {
151f7018c21STomi Valkeinen 	u64 user_ctx;
152f7018c21STomi Valkeinen 	u8 is_vram_gpa_specified;
153f7018c21STomi Valkeinen 	u64 vram_gpa;
154f7018c21STomi Valkeinen } __packed;
155f7018c21STomi Valkeinen 
156f7018c21STomi Valkeinen struct synthvid_vram_location_ack {
157f7018c21STomi Valkeinen 	u64 user_ctx;
158f7018c21STomi Valkeinen } __packed;
159f7018c21STomi Valkeinen 
160f7018c21STomi Valkeinen struct video_output_situation {
161f7018c21STomi Valkeinen 	u8 active;
162f7018c21STomi Valkeinen 	u32 vram_offset;
163f7018c21STomi Valkeinen 	u8 depth_bits;
164f7018c21STomi Valkeinen 	u32 width_pixels;
165f7018c21STomi Valkeinen 	u32 height_pixels;
166f7018c21STomi Valkeinen 	u32 pitch_bytes;
167f7018c21STomi Valkeinen } __packed;
168f7018c21STomi Valkeinen 
169f7018c21STomi Valkeinen struct synthvid_situation_update {
170f7018c21STomi Valkeinen 	u64 user_ctx;
171f7018c21STomi Valkeinen 	u8 video_output_count;
172f7018c21STomi Valkeinen 	struct video_output_situation video_output[1];
173f7018c21STomi Valkeinen } __packed;
174f7018c21STomi Valkeinen 
175f7018c21STomi Valkeinen struct synthvid_situation_update_ack {
176f7018c21STomi Valkeinen 	u64 user_ctx;
177f7018c21STomi Valkeinen } __packed;
178f7018c21STomi Valkeinen 
179f7018c21STomi Valkeinen struct synthvid_pointer_position {
180f7018c21STomi Valkeinen 	u8 is_visible;
181f7018c21STomi Valkeinen 	u8 video_output;
182f7018c21STomi Valkeinen 	s32 image_x;
183f7018c21STomi Valkeinen 	s32 image_y;
184f7018c21STomi Valkeinen } __packed;
185f7018c21STomi Valkeinen 
186f7018c21STomi Valkeinen 
187f7018c21STomi Valkeinen #define CURSOR_MAX_X 96
188f7018c21STomi Valkeinen #define CURSOR_MAX_Y 96
189f7018c21STomi Valkeinen #define CURSOR_ARGB_PIXEL_SIZE 4
190f7018c21STomi Valkeinen #define CURSOR_MAX_SIZE (CURSOR_MAX_X * CURSOR_MAX_Y * CURSOR_ARGB_PIXEL_SIZE)
191f7018c21STomi Valkeinen #define CURSOR_COMPLETE (-1)
192f7018c21STomi Valkeinen 
193f7018c21STomi Valkeinen struct synthvid_pointer_shape {
194f7018c21STomi Valkeinen 	u8 part_idx;
195f7018c21STomi Valkeinen 	u8 is_argb;
196f7018c21STomi Valkeinen 	u32 width; /* CURSOR_MAX_X at most */
197f7018c21STomi Valkeinen 	u32 height; /* CURSOR_MAX_Y at most */
198f7018c21STomi Valkeinen 	u32 hot_x; /* hotspot relative to upper-left of pointer image */
199f7018c21STomi Valkeinen 	u32 hot_y;
200f7018c21STomi Valkeinen 	u8 data[4];
201f7018c21STomi Valkeinen } __packed;
202f7018c21STomi Valkeinen 
203f7018c21STomi Valkeinen struct synthvid_feature_change {
204f7018c21STomi Valkeinen 	u8 is_dirt_needed;
205f7018c21STomi Valkeinen 	u8 is_ptr_pos_needed;
206f7018c21STomi Valkeinen 	u8 is_ptr_shape_needed;
207f7018c21STomi Valkeinen 	u8 is_situ_needed;
208f7018c21STomi Valkeinen } __packed;
209f7018c21STomi Valkeinen 
210f7018c21STomi Valkeinen struct rect {
211f7018c21STomi Valkeinen 	s32 x1, y1; /* top left corner */
212f7018c21STomi Valkeinen 	s32 x2, y2; /* bottom right corner, exclusive */
213f7018c21STomi Valkeinen } __packed;
214f7018c21STomi Valkeinen 
215f7018c21STomi Valkeinen struct synthvid_dirt {
216f7018c21STomi Valkeinen 	u8 video_output;
217f7018c21STomi Valkeinen 	u8 dirt_count;
218f7018c21STomi Valkeinen 	struct rect rect[1];
219f7018c21STomi Valkeinen } __packed;
220f7018c21STomi Valkeinen 
221f7018c21STomi Valkeinen struct synthvid_msg {
222f7018c21STomi Valkeinen 	struct pipe_msg_hdr pipe_hdr;
223f7018c21STomi Valkeinen 	struct synthvid_msg_hdr vid_hdr;
224f7018c21STomi Valkeinen 	union {
225f7018c21STomi Valkeinen 		struct synthvid_version_req ver_req;
226f7018c21STomi Valkeinen 		struct synthvid_version_resp ver_resp;
227f7018c21STomi Valkeinen 		struct synthvid_vram_location vram;
228f7018c21STomi Valkeinen 		struct synthvid_vram_location_ack vram_ack;
229f7018c21STomi Valkeinen 		struct synthvid_situation_update situ;
230f7018c21STomi Valkeinen 		struct synthvid_situation_update_ack situ_ack;
231f7018c21STomi Valkeinen 		struct synthvid_pointer_position ptr_pos;
232f7018c21STomi Valkeinen 		struct synthvid_pointer_shape ptr_shape;
233f7018c21STomi Valkeinen 		struct synthvid_feature_change feature_chg;
234f7018c21STomi Valkeinen 		struct synthvid_dirt dirt;
23567e7cdb4SWei Hu 		struct synthvid_supported_resolution_req resolution_req;
23667e7cdb4SWei Hu 		struct synthvid_supported_resolution_resp resolution_resp;
237f7018c21STomi Valkeinen 	};
238f7018c21STomi Valkeinen } __packed;
239f7018c21STomi Valkeinen 
240f7018c21STomi Valkeinen 
241f7018c21STomi Valkeinen /* FB driver definitions and structures */
242f7018c21STomi Valkeinen #define HVFB_WIDTH 1152 /* default screen width */
243f7018c21STomi Valkeinen #define HVFB_HEIGHT 864 /* default screen height */
244f7018c21STomi Valkeinen #define HVFB_WIDTH_MIN 640
245f7018c21STomi Valkeinen #define HVFB_HEIGHT_MIN 480
246f7018c21STomi Valkeinen 
247f7018c21STomi Valkeinen #define RING_BUFSIZE (256 * 1024)
248f7018c21STomi Valkeinen #define VSP_TIMEOUT (10 * HZ)
249f7018c21STomi Valkeinen #define HVFB_UPDATE_DELAY (HZ / 20)
250d21987d7SWei Hu #define HVFB_ONDEMAND_THROTTLE (HZ / 20)
251f7018c21STomi Valkeinen 
252f7018c21STomi Valkeinen struct hvfb_par {
253f7018c21STomi Valkeinen 	struct fb_info *info;
25435464483SJake Oshins 	struct resource *mem;
255f7018c21STomi Valkeinen 	bool fb_ready; /* fb device is ready */
256f7018c21STomi Valkeinen 	struct completion wait;
257f7018c21STomi Valkeinen 	u32 synthvid_version;
258f7018c21STomi Valkeinen 
259f7018c21STomi Valkeinen 	struct delayed_work dwork;
260f7018c21STomi Valkeinen 	bool update;
2611ecf3020SDexuan Cui 	bool update_saved; /* The value of 'update' before hibernation */
262f7018c21STomi Valkeinen 
263f7018c21STomi Valkeinen 	u32 pseudo_palette[16];
264f7018c21STomi Valkeinen 	u8 init_buf[MAX_VMBUS_PKT_SIZE];
265f7018c21STomi Valkeinen 	u8 recv_buf[MAX_VMBUS_PKT_SIZE];
2663686fe96SDexuan Cui 
2673686fe96SDexuan Cui 	/* If true, the VSC notifies the VSP on every framebuffer change */
2683686fe96SDexuan Cui 	bool synchronous_fb;
2693686fe96SDexuan Cui 
2703a6fb6c4SWei Hu 	/* If true, need to copy from deferred IO mem to framebuffer mem */
2713a6fb6c4SWei Hu 	bool need_docopy;
2723a6fb6c4SWei Hu 
2733686fe96SDexuan Cui 	struct notifier_block hvfb_panic_nb;
274d21987d7SWei Hu 
275d21987d7SWei Hu 	/* Memory for deferred IO and frame buffer itself */
276d21987d7SWei Hu 	unsigned char *dio_vp;
277d21987d7SWei Hu 	unsigned char *mmio_vp;
2783a6fb6c4SWei Hu 	phys_addr_t mmio_pp;
279d21987d7SWei Hu 
280d21987d7SWei Hu 	/* Dirty rectangle, protected by delayed_refresh_lock */
281d21987d7SWei Hu 	int x1, y1, x2, y2;
282d21987d7SWei Hu 	bool delayed_refresh;
283d21987d7SWei Hu 	spinlock_t delayed_refresh_lock;
284f7018c21STomi Valkeinen };
285f7018c21STomi Valkeinen 
286f7018c21STomi Valkeinen static uint screen_width = HVFB_WIDTH;
287f7018c21STomi Valkeinen static uint screen_height = HVFB_HEIGHT;
28867e7cdb4SWei Hu static uint screen_width_max = HVFB_WIDTH;
28967e7cdb4SWei Hu static uint screen_height_max = HVFB_HEIGHT;
290f7018c21STomi Valkeinen static uint screen_depth;
291f7018c21STomi Valkeinen static uint screen_fb_size;
292d21987d7SWei Hu static uint dio_fb_size; /* FB size for deferred IO */
293f7018c21STomi Valkeinen 
294f7018c21STomi Valkeinen /* Send message to Hyper-V host */
295f7018c21STomi Valkeinen static inline int synthvid_send(struct hv_device *hdev,
296f7018c21STomi Valkeinen 				struct synthvid_msg *msg)
297f7018c21STomi Valkeinen {
298f7018c21STomi Valkeinen 	static atomic64_t request_id = ATOMIC64_INIT(0);
299f7018c21STomi Valkeinen 	int ret;
300f7018c21STomi Valkeinen 
301f7018c21STomi Valkeinen 	msg->pipe_hdr.type = PIPE_MSG_DATA;
302f7018c21STomi Valkeinen 	msg->pipe_hdr.size = msg->vid_hdr.size;
303f7018c21STomi Valkeinen 
304f7018c21STomi Valkeinen 	ret = vmbus_sendpacket(hdev->channel, msg,
305f7018c21STomi Valkeinen 			       msg->vid_hdr.size + sizeof(struct pipe_msg_hdr),
306f7018c21STomi Valkeinen 			       atomic64_inc_return(&request_id),
307f7018c21STomi Valkeinen 			       VM_PKT_DATA_INBAND, 0);
308f7018c21STomi Valkeinen 
309f7018c21STomi Valkeinen 	if (ret)
310f7018c21STomi Valkeinen 		pr_err("Unable to send packet via vmbus\n");
311f7018c21STomi Valkeinen 
312f7018c21STomi Valkeinen 	return ret;
313f7018c21STomi Valkeinen }
314f7018c21STomi Valkeinen 
315f7018c21STomi Valkeinen 
316f7018c21STomi Valkeinen /* Send screen resolution info to host */
317f7018c21STomi Valkeinen static int synthvid_send_situ(struct hv_device *hdev)
318f7018c21STomi Valkeinen {
319f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
320f7018c21STomi Valkeinen 	struct synthvid_msg msg;
321f7018c21STomi Valkeinen 
322f7018c21STomi Valkeinen 	if (!info)
323f7018c21STomi Valkeinen 		return -ENODEV;
324f7018c21STomi Valkeinen 
325f7018c21STomi Valkeinen 	memset(&msg, 0, sizeof(struct synthvid_msg));
326f7018c21STomi Valkeinen 
327f7018c21STomi Valkeinen 	msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE;
328f7018c21STomi Valkeinen 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
329f7018c21STomi Valkeinen 		sizeof(struct synthvid_situation_update);
330f7018c21STomi Valkeinen 	msg.situ.user_ctx = 0;
331f7018c21STomi Valkeinen 	msg.situ.video_output_count = 1;
332f7018c21STomi Valkeinen 	msg.situ.video_output[0].active = 1;
333f7018c21STomi Valkeinen 	msg.situ.video_output[0].vram_offset = 0;
334f7018c21STomi Valkeinen 	msg.situ.video_output[0].depth_bits = info->var.bits_per_pixel;
335f7018c21STomi Valkeinen 	msg.situ.video_output[0].width_pixels = info->var.xres;
336f7018c21STomi Valkeinen 	msg.situ.video_output[0].height_pixels = info->var.yres;
337f7018c21STomi Valkeinen 	msg.situ.video_output[0].pitch_bytes = info->fix.line_length;
338f7018c21STomi Valkeinen 
339f7018c21STomi Valkeinen 	synthvid_send(hdev, &msg);
340f7018c21STomi Valkeinen 
341f7018c21STomi Valkeinen 	return 0;
342f7018c21STomi Valkeinen }
343f7018c21STomi Valkeinen 
344f7018c21STomi Valkeinen /* Send mouse pointer info to host */
345f7018c21STomi Valkeinen static int synthvid_send_ptr(struct hv_device *hdev)
346f7018c21STomi Valkeinen {
347f7018c21STomi Valkeinen 	struct synthvid_msg msg;
348f7018c21STomi Valkeinen 
349f7018c21STomi Valkeinen 	memset(&msg, 0, sizeof(struct synthvid_msg));
350f7018c21STomi Valkeinen 	msg.vid_hdr.type = SYNTHVID_POINTER_POSITION;
351f7018c21STomi Valkeinen 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
352f7018c21STomi Valkeinen 		sizeof(struct synthvid_pointer_position);
353f7018c21STomi Valkeinen 	msg.ptr_pos.is_visible = 1;
354f7018c21STomi Valkeinen 	msg.ptr_pos.video_output = 0;
355f7018c21STomi Valkeinen 	msg.ptr_pos.image_x = 0;
356f7018c21STomi Valkeinen 	msg.ptr_pos.image_y = 0;
357f7018c21STomi Valkeinen 	synthvid_send(hdev, &msg);
358f7018c21STomi Valkeinen 
359f7018c21STomi Valkeinen 	memset(&msg, 0, sizeof(struct synthvid_msg));
360f7018c21STomi Valkeinen 	msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE;
361f7018c21STomi Valkeinen 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
362f7018c21STomi Valkeinen 		sizeof(struct synthvid_pointer_shape);
363f7018c21STomi Valkeinen 	msg.ptr_shape.part_idx = CURSOR_COMPLETE;
364f7018c21STomi Valkeinen 	msg.ptr_shape.is_argb = 1;
365f7018c21STomi Valkeinen 	msg.ptr_shape.width = 1;
366f7018c21STomi Valkeinen 	msg.ptr_shape.height = 1;
367f7018c21STomi Valkeinen 	msg.ptr_shape.hot_x = 0;
368f7018c21STomi Valkeinen 	msg.ptr_shape.hot_y = 0;
369f7018c21STomi Valkeinen 	msg.ptr_shape.data[0] = 0;
370f7018c21STomi Valkeinen 	msg.ptr_shape.data[1] = 1;
371f7018c21STomi Valkeinen 	msg.ptr_shape.data[2] = 1;
372f7018c21STomi Valkeinen 	msg.ptr_shape.data[3] = 1;
373f7018c21STomi Valkeinen 	synthvid_send(hdev, &msg);
374f7018c21STomi Valkeinen 
375f7018c21STomi Valkeinen 	return 0;
376f7018c21STomi Valkeinen }
377f7018c21STomi Valkeinen 
378f7018c21STomi Valkeinen /* Send updated screen area (dirty rectangle) location to host */
379d21987d7SWei Hu static int
380d21987d7SWei Hu synthvid_update(struct fb_info *info, int x1, int y1, int x2, int y2)
381f7018c21STomi Valkeinen {
382f7018c21STomi Valkeinen 	struct hv_device *hdev = device_to_hv_device(info->device);
383f7018c21STomi Valkeinen 	struct synthvid_msg msg;
384f7018c21STomi Valkeinen 
385f7018c21STomi Valkeinen 	memset(&msg, 0, sizeof(struct synthvid_msg));
386d21987d7SWei Hu 	if (x2 == INT_MAX)
387d21987d7SWei Hu 		x2 = info->var.xres;
388d21987d7SWei Hu 	if (y2 == INT_MAX)
389d21987d7SWei Hu 		y2 = info->var.yres;
390f7018c21STomi Valkeinen 
391f7018c21STomi Valkeinen 	msg.vid_hdr.type = SYNTHVID_DIRT;
392f7018c21STomi Valkeinen 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
393f7018c21STomi Valkeinen 		sizeof(struct synthvid_dirt);
394f7018c21STomi Valkeinen 	msg.dirt.video_output = 0;
395f7018c21STomi Valkeinen 	msg.dirt.dirt_count = 1;
396d21987d7SWei Hu 	msg.dirt.rect[0].x1 = (x1 > x2) ? 0 : x1;
397d21987d7SWei Hu 	msg.dirt.rect[0].y1 = (y1 > y2) ? 0 : y1;
398d21987d7SWei Hu 	msg.dirt.rect[0].x2 =
399d21987d7SWei Hu 		(x2 < x1 || x2 > info->var.xres) ? info->var.xres : x2;
400d21987d7SWei Hu 	msg.dirt.rect[0].y2 =
401d21987d7SWei Hu 		(y2 < y1 || y2 > info->var.yres) ? info->var.yres : y2;
402f7018c21STomi Valkeinen 
403f7018c21STomi Valkeinen 	synthvid_send(hdev, &msg);
404f7018c21STomi Valkeinen 
405f7018c21STomi Valkeinen 	return 0;
406f7018c21STomi Valkeinen }
407f7018c21STomi Valkeinen 
408d21987d7SWei Hu static void hvfb_docopy(struct hvfb_par *par,
409d21987d7SWei Hu 			unsigned long offset,
410d21987d7SWei Hu 			unsigned long size)
411d21987d7SWei Hu {
412d21987d7SWei Hu 	if (!par || !par->mmio_vp || !par->dio_vp || !par->fb_ready ||
413d21987d7SWei Hu 	    size == 0 || offset >= dio_fb_size)
414d21987d7SWei Hu 		return;
415d21987d7SWei Hu 
416d21987d7SWei Hu 	if (offset + size > dio_fb_size)
417d21987d7SWei Hu 		size = dio_fb_size - offset;
418d21987d7SWei Hu 
419d21987d7SWei Hu 	memcpy(par->mmio_vp + offset, par->dio_vp + offset, size);
420d21987d7SWei Hu }
421d21987d7SWei Hu 
422d21987d7SWei Hu /* Deferred IO callback */
423d21987d7SWei Hu static void synthvid_deferred_io(struct fb_info *p,
424d21987d7SWei Hu 				 struct list_head *pagelist)
425d21987d7SWei Hu {
426d21987d7SWei Hu 	struct hvfb_par *par = p->par;
427d21987d7SWei Hu 	struct page *page;
428d21987d7SWei Hu 	unsigned long start, end;
429d21987d7SWei Hu 	int y1, y2, miny, maxy;
430d21987d7SWei Hu 
431d21987d7SWei Hu 	miny = INT_MAX;
432d21987d7SWei Hu 	maxy = 0;
433d21987d7SWei Hu 
434d21987d7SWei Hu 	/*
435d21987d7SWei Hu 	 * Merge dirty pages. It is possible that last page cross
436d21987d7SWei Hu 	 * over the end of frame buffer row yres. This is taken care of
437d21987d7SWei Hu 	 * in synthvid_update function by clamping the y2
438d21987d7SWei Hu 	 * value to yres.
439d21987d7SWei Hu 	 */
440d21987d7SWei Hu 	list_for_each_entry(page, pagelist, lru) {
441d21987d7SWei Hu 		start = page->index << PAGE_SHIFT;
442d21987d7SWei Hu 		end = start + PAGE_SIZE - 1;
443d21987d7SWei Hu 		y1 = start / p->fix.line_length;
444d21987d7SWei Hu 		y2 = end / p->fix.line_length;
445d21987d7SWei Hu 		miny = min_t(int, miny, y1);
446d21987d7SWei Hu 		maxy = max_t(int, maxy, y2);
447d21987d7SWei Hu 
448d21987d7SWei Hu 		/* Copy from dio space to mmio address */
4493a6fb6c4SWei Hu 		if (par->fb_ready && par->need_docopy)
450d21987d7SWei Hu 			hvfb_docopy(par, start, PAGE_SIZE);
451d21987d7SWei Hu 	}
452d21987d7SWei Hu 
453d21987d7SWei Hu 	if (par->fb_ready && par->update)
454d21987d7SWei Hu 		synthvid_update(p, 0, miny, p->var.xres, maxy + 1);
455d21987d7SWei Hu }
456d21987d7SWei Hu 
457d21987d7SWei Hu static struct fb_deferred_io synthvid_defio = {
458d21987d7SWei Hu 	.delay		= HZ / 20,
459d21987d7SWei Hu 	.deferred_io	= synthvid_deferred_io,
460d21987d7SWei Hu };
461f7018c21STomi Valkeinen 
462f7018c21STomi Valkeinen /*
463f7018c21STomi Valkeinen  * Actions on received messages from host:
464f7018c21STomi Valkeinen  * Complete the wait event.
465f7018c21STomi Valkeinen  * Or, reply with screen and cursor info.
466f7018c21STomi Valkeinen  */
467f7018c21STomi Valkeinen static void synthvid_recv_sub(struct hv_device *hdev)
468f7018c21STomi Valkeinen {
469f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
470f7018c21STomi Valkeinen 	struct hvfb_par *par;
471f7018c21STomi Valkeinen 	struct synthvid_msg *msg;
472f7018c21STomi Valkeinen 
473f7018c21STomi Valkeinen 	if (!info)
474f7018c21STomi Valkeinen 		return;
475f7018c21STomi Valkeinen 
476f7018c21STomi Valkeinen 	par = info->par;
477f7018c21STomi Valkeinen 	msg = (struct synthvid_msg *)par->recv_buf;
478f7018c21STomi Valkeinen 
479f7018c21STomi Valkeinen 	/* Complete the wait event */
480f7018c21STomi Valkeinen 	if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE ||
48167e7cdb4SWei Hu 	    msg->vid_hdr.type == SYNTHVID_RESOLUTION_RESPONSE ||
482f7018c21STomi Valkeinen 	    msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) {
483f7018c21STomi Valkeinen 		memcpy(par->init_buf, msg, MAX_VMBUS_PKT_SIZE);
484f7018c21STomi Valkeinen 		complete(&par->wait);
485f7018c21STomi Valkeinen 		return;
486f7018c21STomi Valkeinen 	}
487f7018c21STomi Valkeinen 
488f7018c21STomi Valkeinen 	/* Reply with screen and cursor info */
489f7018c21STomi Valkeinen 	if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) {
490f7018c21STomi Valkeinen 		if (par->fb_ready) {
491f7018c21STomi Valkeinen 			synthvid_send_ptr(hdev);
492f7018c21STomi Valkeinen 			synthvid_send_situ(hdev);
493f7018c21STomi Valkeinen 		}
494f7018c21STomi Valkeinen 
495f7018c21STomi Valkeinen 		par->update = msg->feature_chg.is_dirt_needed;
496f7018c21STomi Valkeinen 		if (par->update)
497f7018c21STomi Valkeinen 			schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
498f7018c21STomi Valkeinen 	}
499f7018c21STomi Valkeinen }
500f7018c21STomi Valkeinen 
501f7018c21STomi Valkeinen /* Receive callback for messages from the host */
502f7018c21STomi Valkeinen static void synthvid_receive(void *ctx)
503f7018c21STomi Valkeinen {
504f7018c21STomi Valkeinen 	struct hv_device *hdev = ctx;
505f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
506f7018c21STomi Valkeinen 	struct hvfb_par *par;
507f7018c21STomi Valkeinen 	struct synthvid_msg *recv_buf;
508f7018c21STomi Valkeinen 	u32 bytes_recvd;
509f7018c21STomi Valkeinen 	u64 req_id;
510f7018c21STomi Valkeinen 	int ret;
511f7018c21STomi Valkeinen 
512f7018c21STomi Valkeinen 	if (!info)
513f7018c21STomi Valkeinen 		return;
514f7018c21STomi Valkeinen 
515f7018c21STomi Valkeinen 	par = info->par;
516f7018c21STomi Valkeinen 	recv_buf = (struct synthvid_msg *)par->recv_buf;
517f7018c21STomi Valkeinen 
518f7018c21STomi Valkeinen 	do {
519f7018c21STomi Valkeinen 		ret = vmbus_recvpacket(hdev->channel, recv_buf,
520f7018c21STomi Valkeinen 				       MAX_VMBUS_PKT_SIZE,
521f7018c21STomi Valkeinen 				       &bytes_recvd, &req_id);
522f7018c21STomi Valkeinen 		if (bytes_recvd > 0 &&
523f7018c21STomi Valkeinen 		    recv_buf->pipe_hdr.type == PIPE_MSG_DATA)
524f7018c21STomi Valkeinen 			synthvid_recv_sub(hdev);
525f7018c21STomi Valkeinen 	} while (bytes_recvd > 0 && ret == 0);
526f7018c21STomi Valkeinen }
527f7018c21STomi Valkeinen 
52867e7cdb4SWei Hu /* Check if the ver1 version is equal or greater than ver2 */
52967e7cdb4SWei Hu static inline bool synthvid_ver_ge(u32 ver1, u32 ver2)
53067e7cdb4SWei Hu {
53167e7cdb4SWei Hu 	if (SYNTHVID_VER_GET_MAJOR(ver1) > SYNTHVID_VER_GET_MAJOR(ver2) ||
53267e7cdb4SWei Hu 	    (SYNTHVID_VER_GET_MAJOR(ver1) == SYNTHVID_VER_GET_MAJOR(ver2) &&
53367e7cdb4SWei Hu 	     SYNTHVID_VER_GET_MINOR(ver1) >= SYNTHVID_VER_GET_MINOR(ver2)))
53467e7cdb4SWei Hu 		return true;
53567e7cdb4SWei Hu 
53667e7cdb4SWei Hu 	return false;
53767e7cdb4SWei Hu }
53867e7cdb4SWei Hu 
539f7018c21STomi Valkeinen /* Check synthetic video protocol version with the host */
540f7018c21STomi Valkeinen static int synthvid_negotiate_ver(struct hv_device *hdev, u32 ver)
541f7018c21STomi Valkeinen {
542f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
543f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
544f7018c21STomi Valkeinen 	struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
5457dea97e0SNicholas Mc Guire 	int ret = 0;
5467dea97e0SNicholas Mc Guire 	unsigned long t;
547f7018c21STomi Valkeinen 
548f7018c21STomi Valkeinen 	memset(msg, 0, sizeof(struct synthvid_msg));
549f7018c21STomi Valkeinen 	msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST;
550f7018c21STomi Valkeinen 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
551f7018c21STomi Valkeinen 		sizeof(struct synthvid_version_req);
552f7018c21STomi Valkeinen 	msg->ver_req.version = ver;
553f7018c21STomi Valkeinen 	synthvid_send(hdev, msg);
554f7018c21STomi Valkeinen 
555f7018c21STomi Valkeinen 	t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
556f7018c21STomi Valkeinen 	if (!t) {
557f7018c21STomi Valkeinen 		pr_err("Time out on waiting version response\n");
558f7018c21STomi Valkeinen 		ret = -ETIMEDOUT;
559f7018c21STomi Valkeinen 		goto out;
560f7018c21STomi Valkeinen 	}
561f7018c21STomi Valkeinen 	if (!msg->ver_resp.is_accepted) {
562f7018c21STomi Valkeinen 		ret = -ENODEV;
563f7018c21STomi Valkeinen 		goto out;
564f7018c21STomi Valkeinen 	}
565f7018c21STomi Valkeinen 
566f7018c21STomi Valkeinen 	par->synthvid_version = ver;
56767e7cdb4SWei Hu 	pr_info("Synthvid Version major %d, minor %d\n",
56867e7cdb4SWei Hu 		SYNTHVID_VER_GET_MAJOR(ver), SYNTHVID_VER_GET_MINOR(ver));
56967e7cdb4SWei Hu 
57067e7cdb4SWei Hu out:
57167e7cdb4SWei Hu 	return ret;
57267e7cdb4SWei Hu }
57367e7cdb4SWei Hu 
57467e7cdb4SWei Hu /* Get current resolution from the host */
57567e7cdb4SWei Hu static int synthvid_get_supported_resolution(struct hv_device *hdev)
57667e7cdb4SWei Hu {
57767e7cdb4SWei Hu 	struct fb_info *info = hv_get_drvdata(hdev);
57867e7cdb4SWei Hu 	struct hvfb_par *par = info->par;
57967e7cdb4SWei Hu 	struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
58067e7cdb4SWei Hu 	int ret = 0;
58167e7cdb4SWei Hu 	unsigned long t;
58267e7cdb4SWei Hu 	u8 index;
58367e7cdb4SWei Hu 	int i;
58467e7cdb4SWei Hu 
58567e7cdb4SWei Hu 	memset(msg, 0, sizeof(struct synthvid_msg));
58667e7cdb4SWei Hu 	msg->vid_hdr.type = SYNTHVID_RESOLUTION_REQUEST;
58767e7cdb4SWei Hu 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
58867e7cdb4SWei Hu 		sizeof(struct synthvid_supported_resolution_req);
58967e7cdb4SWei Hu 
59067e7cdb4SWei Hu 	msg->resolution_req.maximum_resolution_count =
59167e7cdb4SWei Hu 		SYNTHVID_MAX_RESOLUTION_COUNT;
59267e7cdb4SWei Hu 	synthvid_send(hdev, msg);
59367e7cdb4SWei Hu 
59467e7cdb4SWei Hu 	t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
59567e7cdb4SWei Hu 	if (!t) {
59667e7cdb4SWei Hu 		pr_err("Time out on waiting resolution response\n");
59767e7cdb4SWei Hu 		ret = -ETIMEDOUT;
59867e7cdb4SWei Hu 		goto out;
59967e7cdb4SWei Hu 	}
60067e7cdb4SWei Hu 
60167e7cdb4SWei Hu 	if (msg->resolution_resp.resolution_count == 0) {
60267e7cdb4SWei Hu 		pr_err("No supported resolutions\n");
60367e7cdb4SWei Hu 		ret = -ENODEV;
60467e7cdb4SWei Hu 		goto out;
60567e7cdb4SWei Hu 	}
60667e7cdb4SWei Hu 
60767e7cdb4SWei Hu 	index = msg->resolution_resp.default_resolution_index;
60867e7cdb4SWei Hu 	if (index >= msg->resolution_resp.resolution_count) {
60967e7cdb4SWei Hu 		pr_err("Invalid resolution index: %d\n", index);
61067e7cdb4SWei Hu 		ret = -ENODEV;
61167e7cdb4SWei Hu 		goto out;
61267e7cdb4SWei Hu 	}
61367e7cdb4SWei Hu 
61467e7cdb4SWei Hu 	for (i = 0; i < msg->resolution_resp.resolution_count; i++) {
61567e7cdb4SWei Hu 		screen_width_max = max_t(unsigned int, screen_width_max,
61667e7cdb4SWei Hu 		    msg->resolution_resp.supported_resolution[i].width);
61767e7cdb4SWei Hu 		screen_height_max = max_t(unsigned int, screen_height_max,
61867e7cdb4SWei Hu 		    msg->resolution_resp.supported_resolution[i].height);
61967e7cdb4SWei Hu 	}
62067e7cdb4SWei Hu 
62167e7cdb4SWei Hu 	screen_width =
62267e7cdb4SWei Hu 		msg->resolution_resp.supported_resolution[index].width;
62367e7cdb4SWei Hu 	screen_height =
62467e7cdb4SWei Hu 		msg->resolution_resp.supported_resolution[index].height;
625f7018c21STomi Valkeinen 
626f7018c21STomi Valkeinen out:
627f7018c21STomi Valkeinen 	return ret;
628f7018c21STomi Valkeinen }
629f7018c21STomi Valkeinen 
630f7018c21STomi Valkeinen /* Connect to VSP (Virtual Service Provider) on host */
631f7018c21STomi Valkeinen static int synthvid_connect_vsp(struct hv_device *hdev)
632f7018c21STomi Valkeinen {
633f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
634f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
635f7018c21STomi Valkeinen 	int ret;
636f7018c21STomi Valkeinen 
637f7018c21STomi Valkeinen 	ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE,
638f7018c21STomi Valkeinen 			 NULL, 0, synthvid_receive, hdev);
639f7018c21STomi Valkeinen 	if (ret) {
640f7018c21STomi Valkeinen 		pr_err("Unable to open vmbus channel\n");
641f7018c21STomi Valkeinen 		return ret;
642f7018c21STomi Valkeinen 	}
643f7018c21STomi Valkeinen 
644f7018c21STomi Valkeinen 	/* Negotiate the protocol version with host */
64567e7cdb4SWei Hu 	switch (vmbus_proto_version) {
64667e7cdb4SWei Hu 	case VERSION_WIN10:
64767e7cdb4SWei Hu 	case VERSION_WIN10_V5:
64867e7cdb4SWei Hu 		ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
64967e7cdb4SWei Hu 		if (!ret)
65067e7cdb4SWei Hu 			break;
651df561f66SGustavo A. R. Silva 		fallthrough;
65267e7cdb4SWei Hu 	case VERSION_WIN8:
65367e7cdb4SWei Hu 	case VERSION_WIN8_1:
654f7018c21STomi Valkeinen 		ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8);
65567e7cdb4SWei Hu 		if (!ret)
65667e7cdb4SWei Hu 			break;
657df561f66SGustavo A. R. Silva 		fallthrough;
65867e7cdb4SWei Hu 	case VERSION_WS2008:
65967e7cdb4SWei Hu 	case VERSION_WIN7:
66067e7cdb4SWei Hu 		ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN7);
66167e7cdb4SWei Hu 		break;
66267e7cdb4SWei Hu 	default:
66367e7cdb4SWei Hu 		ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
66467e7cdb4SWei Hu 		break;
66567e7cdb4SWei Hu 	}
666f7018c21STomi Valkeinen 
667f7018c21STomi Valkeinen 	if (ret) {
668f7018c21STomi Valkeinen 		pr_err("Synthetic video device version not accepted\n");
669f7018c21STomi Valkeinen 		goto error;
670f7018c21STomi Valkeinen 	}
671f7018c21STomi Valkeinen 
672f7018c21STomi Valkeinen 	if (par->synthvid_version == SYNTHVID_VERSION_WIN7)
673f7018c21STomi Valkeinen 		screen_depth = SYNTHVID_DEPTH_WIN7;
674f7018c21STomi Valkeinen 	else
675f7018c21STomi Valkeinen 		screen_depth = SYNTHVID_DEPTH_WIN8;
676f7018c21STomi Valkeinen 
67767e7cdb4SWei Hu 	if (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10)) {
67867e7cdb4SWei Hu 		ret = synthvid_get_supported_resolution(hdev);
67967e7cdb4SWei Hu 		if (ret)
68067e7cdb4SWei Hu 			pr_info("Failed to get supported resolution from host, use default\n");
68167e7cdb4SWei Hu 	}
68267e7cdb4SWei Hu 
683f7018c21STomi Valkeinen 	screen_fb_size = hdev->channel->offermsg.offer.
684f7018c21STomi Valkeinen 				mmio_megabytes * 1024 * 1024;
685f7018c21STomi Valkeinen 
686f7018c21STomi Valkeinen 	return 0;
687f7018c21STomi Valkeinen 
688f7018c21STomi Valkeinen error:
689f7018c21STomi Valkeinen 	vmbus_close(hdev->channel);
690f7018c21STomi Valkeinen 	return ret;
691f7018c21STomi Valkeinen }
692f7018c21STomi Valkeinen 
693f7018c21STomi Valkeinen /* Send VRAM and Situation messages to the host */
694f7018c21STomi Valkeinen static int synthvid_send_config(struct hv_device *hdev)
695f7018c21STomi Valkeinen {
696f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
697f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
698f7018c21STomi Valkeinen 	struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
6997dea97e0SNicholas Mc Guire 	int ret = 0;
7007dea97e0SNicholas Mc Guire 	unsigned long t;
701f7018c21STomi Valkeinen 
702f7018c21STomi Valkeinen 	/* Send VRAM location */
703f7018c21STomi Valkeinen 	memset(msg, 0, sizeof(struct synthvid_msg));
704f7018c21STomi Valkeinen 	msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION;
705f7018c21STomi Valkeinen 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
706f7018c21STomi Valkeinen 		sizeof(struct synthvid_vram_location);
707d21987d7SWei Hu 	msg->vram.user_ctx = msg->vram.vram_gpa = par->mmio_pp;
708f7018c21STomi Valkeinen 	msg->vram.is_vram_gpa_specified = 1;
709f7018c21STomi Valkeinen 	synthvid_send(hdev, msg);
710f7018c21STomi Valkeinen 
711f7018c21STomi Valkeinen 	t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
712f7018c21STomi Valkeinen 	if (!t) {
713f7018c21STomi Valkeinen 		pr_err("Time out on waiting vram location ack\n");
714f7018c21STomi Valkeinen 		ret = -ETIMEDOUT;
715f7018c21STomi Valkeinen 		goto out;
716f7018c21STomi Valkeinen 	}
717d21987d7SWei Hu 	if (msg->vram_ack.user_ctx != par->mmio_pp) {
718f7018c21STomi Valkeinen 		pr_err("Unable to set VRAM location\n");
719f7018c21STomi Valkeinen 		ret = -ENODEV;
720f7018c21STomi Valkeinen 		goto out;
721f7018c21STomi Valkeinen 	}
722f7018c21STomi Valkeinen 
723f7018c21STomi Valkeinen 	/* Send pointer and situation update */
724f7018c21STomi Valkeinen 	synthvid_send_ptr(hdev);
725f7018c21STomi Valkeinen 	synthvid_send_situ(hdev);
726f7018c21STomi Valkeinen 
727f7018c21STomi Valkeinen out:
728f7018c21STomi Valkeinen 	return ret;
729f7018c21STomi Valkeinen }
730f7018c21STomi Valkeinen 
731f7018c21STomi Valkeinen 
732f7018c21STomi Valkeinen /*
733f7018c21STomi Valkeinen  * Delayed work callback:
734d21987d7SWei Hu  * It is scheduled to call whenever update request is received and it has
735d21987d7SWei Hu  * not been called in last HVFB_ONDEMAND_THROTTLE time interval.
736f7018c21STomi Valkeinen  */
737f7018c21STomi Valkeinen static void hvfb_update_work(struct work_struct *w)
738f7018c21STomi Valkeinen {
739f7018c21STomi Valkeinen 	struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work);
740f7018c21STomi Valkeinen 	struct fb_info *info = par->info;
741d21987d7SWei Hu 	unsigned long flags;
742d21987d7SWei Hu 	int x1, x2, y1, y2;
743d21987d7SWei Hu 	int j;
744f7018c21STomi Valkeinen 
745d21987d7SWei Hu 	spin_lock_irqsave(&par->delayed_refresh_lock, flags);
746d21987d7SWei Hu 	/* Reset the request flag */
747d21987d7SWei Hu 	par->delayed_refresh = false;
748f7018c21STomi Valkeinen 
749d21987d7SWei Hu 	/* Store the dirty rectangle to local variables */
750d21987d7SWei Hu 	x1 = par->x1;
751d21987d7SWei Hu 	x2 = par->x2;
752d21987d7SWei Hu 	y1 = par->y1;
753d21987d7SWei Hu 	y2 = par->y2;
754d21987d7SWei Hu 
755d21987d7SWei Hu 	/* Clear dirty rectangle */
756d21987d7SWei Hu 	par->x1 = par->y1 = INT_MAX;
757d21987d7SWei Hu 	par->x2 = par->y2 = 0;
758d21987d7SWei Hu 
759d21987d7SWei Hu 	spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
760d21987d7SWei Hu 
761d21987d7SWei Hu 	if (x1 > info->var.xres || x2 > info->var.xres ||
762d21987d7SWei Hu 	    y1 > info->var.yres || y2 > info->var.yres || x2 <= x1)
763d21987d7SWei Hu 		return;
764d21987d7SWei Hu 
765d21987d7SWei Hu 	/* Copy the dirty rectangle to frame buffer memory */
7663a6fb6c4SWei Hu 	if (par->need_docopy)
7673a6fb6c4SWei Hu 		for (j = y1; j < y2; j++)
768d21987d7SWei Hu 			hvfb_docopy(par,
769d21987d7SWei Hu 				    j * info->fix.line_length +
770d21987d7SWei Hu 				    (x1 * screen_depth / 8),
771d21987d7SWei Hu 				    (x2 - x1) * screen_depth / 8);
772d21987d7SWei Hu 
773d21987d7SWei Hu 	/* Refresh */
774d21987d7SWei Hu 	if (par->fb_ready && par->update)
775d21987d7SWei Hu 		synthvid_update(info, x1, y1, x2, y2);
776d21987d7SWei Hu }
777d21987d7SWei Hu 
778d21987d7SWei Hu /*
779d21987d7SWei Hu  * Control the on-demand refresh frequency. It schedules a delayed
780d21987d7SWei Hu  * screen update if it has not yet.
781d21987d7SWei Hu  */
782d21987d7SWei Hu static void hvfb_ondemand_refresh_throttle(struct hvfb_par *par,
783d21987d7SWei Hu 					   int x1, int y1, int w, int h)
784d21987d7SWei Hu {
785d21987d7SWei Hu 	unsigned long flags;
786d21987d7SWei Hu 	int x2 = x1 + w;
787d21987d7SWei Hu 	int y2 = y1 + h;
788d21987d7SWei Hu 
789d21987d7SWei Hu 	spin_lock_irqsave(&par->delayed_refresh_lock, flags);
790d21987d7SWei Hu 
791d21987d7SWei Hu 	/* Merge dirty rectangle */
792d21987d7SWei Hu 	par->x1 = min_t(int, par->x1, x1);
793d21987d7SWei Hu 	par->y1 = min_t(int, par->y1, y1);
794d21987d7SWei Hu 	par->x2 = max_t(int, par->x2, x2);
795d21987d7SWei Hu 	par->y2 = max_t(int, par->y2, y2);
796d21987d7SWei Hu 
797d21987d7SWei Hu 	/* Schedule a delayed screen update if not yet */
798d21987d7SWei Hu 	if (par->delayed_refresh == false) {
799d21987d7SWei Hu 		schedule_delayed_work(&par->dwork,
800d21987d7SWei Hu 				      HVFB_ONDEMAND_THROTTLE);
801d21987d7SWei Hu 		par->delayed_refresh = true;
802d21987d7SWei Hu 	}
803d21987d7SWei Hu 
804d21987d7SWei Hu 	spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
805f7018c21STomi Valkeinen }
806f7018c21STomi Valkeinen 
8073686fe96SDexuan Cui static int hvfb_on_panic(struct notifier_block *nb,
8083686fe96SDexuan Cui 			 unsigned long e, void *p)
8093686fe96SDexuan Cui {
8103686fe96SDexuan Cui 	struct hvfb_par *par;
8113686fe96SDexuan Cui 	struct fb_info *info;
8123686fe96SDexuan Cui 
8133686fe96SDexuan Cui 	par = container_of(nb, struct hvfb_par, hvfb_panic_nb);
8143686fe96SDexuan Cui 	par->synchronous_fb = true;
8153686fe96SDexuan Cui 	info = par->info;
8163a6fb6c4SWei Hu 	if (par->need_docopy)
817d21987d7SWei Hu 		hvfb_docopy(par, 0, dio_fb_size);
818d21987d7SWei Hu 	synthvid_update(info, 0, 0, INT_MAX, INT_MAX);
8193686fe96SDexuan Cui 
8203686fe96SDexuan Cui 	return NOTIFY_DONE;
8213686fe96SDexuan Cui }
822f7018c21STomi Valkeinen 
823f7018c21STomi Valkeinen /* Framebuffer operation handlers */
824f7018c21STomi Valkeinen 
825f7018c21STomi Valkeinen static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
826f7018c21STomi Valkeinen {
827f7018c21STomi Valkeinen 	if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN ||
828f7018c21STomi Valkeinen 	    var->xres > screen_width || var->yres >  screen_height ||
829f7018c21STomi Valkeinen 	    var->bits_per_pixel != screen_depth)
830f7018c21STomi Valkeinen 		return -EINVAL;
831f7018c21STomi Valkeinen 
832f7018c21STomi Valkeinen 	var->xres_virtual = var->xres;
833f7018c21STomi Valkeinen 	var->yres_virtual = var->yres;
834f7018c21STomi Valkeinen 
835f7018c21STomi Valkeinen 	return 0;
836f7018c21STomi Valkeinen }
837f7018c21STomi Valkeinen 
838f7018c21STomi Valkeinen static int hvfb_set_par(struct fb_info *info)
839f7018c21STomi Valkeinen {
840f7018c21STomi Valkeinen 	struct hv_device *hdev = device_to_hv_device(info->device);
841f7018c21STomi Valkeinen 
842f7018c21STomi Valkeinen 	return synthvid_send_situ(hdev);
843f7018c21STomi Valkeinen }
844f7018c21STomi Valkeinen 
845f7018c21STomi Valkeinen 
846f7018c21STomi Valkeinen static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf)
847f7018c21STomi Valkeinen {
848f7018c21STomi Valkeinen 	return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset;
849f7018c21STomi Valkeinen }
850f7018c21STomi Valkeinen 
851f7018c21STomi Valkeinen static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
852f7018c21STomi Valkeinen 			  unsigned blue, unsigned transp, struct fb_info *info)
853f7018c21STomi Valkeinen {
854f7018c21STomi Valkeinen 	u32 *pal = info->pseudo_palette;
855f7018c21STomi Valkeinen 
856f7018c21STomi Valkeinen 	if (regno > 15)
857f7018c21STomi Valkeinen 		return -EINVAL;
858f7018c21STomi Valkeinen 
859f7018c21STomi Valkeinen 	pal[regno] = chan_to_field(red, &info->var.red)
860f7018c21STomi Valkeinen 		| chan_to_field(green, &info->var.green)
861f7018c21STomi Valkeinen 		| chan_to_field(blue, &info->var.blue)
862f7018c21STomi Valkeinen 		| chan_to_field(transp, &info->var.transp);
863f7018c21STomi Valkeinen 
864f7018c21STomi Valkeinen 	return 0;
865f7018c21STomi Valkeinen }
866f7018c21STomi Valkeinen 
867f7018c21STomi Valkeinen static int hvfb_blank(int blank, struct fb_info *info)
868f7018c21STomi Valkeinen {
869f7018c21STomi Valkeinen 	return 1;	/* get fb_blank to set the colormap to all black */
870f7018c21STomi Valkeinen }
871f7018c21STomi Valkeinen 
8723686fe96SDexuan Cui static void hvfb_cfb_fillrect(struct fb_info *p,
8733686fe96SDexuan Cui 			      const struct fb_fillrect *rect)
8743686fe96SDexuan Cui {
8753686fe96SDexuan Cui 	struct hvfb_par *par = p->par;
8763686fe96SDexuan Cui 
8773686fe96SDexuan Cui 	cfb_fillrect(p, rect);
8783686fe96SDexuan Cui 	if (par->synchronous_fb)
879d21987d7SWei Hu 		synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
880d21987d7SWei Hu 	else
881d21987d7SWei Hu 		hvfb_ondemand_refresh_throttle(par, rect->dx, rect->dy,
882d21987d7SWei Hu 					       rect->width, rect->height);
8833686fe96SDexuan Cui }
8843686fe96SDexuan Cui 
8853686fe96SDexuan Cui static void hvfb_cfb_copyarea(struct fb_info *p,
8863686fe96SDexuan Cui 			      const struct fb_copyarea *area)
8873686fe96SDexuan Cui {
8883686fe96SDexuan Cui 	struct hvfb_par *par = p->par;
8893686fe96SDexuan Cui 
8903686fe96SDexuan Cui 	cfb_copyarea(p, area);
8913686fe96SDexuan Cui 	if (par->synchronous_fb)
892d21987d7SWei Hu 		synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
893d21987d7SWei Hu 	else
894d21987d7SWei Hu 		hvfb_ondemand_refresh_throttle(par, area->dx, area->dy,
895d21987d7SWei Hu 					       area->width, area->height);
8963686fe96SDexuan Cui }
8973686fe96SDexuan Cui 
8983686fe96SDexuan Cui static void hvfb_cfb_imageblit(struct fb_info *p,
8993686fe96SDexuan Cui 			       const struct fb_image *image)
9003686fe96SDexuan Cui {
9013686fe96SDexuan Cui 	struct hvfb_par *par = p->par;
9023686fe96SDexuan Cui 
9033686fe96SDexuan Cui 	cfb_imageblit(p, image);
9043686fe96SDexuan Cui 	if (par->synchronous_fb)
905d21987d7SWei Hu 		synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
906d21987d7SWei Hu 	else
907d21987d7SWei Hu 		hvfb_ondemand_refresh_throttle(par, image->dx, image->dy,
908d21987d7SWei Hu 					       image->width, image->height);
9093686fe96SDexuan Cui }
9103686fe96SDexuan Cui 
9118a48ac33SJani Nikula static const struct fb_ops hvfb_ops = {
912f7018c21STomi Valkeinen 	.owner = THIS_MODULE,
913f7018c21STomi Valkeinen 	.fb_check_var = hvfb_check_var,
914f7018c21STomi Valkeinen 	.fb_set_par = hvfb_set_par,
915f7018c21STomi Valkeinen 	.fb_setcolreg = hvfb_setcolreg,
9163686fe96SDexuan Cui 	.fb_fillrect = hvfb_cfb_fillrect,
9173686fe96SDexuan Cui 	.fb_copyarea = hvfb_cfb_copyarea,
9183686fe96SDexuan Cui 	.fb_imageblit = hvfb_cfb_imageblit,
919f7018c21STomi Valkeinen 	.fb_blank = hvfb_blank,
920f7018c21STomi Valkeinen };
921f7018c21STomi Valkeinen 
922f7018c21STomi Valkeinen 
923f7018c21STomi Valkeinen /* Get options from kernel paramenter "video=" */
924f7018c21STomi Valkeinen static void hvfb_get_option(struct fb_info *info)
925f7018c21STomi Valkeinen {
926f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
927f7018c21STomi Valkeinen 	char *opt = NULL, *p;
928f7018c21STomi Valkeinen 	uint x = 0, y = 0;
929f7018c21STomi Valkeinen 
930f7018c21STomi Valkeinen 	if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt)
931f7018c21STomi Valkeinen 		return;
932f7018c21STomi Valkeinen 
933f7018c21STomi Valkeinen 	p = strsep(&opt, "x");
934f7018c21STomi Valkeinen 	if (!*p || kstrtouint(p, 0, &x) ||
935f7018c21STomi Valkeinen 	    !opt || !*opt || kstrtouint(opt, 0, &y)) {
936f7018c21STomi Valkeinen 		pr_err("Screen option is invalid: skipped\n");
937f7018c21STomi Valkeinen 		return;
938f7018c21STomi Valkeinen 	}
939f7018c21STomi Valkeinen 
940f7018c21STomi Valkeinen 	if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN ||
94167e7cdb4SWei Hu 	    (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10) &&
94267e7cdb4SWei Hu 	    (x > screen_width_max || y > screen_height_max)) ||
943f7018c21STomi Valkeinen 	    (par->synthvid_version == SYNTHVID_VERSION_WIN8 &&
944f7018c21STomi Valkeinen 	     x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8) ||
945f7018c21STomi Valkeinen 	    (par->synthvid_version == SYNTHVID_VERSION_WIN7 &&
946f7018c21STomi Valkeinen 	     (x > SYNTHVID_WIDTH_MAX_WIN7 || y > SYNTHVID_HEIGHT_MAX_WIN7))) {
947f7018c21STomi Valkeinen 		pr_err("Screen resolution option is out of range: skipped\n");
948f7018c21STomi Valkeinen 		return;
949f7018c21STomi Valkeinen 	}
950f7018c21STomi Valkeinen 
951f7018c21STomi Valkeinen 	screen_width = x;
952f7018c21STomi Valkeinen 	screen_height = y;
953f7018c21STomi Valkeinen 	return;
954f7018c21STomi Valkeinen }
955f7018c21STomi Valkeinen 
9563a6fb6c4SWei Hu /*
9573a6fb6c4SWei Hu  * Allocate enough contiguous physical memory.
9583a6fb6c4SWei Hu  * Return physical address if succeeded or -1 if failed.
9593a6fb6c4SWei Hu  */
9603a6fb6c4SWei Hu static phys_addr_t hvfb_get_phymem(struct hv_device *hdev,
9613a6fb6c4SWei Hu 				   unsigned int request_size)
9623a6fb6c4SWei Hu {
9633a6fb6c4SWei Hu 	struct page *page = NULL;
9643a6fb6c4SWei Hu 	dma_addr_t dma_handle;
9653a6fb6c4SWei Hu 	void *vmem;
9663a6fb6c4SWei Hu 	phys_addr_t paddr = 0;
9673a6fb6c4SWei Hu 	unsigned int order = get_order(request_size);
9683a6fb6c4SWei Hu 
9693a6fb6c4SWei Hu 	if (request_size == 0)
9703a6fb6c4SWei Hu 		return -1;
9713a6fb6c4SWei Hu 
9723a6fb6c4SWei Hu 	if (order < MAX_ORDER) {
9733a6fb6c4SWei Hu 		/* Call alloc_pages if the size is less than 2^MAX_ORDER */
9743a6fb6c4SWei Hu 		page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
9753a6fb6c4SWei Hu 		if (!page)
9763a6fb6c4SWei Hu 			return -1;
9773a6fb6c4SWei Hu 
9783a6fb6c4SWei Hu 		paddr = (page_to_pfn(page) << PAGE_SHIFT);
9793a6fb6c4SWei Hu 	} else {
9803a6fb6c4SWei Hu 		/* Allocate from CMA */
9813a6fb6c4SWei Hu 		hdev->device.coherent_dma_mask = DMA_BIT_MASK(64);
9823a6fb6c4SWei Hu 
9833a6fb6c4SWei Hu 		vmem = dma_alloc_coherent(&hdev->device,
9843a6fb6c4SWei Hu 					  round_up(request_size, PAGE_SIZE),
9853a6fb6c4SWei Hu 					  &dma_handle,
9863a6fb6c4SWei Hu 					  GFP_KERNEL | __GFP_NOWARN);
9873a6fb6c4SWei Hu 
9883a6fb6c4SWei Hu 		if (!vmem)
9893a6fb6c4SWei Hu 			return -1;
9903a6fb6c4SWei Hu 
9913a6fb6c4SWei Hu 		paddr = virt_to_phys(vmem);
9923a6fb6c4SWei Hu 	}
9933a6fb6c4SWei Hu 
9943a6fb6c4SWei Hu 	return paddr;
9953a6fb6c4SWei Hu }
9963a6fb6c4SWei Hu 
9973a6fb6c4SWei Hu /* Release contiguous physical memory */
9983a6fb6c4SWei Hu static void hvfb_release_phymem(struct hv_device *hdev,
9993a6fb6c4SWei Hu 				phys_addr_t paddr, unsigned int size)
10003a6fb6c4SWei Hu {
10013a6fb6c4SWei Hu 	unsigned int order = get_order(size);
10023a6fb6c4SWei Hu 
10033a6fb6c4SWei Hu 	if (order < MAX_ORDER)
10043a6fb6c4SWei Hu 		__free_pages(pfn_to_page(paddr >> PAGE_SHIFT), order);
10053a6fb6c4SWei Hu 	else
10063a6fb6c4SWei Hu 		dma_free_coherent(&hdev->device,
10073a6fb6c4SWei Hu 				  round_up(size, PAGE_SIZE),
10083a6fb6c4SWei Hu 				  phys_to_virt(paddr),
10093a6fb6c4SWei Hu 				  paddr);
10103a6fb6c4SWei Hu }
10113a6fb6c4SWei Hu 
1012f7018c21STomi Valkeinen 
1013f7018c21STomi Valkeinen /* Get framebuffer memory from Hyper-V video pci space */
101435464483SJake Oshins static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info)
1015f7018c21STomi Valkeinen {
1016f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
1017f7018c21STomi Valkeinen 	struct pci_dev *pdev  = NULL;
1018f7018c21STomi Valkeinen 	void __iomem *fb_virt;
1019f7018c21STomi Valkeinen 	int gen2vm = efi_enabled(EFI_BOOT);
102035464483SJake Oshins 	resource_size_t pot_start, pot_end;
10213a6fb6c4SWei Hu 	phys_addr_t paddr;
1022f7018c21STomi Valkeinen 	int ret;
1023f7018c21STomi Valkeinen 
10243a6fb6c4SWei Hu 	info->apertures = alloc_apertures(1);
10253a6fb6c4SWei Hu 	if (!info->apertures)
10263a6fb6c4SWei Hu 		return -ENOMEM;
10273a6fb6c4SWei Hu 
10283a6fb6c4SWei Hu 	if (!gen2vm) {
10293a6fb6c4SWei Hu 		pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
10303a6fb6c4SWei Hu 			PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
10313a6fb6c4SWei Hu 		if (!pdev) {
10323a6fb6c4SWei Hu 			pr_err("Unable to find PCI Hyper-V video\n");
10333a6fb6c4SWei Hu 			kfree(info->apertures);
10343a6fb6c4SWei Hu 			return -ENODEV;
10353a6fb6c4SWei Hu 		}
10363a6fb6c4SWei Hu 
10373a6fb6c4SWei Hu 		info->apertures->ranges[0].base = pci_resource_start(pdev, 0);
10383a6fb6c4SWei Hu 		info->apertures->ranges[0].size = pci_resource_len(pdev, 0);
10393a6fb6c4SWei Hu 
10403a6fb6c4SWei Hu 		/*
10413a6fb6c4SWei Hu 		 * For Gen 1 VM, we can directly use the contiguous memory
10423a6fb6c4SWei Hu 		 * from VM. If we succeed, deferred IO happens directly
10433a6fb6c4SWei Hu 		 * on this allocated framebuffer memory, avoiding extra
10443a6fb6c4SWei Hu 		 * memory copy.
10453a6fb6c4SWei Hu 		 */
10463a6fb6c4SWei Hu 		paddr = hvfb_get_phymem(hdev, screen_fb_size);
10473a6fb6c4SWei Hu 		if (paddr != (phys_addr_t) -1) {
10483a6fb6c4SWei Hu 			par->mmio_pp = paddr;
10493a6fb6c4SWei Hu 			par->mmio_vp = par->dio_vp = __va(paddr);
10503a6fb6c4SWei Hu 
10513a6fb6c4SWei Hu 			info->fix.smem_start = paddr;
10523a6fb6c4SWei Hu 			info->fix.smem_len = screen_fb_size;
10533a6fb6c4SWei Hu 			info->screen_base = par->mmio_vp;
10543a6fb6c4SWei Hu 			info->screen_size = screen_fb_size;
10553a6fb6c4SWei Hu 
10563a6fb6c4SWei Hu 			par->need_docopy = false;
10573a6fb6c4SWei Hu 			goto getmem_done;
10583a6fb6c4SWei Hu 		}
10593a6fb6c4SWei Hu 		pr_info("Unable to allocate enough contiguous physical memory on Gen 1 VM. Using MMIO instead.\n");
10603a6fb6c4SWei Hu 	} else {
10613a6fb6c4SWei Hu 		info->apertures->ranges[0].base = screen_info.lfb_base;
10623a6fb6c4SWei Hu 		info->apertures->ranges[0].size = screen_info.lfb_size;
10633a6fb6c4SWei Hu 	}
10643a6fb6c4SWei Hu 
10653a6fb6c4SWei Hu 	/*
10663a6fb6c4SWei Hu 	 * Cannot use the contiguous physical memory.
10673a6fb6c4SWei Hu 	 * Allocate mmio space for framebuffer.
10683a6fb6c4SWei Hu 	 */
1069d21987d7SWei Hu 	dio_fb_size =
1070d21987d7SWei Hu 		screen_width * screen_height * screen_depth / 8;
1071d21987d7SWei Hu 
1072f7018c21STomi Valkeinen 	if (gen2vm) {
107335464483SJake Oshins 		pot_start = 0;
107435464483SJake Oshins 		pot_end = -1;
1075f7018c21STomi Valkeinen 	} else {
1076f7018c21STomi Valkeinen 		if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
107767e7cdb4SWei Hu 		    pci_resource_len(pdev, 0) < screen_fb_size) {
107867e7cdb4SWei Hu 			pr_err("Resource not available or (0x%lx < 0x%lx)\n",
107967e7cdb4SWei Hu 			       (unsigned long) pci_resource_len(pdev, 0),
108067e7cdb4SWei Hu 			       (unsigned long) screen_fb_size);
1081f7018c21STomi Valkeinen 			goto err1;
108267e7cdb4SWei Hu 		}
1083f7018c21STomi Valkeinen 
108435464483SJake Oshins 		pot_end = pci_resource_end(pdev, 0);
108535464483SJake Oshins 		pot_start = pot_end - screen_fb_size + 1;
1086f7018c21STomi Valkeinen 	}
1087f7018c21STomi Valkeinen 
108835464483SJake Oshins 	ret = vmbus_allocate_mmio(&par->mem, hdev, pot_start, pot_end,
108935464483SJake Oshins 				  screen_fb_size, 0x100000, true);
109035464483SJake Oshins 	if (ret != 0) {
109135464483SJake Oshins 		pr_err("Unable to allocate framebuffer memory\n");
109235464483SJake Oshins 		goto err1;
109335464483SJake Oshins 	}
109435464483SJake Oshins 
1095*5f1251a4SDexuan Cui 	/*
1096*5f1251a4SDexuan Cui 	 * Map the VRAM cacheable for performance. This is also required for
1097*5f1251a4SDexuan Cui 	 * VM Connect to display properly for ARM64 Linux VM, as the host also
1098*5f1251a4SDexuan Cui 	 * maps the VRAM cacheable.
1099*5f1251a4SDexuan Cui 	 */
1100*5f1251a4SDexuan Cui 	fb_virt = ioremap_cache(par->mem->start, screen_fb_size);
1101f7018c21STomi Valkeinen 	if (!fb_virt)
1102f7018c21STomi Valkeinen 		goto err2;
1103f7018c21STomi Valkeinen 
1104d21987d7SWei Hu 	/* Allocate memory for deferred IO */
1105d21987d7SWei Hu 	par->dio_vp = vzalloc(round_up(dio_fb_size, PAGE_SIZE));
1106d21987d7SWei Hu 	if (par->dio_vp == NULL)
1107d21987d7SWei Hu 		goto err3;
1108d21987d7SWei Hu 
1109d21987d7SWei Hu 	/* Physical address of FB device */
1110d21987d7SWei Hu 	par->mmio_pp = par->mem->start;
1111d21987d7SWei Hu 	/* Virtual address of FB device */
1112d21987d7SWei Hu 	par->mmio_vp = (unsigned char *) fb_virt;
1113d21987d7SWei Hu 
111435464483SJake Oshins 	info->fix.smem_start = par->mem->start;
1115d21987d7SWei Hu 	info->fix.smem_len = dio_fb_size;
1116d21987d7SWei Hu 	info->screen_base = par->dio_vp;
1117d21987d7SWei Hu 	info->screen_size = dio_fb_size;
1118f7018c21STomi Valkeinen 
11193a6fb6c4SWei Hu getmem_done:
11203a6fb6c4SWei Hu 	remove_conflicting_framebuffers(info->apertures,
11213a6fb6c4SWei Hu 					KBUILD_MODNAME, false);
1122f7018c21STomi Valkeinen 	if (!gen2vm)
1123f7018c21STomi Valkeinen 		pci_dev_put(pdev);
11243a6fb6c4SWei Hu 	kfree(info->apertures);
1125f7018c21STomi Valkeinen 
1126f7018c21STomi Valkeinen 	return 0;
1127f7018c21STomi Valkeinen 
1128f7018c21STomi Valkeinen err3:
1129f7018c21STomi Valkeinen 	iounmap(fb_virt);
1130f7018c21STomi Valkeinen err2:
1131696ca5e8SJake Oshins 	vmbus_free_mmio(par->mem->start, screen_fb_size);
113235464483SJake Oshins 	par->mem = NULL;
1133f7018c21STomi Valkeinen err1:
1134f7018c21STomi Valkeinen 	if (!gen2vm)
1135f7018c21STomi Valkeinen 		pci_dev_put(pdev);
11363a6fb6c4SWei Hu 	kfree(info->apertures);
1137f7018c21STomi Valkeinen 
1138f7018c21STomi Valkeinen 	return -ENOMEM;
1139f7018c21STomi Valkeinen }
1140f7018c21STomi Valkeinen 
1141f7018c21STomi Valkeinen /* Release the framebuffer */
11423a6fb6c4SWei Hu static void hvfb_putmem(struct hv_device *hdev, struct fb_info *info)
1143f7018c21STomi Valkeinen {
1144f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
1145f7018c21STomi Valkeinen 
11463a6fb6c4SWei Hu 	if (par->need_docopy) {
1147d21987d7SWei Hu 		vfree(par->dio_vp);
1148f7018c21STomi Valkeinen 		iounmap(info->screen_base);
1149696ca5e8SJake Oshins 		vmbus_free_mmio(par->mem->start, screen_fb_size);
11503a6fb6c4SWei Hu 	} else {
11513a6fb6c4SWei Hu 		hvfb_release_phymem(hdev, info->fix.smem_start,
11523a6fb6c4SWei Hu 				    screen_fb_size);
11533a6fb6c4SWei Hu 	}
11543a6fb6c4SWei Hu 
115535464483SJake Oshins 	par->mem = NULL;
1156f7018c21STomi Valkeinen }
1157f7018c21STomi Valkeinen 
1158f7018c21STomi Valkeinen 
1159f7018c21STomi Valkeinen static int hvfb_probe(struct hv_device *hdev,
1160f7018c21STomi Valkeinen 		      const struct hv_vmbus_device_id *dev_id)
1161f7018c21STomi Valkeinen {
1162f7018c21STomi Valkeinen 	struct fb_info *info;
1163f7018c21STomi Valkeinen 	struct hvfb_par *par;
1164f7018c21STomi Valkeinen 	int ret;
1165f7018c21STomi Valkeinen 
1166f7018c21STomi Valkeinen 	info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device);
11670adcdbcbSBartlomiej Zolnierkiewicz 	if (!info)
1168f7018c21STomi Valkeinen 		return -ENOMEM;
1169f7018c21STomi Valkeinen 
1170f7018c21STomi Valkeinen 	par = info->par;
1171f7018c21STomi Valkeinen 	par->info = info;
1172f7018c21STomi Valkeinen 	par->fb_ready = false;
11733a6fb6c4SWei Hu 	par->need_docopy = true;
1174f7018c21STomi Valkeinen 	init_completion(&par->wait);
1175f7018c21STomi Valkeinen 	INIT_DELAYED_WORK(&par->dwork, hvfb_update_work);
1176f7018c21STomi Valkeinen 
1177d21987d7SWei Hu 	par->delayed_refresh = false;
1178d21987d7SWei Hu 	spin_lock_init(&par->delayed_refresh_lock);
1179d21987d7SWei Hu 	par->x1 = par->y1 = INT_MAX;
1180d21987d7SWei Hu 	par->x2 = par->y2 = 0;
1181d21987d7SWei Hu 
1182f7018c21STomi Valkeinen 	/* Connect to VSP */
1183f7018c21STomi Valkeinen 	hv_set_drvdata(hdev, info);
1184f7018c21STomi Valkeinen 	ret = synthvid_connect_vsp(hdev);
1185f7018c21STomi Valkeinen 	if (ret) {
1186f7018c21STomi Valkeinen 		pr_err("Unable to connect to VSP\n");
1187f7018c21STomi Valkeinen 		goto error1;
1188f7018c21STomi Valkeinen 	}
1189f7018c21STomi Valkeinen 
119067e7cdb4SWei Hu 	hvfb_get_option(info);
119167e7cdb4SWei Hu 	pr_info("Screen resolution: %dx%d, Color depth: %d\n",
119267e7cdb4SWei Hu 		screen_width, screen_height, screen_depth);
119367e7cdb4SWei Hu 
119435464483SJake Oshins 	ret = hvfb_getmem(hdev, info);
1195f7018c21STomi Valkeinen 	if (ret) {
1196f7018c21STomi Valkeinen 		pr_err("No memory for framebuffer\n");
1197f7018c21STomi Valkeinen 		goto error2;
1198f7018c21STomi Valkeinen 	}
1199f7018c21STomi Valkeinen 
1200f7018c21STomi Valkeinen 	/* Set up fb_info */
1201f7018c21STomi Valkeinen 	info->flags = FBINFO_DEFAULT;
1202f7018c21STomi Valkeinen 
1203f7018c21STomi Valkeinen 	info->var.xres_virtual = info->var.xres = screen_width;
1204f7018c21STomi Valkeinen 	info->var.yres_virtual = info->var.yres = screen_height;
1205f7018c21STomi Valkeinen 	info->var.bits_per_pixel = screen_depth;
1206f7018c21STomi Valkeinen 
1207f7018c21STomi Valkeinen 	if (info->var.bits_per_pixel == 16) {
1208f7018c21STomi Valkeinen 		info->var.red = (struct fb_bitfield){11, 5, 0};
1209f7018c21STomi Valkeinen 		info->var.green = (struct fb_bitfield){5, 6, 0};
1210f7018c21STomi Valkeinen 		info->var.blue = (struct fb_bitfield){0, 5, 0};
1211f7018c21STomi Valkeinen 		info->var.transp = (struct fb_bitfield){0, 0, 0};
1212f7018c21STomi Valkeinen 	} else {
1213f7018c21STomi Valkeinen 		info->var.red = (struct fb_bitfield){16, 8, 0};
1214f7018c21STomi Valkeinen 		info->var.green = (struct fb_bitfield){8, 8, 0};
1215f7018c21STomi Valkeinen 		info->var.blue = (struct fb_bitfield){0, 8, 0};
1216f7018c21STomi Valkeinen 		info->var.transp = (struct fb_bitfield){24, 8, 0};
1217f7018c21STomi Valkeinen 	}
1218f7018c21STomi Valkeinen 
1219f7018c21STomi Valkeinen 	info->var.activate = FB_ACTIVATE_NOW;
1220f7018c21STomi Valkeinen 	info->var.height = -1;
1221f7018c21STomi Valkeinen 	info->var.width = -1;
1222f7018c21STomi Valkeinen 	info->var.vmode = FB_VMODE_NONINTERLACED;
1223f7018c21STomi Valkeinen 
1224f7018c21STomi Valkeinen 	strcpy(info->fix.id, KBUILD_MODNAME);
1225f7018c21STomi Valkeinen 	info->fix.type = FB_TYPE_PACKED_PIXELS;
1226f7018c21STomi Valkeinen 	info->fix.visual = FB_VISUAL_TRUECOLOR;
1227f7018c21STomi Valkeinen 	info->fix.line_length = screen_width * screen_depth / 8;
1228f7018c21STomi Valkeinen 	info->fix.accel = FB_ACCEL_NONE;
1229f7018c21STomi Valkeinen 
1230f7018c21STomi Valkeinen 	info->fbops = &hvfb_ops;
1231f7018c21STomi Valkeinen 	info->pseudo_palette = par->pseudo_palette;
1232f7018c21STomi Valkeinen 
1233d21987d7SWei Hu 	/* Initialize deferred IO */
1234d21987d7SWei Hu 	info->fbdefio = &synthvid_defio;
1235d21987d7SWei Hu 	fb_deferred_io_init(info);
1236d21987d7SWei Hu 
1237f7018c21STomi Valkeinen 	/* Send config to host */
1238f7018c21STomi Valkeinen 	ret = synthvid_send_config(hdev);
1239f7018c21STomi Valkeinen 	if (ret)
1240f7018c21STomi Valkeinen 		goto error;
1241f7018c21STomi Valkeinen 
1242f7018c21STomi Valkeinen 	ret = register_framebuffer(info);
1243f7018c21STomi Valkeinen 	if (ret) {
1244f7018c21STomi Valkeinen 		pr_err("Unable to register framebuffer\n");
1245f7018c21STomi Valkeinen 		goto error;
1246f7018c21STomi Valkeinen 	}
1247f7018c21STomi Valkeinen 
1248f7018c21STomi Valkeinen 	par->fb_ready = true;
1249f7018c21STomi Valkeinen 
12503686fe96SDexuan Cui 	par->synchronous_fb = false;
12513686fe96SDexuan Cui 	par->hvfb_panic_nb.notifier_call = hvfb_on_panic;
12523686fe96SDexuan Cui 	atomic_notifier_chain_register(&panic_notifier_list,
12533686fe96SDexuan Cui 				       &par->hvfb_panic_nb);
12543686fe96SDexuan Cui 
1255f7018c21STomi Valkeinen 	return 0;
1256f7018c21STomi Valkeinen 
1257f7018c21STomi Valkeinen error:
1258d21987d7SWei Hu 	fb_deferred_io_cleanup(info);
12593a6fb6c4SWei Hu 	hvfb_putmem(hdev, info);
1260f7018c21STomi Valkeinen error2:
1261f7018c21STomi Valkeinen 	vmbus_close(hdev->channel);
1262f7018c21STomi Valkeinen error1:
1263f7018c21STomi Valkeinen 	cancel_delayed_work_sync(&par->dwork);
1264f7018c21STomi Valkeinen 	hv_set_drvdata(hdev, NULL);
1265f7018c21STomi Valkeinen 	framebuffer_release(info);
1266f7018c21STomi Valkeinen 	return ret;
1267f7018c21STomi Valkeinen }
1268f7018c21STomi Valkeinen 
1269f7018c21STomi Valkeinen 
1270f7018c21STomi Valkeinen static int hvfb_remove(struct hv_device *hdev)
1271f7018c21STomi Valkeinen {
1272f7018c21STomi Valkeinen 	struct fb_info *info = hv_get_drvdata(hdev);
1273f7018c21STomi Valkeinen 	struct hvfb_par *par = info->par;
1274f7018c21STomi Valkeinen 
12753686fe96SDexuan Cui 	atomic_notifier_chain_unregister(&panic_notifier_list,
12763686fe96SDexuan Cui 					 &par->hvfb_panic_nb);
12773686fe96SDexuan Cui 
1278f7018c21STomi Valkeinen 	par->update = false;
1279f7018c21STomi Valkeinen 	par->fb_ready = false;
1280f7018c21STomi Valkeinen 
1281d21987d7SWei Hu 	fb_deferred_io_cleanup(info);
1282d21987d7SWei Hu 
1283f7018c21STomi Valkeinen 	unregister_framebuffer(info);
1284f7018c21STomi Valkeinen 	cancel_delayed_work_sync(&par->dwork);
1285f7018c21STomi Valkeinen 
1286f7018c21STomi Valkeinen 	vmbus_close(hdev->channel);
1287f7018c21STomi Valkeinen 	hv_set_drvdata(hdev, NULL);
1288f7018c21STomi Valkeinen 
12893a6fb6c4SWei Hu 	hvfb_putmem(hdev, info);
1290f7018c21STomi Valkeinen 	framebuffer_release(info);
1291f7018c21STomi Valkeinen 
1292f7018c21STomi Valkeinen 	return 0;
1293f7018c21STomi Valkeinen }
1294f7018c21STomi Valkeinen 
12951ecf3020SDexuan Cui static int hvfb_suspend(struct hv_device *hdev)
12961ecf3020SDexuan Cui {
12971ecf3020SDexuan Cui 	struct fb_info *info = hv_get_drvdata(hdev);
12981ecf3020SDexuan Cui 	struct hvfb_par *par = info->par;
12991ecf3020SDexuan Cui 
13001ecf3020SDexuan Cui 	console_lock();
13011ecf3020SDexuan Cui 
13021ecf3020SDexuan Cui 	/* 1 means do suspend */
13031ecf3020SDexuan Cui 	fb_set_suspend(info, 1);
13041ecf3020SDexuan Cui 
13051ecf3020SDexuan Cui 	cancel_delayed_work_sync(&par->dwork);
1306382a4622SDexuan Cui 	cancel_delayed_work_sync(&info->deferred_work);
13071ecf3020SDexuan Cui 
13081ecf3020SDexuan Cui 	par->update_saved = par->update;
13091ecf3020SDexuan Cui 	par->update = false;
13101ecf3020SDexuan Cui 	par->fb_ready = false;
13111ecf3020SDexuan Cui 
13121ecf3020SDexuan Cui 	vmbus_close(hdev->channel);
13131ecf3020SDexuan Cui 
13141ecf3020SDexuan Cui 	console_unlock();
13151ecf3020SDexuan Cui 
13161ecf3020SDexuan Cui 	return 0;
13171ecf3020SDexuan Cui }
13181ecf3020SDexuan Cui 
13191ecf3020SDexuan Cui static int hvfb_resume(struct hv_device *hdev)
13201ecf3020SDexuan Cui {
13211ecf3020SDexuan Cui 	struct fb_info *info = hv_get_drvdata(hdev);
13221ecf3020SDexuan Cui 	struct hvfb_par *par = info->par;
13231ecf3020SDexuan Cui 	int ret;
13241ecf3020SDexuan Cui 
13251ecf3020SDexuan Cui 	console_lock();
13261ecf3020SDexuan Cui 
13271ecf3020SDexuan Cui 	ret = synthvid_connect_vsp(hdev);
13281ecf3020SDexuan Cui 	if (ret != 0)
13291ecf3020SDexuan Cui 		goto out;
13301ecf3020SDexuan Cui 
13311ecf3020SDexuan Cui 	ret = synthvid_send_config(hdev);
13321ecf3020SDexuan Cui 	if (ret != 0) {
13331ecf3020SDexuan Cui 		vmbus_close(hdev->channel);
13341ecf3020SDexuan Cui 		goto out;
13351ecf3020SDexuan Cui 	}
13361ecf3020SDexuan Cui 
13371ecf3020SDexuan Cui 	par->fb_ready = true;
13381ecf3020SDexuan Cui 	par->update = par->update_saved;
13391ecf3020SDexuan Cui 
1340382a4622SDexuan Cui 	schedule_delayed_work(&info->deferred_work, info->fbdefio->delay);
13411ecf3020SDexuan Cui 	schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
13421ecf3020SDexuan Cui 
13431ecf3020SDexuan Cui 	/* 0 means do resume */
13441ecf3020SDexuan Cui 	fb_set_suspend(info, 0);
13451ecf3020SDexuan Cui 
13461ecf3020SDexuan Cui out:
13471ecf3020SDexuan Cui 	console_unlock();
13481ecf3020SDexuan Cui 
13491ecf3020SDexuan Cui 	return ret;
13501ecf3020SDexuan Cui }
13511ecf3020SDexuan Cui 
1352f7018c21STomi Valkeinen 
13539baa3c34SBenoit Taine static const struct pci_device_id pci_stub_id_table[] = {
1354f7018c21STomi Valkeinen 	{
1355f7018c21STomi Valkeinen 		.vendor      = PCI_VENDOR_ID_MICROSOFT,
1356f7018c21STomi Valkeinen 		.device      = PCI_DEVICE_ID_HYPERV_VIDEO,
1357f7018c21STomi Valkeinen 	},
1358f7018c21STomi Valkeinen 	{ /* end of list */ }
1359f7018c21STomi Valkeinen };
1360f7018c21STomi Valkeinen 
1361f7018c21STomi Valkeinen static const struct hv_vmbus_device_id id_table[] = {
1362f7018c21STomi Valkeinen 	/* Synthetic Video Device GUID */
1363f7018c21STomi Valkeinen 	{HV_SYNTHVID_GUID},
1364f7018c21STomi Valkeinen 	{}
1365f7018c21STomi Valkeinen };
1366f7018c21STomi Valkeinen 
1367f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(pci, pci_stub_id_table);
1368f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(vmbus, id_table);
1369f7018c21STomi Valkeinen 
1370f7018c21STomi Valkeinen static struct hv_driver hvfb_drv = {
1371f7018c21STomi Valkeinen 	.name = KBUILD_MODNAME,
1372f7018c21STomi Valkeinen 	.id_table = id_table,
1373f7018c21STomi Valkeinen 	.probe = hvfb_probe,
1374f7018c21STomi Valkeinen 	.remove = hvfb_remove,
13751ecf3020SDexuan Cui 	.suspend = hvfb_suspend,
13761ecf3020SDexuan Cui 	.resume = hvfb_resume,
1377af0a5646SArjan van de Ven 	.driver = {
1378af0a5646SArjan van de Ven 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1379af0a5646SArjan van de Ven 	},
1380f7018c21STomi Valkeinen };
1381f7018c21STomi Valkeinen 
1382f7018c21STomi Valkeinen static int hvfb_pci_stub_probe(struct pci_dev *pdev,
1383f7018c21STomi Valkeinen 			       const struct pci_device_id *ent)
1384f7018c21STomi Valkeinen {
1385f7018c21STomi Valkeinen 	return 0;
1386f7018c21STomi Valkeinen }
1387f7018c21STomi Valkeinen 
1388f7018c21STomi Valkeinen static void hvfb_pci_stub_remove(struct pci_dev *pdev)
1389f7018c21STomi Valkeinen {
1390f7018c21STomi Valkeinen }
1391f7018c21STomi Valkeinen 
1392f7018c21STomi Valkeinen static struct pci_driver hvfb_pci_stub_driver = {
1393f7018c21STomi Valkeinen 	.name =		KBUILD_MODNAME,
1394f7018c21STomi Valkeinen 	.id_table =	pci_stub_id_table,
1395f7018c21STomi Valkeinen 	.probe =	hvfb_pci_stub_probe,
1396f7018c21STomi Valkeinen 	.remove =	hvfb_pci_stub_remove,
1397af0a5646SArjan van de Ven 	.driver = {
1398af0a5646SArjan van de Ven 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1399af0a5646SArjan van de Ven 	}
1400f7018c21STomi Valkeinen };
1401f7018c21STomi Valkeinen 
1402f7018c21STomi Valkeinen static int __init hvfb_drv_init(void)
1403f7018c21STomi Valkeinen {
1404f7018c21STomi Valkeinen 	int ret;
1405f7018c21STomi Valkeinen 
1406f7018c21STomi Valkeinen 	ret = vmbus_driver_register(&hvfb_drv);
1407f7018c21STomi Valkeinen 	if (ret != 0)
1408f7018c21STomi Valkeinen 		return ret;
1409f7018c21STomi Valkeinen 
1410f7018c21STomi Valkeinen 	ret = pci_register_driver(&hvfb_pci_stub_driver);
1411f7018c21STomi Valkeinen 	if (ret != 0) {
1412f7018c21STomi Valkeinen 		vmbus_driver_unregister(&hvfb_drv);
1413f7018c21STomi Valkeinen 		return ret;
1414f7018c21STomi Valkeinen 	}
1415f7018c21STomi Valkeinen 
1416f7018c21STomi Valkeinen 	return 0;
1417f7018c21STomi Valkeinen }
1418f7018c21STomi Valkeinen 
1419f7018c21STomi Valkeinen static void __exit hvfb_drv_exit(void)
1420f7018c21STomi Valkeinen {
1421f7018c21STomi Valkeinen 	pci_unregister_driver(&hvfb_pci_stub_driver);
1422f7018c21STomi Valkeinen 	vmbus_driver_unregister(&hvfb_drv);
1423f7018c21STomi Valkeinen }
1424f7018c21STomi Valkeinen 
1425f7018c21STomi Valkeinen module_init(hvfb_drv_init);
1426f7018c21STomi Valkeinen module_exit(hvfb_drv_exit);
1427f7018c21STomi Valkeinen 
1428f7018c21STomi Valkeinen MODULE_LICENSE("GPL");
1429f7018c21STomi Valkeinen MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver");
1430