s/drmtest_/igt_/
[platform/upstream/intel-gpu-tools.git] / tests / gem_exec_lut_handle.c
1 /*
2  * Copyright © 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  *
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 BATCH_SIZE              (1024*1024)
44
45 #define LOCAL_I915_EXEC_NO_RELOC (1<<11)
46 #define LOCAL_I915_EXEC_HANDLE_LUT (1<<12)
47
48 #define MAX_NUM_EXEC 2048
49 #define MAX_NUM_RELOC 4096
50
51 #define USE_LUT 0x1
52 #define SKIP_RELOC 0x2
53 #define NO_RELOC 0x4
54
55 struct drm_i915_gem_exec_object2 gem_exec[MAX_NUM_EXEC+1];
56 struct drm_i915_gem_relocation_entry gem_reloc[MAX_NUM_RELOC];
57
58 static uint32_t state = 0x12345678;
59
60 static uint32_t
61 hars_petruska_f54_1_random (void)
62 {
63 #define rol(x,k) ((x << k) | (x >> (32-k)))
64     return state = (state ^ rol (state, 5) ^ rol (state, 24)) + 0x37798849;
65 #undef rol
66 }
67
68
69 static int exec(int fd, int num_exec, int num_relocs, unsigned flags)
70 {
71         struct drm_i915_gem_execbuffer2 execbuf;
72         struct drm_i915_gem_exec_object2 *objects;
73         int i;
74
75         gem_exec[MAX_NUM_EXEC].relocation_count = num_relocs;
76         gem_exec[MAX_NUM_EXEC].relocs_ptr = (uintptr_t) gem_reloc;
77
78         objects = gem_exec + MAX_NUM_EXEC - num_exec;
79
80         for (i = 0; i < num_relocs; i++) {
81                 int target = hars_petruska_f54_1_random() % num_exec;
82                 gem_reloc[i].offset = 1024;
83                 gem_reloc[i].delta = 0;
84                 gem_reloc[i].target_handle =
85                         flags & USE_LUT ? target : objects[target].handle;
86                 gem_reloc[i].read_domains = I915_GEM_DOMAIN_RENDER;
87                 gem_reloc[i].write_domain = 0;
88                 gem_reloc[i].presumed_offset = 0;
89                 if (flags & SKIP_RELOC)
90                         gem_reloc[i].presumed_offset = objects[target].offset;
91         }
92
93         execbuf.buffers_ptr = (uintptr_t)objects;
94         execbuf.buffer_count = num_exec + 1;
95         execbuf.batch_start_offset = 0;
96         execbuf.batch_len = 8;
97         execbuf.cliprects_ptr = 0;
98         execbuf.num_cliprects = 0;
99         execbuf.DR1 = 0;
100         execbuf.DR4 = 0;
101         execbuf.flags = 0;
102         if (flags & USE_LUT)
103                 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
104         if (flags & NO_RELOC)
105                 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
106         i915_execbuffer2_set_context_id(execbuf, 0);
107         execbuf.rsvd2 = 0;
108
109         return drmIoctl(fd,
110                         DRM_IOCTL_I915_GEM_EXECBUFFER2,
111                         &execbuf);
112 }
113
114 #define ELAPSED(a,b) (1e6*((b)->tv_sec - (a)->tv_sec) + ((b)->tv_usec - (a)->tv_usec))
115 int main(int argc, char **argv)
116 {
117         uint32_t batch[2] = {MI_BATCH_BUFFER_END};
118         int fd, n, m, count;
119         const struct {
120                 const char *name;
121                 unsigned int flags;
122         } pass[] = {
123                 { .name = "relocation", .flags = 0 },
124                 { .name = "skip-relocs", .flags = SKIP_RELOC },
125                 { .name = "no-relocs", .flags = NO_RELOC },
126                 { .name = NULL },
127         }, *p;
128
129         igt_skip_on_simulation();
130
131         fd = drm_open_any();
132
133         for (n = 0; n < MAX_NUM_EXEC; n++) {
134                 gem_exec[n].handle = gem_create(fd, 4096);
135                 gem_exec[n].relocation_count = 0;
136                 gem_exec[n].relocs_ptr = 0;
137                 gem_exec[n].alignment = 0;
138                 gem_exec[n].offset = 0;
139                 gem_exec[n].flags = 0;
140                 gem_exec[n].rsvd1 = 0;
141                 gem_exec[n].rsvd2 = 0;
142         }
143
144         gem_exec[n].handle =  gem_create(fd, 4096);
145         gem_write(fd, gem_exec[n].handle, 0, batch, sizeof(batch));
146
147         if (exec(fd, 1, 0, USE_LUT))
148                 return 77;
149
150         for (p = pass; p->name != NULL; p++) {
151                 for (n = 1; n <= MAX_NUM_EXEC; n *= 2) {
152                         for (m = 1; m <= MAX_NUM_RELOC; m *= 2) {
153                                 struct timeval start, end;
154                                 double elapsed[2];
155
156                                 gettimeofday(&start, NULL);
157                                 for (count = 0; count < 1000; count++)
158                                         do_or_die(exec(fd, n, m, 0 | p->flags));
159                                 gettimeofday(&end, NULL);
160                                 gem_sync(fd, gem_exec[MAX_NUM_EXEC].handle);
161                                 elapsed[0] = ELAPSED(&start, &end) / 1000.;
162
163                                 gettimeofday(&start, NULL);
164                                 for (count = 0; count < 1000; count++)
165                                         do_or_die(exec(fd, n, m, USE_LUT | p->flags));
166                                 gettimeofday(&end, NULL);
167                                 gem_sync(fd, gem_exec[MAX_NUM_EXEC].handle);
168                                 elapsed[1] = ELAPSED(&start, &end) / 1000.;
169
170                                 printf("%s: buffer_count=%d, reloc_count=%d: old=%f us, lut=%f us\n",
171                                        p->name, n, m, elapsed[0], elapsed[1]);
172                         }
173                 }
174         }
175
176         return 0;
177 }