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/acpi_io.h>
42 #include <linux/efi.h>
43 #include <linux/ioport.h>
44 #include <linux/list.h>
45 #include <linux/jiffies.h>
46 #include <linux/semaphore.h>
49 #include <asm/uaccess.h>
51 #include <acpi/acpi.h>
52 #include <acpi/acpi_bus.h>
53 #include <acpi/processor.h>
55 #define _COMPONENT ACPI_OS_SERVICES
56 ACPI_MODULE_NAME("osl");
57 #define PREFIX "ACPI: "
59 acpi_osd_exec_callback function;
61 struct work_struct work;
65 #ifdef CONFIG_ACPI_CUSTOM_DSDT
66 #include CONFIG_ACPI_CUSTOM_DSDT_FILE
69 #ifdef ENABLE_DEBUGGER
70 #include <linux/kdb.h>
72 /* stuff for debugger support */
74 EXPORT_SYMBOL(acpi_in_debugger);
76 extern char line_buf[80];
77 #endif /*ENABLE_DEBUGGER */
79 static unsigned int acpi_irq_irq;
80 static acpi_osd_handler acpi_irq_handler;
81 static void *acpi_irq_context;
82 static struct workqueue_struct *kacpid_wq;
83 static struct workqueue_struct *kacpi_notify_wq;
84 static struct workqueue_struct *kacpi_hotplug_wq;
86 struct acpi_res_list {
87 resource_size_t start;
89 acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
90 char name[5]; /* only can have a length of 4 chars, make use of this
91 one instead of res->name, no need to kalloc then */
92 struct list_head resource_list;
96 static LIST_HEAD(resource_list_head);
97 static DEFINE_SPINLOCK(acpi_res_lock);
100 * This list of permanent mappings is for memory that may be accessed from
101 * interrupt context, where we can't do the ioremap().
103 struct acpi_ioremap {
104 struct list_head list;
106 acpi_physical_address phys;
108 unsigned long refcount;
111 static LIST_HEAD(acpi_ioremaps);
112 static DEFINE_MUTEX(acpi_ioremap_lock);
114 static void __init acpi_osi_setup_late(void);
117 * The story of _OSI(Linux)
119 * From pre-history through Linux-2.6.22,
120 * Linux responded TRUE upon a BIOS OSI(Linux) query.
122 * Unfortunately, reference BIOS writers got wind of this
123 * and put OSI(Linux) in their example code, quickly exposing
124 * this string as ill-conceived and opening the door to
125 * an un-bounded number of BIOS incompatibilities.
127 * For example, OSI(Linux) was used on resume to re-POST a
128 * video card on one system, because Linux at that time
129 * could not do a speedy restore in its native driver.
130 * But then upon gaining quick native restore capability,
131 * Linux has no way to tell the BIOS to skip the time-consuming
132 * POST -- putting Linux at a permanent performance disadvantage.
133 * On another system, the BIOS writer used OSI(Linux)
134 * to infer native OS support for IPMI! On other systems,
135 * OSI(Linux) simply got in the way of Linux claiming to
136 * be compatible with other operating systems, exposing
137 * BIOS issues such as skipped device initialization.
139 * So "Linux" turned out to be a really poor chose of
140 * OSI string, and from Linux-2.6.23 onward we respond FALSE.
142 * BIOS writers should NOT query _OSI(Linux) on future systems.
143 * Linux will complain on the console when it sees it, and return FALSE.
144 * To get Linux to return TRUE for your system will require
145 * a kernel source update to add a DMI entry,
146 * or boot with "acpi_osi=Linux"
149 static struct osi_linux {
150 unsigned int enable:1;
152 unsigned int cmdline:1;
153 } osi_linux = {0, 0, 0};
155 static u32 acpi_osi_handler(acpi_string interface, u32 supported)
157 if (!strcmp("Linux", interface)) {
159 printk(KERN_NOTICE FW_BUG PREFIX
160 "BIOS _OSI(Linux) query %s%s\n",
161 osi_linux.enable ? "honored" : "ignored",
162 osi_linux.cmdline ? " via cmdline" :
163 osi_linux.dmi ? " via DMI" : "");
169 static void __init acpi_request_region (struct acpi_generic_address *addr,
170 unsigned int length, char *desc)
172 if (!addr->address || !length)
175 /* Resources are never freed */
176 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
177 request_region(addr->address, length, desc);
178 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
179 request_mem_region(addr->address, length, desc);
182 static int __init acpi_reserve_resources(void)
184 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
185 "ACPI PM1a_EVT_BLK");
187 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
188 "ACPI PM1b_EVT_BLK");
190 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
191 "ACPI PM1a_CNT_BLK");
193 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
194 "ACPI PM1b_CNT_BLK");
196 if (acpi_gbl_FADT.pm_timer_length == 4)
197 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
199 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
202 /* Length of GPE blocks must be a non-negative multiple of 2 */
204 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
205 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
206 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
208 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
209 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
210 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
214 device_initcall(acpi_reserve_resources);
216 void acpi_os_printf(const char *fmt, ...)
220 acpi_os_vprintf(fmt, args);
224 void acpi_os_vprintf(const char *fmt, va_list args)
226 static char buffer[512];
228 vsprintf(buffer, fmt, args);
230 #ifdef ENABLE_DEBUGGER
231 if (acpi_in_debugger) {
232 kdb_printf("%s", buffer);
234 printk(KERN_CONT "%s", buffer);
237 printk(KERN_CONT "%s", buffer);
241 acpi_physical_address __init acpi_os_get_root_pointer(void)
244 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
246 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
249 printk(KERN_ERR PREFIX
250 "System description tables not found\n");
254 acpi_physical_address pa = 0;
256 acpi_find_root_pointer(&pa);
261 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
262 static struct acpi_ioremap *
263 acpi_map_lookup(acpi_physical_address phys, acpi_size size)
265 struct acpi_ioremap *map;
267 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
268 if (map->phys <= phys &&
269 phys + size <= map->phys + map->size)
275 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
276 static void __iomem *
277 acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
279 struct acpi_ioremap *map;
281 map = acpi_map_lookup(phys, size);
283 return map->virt + (phys - map->phys);
288 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
289 static struct acpi_ioremap *
290 acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
292 struct acpi_ioremap *map;
294 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
295 if (map->virt <= virt &&
296 virt + size <= map->virt + map->size)
302 void __iomem *__init_refok
303 acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
305 struct acpi_ioremap *map;
307 acpi_physical_address pg_off;
310 if (phys > ULONG_MAX) {
311 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
315 if (!acpi_gbl_permanent_mmap)
316 return __acpi_map_table((unsigned long)phys, size);
318 mutex_lock(&acpi_ioremap_lock);
319 /* Check if there's a suitable mapping already. */
320 map = acpi_map_lookup(phys, size);
326 map = kzalloc(sizeof(*map), GFP_KERNEL);
328 mutex_unlock(&acpi_ioremap_lock);
332 pg_off = round_down(phys, PAGE_SIZE);
333 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
334 virt = acpi_os_ioremap(pg_off, pg_sz);
336 mutex_unlock(&acpi_ioremap_lock);
341 INIT_LIST_HEAD(&map->list);
347 list_add_tail_rcu(&map->list, &acpi_ioremaps);
350 mutex_unlock(&acpi_ioremap_lock);
351 return map->virt + (phys - map->phys);
353 EXPORT_SYMBOL_GPL(acpi_os_map_memory);
355 static void acpi_os_drop_map_ref(struct acpi_ioremap *map)
357 if (!--map->refcount)
358 list_del_rcu(&map->list);
361 static void acpi_os_map_cleanup(struct acpi_ioremap *map)
363 if (!map->refcount) {
370 void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
372 struct acpi_ioremap *map;
374 if (!acpi_gbl_permanent_mmap) {
375 __acpi_unmap_table(virt, size);
379 mutex_lock(&acpi_ioremap_lock);
380 map = acpi_map_lookup_virt(virt, size);
382 mutex_unlock(&acpi_ioremap_lock);
383 WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
386 acpi_os_drop_map_ref(map);
387 mutex_unlock(&acpi_ioremap_lock);
389 acpi_os_map_cleanup(map);
391 EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
393 void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
395 if (!acpi_gbl_permanent_mmap)
396 __acpi_unmap_table(virt, size);
399 static int acpi_os_map_generic_address(struct acpi_generic_address *addr)
403 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
406 if (!addr->address || !addr->bit_width)
409 virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
416 static void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
418 struct acpi_ioremap *map;
420 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
423 if (!addr->address || !addr->bit_width)
426 mutex_lock(&acpi_ioremap_lock);
427 map = acpi_map_lookup(addr->address, addr->bit_width / 8);
429 mutex_unlock(&acpi_ioremap_lock);
432 acpi_os_drop_map_ref(map);
433 mutex_unlock(&acpi_ioremap_lock);
435 acpi_os_map_cleanup(map);
438 #ifdef ACPI_FUTURE_USAGE
440 acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
443 return AE_BAD_PARAMETER;
445 *phys = virt_to_phys(virt);
451 #define ACPI_MAX_OVERRIDE_LEN 100
453 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
456 acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
457 acpi_string * new_val)
459 if (!init_val || !new_val)
460 return AE_BAD_PARAMETER;
463 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
464 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
466 *new_val = acpi_os_name;
473 acpi_os_table_override(struct acpi_table_header * existing_table,
474 struct acpi_table_header ** new_table)
476 if (!existing_table || !new_table)
477 return AE_BAD_PARAMETER;
481 #ifdef CONFIG_ACPI_CUSTOM_DSDT
482 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
483 *new_table = (struct acpi_table_header *)AmlCode;
485 if (*new_table != NULL) {
486 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
487 "this is unsafe: tainting kernel\n",
488 existing_table->signature,
489 existing_table->oem_table_id);
490 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
495 static irqreturn_t acpi_irq(int irq, void *dev_id)
499 handled = (*acpi_irq_handler) (acpi_irq_context);
505 acpi_irq_not_handled++;
511 acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
516 acpi_irq_stats_init();
519 * Ignore the GSI from the core, and use the value in our copy of the
520 * FADT. It may not be the same if an interrupt source override exists
523 gsi = acpi_gbl_FADT.sci_interrupt;
524 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
525 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
530 acpi_irq_handler = handler;
531 acpi_irq_context = context;
532 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
533 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
534 return AE_NOT_ACQUIRED;
541 acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
544 free_irq(irq, acpi_irq);
545 acpi_irq_handler = NULL;
553 * Running in interpreter thread context, safe to sleep
556 void acpi_os_sleep(u64 ms)
558 schedule_timeout_interruptible(msecs_to_jiffies(ms));
561 void acpi_os_stall(u32 us)
569 touch_nmi_watchdog();
575 * Support ACPI 3.0 AML Timer operand
576 * Returns 64-bit free-running, monotonically increasing timer
577 * with 100ns granularity
579 u64 acpi_os_get_timer(void)
584 /* TBD: use HPET if available */
587 #ifdef CONFIG_X86_PM_TIMER
588 /* TBD: default to PM timer if HPET was not available */
591 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
596 acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
605 *(u8 *) value = inb(port);
606 } else if (width <= 16) {
607 *(u16 *) value = inw(port);
608 } else if (width <= 32) {
609 *(u32 *) value = inl(port);
617 EXPORT_SYMBOL(acpi_os_read_port);
619 acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
623 } else if (width <= 16) {
625 } else if (width <= 32) {
634 EXPORT_SYMBOL(acpi_os_write_port);
637 acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
639 void __iomem *virt_addr;
640 unsigned int size = width / 8;
645 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
648 virt_addr = acpi_os_ioremap(phys_addr, size);
650 return AE_BAD_ADDRESS;
659 *(u8 *) value = readb(virt_addr);
662 *(u16 *) value = readw(virt_addr);
665 *(u32 *) value = readl(virt_addr);
680 acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
682 void __iomem *virt_addr;
683 unsigned int size = width / 8;
687 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
690 virt_addr = acpi_os_ioremap(phys_addr, size);
692 return AE_BAD_ADDRESS;
698 writeb(value, virt_addr);
701 writew(value, virt_addr);
704 writel(value, virt_addr);
719 acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
720 u64 *value, u32 width)
726 return AE_BAD_PARAMETER;
742 result = raw_pci_read(pci_id->segment, pci_id->bus,
743 PCI_DEVFN(pci_id->device, pci_id->function),
744 reg, size, &value32);
747 return (result ? AE_ERROR : AE_OK);
751 acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
752 u64 value, u32 width)
770 result = raw_pci_write(pci_id->segment, pci_id->bus,
771 PCI_DEVFN(pci_id->device, pci_id->function),
774 return (result ? AE_ERROR : AE_OK);
777 static void acpi_os_execute_deferred(struct work_struct *work)
779 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
782 acpi_os_wait_events_complete(NULL);
784 dpc->function(dpc->context);
788 /*******************************************************************************
790 * FUNCTION: acpi_os_execute
792 * PARAMETERS: Type - Type of the callback
793 * Function - Function to be executed
794 * Context - Function parameters
798 * DESCRIPTION: Depending on type, either queues function for deferred execution or
799 * immediately executes function on a separate thread.
801 ******************************************************************************/
803 static acpi_status __acpi_os_execute(acpi_execute_type type,
804 acpi_osd_exec_callback function, void *context, int hp)
806 acpi_status status = AE_OK;
807 struct acpi_os_dpc *dpc;
808 struct workqueue_struct *queue;
810 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
811 "Scheduling function [%p(%p)] for deferred execution.\n",
815 * Allocate/initialize DPC structure. Note that this memory will be
816 * freed by the callee. The kernel handles the work_struct list in a
817 * way that allows us to also free its memory inside the callee.
818 * Because we may want to schedule several tasks with different
819 * parameters we can't use the approach some kernel code uses of
820 * having a static work_struct.
823 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
827 dpc->function = function;
828 dpc->context = context;
831 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
832 * because the hotplug code may call driver .remove() functions,
833 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
834 * to flush these workqueues.
836 queue = hp ? kacpi_hotplug_wq :
837 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
838 dpc->wait = hp ? 1 : 0;
840 if (queue == kacpi_hotplug_wq)
841 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
842 else if (queue == kacpi_notify_wq)
843 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
845 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
848 * On some machines, a software-initiated SMI causes corruption unless
849 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
850 * typically it's done in GPE-related methods that are run via
851 * workqueues, so we can avoid the known corruption cases by always
854 ret = queue_work_on(0, queue, &dpc->work);
857 printk(KERN_ERR PREFIX
858 "Call to queue_work() failed.\n");
865 acpi_status acpi_os_execute(acpi_execute_type type,
866 acpi_osd_exec_callback function, void *context)
868 return __acpi_os_execute(type, function, context, 0);
870 EXPORT_SYMBOL(acpi_os_execute);
872 acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
875 return __acpi_os_execute(0, function, context, 1);
878 void acpi_os_wait_events_complete(void *context)
880 flush_workqueue(kacpid_wq);
881 flush_workqueue(kacpi_notify_wq);
884 EXPORT_SYMBOL(acpi_os_wait_events_complete);
887 * Deallocate the memory for a spinlock.
889 void acpi_os_delete_lock(acpi_spinlock handle)
895 acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
897 struct semaphore *sem = NULL;
899 sem = acpi_os_allocate(sizeof(struct semaphore));
902 memset(sem, 0, sizeof(struct semaphore));
904 sema_init(sem, initial_units);
906 *handle = (acpi_handle *) sem;
908 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
909 *handle, initial_units));
915 * TODO: A better way to delete semaphores? Linux doesn't have a
916 * 'delete_semaphore()' function -- may result in an invalid
917 * pointer dereference for non-synchronized consumers. Should
918 * we at least check for blocked threads and signal/cancel them?
921 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
923 struct semaphore *sem = (struct semaphore *)handle;
926 return AE_BAD_PARAMETER;
928 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
930 BUG_ON(!list_empty(&sem->wait_list));
938 * TODO: Support for units > 1?
940 acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
942 acpi_status status = AE_OK;
943 struct semaphore *sem = (struct semaphore *)handle;
947 if (!sem || (units < 1))
948 return AE_BAD_PARAMETER;
953 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
954 handle, units, timeout));
956 if (timeout == ACPI_WAIT_FOREVER)
957 jiffies = MAX_SCHEDULE_TIMEOUT;
959 jiffies = msecs_to_jiffies(timeout);
961 ret = down_timeout(sem, jiffies);
965 if (ACPI_FAILURE(status)) {
966 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
967 "Failed to acquire semaphore[%p|%d|%d], %s",
968 handle, units, timeout,
969 acpi_format_exception(status)));
971 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
972 "Acquired semaphore[%p|%d|%d]", handle,
980 * TODO: Support for units > 1?
982 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
984 struct semaphore *sem = (struct semaphore *)handle;
986 if (!sem || (units < 1))
987 return AE_BAD_PARAMETER;
992 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1000 #ifdef ACPI_FUTURE_USAGE
1001 u32 acpi_os_get_line(char *buffer)
1004 #ifdef ENABLE_DEBUGGER
1005 if (acpi_in_debugger) {
1008 kdb_read(buffer, sizeof(line_buf));
1010 /* remove the CR kdb includes */
1011 chars = strlen(buffer) - 1;
1012 buffer[chars] = '\0';
1018 #endif /* ACPI_FUTURE_USAGE */
1020 acpi_status acpi_os_signal(u32 function, void *info)
1023 case ACPI_SIGNAL_FATAL:
1024 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1026 case ACPI_SIGNAL_BREAKPOINT:
1029 * ACPI spec. says to treat it as a NOP unless
1030 * you are debugging. So if/when we integrate
1031 * AML debugger into the kernel debugger its
1032 * hook will go here. But until then it is
1033 * not useful to print anything on breakpoints.
1043 static int __init acpi_os_name_setup(char *str)
1045 char *p = acpi_os_name;
1046 int count = ACPI_MAX_OVERRIDE_LEN - 1;
1051 for (; count-- && str && *str; str++) {
1052 if (isalnum(*str) || *str == ' ' || *str == ':')
1054 else if (*str == '\'' || *str == '"')
1065 __setup("acpi_os_name=", acpi_os_name_setup);
1067 #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
1068 #define OSI_STRING_ENTRIES_MAX 16 /* arbitrary */
1070 struct osi_setup_entry {
1071 char string[OSI_STRING_LENGTH_MAX];
1075 static struct osi_setup_entry __initdata osi_setup_entries[OSI_STRING_ENTRIES_MAX];
1077 void __init acpi_osi_setup(char *str)
1079 struct osi_setup_entry *osi;
1083 if (!acpi_gbl_create_osi_method)
1086 if (str == NULL || *str == '\0') {
1087 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1088 acpi_gbl_create_osi_method = FALSE;
1097 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1098 osi = &osi_setup_entries[i];
1099 if (!strcmp(osi->string, str)) {
1100 osi->enable = enable;
1102 } else if (osi->string[0] == '\0') {
1103 osi->enable = enable;
1104 strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
1110 static void __init set_osi_linux(unsigned int enable)
1112 if (osi_linux.enable != enable)
1113 osi_linux.enable = enable;
1115 if (osi_linux.enable)
1116 acpi_osi_setup("Linux");
1118 acpi_osi_setup("!Linux");
1123 static void __init acpi_cmdline_osi_linux(unsigned int enable)
1125 osi_linux.cmdline = 1; /* cmdline set the default and override DMI */
1127 set_osi_linux(enable);
1132 void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1134 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1139 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
1140 set_osi_linux(enable);
1146 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1148 * empty string disables _OSI
1149 * string starting with '!' disables that string
1150 * otherwise string is added to list, augmenting built-in strings
1152 static void __init acpi_osi_setup_late(void)
1154 struct osi_setup_entry *osi;
1159 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1160 osi = &osi_setup_entries[i];
1166 status = acpi_install_interface(str);
1168 if (ACPI_SUCCESS(status))
1169 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1171 status = acpi_remove_interface(str);
1173 if (ACPI_SUCCESS(status))
1174 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1179 static int __init osi_setup(char *str)
1181 if (str && !strcmp("Linux", str))
1182 acpi_cmdline_osi_linux(1);
1183 else if (str && !strcmp("!Linux", str))
1184 acpi_cmdline_osi_linux(0);
1186 acpi_osi_setup(str);
1191 __setup("acpi_osi=", osi_setup);
1193 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1194 static int __init acpi_serialize_setup(char *str)
1196 printk(KERN_INFO PREFIX "serialize enabled\n");
1198 acpi_gbl_all_methods_serialized = TRUE;
1203 __setup("acpi_serialize", acpi_serialize_setup);
1205 /* Check of resource interference between native drivers and ACPI
1206 * OperationRegions (SystemIO and System Memory only).
1207 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1208 * in arbitrary AML code and can interfere with legacy drivers.
1209 * acpi_enforce_resources= can be set to:
1211 * - strict (default) (2)
1212 * -> further driver trying to access the resources will not load
1214 * -> further driver trying to access the resources will load, but you
1215 * get a system message that something might go wrong...
1218 * -> ACPI Operation Region resources will not be registered
1221 #define ENFORCE_RESOURCES_STRICT 2
1222 #define ENFORCE_RESOURCES_LAX 1
1223 #define ENFORCE_RESOURCES_NO 0
1225 static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1227 static int __init acpi_enforce_resources_setup(char *str)
1229 if (str == NULL || *str == '\0')
1232 if (!strcmp("strict", str))
1233 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1234 else if (!strcmp("lax", str))
1235 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1236 else if (!strcmp("no", str))
1237 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1242 __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1244 /* Check for resource conflicts between ACPI OperationRegions and native
1246 int acpi_check_resource_conflict(const struct resource *res)
1248 struct acpi_res_list *res_list_elem;
1249 int ioport = 0, clash = 0;
1251 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1253 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1256 ioport = res->flags & IORESOURCE_IO;
1258 spin_lock(&acpi_res_lock);
1259 list_for_each_entry(res_list_elem, &resource_list_head,
1261 if (ioport && (res_list_elem->resource_type
1262 != ACPI_ADR_SPACE_SYSTEM_IO))
1264 if (!ioport && (res_list_elem->resource_type
1265 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1268 if (res->end < res_list_elem->start
1269 || res_list_elem->end < res->start)
1274 spin_unlock(&acpi_res_lock);
1277 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
1278 printk(KERN_WARNING "ACPI: resource %s %pR"
1279 " conflicts with ACPI region %s "
1280 "[%s 0x%zx-0x%zx]\n",
1281 res->name, res, res_list_elem->name,
1282 (res_list_elem->resource_type ==
1283 ACPI_ADR_SPACE_SYSTEM_IO) ? "io" : "mem",
1284 (size_t) res_list_elem->start,
1285 (size_t) res_list_elem->end);
1286 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1287 printk(KERN_NOTICE "ACPI: This conflict may"
1288 " cause random problems and system"
1290 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1291 " for this device, you should use it instead of"
1292 " the native driver\n");
1294 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1299 EXPORT_SYMBOL(acpi_check_resource_conflict);
1301 int acpi_check_region(resource_size_t start, resource_size_t n,
1304 struct resource res = {
1306 .end = start + n - 1,
1308 .flags = IORESOURCE_IO,
1311 return acpi_check_resource_conflict(&res);
1313 EXPORT_SYMBOL(acpi_check_region);
1316 * Let drivers know whether the resource checks are effective
1318 int acpi_resources_are_enforced(void)
1320 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1322 EXPORT_SYMBOL(acpi_resources_are_enforced);
1325 * Acquire a spinlock.
1327 * handle is a pointer to the spinlock_t.
1330 acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
1332 acpi_cpu_flags flags;
1333 spin_lock_irqsave(lockp, flags);
1338 * Release a spinlock. See above.
1341 void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
1343 spin_unlock_irqrestore(lockp, flags);
1346 #ifndef ACPI_USE_LOCAL_CACHE
1348 /*******************************************************************************
1350 * FUNCTION: acpi_os_create_cache
1352 * PARAMETERS: name - Ascii name for the cache
1353 * size - Size of each cached object
1354 * depth - Maximum depth of the cache (in objects) <ignored>
1355 * cache - Where the new cache object is returned
1359 * DESCRIPTION: Create a cache object
1361 ******************************************************************************/
1364 acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
1366 *cache = kmem_cache_create(name, size, 0, 0, NULL);
1373 /*******************************************************************************
1375 * FUNCTION: acpi_os_purge_cache
1377 * PARAMETERS: Cache - Handle to cache object
1381 * DESCRIPTION: Free all objects within the requested cache.
1383 ******************************************************************************/
1385 acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
1387 kmem_cache_shrink(cache);
1391 /*******************************************************************************
1393 * FUNCTION: acpi_os_delete_cache
1395 * PARAMETERS: Cache - Handle to cache object
1399 * DESCRIPTION: Free all objects within the requested cache and delete the
1402 ******************************************************************************/
1404 acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
1406 kmem_cache_destroy(cache);
1410 /*******************************************************************************
1412 * FUNCTION: acpi_os_release_object
1414 * PARAMETERS: Cache - Handle to cache object
1415 * Object - The object to be released
1419 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1420 * the object is deleted.
1422 ******************************************************************************/
1424 acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
1426 kmem_cache_free(cache, object);
1430 static inline int acpi_res_list_add(struct acpi_res_list *res)
1432 struct acpi_res_list *res_list_elem;
1434 list_for_each_entry(res_list_elem, &resource_list_head,
1437 if (res->resource_type == res_list_elem->resource_type &&
1438 res->start == res_list_elem->start &&
1439 res->end == res_list_elem->end) {
1442 * The Region(addr,len) already exist in the list,
1443 * just increase the count
1446 res_list_elem->count++;
1452 list_add(&res->resource_list, &resource_list_head);
1456 static inline void acpi_res_list_del(struct acpi_res_list *res)
1458 struct acpi_res_list *res_list_elem;
1460 list_for_each_entry(res_list_elem, &resource_list_head,
1463 if (res->resource_type == res_list_elem->resource_type &&
1464 res->start == res_list_elem->start &&
1465 res->end == res_list_elem->end) {
1468 * If the res count is decreased to 0,
1469 * remove and free it
1472 if (--res_list_elem->count == 0) {
1473 list_del(&res_list_elem->resource_list);
1474 kfree(res_list_elem);
1482 acpi_os_invalidate_address(
1484 acpi_physical_address address,
1487 struct acpi_res_list res;
1490 case ACPI_ADR_SPACE_SYSTEM_IO:
1491 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1492 /* Only interference checks against SystemIO and SystemMemory
1494 res.start = address;
1495 res.end = address + length - 1;
1496 res.resource_type = space_id;
1497 spin_lock(&acpi_res_lock);
1498 acpi_res_list_del(&res);
1499 spin_unlock(&acpi_res_lock);
1501 case ACPI_ADR_SPACE_PCI_CONFIG:
1502 case ACPI_ADR_SPACE_EC:
1503 case ACPI_ADR_SPACE_SMBUS:
1504 case ACPI_ADR_SPACE_CMOS:
1505 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1506 case ACPI_ADR_SPACE_DATA_TABLE:
1507 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1513 /******************************************************************************
1515 * FUNCTION: acpi_os_validate_address
1517 * PARAMETERS: space_id - ACPI space ID
1518 * address - Physical address
1519 * length - Address length
1521 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1522 * should return AE_AML_ILLEGAL_ADDRESS.
1524 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1525 * the addresses accessed by AML operation regions.
1527 *****************************************************************************/
1530 acpi_os_validate_address (
1532 acpi_physical_address address,
1536 struct acpi_res_list *res;
1538 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1542 case ACPI_ADR_SPACE_SYSTEM_IO:
1543 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1544 /* Only interference checks against SystemIO and SystemMemory
1546 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1549 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1550 strlcpy(res->name, name, 5);
1551 res->start = address;
1552 res->end = address + length - 1;
1553 res->resource_type = space_id;
1554 spin_lock(&acpi_res_lock);
1555 added = acpi_res_list_add(res);
1556 spin_unlock(&acpi_res_lock);
1557 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1558 "name: %s\n", added ? "Added" : "Already exist",
1559 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
1560 ? "SystemIO" : "System Memory",
1561 (unsigned long long)res->start,
1562 (unsigned long long)res->end,
1567 case ACPI_ADR_SPACE_PCI_CONFIG:
1568 case ACPI_ADR_SPACE_EC:
1569 case ACPI_ADR_SPACE_SMBUS:
1570 case ACPI_ADR_SPACE_CMOS:
1571 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1572 case ACPI_ADR_SPACE_DATA_TABLE:
1573 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1580 acpi_status __init acpi_os_initialize(void)
1582 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1583 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1584 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1585 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1590 acpi_status __init acpi_os_initialize1(void)
1592 kacpid_wq = create_workqueue("kacpid");
1593 kacpi_notify_wq = create_workqueue("kacpi_notify");
1594 kacpi_hotplug_wq = create_workqueue("kacpi_hotplug");
1596 BUG_ON(!kacpi_notify_wq);
1597 BUG_ON(!kacpi_hotplug_wq);
1598 acpi_install_interface_handler(acpi_osi_handler);
1599 acpi_osi_setup_late();
1603 acpi_status acpi_os_terminate(void)
1605 if (acpi_irq_handler) {
1606 acpi_os_remove_interrupt_handler(acpi_irq_irq,
1610 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1611 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1612 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1613 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1615 destroy_workqueue(kacpid_wq);
1616 destroy_workqueue(kacpi_notify_wq);
1617 destroy_workqueue(kacpi_hotplug_wq);