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>
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>
40 #include <acpi/acpi_bus.h>
42 #ifdef CONFIG_ACPI_EFI
43 #include <linux/efi.h>
44 u64 efi_mem_attributes (u64 phys_addr);
48 #define _COMPONENT ACPI_OS_SERVICES
49 ACPI_MODULE_NAME ("osl")
51 #define PREFIX "ACPI: "
55 OSD_EXECUTION_CALLBACK function;
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*/
67 static int acpi_irq_irq = 0;
68 static OSD_HANDLER acpi_irq_handler = NULL;
69 static void *acpi_irq_context = NULL;
71 extern struct pci_ops *pci_root_ops;
74 acpi_os_initialize(void)
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).
80 #ifdef CONFIG_ACPI_PCI
82 printk(KERN_ERR PREFIX "Access to PCI configuration space unavailable\n");
91 acpi_os_terminate(void)
93 if (acpi_irq_handler) {
94 acpi_os_remove_interrupt_handler(acpi_irq_irq,
102 acpi_os_printf(const char *fmt,...)
106 acpi_os_vprintf(fmt, args);
111 acpi_os_vprintf(const char *fmt, va_list args)
113 static char buffer[512];
115 vsprintf(buffer, fmt, args);
117 #ifdef ENABLE_DEBUGGER
118 if (acpi_in_debugger) {
119 kdb_printf("%s", buffer);
121 printk("%s", buffer);
124 printk("%s", buffer);
129 acpi_os_allocate(acpi_size size)
131 return kmalloc(size, GFP_KERNEL);
135 acpi_os_free(void *ptr)
141 acpi_os_get_root_pointer(u32 flags, struct acpi_pointer *addr)
143 #ifdef CONFIG_ACPI_EFI
144 addr->pointer_type = ACPI_PHYSICAL_POINTER;
146 addr->pointer.physical = (acpi_physical_address) virt_to_phys(efi.acpi20);
148 addr->pointer.physical = (acpi_physical_address) virt_to_phys(efi.acpi);
150 printk(KERN_ERR PREFIX "System description tables not found\n");
154 if (ACPI_FAILURE(acpi_find_root_pointer(flags, addr))) {
155 printk(KERN_ERR PREFIX "System description tables not found\n");
158 #endif /*CONFIG_ACPI_EFI*/
164 acpi_os_map_memory(acpi_physical_address phys, acpi_size size, void **virt)
166 #ifdef CONFIG_ACPI_EFI
167 if (EFI_MEMORY_WB & efi_mem_attributes(phys)) {
168 *virt = phys_to_virt(phys);
170 *virt = ioremap(phys, size);
173 if (phys > ULONG_MAX) {
174 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
175 return AE_BAD_PARAMETER;
178 * ioremap checks to ensure this is in reserved space
180 *virt = ioremap((unsigned long) phys, size);
190 acpi_os_unmap_memory(void *virt, acpi_size size)
196 acpi_os_get_physical_address(void *virt, acpi_physical_address *phys)
199 return AE_BAD_PARAMETER;
201 *phys = virt_to_phys(virt);
206 #define ACPI_MAX_OVERRIDE_LEN 100
208 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
211 acpi_os_predefined_override (const struct acpi_predefined_names *init_val,
212 acpi_string *new_val)
214 if (!init_val || !new_val)
215 return AE_BAD_PARAMETER;
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;
227 acpi_os_table_override (struct acpi_table_header *existing_table,
228 struct acpi_table_header **new_table)
230 if (!existing_table || !new_table)
231 return AE_BAD_PARAMETER;
238 acpi_irq(int irq, void *dev_id, struct pt_regs *regs)
240 return (*acpi_irq_handler)(acpi_irq_context);
244 acpi_os_install_interrupt_handler(u32 irq, OSD_HANDLER handler, void *context)
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
251 irq = acpi_fadt.sci_int;
254 irq = gsi_to_vector(irq);
257 acpi_irq_handler = handler;
258 acpi_irq_context = context;
259 if (request_irq(irq, acpi_irq, SA_SHIRQ, "acpi", acpi_irq)) {
260 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
261 return AE_NOT_ACQUIRED;
268 acpi_os_remove_interrupt_handler(u32 irq, OSD_HANDLER handler)
270 if (acpi_irq_handler) {
272 irq = gsi_to_vector(irq);
274 free_irq(irq, acpi_irq);
275 acpi_irq_handler = NULL;
282 * Running in interpreter thread context, safe to sleep
286 acpi_os_sleep(u32 sec, u32 ms)
288 current->state = TASK_INTERRUPTIBLE;
289 schedule_timeout(HZ * sec + (ms * HZ) / 1000);
293 acpi_os_stall(u32 us)
305 acpi_io_address port,
317 *(u8*) value = inb(port);
320 *(u16*) value = inw(port);
323 *(u32*) value = inl(port);
334 acpi_io_address port,
358 acpi_physical_address phys_addr,
365 #ifdef CONFIG_ACPI_EFI
368 if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
369 virt_addr = phys_to_virt(phys_addr);
372 virt_addr = ioremap(phys_addr, width);
375 virt_addr = phys_to_virt(phys_addr);
382 *(u8*) value = *(u8*) virt_addr;
385 *(u16*) value = *(u16*) virt_addr;
388 *(u32*) value = *(u32*) virt_addr;
394 #ifdef CONFIG_ACPI_EFI
403 acpi_os_write_memory(
404 acpi_physical_address phys_addr,
410 #ifdef CONFIG_ACPI_EFI
413 if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
414 virt_addr = phys_to_virt(phys_addr);
417 virt_addr = ioremap(phys_addr, width);
420 virt_addr = phys_to_virt(phys_addr);
425 *(u8*) virt_addr = value;
428 *(u16*) virt_addr = value;
431 *(u32*) virt_addr = value;
437 #ifdef CONFIG_ACPI_EFI
445 #ifdef CONFIG_ACPI_PCI
448 acpi_os_read_pci_configuration (
449 struct acpi_pci_id *pci_id,
459 return AE_BAD_PARAMETER;
475 bus.number = pci_id->bus;
476 result = pci_root_ops->read(&bus, PCI_DEVFN(pci_id->device,
480 return (result ? AE_ERROR : AE_OK);
484 acpi_os_write_pci_configuration (
485 struct acpi_pci_id *pci_id,
508 bus.number = pci_id->bus;
509 result = pci_root_ops->write(&bus, PCI_DEVFN(pci_id->device,
512 return (result ? AE_ERROR : AE_OK);
515 /* TODO: Change code to take advantage of driver model more */
517 acpi_os_derive_pci_id_2 (
518 acpi_handle rhandle, /* upper bound */
519 acpi_handle chandle, /* current node */
520 struct acpi_pci_id **id,
525 struct acpi_pci_id *pci_id = *id;
528 acpi_object_type type;
531 acpi_get_parent(chandle, &handle);
532 if (handle != rhandle) {
533 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge, bus_number);
535 status = acpi_get_type(handle, &type);
536 if ( (ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE) )
539 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &temp);
540 if (ACPI_SUCCESS(status)) {
541 pci_id->device = ACPI_HIWORD (ACPI_LODWORD (temp));
542 pci_id->function = ACPI_LOWORD (ACPI_LODWORD (temp));
545 pci_id->bus = *bus_number;
547 /* any nicer way to get bus number of bridge ? */
548 status = acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8, 8);
549 if (ACPI_SUCCESS(status) &&
550 ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
551 status = acpi_os_read_pci_configuration(pci_id, 0x18, &tu8, 8);
552 if (!ACPI_SUCCESS(status)) {
553 /* Certainly broken... FIX ME */
558 status = acpi_os_read_pci_configuration(pci_id, 0x19, &tu8, 8);
559 if (ACPI_SUCCESS(status)) {
569 acpi_os_derive_pci_id (
570 acpi_handle rhandle, /* upper bound */
571 acpi_handle chandle, /* current node */
572 struct acpi_pci_id **id)
575 u8 bus_number = (*id)->bus;
577 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
580 #else /*!CONFIG_ACPI_PCI*/
583 acpi_os_write_pci_configuration (
584 struct acpi_pci_id *pci_id,
593 acpi_os_read_pci_configuration (
594 struct acpi_pci_id *pci_id,
603 acpi_os_derive_pci_id (
604 acpi_handle rhandle, /* upper bound */
605 acpi_handle chandle, /* current node */
606 struct acpi_pci_id **id)
610 #endif /*CONFIG_ACPI_PCI*/
613 acpi_os_execute_deferred (
616 struct acpi_os_dpc *dpc = NULL;
618 ACPI_FUNCTION_TRACE ("os_execute_deferred");
620 dpc = (struct acpi_os_dpc *) context;
622 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
626 dpc->function(dpc->context);
634 acpi_os_queue_for_execution(
636 OSD_EXECUTION_CALLBACK function,
639 acpi_status status = AE_OK;
640 struct acpi_os_dpc *dpc;
641 struct work_struct *task;
643 ACPI_FUNCTION_TRACE ("os_queue_for_execution");
645 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Scheduling function [%p(%p)] for deferred execution.\n", function, context));
648 return_ACPI_STATUS (AE_BAD_PARAMETER);
651 * Allocate/initialize DPC structure. Note that this memory will be
652 * freed by the callee. The kernel handles the tq_struct list in a
653 * way that allows us to also free its memory inside the callee.
654 * Because we may want to schedule several tasks with different
655 * parameters we can't use the approach some kernel code uses of
656 * having a static tq_struct.
657 * We can save time and code by allocating the DPC and tq_structs
658 * from the same memory.
661 dpc = kmalloc(sizeof(struct acpi_os_dpc)+sizeof(struct work_struct), GFP_ATOMIC);
663 return_ACPI_STATUS (AE_NO_MEMORY);
665 dpc->function = function;
666 dpc->context = context;
668 task = (void *)(dpc+1);
669 INIT_WORK(task, acpi_os_execute_deferred, (void*)dpc);
671 if (!schedule_work(task)) {
672 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Call to schedule_work() failed.\n"));
677 return_ACPI_STATUS (status);
681 * Allocate the memory for a spinlock and initialize it.
684 acpi_os_create_lock (
685 acpi_handle *out_handle)
687 spinlock_t *lock_ptr;
689 ACPI_FUNCTION_TRACE ("os_create_lock");
691 lock_ptr = acpi_os_allocate(sizeof(spinlock_t));
693 spin_lock_init(lock_ptr);
695 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Creating spinlock[%p].\n", lock_ptr));
697 *out_handle = lock_ptr;
699 return_ACPI_STATUS (AE_OK);
704 * Deallocate the memory for a spinlock.
707 acpi_os_delete_lock (
710 ACPI_FUNCTION_TRACE ("os_create_lock");
712 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Deleting spinlock[%p].\n", handle));
714 acpi_os_free(handle);
720 * Acquire a spinlock.
722 * handle is a pointer to the spinlock_t.
723 * flags is *not* the result of save_flags - it is an ACPI-specific flag variable
724 * that indicates whether we are at interrupt level.
727 acpi_os_acquire_lock (
731 ACPI_FUNCTION_TRACE ("os_acquire_lock");
733 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Acquiring spinlock[%p] from %s level\n", handle,
734 ((flags & ACPI_NOT_ISR) ? "non-interrupt" : "interrupt")));
736 if (flags & ACPI_NOT_ISR)
739 spin_lock((spinlock_t *)handle);
746 * Release a spinlock. See above.
749 acpi_os_release_lock (
753 ACPI_FUNCTION_TRACE ("os_release_lock");
755 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Releasing spinlock[%p] from %s level\n", handle,
756 ((flags & ACPI_NOT_ISR) ? "non-interrupt" : "interrupt")));
758 spin_unlock((spinlock_t *)handle);
760 if (flags & ACPI_NOT_ISR)
768 acpi_os_create_semaphore(
773 struct semaphore *sem = NULL;
775 ACPI_FUNCTION_TRACE ("os_create_semaphore");
777 sem = acpi_os_allocate(sizeof(struct semaphore));
779 return_ACPI_STATUS (AE_NO_MEMORY);
780 memset(sem, 0, sizeof(struct semaphore));
782 sema_init(sem, initial_units);
784 *handle = (acpi_handle*)sem;
786 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n", *handle, initial_units));
788 return_ACPI_STATUS (AE_OK);
793 * TODO: A better way to delete semaphores? Linux doesn't have a
794 * 'delete_semaphore()' function -- may result in an invalid
795 * pointer dereference for non-synchronized consumers. Should
796 * we at least check for blocked threads and signal/cancel them?
800 acpi_os_delete_semaphore(
803 struct semaphore *sem = (struct semaphore*) handle;
805 ACPI_FUNCTION_TRACE ("os_delete_semaphore");
808 return_ACPI_STATUS (AE_BAD_PARAMETER);
810 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
812 acpi_os_free(sem); sem = NULL;
814 return_ACPI_STATUS (AE_OK);
819 * TODO: The kernel doesn't have a 'down_timeout' function -- had to
820 * improvise. The process is to sleep for one scheduler quantum
821 * until the semaphore becomes available. Downside is that this
822 * may result in starvation for timeout-based waits when there's
823 * lots of semaphore activity.
825 * TODO: Support for units > 1?
828 acpi_os_wait_semaphore(
833 acpi_status status = AE_OK;
834 struct semaphore *sem = (struct semaphore*)handle;
837 ACPI_FUNCTION_TRACE ("os_wait_semaphore");
839 if (!sem || (units < 1))
840 return_ACPI_STATUS (AE_BAD_PARAMETER);
843 return_ACPI_STATUS (AE_SUPPORT);
845 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n", handle, units, timeout));
855 * A zero timeout value indicates that we shouldn't wait - just
856 * acquire the semaphore if available otherwise return AE_TIME
857 * (a.k.a. 'would block').
860 if(down_trylock(sem))
868 case ACPI_WAIT_FOREVER:
877 // TODO: A better timeout algorithm?
880 static const int quantum_ms = 1000/HZ;
882 ret = down_trylock(sem);
883 for (i = timeout; (i > 0 && ret < 0); i -= quantum_ms) {
884 current->state = TASK_INTERRUPTIBLE;
886 ret = down_trylock(sem);
895 if (ACPI_FAILURE(status)) {
896 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Failed to acquire semaphore[%p|%d|%d], %s\n",
897 handle, units, timeout, acpi_format_exception(status)));
900 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Acquired semaphore[%p|%d|%d]\n", handle, units, timeout));
903 return_ACPI_STATUS (status);
908 * TODO: Support for units > 1?
911 acpi_os_signal_semaphore(
915 struct semaphore *sem = (struct semaphore *) handle;
917 ACPI_FUNCTION_TRACE ("os_signal_semaphore");
919 if (!sem || (units < 1))
920 return_ACPI_STATUS (AE_BAD_PARAMETER);
923 return_ACPI_STATUS (AE_SUPPORT);
925 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle, units));
929 return_ACPI_STATUS (AE_OK);
933 acpi_os_get_line(char *buffer)
936 #ifdef ENABLE_DEBUGGER
937 if (acpi_in_debugger) {
940 kdb_read(buffer, sizeof(line_buf));
942 /* remove the CR kdb includes */
943 chars = strlen(buffer) - 1;
944 buffer[chars] = '\0';
952 * We just have to assume we're dealing with valid memory
956 acpi_os_readable(void *ptr, u32 len)
962 acpi_os_writable(void *ptr, u32 len)
968 acpi_os_get_thread_id (void)
983 case ACPI_SIGNAL_FATAL:
984 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
986 case ACPI_SIGNAL_BREAKPOINT:
988 char *bp_info = (char*) info;
990 printk(KERN_ERR "ACPI breakpoint: %s\n", bp_info);
1000 acpi_os_name_setup(char *str)
1002 char *p = acpi_os_name;
1003 int count = ACPI_MAX_OVERRIDE_LEN-1;
1008 for (; count-- && str && *str; str++) {
1009 if (isalnum(*str) || *str == ' ')
1011 else if (*str == '\'' || *str == '"')
1022 __setup("acpi_os_name=", acpi_os_name_setup);