Debian Codegraph @

omap/omap_drm.c

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