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