Factor out common mptable code to mptable_init().
[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 "       /* There is no extension information... */",
324 "",
325 "       /* Compute the checksums */",
326 "       mc->mpe_checksum = smp_compute_checksum(smp_next_mpc_entry(mc), mc->mpe_length);",
327 "       mc->mpc_checksum = smp_compute_checksum(mc, mc->mpc_length);",
328 "       printk(BIOS_DEBUG, \"Wrote the mp table end at: %p - %p\\n\",",
329 "               mc, smp_next_mpe_entry(mc));",
330 "       return smp_next_mpe_entry(mc);",
331 "}",
332 "",
333 "unsigned long write_smp_table(unsigned long addr)",
334 "{",
335 "       void *v;",
336 "       v = smp_write_floating_table(addr);",
337 "       return (unsigned long)smp_write_config_table(v);",
338 "}",
339 0
340 };
341
342 char *ioapic_code[] = {
343 "       smp_write_ioapic(mc, 2, 0x20, IO_APIC_ADDR);",
344 "       {",
345 "               device_t dev;",
346 "               struct resource *res;",
347 "               dev = dev_find_slot(1, PCI_DEVFN(0x1e,0));",
348 "               if (dev) {",
349 "                       res = find_resource(dev, PCI_BASE_ADDRESS_0);",
350 "                       if (res) {",
351 "                               smp_write_ioapic(mc, 3, 0x20, res->base);",
352 "                       }",
353 "               }",
354 "               dev = dev_find_slot(1, PCI_DEVFN(0x1c,0));",
355 "               if (dev) {",
356 "                       res = find_resource(dev, PCI_BASE_ADDRESS_0);",
357 "                       if (res) {",
358 "                               smp_write_ioapic(mc, 4, 0x20, res->base);",
359 "                       }",
360 "               }",
361 "                dev = dev_find_slot(4, PCI_DEVFN(0x1e,0));",
362 "                if (dev) {",
363 "                       res = find_resource(dev, PCI_BASE_ADDRESS_0);",
364 "                       if (res) {",
365 "                               smp_write_ioapic(mc, 5, 0x20, res->base);",
366 "                       }",
367 "                }",
368 "                dev = dev_find_slot(4, PCI_DEVFN(0x1c,0));",
369 "                if (dev) {",
370 "                       res = find_resource(dev, PCI_BASE_ADDRESS_0);",
371 "                       if (res) {",
372 "                               smp_write_ioapic(mc, 8, 0x20, res->base);",
373 "                       }",
374 "                }",
375 "       }",
376 0
377 };
378 static void
379 usage( void )
380 {
381     fprintf( stderr, "usage: mptable [-dmesg] [-verbose] [-grope] [-help]\n" );
382     exit( 0 );
383 }
384
385 void
386 write_code(char **code)
387 {
388   while (*code)
389         printf("%s\n", *code++);
390 }
391 /*
392  *
393  */
394 int
395 main( int argc, char *argv[] )
396 {
397     vm_offset_t paddr;
398     int         where;
399     mpfps_t     mpfps;
400     int         defaultConfig;
401
402     /* announce ourselves */
403
404     if (verbose) puts( SEP_LINE2 );
405
406     printf( "/* generated by MPTable, version %d.%d.%d*/\n", VMAJOR, VMINOR, VDELTA );
407     printf("/* as modified by RGM for coreboot */\n");
408     write_code(preamble);
409
410     /* Ron hates getopt() */
411
412     for(argc--, argv++; argc; argc--, argv++){
413       if ( strcmp( argv[0], "-dmesg") == 0 ) {
414         dmesg = 1;
415       } else
416         if ( strcmp( argv[0], "-help") == 0 )
417           {
418             usage();
419           } else
420             if ( strcmp( argv[0], "-grope") == 0 ){
421               grope = 1;
422             } else  if ( strcmp( argv[0], "-verbose") == 0 )
423               verbose = 1;
424             else  if ( strcmp( argv[0], "-noisy") == 0 )
425               noisy = 1;
426             else usage();
427     }
428
429     /* open physical memory for access to MP structures */
430     if ( (pfd = open( "/dev/mem", O_RDONLY )) < 0 )
431         err( 1, "mem open" );
432
433     /* probe for MP structures */
434     apic_probe( &paddr, &where );
435     if ( where <= 0 ) {
436         fprintf( stderr, "\n MP FPS NOT found,\n" );
437         fprintf( stderr, " suggest trying -grope option!!!\n\n" );
438         return 1;
439     }
440
441     if ( verbose )
442         printf( "\n MP FPS found in %s @ physical addr: 0x%08x\n",
443               whereStrings[ where - 1 ], paddr );
444
445     if (verbose) puts( SEP_LINE );
446
447     /* analyze the MP Floating Pointer Structure */
448     MPFloatingPointer( paddr, where, &mpfps );
449
450     if (verbose) puts( SEP_LINE );
451
452     /* check whether an MP config table exists */
453     if ( (defaultConfig = mpfps.mpfb1) )
454         MPConfigDefault( defaultConfig );
455     else
456         MPConfigTableHeader( mpfps.pap );
457
458     /* build "options" entries for the kernel config file */
459     if (noisy)
460       doOptionList();
461
462     write_code(postamble);
463     /* do a dmesg output */
464     if ( dmesg )
465         doDmesg();
466
467     if (verbose) puts( SEP_LINE2 );
468
469     return 0;
470 }
471
472
473 /*
474  * set PHYSICAL address of MP floating pointer structure
475  */
476 #define NEXT(X)         ((X) += 4)
477 static void
478 apic_probe( vm_offset_t* paddr, int* where )
479 {
480     /*
481      * c rewrite of apic_probe() by Jack F. Vogel
482      */
483
484     int         x;
485     u_short     segment;
486     vm_offset_t target;
487     u_int       buffer[ BIOS_SIZE / sizeof( int ) ];
488
489     if ( verbose )
490         printf( "\n" );
491
492     /* search Extended Bios Data Area, if present */
493     if ( verbose )
494         printf( " looking for EBDA pointer @ 0x%04x, ", EBDA_POINTER );
495     seekEntry( (vm_offset_t)EBDA_POINTER );
496     readEntry( &segment, 2 );
497     if ( segment ) {                /* search EBDA */
498         target = (vm_offset_t)segment << 4;
499         if ( verbose )
500             printf( "found, searching EBDA @ 0x%08x\n", target );
501         seekEntry( target );
502         readEntry( buffer, ONE_KBYTE );
503
504         for ( x = 0; x < ONE_KBYTE / sizeof ( unsigned int ); NEXT(x) ) {
505             if ( buffer[ x ] == MP_SIG ) {
506                 *where = 1;
507                 *paddr = (x * sizeof( unsigned int )) + target;
508                 return;
509             }
510         }
511     }
512     else {
513         if ( verbose )
514             printf( "NOT found\n" );
515     }
516
517     target = 0;
518     segment = 0;
519     if ( verbose )
520         printf( " searching for coreboot MP table  @ 0x%08x (%dK)\n",
521                 target, segment );
522     seekEntry( target );
523     readEntry( buffer, ONE_KBYTE );
524
525     for ( x = 0; x < ONE_KBYTE / sizeof ( unsigned int ); NEXT(x) ) {
526         if ( buffer[ x ] == MP_SIG ) {
527             *where = 2;
528             *paddr = (x * sizeof( unsigned int )) + target;
529             return;
530         }
531     }
532
533
534     /* read CMOS for real top of mem */
535     seekEntry( (vm_offset_t)TOPOFMEM_POINTER );
536     readEntry( &segment, 2 );
537     --segment;                                          /* less ONE_KBYTE */
538     target = segment * 1024;
539     if ( verbose )
540         printf( " searching CMOS 'top of mem' @ 0x%08x (%dK)\n",
541                 target, segment );
542     seekEntry( target );
543     readEntry( buffer, ONE_KBYTE );
544
545     for ( x = 0; x < ONE_KBYTE / sizeof ( unsigned int ); NEXT(x) ) {
546         if ( buffer[ x ] == MP_SIG ) {
547             *where = 2;
548             *paddr = (x * sizeof( unsigned int )) + target;
549             return;
550         }
551     }
552
553     /* we don't necessarily believe CMOS, check base of the last 1K of 640K */
554     if ( target != (DEFAULT_TOPOFMEM - 1024)) {
555         target = (DEFAULT_TOPOFMEM - 1024);
556         if ( verbose )
557             printf( " searching default 'top of mem' @ 0x%08x (%dK)\n",
558                     target, (target / 1024) );
559         seekEntry( target );
560         readEntry( buffer, ONE_KBYTE );
561
562         for ( x = 0; x < ONE_KBYTE / sizeof ( unsigned int ); NEXT(x) ) {
563             if ( buffer[ x ] == MP_SIG ) {
564                 *where = 3;
565                 *paddr = (x * sizeof( unsigned int )) + target;
566                 return;
567             }
568         }
569     }
570
571     /* search the BIOS */
572     if ( verbose )
573         printf( " searching BIOS @ 0x%08x\n", BIOS_BASE );
574     seekEntry( BIOS_BASE );
575     readEntry( buffer, BIOS_SIZE );
576
577     for ( x = 0; x < BIOS_SIZE / sizeof( unsigned int ); NEXT(x) ) {
578         if ( buffer[ x ] == MP_SIG ) {
579             *where = 4;
580             *paddr = (x * sizeof( unsigned int )) + BIOS_BASE;
581             return;
582         }
583     }
584
585     /* search the extended BIOS */
586     if ( verbose )
587         printf( " searching extended BIOS @ 0x%08x\n", BIOS_BASE2 );
588     seekEntry( BIOS_BASE2 );
589     readEntry( buffer, BIOS_SIZE );
590
591     for ( x = 0; x < BIOS_SIZE / sizeof( unsigned int ); NEXT(x) ) {
592         if ( buffer[ x ] == MP_SIG ) {
593             *where = 5;
594             *paddr = (x * sizeof( unsigned int )) + BIOS_BASE2;
595             return;
596         }
597     }
598
599     if ( grope ) {
600         /* search additional memory */
601         target = GROPE_AREA1;
602         if ( verbose )
603             printf( " groping memory @ 0x%08x\n", target );
604         seekEntry( target );
605         readEntry( buffer, GROPE_SIZE );
606
607         for ( x = 0; x < GROPE_SIZE / sizeof( unsigned int ); NEXT(x) ) {
608             if ( buffer[ x ] == MP_SIG ) {
609                 *where = 6;
610                 *paddr = (x * sizeof( unsigned int )) + GROPE_AREA1;
611                 return;
612             }
613         }
614
615         target = GROPE_AREA2;
616         if ( verbose )
617             printf( " groping memory @ 0x%08x\n", target );
618         seekEntry( target );
619         readEntry( buffer, GROPE_SIZE );
620
621         for ( x = 0; x < GROPE_SIZE / sizeof( unsigned int ); NEXT(x) ) {
622             if ( buffer[ x ] == MP_SIG ) {
623                 *where = 7;
624                 *paddr = (x * sizeof( unsigned int )) + GROPE_AREA2;
625                 return;
626             }
627         }
628     }
629
630     *where = 0;
631     *paddr = (vm_offset_t)0;
632 }
633
634
635 /*
636  *
637  */
638 static void
639 MPFloatingPointer( vm_offset_t paddr, int where, mpfps_t* mpfps )
640 {
641
642     /* read in mpfps structure*/
643     seekEntry( paddr );
644     readEntry( mpfps, sizeof( mpfps_t ) );
645
646     /* show its contents */
647     if (verbose) {
648     printf( "MP Floating Pointer Structure:\n\n" );
649
650     printf( "  location:\t\t\t" );
651     switch ( where )
652     {
653     case 1:
654         printf( "EBDA\n" );
655         break;
656     case 2:
657         printf( "BIOS base memory\n" );
658         break;
659     case 3:
660         printf( "DEFAULT base memory (639K)\n" );
661         break;
662     case 4:
663         printf( "BIOS\n" );
664         break;
665     case 5:
666         printf( "Extended BIOS\n" );
667         break;
668
669     case 0:
670         printf( "NOT found!\n" );
671         exit( 1 );
672     default:
673         printf( "BOGUS!\n" );
674         exit( 1 );
675     }
676     printf( "  physical address:\t\t0x%08x\n", paddr );
677
678     printf( "  signature:\t\t\t'" );
679     pnstr( mpfps->signature, 4 );
680     printf( "'\n" );
681
682     printf( "  length:\t\t\t%d bytes\n", mpfps->length * 16 );
683     printf( "  version:\t\t\t1.%1d\n", mpfps->spec_rev );
684     printf( "  checksum:\t\t\t0x%02x\n", mpfps->checksum );
685
686     /* bits 0:6 are RESERVED */
687     if ( mpfps->mpfb2 & 0x7f ) {
688         printf( " warning, MP feature byte 2: 0x%02x\n", mpfps->mpfb2 );
689     }
690
691     /* bit 7 is IMCRP */
692     printf( "  mode:\t\t\t\t%s\n", (mpfps->mpfb2 & 0x80) ?
693             "PIC" : "Virtual Wire" );
694
695     /* MP feature bytes 3-5 are expected to be ZERO */
696     if ( mpfps->mpfb3 )
697         printf( " warning, MP feature byte 3 NONZERO!\n" );
698     if ( mpfps->mpfb4 )
699         printf( " warning, MP feature byte 4 NONZERO!\n" );
700     if ( mpfps->mpfb5 )
701         printf( " warning, MP feature byte 5 NONZERO!\n" );
702     }
703 }
704
705
706 /*
707  *
708  */
709 static void
710 MPConfigDefault( int featureByte )
711 {
712     printf( "  MP default config type: %d\n\n", featureByte );
713     switch ( featureByte ) {
714     case 1:
715         printf( "   bus: ISA, APIC: 82489DX\n" );
716         break;
717     case 2:
718         printf( "   bus: EISA, APIC: 82489DX\n" );
719         break;
720     case 3:
721         printf( "   bus: EISA, APIC: 82489DX\n" );
722         break;
723     case 4:
724         printf( "   bus: MCA, APIC: 82489DX\n" );
725         break;
726     case 5:
727         printf( "   bus: ISA+PCI, APIC: Integrated\n" );
728         break;
729     case 6:
730         printf( "   bus: EISA+PCI, APIC: Integrated\n" );
731         break;
732     case 7:
733         printf( "   bus: MCA+PCI, APIC: Integrated\n" );
734         break;
735     default:
736         printf( "   future type\n" );
737         break;
738     }
739
740     switch ( featureByte ) {
741     case 1:
742     case 2:
743     case 3:
744     case 4:
745         nbus = 1;
746         break;
747     case 5:
748     case 6:
749     case 7:
750         nbus = 2;
751         break;
752     default:
753         printf( "   future type\n" );
754         break;
755     }
756
757     ncpu = 2;
758     napic = 1;
759     nintr = 16;
760 }
761
762
763 /*
764  *
765  */
766 static void
767 MPConfigTableHeader( uint32_t pap )
768 {
769     vm_offset_t paddr;
770     mpcth_t     cth;
771     int         x;
772     int         totalSize, t;
773     int         count, c;
774     int         type;
775
776     if ( pap == 0 ) {
777         printf( "MP Configuration Table Header MISSING!\n" );
778         exit( 1 );
779     }
780
781     /* convert physical address to virtual address */
782     paddr = (vm_offset_t)pap;
783
784     /* read in cth structure */
785     seekEntry( paddr );
786     readEntry( &cth, sizeof( cth ) );
787
788     if (verbose) {
789     printf( "MP Config Table Header:\n\n" );
790
791     printf( "  physical address:\t\t0x%08x\n", pap );
792
793     printf( "  signature:\t\t\t'" );
794     pnstr( cth.signature, 4 );
795     printf( "'\n" );
796
797     printf( "  base table length:\t\t%d\n", cth.base_table_length );
798
799     printf( "  version:\t\t\t1.%1d\n", cth.spec_rev );
800     printf( "  checksum:\t\t\t0x%02x\n", cth.checksum );
801
802     printf( "  OEM ID:\t\t\t'" );
803     pnstr( cth.oem_id, 8 );
804     printf( "'\n" );
805
806     printf( "  Product ID:\t\t\t'" );
807     pnstr( cth.product_id, 12 );
808     printf( "'\n" );
809
810     printf( "  OEM table pointer:\t\t0x%08x\n", cth.oem_table_pointer );
811     printf( "  OEM table size:\t\t%d\n", cth.oem_table_size );
812
813     printf( "  entry count:\t\t\t%d\n", cth.entry_count );
814
815     printf( "  local APIC address:\t\t0x%08x\n", cth.apic_address );
816
817     printf( "  extended table length:\t%d\n", cth.extended_table_length );
818     printf( "  extended table checksum:\t%d\n", cth.extended_table_checksum );
819     }
820
821     totalSize = cth.base_table_length - sizeof( struct MPCTH );
822     count = cth.entry_count;
823
824     if (verbose) {
825     if (verbose) puts( SEP_LINE );
826
827     printf( "MP Config Base Table Entries:\n\n" );
828     }
829
830     /* initialze tables */
831     for ( x = 0; x < 16; ++x ) {
832         busses[ x ] = apics[ x ] = 0xff;
833     }
834
835     ncpu = 0;
836     nbus = 0;
837     napic = 0;
838     nintr = 0;
839
840     /* process all the CPUs */
841     if (verbose) {
842     printf( "--\nProcessors:\tAPIC ID\tVersion\tState"
843             "\t\tFamily\tModel\tStep\tFlags\n" );
844     }
845     for ( t = totalSize, c = count; c; c-- ) {
846         if ( readType() == 0 )
847             processorEntry();
848         totalSize -= basetableEntryTypes[ 0 ].length;
849     }
850
851     /* process all the busses */
852     printf( "/*Bus:\t\tBus ID\tType*/\n" );
853     for ( t = totalSize, c = count; c; c-- ) {
854         if ( readType() == 1 )
855             busEntry();
856         totalSize -= basetableEntryTypes[ 1 ].length;
857     }
858
859     /* process all the apics */
860     printf( "/*I/O APICs:\tAPIC ID\tVersion\tState\t\tAddress*/\n" );
861     for ( t = totalSize, c = count; c; c-- ) {
862         if ( readType() == 2 )
863             ioApicEntry();
864         totalSize -= basetableEntryTypes[ 2 ].length;
865     }
866
867     write_code(ioapic_code);
868
869     /* process all the I/O Ints */
870     printf( "/*I/O Ints:\tType\tPolarity    Trigger\tBus ID\t IRQ\tAPIC ID\tPIN#\n*/" );
871     for ( t = totalSize, c = count; c; c-- ) {
872         if ( readType() == 3 )
873             intEntry();
874         totalSize -= basetableEntryTypes[ 3 ].length;
875     }
876
877     /* process all the Local Ints */
878     printf( "/*Local Ints:\tType\tPolarity    Trigger\tBus ID\t IRQ\tAPIC ID\tPIN#*/\n" );
879     for ( t = totalSize, c = count; c; c-- ) {
880         if ( readType() == 4 )
881             lintEntry();
882         totalSize -= basetableEntryTypes[ 4 ].length;
883     }
884
885
886 #if defined( EXTENDED_PROCESSING_READY )
887     /* process any extended data */
888     if ( (totalSize = cth.extended_table_length) ) {
889         if (verbose) puts( SEP_LINE );
890
891         printf( "MP Config Extended Table Entries:\n\n" );
892
893         while ( totalSize > 0 ) {
894             switch ( type = readType() ) {
895             case 128:
896                 sasEntry();
897                 break;
898             case 129:
899                 bhdEntry();
900                 break;
901             case 130:
902                 cbasmEntry();
903                 break;
904             default:
905                 printf( "Extended Table HOSED!\n" );
906                 exit( 1 );
907             }
908
909             totalSize -= extendedtableEntryTypes[ type-128 ].length;
910         }
911     }
912 #endif  /* EXTENDED_PROCESSING_READY */
913
914     /* process any OEM data */
915     if ( cth.oem_table_pointer && (cth.oem_table_size > 0) ) {
916 #if defined( OEM_PROCESSING_READY )
917 # error your on your own here!
918         /* convert OEM table pointer to virtual address */
919         poemtp = (vm_offset_t)cth.oem_table_pointer;
920
921         /* read in oem table structure */
922         if ( (oemdata = (void*)malloc( cth.oem_table_size )) == NULL )
923             err( 1, "oem malloc" );
924
925         seekEntry( poemtp );
926         readEntry( oemdata, cth.oem_table_size );
927
928         /** process it */
929
930         free( oemdata );
931 #else
932         printf( "\nyou need to modify the source to handle OEM data!\n\n" );
933 #endif  /* OEM_PROCESSING_READY */
934     }
935
936     fflush( stdout );
937
938 #if defined( RAW_DUMP )
939 {
940     int         ofd;
941     u_char      dumpbuf[ 4096 ];
942
943     ofd = open( "/tmp/mpdump", O_CREAT | O_RDWR );
944     seekEntry( paddr );
945     readEntry( dumpbuf, 1024 );
946     write( ofd, dumpbuf, 1024 );
947     close( ofd );
948 }
949 #endif /* RAW_DUMP */
950 }
951
952
953 /*
954  *
955  */
956 static int
957 readType( void )
958 {
959     u_char      type;
960
961     if ( read( pfd, &type, sizeof( u_char ) ) != sizeof( u_char ) )
962         err( 1, "type read; pfd: %d", pfd );
963
964     if ( lseek( pfd, -1, SEEK_CUR ) < 0 )
965         err( 1, "type seek" );
966
967     return (int)type;
968 }
969
970
971 /*
972  *
973  */
974 static void
975 seekEntry( vm_offset_t addr )
976 {
977     if ( lseek( pfd, (off_t)addr, SEEK_SET ) < 0 )
978         err( 1, "/dev/mem seek" );
979 }
980
981
982 /*
983  *
984  */
985 static void
986 readEntry( void* entry, int size )
987 {
988     if ( read( pfd, entry, size ) != size )
989         err( 1, "readEntry" );
990 }
991
992
993 static void
994 processorEntry( void )
995 {
996     ProcEntry   entry;
997
998     /* read it into local memory */
999     readEntry( &entry, sizeof( entry ) );
1000
1001     /* count it */
1002     ++ncpu;
1003     if (noisy) {
1004     printf( "\t\t%2d", entry.apicID );
1005     printf( "\t 0x%2x", entry.apicVersion );
1006
1007     printf( "\t %s, %s",
1008             (entry.cpuFlags & PROCENTRY_FLAG_BP) ? "BSP" : "AP",
1009             (entry.cpuFlags & PROCENTRY_FLAG_EN) ? "usable" : "unusable" );
1010
1011     printf( "\t %d\t %d\t %d",
1012             (entry.cpuSignature >> 8) & 0x0f,
1013             (entry.cpuSignature >> 4) & 0x0f,
1014             entry.cpuSignature & 0x0f );
1015
1016     printf( "\t 0x%04x\n", entry.featureFlags );
1017     }
1018 }
1019
1020
1021 /*
1022  *
1023  */
1024 static int
1025 lookupBusType( char* name )
1026 {
1027     int x;
1028
1029     for ( x = 0; x < MAX_BUSTYPE; ++x )
1030         if ( strcmp( busTypeTable[ x ].name, name ) == 0 )
1031             return busTypeTable[ x ].type;
1032
1033     return UNKNOWN_BUSTYPE;
1034 }
1035
1036
1037 static void
1038 busEntry( void )
1039 {
1040     int         x;
1041     char        name[ 8 ];
1042     char        c;
1043     BusEntry    entry;
1044
1045     /* read it into local memory */
1046     readEntry( &entry, sizeof( entry ) );
1047
1048     /* count it */
1049     ++nbus;
1050
1051     if (verbose)  {
1052     printf( "\t\t%2d", entry.busID );
1053     printf( "\t " ); pnstr( entry.busType, 6 ); printf( "\n" );
1054     }
1055
1056     for ( x = 0; x < 6; ++x ) {
1057 //      if ( (c = entry.busType[ x ]) == ' ' )
1058             //break;
1059         name[ x ] = c;
1060     }
1061     name[ x ] = '\0';
1062     busses[ entry.busID ] = lookupBusType( name );
1063     printf("\tsmp_write_bus(mc, %d, \"", entry.busID);
1064     pnstr( entry.busType, 6 );
1065     printf("\");\n");
1066 }
1067
1068
1069 static void
1070 ioApicEntry( void )
1071 {
1072     IOApicEntry entry;
1073
1074     /* read it into local memory */
1075     readEntry( &entry, sizeof( entry ) );
1076
1077     /* count it */
1078     ++napic;
1079
1080     if (noisy) {
1081     printf( "\t\t%2d", entry.apicID );
1082     printf( "\t 0x%02x", entry.apicVersion );
1083     printf( "\t %s",
1084             (entry.apicFlags & IOAPICENTRY_FLAG_EN) ? "usable" : "unusable" );
1085     printf( "\t\t 0x%x\n", entry.apicAddress );
1086     }
1087
1088     apics[ entry.apicID ] = entry.apicID;
1089
1090     // the numbering and setup of ioapics is so irrational
1091     // that for now we will punt.
1092 #if 0
1093     if (entry.apicFlags & IOAPICENTRY_FLAG_EN)
1094       printf("\tsmp_write_ioapic(mc, 0x%x, 0x%x, 0x%x);\n",
1095              entry.apicID, entry.apicVersion, entry.apicAddress);
1096 #endif
1097
1098 }
1099
1100
1101 char* intTypes[] = {
1102     "mp_INT", "mp_NMI", "mp_SMI", "mp_ExtINT"
1103 };
1104
1105 char* polarityMode[] = {
1106     "MP_IRQ_POLARITY_DEFAULT", "MP_IRQ_POLARITY_HIGH", "reserved", "MP_IRQ_POLARITY_LOW"
1107 };
1108 char* triggerMode[] = {
1109     "MP_IRQ_TRIGGER_DEFAULT", "MP_IRQ_TRIGGER_EDGE", "reserved", "MP_IRQ_TRIGGER_LEVEL"
1110 };
1111
1112 static void
1113 intEntry( void )
1114 {
1115     IntEntry    entry;
1116
1117     /* read it into local memory */
1118     readEntry( &entry, sizeof( entry ) );
1119
1120     /* count it */
1121     if ( (int)entry.type == 3 )
1122         ++nintr;
1123
1124     if (noisy) {
1125     printf( "\n\t\t%s", intTypes[ (int)entry.intType ] );
1126
1127     printf( "\t%9s", polarityMode[ (int)entry.intFlags & 0x03 ] );
1128     printf( "%12s", triggerMode[ ((int)entry.intFlags >> 2) & 0x03 ] );
1129
1130     printf( "\t %5d", (int)entry.srcBusID );
1131     if ( busses[ (int)entry.srcBusID ] == PCI )
1132         printf( "\t%2d:%c",
1133                 ((int)entry.srcBusIRQ >> 2) & 0x1f,
1134                 ((int)entry.srcBusIRQ & 0x03) + 'A' );
1135     else
1136         printf( "\t 0x%x:0x%x(0x%x)",
1137                                 (int)entry.srcBusIRQ>>2,
1138                                 (int)entry.srcBusIRQ & 3,
1139                                 (int)entry.srcBusIRQ );
1140     printf( "\t %6d", (int)entry.dstApicID );
1141     printf( "\t %3d\n", (int)entry.dstApicINT );
1142     }
1143     printf("\tsmp_write_intsrc(mc, %s, %s|%s, 0x%x, 0x%x, 0x%x, 0x%x);\n",
1144            intTypes[ (int)entry.intType ],
1145            triggerMode[ ((int)entry.intFlags >> 2) & 0x03 ] ,
1146            polarityMode[ (int)entry.intFlags & 0x03 ],
1147            (int)entry.srcBusID,
1148            (int)entry.srcBusIRQ,
1149            (int)entry.dstApicID ,
1150            (int)entry.dstApicINT );
1151
1152 }
1153
1154 static void
1155 lintEntry( void )
1156 {
1157     IntEntry    entry;
1158
1159     /* read it into local memory */
1160     readEntry( &entry, sizeof( entry ) );
1161
1162     /* count it */
1163     if ( (int)entry.type == 3 )
1164         ++nintr;
1165
1166     if (noisy) {
1167     printf( "\t\t%s", intTypes[ (int)entry.intType ] );
1168
1169     printf( "\t%9s", polarityMode[ (int)entry.intFlags & 0x03 ] );
1170     printf( "%12s", triggerMode[ ((int)entry.intFlags >> 2) & 0x03 ] );
1171
1172     printf( "\t %5d", (int)entry.srcBusID );
1173     if ( busses[ (int)entry.srcBusID ] == PCI )
1174         printf( "\t%2d:%c",
1175                 ((int)entry.srcBusIRQ >> 2) & 0x1f,
1176                 ((int)entry.srcBusIRQ & 0x03) + 'A' );
1177     else
1178         printf( "\t %3d", (int)entry.srcBusIRQ );
1179     printf( "\t %6d", (int)entry.dstApicID );
1180     printf( "\t %3d\n", (int)entry.dstApicINT );
1181     }
1182     printf("\tsmp_write_intsrc(mc, %s, %s|%s, 0x%x, 0x%x, MP_APIC_ALL, 0x%x);\n",
1183            intTypes[ (int)entry.intType ],
1184            triggerMode[ ((int)entry.intFlags >> 2) & 0x03 ] ,
1185            polarityMode[ (int)entry.intFlags & 0x03 ],
1186            (int)entry.srcBusID,
1187            (int)entry.srcBusIRQ,
1188            (int)entry.dstApicINT );
1189
1190 }
1191
1192
1193 static void
1194 sasEntry( void )
1195 {
1196     SasEntry    entry;
1197
1198     /* read it into local memory */
1199     readEntry( &entry, sizeof( entry ) );
1200
1201     printf( "--\n%s\n", extendedtableEntryTypes[ entry.type - 128 ].name );
1202     printf( " bus ID: %d", entry.busID );
1203     printf( " address type: " );
1204     switch ( entry.addressType ) {
1205     case 0:
1206         printf( "I/O address\n" );
1207         break;
1208     case 1:
1209         printf( "memory address\n" );
1210         break;
1211     case 2:
1212         printf( "prefetch address\n" );
1213         break;
1214     default:
1215         printf( "UNKNOWN type\n" );
1216         break;
1217     }
1218
1219     printf( " address base: 0x%lx\n", entry.addressBase );
1220     printf( " address range: 0x%lx\n", entry.addressLength );
1221 }
1222
1223
1224 static void
1225 bhdEntry( void )
1226 {
1227     BhdEntry    entry;
1228
1229     /* read it into local memory */
1230     readEntry( &entry, sizeof( entry ) );
1231
1232     printf( "--\n%s\n", extendedtableEntryTypes[ entry.type - 128 ].name );
1233     printf( " bus ID: %d", entry.busID );
1234     printf( " bus info: 0x%02x", entry.busInfo );
1235     printf( " parent bus ID: %d", entry.busParent );
1236 }
1237
1238
1239 static void
1240 cbasmEntry( void )
1241 {
1242     CbasmEntry  entry;
1243
1244     /* read it into local memory */
1245     readEntry( &entry, sizeof( entry ) );
1246
1247     printf( "--\n%s\n", extendedtableEntryTypes[ entry.type - 128 ].name );
1248     printf( " bus ID: %d", entry.busID );
1249     printf( " address modifier: %s\n", (entry.addressMod & 0x01) ?
1250                                         "subtract" : "add" );
1251     printf( " predefined range: 0x%08x", entry.predefinedRange );
1252 }
1253
1254
1255 /*
1256  * do a dmesg output
1257  */
1258 static void
1259 doDmesg( void )
1260 {
1261     if (verbose) puts( SEP_LINE );
1262
1263     printf( "dmesg output:\n\n" );
1264     fflush( stdout );
1265     system( "dmesg" );
1266 }
1267
1268
1269 /*
1270  *  build "options" entries for the kernel config file
1271  */
1272 static void
1273 doOptionList( void )
1274 {
1275     if (verbose) puts( SEP_LINE );
1276
1277     printf( "# SMP kernel config file options:\n\n" );
1278     printf( "\n# Required:\n" );
1279     printf( "options            SMP\t\t\t# Symmetric MultiProcessor Kernel\n" );
1280     printf( "options            APIC_IO\t\t\t# Symmetric (APIC) I/O\n" );
1281
1282     printf( "\n# Optional (built-in defaults will work in most cases):\n" );
1283     printf( "#options           NCPU=%d\t\t\t# number of CPUs\n", ncpu );
1284     printf( "#options           NBUS=%d\t\t\t# number of busses\n", nbus );
1285     printf( "#options           NAPIC=%d\t\t\t# number of IO APICs\n", napic );
1286     printf( "#options           NINTR=%d\t\t# number of INTs\n",
1287                 (nintr < 24) ? 24 : nintr );
1288 }
1289
1290
1291 /*
1292  *
1293  */
1294 static void
1295 pnstr( uint8_t* s, int c )
1296 {
1297     uint8_t string[ MAXPNSTR + 1 ];
1298
1299     if ( c > MAXPNSTR )
1300         c = MAXPNSTR;
1301     strncpy( (char *)string, (char *)s, c );
1302     string[ c ] = '\0';
1303     printf( "%s", string );
1304 }