[PATCH] ppc32: Add ppc_sys descriptions for PowerQUICC II devices
[usb.git] / drivers / acpi / osl.c
blob0d11d6e6abd6d652efab6c2831a81b340c7a50f7
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/module.h>
30 #include <linux/kernel.h>
31 #include <linux/slab.h>
32 #include <linux/mm.h>
33 #include <linux/pci.h>
34 #include <linux/smp_lock.h>
35 #include <linux/interrupt.h>
36 #include <linux/kmod.h>
37 #include <linux/delay.h>
38 #include <linux/workqueue.h>
39 #include <linux/nmi.h>
40 #include <acpi/acpi.h>
41 #include <asm/io.h>
42 #include <acpi/acpi_bus.h>
43 #include <acpi/processor.h>
44 #include <asm/uaccess.h>
46 #include <linux/efi.h>
49 #define _COMPONENT ACPI_OS_SERVICES
50 ACPI_MODULE_NAME ("osl")
52 #define PREFIX "ACPI: "
54 struct acpi_os_dpc
56 acpi_osd_exec_callback function;
57 void *context;
60 #ifdef CONFIG_ACPI_CUSTOM_DSDT
61 #include CONFIG_ACPI_CUSTOM_DSDT_FILE
62 #endif
64 #ifdef ENABLE_DEBUGGER
65 #include <linux/kdb.h>
67 /* stuff for debugger support */
68 int acpi_in_debugger;
69 EXPORT_SYMBOL(acpi_in_debugger);
71 extern char line_buf[80];
72 #endif /*ENABLE_DEBUGGER*/
74 int acpi_specific_hotkey_enabled = TRUE;
75 EXPORT_SYMBOL(acpi_specific_hotkey_enabled);
77 static unsigned int acpi_irq_irq;
78 static acpi_osd_handler acpi_irq_handler;
79 static void *acpi_irq_context;
80 static struct workqueue_struct *kacpid_wq;
82 acpi_status
83 acpi_os_initialize(void)
85 return AE_OK;
88 acpi_status
89 acpi_os_initialize1(void)
92 * Initialize PCI configuration space access, as we'll need to access
93 * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
95 #ifdef CONFIG_ACPI_PCI
96 if (!raw_pci_ops) {
97 printk(KERN_ERR PREFIX "Access to PCI configuration space unavailable\n");
98 return AE_NULL_ENTRY;
100 #endif
101 kacpid_wq = create_singlethread_workqueue("kacpid");
102 BUG_ON(!kacpid_wq);
104 return AE_OK;
107 acpi_status
108 acpi_os_terminate(void)
110 if (acpi_irq_handler) {
111 acpi_os_remove_interrupt_handler(acpi_irq_irq,
112 acpi_irq_handler);
115 destroy_workqueue(kacpid_wq);
117 return AE_OK;
120 void
121 acpi_os_printf(const char *fmt,...)
123 va_list args;
124 va_start(args, fmt);
125 acpi_os_vprintf(fmt, args);
126 va_end(args);
128 EXPORT_SYMBOL(acpi_os_printf);
130 void
131 acpi_os_vprintf(const char *fmt, va_list args)
133 static char buffer[512];
135 vsprintf(buffer, fmt, args);
137 #ifdef ENABLE_DEBUGGER
138 if (acpi_in_debugger) {
139 kdb_printf("%s", buffer);
140 } else {
141 printk("%s", buffer);
143 #else
144 printk("%s", buffer);
145 #endif
148 extern int acpi_in_resume;
149 void *
150 acpi_os_allocate(acpi_size size)
152 if (acpi_in_resume)
153 return kmalloc(size, GFP_ATOMIC);
154 else
155 return kmalloc(size, GFP_KERNEL);
158 void
159 acpi_os_free(void *ptr)
161 kfree(ptr);
163 EXPORT_SYMBOL(acpi_os_free);
165 acpi_status
166 acpi_os_get_root_pointer(u32 flags, struct acpi_pointer *addr)
168 if (efi_enabled) {
169 addr->pointer_type = ACPI_PHYSICAL_POINTER;
170 if (efi.acpi20)
171 addr->pointer.physical =
172 (acpi_physical_address) virt_to_phys(efi.acpi20);
173 else if (efi.acpi)
174 addr->pointer.physical =
175 (acpi_physical_address) virt_to_phys(efi.acpi);
176 else {
177 printk(KERN_ERR PREFIX "System description tables not found\n");
178 return AE_NOT_FOUND;
180 } else {
181 if (ACPI_FAILURE(acpi_find_root_pointer(flags, addr))) {
182 printk(KERN_ERR PREFIX "System description tables not found\n");
183 return AE_NOT_FOUND;
187 return AE_OK;
190 acpi_status
191 acpi_os_map_memory(acpi_physical_address phys, acpi_size size, void __iomem **virt)
193 if (efi_enabled) {
194 if (EFI_MEMORY_WB & efi_mem_attributes(phys)) {
195 *virt = (void __iomem *) phys_to_virt(phys);
196 } else {
197 *virt = ioremap(phys, size);
199 } else {
200 if (phys > ULONG_MAX) {
201 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
202 return AE_BAD_PARAMETER;
205 * ioremap checks to ensure this is in reserved space
207 *virt = ioremap((unsigned long) phys, size);
210 if (!*virt)
211 return AE_NO_MEMORY;
213 return AE_OK;
216 void
217 acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
219 iounmap(virt);
222 #ifdef ACPI_FUTURE_USAGE
223 acpi_status
224 acpi_os_get_physical_address(void *virt, acpi_physical_address *phys)
226 if(!phys || !virt)
227 return AE_BAD_PARAMETER;
229 *phys = virt_to_phys(virt);
231 return AE_OK;
233 #endif
235 #define ACPI_MAX_OVERRIDE_LEN 100
237 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
239 acpi_status
240 acpi_os_predefined_override (const struct acpi_predefined_names *init_val,
241 acpi_string *new_val)
243 if (!init_val || !new_val)
244 return AE_BAD_PARAMETER;
246 *new_val = NULL;
247 if (!memcmp (init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
248 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
249 acpi_os_name);
250 *new_val = acpi_os_name;
253 return AE_OK;
256 acpi_status
257 acpi_os_table_override (struct acpi_table_header *existing_table,
258 struct acpi_table_header **new_table)
260 if (!existing_table || !new_table)
261 return AE_BAD_PARAMETER;
263 #ifdef CONFIG_ACPI_CUSTOM_DSDT
264 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
265 *new_table = (struct acpi_table_header*)AmlCode;
266 else
267 *new_table = NULL;
268 #else
269 *new_table = NULL;
270 #endif
271 return AE_OK;
274 static irqreturn_t
275 acpi_irq(int irq, void *dev_id, struct pt_regs *regs)
277 return (*acpi_irq_handler)(acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
280 acpi_status
281 acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler, void *context)
283 unsigned int irq;
286 * Ignore the GSI from the core, and use the value in our copy of the
287 * FADT. It may not be the same if an interrupt source override exists
288 * for the SCI.
290 gsi = acpi_fadt.sci_int;
291 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
292 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
293 gsi);
294 return AE_OK;
297 acpi_irq_handler = handler;
298 acpi_irq_context = context;
299 if (request_irq(irq, acpi_irq, SA_SHIRQ, "acpi", acpi_irq)) {
300 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
301 return AE_NOT_ACQUIRED;
303 acpi_irq_irq = irq;
305 return AE_OK;
308 acpi_status
309 acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
311 if (irq) {
312 free_irq(irq, acpi_irq);
313 acpi_irq_handler = NULL;
314 acpi_irq_irq = 0;
317 return AE_OK;
321 * Running in interpreter thread context, safe to sleep
324 void
325 acpi_os_sleep(acpi_integer ms)
327 current->state = TASK_INTERRUPTIBLE;
328 schedule_timeout(((signed long) ms * HZ) / 1000);
330 EXPORT_SYMBOL(acpi_os_sleep);
332 void
333 acpi_os_stall(u32 us)
335 while (us) {
336 u32 delay = 1000;
338 if (delay > us)
339 delay = us;
340 udelay(delay);
341 touch_nmi_watchdog();
342 us -= delay;
345 EXPORT_SYMBOL(acpi_os_stall);
348 * Support ACPI 3.0 AML Timer operand
349 * Returns 64-bit free-running, monotonically increasing timer
350 * with 100ns granularity
353 acpi_os_get_timer (void)
355 static u64 t;
357 #ifdef CONFIG_HPET
358 /* TBD: use HPET if available */
359 #endif
361 #ifdef CONFIG_X86_PM_TIMER
362 /* TBD: default to PM timer if HPET was not available */
363 #endif
364 if (!t)
365 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
367 return ++t;
370 acpi_status
371 acpi_os_read_port(
372 acpi_io_address port,
373 u32 *value,
374 u32 width)
376 u32 dummy;
378 if (!value)
379 value = &dummy;
381 switch (width)
383 case 8:
384 *(u8*) value = inb(port);
385 break;
386 case 16:
387 *(u16*) value = inw(port);
388 break;
389 case 32:
390 *(u32*) value = inl(port);
391 break;
392 default:
393 BUG();
396 return AE_OK;
398 EXPORT_SYMBOL(acpi_os_read_port);
400 acpi_status
401 acpi_os_write_port(
402 acpi_io_address port,
403 u32 value,
404 u32 width)
406 switch (width)
408 case 8:
409 outb(value, port);
410 break;
411 case 16:
412 outw(value, port);
413 break;
414 case 32:
415 outl(value, port);
416 break;
417 default:
418 BUG();
421 return AE_OK;
423 EXPORT_SYMBOL(acpi_os_write_port);
425 acpi_status
426 acpi_os_read_memory(
427 acpi_physical_address phys_addr,
428 u32 *value,
429 u32 width)
431 u32 dummy;
432 void __iomem *virt_addr;
433 int iomem = 0;
435 if (efi_enabled) {
436 if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
437 /* HACK ALERT! We can use readb/w/l on real memory too.. */
438 virt_addr = (void __iomem *) phys_to_virt(phys_addr);
439 } else {
440 iomem = 1;
441 virt_addr = ioremap(phys_addr, width);
443 } else
444 virt_addr = (void __iomem *) phys_to_virt(phys_addr);
445 if (!value)
446 value = &dummy;
448 switch (width) {
449 case 8:
450 *(u8*) value = readb(virt_addr);
451 break;
452 case 16:
453 *(u16*) value = readw(virt_addr);
454 break;
455 case 32:
456 *(u32*) value = readl(virt_addr);
457 break;
458 default:
459 BUG();
462 if (efi_enabled) {
463 if (iomem)
464 iounmap(virt_addr);
467 return AE_OK;
470 acpi_status
471 acpi_os_write_memory(
472 acpi_physical_address phys_addr,
473 u32 value,
474 u32 width)
476 void __iomem *virt_addr;
477 int iomem = 0;
479 if (efi_enabled) {
480 if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
481 /* HACK ALERT! We can use writeb/w/l on real memory too */
482 virt_addr = (void __iomem *) phys_to_virt(phys_addr);
483 } else {
484 iomem = 1;
485 virt_addr = ioremap(phys_addr, width);
487 } else
488 virt_addr = (void __iomem *) phys_to_virt(phys_addr);
490 switch (width) {
491 case 8:
492 writeb(value, virt_addr);
493 break;
494 case 16:
495 writew(value, virt_addr);
496 break;
497 case 32:
498 writel(value, virt_addr);
499 break;
500 default:
501 BUG();
504 if (iomem)
505 iounmap(virt_addr);
507 return AE_OK;
510 #ifdef CONFIG_ACPI_PCI
512 acpi_status
513 acpi_os_read_pci_configuration (struct acpi_pci_id *pci_id, u32 reg, void *value, u32 width)
515 int result, size;
517 if (!value)
518 return AE_BAD_PARAMETER;
520 switch (width) {
521 case 8:
522 size = 1;
523 break;
524 case 16:
525 size = 2;
526 break;
527 case 32:
528 size = 4;
529 break;
530 default:
531 return AE_ERROR;
534 BUG_ON(!raw_pci_ops);
536 result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
537 PCI_DEVFN(pci_id->device, pci_id->function),
538 reg, size, value);
540 return (result ? AE_ERROR : AE_OK);
542 EXPORT_SYMBOL(acpi_os_read_pci_configuration);
544 acpi_status
545 acpi_os_write_pci_configuration (struct acpi_pci_id *pci_id, u32 reg, acpi_integer value, u32 width)
547 int result, size;
549 switch (width) {
550 case 8:
551 size = 1;
552 break;
553 case 16:
554 size = 2;
555 break;
556 case 32:
557 size = 4;
558 break;
559 default:
560 return AE_ERROR;
563 BUG_ON(!raw_pci_ops);
565 result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
566 PCI_DEVFN(pci_id->device, pci_id->function),
567 reg, size, value);
569 return (result ? AE_ERROR : AE_OK);
572 /* TODO: Change code to take advantage of driver model more */
573 static void
574 acpi_os_derive_pci_id_2 (
575 acpi_handle rhandle, /* upper bound */
576 acpi_handle chandle, /* current node */
577 struct acpi_pci_id **id,
578 int *is_bridge,
579 u8 *bus_number)
581 acpi_handle handle;
582 struct acpi_pci_id *pci_id = *id;
583 acpi_status status;
584 unsigned long temp;
585 acpi_object_type type;
586 u8 tu8;
588 acpi_get_parent(chandle, &handle);
589 if (handle != rhandle) {
590 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge, bus_number);
592 status = acpi_get_type(handle, &type);
593 if ( (ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE) )
594 return;
596 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &temp);
597 if (ACPI_SUCCESS(status)) {
598 pci_id->device = ACPI_HIWORD (ACPI_LODWORD (temp));
599 pci_id->function = ACPI_LOWORD (ACPI_LODWORD (temp));
601 if (*is_bridge)
602 pci_id->bus = *bus_number;
604 /* any nicer way to get bus number of bridge ? */
605 status = acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8, 8);
606 if (ACPI_SUCCESS(status) &&
607 ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
608 status = acpi_os_read_pci_configuration(pci_id, 0x18, &tu8, 8);
609 if (!ACPI_SUCCESS(status)) {
610 /* Certainly broken... FIX ME */
611 return;
613 *is_bridge = 1;
614 pci_id->bus = tu8;
615 status = acpi_os_read_pci_configuration(pci_id, 0x19, &tu8, 8);
616 if (ACPI_SUCCESS(status)) {
617 *bus_number = tu8;
619 } else
620 *is_bridge = 0;
625 void
626 acpi_os_derive_pci_id (
627 acpi_handle rhandle, /* upper bound */
628 acpi_handle chandle, /* current node */
629 struct acpi_pci_id **id)
631 int is_bridge = 1;
632 u8 bus_number = (*id)->bus;
634 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
637 #else /*!CONFIG_ACPI_PCI*/
639 acpi_status
640 acpi_os_write_pci_configuration (
641 struct acpi_pci_id *pci_id,
642 u32 reg,
643 acpi_integer value,
644 u32 width)
646 return AE_SUPPORT;
649 acpi_status
650 acpi_os_read_pci_configuration (
651 struct acpi_pci_id *pci_id,
652 u32 reg,
653 void *value,
654 u32 width)
656 return AE_SUPPORT;
659 void
660 acpi_os_derive_pci_id (
661 acpi_handle rhandle, /* upper bound */
662 acpi_handle chandle, /* current node */
663 struct acpi_pci_id **id)
667 #endif /*CONFIG_ACPI_PCI*/
669 static void
670 acpi_os_execute_deferred (
671 void *context)
673 struct acpi_os_dpc *dpc = NULL;
675 ACPI_FUNCTION_TRACE ("os_execute_deferred");
677 dpc = (struct acpi_os_dpc *) context;
678 if (!dpc) {
679 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
680 return_VOID;
683 dpc->function(dpc->context);
685 kfree(dpc);
687 return_VOID;
690 acpi_status
691 acpi_os_queue_for_execution(
692 u32 priority,
693 acpi_osd_exec_callback function,
694 void *context)
696 acpi_status status = AE_OK;
697 struct acpi_os_dpc *dpc;
698 struct work_struct *task;
700 ACPI_FUNCTION_TRACE ("os_queue_for_execution");
702 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Scheduling function [%p(%p)] for deferred execution.\n", function, context));
704 if (!function)
705 return_ACPI_STATUS (AE_BAD_PARAMETER);
708 * Allocate/initialize DPC structure. Note that this memory will be
709 * freed by the callee. The kernel handles the tq_struct list in a
710 * way that allows us to also free its memory inside the callee.
711 * Because we may want to schedule several tasks with different
712 * parameters we can't use the approach some kernel code uses of
713 * having a static tq_struct.
714 * We can save time and code by allocating the DPC and tq_structs
715 * from the same memory.
718 dpc = kmalloc(sizeof(struct acpi_os_dpc)+sizeof(struct work_struct), GFP_ATOMIC);
719 if (!dpc)
720 return_ACPI_STATUS (AE_NO_MEMORY);
722 dpc->function = function;
723 dpc->context = context;
725 task = (void *)(dpc+1);
726 INIT_WORK(task, acpi_os_execute_deferred, (void*)dpc);
728 if (!queue_work(kacpid_wq, task)) {
729 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Call to queue_work() failed.\n"));
730 kfree(dpc);
731 status = AE_ERROR;
734 return_ACPI_STATUS (status);
736 EXPORT_SYMBOL(acpi_os_queue_for_execution);
738 void
739 acpi_os_wait_events_complete(
740 void *context)
742 flush_workqueue(kacpid_wq);
744 EXPORT_SYMBOL(acpi_os_wait_events_complete);
747 * Allocate the memory for a spinlock and initialize it.
749 acpi_status
750 acpi_os_create_lock (
751 acpi_handle *out_handle)
753 spinlock_t *lock_ptr;
755 ACPI_FUNCTION_TRACE ("os_create_lock");
757 lock_ptr = acpi_os_allocate(sizeof(spinlock_t));
759 spin_lock_init(lock_ptr);
761 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Creating spinlock[%p].\n", lock_ptr));
763 *out_handle = lock_ptr;
765 return_ACPI_STATUS (AE_OK);
770 * Deallocate the memory for a spinlock.
772 void
773 acpi_os_delete_lock (
774 acpi_handle handle)
776 ACPI_FUNCTION_TRACE ("os_create_lock");
778 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Deleting spinlock[%p].\n", handle));
780 acpi_os_free(handle);
782 return_VOID;
786 * Acquire a spinlock.
788 * handle is a pointer to the spinlock_t.
789 * flags is *not* the result of save_flags - it is an ACPI-specific flag variable
790 * that indicates whether we are at interrupt level.
792 void
793 acpi_os_acquire_lock (
794 acpi_handle handle,
795 u32 flags)
797 ACPI_FUNCTION_TRACE ("os_acquire_lock");
799 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Acquiring spinlock[%p] from %s level\n", handle,
800 ((flags & ACPI_NOT_ISR) ? "non-interrupt" : "interrupt")));
802 if (flags & ACPI_NOT_ISR)
803 ACPI_DISABLE_IRQS();
805 spin_lock((spinlock_t *)handle);
807 return_VOID;
812 * Release a spinlock. See above.
814 void
815 acpi_os_release_lock (
816 acpi_handle handle,
817 u32 flags)
819 ACPI_FUNCTION_TRACE ("os_release_lock");
821 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Releasing spinlock[%p] from %s level\n", handle,
822 ((flags & ACPI_NOT_ISR) ? "non-interrupt" : "interrupt")));
824 spin_unlock((spinlock_t *)handle);
826 if (flags & ACPI_NOT_ISR)
827 ACPI_ENABLE_IRQS();
829 return_VOID;
833 acpi_status
834 acpi_os_create_semaphore(
835 u32 max_units,
836 u32 initial_units,
837 acpi_handle *handle)
839 struct semaphore *sem = NULL;
841 ACPI_FUNCTION_TRACE ("os_create_semaphore");
843 sem = acpi_os_allocate(sizeof(struct semaphore));
844 if (!sem)
845 return_ACPI_STATUS (AE_NO_MEMORY);
846 memset(sem, 0, sizeof(struct semaphore));
848 sema_init(sem, initial_units);
850 *handle = (acpi_handle*)sem;
852 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n", *handle, initial_units));
854 return_ACPI_STATUS (AE_OK);
856 EXPORT_SYMBOL(acpi_os_create_semaphore);
860 * TODO: A better way to delete semaphores? Linux doesn't have a
861 * 'delete_semaphore()' function -- may result in an invalid
862 * pointer dereference for non-synchronized consumers. Should
863 * we at least check for blocked threads and signal/cancel them?
866 acpi_status
867 acpi_os_delete_semaphore(
868 acpi_handle handle)
870 struct semaphore *sem = (struct semaphore*) handle;
872 ACPI_FUNCTION_TRACE ("os_delete_semaphore");
874 if (!sem)
875 return_ACPI_STATUS (AE_BAD_PARAMETER);
877 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
879 acpi_os_free(sem); sem = NULL;
881 return_ACPI_STATUS (AE_OK);
883 EXPORT_SYMBOL(acpi_os_delete_semaphore);
887 * TODO: The kernel doesn't have a 'down_timeout' function -- had to
888 * improvise. The process is to sleep for one scheduler quantum
889 * until the semaphore becomes available. Downside is that this
890 * may result in starvation for timeout-based waits when there's
891 * lots of semaphore activity.
893 * TODO: Support for units > 1?
895 acpi_status
896 acpi_os_wait_semaphore(
897 acpi_handle handle,
898 u32 units,
899 u16 timeout)
901 acpi_status status = AE_OK;
902 struct semaphore *sem = (struct semaphore*)handle;
903 int ret = 0;
905 ACPI_FUNCTION_TRACE ("os_wait_semaphore");
907 if (!sem || (units < 1))
908 return_ACPI_STATUS (AE_BAD_PARAMETER);
910 if (units > 1)
911 return_ACPI_STATUS (AE_SUPPORT);
913 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n", handle, units, timeout));
915 if (in_atomic())
916 timeout = 0;
918 switch (timeout)
921 * No Wait:
922 * --------
923 * A zero timeout value indicates that we shouldn't wait - just
924 * acquire the semaphore if available otherwise return AE_TIME
925 * (a.k.a. 'would block').
927 case 0:
928 if(down_trylock(sem))
929 status = AE_TIME;
930 break;
933 * Wait Indefinitely:
934 * ------------------
936 case ACPI_WAIT_FOREVER:
937 down(sem);
938 break;
941 * Wait w/ Timeout:
942 * ----------------
944 default:
945 // TODO: A better timeout algorithm?
947 int i = 0;
948 static const int quantum_ms = 1000/HZ;
950 ret = down_trylock(sem);
951 for (i = timeout; (i > 0 && ret < 0); i -= quantum_ms) {
952 current->state = TASK_INTERRUPTIBLE;
953 schedule_timeout(1);
954 ret = down_trylock(sem);
957 if (ret != 0)
958 status = AE_TIME;
960 break;
963 if (ACPI_FAILURE(status)) {
964 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Failed to acquire semaphore[%p|%d|%d], %s\n",
965 handle, units, timeout, acpi_format_exception(status)));
967 else {
968 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Acquired semaphore[%p|%d|%d]\n", handle, units, timeout));
971 return_ACPI_STATUS (status);
973 EXPORT_SYMBOL(acpi_os_wait_semaphore);
977 * TODO: Support for units > 1?
979 acpi_status
980 acpi_os_signal_semaphore(
981 acpi_handle handle,
982 u32 units)
984 struct semaphore *sem = (struct semaphore *) handle;
986 ACPI_FUNCTION_TRACE ("os_signal_semaphore");
988 if (!sem || (units < 1))
989 return_ACPI_STATUS (AE_BAD_PARAMETER);
991 if (units > 1)
992 return_ACPI_STATUS (AE_SUPPORT);
994 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle, units));
996 up(sem);
998 return_ACPI_STATUS (AE_OK);
1000 EXPORT_SYMBOL(acpi_os_signal_semaphore);
1002 #ifdef ACPI_FUTURE_USAGE
1004 acpi_os_get_line(char *buffer)
1007 #ifdef ENABLE_DEBUGGER
1008 if (acpi_in_debugger) {
1009 u32 chars;
1011 kdb_read(buffer, sizeof(line_buf));
1013 /* remove the CR kdb includes */
1014 chars = strlen(buffer) - 1;
1015 buffer[chars] = '\0';
1017 #endif
1019 return 0;
1021 #endif /* ACPI_FUTURE_USAGE */
1023 /* Assumes no unreadable holes inbetween */
1025 acpi_os_readable(void *ptr, acpi_size len)
1027 #if defined(__i386__) || defined(__x86_64__)
1028 char tmp;
1029 return !__get_user(tmp, (char __user *)ptr) && !__get_user(tmp, (char __user *)ptr + len - 1);
1030 #endif
1031 return 1;
1034 #ifdef ACPI_FUTURE_USAGE
1036 acpi_os_writable(void *ptr, acpi_size len)
1038 /* could do dummy write (racy) or a kernel page table lookup.
1039 The later may be difficult at early boot when kmap doesn't work yet. */
1040 return 1;
1042 #endif
1045 acpi_os_get_thread_id (void)
1047 if (!in_atomic())
1048 return current->pid;
1050 return 0;
1053 acpi_status
1054 acpi_os_signal (
1055 u32 function,
1056 void *info)
1058 switch (function)
1060 case ACPI_SIGNAL_FATAL:
1061 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1062 break;
1063 case ACPI_SIGNAL_BREAKPOINT:
1065 * AML Breakpoint
1066 * ACPI spec. says to treat it as a NOP unless
1067 * you are debugging. So if/when we integrate
1068 * AML debugger into the kernel debugger its
1069 * hook will go here. But until then it is
1070 * not useful to print anything on breakpoints.
1072 break;
1073 default:
1074 break;
1077 return AE_OK;
1079 EXPORT_SYMBOL(acpi_os_signal);
1081 static int __init
1082 acpi_os_name_setup(char *str)
1084 char *p = acpi_os_name;
1085 int count = ACPI_MAX_OVERRIDE_LEN-1;
1087 if (!str || !*str)
1088 return 0;
1090 for (; count-- && str && *str; str++) {
1091 if (isalnum(*str) || *str == ' ' || *str == ':')
1092 *p++ = *str;
1093 else if (*str == '\'' || *str == '"')
1094 continue;
1095 else
1096 break;
1098 *p = 0;
1100 return 1;
1104 __setup("acpi_os_name=", acpi_os_name_setup);
1107 * _OSI control
1108 * empty string disables _OSI
1109 * TBD additional string adds to _OSI
1111 static int __init
1112 acpi_osi_setup(char *str)
1114 if (str == NULL || *str == '\0') {
1115 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1116 acpi_gbl_create_osi_method = FALSE;
1117 } else
1119 /* TBD */
1120 printk(KERN_ERR PREFIX "_OSI additional string ignored -- %s\n", str);
1123 return 1;
1126 __setup("acpi_osi=", acpi_osi_setup);
1128 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1129 static int __init
1130 acpi_serialize_setup(char *str)
1132 printk(KERN_INFO PREFIX "serialize enabled\n");
1134 acpi_gbl_all_methods_serialized = TRUE;
1136 return 1;
1139 __setup("acpi_serialize", acpi_serialize_setup);
1142 * Wake and Run-Time GPES are expected to be separate.
1143 * We disable wake-GPEs at run-time to prevent spurious
1144 * interrupts.
1146 * However, if a system exists that shares Wake and
1147 * Run-time events on the same GPE this flag is available
1148 * to tell Linux to keep the wake-time GPEs enabled at run-time.
1150 static int __init
1151 acpi_wake_gpes_always_on_setup(char *str)
1153 printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
1155 acpi_gbl_leave_wake_gpes_disabled = FALSE;
1157 return 1;
1160 __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
1162 int __init
1163 acpi_hotkey_setup(char *str)
1165 acpi_specific_hotkey_enabled = FALSE;
1166 return 1;
1169 __setup("acpi_generic_hotkey", acpi_hotkey_setup);
1172 * max_cstate is defined in the base kernel so modules can
1173 * change it w/o depending on the state of the processor module.
1175 unsigned int max_cstate = ACPI_PROCESSOR_MAX_POWER;
1178 EXPORT_SYMBOL(max_cstate);