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)
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)
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
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(&hv->dev, "Unable to send packet via vmbus\n");
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);
302 int hyperv_update_dirt(struct hv_device *hdev, struct drm_rect *rect)
304 struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
305 struct synthvid_msg msg;
307 if (!hv->dirt_needed)
310 memset(&msg, 0, sizeof(struct synthvid_msg));
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;
322 hyperv_sendpacket(hdev, &msg);
327 static int hyperv_get_supported_resolution(struct hv_device *hdev)
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;
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);
344 t = wait_for_completion_timeout(&hv->wait, VMBUS_VSP_TIMEOUT);
346 drm_err(dev, "Time out on waiting resolution response\n");
350 if (msg->resolution_resp.resolution_count == 0) {
351 drm_err(dev, "No supported resolutions\n");
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);
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);
368 hv->preferred_width =
369 msg->resolution_resp.supported_resolution[index].width;
370 hv->preferred_height =
371 msg->resolution_resp.supported_resolution[index].height;
376 static void hyperv_receive_sub(struct hv_device *hdev)
378 struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
379 struct synthvid_msg *msg;
384 msg = (struct synthvid_msg *)hv->recv_buf;
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);
395 if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE)
396 hv->dirt_needed = msg->feature_chg.is_dirt_needed;
399 static void hyperv_receive(void *ctx)
401 struct hv_device *hdev = ctx;
402 struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
403 struct synthvid_msg *recv_buf;
411 recv_buf = (struct synthvid_msg *)hv->recv_buf;
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);
423 int hyperv_connect_vsp(struct hv_device *hdev)
425 struct hyperv_drm_device *hv = hv_get_drvdata(hdev);
426 struct drm_device *dev = &hv->dev;
429 ret = vmbus_open(hdev->channel, VMBUS_RING_BUFSIZE, VMBUS_RING_BUFSIZE,
430 NULL, 0, hyperv_receive, hdev);
432 drm_err(dev, "Unable to open vmbus channel\n");
436 /* Negotiate the protocol version with host */
437 switch (vmbus_proto_version) {
439 case VERSION_WIN10_V5:
440 ret = hyperv_negotiate_version(hdev, SYNTHVID_VERSION_WIN10);
446 ret = hyperv_negotiate_version(hdev, SYNTHVID_VERSION_WIN8);
452 ret = hyperv_negotiate_version(hdev, SYNTHVID_VERSION_WIN7);
455 ret = hyperv_negotiate_version(hdev, SYNTHVID_VERSION_WIN10);
460 drm_err(dev, "Synthetic video device version not accepted %d\n", ret);
464 if (hv->synthvid_version == SYNTHVID_VERSION_WIN7)
465 hv->screen_depth = SYNTHVID_DEPTH_WIN7;
467 hv->screen_depth = SYNTHVID_DEPTH_WIN8;
469 if (hyperv_version_ge(hv->synthvid_version, SYNTHVID_VERSION_WIN10)) {
470 ret = hyperv_get_supported_resolution(hdev);
472 drm_err(dev, "Failed to get supported resolution from host, use default\n");
474 hv->screen_width_max = SYNTHVID_WIDTH_MAX_WIN7;
475 hv->screen_height_max = SYNTHVID_HEIGHT_MAX_WIN7;
478 hv->mmio_megabytes = hdev->channel->offermsg.offer.mmio_megabytes;
483 vmbus_close(hdev->channel);