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 // Legacy SBI extensions (avoid using)
36 #define SBI_SET_TIMER               0x00, 0
37 #define SBI_CONSOLE_PUTCHAR         0x01, 0
38 #define SBI_CONSOLE_GETCHAR         0x02, 0
39 #define SBI_CLEAR_IPI               0x03, 0
40 #define SBI_SEND_IPI                0x04, 0
41 #define SBI_REMOTE_FENCEI           0x05, 0
42 #define SBI_REMOTE_SFENCE_VMA       0x06, 0
43 #define SBI_REMOTE_SFENCE_VMA_ASID  0x07, 0
44 #define SBI_SHUTDOWN                0x08, 0
45 
46 // Base extension
47 #define SBI_GET_SBI_SPEC_VERSION    0x10, 0
48 #define SBI_GET_SBI_IMPL_ID         0x10, 1
49 #define SBI_GET_SBI_IMPL_VERSION    0x10, 2
50 #define SBI_PROBE_EXTENSION         0x10, 3
51 #define SBI_GET_MVENDORID           0x10, 4
52 #define SBI_GET_MARCHID             0x10, 5
53 #define SBI_GET_MIMPID              0x10, 6
54 
55 // Extension signatures
56 // use SBI_PROBE_EXTENSION to discover
57 #define SBI_EXT_TIMER_SIG           0x54494d45 // TIME
58 #define SBI_EXT_IPI_SIG             0x00735049 // sPI
59 #define SBI_EXT_RFENCE_SIG          0x52464e43 // RFNC
60 #define SBI_EXT_HSM_SIG             0x0048534d // HSM
61 #define SBI_EXT_SRST_SIG            0x53525354 // SRST
62 #define SBI_EXT_PMU_SIG             0x00504d55 // PMU
63 #define SBI_EXT_DBCN_SIG            0x4442434e // DBCN
64 #define SBI_EXT_SUSP_SIG            0x53555350 // SUSP
65 #define SBI_EXT_CPPC_SIG            0x43505043 // CPPC
66 #define SBI_EXT_NACL_SIG            0x4e41434c // NACL
67 #define SBI_EXT_STA_SIG             0x00535441 // STA
68 
69 void sbi_early_init(void);
70 void sbi_init(void);
71 
72 void sbi_set_timer(uint64_t stime_value);
73 void sbi_send_ipis(const unsigned long *hart_mask);
74 void sbi_clear_ipi(void);
75 status_t sbi_boot_hart(uint hartid, paddr_t start_addr, ulong arg);
76 
77 void sbi_rfence_vma(const unsigned long *hart_mask, vaddr_t vma, size_t size);
78 
79 bool sbi_probe_extension(ulong extension);
80 
81 struct sbiret sbi_generic_call_2(ulong extension, ulong function);
82 struct sbiret sbi_generic_call_3(ulong extension, ulong function);
83 
84 #define SBI_RESET_TYPE_SHUTDOWN 0
85 #define SBI_RESET_TYPE_COLD_REBOOT 1
86 #define SBI_RESET_TYPE_WARM_REBOOT 2
87 #define SBI_RESET_TYPE_VENDOR_BASE (0xf0000000)
88 
89 #define SBI_RESET_REASON_NONE 0
90 #define SBI_RESET_REASON_SYSTEM_FAILURE 1
91 #define SBI_RESET_REASON_SBI_BASE (0xe0000000)
92 #define SBI_RESET_REASON_VENDOR_BASE (0xf0000000)
93 status_t sbi_system_reset(uint32_t type, uint32_t reason);
94 
95 #endif
96 
97 __END_CDECLS
98 
99