/** 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;
/** 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;
}
}
/**
- * @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;
+ }
}
/**
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 */