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