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 #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_WIN7 16
26 #define SYNTHVID_DEPTH_WIN8 32
27 #define SYNTHVID_FB_SIZE_WIN7 (4 * 1024 * 1024)
28 #define SYNTHVID_FB_SIZE_WIN8 (8 * 1024 * 1024)
29 #define SYNTHVID_WIDTH_MAX_WIN7 1600
30 #define SYNTHVID_HEIGHT_MAX_WIN7 1200
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 
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 
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(&hv->dev, "Unable to send packet via vmbus\n");
212 
213 	return ret;
214 }
215 
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 
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 
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 int hyperv_update_dirt(struct hv_device *hdev, struct drm_rect *rect)
303 {
304 	struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
305 	struct synthvid_msg msg;
306 
307 	if (!hv->dirt_needed)
308 		return 0;
309 
310 	memset(&msg, 0, sizeof(struct synthvid_msg));
311 
312 	msg.vid_hdr.type = SYNTHVID_DIRT;
313 	msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
314 		sizeof(struct synthvid_dirt);
315 	msg.dirt.video_output = 0;
316 	msg.dirt.dirt_count = 1;
317 	msg.dirt.rect[0].x1 = rect->x1;
318 	msg.dirt.rect[0].y1 = rect->y1;
319 	msg.dirt.rect[0].x2 = rect->x2;
320 	msg.dirt.rect[0].y2 = rect->y2;
321 
322 	hyperv_sendpacket(hdev, &msg);
323 
324 	return 0;
325 }
326 
327 static int hyperv_get_supported_resolution(struct hv_device *hdev)
328 {
329 	struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
330 	struct synthvid_msg *msg = (struct synthvid_msg *)hv->init_buf;
331 	struct drm_device *dev = &hv->dev;
332 	unsigned long t;
333 	u8 index;
334 	int i;
335 
336 	memset(msg, 0, sizeof(struct synthvid_msg));
337 	msg->vid_hdr.type = SYNTHVID_RESOLUTION_REQUEST;
338 	msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
339 		sizeof(struct synthvid_supported_resolution_req);
340 	msg->resolution_req.maximum_resolution_count =
341 		SYNTHVID_MAX_RESOLUTION_COUNT;
342 	hyperv_sendpacket(hdev, msg);
343 
344 	t = wait_for_completion_timeout(&hv->wait, VMBUS_VSP_TIMEOUT);
345 	if (!t) {
346 		drm_err(dev, "Time out on waiting resolution response\n");
347 		return -ETIMEDOUT;
348 	}
349 
350 	if (msg->resolution_resp.resolution_count == 0) {
351 		drm_err(dev, "No supported resolutions\n");
352 		return -ENODEV;
353 	}
354 
355 	index = msg->resolution_resp.default_resolution_index;
356 	if (index >= msg->resolution_resp.resolution_count) {
357 		drm_err(dev, "Invalid resolution index: %d\n", index);
358 		return -ENODEV;
359 	}
360 
361 	for (i = 0; i < msg->resolution_resp.resolution_count; i++) {
362 		hv->screen_width_max = max_t(u32, hv->screen_width_max,
363 			msg->resolution_resp.supported_resolution[i].width);
364 		hv->screen_height_max = max_t(u32, hv->screen_height_max,
365 			msg->resolution_resp.supported_resolution[i].height);
366 	}
367 
368 	hv->preferred_width =
369 		msg->resolution_resp.supported_resolution[index].width;
370 	hv->preferred_height =
371 		msg->resolution_resp.supported_resolution[index].height;
372 
373 	return 0;
374 }
375 
376 static void hyperv_receive_sub(struct hv_device *hdev)
377 {
378 	struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
379 	struct synthvid_msg *msg;
380 
381 	if (!hv)
382 		return;
383 
384 	msg = (struct synthvid_msg *)hv->recv_buf;
385 
386 	/* Complete the wait event */
387 	if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE ||
388 	    msg->vid_hdr.type == SYNTHVID_RESOLUTION_RESPONSE ||
389 	    msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) {
390 		memcpy(hv->init_buf, msg, VMBUS_MAX_PACKET_SIZE);
391 		complete(&hv->wait);
392 		return;
393 	}
394 
395 	if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE)
396 		hv->dirt_needed = msg->feature_chg.is_dirt_needed;
397 }
398 
399 static void hyperv_receive(void *ctx)
400 {
401 	struct hv_device *hdev = ctx;
402 	struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
403 	struct synthvid_msg *recv_buf;
404 	u32 bytes_recvd;
405 	u64 req_id;
406 	int ret;
407 
408 	if (!hv)
409 		return;
410 
411 	recv_buf = (struct synthvid_msg *)hv->recv_buf;
412 
413 	do {
414 		ret = vmbus_recvpacket(hdev->channel, recv_buf,
415 				       VMBUS_MAX_PACKET_SIZE,
416 				       &bytes_recvd, &req_id);
417 		if (bytes_recvd > 0 &&
418 		    recv_buf->pipe_hdr.type == PIPE_MSG_DATA)
419 			hyperv_receive_sub(hdev);
420 	} while (bytes_recvd > 0 && ret == 0);
421 }
422 
423 int hyperv_connect_vsp(struct hv_device *hdev)
424 {
425 	struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
426 	struct drm_device *dev = &hv->dev;
427 	int ret;
428 
429 	ret = vmbus_open(hdev->channel, VMBUS_RING_BUFSIZE, VMBUS_RING_BUFSIZE,
430 			 NULL, 0, hyperv_receive, hdev);
431 	if (ret) {
432 		drm_err(dev, "Unable to open vmbus channel\n");
433 		return ret;
434 	}
435 
436 	/* Negotiate the protocol version with host */
437 	switch (vmbus_proto_version) {
438 	case VERSION_WIN10:
439 	case VERSION_WIN10_V5:
440 		ret = hyperv_negotiate_version(hdev, SYNTHVID_VERSION_WIN10);
441 		if (!ret)
442 			break;
443 		fallthrough;
444 	case VERSION_WIN8:
445 	case VERSION_WIN8_1:
446 		ret = hyperv_negotiate_version(hdev, SYNTHVID_VERSION_WIN8);
447 		if (!ret)
448 			break;
449 		fallthrough;
450 	case VERSION_WS2008:
451 	case VERSION_WIN7:
452 		ret = hyperv_negotiate_version(hdev, SYNTHVID_VERSION_WIN7);
453 		break;
454 	default:
455 		ret = hyperv_negotiate_version(hdev, SYNTHVID_VERSION_WIN10);
456 		break;
457 	}
458 
459 	if (ret) {
460 		drm_err(dev, "Synthetic video device version not accepted %d\n", ret);
461 		goto error;
462 	}
463 
464 	if (hv->synthvid_version == SYNTHVID_VERSION_WIN7)
465 		hv->screen_depth = SYNTHVID_DEPTH_WIN7;
466 	else
467 		hv->screen_depth = SYNTHVID_DEPTH_WIN8;
468 
469 	if (hyperv_version_ge(hv->synthvid_version, SYNTHVID_VERSION_WIN10)) {
470 		ret = hyperv_get_supported_resolution(hdev);
471 		if (ret)
472 			drm_err(dev, "Failed to get supported resolution from host, use default\n");
473 	} else {
474 		hv->screen_width_max = SYNTHVID_WIDTH_MAX_WIN7;
475 		hv->screen_height_max = SYNTHVID_HEIGHT_MAX_WIN7;
476 	}
477 
478 	hv->mmio_megabytes = hdev->channel->offermsg.offer.mmio_megabytes;
479 
480 	return 0;
481 
482 error:
483 	vmbus_close(hdev->channel);
484 	return ret;
485 }
486