diff options
Diffstat (limited to 'hifi/xaf/host-apf/proxy/xf-proxy.c')
-rw-r--r-- | hifi/xaf/host-apf/proxy/xf-proxy.c | 686 |
1 files changed, 686 insertions, 0 deletions
diff --git a/hifi/xaf/host-apf/proxy/xf-proxy.c b/hifi/xaf/host-apf/proxy/xf-proxy.c new file mode 100644 index 00000000..0af45179 --- /dev/null +++ b/hifi/xaf/host-apf/proxy/xf-proxy.c @@ -0,0 +1,686 @@ +/******************************************************************************* +* Copyright (C) 2018 Cadence Design Systems, Inc. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to use this Software with Cadence processor cores only and +* not with any other processors and platforms, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be included +* in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +******************************************************************************/ + +#define MODULE_TAG PROXY + +/******************************************************************************* + * Includes + ******************************************************************************/ + +#include "xf.h" + +/******************************************************************************* + * Tracing configuration + ******************************************************************************/ + +TRACE_TAG(INIT, 1); +TRACE_TAG(CMD, 1); +TRACE_TAG(EXEC, 1); +TRACE_TAG(RSP, 1); +TRACE_TAG(REG, 1); +TRACE_TAG(MEM, 1); +TRACE_TAG(GRAPH, 1); +TRACE_TAG(BUFFER, 1); + +/******************************************************************************* + * Internal functions definitions + ******************************************************************************/ + +/* ...execute proxy command synchronously */ +static inline int xf_proxy_cmd_exec(xf_proxy_t *proxy, xf_user_msg_t *msg) +{ + xf_proxy_msg_t m; + + /* ...send command to remote proxy */ + m.id = msg->id, m.opcode = msg->opcode, m.length = msg->length; + + /* ...translate address */ + XF_CHK_ERR((m.address = xf_proxy_b2a(proxy, msg->buffer)) != XF_PROXY_BADADDR, -EINVAL); + + /* ...pass command to remote proxy */ + XF_CHK_API(xf_ipc_send(&proxy->ipc, &m, msg->buffer)); + + /* ...wait for response reception indication from proxy thread */ + XF_CHK_API(xf_proxy_response_get(proxy, &m)); + + /* ...copy parameters */ + msg->id = m.id, msg->opcode = m.opcode, msg->length = m.length; + + /* ...translate address back to virtual space */ + XF_CHK_ERR((msg->buffer = xf_proxy_a2b(proxy, m.address)) != (void *)-1, -EBADFD); + + TRACE(EXEC, _b("proxy[%p]: command done: [%08x:%p:%u]"), proxy, msg->opcode, msg->buffer, msg->length); + + return 0; +} + +#if 0 +/* ...pass command to remote DSP */ +static inline int xf_proxy_cmd(xf_proxy_t *proxy, xf_handle_t *handle, xf_user_msg_t *m) +{ + xf_proxy_msg_t msg; + + /* ...set session-id of the message */ + msg.id = __XF_MSG_ID(__XF_AP_CLIENT(proxy->core, handle->client), m->id); + msg.opcode = m->opcode; + msg.length = m->length; + + /* ...translate buffer pointer to shared address */ + XF_CHK_ERR((msg.address = xf_proxy_b2a(proxy, m->buffer)) != XF_PROXY_BADADDR, -EINVAL); + + /* ...submit command message to IPC layer */ + return XF_CHK_API(xf_ipc_send(&proxy->ipc, &msg, m->buffer)); +} +#endif /* 0 */ + +/* ...allocate local client-id number */ +static inline u32 xf_client_alloc(xf_proxy_t *proxy, xf_handle_t *handle) +{ + u32 client; + + if ((client = proxy->cmap[0].next) != 0) + { + /* ...pop client from free clients list */ + proxy->cmap[0].next = proxy->cmap[client].next; + + /* ...put client handle into association map */ + handle->client = client, proxy->cmap[client].handle = handle; + } + + return client; +} + +/* ...recycle local client-id number */ +static inline void xf_client_free(xf_proxy_t *proxy, xf_handle_t *handle) +{ + u32 client = handle->client; + + /* ...push client into head of the free clients list */ + proxy->cmap[client].next = proxy->cmap[0].next; + + /* ...adjust head of free clients */ + proxy->cmap[0].next = client; +} + +/* ...lookup client basing on its local id */ +static inline xf_handle_t * xf_client_lookup(xf_proxy_t *proxy, u32 client) +{ + /* ...client index must be in proper range */ + BUG(client >= XF_CFG_PROXY_MAX_CLIENTS, _x("Invalid client index: %u"), client); + + /* ...check if client index is small */ + if (proxy->cmap[client].next < XF_CFG_PROXY_MAX_CLIENTS) + return NULL; + else + return proxy->cmap[client].handle; +} + +/* ...create new client on remote core */ +static inline int xf_client_register(xf_proxy_t *proxy, xf_handle_t *handle, xf_id_t id, u32 core) +{ + void *b = xf_handle_aux(handle); + xf_user_msg_t msg; + + /* ...set session-id: source is local proxy, destination is remote proxy */ + msg.id = __XF_MSG_ID(__XF_AP_PROXY(proxy->core), __XF_DSP_PROXY(core)); + msg.opcode = XF_REGISTER; + msg.buffer = b; + msg.length = strlen(id) + 1; + + /* ...copy component identifier */ + strncpy(b, id, xf_buffer_length(handle->aux)); + + /* ...execute command synchronously */ + XF_CHK_API(xf_proxy_cmd_exec(proxy, &msg)); + + /* ...check operation is successfull */ + XF_CHK_ERR(msg.opcode == (u32) XF_REGISTER, -EFAULT); + + /* ...save received component global client-id */ + handle->id = XF_MSG_SRC(msg.id); + + TRACE(REG, _b("[%p]=[%s:%u:%u]"), handle, id, XF_PORT_CORE(handle->id), XF_PORT_CLIENT(handle->id)); + + return 0; +} + +/* ...unregister client from remote proxy */ +static inline int xf_client_unregister(xf_proxy_t *proxy, xf_handle_t *handle) +{ + xf_user_msg_t msg; + + /* ...make sure the client is consistent */ + BUG(proxy->cmap[handle->client].handle != handle, _x("Invalid handle: %p"), handle); + + /* ...set message parameters */ + msg.id = __XF_MSG_ID(__XF_AP_PROXY(proxy->core), handle->id); + msg.opcode = XF_UNREGISTER; + msg.buffer = NULL; + msg.length = 0; + + /* ...synchronously execute command on remote proxy */ + XF_CHK_API(xf_proxy_cmd_exec(proxy, &msg)); + + /* ...opcode must be XF_UNREGISTER - tbd */ + BUG(msg.opcode != XF_UNREGISTER, _x("Invalid opcode: %X"), msg.opcode); + + TRACE(REG, _b("%p[%u:%u] unregistered"), handle, XF_PORT_CORE(handle->id), XF_PORT_CLIENT(handle->id)); + + return 0; +} + +/* ...allocate shared buffer */ +static inline int xf_proxy_buffer_alloc(xf_proxy_t *proxy, u32 length, void **buffer) +{ + u32 core = proxy->core; + xf_user_msg_t msg; + + /* ...prepare command parameters */ + msg.id = __XF_MSG_ID(__XF_AP_PROXY(core), __XF_DSP_PROXY(core)); + msg.opcode = XF_ALLOC; + msg.length = length; + msg.buffer = NULL; + + /* ...synchronously execute command on remote proxy */ + XF_CHK_API(xf_proxy_cmd_exec(proxy, &msg)); + + /* ...check if response is valid */ + XF_CHK_ERR(msg.opcode == XF_ALLOC, -EBADFD); + + /* ...check if allocation is successful */ + XF_CHK_ERR(msg.buffer != NULL, -ENOMEM); + + /* ...save output parameter */ + *buffer = msg.buffer; + + TRACE(MEM, _b("proxy-%u: allocated [%p:%u]"), core, *buffer, length); + + return 0; +} + +/* ...free shared AP-DSP memory */ +static inline int xf_proxy_buffer_free(xf_proxy_t *proxy, void *buffer, u32 length) +{ + u32 core = proxy->core; + xf_user_msg_t msg; + + /* ...prepare command parameters */ + msg.id = __XF_MSG_ID(__XF_AP_PROXY(core), __XF_DSP_PROXY(core)); + msg.opcode = XF_FREE; + msg.length = length; + msg.buffer = buffer; + + /* ...synchronously execute command on remote proxy */ + XF_CHK_API(xf_proxy_cmd_exec(proxy, &msg)); + + /* ...check if response is valid */ + XF_CHK_ERR(msg.opcode == XF_FREE, -EBADFD); + + TRACE(MEM, _b("proxy-%u: free [%p:%u]"), core, buffer, length); + + return 0; +} + +/******************************************************************************* + * Proxy interface asynchronous receiving thread + ******************************************************************************/ + +static void * xf_proxy_thread(void *arg) +{ + xf_proxy_t *proxy = arg; + xf_handle_t *client; + int r; + + /* ...start polling thread */ + while (1) + { + xf_proxy_msg_t m; + xf_user_msg_t msg; + + /* ...wait for response from remote proxy (infinite timeout) */ + if ((r = xf_ipc_wait(&proxy->ipc, 0)) < 0) + break; + + /* ...retrieve all responses received */ + while ((r = xf_ipc_recv(&proxy->ipc, &m, &msg.buffer)) == sizeof(m)) + { + /* ...make sure we have proper core identifier of SHMEM interface */ + BUG(XF_MSG_DST_CORE(m.id) != proxy->core, _x("Invalid session-id: %X (core=%u)"), m.id, proxy->core); + + /* ...make sure translation is successful */ + BUG(msg.buffer == (void *)-1, _x("Invalid buffer address: %08x"), m.address); + + /* ...retrieve information fields */ + msg.id = XF_MSG_SRC(m.id), msg.opcode = m.opcode, msg.length = m.length; + + TRACE(RSP, _b("R[%08x]:(%08x,%u,%08x)"), m.id, m.opcode, m.length, m.address); + + /* ...lookup component basing on destination port specification */ + if (XF_AP_CLIENT(m.id) == 0) + { + /* ...put proxy response to local IPC queue */ + xf_proxy_response_put(proxy, &m); + } + else if ((client = xf_client_lookup(proxy, XF_AP_CLIENT(m.id))) != NULL) + { + /* ...client is found; invoke its response callback (must be non-blocking) */ + client->response(client, &msg); + } + else + { + /* ...client has been disconnected already; drop message */ + TRACE(RSP, _b("Client look-up failed - drop message")); + } + } + + /* ...if result code is negative; terminate thread operation */ + if (r < 0) + { + TRACE(ERROR, _x("abnormal proxy[%p] thread termination: %d"), proxy, r); + break; + } + } + + TRACE(INIT, _b("IPC proxy[%p] thread terminated: %d"), proxy, r); + + return (void *)(intptr_t)r; +} + +/******************************************************************************* + * HiFi proxy API + ******************************************************************************/ + +/* ...open HiFi proxy */ +int xf_proxy_init(xf_proxy_t *proxy, u32 core, void *p_shmem) +{ + u32 i; + int r; + + /* ...initialize proxy lock */ + __xf_lock_init(&proxy->lock); + + /* ...open proxy IPC interface */ + XF_CHK_API(xf_ipc_open(&proxy->ipc, core, p_shmem)); + + /* ...save proxy core - hmm, too much core identifiers - tbd */ + proxy->core = core; + + /* ...line-up all clients into single-linked list */ + for (i = 0; i < XF_CFG_PROXY_MAX_CLIENTS - 1; i++) + { + proxy->cmap[i].next = i + 1; + } + + /* ...tail of the list points back to head (list terminator) */ + proxy->cmap[i].next = 0; + + /* ...initialize thread attributes (joinable, with minimal stack) */ + if ((r = __xf_thread_create(&proxy->thread, xf_proxy_thread, proxy)) < 0) + { + TRACE(ERROR, _x("Failed to create polling thread: %d"), r); + xf_ipc_close(&proxy->ipc, core); + return r; + } + + TRACE(INIT, _b("proxy-%u[%p] opened"), core, proxy); + + return 0; +} + +/* ...close proxy handle */ +void xf_proxy_close(xf_proxy_t *proxy) +{ + u32 core = proxy->core; + + /* ...terminate proxy thread */ + __xf_thread_destroy(&proxy->thread); + + /* ...close proxy IPC interface */ + xf_ipc_close(&proxy->ipc, core); + + TRACE(INIT, _b("proxy-%u[%p] closed"), core, proxy); +} + +/******************************************************************************* + * HiFi component API + ******************************************************************************/ + +/* ...open component handle */ +int xf_open(xf_proxy_t *proxy, xf_handle_t *handle, xf_id_t id, u32 core, xf_response_cb response) +{ + int r; + + /* ...retrieve auxiliary control buffer from proxy - need I */ + XF_CHK_ERR(handle->aux = xf_buffer_get(proxy->aux), -EBUSY); + + /* ...initialize IPC data */ + XF_CHK_API(xf_ipc_data_init(&handle->ipc)); + + /* ...register client in interlocked fashion */ + xf_proxy_lock(proxy); + + /* ...allocate local client */ + if (xf_client_alloc(proxy, handle) == 0) + { + TRACE(ERROR, _x("client allocation failed")); + r = -EBUSY; + } + else if ((r = xf_client_register(proxy, handle, id, core)) < 0) + { + TRACE(ERROR, _x("client registering failed")); + xf_client_free(proxy, handle); + } + + xf_proxy_unlock(proxy); + + /* ...if failed, release buffer handle */ + if (r < 0) + { + /* ...operation failed; return buffer back to proxy pool */ + xf_buffer_put(handle->aux), handle->aux = NULL; + } + else + { + /* ...operation completed successfully; assign handle data */ + handle->response = response; + handle->proxy = proxy; + + TRACE(INIT, _b("component[%p]:(id=%s,core=%u) created"), handle, id, core); + } + + return XF_CHK_API(r); +} + +/* ...close component handle */ +void xf_close(xf_handle_t *handle) +{ + xf_proxy_t *proxy = handle->proxy; + + /* ...do I need to take component lock here? guess no - tbd */ + + /* ...buffers and stuff? - tbd */ + + /* ...acquire global proxy lock */ + xf_proxy_lock(proxy); + + /* ...unregister component from remote DSP proxy (ignore result code) */ + (void) xf_client_unregister(proxy, handle); + + /* ...recycle client-id afterwards */ + xf_client_free(proxy, handle); + + /* ...release global proxy lock */ + xf_proxy_unlock(proxy); + + /* ...destroy IPC data */ + xf_ipc_data_destroy(&handle->ipc); + + /* ...clear handle data */ + xf_buffer_put(handle->aux), handle->aux = NULL; + + /* ...wipe out proxy pointer */ + handle->proxy = NULL; + + TRACE(INIT, _b("component[%p] destroyed"), handle); +} + +/* ...port binding function */ +int xf_route(xf_handle_t *src, u32 src_port, xf_handle_t *dst, u32 dst_port, u32 num, u32 size, u32 align) +{ + xf_proxy_t *proxy = src->proxy; + xf_buffer_t *b; + xf_route_port_msg_t *m; + xf_user_msg_t msg; + + /* ...validity checks - proxy pointers are same */ + XF_CHK_ERR(proxy == dst->proxy, -EINVAL); + + /* ...buffer data is valid */ + XF_CHK_ERR(num && size && xf_is_power_of_two(align), -EINVAL); + + /* ...get control buffer */ + XF_CHK_ERR(b = xf_buffer_get(proxy->aux), -EBUSY); + + /* ...get message buffer */ + m = xf_buffer_data(b); + + /* ...fill-in message parameters */ + //m->src = __XF_PORT_SPEC2(src->id, src_port); + m->dst = __XF_PORT_SPEC2(dst->id, dst_port); + m->alloc_number = num; + m->alloc_size = size; + m->alloc_align = align; + + /* ...set command parameters */ + msg.id = __XF_MSG_ID(__XF_AP_PROXY(proxy->core), __XF_PORT_SPEC2(src->id, src_port)); + msg.opcode = XF_ROUTE; + msg.length = sizeof(*m); + msg.buffer = m; + + /* ...synchronously execute command on remote DSP */ + XF_CHK_API(xf_proxy_cmd_exec(proxy, &msg)); + + /* ...return buffer to proxy */ + xf_buffer_put(b); + + /* ...check result is successfull */ + XF_CHK_ERR(msg.opcode == (u32) XF_ROUTE, -ENOMEM); + + /* ...port binding completed */ + TRACE(GRAPH, _b("[%p]:%u bound to [%p]:%u"), src, src_port, dst, dst_port); + + return 0; +} + +/* ...port unbinding function */ +int xf_unroute(xf_handle_t *src, u32 src_port) +{ + xf_proxy_t *proxy = src->proxy; + xf_buffer_t *b; + xf_unroute_port_msg_t *m; + xf_user_msg_t msg; + int r; + + /* ...get control buffer */ + XF_CHK_ERR(b = xf_buffer_get(proxy->aux), -EBUSY); + + /* ...get message buffer */ + m = xf_buffer_data(b); + + /* ...fill-in message parameters */ + //m->src = __XF_PORT_SPEC2(src->id, src_port); + + /* ...set command parameters */ + msg.id = __XF_MSG_ID(__XF_AP_PROXY(proxy->core), __XF_PORT_SPEC2(src->id, src_port)); + msg.opcode = XF_UNROUTE; + msg.length = sizeof(*m); + msg.buffer = m; + + /* ...synchronously execute command on remote DSP */ + if ((r = xf_proxy_cmd_exec(proxy, &msg)) != 0) + { + TRACE(ERROR, _x("Command failed: %d"), r); + goto out; + } + else if (msg.opcode != (u32) XF_UNROUTE) + { + TRACE(ERROR, _x("Port unbinding failed")); + r = -EBADFD; + goto out; + } + + /* ...port binding completed */ + TRACE(GRAPH, _b("[%p]:%u unbound"), src, src_port); + +out: + /* ...return buffer to proxy */ + xf_buffer_put(b); + + return r; +} + +/* ...send a command message to component */ +int xf_command(xf_handle_t *handle, u32 port, u32 opcode, void *buffer, u32 length) +{ + xf_proxy_t *proxy = handle->proxy; + xf_proxy_msg_t msg; + + /* ...fill-in message parameters */ + msg.id = __XF_MSG_ID(__XF_AP_CLIENT(proxy->core, handle->client), __XF_PORT_SPEC2(handle->id, port)); + msg.opcode = opcode; + msg.length = length; + XF_CHK_ERR((msg.address = xf_proxy_b2a(proxy, buffer)) != XF_PROXY_BADADDR, -EINVAL); + + TRACE(CMD, _b("[%p]:[%08x]:(%08x,%u,%p)"), handle, msg.id, opcode, length, buffer); + + /* ...pass command to IPC layer */ + return XF_CHK_API(xf_ipc_send(&proxy->ipc, &msg, buffer)); +} + +/******************************************************************************* + * Buffer pool API + ******************************************************************************/ + +/* ...allocate buffer pool */ +int xf_pool_alloc(xf_proxy_t *proxy, u32 number, u32 length, xf_pool_type_t type, xf_pool_t **pool, s32 id, + xaf_mem_malloc_fxn_t xaf_malloc, xaf_mem_free_fxn_t xaf_free) +{ + xf_pool_t *p; + xf_buffer_t *b; + void *data; + int r; + + /* ...unused arg */ + (void) type; + + /* ...basic validity checks; number of buffers is positive */ + XF_CHK_ERR(number > 0, -EINVAL); + + /* ...get properly aligned buffer length */ + length = (length + XF_PROXY_ALIGNMENT - 1) & ~(XF_PROXY_ALIGNMENT - 1); + + /* ...allocate data structure */ + p = xaf_malloc(offset_of(xf_pool_t, buffer) + number * sizeof(xf_buffer_t), id); + XF_CHK_ERR(p, -ENOMEM); + + /* ...issue memory pool allocation request to remote DSP */ + xf_proxy_lock(proxy); + r = xf_proxy_buffer_alloc(proxy, number * length, &p->p); + xf_proxy_unlock(proxy); + + /* ...if operation is failed, do cleanup */ + if (r < 0) + { + TRACE(ERROR, _x("failed to allocate buffer: %d"), r); + xaf_free(p, id); + return r; + } + else + { + /* ...set pool parameters */ + p->number = number, p->length = length; + p->proxy = proxy; + } + + /* ...create individual buffers and link them into free list */ + for (p->free = b = &p->buffer[0], data = p->p; number > 0; number--, b++) + { + /* ...set address of the buffer (no length there) */ + b->address = data; + + /* ...file buffer into the free list */ + b->link.next = b + 1; + + /* ...advance data pointer in contiguous buffer */ + data = (unsigned char *) data + length; + } + + /* ...terminate list of buffers (not too good - tbd) */ + b[-1].link.next = NULL; + + TRACE(BUFFER, _b("[%p]: pool[%p] created: %u * %u"), proxy, p, p->number, p->length); + + /* ...return buffer pointer */ + *pool = p; + + return 0; +} + +/* ...buffer pool destruction */ +void xf_pool_free(xf_pool_t *pool, s32 id, xaf_mem_free_fxn_t xaf_free) +{ + xf_proxy_t *proxy = pool->proxy; + + /* ...check buffers are all freed - tbd */ + + /* ...use global proxy lock for pool operations protection */ + xf_proxy_lock(proxy); + + /* ...release allocated buffer on remote DSP */ + xf_proxy_buffer_free(proxy, pool->p, pool->length * pool->number); + + /* ...release global proxy lock */ + xf_proxy_unlock(proxy); + + /* ...deallocate pool structure itself */ + xaf_free(pool, id); + + TRACE(BUFFER, _b("[%p]::pool[%p] destroyed"), proxy, pool); +} + +/* ...get new buffer from a pool */ +xf_buffer_t * xf_buffer_get(xf_pool_t *pool) +{ + xf_buffer_t *b; + + /* ...use global proxy lock for pool operations protection */ + xf_proxy_lock(pool->proxy); + + /* ...take buffer from a head of the free list */ + if ((b = pool->free) != NULL) + { + /* ...advance free list head */ + pool->free = b->link.next, b->link.pool = pool; + + TRACE(BUFFER, _b("pool[%p]::get[%p]"), pool, b); + } + + xf_proxy_unlock(pool->proxy); + + return b; +} + +/* ...return buffer back to pool */ +void xf_buffer_put(xf_buffer_t *buffer) +{ + xf_pool_t *pool = buffer->link.pool; + + /* ...use global proxy lock for pool operations protection */ + xf_proxy_lock(pool->proxy); + + /* ...put buffer back to a pool */ + buffer->link.next = pool->free, pool->free = buffer; + + TRACE(BUFFER, _b("pool[%p]::put[%p]"), pool, buffer); + + xf_proxy_unlock(pool->proxy); +} |