xref: /openbmc/linux/arch/riscv/include/asm/sbi.h (revision 2025cf9e)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2015 Regents of the University of California
4  */
5 
6 #ifndef _ASM_RISCV_SBI_H
7 #define _ASM_RISCV_SBI_H
8 
9 #include <linux/types.h>
10 
11 #define SBI_SET_TIMER 0
12 #define SBI_CONSOLE_PUTCHAR 1
13 #define SBI_CONSOLE_GETCHAR 2
14 #define SBI_CLEAR_IPI 3
15 #define SBI_SEND_IPI 4
16 #define SBI_REMOTE_FENCE_I 5
17 #define SBI_REMOTE_SFENCE_VMA 6
18 #define SBI_REMOTE_SFENCE_VMA_ASID 7
19 #define SBI_SHUTDOWN 8
20 
21 #define SBI_CALL(which, arg0, arg1, arg2, arg3) ({		\
22 	register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0);	\
23 	register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1);	\
24 	register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2);	\
25 	register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3);	\
26 	register uintptr_t a7 asm ("a7") = (uintptr_t)(which);	\
27 	asm volatile ("ecall"					\
28 		      : "+r" (a0)				\
29 		      : "r" (a1), "r" (a2), "r" (a3), "r" (a7)	\
30 		      : "memory");				\
31 	a0;							\
32 })
33 
34 /* Lazy implementations until SBI is finalized */
35 #define SBI_CALL_0(which) SBI_CALL(which, 0, 0, 0, 0)
36 #define SBI_CALL_1(which, arg0) SBI_CALL(which, arg0, 0, 0, 0)
37 #define SBI_CALL_2(which, arg0, arg1) SBI_CALL(which, arg0, arg1, 0, 0)
38 #define SBI_CALL_3(which, arg0, arg1, arg2) \
39 		SBI_CALL(which, arg0, arg1, arg2, 0)
40 #define SBI_CALL_4(which, arg0, arg1, arg2, arg3) \
41 		SBI_CALL(which, arg0, arg1, arg2, arg3)
42 
43 static inline void sbi_console_putchar(int ch)
44 {
45 	SBI_CALL_1(SBI_CONSOLE_PUTCHAR, ch);
46 }
47 
48 static inline int sbi_console_getchar(void)
49 {
50 	return SBI_CALL_0(SBI_CONSOLE_GETCHAR);
51 }
52 
53 static inline void sbi_set_timer(uint64_t stime_value)
54 {
55 #if __riscv_xlen == 32
56 	SBI_CALL_2(SBI_SET_TIMER, stime_value, stime_value >> 32);
57 #else
58 	SBI_CALL_1(SBI_SET_TIMER, stime_value);
59 #endif
60 }
61 
62 static inline void sbi_shutdown(void)
63 {
64 	SBI_CALL_0(SBI_SHUTDOWN);
65 }
66 
67 static inline void sbi_clear_ipi(void)
68 {
69 	SBI_CALL_0(SBI_CLEAR_IPI);
70 }
71 
72 static inline void sbi_send_ipi(const unsigned long *hart_mask)
73 {
74 	SBI_CALL_1(SBI_SEND_IPI, hart_mask);
75 }
76 
77 static inline void sbi_remote_fence_i(const unsigned long *hart_mask)
78 {
79 	SBI_CALL_1(SBI_REMOTE_FENCE_I, hart_mask);
80 }
81 
82 static inline void sbi_remote_sfence_vma(const unsigned long *hart_mask,
83 					 unsigned long start,
84 					 unsigned long size)
85 {
86 	SBI_CALL_3(SBI_REMOTE_SFENCE_VMA, hart_mask, start, size);
87 }
88 
89 static inline void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask,
90 					      unsigned long start,
91 					      unsigned long size,
92 					      unsigned long asid)
93 {
94 	SBI_CALL_4(SBI_REMOTE_SFENCE_VMA_ASID, hart_mask, start, size, asid);
95 }
96 
97 #endif
98