tests/gem_lut_handle: Expand negative testing
[platform/upstream/intel-gpu-tools.git] / tests / gem_lut_handle.c
1 /*
2  * Copyright © 2012,2013 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  *
26  */
27
28 /* Exercises the basic execbuffer using theh andle LUT interface */
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <assert.h>
34 #include <fcntl.h>
35 #include <inttypes.h>
36 #include <errno.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include "drm.h"
40 #include "i915_drm.h"
41 #include "drmtest.h"
42
43 #define MI_BATCH_BUFFER_END     (0xA<<23)
44 #define BATCH_SIZE              (1024*1024)
45
46 #define LOCAL_I915_EXEC_HANDLE_LUT (1<<12)
47
48 #define NORMAL 0
49 #define USE_LUT 0x1
50 #define BROKEN 0x2
51
52 static int exec(int fd, uint32_t handle, unsigned int flags)
53 {
54         struct drm_i915_gem_execbuffer2 execbuf;
55         struct drm_i915_gem_exec_object2 gem_exec[1];
56         struct drm_i915_gem_relocation_entry gem_reloc[1];
57
58         gem_reloc[0].offset = 1024;
59         gem_reloc[0].delta = 0;
60         gem_reloc[0].target_handle =
61                 !!(flags & USE_LUT) ^ !!(flags & BROKEN) ? 0 : handle;
62         gem_reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
63         gem_reloc[0].write_domain = 0;
64         gem_reloc[0].presumed_offset = 0;
65
66         gem_exec[0].handle = handle;
67         gem_exec[0].relocation_count = 1;
68         gem_exec[0].relocs_ptr = (uintptr_t) gem_reloc;
69         gem_exec[0].alignment = 0;
70         gem_exec[0].offset = 0;
71         gem_exec[0].flags = 0;
72         gem_exec[0].rsvd1 = 0;
73         gem_exec[0].rsvd2 = 0;
74
75         execbuf.buffers_ptr = (uintptr_t)gem_exec;
76         execbuf.buffer_count = 1;
77         execbuf.batch_start_offset = 0;
78         execbuf.batch_len = 8;
79         execbuf.cliprects_ptr = 0;
80         execbuf.num_cliprects = 0;
81         execbuf.DR1 = 0;
82         execbuf.DR4 = 0;
83         execbuf.flags = flags & USE_LUT ? LOCAL_I915_EXEC_HANDLE_LUT : 0;
84         i915_execbuffer2_set_context_id(execbuf, 0);
85         execbuf.rsvd2 = 0;
86
87         return drmIoctl(fd,
88                         DRM_IOCTL_I915_GEM_EXECBUFFER2,
89                         &execbuf);
90 }
91
92 static int many_exec(int fd, uint32_t batch, int num_exec, int num_reloc, unsigned flags)
93 {
94         struct drm_i915_gem_execbuffer2 execbuf;
95         struct drm_i915_gem_exec_object2 *gem_exec;
96         struct drm_i915_gem_relocation_entry *gem_reloc;
97         unsigned min_handle = 0, max_handle = ~0;
98         int ret, n;
99
100         gem_exec = calloc(num_exec+1, sizeof(*gem_exec));
101         gem_reloc = calloc(num_reloc, sizeof(*gem_reloc));
102         assert(gem_exec && gem_reloc);
103
104         for (n = 0; n < num_exec; n++) {
105                 gem_exec[n].handle = gem_create(fd, 4096);
106                 if (gem_exec[n].handle < min_handle)
107                         min_handle = gem_exec[n].handle;
108                 if (gem_exec[n].handle > max_handle)
109                         max_handle = gem_exec[n].handle;
110                 gem_exec[n].relocation_count = 0;
111                 gem_exec[n].relocs_ptr = 0;
112                 gem_exec[n].alignment = 0;
113                 gem_exec[n].offset = 0;
114                 gem_exec[n].flags = 0;
115                 gem_exec[n].rsvd1 = 0;
116                 gem_exec[n].rsvd2 = 0;
117         }
118
119         gem_exec[n].handle = batch;
120         gem_exec[n].relocation_count = num_reloc;
121         gem_exec[n].relocs_ptr = (uintptr_t) gem_reloc;
122
123         if (flags & USE_LUT) {
124                 min_handle = 0;
125                 max_handle = num_exec + 1;
126         }
127
128         for (n = 0; n < num_reloc; n++) {
129                 unsigned target;
130
131                 if (flags & BROKEN) {
132                         target = rand();
133                         if (target <= max_handle && target >= min_handle)
134                                 target = target & 1 ? min_handle - target : max_handle + target;
135                 } else {
136                         target = rand() % (num_exec + 1);
137                         if ((flags & USE_LUT) == 0)
138                                 target = gem_exec[target].handle;
139                 }
140
141                 gem_reloc[n].offset = 1024;
142                 gem_reloc[n].delta = 0;
143                 gem_reloc[n].target_handle = target;
144                 gem_reloc[n].read_domains = I915_GEM_DOMAIN_RENDER;
145                 gem_reloc[n].write_domain = 0;
146                 gem_reloc[n].presumed_offset = 0;
147         }
148
149         execbuf.buffers_ptr = (uintptr_t)gem_exec;
150         execbuf.buffer_count = num_exec + 1;
151         execbuf.batch_start_offset = 0;
152         execbuf.batch_len = 8;
153         execbuf.cliprects_ptr = 0;
154         execbuf.num_cliprects = 0;
155         execbuf.DR1 = 0;
156         execbuf.DR4 = 0;
157         execbuf.flags = flags & USE_LUT ? LOCAL_I915_EXEC_HANDLE_LUT : 0;
158         i915_execbuffer2_set_context_id(execbuf, 0);
159         execbuf.rsvd2 = 0;
160
161         ret = drmIoctl(fd,
162                        DRM_IOCTL_I915_GEM_EXECBUFFER2,
163                        &execbuf);
164
165         for (n = 0; n < num_exec; n++)
166                 gem_close(fd, gem_exec[n].handle);
167
168         free(gem_exec);
169         free(gem_reloc);
170
171         return ret;
172 }
173
174 #define fail(x) assert(x == -1 && errno == ENOENT)
175
176 int main(int argc, char **argv)
177 {
178         uint32_t batch[2] = {MI_BATCH_BUFFER_END};
179         uint32_t handle;
180         int fd, i;
181
182         fd = drm_open_any();
183
184         handle = gem_create(fd, 4096);
185         gem_write(fd, handle, 0, batch, sizeof(batch));
186
187         do_or_die(exec(fd, handle, NORMAL));
188         do_or_die(exec(fd, handle, USE_LUT));
189
190         fail(exec(fd, handle, BROKEN));
191         fail(exec(fd, handle, USE_LUT | BROKEN));
192
193         for (i = 2; i <= 65536; i *= 2) {
194                 do_or_die(many_exec(fd, handle, i-1, i-1, NORMAL));
195                 do_or_die(many_exec(fd, handle, i-1, i, NORMAL));
196                 do_or_die(many_exec(fd, handle, i-1, i+1, NORMAL));
197                 do_or_die(many_exec(fd, handle, i, i-1, NORMAL));
198                 do_or_die(many_exec(fd, handle, i, i, NORMAL));
199                 do_or_die(many_exec(fd, handle, i, i+1, NORMAL));
200                 do_or_die(many_exec(fd, handle, i+1, i-1, NORMAL));
201                 do_or_die(many_exec(fd, handle, i+1, i, NORMAL));
202                 do_or_die(many_exec(fd, handle, i+1, i+1, NORMAL));
203
204                 fail(many_exec(fd, handle, i-1, i-1, NORMAL | BROKEN));
205                 fail(many_exec(fd, handle, i-1, i, NORMAL | BROKEN));
206                 fail(many_exec(fd, handle, i-1, i+1, NORMAL | BROKEN));
207                 fail(many_exec(fd, handle, i, i-1, NORMAL | BROKEN));
208                 fail(many_exec(fd, handle, i, i, NORMAL | BROKEN));
209                 fail(many_exec(fd, handle, i, i+1, NORMAL | BROKEN));
210                 fail(many_exec(fd, handle, i+1, i-1, NORMAL | BROKEN));
211                 fail(many_exec(fd, handle, i+1, i, NORMAL | BROKEN));
212                 fail(many_exec(fd, handle, i+1, i+1, NORMAL | BROKEN));
213
214                 do_or_die(many_exec(fd, handle, i-1, i-1, USE_LUT));
215                 do_or_die(many_exec(fd, handle, i-1, i, USE_LUT));
216                 do_or_die(many_exec(fd, handle, i-1, i+1, USE_LUT));
217                 do_or_die(many_exec(fd, handle, i, i-1, USE_LUT));
218                 do_or_die(many_exec(fd, handle, i, i, USE_LUT));
219                 do_or_die(many_exec(fd, handle, i, i+1, USE_LUT));
220                 do_or_die(many_exec(fd, handle, i+1, i-1, USE_LUT));
221                 do_or_die(many_exec(fd, handle, i+1, i, USE_LUT));
222                 do_or_die(many_exec(fd, handle, i+1, i+1, USE_LUT));
223
224                 fail(many_exec(fd, handle, i-1, i-1, USE_LUT | BROKEN));
225                 fail(many_exec(fd, handle, i-1, i, USE_LUT | BROKEN));
226                 fail(many_exec(fd, handle, i-1, i+1, USE_LUT | BROKEN));
227                 fail(many_exec(fd, handle, i, i-1, USE_LUT | BROKEN));
228                 fail(many_exec(fd, handle, i, i, USE_LUT | BROKEN));
229                 fail(many_exec(fd, handle, i, i+1, USE_LUT | BROKEN));
230                 fail(many_exec(fd, handle, i+1, i-1, USE_LUT | BROKEN));
231                 fail(many_exec(fd, handle, i+1, i, USE_LUT | BROKEN));
232                 fail(many_exec(fd, handle, i+1, i+1, USE_LUT | BROKEN));
233         }
234
235         return 0;
236 }