Input: xpad - add support for Xbox1 PDP Camo series gamepad
[linux/fpc-iii.git] / drivers / gpu / drm / vmwgfx / vmwgfx_msg.c
blobe57a0bad7a626daf505625ce019f2583218a51ab
1 /*
2 * Copyright © 2016 VMware, Inc., Palo Alto, CA., USA
3 * All Rights Reserved.
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
15 * of the Software.
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>
33 #include "drmP.h"
34 #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
45 #define RETRIES 3
47 #define VMW_HYPERVISOR_MAGIC 0x564D5868
48 #define VMW_HYPERVISOR_PORT 0x5658
49 #define VMW_HYPERVISOR_HB_PORT 0x5659
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)
59 #define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)
61 static u32 vmw_msg_enabled = 1;
63 enum 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,
73 struct rpc_channel {
74 u16 channel_id;
75 u32 cookie_high;
76 u32 cookie_low;
81 /**
82 * vmw_open_channel
84 * @channel: RPC channel
85 * @protocol:
87 * Returns: 0 on success
89 static int vmw_open_channel(struct rpc_channel *channel, unsigned int protocol)
91 unsigned long eax, ebx, ecx, edx, si = 0, di = 0;
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);
99 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
100 return -EINVAL;
102 channel->channel_id = HIGH_WORD(edx);
103 channel->cookie_high = si;
104 channel->cookie_low = di;
106 return 0;
112 * vmw_close_channel
114 * @channel: RPC channel
116 * Returns: 0 on success
118 static int vmw_close_channel(struct rpc_channel *channel)
120 unsigned long eax, ebx, ecx, edx, si, di;
122 /* Set up additional parameters */
123 si = channel->cookie_high;
124 di = channel->cookie_low;
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);
132 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
133 return -EINVAL;
135 return 0;
141 * vmw_send_msg: Sends a message to the host
143 * @channel: RPC channel
144 * @logmsg: NULL terminated string
146 * Returns: 0 on success
148 static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
150 unsigned long eax, ebx, ecx, edx, si, di, bp;
151 size_t msg_len = strlen(msg);
152 int retries = 0;
155 while (retries < RETRIES) {
156 retries++;
158 /* Set up additional parameters */
159 si = channel->cookie_high;
160 di = channel->cookie_low;
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);
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;
174 /* Send msg */
175 si = (uintptr_t) msg;
176 di = channel->cookie_low;
177 bp = channel->cookie_high;
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);
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;
196 return -EINVAL;
198 STACK_FRAME_NON_STANDARD(vmw_send_msg);
202 * vmw_recv_msg: Receives a message from the host
204 * Note: It is the caller's responsibility to call kfree() on msg.
206 * @channel: channel opened by vmw_open_channel
207 * @msg: [OUT] message received from the host
208 * @msg_len: message length
210 static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
211 size_t *msg_len)
213 unsigned long eax, ebx, ecx, edx, si, di, bp;
214 char *reply;
215 size_t reply_len;
216 int retries = 0;
219 *msg_len = 0;
220 *msg = NULL;
222 while (retries < RETRIES) {
223 retries++;
225 /* Set up additional parameters */
226 si = channel->cookie_high;
227 di = channel->cookie_low;
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);
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;
241 /* No reply available. This is okay. */
242 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_DORECV) == 0)
243 return 0;
245 reply_len = ebx;
246 reply = kzalloc(reply_len + 1, GFP_KERNEL);
247 if (reply == NULL) {
248 DRM_ERROR("Cannot allocate memory for reply\n");
249 return -ENOMEM;
253 /* Receive buffer */
254 si = channel->cookie_high;
255 di = (uintptr_t) reply;
256 bp = channel->cookie_low;
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);
265 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) == 0) {
266 kfree(reply);
268 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
269 /* A checkpoint occurred. Retry. */
270 continue;
273 return -EINVAL;
276 reply[reply_len] = '\0';
279 /* Ack buffer */
280 si = channel->cookie_high;
281 di = channel->cookie_low;
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);
289 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
290 kfree(reply);
292 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) {
293 /* A checkpoint occurred. Retry. */
294 continue;
297 return -EINVAL;
300 break;
303 if (retries == RETRIES)
304 return -EINVAL;
306 *msg_len = reply_len;
307 *msg = reply;
309 return 0;
311 STACK_FRAME_NON_STANDARD(vmw_recv_msg);
315 * vmw_host_get_guestinfo: Gets a GuestInfo parameter
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.
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
324 * Returns: 0 on success
326 int vmw_host_get_guestinfo(const char *guest_info_param,
327 char *buffer, size_t *length)
329 struct rpc_channel channel;
330 char *msg, *reply = NULL;
331 size_t msg_len, reply_len = 0;
332 int ret = 0;
335 if (!vmw_msg_enabled)
336 return -ENODEV;
338 if (!guest_info_param || !length)
339 return -EINVAL;
341 msg_len = strlen(guest_info_param) + strlen("info-get ") + 1;
342 msg = kzalloc(msg_len, GFP_KERNEL);
343 if (msg == NULL) {
344 DRM_ERROR("Cannot allocate memory to get %s", guest_info_param);
345 return -ENOMEM;
348 sprintf(msg, "info-get %s", guest_info_param);
350 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM) ||
351 vmw_send_msg(&channel, msg) ||
352 vmw_recv_msg(&channel, (void *) &reply, &reply_len) ||
353 vmw_close_channel(&channel)) {
354 DRM_ERROR("Failed to get %s", guest_info_param);
356 ret = -EINVAL;
359 if (buffer && reply && reply_len > 0) {
360 /* Remove reply code, which are the first 2 characters of
361 * the reply
363 reply_len = max(reply_len - 2, (size_t) 0);
364 reply_len = min(reply_len, *length);
366 if (reply_len > 0)
367 memcpy(buffer, reply + 2, reply_len);
370 *length = reply_len;
372 kfree(reply);
373 kfree(msg);
375 return ret;
381 * vmw_host_log: Sends a log message to the host
383 * @log: NULL terminated string
385 * Returns: 0 on success
387 int vmw_host_log(const char *log)
389 struct rpc_channel channel;
390 char *msg;
391 int msg_len;
392 int ret = 0;
395 if (!vmw_msg_enabled)
396 return -ENODEV;
398 if (!log)
399 return ret;
401 msg_len = strlen(log) + strlen("log ") + 1;
402 msg = kzalloc(msg_len, GFP_KERNEL);
403 if (msg == NULL) {
404 DRM_ERROR("Cannot allocate memory for log message\n");
405 return -ENOMEM;
408 sprintf(msg, "log %s", log);
410 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM) ||
411 vmw_send_msg(&channel, msg) ||
412 vmw_close_channel(&channel)) {
413 DRM_ERROR("Failed to send log\n");
415 ret = -EINVAL;
418 kfree(msg);
420 return ret;