2 * Copyright © 2016 VMware, Inc., Palo Alto, CA., USA
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
28 #include <linux/slab.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/frame.h>
32 #include <asm/hypervisor.h>
34 #include "vmwgfx_msg.h"
37 #define MESSAGE_STATUS_SUCCESS 0x0001
38 #define MESSAGE_STATUS_DORECV 0x0002
39 #define MESSAGE_STATUS_CPT 0x0010
40 #define MESSAGE_STATUS_HB 0x0080
42 #define RPCI_PROTOCOL_NUM 0x49435052
43 #define GUESTMSG_FLAG_COOKIE 0x80000000
47 #define VMW_HYPERVISOR_MAGIC 0x564D5868
48 #define VMW_HYPERVISOR_PORT 0x5658
49 #define VMW_HYPERVISOR_HB_PORT 0x5659
51 #define VMW_PORT_CMD_MSG 30
52 #define VMW_PORT_CMD_HB_MSG 0
53 #define VMW_PORT_CMD_OPEN_CHANNEL (MSG_TYPE_OPEN << 16 | VMW_PORT_CMD_MSG)
54 #define VMW_PORT_CMD_CLOSE_CHANNEL (MSG_TYPE_CLOSE << 16 | VMW_PORT_CMD_MSG)
55 #define VMW_PORT_CMD_SENDSIZE (MSG_TYPE_SENDSIZE << 16 | VMW_PORT_CMD_MSG)
56 #define VMW_PORT_CMD_RECVSIZE (MSG_TYPE_RECVSIZE << 16 | VMW_PORT_CMD_MSG)
57 #define VMW_PORT_CMD_RECVSTATUS (MSG_TYPE_RECVSTATUS << 16 | VMW_PORT_CMD_MSG)
59 #define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)
61 static u32 vmw_msg_enabled = 1;
84 * @channel: RPC channel
87 * Returns: 0 on success
89 static int vmw_open_channel(struct rpc_channel *channel, unsigned int protocol)
91 unsigned long eax, ebx, ecx, edx, si = 0, di = 0;
93 VMW_PORT(VMW_PORT_CMD_OPEN_CHANNEL,
94 (protocol | GUESTMSG_FLAG_COOKIE), si, di,
97 eax, ebx, ecx, edx, si, di);
99 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
102 channel->channel_id = HIGH_WORD(edx);
103 channel->cookie_high = si;
104 channel->cookie_low = di;
114 * @channel: RPC channel
116 * Returns: 0 on success
118 static int vmw_close_channel(struct rpc_channel *channel)
120 unsigned long eax, ebx, ecx, edx, si, di;
122 /* Set up additional parameters */
123 si = channel->cookie_high;
124 di = channel->cookie_low;
126 VMW_PORT(VMW_PORT_CMD_CLOSE_CHANNEL,
128 (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
129 VMW_HYPERVISOR_MAGIC,
130 eax, ebx, ecx, edx, si, di);
132 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
141 * vmw_send_msg: Sends a message to the host
143 * @channel: RPC channel
144 * @logmsg: NULL terminated string
146 * Returns: 0 on success
148 static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
150 unsigned long eax, ebx, ecx, edx, si, di, bp;
151 size_t msg_len = strlen(msg);
155 while (retries < RETRIES) {
158 /* Set up additional parameters */
159 si = channel->cookie_high;
160 di = channel->cookie_low;
162 VMW_PORT(VMW_PORT_CMD_SENDSIZE,
164 VMW_HYPERVISOR_PORT | (channel->channel_id << 16),
165 VMW_HYPERVISOR_MAGIC,
166 eax, ebx, ecx, edx, si, di);
168 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0 ||
169 (HIGH_WORD(ecx) & MESSAGE_STATUS_HB) == 0) {
170 /* Expected success + high-bandwidth. Give up. */
175 si = (uintptr_t) msg;
176 di = channel->cookie_low;
177 bp = channel->cookie_high;
180 (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
182 VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
183 VMW_HYPERVISOR_MAGIC, bp,
184 eax, ebx, ecx, edx, si, di);
186 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) != 0) {
188 } else if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
189 /* A checkpoint occurred. Retry. */
198 STACK_FRAME_NON_STANDARD(vmw_send_msg);
202 * vmw_recv_msg: Receives a message from the host
204 * Note: It is the caller's responsibility to call kfree() on msg.
206 * @channel: channel opened by vmw_open_channel
207 * @msg: [OUT] message received from the host
208 * @msg_len: message length
210 static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
213 unsigned long eax, ebx, ecx, edx, si, di, bp;
222 while (retries < RETRIES) {
225 /* Set up additional parameters */
226 si = channel->cookie_high;
227 di = channel->cookie_low;
229 VMW_PORT(VMW_PORT_CMD_RECVSIZE,
231 (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
232 VMW_HYPERVISOR_MAGIC,
233 eax, ebx, ecx, edx, si, di);
235 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0 ||
236 (HIGH_WORD(ecx) & MESSAGE_STATUS_HB) == 0) {
237 DRM_ERROR("Failed to get reply size\n");
241 /* No reply available. This is okay. */
242 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_DORECV) == 0)
246 reply = kzalloc(reply_len + 1, GFP_KERNEL);
248 DRM_ERROR("Cannot allocate memory for reply\n");
254 si = channel->cookie_high;
255 di = (uintptr_t) reply;
256 bp = channel->cookie_low;
259 (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
261 VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
262 VMW_HYPERVISOR_MAGIC, bp,
263 eax, ebx, ecx, edx, si, di);
265 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) == 0) {
268 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
269 /* A checkpoint occurred. Retry. */
276 reply[reply_len] = '\0';
280 si = channel->cookie_high;
281 di = channel->cookie_low;
283 VMW_PORT(VMW_PORT_CMD_RECVSTATUS,
284 MESSAGE_STATUS_SUCCESS, si, di,
285 (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
286 VMW_HYPERVISOR_MAGIC,
287 eax, ebx, ecx, edx, si, di);
289 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
292 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) {
293 /* A checkpoint occurred. Retry. */
303 if (retries == RETRIES)
306 *msg_len = reply_len;
311 STACK_FRAME_NON_STANDARD(vmw_recv_msg);
315 * vmw_host_get_guestinfo: Gets a GuestInfo parameter
317 * Gets the value of a GuestInfo.* parameter. The value returned will be in
318 * a string, and it is up to the caller to post-process.
320 * @guest_info_param: Parameter to get, e.g. GuestInfo.svga.gl3
321 * @buffer: if NULL, *reply_len will contain reply size.
322 * @length: size of the reply_buf. Set to size of reply upon return
324 * Returns: 0 on success
326 int vmw_host_get_guestinfo(const char *guest_info_param,
327 char *buffer, size_t *length)
329 struct rpc_channel channel;
330 char *msg, *reply = NULL;
331 size_t msg_len, reply_len = 0;
335 if (!vmw_msg_enabled)
338 if (!guest_info_param || !length)
341 msg_len = strlen(guest_info_param) + strlen("info-get ") + 1;
342 msg = kzalloc(msg_len, GFP_KERNEL);
344 DRM_ERROR("Cannot allocate memory to get %s", guest_info_param);
348 sprintf(msg, "info-get %s", guest_info_param);
350 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM) ||
351 vmw_send_msg(&channel, msg) ||
352 vmw_recv_msg(&channel, (void *) &reply, &reply_len) ||
353 vmw_close_channel(&channel)) {
354 DRM_ERROR("Failed to get %s", guest_info_param);
359 if (buffer && reply && reply_len > 0) {
360 /* Remove reply code, which are the first 2 characters of
363 reply_len = max(reply_len - 2, (size_t) 0);
364 reply_len = min(reply_len, *length);
367 memcpy(buffer, reply + 2, reply_len);
381 * vmw_host_log: Sends a log message to the host
383 * @log: NULL terminated string
385 * Returns: 0 on success
387 int vmw_host_log(const char *log)
389 struct rpc_channel channel;
395 if (!vmw_msg_enabled)
401 msg_len = strlen(log) + strlen("log ") + 1;
402 msg = kzalloc(msg_len, GFP_KERNEL);
404 DRM_ERROR("Cannot allocate memory for log message\n");
408 sprintf(msg, "log %s", log);
410 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM) ||
411 vmw_send_msg(&channel, msg) ||
412 vmw_close_channel(&channel)) {
413 DRM_ERROR("Failed to send log\n");