1 | /** @file
|
---|
2 | Instance of the SBI ecall library.
|
---|
3 |
|
---|
4 | It allows calling an SBI function via an ecall from S-Mode.
|
---|
5 |
|
---|
6 | Copyright (c) 2021-2022, Hewlett Packard Development LP. All rights reserved.<BR>
|
---|
7 | Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
|
---|
8 |
|
---|
9 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
10 |
|
---|
11 | **/
|
---|
12 |
|
---|
13 | #include <Library/BaseLib.h>
|
---|
14 | #include <Library/BaseMemoryLib.h>
|
---|
15 | #include <Library/DebugLib.h>
|
---|
16 | #include <Library/BaseRiscVSbiLib.h>
|
---|
17 |
|
---|
18 | //
|
---|
19 | // Maximum arguments for SBI ecall
|
---|
20 | #define SBI_CALL_MAX_ARGS 6
|
---|
21 |
|
---|
22 | /**
|
---|
23 | Call SBI call using ecall instruction.
|
---|
24 |
|
---|
25 | Asserts when NumArgs exceeds SBI_CALL_MAX_ARGS.
|
---|
26 |
|
---|
27 | @param[in] ExtId SBI extension ID.
|
---|
28 | @param[in] FuncId SBI function ID.
|
---|
29 | @param[in] NumArgs Number of arguments to pass to the ecall.
|
---|
30 | @param[in] ... Argument list for the ecall.
|
---|
31 |
|
---|
32 | @retval Returns SBI_RET structure with value and error code.
|
---|
33 |
|
---|
34 | **/
|
---|
35 | SBI_RET
|
---|
36 | EFIAPI
|
---|
37 | SbiCall (
|
---|
38 | IN UINTN ExtId,
|
---|
39 | IN UINTN FuncId,
|
---|
40 | IN UINTN NumArgs,
|
---|
41 | ...
|
---|
42 | )
|
---|
43 | {
|
---|
44 | UINTN I;
|
---|
45 | SBI_RET Ret;
|
---|
46 | UINTN Args[SBI_CALL_MAX_ARGS];
|
---|
47 | VA_LIST ArgList;
|
---|
48 |
|
---|
49 | VA_START (ArgList, NumArgs);
|
---|
50 |
|
---|
51 | if (NumArgs > SBI_CALL_MAX_ARGS) {
|
---|
52 | Ret.Error = SBI_ERR_INVALID_PARAM;
|
---|
53 | Ret.Value = -1;
|
---|
54 | return Ret;
|
---|
55 | }
|
---|
56 |
|
---|
57 | for (I = 0; I < SBI_CALL_MAX_ARGS; I++) {
|
---|
58 | if (I < NumArgs) {
|
---|
59 | Args[I] = VA_ARG (ArgList, UINTN);
|
---|
60 | } else {
|
---|
61 | // Default to 0 for all arguments that are not given
|
---|
62 | Args[I] = 0;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | VA_END (ArgList);
|
---|
67 |
|
---|
68 | // ECALL updates the a0 and a1 registers as return values.
|
---|
69 | RiscVSbiEcall (
|
---|
70 | &Args[0],
|
---|
71 | &Args[1],
|
---|
72 | Args[2],
|
---|
73 | Args[3],
|
---|
74 | Args[4],
|
---|
75 | Args[5],
|
---|
76 | (UINTN)(FuncId),
|
---|
77 | (UINTN)(ExtId)
|
---|
78 | );
|
---|
79 |
|
---|
80 | Ret.Error = Args[0];
|
---|
81 | Ret.Value = Args[1];
|
---|
82 | return Ret;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | Translate SBI error code to EFI status.
|
---|
87 |
|
---|
88 | @param[in] SbiError SBI error code
|
---|
89 | @retval EFI_STATUS
|
---|
90 | **/
|
---|
91 | EFI_STATUS
|
---|
92 | EFIAPI
|
---|
93 | TranslateError (
|
---|
94 | IN UINTN SbiError
|
---|
95 | )
|
---|
96 | {
|
---|
97 | switch (SbiError) {
|
---|
98 | case SBI_SUCCESS:
|
---|
99 | return EFI_SUCCESS;
|
---|
100 | case SBI_ERR_FAILED:
|
---|
101 | return EFI_DEVICE_ERROR;
|
---|
102 | break;
|
---|
103 | case SBI_ERR_NOT_SUPPORTED:
|
---|
104 | return EFI_UNSUPPORTED;
|
---|
105 | break;
|
---|
106 | case SBI_ERR_INVALID_PARAM:
|
---|
107 | return EFI_INVALID_PARAMETER;
|
---|
108 | break;
|
---|
109 | case SBI_ERR_DENIED:
|
---|
110 | return EFI_ACCESS_DENIED;
|
---|
111 | break;
|
---|
112 | case SBI_ERR_INVALID_ADDRESS:
|
---|
113 | return EFI_LOAD_ERROR;
|
---|
114 | break;
|
---|
115 | case SBI_ERR_ALREADY_AVAILABLE:
|
---|
116 | return EFI_ALREADY_STARTED;
|
---|
117 | break;
|
---|
118 | default:
|
---|
119 | //
|
---|
120 | // Reaches here only if SBI has defined a new error type
|
---|
121 | //
|
---|
122 | ASSERT (FALSE);
|
---|
123 | return EFI_UNSUPPORTED;
|
---|
124 | break;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | Clear pending timer interrupt bit and set timer for next event after Time.
|
---|
130 |
|
---|
131 | To clear the timer without scheduling a timer event, set Time to a
|
---|
132 | practically infinite value or mask the timer interrupt by clearing sie.STIE.
|
---|
133 |
|
---|
134 | @param[in] Time The time offset to the next scheduled timer interrupt.
|
---|
135 | **/
|
---|
136 | VOID
|
---|
137 | EFIAPI
|
---|
138 | SbiSetTimer (
|
---|
139 | IN UINT64 Time
|
---|
140 | )
|
---|
141 | {
|
---|
142 | SbiCall (SBI_EXT_TIME, SBI_EXT_TIME_SET_TIMER, 1, Time);
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | Reset the system using SRST SBI extenion
|
---|
147 |
|
---|
148 | @param[in] ResetType The SRST System Reset Type.
|
---|
149 | @param[in] ResetReason The SRST System Reset Reason.
|
---|
150 | **/
|
---|
151 | EFI_STATUS
|
---|
152 | EFIAPI
|
---|
153 | SbiSystemReset (
|
---|
154 | IN UINTN ResetType,
|
---|
155 | IN UINTN ResetReason
|
---|
156 | )
|
---|
157 | {
|
---|
158 | SBI_RET Ret;
|
---|
159 |
|
---|
160 | Ret = SbiCall (
|
---|
161 | SBI_EXT_SRST,
|
---|
162 | SBI_EXT_SRST_RESET,
|
---|
163 | 2,
|
---|
164 | ResetType,
|
---|
165 | ResetReason
|
---|
166 | );
|
---|
167 |
|
---|
168 | return TranslateError (Ret.Error);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | Get firmware context of the calling hart.
|
---|
173 |
|
---|
174 | @param[out] FirmwareContext The firmware context pointer.
|
---|
175 | **/
|
---|
176 | VOID
|
---|
177 | EFIAPI
|
---|
178 | GetFirmwareContext (
|
---|
179 | OUT EFI_RISCV_FIRMWARE_CONTEXT **FirmwareContext
|
---|
180 | )
|
---|
181 | {
|
---|
182 | *FirmwareContext = (EFI_RISCV_FIRMWARE_CONTEXT *)RiscVGetSupervisorScratch ();
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | Set firmware context of the calling hart.
|
---|
187 |
|
---|
188 | @param[in] FirmwareContext The firmware context pointer.
|
---|
189 | **/
|
---|
190 | VOID
|
---|
191 | EFIAPI
|
---|
192 | SetFirmwareContext (
|
---|
193 | IN EFI_RISCV_FIRMWARE_CONTEXT *FirmwareContext
|
---|
194 | )
|
---|
195 | {
|
---|
196 | RiscVSetSupervisorScratch ((UINT64)FirmwareContext);
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | Get pointer to OpenSBI Firmware Context
|
---|
201 |
|
---|
202 | Get the pointer of firmware context through OpenSBI FW Extension SBI.
|
---|
203 |
|
---|
204 | @param FirmwareContextPtr Pointer to retrieve pointer to the
|
---|
205 | Firmware Context.
|
---|
206 | **/
|
---|
207 | VOID
|
---|
208 | EFIAPI
|
---|
209 | GetFirmwareContextPointer (
|
---|
210 | IN OUT EFI_RISCV_FIRMWARE_CONTEXT **FirmwareContextPtr
|
---|
211 | )
|
---|
212 | {
|
---|
213 | GetFirmwareContext (FirmwareContextPtr);
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | Set the pointer to OpenSBI Firmware Context
|
---|
218 |
|
---|
219 | Set the pointer of firmware context through OpenSBI FW Extension SBI.
|
---|
220 |
|
---|
221 | @param FirmwareContextPtr Pointer to Firmware Context.
|
---|
222 | **/
|
---|
223 | VOID
|
---|
224 | EFIAPI
|
---|
225 | SetFirmwareContextPointer (
|
---|
226 | IN EFI_RISCV_FIRMWARE_CONTEXT *FirmwareContextPtr
|
---|
227 | )
|
---|
228 | {
|
---|
229 | SetFirmwareContext (FirmwareContextPtr);
|
---|
230 | }
|
---|