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>
7 * Copyright (c) 2008 Intel Corporation
8 * Author: Matthew Wilcox <willy@linux.intel.com>
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
34 #include <linux/pci.h>
35 #include <linux/interrupt.h>
36 #include <linux/kmod.h>
37 #include <linux/delay.h>
38 #include <linux/workqueue.h>
39 #include <linux/nmi.h>
40 #include <linux/acpi.h>
41 #include <linux/efi.h>
42 #include <linux/ioport.h>
43 #include <linux/list.h>
44 #include <linux/jiffies.h>
45 #include <linux/semaphore.h>
48 #include <asm/uaccess.h>
50 #include <acpi/acpi.h>
51 #include <acpi/acpi_bus.h>
52 #include <acpi/processor.h>
54 #define _COMPONENT ACPI_OS_SERVICES
55 ACPI_MODULE_NAME("osl");
56 #define PREFIX "ACPI: "
58 acpi_osd_exec_callback function;
60 struct work_struct work;
64 #ifdef CONFIG_ACPI_CUSTOM_DSDT
65 #include CONFIG_ACPI_CUSTOM_DSDT_FILE
68 #ifdef ENABLE_DEBUGGER
69 #include <linux/kdb.h>
71 /* stuff for debugger support */
73 EXPORT_SYMBOL(acpi_in_debugger);
75 extern char line_buf[80];
76 #endif /*ENABLE_DEBUGGER */
78 static unsigned int acpi_irq_irq;
79 static acpi_osd_handler acpi_irq_handler;
80 static void *acpi_irq_context;
81 static struct workqueue_struct *kacpid_wq;
82 static struct workqueue_struct *kacpi_notify_wq;
83 static struct workqueue_struct *kacpi_hotplug_wq;
85 struct acpi_res_list {
86 resource_size_t start;
88 acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
89 char name[5]; /* only can have a length of 4 chars, make use of this
90 one instead of res->name, no need to kalloc then */
91 struct list_head resource_list;
95 static LIST_HEAD(resource_list_head);
96 static DEFINE_SPINLOCK(acpi_res_lock);
99 * This list of permanent mappings is for memory that may be accessed from
100 * interrupt context, where we can't do the ioremap().
102 struct acpi_ioremap {
103 struct list_head list;
105 acpi_physical_address phys;
109 static LIST_HEAD(acpi_ioremaps);
110 static DEFINE_SPINLOCK(acpi_ioremap_lock);
112 #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
113 static char osi_additional_string[OSI_STRING_LENGTH_MAX];
116 * The story of _OSI(Linux)
118 * From pre-history through Linux-2.6.22,
119 * Linux responded TRUE upon a BIOS OSI(Linux) query.
121 * Unfortunately, reference BIOS writers got wind of this
122 * and put OSI(Linux) in their example code, quickly exposing
123 * this string as ill-conceived and opening the door to
124 * an un-bounded number of BIOS incompatibilities.
126 * For example, OSI(Linux) was used on resume to re-POST a
127 * video card on one system, because Linux at that time
128 * could not do a speedy restore in its native driver.
129 * But then upon gaining quick native restore capability,
130 * Linux has no way to tell the BIOS to skip the time-consuming
131 * POST -- putting Linux at a permanent performance disadvantage.
132 * On another system, the BIOS writer used OSI(Linux)
133 * to infer native OS support for IPMI! On other systems,
134 * OSI(Linux) simply got in the way of Linux claiming to
135 * be compatible with other operating systems, exposing
136 * BIOS issues such as skipped device initialization.
138 * So "Linux" turned out to be a really poor chose of
139 * OSI string, and from Linux-2.6.23 onward we respond FALSE.
141 * BIOS writers should NOT query _OSI(Linux) on future systems.
142 * Linux will complain on the console when it sees it, and return FALSE.
143 * To get Linux to return TRUE for your system will require
144 * a kernel source update to add a DMI entry,
145 * or boot with "acpi_osi=Linux"
148 static struct osi_linux {
149 unsigned int enable:1;
151 unsigned int cmdline:1;
152 unsigned int known:1;
153 } osi_linux = { 0, 0, 0, 0};
155 static void __init acpi_request_region (struct acpi_generic_address *addr,
156 unsigned int length, char *desc)
158 if (!addr->address || !length)
161 /* Resources are never freed */
162 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
163 request_region(addr->address, length, desc);
164 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
165 request_mem_region(addr->address, length, desc);
168 static int __init acpi_reserve_resources(void)
170 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
171 "ACPI PM1a_EVT_BLK");
173 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
174 "ACPI PM1b_EVT_BLK");
176 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
177 "ACPI PM1a_CNT_BLK");
179 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
180 "ACPI PM1b_CNT_BLK");
182 if (acpi_gbl_FADT.pm_timer_length == 4)
183 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
185 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
188 /* Length of GPE blocks must be a non-negative multiple of 2 */
190 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
191 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
192 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
194 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
195 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
196 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
200 device_initcall(acpi_reserve_resources);
202 void acpi_os_printf(const char *fmt, ...)
206 acpi_os_vprintf(fmt, args);
210 void acpi_os_vprintf(const char *fmt, va_list args)
212 static char buffer[512];
214 vsprintf(buffer, fmt, args);
216 #ifdef ENABLE_DEBUGGER
217 if (acpi_in_debugger) {
218 kdb_printf("%s", buffer);
220 printk(KERN_CONT "%s", buffer);
223 printk(KERN_CONT "%s", buffer);
227 acpi_physical_address __init acpi_os_get_root_pointer(void)
230 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
232 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
235 printk(KERN_ERR PREFIX
236 "System description tables not found\n");
240 acpi_physical_address pa = 0;
242 acpi_find_root_pointer(&pa);
247 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
248 static void __iomem *
249 acpi_map_vaddr_lookup(acpi_physical_address phys, acpi_size size)
251 struct acpi_ioremap *map;
253 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
254 if (map->phys <= phys &&
255 phys + size <= map->phys + map->size)
256 return map->virt + (phys - map->phys);
261 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
262 static struct acpi_ioremap *
263 acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
265 struct acpi_ioremap *map;
267 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
268 if (map->virt == virt && map->size == size)
274 void __iomem *__init_refok
275 acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
277 struct acpi_ioremap *map;
281 if (phys > ULONG_MAX) {
282 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
286 if (!acpi_gbl_permanent_mmap)
287 return __acpi_map_table((unsigned long)phys, size);
289 map = kzalloc(sizeof(*map), GFP_KERNEL);
293 virt = ioremap(phys, size);
299 INIT_LIST_HEAD(&map->list);
304 spin_lock_irqsave(&acpi_ioremap_lock, flags);
305 list_add_tail_rcu(&map->list, &acpi_ioremaps);
306 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
310 EXPORT_SYMBOL_GPL(acpi_os_map_memory);
312 void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
314 struct acpi_ioremap *map;
317 if (!acpi_gbl_permanent_mmap) {
318 __acpi_unmap_table(virt, size);
322 spin_lock_irqsave(&acpi_ioremap_lock, flags);
323 map = acpi_map_lookup_virt(virt, size);
325 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
326 printk(KERN_ERR PREFIX "%s: bad address %p\n", __func__, virt);
331 list_del_rcu(&map->list);
332 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
338 EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
340 void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
342 if (!acpi_gbl_permanent_mmap)
343 __acpi_unmap_table(virt, size);
346 int acpi_os_map_generic_address(struct acpi_generic_address *addr)
350 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
353 if (!addr->address || !addr->bit_width)
356 virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
362 EXPORT_SYMBOL_GPL(acpi_os_map_generic_address);
364 void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
368 acpi_size size = addr->bit_width / 8;
370 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
373 if (!addr->address || !addr->bit_width)
376 spin_lock_irqsave(&acpi_ioremap_lock, flags);
377 virt = acpi_map_vaddr_lookup(addr->address, size);
378 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
380 acpi_os_unmap_memory(virt, size);
382 EXPORT_SYMBOL_GPL(acpi_os_unmap_generic_address);
384 #ifdef ACPI_FUTURE_USAGE
386 acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
389 return AE_BAD_PARAMETER;
391 *phys = virt_to_phys(virt);
397 #define ACPI_MAX_OVERRIDE_LEN 100
399 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
402 acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
403 acpi_string * new_val)
405 if (!init_val || !new_val)
406 return AE_BAD_PARAMETER;
409 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
410 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
412 *new_val = acpi_os_name;
419 acpi_os_table_override(struct acpi_table_header * existing_table,
420 struct acpi_table_header ** new_table)
422 if (!existing_table || !new_table)
423 return AE_BAD_PARAMETER;
427 #ifdef CONFIG_ACPI_CUSTOM_DSDT
428 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
429 *new_table = (struct acpi_table_header *)AmlCode;
431 if (*new_table != NULL) {
432 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
433 "this is unsafe: tainting kernel\n",
434 existing_table->signature,
435 existing_table->oem_table_id);
436 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
441 static irqreturn_t acpi_irq(int irq, void *dev_id)
445 handled = (*acpi_irq_handler) (acpi_irq_context);
451 acpi_irq_not_handled++;
457 acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
462 acpi_irq_stats_init();
465 * Ignore the GSI from the core, and use the value in our copy of the
466 * FADT. It may not be the same if an interrupt source override exists
469 gsi = acpi_gbl_FADT.sci_interrupt;
470 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
471 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
476 acpi_irq_handler = handler;
477 acpi_irq_context = context;
478 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
479 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
480 return AE_NOT_ACQUIRED;
487 acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
490 free_irq(irq, acpi_irq);
491 acpi_irq_handler = NULL;
499 * Running in interpreter thread context, safe to sleep
502 void acpi_os_sleep(u64 ms)
504 schedule_timeout_interruptible(msecs_to_jiffies(ms));
507 void acpi_os_stall(u32 us)
515 touch_nmi_watchdog();
521 * Support ACPI 3.0 AML Timer operand
522 * Returns 64-bit free-running, monotonically increasing timer
523 * with 100ns granularity
525 u64 acpi_os_get_timer(void)
530 /* TBD: use HPET if available */
533 #ifdef CONFIG_X86_PM_TIMER
534 /* TBD: default to PM timer if HPET was not available */
537 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
542 acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
551 *(u8 *) value = inb(port);
552 } else if (width <= 16) {
553 *(u16 *) value = inw(port);
554 } else if (width <= 32) {
555 *(u32 *) value = inl(port);
563 EXPORT_SYMBOL(acpi_os_read_port);
565 acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
569 } else if (width <= 16) {
571 } else if (width <= 32) {
580 EXPORT_SYMBOL(acpi_os_write_port);
583 acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
586 void __iomem *virt_addr;
587 int size = width / 8, unmap = 0;
590 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
593 virt_addr = ioremap(phys_addr, size);
601 *(u8 *) value = readb(virt_addr);
604 *(u16 *) value = readw(virt_addr);
607 *(u32 *) value = readl(virt_addr);
620 acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
622 void __iomem *virt_addr;
623 int size = width / 8, unmap = 0;
626 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
629 virt_addr = ioremap(phys_addr, size);
635 writeb(value, virt_addr);
638 writew(value, virt_addr);
641 writel(value, virt_addr);
654 acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
655 u32 *value, u32 width)
660 return AE_BAD_PARAMETER;
676 result = raw_pci_read(pci_id->segment, pci_id->bus,
677 PCI_DEVFN(pci_id->device, pci_id->function),
680 return (result ? AE_ERROR : AE_OK);
684 acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
685 u64 value, u32 width)
703 result = raw_pci_write(pci_id->segment, pci_id->bus,
704 PCI_DEVFN(pci_id->device, pci_id->function),
707 return (result ? AE_ERROR : AE_OK);
710 /* TODO: Change code to take advantage of driver model more */
711 static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
712 acpi_handle chandle, /* current node */
713 struct acpi_pci_id **id,
714 int *is_bridge, u8 * bus_number)
717 struct acpi_pci_id *pci_id = *id;
719 unsigned long long temp;
720 acpi_object_type type;
722 acpi_get_parent(chandle, &handle);
723 if (handle != rhandle) {
724 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
727 status = acpi_get_type(handle, &type);
728 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
731 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
733 if (ACPI_SUCCESS(status)) {
735 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
736 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
739 pci_id->bus = *bus_number;
741 /* any nicer way to get bus number of bridge ? */
743 acpi_os_read_pci_configuration(pci_id, 0x0e, &val,
745 if (ACPI_SUCCESS(status)
746 && ((val & 0x7f) == 1 || (val & 0x7f) == 2)) {
748 acpi_os_read_pci_configuration(pci_id, 0x18,
750 if (!ACPI_SUCCESS(status)) {
751 /* Certainly broken... FIX ME */
757 acpi_os_read_pci_configuration(pci_id, 0x19,
759 if (ACPI_SUCCESS(status)) {
768 void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
769 acpi_handle chandle, /* current node */
770 struct acpi_pci_id **id)
773 u8 bus_number = (*id)->bus;
775 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
778 static void acpi_os_execute_deferred(struct work_struct *work)
780 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
783 acpi_os_wait_events_complete(NULL);
785 dpc->function(dpc->context);
789 /*******************************************************************************
791 * FUNCTION: acpi_os_execute
793 * PARAMETERS: Type - Type of the callback
794 * Function - Function to be executed
795 * Context - Function parameters
799 * DESCRIPTION: Depending on type, either queues function for deferred execution or
800 * immediately executes function on a separate thread.
802 ******************************************************************************/
804 static acpi_status __acpi_os_execute(acpi_execute_type type,
805 acpi_osd_exec_callback function, void *context, int hp)
807 acpi_status status = AE_OK;
808 struct acpi_os_dpc *dpc;
809 struct workqueue_struct *queue;
811 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
812 "Scheduling function [%p(%p)] for deferred execution.\n",
816 * Allocate/initialize DPC structure. Note that this memory will be
817 * freed by the callee. The kernel handles the work_struct list in a
818 * way that allows us to also free its memory inside the callee.
819 * Because we may want to schedule several tasks with different
820 * parameters we can't use the approach some kernel code uses of
821 * having a static work_struct.
824 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
828 dpc->function = function;
829 dpc->context = context;
832 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
833 * because the hotplug code may call driver .remove() functions,
834 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
835 * to flush these workqueues.
837 queue = hp ? kacpi_hotplug_wq :
838 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
839 dpc->wait = hp ? 1 : 0;
841 if (queue == kacpi_hotplug_wq)
842 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
843 else if (queue == kacpi_notify_wq)
844 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
846 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
849 * On some machines, a software-initiated SMI causes corruption unless
850 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
851 * typically it's done in GPE-related methods that are run via
852 * workqueues, so we can avoid the known corruption cases by always
855 ret = queue_work_on(0, queue, &dpc->work);
858 printk(KERN_ERR PREFIX
859 "Call to queue_work() failed.\n");
866 acpi_status acpi_os_execute(acpi_execute_type type,
867 acpi_osd_exec_callback function, void *context)
869 return __acpi_os_execute(type, function, context, 0);
871 EXPORT_SYMBOL(acpi_os_execute);
873 acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
876 return __acpi_os_execute(0, function, context, 1);
879 void acpi_os_wait_events_complete(void *context)
881 flush_workqueue(kacpid_wq);
882 flush_workqueue(kacpi_notify_wq);
885 EXPORT_SYMBOL(acpi_os_wait_events_complete);
888 * Allocate the memory for a spinlock and initialize it.
890 acpi_status acpi_os_create_lock(acpi_spinlock * handle)
892 spin_lock_init(*handle);
898 * Deallocate the memory for a spinlock.
900 void acpi_os_delete_lock(acpi_spinlock handle)
906 acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
908 struct semaphore *sem = NULL;
910 sem = acpi_os_allocate(sizeof(struct semaphore));
913 memset(sem, 0, sizeof(struct semaphore));
915 sema_init(sem, initial_units);
917 *handle = (acpi_handle *) sem;
919 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
920 *handle, initial_units));
926 * TODO: A better way to delete semaphores? Linux doesn't have a
927 * 'delete_semaphore()' function -- may result in an invalid
928 * pointer dereference for non-synchronized consumers. Should
929 * we at least check for blocked threads and signal/cancel them?
932 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
934 struct semaphore *sem = (struct semaphore *)handle;
937 return AE_BAD_PARAMETER;
939 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
941 BUG_ON(!list_empty(&sem->wait_list));
949 * TODO: Support for units > 1?
951 acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
953 acpi_status status = AE_OK;
954 struct semaphore *sem = (struct semaphore *)handle;
958 if (!sem || (units < 1))
959 return AE_BAD_PARAMETER;
964 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
965 handle, units, timeout));
967 if (timeout == ACPI_WAIT_FOREVER)
968 jiffies = MAX_SCHEDULE_TIMEOUT;
970 jiffies = msecs_to_jiffies(timeout);
972 ret = down_timeout(sem, jiffies);
976 if (ACPI_FAILURE(status)) {
977 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
978 "Failed to acquire semaphore[%p|%d|%d], %s",
979 handle, units, timeout,
980 acpi_format_exception(status)));
982 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
983 "Acquired semaphore[%p|%d|%d]", handle,
991 * TODO: Support for units > 1?
993 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
995 struct semaphore *sem = (struct semaphore *)handle;
997 if (!sem || (units < 1))
998 return AE_BAD_PARAMETER;
1003 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1011 #ifdef ACPI_FUTURE_USAGE
1012 u32 acpi_os_get_line(char *buffer)
1015 #ifdef ENABLE_DEBUGGER
1016 if (acpi_in_debugger) {
1019 kdb_read(buffer, sizeof(line_buf));
1021 /* remove the CR kdb includes */
1022 chars = strlen(buffer) - 1;
1023 buffer[chars] = '\0';
1029 #endif /* ACPI_FUTURE_USAGE */
1031 acpi_status acpi_os_signal(u32 function, void *info)
1034 case ACPI_SIGNAL_FATAL:
1035 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1037 case ACPI_SIGNAL_BREAKPOINT:
1040 * ACPI spec. says to treat it as a NOP unless
1041 * you are debugging. So if/when we integrate
1042 * AML debugger into the kernel debugger its
1043 * hook will go here. But until then it is
1044 * not useful to print anything on breakpoints.
1054 static int __init acpi_os_name_setup(char *str)
1056 char *p = acpi_os_name;
1057 int count = ACPI_MAX_OVERRIDE_LEN - 1;
1062 for (; count-- && str && *str; str++) {
1063 if (isalnum(*str) || *str == ' ' || *str == ':')
1065 else if (*str == '\'' || *str == '"')
1076 __setup("acpi_os_name=", acpi_os_name_setup);
1078 static void __init set_osi_linux(unsigned int enable)
1080 if (osi_linux.enable != enable) {
1081 osi_linux.enable = enable;
1082 printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
1083 enable ? "Add": "Delet");
1088 static void __init acpi_cmdline_osi_linux(unsigned int enable)
1090 osi_linux.cmdline = 1; /* cmdline set the default */
1091 set_osi_linux(enable);
1096 void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1098 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
1100 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1105 osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
1107 set_osi_linux(enable);
1113 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1115 * empty string disables _OSI
1116 * string starting with '!' disables that string
1117 * otherwise string is added to list, augmenting built-in strings
1119 int __init acpi_osi_setup(char *str)
1121 if (str == NULL || *str == '\0') {
1122 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1123 acpi_gbl_create_osi_method = FALSE;
1124 } else if (!strcmp("!Linux", str)) {
1125 acpi_cmdline_osi_linux(0); /* !enable */
1126 } else if (*str == '!') {
1127 if (acpi_osi_invalidate(++str) == AE_OK)
1128 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1129 } else if (!strcmp("Linux", str)) {
1130 acpi_cmdline_osi_linux(1); /* enable */
1131 } else if (*osi_additional_string == '\0') {
1132 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
1133 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1139 __setup("acpi_osi=", acpi_osi_setup);
1141 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1142 static int __init acpi_serialize_setup(char *str)
1144 printk(KERN_INFO PREFIX "serialize enabled\n");
1146 acpi_gbl_all_methods_serialized = TRUE;
1151 __setup("acpi_serialize", acpi_serialize_setup);
1153 /* Check of resource interference between native drivers and ACPI
1154 * OperationRegions (SystemIO and System Memory only).
1155 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1156 * in arbitrary AML code and can interfere with legacy drivers.
1157 * acpi_enforce_resources= can be set to:
1159 * - strict (default) (2)
1160 * -> further driver trying to access the resources will not load
1162 * -> further driver trying to access the resources will load, but you
1163 * get a system message that something might go wrong...
1166 * -> ACPI Operation Region resources will not be registered
1169 #define ENFORCE_RESOURCES_STRICT 2
1170 #define ENFORCE_RESOURCES_LAX 1
1171 #define ENFORCE_RESOURCES_NO 0
1173 static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1175 static int __init acpi_enforce_resources_setup(char *str)
1177 if (str == NULL || *str == '\0')
1180 if (!strcmp("strict", str))
1181 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1182 else if (!strcmp("lax", str))
1183 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1184 else if (!strcmp("no", str))
1185 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1190 __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1192 /* Check for resource conflicts between ACPI OperationRegions and native
1194 int acpi_check_resource_conflict(const struct resource *res)
1196 struct acpi_res_list *res_list_elem;
1200 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1202 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1205 ioport = res->flags & IORESOURCE_IO;
1207 spin_lock(&acpi_res_lock);
1208 list_for_each_entry(res_list_elem, &resource_list_head,
1210 if (ioport && (res_list_elem->resource_type
1211 != ACPI_ADR_SPACE_SYSTEM_IO))
1213 if (!ioport && (res_list_elem->resource_type
1214 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1217 if (res->end < res_list_elem->start
1218 || res_list_elem->end < res->start)
1223 spin_unlock(&acpi_res_lock);
1226 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
1227 printk(KERN_WARNING "ACPI: resource %s %pR"
1228 " conflicts with ACPI region %s %pR\n",
1229 res->name, res, res_list_elem->name,
1231 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1232 printk(KERN_NOTICE "ACPI: This conflict may"
1233 " cause random problems and system"
1235 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1236 " for this device, you should use it instead of"
1237 " the native driver\n");
1239 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1244 EXPORT_SYMBOL(acpi_check_resource_conflict);
1246 int acpi_check_region(resource_size_t start, resource_size_t n,
1249 struct resource res = {
1251 .end = start + n - 1,
1253 .flags = IORESOURCE_IO,
1256 return acpi_check_resource_conflict(&res);
1258 EXPORT_SYMBOL(acpi_check_region);
1260 int acpi_check_mem_region(resource_size_t start, resource_size_t n,
1263 struct resource res = {
1265 .end = start + n - 1,
1267 .flags = IORESOURCE_MEM,
1270 return acpi_check_resource_conflict(&res);
1273 EXPORT_SYMBOL(acpi_check_mem_region);
1276 * Let drivers know whether the resource checks are effective
1278 int acpi_resources_are_enforced(void)
1280 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1282 EXPORT_SYMBOL(acpi_resources_are_enforced);
1285 * Acquire a spinlock.
1287 * handle is a pointer to the spinlock_t.
1290 acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
1292 acpi_cpu_flags flags;
1293 spin_lock_irqsave(lockp, flags);
1298 * Release a spinlock. See above.
1301 void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
1303 spin_unlock_irqrestore(lockp, flags);
1306 #ifndef ACPI_USE_LOCAL_CACHE
1308 /*******************************************************************************
1310 * FUNCTION: acpi_os_create_cache
1312 * PARAMETERS: name - Ascii name for the cache
1313 * size - Size of each cached object
1314 * depth - Maximum depth of the cache (in objects) <ignored>
1315 * cache - Where the new cache object is returned
1319 * DESCRIPTION: Create a cache object
1321 ******************************************************************************/
1324 acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
1326 *cache = kmem_cache_create(name, size, 0, 0, NULL);
1333 /*******************************************************************************
1335 * FUNCTION: acpi_os_purge_cache
1337 * PARAMETERS: Cache - Handle to cache object
1341 * DESCRIPTION: Free all objects within the requested cache.
1343 ******************************************************************************/
1345 acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
1347 kmem_cache_shrink(cache);
1351 /*******************************************************************************
1353 * FUNCTION: acpi_os_delete_cache
1355 * PARAMETERS: Cache - Handle to cache object
1359 * DESCRIPTION: Free all objects within the requested cache and delete the
1362 ******************************************************************************/
1364 acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
1366 kmem_cache_destroy(cache);
1370 /*******************************************************************************
1372 * FUNCTION: acpi_os_release_object
1374 * PARAMETERS: Cache - Handle to cache object
1375 * Object - The object to be released
1379 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1380 * the object is deleted.
1382 ******************************************************************************/
1384 acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
1386 kmem_cache_free(cache, object);
1390 /******************************************************************************
1392 * FUNCTION: acpi_os_validate_interface
1394 * PARAMETERS: interface - Requested interface to be validated
1396 * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
1398 * DESCRIPTION: Match an interface string to the interfaces supported by the
1399 * host. Strings originate from an AML call to the _OSI method.
1401 *****************************************************************************/
1404 acpi_os_validate_interface (char *interface)
1406 if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1408 if (!strcmp("Linux", interface)) {
1410 printk(KERN_NOTICE PREFIX
1411 "BIOS _OSI(Linux) query %s%s\n",
1412 osi_linux.enable ? "honored" : "ignored",
1413 osi_linux.cmdline ? " via cmdline" :
1414 osi_linux.dmi ? " via DMI" : "");
1416 if (osi_linux.enable)
1422 static inline int acpi_res_list_add(struct acpi_res_list *res)
1424 struct acpi_res_list *res_list_elem;
1426 list_for_each_entry(res_list_elem, &resource_list_head,
1429 if (res->resource_type == res_list_elem->resource_type &&
1430 res->start == res_list_elem->start &&
1431 res->end == res_list_elem->end) {
1434 * The Region(addr,len) already exist in the list,
1435 * just increase the count
1438 res_list_elem->count++;
1444 list_add(&res->resource_list, &resource_list_head);
1448 static inline void acpi_res_list_del(struct acpi_res_list *res)
1450 struct acpi_res_list *res_list_elem;
1452 list_for_each_entry(res_list_elem, &resource_list_head,
1455 if (res->resource_type == res_list_elem->resource_type &&
1456 res->start == res_list_elem->start &&
1457 res->end == res_list_elem->end) {
1460 * If the res count is decreased to 0,
1461 * remove and free it
1464 if (--res_list_elem->count == 0) {
1465 list_del(&res_list_elem->resource_list);
1466 kfree(res_list_elem);
1474 acpi_os_invalidate_address(
1476 acpi_physical_address address,
1479 struct acpi_res_list res;
1482 case ACPI_ADR_SPACE_SYSTEM_IO:
1483 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1484 /* Only interference checks against SystemIO and SystemMemory
1486 res.start = address;
1487 res.end = address + length - 1;
1488 res.resource_type = space_id;
1489 spin_lock(&acpi_res_lock);
1490 acpi_res_list_del(&res);
1491 spin_unlock(&acpi_res_lock);
1493 case ACPI_ADR_SPACE_PCI_CONFIG:
1494 case ACPI_ADR_SPACE_EC:
1495 case ACPI_ADR_SPACE_SMBUS:
1496 case ACPI_ADR_SPACE_CMOS:
1497 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1498 case ACPI_ADR_SPACE_DATA_TABLE:
1499 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1505 /******************************************************************************
1507 * FUNCTION: acpi_os_validate_address
1509 * PARAMETERS: space_id - ACPI space ID
1510 * address - Physical address
1511 * length - Address length
1513 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1514 * should return AE_AML_ILLEGAL_ADDRESS.
1516 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1517 * the addresses accessed by AML operation regions.
1519 *****************************************************************************/
1522 acpi_os_validate_address (
1524 acpi_physical_address address,
1528 struct acpi_res_list *res;
1530 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1534 case ACPI_ADR_SPACE_SYSTEM_IO:
1535 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1536 /* Only interference checks against SystemIO and SystemMemory
1538 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1541 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1542 strlcpy(res->name, name, 5);
1543 res->start = address;
1544 res->end = address + length - 1;
1545 res->resource_type = space_id;
1546 spin_lock(&acpi_res_lock);
1547 added = acpi_res_list_add(res);
1548 spin_unlock(&acpi_res_lock);
1549 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1550 "name: %s\n", added ? "Added" : "Already exist",
1551 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
1552 ? "SystemIO" : "System Memory",
1553 (unsigned long long)res->start,
1554 (unsigned long long)res->end,
1559 case ACPI_ADR_SPACE_PCI_CONFIG:
1560 case ACPI_ADR_SPACE_EC:
1561 case ACPI_ADR_SPACE_SMBUS:
1562 case ACPI_ADR_SPACE_CMOS:
1563 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1564 case ACPI_ADR_SPACE_DATA_TABLE:
1565 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1572 acpi_status __init acpi_os_initialize(void)
1574 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1575 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1576 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1577 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1582 acpi_status acpi_os_initialize1(void)
1584 kacpid_wq = create_workqueue("kacpid");
1585 kacpi_notify_wq = create_workqueue("kacpi_notify");
1586 kacpi_hotplug_wq = create_workqueue("kacpi_hotplug");
1588 BUG_ON(!kacpi_notify_wq);
1589 BUG_ON(!kacpi_hotplug_wq);
1593 acpi_status acpi_os_terminate(void)
1595 if (acpi_irq_handler) {
1596 acpi_os_remove_interrupt_handler(acpi_irq_irq,
1600 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1601 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1602 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1603 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1605 destroy_workqueue(kacpid_wq);
1606 destroy_workqueue(kacpi_notify_wq);
1607 destroy_workqueue(kacpi_hotplug_wq);