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