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