From 63d939aaed2fa7c5ed12ae7f629708826979962c Mon Sep 17 00:00:00 2001 From: pajgaonk Date: Mon, 26 Feb 2024 16:51:12 -0800 Subject: [PATCH 1/3] code udpates for usrclk dfhv0 -> dfhv1 migration --- .../plugins/xfpga/usrclk/fpga_user_clk.c | 94 ++++++++++++++++--- 1 file changed, 80 insertions(+), 14 deletions(-) diff --git a/libraries/plugins/xfpga/usrclk/fpga_user_clk.c b/libraries/plugins/xfpga/usrclk/fpga_user_clk.c index e237d1d10626..718b31cc4dc3 100644 --- a/libraries/plugins/xfpga/usrclk/fpga_user_clk.c +++ b/libraries/plugins/xfpga/usrclk/fpga_user_clk.c @@ -135,6 +135,7 @@ #define IOPLL_WRITE_POLL_TIMEOUT_US 1000000 /* Write poll timeout */ #define USRCLK_FEATURE_ID 0x14 +const char USRCLK_GUID[] = "FFFFFFFFFFFFFFFFEEEEEEEEEEEEEEEE"; // DFHv0 struct dfh { @@ -151,10 +152,22 @@ struct dfh { uint64_t type : 4; }; }; + char guid_l[64]; + char guid_h[64]; }; static int using_iopll(char *sysfs_usrpath, const char *sysfs_path); +bool is_file_empty(char* file_path) { + FILE *fp = fopen(file_path, "r"); + if (feof(fp)){ + return 1; + } else{ + return 0; + } + fclose(fp); +} + fpga_result usrclk_reset(uint8_t *uio_ptr) { uint64_t v = 0; @@ -501,15 +514,20 @@ fpga_result usrclk_calibrate(uint8_t *uio_ptr, uint8_t *seq) fpga_result get_usrclk_uio(const char *sysfs_path, uint32_t feature_id, + const char* guid, struct opae_uio *uio, uint8_t **uio_ptr) { fpga_result res = FPGA_NOT_FOUND; char feature_path[SYSFS_PATH_MAX] = { 0 }; + char guid_path[SYSFS_PATH_MAX] = { 0 }; char dfl_dev_str[SYSFS_PATH_MAX] = { 0 }; - int gres, ret = 0; + int feature_gres, guid_gres,ret = 0; size_t i = 0; uint64_t value = 0; + char userclk_guid[128] = { 0 }; + glob_t feature_pglob; + glob_t guid_pglob; glob_t pglob; if (sysfs_path == NULL) { @@ -524,29 +542,73 @@ fpga_result get_usrclk_uio(const char *sysfs_path, return FPGA_EXCEPTION; } - gres = opae_glob(feature_path, GLOB_NOSORT, NULL, &pglob); - if (gres) { - OPAE_ERR("Failed pattern match %s: %s", - feature_path, strerror(errno)); - opae_globfree(&pglob); + if (snprintf(guid_path, sizeof(guid_path), + "%s/dfl_dev*/guid", + sysfs_path) < 0) { + OPAE_ERR("snprintf buffer overflow"); + return FPGA_EXCEPTION; + } + + feature_gres = opae_glob(feature_path, GLOB_NOSORT, NULL, &feature_pglob); + //loop over feature_pglob.gl_pathv[i]check if above all files are empty, if all files empty then feature_gres = 1; + // need to do this because I see empty guid file in dfhv0 sysfs path - /sys/class/fpga_region/region0/dfl-port.0/dfl_dev.0/guid + // its possible feature_id can be empty by some mistake which would need to be flagged + + guid_gres = opae_glob(guid_path, GLOB_NOSORT, NULL, &guid_pglob); + // loop over guid_pglob.gl_pathv[i]check if above all files are empty, if all files empty then guid_gres = 1; + // need to do this because I see empty guid file in dfhv0 sysfs path - /sys/class/fpga_region/region0/dfl-port.0/dfl_dev.0/guid + printf("feature_gres = %d, guid gres = %d\n",feature_gres, guid_gres); + + if ((feature_gres) && (guid_gres)) { + OPAE_ERR("Failed pattern match feature id - %s: %s ,guid - %s: %s ", + feature_path, strerror(errno), guid_path, strerror(errno)); + opae_globfree(&feature_pglob); + opae_globfree(&guid_pglob); return FPGA_INVALID_PARAM; + } + + // guid gets priority over feature_id + // non-empty guid file being present in sysfs path (eg /sys/class/fpga_region/region0/dfl-port.0/dfl_dev.0/guid) implies user is using dfhv1 IP + if (!guid_gres) { + printf("here 0\n"); + pglob = guid_pglob; + } else { + printf("here 1\n"); + pglob = feature_pglob; } for (i = 0; i < pglob.gl_pathc; i++) { - res = sysfs_read_u64(pglob.gl_pathv[i], &value); - if (res != FPGA_OK) { - OPAE_MSG("Failed to read sysfs value"); - continue; + if (!guid_gres) { + int fd = open(pglob.gl_pathv[i],O_RDONLY); + if(fd == -1) { + OPAE_MSG("Could not open file %s\n", pglob.gl_pathv[i]); + continue; + } + ssize_t bytes_read = read(fd, userclk_guid, sizeof(userclk_guid)); + if (bytes_read == -1) { + OPAE_MSG("Could not read from file %s\n",pglob.gl_pathv[i]); + continue; + } + if(close(fd) == -1) { + OPAE_MSG("Could not close file %s\n", pglob.gl_pathv[i]); + goto free; + } + } else { + res = sysfs_read_u64(pglob.gl_pathv[i], &value); + if (res != FPGA_OK) { + OPAE_MSG("Failed to read sysfs value"); + continue; + } } - - if (value == feature_id) { + + if ((value == feature_id) || (*guid == *userclk_guid)) { + printf("here 2\n"); res = FPGA_OK; char *p = strstr(pglob.gl_pathv[i], "dfl_dev"); if (p == NULL) { res = FPGA_NOT_FOUND; goto free; } - char *end = strchr(p, '/'); if (end == NULL) { res = FPGA_NOT_FOUND; @@ -574,7 +636,8 @@ fpga_result get_usrclk_uio(const char *sysfs_path, } free: - opae_globfree(&pglob); + opae_globfree(&feature_pglob); + opae_globfree(&guid_pglob); return res; } @@ -616,6 +679,7 @@ fpga_result get_userclock(const char *sysfs_path, result = get_usrclk_uio(sysfs_path, USRCLK_FEATURE_ID, + USRCLK_GUID, &uio, &uio_ptr); if (result != FPGA_OK) { @@ -742,6 +806,7 @@ fpga_result set_userclock(const char *sysfs_path, result = get_usrclk_uio(sysfs_path, USRCLK_FEATURE_ID, + USRCLK_GUID, &uio, &uio_ptr); if (result != FPGA_OK) { @@ -827,6 +892,7 @@ fpga_result get_userclk_revision(const char *sysfs_path, // get user clock dfh revision from UIO result = get_usrclk_uio(sysfs_path, USRCLK_FEATURE_ID, + USRCLK_GUID, &uio, &uio_ptr); if (result == FPGA_OK) { From 2d8487301bcfeffd646abcd172ef65ffe565f925 Mon Sep 17 00:00:00 2001 From: pajgaonk Date: Tue, 27 Feb 2024 15:15:06 -0800 Subject: [PATCH 2/3] code udpates for usrclk dfhv0 -> dfhv1 migration --- .../plugins/xfpga/usrclk/fpga_user_clk.c | 57 +++++++++++++------ 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/libraries/plugins/xfpga/usrclk/fpga_user_clk.c b/libraries/plugins/xfpga/usrclk/fpga_user_clk.c index 718b31cc4dc3..2dfed1202e10 100644 --- a/libraries/plugins/xfpga/usrclk/fpga_user_clk.c +++ b/libraries/plugins/xfpga/usrclk/fpga_user_clk.c @@ -135,7 +135,8 @@ #define IOPLL_WRITE_POLL_TIMEOUT_US 1000000 /* Write poll timeout */ #define USRCLK_FEATURE_ID 0x14 -const char USRCLK_GUID[] = "FFFFFFFFFFFFFFFFEEEEEEEEEEEEEEEE"; +const char USRCLK_GUID[] = "45AC5052C481412582380B8FF6C2F943"; + // DFHv0 struct dfh { @@ -160,12 +161,19 @@ static int using_iopll(char *sysfs_usrpath, const char *sysfs_path); bool is_file_empty(char* file_path) { FILE *fp = fopen(file_path, "r"); - if (feof(fp)){ + if(fp == NULL){ + printf("error opening file\n"); + return 1; + } + int c = fgetc(fp); + if(c == EOF) { + fclose(fp); return 1; - } else{ + } else { + ungetc(c, fp); + fclose(fp); return 0; - } - fclose(fp); + } } fpga_result usrclk_reset(uint8_t *uio_ptr) @@ -550,15 +558,34 @@ fpga_result get_usrclk_uio(const char *sysfs_path, } feature_gres = opae_glob(feature_path, GLOB_NOSORT, NULL, &feature_pglob); - //loop over feature_pglob.gl_pathv[i]check if above all files are empty, if all files empty then feature_gres = 1; - // need to do this because I see empty guid file in dfhv0 sysfs path - /sys/class/fpga_region/region0/dfl-port.0/dfl_dev.0/guid - // its possible feature_id can be empty by some mistake which would need to be flagged + /* Loop over sysfs feature_id file paths to check if any file is not empty, if all files are empty then we need non empty guid file or it's an error; + We need to do this because I see empty guid file in dfhv0 sysfs path eg - /sys/class/fpga_region/region0/dfl-port.0/dfl_dev.0/guid + It is possible feature_id can be empty by some mistake which would need to be flagged + */ + for (i = 0; i < feature_pglob.gl_pathc; i++) { + if(!is_file_empty(feature_pglob.gl_pathv[i])){ + feature_gres = 0; + break; + } else{ + feature_gres = 1; + } + } guid_gres = opae_glob(guid_path, GLOB_NOSORT, NULL, &guid_pglob); - // loop over guid_pglob.gl_pathv[i]check if above all files are empty, if all files empty then guid_gres = 1; - // need to do this because I see empty guid file in dfhv0 sysfs path - /sys/class/fpga_region/region0/dfl-port.0/dfl_dev.0/guid - printf("feature_gres = %d, guid gres = %d\n",feature_gres, guid_gres); + /* Loop over sysfs guid file paths to check if any file is not empty, if all files are empty then we need non empty feature_id file or it's an error; + We need to do this because I see empty guid file in dfhv0 sysfs path eg - /sys/class/fpga_region/region0/dfl-port.0/dfl_dev.0/guid + */ + for (i = 0; i < guid_pglob.gl_pathc; i++) { + if(!is_file_empty(guid_pglob.gl_pathv[i])){ + guid_gres = 0; + break; + } else{ + guid_gres = 1; + } + } + /* If we don't find guid or feature_id files in sysfs paths then error out + */ if ((feature_gres) && (guid_gres)) { OPAE_ERR("Failed pattern match feature id - %s: %s ,guid - %s: %s ", feature_path, strerror(errno), guid_path, strerror(errno)); @@ -567,13 +594,12 @@ fpga_result get_usrclk_uio(const char *sysfs_path, return FPGA_INVALID_PARAM; } - // guid gets priority over feature_id - // non-empty guid file being present in sysfs path (eg /sys/class/fpga_region/region0/dfl-port.0/dfl_dev.0/guid) implies user is using dfhv1 IP + /* guid gets priority over feature_id + non-empty guid file being present in sysfs path (eg /sys/class/fpga_region/region0/dfl-port.0/dfl_dev.0/guid) implies user is using dfhv1 IP + */ if (!guid_gres) { - printf("here 0\n"); pglob = guid_pglob; } else { - printf("here 1\n"); pglob = feature_pglob; } @@ -602,7 +628,6 @@ fpga_result get_usrclk_uio(const char *sysfs_path, } if ((value == feature_id) || (*guid == *userclk_guid)) { - printf("here 2\n"); res = FPGA_OK; char *p = strstr(pglob.gl_pathv[i], "dfl_dev"); if (p == NULL) { From 54949e83debd40f42cb3174eb87ec468a4481471 Mon Sep 17 00:00:00 2001 From: pajgaonk Date: Tue, 27 Feb 2024 15:19:11 -0800 Subject: [PATCH 3/3] code udpates for usrclk dfhv0 -> dfhv1 migration --- libraries/plugins/xfpga/usrclk/fpga_user_clk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/plugins/xfpga/usrclk/fpga_user_clk.c b/libraries/plugins/xfpga/usrclk/fpga_user_clk.c index 2dfed1202e10..71ba4103da1c 100644 --- a/libraries/plugins/xfpga/usrclk/fpga_user_clk.c +++ b/libraries/plugins/xfpga/usrclk/fpga_user_clk.c @@ -135,7 +135,7 @@ #define IOPLL_WRITE_POLL_TIMEOUT_US 1000000 /* Write poll timeout */ #define USRCLK_FEATURE_ID 0x14 -const char USRCLK_GUID[] = "45AC5052C481412582380B8FF6C2F943"; +const char USRCLK_GUID[] = "45AC5052C481412582380B8FF6C2F943"; // DFHv0