kms_rotation_crc: Remove useless comments
[platform/upstream/intel-gpu-tools.git] / tests / gem_render_tiled_blits.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  *    Chris Wilson <chris@chris-wilson.co.uk>
25  *
26  */
27
28 /** @file gem_linear_render_blits.c
29  *
30  * This is a test of doing many blits, with a working set
31  * larger than the aperture size.
32  *
33  * The goal is to simply ensure the basics work.
34  */
35
36 #include <stdlib.h>
37 #include <sys/ioctl.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <fcntl.h>
41 #include <inttypes.h>
42 #include <errno.h>
43 #include <sys/stat.h>
44 #include <sys/time.h>
45 #include <getopt.h>
46
47 #include <drm.h>
48
49 #include "ioctl_wrappers.h"
50 #include "drmtest.h"
51 #include "intel_bufmgr.h"
52 #include "intel_batchbuffer.h"
53 #include "intel_io.h"
54 #include "intel_chipset.h"
55 #include "igt_aux.h"
56
57 #define WIDTH 512
58 #define STRIDE (WIDTH*4)
59 #define HEIGHT 512
60 #define SIZE (HEIGHT*STRIDE)
61
62 static igt_render_copyfunc_t render_copy;
63 static drm_intel_bo *linear;
64 static uint32_t data[WIDTH*HEIGHT];
65 static int snoop;
66
67 static void
68 check_bo(struct intel_batchbuffer *batch, struct igt_buf *buf, uint32_t val)
69 {
70         struct igt_buf tmp;
71         uint32_t *ptr;
72         int i;
73
74         tmp.bo = linear;
75         tmp.stride = STRIDE;
76         tmp.tiling = I915_TILING_NONE;
77         tmp.size = SIZE;
78
79         render_copy(batch, NULL, buf, 0, 0, WIDTH, HEIGHT, &tmp, 0, 0);
80         if (snoop) {
81                 do_or_die(dri_bo_map(linear, 0));
82                 ptr = linear->virtual;
83         } else {
84                 do_or_die(drm_intel_bo_get_subdata(linear, 0, sizeof(data), data));
85                 ptr = data;
86         }
87         for (i = 0; i < WIDTH*HEIGHT; i++) {
88                 igt_assert_f(ptr[i] == val,
89                              "Expected 0x%08x, found 0x%08x "
90                              "at offset 0x%08x\n",
91                              val, ptr[i], i * 4);
92                 val++;
93         }
94         if (ptr != data)
95                 dri_bo_unmap(linear);
96 }
97
98 int main(int argc, char **argv)
99 {
100         drm_intel_bufmgr *bufmgr;
101         struct intel_batchbuffer *batch;
102         uint32_t *start_val;
103         struct igt_buf *buf;
104         uint32_t start = 0;
105         int i, j, fd, count;
106         uint32_t devid;
107
108         igt_simple_init();
109
110         igt_skip_on_simulation();
111
112         fd = drm_open_any();
113         devid = intel_get_drm_devid(fd);
114
115         render_copy = igt_get_render_copyfunc(devid);
116         igt_require(render_copy);
117
118         snoop = 1;
119         if (IS_GEN2(devid)) /* chipset only handles cached -> uncached */
120                 snoop = 0;
121         if (IS_BROADWATER(devid) || IS_CRESTLINE(devid)) /* snafu */
122                 snoop = 0;
123
124         bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
125         drm_intel_bufmgr_gem_set_vma_cache_size(bufmgr, 32);
126         batch = intel_batchbuffer_alloc(bufmgr, devid);
127
128         count = 0;
129         if (argc > 1)
130                 count = atoi(argv[1]);
131         if (count == 0)
132                 count = 3 * gem_aperture_size(fd) / SIZE / 2;
133         else if (count < 2) {
134                 igt_warn("count must be >= 2\n");
135                 return 1;
136         }
137
138         if (count > intel_get_total_ram_mb() * 9 / 10) {
139                 count = intel_get_total_ram_mb() * 9 / 10;
140                 igt_info("not enough RAM to run test, reducing buffer count\n");
141         }
142
143         igt_info("Using %d 1MiB buffers\n", count);
144
145         linear = drm_intel_bo_alloc(bufmgr, "linear", WIDTH*HEIGHT*4, 0);
146         if (snoop) {
147                 gem_set_caching(fd, linear->handle, 1);
148                 igt_info("Using a snoop linear buffer for comparisons\n");
149         }
150
151         buf = malloc(sizeof(*buf)*count);
152         start_val = malloc(sizeof(*start_val)*count);
153
154         for (i = 0; i < count; i++) {
155                 uint32_t tiling = I915_TILING_X + (random() & 1);
156                 unsigned long pitch = STRIDE;
157                 uint32_t *ptr;
158
159                 buf[i].bo = drm_intel_bo_alloc_tiled(bufmgr, "",
160                                                      WIDTH, HEIGHT, 4,
161                                                      &tiling, &pitch, 0);
162                 buf[i].stride = pitch;
163                 buf[i].tiling = tiling;
164                 buf[i].size = SIZE;
165
166                 start_val[i] = start;
167
168                 do_or_die(drm_intel_gem_bo_map_gtt(buf[i].bo));
169                 ptr = buf[i].bo->virtual;
170                 for (j = 0; j < WIDTH*HEIGHT; j++)
171                         ptr[j] = start++;
172                 drm_intel_gem_bo_unmap_gtt(buf[i].bo);
173         }
174
175         igt_info("Verifying initialisation...\n");
176         for (i = 0; i < count; i++)
177                 check_bo(batch, &buf[i], start_val[i]);
178
179         igt_info("Cyclic blits, forward...\n");
180         for (i = 0; i < count * 4; i++) {
181                 int src = i % count;
182                 int dst = (i + 1) % count;
183
184                 render_copy(batch, NULL, buf+src, 0, 0, WIDTH, HEIGHT, buf+dst, 0, 0);
185                 start_val[dst] = start_val[src];
186         }
187         for (i = 0; i < count; i++)
188                 check_bo(batch, &buf[i], start_val[i]);
189
190         igt_info("Cyclic blits, backward...\n");
191         for (i = 0; i < count * 4; i++) {
192                 int src = (i + 1) % count;
193                 int dst = i % count;
194
195                 render_copy(batch, NULL, buf+src, 0, 0, WIDTH, HEIGHT, buf+dst, 0, 0);
196                 start_val[dst] = start_val[src];
197         }
198         for (i = 0; i < count; i++)
199                 check_bo(batch, &buf[i], start_val[i]);
200
201         igt_info("Random blits...\n");
202         for (i = 0; i < count * 4; i++) {
203                 int src = random() % count;
204                 int dst = random() % count;
205
206                 if (src == dst)
207                         continue;
208
209                 render_copy(batch, NULL, buf+src, 0, 0, WIDTH, HEIGHT, buf+dst, 0, 0);
210                 start_val[dst] = start_val[src];
211         }
212         for (i = 0; i < count; i++)
213                 check_bo(batch, &buf[i], start_val[i]);
214
215         return 0;
216 }