2 Produces the CPU I/O 2 Protocol.
4 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include <Protocol/CpuIo2.h>
21 #include <Library/BaseLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/IoLib.h>
24 #include <Library/PcdLib.h>
25 #include <Library/UefiBootServicesTableLib.h>
27 #define MAX_IO_PORT_ADDRESS 0xFFFF
30 // Handle for the CPU I/O 2 Protocol
32 STATIC EFI_HANDLE mHandle
= NULL
;
35 // Lookup table for increment values based on transfer widths
37 STATIC CONST UINT8 mInStride
[] = {
38 1, // EfiCpuIoWidthUint8
39 2, // EfiCpuIoWidthUint16
40 4, // EfiCpuIoWidthUint32
41 8, // EfiCpuIoWidthUint64
42 0, // EfiCpuIoWidthFifoUint8
43 0, // EfiCpuIoWidthFifoUint16
44 0, // EfiCpuIoWidthFifoUint32
45 0, // EfiCpuIoWidthFifoUint64
46 1, // EfiCpuIoWidthFillUint8
47 2, // EfiCpuIoWidthFillUint16
48 4, // EfiCpuIoWidthFillUint32
49 8 // EfiCpuIoWidthFillUint64
53 // Lookup table for increment values based on transfer widths
55 STATIC CONST UINT8 mOutStride
[] = {
56 1, // EfiCpuIoWidthUint8
57 2, // EfiCpuIoWidthUint16
58 4, // EfiCpuIoWidthUint32
59 8, // EfiCpuIoWidthUint64
60 1, // EfiCpuIoWidthFifoUint8
61 2, // EfiCpuIoWidthFifoUint16
62 4, // EfiCpuIoWidthFifoUint32
63 8, // EfiCpuIoWidthFifoUint64
64 0, // EfiCpuIoWidthFillUint8
65 0, // EfiCpuIoWidthFillUint16
66 0, // EfiCpuIoWidthFillUint32
67 0 // EfiCpuIoWidthFillUint64
71 Check parameters to a CPU I/O 2 Protocol service request.
73 The I/O operations are carried out exactly as requested. The caller is responsible
74 for satisfying any alignment and I/O width restrictions that a PI System on a
75 platform might require. For example on some platforms, width requests of
76 EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other hand, will
77 be handled by the driver.
79 @param[in] MmioOperation TRUE for an MMIO operation, FALSE for I/O Port operation.
80 @param[in] Width Signifies the width of the I/O or Memory operation.
81 @param[in] Address The base address of the I/O operation.
82 @param[in] Count The number of I/O operations to perform. The number of
83 bytes moved is Width size * Count, starting at Address.
84 @param[in] Buffer For read operations, the destination buffer to store the results.
85 For write operations, the source buffer from which to write data.
87 @retval EFI_SUCCESS The parameters for this request pass the checks.
88 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
89 @retval EFI_INVALID_PARAMETER Buffer is NULL.
90 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
91 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
92 and Count is not valid for this PI system.
98 IN BOOLEAN MmioOperation
,
99 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
109 // Check to see if Buffer is NULL
111 if (Buffer
== NULL
) {
112 return EFI_INVALID_PARAMETER
;
116 // Check to see if Width is in the valid range
118 if ((UINT32
)Width
>= EfiCpuIoWidthMaximum
) {
119 return EFI_INVALID_PARAMETER
;
123 // For FIFO type, the target address won't increase during the access,
124 // so treat Count as 1
126 if (Width
>= EfiCpuIoWidthFifoUint8
&& Width
<= EfiCpuIoWidthFifoUint64
) {
131 // Check to see if Width is in the valid range for I/O Port operations
133 Width
= (EFI_CPU_IO_PROTOCOL_WIDTH
) (Width
& 0x03);
134 if (!MmioOperation
&& (Width
== EfiCpuIoWidthUint64
)) {
135 return EFI_INVALID_PARAMETER
;
139 // Check to see if Address is aligned
141 if ((Address
& (UINT64
)(mInStride
[Width
] - 1)) != 0) {
142 return EFI_UNSUPPORTED
;
146 // Check to see if any address associated with this transfer exceeds the maximum
147 // allowed address. The maximum address implied by the parameters passed in is
148 // Address + Size * Count. If the following condition is met, then the transfer
151 // Address + Size * Count > (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS) + 1
153 // Since MAX_ADDRESS can be the maximum integer value supported by the CPU and Count
154 // can also be the maximum integer value supported by the CPU, this range
155 // check must be adjusted to avoid all oveflow conditions.
157 // The following form of the range check is equivalent but assumes that
158 // MAX_ADDRESS and MAX_IO_PORT_ADDRESS are of the form (2^n - 1).
160 Limit
= (MmioOperation
? MAX_ADDRESS
: MAX_IO_PORT_ADDRESS
);
162 if (Address
> Limit
) {
163 return EFI_UNSUPPORTED
;
166 MaxCount
= RShiftU64 (Limit
, Width
);
167 if (MaxCount
< (Count
- 1)) {
168 return EFI_UNSUPPORTED
;
170 if (Address
> LShiftU64 (MaxCount
- Count
+ 1, Width
)) {
171 return EFI_UNSUPPORTED
;
176 // Check to see if Buffer is aligned
178 if (((UINTN
)Buffer
& ((MIN (sizeof (UINTN
), mInStride
[Width
]) - 1))) != 0) {
179 return EFI_UNSUPPORTED
;
186 Reads memory-mapped registers.
188 The I/O operations are carried out exactly as requested. The caller is responsible
189 for satisfying any alignment and I/O width restrictions that a PI System on a
190 platform might require. For example on some platforms, width requests of
191 EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other hand, will
192 be handled by the driver.
194 If Width is EfiCpuIoWidthUint8, EfiCpuIoWidthUint16, EfiCpuIoWidthUint32,
195 or EfiCpuIoWidthUint64, then both Address and Buffer are incremented for
196 each of the Count operations that is performed.
198 If Width is EfiCpuIoWidthFifoUint8, EfiCpuIoWidthFifoUint16,
199 EfiCpuIoWidthFifoUint32, or EfiCpuIoWidthFifoUint64, then only Buffer is
200 incremented for each of the Count operations that is performed. The read or
201 write operation is performed Count times on the same Address.
203 If Width is EfiCpuIoWidthFillUint8, EfiCpuIoWidthFillUint16,
204 EfiCpuIoWidthFillUint32, or EfiCpuIoWidthFillUint64, then only Address is
205 incremented for each of the Count operations that is performed. The read or
206 write operation is performed Count times from the first element of Buffer.
208 @param[in] This A pointer to the EFI_CPU_IO2_PROTOCOL instance.
209 @param[in] Width Signifies the width of the I/O or Memory operation.
210 @param[in] Address The base address of the I/O operation.
211 @param[in] Count The number of I/O operations to perform. The number of
212 bytes moved is Width size * Count, starting at Address.
213 @param[out] Buffer For read operations, the destination buffer to store the results.
214 For write operations, the source buffer from which to write data.
216 @retval EFI_SUCCESS The data was read from or written to the PI system.
217 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
218 @retval EFI_INVALID_PARAMETER Buffer is NULL.
219 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
220 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
221 and Count is not valid for this PI system.
227 CpuMemoryServiceRead (
228 IN EFI_CPU_IO2_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_IO2_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.
308 CpuMemoryServiceWrite (
309 IN EFI_CPU_IO2_PROTOCOL
*This
,
310 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
319 EFI_CPU_IO_PROTOCOL_WIDTH OperationWidth
;
322 Status
= CpuIoCheckParameter (TRUE
, Width
, Address
, Count
, Buffer
);
323 if (EFI_ERROR (Status
)) {
328 // Select loop based on the width of the transfer
330 InStride
= mInStride
[Width
];
331 OutStride
= mOutStride
[Width
];
332 OperationWidth
= (EFI_CPU_IO_PROTOCOL_WIDTH
) (Width
& 0x03);
333 for (Uint8Buffer
= Buffer
; Count
> 0; Address
+= InStride
, Uint8Buffer
+= OutStride
, Count
--) {
334 if (OperationWidth
== EfiCpuIoWidthUint8
) {
335 MmioWrite8 ((UINTN
)Address
, *Uint8Buffer
);
336 } else if (OperationWidth
== EfiCpuIoWidthUint16
) {
337 MmioWrite16 ((UINTN
)Address
, *((UINT16
*)Uint8Buffer
));
338 } else if (OperationWidth
== EfiCpuIoWidthUint32
) {
339 MmioWrite32 ((UINTN
)Address
, *((UINT32
*)Uint8Buffer
));
340 } else if (OperationWidth
== EfiCpuIoWidthUint64
) {
341 MmioWrite64 ((UINTN
)Address
, *((UINT64
*)Uint8Buffer
));
350 The I/O operations are carried out exactly as requested. The caller is responsible
351 for satisfying any alignment and I/O width restrictions that a PI System on a
352 platform might require. For example on some platforms, width requests of
353 EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other hand, will
354 be handled by the driver.
356 If Width is EfiCpuIoWidthUint8, EfiCpuIoWidthUint16, EfiCpuIoWidthUint32,
357 or EfiCpuIoWidthUint64, then both Address and Buffer are incremented for
358 each of the Count operations that is performed.
360 If Width is EfiCpuIoWidthFifoUint8, EfiCpuIoWidthFifoUint16,
361 EfiCpuIoWidthFifoUint32, or EfiCpuIoWidthFifoUint64, then only Buffer is
362 incremented for each of the Count operations that is performed. The read or
363 write operation is performed Count times on the same Address.
365 If Width is EfiCpuIoWidthFillUint8, EfiCpuIoWidthFillUint16,
366 EfiCpuIoWidthFillUint32, or EfiCpuIoWidthFillUint64, then only Address is
367 incremented for each of the Count operations that is performed. The read or
368 write operation is performed Count times from the first element of Buffer.
370 @param[in] This A pointer to the EFI_CPU_IO2_PROTOCOL instance.
371 @param[in] Width Signifies the width of the I/O or Memory operation.
372 @param[in] Address The base address of the I/O operation.
373 @param[in] Count The number of I/O operations to perform. The number of
374 bytes moved is Width size * Count, starting at Address.
375 @param[out] Buffer For read operations, the destination buffer to store the results.
376 For write operations, the source buffer from which to write data.
378 @retval EFI_SUCCESS The data was read from or written to the PI system.
379 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
380 @retval EFI_INVALID_PARAMETER Buffer is NULL.
381 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
382 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
383 and Count is not valid for this PI system.
390 IN EFI_CPU_IO2_PROTOCOL
*This
,
391 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
400 EFI_CPU_IO_PROTOCOL_WIDTH OperationWidth
;
403 Status
= CpuIoCheckParameter (FALSE
, Width
, Address
, Count
, Buffer
);
404 if (EFI_ERROR (Status
)) {
408 Address
+= PcdGet64 (PcdPciIoTranslation
);
411 // Select loop based on the width of the transfer
413 InStride
= mInStride
[Width
];
414 OutStride
= mOutStride
[Width
];
415 OperationWidth
= (EFI_CPU_IO_PROTOCOL_WIDTH
) (Width
& 0x03);
417 for (Uint8Buffer
= Buffer
; Count
> 0; Address
+= InStride
, Uint8Buffer
+= OutStride
, Count
--) {
418 if (OperationWidth
== EfiCpuIoWidthUint8
) {
419 *Uint8Buffer
= MmioRead8 ((UINTN
)Address
);
420 } else if (OperationWidth
== EfiCpuIoWidthUint16
) {
421 *((UINT16
*)Uint8Buffer
) = MmioRead16 ((UINTN
)Address
);
422 } else if (OperationWidth
== EfiCpuIoWidthUint32
) {
423 *((UINT32
*)Uint8Buffer
) = MmioRead32 ((UINTN
)Address
);
433 The I/O operations are carried out exactly as requested. The caller is responsible
434 for satisfying any alignment and I/O width restrictions that a PI System on a
435 platform might require. For example on some platforms, width requests of
436 EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other hand, will
437 be handled by the driver.
439 If Width is EfiCpuIoWidthUint8, EfiCpuIoWidthUint16, EfiCpuIoWidthUint32,
440 or EfiCpuIoWidthUint64, then both Address and Buffer are incremented for
441 each of the Count operations that is performed.
443 If Width is EfiCpuIoWidthFifoUint8, EfiCpuIoWidthFifoUint16,
444 EfiCpuIoWidthFifoUint32, or EfiCpuIoWidthFifoUint64, then only Buffer is
445 incremented for each of the Count operations that is performed. The read or
446 write operation is performed Count times on the same Address.
448 If Width is EfiCpuIoWidthFillUint8, EfiCpuIoWidthFillUint16,
449 EfiCpuIoWidthFillUint32, or EfiCpuIoWidthFillUint64, then only Address is
450 incremented for each of the Count operations that is performed. The read or
451 write operation is performed Count times from the first element of Buffer.
453 @param[in] This A pointer to the EFI_CPU_IO2_PROTOCOL instance.
454 @param[in] Width Signifies the width of the I/O or Memory operation.
455 @param[in] Address The base address of the I/O operation.
456 @param[in] Count The number of I/O operations to perform. The number of
457 bytes moved is Width size * Count, starting at Address.
458 @param[in] Buffer For read operations, the destination buffer to store the results.
459 For write operations, the source buffer from which to write data.
461 @retval EFI_SUCCESS The data was read from or written to the PI system.
462 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
463 @retval EFI_INVALID_PARAMETER Buffer is NULL.
464 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
465 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
466 and Count is not valid for this PI system.
473 IN EFI_CPU_IO2_PROTOCOL
*This
,
474 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
483 EFI_CPU_IO_PROTOCOL_WIDTH OperationWidth
;
487 // Make sure the parameters are valid
489 Status
= CpuIoCheckParameter (FALSE
, Width
, Address
, Count
, Buffer
);
490 if (EFI_ERROR (Status
)) {
494 Address
+= PcdGet64 (PcdPciIoTranslation
);
497 // Select loop based on the width of the transfer
499 InStride
= mInStride
[Width
];
500 OutStride
= mOutStride
[Width
];
501 OperationWidth
= (EFI_CPU_IO_PROTOCOL_WIDTH
) (Width
& 0x03);
503 for (Uint8Buffer
= (UINT8
*)Buffer
; Count
> 0; Address
+= InStride
, Uint8Buffer
+= OutStride
, Count
--) {
504 if (OperationWidth
== EfiCpuIoWidthUint8
) {
505 MmioWrite8 ((UINTN
)Address
, *Uint8Buffer
);
506 } else if (OperationWidth
== EfiCpuIoWidthUint16
) {
507 MmioWrite16 ((UINTN
)Address
, *((UINT16
*)Uint8Buffer
));
508 } else if (OperationWidth
== EfiCpuIoWidthUint32
) {
509 MmioWrite32 ((UINTN
)Address
, *((UINT32
*)Uint8Buffer
));
517 // CPU I/O 2 Protocol instance
519 STATIC EFI_CPU_IO2_PROTOCOL mCpuIo2
= {
521 CpuMemoryServiceRead
,
522 CpuMemoryServiceWrite
532 The user Entry Point for module CpuIo2Dxe. The user code starts with this function.
534 @param[in] ImageHandle The firmware allocated handle for the EFI image.
535 @param[in] SystemTable A pointer to the EFI System Table.
537 @retval EFI_SUCCESS The entry point is executed successfully.
538 @retval other Some error occurs when executing this entry point.
543 ArmPciCpuIo2Initialize (
544 IN EFI_HANDLE ImageHandle
,
545 IN EFI_SYSTEM_TABLE
*SystemTable
550 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiCpuIo2ProtocolGuid
);
551 Status
= gBS
->InstallMultipleProtocolInterfaces (
553 &gEfiCpuIo2ProtocolGuid
, &mCpuIo2
,
556 ASSERT_EFI_ERROR (Status
);