1 /*
2  * Copyright © 2017 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 
24 #include <errno.h>
25 #include <string.h>
26 
27 #include "ioctl_wrappers.h"
28 #include "drmtest.h"
29 
30 #include "i915/gem_context.h"
31 
32 /**
33  * SECTION:gem_context
34  * @short_description: Helpers for dealing with contexts
35  * @title: GEM Context
36  *
37  * This helper library contains functions used for handling gem contexts.
38  * Conceptually, gem contexts are similar to their CPU counterparts, in that
39  * they are a mix of software and hardware features allowing to isolate some
40  * aspects of task execution. Initially it was just a matter of maintaining
41  * separate state for each context, but more features were added, some
42  * improving contexts isolation (per-context address space), some are just
43  * software features improving submission model (context priority).
44  */
45 
46 /**
47  * gem_has_contexts:
48  * @fd: open i915 drm file descriptor
49  *
50  * Queries whether context creation is supported or not.
51  *
52  * Returns: Context creation availability.
53  */
gem_has_contexts(int fd)54 bool gem_has_contexts(int fd)
55 {
56 	uint32_t ctx_id = 0;
57 
58 	__gem_context_create(fd, &ctx_id);
59 	if (ctx_id)
60 		gem_context_destroy(fd, ctx_id);
61 
62 	return ctx_id;
63 }
64 
65 /**
66  * gem_require_contexts:
67  * @fd: open i915 drm file descriptor
68  *
69  * This helper will automatically skip the test on platforms where context
70  * support is not available.
71  */
gem_require_contexts(int fd)72 void gem_require_contexts(int fd)
73 {
74 	igt_require(gem_has_contexts(fd));
75 }
76 
__gem_context_create(int fd,uint32_t * ctx_id)77 int __gem_context_create(int fd, uint32_t *ctx_id)
78 {
79        struct drm_i915_gem_context_create create;
80        int err = 0;
81 
82        memset(&create, 0, sizeof(create));
83        if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create) == 0) {
84                *ctx_id = create.ctx_id;
85        } else {
86 	       err = -errno;
87 	       igt_assume(err != 0);
88        }
89 
90        errno = 0;
91        return err;
92 }
93 
94 /**
95  * gem_context_create:
96  * @fd: open i915 drm file descriptor
97  *
98  * This wraps the CONTEXT_CREATE ioctl, which is used to allocate a new
99  * context. Note that similarly to gem_set_caching() this wrapper skips on
100  * kernels and platforms where context support is not available.
101  *
102  * Returns: The id of the allocated context.
103  */
gem_context_create(int fd)104 uint32_t gem_context_create(int fd)
105 {
106 	uint32_t ctx_id;
107 
108 	igt_assert_eq(__gem_context_create(fd, &ctx_id), 0);
109 	igt_assert(ctx_id != 0);
110 
111 	return ctx_id;
112 }
113 
__gem_context_destroy(int fd,uint32_t ctx_id)114 int __gem_context_destroy(int fd, uint32_t ctx_id)
115 {
116 	struct drm_i915_gem_context_destroy destroy = { ctx_id };
117 	int err = 0;
118 
119 	if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &destroy)) {
120 		err = -errno;
121 		igt_assume(err);
122 	}
123 
124 	errno = 0;
125 	return err;
126 }
127 
128 /**
129  * gem_context_destroy:
130  * @fd: open i915 drm file descriptor
131  * @ctx_id: i915 context id
132  *
133  * This wraps the CONTEXT_DESTROY ioctl, which is used to free a context.
134  */
gem_context_destroy(int fd,uint32_t ctx_id)135 void gem_context_destroy(int fd, uint32_t ctx_id)
136 {
137 	igt_assert_eq(__gem_context_destroy(fd, ctx_id), 0);
138 }
139 
__gem_context_get_param(int fd,struct drm_i915_gem_context_param * p)140 int __gem_context_get_param(int fd, struct drm_i915_gem_context_param *p)
141 {
142 	int err = 0;
143 
144 	if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM, p)) {
145 		err = -errno;
146 		igt_assume(err);
147 	}
148 
149 	errno = 0;
150 	return err;
151 }
152 
153 /**
154  * gem_context_get_param:
155  * @fd: open i915 drm file descriptor
156  * @p: i915 context parameter
157  *
158  * This wraps the CONTEXT_GET_PARAM ioctl, which is used to get a context
159  * parameter.
160  */
gem_context_get_param(int fd,struct drm_i915_gem_context_param * p)161 void gem_context_get_param(int fd, struct drm_i915_gem_context_param *p)
162 {
163 	igt_assert_eq(__gem_context_get_param(fd, p), 0);
164 }
165 
__gem_context_set_param(int fd,struct drm_i915_gem_context_param * p)166 int __gem_context_set_param(int fd, struct drm_i915_gem_context_param *p)
167 {
168 	int err = 0;
169 
170 	if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_SETPARAM, p)) {
171 		err = -errno;
172 		igt_assume(err);
173 	}
174 
175 	errno = 0;
176 	return err;
177 }
178 
179 /**
180  * gem_context_set_param:
181  * @fd: open i915 drm file descriptor
182  * @p: i915 context parameter
183  *
184  * This wraps the CONTEXT_SET_PARAM ioctl, which is used to set a context
185  * parameter.
186  */
gem_context_set_param(int fd,struct drm_i915_gem_context_param * p)187 void gem_context_set_param(int fd, struct drm_i915_gem_context_param *p)
188 {
189 	igt_assert_eq(__gem_context_set_param(fd, p), 0);
190 }
191 
192 /**
193  * gem_context_require_param:
194  * @fd: open i915 drm file descriptor
195  * @param: i915 context parameter
196  *
197  * Feature test macro to query whether context parameter support for @param
198  * is available. Automatically skips through igt_require() if not.
199  */
gem_context_require_param(int fd,uint64_t param)200 void gem_context_require_param(int fd, uint64_t param)
201 {
202 	struct drm_i915_gem_context_param p = { .param = param };
203 
204 	igt_require(__gem_context_get_param(fd, &p) == 0);
205 }
206 
gem_context_require_bannable(int fd)207 void gem_context_require_bannable(int fd)
208 {
209 	static int has_ban_period = -1;
210 	static int has_bannable = -1;
211 
212 	if (has_bannable < 0) {
213 		struct drm_i915_gem_context_param p;
214 
215 		p.ctx_id = 0;
216 		p.param = I915_CONTEXT_PARAM_BANNABLE;
217 		p.value = 0;
218 		p.size = 0;
219 
220 		has_bannable = igt_ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM, &p) == 0;
221 	}
222 
223 	if (has_ban_period < 0) {
224 		struct drm_i915_gem_context_param p;
225 
226 		p.ctx_id = 0;
227 		p.param = I915_CONTEXT_PARAM_BAN_PERIOD;
228 		p.value = 0;
229 		p.size = 0;
230 
231 		has_ban_period = igt_ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM, &p) == 0;
232 	}
233 
234 	igt_require(has_ban_period || has_bannable);
235 }
236 
237 #define DRM_I915_CONTEXT_PARAM_PRIORITY 0x6
238 
239 /**
240  * __gem_context_set_priority:
241  * @fd: open i915 drm file descriptor
242  * @ctx_id: i915 context id
243  * @prio: desired context priority
244  *
245  * This function modifies priority property of the context.
246  * It is used by the scheduler to decide on the ordering of requests submitted
247  * to the hardware.
248  *
249  * Returns: An integer equal to zero for success and negative for failure
250  */
__gem_context_set_priority(int fd,uint32_t ctx_id,int prio)251 int __gem_context_set_priority(int fd, uint32_t ctx_id, int prio)
252 {
253 	struct drm_i915_gem_context_param p = {
254 		.ctx_id = ctx_id,
255 		.param = DRM_I915_CONTEXT_PARAM_PRIORITY,
256 		.value = prio,
257 	};
258 
259 	return __gem_context_set_param(fd, &p);
260 }
261 
262 /**
263  * gem_context_set_priority:
264  * @fd: open i915 drm file descriptor
265  * @ctx_id: i915 context id
266  * @prio: desired context priority
267  *
268  * Like __gem_context_set_priority(), except we assert on failure.
269  */
gem_context_set_priority(int fd,uint32_t ctx_id,int prio)270 void gem_context_set_priority(int fd, uint32_t ctx_id, int prio)
271 {
272 	igt_assert_eq(__gem_context_set_priority(fd, ctx_id, prio), 0);
273 }
274 
275 int
__gem_context_clone(int i915,uint32_t src,unsigned int share,unsigned int flags,uint32_t * out)276 __gem_context_clone(int i915,
277 		    uint32_t src, unsigned int share,
278 		    unsigned int flags,
279 		    uint32_t *out)
280 {
281 	struct drm_i915_gem_context_create_ext_clone clone = {
282 		{ .name = I915_CONTEXT_CREATE_EXT_CLONE },
283 		.clone_id = src,
284 		.flags = share,
285 	};
286 	struct drm_i915_gem_context_create_ext arg = {
287 		.flags = flags | I915_CONTEXT_CREATE_FLAGS_USE_EXTENSIONS,
288 		.extensions = to_user_pointer(&clone),
289 	};
290 	int err = 0;
291 
292 	if (igt_ioctl(i915, DRM_IOCTL_I915_GEM_CONTEXT_CREATE_EXT, &arg)) {
293 		err = -errno;
294 		igt_assume(err);
295 	}
296 
297 	*out = arg.ctx_id;
298 
299 	errno = 0;
300 	return err;
301 }
302 
__gem_context_has(int i915,uint32_t share,unsigned int flags)303 static bool __gem_context_has(int i915, uint32_t share, unsigned int flags)
304 {
305 	uint32_t ctx;
306 
307 	__gem_context_clone(i915, 0, share, flags, &ctx);
308 	if (ctx)
309 		gem_context_destroy(i915, ctx);
310 
311 	errno = 0;
312 	return ctx;
313 }
314 
gem_contexts_has_shared_gtt(int i915)315 bool gem_contexts_has_shared_gtt(int i915)
316 {
317 	return __gem_context_has(i915, I915_CONTEXT_CLONE_VM, 0);
318 }
319 
gem_has_queues(int i915)320 bool gem_has_queues(int i915)
321 {
322 	return __gem_context_has(i915,
323 				 I915_CONTEXT_CLONE_VM,
324 				 I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE);
325 }
326 
gem_context_clone(int i915,uint32_t src,unsigned int share,unsigned int flags)327 uint32_t gem_context_clone(int i915,
328 			   uint32_t src, unsigned int share,
329 			   unsigned int flags)
330 {
331 	uint32_t ctx;
332 
333 	igt_assert_eq(__gem_context_clone(i915, src, share, flags, &ctx), 0);
334 
335 	return ctx;
336 }
337 
gem_queue_create(int i915)338 uint32_t gem_queue_create(int i915)
339 {
340 	return gem_context_clone(i915, 0,
341 				 I915_CONTEXT_CLONE_VM,
342 				 I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE);
343 }
344 
gem_context_has_engine(int fd,uint32_t ctx,uint64_t engine)345 bool gem_context_has_engine(int fd, uint32_t ctx, uint64_t engine)
346 {
347 	struct drm_i915_gem_exec_object2 exec = {};
348 	struct drm_i915_gem_execbuffer2 execbuf = {
349 		.buffers_ptr = to_user_pointer(&exec),
350 		.buffer_count = 1,
351 		.flags = engine,
352 		.rsvd1 = ctx,
353 	};
354 
355 	/*
356 	 * 'engine' value can either store an execbuf engine selector
357 	 * or a context map index; for the latter case we do not expect
358 	 * to have any value at bit 13 and 14 (BSD1/2 selector),
359 	 * therefore, we assume that the following check is safe and it
360 	 * wouldn't produce any result.
361 	 */
362 	if ((engine & ~(3<<13)) == I915_EXEC_BSD) {
363 		if (engine & (2 << 13) && !gem_has_bsd2(fd))
364 			return false;
365 	}
366 
367 	return __gem_execbuf(fd, &execbuf) == -ENOENT;
368 }
369