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