hc
2024-03-22 a0752693d998599af469473b8dc239ef973a012f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
/** @file
  Instance of the SBI ecall library.
 
  It allows calling an SBI function via an ecall from S-Mode.
 
  The legacy extensions are not included because they are not necessary.
  They would be:
  - SbiLegacySetTimer            -> Use SbiSetTimer
  - SbiLegacyConsolePutChar      -> No replacement - Use regular UEFI functions
  - SbiLegacyConsoleGetChar      -> No replacement - Use regular UEFI functions
  - SbiLegacyClearIpi            -> Write 0 to SSIP
  - SbiLegacySendIpi             -> Use SbiSendIpi
  - SbiLegacyRemoteFenceI        -> Use SbiRemoteFenceI
  - SbiLegacyRemoteSfenceVma     -> Use SbiRemoteSfenceVma
  - SbiLegacyRemoteSfenceVmaAsid -> Use SbiRemoteSfenceVmaAsid
  - SbiLegacyShutdown            -> Wait for new System Reset extension
 
  Copyright (c) 2020, Hewlett Packard Development LP. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent
 
  @par Revision Reference:
    - OpenSBI Version 0.6
**/
 
#include <IndustryStandard/RiscVOpensbi.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/RiscVEdk2SbiLib.h>
#include <sbi/riscv_asm.h>
#include <sbi/sbi_hart.h>
#include <sbi/sbi_types.h>
#include <sbi/sbi_init.h>
 
 
//
// Maximum arguments for SBI ecall
// It's possible to pass more but no SBI call uses more as of SBI 0.2.
// The additional arguments would have to be passed on the stack instead of as
// registers, like it's done now.
//
#define SBI_CALL_MAX_ARGS 6
 
/**
  Call SBI call using ecall instruction.
 
  Asserts when NumArgs exceeds SBI_CALL_MAX_ARGS.
 
  @param[in] ExtId    SBI extension ID.
  @param[in] FuncId   SBI function ID.
  @param[in] NumArgs  Number of arguments to pass to the ecall.
  @param[in] ...      Argument list for the ecall.
 
  @retval  Returns SbiRet structure with value and error code.
 
**/
STATIC
SbiRet
EFIAPI
SbiCall(
  IN  UINTN ExtId,
  IN  UINTN FuncId,
  IN  UINTN NumArgs,
  ...
  )
{
    UINTN I;
    SbiRet Ret;
    UINTN Args[SBI_CALL_MAX_ARGS];
    VA_LIST ArgList;
    VA_START (ArgList, NumArgs);
 
    ASSERT (NumArgs <= SBI_CALL_MAX_ARGS);
 
    for (I = 0; I < SBI_CALL_MAX_ARGS; I++) {
      if (I < NumArgs) {
        Args[I] = VA_ARG (ArgList, UINTN);
      } else {
        // Default to 0 for all arguments that are not given
        Args[I] = 0;
      }
    }
 
    VA_END(ArgList);
 
    register UINTN a0 asm ("a0") = Args[0];
    register UINTN a1 asm ("a1") = Args[1];
    register UINTN a2 asm ("a2") = Args[2];
    register UINTN a3 asm ("a3") = Args[3];
    register UINTN a4 asm ("a4") = Args[4];
    register UINTN a5 asm ("a5") = Args[5];
    register UINTN a6 asm ("a6") = (UINTN)(FuncId);
    register UINTN a7 asm ("a7") = (UINTN)(ExtId);
    asm volatile ("ecall" \
         : "+r" (a0), "+r" (a1) \
         : "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7) \
         : "memory"); \
    Ret.Error = a0;
    Ret.Value = a1;
    return Ret;
}
 
/**
  Translate SBI error code to EFI status.
 
  @param[in] SbiError   SBI error code
  @retval EFI_STATUS
**/
 
STATIC
EFI_STATUS
EFIAPI
TranslateError(
  IN  UINTN SbiError
  )
{
  switch (SbiError) {
    case SBI_SUCCESS:
      return EFI_SUCCESS;
    case SBI_ERR_FAILED:
      return EFI_DEVICE_ERROR;
      break;
    case SBI_ERR_NOT_SUPPORTED:
      return EFI_UNSUPPORTED;
      break;
    case SBI_ERR_INVALID_PARAM:
      return EFI_INVALID_PARAMETER;
      break;
    case SBI_ERR_DENIED:
      return EFI_ACCESS_DENIED;
      break;
    case SBI_ERR_INVALID_ADDRESS:
      return EFI_LOAD_ERROR;
      break;
    case SBI_ERR_ALREADY_AVAILABLE:
      return EFI_ALREADY_STARTED;
      break;
    default:
      //
      // Reaches here only if SBI has defined a new error type
      //
      ASSERT (FALSE);
      return EFI_UNSUPPORTED;
      break;
  }
}
 
//
// OpenSBI library interface function for the base extension
//
 
/**
  Get the implemented SBI specification version
 
  The minor number of the SBI specification is encoded in the low 24 bits,
  with the major number encoded in the next 7 bits.  Bit 32 must be 0 and is
  reserved for future expansion.
 
  @param[out] SpecVersion          The Version of the SBI specification.
**/
VOID
EFIAPI
SbiGetSpecVersion (
  OUT UINTN                       *SpecVersion
  )
{
  SbiRet Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_GET_SPEC_VERSION, 0);
 
  if (!Ret.Error) {
    *SpecVersion = (UINTN)Ret.Value;
  }
}
 
