sbi.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
  13. unsigned long arg1, unsigned long arg2,
  14. unsigned long arg3, unsigned long arg4,
  15. unsigned long arg5)
  16. {
  17. struct sbiret ret;
  18. register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0);
  19. register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1);
  20. register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2);
  21. register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3);
  22. register uintptr_t a4 asm ("a4") = (uintptr_t)(arg4);
  23. register uintptr_t a5 asm ("a5") = (uintptr_t)(arg5);
  24. register uintptr_t a6 asm ("a6") = (uintptr_t)(fid);
  25. register uintptr_t a7 asm ("a7") = (uintptr_t)(ext);
  26. asm volatile ("ecall"
  27. : "+r" (a0), "+r" (a1)
  28. : "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7)
  29. : "memory");
  30. ret.error = a0;
  31. ret.value = a1;
  32. return ret;
  33. }
  34. /**
  35. * sbi_set_timer() - Program the timer for next timer event.
  36. * @stime_value: The value after which next timer event should fire.
  37. *
  38. * Return: None
  39. */
  40. void sbi_set_timer(uint64_t stime_value)
  41. {
  42. #if __riscv_xlen == 32
  43. sbi_ecall(SBI_EXT_SET_TIMER, SBI_FID_SET_TIMER, stime_value,
  44. stime_value >> 32, 0, 0, 0, 0);
  45. #else
  46. sbi_ecall(SBI_EXT_SET_TIMER, SBI_FID_SET_TIMER, stime_value,
  47. 0, 0, 0, 0, 0);
  48. #endif
  49. }
  50. /**
  51. * sbi_get_spec_version() - get current SBI specification version
  52. *
  53. * Return: version id
  54. */
  55. long sbi_get_spec_version(void)
  56. {
  57. struct sbiret ret;
  58. ret = sbi_ecall(SBI_EXT_BASE, SBI_EXT_BASE_GET_SPEC_VERSION,
  59. 0, 0, 0, 0, 0, 0);
  60. if (!ret.error)
  61. if (ret.value)
  62. return ret.value;
  63. return -ENOTSUPP;
  64. }
  65. /**
  66. * sbi_get_impl_id() - get SBI implementation ID
  67. *
  68. * Return: implementation ID
  69. */
  70. int sbi_get_impl_id(void)
  71. {
  72. struct sbiret ret;
  73. ret = sbi_ecall(SBI_EXT_BASE, SBI_EXT_BASE_GET_IMP_ID,
  74. 0, 0, 0, 0, 0, 0);
  75. if (!ret.error)
  76. if (ret.value)
  77. return ret.value;
  78. return -ENOTSUPP;
  79. }
  80. /**
  81. * sbi_probe_extension() - Check if an SBI extension ID is supported or not.
  82. * @extid: The extension ID to be probed.
  83. *
  84. * Return: Extension specific nonzero value f yes, -ENOTSUPP otherwise.
  85. */
  86. int sbi_probe_extension(int extid)
  87. {
  88. struct sbiret ret;
  89. ret = sbi_ecall(SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, extid,
  90. 0, 0, 0, 0, 0);
  91. if (!ret.error)
  92. if (ret.value)
  93. return ret.value;
  94. return -ENOTSUPP;
  95. }
  96. /**
  97. * sbi_srst_reset() - invoke system reset extension
  98. *
  99. * @type: type of reset
  100. * @reason: reason for reset
  101. */
  102. void sbi_srst_reset(unsigned long type, unsigned long reason)
  103. {
  104. sbi_ecall(SBI_EXT_SRST, SBI_EXT_SRST_RESET, type, reason,
  105. 0, 0, 0, 0);
  106. }
  107. #ifdef CONFIG_SBI_V01
  108. /**
  109. * sbi_console_putchar() - Writes given character to the console device.
  110. * @ch: The data to be written to the console.
  111. *
  112. * Return: None
  113. */
  114. void sbi_console_putchar(int ch)
  115. {
  116. sbi_ecall(SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, ch, 0, 0, 0, 0, 0);
  117. }
  118. /**
  119. * sbi_console_getchar() - Reads a byte from console device.
  120. *
  121. * Returns the value read from console.
  122. */
  123. int sbi_console_getchar(void)
  124. {
  125. struct sbiret ret;
  126. ret = sbi_ecall(SBI_EXT_0_1_CONSOLE_GETCHAR, 0, 0, 0, 0, 0, 0, 0);
  127. return ret.error;
  128. }
  129. /**
  130. * sbi_clear_ipi() - Clear any pending IPIs for the calling hart.
  131. *
  132. * Return: None
  133. */
  134. void sbi_clear_ipi(void)
  135. {
  136. sbi_ecall(SBI_EXT_0_1_CLEAR_IPI, 0, 0, 0, 0, 0, 0, 0);
  137. }
  138. /**
  139. * sbi_shutdown() - Remove all the harts from executing supervisor code.
  140. *
  141. * Return: None
  142. */
  143. void sbi_shutdown(void)
  144. {
  145. sbi_ecall(SBI_EXT_0_1_SHUTDOWN, 0, 0, 0, 0, 0, 0, 0);
  146. }
  147. /**
  148. * sbi_send_ipi() - Send an IPI to any hart.
  149. * @hart_mask: A cpu mask containing all the target harts.
  150. *
  151. * Return: None
  152. */
  153. void sbi_send_ipi(const unsigned long *hart_mask)
  154. {
  155. sbi_ecall(SBI_EXT_SEND_IPI, SBI_FID_SEND_IPI, (unsigned long)hart_mask,
  156. 0, 0, 0, 0, 0);
  157. }
  158. /**
  159. * sbi_remote_fence_i() - Execute FENCE.I instruction on given remote harts.
  160. * @hart_mask: A cpu mask containing all the target harts.
  161. *
  162. * Return: None
  163. */
  164. void sbi_remote_fence_i(const unsigned long *hart_mask)
  165. {
  166. sbi_ecall(SBI_EXT_REMOTE_FENCE_I, SBI_FID_REMOTE_FENCE_I,
  167. (unsigned long)hart_mask, 0, 0, 0, 0, 0);
  168. }
  169. /**
  170. * sbi_remote_sfence_vma() - Execute SFENCE.VMA instructions on given remote
  171. * harts for the specified virtual address range.
  172. * @hart_mask: A cpu mask containing all the target harts.
  173. * @start: Start of the virtual address
  174. * @size: Total size of the virtual address range.
  175. *
  176. * Return: None
  177. */
  178. void sbi_remote_sfence_vma(const unsigned long *hart_mask,
  179. unsigned long start,
  180. unsigned long size)
  181. {
  182. sbi_ecall(SBI_EXT_REMOTE_SFENCE_VMA, SBI_FID_REMOTE_SFENCE_VMA,
  183. (unsigned long)hart_mask, start, size, 0, 0, 0);
  184. }
  185. /**
  186. * sbi_remote_sfence_vma_asid() - Execute SFENCE.VMA instructions on given
  187. * remote harts for a virtual address range belonging to a specific ASID.
  188. *
  189. * @hart_mask: A cpu mask containing all the target harts.
  190. * @start: Start of the virtual address
  191. * @size: Total size of the virtual address range.
  192. * @asid: The value of address space identifier (ASID).
  193. *
  194. * Return: None
  195. */
  196. void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask,
  197. unsigned long start,
  198. unsigned long size,
  199. unsigned long asid)
  200. {
  201. sbi_ecall(SBI_EXT_REMOTE_SFENCE_VMA_ASID,
  202. SBI_FID_REMOTE_SFENCE_VMA_ASID,
  203. (unsigned long)hart_mask, start, size, asid, 0, 0);
  204. }
  205. #endif /* CONFIG_SBI_V01 */