1 /*
2 * Copyright © 2012, 2013 Thierry Reding
3 * Copyright © 2013 Erik Faye-Lund
4 * Copyright © 2014 NVIDIA Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include <sys/mman.h>
31
32 #include <xf86drm.h>
33
34 #include <tegra_drm.h>
35
36 #include "private.h"
37
drm_tegra_bo_free(struct drm_tegra_bo * bo)38 static void drm_tegra_bo_free(struct drm_tegra_bo *bo)
39 {
40 struct drm_tegra *drm = bo->drm;
41 struct drm_gem_close args;
42
43 if (bo->map)
44 munmap(bo->map, bo->size);
45
46 memset(&args, 0, sizeof(args));
47 args.handle = bo->handle;
48
49 drmIoctl(drm->fd, DRM_IOCTL_GEM_CLOSE, &args);
50
51 free(bo);
52 }
53
drm_tegra_wrap(struct drm_tegra ** drmp,int fd,bool close)54 static int drm_tegra_wrap(struct drm_tegra **drmp, int fd, bool close)
55 {
56 struct drm_tegra *drm;
57
58 if (fd < 0 || !drmp)
59 return -EINVAL;
60
61 drm = calloc(1, sizeof(*drm));
62 if (!drm)
63 return -ENOMEM;
64
65 drm->close = close;
66 drm->fd = fd;
67
68 *drmp = drm;
69
70 return 0;
71 }
72
drm_tegra_new(struct drm_tegra ** drmp,int fd)73 drm_public int drm_tegra_new(struct drm_tegra **drmp, int fd)
74 {
75 bool supported = false;
76 drmVersionPtr version;
77
78 version = drmGetVersion(fd);
79 if (!version)
80 return -ENOMEM;
81
82 if (!strncmp(version->name, "tegra", version->name_len))
83 supported = true;
84
85 drmFreeVersion(version);
86
87 if (!supported)
88 return -ENOTSUP;
89
90 return drm_tegra_wrap(drmp, fd, false);
91 }
92
drm_tegra_close(struct drm_tegra * drm)93 drm_public void drm_tegra_close(struct drm_tegra *drm)
94 {
95 if (!drm)
96 return;
97
98 if (drm->close)
99 close(drm->fd);
100
101 free(drm);
102 }
103
drm_tegra_bo_new(struct drm_tegra_bo ** bop,struct drm_tegra * drm,uint32_t flags,uint32_t size)104 drm_public int drm_tegra_bo_new(struct drm_tegra_bo **bop, struct drm_tegra *drm,
105 uint32_t flags, uint32_t size)
106 {
107 struct drm_tegra_gem_create args;
108 struct drm_tegra_bo *bo;
109 int err;
110
111 if (!drm || size == 0 || !bop)
112 return -EINVAL;
113
114 bo = calloc(1, sizeof(*bo));
115 if (!bo)
116 return -ENOMEM;
117
118 atomic_set(&bo->ref, 1);
119 bo->flags = flags;
120 bo->size = size;
121 bo->drm = drm;
122
123 memset(&args, 0, sizeof(args));
124 args.flags = flags;
125 args.size = size;
126
127 err = drmCommandWriteRead(drm->fd, DRM_TEGRA_GEM_CREATE, &args,
128 sizeof(args));
129 if (err < 0) {
130 err = -errno;
131 free(bo);
132 return err;
133 }
134
135 bo->handle = args.handle;
136
137 *bop = bo;
138
139 return 0;
140 }
141
drm_tegra_bo_wrap(struct drm_tegra_bo ** bop,struct drm_tegra * drm,uint32_t handle,uint32_t flags,uint32_t size)142 drm_public int drm_tegra_bo_wrap(struct drm_tegra_bo **bop, struct drm_tegra *drm,
143 uint32_t handle, uint32_t flags, uint32_t size)
144 {
145 struct drm_tegra_bo *bo;
146
147 if (!drm || !bop)
148 return -EINVAL;
149
150 bo = calloc(1, sizeof(*bo));
151 if (!bo)
152 return -ENOMEM;
153
154 atomic_set(&bo->ref, 1);
155 bo->handle = handle;
156 bo->flags = flags;
157 bo->size = size;
158 bo->drm = drm;
159
160 *bop = bo;
161
162 return 0;
163 }
164
drm_tegra_bo_ref(struct drm_tegra_bo * bo)165 drm_public struct drm_tegra_bo *drm_tegra_bo_ref(struct drm_tegra_bo *bo)
166 {
167 if (bo)
168 atomic_inc(&bo->ref);
169
170 return bo;
171 }
172
drm_tegra_bo_unref(struct drm_tegra_bo * bo)173 drm_public void drm_tegra_bo_unref(struct drm_tegra_bo *bo)
174 {
175 if (bo && atomic_dec_and_test(&bo->ref))
176 drm_tegra_bo_free(bo);
177 }
178
drm_tegra_bo_get_handle(struct drm_tegra_bo * bo,uint32_t * handle)179 drm_public int drm_tegra_bo_get_handle(struct drm_tegra_bo *bo, uint32_t *handle)
180 {
181 if (!bo || !handle)
182 return -EINVAL;
183
184 *handle = bo->handle;
185
186 return 0;
187 }
188
drm_tegra_bo_map(struct drm_tegra_bo * bo,void ** ptr)189 drm_public int drm_tegra_bo_map(struct drm_tegra_bo *bo, void **ptr)
190 {
191 struct drm_tegra *drm = bo->drm;
192
193 if (!bo->map) {
194 struct drm_tegra_gem_mmap args;
195 int err;
196
197 memset(&args, 0, sizeof(args));
198 args.handle = bo->handle;
199
200 err = drmCommandWriteRead(drm->fd, DRM_TEGRA_GEM_MMAP, &args,
201 sizeof(args));
202 if (err < 0)
203 return -errno;
204
205 bo->offset = args.offset;
206
207 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
208 drm->fd, bo->offset);
209 if (bo->map == MAP_FAILED) {
210 bo->map = NULL;
211 return -errno;
212 }
213 }
214
215 if (ptr)
216 *ptr = bo->map;
217
218 return 0;
219 }
220
drm_tegra_bo_unmap(struct drm_tegra_bo * bo)221 drm_public int drm_tegra_bo_unmap(struct drm_tegra_bo *bo)
222 {
223 if (!bo)
224 return -EINVAL;
225
226 if (!bo->map)
227 return 0;
228
229 if (munmap(bo->map, bo->size))
230 return -errno;
231
232 bo->map = NULL;
233
234 return 0;
235 }
236
drm_tegra_bo_get_flags(struct drm_tegra_bo * bo,uint32_t * flags)237 drm_public int drm_tegra_bo_get_flags(struct drm_tegra_bo *bo, uint32_t *flags)
238 {
239 struct drm_tegra_gem_get_flags args;
240 struct drm_tegra *drm = bo->drm;
241 int err;
242
243 if (!bo)
244 return -EINVAL;
245
246 memset(&args, 0, sizeof(args));
247 args.handle = bo->handle;
248
249 err = drmCommandWriteRead(drm->fd, DRM_TEGRA_GEM_GET_FLAGS, &args,
250 sizeof(args));
251 if (err < 0)
252 return -errno;
253
254 if (flags)
255 *flags = args.flags;
256
257 return 0;
258 }
259
drm_tegra_bo_set_flags(struct drm_tegra_bo * bo,uint32_t flags)260 drm_public int drm_tegra_bo_set_flags(struct drm_tegra_bo *bo, uint32_t flags)
261 {
262 struct drm_tegra_gem_get_flags args;
263 struct drm_tegra *drm = bo->drm;
264 int err;
265
266 if (!bo)
267 return -EINVAL;
268
269 memset(&args, 0, sizeof(args));
270 args.handle = bo->handle;
271 args.flags = flags;
272
273 err = drmCommandWriteRead(drm->fd, DRM_TEGRA_GEM_SET_FLAGS, &args,
274 sizeof(args));
275 if (err < 0)
276 return -errno;
277
278 return 0;
279 }
280
drm_tegra_bo_get_tiling(struct drm_tegra_bo * bo,struct drm_tegra_bo_tiling * tiling)281 drm_public int drm_tegra_bo_get_tiling(struct drm_tegra_bo *bo,
282 struct drm_tegra_bo_tiling *tiling)
283 {
284 struct drm_tegra_gem_get_tiling args;
285 struct drm_tegra *drm = bo->drm;
286 int err;
287
288 if (!bo)
289 return -EINVAL;
290
291 memset(&args, 0, sizeof(args));
292 args.handle = bo->handle;
293
294 err = drmCommandWriteRead(drm->fd, DRM_TEGRA_GEM_GET_TILING, &args,
295 sizeof(args));
296 if (err < 0)
297 return -errno;
298
299 if (tiling) {
300 tiling->mode = args.mode;
301 tiling->value = args.value;
302 }
303
304 return 0;
305 }
306
drm_tegra_bo_set_tiling(struct drm_tegra_bo * bo,const struct drm_tegra_bo_tiling * tiling)307 drm_public int drm_tegra_bo_set_tiling(struct drm_tegra_bo *bo,
308 const struct drm_tegra_bo_tiling *tiling)
309 {
310 struct drm_tegra_gem_set_tiling args;
311 struct drm_tegra *drm = bo->drm;
312 int err;
313
314 if (!bo)
315 return -EINVAL;
316
317 memset(&args, 0, sizeof(args));
318 args.handle = bo->handle;
319 args.mode = tiling->mode;
320 args.value = tiling->value;
321
322 err = drmCommandWriteRead(drm->fd, DRM_TEGRA_GEM_SET_TILING, &args,
323 sizeof(args));
324 if (err < 0)
325 return -errno;
326
327 return 0;
328 }
329