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