sbi_platform.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2020 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Atish Patra <atish.patra@wdc.com>
  8. */
  9. #include <sbi/sbi_console.h>
  10. #include <sbi/sbi_platform.h>
  11. #include <sbi/sbi_string.h>
  12. static inline char *sbi_platform_feature_id2string(unsigned long feature)
  13. {
  14. char *fstr = NULL;
  15. if (!feature)
  16. return NULL;
  17. switch (feature) {
  18. case SBI_PLATFORM_HAS_MFAULTS_DELEGATION:
  19. fstr = "medeleg";
  20. break;
  21. default:
  22. break;
  23. }
  24. return fstr;
  25. }
  26. void sbi_platform_get_features_str(const struct sbi_platform *plat,
  27. char *features_str, int nfstr)
  28. {
  29. unsigned long features, feat = 1UL;
  30. char *temp;
  31. int offset = 0;
  32. if (!plat || !features_str || !nfstr)
  33. return;
  34. sbi_memset(features_str, 0, nfstr);
  35. features = sbi_platform_get_features(plat);
  36. if (!features)
  37. goto done;
  38. do {
  39. if (features & feat) {
  40. temp = sbi_platform_feature_id2string(feat);
  41. if (temp) {
  42. int len = sbi_snprintf(features_str + offset,
  43. nfstr - offset,
  44. "%s,", temp);
  45. if (len < 0)
  46. break;
  47. if (offset + len >= nfstr) {
  48. /* No more space for features */
  49. offset = nfstr;
  50. break;
  51. } else
  52. offset = offset + len;
  53. }
  54. }
  55. feat = feat << 1;
  56. } while (feat <= SBI_PLATFORM_HAS_LAST_FEATURE);
  57. done:
  58. if (offset)
  59. features_str[offset - 1] = '\0';
  60. else
  61. sbi_strncpy(features_str, "none", nfstr);
  62. }
  63. u32 sbi_platform_hart_index(const struct sbi_platform *plat, u32 hartid)
  64. {
  65. u32 i;
  66. if (!plat)
  67. return -1U;
  68. if (plat->hart_index2id) {
  69. for (i = 0; i < plat->hart_count; i++) {
  70. if (plat->hart_index2id[i] == hartid)
  71. return i;
  72. }
  73. return -1U;
  74. }
  75. return hartid;
  76. }