--- /dev/null
+/** @file\r
+ Produces the SMM CPU I/O Protocol.\r
+\r
+Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "CpuIo2Mm.h"\r
+\r
+//\r
+// Handle for the SMM CPU I/O Protocol\r
+//\r
+EFI_HANDLE mHandle = NULL;\r
+\r
+//\r
+// SMM CPU I/O Protocol instance\r
+//\r
+EFI_SMM_CPU_IO2_PROTOCOL mSmmCpuIo2 = {\r
+ {\r
+ CpuMemoryServiceRead,\r
+ CpuMemoryServiceWrite\r
+ },\r
+ {\r
+ CpuIoServiceRead,\r
+ CpuIoServiceWrite\r
+ }\r
+};\r
+\r
+//\r
+// Lookup table for increment values based on transfer widths\r
+//\r
+UINT8 mStride[] = {\r
+ 1, // SMM_IO_UINT8\r
+ 2, // SMM_IO_UINT16\r
+ 4, // SMM_IO_UINT32\r
+ 8 // SMM_IO_UINT64\r
+};\r
+\r
+/**\r
+ Check parameters to a SMM CPU I/O Protocol service request.\r
+\r
+ @param[in] MmioOperation TRUE for an MMIO operation, FALSE for I/O Port operation.\r
+ @param[in] Width Signifies the width of the I/O operations.\r
+ @param[in] Address The base address of the I/O operations. The caller is\r
+ responsible for aligning the Address if required.\r
+ @param[in] Count The number of I/O operations to perform.\r
+ @param[in] Buffer For read operations, the destination buffer to store\r
+ the results. For write operations, the source buffer\r
+ from which to write data.\r
+\r
+ @retval EFI_SUCCESS The data was read from or written to the device.\r
+ @retval EFI_UNSUPPORTED The Address is not valid for this system.\r
+ @retval EFI_INVALID_PARAMETER Width or Count, or both, were invalid.\r
+\r
+**/\r
+EFI_STATUS\r
+CpuIoCheckParameter (\r
+ IN BOOLEAN MmioOperation,\r
+ IN EFI_SMM_IO_WIDTH Width,\r
+ IN UINT64 Address,\r
+ IN UINTN Count,\r
+ IN VOID *Buffer\r
+ )\r
+{\r
+ UINT64 MaxCount;\r
+ UINT64 Limit;\r
+\r
+ //\r
+ // Check to see if Buffer is NULL\r
+ //\r
+ if (Buffer == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Check to see if Width is in the valid range\r
+ //\r
+ if ((UINT32)Width > SMM_IO_UINT64) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Check to see if Width is in the valid range for I/O Port operations\r
+ //\r
+ if (!MmioOperation && (Width == SMM_IO_UINT64)) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Check to see if any address associated with this transfer exceeds the maximum\r
+ // allowed address. The maximum address implied by the parameters passed in is\r
+ // Address + Size * Count. If the following condition is met, then the transfer\r
+ // is not supported.\r
+ //\r
+ // Address + Size * Count > (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS) + 1\r
+ //\r
+ // Since MAX_ADDRESS can be the maximum integer value supported by the CPU and Count\r
+ // can also be the maximum integer value supported by the CPU, this range\r
+ // check must be adjusted to avoid all overflow conditions.\r
+ //\r
+ // The following form of the range check is equivalent but assumes that\r
+ // MAX_ADDRESS and MAX_IO_PORT_ADDRESS are of the form (2^n - 1).\r
+ //\r
+ Limit = (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS);\r
+ if (Count == 0) {\r
+ if (Address > Limit) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ } else {\r
+ MaxCount = RShiftU64 (Limit, Width);\r
+ if (MaxCount < (Count - 1)) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ if (Address > LShiftU64 (MaxCount - Count + 1, Width)) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Check to see if Address is aligned\r
+ //\r
+ if ((Address & ((UINT64)mStride[Width] - 1)) != 0) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Reads memory-mapped registers.\r
+\r
+ The I/O operations are carried out exactly as requested. The caller is\r
+ responsible for any alignment and I/O width issues that the bus, device,\r
+ platform, or type of I/O might require.\r
+\r
+ @param[in] This The EFI_SMM_CPU_IO2_PROTOCOL instance.\r
+ @param[in] Width Signifies the width of the I/O operations.\r
+ @param[in] Address The base address of the I/O operations. The caller is\r
+ responsible for aligning the Address if required.\r
+ @param[in] Count The number of I/O operations to perform.\r
+ @param[out] Buffer For read operations, the destination buffer to store\r
+ the results. For write operations, the source buffer\r
+ from which to write data.\r
+\r
+ @retval EFI_SUCCESS The data was read from or written to the device.\r
+ @retval EFI_UNSUPPORTED The Address is not valid for this system.\r
+ @retval EFI_INVALID_PARAMETER Width or Count, or both, were invalid.\r
+ @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
+ lack of resources\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CpuMemoryServiceRead (\r
+ IN CONST EFI_SMM_CPU_IO2_PROTOCOL *This,\r
+ IN EFI_SMM_IO_WIDTH Width,\r
+ IN UINT64 Address,\r
+ IN UINTN Count,\r
+ OUT VOID *Buffer\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINT8 Stride;\r
+ UINT8 *Uint8Buffer;\r
+\r
+ Status = CpuIoCheckParameter (TRUE, Width, Address, Count, Buffer);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Select loop based on the width of the transfer\r
+ //\r
+ Stride = mStride[Width];\r
+ for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {\r
+ if (Width == SMM_IO_UINT8) {\r
+ *Uint8Buffer = MmioRead8 ((UINTN)Address);\r
+ } else if (Width == SMM_IO_UINT16) {\r
+ *((UINT16 *)Uint8Buffer) = MmioRead16 ((UINTN)Address);\r
+ } else if (Width == SMM_IO_UINT32) {\r
+ *((UINT32 *)Uint8Buffer) = MmioRead32 ((UINTN)Address);\r
+ } else if (Width == SMM_IO_UINT64) {\r
+ *((UINT64 *)Uint8Buffer) = MmioRead64 ((UINTN)Address);\r
+ }\r
+ }\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Writes memory-mapped registers.\r
+\r
+ The I/O operations are carried out exactly as requested. The caller is\r
+ responsible for any alignment and I/O width issues that the bus, device,\r
+ platform, or type of I/O might require.\r
+\r
+ @param[in] This The EFI_SMM_CPU_IO2_PROTOCOL instance.\r
+ @param[in] Width Signifies the width of the I/O operations.\r
+ @param[in] Address The base address of the I/O operations. The caller is\r
+ responsible for aligning the Address if required.\r
+ @param[in] Count The number of I/O operations to perform.\r
+ @param[in] Buffer For read operations, the destination buffer to store\r
+ the results. For write operations, the source buffer\r
+ from which to write data.\r
+\r
+ @retval EFI_SUCCESS The data was read from or written to the device.\r
+ @retval EFI_UNSUPPORTED The Address is not valid for this system.\r
+ @retval EFI_INVALID_PARAMETER Width or Count, or both, were invalid.\r
+ @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
+ lack of resources\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CpuMemoryServiceWrite (\r
+ IN CONST EFI_SMM_CPU_IO2_PROTOCOL *This,\r
+ IN EFI_SMM_IO_WIDTH Width,\r
+ IN UINT64 Address,\r
+ IN UINTN Count,\r
+ IN VOID *Buffer\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINT8 Stride;\r
+ UINT8 *Uint8Buffer;\r
+\r
+ Status = CpuIoCheckParameter (TRUE, Width, Address, Count, Buffer);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Select loop based on the width of the transfer\r
+ //\r
+ Stride = mStride[Width];\r
+ for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {\r
+ if (Width == SMM_IO_UINT8) {\r
+ MmioWrite8 ((UINTN)Address, *Uint8Buffer);\r
+ } else if (Width == SMM_IO_UINT16) {\r
+ MmioWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));\r
+ } else if (Width == SMM_IO_UINT32) {\r
+ MmioWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));\r
+ } else if (Width == SMM_IO_UINT64) {\r
+ MmioWrite64 ((UINTN)Address, *((UINT64 *)Uint8Buffer));\r
+ }\r
+ }\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Reads I/O registers.\r
+\r
+ The I/O operations are carried out exactly as requested. The caller is\r
+ responsible for any alignment and I/O width issues that the bus, device,\r
+ platform, or type of I/O might require.\r
+\r
+ @param[in] This The EFI_SMM_CPU_IO2_PROTOCOL instance.\r
+ @param[in] Width Signifies the width of the I/O operations.\r
+ @param[in] Address The base address of the I/O operations. The caller is\r
+ responsible for aligning the Address if required.\r
+ @param[in] Count The number of I/O operations to perform.\r
+ @param[out] Buffer For read operations, the destination buffer to store\r
+ the results. For write operations, the source buffer\r
+ from which to write data.\r
+\r
+ @retval EFI_SUCCESS The data was read from or written to the device.\r
+ @retval EFI_UNSUPPORTED The Address is not valid for this system.\r
+ @retval EFI_INVALID_PARAMETER Width or Count, or both, were invalid.\r
+ @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
+ lack of resources\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CpuIoServiceRead (\r
+ IN CONST EFI_SMM_CPU_IO2_PROTOCOL *This,\r
+ IN EFI_SMM_IO_WIDTH Width,\r
+ IN UINT64 Address,\r
+ IN UINTN Count,\r
+ OUT VOID *Buffer\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINT8 Stride;\r
+ UINT8 *Uint8Buffer;\r
+\r
+ Status = CpuIoCheckParameter (FALSE, Width, Address, Count, Buffer);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Select loop based on the width of the transfer\r
+ //\r
+ Stride = mStride[Width];\r
+ for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {\r
+ if (Width == SMM_IO_UINT8) {\r
+ *Uint8Buffer = IoRead8 ((UINTN)Address);\r
+ } else if (Width == SMM_IO_UINT16) {\r
+ *((UINT16 *)Uint8Buffer) = IoRead16 ((UINTN)Address);\r
+ } else if (Width == SMM_IO_UINT32) {\r
+ *((UINT32 *)Uint8Buffer) = IoRead32 ((UINTN)Address);\r
+ }\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Write I/O registers.\r
+\r
+ The I/O operations are carried out exactly as requested. The caller is\r
+ responsible for any alignment and I/O width issues that the bus, device,\r
+ platform, or type of I/O might require.\r
+\r
+ @param[in] This The EFI_SMM_CPU_IO2_PROTOCOL instance.\r
+ @param[in] Width Signifies the width of the I/O operations.\r
+ @param[in] Address The base address of the I/O operations. The caller is\r
+ responsible for aligning the Address if required.\r
+ @param[in] Count The number of I/O operations to perform.\r
+ @param[in] Buffer For read operations, the destination buffer to store\r
+ the results. For write operations, the source buffer\r
+ from which to write data.\r
+\r
+ @retval EFI_SUCCESS The data was read from or written to the device.\r
+ @retval EFI_UNSUPPORTED The Address is not valid for this system.\r
+ @retval EFI_INVALID_PARAMETER Width or Count, or both, were invalid.\r
+ @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
+ lack of resources\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CpuIoServiceWrite (\r
+ IN CONST EFI_SMM_CPU_IO2_PROTOCOL *This,\r
+ IN EFI_SMM_IO_WIDTH Width,\r
+ IN UINT64 Address,\r
+ IN UINTN Count,\r
+ IN VOID *Buffer\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINT8 Stride;\r
+ UINT8 *Uint8Buffer;\r
+\r
+ //\r
+ // Make sure the parameters are valid\r
+ //\r
+ Status = CpuIoCheckParameter (FALSE, Width, Address, Count, Buffer);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Select loop based on the width of the transfer\r
+ //\r
+ Stride = mStride[Width];\r
+ for (Uint8Buffer = (UINT8 *)Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {\r
+ if (Width == SMM_IO_UINT8) {\r
+ IoWrite8 ((UINTN)Address, *Uint8Buffer);\r
+ } else if (Width == SMM_IO_UINT16) {\r
+ IoWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));\r
+ } else if (Width == SMM_IO_UINT32) {\r
+ IoWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));\r
+ }\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ The module Entry Point SmmCpuIoProtocol driver\r
+\r
+ @retval EFI_SUCCESS The entry point is executed successfully.\r
+ @retval Other Some error occurs when executing this entry point.\r
+\r
+**/\r
+EFI_STATUS\r
+CommonCpuIo2Initialize (\r
+ VOID\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Copy the SMM CPU I/O Protocol instance into the System Management System Table\r
+ //\r
+ CopyMem (&gMmst->MmIo, &mSmmCpuIo2, sizeof (mSmmCpuIo2));\r
+\r
+ //\r
+ // Install the SMM CPU I/O Protocol into the MM protocol database\r
+ //\r
+ Status = gMmst->MmInstallProtocolInterface (\r
+ &mHandle,\r
+ &gEfiSmmCpuIo2ProtocolGuid,\r
+ EFI_NATIVE_INTERFACE,\r
+ &mSmmCpuIo2\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ return Status;\r
+}\r