summaryrefslogtreecommitdiffstats
path: root/private/ntos/nthals/halcbus/i386/cbus.c
blob: d581e2a840073d9be4593376b1a0b1541c4d917e (plain) (blame)
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
/*++

Copyright (c) 1992, 1993, 1994  Corollary Inc.

Module Name:

    cbus.c

Abstract:

    This module implements the initialization of the system dependent
    functions that define the Hardware Architecture Layer (HAL) for the
    MP Corollary machines under Windows NT.

    This includes the Corollary C-bus II machines which use Corollary's
    CBC chips as well as Corollary C-bus I machines which use the Intel APIC.
    Hardware dependencies of each C-bus backend are isolated in their
    independent hardware modules.  This module is completely hardware
    independent.

Author:

    Landy Wang (landy@corollary.com) 26-Mar-1992

Environment:

    Kernel mode only.

Revision History:

--*/

#include "halp.h"
#include "cbus.h"               // Cbus1 & Cbus2 max number of elements is here
#include "cbusrrd.h"            // HAL <-> RRD interface definitions
#include "cbus_nt.h"            // C-bus NT-specific implementation stuff
#include "cbusnls.h"


PVOID
HalpRemapVirtualAddress(IN PVOID, IN PVOID, IN BOOLEAN);

BOOLEAN
CbusMPMachine(VOID);

PUCHAR
CbusFindString (
IN PUCHAR       Str,
IN PUCHAR       StartAddr,
IN LONG         Len
);

ULONG
CbusStringLength (
IN PUCHAR       Str
);

ULONG
CbusReadExtIDs(
IN PEXT_ID_INFO From,
IN PEXT_ID_INFO To
);

PVOID
CbusMappings(
IN ULONG                Processor,
IN PEXT_ID_INFO         Idp
);

VOID
CbusMapMemoryRegisters(
IN PEXT_ID_INFO Idp
);

VOID
CbusEstablishMaps(
IN PEXT_ID_INFO Table,
IN ULONG Count
);

VOID
CbusReadRRD(VOID);

VOID
CbusCheckBusRanges(VOID);

VOID
CbusAddMemoryHoles(VOID);

VOID
CbusInitializeOtherPciBus(VOID);

VOID
HalInitializeProcessor(
IN ULONG Processor
);

#ifdef ALLOC_PRAGMA
#pragma alloc_text(INIT, CbusStringLength)
#pragma alloc_text(INIT, CbusFindString)
#pragma alloc_text(INIT, CbusReadExtIDs)
#pragma alloc_text(INIT, CbusMappings)
#pragma alloc_text(INIT, CbusMapMemoryRegisters)
#pragma alloc_text(INIT, CbusEstablishMaps)
#pragma alloc_text(INIT, CbusReadRRD)
#pragma alloc_text(PAGE, HalInitializeProcessor)
#endif

#define MIN(a,b)                (((a)>(b))?(b):(a))

EXT_CFG_OVERRIDE_T              CbusGlobal;

ULONG                           CbusProcessors;
ULONG                           CbusProcessorMask;
ULONG                           CbusBootedProcessors;
ULONG                           CbusBootedProcessorsMask;

ULONG                           CbusTemp;

//
// 8254 spinlock.  This must be acquired before touching the 8254 chip.
//

ULONG                           Halp8254Lock;

ULONG                           HalpDefaultInterruptAffinity;

extern ULONG			CbusVectorToIrql[MAXIMUM_IDTVECTOR + 1];

PULONG                          CbusTimeStamp;

//
// For Cbus1, the CbusCSR[] & CbusBroadcastCSR really point at
// Cbus1 I/O space which can vary from platform to platform.
//
// For Cbus2, the CbusCSR[] & CbusBroadcastCSR really do point at
// the Cbus2 CSR for the particular element.
//
PVOID                           CbusBroadcastCSR;

//
// Cbus information table for all elements (an element may not
// necessarily contain an x86 processor; ie: it may be a pure
// I/O element).
//
ELEMENT_T                       CbusCSR[MAX_CBUS_ELEMENTS];


MEMORY_CARD_T                   CbusMemoryBoards[MAX_ELEMENT_CSRS];
ULONG                           CbusMemoryBoardIndex;

//
// hardcoded size for now - see cbus.inc for the register definition
// and layout of CbusRebootRegs[].
//
ULONG                           CbusRebootRegs[8];

