pci, x86, acpi: fix early_ioremap() leak
[pandora-kernel.git] / arch / x86 / kernel / acpi / boot.c
1 /*
2  *  boot.c - Architecture-Specific Low-Level ACPI Boot Support
3  *
4  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
5  *  Copyright (C) 2001 Jun Nakajima <jun.nakajima@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/init.h>
27 #include <linux/acpi.h>
28 #include <linux/acpi_pmtmr.h>
29 #include <linux/efi.h>
30 #include <linux/cpumask.h>
31 #include <linux/module.h>
32 #include <linux/dmi.h>
33 #include <linux/irq.h>
34 #include <linux/bootmem.h>
35 #include <linux/ioport.h>
36
37 #include <asm/pgtable.h>
38 #include <asm/io_apic.h>
39 #include <asm/apic.h>
40 #include <asm/genapic.h>
41 #include <asm/io.h>
42 #include <asm/mpspec.h>
43 #include <asm/smp.h>
44
45 #ifdef CONFIG_X86_LOCAL_APIC
46 # include <mach_apic.h>
47 #endif
48
49 static int __initdata acpi_force = 0;
50 u32 acpi_rsdt_forced;
51 #ifdef  CONFIG_ACPI
52 int acpi_disabled = 0;
53 #else
54 int acpi_disabled = 1;
55 #endif
56 EXPORT_SYMBOL(acpi_disabled);
57
58 #ifdef  CONFIG_X86_64
59
60 #include <asm/proto.h>
61
62 #else                           /* X86 */
63
64 #ifdef  CONFIG_X86_LOCAL_APIC
65 #include <mach_apic.h>
66 #include <mach_mpparse.h>
67 #endif                          /* CONFIG_X86_LOCAL_APIC */
68
69 #endif                          /* X86 */
70
71 #define BAD_MADT_ENTRY(entry, end) (                                        \
72                 (!entry) || (unsigned long)entry + sizeof(*entry) > end ||  \
73                 ((struct acpi_subtable_header *)entry)->length < sizeof(*entry))
74
75 #define PREFIX                  "ACPI: "
76
77 int acpi_noirq;                         /* skip ACPI IRQ initialization */
78 int acpi_pci_disabled;          /* skip ACPI PCI scan and IRQ initialization */
79 EXPORT_SYMBOL(acpi_pci_disabled);
80 int acpi_ht __initdata = 1;     /* enable HT */
81
82 int acpi_lapic;
83 int acpi_ioapic;
84 int acpi_strict;
85
86 u8 acpi_sci_flags __initdata;
87 int acpi_sci_override_gsi __initdata;
88 int acpi_skip_timer_override __initdata;
89 int acpi_use_timer_override __initdata;
90
91 #ifdef CONFIG_X86_LOCAL_APIC
92 static u64 acpi_lapic_addr __initdata = APIC_DEFAULT_PHYS_BASE;
93 #endif
94
95 #ifndef __HAVE_ARCH_CMPXCHG
96 #warning ACPI uses CMPXCHG, i486 and later hardware
97 #endif
98
99 /* --------------------------------------------------------------------------
100                               Boot-time Configuration
101    -------------------------------------------------------------------------- */
102
103 /*
104  * The default interrupt routing model is PIC (8259).  This gets
105  * overridden if IOAPICs are enumerated (below).
106  */
107 enum acpi_irq_model_id acpi_irq_model = ACPI_IRQ_MODEL_PIC;
108
109
110 /*
111  * Temporarily use the virtual area starting from FIX_IO_APIC_BASE_END,
112  * to map the target physical address. The problem is that set_fixmap()
113  * provides a single page, and it is possible that the page is not
114  * sufficient.
115  * By using this area, we can map up to MAX_IO_APICS pages temporarily,
116  * i.e. until the next __va_range() call.
117  *
118  * Important Safety Note:  The fixed I/O APIC page numbers are *subtracted*
119  * from the fixed base.  That's why we start at FIX_IO_APIC_BASE_END and
120  * count idx down while incrementing the phys address.
121  */
122 char *__init __acpi_map_table(unsigned long phys, unsigned long size)
123 {
124
125         if (!phys || !size)
126                 return NULL;
127
128         return early_ioremap(phys, size);
129 }
130 void __init __acpi_unmap_table(char *map, unsigned long size)
131 {
132         if (!map || !size)
133                 return;
134
135         early_iounmap(map, size);
136 }
137
138 #ifdef CONFIG_PCI_MMCONFIG
139
140 static int acpi_mcfg_64bit_base_addr __initdata = FALSE;
141
142 /* The physical address of the MMCONFIG aperture.  Set from ACPI tables. */
143 struct acpi_mcfg_allocation *pci_mmcfg_config;
144 int pci_mmcfg_config_num;
145
146 static int __init acpi_mcfg_oem_check(struct acpi_table_mcfg *mcfg)
147 {
148         if (!strcmp(mcfg->header.oem_id, "SGI"))
149                 acpi_mcfg_64bit_base_addr = TRUE;
150
151         return 0;
152 }
153
154 int __init acpi_parse_mcfg(struct acpi_table_header *header)
155 {
156         struct acpi_table_mcfg *mcfg;
157         unsigned long i;
158         int config_size;
159
160         if (!header)
161                 return -EINVAL;
162
163         mcfg = (struct acpi_table_mcfg *)header;
164
165         /* how many config structures do we have */
166         pci_mmcfg_config_num = 0;
167         i = header->length - sizeof(struct acpi_table_mcfg);
168         while (i >= sizeof(struct acpi_mcfg_allocation)) {
169                 ++pci_mmcfg_config_num;
170                 i -= sizeof(struct acpi_mcfg_allocation);
171         };
172         if (pci_mmcfg_config_num == 0) {
173                 printk(KERN_ERR PREFIX "MMCONFIG has no entries\n");
174                 return -ENODEV;
175         }
176
177         config_size = pci_mmcfg_config_num * sizeof(*pci_mmcfg_config);
178         pci_mmcfg_config = kmalloc(config_size, GFP_KERNEL);
179         if (!pci_mmcfg_config) {
180                 printk(KERN_WARNING PREFIX
181                        "No memory for MCFG config tables\n");
182                 return -ENOMEM;
183         }
184
185         memcpy(pci_mmcfg_config, &mcfg[1], config_size);
186
187         acpi_mcfg_oem_check(mcfg);
188
189         for (i = 0; i < pci_mmcfg_config_num; ++i) {
190                 if ((pci_mmcfg_config[i].address > 0xFFFFFFFF) &&
191                     !acpi_mcfg_64bit_base_addr) {
192                         printk(KERN_ERR PREFIX
193                                "MMCONFIG not in low 4GB of memory\n");
194                         kfree(pci_mmcfg_config);
195                         pci_mmcfg_config_num = 0;
196                         return -ENODEV;
197                 }
198         }
199
200         return 0;
201 }
202 #endif                          /* CONFIG_PCI_MMCONFIG */
203
204 #ifdef CONFIG_X86_LOCAL_APIC
205 static int __init acpi_parse_madt(struct acpi_table_header *table)
206 {
207         struct acpi_table_madt *madt = NULL;
208
209         if (!cpu_has_apic)
210                 return -EINVAL;
211
212         madt = (struct acpi_table_madt *)table;
213         if (!madt) {
214                 printk(KERN_WARNING PREFIX "Unable to map MADT\n");
215                 return -ENODEV;
216         }
217
218         if (madt->address) {
219                 acpi_lapic_addr = (u64) madt->address;
220
221                 printk(KERN_DEBUG PREFIX "Local APIC address 0x%08x\n",
222                        madt->address);
223         }
224
225         acpi_madt_oem_check(madt->header.oem_id, madt->header.oem_table_id);
226
227         return 0;
228 }
229
230 static void __cpuinit acpi_register_lapic(int id, u8 enabled)
231 {
232         unsigned int ver = 0;
233
234         if (!enabled) {
235                 ++disabled_cpus;
236                 return;
237         }
238
239         if (boot_cpu_physical_apicid != -1U)
240                 ver = apic_version[boot_cpu_physical_apicid];
241
242         generic_processor_info(id, ver);
243 }
244
245 static int __init
246 acpi_parse_lapic(struct acpi_subtable_header * header, const unsigned long end)
247 {
248         struct acpi_madt_local_apic *processor = NULL;
249
250         processor = (struct acpi_madt_local_apic *)header;
251
252         if (BAD_MADT_ENTRY(processor, end))
253                 return -EINVAL;
254
255         acpi_table_print_madt_entry(header);
256
257         /*
258          * We need to register disabled CPU as well to permit
259          * counting disabled CPUs. This allows us to size
260          * cpus_possible_map more accurately, to permit
261          * to not preallocating memory for all NR_CPUS
262          * when we use CPU hotplug.
263          */
264         acpi_register_lapic(processor->id,      /* APIC ID */
265                             processor->lapic_flags & ACPI_MADT_ENABLED);
266
267         return 0;
268 }
269
270 static int __init
271 acpi_parse_sapic(struct acpi_subtable_header *header, const unsigned long end)
272 {
273         struct acpi_madt_local_sapic *processor = NULL;
274
275         processor = (struct acpi_madt_local_sapic *)header;
276
277         if (BAD_MADT_ENTRY(processor, end))
278                 return -EINVAL;
279
280         acpi_table_print_madt_entry(header);
281
282         acpi_register_lapic((processor->id << 8) | processor->eid,/* APIC ID */
283                             processor->lapic_flags & ACPI_MADT_ENABLED);
284
285         return 0;
286 }
287
288 static int __init
289 acpi_parse_lapic_addr_ovr(struct acpi_subtable_header * header,
290                           const unsigned long end)
291 {
292         struct acpi_madt_local_apic_override *lapic_addr_ovr = NULL;
293
294         lapic_addr_ovr = (struct acpi_madt_local_apic_override *)header;
295
296         if (BAD_MADT_ENTRY(lapic_addr_ovr, end))
297                 return -EINVAL;
298
299         acpi_lapic_addr = lapic_addr_ovr->address;
300
301         return 0;
302 }
303
304 static int __init
305 acpi_parse_lapic_nmi(struct acpi_subtable_header * header, const unsigned long end)
306 {
307         struct acpi_madt_local_apic_nmi *lapic_nmi = NULL;
308
309         lapic_nmi = (struct acpi_madt_local_apic_nmi *)header;
310
311         if (BAD_MADT_ENTRY(lapic_nmi, end))
312                 return -EINVAL;
313
314         acpi_table_print_madt_entry(header);
315
316         if (lapic_nmi->lint != 1)
317                 printk(KERN_WARNING PREFIX "NMI not connected to LINT 1!\n");
318
319         return 0;
320 }
321
322 #endif                          /*CONFIG_X86_LOCAL_APIC */
323
324 #ifdef CONFIG_X86_IO_APIC
325
326 static int __init
327 acpi_parse_ioapic(struct acpi_subtable_header * header, const unsigned long end)
328 {
329         struct acpi_madt_io_apic *ioapic = NULL;
330
331         ioapic = (struct acpi_madt_io_apic *)header;
332
333         if (BAD_MADT_ENTRY(ioapic, end))
334                 return -EINVAL;
335
336         acpi_table_print_madt_entry(header);
337
338         mp_register_ioapic(ioapic->id,
339                            ioapic->address, ioapic->global_irq_base);
340
341         return 0;
342 }
343
344 /*
345  * Parse Interrupt Source Override for the ACPI SCI
346  */
347 static void __init acpi_sci_ioapic_setup(u32 gsi, u16 polarity, u16 trigger)
348 {
349         if (trigger == 0)       /* compatible SCI trigger is level */
350                 trigger = 3;
351
352         if (polarity == 0)      /* compatible SCI polarity is low */
353                 polarity = 3;
354
355         /* Command-line over-ride via acpi_sci= */
356         if (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)
357                 trigger = (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2;
358
359         if (acpi_sci_flags & ACPI_MADT_POLARITY_MASK)
360                 polarity = acpi_sci_flags & ACPI_MADT_POLARITY_MASK;
361
362         /*
363          * mp_config_acpi_legacy_irqs() already setup IRQs < 16
364          * If GSI is < 16, this will update its flags,
365          * else it will create a new mp_irqs[] entry.
366          */
367         mp_override_legacy_irq(gsi, polarity, trigger, gsi);
368
369         /*
370          * stash over-ride to indicate we've been here
371          * and for later update of acpi_gbl_FADT
372          */
373         acpi_sci_override_gsi = gsi;
374         return;
375 }
376
377 static int __init
378 acpi_parse_int_src_ovr(struct acpi_subtable_header * header,
379                        const unsigned long end)
380 {
381         struct acpi_madt_interrupt_override *intsrc = NULL;
382
383         intsrc = (struct acpi_madt_interrupt_override *)header;
384
385         if (BAD_MADT_ENTRY(intsrc, end))
386                 return -EINVAL;
387
388         acpi_table_print_madt_entry(header);
389
390         if (intsrc->source_irq == acpi_gbl_FADT.sci_interrupt) {
391                 acpi_sci_ioapic_setup(intsrc->global_irq,
392                                       intsrc->inti_flags & ACPI_MADT_POLARITY_MASK,
393                                       (intsrc->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
394                 return 0;
395         }
396
397         if (acpi_skip_timer_override &&
398             intsrc->source_irq == 0 && intsrc->global_irq == 2) {
399                 printk(PREFIX "BIOS IRQ0 pin2 override ignored.\n");
400                 return 0;
401         }
402
403         mp_override_legacy_irq(intsrc->source_irq,
404                                 intsrc->inti_flags & ACPI_MADT_POLARITY_MASK,
405                                 (intsrc->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2,
406                                 intsrc->global_irq);
407
408         return 0;
409 }
410
411 static int __init
412 acpi_parse_nmi_src(struct acpi_subtable_header * header, const unsigned long end)
413 {
414         struct acpi_madt_nmi_source *nmi_src = NULL;
415
416         nmi_src = (struct acpi_madt_nmi_source *)header;
417
418         if (BAD_MADT_ENTRY(nmi_src, end))
419                 return -EINVAL;
420
421         acpi_table_print_madt_entry(header);
422
423         /* TBD: Support nimsrc entries? */
424
425         return 0;
426 }
427
428 #endif                          /* CONFIG_X86_IO_APIC */
429
430 /*
431  * acpi_pic_sci_set_trigger()
432  *
433  * use ELCR to set PIC-mode trigger type for SCI
434  *
435  * If a PIC-mode SCI is not recognized or gives spurious IRQ7's
436  * it may require Edge Trigger -- use "acpi_sci=edge"
437  *
438  * Port 0x4d0-4d1 are ECLR1 and ECLR2, the Edge/Level Control Registers
439  * for the 8259 PIC.  bit[n] = 1 means irq[n] is Level, otherwise Edge.
440  * ECLR1 is IRQs 0-7 (IRQ 0, 1, 2 must be 0)
441  * ECLR2 is IRQs 8-15 (IRQ 8, 13 must be 0)
442  */
443
444 void __init acpi_pic_sci_set_trigger(unsigned int irq, u16 trigger)
445 {
446         unsigned int mask = 1 << irq;
447         unsigned int old, new;
448
449         /* Real old ELCR mask */
450         old = inb(0x4d0) | (inb(0x4d1) << 8);
451
452         /*
453          * If we use ACPI to set PCI IRQs, then we should clear ELCR
454          * since we will set it correctly as we enable the PCI irq
455          * routing.
456          */
457         new = acpi_noirq ? old : 0;
458
459         /*
460          * Update SCI information in the ELCR, it isn't in the PCI
461          * routing tables..
462          */
463         switch (trigger) {
464         case 1:         /* Edge - clear */
465                 new &= ~mask;
466                 break;
467         case 3:         /* Level - set */
468                 new |= mask;
469                 break;
470         }
471
472         if (old == new)
473                 return;
474
475         printk(PREFIX "setting ELCR to %04x (from %04x)\n", new, old);
476         outb(new, 0x4d0);
477         outb(new >> 8, 0x4d1);
478 }
479
480 int acpi_gsi_to_irq(u32 gsi, unsigned int *irq)
481 {
482         *irq = gsi;
483         return 0;
484 }
485
486 /*
487  * success: return IRQ number (>=0)
488  * failure: return < 0
489  */
490 int acpi_register_gsi(u32 gsi, int triggering, int polarity)
491 {
492         unsigned int irq;
493         unsigned int plat_gsi = gsi;
494
495 #ifdef CONFIG_PCI
496         /*
497          * Make sure all (legacy) PCI IRQs are set as level-triggered.
498          */
499         if (acpi_irq_model == ACPI_IRQ_MODEL_PIC) {
500                 if (triggering == ACPI_LEVEL_SENSITIVE)
501                         eisa_set_level_irq(gsi);
502         }
503 #endif
504
505 #ifdef CONFIG_X86_IO_APIC
506         if (acpi_irq_model == ACPI_IRQ_MODEL_IOAPIC) {
507                 plat_gsi = mp_register_gsi(gsi, triggering, polarity);
508         }
509 #endif
510         acpi_gsi_to_irq(plat_gsi, &irq);
511         return irq;
512 }
513
514 /*
515  *  ACPI based hotplug support for CPU
516  */
517 #ifdef CONFIG_ACPI_HOTPLUG_CPU
518
519 static int __cpuinit _acpi_map_lsapic(acpi_handle handle, int *pcpu)
520 {
521         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
522         union acpi_object *obj;
523         struct acpi_madt_local_apic *lapic;
524         cpumask_var_t tmp_map, new_map;
525         u8 physid;
526         int cpu;
527         int retval = -ENOMEM;
528
529         if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
530                 return -EINVAL;
531
532         if (!buffer.length || !buffer.pointer)
533                 return -EINVAL;
534
535         obj = buffer.pointer;
536         if (obj->type != ACPI_TYPE_BUFFER ||
537             obj->buffer.length < sizeof(*lapic)) {
538                 kfree(buffer.pointer);
539                 return -EINVAL;
540         }
541
542         lapic = (struct acpi_madt_local_apic *)obj->buffer.pointer;
543
544         if (lapic->header.type != ACPI_MADT_TYPE_LOCAL_APIC ||
545             !(lapic->lapic_flags & ACPI_MADT_ENABLED)) {
546                 kfree(buffer.pointer);
547                 return -EINVAL;
548         }
549
550         physid = lapic->id;
551
552         kfree(buffer.pointer);
553         buffer.length = ACPI_ALLOCATE_BUFFER;
554         buffer.pointer = NULL;
555
556         if (!alloc_cpumask_var(&tmp_map, GFP_KERNEL))
557                 goto out;
558
559         if (!alloc_cpumask_var(&new_map, GFP_KERNEL))
560                 goto free_tmp_map;
561
562         cpumask_copy(tmp_map, cpu_present_mask);
563         acpi_register_lapic(physid, lapic->lapic_flags & ACPI_MADT_ENABLED);
564
565         /*
566          * If mp_register_lapic successfully generates a new logical cpu
567          * number, then the following will get us exactly what was mapped
568          */
569         cpumask_andnot(new_map, cpu_present_mask, tmp_map);
570         if (cpumask_empty(new_map)) {
571                 printk ("Unable to map lapic to logical cpu number\n");
572                 retval = -EINVAL;
573                 goto free_new_map;
574         }
575
576         cpu = cpumask_first(new_map);
577
578         *pcpu = cpu;
579         retval = 0;
580
581 free_new_map:
582         free_cpumask_var(new_map);
583 free_tmp_map:
584         free_cpumask_var(tmp_map);
585 out:
586         return retval;
587 }
588
589 /* wrapper to silence section mismatch warning */
590 int __ref acpi_map_lsapic(acpi_handle handle, int *pcpu)
591 {
592         return _acpi_map_lsapic(handle, pcpu);
593 }
594 EXPORT_SYMBOL(acpi_map_lsapic);
595
596 int acpi_unmap_lsapic(int cpu)
597 {
598         per_cpu(x86_cpu_to_apicid, cpu) = -1;
599         set_cpu_present(cpu, false);
600         num_processors--;
601
602         return (0);
603 }
604
605 EXPORT_SYMBOL(acpi_unmap_lsapic);
606 #endif                          /* CONFIG_ACPI_HOTPLUG_CPU */
607
608 int acpi_register_ioapic(acpi_handle handle, u64 phys_addr, u32 gsi_base)
609 {
610         /* TBD */
611         return -EINVAL;
612 }
613
614 EXPORT_SYMBOL(acpi_register_ioapic);
615
616 int acpi_unregister_ioapic(acpi_handle handle, u32 gsi_base)
617 {
618         /* TBD */
619         return -EINVAL;
620 }
621
622 EXPORT_SYMBOL(acpi_unregister_ioapic);
623
624 static int __init acpi_parse_sbf(struct acpi_table_header *table)
625 {
626         struct acpi_table_boot *sb;
627
628         sb = (struct acpi_table_boot *)table;
629         if (!sb) {
630                 printk(KERN_WARNING PREFIX "Unable to map SBF\n");
631                 return -ENODEV;
632         }
633
634         sbf_port = sb->cmos_index;      /* Save CMOS port */
635
636         return 0;
637 }
638
639 #ifdef CONFIG_HPET_TIMER
640 #include <asm/hpet.h>
641
642 static struct __initdata resource *hpet_res;
643
644 static int __init acpi_parse_hpet(struct acpi_table_header *table)
645 {
646         struct acpi_table_hpet *hpet_tbl;
647
648         hpet_tbl = (struct acpi_table_hpet *)table;
649         if (!hpet_tbl) {
650                 printk(KERN_WARNING PREFIX "Unable to map HPET\n");
651                 return -ENODEV;
652         }
653
654         if (hpet_tbl->address.space_id != ACPI_SPACE_MEM) {
655                 printk(KERN_WARNING PREFIX "HPET timers must be located in "
656                        "memory.\n");
657                 return -1;
658         }
659
660         hpet_address = hpet_tbl->address.address;
661
662         /*
663          * Some broken BIOSes advertise HPET at 0x0. We really do not
664          * want to allocate a resource there.
665          */
666         if (!hpet_address) {
667                 printk(KERN_WARNING PREFIX
668                        "HPET id: %#x base: %#lx is invalid\n",
669                        hpet_tbl->id, hpet_address);
670                 return 0;
671         }
672 #ifdef CONFIG_X86_64
673         /*
674          * Some even more broken BIOSes advertise HPET at
675          * 0xfed0000000000000 instead of 0xfed00000. Fix it up and add
676          * some noise:
677          */
678         if (hpet_address == 0xfed0000000000000UL) {
679                 if (!hpet_force_user) {
680                         printk(KERN_WARNING PREFIX "HPET id: %#x "
681                                "base: 0xfed0000000000000 is bogus\n "
682                                "try hpet=force on the kernel command line to "
683                                "fix it up to 0xfed00000.\n", hpet_tbl->id);
684                         hpet_address = 0;
685                         return 0;
686                 }
687                 printk(KERN_WARNING PREFIX
688                        "HPET id: %#x base: 0xfed0000000000000 fixed up "
689                        "to 0xfed00000.\n", hpet_tbl->id);
690                 hpet_address >>= 32;
691         }
692 #endif
693         printk(KERN_INFO PREFIX "HPET id: %#x base: %#lx\n",
694                hpet_tbl->id, hpet_address);
695
696         /*
697          * Allocate and initialize the HPET firmware resource for adding into
698          * the resource tree during the lateinit timeframe.
699          */
700 #define HPET_RESOURCE_NAME_SIZE 9
701         hpet_res = alloc_bootmem(sizeof(*hpet_res) + HPET_RESOURCE_NAME_SIZE);
702
703         hpet_res->name = (void *)&hpet_res[1];
704         hpet_res->flags = IORESOURCE_MEM;
705         snprintf((char *)hpet_res->name, HPET_RESOURCE_NAME_SIZE, "HPET %u",
706                  hpet_tbl->sequence);
707
708         hpet_res->start = hpet_address;
709         hpet_res->end = hpet_address + (1 * 1024) - 1;
710
711         return 0;
712 }
713
714 /*
715  * hpet_insert_resource inserts the HPET resources used into the resource
716  * tree.
717  */
718 static __init int hpet_insert_resource(void)
719 {
720         if (!hpet_res)
721                 return 1;
722
723         return insert_resource(&iomem_resource, hpet_res);
724 }
725
726 late_initcall(hpet_insert_resource);
727
728 #else
729 #define acpi_parse_hpet NULL
730 #endif
731
732 static int __init acpi_parse_fadt(struct acpi_table_header *table)
733 {
734
735 #ifdef CONFIG_X86_PM_TIMER
736         /* detect the location of the ACPI PM Timer */
737         if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID) {
738                 /* FADT rev. 2 */
739                 if (acpi_gbl_FADT.xpm_timer_block.space_id !=
740                     ACPI_ADR_SPACE_SYSTEM_IO)
741                         return 0;
742
743                 pmtmr_ioport = acpi_gbl_FADT.xpm_timer_block.address;
744                 /*
745                  * "X" fields are optional extensions to the original V1.0
746                  * fields, so we must selectively expand V1.0 fields if the
747                  * corresponding X field is zero.
748                  */
749                 if (!pmtmr_ioport)
750                         pmtmr_ioport = acpi_gbl_FADT.pm_timer_block;
751         } else {
752                 /* FADT rev. 1 */
753                 pmtmr_ioport = acpi_gbl_FADT.pm_timer_block;
754         }
755         if (pmtmr_ioport)
756                 printk(KERN_INFO PREFIX "PM-Timer IO Port: %#x\n",
757                        pmtmr_ioport);
758 #endif
759         return 0;
760 }
761
762 #ifdef  CONFIG_X86_LOCAL_APIC
763 /*
764  * Parse LAPIC entries in MADT
765  * returns 0 on success, < 0 on error
766  */
767
768 static void __init acpi_register_lapic_address(unsigned long address)
769 {
770         mp_lapic_addr = address;
771
772         set_fixmap_nocache(FIX_APIC_BASE, address);
773         if (boot_cpu_physical_apicid == -1U) {
774                 boot_cpu_physical_apicid  = read_apic_id();
775                 apic_version[boot_cpu_physical_apicid] =
776                          GET_APIC_VERSION(apic_read(APIC_LVR));
777         }
778 }
779
780 static int __init early_acpi_parse_madt_lapic_addr_ovr(void)
781 {
782         int count;
783
784         if (!cpu_has_apic)
785                 return -ENODEV;
786
787         /*
788          * Note that the LAPIC address is obtained from the MADT (32-bit value)
789          * and (optionally) overriden by a LAPIC_ADDR_OVR entry (64-bit value).
790          */
791
792         count =
793             acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE,
794                                   acpi_parse_lapic_addr_ovr, 0);
795         if (count < 0) {
796                 printk(KERN_ERR PREFIX
797                        "Error parsing LAPIC address override entry\n");
798                 return count;
799         }
800
801         acpi_register_lapic_address(acpi_lapic_addr);
802
803         return count;
804 }
805
806 static int __init acpi_parse_madt_lapic_entries(void)
807 {
808         int count;
809
810         if (!cpu_has_apic)
811                 return -ENODEV;
812
813         /*
814          * Note that the LAPIC address is obtained from the MADT (32-bit value)
815          * and (optionally) overriden by a LAPIC_ADDR_OVR entry (64-bit value).
816          */
817
818         count =
819             acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE,
820                                   acpi_parse_lapic_addr_ovr, 0);
821         if (count < 0) {
822                 printk(KERN_ERR PREFIX
823                        "Error parsing LAPIC address override entry\n");
824                 return count;
825         }
826
827         acpi_register_lapic_address(acpi_lapic_addr);
828
829         count = acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_SAPIC,
830                                       acpi_parse_sapic, MAX_APICS);
831
832         if (!count)
833                 count = acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_APIC,
834                                               acpi_parse_lapic, MAX_APICS);
835         if (!count) {
836                 printk(KERN_ERR PREFIX "No LAPIC entries present\n");
837                 /* TBD: Cleanup to allow fallback to MPS */
838                 return -ENODEV;
839         } else if (count < 0) {
840                 printk(KERN_ERR PREFIX "Error parsing LAPIC entry\n");
841                 /* TBD: Cleanup to allow fallback to MPS */
842                 return count;
843         }
844
845         count =
846             acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_APIC_NMI, acpi_parse_lapic_nmi, 0);
847         if (count < 0) {
848                 printk(KERN_ERR PREFIX "Error parsing LAPIC NMI entry\n");
849                 /* TBD: Cleanup to allow fallback to MPS */
850                 return count;
851         }
852         return 0;
853 }
854 #endif                          /* CONFIG_X86_LOCAL_APIC */
855
856 #ifdef  CONFIG_X86_IO_APIC
857 #define MP_ISA_BUS              0
858
859 #ifdef CONFIG_X86_ES7000
860 extern int es7000_plat;
861 #endif
862
863 static struct {
864         int apic_id;
865         int gsi_base;
866         int gsi_end;
867         DECLARE_BITMAP(pin_programmed, MP_MAX_IOAPIC_PIN + 1);
868 } mp_ioapic_routing[MAX_IO_APICS];
869
870 static int mp_find_ioapic(int gsi)
871 {
872         int i = 0;
873
874         /* Find the IOAPIC that manages this GSI. */
875         for (i = 0; i < nr_ioapics; i++) {
876                 if ((gsi >= mp_ioapic_routing[i].gsi_base)
877                     && (gsi <= mp_ioapic_routing[i].gsi_end))
878                         return i;
879         }
880
881         printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
882         return -1;
883 }
884
885 static u8 __init uniq_ioapic_id(u8 id)
886 {
887 #ifdef CONFIG_X86_32
888         if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
889             !APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
890                 return io_apic_get_unique_id(nr_ioapics, id);
891         else
892                 return id;
893 #else
894         int i;
895         DECLARE_BITMAP(used, 256);
896         bitmap_zero(used, 256);
897         for (i = 0; i < nr_ioapics; i++) {
898                 struct mp_config_ioapic *ia = &mp_ioapics[i];
899                 __set_bit(ia->mp_apicid, used);
900         }
901         if (!test_bit(id, used))
902                 return id;
903         return find_first_zero_bit(used, 256);
904 #endif
905 }
906
907 static int bad_ioapic(unsigned long address)
908 {
909         if (nr_ioapics >= MAX_IO_APICS) {
910                 printk(KERN_ERR "ERROR: Max # of I/O APICs (%d) exceeded "
911                        "(found %d)\n", MAX_IO_APICS, nr_ioapics);
912                 panic("Recompile kernel with bigger MAX_IO_APICS!\n");
913         }
914         if (!address) {
915                 printk(KERN_ERR "WARNING: Bogus (zero) I/O APIC address"
916                        " found in table, skipping!\n");
917                 return 1;
918         }
919         return 0;
920 }
921
922 void __init mp_register_ioapic(int id, u32 address, u32 gsi_base)
923 {
924         int idx = 0;
925
926         if (bad_ioapic(address))
927                 return;
928
929         idx = nr_ioapics;
930
931         mp_ioapics[idx].mp_type = MP_IOAPIC;
932         mp_ioapics[idx].mp_flags = MPC_APIC_USABLE;
933         mp_ioapics[idx].mp_apicaddr = address;
934
935         set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
936         mp_ioapics[idx].mp_apicid = uniq_ioapic_id(id);
937 #ifdef CONFIG_X86_32
938         mp_ioapics[idx].mp_apicver = io_apic_get_version(idx);
939 #else
940         mp_ioapics[idx].mp_apicver = 0;
941 #endif
942         /*
943          * Build basic GSI lookup table to facilitate gsi->io_apic lookups
944          * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
945          */
946         mp_ioapic_routing[idx].apic_id = mp_ioapics[idx].mp_apicid;
947         mp_ioapic_routing[idx].gsi_base = gsi_base;
948         mp_ioapic_routing[idx].gsi_end = gsi_base +
949             io_apic_get_redir_entries(idx);
950
951         printk(KERN_INFO "IOAPIC[%d]: apic_id %d, version %d, address 0x%lx, "
952                "GSI %d-%d\n", idx, mp_ioapics[idx].mp_apicid,
953                mp_ioapics[idx].mp_apicver, mp_ioapics[idx].mp_apicaddr,
954                mp_ioapic_routing[idx].gsi_base, mp_ioapic_routing[idx].gsi_end);
955
956         nr_ioapics++;
957 }
958
959 static void assign_to_mp_irq(struct mp_config_intsrc *m,
960                                     struct mp_config_intsrc *mp_irq)
961 {
962         memcpy(mp_irq, m, sizeof(struct mp_config_intsrc));
963 }
964
965 static int mp_irq_cmp(struct mp_config_intsrc *mp_irq,
966                                 struct mp_config_intsrc *m)
967 {
968         return memcmp(mp_irq, m, sizeof(struct mp_config_intsrc));
969 }
970
971 static void save_mp_irq(struct mp_config_intsrc *m)
972 {
973         int i;
974
975         for (i = 0; i < mp_irq_entries; i++) {
976                 if (!mp_irq_cmp(&mp_irqs[i], m))
977                         return;
978         }
979
980         assign_to_mp_irq(m, &mp_irqs[mp_irq_entries]);
981         if (++mp_irq_entries == MAX_IRQ_SOURCES)
982                 panic("Max # of irq sources exceeded!!\n");
983 }
984
985 void __init mp_override_legacy_irq(u8 bus_irq, u8 polarity, u8 trigger, u32 gsi)
986 {
987         int ioapic;
988         int pin;
989         struct mp_config_intsrc mp_irq;
990
991         /*
992          * Convert 'gsi' to 'ioapic.pin'.
993          */
994         ioapic = mp_find_ioapic(gsi);
995         if (ioapic < 0)
996                 return;
997         pin = gsi - mp_ioapic_routing[ioapic].gsi_base;
998
999         /*
1000          * TBD: This check is for faulty timer entries, where the override
1001          *      erroneously sets the trigger to level, resulting in a HUGE
1002          *      increase of timer interrupts!
1003          */
1004         if ((bus_irq == 0) && (trigger == 3))
1005                 trigger = 1;
1006
1007         mp_irq.mp_type = MP_INTSRC;
1008         mp_irq.mp_irqtype = mp_INT;
1009         mp_irq.mp_irqflag = (trigger << 2) | polarity;
1010         mp_irq.mp_srcbus = MP_ISA_BUS;
1011         mp_irq.mp_srcbusirq = bus_irq;  /* IRQ */
1012         mp_irq.mp_dstapic = mp_ioapics[ioapic].mp_apicid; /* APIC ID */
1013         mp_irq.mp_dstirq = pin; /* INTIN# */
1014
1015         save_mp_irq(&mp_irq);
1016 }
1017
1018 void __init mp_config_acpi_legacy_irqs(void)
1019 {
1020         int i;
1021         int ioapic;
1022         unsigned int dstapic;
1023         struct mp_config_intsrc mp_irq;
1024
1025 #if defined (CONFIG_MCA) || defined (CONFIG_EISA)
1026         /*
1027          * Fabricate the legacy ISA bus (bus #31).
1028          */
1029         mp_bus_id_to_type[MP_ISA_BUS] = MP_BUS_ISA;
1030 #endif
1031         set_bit(MP_ISA_BUS, mp_bus_not_pci);
1032         pr_debug("Bus #%d is ISA\n", MP_ISA_BUS);
1033
1034 #ifdef CONFIG_X86_ES7000
1035         /*
1036          * Older generations of ES7000 have no legacy identity mappings
1037          */
1038         if (es7000_plat == 1)
1039                 return;
1040 #endif
1041
1042         /*
1043          * Locate the IOAPIC that manages the ISA IRQs (0-15).
1044          */
1045         ioapic = mp_find_ioapic(0);
1046         if (ioapic < 0)
1047                 return;
1048         dstapic = mp_ioapics[ioapic].mp_apicid;
1049
1050         /*
1051          * Use the default configuration for the IRQs 0-15.  Unless
1052          * overridden by (MADT) interrupt source override entries.
1053          */
1054         for (i = 0; i < 16; i++) {
1055                 int idx;
1056
1057                 for (idx = 0; idx < mp_irq_entries; idx++) {
1058                         struct mp_config_intsrc *irq = mp_irqs + idx;
1059
1060                         /* Do we already have a mapping for this ISA IRQ? */
1061                         if (irq->mp_srcbus == MP_ISA_BUS
1062                             && irq->mp_srcbusirq == i)
1063                                 break;
1064
1065                         /* Do we already have a mapping for this IOAPIC pin */
1066                         if (irq->mp_dstapic == dstapic &&
1067                             irq->mp_dstirq == i)
1068                                 break;
1069                 }
1070
1071                 if (idx != mp_irq_entries) {
1072                         printk(KERN_DEBUG "ACPI: IRQ%d used by override.\n", i);
1073                         continue;       /* IRQ already used */
1074                 }
1075
1076                 mp_irq.mp_type = MP_INTSRC;
1077                 mp_irq.mp_irqflag = 0;  /* Conforming */
1078                 mp_irq.mp_srcbus = MP_ISA_BUS;
1079                 mp_irq.mp_dstapic = dstapic;
1080                 mp_irq.mp_irqtype = mp_INT;
1081                 mp_irq.mp_srcbusirq = i; /* Identity mapped */
1082                 mp_irq.mp_dstirq = i;
1083
1084                 save_mp_irq(&mp_irq);
1085         }
1086 }
1087
1088 int mp_register_gsi(u32 gsi, int triggering, int polarity)
1089 {
1090         int ioapic;
1091         int ioapic_pin;
1092 #ifdef CONFIG_X86_32
1093 #define MAX_GSI_NUM     4096
1094 #define IRQ_COMPRESSION_START   64
1095
1096         static int pci_irq = IRQ_COMPRESSION_START;
1097         /*
1098          * Mapping between Global System Interrupts, which
1099          * represent all possible interrupts, and IRQs
1100          * assigned to actual devices.
1101          */
1102         static int gsi_to_irq[MAX_GSI_NUM];
1103 #else
1104
1105         if (acpi_irq_model != ACPI_IRQ_MODEL_IOAPIC)
1106                 return gsi;
1107 #endif
1108
1109         /* Don't set up the ACPI SCI because it's already set up */
1110         if (acpi_gbl_FADT.sci_interrupt == gsi)
1111                 return gsi;
1112
1113         ioapic = mp_find_ioapic(gsi);
1114         if (ioapic < 0) {
1115                 printk(KERN_WARNING "No IOAPIC for GSI %u\n", gsi);
1116                 return gsi;
1117         }
1118
1119         ioapic_pin = gsi - mp_ioapic_routing[ioapic].gsi_base;
1120
1121 #ifdef CONFIG_X86_32
1122         if (ioapic_renumber_irq)
1123                 gsi = ioapic_renumber_irq(ioapic, gsi);
1124 #endif
1125
1126         /*
1127          * Avoid pin reprogramming.  PRTs typically include entries
1128          * with redundant pin->gsi mappings (but unique PCI devices);
1129          * we only program the IOAPIC on the first.
1130          */
1131         if (ioapic_pin > MP_MAX_IOAPIC_PIN) {
1132                 printk(KERN_ERR "Invalid reference to IOAPIC pin "
1133                        "%d-%d\n", mp_ioapic_routing[ioapic].apic_id,
1134                        ioapic_pin);
1135                 return gsi;
1136         }
1137         if (test_bit(ioapic_pin, mp_ioapic_routing[ioapic].pin_programmed)) {
1138                 pr_debug("Pin %d-%d already programmed\n",
1139                          mp_ioapic_routing[ioapic].apic_id, ioapic_pin);
1140 #ifdef CONFIG_X86_32
1141                 return (gsi < IRQ_COMPRESSION_START ? gsi : gsi_to_irq[gsi]);
1142 #else
1143                 return gsi;
1144 #endif
1145         }
1146
1147         set_bit(ioapic_pin, mp_ioapic_routing[ioapic].pin_programmed);
1148 #ifdef CONFIG_X86_32
1149         /*
1150          * For GSI >= 64, use IRQ compression
1151          */
1152         if ((gsi >= IRQ_COMPRESSION_START)
1153             && (triggering == ACPI_LEVEL_SENSITIVE)) {
1154                 /*
1155                  * For PCI devices assign IRQs in order, avoiding gaps
1156                  * due to unused I/O APIC pins.
1157                  */
1158                 int irq = gsi;
1159                 if (gsi < MAX_GSI_NUM) {
1160                         /*
1161                          * Retain the VIA chipset work-around (gsi > 15), but
1162                          * avoid a problem where the 8254 timer (IRQ0) is setup
1163                          * via an override (so it's not on pin 0 of the ioapic),
1164                          * and at the same time, the pin 0 interrupt is a PCI
1165                          * type.  The gsi > 15 test could cause these two pins
1166                          * to be shared as IRQ0, and they are not shareable.
1167                          * So test for this condition, and if necessary, avoid
1168                          * the pin collision.
1169                          */
1170                         gsi = pci_irq++;
1171                         /*
1172                          * Don't assign IRQ used by ACPI SCI
1173                          */
1174                         if (gsi == acpi_gbl_FADT.sci_interrupt)
1175                                 gsi = pci_irq++;
1176                         gsi_to_irq[irq] = gsi;
1177                 } else {
1178                         printk(KERN_ERR "GSI %u is too high\n", gsi);
1179                         return gsi;
1180                 }
1181         }
1182 #endif
1183         io_apic_set_pci_routing(ioapic, ioapic_pin, gsi,
1184                                 triggering == ACPI_EDGE_SENSITIVE ? 0 : 1,
1185                                 polarity == ACPI_ACTIVE_HIGH ? 0 : 1);
1186         return gsi;
1187 }
1188
1189 int mp_config_acpi_gsi(unsigned char number, unsigned int devfn, u8 pin,
1190                         u32 gsi, int triggering, int polarity)
1191 {
1192 #ifdef CONFIG_X86_MPPARSE
1193         struct mp_config_intsrc mp_irq;
1194         int ioapic;
1195
1196         if (!acpi_ioapic)
1197                 return 0;
1198
1199         /* print the entry should happen on mptable identically */
1200         mp_irq.mp_type = MP_INTSRC;
1201         mp_irq.mp_irqtype = mp_INT;
1202         mp_irq.mp_irqflag = (triggering == ACPI_EDGE_SENSITIVE ? 4 : 0x0c) |
1203                                 (polarity == ACPI_ACTIVE_HIGH ? 1 : 3);
1204         mp_irq.mp_srcbus = number;
1205         mp_irq.mp_srcbusirq = (((devfn >> 3) & 0x1f) << 2) | ((pin - 1) & 3);
1206         ioapic = mp_find_ioapic(gsi);
1207         mp_irq.mp_dstapic = mp_ioapic_routing[ioapic].apic_id;
1208         mp_irq.mp_dstirq = gsi - mp_ioapic_routing[ioapic].gsi_base;
1209
1210         save_mp_irq(&mp_irq);
1211 #endif
1212         return 0;
1213 }
1214
1215 /*
1216  * Parse IOAPIC related entries in MADT
1217  * returns 0 on success, < 0 on error
1218  */
1219 static int __init acpi_parse_madt_ioapic_entries(void)
1220 {
1221         int count;
1222
1223         /*
1224          * ACPI interpreter is required to complete interrupt setup,
1225          * so if it is off, don't enumerate the io-apics with ACPI.
1226          * If MPS is present, it will handle them,
1227          * otherwise the system will stay in PIC mode
1228          */
1229         if (acpi_disabled || acpi_noirq) {
1230                 return -ENODEV;
1231         }
1232
1233         if (!cpu_has_apic)
1234                 return -ENODEV;
1235
1236         /*
1237          * if "noapic" boot option, don't look for IO-APICs
1238          */
1239         if (skip_ioapic_setup) {
1240                 printk(KERN_INFO PREFIX "Skipping IOAPIC probe "
1241                        "due to 'noapic' option.\n");
1242                 return -ENODEV;
1243         }
1244
1245         count =
1246             acpi_table_parse_madt(ACPI_MADT_TYPE_IO_APIC, acpi_parse_ioapic,
1247                                   MAX_IO_APICS);
1248         if (!count) {
1249                 printk(KERN_ERR PREFIX "No IOAPIC entries present\n");
1250                 return -ENODEV;
1251         } else if (count < 0) {
1252                 printk(KERN_ERR PREFIX "Error parsing IOAPIC entry\n");
1253                 return count;
1254         }
1255
1256         count =
1257             acpi_table_parse_madt(ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, acpi_parse_int_src_ovr,
1258                                   nr_irqs);
1259         if (count < 0) {
1260                 printk(KERN_ERR PREFIX
1261                        "Error parsing interrupt source overrides entry\n");
1262                 /* TBD: Cleanup to allow fallback to MPS */
1263                 return count;
1264         }
1265
1266         /*
1267          * If BIOS did not supply an INT_SRC_OVR for the SCI
1268          * pretend we got one so we can set the SCI flags.
1269          */
1270         if (!acpi_sci_override_gsi)
1271                 acpi_sci_ioapic_setup(acpi_gbl_FADT.sci_interrupt, 0, 0);
1272
1273         /* Fill in identity legacy mapings where no override */
1274         mp_config_acpi_legacy_irqs();
1275
1276         count =
1277             acpi_table_parse_madt(ACPI_MADT_TYPE_NMI_SOURCE, acpi_parse_nmi_src,
1278                                   nr_irqs);
1279         if (count < 0) {
1280                 printk(KERN_ERR PREFIX "Error parsing NMI SRC entry\n");
1281                 /* TBD: Cleanup to allow fallback to MPS */
1282                 return count;
1283         }
1284
1285         return 0;
1286 }
1287 #else
1288 static inline int acpi_parse_madt_ioapic_entries(void)
1289 {
1290         return -1;
1291 }
1292 #endif  /* !CONFIG_X86_IO_APIC */
1293
1294 static void __init early_acpi_process_madt(void)
1295 {
1296 #ifdef CONFIG_X86_LOCAL_APIC
1297         int error;
1298
1299         if (!acpi_table_parse(ACPI_SIG_MADT, acpi_parse_madt)) {
1300
1301                 /*
1302                  * Parse MADT LAPIC entries
1303                  */
1304                 error = early_acpi_parse_madt_lapic_addr_ovr();
1305                 if (!error) {
1306                         acpi_lapic = 1;
1307                         smp_found_config = 1;
1308                 }
1309                 if (error == -EINVAL) {
1310                         /*
1311                          * Dell Precision Workstation 410, 610 come here.
1312                          */
1313                         printk(KERN_ERR PREFIX
1314                                "Invalid BIOS MADT, disabling ACPI\n");
1315                         disable_acpi();
1316                 }
1317         }
1318 #endif
1319 }
1320
1321 static void __init acpi_process_madt(void)
1322 {
1323 #ifdef CONFIG_X86_LOCAL_APIC
1324         int error;
1325
1326         if (!acpi_table_parse(ACPI_SIG_MADT, acpi_parse_madt)) {
1327
1328                 /*
1329                  * Parse MADT LAPIC entries
1330                  */
1331                 error = acpi_parse_madt_lapic_entries();
1332                 if (!error) {
1333                         acpi_lapic = 1;
1334
1335 #ifdef CONFIG_X86_GENERICARCH
1336                         generic_bigsmp_probe();
1337 #endif
1338                         /*
1339                          * Parse MADT IO-APIC entries
1340                          */
1341                         error = acpi_parse_madt_ioapic_entries();
1342                         if (!error) {
1343                                 acpi_irq_model = ACPI_IRQ_MODEL_IOAPIC;
1344                                 acpi_ioapic = 1;
1345
1346                                 smp_found_config = 1;
1347 #ifdef CONFIG_X86_32
1348                                 setup_apic_routing();
1349 #endif
1350                         }
1351                 }
1352                 if (error == -EINVAL) {
1353                         /*
1354                          * Dell Precision Workstation 410, 610 come here.
1355                          */
1356                         printk(KERN_ERR PREFIX
1357                                "Invalid BIOS MADT, disabling ACPI\n");
1358                         disable_acpi();
1359                 }
1360         } else {
1361                 /*
1362                  * ACPI found no MADT, and so ACPI wants UP PIC mode.
1363                  * In the event an MPS table was found, forget it.
1364                  * Boot with "acpi=off" to use MPS on such a system.
1365                  */
1366                 if (smp_found_config) {
1367                         printk(KERN_WARNING PREFIX
1368                                 "No APIC-table, disabling MPS\n");
1369                         smp_found_config = 0;
1370                 }
1371         }
1372
1373         /*
1374          * ACPI supports both logical (e.g. Hyper-Threading) and physical
1375          * processors, where MPS only supports physical.
1376          */
1377         if (acpi_lapic && acpi_ioapic)
1378                 printk(KERN_INFO "Using ACPI (MADT) for SMP configuration "
1379                        "information\n");
1380         else if (acpi_lapic)
1381                 printk(KERN_INFO "Using ACPI for processor (LAPIC) "
1382                        "configuration information\n");
1383 #endif
1384         return;
1385 }
1386
1387 static int __init disable_acpi_irq(const struct dmi_system_id *d)
1388 {
1389         if (!acpi_force) {
1390                 printk(KERN_NOTICE "%s detected: force use of acpi=noirq\n",
1391                        d->ident);
1392                 acpi_noirq_set();
1393         }
1394         return 0;
1395 }
1396
1397 static int __init disable_acpi_pci(const struct dmi_system_id *d)
1398 {
1399         if (!acpi_force) {
1400                 printk(KERN_NOTICE "%s detected: force use of pci=noacpi\n",
1401                        d->ident);
1402                 acpi_disable_pci();
1403         }
1404         return 0;
1405 }
1406
1407 static int __init dmi_disable_acpi(const struct dmi_system_id *d)
1408 {
1409         if (!acpi_force) {
1410                 printk(KERN_NOTICE "%s detected: acpi off\n", d->ident);
1411                 disable_acpi();
1412         } else {
1413                 printk(KERN_NOTICE
1414                        "Warning: DMI blacklist says broken, but acpi forced\n");
1415         }
1416         return 0;
1417 }
1418
1419 /*
1420  * Limit ACPI to CPU enumeration for HT
1421  */
1422 static int __init force_acpi_ht(const struct dmi_system_id *d)
1423 {
1424         if (!acpi_force) {
1425                 printk(KERN_NOTICE "%s detected: force use of acpi=ht\n",
1426                        d->ident);
1427                 disable_acpi();
1428                 acpi_ht = 1;
1429         } else {
1430                 printk(KERN_NOTICE
1431                        "Warning: acpi=force overrules DMI blacklist: acpi=ht\n");
1432         }
1433         return 0;
1434 }
1435
1436 /*
1437  * Force ignoring BIOS IRQ0 pin2 override
1438  */
1439 static int __init dmi_ignore_irq0_timer_override(const struct dmi_system_id *d)
1440 {
1441         /*
1442          * The ati_ixp4x0_rev() early PCI quirk should have set
1443          * the acpi_skip_timer_override flag already:
1444          */
1445         if (!acpi_skip_timer_override) {
1446                 WARN(1, KERN_ERR "ati_ixp4x0 quirk not complete.\n");
1447                 pr_notice("%s detected: Ignoring BIOS IRQ0 pin2 override\n",
1448                         d->ident);
1449                 acpi_skip_timer_override = 1;
1450         }
1451         return 0;
1452 }
1453
1454 /*
1455  * If your system is blacklisted here, but you find that acpi=force
1456  * works for you, please contact acpi-devel@sourceforge.net
1457  */
1458 static struct dmi_system_id __initdata acpi_dmi_table[] = {
1459         /*
1460          * Boxes that need ACPI disabled
1461          */
1462         {
1463          .callback = dmi_disable_acpi,
1464          .ident = "IBM Thinkpad",
1465          .matches = {
1466                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1467                      DMI_MATCH(DMI_BOARD_NAME, "2629H1G"),
1468                      },
1469          },
1470
1471         /*
1472          * Boxes that need acpi=ht
1473          */
1474         {
1475          .callback = force_acpi_ht,
1476          .ident = "FSC Primergy T850",
1477          .matches = {
1478                      DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
1479                      DMI_MATCH(DMI_PRODUCT_NAME, "PRIMERGY T850"),
1480                      },
1481          },
1482         {
1483          .callback = force_acpi_ht,
1484          .ident = "HP VISUALIZE NT Workstation",
1485          .matches = {
1486                      DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
1487                      DMI_MATCH(DMI_PRODUCT_NAME, "HP VISUALIZE NT Workstation"),
1488                      },
1489          },
1490         {
1491          .callback = force_acpi_ht,
1492          .ident = "Compaq Workstation W8000",
1493          .matches = {
1494                      DMI_MATCH(DMI_SYS_VENDOR, "Compaq"),
1495                      DMI_MATCH(DMI_PRODUCT_NAME, "Workstation W8000"),
1496                      },
1497          },
1498         {
1499          .callback = force_acpi_ht,
1500          .ident = "ASUS P4B266",
1501          .matches = {
1502                      DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1503                      DMI_MATCH(DMI_BOARD_NAME, "P4B266"),
1504                      },
1505          },
1506         {
1507          .callback = force_acpi_ht,
1508          .ident = "ASUS P2B-DS",
1509          .matches = {
1510                      DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1511                      DMI_MATCH(DMI_BOARD_NAME, "P2B-DS"),
1512                      },
1513          },
1514         {
1515          .callback = force_acpi_ht,
1516          .ident = "ASUS CUR-DLS",
1517          .matches = {
1518                      DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1519                      DMI_MATCH(DMI_BOARD_NAME, "CUR-DLS"),
1520                      },
1521          },
1522         {
1523          .callback = force_acpi_ht,
1524          .ident = "ABIT i440BX-W83977",
1525          .matches = {
1526                      DMI_MATCH(DMI_BOARD_VENDOR, "ABIT <http://www.abit.com>"),
1527                      DMI_MATCH(DMI_BOARD_NAME, "i440BX-W83977 (BP6)"),
1528                      },
1529          },
1530         {
1531          .callback = force_acpi_ht,
1532          .ident = "IBM Bladecenter",
1533          .matches = {
1534                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1535                      DMI_MATCH(DMI_BOARD_NAME, "IBM eServer BladeCenter HS20"),
1536                      },
1537          },
1538         {
1539          .callback = force_acpi_ht,
1540          .ident = "IBM eServer xSeries 360",
1541          .matches = {
1542                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1543                      DMI_MATCH(DMI_BOARD_NAME, "eServer xSeries 360"),
1544                      },
1545          },
1546         {
1547          .callback = force_acpi_ht,
1548          .ident = "IBM eserver xSeries 330",
1549          .matches = {
1550                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1551                      DMI_MATCH(DMI_BOARD_NAME, "eserver xSeries 330"),
1552                      },
1553          },
1554         {
1555          .callback = force_acpi_ht,
1556          .ident = "IBM eserver xSeries 440",
1557          .matches = {
1558                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1559                      DMI_MATCH(DMI_PRODUCT_NAME, "eserver xSeries 440"),
1560                      },
1561          },
1562
1563         /*
1564          * Boxes that need ACPI PCI IRQ routing disabled
1565          */
1566         {
1567          .callback = disable_acpi_irq,
1568          .ident = "ASUS A7V",
1569          .matches = {
1570                      DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC"),
1571                      DMI_MATCH(DMI_BOARD_NAME, "<A7V>"),
1572                      /* newer BIOS, Revision 1011, does work */
1573                      DMI_MATCH(DMI_BIOS_VERSION,
1574                                "ASUS A7V ACPI BIOS Revision 1007"),
1575                      },
1576          },
1577         {
1578                 /*
1579                  * Latest BIOS for IBM 600E (1.16) has bad pcinum
1580                  * for LPC bridge, which is needed for the PCI
1581                  * interrupt links to work. DSDT fix is in bug 5966.
1582                  * 2645, 2646 model numbers are shared with 600/600E/600X
1583                  */
1584          .callback = disable_acpi_irq,
1585          .ident = "IBM Thinkpad 600 Series 2645",
1586          .matches = {
1587                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1588                      DMI_MATCH(DMI_BOARD_NAME, "2645"),
1589                      },
1590          },
1591         {
1592          .callback = disable_acpi_irq,
1593          .ident = "IBM Thinkpad 600 Series 2646",
1594          .matches = {
1595                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1596                      DMI_MATCH(DMI_BOARD_NAME, "2646"),
1597                      },
1598          },
1599         /*
1600          * Boxes that need ACPI PCI IRQ routing and PCI scan disabled
1601          */
1602         {                       /* _BBN 0 bug */
1603          .callback = disable_acpi_pci,
1604          .ident = "ASUS PR-DLS",
1605          .matches = {
1606                      DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1607                      DMI_MATCH(DMI_BOARD_NAME, "PR-DLS"),
1608                      DMI_MATCH(DMI_BIOS_VERSION,
1609                                "ASUS PR-DLS ACPI BIOS Revision 1010"),
1610                      DMI_MATCH(DMI_BIOS_DATE, "03/21/2003")
1611                      },
1612          },
1613         {
1614          .callback = disable_acpi_pci,
1615          .ident = "Acer TravelMate 36x Laptop",
1616          .matches = {
1617                      DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1618                      DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 360"),
1619                      },
1620          },
1621         {}
1622 };
1623
1624 /* second table for DMI checks that should run after early-quirks */
1625 static struct dmi_system_id __initdata acpi_dmi_table_late[] = {
1626         /*
1627          * HP laptops which use a DSDT reporting as HP/SB400/10000,
1628          * which includes some code which overrides all temperature
1629          * trip points to 16C if the INTIN2 input of the I/O APIC
1630          * is enabled.  This input is incorrectly designated the
1631          * ISA IRQ 0 via an interrupt source override even though
1632          * it is wired to the output of the master 8259A and INTIN0
1633          * is not connected at all.  Force ignoring BIOS IRQ0 pin2
1634          * override in that cases.
1635          */
1636         {
1637          .callback = dmi_ignore_irq0_timer_override,
1638          .ident = "HP nx6115 laptop",
1639          .matches = {
1640                      DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1641                      DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq nx6115"),
1642                      },
1643          },
1644         {
1645          .callback = dmi_ignore_irq0_timer_override,
1646          .ident = "HP NX6125 laptop",
1647          .matches = {
1648                      DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1649                      DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq nx6125"),
1650                      },
1651          },
1652         {
1653          .callback = dmi_ignore_irq0_timer_override,
1654          .ident = "HP NX6325 laptop",
1655          .matches = {
1656                      DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1657                      DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq nx6325"),
1658                      },
1659          },
1660         {
1661          .callback = dmi_ignore_irq0_timer_override,
1662          .ident = "HP 6715b laptop",
1663          .matches = {
1664                      DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1665                      DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq 6715b"),
1666                      },
1667          },
1668         {}
1669 };
1670
1671 /*
1672  * acpi_boot_table_init() and acpi_boot_init()
1673  *  called from setup_arch(), always.
1674  *      1. checksums all tables
1675  *      2. enumerates lapics
1676  *      3. enumerates io-apics
1677  *
1678  * acpi_table_init() is separate to allow reading SRAT without
1679  * other side effects.
1680  *
1681  * side effects of acpi_boot_init:
1682  *      acpi_lapic = 1 if LAPIC found
1683  *      acpi_ioapic = 1 if IOAPIC found
1684  *      if (acpi_lapic && acpi_ioapic) smp_found_config = 1;
1685  *      if acpi_blacklisted() acpi_disabled = 1;
1686  *      acpi_irq_model=...
1687  *      ...
1688  *
1689  * return value: (currently ignored)
1690  *      0: success
1691  *      !0: failure
1692  */
1693
1694 int __init acpi_boot_table_init(void)
1695 {
1696         int error;
1697
1698         dmi_check_system(acpi_dmi_table);
1699
1700         /*
1701          * If acpi_disabled, bail out
1702          * One exception: acpi=ht continues far enough to enumerate LAPICs
1703          */
1704         if (acpi_disabled && !acpi_ht)
1705                 return 1;
1706
1707         /*
1708          * Initialize the ACPI boot-time table parser.
1709          */
1710         error = acpi_table_init();
1711         if (error) {
1712                 disable_acpi();
1713                 return error;
1714         }
1715
1716         acpi_table_parse(ACPI_SIG_BOOT, acpi_parse_sbf);
1717
1718         /*
1719          * blacklist may disable ACPI entirely
1720          */
1721         error = acpi_blacklisted();
1722         if (error) {
1723                 if (acpi_force) {
1724                         printk(KERN_WARNING PREFIX "acpi=force override\n");
1725                 } else {
1726                         printk(KERN_WARNING PREFIX "Disabling ACPI support\n");
1727                         disable_acpi();
1728                         return error;
1729                 }
1730         }
1731
1732         return 0;
1733 }
1734
1735 int __init early_acpi_boot_init(void)
1736 {
1737         /*
1738          * If acpi_disabled, bail out
1739          * One exception: acpi=ht continues far enough to enumerate LAPICs
1740          */
1741         if (acpi_disabled && !acpi_ht)
1742                 return 1;
1743
1744         /*
1745          * Process the Multiple APIC Description Table (MADT), if present
1746          */
1747         early_acpi_process_madt();
1748
1749         return 0;
1750 }
1751
1752 int __init acpi_boot_init(void)
1753 {
1754         /* those are executed after early-quirks are executed */
1755         dmi_check_system(acpi_dmi_table_late);
1756
1757         /*
1758          * If acpi_disabled, bail out
1759          * One exception: acpi=ht continues far enough to enumerate LAPICs
1760          */
1761         if (acpi_disabled && !acpi_ht)
1762                 return 1;
1763
1764         acpi_table_parse(ACPI_SIG_BOOT, acpi_parse_sbf);
1765
1766         /*
1767          * set sci_int and PM timer address
1768          */
1769         acpi_table_parse(ACPI_SIG_FADT, acpi_parse_fadt);
1770
1771         /*
1772          * Process the Multiple APIC Description Table (MADT), if present
1773          */
1774         acpi_process_madt();
1775
1776         acpi_table_parse(ACPI_SIG_HPET, acpi_parse_hpet);
1777
1778         return 0;
1779 }
1780
1781 static int __init parse_acpi(char *arg)
1782 {
1783         if (!arg)
1784                 return -EINVAL;
1785
1786         /* "acpi=off" disables both ACPI table parsing and interpreter */
1787         if (strcmp(arg, "off") == 0) {
1788                 disable_acpi();
1789         }
1790         /* acpi=force to over-ride black-list */
1791         else if (strcmp(arg, "force") == 0) {
1792                 acpi_force = 1;
1793                 acpi_ht = 1;
1794                 acpi_disabled = 0;
1795         }
1796         /* acpi=strict disables out-of-spec workarounds */
1797         else if (strcmp(arg, "strict") == 0) {
1798                 acpi_strict = 1;
1799         }
1800         /* Limit ACPI just to boot-time to enable HT */
1801         else if (strcmp(arg, "ht") == 0) {
1802                 if (!acpi_force)
1803                         disable_acpi();
1804                 acpi_ht = 1;
1805         }
1806         /* acpi=rsdt use RSDT instead of XSDT */
1807         else if (strcmp(arg, "rsdt") == 0) {
1808                 acpi_rsdt_forced = 1;
1809         }
1810         /* "acpi=noirq" disables ACPI interrupt routing */
1811         else if (strcmp(arg, "noirq") == 0) {
1812                 acpi_noirq_set();
1813         } else {
1814                 /* Core will printk when we return error. */
1815                 return -EINVAL;
1816         }
1817         return 0;
1818 }
1819 early_param("acpi", parse_acpi);
1820
1821 /* FIXME: Using pci= for an ACPI parameter is a travesty. */
1822 static int __init parse_pci(char *arg)
1823 {
1824         if (arg && strcmp(arg, "noacpi") == 0)
1825                 acpi_disable_pci();
1826         return 0;
1827 }
1828 early_param("pci", parse_pci);
1829
1830 int __init acpi_mps_check(void)
1831 {
1832 #if defined(CONFIG_X86_LOCAL_APIC) && !defined(CONFIG_X86_MPPARSE)
1833 /* mptable code is not built-in*/
1834         if (acpi_disabled || acpi_noirq) {
1835                 printk(KERN_WARNING "MPS support code is not built-in.\n"
1836                        "Using acpi=off or acpi=noirq or pci=noacpi "
1837                        "may have problem\n");
1838                 return 1;
1839         }
1840 #endif
1841         return 0;
1842 }
1843
1844 #ifdef CONFIG_X86_IO_APIC
1845 static int __init parse_acpi_skip_timer_override(char *arg)
1846 {
1847         acpi_skip_timer_override = 1;
1848         return 0;
1849 }
1850 early_param("acpi_skip_timer_override", parse_acpi_skip_timer_override);
1851
1852 static int __init parse_acpi_use_timer_override(char *arg)
1853 {
1854         acpi_use_timer_override = 1;
1855         return 0;
1856 }
1857 early_param("acpi_use_timer_override", parse_acpi_use_timer_override);
1858 #endif /* CONFIG_X86_IO_APIC */
1859
1860 static int __init setup_acpi_sci(char *s)
1861 {
1862         if (!s)
1863                 return -EINVAL;
1864         if (!strcmp(s, "edge"))
1865                 acpi_sci_flags =  ACPI_MADT_TRIGGER_EDGE |
1866                         (acpi_sci_flags & ~ACPI_MADT_TRIGGER_MASK);
1867         else if (!strcmp(s, "level"))
1868                 acpi_sci_flags = ACPI_MADT_TRIGGER_LEVEL |
1869                         (acpi_sci_flags & ~ACPI_MADT_TRIGGER_MASK);
1870         else if (!strcmp(s, "high"))
1871                 acpi_sci_flags = ACPI_MADT_POLARITY_ACTIVE_HIGH |
1872                         (acpi_sci_flags & ~ACPI_MADT_POLARITY_MASK);
1873         else if (!strcmp(s, "low"))
1874                 acpi_sci_flags = ACPI_MADT_POLARITY_ACTIVE_LOW |
1875                         (acpi_sci_flags & ~ACPI_MADT_POLARITY_MASK);
1876         else
1877                 return -EINVAL;
1878         return 0;
1879 }
1880 early_param("acpi_sci", setup_acpi_sci);
1881
1882 int __acpi_acquire_global_lock(unsigned int *lock)
1883 {
1884         unsigned int old, new, val;
1885         do {
1886                 old = *lock;
1887                 new = (((old & ~0x3) + 2) + ((old >> 1) & 0x1));
1888                 val = cmpxchg(lock, old, new);
1889         } while (unlikely (val != old));
1890         return (new < 3) ? -1 : 0;
1891 }
1892
1893 int __acpi_release_global_lock(unsigned int *lock)
1894 {
1895         unsigned int old, new, val;
1896         do {
1897                 old = *lock;
1898                 new = old & ~0x3;
1899                 val = cmpxchg(lock, old, new);
1900         } while (unlikely (val != old));
1901         return old & 0x1;
1902 }