3 The implementation of I/O operation for this library instance
4 are based on EFI_CPU_IO_PROTOCOL.
6 Copyright (c) 2006, Intel Corporation<BR>
7 All rights reserved. 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.
20 #include "DxeCpuIoLibInternal.h"
23 // Globle varible to cache pointer to CpuIo protocol.
25 STATIC EFI_CPU_IO_PROTOCOL
*mCpuIo
= NULL
;
26 STATIC EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*mPciRootBridgeIo
= NULL
;
29 The constructor function caches the pointer to CpuIo protocol.
31 The constructor function locates CpuIo protocol from protocol database.
32 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
34 @param ImageHandle The firmware allocated handle for the EFI image.
35 @param SystemTable A pointer to the EFI System Table.
37 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
43 IN EFI_HANDLE ImageHandle
,
44 IN EFI_SYSTEM_TABLE
*SystemTable
49 Status
= gBS
->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid
, NULL
, (VOID
**) &mPciRootBridgeIo
);
50 if (EFI_ERROR (Status
)) {
51 Status
= gBS
->LocateProtocol (&gEfiCpuIoProtocolGuid
, NULL
, (VOID
**) &mCpuIo
);
53 ASSERT_EFI_ERROR (Status
);
59 Reads registers in the EFI CPU I/O space.
61 Reads the I/O port specified by Port with registers width specified by Width.
62 The read value is returned. If such operations are not supported, then ASSERT().
63 This function must guarantee that all I/O read and write operations are serialized.
65 @param Port The base address of the I/O operation.
66 The caller is responsible for aligning the Address if required.
67 @param Width The width of the I/O operation.
69 @return Data read from registers in the EFI CPU I/O space.
76 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
82 if (mPciRootBridgeIo
!= NULL
) {
83 Status
= mPciRootBridgeIo
->Io
.Read (mPciRootBridgeIo
, (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
) Width
, Port
, 1, &Data
);
85 Status
= mCpuIo
->Io
.Read (mCpuIo
, Width
, Port
, 1, &Data
);
87 ASSERT_EFI_ERROR (Status
);
93 Writes registers in the EFI CPU I/O space.
95 Writes the I/O port specified by Port with registers width and value specified by Width
96 and Data respectively. Data is returned. If such operations are not supported, then ASSERT().
97 This function must guarantee that all I/O read and write operations are serialized.
99 @param Port The base address of the I/O operation.
100 The caller is responsible for aligning the Address if required.
101 @param Width The width of the I/O operation.
102 @param Data The value to write to the I/O port.
104 @return The paramter of Data.
111 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
117 if (mPciRootBridgeIo
!= NULL
) {
118 Status
= mPciRootBridgeIo
->Io
.Write (mPciRootBridgeIo
, (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
) Width
, Port
, 1, &Data
);
120 Status
= mCpuIo
->Io
.Write (mCpuIo
, Width
, Port
, 1, &Data
);
122 ASSERT_EFI_ERROR (Status
);
128 Reads memory-mapped registers in the EFI system memory space.
130 Reads the MMIO registers specified by Address with registers width specified by Width.
131 The read value is returned. If such operations are not supported, then ASSERT().
132 This function must guarantee that all MMIO read and write operations are serialized.
134 @param Address The MMIO register to read.
135 The caller is responsible for aligning the Address if required.
136 @param Width The width of the I/O operation.
138 @return Data read from registers in the EFI system memory space.
145 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
151 if (mPciRootBridgeIo
!= NULL
) {
152 Status
= mPciRootBridgeIo
->Mem
.Read (mPciRootBridgeIo
, (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
) Width
, Address
, 1, &Data
);
154 Status
= mCpuIo
->Mem
.Read (mCpuIo
, Width
, Address
, 1, &Data
);
156 ASSERT_EFI_ERROR (Status
);
162 Writes memory-mapped registers in the EFI system memory space.
164 Writes the MMIO registers specified by Address with registers width and value specified by Width
165 and Data respectively. Data is returned. If such operations are not supported, then ASSERT().
166 This function must guarantee that all MMIO read and write operations are serialized.
168 @param Address The MMIO register to read.
169 The caller is responsible for aligning the Address if required.
170 @param Width The width of the I/O operation.
171 @param Data The value to write to the I/O port.
173 @return Data read from registers in the EFI system memory space.
180 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
186 if (mPciRootBridgeIo
!= NULL
) {
187 Status
= mPciRootBridgeIo
->Mem
.Write (mPciRootBridgeIo
, (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH
) Width
, Address
, 1, &Data
);
189 Status
= mCpuIo
->Mem
.Write (mCpuIo
, Width
, Address
, 1, &Data
);
191 ASSERT_EFI_ERROR (Status
);
197 Reads an 8-bit I/O port.
199 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
200 This function must guarantee that all I/O read and write operations are
203 If 8-bit I/O port operations are not supported, then ASSERT().
205 @param Port The I/O port to read.
207 @return The value read.
216 return (UINT8
)IoReadWorker (Port
, EfiCpuIoWidthUint8
);
220 Writes an 8-bit I/O port.
222 Writes the 8-bit I/O port specified by Port with the value specified by Value
223 and returns Value. This function must guarantee that all I/O read and write
224 operations are serialized.
226 If 8-bit I/O port operations are not supported, then ASSERT().
228 @param Port The I/O port to write.
229 @param Value The value to write to the I/O port.
231 @return The value written the I/O port.
241 return (UINT8
)IoWriteWorker (Port
, EfiCpuIoWidthUint8
, Value
);
245 Reads a 16-bit I/O port.
247 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
248 This function must guarantee that all I/O read and write operations are
251 If 16-bit I/O port operations are not supported, then ASSERT().
253 @param Port The I/O port to read.
255 @return The value read.
265 // Make sure Port is aligned on a 16-bit boundary.
267 ASSERT ((Port
& 1) == 0);
268 return (UINT16
)IoReadWorker (Port
, EfiCpuIoWidthUint16
);
272 Writes a 16-bit I/O port.
274 Writes the 16-bit I/O port specified by Port with the value specified by Value
275 and returns Value. This function must guarantee that all I/O read and write
276 operations are serialized.
278 If 16-bit I/O port operations are not supported, then ASSERT().
280 @param Port The I/O port to write.
281 @param Value The value to write to the I/O port.
283 @return The value written the I/O port.
294 // Make sure Port is aligned on a 16-bit boundary.
296 ASSERT ((Port
& 1) == 0);
297 return (UINT16
)IoWriteWorker (Port
, EfiCpuIoWidthUint16
, Value
);
301 Reads a 32-bit I/O port.
303 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
304 This function must guarantee that all I/O read and write operations are
307 If 32-bit I/O port operations are not supported, then ASSERT().
309 @param Port The I/O port to read.
311 @return The value read.
321 // Make sure Port is aligned on a 32-bit boundary.
323 ASSERT ((Port
& 3) == 0);
324 return (UINT32
)IoReadWorker (Port
, EfiCpuIoWidthUint32
);
328 Writes a 32-bit I/O port.
330 Writes the 32-bit I/O port specified by Port with the value specified by Value
331 and returns Value. This function must guarantee that all I/O read and write
332 operations are serialized.
334 If 32-bit I/O port operations are not supported, then ASSERT().
336 @param Port The I/O port to write.
337 @param Value The value to write to the I/O port.
339 @return The value written the I/O port.
350 // Make sure Port is aligned on a 32-bit boundary.
352 ASSERT ((Port
& 3) == 0);
353 return (UINT32
)IoWriteWorker (Port
, EfiCpuIoWidthUint32
, Value
);
357 Reads a 64-bit I/O port.
359 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
360 This function must guarantee that all I/O read and write operations are
363 If 64-bit I/O port operations are not supported, then ASSERT().
365 @param Port The I/O port to read.
367 @return The value read.
377 // Make sure Port is aligned on a 64-bit boundary.
379 ASSERT ((Port
& 7) == 0);
380 return IoReadWorker (Port
, EfiCpuIoWidthUint64
);
384 Writes a 64-bit I/O port.
386 Writes the 64-bit I/O port specified by Port with the value specified by Value
387 and returns Value. This function must guarantee that all I/O read and write
388 operations are serialized.
390 If 64-bit I/O port operations are not supported, then ASSERT().
392 @param Port The I/O port to write.
393 @param Value The value to write to the I/O port.
395 @return The value written the I/O port.
406 // Make sure Port is aligned on a 64-bit boundary.
408 ASSERT ((Port
& 7) == 0);
409 return IoWriteWorker (Port
, EfiCpuIoWidthUint64
, Value
);
413 Reads an 8-bit MMIO register.
415 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
416 returned. This function must guarantee that all MMIO read and write
417 operations are serialized.
419 If 8-bit MMIO register operations are not supported, then ASSERT().
421 @param Address The MMIO register to read.
423 @return The value read.
432 return (UINT8
)MmioReadWorker (Address
, EfiCpuIoWidthUint8
);
436 Writes an 8-bit MMIO register.
438 Writes the 8-bit MMIO register specified by Address with the value specified
439 by Value and returns Value. This function must guarantee that all MMIO read
440 and write operations are serialized.
442 If 8-bit MMIO register operations are not supported, then ASSERT().
444 @param Address The MMIO register to write.
445 @param Value The value to write to the MMIO register.
455 return (UINT8
)MmioWriteWorker (Address
, EfiCpuIoWidthUint8
, Value
);
459 Reads a 16-bit MMIO register.
461 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
462 returned. This function must guarantee that all MMIO read and write
463 operations are serialized.
465 If 16-bit MMIO register operations are not supported, then ASSERT().
467 @param Address The MMIO register to read.
469 @return The value read.
479 // Make sure Address is aligned on a 16-bit boundary.
481 ASSERT ((Address
& 1) == 0);
482 return (UINT16
)MmioReadWorker (Address
, EfiCpuIoWidthUint16
);
486 Writes a 16-bit MMIO register.
488 Writes the 16-bit MMIO register specified by Address with the value specified
489 by Value and returns Value. This function must guarantee that all MMIO read
490 and write operations are serialized.
492 If 16-bit MMIO register operations are not supported, then ASSERT().
494 @param Address The MMIO register to write.
495 @param Value The value to write to the MMIO register.
506 // Make sure Address is aligned on a 16-bit boundary.
508 ASSERT ((Address
& 1) == 0);
509 return (UINT16
)MmioWriteWorker (Address
, EfiCpuIoWidthUint16
, Value
);
513 Reads a 32-bit MMIO register.
515 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
516 returned. This function must guarantee that all MMIO read and write
517 operations are serialized.
519 If 32-bit MMIO register operations are not supported, then ASSERT().
521 @param Address The MMIO register to read.
523 @return The value read.
533 // Make sure Address is aligned on a 32-bit boundary.
535 ASSERT ((Address
& 3) == 0);
536 return (UINT32
)MmioReadWorker (Address
, EfiCpuIoWidthUint32
);
540 Writes a 32-bit MMIO register.
542 Writes the 32-bit MMIO register specified by Address with the value specified
543 by Value and returns Value. This function must guarantee that all MMIO read
544 and write operations are serialized.
546 If 32-bit MMIO register operations are not supported, then ASSERT().
548 @param Address The MMIO register to write.
549 @param Value The value to write to the MMIO register.
560 // Make sure Address is aligned on a 32-bit boundary.
562 ASSERT ((Address
& 3) == 0);
563 return (UINT32
)MmioWriteWorker (Address
, EfiCpuIoWidthUint32
, Value
);
567 Reads a 64-bit MMIO register.
569 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
570 returned. This function must guarantee that all MMIO read and write
571 operations are serialized.
573 If 64-bit MMIO register operations are not supported, then ASSERT().
575 @param Address The MMIO register to read.
577 @return The value read.
587 // Make sure Address is aligned on a 64-bit boundary.
589 ASSERT ((Address
& 7) == 0);
590 return (UINT64
)MmioReadWorker (Address
, EfiCpuIoWidthUint64
);
594 Writes a 64-bit MMIO register.
596 Writes the 64-bit MMIO register specified by Address with the value specified
597 by Value and returns Value. This function must guarantee that all MMIO read
598 and write operations are serialized.
600 If 64-bit MMIO register operations are not supported, then ASSERT().
602 @param Address The MMIO register to write.
603 @param Value The value to write to the MMIO register.
614 // Make sure Address is aligned on a 64-bit boundary.
616 ASSERT ((Address
& 7) == 0);
617 return (UINT64
)MmioWriteWorker (Address
, EfiCpuIoWidthUint64
, Value
);