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