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