Merge tag 'ovl-fixes-5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs
[linux-2.6-microblaze.git] / drivers / gpu / drm / vmwgfx / vmwgfx_msg.c
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
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_drv.h"
35 #include "vmwgfx_msg.h"
36
37
38 #define MESSAGE_STATUS_SUCCESS  0x0001
39 #define MESSAGE_STATUS_DORECV   0x0002
40 #define MESSAGE_STATUS_CPT      0x0010
41 #define MESSAGE_STATUS_HB       0x0080
42
43 #define RPCI_PROTOCOL_NUM       0x49435052
44 #define GUESTMSG_FLAG_COOKIE    0x80000000
45
46 #define RETRIES                 3
47
48 #define VMW_HYPERVISOR_MAGIC    0x564D5868
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                 0,
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                 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  * vmw_port_hb_out - Send the message payload either through the
139  * high-bandwidth port if available, or through the backdoor otherwise.
140  * @channel: The rpc channel.
141  * @msg: NULL-terminated message.
142  * @hb: Whether the high-bandwidth port is available.
143  *
144  * Return: The port status.
145  */
146 static unsigned long vmw_port_hb_out(struct rpc_channel *channel,
147                                      const char *msg, bool hb)
148 {
149         unsigned long si, di, eax, ebx, ecx, edx;
150         unsigned long msg_len = strlen(msg);
151
152         if (hb) {
153                 unsigned long bp = channel->cookie_high;
154
155                 si = (uintptr_t) msg;
156                 di = channel->cookie_low;
157
158                 VMW_PORT_HB_OUT(
159                         (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
160                         msg_len, si, di,
161                         VMWARE_HYPERVISOR_HB | (channel->channel_id << 16) |
162                         VMWARE_HYPERVISOR_OUT,
163                         VMW_HYPERVISOR_MAGIC, bp,
164                         eax, ebx, ecx, edx, si, di);
165
166                 return ebx;
167         }
168
169         /* HB port not available. Send the message 4 bytes at a time. */
170         ecx = MESSAGE_STATUS_SUCCESS << 16;
171         while (msg_len && (HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS)) {
172                 unsigned int bytes = min_t(size_t, msg_len, 4);
173                 unsigned long word = 0;
174
175                 memcpy(&word, msg, bytes);
176                 msg_len -= bytes;
177                 msg += bytes;
178                 si = channel->cookie_high;
179                 di = channel->cookie_low;
180
181                 VMW_PORT(VMW_PORT_CMD_MSG | (MSG_TYPE_SENDPAYLOAD << 16),
182                          word, si, di,
183                          channel->channel_id << 16,
184                          VMW_HYPERVISOR_MAGIC,
185                          eax, ebx, ecx, edx, si, di);
186         }
187
188         return ecx;
189 }
190
191 /**
192  * vmw_port_hb_in - Receive the message payload either through the
193  * high-bandwidth port if available, or through the backdoor otherwise.
194  * @channel: The rpc channel.
195  * @reply: Pointer to buffer holding reply.
196  * @reply_len: Length of the reply.
197  * @hb: Whether the high-bandwidth port is available.
198  *
199  * Return: The port status.
200  */
201 static unsigned long vmw_port_hb_in(struct rpc_channel *channel, char *reply,
202                                     unsigned long reply_len, bool hb)
203 {
204         unsigned long si, di, eax, ebx, ecx, edx;
205
206         if (hb) {
207                 unsigned long bp = channel->cookie_low;
208
209                 si = channel->cookie_high;
210                 di = (uintptr_t) reply;
211
212                 VMW_PORT_HB_IN(
213                         (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
214                         reply_len, si, di,
215                         VMWARE_HYPERVISOR_HB | (channel->channel_id << 16),
216                         VMW_HYPERVISOR_MAGIC, bp,
217                         eax, ebx, ecx, edx, si, di);
218
219                 return ebx;
220         }
221
222         /* HB port not available. Retrieve the message 4 bytes at a time. */
223         ecx = MESSAGE_STATUS_SUCCESS << 16;
224         while (reply_len) {
225                 unsigned int bytes = min_t(unsigned long, reply_len, 4);
226
227                 si = channel->cookie_high;
228                 di = channel->cookie_low;
229
230                 VMW_PORT(VMW_PORT_CMD_MSG | (MSG_TYPE_RECVPAYLOAD << 16),
231                          MESSAGE_STATUS_SUCCESS, si, di,
232                          channel->channel_id << 16,
233                          VMW_HYPERVISOR_MAGIC,
234                          eax, ebx, ecx, edx, si, di);
235
236                 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
237                         break;
238
239                 memcpy(reply, &ebx, bytes);
240                 reply_len -= bytes;
241                 reply += bytes;
242         }
243
244         return ecx;
245 }
246
247
248 /**
249  * vmw_send_msg: Sends a message to the host
250  *
251  * @channel: RPC channel
252  * @logmsg: NULL terminated string
253  *
254  * Returns: 0 on success
255  */
256 static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
257 {
258         unsigned long eax, ebx, ecx, edx, si, di;
259         size_t msg_len = strlen(msg);
260         int retries = 0;
261
262         while (retries < RETRIES) {
263                 retries++;
264
265                 /* Set up additional parameters */
266                 si  = channel->cookie_high;
267                 di  = channel->cookie_low;
268
269                 VMW_PORT(VMW_PORT_CMD_SENDSIZE,
270                         msg_len, si, di,
271                         channel->channel_id << 16,
272                         VMW_HYPERVISOR_MAGIC,
273                         eax, ebx, ecx, edx, si, di);
274
275                 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
276                         /* Expected success. Give up. */
277                         return -EINVAL;
278                 }
279
280                 /* Send msg */
281                 ebx = vmw_port_hb_out(channel, msg,
282                                       !!(HIGH_WORD(ecx) & MESSAGE_STATUS_HB));
283
284                 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) != 0) {
285                         return 0;
286                 } else if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
287                         /* A checkpoint occurred. Retry. */
288                         continue;
289                 } else {
290                         break;
291                 }
292         }
293
294         return -EINVAL;
295 }
296 STACK_FRAME_NON_STANDARD(vmw_send_msg);
297
298
299 /**
300  * vmw_recv_msg: Receives a message from the host
301  *
302  * Note:  It is the caller's responsibility to call kfree() on msg.
303  *
304  * @channel:  channel opened by vmw_open_channel
305  * @msg:  [OUT] message received from the host
306  * @msg_len: message length
307  */
308 static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
309                         size_t *msg_len)
310 {
311         unsigned long eax, ebx, ecx, edx, si, di;
312         char *reply;
313         size_t reply_len;
314         int retries = 0;
315
316
317         *msg_len = 0;
318         *msg = NULL;
319
320         while (retries < RETRIES) {
321                 retries++;
322
323                 /* Set up additional parameters */
324                 si  = channel->cookie_high;
325                 di  = channel->cookie_low;
326
327                 VMW_PORT(VMW_PORT_CMD_RECVSIZE,
328                         0, si, di,
329                         channel->channel_id << 16,
330                         VMW_HYPERVISOR_MAGIC,
331                         eax, ebx, ecx, edx, si, di);
332
333                 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
334                         DRM_ERROR("Failed to get reply size for host message.\n");
335                         return -EINVAL;
336                 }
337
338                 /* No reply available.  This is okay. */
339                 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_DORECV) == 0)
340                         return 0;
341
342                 reply_len = ebx;
343                 reply     = kzalloc(reply_len + 1, GFP_KERNEL);
344                 if (!reply) {
345                         DRM_ERROR("Cannot allocate memory for host message reply.\n");
346                         return -ENOMEM;
347                 }
348
349
350                 /* Receive buffer */
351                 ebx = vmw_port_hb_in(channel, reply, reply_len,
352                                      !!(HIGH_WORD(ecx) & MESSAGE_STATUS_HB));
353                 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) == 0) {
354                         kfree(reply);
355                         reply = NULL;
356                         if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
357                                 /* A checkpoint occurred. Retry. */
358                                 continue;
359                         }
360
361                         return -EINVAL;
362                 }
363
364                 reply[reply_len] = '\0';
365
366
367                 /* Ack buffer */
368                 si  = channel->cookie_high;
369                 di  = channel->cookie_low;
370
371                 VMW_PORT(VMW_PORT_CMD_RECVSTATUS,
372                         MESSAGE_STATUS_SUCCESS, si, di,
373                         channel->channel_id << 16,
374                         VMW_HYPERVISOR_MAGIC,
375                         eax, ebx, ecx, edx, si, di);
376
377                 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
378                         kfree(reply);
379                         reply = NULL;
380                         if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) {
381                                 /* A checkpoint occurred. Retry. */
382                                 continue;
383                         }
384
385                         return -EINVAL;
386                 }
387
388                 break;
389         }
390
391         if (!reply)
392                 return -EINVAL;
393
394         *msg_len = reply_len;
395         *msg     = reply;
396
397         return 0;
398 }
399 STACK_FRAME_NON_STANDARD(vmw_recv_msg);
400
401
402 /**
403  * vmw_host_get_guestinfo: Gets a GuestInfo parameter
404  *
405  * Gets the value of a  GuestInfo.* parameter.  The value returned will be in
406  * a string, and it is up to the caller to post-process.
407  *
408  * @guest_info_param:  Parameter to get, e.g. GuestInfo.svga.gl3
409  * @buffer: if NULL, *reply_len will contain reply size.
410  * @length: size of the reply_buf.  Set to size of reply upon return
411  *
412  * Returns: 0 on success
413  */
414 int vmw_host_get_guestinfo(const char *guest_info_param,
415                            char *buffer, size_t *length)
416 {
417         struct rpc_channel channel;
418         char *msg, *reply = NULL;
419         size_t reply_len = 0;
420
421         if (!vmw_msg_enabled)
422                 return -ENODEV;
423
424         if (!guest_info_param || !length)
425                 return -EINVAL;
426
427         msg = kasprintf(GFP_KERNEL, "info-get %s", guest_info_param);
428         if (!msg) {
429                 DRM_ERROR("Cannot allocate memory to get guest info \"%s\".",
430                           guest_info_param);
431                 return -ENOMEM;
432         }
433
434         if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
435                 goto out_open;
436
437         if (vmw_send_msg(&channel, msg) ||
438             vmw_recv_msg(&channel, (void *) &reply, &reply_len))
439                 goto out_msg;
440
441         vmw_close_channel(&channel);
442         if (buffer && reply && reply_len > 0) {
443                 /* Remove reply code, which are the first 2 characters of
444                  * the reply
445                  */
446                 reply_len = max(reply_len - 2, (size_t) 0);
447                 reply_len = min(reply_len, *length);
448
449                 if (reply_len > 0)
450                         memcpy(buffer, reply + 2, reply_len);
451         }
452
453         *length = reply_len;
454
455         kfree(reply);
456         kfree(msg);
457
458         return 0;
459
460 out_msg:
461         vmw_close_channel(&channel);
462         kfree(reply);
463 out_open:
464         *length = 0;
465         kfree(msg);
466         DRM_ERROR("Failed to get guest info \"%s\".", guest_info_param);
467
468         return -EINVAL;
469 }
470
471
472
473 /**
474  * vmw_host_log: Sends a log message to the host
475  *
476  * @log: NULL terminated string
477  *
478  * Returns: 0 on success
479  */
480 int vmw_host_log(const char *log)
481 {
482         struct rpc_channel channel;
483         char *msg;
484         int ret = 0;
485
486
487         if (!vmw_msg_enabled)
488                 return -ENODEV;
489
490         if (!log)
491                 return ret;
492
493         msg = kasprintf(GFP_KERNEL, "log %s", log);
494         if (!msg) {
495                 DRM_ERROR("Cannot allocate memory for host log message.\n");
496                 return -ENOMEM;
497         }
498
499         if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
500                 goto out_open;
501
502         if (vmw_send_msg(&channel, msg))
503                 goto out_msg;
504
505         vmw_close_channel(&channel);
506         kfree(msg);
507
508         return 0;
509
510 out_msg:
511         vmw_close_channel(&channel);
512 out_open:
513         kfree(msg);
514         DRM_ERROR("Failed to send host log message.\n");
515
516         return -EINVAL;
517 }