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