blob: 7f933b23bd4f866042c470001094299bc2fe544d [file] [log] [blame]
Chris Wilsonc3440442016-06-18 00:42:19 +01001/*
2 * Copyright © 2016 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
Chris Wilsonc3440442016-06-18 00:42:19 +010024#include <sys/mman.h>
Daniel Vetter8c1fcc62017-09-08 11:23:15 +020025#include <xf86drm.h>
26#include <xf86drmMode.h>
27#include <errno.h>
28
29#include "igt_vgem.h"
30#include "igt_core.h"
31#include "ioctl_wrappers.h"
Chris Wilsonc3440442016-06-18 00:42:19 +010032
Daniel Vetter93153992016-07-27 14:34:59 +020033/**
34 * SECTION:igt_vgem
35 * @short_description: VGEM support library
36 * @title: VGEM
37 * @include: igt.h
38 *
39 * This library provides various auxiliary helper functions for writing VGEM
40 * tests. VGEM is especially useful as a virtual dma-buf import and exporter and
41 * for testing cross driver synchronization (either using epxlicit dma-fences or
42 * using implicit fences attached to dma-bufs).
43 */
44
Chris Wilsonc3440442016-06-18 00:42:19 +010045int __vgem_create(int fd, struct vgem_bo *bo)
46{
47 struct drm_mode_create_dumb arg;
48
49 memset(&arg, 0, sizeof(arg));
50 arg.width = bo->width;
51 arg.height = bo->height;
52 arg.bpp = bo->bpp;
53
54 if (drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg))
55 return -errno;
56
57 bo->handle = arg.handle;
58 bo->pitch = arg.pitch;
59 bo->size = arg.size;
60
61 return 0;
62}
63
64void vgem_create(int fd, struct vgem_bo *bo)
65{
66 igt_assert_eq(__vgem_create(fd, bo), 0);
67}
68
69void *__vgem_mmap(int fd, struct vgem_bo *bo, unsigned prot)
70{
71 struct drm_mode_map_dumb arg;
72 void *ptr;
73
74 memset(&arg, 0, sizeof(arg));
75 arg.handle = bo->handle;
76 if (drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &arg))
77 return NULL;
78
79 ptr = mmap64(0, bo->size, prot, MAP_SHARED, fd, arg.offset);
80 if (ptr == MAP_FAILED)
81 return NULL;
82
83 return ptr;
84}
85
86void *vgem_mmap(int fd, struct vgem_bo *bo, unsigned prot)
87{
88 void *ptr;
89
90 igt_assert_f((ptr = __vgem_mmap(fd, bo, prot)),
91 "vgem_map(fd=%d, bo->handle=%d, prot=%x)\n",
92 fd, bo->handle, prot);
93
94 return ptr;
95}
96
Chris Wilson93256e32016-06-22 07:21:09 +010097#define LOCAL_VGEM_FENCE_ATTACH 0x1
98#define LOCAL_VGEM_FENCE_SIGNAL 0x2
99
100#define LOCAL_IOCTL_VGEM_FENCE_ATTACH DRM_IOWR( DRM_COMMAND_BASE + LOCAL_VGEM_FENCE_ATTACH, struct local_vgem_fence_attach)
101#define LOCAL_IOCTL_VGEM_FENCE_SIGNAL DRM_IOW( DRM_COMMAND_BASE + LOCAL_VGEM_FENCE_SIGNAL, struct local_vgem_fence_signal)
102
103struct local_vgem_fence_attach {
104 uint32_t handle;
105 uint32_t flags;
Chris Wilson93256e32016-06-22 07:21:09 +0100106 uint32_t out_fence;
107 uint32_t pad;
108};
109
110struct local_vgem_fence_signal {
111 uint32_t fence;
112 uint32_t flags;
113};
114
115bool vgem_has_fences(int fd)
116{
117 struct local_vgem_fence_signal arg;
118 int err;
119
120 err = 0;
121 memset(&arg, 0, sizeof(arg));
122 if (drmIoctl(fd, LOCAL_IOCTL_VGEM_FENCE_SIGNAL, &arg))
123 err = -errno;
124 errno = 0;
125 return err == -ENOENT;
126}
127
128static int __vgem_fence_attach(int fd, struct local_vgem_fence_attach *arg)
129{
130 int err = 0;
131 if (igt_ioctl(fd, LOCAL_IOCTL_VGEM_FENCE_ATTACH, arg))
132 err = -errno;
133 errno = 0;
134 return err;
135}
136
Chris Wilsonc8ab5772016-07-15 09:01:59 +0100137bool vgem_fence_has_flag(int fd, unsigned flags)
138{
139 struct local_vgem_fence_attach arg;
140 struct vgem_bo bo;
141 bool ret = false;
142
143 memset(&bo, 0, sizeof(bo));
144 bo.width = 1;
145 bo.height = 1;
146 bo.bpp = 32;
147 vgem_create(fd, &bo);
148
149 memset(&arg, 0, sizeof(arg));
150 arg.handle = bo.handle;
151 arg.flags = flags;
152 if (__vgem_fence_attach(fd, &arg) == 0) {
153 vgem_fence_signal(fd, arg.out_fence);
154 ret = true;
155 }
156 gem_close(fd, bo.handle);
157
158 return ret;
159}
160
161uint32_t vgem_fence_attach(int fd, struct vgem_bo *bo, unsigned flags)
Chris Wilson93256e32016-06-22 07:21:09 +0100162{
163 struct local_vgem_fence_attach arg;
164
165 memset(&arg, 0, sizeof(arg));
166 arg.handle = bo->handle;
Chris Wilsonc8ab5772016-07-15 09:01:59 +0100167 arg.flags = flags;
Chris Wilson93256e32016-06-22 07:21:09 +0100168 igt_assert_eq(__vgem_fence_attach(fd, &arg), 0);
169 return arg.out_fence;
170}
171
Chris Wilson3d8f55c2016-07-18 10:04:51 +0100172static int ioctl_vgem_fence_signal(int fd, struct local_vgem_fence_signal *arg)
Chris Wilson93256e32016-06-22 07:21:09 +0100173{
174 int err = 0;
175 if (igt_ioctl(fd, LOCAL_IOCTL_VGEM_FENCE_SIGNAL, arg))
176 err = -errno;
177 errno = 0;
178 return err;
179}
180
Chris Wilson3d8f55c2016-07-18 10:04:51 +0100181int __vgem_fence_signal(int fd, uint32_t fence)
Chris Wilson93256e32016-06-22 07:21:09 +0100182{
183 struct local_vgem_fence_signal arg;
184
185 memset(&arg, 0, sizeof(arg));
186 arg.fence = fence;
Chris Wilson3d8f55c2016-07-18 10:04:51 +0100187
188 return ioctl_vgem_fence_signal(fd, &arg);
189}
190
191void vgem_fence_signal(int fd, uint32_t fence)
192{
193 igt_assert_eq(__vgem_fence_signal(fd, fence), 0);
Chris Wilson93256e32016-06-22 07:21:09 +0100194}