1 /*
2  * Copyright © 2007,2014 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  *    Daniel Vetter <daniel.vetter@ffwll.ch>
26  *
27  */
28 
29 
30 #ifndef IOCTL_WRAPPERS_H
31 #define IOCTL_WRAPPERS_H
32 
33 #include <stdint.h>
34 #include <stdbool.h>
35 #include <sys/mman.h>
36 #include <intel_bufmgr.h>
37 #include <i915_drm.h>
38 
39 #include "i915/gem_context.h"
40 #include "i915/gem_scheduler.h"
41 
42 /**
43  * igt_ioctl:
44  * @fd: file descriptor
45  * @request: IOCTL request number
46  * @arg: argument pointer
47  *
48  * This is a wrapper around drmIoctl(), which can be augmented with special code
49  * blocks like #igt_while_interruptible.
50  */
51 extern int (*igt_ioctl)(int fd, unsigned long request, void *arg);
52 
53 /* libdrm interfacing */
54 drm_intel_bo * gem_handle_to_libdrm_bo(drm_intel_bufmgr *bufmgr, int fd,
55 				       const char *name, uint32_t handle);
56 
57 /* ioctl_wrappers.c:
58  *
59  * ioctl wrappers and similar stuff for bare metal testing */
60 bool gem_get_tiling(int fd, uint32_t handle, uint32_t *tiling, uint32_t *swizzle);
61 void gem_set_tiling(int fd, uint32_t handle, uint32_t tiling, uint32_t stride);
62 int __gem_set_tiling(int fd, uint32_t handle, uint32_t tiling, uint32_t stride);
63 
64 int __gem_set_caching(int fd, uint32_t handle, uint32_t caching);
65 void gem_set_caching(int fd, uint32_t handle, uint32_t caching);
66 uint32_t gem_get_caching(int fd, uint32_t handle);
67 uint32_t gem_flink(int fd, uint32_t handle);
68 uint32_t gem_open(int fd, uint32_t name);
69 void gem_close(int fd, uint32_t handle);
70 int __gem_write(int fd, uint32_t handle, uint64_t offset, const void *buf, uint64_t length);
71 void gem_write(int fd, uint32_t handle, uint64_t offset,  const void *buf, uint64_t length);
72 void gem_read(int fd, uint32_t handle, uint64_t offset, void *buf, uint64_t length);
73 int __gem_set_domain(int fd, uint32_t handle, uint32_t read, uint32_t write);
74 void gem_set_domain(int fd, uint32_t handle, uint32_t read, uint32_t write);
75 int gem_wait(int fd, uint32_t handle, int64_t *timeout_ns);
76 void gem_sync(int fd, uint32_t handle);
77 bool gem_create__has_stolen_support(int fd);
78 uint32_t __gem_create_stolen(int fd, uint64_t size);
79 uint32_t gem_create_stolen(int fd, uint64_t size);
80 int __gem_create(int fd, uint64_t size, uint32_t *handle);
81 uint32_t gem_create(int fd, uint64_t size);
82 void gem_execbuf_wr(int fd, struct drm_i915_gem_execbuffer2 *execbuf);
83 int __gem_execbuf_wr(int fd, struct drm_i915_gem_execbuffer2 *execbuf);
84 void gem_execbuf(int fd, struct drm_i915_gem_execbuffer2 *execbuf);
85 int __gem_execbuf(int fd, struct drm_i915_gem_execbuffer2 *execbuf);
86 
87 #ifndef I915_GEM_DOMAIN_WC
88 #define I915_GEM_DOMAIN_WC 0x80
89 #endif
90 
91 /**
92  * gem_require_stolen_support:
93  * @fd: open i915 drm file descriptor
94  *
95  * Test macro to query whether support for allocating objects from stolen
96  * memory is available. Automatically skips through igt_require() if not.
97  */
98 #define gem_require_stolen_support(fd) \
99 			igt_require(gem_create__has_stolen_support(fd) && \
100 				    (gem_total_stolen_size(fd) > 0))
101 
102 int gem_madvise(int fd, uint32_t handle, int state);
103 
104 #define LOCAL_I915_GEM_USERPTR       0x33
105 #define LOCAL_IOCTL_I915_GEM_USERPTR DRM_IOWR (DRM_COMMAND_BASE + LOCAL_I915_GEM_USERPTR, struct local_i915_gem_userptr)
106 struct local_i915_gem_userptr {
107 	uint64_t user_ptr;
108 	uint64_t user_size;
109 	uint32_t flags;
110 #define LOCAL_I915_USERPTR_READ_ONLY (1<<0)
111 #define LOCAL_I915_USERPTR_UNSYNCHRONIZED (1<<31)
112 	uint32_t handle;
113 };
114 void gem_userptr(int fd, void *ptr, uint64_t size, int read_only, uint32_t flags, uint32_t *handle);
115 int __gem_userptr(int fd, void *ptr, uint64_t size, int read_only, uint32_t flags, uint32_t *handle);
116 
117 void gem_sw_finish(int fd, uint32_t handle);
118 
119 bool gem_bo_busy(int fd, uint32_t handle);
120 
121 /* feature test helpers */
122 void igt_require_gem(int fd);
123 bool gem_has_llc(int fd);
124 bool gem_has_bsd(int fd);
125 bool gem_has_blt(int fd);
126 bool gem_has_vebox(int fd);
127 bool gem_has_bsd2(int fd);
128 bool gem_uses_ppgtt(int fd);
129 bool gem_uses_full_ppgtt(int fd);
130 int gem_gpu_reset_type(int fd);
131 bool gem_gpu_reset_enabled(int fd);
132 bool gem_engine_reset_enabled(int fd);
133 int gem_available_fences(int fd);
134 uint64_t gem_total_mappable_size(int fd);
135 uint64_t gem_total_stolen_size(int fd);
136 uint64_t gem_available_aperture_size(int fd);
137 uint64_t gem_aperture_size(int fd);
138 uint64_t gem_global_aperture_size(int fd);
139 uint64_t gem_mappable_aperture_size(void);
140 bool gem_has_softpin(int fd);
141 bool gem_has_exec_fence(int fd);
142 
143 /* check functions which auto-skip tests by calling igt_skip() */
144 void gem_require_caching(int fd);
145 void gem_require_ring(int fd, unsigned ring);
146 bool gem_has_mocs_registers(int fd);
147 void gem_require_mocs_registers(int fd);
148 
149 #define gem_has_ring(f, r) gem_context_has_engine(f, 0, r)
150 
151 /* prime */
152 struct local_dma_buf_sync {
153 	uint64_t flags;
154 };
155 
156 #define LOCAL_DMA_BUF_SYNC_READ      (1 << 0)
157 #define LOCAL_DMA_BUF_SYNC_WRITE     (2 << 0)
158 #define LOCAL_DMA_BUF_SYNC_RW        (LOCAL_DMA_BUF_SYNC_READ | LOCAL_DMA_BUF_SYNC_WRITE)
159 #define LOCAL_DMA_BUF_SYNC_START     (0 << 2)
160 #define LOCAL_DMA_BUF_SYNC_END       (1 << 2)
161 #define LOCAL_DMA_BUF_SYNC_VALID_FLAGS_MASK \
162 		(LOCAL_DMA_BUF_SYNC_RW | LOCAL_DMA_BUF_SYNC_END)
163 
164 #define LOCAL_DMA_BUF_BASE 'b'
165 #define LOCAL_DMA_BUF_IOCTL_SYNC _IOW(LOCAL_DMA_BUF_BASE, 0, struct local_dma_buf_sync)
166 
167 int prime_handle_to_fd(int fd, uint32_t handle);
168 #ifndef DRM_RDWR
169 #define DRM_RDWR O_RDWR
170 #endif
171 int prime_handle_to_fd_for_mmap(int fd, uint32_t handle);
172 uint32_t prime_fd_to_handle(int fd, int dma_buf_fd);
173 off_t prime_get_size(int dma_buf_fd);
174 void prime_sync_start(int dma_buf_fd, bool write);
175 void prime_sync_end(int dma_buf_fd, bool write);
176 
177 /* addfb2 fb modifiers */
178 struct local_drm_mode_fb_cmd2 {
179 	uint32_t fb_id;
180 	uint32_t width, height;
181 	uint32_t pixel_format;
182 	uint32_t flags;
183 	uint32_t handles[4];
184 	uint32_t pitches[4];
185 	uint32_t offsets[4];
186 	uint64_t modifier[4];
187 };
188 
189 #define LOCAL_DRM_MODE_FB_MODIFIERS	(1<<1)
190 
191 #define LOCAL_DRM_FORMAT_MOD_VENDOR_INTEL	0x01
192 
193 #define local_fourcc_mod_code(vendor, val) \
194 		((((uint64_t)LOCAL_DRM_FORMAT_MOD_VENDOR_## vendor) << 56) | \
195 		(val & 0x00ffffffffffffffL))
196 
197 #define LOCAL_DRM_FORMAT_MOD_NONE	(0)
198 #define LOCAL_I915_FORMAT_MOD_X_TILED	local_fourcc_mod_code(INTEL, 1)
199 #define LOCAL_I915_FORMAT_MOD_Y_TILED	local_fourcc_mod_code(INTEL, 2)
200 #define LOCAL_I915_FORMAT_MOD_Yf_TILED	local_fourcc_mod_code(INTEL, 3)
201 #define LOCAL_I915_FORMAT_MOD_Y_TILED_CCS	local_fourcc_mod_code(INTEL, 4)
202 #define LOCAL_I915_FORMAT_MOD_Yf_TILED_CCS	local_fourcc_mod_code(INTEL, 5)
203 
204 #define LOCAL_DRM_IOCTL_MODE_ADDFB2	DRM_IOWR(0xB8, \
205 						 struct local_drm_mode_fb_cmd2)
206 
207 #define LOCAL_DRM_CAP_ADDFB2_MODIFIERS	0x10
208 
209 bool igt_has_fb_modifiers(int fd);
210 void igt_require_fb_modifiers(int fd);
211 
212 /**
213  * __kms_addfb:
214  *
215  * Creates a framebuffer object.
216  */
217 int __kms_addfb(int fd, uint32_t handle,
218 		uint32_t width, uint32_t height,
219 		uint32_t pixel_format, uint64_t modifier,
220 		uint32_t strides[4], uint32_t offsets[4],
221 		int num_planes, uint32_t flags, uint32_t *buf_id);
222 
223 /**
224  * to_user_pointer:
225  *
226  * Makes sure that pointer on 32 and 64-bit systems
227  * are casted properly for being sent through an ioctl.
228  */
to_user_pointer(const void * ptr)229 static inline uint64_t to_user_pointer(const void *ptr)
230 {
231 	return (uintptr_t)ptr;
232 }
233 
234 /**
235  * from_user_pointer:
236  *
237  * Casts a 64bit value from an ioctl into a pointer.
238  */
from_user_pointer(uint64_t u64)239 static inline void *from_user_pointer(uint64_t u64)
240 {
241 	return (void *)(uintptr_t)u64;
242 }
243 
244 #endif /* IOCTL_WRAPPERS_H */
245