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