blob: 187ca58dc0cf97022968cae8a4b6ab98c2a9c222 [file] [log] [blame]
Lukas Auer34a06262019-03-17 19:28:33 +01001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2015 Regents of the University of California
Bin Meng215c3a72020-03-09 19:35:27 -07004 * Copyright (c) 2020 Western Digital Corporation or its affiliates.
Lukas Auer34a06262019-03-17 19:28:33 +01005 *
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
Bin Meng215c3a72020-03-09 19:35:27 -070014#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
Lukas Auer34a06262019-03-17 19:28:33 +010023
Bin Mengfe136922020-03-06 00:44:16 -080024#define SBI_CALL(which, arg0, arg1, arg2, arg3) ({ \
Lukas Auer34a06262019-03-17 19:28:33 +010025 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); \
Bin Mengfe136922020-03-06 00:44:16 -080028 register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3); \
Lukas Auer34a06262019-03-17 19:28:33 +010029 register uintptr_t a7 asm ("a7") = (uintptr_t)(which); \
30 asm volatile ("ecall" \
31 : "+r" (a0) \
Bin Mengfe136922020-03-06 00:44:16 -080032 : "r" (a1), "r" (a2), "r" (a3), "r" (a7) \
Lukas Auer34a06262019-03-17 19:28:33 +010033 : "memory"); \
34 a0; \
35})
36
37/* Lazy implementations until SBI is finalized */
Bin Mengfe136922020-03-06 00:44:16 -080038#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)
Lukas Auer34a06262019-03-17 19:28:33 +010045
46static inline void sbi_console_putchar(int ch)
47{
Bin Meng215c3a72020-03-09 19:35:27 -070048 SBI_CALL_1(SBI_EXT_0_1_CONSOLE_PUTCHAR, ch);
Lukas Auer34a06262019-03-17 19:28:33 +010049}
50
51static inline int sbi_console_getchar(void)
52{
Bin Meng215c3a72020-03-09 19:35:27 -070053 return SBI_CALL_0(SBI_EXT_0_1_CONSOLE_GETCHAR);
Lukas Auer34a06262019-03-17 19:28:33 +010054}
55
56static inline void sbi_set_timer(uint64_t stime_value)
57{
58#if __riscv_xlen == 32
Bin Meng215c3a72020-03-09 19:35:27 -070059 SBI_CALL_2(SBI_EXT_0_1_SET_TIMER, stime_value, stime_value >> 32);
Lukas Auer34a06262019-03-17 19:28:33 +010060#else
Bin Meng215c3a72020-03-09 19:35:27 -070061 SBI_CALL_1(SBI_EXT_0_1_SET_TIMER, stime_value);
Lukas Auer34a06262019-03-17 19:28:33 +010062#endif
63}
64
65static inline void sbi_shutdown(void)
66{
Bin Meng215c3a72020-03-09 19:35:27 -070067 SBI_CALL_0(SBI_EXT_0_1_SHUTDOWN);
Lukas Auer34a06262019-03-17 19:28:33 +010068}
69
70static inline void sbi_clear_ipi(void)
71{
Bin Meng215c3a72020-03-09 19:35:27 -070072 SBI_CALL_0(SBI_EXT_0_1_CLEAR_IPI);
Lukas Auer34a06262019-03-17 19:28:33 +010073}
74
75static inline void sbi_send_ipi(const unsigned long *hart_mask)
76{
Bin Meng215c3a72020-03-09 19:35:27 -070077 SBI_CALL_1(SBI_EXT_0_1_SEND_IPI, hart_mask);
Lukas Auer34a06262019-03-17 19:28:33 +010078}
79
80static inline void sbi_remote_fence_i(const unsigned long *hart_mask)
81{
Bin Meng215c3a72020-03-09 19:35:27 -070082 SBI_CALL_1(SBI_EXT_0_1_REMOTE_FENCE_I, hart_mask);
Lukas Auer34a06262019-03-17 19:28:33 +010083}
84
85static inline void sbi_remote_sfence_vma(const unsigned long *hart_mask,
86 unsigned long start,
87 unsigned long size)
88{
Bin Meng215c3a72020-03-09 19:35:27 -070089 SBI_CALL_3(SBI_EXT_0_1_REMOTE_SFENCE_VMA, hart_mask, start, size);
Lukas Auer34a06262019-03-17 19:28:33 +010090}
91
92static 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{
Bin Meng215c3a72020-03-09 19:35:27 -070097 SBI_CALL_4(SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID, hart_mask,
98 start, size, asid);
Lukas Auer34a06262019-03-17 19:28:33 +010099}
100
101#endif