[Log] Add missing error logs for triv2 driver API
authorDongju Chae <dongju.chae@samsung.com>
Fri, 11 Jun 2021 08:39:31 +0000 (17:39 +0900)
committer채동주/On-Device Lab(SR)/Staff Engineer/삼성전자 <dongju.chae@samsung.com>
Mon, 14 Jun 2021 03:26:58 +0000 (12:26 +0900)
This patch adds missing error logs for triv2 driver API.

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
src/core/npu/NPUdrvAPI_triv2.cc

index e4dd12868a6281d0d8646ef49d39e53234f1fc30..8279d51ea0223333af6cf9e72858d06eca07db2a 100644 (file)
 #include <ne-conf.h>
 #include <npubinfmt.h>
 
+#define TAG _N92
+#define IOCTL_RETURN_ERRNO(cmd)                        \
+  do {                                                 \
+    ret = -errno;                                      \
+    logerr (TAG, "ioctl(" #cmd ") failed: %d\n", ret); \
+    return ret;                                        \
+  } while (0)
+
 /* Only 2 devices are supported for now. 8 will be enough value even in future */
 constexpr int max_num_devs = 8;
 constexpr size_t default_buf_size = (256 * PAGE_SIZE);
@@ -90,17 +98,23 @@ TrinityVision2API::open () {
   std::string path;
   int fd;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
-  if (dev_id_ == -1)
+  if (dev_id_ == -1) {
+    logerr (TAG, "Uninitialized device id\n");
     return -ENODEV;
+  }
 
   path = "/dev/" + TrinityVision2API::dev_node_base + "-" +
          std::to_string (dev_id_);
   fd = ::open (path.c_str (), O_RDWR);
-  if (fd < 0)
+  if (fd < 0) {
+    logerr (TAG, "Unable to find device node: %s\n", path.c_str ());
     return -ENODEV;
+  }
   dev_fd_ = fd;
   return 0;
 }
@@ -113,8 +127,10 @@ TrinityVision2API::checkSanity () {
       (min_ver & TRINITY_MASK_DEV) >> TRINITY_SHIFT_DEV);
 
   /* Check if this device is a TRIV2-compatible one */
-  if (type != TRINITY_DEV_VISION2 && type != TRINITY_DEV_VISION2_CUSE)
+  if (type != TRINITY_DEV_VISION2 && type != TRINITY_DEV_VISION2_CUSE) {
+    logerr (TAG, "Invalid device type: %d\n", type);
     return -ENODEV;
+  }
 
   /* Check if this device is using a cuse-based driver */
   if (type == TRINITY_DEV_VISION2_CUSE)
@@ -122,12 +138,17 @@ TrinityVision2API::checkSanity () {
 
   min_ver &= ~TRINITY_MASK_DEV;
   /* Check if the major version numbers are same */
-  if ((lib_ver & TRINITY_MASK_MAJOR_VER) != (min_ver & TRINITY_MASK_MAJOR_VER))
+  if ((lib_ver & TRINITY_MASK_MAJOR_VER) !=
+      (min_ver & TRINITY_MASK_MAJOR_VER)) {
+    logerr (TAG, "Version mismatch between driver and library\n");
     return -ENOTSUP;
+  }
 
   /* Check if the library version is lower than the driver one */
-  if (lib_ver < min_ver)
+  if (lib_ver < min_ver) {
+    logerr (TAG, "Unsupported library version\n");
     return -ENOTSUP;
+  }
 
   return 0;
 }
@@ -167,8 +188,10 @@ TrinityVision2API::isReady () const {
   device_state_t state;
   int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return device_state_t::TRINITY_STATE_UNKNOWN;
+  }
 
   ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_STATE, &state);
   if (ret != 0)
@@ -212,18 +235,22 @@ TrinityVision2API::alloc (size_t size, bool contiguous) const {
   struct trinity_ioctl_hwmem hwmem;
   int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
-  if (size == 0 || size > UINT32_MAX)
+  if (size == 0 || size > UINT32_MAX) {
+    logerr (TAG, "Invalid size detected: %zu\n", size);
     return -EINVAL;
+  }
 
   hwmem.type = contiguous ? TRINITY_HWMEM_DMA_CONT : TRINITY_HWMEM_DMA_IOMMU;
   hwmem.size = size;
 
   ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_HWMEM_ALLOC, &hwmem);
   if (ret < 0)
-    return -errno;
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_HWMEM_ALLOC);
 
   if (!is_cuse_)
     return ret;
