sbi.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #ifdef CONFIG_SBI_V01
  51. /**
  52. * sbi_console_putchar() - Writes given character to the console device.
  53. * @ch: The data to be written to the console.
  54. *
  55. * Return: None
  56. */
  57. void sbi_console_putchar(int ch)
  58. {
  59. sbi_ecall(SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, ch, 0, 0, 0, 0, 0);
  60. }
  61. /**
  62. * sbi_console_getchar() - Reads a byte from console device.
  63. *
  64. * Returns the value read from console.
  65. */
  66. int sbi_console_getchar(void)
  67. {
  68. struct sbiret ret;
  69. ret = sbi_ecall(SBI_EXT_0_1_CONSOLE_GETCHAR, 0, 0, 0, 0, 0, 0, 0);
  70. return ret.error;
  71. }
  72. /**
  73. * sbi_clear_ipi() - Clear any pending IPIs for the calling hart.
  74. *
  75. * Return: None
  76. */
  77. void sbi_clear_ipi(void)
  78. {
  79. sbi_ecall(SBI_EXT_0_1_CLEAR_IPI, 0, 0, 0, 0, 0, 0, 0);
  80. }
  81. /**
  82. * sbi_shutdown() - Remove all the harts from executing supervisor code.
  83. *
  84. * Return: None
  85. */
  86. void sbi_shutdown(void)
  87. {
  88. sbi_ecall(SBI_EXT_0_1_SHUTDOWN, 0, 0, 0, 0, 0, 0, 0);
  89. }
  90. /**
  91. * sbi_send_ipi() - Send an IPI to any hart.
  92. * @hart_mask: A cpu mask containing all the target harts.
  93. *
  94. * Return: None
  95. */
  96. void sbi_send_ipi(const unsigned long *hart_mask)
  97. {
  98. sbi_ecall(SBI_EXT_SEND_IPI, SBI_FID_SEND_IPI, (unsigned long)hart_mask,
  99. 0, 0, 0, 0, 0);
  100. }
  101. /**
  102. * sbi_remote_fence_i() - Execute FENCE.I instruction on given remote harts.
  103. * @hart_mask: A cpu mask containing all the target harts.
  104. *
  105. * Return: None
  106. */
  107. void sbi_remote_fence_i(const unsigned long *hart_mask)
  108. {
  109. sbi_ecall(SBI_EXT_REMOTE_FENCE_I, SBI_FID_REMOTE_FENCE_I,
  110. (unsigned long)hart_mask, 0, 0, 0, 0, 0);
  111. }
  112. /**
  113. * sbi_remote_sfence_vma() - Execute SFENCE.VMA instructions on given remote
  114. * harts for the specified virtual address range.
  115. * @hart_mask: A cpu mask containing all the target harts.
  116. * @start: Start of the virtual address
  117. * @size: Total size of the virtual address range.
  118. *
  119. * Return: None
  120. */
  121. void sbi_remote_sfence_vma(const unsigned long *hart_mask,
  122. unsigned long start,
  123. unsigned long size)
  124. {
  125. sbi_ecall(SBI_EXT_REMOTE_SFENCE_VMA, SBI_FID_REMOTE_SFENCE_VMA,
  126. (unsigned long)hart_mask, start, size, 0, 0, 0);
  127. }
  128. /**
  129. * sbi_remote_sfence_vma_asid() - Execute SFENCE.VMA instructions on given
  130. * remote harts for a virtual address range belonging to a specific ASID.
  131. *
  132. * @hart_mask: A cpu mask containing all the target harts.
  133. * @start: Start of the virtual address
  134. * @size: Total size of the virtual address range.
  135. * @asid: The value of address space identifier (ASID).
  136. *
  137. * Return: None
  138. */
  139. void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask,
  140. unsigned long start,
  141. unsigned long size,
  142. unsigned long asid)
  143. {
  144. sbi_ecall(SBI_EXT_REMOTE_SFENCE_VMA_ASID,
  145. SBI_FID_REMOTE_SFENCE_VMA_ASID,
  146. (unsigned long)hart_mask, start, size, asid, 0, 0);
  147. }
  148. /**
  149. * sbi_probe_extension() - Check if an SBI extension ID is supported or not.
  150. * @extid: The extension ID to be probed.
  151. *
  152. * Return: Extension specific nonzero value f yes, -ENOTSUPP otherwise.
  153. */
  154. int sbi_probe_extension(int extid)
  155. {
  156. struct sbiret ret;
  157. ret = sbi_ecall(SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, extid,
  158. 0, 0, 0, 0, 0);
  159. if (!ret.error)
  160. if (ret.value)
  161. return ret.value;
  162. return -ENOTSUPP;
  163. }
  164. #endif /* CONFIG_SBI_V01 */