51e223b4214c2b41d105b44bc555fcdeec80ecfd
[platform/upstream/intel-gpu-tools.git] / lib / drmtest.h
1 /*
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
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <assert.h>
32 #include <errno.h>
33 #include <stdbool.h>
34 #include <cairo.h>
35 #include <setjmp.h>
36
37 #include "xf86drm.h"
38 #include "xf86drmMode.h"
39 #include "intel_batchbuffer.h"
40 #include "intel_chipset.h"
41 #include "intel_gpu_tools.h"
42
43 drm_intel_bo * gem_handle_to_libdrm_bo(drm_intel_bufmgr *bufmgr, int fd,
44                                        const char *name, uint32_t handle);
45
46 int drm_get_card(int master);
47 int drm_open_any(void);
48 int drm_open_any_master(void);
49
50 void gem_quiescent_gpu(int fd);
51
52 /* ioctl wrappers and similar stuff for bare metal testing */
53 void gem_set_tiling(int fd, uint32_t handle, int tiling, int stride);
54 bool gem_has_enable_ring(int fd,int param);
55 bool gem_has_bsd(int fd);
56 bool gem_has_blt(int fd);
57 bool gem_has_vebox(int fd);
58 int gem_get_num_rings(int fd);
59
60 void gem_set_caching(int fd, uint32_t handle, int caching);
61 uint32_t gem_get_caching(int fd, uint32_t handle);
62 uint32_t gem_flink(int fd, uint32_t handle);
63 uint32_t gem_open(int fd, uint32_t name);
64 void gem_close(int fd, uint32_t handle);
65 void gem_write(int fd, uint32_t handle, uint32_t offset,  const void *buf, uint32_t size);
66 void gem_read(int fd, uint32_t handle, uint32_t offset, void *buf, uint32_t size);
67 void gem_set_domain(int fd, uint32_t handle,
68                     uint32_t read_domains, uint32_t write_domain);
69 void gem_sync(int fd, uint32_t handle);
70 uint32_t gem_create(int fd, int size);
71
72 void *gem_mmap__gtt(int fd, uint32_t handle, int size, int prot);
73 void *gem_mmap__cpu(int fd, uint32_t handle, int size, int prot);
74 #define gem_mmap gem_mmap__gtt
75
76 uint64_t gem_aperture_size(int fd);
77 uint64_t gem_mappable_aperture_size(void);
78 int gem_madvise(int fd, uint32_t handle, int state);
79
80 /* feature test helpers */
81 bool gem_uses_aliasing_ppgtt(int fd);
82 int gem_available_fences(int fd);
83
84 /* prime */
85 int prime_handle_to_fd(int fd, uint32_t handle);
86 uint32_t prime_fd_to_handle(int fd, int dma_buf_fd);
87
88 /* generally useful helpers */
89 void igt_fork_signal_helper(void);
90 void igt_stop_signal_helper(void);
91 void igt_exchange_int(void *array, unsigned i, unsigned j);
92 void igt_permute_array(void *array, unsigned size,
93                            void (*exchange_func)(void *array,
94                                                  unsigned i,
95                                                  unsigned j));
96 void igt_progress(const char *header, uint64_t i, uint64_t total);
97
98 /* subtest infrastructure */
99 jmp_buf igt_subtest_jmpbuf;
100 void igt_subtest_init(int argc, char **argv);
101 bool __igt_run_subtest(const char *subtest_name);
102 #define igt_subtest(name) for (; __igt_run_subtest((name)) && \
103                                    (setjmp(igt_subtest_jmpbuf) == 0); \
104                                    igt_success())
105 bool igt_only_list_subtests(void);
106 void igt_skip(void);
107 void igt_success(void);
108 void igt_fail(int exitcode) __attribute__((noreturn));
109 int igt_retval(void);
110
111 /* check functions which auto-skip tests by calling igt_skip() */
112 void gem_check_caching(int fd);
113 static inline bool gem_check_vebox(int fd)
114 {
115         if (gem_has_vebox(fd))
116                 return true;
117
118         igt_skip();
119         return false;
120 }
121
122 static inline bool gem_check_bsd(int fd)
123 {
124         if (HAS_BSD_RING(intel_get_drm_devid(fd)))
125                 return true;
126
127         igt_skip();
128         return false;
129 }
130
131 static inline bool gem_check_blt(int fd)
132 {
133         if (HAS_BLT_RING(intel_get_drm_devid(fd)))
134                 return true;
135
136         igt_skip();
137         return false;
138 }
139
140 /* helpers to automatically reduce test runtime in simulation */
141 bool igt_run_in_simulation(void);
142 #define SLOW_QUICK(slow,quick) (igt_run_in_simulation() ? (quick) : (slow))
143 void igt_skip_on_simulation(void);
144
145 /* helpers based upon the libdrm buffer manager */
146 void igt_init_aperture_trashers(drm_intel_bufmgr *bufmgr);
147 void igt_trash_aperture(void);
148 void igt_cleanup_aperture_trashers(void);
149
150 struct kmstest_connector_config {
151         drmModeCrtc *crtc;
152         drmModeConnector *connector;
153         drmModeEncoder *encoder;
154         drmModeModeInfo default_mode;
155         int crtc_idx;
156         int pipe;
157 };
158
159 int kmstest_get_connector_config(int drm_fd, uint32_t connector_id,
160                                  unsigned long crtc_idx_mask,
161                                  struct kmstest_connector_config *config);
162 void kmstest_free_connector_config(struct kmstest_connector_config *config);
163
164 /* helpers to create nice-looking framebuffers */
165 struct kmstest_fb {
166         uint32_t fb_id;
167         uint32_t gem_handle;
168         uint32_t drm_format;
169         int width;
170         int height;
171         int depth;
172         unsigned stride;
173         unsigned size;
174         cairo_t *cairo_ctx;
175 };
176
177 enum kmstest_text_align {
178         align_left,
179         align_bottom    = align_left,
180         align_right     = 0x01,
181         align_top       = 0x02,
182         align_vcenter   = 0x04,
183         align_hcenter   = 0x08,
184 };
185
186 int kmstest_cairo_printf_line(cairo_t *cr, enum kmstest_text_align align,
187                                double yspacing, const char *fmt, ...)
188                                __attribute__((format (printf, 4, 5)));
189
190 unsigned int kmstest_create_fb(int fd, int width, int height, int bpp,
191                                int depth, bool tiled,
192                                struct kmstest_fb *fb_info);
193 unsigned int kmstest_create_fb2(int fd, int width, int height, uint32_t format,
194                                 bool tiled, struct kmstest_fb *fb);
195 void kmstest_remove_fb(int fd, struct kmstest_fb *fb_info);
196 cairo_t *kmstest_get_cairo_ctx(int fd, struct kmstest_fb *fb);
197 void kmstest_paint_color_gradient(cairo_t *cr, int x, int y, int w, int h,
198                                   int r, int g, int b);
199 void kmstest_paint_test_pattern(cairo_t *cr, int width, int height);
200 void kmstest_dump_mode(drmModeModeInfo *mode);
201 int kmstest_get_pipe_from_crtc_id(int fd, int crtc_id);
202 const char *kmstest_format_str(uint32_t drm_format);
203 const char *kmstest_pipe_str(int pipe);
204 void kmstest_get_all_formats(const uint32_t **formats, int *format_count);
205 const char *kmstest_encoder_type_str(int type);
206 const char *kmstest_connector_status_str(int type);
207 const char *kmstest_connector_type_str(int type);
208
209 uint32_t drm_format_to_bpp(uint32_t drm_format);
210
211 inline static void _do_or_die(const char *function, int line, int ret)
212 {
213         if (ret == 0)
214                 return;
215
216         fprintf(stderr, "%s:%d failed, ret=%d, errno=%d\n",
217                 function, line, ret, errno);
218         abort();
219 }
220 #define do_or_die(x) _do_or_die(__FUNCTION__, __LINE__, x)
221 #define do_ioctl(fd, ptr, sz) do_or_die(drmIoctl((fd), (ptr), (sz)))
222
223 typedef void (*igt_exit_handler_t)(int sig);
224
225 /* reliable atexit helpers, also work when killed by a signal (if possible) */
226 int igt_install_exit_handler(igt_exit_handler_t fn);
227 void igt_enable_exit_handler(void);
228 void igt_disable_exit_handler(void);
229
230 /* set vt into graphics mode, required to prevent fbcon from interfering */
231 int igt_set_vt_graphics_mode(void);
232
233 /* prefault disabling, needs the corresponding debugfs interface */
234 int igt_disable_prefault(void);
235 int igt_enable_prefault(void);
236
237 /* suspend and auto-resume system */
238 void igt_system_suspend_autoresume(void);