RRD_CONFIGURATION_T             CbusJumpers;

EXT_ID_INFO_T                   CbusExtIDTable[MAX_CBUS_ELEMENTS];
ULONG                           CbusValidIDs;

ULONG                           CbusVectorToHwmap[MAXIMUM_IDTVECTOR + 1];

//
// Declare the task priority system vectors which vary from APIC to CBC.
// About the only ones that remain constant are high, low and APC & DPC.
// This is primarily due to shortcomings and errata in the APIC.
//

ULONG                           ProfileVector;
ULONG                           CbusIpiVector;
ULONG                           CbusClockVector;
ULONG                           CbusRedirVector;
ULONG                           CbusRebootVector;

//
// Declare these two pointers here for speed - it eliminates an
// extra asm instruction each time they are called.
//

VOID            (*CbusRequestIPI)(IN ULONG);
VOID            (*CbusRequestSoftwareInterrupt) ( IN KIRQL);
LARGE_INTEGER   (*CbusQueryPerformanceCounter) ( IN OUT PLARGE_INTEGER);

ADDRESS_USAGE                   HalpCbusMemoryHole = {
                                    NULL, CmResourceTypeMemory, InternalUsage,
                                    {
                                        0, 0,
                                        0, 0,
                                        0, 0,
                                        0, 0,

                                        0, 0,
                                        0, 0,
                                        0, 0,
                                        0, 0,

                                        0, 0,
                                        0, 0,
                                        0, 0,
                                        0, 0,

                                        0, 0,
                                        0, 0,
                                        0, 0,
                                        0, 0

                                    }
};

//
// this structure differs from the one above in that it only contains
// memory ranges that we want reserved for the HAL and ensure that
// devices do not get dynamically assigned memory from these ranges.
// specifically, the table above will include more ranges that the
// BIOS E820 function will remove, but that need to remain available
// for resources on the secondary peer PCI bus for C-bus II.
//
ADDRESS_USAGE                   HalpCbusMemoryResource = {
                                    NULL, CmResourceTypeMemory, InternalUsage,
                                    {
                                        0, 0,
                                        0, 0,
                                        0, 0,
                                        0, 0,

                                        0, 0,
                                        0, 0,
                                        0, 0,
                                        0, 0,

                                        0, 0,
                                        0, 0,
                                        0, 0,
                                        0, 0,

                                        0, 0,
                                        0, 0,
                                        0, 0,
                                        0, 0

                                    }
};

ULONG                           CbusMemoryHoleIndex;
ULONG                           CbusMemoryResourceIndex;


ULONG
CbusStringLength (
IN PUCHAR       Str
)

/*++

Routine Description:

    Return the length of the input NULL-terminated Ansi string, including
    the NULL terminator at the end.

Arguments:

    Str - Supplies a pointer to the string

Return Value:

    Length of the string in bytes

--*/

{
        ULONG    n;

        for (n = 0; Str[n]; ++n)
                ;

        return ++n;
}


PUCHAR
CbusFindString (
IN PUCHAR       Str,
IN PUCHAR       StartAddr,
IN LONG         Len
)

/*++

Routine Description:

    Searches a given virtual address for the specified string
    up to the specified length.

Arguments:

    Str - Supplies a pointer to the string

    StartAddr - Supplies a pointer to memory to be searched

    Len - Maximum length for the search

Return Value:

    Pointer to the string if found, 0 if not.

--*/

{
        LONG    Index, n;

        for (n = 0; Str[n]; ++n)
                ;

        if (--n < 0) {
                return StartAddr;
        }

        for (Len -= n; Len > 0; --Len, ++StartAddr) {
                if ((StartAddr[0] == Str[0]) && (StartAddr[n] == Str[n])) {
                        for (Index = 1; Index < n; ++Index)
                                if (StartAddr[Index] != Str[Index])
                                        break;
                        if (Index >= n) {
                                return StartAddr;
                        }
                }
        }

        return (PUCHAR)0;
}

ULONG
CbusReadExtIDs(
IN PEXT_ID_INFO From,
IN PEXT_ID_INFO To
)

/*++

Routine Description:

    Read in the C-bus II extended id information table.

Arguments:

    From - Supplies a pointer to the RRD source table

    To - Supplies a pointer to the destination storage for the table

Return Value:

    Number of valid table entries.

--*/

