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 acpi_status __init acpi_os_initialize(void)
207 acpi_status acpi_os_initialize1(void)
209 kacpid_wq = create_workqueue("kacpid");
210 kacpi_notify_wq = create_workqueue("kacpi_notify");
211 kacpi_hotplug_wq = create_workqueue("kacpi_hotplug");
213 BUG_ON(!kacpi_notify_wq);
214 BUG_ON(!kacpi_hotplug_wq);
218 acpi_status acpi_os_terminate(void)
220 if (acpi_irq_handler) {
221 acpi_os_remove_interrupt_handler(acpi_irq_irq,
225 destroy_workqueue(kacpid_wq);
226 destroy_workqueue(kacpi_notify_wq);
227 destroy_workqueue(kacpi_hotplug_wq);
232 void acpi_os_printf(const char *fmt, ...)
236 acpi_os_vprintf(fmt, args);
240 void acpi_os_vprintf(const char *fmt, va_list args)
242 static char buffer[512];
244 vsprintf(buffer, fmt, args);
246 #ifdef ENABLE_DEBUGGER
247 if (acpi_in_debugger) {
248 kdb_printf("%s", buffer);
250 printk(KERN_CONT "%s", buffer);
253 printk(KERN_CONT "%s", buffer);
257 acpi_physical_address __init acpi_os_get_root_pointer(void)
260 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
262 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
265 printk(KERN_ERR PREFIX
266 "System description tables not found\n");
270 acpi_physical_address pa = 0;
272 acpi_find_root_pointer(&pa);
277 /* Must be called with 'acpi_ioremap_lock' lock held. */
278 static void __iomem *
279 acpi_map_vaddr_lookup(acpi_physical_address phys, acpi_size size)
281 struct acpi_ioremap *map;
283 list_for_each_entry(map, &acpi_ioremaps, list)
284 if (map->phys <= phys &&
285 phys + size <= map->phys + map->size)
286 return map->virt + (phys - map->phys);
291 /* Must be called with 'acpi_ioremap_lock' lock held. */
292 static struct acpi_ioremap *
293 acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
295 struct acpi_ioremap *map;
297 list_for_each_entry(map, &acpi_ioremaps, list)
298 if (map->virt == virt && map->size == size)
304 void __iomem *__init_refok
305 acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
307 struct acpi_ioremap *map;
311 if (phys > ULONG_MAX) {
312 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
316 if (!acpi_gbl_permanent_mmap)
317 return __acpi_map_table((unsigned long)phys, size);
319 map = kzalloc(sizeof(*map), GFP_KERNEL);
323 virt = ioremap(phys, size);
329 INIT_LIST_HEAD(&map->list);
334 spin_lock_irqsave(&acpi_ioremap_lock, flags);
335 list_add_tail(&map->list, &acpi_ioremaps);
336 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
340 EXPORT_SYMBOL_GPL(acpi_os_map_memory);
342 void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
344 struct acpi_ioremap *map;
347 if (!acpi_gbl_permanent_mmap) {
348 __acpi_unmap_table(virt, size);
352 spin_lock_irqsave(&acpi_ioremap_lock, flags);
353 map = acpi_map_lookup_virt(virt, size);
355 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
356 printk(KERN_ERR PREFIX "%s: bad address %p\n", __func__, virt);
361 list_del(&map->list);
362 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
367 EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
369 void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
371 if (!acpi_gbl_permanent_mmap)
372 __acpi_unmap_table(virt, size);
375 int acpi_os_map_generic_address(struct acpi_generic_address *addr)
379 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
382 if (!addr->address || !addr->bit_width)
385 virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
391 EXPORT_SYMBOL_GPL(acpi_os_map_generic_address);
393 void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
397 acpi_size size = addr->bit_width / 8;
399 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
402 if (!addr->address || !addr->bit_width)
405 spin_lock_irqsave(&acpi_ioremap_lock, flags);
406 virt = acpi_map_vaddr_lookup(addr->address, size);
407 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
409 acpi_os_unmap_memory(virt, size);
411 EXPORT_SYMBOL_GPL(acpi_os_unmap_generic_address);
413 #ifdef ACPI_FUTURE_USAGE
415 acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
418 return AE_BAD_PARAMETER;
420 *phys = virt_to_phys(virt);
426 #define ACPI_MAX_OVERRIDE_LEN 100
428 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
431 acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
432 acpi_string * new_val)
434 if (!init_val || !new_val)
435 return AE_BAD_PARAMETER;
438 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
439 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
441 *new_val = acpi_os_name;
448 acpi_os_table_override(struct acpi_table_header * existing_table,
449 struct acpi_table_header ** new_table)
451 if (!existing_table || !new_table)
452 return AE_BAD_PARAMETER;
456 #ifdef CONFIG_ACPI_CUSTOM_DSDT
457 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
458 *new_table = (struct acpi_table_header *)AmlCode;
460 if (*new_table != NULL) {
461 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
462 "this is unsafe: tainting kernel\n",
463 existing_table->signature,
464 existing_table->oem_table_id);
465 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
470 static irqreturn_t acpi_irq(int irq, void *dev_id)
474 handled = (*acpi_irq_handler) (acpi_irq_context);
480 acpi_irq_not_handled++;
486 acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
491 acpi_irq_stats_init();
494 * Ignore the GSI from the core, and use the value in our copy of the
495 * FADT. It may not be the same if an interrupt source override exists
498 gsi = acpi_gbl_FADT.sci_interrupt;
499 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
500 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
505 acpi_irq_handler = handler;
506 acpi_irq_context = context;
507 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
508 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
509 return AE_NOT_ACQUIRED;
516 acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
519 free_irq(irq, acpi_irq);
520 acpi_irq_handler = NULL;
528 * Running in interpreter thread context, safe to sleep
531 void acpi_os_sleep(u64 ms)
533 schedule_timeout_interruptible(msecs_to_jiffies(ms));
536 void acpi_os_stall(u32 us)
544 touch_nmi_watchdog();
550 * Support ACPI 3.0 AML Timer operand
551 * Returns 64-bit free-running, monotonically increasing timer
552 * with 100ns granularity
554 u64 acpi_os_get_timer(void)
559 /* TBD: use HPET if available */
562 #ifdef CONFIG_X86_PM_TIMER
563 /* TBD: default to PM timer if HPET was not available */
566 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
571 acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
580 *(u8 *) value = inb(port);
581 } else if (width <= 16) {
582 *(u16 *) value = inw(port);
583 } else if (width <= 32) {
584 *(u32 *) value = inl(port);
592 EXPORT_SYMBOL(acpi_os_read_port);
594 acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
598 } else if (width <= 16) {
600 } else if (width <= 32) {
609 EXPORT_SYMBOL(acpi_os_write_port);
612 acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
615 void __iomem *virt_addr;
616 int size = width / 8, unmap = 0;
619 spin_lock_irqsave(&acpi_ioremap_lock, flags);
620 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
621 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
623 virt_addr = ioremap(phys_addr, size);
631 *(u8 *) value = readb(virt_addr);
634 *(u16 *) value = readw(virt_addr);
637 *(u32 *) value = readl(virt_addr);
650 acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
652 void __iomem *virt_addr;
653 int size = width / 8, unmap = 0;
656 spin_lock_irqsave(&acpi_ioremap_lock, flags);
657 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
658 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
660 virt_addr = ioremap(phys_addr, size);
666 writeb(value, virt_addr);
669 writew(value, virt_addr);
672 writel(value, virt_addr);
685 acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
686 u32 *value, u32 width)
691 return AE_BAD_PARAMETER;
707 result = raw_pci_read(pci_id->segment, pci_id->bus,
708 PCI_DEVFN(pci_id->device, pci_id->function),
711 return (result ? AE_ERROR : AE_OK);
715 acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
716 u64 value, u32 width)
734 result = raw_pci_write(pci_id->segment, pci_id->bus,
735 PCI_DEVFN(pci_id->device, pci_id->function),
738 return (result ? AE_ERROR : AE_OK);
741 /* TODO: Change code to take advantage of driver model more */
742 static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
743 acpi_handle chandle, /* current node */
744 struct acpi_pci_id **id,
745 int *is_bridge, u8 * bus_number)
748 struct acpi_pci_id *pci_id = *id;
750 unsigned long long temp;
751 acpi_object_type type;
753 acpi_get_parent(chandle, &handle);
754 if (handle != rhandle) {
755 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
758 status = acpi_get_type(handle, &type);
759 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
762 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
764 if (ACPI_SUCCESS(status)) {
766 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
767 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
770 pci_id->bus = *bus_number;
772 /* any nicer way to get bus number of bridge ? */
774 acpi_os_read_pci_configuration(pci_id, 0x0e, &val,
776 if (ACPI_SUCCESS(status)
777 && ((val & 0x7f) == 1 || (val & 0x7f) == 2)) {
779 acpi_os_read_pci_configuration(pci_id, 0x18,
781 if (!ACPI_SUCCESS(status)) {
782 /* Certainly broken... FIX ME */
788 acpi_os_read_pci_configuration(pci_id, 0x19,
790 if (ACPI_SUCCESS(status)) {
799 void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
800 acpi_handle chandle, /* current node */
801 struct acpi_pci_id **id)
804 u8 bus_number = (*id)->bus;
806 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
809 static void acpi_os_execute_deferred(struct work_struct *work)
811 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
814 acpi_os_wait_events_complete(NULL);
816 dpc->function(dpc->context);
820 /*******************************************************************************
822 * FUNCTION: acpi_os_execute
824 * PARAMETERS: Type - Type of the callback
825 * Function - Function to be executed
826 * Context - Function parameters
830 * DESCRIPTION: Depending on type, either queues function for deferred execution or
831 * immediately executes function on a separate thread.
833 ******************************************************************************/
835 static acpi_status __acpi_os_execute(acpi_execute_type type,
836 acpi_osd_exec_callback function, void *context, int hp)
838 acpi_status status = AE_OK;
839 struct acpi_os_dpc *dpc;
840 struct workqueue_struct *queue;
842 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
843 "Scheduling function [%p(%p)] for deferred execution.\n",
847 * Allocate/initialize DPC structure. Note that this memory will be
848 * freed by the callee. The kernel handles the work_struct list in a
849 * way that allows us to also free its memory inside the callee.
850 * Because we may want to schedule several tasks with different
851 * parameters we can't use the approach some kernel code uses of
852 * having a static work_struct.
855 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
859 dpc->function = function;
860 dpc->context = context;
863 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
864 * because the hotplug code may call driver .remove() functions,
865 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
866 * to flush these workqueues.
868 queue = hp ? kacpi_hotplug_wq :
869 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
870 dpc->wait = hp ? 1 : 0;
872 if (queue == kacpi_hotplug_wq)
873 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
874 else if (queue == kacpi_notify_wq)
875 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
877 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
880 * On some machines, a software-initiated SMI causes corruption unless
881 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
882 * typically it's done in GPE-related methods that are run via
883 * workqueues, so we can avoid the known corruption cases by always
886 ret = queue_work_on(0, queue, &dpc->work);
889 printk(KERN_ERR PREFIX
890 "Call to queue_work() failed.\n");
897 acpi_status acpi_os_execute(acpi_execute_type type,
898 acpi_osd_exec_callback function, void *context)
900 return __acpi_os_execute(type, function, context, 0);
902 EXPORT_SYMBOL(acpi_os_execute);
904 acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
907 return __acpi_os_execute(0, function, context, 1);
910 void acpi_os_wait_events_complete(void *context)
912 flush_workqueue(kacpid_wq);
913 flush_workqueue(kacpi_notify_wq);
916 EXPORT_SYMBOL(acpi_os_wait_events_complete);
919 * Allocate the memory for a spinlock and initialize it.
921 acpi_status acpi_os_create_lock(acpi_spinlock * handle)
923 spin_lock_init(*handle);
929 * Deallocate the memory for a spinlock.
931 void acpi_os_delete_lock(acpi_spinlock handle)
937 acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
939 struct semaphore *sem = NULL;
941 sem = acpi_os_allocate(sizeof(struct semaphore));
944 memset(sem, 0, sizeof(struct semaphore));
946 sema_init(sem, initial_units);
948 *handle = (acpi_handle *) sem;
950 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
951 *handle, initial_units));
957 * TODO: A better way to delete semaphores? Linux doesn't have a
958 * 'delete_semaphore()' function -- may result in an invalid
959 * pointer dereference for non-synchronized consumers. Should
960 * we at least check for blocked threads and signal/cancel them?
963 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
965 struct semaphore *sem = (struct semaphore *)handle;
968 return AE_BAD_PARAMETER;
970 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
972 BUG_ON(!list_empty(&sem->wait_list));
980 * TODO: Support for units > 1?
982 acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
984 acpi_status status = AE_OK;
985 struct semaphore *sem = (struct semaphore *)handle;
989 if (!sem || (units < 1))
990 return AE_BAD_PARAMETER;
995 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
996 handle, units, timeout));
998 if (timeout == ACPI_WAIT_FOREVER)
999 jiffies = MAX_SCHEDULE_TIMEOUT;
1001 jiffies = msecs_to_jiffies(timeout);
1003 ret = down_timeout(sem, jiffies);
1007 if (ACPI_FAILURE(status)) {
1008 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
1009 "Failed to acquire semaphore[%p|%d|%d], %s",
1010 handle, units, timeout,
1011 acpi_format_exception(status)));
1013 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
1014 "Acquired semaphore[%p|%d|%d]", handle,
1022 * TODO: Support for units > 1?
1024 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
1026 struct semaphore *sem = (struct semaphore *)handle;
1028 if (!sem || (units < 1))
1029 return AE_BAD_PARAMETER;
1034 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1042 #ifdef ACPI_FUTURE_USAGE
1043 u32 acpi_os_get_line(char *buffer)
1046 #ifdef ENABLE_DEBUGGER
1047 if (acpi_in_debugger) {
1050 kdb_read(buffer, sizeof(line_buf));
1052 /* remove the CR kdb includes */
1053 chars = strlen(buffer) - 1;
1054 buffer[chars] = '\0';
1060 #endif /* ACPI_FUTURE_USAGE */
1062 acpi_status acpi_os_signal(u32 function, void *info)
1065 case ACPI_SIGNAL_FATAL:
1066 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1068 case ACPI_SIGNAL_BREAKPOINT:
1071 * ACPI spec. says to treat it as a NOP unless
1072 * you are debugging. So if/when we integrate
1073 * AML debugger into the kernel debugger its
1074 * hook will go here. But until then it is
1075 * not useful to print anything on breakpoints.
1085 static int __init acpi_os_name_setup(char *str)
1087 char *p = acpi_os_name;
1088 int count = ACPI_MAX_OVERRIDE_LEN - 1;
1093 for (; count-- && str && *str; str++) {
1094 if (isalnum(*str) || *str == ' ' || *str == ':')
1096 else if (*str == '\'' || *str == '"')
1107 __setup("acpi_os_name=", acpi_os_name_setup);
1109 static void __init set_osi_linux(unsigned int enable)
1111 if (osi_linux.enable != enable) {
1112 osi_linux.enable = enable;
1113 printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
1114 enable ? "Add": "Delet");
1119 static void __init acpi_cmdline_osi_linux(unsigned int enable)
1121 osi_linux.cmdline = 1; /* cmdline set the default */
1122 set_osi_linux(enable);
1127 void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1129 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
1131 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1136 osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
1138 set_osi_linux(enable);
1144 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1146 * empty string disables _OSI
1147 * string starting with '!' disables that string
1148 * otherwise string is added to list, augmenting built-in strings
1150 int __init acpi_osi_setup(char *str)
1152 if (str == NULL || *str == '\0') {
1153 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1154 acpi_gbl_create_osi_method = FALSE;
1155 } else if (!strcmp("!Linux", str)) {
1156 acpi_cmdline_osi_linux(0); /* !enable */
1157 } else if (*str == '!') {
1158 if (acpi_osi_invalidate(++str) == AE_OK)
1159 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1160 } else if (!strcmp("Linux", str)) {
1161 acpi_cmdline_osi_linux(1); /* enable */
1162 } else if (*osi_additional_string == '\0') {
1163 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
1164 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1170 __setup("acpi_osi=", acpi_osi_setup);
1172 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1173 static int __init acpi_serialize_setup(char *str)
1175 printk(KERN_INFO PREFIX "serialize enabled\n");
1177 acpi_gbl_all_methods_serialized = TRUE;
1182 __setup("acpi_serialize", acpi_serialize_setup);
1184 /* Check of resource interference between native drivers and ACPI
1185 * OperationRegions (SystemIO and System Memory only).
1186 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1187 * in arbitrary AML code and can interfere with legacy drivers.
1188 * acpi_enforce_resources= can be set to:
1190 * - strict (default) (2)
1191 * -> further driver trying to access the resources will not load
1193 * -> further driver trying to access the resources will load, but you
1194 * get a system message that something might go wrong...
1197 * -> ACPI Operation Region resources will not be registered
1200 #define ENFORCE_RESOURCES_STRICT 2
1201 #define ENFORCE_RESOURCES_LAX 1
1202 #define ENFORCE_RESOURCES_NO 0
1204 static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1206 static int __init acpi_enforce_resources_setup(char *str)
1208 if (str == NULL || *str == '\0')
1211 if (!strcmp("strict", str))
1212 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1213 else if (!strcmp("lax", str))
1214 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1215 else if (!strcmp("no", str))
1216 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1221 __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1223 /* Check for resource conflicts between ACPI OperationRegions and native
1225 int acpi_check_resource_conflict(const struct resource *res)
1227 struct acpi_res_list *res_list_elem;
1231 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1233 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1236 ioport = res->flags & IORESOURCE_IO;
1238 spin_lock(&acpi_res_lock);
1239 list_for_each_entry(res_list_elem, &resource_list_head,
1241 if (ioport && (res_list_elem->resource_type
1242 != ACPI_ADR_SPACE_SYSTEM_IO))
1244 if (!ioport && (res_list_elem->resource_type
1245 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1248 if (res->end < res_list_elem->start
1249 || res_list_elem->end < res->start)
1254 spin_unlock(&acpi_res_lock);
1257 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
1258 printk(KERN_WARNING "ACPI: resource %s %pR"
1259 " conflicts with ACPI region %s %pR\n",
1260 res->name, res, res_list_elem->name,
1262 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1263 printk(KERN_NOTICE "ACPI: This conflict may"
1264 " cause random problems and system"
1266 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1267 " for this device, you should use it instead of"
1268 " the native driver\n");
1270 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1275 EXPORT_SYMBOL(acpi_check_resource_conflict);
1277 int acpi_check_region(resource_size_t start, resource_size_t n,
1280 struct resource res = {
1282 .end = start + n - 1,
1284 .flags = IORESOURCE_IO,
1287 return acpi_check_resource_conflict(&res);
1289 EXPORT_SYMBOL(acpi_check_region);
1291 int acpi_check_mem_region(resource_size_t start, resource_size_t n,
1294 struct resource res = {
1296 .end = start + n - 1,
1298 .flags = IORESOURCE_MEM,
1301 return acpi_check_resource_conflict(&res);
1304 EXPORT_SYMBOL(acpi_check_mem_region);
1307 * Let drivers know whether the resource checks are effective
1309 int acpi_resources_are_enforced(void)
1311 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1313 EXPORT_SYMBOL(acpi_resources_are_enforced);
1316 * Acquire a spinlock.
1318 * handle is a pointer to the spinlock_t.
1321 acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
1323 acpi_cpu_flags flags;
1324 spin_lock_irqsave(lockp, flags);
1329 * Release a spinlock. See above.
1332 void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
1334 spin_unlock_irqrestore(lockp, flags);
1337 #ifndef ACPI_USE_LOCAL_CACHE
1339 /*******************************************************************************
1341 * FUNCTION: acpi_os_create_cache
1343 * PARAMETERS: name - Ascii name for the cache
1344 * size - Size of each cached object
1345 * depth - Maximum depth of the cache (in objects) <ignored>
1346 * cache - Where the new cache object is returned
1350 * DESCRIPTION: Create a cache object
1352 ******************************************************************************/
1355 acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
1357 *cache = kmem_cache_create(name, size, 0, 0, NULL);
1364 /*******************************************************************************
1366 * FUNCTION: acpi_os_purge_cache
1368 * PARAMETERS: Cache - Handle to cache object
1372 * DESCRIPTION: Free all objects within the requested cache.
1374 ******************************************************************************/
1376 acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
1378 kmem_cache_shrink(cache);
1382 /*******************************************************************************
1384 * FUNCTION: acpi_os_delete_cache
1386 * PARAMETERS: Cache - Handle to cache object
1390 * DESCRIPTION: Free all objects within the requested cache and delete the
1393 ******************************************************************************/
1395 acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
1397 kmem_cache_destroy(cache);
1401 /*******************************************************************************
1403 * FUNCTION: acpi_os_release_object
1405 * PARAMETERS: Cache - Handle to cache object
1406 * Object - The object to be released
1410 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1411 * the object is deleted.
1413 ******************************************************************************/
1415 acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
1417 kmem_cache_free(cache, object);
1421 /******************************************************************************
1423 * FUNCTION: acpi_os_validate_interface
1425 * PARAMETERS: interface - Requested interface to be validated
1427 * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
1429 * DESCRIPTION: Match an interface string to the interfaces supported by the
1430 * host. Strings originate from an AML call to the _OSI method.
1432 *****************************************************************************/
1435 acpi_os_validate_interface (char *interface)
1437 if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1439 if (!strcmp("Linux", interface)) {
1441 printk(KERN_NOTICE PREFIX
1442 "BIOS _OSI(Linux) query %s%s\n",
1443 osi_linux.enable ? "honored" : "ignored",
1444 osi_linux.cmdline ? " via cmdline" :
1445 osi_linux.dmi ? " via DMI" : "");
1447 if (osi_linux.enable)
1453 static inline int acpi_res_list_add(struct acpi_res_list *res)
1455 struct acpi_res_list *res_list_elem;
1457 list_for_each_entry(res_list_elem, &resource_list_head,
1460 if (res->resource_type == res_list_elem->resource_type &&
1461 res->start == res_list_elem->start &&
1462 res->end == res_list_elem->end) {
1465 * The Region(addr,len) already exist in the list,
1466 * just increase the count
1469 res_list_elem->count++;
1475 list_add(&res->resource_list, &resource_list_head);
1479 static inline void acpi_res_list_del(struct acpi_res_list *res)
1481 struct acpi_res_list *res_list_elem;
1483 list_for_each_entry(res_list_elem, &resource_list_head,
1486 if (res->resource_type == res_list_elem->resource_type &&
1487 res->start == res_list_elem->start &&
1488 res->end == res_list_elem->end) {
1491 * If the res count is decreased to 0,
1492 * remove and free it
1495 if (--res_list_elem->count == 0) {
1496 list_del(&res_list_elem->resource_list);
1497 kfree(res_list_elem);
1505 acpi_os_invalidate_address(
1507 acpi_physical_address address,
1510 struct acpi_res_list res;
1513 case ACPI_ADR_SPACE_SYSTEM_IO:
1514 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1515 /* Only interference checks against SystemIO and SystemMemory
1517 res.start = address;
1518 res.end = address + length - 1;
1519 res.resource_type = space_id;
1520 spin_lock(&acpi_res_lock);
1521 acpi_res_list_del(&res);
1522 spin_unlock(&acpi_res_lock);
1524 case ACPI_ADR_SPACE_PCI_CONFIG:
1525 case ACPI_ADR_SPACE_EC:
1526 case ACPI_ADR_SPACE_SMBUS:
1527 case ACPI_ADR_SPACE_CMOS:
1528 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1529 case ACPI_ADR_SPACE_DATA_TABLE:
1530 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1536 /******************************************************************************
1538 * FUNCTION: acpi_os_validate_address
1540 * PARAMETERS: space_id - ACPI space ID
1541 * address - Physical address
1542 * length - Address length
1544 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1545 * should return AE_AML_ILLEGAL_ADDRESS.
1547 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1548 * the addresses accessed by AML operation regions.
1550 *****************************************************************************/
1553 acpi_os_validate_address (
1555 acpi_physical_address address,
1559 struct acpi_res_list *res;
1561 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1565 case ACPI_ADR_SPACE_SYSTEM_IO:
1566 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1567 /* Only interference checks against SystemIO and SystemMemory
1569 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1572 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1573 strlcpy(res->name, name, 5);
1574 res->start = address;
1575 res->end = address + length - 1;
1576 res->resource_type = space_id;
1577 spin_lock(&acpi_res_lock);
1578 added = acpi_res_list_add(res);
1579 spin_unlock(&acpi_res_lock);
1580 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1581 "name: %s\n", added ? "Added" : "Already exist",
1582 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
1583 ? "SystemIO" : "System Memory",
1584 (unsigned long long)res->start,
1585 (unsigned long long)res->end,
1590 case ACPI_ADR_SPACE_PCI_CONFIG:
1591 case ACPI_ADR_SPACE_EC:
1592 case ACPI_ADR_SPACE_SMBUS:
1593 case ACPI_ADR_SPACE_CMOS:
1594 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1595 case ACPI_ADR_SPACE_DATA_TABLE:
1596 case ACPI_ADR_SPACE_FIXED_HARDWARE: