vtest_renderer.c 18.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/**************************************************************************
 *
 * Copyright (C) 2015 Red Hat Inc.
 *
 * 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, sublicense,
 * 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 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 *
 **************************************************************************/
24
#include <stdlib.h>
25
#include <stdio.h>
Dave Airlie's avatar
Dave Airlie committed
26
#include <string.h>
27
#include <unistd.h>
28
#include <fcntl.h>
29
#include <limits.h>
30

31 32
#include "virglrenderer.h"

33 34
#include <sys/uio.h>
#include "vtest.h"
Dave Airlie's avatar
Dave Airlie committed
35
#include "vtest_protocol.h"
36
#include "util.h"
Gert Wollny's avatar
Gert Wollny committed
37
#include "util/u_debug.h"
38
#include "util/u_math.h"
39 40
#include "util/u_memory.h"
#include "util/u_hash_table.h"
Dave Airlie's avatar
Dave Airlie committed
41 42

static int ctx_id = 1;
43 44 45
static int fence_id = 1;

static int last_fence;
Gert Wollny's avatar
Gert Wollny committed
46
static void vtest_write_fence(UNUSED void *cookie, uint32_t fence_id_in)
47
{
48
  last_fence = fence_id_in;
49 50
}

51 52
struct virgl_renderer_callbacks vtest_cbs = {
    .version = 1,
53
    .write_fence = vtest_write_fence,
54 55 56
};

struct vtest_renderer {
57 58
  int in_fd;
  int out_fd;
59
  unsigned protocol_version;
60
  struct util_hash_table *iovec_hash;
61 62 63 64
};

struct vtest_renderer renderer;

65 66 67 68 69
struct virgl_box {
	uint32_t x, y, z;
	uint32_t w, h, d;
};

70 71 72 73 74 75 76 77 78 79
static int
__failed_call(const char* func, const char *called, int ret)
{
  fprintf(stderr, "%s called %s which failed (%d)\n", func, called, ret);
  return ret;
}

#define report_failed_call(called, ret) \
  __failed_call(__FUNCTION__, called, ret)

80 81 82 83 84 85 86 87 88 89
static int
__failure(const char* func, const char *reason, int ret)
{
  fprintf(stderr, "%s %s (%d)\n", func, reason, ret);
  return ret;
}

#define report_failure(reason, ret) \
  __failure(__FUNCTION__, reason, ret)

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
static unsigned
hash_func(void *key)
{
   intptr_t ip = pointer_to_intptr(key);
   return (unsigned)(ip & 0xffffffff);
}

static int
compare_iovecs(void *key1, void *key2)
{
   if (key1 < key2)
      return -1;
   if (key1 > key2)
      return 1;
   else
      return 0;
}

