1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * 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_drv.h"
35 #include "vmwgfx_msg.h"
38 #define MESSAGE_STATUS_SUCCESS 0x0001
39 #define MESSAGE_STATUS_DORECV 0x0002
40 #define MESSAGE_STATUS_CPT 0x0010
41 #define MESSAGE_STATUS_HB 0x0080
43 #define RPCI_PROTOCOL_NUM 0x49435052
44 #define GUESTMSG_FLAG_COOKIE 0x80000000
48 #define VMW_HYPERVISOR_MAGIC 0x564D5868
49 #define VMW_HYPERVISOR_PORT 0x5658
50 #define VMW_HYPERVISOR_HB_PORT 0x5659
52 #define VMW_PORT_CMD_MSG 30
53 #define VMW_PORT_CMD_HB_MSG 0
54 #define VMW_PORT_CMD_OPEN_CHANNEL (MSG_TYPE_OPEN << 16 | VMW_PORT_CMD_MSG)
55 #define VMW_PORT_CMD_CLOSE_CHANNEL (MSG_TYPE_CLOSE << 16 | VMW_PORT_CMD_MSG)
56 #define VMW_PORT_CMD_SENDSIZE (MSG_TYPE_SENDSIZE << 16 | VMW_PORT_CMD_MSG)
57 #define VMW_PORT_CMD_RECVSIZE (MSG_TYPE_RECVSIZE << 16 | VMW_PORT_CMD_MSG)
58 #define VMW_PORT_CMD_RECVSTATUS (MSG_TYPE_RECVSTATUS << 16 | VMW_PORT_CMD_MSG)
60 #define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)
62 static u32 vmw_msg_enabled = 1;
85 * @channel: RPC channel
88 * Returns: 0 on success
90 static int vmw_open_channel(struct rpc_channel *channel, unsigned int protocol)
92 unsigned long eax, ebx, ecx, edx, si = 0, di = 0;
94 VMW_PORT(VMW_PORT_CMD_OPEN_CHANNEL,
95 (protocol | GUESTMSG_FLAG_COOKIE), si, di,
98 eax, ebx, ecx, edx, si, di);
100 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
103 channel->channel_id = HIGH_WORD(edx);
104 channel->cookie_high = si;
105 channel->cookie_low = di;
115 * @channel: RPC channel
117 * Returns: 0 on success
119 static int vmw_close_channel(struct rpc_channel *channel)
121 unsigned long eax, ebx, ecx, edx, si, di;
123 /* Set up additional parameters */
124 si = channel->cookie_high;
125 di = channel->cookie_low;
127 VMW_PORT(VMW_PORT_CMD_CLOSE_CHANNEL,
129 (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
130 VMW_HYPERVISOR_MAGIC,
131 eax, ebx, ecx, edx, si, di);
133 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
142 * vmw_send_msg: Sends a message to the host
144 * @channel: RPC channel
145 * @logmsg: NULL terminated string
147 * Returns: 0 on success
149 static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
151 unsigned long eax, ebx, ecx, edx, si, di, bp;
152 size_t msg_len = strlen(msg);
156 while (retries < RETRIES) {
159 /* Set up additional parameters */
160 si = channel->cookie_high;
161 di = channel->cookie_low;
163 VMW_PORT(VMW_PORT_CMD_SENDSIZE,
165 VMW_HYPERVISOR_PORT | (channel->channel_id << 16),
166 VMW_HYPERVISOR_MAGIC,
167 eax, ebx, ecx, edx, si, di);
169 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0 ||
170 (HIGH_WORD(ecx) & MESSAGE_STATUS_HB) == 0) {
171 /* Expected success + high-bandwidth. Give up. */
176 si = (uintptr_t) msg;
177 di = channel->cookie_low;
178 bp = channel->cookie_high;
181 (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
183 VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
184 VMW_HYPERVISOR_MAGIC, bp,
185 eax, ebx, ecx, edx, si, di);
187 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) != 0) {
189 } else if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
190 /* A checkpoint occurred. Retry. */
199 STACK_FRAME_NON_STANDARD(vmw_send_msg);
203 * vmw_recv_msg: Receives a message from the host
205 * Note: It is the caller's responsibility to call kfree() on msg.
207 * @channel: channel opened by vmw_open_channel
208 * @msg: [OUT] message received from the host
209 * @msg_len: message length
211 static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
214 unsigned long eax, ebx, ecx, edx, si, di, bp;
223 while (retries < RETRIES) {
226 /* Set up additional parameters */
227 si = channel->cookie_high;
228 di = channel->cookie_low;
230 VMW_PORT(VMW_PORT_CMD_RECVSIZE,
232 (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
233 VMW_HYPERVISOR_MAGIC,
234 eax, ebx, ecx, edx, si, di);
236 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0 ||
237 (HIGH_WORD(ecx) & MESSAGE_STATUS_HB) == 0) {
238 DRM_ERROR("Failed to get reply size for host message.\n");
242 /* No reply available. This is okay. */
243 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_DORECV) == 0)
247 reply = kzalloc(reply_len + 1, GFP_KERNEL);
249 DRM_ERROR("Cannot allocate memory for host message reply.\n");
255 si = channel->cookie_high;
256 di = (uintptr_t) reply;
257 bp = channel->cookie_low;
260 (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
262 VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
263 VMW_HYPERVISOR_MAGIC, bp,
264 eax, ebx, ecx, edx, si, di);
266 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) == 0) {
269 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
270 /* A checkpoint occurred. Retry. */
277 reply[reply_len] = '\0';
281 si = channel->cookie_high;
282 di = channel->cookie_low;
284 VMW_PORT(VMW_PORT_CMD_RECVSTATUS,
285 MESSAGE_STATUS_SUCCESS, si, di,
286 (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
287 VMW_HYPERVISOR_MAGIC,
288 eax, ebx, ecx, edx, si, di);
290 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
293 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) {
294 /* A checkpoint occurred. Retry. */
304 if (retries == RETRIES)
307 *msg_len = reply_len;
312 STACK_FRAME_NON_STANDARD(vmw_recv_msg);
316 * vmw_host_get_guestinfo: Gets a GuestInfo parameter
318 * Gets the value of a GuestInfo.* parameter. The value returned will be in
319 * a string, and it is up to the caller to post-process.
321 * @guest_info_param: Parameter to get, e.g. GuestInfo.svga.gl3
322 * @buffer: if NULL, *reply_len will contain reply size.
323 * @length: size of the reply_buf. Set to size of reply upon return
325 * Returns: 0 on success
327 int vmw_host_get_guestinfo(const char *guest_info_param,
328 char *buffer, size_t *length)
330 struct rpc_channel channel;
331 char *msg, *reply = NULL;
332 size_t reply_len = 0;
334 if (!vmw_msg_enabled)
337 if (!guest_info_param || !length)
340 msg = kasprintf(GFP_KERNEL, "info-get %s", guest_info_param);
342 DRM_ERROR("Cannot allocate memory to get guest info \"%s\".",
347 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
350 if (vmw_send_msg(&channel, msg) ||
351 vmw_recv_msg(&channel, (void *) &reply, &reply_len))
354 vmw_close_channel(&channel);
355 if (buffer && reply && reply_len > 0) {
356 /* Remove reply code, which are the first 2 characters of
359 reply_len = max(reply_len - 2, (size_t) 0);
360 reply_len = min(reply_len, *length);
363 memcpy(buffer, reply + 2, reply_len);
374 vmw_close_channel(&channel);
379 DRM_ERROR("Failed to get guest info \"%s\".", guest_info_param);
387 * vmw_host_log: Sends a log message to the host
389 * @log: NULL terminated string
391 * Returns: 0 on success
393 int vmw_host_log(const char *log)
395 struct rpc_channel channel;
400 if (!vmw_msg_enabled)
406 msg = kasprintf(GFP_KERNEL, "log %s", log);
408 DRM_ERROR("Cannot allocate memory for host log message.\n");
412 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
415 if (vmw_send_msg(&channel, msg))
418 vmw_close_channel(&channel);
424 vmw_close_channel(&channel);
427 DRM_ERROR("Failed to send host log message.\n");