2 I/O Library instance based on EFI_CPU_IO2_PROTOCOL.
4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "DxeCpuIo2LibInternal.h"
14 // Global variable to cache pointer to CpuIo2 protocol.
16 EFI_CPU_IO2_PROTOCOL
*mCpuIo
= NULL
;
19 The constructor function caches the pointer to CpuIo2 protocol.
21 The constructor function locates CpuIo2 protocol from protocol database.
22 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
24 @param ImageHandle The firmware allocated handle for the EFI image.
25 @param SystemTable A pointer to the EFI System Table.
27 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
33 IN EFI_HANDLE ImageHandle
,
34 IN EFI_SYSTEM_TABLE
*SystemTable
39 Status
= gBS
->LocateProtocol (&gEfiCpuIo2ProtocolGuid
, NULL
, (VOID
**)&mCpuIo
);
40 ASSERT_EFI_ERROR (Status
);
46 Reads registers in the EFI CPU I/O space.
48 Reads the I/O port specified by Port with registers width specified by Width.
49 The read value is returned.
51 This function must guarantee that all I/O read and write operations are serialized.
52 If such operations are not supported, then ASSERT().
54 @param Port The base address of the I/O operation.
55 The caller is responsible for aligning the Address if required.
56 @param Width The width of the I/O operation.
58 @return Data read from registers in the EFI CPU I/O space.
65 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
71 Status
= mCpuIo
->Io
.Read (mCpuIo
, Width
, Port
, 1, &Data
);
72 ASSERT_EFI_ERROR (Status
);
78 Writes registers in the EFI CPU I/O space.
80 Writes the I/O port specified by Port with registers width and value specified by Width
81 and Data respectively. Data is returned.
83 This function must guarantee that all I/O read and write operations are serialized.
84 If such operations are not supported, then ASSERT().
86 @param Port The base address of the I/O operation.
87 The caller is responsible for aligning the Address if required.
88 @param Width The width of the I/O operation.
89 @param Data The value to write to the I/O port.
91 @return The parameter of Data.
98 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
104 Status
= mCpuIo
->Io
.Write (mCpuIo
, Width
, Port
, 1, &Data
);
105 ASSERT_EFI_ERROR (Status
);
111 Reads registers in the EFI CPU I/O space.
113 Reads the I/O port specified by Port with registers width specified by Width.
114 The port is read Count times, and the read data is stored in the provided Buffer.
116 This function must guarantee that all I/O read and write operations are serialized.
117 If such operations are not supported, then ASSERT().
119 @param Port The base address of the I/O operation.
120 The caller is responsible for aligning the Address if required.
121 @param Width The width of the I/O operation.
122 @param Count The number of times to read I/O port.
123 @param Buffer The buffer to store the read data into.
130 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
137 Status
= mCpuIo
->Io
.Read (mCpuIo
, Width
, Port
, Count
, Buffer
);
138 ASSERT_EFI_ERROR (Status
);
142 Writes registers in the EFI CPU I/O space.
144 Writes the I/O port specified by Port with registers width specified by Width.
145 The port is written Count times, and the write data is retrieved from the provided Buffer.
147 This function must guarantee that all I/O read and write operations are serialized.
148 If such operations are not supported, then ASSERT().
150 @param Port The base address of the I/O operation.
151 The caller is responsible for aligning the Address if required.
152 @param Width The width of the I/O operation.
153 @param Count The number of times to write I/O port.
154 @param Buffer The buffer to store the read data into.
161 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
168 Status
= mCpuIo
->Io
.Write (mCpuIo
, Width
, Port
, Count
, Buffer
);
169 ASSERT_EFI_ERROR (Status
);
173 Reads memory-mapped registers in the EFI system memory space.
175 Reads the MMIO registers specified by Address with registers width specified by Width.
176 The read value is returned. If such operations are not supported, then ASSERT().
177 This function must guarantee that all MMIO read and write operations are serialized.
179 @param Address The MMIO register to read.
180 The caller is responsible for aligning the Address if required.
181 @param Width The width of the I/O operation.
183 @return Data read from registers in the EFI system memory space.
190 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
196 Status
= mCpuIo
->Mem
.Read (mCpuIo
, Width
, Address
, 1, &Data
);
197 ASSERT_EFI_ERROR (Status
);
203 Writes memory-mapped registers in the EFI system memory space.
205 Writes the MMIO registers specified by Address with registers width and value specified by Width
206 and Data respectively. Data is returned. If such operations are not supported, then ASSERT().
207 This function must guarantee that all MMIO read and write operations are serialized.
209 @param Address The MMIO register to read.
210 The caller is responsible for aligning the Address if required.
211 @param Width The width of the I/O operation.
212 @param Data The value to write to the I/O port.
214 @return Data read from registers in the EFI system memory space.
221 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
227 Status
= mCpuIo
->Mem
.Write (mCpuIo
, Width
, Address
, 1, &Data
);
228 ASSERT_EFI_ERROR (Status
);
234 Reads an 8-bit I/O port.
236 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
237 This function must guarantee that all I/O read and write operations are
240 If 8-bit I/O port operations are not supported, then ASSERT().
242 @param Port The I/O port to read.
244 @return The value read.
253 return (UINT8
)IoReadWorker (Port
, EfiCpuIoWidthUint8
);
257 Writes an 8-bit I/O port.
259 Writes the 8-bit I/O port specified by Port with the value specified by Value
260 and returns Value. This function must guarantee that all I/O read and write
261 operations are serialized.
263 If 8-bit I/O port operations are not supported, then ASSERT().
265 @param Port The I/O port to write.
266 @param Value The value to write to the I/O port.
268 @return The value written the I/O port.
278 return (UINT8
)IoWriteWorker (Port
, EfiCpuIoWidthUint8
, Value
);
282 Reads a 16-bit I/O port.
284 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
285 This function must guarantee that all I/O read and write operations are
288 If Port is not aligned on a 16-bit boundary, then ASSERT().
290 If 16-bit I/O port operations are not supported, then ASSERT().
292 @param Port The I/O port to read.
294 @return The value read.
304 // Make sure Port is aligned on a 16-bit boundary.
306 ASSERT ((Port
& 1) == 0);
307 return (UINT16
)IoReadWorker (Port
, EfiCpuIoWidthUint16
);
311 Writes a 16-bit I/O port.
313 Writes the 16-bit I/O port specified by Port with the value specified by Value
314 and returns Value. This function must guarantee that all I/O read and write
315 operations are serialized.
317 If Port is not aligned on a 16-bit boundary, then ASSERT().
319 If 16-bit I/O port operations are not supported, then ASSERT().
321 @param Port The I/O port to write.
322 @param Value The value to write to the I/O port.
324 @return The value written the I/O port.
335 // Make sure Port is aligned on a 16-bit boundary.
337 ASSERT ((Port
& 1) == 0);
338 return (UINT16
)IoWriteWorker (Port
, EfiCpuIoWidthUint16
, Value
);
342 Reads a 32-bit I/O port.
344 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
345 This function must guarantee that all I/O read and write operations are
348 If Port is not aligned on a 32-bit boundary, then ASSERT().
350 If 32-bit I/O port operations are not supported, then ASSERT().
352 @param Port The I/O port to read.
354 @return The value read.
364 // Make sure Port is aligned on a 32-bit boundary.
366 ASSERT ((Port
& 3) == 0);
367 return (UINT32
)IoReadWorker (Port
, EfiCpuIoWidthUint32
);
371 Writes a 32-bit I/O port.
373 Writes the 32-bit I/O port specified by Port with the value specified by Value
374 and returns Value. This function must guarantee that all I/O read and write
375 operations are serialized.
377 If Port is not aligned on a 32-bit boundary, then ASSERT().
379 If 32-bit I/O port operations are not supported, then ASSERT().
381 @param Port The I/O port to write.
382 @param Value The value to write to the I/O port.
384 @return The value written the I/O port.
395 // Make sure Port is aligned on a 32-bit boundary.
397 ASSERT ((Port
& 3) == 0);
398 return (UINT32
)IoWriteWorker (Port
, EfiCpuIoWidthUint32
, Value
);
402 Reads a 64-bit I/O port.
404 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
405 This function must guarantee that all I/O read and write operations are
408 If Port is not aligned on a 64-bit boundary, then ASSERT().
410 If 64-bit I/O port operations are not supported, then ASSERT().
412 @param Port The I/O port to read.
414 @return The value read.
424 // Make sure Port is aligned on a 64-bit boundary.
426 ASSERT ((Port
& 7) == 0);
427 return IoReadWorker (Port
, EfiCpuIoWidthUint64
);
431 Writes a 64-bit I/O port.
433 Writes the 64-bit I/O port specified by Port with the value specified by Value
434 and returns Value. This function must guarantee that all I/O read and write
435 operations are serialized.
437 If Port is not aligned on a 64-bit boundary, then ASSERT().
439 If 64-bit I/O port operations are not supported, then ASSERT().
441 @param Port The I/O port to write.
442 @param Value The value to write to the I/O port.
444 @return The value written the I/O port.
455 // Make sure Port is aligned on a 64-bit boundary.
457 ASSERT ((Port
& 7) == 0);
458 return IoWriteWorker (Port
, EfiCpuIoWidthUint64
, Value
);
462 Reads an 8-bit I/O port fifo into a block of memory.
464 Reads the 8-bit I/O fifo port specified by Port.
465 The port is read Count times, and the read data is
466 stored in the provided Buffer.
468 This function must guarantee that all I/O read and write operations are
471 If 8-bit I/O port operations are not supported, then ASSERT().
473 @param Port The I/O port to read.
474 @param Count The number of times to read I/O port.
475 @param Buffer The buffer to store the read data into.
486 IoReadFifoWorker (Port
, EfiCpuIoWidthFifoUint8
, Count
, Buffer
);
490 Writes a block of memory into an 8-bit I/O port fifo.
492 Writes the 8-bit I/O fifo port specified by Port.
493 The port is written Count times, and the write data is
494 retrieved from the provided Buffer.
496 This function must guarantee that all I/O write and write operations are
499 If 8-bit I/O port operations are not supported, then ASSERT().
501 @param Port The I/O port to write.
502 @param Count The number of times to write I/O port.
503 @param Buffer The buffer to retrieve the write data from.
514 IoWriteFifoWorker (Port
, EfiCpuIoWidthFifoUint8
, Count
, Buffer
);
518 Reads a 16-bit I/O port fifo into a block of memory.
520 Reads the 16-bit I/O fifo port specified by Port.
521 The port is read Count times, and the read data is
522 stored in the provided Buffer.
524 This function must guarantee that all I/O read and write operations are
527 If 16-bit I/O port operations are not supported, then ASSERT().
529 @param Port The I/O port to read.
530 @param Count The number of times to read I/O port.
531 @param Buffer The buffer to store the read data into.
543 // Make sure Port is aligned on a 16-bit boundary.
545 ASSERT ((Port
& 1) == 0);
546 IoReadFifoWorker (Port
, EfiCpuIoWidthFifoUint16
, Count
, Buffer
);
550 Writes a block of memory into a 16-bit I/O port fifo.
552 Writes the 16-bit I/O fifo port specified by Port.
553 The port is written Count times, and the write data is
554 retrieved from the provided Buffer.
556 This function must guarantee that all I/O write and write operations are
559 If 16-bit I/O port operations are not supported, then ASSERT().
561 @param Port The I/O port to write.
562 @param Count The number of times to write I/O port.
563 @param Buffer The buffer to retrieve the write data from.
575 // Make sure Port is aligned on a 16-bit boundary.
577 ASSERT ((Port
& 1) == 0);
578 IoWriteFifoWorker (Port
, EfiCpuIoWidthFifoUint16
, Count
, Buffer
);
582 Reads a 32-bit I/O port fifo into a block of memory.
584 Reads the 32-bit I/O fifo port specified by Port.
585 The port is read Count times, and the read data is
586 stored in the provided Buffer.
588 This function must guarantee that all I/O read and write operations are
591 If 32-bit I/O port operations are not supported, then ASSERT().
593 @param Port The I/O port to read.
594 @param Count The number of times to read I/O port.
595 @param Buffer The buffer to store the read data into.
607 // Make sure Port is aligned on a 32-bit boundary.
609 ASSERT ((Port
& 3) == 0);
610 IoReadFifoWorker (Port
, EfiCpuIoWidthFifoUint32
, Count
, Buffer
);
614 Writes a block of memory into a 32-bit I/O port fifo.
616 Writes the 32-bit I/O fifo port specified by Port.
617 The port is written Count times, and the write data is
618 retrieved from the provided Buffer.
620 This function must guarantee that all I/O write and write operations are
623 If 32-bit I/O port operations are not supported, then ASSERT().
625 @param Port The I/O port to write.
626 @param Count The number of times to write I/O port.
627 @param Buffer The buffer to retrieve the write data from.
639 // Make sure Port is aligned on a 32-bit boundary.
641 ASSERT ((Port
& 3) == 0);
642 IoWriteFifoWorker (Port
, EfiCpuIoWidthFifoUint32
, Count
, Buffer
);
646 Reads an 8-bit MMIO register.
648 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
649 returned. This function must guarantee that all MMIO read and write
650 operations are serialized.
652 If 8-bit MMIO register operations are not supported, then ASSERT().
654 @param Address The MMIO register to read.
656 @return The value read.
665 return (UINT8
)MmioReadWorker (Address
, EfiCpuIoWidthUint8
);
669 Writes an 8-bit MMIO register.
671 Writes the 8-bit MMIO register specified by Address with the value specified
672 by Value and returns Value. This function must guarantee that all MMIO read
673 and write operations are serialized.
675 If 8-bit MMIO register operations are not supported, then ASSERT().
677 @param Address The MMIO register to write.
678 @param Value The value to write to the MMIO register.
688 return (UINT8
)MmioWriteWorker (Address
, EfiCpuIoWidthUint8
, Value
);
692 Reads a 16-bit MMIO register.
694 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
695 returned. This function must guarantee that all MMIO read and write
696 operations are serialized.
698 If Address is not aligned on a 16-bit boundary, then ASSERT().
700 If 16-bit MMIO register operations are not supported, then ASSERT().
702 @param Address The MMIO register to read.
704 @return The value read.
714 // Make sure Address is aligned on a 16-bit boundary.
716 ASSERT ((Address
& 1) == 0);
717 return (UINT16
)MmioReadWorker (Address
, EfiCpuIoWidthUint16
);
721 Writes a 16-bit MMIO register.
723 Writes the 16-bit MMIO register specified by Address with the value specified
724 by Value and returns Value. This function must guarantee that all MMIO read
725 and write operations are serialized.
727 If Address is not aligned on a 16-bit boundary, then ASSERT().
729 If 16-bit MMIO register operations are not supported, then ASSERT().
731 @param Address The MMIO register to write.
732 @param Value The value to write to the MMIO register.
743 // Make sure Address is aligned on a 16-bit boundary.
745 ASSERT ((Address
& 1) == 0);
746 return (UINT16
)MmioWriteWorker (Address
, EfiCpuIoWidthUint16
, Value
);
750 Reads a 32-bit MMIO register.
752 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
753 returned. This function must guarantee that all MMIO read and write
754 operations are serialized.
756 If Address is not aligned on a 32-bit boundary, then ASSERT().
758 If 32-bit MMIO register operations are not supported, then ASSERT().
760 @param Address The MMIO register to read.
762 @return The value read.
772 // Make sure Address is aligned on a 32-bit boundary.
774 ASSERT ((Address
& 3) == 0);
775 return (UINT32
)MmioReadWorker (Address
, EfiCpuIoWidthUint32
);
779 Writes a 32-bit MMIO register.
781 Writes the 32-bit MMIO register specified by Address with the value specified
782 by Value and returns Value. This function must guarantee that all MMIO read
783 and write operations are serialized.
785 If Address is not aligned on a 32-bit boundary, then ASSERT().
787 If 32-bit MMIO register operations are not supported, then ASSERT().
789 @param Address The MMIO register to write.
790 @param Value The value to write to the MMIO register.
801 // Make sure Address is aligned on a 32-bit boundary.
803 ASSERT ((Address
& 3) == 0);
804 return (UINT32
)MmioWriteWorker (Address
, EfiCpuIoWidthUint32
, Value
);
808 Reads a 64-bit MMIO register.
810 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
811 returned. This function must guarantee that all MMIO read and write
812 operations are serialized.
814 If Address is not aligned on a 64-bit boundary, then ASSERT().
816 If 64-bit MMIO register operations are not supported, then ASSERT().
818 @param Address The MMIO register to read.
820 @return The value read.
830 // Make sure Address is aligned on a 64-bit boundary.
832 ASSERT ((Address
& 7) == 0);
833 return (UINT64
)MmioReadWorker (Address
, EfiCpuIoWidthUint64
);
837 Writes a 64-bit MMIO register.
839 Writes the 64-bit MMIO register specified by Address with the value specified
840 by Value and returns Value. This function must guarantee that all MMIO read
841 and write operations are serialized.
843 If Address is not aligned on a 64-bit boundary, then ASSERT().
845 If 64-bit MMIO register operations are not supported, then ASSERT().
847 @param Address The MMIO register to write.
848 @param Value The value to write to the MMIO register.
859 // Make sure Address is aligned on a 64-bit boundary.
861 ASSERT ((Address
& 7) == 0);
862 return (UINT64
)MmioWriteWorker (Address
, EfiCpuIoWidthUint64
, Value
);