1d37e451d903c1c8176de6693400edc2bcfe4ba1
[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 #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 /* import a buffer from dmabuf fd, does not take ownership of the
222  * fd so caller should close() the fd when it is otherwise done
223  * with it (even if it is still using the 'struct omap_bo *')
224  */
225 struct omap_bo * omap_bo_from_dmabuf(struct omap_device *dev, int fd)
226 {
227         struct omap_bo *bo;
228         struct drm_prime_handle req = {
229                         .fd = fd,
230         };
231         int ret;
232
233         bo = calloc(sizeof(*bo), 1);
234         if (!bo) {
235                 goto fail;
236         }
237
238         ret = drmIoctl(dev->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &req);
239         if (ret) {
240                 goto fail;
241         }
242
243         bo->dev = dev;
244         bo->handle = req.handle;
245
246         return bo;
247
248 fail:
249         free(bo);
250         return NULL;
251 }
252
253 /* destroy a buffer object */
254 void omap_bo_del(struct omap_bo *bo)
255 {
256         if (!bo) {
257                 return;
258         }
259
260         if (bo->map) {
261                 munmap(bo->map, bo->size);
262         }
263
264         if (bo->fd) {
265                 close(bo->fd);
266         }
267
268         if (bo->handle) {
269                 struct drm_gem_close req = {
270                                 .handle = bo->handle,
271                 };
272
273                 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
274         }
275
276         free(bo);
277 }
278
279 /* get the global flink/DRI2 buffer name */
280 int omap_bo_get_name(struct omap_bo *bo, uint32_t *name)
281 {
282         if (!bo->name) {
283                 struct drm_gem_flink req = {
284                                 .handle = bo->handle,
285                 };
286                 int ret;
287
288                 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
289                 if (ret) {
290                         return ret;
291                 }
292
293                 bo->name = req.name;
294         }
295
296         *name = bo->name;
297
298         return 0;
299 }
300
301 uint32_t omap_bo_handle(struct omap_bo *bo)
302 {
303         return bo->handle;
304 }
305
306 /* caller owns the dmabuf fd that is returned and is responsible
307  * to close() it when done
308  */
309 int omap_bo_dmabuf(struct omap_bo *bo)
310 {
311         if (!bo->fd) {
312                 struct drm_prime_handle req = {
313                                 .handle = bo->handle,
314                                 .flags = DRM_CLOEXEC,
315                 };
316                 int ret;
317
318                 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &req);
319                 if (ret) {
320                         return ret;
321                 }
322
323                 bo->fd = req.fd;
324         }
325         return dup(bo->fd);
326 }
327
328 uint32_t omap_bo_size(struct omap_bo *bo)
329 {
330         if (!bo->size) {
331                 get_buffer_info(bo);
332         }
333         return bo->size;
334 }
335
336 void * omap_bo_map(struct omap_bo *bo)
337 {
338         if (!bo->map) {
339                 if (!bo->offset) {
340                         get_buffer_info(bo);
341                 }
342
343                 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
344                                 MAP_SHARED, bo->dev->fd, bo->offset);
345                 if (bo->map == MAP_FAILED) {
346                         bo->map = NULL;
347                 }
348         }
349         return bo->map;
350 }
351
352 int omap_bo_cpu_prep(struct omap_bo *bo, enum omap_gem_op op)
353 {
354         struct drm_omap_gem_cpu_prep req = {
355                         .handle = bo->handle,
356                         .op = op,
357         };
358         return drmCommandWrite(bo->dev->fd,
359                         DRM_OMAP_GEM_CPU_PREP, &req, sizeof(req));
360 }
361
362 int omap_bo_cpu_fini(struct omap_bo *bo, enum omap_gem_op op)
363 {
364         struct drm_omap_gem_cpu_fini req = {
365                         .handle = bo->handle,
366                         .op = op,
367                         .nregions = 0,
368         };
369         return drmCommandWrite(bo->dev->fd,
370                         DRM_OMAP_GEM_CPU_FINI, &req, sizeof(req));
371 }