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