@@ -270,8 +297,10 @@ TrinityVision2API::dealloc (int dmabuf, bool contiguous) const {
   struct trinity_ioctl_hwmem hwmem;
   int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
   hwmem.type = contiguous ? TRINITY_HWMEM_DMA_CONT : TRINITY_HWMEM_DMA_IOMMU;
 
@@ -290,7 +319,7 @@ TrinityVision2API::dealloc (int dmabuf, bool contiguous) const {
 
   ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_HWMEM_DEALLOC, &hwmem);
   if (ret < 0)
-    return -errno;
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_HWMEM_DEALLOC);
 
   return 0;
 }
@@ -307,15 +336,19 @@ TrinityVision2API::getMemoryStatus (size_t *alloc_total,
   struct trinity_ioctl_stat_app stat;
   int ret;
 
-  if (!initialized ())
+  if (!initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
-  if (alloc_total == nullptr || free_total == nullptr)
+  if (alloc_total == nullptr || free_total == nullptr) {
+    logerr (TAG, "Invalid arguments\n");
     return -EINVAL;
+  }
 
   ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_STAT_CURRENT_APP, &stat);
   if (ret < 0)
-    return -errno;
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_STAT_CURRENT_APP);
 
   *alloc_total = stat.total_alloc_mem;
   *free_total = stat.total_freed_mem;
@@ -334,8 +367,10 @@ void *
 TrinityVision2API::mmap (int dmabuf, size_t size) const {
   void *ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return nullptr;
+  }
 
   if (size != ALIGNED_SIZE (size))
     return nullptr;
@@ -358,8 +393,10 @@ int
 TrinityVision2API::munmap (void *addr, size_t size) const {
   int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
   if (addr == nullptr || size == 0)
     return -EINVAL;
@@ -382,11 +419,15 @@ TrinityVision2API::registerModel (model_config_t *model_config,
   uint32_t device_tops;
   int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
-  if (NPU_VERSION_MAJOR (npu_version) != 2)
+  if (NPU_VERSION_MAJOR (npu_version) != 2) {
+    logerr (TAG, "Invalid driver version\n");
     return -EINVAL;
+  }
 
   model_tops = NPU_VERSION_TOPS (npu_version);
   if (model_tops != 0) {
@@ -394,8 +435,10 @@ TrinityVision2API::registerModel (model_config_t *model_config,
     if (ret != 0)
       return ret;
 
-    if (model_tops != device_tops)
+    if (model_tops != device_tops) {
+      logerr (TAG, "Unmatched TOPS (%u vs. %u)\n", model_tops, device_tops);
       return -EINVAL;
+    }
   } /** else: don't care */
 
   /* translate dmabuf FDs to cuse-compatible ones */
@@ -424,8 +467,8 @@ TrinityVision2API::registerModel (model_config_t *model_config,
     model_config->metadata_dbuf_fd = metadata_dbuf_fd;
   }
 
-  if (ret != 0)
-    return -errno;
+  if (ret < 0)
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_REGISTER_MODEL);
 
   return ret;
 }
@@ -434,13 +477,15 @@ int
 TrinityVision2API::deregisterModel (unsigned long long id) const {
   int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
   ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_DEREGISTER_MODEL, &id);
 
-  if (ret != 0)
-    return -errno;
+  if (ret < 0)
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_DEREGISTER_MODEL);
 
   return ret;
 }
@@ -454,11 +499,15 @@ int
 TrinityVision2API::runInput (input_config_t *input_config) const {
   int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
-  if (input_config == nullptr)
+  if (input_config == nullptr) {
+    logerr (TAG, "Invalid argument\n");
     return -EINVAL;
+  }
 
   /* translate dmabuf FDs to cuse-compatible ones */
   if (is_cuse_) {
@@ -500,8 +549,8 @@ TrinityVision2API::runInput (input_config_t *input_config) const {
   input_config->task_handle = UINT32_MAX;
 
   ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_RUN_INPUT, input_config);
-  if (ret != 0)
-    return -errno;
+  if (ret < 0)
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_RUN_INPUT);
 
   return input_config->req_id;
 }
@@ -516,12 +565,14 @@ int
 TrinityVision2API::stop () const {
   int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
   ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_STOP_REQUESTS);
-  if (ret != 0)
-    return -errno;
+  if (ret < 0)
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_STOP_REQUESTS);
 
   return 0;
 }
@@ -530,12 +581,14 @@ int
 TrinityVision2API::stop_target (int id) const {
   int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
   ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_STOP_REQUEST, &id);
