sbi_platform.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 = "mfdeleg";
  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. sbi_snprintf(features_str + offset, nfstr,
  43. "%s,", temp);
  44. offset = offset + sbi_strlen(temp) + 1;
  45. }
  46. }
  47. feat = feat << 1;
  48. } while (feat <= SBI_PLATFORM_HAS_LAST_FEATURE);
  49. done:
  50. if (offset)
  51. features_str[offset - 1] = '\0';
  52. else
  53. sbi_strncpy(features_str, "none", nfstr);
  54. }
  55. u32 sbi_platform_hart_index(const struct sbi_platform *plat, u32 hartid)
  56. {
  57. u32 i;
  58. if (!plat)
  59. return -1U;
  60. if (plat->hart_index2id) {
  61. for (i = 0; i < plat->hart_count; i++) {
  62. if (plat->hart_index2id[i] == hartid)
  63. return i;
  64. }
  65. return -1U;
  66. }
  67. return hartid;
  68. }