1 | /** @file
|
---|
2 |
|
---|
3 | Copyright (c) 2008, Intel Corporation. All rights reserved.<BR>
|
---|
4 | (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
|
---|
5 |
|
---|
6 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
7 |
|
---|
8 | **/
|
---|
9 |
|
---|
10 | #include <Library/DebugLib.h>
|
---|
11 | #include <Library/BaseMemoryLib.h>
|
---|
12 | #include <Library/PcdLib.h>
|
---|
13 | #include <Library/CpuLib.h>
|
---|
14 | #include <Library/SynchronizationLib.h>
|
---|
15 | #include <Uefi/UefiBaseType.h>
|
---|
16 | #include <IndustryStandard/IntelTdx.h>
|
---|
17 | #include <IndustryStandard/Tdx.h>
|
---|
18 | #include <Library/TdxMailboxLib.h>
|
---|
19 |
|
---|
20 | /**
|
---|
21 | This function will be called by BSP to get the CPU number.
|
---|
22 |
|
---|
23 | @retval CPU number
|
---|
24 | **/
|
---|
25 | UINT32
|
---|
26 | EFIAPI
|
---|
27 | GetCpusNum (
|
---|
28 | VOID
|
---|
29 | )
|
---|
30 | {
|
---|
31 | UINT64 Status;
|
---|
32 | TD_RETURN_DATA TdReturnData;
|
---|
33 |
|
---|
34 | Status = TdCall (TDCALL_TDINFO, 0, 0, 0, &TdReturnData);
|
---|
35 | if (Status == TDX_EXIT_REASON_SUCCESS) {
|
---|
36 | return TdReturnData.TdInfo.NumVcpus;
|
---|
37 | } else {
|
---|
38 | DEBUG ((DEBUG_ERROR, "Failed call TDCALL_TDINFO. %llx\n", Status));
|
---|
39 | }
|
---|
40 |
|
---|
41 | return 0;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | Get the address of Td mailbox.
|
---|
46 | **/
|
---|
47 | volatile VOID *
|
---|
48 | EFIAPI
|
---|
49 | GetTdxMailBox (
|
---|
50 | VOID
|
---|
51 | )
|
---|
52 | {
|
---|
53 | return (VOID *)(UINTN)PcdGet32 (PcdOvmfSecGhcbBackupBase);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | This function will be called by BSP to wakeup APs the are spinning on mailbox
|
---|
58 | in protected mode
|
---|
59 |
|
---|
60 | @param[in] Command Command to send APs
|
---|
61 | @param[in] WakeupVector If used, address for APs to start executing
|
---|
62 | @param[in] WakeArgsX Args to pass to APs for excuting commands
|
---|
63 | **/
|
---|
64 | VOID
|
---|
65 | EFIAPI
|
---|
66 | MpSendWakeupCommand (
|
---|
67 | IN UINT16 Command,
|
---|
68 | IN UINT64 WakeupVector,
|
---|
69 | IN UINT64 WakeupArgs1,
|
---|
70 | IN UINT64 WakeupArgs2,
|
---|
71 | IN UINT64 WakeupArgs3,
|
---|
72 | IN UINT64 WakeupArgs4
|
---|
73 | )
|
---|
74 | {
|
---|
75 | volatile MP_WAKEUP_MAILBOX *MailBox;
|
---|
76 |
|
---|
77 | MailBox = (volatile MP_WAKEUP_MAILBOX *)GetTdxMailBox ();
|
---|
78 | MailBox->ApicId = MP_CPU_PROTECTED_MODE_MAILBOX_APICID_INVALID;
|
---|
79 | MailBox->WakeUpVector = 0;
|
---|
80 | MailBox->Command = MpProtectedModeWakeupCommandNoop;
|
---|
81 | MailBox->ApicId = MP_CPU_PROTECTED_MODE_MAILBOX_APICID_BROADCAST;
|
---|
82 | MailBox->WakeUpVector = WakeupVector;
|
---|
83 | MailBox->WakeUpArgs1 = WakeupArgs1;
|
---|
84 | MailBox->WakeUpArgs2 = WakeupArgs2;
|
---|
85 | MailBox->WakeUpArgs3 = WakeupArgs3;
|
---|
86 | MailBox->WakeUpArgs4 = WakeupArgs4;
|
---|
87 | AsmCpuid (0x01, NULL, NULL, NULL, NULL);
|
---|
88 | MailBox->Command = Command;
|
---|
89 | AsmCpuid (0x01, NULL, NULL, NULL, NULL);
|
---|
90 | return;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | BSP wait until all the APs arriving. It means the task triggered by BSP is started.
|
---|
95 | **/
|
---|
96 | VOID
|
---|
97 | EFIAPI
|
---|
98 | MpSerializeStart (
|
---|
99 | VOID
|
---|
100 | )
|
---|
101 | {
|
---|
102 | volatile MP_WAKEUP_MAILBOX *MailBox;
|
---|
103 | UINT32 NumOfCpus;
|
---|
104 |
|
---|
105 | NumOfCpus = GetCpusNum ();
|
---|
106 | MailBox = (volatile MP_WAKEUP_MAILBOX *)GetTdxMailBox ();
|
---|
107 |
|
---|
108 | DEBUG ((DEBUG_VERBOSE, "Waiting for APs to arriving. NumOfCpus=%d, MailBox=%p\n", NumOfCpus, MailBox));
|
---|
109 | while (MailBox->NumCpusArriving != (NumOfCpus -1)) {
|
---|
110 | CpuPause ();
|
---|
111 | }
|
---|
112 |
|
---|
113 | DEBUG ((DEBUG_VERBOSE, "Releasing APs\n"));
|
---|
114 | MailBox->NumCpusExiting = NumOfCpus;
|
---|
115 | InterlockedIncrement ((UINT32 *)&MailBox->NumCpusArriving);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | BSP wait until all the APs arriving. It means the task triggered by BSP is ended.
|
---|
120 | **/
|
---|
121 | VOID
|
---|
122 | EFIAPI
|
---|
123 | MpSerializeEnd (
|
---|
124 | VOID
|
---|
125 | )
|
---|
126 | {
|
---|
127 | volatile MP_WAKEUP_MAILBOX *MailBox;
|
---|
128 |
|
---|
129 | MailBox = (volatile MP_WAKEUP_MAILBOX *)GetTdxMailBox ();
|
---|
130 | DEBUG ((DEBUG_VERBOSE, "Waiting for APs to finish\n"));
|
---|
131 | while (MailBox->NumCpusExiting != 1 ) {
|
---|
132 | CpuPause ();
|
---|
133 | }
|
---|
134 |
|
---|
135 | DEBUG ((DEBUG_VERBOSE, "Restarting APs\n"));
|
---|
136 | MailBox->Command = MpProtectedModeWakeupCommandNoop;
|
---|
137 | MailBox->NumCpusArriving = 0;
|
---|
138 | InterlockedDecrement ((UINT32 *)&MailBox->NumCpusExiting);
|
---|
139 | }
|
---|