summaryrefslogblamecommitdiffstats
path: root/private/ntos/nthals/halsable/alpha/sbinitnt.c
blob: d05e95631c3a0ea6b4aa15bae91356d85b95471f (plain) (tree)
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
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292











































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                  
/*++

Copyright (c) 1993  Digital Equipment Corporation

Module Name:

    sbinitnt.c

Abstract:


    This module implements the platform-specific initialization for
    a Sable system.

Author:

    Joe Notarangelo  25-Oct-1993
    Steve Jenness    28-Oct-1993 (Sable)

Environment:

    Kernel mode only.

Revision History:

--*/

#include "halp.h"
#include "sablertc.h"
#include "halpcsl.h"
#include "pci.h"
#include "pcip.h"
#include "isaaddr.h"
#include "eisa.h"
#include "iousage.h"
#include "lyintsup.h"
#include "siintsup.h"
#include "xiintsup.h"
#include "stdio.h"

#include "fwcallbk.h"

#include <ntverp.h>

//
// Include the header containing Error Frame Definitions(in halalpha).
//
#include "errframe.h"

//
// Define extern global buffer for the Uncorrectable Error Frame.
// declared in halalpha\inithal.c
//

extern PERROR_FRAME  PUncorrectableError;

//
// Don't change these values unless you know exactly what you are doing
//
#define HAE0_1_REGISTER_VALUE 0x0       // to address SPARSE PCI MEMORY
#define HAE0_2_REGISTER_VALUE 0x0       // to address PCI IO space
#define HAE0_3_REGISTER_VALUE 0x0       // For PCI config cycle type
#define HAE0_4_REGISTER_VALUE 0x0       // to address DENCE PCI Memory

ULONG HalpMemorySlot[4] = { (ULONG)SABLE_MEM0_CSRS_QVA,
                            (ULONG)SABLE_MEM1_CSRS_QVA,
                            (ULONG)SABLE_MEM2_CSRS_QVA,
                            (ULONG)SABLE_MEM3_CSRS_QVA };

ULONG HalpCPUSlot[4] = {    (ULONG)SABLE_CPU0_CSRS_QVA,
                            (ULONG)SABLE_CPU1_CSRS_QVA,
                            (ULONG)SABLE_CPU2_CSRS_QVA,
                            (ULONG)SABLE_CPU3_CSRS_QVA };

//
// Prototypes
//

VOID
HalpInitializeHAERegisters(
    VOID
    );

VOID
HalpSenseCBusSlots(
    VOID
    );

//
// This is the PCI Memory space that cannot be used by anyone
// and therefore the HAL says it is reserved for itself
// Block out 8Mb to 144MB
//

ADDRESS_USAGE
SablePCIMemorySpace = {
    NULL, CmResourceTypeMemory, PCIUsage,
    {
        __8MB,  (__32MB - __8MB),       // Start=8MB; Length=24MB
        0,0
    }
};


//
// Define global data for builtin device interrupt enables.
//

USHORT HalpBuiltinInterruptEnable;

//
// Define global for saving the T2 Chipset's version
//

ULONG T2VersionNumber;

// irql mask and tables
//
//    irql 0 - passive
//    irql 1 - sfw apc level
//    irql 2 - sfw dispatch level
//    irql 3 - device low
//    irql 4 - device high
//    irql 5 - clock
//    irql 6 - real time, ipi, performance counters
//    irql 7 - error, mchk, nmi, halt
//
//
//  IDT mappings:
//  For the built-ins, GetInterruptVector will need more info,
//      or it will have to be built-in to the routines, since
//      these don't match IRQL levels in any meaningful way.
//
//      0 passive       8  perf cntr 1
//      1 apc           9
//      2 dispatch      10 PIC
//      3               11
//      4               12 errors
//      5 clock         13
//      6 perf cntr 0   14 halt
//      7 nmi           15
//
//  This is assuming the following prioritization:
//      nmi
//      halt
//      errors
//      performance counters
//      clock
//      pic

