• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2011,2012 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 <chris@chris-wilson.co.uk>
25  *    Daniel Vetter <daniel.vetter@ffwll.ch>
26  *
27  */
28 
29 /*
30  * Testcase: run a couple of big batches to force the unbind on misalignment code.
31  */
32 
33 #include "igt.h"
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <string.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 
46 #include <drm.h>
47 
48 
49 IGT_TEST_DESCRIPTION("Run a couple of big batches to force the unbind on"
50 		     " misalignment code.");
51 
52 #define HEIGHT 256
53 #define WIDTH 1024
54 
55 static void
copy(int fd,uint32_t dst,uint32_t src,uint32_t * all_bo,uint64_t n_bo,uint64_t alignment,int error)56 copy(int fd, uint32_t dst, uint32_t src, uint32_t *all_bo,
57      uint64_t n_bo, uint64_t alignment, int error)
58 {
59 	uint32_t batch[12];
60 	struct drm_i915_gem_relocation_entry reloc[2];
61 	struct drm_i915_gem_exec_object2 *obj;
62 	struct drm_i915_gem_execbuffer2 exec;
63 	uint32_t handle;
64 	int n, i=0;
65 
66 	batch[i++] = (XY_SRC_COPY_BLT_CMD |
67 		    XY_SRC_COPY_BLT_WRITE_ALPHA |
68 		    XY_SRC_COPY_BLT_WRITE_RGB | 6);
69 	if (intel_gen(intel_get_drm_devid(fd)) >= 8)
70 		batch[i - 1] += 2;
71 	batch[i++] = (3 << 24) | /* 32 bits */
72 		  (0xcc << 16) | /* copy ROP */
73 		  WIDTH*4;
74 	batch[i++] = 0; /* dst x1,y1 */
75 	batch[i++] = (HEIGHT << 16) | WIDTH; /* dst x2,y2 */
76 	batch[i++] = 0; /* dst reloc */
77 	if (intel_gen(intel_get_drm_devid(fd)) >= 8)
78 		batch[i++] = 0; /* FIXME */
79 	batch[i++] = 0; /* src x1,y1 */
80 	batch[i++] = WIDTH*4;
81 	batch[i++] = 0; /* src reloc */
82 	if (intel_gen(intel_get_drm_devid(fd)) >= 8)
83 		batch[i++] = 0; /* FIXME */
84 	batch[i++] = MI_BATCH_BUFFER_END;
85 	batch[i++] = MI_NOOP;
86 
87 	handle = gem_create(fd, 4096);
88 	gem_write(fd, handle, 0, batch, sizeof(batch));
89 
90 	reloc[0].target_handle = dst;
91 	reloc[0].delta = 0;
92 	reloc[0].offset = 4 * sizeof(batch[0]);
93 	reloc[0].presumed_offset = 0;
94 	reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
95 	reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
96 
97 	reloc[1].target_handle = src;
98 	reloc[1].delta = 0;
99 	reloc[1].offset = 7 * sizeof(batch[0]);
100 	reloc[1].presumed_offset = 0;
101 	reloc[1].read_domains = I915_GEM_DOMAIN_RENDER;
102 	reloc[1].write_domain = 0;
103 
104 	obj = calloc(n_bo + 1, sizeof(*obj));
105 	for (n = 0; n < n_bo; n++) {
106 		obj[n].handle = all_bo[n];
107 		obj[n].alignment = alignment;
108 	}
109 	obj[n].handle = handle;
110 	obj[n].relocation_count = 2;
111 	obj[n].relocs_ptr = to_user_pointer(reloc);
112 
113 	exec.buffers_ptr = to_user_pointer(obj);
114 	exec.buffer_count = n_bo + 1;
115 	exec.batch_start_offset = 0;
116 	exec.batch_len = i * 4;
117 	exec.DR1 = exec.DR4 = 0;
118 	exec.num_cliprects = 0;
119 	exec.cliprects_ptr = 0;
120 	exec.flags = HAS_BLT_RING(intel_get_drm_devid(fd)) ? I915_EXEC_BLT : 0;
121 	i915_execbuffer2_set_context_id(exec, 0);
122 	exec.rsvd2 = 0;
123 
124 	igt_assert_eq(__gem_execbuf(fd, &exec), -error);
125 
126 	gem_close(fd, handle);
127 	free(obj);
128 }
129 
minor_evictions(int fd,uint64_t size,uint64_t count)130 static void minor_evictions(int fd, uint64_t size, uint64_t count)
131 {
132 	uint32_t *bo, *sel;
133 	uint64_t n, m, alignment;
134 	int pass, fail;
135 
136 	intel_require_memory(2 * count, size, CHECK_RAM);
137 
138 	bo = malloc(3*count*sizeof(*bo));
139 	igt_assert(bo);
140 
141 	for (n = 0; n < 2*count; n++)
142 		bo[n] = gem_create(fd, size);
143 
144 	sel = bo + n;
145 	for (alignment = m = 4096; alignment <= size; alignment <<= 1) {
146 		for (fail = 0; fail < 10; fail++) {
147 			for (pass = 0; pass < 100; pass++) {
148 				for (n = 0; n < count; n++, m += 7)
149 					sel[n] = bo[m%(2*count)];
150 				copy(fd, sel[0], sel[1], sel, count, alignment, 0);
151 			}
152 			copy(fd, bo[0], bo[0], bo, 2*count, alignment, ENOSPC);
153 		}
154 	}
155 
156 	for (n = 0; n < 2*count; n++)
157 		gem_close(fd, bo[n]);
158 	free(bo);
159 }
160 
major_evictions(int fd,uint64_t size,uint64_t count)161 static void major_evictions(int fd, uint64_t size, uint64_t count)
162 {
163 	uint64_t n, m, alignment, max;
164 	int loop;
165 	uint32_t *bo;
166 
167 	intel_require_memory(count, size, CHECK_RAM);
168 
169 	bo = malloc(count*sizeof(*bo));
170 	igt_assert(bo);
171 
172 	for (n = 0; n < count; n++)
173 		bo[n] = gem_create(fd, size);
174 
175 	max = gem_aperture_size(fd) - size;
176 	for (alignment = m = 4096; alignment < max; alignment <<= 1) {
177 		for (loop = 0; loop < 100; loop++, m += 17) {
178 			n = m % count;
179 			copy(fd, bo[n], bo[n], &bo[n], 1, alignment, 0);
180 		}
181 	}
182 
183 	for (n = 0; n < count; n++)
184 		gem_close(fd, bo[n]);
185 	free(bo);
186 }
187 
188 #define MAX_32b ((1ull << 32) - 4096)
189 
190 igt_main
191 {
192 	uint64_t size, count;
193 	int fd = -1;
194 
195 	igt_fixture {
196 		fd = drm_open_driver(DRIVER_INTEL);
197 		igt_require_gem(fd);
198 		igt_fork_hang_detector(fd);
199 	}
200 
201 	igt_subtest("minor-normal") {
202 		size = 1024 * 1024;
203 		count = gem_aperture_size(fd);
204 		if (count >> 32)
205 			count = MAX_32b;
206 		count = 3 * count / size / 4;
207 		minor_evictions(fd, size, count);
208 	}
209 
210 	igt_subtest("major-normal") {
211 		size = gem_aperture_size(fd);
212 		if (size >> 32)
213 			size = MAX_32b;
214 		size = 3 * size / 4;
215 		count = 4;
216 		major_evictions(fd, size, count);
217 	}
218 
219 	igt_fixture {
220 		igt_stop_hang_detector();
221 	}
222 
223 	igt_fork_signal_helper();
224 	igt_subtest("minor-interruptible") {
225 		size = 1024 * 1024;
226 		count = gem_aperture_size(fd);
227 		if (count >> 32)
228 			count = MAX_32b;
229 		count = 3 * count / size / 4;
230 		minor_evictions(fd, size, count);
231 	}
232 
233 	igt_subtest("major-interruptible") {
234 		size = gem_aperture_size(fd);
235 		if (size >> 32)
236 			size = MAX_32b;
237 		size = 3 * size / 4;
238 		count = 4;
239 		major_evictions(fd, size, count);
240 	}
241 
242 	igt_subtest("minor-hang") {
243 		igt_fork_hang_helper();
244 		size = 1024 * 1024;
245 		count = gem_aperture_size(fd);
246 		if (count >> 32)
247 			count = MAX_32b;
248 		count = 3 * count / size / 4;
249 		minor_evictions(fd, size, count);
250 	}
251 
252 	igt_subtest("major-hang") {
253 		size = gem_aperture_size(fd);
254 		if (size >> 32)
255 			size = MAX_32b;
256 		size = 3 * size / 4;
257 		count = 4;
258 		major_evictions(fd, size, count);
259 	}
260 	igt_stop_signal_helper();
261 
262 	igt_fixture {
263 		igt_stop_hang_helper();
264 		close(fd);
265 	}
266 }
267