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 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
|
/*
* Copyright © 2017 Intel Corporation
*
* 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 (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 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.
*/
#include "igt.h"
#include "sw_sync.h"
#include "igt_syncobj.h"
#include <unistd.h>
#include <time.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <signal.h>
#include "drm.h"
/**
* TEST: syncobj wait
* Description: Tests for the drm sync object wait API
* Category: Core
* Mega feature: General Core features
* Sub-category: Synchronization
* Functionality: semaphore
* Feature: synchronization
* Test category: GEM_Legacy
*
* SUBTEST: invalid-multi-wait-all-unsubmitted
*
* SUBTEST: invalid-multi-wait-all-unsubmitted-signaled
*
* SUBTEST: invalid-multi-wait-all-unsubmitted-submitted
*
* SUBTEST: invalid-multi-wait-all-unsubmitted-submitted-signaled
*
* SUBTEST: invalid-multi-wait-unsubmitted
*
* SUBTEST: invalid-multi-wait-unsubmitted-signaled
*
* SUBTEST: invalid-multi-wait-unsubmitted-submitted
*
* SUBTEST: invalid-multi-wait-unsubmitted-submitted-signaled
*
* SUBTEST: invalid-reset-bad-pad
*
* SUBTEST: invalid-reset-illegal-handle
*
* SUBTEST: invalid-reset-one-illegal-handle
*
* SUBTEST: invalid-reset-zero-handles
*
* SUBTEST: invalid-signal-bad-pad
*
* SUBTEST: invalid-signal-illegal-handle
*
* SUBTEST: invalid-signal-one-illegal-handle
*
* SUBTEST: invalid-signal-zero-handles
*
* SUBTEST: invalid-single-wait-all-unsubmitted
*
* SUBTEST: invalid-single-wait-unsubmitted
*
* SUBTEST: invalid-wait-bad-flags
*
* SUBTEST: invalid-wait-illegal-handle
*
* SUBTEST: multi-wait-all-for-submit-signaled
*
* SUBTEST: multi-wait-all-for-submit-submitted
*
* SUBTEST: multi-wait-all-for-submit-submitted-signaled
*
* SUBTEST: multi-wait-all-for-submit-unsubmitted
*
* SUBTEST: multi-wait-all-for-submit-unsubmitted-signaled
*
* SUBTEST: multi-wait-all-for-submit-unsubmitted-submitted
*
* SUBTEST: multi-wait-all-signaled
*
* SUBTEST: multi-wait-all-submitted
*
* SUBTEST: multi-wait-all-submitted-signaled
*
* SUBTEST: multi-wait-for-submit-signaled
*
* SUBTEST: multi-wait-for-submit-submitted
*
* SUBTEST: multi-wait-for-submit-submitted-signaled
*
* SUBTEST: multi-wait-for-submit-unsubmitted
*
* SUBTEST: multi-wait-for-submit-unsubmitted-signaled
*
* SUBTEST: multi-wait-for-submit-unsubmitted-submitted
*
* SUBTEST: multi-wait-for-submit-unsubmitted-submitted-signaled
*
* SUBTEST: multi-wait-signaled
*
* SUBTEST: multi-wait-submitted
*
* SUBTEST: multi-wait-submitted-signaled
*
* SUBTEST: reset-during-wait-for-submit
*
* SUBTEST: reset-multiple-signaled
*
* SUBTEST: reset-signaled
*
* SUBTEST: reset-unsignaled
*
* SUBTEST: signal
*
* SUBTEST: single-wait-all-for-submit-signaled
*
* SUBTEST: single-wait-all-for-submit-submitted
*
* SUBTEST: single-wait-all-for-submit-unsubmitted
*
* SUBTEST: single-wait-all-signaled
*
* SUBTEST: single-wait-all-submitted
*
* SUBTEST: single-wait-for-submit-signaled
*
* SUBTEST: single-wait-for-submit-submitted
*
* SUBTEST: single-wait-for-submit-unsubmitted
*
* SUBTEST: single-wait-signaled
*
* SUBTEST: single-wait-submitted
*
* SUBTEST: wait-all-complex
*
* SUBTEST: wait-all-delayed-signal
*
* SUBTEST: wait-all-for-submit-complex
*
* SUBTEST: wait-all-for-submit-delayed-submit
*
* SUBTEST: wait-all-for-submit-snapshot
*
* SUBTEST: wait-all-interrupted
*
* SUBTEST: wait-all-snapshot
*
* SUBTEST: wait-any-complex
*
* SUBTEST: wait-any-interrupted
*
* SUBTEST: wait-any-snapshot
*
* SUBTEST: wait-delayed-signal
*
* SUBTEST: wait-for-submit-complex
*
* SUBTEST: wait-for-submit-delayed-submit
*
* SUBTEST: wait-for-submit-snapshot
*
* SUBTEST: wait-zero-handles
*/
IGT_TEST_DESCRIPTION("Tests for the drm sync object wait API");
/* One tenth of a second */
#define SHORT_TIME_NSEC 100000000ull
#define NSECS_PER_SEC 1000000000ull
static uint64_t
gettime_ns(void)
{
struct timespec current;
clock_gettime(CLOCK_MONOTONIC, ¤t);
return (uint64_t)current.tv_sec * NSECS_PER_SEC + current.tv_nsec;
}
static void
sleep_nsec(uint64_t time_nsec)
{
struct timespec t;
t.tv_sec = time_nsec / NSECS_PER_SEC;
t.tv_nsec = time_nsec % NSECS_PER_SEC;
igt_assert_eq(nanosleep(&t, NULL), 0);
}
static uint64_t
short_timeout(void)
{
return gettime_ns() + SHORT_TIME_NSEC;
}
static int
syncobj_attach_sw_sync(int fd, uint32_t handle)
{
struct drm_syncobj_handle;
int timeline, fence;
timeline = sw_sync_timeline_create();
fence = sw_sync_timeline_create_fence(timeline, 1);
syncobj_import_sync_file(fd, handle, fence);
close(fence);
return timeline;
}
static void
syncobj_trigger(int fd, uint32_t handle)
{
int timeline = syncobj_attach_sw_sync(fd, handle);
sw_sync_timeline_inc(timeline, 1);
close(timeline);
}
static timer_t
set_timer(void (*cb)(union sigval), void *ptr, int i, uint64_t nsec)
{
timer_t timer;
struct sigevent sev;
struct itimerspec its;
memset(&sev, 0, sizeof(sev));
sev.sigev_notify = SIGEV_THREAD;
if (ptr)
sev.sigev_value.sival_ptr = ptr;
else
sev.sigev_value.sival_int = i;
sev.sigev_notify_function = cb;
igt_assert(timer_create(CLOCK_MONOTONIC, &sev, &timer) == 0);
memset(&its, 0, sizeof(its));
its.it_value.tv_sec = nsec / NSEC_PER_SEC;
its.it_value.tv_nsec = nsec % NSEC_PER_SEC;
igt_assert(timer_settime(timer, 0, &its, NULL) == 0);
return timer;
}
struct fd_handle_pair {
int fd;
uint32_t handle;
};
static void
timeline_inc_func(union sigval sigval)
{
sw_sync_timeline_inc(sigval.sival_int, 1);
}
static void
syncobj_trigger_free_pair_func(union sigval sigval)
{
struct fd_handle_pair *pair = sigval.sival_ptr;
syncobj_trigger(pair->fd, pair->handle);
free(pair);
}
static timer_t
syncobj_trigger_delayed(int fd, uint32_t syncobj, uint64_t nsec)
{
struct fd_handle_pair *pair = malloc(sizeof(*pair));
pair->fd = fd;
pair->handle = syncobj;
return set_timer(syncobj_trigger_free_pair_func, pair, 0, nsec);
}
static void
test_wait_bad_flags(int fd)
{
struct drm_syncobj_wait wait = { 0 };
wait.flags = 0xdeadbeef;
igt_assert_eq(__syncobj_wait(fd, &wait), -EINVAL);
}
static void
test_wait_zero_handles(int fd)
{
struct drm_syncobj_wait wait = { 0 };
igt_assert_eq(__syncobj_wait(fd, &wait), 0);
}
static void
test_wait_illegal_handle(int fd)
{
struct drm_syncobj_wait wait = { 0 };
uint32_t handle = 0;
wait.count_handles = 1;
wait.handles = to_user_pointer(&handle);
igt_assert_eq(__syncobj_wait(fd, &wait), -ENOENT);
}
static void
test_reset_zero_handles(int fd)
{
struct drm_syncobj_array array = { 0 };
int ret;
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &array);
igt_assert(ret == -1 && errno == EINVAL);
}
static void
test_reset_illegal_handle(int fd)
{
struct drm_syncobj_array array = { 0 };
uint32_t handle = 0;
int ret;
array.count_handles = 1;
array.handles = to_user_pointer(&handle);
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &array);
igt_assert(ret == -1 && errno == ENOENT);
}
static void
test_reset_one_illegal_handle(int fd)
{
struct drm_syncobj_array array = { 0 };
uint32_t syncobjs[3];
int ret;
syncobjs[0] = syncobj_create(fd, DRM_SYNCOBJ_CREATE_SIGNALED);
syncobjs[1] = 0;
syncobjs[2] = syncobj_create(fd, DRM_SYNCOBJ_CREATE_SIGNALED);
igt_assert_eq(syncobj_wait_err(fd, &syncobjs[0], 1, 0, 0), 0);
igt_assert_eq(syncobj_wait_err(fd, &syncobjs[2], 1, 0, 0), 0);
array.count_handles = 3;
array.handles = to_user_pointer(syncobjs);
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &array);
igt_assert(ret == -1 && errno == ENOENT);
/* Assert that we didn't actually reset anything */
igt_assert_eq(syncobj_wait_err(fd, &syncobjs[0], 1, 0, 0), 0);
igt_assert_eq(syncobj_wait_err(fd, &syncobjs[2], 1, 0, 0), 0);
syncobj_destroy(fd, syncobjs[0]);
syncobj_destroy(fd, syncobjs[2]);
}
static void
test_reset_bad_pad(int fd)
{
struct drm_syncobj_array array = { 0 };
uint32_t handle = 0;
int ret;
array.pad = 0xdeadbeef;
array.count_handles = 1;
array.handles = to_user_pointer(&handle);
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &array);
igt_assert(ret == -1 && errno == EINVAL);
}
static void
test_signal_zero_handles(int fd)
{
struct drm_syncobj_array array = { 0 };
int ret;
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &array);
igt_assert(ret == -1 && errno == EINVAL);
}
static void
test_signal_illegal_handle(int fd)
{
struct drm_syncobj_array array = { 0 };
uint32_t handle = 0;
int ret;
array.count_handles = 1;
array.handles = to_user_pointer(&handle);
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &array);
igt_assert(ret == -1 && errno == ENOENT);
}
static void
test_signal_one_illegal_handle(int fd)
{
struct drm_syncobj_array array = { 0 };
uint32_t syncobjs[3];
int ret;
syncobjs[0] = syncobj_create(fd, 0);
syncobjs[1] = 0;
syncobjs[2] = syncobj_create(fd, 0);
igt_assert_eq(syncobj_wait_err(fd, &syncobjs[0], 1, 0, 0), -EINVAL);
igt_assert_eq(syncobj_wait_err(fd, &syncobjs[2], 1, 0, 0), -EINVAL);
array.count_handles = 3;
array.handles = to_user_pointer(syncobjs);
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &array);
igt_assert(ret == -1 && errno == ENOENT);
/* Assert that we didn't actually reset anything */
igt_assert_eq(syncobj_wait_err(fd, &syncobjs[0], 1, 0, 0), -EINVAL);
igt_assert_eq(syncobj_wait_err(fd, &syncobjs[2], 1, 0, 0), -EINVAL);
syncobj_destroy(fd, syncobjs[0]);
syncobj_destroy(fd, syncobjs[2]);
}
static void
test_signal_bad_pad(int fd)
{
struct drm_syncobj_array array = { 0 };
uint32_t handle = 0;
int ret;
array.pad = 0xdeadbeef;
array.count_handles = 1;
array.handles = to_user_pointer(&handle);
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &array);
igt_assert(ret == -1 && errno == EINVAL);
}
#define WAIT_FOR_SUBMIT (1 << 0)
#define WAIT_ALL (1 << 1)
#define WAIT_UNSUBMITTED (1 << 2)
#define WAIT_SUBMITTED (1 << 3)
#define WAIT_SIGNALED (1 << 4)
#define WAIT_FLAGS_MAX (1 << 5) - 1
static uint32_t
flags_for_test_flags(uint32_t test_flags)
{
uint32_t flags = 0;
if (test_flags & WAIT_FOR_SUBMIT)
flags |= DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT;
if (test_flags & WAIT_ALL)
flags |= DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL;
return flags;
}
static void
test_single_wait(int fd, uint32_t test_flags, int expect)
{
uint32_t syncobj = syncobj_create(fd, 0);
uint32_t flags = flags_for_test_flags(test_flags);
int timeline = -1;
if (test_flags & (WAIT_SUBMITTED | WAIT_SIGNALED))
timeline = syncobj_attach_sw_sync(fd, syncobj);
if (test_flags & WAIT_SIGNALED)
sw_sync_timeline_inc(timeline, 1);
igt_assert_eq(syncobj_wait_err(fd, &syncobj, 1, 0, flags), expect);
igt_assert_eq(syncobj_wait_err(fd, &syncobj, 1, short_timeout(),
flags), expect);
if (expect != -ETIME) {
igt_assert_eq(syncobj_wait_err(fd, &syncobj, 1, UINT64_MAX,
flags), expect);
}
syncobj_destroy(fd, syncobj);
if (timeline != -1)
close(timeline);
}
static void
test_wait_delayed_signal(int fd, uint32_t test_flags)
{
uint32_t syncobj = syncobj_create(fd, 0);
uint32_t flags = flags_for_test_flags(test_flags);
int timeline = -1;
timer_t timer;
if (test_flags & WAIT_FOR_SUBMIT) {
timer = syncobj_trigger_delayed(fd, syncobj, SHORT_TIME_NSEC);
} else {
timeline = syncobj_attach_sw_sync(fd, syncobj);
timer = set_timer(timeline_inc_func, NULL,
timeline, SHORT_TIME_NSEC);
}
igt_assert(syncobj_wait(fd, &syncobj, 1,
gettime_ns() + SHORT_TIME_NSEC * 2,
flags, NULL));
timer_delete(timer);
if (timeline != -1)
close(timeline);
syncobj_destroy(fd, syncobj);
}
static void
test_reset_unsignaled(int fd)
{
uint32_t syncobj = syncobj_create(fd, 0);
igt_assert_eq(syncobj_wait_err(fd, &syncobj, 1, 0, 0), -EINVAL);
syncobj_reset(fd, &syncobj, 1);
igt_assert_eq(syncobj_wait_err(fd, &syncobj, 1, 0, 0), -EINVAL);
syncobj_destroy(fd, syncobj);
}
static void
test_reset_signaled(int fd)
{
uint32_t syncobj = syncobj_create(fd, 0);
syncobj_trigger(fd, syncobj);
igt_assert_eq(syncobj_wait_err(fd, &syncobj, 1, 0, 0), 0);
syncobj_reset(fd, &syncobj, 1);
igt_assert_eq(syncobj_wait_err(fd, &syncobj, 1, 0, 0), -EINVAL);
syncobj_destroy(fd, syncobj);
}
static void
test_reset_multiple_signaled(int fd)
{
uint32_t syncobjs[3];
int i;
for (i = 0; i < 3; i++) {
syncobjs[i] = syncobj_create(fd, 0);
syncobj_trigger(fd, syncobjs[i]);
}
igt_assert_eq(syncobj_wait_err(fd, syncobjs, 3, 0, 0), 0);
syncobj_reset(fd, syncobjs, 3);
for (i = 0; i < 3; i++) {
igt_assert_eq(syncobj_wait_err(fd, &syncobjs[i], 1,
0, 0), -EINVAL);
syncobj_destroy(fd, syncobjs[i]);
}
}
static void
reset_and_trigger_func(union sigval sigval)
{
struct fd_handle_pair *pair = sigval.sival_ptr;
syncobj_reset(pair->fd, &pair->handle, 1);
syncobj_trigger(pair->fd, pair->handle);
}
static void
test_reset_during_wait_for_submit(int fd)
{
uint32_t syncobj = syncobj_create(fd, 0);
uint32_t flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT;
struct fd_handle_pair pair;
timer_t timer;
pair.fd = fd;
pair.handle = syncobj;
timer = set_timer(reset_and_trigger_func, &pair, 0, SHORT_TIME_NSEC);
/* A reset should be a no-op even if we're in the middle of a wait */
igt_assert(syncobj_wait(fd, &syncobj, 1,
gettime_ns() + SHORT_TIME_NSEC * 2,
flags, NULL));
timer_delete(timer);
syncobj_destroy(fd, syncobj);
}
static void
test_signal(int fd)
{
uint32_t syncobj = syncobj_create(fd, 0);
uint32_t flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT;
igt_assert_eq(syncobj_wait_err(fd, &syncobj, 1, 0, 0), -EINVAL);
igt_assert_eq(syncobj_wait_err(fd, &syncobj, 1, 0, flags), -ETIME);
syncobj_signal(fd, &syncobj, 1);
igt_assert(syncobj_wait(fd, &syncobj, 1, 0, 0, NULL));
igt_assert(syncobj_wait(fd, &syncobj, 1, 0, flags, NULL));
syncobj_destroy(fd, syncobj);
}
static void
test_multi_wait(int fd, uint32_t test_flags, int expect)
{
uint32_t syncobjs[3];
uint32_t tflag, flags;
int i, fidx, timeline;
syncobjs[0] = syncobj_create(fd, 0);
syncobjs[1] = syncobj_create(fd, 0);
syncobjs[2] = syncobj_create(fd, 0);
flags = flags_for_test_flags(test_flags);
test_flags &= ~(WAIT_ALL | WAIT_FOR_SUBMIT);
for (i = 0; i < 3; i++) {
fidx = ffs(test_flags) - 1;
tflag = (1 << fidx);
if (test_flags & ~tflag)
test_flags &= ~tflag;
if (tflag & (WAIT_SUBMITTED | WAIT_SIGNALED))
timeline = syncobj_attach_sw_sync(fd, syncobjs[i]);
if (tflag & WAIT_SIGNALED)
sw_sync_timeline_inc(timeline, 1);
}
igt_assert_eq(syncobj_wait_err(fd, syncobjs, 3, 0, flags), expect);
igt_assert_eq(syncobj_wait_err(fd, syncobjs, 3, short_timeout(),
flags), expect);
if (expect != -ETIME) {
igt_assert_eq(syncobj_wait_err(fd, syncobjs, 3, UINT64_MAX,
flags), expect);
}
syncobj_destroy(fd, syncobjs[0]);
syncobj_destroy(fd, syncobjs[1]);
syncobj_destroy(fd, syncobjs[2]);
}
struct wait_thread_data {
int fd;
struct drm_syncobj_wait wait;
};
static void *
wait_thread_func(void *data)
{
struct wait_thread_data *wait = data;
igt_assert_eq(__syncobj_wait(wait->fd, &wait->wait), 0);
return NULL;
}
static void
test_wait_snapshot(int fd, uint32_t test_flags)
{
struct wait_thread_data wait = { 0 };
uint32_t syncobjs[2];
int timelines[3] = { -1, -1, -1 };
pthread_t thread;
syncobjs[0] = syncobj_create(fd, 0);
syncobjs[1] = syncobj_create(fd, 0);
if (!(test_flags & WAIT_FOR_SUBMIT)) {
timelines[0] = syncobj_attach_sw_sync(fd, syncobjs[0]);
timelines[1] = syncobj_attach_sw_sync(fd, syncobjs[1]);
}
wait.fd = fd;
wait.wait.handles = to_user_pointer(syncobjs);
wait.wait.count_handles = 2;
wait.wait.timeout_nsec = short_timeout();
wait.wait.flags = flags_for_test_flags(test_flags);
igt_assert_eq(pthread_create(&thread, NULL, wait_thread_func, &wait), 0);
sleep_nsec(SHORT_TIME_NSEC / 5);
/* Try to fake the kernel out by triggering or partially triggering
* the first fence.
*/
if (test_flags & WAIT_ALL) {
/* If it's WAIT_ALL, actually trigger it */
if (timelines[0] == -1)
syncobj_trigger(fd, syncobjs[0]);
else
sw_sync_timeline_inc(timelines[0], 1);
} else if (test_flags & WAIT_FOR_SUBMIT) {
timelines[0] = syncobj_attach_sw_sync(fd, syncobjs[0]);
}
sleep_nsec(SHORT_TIME_NSEC / 5);
/* Then reset it */
syncobj_reset(fd, &syncobjs[0], 1);
sleep_nsec(SHORT_TIME_NSEC / 5);
/* Then "submit" it in a way that will never trigger. This way, if
* the kernel picks up on the new fence (it shouldn't), we'll get a
* timeout.
*/
timelines[2] = syncobj_attach_sw_sync(fd, syncobjs[0]);
sleep_nsec(SHORT_TIME_NSEC / 5);
/* Now trigger the second fence to complete the wait */
if (timelines[1] == -1)
syncobj_trigger(fd, syncobjs[1]);
else
sw_sync_timeline_inc(timelines[1], 1);
pthread_join(thread, NULL);
if (!(test_flags & WAIT_ALL))
igt_assert_eq(wait.wait.first_signaled, 1);
close(timelines[0]);
close(timelines[1]);
close(timelines[2]);
syncobj_destroy(fd, syncobjs[0]);
syncobj_destroy(fd, syncobjs[1]);
}
/* The numbers 0-7, each repeated 5x and shuffled. */
static const unsigned shuffled_0_7_x4[] = {
2, 0, 6, 1, 1, 4, 5, 2, 0, 7, 1, 7, 6, 3, 4, 5,
0, 2, 7, 3, 5, 4, 0, 6, 7, 3, 2, 5, 6, 1, 4, 3,
};
enum syncobj_stage {
STAGE_UNSUBMITTED,
STAGE_SUBMITTED,
STAGE_SIGNALED,
STAGE_RESET,
STAGE_RESUBMITTED,
};
static void
test_wait_complex(int fd, uint32_t test_flags)
{
struct wait_thread_data wait = { 0 };
uint32_t syncobjs[8];
enum syncobj_stage stage[8];
int i, j, timelines[8];
uint32_t first_signaled = -1, num_signaled = 0;
pthread_t thread;
for (i = 0; i < 8; i++) {
stage[i] = STAGE_UNSUBMITTED;
syncobjs[i] = syncobj_create(fd, 0);
}
if (test_flags & WAIT_FOR_SUBMIT) {
for (i = 0; i < 8; i++)
timelines[i] = -1;
} else {
for (i = 0; i < 8; i++)
timelines[i] = syncobj_attach_sw_sync(fd, syncobjs[i]);
}
wait.fd = fd;
wait.wait.handles = to_user_pointer(syncobjs);
wait.wait.count_handles = 2;
wait.wait.timeout_nsec = gettime_ns() + NSECS_PER_SEC;
wait.wait.flags = flags_for_test_flags(test_flags);
igt_assert_eq(pthread_create(&thread, NULL, wait_thread_func, &wait), 0);
sleep_nsec(NSECS_PER_SEC / 50);
num_signaled = 0;
for (j = 0; j < ARRAY_SIZE(shuffled_0_7_x4); j++) {
i = shuffled_0_7_x4[j];
igt_assert_lt(i, ARRAY_SIZE(syncobjs));
switch (stage[i]++) {
case STAGE_UNSUBMITTED:
/* We need to submit attach a fence */
if (!(test_flags & WAIT_FOR_SUBMIT)) {
/* We had to attach one up-front */
igt_assert_neq(timelines[i], -1);
break;
}
timelines[i] = syncobj_attach_sw_sync(fd, syncobjs[i]);
break;
case STAGE_SUBMITTED:
/* We have a fence, trigger it */
igt_assert_neq(timelines[i], -1);
sw_sync_timeline_inc(timelines[i], 1);
close(timelines[i]);
timelines[i] = -1;
if (num_signaled == 0)
first_signaled = i;
num_signaled++;
break;
case STAGE_SIGNALED:
/* We're already signaled, reset */
syncobj_reset(fd, &syncobjs[i], 1);
break;
case STAGE_RESET:
/* We're reset, submit and don't signal */
timelines[i] = syncobj_attach_sw_sync(fd, syncobjs[i]);
break;
case STAGE_RESUBMITTED:
igt_assert(!"Should not reach this stage");
break;
}
if (test_flags & WAIT_ALL) {
if (num_signaled == ARRAY_SIZE(syncobjs))
break;
} else {
if (num_signaled > 0)
break;
}
sleep_nsec(NSECS_PER_SEC / 100);
}
pthread_join(thread, NULL);
if (test_flags & WAIT_ALL) {
igt_assert_eq(num_signaled, ARRAY_SIZE(syncobjs));
} else {
igt_assert_eq(num_signaled, 1);
igt_assert_eq(wait.wait.first_signaled, first_signaled);
}
for (i = 0; i < 8; i++) {
close(timelines[i]);
syncobj_destroy(fd, syncobjs[i]);
}
}
static void
test_wait_interrupted(int fd, uint32_t test_flags)
{
struct drm_syncobj_wait wait = { 0 };
uint32_t syncobj = syncobj_create(fd, 0);
int timeline;
wait.handles = to_user_pointer(&syncobj);
wait.count_handles = 1;
wait.flags = flags_for_test_flags(test_flags);
if (test_flags & WAIT_FOR_SUBMIT) {
wait.timeout_nsec = short_timeout();
igt_while_interruptible(true)
igt_assert_eq(__syncobj_wait(fd, &wait), -ETIME);
}
timeline = syncobj_attach_sw_sync(fd, syncobj);
wait.timeout_nsec = short_timeout();
igt_while_interruptible(true)
igt_assert_eq(__syncobj_wait(fd, &wait), -ETIME);
syncobj_destroy(fd, syncobj);
close(timeline);
}
static bool
has_syncobj_wait(int fd)
{
struct drm_syncobj_wait wait = { 0 };
uint32_t handle = 0;
uint64_t value;
int ret;
if (drmGetCap(fd, DRM_CAP_SYNCOBJ, &value))
return false;
if (!value)
return false;
/* Try waiting for zero sync objects should fail with EINVAL */
wait.count_handles = 1;
wait.handles = to_user_pointer(&handle);
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &wait);
return ret == -1 && errno == ENOENT;
}
igt_main
{
int fd = -1;
igt_fixture {
fd = drm_open_driver(DRIVER_ANY);
igt_require(has_syncobj_wait(fd));
igt_require_sw_sync();
}
igt_subtest("invalid-wait-bad-flags")
test_wait_bad_flags(fd);
igt_subtest("wait-zero-handles")
test_wait_zero_handles(fd);
igt_subtest("invalid-wait-illegal-handle")
test_wait_illegal_handle(fd);
igt_subtest("invalid-reset-zero-handles")
test_reset_zero_handles(fd);
igt_subtest("invalid-reset-illegal-handle")
test_reset_illegal_handle(fd);
igt_subtest("invalid-reset-one-illegal-handle")
test_reset_one_illegal_handle(fd);
igt_subtest("invalid-reset-bad-pad")
test_reset_bad_pad(fd);
igt_subtest("invalid-signal-zero-handles")
test_signal_zero_handles(fd);
igt_subtest("invalid-signal-illegal-handle")
test_signal_illegal_handle(fd);
igt_subtest("invalid-signal-one-illegal-handle")
test_signal_one_illegal_handle(fd);
igt_subtest("invalid-signal-bad-pad")
test_signal_bad_pad(fd);
for (unsigned flags = 0; flags < WAIT_FLAGS_MAX; flags++) {
int err;
/* Only one wait mode for single-wait tests */
if (__builtin_popcount(flags & (WAIT_UNSUBMITTED |
WAIT_SUBMITTED |
WAIT_SIGNALED)) != 1)
continue;
if ((flags & WAIT_UNSUBMITTED) && !(flags & WAIT_FOR_SUBMIT))
err = -EINVAL;
else if (!(flags & WAIT_SIGNALED))
err = -ETIME;
else
err = 0;
igt_subtest_f("%ssingle-wait%s%s%s%s%s",
err == -EINVAL ? "invalid-" : "",
(flags & WAIT_ALL) ? "-all" : "",
(flags & WAIT_FOR_SUBMIT) ? "-for-submit" : "",
(flags & WAIT_UNSUBMITTED) ? "-unsubmitted" : "",
(flags & WAIT_SUBMITTED) ? "-submitted" : "",
(flags & WAIT_SIGNALED) ? "-signaled" : "")
test_single_wait(fd, flags, err);
}
igt_subtest("wait-delayed-signal")
test_wait_delayed_signal(fd, 0);
igt_subtest("wait-for-submit-delayed-submit")
test_wait_delayed_signal(fd, WAIT_FOR_SUBMIT);
igt_subtest("wait-all-delayed-signal")
test_wait_delayed_signal(fd, WAIT_ALL);
igt_subtest("wait-all-for-submit-delayed-submit")
test_wait_delayed_signal(fd, WAIT_ALL | WAIT_FOR_SUBMIT);
igt_subtest("reset-unsignaled")
test_reset_unsignaled(fd);
igt_subtest("reset-signaled")
test_reset_signaled(fd);
igt_subtest("reset-multiple-signaled")
test_reset_multiple_signaled(fd);
igt_subtest("reset-during-wait-for-submit")
test_reset_during_wait_for_submit(fd);
igt_subtest("signal")
test_signal(fd);
for (unsigned flags = 0; flags < WAIT_FLAGS_MAX; flags++) {
int err;
/* At least one wait mode for multi-wait tests */
if (!(flags & (WAIT_UNSUBMITTED |
WAIT_SUBMITTED |
WAIT_SIGNALED)))
continue;
err = 0;
if ((flags & WAIT_UNSUBMITTED) && !(flags & WAIT_FOR_SUBMIT)) {
err = -EINVAL;
} else if (flags & WAIT_ALL) {
if (flags & (WAIT_UNSUBMITTED | WAIT_SUBMITTED))
err = -ETIME;
} else {
if (!(flags & WAIT_SIGNALED))
err = -ETIME;
}
igt_subtest_f("%smulti-wait%s%s%s%s%s",
err == -EINVAL ? "invalid-" : "",
(flags & WAIT_ALL) ? "-all" : "",
(flags & WAIT_FOR_SUBMIT) ? "-for-submit" : "",
(flags & WAIT_UNSUBMITTED) ? "-unsubmitted" : "",
(flags & WAIT_SUBMITTED) ? "-submitted" : "",
(flags & WAIT_SIGNALED) ? "-signaled" : "")
test_multi_wait(fd, flags, err);
}
igt_subtest("wait-any-snapshot")
test_wait_snapshot(fd, 0);
igt_subtest("wait-all-snapshot")
test_wait_snapshot(fd, WAIT_ALL);
igt_subtest("wait-for-submit-snapshot")
test_wait_snapshot(fd, WAIT_FOR_SUBMIT);
igt_subtest("wait-all-for-submit-snapshot")
test_wait_snapshot(fd, WAIT_ALL | WAIT_FOR_SUBMIT);
igt_subtest("wait-any-complex")
test_wait_complex(fd, 0);
igt_subtest("wait-all-complex")
test_wait_complex(fd, WAIT_ALL);
igt_subtest("wait-for-submit-complex")
test_wait_complex(fd, WAIT_FOR_SUBMIT);
igt_subtest("wait-all-for-submit-complex")
test_wait_complex(fd, WAIT_ALL | WAIT_FOR_SUBMIT);
igt_subtest("wait-any-interrupted")
test_wait_interrupted(fd, 0);
igt_subtest("wait-all-interrupted")
test_wait_interrupted(fd, WAIT_ALL);
igt_fixture {
drm_close_driver(fd);
}
}
|