c63d4cb37dabae4d1ce224065596866432c1c013
[pandora-kernel.git] / drivers / acpi / osl.c
1 /*
2  *  acpi_osl.c - OS-dependent functions ($Revision: 83 $)
3  *
4  *  Copyright (C) 2000       Andrew Henroid
5  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7  *  Copyright (c) 2008 Intel Corporation
8  *   Author: Matthew Wilcox <willy@linux.intel.com>
9  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  *
26  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27  *
28  */
29
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
33 #include <linux/mm.h>
34 #include <linux/pci.h>
35 #include <linux/interrupt.h>
36 #include <linux/kmod.h>
37 #include <linux/delay.h>
38 #include <linux/workqueue.h>
39 #include <linux/nmi.h>
40 #include <linux/acpi.h>
41 #include <linux/efi.h>
42 #include <linux/ioport.h>
43 #include <linux/list.h>
44 #include <linux/jiffies.h>
45 #include <linux/semaphore.h>
46
47 #include <asm/io.h>
48 #include <asm/uaccess.h>
49
50 #include <acpi/acpi.h>
51 #include <acpi/acpi_bus.h>
52 #include <acpi/processor.h>
53
54 #define _COMPONENT              ACPI_OS_SERVICES
55 ACPI_MODULE_NAME("osl");
56 #define PREFIX          "ACPI: "
57 struct acpi_os_dpc {
58         acpi_osd_exec_callback function;
59         void *context;
60         struct work_struct work;
61         int wait;
62 };
63
64 #ifdef CONFIG_ACPI_CUSTOM_DSDT
65 #include CONFIG_ACPI_CUSTOM_DSDT_FILE
66 #endif
67
68 #ifdef ENABLE_DEBUGGER
69 #include <linux/kdb.h>
70
71 /* stuff for debugger support */
72 int acpi_in_debugger;
73 EXPORT_SYMBOL(acpi_in_debugger);
74
75 extern char line_buf[80];
76 #endif                          /*ENABLE_DEBUGGER */
77
78 static unsigned int acpi_irq_irq;
79 static acpi_osd_handler acpi_irq_handler;
80 static void *acpi_irq_context;
81 static struct workqueue_struct *kacpid_wq;
82 static struct workqueue_struct *kacpi_notify_wq;
83 static struct workqueue_struct *kacpi_hotplug_wq;
84
85 struct acpi_res_list {
86         resource_size_t start;
87         resource_size_t end;
88         acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
89         char name[5];   /* only can have a length of 4 chars, make use of this
90                            one instead of res->name, no need to kalloc then */
91         struct list_head resource_list;
92         int count;
93 };
94
95 static LIST_HEAD(resource_list_head);
96 static DEFINE_SPINLOCK(acpi_res_lock);
97
98 /*
99  * This list of permanent mappings is for memory that may be accessed from
100  * interrupt context, where we can't do the ioremap().
101  */
102 struct acpi_ioremap {
103         struct list_head list;
104         void __iomem *virt;
105         acpi_physical_address phys;
106         acpi_size size;
107 };
108
109 static LIST_HEAD(acpi_ioremaps);
110 static DEFINE_SPINLOCK(acpi_ioremap_lock);
111
112 #define OSI_STRING_LENGTH_MAX 64        /* arbitrary */
113 static char osi_additional_string[OSI_STRING_LENGTH_MAX];
114
115 /*
116  * The story of _OSI(Linux)
117  *
118  * From pre-history through Linux-2.6.22,
119  * Linux responded TRUE upon a BIOS OSI(Linux) query.
120  *
121  * Unfortunately, reference BIOS writers got wind of this
122  * and put OSI(Linux) in their example code, quickly exposing
123  * this string as ill-conceived and opening the door to
124  * an un-bounded number of BIOS incompatibilities.
125  *
126  * For example, OSI(Linux) was used on resume to re-POST a
127  * video card on one system, because Linux at that time
128  * could not do a speedy restore in its native driver.
129  * But then upon gaining quick native restore capability,
130  * Linux has no way to tell the BIOS to skip the time-consuming
131  * POST -- putting Linux at a permanent performance disadvantage.
132  * On another system, the BIOS writer used OSI(Linux)
133  * to infer native OS support for IPMI!  On other systems,
134  * OSI(Linux) simply got in the way of Linux claiming to
135  * be compatible with other operating systems, exposing
136  * BIOS issues such as skipped device initialization.
137  *
138  * So "Linux" turned out to be a really poor chose of
139  * OSI string, and from Linux-2.6.23 onward we respond FALSE.
140  *
141  * BIOS writers should NOT query _OSI(Linux) on future systems.
142  * Linux will complain on the console when it sees it, and return FALSE.
143  * To get Linux to return TRUE for your system  will require
144  * a kernel source update to add a DMI entry,
145  * or boot with "acpi_osi=Linux"
146  */
147
148 static struct osi_linux {
149         unsigned int    enable:1;
150         unsigned int    dmi:1;
151         unsigned int    cmdline:1;
152         unsigned int    known:1;
153 } osi_linux = { 0, 0, 0, 0};
154
155 static void __init acpi_request_region (struct acpi_generic_address *addr,
156         unsigned int length, char *desc)
157 {
158         if (!addr->address || !length)
159                 return;
160
161         /* Resources are never freed */
162         if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
163                 request_region(addr->address, length, desc);
164         else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
165                 request_mem_region(addr->address, length, desc);
166 }
167
168 static int __init acpi_reserve_resources(void)
169 {
170         acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
171                 "ACPI PM1a_EVT_BLK");
172
173         acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
174                 "ACPI PM1b_EVT_BLK");
175
176         acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
177                 "ACPI PM1a_CNT_BLK");
178
179         acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
180                 "ACPI PM1b_CNT_BLK");
181
182         if (acpi_gbl_FADT.pm_timer_length == 4)
183                 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
184
185         acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
186                 "ACPI PM2_CNT_BLK");
187
188         /* Length of GPE blocks must be a non-negative multiple of 2 */
189
190         if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
191                 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
192                                acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
193
194         if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
195                 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
196                                acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
197
198         return 0;
199 }
200 device_initcall(acpi_reserve_resources);
201
202 void acpi_os_printf(const char *fmt, ...)
203 {
204         va_list args;
205         va_start(args, fmt);
206         acpi_os_vprintf(fmt, args);
207         va_end(args);
208 }
209
210 void acpi_os_vprintf(const char *fmt, va_list args)
211 {
212         static char buffer[512];
213
214         vsprintf(buffer, fmt, args);
215
216 #ifdef ENABLE_DEBUGGER
217         if (acpi_in_debugger) {
218                 kdb_printf("%s", buffer);
219         } else {
220                 printk(KERN_CONT "%s", buffer);
221         }
222 #else
223         printk(KERN_CONT "%s", buffer);
224 #endif
225 }
226
227 acpi_physical_address __init acpi_os_get_root_pointer(void)
228 {
229         if (efi_enabled) {
230                 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
231                         return efi.acpi20;
232                 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
233                         return efi.acpi;
234                 else {
235                         printk(KERN_ERR PREFIX
236                                "System description tables not found\n");
237                         return 0;
238                 }
239         } else {
240                 acpi_physical_address pa = 0;
241
242                 acpi_find_root_pointer(&pa);
243                 return pa;
244         }
245 }
246
247 /* Must be called with 'acpi_ioremap_lock' lock held. */
248 static void __iomem *
249 acpi_map_vaddr_lookup(acpi_physical_address phys, acpi_size size)
250 {
251         struct acpi_ioremap *map;
252
253         list_for_each_entry(map, &acpi_ioremaps, list)
254                 if (map->phys <= phys &&
255                     phys + size <= map->phys + map->size)
256                         return map->virt + (phys - map->phys);
257
258         return NULL;
259 }
260
261 /* Must be called with 'acpi_ioremap_lock' lock held. */
262 static struct acpi_ioremap *
263 acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
264 {
265         struct acpi_ioremap *map;
266
267         list_for_each_entry(map, &acpi_ioremaps, list)
268                 if (map->virt == virt && map->size == size)
269                         return map;
270
271         return NULL;
272 }
273
274 void __iomem *__init_refok
275 acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
276 {
277         struct acpi_ioremap *map;
278         unsigned long flags;
279         void __iomem *virt;
280
281         if (phys > ULONG_MAX) {
282                 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
283                 return NULL;
284         }
285
286         if (!acpi_gbl_permanent_mmap)
287                 return __acpi_map_table((unsigned long)phys, size);
288
289         map = kzalloc(sizeof(*map), GFP_KERNEL);
290         if (!map)
291                 return NULL;
292
293         virt = ioremap(phys, size);
294         if (!virt) {
295                 kfree(map);
296                 return NULL;
297         }
298
299         INIT_LIST_HEAD(&map->list);
300         map->virt = virt;
301         map->phys = phys;
302         map->size = size;
303
304         spin_lock_irqsave(&acpi_ioremap_lock, flags);
305         list_add_tail(&map->list, &acpi_ioremaps);
306         spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
307
308         return virt;
309 }
310 EXPORT_SYMBOL_GPL(acpi_os_map_memory);
311
312 void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
313 {
314         struct acpi_ioremap *map;
315         unsigned long flags;
316
317         if (!acpi_gbl_permanent_mmap) {
318                 __acpi_unmap_table(virt, size);
319                 return;
320         }
321
322         spin_lock_irqsave(&acpi_ioremap_lock, flags);
323         map = acpi_map_lookup_virt(virt, size);
324         if (!map) {
325                 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
326                 printk(KERN_ERR PREFIX "%s: bad address %p\n", __func__, virt);
327                 dump_stack();
328                 return;
329         }
330
331         list_del(&map->list);
332         spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
333
334         iounmap(map->virt);
335         kfree(map);
336 }
337 EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
338
339 void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
340 {
341         if (!acpi_gbl_permanent_mmap)
342                 __acpi_unmap_table(virt, size);
343 }
344
345 int acpi_os_map_generic_address(struct acpi_generic_address *addr)
346 {
347         void __iomem *virt;
348
349         if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
350                 return 0;
351
352         if (!addr->address || !addr->bit_width)
353                 return -EINVAL;
354
355         virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
356         if (!virt)
357                 return -EIO;
358
359         return 0;
360 }
361 EXPORT_SYMBOL_GPL(acpi_os_map_generic_address);
362
363 void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
364 {
365         void __iomem *virt;
366         unsigned long flags;
367         acpi_size size = addr->bit_width / 8;
368
369         if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
370                 return;
371
372         if (!addr->address || !addr->bit_width)
373                 return;
374
375         spin_lock_irqsave(&acpi_ioremap_lock, flags);
376         virt = acpi_map_vaddr_lookup(addr->address, size);
377         spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
378
379         acpi_os_unmap_memory(virt, size);
380 }
381 EXPORT_SYMBOL_GPL(acpi_os_unmap_generic_address);
382
383 #ifdef ACPI_FUTURE_USAGE
384 acpi_status
385 acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
386 {
387         if (!phys || !virt)
388                 return AE_BAD_PARAMETER;
389
390         *phys = virt_to_phys(virt);
391
392         return AE_OK;
393 }
394 #endif
395
396 #define ACPI_MAX_OVERRIDE_LEN 100
397
398 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
399
400 acpi_status
401 acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
402                             acpi_string * new_val)
403 {
404         if (!init_val || !new_val)
405                 return AE_BAD_PARAMETER;
406
407         *new_val = NULL;
408         if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
409                 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
410                        acpi_os_name);
411                 *new_val = acpi_os_name;
412         }
413
414         return AE_OK;
415 }
416
417 acpi_status
418 acpi_os_table_override(struct acpi_table_header * existing_table,
419                        struct acpi_table_header ** new_table)
420 {
421         if (!existing_table || !new_table)
422                 return AE_BAD_PARAMETER;
423
424         *new_table = NULL;
425
426 #ifdef CONFIG_ACPI_CUSTOM_DSDT
427         if (strncmp(existing_table->signature, "DSDT", 4) == 0)
428                 *new_table = (struct acpi_table_header *)AmlCode;
429 #endif
430         if (*new_table != NULL) {
431                 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
432                            "this is unsafe: tainting kernel\n",
433                        existing_table->signature,
434                        existing_table->oem_table_id);
435                 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
436         }
437         return AE_OK;
438 }
439
440 static irqreturn_t acpi_irq(int irq, void *dev_id)
441 {
442         u32 handled;
443
444         handled = (*acpi_irq_handler) (acpi_irq_context);
445
446         if (handled) {
447                 acpi_irq_handled++;
448                 return IRQ_HANDLED;
449         } else {
450                 acpi_irq_not_handled++;
451                 return IRQ_NONE;
452         }
453 }
454
455 acpi_status
456 acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
457                                   void *context)
458 {
459         unsigned int irq;
460
461         acpi_irq_stats_init();
462
463         /*
464          * Ignore the GSI from the core, and use the value in our copy of the
465          * FADT. It may not be the same if an interrupt source override exists
466          * for the SCI.
467          */
468         gsi = acpi_gbl_FADT.sci_interrupt;
469         if (acpi_gsi_to_irq(gsi, &irq) < 0) {
470                 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
471                        gsi);
472                 return AE_OK;
473         }
474
475         acpi_irq_handler = handler;
476         acpi_irq_context = context;
477         if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
478                 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
479                 return AE_NOT_ACQUIRED;
480         }
481         acpi_irq_irq = irq;
482
483         return AE_OK;
484 }
485
486 acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
487 {
488         if (irq) {
489                 free_irq(irq, acpi_irq);
490                 acpi_irq_handler = NULL;
491                 acpi_irq_irq = 0;
492         }
493
494         return AE_OK;
495 }
496
497 /*
498  * Running in interpreter thread context, safe to sleep
499  */
500
501 void acpi_os_sleep(u64 ms)
502 {
503         schedule_timeout_interruptible(msecs_to_jiffies(ms));
504 }
505
506 void acpi_os_stall(u32 us)
507 {
508         while (us) {
509                 u32 delay = 1000;
510
511                 if (delay > us)
512                         delay = us;
513                 udelay(delay);
514                 touch_nmi_watchdog();
515                 us -= delay;
516         }
517 }
518
519 /*
520  * Support ACPI 3.0 AML Timer operand
521  * Returns 64-bit free-running, monotonically increasing timer
522  * with 100ns granularity
523  */
524 u64 acpi_os_get_timer(void)
525 {
526         static u64 t;
527
528 #ifdef  CONFIG_HPET
529         /* TBD: use HPET if available */
530 #endif
531
532 #ifdef  CONFIG_X86_PM_TIMER
533         /* TBD: default to PM timer if HPET was not available */
534 #endif
535         if (!t)
536                 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
537
538         return ++t;
539 }
540
541 acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
542 {
543         u32 dummy;
544
545         if (!value)
546                 value = &dummy;
547
548         *value = 0;
549         if (width <= 8) {
550                 *(u8 *) value = inb(port);
551         } else if (width <= 16) {
552                 *(u16 *) value = inw(port);
553         } else if (width <= 32) {
554                 *(u32 *) value = inl(port);
555         } else {
556                 BUG();
557         }
558
559         return AE_OK;
560 }
561
562 EXPORT_SYMBOL(acpi_os_read_port);
563
564 acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
565 {
566         if (width <= 8) {
567                 outb(value, port);
568         } else if (width <= 16) {
569                 outw(value, port);
570         } else if (width <= 32) {
571                 outl(value, port);
572         } else {
573                 BUG();
574         }
575
576         return AE_OK;
577 }
578
579 EXPORT_SYMBOL(acpi_os_write_port);
580
581 acpi_status
582 acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
583 {
584         u32 dummy;
585         void __iomem *virt_addr;
586         int size = width / 8, unmap = 0;
587         unsigned long flags;
588
589         spin_lock_irqsave(&acpi_ioremap_lock, flags);
590         virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
591         spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
592         if (!virt_addr) {
593                 virt_addr = ioremap(phys_addr, size);
594                 unmap = 1;
595         }
596         if (!value)
597                 value = &dummy;
598
599         switch (width) {
600         case 8:
601                 *(u8 *) value = readb(virt_addr);
602                 break;
603         case 16:
604                 *(u16 *) value = readw(virt_addr);
605                 break;
606         case 32:
607                 *(u32 *) value = readl(virt_addr);
608                 break;
609         default:
610                 BUG();
611         }
612
613         if (unmap)
614                 iounmap(virt_addr);
615
616         return AE_OK;
617 }
618
619 acpi_status
620 acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
621 {
622         void __iomem *virt_addr;
623         int size = width / 8, unmap = 0;
624         unsigned long flags;
625
626         spin_lock_irqsave(&acpi_ioremap_lock, flags);
627         virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
628         spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
629         if (!virt_addr) {
630                 virt_addr = ioremap(phys_addr, size);
631                 unmap = 1;
632         }
633
634         switch (width) {
635         case 8:
636                 writeb(value, virt_addr);
637                 break;
638         case 16:
639                 writew(value, virt_addr);
640                 break;
641         case 32:
642                 writel(value, virt_addr);
643                 break;
644         default:
645                 BUG();
646         }
647
648         if (unmap)
649                 iounmap(virt_addr);
650
651         return AE_OK;
652 }
653
654 acpi_status
655 acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
656                                u32 *value, u32 width)
657 {
658         int result, size;
659
660         if (!value)
661                 return AE_BAD_PARAMETER;
662
663         switch (width) {
664         case 8:
665                 size = 1;
666                 break;
667         case 16:
668                 size = 2;
669                 break;
670         case 32:
671                 size = 4;
672                 break;
673         default:
674                 return AE_ERROR;
675         }
676
677         result = raw_pci_read(pci_id->segment, pci_id->bus,
678                                 PCI_DEVFN(pci_id->device, pci_id->function),
679                                 reg, size, value);
680
681         return (result ? AE_ERROR : AE_OK);
682 }
683
684 acpi_status
685 acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
686                                 u64 value, u32 width)
687 {
688         int result, size;
689
690         switch (width) {
691         case 8:
692                 size = 1;
693                 break;
694         case 16:
695                 size = 2;
696                 break;
697         case 32:
698                 size = 4;
699                 break;
700         default:
701                 return AE_ERROR;
702         }
703
704         result = raw_pci_write(pci_id->segment, pci_id->bus,
705                                 PCI_DEVFN(pci_id->device, pci_id->function),
706                                 reg, size, value);
707
708         return (result ? AE_ERROR : AE_OK);
709 }
710
711 /* TODO: Change code to take advantage of driver model more */
712 static void acpi_os_derive_pci_id_2(acpi_handle rhandle,        /* upper bound  */
713                                     acpi_handle chandle,        /* current node */
714                                     struct acpi_pci_id **id,
715                                     int *is_bridge, u8 * bus_number)
716 {
717         acpi_handle handle;
718         struct acpi_pci_id *pci_id = *id;
719         acpi_status status;
720         unsigned long long temp;
721         acpi_object_type type;
722
723         acpi_get_parent(chandle, &handle);
724         if (handle != rhandle) {
725                 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
726                                         bus_number);
727
728                 status = acpi_get_type(handle, &type);
729                 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
730                         return;
731
732                 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
733                                           &temp);
734                 if (ACPI_SUCCESS(status)) {
735                         u32 val;
736                         pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
737                         pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
738
739                         if (*is_bridge)
740                                 pci_id->bus = *bus_number;
741
742                         /* any nicer way to get bus number of bridge ? */
743                         status =
744                             acpi_os_read_pci_configuration(pci_id, 0x0e, &val,
745                                                            8);
746                         if (ACPI_SUCCESS(status)
747                             && ((val & 0x7f) == 1 || (val & 0x7f) == 2)) {
748                                 status =
749                                     acpi_os_read_pci_configuration(pci_id, 0x18,
750                                                                    &val, 8);
751                                 if (!ACPI_SUCCESS(status)) {
752                                         /* Certainly broken...  FIX ME */
753                                         return;
754                                 }
755                                 *is_bridge = 1;
756                                 pci_id->bus = val;
757                                 status =
758                                     acpi_os_read_pci_configuration(pci_id, 0x19,
759                                                                    &val, 8);
760                                 if (ACPI_SUCCESS(status)) {
761                                         *bus_number = val;
762                                 }
763                         } else
764                                 *is_bridge = 0;
765                 }
766         }
767 }
768
769 void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound  */
770                            acpi_handle chandle, /* current node */
771                            struct acpi_pci_id **id)
772 {
773         int is_bridge = 1;
774         u8 bus_number = (*id)->bus;
775
776         acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
777 }
778
779 static void acpi_os_execute_deferred(struct work_struct *work)
780 {
781         struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
782
783         if (dpc->wait)
784                 acpi_os_wait_events_complete(NULL);
785
786         dpc->function(dpc->context);
787         kfree(dpc);
788 }
789
790 /*******************************************************************************
791  *
792  * FUNCTION:    acpi_os_execute
793  *
794  * PARAMETERS:  Type               - Type of the callback
795  *              Function           - Function to be executed
796  *              Context            - Function parameters
797  *
798  * RETURN:      Status
799  *
800  * DESCRIPTION: Depending on type, either queues function for deferred execution or
801  *              immediately executes function on a separate thread.
802  *
803  ******************************************************************************/
804
805 static acpi_status __acpi_os_execute(acpi_execute_type type,
806         acpi_osd_exec_callback function, void *context, int hp)
807 {
808         acpi_status status = AE_OK;
809         struct acpi_os_dpc *dpc;
810         struct workqueue_struct *queue;
811         int ret;
812         ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
813                           "Scheduling function [%p(%p)] for deferred execution.\n",
814                           function, context));
815
816         /*
817          * Allocate/initialize DPC structure.  Note that this memory will be
818          * freed by the callee.  The kernel handles the work_struct list  in a
819          * way that allows us to also free its memory inside the callee.
820          * Because we may want to schedule several tasks with different
821          * parameters we can't use the approach some kernel code uses of
822          * having a static work_struct.
823          */
824
825         dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
826         if (!dpc)
827                 return AE_NO_MEMORY;
828
829         dpc->function = function;
830         dpc->context = context;
831
832         /*
833          * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
834          * because the hotplug code may call driver .remove() functions,
835          * which invoke flush_scheduled_work/acpi_os_wait_events_complete
836          * to flush these workqueues.
837          */
838         queue = hp ? kacpi_hotplug_wq :
839                 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
840         dpc->wait = hp ? 1 : 0;
841
842         if (queue == kacpi_hotplug_wq)
843                 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
844         else if (queue == kacpi_notify_wq)
845                 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
846         else
847                 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
848
849         /*
850          * On some machines, a software-initiated SMI causes corruption unless
851          * the SMI runs on CPU 0.  An SMI can be initiated by any AML, but
852          * typically it's done in GPE-related methods that are run via
853          * workqueues, so we can avoid the known corruption cases by always
854          * queueing on CPU 0.
855          */
856         ret = queue_work_on(0, queue, &dpc->work);
857
858         if (!ret) {
859                 printk(KERN_ERR PREFIX
860                           "Call to queue_work() failed.\n");
861                 status = AE_ERROR;
862                 kfree(dpc);
863         }
864         return status;
865 }
866
867 acpi_status acpi_os_execute(acpi_execute_type type,
868                             acpi_osd_exec_callback function, void *context)
869 {
870         return __acpi_os_execute(type, function, context, 0);
871 }
872 EXPORT_SYMBOL(acpi_os_execute);
873
874 acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
875         void *context)
876 {
877         return __acpi_os_execute(0, function, context, 1);
878 }
879
880 void acpi_os_wait_events_complete(void *context)
881 {
882         flush_workqueue(kacpid_wq);
883         flush_workqueue(kacpi_notify_wq);
884 }
885
886 EXPORT_SYMBOL(acpi_os_wait_events_complete);
887
888 /*
889  * Allocate the memory for a spinlock and initialize it.
890  */
891 acpi_status acpi_os_create_lock(acpi_spinlock * handle)
892 {
893         spin_lock_init(*handle);
894
895         return AE_OK;
896 }
897
898 /*
899  * Deallocate the memory for a spinlock.
900  */
901 void acpi_os_delete_lock(acpi_spinlock handle)
902 {
903         return;
904 }
905
906 acpi_status
907 acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
908 {
909         struct semaphore *sem = NULL;
910
911         sem = acpi_os_allocate(sizeof(struct semaphore));
912         if (!sem)
913                 return AE_NO_MEMORY;
914         memset(sem, 0, sizeof(struct semaphore));
915
916         sema_init(sem, initial_units);
917
918         *handle = (acpi_handle *) sem;
919
920         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
921                           *handle, initial_units));
922
923         return AE_OK;
924 }
925
926 /*
927  * TODO: A better way to delete semaphores?  Linux doesn't have a
928  * 'delete_semaphore()' function -- may result in an invalid
929  * pointer dereference for non-synchronized consumers.  Should
930  * we at least check for blocked threads and signal/cancel them?
931  */
932
933 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
934 {
935         struct semaphore *sem = (struct semaphore *)handle;
936
937         if (!sem)
938                 return AE_BAD_PARAMETER;
939
940         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
941
942         BUG_ON(!list_empty(&sem->wait_list));
943         kfree(sem);
944         sem = NULL;
945
946         return AE_OK;
947 }
948
949 /*
950  * TODO: Support for units > 1?
951  */
952 acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
953 {
954         acpi_status status = AE_OK;
955         struct semaphore *sem = (struct semaphore *)handle;
956         long jiffies;
957         int ret = 0;
958
959         if (!sem || (units < 1))
960                 return AE_BAD_PARAMETER;
961
962         if (units > 1)
963                 return AE_SUPPORT;
964
965         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
966                           handle, units, timeout));
967
968         if (timeout == ACPI_WAIT_FOREVER)
969                 jiffies = MAX_SCHEDULE_TIMEOUT;
970         else
971                 jiffies = msecs_to_jiffies(timeout);
972         
973         ret = down_timeout(sem, jiffies);
974         if (ret)
975                 status = AE_TIME;
976
977         if (ACPI_FAILURE(status)) {
978                 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
979                                   "Failed to acquire semaphore[%p|%d|%d], %s",
980                                   handle, units, timeout,
981                                   acpi_format_exception(status)));
982         } else {
983                 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
984                                   "Acquired semaphore[%p|%d|%d]", handle,
985                                   units, timeout));
986         }
987
988         return status;
989 }
990
991 /*
992  * TODO: Support for units > 1?
993  */
994 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
995 {
996         struct semaphore *sem = (struct semaphore *)handle;
997
998         if (!sem || (units < 1))
999                 return AE_BAD_PARAMETER;
1000
1001         if (units > 1)
1002                 return AE_SUPPORT;
1003
1004         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1005                           units));
1006
1007         up(sem);
1008
1009         return AE_OK;
1010 }
1011
1012 #ifdef ACPI_FUTURE_USAGE
1013 u32 acpi_os_get_line(char *buffer)
1014 {
1015
1016 #ifdef ENABLE_DEBUGGER
1017         if (acpi_in_debugger) {
1018                 u32 chars;
1019
1020                 kdb_read(buffer, sizeof(line_buf));
1021
1022                 /* remove the CR kdb includes */
1023                 chars = strlen(buffer) - 1;
1024                 buffer[chars] = '\0';
1025         }
1026 #endif
1027
1028         return 0;
1029 }
1030 #endif                          /*  ACPI_FUTURE_USAGE  */
1031
1032 acpi_status acpi_os_signal(u32 function, void *info)
1033 {
1034         switch (function) {
1035         case ACPI_SIGNAL_FATAL:
1036                 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1037                 break;
1038         case ACPI_SIGNAL_BREAKPOINT:
1039                 /*
1040                  * AML Breakpoint
1041                  * ACPI spec. says to treat it as a NOP unless
1042                  * you are debugging.  So if/when we integrate
1043                  * AML debugger into the kernel debugger its
1044                  * hook will go here.  But until then it is
1045                  * not useful to print anything on breakpoints.
1046                  */
1047                 break;
1048         default:
1049                 break;
1050         }
1051
1052         return AE_OK;
1053 }
1054
1055 static int __init acpi_os_name_setup(char *str)
1056 {
1057         char *p = acpi_os_name;
1058         int count = ACPI_MAX_OVERRIDE_LEN - 1;
1059
1060         if (!str || !*str)
1061                 return 0;
1062
1063         for (; count-- && str && *str; str++) {
1064                 if (isalnum(*str) || *str == ' ' || *str == ':')
1065                         *p++ = *str;
1066                 else if (*str == '\'' || *str == '"')
1067                         continue;
1068                 else
1069                         break;
1070         }
1071         *p = 0;
1072
1073         return 1;
1074
1075 }
1076
1077 __setup("acpi_os_name=", acpi_os_name_setup);
1078
1079 static void __init set_osi_linux(unsigned int enable)
1080 {
1081         if (osi_linux.enable != enable) {
1082                 osi_linux.enable = enable;
1083                 printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
1084                         enable ? "Add": "Delet");
1085         }
1086         return;
1087 }
1088
1089 static void __init acpi_cmdline_osi_linux(unsigned int enable)
1090 {
1091         osi_linux.cmdline = 1;  /* cmdline set the default */
1092         set_osi_linux(enable);
1093
1094         return;
1095 }
1096
1097 void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1098 {
1099         osi_linux.dmi = 1;      /* DMI knows that this box asks OSI(Linux) */
1100
1101         printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1102
1103         if (enable == -1)
1104                 return;
1105
1106         osi_linux.known = 1;    /* DMI knows which OSI(Linux) default needed */
1107
1108         set_osi_linux(enable);
1109
1110         return;
1111 }
1112
1113 /*
1114  * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1115  *
1116  * empty string disables _OSI
1117  * string starting with '!' disables that string
1118  * otherwise string is added to list, augmenting built-in strings
1119  */
1120 int __init acpi_osi_setup(char *str)
1121 {
1122         if (str == NULL || *str == '\0') {
1123                 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1124                 acpi_gbl_create_osi_method = FALSE;
1125         } else if (!strcmp("!Linux", str)) {
1126                 acpi_cmdline_osi_linux(0);      /* !enable */
1127         } else if (*str == '!') {
1128                 if (acpi_osi_invalidate(++str) == AE_OK)
1129                         printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1130         } else if (!strcmp("Linux", str)) {
1131                 acpi_cmdline_osi_linux(1);      /* enable */
1132         } else if (*osi_additional_string == '\0') {
1133                 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
1134                 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1135         }
1136
1137         return 1;
1138 }
1139
1140 __setup("acpi_osi=", acpi_osi_setup);
1141
1142 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1143 static int __init acpi_serialize_setup(char *str)
1144 {
1145         printk(KERN_INFO PREFIX "serialize enabled\n");
1146
1147         acpi_gbl_all_methods_serialized = TRUE;
1148
1149         return 1;
1150 }
1151
1152 __setup("acpi_serialize", acpi_serialize_setup);
1153
1154 /* Check of resource interference between native drivers and ACPI
1155  * OperationRegions (SystemIO and System Memory only).
1156  * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1157  * in arbitrary AML code and can interfere with legacy drivers.
1158  * acpi_enforce_resources= can be set to:
1159  *
1160  *   - strict (default) (2)
1161  *     -> further driver trying to access the resources will not load
1162  *   - lax              (1)
1163  *     -> further driver trying to access the resources will load, but you
1164  *     get a system message that something might go wrong...
1165  *
1166  *   - no               (0)
1167  *     -> ACPI Operation Region resources will not be registered
1168  *
1169  */
1170 #define ENFORCE_RESOURCES_STRICT 2
1171 #define ENFORCE_RESOURCES_LAX    1
1172 #define ENFORCE_RESOURCES_NO     0
1173
1174 static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1175
1176 static int __init acpi_enforce_resources_setup(char *str)
1177 {
1178         if (str == NULL || *str == '\0')
1179                 return 0;
1180
1181         if (!strcmp("strict", str))
1182                 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1183         else if (!strcmp("lax", str))
1184                 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1185         else if (!strcmp("no", str))
1186                 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1187
1188         return 1;
1189 }
1190
1191 __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1192
1193 /* Check for resource conflicts between ACPI OperationRegions and native
1194  * drivers */
1195 int acpi_check_resource_conflict(const struct resource *res)
1196 {
1197         struct acpi_res_list *res_list_elem;
1198         int ioport;
1199         int clash = 0;
1200
1201         if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1202                 return 0;
1203         if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1204                 return 0;
1205
1206         ioport = res->flags & IORESOURCE_IO;
1207
1208         spin_lock(&acpi_res_lock);
1209         list_for_each_entry(res_list_elem, &resource_list_head,
1210                             resource_list) {
1211                 if (ioport && (res_list_elem->resource_type
1212                                != ACPI_ADR_SPACE_SYSTEM_IO))
1213                         continue;
1214                 if (!ioport && (res_list_elem->resource_type
1215                                 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1216                         continue;
1217
1218                 if (res->end < res_list_elem->start
1219                     || res_list_elem->end < res->start)
1220                         continue;
1221                 clash = 1;
1222                 break;
1223         }
1224         spin_unlock(&acpi_res_lock);
1225
1226         if (clash) {
1227                 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
1228                         printk(KERN_WARNING "ACPI: resource %s %pR"
1229                                " conflicts with ACPI region %s %pR\n",
1230                                res->name, res, res_list_elem->name,
1231                                res_list_elem);
1232                         if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1233                                 printk(KERN_NOTICE "ACPI: This conflict may"
1234                                        " cause random problems and system"
1235                                        " instability\n");
1236                         printk(KERN_INFO "ACPI: If an ACPI driver is available"
1237                                " for this device, you should use it instead of"
1238                                " the native driver\n");
1239                 }
1240                 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1241                         return -EBUSY;
1242         }
1243         return 0;
1244 }
1245 EXPORT_SYMBOL(acpi_check_resource_conflict);
1246
1247 int acpi_check_region(resource_size_t start, resource_size_t n,
1248                       const char *name)
1249 {
1250         struct resource res = {
1251                 .start = start,
1252                 .end   = start + n - 1,
1253                 .name  = name,
1254                 .flags = IORESOURCE_IO,
1255         };
1256
1257         return acpi_check_resource_conflict(&res);
1258 }
1259 EXPORT_SYMBOL(acpi_check_region);
1260
1261 int acpi_check_mem_region(resource_size_t start, resource_size_t n,
1262                       const char *name)
1263 {
1264         struct resource res = {
1265                 .start = start,
1266                 .end   = start + n - 1,
1267                 .name  = name,
1268                 .flags = IORESOURCE_MEM,
1269         };
1270
1271         return acpi_check_resource_conflict(&res);
1272
1273 }
1274 EXPORT_SYMBOL(acpi_check_mem_region);
1275
1276 /*
1277  * Let drivers know whether the resource checks are effective
1278  */
1279 int acpi_resources_are_enforced(void)
1280 {
1281         return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1282 }
1283 EXPORT_SYMBOL(acpi_resources_are_enforced);
1284
1285 /*
1286  * Acquire a spinlock.
1287  *
1288  * handle is a pointer to the spinlock_t.
1289  */
1290
1291 acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
1292 {
1293         acpi_cpu_flags flags;
1294         spin_lock_irqsave(lockp, flags);
1295         return flags;
1296 }
1297
1298 /*
1299  * Release a spinlock. See above.
1300  */
1301
1302 void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
1303 {
1304         spin_unlock_irqrestore(lockp, flags);
1305 }
1306
1307 #ifndef ACPI_USE_LOCAL_CACHE
1308
1309 /*******************************************************************************
1310  *
1311  * FUNCTION:    acpi_os_create_cache
1312  *
1313  * PARAMETERS:  name      - Ascii name for the cache
1314  *              size      - Size of each cached object
1315  *              depth     - Maximum depth of the cache (in objects) <ignored>
1316  *              cache     - Where the new cache object is returned
1317  *
1318  * RETURN:      status
1319  *
1320  * DESCRIPTION: Create a cache object
1321  *
1322  ******************************************************************************/
1323
1324 acpi_status
1325 acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
1326 {
1327         *cache = kmem_cache_create(name, size, 0, 0, NULL);
1328         if (*cache == NULL)
1329                 return AE_ERROR;
1330         else
1331                 return AE_OK;
1332 }
1333
1334 /*******************************************************************************
1335  *
1336  * FUNCTION:    acpi_os_purge_cache
1337  *
1338  * PARAMETERS:  Cache           - Handle to cache object
1339  *
1340  * RETURN:      Status
1341  *
1342  * DESCRIPTION: Free all objects within the requested cache.
1343  *
1344  ******************************************************************************/
1345
1346 acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
1347 {
1348         kmem_cache_shrink(cache);
1349         return (AE_OK);
1350 }
1351
1352 /*******************************************************************************
1353  *
1354  * FUNCTION:    acpi_os_delete_cache
1355  *
1356  * PARAMETERS:  Cache           - Handle to cache object
1357  *
1358  * RETURN:      Status
1359  *
1360  * DESCRIPTION: Free all objects within the requested cache and delete the
1361  *              cache object.
1362  *
1363  ******************************************************************************/
1364
1365 acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
1366 {
1367         kmem_cache_destroy(cache);
1368         return (AE_OK);
1369 }
1370
1371 /*******************************************************************************
1372  *
1373  * FUNCTION:    acpi_os_release_object
1374  *
1375  * PARAMETERS:  Cache       - Handle to cache object
1376  *              Object      - The object to be released
1377  *
1378  * RETURN:      None
1379  *
1380  * DESCRIPTION: Release an object to the specified cache.  If cache is full,
1381  *              the object is deleted.
1382  *
1383  ******************************************************************************/
1384
1385 acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
1386 {
1387         kmem_cache_free(cache, object);
1388         return (AE_OK);
1389 }
1390
1391 /******************************************************************************
1392  *
1393  * FUNCTION:    acpi_os_validate_interface
1394  *
1395  * PARAMETERS:  interface           - Requested interface to be validated
1396  *
1397  * RETURN:      AE_OK if interface is supported, AE_SUPPORT otherwise
1398  *
1399  * DESCRIPTION: Match an interface string to the interfaces supported by the
1400  *              host. Strings originate from an AML call to the _OSI method.
1401  *
1402  *****************************************************************************/
1403
1404 acpi_status
1405 acpi_os_validate_interface (char *interface)
1406 {
1407         if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1408                 return AE_OK;
1409         if (!strcmp("Linux", interface)) {
1410
1411                 printk(KERN_NOTICE PREFIX
1412                         "BIOS _OSI(Linux) query %s%s\n",
1413                         osi_linux.enable ? "honored" : "ignored",
1414                         osi_linux.cmdline ? " via cmdline" :
1415                         osi_linux.dmi ? " via DMI" : "");
1416
1417                 if (osi_linux.enable)
1418                         return AE_OK;
1419         }
1420         return AE_SUPPORT;
1421 }
1422
1423 static inline int acpi_res_list_add(struct acpi_res_list *res)
1424 {
1425         struct acpi_res_list *res_list_elem;
1426
1427         list_for_each_entry(res_list_elem, &resource_list_head,
1428                             resource_list) {
1429
1430                 if (res->resource_type == res_list_elem->resource_type &&
1431                     res->start == res_list_elem->start &&
1432                     res->end == res_list_elem->end) {
1433
1434                         /*
1435                          * The Region(addr,len) already exist in the list,
1436                          * just increase the count
1437                          */
1438
1439                         res_list_elem->count++;
1440                         return 0;
1441                 }
1442         }
1443
1444         res->count = 1;
1445         list_add(&res->resource_list, &resource_list_head);
1446         return 1;
1447 }
1448
1449 static inline void acpi_res_list_del(struct acpi_res_list *res)
1450 {
1451         struct acpi_res_list *res_list_elem;
1452
1453         list_for_each_entry(res_list_elem, &resource_list_head,
1454                             resource_list) {
1455
1456                 if (res->resource_type == res_list_elem->resource_type &&
1457                     res->start == res_list_elem->start &&
1458                     res->end == res_list_elem->end) {
1459
1460                         /*
1461                          * If the res count is decreased to 0,
1462                          * remove and free it
1463                          */
1464
1465                         if (--res_list_elem->count == 0) {
1466                                 list_del(&res_list_elem->resource_list);
1467                                 kfree(res_list_elem);
1468                         }
1469                         return;
1470                 }
1471         }
1472 }
1473
1474 acpi_status
1475 acpi_os_invalidate_address(
1476     u8                   space_id,
1477     acpi_physical_address   address,
1478     acpi_size               length)
1479 {
1480         struct acpi_res_list res;
1481
1482         switch (space_id) {
1483         case ACPI_ADR_SPACE_SYSTEM_IO:
1484         case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1485                 /* Only interference checks against SystemIO and SystemMemory
1486                    are needed */
1487                 res.start = address;
1488                 res.end = address + length - 1;
1489                 res.resource_type = space_id;
1490                 spin_lock(&acpi_res_lock);
1491                 acpi_res_list_del(&res);
1492                 spin_unlock(&acpi_res_lock);
1493                 break;
1494         case ACPI_ADR_SPACE_PCI_CONFIG:
1495         case ACPI_ADR_SPACE_EC:
1496         case ACPI_ADR_SPACE_SMBUS:
1497         case ACPI_ADR_SPACE_CMOS:
1498         case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1499         case ACPI_ADR_SPACE_DATA_TABLE:
1500         case ACPI_ADR_SPACE_FIXED_HARDWARE:
1501                 break;
1502         }
1503         return AE_OK;
1504 }
1505
1506 /******************************************************************************
1507  *
1508  * FUNCTION:    acpi_os_validate_address
1509  *
1510  * PARAMETERS:  space_id             - ACPI space ID
1511  *              address             - Physical address
1512  *              length              - Address length
1513  *
1514  * RETURN:      AE_OK if address/length is valid for the space_id. Otherwise,
1515  *              should return AE_AML_ILLEGAL_ADDRESS.
1516  *
1517  * DESCRIPTION: Validate a system address via the host OS. Used to validate
1518  *              the addresses accessed by AML operation regions.
1519  *
1520  *****************************************************************************/
1521
1522 acpi_status
1523 acpi_os_validate_address (
1524     u8                   space_id,
1525     acpi_physical_address   address,
1526     acpi_size               length,
1527     char *name)
1528 {
1529         struct acpi_res_list *res;
1530         int added;
1531         if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1532                 return AE_OK;
1533
1534         switch (space_id) {
1535         case ACPI_ADR_SPACE_SYSTEM_IO:
1536         case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1537                 /* Only interference checks against SystemIO and SystemMemory
1538                    are needed */
1539                 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1540                 if (!res)
1541                         return AE_OK;
1542                 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1543                 strlcpy(res->name, name, 5);
1544                 res->start = address;
1545                 res->end = address + length - 1;
1546                 res->resource_type = space_id;
1547                 spin_lock(&acpi_res_lock);
1548                 added = acpi_res_list_add(res);
1549                 spin_unlock(&acpi_res_lock);
1550                 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1551                          "name: %s\n", added ? "Added" : "Already exist",
1552                          (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
1553                          ? "SystemIO" : "System Memory",
1554                          (unsigned long long)res->start,
1555                          (unsigned long long)res->end,
1556                          res->name);
1557                 if (!added)
1558                         kfree(res);
1559                 break;
1560         case ACPI_ADR_SPACE_PCI_CONFIG:
1561         case ACPI_ADR_SPACE_EC:
1562         case ACPI_ADR_SPACE_SMBUS:
1563         case ACPI_ADR_SPACE_CMOS:
1564         case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1565         case ACPI_ADR_SPACE_DATA_TABLE:
1566         case ACPI_ADR_SPACE_FIXED_HARDWARE:
1567                 break;
1568         }
1569         return AE_OK;
1570 }
1571 #endif
1572
1573 acpi_status __init acpi_os_initialize(void)
1574 {
1575         acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1576         acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1577         acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1578         acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1579
1580         return AE_OK;
1581 }
1582
1583 acpi_status acpi_os_initialize1(void)
1584 {
1585         kacpid_wq = create_workqueue("kacpid");
1586         kacpi_notify_wq = create_workqueue("kacpi_notify");
1587         kacpi_hotplug_wq = create_workqueue("kacpi_hotplug");
1588         BUG_ON(!kacpid_wq);
1589         BUG_ON(!kacpi_notify_wq);
1590         BUG_ON(!kacpi_hotplug_wq);
1591         return AE_OK;
1592 }
1593
1594 acpi_status acpi_os_terminate(void)
1595 {
1596         if (acpi_irq_handler) {
1597                 acpi_os_remove_interrupt_handler(acpi_irq_irq,
1598                                                  acpi_irq_handler);
1599         }
1600
1601         acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1602         acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1603         acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1604         acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1605
1606         destroy_workqueue(kacpid_wq);
1607         destroy_workqueue(kacpi_notify_wq);
1608         destroy_workqueue(kacpi_hotplug_wq);
1609
1610         return AE_OK;
1611 }