static void free_iovec(void *value)
{
   struct iovec *iovec = value;

   free(iovec->iov_base);
   free(iovec);
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
static int vtest_block_write(int fd, void *buf, int size)
{
   void *ptr = buf;
   int left;
   int ret;
   left = size;
   do {
      ret = write(fd, ptr, left);
      if (ret < 0)
         return -errno;
      left -= ret;
      ptr += ret;
   } while (left);
   return size;
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
static int vtest_block_write_zero(int fd, int size)
{
   char zero[256] = {0};
   int left;
   int ret;
   left = size;
   do {
      ret = write(fd, zero, MIN2(left, 256));
      if (ret < 0)
         return -errno;
      left -= ret;
   } while (left);
   return size;
}

147 148 149 150 151
int vtest_block_read(int fd, void *buf, int size)
{
   void *ptr = buf;
   int left;
   int ret;
152 153
   static int savefd = -1;

154 155 156
   left = size;
   do {
      ret = read(fd, ptr, left);
157 158
      if (ret <= 0)
	return ret == -1 ? -errno : 0;
159 160 161
      left -= ret;
      ptr += ret;
   } while (left);
162 163 164 165 166 167 168 169 170 171 172 173 174 175
   if (getenv("VTEST_SAVE")) {
      if (savefd == -1) {
         savefd = open(getenv("VTEST_SAVE"),
                       O_CLOEXEC|O_CREAT|O_WRONLY|O_TRUNC|O_DSYNC, S_IRUSR|S_IWUSR);
         if (savefd == -1) {
            perror("error opening save file");
            exit(1);
         }
      }
      if (write(savefd, buf, size) != size) {
         perror("failed to save");
         exit(1);
      }
   }
176 177 178
   return size;
}

179
int vtest_create_renderer(int in_fd, int out_fd, uint32_t length)
180
{
181
    char *vtestname;
Dave Airlie's avatar
Dave Airlie committed
182
    int ret;
183
    int ctx = VIRGL_RENDERER_USE_EGL;
Dave Airlie's avatar
Dave Airlie committed
184

185
    renderer.iovec_hash = util_hash_table_create(hash_func, compare_iovecs, free_iovec);
186 187
    renderer.in_fd = in_fd;
    renderer.out_fd = out_fd;
188

189 190 191
    /* By default we support version 0 unless VCMD_PROTOCOL_VERSION is sent */
    renderer.protocol_version = 0;

192 193 194
    if (getenv("VTEST_USE_GLX"))
       ctx = VIRGL_RENDERER_USE_GLX;

195 196 197 198 199 200 201 202
    if (getenv("VTEST_USE_EGL_SURFACELESS")) {
        if (ctx & VIRGL_RENDERER_USE_GLX) {
            fprintf(stderr, "Cannot use surfaceless with GLX.\n");
            return -1;
        }
        ctx |= VIRGL_RENDERER_USE_SURFACELESS;
    }

203 204 205 206 207 208 209 210
    if (getenv("VTEST_USE_GLES")) {
        if (ctx & VIRGL_RENDERER_USE_GLX) {
            fprintf(stderr, "Cannot use GLES with GLX.\n");
            return -1;
        }
        ctx |= VIRGL_RENDERER_USE_GLES;
    }

211
    ret = virgl_renderer_init(&renderer,
212
                              ctx | VIRGL_RENDERER_THREAD_SYNC, &vtest_cbs);
213 214 215 216
    if (ret) {
      fprintf(stderr, "failed to initialise renderer.\n");
      return -1;
    }
Dave Airlie's avatar
Dave Airlie committed
217

218
    vtestname = calloc(1, length + 1);
219 220 221
    if (!vtestname)
      return -1;

222
    ret = vtest_block_read(renderer.in_fd, vtestname, length);
Gert Wollny's avatar
Gert Wollny committed
223
    if (ret != (int)length) {
224 225 226 227
       ret = -1;
       goto end;
    }

Dave Airlie's avatar
Dave Airlie committed
228
    ret = virgl_renderer_context_create(ctx_id, strlen(vtestname), vtestname);
229 230 231

end:
    free(vtestname);
Dave Airlie's avatar
Dave Airlie committed
232
    return ret;
233 234
}

235 236 237 238 239 240 241 242 243 244 245 246 247
int vtest_ping_protocol_version(void)
{
    uint32_t hdr_buf[VTEST_HDR_SIZE];
    int ret;

    hdr_buf[VTEST_CMD_LEN] = VCMD_PING_PROTOCOL_VERSION_SIZE;
    hdr_buf[VTEST_CMD_ID] = VCMD_PING_PROTOCOL_VERSION;
    ret = vtest_block_write(renderer.out_fd, hdr_buf, sizeof(hdr_buf));
    if (ret < 0)
	return ret;
    return 0;
}

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
int vtest_protocol_version(void)
{
    uint32_t hdr_buf[VTEST_HDR_SIZE];
    uint32_t version_buf[VCMD_PROTOCOL_VERSION_SIZE];
    int ret;

    ret = vtest_block_read(renderer.in_fd, &version_buf, sizeof(version_buf));
    if (ret != sizeof(version_buf))
        return -1;

    renderer.protocol_version = MIN2(version_buf[VCMD_PROTOCOL_VERSION_VERSION],
                                     VTEST_PROTOCOL_VERSION);

    hdr_buf[VTEST_CMD_LEN] = VCMD_PROTOCOL_VERSION_SIZE;
    hdr_buf[VTEST_CMD_ID] = VCMD_PROTOCOL_VERSION;

    version_buf[VCMD_PROTOCOL_VERSION_VERSION] = renderer.protocol_version;

    ret = vtest_block_write(renderer.out_fd, hdr_buf, sizeof(hdr_buf));
    if (ret < 0)
      return ret;

    ret = vtest_block_write(renderer.out_fd, version_buf, sizeof(version_buf));
    if (ret < 0)
      return ret;

    return 0;
}

277 278 279 280
void vtest_destroy_renderer(void)
{
  virgl_renderer_context_destroy(ctx_id);
  virgl_renderer_cleanup(&renderer);
281 282
  renderer.in_fd = -1;
  renderer.out_fd = -1;
283 284
}

285
int vtest_send_caps2(UNUSED uint32_t length_dw)
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
{
    uint32_t hdr_buf[2];
    void *caps_buf;
    int ret;
    uint32_t max_ver, max_size;

    virgl_renderer_get_cap_set(2, &max_ver, &max_size);

    if (max_size == 0)
	return -1;
    caps_buf = malloc(max_size);
    if (!caps_buf)
	return -1;

    virgl_renderer_fill_caps(2, 1, caps_buf);

    hdr_buf[0] = max_size + 1;
    hdr_buf[1] = 2;
    ret = vtest_block_write(renderer.out_fd, hdr_buf, 8);
    if (ret < 0)
	goto end;
    vtest_block_write(renderer.out_fd, caps_buf, max_size);
    if (ret < 0)
	goto end;

end:
    free(caps_buf);
    return 0;
}

316
int vtest_send_caps(UNUSED uint32_t length_dw)
317 318 319 320
{
    uint32_t  max_ver, max_size;
    void *caps_buf;
    uint32_t hdr_buf[2];
321 322
    int ret;

323 324 325 326 327 328 329 330 331 332
    virgl_renderer_get_cap_set(1, &max_ver, &max_size);

    caps_buf = malloc(max_size);
    if (!caps_buf)
	return -1;
    
    virgl_renderer_fill_caps(1, 1, caps_buf);

    hdr_buf[0] = max_size + 1;
    hdr_buf[1] = 1;
333
    ret = vtest_block_write(renderer.out_fd, hdr_buf, 8);
334
    if (ret < 0)
335
       goto end;
336
    vtest_block_write(renderer.out_fd, caps_buf, max_size);
337
    if (ret < 0)
338
       goto end;
339

340 341
end:
    free(caps_buf);
342 343
    return 0;
}
Dave Airlie's avatar
Dave Airlie committed
344

345
int vtest_create_resource(UNUSED uint32_t length_dw)
Dave Airlie's avatar
Dave Airlie committed
346 347 348 349 350
{
    uint32_t res_create_buf[VCMD_RES_CREATE_SIZE];
    struct virgl_renderer_resource_create_args args;
    int ret;

351
    ret = vtest_block_read(renderer.in_fd, &res_create_buf, sizeof(res_create_buf));
Dave Airlie's avatar
Dave Airlie committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    if (ret != sizeof(res_create_buf))
	return -1;
	
    args.handle = res_create_buf[VCMD_RES_CREATE_RES_HANDLE];
    args.target = res_create_buf[VCMD_RES_CREATE_TARGET];
    args.format = res_create_buf[VCMD_RES_CREATE_FORMAT];
    args.bind = res_create_buf[VCMD_RES_CREATE_BIND];

    args.width = res_create_buf[VCMD_RES_CREATE_WIDTH];
    args.height = res_create_buf[VCMD_RES_CREATE_HEIGHT];
    args.depth = res_create_buf[VCMD_RES_CREATE_DEPTH];
    args.array_size = res_create_buf[VCMD_RES_CREATE_ARRAY_SIZE];
    args.last_level = res_create_buf[VCMD_RES_CREATE_LAST_LEVEL];
    args.nr_samples = res_create_buf[VCMD_RES_CREATE_NR_SAMPLES];
    args.flags = 0;

    ret = virgl_renderer_resource_create(&args, NULL, 0);

    virgl_renderer_ctx_attach_resource(ctx_id, args.handle);
    return ret;
}

374
int vtest_create_resource2(UNUSED uint32_t length_dw)
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
{
    uint32_t res_create_buf[VCMD_RES_CREATE2_SIZE];
    struct virgl_renderer_resource_create_args args;
    struct iovec *iovec;
    int ret;

    ret = vtest_block_read(renderer.in_fd, &res_create_buf, sizeof(res_create_buf));
    if (ret != sizeof(res_create_buf))
	return -1;

    args.handle = res_create_buf[VCMD_RES_CREATE2_RES_HANDLE];
    args.target = res_create_buf[VCMD_RES_CREATE2_TARGET];
    args.format = res_create_buf[VCMD_RES_CREATE2_FORMAT];
    args.bind = res_create_buf[VCMD_RES_CREATE2_BIND];

    args.width = res_create_buf[VCMD_RES_CREATE2_WIDTH];
    args.height = res_create_buf[VCMD_RES_CREATE2_HEIGHT];
    args.depth = res_create_buf[VCMD_RES_CREATE2_DEPTH];
    args.array_size = res_create_buf[VCMD_RES_CREATE2_ARRAY_SIZE];
    args.last_level = res_create_buf[VCMD_RES_CREATE2_LAST_LEVEL];
    args.nr_samples = res_create_buf[VCMD_RES_CREATE2_NR_SAMPLES];
    args.flags = 0;

398 399 400 401
    // Check that the handle doesn't already exist.
    if (util_hash_table_get(renderer.iovec_hash, intptr_to_pointer(args.handle)))
      return -EEXIST;

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
    ret = virgl_renderer_resource_create(&args, NULL, 0);
    if (ret)
      return ret;

    virgl_renderer_ctx_attach_resource(ctx_id, args.handle);

    iovec = CALLOC_STRUCT(iovec);
    if (!iovec)
      return -ENOMEM;

    iovec->iov_len = res_create_buf[VCMD_RES_CREATE2_DATA_SIZE];
    iovec->iov_base = calloc(1, iovec->iov_len);
    if (!iovec->iov_base)
      return -ENOMEM;

    virgl_renderer_resource_attach_iov(args.handle, iovec, 1);
    util_hash_table_set(renderer.iovec_hash, intptr_to_pointer(args.handle), iovec);

    return ret;
}

423
int vtest_resource_unref(UNUSED uint32_t length_dw)
Dave Airlie's avatar
Dave Airlie committed
424 425 426 427 428
{
    uint32_t res_unref_buf[VCMD_RES_UNREF_SIZE];
    int ret;
    uint32_t handle;

429
    ret = vtest_block_read(renderer.in_fd, &res_unref_buf, sizeof(res_unref_buf));
Dave Airlie's avatar
Dave Airlie committed
430 431 432 433 434
    if (ret != sizeof(res_unref_buf))
      return -1;

    handle = res_unref_buf[VCMD_RES_UNREF_RES_HANDLE];
    virgl_renderer_ctx_attach_resource(ctx_id, handle);
435 436 437 438

    virgl_renderer_resource_detach_iov(handle, NULL, NULL);
    util_hash_table_remove(renderer.iovec_hash, intptr_to_pointer(handle));

Dave Airlie's avatar
Dave Airlie committed
439 440 441
    virgl_renderer_resource_unref(handle);
    return 0;
}
Dave Airlie's avatar
Dave Airlie committed
442 443 444 445 446 447

int vtest_submit_cmd(uint32_t length_dw)
{
    uint32_t *cbuf;
    int ret;

448 449 450
    if (length_dw > UINT_MAX / 4)
       return -1;

Dave Airlie's avatar
Dave Airlie committed
451 452 453 454
    cbuf = malloc(length_dw * 4);
    if (!cbuf)
	return -1;

455
    ret = vtest_block_read(renderer.in_fd, cbuf, length_dw * 4);
Gert Wollny's avatar
Gert Wollny committed
456
    if (ret != (int)length_dw * 4) {
457 458 459
       free(cbuf);
       return -1;
    }
Dave Airlie's avatar
Dave Airlie committed
460 461 462 463 464 465

    virgl_renderer_submit_cmd(cbuf, ctx_id, length_dw);

    free(cbuf);
    return 0;
}
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482

#define DECODE_TRANSFER \
  do {							\
  handle = thdr_buf[VCMD_TRANSFER_RES_HANDLE];		\
  level = thdr_buf[VCMD_TRANSFER_LEVEL];		\
  stride = thdr_buf[VCMD_TRANSFER_STRIDE];		\
  layer_stride = thdr_buf[VCMD_TRANSFER_LAYER_STRIDE];	\
  box.x = thdr_buf[VCMD_TRANSFER_X];			\
  box.y = thdr_buf[VCMD_TRANSFER_Y];			\
  box.z = thdr_buf[VCMD_TRANSFER_Z];			\
  box.w = thdr_buf[VCMD_TRANSFER_WIDTH];		\
  box.h = thdr_buf[VCMD_TRANSFER_HEIGHT];		\
  box.d = thdr_buf[VCMD_TRANSFER_DEPTH];		\
  data_size = thdr_buf[VCMD_TRANSFER_DATA_SIZE];		\
  } while(0)


Gert Wollny's avatar
Gert Wollny committed
483
int vtest_transfer_get(UNUSED uint32_t length_dw)
484 485 486 487 488 489 490 491 492 493
{
    uint32_t thdr_buf[VCMD_TRANSFER_HDR_SIZE];
    int ret;
    int level;
    uint32_t stride, layer_stride, handle;
    struct virgl_box box;
    uint32_t data_size;
    void *ptr;
    struct iovec iovec;

494
    ret = vtest_block_read(renderer.in_fd, thdr_buf, VCMD_TRANSFER_HDR_SIZE * 4);
495 496 497 498 499 500 501 502 503 504 505
    if (ret != VCMD_TRANSFER_HDR_SIZE * 4)
      return ret;

    DECODE_TRANSFER;

    ptr = malloc(data_size);
    if (!ptr)
      return -ENOMEM;

    iovec.iov_len = data_size;
    iovec.iov_base = ptr;
506
    ret = virgl_renderer_transfer_read_iov(handle,
507 508 509 510 511 512 513
				     ctx_id,
				     level,
				     stride,
				     layer_stride,
				     &box,
				     0,
				     &iovec, 1);
514 515
    if (ret)
      fprintf(stderr," transfer read failed %d\n", ret);
516
    ret = vtest_block_write(renderer.out_fd, ptr, data_size);
517 518

    free(ptr);
519
    return ret < 0 ? ret : 0;
520 521
}

Gert Wollny's avatar
Gert Wollny committed
522
int vtest_transfer_put(UNUSED uint32_t length_dw)
523 524 525 526 527 528 529 530 531
{
    uint32_t thdr_buf[VCMD_TRANSFER_HDR_SIZE];
    int ret;
    int level;
    uint32_t stride, layer_stride, handle;
    struct virgl_box box;
    uint32_t data_size;
    void *ptr;
    struct iovec iovec;
532

533
    ret = vtest_block_read(renderer.in_fd, thdr_buf, VCMD_TRANSFER_HDR_SIZE * 4);
534 535 536 537 538 539 540 541 542
    if (ret != VCMD_TRANSFER_HDR_SIZE * 4)
      return ret;

    DECODE_TRANSFER;

    ptr = malloc(data_size);
    if (!ptr)
      return -ENOMEM;

543
    ret = vtest_block_read(renderer.in_fd, ptr, data_size);
544 545
    if (ret < 0)
      return ret;
546 547 548

    iovec.iov_len = data_size;
    iovec.iov_base = ptr;
549 550 551 552 553 554 555 556 557 558
    ret = virgl_renderer_transfer_write_iov(handle,
					    ctx_id,
					    level,
					    stride,
					    layer_stride,
					    &box,
					    0,
					    &iovec, 1);
    if (ret)
      fprintf(stderr," transfer write failed %d\n", ret);
559 560 561 562
    free(ptr);
    return 0;
}

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
#define DECODE_TRANSFER2 \
  do {							\
  handle = thdr_buf[VCMD_TRANSFER2_RES_HANDLE];		\
  level = thdr_buf[VCMD_TRANSFER2_LEVEL];		\
  box.x = thdr_buf[VCMD_TRANSFER2_X];			\
  box.y = thdr_buf[VCMD_TRANSFER2_Y];			\
  box.z = thdr_buf[VCMD_TRANSFER2_Z];			\
  box.w = thdr_buf[VCMD_TRANSFER2_WIDTH];		\
  box.h = thdr_buf[VCMD_TRANSFER2_HEIGHT];		\
  box.d = thdr_buf[VCMD_TRANSFER2_DEPTH];		\
  data_size = thdr_buf[VCMD_TRANSFER2_DATA_SIZE];		\
  offset = thdr_buf[VCMD_TRANSFER2_OFFSET];		\
  } while(0)


578
int vtest_transfer_get2(UNUSED uint32_t length_dw)
579 580 581 582 583 584 585 586
{
    uint32_t thdr_buf[VCMD_TRANSFER2_HDR_SIZE];
    int ret;
    int level;
    uint32_t handle;
    struct virgl_box box;
    uint32_t data_size;
    uint32_t offset;
587
    uint32_t extra_data = 0;
588 589 590
    struct iovec *iovec;

    ret = vtest_block_read(renderer.in_fd, thdr_buf, sizeof(thdr_buf));
591
    if (ret != sizeof(thdr_buf)) {
592
      return ret;
593
    }
594 595 596

    DECODE_TRANSFER2;

597
    iovec = util_hash_table_get(renderer.iovec_hash, intptr_to_pointer(handle));
598 599 600
    if (!iovec) {
      return report_failed_call("util_hash_table_get", -ESRCH);
    }
601

602 603 604 605
    if (offset >= iovec->iov_len) {
      return report_failure("offset larger then length of backing store", -EFAULT);
    }

606 607 608 609 610 611 612 613
    ret = virgl_renderer_transfer_read_iov(handle,
				     ctx_id,
				     level,
				     0,
				     0,
				     &box,
				     offset,
				     NULL, 0);
614 615 616
    if (ret) {
      return report_failed_call("virgl_renderer_transfer_read_iov", ret);
    }
617

618 619 620 621 622 623
    /* Make sure we don't read out of bounds. */
    if (data_size > (iovec->iov_len - offset)) {
      extra_data = data_size - (iovec->iov_len - offset);
      data_size -= extra_data;
    }

624 625 626
    ret = vtest_block_write(renderer.out_fd,
                            iovec->iov_base + offset,
                            data_size);
627 628 629 630
    if (ret < 0) {
      return report_failed_call("vtest_block_write", ret);
    }

631 632 633 634 635 636 637
    if (extra_data) {
      ret = vtest_block_write_zero(renderer.out_fd, extra_data);
      if (ret < 0) {
        return report_failed_call("vtest_block_write_zero", ret);
      }
    }

638 639 640
    return ret < 0 ? ret : 0;
}

641
int vtest_transfer_put2(UNUSED uint32_t length_dw)
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
{
    uint32_t thdr_buf[VCMD_TRANSFER2_HDR_SIZE];
    int ret;
    int level;
    uint32_t handle;
    struct virgl_box box;
    uint32_t data_size;
    uint32_t offset;
    struct iovec *iovec;

    ret = vtest_block_read(renderer.in_fd, thdr_buf, sizeof(thdr_buf));
    if (ret != sizeof(thdr_buf))
      return ret;

    DECODE_TRANSFER2;

    iovec = util_hash_table_get(renderer.iovec_hash, intptr_to_pointer(handle));
659 660 661
    if (!iovec) {
      return report_failed_call("util_hash_table_get", -ESRCH);
    }
662

663
    ret = vtest_block_read(renderer.in_fd, iovec->iov_base + offset, data_size);
664 665 666
    if (ret < 0) {
      return report_failed_call("vtest_block_read", ret);
    }
667 668 669 670 671 672 673 674 675

    ret = virgl_renderer_transfer_write_iov(handle,
					    ctx_id,
					    level,
					    0,
					    0,
					    &box,
					    offset,
					    NULL, 0);
676 677 678 679
    if (ret) {
      return report_failed_call("virgl_renderer_transfer_write_iov", ret);
    }

680 681 682
    return 0;
}

683
int vtest_resource_busy_wait(UNUSED uint32_t length_dw)
684 685
{
  uint32_t bw_buf[VCMD_BUSY_WAIT_SIZE];
686
  int ret, fd;
687 688 689 690
  int flags;
  uint32_t hdr_buf[VTEST_HDR_SIZE];
  uint32_t reply_buf[1];
  bool busy = false;
691 692

  vtest_renderer_create_fence();
693
  ret = vtest_block_read(renderer.in_fd, &bw_buf, sizeof(bw_buf));
694 695 696
  if (ret != sizeof(bw_buf))
    return -1;

Dave Airlie's avatar
Dave Airlie committed
697
  /*  handle = bw_buf[VCMD_BUSY_WAIT_HANDLE]; unused as of now */
698 699 700 701
  flags = bw_buf[VCMD_BUSY_WAIT_FLAGS];

  if (flags == VCMD_BUSY_WAIT_FLAG_WAIT) {
    do {
702 703 704 705 706 707 708
       if (last_fence == (fence_id - 1))
          break;

       fd = virgl_renderer_get_poll_fd();
       if (fd != -1)
          vtest_wait_for_fd_read(fd);
       virgl_renderer_poll();
709 710 711 712 713 714 715 716 717 718
    } while (1);
    busy = false;
  } else {
    busy = last_fence != (fence_id - 1);
  }

  hdr_buf[VTEST_CMD_LEN] = 1;
  hdr_buf[VTEST_CMD_ID] = VCMD_RESOURCE_BUSY_WAIT;
  reply_buf[0] = busy ? 1 : 0;

719
  ret = vtest_block_write(renderer.out_fd, hdr_buf, sizeof(hdr_buf));
720 721 722
  if (ret < 0)
    return ret;

723
  ret = vtest_block_write(renderer.out_fd, reply_buf, sizeof(reply_buf));
724 725 726
  if (ret < 0)
    return ret;

727 728 729 730 731 732 733 734 735 736 737 738
  return 0;
}

int vtest_renderer_create_fence(void)
{
  virgl_renderer_create_fence(fence_id++, ctx_id);
  return 0;
}

int vtest_poll(void)
{
  virgl_renderer_poll();
739
  return 0;
740
}