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