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 <linux/nmi.h>
39 #include <acpi/acpi.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: "
54 OSD_EXECUTION_CALLBACK function;
59 #ifdef ENABLE_DEBUGGER
60 #include <linux/kdb.h>
61 /* stuff for debugger support */
63 extern char line_buf[80];
64 #endif /*ENABLE_DEBUGGER*/
66 static unsigned int acpi_irq_irq;
67 static OSD_HANDLER acpi_irq_handler;
68 static void *acpi_irq_context;
71 acpi_os_initialize(void)
74 * Initialize PCI configuration space access, as we'll need to access
75 * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
77 #ifdef CONFIG_ACPI_PCI
79 printk(KERN_ERR PREFIX "Access to PCI configuration space unavailable\n");
88 acpi_os_terminate(void)
90 if (acpi_irq_handler) {
91 acpi_os_remove_interrupt_handler(acpi_irq_irq,
99 acpi_os_printf(const char *fmt,...)
103 acpi_os_vprintf(fmt, args);
108 acpi_os_vprintf(const char *fmt, va_list args)
110 static char buffer[512];
112 vsprintf(buffer, fmt, args);
114 #ifdef ENABLE_DEBUGGER
115 if (acpi_in_debugger) {
116 kdb_printf("%s", buffer);
118 printk("%s", buffer);
121 printk("%s", buffer);
126 acpi_os_allocate(acpi_size size)
128 return kmalloc(size, GFP_KERNEL);
132 acpi_os_free(void *ptr)
138 acpi_os_get_root_pointer(u32 flags, struct acpi_pointer *addr)
141 addr->pointer_type = ACPI_PHYSICAL_POINTER;
143 addr->pointer.physical =
144 (acpi_physical_address) virt_to_phys(efi.acpi20);
146 addr->pointer.physical =
147 (acpi_physical_address) virt_to_phys(efi.acpi);
149 printk(KERN_ERR PREFIX "System description tables not found\n");
153 if (ACPI_FAILURE(acpi_find_root_pointer(flags, addr))) {
154 printk(KERN_ERR PREFIX "System description tables not found\n");
163 acpi_os_map_memory(acpi_physical_address phys, acpi_size size, void **virt)
166 if (EFI_MEMORY_WB & efi_mem_attributes(phys)) {
167 *virt = phys_to_virt(phys);
169 *virt = ioremap(phys, size);
172 if (phys > ULONG_MAX) {
173 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
174 return AE_BAD_PARAMETER;
177 * ioremap checks to ensure this is in reserved space
179 *virt = ioremap((unsigned long) phys, size);
189 acpi_os_unmap_memory(void *virt, acpi_size size)
195 acpi_os_get_physical_address(void *virt, acpi_physical_address *phys)
198 return AE_BAD_PARAMETER;
200 *phys = virt_to_phys(virt);
205 #define ACPI_MAX_OVERRIDE_LEN 100
207 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
210 acpi_os_predefined_override (const struct acpi_predefined_names *init_val,
211 acpi_string *new_val)
213 if (!init_val || !new_val)
214 return AE_BAD_PARAMETER;
217 if (!memcmp (init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
218 printk(KERN_INFO PREFIX "Overriding _OS definition %s\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) ? IRQ_HANDLED : IRQ_NONE;
244 acpi_os_install_interrupt_handler(u32 gsi, OSD_HANDLER handler, void *context)
249 * Ignore the GSI from the core, and use the value in our copy of the
250 * FADT. It may not be the same if an interrupt source override exists
253 gsi = acpi_fadt.sci_int;
254 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
255 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
260 acpi_irq_handler = handler;
261 acpi_irq_context = context;
262 if (request_irq(irq, acpi_irq, SA_SHIRQ, "acpi", acpi_irq)) {
263 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
264 return AE_NOT_ACQUIRED;
272 acpi_os_remove_interrupt_handler(u32 irq, OSD_HANDLER handler)
275 free_irq(irq, acpi_irq);
276 acpi_irq_handler = NULL;
284 * Running in interpreter thread context, safe to sleep
288 acpi_os_sleep(u32 sec, u32 ms)
290 current->state = TASK_INTERRUPTIBLE;
291 schedule_timeout(HZ * sec + (ms * HZ) / 1000);
295 acpi_os_stall(u32 us)
303 touch_nmi_watchdog();
310 acpi_io_address port,
322 *(u8*) value = inb(port);
325 *(u16*) value = inw(port);
328 *(u32*) value = inl(port);
339 acpi_io_address port,
363 acpi_physical_address phys_addr,
372 if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
373 virt_addr = phys_to_virt(phys_addr);
376 virt_addr = ioremap(phys_addr, width);
379 virt_addr = phys_to_virt(phys_addr);
385 *(u8*) value = *(u8*) virt_addr;
388 *(u16*) value = *(u16*) virt_addr;
391 *(u32*) value = *(u32*) virt_addr;
406 acpi_os_write_memory(
407 acpi_physical_address phys_addr,
415 if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
416 virt_addr = phys_to_virt(phys_addr);
419 virt_addr = ioremap(phys_addr, width);
422 virt_addr = phys_to_virt(phys_addr);
426 *(u8*) virt_addr = value;
429 *(u16*) virt_addr = value;
432 *(u32*) virt_addr = value;
444 #ifdef CONFIG_ACPI_PCI
447 acpi_os_read_pci_configuration (struct acpi_pci_id *pci_id, u32 reg, void *value, u32 width)
452 return AE_BAD_PARAMETER;
468 result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
469 PCI_DEVFN(pci_id->device, pci_id->function),
472 return (result ? AE_ERROR : AE_OK);
476 acpi_os_write_pci_configuration (struct acpi_pci_id *pci_id, u32 reg, acpi_integer value, u32 width)
494 result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
495 PCI_DEVFN(pci_id->device, pci_id->function),
498 return (result ? AE_ERROR : AE_OK);
501 /* TODO: Change code to take advantage of driver model more */
503 acpi_os_derive_pci_id_2 (
504 acpi_handle rhandle, /* upper bound */
505 acpi_handle chandle, /* current node */
506 struct acpi_pci_id **id,
511 struct acpi_pci_id *pci_id = *id;
514 acpi_object_type type;
517 acpi_get_parent(chandle, &handle);
518 if (handle != rhandle) {
519 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge, bus_number);
521 status = acpi_get_type(handle, &type);
522 if ( (ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE) )
525 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &temp);
526 if (ACPI_SUCCESS(status)) {
527 pci_id->device = ACPI_HIWORD (ACPI_LODWORD (temp));
528 pci_id->function = ACPI_LOWORD (ACPI_LODWORD (temp));
531 pci_id->bus = *bus_number;
533 /* any nicer way to get bus number of bridge ? */
534 status = acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8, 8);
535 if (ACPI_SUCCESS(status) &&
536 ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
537 status = acpi_os_read_pci_configuration(pci_id, 0x18, &tu8, 8);
538 if (!ACPI_SUCCESS(status)) {
539 /* Certainly broken... FIX ME */
544 status = acpi_os_read_pci_configuration(pci_id, 0x19, &tu8, 8);
545 if (ACPI_SUCCESS(status)) {
555 acpi_os_derive_pci_id (
556 acpi_handle rhandle, /* upper bound */
557 acpi_handle chandle, /* current node */
558 struct acpi_pci_id **id)
561 u8 bus_number = (*id)->bus;
563 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
566 #else /*!CONFIG_ACPI_PCI*/
569 acpi_os_write_pci_configuration (
570 struct acpi_pci_id *pci_id,
579 acpi_os_read_pci_configuration (
580 struct acpi_pci_id *pci_id,
589 acpi_os_derive_pci_id (
590 acpi_handle rhandle, /* upper bound */
591 acpi_handle chandle, /* current node */
592 struct acpi_pci_id **id)
596 #endif /*CONFIG_ACPI_PCI*/
599 acpi_os_execute_deferred (
602 struct acpi_os_dpc *dpc = NULL;
604 ACPI_FUNCTION_TRACE ("os_execute_deferred");
606 dpc = (struct acpi_os_dpc *) context;
608 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
612 dpc->function(dpc->context);
620 acpi_os_queue_for_execution(
622 OSD_EXECUTION_CALLBACK function,
625 acpi_status status = AE_OK;
626 struct acpi_os_dpc *dpc;
627 struct work_struct *task;
629 ACPI_FUNCTION_TRACE ("os_queue_for_execution");
631 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Scheduling function [%p(%p)] for deferred execution.\n", function, context));
634 return_ACPI_STATUS (AE_BAD_PARAMETER);
637 * Allocate/initialize DPC structure. Note that this memory will be
638 * freed by the callee. The kernel handles the tq_struct list in a
639 * way that allows us to also free its memory inside the callee.
640 * Because we may want to schedule several tasks with different
641 * parameters we can't use the approach some kernel code uses of
642 * having a static tq_struct.
643 * We can save time and code by allocating the DPC and tq_structs
644 * from the same memory.
647 dpc = kmalloc(sizeof(struct acpi_os_dpc)+sizeof(struct work_struct), GFP_ATOMIC);
649 return_ACPI_STATUS (AE_NO_MEMORY);
651 dpc->function = function;
652 dpc->context = context;
654 task = (void *)(dpc+1);
655 INIT_WORK(task, acpi_os_execute_deferred, (void*)dpc);
657 if (!schedule_work(task)) {
658 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Call to schedule_work() failed.\n"));
663 return_ACPI_STATUS (status);
667 * Allocate the memory for a spinlock and initialize it.
670 acpi_os_create_lock (
671 acpi_handle *out_handle)
673 spinlock_t *lock_ptr;
675 ACPI_FUNCTION_TRACE ("os_create_lock");
677 lock_ptr = acpi_os_allocate(sizeof(spinlock_t));
679 spin_lock_init(lock_ptr);
681 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Creating spinlock[%p].\n", lock_ptr));
683 *out_handle = lock_ptr;
685 return_ACPI_STATUS (AE_OK);
690 * Deallocate the memory for a spinlock.
693 acpi_os_delete_lock (
696 ACPI_FUNCTION_TRACE ("os_create_lock");
698 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Deleting spinlock[%p].\n", handle));
700 acpi_os_free(handle);
706 * Acquire a spinlock.
708 * handle is a pointer to the spinlock_t.
709 * flags is *not* the result of save_flags - it is an ACPI-specific flag variable
710 * that indicates whether we are at interrupt level.
713 acpi_os_acquire_lock (
717 ACPI_FUNCTION_TRACE ("os_acquire_lock");
719 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Acquiring spinlock[%p] from %s level\n", handle,
720 ((flags & ACPI_NOT_ISR) ? "non-interrupt" : "interrupt")));
722 if (flags & ACPI_NOT_ISR)
725 spin_lock((spinlock_t *)handle);
732 * Release a spinlock. See above.
735 acpi_os_release_lock (
739 ACPI_FUNCTION_TRACE ("os_release_lock");
741 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Releasing spinlock[%p] from %s level\n", handle,
742 ((flags & ACPI_NOT_ISR) ? "non-interrupt" : "interrupt")));
744 spin_unlock((spinlock_t *)handle);
746 if (flags & ACPI_NOT_ISR)
754 acpi_os_create_semaphore(
759 struct semaphore *sem = NULL;
761 ACPI_FUNCTION_TRACE ("os_create_semaphore");
763 sem = acpi_os_allocate(sizeof(struct semaphore));
765 return_ACPI_STATUS (AE_NO_MEMORY);
766 memset(sem, 0, sizeof(struct semaphore));
768 sema_init(sem, initial_units);
770 *handle = (acpi_handle*)sem;
772 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n", *handle, initial_units));
774 return_ACPI_STATUS (AE_OK);
779 * TODO: A better way to delete semaphores? Linux doesn't have a
780 * 'delete_semaphore()' function -- may result in an invalid
781 * pointer dereference for non-synchronized consumers. Should
782 * we at least check for blocked threads and signal/cancel them?
786 acpi_os_delete_semaphore(
789 struct semaphore *sem = (struct semaphore*) handle;
791 ACPI_FUNCTION_TRACE ("os_delete_semaphore");
794 return_ACPI_STATUS (AE_BAD_PARAMETER);
796 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
798 acpi_os_free(sem); sem = NULL;
800 return_ACPI_STATUS (AE_OK);
805 * TODO: The kernel doesn't have a 'down_timeout' function -- had to
806 * improvise. The process is to sleep for one scheduler quantum
807 * until the semaphore becomes available. Downside is that this
808 * may result in starvation for timeout-based waits when there's
809 * lots of semaphore activity.
811 * TODO: Support for units > 1?
814 acpi_os_wait_semaphore(
819 acpi_status status = AE_OK;
820 struct semaphore *sem = (struct semaphore*)handle;
823 ACPI_FUNCTION_TRACE ("os_wait_semaphore");
825 if (!sem || (units < 1))
826 return_ACPI_STATUS (AE_BAD_PARAMETER);
829 return_ACPI_STATUS (AE_SUPPORT);
831 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n", handle, units, timeout));
841 * A zero timeout value indicates that we shouldn't wait - just
842 * acquire the semaphore if available otherwise return AE_TIME
843 * (a.k.a. 'would block').
846 if(down_trylock(sem))
854 case ACPI_WAIT_FOREVER:
863 // TODO: A better timeout algorithm?
866 static const int quantum_ms = 1000/HZ;
868 ret = down_trylock(sem);
869 for (i = timeout; (i > 0 && ret < 0); i -= quantum_ms) {
870 current->state = TASK_INTERRUPTIBLE;
872 ret = down_trylock(sem);
881 if (ACPI_FAILURE(status)) {
882 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Failed to acquire semaphore[%p|%d|%d], %s\n",
883 handle, units, timeout, acpi_format_exception(status)));
886 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Acquired semaphore[%p|%d|%d]\n", handle, units, timeout));
889 return_ACPI_STATUS (status);
894 * TODO: Support for units > 1?
897 acpi_os_signal_semaphore(
901 struct semaphore *sem = (struct semaphore *) handle;
903 ACPI_FUNCTION_TRACE ("os_signal_semaphore");
905 if (!sem || (units < 1))
906 return_ACPI_STATUS (AE_BAD_PARAMETER);
909 return_ACPI_STATUS (AE_SUPPORT);
911 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle, units));
915 return_ACPI_STATUS (AE_OK);
919 acpi_os_get_line(char *buffer)
922 #ifdef ENABLE_DEBUGGER
923 if (acpi_in_debugger) {
926 kdb_read(buffer, sizeof(line_buf));
928 /* remove the CR kdb includes */
929 chars = strlen(buffer) - 1;
930 buffer[chars] = '\0';
937 /* Assumes no unreadable holes inbetween */
939 acpi_os_readable(void *ptr, acpi_size len)
941 #if defined(__i386__) || defined(__x86_64__)
943 return !__get_user(tmp, (char *)ptr) && !__get_user(tmp, (char *)ptr + len - 1);
949 acpi_os_writable(void *ptr, acpi_size len)
951 /* could do dummy write (racy) or a kernel page table lookup.
952 The later may be difficult at early boot when kmap doesn't work yet. */
957 acpi_os_get_thread_id (void)
972 case ACPI_SIGNAL_FATAL:
973 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
975 case ACPI_SIGNAL_BREAKPOINT:
977 char *bp_info = (char*) info;
979 printk(KERN_ERR "ACPI breakpoint: %s\n", bp_info);
989 acpi_os_name_setup(char *str)
991 char *p = acpi_os_name;
992 int count = ACPI_MAX_OVERRIDE_LEN-1;
997 for (; count-- && str && *str; str++) {
998 if (isalnum(*str) || *str == ' ' || *str == ':')
1000 else if (*str == '\'' || *str == '"')
1011 __setup("acpi_os_name=", acpi_os_name_setup);
1015 * empty string disables _OSI
1016 * TBD additional string adds to _OSI
1019 acpi_osi_setup(char *str)
1021 if (str == NULL || *str == '\0') {
1022 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1023 acpi_gbl_create_osi_method = FALSE;
1027 printk(KERN_ERR PREFIX "_OSI additional string ignored -- %s\n", str);
1033 __setup("acpi_osi=", acpi_osi_setup);
1035 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1037 acpi_serialize_setup(char *str)
1039 printk(KERN_INFO PREFIX "serialize enabled\n");
1041 acpi_gbl_all_methods_serialized = TRUE;
1046 __setup("acpi_serialize", acpi_serialize_setup);
1049 * Wake and Run-Time GPES are expected to be separate.
1050 * We disable wake-GPEs at run-time to prevent spurious
1053 * However, if a system exists that shares Wake and
1054 * Run-time events on the same GPE this flag is available
1055 * to tell Linux to keep the wake-time GPEs enabled at run-time.
1058 acpi_leave_gpes_disabled_setup(char *str)
1060 printk(KERN_INFO PREFIX "leave wake GPEs disabled\n");
1062 acpi_gbl_leave_wake_gpes_disabled = TRUE;
1067 __setup("acpi_leave_gpes_disabled", acpi_leave_gpes_disabled_setup);