vmwgfx_msg.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * Copyright © 2016 VMware, Inc., Palo Alto, CA., USA
  3. * All Rights Reserved.
  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_msg.h"
  33. #define MESSAGE_STATUS_SUCCESS 0x0001
  34. #define MESSAGE_STATUS_DORECV 0x0002
  35. #define MESSAGE_STATUS_CPT 0x0010
  36. #define MESSAGE_STATUS_HB 0x0080
  37. #define RPCI_PROTOCOL_NUM 0x49435052
  38. #define GUESTMSG_FLAG_COOKIE 0x80000000
  39. #define RETRIES 3
  40. #define VMW_HYPERVISOR_MAGIC 0x564D5868
  41. #define VMW_HYPERVISOR_PORT 0x5658
  42. #define VMW_HYPERVISOR_HB_PORT 0x5659
  43. #define VMW_PORT_CMD_MSG 30
  44. #define VMW_PORT_CMD_HB_MSG 0
  45. #define VMW_PORT_CMD_OPEN_CHANNEL (MSG_TYPE_OPEN << 16 | VMW_PORT_CMD_MSG)
  46. #define VMW_PORT_CMD_CLOSE_CHANNEL (MSG_TYPE_CLOSE << 16 | VMW_PORT_CMD_MSG)
  47. #define VMW_PORT_CMD_SENDSIZE (MSG_TYPE_SENDSIZE << 16 | VMW_PORT_CMD_MSG)
  48. #define VMW_PORT_CMD_RECVSIZE (MSG_TYPE_RECVSIZE << 16 | VMW_PORT_CMD_MSG)
  49. #define VMW_PORT_CMD_RECVSTATUS (MSG_TYPE_RECVSTATUS << 16 | VMW_PORT_CMD_MSG)
  50. #define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)
  51. static u32 vmw_msg_enabled = 1;
  52. enum rpc_msg_type {
  53. MSG_TYPE_OPEN,
  54. MSG_TYPE_SENDSIZE,
  55. MSG_TYPE_SENDPAYLOAD,
  56. MSG_TYPE_RECVSIZE,
  57. MSG_TYPE_RECVPAYLOAD,
  58. MSG_TYPE_RECVSTATUS,
  59. MSG_TYPE_CLOSE,
  60. };
  61. struct rpc_channel {
  62. u16 channel_id;
  63. u32 cookie_high;
  64. u32 cookie_low;
  65. };
  66. /**
  67. * vmw_open_channel
  68. *
  69. * @channel: RPC channel
  70. * @protocol:
  71. *
  72. * Returns: 0 on success
  73. */
  74. static int vmw_open_channel(struct rpc_channel *channel, unsigned int protocol)
  75. {
  76. unsigned long eax, ebx, ecx, edx, si = 0, di = 0;
  77. VMW_PORT(VMW_PORT_CMD_OPEN_CHANNEL,
  78. (protocol | GUESTMSG_FLAG_COOKIE), si, di,
  79. VMW_HYPERVISOR_PORT,
  80. VMW_HYPERVISOR_MAGIC,
  81. eax, ebx, ecx, edx, si, di);
  82. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
  83. return -EINVAL;
  84. channel->channel_id = HIGH_WORD(edx);
  85. channel->cookie_high = si;
  86. channel->cookie_low = di;
  87. return 0;
  88. }
  89. /**
  90. * vmw_close_channel
  91. *
  92. * @channel: RPC channel
  93. *
  94. * Returns: 0 on success
  95. */
  96. static int vmw_close_channel(struct rpc_channel *channel)
  97. {
  98. unsigned long eax, ebx, ecx, edx, si, di;
  99. /* Set up additional parameters */
  100. si = channel->cookie_high;
  101. di = channel->cookie_low;
  102. VMW_PORT(VMW_PORT_CMD_CLOSE_CHANNEL,
  103. 0, si, di,
  104. (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
  105. VMW_HYPERVISOR_MAGIC,
  106. eax, ebx, ecx, edx, si, di);
  107. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
  108. return -EINVAL;
  109. return 0;
  110. }
  111. /**
  112. * vmw_send_msg: Sends a message to the host
  113. *
  114. * @channel: RPC channel
  115. * @logmsg: NULL terminated string
  116. *
  117. * Returns: 0 on success
  118. */
  119. static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
  120. {
  121. unsigned long eax, ebx, ecx, edx, si, di, bp;
  122. size_t msg_len = strlen(msg);
  123. int retries = 0;
  124. while (retries < RETRIES) {
  125. retries++;
  126. /* Set up additional parameters */
  127. si = channel->cookie_high;
  128. di = channel->cookie_low;
  129. VMW_PORT(VMW_PORT_CMD_SENDSIZE,
  130. msg_len, si, di,
  131. VMW_HYPERVISOR_PORT | (channel->channel_id << 16),
  132. VMW_HYPERVISOR_MAGIC,
  133. eax, ebx, ecx, edx, si, di);
  134. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0 ||
  135. (HIGH_WORD(ecx) & MESSAGE_STATUS_HB) == 0) {
  136. /* Expected success + high-bandwidth. Give up. */
  137. return -EINVAL;
  138. }
  139. /* Send msg */
  140. si = (uintptr_t) msg;
  141. di = channel->cookie_low;
  142. bp = channel->cookie_high;
  143. VMW_PORT_HB_OUT(
  144. (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
  145. msg_len, si, di,
  146. VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
  147. VMW_HYPERVISOR_MAGIC, bp,
  148. eax, ebx, ecx, edx, si, di);
  149. if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) != 0) {
  150. return 0;
  151. } else if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
  152. /* A checkpoint occurred. Retry. */
  153. continue;
  154. } else {
  155. break;
  156. }
  157. }
  158. return -EINVAL;
  159. }
  160. STACK_FRAME_NON_STANDARD(vmw_send_msg);
  161. /**
  162. * vmw_recv_msg: Receives a message from the host
  163. *
  164. * Note: It is the caller's responsibility to call kfree() on msg.
  165. *
  166. * @channel: channel opened by vmw_open_channel
  167. * @msg: [OUT] message received from the host
  168. * @msg_len: message length
  169. */
  170. static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
  171. size_t *msg_len)
  172. {
  173. unsigned long eax, ebx, ecx, edx, si, di, bp;
  174. char *reply;
  175. size_t reply_len;
  176. int retries = 0;
  177. *msg_len = 0;
  178. *msg = NULL;
  179. while (retries < RETRIES) {
  180. retries++;
  181. /* Set up additional parameters */
  182. si = channel->cookie_high;
  183. di = channel->cookie_low;
  184. VMW_PORT(VMW_PORT_CMD_RECVSIZE,
  185. 0, si, di,
  186. (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
  187. VMW_HYPERVISOR_MAGIC,
  188. eax, ebx, ecx, edx, si, di);
  189. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0 ||
  190. (HIGH_WORD(ecx) & MESSAGE_STATUS_HB) == 0) {
  191. DRM_ERROR("Failed to get reply size\n");
  192. return -EINVAL;
  193. }
  194. /* No reply available. This is okay. */
  195. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_DORECV) == 0)
  196. return 0;
  197. reply_len = ebx;
  198. reply = kzalloc(reply_len + 1, GFP_KERNEL);
  199. if (!reply) {
  200. DRM_ERROR("Cannot allocate memory for reply\n");
  201. return -ENOMEM;
  202. }
  203. /* Receive buffer */
  204. si = channel->cookie_high;
  205. di = (uintptr_t) reply;
  206. bp = channel->cookie_low;
  207. VMW_PORT_HB_IN(
  208. (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
  209. reply_len, si, di,
  210. VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
  211. VMW_HYPERVISOR_MAGIC, bp,
  212. eax, ebx, ecx, edx, si, di);
  213. if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) == 0) {
  214. kfree(reply);
  215. reply = NULL;
  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. reply = NULL;
  234. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) {
  235. /* A checkpoint occurred. Retry. */
  236. continue;
  237. }
  238. return -EINVAL;
  239. }
  240. break;
  241. }
  242. if (!reply)
  243. return -EINVAL;
  244. *msg_len = reply_len;
  245. *msg = reply;
  246. return 0;
  247. }
  248. STACK_FRAME_NON_STANDARD(vmw_recv_msg);
  249. /**
  250. * vmw_host_get_guestinfo: Gets a GuestInfo parameter
  251. *
  252. * Gets the value of a GuestInfo.* parameter. The value returned will be in
  253. * a string, and it is up to the caller to post-process.
  254. *
  255. * @guest_info_param: Parameter to get, e.g. GuestInfo.svga.gl3
  256. * @buffer: if NULL, *reply_len will contain reply size.
  257. * @length: size of the reply_buf. Set to size of reply upon return
  258. *
  259. * Returns: 0 on success
  260. */
  261. int vmw_host_get_guestinfo(const char *guest_info_param,
  262. char *buffer, size_t *length)
  263. {
  264. struct rpc_channel channel;
  265. char *msg, *reply = NULL;
  266. size_t msg_len, reply_len = 0;
  267. int ret = 0;
  268. if (!vmw_msg_enabled)
  269. return -ENODEV;
  270. if (!guest_info_param || !length)
  271. return -EINVAL;
  272. msg_len = strlen(guest_info_param) + strlen("info-get ") + 1;
  273. msg = kzalloc(msg_len, GFP_KERNEL);
  274. if (!msg) {
  275. DRM_ERROR("Cannot allocate memory to get %s", guest_info_param);
  276. return -ENOMEM;
  277. }
  278. sprintf(msg, "info-get %s", guest_info_param);
  279. if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM) ||
  280. vmw_send_msg(&channel, msg) ||
  281. vmw_recv_msg(&channel, (void *) &reply, &reply_len) ||
  282. vmw_close_channel(&channel)) {
  283. DRM_ERROR("Failed to get %s", guest_info_param);
  284. ret = -EINVAL;
  285. }
  286. if (buffer && reply && reply_len > 0) {
  287. /* Remove reply code, which are the first 2 characters of
  288. * the reply
  289. */
  290. reply_len = max(reply_len - 2, (size_t) 0);
  291. reply_len = min(reply_len, *length);
  292. if (reply_len > 0)
  293. memcpy(buffer, reply + 2, reply_len);
  294. }
  295. *length = reply_len;
  296. kfree(reply);
  297. kfree(msg);
  298. return ret;
  299. }
  300. /**
  301. * vmw_host_log: Sends a log message to the host
  302. *
  303. * @log: NULL terminated string
  304. *
  305. * Returns: 0 on success
  306. */
  307. int vmw_host_log(const char *log)
  308. {
  309. struct rpc_channel channel;
  310. char *msg;
  311. int msg_len;
  312. int ret = 0;
  313. if (!vmw_msg_enabled)
  314. return -ENODEV;
  315. if (!log)
  316. return ret;
  317. msg_len = strlen(log) + strlen("log ") + 1;
  318. msg = kzalloc(msg_len, GFP_KERNEL);
  319. if (!msg) {
  320. DRM_ERROR("Cannot allocate memory for log message\n");
  321. return -ENOMEM;
  322. }
  323. sprintf(msg, "log %s", log);
  324. if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM) ||
  325. vmw_send_msg(&channel, msg) ||
  326. vmw_close_channel(&channel)) {
  327. DRM_ERROR("Failed to send log\n");
  328. ret = -EINVAL;
  329. }
  330. kfree(msg);
  331. return ret;
  332. }