aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/svga/svga_msg.c
blob: 3bbe89f5f6b96dd5d6926d50f12dfa04c6046d13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 * Copyright © 2016 VMware, Inc., Palo Alto, CA., USA
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include "util/u_math.h" /* for MAX2/MIN2 */
#include "util/u_debug.h"
#include "util/u_memory.h"
#include "util/u_string.h"
#include "pipe/p_defines.h"
#include "svga_msg.h"


#define MESSAGE_STATUS_SUCCESS  0x0001
#define MESSAGE_STATUS_DORECV   0x0002
#define MESSAGE_STATUS_CPT      0x0010
#define MESSAGE_STATUS_HB       0x0080

#define RPCI_PROTOCOL_NUM       0x49435052
#define GUESTMSG_FLAG_COOKIE    0x80000000

#define RETRIES                 3

#define VMW_HYPERVISOR_MAGIC    0x564D5868
#define VMW_HYPERVISOR_PORT     0x5658
#define VMW_HYPERVISOR_HB_PORT  0x5659

#define VMW_PORT_CMD_MSG        30
#define VMW_PORT_CMD_HB_MSG     0
#define VMW_PORT_CMD_OPEN_CHANNEL  (MSG_TYPE_OPEN << 16 | VMW_PORT_CMD_MSG)
#define VMW_PORT_CMD_CLOSE_CHANNEL (MSG_TYPE_CLOSE << 16 | VMW_PORT_CMD_MSG)
#define VMW_PORT_CMD_SENDSIZE   (MSG_TYPE_SENDSIZE << 16 | VMW_PORT_CMD_MSG)
#define VMW_PORT_CMD_RECVSIZE   (MSG_TYPE_RECVSIZE << 16 | VMW_PORT_CMD_MSG)
#define VMW_PORT_CMD_RECVSTATUS (MSG_TYPE_RECVSTATUS << 16 | VMW_PORT_CMD_MSG)

#define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)


#if defined(PIPE_CC_GCC)

/**
 * Hypervisor-specific bi-directional communication channel.  Should never
 * execute on bare metal hardware.  The caller must make sure to check for
 * supported hypervisor before using these macros.
 *
 * The last two parameters are both input and output and must be initialized.
 *
 * @cmd: [IN] Message Cmd
 * @in_bx: [IN] Message Len, through BX
 * @in_si: [IN] Input argument through SI, set to 0 if not used
 * @in_di: [IN] Input argument through DI, set ot 0 if not used
 * @port_num: [IN] port number + [channel id]
 * @magic: [IN] hypervisor magic value
 * @ax: [OUT] value of AX register
 * @bx: [OUT] e.g. status from an HB message status command
 * @cx: [OUT] e.g. status from a non-HB message status command
 * @dx: [OUT] e.g. channel id
 * @si:  [OUT]
 * @di:  [OUT]
 */
#define VMW_PORT(cmd, in_bx, in_si, in_di, \
         port_num, magic,                  \
         ax, bx, cx, dx, si, di)           \
({                                         \
   asm volatile ("inl %%dx, %%eax;" :      \
      "=a"(ax),                            \
      "=b"(bx),                            \
      "=c"(cx),                            \
      "=d"(dx),                            \
      "=S"(si),                            \
      "=D"(di) :                           \
      "a"(magic),                          \
      "b"(in_bx),                          \
      "c"(cmd),                            \
      "d"(port_num),                       \
      "S"(in_si),                          \
      "D"(in_di) :                         \
      "memory");                           \
})



/**
 * Hypervisor-specific bi-directional communication channel.  Should never
 * execute on bare metal hardware.  The caller must make sure to check for
 * supported hypervisor before using these macros.
 *
 * @cmd: [IN] Message Cmd
 * @in_cx: [IN] Message Len, through CX
 * @in_si: [IN] Input argument through SI, set to 0 if not used
 * @in_di: [IN] Input argument through DI, set to 0 if not used
 * @port_num: [IN] port number + [channel id]
 * @magic: [IN] hypervisor magic value
 * @bp:  [IN]
 * @ax: [OUT] value of AX register
 * @bx: [OUT] e.g. status from an HB message status command
 * @cx: [OUT] e.g. status from a non-HB message status command
 * @dx: [OUT] e.g. channel id
 * @si:  [OUT]
 * @di:  [OUT]
 */
#if defined(PIPE_ARCH_X86_64)

typedef uint64_t VMW_REG;

#define VMW_PORT_HB_OUT(cmd, in_cx, in_si, in_di, \
         port_num, magic, bp,                     \
         ax, bx, cx, dx, si, di)                  \
