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