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>
37 #include <linux/stddef.h>
41 #include "libdrm_macros.h"
42 #include "exynos_drm.h"
43 #include "exynos_drmif.h"
46 * Create exynos drm device object.
48 * @fd: file descriptor to exynos drm driver opened.
50 * if true, return the device object else NULL.
52 struct exynos_device * exynos_device_create(int fd)
54 struct exynos_device *dev;
56 dev = calloc(sizeof(*dev), 1);
58 fprintf(stderr, "failed to create device[%s].\n",
69 * Destroy exynos drm device object
71 * @dev: exynos drm device object.
73 void exynos_device_destroy(struct exynos_device *dev)
79 * Create a exynos buffer object to exynos drm device.
81 * @dev: exynos 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 * EXYNOS_BO_NONCONTIG and EXYNOS-BO_NONCACHABLE types would
89 * if true, return a exynos buffer object else NULL.
91 struct exynos_bo * exynos_bo_create(struct exynos_device *dev,
92 size_t size, uint32_t flags)
95 struct drm_exynos_gem_create req = {
101 fprintf(stderr, "invalid size.\n");
105 bo = calloc(sizeof(*bo), 1);
107 fprintf(stderr, "failed to create bo[%s].\n",
114 if (drmIoctl(dev->fd, DRM_IOCTL_EXYNOS_GEM_CREATE, &req)){
115 fprintf(stderr, "failed to create gem object[%s].\n",
120 bo->handle = req.handle;
133 * Get information to gem region allocated.
135 * @dev: exynos drm device object.
136 * @handle: gem handle to request gem info.
137 * @size: size to gem object and returned by kernel side.
138 * @flags: gem flags to gem object and returned by kernel side.
140 * with this function call, you can get flags and size to gem handle
143 * if true, return 0 else negative.
145 int exynos_bo_get_info(struct exynos_device *dev, uint32_t handle,
146 size_t *size, uint32_t *flags)
149 struct drm_exynos_gem_info req = {
153 ret = drmIoctl(dev->fd, DRM_IOCTL_EXYNOS_GEM_GET, &req);
155 fprintf(stderr, "failed to get gem object information[%s].\n",
167 * Destroy a exynos buffer object.
169 * @bo: a exynos buffer object to be destroyed.
171 void exynos_bo_destroy(struct exynos_bo *bo)
177 munmap(bo->vaddr, bo->size);
180 struct drm_gem_close req = {
181 .handle = bo->handle,
184 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
192 * Get a exynos buffer object from a gem global object name.
194 * @dev: a exynos device object.
195 * @name: a gem global object name exported by another process.
197 * this interface is used to get a exynos buffer object from a gem
198 * global object name sent by another process for buffer sharing.
200 * if true, return a exynos buffer object else NULL.
204 exynos_bo_from_name(struct exynos_device *dev, uint32_t name)
206 struct exynos_bo *bo;
207 struct drm_gem_open req = {
211 bo = calloc(sizeof(*bo), 1);
213 fprintf(stderr, "failed to allocate bo[%s].\n",
218 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
219 fprintf(stderr, "failed to open gem object[%s].\n",
226 bo->handle = req.handle;
236 * Get a gem global object name from a gem object handle.
238 * @bo: a exynos buffer object including gem handle.
239 * @name: a gem global object name to be got by kernel driver.
241 * this interface is used to get a gem global object name from a gem object
242 * handle to a buffer that wants to share it with another process.
244 * if true, return 0 else negative.
246 int exynos_bo_get_name(struct exynos_bo *bo, uint32_t *name)
249 struct drm_gem_flink req = {
250 .handle = bo->handle,
254 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
256 fprintf(stderr, "failed to get gem global name[%s].\n",
269 uint32_t exynos_bo_handle(struct exynos_bo *bo)
275 * Mmap a buffer to user space.
277 * @bo: a exynos buffer object including a gem object handle to be mmapped
280 * if true, user pointer mmaped else NULL.
282 void *exynos_bo_map(struct exynos_bo *bo)
285 struct exynos_device *dev = bo->dev;
286 struct drm_mode_map_dumb arg;
290 memset(&arg, 0, sizeof(arg));
291 arg.handle = bo->handle;
293 ret = drmIoctl(dev->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
295 fprintf(stderr, "failed to map dumb buffer[%s].\n",
300 map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
301 dev->fd, arg.offset);
303 if (map != MAP_FAILED)
311 * Export gem object to dmabuf as file descriptor.
313 * @dev: exynos device object
314 * @handle: gem handle to export as file descriptor of dmabuf
315 * @fd: file descriptor returned from kernel
317 * @return: 0 on success, -1 on error, and errno will be set
320 exynos_prime_handle_to_fd(struct exynos_device *dev, uint32_t handle, int *fd)
322 return drmPrimeHandleToFD(dev->fd, handle, 0, fd);
326 * Import file descriptor into gem handle.
328 * @dev: exynos device object
329 * @fd: file descriptor of dmabuf to import
330 * @handle: gem handle returned from kernel
332 * @return: 0 on success, -1 on error, and errno will be set
335 exynos_prime_fd_to_handle(struct exynos_device *dev, int fd, uint32_t *handle)
337 return drmPrimeFDToHandle(dev->fd, fd, handle);
343 * Request Wireless Display connection or disconnection.
345 * @dev: a exynos device object.
346 * @connect: indicate whether connectoin or disconnection request.
347 * @ext: indicate whether edid data includes extentions data or not.
348 * @edid: a pointer to edid data from Wireless Display device.
350 * this interface is used to request Virtual Display driver connection or
351 * disconnection. for this, user should get a edid data from the Wireless
352 * Display device and then send that data to kernel driver with connection
355 * if true, return 0 else negative.
358 exynos_vidi_connection(struct exynos_device *dev, uint32_t connect,
359 uint32_t ext, void *edid)
361 struct drm_exynos_vidi_connection req = {
362 .connection = connect,
364 .edid = (uint64_t)(uintptr_t)edid,
368 ret = drmIoctl(dev->fd, DRM_IOCTL_EXYNOS_VIDI_CONNECTION, &req);
370 fprintf(stderr, "failed to request vidi connection[%s].\n",