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 <fcntl.h>
22 #include <sys/ioctl.h>
23 #include "securec.h"
24 #include "hilog/log.h"
25 #include "dmabuf_alloc.h"
26
27 #define DMA_BUF_HEAP_ROOT "/dev/dma_heap/"
28 #define HEAP_ROOT_LEN strlen(DMA_BUF_HEAP_ROOT)
29 #define HEAP_NAME_MAX_LEN 128
30 #define HEAP_PATH_LEN (HEAP_ROOT_LEN + HEAP_NAME_MAX_LEN + 1)
31
IsHeapNameValid(const char * heapName)32 static bool IsHeapNameValid(const char *heapName)
33 {
34 if ((heapName == NULL) || (strlen(heapName) == 0) ||
35 (strlen(heapName) > HEAP_NAME_MAX_LEN)) {
36 return false;
37 }
38
39 return true;
40 }
41
IsSyncTypeValid(DmabufHeapBufferSyncType syncType)42 static bool IsSyncTypeValid(DmabufHeapBufferSyncType syncType)
43 {
44 switch (syncType) {
45 case DMA_BUF_HEAP_BUF_SYNC_RW:
46 case DMA_BUF_HEAP_BUF_SYNC_READ:
47 case DMA_BUF_HEAP_BUF_SYNC_WRITE:
48 return true;
49 default:
50 return false;
51 }
52 }
53
DmabufHeapOpen(const char * heapName)54 int DmabufHeapOpen(const char *heapName)
55 {
56 if (!IsHeapNameValid(heapName)) {
57 HILOG_ERROR(LOG_CORE, "heapName is wrong, name = %s.", (heapName == NULL) ? "NULL" : heapName);
58 return -EINVAL;
59 }
60
61 char heapPath[HEAP_PATH_LEN] = DMA_BUF_HEAP_ROOT;
62 errno_t ret = strcat_s(heapPath, HEAP_PATH_LEN, heapName);
63 if (ret != EOK) {
64 HILOG_ERROR(LOG_CORE, "strcat_s is wrong, heapName = %s, ret = %d.", heapName, ret);
65 return -EINVAL;
66 }
67
68 return open(heapPath, O_RDONLY | O_CLOEXEC);
69 }
70
DmabufHeapClose(unsigned int fd)71 int DmabufHeapClose(unsigned int fd)
72 {
73 return close(fd);
74 }
75
DmabufHeapBufferAlloc(unsigned int heapFd,DmabufHeapBuffer * buffer)76 int DmabufHeapBufferAlloc(unsigned int heapFd, DmabufHeapBuffer *buffer)
77 {
78 if (buffer->size == 0) {
79 HILOG_ERROR(LOG_CORE, "alloc buffer size is wrong.");
80 return -EINVAL;
81 }
82
83 struct dma_heap_allocation_data data = {
84 .len = buffer->size,
85 .fd_flags = O_RDWR | O_CLOEXEC,
86 .heap_flags = buffer->heapFlags,
87 };
88 int ret = ioctl(heapFd, DMA_HEAP_IOCTL_ALLOC, &data);
89 if (ret < 0) {
90 HILOG_ERROR(LOG_CORE, "alloc buffer failed, size = %zu, ret = %d.", buffer->size, ret);
91 return ret;
92 }
93
94 buffer->fd = data.fd;
95 return ret;
96 }
97
DmabufHeapBufferFree(DmabufHeapBuffer * buffer)98 int DmabufHeapBufferFree(DmabufHeapBuffer *buffer)
99 {
100 return close(buffer->fd);
101 }
102
DmabufHeapBufferSyncStart(unsigned int fd,DmabufHeapBufferSyncType syncType)103 int DmabufHeapBufferSyncStart(unsigned int fd, DmabufHeapBufferSyncType syncType)
104 {
105 if (!IsSyncTypeValid(syncType)) {
106 HILOG_ERROR(LOG_CORE, "buffer start syncType is wrong, syncType = %u.", syncType);
107 return -EINVAL;
108 }
109
110 struct dma_buf_sync sync = {0};
111 sync.flags = DMA_BUF_SYNC_START | syncType;
112 return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
113 }
114
DmabufHeapBufferSyncEnd(unsigned int fd,DmabufHeapBufferSyncType syncType)115 int DmabufHeapBufferSyncEnd(unsigned int fd, DmabufHeapBufferSyncType syncType)
116 {
117 if (!IsSyncTypeValid(syncType)) {
118 HILOG_ERROR(LOG_CORE, "buffer end syncType is wrong, syncType = %u.", syncType);
119 return -EINVAL;
120 }
121
122 struct dma_buf_sync sync = {0};
123 sync.flags = DMA_BUF_SYNC_END | syncType;
124 return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
125 }