PchSmiDispatchSmm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /** @file
  2. SMM SwDispatch2 Protocol.
  3. Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "PchSmiDispatchSmm.h"
  7. typedef struct {
  8. UINT8 EosBitOffset;
  9. UINT8 ApmBitOffset;
  10. UINT32 SmiEosAddr;
  11. UINT32 SmiApmStsAddr;
  12. } SMM_PCH_REGISTER;
  13. SMM_PCH_REGISTER mSmiPchReg;
  14. EFI_SMM_CPU_PROTOCOL *mSmmCpuProtocol;
  15. LIST_ENTRY mSmmSwDispatch2Queue = INITIALIZE_LIST_HEAD_VARIABLE (mSmmSwDispatch2Queue);
  16. /**
  17. Find SmmSwDispatch2Context by SwSmiInputValue.
  18. @param[in] SwSmiInputValue The value to indentify the SmmSwDispatch2 context
  19. @return Pointer to EFI_SMM_SW_DISPATCH2_CONTEXT context
  20. **/
  21. EFI_SMM_SW_DISPATCH2_CONTEXT *
  22. FindContextBySwSmiInputValue (
  23. IN UINTN SwSmiInputValue
  24. )
  25. {
  26. LIST_ENTRY *Node;
  27. EFI_SMM_SW_DISPATCH2_CONTEXT *Dispatch2Context;
  28. Node = mSmmSwDispatch2Queue.ForwardLink;
  29. for ( ; Node != &mSmmSwDispatch2Queue; Node = Node->ForwardLink) {
  30. Dispatch2Context = BASE_CR (Node, EFI_SMM_SW_DISPATCH2_CONTEXT, Link);
  31. if (Dispatch2Context->SwSmiInputValue == SwSmiInputValue) {
  32. return Dispatch2Context;
  33. }
  34. }
  35. return NULL;
  36. }
  37. /**
  38. Find SmmSwDispatch2Context by DispatchHandle.
  39. @param DispatchHandle The handle to indentify the SmmSwDispatch2 context
  40. @return Pointer to EFI_SMM_SW_DISPATCH2_CONTEXT context
  41. **/
  42. EFI_SMM_SW_DISPATCH2_CONTEXT *
  43. FindContextByDispatchHandle (
  44. IN EFI_HANDLE DispatchHandle
  45. )
  46. {
  47. LIST_ENTRY *Node;
  48. EFI_SMM_SW_DISPATCH2_CONTEXT *Dispatch2Context;
  49. Node = mSmmSwDispatch2Queue.ForwardLink;
  50. for ( ; Node != &mSmmSwDispatch2Queue; Node = Node->ForwardLink) {
  51. Dispatch2Context = BASE_CR (Node, EFI_SMM_SW_DISPATCH2_CONTEXT, Link);
  52. if (Dispatch2Context->DispatchHandle == DispatchHandle) {
  53. return Dispatch2Context;
  54. }
  55. }
  56. return NULL;
  57. }
  58. /**
  59. Dispatch registered SMM handlers
  60. @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
  61. @param RegisterContext Points to an optional handler context which was specified when the handler was registered.
  62. @param CommBuffer A pointer to a collection of data in memory that will
  63. be conveyed from a non-SMM environment into an SMM environment.
  64. @param CommBufferSize The size of the CommBuffer.
  65. @return Status Code
  66. **/
  67. EFI_STATUS
  68. SmmSwDispatcher (
  69. IN EFI_HANDLE DispatchHandle,
  70. IN CONST VOID *RegisterContext,
  71. IN OUT VOID *CommBuffer,
  72. IN OUT UINTN *CommBufferSize
  73. )
  74. {
  75. EFI_STATUS Status;
  76. EFI_SMM_SW_CONTEXT SwContext;
  77. UINTN Index;
  78. EFI_SMM_SW_DISPATCH2_CONTEXT *Context;
  79. EFI_SMM_HANDLER_ENTRY_POINT2 DispatchFunction;
  80. EFI_SMM_SW_REGISTER_CONTEXT DispatchContext;
  81. UINTN Size;
  82. EFI_SMM_SAVE_STATE_IO_INFO IoInfo;
  83. //
  84. // Construct new context
  85. //
  86. SwContext.SwSmiCpuIndex = 0;
  87. SwContext.CommandPort = IoRead8 (SMM_CONTROL_PORT);
  88. SwContext.DataPort = IoRead8 (SMM_DATA_PORT);
  89. //
  90. // Try to find which CPU trigger SWSMI
  91. //
  92. for (Index = 0; Index < gSmst->NumberOfCpus; Index++) {
  93. Status = mSmmCpuProtocol->ReadSaveState (
  94. mSmmCpuProtocol,
  95. sizeof (IoInfo),
  96. EFI_SMM_SAVE_STATE_REGISTER_IO,
  97. Index,
  98. &IoInfo
  99. );
  100. if (EFI_ERROR (Status)) {
  101. continue;
  102. }
  103. if (IoInfo.IoPort == SMM_CONTROL_PORT) {
  104. //
  105. // Great! Find it.
  106. //
  107. SwContext.SwSmiCpuIndex = Index;
  108. DEBUG ((DEBUG_VERBOSE, "CPU index = 0x%x/0x%x\n", Index, gSmst->NumberOfCpus));
  109. break;
  110. }
  111. }
  112. if (SwContext.CommandPort == 0) {
  113. DEBUG ((DEBUG_VERBOSE, "NOT SW SMI\n"));
  114. Status = EFI_SUCCESS;
  115. goto End;
  116. }
  117. //
  118. // Search context
  119. //
  120. Context = FindContextBySwSmiInputValue (SwContext.CommandPort);
  121. if (Context == NULL) {
  122. DEBUG ((DEBUG_INFO, "No handler for SMI value 0x%x\n", SwContext.CommandPort));
  123. Status = EFI_SUCCESS;
  124. goto End;
  125. }
  126. DEBUG ((DEBUG_VERBOSE, "Prepare to call handler for 0x%x\n", SwContext.CommandPort));
  127. //
  128. // Dispatch
  129. //
  130. DispatchContext.SwSmiInputValue = SwContext.CommandPort;
  131. Size = sizeof (SwContext);
  132. DispatchFunction = (EFI_SMM_HANDLER_ENTRY_POINT2)Context->DispatchFunction;
  133. Status = DispatchFunction (DispatchHandle, &DispatchContext, &SwContext, &Size);
  134. End:
  135. //
  136. // Clear SMI APM status
  137. //
  138. IoOr32 (mSmiPchReg.SmiApmStsAddr, 1 << mSmiPchReg.ApmBitOffset);
  139. //
  140. // Set EOS bit
  141. //
  142. IoOr32 (mSmiPchReg.SmiEosAddr, 1 << mSmiPchReg.EosBitOffset);
  143. return Status;
  144. }
  145. /**
  146. Check the SwSmiInputValue is already used
  147. @param[in] SwSmiInputValue To indentify the SmmSwDispatch2 context
  148. @retval EFI_SUCCESS SwSmiInputValue could be used.
  149. @retval EFI_INVALID_PARAMETER SwSmiInputValue is already be used.
  150. **/
  151. EFI_STATUS
  152. SmiInputValueCheck (
  153. IN UINTN SwSmiInputValue
  154. )
  155. {
  156. LIST_ENTRY *Node;
  157. EFI_SMM_SW_DISPATCH2_CONTEXT *Dispatch2Context;
  158. Node = mSmmSwDispatch2Queue.ForwardLink;
  159. for ( ; Node != &mSmmSwDispatch2Queue; Node = Node->ForwardLink) {
  160. Dispatch2Context = BASE_CR (Node, EFI_SMM_SW_DISPATCH2_CONTEXT, Link);
  161. if (Dispatch2Context->SwSmiInputValue == SwSmiInputValue) {
  162. return EFI_INVALID_PARAMETER;
  163. }
  164. }
  165. return EFI_SUCCESS;
  166. }
  167. /**
  168. Register a child SMI source dispatch function for the specified software SMI.
  169. This service registers a function (DispatchFunction) which will be called when the software
  170. SMI source specified by RegContext->SwSmiCpuIndex is detected. On return, DispatchHandle
  171. contains a unique handle which may be used later to unregister the function using UnRegister().
  172. @param[in] This Pointer to the EFI_SMM_SW_DISPATCH2_PROTOCOL instance.
  173. @param[in] DispatchFunction Function to register for handler when the specified software
  174. SMI is generated.
  175. @param[in, out] RegContext Pointer to the dispatch function's context.
  176. The caller fills this context in before calling
  177. the register function to indicate to the register
  178. function which Software SMI input value the
  179. dispatch function should be invoked for.
  180. @param[out] DispatchHandle Handle generated by the dispatcher to track the
  181. function instance.
  182. @retval EFI_SUCCESS The dispatch function has been successfully
  183. registered and the SMI source has been enabled.
  184. @retval EFI_DEVICE_ERROR The SW driver was unable to enable the SMI source.
  185. @retval EFI_INVALID_PARAMETER RegisterContext is invalid. The SW SMI input value
  186. is not within valid range.
  187. @retval EFI_OUT_OF_RESOURCES There is not enough memory (system or SMM) to manage this
  188. child.
  189. @retval EFI_OUT_OF_RESOURCES A unique software SMI value could not be assigned
  190. for this dispatch.
  191. **/
  192. EFI_STATUS
  193. EFIAPI
  194. SmmSwDispatch2Register (
  195. IN CONST EFI_SMM_SW_DISPATCH2_PROTOCOL *This,
  196. IN EFI_SMM_HANDLER_ENTRY_POINT2 DispatchFunction,
  197. IN OUT EFI_SMM_SW_REGISTER_CONTEXT *RegContext,
  198. OUT EFI_HANDLE *DispatchHandle
  199. )
  200. {
  201. EFI_STATUS Status;
  202. UINTN Index;
  203. EFI_SMM_SW_DISPATCH2_CONTEXT *Context;
  204. if (RegContext->SwSmiInputValue == (UINTN)-1) {
  205. //
  206. // If SwSmiInputValue is set to (UINTN) -1 then a unique value
  207. // will be assigned and returned in the structure.
  208. //
  209. Status = EFI_NOT_FOUND;
  210. for (Index = 1; Index < MAXIMUM_SWI_VALUE; Index++) {
  211. Status = SmiInputValueCheck (Index);
  212. if (!EFI_ERROR (Status)) {
  213. RegContext->SwSmiInputValue = Index;
  214. break;
  215. }
  216. }
  217. if (RegContext->SwSmiInputValue == (UINTN)-1) {
  218. return EFI_OUT_OF_RESOURCES;
  219. }
  220. }
  221. if ((RegContext->SwSmiInputValue > MAXIMUM_SWI_VALUE) || (RegContext->SwSmiInputValue == 0)) {
  222. DEBUG ((DEBUG_ERROR, "ERROR: SMI value range (1 ~ 0x%x)\n", MAXIMUM_SWI_VALUE));
  223. return EFI_INVALID_PARAMETER;
  224. }
  225. //
  226. // Register
  227. //
  228. Status = gSmst->SmmAllocatePool (EfiRuntimeServicesData, sizeof (*Context), (VOID **)&Context);
  229. ASSERT_EFI_ERROR (Status);
  230. if (EFI_ERROR (Status)) {
  231. return EFI_OUT_OF_RESOURCES;
  232. }
  233. *DispatchHandle = (EFI_HANDLE)Context;
  234. Context->Signature = SMI_SW_HANDLER_SIGNATURE;
  235. Context->SwSmiInputValue = RegContext->SwSmiInputValue;
  236. Context->DispatchFunction = (UINTN)DispatchFunction;
  237. Context->DispatchHandle = *DispatchHandle;
  238. InsertTailList (&mSmmSwDispatch2Queue, &Context->Link);
  239. return Status;
  240. }
  241. /**
  242. Unregister a child SMI source dispatch function for the specified software SMI.
  243. This service removes the handler associated with DispatchHandle so that it will no longer be
  244. called in response to a software SMI.
  245. @param[in] This Pointer to the EFI_SMM_SW_DISPATCH2_PROTOCOL instance.
  246. @param[in] DispatchHandle Handle of dispatch function to deregister.
  247. @retval EFI_SUCCESS The dispatch function has been successfully unregistered.
  248. @retval EFI_INVALID_PARAMETER The DispatchHandle was not valid.
  249. **/
  250. EFI_STATUS
  251. EFIAPI
  252. SmmSwDispatch2UnRegister (
  253. IN CONST EFI_SMM_SW_DISPATCH2_PROTOCOL *This,
  254. IN EFI_HANDLE DispatchHandle
  255. )
  256. {
  257. EFI_SMM_SW_DISPATCH2_CONTEXT *Context;
  258. //
  259. // Unregister
  260. //
  261. Context = FindContextByDispatchHandle (DispatchHandle);
  262. ASSERT (Context != NULL);
  263. if (Context != NULL) {
  264. RemoveEntryList (&Context->Link);
  265. gSmst->SmmFreePool (Context);
  266. }
  267. return EFI_SUCCESS;
  268. }
  269. EFI_SMM_SW_DISPATCH2_PROTOCOL gSmmSwDispatch2 = {
  270. SmmSwDispatch2Register,
  271. SmmSwDispatch2UnRegister,
  272. MAXIMUM_SWI_VALUE
  273. };
  274. /**
  275. Get specified SMI register based on given register ID
  276. @param[in] SmmRegister SMI related register array from bootloader
  277. @param[in] Id The register ID to get.
  278. @retval NULL The register is not found or the format is not expected.
  279. @return smi register
  280. **/
  281. PLD_GENERIC_REGISTER *
  282. GetSmmCtrlRegById (
  283. IN PLD_SMM_REGISTERS *SmmRegister,
  284. IN UINT32 Id
  285. )
  286. {
  287. UINT32 Index;
  288. PLD_GENERIC_REGISTER *PldReg;
  289. PldReg = NULL;
  290. for (Index = 0; Index < SmmRegister->Count; Index++) {
  291. if (SmmRegister->Registers[Index].Id == Id) {
  292. PldReg = &SmmRegister->Registers[Index];
  293. break;
  294. }
  295. }
  296. if (PldReg == NULL) {
  297. DEBUG ((DEBUG_INFO, "Register %d not found.\n", Id));
  298. return NULL;
  299. }
  300. //
  301. // Checking the register if it is expected.
  302. //
  303. if ((PldReg->Address.AccessSize != EFI_ACPI_3_0_DWORD) ||
  304. (PldReg->Address.Address == 0) ||
  305. (PldReg->Address.RegisterBitWidth != 1) ||
  306. (PldReg->Address.AddressSpaceId != EFI_ACPI_3_0_SYSTEM_IO) ||
  307. (PldReg->Value != 1))
  308. {
  309. DEBUG ((DEBUG_INFO, "Unexpected SMM register.\n"));
  310. DEBUG ((DEBUG_INFO, "AddressSpaceId= 0x%x\n", PldReg->Address.AddressSpaceId));
  311. DEBUG ((DEBUG_INFO, "RegBitWidth = 0x%x\n", PldReg->Address.RegisterBitWidth));
  312. DEBUG ((DEBUG_INFO, "RegBitOffset = 0x%x\n", PldReg->Address.RegisterBitOffset));
  313. DEBUG ((DEBUG_INFO, "AccessSize = 0x%x\n", PldReg->Address.AccessSize));
  314. DEBUG ((DEBUG_INFO, "Address = 0x%lx\n", PldReg->Address.Address));
  315. return NULL;
  316. }
  317. return PldReg;
  318. }
  319. /**
  320. Entry Point for this driver.
  321. @param[in] ImageHandle Image handle of this driver.
  322. @param[in] SystemTable A Pointer to the EFI System Table.
  323. @retval EFI_SUCCESS The entry point is executed successfully.
  324. @retval other Some error occurred when executing this entry point.
  325. **/
  326. EFI_STATUS
  327. EFIAPI
  328. PchSmiDispatchEntryPoint (
  329. IN EFI_HANDLE ImageHandle,
  330. IN EFI_SYSTEM_TABLE *SystemTable
  331. )
  332. {
  333. EFI_STATUS Status;
  334. EFI_HANDLE DispatchHandle;
  335. EFI_HOB_GUID_TYPE *GuidHob;
  336. PLD_SMM_REGISTERS *SmmRegister;
  337. PLD_GENERIC_REGISTER *SmiEosReg;
  338. PLD_GENERIC_REGISTER *SmiApmStsReg;
  339. GuidHob = GetFirstGuidHob (&gSmmRegisterInfoGuid);
  340. if (GuidHob == NULL) {
  341. return EFI_UNSUPPORTED;
  342. }
  343. SmmRegister = (PLD_SMM_REGISTERS *)GET_GUID_HOB_DATA (GuidHob);
  344. SmiEosReg = GetSmmCtrlRegById (SmmRegister, REGISTER_ID_SMI_EOS);
  345. if (SmiEosReg == NULL) {
  346. DEBUG ((DEBUG_ERROR, "SMI EOS reg not found.\n"));
  347. return EFI_NOT_FOUND;
  348. }
  349. mSmiPchReg.SmiEosAddr = (UINT32)SmiEosReg->Address.Address;
  350. mSmiPchReg.EosBitOffset = SmiEosReg->Address.RegisterBitOffset;
  351. SmiApmStsReg = GetSmmCtrlRegById (SmmRegister, REGISTER_ID_SMI_APM_STS);
  352. if (SmiApmStsReg == NULL) {
  353. DEBUG ((DEBUG_ERROR, "SMI APM status reg not found.\n"));
  354. return EFI_NOT_FOUND;
  355. }
  356. mSmiPchReg.SmiApmStsAddr = (UINT32)SmiApmStsReg->Address.Address;
  357. mSmiPchReg.ApmBitOffset = SmiApmStsReg->Address.RegisterBitOffset;
  358. //
  359. // Locate PI SMM CPU protocol
  360. //
  361. Status = gSmst->SmmLocateProtocol (&gEfiSmmCpuProtocolGuid, NULL, (VOID **)&mSmmCpuProtocol);
  362. ASSERT_EFI_ERROR (Status);
  363. //
  364. // Register a SMM handler to handle subsequent SW SMIs.
  365. //
  366. Status = gSmst->SmiHandlerRegister ((EFI_MM_HANDLER_ENTRY_POINT)SmmSwDispatcher, NULL, &DispatchHandle);
  367. ASSERT_EFI_ERROR (Status);
  368. //
  369. // Publish PI SMM SwDispatch2 Protocol
  370. //
  371. ImageHandle = NULL;
  372. Status = gSmst->SmmInstallProtocolInterface (
  373. &ImageHandle,
  374. &gEfiSmmSwDispatch2ProtocolGuid,
  375. EFI_NATIVE_INTERFACE,
  376. &gSmmSwDispatch2
  377. );
  378. ASSERT_EFI_ERROR (Status);
  379. return Status;
  380. }