MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / acpi / osl.c
bloba7c4fdfcbf8557e6116c5abf181f5c17b356551a
1 /*
2 * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
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>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>
44 #include <linux/efi.h>
47 #define _COMPONENT ACPI_OS_SERVICES
48 ACPI_MODULE_NAME ("osl")
50 #define PREFIX "ACPI: "
52 struct acpi_os_dpc
54 acpi_osd_exec_callback function;
55 void *context;
58 #ifdef CONFIG_ACPI_CUSTOM_DSDT
59 #include CONFIG_ACPI_CUSTOM_DSDT_FILE
60 #endif
62 #ifdef ENABLE_DEBUGGER
63 #include <linux/kdb.h>
64 /* stuff for debugger support */
65 int acpi_in_debugger;
66 extern char line_buf[80];
67 #endif /*ENABLE_DEBUGGER*/
69 static unsigned int acpi_irq_irq;
70 static acpi_osd_handler acpi_irq_handler;
71 static void *acpi_irq_context;
72 static struct workqueue_struct *kacpid_wq;
74 acpi_status
75 acpi_os_initialize(void)
77 return AE_OK;
80 acpi_status
81 acpi_os_initialize1(void)
84 * Initialize PCI configuration space access, as we'll need to access
85 * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
87 #ifdef CONFIG_ACPI_PCI
88 if (!raw_pci_ops) {
89 printk(KERN_ERR PREFIX "Access to PCI configuration space unavailable\n");
90 return AE_NULL_ENTRY;
92 #endif
93 kacpid_wq = create_singlethread_workqueue("kacpid");
94 BUG_ON(!kacpid_wq);
96 return AE_OK;
99 acpi_status
100 acpi_os_terminate(void)
102 if (acpi_irq_handler) {
103 acpi_os_remove_interrupt_handler(acpi_irq_irq,
104 acpi_irq_handler);
107 destroy_workqueue(kacpid_wq);
109 return AE_OK;
112 void
113 acpi_os_printf(const char *fmt,...)
115 va_list args;
116 va_start(args, fmt);
117 acpi_os_vprintf(fmt, args);
118 va_end(args);
121 void
122 acpi_os_vprintf(const char *fmt, va_list args)
124 static char buffer[512];
126 vsprintf(buffer, fmt, args);
128 #ifdef ENABLE_DEBUGGER
129 if (acpi_in_debugger) {
130 kdb_printf("%s", buffer);
131 } else {
132 printk("%s", buffer);
134 #else
135 printk("%s", buffer);
136 #endif
139 void *
140 acpi_os_allocate(acpi_size size)
142 return kmalloc(size, GFP_KERNEL);
145 void
146 acpi_os_free(void *ptr)
148 kfree(ptr);
151 acpi_status
152 acpi_os_get_root_pointer(u32 flags, struct acpi_pointer *addr)
154 if (efi_enabled) {
155 addr->pointer_type = ACPI_PHYSICAL_POINTER;
156 if (efi.acpi20)
157 addr->pointer.physical =
158 (acpi_physical_address) virt_to_phys(efi.acpi20);
159 else if (efi.acpi)
160 addr->pointer.physical =
161 (acpi_physical_address) virt_to_phys(efi.acpi);
162 else {
163 printk(KERN_ERR PREFIX "System description tables not found\n");
164 return AE_NOT_FOUND;
166 } else {
167 if (ACPI_FAILURE(acpi_find_root_pointer(flags, addr))) {
168 printk(KERN_ERR PREFIX "System description tables not found\n");
169 return AE_NOT_FOUND;
173 return AE_OK;
176 acpi_status
177 acpi_os_map_memory(acpi_physical_address phys, acpi_size size, void __iomem **virt)
179 if (efi_enabled) {
180 if (EFI_MEMORY_WB & efi_mem_attributes(phys)) {
181 *virt = (void __iomem *) phys_to_virt(phys);
182 } else {
183 *virt = ioremap(phys, size);
185 } else {
186 if (phys > ULONG_MAX) {
187 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
188 return AE_BAD_PARAMETER;
191 * ioremap checks to ensure this is in reserved space
193 *virt = ioremap((unsigned long) phys, size);
196 if (!*virt)
197 return AE_NO_MEMORY;
199 return AE_OK;
202 void
203 acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
205 iounmap(virt);
208 acpi_status
209 acpi_os_get_physical_address(void *virt, acpi_physical_address *phys)
211 if(!phys || !virt)
212 return AE_BAD_PARAMETER;
214 *phys = virt_to_phys(virt);
216 return AE_OK;
219 #define ACPI_MAX_OVERRIDE_LEN 100
221 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
223 acpi_status
224 acpi_os_predefined_override (const struct acpi_predefined_names *init_val,
225 acpi_string *new_val)
227 if (!init_val || !new_val)
228 return AE_BAD_PARAMETER;
230 *new_val = NULL;
231 if (!memcmp (init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
232 printk(KERN_INFO PREFIX "Overriding _OS definition %s\n",
233 acpi_os_name);
234 *new_val = acpi_os_name;
237 return AE_OK;
240 acpi_status
241 acpi_os_table_override (struct acpi_table_header *existing_table,
242 struct acpi_table_header **new_table)
244 if (!existing_table || !new_table)
245 return AE_BAD_PARAMETER;
247 #ifdef CONFIG_ACPI_CUSTOM_DSDT
248 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
249 *new_table = (struct acpi_table_header*)AmlCode;
250 else
251 *new_table = NULL;
252 #else
253 *new_table = NULL;
254 #endif
255 return AE_OK;
258 static irqreturn_t
259 acpi_irq(int irq, void *dev_id, struct pt_regs *regs)
261 return (*acpi_irq_handler)(acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
264 acpi_status
265 acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler, void *context)
267 unsigned int irq;
270 * Ignore the GSI from the core, and use the value in our copy of the
271 * FADT. It may not be the same if an interrupt source override exists
272 * for the SCI.
274 gsi = acpi_fadt.sci_int;
275 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
276 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
277 gsi);
278 return AE_OK;
281 acpi_irq_handler = handler;
282 acpi_irq_context = context;
283 if (request_irq(irq, acpi_irq, SA_SHIRQ, "acpi", acpi_irq)) {
284 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
285 return AE_NOT_ACQUIRED;
287 acpi_irq_irq = irq;
289 return AE_OK;
292 acpi_status
293 acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
295 if (irq) {
296 free_irq(irq, acpi_irq);
297 acpi_irq_handler = NULL;
298 acpi_irq_irq = 0;
301 return AE_OK;
305 * Running in interpreter thread context, safe to sleep
308 void
309 acpi_os_sleep(u32 sec, u32 ms)
311 current->state = TASK_INTERRUPTIBLE;
312 schedule_timeout(HZ * sec + (ms * HZ) / 1000);
315 void
316 acpi_os_stall(u32 us)
318 while (us) {
319 u32 delay = 1000;
321 if (delay > us)
322 delay = us;
323 udelay(delay);
324 touch_nmi_watchdog();
325 us -= delay;
329 acpi_status
330 acpi_os_read_port(
331 acpi_io_address port,
332 u32 *value,
333 u32 width)
335 u32 dummy;
337 if (!value)
338 value = &dummy;
340 switch (width)
342 case 8:
343 *(u8*) value = inb(port);
344 break;
345 case 16:
346 *(u16*) value = inw(port);
347 break;
348 case 32:
349 *(u32*) value = inl(port);
350 break;
351 default:
352 BUG();
355 return AE_OK;
358 acpi_status
359 acpi_os_write_port(
360 acpi_io_address port,
361 u32 value,
362 u32 width)
364 switch (width)
366 case 8:
367 outb(value, port);
368 break;
369 case 16:
370 outw(value, port);
371 break;
372 case 32:
373 outl(value, port);
374 break;
375 default:
376 BUG();
379 return AE_OK;
382 acpi_status
383 acpi_os_read_memory(
384 acpi_physical_address phys_addr,
385 u32 *value,
386 u32 width)
388 u32 dummy;
389 void __iomem *virt_addr;
390 int iomem = 0;
392 if (efi_enabled) {
393 if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
394 /* HACK ALERT! We can use readb/w/l on real memory too.. */
395 virt_addr = (void __iomem *) phys_to_virt(phys_addr);
396 } else {
397 iomem = 1;
398 virt_addr = ioremap(phys_addr, width);
400 } else
401 virt_addr = (void __iomem *) phys_to_virt(phys_addr);
402 if (!value)
403 value = &dummy;
405 switch (width) {
406 case 8:
407 *(u8*) value = readb(virt_addr);
408 break;
409 case 16:
410 *(u16*) value = readw(virt_addr);
411 break;
412 case 32:
413 *(u32*) value = readl(virt_addr);
414 break;
415 default:
416 BUG();
419 if (efi_enabled) {
420 if (iomem)
421 iounmap(virt_addr);
424 return AE_OK;
427 acpi_status
428 acpi_os_write_memory(
429 acpi_physical_address phys_addr,
430 u32 value,
431 u32 width)
433 void __iomem *virt_addr;
434 int iomem = 0;
436 if (efi_enabled) {
437 if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
438 /* HACK ALERT! We can use writeb/w/l on real memory too */
439 virt_addr = (void __iomem *) phys_to_virt(phys_addr);
440 } else {
441 iomem = 1;
442 virt_addr = ioremap(phys_addr, width);
444 } else
445 virt_addr = (void __iomem *) phys_to_virt(phys_addr);
447 switch (width) {
448 case 8:
449 writeb(value, virt_addr);
450 break;
451 case 16:
452 writew(value, virt_addr);
453 break;
454 case 32:
455 writel(value, virt_addr);
456 break;
457 default:
458 BUG();
461 if (iomem)
462 iounmap(virt_addr);
464 return AE_OK;
467 #ifdef CONFIG_ACPI_PCI
469 acpi_status
470 acpi_os_read_pci_configuration (struct acpi_pci_id *pci_id, u32 reg, void *value, u32 width)
472 int result, size;
474 if (!value)
475 return AE_BAD_PARAMETER;
477 switch (width) {
478 case 8:
479 size = 1;
480 break;
481 case 16:
482 size = 2;
483 break;
484 case 32:
485 size = 4;
486 break;
487 default:
488 return AE_ERROR;
491 BUG_ON(!raw_pci_ops);
493 result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
494 PCI_DEVFN(pci_id->device, pci_id->function),
495 reg, size, value);
497 return (result ? AE_ERROR : AE_OK);
500 acpi_status
501 acpi_os_write_pci_configuration (struct acpi_pci_id *pci_id, u32 reg, acpi_integer value, u32 width)
503 int result, size;
505 switch (width) {
506 case 8:
507 size = 1;
508 break;
509 case 16:
510 size = 2;
511 break;
512 case 32:
513 size = 4;
514 break;
515 default:
516 return AE_ERROR;
519 BUG_ON(!raw_pci_ops);
521 result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
522 PCI_DEVFN(pci_id->device, pci_id->function),
523 reg, size, value);
525 return (result ? AE_ERROR : AE_OK);
528 /* TODO: Change code to take advantage of driver model more */
529 void
530 acpi_os_derive_pci_id_2 (
531 acpi_handle rhandle, /* upper bound */
532 acpi_handle chandle, /* current node */
533 struct acpi_pci_id **id,
534 int *is_bridge,
535 u8 *bus_number)
537 acpi_handle handle;
538 struct acpi_pci_id *pci_id = *id;
539 acpi_status status;
540 unsigned long temp;
541 acpi_object_type type;
542 u8 tu8;
544 acpi_get_parent(chandle, &handle);
545 if (handle != rhandle) {
546 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge, bus_number);
548 status = acpi_get_type(handle, &type);
549 if ( (ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE) )
550 return;
552 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &temp);
553 if (ACPI_SUCCESS(status)) {
554 pci_id->device = ACPI_HIWORD (ACPI_LODWORD (temp));
555 pci_id->function = ACPI_LOWORD (ACPI_LODWORD (temp));
557 if (*is_bridge)
558 pci_id->bus = *bus_number;
560 /* any nicer way to get bus number of bridge ? */
561 status = acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8, 8);
562 if (ACPI_SUCCESS(status) &&
563 ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
564 status = acpi_os_read_pci_configuration(pci_id, 0x18, &tu8, 8);
565 if (!ACPI_SUCCESS(status)) {
566 /* Certainly broken... FIX ME */
567 return;
569 *is_bridge = 1;
570 pci_id->bus = tu8;
571 status = acpi_os_read_pci_configuration(pci_id, 0x19, &tu8, 8);
572 if (ACPI_SUCCESS(status)) {
573 *bus_number = tu8;
575 } else
576 *is_bridge = 0;
581 void
582 acpi_os_derive_pci_id (
583 acpi_handle rhandle, /* upper bound */
584 acpi_handle chandle, /* current node */
585 struct acpi_pci_id **id)
587 int is_bridge = 1;
588 u8 bus_number = (*id)->bus;
590 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
593 #else /*!CONFIG_ACPI_PCI*/
595 acpi_status
596 acpi_os_write_pci_configuration (
597 struct acpi_pci_id *pci_id,
598 u32 reg,
599 acpi_integer value,
600 u32 width)
602 return (AE_SUPPORT);
605 acpi_status
606 acpi_os_read_pci_configuration (
607 struct acpi_pci_id *pci_id,
608 u32 reg,
609 void *value,
610 u32 width)
612 return (AE_SUPPORT);
615 void
616 acpi_os_derive_pci_id (
617 acpi_handle rhandle, /* upper bound */
618 acpi_handle chandle, /* current node */
619 struct acpi_pci_id **id)
623 #endif /*CONFIG_ACPI_PCI*/
625 static void
626 acpi_os_execute_deferred (
627 void *context)
629 struct acpi_os_dpc *dpc = NULL;
631 ACPI_FUNCTION_TRACE ("os_execute_deferred");
633 dpc = (struct acpi_os_dpc *) context;
634 if (!dpc) {
635 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
636 return_VOID;
639 dpc->function(dpc->context);
641 kfree(dpc);
643 return_VOID;
646 acpi_status
647 acpi_os_queue_for_execution(
648 u32 priority,
649 acpi_osd_exec_callback function,
650 void *context)
652 acpi_status status = AE_OK;
653 struct acpi_os_dpc *dpc;
654 struct work_struct *task;
656 ACPI_FUNCTION_TRACE ("os_queue_for_execution");
658 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Scheduling function [%p(%p)] for deferred execution.\n", function, context));
660 if (!function)
661 return_ACPI_STATUS (AE_BAD_PARAMETER);
664 * Allocate/initialize DPC structure. Note that this memory will be
665 * freed by the callee. The kernel handles the tq_struct list in a
666 * way that allows us to also free its memory inside the callee.
667 * Because we may want to schedule several tasks with different
668 * parameters we can't use the approach some kernel code uses of
669 * having a static tq_struct.
670 * We can save time and code by allocating the DPC and tq_structs
671 * from the same memory.
674 dpc = kmalloc(sizeof(struct acpi_os_dpc)+sizeof(struct work_struct), GFP_ATOMIC);
675 if (!dpc)
676 return_ACPI_STATUS (AE_NO_MEMORY);
678 dpc->function = function;
679 dpc->context = context;
681 task = (void *)(dpc+1);
682 INIT_WORK(task, acpi_os_execute_deferred, (void*)dpc);
684 if (!queue_work(kacpid_wq, task)) {
685 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Call to queue_work() failed.\n"));
686 kfree(dpc);
687 status = AE_ERROR;
690 return_ACPI_STATUS (status);
693 void
694 acpi_os_wait_events_complete(
695 void *context)
697 flush_workqueue(kacpid_wq);
701 * Allocate the memory for a spinlock and initialize it.
703 acpi_status
704 acpi_os_create_lock (
705 acpi_handle *out_handle)
707 spinlock_t *lock_ptr;
709 ACPI_FUNCTION_TRACE ("os_create_lock");
711 lock_ptr = acpi_os_allocate(sizeof(spinlock_t));
713 spin_lock_init(lock_ptr);
715 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Creating spinlock[%p].\n", lock_ptr));
717 *out_handle = lock_ptr;
719 return_ACPI_STATUS (AE_OK);
724 * Deallocate the memory for a spinlock.
726 void
727 acpi_os_delete_lock (
728 acpi_handle handle)
730 ACPI_FUNCTION_TRACE ("os_create_lock");
732 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Deleting spinlock[%p].\n", handle));
734 acpi_os_free(handle);
736 return_VOID;
740 * Acquire a spinlock.
742 * handle is a pointer to the spinlock_t.
743 * flags is *not* the result of save_flags - it is an ACPI-specific flag variable
744 * that indicates whether we are at interrupt level.
746 void
747 acpi_os_acquire_lock (
748 acpi_handle handle,
749 u32 flags)
751 ACPI_FUNCTION_TRACE ("os_acquire_lock");
753 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Acquiring spinlock[%p] from %s level\n", handle,
754 ((flags & ACPI_NOT_ISR) ? "non-interrupt" : "interrupt")));
756 if (flags & ACPI_NOT_ISR)
757 ACPI_DISABLE_IRQS();
759 spin_lock((spinlock_t *)handle);
761 return_VOID;
766 * Release a spinlock. See above.
768 void
769 acpi_os_release_lock (
770 acpi_handle handle,
771 u32 flags)
773 ACPI_FUNCTION_TRACE ("os_release_lock");
775 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Releasing spinlock[%p] from %s level\n", handle,
776 ((flags & ACPI_NOT_ISR) ? "non-interrupt" : "interrupt")));
778 spin_unlock((spinlock_t *)handle);
780 if (flags & ACPI_NOT_ISR)
781 ACPI_ENABLE_IRQS();
783 return_VOID;
787 acpi_status
788 acpi_os_create_semaphore(
789 u32 max_units,
790 u32 initial_units,
791 acpi_handle *handle)
793 struct semaphore *sem = NULL;
795 ACPI_FUNCTION_TRACE ("os_create_semaphore");
797 sem = acpi_os_allocate(sizeof(struct semaphore));
798 if (!sem)
799 return_ACPI_STATUS (AE_NO_MEMORY);
800 memset(sem, 0, sizeof(struct semaphore));
802 sema_init(sem, initial_units);
804 *handle = (acpi_handle*)sem;
806 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n", *handle, initial_units));
808 return_ACPI_STATUS (AE_OK);
813 * TODO: A better way to delete semaphores? Linux doesn't have a
814 * 'delete_semaphore()' function -- may result in an invalid
815 * pointer dereference for non-synchronized consumers. Should
816 * we at least check for blocked threads and signal/cancel them?
819 acpi_status
820 acpi_os_delete_semaphore(
821 acpi_handle handle)
823 struct semaphore *sem = (struct semaphore*) handle;
825 ACPI_FUNCTION_TRACE ("os_delete_semaphore");
827 if (!sem)
828 return_ACPI_STATUS (AE_BAD_PARAMETER);
830 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
832 acpi_os_free(sem); sem = NULL;
834 return_ACPI_STATUS (AE_OK);
839 * TODO: The kernel doesn't have a 'down_timeout' function -- had to
840 * improvise. The process is to sleep for one scheduler quantum
841 * until the semaphore becomes available. Downside is that this
842 * may result in starvation for timeout-based waits when there's
843 * lots of semaphore activity.
845 * TODO: Support for units > 1?
847 acpi_status
848 acpi_os_wait_semaphore(
849 acpi_handle handle,
850 u32 units,
851 u16 timeout)
853 acpi_status status = AE_OK;
854 struct semaphore *sem = (struct semaphore*)handle;
855 int ret = 0;
857 ACPI_FUNCTION_TRACE ("os_wait_semaphore");
859 if (!sem || (units < 1))
860 return_ACPI_STATUS (AE_BAD_PARAMETER);
862 if (units > 1)
863 return_ACPI_STATUS (AE_SUPPORT);
865 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n", handle, units, timeout));
867 if (in_atomic())
868 timeout = 0;
870 switch (timeout)
873 * No Wait:
874 * --------
875 * A zero timeout value indicates that we shouldn't wait - just
876 * acquire the semaphore if available otherwise return AE_TIME
877 * (a.k.a. 'would block').
879 case 0:
880 if(down_trylock(sem))
881 status = AE_TIME;
882 break;
885 * Wait Indefinitely:
886 * ------------------
888 case ACPI_WAIT_FOREVER:
889 down(sem);
890 break;
893 * Wait w/ Timeout:
894 * ----------------
896 default:
897 // TODO: A better timeout algorithm?
899 int i = 0;
900 static const int quantum_ms = 1000/HZ;
902 ret = down_trylock(sem);
903 for (i = timeout; (i > 0 && ret < 0); i -= quantum_ms) {
904 current->state = TASK_INTERRUPTIBLE;
905 schedule_timeout(1);
906 ret = down_trylock(sem);
909 if (ret != 0)
910 status = AE_TIME;
912 break;
915 if (ACPI_FAILURE(status)) {
916 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Failed to acquire semaphore[%p|%d|%d], %s\n",
917 handle, units, timeout, acpi_format_exception(status)));
919 else {
920 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Acquired semaphore[%p|%d|%d]\n", handle, units, timeout));
923 return_ACPI_STATUS (status);
928 * TODO: Support for units > 1?
930 acpi_status
931 acpi_os_signal_semaphore(
932 acpi_handle handle,
933 u32 units)
935 struct semaphore *sem = (struct semaphore *) handle;
937 ACPI_FUNCTION_TRACE ("os_signal_semaphore");
939 if (!sem || (units < 1))
940 return_ACPI_STATUS (AE_BAD_PARAMETER);
942 if (units > 1)
943 return_ACPI_STATUS (AE_SUPPORT);
945 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle, units));
947 up(sem);
949 return_ACPI_STATUS (AE_OK);
953 acpi_os_get_line(char *buffer)
956 #ifdef ENABLE_DEBUGGER
957 if (acpi_in_debugger) {
958 u32 chars;
960 kdb_read(buffer, sizeof(line_buf));
962 /* remove the CR kdb includes */
963 chars = strlen(buffer) - 1;
964 buffer[chars] = '\0';
966 #endif
968 return 0;
971 /* Assumes no unreadable holes inbetween */
973 acpi_os_readable(void *ptr, acpi_size len)
975 #if defined(__i386__) || defined(__x86_64__)
976 char tmp;
977 return !__get_user(tmp, (char __user *)ptr) && !__get_user(tmp, (char __user *)ptr + len - 1);
978 #endif
979 return 1;
983 acpi_os_writable(void *ptr, acpi_size len)
985 /* could do dummy write (racy) or a kernel page table lookup.
986 The later may be difficult at early boot when kmap doesn't work yet. */
987 return 1;
991 acpi_os_get_thread_id (void)
993 if (!in_atomic())
994 return current->pid;
996 return 0;
999 acpi_status
1000 acpi_os_signal (
1001 u32 function,
1002 void *info)
1004 switch (function)
1006 case ACPI_SIGNAL_FATAL:
1007 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1008 break;
1009 case ACPI_SIGNAL_BREAKPOINT:
1011 char *bp_info = (char*) info;
1013 printk(KERN_ERR "ACPI breakpoint: %s\n", bp_info);
1015 default:
1016 break;
1019 return AE_OK;
1022 int __init
1023 acpi_os_name_setup(char *str)
1025 char *p = acpi_os_name;
1026 int count = ACPI_MAX_OVERRIDE_LEN-1;
1028 if (!str || !*str)
1029 return 0;
1031 for (; count-- && str && *str; str++) {
1032 if (isalnum(*str) || *str == ' ' || *str == ':')
1033 *p++ = *str;
1034 else if (*str == '\'' || *str == '"')
1035 continue;
1036 else
1037 break;
1039 *p = 0;
1041 return 1;
1045 __setup("acpi_os_name=", acpi_os_name_setup);
1048 * _OSI control
1049 * empty string disables _OSI
1050 * TBD additional string adds to _OSI
1052 int __init
1053 acpi_osi_setup(char *str)
1055 if (str == NULL || *str == '\0') {
1056 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1057 acpi_gbl_create_osi_method = FALSE;
1058 } else
1060 /* TBD */
1061 printk(KERN_ERR PREFIX "_OSI additional string ignored -- %s\n", str);
1064 return 1;
1067 __setup("acpi_osi=", acpi_osi_setup);
1069 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1070 int __init
1071 acpi_serialize_setup(char *str)
1073 printk(KERN_INFO PREFIX "serialize enabled\n");
1075 acpi_gbl_all_methods_serialized = TRUE;
1077 return 1;
1080 __setup("acpi_serialize", acpi_serialize_setup);
1083 * Wake and Run-Time GPES are expected to be separate.
1084 * We disable wake-GPEs at run-time to prevent spurious
1085 * interrupts.
1087 * However, if a system exists that shares Wake and
1088 * Run-time events on the same GPE this flag is available
1089 * to tell Linux to keep the wake-time GPEs enabled at run-time.
1091 int __init
1092 acpi_wake_gpes_always_on_setup(char *str)
1094 printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
1096 acpi_gbl_leave_wake_gpes_disabled = FALSE;
1098 return 1;
1101 __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);