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 EFI_CPU_IO_PROTOCOL
*mCpuIo
= NULL
;
26 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 Port is not aligned on a 16-bit boundary, then ASSERT().
253 If 16-bit I/O port operations are not supported, then ASSERT().
255 @param Port The I/O port to read.
257 @return The value read.
267 // Make sure Port is aligned on a 16-bit boundary.
269 ASSERT ((Port
& 1) == 0);
270 return (UINT16
)IoReadWorker (Port
, EfiCpuIoWidthUint16
);
274 Writes a 16-bit I/O port.
276 Writes the 16-bit I/O port specified by Port with the value specified by Value
277 and returns Value. This function must guarantee that all I/O read and write
278 operations are serialized.
280 If Port is not aligned on a 16-bit boundary, then ASSERT().
282 If 16-bit I/O port operations are not supported, then ASSERT().
284 @param Port The I/O port to write.
285 @param Value The value to write to the I/O port.
287 @return The value written the I/O port.
298 // Make sure Port is aligned on a 16-bit boundary.
300 ASSERT ((Port
& 1) == 0);
301 return (UINT16
)IoWriteWorker (Port
, EfiCpuIoWidthUint16
, Value
);
305 Reads a 32-bit I/O port.
307 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
308 This function must guarantee that all I/O read and write operations are
311 If Port is not aligned on a 32-bit boundary, then ASSERT().
313 If 32-bit I/O port operations are not supported, then ASSERT().
315 @param Port The I/O port to read.
317 @return The value read.
327 // Make sure Port is aligned on a 32-bit boundary.
329 ASSERT ((Port
& 3) == 0);
330 return (UINT32
)IoReadWorker (Port
, EfiCpuIoWidthUint32
);
334 Writes a 32-bit I/O port.
336 Writes the 32-bit I/O port specified by Port with the value specified by Value
337 and returns Value. This function must guarantee that all I/O read and write
338 operations are serialized.
340 If Port is not aligned on a 32-bit boundary, then ASSERT().
342 If 32-bit I/O port operations are not supported, then ASSERT().
344 @param Port The I/O port to write.
345 @param Value The value to write to the I/O port.
347 @return The value written the I/O port.
358 // Make sure Port is aligned on a 32-bit boundary.
360 ASSERT ((Port
& 3) == 0);
361 return (UINT32
)IoWriteWorker (Port
, EfiCpuIoWidthUint32
, Value
);
365 Reads a 64-bit I/O port.
367 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
368 This function must guarantee that all I/O read and write operations are
371 If Port is not aligned on a 64-bit boundary, then ASSERT().
373 If 64-bit I/O port operations are not supported, then ASSERT().
375 @param Port The I/O port to read.
377 @return The value read.
387 // Make sure Port is aligned on a 64-bit boundary.
389 ASSERT ((Port
& 7) == 0);
390 return IoReadWorker (Port
, EfiCpuIoWidthUint64
);
394 Writes a 64-bit I/O port.
396 Writes the 64-bit I/O port specified by Port with the value specified by Value
397 and returns Value. This function must guarantee that all I/O read and write
398 operations are serialized.
400 If Port is not aligned on a 64-bit boundary, then ASSERT().
402 If 64-bit I/O port operations are not supported, then ASSERT().
404 @param Port The I/O port to write.
405 @param Value The value to write to the I/O port.
407 @return The value written the I/O port.
418 // Make sure Port is aligned on a 64-bit boundary.
420 ASSERT ((Port
& 7) == 0);
421 return IoWriteWorker (Port
, EfiCpuIoWidthUint64
, Value
);
425 Reads an 8-bit MMIO register.
427 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
428 returned. This function must guarantee that all MMIO read and write
429 operations are serialized.
431 If 8-bit MMIO register operations are not supported, then ASSERT().
433 @param Address The MMIO register to read.
435 @return The value read.
444 return (UINT8
)MmioReadWorker (Address
, EfiCpuIoWidthUint8
);
448 Writes an 8-bit MMIO register.
450 Writes the 8-bit MMIO register specified by Address with the value specified
451 by Value and returns Value. This function must guarantee that all MMIO read
452 and write operations are serialized.
454 If 8-bit MMIO register operations are not supported, then ASSERT().
456 @param Address The MMIO register to write.
457 @param Value The value to write to the MMIO register.
467 return (UINT8
)MmioWriteWorker (Address
, EfiCpuIoWidthUint8
, Value
);
471 Reads a 16-bit MMIO register.
473 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
474 returned. This function must guarantee that all MMIO read and write
475 operations are serialized.
477 If Address is not aligned on a 16-bit boundary, then ASSERT().
479 If 16-bit MMIO register operations are not supported, then ASSERT().
481 @param Address The MMIO register to read.
483 @return The value read.
493 // Make sure Address is aligned on a 16-bit boundary.
495 ASSERT ((Address
& 1) == 0);
496 return (UINT16
)MmioReadWorker (Address
, EfiCpuIoWidthUint16
);
500 Writes a 16-bit MMIO register.
502 Writes the 16-bit MMIO register specified by Address with the value specified
503 by Value and returns Value. This function must guarantee that all MMIO read
504 and write operations are serialized.
506 If Address is not aligned on a 16-bit boundary, then ASSERT().
508 If 16-bit MMIO register operations are not supported, then ASSERT().
510 @param Address The MMIO register to write.
511 @param Value The value to write to the MMIO register.
522 // Make sure Address is aligned on a 16-bit boundary.
524 ASSERT ((Address
& 1) == 0);
525 return (UINT16
)MmioWriteWorker (Address
, EfiCpuIoWidthUint16
, Value
);
529 Reads a 32-bit MMIO register.
531 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
532 returned. This function must guarantee that all MMIO read and write
533 operations are serialized.
535 If Address is not aligned on a 32-bit boundary, then ASSERT().
537 If 32-bit MMIO register operations are not supported, then ASSERT().
539 @param Address The MMIO register to read.
541 @return The value read.
551 // Make sure Address is aligned on a 32-bit boundary.
553 ASSERT ((Address
& 3) == 0);
554 return (UINT32
)MmioReadWorker (Address
, EfiCpuIoWidthUint32
);
558 Writes a 32-bit MMIO register.
560 Writes the 32-bit MMIO register specified by Address with the value specified
561 by Value and returns Value. This function must guarantee that all MMIO read
562 and write operations are serialized.
564 If Address is not aligned on a 32-bit boundary, then ASSERT().
566 If 32-bit MMIO register operations are not supported, then ASSERT().
568 @param Address The MMIO register to write.
569 @param Value The value to write to the MMIO register.
580 // Make sure Address is aligned on a 32-bit boundary.
582 ASSERT ((Address
& 3) == 0);
583 return (UINT32
)MmioWriteWorker (Address
, EfiCpuIoWidthUint32
, Value
);
587 Reads a 64-bit MMIO register.
589 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
590 returned. This function must guarantee that all MMIO read and write
591 operations are serialized.
593 If Address is not aligned on a 64-bit boundary, then ASSERT().
595 If 64-bit MMIO register operations are not supported, then ASSERT().
597 @param Address The MMIO register to read.
599 @return The value read.
609 // Make sure Address is aligned on a 64-bit boundary.
611 ASSERT ((Address
& 7) == 0);
612 return (UINT64
)MmioReadWorker (Address
, EfiCpuIoWidthUint64
);
616 Writes a 64-bit MMIO register.
618 Writes the 64-bit MMIO register specified by Address with the value specified
619 by Value and returns Value. This function must guarantee that all MMIO read
620 and write operations are serialized.
622 If Address is not aligned on a 64-bit boundary, then ASSERT().
624 If 64-bit MMIO register operations are not supported, then ASSERT().
626 @param Address The MMIO register to write.
627 @param Value The value to write to the MMIO register.
638 // Make sure Address is aligned on a 64-bit boundary.
640 ASSERT ((Address
& 7) == 0);
641 return (UINT64
)MmioWriteWorker (Address
, EfiCpuIoWidthUint64
, Value
);