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>
41 #include "omap_drmif.h"
43 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
44 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
45 #define PAGE_SIZE 4096
51 /* a GEM buffer object allocated from the DRM device */
53 struct omap_device *dev;
54 void *map; /* userspace mmap'ing (if there is one) */
57 uint32_t name; /* flink global handle (DRI2 name) */
58 uint64_t offset; /* offset to mmap() */
61 struct omap_device * omap_device_new(int fd)
63 struct omap_device *dev = calloc(sizeof(*dev), 1);
70 void omap_device_del(struct omap_device *dev)
75 int omap_get_param(struct omap_device *dev, uint64_t param, uint64_t *value)
77 struct drm_omap_param req = {
82 ret = drmCommandWriteRead(dev->fd, DRM_OMAP_GET_PARAM, &req, sizeof(req));
92 int omap_set_param(struct omap_device *dev, uint64_t param, uint64_t value)
94 struct drm_omap_param req = {
98 return drmCommandWrite(dev->fd, DRM_OMAP_SET_PARAM, &req, sizeof(req));
101 /* allocate a new buffer object */
102 static struct omap_bo * omap_bo_new_impl(struct omap_device *dev,
103 union omap_gem_size size, uint32_t flags)
105 struct omap_bo *bo = NULL;
106 struct drm_omap_gem_new req = {
111 if (size.bytes == 0) {
115 bo = calloc(sizeof(*bo), 1);
122 if (flags & OMAP_BO_TILED) {
123 bo->size = round_up(size.tiled.width, PAGE_SIZE) * size.tiled.height;
125 bo->size = size.bytes;
128 if (drmCommandWriteRead(dev->fd, DRM_OMAP_GEM_NEW, &req, sizeof(req))) {
132 bo->handle = req.handle;
142 /* allocate a new (un-tiled) buffer object */
143 struct omap_bo * omap_bo_new(struct omap_device *dev,
144 uint32_t size, uint32_t flags)
146 union omap_gem_size gsize = {
149 if (flags & OMAP_BO_TILED) {
152 return omap_bo_new_impl(dev, gsize, flags);
155 /* allocate a new buffer object */
156 struct omap_bo * omap_bo_new_tiled(struct omap_device *dev,
157 uint32_t width, uint32_t height, uint32_t flags)
159 union omap_gem_size gsize = {
165 if (!(flags & OMAP_BO_TILED)) {
168 return omap_bo_new_impl(dev, gsize, flags);
171 /* get buffer info */
172 static int get_buffer_info(struct omap_bo *bo)
174 struct drm_omap_gem_info req = {
175 .handle = bo->handle,
177 int ret = drmCommandWriteRead(bo->dev->fd, DRM_OMAP_GEM_INFO,
183 /* really all we need for now is mmap offset */
184 bo->offset = req.offset;
190 /* import a buffer object from DRI2 name */
191 struct omap_bo * omap_bo_from_name(struct omap_device *dev, uint32_t name)
194 struct drm_gem_open req = {
198 bo = calloc(sizeof(*bo), 1);
203 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
209 bo->handle = req.handle;
218 /* destroy a buffer object */
219 void omap_bo_del(struct omap_bo *bo)
226 munmap(bo->map, bo->size);
230 struct drm_gem_close req = {
231 .handle = bo->handle,
234 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
240 /* get the global flink/DRI2 buffer name */
241 int omap_bo_get_name(struct omap_bo *bo, uint32_t *name)
244 struct drm_gem_flink req = {
245 .handle = bo->handle,
249 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
262 uint32_t omap_bo_handle(struct omap_bo *bo)
267 uint32_t omap_bo_size(struct omap_bo *bo)
275 void * omap_bo_map(struct omap_bo *bo)
282 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
283 MAP_SHARED, bo->dev->fd, bo->offset);
284 if (bo->map == MAP_FAILED) {
291 int omap_bo_cpu_prep(struct omap_bo *bo, enum omap_gem_op op)
293 struct drm_omap_gem_cpu_prep req = {
294 .handle = bo->handle,
297 return drmCommandWrite(bo->dev->fd,
298 DRM_OMAP_GEM_CPU_PREP, &req, sizeof(req));
301 int omap_bo_cpu_fini(struct omap_bo *bo, enum omap_gem_op op)
303 struct drm_omap_gem_cpu_fini req = {
304 .handle = bo->handle,
308 return drmCommandWrite(bo->dev->fd,
309 DRM_OMAP_GEM_CPU_FINI, &req, sizeof(req));