{
        ULONG Index = 0;
        ULONG ValidEntries = 0;

        for ( ; Index < MAX_CBUS_ELEMENTS && From->id != LAST_EXT_ID; Index++) {

                //
                // we cannot skip blank RRD entries
                //
                // if (From->pm == 0 && From->io_function == IOF_INVALID_ENTRY)
                //                      continue;

                RtlMoveMemory((PVOID)To, (PVOID)From, sizeof(EXT_ID_INFO_T));

                From++;
                To++;
                ValidEntries++;
        }

        //
        //  WARNING: this is not necessarily the number of valid CPUs !!!
        //
        return ValidEntries;
}

PVOID
CbusMappings(
IN ULONG                Processor,
IN PEXT_ID_INFO         Idp
)
/*++

Routine Description:

    Map a given processor's CSR space and save an idp pointer as well.

Arguments:

    Processor - Supplies a logical processor number

    Idp - Supplies an RRD extended ID pointer for this processor element

Return Value:

    Opaque pointer to this processor's CSR space.c

--*/
{
        //
        // RRD specifies how much to map in per processor - this
        // will usually be just 8K of the 64K CSR space for Cbus2.
        // For Cbus1, RRD must give us a size which includes any
        // processor-specific registers the HAL may access,
        // generally indicated via the CbusGlobal structure.
        //
        CbusCSR[Processor].csr = HalpMapPhysicalMemoryWriteThrough (
                (PVOID)Idp->pel_start,
                (ULONG)ADDRESS_AND_SIZE_TO_SPAN_PAGES(
                        Idp->pel_start, Idp->pel_size));

        CbusCSR[Processor].idp = (PVOID)Idp;

        return CbusCSR[Processor].csr;
}

VOID
CbusMapMemoryRegisters(
IN PEXT_ID_INFO Idp
)
/*++

Routine Description:

    Maps a given RRD entry into the HAL's memory board structures.
    This is used later to determine ECC error addresses, etc.

Arguments:

    Idp - Supplies a pointer to the RRD extended information structure entry

Return Value:

    None.

--*/
{
        PMEMORY_CARD    pm;

        pm = &CbusMemoryBoards[CbusMemoryBoardIndex];

        pm->physical_start = Idp->pel_start;
        pm->physical_size = Idp->pel_size;
        pm->io_attr = (ULONG)Idp->io_attr;

        //
        // map in the csr space for this memory card
        //
        pm->regmap = HalpMapPhysicalMemoryWriteThrough (
                                (PVOID)Idp->io_start,
                                (ULONG)ADDRESS_AND_SIZE_TO_SPAN_PAGES(
                                        Idp->io_start, Idp->io_size));

        CbusMemoryBoardIndex++;
}

