2 Produces the SMM CPU I/O Protocol.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
12 // Handle for the SMM CPU I/O Protocol
14 EFI_HANDLE mHandle
= NULL
;
17 // SMM CPU I/O Protocol instance
19 EFI_SMM_CPU_IO2_PROTOCOL mSmmCpuIo2
= {
31 // Lookup table for increment values based on transfer widths
41 Check parameters to a SMM CPU I/O Protocol service request.
43 @param[in] MmioOperation TRUE for an MMIO operation, FALSE for I/O Port operation.
44 @param[in] Width Signifies the width of the I/O operations.
45 @param[in] Address The base address of the I/O operations. The caller is
46 responsible for aligning the Address if required.
47 @param[in] Count The number of I/O operations to perform.
48 @param[in] Buffer For read operations, the destination buffer to store
49 the results. For write operations, the source buffer
50 from which to write data.
52 @retval EFI_SUCCESS The data was read from or written to the device.
53 @retval EFI_UNSUPPORTED The Address is not valid for this system.
54 @retval EFI_INVALID_PARAMETER Width or Count, or both, were invalid.
59 IN BOOLEAN MmioOperation
,
60 IN EFI_SMM_IO_WIDTH Width
,
70 // Check to see if Buffer is NULL
73 return EFI_INVALID_PARAMETER
;
77 // Check to see if Width is in the valid range
79 if ((UINT32
)Width
> SMM_IO_UINT64
) {
80 return EFI_INVALID_PARAMETER
;
84 // Check to see if Width is in the valid range for I/O Port operations
86 if (!MmioOperation
&& (Width
== SMM_IO_UINT64
)) {
87 return EFI_INVALID_PARAMETER
;
91 // Check to see if any address associated with this transfer exceeds the maximum
92 // allowed address. The maximum address implied by the parameters passed in is
93 // Address + Size * Count. If the following condition is met, then the transfer
96 // Address + Size * Count > (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS) + 1
98 // Since MAX_ADDRESS can be the maximum integer value supported by the CPU and Count
99 // can also be the maximum integer value supported by the CPU, this range
100 // check must be adjusted to avoid all overflow conditions.
102 // The following form of the range check is equivalent but assumes that
103 // MAX_ADDRESS and MAX_IO_PORT_ADDRESS are of the form (2^n - 1).
105 Limit
= (MmioOperation
? MAX_ADDRESS
: MAX_IO_PORT_ADDRESS
);
107 if (Address
> Limit
) {
108 return EFI_UNSUPPORTED
;
111 MaxCount
= RShiftU64 (Limit
, Width
);
112 if (MaxCount
< (Count
- 1)) {
113 return EFI_UNSUPPORTED
;
116 if (Address
> LShiftU64 (MaxCount
- Count
+ 1, Width
)) {
117 return EFI_UNSUPPORTED
;
122 // Check to see if Address is aligned
124 if ((Address
& ((UINT64
)mStride
[Width
] - 1)) != 0) {
125 return EFI_UNSUPPORTED
;
132 Reads memory-mapped registers.
134 The I/O operations are carried out exactly as requested. The caller is
135 responsible for any alignment and I/O width issues that the bus, device,
136 platform, or type of I/O might require.
138 @param[in] This The EFI_SMM_CPU_IO2_PROTOCOL instance.
139 @param[in] Width Signifies the width of the I/O operations.
140 @param[in] Address The base address of the I/O operations. The caller is
141 responsible for aligning the Address if required.
142 @param[in] Count The number of I/O operations to perform.
143 @param[out] Buffer For read operations, the destination buffer to store
144 the results. For write operations, the source buffer
145 from which to write data.
147 @retval EFI_SUCCESS The data was read from or written to the device.
148 @retval EFI_UNSUPPORTED The Address is not valid for this system.
149 @retval EFI_INVALID_PARAMETER Width or Count, or both, were invalid.
150 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
156 CpuMemoryServiceRead (
157 IN CONST EFI_SMM_CPU_IO2_PROTOCOL
*This
,
158 IN EFI_SMM_IO_WIDTH Width
,
168 Status
= CpuIoCheckParameter (TRUE
, Width
, Address
, Count
, Buffer
);
169 if (EFI_ERROR (Status
)) {
174 // Select loop based on the width of the transfer
176 Stride
= mStride
[Width
];
177 for (Uint8Buffer
= Buffer
; Count
> 0; Address
+= Stride
, Uint8Buffer
+= Stride
, Count
--) {
178 if (Width
== SMM_IO_UINT8
) {
179 *Uint8Buffer
= MmioRead8 ((UINTN
)Address
);
180 } else if (Width
== SMM_IO_UINT16
) {
181 *((UINT16
*)Uint8Buffer
) = MmioRead16 ((UINTN
)Address
);
182 } else if (Width
== SMM_IO_UINT32
) {
183 *((UINT32
*)Uint8Buffer
) = MmioRead32 ((UINTN
)Address
);
184 } else if (Width
== SMM_IO_UINT64
) {
185 *((UINT64
*)Uint8Buffer
) = MmioRead64 ((UINTN
)Address
);
193 Writes memory-mapped registers.
195 The I/O operations are carried out exactly as requested. The caller is
196 responsible for any alignment and I/O width issues that the bus, device,
197 platform, or type of I/O might require.
199 @param[in] This The EFI_SMM_CPU_IO2_PROTOCOL instance.
200 @param[in] Width Signifies the width of the I/O operations.
201 @param[in] Address The base address of the I/O operations. The caller is
202 responsible for aligning the Address if required.
203 @param[in] Count The number of I/O operations to perform.
204 @param[in] Buffer For read operations, the destination buffer to store
205 the results. For write operations, the source buffer
206 from which to write data.
208 @retval EFI_SUCCESS The data was read from or written to the device.
209 @retval EFI_UNSUPPORTED The Address is not valid for this system.
210 @retval EFI_INVALID_PARAMETER Width or Count, or both, were invalid.
211 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
217 CpuMemoryServiceWrite (
218 IN CONST EFI_SMM_CPU_IO2_PROTOCOL
*This
,
219 IN EFI_SMM_IO_WIDTH Width
,
229 Status
= CpuIoCheckParameter (TRUE
, Width
, Address
, Count
, Buffer
);
230 if (EFI_ERROR (Status
)) {
235 // Select loop based on the width of the transfer
237 Stride
= mStride
[Width
];
238 for (Uint8Buffer
= Buffer
; Count
> 0; Address
+= Stride
, Uint8Buffer
+= Stride
, Count
--) {
239 if (Width
== SMM_IO_UINT8
) {
240 MmioWrite8 ((UINTN
)Address
, *Uint8Buffer
);
241 } else if (Width
== SMM_IO_UINT16
) {
242 MmioWrite16 ((UINTN
)Address
, *((UINT16
*)Uint8Buffer
));
243 } else if (Width
== SMM_IO_UINT32
) {
244 MmioWrite32 ((UINTN
)Address
, *((UINT32
*)Uint8Buffer
));
245 } else if (Width
== SMM_IO_UINT64
) {
246 MmioWrite64 ((UINTN
)Address
, *((UINT64
*)Uint8Buffer
));
256 The I/O operations are carried out exactly as requested. The caller is
257 responsible for any alignment and I/O width issues that the bus, device,
258 platform, or type of I/O might require.
260 @param[in] This The EFI_SMM_CPU_IO2_PROTOCOL instance.
261 @param[in] Width Signifies the width of the I/O operations.
262 @param[in] Address The base address of the I/O operations. The caller is
263 responsible for aligning the Address if required.
264 @param[in] Count The number of I/O operations to perform.
265 @param[out] Buffer For read operations, the destination buffer to store
266 the results. For write operations, the source buffer
267 from which to write data.
269 @retval EFI_SUCCESS The data was read from or written to the device.
270 @retval EFI_UNSUPPORTED The Address is not valid for this system.
271 @retval EFI_INVALID_PARAMETER Width or Count, or both, were invalid.
272 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
279 IN CONST EFI_SMM_CPU_IO2_PROTOCOL
*This
,
280 IN EFI_SMM_IO_WIDTH Width
,
290 Status
= CpuIoCheckParameter (FALSE
, Width
, Address
, Count
, Buffer
);
291 if (EFI_ERROR (Status
)) {
296 // Select loop based on the width of the transfer
298 Stride
= mStride
[Width
];
299 for (Uint8Buffer
= Buffer
; Count
> 0; Address
+= Stride
, Uint8Buffer
+= Stride
, Count
--) {
300 if (Width
== SMM_IO_UINT8
) {
301 *Uint8Buffer
= IoRead8 ((UINTN
)Address
);
302 } else if (Width
== SMM_IO_UINT16
) {
303 *((UINT16
*)Uint8Buffer
) = IoRead16 ((UINTN
)Address
);
304 } else if (Width
== SMM_IO_UINT32
) {
305 *((UINT32
*)Uint8Buffer
) = IoRead32 ((UINTN
)Address
);
315 The I/O operations are carried out exactly as requested. The caller is
316 responsible for any alignment and I/O width issues that the bus, device,
317 platform, or type of I/O might require.
319 @param[in] This The EFI_SMM_CPU_IO2_PROTOCOL instance.
320 @param[in] Width Signifies the width of the I/O operations.
321 @param[in] Address The base address of the I/O operations. The caller is
322 responsible for aligning the Address if required.
323 @param[in] Count The number of I/O operations to perform.
324 @param[in] Buffer For read operations, the destination buffer to store
325 the results. For write operations, the source buffer
326 from which to write data.
328 @retval EFI_SUCCESS The data was read from or written to the device.
329 @retval EFI_UNSUPPORTED The Address is not valid for this system.
330 @retval EFI_INVALID_PARAMETER Width or Count, or both, were invalid.
331 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
338 IN CONST EFI_SMM_CPU_IO2_PROTOCOL
*This
,
339 IN EFI_SMM_IO_WIDTH Width
,
350 // Make sure the parameters are valid
352 Status
= CpuIoCheckParameter (FALSE
, Width
, Address
, Count
, Buffer
);
353 if (EFI_ERROR (Status
)) {
358 // Select loop based on the width of the transfer
360 Stride
= mStride
[Width
];
361 for (Uint8Buffer
= (UINT8
*)Buffer
; Count
> 0; Address
+= Stride
, Uint8Buffer
+= Stride
, Count
--) {
362 if (Width
== SMM_IO_UINT8
) {
363 IoWrite8 ((UINTN
)Address
, *Uint8Buffer
);
364 } else if (Width
== SMM_IO_UINT16
) {
365 IoWrite16 ((UINTN
)Address
, *((UINT16
*)Uint8Buffer
));
366 } else if (Width
== SMM_IO_UINT32
) {
367 IoWrite32 ((UINTN
)Address
, *((UINT32
*)Uint8Buffer
));
375 The module Entry Point SmmCpuIoProtocol driver
377 @retval EFI_SUCCESS The entry point is executed successfully.
378 @retval Other Some error occurs when executing this entry point.
382 CommonCpuIo2Initialize (
389 // Copy the SMM CPU I/O Protocol instance into the System Management System Table
391 CopyMem (&gMmst
->MmIo
, &mSmmCpuIo2
, sizeof (mSmmCpuIo2
));
394 // Install the SMM CPU I/O Protocol into the MM protocol database
396 Status
= gMmst
->MmInstallProtocolInterface (
398 &gEfiSmmCpuIo2ProtocolGuid
,
399 EFI_NATIVE_INTERFACE
,
402 ASSERT_EFI_ERROR (Status
);