• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdlib.h>
17 #include <stdbool.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <sys/ioctl.h>
25 #include <linux/dma-buf.h>
26 #include <linux/dma-heap.h>
27 #include "securec.h"
28 #include "hilog/log.h"
29 #include "dmabuf_alloc.h"
30 
31 #define DMA_BUF_HEAP_ROOT "/dev/dma_heap/"
32 #define HEAP_ROOT_LEN strlen(DMA_BUF_HEAP_ROOT)
33 #define HEAP_NAME_MAX_LEN 128
34 #define HEAP_PATH_LEN (HEAP_ROOT_LEN + HEAP_NAME_MAX_LEN + 1)
35 
IsHeapNameValid(const char * heapName)36 static bool IsHeapNameValid(const char *heapName)
37 {
38     if ((heapName == NULL) || (strlen(heapName) == 0) ||
39         (strlen(heapName) >= HEAP_NAME_MAX_LEN)) {
40         return false;
41     }
42 
43     return true;
44 }
45 
IsSyncTypeValid(DmabufHeapBufferSyncType syncType)46 static bool IsSyncTypeValid(DmabufHeapBufferSyncType syncType)
47 {
48     switch (syncType) {
49         case DMA_BUF_HEAP_BUF_SYNC_RW:
50         case DMA_BUF_HEAP_BUF_SYNC_READ:
51         case DMA_BUF_HEAP_BUF_SYNC_WRITE:
52             return true;
53         default:
54             return false;
55     }
56 }
57 
DmabufHeapOpen(const char * heapName)58 int DmabufHeapOpen(const char *heapName)
59 {
60     if (!IsHeapNameValid(heapName)) {
61         HILOG_ERROR(LOG_CORE, "heapName is wrong, name = %s.", (heapName == NULL) ? "NULL" : heapName);
62         return -EINVAL;
63     }
64 
65     char heapPath[HEAP_PATH_LEN] = DMA_BUF_HEAP_ROOT;
66     errno_t ret = strcat_s(heapPath, HEAP_PATH_LEN, heapName);
67     if (ret != EOK) {
68         HILOG_ERROR(LOG_CORE, "strcat_s is wrong, heapName = %s, ret = %d.", heapName, ret);
69         return -EINVAL;
70     }
71 
72     return open(heapPath, O_RDONLY | O_CLOEXEC);
73 }
74 
DmabufHeapClose(unsigned int fd)75 int DmabufHeapClose(unsigned int fd)
76 {
77     return close(fd);
78 }
79 
DmabufHeapBufferAlloc(unsigned int heapFd,DmabufHeapBuffer * buffer)80 int DmabufHeapBufferAlloc(unsigned int heapFd, DmabufHeapBuffer *buffer)
81 {
82     if (buffer->size == 0) {
83         HILOG_ERROR(LOG_CORE, "alloc buffer size is wrong.");
84         return -EINVAL;
85     }
86 
87     struct dma_heap_allocation_data data = {
88         .len = buffer->size,
89         .fd_flags = O_RDWR | O_CLOEXEC,
90     };
91     int ret = ioctl(heapFd, DMA_HEAP_IOCTL_ALLOC, &data);
92     if (ret < 0) {
93         HILOG_ERROR(LOG_CORE, "alloc buffer failed, size = %zu, ret = %d.", buffer->size, ret);
94         return ret;
95     }
96 
97     buffer->fd = data.fd;
98     return ret;
99 }
100 
DmabufHeapBufferFree(DmabufHeapBuffer * buffer)101 int DmabufHeapBufferFree(DmabufHeapBuffer *buffer)
102 {
103     return close(buffer->fd);
104 }
105 
DmabufHeapBufferSyncStart(unsigned int fd,DmabufHeapBufferSyncType syncType)106 int DmabufHeapBufferSyncStart(unsigned int fd, DmabufHeapBufferSyncType syncType)
107 {
108     if (!IsSyncTypeValid(syncType)) {
109         HILOG_ERROR(LOG_CORE, "buffer start syncType is wrong, syncType = %u.", syncType);
110         return -EINVAL;
111     }
112 
113     struct dma_buf_sync sync = {0};
114     sync.flags = DMA_BUF_SYNC_START | syncType;
115     return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
116 }
117 
DmabufHeapBufferSyncEnd(unsigned int fd,DmabufHeapBufferSyncType syncType)118 int DmabufHeapBufferSyncEnd(unsigned int fd, DmabufHeapBufferSyncType syncType)
119 {
120     if (!IsSyncTypeValid(syncType)) {
121         HILOG_ERROR(LOG_CORE, "buffer end syncType is wrong, syncType = %u.", syncType);
122         return -EINVAL;
123     }
124 
125     struct dma_buf_sync sync = {0};
126     sync.flags = DMA_BUF_SYNC_END | syncType;
127     return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
128 }