[UAPI] Use a hwmem type to support contiguous mapping
authorDongju Chae <dongju.chae@samsung.com>
Mon, 25 Jan 2021 04:53:38 +0000 (13:53 +0900)
committer채동주/On-Device Lab(SR)/Staff Engineer/삼성전자 <dongju.chae@samsung.com>
Mon, 22 Feb 2021 04:28:04 +0000 (13:28 +0900)
This patch changes hwmem_alloc/dealloc to require a hwmem type,
to support the physically-contiguous mapping.

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
src/core/ne-handler.cc
src/core/ne-hwmem.cc
src/core/ne-hwmem.h
src/core/npu/NPUdrvAPI.h
src/core/npu/NPUdrvAPI_emul.cc
src/core/npu/NPUdrvAPI_triv2.cc

index 9e545b3..cdb301a 100644 (file)
@@ -816,6 +816,7 @@ TrinityVision2::setModel (const generic_buffer *model_buf, Model ** model_ptr)
   if (model->getMetadata()->getProgramSize() > 0) {
     HWmem * hwmem_prog = new HWmem (new HWmemDevice);
     hwmem_prog->setDriverAPI (api_.get());
+    hwmem_prog->setContiguous (true);
 
     model->setProgramData (hwmem_prog);
 
index 9e97179..99edd48 100644 (file)
@@ -16,7 +16,7 @@
 /** @brief HWmem constructor */
 HWmem::HWmem (const HWmemImpl* impl)
   : impl_ (impl), api_ (nullptr), dmabuf_ (-1), base_addr_ (nullptr),
-    offset_ (0), size_ (0), parent_ (nullptr)
+    offset_ (0), size_ (0), contiguous_(false), parent_ (nullptr)
 {
 }
 
@@ -88,7 +88,7 @@ HWmemDevice::cleanup (HWmem *hwmem) const
   }
 
   if (hwmem->getDmabuf () >= 0) {
-    hwmem->getDriverAPI()->dealloc (hwmem->getDmabuf ());
+    hwmem->getDriverAPI()->dealloc (hwmem->getDmabuf (), hwmem->isContiguous());
 
     hwmem->setDmabuf (-1);
     hwmem->setSize (0);
@@ -112,7 +112,8 @@ HWmemDevice::alloc (HWmem *hwmem, size_t size) const
     return -EBUSY;
   }
 
-  int dmabuf = hwmem->getDriverAPI()->alloc (ALIGNED_SIZE (size));
+  int dmabuf = hwmem->getDriverAPI()->alloc (ALIGNED_SIZE (size),
+                 hwmem->isContiguous());
   if (dmabuf < 0)
     return dmabuf;
 
@@ -136,7 +137,7 @@ HWmemDevice::dealloc (HWmem *hwmem) const
   if (hwmem->getDmabuf() < 0)
     return -EINVAL;
 
-  hwmem->getDriverAPI()->dealloc (hwmem->getDmabuf());
+  hwmem->getDriverAPI()->dealloc (hwmem->getDmabuf(), hwmem->isContiguous());
   hwmem->setDmabuf (-1);
   hwmem->setSize (0);
   return 0;
