From: Dongju Chae Date: Tue, 10 Sep 2019 05:45:32 +0000 (+0900) Subject: [MEM] Revise .get_dmabuf() to support KERNEL_CMA X-Git-Tag: accepted/tizen/unified/20220103.130045~667 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7f451a1cf9ae1e05231e2336f5d3b63bcfd9476e;p=platform%2Fadaptation%2Fnpu%2Ftrix-engine.git [MEM] Revise .get_dmabuf() to support KERNEL_CMA This commit revise .get_dmabuf() to support KERNEL_CMA mode. Currently, get_dmabuf() returns the global dmabuf fd for internal memory pool. As KERNEL_CMA mode handles multiple CMA buffers, it should be revised to support this. Signed-off-by: Dongju Chae --- diff --git a/src/core/ne-host-input-service.c b/src/core/ne-host-input-service.c index b644c0d..b0c9497 100644 --- a/src/core/ne-host-input-service.c +++ b/src/core/ne-host-input-service.c @@ -359,7 +359,8 @@ static int configureHost (inputservice *me, const model *m, n40_data *data) /** setup dma buf */ mem_ptr = mem_get_instance(); - dmabuf_fd = mem_ptr->get_dmabuf(); + /** @todo change this hwmem after revising NPU API interface */ + dmabuf_fd = mem_ptr->get_dmabuf(npriv->model->memblock); offset = 0; if ((status = npu_set_buffer(npriv->fd, dmabuf_fd, offset, offset)) < 0) { return status; diff --git a/src/core/ne-mem.c b/src/core/ne-mem.c index 363ba5c..2166695 100644 --- a/src/core/ne-mem.c +++ b/src/core/ne-mem.c @@ -1130,9 +1130,12 @@ mem_fini (void) /** close gem driver */ gem_close (mpriv.fd); + /** reset private variables */ pthread_mutex_destroy (&mpriv.mutex); pthread_cond_destroy (&mpriv.cond); + memset(&mpriv, '\x00', sizeof(mem_priv)); + return 0; } @@ -1530,14 +1533,43 @@ mem_register_dmabuf (int dmabuf, uint64_t size, hwmem **hwmem_p) } /** - * @brief get the exported dmabuf fd handle for sharing - * @return dmabuf fd handle - * @todo update this API to return a dmabuf fd for hwmem passed as argument + * @brief get the exported dmabuf fd handle for CMA buffer + * @param[in] hwmem the hwmem instance + * @return dmabuf fd handle if no error. otherwise a negative errno */ static int -mem_get_dmabuf (void) +mem_get_dmabuf (const hwmem *hwmem) { - return mpriv.dmabuf; + hwmem_priv *hpriv; + + if (!hwmem) + return -EINVAL; + + hpriv = TO_HWMEM_PRIVATE (hwmem); + + assert (hpriv); + + if (hpriv->type == HWMEM_TYPE_USER) { + if (hpriv->user.fd <= 0) { + logerr (MEM_TAG, "invalid dma-buf fd for user-provided mem\n"); + return -EINVAL; + } + + return hpriv->user.fd; + } + + if (mpriv.cmode == COMPACT_MODE_KERNEL_CMA) { + /** export the handle to dmabuf fd handle */ + return gem_prime_export (mpriv.fd, hpriv->cma.handle); + } else { /** COMPACT_MODE_KERNEL_CMA */ + if (mpriv.dmabuf <= 0) { + logerr (MEM_TAG, "invalid dma-buf fd for internal memory pool. " + "did you call mem_init()?\n"); + return -EINVAL; + } + + return mpriv.dmabuf; + } } /** diff --git a/src/core/ne-mem.h b/src/core/ne-mem.h index 7548ebe..e1383d9 100644 --- a/src/core/ne-mem.h +++ b/src/core/ne-mem.h @@ -235,10 +235,11 @@ typedef struct { int (*register_dmabuf) (int dmabuf, uint64_t size, hwmem **hwmem_p); /** - * @brief get the exported dmabuf fd for memory pool (used only for COMPACT_MODE_INTERNAL) - * @return dmabuf fd handle + * @brief get the exported dmabuf fd handle for CMA buffer + * @param[in] hwmem the hwmem instance + * @return dmabuf fd handle if no error. otherwise a negative errno */ - int (*get_dmabuf) (void); + int (*get_dmabuf) (const hwmem *hwmem); /** Misc APIs */