lib/igt_kms: Unify pipe name helpers
[platform/upstream/intel-gpu-tools.git] / tests / gem_evict_alignment.c
1 /*
2  * Copyright © 2011,2012 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  *    Daniel Vetter <daniel.vetter@ffwll.ch>
26  *
27  */
28
29 /*
30  * Testcase: run a couple of big batches to force the unbind on misalignment code.
31  */
32
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdint.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/ioctl.h>
43 #include <sys/time.h>
44
45 #include <drm.h>
46
47 #include "ioctl_wrappers.h"
48 #include "drmtest.h"
49 #include "intel_chipset.h"
50 #include "igt_aux.h"
51
52 #define HEIGHT 256
53 #define WIDTH 1024
54
55 static void
56 copy(int fd, uint32_t dst, uint32_t src, uint32_t *all_bo, int n_bo, int alignment, int error)
57 {
58         uint32_t batch[12];
59         struct drm_i915_gem_relocation_entry reloc[2];
60         struct drm_i915_gem_exec_object2 *obj;
61         struct drm_i915_gem_execbuffer2 exec;
62         uint32_t handle;
63         int n, ret, i=0;
64
65         batch[i++] = (XY_SRC_COPY_BLT_CMD |
66                     XY_SRC_COPY_BLT_WRITE_ALPHA |
67                     XY_SRC_COPY_BLT_WRITE_RGB | 6);
68         if (intel_gen(intel_get_drm_devid(fd)) >= 8)
69                 batch[i - 1] += 2;
70         batch[i++] = (3 << 24) | /* 32 bits */
71                   (0xcc << 16) | /* copy ROP */
72                   WIDTH*4;
73         batch[i++] = 0; /* dst x1,y1 */
74         batch[i++] = (HEIGHT << 16) | WIDTH; /* dst x2,y2 */
75         batch[i++] = 0; /* dst reloc */
76         if (intel_gen(intel_get_drm_devid(fd)) >= 8)
77                 batch[i++] = 0; /* FIXME */
78         batch[i++] = 0; /* src x1,y1 */
79         batch[i++] = WIDTH*4;
80         batch[i++] = 0; /* src reloc */
81         if (intel_gen(intel_get_drm_devid(fd)) >= 8)
82                 batch[i++] = 0; /* FIXME */
83         batch[i++] = MI_BATCH_BUFFER_END;
84         batch[i++] = MI_NOOP;
85
86         handle = gem_create(fd, 4096);
87         gem_write(fd, handle, 0, batch, sizeof(batch));
88
89         reloc[0].target_handle = dst;
90         reloc[0].delta = 0;
91         reloc[0].offset = 4 * sizeof(batch[0]);
92         reloc[0].presumed_offset = 0;
93         reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
94         reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
95
96         reloc[1].target_handle = src;
97         reloc[1].delta = 0;
98         reloc[1].offset = 7 * sizeof(batch[0]);
99         reloc[1].presumed_offset = 0;
100         reloc[1].read_domains = I915_GEM_DOMAIN_RENDER;
101         reloc[1].write_domain = 0;
102
103         obj = calloc(n_bo + 1, sizeof(*obj));
104         for (n = 0; n < n_bo; n++) {
105                 obj[n].handle = all_bo[n];
106                 obj[n].alignment = alignment;
107         }
108         obj[n].handle = handle;
109         obj[n].relocation_count = 2;
110         obj[n].relocs_ptr = (uintptr_t)reloc;
111
112         exec.buffers_ptr = (uintptr_t)obj;
113         exec.buffer_count = n_bo + 1;
114         exec.batch_start_offset = 0;
115         exec.batch_len = i * 4;
116         exec.DR1 = exec.DR4 = 0;
117         exec.num_cliprects = 0;
118         exec.cliprects_ptr = 0;
119         exec.flags = HAS_BLT_RING(intel_get_drm_devid(fd)) ? I915_EXEC_BLT : 0;
120         i915_execbuffer2_set_context_id(exec, 0);
121         exec.rsvd2 = 0;
122
123         ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
124         if (ret)
125                 ret = errno;
126         igt_assert(ret == error);
127
128         gem_close(fd, handle);
129         free(obj);
130 }
131
132 static void minor_evictions(int fd, int size, int count)
133 {
134         uint32_t *bo, *sel;
135         int n, m, alignment, pass, fail;
136
137         igt_require(intel_check_memory(2*count, size, CHECK_RAM));
138
139         bo = malloc(3*count*sizeof(*bo));
140         igt_assert(bo);
141
142         for (n = 0; n < 2*count; n++)
143                 bo[n] = gem_create(fd, size);
144
145         sel = bo + n;
146         for (alignment = m = 4096; alignment <= size; alignment <<= 1) {
147                 for (fail = 0; fail < 10; fail++) {
148                         for (pass = 0; pass < 100; pass++) {
149                                 for (n = 0; n < count; n++, m += 7)
150                                         sel[n] = bo[m%(2*count)];
151                                 copy(fd, sel[0], sel[1], sel, count, alignment, 0);
152                         }
153                         copy(fd, bo[0], bo[0], bo, 2*count, alignment, ENOSPC);
154                 }
155         }
156
157         for (n = 0; n < 2*count; n++)
158                 gem_close(fd, bo[n]);
159         free(bo);
160 }
161
162 static void major_evictions(int fd, int size, int count)
163 {
164         int n, m, loop, alignment, max;
165         uint32_t *bo;
166
167         igt_require(intel_check_memory(count, size, CHECK_RAM));
168
169         bo = malloc(count*sizeof(*bo));
170         igt_assert(bo);
171
172         for (n = 0; n < count; n++)
173                 bo[n] = gem_create(fd, size);
174
175         max = gem_aperture_size(fd) - size;
176         for (alignment = m = 4096; alignment < max; alignment <<= 1) {
177                 for (loop = 0; loop < 100; loop++, m += 17) {
178                         n = m % count;
179                         copy(fd, bo[n], bo[n], &bo[n], 1, alignment, 0);
180                 }
181         }
182
183         for (n = 0; n < count; n++)
184                 gem_close(fd, bo[n]);
185         free(bo);
186 }
187
188 int fd;
189
190 igt_main
191 {
192         int size, count;
193
194         igt_skip_on_simulation();
195
196         igt_fixture {
197                 fd = drm_open_any();
198         }
199
200         igt_subtest("minor-normal") {
201                 size = 1024 * 1024;
202                 count = 3*gem_aperture_size(fd) / size / 4;
203                 minor_evictions(fd, size, count);
204         }
205
206         igt_subtest("major-normal") {
207                 size = 3*gem_aperture_size(fd) / 4;
208                 count = 4;
209                 major_evictions(fd, size, count);
210         }
211
212         igt_fork_signal_helper();
213         igt_subtest("minor-interruptible") {
214                 size = 1024 * 1024;
215                 count = 3*gem_aperture_size(fd) / size / 4;
216                 minor_evictions(fd, size, count);
217         }
218
219         igt_subtest("major-interruptible") {
220                 size = 3*gem_aperture_size(fd) / 4;
221                 count = 4;
222                 major_evictions(fd, size, count);
223         }
224         igt_stop_signal_helper();
225
226         igt_fixture
227                 close(fd);
228 }