4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. 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 // Include common header file for this module.
20 #include "CommonHeader.h"
22 #include "DxeCpuIoLibInternal.h"
25 // Globle varible to cache pointer to CpuIo protocol.
27 STATIC EFI_CPU_IO_PROTOCOL
*mCpuIo
= NULL
;
28 STATIC EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*mPciRootBridgeIo
= NULL
;
31 The constructor function caches the pointer to CpuIo protocol.
33 The constructor function locates CpuIo protocol from protocol database.
34 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
36 @param ImageHandle The firmware allocated handle for the EFI image.
37 @param SystemTable A pointer to the EFI System Table.
39 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
45 IN EFI_HANDLE ImageHandle
,
46 IN EFI_SYSTEM_TABLE
*SystemTable
51 Status
= gBS
->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid
, NULL
, &mPciRootBridgeIo
);
52 if (EFI_ERROR (Status
)) {
53 Status
= gBS
->LocateProtocol (&gEfiCpuIoProtocolGuid
, NULL
, &mCpuIo
);
55 ASSERT_EFI_ERROR (Status
);
61 Reads registers in the EFI CPU I/O space.
63 Reads the I/O port specified by Port with registers width specified by Width.
64 The read value is returned. If such operations are not supported, then ASSERT().
65 This function must guarantee that all I/O read and write operations are serialized.
67 @param Port The base address of the I/O operation.
68 The caller is responsible for aligning the Address if required.
69 @param Width The width of the I/O operation.
71 @return Data read from registers in the EFI CPU I/O space.
78 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
84 if (mPciRootBridgeIo
!= NULL
) {
85 Status
= mPciRootBridgeIo
->Io
.Read (mPciRootBridgeIo
, Width
, Port
, 1, &Data
);
87 Status
= mCpuIo
->Io
.Read (mCpuIo
, Width
, Port
, 1, &Data
);
89 ASSERT_EFI_ERROR (Status
);
95 Writes registers in the EFI CPU I/O space.
97 Writes the I/O port specified by Port with registers width and value specified by Width
98 and Data respectively. Data is returned. If such operations are not supported, then ASSERT().
99 This function must guarantee that all I/O read and write operations are serialized.
101 @param Port The base address of the I/O operation.
102 The caller is responsible for aligning the Address if required.
103 @param Width The width of the I/O operation.
104 @param Data The value to write to the I/O port.
106 @return The paramter of Data.
113 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
119 if (mPciRootBridgeIo
!= NULL
) {
120 Status
= mPciRootBridgeIo
->Io
.Write (mPciRootBridgeIo
, Width
, Port
, 1, &Data
);
122 Status
= mCpuIo
->Io
.Write (mCpuIo
, Width
, Port
, 1, &Data
);
124 ASSERT_EFI_ERROR (Status
);
130 Reads memory-mapped registers in the EFI system memory space.
132 Reads the MMIO registers specified by Address with registers width specified by Width.
133 The read value is returned. If such operations are not supported, then ASSERT().
134 This function must guarantee that all MMIO read and write operations are serialized.
136 @param Address The MMIO register to read.
137 The caller is responsible for aligning the Address if required.
138 @param Width The width of the I/O operation.
140 @return Data read from registers in the EFI system memory space.
147 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
153 if (mPciRootBridgeIo
!= NULL
) {
154 Status
= mPciRootBridgeIo
->Mem
.Read (mPciRootBridgeIo
, Width
, Address
, 1, &Data
);
156 Status
= mCpuIo
->Mem
.Read (mCpuIo
, Width
, Address
, 1, &Data
);
158 ASSERT_EFI_ERROR (Status
);
164 Writes memory-mapped registers in the EFI system memory space.
166 Writes the MMIO registers specified by Address with registers width and value specified by Width
167 and Data respectively. Data is returned. If such operations are not supported, then ASSERT().
168 This function must guarantee that all MMIO read and write operations are serialized.
170 @param Address The MMIO register to read.
171 The caller is responsible for aligning the Address if required.
172 @param Width The width of the I/O operation.
174 @return Data read from registers in the EFI system memory space.
181 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
187 if (mPciRootBridgeIo
!= NULL
) {
188 Status
= mPciRootBridgeIo
->Mem
.Write (mPciRootBridgeIo
, Width
, Address
, 1, &Data
);
190 Status
= mCpuIo
->Mem
.Write (mCpuIo
, Width
, Address
, 1, &Data
);
192 ASSERT_EFI_ERROR (Status
);
198 Reads an 8-bit I/O port.
200 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
201 This function must guarantee that all I/O read and write operations are
204 If 8-bit I/O port operations are not supported, then ASSERT().
206 @param Port The I/O port to read.
208 @return The value read.
217 return (UINT8
)IoReadWorker (Port
, EfiCpuIoWidthUint8
);
221 Writes an 8-bit I/O port.
223 Writes the 8-bit I/O port specified by Port with the value specified by Value
224 and returns Value. This function must guarantee that all I/O read and write
225 operations are serialized.
227 If 8-bit I/O port operations are not supported, then ASSERT().
229 @param Port The I/O port to write.
230 @param Value The value to write to the I/O port.
232 @return The value written the I/O port.
242 return (UINT8
)IoWriteWorker (Port
, EfiCpuIoWidthUint8
, Value
);
246 Reads a 16-bit I/O port.
248 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
249 This function must guarantee that all I/O read and write operations are
252 If 16-bit I/O port operations are not supported, then ASSERT().
254 @param Port The I/O port to read.
256 @return The value read.
266 // Make sure Port is aligned on a 16-bit boundary.
268 ASSERT ((Port
& 1) == 0);
269 return (UINT16
)IoReadWorker (Port
, EfiCpuIoWidthUint16
);
273 Writes a 16-bit I/O port.
275 Writes the 16-bit I/O port specified by Port with the value specified by Value
276 and returns Value. This function must guarantee that all I/O read and write
277 operations are serialized.
279 If 16-bit I/O port operations are not supported, then ASSERT().
281 @param Port The I/O port to write.
282 @param Value The value to write to the I/O port.
284 @return The value written the I/O port.
295 // Make sure Port is aligned on a 16-bit boundary.
297 ASSERT ((Port
& 1) == 0);
298 return (UINT16
)IoWriteWorker (Port
, EfiCpuIoWidthUint16
, Value
);
302 Reads a 32-bit I/O port.
304 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
305 This function must guarantee that all I/O read and write operations are
308 If 32-bit I/O port operations are not supported, then ASSERT().
310 @param Port The I/O port to read.
312 @return The value read.
322 // Make sure Port is aligned on a 32-bit boundary.
324 ASSERT ((Port
& 3) == 0);
325 return (UINT32
)IoReadWorker (Port
, EfiCpuIoWidthUint32
);
329 Writes a 32-bit I/O port.
331 Writes the 32-bit I/O port specified by Port with the value specified by Value
332 and returns Value. This function must guarantee that all I/O read and write
333 operations are serialized.
335 If 32-bit I/O port operations are not supported, then ASSERT().
337 @param Port The I/O port to write.
338 @param Value The value to write to the I/O port.
340 @return The value written the I/O port.
351 // Make sure Port is aligned on a 32-bit boundary.
353 ASSERT ((Port
& 3) == 0);
354 return (UINT32
)IoWriteWorker (Port
, EfiCpuIoWidthUint32
, Value
);
358 Reads a 64-bit I/O port.
360 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
361 This function must guarantee that all I/O read and write operations are
364 If 64-bit I/O port operations are not supported, then ASSERT().
366 @param Port The I/O port to read.
368 @return The value read.
378 // Make sure Port is aligned on a 64-bit boundary.
380 ASSERT ((Port
& 7) == 0);
381 return IoReadWorker (Port
, EfiCpuIoWidthUint64
);
385 Writes a 64-bit I/O port.
387 Writes the 64-bit I/O port specified by Port with the value specified by Value
388 and returns Value. This function must guarantee that all I/O read and write
389 operations are serialized.
391 If 64-bit I/O port operations are not supported, then ASSERT().
393 @param Port The I/O port to write.
394 @param Value The value to write to the I/O port.
396 @return The value written the I/O port.
407 // Make sure Port is aligned on a 64-bit boundary.
409 ASSERT ((Port
& 7) == 0);
410 return IoWriteWorker (Port
, EfiCpuIoWidthUint64
, Value
);
414 Reads an 8-bit MMIO register.
416 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
417 returned. This function must guarantee that all MMIO read and write
418 operations are serialized.
420 If 8-bit MMIO register operations are not supported, then ASSERT().
422 @param Address The MMIO register to read.
424 @return The value read.
433 return (UINT8
)MmioReadWorker (Address
, EfiCpuIoWidthUint8
);
437 Writes an 8-bit MMIO register.
439 Writes the 8-bit MMIO register specified by Address with the value specified
440 by Value and returns Value. This function must guarantee that all MMIO read
441 and write operations are serialized.
443 If 8-bit MMIO register operations are not supported, then ASSERT().
445 @param Address The MMIO register to write.
446 @param Value The value to write to the MMIO register.
456 return (UINT8
)MmioWriteWorker (Address
, EfiCpuIoWidthUint8
, Value
);
460 Reads a 16-bit MMIO register.
462 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
463 returned. This function must guarantee that all MMIO read and write
464 operations are serialized.
466 If 16-bit MMIO register operations are not supported, then ASSERT().
468 @param Address The MMIO register to read.
470 @return The value read.
480 // Make sure Address is aligned on a 16-bit boundary.
482 ASSERT ((Address
& 1) == 0);
483 return (UINT16
)MmioReadWorker (Address
, EfiCpuIoWidthUint16
);
487 Writes a 16-bit MMIO register.
489 Writes the 16-bit MMIO register specified by Address with the value specified
490 by Value and returns Value. This function must guarantee that all MMIO read
491 and write operations are serialized.
493 If 16-bit MMIO register operations are not supported, then ASSERT().
495 @param Address The MMIO register to write.
496 @param Value The value to write to the MMIO register.
507 // Make sure Address is aligned on a 16-bit boundary.
509 ASSERT ((Address
& 1) == 0);
510 return (UINT16
)MmioWriteWorker (Address
, EfiCpuIoWidthUint16
, Value
);
514 Reads a 32-bit MMIO register.
516 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
517 returned. This function must guarantee that all MMIO read and write
518 operations are serialized.
520 If 32-bit MMIO register operations are not supported, then ASSERT().
522 @param Address The MMIO register to read.
524 @return The value read.
534 // Make sure Address is aligned on a 32-bit boundary.
536 ASSERT ((Address
& 3) == 0);
537 return (UINT32
)MmioReadWorker (Address
, EfiCpuIoWidthUint32
);
541 Writes a 32-bit MMIO register.
543 Writes the 32-bit MMIO register specified by Address with the value specified
544 by Value and returns Value. This function must guarantee that all MMIO read
545 and write operations are serialized.
547 If 32-bit MMIO register operations are not supported, then ASSERT().
549 @param Address The MMIO register to write.
550 @param Value The value to write to the MMIO register.
561 // Make sure Address is aligned on a 32-bit boundary.
563 ASSERT ((Address
& 3) == 0);
564 return (UINT32
)MmioWriteWorker (Address
, EfiCpuIoWidthUint32
, Value
);
568 Reads a 64-bit MMIO register.
570 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
571 returned. This function must guarantee that all MMIO read and write
572 operations are serialized.
574 If 64-bit MMIO register operations are not supported, then ASSERT().
576 @param Address The MMIO register to read.
578 @return The value read.
588 // Make sure Address is aligned on a 64-bit boundary.
590 ASSERT ((Address
& 7) == 0);
591 return (UINT64
)MmioReadWorker (Address
, EfiCpuIoWidthUint64
);
595 Writes a 64-bit MMIO register.
597 Writes the 64-bit MMIO register specified by Address with the value specified
598 by Value and returns Value. This function must guarantee that all MMIO read
599 and write operations are serialized.
601 If 64-bit MMIO register operations are not supported, then ASSERT().
603 @param Address The MMIO register to write.
604 @param Value The value to write to the MMIO register.
615 // Make sure Address is aligned on a 64-bit boundary.
617 ASSERT ((Address
& 7) == 0);
618 return (UINT64
)MmioWriteWorker (Address
, EfiCpuIoWidthUint64
, Value
);