kms_rotation_crc: Remove useless comments
[platform/upstream/intel-gpu-tools.git] / tests / gem_pin.c
1 /*
2  * Copyright © 20013 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 /* Exercises pinning of small bo */
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <inttypes.h>
35 #include <errno.h>
36 #include <sys/stat.h>
37 #include <sys/time.h>
38
39 #include <drm.h>
40
41 #include "ioctl_wrappers.h"
42 #include "drmtest.h"
43 #include "intel_chipset.h"
44 #include "intel_io.h"
45 #include "igt_aux.h"
46
47 #define COPY_BLT_CMD            (2<<29|0x53<<22|0x6)
48 #define BLT_WRITE_ALPHA         (1<<21)
49 #define BLT_WRITE_RGB           (1<<20)
50
51 static void exec(int fd, uint32_t handle, uint32_t offset)
52 {
53         struct drm_i915_gem_execbuffer2 execbuf;
54         struct drm_i915_gem_exec_object2 gem_exec[1];
55         struct drm_i915_gem_relocation_entry gem_reloc[1];
56
57         gem_reloc[0].offset = 1024;
58         gem_reloc[0].delta = 0;
59         gem_reloc[0].target_handle = handle;
60         gem_reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
61         gem_reloc[0].write_domain = 0;
62         gem_reloc[0].presumed_offset = 0;
63
64         gem_exec[0].handle = handle;
65         gem_exec[0].relocation_count = 1;
66         gem_exec[0].relocs_ptr = (uintptr_t) gem_reloc;
67         gem_exec[0].alignment = 0;
68         gem_exec[0].offset = 0;
69         gem_exec[0].flags = 0;
70         gem_exec[0].rsvd1 = 0;
71         gem_exec[0].rsvd2 = 0;
72
73         execbuf.buffers_ptr = (uintptr_t)gem_exec;
74         execbuf.buffer_count = 1;
75         execbuf.batch_start_offset = 0;
76         execbuf.batch_len = 8;
77         execbuf.cliprects_ptr = 0;
78         execbuf.num_cliprects = 0;
79         execbuf.DR1 = 0;
80         execbuf.DR4 = 0;
81         execbuf.flags = 0;
82         i915_execbuffer2_set_context_id(execbuf, 0);
83         execbuf.rsvd2 = 0;
84
85         do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
86         igt_assert(gem_exec[0].offset == offset);
87 }
88
89 static int gem_linear_blt(int fd,
90                           uint32_t *batch,
91                           uint32_t src,
92                           uint32_t dst,
93                           uint32_t length,
94                           struct drm_i915_gem_relocation_entry *reloc)
95 {
96         uint32_t *b = batch;
97
98         *b++ = COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB;
99         *b++ = 0x66 << 16 | 1 << 25 | 1 << 24 | (4*1024);
100         *b++ = 0;
101         *b++ = (length / (4*1024)) << 16 | 1024;
102         *b++ = 0;
103         reloc->offset = (b-batch-1) * sizeof(uint32_t);
104         reloc->delta = 0;
105         reloc->target_handle = dst;
106         reloc->read_domains = I915_GEM_DOMAIN_RENDER;
107         reloc->write_domain = I915_GEM_DOMAIN_RENDER;
108         reloc->presumed_offset = 0;
109         reloc++;
110         if (intel_gen(intel_get_drm_devid(fd)) >= 8)
111                 *b++ = 0; /* FIXME */
112
113         *b++ = 0;
114         *b++ = 4*1024;
115         *b++ = 0;
116         reloc->offset = (b-batch-1) * sizeof(uint32_t);
117         reloc->delta = 0;
118         reloc->target_handle = src;
119         reloc->read_domains = I915_GEM_DOMAIN_RENDER;
120         reloc->write_domain = 0;
121         reloc->presumed_offset = 0;
122         reloc++;
123         if (intel_gen(intel_get_drm_devid(fd)) >= 8)
124                 *b++ = 0; /* FIXME */
125
126         *b++ = MI_BATCH_BUFFER_END;
127         *b++ = 0;
128
129         return (b - batch) * sizeof(uint32_t);
130 }
131
132 static void make_busy(int fd, uint32_t handle)
133 {
134         struct drm_i915_gem_execbuffer2 execbuf;
135         struct drm_i915_gem_exec_object2 obj[2];
136         struct drm_i915_gem_relocation_entry reloc[2];
137         uint32_t batch[20];
138         uint32_t tmp;
139         int count;
140
141         tmp = gem_create(fd, 1024*1024);
142
143         obj[0].handle = tmp;
144         obj[0].relocation_count = 0;
145         obj[0].relocs_ptr = 0;
146         obj[0].alignment = 0;
147         obj[0].offset = 0;
148         obj[0].flags = 0;
149         obj[0].rsvd1 = 0;
150         obj[0].rsvd2 = 0;
151
152         obj[1].handle = handle;
153         obj[1].relocation_count = 2;
154         obj[1].relocs_ptr = (uintptr_t) reloc;
155         obj[1].alignment = 0;
156         obj[1].offset = 0;
157         obj[1].flags = 0;
158         obj[1].rsvd1 = 0;
159         obj[1].rsvd2 = 0;
160
161         execbuf.buffers_ptr = (uintptr_t)obj;
162         execbuf.buffer_count = 2;
163         execbuf.batch_start_offset = 0;
164         execbuf.batch_len = gem_linear_blt(fd, batch, tmp, tmp, 1024*1024,reloc);
165         execbuf.cliprects_ptr = 0;
166         execbuf.num_cliprects = 0;
167         execbuf.DR1 = 0;
168         execbuf.DR4 = 0;
169         execbuf.flags = 0;
170         if (HAS_BLT_RING(intel_get_drm_devid(fd)))
171                 execbuf.flags |= I915_EXEC_BLT;
172         i915_execbuffer2_set_context_id(execbuf, 0);
173         execbuf.rsvd2 = 0;
174
175         gem_write(fd, handle, 0, batch, execbuf.batch_len);
176         for (count = 0; count < 10; count++)
177                 do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
178         gem_close(fd, tmp);
179 }
180
181 static int test_can_pin(int fd)
182 {
183         struct drm_i915_gem_pin pin;
184         int ret;
185
186         pin.handle = gem_create(fd, 4096);;
187         pin.alignment = 0;
188         ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_PIN, &pin);
189         gem_close(fd, pin.handle);
190
191         return ret == 0;;
192 }
193
194 static uint32_t gem_pin(int fd, int handle, int alignment)
195 {
196         struct drm_i915_gem_pin pin;
197
198         pin.handle = handle;
199         pin.alignment = alignment;
200         do_ioctl(fd, DRM_IOCTL_I915_GEM_PIN, &pin);
201         return pin.offset;
202 }
203
204 igt_simple_main
205 {
206         const uint32_t batch[2] = {MI_BATCH_BUFFER_END};
207         struct timeval start, now;
208         uint32_t *handle, *offset;
209         int fd, i;
210
211         igt_skip_on_simulation();
212
213         fd = drm_open_any();
214
215         igt_require(test_can_pin(fd));
216
217         handle = malloc(sizeof(uint32_t)*100);
218         offset = malloc(sizeof(uint32_t)*100);
219
220         /* Race creation/use against interrupts */
221         igt_fork_signal_helper();
222         gettimeofday(&start, NULL);
223         do {
224                 for (i = 0; i < 100; i++) {
225                         if (i & 1) {
226                                 /* pin anidle bo */
227                                 handle[i] = gem_create(fd, 4096);
228                                 offset[i] = gem_pin(fd, handle[i], 0);
229                                 igt_assert(offset[i]);
230                                 gem_write(fd, handle[i], 0, batch, sizeof(batch));
231                         } else {
232                                 /* try to pin an anidle bo */
233                                 handle[i] = gem_create(fd, 4096);
234                                 make_busy(fd, handle[i]);
235                                 offset[i] = gem_pin(fd, handle[i], 256*1024);
236                                 igt_assert(offset[i]);
237                                 igt_assert((offset[i] & (256*1024-1)) == 0);
238                                 gem_write(fd, handle[i], 0, batch, sizeof(batch));
239                         }
240                 }
241                 for (i = 0; i < 1000; i++) {
242                         int j = rand() % 100;
243                         exec(fd, handle[j], offset[j]);
244                 }
245                 for (i = 0; i < 100; i++)
246                         gem_close(fd, handle[i]);
247                 gettimeofday(&now, NULL);
248         } while ((now.tv_sec - start.tv_sec)*1000 + (now.tv_usec - start.tv_usec) / 1000 < 10000);
249         igt_stop_signal_helper();
250 }