/**
  Get the SBI implementation ID
 
  This ID is used to idenetify a specific SBI implementation in order to work
  around any quirks it might have.
 
  @param[out] ImplId               The ID of the SBI implementation.
**/
VOID
EFIAPI
SbiGetImplId (
  OUT UINTN                       *ImplId
  )
{
  SbiRet Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_GET_IMP_ID, 0);
  *ImplId = (UINTN)Ret.Value;
}
 
/**
  Get the SBI implementation version
 
  The version of this SBI implementation.
  The encoding of this number is determined by the specific SBI implementation.
 
  @param[out] ImplVersion          The version of the SBI implementation.
**/
VOID
EFIAPI
SbiGetImplVersion (
  OUT UINTN                       *ImplVersion
  )
{
  SbiRet Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_GET_IMP_VERSION, 0);
  *ImplVersion = (UINTN)Ret.Value;
}
 
/**
  Probe whether an SBI extension is available
 
  ProbeResult is set to 0 if the extension is not available or to an extension
  specified value if it is available.
 
  @param[in]  ExtensionId          The extension ID.
  @param[out] ProbeResult          The return value of the probe.
**/
VOID
EFIAPI
SbiProbeExtension (
  IN  INTN                         ExtensionId,
  OUT INTN                        *ProbeResult
  )
{
  SbiRet Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 0);
  *ProbeResult = (UINTN)Ret.Value;
}
 
/**
  Get the CPU's vendor ID
 
  Reads the mvendorid CSR.
 
  @param[out] MachineVendorId      The CPU's vendor ID.
**/
VOID
EFIAPI
SbiGetMachineVendorId (
  OUT UINTN                       *MachineVendorId
  )
{
  SbiRet Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_GET_MVENDORID, 0);
  *MachineVendorId = (UINTN)Ret.Value;
}
 
/**
  Get the CPU's architecture ID
 
  Reads the marchid CSR.
 
  @param[out] MachineArchId        The CPU's architecture ID.
**/
VOID
EFIAPI
SbiGetMachineArchId (
  OUT UINTN                       *MachineArchId
  )
{
  SbiRet Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_GET_MARCHID, 0);
  *MachineArchId = (UINTN)Ret.Value;
}
 
/**
  Get the CPU's architecture ID
 
  Reads the marchid CSR.
 
  @param[out] MachineImplId        The CPU's implementation ID.
**/
VOID
EFIAPI
SbiGetMachineImplId (
  OUT UINTN                       *MachineImplId
  )
{
  SbiRet Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_GET_MIMPID, 0);
  *MachineImplId = (UINTN)Ret.Value;
}
 
//
// SBI interface function for the hart state management extension
//
 
