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.
38 #include <tegra_drm.h>
42 static void drm_tegra_bo_free(struct drm_tegra_bo *bo)
44 struct drm_tegra *drm = bo->drm;
45 struct drm_gem_close args;
48 munmap(bo->map, bo->size);
50 memset(&args, 0, sizeof(args));
51 args.handle = bo->handle;
53 drmIoctl(drm->fd, DRM_IOCTL_GEM_CLOSE, &args);
58 static int drm_tegra_wrap(struct drm_tegra **drmp, int fd, bool close)
60 struct drm_tegra *drm;
65 drm = calloc(1, sizeof(*drm));
78 int drm_tegra_new(struct drm_tegra **drmp, int fd)
80 bool supported = false;
81 drmVersionPtr version;
83 version = drmGetVersion(fd);
87 if (!strncmp(version->name, "tegra", version->name_len))
90 drmFreeVersion(version);
95 return drm_tegra_wrap(drmp, fd, false);
99 void drm_tegra_close(struct drm_tegra *drm)
111 int drm_tegra_bo_new(struct drm_tegra_bo **bop, struct drm_tegra *drm,
112 uint32_t flags, uint32_t size)
114 struct drm_tegra_gem_create args;
115 struct drm_tegra_bo *bo;
118 if (!drm || size == 0 || !bop)
121 bo = calloc(1, sizeof(*bo));
125 atomic_set(&bo->ref, 1);
130 memset(&args, 0, sizeof(args));
134 err = drmCommandWriteRead(drm->fd, DRM_TEGRA_GEM_CREATE, &args,
142 bo->handle = args.handle;
150 int drm_tegra_bo_wrap(struct drm_tegra_bo **bop, struct drm_tegra *drm,
151 uint32_t handle, uint32_t flags, uint32_t size)
153 struct drm_tegra_bo *bo;
158 bo = calloc(1, sizeof(*bo));
162 atomic_set(&bo->ref, 1);
174 struct drm_tegra_bo *drm_tegra_bo_ref(struct drm_tegra_bo *bo)
177 atomic_inc(&bo->ref);
183 void drm_tegra_bo_unref(struct drm_tegra_bo *bo)
185 if (bo && atomic_dec_and_test(&bo->ref))
186 drm_tegra_bo_free(bo);
190 int drm_tegra_bo_get_handle(struct drm_tegra_bo *bo, uint32_t *handle)
195 *handle = bo->handle;
201 int drm_tegra_bo_map(struct drm_tegra_bo *bo, void **ptr)
203 struct drm_tegra *drm = bo->drm;
206 struct drm_tegra_gem_mmap args;
209 memset(&args, 0, sizeof(args));
210 args.handle = bo->handle;
212 err = drmCommandWriteRead(drm->fd, DRM_TEGRA_GEM_MMAP, &args,
217 bo->offset = args.offset;
219 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
220 drm->fd, bo->offset);
221 if (bo->map == MAP_FAILED) {
234 int drm_tegra_bo_unmap(struct drm_tegra_bo *bo)
242 if (munmap(bo->map, bo->size))