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