lib/igt.cocci: Add stanza for for_each_pipe
[platform/upstream/intel-gpu-tools.git] / tests / gem_fence_thrash.c
1 /*
2  * Copyright © 2008-9 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  *    Eric Anholt <eric@anholt.net>
25  *    Chris Wilson <chris@chris-wilson.co.uk>
26  *
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <fcntl.h>
38 #include <inttypes.h>
39 #include <errno.h>
40 #include <sys/stat.h>
41 #include <sys/ioctl.h>
42 #include <pthread.h>
43 #include "drm.h"
44 #include "ioctl_wrappers.h"
45 #include "drmtest.h"
46
47 #define OBJECT_SIZE (128*1024) /* restricted to 1MiB alignment on i915 fences */
48
49 /* Before introduction of the LRU list for fences, allocation of a fence for a page
50  * fault would use the first inactive fence (i.e. in preference one with no outstanding
51  * GPU activity, or it would wait on the first to finish). Given the choice, it would simply
52  * reuse the fence that had just been allocated for the previous page-fault - the worst choice
53  * when copying between two buffers and thus constantly swapping fences.
54  */
55
56 struct test {
57         int fd;
58         int tiling;
59         int num_surfaces;
60 };
61
62 static void *
63 bo_create (int fd, int tiling)
64 {
65         void *ptr;
66         int handle;
67
68         handle = gem_create(fd, OBJECT_SIZE);
69
70         /* dirty cpu caches a bit ... */
71         ptr = gem_mmap__cpu(fd, handle, OBJECT_SIZE, PROT_READ | PROT_WRITE);
72         igt_assert(ptr);
73         memset(ptr, 0, OBJECT_SIZE);
74         munmap(ptr, OBJECT_SIZE);
75
76         gem_set_tiling(fd, handle, tiling, 1024);
77
78         ptr = gem_mmap(fd, handle, OBJECT_SIZE, PROT_READ | PROT_WRITE);
79         igt_assert(ptr);
80
81         /* XXX: mmap_gtt pulls the bo into the GTT read domain. */
82         gem_sync(fd, handle);
83
84         return ptr;
85 }
86
87 static void *
88 bo_copy (void *_arg)
89 {
90         struct test *t = (struct test *)_arg;
91         int fd = t->fd;
92         int n;
93         char *a, *b;
94
95         a = bo_create (fd, t->tiling);
96         b = bo_create (fd, t->tiling);
97
98         for (n = 0; n < 1000; n++) {
99                 memcpy (a, b, OBJECT_SIZE);
100                 sched_yield ();
101         }
102
103         return NULL;
104 }
105
106 static void
107 _bo_write_verify(struct test *t)
108 {
109         int fd = t->fd;
110         int i, k;
111         uint32_t **s;
112         uint32_t v;
113         unsigned int dwords = OBJECT_SIZE >> 2;
114         const char *tile_str[] = { "none", "x", "y" };
115
116         igt_assert(t->tiling >= 0 && t->tiling <= I915_TILING_Y);
117         igt_assert(t->num_surfaces > 0);
118
119         s = calloc(sizeof(*s), t->num_surfaces);
120         igt_assert(s);
121
122         for (k = 0; k < t->num_surfaces; k++)
123                 s[k] = bo_create(fd, t->tiling);
124
125         for (k = 0; k < t->num_surfaces; k++) {
126                 volatile uint32_t *a = s[k];
127
128                 for (i = 0; i < dwords; i++) {
129                         a[i] = i;
130                         v = a[i];
131                         igt_assert_f(v == i,
132                                      "tiling %s: write failed at %d (%x)\n",
133                                      tile_str[t->tiling], i, v);
134                 }
135
136                 for (i = 0; i < dwords; i++) {
137                         v = a[i];
138                         igt_assert_f(v == i,
139                                      "tiling %s: verify failed at %d (%x)\n",
140                                      tile_str[t->tiling], i, v);
141                 }
142         }
143
144         for (k = 0; k < t->num_surfaces; k++)
145                 munmap(s[k], OBJECT_SIZE);
146
147         free(s);
148 }
149
150 static void *
151 bo_write_verify(void *_arg)
152 {
153         struct test *t = (struct test *)_arg;
154         int i;
155
156         for (i = 0; i < 10; i++)
157                 _bo_write_verify(t);
158
159         return 0;
160 }
161
162 static int run_test(int threads_per_fence, void *f, int tiling,
163                     int surfaces_per_thread)
164 {
165         struct test t;
166         pthread_t *threads;
167         int n, num_fences, num_threads;
168
169         t.fd = drm_open_any();
170         t.tiling = tiling;
171         t.num_surfaces = surfaces_per_thread;
172
173         num_fences = gem_available_fences(t.fd);
174         igt_assert(num_fences > 0);
175
176         num_threads = threads_per_fence * num_fences;
177
178         igt_info("%s: threads %d, fences %d, tiling %d, surfaces per thread %d\n",
179                  f == bo_copy ? "copy" : "write-verify", num_threads,
180                  num_fences, tiling, surfaces_per_thread);
181
182         if (threads_per_fence) {
183                 threads = calloc(sizeof(*threads), num_threads);
184                 igt_assert(threads != NULL);
185
186                 for (n = 0; n < num_threads; n++)
187                         pthread_create (&threads[n], NULL, f, &t);
188
189                 for (n = 0; n < num_threads; n++)
190                         pthread_join (threads[n], NULL);
191         } else {
192                 void *(*func)(void *) = f;
193                 igt_assert(func(&t) == (void *)0);
194         }
195
196         close(t.fd);
197
198         return 0;
199 }
200
201 igt_main
202 {
203         igt_skip_on_simulation();
204
205         igt_subtest("bo-write-verify-none")
206                 igt_assert(run_test(0, bo_write_verify, I915_TILING_NONE, 80) == 0);
207
208         igt_subtest("bo-write-verify-x")
209                 igt_assert(run_test(0, bo_write_verify, I915_TILING_X, 80) == 0);
210
211         igt_subtest("bo-write-verify-y")
212                 igt_assert(run_test(0, bo_write_verify, I915_TILING_Y, 80) == 0);
213
214         igt_subtest("bo-write-verify-threaded-none")
215                 igt_assert(run_test(5, bo_write_verify, I915_TILING_NONE, 2) == 0);
216
217         igt_subtest("bo-write-verify-threaded-x") {
218                 igt_assert(run_test(2, bo_write_verify, I915_TILING_X, 2) == 0);
219                 igt_assert(run_test(5, bo_write_verify, I915_TILING_X, 2) == 0);
220                 igt_assert(run_test(10, bo_write_verify, I915_TILING_X, 2) == 0);
221                 igt_assert(run_test(20, bo_write_verify, I915_TILING_X, 2) == 0);
222         }
223
224         igt_subtest("bo-write-verify-threaded-y") {
225                 igt_assert(run_test(2, bo_write_verify, I915_TILING_Y, 2) == 0);
226                 igt_assert(run_test(5, bo_write_verify, I915_TILING_Y, 2) == 0);
227                 igt_assert(run_test(10, bo_write_verify, I915_TILING_Y, 2) == 0);
228                 igt_assert(run_test(20, bo_write_verify, I915_TILING_Y, 2) == 0);
229         }
230
231         igt_subtest("bo-copy")
232                 igt_assert(run_test(1, bo_copy, I915_TILING_X, 1) == 0);
233 }