3 The implementation of I/O operation for this library instance
4 are based on EFI_CPU_IO_PROTOCOL.
6 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
7 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 #include "DxeCpuIoLibInternal.h"
25 // Globle varible to cache pointer to CpuIo protocol.
27 EFI_CPU_IO_PROTOCOL
*mCpuIo
= NULL
;
30 The constructor function caches the pointer to CpuIo protocol.
32 The constructor function locates CpuIo protocol from protocol database.
33 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
35 @param ImageHandle The firmware allocated handle for the EFI image.
36 @param SystemTable A pointer to the EFI System Table.
38 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
44 IN EFI_HANDLE ImageHandle
,
45 IN EFI_SYSTEM_TABLE
*SystemTable
50 Status
= gBS
->LocateProtocol (&gEfiCpuIoProtocolGuid
, NULL
, (VOID
**) &mCpuIo
);
51 ASSERT_EFI_ERROR (Status
);
57 Reads registers in the EFI CPU I/O space.
59 Reads the I/O port specified by Port with registers width specified by Width.
60 The read value is returned. If such operations are not supported, then ASSERT().
61 This function must guarantee that all I/O read and write operations are serialized.
63 @param Port The base address of the I/O operation.
64 The caller is responsible for aligning the Address if required.
65 @param Width The width of the I/O operation.
67 @return Data read from registers in the EFI CPU I/O space.
74 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
80 Status
= mCpuIo
->Io
.Read (mCpuIo
, Width
, Port
, 1, &Data
);
81 ASSERT_EFI_ERROR (Status
);
87 Writes registers in the EFI CPU I/O space.
89 Writes the I/O port specified by Port with registers width and value specified by Width
90 and Data respectively. Data is returned. If such operations are not supported, then ASSERT().
91 This function must guarantee that all I/O read and write operations are serialized.
93 @param Port The base address of the I/O operation.
94 The caller is responsible for aligning the Address if required.
95 @param Width The width of the I/O operation.
96 @param Data The value to write to the I/O port.
98 @return The parameter of Data.
105 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
111 Status
= mCpuIo
->Io
.Write (mCpuIo
, Width
, Port
, 1, &Data
);
112 ASSERT_EFI_ERROR (Status
);
118 Reads registers in the EFI CPU I/O space.
120 Reads the I/O port specified by Port with registers width specified by Width.
121 The port is read Count times, and the read data is stored in the provided Buffer.
123 This function must guarantee that all I/O read and write operations are serialized.
124 If such operations are not supported, then ASSERT().
126 @param Port The base address of the I/O operation.
127 The caller is responsible for aligning the Address if required.
128 @param Width The width of the I/O operation.
129 @param Count The number of times to read I/O port.
130 @param Buffer The buffer to store the read data into.
137 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
144 Status
= mCpuIo
->Io
.Read (mCpuIo
, Width
, Port
, Count
, Buffer
);
145 ASSERT_EFI_ERROR (Status
);
149 Writes registers in the EFI CPU I/O space.
151 Writes the I/O port specified by Port with registers width specified by Width.
152 The port is written Count times, and the write data is retrieved from the provided Buffer.
154 This function must guarantee that all I/O read and write operations are serialized.
155 If such operations are not supported, then ASSERT().
157 @param Port The base address of the I/O operation.
158 The caller is responsible for aligning the Address if required.
159 @param Width The width of the I/O operation.
160 @param Count The number of times to write I/O port.
161 @param Buffer The buffer to store the read data into.
168 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
175 Status
= mCpuIo
->Io
.Write (mCpuIo
, Width
, Port
, Count
, Buffer
);
176 ASSERT_EFI_ERROR (Status
);
180 Reads memory-mapped registers in the EFI system memory space.
182 Reads the MMIO registers specified by Address with registers width specified by Width.
183 The read value is returned. If such operations are not supported, then ASSERT().
184 This function must guarantee that all MMIO read and write operations are serialized.
186 @param Address The MMIO register to read.
187 The caller is responsible for aligning the Address if required.
188 @param Width The width of the I/O operation.
190 @return Data read from registers in the EFI system memory space.
197 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
203 Status
= mCpuIo
->Mem
.Read (mCpuIo
, Width
, Address
, 1, &Data
);
204 ASSERT_EFI_ERROR (Status
);
210 Writes memory-mapped registers in the EFI system memory space.
212 Writes the MMIO registers specified by Address with registers width and value specified by Width
213 and Data respectively. Data is returned. If such operations are not supported, then ASSERT().
214 This function must guarantee that all MMIO read and write operations are serialized.
216 @param Address The MMIO register to read.
217 The caller is responsible for aligning the Address if required.
218 @param Width The width of the I/O operation.
219 @param Data The value to write to the I/O port.
221 @return Data read from registers in the EFI system memory space.
228 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
234 Status
= mCpuIo
->Mem
.Write (mCpuIo
, Width
, Address
, 1, &Data
);
235 ASSERT_EFI_ERROR (Status
);
241 Reads an 8-bit I/O port.
243 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
244 This function must guarantee that all I/O read and write operations are
247 If 8-bit I/O port operations are not supported, then ASSERT().
249 @param Port The I/O port to read.
251 @return The value read.
260 return (UINT8
)IoReadWorker (Port
, EfiCpuIoWidthUint8
);
264 Writes an 8-bit I/O port.
266 Writes the 8-bit I/O port specified by Port with the value specified by Value
267 and returns Value. This function must guarantee that all I/O read and write
268 operations are serialized.
270 If 8-bit I/O port operations are not supported, then ASSERT().
272 @param Port The I/O port to write.
273 @param Value The value to write to the I/O port.
275 @return The value written the I/O port.
285 return (UINT8
)IoWriteWorker (Port
, EfiCpuIoWidthUint8
, Value
);
289 Reads a 16-bit I/O port.
291 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
292 This function must guarantee that all I/O read and write operations are
295 If Port is not aligned on a 16-bit boundary, then ASSERT().
297 If 16-bit I/O port operations are not supported, then ASSERT().
299 @param Port The I/O port to read.
301 @return The value read.
311 // Make sure Port is aligned on a 16-bit boundary.
313 ASSERT ((Port
& 1) == 0);
314 return (UINT16
)IoReadWorker (Port
, EfiCpuIoWidthUint16
);
318 Writes a 16-bit I/O port.
320 Writes the 16-bit I/O port specified by Port with the value specified by Value
321 and returns Value. This function must guarantee that all I/O read and write
322 operations are serialized.
324 If Port is not aligned on a 16-bit boundary, then ASSERT().
326 If 16-bit I/O port operations are not supported, then ASSERT().
328 @param Port The I/O port to write.
329 @param Value The value to write to the I/O port.
331 @return The value written the I/O port.
342 // Make sure Port is aligned on a 16-bit boundary.
344 ASSERT ((Port
& 1) == 0);
345 return (UINT16
)IoWriteWorker (Port
, EfiCpuIoWidthUint16
, Value
);
349 Reads a 32-bit I/O port.
351 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
352 This function must guarantee that all I/O read and write operations are
355 If Port is not aligned on a 32-bit boundary, then ASSERT().
357 If 32-bit I/O port operations are not supported, then ASSERT().
359 @param Port The I/O port to read.
361 @return The value read.
371 // Make sure Port is aligned on a 32-bit boundary.
373 ASSERT ((Port
& 3) == 0);
374 return (UINT32
)IoReadWorker (Port
, EfiCpuIoWidthUint32
);
378 Writes a 32-bit I/O port.
380 Writes the 32-bit I/O port specified by Port with the value specified by Value
381 and returns Value. This function must guarantee that all I/O read and write
382 operations are serialized.
384 If Port is not aligned on a 32-bit boundary, then ASSERT().
386 If 32-bit I/O port operations are not supported, then ASSERT().
388 @param Port The I/O port to write.
389 @param Value The value to write to the I/O port.
391 @return The value written the I/O port.
402 // Make sure Port is aligned on a 32-bit boundary.
404 ASSERT ((Port
& 3) == 0);
405 return (UINT32
)IoWriteWorker (Port
, EfiCpuIoWidthUint32
, Value
);
409 Reads a 64-bit I/O port.
411 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
412 This function must guarantee that all I/O read and write operations are
415 If Port is not aligned on a 64-bit boundary, then ASSERT().
417 If 64-bit I/O port operations are not supported, then ASSERT().
419 @param Port The I/O port to read.
421 @return The value read.
431 // Make sure Port is aligned on a 64-bit boundary.
433 ASSERT ((Port
& 7) == 0);
434 return IoReadWorker (Port
, EfiCpuIoWidthUint64
);
438 Writes a 64-bit I/O port.
440 Writes the 64-bit I/O port specified by Port with the value specified by Value
441 and returns Value. This function must guarantee that all I/O read and write
442 operations are serialized.
444 If Port is not aligned on a 64-bit boundary, then ASSERT().
446 If 64-bit I/O port operations are not supported, then ASSERT().
448 @param Port The I/O port to write.
449 @param Value The value to write to the I/O port.
451 @return The value written the I/O port.
462 // Make sure Port is aligned on a 64-bit boundary.
464 ASSERT ((Port
& 7) == 0);
465 return IoWriteWorker (Port
, EfiCpuIoWidthUint64
, Value
);
469 Reads an 8-bit I/O port fifo into a block of memory.
471 Reads the 8-bit I/O fifo port specified by Port.
472 The port is read Count times, and the read data is
473 stored in the provided Buffer.
475 This function must guarantee that all I/O read and write operations are
478 If 8-bit I/O port operations are not supported, then ASSERT().
480 @param Port The I/O port to read.
481 @param Count The number of times to read I/O port.
482 @param Buffer The buffer to store the read data into.
493 IoReadFifoWorker (Port
, EfiCpuIoWidthFifoUint8
, Count
, Buffer
);
497 Writes a block of memory into an 8-bit I/O port fifo.
499 Writes the 8-bit I/O fifo port specified by Port.
500 The port is written Count times, and the write data is
501 retrieved from the provided Buffer.
503 This function must guarantee that all I/O write and write operations are
506 If 8-bit I/O port operations are not supported, then ASSERT().
508 @param Port The I/O port to write.
509 @param Count The number of times to write I/O port.
510 @param Buffer The buffer to retrieve the write data from.
521 IoWriteFifoWorker (Port
, EfiCpuIoWidthFifoUint8
, Count
, Buffer
);
525 Reads a 16-bit I/O port fifo into a block of memory.
527 Reads the 16-bit I/O fifo port specified by Port.
528 The port is read Count times, and the read data is
529 stored in the provided Buffer.
531 This function must guarantee that all I/O read and write operations are
534 If 16-bit I/O port operations are not supported, then ASSERT().
536 @param Port The I/O port to read.
537 @param Count The number of times to read I/O port.
538 @param Buffer The buffer to store the read data into.
550 // Make sure Port is aligned on a 16-bit boundary.
552 ASSERT ((Port
& 1) == 0);
553 IoReadFifoWorker (Port
, EfiCpuIoWidthFifoUint16
, Count
, Buffer
);
557 Writes a block of memory into a 16-bit I/O port fifo.
559 Writes the 16-bit I/O fifo port specified by Port.
560 The port is written Count times, and the write data is
561 retrieved from the provided Buffer.
563 This function must guarantee that all I/O write and write operations are
566 If 16-bit I/O port operations are not supported, then ASSERT().
568 @param Port The I/O port to write.
569 @param Count The number of times to write I/O port.
570 @param Buffer The buffer to retrieve the write data from.
582 // Make sure Port is aligned on a 16-bit boundary.
584 ASSERT ((Port
& 1) == 0);
585 IoWriteFifoWorker (Port
, EfiCpuIoWidthFifoUint16
, Count
, Buffer
);
589 Reads a 32-bit I/O port fifo into a block of memory.
591 Reads the 32-bit I/O fifo port specified by Port.
592 The port is read Count times, and the read data is
593 stored in the provided Buffer.
595 This function must guarantee that all I/O read and write operations are
598 If 32-bit I/O port operations are not supported, then ASSERT().
600 @param Port The I/O port to read.
601 @param Count The number of times to read I/O port.
602 @param Buffer The buffer to store the read data into.
614 // Make sure Port is aligned on a 32-bit boundary.
616 ASSERT ((Port
& 3) == 0);
617 IoReadFifoWorker (Port
, EfiCpuIoWidthFifoUint32
, Count
, Buffer
);
621 Writes a block of memory into a 32-bit I/O port fifo.
623 Writes the 32-bit I/O fifo port specified by Port.
624 The port is written Count times, and the write data is
625 retrieved from the provided Buffer.
627 This function must guarantee that all I/O write and write operations are
630 If 32-bit I/O port operations are not supported, then ASSERT().
632 @param Port The I/O port to write.
633 @param Count The number of times to write I/O port.
634 @param Buffer The buffer to retrieve the write data from.
646 // Make sure Port is aligned on a 32-bit boundary.
648 ASSERT ((Port
& 3) == 0);
649 IoWriteFifoWorker (Port
, EfiCpuIoWidthFifoUint32
, Count
, Buffer
);
653 Reads an 8-bit MMIO register.
655 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
656 returned. This function must guarantee that all MMIO read and write
657 operations are serialized.
659 If 8-bit MMIO register operations are not supported, then ASSERT().
661 @param Address The MMIO register to read.
663 @return The value read.
672 return (UINT8
)MmioReadWorker (Address
, EfiCpuIoWidthUint8
);
676 Writes an 8-bit MMIO register.
678 Writes the 8-bit MMIO register specified by Address with the value specified
679 by Value and returns Value. This function must guarantee that all MMIO read
680 and write operations are serialized.
682 If 8-bit MMIO register operations are not supported, then ASSERT().
684 @param Address The MMIO register to write.
685 @param Value The value to write to the MMIO register.
695 return (UINT8
)MmioWriteWorker (Address
, EfiCpuIoWidthUint8
, Value
);
699 Reads a 16-bit MMIO register.
701 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
702 returned. This function must guarantee that all MMIO read and write
703 operations are serialized.
705 If Address is not aligned on a 16-bit boundary, then ASSERT().
707 If 16-bit MMIO register operations are not supported, then ASSERT().
709 @param Address The MMIO register to read.
711 @return The value read.
721 // Make sure Address is aligned on a 16-bit boundary.
723 ASSERT ((Address
& 1) == 0);
724 return (UINT16
)MmioReadWorker (Address
, EfiCpuIoWidthUint16
);
728 Writes a 16-bit MMIO register.
730 Writes the 16-bit MMIO register specified by Address with the value specified
731 by Value and returns Value. This function must guarantee that all MMIO read
732 and write operations are serialized.
734 If Address is not aligned on a 16-bit boundary, then ASSERT().
736 If 16-bit MMIO register operations are not supported, then ASSERT().
738 @param Address The MMIO register to write.
739 @param Value The value to write to the MMIO register.
750 // Make sure Address is aligned on a 16-bit boundary.
752 ASSERT ((Address
& 1) == 0);
753 return (UINT16
)MmioWriteWorker (Address
, EfiCpuIoWidthUint16
, Value
);
757 Reads a 32-bit MMIO register.
759 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
760 returned. This function must guarantee that all MMIO read and write
761 operations are serialized.
763 If Address is not aligned on a 32-bit boundary, then ASSERT().
765 If 32-bit MMIO register operations are not supported, then ASSERT().
767 @param Address The MMIO register to read.
769 @return The value read.
779 // Make sure Address is aligned on a 32-bit boundary.
781 ASSERT ((Address
& 3) == 0);
782 return (UINT32
)MmioReadWorker (Address
, EfiCpuIoWidthUint32
);
786 Writes a 32-bit MMIO register.
788 Writes the 32-bit MMIO register specified by Address with the value specified
789 by Value and returns Value. This function must guarantee that all MMIO read
790 and write operations are serialized.
792 If Address is not aligned on a 32-bit boundary, then ASSERT().
794 If 32-bit MMIO register operations are not supported, then ASSERT().
796 @param Address The MMIO register to write.
797 @param Value The value to write to the MMIO register.
808 // Make sure Address is aligned on a 32-bit boundary.
810 ASSERT ((Address
& 3) == 0);
811 return (UINT32
)MmioWriteWorker (Address
, EfiCpuIoWidthUint32
, Value
);
815 Reads a 64-bit MMIO register.
817 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
818 returned. This function must guarantee that all MMIO read and write
819 operations are serialized.
821 If Address is not aligned on a 64-bit boundary, then ASSERT().
823 If 64-bit MMIO register operations are not supported, then ASSERT().
825 @param Address The MMIO register to read.
827 @return The value read.
837 // Make sure Address is aligned on a 64-bit boundary.
839 ASSERT ((Address
& 7) == 0);
840 return (UINT64
)MmioReadWorker (Address
, EfiCpuIoWidthUint64
);
844 Writes a 64-bit MMIO register.
846 Writes the 64-bit MMIO register specified by Address with the value specified
847 by Value and returns Value. This function must guarantee that all MMIO read
848 and write operations are serialized.
850 If Address is not aligned on a 64-bit boundary, then ASSERT().
852 If 64-bit MMIO register operations are not supported, then ASSERT().
854 @param Address The MMIO register to write.
855 @param Value The value to write to the MMIO register.
866 // Make sure Address is aligned on a 64-bit boundary.
868 ASSERT ((Address
& 7) == 0);
869 return (UINT64
)MmioWriteWorker (Address
, EfiCpuIoWidthUint64
, Value
);