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