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 <asm/hypervisor.h>
32 #include "drmP.h"
33 #include "vmwgfx_msg.h"
34 
35 
36 #define MESSAGE_STATUS_SUCCESS  0x0001
37 #define MESSAGE_STATUS_DORECV   0x0002
38 #define MESSAGE_STATUS_CPT      0x0010
39 #define MESSAGE_STATUS_HB       0x0080
40 
41 #define RPCI_PROTOCOL_NUM       0x49435052
42 #define GUESTMSG_FLAG_COOKIE    0x80000000
43 
44 #define RETRIES                 3
45 
46 #define VMW_HYPERVISOR_MAGIC    0x564D5868
47 #define VMW_HYPERVISOR_PORT     0x5658
48 #define VMW_HYPERVISOR_HB_PORT  0x5659
49 
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)
57 
58 #define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)
59 
60 static u32 vmw_msg_enabled = 1;
61 
62 enum rpc_msg_type {
63 	MSG_TYPE_OPEN,
64 	MSG_TYPE_SENDSIZE,
65 	MSG_TYPE_SENDPAYLOAD,
66 	MSG_TYPE_RECVSIZE,
67 	MSG_TYPE_RECVPAYLOAD,
68 	MSG_TYPE_RECVSTATUS,
69 	MSG_TYPE_CLOSE,
70 };
71 
72 struct rpc_channel {
73 	u16 channel_id;
74 	u32 cookie_high;
75 	u32 cookie_low;
76 };
77 
78 
79 
80 /**
81  * vmw_open_channel
82  *
83  * @channel: RPC channel
84  * @protocol:
85  *
86  * Returns: 0 on success
87  */
88 static int vmw_open_channel(struct rpc_channel *channel, unsigned int protocol)
89 {
90 	unsigned long eax, ebx, ecx, edx, si = 0, di = 0;
91 
92 	VMW_PORT(VMW_PORT_CMD_OPEN_CHANNEL,
93 		(protocol | GUESTMSG_FLAG_COOKIE), si, di,
94 		VMW_HYPERVISOR_PORT,
95 		VMW_HYPERVISOR_MAGIC,
96 		eax, ebx, ecx, edx, si, di);
97 
98 	if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
99 		return -EINVAL;
100 
101 	channel->channel_id  = HIGH_WORD(edx);
102 	channel->cookie_high = si;
103 	channel->cookie_low  = di;
104 
105 	return 0;
106 }
107 
108 
109 
110 /**
111  * vmw_close_channel
112  *
113  * @channel: RPC channel
114  *
115  * Returns: 0 on success
116  */
117 static int vmw_close_channel(struct rpc_channel *channel)
118 {
119 	unsigned long eax, ebx, ecx, edx, si, di;
120 
121 	/* Set up additional parameters */
122 	si  = channel->cookie_high;
123 	di  = channel->cookie_low;
124 
125 	VMW_PORT(VMW_PORT_CMD_CLOSE_CHANNEL,
126 		0, si, di,
127 		(VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
128 		VMW_HYPERVISOR_MAGIC,
129 		eax, ebx, ecx, edx, si, di);
130 
131 	if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
132 		return -EINVAL;
133 
134 	return 0;
135 }
136 
137 
138 
139 /**
140  * vmw_send_msg: Sends a message to the host
141  *
142  * @channel: RPC channel
143  * @logmsg: NULL terminated string
144  *
145  * Returns: 0 on success
146  */
147 static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
148 {
149 	unsigned long eax, ebx, ecx, edx, si, di, bp;
150 	size_t msg_len = strlen(msg);
151 	int retries = 0;
152 
153 
154 	while (retries < RETRIES) {
155 		retries++;
156 
157 		/* Set up additional parameters */
158 		si  = channel->cookie_high;
159 		di  = channel->cookie_low;
160 
161 		VMW_PORT(VMW_PORT_CMD_SENDSIZE,
162 			msg_len, si, di,
163 			VMW_HYPERVISOR_PORT | (channel->channel_id << 16),
164 			VMW_HYPERVISOR_MAGIC,
165 			eax, ebx, ecx, edx, si, di);
166 
167 		if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0 ||
168 		    (HIGH_WORD(ecx) & MESSAGE_STATUS_HB) == 0) {
169 			/* Expected success + high-bandwidth. Give up. */
170 			return -EINVAL;
171 		}
172 
173 		/* Send msg */
174 		si  = (uintptr_t) msg;
175 		di  = channel->cookie_low;
176 		bp  = channel->cookie_high;
177 
178 		VMW_PORT_HB_OUT(
179 			(MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
180 			msg_len, si, di,
181 			VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
182 			VMW_HYPERVISOR_MAGIC, bp,
183 			eax, ebx, ecx, edx, si, di);
184 
185 		if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) != 0) {
186 			return 0;
187 		} else if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
188 			/* A checkpoint occurred. Retry. */
189 			continue;
190 		} else {
191 			break;
192 		}
193 	}
194 
195 	return -EINVAL;
196 }
197 
198 
199 
200 /**
201  * vmw_recv_msg: Receives a message from the host
202  *
203  * Note:  It is the caller's responsibility to call kfree() on msg.
204  *
205  * @channel:  channel opened by vmw_open_channel
206  * @msg:  [OUT] message received from the host
207  * @msg_len: message length
208  */
209 static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
210 			size_t *msg_len)
211 {
212 	unsigned long eax, ebx, ecx, edx, si, di, bp;
213 	char *reply;
214 	size_t reply_len;
215 	int retries = 0;
216 
217 
218 	*msg_len = 0;
219 	*msg = NULL;
220 
221 	while (retries < RETRIES) {
222 		retries++;
223 
224 		/* Set up additional parameters */
225 		si  = channel->cookie_high;
226 		di  = channel->cookie_low;
227 
228 		VMW_PORT(VMW_PORT_CMD_RECVSIZE,
229 			0, si, di,
230 			(VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
231 			VMW_HYPERVISOR_MAGIC,
232 			eax, ebx, ecx, edx, si, di);
233 
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");
237 			return -EINVAL;
238 		}
239 
240 		/* No reply available.  This is okay. */
241 		if ((HIGH_WORD(ecx) & MESSAGE_STATUS_DORECV) == 0)
242 			return 0;
243 
244 		reply_len = ebx;
245 		reply     = kzalloc(reply_len + 1, GFP_KERNEL);
246 		if (reply == NULL) {
247 			DRM_ERROR("Cannot allocate memory for reply\n");
248 			return -ENOMEM;
249 		}
250 
251 
252 		/* Receive buffer */
253 		si  = channel->cookie_high;
254 		di  = (uintptr_t) reply;
255 		bp  = channel->cookie_low;
256 
257 		VMW_PORT_HB_IN(
258 			(MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
259 			reply_len, si, di,
260 			VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
261 			VMW_HYPERVISOR_MAGIC, bp,
262 			eax, ebx, ecx, edx, si, di);
263 
264 		if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) == 0) {
265 			kfree(reply);
266 
267 			if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
268 				/* A checkpoint occurred. Retry. */
269 				continue;
270 			}
271 
272 			return -EINVAL;
273 		}
274 
275 		reply[reply_len] = '\0';
276 
277 
278 		/* Ack buffer */
279 		si  = channel->cookie_high;
280 		di  = channel->cookie_low;
281 
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);
287 
288 		if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
289 			kfree(reply);
290 
291 			if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) {
292 				/* A checkpoint occurred. Retry. */
293 				continue;
294 			}
295 
296 			return -EINVAL;
297 		}
298 
299 		break;
300 	}
301 
302 	*msg_len = reply_len;
303 	*msg     = reply;
304 
305 	return 0;
306 }
307 
308 
309 /**
310  * vmw_host_get_guestinfo: Gets a GuestInfo parameter
311  *
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.
314  *
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
318  *
319  * Returns: 0 on success
320  */
321 int vmw_host_get_guestinfo(const char *guest_info_param,
322 			   char *buffer, size_t *length)
323 {
324 	struct rpc_channel channel;
325 	char *msg, *reply = NULL;
326 	size_t msg_len, reply_len = 0;
327 	int ret = 0;
328 
329 
330 	if (!vmw_msg_enabled)
331 		return -ENODEV;
332 
333 	if (!guest_info_param || !length)
334 		return -EINVAL;
335 
336 	msg_len = strlen(guest_info_param) + strlen("info-get ") + 1;
337 	msg = kzalloc(msg_len, GFP_KERNEL);
338 	if (msg == NULL) {
339 		DRM_ERROR("Cannot allocate memory to get %s", guest_info_param);
340 		return -ENOMEM;
341 	}
342 
343 	sprintf(msg, "info-get %s", guest_info_param);
344 
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);
350 
351 		ret = -EINVAL;
352 	}
353 
354 	if (buffer && reply && reply_len > 0) {
355 		/* Remove reply code, which are the first 2 characters of
356 		 * the reply
357 		 */
358 		reply_len = max(reply_len - 2, (size_t) 0);
359 		reply_len = min(reply_len, *length);
360 
361 		if (reply_len > 0)
362 			memcpy(buffer, reply + 2, reply_len);
363 	}
364 
365 	*length = reply_len;
366 
367 	kfree(reply);
368 	kfree(msg);
369 
370 	return ret;
371 }
372 
373 
374 
375 /**
376  * vmw_host_log: Sends a log message to the host
377  *
378  * @log: NULL terminated string
379  *
380  * Returns: 0 on success
381  */
382 int vmw_host_log(const char *log)
383 {
384 	struct rpc_channel channel;
385 	char *msg;
386 	int msg_len;
387 	int ret = 0;
388 
389 
390 	if (!vmw_msg_enabled)
391 		return -ENODEV;
392 
393 	if (!log)
394 		return ret;
395 
396 	msg_len = strlen(log) + strlen("log ") + 1;
397 	msg = kzalloc(msg_len, GFP_KERNEL);
398 	if (msg == NULL) {
399 		DRM_ERROR("Cannot allocate memory for log message\n");
400 		return -ENOMEM;
401 	}
402 
403 	sprintf(msg, "log %s", log);
404 
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");
409 
410 		ret = -EINVAL;
411 	}
412 
413 	kfree(msg);
414 
415 	return ret;
416 }
417