//
// The hardware interrupt pins are used as follows for Sable
//
//  IRQ_H[0] = Hardware Error
//  IRQ_H[1] = PCI
//  IRQ_H[2] = External IO
//  IRQ_H[3] = IPI
//  IRQ_H[4] = Clock
//  IRQ_H[5] = NMI
//

// smjfix - This comment doesn't match what is currently happening in the
//          code.  Plus it isn't clear that EISA and ISA entries can
//          be split apart.
//
// For information purposes:  here is what the IDT division looks like:
//
//      000-015 Built-ins (we only use 8 entries; NT wants 10)
//      016-031 ISA
//      048-063 EISA
//      080-095 PCI
//      112-127 Turbo Channel
//      128-255 unused, as are all other holes
//

//
// Define the bus type, this value allows us to distinguish between
// EISA and ISA systems.
//

// smjfix - Why is this here?  It is a compile time constant for the
//          platform.  This is useful only if one HAL is being used
//          for multiple platforms and is being passed from the firmware
//          or detected.

ULONG HalpBusType = MACHINE_TYPE_EISA;

#if defined(XIO_PASS1) || defined(XIO_PASS2)

//
// Is the external I/O module present?
//

BOOLEAN HalpXioPresent = FALSE;

#endif

//
// Is this a Lynx platform?
//

BOOLEAN HalpLynxPlatform = FALSE;

//
// How many processors are ready to run?
//

ULONG HalpProcessors = 0;

//
// Define global data used to communicate new clock rates to the clock
// interrupt service routine.
//

ULONG HalpCurrentTimeIncrement;
ULONG HalpNextRateSelect;
ULONG HalpNextTimeIncrement;
ULONG HalpNewTimeIncrement;

VOID
HalpClearInterrupts(
     );


BOOLEAN
HalpInitializeInterrupts (
    VOID
    )

/*++

Routine Description:

    This function initializes interrupts for a Sable system.

Arguments:

    None.

Return Value:

    A value of TRUE is returned if the initialization is successfully
    completed. Otherwise a value of FALSE is returned.

--*/

