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>
42 #include "omap_drmif.h"
44 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
45 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
46 #define PAGE_SIZE 4096
52 /* a GEM buffer object allocated from the DRM device */
54 struct omap_device *dev;
55 void *map; /* userspace mmap'ing (if there is one) */
58 uint32_t name; /* flink global handle (DRI2 name) */
59 uint64_t offset; /* offset to mmap() */
60 int fd; /* dmabuf handle */
63 struct omap_device * omap_device_new(int fd)
65 struct omap_device *dev = calloc(sizeof(*dev), 1);
72 void omap_device_del(struct omap_device *dev)
77 int omap_get_param(struct omap_device *dev, uint64_t param, uint64_t *value)
79 struct drm_omap_param req = {
84 ret = drmCommandWriteRead(dev->fd, DRM_OMAP_GET_PARAM, &req, sizeof(req));
94 int omap_set_param(struct omap_device *dev, uint64_t param, uint64_t value)
96 struct drm_omap_param req = {
100 return drmCommandWrite(dev->fd, DRM_OMAP_SET_PARAM, &req, sizeof(req));
103 /* allocate a new buffer object */
104 static struct omap_bo * omap_bo_new_impl(struct omap_device *dev,
105 union omap_gem_size size, uint32_t flags)
107 struct omap_bo *bo = NULL;
108 struct drm_omap_gem_new req = {
113 if (size.bytes == 0) {
117 bo = calloc(sizeof(*bo), 1);
124 if (flags & OMAP_BO_TILED) {
125 bo->size = round_up(size.tiled.width, PAGE_SIZE) * size.tiled.height;
127 bo->size = size.bytes;
130 if (drmCommandWriteRead(dev->fd, DRM_OMAP_GEM_NEW, &req, sizeof(req))) {
134 bo->handle = req.handle;
144 /* allocate a new (un-tiled) buffer object */
145 struct omap_bo * omap_bo_new(struct omap_device *dev,
146 uint32_t size, uint32_t flags)
148 union omap_gem_size gsize = {
151 if (flags & OMAP_BO_TILED) {
154 return omap_bo_new_impl(dev, gsize, flags);
157 /* allocate a new buffer object */
158 struct omap_bo * omap_bo_new_tiled(struct omap_device *dev,
159 uint32_t width, uint32_t height, uint32_t flags)
161 union omap_gem_size gsize = {
167 if (!(flags & OMAP_BO_TILED)) {
170 return omap_bo_new_impl(dev, gsize, flags);
173 /* get buffer info */
174 static int get_buffer_info(struct omap_bo *bo)
176 struct drm_omap_gem_info req = {
177 .handle = bo->handle,
179 int ret = drmCommandWriteRead(bo->dev->fd, DRM_OMAP_GEM_INFO,
185 /* really all we need for now is mmap offset */
186 bo->offset = req.offset;
192 /* import a buffer object from DRI2 name */
193 struct omap_bo * omap_bo_from_name(struct omap_device *dev, uint32_t name)
196 struct drm_gem_open req = {
200 bo = calloc(sizeof(*bo), 1);
205 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
211 bo->handle = req.handle;
220 /* destroy a buffer object */
221 void omap_bo_del(struct omap_bo *bo)
228 munmap(bo->map, bo->size);
232 struct drm_gem_close req = {
233 .handle = bo->handle,
236 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
242 /* get the global flink/DRI2 buffer name */
243 int omap_bo_get_name(struct omap_bo *bo, uint32_t *name)
246 struct drm_gem_flink req = {
247 .handle = bo->handle,
251 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
264 uint32_t omap_bo_handle(struct omap_bo *bo)
269 int omap_bo_dmabuf(struct omap_bo *bo)
272 struct drm_prime_handle req = {
273 .handle = bo->handle,
274 .flags = DRM_CLOEXEC,
278 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &req);
288 uint32_t omap_bo_size(struct omap_bo *bo)
296 void * omap_bo_map(struct omap_bo *bo)
303 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
304 MAP_SHARED, bo->dev->fd, bo->offset);
305 if (bo->map == MAP_FAILED) {
312 int omap_bo_cpu_prep(struct omap_bo *bo, enum omap_gem_op op)
314 struct drm_omap_gem_cpu_prep req = {
315 .handle = bo->handle,
318 return drmCommandWrite(bo->dev->fd,
319 DRM_OMAP_GEM_CPU_PREP, &req, sizeof(req));
322 int omap_bo_cpu_fini(struct omap_bo *bo, enum omap_gem_op op)
324 struct drm_omap_gem_cpu_fini req = {
325 .handle = bo->handle,
329 return drmCommandWrite(bo->dev->fd,
330 DRM_OMAP_GEM_CPU_FINI, &req, sizeof(req));