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 |
|
---|
8 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
9 |
|
---|
10 | **/
|
---|
11 |
|
---|
12 | #include <Library/BaseLib.h>
|
---|
13 | #include <Library/BaseMemoryLib.h>
|
---|
14 | #include <Library/DebugLib.h>
|
---|
15 | #include <Library/BaseRiscVSbiLib.h>
|
---|
16 |
|
---|
17 | //
|
---|
18 | // Maximum arguments for SBI ecall
|
---|
19 | #define SBI_CALL_MAX_ARGS 6
|
---|
20 |
|
---|
21 | /**
|
---|
22 | Call SBI call using ecall instruction.
|
---|
23 |
|
---|
24 | Asserts when NumArgs exceeds SBI_CALL_MAX_ARGS.
|
---|
25 |
|
---|
26 | @param[in] ExtId SBI extension ID.
|
---|
27 | @param[in] FuncId SBI function ID.
|
---|
28 | @param[in] NumArgs Number of arguments to pass to the ecall.
|
---|
29 | @param[in] ... Argument list for the ecall.
|
---|
30 |
|
---|
31 | @retval Returns SBI_RET structure with value and error code.
|
---|
32 |
|
---|
33 | **/
|
---|
34 | STATIC
|
---|
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 | STATIC
|
---|
92 | EFI_STATUS
|
---|
93 | EFIAPI
|
---|
94 | TranslateError (
|
---|
95 | IN UINTN SbiError
|
---|
96 | )
|
---|
97 | {
|
---|
98 | switch (SbiError) {
|
---|
99 | case SBI_SUCCESS:
|
---|
100 | return EFI_SUCCESS;
|
---|
101 | case SBI_ERR_FAILED:
|
---|
102 | return EFI_DEVICE_ERROR;
|
---|
103 | break;
|
---|
104 | case SBI_ERR_NOT_SUPPORTED:
|
---|
105 | return EFI_UNSUPPORTED;
|
---|
106 | break;
|
---|
107 | case SBI_ERR_INVALID_PARAM:
|
---|
108 | return EFI_INVALID_PARAMETER;
|
---|
109 | break;
|
---|
110 | case SBI_ERR_DENIED:
|
---|
111 | return EFI_ACCESS_DENIED;
|
---|
112 | break;
|
---|
113 | case SBI_ERR_INVALID_ADDRESS:
|
---|
114 | return EFI_LOAD_ERROR;
|
---|
115 | break;
|
---|
116 | case SBI_ERR_ALREADY_AVAILABLE:
|
---|
117 | return EFI_ALREADY_STARTED;
|
---|
118 | break;
|
---|
119 | default:
|
---|
120 | //
|
---|
121 | // Reaches here only if SBI has defined a new error type
|
---|
122 | //
|
---|
123 | ASSERT (FALSE);
|
---|
124 | return EFI_UNSUPPORTED;
|
---|
125 | break;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | Clear pending timer interrupt bit and set timer for next event after Time.
|
---|
131 |
|
---|
132 | To clear the timer without scheduling a timer event, set Time to a
|
---|
133 | practically infinite value or mask the timer interrupt by clearing sie.STIE.
|
---|
134 |
|
---|
135 | @param[in] Time The time offset to the next scheduled timer interrupt.
|
---|
136 | **/
|
---|
137 | VOID
|
---|
138 | EFIAPI
|
---|
139 | SbiSetTimer (
|
---|
140 | IN UINT64 Time
|
---|
141 | )
|
---|
142 | {
|
---|
143 | SbiCall (SBI_EXT_TIME, SBI_EXT_TIME_SET_TIMER, 1, Time);
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | Reset the system using SRST SBI extenion
|
---|
148 |
|
---|
149 | @param[in] ResetType The SRST System Reset Type.
|
---|
150 | @param[in] ResetReason The SRST System Reset Reason.
|
---|
151 | **/
|
---|
152 | EFI_STATUS
|
---|
153 | EFIAPI
|
---|
154 | SbiSystemReset (
|
---|
155 | IN UINTN ResetType,
|
---|
156 | IN UINTN ResetReason
|
---|
157 | )
|
---|
158 | {
|
---|
159 | SBI_RET Ret;
|
---|
160 |
|
---|
161 | Ret = SbiCall (
|
---|
162 | SBI_EXT_SRST,
|
---|
163 | SBI_EXT_SRST_RESET,
|
---|
164 | 2,
|
---|
165 | ResetType,
|
---|
166 | ResetReason
|
---|
167 | );
|
---|
168 |
|
---|
169 | return TranslateError (Ret.Error);
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | Get firmware context of the calling hart.
|
---|
174 |
|
---|
175 | @param[out] FirmwareContext The firmware context pointer.
|
---|
176 | **/
|
---|
177 | VOID
|
---|
178 | EFIAPI
|
---|
179 | GetFirmwareContext (
|
---|
180 | OUT EFI_RISCV_FIRMWARE_CONTEXT **FirmwareContext
|
---|
181 | )
|
---|
182 | {
|
---|
183 | *FirmwareContext = (EFI_RISCV_FIRMWARE_CONTEXT *)RiscVGetSupervisorScratch ();
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | Set firmware context of the calling hart.
|
---|
188 |
|
---|
189 | @param[in] FirmwareContext The firmware context pointer.
|
---|
190 | **/
|
---|
191 | VOID
|
---|
192 | EFIAPI
|
---|
193 | SetFirmwareContext (
|
---|
194 | IN EFI_RISCV_FIRMWARE_CONTEXT *FirmwareContext
|
---|
195 | )
|
---|
196 | {
|
---|
197 | RiscVSetSupervisorScratch ((UINT64)FirmwareContext);
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | Get pointer to OpenSBI Firmware Context
|
---|
202 |
|
---|
203 | Get the pointer of firmware context through OpenSBI FW Extension SBI.
|
---|
204 |
|
---|
205 | @param FirmwareContextPtr Pointer to retrieve pointer to the
|
---|
206 | Firmware Context.
|
---|
207 | **/
|
---|
208 | VOID
|
---|
209 | EFIAPI
|
---|
210 | GetFirmwareContextPointer (
|
---|
211 | IN OUT EFI_RISCV_FIRMWARE_CONTEXT **FirmwareContextPtr
|
---|
212 | )
|
---|
213 | {
|
---|
214 | GetFirmwareContext (FirmwareContextPtr);
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | Set the pointer to OpenSBI Firmware Context
|
---|
219 |
|
---|
220 | Set the pointer of firmware context through OpenSBI FW Extension SBI.
|
---|
221 |
|
---|
222 | @param FirmwareContextPtr Pointer to Firmware Context.
|
---|
223 | **/
|
---|
224 | VOID
|
---|
225 | EFIAPI
|
---|
226 | SetFirmwareContextPointer (
|
---|
227 | IN EFI_RISCV_FIRMWARE_CONTEXT *FirmwareContextPtr
|
---|
228 | )
|
---|
229 | {
|
---|
230 | SetFirmwareContext (FirmwareContextPtr);
|
---|
231 | }
|
---|