187ca58dc0cf97022968cae8a4b6ab98c2a9c222
[oweals/u-boot.git] / arch / riscv / include / asm / sbi.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2015 Regents of the University of California
4  * Copyright (c) 2020 Western Digital Corporation or its affiliates.
5  *
6  * Taken from Linux arch/riscv/include/asm/sbi.h
7  */
8
9 #ifndef _ASM_RISCV_SBI_H
10 #define _ASM_RISCV_SBI_H
11
12 #include <linux/types.h>
13
14 #define SBI_EXT_0_1_SET_TIMER 0x0
15 #define SBI_EXT_0_1_CONSOLE_PUTCHAR 0x1
16 #define SBI_EXT_0_1_CONSOLE_GETCHAR 0x2
17 #define SBI_EXT_0_1_CLEAR_IPI 0x3
18 #define SBI_EXT_0_1_SEND_IPI 0x4
19 #define SBI_EXT_0_1_REMOTE_FENCE_I 0x5
20 #define SBI_EXT_0_1_REMOTE_SFENCE_VMA 0x6
21 #define SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID 0x7
22 #define SBI_EXT_0_1_SHUTDOWN 0x8
23
24 #define SBI_CALL(which, arg0, arg1, arg2, arg3) ({              \
25         register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0);   \
26         register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1);   \
27         register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2);   \
28         register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3);   \
29         register uintptr_t a7 asm ("a7") = (uintptr_t)(which);  \
30         asm volatile ("ecall"                                   \
31                       : "+r" (a0)                               \
32                       : "r" (a1), "r" (a2), "r" (a3), "r" (a7)  \
33                       : "memory");                              \
34         a0;                                                     \
35 })
36
37 /* Lazy implementations until SBI is finalized */
38 #define SBI_CALL_0(which) SBI_CALL(which, 0, 0, 0, 0)
39 #define SBI_CALL_1(which, arg0) SBI_CALL(which, arg0, 0, 0, 0)
40 #define SBI_CALL_2(which, arg0, arg1) SBI_CALL(which, arg0, arg1, 0, 0)
41 #define SBI_CALL_3(which, arg0, arg1, arg2) \
42                 SBI_CALL(which, arg0, arg1, arg2, 0)
43 #define SBI_CALL_4(which, arg0, arg1, arg2, arg3) \
44                 SBI_CALL(which, arg0, arg1, arg2, arg3)
45
46 static inline void sbi_console_putchar(int ch)
47 {
48         SBI_CALL_1(SBI_EXT_0_1_CONSOLE_PUTCHAR, ch);
49 }
50
51 static inline int sbi_console_getchar(void)
52 {
53         return SBI_CALL_0(SBI_EXT_0_1_CONSOLE_GETCHAR);
54 }
55
56 static inline void sbi_set_timer(uint64_t stime_value)
57 {
58 #if __riscv_xlen == 32
59         SBI_CALL_2(SBI_EXT_0_1_SET_TIMER, stime_value, stime_value >> 32);
60 #else
61         SBI_CALL_1(SBI_EXT_0_1_SET_TIMER, stime_value);
62 #endif
63 }
64
65 static inline void sbi_shutdown(void)
66 {
67         SBI_CALL_0(SBI_EXT_0_1_SHUTDOWN);
68 }
69
70 static inline void sbi_clear_ipi(void)
71 {
72         SBI_CALL_0(SBI_EXT_0_1_CLEAR_IPI);
73 }
74
75 static inline void sbi_send_ipi(const unsigned long *hart_mask)
76 {
77         SBI_CALL_1(SBI_EXT_0_1_SEND_IPI, hart_mask);
78 }
79
80 static inline void sbi_remote_fence_i(const unsigned long *hart_mask)
81 {
82         SBI_CALL_1(SBI_EXT_0_1_REMOTE_FENCE_I, hart_mask);
83 }
84
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_3(SBI_EXT_0_1_REMOTE_SFENCE_VMA, hart_mask, start, size);
90 }
91
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_4(SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID, hart_mask,
98                    start, size, asid);
99 }
100
101 #endif