1 /*
2 * Copyright (c) 2019, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "rpcmem.h"
31 #include "verify.h"
32 #include "fastrpc_internal.h"
33 #include "AEEQList.h"
34 #include "AEEstd.h"
35 #include "apps_std.h"
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <pthread.h>
40 #include <fcntl.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <sys/ioctl.h>
44 #include <sys/mman.h>
45 #include <errno.h>
46
47 #define PAGE_SIZE 4096
48 #define PAGE_MASK ~((uintptr_t)PAGE_SIZE - 1)
49
50 static QList rpclst;
51 static pthread_mutex_t rpcmt;
52 struct rpc_info
53 {
54 QNode qn;
55 void *buf;
56 void *aligned_buf;
57 int size;
58 int fd;
59 };
60
61 extern int open_device_node(int domain);
rpcmem_open_dev()62 static int rpcmem_open_dev()
63 {
64 return open_device_node(3);
65 }
66
rpcmem_init()67 void rpcmem_init()
68 {
69 int fd;
70 QList_Ctor(&rpclst);
71 pthread_mutex_init(&rpcmt, 0);
72 }
73
rpcmem_deinit()74 void rpcmem_deinit()
75 {
76 pthread_mutex_destroy(&rpcmt);
77 }
78
rpcmem_to_fd_internal(void * po)79 int rpcmem_to_fd_internal(void *po) {
80 struct rpc_info *rinfo, *rfree = 0;
81 QNode *pn, *pnn;
82
83 pthread_mutex_lock(&rpcmt);
84 QLIST_NEXTSAFE_FOR_ALL(&rpclst, pn, pnn)
85 {
86 rinfo = STD_RECOVER_REC(struct rpc_info, qn, pn);
87 if (rinfo->aligned_buf == po)
88 {
89 rfree = rinfo;
90 break;
91 }
92 }
93 pthread_mutex_unlock(&rpcmt);
94
95 if (rfree)
96 return rfree->fd;
97
98 return -1;
99 }
100
rpcmem_to_fd(void * po)101 int rpcmem_to_fd(void *po) {
102 return rpcmem_to_fd_internal(po);
103 }
104
105
rpcmem_alloc_internal(int heapid,uint32 flags,int size)106 void *rpcmem_alloc_internal(int heapid, uint32 flags, int size)
107 {
108 struct rpc_info *rinfo;
109 struct fastrpc_alloc_dma_buf buf;
110 int nErr = 0;
111 (void)heapid;
112 (void)flags;
113 int dev = rpcmem_open_dev();
114
115 VERIFY(0 != (rinfo = calloc(1, sizeof(*rinfo))));
116
117 buf.size = size + PAGE_SIZE;
118 buf.fd = -1;
119 buf.flags = 0;
120
121 VERIFY((0 == ioctl(dev, FASTRPC_IOCTL_ALLOC_DMA_BUFF, (unsigned long)&buf)) || errno == ENOTTY);
122 VERIFY(0 != (rinfo->buf = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, buf.fd, 0)));
123 rinfo->fd = buf.fd;
124 rinfo->aligned_buf = (void *)(((uintptr_t)rinfo->buf /*+ PAGE_SIZE*/) & PAGE_MASK);
125 rinfo->aligned_buf = rinfo->buf;
126 rinfo->size = size;
127 pthread_mutex_lock(&rpcmt);
128 QList_AppendNode(&rpclst, &rinfo->qn);
129 pthread_mutex_unlock(&rpcmt);
130
131 return rinfo->aligned_buf;
132 bail:
133 if (nErr)
134 {
135 if (rinfo)
136 {
137 if (rinfo->buf)
138 {
139 free(rinfo->buf);
140 }
141 free(rinfo);
142 }
143 }
144 return 0;
145 }
146
rpcmem_free_internal(void * po)147 void rpcmem_free_internal(void *po)
148 {
149 struct rpc_info *rinfo, *rfree = 0;
150 QNode *pn, *pnn;
151 int nErr = 0;
152
153 pthread_mutex_lock(&rpcmt);
154 QLIST_NEXTSAFE_FOR_ALL(&rpclst, pn, pnn)
155 {
156 rinfo = STD_RECOVER_REC(struct rpc_info, qn, pn);
157 if (rinfo->aligned_buf == po)
158 {
159 rfree = rinfo;
160 QNode_Dequeue(&rinfo->qn);
161 break;
162 }
163 }
164 pthread_mutex_unlock(&rpcmt);
165 if (rfree)
166 {
167 int dev = rpcmem_open_dev();
168
169 munmap(rfree->buf, rfree->size);
170 free(rfree);
171 }
172 bail:
173 return;
174
175 }
176
rpcmem_free(void * po)177 void rpcmem_free(void* po) {
178 rpcmem_free_internal(po);
179 }
180
rpcmem_alloc(int heapid,uint32 flags,int size)181 void* rpcmem_alloc(int heapid, uint32 flags, int size) {
182 return rpcmem_alloc_internal(heapid, flags, size);
183 }
184