VOID
CbusEstablishMaps(
IN PEXT_ID_INFO Table,
IN ULONG Count
)
/*++

Routine Description:

    Parse the given RRD extended ID configuration table, and construct
    various HAL data structures accordingly.

Arguments:

    Table - Supplies a pointer to the RRD extended information table

    Count - Supplies a count of the maximum number of entries.

Return Value:

    None.

--*/
{
        ULONG                   Index, processor = 0;
        ULONG                   HighVector;
        ULONG                   Length;
        PEXT_ID_INFO            Idp = Table;
        PUCHAR                  csr;
        extern VOID             CbusMemoryFree(ULONG, ULONG);
        extern VOID             CbusIOPresent(ULONG, ULONG, ULONG, ULONG, ULONG, PVOID);
        extern ULONG            IxProfileVector;

        for (Index = 0; Index < Count; Index++, Idp++) {

                //
                //  Map in the broadcast CSR.  Note this is not a processor.
                //

                if (Idp->id == CbusGlobal.broadcast_id) {
                        CbusBroadcastCSR = HalpMapPhysicalMemoryWriteThrough (
                                (PVOID)Idp->pel_start,
                                (ULONG)ADDRESS_AND_SIZE_TO_SPAN_PAGES(
                                        Idp->pel_start, Idp->pel_size));
                        //
                        // Register the broadcast element's memory
                        // mapped I/O space
                        //
                        continue;
                }


                //
                //  Establish virtual maps for each processor
                //

                if (Idp->pm) {

                        if ((UCHAR)Idp->id == CbusGlobal.bootid) {
                                CbusMappings(0, Idp);
                        }
                        else {
        
                                //
                                // We have an additional processor - set up
                                // his maps and put him in reset.  We will
                                // boot him shortly.
                                //
                
                                processor++;

                                csr = (PUCHAR)CbusMappings(processor, Idp);
                
                                csr += CbusGlobal.smp_sreset;
        
                                *((PULONG)csr) = CbusGlobal.smp_sreset_val;
                        }
                }

                //
                //  Establish virtual maps for each I/O and/or
                //  memory board.  Note that I/O devices may or may
                //  not have an attached processor - a CPU is NOT required!
                //  memory, on the other hand, may NOT have a processor
                //  on the same board.
                //

                switch (Idp->io_function) {
                        case IOF_INVALID_ENTRY:
                        case IOF_NO_IO:
                                break;

                        case IOF_MEMORY:
                                //
                                // If not a processor, must be a memory card.
                                // Add this memory card to our list
                                // of memory cards in the machine.
                                //
                                
                                if (Idp->pm)
                                        break;

                                CbusMapMemoryRegisters(Idp);
        
                                //
                                // Add this memory range to our list
                                // of additional memory to free later.
                                //
        
                                CbusMemoryFree(Idp->pel_start, Idp->pel_size);
                                break;
                                
                        default:
                                //
                                // Add this I/O functionality to our table
                                // to make available to Cbus hardware drivers.
                                // Since I/O card interpretation of the RRD
                                // table is strictly up to the driver, do not
                                // try to register any of this element's space
                                // in the HAL's public consumption list, as we
                                // do for memory and processor cards.
                                //
        
                                if (Idp->pel_features & ELEMENT_HAS_IO) {

                                        //
                                        // If there was an attached processor,
                                        // we already mapped in the CSR.  If
                                        // no attached processor, map in the
                                        // CSR now.  We'll need it later for
                                        // interrupt vector enabling.
                                        //
        
                                        if (Idp->pm == 0) {
                                                csr = HalpMapPhysicalMemoryWriteThrough (
                                                        (PVOID)Idp->pel_start,
                                                        (ULONG)ADDRESS_AND_SIZE_TO_SPAN_PAGES(
                                                                Idp->pel_start, Idp->pel_size));
                                        }

                                        CbusIOPresent(
                                                (ULONG)Idp->id,
                                                (ULONG)Idp->io_function,
                                                (ULONG)Idp->io_attr,
                                                Idp->pel_start,
                                                Idp->pel_size,
                                                (PVOID)csr );
                                }
                                break;
                }
        }

        //
        // Ensure that the memory subsystem does not use areas mapped out by
        // e820 in determining the system memory ranges.
        //
        Length = 0xffffffff - CbusGlobal.cbusio;
        AddMemoryHole(CbusGlobal.cbusio, Length + 1);
        if (CbusBackend->AddMemoryHoles)
                (*CbusBackend->AddMemoryHoles)();
        HalpRegisterAddressUsage (&HalpCbusMemoryResource);

        //
        // Set total number of processors and their global mask
        //

        CbusProcessors = processor + 1;
        CbusProcessorMask = (1 << CbusProcessors) - 1;

        //
        // Initialize the platform data - done only ONCE.
        // Backends are expected to initialize the global Cbus
        // spurious interrupt vector and the irqltovec[] table.
        // We then pull the dispatch, wake and profile vectors
        // from the table.
        //

        (*CbusBackend->InitializePlatform)();

        CbusRequestIPI = CbusBackend->HalRequestInterrupt;
        CbusRequestSoftwareInterrupt = CbusBackend->HalRequestSoftwareInterrupt;
        CbusQueryPerformanceCounter = CbusBackend->HalQueryPerformanceCounter;

        ProfileVector   = CbusIrqlToVector[PROFILE_LEVEL];
        CbusClockVector = CbusIrqlToVector[CLOCK2_LEVEL];
        CbusIpiVector   = CbusIrqlToVector[IPI_LEVEL];

        HighVector = CbusIrqlToVector[HIGH_LEVEL];
	CbusVectorToIrql[HighVector] = HIGH_LEVEL;

        //
        // Initialize the standard IxProfileVector so that we can
        // reuse the standard profile code.
        //

        IxProfileVector = ProfileVector;
}

