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