kms_rotation_crc: Allow the sprite test to run even without universal planes
[platform/upstream/intel-gpu-tools.git] / tests / gem_wait_render_timeout.c
1 /*
2  * Copyright © 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  *    Ben Widawsky <ben@bwidawsk.net>
25  *
26  */
27
28 #include <stdio.h>
29 #include <time.h>
30 #include <stdlib.h>
31 #include <sys/ioctl.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <inttypes.h>
36 #include <errno.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include <getopt.h>
40
41 #include <drm.h>
42
43 #include "ioctl_wrappers.h"
44 #include "drmtest.h"
45 #include "intel_bufmgr.h"
46 #include "intel_batchbuffer.h"
47 #include "intel_io.h"
48 #include "intel_chipset.h"
49 #include "igt_aux.h"
50
51 #define MSEC_PER_SEC    1000L
52 #define USEC_PER_MSEC   1000L
53 #define NSEC_PER_USEC   1000L
54 #define NSEC_PER_MSEC   1000000L
55 #define USEC_PER_SEC    1000000L
56 #define NSEC_PER_SEC    1000000000L
57
58 #define ENOUGH_WORK_IN_SECONDS 2
59 #define BUF_SIZE (8<<20)
60 #define BUF_PAGES ((8<<20)>>12)
61 drm_intel_bo *dst, *dst2;
62
63 /* returns time diff in milliseconds */
64 static int64_t
65 do_time_diff(struct timespec *end, struct timespec *start)
66 {
67         int64_t ret;
68         ret = (MSEC_PER_SEC * difftime(end->tv_sec, start->tv_sec)) +
69               ((end->tv_nsec/NSEC_PER_MSEC) - (start->tv_nsec/NSEC_PER_MSEC));
70         return ret;
71 }
72
73 /* to avoid stupid depencies on libdrm, copy&paste */
74 struct local_drm_i915_gem_wait {
75         /** Handle of BO we shall wait on */
76         __u32 bo_handle;
77         __u32 flags;
78         /** Number of nanoseconds to wait, Returns time remaining. */
79         __u64 timeout_ns;
80 };
81
82 # define WAIT_IOCTL DRM_IOWR(DRM_COMMAND_BASE + 0x2c, struct local_drm_i915_gem_wait)
83
84 static int
85 gem_bo_wait_timeout(int fd, uint32_t handle, uint64_t *timeout_ns)
86 {
87         struct local_drm_i915_gem_wait wait;
88         int ret;
89
90         igt_assert(timeout_ns);
91
92         wait.bo_handle = handle;
93         wait.timeout_ns = *timeout_ns;
94         wait.flags = 0;
95         ret = drmIoctl(fd, WAIT_IOCTL, &wait);
96         *timeout_ns = wait.timeout_ns;
97
98         return ret ? -errno : 0;
99 }
100
101 static void blt_color_fill(struct intel_batchbuffer *batch,
102                            drm_intel_bo *buf,
103                            const unsigned int pages)
104 {
105         const unsigned short height = pages/4;
106         const unsigned short width =  4096;
107
108         if (intel_gen(batch->devid) >= 8) {
109                 BEGIN_BATCH(8);
110                 OUT_BATCH(MI_NOOP);
111                 OUT_BATCH(XY_COLOR_BLT_CMD_NOLEN | 5 |
112                           COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB);
113         } else {
114                 BEGIN_BATCH(6);
115                 OUT_BATCH(XY_COLOR_BLT_CMD_NOLEN | 4 |
116                           COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB);
117         }
118         OUT_BATCH((3 << 24)     | /* 32 Bit Color */
119                   (0xF0 << 16)  | /* Raster OP copy background register */
120                   0);             /* Dest pitch is 0 */
121         OUT_BATCH(0);
122         OUT_BATCH(width << 16   |
123                   height);
124         OUT_RELOC(buf, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, 0);
125         if (intel_gen(batch->devid) >= 8)
126                 OUT_BATCH(0);
127         OUT_BATCH(rand()); /* random pattern */
128         ADVANCE_BATCH();
129 }
130
131 igt_simple_main
132 {
133         drm_intel_bufmgr *bufmgr;
134         struct intel_batchbuffer *batch;
135         uint64_t timeout = ENOUGH_WORK_IN_SECONDS * NSEC_PER_SEC;
136         int fd, ret;
137         const bool do_signals = true; /* signals will seem to make the operation
138                                        * use less process CPU time */
139         bool done = false;
140         int i, iter = 1;
141
142         igt_skip_on_simulation();
143
144         fd = drm_open_any();
145
146         bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
147         drm_intel_bufmgr_gem_enable_reuse(bufmgr);
148         batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
149
150         dst = drm_intel_bo_alloc(bufmgr, "dst", BUF_SIZE, 4096);
151         dst2 = drm_intel_bo_alloc(bufmgr, "dst2", BUF_SIZE, 4096);
152
153         igt_skip_on_f(gem_bo_wait_timeout(fd, dst->handle, &timeout) == -EINVAL,
154                       "kernel doesn't support wait_timeout, skipping test\n");
155         timeout = ENOUGH_WORK_IN_SECONDS * NSEC_PER_SEC;
156
157         /* Figure out a rough number of fills required to consume 1 second of
158          * GPU work.
159          */
160         do {
161                 struct timespec start, end;
162                 long diff;
163
164 #ifndef CLOCK_MONOTONIC_RAW
165 #define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
166 #endif
167
168                 igt_assert(clock_gettime(CLOCK_MONOTONIC_RAW, &start) == 0);
169                 for (i = 0; i < iter; i++)
170                         blt_color_fill(batch, dst, BUF_PAGES);
171                 intel_batchbuffer_flush(batch);
172                 drm_intel_bo_wait_rendering(dst);
173                 igt_assert(clock_gettime(CLOCK_MONOTONIC_RAW, &end) == 0);
174
175                 diff = do_time_diff(&end, &start);
176                 igt_assert(diff >= 0);
177
178                 if ((diff / MSEC_PER_SEC) > ENOUGH_WORK_IN_SECONDS)
179                         done = true;
180                 else
181                         iter <<= 1;
182         } while (!done && iter < 1000000);
183
184         igt_assert_cmpint(iter, <, 1000000);
185
186         igt_info("%d iters is enough work\n", iter);
187         gem_quiescent_gpu(fd);
188         if (do_signals)
189                 igt_fork_signal_helper();
190
191         /* We should be able to do half as much work in the same amount of time,
192          * but because we might schedule almost twice as much as required, we
193          * might accidentally time out. Hence add some fudge. */
194         for (i = 0; i < iter/3; i++)
195                 blt_color_fill(batch, dst2, BUF_PAGES);
196
197         intel_batchbuffer_flush(batch);
198         igt_assert(gem_bo_busy(fd, dst2->handle) == true);
199
200         igt_assert(gem_bo_wait_timeout(fd, dst2->handle, &timeout) == 0);
201         igt_assert(gem_bo_busy(fd, dst2->handle) == false);
202         igt_assert_cmpint(timeout, !=, 0);
203         if (timeout ==  (ENOUGH_WORK_IN_SECONDS * NSEC_PER_SEC))
204                 igt_info("Buffer was already done!\n");
205         else {
206                 igt_info("Finished with %lu time remaining\n", timeout);
207         }
208
209         /* check that polling with timeout=0 works. */
210         timeout = 0;
211         igt_assert(gem_bo_wait_timeout(fd, dst2->handle, &timeout) == 0);
212         igt_assert(timeout == 0);
213
214         /* Now check that we correctly time out, twice the auto-tune load should
215          * be good enough. */
216         timeout = ENOUGH_WORK_IN_SECONDS * NSEC_PER_SEC;
217         for (i = 0; i < iter*2; i++)
218                 blt_color_fill(batch, dst2, BUF_PAGES);
219
220         intel_batchbuffer_flush(batch);
221
222         ret = gem_bo_wait_timeout(fd, dst2->handle, &timeout);
223         igt_assert(ret == -ETIME);
224         igt_assert(timeout == 0);
225         igt_assert(gem_bo_busy(fd, dst2->handle) == true);
226
227         /* check that polling with timeout=0 works. */
228         timeout = 0;
229         igt_assert(gem_bo_wait_timeout(fd, dst2->handle, &timeout) == -ETIME);
230         igt_assert(timeout == 0);
231
232
233         if (do_signals)
234                 igt_stop_signal_helper();
235         drm_intel_bo_unreference(dst2);
236         drm_intel_bo_unreference(dst);
237         intel_batchbuffer_free(batch);
238         drm_intel_bufmgr_destroy(bufmgr);
239
240         close(fd);
241 }