summaryrefslogtreecommitdiff
path: root/Core/CORE_PEI/CpuIo.c
blob: 634f676d7ae608a22420db57e4222f5a27b4a462 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************

//*****************************************************************************
// $Header: /Alaska/SOURCE/Core/EDK/PeiMain/CpuIo.c 13    3/22/11 5:14p Felixp $
//
// $Revision: 13 $
//
// $Date: 3/22/11 5:14p $
//*****************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Core/EDK/PeiMain/CpuIo.c $
// 
// 13    3/22/11 5:14p Felixp
// Method of invocation of the in-memory entry point has changed 
// from LOAD_HOB to eLink registered under PeiCoreMemoryInstalled.
// This is more efficient.
// 
// 12    2/05/11 3:43p Artems
// Changed functions calling interface (added CONST qualifiers) as
// required by PI 1.0 specification
// 
// 11    11/05/09 4:59p Oleksiyy
// EIP 27821 Support for 64 bit operations in IoRead and IoWrite added. To
// use this functions   IA32CLib.c, x64AsmLib.asm files should be updated
// also.
// 
// 10    7/08/09 5:53p Vyacheslava
// Updated according to the coding standards.
//
// 9     6/16/05 10:56a Felixp
// 1. Once memory is available, PEI Core PEIMs that created PEIM_LOAD_HOB
// are reallocated to memory.
// 2. ImagePei.c removed
// 3. FileLoader moved from DxeIpl to PEI Core
//
// 8     6/10/05 5:49p Felixp
// Bug fixes in MemRead, MemWrite, IoRead, IoWrite
//
// 7     6/06/05 7:48p Felixp
//
// 6     6/06/05 1:25p Felixp
//
// 5     6/03/05 3:45p Felixp
// Updated to support changes introduced in PEI CIS 0.91
//
// 4     3/04/05 9:38a Mandal
//
// 3     2/01/05 2:35a Felixp
//
// 2     1/18/05 3:21p Felixp
// PrintDebugMessage renamed to Trace
//
// 1     12/23/04 9:42a Felixp
//
// 7     1/27/04 3:58a Felixp
// Bug fixes and improvements as a part of PEI CORE integration
//
// 6     12/29/03 5:25p Felixp
// preffix 'EFI_' added to the names of the PEIM descriptor structures and
// flags
//
// 5     12/15/03 4:35p Robert
//
// 4     12/15/03 4:25p Robert
//
// 3     12/15/03 12:40p Robert
// changed position of GUID definition below PPI include
//
// 2     12/15/03 11:49a Robert
// updated PPI definitions and removed entry point definitions
//
// 1     12/11/03 5:34p Robert
// Initial Check In
//
//*****************************************************************************
//<AMI_FHDR_START>
//
// Name:    CpuIo.c
//
// Description: This file contains the implementation of the CPU I/O
//      specification.  For questions about the specification refer to the
//      PEI CIS specification chapter 9
//
//<AMI_FHDR_END>
//*****************************************************************************

// Module specific Includes
#include <AmiPeiLib.h>
#include <AmiHobs.h>
// Produced PPIs
#include <PPI/CpuIo.h>

// Function Prototypes
static EFI_STATUS LocalMemRead (
    IN      CONST EFI_PEI_SERVICES          **PeiServices,
    IN      CONST EFI_PEI_CPU_IO_PPI        *This,
    IN      EFI_PEI_CPU_IO_PPI_WIDTH  Width,
    IN      UINT64                    Address,
    IN      UINTN                     Count,
    IN OUT  VOID                      *Buffer
);

static EFI_STATUS LocalMemWrite  (
    IN      CONST EFI_PEI_SERVICES          **PeiServices,
    IN      CONST EFI_PEI_CPU_IO_PPI        *This,
    IN      EFI_PEI_CPU_IO_PPI_WIDTH  Width,
    IN      UINT64                    Address,
    IN      UINTN                     Count,
    IN OUT  VOID                      *Buffer
);

