+- add patches.fixes/linux-post-2.6.3-20040220
[linux-flexiantxendom0-3.2.10.git] / arch / i386 / 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/config.h>
28 #include <linux/acpi.h>
29 #include <linux/efi.h>
30 #include <linux/irq.h>
31 #include <asm/pgalloc.h>
32 #include <asm/io_apic.h>
33 #include <asm/apic.h>
34 #include <asm/io.h>
35 #include <asm/irq.h>
36 #include <asm/mpspec.h>
37
38 #if defined (CONFIG_X86_LOCAL_APIC)
39 #include <mach_apic.h>
40 #include <mach_mpparse.h>
41 #include <asm/io_apic.h>
42 #endif
43
44 #define PREFIX                  "ACPI: "
45
46 int acpi_noirq __initdata = 0;  /* skip ACPI IRQ initialization */
47 int acpi_ht __initdata = 1;     /* enable HT */
48
49 int acpi_lapic;
50 int acpi_ioapic;
51
52 /* --------------------------------------------------------------------------
53                               Boot-time Configuration
54    -------------------------------------------------------------------------- */
55
56 enum acpi_irq_model_id          acpi_irq_model;
57
58 /*
59  * Temporarily use the virtual area starting from FIX_IO_APIC_BASE_END,
60  * to map the target physical address. The problem is that set_fixmap()
61  * provides a single page, and it is possible that the page is not
62  * sufficient.
63  * By using this area, we can map up to MAX_IO_APICS pages temporarily,
64  * i.e. until the next __va_range() call.
65  *
66  * Important Safety Note:  The fixed I/O APIC page numbers are *subtracted*
67  * from the fixed base.  That's why we start at FIX_IO_APIC_BASE_END and
68  * count idx down while incrementing the phys address.
69  */
70 char *__acpi_map_table(unsigned long phys, unsigned long size)
71 {
72         unsigned long base, offset, mapped_size;
73         int idx;
74
75         if (phys + size < 8*1024*1024) 
76                 return __va(phys); 
77
78         offset = phys & (PAGE_SIZE - 1);
79         mapped_size = PAGE_SIZE - offset;
80         set_fixmap(FIX_ACPI_END, phys);
81         base = fix_to_virt(FIX_ACPI_END);
82
83         /*
84          * Most cases can be covered by the below.
85          */
86         idx = FIX_ACPI_END;
87         while (mapped_size < size) {
88                 if (--idx < FIX_ACPI_BEGIN)
89                         return 0;       /* cannot handle this */
90                 phys += PAGE_SIZE;
91                 set_fixmap(idx, phys);
92                 mapped_size += PAGE_SIZE;
93         }
94
95         return ((unsigned char *) base + offset);
96 }
97
98
99 #ifdef CONFIG_X86_LOCAL_APIC
100
101 static u64 acpi_lapic_addr __initdata = APIC_DEFAULT_PHYS_BASE;
102
103
104 static int __init
105 acpi_parse_madt (
106         unsigned long           phys_addr,
107         unsigned long           size)
108 {
109         struct acpi_table_madt  *madt = NULL;
110
111         if (!phys_addr || !size)
112                 return -EINVAL;
113
114         madt = (struct acpi_table_madt *) __acpi_map_table(phys_addr, size);
115         if (!madt) {
116                 printk(KERN_WARNING PREFIX "Unable to map MADT\n");
117                 return -ENODEV;
118         }
119
120         if (madt->lapic_address)
121                 acpi_lapic_addr = (u64) madt->lapic_address;
122
123         printk(KERN_INFO PREFIX "Local APIC address 0x%08x\n",
124                 madt->lapic_address);
125
126         acpi_madt_oem_check(madt->header.oem_id, madt->header.oem_table_id);
127         
128         return 0;
129 }
130
131
132 static int __init
133 acpi_parse_lapic (
134         acpi_table_entry_header *header)
135 {
136         struct acpi_table_lapic *processor = NULL;
137
138         processor = (struct acpi_table_lapic*) header;
139         if (!processor)
140                 return -EINVAL;
141
142         acpi_table_print_madt_entry(header);
143
144         /* no utility in registering a disabled processor */
145         if (processor->flags.enabled == 0)
146                 return 0;
147
148         mp_register_lapic (
149                 processor->id,                                     /* APIC ID */
150                 processor->flags.enabled);                        /* Enabled? */
151
152         return 0;
153 }
154
155
156 static int __init
157 acpi_parse_lapic_addr_ovr (
158         acpi_table_entry_header *header)
159 {
160         struct acpi_table_lapic_addr_ovr *lapic_addr_ovr = NULL;
161
162         lapic_addr_ovr = (struct acpi_table_lapic_addr_ovr*) header;
163         if (!lapic_addr_ovr)
164                 return -EINVAL;
165
166         acpi_lapic_addr = lapic_addr_ovr->address;
167
168         return 0;
169 }
170
171 static int __init
172 acpi_parse_lapic_nmi (
173         acpi_table_entry_header *header)
174 {
175         struct acpi_table_lapic_nmi *lapic_nmi = NULL;
176
177         lapic_nmi = (struct acpi_table_lapic_nmi*) header;
178         if (!lapic_nmi)
179                 return -EINVAL;
180
181         acpi_table_print_madt_entry(header);
182
183         if (lapic_nmi->lint != 1)
184                 printk(KERN_WARNING PREFIX "NMI not connected to LINT 1!\n");
185
186         return 0;
187 }
188
189
190 #endif /*CONFIG_X86_LOCAL_APIC*/
191
192 #if defined(CONFIG_X86_IO_APIC) && defined(CONFIG_ACPI_INTERPRETER)
193
194 static int __init
195 acpi_parse_ioapic (
196         acpi_table_entry_header *header)
197 {
198         struct acpi_table_ioapic *ioapic = NULL;
199
200         ioapic = (struct acpi_table_ioapic*) header;
201         if (!ioapic)
202                 return -EINVAL;
203  
204         acpi_table_print_madt_entry(header);
205
206         mp_register_ioapic (
207                 ioapic->id,
208                 ioapic->address,
209                 ioapic->global_irq_base);
210  
211         return 0;
212 }
213
214
215 static int __init
216 acpi_parse_int_src_ovr (
217         acpi_table_entry_header *header)
218 {
219         struct acpi_table_int_src_ovr *intsrc = NULL;
220
221         intsrc = (struct acpi_table_int_src_ovr*) header;
222         if (!intsrc)
223                 return -EINVAL;
224
225         acpi_table_print_madt_entry(header);
226
227         mp_override_legacy_irq (
228                 intsrc->bus_irq,
229                 intsrc->flags.polarity,
230                 intsrc->flags.trigger,
231                 intsrc->global_irq);
232
233         return 0;
234 }
235
236
237 static int __init
238 acpi_parse_nmi_src (
239         acpi_table_entry_header *header)
240 {
241         struct acpi_table_nmi_src *nmi_src = NULL;
242
243         nmi_src = (struct acpi_table_nmi_src*) header;
244         if (!nmi_src)
245                 return -EINVAL;
246
247         acpi_table_print_madt_entry(header);
248
249         /* TBD: Support nimsrc entries? */
250
251         return 0;
252 }
253
254 #endif /*CONFIG_X86_IO_APIC*/
255
256 #ifdef  CONFIG_ACPI_BUS
257 /*
258  * "acpi_pic_sci=level" (current default)
259  * programs the PIC-mode SCI to Level Trigger.
260  * (NO-OP if the BIOS set Level Trigger already)
261  *
262  * If a PIC-mode SCI is not recogznied or gives spurious IRQ7's
263  * it may require Edge Trigger -- use "acpi_pic_sci=edge"
264  * (NO-OP if the BIOS set Edge Trigger already)
265  *
266  * Port 0x4d0-4d1 are ECLR1 and ECLR2, the Edge/Level Control Registers
267  * for the 8259 PIC.  bit[n] = 1 means irq[n] is Level, otherwise Edge.
268  * ECLR1 is IRQ's 0-7 (IRQ 0, 1, 2 must be 0)
269  * ECLR2 is IRQ's 8-15 (IRQ 8, 13 must be 0)
270  */
271
272 static int __initdata   acpi_pic_sci_trigger;   /* 0: level, 1: edge */
273
274 void __init
275 acpi_pic_sci_set_trigger(unsigned int irq)
276 {
277         unsigned char mask = 1 << (irq & 7);
278         unsigned int port = 0x4d0 + (irq >> 3);
279         unsigned char val = inb(port);
280
281         
282         printk(PREFIX "IRQ%d SCI:", irq);
283         if (!(val & mask)) {
284                 printk(" Edge");
285
286                 if (!acpi_pic_sci_trigger) {
287                         printk(" set to Level");
288                         outb(val | mask, port);
289                 }
290         } else {
291                 printk(" Level");
292
293                 if (acpi_pic_sci_trigger) {
294                         printk(" set to Edge");
295                         outb(val | mask, port);
296                 }
297         }
298         printk(" Trigger.\n");
299 }
300
301 int __init
302 acpi_pic_sci_setup(char *str)
303 {
304         while (str && *str) {
305                 if (strncmp(str, "level", 5) == 0)
306                         acpi_pic_sci_trigger = 0;       /* force level trigger */
307                 if (strncmp(str, "edge", 4) == 0)
308                         acpi_pic_sci_trigger = 1;       /* force edge trigger */
309                 str = strchr(str, ',');
310                 if (str)
311                         str += strspn(str, ", \t");
312         }
313         return 1;
314 }
315
316 __setup("acpi_pic_sci=", acpi_pic_sci_setup);
317
318 #endif /* CONFIG_ACPI_BUS */
319
320 #ifdef CONFIG_X86_IO_APIC
321 int acpi_irq_to_vector(u32 irq)
322 {
323         if (use_pci_vector() && !platform_legacy_irq(irq))
324                 irq = IO_APIC_VECTOR(irq);
325         return irq;
326 }
327 #endif
328
329 static unsigned long __init
330 acpi_scan_rsdp (
331         unsigned long           start,
332         unsigned long           length)
333 {
334         unsigned long           offset = 0;
335         unsigned long           sig_len = sizeof("RSD PTR ") - 1;
336
337         /*
338          * Scan all 16-byte boundaries of the physical memory region for the
339          * RSDP signature.
340          */
341         for (offset = 0; offset < length; offset += 16) {
342                 if (strncmp((char *) (start + offset), "RSD PTR ", sig_len))
343                         continue;
344                 return (start + offset);
345         }
346
347         return 0;
348 }
349
350 #ifdef CONFIG_HPET_TIMER
351 extern unsigned long hpet_address;
352
353 static int __init acpi_parse_hpet(unsigned long phys, unsigned long size)
354 {
355         struct acpi_table_hpet *hpet_tbl;
356
357         if (!phys || !size)
358                 return -EINVAL;
359
360         hpet_tbl = (struct acpi_table_hpet *) __acpi_map_table(phys, size);
361         if (!hpet_tbl) {
362                 printk(KERN_WARNING PREFIX "Unable to map HPET\n");
363                 return -ENODEV;
364         }
365
366         if (hpet_tbl->addr.space_id != ACPI_SPACE_MEM) {
367                 printk(KERN_WARNING PREFIX "HPET timers must be located in "
368                        "memory.\n");
369                 return -1;
370         }
371
372         hpet_address = hpet_tbl->addr.addrl;
373         printk(KERN_INFO PREFIX "HPET id: %#x base: %#lx\n", hpet_tbl->id,
374                hpet_address);
375         return 0;
376 }
377 #endif
378
379 /* detect the location of the ACPI PM Timer */
380 #ifdef CONFIG_X86_PM_TIMER
381 extern u32 pmtmr_ioport;
382
383 static int __init acpi_parse_fadt(unsigned long phys, unsigned long size)
384 {
385         struct fadt_descriptor_rev2 *fadt =0;
386
387         fadt = (struct fadt_descriptor_rev2*) __acpi_map_table(phys,size);
388         if(!fadt) {
389                 printk(KERN_WARNING PREFIX "Unable to map FADT\n");
390                 return 0;
391         }
392
393         if (fadt->revision >= FADT2_REVISION_ID) {
394                 /* FADT rev. 2 */
395                 if (fadt->xpm_tmr_blk.address_space_id != ACPI_ADR_SPACE_SYSTEM_IO)
396                         return 0;
397
398                 pmtmr_ioport = fadt->xpm_tmr_blk.address;
399         } else {
400                 /* FADT rev. 1 */
401                 pmtmr_ioport = fadt->V1_pm_tmr_blk;
402         }
403         if (pmtmr_ioport)
404                 printk(KERN_INFO PREFIX "PM-Timer IO Port: %#x\n", pmtmr_ioport);
405         return 0;
406 }
407 #endif
408
409
410 unsigned long __init
411 acpi_find_rsdp (void)
412 {
413         unsigned long           rsdp_phys = 0;
414
415         if (efi_enabled) {
416                 if (efi.acpi20)
417                         return __pa(efi.acpi20);
418                 else if (efi.acpi)
419                         return __pa(efi.acpi);
420         }
421         /*
422          * Scan memory looking for the RSDP signature. First search EBDA (low
423          * memory) paragraphs and then search upper memory (E0000-FFFFF).
424          */
425         rsdp_phys = acpi_scan_rsdp (0, 0x400);
426         if (!rsdp_phys)
427                 rsdp_phys = acpi_scan_rsdp (0xE0000, 0xFFFFF);
428
429         return rsdp_phys;
430 }
431
432 /*
433  * acpi_boot_init()
434  *  called from setup_arch(), always.
435  *      1. maps ACPI tables for later use
436  *      2. enumerates lapics
437  *      3. enumerates io-apics
438  *
439  * side effects:
440  *      acpi_lapic = 1 if LAPIC found
441  *      acpi_ioapic = 1 if IOAPIC found
442  *      if (acpi_lapic && acpi_ioapic) smp_found_config = 1;
443  *      if acpi_blacklisted() acpi_disabled = 1;
444  *      acpi_irq_model=...
445  *      ...
446  *
447  * return value: (currently ignored)
448  *      0: success
449  *      !0: failure
450  */
451
452 int __init
453 acpi_boot_init (void)
454 {
455         int                     result = 0;
456
457         if (acpi_disabled && !acpi_ht)
458                  return 1;
459
460         /*
461          * The default interrupt routing model is PIC (8259).  This gets
462          * overriden if IOAPICs are enumerated (below).
463          */
464         acpi_irq_model = ACPI_IRQ_MODEL_PIC;
465
466         /* 
467          * Initialize the ACPI boot-time table parser.
468          */
469         result = acpi_table_init();
470         if (result) {
471                 acpi_disabled = 1;
472                 return result;
473         }
474
475         result = acpi_blacklisted();
476         if (result) {
477                 printk(KERN_WARNING PREFIX "BIOS listed in blacklist, disabling ACPI support\n");
478                 acpi_disabled = 1;
479                 return result;
480         }
481
482 #ifdef CONFIG_X86_PM_TIMER
483         acpi_table_parse(ACPI_FADT, acpi_parse_fadt);
484 #endif
485
486 #ifdef CONFIG_X86_LOCAL_APIC
487
488         /* 
489          * MADT
490          * ----
491          * Parse the Multiple APIC Description Table (MADT), if exists.
492          * Note that this table provides platform SMP configuration 
493          * information -- the successor to MPS tables.
494          */
495
496         result = acpi_table_parse(ACPI_APIC, acpi_parse_madt);
497         if (!result) {
498                 return 0;
499         }
500         else if (result < 0) {
501                 printk(KERN_ERR PREFIX "Error parsing MADT\n");
502                 return result;
503         }
504         else if (result > 1) 
505                 printk(KERN_WARNING PREFIX "Multiple MADT tables exist\n");
506
507         /* 
508          * Local APIC
509          * ----------
510          * Note that the LAPIC address is obtained from the MADT (32-bit value)
511          * and (optionally) overriden by a LAPIC_ADDR_OVR entry (64-bit value).
512          */
513
514         result = acpi_table_parse_madt(ACPI_MADT_LAPIC_ADDR_OVR, acpi_parse_lapic_addr_ovr, 0);
515         if (result < 0) {
516                 printk(KERN_ERR PREFIX "Error parsing LAPIC address override entry\n");
517                 return result;
518         }
519
520         mp_register_lapic_address(acpi_lapic_addr);
521
522         result = acpi_table_parse_madt(ACPI_MADT_LAPIC, acpi_parse_lapic,
523                                        MAX_APICS);
524         if (!result) { 
525                 printk(KERN_ERR PREFIX "No LAPIC entries present\n");
526                 /* TBD: Cleanup to allow fallback to MPS */
527                 return -ENODEV;
528         }
529         else if (result < 0) {
530                 printk(KERN_ERR PREFIX "Error parsing LAPIC entry\n");
531                 /* TBD: Cleanup to allow fallback to MPS */
532                 return result;
533         }
534
535         result = acpi_table_parse_madt(ACPI_MADT_LAPIC_NMI, acpi_parse_lapic_nmi, 0);
536         if (result < 0) {
537                 printk(KERN_ERR PREFIX "Error parsing LAPIC NMI entry\n");
538                 /* TBD: Cleanup to allow fallback to MPS */
539                 return result;
540         }
541
542         acpi_lapic = 1;
543
544 #endif /*CONFIG_X86_LOCAL_APIC*/
545
546 #if defined(CONFIG_X86_IO_APIC) && defined(CONFIG_ACPI_INTERPRETER)
547
548         /* 
549          * I/O APIC 
550          * --------
551          */
552
553         /*
554          * ACPI interpreter is required to complete interrupt setup,
555          * so if it is off, don't enumerate the io-apics with ACPI.
556          * If MPS is present, it will handle them,
557          * otherwise the system will stay in PIC mode
558          */
559         if (acpi_disabled || acpi_noirq) {
560                 return 1;
561         }
562
563         /*
564          * if "noapic" boot option, don't look for IO-APICs
565          */
566         if (ioapic_setup_disabled()) {
567                 printk(KERN_INFO PREFIX "Skipping IOAPIC probe "
568                         "due to 'noapic' option.\n");
569                 return 1;
570         }
571
572         result = acpi_table_parse_madt(ACPI_MADT_IOAPIC, acpi_parse_ioapic, MAX_IO_APICS);
573         if (!result) {
574                 printk(KERN_ERR PREFIX "No IOAPIC entries present\n");
575                 return -ENODEV;
576         }
577         else if (result < 0) {
578                 printk(KERN_ERR PREFIX "Error parsing IOAPIC entry\n");
579                 return result;
580         }
581
582         /* Build a default routing table for legacy (ISA) interrupts. */
583         mp_config_acpi_legacy_irqs();
584
585         result = acpi_table_parse_madt(ACPI_MADT_INT_SRC_OVR, acpi_parse_int_src_ovr, NR_IRQ_VECTORS);
586         if (result < 0) {
587                 printk(KERN_ERR PREFIX "Error parsing interrupt source overrides entry\n");
588                 /* TBD: Cleanup to allow fallback to MPS */
589                 return result;
590         }
591
592         result = acpi_table_parse_madt(ACPI_MADT_NMI_SRC, acpi_parse_nmi_src, NR_IRQ_VECTORS);
593         if (result < 0) {
594                 printk(KERN_ERR PREFIX "Error parsing NMI SRC entry\n");
595                 /* TBD: Cleanup to allow fallback to MPS */
596                 return result;
597         }
598
599         acpi_irq_model = ACPI_IRQ_MODEL_IOAPIC;
600
601         acpi_irq_balance_set(NULL);
602
603         acpi_ioapic = 1;
604
605 #endif /* CONFIG_X86_IO_APIC && CONFIG_ACPI_INTERPRETER */
606
607 #ifdef CONFIG_X86_LOCAL_APIC
608         if (acpi_lapic && acpi_ioapic) {
609                 smp_found_config = 1;
610                 clustered_apic_check();
611         }
612 #endif
613
614 #ifdef CONFIG_HPET_TIMER
615         acpi_table_parse(ACPI_HPET, acpi_parse_hpet);
616 #endif
617
618         return 0;
619 }