58f77b90182fbb3ef959f31ae4a94829aebf0432
[platform/upstream/intel-gpu-tools.git] / tests / gem_dummy_reloc_loop.c
1 /*
2  * Copyright © 2011 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  *    Daniel Vetter <daniel.vetter@ffwll.ch> (based on gem_storedw_*.c)
25  *
26  */
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <fcntl.h>
33 #include <inttypes.h>
34 #include <errno.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include "drm.h"
38 #include "i915_drm.h"
39 #include "drmtest.h"
40 #include "intel_bufmgr.h"
41 #include "intel_batchbuffer.h"
42 #include "intel_gpu_tools.h"
43 #include "i830_reg.h"
44
45 #define LOCAL_I915_EXEC_VEBOX (4<<0)
46
47 static drm_intel_bufmgr *bufmgr;
48 struct intel_batchbuffer *batch;
49 static drm_intel_bo *target_buffer;
50
51 /*
52  * Testcase: Basic check of ring<->cpu sync using a dummy reloc
53  *
54  * The last test (that randomly switches the ring) seems to be pretty effective
55  * at hitting the missed irq bug that's worked around with the HWSTAM irq write.
56  */
57
58
59 #define MI_COND_BATCH_BUFFER_END        (0x36<<23 | 1)
60 #define MI_DO_COMPARE                   (1<<21)
61 static void
62 dummy_reloc_loop(int ring)
63 {
64         int i;
65
66         for (i = 0; i < 0x100000; i++) {
67                 if (ring == I915_EXEC_RENDER) {
68                         BEGIN_BATCH(4);
69                         OUT_BATCH(MI_COND_BATCH_BUFFER_END | MI_DO_COMPARE);
70                         OUT_BATCH(0xffffffff); /* compare dword */
71                         OUT_RELOC(target_buffer, I915_GEM_DOMAIN_RENDER,
72                                         I915_GEM_DOMAIN_RENDER, 0);
73                         OUT_BATCH(MI_NOOP);
74                         ADVANCE_BATCH();
75                 } else {
76                         BEGIN_BATCH(4);
77                         OUT_BATCH(MI_FLUSH_DW | 1);
78                         OUT_BATCH(0); /* reserved */
79                         OUT_RELOC(target_buffer, I915_GEM_DOMAIN_RENDER,
80                                         I915_GEM_DOMAIN_RENDER, 0);
81                         OUT_BATCH(MI_NOOP | (1<<22) | (0xf));
82                         ADVANCE_BATCH();
83                 }
84                 intel_batchbuffer_flush_on_ring(batch, ring);
85
86                 drm_intel_bo_map(target_buffer, 0);
87                 // map to force completion
88                 drm_intel_bo_unmap(target_buffer);
89         }
90 }
91
92 static void
93 dummy_reloc_loop_random_ring(int num_rings)
94 {
95         int i;
96
97         srandom(0xdeadbeef);
98
99         for (i = 0; i < 0x100000; i++) {
100                 int ring = random() % num_rings + 1;
101
102                 if (ring == I915_EXEC_RENDER) {
103                         BEGIN_BATCH(4);
104                         OUT_BATCH(MI_COND_BATCH_BUFFER_END | MI_DO_COMPARE);
105                         OUT_BATCH(0xffffffff); /* compare dword */
106                         OUT_RELOC(target_buffer, I915_GEM_DOMAIN_RENDER,
107                                         I915_GEM_DOMAIN_RENDER, 0);
108                         OUT_BATCH(MI_NOOP);
109                         ADVANCE_BATCH();
110                 } else {
111                         BEGIN_BATCH(4);
112                         OUT_BATCH(MI_FLUSH_DW | 1);
113                         OUT_BATCH(0); /* reserved */
114                         OUT_RELOC(target_buffer, I915_GEM_DOMAIN_RENDER,
115                                         I915_GEM_DOMAIN_RENDER, 0);
116                         OUT_BATCH(MI_NOOP | (1<<22) | (0xf));
117                         ADVANCE_BATCH();
118                 }
119                 intel_batchbuffer_flush_on_ring(batch, ring);
120
121                 drm_intel_bo_map(target_buffer, 0);
122                 // map to force waiting on rendering
123                 drm_intel_bo_unmap(target_buffer);
124         }
125 }
126
127 int main(int argc, char **argv)
128 {
129         int fd;
130         int devid;
131         int num_rings;
132
133         igt_subtest_init(argc, argv);
134         igt_skip_on_simulation();
135
136         fd = drm_open_any();
137         devid = intel_get_drm_devid(fd);
138         num_rings = gem_get_num_rings(fd);
139         if (!HAS_BLT_RING(devid)) {
140                 fprintf(stderr, "not (yet) implemented for pre-snb\n");
141                 return 77;
142         }
143
144         bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
145         if (!bufmgr) {
146                 fprintf(stderr, "failed to init libdrm\n");
147                 exit(-1);
148         }
149         drm_intel_bufmgr_gem_enable_reuse(bufmgr);
150
151         batch = intel_batchbuffer_alloc(bufmgr, devid);
152         if (!batch) {
153                 fprintf(stderr, "failed to create batch buffer\n");
154                 exit(-1);
155         }
156
157         target_buffer = drm_intel_bo_alloc(bufmgr, "target bo", 4096, 4096);
158         if (!target_buffer) {
159                 fprintf(stderr, "failed to alloc target buffer\n");
160                 exit(-1);
161         }
162
163         igt_subtest("render") {
164                 printf("running dummy loop on render\n");
165                 dummy_reloc_loop(I915_EXEC_RENDER);
166                 printf("dummy loop run on render completed\n");
167         }
168
169         igt_subtest("bsd") {
170                 if (gem_check_bsd(fd)) {
171                         sleep(2);
172                         printf("running dummy loop on bsd\n");
173                         dummy_reloc_loop(I915_EXEC_BSD);
174                         printf("dummy loop run on bsd completed\n");
175                 }
176         }
177
178         igt_subtest("blt") {
179                 if (gem_check_blt(fd)) {
180                         sleep(2);
181                         printf("running dummy loop on blt\n");
182                         dummy_reloc_loop(I915_EXEC_BLT);
183                         printf("dummy loop run on blt completed\n");
184                 }
185         }
186
187         igt_subtest("vebox") {
188                 if (gem_check_vebox(fd)) {
189                         sleep(2);
190                         printf("running dummy loop on vebox\n");
191                         dummy_reloc_loop(LOCAL_I915_EXEC_VEBOX);
192                         printf("dummy loop run on vebox completed\n");
193                 }
194         }
195
196         igt_subtest("mixed") {
197                 if (num_rings > 1) {
198                         sleep(2);
199                         printf("running dummy loop on random rings\n");
200                         dummy_reloc_loop_random_ring(num_rings);
201                         printf("dummy loop run on random rings completed\n");
202                 }
203         }
204
205         drm_intel_bo_unreference(target_buffer);
206         intel_batchbuffer_free(batch);
207         drm_intel_bufmgr_destroy(bufmgr);
208
209         close(fd);
210
211         return igt_retval();
212 }