nvramtool: 64bit safe CBFS handling
[coreboot.git] / util / mptable / mptable.c
1 /*
2  * Copyright (c) 1996, by Steve Passe
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. The name of the developer may NOT be used to endorse or promote products
11  *    derived from this software without specific prior written permission.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 /*
27  * mptable.c
28  */
29
30 #define VMAJOR                  2
31 #define VMINOR                  0
32 #define VDELTA                  15
33
34 /*
35  * this will cause the raw mp table to be dumped to /tmp/mpdump
36  *
37 #define RAW_DUMP
38  */
39
40 #define MP_SIG                  0x5f504d5f      /* _MP_ */
41 #define EXTENDED_PROCESSING_READY
42 #define OEM_PROCESSING_READY_NOT
43
44 #include <sys/types.h>
45 #include <err.h>
46 #include <fcntl.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <stdint.h>
52
53 #define SEP_LINE \
54 "\n-------------------------------------------------------------------------------\n"
55
56 #define SEP_LINE2 \
57 "\n===============================================================================\n"
58
59 /* EBDA is @ 40:0e in real-mode terms */
60 #define EBDA_POINTER            0x040e          /* location of EBDA pointer */
61
62 /* CMOS 'top of mem' is @ 40:13 in real-mode terms */
63 #define TOPOFMEM_POINTER        0x0413          /* BIOS: base memory size */
64
65 #define DEFAULT_TOPOFMEM        0xa0000
66
67 #define BIOS_BASE               0xf0000
68 #define BIOS_BASE2              0xe0000
69 #define BIOS_SIZE               0x10000
70 #define ONE_KBYTE               1024
71
72 #define GROPE_AREA1             0x80000
73 #define GROPE_AREA2             0x90000
74 #define GROPE_SIZE              0x10000
75
76 #define PROCENTRY_FLAG_EN       0x01
77 #define PROCENTRY_FLAG_BP       0x02
78 #define IOAPICENTRY_FLAG_EN     0x01
79
80 #define MAXPNSTR                132
81
82 #define COREBOOT_MP_TABLE      0
83
84 enum busTypes {
85     CBUS = 1,
86     CBUSII = 2,
87     EISA = 3,
88     ISA = 6,
89     PCI = 13,
90     XPRESS = 18,
91     MAX_BUSTYPE = 18,
92     UNKNOWN_BUSTYPE = 0xff
93 };
94
95 typedef struct BUSTYPENAME {
96     uint8_t     type;
97     char        name[ 7 ];
98 } busTypeName;
99
100 static busTypeName busTypeTable[] =
101 {
102     { CBUS,             "CBUS"   },
103     { CBUSII,           "CBUSII" },
104     { EISA,             "EISA"   },
105     { UNKNOWN_BUSTYPE,  "---"    },
106     { UNKNOWN_BUSTYPE,  "---"    },
107     { ISA,              "ISA"    },
108     { UNKNOWN_BUSTYPE,  "---"    },
109     { UNKNOWN_BUSTYPE,  "---"    },
110     { UNKNOWN_BUSTYPE,  "---"    },
111     { UNKNOWN_BUSTYPE,  "---"    },
112     { UNKNOWN_BUSTYPE,  "---"    },
113     { UNKNOWN_BUSTYPE,  "---"    },
114     { PCI,              "PCI"    },
115     { UNKNOWN_BUSTYPE,  "---"    },
116     { UNKNOWN_BUSTYPE,  "---"    },
117     { UNKNOWN_BUSTYPE,  "---"    },
118     { UNKNOWN_BUSTYPE,  "---"    },
119     { UNKNOWN_BUSTYPE,  "---"    },
120     { UNKNOWN_BUSTYPE,  "---"    }
121 };
122
123 char* whereStrings[] = {
124     "Extended BIOS Data Area",
125     "BIOS top of memory",
126     "Default top of memory",
127     "BIOS",
128     "Extended BIOS",
129     "GROPE AREA #1",
130     "GROPE AREA #2"
131 };
132
133 typedef struct TABLE_ENTRY {
134     uint8_t     type;
135     uint8_t     length;
136     char        name[ 32 ];
137 } tableEntry;
138
139 tableEntry basetableEntryTypes[] =
140 {
141     { 0, 20, "Processor" },
142     { 1,  8, "Bus" },
143     { 2,  8, "I/O APIC" },
144     { 3,  8, "I/O INT" },
145     { 4,  8, "Local INT" }
146 };
147
148 tableEntry extendedtableEntryTypes[] =
149 {
150     { 128, 20, "System Address Space" },
151     { 129,  8, "Bus Heirarchy" },
152     { 130,  8, "Compatibility Bus Address" }
153 };
154
155 /* MP Floating Pointer Structure */
156 typedef struct MPFPS {
157     uint8_t     signature[ 4 ];
158     uint32_t    pap;
159     uint8_t     length;
160     uint8_t     spec_rev;
161     uint8_t     checksum;
162     uint8_t     mpfb1;
163     uint8_t     mpfb2;
164     uint8_t     mpfb3;
165     uint8_t     mpfb4;
166     uint8_t     mpfb5;
167 } mpfps_t;
168
169 /* MP Configuration Table Header */
170 typedef struct MPCTH {
171     uint8_t     signature[ 4 ];
172     uint16_t    base_table_length;
173     uint8_t     spec_rev;
174     uint8_t     checksum;
175     uint8_t     oem_id[ 8 ];
176     uint8_t     product_id[ 12 ];
177     uint32_t    oem_table_pointer;
178     uint16_t    oem_table_size;
179     uint16_t    entry_count;
180     uint32_t    apic_address;
181     uint16_t    extended_table_length;
182     uint8_t     extended_table_checksum;
183     uint8_t     reserved;
184 } mpcth_t;
185
186
187 typedef struct PROCENTRY {
188     uint8_t     type;
189     uint8_t     apicID;
190     uint8_t     apicVersion;
191     uint8_t     cpuFlags;
192     uint32_t    cpuSignature;
193     uint32_t    featureFlags;
194     uint32_t    reserved1;
195     uint32_t    reserved2;
196 } ProcEntry;
197
198 typedef struct BUSENTRY {
199     uint8_t     type;
200     uint8_t     busID;
201     uint8_t     busType[ 6 ];
202 } BusEntry;
203
204 typedef struct IOAPICENTRY {
205     uint8_t     type;
206     uint8_t     apicID;
207     uint8_t     apicVersion;
208     uint8_t     apicFlags;
209     uint32_t    apicAddress;
210 } IOApicEntry;
211
212 typedef struct INTENTRY {
213     uint8_t     type;
214     uint8_t     intType;
215     uint16_t    intFlags;
216     uint8_t     srcBusID;
217     uint8_t     srcBusIRQ;
218     uint8_t     dstApicID;
219     uint8_t     dstApicINT;
220 } IntEntry;
221
222
223 /*
224  * extended entry type structures
225  */
226
227 typedef struct SASENTRY {
228     uint8_t     type;
229     uint8_t     length;
230     uint8_t     busID;
231     uint8_t     addressType;
232     uint64_t    addressBase;
233     uint64_t    addressLength;
234 } SasEntry;
235
236
237 typedef struct BHDENTRY {
238     uint8_t     type;
239     uint8_t     length;
240     uint8_t     busID;
241     uint8_t     busInfo;
242     uint8_t     busParent;
243     uint8_t     reserved[ 3 ];
244 } BhdEntry;
245
246
247 typedef struct CBASMENTRY {
248     uint8_t     type;
249     uint8_t     length;
250     uint8_t     busID;
251     uint8_t     addressMod;
252     uint32_t    predefinedRange;
253 } CbasmEntry;
254
255
256 typedef uint32_t vm_offset_t;
257
258 static void apic_probe( vm_offset_t* paddr, int* where );
259
260 static void MPConfigDefault( int featureByte );
261
262 static void MPFloatingPointer( vm_offset_t paddr, int where, mpfps_t* mpfps );
263 static void MPConfigTableHeader( uint32_t pap );
264
265 static int readType( void );
266 static void seekEntry( vm_offset_t addr );
267 static void readEntry( void* entry, int size );
268
269 static void processorEntry( void );
270 static void busEntry( void );
271 static void ioApicEntry( void );
272 static void intEntry( void );
273 static void lintEntry( void );
274
275 static void sasEntry( void );
276 static void bhdEntry( void );
277 static void cbasmEntry( void );
278
279 static void doOptionList( void );
280 static void doDmesg( void );
281 static void pnstr( uint8_t* s, int c );
282
283 /* global data */
284 int     pfd;            /* physical /dev/mem fd */
285
286 int     busses[ 16 ];
287 int     apics[ 16 ];
288
289 int     ncpu;
290 int     nbus;
291 int     napic;
292 int     nintr;
293
294 int     dmesg = 0;
295 int     grope = 0;
296 int     verbose = 0;
297 int     noisy = 0;
298 /* preamble to the mptable. This is fixed for all coreboots */
299
300 char *preamble[] = {
301 "#include <console/console.h>",
302 "#include <arch/smp/mpspec.h>",
303 "#include <arch/ioapic.h>",
304 "#include <device/pci.h>",
305 "#include <string.h>",
306 "#include <stdint.h>",
307 "",
308 "static void *smp_write_config_table(void *v)",
309 "{",
310 "        struct mp_config_table *mc;",
311 "",
312 "        mc = (void *)(((char *)v) + SMP_FLOATING_TABLE_LEN);",
313 "",
314 "        mptable_init(mc, \"TODO        \", LAPIC_ADDR);",
315 "",
316 "        smp_write_processors(mc);",
317 "",
318 "",
319 0
320 };
321
322 char *postamble[] = {
323 "       /* Compute the checksums. */",
324 "       mc->mpe_checksum = smp_compute_checksum(smp_next_mpc_entry(mc), mc->mpe_length);",
325 "       mc->mpc_checksum = smp_compute_checksum(mc, mc->mpc_length);",
326 "       printk(BIOS_DEBUG, \"Wrote the mp table end at: %p - %p\\n\",",
327 "               mc, smp_next_mpe_entry(mc));",
328 "       return smp_next_mpe_entry(mc);",
329 "}",
330 "",
331 "unsigned long write_smp_table(unsigned long addr)",
332 "{",
333 "       void *v;",
334 "       v = smp_write_floating_table(addr);",
335 "       return (unsigned long)smp_write_config_table(v);",
336 "}",
337 0
338 };
339
340 char *ioapic_code[] = {
341 "       smp_write_ioapic(mc, 2, 0x20, IO_APIC_ADDR);",
342 "       /* TODO: If you have multiple IOAPICs, add them here. */",
343 0
344 };
345 static void
346 usage( void )
347 {
348     fprintf( stderr, "usage: mptable [-dmesg] [-verbose] [-grope] [-help]\n" );
349     exit( 0 );
350 }
351
352 void
353 write_code(char **code)
354 {
355   while (*code)
356         printf("%s\n", *code++);
357 }
358 /*
359  *
360  */
361 int
362 main( int argc, char *argv[] )
363 {
364     vm_offset_t paddr;
365     int         where;
366     mpfps_t     mpfps;
367     int         defaultConfig;
368
369     /* announce ourselves */
370
371     if (verbose) puts( SEP_LINE2 );
372
373     printf( "/* generated by MPTable, version %d.%d.%d*/\n", VMAJOR, VMINOR, VDELTA );
374     printf("/* as modified by RGM for coreboot */\n");
375     write_code(preamble);
376
377     /* Ron hates getopt() */
378
379     for(argc--, argv++; argc; argc--, argv++){
380       if ( strcmp( argv[0], "-dmesg") == 0 ) {
381         dmesg = 1;
382       } else
383         if ( strcmp( argv[0], "-help") == 0 )
384           {
385             usage();
386           } else
387             if ( strcmp( argv[0], "-grope") == 0 ){
388               grope = 1;
389             } else  if ( strcmp( argv[0], "-verbose") == 0 )
390               verbose = 1;
391             else  if ( strcmp( argv[0], "-noisy") == 0 )
392               noisy = 1;
393             else usage();
394     }
395
396     /* open physical memory for access to MP structures */
397     if ( (pfd = open( "/dev/mem", O_RDONLY )) < 0 )
398         err( 1, "mem open" );
399
400     /* probe for MP structures */
401     apic_probe( &paddr, &where );
402     if ( where <= 0 ) {
403         fprintf( stderr, "\n MP FPS NOT found,\n" );
404         fprintf( stderr, " suggest trying -grope option!!!\n\n" );
405         return 1;
406     }
407
408     if ( verbose )
409         printf( "\n MP FPS found in %s @ physical addr: 0x%08x\n",
410               whereStrings[ where - 1 ], paddr );
411
412     if (verbose) puts( SEP_LINE );
413
414     /* analyze the MP Floating Pointer Structure */
415     MPFloatingPointer( paddr, where, &mpfps );
416
417     if (verbose) puts( SEP_LINE );
418
419     /* check whether an MP config table exists */
420     if ( (defaultConfig = mpfps.mpfb1) )
421         MPConfigDefault( defaultConfig );
422     else
423         MPConfigTableHeader( mpfps.pap );
424
425     /* build "options" entries for the kernel config file */
426     if (noisy)
427       doOptionList();
428
429     write_code(postamble);
430     /* do a dmesg output */
431     if ( dmesg )
432         doDmesg();
433
434     if (verbose) puts( SEP_LINE2 );
435
436     return 0;
437 }
438
439
440 /*
441  * set PHYSICAL address of MP floating pointer structure
442  */
443 #define NEXT(X)         ((X) += 4)
444 static void
445 apic_probe( vm_offset_t* paddr, int* where )
446 {
447     /*
448      * c rewrite of apic_probe() by Jack F. Vogel
449      */
450
451     int         x;
452     u_short     segment;
453     vm_offset_t target;
454     u_int       buffer[ BIOS_SIZE / sizeof( int ) ];
455
456     if ( verbose )
457         printf( "\n" );
458
459     /* search Extended Bios Data Area, if present */
460     if ( verbose )
461         printf( " looking for EBDA pointer @ 0x%04x, ", EBDA_POINTER );
462     seekEntry( (vm_offset_t)EBDA_POINTER );
463     readEntry( &segment, 2 );
464     if ( segment ) {                /* search EBDA */
465         target = (vm_offset_t)segment << 4;
466         if ( verbose )
467             printf( "found, searching EBDA @ 0x%08x\n", target );
468         seekEntry( target );
469         readEntry( buffer, ONE_KBYTE );
470
471         for ( x = 0; x < ONE_KBYTE / sizeof ( unsigned int ); NEXT(x) ) {
472             if ( buffer[ x ] == MP_SIG ) {
473                 *where = 1;
474                 *paddr = (x * sizeof( unsigned int )) + target;
475                 return;
476             }
477         }
478     }
479     else {
480         if ( verbose )
481             printf( "NOT found\n" );
482     }
483
484     target = 0;
485     segment = 0;
486     if ( verbose )
487         printf( " searching for coreboot MP table  @ 0x%08x (%dK)\n",
488                 target, segment );
489     seekEntry( target );
490     readEntry( buffer, ONE_KBYTE );
491
492     for ( x = 0; x < ONE_KBYTE / sizeof ( unsigned int ); NEXT(x) ) {
493         if ( buffer[ x ] == MP_SIG ) {
494             *where = 2;
495             *paddr = (x * sizeof( unsigned int )) + target;
496             return;
497         }
498     }
499
500
501     /* read CMOS for real top of mem */
502     seekEntry( (vm_offset_t)TOPOFMEM_POINTER );
503     readEntry( &segment, 2 );
504     --segment;                                          /* less ONE_KBYTE */
505     target = segment * 1024;
506     if ( verbose )
507         printf( " searching CMOS 'top of mem' @ 0x%08x (%dK)\n",
508                 target, segment );
509     seekEntry( target );
510     readEntry( buffer, ONE_KBYTE );
511
512     for ( x = 0; x < ONE_KBYTE / sizeof ( unsigned int ); NEXT(x) ) {
513         if ( buffer[ x ] == MP_SIG ) {
514             *where = 2;
515             *paddr = (x * sizeof( unsigned int )) + target;
516             return;
517         }
518     }
519
520     /* we don't necessarily believe CMOS, check base of the last 1K of 640K */
521     if ( target != (DEFAULT_TOPOFMEM - 1024)) {
522         target = (DEFAULT_TOPOFMEM - 1024);
523         if ( verbose )
524             printf( " searching default 'top of mem' @ 0x%08x (%dK)\n",
525                     target, (target / 1024) );
526         seekEntry( target );
527         readEntry( buffer, ONE_KBYTE );
528
529         for ( x = 0; x < ONE_KBYTE / sizeof ( unsigned int ); NEXT(x) ) {
530             if ( buffer[ x ] == MP_SIG ) {
531                 *where = 3;
532                 *paddr = (x * sizeof( unsigned int )) + target;
533                 return;
534             }
535         }
536     }
537
538     /* search the BIOS */
539     if ( verbose )
540         printf( " searching BIOS @ 0x%08x\n", BIOS_BASE );
541     seekEntry( BIOS_BASE );
542     readEntry( buffer, BIOS_SIZE );
543
544     for ( x = 0; x < BIOS_SIZE / sizeof( unsigned int ); NEXT(x) ) {
545         if ( buffer[ x ] == MP_SIG ) {
546             *where = 4;
547             *paddr = (x * sizeof( unsigned int )) + BIOS_BASE;
548             return;
549         }
550     }
551
552     /* search the extended BIOS */
553     if ( verbose )
554         printf( " searching extended BIOS @ 0x%08x\n", BIOS_BASE2 );
555     seekEntry( BIOS_BASE2 );
556     readEntry( buffer, BIOS_SIZE );
557
558     for ( x = 0; x < BIOS_SIZE / sizeof( unsigned int ); NEXT(x) ) {
559         if ( buffer[ x ] == MP_SIG ) {
560             *where = 5;
561             *paddr = (x * sizeof( unsigned int )) + BIOS_BASE2;
562             return;
563         }
564     }
565
566     if ( grope ) {
567         /* search additional memory */
568         target = GROPE_AREA1;
569         if ( verbose )
570             printf( " groping memory @ 0x%08x\n", target );
571         seekEntry( target );
572         readEntry( buffer, GROPE_SIZE );
573
574         for ( x = 0; x < GROPE_SIZE / sizeof( unsigned int ); NEXT(x) ) {
575             if ( buffer[ x ] == MP_SIG ) {
576                 *where = 6;
577                 *paddr = (x * sizeof( unsigned int )) + GROPE_AREA1;
578                 return;
579             }
580         }
581
582         target = GROPE_AREA2;
583         if ( verbose )
584             printf( " groping memory @ 0x%08x\n", target );
585         seekEntry( target );
586         readEntry( buffer, GROPE_SIZE );
587
588         for ( x = 0; x < GROPE_SIZE / sizeof( unsigned int ); NEXT(x) ) {
589             if ( buffer[ x ] == MP_SIG ) {
590                 *where = 7;
591                 *paddr = (x * sizeof( unsigned int )) + GROPE_AREA2;
592                 return;
593             }
594         }
595     }
596
597     *where = 0;
598     *paddr = (vm_offset_t)0;
599 }
600
601
602 /*
603  *
604  */
605 static void
606 MPFloatingPointer( vm_offset_t paddr, int where, mpfps_t* mpfps )
607 {
608
609     /* read in mpfps structure*/
610     seekEntry( paddr );
611     readEntry( mpfps, sizeof( mpfps_t ) );
612
613     /* show its contents */
614     if (verbose) {
615     printf( "MP Floating Pointer Structure:\n\n" );
616
617     printf( "  location:\t\t\t" );
618     switch ( where )
619     {
620     case 1:
621         printf( "EBDA\n" );
622         break;
623     case 2:
624         printf( "BIOS base memory\n" );
625         break;
626     case 3:
627         printf( "DEFAULT base memory (639K)\n" );
628         break;
629     case 4:
630         printf( "BIOS\n" );
631         break;
632     case 5:
633         printf( "Extended BIOS\n" );
634         break;
635
636     case 0:
637         printf( "NOT found!\n" );
638         exit( 1 );
639     default:
640         printf( "BOGUS!\n" );
641         exit( 1 );
642     }
643     printf( "  physical address:\t\t0x%08x\n", paddr );
644
645     printf( "  signature:\t\t\t'" );
646     pnstr( mpfps->signature, 4 );
647     printf( "'\n" );
648
649     printf( "  length:\t\t\t%d bytes\n", mpfps->length * 16 );
650     printf( "  version:\t\t\t1.%1d\n", mpfps->spec_rev );
651     printf( "  checksum:\t\t\t0x%02x\n", mpfps->checksum );
652
653     /* bits 0:6 are RESERVED */
654     if ( mpfps->mpfb2 & 0x7f ) {
655         printf( " warning, MP feature byte 2: 0x%02x\n", mpfps->mpfb2 );
656     }
657
658     /* bit 7 is IMCRP */
659     printf( "  mode:\t\t\t\t%s\n", (mpfps->mpfb2 & 0x80) ?
660             "PIC" : "Virtual Wire" );
661
662     /* MP feature bytes 3-5 are expected to be ZERO */
663     if ( mpfps->mpfb3 )
664         printf( " warning, MP feature byte 3 NONZERO!\n" );
665     if ( mpfps->mpfb4 )
666         printf( " warning, MP feature byte 4 NONZERO!\n" );
667     if ( mpfps->mpfb5 )
668         printf( " warning, MP feature byte 5 NONZERO!\n" );
669     }
670 }
671
672
673 /*
674  *
675  */
676 static void
677 MPConfigDefault( int featureByte )
678 {
679     printf( "  MP default config type: %d\n\n", featureByte );
680     switch ( featureByte ) {
681     case 1:
682         printf( "   bus: ISA, APIC: 82489DX\n" );
683         break;
684     case 2:
685         printf( "   bus: EISA, APIC: 82489DX\n" );
686         break;
687     case 3:
688         printf( "   bus: EISA, APIC: 82489DX\n" );
689         break;
690     case 4:
691         printf( "   bus: MCA, APIC: 82489DX\n" );
692         break;
693     case 5:
694         printf( "   bus: ISA+PCI, APIC: Integrated\n" );
695         break;
696     case 6:
697         printf( "   bus: EISA+PCI, APIC: Integrated\n" );
698         break;
699     case 7:
700         printf( "   bus: MCA+PCI, APIC: Integrated\n" );
701         break;
702     default:
703         printf( "   future type\n" );
704         break;
705     }
706
707     switch ( featureByte ) {
708     case 1:
709     case 2:
710     case 3:
711     case 4:
712         nbus = 1;
713         break;
714     case 5:
715     case 6:
716     case 7:
717         nbus = 2;
718         break;
719     default:
720         printf( "   future type\n" );
721         break;
722     }
723
724     ncpu = 2;
725     napic = 1;
726     nintr = 16;
727 }
728
729
730 /*
731  *
732  */
733 static void
734 MPConfigTableHeader( uint32_t pap )
735 {
736     vm_offset_t paddr;
737     mpcth_t     cth;
738     int         x;
739     int         totalSize, t;
740     int         count, c;
741     int         type;
742
743     if ( pap == 0 ) {
744         printf( "MP Configuration Table Header MISSING!\n" );
745         exit( 1 );
746     }
747
748     /* convert physical address to virtual address */
749     paddr = (vm_offset_t)pap;
750
751     /* read in cth structure */
752     seekEntry( paddr );
753     readEntry( &cth, sizeof( cth ) );
754
755     if (verbose) {
756     printf( "MP Config Table Header:\n\n" );
757
758     printf( "  physical address:\t\t0x%08x\n", pap );
759
760     printf( "  signature:\t\t\t'" );
761     pnstr( cth.signature, 4 );
762     printf( "'\n" );
763
764     printf( "  base table length:\t\t%d\n", cth.base_table_length );
765
766     printf( "  version:\t\t\t1.%1d\n", cth.spec_rev );
767     printf( "  checksum:\t\t\t0x%02x\n", cth.checksum );
768
769     printf( "  OEM ID:\t\t\t'" );
770     pnstr( cth.oem_id, 8 );
771     printf( "'\n" );
772
773     printf( "  Product ID:\t\t\t'" );
774     pnstr( cth.product_id, 12 );
775     printf( "'\n" );
776
777     printf( "  OEM table pointer:\t\t0x%08x\n", cth.oem_table_pointer );
778     printf( "  OEM table size:\t\t%d\n", cth.oem_table_size );
779
780     printf( "  entry count:\t\t\t%d\n", cth.entry_count );
781
782     printf( "  local APIC address:\t\t0x%08x\n", cth.apic_address );
783
784     printf( "  extended table length:\t%d\n", cth.extended_table_length );
785     printf( "  extended table checksum:\t%d\n", cth.extended_table_checksum );
786     }
787
788     totalSize = cth.base_table_length - sizeof( struct MPCTH );
789     count = cth.entry_count;
790
791     if (verbose) {
792     if (verbose) puts( SEP_LINE );
793
794     printf( "MP Config Base Table Entries:\n\n" );
795     }
796
797     /* initialze tables */
798     for ( x = 0; x < 16; ++x ) {
799         busses[ x ] = apics[ x ] = 0xff;
800     }
801
802     ncpu = 0;
803     nbus = 0;
804     napic = 0;
805     nintr = 0;
806
807     /* process all the CPUs */
808     if (verbose) {
809     printf( "--\nProcessors:\tAPIC ID\tVersion\tState"
810             "\t\tFamily\tModel\tStep\tFlags\n" );
811     }
812     for ( t = totalSize, c = count; c; c-- ) {
813         if ( readType() == 0 )
814             processorEntry();
815         totalSize -= basetableEntryTypes[ 0 ].length;
816     }
817
818     /* process all the busses */
819     printf( "/*Bus:\t\tBus ID\tType*/\n" );
820     for ( t = totalSize, c = count; c; c-- ) {
821         if ( readType() == 1 )
822             busEntry();
823         totalSize -= basetableEntryTypes[ 1 ].length;
824     }
825
826     /* process all the apics */
827     printf( "/*I/O APICs:\tAPIC ID\tVersion\tState\t\tAddress*/\n" );
828     for ( t = totalSize, c = count; c; c-- ) {
829         if ( readType() == 2 )
830             ioApicEntry();
831         totalSize -= basetableEntryTypes[ 2 ].length;
832     }
833
834     write_code(ioapic_code);
835
836     /* process all the I/O Ints */
837     printf( "/*I/O Ints:\tType\tPolarity    Trigger\tBus ID\t IRQ\tAPIC ID\tPIN#\n*/" );
838     for ( t = totalSize, c = count; c; c-- ) {
839         if ( readType() == 3 )
840             intEntry();
841         totalSize -= basetableEntryTypes[ 3 ].length;
842     }
843
844     /* process all the Local Ints */
845     printf( "/*Local Ints:\tType\tPolarity    Trigger\tBus ID\t IRQ\tAPIC ID\tPIN#*/\n" );
846     for ( t = totalSize, c = count; c; c-- ) {
847         if ( readType() == 4 )
848             lintEntry();
849         totalSize -= basetableEntryTypes[ 4 ].length;
850     }
851
852
853 #if defined( EXTENDED_PROCESSING_READY )
854     /* process any extended data */
855     if ( (totalSize = cth.extended_table_length) ) {
856         if (verbose) puts( SEP_LINE );
857
858         printf( "MP Config Extended Table Entries:\n\n" );
859
860         while ( totalSize > 0 ) {
861             switch ( type = readType() ) {
862             case 128:
863                 sasEntry();
864                 break;
865             case 129:
866                 bhdEntry();
867                 break;
868             case 130:
869                 cbasmEntry();
870                 break;
871             default:
872                 printf( "Extended Table HOSED!\n" );
873                 exit( 1 );
874             }
875
876             totalSize -= extendedtableEntryTypes[ type-128 ].length;
877         }
878     }
879 #endif  /* EXTENDED_PROCESSING_READY */
880
881     /* process any OEM data */
882     if ( cth.oem_table_pointer && (cth.oem_table_size > 0) ) {
883 #if defined( OEM_PROCESSING_READY )
884 # error your on your own here!
885         /* convert OEM table pointer to virtual address */
886         poemtp = (vm_offset_t)cth.oem_table_pointer;
887
888         /* read in oem table structure */
889         if ( (oemdata = (void*)malloc( cth.oem_table_size )) == NULL )
890             err( 1, "oem malloc" );
891
892         seekEntry( poemtp );
893         readEntry( oemdata, cth.oem_table_size );
894
895         /** process it */
896
897         free( oemdata );
898 #else
899         printf( "\nyou need to modify the source to handle OEM data!\n\n" );
900 #endif  /* OEM_PROCESSING_READY */
901     }
902
903     fflush( stdout );
904
905 #if defined( RAW_DUMP )
906 {
907     int         ofd;
908     u_char      dumpbuf[ 4096 ];
909
910     ofd = open( "/tmp/mpdump", O_CREAT | O_RDWR );
911     seekEntry( paddr );
912     readEntry( dumpbuf, 1024 );
913     write( ofd, dumpbuf, 1024 );
914     close( ofd );
915 }
916 #endif /* RAW_DUMP */
917 }
918
919
920 /*
921  *
922  */
923 static int
924 readType( void )
925 {
926     u_char      type;
927
928     if ( read( pfd, &type, sizeof( u_char ) ) != sizeof( u_char ) )
929         err( 1, "type read; pfd: %d", pfd );
930
931     if ( lseek( pfd, -1, SEEK_CUR ) < 0 )
932         err( 1, "type seek" );
933
934     return (int)type;
935 }
936
937
938 /*
939  *
940  */
941 static void
942 seekEntry( vm_offset_t addr )
943 {
944     if ( lseek( pfd, (off_t)addr, SEEK_SET ) < 0 )
945         err( 1, "/dev/mem seek" );
946 }
947
948
949 /*
950  *
951  */
952 static void
953 readEntry( void* entry, int size )
954 {
955     if ( read( pfd, entry, size ) != size )
956         err( 1, "readEntry" );
957 }
958
959
960 static void
961 processorEntry( void )
962 {
963     ProcEntry   entry;
964
965     /* read it into local memory */
966     readEntry( &entry, sizeof( entry ) );
967
968     /* count it */
969     ++ncpu;
970     if (noisy) {
971     printf( "\t\t%2d", entry.apicID );
972     printf( "\t 0x%2x", entry.apicVersion );
973
974     printf( "\t %s, %s",
975             (entry.cpuFlags & PROCENTRY_FLAG_BP) ? "BSP" : "AP",
976             (entry.cpuFlags & PROCENTRY_FLAG_EN) ? "usable" : "unusable" );
977
978     printf( "\t %d\t %d\t %d",
979             (entry.cpuSignature >> 8) & 0x0f,
980             (entry.cpuSignature >> 4) & 0x0f,
981             entry.cpuSignature & 0x0f );
982
983     printf( "\t 0x%04x\n", entry.featureFlags );
984     }
985 }
986
987
988 /*
989  *
990  */
991 static int
992 lookupBusType( char* name )
993 {
994     int x;
995
996     for ( x = 0; x < MAX_BUSTYPE; ++x )
997         if ( strcmp( busTypeTable[ x ].name, name ) == 0 )
998             return busTypeTable[ x ].type;
999
1000     return UNKNOWN_BUSTYPE;
1001 }
1002
1003
1004 static void
1005 busEntry( void )
1006 {
1007     int         x;
1008     char        name[ 8 ];
1009     char        c;
1010     BusEntry    entry;
1011
1012     /* read it into local memory */
1013     readEntry( &entry, sizeof( entry ) );
1014
1015     /* count it */
1016     ++nbus;
1017
1018     if (verbose)  {
1019     printf( "\t\t%2d", entry.busID );
1020     printf( "\t " ); pnstr( entry.busType, 6 ); printf( "\n" );
1021     }
1022
1023     for ( x = 0; x < 6; ++x ) {
1024 //      if ( (c = entry.busType[ x ]) == ' ' )
1025             //break;
1026         name[ x ] = c;
1027     }
1028     name[ x ] = '\0';
1029     busses[ entry.busID ] = lookupBusType( name );
1030     printf("\tsmp_write_bus(mc, %d, \"", entry.busID);
1031     pnstr( entry.busType, 6 );
1032     printf("\");\n");
1033 }
1034
1035
1036 static void
1037 ioApicEntry( void )
1038 {
1039     IOApicEntry entry;
1040
1041     /* read it into local memory */
1042     readEntry( &entry, sizeof( entry ) );
1043
1044     /* count it */
1045     ++napic;
1046
1047     if (noisy) {
1048     printf( "\t\t%2d", entry.apicID );
1049     printf( "\t 0x%02x", entry.apicVersion );
1050     printf( "\t %s",
1051             (entry.apicFlags & IOAPICENTRY_FLAG_EN) ? "usable" : "unusable" );
1052     printf( "\t\t 0x%x\n", entry.apicAddress );
1053     }
1054
1055     apics[ entry.apicID ] = entry.apicID;
1056
1057     // the numbering and setup of ioapics is so irrational
1058     // that for now we will punt.
1059 #if 0
1060     if (entry.apicFlags & IOAPICENTRY_FLAG_EN)
1061       printf("\tsmp_write_ioapic(mc, 0x%x, 0x%x, 0x%x);\n",
1062              entry.apicID, entry.apicVersion, entry.apicAddress);
1063 #endif
1064
1065 }
1066
1067
1068 char* intTypes[] = {
1069     "mp_INT", "mp_NMI", "mp_SMI", "mp_ExtINT"
1070 };
1071
1072 char* polarityMode[] = {
1073     "MP_IRQ_POLARITY_DEFAULT", "MP_IRQ_POLARITY_HIGH", "reserved", "MP_IRQ_POLARITY_LOW"
1074 };
1075 char* triggerMode[] = {
1076     "MP_IRQ_TRIGGER_DEFAULT", "MP_IRQ_TRIGGER_EDGE", "reserved", "MP_IRQ_TRIGGER_LEVEL"
1077 };
1078
1079 static void
1080 intEntry( void )
1081 {
1082     IntEntry    entry;
1083
1084     /* read it into local memory */
1085     readEntry( &entry, sizeof( entry ) );
1086
1087     /* count it */
1088     if ( (int)entry.type == 3 )
1089         ++nintr;
1090
1091     if (noisy) {
1092     printf( "\n\t\t%s", intTypes[ (int)entry.intType ] );
1093
1094     printf( "\t%9s", polarityMode[ (int)entry.intFlags & 0x03 ] );
1095     printf( "%12s", triggerMode[ ((int)entry.intFlags >> 2) & 0x03 ] );
1096
1097     printf( "\t %5d", (int)entry.srcBusID );
1098     if ( busses[ (int)entry.srcBusID ] == PCI )
1099         printf( "\t%2d:%c",
1100                 ((int)entry.srcBusIRQ >> 2) & 0x1f,
1101                 ((int)entry.srcBusIRQ & 0x03) + 'A' );
1102     else
1103         printf( "\t 0x%x:0x%x(0x%x)",
1104                                 (int)entry.srcBusIRQ>>2,
1105                                 (int)entry.srcBusIRQ & 3,
1106                                 (int)entry.srcBusIRQ );
1107     printf( "\t %6d", (int)entry.dstApicID );
1108     printf( "\t %3d\n", (int)entry.dstApicINT );
1109     }
1110     printf("\tsmp_write_intsrc(mc, %s, %s|%s, 0x%x, 0x%x, 0x%x, 0x%x);\n",
1111            intTypes[ (int)entry.intType ],
1112            triggerMode[ ((int)entry.intFlags >> 2) & 0x03 ] ,
1113            polarityMode[ (int)entry.intFlags & 0x03 ],
1114            (int)entry.srcBusID,
1115            (int)entry.srcBusIRQ,
1116            (int)entry.dstApicID ,
1117            (int)entry.dstApicINT );
1118
1119 }
1120
1121 static void
1122 lintEntry( void )
1123 {
1124     IntEntry    entry;
1125
1126     /* read it into local memory */
1127     readEntry( &entry, sizeof( entry ) );
1128
1129     /* count it */
1130     if ( (int)entry.type == 3 )
1131         ++nintr;
1132
1133     if (noisy) {
1134     printf( "\t\t%s", intTypes[ (int)entry.intType ] );
1135
1136     printf( "\t%9s", polarityMode[ (int)entry.intFlags & 0x03 ] );
1137     printf( "%12s", triggerMode[ ((int)entry.intFlags >> 2) & 0x03 ] );
1138
1139     printf( "\t %5d", (int)entry.srcBusID );
1140     if ( busses[ (int)entry.srcBusID ] == PCI )
1141         printf( "\t%2d:%c",
1142                 ((int)entry.srcBusIRQ >> 2) & 0x1f,
1143                 ((int)entry.srcBusIRQ & 0x03) + 'A' );
1144     else
1145         printf( "\t %3d", (int)entry.srcBusIRQ );
1146     printf( "\t %6d", (int)entry.dstApicID );
1147     printf( "\t %3d\n", (int)entry.dstApicINT );
1148     }
1149     printf("\tsmp_write_lintsrc(mc, %s, %s|%s, 0x%x, 0x%x, MP_APIC_ALL, 0x%x);\n",
1150            intTypes[ (int)entry.intType ],
1151            triggerMode[ ((int)entry.intFlags >> 2) & 0x03 ] ,
1152            polarityMode[ (int)entry.intFlags & 0x03 ],
1153            (int)entry.srcBusID,
1154            (int)entry.srcBusIRQ,
1155            (int)entry.dstApicINT );
1156
1157 }
1158
1159
1160 static void
1161 sasEntry( void )
1162 {
1163     SasEntry    entry;
1164
1165     /* read it into local memory */
1166     readEntry( &entry, sizeof( entry ) );
1167
1168     printf( "--\n%s\n", extendedtableEntryTypes[ entry.type - 128 ].name );
1169     printf( " bus ID: %d", entry.busID );
1170     printf( " address type: " );
1171     switch ( entry.addressType ) {
1172     case 0:
1173         printf( "I/O address\n" );
1174         break;
1175     case 1:
1176         printf( "memory address\n" );
1177         break;
1178     case 2:
1179         printf( "prefetch address\n" );
1180         break;
1181     default:
1182         printf( "UNKNOWN type\n" );
1183         break;
1184     }
1185
1186     printf( " address base: 0x%lx\n", entry.addressBase );
1187     printf( " address range: 0x%lx\n", entry.addressLength );
1188 }
1189
1190
1191 static void
1192 bhdEntry( void )
1193 {
1194     BhdEntry    entry;
1195
1196     /* read it into local memory */
1197     readEntry( &entry, sizeof( entry ) );
1198
1199     printf( "--\n%s\n", extendedtableEntryTypes[ entry.type - 128 ].name );
1200     printf( " bus ID: %d", entry.busID );
1201     printf( " bus info: 0x%02x", entry.busInfo );
1202     printf( " parent bus ID: %d", entry.busParent );
1203 }
1204
1205
1206 static void
1207 cbasmEntry( void )
1208 {
1209     CbasmEntry  entry;
1210
1211     /* read it into local memory */
1212     readEntry( &entry, sizeof( entry ) );
1213
1214     printf( "--\n%s\n", extendedtableEntryTypes[ entry.type - 128 ].name );
1215     printf( " bus ID: %d", entry.busID );
1216     printf( " address modifier: %s\n", (entry.addressMod & 0x01) ?
1217                                         "subtract" : "add" );
1218     printf( " predefined range: 0x%08x", entry.predefinedRange );
1219 }
1220
1221
1222 /*
1223  * do a dmesg output
1224  */
1225 static void
1226 doDmesg( void )
1227 {
1228     if (verbose) puts( SEP_LINE );
1229
1230     printf( "dmesg output:\n\n" );
1231     fflush( stdout );
1232     system( "dmesg" );
1233 }
1234
1235
1236 /*
1237  *  build "options" entries for the kernel config file
1238  */
1239 static void
1240 doOptionList( void )
1241 {
1242     if (verbose) puts( SEP_LINE );
1243
1244     printf( "# SMP kernel config file options:\n\n" );
1245     printf( "\n# Required:\n" );
1246     printf( "options            SMP\t\t\t# Symmetric MultiProcessor Kernel\n" );
1247     printf( "options            APIC_IO\t\t\t# Symmetric (APIC) I/O\n" );
1248
1249     printf( "\n# Optional (built-in defaults will work in most cases):\n" );
1250     printf( "#options           NCPU=%d\t\t\t# number of CPUs\n", ncpu );
1251     printf( "#options           NBUS=%d\t\t\t# number of busses\n", nbus );
1252     printf( "#options           NAPIC=%d\t\t\t# number of IO APICs\n", napic );
1253     printf( "#options           NINTR=%d\t\t# number of INTs\n",
1254                 (nintr < 24) ? 24 : nintr );
1255 }
1256
1257
1258 /*
1259  *
1260  */
1261 static void
1262 pnstr( uint8_t* s, int c )
1263 {
1264     uint8_t string[ MAXPNSTR + 1 ];
1265
1266     if ( c > MAXPNSTR )
1267         c = MAXPNSTR;
1268     strncpy( (char *)string, (char *)s, c );
1269     string[ c ] = '\0';
1270     printf( "%s", string );
1271 }