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