VOID
HalpResetAllProcessors(VOID)
/*++

Routine Description:

    Called to put all the other processors in reset prior to reboot for
    the Corollary architectures.  Highly architecture specific.

Arguments:

    None.

Return Value:

    None.

--*/
{
        ULONG Processor;

        Processor = KeGetPcr()->HalReserved[PCR_PROCESSOR];

        (*CbusBackend->ResetAllOtherProcessors)(Processor);
}

UCHAR ObsoleteMachine[] = MSG_OBSOLETE;

VOID
FatalError(
IN PUCHAR ErrorString
)
/*++

Routine Description:

    Called to halt the HAL due to a fatal error, printing out a
    string describing the cause of the failure.

Arguments:

    ErrorString - Supplies a pointer to failure message

Return Value:

    None.

--*/

{

        HalDisplayString(ErrorString);
        HalDisplayString(MSG_HALT);

        while (1)
            ;
}

static ULONG RRDextsignature[] = { 0xfeedbeef, 0 };

static ULONG RRDsignature[] = { 0xdeadbeef, 0 };

static UCHAR CorollaryOwns[] = "Copyright(C) Corollary, Inc. 1991. All Rights Reserved";

VOID
CbusReadRRD(VOID)

/*++

    Routine Description:
    
    For robustness, we check for the following before concluding that
    we are indeed a Corollary C-bus I or C-bus II licensee supported
    in multiprocessor mode under this HAL:
    
    a) Corollary string in the BIOS ROM 64K area                (0x000F0000)
    b) Corollary string in the RRD RAM/ROM 64K area             (0xFFFE0000)
    c) 2 Corollary extended configuration tables
                 in the RRD RAM/ROM 64K area                    (0xFFFE0000)
    
    If any of the above checks fail, it is assumed that this machine
    is either a non-Corollary machine or an early Corollary machine
    not supported by this HAL.  Both of these types of machines are,
    however, supported (in uniprocessor mode) by the standard
    uniprocessor HAL.

    If the above checks succeed, then we proceed to fill in various
    configuration structures for later use.

Arguments:

    None.

Return Value:

    None.

--*/
{
        ULONG                   Index;
        PEXT_CFG_HEADER         p;
        PVOID                   s;
        ULONG                   OverrideLength = 0, EntryLength;
        PUCHAR                  Bios;

        //
        // Map in the 64K (== 0x10 pages) of BIOS ROM @ 0xF0000
        // and scan it for our signature...
        //
        
        Bios = (PUCHAR)HalpMapPhysicalMemory ((PVOID)0xF0000, 0x10);

        if (!CbusFindString((PUCHAR)"Corollary", Bios, (LONG)0x10000))
                KeBugCheck (MISMATCHED_HAL);

        //
        // Map in the 64K (== 0x10 pages) of RRD @ 0xFFFE0000 and
        // scan it for our signature.  (Note we are taking advantage
        // of the fact that the lengths are the same, thus reusing
        // the above PTEs, as opposed to allocating new ones).
        //
        
        for (Index = 0; Index < 0x10; Index++) {
                HalpRemapVirtualAddress(
                    Bios + (Index << PAGE_SHIFT),
                    (PVOID)(0xFFFE0000 + (Index << PAGE_SHIFT)),
                    FALSE);
        }

        if (!CbusFindString((PUCHAR)"Corollary", Bios, (LONG)0x10000))
                KeBugCheck (MISMATCHED_HAL);

        //
        // Map in the 32K (== 8 pages) of RRD RAM information @ 0xE0000,
        // again reusing previously gained PTEs.  Note we are only reusing
        // the low half this time.
        //
        
        for (Index = 0; Index < 8; Index++) {
                HalpRemapVirtualAddress(
                    Bios + (Index << PAGE_SHIFT),
                    (PVOID)(RRD_RAM + (Index << PAGE_SHIFT)),
                    FALSE);
        }

        if (!CbusFindString((PUCHAR)CorollaryOwns, Bios, (LONG)0x8000))
                FatalError(ObsoleteMachine);

        //
        // At this point, we are assured that it is indeed a
        // Corollary architecture machine.  Search for our
        // extended configuration tables, note we still search for
        // the existence of our earliest 'configuration' structure,
        // ie: the 0xdeadbeef version.  This is not to find out where
        // the memory is, but to find out which Cbus1 megabytes have been
        // 'jumpered' so that I/O cards can use the RAM address(es) for
        // their own dual-ported RAM buffers.  This early configuration
        // structure will not be present in Cbus2.
        //
        // If there is no extended configuration structure,
        // this must be an old rom.  NO SUPPORT FOR THESE.
        //

        s = (PVOID)CbusFindString((PUCHAR)RRDsignature, Bios,
                                                (LONG)0x8000);

        if (s) {
                RtlMoveMemory((PVOID)&CbusJumpers, (PVOID)s, JUMPER_SIZE);
        }
#if DBG
        else {
                //
                // RRD configuration is not expected on Cbus2, but is for Cbus1
                //
                HalDisplayString("HAL: No RRD ROM configuration table\n");
        }
#endif

        //
        // Now go for the extended configuration structure which will tell
        // us about memory, processors and I/O devices.
        //
        
        p = (PEXT_CFG_HEADER)CbusFindString((PUCHAR)RRDextsignature,
                                         Bios, (LONG)0x8000);

        if (!p) {
#if DBG
                HalDisplayString("HAL: No extended configuration table\n");
#endif
                FatalError(ObsoleteMachine);
        }

        //
        // Read in the 'extended ID information' table which,
        // among other things, will give us the processor
        // configuration.
        //
        // Multiple structures are strung together with a "checkword",
        // "length", and "data" structure.  The first null "checkword"
        // entry marks the end of the extended configuration
        // structure.
        //
        // We are only actively reading two types of structures, and
        // they MUST be in the following order, although not necessarily
        // consecutive:
        //
        //      - ext_id_info
        //
        //      - ext_cfg_override
        //
        // We ignore all other extended configuration entries built
        // by RRD - they are mainly for early UNIX kernels.
        //
                
        do {
                EntryLength = p->ext_cfg_length;

                switch (p->ext_cfg_checkword) {

                case EXT_ID_INFO:

                        CbusValidIDs = CbusReadExtIDs((PEXT_ID_INFO)(p+1),
                                                (PEXT_ID_INFO)CbusExtIDTable);

                        break;

                case EXT_CFG_OVERRIDE:
                        //
                        // We just copy the size of the structures
                        // we know about.  If an rrd tries to pass us
                        // more than we know about, we ignore the
                        // overflow.  Underflow is interpreted as
                        // "this must be a pre-XM machine", and such
                        // machines must default to the standard Windows NT
                        // uniprocessor HAL.
                        //

                        if (EntryLength < sizeof(EXT_CFG_OVERRIDE_T)) {
                                FatalError(MSG_RRD_ERROR);
                        }
                        
                        OverrideLength = MIN(sizeof(EXT_CFG_OVERRIDE_T),
                                         EntryLength);

                        RtlMoveMemory((PVOID)&CbusGlobal,
                                (PVOID)(p + 1), OverrideLength);

                        break;

                case EXT_CFG_END:

                        //
                        // If ancient C-bus box, it's not supported in MP mode
                        //
                        if (CbusValidIDs == 0 || OverrideLength == 0) {
#if DBG
                                HalDisplayString("HAL: Missing RRD tables\n");
#endif
                                FatalError(ObsoleteMachine);
                        }

                        if (CbusMPMachine() == FALSE) {
#if DBG
                                HalDisplayString("HAL: This Corollary machine is not supported under this HAL\n");
#endif
                                FatalError(ObsoleteMachine);
                        }

                        (*CbusBackend->ParseRRD)((PVOID)CbusExtIDTable,
                                        &CbusValidIDs);

        
                        CbusEstablishMaps(CbusExtIDTable, CbusValidIDs);

                        return;

                default:
                        //
                        // Skip unused or unrecognized configuration entries
                        //
                        
                        break;
                }
                
                //
                // Get past the header, add in the length and then
                // we're at the next entry.
                //
                p = (PEXT_CFG_HEADER) ((PUCHAR)(p + 1) + EntryLength);

        } while (1);

        // never reached
}

