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 char fw_api_ver_str[5];
418 ni_device_handle_t device_handle;
427 if (device_handle == NI_INVALID_DEVICE_HANDLE)
429 if (device_open_should_succeed)
432 "ERROR: %s(): Failed to add %s\n: Failed ni_device_open2()\n",
453 if (should_match_rev && \
458 "Skipping %s init: device FW v%s incompatible with this version of Libxcoder\n",
471 "%s %s disabled...\n",
478 guid = j ? device_queue->
xcoders[i][j-1] + 1 : 0;
482 "initialized devices exceeds %d\n", __FUNCTION__,
487 device_queue->
xcoders[i][j] = guid;
490 fill_device_info(&device_info,
494 (should_match_rev && fw_compat_cmp) ? 1 : 0,
501 if (fw_compat_cmp < 0) {
503 "Libxcoder supported FW API version\n", device_name,
505 }
else if (fw_compat_cmp > 0) {
507 "Libxcoder supported FW API version\n", device_name,
513 ret_ts = system(
"host_ts.sh");
516 printf(
"Unable to send Host time\n");
533 for(i = 0; i <
sizeof(XCODERS_RETRY_LCK_NAME) /
sizeof(XCODERS_RETRY_LCK_NAME[0]); ++i)
535 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);
542 ni_log(
NI_LOG_ERROR,
"Failed to create %s ERROR: %s\n", XCODERS_RETRY_LCK_NAME[i], errmsg);
588 char shm_name[32] = { 0 };
589 char lck_name[32] = { 0 };
592 HANDLE map_file_handle = NULL;
593 ni_lock_handle_t mutex_handle = NULL;
594 SECURITY_DESCRIPTOR security_descriptor = { 0 };
603 ni_log(
NI_LOG_DEBUG,
"%s(): shm_name %s, lck_name %s\n", __func__, shm_name, lck_name);
606 mutex_handle = CreateMutex(NULL,
609 if (NULL == mutex_handle)
615 if (WAIT_ABANDONED == WaitForSingleObject(mutex_handle, INFINITE))
618 "obtain mutex: %p\n", mutex_handle);
622 InitializeSecurityDescriptor(&security_descriptor, SECURITY_DESCRIPTOR_REVISION);
625 map_file_handle = CreateFileMapping(
626 INVALID_HANDLE_VALUE,
634 if (NULL == map_file_handle)
644 if (ERROR_ALREADY_EXISTS == rc)
651 ni_log(
NI_LOG_INFO,
"CreateFileMapping created a new mapFile for %s, handle: %p ..\n", shm_name, map_file_handle);
663 if (NULL == p_coder_info_map)
673 if (p_coder_info_map)
675 UnmapViewOfFile(p_coder_info_map);
679 ReleaseMutex(mutex_handle);
699 if ((!p_device_context) || (!p_session_context))
749 const int existing_number_of_devices,
754 HANDLE map_file_handle = NULL;
756 map_file_handle = CreateFileMapping(
757 INVALID_HANDLE_VALUE,
765 if (NULL == map_file_handle)
774 if(ERROR_ALREADY_EXISTS == rc)
776 ni_log(
NI_LOG_INFO,
"NETINT resources have been initialized already, exiting ..\n");
777 CloseHandle(map_file_handle);
782 ni_log(
NI_LOG_INFO,
"NETINT resources not initialized, starting initialization ..\n");
794 if (NULL == p_device_queue)
798 CloseHandle(map_file_handle);
807 UnmapViewOfFile(p_device_queue);
808 CloseHandle(map_file_handle);
812 if (WAIT_ABANDONED == WaitForSingleObject(lock, INFINITE))
817 UnmapViewOfFile(p_device_queue);
818 CloseHandle(map_file_handle);
822 fill_shared_memory(p_device_queue,
824 existing_number_of_devices,
825 existing_device_names);
827 UnmapViewOfFile(p_device_queue);
832 #elif __linux__ || __APPLE__
837 static bool check_correctness_count(
const ni_device_queue_t *existing_device_queue,
838 const int should_match_rev,
839 const int existing_number_of_devices,
845 ni_device_handle_t device_handle;
850 for (i = 0; i < existing_number_of_devices; i++)
853 if (device_handle == NI_INVALID_DEVICE_HANDLE)
861 (should_match_rev && \
871 if (existing_device_queue->
xcoders[j][i] == -1 && device_capability.
xcoder_cnt[j] != 0)
876 if (existing_device_queue->
xcoders[k][i] != -1)
879 "ERROR: %s(): Discovered device %s is not in queue for module %s but is in %s\n",
902 "WARNING: %s(): Discovered %u %s, expected %u\n",
914 const int existing_number_of_devices,
922 for (i = 0; i < existing_number_of_devices; i++)
926 module_id = existing_device_queue->
xcoders[j][i];
936 "WARNING: %s(): Missing device context for %s %s\n",
955 static void sigbus_handler(
int signal)
960 static void setup_signal_handler(
struct sigaction *p,
const int signum)
964 memset(&c, 0,
sizeof(
struct sigaction));
965 if (sigemptyset(&c.sa_mask) == -1)
968 "ERROR: %s(): Could not initialize signal set: %d\n",
973 c.sa_handler = sigbus_handler;
975 if (sigaction(signum, NULL, p) == -1)
978 "ERROR: %s(): Could not save previous signal handler: %d\n",
984 if (sigaction(signum, &c, NULL) == -1)
987 "ERROR: %s(): Could not register signal handler: %d\n",
996 const int should_match_rev,
997 const int existing_number_of_devices,
1000 bool result =
false;
1002 const int signum = SIGBUS;
1005 setup_signal_handler(&p, signum);
1007 if (sigsetjmp(env, 1))
1013 if (!check_correctness_count(existing_device_queue,
1015 existing_number_of_devices,
1021 if (!check_device_queue(existing_device_queue,
1022 existing_number_of_devices,
1033 if (sigaction(signum, &p, NULL) == -1)
1036 "ERROR: %s(): Could not restore previous signal handler: %d\n",
1046 const int existing_number_of_devices,
1050 int return_value = 0;
1053 ni_rsrc_shm_state state = NI_RSRC_SHM_IS_INVALID;
1054 int flags = O_CREAT | O_RDWR | O_CLOEXEC;
1055 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
1058 if ((ni_rsrc_try_get_shm_lock(
CODERS_LCK_NAME, flags, mode, &lck_fd) < 0) ||
1067 (
void **)&p_device_queue)) < 0) {
1074 if (NI_RSRC_SHM_IS_EXISTED == state) {
1075 if (check_correctness(p_device_queue,
1077 existing_number_of_devices,
1078 existing_device_names))
1086 if (lockf(lck_fd, F_ULOCK, 0) < 0) {
1092 #ifndef __OPENHARMONY__
1095 ni_rsrc_remove_all_shm();
1097 if (limit_depth <= 0)
1102 existing_number_of_devices,
1103 existing_device_names,
1107 fill_shared_memory(p_device_queue,
1109 existing_number_of_devices,
1110 existing_device_names);
1113 if (p_device_queue && p_device_queue != MAP_FAILED) {
1115 p_device_queue = NULL;
1120 if (lockf(lck_fd, F_ULOCK, 0) < 0) {
1127 #ifndef __OPENHARMONY__
1133 return return_value;
1145 int32_t shm_fd = -1;
1146 ni_rsrc_shm_state state = NI_RSRC_SHM_IS_INVALID;
1148 int flags = O_CREAT | O_RDWR | O_CLOEXEC;
1149 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
1150 char shm_name[32] = { 0 };
1151 char lck_name[32] = { 0 };
1154 if(! p_device_info) {
1161 ni_log(
NI_LOG_DEBUG,
"%s(): shm_name %s, lck_name %s\n", __func__, shm_name, lck_name);
1163 if (ni_rsrc_try_get_shm_lock(lck_name, flags, mode, (
int *)&lock) < 0) {
1168 if (ni_rsrc_open_shm(shm_name,
1171 (
int *)&shm_fd) < 0) {
1176 if ((ni_rsrc_mmap_shm(shm_name,
1179 (
void **)&p_coder_info_dst)) < 0) {
1186 #ifndef __OPENHARMONY__
1187 if (msync((
void*)p_coder_info_dst,
sizeof(
ni_device_info_t), MS_SYNC | MS_INVALIDATE)) {
1198 if (p_coder_info_dst && p_coder_info_dst != MAP_FAILED) {
1200 p_coder_info_dst = NULL;
1204 #ifndef __OPENHARMONY__
1210 if (lockf(lock, F_ULOCK, 0) < 0) {
1211 ni_log(
NI_LOG_ERROR,
"Will exit from %s(), but failed to unlock lck_fd for %s\n", __func__, shm_name);
1230 if ((!p_device_context) || (!p_session_context))
1265 "p_device_info: %s\n", __func__, errmsg);
1284 char *domain,
char *slot,
char *dev,
char *func)
1292 char path[PATH_MAX];
1295 if(!device_name || !strstr(device_name,
"/dev/nvme") || !pcie)
1301 char *start = device_name + 5;
1304 snprintf(path,
sizeof(path),
"/sys/block/%s", start);
1308 char target[PATH_MAX];
1310 ssize_t len = readlink(path, target,
sizeof(target) - 1);
1319 char *saveptr = NULL;
1321 pcie[4] = pcie[7] =
':';
1324 while(ptr != NULL) {
1326 if (strlen(ptr) == 12)
1328 ret = sscanf(ptr,
"%4c:%2c:%2c.%1c", pcie, pcie+5,pcie+8,pcie+11);
1341 if (!domain || !slot || !dev || !func)
1345 domain[4] = slot[2] = dev[2] = func[1] =
'\0';
1346 sscanf(pcie,
"%4[^:]:%2[^:]:%2[^.].%1s", domain, slot, dev, func);
1347 ni_log2(NULL,
NI_LOG_DEBUG,
"\t%d: Domain: %s, Slot: %s, Device: %s, Function: %s\n", i, domain, slot, dev, func);
1367 ni_retcode_t ni_rsrc_try_get_shm_lock(
const char *lck_name,
1374 if (!lck_name || !lck_fd) {
1381 if (0 != mkdir(
LOCK_DIR, S_IRWXU | S_IRWXG | S_IRWXO)) {
1389 lock = open(lck_name, flags, mode);
1393 __func__, lck_name, errmsg);
1399 while (lockf(lock, F_TLOCK, 0) != 0)
1403 if (retry_cnt >= 900)
1406 ni_log(
NI_LOG_ERROR,
"ERROR %s() lockf() %s fail: %s\n", __func__, lck_name, errmsg);
1407 ni_log(
NI_LOG_ERROR,
"ERROR %s() If persists, stop traffic and run rm /dev/shm/NI_*\n", __func__);
1418 #if defined(__OPENHARMONY__)
1419 static ni_retcode_t openharmony_open_shm(
const char *shm_name,
1421 ni_rsrc_shm_state *state,
1425 int flag = IPC_CREAT | IPC_EXCL;
1426 char shm_path[PATH_MAX];
1428 if (!shm_name || !shm_fd) {
1433 memset(shm_path, 0, PATH_MAX);
1434 snprintf(shm_path, PATH_MAX,
"%s/%s",
LOCK_DIR, shm_name);
1438 if (0 != access(shm_path, F_OK)) {
1439 int fd = open(shm_path, O_RDWR | O_CREAT | O_CLOEXEC,
1440 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1443 ni_log(
NI_LOG_ERROR,
"ERROR: %s() open() %s fail: %s\n", __func__, shm_name, errmsg);
1451 key_t key = ftok(shm_path, PROJ_ID);
1458 *state = NI_RSRC_SHM_IS_CREATED;
1461 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1462 shm_id = shmget(key, shm_size, mode | flag);
1465 *state = NI_RSRC_SHM_IS_EXISTED;
1467 shm_id = shmget(key, shm_size, mode | flag);
1481 static ni_retcode_t openharmony_remove_shm(
const char *shm_name,
1485 char shm_path[PATH_MAX];
1492 memset(shm_path, 0, PATH_MAX);
1493 snprintf(shm_path, PATH_MAX,
"%s/%s",
LOCK_DIR, shm_name);
1496 if (0 != access(shm_path, F_OK)) {
1502 key_t key = ftok(shm_path, PROJ_ID);
1510 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1511 shm_id = shmget(key, shm_size, mode);
1518 shmctl(shm_id, IPC_RMID,
nullptr);
1523 #elif defined(_ANDROID)
1524 static ni_retcode_t android_open_shm(
const char *shm_name,
1526 ni_rsrc_shm_state *state,
1529 int shm_fd_tmp = -1;
1531 if (!shm_name || !shm_fd) {
1542 *state = NI_RSRC_SHM_IS_CREATED;
1544 string param = shm_name;
1545 Return<void> retvalue =
1546 service->GetAppFlag(param, [&](int32_t ret, hidl_handle handle) {
1548 *state = NI_RSRC_SHM_IS_EXISTED;
1549 shm_fd_tmp = dup(handle->data[0]);
1555 if (!retvalue.isOk()) {
1560 if (shm_fd_tmp < 0) {
1561 int fd = ashmem_create_region(shm_name, shm_size);
1563 native_handle_t *native_handle = native_handle_create(1, 0);
1564 if (!native_handle) {
1568 native_handle->data[0] = fd;
1571 handle.setTo(native_handle,
true);
1572 service->SetAppFlag(param, handle);
1573 shm_fd_tmp = dup(fd);
1579 *shm_fd = shm_fd_tmp;
1584 static ni_retcode_t android_remove_shm(
const char *shm_name,
1598 string param = shm_name;
1599 Return<void> retvalue =
service->RemoveAppFlag(param);
1600 if (!retvalue.isOk()) {
1616 Return<void> retvalue =
service->RemoveAllAppFlags();
1617 if (!retvalue.isOk()) {
1626 static ni_retcode_t linux_open_shm(
const char *shm_name,
1628 ni_rsrc_shm_state *state,
1631 int shm_fd_tmp = -1;
1632 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
1633 int flag = O_CREAT | O_EXCL | O_RDWR;
1634 bool skip_ftruncate =
false;
1636 if (!shm_name || !shm_fd) {
1641 *state = NI_RSRC_SHM_IS_CREATED;
1644 shm_fd_tmp = shm_open(shm_name, flag, mode);
1645 if (shm_fd_tmp < 0) {
1647 skip_ftruncate =
true;
1648 *state = NI_RSRC_SHM_IS_EXISTED;
1650 shm_fd_tmp = shm_open(shm_name, flag, mode);
1653 if (shm_fd_tmp < 0) {
1657 __func__, shm_name, errmsg);
1663 if (!skip_ftruncate && ftruncate(shm_fd_tmp, shm_size) < 0) {
1665 shm_unlink(shm_name);
1669 *shm_fd = shm_fd_tmp;
1695 ni_rsrc_shm_state *state,
1698 #if defined(__OPENHARMONY__)
1699 return openharmony_open_shm(shm_name, shm_size, state, shm_fd);
1700 #elif defined(_ANDROID)
1701 return android_open_shm(shm_name, shm_size, state, shm_fd);
1703 return linux_open_shm(shm_name, shm_size, state, shm_fd);
1726 if (!shm_name || !shm_addr) {
1732 #ifdef __OPENHARMONY__
1733 *shm_addr = shmat(shm_fd,
nullptr, 0);
1734 if ((
void *)(-1) == *shm_addr) {
1741 *shm_addr = mmap(0, shm_size, PROT_READ | PROT_WRITE,
1742 MAP_SHARED, shm_fd, 0);
1743 if (MAP_FAILED == *shm_addr) {
1773 #ifdef __OPENHARMONY__
1776 munmap(shm_addr, shm_size);
1797 #ifdef __OPENHARMONY__
1798 return openharmony_remove_shm(shm_name, shm_size);
1799 #elif defined(_ANDROID)
1800 return android_remove_shm(shm_name, shm_size);
1802 shm_unlink(shm_name);
1821 struct dirent *dirent;
1822 char path_to_remove[PATH_MAX];
1835 while ((dirent = readdir(dir)) != NULL) {
1836 if (strncmp(dirent->d_name,
"NI_", 3) != 0) {
1840 snprintf(path_to_remove, PATH_MAX,
"%s/%s",
LOCK_DIR, dirent->d_name);
1843 if (strncasecmp(dirent->d_name,
"NI_SHM", 6) == 0) {
1844 #ifdef __OPENHARMONY__
1845 openharmony_remove_shm(dirent->d_name, 1);
1847 shm_unlink(dirent->d_name);
1852 remove(path_to_remove);
1856 android_remove_all_shm();
1859 if (closedir(dir) == -1) {