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.
27 #include <linux/frame.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
32 #include <asm/hypervisor.h>
34 #include "vmwgfx_drv.h"
35 #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
49 #define VMW_PORT_CMD_MSG 30
50 #define VMW_PORT_CMD_HB_MSG 0
51 #define VMW_PORT_CMD_OPEN_CHANNEL (MSG_TYPE_OPEN << 16 | VMW_PORT_CMD_MSG)
52 #define VMW_PORT_CMD_CLOSE_CHANNEL (MSG_TYPE_CLOSE << 16 | VMW_PORT_CMD_MSG)
53 #define VMW_PORT_CMD_SENDSIZE (MSG_TYPE_SENDSIZE << 16 | VMW_PORT_CMD_MSG)
54 #define VMW_PORT_CMD_RECVSIZE (MSG_TYPE_RECVSIZE << 16 | VMW_PORT_CMD_MSG)
55 #define VMW_PORT_CMD_RECVSTATUS (MSG_TYPE_RECVSTATUS << 16 | VMW_PORT_CMD_MSG)
57 #define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)
59 static u32 vmw_msg_enabled
= 1;
82 * @channel: RPC channel
85 * Returns: 0 on success
87 static int vmw_open_channel(struct rpc_channel
*channel
, unsigned int protocol
)
89 unsigned long eax
, ebx
, ecx
, edx
, si
= 0, di
= 0;
91 VMW_PORT(VMW_PORT_CMD_OPEN_CHANNEL
,
92 (protocol
| GUESTMSG_FLAG_COOKIE
), si
, di
,
95 eax
, ebx
, ecx
, edx
, si
, di
);
97 if ((HIGH_WORD(ecx
) & MESSAGE_STATUS_SUCCESS
) == 0)
100 channel
->channel_id
= HIGH_WORD(edx
);
101 channel
->cookie_high
= si
;
102 channel
->cookie_low
= di
;
112 * @channel: RPC channel
114 * Returns: 0 on success
116 static int vmw_close_channel(struct rpc_channel
*channel
)
118 unsigned long eax
, ebx
, ecx
, edx
, si
, di
;
120 /* Set up additional parameters */
121 si
= channel
->cookie_high
;
122 di
= channel
->cookie_low
;
124 VMW_PORT(VMW_PORT_CMD_CLOSE_CHANNEL
,
126 channel
->channel_id
<< 16,
127 VMW_HYPERVISOR_MAGIC
,
128 eax
, ebx
, ecx
, edx
, si
, di
);
130 if ((HIGH_WORD(ecx
) & MESSAGE_STATUS_SUCCESS
) == 0)
137 * vmw_port_hb_out - Send the message payload either through the
138 * high-bandwidth port if available, or through the backdoor otherwise.
139 * @channel: The rpc channel.
140 * @msg: NULL-terminated message.
141 * @hb: Whether the high-bandwidth port is available.
143 * Return: The port status.
145 static unsigned long vmw_port_hb_out(struct rpc_channel
*channel
,
146 const char *msg
, bool hb
)
148 unsigned long si
, di
, eax
, ebx
, ecx
, edx
;
149 unsigned long msg_len
= strlen(msg
);
152 unsigned long bp
= channel
->cookie_high
;
154 si
= (uintptr_t) msg
;
155 di
= channel
->cookie_low
;
158 (MESSAGE_STATUS_SUCCESS
<< 16) | VMW_PORT_CMD_HB_MSG
,
160 VMWARE_HYPERVISOR_HB
| (channel
->channel_id
<< 16) |
161 VMWARE_HYPERVISOR_OUT
,
162 VMW_HYPERVISOR_MAGIC
, bp
,
163 eax
, ebx
, ecx
, edx
, si
, di
);
168 /* HB port not available. Send the message 4 bytes at a time. */
169 ecx
= MESSAGE_STATUS_SUCCESS
<< 16;
170 while (msg_len
&& (HIGH_WORD(ecx
) & MESSAGE_STATUS_SUCCESS
)) {
171 unsigned int bytes
= min_t(size_t, msg_len
, 4);
172 unsigned long word
= 0;
174 memcpy(&word
, msg
, bytes
);
177 si
= channel
->cookie_high
;
178 di
= channel
->cookie_low
;
180 VMW_PORT(VMW_PORT_CMD_MSG
| (MSG_TYPE_SENDPAYLOAD
<< 16),
182 channel
->channel_id
<< 16,
183 VMW_HYPERVISOR_MAGIC
,
184 eax
, ebx
, ecx
, edx
, si
, di
);
191 * vmw_port_hb_in - Receive the message payload either through the
192 * high-bandwidth port if available, or through the backdoor otherwise.
193 * @channel: The rpc channel.
194 * @reply: Pointer to buffer holding reply.
195 * @reply_len: Length of the reply.
196 * @hb: Whether the high-bandwidth port is available.
198 * Return: The port status.
200 static unsigned long vmw_port_hb_in(struct rpc_channel
*channel
, char *reply
,
201 unsigned long reply_len
, bool hb
)
203 unsigned long si
, di
, eax
, ebx
, ecx
, edx
;
206 unsigned long bp
= channel
->cookie_low
;
208 si
= channel
->cookie_high
;
209 di
= (uintptr_t) reply
;
212 (MESSAGE_STATUS_SUCCESS
<< 16) | VMW_PORT_CMD_HB_MSG
,
214 VMWARE_HYPERVISOR_HB
| (channel
->channel_id
<< 16),
215 VMW_HYPERVISOR_MAGIC
, bp
,
216 eax
, ebx
, ecx
, edx
, si
, di
);
221 /* HB port not available. Retrieve the message 4 bytes at a time. */
222 ecx
= MESSAGE_STATUS_SUCCESS
<< 16;
224 unsigned int bytes
= min_t(unsigned long, reply_len
, 4);
226 si
= channel
->cookie_high
;
227 di
= channel
->cookie_low
;
229 VMW_PORT(VMW_PORT_CMD_MSG
| (MSG_TYPE_RECVPAYLOAD
<< 16),
230 MESSAGE_STATUS_SUCCESS
, si
, di
,
231 channel
->channel_id
<< 16,
232 VMW_HYPERVISOR_MAGIC
,
233 eax
, ebx
, ecx
, edx
, si
, di
);
235 if ((HIGH_WORD(ecx
) & MESSAGE_STATUS_SUCCESS
) == 0)
238 memcpy(reply
, &ebx
, bytes
);
248 * vmw_send_msg: Sends a message to the host
250 * @channel: RPC channel
251 * @logmsg: NULL terminated string
253 * Returns: 0 on success
255 static int vmw_send_msg(struct rpc_channel
*channel
, const char *msg
)
257 unsigned long eax
, ebx
, ecx
, edx
, si
, di
;
258 size_t msg_len
= strlen(msg
);
261 while (retries
< RETRIES
) {
264 /* Set up additional parameters */
265 si
= channel
->cookie_high
;
266 di
= channel
->cookie_low
;
268 VMW_PORT(VMW_PORT_CMD_SENDSIZE
,
270 channel
->channel_id
<< 16,
271 VMW_HYPERVISOR_MAGIC
,
272 eax
, ebx
, ecx
, edx
, si
, di
);
274 if ((HIGH_WORD(ecx
) & MESSAGE_STATUS_SUCCESS
) == 0) {
275 /* Expected success. Give up. */
280 ebx
= vmw_port_hb_out(channel
, msg
,
281 !!(HIGH_WORD(ecx
) & MESSAGE_STATUS_HB
));
283 if ((HIGH_WORD(ebx
) & MESSAGE_STATUS_SUCCESS
) != 0) {
285 } else if ((HIGH_WORD(ebx
) & MESSAGE_STATUS_CPT
) != 0) {
286 /* A checkpoint occurred. Retry. */
295 STACK_FRAME_NON_STANDARD(vmw_send_msg
);
299 * vmw_recv_msg: Receives a message from the host
301 * Note: It is the caller's responsibility to call kfree() on msg.
303 * @channel: channel opened by vmw_open_channel
304 * @msg: [OUT] message received from the host
305 * @msg_len: message length
307 static int vmw_recv_msg(struct rpc_channel
*channel
, void **msg
,
310 unsigned long eax
, ebx
, ecx
, edx
, si
, di
;
319 while (retries
< RETRIES
) {
322 /* Set up additional parameters */
323 si
= channel
->cookie_high
;
324 di
= channel
->cookie_low
;
326 VMW_PORT(VMW_PORT_CMD_RECVSIZE
,
328 channel
->channel_id
<< 16,
329 VMW_HYPERVISOR_MAGIC
,
330 eax
, ebx
, ecx
, edx
, si
, di
);
332 if ((HIGH_WORD(ecx
) & MESSAGE_STATUS_SUCCESS
) == 0) {
333 DRM_ERROR("Failed to get reply size for host message.\n");
337 /* No reply available. This is okay. */
338 if ((HIGH_WORD(ecx
) & MESSAGE_STATUS_DORECV
) == 0)
342 reply
= kzalloc(reply_len
+ 1, GFP_KERNEL
);
344 DRM_ERROR("Cannot allocate memory for host message reply.\n");
350 ebx
= vmw_port_hb_in(channel
, reply
, reply_len
,
351 !!(HIGH_WORD(ecx
) & MESSAGE_STATUS_HB
));
352 if ((HIGH_WORD(ebx
) & MESSAGE_STATUS_SUCCESS
) == 0) {
355 if ((HIGH_WORD(ebx
) & MESSAGE_STATUS_CPT
) != 0) {
356 /* A checkpoint occurred. Retry. */
363 reply
[reply_len
] = '\0';
367 si
= channel
->cookie_high
;
368 di
= channel
->cookie_low
;
370 VMW_PORT(VMW_PORT_CMD_RECVSTATUS
,
371 MESSAGE_STATUS_SUCCESS
, si
, di
,
372 channel
->channel_id
<< 16,
373 VMW_HYPERVISOR_MAGIC
,
374 eax
, ebx
, ecx
, edx
, si
, di
);
376 if ((HIGH_WORD(ecx
) & MESSAGE_STATUS_SUCCESS
) == 0) {
379 if ((HIGH_WORD(ecx
) & MESSAGE_STATUS_CPT
) != 0) {
380 /* A checkpoint occurred. Retry. */
393 *msg_len
= reply_len
;
398 STACK_FRAME_NON_STANDARD(vmw_recv_msg
);
402 * vmw_host_get_guestinfo: Gets a GuestInfo parameter
404 * Gets the value of a GuestInfo.* parameter. The value returned will be in
405 * a string, and it is up to the caller to post-process.
407 * @guest_info_param: Parameter to get, e.g. GuestInfo.svga.gl3
408 * @buffer: if NULL, *reply_len will contain reply size.
409 * @length: size of the reply_buf. Set to size of reply upon return
411 * Returns: 0 on success
413 int vmw_host_get_guestinfo(const char *guest_info_param
,
414 char *buffer
, size_t *length
)
416 struct rpc_channel channel
;
417 char *msg
, *reply
= NULL
;
418 size_t reply_len
= 0;
420 if (!vmw_msg_enabled
)
423 if (!guest_info_param
|| !length
)
426 msg
= kasprintf(GFP_KERNEL
, "info-get %s", guest_info_param
);
428 DRM_ERROR("Cannot allocate memory to get guest info \"%s\".",
433 if (vmw_open_channel(&channel
, RPCI_PROTOCOL_NUM
))
436 if (vmw_send_msg(&channel
, msg
) ||
437 vmw_recv_msg(&channel
, (void *) &reply
, &reply_len
))
440 vmw_close_channel(&channel
);
441 if (buffer
&& reply
&& reply_len
> 0) {
442 /* Remove reply code, which are the first 2 characters of
445 reply_len
= max(reply_len
- 2, (size_t) 0);
446 reply_len
= min(reply_len
, *length
);
449 memcpy(buffer
, reply
+ 2, reply_len
);
460 vmw_close_channel(&channel
);
465 DRM_ERROR("Failed to get guest info \"%s\".", guest_info_param
);
473 * vmw_host_log: Sends a log message to the host
475 * @log: NULL terminated string
477 * Returns: 0 on success
479 int vmw_host_log(const char *log
)
481 struct rpc_channel channel
;
486 if (!vmw_msg_enabled
)
492 msg
= kasprintf(GFP_KERNEL
, "log %s", log
);
494 DRM_ERROR("Cannot allocate memory for host log message.\n");
498 if (vmw_open_channel(&channel
, RPCI_PROTOCOL_NUM
))
501 if (vmw_send_msg(&channel
, msg
))
504 vmw_close_channel(&channel
);
510 vmw_close_channel(&channel
);
513 DRM_ERROR("Failed to send host log message.\n");