static EFI_STATUS LocalIoRead (
    IN      CONST EFI_PEI_SERVICES          **PeiServices,
    IN      CONST EFI_PEI_CPU_IO_PPI        *This,
    IN      EFI_PEI_CPU_IO_PPI_WIDTH  Width,
    IN      UINT64                    Address,
    IN      UINTN                     Count,
    IN OUT  VOID                      *Buffer
);

static EFI_STATUS LocalIoWrite  (
    IN      CONST EFI_PEI_SERVICES          **PeiServices,
    IN      CONST EFI_PEI_CPU_IO_PPI        *This,
    IN      EFI_PEI_CPU_IO_PPI_WIDTH  Width,
    IN      UINT64                    Address,
    IN      UINTN                     Count,
    IN OUT  VOID                      *Buffer
);

static UINT8 LocalIoRead8 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address
);

static UINT16 LocalIoRead16 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address
);

static UINT32 LocalIoRead32 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address
);

static UINT64 LocalIoRead64 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address
);

static VOID LocalIoWrite8 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address,
    IN  UINT8                 Data
);

static VOID LocalIoWrite16 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address,
    IN  UINT16                Data
);

static VOID LocalIoWrite32 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address,
    IN  UINT32                Data
);

static VOID LocalIoWrite64 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address,
    IN  UINT64                Data
);

static UINT8 MemRead8 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address
);

static UINT16 MemRead16 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address
);

static UINT32 MemRead32 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address
);

static UINT64 MemRead64 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address
);

static VOID MemWrite8 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address,
    IN  UINT8                 Data
);

static VOID MemWrite16 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address,
    IN  UINT16                Data
);

static VOID MemWrite32 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address,
    IN  UINT32                Data
);

static VOID MemWrite64 (
    IN  CONST EFI_PEI_SERVICES      **PeiServices,
    IN  CONST EFI_PEI_CPU_IO_PPI    *This,
    IN  UINT64                Address,
    IN  UINT64                Data
);

// PPI interface definition
static EFI_PEI_CPU_IO_PPI mCpuIoPpi = {
    {LocalMemRead, LocalMemWrite},
    {LocalIoRead, LocalIoWrite},
    LocalIoRead8, LocalIoRead16, LocalIoRead32, LocalIoRead64,
    LocalIoWrite8, LocalIoWrite16, LocalIoWrite32, LocalIoWrite64,
    MemRead8, MemRead16, MemRead32, MemRead64,
    MemWrite8, MemWrite16, MemWrite32, MemWrite64
};

// PPI that are installed
static EFI_PEI_PPI_DESCRIPTOR mPpiList[] = {
    {
        EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
        &gPeiCpuIoPpiInServiceTableGuid, 
        NULL
    }
};

