kms_rotation_crc: Remove useless comments
[platform/upstream/intel-gpu-tools.git] / tests / gem_ringfill.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_ringfill.c
29  *
30  * This is a test of doing many tiny batchbuffer operations, in the hope of
31  * catching failure to manage the ring properly near full.
32  */
33
34 #include <stdbool.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <fcntl.h>
39 #include <inttypes.h>
40 #include <errno.h>
41 #include <sys/stat.h>
42 #include <sys/time.h>
43
44 #include <drm.h>
45
46 #include "ioctl_wrappers.h"
47 #include "drmtest.h"
48 #include "intel_chipset.h"
49 #include "intel_io.h"
50 #include "igt_aux.h"
51
52 struct bo {
53         const char *ring;
54         drm_intel_bo *src, *dst, *tmp;
55 };
56
57 static const int width = 512, height = 512;
58
59 static void create_bo(drm_intel_bufmgr *bufmgr,
60                       struct bo *b,
61                       const char *ring)
62 {
63         int size = 4 * width * height, i;
64         uint32_t *map;
65
66         b->ring = ring;
67         b->src = drm_intel_bo_alloc(bufmgr, "src", size, 4096);
68         b->dst = drm_intel_bo_alloc(bufmgr, "dst", size, 4096);
69         b->tmp = drm_intel_bo_alloc(bufmgr, "tmp", size, 4096);
70
71         /* Fill the src with indexes of the pixels */
72         drm_intel_bo_map(b->src, true);
73         map = b->src->virtual;
74         for (i = 0; i < width * height; i++)
75                 map[i] = i;
76         drm_intel_bo_unmap(b->src);
77
78         /* Fill the dst with garbage. */
79         drm_intel_bo_map(b->dst, true);
80         map = b->dst->virtual;
81         for (i = 0; i < width * height; i++)
82                 map[i] = 0xd0d0d0d0;
83         drm_intel_bo_unmap(b->dst);
84 }
85
86 static int check_bo(struct bo *b)
87 {
88         const uint32_t *map;
89         int i, fails = 0;
90
91         drm_intel_bo_map(b->dst, false);
92         map = b->dst->virtual;
93         for (i = 0; i < width*height; i++) {
94                 if (map[i] != i && ++fails <= 9) {
95                         int x = i % width;
96                         int y = i / width;
97
98                         igt_info("%s: copy #%d at %d,%d failed: read 0x%08x\n",
99                                  b->ring, i, x, y, map[i]);
100                 }
101         }
102         drm_intel_bo_unmap(b->dst);
103
104         return fails;
105 }
106
107 static void destroy_bo(struct bo *b)
108 {
109         drm_intel_bo_unreference(b->src);
110         drm_intel_bo_unreference(b->tmp);
111         drm_intel_bo_unreference(b->dst);
112 }
113
114 static int check_ring(drm_intel_bufmgr *bufmgr,
115                       struct intel_batchbuffer *batch,
116                       const char *ring,
117                       igt_render_copyfunc_t copy)
118 {
119         struct igt_buf src, tmp, dst;
120         struct bo bo;
121         char output[100];
122         int i;
123
124         snprintf(output, 100, "filling %s ring: ", ring);
125
126         create_bo(bufmgr, &bo, ring);
127
128         src.stride = 4 * width;
129         src.tiling = 0;
130         src.size = 4 * width * height;
131         src.num_tiles = 4 * width * height;
132         dst = tmp = src;
133
134         src.bo = bo.src;
135         tmp.bo = bo.tmp;
136         dst.bo = bo.dst;
137
138         /* The ring we've been using is 128k, and each rendering op
139          * will use at least 8 dwords:
140          *
141          * BATCH_START
142          * BATCH_START offset
143          * MI_FLUSH
144          * STORE_DATA_INDEX
145          * STORE_DATA_INDEX offset
146          * STORE_DATA_INDEX value
147          * MI_USER_INTERRUPT
148          * (padding)
149          *
150          * So iterate just a little more than that -- if we don't fill the ring
151          * doing this, we aren't likely to with this test.
152          */
153         for (i = 0; i < width * height; i++) {
154                 int x = i % width;
155                 int y = i / width;
156
157                 igt_progress(output, i, width*height);
158
159                 igt_assert(y < height);
160
161                 /* Dummy load to fill the ring */
162                 copy(batch, NULL, &src, 0, 0, width, height, &tmp, 0, 0);
163                 /* And copy the src into dst, pixel by pixel */
164                 copy(batch, NULL, &src, x, y, 1, 1, &dst, x, y);
165         }
166
167         /* verify */
168         igt_info("verifying\n");
169         i = check_bo(&bo);
170         destroy_bo(&bo);
171
172         return i;
173 }
174
175 static void blt_copy(struct intel_batchbuffer *batch,
176                      drm_intel_context *context,
177                      struct igt_buf *src, unsigned src_x, unsigned src_y,
178                      unsigned w, unsigned h,
179                      struct igt_buf *dst, unsigned dst_x, unsigned dst_y)
180 {
181         BLIT_COPY_BATCH_START(batch->devid, 0);
182         OUT_BATCH((3 << 24) | /* 32 bits */
183                   (0xcc << 16) | /* copy ROP */
184                   dst->stride);
185         OUT_BATCH((dst_y << 16) | dst_x); /* dst x1,y1 */
186         OUT_BATCH(((dst_y + h) << 16) | (dst_x + w)); /* dst x2,y2 */
187         OUT_RELOC(dst->bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, 0);
188         BLIT_RELOC_UDW(batch->devid);
189         OUT_BATCH((src_y << 16) | src_x); /* src x1,y1 */
190         OUT_BATCH(src->stride);
191         OUT_RELOC(src->bo, I915_GEM_DOMAIN_RENDER, 0, 0);
192         BLIT_RELOC_UDW(batch->devid);
193         ADVANCE_BATCH();
194
195         intel_batchbuffer_flush(batch);
196 }
197
198 drm_intel_bufmgr *bufmgr;
199 struct intel_batchbuffer *batch;
200 int fd;
201
202 igt_main
203 {
204         igt_skip_on_simulation();
205
206         igt_fixture {
207                 fd = drm_open_any();
208
209                 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
210                 drm_intel_bufmgr_gem_enable_reuse(bufmgr);
211                 batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
212         }
213
214         igt_subtest("blitter")
215                 check_ring(bufmgr, batch, "blt", blt_copy);
216
217         /* Strictly only required on architectures with a separate BLT ring,
218          * but lets stress everybody.
219          */
220         igt_subtest("render") {
221                 igt_render_copyfunc_t copy;
222
223                 copy = igt_get_render_copyfunc(batch->devid);
224                 igt_require(copy);
225
226                 check_ring(bufmgr, batch, "render", copy);
227         }
228
229         igt_fixture {
230                 intel_batchbuffer_free(batch);
231                 drm_intel_bufmgr_destroy(bufmgr);
232
233                 close(fd);
234         }
235 }