{

    UCHAR DataByte;
    ULONG DataLong;
    ULONG Index;
    ULONG Irq;
    KIRQL Irql;
    PKPRCB Prcb;
    UCHAR Priority;
    ULONG Vector;

    Prcb = PCR->Prcb;

    //
    // Initialize interrupt handling for the primary processor and
    // any system-wide interrupt initialization.
    //

    if( Prcb->Number == SABLE_PRIMARY_PROCESSOR ){

        //
        // Initialize HAL processor parameters based on estimated CPU speed.
        // This must be done before HalpStallExecution is called. Compute integral
        // megahertz first to avoid rounding errors due to imprecise cycle clock
        // period values.
        //

        HalpInitializeProcessorParameters();

        //
        // Connect the Stall interrupt vector to the clock. When the
        // profile count is calculated, we then connect the normal
        // clock.

//jnfix - this is a noop, can we change this
        PCR->InterruptRoutine[CLOCK_VECTOR] = HalpStallInterrupt;

#if !defined(NT_UP)

        //
        // Connect the interprocessor interrupt handler.
        //

        PCR->InterruptRoutine[IPI_VECTOR] = HalpSableIpiInterrupt;

#endif //NT_UP

        //
        // Clear all pending interrupts
        //

        HalpClearInterrupts();

        //
        // Initialize SIO interrupts.
        //

        if( HalpLynxPlatform ){

            HalpInitializeLynxSioInterrupts();

        } else {

            HalpInitializeSableSioInterrupts();

        }

#if defined(XIO_PASS1) || defined(XIO_PASS2)

        //
        // Initialize XIO interrupts.
        //

        if( HalpXioPresent ){

            HalpInitializeXioInterrupts();

        }

#endif

        //
        // Initialize the 21064 interrupts on the current processor
        // before enabling the individual interrupts.
        //

        HalpInitialize21064Interrupts();

        //
        // Enable the interrupts for the clock, ipi, pic, xio, APC, and DPC.
        //
        // jnfix - enable error interrupts later, including correctable

        HalpEnable21064SoftwareInterrupt( Irql = APC_LEVEL );

        HalpEnable21064SoftwareInterrupt( Irql = DISPATCH_LEVEL );

        if( HalpLynxPlatform ){

            PCR->InterruptRoutine[PIC_VECTOR] = HalpLynxSioDispatch;

        } else {

            PCR->InterruptRoutine[PIC_VECTOR] = HalpSableSioDispatch;

        }

        HalEnableSystemInterrupt(PIC_VECTOR, EISA_DEVICE_LEVEL,
            LevelSensitive);

        HalpEnable21064HardwareInterrupt( Irq = 1,
                                          Irql = DEVICE_LEVEL,
                                          Vector = PIC_VECTOR,
                                          Priority = 1 );

#if defined(XIO_PASS1) || defined(XIO_PASS2)

        if( HalpXioPresent && HalpProcessors < 2 ){

            PCR->InterruptRoutine[XIO_VECTOR] = HalpXioDispatch;
            HalEnableSystemInterrupt(XIO_VECTOR, EISA_DEVICE_LEVEL,
                LevelSensitive);

            HalpEnable21064HardwareInterrupt( Irq = 2,
                                              Irql = DEVICE_LEVEL,
                                              Vector = XIO_VECTOR,
                                              Priority = 1 );

        }

#endif

        HalpEnable21064HardwareInterrupt( Irq = 4,
                                          Irql = CLOCK2_LEVEL,
                                          Vector = CLOCK_VECTOR,
                                          Priority = 0 );

#if !defined(NT_UP)

        HalpEnable21064HardwareInterrupt( Irq = 3,
                                          Irql = IPI_LEVEL,
                                          Vector = IPI_VECTOR,
                                          Priority = 0 );

#endif //NT_UP

        //
        // Start the periodic interrupt from the RTC.
        //

        HalpProgramIntervalTimer(MAXIMUM_RATE_SELECT);

        return TRUE;

    } //end if Prcb->Number == SABLE_PRIMARY_PROCESSOR


#if !defined(NT_UP)

    //
    // Initialize interrupts for the current, secondary processor.
    //

    //
    // Connect the clock and ipi interrupt handlers.
    // jnfix - For now these are the only interrupts we will accept on
    // jnfix - secondary processors.  Later we will add PCI interrupts
    // jnfix - and the error interrupts.
    //

    PCR->InterruptRoutine[CLOCK_VECTOR] = HalpSecondaryClockInterrupt;
    PCR->InterruptRoutine[IPI_VECTOR] = HalpSableIpiInterrupt;

    //
    // Initialize the 21064 interrupts for the current processor
    // before enabling the individual interrupts.
    //

    HalpInitialize21064Interrupts();

    //
    // Enable the clock, ipi, APC, and DPC interrupts.
    //

    HalpEnable21064SoftwareInterrupt( Irql = APC_LEVEL );
    HalpEnable21064SoftwareInterrupt( Irql = DISPATCH_LEVEL );

#if defined(XIO_PASS1) || defined(XIO_PASS2)

    if( HalpXioPresent && Prcb->Number == SABLE_SECONDARY_PROCESSOR ){

        PCR->InterruptRoutine[XIO_VECTOR] = HalpXioDispatch;
        HalEnableSystemInterrupt(XIO_VECTOR, EISA_DEVICE_LEVEL,
            LevelSensitive);

        HalpEnable21064HardwareInterrupt( Irq = 2,
                                          Irql = DEVICE_LEVEL,
                                          Vector = XIO_VECTOR,
                                          Priority = 1 );

    }

#endif

    HalpEnable21064HardwareInterrupt( Irq = 4,
                                      Irql = CLOCK2_LEVEL,
                                      Vector = CLOCK_VECTOR,
                                      Priority = 0 );
    HalpEnable21064HardwareInterrupt( Irq = 3,
                                      Irql = IPI_LEVEL,
                                      Vector = IPI_VECTOR,
                                      Priority = 0 );

#endif //NT_UP

    return TRUE;

}


