]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/UefiPciLibPciRootBridgeIo/PciLib.c
Add runtime registration function to all PCI Libs
[mirror_edk2.git] / MdePkg / Library / UefiPciLibPciRootBridgeIo / PciLib.c
1 /** @file
2 PCI Library using PCI Root Bridge I/O Protocol.
3
4 Copyright (c) 2007 - 2008, Intel Corporation All rights
5 reserved. This program and the accompanying materials are
6 licensed and made available under the terms and conditions of
7 the BSD License which accompanies this distribution. The full
8 text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include <PiDxe.h>
17
18 #include <Protocol/PciRootBridgeIo.h>
19
20 #include <Library/PciLib.h>
21 #include <Library/BaseLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
23 #include <Library/DebugLib.h>
24
25 /**
26 Assert the validity of a PCI address. A valid PCI address should contain 1's
27 only in the low 28 bits.
28
29 @param A The address to validate.
30 @param M Additional bits to assert to be zero.
31
32 **/
33 #define ASSERT_INVALID_PCI_ADDRESS(A,M) \
34 ASSERT (((A) & (~0xfffffff | (M))) == 0)
35
36 /**
37 Translate PCI Lib address into format of PCI Root Bridge I/O Protocol.
38
39 @param A Address that encodes the PCI Bus, Device, Function and
40 Register.
41
42 **/
43 #define PCI_TO_PCI_ROOT_BRIDGE_IO_ADDRESS(A) \
44 ((((A) << 4) & 0xff000000) | (((A) >> 4) & 0x00000700) | (((A) << 1) & 0x001f0000) | (LShiftU64((A) & 0xfff, 32)))
45
46 //
47 // Global varible to cache pointer to PCI Root Bridge I/O protocol.
48 //
49 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *mPciRootBridgeIo = NULL;
50
51 /**
52 The constructor function caches the pointer to PCI Root Bridge I/O protocol.
53
54 The constructor function locates PCI Root Bridge I/O protocol from protocol database.
55 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
56
57 @param ImageHandle The firmware allocated handle for the EFI image.
58 @param SystemTable A pointer to the EFI System Table.
59
60 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
61
62 **/
63 EFI_STATUS
64 EFIAPI
65 PciLibConstructor (
66 IN EFI_HANDLE ImageHandle,
67 IN EFI_SYSTEM_TABLE *SystemTable
68 )
69 {
70 EFI_STATUS Status;
71
72 Status = gBS->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid, NULL, (VOID**) &mPciRootBridgeIo);
73 ASSERT_EFI_ERROR (Status);
74 ASSERT (mPciRootBridgeIo != NULL);
75
76 return EFI_SUCCESS;
77 }
78
79 /**
80 Internal worker function to read a PCI configuration register.
81
82 This function wraps EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.Pci.Read() service.
83 It reads and returns the PCI configuration register specified by Address,
84 the width of data is specified by Width.
85
86 @param Address Address that encodes the PCI Bus, Device, Function and
87 Register.
88 @param Width Width of data to read
89
90 @return The value read from the PCI configuration register.
91
92 **/
93 UINT32
94 DxePciLibPciRootBridgeIoReadWorker (
95 IN UINTN Address,
96 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
97 )
98 {
99 UINT32 Data;
100
101 mPciRootBridgeIo->Pci.Read (
102 mPciRootBridgeIo,
103 Width,
104 PCI_TO_PCI_ROOT_BRIDGE_IO_ADDRESS (Address),
105 1,
106 &Data
107 );
108
109 return Data;
110 }
111
112 /**
113 Internal worker function to writes a PCI configuration register.
114
115 This function wraps EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.Pci.Write() service.
116 It writes the PCI configuration register specified by Address with the
117 value specified by Data. The width of data is specifed by Width.
118 Data is returned.
119
120 @param Address Address that encodes the PCI Bus, Device, Function and
121 Register.
122 @param Width Width of data to write
123 @param Data The value to write.
124
125 @return The value written to the PCI configuration register.
126
127 **/
128 UINT32
129 DxePciLibPciRootBridgeIoWriteWorker (
130 IN UINTN Address,
131 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
132 IN UINT32 Data
133 )
134 {
135 mPciRootBridgeIo->Pci.Write (
136 mPciRootBridgeIo,
137 Width,
138 PCI_TO_PCI_ROOT_BRIDGE_IO_ADDRESS (Address),
139 1,
140 &Data
141 );
142 return Data;
143 }
144
145 /**
146 Register a PCI device so PCI configuration registers may be accessed after
147 SetVirtualAddressMap().
148
149 If Address > 0x0FFFFFFF, then ASSERT().
150
151 @param Address Address that encodes the PCI Bus, Device, Function and
152 Register.
153
154 @retval RETURN_SUCCESS The PCI device was registered for runtime access.
155 @retval RETURN_UNSUPPORTED An attempt was made to call this function
156 after ExitBootServices().
157 @retval RETURN_UNSUPPORTED The resources required to access the PCI device
158 at runtime could not be mapped.
159 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
160 complete the registration.
161
162 **/
163 RETURN_STATUS
164 EFIAPI
165 PciRegisterForRuntimeAccess (
166 IN UINTN Address
167 )
168 {
169 return RETURN_UNSUPPORTED;
170 }
171
172 /**
173 Reads an 8-bit PCI configuration register.
174
175 Reads and returns the 8-bit PCI configuration register specified by Address.
176 This function must guarantee that all PCI read and write operations are
177 serialized.
178
179 If Address > 0x0FFFFFFF, then ASSERT().
180
181 @param Address Address that encodes the PCI Bus, Device, Function and
182 Register.
183
184 @return The value read from the PCI configuration register.
185
186 **/
187 UINT8
188 EFIAPI
189 PciRead8 (
190 IN UINTN Address
191 )
192 {
193 ASSERT_INVALID_PCI_ADDRESS (Address, 0);
194
195 return (UINT8) DxePciLibPciRootBridgeIoReadWorker (Address, EfiPciWidthUint8);
196 }
197
198 /**
199 Writes an 8-bit PCI configuration register.
200
201 Writes the 8-bit PCI configuration register specified by Address with the
202 value specified by Value. Value is returned. This function must guarantee
203 that all PCI read and write operations are serialized.
204
205 If Address > 0x0FFFFFFF, then ASSERT().
206
207 @param Address Address that encodes the PCI Bus, Device, Function and
208 Register.
209 @param Data The value to write.
210
211 @return The value written to the PCI configuration register.
212
213 **/
214 UINT8
215 EFIAPI
216 PciWrite8 (
217 IN UINTN Address,
218 IN UINT8 Data
219 )
220 {
221 ASSERT_INVALID_PCI_ADDRESS (Address, 0);
222
223 return (UINT8) DxePciLibPciRootBridgeIoWriteWorker (Address, EfiPciWidthUint8, Data);
224 }
225
226 /**
227 Performs a bitwise inclusive OR of an 8-bit PCI configuration register with
228 an 8-bit value.
229
230 Reads the 8-bit PCI configuration register specified by Address, performs a
231 bitwise inclusive OR between the read result and the value specified by
232 OrData, and writes the result to the 8-bit PCI configuration register
233 specified by Address. The value written to the PCI configuration register is
234 returned. This function must guarantee that all PCI read and write operations
235 are serialized.
236
237 If Address > 0x0FFFFFFF, then ASSERT().
238
239 @param Address Address that encodes the PCI Bus, Device, Function and
240 Register.
241 @param OrData The value to OR with the PCI configuration register.
242
243 @return The value written back to the PCI configuration register.
244
245 **/
246 UINT8
247 EFIAPI
248 PciOr8 (
249 IN UINTN Address,
250 IN UINT8 OrData
251 )
252 {
253 return PciWrite8 (Address, (UINT8) (PciRead8 (Address) | OrData));
254 }
255
256 /**
257 Performs a bitwise AND of an 8-bit PCI configuration register with an 8-bit
258 value.
259
260 Reads the 8-bit PCI configuration register specified by Address, performs a
261 bitwise AND between the read result and the value specified by AndData, and
262 writes the result to the 8-bit PCI configuration register specified by
263 Address. The value written to the PCI configuration register is returned.
264 This function must guarantee that all PCI read and write operations are
265 serialized.
266
267 If Address > 0x0FFFFFFF, then ASSERT().
268
269 @param Address Address that encodes the PCI Bus, Device, Function and
270 Register.
271 @param AndData The value to AND with the PCI configuration register.
272
273 @return The value written back to the PCI configuration register.
274
275 **/
276 UINT8
277 EFIAPI
278 PciAnd8 (
279 IN UINTN Address,
280 IN UINT8 AndData
281 )
282 {
283 return PciWrite8 (Address, (UINT8) (PciRead8 (Address) & AndData));
284 }
285
286 /**
287 Performs a bitwise AND of an 8-bit PCI configuration register with an 8-bit
288 value, followed a bitwise inclusive OR with another 8-bit value.
289
290 Reads the 8-bit PCI configuration register specified by Address, performs a
291 bitwise AND between the read result and the value specified by AndData,
292 performs a bitwise inclusive OR between the result of the AND operation and
293 the value specified by OrData, and writes the result to the 8-bit PCI
294 configuration register specified by Address. The value written to the PCI
295 configuration register is returned. This function must guarantee that all PCI
296 read and write operations are serialized.
297
298 If Address > 0x0FFFFFFF, then ASSERT().
299
300 @param Address Address that encodes the PCI Bus, Device, Function and
301 Register.
302 @param AndData The value to AND with the PCI configuration register.
303 @param OrData The value to OR with the result of the AND operation.
304
305 @return The value written back to the PCI configuration register.
306
307 **/
308 UINT8
309 EFIAPI
310 PciAndThenOr8 (
311 IN UINTN Address,
312 IN UINT8 AndData,
313 IN UINT8 OrData
314 )
315 {
316 return PciWrite8 (Address, (UINT8) ((PciRead8 (Address) & AndData) | OrData));
317 }
318
319 /**
320 Reads a bit field of a PCI configuration register.
321
322 Reads the bit field in an 8-bit PCI configuration register. The bit field is
323 specified by the StartBit and the EndBit. The value of the bit field is
324 returned.
325
326 If Address > 0x0FFFFFFF, then ASSERT().
327 If StartBit is greater than 7, then ASSERT().
328 If EndBit is greater than 7, then ASSERT().
329 If EndBit is less than StartBit, then ASSERT().
330
331 @param Address PCI configuration register to read.
332 @param StartBit The ordinal of the least significant bit in the bit field.
333 Range 0..7.
334 @param EndBit The ordinal of the most significant bit in the bit field.
335 Range 0..7.
336
337 @return The value of the bit field read from the PCI configuration register.
338
339 **/
340 UINT8
341 EFIAPI
342 PciBitFieldRead8 (
343 IN UINTN Address,
344 IN UINTN StartBit,
345 IN UINTN EndBit
346 )
347 {
348 return BitFieldRead8 (PciRead8 (Address), StartBit, EndBit);
349 }
350
351 /**
352 Writes a bit field to a PCI configuration register.
353
354 Writes Value to the bit field of the PCI configuration register. The bit
355 field is specified by the StartBit and the EndBit. All other bits in the
356 destination PCI configuration register are preserved. The new value of the
357 8-bit register is returned.
358
359 If Address > 0x0FFFFFFF, then ASSERT().
360 If StartBit is greater than 7, then ASSERT().
361 If EndBit is greater than 7, then ASSERT().
362 If EndBit is less than StartBit, then ASSERT().
363
364 @param Address PCI configuration register to write.
365 @param StartBit The ordinal of the least significant bit in the bit field.
366 Range 0..7.
367 @param EndBit The ordinal of the most significant bit in the bit field.
368 Range 0..7.
369 @param Value New value of the bit field.
370
371 @return The value written back to the PCI configuration register.
372
373 **/
374 UINT8
375 EFIAPI
376 PciBitFieldWrite8 (
377 IN UINTN Address,
378 IN UINTN StartBit,
379 IN UINTN EndBit,
380 IN UINT8 Value
381 )
382 {
383 return PciWrite8 (
384 Address,
385 BitFieldWrite8 (PciRead8 (Address), StartBit, EndBit, Value)
386 );
387 }
388
389 /**
390 Reads a bit field in an 8-bit PCI configuration, performs a bitwise OR, and
391 writes the result back to the bit field in the 8-bit port.
392
393 Reads the 8-bit PCI configuration register specified by Address, performs a
394 bitwise inclusive OR between the read result and the value specified by
395 OrData, and writes the result to the 8-bit PCI configuration register
396 specified by Address. The value written to the PCI configuration register is
397 returned. This function must guarantee that all PCI read and write operations
398 are serialized. Extra left bits in OrData are stripped.
399
400 If Address > 0x0FFFFFFF, then ASSERT().
401 If StartBit is greater than 7, then ASSERT().
402 If EndBit is greater than 7, then ASSERT().
403 If EndBit is less than StartBit, then ASSERT().
404
405 @param Address PCI configuration register to write.
406 @param StartBit The ordinal of the least significant bit in the bit field.
407 Range 0..7.
408 @param EndBit The ordinal of the most significant bit in the bit field.
409 Range 0..7.
410 @param OrData The value to OR with the PCI configuration register.
411
412 @return The value written back to the PCI configuration register.
413
414 **/
415 UINT8
416 EFIAPI
417 PciBitFieldOr8 (
418 IN UINTN Address,
419 IN UINTN StartBit,
420 IN UINTN EndBit,
421 IN UINT8 OrData
422 )
423 {
424 return PciWrite8 (
425 Address,
426 BitFieldOr8 (PciRead8 (Address), StartBit, EndBit, OrData)
427 );
428 }
429
430 /**
431 Reads a bit field in an 8-bit PCI configuration register, performs a bitwise
432 AND, and writes the result back to the bit field in the 8-bit register.
433
434 Reads the 8-bit PCI configuration register specified by Address, performs a
435 bitwise AND between the read result and the value specified by AndData, and
436 writes the result to the 8-bit PCI configuration register specified by
437 Address. The value written to the PCI configuration register is returned.
438 This function must guarantee that all PCI read and write operations are
439 serialized. Extra left bits in AndData are stripped.
440
441 If Address > 0x0FFFFFFF, then ASSERT().
442 If StartBit is greater than 7, then ASSERT().
443 If EndBit is greater than 7, then ASSERT().
444 If EndBit is less than StartBit, then ASSERT().
445
446 @param Address PCI configuration register to write.
447 @param StartBit The ordinal of the least significant bit in the bit field.
448 Range 0..7.
449 @param EndBit The ordinal of the most significant bit in the bit field.
450 Range 0..7.
451 @param AndData The value to AND with the PCI configuration register.
452
453 @return The value written back to the PCI configuration register.
454
455 **/
456 UINT8
457 EFIAPI
458 PciBitFieldAnd8 (
459 IN UINTN Address,
460 IN UINTN StartBit,
461 IN UINTN EndBit,
462 IN UINT8 AndData
463 )
464 {
465 return PciWrite8 (
466 Address,
467 BitFieldAnd8 (PciRead8 (Address), StartBit, EndBit, AndData)
468 );
469 }
470
471 /**
472 Reads a bit field in an 8-bit port, performs a bitwise AND followed by a
473 bitwise inclusive OR, and writes the result back to the bit field in the
474 8-bit port.
475
476 Reads the 8-bit PCI configuration register specified by Address, performs a
477 bitwise AND followed by a bitwise inclusive OR between the read result and
478 the value specified by AndData, and writes the result to the 8-bit PCI
479 configuration register specified by Address. The value written to the PCI
480 configuration register is returned. This function must guarantee that all PCI
481 read and write operations are serialized. Extra left bits in both AndData and
482 OrData are stripped.
483
484 If Address > 0x0FFFFFFF, then ASSERT().
485 If StartBit is greater than 7, then ASSERT().
486 If EndBit is greater than 7, then ASSERT().
487 If EndBit is less than StartBit, then ASSERT().
488
489 @param Address PCI configuration register to write.
490 @param StartBit The ordinal of the least significant bit in the bit field.
491 Range 0..7.
492 @param EndBit The ordinal of the most significant bit in the bit field.
493 Range 0..7.
494 @param AndData The value to AND with the PCI configuration register.
495 @param OrData The value to OR with the result of the AND operation.
496
497 @return The value written back to the PCI configuration register.
498
499 **/
500 UINT8
501 EFIAPI
502 PciBitFieldAndThenOr8 (
503 IN UINTN Address,
504 IN UINTN StartBit,
505 IN UINTN EndBit,
506 IN UINT8 AndData,
507 IN UINT8 OrData
508 )
509 {
510 return PciWrite8 (
511 Address,
512 BitFieldAndThenOr8 (PciRead8 (Address), StartBit, EndBit, AndData, OrData)
513 );
514 }
515
516 /**
517 Reads a 16-bit PCI configuration register.
518
519 Reads and returns the 16-bit PCI configuration register specified by Address.
520 This function must guarantee that all PCI read and write operations are
521 serialized.
522
523 If Address > 0x0FFFFFFF, then ASSERT().
524 If Address is not aligned on a 16-bit boundary, then ASSERT().
525
526 @param Address Address that encodes the PCI Bus, Device, Function and
527 Register.
528
529 @return The value read from the PCI configuration register.
530
531 **/
532 UINT16
533 EFIAPI
534 PciRead16 (
535 IN UINTN Address
536 )
537 {
538 ASSERT_INVALID_PCI_ADDRESS (Address, 1);
539
540 return (UINT16) DxePciLibPciRootBridgeIoReadWorker (Address, EfiPciWidthUint16);
541 }
542
543 /**
544 Writes a 16-bit PCI configuration register.
545
546 Writes the 16-bit PCI configuration register specified by Address with the
547 value specified by Value. Value is returned. This function must guarantee
548 that all PCI read and write operations are serialized.
549
550 If Address > 0x0FFFFFFF, then ASSERT().
551 If Address is not aligned on a 16-bit boundary, then ASSERT().
552
553 @param Address Address that encodes the PCI Bus, Device, Function and
554 Register.
555 @param Data The value to write.
556
557 @return The value written to the PCI configuration register.
558
559 **/
560 UINT16
561 EFIAPI
562 PciWrite16 (
563 IN UINTN Address,
564 IN UINT16 Data
565 )
566 {
567 ASSERT_INVALID_PCI_ADDRESS (Address, 1);
568
569 return (UINT16) DxePciLibPciRootBridgeIoWriteWorker (Address, EfiPciWidthUint16, Data);
570 }
571
572 /**
573 Performs a bitwise inclusive OR of a 16-bit PCI configuration register with
574 a 16-bit value.
575
576 Reads the 16-bit PCI configuration register specified by Address, performs a
577 bitwise inclusive OR between the read result and the value specified by
578 OrData, and writes the result to the 16-bit PCI configuration register
579 specified by Address. The value written to the PCI configuration register is
580 returned. This function must guarantee that all PCI read and write operations
581 are serialized.
582
583 If Address > 0x0FFFFFFF, then ASSERT().
584 If Address is not aligned on a 16-bit boundary, then ASSERT().
585
586 @param Address Address that encodes the PCI Bus, Device, Function and
587 Register.
588 @param OrData The value to OR with the PCI configuration register.
589
590 @return The value written back to the PCI configuration register.
591
592 **/
593 UINT16
594 EFIAPI
595 PciOr16 (
596 IN UINTN Address,
597 IN UINT16 OrData
598 )
599 {
600 return PciWrite16 (Address, (UINT16) (PciRead16 (Address) | OrData));
601 }
602
603 /**
604 Performs a bitwise AND of a 16-bit PCI configuration register with a 16-bit
605 value.
606
607 Reads the 16-bit PCI configuration register specified by Address, performs a
608 bitwise AND between the read result and the value specified by AndData, and
609 writes the result to the 16-bit PCI configuration register specified by
610 Address. The value written to the PCI configuration register is returned.
611 This function must guarantee that all PCI read and write operations are
612 serialized.
613
614 If Address > 0x0FFFFFFF, then ASSERT().
615 If Address is not aligned on a 16-bit boundary, then ASSERT().
616
617 @param Address Address that encodes the PCI Bus, Device, Function and
618 Register.
619 @param AndData The value to AND with the PCI configuration register.
620
621 @return The value written back to the PCI configuration register.
622
623 **/
624 UINT16
625 EFIAPI
626 PciAnd16 (
627 IN UINTN Address,
628 IN UINT16 AndData
629 )
630 {
631 return PciWrite16 (Address, (UINT16) (PciRead16 (Address) & AndData));
632 }
633
634 /**
635 Performs a bitwise AND of a 16-bit PCI configuration register with a 16-bit
636 value, followed a bitwise inclusive OR with another 16-bit value.
637
638 Reads the 16-bit PCI configuration register specified by Address, performs a
639 bitwise AND between the read result and the value specified by AndData,
640 performs a bitwise inclusive OR between the result of the AND operation and
641 the value specified by OrData, and writes the result to the 16-bit PCI
642 configuration register specified by Address. The value written to the PCI
643 configuration register is returned. This function must guarantee that all PCI
644 read and write operations are serialized.
645
646 If Address > 0x0FFFFFFF, then ASSERT().
647 If Address is not aligned on a 16-bit boundary, then ASSERT().
648
649 @param Address Address that encodes the PCI Bus, Device, Function and
650 Register.
651 @param AndData The value to AND with the PCI configuration register.
652 @param OrData The value to OR with the result of the AND operation.
653
654 @return The value written back to the PCI configuration register.
655
656 **/
657 UINT16
658 EFIAPI
659 PciAndThenOr16 (
660 IN UINTN Address,
661 IN UINT16 AndData,
662 IN UINT16 OrData
663 )
664 {
665 return PciWrite16 (Address, (UINT16) ((PciRead16 (Address) & AndData) | OrData));
666 }
667
668 /**
669 Reads a bit field of a PCI configuration register.
670
671 Reads the bit field in a 16-bit PCI configuration register. The bit field is
672 specified by the StartBit and the EndBit. The value of the bit field is
673 returned.
674
675 If Address > 0x0FFFFFFF, then ASSERT().
676 If Address is not aligned on a 16-bit boundary, then ASSERT().
677 If StartBit is greater than 15, then ASSERT().
678 If EndBit is greater than 15, then ASSERT().
679 If EndBit is less than StartBit, then ASSERT().
680
681 @param Address PCI configuration register to read.
682 @param StartBit The ordinal of the least significant bit in the bit field.
683 Range 0..15.
684 @param EndBit The ordinal of the most significant bit in the bit field.
685 Range 0..15.
686
687 @return The value of the bit field read from the PCI configuration register.
688
689 **/
690 UINT16
691 EFIAPI
692 PciBitFieldRead16 (
693 IN UINTN Address,
694 IN UINTN StartBit,
695 IN UINTN EndBit
696 )
697 {
698 return BitFieldRead16 (PciRead16 (Address), StartBit, EndBit);
699 }
700
701 /**
702 Writes a bit field to a PCI configuration register.
703
704 Writes Value to the bit field of the PCI configuration register. The bit
705 field is specified by the StartBit and the EndBit. All other bits in the
706 destination PCI configuration register are preserved. The new value of the
707 16-bit register is returned.
708
709 If Address > 0x0FFFFFFF, then ASSERT().
710 If Address is not aligned on a 16-bit boundary, then ASSERT().
711 If StartBit is greater than 15, then ASSERT().
712 If EndBit is greater than 15, then ASSERT().
713 If EndBit is less than StartBit, then ASSERT().
714
715 @param Address PCI configuration register to write.
716 @param StartBit The ordinal of the least significant bit in the bit field.
717 Range 0..15.
718 @param EndBit The ordinal of the most significant bit in the bit field.
719 Range 0..15.
720 @param Value New value of the bit field.
721
722 @return The value written back to the PCI configuration register.
723
724 **/
725 UINT16
726 EFIAPI
727 PciBitFieldWrite16 (
728 IN UINTN Address,
729 IN UINTN StartBit,
730 IN UINTN EndBit,
731 IN UINT16 Value
732 )
733 {
734 return PciWrite16 (
735 Address,
736 BitFieldWrite16 (PciRead16 (Address), StartBit, EndBit, Value)
737 );
738 }
739
740 /**
741 Reads a bit field in a 16-bit PCI configuration, performs a bitwise OR, and
742 writes the result back to the bit field in the 16-bit port.
743
744 Reads the 16-bit PCI configuration register specified by Address, performs a
745 bitwise inclusive OR between the read result and the value specified by
746 OrData, and writes the result to the 16-bit PCI configuration register
747 specified by Address. The value written to the PCI configuration register is
748 returned. This function must guarantee that all PCI read and write operations
749 are serialized. Extra left bits in OrData are stripped.
750
751 If Address > 0x0FFFFFFF, then ASSERT().
752 If Address is not aligned on a 16-bit boundary, then ASSERT().
753 If StartBit is greater than 15, then ASSERT().
754 If EndBit is greater than 15, then ASSERT().
755 If EndBit is less than StartBit, then ASSERT().
756
757 @param Address PCI configuration register to write.
758 @param StartBit The ordinal of the least significant bit in the bit field.
759 Range 0..15.
760 @param EndBit The ordinal of the most significant bit in the bit field.
761 Range 0..15.
762 @param OrData The value to OR with the PCI configuration register.
763
764 @return The value written back to the PCI configuration register.
765
766 **/
767 UINT16
768 EFIAPI
769 PciBitFieldOr16 (
770 IN UINTN Address,
771 IN UINTN StartBit,
772 IN UINTN EndBit,
773 IN UINT16 OrData
774 )
775 {
776 return PciWrite16 (
777 Address,
778 BitFieldOr16 (PciRead16 (Address), StartBit, EndBit, OrData)
779 );
780 }
781
782 /**
783 Reads a bit field in a 16-bit PCI configuration register, performs a bitwise
784 AND, and writes the result back to the bit field in the 16-bit register.
785
786 Reads the 16-bit PCI configuration register specified by Address, performs a
787 bitwise AND between the read result and the value specified by AndData, and
788 writes the result to the 16-bit PCI configuration register specified by
789 Address. The value written to the PCI configuration register is returned.
790 This function must guarantee that all PCI read and write operations are
791 serialized. Extra left bits in AndData are stripped.
792
793 If Address > 0x0FFFFFFF, then ASSERT().
794 If Address is not aligned on a 16-bit boundary, then ASSERT().
795 If StartBit is greater than 15, then ASSERT().
796 If EndBit is greater than 15, then ASSERT().
797 If EndBit is less than StartBit, then ASSERT().
798
799 @param Address PCI configuration register to write.
800 @param StartBit The ordinal of the least significant bit in the bit field.
801 Range 0..15.
802 @param EndBit The ordinal of the most significant bit in the bit field.
803 Range 0..15.
804 @param AndData The value to AND with the PCI configuration register.
805
806 @return The value written back to the PCI configuration register.
807
808 **/
809 UINT16
810 EFIAPI
811 PciBitFieldAnd16 (
812 IN UINTN Address,
813 IN UINTN StartBit,
814 IN UINTN EndBit,
815 IN UINT16 AndData
816 )
817 {
818 return PciWrite16 (
819 Address,
820 BitFieldAnd16 (PciRead16 (Address), StartBit, EndBit, AndData)
821 );
822 }
823
824 /**
825 Reads a bit field in a 16-bit port, performs a bitwise AND followed by a
826 bitwise inclusive OR, and writes the result back to the bit field in the
827 16-bit port.
828
829 Reads the 16-bit PCI configuration register specified by Address, performs a
830 bitwise AND followed by a bitwise inclusive OR between the read result and
831 the value specified by AndData, and writes the result to the 16-bit PCI
832 configuration register specified by Address. The value written to the PCI
833 configuration register is returned. This function must guarantee that all PCI
834 read and write operations are serialized. Extra left bits in both AndData and
835 OrData are stripped.
836
837 If Address > 0x0FFFFFFF, then ASSERT().
838 If Address is not aligned on a 16-bit boundary, then ASSERT().
839 If StartBit is greater than 15, then ASSERT().
840 If EndBit is greater than 15, then ASSERT().
841 If EndBit is less than StartBit, then ASSERT().
842
843 @param Address PCI configuration register to write.
844 @param StartBit The ordinal of the least significant bit in the bit field.
845 Range 0..15.
846 @param EndBit The ordinal of the most significant bit in the bit field.
847 Range 0..15.
848 @param AndData The value to AND with the PCI configuration register.
849 @param OrData The value to OR with the result of the AND operation.
850
851 @return The value written back to the PCI configuration register.
852
853 **/
854 UINT16
855 EFIAPI
856 PciBitFieldAndThenOr16 (
857 IN UINTN Address,
858 IN UINTN StartBit,
859 IN UINTN EndBit,
860 IN UINT16 AndData,
861 IN UINT16 OrData
862 )
863 {
864 return PciWrite16 (
865 Address,
866 BitFieldAndThenOr16 (PciRead16 (Address), StartBit, EndBit, AndData, OrData)
867 );
868 }
869
870 /**
871 Reads a 32-bit PCI configuration register.
872
873 Reads and returns the 32-bit PCI configuration register specified by Address.
874 This function must guarantee that all PCI read and write operations are
875 serialized.
876
877 If Address > 0x0FFFFFFF, then ASSERT().
878 If Address is not aligned on a 32-bit boundary, then ASSERT().
879
880 @param Address Address that encodes the PCI Bus, Device, Function and
881 Register.
882
883 @return The value read from the PCI configuration register.
884
885 **/
886 UINT32
887 EFIAPI
888 PciRead32 (
889 IN UINTN Address
890 )
891 {
892 ASSERT_INVALID_PCI_ADDRESS (Address, 3);
893
894 return DxePciLibPciRootBridgeIoReadWorker (Address, EfiPciWidthUint32);
895 }
896
897 /**
898 Writes a 32-bit PCI configuration register.
899
900 Writes the 32-bit PCI configuration register specified by Address with the
901 value specified by Value. Value is returned. This function must guarantee
902 that all PCI read and write operations are serialized.
903
904 If Address > 0x0FFFFFFF, then ASSERT().
905 If Address is not aligned on a 32-bit boundary, then ASSERT().
906
907 @param Address Address that encodes the PCI Bus, Device, Function and
908 Register.
909 @param Data The value to write.
910
911 @return The value written to the PCI configuration register.
912
913 **/
914 UINT32
915 EFIAPI
916 PciWrite32 (
917 IN UINTN Address,
918 IN UINT32 Data
919 )
920 {
921 ASSERT_INVALID_PCI_ADDRESS (Address, 3);
922
923 return DxePciLibPciRootBridgeIoWriteWorker (Address, EfiPciWidthUint32, Data);
924 }
925
926 /**
927 Performs a bitwise inclusive OR of a 32-bit PCI configuration register with
928 a 32-bit value.
929
930 Reads the 32-bit PCI configuration register specified by Address, performs a
931 bitwise inclusive OR between the read result and the value specified by
932 OrData, and writes the result to the 32-bit PCI configuration register
933 specified by Address. The value written to the PCI configuration register is
934 returned. This function must guarantee that all PCI read and write operations
935 are serialized.
936
937 If Address > 0x0FFFFFFF, then ASSERT().
938 If Address is not aligned on a 32-bit boundary, then ASSERT().
939
940 @param Address Address that encodes the PCI Bus, Device, Function and
941 Register.
942 @param OrData The value to OR with the PCI configuration register.
943
944 @return The value written back to the PCI configuration register.
945
946 **/
947 UINT32
948 EFIAPI
949 PciOr32 (
950 IN UINTN Address,
951 IN UINT32 OrData
952 )
953 {
954 return PciWrite32 (Address, PciRead32 (Address) | OrData);
955 }
956
957 /**
958 Performs a bitwise AND of a 32-bit PCI configuration register with a 32-bit
959 value.
960
961 Reads the 32-bit PCI configuration register specified by Address, performs a
962 bitwise AND between the read result and the value specified by AndData, and
963 writes the result to the 32-bit PCI configuration register specified by
964 Address. The value written to the PCI configuration register is returned.
965 This function must guarantee that all PCI read and write operations are
966 serialized.
967
968 If Address > 0x0FFFFFFF, then ASSERT().
969 If Address is not aligned on a 32-bit boundary, then ASSERT().
970
971 @param Address Address that encodes the PCI Bus, Device, Function and
972 Register.
973 @param AndData The value to AND with the PCI configuration register.
974
975 @return The value written back to the PCI configuration register.
976
977 **/
978 UINT32
979 EFIAPI
980 PciAnd32 (
981 IN UINTN Address,
982 IN UINT32 AndData
983 )
984 {
985 return PciWrite32 (Address, PciRead32 (Address) & AndData);
986 }
987
988 /**
989 Performs a bitwise AND of a 32-bit PCI configuration register with a 32-bit
990 value, followed a bitwise inclusive OR with another 32-bit value.
991
992 Reads the 32-bit PCI configuration register specified by Address, performs a
993 bitwise AND between the read result and the value specified by AndData,
994 performs a bitwise inclusive OR between the result of the AND operation and
995 the value specified by OrData, and writes the result to the 32-bit PCI
996 configuration register specified by Address. The value written to the PCI
997 configuration register is returned. This function must guarantee that all PCI
998 read and write operations are serialized.
999
1000 If Address > 0x0FFFFFFF, then ASSERT().
1001 If Address is not aligned on a 32-bit boundary, then ASSERT().
1002
1003 @param Address Address that encodes the PCI Bus, Device, Function and
1004 Register.
1005 @param AndData The value to AND with the PCI configuration register.
1006 @param OrData The value to OR with the result of the AND operation.
1007
1008 @return The value written back to the PCI configuration register.
1009
1010 **/
1011 UINT32
1012 EFIAPI
1013 PciAndThenOr32 (
1014 IN UINTN Address,
1015 IN UINT32 AndData,
1016 IN UINT32 OrData
1017 )
1018 {
1019 return PciWrite32 (Address, (PciRead32 (Address) & AndData) | OrData);
1020 }
1021
1022 /**
1023 Reads a bit field of a PCI configuration register.
1024
1025 Reads the bit field in a 32-bit PCI configuration register. The bit field is
1026 specified by the StartBit and the EndBit. The value of the bit field is
1027 returned.
1028
1029 If Address > 0x0FFFFFFF, then ASSERT().
1030 If Address is not aligned on a 32-bit boundary, then ASSERT().
1031 If StartBit is greater than 31, then ASSERT().
1032 If EndBit is greater than 31, then ASSERT().
1033 If EndBit is less than StartBit, then ASSERT().
1034
1035 @param Address PCI configuration register to read.
1036 @param StartBit The ordinal of the least significant bit in the bit field.
1037 Range 0..31.
1038 @param EndBit The ordinal of the most significant bit in the bit field.
1039 Range 0..31.
1040
1041 @return The value of the bit field read from the PCI configuration register.
1042
1043 **/
1044 UINT32
1045 EFIAPI
1046 PciBitFieldRead32 (
1047 IN UINTN Address,
1048 IN UINTN StartBit,
1049 IN UINTN EndBit
1050 )
1051 {
1052 return BitFieldRead32 (PciRead32 (Address), StartBit, EndBit);
1053 }
1054
1055 /**
1056 Writes a bit field to a PCI configuration register.
1057
1058 Writes Value to the bit field of the PCI configuration register. The bit
1059 field is specified by the StartBit and the EndBit. All other bits in the
1060 destination PCI configuration register are preserved. The new value of the
1061 32-bit register is returned.
1062
1063 If Address > 0x0FFFFFFF, then ASSERT().
1064 If Address is not aligned on a 32-bit boundary, then ASSERT().
1065 If StartBit is greater than 31, then ASSERT().
1066 If EndBit is greater than 31, then ASSERT().
1067 If EndBit is less than StartBit, then ASSERT().
1068
1069 @param Address PCI configuration register to write.
1070 @param StartBit The ordinal of the least significant bit in the bit field.
1071 Range 0..31.
1072 @param EndBit The ordinal of the most significant bit in the bit field.
1073 Range 0..31.
1074 @param Value New value of the bit field.
1075
1076 @return The value written back to the PCI configuration register.
1077
1078 **/
1079 UINT32
1080 EFIAPI
1081 PciBitFieldWrite32 (
1082 IN UINTN Address,
1083 IN UINTN StartBit,
1084 IN UINTN EndBit,
1085 IN UINT32 Value
1086 )
1087 {
1088 return PciWrite32 (
1089 Address,
1090 BitFieldWrite32 (PciRead32 (Address), StartBit, EndBit, Value)
1091 );
1092 }
1093
1094 /**
1095 Reads a bit field in a 32-bit PCI configuration, performs a bitwise OR, and
1096 writes the result back to the bit field in the 32-bit port.
1097
1098 Reads the 32-bit PCI configuration register specified by Address, performs a
1099 bitwise inclusive OR between the read result and the value specified by
1100 OrData, and writes the result to the 32-bit PCI configuration register
1101 specified by Address. The value written to the PCI configuration register is
1102 returned. This function must guarantee that all PCI read and write operations
1103 are serialized. Extra left bits in OrData are stripped.
1104
1105 If Address > 0x0FFFFFFF, then ASSERT().
1106 If Address is not aligned on a 32-bit boundary, then ASSERT().
1107 If StartBit is greater than 31, then ASSERT().
1108 If EndBit is greater than 31, then ASSERT().
1109 If EndBit is less than StartBit, then ASSERT().
1110
1111 @param Address PCI configuration register to write.
1112 @param StartBit The ordinal of the least significant bit in the bit field.
1113 Range 0..31.
1114 @param EndBit The ordinal of the most significant bit in the bit field.
1115 Range 0..31.
1116 @param OrData The value to OR with the PCI configuration register.
1117
1118 @return The value written back to the PCI configuration register.
1119
1120 **/
1121 UINT32
1122 EFIAPI
1123 PciBitFieldOr32 (
1124 IN UINTN Address,
1125 IN UINTN StartBit,
1126 IN UINTN EndBit,
1127 IN UINT32 OrData
1128 )
1129 {
1130 return PciWrite32 (
1131 Address,
1132 BitFieldOr32 (PciRead32 (Address), StartBit, EndBit, OrData)
1133 );
1134 }
1135
1136 /**
1137 Reads a bit field in a 32-bit PCI configuration register, performs a bitwise
1138 AND, and writes the result back to the bit field in the 32-bit register.
1139
1140 Reads the 32-bit PCI configuration register specified by Address, performs a
1141 bitwise AND between the read result and the value specified by AndData, and
1142 writes the result to the 32-bit PCI configuration register specified by
1143 Address. The value written to the PCI configuration register is returned.
1144 This function must guarantee that all PCI read and write operations are
1145 serialized. Extra left bits in AndData are stripped.
1146
1147 If Address > 0x0FFFFFFF, then ASSERT().
1148 If Address is not aligned on a 32-bit boundary, then ASSERT().
1149 If StartBit is greater than 31, then ASSERT().
1150 If EndBit is greater than 31, then ASSERT().
1151 If EndBit is less than StartBit, then ASSERT().
1152
1153 @param Address PCI configuration register to write.
1154 @param StartBit The ordinal of the least significant bit in the bit field.
1155 Range 0..31.
1156 @param EndBit The ordinal of the most significant bit in the bit field.
1157 Range 0..31.
1158 @param AndData The value to AND with the PCI configuration register.
1159
1160 @return The value written back to the PCI configuration register.
1161
1162 **/
1163 UINT32
1164 EFIAPI
1165 PciBitFieldAnd32 (
1166 IN UINTN Address,
1167 IN UINTN StartBit,
1168 IN UINTN EndBit,
1169 IN UINT32 AndData
1170 )
1171 {
1172 return PciWrite32 (
1173 Address,
1174 BitFieldAnd32 (PciRead32 (Address), StartBit, EndBit, AndData)
1175 );
1176 }
1177
1178 /**
1179 Reads a bit field in a 32-bit port, performs a bitwise AND followed by a
1180 bitwise inclusive OR, and writes the result back to the bit field in the
1181 32-bit port.
1182
1183 Reads the 32-bit PCI configuration register specified by Address, performs a
1184 bitwise AND followed by a bitwise inclusive OR between the read result and
1185 the value specified by AndData, and writes the result to the 32-bit PCI
1186 configuration register specified by Address. The value written to the PCI
1187 configuration register is returned. This function must guarantee that all PCI
1188 read and write operations are serialized. Extra left bits in both AndData and
1189 OrData are stripped.
1190
1191 If Address > 0x0FFFFFFF, then ASSERT().
1192 If Address is not aligned on a 32-bit boundary, then ASSERT().
1193 If StartBit is greater than 31, then ASSERT().
1194 If EndBit is greater than 31, then ASSERT().
1195 If EndBit is less than StartBit, then ASSERT().
1196
1197 @param Address PCI configuration register to write.
1198 @param StartBit The ordinal of the least significant bit in the bit field.
1199 Range 0..31.
1200 @param EndBit The ordinal of the most significant bit in the bit field.
1201 Range 0..31.
1202 @param AndData The value to AND with the PCI configuration register.
1203 @param OrData The value to OR with the result of the AND operation.
1204
1205 @return The value written back to the PCI configuration register.
1206
1207 **/
1208 UINT32
1209 EFIAPI
1210 PciBitFieldAndThenOr32 (
1211 IN UINTN Address,
1212 IN UINTN StartBit,
1213 IN UINTN EndBit,
1214 IN UINT32 AndData,
1215 IN UINT32 OrData
1216 )
1217 {
1218 return PciWrite32 (
1219 Address,
1220 BitFieldAndThenOr32 (PciRead32 (Address), StartBit, EndBit, AndData, OrData)
1221 );
1222 }
1223
1224 /**
1225 Reads a range of PCI configuration registers into a caller supplied buffer.
1226
1227 Reads the range of PCI configuration registers specified by StartAddress and
1228 Size into the buffer specified by Buffer. This function only allows the PCI
1229 configuration registers from a single PCI function to be read. Size is
1230 returned. When possible 32-bit PCI configuration read cycles are used to read
1231 from StartAdress to StartAddress + Size. Due to alignment restrictions, 8-bit
1232 and 16-bit PCI configuration read cycles may be used at the beginning and the
1233 end of the range.
1234
1235 If StartAddress > 0x0FFFFFFF, then ASSERT().
1236 If ((StartAddress & 0xFFF) + Size) > 0x1000, then ASSERT().
1237 If Size > 0 and Buffer is NULL, then ASSERT().
1238
1239 @param StartAddress Starting address that encodes the PCI Bus, Device,
1240 Function and Register.
1241 @param Size Size in bytes of the transfer.
1242 @param Buffer Pointer to a buffer receiving the data read.
1243
1244 @return Size
1245
1246 **/
1247 UINTN
1248 EFIAPI
1249 PciReadBuffer (
1250 IN UINTN StartAddress,
1251 IN UINTN Size,
1252 OUT VOID *Buffer
1253 )
1254 {
1255 UINTN ReturnValue;
1256
1257 ASSERT_INVALID_PCI_ADDRESS (StartAddress, 0);
1258 ASSERT (((StartAddress & 0xFFF) + Size) <= 0x1000);
1259
1260 if (Size == 0) {
1261 return Size;
1262 }
1263
1264 ASSERT (Buffer != NULL);
1265
1266 //
1267 // Save Size for return
1268 //
1269 ReturnValue = Size;
1270
1271 if ((StartAddress & BIT0) != 0) {
1272 //
1273 // Read a byte if StartAddress is byte aligned
1274 //
1275 *(volatile UINT8 *)Buffer = PciRead8 (StartAddress);
1276 StartAddress += sizeof (UINT8);
1277 Size -= sizeof (UINT8);
1278 Buffer = (UINT8*)Buffer + 1;
1279 }
1280
1281 if (Size >= sizeof (UINT16) && (StartAddress & BIT1) != 0) {
1282 //
1283 // Read a word if StartAddress is word aligned
1284 //
1285 *(volatile UINT16 *)Buffer = PciRead16 (StartAddress);
1286 StartAddress += sizeof (UINT16);
1287 Size -= sizeof (UINT16);
1288 Buffer = (UINT16*)Buffer + 1;
1289 }
1290
1291 while (Size >= sizeof (UINT32)) {
1292 //
1293 // Read as many double words as possible
1294 //
1295 *(volatile UINT32 *)Buffer = PciRead32 (StartAddress);
1296 StartAddress += sizeof (UINT32);
1297 Size -= sizeof (UINT32);
1298 Buffer = (UINT32*)Buffer + 1;
1299 }
1300
1301 if (Size >= sizeof (UINT16)) {
1302 //
1303 // Read the last remaining word if exist
1304 //
1305 *(volatile UINT16 *)Buffer = PciRead16 (StartAddress);
1306 StartAddress += sizeof (UINT16);
1307 Size -= sizeof (UINT16);
1308 Buffer = (UINT16*)Buffer + 1;
1309 }
1310
1311 if (Size >= sizeof (UINT8)) {
1312 //
1313 // Read the last remaining byte if exist
1314 //
1315 *(volatile UINT8 *)Buffer = PciRead8 (StartAddress);
1316 }
1317
1318 return ReturnValue;
1319 }
1320
1321 /**
1322 Copies the data in a caller supplied buffer to a specified range of PCI
1323 configuration space.
1324
1325 Writes the range of PCI configuration registers specified by StartAddress and
1326 Size from the buffer specified by Buffer. This function only allows the PCI
1327 configuration registers from a single PCI function to be written. Size is
1328 returned. When possible 32-bit PCI configuration write cycles are used to
1329 write from StartAdress to StartAddress + Size. Due to alignment restrictions,
1330 8-bit and 16-bit PCI configuration write cycles may be used at the beginning
1331 and the end of the range.
1332
1333 If StartAddress > 0x0FFFFFFF, then ASSERT().
1334 If ((StartAddress & 0xFFF) + Size) > 0x1000, then ASSERT().
1335 If Size > 0 and Buffer is NULL, then ASSERT().
1336
1337 @param StartAddress Starting address that encodes the PCI Bus, Device,
1338 Function and Register.
1339 @param Size Size in bytes of the transfer.
1340 @param Buffer Pointer to a buffer containing the data to write.
1341
1342 @return Size
1343
1344 **/
1345 UINTN
1346 EFIAPI
1347 PciWriteBuffer (
1348 IN UINTN StartAddress,
1349 IN UINTN Size,
1350 IN VOID *Buffer
1351 )
1352 {
1353 UINTN ReturnValue;
1354
1355 ASSERT_INVALID_PCI_ADDRESS (StartAddress, 0);
1356 ASSERT (((StartAddress & 0xFFF) + Size) <= 0x1000);
1357
1358 if (Size == 0) {
1359 return 0;
1360 }
1361
1362 ASSERT (Buffer != NULL);
1363
1364 //
1365 // Save Size for return
1366 //
1367 ReturnValue = Size;
1368
1369 if ((StartAddress & BIT0) != 0) {
1370 //
1371 // Write a byte if StartAddress is byte aligned
1372 //
1373 PciWrite8 (StartAddress, *(UINT8*)Buffer);
1374 StartAddress += sizeof (UINT8);
1375 Size -= sizeof (UINT8);
1376 Buffer = (UINT8*)Buffer + 1;
1377 }
1378
1379 if (Size >= sizeof (UINT16) && (StartAddress & BIT1) != 0) {
1380 //
1381 // Write a word if StartAddress is word aligned
1382 //
1383 PciWrite16 (StartAddress, *(UINT16*)Buffer);
1384 StartAddress += sizeof (UINT16);
1385 Size -= sizeof (UINT16);
1386 Buffer = (UINT16*)Buffer + 1;
1387 }
1388
1389 while (Size >= sizeof (UINT32)) {
1390 //
1391 // Write as many double words as possible
1392 //
1393 PciWrite32 (StartAddress, *(UINT32*)Buffer);
1394 StartAddress += sizeof (UINT32);
1395 Size -= sizeof (UINT32);
1396 Buffer = (UINT32*)Buffer + 1;
1397 }
1398
1399 if (Size >= sizeof (UINT16)) {
1400 //
1401 // Write the last remaining word if exist
1402 //
1403 PciWrite16 (StartAddress, *(UINT16*)Buffer);
1404 StartAddress += sizeof (UINT16);
1405 Size -= sizeof (UINT16);
1406 Buffer = (UINT16*)Buffer + 1;
1407 }
1408
1409 if (Size >= sizeof (UINT8)) {
1410 //
1411 // Write the last remaining byte if exist
1412 //
1413 PciWrite8 (StartAddress, *(UINT8*)Buffer);
1414 }
1415
1416 return ReturnValue;
1417 }