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 #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
omap_device_new_impl(int fd)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
omap_device_new(int fd)91 drm_public 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
omap_device_ref(struct omap_device * dev)114 drm_public struct omap_device * omap_device_ref(struct omap_device *dev)
115 {
116 atomic_inc(&dev->refcnt);
117 return dev;
118 }
119
omap_device_del(struct omap_device * dev)120 drm_public 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 drm_public int
omap_get_param(struct omap_device * dev,uint64_t param,uint64_t * value)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 drm_public int
omap_set_param(struct omap_device * dev,uint64_t param,uint64_t value)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: */
lookup_bo(struct omap_device * dev,uint32_t handle)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 */
bo_from_handle(struct omap_device * dev,uint32_t handle)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 drmCloseBufferHandle(dev->fd, handle);
178 return NULL;
179 }
180 bo->dev = omap_device_ref(dev);
181 bo->handle = handle;
182 bo->fd = -1;
183 atomic_set(&bo->refcnt, 1);
184 /* add ourselves to the handle table: */
185 drmHashInsert(dev->handle_table, handle, bo);
186 return bo;
187 }
188
189 /* allocate a new buffer object */
omap_bo_new_impl(struct omap_device * dev,union omap_gem_size size,uint32_t flags)190 static struct omap_bo * omap_bo_new_impl(struct omap_device *dev,
191 union omap_gem_size size, uint32_t flags)
192 {
193 struct omap_bo *bo = NULL;
194 struct drm_omap_gem_new req = {
195 .size = size,
196 .flags = flags,
197 };
198
199 if (size.bytes == 0) {
200 goto fail;
201 }
202
203 if (drmCommandWriteRead(dev->fd, DRM_OMAP_GEM_NEW, &req, sizeof(req))) {
204 goto fail;
205 }
206
207 pthread_mutex_lock(&table_lock);
208 bo = bo_from_handle(dev, req.handle);
209 pthread_mutex_unlock(&table_lock);
210
211 if (flags & OMAP_BO_TILED) {
212 bo->size = round_up(size.tiled.width, PAGE_SIZE) * size.tiled.height;
213 } else {
214 bo->size = size.bytes;
215 }
216
217 return bo;
218
219 fail:
220 free(bo);
221 return NULL;
222 }
223
224
225 /* allocate a new (un-tiled) buffer object */
226 drm_public struct omap_bo *
omap_bo_new(struct omap_device * dev,uint32_t size,uint32_t flags)227 omap_bo_new(struct omap_device *dev, uint32_t size, uint32_t flags)
228 {
229 union omap_gem_size gsize = {
230 .bytes = size,
231 };
232 if (flags & OMAP_BO_TILED) {
233 return NULL;
234 }
235 return omap_bo_new_impl(dev, gsize, flags);
236 }
237
238 /* allocate a new buffer object */
239 drm_public struct omap_bo *
omap_bo_new_tiled(struct omap_device * dev,uint32_t width,uint32_t height,uint32_t flags)240 omap_bo_new_tiled(struct omap_device *dev, uint32_t width,
241 uint32_t height, uint32_t flags)
242 {
243 union omap_gem_size gsize = {
244 .tiled = {
245 .width = width,
246 .height = height,
247 },
248 };
249 if (!(flags & OMAP_BO_TILED)) {
250 return NULL;
251 }
252 return omap_bo_new_impl(dev, gsize, flags);
253 }
254
omap_bo_ref(struct omap_bo * bo)255 drm_public struct omap_bo *omap_bo_ref(struct omap_bo *bo)
256 {
257 atomic_inc(&bo->refcnt);
258 return bo;
259 }
260
261 /* get buffer info */
get_buffer_info(struct omap_bo * bo)262 static int get_buffer_info(struct omap_bo *bo)
263 {
264 struct drm_omap_gem_info req = {
265 .handle = bo->handle,
266 };
267 int ret = drmCommandWriteRead(bo->dev->fd, DRM_OMAP_GEM_INFO,
268 &req, sizeof(req));
269 if (ret) {
270 return ret;
271 }
272
273 /* really all we need for now is mmap offset */
274 bo->offset = req.offset;
275 bo->size = req.size;
276
277 return 0;
278 }
279
280 /* import a buffer object from DRI2 name */
281 drm_public struct omap_bo *
omap_bo_from_name(struct omap_device * dev,uint32_t name)282 omap_bo_from_name(struct omap_device *dev, uint32_t name)
283 {
284 struct omap_bo *bo = NULL;
285 struct drm_gem_open req = {
286 .name = name,
287 };
288
289 pthread_mutex_lock(&table_lock);
290
291 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
292 goto fail;
293 }
294
295 bo = lookup_bo(dev, req.handle);
296 if (!bo) {
297 bo = bo_from_handle(dev, req.handle);
298 bo->name = name;
299 }
300
301 pthread_mutex_unlock(&table_lock);
302
303 return bo;
304
305 fail:
306 pthread_mutex_unlock(&table_lock);
307 free(bo);
308 return NULL;
309 }
310
311 /* import a buffer from dmabuf fd, does not take ownership of the
312 * fd so caller should close() the fd when it is otherwise done
313 * with it (even if it is still using the 'struct omap_bo *')
314 */
315 drm_public struct omap_bo *
omap_bo_from_dmabuf(struct omap_device * dev,int fd)316 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 */
omap_bo_del(struct omap_bo * bo)347 drm_public 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 >= 0) {
361 close(bo->fd);
362 }
363
364 if (bo->handle) {
365 pthread_mutex_lock(&table_lock);
366 drmHashDelete(bo->dev->handle_table, bo->handle);
367 drmCloseBufferHandle(bo->dev->fd, bo->handle);
368 pthread_mutex_unlock(&table_lock);
369 }
370
371 omap_device_del(bo->dev);
372
373 free(bo);
374 }
375
376 /* get the global flink/DRI2 buffer name */
omap_bo_get_name(struct omap_bo * bo,uint32_t * name)377 drm_public int omap_bo_get_name(struct omap_bo *bo, uint32_t *name)
378 {
379 if (!bo->name) {
380 struct drm_gem_flink req = {
381 .handle = bo->handle,
382 };
383 int ret;
384
385 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
386 if (ret) {
387 return ret;
388 }
389
390 bo->name = req.name;
391 }
392
393 *name = bo->name;
394
395 return 0;
396 }
397
omap_bo_handle(struct omap_bo * bo)398 drm_public uint32_t omap_bo_handle(struct omap_bo *bo)
399 {
400 return bo->handle;
401 }
402
403 /* caller owns the dmabuf fd that is returned and is responsible
404 * to close() it when done
405 */
omap_bo_dmabuf(struct omap_bo * bo)406 drm_public int omap_bo_dmabuf(struct omap_bo *bo)
407 {
408 if (bo->fd < 0) {
409 struct drm_prime_handle req = {
410 .handle = bo->handle,
411 .flags = DRM_CLOEXEC | DRM_RDWR,
412 };
413 int ret;
414
415 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &req);
416 if (ret) {
417 return ret;
418 }
419
420 bo->fd = req.fd;
421 }
422 return dup(bo->fd);
423 }
424
omap_bo_size(struct omap_bo * bo)425 drm_public uint32_t omap_bo_size(struct omap_bo *bo)
426 {
427 if (!bo->size) {
428 get_buffer_info(bo);
429 }
430 return bo->size;
431 }
432
omap_bo_map(struct omap_bo * bo)433 drm_public void *omap_bo_map(struct omap_bo *bo)
434 {
435 if (!bo->map) {
436 if (!bo->offset) {
437 get_buffer_info(bo);
438 }
439
440 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
441 MAP_SHARED, bo->dev->fd, bo->offset);
442 if (bo->map == MAP_FAILED) {
443 bo->map = NULL;
444 }
445 }
446 return bo->map;
447 }
448
omap_bo_cpu_prep(struct omap_bo * bo,enum omap_gem_op op)449 drm_public int omap_bo_cpu_prep(struct omap_bo *bo, enum omap_gem_op op)
450 {
451 struct drm_omap_gem_cpu_prep req = {
452 .handle = bo->handle,
453 .op = op,
454 };
455 return drmCommandWrite(bo->dev->fd,
456 DRM_OMAP_GEM_CPU_PREP, &req, sizeof(req));
457 }
458
omap_bo_cpu_fini(struct omap_bo * bo,enum omap_gem_op op)459 drm_public int omap_bo_cpu_fini(struct omap_bo *bo, enum omap_gem_op op)
460 {
461 struct drm_omap_gem_cpu_fini req = {
462 .handle = bo->handle,
463 .op = op,
464 .nregions = 0,
465 };
466 return drmCommandWrite(bo->dev->fd,
467 DRM_OMAP_GEM_CPU_FINI, &req, sizeof(req));
468 }
469