1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2011 Texas Instruments, Inc
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 (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <rob@ti.com>
34 #include <linux/stddef.h>
43 #include "omap_drmif.h"
45 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
46 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
47 #define PAGE_SIZE 4096
53 /* a GEM buffer object allocated from the DRM device */
55 struct omap_device *dev;
56 void *map; /* userspace mmap'ing (if there is one) */
59 uint32_t name; /* flink global handle (DRI2 name) */
60 uint64_t offset; /* offset to mmap() */
61 int fd; /* dmabuf handle */
64 struct omap_device * omap_device_new(int fd)
66 struct omap_device *dev = calloc(sizeof(*dev), 1);
73 void omap_device_del(struct omap_device *dev)
78 int omap_get_param(struct omap_device *dev, uint64_t param, uint64_t *value)
80 struct drm_omap_param req = {
85 ret = drmCommandWriteRead(dev->fd, DRM_OMAP_GET_PARAM, &req, sizeof(req));
95 int omap_set_param(struct omap_device *dev, uint64_t param, uint64_t value)
97 struct drm_omap_param req = {
101 return drmCommandWrite(dev->fd, DRM_OMAP_SET_PARAM, &req, sizeof(req));
104 /* allocate a new buffer object */
105 static struct omap_bo * omap_bo_new_impl(struct omap_device *dev,
106 union omap_gem_size size, uint32_t flags)
108 struct omap_bo *bo = NULL;
109 struct drm_omap_gem_new req = {
114 if (size.bytes == 0) {
118 bo = calloc(sizeof(*bo), 1);
125 if (flags & OMAP_BO_TILED) {
126 bo->size = round_up(size.tiled.width, PAGE_SIZE) * size.tiled.height;
128 bo->size = size.bytes;
131 if (drmCommandWriteRead(dev->fd, DRM_OMAP_GEM_NEW, &req, sizeof(req))) {
135 bo->handle = req.handle;
145 /* allocate a new (un-tiled) buffer object */
146 struct omap_bo * omap_bo_new(struct omap_device *dev,
147 uint32_t size, uint32_t flags)
149 union omap_gem_size gsize = {
152 if (flags & OMAP_BO_TILED) {
155 return omap_bo_new_impl(dev, gsize, flags);
158 /* allocate a new buffer object */
159 struct omap_bo * omap_bo_new_tiled(struct omap_device *dev,
160 uint32_t width, uint32_t height, uint32_t flags)
162 union omap_gem_size gsize = {
168 if (!(flags & OMAP_BO_TILED)) {
171 return omap_bo_new_impl(dev, gsize, flags);
174 /* get buffer info */
175 static int get_buffer_info(struct omap_bo *bo)
177 struct drm_omap_gem_info req = {
178 .handle = bo->handle,
180 int ret = drmCommandWriteRead(bo->dev->fd, DRM_OMAP_GEM_INFO,
186 /* really all we need for now is mmap offset */
187 bo->offset = req.offset;
193 /* import a buffer object from DRI2 name */
194 struct omap_bo * omap_bo_from_name(struct omap_device *dev, uint32_t name)
197 struct drm_gem_open req = {
201 bo = calloc(sizeof(*bo), 1);
206 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
212 bo->handle = req.handle;
221 /* destroy a buffer object */
222 void omap_bo_del(struct omap_bo *bo)
229 munmap(bo->map, bo->size);
237 struct drm_gem_close req = {
238 .handle = bo->handle,
241 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
247 /* get the global flink/DRI2 buffer name */
248 int omap_bo_get_name(struct omap_bo *bo, uint32_t *name)
251 struct drm_gem_flink req = {
252 .handle = bo->handle,
256 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
269 uint32_t omap_bo_handle(struct omap_bo *bo)
274 /* caller owns the dmabuf fd that is returned and is responsible
275 * to close() it when done
277 int omap_bo_dmabuf(struct omap_bo *bo)
280 struct drm_prime_handle req = {
281 .handle = bo->handle,
282 .flags = DRM_CLOEXEC,
286 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &req);
296 uint32_t omap_bo_size(struct omap_bo *bo)
304 void * omap_bo_map(struct omap_bo *bo)
311 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
312 MAP_SHARED, bo->dev->fd, bo->offset);
313 if (bo->map == MAP_FAILED) {
320 int omap_bo_cpu_prep(struct omap_bo *bo, enum omap_gem_op op)
322 struct drm_omap_gem_cpu_prep req = {
323 .handle = bo->handle,
326 return drmCommandWrite(bo->dev->fd,
327 DRM_OMAP_GEM_CPU_PREP, &req, sizeof(req));
330 int omap_bo_cpu_fini(struct omap_bo *bo, enum omap_gem_op op)
332 struct drm_omap_gem_cpu_fini req = {
333 .handle = bo->handle,
337 return drmCommandWrite(bo->dev->fd,
338 DRM_OMAP_GEM_CPU_FINI, &req, sizeof(req));