VOID
HalpClearInterrupts(
     )
/*++

Routine Description:

    This function clears all pending interrupts on the Sable.

Arguments:

    None.

Return Value:

    None.

--*/

{
   return;
}


VOID
HalpSetTimeIncrement(
    VOID
    )
/*++

Routine Description:

    This routine is responsible for setting the time increment for Sable
    via a call into the kernel.

Arguments:

    None.

Return Value:

    None.

--*/
{

    //
    // Set the time increment value.
    //

    HalpCurrentTimeIncrement = MAXIMUM_INCREMENT;
    HalpNextTimeIncrement = MAXIMUM_INCREMENT;
    HalpNextRateSelect = 0;
    KeSetTimeIncrement( MAXIMUM_INCREMENT, MINIMUM_INCREMENT );

}

//
// Define global data used to calibrate and stall processor execution.
//

// smjfix - Where is this referenced?

ULONG HalpProfileCountRate;

VOID
HalpInitializeClockInterrupts(
    VOID
    )

/*++

Routine Description:

    This function is called during phase 1 initialization to complete
    the initialization of clock interrupts.  For Sable, this function
    connects the true clock interrupt handler and initializes the values
    required to handle profile interrupts.

Arguments:

    None.

Return Value:

    None.

--*/

{

    //
    // Compute the profile interrupt rate.
    //

    HalpProfileCountRate = ((1000 * 1000 * 10) / KeQueryTimeIncrement());

    //
    // Set the time increment value and connect the real clock interrupt
    // routine.
    //

    PCR->InterruptRoutine[CLOCK2_LEVEL] = HalpClockInterrupt;

    return;
}

VOID
HalpEstablishErrorHandler(
    VOID
    )
/*++

Routine Description:

    This routine performs the initialization necessary for the HAL to
    begin servicing machine checks.

Arguments:

    None.

Return Value:

    None.

--*/
{
    BOOLEAN ReportCorrectables;

    //
    // Connect the machine check handler via the PCR.  The machine check
    // handler for Sable is the default EV4 parity-mode handler.
    //

    PCR->MachineCheckError = HalMachineCheck;

    //
    // Initialize machine check handling for Sable.
    //

    HalpInitializeMachineChecks( ReportCorrectables = FALSE );

    return;
}

VOID
HalpInitializeMachineDependent(
    IN ULONG Phase,
    IN PLOADER_PARAMETER_BLOCK LoaderBlock
    )