({                                                \
   asm volatile ("push %%rbp;"                    \
      "movq %12, %%rbp;"                          \
      "rep outsb;"                                \
      "pop %%rbp;" :                              \
      "=a"(ax),                                   \
      "=b"(bx),                                   \
      "=c"(cx),                                   \
      "=d"(dx),                                   \
      "=S"(si),                                   \
      "=D"(di) :                                  \
      "a"(magic),                                 \
      "b"(cmd),                                   \
      "c"(in_cx),                                 \
      "d"(port_num),                              \
      "S"(in_si),                                 \
      "D"(in_di),                                 \
      "r"(bp) :                                   \
      "memory", "cc");                            \
})

#define VMW_PORT_HB_IN(cmd, in_cx, in_si, in_di,  \
         port_num, magic, bp,                     \
         ax, bx, cx, dx, si, di)                  \
({                                                \
   asm volatile ("push %%rbp;"                    \
      "movq %12, %%rbp;"                          \
      "rep insb;"                                 \
      "pop %%rbp" :                               \
      "=a"(ax),                                   \
      "=b"(bx),                                   \
      "=c"(cx),                                   \
      "=d"(dx),                                   \
      "=S"(si),                                   \
      "=D"(di) :                                  \
      "a"(magic),                                 \
      "b"(cmd),                                   \
      "c"(in_cx),                                 \
      "d"(port_num),                              \
      "S"(in_si),                                 \
      "D"(in_di),                                 \
      "r"(bp) :                                   \
      "memory", "cc");                            \
})

#else

typedef uint32_t VMW_REG;

/* In the 32-bit version of this macro, we use "m" because there is no
 * more register left for bp
 */
#define VMW_PORT_HB_OUT(cmd, in_cx, in_si, in_di, \
         port_num, magic, bp,                     \
         ax, bx, cx, dx, si, di)                  \
({                                                \
   asm volatile ("push %%ebp;"                    \
      "mov %12, %%ebp;"                           \
      "rep outsb;"                                \
      "pop %%ebp;" :                              \
      "=a"(ax),                                   \
      "=b"(bx),                                   \
      "=c"(cx),                                   \
      "=d"(dx),                                   \
      "=S"(si),                                   \
      "=D"(di) :                                  \
      "a"(magic),                                 \
      "b"(cmd),                                   \
      "c"(in_cx),                                 \
      "d"(port_num),                              \
      "S"(in_si),                                 \
      "D"(in_di),                                 \
      "m"(bp) :                                   \
      "memory", "cc");                            \
})


#define VMW_PORT_HB_IN(cmd, in_cx, in_si, in_di,  \
         port_num, magic, bp,                     \
         ax, bx, cx, dx, si, di)                  \
({                                                \
   asm volatile ("push %%ebp;"                    \
      "mov %12, %%ebp;"                           \
      "rep insb;"                                 \
      "pop %%ebp" :                               \
      "=a"(ax),                                   \
      "=b"(bx),                                   \
      "=c"(cx),                                   \
      "=d"(dx),                                   \
      "=S"(si),                                   \
      "=D"(di) :                                  \
      "a"(magic),                                 \
      "b"(cmd),                                   \
      "c"(in_cx),                                 \
      "d"(port_num),                              \
      "S"(in_si),                                 \
      "D"(in_di),                                 \
      "m"(bp) :                                   \
      "memory", "cc");                            \
})

#endif

#else

#define MSG_NOT_IMPLEMENTED 1

/* not implemented */

typedef uint32_t VMW_REG;


#define VMW_PORT(cmd, in_bx, in_si, in_di, \
         port_num, magic,                  \
         ax, bx, cx, dx, si, di)

#define VMW_PORT_HB_OUT(cmd, in_cx, in_si, in_di, \
         port_num, magic, bp,                     \
         ax, bx, cx, dx, si, di)

#define VMW_PORT_HB_IN(cmd, in_cx, in_si, in_di,  \
         port_num, magic, bp,                     \
         ax, bx, cx, dx, si, di)

#endif /* #if PIPE_CC_GCC */


enum rpc_msg_type {
   MSG_TYPE_OPEN,
   MSG_TYPE_SENDSIZE,
   MSG_TYPE_SENDPAYLOAD,
   MSG_TYPE_RECVSIZE,
   MSG_TYPE_RECVPAYLOAD,
   MSG_TYPE_RECVSTATUS,
   MSG_TYPE_CLOSE,
};

struct rpc_channel {
   uint16_t channel_id;
   uint32_t cookie_high;
   uint32_t cookie_low;
};