/**
  Politely ask the SBI to start a given hart.
 
  This call may return before the hart has actually started executing, if the
  SBI implementation can guarantee that the hart is actually going to start.
 
  Before the hart jumps to StartAddr, the hart MUST configure PMP if present
  and switch to S-mode.
 
  @param[in]  HartId               The id of the hart to start.
  @param[in]  StartAddr            The physical address, where the hart starts
                                   executing from.
  @param[in]  Priv                 An XLEN-bit value, which will be in register
                                   a1 when the hart starts.
  @retval EFI_SUCCESS              Hart was stopped and will start executing from StartAddr.
  @retval EFI_LOAD_ERROR           StartAddr is not valid, possibly due to following reasons:
                                     - It is not a valid physical address.
                                     - The address is prohibited by PMP to run in
                                       supervisor mode.
  @retval EFI_INVALID_PARAMETER    HartId is not a valid hart id
  @retval EFI_ALREADY_STARTED      The hart is already running.
  @retval other                    The start request failed for unknown reasons.
**/
EFI_STATUS
EFIAPI
SbiHartStart (
  IN  UINTN                          HartId,
  IN  UINTN                          StartAddr,
  IN  UINTN                          Priv
  )
{
  SbiRet Ret = SbiCall (
                 SBI_EXT_HSM,
                 SBI_EXT_HSM_HART_START,
                 3,
                 HartId,
                 StartAddr,
                 Priv
                 );
  return TranslateError (Ret.Error);
}
 
/**
  Return execution of the calling hart to SBI.
 
  MUST be called in S-Mode with user interrupts disabled.
  This call is not expected to return, unless a failure occurs.
 
  @retval     EFI_SUCCESS          Never occurs. When successful, the call does not return.
  @retval     other                Failed to stop hard for an unknown reason.
**/
EFI_STATUS
EFIAPI
SbiHartStop (
  )
{
  SbiRet Ret = SbiCall (SBI_EXT_HSM, SBI_EXT_HSM_HART_STOP, 0);
  return TranslateError (Ret.Error);
}
 
/**
  Get the current status of a hart.
 
  Since harts can transition between states at any time, the status retrieved
  by this function may already be out of date, once it returns.
 
  Possible values for HartStatus are:
  0: STARTED
  1: STOPPED
  2: START_REQUEST_PENDING
  3: STOP_REQUEST_PENDING
 
  @param[out] HartStatus           The pointer in which the hart's status is
                                   stored.
  @retval EFI_SUCCESS              The operation succeeds.
  @retval EFI_INVALID_PARAMETER    A parameter is invalid.
**/
EFI_STATUS
EFIAPI
SbiHartGetStatus (
  IN  UINTN                          HartId,
  OUT UINTN                         *HartStatus
  )
{
  SbiRet Ret = SbiCall (SBI_EXT_HSM, SBI_EXT_HSM_HART_GET_STATUS, 1, HartId);
 
  if (!Ret.Error) {
    *HartStatus = (UINTN)Ret.Value;
  }
 
  return TranslateError (Ret.Error);
}
 
/**
  Clear pending timer interrupt bit and set timer for next event after Time.
 
  To clear the timer without scheduling a timer event, set Time to a
  practically infinite value or mask the timer interrupt by clearing sie.STIE.
 
  @param[in]  Time                 The time offset to the next scheduled timer interrupt.
**/
VOID
EFIAPI
SbiSetTimer (
  IN  UINT64                         Time
  )
{
  SbiCall (SBI_EXT_TIME, SBI_EXT_TIME_SET_TIMER, 1, Time);
}
 
EFI_STATUS
EFIAPI
SbiSendIpi (
  IN  UINTN                         *HartMask,
  IN  UINTN                          HartMaskBase
  )
{
  SbiRet Ret = SbiCall (
                 SBI_EXT_IPI,
                 SBI_EXT_IPI_SEND_IPI,
                 2,
                 (UINTN)HartMask,
                 HartMaskBase
                 );
  return TranslateError (Ret.Error);
}
 
/**
  Instructs remote harts to execute a FENCE.I instruction.
 
  @param[in]  HartMask             Scalar bit-vector containing hart ids
  @param[in]  HartMaskBase         The starting hartid from which the bit-vector
                                   must be computed. If set to -1, HartMask is
                                   ignored and all harts are considered.
  @retval EFI_SUCCESS              IPI was sent to all the targeted harts.
  @retval EFI_INVALID_PARAMETER    Either hart_mask_base or any of the hartid
                                   from hart_mask is not valid i.e. either the
                                   hartid is not enabled by the platform or is
                                   not available to the supervisor.
**/
EFI_STATUS
EFIAPI
SbiRemoteFenceI (
  IN  UINTN                         *HartMask,
  IN  UINTN                          HartMaskBase
  )
{
  SbiRet Ret = SbiCall (
                 SBI_EXT_RFENCE,
                 SBI_EXT_RFENCE_REMOTE_FENCE_I,
                 2,
                 (UINTN)HartMask,
                 HartMaskBase
                 );
  return TranslateError (Ret.Error);
}
 