index 129f7e2..30700c0 100644 (file)
@@ -79,6 +79,7 @@ class HWmem {
     void *getBaseAddr () const { return base_addr_; }
     HWmem *getParent () const { return parent_; }
     bool isExternal () const { return nullptr != dynamic_cast<const HWmemExternal *>(impl_); }
+    bool isContiguous () const { return contiguous_; }
 
     /** set private member variables */
     void setDriverAPI (const DriverAPI* api) { api_ = api; }
@@ -87,6 +88,7 @@ class HWmem {
     void setOffset (size_t offset) { offset_ = offset; }
     void setBaseAddr (void *base_addr) { base_addr_ = base_addr; }
     void setParent (HWmem *hwmem) { parent_ = hwmem; }
+    void setContiguous (bool contiguous) { contiguous_ = contiguous; }
 
     /** the below APIs require its HWmem impl. */
     char *getData ();
@@ -102,6 +104,8 @@ class HWmem {
     size_t offset_;         /**< offset of hwmem (starting from base address) */
     size_t size_;           /**< size of hwmem */
 
+    bool contiguous_;       /**< Force physically-contiguous mapping (no iommu) */
+
     HWmem *parent_;         /**< optional. used only for HWmemChunk */
 };
 
index b76081c..315e7ca 100644 (file)
@@ -79,9 +79,9 @@ class DriverAPI {
     virtual uint32_t numRequests () const { return 0; }
 
     /** @brief allocate memory with the given size. return dmabuf fd */
-    virtual int alloc (size_t size) const { return -EPERM; }
+    virtual int alloc (size_t size, bool contiguous = false) const { return -EPERM; }
     /** @brief deallocate memory with the corresponding dmabuf fd */
-    virtual int dealloc (int dmabuf) const { return -EPERM; }
+    virtual int dealloc (int dmabuf, bool contiguous = false) const { return -EPERM; }
     /** @brief get memory status */
     virtual int getMemoryStatus (size_t *alloc, size_t *free) const { return -EPERM; }
 
@@ -139,8 +139,8 @@ class TrinityVision2API : public DriverAPI {
     device_state_t isReady () const;
     uint32_t numRequests () const;
 
-    int alloc (size_t size) const;
-    int dealloc (int dmabuf) const;
+    int alloc (size_t size, bool contiguous) const;
+    int dealloc (int dmabuf, bool contiguous) const;
     int getMemoryStatus (size_t *alloc, size_t *free) const;
 
     void *mmap (int dmabuf, size_t size) const;
@@ -189,8 +189,8 @@ class TrinityEmulAPI : public DriverAPI {
     int open ();
     device_state_t isReady () const;
 
-    int alloc (size_t size) const;
-    int dealloc (int dmabuf) const;
+    int alloc (size_t size, bool contiguous) const;
+    int dealloc (int dmabuf, bool contiguous) const;
     int getMemoryStatus (size_t *alloc_total, size_t *free_total) const;
 
     void *mmap (int dmabuf, size_t size) const;
index 223d106..f79615b 100644 (file)
@@ -338,10 +338,11 @@ TrinityEmulAPI::isReady () const
 /**
  * @brief allocate memory with the given size
  * @param[in] size the memory size
+ * @param[in] contiguous contiguous mapping; don't care in emulation
  * @return dmabuf fd (>= 0) if no error. otherwise a negative errno.
  */
 int
-TrinityEmulAPI::alloc (size_t size) const
+TrinityEmulAPI::alloc (size_t size, bool contiguous) const
 {
   if (!initialized())
     return -EPERM;
@@ -364,10 +365,11 @@ TrinityEmulAPI::alloc (size_t size) const
 /**
  * @brief deallocate memory with the corresponding dmabuf fd
  * @param[in] dmabuf the dmabuf fd to be deallocated
+ * @param[in] contiguous contiguous mapping; don't care in emulation
  * @return 0 if no error. otherwise a negative errno.
  */
 int
-TrinityEmulAPI::dealloc (int dmabuf) const
+TrinityEmulAPI::dealloc (int dmabuf, bool contiguous) const
 {
   if (!initialized())
     return -EPERM;
index 19d357b..e1334a6 100644 (file)
@@ -165,17 +165,22 @@ TrinityVision2API::numRequests () const
 /**
  * @brief allocate memory with the given size
  * @param[in] size the memory size
+ * @param[in] contiguous contiguous mapping
  * @return dmabuf fd (>= 0) if no error. otherwise a negative errno.
  */
 int
-TrinityVision2API::alloc (size_t size) const
+TrinityVision2API::alloc (size_t size, bool contiguous) const
 {
+  struct trinity_ioctl_hwmem hwmem;
   int ret;
 
   if (!this->initialized())
     return -EPERM;
 
-  ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_HWMEM_ALLOC, &size);
+  hwmem.type = contiguous ? HWMEM_DMA_CONT : HWMEM_DMA_IOMMU;
+  hwmem.size = size;
+
+  ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_HWMEM_ALLOC, &hwmem);
   if (ret < 0)
     return -errno;
 
@@ -185,17 +190,22 @@ TrinityVision2API::alloc (size_t size) const
 /**
  * @brief deallocate memory with the corresponding dmabuf fd
  * @param[in] dmabuf the dmabuf fd to be deallocated
+ * @param[in] contiguous contiguous mapping
  * @return 0 if no error. otherwise a negative errno.
  */
 int
-TrinityVision2API::dealloc (int dmabuf) const
+TrinityVision2API::dealloc (int dmabuf, bool contiguous) const
 {
+  struct trinity_ioctl_hwmem hwmem;
   int ret;
 
   if (!this->initialized())
     return -EPERM;
 
-  ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_HWMEM_DEALLOC, &dmabuf);
+  hwmem.type = contiguous ? HWMEM_DMA_CONT : HWMEM_DMA_IOMMU;
+  hwmem.dbuf_fd = dmabuf;
+
+  ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_HWMEM_DEALLOC, &hwmem);
   if (ret < 0)
     return -errno;