kms_rotation_crc: Remove now unnecessary defines
[platform/upstream/intel-gpu-tools.git] / tests / gem_tiled_blits.c
1 /*
2  * Copyright © 2009 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  *
26  */
27
28 /** @file gem_tiled_blits.c
29  *
30  * This is a test of doing many tiled blits, with a working set
31  * larger than the aperture size.
32  *
33  * The goal is to catch a couple types of failure;
34  * - Fence management problems on pre-965.
35  * - A17 or L-shaped memory tiling workaround problems in acceleration.
36  *
37  * The model is to fill a collection of 1MB objects in a way that can't trip
38  * over A6 swizzling -- upload data to a non-tiled object, blit to the tiled
39  * object.  Then, copy the 1MB objects randomly between each other for a while.
40  * Finally, download their data through linear objects again and see what
41  * resulted.
42  */
43
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <fcntl.h>
48 #include <inttypes.h>
49 #include <errno.h>
50 #include <sys/stat.h>
51 #include <sys/time.h>
52
53 #include <drm.h>
54
55 #include "ioctl_wrappers.h"
56 #include "drmtest.h"
57 #include "intel_chipset.h"
58 #include "intel_io.h"
59 #include "igt_aux.h"
60
61 static drm_intel_bufmgr *bufmgr;
62 struct intel_batchbuffer *batch;
63 static int width = 512, height = 512;
64
65 static drm_intel_bo *
66 create_bo(uint32_t start_val)
67 {
68         drm_intel_bo *bo, *linear_bo;
69         uint32_t *linear;
70         uint32_t tiling = I915_TILING_X;
71         int i;
72
73         bo = drm_intel_bo_alloc(bufmgr, "tiled bo", 1024 * 1024, 4096);
74         do_or_die(drm_intel_bo_set_tiling(bo, &tiling, width * 4));
75         igt_assert(tiling == I915_TILING_X);
76
77         linear_bo = drm_intel_bo_alloc(bufmgr, "linear src", 1024 * 1024, 4096);
78
79         /* Fill the BO with dwords starting at start_val */
80         do_or_die(drm_intel_bo_map(linear_bo, 1));
81         linear = linear_bo->virtual;
82         for (i = 0; i < 1024 * 1024 / 4; i++)
83                 linear[i] = start_val++;
84         drm_intel_bo_unmap(linear_bo);
85
86         intel_copy_bo (batch, bo, linear_bo, width*height*4);
87
88         drm_intel_bo_unreference(linear_bo);
89
90         return bo;
91 }
92
93 static void
94 check_bo(drm_intel_bo *bo, uint32_t start_val)
95 {
96         drm_intel_bo *linear_bo;
97         uint32_t *linear;
98         int i;
99
100         linear_bo = drm_intel_bo_alloc(bufmgr, "linear dst", 1024 * 1024, 4096);
101
102         intel_copy_bo(batch, linear_bo, bo, width*height*4);
103
104         do_or_die(drm_intel_bo_map(linear_bo, 0));
105         linear = linear_bo->virtual;
106
107         for (i = 0; i < 1024 * 1024 / 4; i++) {
108                 igt_assert_f(linear[i] == start_val,
109                              "Expected 0x%08x, found 0x%08x "
110                              "at offset 0x%08x\n",
111                              start_val, linear[i], i * 4);
112                 start_val++;
113         }
114         drm_intel_bo_unmap(linear_bo);
115
116         drm_intel_bo_unreference(linear_bo);
117 }
118
119 static void run_test(int count)
120 {
121         drm_intel_bo **bo;
122         uint32_t *bo_start_val;
123         uint32_t start = 0;
124         int i;
125
126         bo = malloc(sizeof(drm_intel_bo *)*count);
127         bo_start_val = malloc(sizeof(uint32_t)*count);
128
129         for (i = 0; i < count; i++) {
130                 bo[i] = create_bo(start);
131                 bo_start_val[i] = start;
132                 start += 1024 * 1024 / 4;
133         }
134         igt_info("Verifying initialisation...\n");
135         for (i = 0; i < count; i++)
136                 check_bo(bo[i], bo_start_val[i]);
137
138         igt_info("Cyclic blits, forward...\n");
139         for (i = 0; i < count * 4; i++) {
140                 int src = i % count;
141                 int dst = (i+1) % count;
142
143                 if (src == dst)
144                         continue;
145
146                 intel_copy_bo(batch, bo[dst], bo[src], width*height*4);
147                 bo_start_val[dst] = bo_start_val[src];
148         }
149         for (i = 0; i < count; i++)
150                 check_bo(bo[i], bo_start_val[i]);
151
152         if (igt_run_in_simulation()) {
153                 for (i = 0; i < count; i++)
154                         drm_intel_bo_unreference(bo[i]);
155                 free(bo_start_val);
156                 free(bo);
157                 return;
158         }
159
160         igt_info("Cyclic blits, backward...\n");
161         for (i = 0; i < count * 4; i++) {
162                 int src = (i+1) % count;
163                 int dst = i % count;
164
165                 if (src == dst)
166                         continue;
167
168                 intel_copy_bo(batch, bo[dst], bo[src], width*height*4);
169                 bo_start_val[dst] = bo_start_val[src];
170         }
171         for (i = 0; i < count; i++)
172                 check_bo(bo[i], bo_start_val[i]);
173
174         igt_info("Random blits...\n");
175         for (i = 0; i < count * 4; i++) {
176                 int src = random() % count;
177                 int dst = random() % count;
178
179                 if (src == dst)
180                         continue;
181
182                 intel_copy_bo(batch, bo[dst], bo[src], width*height*4);
183                 bo_start_val[dst] = bo_start_val[src];
184         }
185         for (i = 0; i < count; i++) {
186                 check_bo(bo[i], bo_start_val[i]);
187                 drm_intel_bo_unreference(bo[i]);
188         }
189
190         free(bo_start_val);
191         free(bo);
192 }
193
194 int fd;
195
196 int main(int argc, char **argv)
197 {
198         int count = 0;
199
200         igt_subtest_init(argc, argv);
201
202         igt_fixture {
203                 fd = drm_open_any();
204
205                 if (igt_run_in_simulation())
206                         count = 2;
207                 if (argc > 1)
208                         count = atoi(argv[1]);
209                 if (count == 0) {
210                         count = 3 * gem_aperture_size(fd) / (1024*1024) / 2;
211                         count += (count & 1) == 0;
212                 } else if (count < 2) {
213                         fprintf(stderr, "count must be >= 2\n");
214                         return 1;
215                 }
216
217                 if (count > intel_get_total_ram_mb() * 9 / 10) {
218                         count = intel_get_total_ram_mb() * 9 / 10;
219                         igt_info("not enough RAM to run test, reducing buffer count\n");
220                 }
221
222                 igt_info("Using %d 1MiB buffers\n", count);
223
224                 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
225                 drm_intel_bufmgr_gem_enable_reuse(bufmgr);
226                 drm_intel_bufmgr_gem_set_vma_cache_size(bufmgr, 32);
227                 batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
228         }
229
230         igt_subtest("normal")
231                 run_test(count);
232
233         igt_subtest("interruptible") {
234                 igt_fork_signal_helper();
235                 run_test(count);
236                 igt_stop_signal_helper();
237         }
238
239         igt_fixture {
240                 intel_batchbuffer_free(batch);
241                 drm_intel_bufmgr_destroy(bufmgr);
242
243                 close(fd);
244         }
245
246         igt_exit();
247 }