VOID
CbusCheckBusRanges(VOID)
/*++

Routine Description:

    Check all buses and determine SystemBase
    for all ranges within all buses.

Arguments:

    None.

Return Value:

    None.

--*/

{
        if (CbusBackend->CheckBusRanges) {
            (*CbusBackend->CheckBusRanges)();
        }
}

VOID
CbusAddMemoryHoles(VOID)
/*++

Routine Description:

    Find the holes in the C-bus memory space that is not
    useable for device allocation.

Arguments:

    None.

Return Value:

    None.

--*/

{
        if (CbusBackend->AddMemoryHoles) {
            (*CbusBackend->AddMemoryHoles)();
        }
}

VOID
CbusInitializeOtherPciBus(VOID)
/*++

Routine Description:

    Find and initialize other PCI system buses.

Arguments:

    None.

Return Value:

    None.

--*/

{
        if (CbusBackend->InitializeOtherPciBus) {
            (*CbusBackend->InitializeOtherPciBus)();
        }
}

VOID
HalpDisableAllInterrupts (VOID)
/*++

Routine Description:
    This routine is called during a system crash.  The Hal needs all
    interrupts disabled.  Interrupts will NOT be enabled upon leaving
    this routine, nor is it allowed to turn them back on later.  this
    is a one-time thing, done as the system is coming down.

    Disables all incoming interrupts for the calling processor.

Arguments:

    None.

Return Value:

    None - all interrupts are masked off

--*/
{
        KfRaiseIrql(HIGH_LEVEL);
}


