Home | History | Annotate | Download | only in omap
      1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
      2 
      3 /*
      4  * Copyright (C) 2011 Texas Instruments, Inc
      5  *
      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:
     12  *
     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
     15  * Software.
     16  *
     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
     23  * SOFTWARE.
     24  *
     25  * Authors:
     26  *    Rob Clark <rob (at) ti.com>
     27  */
     28 
     29 #include <stdlib.h>
     30 #include <linux/stddef.h>
     31 #include <linux/types.h>
     32 #include <errno.h>
     33 #include <sys/mman.h>
     34 #include <fcntl.h>
     35 #include <unistd.h>
     36 #include <pthread.h>
     37 
     38 #include <libdrm_macros.h>
     39 #include <xf86drm.h>
     40 #include <xf86atomic.h>
     41 
     42 #include "omap_drm.h"
     43 #include "omap_drmif.h"
     44 
     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
     48 
     49 static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
     50 static void * dev_table;
     51 
     52 struct omap_device {
     53 	int fd;
     54 	atomic_t refcnt;
     55 
     56 	/* The handle_table is used to track GEM bo handles associated w/
     57 	 * this fd.  This is needed, in particular, when importing
     58 	 * dmabuf's because we don't want multiple 'struct omap_bo's
     59 	 * floating around with the same handle.  Otherwise, when the
     60 	 * first one is omap_bo_del()'d the handle becomes no longer
     61 	 * valid, and the remaining 'struct omap_bo's are left pointing
     62 	 * to an invalid handle (and possible a GEM bo that is already
     63 	 * free'd).
     64 	 */
     65 	void *handle_table;
     66 };
     67 
     68 /* a GEM buffer object allocated from the DRM device */
     69 struct omap_bo {
     70 	struct omap_device	*dev;
     71 	void		*map;		/* userspace mmap'ing (if there is one) */
     72 	uint32_t	size;
     73 	uint32_t	handle;
     74 	uint32_t	name;		/* flink global handle (DRI2 name) */
     75 	uint64_t	offset;		/* offset to mmap() */
     76 	int		fd;		/* dmabuf handle */
     77 	atomic_t	refcnt;
     78 };
     79 
     80 static struct omap_device * omap_device_new_impl(int fd)
     81 {
     82 	struct omap_device *dev = calloc(sizeof(*dev), 1);
     83 	if (!dev)
     84 		return NULL;
     85 	dev->fd = fd;
     86 	atomic_set(&dev->refcnt, 1);
     87 	dev->handle_table = drmHashCreate();
     88 	return dev;
     89 }
     90 
     91 struct omap_device * omap_device_new(int fd)
     92 {
     93 	struct omap_device *dev = NULL;
     94 
     95 	pthread_mutex_lock(&table_lock);
     96 
     97 	if (!dev_table)
     98 		dev_table = drmHashCreate();
     99 
    100 	if (drmHashLookup(dev_table, fd, (void **)&dev)) {
    101 		/* not found, create new device */
    102 		dev = omap_device_new_impl(fd);
    103 		drmHashInsert(dev_table, fd, dev);
    104 	} else {
    105 		/* found, just incr refcnt */
    106 		dev = omap_device_ref(dev);
    107 	}
    108 
    109 	pthread_mutex_unlock(&table_lock);
    110 
    111 	return dev;
    112 }
    113 
    114 struct omap_device * omap_device_ref(struct omap_device *dev)
    115 {
    116 	atomic_inc(&dev->refcnt);
    117 	return dev;
    118 }
    119 
    120 void omap_device_del(struct omap_device *dev)
    121 {
    122 	if (!atomic_dec_and_test(&dev->refcnt))
    123 		return;
    124 	pthread_mutex_lock(&table_lock);
    125 	drmHashDestroy(dev->handle_table);
    126 	drmHashDelete(dev_table, dev->fd);
    127 	pthread_mutex_unlock(&table_lock);
    128 	free(dev);
    129 }
    130 
    131 int
    132 omap_get_param(struct omap_device *dev, uint64_t param, uint64_t *value)
    133 {
    134 	struct drm_omap_param req = {
    135 			.param = param,
    136 	};
    137 	int ret;
    138 
    139 	ret = drmCommandWriteRead(dev->fd, DRM_OMAP_GET_PARAM, &req, sizeof(req));
    140 	if (ret) {
    141 		return ret;
    142 	}
    143 
    144 	*value = req.value;
    145 
    146 	return 0;
    147 }
    148 
    149 int
    150 omap_set_param(struct omap_device *dev, uint64_t param, uint64_t value)
    151 {
    152 	struct drm_omap_param req = {
    153 			.param = param,
    154 			.value = value,
    155 	};
    156 	return drmCommandWrite(dev->fd, DRM_OMAP_SET_PARAM, &req, sizeof(req));
    157 }
    158 
    159 /* lookup a buffer from it's handle, call w/ table_lock held: */
    160 static struct omap_bo * lookup_bo(struct omap_device *dev,
    161 		uint32_t handle)
    162 {
    163 	struct omap_bo *bo = NULL;
    164 	if (!drmHashLookup(dev->handle_table, handle, (void **)&bo)) {
    165 		/* found, incr refcnt and return: */
    166 		bo = omap_bo_ref(bo);
    167 	}
    168 	return bo;
    169 }
    170 
    171 /* allocate a new buffer object, call w/ table_lock held */
    172 static struct omap_bo * bo_from_handle(struct omap_device *dev,
    173 		uint32_t handle)
    174 {
    175 	struct omap_bo *bo = calloc(sizeof(*bo), 1);
    176 	if (!bo) {
    177 		struct drm_gem_close req = {
    178 				.handle = handle,
    179 		};
    180 		drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
    181 		return NULL;
    182 	}
    183 	bo->dev = omap_device_ref(dev);
    184 	bo->handle = handle;
    185 	bo->fd = -1;
    186 	atomic_set(&bo->refcnt, 1);
    187 	/* add ourselves to the handle table: */
    188 	drmHashInsert(dev->handle_table, handle, bo);
    189 	return bo;
    190 }
    191 
    192 /* allocate a new buffer object */
    193 static struct omap_bo * omap_bo_new_impl(struct omap_device *dev,
    194 		union omap_gem_size size, uint32_t flags)
    195 {
    196 	struct omap_bo *bo = NULL;
    197 	struct drm_omap_gem_new req = {
    198 			.size = size,
    199 			.flags = flags,
    200 	};
    201 
    202 	if (size.bytes == 0) {
    203 		goto fail;
    204 	}
    205 
    206 	if (drmCommandWriteRead(dev->fd, DRM_OMAP_GEM_NEW, &req, sizeof(req))) {
    207 		goto fail;
    208 	}
    209 
    210 	pthread_mutex_lock(&table_lock);
    211 	bo = bo_from_handle(dev, req.handle);
    212 	pthread_mutex_unlock(&table_lock);
    213 
    214 	if (flags & OMAP_BO_TILED) {
    215 		bo->size = round_up(size.tiled.width, PAGE_SIZE) * size.tiled.height;
    216 	} else {
    217 		bo->size = size.bytes;
    218 	}
    219 
    220 	return bo;
    221 
    222 fail:
    223 	free(bo);
    224 	return NULL;
    225 }
    226 
    227 
    228 /* allocate a new (un-tiled) buffer object */
    229 struct omap_bo *
    230 omap_bo_new(struct omap_device *dev, uint32_t size, uint32_t flags)
    231 {
    232 	union omap_gem_size gsize = {
    233 			.bytes = size,
    234 	};
    235 	if (flags & OMAP_BO_TILED) {
    236 		return NULL;
    237 	}
    238 	return omap_bo_new_impl(dev, gsize, flags);
    239 }
    240 
    241 /* allocate a new buffer object */
    242 struct omap_bo *
    243 omap_bo_new_tiled(struct omap_device *dev, uint32_t width,
    244 		  uint32_t height, uint32_t flags)
    245 {
    246 	union omap_gem_size gsize = {
    247 			.tiled = {
    248 				.width = width,
    249 				.height = height,
    250 			},
    251 	};
    252 	if (!(flags & OMAP_BO_TILED)) {
    253 		return NULL;
    254 	}
    255 	return omap_bo_new_impl(dev, gsize, flags);
    256 }
    257 
    258 struct omap_bo *omap_bo_ref(struct omap_bo *bo)
    259 {
    260 	atomic_inc(&bo->refcnt);
    261 	return bo;
    262 }
    263 
    264 /* get buffer info */
    265 static int get_buffer_info(struct omap_bo *bo)
    266 {
    267 	struct drm_omap_gem_info req = {
    268 			.handle = bo->handle,
    269 	};
    270 	int ret = drmCommandWriteRead(bo->dev->fd, DRM_OMAP_GEM_INFO,
    271 			&req, sizeof(req));
    272 	if (ret) {
    273 		return ret;
    274 	}
    275 
    276 	/* really all we need for now is mmap offset */
    277 	bo->offset = req.offset;
    278 	bo->size = req.size;
    279 
    280 	return 0;
    281 }
    282 
    283 /* import a buffer object from DRI2 name */
    284 struct omap_bo *
    285 omap_bo_from_name(struct omap_device *dev, uint32_t name)
    286 {
    287 	struct omap_bo *bo = NULL;
    288 	struct drm_gem_open req = {
    289 			.name = name,
    290 	};
    291 
    292 	pthread_mutex_lock(&table_lock);
    293 
    294 	if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
    295 		goto fail;
    296 	}
    297 
    298 	bo = lookup_bo(dev, req.handle);
    299 	if (!bo) {
    300 		bo = bo_from_handle(dev, req.handle);
    301 		bo->name = name;
    302 	}
    303 
    304 	pthread_mutex_unlock(&table_lock);
    305 
    306 	return bo;
    307 
    308 fail:
    309 	pthread_mutex_unlock(&table_lock);
    310 	free(bo);
    311 	return NULL;
    312 }
    313 
    314 /* import a buffer from dmabuf fd, does not take ownership of the
    315  * fd so caller should close() the fd when it is otherwise done
    316  * with it (even if it is still using the 'struct omap_bo *')
    317  */
    318 struct omap_bo *
    319 omap_bo_from_dmabuf(struct omap_device *dev, int fd)
    320 {
    321 	struct omap_bo *bo = NULL;
    322 	struct drm_prime_handle req = {
    323 			.fd = fd,
    324 	};
    325 	int ret;
    326 
    327 	pthread_mutex_lock(&table_lock);
    328 
    329 	ret = drmIoctl(dev->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &req);
    330 	if (ret) {
    331 		goto fail;
    332 	}
    333 
    334 	bo = lookup_bo(dev, req.handle);
    335 	if (!bo) {
    336 		bo = bo_from_handle(dev, req.handle);
    337 	}
    338 
    339 	pthread_mutex_unlock(&table_lock);
    340 
    341 	return bo;
    342 
    343 fail:
    344 	pthread_mutex_unlock(&table_lock);
    345 	free(bo);
    346 	return NULL;
    347 }
    348 
    349 /* destroy a buffer object */
    350 void omap_bo_del(struct omap_bo *bo)
    351 {
    352 	if (!bo) {
    353 		return;
    354 	}
    355 
    356 	if (!atomic_dec_and_test(&bo->refcnt))
    357 		return;
    358 
    359 	if (bo->map) {
    360 		munmap(bo->map, bo->size);
    361 	}
    362 
    363 	if (bo->fd >= 0) {
    364 		close(bo->fd);
    365 	}
    366 
    367 	if (bo->handle) {
    368 		struct drm_gem_close req = {
    369 				.handle = bo->handle,
    370 		};
    371 		pthread_mutex_lock(&table_lock);
    372 		drmHashDelete(bo->dev->handle_table, bo->handle);
    373 		drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
    374 		pthread_mutex_unlock(&table_lock);
    375 	}
    376 
    377 	omap_device_del(bo->dev);
    378 
    379 	free(bo);
    380 }
    381 
    382 /* get the global flink/DRI2 buffer name */
    383 int omap_bo_get_name(struct omap_bo *bo, uint32_t *name)
    384 {
    385 	if (!bo->name) {
    386 		struct drm_gem_flink req = {
    387 				.handle = bo->handle,
    388 		};
    389 		int ret;
    390 
    391 		ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
    392 		if (ret) {
    393 			return ret;
    394 		}
    395 
    396 		bo->name = req.name;
    397 	}
    398 
    399 	*name = bo->name;
    400 
    401 	return 0;
    402 }
    403 
    404 uint32_t omap_bo_handle(struct omap_bo *bo)
    405 {
    406 	return bo->handle;
    407 }
    408 
    409 /* caller owns the dmabuf fd that is returned and is responsible
    410  * to close() it when done
    411  */
    412 int omap_bo_dmabuf(struct omap_bo *bo)
    413 {
    414 	if (bo->fd < 0) {
    415 		struct drm_prime_handle req = {
    416 				.handle = bo->handle,
    417 				.flags = DRM_CLOEXEC | DRM_RDWR,
    418 		};
    419 		int ret;
    420 
    421 		ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &req);
    422 		if (ret) {
    423 			return ret;
    424 		}
    425 
    426 		bo->fd = req.fd;
    427 	}
    428 	return dup(bo->fd);
    429 }
    430 
    431 uint32_t omap_bo_size(struct omap_bo *bo)
    432 {
    433 	if (!bo->size) {
    434 		get_buffer_info(bo);
    435 	}
    436 	return bo->size;
    437 }
    438 
    439 void *omap_bo_map(struct omap_bo *bo)
    440 {
    441 	if (!bo->map) {
    442 		if (!bo->offset) {
    443 			get_buffer_info(bo);
    444 		}
    445 
    446 		bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
    447 				MAP_SHARED, bo->dev->fd, bo->offset);
    448 		if (bo->map == MAP_FAILED) {
    449 			bo->map = NULL;
    450 		}
    451 	}
    452 	return bo->map;
    453 }
    454 
    455 int omap_bo_cpu_prep(struct omap_bo *bo, enum omap_gem_op op)
    456 {
    457 	struct drm_omap_gem_cpu_prep req = {
    458 			.handle = bo->handle,
    459 			.op = op,
    460 	};
    461 	return drmCommandWrite(bo->dev->fd,
    462 			DRM_OMAP_GEM_CPU_PREP, &req, sizeof(req));
    463 }
    464 
    465 int omap_bo_cpu_fini(struct omap_bo *bo, enum omap_gem_op op)
    466 {
    467 	struct drm_omap_gem_cpu_fini req = {
    468 			.handle = bo->handle,
    469 			.op = op,
    470 			.nregions = 0,
    471 	};
    472 	return drmCommandWrite(bo->dev->fd,
    473 			DRM_OMAP_GEM_CPU_FINI, &req, sizeof(req));
    474 }
    475