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