vmwgfx_msg.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright 2016 VMware, Inc., Palo Alto, CA., USA
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial portions
  15. * of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  20. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  21. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  23. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/frame.h>
  30. #include <asm/hypervisor.h>
  31. #include <drm/drmP.h>
  32. #include "vmwgfx_drv.h"
  33. #include "vmwgfx_msg.h"
  34. #define MESSAGE_STATUS_SUCCESS 0x0001
  35. #define MESSAGE_STATUS_DORECV 0x0002
  36. #define MESSAGE_STATUS_CPT 0x0010
  37. #define MESSAGE_STATUS_HB 0x0080
  38. #define RPCI_PROTOCOL_NUM 0x49435052
  39. #define GUESTMSG_FLAG_COOKIE 0x80000000
  40. #define RETRIES 3
  41. #define VMW_HYPERVISOR_MAGIC 0x564D5868
  42. #define VMW_HYPERVISOR_PORT 0x5658
  43. #define VMW_HYPERVISOR_HB_PORT 0x5659
  44. #define VMW_PORT_CMD_MSG 30
  45. #define VMW_PORT_CMD_HB_MSG 0
  46. #define VMW_PORT_CMD_OPEN_CHANNEL (MSG_TYPE_OPEN << 16 | VMW_PORT_CMD_MSG)
  47. #define VMW_PORT_CMD_CLOSE_CHANNEL (MSG_TYPE_CLOSE << 16 | VMW_PORT_CMD_MSG)
  48. #define VMW_PORT_CMD_SENDSIZE (MSG_TYPE_SENDSIZE << 16 | VMW_PORT_CMD_MSG)
  49. #define VMW_PORT_CMD_RECVSIZE (MSG_TYPE_RECVSIZE << 16 | VMW_PORT_CMD_MSG)
  50. #define VMW_PORT_CMD_RECVSTATUS (MSG_TYPE_RECVSTATUS << 16 | VMW_PORT_CMD_MSG)
  51. #define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)
  52. static u32 vmw_msg_enabled = 1;
  53. enum rpc_msg_type {
  54. MSG_TYPE_OPEN,
  55. MSG_TYPE_SENDSIZE,
  56. MSG_TYPE_SENDPAYLOAD,
  57. MSG_TYPE_RECVSIZE,
  58. MSG_TYPE_RECVPAYLOAD,
  59. MSG_TYPE_RECVSTATUS,
  60. MSG_TYPE_CLOSE,
  61. };
  62. struct rpc_channel {
  63. u16 channel_id;
  64. u32 cookie_high;
  65. u32 cookie_low;
  66. };
  67. /**
  68. * vmw_open_channel
  69. *
  70. * @channel: RPC channel
  71. * @protocol:
  72. *
  73. * Returns: 0 on success
  74. */
  75. static int vmw_open_channel(struct rpc_channel *channel, unsigned int protocol)
  76. {
  77. unsigned long eax, ebx, ecx, edx, si = 0, di = 0;
  78. VMW_PORT(VMW_PORT_CMD_OPEN_CHANNEL,
  79. (protocol | GUESTMSG_FLAG_COOKIE), si, di,
  80. VMW_HYPERVISOR_PORT,
  81. VMW_HYPERVISOR_MAGIC,
  82. eax, ebx, ecx, edx, si, di);
  83. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
  84. return -EINVAL;
  85. channel->channel_id = HIGH_WORD(edx);
  86. channel->cookie_high = si;
  87. channel->cookie_low = di;
  88. return 0;
  89. }
  90. /**
  91. * vmw_close_channel
  92. *
  93. * @channel: RPC channel
  94. *
  95. * Returns: 0 on success
  96. */
  97. static int vmw_close_channel(struct rpc_channel *channel)
  98. {
  99. unsigned long eax, ebx, ecx, edx, si, di;
  100. /* Set up additional parameters */
  101. si = channel->cookie_high;
  102. di = channel->cookie_low;
  103. VMW_PORT(VMW_PORT_CMD_CLOSE_CHANNEL,
  104. 0, si, di,
  105. (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
  106. VMW_HYPERVISOR_MAGIC,
  107. eax, ebx, ecx, edx, si, di);
  108. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
  109. return -EINVAL;
  110. return 0;
  111. }
  112. /**
  113. * vmw_port_hb_out - Send the message payload either through the
  114. * high-bandwidth port if available, or through the backdoor otherwise.
  115. * @channel: The rpc channel.
  116. * @msg: NULL-terminated message.
  117. * @hb: Whether the high-bandwidth port is available.
  118. *
  119. * Return: The port status.
  120. */
  121. static unsigned long vmw_port_hb_out(struct rpc_channel *channel,
  122. const char *msg, bool hb)
  123. {
  124. unsigned long si, di, eax, ebx, ecx, edx;
  125. unsigned long msg_len = strlen(msg);
  126. if (hb) {
  127. unsigned long bp = channel->cookie_high;
  128. si = (uintptr_t) msg;
  129. di = channel->cookie_low;
  130. VMW_PORT_HB_OUT(
  131. (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
  132. msg_len, si, di,
  133. VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
  134. VMW_HYPERVISOR_MAGIC, bp,
  135. eax, ebx, ecx, edx, si, di);
  136. return ebx;
  137. }
  138. /* HB port not available. Send the message 4 bytes at a time. */
  139. ecx = MESSAGE_STATUS_SUCCESS << 16;
  140. while (msg_len && (HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS)) {
  141. unsigned int bytes = min_t(size_t, msg_len, 4);
  142. unsigned long word = 0;
  143. memcpy(&word, msg, bytes);
  144. msg_len -= bytes;
  145. msg += bytes;
  146. si = channel->cookie_high;
  147. di = channel->cookie_low;
  148. VMW_PORT(VMW_PORT_CMD_MSG | (MSG_TYPE_SENDPAYLOAD << 16),
  149. word, si, di,
  150. VMW_HYPERVISOR_PORT | (channel->channel_id << 16),
  151. VMW_HYPERVISOR_MAGIC,
  152. eax, ebx, ecx, edx, si, di);
  153. }
  154. return ecx;
  155. }
  156. /**
  157. * vmw_port_hb_in - Receive the message payload either through the
  158. * high-bandwidth port if available, or through the backdoor otherwise.
  159. * @channel: The rpc channel.
  160. * @reply: Pointer to buffer holding reply.
  161. * @reply_len: Length of the reply.
  162. * @hb: Whether the high-bandwidth port is available.
  163. *
  164. * Return: The port status.
  165. */
  166. static unsigned long vmw_port_hb_in(struct rpc_channel *channel, char *reply,
  167. unsigned long reply_len, bool hb)
  168. {
  169. unsigned long si, di, eax, ebx, ecx, edx;
  170. if (hb) {
  171. unsigned long bp = channel->cookie_low;
  172. si = channel->cookie_high;
  173. di = (uintptr_t) reply;
  174. VMW_PORT_HB_IN(
  175. (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
  176. reply_len, si, di,
  177. VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
  178. VMW_HYPERVISOR_MAGIC, bp,
  179. eax, ebx, ecx, edx, si, di);
  180. return ebx;
  181. }
  182. /* HB port not available. Retrieve the message 4 bytes at a time. */
  183. ecx = MESSAGE_STATUS_SUCCESS << 16;
  184. while (reply_len) {
  185. unsigned int bytes = min_t(unsigned long, reply_len, 4);
  186. si = channel->cookie_high;
  187. di = channel->cookie_low;
  188. VMW_PORT(VMW_PORT_CMD_MSG | (MSG_TYPE_RECVPAYLOAD << 16),
  189. MESSAGE_STATUS_SUCCESS, si, di,
  190. VMW_HYPERVISOR_PORT | (channel->channel_id << 16),
  191. VMW_HYPERVISOR_MAGIC,
  192. eax, ebx, ecx, edx, si, di);
  193. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
  194. break;
  195. memcpy(reply, &ebx, bytes);
  196. reply_len -= bytes;
  197. reply += bytes;
  198. }
  199. return ecx;
  200. }
  201. /**
  202. * vmw_send_msg: Sends a message to the host
  203. *
  204. * @channel: RPC channel
  205. * @logmsg: NULL terminated string
  206. *
  207. * Returns: 0 on success
  208. */
  209. static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
  210. {
  211. unsigned long eax, ebx, ecx, edx, si, di;
  212. size_t msg_len = strlen(msg);
  213. int retries = 0;
  214. while (retries < RETRIES) {
  215. retries++;
  216. /* Set up additional parameters */
  217. si = channel->cookie_high;
  218. di = channel->cookie_low;
  219. VMW_PORT(VMW_PORT_CMD_SENDSIZE,
  220. msg_len, si, di,
  221. VMW_HYPERVISOR_PORT | (channel->channel_id << 16),
  222. VMW_HYPERVISOR_MAGIC,
  223. eax, ebx, ecx, edx, si, di);
  224. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
  225. /* Expected success. Give up. */
  226. return -EINVAL;
  227. }
  228. /* Send msg */
  229. ebx = vmw_port_hb_out(channel, msg,
  230. !!(HIGH_WORD(ecx) & MESSAGE_STATUS_HB));
  231. if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) != 0) {
  232. return 0;
  233. } else if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
  234. /* A checkpoint occurred. Retry. */
  235. continue;
  236. } else {
  237. break;
  238. }
  239. }
  240. return -EINVAL;
  241. }
  242. STACK_FRAME_NON_STANDARD(vmw_send_msg);
  243. /**
  244. * vmw_recv_msg: Receives a message from the host
  245. *
  246. * Note: It is the caller's responsibility to call kfree() on msg.
  247. *
  248. * @channel: channel opened by vmw_open_channel
  249. * @msg: [OUT] message received from the host
  250. * @msg_len: message length
  251. */
  252. static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
  253. size_t *msg_len)
  254. {
  255. unsigned long eax, ebx, ecx, edx, si, di;
  256. char *reply;
  257. size_t reply_len;
  258. int retries = 0;
  259. *msg_len = 0;
  260. *msg = NULL;
  261. while (retries < RETRIES) {
  262. retries++;
  263. /* Set up additional parameters */
  264. si = channel->cookie_high;
  265. di = channel->cookie_low;
  266. VMW_PORT(VMW_PORT_CMD_RECVSIZE,
  267. 0, si, di,
  268. (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
  269. VMW_HYPERVISOR_MAGIC,
  270. eax, ebx, ecx, edx, si, di);
  271. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
  272. DRM_ERROR("Failed to get reply size for host message.\n");
  273. return -EINVAL;
  274. }
  275. /* No reply available. This is okay. */
  276. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_DORECV) == 0)
  277. return 0;
  278. reply_len = ebx;
  279. reply = kzalloc(reply_len + 1, GFP_KERNEL);
  280. if (!reply) {
  281. DRM_ERROR("Cannot allocate memory for host message reply.\n");
  282. return -ENOMEM;
  283. }
  284. /* Receive buffer */
  285. ebx = vmw_port_hb_in(channel, reply, reply_len,
  286. !!(HIGH_WORD(ecx) & MESSAGE_STATUS_HB));
  287. if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) == 0) {
  288. kfree(reply);
  289. reply = NULL;
  290. if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
  291. /* A checkpoint occurred. Retry. */
  292. continue;
  293. }
  294. return -EINVAL;
  295. }
  296. reply[reply_len] = '\0';
  297. /* Ack buffer */
  298. si = channel->cookie_high;
  299. di = channel->cookie_low;
  300. VMW_PORT(VMW_PORT_CMD_RECVSTATUS,
  301. MESSAGE_STATUS_SUCCESS, si, di,
  302. (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
  303. VMW_HYPERVISOR_MAGIC,
  304. eax, ebx, ecx, edx, si, di);
  305. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
  306. kfree(reply);
  307. reply = NULL;
  308. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) {
  309. /* A checkpoint occurred. Retry. */
  310. continue;
  311. }
  312. return -EINVAL;
  313. }
  314. break;
  315. }
  316. if (!reply)
  317. return -EINVAL;
  318. *msg_len = reply_len;
  319. *msg = reply;
  320. return 0;
  321. }
  322. STACK_FRAME_NON_STANDARD(vmw_recv_msg);
  323. /**
  324. * vmw_host_get_guestinfo: Gets a GuestInfo parameter
  325. *
  326. * Gets the value of a GuestInfo.* parameter. The value returned will be in
  327. * a string, and it is up to the caller to post-process.
  328. *
  329. * @guest_info_param: Parameter to get, e.g. GuestInfo.svga.gl3
  330. * @buffer: if NULL, *reply_len will contain reply size.
  331. * @length: size of the reply_buf. Set to size of reply upon return
  332. *
  333. * Returns: 0 on success
  334. */
  335. int vmw_host_get_guestinfo(const char *guest_info_param,
  336. char *buffer, size_t *length)
  337. {
  338. struct rpc_channel channel;
  339. char *msg, *reply = NULL;
  340. size_t reply_len = 0;
  341. if (!vmw_msg_enabled)
  342. return -ENODEV;
  343. if (!guest_info_param || !length)
  344. return -EINVAL;
  345. msg = kasprintf(GFP_KERNEL, "info-get %s", guest_info_param);
  346. if (!msg) {
  347. DRM_ERROR("Cannot allocate memory to get guest info \"%s\".",
  348. guest_info_param);
  349. return -ENOMEM;
  350. }
  351. if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
  352. goto out_open;
  353. if (vmw_send_msg(&channel, msg) ||
  354. vmw_recv_msg(&channel, (void *) &reply, &reply_len))
  355. goto out_msg;
  356. vmw_close_channel(&channel);
  357. if (buffer && reply && reply_len > 0) {
  358. /* Remove reply code, which are the first 2 characters of
  359. * the reply
  360. */
  361. reply_len = max(reply_len - 2, (size_t) 0);
  362. reply_len = min(reply_len, *length);
  363. if (reply_len > 0)
  364. memcpy(buffer, reply + 2, reply_len);
  365. }
  366. *length = reply_len;
  367. kfree(reply);
  368. kfree(msg);
  369. return 0;
  370. out_msg:
  371. vmw_close_channel(&channel);
  372. kfree(reply);
  373. out_open:
  374. *length = 0;
  375. kfree(msg);
  376. DRM_ERROR("Failed to get guest info \"%s\".", guest_info_param);
  377. return -EINVAL;
  378. }
  379. /**
  380. * vmw_host_log: Sends a log message to the host
  381. *
  382. * @log: NULL terminated string
  383. *
  384. * Returns: 0 on success
  385. */
  386. int vmw_host_log(const char *log)
  387. {
  388. struct rpc_channel channel;
  389. char *msg;
  390. int ret = 0;
  391. if (!vmw_msg_enabled)
  392. return -ENODEV;
  393. if (!log)
  394. return ret;
  395. msg = kasprintf(GFP_KERNEL, "log %s", log);
  396. if (!msg) {
  397. DRM_ERROR("Cannot allocate memory for host log message.\n");
  398. return -ENOMEM;
  399. }
  400. if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
  401. goto out_open;
  402. if (vmw_send_msg(&channel, msg))
  403. goto out_msg;
  404. vmw_close_channel(&channel);
  405. kfree(msg);
  406. return 0;
  407. out_msg:
  408. vmw_close_channel(&channel);
  409. out_open:
  410. kfree(msg);
  411. DRM_ERROR("Failed to send host log message.\n");
  412. return -EINVAL;
  413. }