2 Uses the services of the I/O Library to produce the CPU I/O Protocol
4 Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 // Handle for the CPU I/O Protocol
20 EFI_HANDLE mHandle
= NULL
;
23 // CPU I/O Protocol inatance
25 EFI_CPU_IO_PROTOCOL mCpuIo
= {
37 // Lookup table for increment values based on transfer widths
40 1, // EfiCpuIoWidthUint8
41 2, // EfiCpuIoWidthUint16
42 4, // EfiCpuIoWidthUint32
43 8, // EfiCpuIoWidthUint64
44 0, // EfiCpuIoWidthFifoUint8
45 0, // EfiCpuIoWidthFifoUint16
46 0, // EfiCpuIoWidthFifoUint32
47 0, // EfiCpuIoWidthFifoUint64
48 1, // EfiCpuIoWidthFillUint8
49 2, // EfiCpuIoWidthFillUint16
50 4, // EfiCpuIoWidthFillUint32
51 8 // EfiCpuIoWidthFillUint64
55 // Lookup table for increment values based on transfer widths
57 UINT8 mOutStride
[] = {
58 1, // EfiCpuIoWidthUint8
59 2, // EfiCpuIoWidthUint16
60 4, // EfiCpuIoWidthUint32
61 8, // EfiCpuIoWidthUint64
62 1, // EfiCpuIoWidthFifoUint8
63 2, // EfiCpuIoWidthFifoUint16
64 4, // EfiCpuIoWidthFifoUint32
65 8, // EfiCpuIoWidthFifoUint64
66 0, // EfiCpuIoWidthFillUint8
67 0, // EfiCpuIoWidthFillUint16
68 0, // EfiCpuIoWidthFillUint32
69 0 // EfiCpuIoWidthFillUint64
73 Check parameters to a CPU I/O Protocol service request.
75 The I/O operations are carried out exactly as requested. The caller is responsible
76 for satisfying any alignment and I/O width restrictions that a PI System on a
77 platform might require. For example on some platforms, width requests of
78 EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other hand, will
79 be handled by the driver.
81 @param[in] MmioOperation TRUE for an MMIO operation, FALSE for I/O Port operation.
82 @param[in] Width Signifies the width of the I/O or Memory operation.
83 @param[in] Address The base address of the I/O operation.
84 @param[in] Count The number of I/O operations to perform. The number of
85 bytes moved is Width size * Count, starting at Address.
86 @param[in] Buffer For read operations, the destination buffer to store the results.
87 For write operations, the source buffer from which to write data.
89 @retval EFI_SUCCESS The parameters for this request pass the checks.
90 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
91 @retval EFI_INVALID_PARAMETER Buffer is NULL.
92 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
93 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
94 and Count is not valid for this PI system.
99 IN BOOLEAN MmioOperation
,
100 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
110 // Check to see if Buffer is NULL
112 if (Buffer
== NULL
) {
113 return EFI_INVALID_PARAMETER
;
117 // Check to see if Width is in the valid range
119 if (Width
< 0 || Width
>= EfiCpuIoWidthMaximum
) {
120 return EFI_INVALID_PARAMETER
;
124 // For FIFO type, the target address won't increase during the access,
125 // so treat Count as 1
127 if (Width
>= EfiCpuIoWidthFifoUint8
&& Width
<= EfiCpuIoWidthFifoUint64
) {
132 // Check to see if Width is in the valid range for I/O Port operations
134 Width
= (EFI_CPU_IO_PROTOCOL_WIDTH
) (Width
& 0x03);
135 if (!MmioOperation
&& (Width
== EfiCpuIoWidthUint64
)) {
136 return EFI_INVALID_PARAMETER
;
140 // Check to see if Address is aligned
142 if ((Address
& (UINT64
)(mInStride
[Width
] - 1)) != 0) {
143 return EFI_UNSUPPORTED
;
147 // Check to see if any address associated with this transfer exceeds the maximum
148 // allowed address. The maximum address implied by the parameters passed in is
149 // Address + Size * Count. If the following condition is met, then the transfer
152 // Address + Size * Count > (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS) + 1
154 // Since MAX_ADDRESS can be the maximum integer value supported by the CPU and Count
155 // can also be the maximum integer value supported by the CPU, this range
156 // check must be adjusted to avoid all overflow conditions.
158 // The following form of the range check is equivalent but assumes that
159 // MAX_ADDRESS and MAX_IO_PORT_ADDRESS are of the form (2^n - 1).
161 Limit
= (MmioOperation
? MAX_ADDRESS
: MAX_IO_PORT_ADDRESS
);
163 if (Address
> Limit
) {
164 return EFI_UNSUPPORTED
;
167 MaxCount
= RShiftU64 (Limit
, Width
);
168 if (MaxCount
< (Count
- 1)) {
169 return EFI_UNSUPPORTED
;
171 if (Address
> LShiftU64 (MaxCount
- Count
+ 1, Width
)) {
172 return EFI_UNSUPPORTED
;
177 // Check to see if Buffer is aligned
179 if (((UINTN
)Buffer
& (mInStride
[Width
] - 1)) != 0) {
180 return EFI_UNSUPPORTED
;
187 Reads memory-mapped registers.
189 The I/O operations are carried out exactly as requested. The caller is responsible
190 for satisfying any alignment and I/O width restrictions that a PI System on a
191 platform might require. For example on some platforms, width requests of
192 EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other hand, will
193 be handled by the driver.
195 If Width is EfiCpuIoWidthUint8, EfiCpuIoWidthUint16, EfiCpuIoWidthUint32,
196 or EfiCpuIoWidthUint64, then both Address and Buffer are incremented for
197 each of the Count operations that is performed.
199 If Width is EfiCpuIoWidthFifoUint8, EfiCpuIoWidthFifoUint16,
200 EfiCpuIoWidthFifoUint32, or EfiCpuIoWidthFifoUint64, then only Buffer is
201 incremented for each of the Count operations that is performed. The read or
202 write operation is performed Count times on the same Address.
204 If Width is EfiCpuIoWidthFillUint8, EfiCpuIoWidthFillUint16,
205 EfiCpuIoWidthFillUint32, or EfiCpuIoWidthFillUint64, then only Address is
206 incremented for each of the Count operations that is performed. The read or
207 write operation is performed Count times from the first element of Buffer.
209 @param[in] This A pointer to the EFI_CPU_IO_PROTOCOL instance.
210 @param[in] Width Signifies the width of the I/O or Memory operation.
211 @param[in] Address The base address of the I/O operation.
212 @param[in] Count The number of I/O operations to perform. The number of
213 bytes moved is Width size * Count, starting at Address.
214 @param[out] Buffer For read operations, the destination buffer to store the results.
215 For write operations, the source buffer from which to write data.
217 @retval EFI_SUCCESS The data was read from or written to the PI system.
218 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
219 @retval EFI_INVALID_PARAMETER Buffer is NULL.
220 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
221 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
222 and Count is not valid for this PI system.
227 CpuMemoryServiceRead (
228 IN EFI_CPU_IO_PROTOCOL
*This
,
229 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
238 EFI_CPU_IO_PROTOCOL_WIDTH OperationWidth
;
241 Status
= CpuIoCheckParameter (TRUE
, Width
, Address
, Count
, Buffer
);
242 if (EFI_ERROR (Status
)) {
247 // Select loop based on the width of the transfer
249 InStride
= mInStride
[Width
];
250 OutStride
= mOutStride
[Width
];
251 OperationWidth
= (EFI_CPU_IO_PROTOCOL_WIDTH
) (Width
& 0x03);
252 for (Uint8Buffer
= Buffer
; Count
> 0; Address
+= InStride
, Uint8Buffer
+= OutStride
, Count
--) {
253 if (OperationWidth
== EfiCpuIoWidthUint8
) {
254 *Uint8Buffer
= MmioRead8 ((UINTN
)Address
);
255 } else if (OperationWidth
== EfiCpuIoWidthUint16
) {
256 *((UINT16
*)Uint8Buffer
) = MmioRead16 ((UINTN
)Address
);
257 } else if (OperationWidth
== EfiCpuIoWidthUint32
) {
258 *((UINT32
*)Uint8Buffer
) = MmioRead32 ((UINTN
)Address
);
259 } else if (OperationWidth
== EfiCpuIoWidthUint64
) {
260 *((UINT64
*)Uint8Buffer
) = MmioRead64 ((UINTN
)Address
);
267 Writes memory-mapped registers.
269 The I/O operations are carried out exactly as requested. The caller is responsible
270 for satisfying any alignment and I/O width restrictions that a PI System on a
271 platform might require. For example on some platforms, width requests of
272 EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other hand, will
273 be handled by the driver.
275 If Width is EfiCpuIoWidthUint8, EfiCpuIoWidthUint16, EfiCpuIoWidthUint32,
276 or EfiCpuIoWidthUint64, then both Address and Buffer are incremented for
277 each of the Count operations that is performed.
279 If Width is EfiCpuIoWidthFifoUint8, EfiCpuIoWidthFifoUint16,
280 EfiCpuIoWidthFifoUint32, or EfiCpuIoWidthFifoUint64, then only Buffer is
281 incremented for each of the Count operations that is performed. The read or
282 write operation is performed Count times on the same Address.
284 If Width is EfiCpuIoWidthFillUint8, EfiCpuIoWidthFillUint16,
285 EfiCpuIoWidthFillUint32, or EfiCpuIoWidthFillUint64, then only Address is
286 incremented for each of the Count operations that is performed. The read or
287 write operation is performed Count times from the first element of Buffer.
289 @param[in] This A pointer to the EFI_CPU_IO_PROTOCOL instance.
290 @param[in] Width Signifies the width of the I/O or Memory operation.
291 @param[in] Address The base address of the I/O operation.
292 @param[in] Count The number of I/O operations to perform. The number of
293 bytes moved is Width size * Count, starting at Address.
294 @param[in] Buffer For read operations, the destination buffer to store the results.
295 For write operations, the source buffer from which to write data.
297 @retval EFI_SUCCESS The data was read from or written to the PI system.
298 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
299 @retval EFI_INVALID_PARAMETER Buffer is NULL.
300 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
301 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
302 and Count is not valid for this PI system.
307 CpuMemoryServiceWrite (
308 IN EFI_CPU_IO_PROTOCOL
*This
,
309 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
318 EFI_CPU_IO_PROTOCOL_WIDTH OperationWidth
;
321 Status
= CpuIoCheckParameter (TRUE
, Width
, Address
, Count
, Buffer
);
322 if (EFI_ERROR (Status
)) {
327 // Select loop based on the width of the transfer
329 InStride
= mInStride
[Width
];
330 OutStride
= mOutStride
[Width
];
331 OperationWidth
= (EFI_CPU_IO_PROTOCOL_WIDTH
) (Width
& 0x03);
332 for (Uint8Buffer
= Buffer
; Count
> 0; Address
+= InStride
, Uint8Buffer
+= OutStride
, Count
--) {
333 if (OperationWidth
== EfiCpuIoWidthUint8
) {
334 MmioWrite8 ((UINTN
)Address
, *Uint8Buffer
);
335 } else if (OperationWidth
== EfiCpuIoWidthUint16
) {
336 MmioWrite16 ((UINTN
)Address
, *((UINT16
*)Uint8Buffer
));
337 } else if (OperationWidth
== EfiCpuIoWidthUint32
) {
338 MmioWrite32 ((UINTN
)Address
, *((UINT32
*)Uint8Buffer
));
339 } else if (OperationWidth
== EfiCpuIoWidthUint64
) {
340 MmioWrite64 ((UINTN
)Address
, *((UINT64
*)Uint8Buffer
));
349 The I/O operations are carried out exactly as requested. The caller is responsible
350 for satisfying any alignment and I/O width restrictions that a PI System on a
351 platform might require. For example on some platforms, width requests of
352 EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other hand, will
353 be handled by the driver.
355 If Width is EfiCpuIoWidthUint8, EfiCpuIoWidthUint16, EfiCpuIoWidthUint32,
356 or EfiCpuIoWidthUint64, then both Address and Buffer are incremented for
357 each of the Count operations that is performed.
359 If Width is EfiCpuIoWidthFifoUint8, EfiCpuIoWidthFifoUint16,
360 EfiCpuIoWidthFifoUint32, or EfiCpuIoWidthFifoUint64, then only Buffer is
361 incremented for each of the Count operations that is performed. The read or
362 write operation is performed Count times on the same Address.
364 If Width is EfiCpuIoWidthFillUint8, EfiCpuIoWidthFillUint16,
365 EfiCpuIoWidthFillUint32, or EfiCpuIoWidthFillUint64, then only Address is
366 incremented for each of the Count operations that is performed. The read or
367 write operation is performed Count times from the first element of Buffer.
369 @param[in] This A pointer to the EFI_CPU_IO_PROTOCOL instance.
370 @param[in] Width Signifies the width of the I/O or Memory operation.
371 @param[in] Address The base address of the I/O operation.
372 @param[in] Count The number of I/O operations to perform. The number of
373 bytes moved is Width size * Count, starting at Address.
374 @param[out] Buffer For read operations, the destination buffer to store the results.
375 For write operations, the source buffer from which to write data.
377 @retval EFI_SUCCESS The data was read from or written to the PI system.
378 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
379 @retval EFI_INVALID_PARAMETER Buffer is NULL.
380 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
381 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
382 and Count is not valid for this PI system.
388 IN EFI_CPU_IO_PROTOCOL
*This
,
389 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
398 EFI_CPU_IO_PROTOCOL_WIDTH OperationWidth
;
401 Status
= CpuIoCheckParameter (FALSE
, Width
, Address
, Count
, Buffer
);
402 if (EFI_ERROR (Status
)) {
407 // Select loop based on the width of the transfer
409 InStride
= mInStride
[Width
];
410 OutStride
= mOutStride
[Width
];
411 OperationWidth
= (EFI_CPU_IO_PROTOCOL_WIDTH
) (Width
& 0x03);
412 for (Uint8Buffer
= Buffer
; Count
> 0; Address
+= InStride
, Uint8Buffer
+= OutStride
, Count
--) {
413 if (OperationWidth
== EfiCpuIoWidthUint8
) {
414 *Uint8Buffer
= IoRead8 ((UINTN
)Address
);
415 } else if (OperationWidth
== EfiCpuIoWidthUint16
) {
416 *((UINT16
*)Uint8Buffer
) = IoRead16 ((UINTN
)Address
);
417 } else if (OperationWidth
== EfiCpuIoWidthUint32
) {
418 *((UINT32
*)Uint8Buffer
) = IoRead32 ((UINTN
)Address
);
428 The I/O operations are carried out exactly as requested. The caller is responsible
429 for satisfying any alignment and I/O width restrictions that a PI System on a
430 platform might require. For example on some platforms, width requests of
431 EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other hand, will
432 be handled by the driver.
434 If Width is EfiCpuIoWidthUint8, EfiCpuIoWidthUint16, EfiCpuIoWidthUint32,
435 or EfiCpuIoWidthUint64, then both Address and Buffer are incremented for
436 each of the Count operations that is performed.
438 If Width is EfiCpuIoWidthFifoUint8, EfiCpuIoWidthFifoUint16,
439 EfiCpuIoWidthFifoUint32, or EfiCpuIoWidthFifoUint64, then only Buffer is
440 incremented for each of the Count operations that is performed. The read or
441 write operation is performed Count times on the same Address.
443 If Width is EfiCpuIoWidthFillUint8, EfiCpuIoWidthFillUint16,
444 EfiCpuIoWidthFillUint32, or EfiCpuIoWidthFillUint64, then only Address is
445 incremented for each of the Count operations that is performed. The read or
446 write operation is performed Count times from the first element of Buffer.
448 @param[in] This A pointer to the EFI_CPU_IO_PROTOCOL instance.
449 @param[in] Width Signifies the width of the I/O or Memory operation.
450 @param[in] Address The base address of the I/O operation.
451 @param[in] Count The number of I/O operations to perform. The number of
452 bytes moved is Width size * Count, starting at Address.
453 @param[in] Buffer For read operations, the destination buffer to store the results.
454 For write operations, the source buffer from which to write data.
456 @retval EFI_SUCCESS The data was read from or written to the PI system.
457 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
458 @retval EFI_INVALID_PARAMETER Buffer is NULL.
459 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
460 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
461 and Count is not valid for this PI system.
467 IN EFI_CPU_IO_PROTOCOL
*This
,
468 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
477 EFI_CPU_IO_PROTOCOL_WIDTH OperationWidth
;
481 // Make sure the parameters are valid
483 Status
= CpuIoCheckParameter (FALSE
, Width
, Address
, Count
, Buffer
);
484 if (EFI_ERROR (Status
)) {
489 // Select loop based on the width of the transfer
491 InStride
= mInStride
[Width
];
492 OutStride
= mOutStride
[Width
];
493 OperationWidth
= (EFI_CPU_IO_PROTOCOL_WIDTH
) (Width
& 0x03);
494 for (Uint8Buffer
= (UINT8
*)Buffer
; Count
> 0; Address
+= InStride
, Uint8Buffer
+= OutStride
, Count
--) {
495 if (OperationWidth
== EfiCpuIoWidthUint8
) {
496 IoWrite8 ((UINTN
)Address
, *Uint8Buffer
);
497 } else if (OperationWidth
== EfiCpuIoWidthUint16
) {
498 IoWrite16 ((UINTN
)Address
, *((UINT16
*)Uint8Buffer
));
499 } else if (OperationWidth
== EfiCpuIoWidthUint32
) {
500 IoWrite32 ((UINTN
)Address
, *((UINT32
*)Uint8Buffer
));
508 The user Entry Point for module CpuIo. The user code starts with this function.
510 @param[in] ImageHandle The firmware allocated handle for the EFI image.
511 @param[in] SystemTable A pointer to the EFI System Table.
513 @retval EFI_SUCCESS The entry point is executed successfully.
514 @retval other Some error occurs when executing this entry point.
520 IN EFI_HANDLE ImageHandle
,
521 IN EFI_SYSTEM_TABLE
*SystemTable
526 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiCpuIoProtocolGuid
);
527 Status
= gBS
->InstallMultipleProtocolInterfaces (
529 &gEfiCpuIoProtocolGuid
, &mCpuIo
,
532 ASSERT_EFI_ERROR (Status
);