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;
69 static struct workqueue_struct *kacpid_wq;
72 acpi_os_initialize(void)
75 * Initialize PCI configuration space access, as we'll need to access
76 * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
78 #ifdef CONFIG_ACPI_PCI
80 printk(KERN_ERR PREFIX "Access to PCI configuration space unavailable\n");
84 kacpid_wq = create_singlethread_workqueue("kacpid");
91 acpi_os_terminate(void)
93 if (acpi_irq_handler) {
94 acpi_os_remove_interrupt_handler(acpi_irq_irq,
98 destroy_workqueue(kacpid_wq);
104 acpi_os_printf(const char *fmt,...)
108 acpi_os_vprintf(fmt, args);
113 acpi_os_vprintf(const char *fmt, va_list args)
115 static char buffer[512];
117 vsprintf(buffer, fmt, args);
119 #ifdef ENABLE_DEBUGGER
120 if (acpi_in_debugger) {
121 kdb_printf("%s", buffer);
123 printk("%s", buffer);
126 printk("%s", buffer);
131 acpi_os_allocate(acpi_size size)
133 return kmalloc(size, GFP_KERNEL);
137 acpi_os_free(void *ptr)
143 acpi_os_get_root_pointer(u32 flags, struct acpi_pointer *addr)
146 addr->pointer_type = ACPI_PHYSICAL_POINTER;
148 addr->pointer.physical =
149 (acpi_physical_address) virt_to_phys(efi.acpi20);
151 addr->pointer.physical =
152 (acpi_physical_address) virt_to_phys(efi.acpi);
154 printk(KERN_ERR PREFIX "System description tables not found\n");
158 if (ACPI_FAILURE(acpi_find_root_pointer(flags, addr))) {
159 printk(KERN_ERR PREFIX "System description tables not found\n");
168 acpi_os_map_memory(acpi_physical_address phys, acpi_size size, void **virt)
171 if (EFI_MEMORY_WB & efi_mem_attributes(phys)) {
172 *virt = phys_to_virt(phys);
174 *virt = ioremap(phys, size);
177 if (phys > ULONG_MAX) {
178 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
179 return AE_BAD_PARAMETER;
182 * ioremap checks to ensure this is in reserved space
184 *virt = ioremap((unsigned long) phys, size);
194 acpi_os_unmap_memory(void *virt, acpi_size size)
200 acpi_os_get_physical_address(void *virt, acpi_physical_address *phys)
203 return AE_BAD_PARAMETER;
205 *phys = virt_to_phys(virt);
210 #define ACPI_MAX_OVERRIDE_LEN 100
212 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
215 acpi_os_predefined_override (const struct acpi_predefined_names *init_val,
216 acpi_string *new_val)
218 if (!init_val || !new_val)
219 return AE_BAD_PARAMETER;
222 if (!memcmp (init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
223 printk(KERN_INFO PREFIX "Overriding _OS definition %s\n",
225 *new_val = acpi_os_name;
232 acpi_os_table_override (struct acpi_table_header *existing_table,
233 struct acpi_table_header **new_table)
235 if (!existing_table || !new_table)
236 return AE_BAD_PARAMETER;
243 acpi_irq(int irq, void *dev_id, struct pt_regs *regs)
245 return (*acpi_irq_handler)(acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
249 acpi_os_install_interrupt_handler(u32 gsi, OSD_HANDLER handler, void *context)
254 * Ignore the GSI from the core, and use the value in our copy of the
255 * FADT. It may not be the same if an interrupt source override exists
258 gsi = acpi_fadt.sci_int;
259 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
260 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
265 acpi_irq_handler = handler;
266 acpi_irq_context = context;
267 if (request_irq(irq, acpi_irq, SA_SHIRQ, "acpi", acpi_irq)) {
268 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
269 return AE_NOT_ACQUIRED;
277 acpi_os_remove_interrupt_handler(u32 irq, OSD_HANDLER handler)
280 free_irq(irq, acpi_irq);
281 acpi_irq_handler = NULL;
289 * Running in interpreter thread context, safe to sleep
293 acpi_os_sleep(u32 sec, u32 ms)
295 current->state = TASK_INTERRUPTIBLE;
296 schedule_timeout(HZ * sec + (ms * HZ) / 1000);
300 acpi_os_stall(u32 us)
308 touch_nmi_watchdog();
315 acpi_io_address port,
327 *(u8*) value = inb(port);
330 *(u16*) value = inw(port);
333 *(u32*) value = inl(port);
344 acpi_io_address port,
368 acpi_physical_address phys_addr,
377 if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
378 virt_addr = phys_to_virt(phys_addr);
381 virt_addr = ioremap(phys_addr, width);
384 virt_addr = phys_to_virt(phys_addr);
390 *(u8*) value = *(u8*) virt_addr;
393 *(u16*) value = *(u16*) virt_addr;
396 *(u32*) value = *(u32*) virt_addr;
411 acpi_os_write_memory(
412 acpi_physical_address phys_addr,
420 if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
421 virt_addr = phys_to_virt(phys_addr);
424 virt_addr = ioremap(phys_addr, width);
427 virt_addr = phys_to_virt(phys_addr);
431 *(u8*) virt_addr = value;
434 *(u16*) virt_addr = value;
437 *(u32*) virt_addr = value;
449 #ifdef CONFIG_ACPI_PCI
452 acpi_os_read_pci_configuration (struct acpi_pci_id *pci_id, u32 reg, void *value, u32 width)
457 return AE_BAD_PARAMETER;
473 result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
474 PCI_DEVFN(pci_id->device, pci_id->function),
477 return (result ? AE_ERROR : AE_OK);
481 acpi_os_write_pci_configuration (struct acpi_pci_id *pci_id, u32 reg, acpi_integer value, u32 width)
499 result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
500 PCI_DEVFN(pci_id->device, pci_id->function),
503 return (result ? AE_ERROR : AE_OK);
506 /* TODO: Change code to take advantage of driver model more */
508 acpi_os_derive_pci_id_2 (
509 acpi_handle rhandle, /* upper bound */
510 acpi_handle chandle, /* current node */
511 struct acpi_pci_id **id,
516 struct acpi_pci_id *pci_id = *id;
519 acpi_object_type type;
522 acpi_get_parent(chandle, &handle);
523 if (handle != rhandle) {
524 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge, bus_number);
526 status = acpi_get_type(handle, &type);
527 if ( (ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE) )
530 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &temp);
531 if (ACPI_SUCCESS(status)) {
532 pci_id->device = ACPI_HIWORD (ACPI_LODWORD (temp));
533 pci_id->function = ACPI_LOWORD (ACPI_LODWORD (temp));
536 pci_id->bus = *bus_number;
538 /* any nicer way to get bus number of bridge ? */
539 status = acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8, 8);
540 if (ACPI_SUCCESS(status) &&
541 ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
542 status = acpi_os_read_pci_configuration(pci_id, 0x18, &tu8, 8);
543 if (!ACPI_SUCCESS(status)) {
544 /* Certainly broken... FIX ME */
549 status = acpi_os_read_pci_configuration(pci_id, 0x19, &tu8, 8);
550 if (ACPI_SUCCESS(status)) {
560 acpi_os_derive_pci_id (
561 acpi_handle rhandle, /* upper bound */
562 acpi_handle chandle, /* current node */
563 struct acpi_pci_id **id)
566 u8 bus_number = (*id)->bus;
568 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
571 #else /*!CONFIG_ACPI_PCI*/
574 acpi_os_write_pci_configuration (
575 struct acpi_pci_id *pci_id,
584 acpi_os_read_pci_configuration (
585 struct acpi_pci_id *pci_id,
594 acpi_os_derive_pci_id (
595 acpi_handle rhandle, /* upper bound */
596 acpi_handle chandle, /* current node */
597 struct acpi_pci_id **id)
601 #endif /*CONFIG_ACPI_PCI*/
604 acpi_os_execute_deferred (
607 struct acpi_os_dpc *dpc = NULL;
609 ACPI_FUNCTION_TRACE ("os_execute_deferred");
611 dpc = (struct acpi_os_dpc *) context;
613 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
617 dpc->function(dpc->context);
625 acpi_os_queue_for_execution(
627 OSD_EXECUTION_CALLBACK function,
630 acpi_status status = AE_OK;
631 struct acpi_os_dpc *dpc;
632 struct work_struct *task;
634 ACPI_FUNCTION_TRACE ("os_queue_for_execution");
636 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Scheduling function [%p(%p)] for deferred execution.\n", function, context));
639 return_ACPI_STATUS (AE_BAD_PARAMETER);
642 * Allocate/initialize DPC structure. Note that this memory will be
643 * freed by the callee. The kernel handles the tq_struct list in a
644 * way that allows us to also free its memory inside the callee.
645 * Because we may want to schedule several tasks with different
646 * parameters we can't use the approach some kernel code uses of
647 * having a static tq_struct.
648 * We can save time and code by allocating the DPC and tq_structs
649 * from the same memory.
652 dpc = kmalloc(sizeof(struct acpi_os_dpc)+sizeof(struct work_struct), GFP_ATOMIC);
654 return_ACPI_STATUS (AE_NO_MEMORY);
656 dpc->function = function;
657 dpc->context = context;
659 task = (void *)(dpc+1);
660 INIT_WORK(task, acpi_os_execute_deferred, (void*)dpc);
662 if (!queue_work(kacpid_wq, task)) {
663 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Call to queue_work() failed.\n"));
668 return_ACPI_STATUS (status);
672 acpi_os_wait_events_complete(
675 flush_workqueue(kacpid_wq);
679 * Allocate the memory for a spinlock and initialize it.
682 acpi_os_create_lock (
683 acpi_handle *out_handle)
685 spinlock_t *lock_ptr;
687 ACPI_FUNCTION_TRACE ("os_create_lock");
689 lock_ptr = acpi_os_allocate(sizeof(spinlock_t));
691 spin_lock_init(lock_ptr);
693 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Creating spinlock[%p].\n", lock_ptr));
695 *out_handle = lock_ptr;
697 return_ACPI_STATUS (AE_OK);
702 * Deallocate the memory for a spinlock.
705 acpi_os_delete_lock (
708 ACPI_FUNCTION_TRACE ("os_create_lock");
710 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Deleting spinlock[%p].\n", handle));
712 acpi_os_free(handle);
718 * Acquire a spinlock.
720 * handle is a pointer to the spinlock_t.
721 * flags is *not* the result of save_flags - it is an ACPI-specific flag variable
722 * that indicates whether we are at interrupt level.
725 acpi_os_acquire_lock (
729 ACPI_FUNCTION_TRACE ("os_acquire_lock");
731 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Acquiring spinlock[%p] from %s level\n", handle,
732 ((flags & ACPI_NOT_ISR) ? "non-interrupt" : "interrupt")));
734 if (flags & ACPI_NOT_ISR)
737 spin_lock((spinlock_t *)handle);
744 * Release a spinlock. See above.
747 acpi_os_release_lock (
751 ACPI_FUNCTION_TRACE ("os_release_lock");
753 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Releasing spinlock[%p] from %s level\n", handle,
754 ((flags & ACPI_NOT_ISR) ? "non-interrupt" : "interrupt")));
756 spin_unlock((spinlock_t *)handle);
758 if (flags & ACPI_NOT_ISR)
766 acpi_os_create_semaphore(
771 struct semaphore *sem = NULL;
773 ACPI_FUNCTION_TRACE ("os_create_semaphore");
775 sem = acpi_os_allocate(sizeof(struct semaphore));
777 return_ACPI_STATUS (AE_NO_MEMORY);
778 memset(sem, 0, sizeof(struct semaphore));
780 sema_init(sem, initial_units);
782 *handle = (acpi_handle*)sem;
784 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n", *handle, initial_units));
786 return_ACPI_STATUS (AE_OK);
791 * TODO: A better way to delete semaphores? Linux doesn't have a
792 * 'delete_semaphore()' function -- may result in an invalid
793 * pointer dereference for non-synchronized consumers. Should
794 * we at least check for blocked threads and signal/cancel them?
798 acpi_os_delete_semaphore(
801 struct semaphore *sem = (struct semaphore*) handle;
803 ACPI_FUNCTION_TRACE ("os_delete_semaphore");
806 return_ACPI_STATUS (AE_BAD_PARAMETER);
808 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
810 acpi_os_free(sem); sem = NULL;
812 return_ACPI_STATUS (AE_OK);
817 * TODO: The kernel doesn't have a 'down_timeout' function -- had to
818 * improvise. The process is to sleep for one scheduler quantum
819 * until the semaphore becomes available. Downside is that this
820 * may result in starvation for timeout-based waits when there's
821 * lots of semaphore activity.
823 * TODO: Support for units > 1?
826 acpi_os_wait_semaphore(
831 acpi_status status = AE_OK;
832 struct semaphore *sem = (struct semaphore*)handle;
835 ACPI_FUNCTION_TRACE ("os_wait_semaphore");
837 if (!sem || (units < 1))
838 return_ACPI_STATUS (AE_BAD_PARAMETER);
841 return_ACPI_STATUS (AE_SUPPORT);
843 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n", handle, units, timeout));
853 * A zero timeout value indicates that we shouldn't wait - just
854 * acquire the semaphore if available otherwise return AE_TIME
855 * (a.k.a. 'would block').
858 if(down_trylock(sem))
866 case ACPI_WAIT_FOREVER:
875 // TODO: A better timeout algorithm?
878 static const int quantum_ms = 1000/HZ;
880 ret = down_trylock(sem);
881 for (i = timeout; (i > 0 && ret < 0); i -= quantum_ms) {
882 current->state = TASK_INTERRUPTIBLE;
884 ret = down_trylock(sem);
893 if (ACPI_FAILURE(status)) {
894 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Failed to acquire semaphore[%p|%d|%d], %s\n",
895 handle, units, timeout, acpi_format_exception(status)));
898 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Acquired semaphore[%p|%d|%d]\n", handle, units, timeout));
901 return_ACPI_STATUS (status);
906 * TODO: Support for units > 1?
909 acpi_os_signal_semaphore(
913 struct semaphore *sem = (struct semaphore *) handle;
915 ACPI_FUNCTION_TRACE ("os_signal_semaphore");
917 if (!sem || (units < 1))
918 return_ACPI_STATUS (AE_BAD_PARAMETER);
921 return_ACPI_STATUS (AE_SUPPORT);
923 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle, units));
927 return_ACPI_STATUS (AE_OK);
931 acpi_os_get_line(char *buffer)
934 #ifdef ENABLE_DEBUGGER
935 if (acpi_in_debugger) {
938 kdb_read(buffer, sizeof(line_buf));
940 /* remove the CR kdb includes */
941 chars = strlen(buffer) - 1;
942 buffer[chars] = '\0';
949 /* Assumes no unreadable holes inbetween */
951 acpi_os_readable(void *ptr, acpi_size len)
953 #if defined(__i386__) || defined(__x86_64__)
955 return !__get_user(tmp, (char *)ptr) && !__get_user(tmp, (char *)ptr + len - 1);
961 acpi_os_writable(void *ptr, acpi_size len)
963 /* could do dummy write (racy) or a kernel page table lookup.
964 The later may be difficult at early boot when kmap doesn't work yet. */
969 acpi_os_get_thread_id (void)
984 case ACPI_SIGNAL_FATAL:
985 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
987 case ACPI_SIGNAL_BREAKPOINT:
989 char *bp_info = (char*) info;
991 printk(KERN_ERR "ACPI breakpoint: %s\n", bp_info);
1001 acpi_os_name_setup(char *str)
1003 char *p = acpi_os_name;
1004 int count = ACPI_MAX_OVERRIDE_LEN-1;
1009 for (; count-- && str && *str; str++) {
1010 if (isalnum(*str) || *str == ' ' || *str == ':')
1012 else if (*str == '\'' || *str == '"')
1023 __setup("acpi_os_name=", acpi_os_name_setup);
1027 * empty string disables _OSI
1028 * TBD additional string adds to _OSI
1031 acpi_osi_setup(char *str)
1033 if (str == NULL || *str == '\0') {
1034 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1035 acpi_gbl_create_osi_method = FALSE;
1039 printk(KERN_ERR PREFIX "_OSI additional string ignored -- %s\n", str);
1045 __setup("acpi_osi=", acpi_osi_setup);
1047 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1049 acpi_serialize_setup(char *str)
1051 printk(KERN_INFO PREFIX "serialize enabled\n");
1053 acpi_gbl_all_methods_serialized = TRUE;
1058 __setup("acpi_serialize", acpi_serialize_setup);
1061 * Wake and Run-Time GPES are expected to be separate.
1062 * We disable wake-GPEs at run-time to prevent spurious
1065 * However, if a system exists that shares Wake and
1066 * Run-time events on the same GPE this flag is available
1067 * to tell Linux to keep the wake-time GPEs enabled at run-time.
1070 acpi_leave_gpes_disabled_setup(char *str)
1072 printk(KERN_INFO PREFIX "leave wake GPEs disabled\n");
1074 acpi_gbl_leave_wake_gpes_disabled = TRUE;
1079 __setup("acpi_leave_gpes_disabled", acpi_leave_gpes_disabled_setup);