1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
/*
* Copyright (C) 2012 Samsung Electronics Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Authors:
* Inki Dae <inki.dae@samsung.com>
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/mman.h>
#include <linux/stddef.h>
#include <xf86drm.h>
#include "libdrm_macros.h"
#include "exynos_drm.h"
#include "exynos_drmif.h"
#define U642VOID(x) ((void *)(unsigned long)(x))
/*
* Create exynos drm device object.
*
* @fd: file descriptor to exynos drm driver opened.
*
* if true, return the device object else NULL.
*/
drm_public struct exynos_device * exynos_device_create(int fd)
{
struct exynos_device *dev;
dev = calloc(sizeof(*dev), 1);
if (!dev) {
fprintf(stderr, "failed to create device[%s].\n",
strerror(errno));
return NULL;
}
dev->fd = fd;
return dev;
}
/*
* Destroy exynos drm device object
*
* @dev: exynos drm device object.
*/
drm_public void exynos_device_destroy(struct exynos_device *dev)
{
free(dev);
}
/*
* Create a exynos buffer object to exynos drm device.
*
* @dev: exynos drm device object.
* @size: user-desired size.
* flags: user-desired memory type.
* user can set one or more types among several types to memory
* allocation and cache attribute types. and as default,
* EXYNOS_BO_NONCONTIG and EXYNOS-BO_NONCACHABLE types would
* be used.
*
* if true, return a exynos buffer object else NULL.
*/
drm_public struct exynos_bo * exynos_bo_create(struct exynos_device *dev,
size_t size, uint32_t flags)
{
struct exynos_bo *bo;
struct drm_exynos_gem_create req = {
.size = size,
.flags = flags,
};
if (size == 0) {
fprintf(stderr, "invalid size.\n");
goto fail;
}
bo = calloc(sizeof(*bo), 1);
if (!bo) {
fprintf(stderr, "failed to create bo[%s].\n",
strerror(errno));
goto err_free_bo;
}
bo->dev = dev;
if (drmIoctl(dev->fd, DRM_IOCTL_EXYNOS_GEM_CREATE, &req)){
fprintf(stderr, "failed to create gem object[%s].\n",
strerror(errno));
goto err_free_bo;
}
bo->handle = req.handle;
bo->size = size;
bo->flags = flags;
return bo;
err_free_bo:
free(bo);
fail:
return NULL;
}
/*
* Get information to gem region allocated.
*
* @dev: exynos drm device object.
* @handle: gem handle to request gem info.
* @size: size to gem object and returned by kernel side.
* @flags: gem flags to gem object and returned by kernel side.
*
* with this function call, you can get flags and size to gem handle
* through bo object.
*
* if true, return 0 else negative.
*/
drm_public int exynos_bo_get_info(struct exynos_device *dev, uint32_t handle,
size_t *size, uint32_t *flags)
{
int ret;
struct drm_exynos_gem_info req = {
.handle = handle,
};
ret = drmIoctl(dev->fd, DRM_IOCTL_EXYNOS_GEM_GET, &req);
if (ret < 0) {
fprintf(stderr, "failed to get gem object information[%s].\n",
strerror(errno));
return ret;
}
*size = req.size;
*flags = req.flags;
return 0;
}
/*
* Destroy a exynos buffer object.
*
* @bo: a exynos buffer object to be destroyed.
*/
drm_public void exynos_bo_destroy(struct exynos_bo *bo)
{
if (!bo)
return;
if (bo->vaddr)
munmap(bo->vaddr, bo->size);
if (bo->handle) {
struct drm_gem_close req = {
.handle = bo->handle,
};
drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
}
free(bo);
}
/*
* Get a exynos buffer object from a gem global object name.
*
* @dev: a exynos device object.
* @name: a gem global object name exported by another process.
*
* this interface is used to get a exynos buffer object from a gem
* global object name sent by another process for buffer sharing.
*
* if true, return a exynos buffer object else NULL.
*
*/
drm_public struct exynos_bo *
exynos_bo_from_name(struct exynos_device *dev, uint32_t name)
{
struct exynos_bo *bo;
struct drm_gem_open req = {
.name = name,
};
bo = calloc(sizeof(*bo), 1);
if (!bo) {
fprintf(stderr, "failed to allocate bo[%s].\n",
strerror(errno));
return NULL;
}
if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
fprintf(stderr, "failed to open gem object[%s].\n",
strerror(errno));
goto err_free_bo;
}
bo->dev = dev;
bo->name = name;
bo->handle = req.handle;
return bo;
err_free_bo:
free(bo);
return NULL;
}
/*
* Get a gem global object name from a gem object handle.
*
* @bo: a exynos buffer object including gem handle.
* @name: a gem global object name to be got by kernel driver.
*
* this interface is used to get a gem global object name from a gem object
* handle to a buffer that wants to share it with another process.
*
* if true, return 0 else negative.
*/
drm_public int exynos_bo_get_name(struct exynos_bo *bo, uint32_t *name)
{
if (!bo->name) {
struct drm_gem_flink req = {
.handle = bo->handle,
};
int ret;
ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
if (ret) {
fprintf(stderr, "failed to get gem global name[%s].\n",
strerror(errno));
return ret;
}
bo->name = req.name;
}
*name = bo->name;
return 0;
}
drm_public uint32_t exynos_bo_handle(struct exynos_bo *bo)
{
return bo->handle;
}
/*
* Mmap a buffer to user space.
*
* @bo: a exynos buffer object including a gem object handle to be mmapped
* to user space.
*
* if true, user pointer mmapped else NULL.
*/
drm_public void *exynos_bo_map(struct exynos_bo *bo)
{
if (!bo->vaddr) {
struct exynos_device *dev = bo->dev;
struct drm_exynos_gem_map arg;
void *map = NULL;
int ret;
memset(&arg, 0, sizeof(arg));
arg.handle = bo->handle;
ret = drmIoctl(dev->fd, DRM_IOCTL_EXYNOS_GEM_MAP, &arg);
if (ret) {
fprintf(stderr, "failed to map dumb buffer[%s].\n",
strerror(errno));
return NULL;
}
map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
dev->fd, arg.offset);
if (map != MAP_FAILED)
bo->vaddr = map;
}
return bo->vaddr;
}
/*
* Export gem object to dmabuf as file descriptor.
*
* @dev: exynos device object
* @handle: gem handle to export as file descriptor of dmabuf
* @fd: file descriptor returned from kernel
*
* @return: 0 on success, -1 on error, and errno will be set
*/
drm_public int
exynos_prime_handle_to_fd(struct exynos_device *dev, uint32_t handle, int *fd)
{
return drmPrimeHandleToFD(dev->fd, handle, 0, fd);
}
/*
* Import file descriptor into gem handle.
*
* @dev: exynos device object
* @fd: file descriptor of dmabuf to import
* @handle: gem handle returned from kernel
*
* @return: 0 on success, -1 on error, and errno will be set
*/
drm_public int
exynos_prime_fd_to_handle(struct exynos_device *dev, int fd, uint32_t *handle)
{
return drmPrimeFDToHandle(dev->fd, fd, handle);
}
/*
* Request Wireless Display connection or disconnection.
*
* @dev: a exynos device object.
* @connect: indicate whether connectoin or disconnection request.
* @ext: indicate whether edid data includes extensions data or not.
* @edid: a pointer to edid data from Wireless Display device.
*
* this interface is used to request Virtual Display driver connection or
* disconnection. for this, user should get a edid data from the Wireless
* Display device and then send that data to kernel driver with connection
* request
*
* if true, return 0 else negative.
*/
drm_public int
exynos_vidi_connection(struct exynos_device *dev, uint32_t connect,
uint32_t ext, void *edid)
{
struct drm_exynos_vidi_connection req = {
.connection = connect,
.extensions = ext,
.edid = (uint64_t)(uintptr_t)edid,
};
int ret;
ret = drmIoctl(dev->fd, DRM_IOCTL_EXYNOS_VIDI_CONNECTION, &req);
if (ret) {
fprintf(stderr, "failed to request vidi connection[%s].\n",
strerror(errno));
return ret;
}
return 0;
}
static void
exynos_handle_vendor(int fd, struct drm_event *e, void *ctx)
{
struct drm_exynos_g2d_event *g2d;
struct exynos_event_context *ectx = ctx;
switch (e->type) {
case DRM_EXYNOS_G2D_EVENT:
if (ectx->version < 1 || ectx->g2d_event_handler == NULL)
break;
g2d = (struct drm_exynos_g2d_event *)e;
ectx->g2d_event_handler(fd, g2d->cmdlist_no, g2d->tv_sec,
g2d->tv_usec, U642VOID(g2d->user_data));
break;
default:
break;
}
}
drm_public int
exynos_handle_event(struct exynos_device *dev, struct exynos_event_context *ctx)
{
char buffer[1024];
int len, i;
struct drm_event *e;
struct drm_event_vblank *vblank;
drmEventContextPtr evctx = &ctx->base;
/* The DRM read semantics guarantees that we always get only
* complete events. */
len = read(dev->fd, buffer, sizeof buffer);
if (len == 0)
return 0;
if (len < (int)sizeof *e)
return -1;
i = 0;
while (i < len) {
e = (struct drm_event *)(buffer + i);
switch (e->type) {
case DRM_EVENT_VBLANK:
if (evctx->version < 1 ||
evctx->vblank_handler == NULL)
break;
vblank = (struct drm_event_vblank *) e;
evctx->vblank_handler(dev->fd,
vblank->sequence,
vblank->tv_sec,
vblank->tv_usec,
U642VOID (vblank->user_data));
break;
case DRM_EVENT_FLIP_COMPLETE:
if (evctx->version < 2 ||
evctx->page_flip_handler == NULL)
break;
vblank = (struct drm_event_vblank *) e;
evctx->page_flip_handler(dev->fd,
vblank->sequence,
vblank->tv_sec,
vblank->tv_usec,
U642VOID (vblank->user_data));
break;
default:
exynos_handle_vendor(dev->fd, e, evctx);
break;
}
i += e->length;
}
return 0;
}
|