OSDN Git Service

omap: clarify dmabuf file descriptor ownership
[android-x86/external-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 <errno.h>
36 #include <sys/mman.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39
40 #include <xf86drm.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 struct omap_device {
50         int fd;
51 };
52
53 /* a GEM buffer object allocated from the DRM device */
54 struct omap_bo {
55         struct omap_device      *dev;
56         void            *map;           /* userspace mmap'ing (if there is one) */
57         uint32_t        size;
58         uint32_t        handle;
59         uint32_t        name;           /* flink global handle (DRI2 name) */
60         uint64_t        offset;         /* offset to mmap() */
61         int             fd;             /* dmabuf handle */
62 };
63
64 struct omap_device * omap_device_new(int fd)
65 {
66         struct omap_device *dev = calloc(sizeof(*dev), 1);
67         if (!dev)
68                 return NULL;
69         dev->fd = fd;
70         return dev;
71 }
72
73 void omap_device_del(struct omap_device *dev)
74 {
75         free(dev);
76 }
77
78 int omap_get_param(struct omap_device *dev, uint64_t param, uint64_t *value)
79 {
80         struct drm_omap_param req = {
81                         .param = param,
82         };
83         int ret;
84
85         ret = drmCommandWriteRead(dev->fd, DRM_OMAP_GET_PARAM, &req, sizeof(req));
86         if (ret) {
87                 return ret;
88         }
89
90         *value = req.value;
91
92         return 0;
93 }
94
95 int omap_set_param(struct omap_device *dev, uint64_t param, uint64_t value)
96 {
97         struct drm_omap_param req = {
98                         .param = param,
99                         .value = value,
100         };
101         return drmCommandWrite(dev->fd, DRM_OMAP_SET_PARAM, &req, sizeof(req));
102 }
103
104 /* allocate a new buffer object */
105 static struct omap_bo * omap_bo_new_impl(struct omap_device *dev,
106                 union omap_gem_size size, uint32_t flags)
107 {
108         struct omap_bo *bo = NULL;
109         struct drm_omap_gem_new req = {
110                         .size = size,
111                         .flags = flags,
112         };
113
114         if (size.bytes == 0) {
115                 goto fail;
116         }
117
118         bo = calloc(sizeof(*bo), 1);
119         if (!bo) {
120                 goto fail;
121         }
122
123         bo->dev = dev;
124
125         if (flags & OMAP_BO_TILED) {
126                 bo->size = round_up(size.tiled.width, PAGE_SIZE) * size.tiled.height;
127         } else {
128                 bo->size = size.bytes;
129         }
130
131         if (drmCommandWriteRead(dev->fd, DRM_OMAP_GEM_NEW, &req, sizeof(req))) {
132                 goto fail;
133         }
134
135         bo->handle = req.handle;
136
137         return bo;
138
139 fail:
140         free(bo);
141         return NULL;
142 }
143
144
145 /* allocate a new (un-tiled) buffer object */
146 struct omap_bo * omap_bo_new(struct omap_device *dev,
147                 uint32_t size, uint32_t flags)
148 {
149         union omap_gem_size gsize = {
150                         .bytes = size,
151         };
152         if (flags & OMAP_BO_TILED) {
153                 return NULL;
154         }
155         return omap_bo_new_impl(dev, gsize, flags);
156 }
157
158 /* allocate a new buffer object */
159 struct omap_bo * omap_bo_new_tiled(struct omap_device *dev,
160                 uint32_t width, uint32_t height, uint32_t flags)
161 {
162         union omap_gem_size gsize = {
163                         .tiled = {
164                                 .width = width,
165                                 .height = height,
166                         },
167         };
168         if (!(flags & OMAP_BO_TILED)) {
169                 return NULL;
170         }
171         return omap_bo_new_impl(dev, gsize, flags);
172 }
173
174 /* get buffer info */
175 static int get_buffer_info(struct omap_bo *bo)
176 {
177         struct drm_omap_gem_info req = {
178                         .handle = bo->handle,
179         };
180         int ret = drmCommandWriteRead(bo->dev->fd, DRM_OMAP_GEM_INFO,
181                         &req, sizeof(req));
182         if (ret) {
183                 return ret;
184         }
185
186         /* really all we need for now is mmap offset */
187         bo->offset = req.offset;
188         bo->size = req.size;
189
190         return 0;
191 }
192
193 /* import a buffer object from DRI2 name */
194 struct omap_bo * omap_bo_from_name(struct omap_device *dev, uint32_t name)
195 {
196         struct omap_bo *bo;
197         struct drm_gem_open req = {
198                         .name = name,
199         };
200
201         bo = calloc(sizeof(*bo), 1);
202         if (!bo) {
203                 goto fail;
204         }
205
206         if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
207                 goto fail;
208         }
209
210         bo->dev = dev;
211         bo->name = name;
212         bo->handle = req.handle;
213
214         return bo;
215
216 fail:
217         free(bo);
218         return NULL;
219 }
220
221 /* destroy a buffer object */
222 void omap_bo_del(struct omap_bo *bo)
223 {
224         if (!bo) {
225                 return;
226         }
227
228         if (bo->map) {
229                 munmap(bo->map, bo->size);
230         }
231
232         if (bo->fd) {
233                 close(bo->fd);
234         }
235
236         if (bo->handle) {
237                 struct drm_gem_close req = {
238                                 .handle = bo->handle,
239                 };
240
241                 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
242         }
243
244         free(bo);
245 }
246
247 /* get the global flink/DRI2 buffer name */
248 int omap_bo_get_name(struct omap_bo *bo, uint32_t *name)
249 {
250         if (!bo->name) {
251                 struct drm_gem_flink req = {
252                                 .handle = bo->handle,
253                 };
254                 int ret;
255
256                 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
257                 if (ret) {
258                         return ret;
259                 }
260
261                 bo->name = req.name;
262         }
263
264         *name = bo->name;
265
266         return 0;
267 }
268
269 uint32_t omap_bo_handle(struct omap_bo *bo)
270 {
271         return bo->handle;
272 }
273
274 /* caller owns the dmabuf fd that is returned and is responsible
275  * to close() it when done
276  */
277 int omap_bo_dmabuf(struct omap_bo *bo)
278 {
279         if (!bo->fd) {
280                 struct drm_prime_handle req = {
281                                 .handle = bo->handle,
282                                 .flags = DRM_CLOEXEC,
283                 };
284                 int ret;
285
286                 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &req);
287                 if (ret) {
288                         return ret;
289                 }
290
291                 bo->fd = req.fd;
292         }
293         return dup(bo->fd);
294 }
295
296 uint32_t omap_bo_size(struct omap_bo *bo)
297 {
298         if (!bo->size) {
299                 get_buffer_info(bo);
300         }
301         return bo->size;
302 }
303
304 void * omap_bo_map(struct omap_bo *bo)
305 {
306         if (!bo->map) {
307                 if (!bo->offset) {
308                         get_buffer_info(bo);
309                 }
310
311                 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
312                                 MAP_SHARED, bo->dev->fd, bo->offset);
313                 if (bo->map == MAP_FAILED) {
314                         bo->map = NULL;
315                 }
316         }
317         return bo->map;
318 }
319
320 int omap_bo_cpu_prep(struct omap_bo *bo, enum omap_gem_op op)
321 {
322         struct drm_omap_gem_cpu_prep req = {
323                         .handle = bo->handle,
324                         .op = op,
325         };
326         return drmCommandWrite(bo->dev->fd,
327                         DRM_OMAP_GEM_CPU_PREP, &req, sizeof(req));
328 }
329
330 int omap_bo_cpu_fini(struct omap_bo *bo, enum omap_gem_op op)
331 {
332         struct drm_omap_gem_cpu_fini req = {
333                         .handle = bo->handle,
334                         .op = op,
335                         .nregions = 0,
336         };
337         return drmCommandWrite(bo->dev->fd,
338                         DRM_OMAP_GEM_CPU_FINI, &req, sizeof(req));
339 }