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