-  if (ret != 0)
-    return -errno;
+  if (ret < 0)
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_STOP_REQUEST);
 
   return 0;
 }
@@ -543,14 +596,16 @@ TrinityVision2API::stop_target (int id) const {
 int
 TrinityVision2API::getDrvVersion () const {
   int ver;
-  int err;
+  int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
-  err = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_VERSION, &ver);
-  if (err != 0)
-    return -errno;
+  ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_VERSION, &ver);
+  if (ret < 0)
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_GET_VERSION);
 
   return ver;
 }
@@ -562,8 +617,10 @@ TrinityVision2API::fpga_memcpy (int dmabuf, uint32_t offset, void *addr,
   struct trinity_ioctl_fpga_memcpy fpga;
   int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
   fpga.dbuf_fd = dmabuf;
   fpga.dbuf_off = offset;
@@ -571,8 +628,8 @@ TrinityVision2API::fpga_memcpy (int dmabuf, uint32_t offset, void *addr,
   fpga.user_size = size;
 
   ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_FPGA_MEMCPY, &fpga);
-  if (ret != 0)
-    return -errno;
+  if (ret < 0)
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_FPGA_MEMCPY);
 
   return 0;
 }
@@ -590,8 +647,8 @@ TrinityVision2API::getProfile (int req_id, npu_profile *profile) const {
 
   ret =
       ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_PROFILE_META, &t_profile);
-  if (ret != 0)
-    return -errno;
+  if (ret < 0)
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_GET_PROFILE_META);
 
   /** no profiling buffer provided but it's okay */
   if (t_profile.total_ops == 0)
@@ -619,7 +676,7 @@ TrinityVision2API::getProfile (int req_id, npu_profile *profile) const {
     profile->layers = nullptr;
     profile->num_layers = 0;
 
-    return -errno;
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_GET_PROFILE_BUFF);
   }
 
   return 0;
@@ -627,14 +684,16 @@ TrinityVision2API::getProfile (int req_id, npu_profile *profile) const {
 
 int
 TrinityVision2API::getAPILevel (uint32_t *level) const {
-  int err;
+  int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
-  err = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_API_LEVEL, level);
-  if (err != 0)
-    return -errno;
+  ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_API_LEVEL, level);
+  if (ret != 0)
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_GET_API_LEVEL);
 
   return 0;
 }
@@ -648,12 +707,14 @@ int
 TrinityVision2API::getTops (uint32_t *tops) const {
   int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
   ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_TOPS, tops);
   if (ret != 0)
-    return -errno;
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_GET_TOPS);
 
   return ret;
 }
@@ -662,12 +723,14 @@ int
 TrinityVision2API::getDspmSize (uint32_t *dspm) const {
   int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
   ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_DSPM, dspm);
   if (ret != 0)
-    return -errno;
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_GET_DSPM);
 
   return ret;
 }
@@ -677,18 +740,22 @@ TrinityVision2API::getStatApps (npu_stat_apps *stat) const {
   struct trinity_ioctl_stat_apps stat_apps;
   int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
-  if (stat == nullptr)
+  if (stat == nullptr) {
+    logerr (TAG, "Invalid arguments\n");
     return -EINVAL;
+  }
 
   stat->num = 0;
   stat->stat = nullptr;
 
   ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_STAT_APPS, &stat_apps);
   if (ret != 0)
-    return -errno;
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_STAT_APPS);
 
   stat->num = stat_apps.num_apps;
   if (stat->num == 0)
@@ -719,11 +786,15 @@ TrinityVision2API::getStatReqs (int app_id, npu_stat_reqs *stat) const {
   struct trinity_ioctl_stat_reqs stat_reqs;
   int ret;
 
-  if (!this->initialized ())
+  if (!this->initialized ()) {
+    logerr (TAG, "Not yet initialized\n");
     return -EPERM;
+  }
 
-  if (stat == nullptr)
+  if (stat == nullptr) {
+    logerr (TAG, "Invalid arguments\n");
     return -EINVAL;
+  }
 
   stat->num = 0;
   stat->stat = nullptr;
@@ -731,7 +802,7 @@ TrinityVision2API::getStatReqs (int app_id, npu_stat_reqs *stat) const {
   stat_reqs.app_id = app_id;
   ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_STAT_REQS, &stat_reqs);
   if (ret != 0)
-    return -errno;
+    IOCTL_RETURN_ERRNO (TRINITY_IOCTL_STAT_REQS);
 
   stat->num = stat_reqs.num_reqs;
   if (stat->num == 0)