blob: 27573bfd3384ea0ee8648c8233ceec18e67a097a [file] [log] [blame]
Eric Anholt8c641832009-03-26 17:15:11 -07001/*
2 * Copyright © 2007 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 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
Chris Wilsond0ed9122013-08-29 15:11:47 +010028#ifndef DRMTEST_H
29#define DRMTEST_H
30
Eric Anholt8c641832009-03-26 17:15:11 -070031#include <stdio.h>
32#include <stdlib.h>
33#include <unistd.h>
34#include <assert.h>
35#include <errno.h>
Daniel Vetterafbdc7a2012-03-22 13:47:57 +010036#include <stdbool.h>
Daniel Vetter662d7322012-05-22 14:37:19 +020037#include <cairo.h>
Daniel Vetterba0d2ac2013-08-12 09:31:55 +020038#include <setjmp.h>
Eric Anholt8c641832009-03-26 17:15:11 -070039
40#include "xf86drm.h"
Daniel Vetter17787f32012-05-22 16:15:15 +020041#include "xf86drmMode.h"
Daniel Vetter8f5387e2013-08-13 13:20:58 +020042#include "i915_drm.h"
Daniel Vetterd75d69d2012-01-15 18:32:11 +010043#include "intel_batchbuffer.h"
Daniel Vetter7847ea22013-08-12 11:03:29 +020044#include "intel_chipset.h"
45#include "intel_gpu_tools.h"
Eric Anholt8c641832009-03-26 17:15:11 -070046
Daniel Vetterdf7edaa2012-10-02 10:32:10 +020047drm_intel_bo * gem_handle_to_libdrm_bo(drm_intel_bufmgr *bufmgr, int fd,
48 const char *name, uint32_t handle);
49
Daniel Vetter5951ffb2013-08-19 10:34:34 +020050int drm_get_card(void);
Eric Anholt8c641832009-03-26 17:15:11 -070051int drm_open_any(void);
Daniel Vetteraa67b222012-01-10 14:59:58 +010052
Daniel Vetter9f876602012-01-11 17:19:53 +010053void gem_quiescent_gpu(int fd);
Daniel Vetteraa67b222012-01-10 14:59:58 +010054
Daniel Vetterd75d69d2012-01-15 18:32:11 +010055/* ioctl wrappers and similar stuff for bare metal testing */
Daniel Vetteraa67b222012-01-10 14:59:58 +010056void gem_set_tiling(int fd, uint32_t handle, int tiling, int stride);
Zhong Li8ddf84d2013-05-03 15:54:48 +080057bool gem_has_enable_ring(int fd,int param);
58bool gem_has_bsd(int fd);
59bool gem_has_blt(int fd);
60bool gem_has_vebox(int fd);
61int gem_get_num_rings(int fd);
Daniel Vetter7847ea22013-08-12 11:03:29 +020062
Daniel Vetter7553ad62013-08-12 10:43:59 +020063void gem_set_caching(int fd, uint32_t handle, int caching);
Daniel Vetter40ea6f42013-08-12 08:41:05 +020064uint32_t gem_get_caching(int fd, uint32_t handle);
Daniel Vetter8e46c382013-07-23 22:43:30 +020065uint32_t gem_flink(int fd, uint32_t handle);
66uint32_t gem_open(int fd, uint32_t name);
Daniel Vetter5dd17d32012-01-10 15:07:19 +010067void gem_close(int fd, uint32_t handle);
Daniel Vetter319638b2012-01-10 15:31:11 +010068void gem_write(int fd, uint32_t handle, uint32_t offset, const void *buf, uint32_t size);
Daniel Vetterbd5cf9a2012-01-10 15:37:53 +010069void gem_read(int fd, uint32_t handle, uint32_t offset, void *buf, uint32_t size);
Daniel Vetter673e6b22012-01-10 16:05:34 +010070void gem_set_domain(int fd, uint32_t handle,
71 uint32_t read_domains, uint32_t write_domain);
72void gem_sync(int fd, uint32_t handle);
Daniel Vetter7a6042e2012-01-10 18:29:30 +010073uint32_t gem_create(int fd, int size);
Chris Wilson77586dc2012-06-04 17:29:20 +010074
75void *gem_mmap__gtt(int fd, uint32_t handle, int size, int prot);
76void *gem_mmap__cpu(int fd, uint32_t handle, int size, int prot);
77#define gem_mmap gem_mmap__gtt
78
Daniel Vetter7f944342012-01-15 17:14:56 +010079uint64_t gem_aperture_size(int fd);
Daniel Vetter7b14b092012-01-15 17:40:41 +010080uint64_t gem_mappable_aperture_size(void);
Daniel Vetter16c4f522012-03-20 14:24:14 +010081int gem_madvise(int fd, uint32_t handle, int state);
Daniel Vettercbaa8a32012-01-11 15:33:00 +010082
Daniel Vetter9ad062d2013-08-19 10:55:28 +020083uint32_t gem_context_create(int fd);
84
Daniel Vetterafbdc7a2012-03-22 13:47:57 +010085/* feature test helpers */
86bool gem_uses_aliasing_ppgtt(int fd);
Daniel Vettercf72da52012-04-22 12:35:23 +020087int gem_available_fences(int fd);
Daniel Vetterafbdc7a2012-03-22 13:47:57 +010088
Daniel Vetter6bc22e32012-07-23 09:49:06 +020089/* prime */
90int prime_handle_to_fd(int fd, uint32_t handle);
91uint32_t prime_fd_to_handle(int fd, int dma_buf_fd);
Daniel Vettere58891d2013-08-15 14:00:23 +020092off_t prime_get_size(int dma_buf_fd);
Daniel Vetter6bc22e32012-07-23 09:49:06 +020093
Daniel Vetterd75d69d2012-01-15 18:32:11 +010094/* generally useful helpers */
Daniel Vetter1caaf0a2013-08-12 12:17:35 +020095void igt_fork_signal_helper(void);
96void igt_stop_signal_helper(void);
97void igt_exchange_int(void *array, unsigned i, unsigned j);
98void igt_permute_array(void *array, unsigned size,
Daniel Vetterfbfe3742012-01-22 20:09:54 +010099 void (*exchange_func)(void *array,
100 unsigned i,
101 unsigned j));
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200102void igt_progress(const char *header, uint64_t i, uint64_t total);
Daniel Vetter5bc03242013-07-19 19:08:43 +0200103
104/* subtest infrastructure */
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200105jmp_buf igt_subtest_jmpbuf;
106void igt_subtest_init(int argc, char **argv);
Imre Deak5cabaae2013-08-03 02:10:11 +0300107typedef int (*igt_opt_handler_t)(int opt, int opt_index);
108struct option;
109int igt_subtest_init_parse_opts(int argc, char **argv,
110 const char *extra_short_opts,
111 struct option *extra_long_opts,
112 const char *help_str,
113 igt_opt_handler_t opt_handler);
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200114bool __igt_run_subtest(const char *subtest_name);
Daniel Vetterf8c0dfe2013-08-14 16:51:26 +0200115/**
116 * igt_subtest/_f - Denote a subtest code block
117 *
118 * Magic control flow which denotes a subtest code block. Within that codeblock
119 * igt_skip|success will only bail out of the subtest. The _f variant accepts a
120 * printf format string, which is useful for constructing combinatorial tests.
121 */
Daniel Vetter62346582013-08-14 13:47:47 +0200122#define igt_tokencat2(x, y) x ## y
123#define igt_tokencat(x, y) igt_tokencat2(x, y)
124#define __igt_subtest_f(tmp, format, args...) \
125 for (char tmp [256]; \
126 snprintf( tmp , sizeof( tmp ), \
127 format, args), \
128 __igt_run_subtest( tmp ) && \
129 (setjmp(igt_subtest_jmpbuf) == 0); \
130 igt_success())
131#define igt_subtest_f(f, a...) \
132 __igt_subtest_f(igt_tokencat(__tmpchar, __LINE__), f, a)
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200133#define igt_subtest(name) for (; __igt_run_subtest((name)) && \
134 (setjmp(igt_subtest_jmpbuf) == 0); \
135 igt_success())
Daniel Vetter15c19222013-08-14 18:03:09 +0200136const char *igt_subtest_name(void);
Daniel Vetterf8c0dfe2013-08-14 16:51:26 +0200137/**
138 * igt_skip - subtest aware test skipping
139 *
140 * For tests with subtests this will either bail out of the current subtest or
141 * mark all subsequent subtests as SKIP (in case some global setup code failed).
142 *
143 * For normal tests without subtest it will directly exit.
144 */
Daniel Vetter8dbd1fb2013-08-19 11:09:25 +0200145__attribute__((format(printf, 1, 2))) void igt_skip(const char *f, ...);
Daniel Vetter8f5387e2013-08-13 13:20:58 +0200146void __igt_skip_check(const char *file, const int line,
147 const char *func, const char *check);
Daniel Vetterf8c0dfe2013-08-14 16:51:26 +0200148/**
149 * igt_success - complete a (subtest) as successfull
150 *
151 * This bails out of a subtests and marks it as successful. For global tests it
152 * it won't bail out of anything.
153 */
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200154void igt_success(void);
Daniel Vetterf8c0dfe2013-08-14 16:51:26 +0200155/**
156 * igt_fail - fail a testcase
157 *
158 * For subtest it just bails out of the subtest, when run in global context it
159 * will exit. Note that it won't attempt to keep on running further tests,
160 * presuming that some mandatory setup failed.
161 */
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200162void igt_fail(int exitcode) __attribute__((noreturn));
Daniel Vetter646a6fe2013-08-13 11:52:01 +0200163void __igt_fail_assert(int exitcode, const char *file,
164 const int line, const char *func, const char *assertion)
165 __attribute__((noreturn));
Daniel Vetterf8c0dfe2013-08-14 16:51:26 +0200166/**
167 * igt_exit - exit() for igts
168 *
169 * This will exit the test with the right exit code when subtests have been
170 * skipped. For normal tests it exits with a successful exit code, presuming
171 * everything has worked out. For subtests it also checks that at least one
172 * subtest has been run (save when only listing subtests.
173 */
174void igt_exit(void) __attribute__((noreturn));
Daniel Vetter8f5387e2013-08-13 13:20:58 +0200175/**
176 * igt_assert - fails (sub-)test if a condition is not met
177 *
178 * Should be used everywhere where a test checks results.
179 */
Daniel Vetter646a6fe2013-08-13 11:52:01 +0200180#define igt_assert(expr) do { if (!(expr)) __igt_fail_assert(99, __FILE__, __LINE__, __func__, #expr ); } while (0)
Daniel Vetter8f5387e2013-08-13 13:20:58 +0200181/**
182 * igt_require - skip a (sub-)test if a condition is not met
183 *
184 * This is useful to streamline the skip logic since it allows for a more flat
185 * code control flow.
186 */
187#define igt_require(expr) do { if (!(expr)) __igt_skip_check(__FILE__, __LINE__, __func__, #expr ); } while (0)
Daniel Vetterd75d69d2012-01-15 18:32:11 +0100188
Daniel Vetter78865842013-08-19 07:23:49 +0200189bool __igt_fixture(void);
Daniel Vetter8869e1e2013-08-26 20:52:14 +0200190void __igt_fixture_complete(void);
Daniel Vetter78865842013-08-19 07:23:49 +0200191void __igt_fixture_end(void) __attribute__((noreturn));
Daniel Vetter696c8f62013-08-14 18:30:03 +0200192/**
193 * igt_fixture - annote global test fixture code
194 *
195 * Testcase with subtests often need to set up a bunch of global state as the
196 * common test fixture. To avoid such code interferring with the subtest
197 * enumeration (e.g. when enumerating on systemes without an intel gpu) such
198 * blocks should be annotated with igt_fixture.
199 */
Daniel Vetter8869e1e2013-08-26 20:52:14 +0200200#define igt_fixture for (int igt_tokencat(__tmpint,__LINE__) = 0; \
201 igt_tokencat(__tmpint,__LINE__) < 1 && \
202 __igt_fixture() && \
Daniel Vetter78865842013-08-19 07:23:49 +0200203 (setjmp(igt_subtest_jmpbuf) == 0); \
Daniel Vetter8869e1e2013-08-26 20:52:14 +0200204 igt_tokencat(__tmpint,__LINE__) ++, \
205 __igt_fixture_complete())
Daniel Vetter2dbd9982013-08-14 15:48:54 +0200206
Daniel Vettercd1f2202013-08-29 10:06:51 +0200207bool __igt_fork(void);
208/**
209 * igt_fork - fork parallel test threads with fork()
210 * @child: name of the int variable with the child number
211 * @num_children: number of children to fork
212 *
213 * Joining all test threads should be done with igt_waitchildren to ensure that
214 * the exit codes of all children are properly reflected in the test status.
215 */
216#define igt_fork(child, num_children) \
217 for (int child = 0; child < (num_children); child++) \
218 for (; __igt_fork(); exit(0))
219void igt_waitchildren(void);
220
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200221/* check functions which auto-skip tests by calling igt_skip() */
Daniel Vetter8f5387e2013-08-13 13:20:58 +0200222void gem_require_caching(int fd);
223static inline void gem_require_ring(int fd, int ring_id)
Daniel Vetter7847ea22013-08-12 11:03:29 +0200224{
Daniel Vetter8f5387e2013-08-13 13:20:58 +0200225 switch (ring_id) {
226 case I915_EXEC_RENDER:
227 return;
228 case I915_EXEC_BLT:
229 igt_require(HAS_BLT_RING(intel_get_drm_devid(fd)));
230 return;
231 case I915_EXEC_BSD:
232 igt_require(HAS_BSD_RING(intel_get_drm_devid(fd)));
233 return;
Chris Wilson55b8c332013-08-19 18:15:04 +0100234#ifdef I915_EXEC_VEBOX
Daniel Vetter8f5387e2013-08-13 13:20:58 +0200235 case I915_EXEC_VEBOX:
236 igt_require(gem_has_vebox(fd));
237 return;
Chris Wilson55b8c332013-08-19 18:15:04 +0100238#endif
Daniel Vetter8f5387e2013-08-13 13:20:58 +0200239 default:
240 assert(0);
241 return;
242 }
Daniel Vetter7847ea22013-08-12 11:03:29 +0200243}
244
Daniel Vetter5bc03242013-07-19 19:08:43 +0200245/* helpers to automatically reduce test runtime in simulation */
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200246bool igt_run_in_simulation(void);
247#define SLOW_QUICK(slow,quick) (igt_run_in_simulation() ? (quick) : (slow))
Daniel Vetterf8c0dfe2013-08-14 16:51:26 +0200248/**
249 * igt_skip_on_simulation - skip tests when INTEL_SIMULATION env war is set
250 *
251 * Skip the test when running on simulation (and that's relevant only when
252 * we're not in the mode where we list the subtests).
253 *
254 * This function is subtest aware (since it uses igt_skip) and so can be used to
255 * skip specific subtests or all subsequent subtests.
256 */
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200257void igt_skip_on_simulation(void);
Damien Lespiaubb33d082013-02-13 16:29:01 +0000258
Daniel Vetterd75d69d2012-01-15 18:32:11 +0100259/* helpers based upon the libdrm buffer manager */
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200260void igt_init_aperture_trashers(drm_intel_bufmgr *bufmgr);
261void igt_trash_aperture(void);
262void igt_cleanup_aperture_trashers(void);
Chris Wilsonb7bd1832012-03-29 12:45:59 +0100263
Imre Deakbfb0cdd2013-05-31 00:21:43 +0300264struct kmstest_connector_config {
265 drmModeCrtc *crtc;
266 drmModeConnector *connector;
267 drmModeEncoder *encoder;
268 drmModeModeInfo default_mode;
269 int crtc_idx;
270 int pipe;
271};
272
Imre Deak9a9f1312013-08-01 15:08:19 +0300273int kmstest_get_connector_default_mode(int drm_fd, drmModeConnector *connector,
274 drmModeModeInfo *mode);
Imre Deakbfb0cdd2013-05-31 00:21:43 +0300275int kmstest_get_connector_config(int drm_fd, uint32_t connector_id,
276 unsigned long crtc_idx_mask,
277 struct kmstest_connector_config *config);
278void kmstest_free_connector_config(struct kmstest_connector_config *config);
279
Daniel Vetter662d7322012-05-22 14:37:19 +0200280/* helpers to create nice-looking framebuffers */
281struct kmstest_fb {
282 uint32_t fb_id;
283 uint32_t gem_handle;
Imre Deakf68d9642013-05-24 17:26:54 +0300284 uint32_t drm_format;
285 int width;
286 int height;
287 int depth;
Daniel Vetter662d7322012-05-22 14:37:19 +0200288 unsigned stride;
Chris Wilsond0ed9122013-08-29 15:11:47 +0100289 unsigned tiling;
Daniel Vetter662d7322012-05-22 14:37:19 +0200290 unsigned size;
Imre Deakf68d9642013-05-24 17:26:54 +0300291 cairo_t *cairo_ctx;
Daniel Vetter662d7322012-05-22 14:37:19 +0200292};
293
Imre Deak542a40c2013-05-30 15:03:48 +0300294enum kmstest_text_align {
295 align_left,
296 align_bottom = align_left,
297 align_right = 0x01,
298 align_top = 0x02,
299 align_vcenter = 0x04,
300 align_hcenter = 0x08,
301};
302
303int kmstest_cairo_printf_line(cairo_t *cr, enum kmstest_text_align align,
304 double yspacing, const char *fmt, ...)
305 __attribute__((format (printf, 4, 5)));
306
Daniel Vetter662d7322012-05-22 14:37:19 +0200307unsigned int kmstest_create_fb(int fd, int width, int height, int bpp,
308 int depth, bool tiled,
Imre Deakf68d9642013-05-24 17:26:54 +0300309 struct kmstest_fb *fb_info);
Imre Deak528b1f32013-05-30 22:59:59 +0300310unsigned int kmstest_create_fb2(int fd, int width, int height, uint32_t format,
311 bool tiled, struct kmstest_fb *fb);
Imre Deakf68d9642013-05-24 17:26:54 +0300312void kmstest_remove_fb(int fd, struct kmstest_fb *fb_info);
313cairo_t *kmstest_get_cairo_ctx(int fd, struct kmstest_fb *fb);
Imre Deak528b1f32013-05-30 22:59:59 +0300314void kmstest_paint_color_gradient(cairo_t *cr, int x, int y, int w, int h,
315 int r, int g, int b);
Imre Deakf68d9642013-05-24 17:26:54 +0300316void kmstest_paint_test_pattern(cairo_t *cr, int width, int height);
Daniel Vetter17787f32012-05-22 16:15:15 +0200317void kmstest_dump_mode(drmModeModeInfo *mode);
Daniel Vetter0af2b182012-12-15 01:06:11 +0100318int kmstest_get_pipe_from_crtc_id(int fd, int crtc_id);
Imre Deak528b1f32013-05-30 22:59:59 +0300319const char *kmstest_format_str(uint32_t drm_format);
320const char *kmstest_pipe_str(int pipe);
321void kmstest_get_all_formats(const uint32_t **formats, int *format_count);
Imre Deak3a2aed12013-05-30 22:53:53 +0300322const char *kmstest_encoder_type_str(int type);
323const char *kmstest_connector_status_str(int type);
324const char *kmstest_connector_type_str(int type);
Daniel Vetter662d7322012-05-22 14:37:19 +0200325
Imre Deak24de8ba2013-07-29 17:19:58 +0300326uint32_t drm_format_to_bpp(uint32_t drm_format);
327
Daniel Vetter79abed32013-08-15 11:55:32 +0200328#define do_or_die(x) igt_assert((x) == 0)
329#define do_ioctl(fd, ptr, sz) igt_assert(drmIoctl((fd), (ptr), (sz)) == 0)
Imre Deakcf264352013-05-08 19:06:42 +0300330
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200331typedef void (*igt_exit_handler_t)(int sig);
Imre Deakbce7d8a2013-05-22 17:40:46 +0300332
Daniel Vetter5bc03242013-07-19 19:08:43 +0200333/* reliable atexit helpers, also work when killed by a signal (if possible) */
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200334int igt_install_exit_handler(igt_exit_handler_t fn);
335void igt_enable_exit_handler(void);
336void igt_disable_exit_handler(void);
Imre Deakbce7d8a2013-05-22 17:40:46 +0300337
Daniel Vetter5bc03242013-07-19 19:08:43 +0200338/* set vt into graphics mode, required to prevent fbcon from interfering */
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200339int igt_set_vt_graphics_mode(void);
Xiong Zhang7ea29832013-07-19 18:42:51 +0800340
Daniel Vetter5bc03242013-07-19 19:08:43 +0200341/* prefault disabling, needs the corresponding debugfs interface */
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200342int igt_disable_prefault(void);
343int igt_enable_prefault(void);
Daniel Vetter10834f82013-07-22 09:24:43 +0200344
345/* suspend and auto-resume system */
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200346void igt_system_suspend_autoresume(void);
Chris Wilsond0ed9122013-08-29 15:11:47 +0100347
348#endif /* DRMTEST_H */