Merge branches 'release', 'misc' and 'misc-2.6.25' into release
[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 static 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                 acpi_physical_address pa = 0;
255
256                 acpi_find_root_pointer(&pa);
257                 return pa;
258         }
259 }
260
261 void __iomem *__init_refok
262 acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
263 {
264         if (phys > ULONG_MAX) {
265                 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
266                 return NULL;
267         }
268         if (acpi_gbl_permanent_mmap)
269                 /*
270                 * ioremap checks to ensure this is in reserved space
271                 */
272                 return ioremap((unsigned long)phys, size);
273         else
274                 return __acpi_map_table((unsigned long)phys, size);
275 }
276 EXPORT_SYMBOL_GPL(acpi_os_map_memory);
277
278 void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
279 {
280         if (acpi_gbl_permanent_mmap) {
281                 iounmap(virt);
282         }
283 }
284 EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
285
286 #ifdef ACPI_FUTURE_USAGE
287 acpi_status
288 acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
289 {
290         if (!phys || !virt)
291                 return AE_BAD_PARAMETER;
292
293         *phys = virt_to_phys(virt);
294
295         return AE_OK;
296 }
297 #endif
298
299 #define ACPI_MAX_OVERRIDE_LEN 100
300
301 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
302
303 acpi_status
304 acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
305                             acpi_string * new_val)
306 {
307         if (!init_val || !new_val)
308                 return AE_BAD_PARAMETER;
309
310         *new_val = NULL;
311         if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
312                 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
313                        acpi_os_name);
314                 *new_val = acpi_os_name;
315         }
316
317         return AE_OK;
318 }
319
320 acpi_status
321 acpi_os_table_override(struct acpi_table_header * existing_table,
322                        struct acpi_table_header ** new_table)
323 {
324         if (!existing_table || !new_table)
325                 return AE_BAD_PARAMETER;
326
327 #ifdef CONFIG_ACPI_CUSTOM_DSDT
328         if (strncmp(existing_table->signature, "DSDT", 4) == 0)
329                 *new_table = (struct acpi_table_header *)AmlCode;
330         else
331                 *new_table = NULL;
332 #else
333         *new_table = NULL;
334 #endif
335         return AE_OK;
336 }
337
338 static irqreturn_t acpi_irq(int irq, void *dev_id)
339 {
340         return (*acpi_irq_handler) (acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
341 }
342
343 acpi_status
344 acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
345                                   void *context)
346 {
347         unsigned int irq;
348
349         /*
350          * Ignore the GSI from the core, and use the value in our copy of the
351          * FADT. It may not be the same if an interrupt source override exists
352          * for the SCI.
353          */
354         gsi = acpi_gbl_FADT.sci_interrupt;
355         if (acpi_gsi_to_irq(gsi, &irq) < 0) {
356                 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
357                        gsi);
358                 return AE_OK;
359         }
360
361         acpi_irq_handler = handler;
362         acpi_irq_context = context;
363         if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
364                 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
365                 return AE_NOT_ACQUIRED;
366         }
367         acpi_irq_irq = irq;
368
369         return AE_OK;
370 }
371
372 acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
373 {
374         if (irq) {
375                 free_irq(irq, acpi_irq);
376                 acpi_irq_handler = NULL;
377                 acpi_irq_irq = 0;
378         }
379
380         return AE_OK;
381 }
382
383 /*
384  * Running in interpreter thread context, safe to sleep
385  */
386
387 void acpi_os_sleep(acpi_integer ms)
388 {
389         schedule_timeout_interruptible(msecs_to_jiffies(ms));
390 }
391
392 EXPORT_SYMBOL(acpi_os_sleep);
393
394 void acpi_os_stall(u32 us)
395 {
396         while (us) {
397                 u32 delay = 1000;
398
399                 if (delay > us)
400                         delay = us;
401                 udelay(delay);
402                 touch_nmi_watchdog();
403                 us -= delay;
404         }
405 }
406
407 EXPORT_SYMBOL(acpi_os_stall);
408
409 /*
410  * Support ACPI 3.0 AML Timer operand
411  * Returns 64-bit free-running, monotonically increasing timer
412  * with 100ns granularity
413  */
414 u64 acpi_os_get_timer(void)
415 {
416         static u64 t;
417
418 #ifdef  CONFIG_HPET
419         /* TBD: use HPET if available */
420 #endif
421
422 #ifdef  CONFIG_X86_PM_TIMER
423         /* TBD: default to PM timer if HPET was not available */
424 #endif
425         if (!t)
426                 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
427
428         return ++t;
429 }
430
431 acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
432 {
433         u32 dummy;
434
435         if (!value)
436                 value = &dummy;
437
438         *value = 0;
439         if (width <= 8) {
440                 *(u8 *) value = inb(port);
441         } else if (width <= 16) {
442                 *(u16 *) value = inw(port);
443         } else if (width <= 32) {
444                 *(u32 *) value = inl(port);
445         } else {
446                 BUG();
447         }
448
449         return AE_OK;
450 }
451
452 EXPORT_SYMBOL(acpi_os_read_port);
453
454 acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
455 {
456         if (width <= 8) {
457                 outb(value, port);
458         } else if (width <= 16) {
459                 outw(value, port);
460         } else if (width <= 32) {
461                 outl(value, port);
462         } else {
463                 BUG();
464         }
465
466         return AE_OK;
467 }
468
469 EXPORT_SYMBOL(acpi_os_write_port);
470
471 acpi_status
472 acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
473 {
474         u32 dummy;
475         void __iomem *virt_addr;
476
477         virt_addr = ioremap(phys_addr, width);
478         if (!value)
479                 value = &dummy;
480
481         switch (width) {
482         case 8:
483                 *(u8 *) value = readb(virt_addr);
484                 break;
485         case 16:
486                 *(u16 *) value = readw(virt_addr);
487                 break;
488         case 32:
489                 *(u32 *) value = readl(virt_addr);
490                 break;
491         default:
492                 BUG();
493         }
494
495         iounmap(virt_addr);
496
497         return AE_OK;
498 }
499
500 acpi_status
501 acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
502 {
503         void __iomem *virt_addr;
504
505         virt_addr = ioremap(phys_addr, width);
506
507         switch (width) {
508         case 8:
509                 writeb(value, virt_addr);
510                 break;
511         case 16:
512                 writew(value, virt_addr);
513                 break;
514         case 32:
515                 writel(value, virt_addr);
516                 break;
517         default:
518                 BUG();
519         }
520
521         iounmap(virt_addr);
522
523         return AE_OK;
524 }
525
526 acpi_status
527 acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
528                                void *value, u32 width)
529 {
530         int result, size;
531
532         if (!value)
533                 return AE_BAD_PARAMETER;
534
535         switch (width) {
536         case 8:
537                 size = 1;
538                 break;
539         case 16:
540                 size = 2;
541                 break;
542         case 32:
543                 size = 4;
544                 break;
545         default:
546                 return AE_ERROR;
547         }
548
549         BUG_ON(!raw_pci_ops);
550
551         result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
552                                    PCI_DEVFN(pci_id->device, pci_id->function),
553                                    reg, size, value);
554
555         return (result ? AE_ERROR : AE_OK);
556 }
557
558 EXPORT_SYMBOL(acpi_os_read_pci_configuration);
559
560 acpi_status
561 acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
562                                 acpi_integer value, u32 width)
563 {
564         int result, size;
565
566         switch (width) {
567         case 8:
568                 size = 1;
569                 break;
570         case 16:
571                 size = 2;
572                 break;
573         case 32:
574                 size = 4;
575                 break;
576         default:
577                 return AE_ERROR;
578         }
579
580         BUG_ON(!raw_pci_ops);
581
582         result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
583                                     PCI_DEVFN(pci_id->device, pci_id->function),
584                                     reg, size, value);
585
586         return (result ? AE_ERROR : AE_OK);
587 }
588
589 /* TODO: Change code to take advantage of driver model more */
590 static void acpi_os_derive_pci_id_2(acpi_handle rhandle,        /* upper bound  */
591                                     acpi_handle chandle,        /* current node */
592                                     struct acpi_pci_id **id,
593                                     int *is_bridge, u8 * bus_number)
594 {
595         acpi_handle handle;
596         struct acpi_pci_id *pci_id = *id;
597         acpi_status status;
598         unsigned long temp;
599         acpi_object_type type;
600         u8 tu8;
601
602         acpi_get_parent(chandle, &handle);
603         if (handle != rhandle) {
604                 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
605                                         bus_number);
606
607                 status = acpi_get_type(handle, &type);
608                 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
609                         return;
610
611                 status =
612                     acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
613                                           &temp);
614                 if (ACPI_SUCCESS(status)) {
615                         pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
616                         pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
617
618                         if (*is_bridge)
619                                 pci_id->bus = *bus_number;
620
621                         /* any nicer way to get bus number of bridge ? */
622                         status =
623                             acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
624                                                            8);
625                         if (ACPI_SUCCESS(status)
626                             && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
627                                 status =
628                                     acpi_os_read_pci_configuration(pci_id, 0x18,
629                                                                    &tu8, 8);
630                                 if (!ACPI_SUCCESS(status)) {
631                                         /* Certainly broken...  FIX ME */
632                                         return;
633                                 }
634                                 *is_bridge = 1;
635                                 pci_id->bus = tu8;
636                                 status =
637                                     acpi_os_read_pci_configuration(pci_id, 0x19,
638                                                                    &tu8, 8);
639                                 if (ACPI_SUCCESS(status)) {
640                                         *bus_number = tu8;
641                                 }
642                         } else
643                                 *is_bridge = 0;
644                 }
645         }
646 }
647
648 void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound  */
649                            acpi_handle chandle, /* current node */
650                            struct acpi_pci_id **id)
651 {
652         int is_bridge = 1;
653         u8 bus_number = (*id)->bus;
654
655         acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
656 }
657
658 static void acpi_os_execute_deferred(struct work_struct *work)
659 {
660         struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
661         if (!dpc) {
662                 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
663                 return;
664         }
665
666         dpc->function(dpc->context);
667         kfree(dpc);
668
669         return;
670 }
671
672 /*******************************************************************************
673  *
674  * FUNCTION:    acpi_os_execute
675  *
676  * PARAMETERS:  Type               - Type of the callback
677  *              Function           - Function to be executed
678  *              Context            - Function parameters
679  *
680  * RETURN:      Status
681  *
682  * DESCRIPTION: Depending on type, either queues function for deferred execution or
683  *              immediately executes function on a separate thread.
684  *
685  ******************************************************************************/
686
687 acpi_status acpi_os_execute(acpi_execute_type type,
688                             acpi_osd_exec_callback function, void *context)
689 {
690         acpi_status status = AE_OK;
691         struct acpi_os_dpc *dpc;
692         struct workqueue_struct *queue;
693         ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
694                           "Scheduling function [%p(%p)] for deferred execution.\n",
695                           function, context));
696
697         if (!function)
698                 return AE_BAD_PARAMETER;
699
700         /*
701          * Allocate/initialize DPC structure.  Note that this memory will be
702          * freed by the callee.  The kernel handles the work_struct list  in a
703          * way that allows us to also free its memory inside the callee.
704          * Because we may want to schedule several tasks with different
705          * parameters we can't use the approach some kernel code uses of
706          * having a static work_struct.
707          */
708
709         dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
710         if (!dpc)
711                 return_ACPI_STATUS(AE_NO_MEMORY);
712
713         dpc->function = function;
714         dpc->context = context;
715
716         INIT_WORK(&dpc->work, acpi_os_execute_deferred);
717         queue = (type == OSL_NOTIFY_HANDLER) ? kacpi_notify_wq : kacpid_wq;
718         if (!queue_work(queue, &dpc->work)) {
719                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
720                           "Call to queue_work() failed.\n"));
721                 status = AE_ERROR;
722                 kfree(dpc);
723         }
724         return_ACPI_STATUS(status);
725 }
726
727 EXPORT_SYMBOL(acpi_os_execute);
728
729 void acpi_os_wait_events_complete(void *context)
730 {
731         flush_workqueue(kacpid_wq);
732 }
733
734 EXPORT_SYMBOL(acpi_os_wait_events_complete);
735
736 /*
737  * Allocate the memory for a spinlock and initialize it.
738  */
739 acpi_status acpi_os_create_lock(acpi_spinlock * handle)
740 {
741         spin_lock_init(*handle);
742
743         return AE_OK;
744 }
745
746 /*
747  * Deallocate the memory for a spinlock.
748  */
749 void acpi_os_delete_lock(acpi_spinlock handle)
750 {
751         return;
752 }
753
754 acpi_status
755 acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
756 {
757         struct semaphore *sem = NULL;
758
759
760         sem = acpi_os_allocate(sizeof(struct semaphore));
761         if (!sem)
762                 return AE_NO_MEMORY;
763         memset(sem, 0, sizeof(struct semaphore));
764
765         sema_init(sem, initial_units);
766
767         *handle = (acpi_handle *) sem;
768
769         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
770                           *handle, initial_units));
771
772         return AE_OK;
773 }
774
775 EXPORT_SYMBOL(acpi_os_create_semaphore);
776
777 /*
778  * TODO: A better way to delete semaphores?  Linux doesn't have a
779  * 'delete_semaphore()' function -- may result in an invalid
780  * pointer dereference for non-synchronized consumers.  Should
781  * we at least check for blocked threads and signal/cancel them?
782  */
783
784 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
785 {
786         struct semaphore *sem = (struct semaphore *)handle;
787
788
789         if (!sem)
790                 return AE_BAD_PARAMETER;
791
792         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
793
794         kfree(sem);
795         sem = NULL;
796
797         return AE_OK;
798 }
799
800 EXPORT_SYMBOL(acpi_os_delete_semaphore);
801
802 /*
803  * TODO: The kernel doesn't have a 'down_timeout' function -- had to
804  * improvise.  The process is to sleep for one scheduler quantum
805  * until the semaphore becomes available.  Downside is that this
806  * may result in starvation for timeout-based waits when there's
807  * lots of semaphore activity.
808  *
809  * TODO: Support for units > 1?
810  */
811 acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
812 {
813         acpi_status status = AE_OK;
814         struct semaphore *sem = (struct semaphore *)handle;
815         int ret = 0;
816
817
818         if (!sem || (units < 1))
819                 return AE_BAD_PARAMETER;
820
821         if (units > 1)
822                 return AE_SUPPORT;
823
824         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
825                           handle, units, timeout));
826
827         /*
828          * This can be called during resume with interrupts off.
829          * Like boot-time, we should be single threaded and will
830          * always get the lock if we try -- timeout or not.
831          * If this doesn't succeed, then we will oops courtesy of
832          * might_sleep() in down().
833          */
834         if (!down_trylock(sem))
835                 return AE_OK;
836
837         switch (timeout) {
838                 /*
839                  * No Wait:
840                  * --------
841                  * A zero timeout value indicates that we shouldn't wait - just
842                  * acquire the semaphore if available otherwise return AE_TIME
843                  * (a.k.a. 'would block').
844                  */
845         case 0:
846                 if (down_trylock(sem))
847                         status = AE_TIME;
848                 break;
849
850                 /*
851                  * Wait Indefinitely:
852                  * ------------------
853                  */
854         case ACPI_WAIT_FOREVER:
855                 down(sem);
856                 break;
857
858                 /*
859                  * Wait w/ Timeout:
860                  * ----------------
861                  */
862         default:
863                 // TODO: A better timeout algorithm?
864                 {
865                         int i = 0;
866                         static const int quantum_ms = 1000 / HZ;
867
868                         ret = down_trylock(sem);
869                         for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
870                                 schedule_timeout_interruptible(1);
871                                 ret = down_trylock(sem);
872                         }
873
874                         if (ret != 0)
875                                 status = AE_TIME;
876                 }
877                 break;
878         }
879
880         if (ACPI_FAILURE(status)) {
881                 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
882                                   "Failed to acquire semaphore[%p|%d|%d], %s",
883                                   handle, units, timeout,
884                                   acpi_format_exception(status)));
885         } else {
886                 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
887                                   "Acquired semaphore[%p|%d|%d]", handle,
888                                   units, timeout));
889         }
890
891         return status;
892 }
893
894 EXPORT_SYMBOL(acpi_os_wait_semaphore);
895
896 /*
897  * TODO: Support for units > 1?
898  */
899 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
900 {
901         struct semaphore *sem = (struct semaphore *)handle;
902
903
904         if (!sem || (units < 1))
905                 return AE_BAD_PARAMETER;
906
907         if (units > 1)
908                 return AE_SUPPORT;
909
910         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
911                           units));
912
913         up(sem);
914
915         return AE_OK;
916 }
917
918 EXPORT_SYMBOL(acpi_os_signal_semaphore);
919
920 #ifdef ACPI_FUTURE_USAGE
921 u32 acpi_os_get_line(char *buffer)
922 {
923
924 #ifdef ENABLE_DEBUGGER
925         if (acpi_in_debugger) {
926                 u32 chars;
927
928                 kdb_read(buffer, sizeof(line_buf));
929
930                 /* remove the CR kdb includes */
931                 chars = strlen(buffer) - 1;
932                 buffer[chars] = '\0';
933         }
934 #endif
935
936         return 0;
937 }
938 #endif                          /*  ACPI_FUTURE_USAGE  */
939
940 acpi_status acpi_os_signal(u32 function, void *info)
941 {
942         switch (function) {
943         case ACPI_SIGNAL_FATAL:
944                 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
945                 break;
946         case ACPI_SIGNAL_BREAKPOINT:
947                 /*
948                  * AML Breakpoint
949                  * ACPI spec. says to treat it as a NOP unless
950                  * you are debugging.  So if/when we integrate
951                  * AML debugger into the kernel debugger its
952                  * hook will go here.  But until then it is
953                  * not useful to print anything on breakpoints.
954                  */
955                 break;
956         default:
957                 break;
958         }
959
960         return AE_OK;
961 }
962
963 EXPORT_SYMBOL(acpi_os_signal);
964
965 static int __init acpi_os_name_setup(char *str)
966 {
967         char *p = acpi_os_name;
968         int count = ACPI_MAX_OVERRIDE_LEN - 1;
969
970         if (!str || !*str)
971                 return 0;
972
973         for (; count-- && str && *str; str++) {
974                 if (isalnum(*str) || *str == ' ' || *str == ':')
975                         *p++ = *str;
976                 else if (*str == '\'' || *str == '"')
977                         continue;
978                 else
979                         break;
980         }
981         *p = 0;
982
983         return 1;
984
985 }
986
987 __setup("acpi_os_name=", acpi_os_name_setup);
988
989 static void __init set_osi_linux(unsigned int enable)
990 {
991         if (osi_linux.enable != enable) {
992                 osi_linux.enable = enable;
993                 printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
994                         enable ? "Add": "Delet");
995         }
996         return;
997 }
998
999 static void __init acpi_cmdline_osi_linux(unsigned int enable)
1000 {
1001         osi_linux.cmdline = 1;  /* cmdline set the default */
1002         set_osi_linux(enable);
1003
1004         return;
1005 }
1006
1007 void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1008 {
1009         osi_linux.dmi = 1;      /* DMI knows that this box asks OSI(Linux) */
1010
1011         printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1012
1013         if (enable == -1)
1014                 return;
1015
1016         osi_linux.known = 1;    /* DMI knows which OSI(Linux) default needed */
1017
1018         set_osi_linux(enable);
1019
1020         return;
1021 }
1022
1023 /*
1024  * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1025  *
1026  * empty string disables _OSI
1027  * string starting with '!' disables that string
1028  * otherwise string is added to list, augmenting built-in strings
1029  */
1030 static int __init acpi_osi_setup(char *str)
1031 {
1032         if (str == NULL || *str == '\0') {
1033                 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1034                 acpi_gbl_create_osi_method = FALSE;
1035         } else if (!strcmp("!Linux", str)) {
1036                 acpi_cmdline_osi_linux(0);      /* !enable */
1037         } else if (*str == '!') {
1038                 if (acpi_osi_invalidate(++str) == AE_OK)
1039                         printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1040         } else if (!strcmp("Linux", str)) {
1041                 acpi_cmdline_osi_linux(1);      /* enable */
1042         } else if (*osi_additional_string == '\0') {
1043                 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
1044                 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1045         }
1046
1047         return 1;
1048 }
1049
1050 __setup("acpi_osi=", acpi_osi_setup);
1051
1052 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1053 static int __init acpi_serialize_setup(char *str)
1054 {
1055         printk(KERN_INFO PREFIX "serialize enabled\n");
1056
1057         acpi_gbl_all_methods_serialized = TRUE;
1058
1059         return 1;
1060 }
1061
1062 __setup("acpi_serialize", acpi_serialize_setup);
1063
1064 /*
1065  * Wake and Run-Time GPES are expected to be separate.
1066  * We disable wake-GPEs at run-time to prevent spurious
1067  * interrupts.
1068  *
1069  * However, if a system exists that shares Wake and
1070  * Run-time events on the same GPE this flag is available
1071  * to tell Linux to keep the wake-time GPEs enabled at run-time.
1072  */
1073 static int __init acpi_wake_gpes_always_on_setup(char *str)
1074 {
1075         printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
1076
1077         acpi_gbl_leave_wake_gpes_disabled = FALSE;
1078
1079         return 1;
1080 }
1081
1082 __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
1083
1084 /*
1085  * Acquire a spinlock.
1086  *
1087  * handle is a pointer to the spinlock_t.
1088  */
1089
1090 acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
1091 {
1092         acpi_cpu_flags flags;
1093         spin_lock_irqsave(lockp, flags);
1094         return flags;
1095 }
1096
1097 /*
1098  * Release a spinlock. See above.
1099  */
1100
1101 void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
1102 {
1103         spin_unlock_irqrestore(lockp, flags);
1104 }
1105
1106 #ifndef ACPI_USE_LOCAL_CACHE
1107
1108 /*******************************************************************************
1109  *
1110  * FUNCTION:    acpi_os_create_cache
1111  *
1112  * PARAMETERS:  name      - Ascii name for the cache
1113  *              size      - Size of each cached object
1114  *              depth     - Maximum depth of the cache (in objects) <ignored>
1115  *              cache     - Where the new cache object is returned
1116  *
1117  * RETURN:      status
1118  *
1119  * DESCRIPTION: Create a cache object
1120  *
1121  ******************************************************************************/
1122
1123 acpi_status
1124 acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
1125 {
1126         *cache = kmem_cache_create(name, size, 0, 0, NULL);
1127         if (*cache == NULL)
1128                 return AE_ERROR;
1129         else
1130                 return AE_OK;
1131 }
1132
1133 /*******************************************************************************
1134  *
1135  * FUNCTION:    acpi_os_purge_cache
1136  *
1137  * PARAMETERS:  Cache           - Handle to cache object
1138  *
1139  * RETURN:      Status
1140  *
1141  * DESCRIPTION: Free all objects within the requested cache.
1142  *
1143  ******************************************************************************/
1144
1145 acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
1146 {
1147         kmem_cache_shrink(cache);
1148         return (AE_OK);
1149 }
1150
1151 /*******************************************************************************
1152  *
1153  * FUNCTION:    acpi_os_delete_cache
1154  *
1155  * PARAMETERS:  Cache           - Handle to cache object
1156  *
1157  * RETURN:      Status
1158  *
1159  * DESCRIPTION: Free all objects within the requested cache and delete the
1160  *              cache object.
1161  *
1162  ******************************************************************************/
1163
1164 acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
1165 {
1166         kmem_cache_destroy(cache);
1167         return (AE_OK);
1168 }
1169
1170 /*******************************************************************************
1171  *
1172  * FUNCTION:    acpi_os_release_object
1173  *
1174  * PARAMETERS:  Cache       - Handle to cache object
1175  *              Object      - The object to be released
1176  *
1177  * RETURN:      None
1178  *
1179  * DESCRIPTION: Release an object to the specified cache.  If cache is full,
1180  *              the object is deleted.
1181  *
1182  ******************************************************************************/
1183
1184 acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
1185 {
1186         kmem_cache_free(cache, object);
1187         return (AE_OK);
1188 }
1189
1190 /**
1191  *      acpi_dmi_dump - dump DMI slots needed for blacklist entry
1192  *
1193  *      Returns 0 on success
1194  */
1195 static int acpi_dmi_dump(void)
1196 {
1197
1198         if (!dmi_available)
1199                 return -1;
1200
1201         printk(KERN_NOTICE PREFIX "DMI System Vendor: %s\n",
1202                 dmi_get_system_info(DMI_SYS_VENDOR));
1203         printk(KERN_NOTICE PREFIX "DMI Product Name: %s\n",
1204                 dmi_get_system_info(DMI_PRODUCT_NAME));
1205         printk(KERN_NOTICE PREFIX "DMI Product Version: %s\n",
1206                 dmi_get_system_info(DMI_PRODUCT_VERSION));
1207         printk(KERN_NOTICE PREFIX "DMI Board Name: %s\n",
1208                 dmi_get_system_info(DMI_BOARD_NAME));
1209         printk(KERN_NOTICE PREFIX "DMI BIOS Vendor: %s\n",
1210                 dmi_get_system_info(DMI_BIOS_VENDOR));
1211         printk(KERN_NOTICE PREFIX "DMI BIOS Date: %s\n",
1212                 dmi_get_system_info(DMI_BIOS_DATE));
1213
1214         return 0;
1215 }
1216
1217
1218 /******************************************************************************
1219  *
1220  * FUNCTION:    acpi_os_validate_interface
1221  *
1222  * PARAMETERS:  interface           - Requested interface to be validated
1223  *
1224  * RETURN:      AE_OK if interface is supported, AE_SUPPORT otherwise
1225  *
1226  * DESCRIPTION: Match an interface string to the interfaces supported by the
1227  *              host. Strings originate from an AML call to the _OSI method.
1228  *
1229  *****************************************************************************/
1230
1231 acpi_status
1232 acpi_os_validate_interface (char *interface)
1233 {
1234         if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1235                 return AE_OK;
1236         if (!strcmp("Linux", interface)) {
1237
1238                 printk(KERN_NOTICE PREFIX
1239                         "BIOS _OSI(Linux) query %s%s\n",
1240                         osi_linux.enable ? "honored" : "ignored",
1241                         osi_linux.cmdline ? " via cmdline" :
1242                         osi_linux.dmi ? " via DMI" : "");
1243
1244                 if (!osi_linux.dmi) {
1245                         if (acpi_dmi_dump())
1246                                 printk(KERN_NOTICE PREFIX
1247                                         "[please extract dmidecode output]\n");
1248                         printk(KERN_NOTICE PREFIX
1249                                 "Please send DMI info above to "
1250                                 "linux-acpi@vger.kernel.org\n");
1251                 }
1252                 if (!osi_linux.known && !osi_linux.cmdline) {
1253                         printk(KERN_NOTICE PREFIX
1254                                 "If \"acpi_osi=%sLinux\" works better, "
1255                                 "please notify linux-acpi@vger.kernel.org\n",
1256                                 osi_linux.enable ? "!" : "");
1257                 }
1258
1259                 if (osi_linux.enable)
1260                         return AE_OK;
1261         }
1262         return AE_SUPPORT;
1263 }
1264
1265 /******************************************************************************
1266  *
1267  * FUNCTION:    acpi_os_validate_address
1268  *
1269  * PARAMETERS:  space_id             - ACPI space ID
1270  *              address             - Physical address
1271  *              length              - Address length
1272  *
1273  * RETURN:      AE_OK if address/length is valid for the space_id. Otherwise,
1274  *              should return AE_AML_ILLEGAL_ADDRESS.
1275  *
1276  * DESCRIPTION: Validate a system address via the host OS. Used to validate
1277  *              the addresses accessed by AML operation regions.
1278  *
1279  *****************************************************************************/
1280
1281 acpi_status
1282 acpi_os_validate_address (
1283     u8                   space_id,
1284     acpi_physical_address   address,
1285     acpi_size               length)
1286 {
1287
1288     return AE_OK;
1289 }
1290
1291 #endif