// Function Definitions
EFI_STATUS EFIAPI CpuIoMemInit (
    IN EFI_FFS_FILE_HEADER       *FfsHeader,
    IN EFI_PEI_SERVICES          **PeiServices
)
{
    // Update the CpuIo pointer
    (*PeiServices)->CpuIo = &mCpuIoPpi;
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//============================================================================
// Procedure:   CpuIoInit
//
// Description: This function installs the CPU I/O PPI and exits
//
// Input:
//      *FfsHeader - pointer to the header of the current firmware file system
//      **PeiServices - pointer to the Pei Services function and data structure
//
// Output:  always returns EFI_SUCCESS
//
//============================================================================
//<AMI_PHDR_END>

EFI_STATUS EFIAPI CpuIoInit (
    IN EFI_FFS_FILE_HEADER       *FfsHeader,
    IN EFI_PEI_SERVICES          **PeiServices
)
{
    EFI_STATUS      Status;
    
    // Install the CpuIo Ppi
    (*PeiServices)->CpuIo = &mCpuIoPpi;
    Status = (*PeiServices)->InstallPpi( PeiServices, mPpiList );
    
    return Status;
}

//<AMI_PHDR_START>
//============================================================================
// Procedure:   CalcAddrChanges
//
// Description: This function determines which addresses need to be changed
//      as data is read or written.
//
// Input:
//      Width - Contains the width and type of memory or I/O read that is needed
//
// Output:
//      *In - The number of bytes to increment the input buffer address after
//              every read or write access
//      *Out - The number of bytes to increment the output buffer address after
//              every read or write access
//
//============================================================================
//<AMI_PHDR_END>

VOID CalcAddrChanges(
    IN  EFI_PEI_CPU_IO_PPI_WIDTH    Width,
    OUT UINTN                       *In,
    OUT UINTN                       *Out
)
{
    // get the number of bytes per read or write access and the turn that into
    //  an Hex Address Incrementing value
    *In = *Out = 1 << (Width % 4);
    
    if ( (Width >= EfiPeiCpuIoWidthFifoUint8) && 
         (Width <= EfiPeiCpuIoWidthFifoUint64) )
        *Out = 0;
        
    else if ( (Width >= EfiPeiCpuIoWidthFillUint8) && 
              (Width <= EfiPeiCpuIoWidthFillUint64) )
        *In = 0;
}


//<AMI_PHDR_START>
//============================================================================
// Procedure:   LocalIoRead8
//
// Description: This function is called to read a byte from the I/O space
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be read from
//
// Output:  UINT8 - This is the value read from the I/O space specified by the
//                      value passed in by the input variable Address
//
//============================================================================
//<AMI_PHDR_END>

static UINT8 LocalIoRead8 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address
)
{
    return IoRead8 ((UINT16) Address);
}


//<AMI_PHDR_START>
//============================================================================
// Procedure:   LocalIoRead16
//
// Description: This function is called to read a word from the I/O space
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be read from
//
// Output:  UINT16 - This is the value read from the I/O space specified by the
//                      by the input variable Address
//
//============================================================================
//<AMI_PHDR_END>

static UINT16 LocalIoRead16 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address
)
{
    return IoRead16 ((UINT16) Address);
}


//<AMI_PHDR_START>
//============================================================================
// Procedure:   LocalIoRead32
//
// Description: This function is called to read a dword from the I/O space
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be read from
//
// Output:  UINT32 - This is the value read from the I/O space specified by the
//                      value passed in by the input variable Address
//
//============================================================================
//<AMI_PHDR_END>

static UINT32 LocalIoRead32 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address
)
{
    return IoRead32 ((UINT16) Address);
}



//<AMI_PHDR_START>
//============================================================================
// Procedure:   LocalIoRead64
//
// Description: This function is called to read a qword from the I/O space
//      This function is not available in a 32bit system so we just return 0 if
//      it is called. We cannot remove this function because it is part of the
//      specification
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be read from
//
// Output:  UINT64 - This is the value read from the I/O space specified by the
//                      value passed in by the input variable Address
//
//============================================================================
//<AMI_PHDR_END>
static UINT64 LocalIoRead64 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address
)
{
    return IoRead64 ((UINT16) Address);
}


//<AMI_PHDR_START>
//============================================================================
// Procedure:   LocalIoWrite8
//
// Description: This function is called to write a byte to the I/O space
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be written to
//      Buffer8 -   The value to be written to location specified by Address
//
// Output:  None
//
//============================================================================
//<AMI_PHDR_END>

static VOID LocalIoWrite8 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address,
    IN UINT8  Buffer8
)
{
    IoWrite8 ((UINT16) Address, Buffer8);
}


//<AMI_PHDR_START>
//============================================================================
// Procedure:   LocaIoWrite16
//
// Description: This function is called to write a word to the I/O space
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be written to
//      Buffer16 -  The value to be written to location specified by Address
//
// Output:  VOID
//
//============================================================================
//<AMI_PHDR_END>

