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