1 /*
2  * Copyright © 2016 Broadcom
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "igt.h"
25 #include "igt_vc4.h"
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <inttypes.h>
32 #include <errno.h>
33 #include <sys/stat.h>
34 #include <sys/ioctl.h>
35 #include "vc4_drm.h"
36 
37 static void
test_used_bo(int fd,uint64_t timeout)38 test_used_bo(int fd, uint64_t timeout)
39 {
40 	size_t size = 4096;
41 	uint32_t clearval = 0xaabbccdd + timeout;
42 	int handle = igt_vc4_get_cleared_bo(fd, size, clearval);
43 	struct drm_vc4_wait_bo wait = {
44 		.timeout_ns = timeout,
45 		.handle = handle,
46 	};
47 	int ret, i;
48 
49 	ret = ioctl(fd, DRM_IOCTL_VC4_WAIT_BO, &wait);
50 	if (timeout == ~0ull) {
51 		igt_assert_eq_u32(ret, 0);
52 	} else {
53 		if (ret == -1 && errno == ETIME)
54 			igt_debug("Timeout triggered\n");
55 		igt_assert(ret == 0 || (ret == -1 && errno == ETIME));
56 	}
57 
58 	if (ret == 0) {
59 		uint32_t *map = igt_vc4_mmap_bo(fd, handle, size, PROT_READ);
60 		for (i = 0; i < size / 4; i++) {
61 			igt_assert_eq_u32(map[i], clearval);
62 		}
63 		munmap((void *)map, size);
64 	}
65 
66 	gem_close(fd, handle);
67 }
68 
69 igt_main
70 {
71 	int fd;
72 	int bo_handle;
73 
74 	igt_fixture {
75 		fd = drm_open_driver(DRIVER_VC4);
76 		bo_handle = igt_vc4_create_bo(fd, 4096);
77 	}
78 
79 	igt_subtest("bad-bo") {
80 		struct drm_vc4_wait_bo arg = {
81 			.handle = bo_handle + 1,
82 			.timeout_ns = 0,
83 		};
84 		do_ioctl_err(fd, DRM_IOCTL_VC4_WAIT_BO, &arg, EINVAL);
85 	}
86 
87 	igt_subtest("bad-pad") {
88 		struct drm_vc4_wait_bo arg = {
89 			.pad = 1,
90 			.handle = bo_handle,
91 			.timeout_ns = 0,
92 		};
93 		do_ioctl_err(fd, DRM_IOCTL_VC4_WAIT_BO, &arg, EINVAL);
94 	}
95 
96 	igt_subtest("unused-bo-0ns") {
97 		struct drm_vc4_wait_bo arg = {
98 			.handle = bo_handle,
99 			.timeout_ns = 0,
100 		};
101 		do_ioctl(fd, DRM_IOCTL_VC4_WAIT_BO, &arg);
102 	}
103 
104 	igt_subtest("unused-bo-1ns") {
105 		struct drm_vc4_wait_bo arg = {
106 			.handle = bo_handle,
107 			.timeout_ns = 1,
108 		};
109 		do_ioctl(fd, DRM_IOCTL_VC4_WAIT_BO, &arg);
110 	}
111 
112 	igt_subtest("used-bo-0ns")
113 		test_used_bo(fd, 0);
114 
115 	igt_subtest("used-bo-1ns")
116 		test_used_bo(fd, 1);
117 
118 	igt_subtest("used-bo")
119 		test_used_bo(fd, ~0ull);
120 
121 	igt_fixture
122 		close(fd);
123 }
124