static VOID LocalIoWrite16 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address,
    IN UINT16 Buffer16
)
{
    IoWrite16 ((UINT16) Address, Buffer16);
}


//<AMI_PHDR_START>
//============================================================================
// Procedure:   LocaIoWrite32
//
// Description: This function is called to write a dword to the I/O space
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be written to
//      Buffer32 -  The value to be written to location specified by Address
//
// Output:  None
//
//============================================================================
//<AMI_PHDR_END>

static VOID LocalIoWrite32 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address,
    IN UINT32 Buffer32
)
{
    IoWrite32 ((UINT16) Address, Buffer32);
}


//<AMI_PHDR_START>
//============================================================================
// Procedure:   LocaIoWrite64
//
// Description: This function is called to write a qword to the I/O space
//      This function is not available in a 32bit system so we just do nothing
//      if it is called. We cannot remove this function because it is part of
//      the specification
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be written to
//      Buffer64 -  The value to be written to location specified by Address
//
// Output:  None
//
//============================================================================
//<AMI_PHDR_END>

static VOID LocalIoWrite64 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address,
    IN UINT64 Buffer64
)
{
    IoWrite64 ((UINT16) Address, Buffer64);
}

//<AMI_PHDR_START>
//============================================================================
// Procedure:   MemRead8
//
// Description: This function is called to read a byte from the Memory space
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be read from
//
// Output:  UINT8 - This is the value read from the Memory space specified by
//                      the input variable Address
//
// Referrals:
//
//============================================================================
//<AMI_PHDR_END>

static UINT8 MemRead8 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST struct _EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address
)
{
	return  *(volatile UINT8*) Address;
}

//<AMI_PHDR_START>
//============================================================================
// Procedure:   MemRead16
//
// Description: This function is called to read a word from the Memory space
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be read from
//
// Output:  UINT16 - This is the value read from the Memory space specified
//                      by the input variable Address
//
//============================================================================
//<AMI_PHDR_END>

static UINT16 MemRead16 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST struct _EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address
)
{
	return  *(volatile UINT16*) Address;
}

//<AMI_PHDR_START>
//============================================================================
// Procedure:   MemRead32
//
// Description: This function is called to read a dword from the Memory space
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be read from
//
// Output:  UINT32 - This is the value read from the Memory space specified by
//                      the input variable Address
//
//============================================================================
//<AMI_PHDR_END>

static UINT32 MemRead32 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST struct _EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address
)
{
	return  *(volatile UINT32*) Address;
}

//<AMI_PHDR_START>
//============================================================================
// Procedure:   MemRead64
//
// Description: This function is called to read a qword from the Memory space
//      This function is not available in a 32bit system so we just return 0 if
//      it is called. We cannot remove this function because it is part of the
//      specification
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be read from
//
// Output:  UINT64 - This is the value read from the Memory space specified by
//                      the input variable Address
//
//============================================================================
//<AMI_PHDR_END>

static UINT64 MemRead64 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST struct _EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address
)
{
	return  *(volatile UINT64*) Address;
}

//<AMI_PHDR_START>
//============================================================================
// Procedure:   MemWrite8
//
// Description: This function is called to write a byte to the Memory space
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be written to
//      Buffer8 -   The value to be written to location specified by Address
//
// Output:  None
//
//============================================================================
//<AMI_PHDR_END>

static VOID MemWrite8 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST struct _EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address,
    IN UINT8    Buffer8
)
{
	*(volatile UINT8*) Address = Buffer8;
}

//<AMI_PHDR_START>
//============================================================================
// Procedure:   MemWrite16
//
// Description: This function is called to write a word to the Memory space
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be written to
//      Buffer16 -  The value to be written to location specified by Address
//
// Output:  None
//
//============================================================================
//<AMI_PHDR_END>

static VOID MemWrite16 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST struct _EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address,
    IN UINT16 Buffer16
)
{
	*(volatile UINT16*) Address = Buffer16;
}

