xref: /aosp_15_r20/external/igt-gpu-tools/tests/drm_read.c (revision d83cc019efdc2edc6c4b16e9034a3ceb8d35d77c)
1 /*
2  * Copyright © 2014 Intel Corporation
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  * Authors:
24  *    Chris Wilson <[email protected]>
25  *
26  */
27 
28 /*
29  * Testcase: boundary testing of read(drm_fd)
30  */
31 
32 #include "igt.h"
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <signal.h>
39 #include <fcntl.h>
40 #include <inttypes.h>
41 #include <errno.h>
42 #include <sys/stat.h>
43 #include <sys/ioctl.h>
44 #include <sys/time.h>
45 #include <sys/poll.h>
46 #include <pthread.h>
47 #include <sched.h>
48 #include "drm.h"
49 
50 IGT_TEST_DESCRIPTION("Call read(drm) and see if it behaves.");
51 
sighandler(int sig)52 static void sighandler(int sig)
53 {
54 }
55 
assert_empty(int fd)56 static void assert_empty(int fd)
57 {
58 	struct pollfd pfd = {fd, POLLIN};
59 	do_or_die(poll(&pfd, 1, 0));
60 }
61 
generate_event(int fd,enum pipe pipe)62 static void generate_event(int fd, enum pipe pipe)
63 {
64 	igt_assert(kmstest_get_vblank(fd, pipe, DRM_VBLANK_EVENT));
65 }
66 
wait_for_event(int fd)67 static void wait_for_event(int fd)
68 {
69 	struct pollfd pfd = {fd, POLLIN};
70 	igt_assert(poll(&pfd, 1, -1) == 1);
71 }
72 
setup(int in,int nonblock)73 static int setup(int in, int nonblock)
74 {
75 	int fd;
76 	int ret = -1;
77 
78 	alarm(0);
79 
80 	fd = dup(in);
81 	if (fd != -1)
82 		ret = fcntl(fd, F_GETFL);
83 	if (ret != -1) {
84 		if (nonblock)
85 			ret |= O_NONBLOCK;
86 		else
87 			ret &= ~O_NONBLOCK;
88 		ret = fcntl(fd, F_SETFL, ret);
89 	}
90 	igt_require(ret != -1);
91 
92 	assert_empty(fd);
93 	return fd;
94 }
95 
teardown(int fd)96 static void teardown(int fd)
97 {
98 	alarm(0);
99 	assert_empty(fd);
100 	close(fd);
101 	errno = 0;
102 }
103 
test_invalid_buffer(int in)104 static void test_invalid_buffer(int in)
105 {
106 	int fd = setup(in, 0);
107 
108 	alarm(1);
109 
110 	igt_assert_eq(read(fd, (void *)-1, 0), -1);
111 	igt_assert_eq(errno, EFAULT);
112 
113 	teardown(fd);
114 }
115 
test_fault_buffer(int in,enum pipe pipe)116 static void test_fault_buffer(int in, enum pipe pipe)
117 {
118 	int fd = setup(in, 0);
119 	struct drm_mode_map_dumb arg;
120 	char *buf;
121 
122 	memset(&arg, 0, sizeof(arg));
123 	arg.handle = kmstest_dumb_create(fd, 32, 32, 32, NULL, NULL);
124 
125 	do_ioctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
126 
127 	buf = mmap(0, 4096, PROT_WRITE, MAP_SHARED, fd, arg.offset);
128 	igt_assert(buf != MAP_FAILED);
129 
130 	generate_event(fd, pipe);
131 
132 	alarm(1);
133 
134 	igt_assert(read(fd, buf, 4096) > 0);
135 
136 	munmap(buf, 4096);
137 	teardown(fd);
138 }
139 
test_empty(int in,int nonblock,int expected)140 static void test_empty(int in, int nonblock, int expected)
141 {
142 	char buffer[1024];
143 	int fd = setup(in, nonblock);
144 
145 	alarm(1);
146 	igt_assert_eq(read(fd, buffer, sizeof(buffer)), -1);
147 	igt_assert_eq(errno, expected);
148 
149 	teardown(fd);
150 }
151 
test_short_buffer(int in,int nonblock,enum pipe pipe)152 static void test_short_buffer(int in, int nonblock, enum pipe pipe)
153 {
154 	char buffer[1024]; /* events are typically 32 bytes */
155 	int fd = setup(in, nonblock);
156 
157 	generate_event(fd, pipe);
158 	generate_event(fd, pipe);
159 
160 	wait_for_event(fd);
161 
162 	alarm(3);
163 
164 	igt_assert_eq(read(fd, buffer, 4), 0);
165 	igt_assert(read(fd, buffer, 40) > 0);
166 	igt_assert(read(fd, buffer, 40) > 0);
167 
168 	teardown(fd);
169 }
170 
171 struct short_buffer_wakeup {
172 	pthread_mutex_t mutex;
173 	pthread_cond_t send, recv;
174 	int counter;
175 	int done;
176 	int fd;
177 };
178 
thread_short_buffer_wakeup(void * arg)179 static void *thread_short_buffer_wakeup(void *arg)
180 {
181 	struct short_buffer_wakeup *w = arg;
182 	char buffer; /* events are typically 32 bytes */
183 
184 	while (!w->done) {
185 		/* Short read, does not consume the event. */
186 		igt_assert_eq(read(w->fd, &buffer, sizeof(buffer)), 0);
187 
188 		pthread_mutex_lock(&w->mutex);
189 		if (!--w->counter)
190 			pthread_cond_signal(&w->send);
191 		pthread_cond_wait(&w->recv, &w->mutex);
192 		pthread_mutex_unlock(&w->mutex);
193 	}
194 
195 	return NULL;
196 }
197 
test_short_buffer_wakeup(int in,enum pipe pipe)198 static void test_short_buffer_wakeup(int in, enum pipe pipe)
199 {
200 	const int nt = sysconf(_SC_NPROCESSORS_ONLN) + 1;
201 	struct short_buffer_wakeup w = {
202 		.fd = setup(in, 0),
203 	};
204 	pthread_t t[nt];
205 	char buffer[1024]; /* events are typically 32 bytes */
206 
207 	pthread_mutex_init(&w.mutex, NULL);
208 	pthread_cond_init(&w.send, NULL);
209 	pthread_cond_init(&w.recv, NULL);
210 
211 	for (int n = 0; n < nt; n++)
212 		pthread_create(&t[n], NULL, thread_short_buffer_wakeup, &w);
213 
214 	igt_until_timeout(30) {
215 		struct timespec tv;
216 		int err = 0;
217 
218 		pthread_mutex_lock(&w.mutex);
219 		w.counter = nt;
220 		pthread_cond_broadcast(&w.recv);
221 		pthread_mutex_unlock(&w.mutex);
222 
223 		/* Give each thread a chance to sleep in drm_read() */
224 		sched_yield();
225 
226 		/* One event should wake all threads as none consume */
227 		generate_event(w.fd, pipe);
228 
229 		clock_gettime(CLOCK_REALTIME, &tv);
230 		tv.tv_sec += 5; /* Let's be very generous to the scheduler */
231 
232 		pthread_mutex_lock(&w.mutex);
233 		while (w.counter && !err)
234 			err = pthread_cond_timedwait(&w.send, &w.mutex, &tv);
235 		pthread_mutex_unlock(&w.mutex);
236 
237 		igt_assert_f(err == 0,
238 			     "Timed out waiting for drm_read() to wakeup on an event\n");
239 
240 		/* No thread should consume the event */
241 		igt_assert(read(w.fd, buffer, sizeof(buffer)) > 0);
242 	}
243 	pthread_mutex_lock(&w.mutex);
244 	w.done = true;
245 	pthread_cond_broadcast(&w.recv);
246 	pthread_mutex_unlock(&w.mutex);
247 
248 	for (int n = 0; n < nt; n++)
249 		pthread_join(t[n], NULL);
250 
251 	close(w.fd);
252 }
253 
254 igt_main
255 {
256 	int fd;
257 	igt_display_t display;
258 	struct igt_fb fb;
259 	enum pipe pipe;
260 
261 	signal(SIGALRM, sighandler);
262 	siginterrupt(SIGALRM, 1);
263 
264 	igt_fixture {
265 		igt_output_t *output;
266 
267 		fd = drm_open_driver_master(DRIVER_ANY);
268 		kmstest_set_vt_graphics_mode();
269 
270 		igt_display_require(&display, fd);
271 		igt_display_require_output(&display);
272 
273 		for_each_pipe_with_valid_output(&display, pipe, output) {
274 			drmModeModeInfo *mode = igt_output_get_mode(output);
275 
276 			igt_create_pattern_fb(fd, mode->hdisplay, mode->vdisplay,
277 					      DRM_FORMAT_XRGB8888,
278 					      LOCAL_DRM_FORMAT_MOD_NONE, &fb);
279 
280 			igt_output_set_pipe(output, pipe);
281 			igt_plane_set_fb(igt_output_get_plane_type(output, DRM_PLANE_TYPE_PRIMARY), &fb);
282 			break;
283 		}
284 
285 		igt_display_commit2(&display, display.is_atomic ? COMMIT_ATOMIC : COMMIT_LEGACY);
286 		igt_require(kmstest_get_vblank(fd, pipe, 0));
287 	}
288 
289 	igt_subtest("invalid-buffer")
290 		test_invalid_buffer(fd);
291 
292 	igt_subtest("fault-buffer")
293 		test_fault_buffer(fd, pipe);
294 
295 	igt_subtest("empty-block")
296 		test_empty(fd, 0, EINTR);
297 
298 	igt_subtest("empty-nonblock")
299 		test_empty(fd, 1, EAGAIN);
300 
301 	igt_subtest("short-buffer-block")
302 		test_short_buffer(fd, 0, pipe);
303 
304 	igt_subtest("short-buffer-nonblock")
305 		test_short_buffer(fd, 1, pipe);
306 
307 	igt_subtest("short-buffer-wakeup")
308 		test_short_buffer_wakeup(fd, pipe);
309 }
310