2 * Copyright (C) 2016 Nexell Co., Ltd.
3 * Author: SeongO, Park <ray@nexell.co.kr>
17 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/mman.h> // PROT_READ/PROT_WRITE/MAP_SHARED/mmap/munmap
22 #include <nexell/nexell_drm.h>
23 #include <nx_video_alloc.h>
25 #include <libdrm/drm_fourcc.h>
26 #include <linux/videodev2.h>
27 #include <linux/videodev2_nxp_media.h>
28 #ifdef TIZEN_FEATURE_ARTIK530
29 #include <tbm_bufmgr.h>
31 #include "nx_video_log.h"
33 #define DRM_DEVICE_NAME "/dev/dri/renderD128"
35 #define DRM_IOCTL_NR(n) _IOC_NR(n)
36 #define DRM_IOC_VOID _IOC_NONE
37 #define DRM_IOC_READ _IOC_READ
38 #define DRM_IOC_WRITE _IOC_WRITE
39 #define DRM_IOC_READWRITE _IOC_READ|_IOC_WRITE
40 #define DRM_IOC(dir, group, nr, size) _IOC(dir, group, nr, size)
43 drm_ioctl (int32_t drm_fd, uint32_t request, void *arg)
48 ret = ioctl (drm_fd, request, arg);
49 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
54 drm_command_write_read (int fd, uint32_t command_index,
55 void *data, uint32_t size)
59 request = DRM_IOC (DRM_IOC_READ | DRM_IOC_WRITE, DRM_IOCTL_BASE,
60 DRM_COMMAND_BASE + command_index, size);
61 if (drm_ioctl (fd, request, data))
70 alloc_gem (int drm_fd, int size, int flags)
72 struct nx_drm_gem_create arg = { 0, };
75 arg.size = (uint32_t) size;
78 ret = drm_command_write_read (drm_fd, DRM_NX_GEM_CREATE, &arg, sizeof (arg));
80 _E ("drm_command_write_read(errno=%d, flags=%d)\n", -ret, flags);
83 //_D ("[DRM ALLOC] gem %d, size %d, flags 0x%x\n", arg.handle, size, flags);
89 free_gem (int drm_fd, int gem)
91 struct drm_gem_close arg = { 0, };
94 drm_ioctl (drm_fd, DRM_IOCTL_GEM_CLOSE, &arg);
101 gem_to_dmafd (int drm_fd, int gem_fd)
104 struct drm_prime_handle arg = { 0, };
107 arg.flags = O_CLOEXEC | O_RDWR;
108 ret = drm_ioctl (drm_fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg);
110 _E ("fail : get fd from gem:%d (DRM_IOCTL_PRIME_HANDLE_TO_FD)\n", ret);
117 get_flink_name (int fd, int gem)
119 struct drm_gem_flink arg = { 0, };
122 if (drm_ioctl (fd, DRM_IOCTL_GEM_FLINK, &arg)) {
123 _E ("fail : get flink from gem:%d (DRM_IOCTL_GEM_FLINK)\n", gem);
130 gem_from_flink (int fd, uint32_t flink_name)
132 struct drm_gem_open arg = { 0, };
133 /* struct nx_drm_gem_info info = { 0, }; */
135 arg.name = flink_name;
136 if (drm_ioctl (fd, DRM_IOCTL_GEM_OPEN, &arg)) {
137 _E ("fail : cannot open gem name=%d\n", flink_name);
145 // Nexell Private Video Memory Allocator for DRM
149 #define ALIGN(X,N) ( (X+N-1) & (~(N-1)) )
152 #define ALIGNED16(X) ALIGN(X,16)
155 // Nexell Private Memory Allocator
157 NX_AllocateMemory (int size, int align)
162 NX_MEMORY_INFO *pMem;
164 int drmFd = open (DRM_DEVICE_NAME, O_RDWR);
168 drmDropMaster (drmFd);
170 gemFd = alloc_gem (drmFd, size, flags);
174 dmaFd = gem_to_dmafd (drmFd, gemFd);
178 pMem = (NX_MEMORY_INFO *) calloc (1, sizeof (NX_MEMORY_INFO));
191 free_gem (drmFd, gemFd);
199 NX_FreeMemory (NX_MEMORY_INFO * pMem)
203 munmap (pMem->pBuffer, pMem->size);
206 free_gem (pMem->drmFd, pMem->gemFd);
214 // Video Specific Allocator Wrapper
216 // Suport Format & Planes
218 // 1 Plane : I420, NV12
223 NX_AllocateVideoMemory (void *bufmgr, int width, int height, int32_t planes, uint32_t format,
226 int gemFd[NX_MAX_PLANES] = { 0, };
227 int dmaFd[NX_MAX_PLANES] = { 0, };
228 #ifdef TIZEN_FEATURE_ARTIK530
229 int32_t flags = NEXELL_BO_DMA, i = 0;
231 int32_t flags = 0, i = 0;
233 int32_t luStride, cStride;
234 int32_t luVStride, cVStride;
235 int32_t stride[NX_MAX_PLANES];
236 int32_t size[NX_MAX_PLANES];
237 uint32_t flink[NX_MAX_PLANES];
238 #ifdef TIZEN_FEATURE_ARTIK530
239 tbm_surface_info_s info;
242 NX_VID_MEMORY_INFO *pVidMem = NULL;
244 int drmFd = open (DRM_DEVICE_NAME, O_RDWR);
248 drmDropMaster (drmFd);
251 luStride = ALIGN (width, 32);
252 luVStride = ALIGN (height, 16);
256 case DRM_FORMAT_YUV420:
257 case DRM_FORMAT_NV12:
258 case DRM_FORMAT_NV21:
259 case V4L2_PIX_FMT_YUV420M:
260 case V4L2_PIX_FMT_NV12M:
261 case V4L2_PIX_FMT_NV21M:
262 cStride = luStride / 2;
263 cVStride = ALIGN (height / 2, 16);
266 case DRM_FORMAT_YUV422:
267 case DRM_FORMAT_NV16:
268 case DRM_FORMAT_NV61:
269 case V4L2_PIX_FMT_YUV422M:
270 #if 0 // Disabled by DIGNSYS
271 case V4L2_PIX_FMT_NV16M:
272 case V4L2_PIX_FMT_NV61M:
274 cStride = luStride / 2;
275 cVStride = luVStride;
278 case DRM_FORMAT_YUV444:
279 //case DRM_FORMAT_NV24:
280 //case DRM_FORMAT_NV42:
281 case V4L2_PIX_FMT_YUV444M:
282 case V4L2_PIX_FMT_NV24M:
283 case V4L2_PIX_FMT_NV42M:
285 cVStride = luVStride;
288 case V4L2_PIX_FMT_GREY:
293 _E ("Unknown format type\n");
297 // Decide Memory Size
300 size[0] = luStride * luVStride + cStride * cVStride * 2;
301 stride[0] = luStride;
302 gemFd[0] = alloc_gem (drmFd, size[0], flags);
305 dmaFd[0] = gem_to_dmafd (drmFd, gemFd[0]);
311 size[0] = luStride * luVStride;
312 stride[0] = luStride;
313 gemFd[0] = alloc_gem (drmFd, size[0], flags);
316 dmaFd[0] = gem_to_dmafd (drmFd, gemFd[0]);
321 size[1] = cStride * cVStride * 2;
322 stride[1] = cStride * 2;
323 gemFd[1] = alloc_gem (drmFd, size[1], flags);
326 dmaFd[1] = gem_to_dmafd (drmFd, gemFd[1]);
332 size[0] = luStride * luVStride;
333 stride[0] = luStride;
334 gemFd[0] = alloc_gem (drmFd, size[0], flags);
337 dmaFd[0] = gem_to_dmafd (drmFd, gemFd[0]);
342 size[1] = cStride * cVStride;
344 gemFd[1] = alloc_gem (drmFd, size[1], flags);
347 dmaFd[1] = gem_to_dmafd (drmFd, gemFd[1]);
352 size[2] = cStride * cVStride;
354 gemFd[2] = alloc_gem (drmFd, size[2], flags);
357 dmaFd[2] = gem_to_dmafd (drmFd, gemFd[2]);
364 pVidMem = (NX_VID_MEMORY_INFO *) calloc (1, sizeof (NX_VID_MEMORY_INFO));
368 pVidMem->width = width;
369 pVidMem->height = height;
370 pVidMem->align = align;
371 pVidMem->planes = planes;
372 pVidMem->format = format;
373 pVidMem->drmFd = drmFd;
375 #ifdef TIZEN_FEATURE_ARTIK530
377 info.height = height;
378 info.format = TBM_FORMAT_YUV420;
381 info.planes[0].stride = luStride;
382 info.planes[0].size = luStride * luVStride;
383 info.planes[0].offset = 0;
384 info.planes[1].stride = cStride;
385 info.planes[1].size = cStride * cVStride;
386 info.planes[1].offset = info.planes[0].size;
387 info.planes[2].stride = cStride;
388 info.planes[2].size = cStride * cVStride;
389 info.planes[2].offset = info.planes[1].offset + info.planes[1].size;
391 for (i = 0; i < planes; i++) {
392 pVidMem->dmaFd[i] = dmaFd[i];
393 pVidMem->gemFd[i] = gemFd[i];
394 pVidMem->size[i] = size[i];
395 pVidMem->stride[i] = stride[i];
396 #ifdef TIZEN_FEATURE_ARTIK530
397 pVidMem->bo[i] = tbm_bo_import_fd (bufmgr, dmaFd[i]);
402 pVidMem->surface = tbm_surface_internal_create_with_bos (&info, pVidMem->bo, planes);
403 if (!pVidMem->surface)
408 #ifdef TIZEN_FEATURE_ARTIK530
410 for (i = 0; i < planes; i++) {
412 tbm_bo_unref (pVidMem->bo[i]);
417 for (i = 0; i < planes; i++) {
419 free_gem (drmFd, gemFd[i]);
435 NX_FreeVideoMemory (NX_VID_MEMORY_INFO * pMem)
439 for (i = 0; i < pMem->planes; i++) {
440 #ifdef TIZEN_FEATURE_ARTIK530
442 tbm_bo_unref (pMem->bo[i]);
444 if (pMem->pBuffer[i]) {
445 munmap (pMem->pBuffer[i], pMem->size[i]);
447 free_gem (pMem->drmFd, pMem->gemFd[i]);
448 close (pMem->dmaFd[i]);
450 #ifdef TIZEN_FEATURE_ARTIK530
452 tbm_surface_internal_destroy (pMem->surface);
461 // Memory Mapping/Unmapping Memory
464 NX_MapMemory (NX_MEMORY_INFO * pMem)
475 (char *) mmap (0, pMem->size, PROT_READ | PROT_WRITE, MAP_SHARED, pMem->dmaFd, 0);
476 if (pBuf == MAP_FAILED) {
477 _E ("Map failed : size %d, fd %d", pMem->size, pMem->dmaFd);
480 pMem->pBuffer = pBuf;
486 NX_UnmapMemory (NX_MEMORY_INFO * pMem)
494 if (0 != munmap (pMem->pBuffer, pMem->size))
497 pMem->pBuffer = NULL;
502 NX_MapVideoMemory (NX_VID_MEMORY_INFO * pMem)
510 for (i = 0; i < pMem->planes; i++) {
511 if (pMem->pBuffer[i])
515 (char *) mmap (0, pMem->size[i], PROT_READ | PROT_WRITE, MAP_SHARED,
517 if (pBuf == MAP_FAILED) {
521 pMem->pBuffer[i] = pBuf;
527 NX_UnmapVideoMemory (NX_VID_MEMORY_INFO * pMem)
532 for (i = 0; i < pMem->planes; i++) {
533 if (pMem->pBuffer[i]) {
534 munmap (pMem->pBuffer[i], pMem->size[i]);
542 NX_GetGEMHandles (int drmFd, NX_VID_MEMORY_INFO * pMem,
543 uint32_t handles[NX_MAX_PLANES])
546 memset (handles, 0, sizeof (uint32_t) * NX_MAX_PLANES);
548 for (i = 0; i < pMem->planes; i++) {
549 handles[i] = gem_from_flink (drmFd, pMem->flink[i]);
550 if (0 > (int) handles[i]) {
558 NX_GetGemHandle (int drmFd, NX_VID_MEMORY_INFO * pMem, int32_t plane)
560 if (plane >= NX_MAX_PLANES || plane < 0)
563 return gem_from_flink (drmFd, pMem->flink[plane]);