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 acpi_osd_exec_callback function
;
58 #ifdef CONFIG_ACPI_CUSTOM_DSDT
59 #include CONFIG_ACPI_CUSTOM_DSDT_FILE
62 #ifdef ENABLE_DEBUGGER
63 #include <linux/kdb.h>
64 /* stuff for debugger support */
66 extern char line_buf
[80];
67 #endif /*ENABLE_DEBUGGER*/
69 static unsigned int acpi_irq_irq
;
70 static acpi_osd_handler acpi_irq_handler
;
71 static void *acpi_irq_context
;
72 static struct workqueue_struct
*kacpid_wq
;
75 acpi_os_initialize(void)
81 acpi_os_initialize1(void)
84 * Initialize PCI configuration space access, as we'll need to access
85 * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
87 #ifdef CONFIG_ACPI_PCI
89 printk(KERN_ERR PREFIX
"Access to PCI configuration space unavailable\n");
93 kacpid_wq
= create_singlethread_workqueue("kacpid");
100 acpi_os_terminate(void)
102 if (acpi_irq_handler
) {
103 acpi_os_remove_interrupt_handler(acpi_irq_irq
,
107 destroy_workqueue(kacpid_wq
);
113 acpi_os_printf(const char *fmt
,...)
117 acpi_os_vprintf(fmt
, args
);
122 acpi_os_vprintf(const char *fmt
, va_list args
)
124 static char buffer
[512];
126 vsprintf(buffer
, fmt
, args
);
128 #ifdef ENABLE_DEBUGGER
129 if (acpi_in_debugger
) {
130 kdb_printf("%s", buffer
);
132 printk("%s", buffer
);
135 printk("%s", buffer
);
140 acpi_os_allocate(acpi_size size
)
142 return kmalloc(size
, GFP_KERNEL
);
146 acpi_os_free(void *ptr
)
152 acpi_os_get_root_pointer(u32 flags
, struct acpi_pointer
*addr
)
155 addr
->pointer_type
= ACPI_PHYSICAL_POINTER
;
157 addr
->pointer
.physical
=
158 (acpi_physical_address
) virt_to_phys(efi
.acpi20
);
160 addr
->pointer
.physical
=
161 (acpi_physical_address
) virt_to_phys(efi
.acpi
);
163 printk(KERN_ERR PREFIX
"System description tables not found\n");
167 if (ACPI_FAILURE(acpi_find_root_pointer(flags
, addr
))) {
168 printk(KERN_ERR PREFIX
"System description tables not found\n");
177 acpi_os_map_memory(acpi_physical_address phys
, acpi_size size
, void __iomem
**virt
)
180 if (EFI_MEMORY_WB
& efi_mem_attributes(phys
)) {
181 *virt
= (void __iomem
*) phys_to_virt(phys
);
183 *virt
= ioremap(phys
, size
);
186 if (phys
> ULONG_MAX
) {
187 printk(KERN_ERR PREFIX
"Cannot map memory that high\n");
188 return AE_BAD_PARAMETER
;
191 * ioremap checks to ensure this is in reserved space
193 *virt
= ioremap((unsigned long) phys
, size
);
203 acpi_os_unmap_memory(void __iomem
*virt
, acpi_size size
)
209 acpi_os_get_physical_address(void *virt
, acpi_physical_address
*phys
)
212 return AE_BAD_PARAMETER
;
214 *phys
= virt_to_phys(virt
);
219 #define ACPI_MAX_OVERRIDE_LEN 100
221 static char acpi_os_name
[ACPI_MAX_OVERRIDE_LEN
];
224 acpi_os_predefined_override (const struct acpi_predefined_names
*init_val
,
225 acpi_string
*new_val
)
227 if (!init_val
|| !new_val
)
228 return AE_BAD_PARAMETER
;
231 if (!memcmp (init_val
->name
, "_OS_", 4) && strlen(acpi_os_name
)) {
232 printk(KERN_INFO PREFIX
"Overriding _OS definition %s\n",
234 *new_val
= acpi_os_name
;
241 acpi_os_table_override (struct acpi_table_header
*existing_table
,
242 struct acpi_table_header
**new_table
)
244 if (!existing_table
|| !new_table
)
245 return AE_BAD_PARAMETER
;
247 #ifdef CONFIG_ACPI_CUSTOM_DSDT
248 if (strncmp(existing_table
->signature
, "DSDT", 4) == 0)
249 *new_table
= (struct acpi_table_header
*)AmlCode
;
259 acpi_irq(int irq
, void *dev_id
, struct pt_regs
*regs
)
261 return (*acpi_irq_handler
)(acpi_irq_context
) ? IRQ_HANDLED
: IRQ_NONE
;
265 acpi_os_install_interrupt_handler(u32 gsi
, acpi_osd_handler handler
, void *context
)
270 * Ignore the GSI from the core, and use the value in our copy of the
271 * FADT. It may not be the same if an interrupt source override exists
274 gsi
= acpi_fadt
.sci_int
;
275 if (acpi_gsi_to_irq(gsi
, &irq
) < 0) {
276 printk(KERN_ERR PREFIX
"SCI (ACPI GSI %d) not registered\n",
281 acpi_irq_handler
= handler
;
282 acpi_irq_context
= context
;
283 if (request_irq(irq
, acpi_irq
, SA_SHIRQ
, "acpi", acpi_irq
)) {
284 printk(KERN_ERR PREFIX
"SCI (IRQ%d) allocation failed\n", irq
);
285 return AE_NOT_ACQUIRED
;
293 acpi_os_remove_interrupt_handler(u32 irq
, acpi_osd_handler handler
)
296 free_irq(irq
, acpi_irq
);
297 acpi_irq_handler
= NULL
;
305 * Running in interpreter thread context, safe to sleep
309 acpi_os_sleep(u32 sec
, u32 ms
)
311 current
->state
= TASK_INTERRUPTIBLE
;
312 schedule_timeout(HZ
* sec
+ (ms
* HZ
) / 1000);
316 acpi_os_stall(u32 us
)
324 touch_nmi_watchdog();
331 acpi_io_address port
,
343 *(u8
*) value
= inb(port
);
346 *(u16
*) value
= inw(port
);
349 *(u32
*) value
= inl(port
);
360 acpi_io_address port
,
384 acpi_physical_address phys_addr
,
389 void __iomem
*virt_addr
;
393 if (EFI_MEMORY_WB
& efi_mem_attributes(phys_addr
)) {
394 /* HACK ALERT! We can use readb/w/l on real memory too.. */
395 virt_addr
= (void __iomem
*) phys_to_virt(phys_addr
);
398 virt_addr
= ioremap(phys_addr
, width
);
401 virt_addr
= (void __iomem
*) phys_to_virt(phys_addr
);
407 *(u8
*) value
= readb(virt_addr
);
410 *(u16
*) value
= readw(virt_addr
);
413 *(u32
*) value
= readl(virt_addr
);
428 acpi_os_write_memory(
429 acpi_physical_address phys_addr
,
433 void __iomem
*virt_addr
;
437 if (EFI_MEMORY_WB
& efi_mem_attributes(phys_addr
)) {
438 /* HACK ALERT! We can use writeb/w/l on real memory too */
439 virt_addr
= (void __iomem
*) phys_to_virt(phys_addr
);
442 virt_addr
= ioremap(phys_addr
, width
);
445 virt_addr
= (void __iomem
*) phys_to_virt(phys_addr
);
449 writeb(value
, virt_addr
);
452 writew(value
, virt_addr
);
455 writel(value
, virt_addr
);
467 #ifdef CONFIG_ACPI_PCI
470 acpi_os_read_pci_configuration (struct acpi_pci_id
*pci_id
, u32 reg
, void *value
, u32 width
)
475 return AE_BAD_PARAMETER
;
491 BUG_ON(!raw_pci_ops
);
493 result
= raw_pci_ops
->read(pci_id
->segment
, pci_id
->bus
,
494 PCI_DEVFN(pci_id
->device
, pci_id
->function
),
497 return (result
? AE_ERROR
: AE_OK
);
501 acpi_os_write_pci_configuration (struct acpi_pci_id
*pci_id
, u32 reg
, acpi_integer value
, u32 width
)
519 BUG_ON(!raw_pci_ops
);
521 result
= raw_pci_ops
->write(pci_id
->segment
, pci_id
->bus
,
522 PCI_DEVFN(pci_id
->device
, pci_id
->function
),
525 return (result
? AE_ERROR
: AE_OK
);
528 /* TODO: Change code to take advantage of driver model more */
530 acpi_os_derive_pci_id_2 (
531 acpi_handle rhandle
, /* upper bound */
532 acpi_handle chandle
, /* current node */
533 struct acpi_pci_id
**id
,
538 struct acpi_pci_id
*pci_id
= *id
;
541 acpi_object_type type
;
544 acpi_get_parent(chandle
, &handle
);
545 if (handle
!= rhandle
) {
546 acpi_os_derive_pci_id_2(rhandle
, handle
, &pci_id
, is_bridge
, bus_number
);
548 status
= acpi_get_type(handle
, &type
);
549 if ( (ACPI_FAILURE(status
)) || (type
!= ACPI_TYPE_DEVICE
) )
552 status
= acpi_evaluate_integer(handle
, METHOD_NAME__ADR
, NULL
, &temp
);
553 if (ACPI_SUCCESS(status
)) {
554 pci_id
->device
= ACPI_HIWORD (ACPI_LODWORD (temp
));
555 pci_id
->function
= ACPI_LOWORD (ACPI_LODWORD (temp
));
558 pci_id
->bus
= *bus_number
;
560 /* any nicer way to get bus number of bridge ? */
561 status
= acpi_os_read_pci_configuration(pci_id
, 0x0e, &tu8
, 8);
562 if (ACPI_SUCCESS(status
) &&
563 ((tu8
& 0x7f) == 1 || (tu8
& 0x7f) == 2)) {
564 status
= acpi_os_read_pci_configuration(pci_id
, 0x18, &tu8
, 8);
565 if (!ACPI_SUCCESS(status
)) {
566 /* Certainly broken... FIX ME */
571 status
= acpi_os_read_pci_configuration(pci_id
, 0x19, &tu8
, 8);
572 if (ACPI_SUCCESS(status
)) {
582 acpi_os_derive_pci_id (
583 acpi_handle rhandle
, /* upper bound */
584 acpi_handle chandle
, /* current node */
585 struct acpi_pci_id
**id
)
588 u8 bus_number
= (*id
)->bus
;
590 acpi_os_derive_pci_id_2(rhandle
, chandle
, id
, &is_bridge
, &bus_number
);
593 #else /*!CONFIG_ACPI_PCI*/
596 acpi_os_write_pci_configuration (
597 struct acpi_pci_id
*pci_id
,
606 acpi_os_read_pci_configuration (
607 struct acpi_pci_id
*pci_id
,
616 acpi_os_derive_pci_id (
617 acpi_handle rhandle
, /* upper bound */
618 acpi_handle chandle
, /* current node */
619 struct acpi_pci_id
**id
)
623 #endif /*CONFIG_ACPI_PCI*/
626 acpi_os_execute_deferred (
629 struct acpi_os_dpc
*dpc
= NULL
;
631 ACPI_FUNCTION_TRACE ("os_execute_deferred");
633 dpc
= (struct acpi_os_dpc
*) context
;
635 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
, "Invalid (NULL) context.\n"));
639 dpc
->function(dpc
->context
);
647 acpi_os_queue_for_execution(
649 acpi_osd_exec_callback function
,
652 acpi_status status
= AE_OK
;
653 struct acpi_os_dpc
*dpc
;
654 struct work_struct
*task
;
656 ACPI_FUNCTION_TRACE ("os_queue_for_execution");
658 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
, "Scheduling function [%p(%p)] for deferred execution.\n", function
, context
));
661 return_ACPI_STATUS (AE_BAD_PARAMETER
);
664 * Allocate/initialize DPC structure. Note that this memory will be
665 * freed by the callee. The kernel handles the tq_struct list in a
666 * way that allows us to also free its memory inside the callee.
667 * Because we may want to schedule several tasks with different
668 * parameters we can't use the approach some kernel code uses of
669 * having a static tq_struct.
670 * We can save time and code by allocating the DPC and tq_structs
671 * from the same memory.
674 dpc
= kmalloc(sizeof(struct acpi_os_dpc
)+sizeof(struct work_struct
), GFP_ATOMIC
);
676 return_ACPI_STATUS (AE_NO_MEMORY
);
678 dpc
->function
= function
;
679 dpc
->context
= context
;
681 task
= (void *)(dpc
+1);
682 INIT_WORK(task
, acpi_os_execute_deferred
, (void*)dpc
);
684 if (!queue_work(kacpid_wq
, task
)) {
685 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
, "Call to queue_work() failed.\n"));
690 return_ACPI_STATUS (status
);
694 acpi_os_wait_events_complete(
697 flush_workqueue(kacpid_wq
);
701 * Allocate the memory for a spinlock and initialize it.
704 acpi_os_create_lock (
705 acpi_handle
*out_handle
)
707 spinlock_t
*lock_ptr
;
709 ACPI_FUNCTION_TRACE ("os_create_lock");
711 lock_ptr
= acpi_os_allocate(sizeof(spinlock_t
));
713 spin_lock_init(lock_ptr
);
715 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
, "Creating spinlock[%p].\n", lock_ptr
));
717 *out_handle
= lock_ptr
;
719 return_ACPI_STATUS (AE_OK
);
724 * Deallocate the memory for a spinlock.
727 acpi_os_delete_lock (
730 ACPI_FUNCTION_TRACE ("os_create_lock");
732 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
, "Deleting spinlock[%p].\n", handle
));
734 acpi_os_free(handle
);
740 * Acquire a spinlock.
742 * handle is a pointer to the spinlock_t.
743 * flags is *not* the result of save_flags - it is an ACPI-specific flag variable
744 * that indicates whether we are at interrupt level.
747 acpi_os_acquire_lock (
751 ACPI_FUNCTION_TRACE ("os_acquire_lock");
753 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
, "Acquiring spinlock[%p] from %s level\n", handle
,
754 ((flags
& ACPI_NOT_ISR
) ? "non-interrupt" : "interrupt")));
756 if (flags
& ACPI_NOT_ISR
)
759 spin_lock((spinlock_t
*)handle
);
766 * Release a spinlock. See above.
769 acpi_os_release_lock (
773 ACPI_FUNCTION_TRACE ("os_release_lock");
775 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
, "Releasing spinlock[%p] from %s level\n", handle
,
776 ((flags
& ACPI_NOT_ISR
) ? "non-interrupt" : "interrupt")));
778 spin_unlock((spinlock_t
*)handle
);
780 if (flags
& ACPI_NOT_ISR
)
788 acpi_os_create_semaphore(
793 struct semaphore
*sem
= NULL
;
795 ACPI_FUNCTION_TRACE ("os_create_semaphore");
797 sem
= acpi_os_allocate(sizeof(struct semaphore
));
799 return_ACPI_STATUS (AE_NO_MEMORY
);
800 memset(sem
, 0, sizeof(struct semaphore
));
802 sema_init(sem
, initial_units
);
804 *handle
= (acpi_handle
*)sem
;
806 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
, "Creating semaphore[%p|%d].\n", *handle
, initial_units
));
808 return_ACPI_STATUS (AE_OK
);
813 * TODO: A better way to delete semaphores? Linux doesn't have a
814 * 'delete_semaphore()' function -- may result in an invalid
815 * pointer dereference for non-synchronized consumers. Should
816 * we at least check for blocked threads and signal/cancel them?
820 acpi_os_delete_semaphore(
823 struct semaphore
*sem
= (struct semaphore
*) handle
;
825 ACPI_FUNCTION_TRACE ("os_delete_semaphore");
828 return_ACPI_STATUS (AE_BAD_PARAMETER
);
830 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
, "Deleting semaphore[%p].\n", handle
));
832 acpi_os_free(sem
); sem
= NULL
;
834 return_ACPI_STATUS (AE_OK
);
839 * TODO: The kernel doesn't have a 'down_timeout' function -- had to
840 * improvise. The process is to sleep for one scheduler quantum
841 * until the semaphore becomes available. Downside is that this
842 * may result in starvation for timeout-based waits when there's
843 * lots of semaphore activity.
845 * TODO: Support for units > 1?
848 acpi_os_wait_semaphore(
853 acpi_status status
= AE_OK
;
854 struct semaphore
*sem
= (struct semaphore
*)handle
;
857 ACPI_FUNCTION_TRACE ("os_wait_semaphore");
859 if (!sem
|| (units
< 1))
860 return_ACPI_STATUS (AE_BAD_PARAMETER
);
863 return_ACPI_STATUS (AE_SUPPORT
);
865 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
, "Waiting for semaphore[%p|%d|%d]\n", handle
, units
, timeout
));
875 * A zero timeout value indicates that we shouldn't wait - just
876 * acquire the semaphore if available otherwise return AE_TIME
877 * (a.k.a. 'would block').
880 if(down_trylock(sem
))
888 case ACPI_WAIT_FOREVER
:
897 // TODO: A better timeout algorithm?
900 static const int quantum_ms
= 1000/HZ
;
902 ret
= down_trylock(sem
);
903 for (i
= timeout
; (i
> 0 && ret
< 0); i
-= quantum_ms
) {
904 current
->state
= TASK_INTERRUPTIBLE
;
906 ret
= down_trylock(sem
);
915 if (ACPI_FAILURE(status
)) {
916 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
, "Failed to acquire semaphore[%p|%d|%d], %s\n",
917 handle
, units
, timeout
, acpi_format_exception(status
)));
920 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
, "Acquired semaphore[%p|%d|%d]\n", handle
, units
, timeout
));
923 return_ACPI_STATUS (status
);
928 * TODO: Support for units > 1?
931 acpi_os_signal_semaphore(
935 struct semaphore
*sem
= (struct semaphore
*) handle
;
937 ACPI_FUNCTION_TRACE ("os_signal_semaphore");
939 if (!sem
|| (units
< 1))
940 return_ACPI_STATUS (AE_BAD_PARAMETER
);
943 return_ACPI_STATUS (AE_SUPPORT
);
945 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
, "Signaling semaphore[%p|%d]\n", handle
, units
));
949 return_ACPI_STATUS (AE_OK
);
953 acpi_os_get_line(char *buffer
)
956 #ifdef ENABLE_DEBUGGER
957 if (acpi_in_debugger
) {
960 kdb_read(buffer
, sizeof(line_buf
));
962 /* remove the CR kdb includes */
963 chars
= strlen(buffer
) - 1;
964 buffer
[chars
] = '\0';
971 /* Assumes no unreadable holes inbetween */
973 acpi_os_readable(void *ptr
, acpi_size len
)
975 #if defined(__i386__) || defined(__x86_64__)
977 return !__get_user(tmp
, (char __user
*)ptr
) && !__get_user(tmp
, (char __user
*)ptr
+ len
- 1);
983 acpi_os_writable(void *ptr
, acpi_size len
)
985 /* could do dummy write (racy) or a kernel page table lookup.
986 The later may be difficult at early boot when kmap doesn't work yet. */
991 acpi_os_get_thread_id (void)
1006 case ACPI_SIGNAL_FATAL
:
1007 printk(KERN_ERR PREFIX
"Fatal opcode executed\n");
1009 case ACPI_SIGNAL_BREAKPOINT
:
1011 char *bp_info
= (char*) info
;
1013 printk(KERN_ERR
"ACPI breakpoint: %s\n", bp_info
);
1023 acpi_os_name_setup(char *str
)
1025 char *p
= acpi_os_name
;
1026 int count
= ACPI_MAX_OVERRIDE_LEN
-1;
1031 for (; count
-- && str
&& *str
; str
++) {
1032 if (isalnum(*str
) || *str
== ' ' || *str
== ':')
1034 else if (*str
== '\'' || *str
== '"')
1045 __setup("acpi_os_name=", acpi_os_name_setup
);
1049 * empty string disables _OSI
1050 * TBD additional string adds to _OSI
1053 acpi_osi_setup(char *str
)
1055 if (str
== NULL
|| *str
== '\0') {
1056 printk(KERN_INFO PREFIX
"_OSI method disabled\n");
1057 acpi_gbl_create_osi_method
= FALSE
;
1061 printk(KERN_ERR PREFIX
"_OSI additional string ignored -- %s\n", str
);
1067 __setup("acpi_osi=", acpi_osi_setup
);
1069 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1071 acpi_serialize_setup(char *str
)
1073 printk(KERN_INFO PREFIX
"serialize enabled\n");
1075 acpi_gbl_all_methods_serialized
= TRUE
;
1080 __setup("acpi_serialize", acpi_serialize_setup
);
1083 * Wake and Run-Time GPES are expected to be separate.
1084 * We disable wake-GPEs at run-time to prevent spurious
1087 * However, if a system exists that shares Wake and
1088 * Run-time events on the same GPE this flag is available
1089 * to tell Linux to keep the wake-time GPEs enabled at run-time.
1092 acpi_wake_gpes_always_on_setup(char *str
)
1094 printk(KERN_INFO PREFIX
"wake GPEs not disabled\n");
1096 acpi_gbl_leave_wake_gpes_disabled
= FALSE
;
1101 __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup
);