tests: sprinkle igt logging
[platform/upstream/intel-gpu-tools.git] / tests / gen3_mixed_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 gen3_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 <stdio.h>
38 #include <string.h>
39 #include <fcntl.h>
40 #include <inttypes.h>
41 #include <errno.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <sys/ioctl.h>
45 #include "drm.h"
46 #include "ioctl_wrappers.h"
47 #include "drmtest.h"
48 #include "intel_io.h"
49 #include "intel_chipset.h"
50
51 #include "i915_reg.h"
52 #include "i915_3d.h"
53
54 #define WIDTH (512)
55 #define HEIGHT (512)
56
57 static inline uint32_t pack_float(float f)
58 {
59         union {
60                 uint32_t dw;
61                 float f;
62         } u;
63         u.f = f;
64         return u.dw;
65 }
66
67 static uint32_t fill_reloc(struct drm_i915_gem_relocation_entry *reloc,
68                            uint32_t offset,
69                            uint32_t handle,
70                            uint32_t read_domain,
71                            uint32_t write_domain)
72 {
73         reloc->target_handle = handle;
74         reloc->delta = 0;
75         reloc->offset = offset * sizeof(uint32_t);
76         reloc->presumed_offset = 0;
77         reloc->read_domains = read_domain;
78         reloc->write_domain = write_domain;
79
80         return reloc->presumed_offset + reloc->delta;
81 }
82
83 static void
84 render_copy(int fd,
85             uint32_t dst, int dst_tiling,
86             uint32_t src, int src_tiling,
87             int use_fence)
88 {
89         uint32_t batch[1024], *b = batch;
90         struct drm_i915_gem_relocation_entry reloc[2], *r = reloc;
91         struct drm_i915_gem_exec_object2 obj[3];
92         struct drm_i915_gem_execbuffer2 exec;
93         uint32_t handle;
94         uint32_t tiling_bits;
95         int ret;
96
97         /* invariant state */
98         *b++ = (_3DSTATE_AA_CMD |
99                 AA_LINE_ECAAR_WIDTH_ENABLE |
100                 AA_LINE_ECAAR_WIDTH_1_0 |
101                 AA_LINE_REGION_WIDTH_ENABLE | AA_LINE_REGION_WIDTH_1_0);
102         *b++ = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
103                 IAB_MODIFY_ENABLE |
104                 IAB_MODIFY_FUNC | (BLENDFUNC_ADD << IAB_FUNC_SHIFT) |
105                 IAB_MODIFY_SRC_FACTOR |
106                 (BLENDFACT_ONE << IAB_SRC_FACTOR_SHIFT) |
107                 IAB_MODIFY_DST_FACTOR |
108                 (BLENDFACT_ZERO << IAB_DST_FACTOR_SHIFT));
109         *b++ = (_3DSTATE_DFLT_DIFFUSE_CMD);
110         *b++ = (0);
111         *b++ = (_3DSTATE_DFLT_SPEC_CMD);
112         *b++ = (0);
113         *b++ = (_3DSTATE_DFLT_Z_CMD);
114         *b++ = (0);
115         *b++ = (_3DSTATE_COORD_SET_BINDINGS |
116                 CSB_TCB(0, 0) |
117                 CSB_TCB(1, 1) |
118                 CSB_TCB(2, 2) |
119                 CSB_TCB(3, 3) |
120                 CSB_TCB(4, 4) |
121                 CSB_TCB(5, 5) |
122                 CSB_TCB(6, 6) |
123                 CSB_TCB(7, 7));
124         *b++ = (_3DSTATE_RASTER_RULES_CMD |
125                 ENABLE_POINT_RASTER_RULE |
126                 OGL_POINT_RASTER_RULE |
127                 ENABLE_LINE_STRIP_PROVOKE_VRTX |
128                 ENABLE_TRI_FAN_PROVOKE_VRTX |
129                 LINE_STRIP_PROVOKE_VRTX(1) |
130                 TRI_FAN_PROVOKE_VRTX(2) |
131                 ENABLE_TEXKILL_3D_4D |
132                 TEXKILL_4D);
133         *b++ = (_3DSTATE_MODES_4_CMD |
134                 ENABLE_LOGIC_OP_FUNC | LOGIC_OP_FUNC(LOGICOP_COPY) |
135                 ENABLE_STENCIL_WRITE_MASK | STENCIL_WRITE_MASK(0xff) |
136                 ENABLE_STENCIL_TEST_MASK | STENCIL_TEST_MASK(0xff));
137         *b++ = (_3DSTATE_LOAD_STATE_IMMEDIATE_1 | I1_LOAD_S(3) | I1_LOAD_S(4) | I1_LOAD_S(5) | 2);
138         *b++ = (0x00000000);    /* Disable texture coordinate wrap-shortest */
139         *b++ = ((1 << S4_POINT_WIDTH_SHIFT) |
140                 S4_LINE_WIDTH_ONE |
141                 S4_CULLMODE_NONE |
142                 S4_VFMT_XY);
143         *b++ = (0x00000000);    /* Stencil. */
144         *b++ = (_3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT);
145         *b++ = (_3DSTATE_SCISSOR_RECT_0_CMD);
146         *b++ = (0);
147         *b++ = (0);
148         *b++ = (_3DSTATE_DEPTH_SUBRECT_DISABLE);
149         *b++ = (_3DSTATE_LOAD_INDIRECT | 0);    /* disable indirect state */
150         *b++ = (0);
151         *b++ = (_3DSTATE_STIPPLE);
152         *b++ = (0x00000000);
153         *b++ = (_3DSTATE_BACKFACE_STENCIL_OPS | BFO_ENABLE_STENCIL_TWO_SIDE | 0);
154
155         /* samler state */
156         if (use_fence) {
157                 tiling_bits = MS3_USE_FENCE_REGS;
158         } else {
159                 tiling_bits = 0;
160                 if (src_tiling != I915_TILING_NONE)
161                         tiling_bits = MS3_TILED_SURFACE;
162                 if (src_tiling == I915_TILING_Y)
163                         tiling_bits |= MS3_TILE_WALK;
164         }
165
166 #define TEX_COUNT 1
167         *b++ = (_3DSTATE_MAP_STATE | (3 * TEX_COUNT));
168         *b++ = ((1 << TEX_COUNT) - 1);
169         *b = fill_reloc(r++, b-batch, src, I915_GEM_DOMAIN_SAMPLER, 0); b++;
170         *b++ = (MAPSURF_32BIT | MT_32BIT_ARGB8888 | tiling_bits |
171                 (HEIGHT - 1) << MS3_HEIGHT_SHIFT |
172                 (WIDTH - 1) << MS3_WIDTH_SHIFT);
173         *b++ = ((WIDTH-1) << MS4_PITCH_SHIFT);
174
175         *b++ = (_3DSTATE_SAMPLER_STATE | (3 * TEX_COUNT));
176         *b++ = ((1 << TEX_COUNT) - 1);
177         *b++ = (MIPFILTER_NONE << SS2_MIP_FILTER_SHIFT |
178                 FILTER_NEAREST << SS2_MAG_FILTER_SHIFT |
179                 FILTER_NEAREST << SS2_MIN_FILTER_SHIFT);
180         *b++ = (TEXCOORDMODE_WRAP << SS3_TCX_ADDR_MODE_SHIFT |
181                 TEXCOORDMODE_WRAP << SS3_TCY_ADDR_MODE_SHIFT |
182                 0 << SS3_TEXTUREMAP_INDEX_SHIFT);
183         *b++ = (0x00000000);
184
185         /* render target state */
186         if (use_fence) {
187                 tiling_bits = BUF_3D_USE_FENCE;
188         } else {
189                 tiling_bits = 0;
190                 if (dst_tiling != I915_TILING_NONE)
191                         tiling_bits = BUF_3D_TILED_SURFACE;
192                 if (dst_tiling == I915_TILING_Y)
193                         tiling_bits |= BUF_3D_TILE_WALK_Y;
194         }
195         *b++ = (_3DSTATE_BUF_INFO_CMD);
196         *b++ = (BUF_3D_ID_COLOR_BACK | tiling_bits | WIDTH*4);
197         *b = fill_reloc(r++, b-batch, dst,
198                         I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER);
199         b++;
200
201         *b++ = (_3DSTATE_DST_BUF_VARS_CMD);
202         *b++ = (COLR_BUF_ARGB8888 |
203                 DSTORG_HORT_BIAS(0x8) |
204                 DSTORG_VERT_BIAS(0x8));
205
206         /* draw rect is unconditional */
207         *b++ = (_3DSTATE_DRAW_RECT_CMD);
208         *b++ = (0x00000000);
209         *b++ = (0x00000000);    /* ymin, xmin */
210         *b++ = (DRAW_YMAX(HEIGHT - 1) |
211                 DRAW_XMAX(WIDTH - 1));
212         /* yorig, xorig (relate to color buffer?) */
213         *b++ = (0x00000000);
214
215         /* texfmt */
216         *b++ = (_3DSTATE_LOAD_STATE_IMMEDIATE_1 | I1_LOAD_S(1) | I1_LOAD_S(2) | I1_LOAD_S(6) | 2);
217         *b++ = ((4 << S1_VERTEX_WIDTH_SHIFT) | (4 << S1_VERTEX_PITCH_SHIFT));
218         *b++ = (~S2_TEXCOORD_FMT(0, TEXCOORDFMT_NOT_PRESENT) |
219                 S2_TEXCOORD_FMT(0, TEXCOORDFMT_2D));
220         *b++ = (S6_CBUF_BLEND_ENABLE | S6_COLOR_WRITE_ENABLE |
221                 BLENDFUNC_ADD << S6_CBUF_BLEND_FUNC_SHIFT |
222                 BLENDFACT_ONE << S6_CBUF_SRC_BLEND_FACT_SHIFT |
223                 BLENDFACT_ZERO << S6_CBUF_DST_BLEND_FACT_SHIFT);
224
225         /* pixel shader */
226         *b++ = (_3DSTATE_PIXEL_SHADER_PROGRAM | (1 + 3*3 - 2));
227         /* decl FS_T0 */
228         *b++ = (D0_DCL |
229                 REG_TYPE(FS_T0) << D0_TYPE_SHIFT |
230                 REG_NR(FS_T0) << D0_NR_SHIFT |
231                 ((REG_TYPE(FS_T0) != REG_TYPE_S) ? D0_CHANNEL_ALL : 0));
232         *b++ = (0);
233         *b++ = (0);
234         /* decl FS_S0 */
235         *b++ = (D0_DCL |
236                 (REG_TYPE(FS_S0) << D0_TYPE_SHIFT) |
237                 (REG_NR(FS_S0) << D0_NR_SHIFT) |
238                 ((REG_TYPE(FS_S0) != REG_TYPE_S) ? D0_CHANNEL_ALL : 0));
239         *b++ = (0);
240         *b++ = (0);
241         /* texld(FS_OC, FS_S0, FS_T0 */
242         *b++ = (T0_TEXLD |
243                 (REG_TYPE(FS_OC) << T0_DEST_TYPE_SHIFT) |
244                 (REG_NR(FS_OC) << T0_DEST_NR_SHIFT) |
245                 (REG_NR(FS_S0) << T0_SAMPLER_NR_SHIFT));
246         *b++ = ((REG_TYPE(FS_T0) << T1_ADDRESS_REG_TYPE_SHIFT) |
247                 (REG_NR(FS_T0) << T1_ADDRESS_REG_NR_SHIFT));
248         *b++ = (0);
249
250         *b++ = (PRIM3D_RECTLIST | (3*4 - 1));
251         *b++ = pack_float(WIDTH);
252         *b++ = pack_float(HEIGHT);
253         *b++ = pack_float(WIDTH);
254         *b++ = pack_float(HEIGHT);
255
256         *b++ = pack_float(0);
257         *b++ = pack_float(HEIGHT);
258         *b++ = pack_float(0);
259         *b++ = pack_float(HEIGHT);
260
261         *b++ = pack_float(0);
262         *b++ = pack_float(0);
263         *b++ = pack_float(0);
264         *b++ = pack_float(0);
265
266         *b++ = MI_BATCH_BUFFER_END;
267         if ((b - batch) & 1)
268                 *b++ = 0;
269
270         igt_assert(b - batch <= 1024);
271         handle = gem_create(fd, 4096);
272         gem_write(fd, handle, 0, batch, (b-batch)*sizeof(batch[0]));
273
274         igt_assert(r-reloc == 2);
275
276         tiling_bits = 0;
277         if (use_fence)
278                 tiling_bits = EXEC_OBJECT_NEEDS_FENCE;
279
280         obj[0].handle = dst;
281         obj[0].relocation_count = 0;
282         obj[0].relocs_ptr = 0;
283         obj[0].alignment = 0;
284         obj[0].offset = 0;
285         obj[0].flags = tiling_bits;
286         obj[0].rsvd1 = 0;
287         obj[0].rsvd2 = 0;
288
289         obj[1].handle = src;
290         obj[1].relocation_count = 0;
291         obj[1].relocs_ptr = 0;
292         obj[1].alignment = 0;
293         obj[1].offset = 0;
294         obj[1].flags = tiling_bits;
295         obj[1].rsvd1 = 0;
296         obj[1].rsvd2 = 0;
297
298         obj[2].handle = handle;
299         obj[2].relocation_count = 2;
300         obj[2].relocs_ptr = (uintptr_t)reloc;
301         obj[2].alignment = 0;
302         obj[2].offset = 0;
303         obj[2].flags = 0;
304         obj[2].rsvd1 = obj[2].rsvd2 = 0;
305
306         exec.buffers_ptr = (uintptr_t)obj;
307         exec.buffer_count = 3;
308         exec.batch_start_offset = 0;
309         exec.batch_len = (b-batch)*sizeof(batch[0]);
310         exec.DR1 = exec.DR4 = 0;
311         exec.num_cliprects = 0;
312         exec.cliprects_ptr = 0;
313         exec.flags = 0;
314         i915_execbuffer2_set_context_id(exec, 0);
315         exec.rsvd2 = 0;
316
317         ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
318         while (ret && errno == EBUSY) {
319                 drmCommandNone(fd, DRM_I915_GEM_THROTTLE);
320                 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
321         }
322         igt_assert(ret == 0);
323
324         gem_close(fd, handle);
325 }
326
327 static void blt_copy(int fd, uint32_t dst, uint32_t src)
328 {
329         uint32_t batch[1024], *b = batch;
330         struct drm_i915_gem_relocation_entry reloc[2], *r = reloc;
331         struct drm_i915_gem_exec_object2 obj[3];
332         struct drm_i915_gem_execbuffer2 exec;
333         uint32_t handle;
334         int ret;
335
336         *b++ = (XY_SRC_COPY_BLT_CMD |
337                 XY_SRC_COPY_BLT_WRITE_ALPHA |
338                 XY_SRC_COPY_BLT_WRITE_RGB | 6);
339         *b++ = 3 << 24 | 0xcc << 16 | WIDTH * 4;
340         *b++ = 0;
341         *b++ = HEIGHT << 16 | WIDTH;
342         *b = fill_reloc(r++, b-batch, dst,
343                         I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER); b++;
344         *b++ = 0;
345         *b++ = WIDTH*4;
346         *b = fill_reloc(r++, b-batch, src, I915_GEM_DOMAIN_RENDER, 0); b++;
347
348         *b++ = MI_BATCH_BUFFER_END;
349         if ((b - batch) & 1)
350                 *b++ = 0;
351
352         igt_assert(b - batch <= 1024);
353         handle = gem_create(fd, 4096);
354         gem_write(fd, handle, 0, batch, (b-batch)*sizeof(batch[0]));
355
356         igt_assert(r-reloc == 2);
357
358         obj[0].handle = dst;
359         obj[0].relocation_count = 0;
360         obj[0].relocs_ptr = 0;
361         obj[0].alignment = 0;
362         obj[0].offset = 0;
363         obj[0].flags = EXEC_OBJECT_NEEDS_FENCE;
364         obj[0].rsvd1 = 0;
365         obj[0].rsvd2 = 0;
366
367         obj[1].handle = src;
368         obj[1].relocation_count = 0;
369         obj[1].relocs_ptr = 0;
370         obj[1].alignment = 0;
371         obj[1].offset = 0;
372         obj[1].flags = EXEC_OBJECT_NEEDS_FENCE;
373         obj[1].rsvd1 = 0;
374         obj[1].rsvd2 = 0;
375
376         obj[2].handle = handle;
377         obj[2].relocation_count = 2;
378         obj[2].relocs_ptr = (uintptr_t)reloc;
379         obj[2].alignment = 0;
380         obj[2].offset = 0;
381         obj[2].flags = 0;
382         obj[2].rsvd1 = obj[2].rsvd2 = 0;
383
384         exec.buffers_ptr = (uintptr_t)obj;
385         exec.buffer_count = 3;
386         exec.batch_start_offset = 0;
387         exec.batch_len = (b-batch)*sizeof(batch[0]);
388         exec.DR1 = exec.DR4 = 0;
389         exec.num_cliprects = 0;
390         exec.cliprects_ptr = 0;
391         exec.flags = 0;
392         i915_execbuffer2_set_context_id(exec, 0);
393         exec.rsvd2 = 0;
394
395         ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
396         while (ret && errno == EBUSY) {
397                 drmCommandNone(fd, DRM_I915_GEM_THROTTLE);
398                 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
399         }
400         igt_assert(ret == 0);
401
402         gem_close(fd, handle);
403 }
404
405
406 static void
407 copy(int fd,
408      uint32_t dst, int dst_tiling,
409      uint32_t src, int src_tiling)
410 {
411 retry:
412         switch (random() % 3) {
413         case 0: render_copy(fd, dst, dst_tiling, src, src_tiling, 0); break;
414         case 1: render_copy(fd, dst, dst_tiling, src, src_tiling, 1); break;
415         case 2: if (dst_tiling == I915_TILING_Y || src_tiling == I915_TILING_Y)
416                         goto retry;
417                 blt_copy(fd, dst, src);
418                 break;
419         }
420 }
421
422 static uint32_t
423 create_bo(int fd, uint32_t val, int tiling)
424 {
425         uint32_t handle;
426         uint32_t *v;
427         int i;
428
429         handle = gem_create(fd, WIDTH*HEIGHT*4);
430         gem_set_tiling(fd, handle, tiling, WIDTH*4);
431
432         /* Fill the BO with dwords starting at val */
433         v = gem_mmap(fd, handle, WIDTH*HEIGHT*4, PROT_READ | PROT_WRITE);
434         igt_assert(v);
435         for (i = 0; i < WIDTH*HEIGHT; i++)
436                 v[i] = val++;
437         munmap(v, WIDTH*HEIGHT*4);
438
439         return handle;
440 }
441
442 static void
443 check_bo(int fd, uint32_t handle, uint32_t val)
444 {
445         uint32_t *v;
446         int i;
447
448         v = gem_mmap(fd, handle, WIDTH*HEIGHT*4, PROT_READ);
449         igt_assert(v);
450         for (i = 0; i < WIDTH*HEIGHT; i++) {
451                 if (v[i] != val) {
452                         fprintf(stderr, "Expected 0x%08x, found 0x%08x "
453                                 "at offset 0x%08x\n",
454                                 val, v[i], i * 4);
455                         abort();
456                 }
457                 val++;
458         }
459         munmap(v, WIDTH*HEIGHT*4);
460 }
461
462 int main(int argc, char **argv)
463 {
464         uint32_t *handle, *tiling, *start_val;
465         uint32_t start = 0;
466         int i, fd, count;
467
468         igt_simple_init();
469
470         fd = drm_open_any();
471
472         if (!IS_GEN3(intel_get_drm_devid(fd))) {
473                 printf("gen3-only test, doing nothing\n");
474                 return 77;
475         }
476
477         count = 0;
478         if (argc > 1)
479                 count = atoi(argv[1]);
480         if (count == 0)
481                 count = 3 * gem_aperture_size(fd) / (1024*1024) / 2;
482         igt_info("Using %d 1MiB buffers\n", count);
483
484         handle = malloc(sizeof(uint32_t)*count*3);
485         tiling = handle + count;
486         start_val = tiling + count;
487
488         for (i = 0; i < count; i++) {
489                 handle[i] = create_bo(fd, start, tiling[i] = i % 3);
490                 start_val[i] = start;
491                 start += 1024 * 1024 / 4;
492         }
493
494         igt_info("Verifying initialisation..."); fflush(stdout);
495         for (i = 0; i < count; i++)
496                 check_bo(fd, handle[i], start_val[i]);
497         igt_info("done\n");
498
499         igt_info("Cyclic blits, forward..."); fflush(stdout);
500         for (i = 0; i < count * 32; i++) {
501                 int src = i % count;
502                 int dst = (i + 1) % count;
503
504                 copy(fd, handle[dst], tiling[dst], handle[src], tiling[src]);
505                 start_val[dst] = start_val[src];
506         }
507         igt_info("verifying..."); fflush(stdout);
508         for (i = 0; i < count; i++)
509                 check_bo(fd, handle[i], start_val[i]);
510         igt_info("done\n");
511
512         igt_info("Cyclic blits, backward..."); fflush(stdout);
513         for (i = 0; i < count * 32; i++) {
514                 int src = (i + 1) % count;
515                 int dst = i % count;
516
517                 copy(fd, handle[dst], tiling[dst], handle[src], tiling[src]);
518                 start_val[dst] = start_val[src];
519         }
520         igt_info("verifying..."); fflush(stdout);
521         for (i = 0; i < count; i++)
522                 check_bo(fd, handle[i], start_val[i]);
523         igt_info("done\n");
524
525         igt_info("Random blits..."); fflush(stdout);
526         for (i = 0; i < count * 32; i++) {
527                 int src = random() % count;
528                 int dst = random() % count;
529
530                 while (src == dst)
531                         dst = random() % count;
532
533                         copy(fd, handle[dst], tiling[dst], handle[src], tiling[src]);
534                 start_val[dst] = start_val[src];
535         }
536         igt_info("verifying..."); fflush(stdout);
537         for (i = 0; i < count; i++)
538                 check_bo(fd, handle[i], start_val[i]);
539         igt_info("done\n");
540
541         return 0;
542 }