aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoryzq2014-10-08 01:38:23 -0500
committerTomasz Figa2015-06-04 23:19:54 -0500
commitbd00c5c41dab43ab3bd2d7f505df0e8ea3b834e8 (patch)
treec982796f27a0eb472c6da2526d6cd48890938662 /rockchip
parent67597854f92b8884cde237a9dfd9691cfa68253f (diff)
downloadexternal-libdrm-bd00c5c41dab43ab3bd2d7f505df0e8ea3b834e8.tar.gz
external-libdrm-bd00c5c41dab43ab3bd2d7f505df0e8ea3b834e8.tar.xz
external-libdrm-bd00c5c41dab43ab3bd2d7f505df0e8ea3b834e8.zip
rockchip: Add rockchip drm API
This patch adds rockchip libdrm API to provide helpers for BO management on systems using Rockchip DRM. Change-Id: I2abe6434a1aad8ecf63930119b1a2adf3181d9fe Signed-off-by: Tomasz Figa <tfiga@google.com>
Diffstat (limited to 'rockchip')
-rw-r--r--rockchip/Makefile.am20
-rw-r--r--rockchip/libdrm_rockchip.pc.in11
-rw-r--r--rockchip/rockchip_drm.c274
-rw-r--r--rockchip/rockchip_drm.h73
-rw-r--r--rockchip/rockchip_drmif.h77
5 files changed, 455 insertions, 0 deletions
diff --git a/rockchip/Makefile.am b/rockchip/Makefile.am
new file mode 100644
index 00000000..2ebb82f1
--- /dev/null
+++ b/rockchip/Makefile.am
@@ -0,0 +1,20 @@
1AM_CFLAGS = \
2 $(WARN_CFLAGS) \
3 -I$(top_srcdir) \
4 -I$(top_srcdir)/rockchip \
5 $(PTHREADSTUBS_CFLAGS) \
6 -I$(top_srcdir)/include/drm
7
8libdrm_rockchip_la_LTLIBRARIES = libdrm_rockchip.la
9libdrm_rockchip_ladir = $(libdir)
10libdrm_rockchip_la_LDFLAGS = -version-number 1:0:0 -no-undefined
11libdrm_rockchip_la_LIBADD = ../libdrm.la @PTHREADSTUBS_LIBS@
12
13libdrm_rockchip_la_SOURCES = \
14 rockchip_drm.c
15
16libdrm_rockchipincludedir = ${includedir}/libdrm
17libdrm_rockchipinclude_HEADERS = rockchip_drmif.h rockchip_drm.h
18
19pkgconfigdir = @pkgconfigdir@
20pkgconfig_DATA = libdrm_rockchip.pc
diff --git a/rockchip/libdrm_rockchip.pc.in b/rockchip/libdrm_rockchip.pc.in
new file mode 100644
index 00000000..13f22ac7
--- /dev/null
+++ b/rockchip/libdrm_rockchip.pc.in
@@ -0,0 +1,11 @@
1prefix=@prefix@
2exec_prefix=@exec_prefix@
3libdir=@libdir@
4includedir=@includedir@
5
6Name: libdrm_rockchip
7Description: Userspace interface to rockchip kernel DRM services
8Version: 0.1
9Libs: -L${libdir} -ldrm_rockchip
10Cflags: -I${includedir} -I${includedir}/libdrm
11Requires.private: libdrm
diff --git a/rockchip/rockchip_drm.c b/rockchip/rockchip_drm.c
new file mode 100644
index 00000000..d56a68b4
--- /dev/null
+++ b/rockchip/rockchip_drm.c
@@ -0,0 +1,274 @@
1/*
2 * Copyright (C) ROCKCHIP, Inc.
3 * Author:yzq<yzq@rock-chips.com>
4 *
5 * based on exynos_drm.c
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 *
26 */
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#include <stdlib.h>
33#include <stdio.h>
34#include <string.h>
35#include <errno.h>
36
37#include <sys/mman.h>
38#include <linux/stddef.h>
39
40#include <xf86drm.h>
41
42#include "rockchip_drm.h"
43#include "rockchip_drmif.h"
44
45/*
46 * Create rockchip drm device object.
47 *
48 * @fd: file descriptor to rockchip drm driver opened.
49 *
50 * if true, return the device object else NULL.
51 */
52struct rockchip_device *rockchip_device_create(int fd)
53{
54 struct rockchip_device *dev;
55
56 dev = calloc(1, sizeof(*dev));
57 if (!dev) {
58 fprintf(stderr, "failed to create device[%s].\n",
59 strerror(errno));
60 return NULL;
61 }
62
63 dev->fd = fd;
64
65 return dev;
66}
67
68/*
69 * Destroy rockchip drm device object
70 *
71 * @dev: rockchip drm device object.
72 */
73void rockchip_device_destroy(struct rockchip_device *dev)
74{
75 free(dev);
76}
77
78/*
79 * Create a rockchip buffer object to rockchip drm device.
80 *
81 * @dev: rockchip drm device object.
82 * @size: user-desired size.
83 * flags: user-desired memory type.
84 * user can set one or more types among several types to memory
85 * allocation and cache attribute types. and as default,
86 * ROCKCHIP_BO_NONCONTIG and ROCKCHIP-BO_NONCACHABLE types would
87 * be used.
88 *
89 * if true, return a rockchip buffer object else NULL.
90 */
91struct rockchip_bo *rockchip_bo_create(struct rockchip_device *dev,
92 size_t size, uint32_t flags)
93{
94 struct rockchip_bo *bo;
95 struct drm_rockchip_gem_create req = {
96 .size = size,
97 .flags = flags,
98 };
99
100 if (size == 0) {
101 fprintf(stderr, "invalid size.\n");
102 return NULL;
103 }
104
105 bo = calloc(1, sizeof(*bo));
106 if (!bo) {
107 fprintf(stderr, "failed to create bo[%s].\n",
108 strerror(errno));
109 goto fail;
110 }
111
112 bo->dev = dev;
113
114 if (drmIoctl(dev->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE, &req)){
115 fprintf(stderr, "failed to create gem object[%s].\n",
116 strerror(errno));
117 goto err_free_bo;
118 }
119
120 bo->handle = req.handle;
121 bo->size = size;
122 bo->flags = flags;
123
124 return bo;
125
126err_free_bo:
127 free(bo);
128fail:
129 return NULL;
130}
131
132/*
133 * Destroy a rockchip buffer object.
134 *
135 * @bo: a rockchip buffer object to be destroyed.
136 */
137void rockchip_bo_destroy(struct rockchip_bo *bo)
138{
139 if (!bo)
140 return;
141
142 if (bo->vaddr)
143 munmap(bo->vaddr, bo->size);
144
145 if (bo->handle) {
146 struct drm_gem_close req = {
147 .handle = bo->handle,
148 };
149
150 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
151 }
152
153 free(bo);
154}
155
156
157/*
158 * Get a rockchip buffer object from a gem global object name.
159 *
160 * @dev: a rockchip device object.
161 * @name: a gem global object name exported by another process.
162 *
163 * this interface is used to get a rockchip buffer object from a gem
164 * global object name sent by another process for buffer sharing.
165 *
166 * if true, return a rockchip buffer object else NULL.
167 *
168 */
169struct rockchip_bo *rockchip_bo_from_name(struct rockchip_device *dev,
170 uint32_t name)
171{
172 struct rockchip_bo *bo;
173 struct drm_gem_open req = {
174 .name = name,
175 };
176
177 bo = calloc(1, sizeof(*bo));
178 if (!bo) {
179 fprintf(stderr, "failed to allocate bo[%s].\n",
180 strerror(errno));
181 return NULL;
182 }
183
184 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
185 fprintf(stderr, "failed to open gem object[%s].\n",
186 strerror(errno));
187 goto err_free_bo;
188 }
189
190 bo->dev = dev;
191 bo->name = name;
192 bo->handle = req.handle;
193
194 return bo;
195
196err_free_bo:
197 free(bo);
198 return NULL;
199}
200
201/*
202 * Get a gem global object name from a gem object handle.
203 *
204 * @bo: a rockchip buffer object including gem handle.
205 * @name: a gem global object name to be got by kernel driver.
206 *
207 * this interface is used to get a gem global object name from a gem object
208 * handle to a buffer that wants to share it with another process.
209 *
210 * if true, return 0 else negative.
211 */
212int rockchip_bo_get_name(struct rockchip_bo *bo, uint32_t *name)
213{
214 if (!bo->name) {
215 struct drm_gem_flink req = {
216 .handle = bo->handle,
217 };
218 int ret;
219
220 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
221 if (ret) {
222 fprintf(stderr, "failed to get gem global name[%s].\n",
223 strerror(errno));
224 return ret;
225 }
226
227 bo->name = req.name;
228 }
229
230 *name = bo->name;
231
232 return 0;
233}
234
235uint32_t rockchip_bo_handle(struct rockchip_bo *bo)
236{
237 return bo->handle;
238}
239
240/*
241 * Mmap a buffer to user space.
242 *
243 * @bo: a rockchip buffer object including a gem object handle to be mmapped
244 * to user space.
245 *
246 * if true, user pointer mmaped else NULL.
247 */
248void *rockchip_bo_map(struct rockchip_bo *bo)
249{
250 if (!bo->vaddr) {
251 struct rockchip_device *dev = bo->dev;
252 struct drm_rockchip_gem_map_off req = {
253 .handle = bo->handle,
254 };
255 int ret;
256
257 ret = drmIoctl(dev->fd, DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET, &req);
258 if (ret) {
259 fprintf(stderr, "failed to ioctl gem map offset[%s].\n",
260 strerror(errno));
261 return NULL;
262 }
263
264 bo->vaddr = mmap(0, bo->size, PROT_READ | PROT_WRITE,
265 MAP_SHARED, dev->fd, req.offset);
266 if (bo->vaddr == MAP_FAILED) {
267 fprintf(stderr, "failed to mmap buffer[%s].\n",
268 strerror(errno));
269 return NULL;
270 }
271 }
272
273 return bo->vaddr;
274}
diff --git a/rockchip/rockchip_drm.h b/rockchip/rockchip_drm.h
new file mode 100644
index 00000000..13977d59
--- /dev/null
+++ b/rockchip/rockchip_drm.h
@@ -0,0 +1,73 @@
1/*
2 * Copyright (c) Fuzhou Rockchip Electronics Co.Ltd
3 * Authors:
4 * Mark Yao <yzq@rock-chips.com>
5 *
6 * based on exynos_drm.h
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 *
27 */
28
29#ifndef _ROCKCHIP_DRM_H_
30#define _ROCKCHIP_DRM_H_
31
32#include <stdint.h>
33#include "drm.h"
34
35/**
36 * User-desired buffer creation information structure.
37 *
38 * @size: user-desired memory allocation size.
39 * - this size value would be page-aligned internally.
40 * @flags: user request for setting memory type or cache attributes.
41 * @handle: returned a handle to created gem object.
42 * - this handle will be set by gem module of kernel side.
43 */
44struct drm_rockchip_gem_create {
45 uint64_t size;
46 uint32_t flags;
47 uint32_t handle;
48};
49
50/**
51 * A structure for getting buffer offset.
52 *
53 * @handle: a pointer to gem object created.
54 * @pad: just padding to be 64-bit aligned.
55 * @offset: relatived offset value of the memory region allocated.
56 * - this value should be set by user.
57 */
58struct drm_rockchip_gem_map_off {
59 uint32_t handle;
60 uint32_t pad;
61 uint64_t offset;
62};
63
64#define DRM_ROCKCHIP_GEM_CREATE 0x00
65#define DRM_ROCKCHIP_GEM_MAP_OFFSET 0x01
66
67#define DRM_IOCTL_ROCKCHIP_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + \
68 DRM_ROCKCHIP_GEM_CREATE, struct drm_rockchip_gem_create)
69
70#define DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET DRM_IOWR(DRM_COMMAND_BASE + \
71 DRM_ROCKCHIP_GEM_MAP_OFFSET, struct drm_rockchip_gem_map_off)
72
73#endif
diff --git a/rockchip/rockchip_drmif.h b/rockchip/rockchip_drmif.h
new file mode 100644
index 00000000..bcc55539
--- /dev/null
+++ b/rockchip/rockchip_drmif.h
@@ -0,0 +1,77 @@
1/*
2 * Copyright (C) ROCKCHIP, Inc.
3 * Author:yzq<yzq@rock-chips.com>
4 *
5 * based on exynos_drmif.h
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 *
26 */
27
28#ifndef ROCKCHIP_DRMIF_H_
29#define ROCKCHIP_DRMIF_H_
30
31#include <xf86drm.h>
32#include <stdint.h>
33#include "rockchip_drm.h"
34
35struct rockchip_device {
36 int fd;
37};
38
39/*
40 * Rockchip Buffer Object structure.
41 *
42 * @dev: rockchip device object allocated.
43 * @handle: a gem handle to gem object created.
44 * @flags: indicate memory allocation and cache attribute types.
45 * @size: size to the buffer created.
46 * @vaddr: user space address to a gem buffer mmaped.
47 * @name: a gem global handle from flink request.
48 */
49struct rockchip_bo {
50 struct rockchip_device *dev;
51 uint32_t handle;
52 uint32_t flags;
53 size_t size;
54 void *vaddr;
55 uint32_t name;
56};
57
58/*
59 * device related functions:
60 */
61struct rockchip_device *rockchip_device_create(int fd);
62void rockchip_device_destroy(struct rockchip_device *dev);
63
64/*
65 * buffer-object related functions:
66 */
67struct rockchip_bo *rockchip_bo_create(struct rockchip_device *dev,
68 size_t size, uint32_t flags);
69int rockchip_bo_get_info(struct rockchip_device *dev, uint32_t handle,
70 size_t *size, uint32_t *flags);
71void rockchip_bo_destroy(struct rockchip_bo *bo);
72struct rockchip_bo *rockchip_bo_from_name(struct rockchip_device *dev,
73 uint32_t name);
74int rockchip_bo_get_name(struct rockchip_bo *bo, uint32_t *name);
75uint32_t rockchip_bo_handle(struct rockchip_bo *bo);
76void *rockchip_bo_map(struct rockchip_bo *bo);
77#endif /* ROCKCHIP_DRMIF_H_ */