kms_rotation_crc: Remove now unnecessary defines
[platform/upstream/intel-gpu-tools.git] / tests / gem_cs_prefetch.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  *    Daniel Vetter <daniel.vetter@ffwll.ch>
25  *
26  */
27
28 /*
29  * Testcase: Test the CS prefetch behaviour on batches
30  *
31  * Historically the batch prefetcher doesn't check whether it's crossing page
32  * boundaries and likes to throw up when it gets a pagefault in return for his
33  * over-eager behaviour. Check for this.
34  *
35  * This test for a bug where we've failed to plug a scratch pte entry into the
36  * very last gtt pte.
37  */
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <fcntl.h>
42 #include <inttypes.h>
43 #include <errno.h>
44 #include <sys/stat.h>
45 #include <sys/time.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 static drm_intel_bufmgr *bufmgr;
58 struct intel_batchbuffer *batch;
59
60 static void exec(int fd, uint32_t handle)
61 {
62         struct drm_i915_gem_execbuffer2 execbuf;
63         struct drm_i915_gem_exec_object2 gem_exec[1];
64         int ret = 0;
65
66         gem_exec[0].handle = handle;
67         gem_exec[0].relocation_count = 0;
68         gem_exec[0].relocs_ptr = 0;
69         gem_exec[0].alignment = 0;
70         gem_exec[0].offset = 0;
71         gem_exec[0].flags = 0;
72         gem_exec[0].rsvd1 = 0;
73         gem_exec[0].rsvd2 = 0;
74
75         execbuf.buffers_ptr = (uintptr_t)gem_exec;
76         execbuf.buffer_count = 1;
77         execbuf.batch_start_offset = 0;
78         execbuf.batch_len = 4096;
79         execbuf.cliprects_ptr = 0;
80         execbuf.num_cliprects = 0;
81         execbuf.DR1 = 0;
82         execbuf.DR4 = 0;
83         execbuf.flags = 0;
84         execbuf.rsvd1 = 0;
85         execbuf.rsvd2 = 0;
86
87         ret = drmIoctl(fd,
88                        DRM_IOCTL_I915_GEM_EXECBUFFER2,
89                        &execbuf);
90         gem_sync(fd, handle);
91         igt_assert(ret == 0);
92 }
93
94 igt_simple_main
95 {
96         uint32_t batch_end[4] = {MI_BATCH_BUFFER_END, 0, 0, 0};
97         int fd, i, ret;
98         uint64_t aper_size;
99         int count;
100         drm_intel_bo *sample_batch_bo;
101
102         igt_skip_on_simulation();
103
104         fd = drm_open_any();
105
106         bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
107         igt_assert(bufmgr);
108
109         drm_intel_bufmgr_gem_enable_reuse(bufmgr);
110         
111         aper_size = gem_aperture_size(fd);
112
113         /* presume a big per-bo overhead */
114         igt_skip_on_f(intel_get_total_ram_mb() < (aper_size / (1024*1024)) * 3 / 2,
115                       "not enough mem to run test\n");
116
117         count = aper_size / 4096;
118
119         batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
120         igt_assert(batch);
121
122         sample_batch_bo = drm_intel_bo_alloc(bufmgr, "", 4096, 4096);
123         igt_assert(sample_batch_bo);
124         ret = drm_intel_bo_subdata(sample_batch_bo, 4096-sizeof(batch_end),
125                                    sizeof(batch_end), batch_end);
126         igt_assert(ret == 0);
127
128         /* fill the entire gart with batches and run them */
129         for (i = 0; i < count; i++) {
130                 drm_intel_bo *batch_bo;
131
132                 batch_bo = drm_intel_bo_alloc(bufmgr, "", 4096, 4096);
133                 igt_assert(batch_bo);
134
135                 /* copy the sample batch with the gpu to the new one, so that we
136                  * also test the unmappable part of the gtt. */
137                 BLIT_COPY_BATCH_START(batch->devid, 0);
138                 OUT_BATCH((3 << 24) | /* 32 bits */
139                           (0xcc << 16) | /* copy ROP */
140                           4096);
141                 OUT_BATCH(0); /* dst y1,x1 */
142                 OUT_BATCH((1 << 16) | 1024);
143                 OUT_RELOC(batch_bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, 0);
144                 BLIT_RELOC_UDW(batch->devid);
145                 OUT_BATCH((0 << 16) | 0); /* src x1, y1 */
146                 OUT_BATCH(4096);
147                 OUT_RELOC(sample_batch_bo, I915_GEM_DOMAIN_RENDER, 0, 0);
148                 BLIT_RELOC_UDW(batch->devid);
149                 ADVANCE_BATCH();
150
151                 intel_batchbuffer_flush(batch);
152                 if (i % 100 == 0)
153                         gem_sync(fd, batch_bo->handle);
154
155                 drm_intel_bo_disable_reuse(batch_bo);
156
157                 /* launch the newly created batch */
158                 exec(fd, batch_bo->handle);
159
160                 // leak buffers
161                 //drm_intel_bo_unreference(batch_bo);
162                 igt_progress("gem_cs_prefetch: ", i, count);
163         }
164
165         igt_info("Test suceeded, cleanup up - this might take a while.\n");
166         drm_intel_bufmgr_destroy(bufmgr);
167
168         close(fd);
169 }