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