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