ACPI: create /sys/firmware/acpi/interrupts
[safe/jmp/linux-2.6] / 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  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  *
26  */
27
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/mm.h>
32 #include <linux/pci.h>
33 #include <linux/interrupt.h>
34 #include <linux/kmod.h>
35 #include <linux/delay.h>
36 #include <linux/dmi.h>
37 #include <linux/workqueue.h>
38 #include <linux/nmi.h>
39 #include <linux/acpi.h>
40 #include <acpi/acpi.h>
41 #include <asm/io.h>
42 #include <acpi/acpi_bus.h>
43 #include <acpi/processor.h>
44 #include <asm/uaccess.h>
45
46 #include <linux/efi.h>
47
48 #define _COMPONENT              ACPI_OS_SERVICES
49 ACPI_MODULE_NAME("osl");
50 #define PREFIX          "ACPI: "
51 struct acpi_os_dpc {
52         acpi_osd_exec_callback function;
53         void *context;
54         struct work_struct work;
55 };
56
57 #ifdef CONFIG_ACPI_CUSTOM_DSDT
58 #include CONFIG_ACPI_CUSTOM_DSDT_FILE
59 #endif
60
61 #ifdef ENABLE_DEBUGGER
62 #include <linux/kdb.h>
63
64 /* stuff for debugger support */
65 int acpi_in_debugger;
66 EXPORT_SYMBOL(acpi_in_debugger);
67
68 extern char line_buf[80];
69 #endif                          /*ENABLE_DEBUGGER */
70
71 static unsigned int acpi_irq_irq;
72 static acpi_osd_handler acpi_irq_handler;
73 static void *acpi_irq_context;
74 static struct workqueue_struct *kacpid_wq;
75 static struct workqueue_struct *kacpi_notify_wq;
76
77 #define OSI_STRING_LENGTH_MAX 64        /* arbitrary */
78 static char osi_additional_string[OSI_STRING_LENGTH_MAX];
79
80 /*
81  * "Ode to _OSI(Linux)"
82  *
83  * osi_linux -- Control response to BIOS _OSI(Linux) query.
84  *
85  * As Linux evolves, the features that it supports change.
86  * So an OSI string such as "Linux" is not specific enough
87  * to be useful across multiple versions of Linux.  It
88  * doesn't identify any particular feature, interface,
89  * or even any particular version of Linux...
90  *
91  * Unfortunately, Linux-2.6.22 and earlier responded "yes"
92  * to a BIOS _OSI(Linux) query.  When
93  * a reference mobile BIOS started using it, its use
94  * started to spread to many vendor platforms.
95  * As it is not supportable, we need to halt that spread.
96  *
97  * Today, most BIOS references to _OSI(Linux) are noise --
98  * they have no functional effect and are just dead code
99  * carried over from the reference BIOS.
100  *
101  * The next most common case is that _OSI(Linux) harms Linux,
102  * usually by causing the BIOS to follow paths that are
103  * not tested during Windows validation.
104  *
105  * Finally, there is a short list of platforms
106  * where OSI(Linux) benefits Linux.
107  *
108  * In Linux-2.6.23, OSI(Linux) is first disabled by default.
109  * DMI is used to disable the dmesg warning about OSI(Linux)
110  * on platforms where it is known to have no effect.
111  * But a dmesg warning remains for systems where
112  * we do not know if OSI(Linux) is good or bad for the system.
113  * DMI is also used to enable OSI(Linux) for the machines
114  * that are known to need it.
115  *
116  * BIOS writers should NOT query _OSI(Linux) on future systems.
117  * It will be ignored by default, and to get Linux to
118  * not ignore it will require a kernel source update to
119  * add a DMI entry, or a boot-time "acpi_osi=Linux" invocation.
120  */
121 #define OSI_LINUX_ENABLE 0
122
123 struct osi_linux {
124         unsigned int    enable:1;
125         unsigned int    dmi:1;
126         unsigned int    cmdline:1;
127         unsigned int    known:1;
128 } osi_linux = { OSI_LINUX_ENABLE, 0, 0, 0};
129
130 static void __init acpi_request_region (struct acpi_generic_address *addr,
131         unsigned int length, char *desc)
132 {
133         struct resource *res;
134
135         if (!addr->address || !length)
136                 return;
137
138         if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
139                 res = request_region(addr->address, length, desc);
140         else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
141                 res = request_mem_region(addr->address, length, desc);
142 }
143
144 static int __init acpi_reserve_resources(void)
145 {
146         acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
147                 "ACPI PM1a_EVT_BLK");
148
149         acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
150                 "ACPI PM1b_EVT_BLK");
151
152         acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
153                 "ACPI PM1a_CNT_BLK");
154
155         acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
156                 "ACPI PM1b_CNT_BLK");
157
158         if (acpi_gbl_FADT.pm_timer_length == 4)
159                 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
160
161         acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
162                 "ACPI PM2_CNT_BLK");
163
164         /* Length of GPE blocks must be a non-negative multiple of 2 */
165
166         if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
167                 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
168                                acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
169
170         if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
171                 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
172                                acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
173
174         return 0;
175 }
176 device_initcall(acpi_reserve_resources);
177
178 acpi_status __init acpi_os_initialize(void)
179 {
180         return AE_OK;
181 }
182
183 acpi_status acpi_os_initialize1(void)
184 {
185         /*
186          * Initialize PCI configuration space access, as we'll need to access
187          * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
188          */
189         if (!raw_pci_ops) {
190                 printk(KERN_ERR PREFIX
191                        "Access to PCI configuration space unavailable\n");
192                 return AE_NULL_ENTRY;
193         }
194         kacpid_wq = create_singlethread_workqueue("kacpid");
195         kacpi_notify_wq = create_singlethread_workqueue("kacpi_notify");
196         BUG_ON(!kacpid_wq);
197         BUG_ON(!kacpi_notify_wq);
198         return AE_OK;
199 }
200
201 acpi_status acpi_os_terminate(void)
202 {
203         if (acpi_irq_handler) {
204                 acpi_os_remove_interrupt_handler(acpi_irq_irq,
205                                                  acpi_irq_handler);
206         }
207
208         destroy_workqueue(kacpid_wq);
209         destroy_workqueue(kacpi_notify_wq);
210
211         return AE_OK;
212 }
213
214 void acpi_os_printf(const char *fmt, ...)
215 {
216         va_list args;
217         va_start(args, fmt);
218         acpi_os_vprintf(fmt, args);
219         va_end(args);
220 }
221
222 EXPORT_SYMBOL(acpi_os_printf);
223
224 void acpi_os_vprintf(const char *fmt, va_list args)
225 {
226         static char buffer[512];
227
228         vsprintf(buffer, fmt, args);
229
230 #ifdef ENABLE_DEBUGGER
231         if (acpi_in_debugger) {
232                 kdb_printf("%s", buffer);
233         } else {
234                 printk("%s", buffer);
235         }
236 #else
237         printk("%s", buffer);
238 #endif
239 }
240
241 acpi_physical_address __init acpi_os_get_root_pointer(void)
242 {
243         if (efi_enabled) {
244                 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
245                         return efi.acpi20;
246                 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
247                         return efi.acpi;
248                 else {
249                         printk(KERN_ERR PREFIX
250                                "System description tables not found\n");
251                         return 0;
252                 }
253         } else
254                 return acpi_find_rsdp();
255 }
256
257 void __iomem *acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
258 {
259         if (phys > ULONG_MAX) {
260                 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
261                 return NULL;
262         }
263         if (acpi_gbl_permanent_mmap)
264                 /*
265                 * ioremap checks to ensure this is in reserved space
266                 */
267                 return ioremap((unsigned long)phys, size);
268         else
269                 return __acpi_map_table((unsigned long)phys, size);
270 }
271 EXPORT_SYMBOL_GPL(acpi_os_map_memory);
272
273 void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
274 {
275         if (acpi_gbl_permanent_mmap) {
276                 iounmap(virt);
277         }
278 }
279 EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
280
281 #ifdef ACPI_FUTURE_USAGE
282 acpi_status
283 acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
284 {
285         if (!phys || !virt)
286                 return AE_BAD_PARAMETER;
287
288         *phys = virt_to_phys(virt);
289
290         return AE_OK;
291 }
292 #endif
293
294 #define ACPI_MAX_OVERRIDE_LEN 100
295
296 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
297
298 acpi_status
299 acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
300                             acpi_string * new_val)
301 {
302         if (!init_val || !new_val)
303                 return AE_BAD_PARAMETER;
304
305         *new_val = NULL;
306         if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
307                 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
308                        acpi_os_name);
309                 *new_val = acpi_os_name;
310         }
311
312         return AE_OK;
313 }
314
315 acpi_status
316 acpi_os_table_override(struct acpi_table_header * existing_table,
317                        struct acpi_table_header ** new_table)
318 {
319         if (!existing_table || !new_table)
320                 return AE_BAD_PARAMETER;
321
322 #ifdef CONFIG_ACPI_CUSTOM_DSDT
323         if (strncmp(existing_table->signature, "DSDT", 4) == 0)
324                 *new_table = (struct acpi_table_header *)AmlCode;
325         else
326                 *new_table = NULL;
327 #else
328         *new_table = NULL;
329 #endif
330         return AE_OK;
331 }
332
333 static irqreturn_t acpi_irq(int irq, void *dev_id)
334 {
335         u32 handled;
336
337         handled = (*acpi_irq_handler) (acpi_irq_context);
338
339         if (handled) {
340                 acpi_irq_handled++;
341                 return IRQ_HANDLED;
342         } else
343                 return IRQ_NONE;
344 }
345
346 acpi_status
347 acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
348                                   void *context)
349 {
350         unsigned int irq;
351
352         acpi_irq_stats_init();
353
354         /*
355          * Ignore the GSI from the core, and use the value in our copy of the
356          * FADT. It may not be the same if an interrupt source override exists
357          * for the SCI.
358          */
359         gsi = acpi_gbl_FADT.sci_interrupt;
360         if (acpi_gsi_to_irq(gsi, &irq) < 0) {
361                 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
362                        gsi);
363                 return AE_OK;
364         }
365
366         acpi_irq_handler = handler;
367         acpi_irq_context = context;
368         if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
369                 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
370                 return AE_NOT_ACQUIRED;
371         }
372         acpi_irq_irq = irq;
373
374         return AE_OK;
375 }
376
377 acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
378 {
379         if (irq) {
380                 free_irq(irq, acpi_irq);
381                 acpi_irq_handler = NULL;
382                 acpi_irq_irq = 0;
383         }
384
385         return AE_OK;
386 }
387
388 /*
389  * Running in interpreter thread context, safe to sleep
390  */
391
392 void acpi_os_sleep(acpi_integer ms)
393 {
394         schedule_timeout_interruptible(msecs_to_jiffies(ms));
395 }
396
397 EXPORT_SYMBOL(acpi_os_sleep);
398
399 void acpi_os_stall(u32 us)
400 {
401         while (us) {
402                 u32 delay = 1000;
403
404                 if (delay > us)
405                         delay = us;
406                 udelay(delay);
407                 touch_nmi_watchdog();
408                 us -= delay;
409         }
410 }
411
412 EXPORT_SYMBOL(acpi_os_stall);
413
414 /*
415  * Support ACPI 3.0 AML Timer operand
416  * Returns 64-bit free-running, monotonically increasing timer
417  * with 100ns granularity
418  */
419 u64 acpi_os_get_timer(void)
420 {
421         static u64 t;
422
423 #ifdef  CONFIG_HPET
424         /* TBD: use HPET if available */
425 #endif
426
427 #ifdef  CONFIG_X86_PM_TIMER
428         /* TBD: default to PM timer if HPET was not available */
429 #endif
430         if (!t)
431                 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
432
433         return ++t;
434 }
435
436 acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
437 {
438         u32 dummy;
439
440         if (!value)
441                 value = &dummy;
442
443         *value = 0;
444         if (width <= 8) {
445                 *(u8 *) value = inb(port);
446         } else if (width <= 16) {
447                 *(u16 *) value = inw(port);
448         } else if (width <= 32) {
449                 *(u32 *) value = inl(port);
450         } else {
451                 BUG();
452         }
453
454         return AE_OK;
455 }
456
457 EXPORT_SYMBOL(acpi_os_read_port);
458
459 acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
460 {
461         if (width <= 8) {
462                 outb(value, port);
463         } else if (width <= 16) {
464                 outw(value, port);
465         } else if (width <= 32) {
466                 outl(value, port);
467         } else {
468                 BUG();
469         }
470
471         return AE_OK;
472 }
473
474 EXPORT_SYMBOL(acpi_os_write_port);
475
476 acpi_status
477 acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
478 {
479         u32 dummy;
480         void __iomem *virt_addr;
481
482         virt_addr = ioremap(phys_addr, width);
483         if (!value)
484                 value = &dummy;
485
486         switch (width) {
487         case 8:
488                 *(u8 *) value = readb(virt_addr);
489                 break;
490         case 16:
491                 *(u16 *) value = readw(virt_addr);
492                 break;
493         case 32:
494                 *(u32 *) value = readl(virt_addr);
495                 break;
496         default:
497                 BUG();
498         }
499
500         iounmap(virt_addr);
501
502         return AE_OK;
503 }
504
505 acpi_status
506 acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
507 {
508         void __iomem *virt_addr;
509
510         virt_addr = ioremap(phys_addr, width);
511
512         switch (width) {
513         case 8:
514                 writeb(value, virt_addr);
515                 break;
516         case 16:
517                 writew(value, virt_addr);
518                 break;
519         case 32:
520                 writel(value, virt_addr);
521                 break;
522         default:
523                 BUG();
524         }
525
526         iounmap(virt_addr);
527
528         return AE_OK;
529 }
530
531 acpi_status
532 acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
533                                void *value, u32 width)
534 {
535         int result, size;
536
537         if (!value)
538                 return AE_BAD_PARAMETER;
539
540         switch (width) {
541         case 8:
542                 size = 1;
543                 break;
544         case 16:
545                 size = 2;
546                 break;
547         case 32:
548                 size = 4;
549                 break;
550         default:
551                 return AE_ERROR;
552         }
553
554         BUG_ON(!raw_pci_ops);
555
556         result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
557                                    PCI_DEVFN(pci_id->device, pci_id->function),
558                                    reg, size, value);
559
560         return (result ? AE_ERROR : AE_OK);
561 }
562
563 EXPORT_SYMBOL(acpi_os_read_pci_configuration);
564
565 acpi_status
566 acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
567                                 acpi_integer value, u32 width)
568 {
569         int result, size;
570
571         switch (width) {
572         case 8:
573                 size = 1;
574                 break;
575         case 16:
576                 size = 2;
577                 break;
578         case 32:
579                 size = 4;
580                 break;
581         default:
582                 return AE_ERROR;
583         }
584
585         BUG_ON(!raw_pci_ops);
586
587         result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
588                                     PCI_DEVFN(pci_id->device, pci_id->function),
589                                     reg, size, value);
590
591         return (result ? AE_ERROR : AE_OK);
592 }
593
594 /* TODO: Change code to take advantage of driver model more */
595 static void acpi_os_derive_pci_id_2(acpi_handle rhandle,        /* upper bound  */
596                                     acpi_handle chandle,        /* current node */
597                                     struct acpi_pci_id **id,
598                                     int *is_bridge, u8 * bus_number)
599 {
600         acpi_handle handle;
601         struct acpi_pci_id *pci_id = *id;
602         acpi_status status;
603         unsigned long temp;
604         acpi_object_type type;
605         u8 tu8;
606
607         acpi_get_parent(chandle, &handle);
608         if (handle != rhandle) {
609                 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
610                                         bus_number);
611
612                 status = acpi_get_type(handle, &type);
613                 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
614                         return;
615
616                 status =
617                     acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
618                                           &temp);
619                 if (ACPI_SUCCESS(status)) {
620                         pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
621                         pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
622
623                         if (*is_bridge)
624                                 pci_id->bus = *bus_number;
625
626                         /* any nicer way to get bus number of bridge ? */
627                         status =
628                             acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
629                                                            8);
630                         if (ACPI_SUCCESS(status)
631                             && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
632                                 status =
633                                     acpi_os_read_pci_configuration(pci_id, 0x18,
634                                                                    &tu8, 8);
635                                 if (!ACPI_SUCCESS(status)) {
636                                         /* Certainly broken...  FIX ME */
637                                         return;
638                                 }
639                                 *is_bridge = 1;
640                                 pci_id->bus = tu8;
641                                 status =
642                                     acpi_os_read_pci_configuration(pci_id, 0x19,
643                                                                    &tu8, 8);
644                                 if (ACPI_SUCCESS(status)) {
645                                         *bus_number = tu8;
646                                 }
647                         } else
648                                 *is_bridge = 0;
649                 }
650         }
651 }
652
653 void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound  */
654                            acpi_handle chandle, /* current node */
655                            struct acpi_pci_id **id)
656 {
657         int is_bridge = 1;
658         u8 bus_number = (*id)->bus;
659
660         acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
661 }
662
663 static void acpi_os_execute_deferred(struct work_struct *work)
664 {
665         struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
666         if (!dpc) {
667                 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
668                 return;
669         }
670
671         dpc->function(dpc->context);
672         kfree(dpc);
673
674         /* Yield cpu to notify thread */
675         cond_resched();
676
677         return;
678 }
679
680 static void acpi_os_execute_notify(struct work_struct *work)
681 {
682         struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
683
684         if (!dpc) {
685                 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
686                 return;
687         }
688
689         dpc->function(dpc->context);
690
691         kfree(dpc);
692
693         return;
694 }
695
696 /*******************************************************************************
697  *
698  * FUNCTION:    acpi_os_execute
699  *
700  * PARAMETERS:  Type               - Type of the callback
701  *              Function           - Function to be executed
702  *              Context            - Function parameters
703  *
704  * RETURN:      Status
705  *
706  * DESCRIPTION: Depending on type, either queues function for deferred execution or
707  *              immediately executes function on a separate thread.
708  *
709  ******************************************************************************/
710
711 acpi_status acpi_os_execute(acpi_execute_type type,
712                             acpi_osd_exec_callback function, void *context)
713 {
714         acpi_status status = AE_OK;
715         struct acpi_os_dpc *dpc;
716
717         ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
718                           "Scheduling function [%p(%p)] for deferred execution.\n",
719                           function, context));
720
721         if (!function)
722                 return AE_BAD_PARAMETER;
723
724         /*
725          * Allocate/initialize DPC structure.  Note that this memory will be
726          * freed by the callee.  The kernel handles the work_struct list  in a
727          * way that allows us to also free its memory inside the callee.
728          * Because we may want to schedule several tasks with different
729          * parameters we can't use the approach some kernel code uses of
730          * having a static work_struct.
731          */
732
733         dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
734         if (!dpc)
735                 return_ACPI_STATUS(AE_NO_MEMORY);
736
737         dpc->function = function;
738         dpc->context = context;
739
740         if (type == OSL_NOTIFY_HANDLER) {
741                 INIT_WORK(&dpc->work, acpi_os_execute_notify);
742                 if (!queue_work(kacpi_notify_wq, &dpc->work)) {
743                         status = AE_ERROR;
744                         kfree(dpc);
745                 }
746         } else {
747                 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
748                 if (!queue_work(kacpid_wq, &dpc->work)) {
749                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
750                                   "Call to queue_work() failed.\n"));
751                         status = AE_ERROR;
752                         kfree(dpc);
753                 }
754         }
755         return_ACPI_STATUS(status);
756 }
757
758 EXPORT_SYMBOL(acpi_os_execute);
759
760 void acpi_os_wait_events_complete(void *context)
761 {
762         flush_workqueue(kacpid_wq);
763 }
764
765 EXPORT_SYMBOL(acpi_os_wait_events_complete);
766
767 /*
768  * Allocate the memory for a spinlock and initialize it.
769  */
770 acpi_status acpi_os_create_lock(acpi_spinlock * handle)
771 {
772         spin_lock_init(*handle);
773
774         return AE_OK;
775 }
776
777 /*
778  * Deallocate the memory for a spinlock.
779  */
780 void acpi_os_delete_lock(acpi_spinlock handle)
781 {
782         return;
783 }
784
785 acpi_status
786 acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
787 {
788         struct semaphore *sem = NULL;
789
790
791         sem = acpi_os_allocate(sizeof(struct semaphore));
792         if (!sem)
793                 return AE_NO_MEMORY;
794         memset(sem, 0, sizeof(struct semaphore));
795
796         sema_init(sem, initial_units);
797
798         *handle = (acpi_handle *) sem;
799
800         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
801                           *handle, initial_units));
802
803         return AE_OK;
804 }
805
806 EXPORT_SYMBOL(acpi_os_create_semaphore);
807
808 /*
809  * TODO: A better way to delete semaphores?  Linux doesn't have a
810  * 'delete_semaphore()' function -- may result in an invalid
811  * pointer dereference for non-synchronized consumers.  Should
812  * we at least check for blocked threads and signal/cancel them?
813  */
814
815 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
816 {
817         struct semaphore *sem = (struct semaphore *)handle;
818
819
820         if (!sem)
821                 return AE_BAD_PARAMETER;
822
823         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
824
825         kfree(sem);
826         sem = NULL;
827
828         return AE_OK;
829 }
830
831 EXPORT_SYMBOL(acpi_os_delete_semaphore);
832
833 /*
834  * TODO: The kernel doesn't have a 'down_timeout' function -- had to
835  * improvise.  The process is to sleep for one scheduler quantum
836  * until the semaphore becomes available.  Downside is that this
837  * may result in starvation for timeout-based waits when there's
838  * lots of semaphore activity.
839  *
840  * TODO: Support for units > 1?
841  */
842 acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
843 {
844         acpi_status status = AE_OK;
845         struct semaphore *sem = (struct semaphore *)handle;
846         int ret = 0;
847
848
849         if (!sem || (units < 1))
850                 return AE_BAD_PARAMETER;
851
852         if (units > 1)
853                 return AE_SUPPORT;
854
855         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
856                           handle, units, timeout));
857
858         /*
859          * This can be called during resume with interrupts off.
860          * Like boot-time, we should be single threaded and will
861          * always get the lock if we try -- timeout or not.
862          * If this doesn't succeed, then we will oops courtesy of
863          * might_sleep() in down().
864          */
865         if (!down_trylock(sem))
866                 return AE_OK;
867
868         switch (timeout) {
869                 /*
870                  * No Wait:
871                  * --------
872                  * A zero timeout value indicates that we shouldn't wait - just
873                  * acquire the semaphore if available otherwise return AE_TIME
874                  * (a.k.a. 'would block').
875                  */
876         case 0:
877                 if (down_trylock(sem))
878                         status = AE_TIME;
879                 break;
880
881                 /*
882                  * Wait Indefinitely:
883                  * ------------------
884                  */
885         case ACPI_WAIT_FOREVER:
886                 down(sem);
887                 break;
888
889                 /*
890                  * Wait w/ Timeout:
891                  * ----------------
892                  */
893         default:
894                 // TODO: A better timeout algorithm?
895                 {
896                         int i = 0;
897                         static const int quantum_ms = 1000 / HZ;
898
899                         ret = down_trylock(sem);
900                         for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
901                                 schedule_timeout_interruptible(1);
902                                 ret = down_trylock(sem);
903                         }
904
905                         if (ret != 0)
906                                 status = AE_TIME;
907                 }
908                 break;
909         }
910
911         if (ACPI_FAILURE(status)) {
912                 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
913                                   "Failed to acquire semaphore[%p|%d|%d], %s",
914                                   handle, units, timeout,
915                                   acpi_format_exception(status)));
916         } else {
917                 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
918                                   "Acquired semaphore[%p|%d|%d]", handle,
919                                   units, timeout));
920         }
921
922         return status;
923 }
924
925 EXPORT_SYMBOL(acpi_os_wait_semaphore);
926
927 /*
928  * TODO: Support for units > 1?
929  */
930 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
931 {
932         struct semaphore *sem = (struct semaphore *)handle;
933
934
935         if (!sem || (units < 1))
936                 return AE_BAD_PARAMETER;
937
938         if (units > 1)
939                 return AE_SUPPORT;
940
941         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
942                           units));
943
944         up(sem);
945
946         return AE_OK;
947 }
948
949 EXPORT_SYMBOL(acpi_os_signal_semaphore);
950
951 #ifdef ACPI_FUTURE_USAGE
952 u32 acpi_os_get_line(char *buffer)
953 {
954
955 #ifdef ENABLE_DEBUGGER
956         if (acpi_in_debugger) {
957                 u32 chars;
958
959                 kdb_read(buffer, sizeof(line_buf));
960
961                 /* remove the CR kdb includes */
962                 chars = strlen(buffer) - 1;
963                 buffer[chars] = '\0';
964         }
965 #endif
966
967         return 0;
968 }
969 #endif                          /*  ACPI_FUTURE_USAGE  */
970
971 acpi_status acpi_os_signal(u32 function, void *info)
972 {
973         switch (function) {
974         case ACPI_SIGNAL_FATAL:
975                 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
976                 break;
977         case ACPI_SIGNAL_BREAKPOINT:
978                 /*
979                  * AML Breakpoint
980                  * ACPI spec. says to treat it as a NOP unless
981                  * you are debugging.  So if/when we integrate
982                  * AML debugger into the kernel debugger its
983                  * hook will go here.  But until then it is
984                  * not useful to print anything on breakpoints.
985                  */
986                 break;
987         default:
988                 break;
989         }
990
991         return AE_OK;
992 }
993
994 EXPORT_SYMBOL(acpi_os_signal);
995
996 static int __init acpi_os_name_setup(char *str)
997 {
998         char *p = acpi_os_name;
999         int count = ACPI_MAX_OVERRIDE_LEN - 1;
1000
1001         if (!str || !*str)
1002                 return 0;
1003
1004         for (; count-- && str && *str; str++) {
1005                 if (isalnum(*str) || *str == ' ' || *str == ':')
1006                         *p++ = *str;
1007                 else if (*str == '\'' || *str == '"')
1008                         continue;
1009                 else
1010                         break;
1011         }
1012         *p = 0;
1013
1014         return 1;
1015
1016 }
1017
1018 __setup("acpi_os_name=", acpi_os_name_setup);
1019
1020 static void __init set_osi_linux(unsigned int enable)
1021 {
1022         if (osi_linux.enable != enable) {
1023                 osi_linux.enable = enable;
1024                 printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
1025                         enable ? "Add": "Delet");
1026         }
1027         return;
1028 }
1029
1030 static void __init acpi_cmdline_osi_linux(unsigned int enable)
1031 {
1032         osi_linux.cmdline = 1;  /* cmdline set the default */
1033         set_osi_linux(enable);
1034
1035         return;
1036 }
1037
1038 void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1039 {
1040         osi_linux.dmi = 1;      /* DMI knows that this box asks OSI(Linux) */
1041
1042         printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1043
1044         if (enable == -1)
1045                 return;
1046
1047         osi_linux.known = 1;    /* DMI knows which OSI(Linux) default needed */
1048
1049         set_osi_linux(enable);
1050
1051         return;
1052 }
1053
1054 /*
1055  * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1056  *
1057  * empty string disables _OSI
1058  * string starting with '!' disables that string
1059  * otherwise string is added to list, augmenting built-in strings
1060  */
1061 static int __init acpi_osi_setup(char *str)
1062 {
1063         if (str == NULL || *str == '\0') {
1064                 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1065                 acpi_gbl_create_osi_method = FALSE;
1066         } else if (!strcmp("!Linux", str)) {
1067                 acpi_cmdline_osi_linux(0);      /* !enable */
1068         } else if (*str == '!') {
1069                 if (acpi_osi_invalidate(++str) == AE_OK)
1070                         printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1071         } else if (!strcmp("Linux", str)) {
1072                 acpi_cmdline_osi_linux(1);      /* enable */
1073         } else if (*osi_additional_string == '\0') {
1074                 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
1075                 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1076         }
1077
1078         return 1;
1079 }
1080
1081 __setup("acpi_osi=", acpi_osi_setup);
1082
1083 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1084 static int __init acpi_serialize_setup(char *str)
1085 {
1086         printk(KERN_INFO PREFIX "serialize enabled\n");
1087
1088         acpi_gbl_all_methods_serialized = TRUE;
1089
1090         return 1;
1091 }
1092
1093 __setup("acpi_serialize", acpi_serialize_setup);
1094
1095 /*
1096  * Wake and Run-Time GPES are expected to be separate.
1097  * We disable wake-GPEs at run-time to prevent spurious
1098  * interrupts.
1099  *
1100  * However, if a system exists that shares Wake and
1101  * Run-time events on the same GPE this flag is available
1102  * to tell Linux to keep the wake-time GPEs enabled at run-time.
1103  */
1104 static int __init acpi_wake_gpes_always_on_setup(char *str)
1105 {
1106         printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
1107
1108         acpi_gbl_leave_wake_gpes_disabled = FALSE;
1109
1110         return 1;
1111 }
1112
1113 __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
1114
1115 /*
1116  * Acquire a spinlock.
1117  *
1118  * handle is a pointer to the spinlock_t.
1119  */
1120
1121 acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
1122 {
1123         acpi_cpu_flags flags;
1124         spin_lock_irqsave(lockp, flags);
1125         return flags;
1126 }
1127
1128 /*
1129  * Release a spinlock. See above.
1130  */
1131
1132 void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
1133 {
1134         spin_unlock_irqrestore(lockp, flags);
1135 }
1136
1137 #ifndef ACPI_USE_LOCAL_CACHE
1138
1139 /*******************************************************************************
1140  *
1141  * FUNCTION:    acpi_os_create_cache
1142  *
1143  * PARAMETERS:  name      - Ascii name for the cache
1144  *              size      - Size of each cached object
1145  *              depth     - Maximum depth of the cache (in objects) <ignored>
1146  *              cache     - Where the new cache object is returned
1147  *
1148  * RETURN:      status
1149  *
1150  * DESCRIPTION: Create a cache object
1151  *
1152  ******************************************************************************/
1153
1154 acpi_status
1155 acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
1156 {
1157         *cache = kmem_cache_create(name, size, 0, 0, NULL);
1158         if (*cache == NULL)
1159                 return AE_ERROR;
1160         else
1161                 return AE_OK;
1162 }
1163
1164 /*******************************************************************************
1165  *
1166  * FUNCTION:    acpi_os_purge_cache
1167  *
1168  * PARAMETERS:  Cache           - Handle to cache object
1169  *
1170  * RETURN:      Status
1171  *
1172  * DESCRIPTION: Free all objects within the requested cache.
1173  *
1174  ******************************************************************************/
1175
1176 acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
1177 {
1178         kmem_cache_shrink(cache);
1179         return (AE_OK);
1180 }
1181
1182 /*******************************************************************************
1183  *
1184  * FUNCTION:    acpi_os_delete_cache
1185  *
1186  * PARAMETERS:  Cache           - Handle to cache object
1187  *
1188  * RETURN:      Status
1189  *
1190  * DESCRIPTION: Free all objects within the requested cache and delete the
1191  *              cache object.
1192  *
1193  ******************************************************************************/
1194
1195 acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
1196 {
1197         kmem_cache_destroy(cache);
1198         return (AE_OK);
1199 }
1200
1201 /*******************************************************************************
1202  *
1203  * FUNCTION:    acpi_os_release_object
1204  *
1205  * PARAMETERS:  Cache       - Handle to cache object
1206  *              Object      - The object to be released
1207  *
1208  * RETURN:      None
1209  *
1210  * DESCRIPTION: Release an object to the specified cache.  If cache is full,
1211  *              the object is deleted.
1212  *
1213  ******************************************************************************/
1214
1215 acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
1216 {
1217         kmem_cache_free(cache, object);
1218         return (AE_OK);
1219 }
1220
1221 /**
1222  *      acpi_dmi_dump - dump DMI slots needed for blacklist entry
1223  *
1224  *      Returns 0 on success
1225  */
1226 int acpi_dmi_dump(void)
1227 {
1228
1229         if (!dmi_available)
1230                 return -1;
1231
1232         printk(KERN_NOTICE PREFIX "DMI System Vendor: %s\n",
1233                 dmi_get_slot(DMI_SYS_VENDOR));
1234         printk(KERN_NOTICE PREFIX "DMI Product Name: %s\n",
1235                 dmi_get_slot(DMI_PRODUCT_NAME));
1236         printk(KERN_NOTICE PREFIX "DMI Product Version: %s\n",
1237                 dmi_get_slot(DMI_PRODUCT_VERSION));
1238         printk(KERN_NOTICE PREFIX "DMI Board Name: %s\n",
1239                 dmi_get_slot(DMI_BOARD_NAME));
1240         printk(KERN_NOTICE PREFIX "DMI BIOS Vendor: %s\n",
1241                 dmi_get_slot(DMI_BIOS_VENDOR));
1242         printk(KERN_NOTICE PREFIX "DMI BIOS Date: %s\n",
1243                 dmi_get_slot(DMI_BIOS_DATE));
1244
1245         return 0;
1246 }
1247
1248
1249 /******************************************************************************
1250  *
1251  * FUNCTION:    acpi_os_validate_interface
1252  *
1253  * PARAMETERS:  interface           - Requested interface to be validated
1254  *
1255  * RETURN:      AE_OK if interface is supported, AE_SUPPORT otherwise
1256  *
1257  * DESCRIPTION: Match an interface string to the interfaces supported by the
1258  *              host. Strings originate from an AML call to the _OSI method.
1259  *
1260  *****************************************************************************/
1261
1262 acpi_status
1263 acpi_os_validate_interface (char *interface)
1264 {
1265         if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1266                 return AE_OK;
1267         if (!strcmp("Linux", interface)) {
1268
1269                 printk(KERN_NOTICE PREFIX
1270                         "BIOS _OSI(Linux) query %s%s\n",
1271                         osi_linux.enable ? "honored" : "ignored",
1272                         osi_linux.cmdline ? " via cmdline" :
1273                         osi_linux.dmi ? " via DMI" : "");
1274
1275                 if (!osi_linux.dmi) {
1276                         if (acpi_dmi_dump())
1277                                 printk(KERN_NOTICE PREFIX
1278                                         "[please extract dmidecode output]\n");
1279                         printk(KERN_NOTICE PREFIX
1280                                 "Please send DMI info above to "
1281                                 "linux-acpi@vger.kernel.org\n");
1282                 }
1283                 if (!osi_linux.known && !osi_linux.cmdline) {
1284                         printk(KERN_NOTICE PREFIX
1285                                 "If \"acpi_osi=%sLinux\" works better, "
1286                                 "please notify linux-acpi@vger.kernel.org\n",
1287                                 osi_linux.enable ? "!" : "");
1288                 }
1289
1290                 if (osi_linux.enable)
1291                         return AE_OK;
1292         }
1293         return AE_SUPPORT;
1294 }
1295
1296 /******************************************************************************
1297  *
1298  * FUNCTION:    acpi_os_validate_address
1299  *
1300  * PARAMETERS:  space_id             - ACPI space ID
1301  *              address             - Physical address
1302  *              length              - Address length
1303  *
1304  * RETURN:      AE_OK if address/length is valid for the space_id. Otherwise,
1305  *              should return AE_AML_ILLEGAL_ADDRESS.
1306  *
1307  * DESCRIPTION: Validate a system address via the host OS. Used to validate
1308  *              the addresses accessed by AML operation regions.
1309  *
1310  *****************************************************************************/
1311
1312 acpi_status
1313 acpi_os_validate_address (
1314     u8                   space_id,
1315     acpi_physical_address   address,
1316     acpi_size               length)
1317 {
1318
1319     return AE_OK;
1320 }
1321
1322 #endif