]> err.no Git - linux-2.6/blob - arch/x86/kernel/acpi/boot.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/steve/gfs2-2.6-nmw
[linux-2.6] / 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/io.h>
41 #include <asm/mpspec.h>
42 #include <asm/smp.h>
43
44 #ifdef CONFIG_X86_LOCAL_APIC
45 # include <mach_apic.h>
46 #endif
47
48 static int __initdata acpi_force = 0;
49
50 #ifdef  CONFIG_ACPI
51 int acpi_disabled = 0;
52 #else
53 int acpi_disabled = 1;
54 #endif
55 EXPORT_SYMBOL(acpi_disabled);
56
57 #ifdef  CONFIG_X86_64
58
59 #include <asm/proto.h>
60 #include <asm/genapic.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 #ifdef  CONFIG_X86_64
110
111 /* rely on all ACPI tables being in the direct mapping */
112 char *__init __acpi_map_table(unsigned long phys_addr, unsigned long size)
113 {
114         if (!phys_addr || !size)
115                 return NULL;
116
117         if (phys_addr+size <= (max_pfn_mapped << PAGE_SHIFT) + PAGE_SIZE)
118                 return __va(phys_addr);
119
120         return NULL;
121 }
122
123 #else
124
125 /*
126  * Temporarily use the virtual area starting from FIX_IO_APIC_BASE_END,
127  * to map the target physical address. The problem is that set_fixmap()
128  * provides a single page, and it is possible that the page is not
129  * sufficient.
130  * By using this area, we can map up to MAX_IO_APICS pages temporarily,
131  * i.e. until the next __va_range() call.
132  *
133  * Important Safety Note:  The fixed I/O APIC page numbers are *subtracted*
134  * from the fixed base.  That's why we start at FIX_IO_APIC_BASE_END and
135  * count idx down while incrementing the phys address.
136  */
137 char *__init __acpi_map_table(unsigned long phys, unsigned long size)
138 {
139         unsigned long base, offset, mapped_size;
140         int idx;
141
142         if (phys + size < 8 * 1024 * 1024)
143                 return __va(phys);
144
145         offset = phys & (PAGE_SIZE - 1);
146         mapped_size = PAGE_SIZE - offset;
147         set_fixmap(FIX_ACPI_END, phys);
148         base = fix_to_virt(FIX_ACPI_END);
149
150         /*
151          * Most cases can be covered by the below.
152          */
153         idx = FIX_ACPI_END;
154         while (mapped_size < size) {
155                 if (--idx < FIX_ACPI_BEGIN)
156                         return NULL;    /* cannot handle this */
157                 phys += PAGE_SIZE;
158                 set_fixmap(idx, phys);
159                 mapped_size += PAGE_SIZE;
160         }
161
162         return ((unsigned char *)base + offset);
163 }
164 #endif
165
166 #ifdef CONFIG_PCI_MMCONFIG
167 /* The physical address of the MMCONFIG aperture.  Set from ACPI tables. */
168 struct acpi_mcfg_allocation *pci_mmcfg_config;
169 int pci_mmcfg_config_num;
170
171 int __init acpi_parse_mcfg(struct acpi_table_header *header)
172 {
173         struct acpi_table_mcfg *mcfg;
174         unsigned long i;
175         int config_size;
176
177         if (!header)
178                 return -EINVAL;
179
180         mcfg = (struct acpi_table_mcfg *)header;
181
182         /* how many config structures do we have */
183         pci_mmcfg_config_num = 0;
184         i = header->length - sizeof(struct acpi_table_mcfg);
185         while (i >= sizeof(struct acpi_mcfg_allocation)) {
186                 ++pci_mmcfg_config_num;
187                 i -= sizeof(struct acpi_mcfg_allocation);
188         };
189         if (pci_mmcfg_config_num == 0) {
190                 printk(KERN_ERR PREFIX "MMCONFIG has no entries\n");
191                 return -ENODEV;
192         }
193
194         config_size = pci_mmcfg_config_num * sizeof(*pci_mmcfg_config);
195         pci_mmcfg_config = kmalloc(config_size, GFP_KERNEL);
196         if (!pci_mmcfg_config) {
197                 printk(KERN_WARNING PREFIX
198                        "No memory for MCFG config tables\n");
199                 return -ENOMEM;
200         }
201
202         memcpy(pci_mmcfg_config, &mcfg[1], config_size);
203         for (i = 0; i < pci_mmcfg_config_num; ++i) {
204                 if (pci_mmcfg_config[i].address > 0xFFFFFFFF) {
205                         printk(KERN_ERR PREFIX
206                                "MMCONFIG not in low 4GB of memory\n");
207                         kfree(pci_mmcfg_config);
208                         pci_mmcfg_config_num = 0;
209                         return -ENODEV;
210                 }
211         }
212
213         return 0;
214 }
215 #endif                          /* CONFIG_PCI_MMCONFIG */
216
217 #ifdef CONFIG_X86_LOCAL_APIC
218 static int __init acpi_parse_madt(struct acpi_table_header *table)
219 {
220         struct acpi_table_madt *madt = NULL;
221
222         if (!cpu_has_apic)
223                 return -EINVAL;
224
225         madt = (struct acpi_table_madt *)table;
226         if (!madt) {
227                 printk(KERN_WARNING PREFIX "Unable to map MADT\n");
228                 return -ENODEV;
229         }
230
231         if (madt->address) {
232                 acpi_lapic_addr = (u64) madt->address;
233
234                 printk(KERN_DEBUG PREFIX "Local APIC address 0x%08x\n",
235                        madt->address);
236         }
237
238         acpi_madt_oem_check(madt->header.oem_id, madt->header.oem_table_id);
239
240         return 0;
241 }
242
243 static void __cpuinit acpi_register_lapic(int id, u8 enabled)
244 {
245         if (!enabled) {
246                 ++disabled_cpus;
247                 return;
248         }
249
250         generic_processor_info(id, 0);
251 }
252
253 static int __init
254 acpi_parse_lapic(struct acpi_subtable_header * header, const unsigned long end)
255 {
256         struct acpi_madt_local_apic *processor = NULL;
257
258         processor = (struct acpi_madt_local_apic *)header;
259
260         if (BAD_MADT_ENTRY(processor, end))
261                 return -EINVAL;
262
263         acpi_table_print_madt_entry(header);
264
265         /*
266          * We need to register disabled CPU as well to permit
267          * counting disabled CPUs. This allows us to size
268          * cpus_possible_map more accurately, to permit
269          * to not preallocating memory for all NR_CPUS
270          * when we use CPU hotplug.
271          */
272         acpi_register_lapic(processor->id,      /* APIC ID */
273                             processor->lapic_flags & ACPI_MADT_ENABLED);
274
275         return 0;
276 }
277
278 static int __init
279 acpi_parse_sapic(struct acpi_subtable_header *header, const unsigned long end)
280 {
281         struct acpi_madt_local_sapic *processor = NULL;
282
283         processor = (struct acpi_madt_local_sapic *)header;
284
285         if (BAD_MADT_ENTRY(processor, end))
286                 return -EINVAL;
287
288         acpi_table_print_madt_entry(header);
289
290         acpi_register_lapic((processor->id << 8) | processor->eid,/* APIC ID */
291                             processor->lapic_flags & ACPI_MADT_ENABLED);
292
293         return 0;
294 }
295
296 static int __init
297 acpi_parse_lapic_addr_ovr(struct acpi_subtable_header * header,
298                           const unsigned long end)
299 {
300         struct acpi_madt_local_apic_override *lapic_addr_ovr = NULL;
301
302         lapic_addr_ovr = (struct acpi_madt_local_apic_override *)header;
303
304         if (BAD_MADT_ENTRY(lapic_addr_ovr, end))
305                 return -EINVAL;
306
307         acpi_lapic_addr = lapic_addr_ovr->address;
308
309         return 0;
310 }
311
312 static int __init
313 acpi_parse_lapic_nmi(struct acpi_subtable_header * header, const unsigned long end)
314 {
315         struct acpi_madt_local_apic_nmi *lapic_nmi = NULL;
316
317         lapic_nmi = (struct acpi_madt_local_apic_nmi *)header;
318
319         if (BAD_MADT_ENTRY(lapic_nmi, end))
320                 return -EINVAL;
321
322         acpi_table_print_madt_entry(header);
323
324         if (lapic_nmi->lint != 1)
325                 printk(KERN_WARNING PREFIX "NMI not connected to LINT 1!\n");
326
327         return 0;
328 }
329
330 #endif                          /*CONFIG_X86_LOCAL_APIC */
331
332 #ifdef CONFIG_X86_IO_APIC
333
334 struct mp_ioapic_routing mp_ioapic_routing[MAX_IO_APICS];
335
336 static int __init
337 acpi_parse_ioapic(struct acpi_subtable_header * header, const unsigned long end)
338 {
339         struct acpi_madt_io_apic *ioapic = NULL;
340
341         ioapic = (struct acpi_madt_io_apic *)header;
342
343         if (BAD_MADT_ENTRY(ioapic, end))
344                 return -EINVAL;
345
346         acpi_table_print_madt_entry(header);
347
348         mp_register_ioapic(ioapic->id,
349                            ioapic->address, ioapic->global_irq_base);
350
351         return 0;
352 }
353
354 /*
355  * Parse Interrupt Source Override for the ACPI SCI
356  */
357 static void __init acpi_sci_ioapic_setup(u32 gsi, u16 polarity, u16 trigger)
358 {
359         if (trigger == 0)       /* compatible SCI trigger is level */
360                 trigger = 3;
361
362         if (polarity == 0)      /* compatible SCI polarity is low */
363                 polarity = 3;
364
365         /* Command-line over-ride via acpi_sci= */
366         if (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)
367                 trigger = (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2;
368
369         if (acpi_sci_flags & ACPI_MADT_POLARITY_MASK)
370                 polarity = acpi_sci_flags & ACPI_MADT_POLARITY_MASK;
371
372         /*
373          * mp_config_acpi_legacy_irqs() already setup IRQs < 16
374          * If GSI is < 16, this will update its flags,
375          * else it will create a new mp_irqs[] entry.
376          */
377         mp_override_legacy_irq(gsi, polarity, trigger, gsi);
378
379         /*
380          * stash over-ride to indicate we've been here
381          * and for later update of acpi_gbl_FADT
382          */
383         acpi_sci_override_gsi = gsi;
384         return;
385 }
386
387 static int __init
388 acpi_parse_int_src_ovr(struct acpi_subtable_header * header,
389                        const unsigned long end)
390 {
391         struct acpi_madt_interrupt_override *intsrc = NULL;
392
393         intsrc = (struct acpi_madt_interrupt_override *)header;
394
395         if (BAD_MADT_ENTRY(intsrc, end))
396                 return -EINVAL;
397
398         acpi_table_print_madt_entry(header);
399
400         if (intsrc->source_irq == acpi_gbl_FADT.sci_interrupt) {
401                 acpi_sci_ioapic_setup(intsrc->global_irq,
402                                       intsrc->inti_flags & ACPI_MADT_POLARITY_MASK,
403                                       (intsrc->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
404                 return 0;
405         }
406
407         if (acpi_skip_timer_override &&
408             intsrc->source_irq == 0 && intsrc->global_irq == 2) {
409                 printk(PREFIX "BIOS IRQ0 pin2 override ignored.\n");
410                 return 0;
411         }
412
413         mp_override_legacy_irq(intsrc->source_irq,
414                                 intsrc->inti_flags & ACPI_MADT_POLARITY_MASK,
415                                 (intsrc->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2,
416                                 intsrc->global_irq);
417
418         return 0;
419 }
420
421 static int __init
422 acpi_parse_nmi_src(struct acpi_subtable_header * header, const unsigned long end)
423 {
424         struct acpi_madt_nmi_source *nmi_src = NULL;
425
426         nmi_src = (struct acpi_madt_nmi_source *)header;
427
428         if (BAD_MADT_ENTRY(nmi_src, end))
429                 return -EINVAL;
430
431         acpi_table_print_madt_entry(header);
432
433         /* TBD: Support nimsrc entries? */
434
435         return 0;
436 }
437
438 #endif                          /* CONFIG_X86_IO_APIC */
439
440 /*
441  * acpi_pic_sci_set_trigger()
442  *
443  * use ELCR to set PIC-mode trigger type for SCI
444  *
445  * If a PIC-mode SCI is not recognized or gives spurious IRQ7's
446  * it may require Edge Trigger -- use "acpi_sci=edge"
447  *
448  * Port 0x4d0-4d1 are ECLR1 and ECLR2, the Edge/Level Control Registers
449  * for the 8259 PIC.  bit[n] = 1 means irq[n] is Level, otherwise Edge.
450  * ECLR1 is IRQs 0-7 (IRQ 0, 1, 2 must be 0)
451  * ECLR2 is IRQs 8-15 (IRQ 8, 13 must be 0)
452  */
453
454 void __init acpi_pic_sci_set_trigger(unsigned int irq, u16 trigger)
455 {
456         unsigned int mask = 1 << irq;
457         unsigned int old, new;
458
459         /* Real old ELCR mask */
460         old = inb(0x4d0) | (inb(0x4d1) << 8);
461
462         /*
463          * If we use ACPI to set PCI IRQs, then we should clear ELCR
464          * since we will set it correctly as we enable the PCI irq
465          * routing.
466          */
467         new = acpi_noirq ? old : 0;
468
469         /*
470          * Update SCI information in the ELCR, it isn't in the PCI
471          * routing tables..
472          */
473         switch (trigger) {
474         case 1:         /* Edge - clear */
475                 new &= ~mask;
476                 break;
477         case 3:         /* Level - set */
478                 new |= mask;
479                 break;
480         }
481
482         if (old == new)
483                 return;
484
485         printk(PREFIX "setting ELCR to %04x (from %04x)\n", new, old);
486         outb(new, 0x4d0);
487         outb(new >> 8, 0x4d1);
488 }
489
490 int acpi_gsi_to_irq(u32 gsi, unsigned int *irq)
491 {
492         *irq = gsi;
493         return 0;
494 }
495
496 /*
497  * success: return IRQ number (>=0)
498  * failure: return < 0
499  */
500 int acpi_register_gsi(u32 gsi, int triggering, int polarity)
501 {
502         unsigned int irq;
503         unsigned int plat_gsi = gsi;
504
505 #ifdef CONFIG_PCI
506         /*
507          * Make sure all (legacy) PCI IRQs are set as level-triggered.
508          */
509         if (acpi_irq_model == ACPI_IRQ_MODEL_PIC) {
510                 extern void eisa_set_level_irq(unsigned int irq);
511
512                 if (triggering == ACPI_LEVEL_SENSITIVE)
513                         eisa_set_level_irq(gsi);
514         }
515 #endif
516
517 #ifdef CONFIG_X86_IO_APIC
518         if (acpi_irq_model == ACPI_IRQ_MODEL_IOAPIC) {
519                 plat_gsi = mp_register_gsi(gsi, triggering, polarity);
520         }
521 #endif
522         acpi_gsi_to_irq(plat_gsi, &irq);
523         return irq;
524 }
525
526 /*
527  *  ACPI based hotplug support for CPU
528  */
529 #ifdef CONFIG_ACPI_HOTPLUG_CPU
530
531 static int __cpuinit _acpi_map_lsapic(acpi_handle handle, int *pcpu)
532 {
533         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
534         union acpi_object *obj;
535         struct acpi_madt_local_apic *lapic;
536         cpumask_t tmp_map, new_map;
537         u8 physid;
538         int cpu;
539
540         if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
541                 return -EINVAL;
542
543         if (!buffer.length || !buffer.pointer)
544                 return -EINVAL;
545
546         obj = buffer.pointer;
547         if (obj->type != ACPI_TYPE_BUFFER ||
548             obj->buffer.length < sizeof(*lapic)) {
549                 kfree(buffer.pointer);
550                 return -EINVAL;
551         }
552
553         lapic = (struct acpi_madt_local_apic *)obj->buffer.pointer;
554
555         if (lapic->header.type != ACPI_MADT_TYPE_LOCAL_APIC ||
556             !(lapic->lapic_flags & ACPI_MADT_ENABLED)) {
557                 kfree(buffer.pointer);
558                 return -EINVAL;
559         }
560
561         physid = lapic->id;
562
563         kfree(buffer.pointer);
564         buffer.length = ACPI_ALLOCATE_BUFFER;
565         buffer.pointer = NULL;
566
567         tmp_map = cpu_present_map;
568         acpi_register_lapic(physid, lapic->lapic_flags & ACPI_MADT_ENABLED);
569
570         /*
571          * If mp_register_lapic successfully generates a new logical cpu
572          * number, then the following will get us exactly what was mapped
573          */
574         cpus_andnot(new_map, cpu_present_map, tmp_map);
575         if (cpus_empty(new_map)) {
576                 printk ("Unable to map lapic to logical cpu number\n");
577                 return -EINVAL;
578         }
579
580         cpu = first_cpu(new_map);
581
582         *pcpu = cpu;
583         return 0;
584 }
585
586 /* wrapper to silence section mismatch warning */
587 int __ref acpi_map_lsapic(acpi_handle handle, int *pcpu)
588 {
589         return _acpi_map_lsapic(handle, pcpu);
590 }
591 EXPORT_SYMBOL(acpi_map_lsapic);
592
593 int acpi_unmap_lsapic(int cpu)
594 {
595         per_cpu(x86_cpu_to_apicid, cpu) = -1;
596         cpu_clear(cpu, cpu_present_map);
597         num_processors--;
598
599         return (0);
600 }
601
602 EXPORT_SYMBOL(acpi_unmap_lsapic);
603 #endif                          /* CONFIG_ACPI_HOTPLUG_CPU */
604
605 int acpi_register_ioapic(acpi_handle handle, u64 phys_addr, u32 gsi_base)
606 {
607         /* TBD */
608         return -EINVAL;
609 }
610
611 EXPORT_SYMBOL(acpi_register_ioapic);
612
613 int acpi_unregister_ioapic(acpi_handle handle, u32 gsi_base)
614 {
615         /* TBD */
616         return -EINVAL;
617 }
618
619 EXPORT_SYMBOL(acpi_unregister_ioapic);
620
621 static int __init acpi_parse_sbf(struct acpi_table_header *table)
622 {
623         struct acpi_table_boot *sb;
624
625         sb = (struct acpi_table_boot *)table;
626         if (!sb) {
627                 printk(KERN_WARNING PREFIX "Unable to map SBF\n");
628                 return -ENODEV;
629         }
630
631         sbf_port = sb->cmos_index;      /* Save CMOS port */
632
633         return 0;
634 }
635
636 #ifdef CONFIG_HPET_TIMER
637 #include <asm/hpet.h>
638
639 static struct __initdata resource *hpet_res;
640
641 static int __init acpi_parse_hpet(struct acpi_table_header *table)
642 {
643         struct acpi_table_hpet *hpet_tbl;
644
645         hpet_tbl = (struct acpi_table_hpet *)table;
646         if (!hpet_tbl) {
647                 printk(KERN_WARNING PREFIX "Unable to map HPET\n");
648                 return -ENODEV;
649         }
650
651         if (hpet_tbl->address.space_id != ACPI_SPACE_MEM) {
652                 printk(KERN_WARNING PREFIX "HPET timers must be located in "
653                        "memory.\n");
654                 return -1;
655         }
656
657         hpet_address = hpet_tbl->address.address;
658
659         /*
660          * Some broken BIOSes advertise HPET at 0x0. We really do not
661          * want to allocate a resource there.
662          */
663         if (!hpet_address) {
664                 printk(KERN_WARNING PREFIX
665                        "HPET id: %#x base: %#lx is invalid\n",
666                        hpet_tbl->id, hpet_address);
667                 return 0;
668         }
669 #ifdef CONFIG_X86_64
670         /*
671          * Some even more broken BIOSes advertise HPET at
672          * 0xfed0000000000000 instead of 0xfed00000. Fix it up and add
673          * some noise:
674          */
675         if (hpet_address == 0xfed0000000000000UL) {
676                 if (!hpet_force_user) {
677                         printk(KERN_WARNING PREFIX "HPET id: %#x "
678                                "base: 0xfed0000000000000 is bogus\n "
679                                "try hpet=force on the kernel command line to "
680                                "fix it up to 0xfed00000.\n", hpet_tbl->id);
681                         hpet_address = 0;
682                         return 0;
683                 }
684                 printk(KERN_WARNING PREFIX
685                        "HPET id: %#x base: 0xfed0000000000000 fixed up "
686                        "to 0xfed00000.\n", hpet_tbl->id);
687                 hpet_address >>= 32;
688         }
689 #endif
690         printk(KERN_INFO PREFIX "HPET id: %#x base: %#lx\n",
691                hpet_tbl->id, hpet_address);
692
693         /*
694          * Allocate and initialize the HPET firmware resource for adding into
695          * the resource tree during the lateinit timeframe.
696          */
697 #define HPET_RESOURCE_NAME_SIZE 9
698         hpet_res = alloc_bootmem(sizeof(*hpet_res) + HPET_RESOURCE_NAME_SIZE);
699
700         if (!hpet_res)
701                 return 0;
702
703         memset(hpet_res, 0, sizeof(*hpet_res));
704         hpet_res->name = (void *)&hpet_res[1];
705         hpet_res->flags = IORESOURCE_MEM;
706         snprintf((char *)hpet_res->name, HPET_RESOURCE_NAME_SIZE, "HPET %u",
707                  hpet_tbl->sequence);
708
709         hpet_res->start = hpet_address;
710         hpet_res->end = hpet_address + (1 * 1024) - 1;
711
712         return 0;
713 }
714
715 /*
716  * hpet_insert_resource inserts the HPET resources used into the resource
717  * tree.
718  */
719 static __init int hpet_insert_resource(void)
720 {
721         if (!hpet_res)
722                 return 1;
723
724         return insert_resource(&iomem_resource, hpet_res);
725 }
726
727 late_initcall(hpet_insert_resource);
728
729 #else
730 #define acpi_parse_hpet NULL
731 #endif
732
733 static int __init acpi_parse_fadt(struct acpi_table_header *table)
734 {
735
736 #ifdef CONFIG_X86_PM_TIMER
737         /* detect the location of the ACPI PM Timer */
738         if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID) {
739                 /* FADT rev. 2 */
740                 if (acpi_gbl_FADT.xpm_timer_block.space_id !=
741                     ACPI_ADR_SPACE_SYSTEM_IO)
742                         return 0;
743
744                 pmtmr_ioport = acpi_gbl_FADT.xpm_timer_block.address;
745                 /*
746                  * "X" fields are optional extensions to the original V1.0
747                  * fields, so we must selectively expand V1.0 fields if the
748                  * corresponding X field is zero.
749                  */
750                 if (!pmtmr_ioport)
751                         pmtmr_ioport = acpi_gbl_FADT.pm_timer_block;
752         } else {
753                 /* FADT rev. 1 */
754                 pmtmr_ioport = acpi_gbl_FADT.pm_timer_block;
755         }
756         if (pmtmr_ioport)
757                 printk(KERN_INFO PREFIX "PM-Timer IO Port: %#x\n",
758                        pmtmr_ioport);
759 #endif
760         return 0;
761 }
762
763 #ifdef  CONFIG_X86_LOCAL_APIC
764 /*
765  * Parse LAPIC entries in MADT
766  * returns 0 on success, < 0 on error
767  */
768
769 static void __init acpi_register_lapic_address(unsigned long address)
770 {
771         mp_lapic_addr = address;
772
773         set_fixmap_nocache(FIX_APIC_BASE, address);
774         if (boot_cpu_physical_apicid == -1U)
775                 boot_cpu_physical_apicid  = GET_APIC_ID(read_apic_id());
776 }
777
778 static int __init acpi_parse_madt_lapic_entries(void)
779 {
780         int count;
781
782         if (!cpu_has_apic)
783                 return -ENODEV;
784
785         /*
786          * Note that the LAPIC address is obtained from the MADT (32-bit value)
787          * and (optionally) overriden by a LAPIC_ADDR_OVR entry (64-bit value).
788          */
789
790         count =
791             acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE,
792                                   acpi_parse_lapic_addr_ovr, 0);
793         if (count < 0) {
794                 printk(KERN_ERR PREFIX
795                        "Error parsing LAPIC address override entry\n");
796                 return count;
797         }
798
799         acpi_register_lapic_address(acpi_lapic_addr);
800
801         count = acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_SAPIC,
802                                       acpi_parse_sapic, MAX_APICS);
803
804         if (!count)
805                 count = acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_APIC,
806                                               acpi_parse_lapic, MAX_APICS);
807         if (!count) {
808                 printk(KERN_ERR PREFIX "No LAPIC entries present\n");
809                 /* TBD: Cleanup to allow fallback to MPS */
810                 return -ENODEV;
811         } else if (count < 0) {
812                 printk(KERN_ERR PREFIX "Error parsing LAPIC entry\n");
813                 /* TBD: Cleanup to allow fallback to MPS */
814                 return count;
815         }
816
817         count =
818             acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_APIC_NMI, acpi_parse_lapic_nmi, 0);
819         if (count < 0) {
820                 printk(KERN_ERR PREFIX "Error parsing LAPIC NMI entry\n");
821                 /* TBD: Cleanup to allow fallback to MPS */
822                 return count;
823         }
824         return 0;
825 }
826 #endif                          /* CONFIG_X86_LOCAL_APIC */
827
828 #ifdef  CONFIG_X86_IO_APIC
829 /*
830  * Parse IOAPIC related entries in MADT
831  * returns 0 on success, < 0 on error
832  */
833 static int __init acpi_parse_madt_ioapic_entries(void)
834 {
835         int count;
836
837         /*
838          * ACPI interpreter is required to complete interrupt setup,
839          * so if it is off, don't enumerate the io-apics with ACPI.
840          * If MPS is present, it will handle them,
841          * otherwise the system will stay in PIC mode
842          */
843         if (acpi_disabled || acpi_noirq) {
844                 return -ENODEV;
845         }
846
847         if (!cpu_has_apic)
848                 return -ENODEV;
849
850         /*
851          * if "noapic" boot option, don't look for IO-APICs
852          */
853         if (skip_ioapic_setup) {
854                 printk(KERN_INFO PREFIX "Skipping IOAPIC probe "
855                        "due to 'noapic' option.\n");
856                 return -ENODEV;
857         }
858
859         count =
860             acpi_table_parse_madt(ACPI_MADT_TYPE_IO_APIC, acpi_parse_ioapic,
861                                   MAX_IO_APICS);
862         if (!count) {
863                 printk(KERN_ERR PREFIX "No IOAPIC entries present\n");
864                 return -ENODEV;
865         } else if (count < 0) {
866                 printk(KERN_ERR PREFIX "Error parsing IOAPIC entry\n");
867                 return count;
868         }
869
870         count =
871             acpi_table_parse_madt(ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, acpi_parse_int_src_ovr,
872                                   NR_IRQ_VECTORS);
873         if (count < 0) {
874                 printk(KERN_ERR PREFIX
875                        "Error parsing interrupt source overrides entry\n");
876                 /* TBD: Cleanup to allow fallback to MPS */
877                 return count;
878         }
879
880         /*
881          * If BIOS did not supply an INT_SRC_OVR for the SCI
882          * pretend we got one so we can set the SCI flags.
883          */
884         if (!acpi_sci_override_gsi)
885                 acpi_sci_ioapic_setup(acpi_gbl_FADT.sci_interrupt, 0, 0);
886
887         /* Fill in identity legacy mapings where no override */
888         mp_config_acpi_legacy_irqs();
889
890         count =
891             acpi_table_parse_madt(ACPI_MADT_TYPE_NMI_SOURCE, acpi_parse_nmi_src,
892                                   NR_IRQ_VECTORS);
893         if (count < 0) {
894                 printk(KERN_ERR PREFIX "Error parsing NMI SRC entry\n");
895                 /* TBD: Cleanup to allow fallback to MPS */
896                 return count;
897         }
898
899         return 0;
900 }
901 #else
902 static inline int acpi_parse_madt_ioapic_entries(void)
903 {
904         return -1;
905 }
906 #endif  /* !CONFIG_X86_IO_APIC */
907
908 static void __init acpi_process_madt(void)
909 {
910 #ifdef CONFIG_X86_LOCAL_APIC
911         int error;
912
913         if (!acpi_table_parse(ACPI_SIG_MADT, acpi_parse_madt)) {
914
915                 /*
916                  * Parse MADT LAPIC entries
917                  */
918                 error = acpi_parse_madt_lapic_entries();
919                 if (!error) {
920                         acpi_lapic = 1;
921
922 #ifdef CONFIG_X86_GENERICARCH
923                         generic_bigsmp_probe();
924 #endif
925                         /*
926                          * Parse MADT IO-APIC entries
927                          */
928                         error = acpi_parse_madt_ioapic_entries();
929                         if (!error) {
930                                 acpi_irq_model = ACPI_IRQ_MODEL_IOAPIC;
931                                 acpi_irq_balance_set(NULL);
932                                 acpi_ioapic = 1;
933
934                                 smp_found_config = 1;
935                                 setup_apic_routing();
936                         }
937                 }
938                 if (error == -EINVAL) {
939                         /*
940                          * Dell Precision Workstation 410, 610 come here.
941                          */
942                         printk(KERN_ERR PREFIX
943                                "Invalid BIOS MADT, disabling ACPI\n");
944                         disable_acpi();
945                 }
946         }
947 #endif
948         return;
949 }
950
951 #ifdef __i386__
952
953 static int __init disable_acpi_irq(const struct dmi_system_id *d)
954 {
955         if (!acpi_force) {
956                 printk(KERN_NOTICE "%s detected: force use of acpi=noirq\n",
957                        d->ident);
958                 acpi_noirq_set();
959         }
960         return 0;
961 }
962
963 static int __init disable_acpi_pci(const struct dmi_system_id *d)
964 {
965         if (!acpi_force) {
966                 printk(KERN_NOTICE "%s detected: force use of pci=noacpi\n",
967                        d->ident);
968                 acpi_disable_pci();
969         }
970         return 0;
971 }
972
973 static int __init dmi_disable_acpi(const struct dmi_system_id *d)
974 {
975         if (!acpi_force) {
976                 printk(KERN_NOTICE "%s detected: acpi off\n", d->ident);
977                 disable_acpi();
978         } else {
979                 printk(KERN_NOTICE
980                        "Warning: DMI blacklist says broken, but acpi forced\n");
981         }
982         return 0;
983 }
984
985 /*
986  * Limit ACPI to CPU enumeration for HT
987  */
988 static int __init force_acpi_ht(const struct dmi_system_id *d)
989 {
990         if (!acpi_force) {
991                 printk(KERN_NOTICE "%s detected: force use of acpi=ht\n",
992                        d->ident);
993                 disable_acpi();
994                 acpi_ht = 1;
995         } else {
996                 printk(KERN_NOTICE
997                        "Warning: acpi=force overrules DMI blacklist: acpi=ht\n");
998         }
999         return 0;
1000 }
1001
1002 /*
1003  * If your system is blacklisted here, but you find that acpi=force
1004  * works for you, please contact acpi-devel@sourceforge.net
1005  */
1006 static struct dmi_system_id __initdata acpi_dmi_table[] = {
1007         /*
1008          * Boxes that need ACPI disabled
1009          */
1010         {
1011          .callback = dmi_disable_acpi,
1012          .ident = "IBM Thinkpad",
1013          .matches = {
1014                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1015                      DMI_MATCH(DMI_BOARD_NAME, "2629H1G"),
1016                      },
1017          },
1018
1019         /*
1020          * Boxes that need acpi=ht
1021          */
1022         {
1023          .callback = force_acpi_ht,
1024          .ident = "FSC Primergy T850",
1025          .matches = {
1026                      DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
1027                      DMI_MATCH(DMI_PRODUCT_NAME, "PRIMERGY T850"),
1028                      },
1029          },
1030         {
1031          .callback = force_acpi_ht,
1032          .ident = "HP VISUALIZE NT Workstation",
1033          .matches = {
1034                      DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
1035                      DMI_MATCH(DMI_PRODUCT_NAME, "HP VISUALIZE NT Workstation"),
1036                      },
1037          },
1038         {
1039          .callback = force_acpi_ht,
1040          .ident = "Compaq Workstation W8000",
1041          .matches = {
1042                      DMI_MATCH(DMI_SYS_VENDOR, "Compaq"),
1043                      DMI_MATCH(DMI_PRODUCT_NAME, "Workstation W8000"),
1044                      },
1045          },
1046         {
1047          .callback = force_acpi_ht,
1048          .ident = "ASUS P4B266",
1049          .matches = {
1050                      DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1051                      DMI_MATCH(DMI_BOARD_NAME, "P4B266"),
1052                      },
1053          },
1054         {
1055          .callback = force_acpi_ht,
1056          .ident = "ASUS P2B-DS",
1057          .matches = {
1058                      DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1059                      DMI_MATCH(DMI_BOARD_NAME, "P2B-DS"),
1060                      },
1061          },
1062         {
1063          .callback = force_acpi_ht,
1064          .ident = "ASUS CUR-DLS",
1065          .matches = {
1066                      DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1067                      DMI_MATCH(DMI_BOARD_NAME, "CUR-DLS"),
1068                      },
1069          },
1070         {
1071          .callback = force_acpi_ht,
1072          .ident = "ABIT i440BX-W83977",
1073          .matches = {
1074                      DMI_MATCH(DMI_BOARD_VENDOR, "ABIT <http://www.abit.com>"),
1075                      DMI_MATCH(DMI_BOARD_NAME, "i440BX-W83977 (BP6)"),
1076                      },
1077          },
1078         {
1079          .callback = force_acpi_ht,
1080          .ident = "IBM Bladecenter",
1081          .matches = {
1082                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1083                      DMI_MATCH(DMI_BOARD_NAME, "IBM eServer BladeCenter HS20"),
1084                      },
1085          },
1086         {
1087          .callback = force_acpi_ht,
1088          .ident = "IBM eServer xSeries 360",
1089          .matches = {
1090                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1091                      DMI_MATCH(DMI_BOARD_NAME, "eServer xSeries 360"),
1092                      },
1093          },
1094         {
1095          .callback = force_acpi_ht,
1096          .ident = "IBM eserver xSeries 330",
1097          .matches = {
1098                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1099                      DMI_MATCH(DMI_BOARD_NAME, "eserver xSeries 330"),
1100                      },
1101          },
1102         {
1103          .callback = force_acpi_ht,
1104          .ident = "IBM eserver xSeries 440",
1105          .matches = {
1106                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1107                      DMI_MATCH(DMI_PRODUCT_NAME, "eserver xSeries 440"),
1108                      },
1109          },
1110
1111         /*
1112          * Boxes that need ACPI PCI IRQ routing disabled
1113          */
1114         {
1115          .callback = disable_acpi_irq,
1116          .ident = "ASUS A7V",
1117          .matches = {
1118                      DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC"),
1119                      DMI_MATCH(DMI_BOARD_NAME, "<A7V>"),
1120                      /* newer BIOS, Revision 1011, does work */
1121                      DMI_MATCH(DMI_BIOS_VERSION,
1122                                "ASUS A7V ACPI BIOS Revision 1007"),
1123                      },
1124          },
1125         {
1126                 /*
1127                  * Latest BIOS for IBM 600E (1.16) has bad pcinum
1128                  * for LPC bridge, which is needed for the PCI
1129                  * interrupt links to work. DSDT fix is in bug 5966.
1130                  * 2645, 2646 model numbers are shared with 600/600E/600X
1131                  */
1132          .callback = disable_acpi_irq,
1133          .ident = "IBM Thinkpad 600 Series 2645",
1134          .matches = {
1135                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1136                      DMI_MATCH(DMI_BOARD_NAME, "2645"),
1137                      },
1138          },
1139         {
1140          .callback = disable_acpi_irq,
1141          .ident = "IBM Thinkpad 600 Series 2646",
1142          .matches = {
1143                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
1144                      DMI_MATCH(DMI_BOARD_NAME, "2646"),
1145                      },
1146          },
1147         /*
1148          * Boxes that need ACPI PCI IRQ routing and PCI scan disabled
1149          */
1150         {                       /* _BBN 0 bug */
1151          .callback = disable_acpi_pci,
1152          .ident = "ASUS PR-DLS",
1153          .matches = {
1154                      DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1155                      DMI_MATCH(DMI_BOARD_NAME, "PR-DLS"),
1156                      DMI_MATCH(DMI_BIOS_VERSION,
1157                                "ASUS PR-DLS ACPI BIOS Revision 1010"),
1158                      DMI_MATCH(DMI_BIOS_DATE, "03/21/2003")
1159                      },
1160          },
1161         {
1162          .callback = disable_acpi_pci,
1163          .ident = "Acer TravelMate 36x Laptop",
1164          .matches = {
1165                      DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1166                      DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 360"),
1167                      },
1168          },
1169         {}
1170 };
1171
1172 #endif                          /* __i386__ */
1173
1174 /*
1175  * acpi_boot_table_init() and acpi_boot_init()
1176  *  called from setup_arch(), always.
1177  *      1. checksums all tables
1178  *      2. enumerates lapics
1179  *      3. enumerates io-apics
1180  *
1181  * acpi_table_init() is separate to allow reading SRAT without
1182  * other side effects.
1183  *
1184  * side effects of acpi_boot_init:
1185  *      acpi_lapic = 1 if LAPIC found
1186  *      acpi_ioapic = 1 if IOAPIC found
1187  *      if (acpi_lapic && acpi_ioapic) smp_found_config = 1;
1188  *      if acpi_blacklisted() acpi_disabled = 1;
1189  *      acpi_irq_model=...
1190  *      ...
1191  *
1192  * return value: (currently ignored)
1193  *      0: success
1194  *      !0: failure
1195  */
1196
1197 int __init acpi_boot_table_init(void)
1198 {
1199         int error;
1200
1201 #ifdef __i386__
1202         dmi_check_system(acpi_dmi_table);
1203 #endif
1204
1205         /*
1206          * If acpi_disabled, bail out
1207          * One exception: acpi=ht continues far enough to enumerate LAPICs
1208          */
1209         if (acpi_disabled && !acpi_ht)
1210                 return 1;
1211
1212         /*
1213          * Initialize the ACPI boot-time table parser.
1214          */
1215         error = acpi_table_init();
1216         if (error) {
1217                 disable_acpi();
1218                 return error;
1219         }
1220
1221         acpi_table_parse(ACPI_SIG_BOOT, acpi_parse_sbf);
1222
1223         /*
1224          * blacklist may disable ACPI entirely
1225          */
1226         error = acpi_blacklisted();
1227         if (error) {
1228                 if (acpi_force) {
1229                         printk(KERN_WARNING PREFIX "acpi=force override\n");
1230                 } else {
1231                         printk(KERN_WARNING PREFIX "Disabling ACPI support\n");
1232                         disable_acpi();
1233                         return error;
1234                 }
1235         }
1236
1237         return 0;
1238 }
1239
1240 int __init acpi_boot_init(void)
1241 {
1242         /*
1243          * If acpi_disabled, bail out
1244          * One exception: acpi=ht continues far enough to enumerate LAPICs
1245          */
1246         if (acpi_disabled && !acpi_ht)
1247                 return 1;
1248
1249         acpi_table_parse(ACPI_SIG_BOOT, acpi_parse_sbf);
1250
1251         /*
1252          * set sci_int and PM timer address
1253          */
1254         acpi_table_parse(ACPI_SIG_FADT, acpi_parse_fadt);
1255
1256         /*
1257          * Process the Multiple APIC Description Table (MADT), if present
1258          */
1259         acpi_process_madt();
1260
1261         acpi_table_parse(ACPI_SIG_HPET, acpi_parse_hpet);
1262
1263         return 0;
1264 }
1265
1266 static int __init parse_acpi(char *arg)
1267 {
1268         if (!arg)
1269                 return -EINVAL;
1270
1271         /* "acpi=off" disables both ACPI table parsing and interpreter */
1272         if (strcmp(arg, "off") == 0) {
1273                 disable_acpi();
1274         }
1275         /* acpi=force to over-ride black-list */
1276         else if (strcmp(arg, "force") == 0) {
1277                 acpi_force = 1;
1278                 acpi_ht = 1;
1279                 acpi_disabled = 0;
1280         }
1281         /* acpi=strict disables out-of-spec workarounds */
1282         else if (strcmp(arg, "strict") == 0) {
1283                 acpi_strict = 1;
1284         }
1285         /* Limit ACPI just to boot-time to enable HT */
1286         else if (strcmp(arg, "ht") == 0) {
1287                 if (!acpi_force)
1288                         disable_acpi();
1289                 acpi_ht = 1;
1290         }
1291         /* "acpi=noirq" disables ACPI interrupt routing */
1292         else if (strcmp(arg, "noirq") == 0) {
1293                 acpi_noirq_set();
1294         } else {
1295                 /* Core will printk when we return error. */
1296                 return -EINVAL;
1297         }
1298         return 0;
1299 }
1300 early_param("acpi", parse_acpi);
1301
1302 /* FIXME: Using pci= for an ACPI parameter is a travesty. */
1303 static int __init parse_pci(char *arg)
1304 {
1305         if (arg && strcmp(arg, "noacpi") == 0)
1306                 acpi_disable_pci();
1307         return 0;
1308 }
1309 early_param("pci", parse_pci);
1310
1311 #ifdef CONFIG_X86_IO_APIC
1312 static int __init parse_acpi_skip_timer_override(char *arg)
1313 {
1314         acpi_skip_timer_override = 1;
1315         return 0;
1316 }
1317 early_param("acpi_skip_timer_override", parse_acpi_skip_timer_override);
1318
1319 static int __init parse_acpi_use_timer_override(char *arg)
1320 {
1321         acpi_use_timer_override = 1;
1322         return 0;
1323 }
1324 early_param("acpi_use_timer_override", parse_acpi_use_timer_override);
1325 #endif /* CONFIG_X86_IO_APIC */
1326
1327 static int __init setup_acpi_sci(char *s)
1328 {
1329         if (!s)
1330                 return -EINVAL;
1331         if (!strcmp(s, "edge"))
1332                 acpi_sci_flags =  ACPI_MADT_TRIGGER_EDGE |
1333                         (acpi_sci_flags & ~ACPI_MADT_TRIGGER_MASK);
1334         else if (!strcmp(s, "level"))
1335                 acpi_sci_flags = ACPI_MADT_TRIGGER_LEVEL |
1336                         (acpi_sci_flags & ~ACPI_MADT_TRIGGER_MASK);
1337         else if (!strcmp(s, "high"))
1338                 acpi_sci_flags = ACPI_MADT_POLARITY_ACTIVE_HIGH |
1339                         (acpi_sci_flags & ~ACPI_MADT_POLARITY_MASK);
1340         else if (!strcmp(s, "low"))
1341                 acpi_sci_flags = ACPI_MADT_POLARITY_ACTIVE_LOW |
1342                         (acpi_sci_flags & ~ACPI_MADT_POLARITY_MASK);
1343         else
1344                 return -EINVAL;
1345         return 0;
1346 }
1347 early_param("acpi_sci", setup_acpi_sci);
1348
1349 int __acpi_acquire_global_lock(unsigned int *lock)
1350 {
1351         unsigned int old, new, val;
1352         do {
1353                 old = *lock;
1354                 new = (((old & ~0x3) + 2) + ((old >> 1) & 0x1));
1355                 val = cmpxchg(lock, old, new);
1356         } while (unlikely (val != old));
1357         return (new < 3) ? -1 : 0;
1358 }
1359
1360 int __acpi_release_global_lock(unsigned int *lock)
1361 {
1362         unsigned int old, new, val;
1363         do {
1364                 old = *lock;
1365                 new = old & ~0x3;
1366                 val = cmpxchg(lock, old, new);
1367         } while (unlikely (val != old));
1368         return old & 0x1;
1369 }