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