Skip to content

Commit 57f7e64

Browse files
committed
Add fallback in rpcmem_alloc
By default rpcmem_alloc will use system heap for memory allocation. Add a fallback to support allocation using fastrpc ioctl request in case system heap is not enabled. Signed-off-by: Ekansh Gupta <ekansh.gupta@oss.qualcomm.com>
1 parent 5db5b0e commit 57f7e64

File tree

3 files changed

+70
-33
lines changed

3 files changed

+70
-33
lines changed

inc/fastrpc_common.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,25 @@ int fastrpc_get_property_string(fastrpc_properties UserPropertyKey, char * value
212212
*/
213213
bool is_process_exiting(int domain);
214214

215+
/* Opens device node based on the domain
216+
* This function takes care of the backward compatibility to open
217+
* approriate device for following configurations of the device nodes
218+
* 1. 4 different device nodes
219+
* 2. 1 device node (adsprpc-smd)
220+
* 3. 2 device nodes (adsprpc-smd, adsprpc-smd-secure)
221+
* Algorithm
222+
* For ADSP, SDSP, MDSP domains:
223+
* Open secure device node fist
224+
* if no secure device, open actual device node
225+
* if still no device, open default node
226+
* if failed to open the secure node due to permission,
227+
* open default node
228+
* For CDSP domain:
229+
* Open secure device node fist
230+
* If the node does not exist or if no permission, open actual device node
231+
* If still no device, open default node
232+
* If no permission to access the default node, access thorugh HAL.
233+
*/
234+
int open_device_node(int domain_id);
235+
215236
#endif //FASTRPC_COMMON_H

src/fastrpc_apps_user.c

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ static void *dsp_client_instance[NUM_SESSIONS];
333333

334334
static int domain_init(int domain, int *dev);
335335
static void domain_deinit(int domain);
336-
static int open_device_node(int domain_id);
337336
static int close_device_node(int domain_id, int dev);
338337
extern int apps_mem_table_init(void);
339338
extern void apps_mem_table_deinit(void);
@@ -3265,26 +3264,7 @@ static const char *get_domain_name(int domain_id) {
32653264
return name;
32663265
}
32673266

3268-
/* Opens device node based on the domain
3269-
This function takes care of the backward compatibility to open
3270-
approriate device for following configurations of the device nodes
3271-
1. 4 different device nodes
3272-
2. 1 device node (adsprpc-smd)
3273-
3. 2 device nodes (adsprpc-smd, adsprpc-smd-secure)
3274-
Algorithm
3275-
For ADSP, SDSP, MDSP domains:
3276-
Open secure device node fist
3277-
if no secure device, open actual device node
3278-
if still no device, open default node
3279-
if failed to open the secure node due to permission,
3280-
open default node
3281-
For CDSP domain:
3282-
Open secure device node fist
3283-
If the node does not exist or if no permission, open actual device node
3284-
If still no device, open default node
3285-
If no permission to access the default node, access thorugh HAL.
3286-
*/
3287-
static int open_device_node(int domain_id) {
3267+
int open_device_node(int domain_id) {
32883268
int dev = -1, nErr = 0;
32893269
int domain = GET_DOMAIN_FROM_EFFEC_DOMAIN_ID(domain_id);
32903270
int sess_id = GET_SESSION_ID_FROM_DOMAIN_ID(domain_id);

src/rpcmem_linux.c

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "HAP_farf.h"
4848
#include "apps_std.h"
4949
#include "fastrpc_common.h"
50+
#include "fastrpc_ioctl.h"
5051
#include "rpcmem.h"
5152
#include "verify.h"
5253

@@ -68,6 +69,7 @@ struct dma_heap_allocation_data {
6869
_IOWR(DMA_HEAP_IOC_MAGIC, 0x0, struct dma_heap_allocation_data)
6970
#define DMA_HEAP_NAME "/dev/dma_heap/system"
7071
static int dmafd = -1;
72+
static int rpcfd = -1;
7173
static QList rpclst;
7274
static pthread_mutex_t rpcmt;
7375
struct rpc_info {
@@ -92,7 +94,15 @@ void rpcmem_init() {
9294

9395
dmafd = open(DMA_HEAP_NAME, O_RDONLY | O_CLOEXEC);
9496
if (dmafd < 0) {
95-
FARF(ERROR, "Error %d: Unable to open %s\n", errno, DMA_HEAP_NAME);
97+
FARF(ALWAYS, "Warning %d: Unable to open %s, falling back to fastrpc ioctl\n", errno, DMA_HEAP_NAME);
98+
/*
99+
* Application should link proper library as DEFAULT_DOMAIN_ID
100+
* is used to open rpc device node and not the uri passed by
101+
* user.
102+
*/
103+
rpcfd = open_device_node(DEFAULT_DOMAIN_ID);
104+
if (rpcfd < 0)
105+
FARF(ALWAYS, "Warning %d: Unable to open fastrpc dev node for domain: %d\n", errno, DEFAULT_DOMAIN_ID);
96106
}
97107
pthread_mutex_unlock(&rpcmt);
98108
}
@@ -101,6 +111,8 @@ void rpcmem_deinit() {
101111
pthread_mutex_lock(&rpcmt);
102112
if (dmafd != -1)
103113
close(dmafd);
114+
if (rpcfd != -1)
115+
close(rpcfd);
104116
pthread_mutex_unlock(&rpcmt);
105117
pthread_mutex_destroy(&rpcmt);
106118
}
@@ -135,28 +147,52 @@ int rpcmem_to_fd(void *po) { return rpcmem_to_fd_internal(po); }
135147

136148
void *rpcmem_alloc_internal(int heapid, uint32 flags, size_t size) {
137149
struct rpc_info *rinfo;
138-
int nErr = 0;
150+
int nErr = 0, fd = -1;
139151
struct dma_heap_allocation_data dmabuf = {
140152
.len = size,
141153
.fd_flags = O_RDWR | O_CLOEXEC,
142154
};
143155

144-
if (dmafd == -1 || size <= 0)
156+
if ((dmafd == -1 && rpcfd == -1) || size <= 0) {
157+
FARF(ERROR,
158+
"Error: Unable to allocate memory dmaheap fd %d, rpcfd %d, size "
159+
"%zu, flags %u",
160+
dmafd, rpcfd, size, flags);
145161
return NULL;
162+
}
146163

147164
VERIFY(0 != (rinfo = calloc(1, sizeof(*rinfo))));
148165

149-
nErr = ioctl(dmafd, DMA_HEAP_IOCTL_ALLOC, &dmabuf);
150-
if (nErr) {
151-
FARF(ERROR,
152-
"Error %d: Unable to allocate memory dmaheap fd %d, heapid %d, size "
153-
"%zu, flags %u",
154-
errno, dmafd, heapid, size, flags);
155-
goto bail;
166+
if (dmafd != -1) {
167+
nErr = ioctl(dmafd, DMA_HEAP_IOCTL_ALLOC, &dmabuf);
168+
if (nErr) {
169+
FARF(ERROR,
170+
"Error %d: Unable to allocate memory dmaheap fd %d, heapid %d, size "
171+
"%zu, flags %u",
172+
errno, dmafd, heapid, size, flags);
173+
goto bail;
174+
}
175+
fd = dmabuf.fd;
176+
} else {
177+
struct fastrpc_ioctl_alloc_dma_buf buf;
178+
179+
buf.size = size + PAGE_SIZE;
180+
buf.fd = -1;
181+
buf.flags = 0;
182+
183+
nErr = ioctl(rpcfd, FASTRPC_IOCTL_ALLOC_DMA_BUFF, (unsigned long)&buf);
184+
if (nErr) {
185+
FARF(ERROR,
186+
"Error %d: Unable to allocate memory fastrpc fd %d, heapid %d, size "
187+
"%zu, flags %u",
188+
errno, rpcfd, heapid, size, flags);
189+
goto bail;
190+
}
191+
fd = buf.fd;
156192
}
157193
VERIFY(0 != (rinfo->buf = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED,
158-
dmabuf.fd, 0)));
159-
rinfo->fd = dmabuf.fd;
194+
fd, 0)));
195+
rinfo->fd = fd;
160196
rinfo->aligned_buf =
161197
(void *)(((uintptr_t)rinfo->buf /*+ PAGE_SIZE*/) & PAGE_MASK);
162198
rinfo->aligned_buf = rinfo->buf;

0 commit comments

Comments
 (0)