/**
  Instructs the remote harts to execute one or more SFENCE.VMA instructions.
 
  The SFENCE.VMA covers the range of virtual addresses between StartAaddr and Size.
 
  The remote fence function acts as a full tlb flush if * StartAddr and size
  are both 0 * size is equal to 2^XLEN-1
 
  @param[in]  HartMask             Scalar bit-vector containing hart ids
  @param[in]  HartMaskBase         The starting hartid from which the bit-vector
                                   must be computed. If set to -1, HartMask is
                                   ignored and all harts are considered.
  @param[in]  StartAddr            The first address of the affected range.
  @param[in]  Size                 How many addresses are affected.
  @retval EFI_SUCCESS              IPI was sent to all the targeted harts.
  @retval EFI_LOAD_ERROR           StartAddr or Size is not valid.
  @retval EFI_INVALID_PARAMETER    Either hart_mask_base or any of the hartid
                                   from hart_mask is not valid i.e. either the
                                   hartid is not enabled by the platform or is
                                   not available to the supervisor.
**/
EFI_STATUS
EFIAPI
SbiRemoteSfenceVma (
  IN  UINTN                         *HartMask,
  IN  UINTN                          HartMaskBase,
  IN  UINTN                          StartAddr,
  IN  UINTN                          Size
  )
{
  SbiRet Ret = SbiCall (
                 SBI_EXT_RFENCE,
                 SBI_EXT_RFENCE_REMOTE_SFENCE_VMA,
                 4,
                 (UINTN)HartMask,
                 HartMaskBase,
                 StartAddr,
                 Size
                 );
  return TranslateError (Ret.Error);
}
 
/**
  Instructs the remote harts to execute one or more SFENCE.VMA instructions.
 
  The SFENCE.VMA covers the range of virtual addresses between StartAaddr and Size.
  Covers only the given ASID.
 
  The remote fence function acts as a full tlb flush if * StartAddr and size
  are both 0 * size is equal to 2^XLEN-1
 
  @param[in]  HartMask             Scalar bit-vector containing hart ids
  @param[in]  HartMaskBase         The starting hartid from which the bit-vector
                                   must be computed. If set to -1, HartMask is
                                   ignored and all harts are considered.
  @param[in]  StartAddr            The first address of the affected range.
  @param[in]  Size                 How many addresses are affected.
  @retval EFI_SUCCESS              IPI was sent to all the targeted harts.
  @retval EFI_LOAD_ERROR           StartAddr or Size is not valid.
  @retval EFI_INVALID_PARAMETER    Either hart_mask_base or any of the hartid
                                   from hart_mask is not valid i.e. either the
                                   hartid is not enabled by the platform or is
                                   not available to the supervisor.
**/
EFI_STATUS
EFIAPI
SbiRemoteSfenceVmaAsid (
  IN  UINTN                         *HartMask,
  IN  UINTN                          HartMaskBase,
  IN  UINTN                          StartAddr,
  IN  UINTN                          Size,
  IN  UINTN                          Asid
  )
{
  SbiRet Ret = SbiCall (
                 SBI_EXT_RFENCE,
                 SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID,
                 5,
                 (UINTN)HartMask,
                 HartMaskBase,
                 StartAddr,
                 Size,
                 Asid
                 );
  return TranslateError (Ret.Error);
}
 
