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