/*++

Routine Description:

    This function performs any  Sable-specific initialization based on
    the current phase on initialization.

Arguments:

    Phase - Supplies an indicator for phase of initialization, phase 0 or
            phase 1.

    LoaderBlock - supplies the loader block passed in via the OsLoader.

Return Value:

    None.

--*/
{
    PKPRCB Prcb;
    PRESTART_BLOCK RestartBlock;
    ULONGLONG Value;
    T2_IOCSR Iocsr;

    Prcb = PCR->Prcb;

    if( Prcb->Number == HAL_PRIMARY_PROCESSOR) {

       if( Phase == 0 ){

           //
           // Phase 0 Initialization.
           //

           //
           // Determine how many processors are ready to run.  We use
           // this information to decide whether to split SIO/XIO
           // interrupts across two processors or not.
           //

#ifdef NT_UP

           HalpProcessors = 1;

#else

           HalpProcessors = 0;

           for( RestartBlock = SYSTEM_BLOCK->RestartBlock;
                RestartBlock != NULL;
                RestartBlock = RestartBlock->NextRestartBlock ){

               if( RestartBlock->BootStatus.ProcessorReady ){

                   HalpProcessors++;

               }

           }

#endif

           //
           // Initialize the performance counter.
           //

           HalCalibratePerformanceCounter( NULL );


           //
           // Parse the loader block - this sets global for PCI parity check
           //

           HalpParseLoaderBlock( LoaderBlock );

           //
           // Re-establish Error Handler to pick up PCI parity changes
           //

           HalpEstablishErrorHandler();

           //
           // Determine the T2 Chipset's Version Number. Save Version in Global.
           //
           // T2VersionNumber      Pass
           //
           //   000                 1
           //   001                 2
           //

           Value = READ_T2_REGISTER(&((PT2_CSRS)(T2_CSRS_QVA))->Iocsr);
           Iocsr = *(PT2_IOCSR)&Value;
           T2VersionNumber = Iocsr.T2RevisionNumber;

           HalpInitializeHAERegisters();

           HalpSenseCBusSlots();

       } else {

           //
           // Phase 1 Initialization.
           //

           //
           // Initialize the existing bus handlers.
           //

           HalpRegisterInternalBusHandlers();

           //
           // Initialize the PCI bus.
           //

           HalpInitializePCIBus(LoaderBlock);

           //
           // Initialize profiling for the primary processor.
           //

           HalpInitializeProfiler();

       }

    } else {

        //
        // Connect the machine check handler via the PCR.  The machine check
        // handler for Sable is the default EV4 parity-mode handler.  Note
        // that this was done in HalpEstablishErrorHandler() for the
        // primary processor.
        //

        PCR->MachineCheckError = HalMachineCheck;

        //
        // Initialize profiling on this secondary processor.
        //

        HalpInitializeProfiler();

    }

    return;

}


VOID
HalpStallInterrupt (
    VOID
    )

/*++

Routine Description:

    This function serves as the stall calibration interrupt service
    routine. It is executed in response to system clock interrupts
    during the initialization of the HAL layer.

Arguments:

    None.

Return Value:

    None.

--*/

{
    UCHAR data;
    SABLE_SIC_CSR Sic;

    //
    // Acknowledge the clock interrupt in the real time clock.
    // We don't need to do an ACK to the RTC since we're using the
    // DS1459A RTC's square wave instead of periodic interrupt to
    // generate the periodic clock.  Each processor board has it's own
    // periodic clock interrupt latch that needs to be cleared.
    //
    // This code is MP safe since the Sic is per-processor.

    RtlZeroMemory( &Sic, sizeof(Sic) );

    Sic.IntervalTimerInterruptClear = 1;

    WRITE_CPU_REGISTER( &((PSABLE_CPU_CSRS)(SABLE_CPU0_CSRS_QVA))->Sic,
                       *(PULONGLONG)&Sic );

    return;
}

ULONG
HalSetTimeIncrement (
    IN ULONG DesiredIncrement
    )

/*++

Routine Description:

    This function is called to set the clock interrupt rate to the frequency
    required by the specified time increment value.

Arguments:

    DesiredIncrement - Supplies desired number of 100ns units between clock
        interrupts.

Return Value:

    The actual time increment in 100ns units.

--*/