//<AMI_PHDR_START>
//============================================================================
// Procedure:   MemWrite32
//
// Description: This function is called to write a dword to the Memory space
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be written to
//      Buffer32 -  The value to be written to location specified by Address
//
// Output:  None
//
//============================================================================
//<AMI_PHDR_END>

static VOID MemWrite32 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST struct _EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address,
    IN UINT32 Buffer32
)
{
	*(volatile UINT32*) Address = Buffer32;
}

//<AMI_PHDR_START>
//============================================================================
// Procedure:   MemWrite64
//
// Description: This function is called to write a qword to the Memory space
//      This function is not available in a 32bit system so we just do nothing
//      if it is called. We cannot remove this function because it is part of
//      the specification
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Address - A 64 bit address that points to the location to be written to
//      Buffer64 -  The value to be written to location specified by Address
//
// Output:  None
//
//============================================================================
//<AMI_PHDR_END>

static VOID MemWrite64 (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST struct _EFI_PEI_CPU_IO_PPI *This,
    IN UINT64 Address,
    IN UINT64 Buffer
)
{
	*(volatile UINT64*) Address = Buffer;
}

//<AMI_PHDR_START>
//============================================================================
// Procedure:   LocalMemWrite
//
// Description: This function is called to do multiple writes to the memory
//      space. It also is capable of doing a memory space fill of a single value
//      or a serial write from a buffer depending on the Width type being passed
//      in.
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Width - Size of the I/O space that is to be written
//      Address - A 64 bit address that points to the location to be read from
//      Count - Number of times to write
//      *Buffer - Data to be written to the I/O space
//
// Output:  always returns EFI_SUCCESS
//
//============================================================================
//<AMI_PHDR_END>

