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