1 /*
2  * Copyright (c) 2019 Elliot Berman
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 
9 #pragma once
10 
11 #include <lk/compiler.h>
12 #include <stdint.h>
13 #include <stdbool.h>
14 #include <sys/types.h>
15 #include <arch/riscv.h>
16 
17 __BEGIN_CDECLS
18 
19 #if !defined(RISCV_M_MODE) || !(RISCV_M_MODE)
20 
21 struct sbiret {
22     long error;
23     long value;
24 };
25 
26 enum sbi_return_code {
27     SBI_SUCCESS = 0,
28     SBI_ERR_FAILURE = -1,
29     SBI_ERR_NOT_SUPPORTED = -2,
30     SBI_ERR_INVALID_PARAM = -3,
31     SBI_ERR_DENIED = -4,
32     SBI_ERR_INVALID_ADDRESS = -5,
33 };
34 
35 #define SBI_SET_TIMER               0x00, 0
36 #define SBI_CONSOLE_PUTCHAR         0x01, 0
37 #define SBI_CONSOLE_GETCHAR         0x02, 0
38 #define SBI_CLEAR_IPI               0x03, 0
39 #define SBI_SEND_IPI                0x04, 0
40 #define SBI_REMOTE_FENCEI           0x05, 0
41 #define SBI_REMOTE_SFENCE_VMA       0x06, 0
42 #define SBI_REMOTE_SFENCE_VMA_ASID  0x07, 0
43 #define SBI_SHUTDOWN                0x08, 0
44 
45 #define SBI_GET_SBI_SPEC_VERSION    0x10, 0
46 #define SBI_GET_SBI_IMPL_ID         0x10, 1
47 #define SBI_GET_SBI_IMPL_VERSION    0x10, 2
48 #define SBI_PROBE_EXTENSION         0x10, 3
49 #define SBI_GET_MVENDORID           0x10, 4
50 #define SBI_GET_MARCHID             0x10, 5
51 #define SBI_GET_MIMPID              0x10, 6
52 
53 #define SBI_EXT_TIMER_SIG           0x54494d45
54 #define SBI_EXT_IPI_SIG             0x00735049
55 #define SBI_EXT_RFENCE_SIG          0x52464e43
56 #define SBI_EXT_HSM_SIG             0x0048534d
57 #define SBI_EXT_SRST_SIG            0x53525354
58 
59 void sbi_early_init(void);
60 void sbi_init(void);
61 
62 void sbi_set_timer(uint64_t stime_value);
63 void sbi_send_ipis(const unsigned long *hart_mask);
64 void sbi_clear_ipi(void);
65 status_t sbi_boot_hart(uint hartid, paddr_t start_addr, ulong arg);
66 
67 void sbi_rfence_vma(const unsigned long *hart_mask, vaddr_t vma, size_t size);
68 
69 bool sbi_probe_extension(ulong extension);
70 
71 struct sbiret sbi_generic_call_2(ulong extension, ulong function);
72 struct sbiret sbi_generic_call_3(ulong extension, ulong function);
73 
74 #endif
75 
76 __END_CDECLS
77 
78