Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * Copyright (C) 2015 Regents of the University of California |
Bin Meng | 215c3a7 | 2020-03-09 19:35:27 -0700 | [diff] [blame^] | 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 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 | |
Bin Meng | 215c3a7 | 2020-03-09 19:35:27 -0700 | [diff] [blame^] | 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 |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 23 | |
Bin Meng | fe13692 | 2020-03-06 00:44:16 -0800 | [diff] [blame] | 24 | #define SBI_CALL(which, arg0, arg1, arg2, arg3) ({ \ |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 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); \ |
Bin Meng | fe13692 | 2020-03-06 00:44:16 -0800 | [diff] [blame] | 28 | register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3); \ |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 29 | register uintptr_t a7 asm ("a7") = (uintptr_t)(which); \ |
| 30 | asm volatile ("ecall" \ |
| 31 | : "+r" (a0) \ |
Bin Meng | fe13692 | 2020-03-06 00:44:16 -0800 | [diff] [blame] | 32 | : "r" (a1), "r" (a2), "r" (a3), "r" (a7) \ |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 33 | : "memory"); \ |
| 34 | a0; \ |
| 35 | }) |
| 36 | |
| 37 | /* Lazy implementations until SBI is finalized */ |
Bin Meng | fe13692 | 2020-03-06 00:44:16 -0800 | [diff] [blame] | 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) |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 45 | |
| 46 | static inline void sbi_console_putchar(int ch) |
| 47 | { |
Bin Meng | 215c3a7 | 2020-03-09 19:35:27 -0700 | [diff] [blame^] | 48 | SBI_CALL_1(SBI_EXT_0_1_CONSOLE_PUTCHAR, ch); |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static inline int sbi_console_getchar(void) |
| 52 | { |
Bin Meng | 215c3a7 | 2020-03-09 19:35:27 -0700 | [diff] [blame^] | 53 | return SBI_CALL_0(SBI_EXT_0_1_CONSOLE_GETCHAR); |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static inline void sbi_set_timer(uint64_t stime_value) |
| 57 | { |
| 58 | #if __riscv_xlen == 32 |
Bin Meng | 215c3a7 | 2020-03-09 19:35:27 -0700 | [diff] [blame^] | 59 | SBI_CALL_2(SBI_EXT_0_1_SET_TIMER, stime_value, stime_value >> 32); |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 60 | #else |
Bin Meng | 215c3a7 | 2020-03-09 19:35:27 -0700 | [diff] [blame^] | 61 | SBI_CALL_1(SBI_EXT_0_1_SET_TIMER, stime_value); |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 62 | #endif |
| 63 | } |
| 64 | |
| 65 | static inline void sbi_shutdown(void) |
| 66 | { |
Bin Meng | 215c3a7 | 2020-03-09 19:35:27 -0700 | [diff] [blame^] | 67 | SBI_CALL_0(SBI_EXT_0_1_SHUTDOWN); |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static inline void sbi_clear_ipi(void) |
| 71 | { |
Bin Meng | 215c3a7 | 2020-03-09 19:35:27 -0700 | [diff] [blame^] | 72 | SBI_CALL_0(SBI_EXT_0_1_CLEAR_IPI); |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static inline void sbi_send_ipi(const unsigned long *hart_mask) |
| 76 | { |
Bin Meng | 215c3a7 | 2020-03-09 19:35:27 -0700 | [diff] [blame^] | 77 | SBI_CALL_1(SBI_EXT_0_1_SEND_IPI, hart_mask); |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static inline void sbi_remote_fence_i(const unsigned long *hart_mask) |
| 81 | { |
Bin Meng | 215c3a7 | 2020-03-09 19:35:27 -0700 | [diff] [blame^] | 82 | SBI_CALL_1(SBI_EXT_0_1_REMOTE_FENCE_I, hart_mask); |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static inline void sbi_remote_sfence_vma(const unsigned long *hart_mask, |
| 86 | unsigned long start, |
| 87 | unsigned long size) |
| 88 | { |
Bin Meng | 215c3a7 | 2020-03-09 19:35:27 -0700 | [diff] [blame^] | 89 | SBI_CALL_3(SBI_EXT_0_1_REMOTE_SFENCE_VMA, hart_mask, start, size); |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 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 | { |
Bin Meng | 215c3a7 | 2020-03-09 19:35:27 -0700 | [diff] [blame^] | 97 | SBI_CALL_4(SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID, hart_mask, |
| 98 | start, size, asid); |
Lukas Auer | 34a0626 | 2019-03-17 19:28:33 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | #endif |