Skip to content

Commit d1f5390

Browse files
committed
Add internal mmap/munmap functions with DSP address tracking
Introduce fastrpc_mmap_internal() and fastrpc_munmap_internal() to handle buffer mapping with DSP virtual address management. These functions maintain mappings in the static map list and enable lookup by DSP virtual address. Extract common mmap logic into fastrpc_mmap_helper() to eliminate code duplication between fastrpc_mmap() and fastrpc_mmap_internal(). Simplify apps_mem_request_map64() by removing conditional allocation logic and always allocating in userspace, then using fastrpc_mmap_internal() for mapping. Update apps_mem_request_unmap64() to route to appropriate unmap function based on mapping type: - FASTRPC_ALLOC_HLOS_FD: use fastrpc_munmap() - ADSP_MMAP_HEAP_ADDR/ADSP_MMAP_REMOTE_HEAP_ADDR: use remote_munmap64() - Other allocations: use fastrpc_munmap_internal() Signed-off-by: Vinayak Katoch <[email protected]>
1 parent 2c3dbc7 commit d1f5390

File tree

3 files changed

+238
-83
lines changed

3 files changed

+238
-83
lines changed

inc/fastrpc_mem.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,33 @@ int fastrpc_buffer_ref(int domain, int fd, int ref, void **va, size_t *size);
7373
*/
7474
void remote_register_buf(void *buf, int size, int fd);
7575

76+
/*
77+
* Internal function to map a buffer and return the DSP virtual address.
78+
* Creates a mapping on the DSP and stores the mapping information in the static map list.
79+
*
80+
* @param domain The DSP domain ID (-1 for current domain)
81+
* @param fd File descriptor of the buffer
82+
* @param vaddr Virtual address of the buffer on CPU side
83+
* @param offset Offset from the beginning of the buffer (must be 0)
84+
* @param length Size of buffer in bytes
85+
* @param flags Mapping flags (uint32_t)
86+
* @param raddr Output: DSP virtual address of the mapped buffer
87+
*
88+
* @return 0 on success, error code on failure
89+
*/
90+
int fastrpc_mmap_internal(int domain, int fd, void *vaddr, int offset, size_t length, uint32_t flags, uint64_t *raddr);
91+
92+
/*
93+
* Internal function to unmap a buffer using the DSP virtual address.
94+
* Looks up the mapping in the static map list using the DSP virtual address,
95+
* then performs the unmap operation and removes the mapping from the list.
96+
*
97+
* @param domain The DSP domain ID (-1 for current domain)
98+
* @param raddr DSP virtual address of the mapped buffer
99+
* @param length Size of buffer in bytes to unmap
100+
*
101+
* @return 0 on success, error code on failure
102+
*/
103+
int fastrpc_munmap_internal(int domain, uint64_t raddr, size_t length);
104+
76105
#endif //FASTRPC_MEM_H

src/apps_mem_imp.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,23 +163,13 @@ __QAIC_IMPL(apps_mem_request_map64)(int heapid, uint32_t lflags, uint32_t rflags
163163
*/
164164
*vadsp = (uint64_t)fd;
165165
} else {
166-
/* Memory for unsignedPD's user-heap will be allocated in userspace for
167-
* security reasons. Memory for signedPD's user-heap will be allocated in
168-
* kernel.
169-
*/
170-
if (((rflags != ADSP_MMAP_ADD_PAGES) &&
171-
(rflags != ADSP_MMAP_ADD_PAGES_LLC)) ||
172-
(((rflags == ADSP_MMAP_ADD_PAGES) ||
173-
(rflags == ADSP_MMAP_ADD_PAGES_LLC)) &&
174-
(unsigned_module && ualloc_support))) {
175-
VERIFYC(NULL != (buf = rpcmem_alloc_internal(heapid, lflags, len)),
176-
AEE_ENORPCMEMORY);
177-
fd = rpcmem_to_fd_internal(buf);
178-
VERIFYC(fd > 0, AEE_EBADPARM);
179-
}
166+
VERIFYC(NULL != (buf = rpcmem_alloc_internal(heapid, lflags, len)),
167+
AEE_ENORPCMEMORY);
168+
fd = rpcmem_to_fd_internal(buf);
169+
VERIFYC(fd > 0, AEE_EBADPARM);
180170
VERIFY(AEE_SUCCESS ==
181-
(nErr = remote_mmap64_internal(fd, rflags, (uint64_t)buf, len,
182-
(uint64_t *)vadsp)));
171+
(nErr = fastrpc_mmap_internal(domain, fd, buf, 0, len,
172+
rflags, vadsp)));
183173
pbuf = (uint64_t)buf;
184174
*vapps = pbuf;
185175
minfo->vapps = *vapps;
@@ -250,17 +240,27 @@ __QAIC_IMPL(apps_mem_request_unmap64)(uint64_t vadsp,
250240
pthread_mutex_unlock(&me->mem_mut);
251241

252242
/* If apps_mem_request_map64 was called with flag FASTRPC_ALLOC_HLOS_FD,
253-
* use fastrpc_munmap else use remote_munmap64 to unmap.
243+
* use fastrpc_munmap. For ADSP_MMAP_HEAP_ADDR and ADSP_MMAP_REMOTE_HEAP_ADDR,
244+
* use remote_munmap64. For other cases, use fastrpc_munmap_with_raddr.
254245
*/
255246
if(mfree && mfree->rflags == FASTRPC_ALLOC_HLOS_FD) {
256247
fd = (int)vadsp;
257248
VERIFY(AEE_SUCCESS == (nErr = fastrpc_munmap(domain, fd, 0, len)));
249+
} else if (mfree && (mfree->rflags == ADSP_MMAP_HEAP_ADDR ||
250+
mfree->rflags == ADSP_MMAP_REMOTE_HEAP_ADDR)) {
251+
/* These cases use remote_mmap64_internal, so use remote_munmap64 */
252+
VERIFY(AEE_SUCCESS == (nErr = remote_munmap64((uint64_t)vadsp, len)));
258253
} else if (mfree || fastrpc_get_pd_type(domain) == AUDIO_STATICPD){
259254
/*
260255
* Map info not available for Audio static PD after daemon reconnect,
261256
* So continue to unmap to avoid driver global maps leak.
257+
* For other cases, use fastrpc_munmap_with_raddr as they were mapped with fastrpc_mmap_with_raddr.
262258
*/
263-
VERIFY(AEE_SUCCESS == (nErr = remote_munmap64((uint64_t)vadsp, len)));
259+
if (mfree) {
260+
VERIFY(AEE_SUCCESS == (nErr = fastrpc_munmap_internal(domain, (uint64_t)vadsp, len)));
261+
} else {
262+
VERIFY(AEE_SUCCESS == (nErr = remote_munmap64((uint64_t)vadsp, len)));
263+
}
264264
if (!mfree)
265265
goto bail;
266266
}

0 commit comments

Comments
 (0)