/*++

Routine Description:

    Called by each processor in turn, to initialize himself.

    - This is the earliest point at which the HAL gets control of
      the system on each processor.

    - The boot CPU runs it early on in kernel startup.  Much later,
      each additional CPU will also run it shortly after they are
      brought out of reset during Phase 1.

    - When called by the boot processor, this routine also reads in
      the global RRD information which pertains to the entire system,
      including all the processors.

    - Later, the boot cpu will run HalInitSystem --> HalpInitMP.
      This all occurs at Phase 0.

      Much later, the Phase1 thread runs on the boot cpu, and calls
      HalInitSystem --> HalpInitMP again, this time at Phase 1.
      Then the boot cpu runs KeStartAllProcessors.  this serially
      invokes HalStartNextProcessor for each of our additional cpus
      to start them running KiSystemStartup.
     
      As each additional processor runs KiSystemStartup, he then
      runs HalInitializeProcessor.  This is when we will enable the
      additional processor's incoming IPIs.  After that, he will proceed
      to KiInitializeKernel & ExpInitializeExecutive, who then calls
      HalInitSystem.  Each additional processor is always running
      at Phase 1, never Phase 0.

    The above dictates the actions of these routines:

    - HalInitializeProcessor will read in (ONCE only, ie: only the
      boot cpu will do this) all of the element space and set up
      _global_ maps for each processor's CSR.  each CPU will map
      his own _local_ CSR into his HAL pcr.  each CPU will
      enable his incoming IPI here, and disable all other interrupts,
      including all of this CPU's half-card CBC I/O interrupts.

    - It would be nice to move some of the HalInitializeProcessor Phase0
      code to HalInitSystem Phase0, but we need full mappings, etc,
      for entering the debugger from KiSystemStartup early on.

    - KeReadir/LowerIrq's must be available once this function
      returns.  (IPI's are only used once two or more processors are
      available)

Arguments:

    Processor - Supplies a logical processor number

Return Value:

    None.

--*/
VOID
HalInitializeProcessor(
IN ULONG Processor
)
{
        extern VOID             i486cacheon(VOID);
        extern VOID             HalpInitializeCoreIntrs(VOID);
        extern VOID             CbusDefaultStall(VOID);
        extern VOID             HalpIpiHandler( VOID );
        extern PULONG           CbusVectorToEoi[MAXIMUM_IDTVECTOR + 1];
        extern KSPIN_LOCK       CbusVectorLock;
        ULONG                   i;
        ELEMENT_T               csr;
        PEXT_ID_INFO            Idp;
        extern KAFFINITY        HalpActiveProcessors;

        if (Processor == 0) {

                //
                // Find our signature and configuration information
                //
                CbusReadRRD();

                //
                // CbusVectorLock needs to be initialized before
                // any CBC interrupt vectors are given out via
                // HalGetInterruptVector().
                //
                KeInitializeSpinLock(&CbusVectorLock);

                //
                // Initialize the Eoi addresses to point at a known don't-care.
                // Any interrupt that gets enabled later will get the correct
                // EOI address filled in by the hardware backend interrupt
                // enabling code.
                //
                for (i = 0 ; i <= MAXIMUM_IDTVECTOR; i++) {
	                CbusVectorToEoi[i] = &CbusTemp;
                }
                CbusTimeStamp = &CbusTemp;
        }

        //
        // Default stall execution to something reasonable
        // until we initialize it later in HalInitSystem.
        //
        CbusDefaultStall();

        //
        // Enable this processor's internal cache - do this
        // before stall execution is initialized in HalInitSystem.
        //
        csr = CbusCSR[Processor];

        Idp = (PEXT_ID_INFO)csr.idp;

        //
        // Enable the processor internal cache here...
        //
        if (Idp->proc_attr == PA_CACHE_ON) {
                i486cacheon();
        }

        //
        // Map this CPU's CSR stuff into his local address space for
        // fast access.  Also his logical # and bit position.
        //

        (PVOID)   KeGetPcr()->HalReserved[PCR_CSR] = CbusCSR[Processor].csr;

        (ULONG)   KeGetPcr()->HalReserved[PCR_PROCESSOR] = Processor;

        (ULONG)   KeGetPcr()->HalReserved[PCR_BIT] = (1 << Processor);

        (ULONG)   KeGetPcr()->HalReserved[PCR_ALL_OTHERS] =
                        (CbusProcessorMask & ~(1 << Processor));

        (PVOID)   KeGetPcr()->HalReserved[PCR_LED_ON] = (PVOID)
                        ((PUCHAR)CbusCSR[Processor].csr + CbusGlobal.smp_sled);

        (PVOID)   KeGetPcr()->HalReserved[PCR_LED_OFF] = (PVOID)
                        ((PUCHAR)CbusCSR[Processor].csr + CbusGlobal.smp_cled);

        //
        // Since our architecture is completely symmetric,
        // update affinity to contain each booted processor.
        //
        HalpDefaultInterruptAffinity |= (1 << Processor);

        //
        // This parameter is returned by the HAL when the system asks
        // for the HAL's configured resources list.
        //
        HalpActiveProcessors = HalpDefaultInterruptAffinity;

        CbusBootedProcessors += 1;
        CbusBootedProcessorsMask = (1 << CbusBootedProcessors) - 1;

        //
        // Initialize this processor's data - done ONCE by each processor.
        //
        // Typically, this processor's interrupt controller is initialized
        // here.  Also, if it's the first processor, any I/O interrupt
        // controllers will also be initialized here, ie: EISA bridges or
        // I/O APICs.
        //
        (*CbusBackend->InitializeCPU)(Processor);

        //
        // This is where we actually enable IPI, APC, DPC and
        // SPURIOUS interrupts.  Device interrupts (like clock and
        // profile) will not be enabled until HalInitSystem calls
        // HalpInitializePICs later.  Since we are still cli'd, no
        // interrupt could actually bop us until KiSystemStartup
        // calls KiInitializeKernel who drops IRQL.
        //
        HalpInitializeCoreIntrs();
}

