lib/igt_kms: Clean up the other _name functions/macros
[platform/upstream/intel-gpu-tools.git] / lib / ioctl_wrappers.c
1 /*
2  * Copyright © 2007, 2011, 2013, 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 #ifndef ANDROID
30 #define _GNU_SOURCE
31 #else
32 #include <libgen.h>
33 #endif
34 #include <stdio.h>
35 #include <fcntl.h>
36 #include <sys/stat.h>
37 #include <sys/ioctl.h>
38 #include <string.h>
39 #include <sys/mman.h>
40 #include <signal.h>
41 #include <pciaccess.h>
42 #include <getopt.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <sys/wait.h>
46 #include <sys/types.h>
47 #include <sys/syscall.h>
48 #include <sys/utsname.h>
49 #include <termios.h>
50 #include <errno.h>
51
52 #include "drmtest.h"
53 #include "i915_drm.h"
54 #include "intel_chipset.h"
55 #include "intel_io.h"
56 #include "igt_debugfs.h"
57 #include "config.h"
58
59 #include "ioctl_wrappers.h"
60
61 /**
62  * SECTION:ioctl_wrappers
63  * @short_description: ioctl wrappers and related functions
64  * @title: ioctl wrappers
65  * @include: ioctl_wrappers.h
66  *
67  * This helper library contains simple functions to wrap the raw drm/i915 kernel
68  * ioctls. The normal versions never pass any error codes to the caller and use
69  * igt_assert() to check for error conditions instead. For some ioctls raw
70  * wrappers which do pass on error codes are available. These raw wrappers have
71  * a __ prefix.
72  *
73  * For wrappers which check for feature bits there can also be two versions: The
74  * normal one simply returns a boolean to the caller. But when skipping the
75  * testcase entirely is the right action then it's better to use igt_skip()
76  * directly in the wrapper. Such functions have _require_ in their name to
77  * distinguish them.
78  */
79
80 /**
81  * gem_handle_to_libdrm_bo:
82  * @bufmgr: libdrm buffer manager instance
83  * @fd: open i915 drm file descriptor
84  * @name: buffer name in libdrm
85  * @handle: gem buffer object handle
86  *
87  * This helper function imports a raw gem buffer handle into the libdrm buffer
88  * manager.
89  *
90  * Returns: The imported libdrm buffer manager object.
91  */
92 drm_intel_bo *
93 gem_handle_to_libdrm_bo(drm_intel_bufmgr *bufmgr, int fd, const char *name, uint32_t handle)
94 {
95         struct drm_gem_flink flink;
96         int ret;
97         drm_intel_bo *bo;
98
99         memset(&flink, 0, sizeof(handle));
100         flink.handle = handle;
101         ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
102         igt_assert(ret == 0);
103         errno = 0;
104
105         bo = drm_intel_bo_gem_create_from_name(bufmgr, name, flink.name);
106         igt_assert(bo);
107
108         return bo;
109 }
110
111 /**
112  * gem_get_tiling:
113  * @fd: open i915 drm file descriptor
114  * @handle: gem buffer object handle
115  * @tiling: (out) tiling mode of the gem buffer
116  * @swizzle: (out) bit 6 swizzle mode
117  *
118  * This wraps the GET_TILING ioctl.
119  */
120 void
121 gem_get_tiling(int fd, uint32_t handle, uint32_t *tiling, uint32_t *swizzle)
122 {
123         struct drm_i915_gem_get_tiling get_tiling;
124         int ret;
125
126         memset(&get_tiling, 0, sizeof(get_tiling));
127         get_tiling.handle = handle;
128
129         ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling);
130         igt_assert(ret == 0);
131
132         *tiling = get_tiling.tiling_mode;
133         *swizzle = get_tiling.swizzle_mode;
134 }
135
136 int __gem_set_tiling(int fd, uint32_t handle, uint32_t tiling, uint32_t stride)
137 {
138         struct drm_i915_gem_set_tiling st;
139         int ret;
140
141         memset(&st, 0, sizeof(st));
142         do {
143                 st.handle = handle;
144                 st.tiling_mode = tiling;
145                 st.stride = tiling ? stride : 0;
146
147                 ret = ioctl(fd, DRM_IOCTL_I915_GEM_SET_TILING, &st);
148         } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
149         if (ret != 0)
150                 return -errno;
151
152         errno = 0;
153         igt_assert(st.tiling_mode == tiling);
154         return 0;
155 }
156
157 /**
158  * gem_set_tiling:
159  * @fd: open i915 drm file descriptor
160  * @handle: gem buffer object handle
161  * @tiling: tiling mode bits
162  * @stride: stride of the buffer when using a tiled mode, otherwise must be 0
163  *
164  * This wraps the SET_TILING ioctl.
165  */
166 void gem_set_tiling(int fd, uint32_t handle, uint32_t tiling, uint32_t stride)
167 {
168         igt_assert(__gem_set_tiling(fd, handle, tiling, stride) == 0);
169 }
170
171 struct local_drm_i915_gem_caching {
172         uint32_t handle;
173         uint32_t caching;
174 };
175
176 #define LOCAL_DRM_I915_GEM_SET_CACHEING    0x2f
177 #define LOCAL_DRM_I915_GEM_GET_CACHEING    0x30
178 #define LOCAL_DRM_IOCTL_I915_GEM_SET_CACHEING \
179         DRM_IOW(DRM_COMMAND_BASE + LOCAL_DRM_I915_GEM_SET_CACHEING, struct local_drm_i915_gem_caching)
180 #define LOCAL_DRM_IOCTL_I915_GEM_GET_CACHEING \
181         DRM_IOWR(DRM_COMMAND_BASE + LOCAL_DRM_I915_GEM_GET_CACHEING, struct local_drm_i915_gem_caching)
182
183 /**
184  * gem_set_caching:
185  * @fd: open i915 drm file descriptor
186  * @handle: gem buffer object handle
187  * @caching: caching mode bits
188  *
189  * This wraps the SET_CACHING ioctl. Note that this function internally calls
190  * igt_require() when SET_CACHING isn't available, hence automatically skips the
191  * test. Therefore always extract test logic which uses this into its own
192  * subtest.
193  */
194 void gem_set_caching(int fd, uint32_t handle, uint32_t caching)
195 {
196         struct local_drm_i915_gem_caching arg;
197         int ret;
198
199         memset(&arg, 0, sizeof(arg));
200         arg.handle = handle;
201         arg.caching = caching;
202         ret = ioctl(fd, LOCAL_DRM_IOCTL_I915_GEM_SET_CACHEING, &arg);
203
204         igt_assert(ret == 0 || (errno == ENOTTY || errno == EINVAL));
205         igt_require(ret == 0);
206         errno = 0;
207 }
208
209 /**
210  * gem_get_caching:
211  * @fd: open i915 drm file descriptor
212  * @handle: gem buffer object handle
213  *
214  * This wraps the GET_CACHING ioctl.
215  *
216  * Returns: The current caching mode bits.
217  */
218 uint32_t gem_get_caching(int fd, uint32_t handle)
219 {
220         struct local_drm_i915_gem_caching arg;
221         int ret;
222
223         arg.handle = handle;
224         arg.caching = 0;
225         ret = ioctl(fd, LOCAL_DRM_IOCTL_I915_GEM_GET_CACHEING, &arg);
226         igt_assert(ret == 0);
227         errno = 0;
228
229         return arg.caching;
230 }
231
232 /**
233  * gem_open:
234  * @fd: open i915 drm file descriptor
235  * @name: flink buffer name
236  *
237  * This wraps the GEM_OPEN ioctl, which is used to import an flink name.
238  *
239  * Returns: gem file-private buffer handle of the open object.
240  */
241 uint32_t gem_open(int fd, uint32_t name)
242 {
243         struct drm_gem_open open_struct;
244         int ret;
245
246         memset(&open_struct, 0, sizeof(open_struct));
247         open_struct.name = name;
248         ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open_struct);
249         igt_assert(ret == 0);
250         igt_assert(open_struct.handle != 0);
251         errno = 0;
252
253         return open_struct.handle;
254 }
255
256 /**
257  * gem_flink:
258  * @fd: open i915 drm file descriptor
259  * @handle: file-private gem buffer object handle
260  *
261  * This wraps the GEM_FLINK ioctl, which is used to export a gem buffer object
262  * into the device-global flink namespace. See gem_open() for opening such a
263  * buffer name on a different i915 drm file descriptor.
264  *
265  * Returns: The created flink buffer name.
266  */
267 uint32_t gem_flink(int fd, uint32_t handle)
268 {
269         struct drm_gem_flink flink;
270         int ret;
271
272         memset(&flink, 0, sizeof(flink));
273         flink.handle = handle;
274         ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
275         igt_assert(ret == 0);
276         errno = 0;
277
278         return flink.name;
279 }
280
281 /**
282  * gem_close:
283  * @fd: open i915 drm file descriptor
284  * @handle: gem buffer object handle
285  *
286  * This wraps the GEM_CLOSE ioctl, which to release a file-private gem buffer
287  * handle.
288  */
289 void gem_close(int fd, uint32_t handle)
290 {
291         struct drm_gem_close close_bo;
292
293         memset(&close_bo, 0, sizeof(close_bo));
294         close_bo.handle = handle;
295         do_ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
296 }
297
298 /**
299  * gem_write:
300  * @fd: open i915 drm file descriptor
301  * @handle: gem buffer object handle
302  * @offset: offset within the buffer of the subrange
303  * @buf: pointer to the data to write into the buffer
304  * @length: size of the subrange
305  *
306  * This wraps the PWRITE ioctl, which is to upload a linear data to a subrange
307  * of a gem buffer object.
308  */
309 void gem_write(int fd, uint32_t handle, uint32_t offset, const void *buf, uint32_t length)
310 {
311         struct drm_i915_gem_pwrite gem_pwrite;
312
313         memset(&gem_pwrite, 0, sizeof(gem_pwrite));
314         gem_pwrite.handle = handle;
315         gem_pwrite.offset = offset;
316         gem_pwrite.size = length;
317         gem_pwrite.data_ptr = (uintptr_t)buf;
318         do_ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite);
319 }
320
321 /**
322  * gem_read:
323  * @fd: open i915 drm file descriptor
324  * @handle: gem buffer object handle
325  * @offset: offset within the buffer of the subrange
326  * @buf: pointer to the data to read into
327  * @length: size of the subrange
328  *
329  * This wraps the PREAD ioctl, which is to download a linear data to a subrange
330  * of a gem buffer object.
331  */
332 void gem_read(int fd, uint32_t handle, uint32_t offset, void *buf, uint32_t length)
333 {
334         struct drm_i915_gem_pread gem_pread;
335
336         memset(&gem_pread, 0, sizeof(gem_pread));
337         gem_pread.handle = handle;
338         gem_pread.offset = offset;
339         gem_pread.size = length;
340         gem_pread.data_ptr = (uintptr_t)buf;
341         do_ioctl(fd, DRM_IOCTL_I915_GEM_PREAD, &gem_pread);
342 }
343
344 /**
345  * gem_set_domain:
346  * @fd: open i915 drm file descriptor
347  * @handle: gem buffer object handle
348  * @read_domains: gem domain bits for read access
349  * @write_domain: gem domain bit for write access
350  *
351  * This wraps the SET_DOMAIN ioctl, which is used to control the coherency of
352  * the gem buffer object between the cpu and gtt mappings. It is also use to
353  * synchronize with outstanding rendering in general, but for that use-case
354  * please have a look at gem_sync().
355  */
356 void gem_set_domain(int fd, uint32_t handle,
357                     uint32_t read_domains, uint32_t write_domain)
358 {
359         struct drm_i915_gem_set_domain set_domain;
360
361         memset(&set_domain, 0, sizeof(set_domain));
362         set_domain.handle = handle;
363         set_domain.read_domains = read_domains;
364         set_domain.write_domain = write_domain;
365
366         do_ioctl(fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
367 }
368
369 /**
370  * gem_sync:
371  * @fd: open i915 drm file descriptor
372  * @handle: gem buffer object handle
373  *
374  * This is a wrapper around gem_set_domain() which simply blocks for any
375  * outstanding rendering to complete.
376  */
377 void gem_sync(int fd, uint32_t handle)
378 {
379         gem_set_domain(fd, handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
380 }
381
382 uint32_t __gem_create(int fd, int size)
383 {
384         struct drm_i915_gem_create create;
385         int ret;
386
387         memset(&create, 0, sizeof(create));
388         create.handle = 0;
389         create.size = size;
390         ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
391
392         if (ret < 0)
393                 return 0;
394
395         errno = 0;
396         return create.handle;
397 }
398
399 /**
400  * gem_create:
401  * @fd: open i915 drm file descriptor
402  * @size: desired size of the buffer
403  *
404  * This wraps the GEM_CREATE ioctl, which allocates a new gem buffer object of
405  * @size.
406  *
407  * Returns: The file-private handle of the created buffer object
408  */
409 uint32_t gem_create(int fd, int size)
410 {
411         struct drm_i915_gem_create create;
412
413         memset(&create, 0, sizeof(create));
414         create.handle = 0;
415         create.size = size;
416         do_ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
417         igt_assert(create.handle);
418
419         return create.handle;
420 }
421
422 /**
423  * gem_execbuf:
424  * @fd: open i915 drm file descriptor
425  * @execbuf: execbuffer data structure
426  *
427  * This wraps the EXECBUFFER2 ioctl, which submits a batchbuffer for the gpu to
428  * run.
429  */
430 void gem_execbuf(int fd, struct drm_i915_gem_execbuffer2 *execbuf)
431 {
432         int ret;
433
434         ret = drmIoctl(fd,
435                        DRM_IOCTL_I915_GEM_EXECBUFFER2,
436                        execbuf);
437         igt_assert(ret == 0);
438         errno = 0;
439 }
440
441 /**
442  * gem_mmap__gtt:
443  * @fd: open i915 drm file descriptor
444  * @handle: gem buffer object handle
445  * @size: size of the gem buffer
446  * @prot: memory protection bits as used by mmap()
447  *
448  * This functions wraps up procedure to establish a memory mapping through the
449  * GTT.
450  *
451  * Returns: A pointer to the created memory mapping.
452  */
453 void *gem_mmap__gtt(int fd, uint32_t handle, int size, int prot)
454 {
455         struct drm_i915_gem_mmap_gtt mmap_arg;
456         void *ptr;
457
458         memset(&mmap_arg, 0, sizeof(mmap_arg));
459         mmap_arg.handle = handle;
460         if (drmIoctl(fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg))
461                 return NULL;
462
463         ptr = mmap64(0, size, prot, MAP_SHARED, fd, mmap_arg.offset);
464         if (ptr == MAP_FAILED)
465                 ptr = NULL;
466
467         return ptr;
468 }
469
470 /**
471  * gem_mmap__cpu:
472  * @fd: open i915 drm file descriptor
473  * @handle: gem buffer object handle
474  * @size: size of the gem buffer
475  * @prot: memory protection bits as used by mmap()
476  *
477  * This functions wraps up procedure to establish a memory mapping through
478  * direct cpu access, bypassing the gpu completely.
479  *
480  * Returns: A pointer to the created memory mapping.
481  */
482 void *gem_mmap__cpu(int fd, uint32_t handle, int size, int prot)
483 {
484         struct drm_i915_gem_mmap mmap_arg;
485
486         memset(&mmap_arg, 0, sizeof(mmap_arg));
487         mmap_arg.handle = handle;
488         mmap_arg.offset = 0;
489         mmap_arg.size = size;
490         if (drmIoctl(fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg))
491                 return NULL;
492
493         errno = 0;
494         return (void *)(uintptr_t)mmap_arg.addr_ptr;
495 }
496
497 /**
498  * gem_madvise:
499  * @fd: open i915 drm file descriptor
500  * @handle: gem buffer object handle
501  * @state: desired madvise state
502  *
503  * This is a wraps the MADVISE ioctl, which is used in libdrm to implement
504  * opportunistic buffer object caching. Objects in the cache are set to DONTNEED
505  * (internally in the kernel tracked as purgeable objects). When such a cached
506  * object is in need again it must be set back to WILLNEED before first use.
507  *
508  * Returns: When setting the madvise state to WILLNEED this returns whether the
509  * backing storage was still avialable or not.
510  */
511 int gem_madvise(int fd, uint32_t handle, int state)
512 {
513         struct drm_i915_gem_madvise madv;
514
515         memset(&madv, 0, sizeof(madv));
516         madv.handle = handle;
517         madv.madv = state;
518         madv.retained = 1;
519         do_ioctl(fd, DRM_IOCTL_I915_GEM_MADVISE, &madv);
520
521         return madv.retained;
522 }
523
524 /**
525  * gem_context_create:
526  * @fd: open i915 drm file descriptor
527  *
528  * This is a wraps the CONTEXT_CREATE ioctl, which is used to allocate a new
529  * hardware context. Not that similarly to gem_set_caching() this wrapper calls
530  * igt_require() internally to correctly skip on kernels and platforms where hw
531  * context support is not available.
532  *
533  * Returns: The id of the allocated hw context.
534  */
535 uint32_t gem_context_create(int fd)
536 {
537         struct drm_i915_gem_context_create create;
538         int ret;
539
540         memset(&create, 0, sizeof(create));
541         ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create);
542         igt_require(ret == 0 || (errno != ENODEV && errno != EINVAL));
543         igt_assert(ret == 0);
544         errno = 0;
545
546         return create.ctx_id;
547 }
548
549 /**
550  * gem_sw_finish:
551  * @fd: open i915 drm file descriptor
552  * @handle: gem buffer object handle
553  *
554  * This is a wraps the SW_FINISH ioctl, which is used to flush out frontbuffer
555  * rendering done through the direct cpu memory mappings. Shipping userspace
556  * does _not_ call this after frontbuffer rendering through gtt memory mappings.
557  */
558 void gem_sw_finish(int fd, uint32_t handle)
559 {
560         struct drm_i915_gem_sw_finish finish;
561
562         memset(&finish, 0, sizeof(finish));
563         finish.handle = handle;
564
565         do_ioctl(fd, DRM_IOCTL_I915_GEM_SW_FINISH, &finish);
566 }
567
568 /**
569  * gem_bo_busy:
570  * @fd: open i915 drm file descriptor
571  * @handle: gem buffer object handle
572  *
573  * This is a wraps the BUSY ioctl, which tells whether a buffer object is still
574  * actively used by the gpu in a execbuffer.
575  *
576  * Returns: The busy state of the buffer object.
577  */
578 bool gem_bo_busy(int fd, uint32_t handle)
579 {
580         struct drm_i915_gem_busy busy;
581
582         memset(&busy, 0, sizeof(busy));
583         busy.handle = handle;
584
585         do_ioctl(fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
586
587         return !!busy.busy;
588 }
589
590
591 /* feature test helpers */
592
593 /**
594  * gem_uses_aliasing_ppgtt:
595  * @fd: open i915 drm file descriptor
596  *
597  * Feature test macro to check whether the kernel internally uses ppgtt to
598  * execute batches. The /aliasing/ in the function name is a bit a misnomer,
599  * this driver parameter is also true when full ppgtt address spaces are
600  * availabel since for batchbuffer construction only ppgtt or global gtt is
601  * relevant.
602  *
603  * Returns: Whether batches are run through ppgtt.
604  */
605 bool gem_uses_aliasing_ppgtt(int fd)
606 {
607         struct drm_i915_getparam gp;
608         int val = 0;
609
610         memset(&gp, 0, sizeof(gp));
611         gp.param = 18; /* HAS_ALIASING_PPGTT */
612         gp.value = &val;
613
614         if (ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp, sizeof(gp)))
615                 return 0;
616
617         errno = 0;
618         return val;
619 }
620
621 /**
622  * gem_uses_aliasing_ppgtt:
623  * @fd: open i915 drm file descriptor
624  *
625  * Feature test macro to query the kernel for the number of available fences
626  * useable in a batchbuffer. Only relevant for pre-gen4.
627  *
628  * Returns: The number of available fences.
629  */
630 int gem_available_fences(int fd)
631 {
632         struct drm_i915_getparam gp;
633         int val = 0;
634
635         memset(&gp, 0, sizeof(gp));
636         gp.param = I915_PARAM_NUM_FENCES_AVAIL;
637         gp.value = &val;
638
639         if (ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp, sizeof(gp)))
640                 return 0;
641
642         errno = 0;
643         return val;
644 }
645
646 /**
647  * gem_get_num_rings:
648  * @fd: open i915 drm file descriptor
649  *
650  * Feature test macro to query the number of avaible rings. This is useful in
651  * test loops which need to step through all rings and similar logic.
652  *
653  * For more explicit tests of ring availability see gem_has_enable_ring() and
654  * the ring specific versions like gem_has_bsd().
655  *
656  * Returns: The number of available rings.
657  */
658 int gem_get_num_rings(int fd)
659 {
660         int num_rings = 1;      /* render ring is always available */
661
662         if (gem_has_bsd(fd))
663                 num_rings++;
664         else
665                 goto skip;
666
667         if (gem_has_blt(fd))
668                 num_rings++;
669         else
670                 goto skip;
671
672         if (gem_has_vebox(fd))
673                 num_rings++;
674         else
675                 goto skip;
676
677
678 skip:
679         return num_rings;
680 }
681
682 /**
683  * gem_has_enable_ring:
684  * @fd: open i915 drm file descriptor
685  * @param: ring flag bit as used in gem_execbuf()
686  *
687  * Feature test macro to query whether a specific ring is available.
688  *
689  * Returns: Whether the ring is avaible or not.
690  */
691 bool gem_has_enable_ring(int fd,int param)
692 {
693         drm_i915_getparam_t gp;
694         int tmp = 0;
695
696         memset(&gp, 0, sizeof(gp));
697         gp.value = &tmp;
698         gp.param = param;
699
700         if (drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp))
701                 return false;
702
703         errno = 0;
704         return tmp > 0;
705 }
706
707 /**
708  * gem_has_bsd:
709  * @fd: open i915 drm file descriptor
710  *
711  * Feature test macro to query whether the BSD ring is available. This is simply
712  * a specific version of gem_has_enable_ring() for the BSD ring.
713  *
714  * Note that recent Bspec calls this the VCS ring for Video Command Submission.
715  *
716  * Returns: Whether the BSD ring is avaible or not.
717  */
718 bool gem_has_bsd(int fd)
719 {
720         return gem_has_enable_ring(fd,I915_PARAM_HAS_BSD);
721 }
722
723 /**
724  * gem_has_blt:
725  * @fd: open i915 drm file descriptor
726  *
727  * Feature test macro to query whether the blitter ring is available. This is simply
728  * a specific version of gem_has_enable_ring() for the blitter ring.
729  *
730  * Note that recent Bspec calls this the BCS ring for Blitter Command Submission.
731  *
732  * Returns: Whether the blitter ring is avaible or not.
733  */
734 bool gem_has_blt(int fd)
735 {
736         return gem_has_enable_ring(fd,I915_PARAM_HAS_BLT);
737 }
738
739 #define LOCAL_I915_PARAM_HAS_VEBOX 22
740 /**
741  * gem_has_vebox:
742  * @fd: open i915 drm file descriptor
743  *
744  * Feature test macro to query whether the vebox ring is available. This is simply
745  * a specific version of gem_has_enable_ring() for the vebox ring.
746  *
747  * Note that recent Bspec calls this the VECS ring for Video Enhancement Command
748  * Submission.
749  *
750  * Returns: Whether the vebox ring is avaible or not.
751  */
752 bool gem_has_vebox(int fd)
753 {
754         return gem_has_enable_ring(fd,LOCAL_I915_PARAM_HAS_VEBOX);
755 }
756
757 /**
758  * gem_available_aperture_size:
759  * @fd: open i915 drm file descriptor
760  *
761  * Feature test macro to query the kernel for the available gpu aperture size
762  * useable in a batchbuffer.
763  *
764  * Returns: The available gtt address space size.
765  */
766 uint64_t gem_available_aperture_size(int fd)
767 {
768         struct drm_i915_gem_get_aperture aperture;
769
770         memset(&aperture, 0, sizeof(aperture));
771         aperture.aper_size = 256*1024*1024;
772         do_ioctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
773
774         return aperture.aper_available_size;
775 }
776
777 /**
778  * gem_aperture_size:
779  * @fd: open i915 drm file descriptor
780  *
781  * Feature test macro to query the kernel for the total gpu aperture size.
782  *
783  * Returns: The total gtt address space size.
784  */
785 uint64_t gem_aperture_size(int fd)
786 {
787         struct drm_i915_gem_get_aperture aperture;
788
789         memset(&aperture, 0, sizeof(aperture));
790         aperture.aper_size = 256*1024*1024;
791         do_ioctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
792
793         return aperture.aper_size;
794 }
795
796 /**
797  * gem_aperture_size:
798  * @fd: open i915 drm file descriptor
799  *
800  * Feature test macro to query the kernel for the mappable gpu aperture size.
801  * This is the area avaialble for GTT memory mappings.
802  *
803  * Returns: The mappable gtt address space size.
804  */
805 uint64_t gem_mappable_aperture_size(void)
806 {
807         struct pci_device *pci_dev = intel_get_pci_device();
808         int bar;
809
810         if (intel_gen(pci_dev->device_id) < 3)
811                 bar = 0;
812         else
813                 bar = 2;
814
815         return pci_dev->regions[bar].size;
816 }
817
818 /**
819  * gem_require_caching:
820  * @fd: open i915 drm file descriptor
821  *
822  * Feature test macro to query whether buffer object caching control is
823  * available. Automatically skips through igt_require() if not.
824  */
825 void gem_require_caching(int fd)
826 {
827         struct local_drm_i915_gem_caching arg;
828         int ret;
829
830         memset(&arg, 0, sizeof(arg));
831         arg.handle = gem_create(fd, 4096);
832         igt_assert(arg.handle != 0);
833
834         arg.caching = 0;
835         ret = ioctl(fd, LOCAL_DRM_IOCTL_I915_GEM_SET_CACHEING, &arg);
836         gem_close(fd, arg.handle);
837
838         igt_require(ret == 0);
839         errno = 0;
840 }
841
842 /**
843  * gem_require_ring:
844  * @fd: open i915 drm file descriptor
845  * @ring_id: ring flag bit as used in gem_execbuf()
846  *
847  * Feature test macro to query whether a specific ring is available.
848  * In contrast to gem_has_enable_ring() this automagically skips if the ring
849  * isn't available by calling igt_require().
850  */
851 void gem_require_ring(int fd, int ring_id)
852 {
853         switch (ring_id) {
854         case I915_EXEC_RENDER:
855                 return;
856         case I915_EXEC_BLT:
857                 igt_require(HAS_BLT_RING(intel_get_drm_devid(fd)));
858                 return;
859         case I915_EXEC_BSD:
860                 igt_require(HAS_BSD_RING(intel_get_drm_devid(fd)));
861                 return;
862 #ifdef I915_EXEC_VEBOX
863         case I915_EXEC_VEBOX:
864                 igt_require(gem_has_vebox(fd));
865                 return;
866 #endif
867         default:
868                 igt_assert(0);
869                 return;
870         }
871 }
872
873 /* prime */
874
875 /**
876  * prime_handle_to_fd:
877  * @fd: open i915 drm file descriptor
878  * @handle: file-private gem buffer object handle
879  *
880  * This wraps the PRIME_HANDLE_TO_FD ioctl, which is used to export a gem buffer
881  * object into a global (i.e. potentially cross-device) dma-buf file-descriptor
882  * handle.
883  *
884  * Returns: The created dma-buf fd handle.
885  */
886 int prime_handle_to_fd(int fd, uint32_t handle)
887 {
888         struct drm_prime_handle args;
889
890         memset(&args, 0, sizeof(args));
891         args.handle = handle;
892         args.flags = DRM_CLOEXEC;
893         args.fd = -1;
894
895         do_ioctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
896
897         return args.fd;
898 }
899
900 /**
901  * prime_fd_to_handle:
902  * @fd: open i915 drm file descriptor
903  * @dma_buf_fd: dma-buf fd handle
904  *
905  * This wraps the PRIME_FD_TO_HANDLE ioctl, which is used to import a dma-buf
906  * file-descriptor into a gem buffer object.
907  *
908  * Returns: The created gem buffer object handle.
909  */
910 uint32_t prime_fd_to_handle(int fd, int dma_buf_fd)
911 {
912         struct drm_prime_handle args;
913
914         memset(&args, 0, sizeof(args));
915         args.fd = dma_buf_fd;
916         args.flags = 0;
917         args.handle = 0;
918
919         do_ioctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
920
921         return args.handle;
922 }
923
924 /**
925  * prime_get_size:
926  * @dma_buf_fd: dma-buf fd handle
927  *
928  * This wraps the lseek() protocol used to query the invariant size of a
929  * dma-buf.  Not all kernels support this, which is check with igt_require() and
930  * so will result in automagic test skipping.
931  *
932  * Returns: The lifetime-invariant size of the dma-buf object.
933  */
934 off_t prime_get_size(int dma_buf_fd)
935 {
936         off_t ret;
937
938         ret = lseek(dma_buf_fd, 0, SEEK_END);
939         igt_assert(ret >= 0 || errno == ESPIPE);
940         igt_require(ret >= 0);
941         errno = 0;
942
943         return ret;
944 }
945