/**
  Instructs the remote harts to execute one or more SFENCE.GVMA instructions.
 
  The SFENCE.GVMA covers the range of virtual addresses between StartAaddr and Size.
  Covers only the given VMID.
  This function call is only valid for harts implementing the hypervisor extension.
 
  The remote fence function acts as a full tlb flush if * StartAddr and size
  are both 0 * size is equal to 2^XLEN-1
 
  @param[in]  HartMask             Scalar bit-vector containing hart ids
  @param[in]  HartMaskBase         The starting hartid from which the bit-vector
                                   must be computed. If set to -1, HartMask is
                                   ignored and all harts are considered.
  @param[in]  StartAddr            The first address of the affected range.
  @param[in]  Size                 How many addresses are affected.
  @retval EFI_SUCCESS              IPI was sent to all the targeted harts.
  @retval EFI_LOAD_ERROR           StartAddr or Size is not valid.
  @retval EFI_UNSUPPORTED          SBI does not implement this function or one
                                   of the target harts does not support the
                                   hypervisor extension.
  @retval EFI_INVALID_PARAMETER    Either hart_mask_base or any of the hartid
                                   from hart_mask is not valid i.e. either the
                                   hartid is not enabled by the platform or is
                                   not available to the supervisor.
**/
EFI_STATUS
EFIAPI
SbiRemoteHFenceGvmaVmid (
  IN  UINTN                         *HartMask,
  IN  UINTN                          HartMaskBase,
  IN  UINTN                          StartAddr,
  IN  UINTN                          Size,
  IN  UINTN                          Vmid
  )
{
  SbiRet Ret = SbiCall (
                 SBI_EXT_RFENCE,
                 SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA,
                 5,
                 (UINTN)HartMask,
                 HartMaskBase,
                 StartAddr,
                 Size,
                 Vmid
                 );
  return TranslateError (Ret.Error);
}
 
/**
  Instructs the remote harts to execute one or more SFENCE.GVMA instructions.
 
  The SFENCE.GVMA covers the range of virtual addresses between StartAaddr and Size.
  This function call is only valid for harts implementing the hypervisor extension.
 
  The remote fence function acts as a full tlb flush if * StartAddr and size
  are both 0 * size is equal to 2^XLEN-1
 
  @param[in]  HartMask             Scalar bit-vector containing hart ids
  @param[in]  HartMaskBase         The starting hartid from which the bit-vector
                                   must be computed. If set to -1, HartMask is
                                   ignored and all harts are considered.
  @param[in]  StartAddr            The first address of the affected range.
  @param[in]  Size                 How many addresses are affected.
  @retval EFI_SUCCESS              IPI was sent to all the targeted harts.
  @retval EFI_LOAD_ERROR           StartAddr or Size is not valid.
  @retval EFI_UNSUPPORTED          SBI does not implement this function or one
                                   of the target harts does not support the
                                   hypervisor extension.
  @retval EFI_INVALID_PARAMETER    Either hart_mask_base or any of the hartid
                                   from hart_mask is not valid i.e. either the
                                   hartid is not enabled by the platform or is
                                   not available to the supervisor.
**/
EFI_STATUS
EFIAPI
SbiRemoteHFenceGvma (
  IN  UINTN                         *HartMask,
  IN  UINTN                          HartMaskBase,
  IN  UINTN                          StartAddr,
  IN  UINTN                          Size
  )
{
  SbiRet Ret = SbiCall (
                 SBI_EXT_RFENCE,
                 SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID,
                 4,
                 (UINTN)HartMask,
                 HartMaskBase,
                 StartAddr,
                 Size
                 );
  return TranslateError (Ret.Error);
}
 
/**
  Instructs the remote harts to execute one or more SFENCE.VVMA instructions.
 
  The SFENCE.GVMA covers the range of virtual addresses between StartAaddr and Size.
  Covers only the given ASID.
  This function call is only valid for harts implementing the hypervisor extension.
 
  The remote fence function acts as a full tlb flush if * StartAddr and size
  are both 0 * size is equal to 2^XLEN-1
 
  @param[in]  HartMask             Scalar bit-vector containing hart ids
  @param[in]  HartMaskBase         The starting hartid from which the bit-vector
                                   must be computed. If set to -1, HartMask is
                                   ignored and all harts are considered.
  @param[in]  StartAddr            The first address of the affected range.
  @param[in]  Size                 How many addresses are affected.
  @retval EFI_SUCCESS              IPI was sent to all the targeted harts.
  @retval EFI_LOAD_ERROR           StartAddr or Size is not valid.
  @retval EFI_UNSUPPORTED          SBI does not implement this function or one
                                   of the target harts does not support the
                                   hypervisor extension.
  @retval EFI_INVALID_PARAMETER    Either hart_mask_base or any of the hartid
                                   from hart_mask is not valid i.e. either the
                                   hartid is not enabled by the platform or is
                                   not available to the supervisor.
**/
EFI_STATUS
EFIAPI
SbiRemoteHFenceVvmaAsid (
  IN  UINTN                         *HartMask,
  IN  UINTN                          HartMaskBase,
  IN  UINTN                          StartAddr,
  IN  UINTN                          Size,
  IN  UINTN                          Asid
  )
{
  SbiRet Ret = SbiCall (
                 SBI_EXT_RFENCE,
                 SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA,
                 5,
                 (UINTN)HartMask,
                 HartMaskBase,
                 StartAddr,
                 Size,
                 Asid
                 );
  return TranslateError (Ret.Error);
}
 