#define CBUS1_NMI_MASK          (PUCHAR)0x70
#define CBUS1_IO_CHANNEL_CHECK  (PUCHAR)0x61

VOID
CbusClearEISANMI(VOID)
/*++

Routine Description:

    This function clears the NMI on the EISA bus.  Typically this
    was generated by one of Corollary's "NMI cards", used for
    debugging purposes.  Our caller will have pointed us at the
    correct bus bridge prior to calling us.  note therefore we cannot
    display anything because we may not be pointing at the default
    display - if we want to display, we must map the bridge containing
    the default video adapter!

Arguments:

    None.

Return Value:

    None.
--*/
{
        UCHAR   IoChannelCheck;

        WRITE_PORT_UCHAR(CBUS1_NMI_MASK, (UCHAR)0);

        IoChannelCheck = READ_PORT_UCHAR(CBUS1_IO_CHANNEL_CHECK);
        WRITE_PORT_UCHAR(CBUS1_IO_CHANNEL_CHECK,
                (UCHAR)((IoChannelCheck & 0xF) | 0x08));

        IoChannelCheck = READ_PORT_UCHAR(CBUS1_IO_CHANNEL_CHECK);
        WRITE_PORT_UCHAR(CBUS1_IO_CHANNEL_CHECK,
                (UCHAR)(IoChannelCheck & 0x7));

        //
        // Since the NMI we are clearing was caused by pressing the button,
        // which generated an EISA NMI (not a Cbus NMI), don't clear the
        // NMI in Cbus space.
        //
        // COUTB(CbusCSR[Processor].csr, CbusGlobal.smp_cnmi,
        //      CbusGlobal.smp_cnmi_val);
        //
}