libdrm: NOTE! Default branch is now main
[drm/libdrm.git] / tegra / tegra.c
blobcf00a3cac6d83cf28ed69b6fc3e281cdde868dca
1 /*
2 * Copyright © 2012, 2013 Thierry Reding
3 * Copyright © 2013 Erik Faye-Lund
4 * Copyright © 2014 NVIDIA Corporation
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include <sys/mman.h>
32 #include <xf86drm.h>
34 #include <tegra_drm.h>
36 #include "private.h"
38 static void drm_tegra_bo_free(struct drm_tegra_bo *bo)
40 struct drm_tegra *drm = bo->drm;
41 struct drm_gem_close args;
43 if (bo->map)
44 munmap(bo->map, bo->size);
46 memset(&args, 0, sizeof(args));
47 args.handle = bo->handle;
49 drmIoctl(drm->fd, DRM_IOCTL_GEM_CLOSE, &args);
51 free(bo);
54 static int drm_tegra_wrap(struct drm_tegra **drmp, int fd, bool close)
56 struct drm_tegra *drm;
58 if (fd < 0 || !drmp)
59 return -EINVAL;
61 drm = calloc(1, sizeof(*drm));
62 if (!drm)
63 return -ENOMEM;
65 drm->close = close;
66 drm->fd = fd;
68 *drmp = drm;
70 return 0;
73 drm_public int drm_tegra_new(struct drm_tegra **drmp, int fd)
75 bool supported = false;
76 drmVersionPtr version;
78 version = drmGetVersion(fd);
79 if (!version)
80 return -ENOMEM;
82 if (!strncmp(version->name, "tegra", version->name_len))
83 supported = true;
85 drmFreeVersion(version);
87 if (!supported)
88 return -ENOTSUP;
90 return drm_tegra_wrap(drmp, fd, false);
93 drm_public void drm_tegra_close(struct drm_tegra *drm)
95 if (!drm)
96 return;
98 if (drm->close)
99 close(drm->fd);
101 free(drm);
104 drm_public int drm_tegra_bo_new(struct drm_tegra_bo **bop, struct drm_tegra *drm,
105 uint32_t flags, uint32_t size)
107 struct drm_tegra_gem_create args;
108 struct drm_tegra_bo *bo;
109 int err;
111 if (!drm || size == 0 || !bop)
112 return -EINVAL;
114 bo = calloc(1, sizeof(*bo));
115 if (!bo)
116 return -ENOMEM;
118 atomic_set(&bo->ref, 1);
119 bo->flags = flags;
120 bo->size = size;
121 bo->drm = drm;
123 memset(&args, 0, sizeof(args));
124 args.flags = flags;
125 args.size = size;
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;
135 bo->handle = args.handle;
137 *bop = bo;
139 return 0;
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)
145 struct drm_tegra_bo *bo;
147 if (!drm || !bop)
148 return -EINVAL;
150 bo = calloc(1, sizeof(*bo));
151 if (!bo)
152 return -ENOMEM;
154 atomic_set(&bo->ref, 1);
155 bo->handle = handle;
156 bo->flags = flags;
157 bo->size = size;
158 bo->drm = drm;
160 *bop = bo;
162 return 0;
165 drm_public struct drm_tegra_bo *drm_tegra_bo_ref(struct drm_tegra_bo *bo)
167 if (bo)
168 atomic_inc(&bo->ref);
170 return bo;
173 drm_public void drm_tegra_bo_unref(struct drm_tegra_bo *bo)
175 if (bo && atomic_dec_and_test(&bo->ref))
176 drm_tegra_bo_free(bo);
179 drm_public int drm_tegra_bo_get_handle(struct drm_tegra_bo *bo, uint32_t *handle)
181 if (!bo || !handle)
182 return -EINVAL;
184 *handle = bo->handle;
186 return 0;
189 drm_public int drm_tegra_bo_map(struct drm_tegra_bo *bo, void **ptr)
191 struct drm_tegra *drm = bo->drm;
193 if (!bo->map) {
194 struct drm_tegra_gem_mmap args;
195 int err;
197 memset(&args, 0, sizeof(args));
198 args.handle = bo->handle;
200 err = drmCommandWriteRead(drm->fd, DRM_TEGRA_GEM_MMAP, &args,
201 sizeof(args));
202 if (err < 0)
203 return -errno;
205 bo->offset = args.offset;
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;
215 if (ptr)
216 *ptr = bo->map;
218 return 0;
221 drm_public int drm_tegra_bo_unmap(struct drm_tegra_bo *bo)
223 if (!bo)
224 return -EINVAL;
226 if (!bo->map)
227 return 0;
229 if (munmap(bo->map, bo->size))
230 return -errno;
232 bo->map = NULL;
234 return 0;
237 drm_public int drm_tegra_bo_get_flags(struct drm_tegra_bo *bo, uint32_t *flags)
239 struct drm_tegra_gem_get_flags args;
240 struct drm_tegra *drm = bo->drm;
241 int err;
243 if (!bo)
244 return -EINVAL;
246 memset(&args, 0, sizeof(args));
247 args.handle = bo->handle;
249 err = drmCommandWriteRead(drm->fd, DRM_TEGRA_GEM_GET_FLAGS, &args,
250 sizeof(args));
251 if (err < 0)
252 return -errno;
254 if (flags)
255 *flags = args.flags;
257 return 0;
260 drm_public int drm_tegra_bo_set_flags(struct drm_tegra_bo *bo, uint32_t flags)
262 struct drm_tegra_gem_get_flags args;
263 struct drm_tegra *drm = bo->drm;
264 int err;
266 if (!bo)
267 return -EINVAL;
269 memset(&args, 0, sizeof(args));
270 args.handle = bo->handle;
271 args.flags = flags;
273 err = drmCommandWriteRead(drm->fd, DRM_TEGRA_GEM_SET_FLAGS, &args,
274 sizeof(args));
275 if (err < 0)
276 return -errno;
278 return 0;
281 drm_public int drm_tegra_bo_get_tiling(struct drm_tegra_bo *bo,
282 struct drm_tegra_bo_tiling *tiling)
284 struct drm_tegra_gem_get_tiling args;
285 struct drm_tegra *drm = bo->drm;
286 int err;
288 if (!bo)
289 return -EINVAL;
291 memset(&args, 0, sizeof(args));
292 args.handle = bo->handle;
294 err = drmCommandWriteRead(drm->fd, DRM_TEGRA_GEM_GET_TILING, &args,
295 sizeof(args));
296 if (err < 0)
297 return -errno;
299 if (tiling) {
300 tiling->mode = args.mode;
301 tiling->value = args.value;
304 return 0;
307 drm_public int drm_tegra_bo_set_tiling(struct drm_tegra_bo *bo,
308 const struct drm_tegra_bo_tiling *tiling)
310 struct drm_tegra_gem_set_tiling args;
311 struct drm_tegra *drm = bo->drm;
312 int err;
314 if (!bo)
315 return -EINVAL;
317 memset(&args, 0, sizeof(args));
318 args.handle = bo->handle;
319 args.mode = tiling->mode;
320 args.value = tiling->value;
322 err = drmCommandWriteRead(drm->fd, DRM_TEGRA_GEM_SET_TILING, &args,
323 sizeof(args));
324 if (err < 0)
325 return -errno;
327 return 0;