sbi.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SBI initialilization and all extension implementation.
  4. *
  5. * Copyright (c) 2020 Western Digital Corporation or its affiliates.
  6. *
  7. * Taken from Linux arch/riscv/kernel/sbi.c
  8. */
  9. #include <common.h>
  10. #include <asm/encoding.h>
  11. #include <asm/sbi.h>
  12. /* default SBI version is 0.1 */
  13. unsigned long sbi_spec_version = SBI_SPEC_VERSION_DEFAULT;
  14. struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
  15. unsigned long arg1, unsigned long arg2,
  16. unsigned long arg3, unsigned long arg4,
  17. unsigned long arg5)
  18. {
  19. struct sbiret ret;
  20. register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0);
  21. register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1);
  22. register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2);
  23. register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3);
  24. register uintptr_t a4 asm ("a4") = (uintptr_t)(arg4);
  25. register uintptr_t a5 asm ("a5") = (uintptr_t)(arg5);
  26. register uintptr_t a6 asm ("a6") = (uintptr_t)(fid);
  27. register uintptr_t a7 asm ("a7") = (uintptr_t)(ext);
  28. asm volatile ("ecall"
  29. : "+r" (a0), "+r" (a1)
  30. : "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7)
  31. : "memory");
  32. ret.error = a0;
  33. ret.value = a1;
  34. return ret;
  35. }
  36. /**
  37. * sbi_set_timer() - Program the timer for next timer event.
  38. * @stime_value: The value after which next timer event should fire.
  39. *
  40. * Return: None
  41. */
  42. void sbi_set_timer(uint64_t stime_value)
  43. {
  44. #if __riscv_xlen == 32
  45. sbi_ecall(SBI_EXT_SET_TIMER, SBI_FID_SET_TIMER, stime_value,
  46. stime_value >> 32, 0, 0, 0, 0);
  47. #else
  48. sbi_ecall(SBI_EXT_SET_TIMER, SBI_FID_SET_TIMER, stime_value,
  49. 0, 0, 0, 0, 0);
  50. #endif
  51. }
  52. #ifdef CONFIG_SBI_V01
  53. /**
  54. * sbi_console_putchar() - Writes given character to the console device.
  55. * @ch: The data to be written to the console.
  56. *
  57. * Return: None
  58. */
  59. void sbi_console_putchar(int ch)
  60. {
  61. sbi_ecall(SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, ch, 0, 0, 0, 0, 0);
  62. }
  63. /**
  64. * sbi_console_getchar() - Reads a byte from console device.
  65. *
  66. * Returns the value read from console.
  67. */
  68. int sbi_console_getchar(void)
  69. {
  70. struct sbiret ret;
  71. ret = sbi_ecall(SBI_EXT_0_1_CONSOLE_GETCHAR, 0, 0, 0, 0, 0, 0, 0);
  72. return ret.error;
  73. }
  74. /**
  75. * sbi_clear_ipi() - Clear any pending IPIs for the calling hart.
  76. *
  77. * Return: None
  78. */
  79. void sbi_clear_ipi(void)
  80. {
  81. sbi_ecall(SBI_EXT_0_1_CLEAR_IPI, 0, 0, 0, 0, 0, 0, 0);
  82. }
  83. /**
  84. * sbi_shutdown() - Remove all the harts from executing supervisor code.
  85. *
  86. * Return: None
  87. */
  88. void sbi_shutdown(void)
  89. {
  90. sbi_ecall(SBI_EXT_0_1_SHUTDOWN, 0, 0, 0, 0, 0, 0, 0);
  91. }
  92. /**
  93. * sbi_send_ipi() - Send an IPI to any hart.
  94. * @hart_mask: A cpu mask containing all the target harts.
  95. *
  96. * Return: None
  97. */
  98. void sbi_send_ipi(const unsigned long *hart_mask)
  99. {
  100. sbi_ecall(SBI_EXT_SEND_IPI, SBI_FID_SEND_IPI, (unsigned long)hart_mask,
  101. 0, 0, 0, 0, 0);
  102. }
  103. /**
  104. * sbi_remote_fence_i() - Execute FENCE.I instruction on given remote harts.
  105. * @hart_mask: A cpu mask containing all the target harts.
  106. *
  107. * Return: None
  108. */
  109. void sbi_remote_fence_i(const unsigned long *hart_mask)
  110. {
  111. sbi_ecall(SBI_EXT_REMOTE_FENCE_I, SBI_FID_REMOTE_FENCE_I,
  112. (unsigned long)hart_mask, 0, 0, 0, 0, 0);
  113. }
  114. /**
  115. * sbi_remote_sfence_vma() - Execute SFENCE.VMA instructions on given remote
  116. * harts for the specified virtual address range.
  117. * @hart_mask: A cpu mask containing all the target harts.
  118. * @start: Start of the virtual address
  119. * @size: Total size of the virtual address range.
  120. *
  121. * Return: None
  122. */
  123. void sbi_remote_sfence_vma(const unsigned long *hart_mask,
  124. unsigned long start,
  125. unsigned long size)
  126. {
  127. sbi_ecall(SBI_EXT_REMOTE_SFENCE_VMA, SBI_FID_REMOTE_SFENCE_VMA,
  128. (unsigned long)hart_mask, start, size, 0, 0, 0);
  129. }
  130. /**
  131. * sbi_remote_sfence_vma_asid() - Execute SFENCE.VMA instructions on given
  132. * remote harts for a virtual address range belonging to a specific ASID.
  133. *
  134. * @hart_mask: A cpu mask containing all the target harts.
  135. * @start: Start of the virtual address
  136. * @size: Total size of the virtual address range.
  137. * @asid: The value of address space identifier (ASID).
  138. *
  139. * Return: None
  140. */
  141. void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask,
  142. unsigned long start,
  143. unsigned long size,
  144. unsigned long asid)
  145. {
  146. sbi_ecall(SBI_EXT_REMOTE_SFENCE_VMA_ASID,
  147. SBI_FID_REMOTE_SFENCE_VMA_ASID,
  148. (unsigned long)hart_mask, start, size, asid, 0, 0);
  149. }
  150. /**
  151. * sbi_probe_extension() - Check if an SBI extension ID is supported or not.
  152. * @extid: The extension ID to be probed.
  153. *
  154. * Return: Extension specific nonzero value f yes, -ENOTSUPP otherwise.
  155. */
  156. int sbi_probe_extension(int extid)
  157. {
  158. struct sbiret ret;
  159. ret = sbi_ecall(SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, extid,
  160. 0, 0, 0, 0, 0);
  161. if (!ret.error)
  162. if (ret.value)
  163. return ret.value;
  164. return -ENOTSUPP;
  165. }
  166. #endif /* CONFIG_SBI_V01 */