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;
256 vector = acpi_irq_to_vector(irq);
258 printk(KERN_ERR PREFIX "SCI (IRQ%d) not registerd\n", irq);
264 acpi_irq_handler = handler;
265 acpi_irq_context = context;
266 if (request_irq(irq, acpi_irq, SA_SHIRQ, "acpi", acpi_irq)) {
267 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
268 return AE_NOT_ACQUIRED;
275 acpi_os_remove_interrupt_handler(u32 irq, OSD_HANDLER handler)
277 if (acpi_irq_handler) {
279 irq = acpi_irq_to_vector(irq);
281 free_irq(irq, acpi_irq);
282 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)
312 acpi_io_address port,
324 *(u8*) value = inb(port);
327 *(u16*) value = inw(port);
330 *(u32*) value = inl(port);
341 acpi_io_address port,
365 acpi_physical_address phys_addr,
372 #ifdef CONFIG_ACPI_EFI
375 if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
376 virt_addr = phys_to_virt(phys_addr);
379 virt_addr = ioremap(phys_addr, width);
382 virt_addr = phys_to_virt(phys_addr);
389 *(u8*) value = *(u8*) virt_addr;
392 *(u16*) value = *(u16*) virt_addr;
395 *(u32*) value = *(u32*) virt_addr;
401 #ifdef CONFIG_ACPI_EFI
410 acpi_os_write_memory(
411 acpi_physical_address phys_addr,
417 #ifdef CONFIG_ACPI_EFI
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);
432 *(u8*) virt_addr = value;
435 *(u16*) virt_addr = value;
438 *(u32*) virt_addr = value;
444 #ifdef CONFIG_ACPI_EFI
452 #ifdef CONFIG_ACPI_PCI
455 acpi_os_read_pci_configuration (
456 struct acpi_pci_id *pci_id,
465 struct pci_controller ctrl;
469 return AE_BAD_PARAMETER;
485 bus.number = pci_id->bus;
487 ctrl.segment = pci_id->segment;
490 result = pci_root_ops->read(&bus, PCI_DEVFN(pci_id->device,
494 return (result ? AE_ERROR : AE_OK);
498 acpi_os_write_pci_configuration (
499 struct acpi_pci_id *pci_id,
508 struct pci_controller ctrl;
525 bus.number = pci_id->bus;
527 ctrl.segment = pci_id->segment;
530 result = pci_root_ops->write(&bus, PCI_DEVFN(pci_id->device,
533 return (result ? AE_ERROR : AE_OK);
536 /* TODO: Change code to take advantage of driver model more */
538 acpi_os_derive_pci_id_2 (
539 acpi_handle rhandle, /* upper bound */
540 acpi_handle chandle, /* current node */
541 struct acpi_pci_id **id,
546 struct acpi_pci_id *pci_id = *id;
549 acpi_object_type type;
552 acpi_get_parent(chandle, &handle);
553 if (handle != rhandle) {
554 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge, bus_number);
556 status = acpi_get_type(handle, &type);
557 if ( (ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE) )
560 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &temp);
561 if (ACPI_SUCCESS(status)) {
562 pci_id->device = ACPI_HIWORD (ACPI_LODWORD (temp));
563 pci_id->function = ACPI_LOWORD (ACPI_LODWORD (temp));
566 pci_id->bus = *bus_number;
568 /* any nicer way to get bus number of bridge ? */
569 status = acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8, 8);
570 if (ACPI_SUCCESS(status) &&
571 ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
572 status = acpi_os_read_pci_configuration(pci_id, 0x18, &tu8, 8);
573 if (!ACPI_SUCCESS(status)) {
574 /* Certainly broken... FIX ME */
579 status = acpi_os_read_pci_configuration(pci_id, 0x19, &tu8, 8);
580 if (ACPI_SUCCESS(status)) {
590 acpi_os_derive_pci_id (
591 acpi_handle rhandle, /* upper bound */
592 acpi_handle chandle, /* current node */
593 struct acpi_pci_id **id)
596 u8 bus_number = (*id)->bus;
598 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
601 #else /*!CONFIG_ACPI_PCI*/
604 acpi_os_write_pci_configuration (
605 struct acpi_pci_id *pci_id,
614 acpi_os_read_pci_configuration (
615 struct acpi_pci_id *pci_id,
624 acpi_os_derive_pci_id (
625 acpi_handle rhandle, /* upper bound */
626 acpi_handle chandle, /* current node */
627 struct acpi_pci_id **id)
631 #endif /*CONFIG_ACPI_PCI*/
634 acpi_os_execute_deferred (
637 struct acpi_os_dpc *dpc = NULL;
639 ACPI_FUNCTION_TRACE ("os_execute_deferred");
641 dpc = (struct acpi_os_dpc *) context;
643 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
647 dpc->function(dpc->context);
655 acpi_os_queue_for_execution(
657 OSD_EXECUTION_CALLBACK function,
660 acpi_status status = AE_OK;
661 struct acpi_os_dpc *dpc;
662 struct work_struct *task;
664 ACPI_FUNCTION_TRACE ("os_queue_for_execution");
666 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Scheduling function [%p(%p)] for deferred execution.\n", function, context));
669 return_ACPI_STATUS (AE_BAD_PARAMETER);
672 * Allocate/initialize DPC structure. Note that this memory will be
673 * freed by the callee. The kernel handles the tq_struct list in a
674 * way that allows us to also free its memory inside the callee.
675 * Because we may want to schedule several tasks with different
676 * parameters we can't use the approach some kernel code uses of
677 * having a static tq_struct.
678 * We can save time and code by allocating the DPC and tq_structs
679 * from the same memory.
682 dpc = kmalloc(sizeof(struct acpi_os_dpc)+sizeof(struct work_struct), GFP_ATOMIC);
684 return_ACPI_STATUS (AE_NO_MEMORY);
686 dpc->function = function;
687 dpc->context = context;
689 task = (void *)(dpc+1);
690 INIT_WORK(task, acpi_os_execute_deferred, (void*)dpc);
692 if (!schedule_work(task)) {
693 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Call to schedule_work() failed.\n"));
698 return_ACPI_STATUS (status);
702 * Allocate the memory for a spinlock and initialize it.
705 acpi_os_create_lock (
706 acpi_handle *out_handle)
708 spinlock_t *lock_ptr;
710 ACPI_FUNCTION_TRACE ("os_create_lock");
712 lock_ptr = acpi_os_allocate(sizeof(spinlock_t));
714 spin_lock_init(lock_ptr);
716 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Creating spinlock[%p].\n", lock_ptr));
718 *out_handle = lock_ptr;
720 return_ACPI_STATUS (AE_OK);
725 * Deallocate the memory for a spinlock.
728 acpi_os_delete_lock (
731 ACPI_FUNCTION_TRACE ("os_create_lock");
733 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Deleting spinlock[%p].\n", handle));
735 acpi_os_free(handle);
741 * Acquire a spinlock.
743 * handle is a pointer to the spinlock_t.
744 * flags is *not* the result of save_flags - it is an ACPI-specific flag variable
745 * that indicates whether we are at interrupt level.
748 acpi_os_acquire_lock (
752 ACPI_FUNCTION_TRACE ("os_acquire_lock");
754 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Acquiring spinlock[%p] from %s level\n", handle,
755 ((flags & ACPI_NOT_ISR) ? "non-interrupt" : "interrupt")));
757 if (flags & ACPI_NOT_ISR)
760 spin_lock((spinlock_t *)handle);
767 * Release a spinlock. See above.
770 acpi_os_release_lock (
774 ACPI_FUNCTION_TRACE ("os_release_lock");
776 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Releasing spinlock[%p] from %s level\n", handle,
777 ((flags & ACPI_NOT_ISR) ? "non-interrupt" : "interrupt")));
779 spin_unlock((spinlock_t *)handle);
781 if (flags & ACPI_NOT_ISR)
789 acpi_os_create_semaphore(
794 struct semaphore *sem = NULL;
796 ACPI_FUNCTION_TRACE ("os_create_semaphore");
798 sem = acpi_os_allocate(sizeof(struct semaphore));
800 return_ACPI_STATUS (AE_NO_MEMORY);
801 memset(sem, 0, sizeof(struct semaphore));
803 sema_init(sem, initial_units);
805 *handle = (acpi_handle*)sem;
807 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n", *handle, initial_units));
809 return_ACPI_STATUS (AE_OK);
814 * TODO: A better way to delete semaphores? Linux doesn't have a
815 * 'delete_semaphore()' function -- may result in an invalid
816 * pointer dereference for non-synchronized consumers. Should
817 * we at least check for blocked threads and signal/cancel them?
821 acpi_os_delete_semaphore(
824 struct semaphore *sem = (struct semaphore*) handle;
826 ACPI_FUNCTION_TRACE ("os_delete_semaphore");
829 return_ACPI_STATUS (AE_BAD_PARAMETER);
831 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
833 acpi_os_free(sem); sem = NULL;
835 return_ACPI_STATUS (AE_OK);
840 * TODO: The kernel doesn't have a 'down_timeout' function -- had to
841 * improvise. The process is to sleep for one scheduler quantum
842 * until the semaphore becomes available. Downside is that this
843 * may result in starvation for timeout-based waits when there's
844 * lots of semaphore activity.
846 * TODO: Support for units > 1?
849 acpi_os_wait_semaphore(
854 acpi_status status = AE_OK;
855 struct semaphore *sem = (struct semaphore*)handle;
858 ACPI_FUNCTION_TRACE ("os_wait_semaphore");
860 if (!sem || (units < 1))
861 return_ACPI_STATUS (AE_BAD_PARAMETER);
864 return_ACPI_STATUS (AE_SUPPORT);
866 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n", handle, units, timeout));
876 * A zero timeout value indicates that we shouldn't wait - just
877 * acquire the semaphore if available otherwise return AE_TIME
878 * (a.k.a. 'would block').
881 if(down_trylock(sem))
889 case ACPI_WAIT_FOREVER:
898 // TODO: A better timeout algorithm?
901 static const int quantum_ms = 1000/HZ;
903 ret = down_trylock(sem);
904 for (i = timeout; (i > 0 && ret < 0); i -= quantum_ms) {
905 current->state = TASK_INTERRUPTIBLE;
907 ret = down_trylock(sem);
916 if (ACPI_FAILURE(status)) {
917 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Failed to acquire semaphore[%p|%d|%d], %s\n",
918 handle, units, timeout, acpi_format_exception(status)));
921 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Acquired semaphore[%p|%d|%d]\n", handle, units, timeout));
924 return_ACPI_STATUS (status);
929 * TODO: Support for units > 1?
932 acpi_os_signal_semaphore(
936 struct semaphore *sem = (struct semaphore *) handle;
938 ACPI_FUNCTION_TRACE ("os_signal_semaphore");
940 if (!sem || (units < 1))
941 return_ACPI_STATUS (AE_BAD_PARAMETER);
944 return_ACPI_STATUS (AE_SUPPORT);
946 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle, units));
950 return_ACPI_STATUS (AE_OK);
954 acpi_os_get_line(char *buffer)
957 #ifdef ENABLE_DEBUGGER
958 if (acpi_in_debugger) {
961 kdb_read(buffer, sizeof(line_buf));
963 /* remove the CR kdb includes */
964 chars = strlen(buffer) - 1;
965 buffer[chars] = '\0';
973 * We just have to assume we're dealing with valid memory
977 acpi_os_readable(void *ptr, u32 len)
983 acpi_os_writable(void *ptr, u32 len)
989 acpi_os_get_thread_id (void)
1004 case ACPI_SIGNAL_FATAL:
1005 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1007 case ACPI_SIGNAL_BREAKPOINT:
1009 char *bp_info = (char*) info;
1011 printk(KERN_ERR "ACPI breakpoint: %s\n", bp_info);
1021 acpi_os_name_setup(char *str)
1023 char *p = acpi_os_name;
1024 int count = ACPI_MAX_OVERRIDE_LEN-1;
1029 for (; count-- && str && *str; str++) {
1030 if (isalnum(*str) || *str == ' ')
1032 else if (*str == '\'' || *str == '"')
1043 __setup("acpi_os_name=", acpi_os_name_setup);