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 
24 #include "gem_ring.h"
25 
26 #include <signal.h>
27 #include <sys/ioctl.h>
28 #include <sys/time.h>
29 
30 #include "intel_reg.h"
31 #include "drmtest.h"
32 #include "ioctl_wrappers.h"
33 #include "igt_dummyload.h"
34 #include "igt_gt.h"
35 
__execbuf(int fd,struct drm_i915_gem_execbuffer2 * execbuf)36 static int __execbuf(int fd, struct drm_i915_gem_execbuffer2 *execbuf)
37 {
38 	int err;
39 
40 	err = 0;
41 	if (ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, execbuf))
42 		err = -errno;
43 
44 	errno = 0;
45 	return err;
46 }
47 
alarm_handler(int sig)48 static void alarm_handler(int sig)
49 {
50 }
51 
52 static unsigned int
__gem_measure_ring_inflight(int fd,unsigned int engine,enum measure_ring_flags flags)53 __gem_measure_ring_inflight(int fd, unsigned int engine, enum measure_ring_flags flags)
54 {
55 	struct sigaction old_sa, sa = { .sa_handler = alarm_handler };
56 	struct drm_i915_gem_exec_object2 obj[2];
57 	struct drm_i915_gem_execbuffer2 execbuf;
58 	const uint32_t bbe = MI_BATCH_BUFFER_END;
59 	unsigned int last[2]= { -1, -1 }, count;
60 	struct itimerval itv;
61 	IGT_CORK_HANDLE(cork);
62 
63 	memset(obj, 0, sizeof(obj));
64 	obj[1].handle = gem_create(fd, 4096);
65 	gem_write(fd, obj[1].handle, 0, &bbe, sizeof(bbe));
66 
67 	memset(&execbuf, 0, sizeof(execbuf));
68 	execbuf.buffers_ptr = to_user_pointer(&obj[1]);
69 	execbuf.buffer_count = 1;
70 	execbuf.flags = engine;
71 	gem_execbuf(fd, &execbuf);
72 	gem_sync(fd, obj[1].handle);
73 
74 	obj[0].handle = igt_cork_plug(&cork, fd);
75 
76 	execbuf.buffers_ptr = to_user_pointer(obj);
77 	execbuf.buffer_count = 2;
78 
79 	if (flags & MEASURE_RING_NEW_CTX)
80 		execbuf.rsvd1 = gem_context_create(fd);
81 
82 	sigaction(SIGALRM, &sa, &old_sa);
83 	itv.it_interval.tv_sec = 0;
84 	itv.it_interval.tv_usec = 1000;
85 	itv.it_value.tv_sec = 0;
86 	itv.it_value.tv_usec = 10000;
87 	setitimer(ITIMER_REAL, &itv, NULL);
88 
89 	count = 0;
90 	do {
91 		if (__execbuf(fd, &execbuf) == 0) {
92 			count++;
93 			continue;
94 		}
95 
96 		if (last[1] == count)
97 			break;
98 
99 		/* sleep until the next timer interrupt (woken on signal) */
100 		pause();
101 		last[1] = last[0];
102 		last[0] = count;
103 	} while (1);
104 
105 	igt_assert_eq(__execbuf(fd, &execbuf), -EINTR);
106 	igt_assert(count > 1);
107 
108 	memset(&itv, 0, sizeof(itv));
109 	setitimer(ITIMER_REAL, &itv, NULL);
110 	sigaction(SIGALRM, &old_sa, NULL);
111 
112 	igt_cork_unplug(&cork);
113 	gem_close(fd, obj[0].handle);
114 	gem_close(fd, obj[1].handle);
115 
116 	if (flags & MEASURE_RING_NEW_CTX)
117 		gem_context_destroy(fd, execbuf.rsvd1);
118 
119 	gem_quiescent_gpu(fd);
120 
121 	/* Be conservative in case we must wrap later */
122 	return count - 1;
123 }
124 
125 /**
126  * gem_measure_ring_inflight:
127  * @fd: open i915 drm file descriptor
128  * @engine: execbuf engine flag. Use macro ALL_ENGINES to get the minimum
129  *			size across all physical engines.
130  * @flags: flags to affect measurement:
131  *		- MEASURE_RING_NEW_CTX: use a new context to account for the space
132  *		  used by the lrc init.
133  *
134  * This function calculates the maximum number of batches that can be inserted
135  * at the same time in the ring on the selected engine.
136  *
137  * Returns:
138  * Number of batches that fit in the ring
139  */
140 unsigned int
gem_measure_ring_inflight(int fd,unsigned int engine,enum measure_ring_flags flags)141 gem_measure_ring_inflight(int fd, unsigned int engine, enum measure_ring_flags flags)
142 {
143 	if (engine == ALL_ENGINES) {
144 		unsigned int global_min = ~0u;
145 
146 		for_each_physical_engine(fd, engine) {
147 			unsigned int engine_min = __gem_measure_ring_inflight(fd, engine, flags);
148 
149 			if (engine_min < global_min)
150 				global_min = engine_min;
151 		}
152 
153 		return global_min;
154 	}
155 
156 	return __gem_measure_ring_inflight(fd, engine, flags);
157 }
158