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.
34 #include <tegra_drm.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
;
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
);
54 static int drm_tegra_wrap(struct drm_tegra
**drmp
, int fd
, bool close
)
56 struct drm_tegra
*drm
;
61 drm
= calloc(1, sizeof(*drm
));
73 drm_public
int drm_tegra_new(struct drm_tegra
**drmp
, int fd
)
75 bool supported
= false;
76 drmVersionPtr version
;
78 version
= drmGetVersion(fd
);
82 if (!strncmp(version
->name
, "tegra", version
->name_len
))
85 drmFreeVersion(version
);
90 return drm_tegra_wrap(drmp
, fd
, false);
93 drm_public
void drm_tegra_close(struct drm_tegra
*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
;
111 if (!drm
|| size
== 0 || !bop
)
114 bo
= calloc(1, sizeof(*bo
));
118 atomic_set(&bo
->ref
, 1);
123 memset(&args
, 0, sizeof(args
));
127 err
= drmCommandWriteRead(drm
->fd
, DRM_TEGRA_GEM_CREATE
, &args
,
135 bo
->handle
= args
.handle
;
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
;
150 bo
= calloc(1, sizeof(*bo
));
154 atomic_set(&bo
->ref
, 1);
165 drm_public
struct drm_tegra_bo
*drm_tegra_bo_ref(struct drm_tegra_bo
*bo
)
168 atomic_inc(&bo
->ref
);
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
)
184 *handle
= bo
->handle
;
189 drm_public
int drm_tegra_bo_map(struct drm_tegra_bo
*bo
, void **ptr
)
191 struct drm_tegra
*drm
= bo
->drm
;
194 struct drm_tegra_gem_mmap args
;
197 memset(&args
, 0, sizeof(args
));
198 args
.handle
= bo
->handle
;
200 err
= drmCommandWriteRead(drm
->fd
, DRM_TEGRA_GEM_MMAP
, &args
,
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
) {
221 drm_public
int drm_tegra_bo_unmap(struct drm_tegra_bo
*bo
)
229 if (munmap(bo
->map
, bo
->size
))
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
;
246 memset(&args
, 0, sizeof(args
));
247 args
.handle
= bo
->handle
;
249 err
= drmCommandWriteRead(drm
->fd
, DRM_TEGRA_GEM_GET_FLAGS
, &args
,
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
;
269 memset(&args
, 0, sizeof(args
));
270 args
.handle
= bo
->handle
;
273 err
= drmCommandWriteRead(drm
->fd
, DRM_TEGRA_GEM_SET_FLAGS
, &args
,
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
;
291 memset(&args
, 0, sizeof(args
));
292 args
.handle
= bo
->handle
;
294 err
= drmCommandWriteRead(drm
->fd
, DRM_TEGRA_GEM_GET_TILING
, &args
,
300 tiling
->mode
= args
.mode
;
301 tiling
->value
= args
.value
;
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
;
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
,