Loading...
1// SPDX-License-Identifier: GPL-2.0 OR MIT
2/*
3 * Copyright 2016 VMware, Inc., Palo Alto, CA., USA
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
27#include <linux/frame.h>
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/slab.h>
31
32#include <asm/hypervisor.h>
33
34#include "vmwgfx_drv.h"
35#include "vmwgfx_msg.h"
36
37#define MESSAGE_STATUS_SUCCESS 0x0001
38#define MESSAGE_STATUS_DORECV 0x0002
39#define MESSAGE_STATUS_CPT 0x0010
40#define MESSAGE_STATUS_HB 0x0080
41
42#define RPCI_PROTOCOL_NUM 0x49435052
43#define GUESTMSG_FLAG_COOKIE 0x80000000
44
45#define RETRIES 3
46
47#define VMW_HYPERVISOR_MAGIC 0x564D5868
48
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)
56
57#define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)
58
59static u32 vmw_msg_enabled = 1;
60
61enum rpc_msg_type {
62 MSG_TYPE_OPEN,
63 MSG_TYPE_SENDSIZE,
64 MSG_TYPE_SENDPAYLOAD,
65 MSG_TYPE_RECVSIZE,
66 MSG_TYPE_RECVPAYLOAD,
67 MSG_TYPE_RECVSTATUS,
68 MSG_TYPE_CLOSE,
69};
70
71struct rpc_channel {
72 u16 channel_id;
73 u32 cookie_high;
74 u32 cookie_low;
75};
76
77
78
79/**
80 * vmw_open_channel
81 *
82 * @channel: RPC channel
83 * @protocol:
84 *
85 * Returns: 0 on success
86 */
87static int vmw_open_channel(struct rpc_channel *channel, unsigned int protocol)
88{
89 unsigned long eax, ebx, ecx, edx, si = 0, di = 0;
90
91 VMW_PORT(VMW_PORT_CMD_OPEN_CHANNEL,
92 (protocol | GUESTMSG_FLAG_COOKIE), si, di,
93 0,
94 VMW_HYPERVISOR_MAGIC,
95 eax, ebx, ecx, edx, si, di);
96
97 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
98 return -EINVAL;
99
100 channel->channel_id = HIGH_WORD(edx);
101 channel->cookie_high = si;
102 channel->cookie_low = di;
103
104 return 0;
105}
106
107
108
109/**
110 * vmw_close_channel
111 *
112 * @channel: RPC channel
113 *
114 * Returns: 0 on success
115 */
116static int vmw_close_channel(struct rpc_channel *channel)
117{
118 unsigned long eax, ebx, ecx, edx, si, di;
119
120 /* Set up additional parameters */
121 si = channel->cookie_high;
122 di = channel->cookie_low;
123
124 VMW_PORT(VMW_PORT_CMD_CLOSE_CHANNEL,
125 0, si, di,
126 channel->channel_id << 16,
127 VMW_HYPERVISOR_MAGIC,
128 eax, ebx, ecx, edx, si, di);
129
130 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
131 return -EINVAL;
132
133 return 0;
134}
135
136/**
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.
142 *
143 * Return: The port status.
144 */
145static unsigned long vmw_port_hb_out(struct rpc_channel *channel,
146 const char *msg, bool hb)
147{
148 unsigned long si, di, eax, ebx, ecx, edx;
149 unsigned long msg_len = strlen(msg);
150
151 if (hb) {
152 unsigned long bp = channel->cookie_high;
153
154 si = (uintptr_t) msg;
155 di = channel->cookie_low;
156
157 VMW_PORT_HB_OUT(
158 (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
159 msg_len, si, di,
160 VMWARE_HYPERVISOR_HB | (channel->channel_id << 16) |
161 VMWARE_HYPERVISOR_OUT,
162 VMW_HYPERVISOR_MAGIC, bp,
163 eax, ebx, ecx, edx, si, di);
164
165 return ebx;
166 }
167
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;
173
174 memcpy(&word, msg, bytes);
175 msg_len -= bytes;
176 msg += bytes;
177 si = channel->cookie_high;
178 di = channel->cookie_low;
179
180 VMW_PORT(VMW_PORT_CMD_MSG | (MSG_TYPE_SENDPAYLOAD << 16),
181 word, si, di,
182 channel->channel_id << 16,
183 VMW_HYPERVISOR_MAGIC,
184 eax, ebx, ecx, edx, si, di);
185 }
186
187 return ecx;
188}
189
190/**
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.
197 *
198 * Return: The port status.
199 */
200static unsigned long vmw_port_hb_in(struct rpc_channel *channel, char *reply,
201 unsigned long reply_len, bool hb)
202{
203 unsigned long si, di, eax, ebx, ecx, edx;
204
205 if (hb) {
206 unsigned long bp = channel->cookie_low;
207
208 si = channel->cookie_high;
209 di = (uintptr_t) reply;
210
211 VMW_PORT_HB_IN(
212 (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
213 reply_len, si, di,
214 VMWARE_HYPERVISOR_HB | (channel->channel_id << 16),
215 VMW_HYPERVISOR_MAGIC, bp,
216 eax, ebx, ecx, edx, si, di);
217
218 return ebx;
219 }
220
221 /* HB port not available. Retrieve the message 4 bytes at a time. */
222 ecx = MESSAGE_STATUS_SUCCESS << 16;
223 while (reply_len) {
224 unsigned int bytes = min_t(unsigned long, reply_len, 4);
225
226 si = channel->cookie_high;
227 di = channel->cookie_low;
228
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);
234
235 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
236 break;
237
238 memcpy(reply, &ebx, bytes);
239 reply_len -= bytes;
240 reply += bytes;
241 }
242
243 return ecx;
244}
245
246
247/**
248 * vmw_send_msg: Sends a message to the host
249 *
250 * @channel: RPC channel
251 * @logmsg: NULL terminated string
252 *
253 * Returns: 0 on success
254 */
255static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
256{
257 unsigned long eax, ebx, ecx, edx, si, di;
258 size_t msg_len = strlen(msg);
259 int retries = 0;
260
261 while (retries < RETRIES) {
262 retries++;
263
264 /* Set up additional parameters */
265 si = channel->cookie_high;
266 di = channel->cookie_low;
267
268 VMW_PORT(VMW_PORT_CMD_SENDSIZE,
269 msg_len, si, di,
270 channel->channel_id << 16,
271 VMW_HYPERVISOR_MAGIC,
272 eax, ebx, ecx, edx, si, di);
273
274 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
275 /* Expected success. Give up. */
276 return -EINVAL;
277 }
278
279 /* Send msg */
280 ebx = vmw_port_hb_out(channel, msg,
281 !!(HIGH_WORD(ecx) & MESSAGE_STATUS_HB));
282
283 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) != 0) {
284 return 0;
285 } else if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
286 /* A checkpoint occurred. Retry. */
287 continue;
288 } else {
289 break;
290 }
291 }
292
293 return -EINVAL;
294}
295STACK_FRAME_NON_STANDARD(vmw_send_msg);
296
297
298/**
299 * vmw_recv_msg: Receives a message from the host
300 *
301 * Note: It is the caller's responsibility to call kfree() on msg.
302 *
303 * @channel: channel opened by vmw_open_channel
304 * @msg: [OUT] message received from the host
305 * @msg_len: message length
306 */
307static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
308 size_t *msg_len)
309{
310 unsigned long eax, ebx, ecx, edx, si, di;
311 char *reply;
312 size_t reply_len;
313 int retries = 0;
314
315
316 *msg_len = 0;
317 *msg = NULL;
318
319 while (retries < RETRIES) {
320 retries++;
321
322 /* Set up additional parameters */
323 si = channel->cookie_high;
324 di = channel->cookie_low;
325
326 VMW_PORT(VMW_PORT_CMD_RECVSIZE,
327 0, si, di,
328 channel->channel_id << 16,
329 VMW_HYPERVISOR_MAGIC,
330 eax, ebx, ecx, edx, si, di);
331
332 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
333 DRM_ERROR("Failed to get reply size for host message.\n");
334 return -EINVAL;
335 }
336
337 /* No reply available. This is okay. */
338 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_DORECV) == 0)
339 return 0;
340
341 reply_len = ebx;
342 reply = kzalloc(reply_len + 1, GFP_KERNEL);
343 if (!reply) {
344 DRM_ERROR("Cannot allocate memory for host message reply.\n");
345 return -ENOMEM;
346 }
347
348
349 /* Receive buffer */
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) {
353 kfree(reply);
354 reply = NULL;
355 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
356 /* A checkpoint occurred. Retry. */
357 continue;
358 }
359
360 return -EINVAL;
361 }
362
363 reply[reply_len] = '\0';
364
365
366 /* Ack buffer */
367 si = channel->cookie_high;
368 di = channel->cookie_low;
369
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);
375
376 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
377 kfree(reply);
378 reply = NULL;
379 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) {
380 /* A checkpoint occurred. Retry. */
381 continue;
382 }
383
384 return -EINVAL;
385 }
386
387 break;
388 }
389
390 if (!reply)
391 return -EINVAL;
392
393 *msg_len = reply_len;
394 *msg = reply;
395
396 return 0;
397}
398STACK_FRAME_NON_STANDARD(vmw_recv_msg);
399
400
401/**
402 * vmw_host_get_guestinfo: Gets a GuestInfo parameter
403 *
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.
406 *
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
410 *
411 * Returns: 0 on success
412 */
413int vmw_host_get_guestinfo(const char *guest_info_param,
414 char *buffer, size_t *length)
415{
416 struct rpc_channel channel;
417 char *msg, *reply = NULL;
418 size_t reply_len = 0;
419
420 if (!vmw_msg_enabled)
421 return -ENODEV;
422
423 if (!guest_info_param || !length)
424 return -EINVAL;
425
426 msg = kasprintf(GFP_KERNEL, "info-get %s", guest_info_param);
427 if (!msg) {
428 DRM_ERROR("Cannot allocate memory to get guest info \"%s\".",
429 guest_info_param);
430 return -ENOMEM;
431 }
432
433 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
434 goto out_open;
435
436 if (vmw_send_msg(&channel, msg) ||
437 vmw_recv_msg(&channel, (void *) &reply, &reply_len))
438 goto out_msg;
439
440 vmw_close_channel(&channel);
441 if (buffer && reply && reply_len > 0) {
442 /* Remove reply code, which are the first 2 characters of
443 * the reply
444 */
445 reply_len = max(reply_len - 2, (size_t) 0);
446 reply_len = min(reply_len, *length);
447
448 if (reply_len > 0)
449 memcpy(buffer, reply + 2, reply_len);
450 }
451
452 *length = reply_len;
453
454 kfree(reply);
455 kfree(msg);
456
457 return 0;
458
459out_msg:
460 vmw_close_channel(&channel);
461 kfree(reply);
462out_open:
463 *length = 0;
464 kfree(msg);
465 DRM_ERROR("Failed to get guest info \"%s\".", guest_info_param);
466
467 return -EINVAL;
468}
469
470
471
472/**
473 * vmw_host_log: Sends a log message to the host
474 *
475 * @log: NULL terminated string
476 *
477 * Returns: 0 on success
478 */
479int vmw_host_log(const char *log)
480{
481 struct rpc_channel channel;
482 char *msg;
483 int ret = 0;
484
485
486 if (!vmw_msg_enabled)
487 return -ENODEV;
488
489 if (!log)
490 return ret;
491
492 msg = kasprintf(GFP_KERNEL, "log %s", log);
493 if (!msg) {
494 DRM_ERROR("Cannot allocate memory for host log message.\n");
495 return -ENOMEM;
496 }
497
498 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
499 goto out_open;
500
501 if (vmw_send_msg(&channel, msg))
502 goto out_msg;
503
504 vmw_close_channel(&channel);
505 kfree(msg);
506
507 return 0;
508
509out_msg:
510 vmw_close_channel(&channel);
511out_open:
512 kfree(msg);
513 DRM_ERROR("Failed to send host log message.\n");
514
515 return -EINVAL;
516}
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
27
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>
33#include <drm/drmP.h>
34#include "vmwgfx_msg.h"
35
36
37#define MESSAGE_STATUS_SUCCESS 0x0001
38#define MESSAGE_STATUS_DORECV 0x0002
39#define MESSAGE_STATUS_CPT 0x0010
40#define MESSAGE_STATUS_HB 0x0080
41
42#define RPCI_PROTOCOL_NUM 0x49435052
43#define GUESTMSG_FLAG_COOKIE 0x80000000
44
45#define RETRIES 3
46
47#define VMW_HYPERVISOR_MAGIC 0x564D5868
48#define VMW_HYPERVISOR_PORT 0x5658
49#define VMW_HYPERVISOR_HB_PORT 0x5659
50
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)
58
59#define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)
60
61static u32 vmw_msg_enabled = 1;
62
63enum rpc_msg_type {
64 MSG_TYPE_OPEN,
65 MSG_TYPE_SENDSIZE,
66 MSG_TYPE_SENDPAYLOAD,
67 MSG_TYPE_RECVSIZE,
68 MSG_TYPE_RECVPAYLOAD,
69 MSG_TYPE_RECVSTATUS,
70 MSG_TYPE_CLOSE,
71};
72
73struct rpc_channel {
74 u16 channel_id;
75 u32 cookie_high;
76 u32 cookie_low;
77};
78
79
80
81/**
82 * vmw_open_channel
83 *
84 * @channel: RPC channel
85 * @protocol:
86 *
87 * Returns: 0 on success
88 */
89static int vmw_open_channel(struct rpc_channel *channel, unsigned int protocol)
90{
91 unsigned long eax, ebx, ecx, edx, si = 0, di = 0;
92
93 VMW_PORT(VMW_PORT_CMD_OPEN_CHANNEL,
94 (protocol | GUESTMSG_FLAG_COOKIE), si, di,
95 VMW_HYPERVISOR_PORT,
96 VMW_HYPERVISOR_MAGIC,
97 eax, ebx, ecx, edx, si, di);
98
99 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
100 return -EINVAL;
101
102 channel->channel_id = HIGH_WORD(edx);
103 channel->cookie_high = si;
104 channel->cookie_low = di;
105
106 return 0;
107}
108
109
110
111/**
112 * vmw_close_channel
113 *
114 * @channel: RPC channel
115 *
116 * Returns: 0 on success
117 */
118static int vmw_close_channel(struct rpc_channel *channel)
119{
120 unsigned long eax, ebx, ecx, edx, si, di;
121
122 /* Set up additional parameters */
123 si = channel->cookie_high;
124 di = channel->cookie_low;
125
126 VMW_PORT(VMW_PORT_CMD_CLOSE_CHANNEL,
127 0, si, di,
128 (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
129 VMW_HYPERVISOR_MAGIC,
130 eax, ebx, ecx, edx, si, di);
131
132 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
133 return -EINVAL;
134
135 return 0;
136}
137
138
139
140/**
141 * vmw_send_msg: Sends a message to the host
142 *
143 * @channel: RPC channel
144 * @logmsg: NULL terminated string
145 *
146 * Returns: 0 on success
147 */
148static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
149{
150 unsigned long eax, ebx, ecx, edx, si, di, bp;
151 size_t msg_len = strlen(msg);
152 int retries = 0;
153
154
155 while (retries < RETRIES) {
156 retries++;
157
158 /* Set up additional parameters */
159 si = channel->cookie_high;
160 di = channel->cookie_low;
161
162 VMW_PORT(VMW_PORT_CMD_SENDSIZE,
163 msg_len, si, di,
164 VMW_HYPERVISOR_PORT | (channel->channel_id << 16),
165 VMW_HYPERVISOR_MAGIC,
166 eax, ebx, ecx, edx, si, di);
167
168 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0 ||
169 (HIGH_WORD(ecx) & MESSAGE_STATUS_HB) == 0) {
170 /* Expected success + high-bandwidth. Give up. */
171 return -EINVAL;
172 }
173
174 /* Send msg */
175 si = (uintptr_t) msg;
176 di = channel->cookie_low;
177 bp = channel->cookie_high;
178
179 VMW_PORT_HB_OUT(
180 (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
181 msg_len, si, di,
182 VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
183 VMW_HYPERVISOR_MAGIC, bp,
184 eax, ebx, ecx, edx, si, di);
185
186 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) != 0) {
187 return 0;
188 } else if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
189 /* A checkpoint occurred. Retry. */
190 continue;
191 } else {
192 break;
193 }
194 }
195
196 return -EINVAL;
197}
198STACK_FRAME_NON_STANDARD(vmw_send_msg);
199
200
201/**
202 * vmw_recv_msg: Receives a message from the host
203 *
204 * Note: It is the caller's responsibility to call kfree() on msg.
205 *
206 * @channel: channel opened by vmw_open_channel
207 * @msg: [OUT] message received from the host
208 * @msg_len: message length
209 */
210static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
211 size_t *msg_len)
212{
213 unsigned long eax, ebx, ecx, edx, si, di, bp;
214 char *reply;
215 size_t reply_len;
216 int retries = 0;
217
218
219 *msg_len = 0;
220 *msg = NULL;
221
222 while (retries < RETRIES) {
223 retries++;
224
225 /* Set up additional parameters */
226 si = channel->cookie_high;
227 di = channel->cookie_low;
228
229 VMW_PORT(VMW_PORT_CMD_RECVSIZE,
230 0, si, di,
231 (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
232 VMW_HYPERVISOR_MAGIC,
233 eax, ebx, ecx, edx, si, di);
234
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");
238 return -EINVAL;
239 }
240
241 /* No reply available. This is okay. */
242 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_DORECV) == 0)
243 return 0;
244
245 reply_len = ebx;
246 reply = kzalloc(reply_len + 1, GFP_KERNEL);
247 if (!reply) {
248 DRM_ERROR("Cannot allocate memory for reply\n");
249 return -ENOMEM;
250 }
251
252
253 /* Receive buffer */
254 si = channel->cookie_high;
255 di = (uintptr_t) reply;
256 bp = channel->cookie_low;
257
258 VMW_PORT_HB_IN(
259 (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
260 reply_len, si, di,
261 VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
262 VMW_HYPERVISOR_MAGIC, bp,
263 eax, ebx, ecx, edx, si, di);
264
265 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) == 0) {
266 kfree(reply);
267
268 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
269 /* A checkpoint occurred. Retry. */
270 continue;
271 }
272
273 return -EINVAL;
274 }
275
276 reply[reply_len] = '\0';
277
278
279 /* Ack buffer */
280 si = channel->cookie_high;
281 di = channel->cookie_low;
282
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);
288
289 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
290 kfree(reply);
291
292 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) {
293 /* A checkpoint occurred. Retry. */
294 continue;
295 }
296
297 return -EINVAL;
298 }
299
300 break;
301 }
302
303 if (retries == RETRIES)
304 return -EINVAL;
305
306 *msg_len = reply_len;
307 *msg = reply;
308
309 return 0;
310}
311STACK_FRAME_NON_STANDARD(vmw_recv_msg);
312
313
314/**
315 * vmw_host_get_guestinfo: Gets a GuestInfo parameter
316 *
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.
319 *
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
323 *
324 * Returns: 0 on success
325 */
326int vmw_host_get_guestinfo(const char *guest_info_param,
327 char *buffer, size_t *length)
328{
329 struct rpc_channel channel;
330 char *msg, *reply = NULL;
331 size_t reply_len = 0;
332
333 if (!vmw_msg_enabled)
334 return -ENODEV;
335
336 if (!guest_info_param || !length)
337 return -EINVAL;
338
339 msg = kasprintf(GFP_KERNEL, "info-get %s", guest_info_param);
340 if (!msg) {
341 DRM_ERROR("Cannot allocate memory to get %s", guest_info_param);
342 return -ENOMEM;
343 }
344
345 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
346 goto out_open;
347
348 if (vmw_send_msg(&channel, msg) ||
349 vmw_recv_msg(&channel, (void *) &reply, &reply_len))
350 goto out_msg;
351
352 vmw_close_channel(&channel);
353 if (buffer && reply && reply_len > 0) {
354 /* Remove reply code, which are the first 2 characters of
355 * the reply
356 */
357 reply_len = max(reply_len - 2, (size_t) 0);
358 reply_len = min(reply_len, *length);
359
360 if (reply_len > 0)
361 memcpy(buffer, reply + 2, reply_len);
362 }
363
364 *length = reply_len;
365
366 kfree(reply);
367 kfree(msg);
368
369 return 0;
370
371out_msg:
372 vmw_close_channel(&channel);
373 kfree(reply);
374out_open:
375 *length = 0;
376 kfree(msg);
377 DRM_ERROR("Failed to get %s", guest_info_param);
378
379 return -EINVAL;
380}
381
382
383
384/**
385 * vmw_host_log: Sends a log message to the host
386 *
387 * @log: NULL terminated string
388 *
389 * Returns: 0 on success
390 */
391int vmw_host_log(const char *log)
392{
393 struct rpc_channel channel;
394 char *msg;
395 int ret = 0;
396
397
398 if (!vmw_msg_enabled)
399 return -ENODEV;
400
401 if (!log)
402 return ret;
403
404 msg = kasprintf(GFP_KERNEL, "log %s", log);
405 if (!msg) {
406 DRM_ERROR("Cannot allocate memory for log message\n");
407 return -ENOMEM;
408 }
409
410 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
411 goto out_open;
412
413 if (vmw_send_msg(&channel, msg))
414 goto out_msg;
415
416 vmw_close_channel(&channel);
417 kfree(msg);
418
419 return 0;
420
421out_msg:
422 vmw_close_channel(&channel);
423out_open:
424 kfree(msg);
425 DRM_ERROR("Failed to send log\n");
426
427 return -EINVAL;
428}