{
    ULONG NewTimeIncrement;
    ULONG NextRateSelect;
    KIRQL OldIrql;

    //
    // Raise IRQL to the highest level, set the new clock interrupt
    // parameters, lower IRQl, and return the new time increment value.
    //
    KeRaiseIrql(HIGH_LEVEL, &OldIrql);
    if (DesiredIncrement < MINIMUM_INCREMENT) {
        DesiredIncrement = MINIMUM_INCREMENT;
    }
    if (DesiredIncrement > MAXIMUM_INCREMENT) {
        DesiredIncrement = MAXIMUM_INCREMENT;
    }

    //
    // Find the allowed increment that is less than or equal to
    // the desired increment.
    //
    if (DesiredIncrement >= RTC_PERIOD_IN_CLUNKS4) {
        NewTimeIncrement = RTC_PERIOD_IN_CLUNKS4;
        NextRateSelect = RTC_RATE_SELECT4;
    } else if (DesiredIncrement >= RTC_PERIOD_IN_CLUNKS3) {
        NewTimeIncrement = RTC_PERIOD_IN_CLUNKS3;
        NextRateSelect = RTC_RATE_SELECT3;
    } else if (DesiredIncrement >= RTC_PERIOD_IN_CLUNKS2) {
        NewTimeIncrement = RTC_PERIOD_IN_CLUNKS2;
        NextRateSelect = RTC_RATE_SELECT2;
    } else {
        NewTimeIncrement = RTC_PERIOD_IN_CLUNKS1;
        NextRateSelect = RTC_RATE_SELECT1;
    }

    HalpNextRateSelect = NextRateSelect;
    HalpNewTimeIncrement = NewTimeIncrement;

    KeLowerIrql(OldIrql);

    return NewTimeIncrement;
}

//
//
//
VOID
HalpInitializeHAERegisters(
    VOID
    )
/*++

Routine Description:

    This function initializes the HAE registers in the T2 chipset.
    It also register the holes in the PCI memory space if any.

Arguments:

    none

Return Value:

    none

--*/
{
        //
        // Set Hae0_1, Hae0_2, Hae0_3 and Hae0_4 registers
        //
        // IMPORTANT: IF YOU CHANGE THE VALUE OF THE HAE0_1 REGISTERS PLEASE
        //            REMEMBER YOU WILL NEED TO CHANGE:
        //
        //            PCI_MAX_MEMORY_ADDRESS IN halalpha\sable.h
        //            SablePCIMemorySpace   in this file to report holes
        //

        // SPARSE SPACE: Hae0_1
        //
        // We set Hae0_1 to 0MB. Which means we have the following
        // PCI Sparse Memory addresses:
        // 0   to  8MB    VALID. Hae0_1 Not used in address translation
        // 8   to  16MB   Invalid.
        // 16  to  32MB   Invalid. Used for Isa DMA copy for above 16MB
        // 32  to  128MB  VALID. Hae0_1 used in address translation

        //
        // All invalid addresses are reported to be used by the hal.
        // see SablePCIMemorySpace above.
        //

        WRITE_T2_REGISTER( &((PT2_CSRS)(T2_CSRS_QVA))->Hae0_1,
                                                        HAE0_1_REGISTER_VALUE );


        // PCI IO SPACE: Hae0_2
        //
        // We set Hae0_2 to MB. Which means we have the following
        // PCI IO addresses:
        // 0   to  64KB  VALID. Hae0_2 Not used in address translation
        // 64K to  16MB  VALID. Hae0_2 is used in the address translation
        //

        WRITE_T2_REGISTER( &((PT2_CSRS)(T2_CSRS_QVA))->Hae0_2,
                                                        HAE0_2_REGISTER_VALUE );


        if( T2VersionNumber != 0 ) {

            // PCI CONFIG CYCLE TYPE: Hae0_3
            //
            // We default to zero
            //

            WRITE_T2_REGISTER( &((PT2_CSRS)(T2_CSRS_QVA))->Hae0_3,
                                                        HAE0_3_REGISTER_VALUE );

            // PCI DENSE MEMORY: Hae0_4
            //
            // We default to zero
            //

            WRITE_T2_REGISTER( &((PT2_CSRS)(T2_CSRS_QVA))->Hae0_4,
                                                        HAE0_4_REGISTER_VALUE );

        }

#if defined(XIO_PASS1) || defined(XIO_PASS2)

        //
        // If the external I/O module is present, initialize the HAe
        // register on the T4.
        //

        if( HalpXioPresent ){

            //
            // All invalid addresses are reported to be used by the hal.
            // see SablePCIMemorySpace above.
            //

            WRITE_T2_REGISTER( &((PT2_CSRS)(T4_CSRS_QVA))->Hae0_1,
                HAE0_1_REGISTER_VALUE );

            //
            // PCI IO SPACE: Hae0_2
            //
            // We set Hae0_2 to MB. Which means we have the following
            // PCI IO addresses:
            // 0   to  64KB  VALID. Hae0_2 Not used in address translation
            // 64K to  16MB  VALID. Hae0_2 is used in the address translation
            //

            WRITE_T2_REGISTER( &((PT2_CSRS)(T4_CSRS_QVA))->Hae0_2,
                HAE0_2_REGISTER_VALUE );


            //
            // PCI CONFIG CYCLE TYPE: Hae0_3
            //
            // We default to zero
            //

            WRITE_T2_REGISTER( &((PT2_CSRS)(T4_CSRS_QVA))->Hae0_3,
                HAE0_3_REGISTER_VALUE );

            //
            // PCI DENSE MEMORY: Hae0_4
            //
            // We default to zero
            //

            WRITE_T2_REGISTER( &((PT2_CSRS)(T4_CSRS_QVA))->Hae0_4,
                HAE0_4_REGISTER_VALUE );

        }

#endif

        //
        // Report that the SPARSE SPACE mapping to the Io subsystem
        //

        HalpRegisterAddressUsage (&SablePCIMemorySpace);

}

