2 * Copyright (C) 2012 Samsung Electronics Co., Ltd.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Inki Dae <inki.dae@samsung.com>
38 #include <linux/stddef.h>
42 #include "libdrm_macros.h"
43 #include "exynos_drm.h"
44 #include "exynos_drmif.h"
46 #define U642VOID(x) ((void *)(unsigned long)(x))
49 * Create exynos drm device object.
51 * @fd: file descriptor to exynos drm driver opened.
53 * if true, return the device object else NULL.
55 struct exynos_device * exynos_device_create(int fd)
57 struct exynos_device *dev;
59 dev = calloc(sizeof(*dev), 1);
61 fprintf(stderr, "failed to create device[%s].\n",
72 * Destroy exynos drm device object
74 * @dev: exynos drm device object.
76 void exynos_device_destroy(struct exynos_device *dev)
82 * Create a exynos buffer object to exynos drm device.
84 * @dev: exynos drm device object.
85 * @size: user-desired size.
86 * flags: user-desired memory type.
87 * user can set one or more types among several types to memory
88 * allocation and cache attribute types. and as default,
89 * EXYNOS_BO_NONCONTIG and EXYNOS-BO_NONCACHABLE types would
92 * if true, return a exynos buffer object else NULL.
94 struct exynos_bo * exynos_bo_create(struct exynos_device *dev,
95 size_t size, uint32_t flags)
98 struct drm_exynos_gem_create req = {
104 fprintf(stderr, "invalid size.\n");
108 bo = calloc(sizeof(*bo), 1);
110 fprintf(stderr, "failed to create bo[%s].\n",
117 if (drmIoctl(dev->fd, DRM_IOCTL_EXYNOS_GEM_CREATE, &req)){
118 fprintf(stderr, "failed to create gem object[%s].\n",
123 bo->handle = req.handle;
136 * Get information to gem region allocated.
138 * @dev: exynos drm device object.
139 * @handle: gem handle to request gem info.
140 * @size: size to gem object and returned by kernel side.
141 * @flags: gem flags to gem object and returned by kernel side.
143 * with this function call, you can get flags and size to gem handle
146 * if true, return 0 else negative.
148 int exynos_bo_get_info(struct exynos_device *dev, uint32_t handle,
149 size_t *size, uint32_t *flags)
152 struct drm_exynos_gem_info req = {
156 ret = drmIoctl(dev->fd, DRM_IOCTL_EXYNOS_GEM_GET, &req);
158 fprintf(stderr, "failed to get gem object information[%s].\n",
170 * Destroy a exynos buffer object.
172 * @bo: a exynos buffer object to be destroyed.
174 void exynos_bo_destroy(struct exynos_bo *bo)
180 munmap(bo->vaddr, bo->size);
183 struct drm_gem_close req = {
184 .handle = bo->handle,
187 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
195 * Get a exynos buffer object from a gem global object name.
197 * @dev: a exynos device object.
198 * @name: a gem global object name exported by another process.
200 * this interface is used to get a exynos buffer object from a gem
201 * global object name sent by another process for buffer sharing.
203 * if true, return a exynos buffer object else NULL.
207 exynos_bo_from_name(struct exynos_device *dev, uint32_t name)
209 struct exynos_bo *bo;
210 struct drm_gem_open req = {
214 bo = calloc(sizeof(*bo), 1);
216 fprintf(stderr, "failed to allocate bo[%s].\n",
221 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
222 fprintf(stderr, "failed to open gem object[%s].\n",
229 bo->handle = req.handle;
239 * Get a gem global object name from a gem object handle.
241 * @bo: a exynos buffer object including gem handle.
242 * @name: a gem global object name to be got by kernel driver.
244 * this interface is used to get a gem global object name from a gem object
245 * handle to a buffer that wants to share it with another process.
247 * if true, return 0 else negative.
249 int exynos_bo_get_name(struct exynos_bo *bo, uint32_t *name)
252 struct drm_gem_flink req = {
253 .handle = bo->handle,
257 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
259 fprintf(stderr, "failed to get gem global name[%s].\n",
272 uint32_t exynos_bo_handle(struct exynos_bo *bo)
278 * Mmap a buffer to user space.
280 * @bo: a exynos buffer object including a gem object handle to be mmapped
283 * if true, user pointer mmaped else NULL.
285 void *exynos_bo_map(struct exynos_bo *bo)
288 struct exynos_device *dev = bo->dev;
289 struct drm_mode_map_dumb arg;
293 memset(&arg, 0, sizeof(arg));
294 arg.handle = bo->handle;
296 ret = drmIoctl(dev->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
298 fprintf(stderr, "failed to map dumb buffer[%s].\n",
303 map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
304 dev->fd, arg.offset);
306 if (map != MAP_FAILED)
314 * Export gem object to dmabuf as file descriptor.
316 * @dev: exynos device object
317 * @handle: gem handle to export as file descriptor of dmabuf
318 * @fd: file descriptor returned from kernel
320 * @return: 0 on success, -1 on error, and errno will be set
323 exynos_prime_handle_to_fd(struct exynos_device *dev, uint32_t handle, int *fd)
325 return drmPrimeHandleToFD(dev->fd, handle, 0, fd);
329 * Import file descriptor into gem handle.
331 * @dev: exynos device object
332 * @fd: file descriptor of dmabuf to import
333 * @handle: gem handle returned from kernel
335 * @return: 0 on success, -1 on error, and errno will be set
338 exynos_prime_fd_to_handle(struct exynos_device *dev, int fd, uint32_t *handle)
340 return drmPrimeFDToHandle(dev->fd, fd, handle);
346 * Request Wireless Display connection or disconnection.
348 * @dev: a exynos device object.
349 * @connect: indicate whether connectoin or disconnection request.
350 * @ext: indicate whether edid data includes extensions data or not.
351 * @edid: a pointer to edid data from Wireless Display device.
353 * this interface is used to request Virtual Display driver connection or
354 * disconnection. for this, user should get a edid data from the Wireless
355 * Display device and then send that data to kernel driver with connection
358 * if true, return 0 else negative.
361 exynos_vidi_connection(struct exynos_device *dev, uint32_t connect,
362 uint32_t ext, void *edid)
364 struct drm_exynos_vidi_connection req = {
365 .connection = connect,
367 .edid = (uint64_t)(uintptr_t)edid,
371 ret = drmIoctl(dev->fd, DRM_IOCTL_EXYNOS_VIDI_CONNECTION, &req);
373 fprintf(stderr, "failed to request vidi connection[%s].\n",
382 exynos_handle_vendor(int fd, struct drm_event *e, void *ctx)
384 struct drm_exynos_g2d_event *g2d;
385 struct exynos_event_context *ectx = ctx;
388 case DRM_EXYNOS_G2D_EVENT:
389 if (ectx->version < 1 || ectx->g2d_event_handler == NULL)
391 g2d = (struct drm_exynos_g2d_event *)e;
392 ectx->g2d_event_handler(fd, g2d->cmdlist_no, g2d->tv_sec,
393 g2d->tv_usec, U642VOID(g2d->user_data));
402 exynos_handle_event(struct exynos_device *dev, struct exynos_event_context *ctx)
407 struct drm_event_vblank *vblank;
408 drmEventContextPtr evctx = &ctx->base;
410 /* The DRM read semantics guarantees that we always get only
411 * complete events. */
412 len = read(dev->fd, buffer, sizeof buffer);
415 if (len < (int)sizeof *e)
420 e = (struct drm_event *)(buffer + i);
422 case DRM_EVENT_VBLANK:
423 if (evctx->version < 1 ||
424 evctx->vblank_handler == NULL)
426 vblank = (struct drm_event_vblank *) e;
427 evctx->vblank_handler(dev->fd,
431 U642VOID (vblank->user_data));
433 case DRM_EVENT_FLIP_COMPLETE:
434 if (evctx->version < 2 ||
435 evctx->page_flip_handler == NULL)
437 vblank = (struct drm_event_vblank *) e;
438 evctx->page_flip_handler(dev->fd,
442 U642VOID (vblank->user_data));
445 exynos_handle_vendor(dev->fd, e, evctx);