/**
 * svga_open_channel
 *
 * @channel: RPC channel
 * @protocol:
 *
 * Returns: PIPE_OK on success, PIPE_ERROR otherwise
 */
static enum pipe_error
svga_open_channel(struct rpc_channel *channel, unsigned protocol)
{
   VMW_REG ax, bx, cx, dx, si = 0, di = 0;

   VMW_PORT(VMW_PORT_CMD_OPEN_CHANNEL,
      (protocol | GUESTMSG_FLAG_COOKIE), si, di,
      VMW_HYPERVISOR_PORT,
      VMW_HYPERVISOR_MAGIC,
      ax, bx, cx, dx, si, di);

   if ((HIGH_WORD(cx) & MESSAGE_STATUS_SUCCESS) == 0)
      return PIPE_ERROR;

   channel->channel_id = HIGH_WORD(dx);
   channel->cookie_high = si;
   channel->cookie_low = di;

   return PIPE_OK;
}



/**
 * svga_close_channel
 *
 * @channel: RPC channel
 *
 * Returns: PIPE_OK on success, PIPE_ERROR otherwises
 */
static enum pipe_error
svga_close_channel(struct rpc_channel *channel)
{
   VMW_REG ax, bx, cx, dx, si, di;

   /* Set up additional parameters */
   si = channel->cookie_high;
   di = channel->cookie_low;

   VMW_PORT(VMW_PORT_CMD_CLOSE_CHANNEL,
      0, si, di,
      (VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
      VMW_HYPERVISOR_MAGIC,
      ax, bx, cx, dx, si, di);

   if ((HIGH_WORD(cx) & MESSAGE_STATUS_SUCCESS) == 0)
      return PIPE_ERROR;

   return PIPE_OK;
}



/**
 * svga_send_msg: Sends a message to the host
 *
 * @channel: RPC channel
 * @logmsg: NULL terminated string
 *
 * Returns: PIPE_OK on success
 */
static enum pipe_error
svga_send_msg(struct rpc_channel *channel, const char *msg)
{
   VMW_REG ax, bx, cx, dx, si, di, bp;
   size_t msg_len = strlen(msg);
   int retries = 0;


   while (retries < RETRIES) {
      retries++;

      /* Set up additional parameters */
      si = channel->cookie_high;
      di = channel->cookie_low;

      VMW_PORT(VMW_PORT_CMD_SENDSIZE,
         msg_len, si, di,
         VMW_HYPERVISOR_PORT | (channel->channel_id << 16),
         VMW_HYPERVISOR_MAGIC,
         ax, bx, cx, dx, si, di);

      if ((HIGH_WORD(cx) & MESSAGE_STATUS_SUCCESS) == 0 ||
          (HIGH_WORD(cx) & MESSAGE_STATUS_HB) == 0) {
         /* Expected success + high-bandwidth. Give up. */
         return PIPE_ERROR;
      }

      /* Send msg */
      si = (uintptr_t) msg;
      di = channel->cookie_low;
      bp = channel->cookie_high;

      VMW_PORT_HB_OUT(
         (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
         msg_len, si, di,
         VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
         VMW_HYPERVISOR_MAGIC, bp,
         ax, bx, cx, dx, si, di);

      if ((HIGH_WORD(bx) & MESSAGE_STATUS_SUCCESS) != 0) {
         return PIPE_OK;
      } else if ((HIGH_WORD(bx) & MESSAGE_STATUS_CPT) != 0) {
         /* A checkpoint occurred. Retry. */
         continue;
      } else {
         break;
      }
   }

   return PIPE_ERROR;
}



/**
 * svga_host_log: Sends a log message to the host
 *
 * @log: NULL terminated string
 *
 * Returns: PIPE_OK on success
 */
enum pipe_error
svga_host_log(const char *log)
{
   struct rpc_channel channel;
   char *msg;
   int msg_len;
   enum pipe_error ret = PIPE_OK;

#ifdef MSG_NOT_IMPLEMENTED
   return ret;
#endif

   if (!log)
      return ret;

   msg_len = strlen(log) + strlen("log ") + 1;
   msg = CALLOC(1, msg_len);
   if (msg == NULL) {
      debug_printf("Cannot allocate memory for log message\n");
      return PIPE_ERROR_OUT_OF_MEMORY;
   }

   util_sprintf(msg, "log %s", log);

   if (svga_open_channel(&channel, RPCI_PROTOCOL_NUM) ||
       svga_send_msg(&channel, msg) ||
       svga_close_channel(&channel)) {
      debug_printf("Failed to send log\n");

      ret = PIPE_ERROR;
   }

   FREE(msg);

   return ret;
}