blob: 08ba64eb694d7404095e5c1140a7b60cb2db4dbb [file] [log] [blame]
Rob Clarkef1b9582012-03-28 14:39:43 -05001/* -*- 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@ti.com>
27 */
28
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include <stdlib.h>
34#include <linux/stddef.h>
Rob Clark22574aa2012-07-13 16:18:25 -050035#include <linux/types.h>
Rob Clarkef1b9582012-03-28 14:39:43 -050036#include <errno.h>
37#include <sys/mman.h>
Rob Clarkf00efc72012-04-11 09:51:36 -050038#include <fcntl.h>
Rob Clark8116a322012-06-11 08:52:24 -050039#include <unistd.h>
Rob Clark22574aa2012-07-13 16:18:25 -050040#include <pthread.h>
Rob Clarkef1b9582012-03-28 14:39:43 -050041
Emil Velikov42465fe2015-04-05 15:51:59 +010042#include <libdrm_macros.h>
Rob Clarkef1b9582012-03-28 14:39:43 -050043#include <xf86drm.h>
Rob Clark22574aa2012-07-13 16:18:25 -050044#include <xf86atomic.h>
Rob Clarkef1b9582012-03-28 14:39:43 -050045
46#include "omap_drm.h"
47#include "omap_drmif.h"
48
49#define __round_mask(x, y) ((__typeof__(x))((y)-1))
50#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
51#define PAGE_SIZE 4096
52
Rob Clark22574aa2012-07-13 16:18:25 -050053static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
54static void * dev_table;
55
Rob Clarkef1b9582012-03-28 14:39:43 -050056struct omap_device {
57 int fd;
Rob Clark22574aa2012-07-13 16:18:25 -050058 atomic_t refcnt;
59
60 /* The handle_table is used to track GEM bo handles associated w/
61 * this fd. This is needed, in particular, when importing
62 * dmabuf's because we don't want multiple 'struct omap_bo's
63 * floating around with the same handle. Otherwise, when the
64 * first one is omap_bo_del()'d the handle becomes no longer
65 * valid, and the remaining 'struct omap_bo's are left pointing
66 * to an invalid handle (and possible a GEM bo that is already
67 * free'd).
68 */
69 void *handle_table;
Rob Clarkef1b9582012-03-28 14:39:43 -050070};
71
72/* a GEM buffer object allocated from the DRM device */
73struct omap_bo {
74 struct omap_device *dev;
75 void *map; /* userspace mmap'ing (if there is one) */
76 uint32_t size;
77 uint32_t handle;
78 uint32_t name; /* flink global handle (DRI2 name) */
79 uint64_t offset; /* offset to mmap() */
Rob Clarkf00efc72012-04-11 09:51:36 -050080 int fd; /* dmabuf handle */
Rob Clark22574aa2012-07-13 16:18:25 -050081 atomic_t refcnt;
Rob Clarkef1b9582012-03-28 14:39:43 -050082};
83
Rob Clark22574aa2012-07-13 16:18:25 -050084static struct omap_device * omap_device_new_impl(int fd)
Rob Clarkef1b9582012-03-28 14:39:43 -050085{
86 struct omap_device *dev = calloc(sizeof(*dev), 1);
87 if (!dev)
88 return NULL;
89 dev->fd = fd;
Rob Clark22574aa2012-07-13 16:18:25 -050090 atomic_set(&dev->refcnt, 1);
91 dev->handle_table = drmHashCreate();
92 return dev;
93}
94
Emil Velikov0f8da822015-03-31 22:32:11 +010095struct omap_device * omap_device_new(int fd)
Rob Clark22574aa2012-07-13 16:18:25 -050096{
97 struct omap_device *dev = NULL;
98
99 pthread_mutex_lock(&table_lock);
100
101 if (!dev_table)
102 dev_table = drmHashCreate();
103
104 if (drmHashLookup(dev_table, fd, (void **)&dev)) {
105 /* not found, create new device */
106 dev = omap_device_new_impl(fd);
107 drmHashInsert(dev_table, fd, dev);
108 } else {
109 /* found, just incr refcnt */
110 dev = omap_device_ref(dev);
111 }
112
113 pthread_mutex_unlock(&table_lock);
114
115 return dev;
116}
117
Emil Velikov0f8da822015-03-31 22:32:11 +0100118struct omap_device * omap_device_ref(struct omap_device *dev)
Rob Clark22574aa2012-07-13 16:18:25 -0500119{
120 atomic_inc(&dev->refcnt);
Rob Clarkef1b9582012-03-28 14:39:43 -0500121 return dev;
122}
123
Emil Velikov0f8da822015-03-31 22:32:11 +0100124void omap_device_del(struct omap_device *dev)
Rob Clarkef1b9582012-03-28 14:39:43 -0500125{
Rob Clark22574aa2012-07-13 16:18:25 -0500126 if (!atomic_dec_and_test(&dev->refcnt))
127 return;
128 pthread_mutex_lock(&table_lock);
129 drmHashDestroy(dev->handle_table);
130 drmHashDelete(dev_table, dev->fd);
131 pthread_mutex_unlock(&table_lock);
Rob Clarkef1b9582012-03-28 14:39:43 -0500132 free(dev);
133}
134
Emil Velikov0f8da822015-03-31 22:32:11 +0100135int
Maarten Lankhorst2d1044c2014-08-04 11:22:33 +0200136omap_get_param(struct omap_device *dev, uint64_t param, uint64_t *value)
Rob Clarkef1b9582012-03-28 14:39:43 -0500137{
138 struct drm_omap_param req = {
139 .param = param,
140 };
141 int ret;
142
143 ret = drmCommandWriteRead(dev->fd, DRM_OMAP_GET_PARAM, &req, sizeof(req));
144 if (ret) {
145 return ret;
146 }
147
148 *value = req.value;
149
150 return 0;
151}
152
Emil Velikov0f8da822015-03-31 22:32:11 +0100153int
Maarten Lankhorst2d1044c2014-08-04 11:22:33 +0200154omap_set_param(struct omap_device *dev, uint64_t param, uint64_t value)
Rob Clarkef1b9582012-03-28 14:39:43 -0500155{
156 struct drm_omap_param req = {
157 .param = param,
158 .value = value,
159 };
160 return drmCommandWrite(dev->fd, DRM_OMAP_SET_PARAM, &req, sizeof(req));
161}
162
Rob Clark22574aa2012-07-13 16:18:25 -0500163/* lookup a buffer from it's handle, call w/ table_lock held: */
164static struct omap_bo * lookup_bo(struct omap_device *dev,
165 uint32_t handle)
166{
167 struct omap_bo *bo = NULL;
168 if (!drmHashLookup(dev->handle_table, handle, (void **)&bo)) {
169 /* found, incr refcnt and return: */
170 bo = omap_bo_ref(bo);
171 }
172 return bo;
173}
174
175/* allocate a new buffer object, call w/ table_lock held */
176static struct omap_bo * bo_from_handle(struct omap_device *dev,
177 uint32_t handle)
178{
179 struct omap_bo *bo = calloc(sizeof(*bo), 1);
180 if (!bo) {
181 struct drm_gem_close req = {
182 .handle = handle,
183 };
184 drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
185 return NULL;
186 }
187 bo->dev = omap_device_ref(dev);
188 bo->handle = handle;
Thierry Reding508a5f72015-08-26 10:05:48 +0200189 bo->fd = -1;
Rob Clark22574aa2012-07-13 16:18:25 -0500190 atomic_set(&bo->refcnt, 1);
191 /* add ourselves to the handle table: */
192 drmHashInsert(dev->handle_table, handle, bo);
193 return bo;
194}
195
Rob Clarkef1b9582012-03-28 14:39:43 -0500196/* allocate a new buffer object */
197static struct omap_bo * omap_bo_new_impl(struct omap_device *dev,
198 union omap_gem_size size, uint32_t flags)
199{
Víctor Manuel Jáquez Leale98ed382012-04-02 18:08:29 +0200200 struct omap_bo *bo = NULL;
Rob Clarkef1b9582012-03-28 14:39:43 -0500201 struct drm_omap_gem_new req = {
202 .size = size,
203 .flags = flags,
204 };
205
206 if (size.bytes == 0) {
207 goto fail;
208 }
209
Rob Clark22574aa2012-07-13 16:18:25 -0500210 if (drmCommandWriteRead(dev->fd, DRM_OMAP_GEM_NEW, &req, sizeof(req))) {
Rob Clarkef1b9582012-03-28 14:39:43 -0500211 goto fail;
212 }
213
Rob Clark22574aa2012-07-13 16:18:25 -0500214 pthread_mutex_lock(&table_lock);
215 bo = bo_from_handle(dev, req.handle);
216 pthread_mutex_unlock(&table_lock);
Rob Clarkef1b9582012-03-28 14:39:43 -0500217
218 if (flags & OMAP_BO_TILED) {
219 bo->size = round_up(size.tiled.width, PAGE_SIZE) * size.tiled.height;
220 } else {
221 bo->size = size.bytes;
222 }
223
Rob Clarkef1b9582012-03-28 14:39:43 -0500224 return bo;
225
226fail:
227 free(bo);
228 return NULL;
229}
230
231
232/* allocate a new (un-tiled) buffer object */
Emil Velikov0f8da822015-03-31 22:32:11 +0100233struct omap_bo *
Maarten Lankhorst2d1044c2014-08-04 11:22:33 +0200234omap_bo_new(struct omap_device *dev, uint32_t size, uint32_t flags)
Rob Clarkef1b9582012-03-28 14:39:43 -0500235{
236 union omap_gem_size gsize = {
237 .bytes = size,
238 };
239 if (flags & OMAP_BO_TILED) {
240 return NULL;
241 }
242 return omap_bo_new_impl(dev, gsize, flags);
243}
244
245/* allocate a new buffer object */
Emil Velikov0f8da822015-03-31 22:32:11 +0100246struct omap_bo *
Maarten Lankhorst2d1044c2014-08-04 11:22:33 +0200247omap_bo_new_tiled(struct omap_device *dev, uint32_t width,
248 uint32_t height, uint32_t flags)
Rob Clarkef1b9582012-03-28 14:39:43 -0500249{
250 union omap_gem_size gsize = {
251 .tiled = {
252 .width = width,
253 .height = height,
254 },
255 };
256 if (!(flags & OMAP_BO_TILED)) {
257 return NULL;
258 }
259 return omap_bo_new_impl(dev, gsize, flags);
260}
261
Emil Velikov0f8da822015-03-31 22:32:11 +0100262struct omap_bo *omap_bo_ref(struct omap_bo *bo)
Rob Clark22574aa2012-07-13 16:18:25 -0500263{
264 atomic_inc(&bo->refcnt);
265 return bo;
266}
267
Rob Clarkef1b9582012-03-28 14:39:43 -0500268/* get buffer info */
269static int get_buffer_info(struct omap_bo *bo)
270{
271 struct drm_omap_gem_info req = {
272 .handle = bo->handle,
273 };
274 int ret = drmCommandWriteRead(bo->dev->fd, DRM_OMAP_GEM_INFO,
275 &req, sizeof(req));
276 if (ret) {
277 return ret;
278 }
279
280 /* really all we need for now is mmap offset */
281 bo->offset = req.offset;
282 bo->size = req.size;
283
284 return 0;
285}
286
287/* import a buffer object from DRI2 name */
Emil Velikov0f8da822015-03-31 22:32:11 +0100288struct omap_bo *
Maarten Lankhorst2d1044c2014-08-04 11:22:33 +0200289omap_bo_from_name(struct omap_device *dev, uint32_t name)
Rob Clarkef1b9582012-03-28 14:39:43 -0500290{
Rob Clark22574aa2012-07-13 16:18:25 -0500291 struct omap_bo *bo = NULL;
Rob Clarkef1b9582012-03-28 14:39:43 -0500292 struct drm_gem_open req = {
293 .name = name,
294 };
295
Rob Clark22574aa2012-07-13 16:18:25 -0500296 pthread_mutex_lock(&table_lock);
Rob Clarkef1b9582012-03-28 14:39:43 -0500297
298 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
299 goto fail;
300 }
301
Rob Clark22574aa2012-07-13 16:18:25 -0500302 bo = lookup_bo(dev, req.handle);
303 if (!bo) {
304 bo = bo_from_handle(dev, req.handle);
305 bo->name = name;
306 }
307
308 pthread_mutex_unlock(&table_lock);
Rob Clarkef1b9582012-03-28 14:39:43 -0500309
310 return bo;
311
312fail:
Vincent Penquerc'h28a13f02012-10-09 14:48:34 +0100313 pthread_mutex_unlock(&table_lock);
Rob Clarkef1b9582012-03-28 14:39:43 -0500314 free(bo);
315 return NULL;
316}
317
Rob Clark42f8a682012-06-11 17:17:17 -0500318/* import a buffer from dmabuf fd, does not take ownership of the
319 * fd so caller should close() the fd when it is otherwise done
320 * with it (even if it is still using the 'struct omap_bo *')
321 */
Emil Velikov0f8da822015-03-31 22:32:11 +0100322struct omap_bo *
Maarten Lankhorst2d1044c2014-08-04 11:22:33 +0200323omap_bo_from_dmabuf(struct omap_device *dev, int fd)
Rob Clark42f8a682012-06-11 17:17:17 -0500324{
Rob Clark22574aa2012-07-13 16:18:25 -0500325 struct omap_bo *bo = NULL;
Rob Clark42f8a682012-06-11 17:17:17 -0500326 struct drm_prime_handle req = {
327 .fd = fd,
328 };
329 int ret;
330
Rob Clark22574aa2012-07-13 16:18:25 -0500331 pthread_mutex_lock(&table_lock);
Rob Clark42f8a682012-06-11 17:17:17 -0500332
333 ret = drmIoctl(dev->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &req);
334 if (ret) {
335 goto fail;
336 }
337
Rob Clark22574aa2012-07-13 16:18:25 -0500338 bo = lookup_bo(dev, req.handle);
339 if (!bo) {
340 bo = bo_from_handle(dev, req.handle);
341 }
342
343 pthread_mutex_unlock(&table_lock);
Rob Clark42f8a682012-06-11 17:17:17 -0500344
345 return bo;
346
347fail:
Vincent Penquerc'h28a13f02012-10-09 14:48:34 +0100348 pthread_mutex_unlock(&table_lock);
Rob Clark42f8a682012-06-11 17:17:17 -0500349 free(bo);
350 return NULL;
351}
352
Rob Clarkef1b9582012-03-28 14:39:43 -0500353/* destroy a buffer object */
Emil Velikov0f8da822015-03-31 22:32:11 +0100354void omap_bo_del(struct omap_bo *bo)
Rob Clarkef1b9582012-03-28 14:39:43 -0500355{
356 if (!bo) {
357 return;
358 }
359
Rob Clark22574aa2012-07-13 16:18:25 -0500360 if (!atomic_dec_and_test(&bo->refcnt))
361 return;
362
Rob Clarkef1b9582012-03-28 14:39:43 -0500363 if (bo->map) {
364 munmap(bo->map, bo->size);
365 }
366
Emil Velikovc86dabf2015-07-14 14:59:19 +0100367 if (bo->fd >= 0) {
Rob Clark8116a322012-06-11 08:52:24 -0500368 close(bo->fd);
369 }
370
Rob Clarkef1b9582012-03-28 14:39:43 -0500371 if (bo->handle) {
372 struct drm_gem_close req = {
373 .handle = bo->handle,
374 };
Rob Clark22574aa2012-07-13 16:18:25 -0500375 pthread_mutex_lock(&table_lock);
376 drmHashDelete(bo->dev->handle_table, bo->handle);
Rob Clarkef1b9582012-03-28 14:39:43 -0500377 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
Rob Clark22574aa2012-07-13 16:18:25 -0500378 pthread_mutex_unlock(&table_lock);
Rob Clarkef1b9582012-03-28 14:39:43 -0500379 }
380
Rob Clark22574aa2012-07-13 16:18:25 -0500381 omap_device_del(bo->dev);
382
Rob Clarkef1b9582012-03-28 14:39:43 -0500383 free(bo);
384}
385
386/* get the global flink/DRI2 buffer name */
Emil Velikov0f8da822015-03-31 22:32:11 +0100387int omap_bo_get_name(struct omap_bo *bo, uint32_t *name)
Rob Clarkef1b9582012-03-28 14:39:43 -0500388{
389 if (!bo->name) {
390 struct drm_gem_flink req = {
391 .handle = bo->handle,
392 };
393 int ret;
394
395 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
396 if (ret) {
397 return ret;
398 }
399
400 bo->name = req.name;
401 }
402
403 *name = bo->name;
404
405 return 0;
406}
407
Emil Velikov0f8da822015-03-31 22:32:11 +0100408uint32_t omap_bo_handle(struct omap_bo *bo)
Rob Clarkef1b9582012-03-28 14:39:43 -0500409{
410 return bo->handle;
411}
412
Rob Clark8116a322012-06-11 08:52:24 -0500413/* caller owns the dmabuf fd that is returned and is responsible
414 * to close() it when done
415 */
Emil Velikov0f8da822015-03-31 22:32:11 +0100416int omap_bo_dmabuf(struct omap_bo *bo)
Rob Clarkf00efc72012-04-11 09:51:36 -0500417{
Emil Velikovc86dabf2015-07-14 14:59:19 +0100418 if (bo->fd < 0) {
Rob Clarkf00efc72012-04-11 09:51:36 -0500419 struct drm_prime_handle req = {
420 .handle = bo->handle,
421 .flags = DRM_CLOEXEC,
422 };
423 int ret;
424
425 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &req);
426 if (ret) {
427 return ret;
428 }
429
430 bo->fd = req.fd;
431 }
Rob Clark8116a322012-06-11 08:52:24 -0500432 return dup(bo->fd);
Rob Clarkf00efc72012-04-11 09:51:36 -0500433}
434
Emil Velikov0f8da822015-03-31 22:32:11 +0100435uint32_t omap_bo_size(struct omap_bo *bo)
Rob Clarkef1b9582012-03-28 14:39:43 -0500436{
437 if (!bo->size) {
438 get_buffer_info(bo);
439 }
440 return bo->size;
441}
442
Emil Velikov0f8da822015-03-31 22:32:11 +0100443void *omap_bo_map(struct omap_bo *bo)
Rob Clarkef1b9582012-03-28 14:39:43 -0500444{
445 if (!bo->map) {
446 if (!bo->offset) {
447 get_buffer_info(bo);
448 }
449
450 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
451 MAP_SHARED, bo->dev->fd, bo->offset);
452 if (bo->map == MAP_FAILED) {
453 bo->map = NULL;
454 }
455 }
456 return bo->map;
457}
458
Emil Velikov0f8da822015-03-31 22:32:11 +0100459int omap_bo_cpu_prep(struct omap_bo *bo, enum omap_gem_op op)
Rob Clarkef1b9582012-03-28 14:39:43 -0500460{
461 struct drm_omap_gem_cpu_prep req = {
462 .handle = bo->handle,
463 .op = op,
464 };
465 return drmCommandWrite(bo->dev->fd,
466 DRM_OMAP_GEM_CPU_PREP, &req, sizeof(req));
467}
468
Emil Velikov0f8da822015-03-31 22:32:11 +0100469int omap_bo_cpu_fini(struct omap_bo *bo, enum omap_gem_op op)
Rob Clarkef1b9582012-03-28 14:39:43 -0500470{
471 struct drm_omap_gem_cpu_fini req = {
472 .handle = bo->handle,
473 .op = op,
474 .nregions = 0,
475 };
476 return drmCommandWrite(bo->dev->fd,
477 DRM_OMAP_GEM_CPU_FINI, &req, sizeof(req));
478}