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