1 /*
2  * Copyright (C) 2015 Regents of the University of California
3  *
4  *   This program is free software; you can redistribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation, version 2.
7  *
8  *   This program is distributed in the hope that it will be useful,
9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *   GNU General Public License for more details.
12  */
13 
14 #ifndef _ASM_RISCV_SBI_H
15 #define _ASM_RISCV_SBI_H
16 
17 #include <linux/types.h>
18 
19 #define SBI_SET_TIMER 0
20 #define SBI_CONSOLE_PUTCHAR 1
21 #define SBI_CONSOLE_GETCHAR 2
22 #define SBI_CLEAR_IPI 3
23 #define SBI_SEND_IPI 4
24 #define SBI_REMOTE_FENCE_I 5
25 #define SBI_REMOTE_SFENCE_VMA 6
26 #define SBI_REMOTE_SFENCE_VMA_ASID 7
27 #define SBI_SHUTDOWN 8
28 
29 #define SBI_CALL(which, arg0, arg1, arg2) ({			\
30 	register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0);	\
31 	register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1);	\
32 	register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2);	\
33 	register uintptr_t a7 asm ("a7") = (uintptr_t)(which);	\
34 	asm volatile ("ecall"					\
35 		      : "+r" (a0)				\
36 		      : "r" (a1), "r" (a2), "r" (a7)		\
37 		      : "memory");				\
38 	a0;							\
39 })
40 
41 /* Lazy implementations until SBI is finalized */
42 #define SBI_CALL_0(which) SBI_CALL(which, 0, 0, 0)
43 #define SBI_CALL_1(which, arg0) SBI_CALL(which, arg0, 0, 0)
44 #define SBI_CALL_2(which, arg0, arg1) SBI_CALL(which, arg0, arg1, 0)
45 
sbi_console_putchar(int ch)46 static inline void sbi_console_putchar(int ch)
47 {
48 	SBI_CALL_1(SBI_CONSOLE_PUTCHAR, ch);
49 }
50 
sbi_console_getchar(void)51 static inline int sbi_console_getchar(void)
52 {
53 	return SBI_CALL_0(SBI_CONSOLE_GETCHAR);
54 }
55 
sbi_set_timer(uint64_t stime_value)56 static inline void sbi_set_timer(uint64_t stime_value)
57 {
58 #if __riscv_xlen == 32
59 	SBI_CALL_2(SBI_SET_TIMER, stime_value, stime_value >> 32);
60 #else
61 	SBI_CALL_1(SBI_SET_TIMER, stime_value);
62 #endif
63 }
64 
sbi_shutdown(void)65 static inline void sbi_shutdown(void)
66 {
67 	SBI_CALL_0(SBI_SHUTDOWN);
68 }
69 
sbi_clear_ipi(void)70 static inline void sbi_clear_ipi(void)
71 {
72 	SBI_CALL_0(SBI_CLEAR_IPI);
73 }
74 
sbi_send_ipi(const unsigned long * hart_mask)75 static inline void sbi_send_ipi(const unsigned long *hart_mask)
76 {
77 	SBI_CALL_1(SBI_SEND_IPI, hart_mask);
78 }
79 
sbi_remote_fence_i(const unsigned long * hart_mask)80 static inline void sbi_remote_fence_i(const unsigned long *hart_mask)
81 {
82 	SBI_CALL_1(SBI_REMOTE_FENCE_I, hart_mask);
83 }
84 
sbi_remote_sfence_vma(const unsigned long * hart_mask,unsigned long start,unsigned long size)85 static inline void sbi_remote_sfence_vma(const unsigned long *hart_mask,
86 					 unsigned long start,
87 					 unsigned long size)
88 {
89 	SBI_CALL_1(SBI_REMOTE_SFENCE_VMA, hart_mask);
90 }
91 
sbi_remote_sfence_vma_asid(const unsigned long * hart_mask,unsigned long start,unsigned long size,unsigned long asid)92 static inline void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask,
93 					      unsigned long start,
94 					      unsigned long size,
95 					      unsigned long asid)
96 {
97 	SBI_CALL_1(SBI_REMOTE_SFENCE_VMA_ASID, hart_mask);
98 }
99 
100 #endif
101