]> err.no Git - linux-2.6/blob - arch/x86/kernel/mpparse.c
x86: update mptable
[linux-2.6] / arch / x86 / kernel / mpparse.c
1 /*
2  *      Intel Multiprocessor Specification 1.1 and 1.4
3  *      compliant MP-table parsing routines.
4  *
5  *      (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
6  *      (c) 1998, 1999, 2000 Ingo Molnar <mingo@redhat.com>
7  *      (c) 2008 Alexey Starikovskiy <astarikovskiy@suse.de>
8  */
9
10 #include <linux/mm.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/bootmem.h>
14 #include <linux/kernel_stat.h>
15 #include <linux/mc146818rtc.h>
16 #include <linux/bitops.h>
17 #include <linux/acpi.h>
18 #include <linux/module.h>
19
20 #include <asm/smp.h>
21 #include <asm/mtrr.h>
22 #include <asm/mpspec.h>
23 #include <asm/pgalloc.h>
24 #include <asm/io_apic.h>
25 #include <asm/proto.h>
26 #include <asm/acpi.h>
27 #include <asm/bios_ebda.h>
28 #include <asm/e820.h>
29 #include <asm/trampoline.h>
30
31 #include <mach_apic.h>
32 #ifdef CONFIG_X86_32
33 #include <mach_apicdef.h>
34 #include <mach_mpparse.h>
35 #endif
36
37 /*
38  * Checksum an MP configuration block.
39  */
40
41 static int __init mpf_checksum(unsigned char *mp, int len)
42 {
43         int sum = 0;
44
45         while (len--)
46                 sum += *mp++;
47
48         return sum & 0xFF;
49 }
50
51 #ifdef CONFIG_X86_NUMAQ
52 /*
53  * Have to match translation table entries to main table entries by counter
54  * hence the mpc_record variable .... can't see a less disgusting way of
55  * doing this ....
56  */
57
58 static int mpc_record;
59 static struct mpc_config_translation *translation_table[MAX_MPC_ENTRY]
60     __cpuinitdata;
61 #endif
62
63 static void __cpuinit MP_processor_info(struct mpc_config_processor *m)
64 {
65         int apicid;
66         char *bootup_cpu = "";
67
68         if (!(m->mpc_cpuflag & CPU_ENABLED)) {
69                 disabled_cpus++;
70                 return;
71         }
72 #ifdef CONFIG_X86_NUMAQ
73         apicid = mpc_apic_id(m, translation_table[mpc_record]);
74 #else
75         apicid = m->mpc_apicid;
76 #endif
77         if (m->mpc_cpuflag & CPU_BOOTPROCESSOR) {
78                 bootup_cpu = " (Bootup-CPU)";
79                 boot_cpu_physical_apicid = m->mpc_apicid;
80         }
81
82         printk(KERN_INFO "Processor #%d%s\n", m->mpc_apicid, bootup_cpu);
83         generic_processor_info(apicid, m->mpc_apicver);
84 }
85
86 #ifdef CONFIG_X86_IO_APIC
87 static void __init MP_bus_info(struct mpc_config_bus *m)
88 {
89         char str[7];
90         memcpy(str, m->mpc_bustype, 6);
91         str[6] = 0;
92
93 #ifdef CONFIG_X86_NUMAQ
94         mpc_oem_bus_info(m, str, translation_table[mpc_record]);
95 #else
96         printk(KERN_INFO "Bus #%d is %s\n", m->mpc_busid, str);
97 #endif
98
99 #if MAX_MP_BUSSES < 256
100         if (m->mpc_busid >= MAX_MP_BUSSES) {
101                 printk(KERN_WARNING "MP table busid value (%d) for bustype %s "
102                        " is too large, max. supported is %d\n",
103                        m->mpc_busid, str, MAX_MP_BUSSES - 1);
104                 return;
105         }
106 #endif
107
108         if (strncmp(str, BUSTYPE_ISA, sizeof(BUSTYPE_ISA) - 1) == 0) {
109                  set_bit(m->mpc_busid, mp_bus_not_pci);
110 #if defined(CONFIG_EISA) || defined (CONFIG_MCA)
111                 mp_bus_id_to_type[m->mpc_busid] = MP_BUS_ISA;
112 #endif
113         } else if (strncmp(str, BUSTYPE_PCI, sizeof(BUSTYPE_PCI) - 1) == 0) {
114 #ifdef CONFIG_X86_NUMAQ
115                 mpc_oem_pci_bus(m, translation_table[mpc_record]);
116 #endif
117                 clear_bit(m->mpc_busid, mp_bus_not_pci);
118 #if defined(CONFIG_EISA) || defined (CONFIG_MCA)
119                 mp_bus_id_to_type[m->mpc_busid] = MP_BUS_PCI;
120         } else if (strncmp(str, BUSTYPE_EISA, sizeof(BUSTYPE_EISA) - 1) == 0) {
121                 mp_bus_id_to_type[m->mpc_busid] = MP_BUS_EISA;
122         } else if (strncmp(str, BUSTYPE_MCA, sizeof(BUSTYPE_MCA) - 1) == 0) {
123                 mp_bus_id_to_type[m->mpc_busid] = MP_BUS_MCA;
124 #endif
125         } else
126                 printk(KERN_WARNING "Unknown bustype %s - ignoring\n", str);
127 }
128 #endif
129
130 #ifdef CONFIG_X86_IO_APIC
131
132 static int bad_ioapic(unsigned long address)
133 {
134         if (nr_ioapics >= MAX_IO_APICS) {
135                 printk(KERN_ERR "ERROR: Max # of I/O APICs (%d) exceeded "
136                        "(found %d)\n", MAX_IO_APICS, nr_ioapics);
137                 panic("Recompile kernel with bigger MAX_IO_APICS!\n");
138         }
139         if (!address) {
140                 printk(KERN_ERR "WARNING: Bogus (zero) I/O APIC address"
141                        " found in table, skipping!\n");
142                 return 1;
143         }
144         return 0;
145 }
146
147 static void __init MP_ioapic_info(struct mpc_config_ioapic *m)
148 {
149         if (!(m->mpc_flags & MPC_APIC_USABLE))
150                 return;
151
152         printk(KERN_INFO "I/O APIC #%d Version %d at 0x%X.\n",
153                m->mpc_apicid, m->mpc_apicver, m->mpc_apicaddr);
154
155         if (bad_ioapic(m->mpc_apicaddr))
156                 return;
157
158         mp_ioapics[nr_ioapics].mp_apicaddr = m->mpc_apicaddr;
159         mp_ioapics[nr_ioapics].mp_apicid = m->mpc_apicid;
160         mp_ioapics[nr_ioapics].mp_type = m->mpc_type;
161         mp_ioapics[nr_ioapics].mp_apicver = m->mpc_apicver;
162         mp_ioapics[nr_ioapics].mp_flags = m->mpc_flags;
163         nr_ioapics++;
164 }
165
166 static void print_MP_intsrc_info(struct mpc_config_intsrc *m)
167 {
168         printk(KERN_CONT "Int: type %d, pol %d, trig %d, bus %02x,"
169                 " IRQ %02x, APIC ID %x, APIC INT %02x\n",
170                 m->mpc_irqtype, m->mpc_irqflag & 3,
171                 (m->mpc_irqflag >> 2) & 3, m->mpc_srcbus,
172                 m->mpc_srcbusirq, m->mpc_dstapic, m->mpc_dstirq);
173 }
174
175 static void __init print_mp_irq_info(struct mp_config_intsrc *mp_irq)
176 {
177         printk(KERN_CONT "Int: type %d, pol %d, trig %d, bus %02x,"
178                 " IRQ %02x, APIC ID %x, APIC INT %02x\n",
179                 mp_irq->mp_irqtype, mp_irq->mp_irqflag & 3,
180                 (mp_irq->mp_irqflag >> 2) & 3, mp_irq->mp_srcbus,
181                 mp_irq->mp_srcbusirq, mp_irq->mp_dstapic, mp_irq->mp_dstirq);
182 }
183
184 static void assign_to_mp_irq(struct mpc_config_intsrc *m,
185                                     struct mp_config_intsrc *mp_irq)
186 {
187         mp_irq->mp_dstapic = m->mpc_dstapic;
188         mp_irq->mp_type = m->mpc_type;
189         mp_irq->mp_irqtype = m->mpc_irqtype;
190         mp_irq->mp_irqflag = m->mpc_irqflag;
191         mp_irq->mp_srcbus = m->mpc_srcbus;
192         mp_irq->mp_srcbusirq = m->mpc_srcbusirq;
193         mp_irq->mp_dstirq = m->mpc_dstirq;
194 }
195
196 static void __init assign_to_mpc_intsrc(struct mp_config_intsrc *mp_irq,
197                                         struct mpc_config_intsrc *m)
198 {
199         m->mpc_dstapic = mp_irq->mp_dstapic;
200         m->mpc_type = mp_irq->mp_type;
201         m->mpc_irqtype = mp_irq->mp_irqtype;
202         m->mpc_irqflag = mp_irq->mp_irqflag;
203         m->mpc_srcbus = mp_irq->mp_srcbus;
204         m->mpc_srcbusirq = mp_irq->mp_srcbusirq;
205         m->mpc_dstirq = mp_irq->mp_dstirq;
206 }
207
208 static int mp_irq_mpc_intsrc_cmp(struct mp_config_intsrc *mp_irq,
209                                         struct mpc_config_intsrc *m)
210 {
211         if (mp_irq->mp_dstapic != m->mpc_dstapic)
212                 return 1;
213         if (mp_irq->mp_type != m->mpc_type)
214                 return 2;
215         if (mp_irq->mp_irqtype != m->mpc_irqtype)
216                 return 3;
217         if (mp_irq->mp_irqflag != m->mpc_irqflag)
218                 return 4;
219         if (mp_irq->mp_srcbus != m->mpc_srcbus)
220                 return 5;
221         if (mp_irq->mp_srcbusirq != m->mpc_srcbusirq)
222                 return 6;
223         if (mp_irq->mp_dstirq != m->mpc_dstirq)
224                 return 7;
225
226         return 0;
227 }
228
229 void MP_intsrc_info(struct mpc_config_intsrc *m)
230 {
231         int i;
232
233         print_MP_intsrc_info(m);
234
235         for (i = 0; i < mp_irq_entries; i++) {
236                 if (!mp_irq_mpc_intsrc_cmp(&mp_irqs[i], m))
237                         return;
238         }
239
240         assign_to_mp_irq(m, &mp_irqs[mp_irq_entries]);
241         if (++mp_irq_entries == MAX_IRQ_SOURCES)
242                 panic("Max # of irq sources exceeded!!\n");
243 }
244
245 #endif
246
247 static void __init MP_lintsrc_info(struct mpc_config_lintsrc *m)
248 {
249         printk(KERN_INFO "Lint: type %d, pol %d, trig %d, bus %02x,"
250                 " IRQ %02x, APIC ID %x, APIC LINT %02x\n",
251                 m->mpc_irqtype, m->mpc_irqflag & 3,
252                 (m->mpc_irqflag >> 2) & 3, m->mpc_srcbusid,
253                 m->mpc_srcbusirq, m->mpc_destapic, m->mpc_destapiclint);
254 }
255
256 #ifdef CONFIG_X86_NUMAQ
257 static void __init MP_translation_info(struct mpc_config_translation *m)
258 {
259         printk(KERN_INFO
260                "Translation: record %d, type %d, quad %d, global %d, local %d\n",
261                mpc_record, m->trans_type, m->trans_quad, m->trans_global,
262                m->trans_local);
263
264         if (mpc_record >= MAX_MPC_ENTRY)
265                 printk(KERN_ERR "MAX_MPC_ENTRY exceeded!\n");
266         else
267                 translation_table[mpc_record] = m;      /* stash this for later */
268         if (m->trans_quad < MAX_NUMNODES && !node_online(m->trans_quad))
269                 node_set_online(m->trans_quad);
270 }
271
272 /*
273  * Read/parse the MPC oem tables
274  */
275
276 static void __init smp_read_mpc_oem(struct mp_config_oemtable *oemtable,
277                                     unsigned short oemsize)
278 {
279         int count = sizeof(*oemtable);  /* the header size */
280         unsigned char *oemptr = ((unsigned char *)oemtable) + count;
281
282         mpc_record = 0;
283         printk(KERN_INFO "Found an OEM MPC table at %8p - parsing it ... \n",
284                oemtable);
285         if (memcmp(oemtable->oem_signature, MPC_OEM_SIGNATURE, 4)) {
286                 printk(KERN_WARNING
287                        "SMP mpc oemtable: bad signature [%c%c%c%c]!\n",
288                        oemtable->oem_signature[0], oemtable->oem_signature[1],
289                        oemtable->oem_signature[2], oemtable->oem_signature[3]);
290                 return;
291         }
292         if (mpf_checksum((unsigned char *)oemtable, oemtable->oem_length)) {
293                 printk(KERN_WARNING "SMP oem mptable: checksum error!\n");
294                 return;
295         }
296         while (count < oemtable->oem_length) {
297                 switch (*oemptr) {
298                 case MP_TRANSLATION:
299                         {
300                                 struct mpc_config_translation *m =
301                                     (struct mpc_config_translation *)oemptr;
302                                 MP_translation_info(m);
303                                 oemptr += sizeof(*m);
304                                 count += sizeof(*m);
305                                 ++mpc_record;
306                                 break;
307                         }
308                 default:
309                         {
310                                 printk(KERN_WARNING
311                                        "Unrecognised OEM table entry type! - %d\n",
312                                        (int)*oemptr);
313                                 return;
314                         }
315                 }
316         }
317 }
318
319 static inline void mps_oem_check(struct mp_config_table *mpc, char *oem,
320                                  char *productid)
321 {
322         if (strncmp(oem, "IBM NUMA", 8))
323                 printk("Warning!  May not be a NUMA-Q system!\n");
324         if (mpc->mpc_oemptr)
325                 smp_read_mpc_oem((struct mp_config_oemtable *)mpc->mpc_oemptr,
326                                  mpc->mpc_oemsize);
327 }
328 #endif /* CONFIG_X86_NUMAQ */
329
330 /*
331  * Read/parse the MPC
332  */
333
334 static int __init smp_check_mpc(struct mp_config_table *mpc, char *oem,
335                                 char *str)
336 {
337
338         if (memcmp(mpc->mpc_signature, MPC_SIGNATURE, 4)) {
339                 printk(KERN_ERR "MPTABLE: bad signature [%c%c%c%c]!\n",
340                        mpc->mpc_signature[0], mpc->mpc_signature[1],
341                        mpc->mpc_signature[2], mpc->mpc_signature[3]);
342                 return 0;
343         }
344         if (mpf_checksum((unsigned char *)mpc, mpc->mpc_length)) {
345                 printk(KERN_ERR "MPTABLE: checksum error!\n");
346                 return 0;
347         }
348         if (mpc->mpc_spec != 0x01 && mpc->mpc_spec != 0x04) {
349                 printk(KERN_ERR "MPTABLE: bad table version (%d)!!\n",
350                        mpc->mpc_spec);
351                 return 0;
352         }
353         if (!mpc->mpc_lapic) {
354                 printk(KERN_ERR "MPTABLE: null local APIC address!\n");
355                 return 0;
356         }
357         memcpy(oem, mpc->mpc_oem, 8);
358         oem[8] = 0;
359         printk(KERN_INFO "MPTABLE: OEM ID: %s\n", oem);
360
361         memcpy(str, mpc->mpc_productid, 12);
362         str[12] = 0;
363
364         printk(KERN_INFO "MPTABLE: Product ID: %s\n", str);
365
366         printk(KERN_INFO "MPTABLE: APIC at: 0x%X\n", mpc->mpc_lapic);
367
368         return 1;
369 }
370
371 static int __init smp_read_mpc(struct mp_config_table *mpc, unsigned early)
372 {
373         char str[16];
374         char oem[10];
375
376         int count = sizeof(*mpc);
377         unsigned char *mpt = ((unsigned char *)mpc) + count;
378
379         if (!smp_check_mpc(mpc, oem, str))
380                 return 0;
381
382 #ifdef CONFIG_X86_32
383         mps_oem_check(mpc, oem, str);
384 #endif
385
386         /* save the local APIC address, it might be non-default */
387         if (!acpi_lapic)
388                 mp_lapic_addr = mpc->mpc_lapic;
389
390         if (early)
391                 return 1;
392
393         /*
394          *      Now process the configuration blocks.
395          */
396 #ifdef CONFIG_X86_NUMAQ
397         mpc_record = 0;
398 #endif
399         while (count < mpc->mpc_length) {
400                 switch (*mpt) {
401                 case MP_PROCESSOR:
402                         {
403                                 struct mpc_config_processor *m =
404                                     (struct mpc_config_processor *)mpt;
405                                 /* ACPI may have already provided this data */
406                                 if (!acpi_lapic)
407                                         MP_processor_info(m);
408                                 mpt += sizeof(*m);
409                                 count += sizeof(*m);
410                                 break;
411                         }
412                 case MP_BUS:
413                         {
414                                 struct mpc_config_bus *m =
415                                     (struct mpc_config_bus *)mpt;
416 #ifdef CONFIG_X86_IO_APIC
417                                 MP_bus_info(m);
418 #endif
419                                 mpt += sizeof(*m);
420                                 count += sizeof(*m);
421                                 break;
422                         }
423                 case MP_IOAPIC:
424                         {
425 #ifdef CONFIG_X86_IO_APIC
426                                 struct mpc_config_ioapic *m =
427                                     (struct mpc_config_ioapic *)mpt;
428                                 MP_ioapic_info(m);
429 #endif
430                                 mpt += sizeof(struct mpc_config_ioapic);
431                                 count += sizeof(struct mpc_config_ioapic);
432                                 break;
433                         }
434                 case MP_INTSRC:
435                         {
436 #ifdef CONFIG_X86_IO_APIC
437                                 struct mpc_config_intsrc *m =
438                                     (struct mpc_config_intsrc *)mpt;
439
440                                 MP_intsrc_info(m);
441 #endif
442                                 mpt += sizeof(struct mpc_config_intsrc);
443                                 count += sizeof(struct mpc_config_intsrc);
444                                 break;
445                         }
446                 case MP_LINTSRC:
447                         {
448                                 struct mpc_config_lintsrc *m =
449                                     (struct mpc_config_lintsrc *)mpt;
450                                 MP_lintsrc_info(m);
451                                 mpt += sizeof(*m);
452                                 count += sizeof(*m);
453                                 break;
454                         }
455                 default:
456                         /* wrong mptable */
457                         printk(KERN_ERR "Your mptable is wrong, contact your HW vendor!\n");
458                         printk(KERN_ERR "type %x\n", *mpt);
459                         print_hex_dump(KERN_ERR, "  ", DUMP_PREFIX_ADDRESS, 16,
460                                         1, mpc, mpc->mpc_length, 1);
461                         count = mpc->mpc_length;
462                         break;
463                 }
464 #ifdef CONFIG_X86_NUMAQ
465                 ++mpc_record;
466 #endif
467         }
468         setup_apic_routing();
469         if (!num_processors)
470                 printk(KERN_ERR "MPTABLE: no processors registered!\n");
471         return num_processors;
472 }
473
474 #ifdef CONFIG_X86_IO_APIC
475
476 static int __init ELCR_trigger(unsigned int irq)
477 {
478         unsigned int port;
479
480         port = 0x4d0 + (irq >> 3);
481         return (inb(port) >> (irq & 7)) & 1;
482 }
483
484 static void __init construct_default_ioirq_mptable(int mpc_default_type)
485 {
486         struct mpc_config_intsrc intsrc;
487         int i;
488         int ELCR_fallback = 0;
489
490         intsrc.mpc_type = MP_INTSRC;
491         intsrc.mpc_irqflag = 0; /* conforming */
492         intsrc.mpc_srcbus = 0;
493         intsrc.mpc_dstapic = mp_ioapics[0].mp_apicid;
494
495         intsrc.mpc_irqtype = mp_INT;
496
497         /*
498          *  If true, we have an ISA/PCI system with no IRQ entries
499          *  in the MP table. To prevent the PCI interrupts from being set up
500          *  incorrectly, we try to use the ELCR. The sanity check to see if
501          *  there is good ELCR data is very simple - IRQ0, 1, 2 and 13 can
502          *  never be level sensitive, so we simply see if the ELCR agrees.
503          *  If it does, we assume it's valid.
504          */
505         if (mpc_default_type == 5) {
506                 printk(KERN_INFO "ISA/PCI bus type with no IRQ information... "
507                        "falling back to ELCR\n");
508
509                 if (ELCR_trigger(0) || ELCR_trigger(1) || ELCR_trigger(2) ||
510                     ELCR_trigger(13))
511                         printk(KERN_ERR "ELCR contains invalid data... "
512                                "not using ELCR\n");
513                 else {
514                         printk(KERN_INFO
515                                "Using ELCR to identify PCI interrupts\n");
516                         ELCR_fallback = 1;
517                 }
518         }
519
520         for (i = 0; i < 16; i++) {
521                 switch (mpc_default_type) {
522                 case 2:
523                         if (i == 0 || i == 13)
524                                 continue;       /* IRQ0 & IRQ13 not connected */
525                         /* fall through */
526                 default:
527                         if (i == 2)
528                                 continue;       /* IRQ2 is never connected */
529                 }
530
531                 if (ELCR_fallback) {
532                         /*
533                          *  If the ELCR indicates a level-sensitive interrupt, we
534                          *  copy that information over to the MP table in the
535                          *  irqflag field (level sensitive, active high polarity).
536                          */
537                         if (ELCR_trigger(i))
538                                 intsrc.mpc_irqflag = 13;
539                         else
540                                 intsrc.mpc_irqflag = 0;
541                 }
542
543                 intsrc.mpc_srcbusirq = i;
544                 intsrc.mpc_dstirq = i ? i : 2;  /* IRQ0 to INTIN2 */
545                 MP_intsrc_info(&intsrc);
546         }
547
548         intsrc.mpc_irqtype = mp_ExtINT;
549         intsrc.mpc_srcbusirq = 0;
550         intsrc.mpc_dstirq = 0;  /* 8259A to INTIN0 */
551         MP_intsrc_info(&intsrc);
552 }
553
554
555 static void construct_ioapic_table(int mpc_default_type)
556 {
557         struct mpc_config_ioapic ioapic;
558         struct mpc_config_bus bus;
559
560         bus.mpc_type = MP_BUS;
561         bus.mpc_busid = 0;
562         switch (mpc_default_type) {
563         default:
564                 printk(KERN_ERR "???\nUnknown standard configuration %d\n",
565                        mpc_default_type);
566                 /* fall through */
567         case 1:
568         case 5:
569                 memcpy(bus.mpc_bustype, "ISA   ", 6);
570                 break;
571         case 2:
572         case 6:
573         case 3:
574                 memcpy(bus.mpc_bustype, "EISA  ", 6);
575                 break;
576         case 4:
577         case 7:
578                 memcpy(bus.mpc_bustype, "MCA   ", 6);
579         }
580         MP_bus_info(&bus);
581         if (mpc_default_type > 4) {
582                 bus.mpc_busid = 1;
583                 memcpy(bus.mpc_bustype, "PCI   ", 6);
584                 MP_bus_info(&bus);
585         }
586
587         ioapic.mpc_type = MP_IOAPIC;
588         ioapic.mpc_apicid = 2;
589         ioapic.mpc_apicver = mpc_default_type > 4 ? 0x10 : 0x01;
590         ioapic.mpc_flags = MPC_APIC_USABLE;
591         ioapic.mpc_apicaddr = 0xFEC00000;
592         MP_ioapic_info(&ioapic);
593
594         /*
595          * We set up most of the low 16 IO-APIC pins according to MPS rules.
596          */
597         construct_default_ioirq_mptable(mpc_default_type);
598 }
599 #else
600 static inline void construct_ioapic_table(int mpc_default_type) { }
601 #endif
602
603 static inline void __init construct_default_ISA_mptable(int mpc_default_type)
604 {
605         struct mpc_config_processor processor;
606         struct mpc_config_lintsrc lintsrc;
607         int linttypes[2] = { mp_ExtINT, mp_NMI };
608         int i;
609
610         /*
611          * local APIC has default address
612          */
613         mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
614
615         /*
616          * 2 CPUs, numbered 0 & 1.
617          */
618         processor.mpc_type = MP_PROCESSOR;
619         /* Either an integrated APIC or a discrete 82489DX. */
620         processor.mpc_apicver = mpc_default_type > 4 ? 0x10 : 0x01;
621         processor.mpc_cpuflag = CPU_ENABLED;
622         processor.mpc_cpufeature = (boot_cpu_data.x86 << 8) |
623             (boot_cpu_data.x86_model << 4) | boot_cpu_data.x86_mask;
624         processor.mpc_featureflag = boot_cpu_data.x86_capability[0];
625         processor.mpc_reserved[0] = 0;
626         processor.mpc_reserved[1] = 0;
627         for (i = 0; i < 2; i++) {
628                 processor.mpc_apicid = i;
629                 MP_processor_info(&processor);
630         }
631
632         construct_ioapic_table(mpc_default_type);
633
634         lintsrc.mpc_type = MP_LINTSRC;
635         lintsrc.mpc_irqflag = 0;        /* conforming */
636         lintsrc.mpc_srcbusid = 0;
637         lintsrc.mpc_srcbusirq = 0;
638         lintsrc.mpc_destapic = MP_APIC_ALL;
639         for (i = 0; i < 2; i++) {
640                 lintsrc.mpc_irqtype = linttypes[i];
641                 lintsrc.mpc_destapiclint = i;
642                 MP_lintsrc_info(&lintsrc);
643         }
644 }
645
646 static struct intel_mp_floating *mpf_found;
647
648 /*
649  * Scan the memory blocks for an SMP configuration block.
650  */
651 static void __init __get_smp_config(unsigned early)
652 {
653         struct intel_mp_floating *mpf = mpf_found;
654
655         if (acpi_lapic && early)
656                 return;
657         /*
658          * ACPI supports both logical (e.g. Hyper-Threading) and physical
659          * processors, where MPS only supports physical.
660          */
661         if (acpi_lapic && acpi_ioapic) {
662                 printk(KERN_INFO "Using ACPI (MADT) for SMP configuration "
663                        "information\n");
664                 return;
665         } else if (acpi_lapic)
666                 printk(KERN_INFO "Using ACPI for processor (LAPIC) "
667                        "configuration information\n");
668
669         printk(KERN_INFO "Intel MultiProcessor Specification v1.%d\n",
670                mpf->mpf_specification);
671 #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86_32)
672         if (mpf->mpf_feature2 & (1 << 7)) {
673                 printk(KERN_INFO "    IMCR and PIC compatibility mode.\n");
674                 pic_mode = 1;
675         } else {
676                 printk(KERN_INFO "    Virtual Wire compatibility mode.\n");
677                 pic_mode = 0;
678         }
679 #endif
680         /*
681          * Now see if we need to read further.
682          */
683         if (mpf->mpf_feature1 != 0) {
684                 if (early) {
685                         /*
686                          * local APIC has default address
687                          */
688                         mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
689                         return;
690                 }
691
692                 printk(KERN_INFO "Default MP configuration #%d\n",
693                        mpf->mpf_feature1);
694                 construct_default_ISA_mptable(mpf->mpf_feature1);
695
696         } else if (mpf->mpf_physptr) {
697
698                 /*
699                  * Read the physical hardware table.  Anything here will
700                  * override the defaults.
701                  */
702                 if (!smp_read_mpc(phys_to_virt(mpf->mpf_physptr), early)) {
703 #ifdef CONFIG_X86_LOCAL_APIC
704                         smp_found_config = 0;
705 #endif
706                         printk(KERN_ERR
707                                "BIOS bug, MP table errors detected!...\n");
708                         printk(KERN_ERR "... disabling SMP support. "
709                                "(tell your hw vendor)\n");
710                         return;
711                 }
712
713                 if (early)
714                         return;
715 #ifdef CONFIG_X86_IO_APIC
716                 /*
717                  * If there are no explicit MP IRQ entries, then we are
718                  * broken.  We set up most of the low 16 IO-APIC pins to
719                  * ISA defaults and hope it will work.
720                  */
721                 if (!mp_irq_entries) {
722                         struct mpc_config_bus bus;
723
724                         printk(KERN_ERR "BIOS bug, no explicit IRQ entries, "
725                                "using default mptable. "
726                                "(tell your hw vendor)\n");
727
728                         bus.mpc_type = MP_BUS;
729                         bus.mpc_busid = 0;
730                         memcpy(bus.mpc_bustype, "ISA   ", 6);
731                         MP_bus_info(&bus);
732
733                         construct_default_ioirq_mptable(0);
734                 }
735 #endif
736         } else
737                 BUG();
738
739         if (!early)
740                 printk(KERN_INFO "Processors: %d\n", num_processors);
741         /*
742          * Only use the first configuration found.
743          */
744 }
745
746 void __init early_get_smp_config(void)
747 {
748         __get_smp_config(1);
749 }
750
751 void __init get_smp_config(void)
752 {
753         __get_smp_config(0);
754 }
755
756 static int __init smp_scan_config(unsigned long base, unsigned long length,
757                                   unsigned reserve)
758 {
759         unsigned int *bp = phys_to_virt(base);
760         struct intel_mp_floating *mpf;
761
762         printk(KERN_DEBUG "Scan SMP from %p for %ld bytes.\n", bp, length);
763         BUILD_BUG_ON(sizeof(*mpf) != 16);
764
765         while (length > 0) {
766                 mpf = (struct intel_mp_floating *)bp;
767                 if ((*bp == SMP_MAGIC_IDENT) &&
768                     (mpf->mpf_length == 1) &&
769                     !mpf_checksum((unsigned char *)bp, 16) &&
770                     ((mpf->mpf_specification == 1)
771                      || (mpf->mpf_specification == 4))) {
772 #ifdef CONFIG_X86_LOCAL_APIC
773                         smp_found_config = 1;
774 #endif
775                         mpf_found = mpf;
776 #ifdef CONFIG_X86_32
777                         printk(KERN_INFO "found SMP MP-table at [%p] %08lx\n",
778                                mpf, virt_to_phys(mpf));
779                         reserve_bootmem(virt_to_phys(mpf), PAGE_SIZE,
780                                         BOOTMEM_DEFAULT);
781                         if (mpf->mpf_physptr) {
782                                 /*
783                                  * We cannot access to MPC table to compute
784                                  * table size yet, as only few megabytes from
785                                  * the bottom is mapped now.
786                                  * PC-9800's MPC table places on the very last
787                                  * of physical memory; so that simply reserving
788                                  * PAGE_SIZE from mpg->mpf_physptr yields BUG()
789                                  * in reserve_bootmem.
790                                  */
791                                 unsigned long size = PAGE_SIZE;
792                                 unsigned long end = max_low_pfn * PAGE_SIZE;
793                                 if (mpf->mpf_physptr + size > end)
794                                         size = end - mpf->mpf_physptr;
795                                 reserve_bootmem(mpf->mpf_physptr, size,
796                                                 BOOTMEM_DEFAULT);
797                         }
798
799 #else
800                         if (!reserve)
801                                 return 1;
802
803                         reserve_bootmem_generic(virt_to_phys(mpf), PAGE_SIZE);
804                         if (mpf->mpf_physptr)
805                                 reserve_bootmem_generic(mpf->mpf_physptr,
806                                                         PAGE_SIZE);
807 #endif
808                 return 1;
809                 }
810                 bp += 4;
811                 length -= 16;
812         }
813         return 0;
814 }
815
816 static void __init __find_smp_config(unsigned reserve)
817 {
818         unsigned int address;
819
820         /*
821          * FIXME: Linux assumes you have 640K of base ram..
822          * this continues the error...
823          *
824          * 1) Scan the bottom 1K for a signature
825          * 2) Scan the top 1K of base RAM
826          * 3) Scan the 64K of bios
827          */
828         if (smp_scan_config(0x0, 0x400, reserve) ||
829             smp_scan_config(639 * 0x400, 0x400, reserve) ||
830             smp_scan_config(0xF0000, 0x10000, reserve))
831                 return;
832         /*
833          * If it is an SMP machine we should know now, unless the
834          * configuration is in an EISA/MCA bus machine with an
835          * extended bios data area.
836          *
837          * there is a real-mode segmented pointer pointing to the
838          * 4K EBDA area at 0x40E, calculate and scan it here.
839          *
840          * NOTE! There are Linux loaders that will corrupt the EBDA
841          * area, and as such this kind of SMP config may be less
842          * trustworthy, simply because the SMP table may have been
843          * stomped on during early boot. These loaders are buggy and
844          * should be fixed.
845          *
846          * MP1.4 SPEC states to only scan first 1K of 4K EBDA.
847          */
848
849         address = get_bios_ebda();
850         if (address)
851                 smp_scan_config(address, 0x400, reserve);
852 }
853
854 void __init early_find_smp_config(void)
855 {
856         __find_smp_config(0);
857 }
858
859 void __init find_smp_config(void)
860 {
861         __find_smp_config(1);
862 }
863
864 #ifdef CONFIG_X86_IO_APIC
865 static u8 __initdata irq_used[MAX_IRQ_SOURCES];
866
867 static int  __init get_MP_intsrc_index(struct mpc_config_intsrc *m)
868 {
869         int i;
870
871         if (m->mpc_irqtype != mp_INT)
872                 return 0;
873
874         if (m->mpc_irqflag != 0x0f)
875                 return 0;
876
877         /* not legacy */
878
879         for (i = 0; i < mp_irq_entries; i++) {
880                 if (mp_irqs[i].mp_irqtype != mp_INT)
881                         continue;
882
883                 if (mp_irqs[i].mp_irqflag != 0x0f)
884                         continue;
885
886                 if (mp_irqs[i].mp_srcbus != m->mpc_srcbus)
887                         continue;
888                 if (mp_irqs[i].mp_srcbusirq != m->mpc_srcbusirq)
889                         continue;
890                 if (irq_used[i]) {
891                         /* already claimed */
892                         return -2;
893                 }
894                 irq_used[i] = 1;
895                 return i;
896         }
897
898         /* not found */
899         return -1;
900 }
901
902 #define SPARE_SLOT_NUM 20
903
904 static struct mpc_config_intsrc __initdata *m_spare[SPARE_SLOT_NUM];
905 #endif
906
907 static int  __init replace_intsrc_all(struct mp_config_table *mpc,
908                                         unsigned long mpc_new_phys,
909                                         unsigned long mpc_new_length)
910 {
911 #ifdef CONFIG_X86_IO_APIC
912         int i;
913         int nr_m_spare = 0;
914 #endif
915
916         int count = sizeof(*mpc);
917         unsigned char *mpt = ((unsigned char *)mpc) + count;
918
919         printk(KERN_INFO "mpc_length %x\n", mpc->mpc_length);
920         while (count < mpc->mpc_length) {
921                 switch (*mpt) {
922                 case MP_PROCESSOR:
923                         {
924                                 struct mpc_config_processor *m =
925                                     (struct mpc_config_processor *)mpt;
926                                 mpt += sizeof(*m);
927                                 count += sizeof(*m);
928                                 break;
929                         }
930                 case MP_BUS:
931                         {
932                                 struct mpc_config_bus *m =
933                                     (struct mpc_config_bus *)mpt;
934                                 mpt += sizeof(*m);
935                                 count += sizeof(*m);
936                                 break;
937                         }
938                 case MP_IOAPIC:
939                         {
940                                 mpt += sizeof(struct mpc_config_ioapic);
941                                 count += sizeof(struct mpc_config_ioapic);
942                                 break;
943                         }
944                 case MP_INTSRC:
945                         {
946 #ifdef CONFIG_X86_IO_APIC
947                                 struct mpc_config_intsrc *m =
948                                     (struct mpc_config_intsrc *)mpt;
949
950                                 printk(KERN_INFO "OLD ");
951                                 print_MP_intsrc_info(m);
952                                 i = get_MP_intsrc_index(m);
953                                 if (i > 0) {
954                                         assign_to_mpc_intsrc(&mp_irqs[i], m);
955                                         printk(KERN_INFO "NEW ");
956                                         print_mp_irq_info(&mp_irqs[i]);
957                                 } else if (!i) {
958                                         /* legacy, do nothing */
959                                 } else if (nr_m_spare < SPARE_SLOT_NUM) {
960                                         /*
961                                          * not found (-1), or duplicated (-2)
962                                          * are invalid entries,
963                                          * we need to use the slot  later
964                                          */
965                                         m_spare[nr_m_spare] = m;
966                                         nr_m_spare++;
967                                 }
968 #endif
969                                 mpt += sizeof(struct mpc_config_intsrc);
970                                 count += sizeof(struct mpc_config_intsrc);
971                                 break;
972                         }
973                 case MP_LINTSRC:
974                         {
975                                 struct mpc_config_lintsrc *m =
976                                     (struct mpc_config_lintsrc *)mpt;
977                                 mpt += sizeof(*m);
978                                 count += sizeof(*m);
979                                 break;
980                         }
981                 default:
982                         /* wrong mptable */
983                         printk(KERN_ERR "Your mptable is wrong, contact your HW vendor!\n");
984                         printk(KERN_ERR "type %x\n", *mpt);
985                         print_hex_dump(KERN_ERR, "  ", DUMP_PREFIX_ADDRESS, 16,
986                                         1, mpc, mpc->mpc_length, 1);
987                         goto out;
988                 }
989         }
990
991 #ifdef CONFIG_X86_IO_APIC
992         for (i = 0; i < mp_irq_entries; i++) {
993                 if (irq_used[i])
994                         continue;
995
996                 if (mp_irqs[i].mp_irqtype != mp_INT)
997                         continue;
998
999                 if (mp_irqs[i].mp_irqflag != 0x0f)
1000                         continue;
1001
1002                 if (nr_m_spare > 0) {
1003                         printk(KERN_INFO "*NEW* found ");
1004                         nr_m_spare--;
1005                         assign_to_mpc_intsrc(&mp_irqs[i], m_spare[nr_m_spare]);
1006                         m_spare[nr_m_spare] = NULL;
1007                 } else {
1008                         struct mpc_config_intsrc *m =
1009                             (struct mpc_config_intsrc *)mpt;
1010                         count += sizeof(struct mpc_config_intsrc);
1011                         if (!mpc_new_phys) {
1012                                 printk(KERN_INFO "No spare slots, try to append...take your risk, new mpc_length %x\n", count);
1013                         } else {
1014                                 if (count <= mpc_new_length)
1015                                         printk(KERN_INFO "No spare slots, try to append..., new mpc_length %x\n", count);
1016                                 else {
1017                                         printk(KERN_ERR "mpc_new_length %lx is too small\n", mpc_new_length);
1018                                         goto out;
1019                                 }
1020                         }
1021                         assign_to_mpc_intsrc(&mp_irqs[i], m);
1022                         mpc->mpc_length = count;
1023                         mpt += sizeof(struct mpc_config_intsrc);
1024                 }
1025                 print_mp_irq_info(&mp_irqs[i]);
1026         }
1027 #endif
1028 out:
1029         /* update checksum */
1030         mpc->mpc_checksum = 0;
1031         mpc->mpc_checksum -= mpf_checksum((unsigned char *)mpc,
1032                                            mpc->mpc_length);
1033
1034         return 0;
1035 }
1036
1037 int __initdata enable_update_mptable;
1038
1039 static int __init update_mptable_setup(char *str)
1040 {
1041         enable_update_mptable = 1;
1042         return 0;
1043 }
1044 early_param("update_mptable", update_mptable_setup);
1045
1046 static unsigned long __initdata mpc_new_phys;
1047 static unsigned long mpc_new_length __initdata = 4096;
1048
1049 /* alloc_mptable or alloc_mptable=4k */
1050 static int __initdata alloc_mptable;
1051 static int __init parse_alloc_mptable_opt(char *p)
1052 {
1053         enable_update_mptable = 1;
1054         alloc_mptable = 1;
1055         if (!p)
1056                 return 0;
1057         mpc_new_length = memparse(p, &p);
1058         return 0;
1059 }
1060 early_param("alloc_mptable", parse_alloc_mptable_opt);
1061
1062 void __init early_reserve_e820_mpc_new(void)
1063 {
1064         if (enable_update_mptable && alloc_mptable) {
1065                 u64 startt = 0;
1066 #ifdef CONFIG_X86_TRAMPOLINE
1067                 startt = TRAMPOLINE_BASE;
1068 #endif
1069                 mpc_new_phys = early_reserve_e820(startt, mpc_new_length, 4);
1070         }
1071 }
1072
1073 static int __init update_mp_table(void)
1074 {
1075         char str[16];
1076         char oem[10];
1077         struct intel_mp_floating *mpf;
1078         struct mp_config_table *mpc;
1079         struct mp_config_table *mpc_new;
1080
1081         if (!enable_update_mptable)
1082                 return 0;
1083
1084         mpf = mpf_found;
1085         if (!mpf)
1086                 return 0;
1087
1088         /*
1089          * Now see if we need to go further.
1090          */
1091         if (mpf->mpf_feature1 != 0)
1092                 return 0;
1093
1094         if (!mpf->mpf_physptr)
1095                 return 0;
1096
1097         mpc = phys_to_virt(mpf->mpf_physptr);
1098
1099         if (!smp_check_mpc(mpc, oem, str))
1100                 return 0;
1101
1102         printk(KERN_INFO "mpf: %lx\n", virt_to_phys(mpf));
1103         printk(KERN_INFO "mpf_physptr: %x\n", mpf->mpf_physptr);
1104
1105         if (mpc_new_phys && mpc->mpc_length > mpc_new_length) {
1106                 mpc_new_phys = 0;
1107                 printk(KERN_INFO "mpc_new_length is %ld, please use alloc_mptable=8k\n",
1108                          mpc_new_length);
1109         }
1110
1111         if (!mpc_new_phys) {
1112                 unsigned char old, new;
1113                 /* check if we can change the postion */
1114                 mpc->mpc_checksum = 0;
1115                 old = mpf_checksum((unsigned char *)mpc, mpc->mpc_length);
1116                 mpc->mpc_checksum = 0xff;
1117                 new = mpf_checksum((unsigned char *)mpc, mpc->mpc_length);
1118                 if (old == new) {
1119                         printk(KERN_INFO "mpc is readonly, please try alloc_mptable instead\n");
1120                         return 0;
1121                 }
1122                 printk(KERN_INFO "use in-positon replacing\n");
1123         } else {
1124                 mpf->mpf_physptr = mpc_new_phys;
1125                 mpc_new = phys_to_virt(mpc_new_phys);
1126                 memcpy(mpc_new, mpc, mpc->mpc_length);
1127                 mpc = mpc_new;
1128                 /* check if we can modify that */
1129                 if (mpc_new_phys - mpf->mpf_physptr) {
1130                         struct intel_mp_floating *mpf_new;
1131                         /* steal 16 bytes from [0, 1k) */
1132                         printk(KERN_INFO "mpf new: %x\n", 0x400 - 16);
1133                         mpf_new = phys_to_virt(0x400 - 16);
1134                         memcpy(mpf_new, mpf, 16);
1135                         mpf = mpf_new;
1136                         mpf->mpf_physptr = mpc_new_phys;
1137                 }
1138                 mpf->mpf_checksum = 0;
1139                 mpf->mpf_checksum -= mpf_checksum((unsigned char *)mpf, 16);
1140                 printk(KERN_INFO "mpf_physptr new: %x\n", mpf->mpf_physptr);
1141         }
1142
1143         /*
1144          * only replace the one with mp_INT and
1145          *       MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW,
1146          * already in mp_irqs , stored by ... and mp_config_acpi_gsi,
1147          * may need pci=routeirq for all coverage
1148          */
1149         replace_intsrc_all(mpc, mpc_new_phys, mpc_new_length);
1150
1151         return 0;
1152 }
1153
1154 late_initcall(update_mp_table);