vmwgfx_msg.c 9.4 KB

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