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.
17 #include "DxeCpuIoLibInternal.h"
20 // Globle varible to cache pointer to CpuIo protocol.
22 STATIC EFI_CPU_IO_PROTOCOL
*mCpuIo
= NULL
;
23 STATIC EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*mPciRootBridgeIo
= NULL
;
26 The constructor function caches the pointer to CpuIo protocol.
28 The constructor function locates CpuIo protocol from protocol database.
29 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
31 @param ImageHandle The firmware allocated handle for the EFI image.
32 @param SystemTable A pointer to the EFI System Table.
34 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
40 IN EFI_HANDLE ImageHandle
,
41 IN EFI_SYSTEM_TABLE
*SystemTable
46 Status
= gBS
->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid
, NULL
, &mPciRootBridgeIo
);
47 if (EFI_ERROR (Status
)) {
48 Status
= gBS
->LocateProtocol (&gEfiCpuIoProtocolGuid
, NULL
, &mCpuIo
);
50 ASSERT_EFI_ERROR (Status
);
56 Reads registers in the EFI CPU I/O space.
58 Reads the I/O port specified by Port with registers width specified by Width.
59 The read value is returned. If such operations are not supported, then ASSERT().
60 This function must guarantee that all I/O read and write operations are serialized.
62 @param Port The base address of the I/O operation.
63 The caller is responsible for aligning the Address if required.
64 @param Width The width of the I/O operation.
66 @return Data read from registers in the EFI CPU I/O space.
73 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
79 if (mPciRootBridgeIo
!= NULL
) {
80 Status
= mPciRootBridgeIo
->Io
.Read (mPciRootBridgeIo
, Width
, Port
, 1, &Data
);
82 Status
= mCpuIo
->Io
.Read (mCpuIo
, Width
, Port
, 1, &Data
);
84 ASSERT_EFI_ERROR (Status
);
90 Writes registers in the EFI CPU I/O space.
92 Writes the I/O port specified by Port with registers width and value specified by Width
93 and Data respectively. Data is returned. If such operations are not supported, then ASSERT().
94 This function must guarantee that all I/O read and write operations are serialized.
96 @param Port The base address of the I/O operation.
97 The caller is responsible for aligning the Address if required.
98 @param Width The width of the I/O operation.
99 @param Data The value to write to the I/O port.
101 @return The paramter of Data.
108 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
114 if (mPciRootBridgeIo
!= NULL
) {
115 Status
= mPciRootBridgeIo
->Io
.Write (mPciRootBridgeIo
, Width
, Port
, 1, &Data
);
117 Status
= mCpuIo
->Io
.Write (mCpuIo
, Width
, Port
, 1, &Data
);
119 ASSERT_EFI_ERROR (Status
);
125 Reads memory-mapped registers in the EFI system memory space.
127 Reads the MMIO registers specified by Address with registers width specified by Width.
128 The read value is returned. If such operations are not supported, then ASSERT().
129 This function must guarantee that all MMIO read and write operations are serialized.
131 @param Address The MMIO register to read.
132 The caller is responsible for aligning the Address if required.
133 @param Width The width of the I/O operation.
135 @return Data read from registers in the EFI system memory space.
142 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
148 if (mPciRootBridgeIo
!= NULL
) {
149 Status
= mPciRootBridgeIo
->Mem
.Read (mPciRootBridgeIo
, Width
, Address
, 1, &Data
);
151 Status
= mCpuIo
->Mem
.Read (mCpuIo
, Width
, Address
, 1, &Data
);
153 ASSERT_EFI_ERROR (Status
);
159 Writes memory-mapped registers in the EFI system memory space.
161 Writes the MMIO registers specified by Address with registers width and value specified by Width
162 and Data respectively. Data is returned. If such operations are not supported, then ASSERT().
163 This function must guarantee that all MMIO read and write operations are serialized.
165 @param Address The MMIO register to read.
166 The caller is responsible for aligning the Address if required.
167 @param Width The width of the I/O operation.
169 @return Data read from registers in the EFI system memory space.
176 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
,
182 if (mPciRootBridgeIo
!= NULL
) {
183 Status
= mPciRootBridgeIo
->Mem
.Write (mPciRootBridgeIo
, Width
, Address
, 1, &Data
);
185 Status
= mCpuIo
->Mem
.Write (mCpuIo
, Width
, Address
, 1, &Data
);
187 ASSERT_EFI_ERROR (Status
);
193 Reads an 8-bit I/O port.
195 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
196 This function must guarantee that all I/O read and write operations are
199 If 8-bit I/O port operations are not supported, then ASSERT().
201 @param Port The I/O port to read.
203 @return The value read.
212 return (UINT8
)IoReadWorker (Port
, EfiCpuIoWidthUint8
);
216 Writes an 8-bit I/O port.
218 Writes the 8-bit I/O port specified by Port with the value specified by Value
219 and returns Value. This function must guarantee that all I/O read and write
220 operations are serialized.
222 If 8-bit I/O port operations are not supported, then ASSERT().
224 @param Port The I/O port to write.
225 @param Value The value to write to the I/O port.
227 @return The value written the I/O port.
237 return (UINT8
)IoWriteWorker (Port
, EfiCpuIoWidthUint8
, Value
);
241 Reads a 16-bit I/O port.
243 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
244 This function must guarantee that all I/O read and write operations are
247 If 16-bit I/O port operations are not supported, then ASSERT().
249 @param Port The I/O port to read.
251 @return The value read.
261 // Make sure Port is aligned on a 16-bit boundary.
263 ASSERT ((Port
& 1) == 0);
264 return (UINT16
)IoReadWorker (Port
, EfiCpuIoWidthUint16
);
268 Writes a 16-bit I/O port.
270 Writes the 16-bit I/O port specified by Port with the value specified by Value
271 and returns Value. This function must guarantee that all I/O read and write
272 operations are serialized.
274 If 16-bit I/O port operations are not supported, then ASSERT().
276 @param Port The I/O port to write.
277 @param Value The value to write to the I/O port.
279 @return The value written the I/O port.
290 // Make sure Port is aligned on a 16-bit boundary.
292 ASSERT ((Port
& 1) == 0);
293 return (UINT16
)IoWriteWorker (Port
, EfiCpuIoWidthUint16
, Value
);
297 Reads a 32-bit I/O port.
299 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
300 This function must guarantee that all I/O read and write operations are
303 If 32-bit I/O port operations are not supported, then ASSERT().
305 @param Port The I/O port to read.
307 @return The value read.
317 // Make sure Port is aligned on a 32-bit boundary.
319 ASSERT ((Port
& 3) == 0);
320 return (UINT32
)IoReadWorker (Port
, EfiCpuIoWidthUint32
);
324 Writes a 32-bit I/O port.
326 Writes the 32-bit I/O port specified by Port with the value specified by Value
327 and returns Value. This function must guarantee that all I/O read and write
328 operations are serialized.
330 If 32-bit I/O port operations are not supported, then ASSERT().
332 @param Port The I/O port to write.
333 @param Value The value to write to the I/O port.
335 @return The value written the I/O port.
346 // Make sure Port is aligned on a 32-bit boundary.
348 ASSERT ((Port
& 3) == 0);
349 return (UINT32
)IoWriteWorker (Port
, EfiCpuIoWidthUint32
, Value
);
353 Reads a 64-bit I/O port.
355 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
356 This function must guarantee that all I/O read and write operations are
359 If 64-bit I/O port operations are not supported, then ASSERT().
361 @param Port The I/O port to read.
363 @return The value read.
373 // Make sure Port is aligned on a 64-bit boundary.
375 ASSERT ((Port
& 7) == 0);
376 return IoReadWorker (Port
, EfiCpuIoWidthUint64
);
380 Writes a 64-bit I/O port.
382 Writes the 64-bit I/O port specified by Port with the value specified by Value
383 and returns Value. This function must guarantee that all I/O read and write
384 operations are serialized.
386 If 64-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 64-bit boundary.
404 ASSERT ((Port
& 7) == 0);
405 return IoWriteWorker (Port
, EfiCpuIoWidthUint64
, Value
);
409 Reads an 8-bit MMIO register.
411 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
412 returned. This function must guarantee that all MMIO read and write
413 operations are serialized.
415 If 8-bit MMIO register operations are not supported, then ASSERT().
417 @param Address The MMIO register to read.
419 @return The value read.
428 return (UINT8
)MmioReadWorker (Address
, EfiCpuIoWidthUint8
);
432 Writes an 8-bit MMIO register.
434 Writes the 8-bit MMIO register specified by Address with the value specified
435 by Value and returns Value. This function must guarantee that all MMIO read
436 and write operations are serialized.
438 If 8-bit MMIO register operations are not supported, then ASSERT().
440 @param Address The MMIO register to write.
441 @param Value The value to write to the MMIO register.
451 return (UINT8
)MmioWriteWorker (Address
, EfiCpuIoWidthUint8
, Value
);
455 Reads a 16-bit MMIO register.
457 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
458 returned. This function must guarantee that all MMIO read and write
459 operations are serialized.
461 If 16-bit MMIO register operations are not supported, then ASSERT().
463 @param Address The MMIO register to read.
465 @return The value read.
475 // Make sure Address is aligned on a 16-bit boundary.
477 ASSERT ((Address
& 1) == 0);
478 return (UINT16
)MmioReadWorker (Address
, EfiCpuIoWidthUint16
);
482 Writes a 16-bit MMIO register.
484 Writes the 16-bit MMIO register specified by Address with the value specified
485 by Value and returns Value. This function must guarantee that all MMIO read
486 and write operations are serialized.
488 If 16-bit MMIO register operations are not supported, then ASSERT().
490 @param Address The MMIO register to write.
491 @param Value The value to write to the MMIO register.
502 // Make sure Address is aligned on a 16-bit boundary.
504 ASSERT ((Address
& 1) == 0);
505 return (UINT16
)MmioWriteWorker (Address
, EfiCpuIoWidthUint16
, Value
);
509 Reads a 32-bit MMIO register.
511 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
512 returned. This function must guarantee that all MMIO read and write
513 operations are serialized.
515 If 32-bit MMIO register operations are not supported, then ASSERT().
517 @param Address The MMIO register to read.
519 @return The value read.
529 // Make sure Address is aligned on a 32-bit boundary.
531 ASSERT ((Address
& 3) == 0);
532 return (UINT32
)MmioReadWorker (Address
, EfiCpuIoWidthUint32
);
536 Writes a 32-bit MMIO register.
538 Writes the 32-bit MMIO register specified by Address with the value specified
539 by Value and returns Value. This function must guarantee that all MMIO read
540 and write operations are serialized.
542 If 32-bit MMIO register operations are not supported, then ASSERT().
544 @param Address The MMIO register to write.
545 @param Value The value to write to the MMIO register.
556 // Make sure Address is aligned on a 32-bit boundary.
558 ASSERT ((Address
& 3) == 0);
559 return (UINT32
)MmioWriteWorker (Address
, EfiCpuIoWidthUint32
, Value
);
563 Reads a 64-bit MMIO register.
565 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
566 returned. This function must guarantee that all MMIO read and write
567 operations are serialized.
569 If 64-bit MMIO register operations are not supported, then ASSERT().
571 @param Address The MMIO register to read.
573 @return The value read.
583 // Make sure Address is aligned on a 64-bit boundary.
585 ASSERT ((Address
& 7) == 0);
586 return (UINT64
)MmioReadWorker (Address
, EfiCpuIoWidthUint64
);
590 Writes a 64-bit MMIO register.
592 Writes the 64-bit MMIO register specified by Address with the value specified
593 by Value and returns Value. This function must guarantee that all MMIO read
594 and write operations are serialized.
596 If 64-bit MMIO register operations are not supported, then ASSERT().
598 @param Address The MMIO register to write.
599 @param Value The value to write to the MMIO register.
610 // Make sure Address is aligned on a 64-bit boundary.
612 ASSERT ((Address
& 7) == 0);
613 return (UINT64
)MmioWriteWorker (Address
, EfiCpuIoWidthUint64
, Value
);