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 int (*__acpi_os_prepare_sleep)(u8 sleep_state, u32 pm1a_ctrl,
82 static acpi_osd_handler acpi_irq_handler;
83 static void *acpi_irq_context;
84 static struct workqueue_struct *kacpid_wq;
85 static struct workqueue_struct *kacpi_notify_wq;
86 struct workqueue_struct *kacpi_hotplug_wq;
87 EXPORT_SYMBOL(kacpi_hotplug_wq);
89 struct acpi_res_list {
90 resource_size_t start;
92 acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
93 char name[5]; /* only can have a length of 4 chars, make use of this
94 one instead of res->name, no need to kalloc then */
95 struct list_head resource_list;
99 static LIST_HEAD(resource_list_head);
100 static DEFINE_SPINLOCK(acpi_res_lock);
103 * This list of permanent mappings is for memory that may be accessed from
104 * interrupt context, where we can't do the ioremap().
106 struct acpi_ioremap {
107 struct list_head list;
109 acpi_physical_address phys;
111 unsigned long refcount;
114 static LIST_HEAD(acpi_ioremaps);
115 static DEFINE_MUTEX(acpi_ioremap_lock);
117 static void __init acpi_osi_setup_late(void);
120 * The story of _OSI(Linux)
122 * From pre-history through Linux-2.6.22,
123 * Linux responded TRUE upon a BIOS OSI(Linux) query.
125 * Unfortunately, reference BIOS writers got wind of this
126 * and put OSI(Linux) in their example code, quickly exposing
127 * this string as ill-conceived and opening the door to
128 * an un-bounded number of BIOS incompatibilities.
130 * For example, OSI(Linux) was used on resume to re-POST a
131 * video card on one system, because Linux at that time
132 * could not do a speedy restore in its native driver.
133 * But then upon gaining quick native restore capability,
134 * Linux has no way to tell the BIOS to skip the time-consuming
135 * POST -- putting Linux at a permanent performance disadvantage.
136 * On another system, the BIOS writer used OSI(Linux)
137 * to infer native OS support for IPMI! On other systems,
138 * OSI(Linux) simply got in the way of Linux claiming to
139 * be compatible with other operating systems, exposing
140 * BIOS issues such as skipped device initialization.
142 * So "Linux" turned out to be a really poor chose of
143 * OSI string, and from Linux-2.6.23 onward we respond FALSE.
145 * BIOS writers should NOT query _OSI(Linux) on future systems.
146 * Linux will complain on the console when it sees it, and return FALSE.
147 * To get Linux to return TRUE for your system will require
148 * a kernel source update to add a DMI entry,
149 * or boot with "acpi_osi=Linux"
152 static struct osi_linux {
153 unsigned int enable:1;
155 unsigned int cmdline:1;
156 } osi_linux = {0, 0, 0};
158 static u32 acpi_osi_handler(acpi_string interface, u32 supported)
160 if (!strcmp("Linux", interface)) {
162 printk_once(KERN_NOTICE FW_BUG PREFIX
163 "BIOS _OSI(Linux) query %s%s\n",
164 osi_linux.enable ? "honored" : "ignored",
165 osi_linux.cmdline ? " via cmdline" :
166 osi_linux.dmi ? " via DMI" : "");
172 static void __init acpi_request_region (struct acpi_generic_address *addr,
173 unsigned int length, char *desc)
175 if (!addr->address || !length)
178 /* Resources are never freed */
179 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
180 request_region(addr->address, length, desc);
181 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
182 request_mem_region(addr->address, length, desc);
185 static int __init acpi_reserve_resources(void)
187 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
188 "ACPI PM1a_EVT_BLK");
190 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
191 "ACPI PM1b_EVT_BLK");
193 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
194 "ACPI PM1a_CNT_BLK");
196 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
197 "ACPI PM1b_CNT_BLK");
199 if (acpi_gbl_FADT.pm_timer_length == 4)
200 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
202 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
205 /* Length of GPE blocks must be a non-negative multiple of 2 */
207 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
208 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
209 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
211 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
212 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
213 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
217 device_initcall(acpi_reserve_resources);
219 void acpi_os_printf(const char *fmt, ...)
223 acpi_os_vprintf(fmt, args);
227 void acpi_os_vprintf(const char *fmt, va_list args)
229 static char buffer[512];
231 vsprintf(buffer, fmt, args);
233 #ifdef ENABLE_DEBUGGER
234 if (acpi_in_debugger) {
235 kdb_printf("%s", buffer);
237 printk(KERN_CONT "%s", buffer);
240 printk(KERN_CONT "%s", buffer);
245 static unsigned long acpi_rsdp;
246 static int __init setup_acpi_rsdp(char *arg)
248 acpi_rsdp = simple_strtoul(arg, NULL, 16);
251 early_param("acpi_rsdp", setup_acpi_rsdp);
254 acpi_physical_address __init acpi_os_get_root_pointer(void)
262 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
264 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
267 printk(KERN_ERR PREFIX
268 "System description tables not found\n");
272 acpi_physical_address pa = 0;
274 acpi_find_root_pointer(&pa);
279 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
280 static struct acpi_ioremap *
281 acpi_map_lookup(acpi_physical_address phys, acpi_size size)
283 struct acpi_ioremap *map;
285 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
286 if (map->phys <= phys &&
287 phys + size <= map->phys + map->size)
293 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
294 static void __iomem *
295 acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
297 struct acpi_ioremap *map;
299 map = acpi_map_lookup(phys, size);
301 return map->virt + (phys - map->phys);
306 void __iomem *acpi_os_get_iomem(acpi_physical_address phys, unsigned int size)
308 struct acpi_ioremap *map;
309 void __iomem *virt = NULL;
311 mutex_lock(&acpi_ioremap_lock);
312 map = acpi_map_lookup(phys, size);
314 virt = map->virt + (phys - map->phys);
317 mutex_unlock(&acpi_ioremap_lock);
320 EXPORT_SYMBOL_GPL(acpi_os_get_iomem);
322 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
323 static struct acpi_ioremap *
324 acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
326 struct acpi_ioremap *map;
328 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
329 if (map->virt <= virt &&
330 virt + size <= map->virt + map->size)
336 void __iomem *__init_refok
337 acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
339 struct acpi_ioremap *map;
341 acpi_physical_address pg_off;
344 if (phys > ULONG_MAX) {
345 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
349 if (!acpi_gbl_permanent_mmap)
350 return __acpi_map_table((unsigned long)phys, size);
352 mutex_lock(&acpi_ioremap_lock);
353 /* Check if there's a suitable mapping already. */
354 map = acpi_map_lookup(phys, size);
360 map = kzalloc(sizeof(*map), GFP_KERNEL);
362 mutex_unlock(&acpi_ioremap_lock);
366 pg_off = round_down(phys, PAGE_SIZE);
367 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
368 virt = acpi_os_ioremap(pg_off, pg_sz);
370 mutex_unlock(&acpi_ioremap_lock);
375 INIT_LIST_HEAD(&map->list);
381 list_add_tail_rcu(&map->list, &acpi_ioremaps);
384 mutex_unlock(&acpi_ioremap_lock);
385 return map->virt + (phys - map->phys);
387 EXPORT_SYMBOL_GPL(acpi_os_map_memory);
389 static void acpi_os_drop_map_ref(struct acpi_ioremap *map)
391 if (!--map->refcount)
392 list_del_rcu(&map->list);
395 static void acpi_os_map_cleanup(struct acpi_ioremap *map)
397 if (!map->refcount) {
404 void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
406 struct acpi_ioremap *map;
408 if (!acpi_gbl_permanent_mmap) {
409 __acpi_unmap_table(virt, size);
413 mutex_lock(&acpi_ioremap_lock);
414 map = acpi_map_lookup_virt(virt, size);
416 mutex_unlock(&acpi_ioremap_lock);
417 WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
420 acpi_os_drop_map_ref(map);
421 mutex_unlock(&acpi_ioremap_lock);
423 acpi_os_map_cleanup(map);
425 EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
427 void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
429 if (!acpi_gbl_permanent_mmap)
430 __acpi_unmap_table(virt, size);
433 static int acpi_os_map_generic_address(struct acpi_generic_address *addr)
437 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
440 if (!addr->address || !addr->bit_width)
443 virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
450 static void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
452 struct acpi_ioremap *map;
454 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
457 if (!addr->address || !addr->bit_width)
460 mutex_lock(&acpi_ioremap_lock);
461 map = acpi_map_lookup(addr->address, addr->bit_width / 8);
463 mutex_unlock(&acpi_ioremap_lock);
466 acpi_os_drop_map_ref(map);
467 mutex_unlock(&acpi_ioremap_lock);
469 acpi_os_map_cleanup(map);
472 #ifdef ACPI_FUTURE_USAGE
474 acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
477 return AE_BAD_PARAMETER;
479 *phys = virt_to_phys(virt);
485 #define ACPI_MAX_OVERRIDE_LEN 100
487 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
490 acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
491 acpi_string * new_val)
493 if (!init_val || !new_val)
494 return AE_BAD_PARAMETER;
497 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
498 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
500 *new_val = acpi_os_name;
507 acpi_os_table_override(struct acpi_table_header * existing_table,
508 struct acpi_table_header ** new_table)
510 if (!existing_table || !new_table)
511 return AE_BAD_PARAMETER;
515 #ifdef CONFIG_ACPI_CUSTOM_DSDT
516 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
517 *new_table = (struct acpi_table_header *)AmlCode;
519 if (*new_table != NULL) {
520 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
521 "this is unsafe: tainting kernel\n",
522 existing_table->signature,
523 existing_table->oem_table_id);
524 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
529 static irqreturn_t acpi_irq(int irq, void *dev_id)
533 handled = (*acpi_irq_handler) (acpi_irq_context);
539 acpi_irq_not_handled++;
545 acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
550 acpi_irq_stats_init();
553 * ACPI interrupts different from the SCI in our copy of the FADT are
556 if (gsi != acpi_gbl_FADT.sci_interrupt)
557 return AE_BAD_PARAMETER;
559 if (acpi_irq_handler)
560 return AE_ALREADY_ACQUIRED;
562 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
563 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
568 acpi_irq_handler = handler;
569 acpi_irq_context = context;
570 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
571 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
572 acpi_irq_handler = NULL;
573 return AE_NOT_ACQUIRED;
579 acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
581 if (irq != acpi_gbl_FADT.sci_interrupt)
582 return AE_BAD_PARAMETER;
584 free_irq(irq, acpi_irq);
585 acpi_irq_handler = NULL;
591 * Running in interpreter thread context, safe to sleep
594 void acpi_os_sleep(u64 ms)
596 schedule_timeout_interruptible(msecs_to_jiffies(ms));
599 void acpi_os_stall(u32 us)
607 touch_nmi_watchdog();
613 * Support ACPI 3.0 AML Timer operand
614 * Returns 64-bit free-running, monotonically increasing timer
615 * with 100ns granularity
617 u64 acpi_os_get_timer(void)
622 /* TBD: use HPET if available */
625 #ifdef CONFIG_X86_PM_TIMER
626 /* TBD: default to PM timer if HPET was not available */
629 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
634 acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
643 *(u8 *) value = inb(port);
644 } else if (width <= 16) {
645 *(u16 *) value = inw(port);
646 } else if (width <= 32) {
647 *(u32 *) value = inl(port);
655 EXPORT_SYMBOL(acpi_os_read_port);
657 acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
661 } else if (width <= 16) {
663 } else if (width <= 32) {
672 EXPORT_SYMBOL(acpi_os_write_port);
675 acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
677 void __iomem *virt_addr;
678 unsigned int size = width / 8;
683 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
686 virt_addr = acpi_os_ioremap(phys_addr, size);
688 return AE_BAD_ADDRESS;
697 *(u8 *) value = readb(virt_addr);
700 *(u16 *) value = readw(virt_addr);
703 *(u32 *) value = readl(virt_addr);
718 acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
720 void __iomem *virt_addr;
721 unsigned int size = width / 8;
725 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
728 virt_addr = acpi_os_ioremap(phys_addr, size);
730 return AE_BAD_ADDRESS;
736 writeb(value, virt_addr);
739 writew(value, virt_addr);
742 writel(value, virt_addr);
757 acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
758 u64 *value, u32 width)
764 return AE_BAD_PARAMETER;
780 result = raw_pci_read(pci_id->segment, pci_id->bus,
781 PCI_DEVFN(pci_id->device, pci_id->function),
782 reg, size, &value32);
785 return (result ? AE_ERROR : AE_OK);
789 acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
790 u64 value, u32 width)
808 result = raw_pci_write(pci_id->segment, pci_id->bus,
809 PCI_DEVFN(pci_id->device, pci_id->function),
812 return (result ? AE_ERROR : AE_OK);
815 static void acpi_os_execute_deferred(struct work_struct *work)
817 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
820 acpi_os_wait_events_complete(NULL);
822 dpc->function(dpc->context);
826 /*******************************************************************************
828 * FUNCTION: acpi_os_execute
830 * PARAMETERS: Type - Type of the callback
831 * Function - Function to be executed
832 * Context - Function parameters
836 * DESCRIPTION: Depending on type, either queues function for deferred execution or
837 * immediately executes function on a separate thread.
839 ******************************************************************************/
841 static acpi_status __acpi_os_execute(acpi_execute_type type,
842 acpi_osd_exec_callback function, void *context, int hp)
844 acpi_status status = AE_OK;
845 struct acpi_os_dpc *dpc;
846 struct workqueue_struct *queue;
848 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
849 "Scheduling function [%p(%p)] for deferred execution.\n",
853 * Allocate/initialize DPC structure. Note that this memory will be
854 * freed by the callee. The kernel handles the work_struct list in a
855 * way that allows us to also free its memory inside the callee.
856 * Because we may want to schedule several tasks with different
857 * parameters we can't use the approach some kernel code uses of
858 * having a static work_struct.
861 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
865 dpc->function = function;
866 dpc->context = context;
869 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
870 * because the hotplug code may call driver .remove() functions,
871 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
872 * to flush these workqueues.
874 queue = hp ? kacpi_hotplug_wq :
875 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
876 dpc->wait = hp ? 1 : 0;
878 if (queue == kacpi_hotplug_wq)
879 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
880 else if (queue == kacpi_notify_wq)
881 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
883 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
886 * On some machines, a software-initiated SMI causes corruption unless
887 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
888 * typically it's done in GPE-related methods that are run via
889 * workqueues, so we can avoid the known corruption cases by always
892 ret = queue_work_on(0, queue, &dpc->work);
895 printk(KERN_ERR PREFIX
896 "Call to queue_work() failed.\n");
903 acpi_status acpi_os_execute(acpi_execute_type type,
904 acpi_osd_exec_callback function, void *context)
906 return __acpi_os_execute(type, function, context, 0);
908 EXPORT_SYMBOL(acpi_os_execute);
910 acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
913 return __acpi_os_execute(0, function, context, 1);
916 void acpi_os_wait_events_complete(void *context)
918 flush_workqueue(kacpid_wq);
919 flush_workqueue(kacpi_notify_wq);
922 EXPORT_SYMBOL(acpi_os_wait_events_complete);
925 acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
927 struct semaphore *sem = NULL;
929 sem = acpi_os_allocate(sizeof(struct semaphore));
932 memset(sem, 0, sizeof(struct semaphore));
934 sema_init(sem, initial_units);
936 *handle = (acpi_handle *) sem;
938 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
939 *handle, initial_units));
945 * TODO: A better way to delete semaphores? Linux doesn't have a
946 * 'delete_semaphore()' function -- may result in an invalid
947 * pointer dereference for non-synchronized consumers. Should
948 * we at least check for blocked threads and signal/cancel them?
951 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
953 struct semaphore *sem = (struct semaphore *)handle;
956 return AE_BAD_PARAMETER;
958 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
960 BUG_ON(!list_empty(&sem->wait_list));
968 * TODO: Support for units > 1?
970 acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
972 acpi_status status = AE_OK;
973 struct semaphore *sem = (struct semaphore *)handle;
977 if (!sem || (units < 1))
978 return AE_BAD_PARAMETER;
983 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
984 handle, units, timeout));
986 if (timeout == ACPI_WAIT_FOREVER)
987 jiffies = MAX_SCHEDULE_TIMEOUT;
989 jiffies = msecs_to_jiffies(timeout);
991 ret = down_timeout(sem, jiffies);
995 if (ACPI_FAILURE(status)) {
996 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
997 "Failed to acquire semaphore[%p|%d|%d], %s",
998 handle, units, timeout,
999 acpi_format_exception(status)));
1001 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
1002 "Acquired semaphore[%p|%d|%d]", handle,
1010 * TODO: Support for units > 1?
1012 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
1014 struct semaphore *sem = (struct semaphore *)handle;
1016 if (!sem || (units < 1))
1017 return AE_BAD_PARAMETER;
1022 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1030 #ifdef ACPI_FUTURE_USAGE
1031 u32 acpi_os_get_line(char *buffer)
1034 #ifdef ENABLE_DEBUGGER
1035 if (acpi_in_debugger) {
1038 kdb_read(buffer, sizeof(line_buf));
1040 /* remove the CR kdb includes */
1041 chars = strlen(buffer) - 1;
1042 buffer[chars] = '\0';
1048 #endif /* ACPI_FUTURE_USAGE */
1050 acpi_status acpi_os_signal(u32 function, void *info)
1053 case ACPI_SIGNAL_FATAL:
1054 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1056 case ACPI_SIGNAL_BREAKPOINT:
1059 * ACPI spec. says to treat it as a NOP unless
1060 * you are debugging. So if/when we integrate
1061 * AML debugger into the kernel debugger its
1062 * hook will go here. But until then it is
1063 * not useful to print anything on breakpoints.
1073 static int __init acpi_os_name_setup(char *str)
1075 char *p = acpi_os_name;
1076 int count = ACPI_MAX_OVERRIDE_LEN - 1;
1081 for (; count-- && str && *str; str++) {
1082 if (isalnum(*str) || *str == ' ' || *str == ':')
1084 else if (*str == '\'' || *str == '"')
1095 __setup("acpi_os_name=", acpi_os_name_setup);
1097 #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
1098 #define OSI_STRING_ENTRIES_MAX 16 /* arbitrary */
1100 struct osi_setup_entry {
1101 char string[OSI_STRING_LENGTH_MAX];
1105 static struct osi_setup_entry __initdata
1106 osi_setup_entries[OSI_STRING_ENTRIES_MAX] = {
1107 {"Module Device", true},
1108 {"Processor Device", true},
1109 {"3.0 _SCP Extensions", true},
1110 {"Processor Aggregator Device", true},
1113 void __init acpi_osi_setup(char *str)
1115 struct osi_setup_entry *osi;
1119 if (!acpi_gbl_create_osi_method)
1122 if (str == NULL || *str == '\0') {
1123 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1124 acpi_gbl_create_osi_method = FALSE;
1133 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1134 osi = &osi_setup_entries[i];
1135 if (!strcmp(osi->string, str)) {
1136 osi->enable = enable;
1138 } else if (osi->string[0] == '\0') {
1139 osi->enable = enable;
1140 strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
1146 static void __init set_osi_linux(unsigned int enable)
1148 if (osi_linux.enable != enable)
1149 osi_linux.enable = enable;
1151 if (osi_linux.enable)
1152 acpi_osi_setup("Linux");
1154 acpi_osi_setup("!Linux");
1159 static void __init acpi_cmdline_osi_linux(unsigned int enable)
1161 osi_linux.cmdline = 1; /* cmdline set the default and override DMI */
1163 set_osi_linux(enable);
1168 void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1170 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1175 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
1176 set_osi_linux(enable);
1182 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1184 * empty string disables _OSI
1185 * string starting with '!' disables that string
1186 * otherwise string is added to list, augmenting built-in strings
1188 static void __init acpi_osi_setup_late(void)
1190 struct osi_setup_entry *osi;
1195 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1196 osi = &osi_setup_entries[i];
1202 status = acpi_install_interface(str);
1204 if (ACPI_SUCCESS(status))
1205 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1207 status = acpi_remove_interface(str);
1209 if (ACPI_SUCCESS(status))
1210 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1215 static int __init osi_setup(char *str)
1217 if (str && !strcmp("Linux", str))
1218 acpi_cmdline_osi_linux(1);
1219 else if (str && !strcmp("!Linux", str))
1220 acpi_cmdline_osi_linux(0);
1222 acpi_osi_setup(str);
1227 __setup("acpi_osi=", osi_setup);
1229 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1230 static int __init acpi_serialize_setup(char *str)
1232 printk(KERN_INFO PREFIX "serialize enabled\n");
1234 acpi_gbl_all_methods_serialized = TRUE;
1239 __setup("acpi_serialize", acpi_serialize_setup);
1241 /* Check of resource interference between native drivers and ACPI
1242 * OperationRegions (SystemIO and System Memory only).
1243 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1244 * in arbitrary AML code and can interfere with legacy drivers.
1245 * acpi_enforce_resources= can be set to:
1247 * - strict (default) (2)
1248 * -> further driver trying to access the resources will not load
1250 * -> further driver trying to access the resources will load, but you
1251 * get a system message that something might go wrong...
1254 * -> ACPI Operation Region resources will not be registered
1257 #define ENFORCE_RESOURCES_STRICT 2
1258 #define ENFORCE_RESOURCES_LAX 1
1259 #define ENFORCE_RESOURCES_NO 0
1261 static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1263 static int __init acpi_enforce_resources_setup(char *str)
1265 if (str == NULL || *str == '\0')
1268 if (!strcmp("strict", str))
1269 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1270 else if (!strcmp("lax", str))
1271 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1272 else if (!strcmp("no", str))
1273 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1278 __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1280 /* Check for resource conflicts between ACPI OperationRegions and native
1282 int acpi_check_resource_conflict(const struct resource *res)
1284 struct acpi_res_list *res_list_elem;
1285 int ioport = 0, clash = 0;
1287 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1289 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1292 ioport = res->flags & IORESOURCE_IO;
1294 spin_lock(&acpi_res_lock);
1295 list_for_each_entry(res_list_elem, &resource_list_head,
1297 if (ioport && (res_list_elem->resource_type
1298 != ACPI_ADR_SPACE_SYSTEM_IO))
1300 if (!ioport && (res_list_elem->resource_type
1301 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1304 if (res->end < res_list_elem->start
1305 || res_list_elem->end < res->start)
1310 spin_unlock(&acpi_res_lock);
1313 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
1314 printk(KERN_WARNING "ACPI: resource %s %pR"
1315 " conflicts with ACPI region %s "
1316 "[%s 0x%zx-0x%zx]\n",
1317 res->name, res, res_list_elem->name,
1318 (res_list_elem->resource_type ==
1319 ACPI_ADR_SPACE_SYSTEM_IO) ? "io" : "mem",
1320 (size_t) res_list_elem->start,
1321 (size_t) res_list_elem->end);
1322 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1323 printk(KERN_NOTICE "ACPI: This conflict may"
1324 " cause random problems and system"
1326 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1327 " for this device, you should use it instead of"
1328 " the native driver\n");
1330 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1335 EXPORT_SYMBOL(acpi_check_resource_conflict);
1337 int acpi_check_region(resource_size_t start, resource_size_t n,
1340 struct resource res = {
1342 .end = start + n - 1,
1344 .flags = IORESOURCE_IO,
1347 return acpi_check_resource_conflict(&res);
1349 EXPORT_SYMBOL(acpi_check_region);
1352 * Let drivers know whether the resource checks are effective
1354 int acpi_resources_are_enforced(void)
1356 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1358 EXPORT_SYMBOL(acpi_resources_are_enforced);
1361 * Deallocate the memory for a spinlock.
1363 void acpi_os_delete_lock(acpi_spinlock handle)
1369 * Acquire a spinlock.
1371 * handle is a pointer to the spinlock_t.
1374 acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
1376 acpi_cpu_flags flags;
1377 spin_lock_irqsave(lockp, flags);
1382 * Release a spinlock. See above.
1385 void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
1387 spin_unlock_irqrestore(lockp, flags);
1390 #ifndef ACPI_USE_LOCAL_CACHE
1392 /*******************************************************************************
1394 * FUNCTION: acpi_os_create_cache
1396 * PARAMETERS: name - Ascii name for the cache
1397 * size - Size of each cached object
1398 * depth - Maximum depth of the cache (in objects) <ignored>
1399 * cache - Where the new cache object is returned
1403 * DESCRIPTION: Create a cache object
1405 ******************************************************************************/
1408 acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
1410 *cache = kmem_cache_create(name, size, 0, 0, NULL);
1417 /*******************************************************************************
1419 * FUNCTION: acpi_os_purge_cache
1421 * PARAMETERS: Cache - Handle to cache object
1425 * DESCRIPTION: Free all objects within the requested cache.
1427 ******************************************************************************/
1429 acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
1431 kmem_cache_shrink(cache);
1435 /*******************************************************************************
1437 * FUNCTION: acpi_os_delete_cache
1439 * PARAMETERS: Cache - Handle to cache object
1443 * DESCRIPTION: Free all objects within the requested cache and delete the
1446 ******************************************************************************/
1448 acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
1450 kmem_cache_destroy(cache);
1454 /*******************************************************************************
1456 * FUNCTION: acpi_os_release_object
1458 * PARAMETERS: Cache - Handle to cache object
1459 * Object - The object to be released
1463 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1464 * the object is deleted.
1466 ******************************************************************************/
1468 acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
1470 kmem_cache_free(cache, object);
1474 static inline int acpi_res_list_add(struct acpi_res_list *res)
1476 struct acpi_res_list *res_list_elem;
1478 list_for_each_entry(res_list_elem, &resource_list_head,
1481 if (res->resource_type == res_list_elem->resource_type &&
1482 res->start == res_list_elem->start &&
1483 res->end == res_list_elem->end) {
1486 * The Region(addr,len) already exist in the list,
1487 * just increase the count
1490 res_list_elem->count++;
1496 list_add(&res->resource_list, &resource_list_head);
1500 static inline void acpi_res_list_del(struct acpi_res_list *res)
1502 struct acpi_res_list *res_list_elem;
1504 list_for_each_entry(res_list_elem, &resource_list_head,
1507 if (res->resource_type == res_list_elem->resource_type &&
1508 res->start == res_list_elem->start &&
1509 res->end == res_list_elem->end) {
1512 * If the res count is decreased to 0,
1513 * remove and free it
1516 if (--res_list_elem->count == 0) {
1517 list_del(&res_list_elem->resource_list);
1518 kfree(res_list_elem);
1526 acpi_os_invalidate_address(
1528 acpi_physical_address address,
1531 struct acpi_res_list res;
1534 case ACPI_ADR_SPACE_SYSTEM_IO:
1535 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1536 /* Only interference checks against SystemIO and SystemMemory
1538 res.start = address;
1539 res.end = address + length - 1;
1540 res.resource_type = space_id;
1541 spin_lock(&acpi_res_lock);
1542 acpi_res_list_del(&res);
1543 spin_unlock(&acpi_res_lock);
1545 case ACPI_ADR_SPACE_PCI_CONFIG:
1546 case ACPI_ADR_SPACE_EC:
1547 case ACPI_ADR_SPACE_SMBUS:
1548 case ACPI_ADR_SPACE_CMOS:
1549 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1550 case ACPI_ADR_SPACE_DATA_TABLE:
1551 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1557 /******************************************************************************
1559 * FUNCTION: acpi_os_validate_address
1561 * PARAMETERS: space_id - ACPI space ID
1562 * address - Physical address
1563 * length - Address length
1565 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1566 * should return AE_AML_ILLEGAL_ADDRESS.
1568 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1569 * the addresses accessed by AML operation regions.
1571 *****************************************************************************/
1574 acpi_os_validate_address (
1576 acpi_physical_address address,
1580 struct acpi_res_list *res;
1582 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1586 case ACPI_ADR_SPACE_SYSTEM_IO:
1587 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1588 /* Only interference checks against SystemIO and SystemMemory
1590 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1593 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1594 strlcpy(res->name, name, 5);
1595 res->start = address;
1596 res->end = address + length - 1;
1597 res->resource_type = space_id;
1598 spin_lock(&acpi_res_lock);
1599 added = acpi_res_list_add(res);
1600 spin_unlock(&acpi_res_lock);
1601 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1602 "name: %s\n", added ? "Added" : "Already exist",
1603 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
1604 ? "SystemIO" : "System Memory",
1605 (unsigned long long)res->start,
1606 (unsigned long long)res->end,
1611 case ACPI_ADR_SPACE_PCI_CONFIG:
1612 case ACPI_ADR_SPACE_EC:
1613 case ACPI_ADR_SPACE_SMBUS:
1614 case ACPI_ADR_SPACE_CMOS:
1615 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1616 case ACPI_ADR_SPACE_DATA_TABLE:
1617 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1624 acpi_status __init acpi_os_initialize(void)
1626 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1627 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1628 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1629 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1634 acpi_status __init acpi_os_initialize1(void)
1636 kacpid_wq = alloc_workqueue("kacpid", 0, 1);
1637 kacpi_notify_wq = alloc_workqueue("kacpi_notify", 0, 1);
1638 kacpi_hotplug_wq = alloc_workqueue("kacpi_hotplug", 0, 1);
1640 BUG_ON(!kacpi_notify_wq);
1641 BUG_ON(!kacpi_hotplug_wq);
1642 acpi_install_interface_handler(acpi_osi_handler);
1643 acpi_osi_setup_late();
1647 acpi_status acpi_os_terminate(void)
1649 if (acpi_irq_handler) {
1650 acpi_os_remove_interrupt_handler(acpi_gbl_FADT.sci_interrupt,
1654 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1655 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1656 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1657 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1659 destroy_workqueue(kacpid_wq);
1660 destroy_workqueue(kacpi_notify_wq);
1661 destroy_workqueue(kacpi_hotplug_wq);
1666 acpi_status acpi_os_prepare_sleep(u8 sleep_state, u32 pm1a_control,
1670 if (__acpi_os_prepare_sleep)
1671 rc = __acpi_os_prepare_sleep(sleep_state,
1672 pm1a_control, pm1b_control);
1676 return AE_CTRL_SKIP;
1681 void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
1682 u32 pm1a_ctrl, u32 pm1b_ctrl))
1684 __acpi_os_prepare_sleep = func;