• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2021 Microsoft
4  *
5  * Portions of this code is derived from hyperv_fb.c
6  */
7 
8 #include <linux/hyperv.h>
9 
10 #include <drm/drm_print.h>
11 #include <drm/drm_simple_kms_helper.h>
12 
13 #include "hyperv_drm.h"
14 
15 #define VMBUS_RING_BUFSIZE (256 * 1024)
16 #define VMBUS_VSP_TIMEOUT (10 * HZ)
17 
18 #define SYNTHVID_VERSION(major, minor) ((minor) << 16 | (major))
19 #define SYNTHVID_VER_GET_MAJOR(ver) (ver & 0x0000ffff)
20 #define SYNTHVID_VER_GET_MINOR(ver) ((ver & 0xffff0000) >> 16)
21 
22 /* Support for VERSION_WIN7 is removed. #define is retained for reference. */
23 #define SYNTHVID_VERSION_WIN7 SYNTHVID_VERSION(3, 0)
24 #define SYNTHVID_VERSION_WIN8 SYNTHVID_VERSION(3, 2)
25 #define SYNTHVID_VERSION_WIN10 SYNTHVID_VERSION(3, 5)
26 
27 #define SYNTHVID_DEPTH_WIN8 32
28 #define SYNTHVID_WIDTH_WIN8 1600
29 #define SYNTHVID_HEIGHT_WIN8 1200
30 #define SYNTHVID_FB_SIZE_WIN8 (8 * 1024 * 1024)
31 
32 enum pipe_msg_type {
33 	PIPE_MSG_INVALID,
34 	PIPE_MSG_DATA,
35 	PIPE_MSG_MAX
36 };
37 
38 enum synthvid_msg_type {
39 	SYNTHVID_ERROR			= 0,
40 	SYNTHVID_VERSION_REQUEST	= 1,
41 	SYNTHVID_VERSION_RESPONSE	= 2,
42 	SYNTHVID_VRAM_LOCATION		= 3,
43 	SYNTHVID_VRAM_LOCATION_ACK	= 4,
44 	SYNTHVID_SITUATION_UPDATE	= 5,
45 	SYNTHVID_SITUATION_UPDATE_ACK	= 6,
46 	SYNTHVID_POINTER_POSITION	= 7,
47 	SYNTHVID_POINTER_SHAPE		= 8,
48 	SYNTHVID_FEATURE_CHANGE		= 9,
49 	SYNTHVID_DIRT			= 10,
50 	SYNTHVID_RESOLUTION_REQUEST	= 13,
51 	SYNTHVID_RESOLUTION_RESPONSE	= 14,
52 
53 	SYNTHVID_MAX			= 15
54 };
55 
56 struct pipe_msg_hdr {
57 	u32 type;
58 	u32 size; /* size of message after this field */
59 } __packed;
60 
61 struct hvd_screen_info {
62 	u16 width;
63 	u16 height;
64 } __packed;
65 
66 struct synthvid_msg_hdr {
67 	u32 type;
68 	u32 size;  /* size of this header + payload after this field */
69 } __packed;
70 
71 struct synthvid_version_req {
72 	u32 version;
73 } __packed;
74 
75 struct synthvid_version_resp {
76 	u32 version;
77 	u8 is_accepted;
78 	u8 max_video_outputs;
79 } __packed;
80 
81 struct synthvid_vram_location {
82 	u64 user_ctx;
83 	u8 is_vram_gpa_specified;
84 	u64 vram_gpa;
85 } __packed;
86 
87 struct synthvid_vram_location_ack {
88 	u64 user_ctx;
89 } __packed;
90 
91 struct video_output_situation {
92 	u8 active;
93 	u32 vram_offset;
94 	u8 depth_bits;
95 	u32 width_pixels;
96 	u32 height_pixels;
97 	u32 pitch_bytes;
98 } __packed;
99 
100 struct synthvid_situation_update {
101 	u64 user_ctx;
102 	u8 video_output_count;
103 	struct video_output_situation video_output[1];
104 } __packed;
105 
106 struct synthvid_situation_update_ack {
107 	u64 user_ctx;
108 } __packed;
109 
110 struct synthvid_pointer_position {
111 	u8 is_visible;
112 	u8 video_output;
113 	s32 image_x;
114 	s32 image_y;
115 } __packed;
116 
117 #define SYNTHVID_CURSOR_MAX_X 96
118 #define SYNTHVID_CURSOR_MAX_Y 96
119 #define SYNTHVID_CURSOR_ARGB_PIXEL_SIZE 4
120 #define SYNTHVID_CURSOR_MAX_SIZE (SYNTHVID_CURSOR_MAX_X * \
121 	SYNTHVID_CURSOR_MAX_Y * SYNTHVID_CURSOR_ARGB_PIXEL_SIZE)
122 #define SYNTHVID_CURSOR_COMPLETE (-1)
123 
124 struct synthvid_pointer_shape {
125 	u8 part_idx;
126 	u8 is_argb;
127 	u32 width; /* SYNTHVID_CURSOR_MAX_X at most */
128 	u32 height; /* SYNTHVID_CURSOR_MAX_Y at most */
129 	u32 hot_x; /* hotspot relative to upper-left of pointer image */
130 	u32 hot_y;
131 	u8 data[4];
132 } __packed;
133 
134 struct synthvid_feature_change {
135 	u8 is_dirt_needed;
136 	u8 is_ptr_pos_needed;
137 	u8 is_ptr_shape_needed;
138 	u8 is_situ_needed;
139 } __packed;
140 
141 struct rect {
142 	s32 x1, y1; /* top left corner */
143 	s32 x2, y2; /* bottom right corner, exclusive */
144 } __packed;
145 
146 struct synthvid_dirt {
147 	u8 video_output;
148 	u8 dirt_count;
149 	struct rect rect[1];
150 } __packed;
151 
152 #define SYNTHVID_EDID_BLOCK_SIZE	128
153 #define	SYNTHVID_MAX_RESOLUTION_COUNT	64
154 
155 struct synthvid_supported_resolution_req {
156 	u8 maximum_resolution_count;
157 } __packed;
158 
159 struct synthvid_supported_resolution_resp {
160 	u8 edid_block[SYNTHVID_EDID_BLOCK_SIZE];
161 	u8 resolution_count;
162 	u8 default_resolution_index;
163 	u8 is_standard;
164 	struct hvd_screen_info supported_resolution[SYNTHVID_MAX_RESOLUTION_COUNT];
165 } __packed;
166 
167 struct synthvid_msg {
168 	struct pipe_msg_hdr pipe_hdr;
169 	struct synthvid_msg_hdr vid_hdr;
170 	union {
171 		struct synthvid_version_req ver_req;
172 		struct synthvid_version_resp ver_resp;
173 		struct synthvid_vram_location vram;
174 		struct synthvid_vram_location_ack vram_ack;
175 		struct synthvid_situation_update situ;
176 		struct synthvid_situation_update_ack situ_ack;
177 		struct synthvid_pointer_position ptr_pos;
178 		struct synthvid_pointer_shape ptr_shape;
179 		struct synthvid_feature_change feature_chg;
180 		struct synthvid_dirt dirt;
181 		struct synthvid_supported_resolution_req resolution_req;
182 		struct synthvid_supported_resolution_resp resolution_resp;
183 	};
184 } __packed;
185 
hyperv_version_ge(u32 ver1,u32 ver2)186 static inline bool hyperv_version_ge(u32 ver1, u32 ver2)
187 {
188 	if (SYNTHVID_VER_GET_MAJOR(ver1) > SYNTHVID_VER_GET_MAJOR(ver2) ||
189 	    (SYNTHVID_VER_GET_MAJOR(ver1) == SYNTHVID_VER_GET_MAJOR(ver2) &&
190 	     SYNTHVID_VER_GET_MINOR(ver1) >= SYNTHVID_VER_GET_MINOR(ver2)))
191 		return true;
192 
193 	return false;
194 }
195 
hyperv_sendpacket(struct hv_device * hdev,struct synthvid_msg * msg)196 static inline int hyperv_sendpacket(struct hv_device *hdev, struct synthvid_msg *msg)
197 {
198 	static atomic64_t request_id = ATOMIC64_INIT(0);
199 	struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
200 	int ret;
201 
202 	msg->pipe_hdr.type = PIPE_MSG_DATA;
203 	msg->pipe_hdr.size = msg->vid_hdr.size;
204 
205 	ret = vmbus_sendpacket(hdev->channel, msg,
206 			       msg->vid_hdr.size + sizeof(struct pipe_msg_hdr),
207 			       atomic64_inc_return(&request_id),
208 			       VM_PKT_DATA_INBAND, 0);
209 
210 	if (ret)
211 		drm_err_ratelimited(&hv->dev, "Unable to send packet via vmbus; error %d\n", ret);
212 
213 	return ret;
214 }
215 
hyperv_negotiate_version(struct hv_device * hdev,u32 ver)216 static int hyperv_negotiate_version(struct hv_device *hdev, u32 ver)
217 {
218 	struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
219 	struct synthvid_msg *msg = (struct synthvid_msg *)hv->init_buf;
220 	struct drm_device *dev = &hv->dev;
221 	unsigned long t;
222 
223 	memset(msg, 0, sizeof(struct synthvid_msg));
224 	msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST;
225 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
226 		sizeof(struct synthvid_version_req);
227 	msg->ver_req.version = ver;
228 	hyperv_sendpacket(hdev, msg);
229 
230 	t = wait_for_completion_timeout(&hv->wait, VMBUS_VSP_TIMEOUT);
231 	if (!t) {
232 		drm_err(dev, "Time out on waiting version response\n");
233 		return -ETIMEDOUT;
234 	}
235 
236 	if (!msg->ver_resp.is_accepted) {
237 		drm_err(dev, "Version request not accepted\n");
238 		return -ENODEV;
239 	}
240 
241 	hv->synthvid_version = ver;
242 	drm_info(dev, "Synthvid Version major %d, minor %d\n",
243 		 SYNTHVID_VER_GET_MAJOR(ver), SYNTHVID_VER_GET_MINOR(ver));
244 
245 	return 0;
246 }
247 
hyperv_update_vram_location(struct hv_device * hdev,phys_addr_t vram_pp)248 int hyperv_update_vram_location(struct hv_device *hdev, phys_addr_t vram_pp)
249 {
250 	struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
251 	struct synthvid_msg *msg = (struct synthvid_msg *)hv->init_buf;
252 	struct drm_device *dev = &hv->dev;
253 	unsigned long t;
254 
255 	memset(msg, 0, sizeof(struct synthvid_msg));
256 	msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION;
257 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
258 		sizeof(struct synthvid_vram_location);
259 	msg->vram.user_ctx = vram_pp;
260 	msg->vram.vram_gpa = vram_pp;
261 	msg->vram.is_vram_gpa_specified = 1;
262 	hyperv_sendpacket(hdev, msg);
263 
264 	t = wait_for_completion_timeout(&hv->wait, VMBUS_VSP_TIMEOUT);
265 	if (!t) {
266 		drm_err(dev, "Time out on waiting vram location ack\n");
267 		return -ETIMEDOUT;
268 	}
269 	if (msg->vram_ack.user_ctx != vram_pp) {
270 		drm_err(dev, "Unable to set VRAM location\n");
271 		return -ENODEV;
272 	}
273 
274 	return 0;
275 }
276 
hyperv_update_situation(struct hv_device * hdev,u8 active,u32 bpp,u32 w,u32 h,u32 pitch)277 int hyperv_update_situation(struct hv_device *hdev, u8 active, u32 bpp,
278 			    u32 w, u32 h, u32 pitch)
279 {
280 	struct synthvid_msg msg;
281 
282 	memset(&msg, 0, sizeof(struct synthvid_msg));
283 
284 	msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE;
285 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
286 		sizeof(struct synthvid_situation_update);
287 	msg.situ.user_ctx = 0;
288 	msg.situ.video_output_count = 1;
289 	msg.situ.video_output[0].active = active;
290 	/* vram_offset should always be 0 */
291 	msg.situ.video_output[0].vram_offset = 0;
292 	msg.situ.video_output[0].depth_bits = bpp;
293 	msg.situ.video_output[0].width_pixels = w;
294 	msg.situ.video_output[0].height_pixels = h;
295 	msg.situ.video_output[0].pitch_bytes = pitch;
296 
297 	hyperv_sendpacket(hdev, &msg);
298 
299 	return 0;
300 }
301 
302 /*
303  * Hyper-V supports a hardware cursor feature. It's not used by Linux VM,
304  * but the Hyper-V host still draws a point as an extra mouse pointer,
305  * which is unwanted, especially when Xorg is running.
306  *
307  * The hyperv_fb driver uses synthvid_send_ptr() to hide the unwanted
308  * pointer, by setting msg.ptr_pos.is_visible = 1 and setting the
309  * msg.ptr_shape.data. Note: setting msg.ptr_pos.is_visible to 0 doesn't
310  * work in tests.
311  *
312  * Copy synthvid_send_ptr() to hyperv_drm and rename it to
313  * hyperv_hide_hw_ptr(). Note: hyperv_hide_hw_ptr() is also called in the
314  * handler of the SYNTHVID_FEATURE_CHANGE event, otherwise the host still
315  * draws an extra unwanted mouse pointer after the VM Connection window is
316  * closed and reopened.
317  */
hyperv_hide_hw_ptr(struct hv_device * hdev)318 int hyperv_hide_hw_ptr(struct hv_device *hdev)
319 {
320 	struct synthvid_msg msg;
321 
322 	memset(&msg, 0, sizeof(struct synthvid_msg));
323 	msg.vid_hdr.type = SYNTHVID_POINTER_POSITION;
324 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
325 		sizeof(struct synthvid_pointer_position);
326 	msg.ptr_pos.is_visible = 1;
327 	msg.ptr_pos.video_output = 0;
328 	msg.ptr_pos.image_x = 0;
329 	msg.ptr_pos.image_y = 0;
330 	hyperv_sendpacket(hdev, &msg);
331 
332 	memset(&msg, 0, sizeof(struct synthvid_msg));
333 	msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE;
334 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
335 		sizeof(struct synthvid_pointer_shape);
336 	msg.ptr_shape.part_idx = SYNTHVID_CURSOR_COMPLETE;
337 	msg.ptr_shape.is_argb = 1;
338 	msg.ptr_shape.width = 1;
339 	msg.ptr_shape.height = 1;
340 	msg.ptr_shape.hot_x = 0;
341 	msg.ptr_shape.hot_y = 0;
342 	msg.ptr_shape.data[0] = 0;
343 	msg.ptr_shape.data[1] = 1;
344 	msg.ptr_shape.data[2] = 1;
345 	msg.ptr_shape.data[3] = 1;
346 	hyperv_sendpacket(hdev, &msg);
347 
348 	return 0;
349 }
350 
hyperv_update_dirt(struct hv_device * hdev,struct drm_rect * rect)351 int hyperv_update_dirt(struct hv_device *hdev, struct drm_rect *rect)
352 {
353 	struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
354 	struct synthvid_msg msg;
355 
356 	if (!hv->dirt_needed)
357 		return 0;
358 
359 	memset(&msg, 0, sizeof(struct synthvid_msg));
360 
361 	msg.vid_hdr.type = SYNTHVID_DIRT;
362 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
363 		sizeof(struct synthvid_dirt);
364 	msg.dirt.video_output = 0;
365 	msg.dirt.dirt_count = 1;
366 	msg.dirt.rect[0].x1 = rect->x1;
367 	msg.dirt.rect[0].y1 = rect->y1;
368 	msg.dirt.rect[0].x2 = rect->x2;
369 	msg.dirt.rect[0].y2 = rect->y2;
370 
371 	hyperv_sendpacket(hdev, &msg);
372 
373 	return 0;
374 }
375 
hyperv_get_supported_resolution(struct hv_device * hdev)376 static int hyperv_get_supported_resolution(struct hv_device *hdev)
377 {
378 	struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
379 	struct synthvid_msg *msg = (struct synthvid_msg *)hv->init_buf;
380 	struct drm_device *dev = &hv->dev;
381 	unsigned long t;
382 	u8 index;
383 	int i;
384 
385 	memset(msg, 0, sizeof(struct synthvid_msg));
386 	msg->vid_hdr.type = SYNTHVID_RESOLUTION_REQUEST;
387 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
388 		sizeof(struct synthvid_supported_resolution_req);
389 	msg->resolution_req.maximum_resolution_count =
390 		SYNTHVID_MAX_RESOLUTION_COUNT;
391 	hyperv_sendpacket(hdev, msg);
392 
393 	t = wait_for_completion_timeout(&hv->wait, VMBUS_VSP_TIMEOUT);
394 	if (!t) {
395 		drm_err(dev, "Time out on waiting resolution response\n");
396 		return -ETIMEDOUT;
397 	}
398 
399 	if (msg->resolution_resp.resolution_count == 0) {
400 		drm_err(dev, "No supported resolutions\n");
401 		return -ENODEV;
402 	}
403 
404 	index = msg->resolution_resp.default_resolution_index;
405 	if (index >= msg->resolution_resp.resolution_count) {
406 		drm_err(dev, "Invalid resolution index: %d\n", index);
407 		return -ENODEV;
408 	}
409 
410 	for (i = 0; i < msg->resolution_resp.resolution_count; i++) {
411 		hv->screen_width_max = max_t(u32, hv->screen_width_max,
412 			msg->resolution_resp.supported_resolution[i].width);
413 		hv->screen_height_max = max_t(u32, hv->screen_height_max,
414 			msg->resolution_resp.supported_resolution[i].height);
415 	}
416 
417 	hv->preferred_width =
418 		msg->resolution_resp.supported_resolution[index].width;
419 	hv->preferred_height =
420 		msg->resolution_resp.supported_resolution[index].height;
421 
422 	return 0;
423 }
424 
hyperv_receive_sub(struct hv_device * hdev)425 static void hyperv_receive_sub(struct hv_device *hdev)
426 {
427 	struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
428 	struct synthvid_msg *msg;
429 
430 	if (!hv)
431 		return;
432 
433 	msg = (struct synthvid_msg *)hv->recv_buf;
434 
435 	/* Complete the wait event */
436 	if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE ||
437 	    msg->vid_hdr.type == SYNTHVID_RESOLUTION_RESPONSE ||
438 	    msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) {
439 		memcpy(hv->init_buf, msg, VMBUS_MAX_PACKET_SIZE);
440 		complete(&hv->wait);
441 		return;
442 	}
443 
444 	if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) {
445 		hv->dirt_needed = msg->feature_chg.is_dirt_needed;
446 		if (hv->dirt_needed)
447 			hyperv_hide_hw_ptr(hv->hdev);
448 	}
449 }
450 
hyperv_receive(void * ctx)451 static void hyperv_receive(void *ctx)
452 {
453 	struct hv_device *hdev = ctx;
454 	struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
455 	struct synthvid_msg *recv_buf;
456 	u32 bytes_recvd;
457 	u64 req_id;
458 	int ret;
459 
460 	if (!hv)
461 		return;
462 
463 	recv_buf = (struct synthvid_msg *)hv->recv_buf;
464 
465 	do {
466 		ret = vmbus_recvpacket(hdev->channel, recv_buf,
467 				       VMBUS_MAX_PACKET_SIZE,
468 				       &bytes_recvd, &req_id);
469 		if (bytes_recvd > 0 &&
470 		    recv_buf->pipe_hdr.type == PIPE_MSG_DATA)
471 			hyperv_receive_sub(hdev);
472 	} while (bytes_recvd > 0 && ret == 0);
473 }
474 
hyperv_connect_vsp(struct hv_device * hdev)475 int hyperv_connect_vsp(struct hv_device *hdev)
476 {
477 	struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
478 	struct drm_device *dev = &hv->dev;
479 	int ret;
480 
481 	ret = vmbus_open(hdev->channel, VMBUS_RING_BUFSIZE, VMBUS_RING_BUFSIZE,
482 			 NULL, 0, hyperv_receive, hdev);
483 	if (ret) {
484 		drm_err(dev, "Unable to open vmbus channel\n");
485 		return ret;
486 	}
487 
488 	/* Negotiate the protocol version with host */
489 	switch (vmbus_proto_version) {
490 	case VERSION_WIN10:
491 	case VERSION_WIN10_V5:
492 		ret = hyperv_negotiate_version(hdev, SYNTHVID_VERSION_WIN10);
493 		if (!ret)
494 			break;
495 		fallthrough;
496 	case VERSION_WIN8:
497 	case VERSION_WIN8_1:
498 		ret = hyperv_negotiate_version(hdev, SYNTHVID_VERSION_WIN8);
499 		break;
500 	default:
501 		ret = hyperv_negotiate_version(hdev, SYNTHVID_VERSION_WIN10);
502 		break;
503 	}
504 
505 	if (ret) {
506 		drm_err(dev, "Synthetic video device version not accepted %d\n", ret);
507 		goto error;
508 	}
509 
510 	hv->screen_depth = SYNTHVID_DEPTH_WIN8;
511 
512 	if (hyperv_version_ge(hv->synthvid_version, SYNTHVID_VERSION_WIN10)) {
513 		ret = hyperv_get_supported_resolution(hdev);
514 		if (ret)
515 			drm_err(dev, "Failed to get supported resolution from host, use default\n");
516 	} else {
517 		hv->screen_width_max = SYNTHVID_WIDTH_WIN8;
518 		hv->screen_height_max = SYNTHVID_HEIGHT_WIN8;
519 	}
520 
521 	hv->mmio_megabytes = hdev->channel->offermsg.offer.mmio_megabytes;
522 
523 	return 0;
524 
525 error:
526 	vmbus_close(hdev->channel);
527 	return ret;
528 }
529