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