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 <asm/hypervisor.h>
33 #include "vmwgfx_msg.h"
36 #define MESSAGE_STATUS_SUCCESS 0x0001
37 #define MESSAGE_STATUS_DORECV 0x0002
38 #define MESSAGE_STATUS_CPT 0x0010
39 #define MESSAGE_STATUS_HB 0x0080
41 #define RPCI_PROTOCOL_NUM 0x49435052
42 #define GUESTMSG_FLAG_COOKIE 0x80000000
46 #define VMW_HYPERVISOR_MAGIC 0x564D5868
47 #define VMW_HYPERVISOR_PORT 0x5658
48 #define VMW_HYPERVISOR_HB_PORT 0x5659
50 #define VMW_PORT_CMD_MSG 30
51 #define VMW_PORT_CMD_HB_MSG 0
52 #define VMW_PORT_CMD_OPEN_CHANNEL (MSG_TYPE_OPEN << 16 | VMW_PORT_CMD_MSG)
53 #define VMW_PORT_CMD_CLOSE_CHANNEL (MSG_TYPE_CLOSE << 16 | VMW_PORT_CMD_MSG)
54 #define VMW_PORT_CMD_SENDSIZE (MSG_TYPE_SENDSIZE << 16 | VMW_PORT_CMD_MSG)
55 #define VMW_PORT_CMD_RECVSIZE (MSG_TYPE_RECVSIZE << 16 | VMW_PORT_CMD_MSG)
56 #define VMW_PORT_CMD_RECVSTATUS (MSG_TYPE_RECVSTATUS << 16 | VMW_PORT_CMD_MSG)
58 #define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)
60 static u32 vmw_msg_enabled = 1;
83 * @channel: RPC channel
86 * Returns: 0 on success
88 static int vmw_open_channel(struct rpc_channel *channel, unsigned int protocol)
90 unsigned long eax, ebx, ecx, edx, si = 0, di = 0;
92 VMW_PORT(VMW_PORT_CMD_OPEN_CHANNEL,
93 (protocol | GUESTMSG_FLAG_COOKIE), si, di,
96 eax, ebx, ecx, edx, si, di);
98 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
101 channel->channel_id = HIGH_WORD(edx);
102 channel->cookie_high = si;
103 channel->cookie_low = di;
113 * @channel: RPC channel
115 * Returns: 0 on success
117 static int vmw_close_channel(struct rpc_channel *channel)
119 unsigned long eax, ebx, ecx, edx, si, di;
121 /* Set up additional parameters */
122 si = channel->cookie_high;
123 di = channel->cookie_low;
125 VMW_PORT(VMW_PORT_CMD_CLOSE_CHANNEL,
127 (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
128 VMW_HYPERVISOR_MAGIC,
129 eax, ebx, ecx, edx, si, di);
131 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
140 * vmw_send_msg: Sends a message to the host
142 * @channel: RPC channel
143 * @logmsg: NULL terminated string
145 * Returns: 0 on success
147 static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
149 unsigned long eax, ebx, ecx, edx, si, di, bp;
150 size_t msg_len = strlen(msg);
154 while (retries < RETRIES) {
157 /* Set up additional parameters */
158 si = channel->cookie_high;
159 di = channel->cookie_low;
161 VMW_PORT(VMW_PORT_CMD_SENDSIZE,
163 VMW_HYPERVISOR_PORT | (channel->channel_id << 16),
164 VMW_HYPERVISOR_MAGIC,
165 eax, ebx, ecx, edx, si, di);
167 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0 ||
168 (HIGH_WORD(ecx) & MESSAGE_STATUS_HB) == 0) {
169 /* Expected success + high-bandwidth. Give up. */
174 si = (uintptr_t) msg;
175 di = channel->cookie_low;
176 bp = channel->cookie_high;
179 (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
181 VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
182 VMW_HYPERVISOR_MAGIC, bp,
183 eax, ebx, ecx, edx, si, di);
185 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) != 0) {
187 } else if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
188 /* A checkpoint occurred. Retry. */
201 * vmw_recv_msg: Receives a message from the host
203 * Note: It is the caller's responsibility to call kfree() on msg.
205 * @channel: channel opened by vmw_open_channel
206 * @msg: [OUT] message received from the host
207 * @msg_len: message length
209 static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
212 unsigned long eax, ebx, ecx, edx, si, di, bp;
221 while (retries < RETRIES) {
224 /* Set up additional parameters */
225 si = channel->cookie_high;
226 di = channel->cookie_low;
228 VMW_PORT(VMW_PORT_CMD_RECVSIZE,
230 (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
231 VMW_HYPERVISOR_MAGIC,
232 eax, ebx, ecx, edx, si, di);
234 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0 ||
235 (HIGH_WORD(ecx) & MESSAGE_STATUS_HB) == 0) {
236 DRM_ERROR("Failed to get reply size\n");
240 /* No reply available. This is okay. */
241 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_DORECV) == 0)
245 reply = kzalloc(reply_len + 1, GFP_KERNEL);
247 DRM_ERROR("Cannot allocate memory for reply\n");
253 si = channel->cookie_high;
254 di = (uintptr_t) reply;
255 bp = channel->cookie_low;
258 (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
260 VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
261 VMW_HYPERVISOR_MAGIC, bp,
262 eax, ebx, ecx, edx, si, di);
264 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) == 0) {
267 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
268 /* A checkpoint occurred. Retry. */
275 reply[reply_len] = '\0';
279 si = channel->cookie_high;
280 di = channel->cookie_low;
282 VMW_PORT(VMW_PORT_CMD_RECVSTATUS,
283 MESSAGE_STATUS_SUCCESS, si, di,
284 (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
285 VMW_HYPERVISOR_MAGIC,
286 eax, ebx, ecx, edx, si, di);
288 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
291 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) {
292 /* A checkpoint occurred. Retry. */
302 *msg_len = reply_len;
310 * vmw_host_get_guestinfo: Gets a GuestInfo parameter
312 * Gets the value of a GuestInfo.* parameter. The value returned will be in
313 * a string, and it is up to the caller to post-process.
315 * @guest_info_param: Parameter to get, e.g. GuestInfo.svga.gl3
316 * @buffer: if NULL, *reply_len will contain reply size.
317 * @length: size of the reply_buf. Set to size of reply upon return
319 * Returns: 0 on success
321 int vmw_host_get_guestinfo(const char *guest_info_param,
322 char *buffer, size_t *length)
324 struct rpc_channel channel;
325 char *msg, *reply = NULL;
326 size_t msg_len, reply_len = 0;
330 if (!vmw_msg_enabled)
333 if (!guest_info_param || !length)
336 msg_len = strlen(guest_info_param) + strlen("info-get ") + 1;
337 msg = kzalloc(msg_len, GFP_KERNEL);
339 DRM_ERROR("Cannot allocate memory to get %s", guest_info_param);
343 sprintf(msg, "info-get %s", guest_info_param);
345 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM) ||
346 vmw_send_msg(&channel, msg) ||
347 vmw_recv_msg(&channel, (void *) &reply, &reply_len) ||
348 vmw_close_channel(&channel)) {
349 DRM_ERROR("Failed to get %s", guest_info_param);
354 if (buffer && reply && reply_len > 0) {
355 /* Remove reply code, which are the first 2 characters of
358 reply_len = max(reply_len - 2, (size_t) 0);
359 reply_len = min(reply_len, *length);
362 memcpy(buffer, reply + 2, reply_len);
376 * vmw_host_log: Sends a log message to the host
378 * @log: NULL terminated string
380 * Returns: 0 on success
382 int vmw_host_log(const char *log)
384 struct rpc_channel channel;
390 if (!vmw_msg_enabled)
396 msg_len = strlen(log) + strlen("log ") + 1;
397 msg = kzalloc(msg_len, GFP_KERNEL);
399 DRM_ERROR("Cannot allocate memory for log message\n");
403 sprintf(msg, "log %s", log);
405 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM) ||
406 vmw_send_msg(&channel, msg) ||
407 vmw_close_channel(&channel)) {
408 DRM_ERROR("Failed to send log\n");