hyperv_drm_proto.c 14 KB

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