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