VOID
HalpResetHAERegisters(
    VOID
    )
/*++

Routine Description:

    This function resets the HAE registers in the chipset to 0.
    This is routine called during a shutdown so that the prom
    gets a predictable environment.

Arguments:

    none

Return Value:

    none

--*/
{
    WRITE_T2_REGISTER( &((PT2_CSRS)(T2_CSRS_QVA))->Hae0_1, 0 );
    WRITE_T2_REGISTER( &((PT2_CSRS)(T2_CSRS_QVA))->Hae0_2, 0 );

    if( T2VersionNumber != 0) {

        WRITE_T2_REGISTER( &((PT2_CSRS)(T2_CSRS_QVA))->Hae0_3, 0 );
        WRITE_T2_REGISTER( &((PT2_CSRS)(T2_CSRS_QVA))->Hae0_4, 0 );

    }

    return;
}


VOID
HalpSenseCBusSlots(
    VOID
    )

/*++

Routine Description:

    This function Probes the Cbus looking for slots that are filled.
    are only 8 Cbus locations we can look at (4 memory, 4 CPU).

    If we find a module, we will fill in the table at the 
    appropriate location -- if no module is found, then place a
    zero there. 

Note:

    This routine will machine check in an empty slot -- and is expected.

Arguments:

    none

Return Value:

    none

--*/
{

    ULONG i;
    ULONG CSR;

    for (i=0; i<4; i++) {
        if (HalpMemorySlot[i] != 0) {
            CSR = (ULONG)READ_MEM_REGISTER((PVOID)HalpMemorySlot[i]);
            if (CSR == 0xffffffff) {
                HalpMemorySlot[i] = 0;
            }
#if HALDBG
            if (HalpMemorySlot[i] == 0) {
                DbgPrint("Memory Slot %d is EMPTY\n", i);
            } else {
                DbgPrint("Memory Slot %d is POPULATED\n", i);
            }
#endif
        }
    }

    for (i=0; i<4; i++) {
        if (HalpCPUSlot[i] != 0) {
            CSR = (ULONG)READ_CPU_REGISTER((PVOID)HalpCPUSlot[i]);
            if (CSR == 0xffffffff) {
                HalpCPUSlot[i] = 0;
            }
#if HALDBG
            if (HalpCPUSlot[i] == 0) {
                DbgPrint("CPU Slot %d is EMPTY\n", i);
            } else {
                DbgPrint("CPU Slot %d is POPULATED\n", i);
            }
#endif
        }
    }
}



VOID
HalpGetMachineDependentErrorFrameSizes(
    PULONG          RawProcessorSize,
    PULONG          RawSystemInfoSize
    )
