[MEM] Revise .get_dmabuf() to support KERNEL_CMA
authorDongju Chae <dongju.chae@samsung.com>
Tue, 10 Sep 2019 05:45:32 +0000 (14:45 +0900)
committer함명주/On-Device Lab(SR)/Principal Engineer/삼성전자 <myungjoo.ham@samsung.com>
Mon, 16 Sep 2019 07:21:20 +0000 (16:21 +0900)
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 <dongju.chae@samsung.com>
src/core/ne-host-input-service.c
src/core/ne-mem.c
src/core/ne-mem.h

index b644c0d..b0c9497 100644 (file)
@@ -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;
index 363ba5c..2166695 100644 (file)
@@ -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;
+  }
 }
 
 /**
index 7548ebe..e1383d9 100644 (file)
@@ -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 */