/**
  Instructs the remote harts to execute one or more SFENCE.VVMA instructions.
 
  The SFENCE.GVMA covers the range of virtual addresses between StartAaddr and Size.
  This function call is only valid for harts implementing the hypervisor extension.
 
  The remote fence function acts as a full tlb flush if * StartAddr and size
  are both 0 * size is equal to 2^XLEN-1
 
  @param[in]  HartMask             Scalar bit-vector containing hart ids
  @param[in]  HartMaskBase         The starting hartid from which the bit-vector
                                   must be computed. If set to -1, HartMask is
                                   ignored and all harts are considered.
  @param[in]  StartAddr            The first address of the affected range.
  @param[in]  Size                 How many addresses are affected.
  @retval EFI_SUCCESS              IPI was sent to all the targeted harts.
  @retval EFI_LOAD_ERROR           StartAddr or Size is not valid.
  @retval EFI_UNSUPPORTED          SBI does not implement this function or one
                                   of the target harts does not support the
                                   hypervisor extension.
  @retval EFI_INVALID_PARAMETER    Either hart_mask_base or any of the hartid
                                   from hart_mask is not valid i.e. either the
                                   hartid is not enabled by the platform or is
                                   not available to the supervisor.
**/
EFI_STATUS
EFIAPI
SbiRemoteHFenceVvma (
  IN  UINTN                         *HartMask,
  IN  UINTN                          HartMaskBase,
  IN  UINTN                          StartAddr,
  IN  UINTN                          Size
  )
{
  SbiRet Ret = SbiCall (
                 SBI_EXT_RFENCE,
                 SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID,
                 4,
                 (UINTN)HartMask,
                 HartMaskBase,
                 StartAddr,
                 Size
                 );
  return TranslateError (Ret.Error);
}
 
//
// SBI interface function for the vendor extension
//
 
/**
  Call a function in a vendor defined SBI extension
 
  ASSERT() if the ExtensionId is not in the designated SBI Vendor Extension
  Space or NumArgs exceeds SBI_CALL_MAX_ARGS.
 
  @param[in]  ExtensionId          The SBI vendor extension ID.
  @param[in]  FunctionId           The function ID to call in this extension.
  @param[in]  NumArgs              How many arguments are passed.
  @param[in]  ...                  Actual Arguments to the function.
  @retval EFI_SUCCESS if the SBI function was called and it was successful
  @retval others if the called SBI function returns an error
**/
EFI_STATUS
EFIAPI
SbiVendorCall (
  IN  UINTN                          ExtensionId,
  IN  UINTN                          FunctionId,
  IN  UINTN                          NumArgs,
  ...
  )
{
    SbiRet Ret;
    VA_LIST Args;
    VA_START (Args, NumArgs);
 
    ASSERT (ExtensionId >= SBI_EXT_VENDOR_START && ExtensionId <= SBI_EXT_VENDOR_END);
    ASSERT (NumArgs <= SBI_CALL_MAX_ARGS);
 
    switch (NumArgs) {
      case 0:
        Ret = SbiCall (ExtensionId, FunctionId, NumArgs);
        break;
      case 1:
        Ret = SbiCall (ExtensionId, FunctionId, NumArgs, VA_ARG (Args, UINTN));
        break;
      case 2:
        Ret = SbiCall (ExtensionId, FunctionId, NumArgs, VA_ARG (Args, UINTN),
                       VA_ARG (Args, UINTN));
        break;
      case 3:
        Ret = SbiCall (ExtensionId, FunctionId, NumArgs, VA_ARG (Args, UINTN),
                       VA_ARG (Args, UINTN), VA_ARG (Args, UINTN));
        break;
      case 4:
        Ret = SbiCall (ExtensionId, FunctionId, NumArgs, VA_ARG (Args, UINTN),
                       VA_ARG (Args, UINTN), VA_ARG (Args, UINTN),
                       VA_ARG (Args, UINTN));
        break;
      case 5:
        Ret = SbiCall (ExtensionId, FunctionId, NumArgs, VA_ARG (Args, UINTN),
                       VA_ARG (Args, UINTN), VA_ARG (Args, UINTN),
                       VA_ARG (Args, UINTN), VA_ARG (Args, UINTN));
        break;
      case 6:
        Ret = SbiCall (ExtensionId, FunctionId, NumArgs, VA_ARG (Args, UINTN),
                       VA_ARG (Args, UINTN), VA_ARG (Args, UINTN),
                       VA_ARG (Args, UINTN), VA_ARG (Args, UINTN),
                       VA_ARG (Args, UINTN));
        break;
      default:
        // Too many args. In theory SBI can handle more arguments when they are
        // passed on the stack but no SBI extension uses this, therefore it's
        // not yet implemented here.
        return EFI_INVALID_PARAMETER;
     }
 
    VA_END(Args);
    return TranslateError (Ret.Error);
}
 