/*++

Routine Description:

    This function returns the size of the system specific structures.


Arguments:

    RawProcessorSize  - Pointer to a buffer that will receive the
            size of the processor specific error information buffer.

    RawSystemInfoSize - Pointer to a buffer that will receive the
            size of the system specific error information buffer.

Return Value:

    none

--*/
{
    *RawProcessorSize = sizeof(PROCESSOR_EV4_UNCORRECTABLE);
    *RawSystemInfoSize = sizeof(SABLE_UNCORRECTABLE_FRAME);
    return;
}

VOID
HalpGetSystemInfo(SYSTEM_INFORMATION *SystemInfo)
/*++

Routine Description:

    This function fills in the System information.


Arguments:

    SystemInfo - Pointer to the SYSTEM_INFORMATION buffer that needs
                to be filled in.

Return Value:

    none

--*/
{
    char systemtype[] = "Sable";
    EXTENDED_SYSTEM_INFORMATION  FwExtSysInfo;


    VenReturnExtendedSystemInformation(&FwExtSysInfo);

    RtlCopyMemory(SystemInfo->FirmwareRevisionId,
                    FwExtSysInfo.FirmwareVersion,
                    16);


    RtlCopyMemory(SystemInfo->SystemType,systemtype, 8);

    SystemInfo->ClockSpeed =
        ((1000 * 1000) + (PCR->CycleClockPeriod >> 1)) / PCR->CycleClockPeriod;

    SystemInfo->SystemRevision = PCR->SystemRevision;

    RtlCopyMemory(SystemInfo->SystemSerialNumber,
                    PCR->SystemSerialNumber,
                    16);

    SystemInfo->SystemVariant =  PCR->SystemVariant;


    SystemInfo->PalMajorVersion = PCR->PalMajorVersion;
    SystemInfo->PalMinorVersion = PCR->PalMinorVersion;

    SystemInfo->OsRevisionId = VER_PRODUCTBUILD;

    //
    // For now fill in dummy values.
    //
    SystemInfo->ModuleVariant = 1UL;
    SystemInfo->ModuleRevision = 1UL;
    SystemInfo->ModuleSerialNumber = 0;

    return;
}


VOID
HalpInitializeUncorrectableErrorFrame (
    VOID
    )
/*++

Routine Description:

    This function Allocates an Uncorrectable Error frame for this
    system and initializes the frame with certain constant/global
    values.

    This is routine called during machine dependent system
    Initialization.

Arguments:

    none

Return Value:

    none

--*/
{
    PROCESSOR_EV4_UNCORRECTABLE  processorFrame;
    SABLE_UNCORRECTABLE_FRAME   SableUnCorrrectable;

    //
    // If the Uncorrectable error buffer is not set then simply return
    //
    if(PUncorrectableError == NULL)
        return;

    PUncorrectableError->Signature = ERROR_FRAME_SIGNATURE;

    PUncorrectableError->FrameType = UncorrectableFrame;

    //
    // ERROR_FRAME_VERSION is define in errframe.h and will
    // change as and when there is a change in the errframe.h.
    // This Version number helps the service, that reads this
    // information from the dumpfile, to check if it knows about
    // this frmae version type to decode.  If it doesn't know, it
    // will dump the entire frame to the EventLog with a message
    // "Error Frame Version Mismatch".
    //

    PUncorrectableError->VersionNumber = ERROR_FRAME_VERSION;

    //
    // The sequence number will always be 1 for Uncorrectable errors.
    //

    PUncorrectableError->SequenceNumber = 1;

    //
    // The PerformanceCounterValue field is not used for Uncorrectable
    // errors.
    //

    PUncorrectableError->PerformanceCounterValue = 0;

    //
    // We will fill in the UncorrectableFrame.SystemInfo here.
    //
    HalpGetSystemInfo(&PUncorrectableError->UncorrectableFrame.System);

    PUncorrectableError->UncorrectableFrame.Flags.SystemInformationValid = 1;

    return;
}