38 #if __linux__ || __APPLE__
39 #ifdef __OPENHARMONY__
45 #include <sys/types.h>
53 #include <sys/syslimits.h>
67 #if __linux__ || __APPLE__
207 const char *l = (
const char *)p_str;
208 const char *r = (
const char *)p_str1;
211 while (!isdigit(*l) && (*l) !=
'\0')
215 while (!isdigit(*r) && (*r) !=
'\0')
249 snprintf(p_name, max_name_len,
"%s/NI_QUADRA_lck_%c%d",
LOCK_DIR, type, guid);
253 snprintf(p_name, max_name_len,
"%s/NI_lck_%c%d",
LOCK_DIR, type, guid);
273 snprintf(p_name, max_name_len,
"NI_QUADRA_shm_%c%d", type, guid);
277 snprintf(p_name, max_name_len,
"NI_shm_%c%d", type, guid);
284 const ni_device_handle_t device_handle,
286 const int fw_ver_compat_warning,
300 memcpy(device_info->
fw_rev,
301 device_capability->
fw_rev,
302 sizeof(device_info->
fw_rev));
338 const int should_match_rev,
339 const int existing_number_of_devices,
342 int i, j, compatible_device_counter;
349 device_queue->
xcoders[i][j] = -1;
353 compatible_device_counter = 0;
354 for (i = 0; i < existing_number_of_devices; i++)
363 compatible_device_counter++;
367 "Maximum number of supported and compatible devices "
368 "reached. Ignoring other supported and compatible "
382 uint32_t guid_mask[4] = {0};
386 temp_guid = device_queue->
xcoders[device_type][i];
389 guid_mask[temp_guid / 32] |= (1u << ((uint32_t)temp_guid % 32));
393 for (i = 0; i < 4; i++)
395 for (j = 0; j < 32; j++)
397 if ((guid_mask[i] & (1u << j)) == 0)
399 *guidn = (i * 32) + j;
409 const bool device_open_should_succeed,
410 const int should_match_rev,
414 int i, j, fw_compat_cmp;
415 uint32_t max_io_size;
416 char fw_api_ver_str[5];
419 ni_device_handle_t device_handle;
425 if (device_handle == NI_INVALID_DEVICE_HANDLE)
427 if (device_open_should_succeed)
430 "ERROR: %s(): Failed to add %s\n: Failed ni_device_open()\n",
451 if (should_match_rev && \
456 "Skipping %s init: device FW v%s incompatible with this version of Libxcoder\n",
469 "%s %s disabled...\n",
476 guid = j ? device_queue->
xcoders[i][j-1] + 1 : 0;
480 "initialized devices exceeds %d\n", __FUNCTION__,
485 device_queue->
xcoders[i][j] = guid;
488 fill_device_info(&device_info,
492 (should_match_rev && fw_compat_cmp) ? 1 : 0,
499 if (fw_compat_cmp < 0) {
501 "Libxcoder supported FW API version\n", device_name,
503 }
else if (fw_compat_cmp > 0) {
505 "Libxcoder supported FW API version\n", device_name,
524 for(i = 0; i <
sizeof(XCODERS_RETRY_LCK_NAME) /
sizeof(XCODERS_RETRY_LCK_NAME[0]); ++i)
526 retry_shm_fd = open(XCODERS_RETRY_LCK_NAME[i], O_RDWR | O_CREAT | O_CLOEXEC | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
533 ni_log(
NI_LOG_ERROR,
"Failed to create %s ERROR: %s\n", XCODERS_RETRY_LCK_NAME[i], errmsg);
579 char shm_name[32] = { 0 };
580 char lck_name[32] = { 0 };
583 HANDLE map_file_handle = NULL;
584 ni_lock_handle_t mutex_handle = NULL;
585 SECURITY_DESCRIPTOR security_descriptor = { 0 };
594 ni_log(
NI_LOG_DEBUG,
"%s(): shm_name %s, lck_name %s\n", __func__, shm_name, lck_name);
597 mutex_handle = CreateMutex(NULL,
600 if (NULL == mutex_handle)
606 if (WAIT_ABANDONED == WaitForSingleObject(mutex_handle, INFINITE))
609 "obtain mutex: %p\n", mutex_handle);
613 InitializeSecurityDescriptor(&security_descriptor, SECURITY_DESCRIPTOR_REVISION);
616 map_file_handle = CreateFileMapping(
617 INVALID_HANDLE_VALUE,
625 if (NULL == map_file_handle)
635 if (ERROR_ALREADY_EXISTS == rc)
642 ni_log(
NI_LOG_INFO,
"CreateFileMapping created a new mapFile for %s, handle: %p ..\n", shm_name, map_file_handle);
654 if (NULL == p_coder_info_map)
664 if (p_coder_info_map)
666 UnmapViewOfFile(p_coder_info_map);
670 ReleaseMutex(mutex_handle);
690 if ((!p_device_context) || (!p_session_context))
740 const int existing_number_of_devices,
745 HANDLE map_file_handle = NULL;
747 map_file_handle = CreateFileMapping(
748 INVALID_HANDLE_VALUE,
756 if (NULL == map_file_handle)
765 if(ERROR_ALREADY_EXISTS == rc)
767 ni_log(
NI_LOG_INFO,
"NETINT resources have been initialized already, exiting ..\n");
768 CloseHandle(map_file_handle);
773 ni_log(
NI_LOG_INFO,
"NETINT resources not initialized, starting initialization ..\n");
785 if (NULL == p_device_queue)
789 CloseHandle(map_file_handle);
798 UnmapViewOfFile(p_device_queue);
799 CloseHandle(map_file_handle);
803 if (WAIT_ABANDONED == WaitForSingleObject(lock, INFINITE))
808 UnmapViewOfFile(p_device_queue);
809 CloseHandle(map_file_handle);
813 fill_shared_memory(p_device_queue,
815 existing_number_of_devices,
816 existing_device_names);
818 UnmapViewOfFile(p_device_queue);
823 #elif __linux__ || __APPLE__
828 static bool check_correctness_count(
const ni_device_queue_t *existing_device_queue,
829 const int should_match_rev,
830 const int existing_number_of_devices,
833 uint32_t max_io_size;
837 ni_device_handle_t device_handle;
843 for (i = 0; i < existing_number_of_devices; i++)
846 if (device_handle == NI_INVALID_DEVICE_HANDLE)
854 (should_match_rev && \
864 if (existing_device_queue->
xcoders[j][i] == -1 && device_capability.
xcoder_cnt[j] != 0)
869 if (existing_device_queue->
xcoders[k][i] != -1)
872 "ERROR: %s(): Discovered device %s is not in queue for module %s but is in %s\n",
895 "WARNING: %s(): Discovered %u %s, expected %u\n",
907 const int existing_number_of_devices,
915 for (i = 0; i < existing_number_of_devices; i++)
919 module_id = existing_device_queue->
xcoders[j][i];
929 "WARNING: %s(): Missing device context for %s %s\n",
948 static void sigbus_handler(
int signal)
953 static void setup_signal_handler(
struct sigaction *p,
const int signum)
957 memset(&c, 0,
sizeof(
struct sigaction));
958 if (sigemptyset(&c.sa_mask) == -1)
961 "ERROR: %s(): Could not initialize signal set: %d\n",
966 c.sa_handler = sigbus_handler;
968 if (sigaction(signum, NULL, p) == -1)
971 "ERROR: %s(): Could not save previous signal handler: %d\n",
977 if (sigaction(signum, &c, NULL) == -1)
980 "ERROR: %s(): Could not register signal handler: %d\n",
989 const int should_match_rev,
990 const int existing_number_of_devices,
995 const int signum = SIGBUS;
998 setup_signal_handler(&p, signum);
1000 if (sigsetjmp(env, 1))
1006 if (!check_correctness_count(existing_device_queue,
1008 existing_number_of_devices,
1014 if (!check_device_queue(existing_device_queue,
1015 existing_number_of_devices,
1026 if (sigaction(signum, &p, NULL) == -1)
1029 "ERROR: %s(): Could not restore previous signal handler: %d\n",
1039 const int existing_number_of_devices,
1043 int return_value = 0;
1046 ni_rsrc_shm_state state = NI_RSRC_SHM_IS_INVALID;
1047 int flags = O_CREAT | O_RDWR | O_CLOEXEC;
1048 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
1051 if ((ni_rsrc_try_get_shm_lock(
CODERS_LCK_NAME, flags, mode, &lck_fd) < 0) ||
1060 (
void **)&p_device_queue)) < 0) {
1067 if (NI_RSRC_SHM_IS_EXISTED == state) {
1068 if (check_correctness(p_device_queue,
1070 existing_number_of_devices,
1071 existing_device_names))
1079 if (lockf(lck_fd, F_ULOCK, 0) < 0) {
1085 #ifndef __OPENHARMONY__
1088 ni_rsrc_remove_all_shm();
1090 if (limit_depth <= 0)
1095 existing_number_of_devices,
1096 existing_device_names,
1100 fill_shared_memory(p_device_queue,
1102 existing_number_of_devices,
1103 existing_device_names);
1106 if (p_device_queue && p_device_queue != MAP_FAILED) {
1108 p_device_queue = NULL;
1113 if (lockf(lck_fd, F_ULOCK, 0) < 0) {
1120 #ifndef __OPENHARMONY__
1126 return return_value;
1138 int32_t shm_fd = -1;
1139 ni_rsrc_shm_state state = NI_RSRC_SHM_IS_INVALID;
1141 int flags = O_CREAT | O_RDWR | O_CLOEXEC;
1142 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
1143 char shm_name[32] = { 0 };
1144 char lck_name[32] = { 0 };
1147 if(! p_device_info) {
1154 ni_log(
NI_LOG_DEBUG,
"%s(): shm_name %s, lck_name %s\n", __func__, shm_name, lck_name);
1156 if (ni_rsrc_try_get_shm_lock(lck_name, flags, mode, (
int *)&lock) < 0) {
1161 if (ni_rsrc_open_shm(shm_name,
1164 (
int *)&shm_fd) < 0) {
1169 if ((ni_rsrc_mmap_shm(shm_name,
1172 (
void **)&p_coder_info_dst)) < 0) {
1179 #ifndef __OPENHARMONY__
1180 if (msync((
void*)p_coder_info_dst,
sizeof(
ni_device_info_t), MS_SYNC | MS_INVALIDATE)) {
1191 if (p_coder_info_dst && p_coder_info_dst != MAP_FAILED) {
1193 p_coder_info_dst = NULL;
1197 #ifndef __OPENHARMONY__
1203 if (lockf(lock, F_ULOCK, 0) < 0) {
1204 ni_log(
NI_LOG_ERROR,
"Will exit from %s(), but failed to unlock lck_fd for %s\n", __func__, shm_name);
1223 if ((!p_device_context) || (!p_session_context))
1258 "p_device_info: %s\n", __func__, errmsg);
1277 char *domain,
char *slot,
char *dev,
char *func)
1285 char path[PATH_MAX];
1288 if(!device_name || !strstr(device_name,
"/dev/nvme") || !pcie)
1294 char *start = device_name + 5;
1297 snprintf(path,
sizeof(path),
"/sys/block/%s", start);
1301 char target[PATH_MAX];
1303 ssize_t len = readlink(path, target,
sizeof(target) - 1);
1312 char *saveptr = NULL;
1314 pcie[4] = pcie[7] =
':';
1317 while(ptr != NULL) {
1319 if (strlen(ptr) == 12)
1321 ret = sscanf(ptr,
"%4c:%2c:%2c.%1c", pcie, pcie+5,pcie+8,pcie+11);
1334 if (!domain || !slot || !dev || !func)
1338 domain[4] = slot[2] = dev[2] = func[1] =
'\0';
1339 sscanf(pcie,
"%4[^:]:%2[^:]:%2[^.].%1s", domain, slot, dev, func);
1340 ni_log2(NULL,
NI_LOG_DEBUG,
"\t%d: Domain: %s, Slot: %s, Device: %s, Function: %s\n", i, domain, slot, dev, func);
1360 ni_retcode_t ni_rsrc_try_get_shm_lock(
const char *lck_name,
1367 if (!lck_name || !lck_fd) {
1374 if (0 != mkdir(
LOCK_DIR, S_IRWXU | S_IRWXG | S_IRWXO)) {
1382 lock = open(lck_name, flags, mode);
1386 __func__, lck_name, errmsg);
1392 while (lockf(lock, F_TLOCK, 0) != 0)
1396 if (retry_cnt >= 900)
1399 ni_log(
NI_LOG_ERROR,
"ERROR %s() lockf() %s fail: %s\n", __func__, lck_name, errmsg);
1400 ni_log(
NI_LOG_ERROR,
"ERROR %s() If persists, stop traffic and run rm /dev/shm/NI_*\n", __func__);
1411 #if defined(__OPENHARMONY__)
1412 static ni_retcode_t openharmony_open_shm(
const char *shm_name,
1414 ni_rsrc_shm_state *state,
1418 int flag = IPC_CREAT | IPC_EXCL;
1419 char shm_path[PATH_MAX];
1421 if (!shm_name || !shm_fd) {
1426 memset(shm_path, 0, PATH_MAX);
1427 snprintf(shm_path, PATH_MAX,
"%s/%s",
LOCK_DIR, shm_name);
1431 if (0 != access(shm_path, F_OK)) {
1432 int fd = open(shm_path, O_RDWR | O_CREAT | O_CLOEXEC,
1433 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1436 ni_log(
NI_LOG_ERROR,
"ERROR: %s() open() %s fail: %s\n", __func__, shm_name, errmsg);
1444 key_t key = ftok(shm_path, PROJ_ID);
1451 *state = NI_RSRC_SHM_IS_CREATED;
1454 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1455 shm_id = shmget(key, shm_size, mode | flag);
1458 *state = NI_RSRC_SHM_IS_EXISTED;
1460 shm_id = shmget(key, shm_size, mode | flag);
1474 static ni_retcode_t openharmony_remove_shm(
const char *shm_name,
1478 char shm_path[PATH_MAX];
1485 memset(shm_path, 0, PATH_MAX);
1486 snprintf(shm_path, PATH_MAX,
"%s/%s",
LOCK_DIR, shm_name);
1489 if (0 != access(shm_path, F_OK)) {
1495 key_t key = ftok(shm_path, PROJ_ID);
1503 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1504 shm_id = shmget(key, shm_size, mode);
1511 shmctl(shm_id, IPC_RMID,
nullptr);
1516 #elif defined(_ANDROID)
1517 static ni_retcode_t android_open_shm(
const char *shm_name,
1519 ni_rsrc_shm_state *state,
1522 int shm_fd_tmp = -1;
1524 if (!shm_name || !shm_fd) {
1535 *state = NI_RSRC_SHM_IS_CREATED;
1537 string param = shm_name;
1538 Return<void> retvalue =
1539 service->GetAppFlag(param, [&](int32_t ret, hidl_handle handle) {
1541 *state = NI_RSRC_SHM_IS_EXISTED;
1542 shm_fd_tmp = dup(handle->data[0]);
1548 if (!retvalue.isOk()) {
1553 if (shm_fd_tmp < 0) {
1554 int fd = ashmem_create_region(shm_name, shm_size);
1556 native_handle_t *native_handle = native_handle_create(1, 0);
1557 if (!native_handle) {
1561 native_handle->data[0] = fd;
1564 handle.setTo(native_handle,
true);
1565 service->SetAppFlag(param, handle);
1566 shm_fd_tmp = dup(fd);
1572 *shm_fd = shm_fd_tmp;
1577 static ni_retcode_t android_remove_shm(
const char *shm_name,
1591 string param = shm_name;
1592 Return<void> retvalue =
service->RemoveAppFlag(param);
1593 if (!retvalue.isOk()) {
1609 Return<void> retvalue =
service->RemoveAllAppFlags();
1610 if (!retvalue.isOk()) {
1619 static ni_retcode_t linux_open_shm(
const char *shm_name,
1621 ni_rsrc_shm_state *state,
1624 int shm_fd_tmp = -1;
1625 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
1626 int flag = O_CREAT | O_EXCL | O_RDWR;
1627 bool skip_ftruncate =
false;
1629 if (!shm_name || !shm_fd) {
1634 *state = NI_RSRC_SHM_IS_CREATED;
1637 shm_fd_tmp = shm_open(shm_name, flag, mode);
1638 if (shm_fd_tmp < 0) {
1640 skip_ftruncate =
true;
1641 *state = NI_RSRC_SHM_IS_EXISTED;
1643 shm_fd_tmp = shm_open(shm_name, flag, mode);
1646 if (shm_fd_tmp < 0) {
1650 __func__, shm_name, errmsg);
1656 if (!skip_ftruncate && ftruncate(shm_fd_tmp, shm_size) < 0) {
1658 shm_unlink(shm_name);
1662 *shm_fd = shm_fd_tmp;
1688 ni_rsrc_shm_state *state,
1691 #if defined(__OPENHARMONY__)
1692 return openharmony_open_shm(shm_name, shm_size, state, shm_fd);
1693 #elif defined(_ANDROID)
1694 return android_open_shm(shm_name, shm_size, state, shm_fd);
1696 return linux_open_shm(shm_name, shm_size, state, shm_fd);
1719 if (!shm_name || !shm_addr) {
1725 #ifdef __OPENHARMONY__
1726 *shm_addr = shmat(shm_fd,
nullptr, 0);
1727 if ((
void *)(-1) == *shm_addr) {
1734 *shm_addr = mmap(0, shm_size, PROT_READ | PROT_WRITE,
1735 MAP_SHARED, shm_fd, 0);
1736 if (MAP_FAILED == *shm_addr) {
1766 #ifdef __OPENHARMONY__
1769 munmap(shm_addr, shm_size);
1790 #ifdef __OPENHARMONY__
1791 return openharmony_remove_shm(shm_name, shm_size);
1792 #elif defined(_ANDROID)
1793 return android_remove_shm(shm_name, shm_size);
1795 shm_unlink(shm_name);
1814 struct dirent *dirent;
1815 char path_to_remove[PATH_MAX];
1828 while ((dirent = readdir(dir)) != NULL) {
1829 if (strncmp(dirent->d_name,
"NI_", 3) != 0) {
1833 snprintf(path_to_remove, PATH_MAX,
"%s/%s",
LOCK_DIR, dirent->d_name);
1836 if (strncasecmp(dirent->d_name,
"NI_SHM", 6) == 0) {
1837 #ifdef __OPENHARMONY__
1838 openharmony_remove_shm(dirent->d_name, 1);
1840 shm_unlink(dirent->d_name);
1845 remove(path_to_remove);
1849 android_remove_all_shm();
1852 if (closedir(dir) == -1) {