1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2021 Microsoft
5 * Portions of this code is derived from hyperv_fb.c
8 #include <linux/hyperv.h>
10 #include <drm/drm_print.h>
11 #include <drm/drm_simple_kms_helper.h>
13 #include "hyperv_drm.h"
15 #define VMBUS_RING_BUFSIZE (256 * 1024)
16 #define VMBUS_VSP_TIMEOUT (10 * HZ)
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)
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)
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)
38 enum synthvid_msg_type {
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,
50 SYNTHVID_RESOLUTION_REQUEST = 13,
51 SYNTHVID_RESOLUTION_RESPONSE = 14,
58 u32 size; /* size of message after this field */
61 struct hvd_screen_info {
66 struct synthvid_msg_hdr {
68 u32 size; /* size of this header + payload after this field */
71 struct synthvid_version_req {
75 struct synthvid_version_resp {
81 struct synthvid_vram_location {
83 u8 is_vram_gpa_specified;
87 struct synthvid_vram_location_ack {
91 struct video_output_situation {
100 struct synthvid_situation_update {
102 u8 video_output_count;
103 struct video_output_situation video_output[1];
106 struct synthvid_situation_update_ack {
110 struct synthvid_pointer_position {
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)
124 struct synthvid_pointer_shape {
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 */
134 struct synthvid_feature_change {
136 u8 is_ptr_pos_needed;
137 u8 is_ptr_shape_needed;
142 s32 x1, y1; /* top left corner */
143 s32 x2, y2; /* bottom right corner, exclusive */
146 struct synthvid_dirt {
152 #define SYNTHVID_EDID_BLOCK_SIZE 128
153 #define SYNTHVID_MAX_RESOLUTION_COUNT 64
155 struct synthvid_supported_resolution_req {
156 u8 maximum_resolution_count;
159 struct synthvid_supported_resolution_resp {
160 u8 edid_block[SYNTHVID_EDID_BLOCK_SIZE];
162 u8 default_resolution_index;
164 struct hvd_screen_info supported_resolution[SYNTHVID_MAX_RESOLUTION_COUNT];
167 struct synthvid_msg {
168 struct pipe_msg_hdr pipe_hdr;
169 struct synthvid_msg_hdr vid_hdr;
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;
186 static inline bool hyperv_version_ge(u32 ver1, u32 ver2)
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)))
196 static inline int hyperv_sendpacket(struct hv_device *hdev, struct synthvid_msg *msg)
198 static atomic64_t request_id = ATOMIC64_INIT(0);
199 struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
202 msg->pipe_hdr.type = PIPE_MSG_DATA;
203 msg->pipe_hdr.size = msg->vid_hdr.size;
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);
211 drm_err_ratelimited(&hv->dev, "Unable to send packet via vmbus; error %d\n", ret);
216 static int hyperv_negotiate_version(struct hv_device *hdev, u32 ver)
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;
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);
230 t = wait_for_completion_timeout(&hv->wait, VMBUS_VSP_TIMEOUT);
232 drm_err(dev, "Time out on waiting version response\n");
236 if (!msg->ver_resp.is_accepted) {
237 drm_err(dev, "Version request not accepted\n");
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));
248 int hyperv_update_vram_location(struct hv_device *hdev, phys_addr_t vram_pp)
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;
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);
264 t = wait_for_completion_timeout(&hv->wait, VMBUS_VSP_TIMEOUT);
266 drm_err(dev, "Time out on waiting vram location ack\n");
269 if (msg->vram_ack.user_ctx != vram_pp) {
270 drm_err(dev, "Unable to set VRAM location\n");
277 int hyperv_update_situation(struct hv_device *hdev, u8 active, u32 bpp,
278 u32 w, u32 h, u32 pitch)
280 struct synthvid_msg msg;
282 memset(&msg, 0, sizeof(struct synthvid_msg));
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;
297 hyperv_sendpacket(hdev, &msg);
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.
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
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.
318 int hyperv_hide_hw_ptr(struct hv_device *hdev)
320 struct synthvid_msg msg;
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);
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);
351 int hyperv_update_dirt(struct hv_device *hdev, struct drm_rect *rect)
353 struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
354 struct synthvid_msg msg;
356 if (!hv->dirt_needed)
359 memset(&msg, 0, sizeof(struct synthvid_msg));
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;
371 hyperv_sendpacket(hdev, &msg);
376 static int hyperv_get_supported_resolution(struct hv_device *hdev)
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;
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);
393 t = wait_for_completion_timeout(&hv->wait, VMBUS_VSP_TIMEOUT);
395 drm_err(dev, "Time out on waiting resolution response\n");
399 if (msg->resolution_resp.resolution_count == 0) {
400 drm_err(dev, "No supported resolutions\n");
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);
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);
417 hv->preferred_width =
418 msg->resolution_resp.supported_resolution[index].width;
419 hv->preferred_height =
420 msg->resolution_resp.supported_resolution[index].height;
425 static void hyperv_receive_sub(struct hv_device *hdev)
427 struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
428 struct synthvid_msg *msg;
433 msg = (struct synthvid_msg *)hv->recv_buf;
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);
444 if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) {
445 hv->dirt_needed = msg->feature_chg.is_dirt_needed;
447 hyperv_hide_hw_ptr(hv->hdev);
451 static void hyperv_receive(void *ctx)
453 struct hv_device *hdev = ctx;
454 struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
455 struct synthvid_msg *recv_buf;
463 recv_buf = (struct synthvid_msg *)hv->recv_buf;
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);
475 int hyperv_connect_vsp(struct hv_device *hdev)
477 struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
478 struct drm_device *dev = &hv->dev;
481 ret = vmbus_open(hdev->channel, VMBUS_RING_BUFSIZE, VMBUS_RING_BUFSIZE,
482 NULL, 0, hyperv_receive, hdev);
484 drm_err(dev, "Unable to open vmbus channel\n");
488 /* Negotiate the protocol version with host */
489 switch (vmbus_proto_version) {
491 case VERSION_WIN10_V5:
492 ret = hyperv_negotiate_version(hdev, SYNTHVID_VERSION_WIN10);
498 ret = hyperv_negotiate_version(hdev, SYNTHVID_VERSION_WIN8);
501 ret = hyperv_negotiate_version(hdev, SYNTHVID_VERSION_WIN10);
506 drm_err(dev, "Synthetic video device version not accepted %d\n", ret);
510 hv->screen_depth = SYNTHVID_DEPTH_WIN8;
512 if (hyperv_version_ge(hv->synthvid_version, SYNTHVID_VERSION_WIN10)) {
513 ret = hyperv_get_supported_resolution(hdev);
515 drm_err(dev, "Failed to get supported resolution from host, use default\n");
517 hv->screen_width_max = SYNTHVID_WIDTH_WIN8;
518 hv->screen_height_max = SYNTHVID_HEIGHT_WIN8;
521 hv->mmio_megabytes = hdev->channel->offermsg.offer.mmio_megabytes;
526 vmbus_close(hdev->channel);