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