vmwgfx_msg.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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_send_msg: Sends a message to the host
  114. *
  115. * @channel: RPC channel
  116. * @logmsg: NULL terminated string
  117. *
  118. * Returns: 0 on success
  119. */
  120. static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
  121. {
  122. unsigned long eax, ebx, ecx, edx, si, di, bp;
  123. size_t msg_len = strlen(msg);
  124. int retries = 0;
  125. while (retries < RETRIES) {
  126. retries++;
  127. /* Set up additional parameters */
  128. si = channel->cookie_high;
  129. di = channel->cookie_low;
  130. VMW_PORT(VMW_PORT_CMD_SENDSIZE,
  131. msg_len, si, di,
  132. VMW_HYPERVISOR_PORT | (channel->channel_id << 16),
  133. VMW_HYPERVISOR_MAGIC,
  134. eax, ebx, ecx, edx, si, di);
  135. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0 ||
  136. (HIGH_WORD(ecx) & MESSAGE_STATUS_HB) == 0) {
  137. /* Expected success + high-bandwidth. Give up. */
  138. return -EINVAL;
  139. }
  140. /* Send msg */
  141. si = (uintptr_t) msg;
  142. di = channel->cookie_low;
  143. bp = channel->cookie_high;
  144. VMW_PORT_HB_OUT(
  145. (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
  146. msg_len, si, di,
  147. VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
  148. VMW_HYPERVISOR_MAGIC, bp,
  149. eax, ebx, ecx, edx, si, di);
  150. if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) != 0) {
  151. return 0;
  152. } else if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
  153. /* A checkpoint occurred. Retry. */
  154. continue;
  155. } else {
  156. break;
  157. }
  158. }
  159. return -EINVAL;
  160. }
  161. STACK_FRAME_NON_STANDARD(vmw_send_msg);
  162. /**
  163. * vmw_recv_msg: Receives a message from the host
  164. *
  165. * Note: It is the caller's responsibility to call kfree() on msg.
  166. *
  167. * @channel: channel opened by vmw_open_channel
  168. * @msg: [OUT] message received from the host
  169. * @msg_len: message length
  170. */
  171. static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
  172. size_t *msg_len)
  173. {
  174. unsigned long eax, ebx, ecx, edx, si, di, bp;
  175. char *reply;
  176. size_t reply_len;
  177. int retries = 0;
  178. *msg_len = 0;
  179. *msg = NULL;
  180. while (retries < RETRIES) {
  181. retries++;
  182. /* Set up additional parameters */
  183. si = channel->cookie_high;
  184. di = channel->cookie_low;
  185. VMW_PORT(VMW_PORT_CMD_RECVSIZE,
  186. 0, si, di,
  187. (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
  188. VMW_HYPERVISOR_MAGIC,
  189. eax, ebx, ecx, edx, si, di);
  190. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0 ||
  191. (HIGH_WORD(ecx) & MESSAGE_STATUS_HB) == 0) {
  192. DRM_ERROR("Failed to get reply size for host message.\n");
  193. return -EINVAL;
  194. }
  195. /* No reply available. This is okay. */
  196. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_DORECV) == 0)
  197. return 0;
  198. reply_len = ebx;
  199. reply = kzalloc(reply_len + 1, GFP_KERNEL);
  200. if (!reply) {
  201. DRM_ERROR("Cannot allocate memory for host message reply.\n");
  202. return -ENOMEM;
  203. }
  204. /* Receive buffer */
  205. si = channel->cookie_high;
  206. di = (uintptr_t) reply;
  207. bp = channel->cookie_low;
  208. VMW_PORT_HB_IN(
  209. (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
  210. reply_len, si, di,
  211. VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
  212. VMW_HYPERVISOR_MAGIC, bp,
  213. eax, ebx, ecx, edx, si, di);
  214. if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) == 0) {
  215. kfree(reply);
  216. if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
  217. /* A checkpoint occurred. Retry. */
  218. continue;
  219. }
  220. return -EINVAL;
  221. }
  222. reply[reply_len] = '\0';
  223. /* Ack buffer */
  224. si = channel->cookie_high;
  225. di = channel->cookie_low;
  226. VMW_PORT(VMW_PORT_CMD_RECVSTATUS,
  227. MESSAGE_STATUS_SUCCESS, si, di,
  228. (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
  229. VMW_HYPERVISOR_MAGIC,
  230. eax, ebx, ecx, edx, si, di);
  231. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
  232. kfree(reply);
  233. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) {
  234. /* A checkpoint occurred. Retry. */
  235. continue;
  236. }
  237. return -EINVAL;
  238. }
  239. break;
  240. }
  241. if (retries == RETRIES)
  242. return -EINVAL;
  243. *msg_len = reply_len;
  244. *msg = reply;
  245. return 0;
  246. }
  247. STACK_FRAME_NON_STANDARD(vmw_recv_msg);
  248. /**
  249. * vmw_host_get_guestinfo: Gets a GuestInfo parameter
  250. *
  251. * Gets the value of a GuestInfo.* parameter. The value returned will be in
  252. * a string, and it is up to the caller to post-process.
  253. *
  254. * @guest_info_param: Parameter to get, e.g. GuestInfo.svga.gl3
  255. * @buffer: if NULL, *reply_len will contain reply size.
  256. * @length: size of the reply_buf. Set to size of reply upon return
  257. *
  258. * Returns: 0 on success
  259. */
  260. int vmw_host_get_guestinfo(const char *guest_info_param,
  261. char *buffer, size_t *length)
  262. {
  263. struct rpc_channel channel;
  264. char *msg, *reply = NULL;
  265. size_t reply_len = 0;
  266. if (!vmw_msg_enabled)
  267. return -ENODEV;
  268. if (!guest_info_param || !length)
  269. return -EINVAL;
  270. msg = kasprintf(GFP_KERNEL, "info-get %s", guest_info_param);
  271. if (!msg) {
  272. DRM_ERROR("Cannot allocate memory to get guest info \"%s\".",
  273. guest_info_param);
  274. return -ENOMEM;
  275. }
  276. if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
  277. goto out_open;
  278. if (vmw_send_msg(&channel, msg) ||
  279. vmw_recv_msg(&channel, (void *) &reply, &reply_len))
  280. goto out_msg;
  281. vmw_close_channel(&channel);
  282. if (buffer && reply && reply_len > 0) {
  283. /* Remove reply code, which are the first 2 characters of
  284. * the reply
  285. */
  286. reply_len = max(reply_len - 2, (size_t) 0);
  287. reply_len = min(reply_len, *length);
  288. if (reply_len > 0)
  289. memcpy(buffer, reply + 2, reply_len);
  290. }
  291. *length = reply_len;
  292. kfree(reply);
  293. kfree(msg);
  294. return 0;
  295. out_msg:
  296. vmw_close_channel(&channel);
  297. kfree(reply);
  298. out_open:
  299. *length = 0;
  300. kfree(msg);
  301. DRM_ERROR("Failed to get guest info \"%s\".", guest_info_param);
  302. return -EINVAL;
  303. }
  304. /**
  305. * vmw_host_log: Sends a log message to the host
  306. *
  307. * @log: NULL terminated string
  308. *
  309. * Returns: 0 on success
  310. */
  311. int vmw_host_log(const char *log)
  312. {
  313. struct rpc_channel channel;
  314. char *msg;
  315. int ret = 0;
  316. if (!vmw_msg_enabled)
  317. return -ENODEV;
  318. if (!log)
  319. return ret;
  320. msg = kasprintf(GFP_KERNEL, "log %s", log);
  321. if (!msg) {
  322. DRM_ERROR("Cannot allocate memory for host log message.\n");
  323. return -ENOMEM;
  324. }
  325. if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
  326. goto out_open;
  327. if (vmw_send_msg(&channel, msg))
  328. goto out_msg;
  329. vmw_close_channel(&channel);
  330. kfree(msg);
  331. return 0;
  332. out_msg:
  333. vmw_close_channel(&channel);
  334. out_open:
  335. kfree(msg);
  336. DRM_ERROR("Failed to send host log message.\n");
  337. return -EINVAL;
  338. }