tests: remove unused getopt header includes
[platform/upstream/intel-gpu-tools.git] / tests / gem_tiled_fence_blits.c
1 /*
2  * Copyright © 2009,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  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27
28 /** @file gem_tiled_fence_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_bufmgr.h"
58 #include "intel_batchbuffer.h"
59 #include "intel_io.h"
60 #include "intel_chipset.h"
61 #include "igt_aux.h"
62
63 static drm_intel_bufmgr *bufmgr;
64 struct intel_batchbuffer *batch;
65 static int width = 512, height = 512;
66 static uint32_t linear[1024*1024/4];
67
68 static drm_intel_bo *
69 create_bo(int fd, uint32_t start_val)
70 {
71         drm_intel_bo *bo;
72         uint32_t tiling = I915_TILING_X;
73         int ret, i;
74
75         bo = drm_intel_bo_alloc(bufmgr, "tiled bo", 1024 * 1024, 4096);
76         ret = drm_intel_bo_set_tiling(bo, &tiling, width * 4);
77         igt_assert(ret == 0);
78         igt_assert(tiling == I915_TILING_X);
79
80         /* Fill the BO with dwords starting at start_val */
81         for (i = 0; i < 1024 * 1024 / 4; i++)
82                 linear[i] = start_val++;
83
84         gem_write(fd, bo->handle, 0, linear, sizeof(linear));
85
86         return bo;
87 }
88
89 static void
90 check_bo(int fd, drm_intel_bo *bo, uint32_t start_val)
91 {
92         int i;
93
94         gem_read(fd, bo->handle, 0, linear, sizeof(linear));
95
96         for (i = 0; i < 1024 * 1024 / 4; i++) {
97                 igt_assert_f(linear[i] == start_val,
98                              "Expected 0x%08x, found 0x%08x "
99                              "at offset 0x%08x\n",
100                              start_val, linear[i], i * 4);
101                 start_val++;
102         }
103 }
104
105 igt_simple_main
106 {
107         drm_intel_bo *bo[4096];
108         uint32_t bo_start_val[4096];
109         uint32_t start = 0;
110         int fd, i, count;
111
112         igt_skip_on_simulation();
113
114         fd = drm_open_any();
115         count = 3 * gem_aperture_size(fd) / (1024*1024) / 2;
116         if (count > intel_get_total_ram_mb() * 9 / 10) {
117                 count = intel_get_total_ram_mb() * 9 / 10;
118                 igt_info("not enough RAM to run test, reducing buffer count\n");
119         }
120         count |= 1;
121         igt_info("Using %d 1MiB buffers\n", count);
122
123         bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
124         drm_intel_bufmgr_gem_enable_reuse(bufmgr);
125         batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
126
127         for (i = 0; i < count; i++) {
128                 bo[i] = create_bo(fd, start);
129                 bo_start_val[i] = start;
130
131                 /*
132                 igt_info("Creating bo %d\n", i);
133                 check_bo(bo[i], bo_start_val[i]);
134                 */
135
136                 start += 1024 * 1024 / 4;
137         }
138
139         for (i = 0; i < count; i++) {
140                 int src = count - i - 1;
141                 intel_copy_bo(batch, bo[i], bo[src], width*height*4);
142                 bo_start_val[i] = bo_start_val[src];
143         }
144
145         for (i = 0; i < count * 4; i++) {
146                 int src = random() % count;
147                 int dst = random() % count;
148
149                 if (src == dst)
150                         continue;
151
152                 intel_copy_bo(batch, bo[dst], bo[src], width*height*4);
153                 bo_start_val[dst] = bo_start_val[src];
154
155                 /*
156                 check_bo(bo[dst], bo_start_val[dst]);
157                 igt_info("%d: copy bo %d to %d\n", i, src, dst);
158                 */
159         }
160
161         for (i = 0; i < count; i++) {
162                 /*
163                 igt_info("check %d\n", i);
164                 */
165                 check_bo(fd, bo[i], bo_start_val[i]);
166
167                 drm_intel_bo_unreference(bo[i]);
168                 bo[i] = NULL;
169         }
170
171         intel_batchbuffer_free(batch);
172         drm_intel_bufmgr_destroy(bufmgr);
173
174         close(fd);
175 }