//
// SBI Firmware extension
//
 
/**
  Get scratch space of the current hart.
 
  Please consider using the wrapper SbiGetFirmwareContext if you only need to
  access the firmware context.
 
  @param[out] ScratchSpace         The scratch space pointer.
**/
VOID
EFIAPI
SbiGetMscratch (
  OUT SBI_SCRATCH                    **ScratchSpace
  )
{
  SbiRet Ret = SbiCall (SBI_EDK2_FW_EXT, SBI_EXT_FW_MSCRATCH_FUNC, 0);
 
  // Our ecall handler never returns an error, only when the func id is invalid
  ASSERT (Ret.Error == SBI_OK);
 
  *ScratchSpace = (SBI_SCRATCH *)Ret.Value;
}
 
/**
  Get scratch space of the given hart id.
 
  @param[in]  HartId               The hart id.
  @param[out] ScratchSpace         The scratch space pointer.
**/
VOID
EFIAPI
SbiGetMscratchHartid (
  IN  UINTN                            HartId,
  OUT SBI_SCRATCH                    **ScratchSpace
  )
{
  SbiRet Ret = SbiCall (
                 SBI_EDK2_FW_EXT,
                 SBI_EXT_FW_MSCRATCH_HARTID_FUNC,
                 1,
                 HartId
                 );
 
  // Our ecall handler never returns an error, only when the func id is invalid
  ASSERT (Ret.Error == SBI_OK);
 
  *ScratchSpace = (SBI_SCRATCH *)Ret.Value;
}
 
/**
  Get firmware context of the calling hart.
 
  @param[out] FirmwareContext      The firmware context pointer.
  @retval EFI_SUCCESS              The operation succeeds.
**/
VOID
EFIAPI
SbiGetFirmwareContext (
  OUT EFI_RISCV_OPENSBI_FIRMWARE_CONTEXT **FirmwareContext
  )
{
  SBI_SCRATCH  *ScratchSpace;
  SBI_PLATFORM *SbiPlatform;
 
  SbiGetMscratch(&ScratchSpace);
  SbiPlatform = (SBI_PLATFORM *)sbi_platform_ptr(ScratchSpace);
  *FirmwareContext = (EFI_RISCV_OPENSBI_FIRMWARE_CONTEXT *)SbiPlatform->firmware_context;
}
 
/**
  Set firmware context of the calling hart.
 
  @param[in] FirmwareContext       The firmware context pointer.
**/
VOID
EFIAPI
SbiSetFirmwareContext (
  IN EFI_RISCV_OPENSBI_FIRMWARE_CONTEXT *FirmwareContext
  )
{
  SBI_SCRATCH  *ScratchSpace;
  SBI_PLATFORM *SbiPlatform;
 
  SbiGetMscratch(&ScratchSpace);
 
  SbiPlatform = (SBI_PLATFORM *)sbi_platform_ptr (ScratchSpace);
  SbiPlatform->firmware_context = (UINTN)FirmwareContext;
}