/**
* global values maintain all the opened npus
- * currently only 1 NPU is supported
* extened npu_fd_table to a table to support multiple devices
*/
static npu_api_priv napi_priv;
}
/**
+ * @brief open npu device with prefix
+ * @param[in] dev_id index of the device to be opened
+ * @param[in] prefix the prefix of the npu driver
+ * @return file descriptor is not error, otherwise -1 with errno set
+ */
+static int _npu_open (const unsigned int dev_id, const char *prefix)
+{
+ /**
+ * 0th device can be srnpu0 or srnpu (srnpu has higher preference in search)
+ * However the next device will have to start from srnpu1 to srnpuN
+ */
+ int fd;
+ char name[64];
+
+ /** open srnpu and return for 0th device */
+ if (dev_id == 0) {
+ if ((fd = open(prefix, O_RDWR)) >= 0) {
+ return fd;
+ }
+ }
+
+ /** open srnpu${dev_id} for other dev_id */
+ snprintf(name, sizeof(name), "%s%u", prefix, dev_id);
+ fd = open(name, O_RDWR);
+
+ return fd;
+}
+
+/**
* @brief open the npu device
* @param[in] dev_id index of the device to be opened
* @return file descriptor is not error. otherwise errno
int npu_open (unsigned int dev_id)
{
int fd;
- char name[64];
+ char prefix[64];
int status;
errno = 0;
- snprintf(name, sizeof(name), "%s/%s%u", DEV_BASE_PATH, DEV_NAME, dev_id);
+ snprintf(prefix, sizeof(prefix), "%s/%s", DEV_BASE_PATH, DEV_NAME);
if (conf->test_mode == 0) {
- fd = open(name, O_RDWR);
+ fd = _npu_open (dev_id, prefix);
} else {
fd = dev_id;
}
/** open/close multiple devices at once */
fd = npu_open (DEV_FD);
EXPECT_EQ (fd, DEV_FD);
- fd = npu_open (1);
- EXPECT_EQ (fd, 1);
- fd = npu_open (2);
- EXPECT_EQ (fd, 2);
- fd = npu_open (3);
- EXPECT_EQ (fd, 3);
- /** open more than allowed devices */
- fd = npu_open (16);
- EXPECT_LT (fd, RET_SUCCESS);
- /** close unopened devices */
- ret = npu_close (17);
- EXPECT_LT (ret, RET_SUCCESS);
-
- /** close device twice */
- ret = npu_close (1);
- EXPECT_EQ (ret, RET_SUCCESS);
- ret = npu_close (1);
- EXPECT_LT (ret, RET_SUCCESS);
-
- ret = npu_close (3);
- EXPECT_EQ (ret, RET_SUCCESS);
- ret = npu_close (2);
- EXPECT_EQ (ret, RET_SUCCESS);
- ret = npu_close (DEV_FD);
- EXPECT_EQ (ret, RET_SUCCESS);
-
- /** reopen closed device */
- fd = npu_open (1);
- EXPECT_EQ (fd, 1);
- ret = npu_close (fd);
+ if (conf->test_mode == 1) {
+ /**
+ * Opening non-existing devices only passes in test mode to test
+ * functionality of NPUdrvAPI
+ */
+ fd = npu_open (1);
+ EXPECT_EQ (fd, 1);
+ fd = npu_open (2);
+ EXPECT_EQ (fd, 2);
+ fd = npu_open (3);
+ EXPECT_EQ (fd, 3);
+ /** open more than allowed devices */
+ fd = npu_open (16);
+ EXPECT_LT (fd, RET_SUCCESS);
+ /** close unopened devices */
+ ret = npu_close (17);
+ EXPECT_LT (ret, RET_SUCCESS);
+
+ /** close device twice */
+ ret = npu_close (1);
+ EXPECT_EQ (ret, RET_SUCCESS);
+ ret = npu_close (1);
+ EXPECT_LT (ret, RET_SUCCESS);
+
+ ret = npu_close (3);
+ EXPECT_EQ (ret, RET_SUCCESS);
+ ret = npu_close (2);
+ EXPECT_EQ (ret, RET_SUCCESS);
+ ret = npu_close (DEV_FD);
+ EXPECT_EQ (ret, RET_SUCCESS);
+
+ /** reopen closed device */
+ fd = npu_open (1);
+ EXPECT_EQ (fd, 1);
+ ret = npu_close (fd);
+ } else {
+ ret = npu_close (DEV_FD);
+ EXPECT_EQ (ret, RET_SUCCESS);
+ fd = npu_open (1);
+ EXPECT_LT (fd, RET_SUCCESS);
+ /** open more than allowed devices */
+ fd = npu_open (16);
+ EXPECT_LT (fd, RET_SUCCESS);
+ }
EXPECT_EQ (ret, RET_SUCCESS);
}