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