static EFI_STATUS LocalMemWrite (
    IN      CONST EFI_PEI_SERVICES            **PeiServices,
    IN      CONST struct _EFI_PEI_CPU_IO_PPI  *This,
    IN      EFI_PEI_CPU_IO_PPI_WIDTH    Width,
    IN      UINT64                      Address,
    IN      UINTN                       Count,
    IN OUT  VOID                        *Buffer
)
{
    UINTN   Idx;
    UINTN   OutputInc, InputInc; // Input and Output counters
    
    CalcAddrChanges(
        Width, 
        &InputInc, 
        &OutputInc
    );
    
    for ( Idx = 0; Idx < Count; Idx++, Address += OutputInc, Buffer = (UINT8*)Buffer + InputInc ) {

        switch (Width) {
            case EfiPeiCpuIoWidthUint8:
            case EfiPeiCpuIoWidthFifoUint8:
            case EfiPeiCpuIoWidthFillUint8:
                    MemWrite8(
                        PeiServices, 
                        This, 
                        Address, 
                        *(UINT8*)Buffer
                    );
                break;
                
            case EfiPeiCpuIoWidthUint16:
            case EfiPeiCpuIoWidthFifoUint16:
            case EfiPeiCpuIoWidthFillUint16:
                    MemWrite16(
                        PeiServices, 
                        This, 
                        Address, 
                        *(UINT16*)Buffer
                    );
                break;
                
            case EfiPeiCpuIoWidthUint32:
            case EfiPeiCpuIoWidthFifoUint32:
            case EfiPeiCpuIoWidthFillUint32:
                    MemWrite32(
                        PeiServices, 
                        This, 
                        Address, 
                        *(UINT32*)Buffer
                    );
                break;
                
            case EfiPeiCpuIoWidthUint64:
            case EfiPeiCpuIoWidthFifoUint64:
            case EfiPeiCpuIoWidthFillUint64: 
                    MemWrite64(
                        PeiServices, 
                        This, 
                        Address, 
                        *(UINT64*)Buffer
                    );
                break;
        }
    }
    
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//============================================================================
// Procedure:   LocalMemRead
//
// Description: This function is called to do multiple reads from the Memory
//      space. It also is capable of doing an read of an I/O space area to a
//      single byte or a serial read into a buffer depending on the Width type
//      being passed in.
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Width - Size of the Memory space that is to be read each time
//      Address - A 64 bit address that points to the location to be read from
//      Count - Number of times to read
//
// Output:  always returns EFI_SUCCESS
//      *Buffer - Data to be read from the Memory space
//
//============================================================================
//<AMI_PHDR_END>

static EFI_STATUS LocalMemRead (
    IN      CONST EFI_PEI_SERVICES            **PeiServices,
    IN      CONST struct _EFI_PEI_CPU_IO_PPI  *This,
    IN      EFI_PEI_CPU_IO_PPI_WIDTH    Width,
    IN      UINT64                      Address,
    IN      UINTN                       Count,
    IN OUT  VOID                        *Buffer
)
{
    UINTN   Idx;
    UINTN   OutputInc, InputInc; // Input and Output counters
    
    CalcAddrChanges(
        Width, 
        &InputInc, 
        &OutputInc
    );
    
    for ( Idx = 0; Idx < Count; Idx++, Address += OutputInc, Buffer = (UINT8*)Buffer + InputInc ) {
    
        switch (Width) {
            case EfiPeiCpuIoWidthUint8:
            case EfiPeiCpuIoWidthFifoUint8:
            case EfiPeiCpuIoWidthFillUint8: 
                    *(UINT8*)Buffer = MemRead8(
                        PeiServices, 
                        This, 
                        Address
                    );
                break;
                
            case EfiPeiCpuIoWidthUint16:
            case EfiPeiCpuIoWidthFifoUint16:
            case EfiPeiCpuIoWidthFillUint16: 
                    *(UINT16*)Buffer = MemRead16(
                        PeiServices, 
                        This, 
                        Address
                    );
                break;
                
            case EfiPeiCpuIoWidthUint32:
            case EfiPeiCpuIoWidthFifoUint32:
            case EfiPeiCpuIoWidthFillUint32: 
                    *(UINT32*)Buffer = MemRead32(
                        PeiServices, 
                        This, 
                        Address
                    );
                break;
                
            case EfiPeiCpuIoWidthUint64:
            case EfiPeiCpuIoWidthFifoUint64:
            case EfiPeiCpuIoWidthFillUint64: 
                    *(UINT64*)Buffer = MemRead64(
                        PeiServices, 
                        This, 
                        Address
                    );
                break;
        }
    }
    
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//============================================================================
// Procedure:   LocalIoRead
//
// Description: This function is called to do multiple reads from the I/O space
//      It also is capable of doing an read of an I/O space area to a single byte
//      or a serial read into a buffer depending on the Width type being passed in.
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Width - Size of the I/O space that is to be read
//      Address - A 64 bit address that points to the location to be read from
//      Count - Number of times to read
//
// Output:  always returns EFI_SUCCESS
//      *Buffer - Data to be read from the I/O space
//
//============================================================================
//<AMI_PHDR_END>

static EFI_STATUS LocalIoRead (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST EFI_PEI_CPU_IO_PPI *This,
    IN EFI_PEI_CPU_IO_PPI_WIDTH Width,
    IN UINT64 Address,
    IN UINTN Count,
    IN OUT VOID *Buffer )
{
    UINTN Idx;
    UINTN OutputInc, InputInc; // Input and Output counters
    
    CalcAddrChanges(
        Width, 
        &InputInc, 
        &OutputInc
    );
    
    for ( Idx = 0; Idx < Count; Idx++, Address += OutputInc, Buffer = (UINT8*)Buffer + InputInc ) {
    
        switch (Width) {
            case EfiPeiCpuIoWidthUint8:
            case EfiPeiCpuIoWidthFifoUint8:
            case EfiPeiCpuIoWidthFillUint8: 
                    *(UINT8*)Buffer = LocalIoRead8(
                        PeiServices, 
                        This, 
                        Address
                    );
                break;
                
            case EfiPeiCpuIoWidthUint16:
            case EfiPeiCpuIoWidthFifoUint16:
            case EfiPeiCpuIoWidthFillUint16:
                    *(UINT16*)Buffer = LocalIoRead16(
                        PeiServices, 
                        This, 
                        Address
                    );
                break;
                
            case EfiPeiCpuIoWidthUint32:
            case EfiPeiCpuIoWidthFifoUint32:
            case EfiPeiCpuIoWidthFillUint32:
                    *(UINT32*)Buffer = LocalIoRead32(
                        PeiServices, 
                        This, 
                        Address
                    );
                break;
                
            case EfiPeiCpuIoWidthUint64:
            case EfiPeiCpuIoWidthFifoUint64:
            case EfiPeiCpuIoWidthFillUint64:
                    *(UINT64*)Buffer = LocalIoRead64(
                        PeiServices, 
                        This, 
                        Address
                    );
                break;
        }
    }
    
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//============================================================================
// Procedure:   LocalIoWrite
//
// Description: This function is called to do multiple writes to the I/O space
//      It also is capable of doing an I/O space fill of a single value or a
//      serial write from a buffer depending on the Width type being passed in.
//
// Input:
//      **PeiServices - pointer to the Pei Services function and data structure
//      *This - Pointer to the instance of the CPU I/O PPI structure
//      Width - Size of the I/O space that is to be written
//      Address - A 64 bit address that points to the location to be read from
//      Count - Number of times to write
//      *Buffer - Data to be written to the I/O space
//
// Output:  always returns EFI_SUCCESS
//
//============================================================================
//<AMI_PHDR_END>

static EFI_STATUS LocalIoWrite (
    IN CONST EFI_PEI_SERVICES **PeiServices,
    IN CONST EFI_PEI_CPU_IO_PPI *This,
    IN EFI_PEI_CPU_IO_PPI_WIDTH Width,
    IN UINT64 Address,
    IN UINTN Count,
    IN OUT VOID *Buffer )
{
    UINTN Idx;
    UINTN OutputInc, InputInc; // Input and Output counters
    
    CalcAddrChanges(
        Width, 
        &InputInc, 
        &OutputInc
    );
    
    for ( Idx = 0; Idx < Count; Idx++, Address += OutputInc, Buffer = (UINT8*)Buffer + InputInc ) {
        switch (Width) {
            case EfiPeiCpuIoWidthUint8:
            case EfiPeiCpuIoWidthFifoUint8:
            case EfiPeiCpuIoWidthFillUint8:
                    LocalIoWrite8(
                        PeiServices, 
                        This, 
                        Address, 
                        *(UINT8*)Buffer
                    );
                break;
                
            case EfiPeiCpuIoWidthUint16:
            case EfiPeiCpuIoWidthFifoUint16:
            case EfiPeiCpuIoWidthFillUint16:
                    LocalIoWrite16(
                        PeiServices, 
                        This, 
                        Address, 
                        *(UINT16*)Buffer
                    );
                break;
                
            case EfiPeiCpuIoWidthUint32:
            case EfiPeiCpuIoWidthFifoUint32:
            case EfiPeiCpuIoWidthFillUint32:
                    LocalIoWrite32(
                        PeiServices, 
                        This, 
                        Address, 
                        *(UINT32*)Buffer
                    );
                break;
                
            case EfiPeiCpuIoWidthUint64:
            case EfiPeiCpuIoWidthFifoUint64:
            case EfiPeiCpuIoWidthFillUint64:
                    LocalIoWrite64(
                        PeiServices, 
                        This, 
                        Address, 
                        *(UINT64*)Buffer
                    );
                break;
        }
    }
    
    return EFI_SUCCESS;
}

//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************