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"
7071static int dmafd = -1 ;
72+ static int rpcfd = -1 ;
7173static QList rpclst ;
7274static pthread_mutex_t rpcmt ;
7375struct 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
136148void * 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