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 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 struct workqueue_struct *kacpi_hotplug_wq;
84 EXPORT_SYMBOL(kacpi_hotplug_wq);
87 * This list of permanent mappings is for memory that may be accessed from
88 * interrupt context, where we can't do the ioremap().
91 struct list_head list;
93 acpi_physical_address phys;
95 unsigned long refcount;
98 static LIST_HEAD(acpi_ioremaps);
99 static DEFINE_MUTEX(acpi_ioremap_lock);
101 static void __init acpi_osi_setup_late(void);
104 * The story of _OSI(Linux)
106 * From pre-history through Linux-2.6.22,
107 * Linux responded TRUE upon a BIOS OSI(Linux) query.
109 * Unfortunately, reference BIOS writers got wind of this
110 * and put OSI(Linux) in their example code, quickly exposing
111 * this string as ill-conceived and opening the door to
112 * an un-bounded number of BIOS incompatibilities.
114 * For example, OSI(Linux) was used on resume to re-POST a
115 * video card on one system, because Linux at that time
116 * could not do a speedy restore in its native driver.
117 * But then upon gaining quick native restore capability,
118 * Linux has no way to tell the BIOS to skip the time-consuming
119 * POST -- putting Linux at a permanent performance disadvantage.
120 * On another system, the BIOS writer used OSI(Linux)
121 * to infer native OS support for IPMI! On other systems,
122 * OSI(Linux) simply got in the way of Linux claiming to
123 * be compatible with other operating systems, exposing
124 * BIOS issues such as skipped device initialization.
126 * So "Linux" turned out to be a really poor chose of
127 * OSI string, and from Linux-2.6.23 onward we respond FALSE.
129 * BIOS writers should NOT query _OSI(Linux) on future systems.
130 * Linux will complain on the console when it sees it, and return FALSE.
131 * To get Linux to return TRUE for your system will require
132 * a kernel source update to add a DMI entry,
133 * or boot with "acpi_osi=Linux"
136 static struct osi_linux {
137 unsigned int enable:1;
139 unsigned int cmdline:1;
140 } osi_linux = {0, 0, 0};
142 static u32 acpi_osi_handler(acpi_string interface, u32 supported)
144 if (!strcmp("Linux", interface)) {
146 printk_once(KERN_NOTICE FW_BUG PREFIX
147 "BIOS _OSI(Linux) query %s%s\n",
148 osi_linux.enable ? "honored" : "ignored",
149 osi_linux.cmdline ? " via cmdline" :
150 osi_linux.dmi ? " via DMI" : "");
156 static void __init acpi_request_region (struct acpi_generic_address *gas,
157 unsigned int length, char *desc)
161 /* Handle possible alignment issues */
162 memcpy(&addr, &gas->address, sizeof(addr));
163 if (!addr || !length)
166 /* Resources are never freed */
167 if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
168 request_region(addr, length, desc);
169 else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
170 request_mem_region(addr, length, desc);
173 static int __init acpi_reserve_resources(void)
175 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
176 "ACPI PM1a_EVT_BLK");
178 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
179 "ACPI PM1b_EVT_BLK");
181 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
182 "ACPI PM1a_CNT_BLK");
184 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
185 "ACPI PM1b_CNT_BLK");
187 if (acpi_gbl_FADT.pm_timer_length == 4)
188 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
190 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
193 /* Length of GPE blocks must be a non-negative multiple of 2 */
195 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
196 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
197 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
199 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
200 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
201 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
205 device_initcall(acpi_reserve_resources);
207 void acpi_os_printf(const char *fmt, ...)
211 acpi_os_vprintf(fmt, args);
215 void acpi_os_vprintf(const char *fmt, va_list args)
217 static char buffer[512];
219 vsprintf(buffer, fmt, args);
221 #ifdef ENABLE_DEBUGGER
222 if (acpi_in_debugger) {
223 kdb_printf("%s", buffer);
225 printk(KERN_CONT "%s", buffer);
228 printk(KERN_CONT "%s", buffer);
233 static unsigned long acpi_rsdp;
234 static int __init setup_acpi_rsdp(char *arg)
236 acpi_rsdp = simple_strtoul(arg, NULL, 16);
239 early_param("acpi_rsdp", setup_acpi_rsdp);
242 acpi_physical_address __init acpi_os_get_root_pointer(void)
250 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
252 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
255 printk(KERN_ERR PREFIX
256 "System description tables not found\n");
260 acpi_physical_address pa = 0;
262 acpi_find_root_pointer(&pa);
267 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
268 static struct acpi_ioremap *
269 acpi_map_lookup(acpi_physical_address phys, acpi_size size)
271 struct acpi_ioremap *map;
273 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
274 if (map->phys <= phys &&
275 phys + size <= map->phys + map->size)
281 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
282 static void __iomem *
283 acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
285 struct acpi_ioremap *map;
287 map = acpi_map_lookup(phys, size);
289 return map->virt + (phys - map->phys);
294 void __iomem *acpi_os_get_iomem(acpi_physical_address phys, unsigned int size)
296 struct acpi_ioremap *map;
297 void __iomem *virt = NULL;
299 mutex_lock(&acpi_ioremap_lock);
300 map = acpi_map_lookup(phys, size);
302 virt = map->virt + (phys - map->phys);
305 mutex_unlock(&acpi_ioremap_lock);
308 EXPORT_SYMBOL_GPL(acpi_os_get_iomem);
310 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
311 static struct acpi_ioremap *
312 acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
314 struct acpi_ioremap *map;
316 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
317 if (map->virt <= virt &&
318 virt + size <= map->virt + map->size)
324 void __iomem *__init_refok
325 acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
327 struct acpi_ioremap *map;
329 acpi_physical_address pg_off;
332 if (phys > ULONG_MAX) {
333 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
337 if (!acpi_gbl_permanent_mmap)
338 return __acpi_map_table((unsigned long)phys, size);
340 mutex_lock(&acpi_ioremap_lock);
341 /* Check if there's a suitable mapping already. */
342 map = acpi_map_lookup(phys, size);
348 map = kzalloc(sizeof(*map), GFP_KERNEL);
350 mutex_unlock(&acpi_ioremap_lock);
354 pg_off = round_down(phys, PAGE_SIZE);
355 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
356 virt = acpi_os_ioremap(pg_off, pg_sz);
358 mutex_unlock(&acpi_ioremap_lock);
363 INIT_LIST_HEAD(&map->list);
369 list_add_tail_rcu(&map->list, &acpi_ioremaps);
372 mutex_unlock(&acpi_ioremap_lock);
373 return map->virt + (phys - map->phys);
375 EXPORT_SYMBOL_GPL(acpi_os_map_memory);
377 static void acpi_os_drop_map_ref(struct acpi_ioremap *map)
379 if (!--map->refcount)
380 list_del_rcu(&map->list);
383 static void acpi_os_map_cleanup(struct acpi_ioremap *map)
385 if (!map->refcount) {
392 void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
394 struct acpi_ioremap *map;
396 if (!acpi_gbl_permanent_mmap) {
397 __acpi_unmap_table(virt, size);
401 mutex_lock(&acpi_ioremap_lock);
402 map = acpi_map_lookup_virt(virt, size);
404 mutex_unlock(&acpi_ioremap_lock);
405 WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
408 acpi_os_drop_map_ref(map);
409 mutex_unlock(&acpi_ioremap_lock);
411 acpi_os_map_cleanup(map);
413 EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
415 void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
417 if (!acpi_gbl_permanent_mmap)
418 __acpi_unmap_table(virt, size);
421 int acpi_os_map_generic_address(struct acpi_generic_address *gas)
426 if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
429 /* Handle possible alignment issues */
430 memcpy(&addr, &gas->address, sizeof(addr));
431 if (!addr || !gas->bit_width)
434 virt = acpi_os_map_memory(addr, gas->bit_width / 8);
440 EXPORT_SYMBOL(acpi_os_map_generic_address);
442 void acpi_os_unmap_generic_address(struct acpi_generic_address *gas)
445 struct acpi_ioremap *map;
447 if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
450 /* Handle possible alignment issues */
451 memcpy(&addr, &gas->address, sizeof(addr));
452 if (!addr || !gas->bit_width)
455 mutex_lock(&acpi_ioremap_lock);
456 map = acpi_map_lookup(addr, gas->bit_width / 8);
458 mutex_unlock(&acpi_ioremap_lock);
461 acpi_os_drop_map_ref(map);
462 mutex_unlock(&acpi_ioremap_lock);
464 acpi_os_map_cleanup(map);
466 EXPORT_SYMBOL(acpi_os_unmap_generic_address);
468 #ifdef ACPI_FUTURE_USAGE
470 acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
473 return AE_BAD_PARAMETER;
475 *phys = virt_to_phys(virt);
481 #define ACPI_MAX_OVERRIDE_LEN 100
483 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
486 acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
487 acpi_string * new_val)
489 if (!init_val || !new_val)
490 return AE_BAD_PARAMETER;
493 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
494 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
496 *new_val = acpi_os_name;
503 acpi_os_table_override(struct acpi_table_header * existing_table,
504 struct acpi_table_header ** new_table)
506 if (!existing_table || !new_table)
507 return AE_BAD_PARAMETER;
511 #ifdef CONFIG_ACPI_CUSTOM_DSDT
512 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
513 *new_table = (struct acpi_table_header *)AmlCode;
515 if (*new_table != NULL) {
516 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
517 "this is unsafe: tainting kernel\n",
518 existing_table->signature,
519 existing_table->oem_table_id);
520 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
525 static irqreturn_t acpi_irq(int irq, void *dev_id)
529 handled = (*acpi_irq_handler) (acpi_irq_context);
535 acpi_irq_not_handled++;
541 acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
546 acpi_irq_stats_init();
549 * ACPI interrupts different from the SCI in our copy of the FADT are
552 if (gsi != acpi_gbl_FADT.sci_interrupt)
553 return AE_BAD_PARAMETER;
555 if (acpi_irq_handler)
556 return AE_ALREADY_ACQUIRED;
558 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
559 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
564 acpi_irq_handler = handler;
565 acpi_irq_context = context;
566 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
567 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
568 acpi_irq_handler = NULL;
569 return AE_NOT_ACQUIRED;
575 acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
577 if (irq != acpi_gbl_FADT.sci_interrupt)
578 return AE_BAD_PARAMETER;
580 free_irq(irq, acpi_irq);
581 acpi_irq_handler = NULL;
587 * Running in interpreter thread context, safe to sleep
590 void acpi_os_sleep(u64 ms)
592 schedule_timeout_interruptible(msecs_to_jiffies(ms));
595 void acpi_os_stall(u32 us)
603 touch_nmi_watchdog();
609 * Support ACPI 3.0 AML Timer operand
610 * Returns 64-bit free-running, monotonically increasing timer
611 * with 100ns granularity
613 u64 acpi_os_get_timer(void)
618 /* TBD: use HPET if available */
621 #ifdef CONFIG_X86_PM_TIMER
622 /* TBD: default to PM timer if HPET was not available */
625 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
630 acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
639 *(u8 *) value = inb(port);
640 } else if (width <= 16) {
641 *(u16 *) value = inw(port);
642 } else if (width <= 32) {
643 *(u32 *) value = inl(port);
651 EXPORT_SYMBOL(acpi_os_read_port);
653 acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
657 } else if (width <= 16) {
659 } else if (width <= 32) {
668 EXPORT_SYMBOL(acpi_os_write_port);
671 acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
673 void __iomem *virt_addr;
674 unsigned int size = width / 8;
679 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
682 virt_addr = acpi_os_ioremap(phys_addr, size);
684 return AE_BAD_ADDRESS;
693 *(u8 *) value = readb(virt_addr);
696 *(u16 *) value = readw(virt_addr);
699 *(u32 *) value = readl(virt_addr);
714 static inline u64 read64(const volatile void __iomem *addr)
719 static inline u64 read64(const volatile void __iomem *addr)
724 return l | (h << 32);
729 acpi_os_read_memory64(acpi_physical_address phys_addr, u64 *value, u32 width)
731 void __iomem *virt_addr;
732 unsigned int size = width / 8;
737 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
740 virt_addr = acpi_os_ioremap(phys_addr, size);
742 return AE_BAD_ADDRESS;
751 *(u8 *) value = readb(virt_addr);
754 *(u16 *) value = readw(virt_addr);
757 *(u32 *) value = readl(virt_addr);
760 *(u64 *) value = read64(virt_addr);
775 acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
777 void __iomem *virt_addr;
778 unsigned int size = width / 8;
782 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
785 virt_addr = acpi_os_ioremap(phys_addr, size);
787 return AE_BAD_ADDRESS;
793 writeb(value, virt_addr);
796 writew(value, virt_addr);
799 writel(value, virt_addr);
814 static inline void write64(u64 val, volatile void __iomem *addr)
819 static inline void write64(u64 val, volatile void __iomem *addr)
822 writel(val>>32, addr+4);
827 acpi_os_write_memory64(acpi_physical_address phys_addr, u64 value, u32 width)
829 void __iomem *virt_addr;
830 unsigned int size = width / 8;
834 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
837 virt_addr = acpi_os_ioremap(phys_addr, size);
839 return AE_BAD_ADDRESS;
845 writeb(value, virt_addr);
848 writew(value, virt_addr);
851 writel(value, virt_addr);
854 write64(value, virt_addr);
869 acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
870 u64 *value, u32 width)
876 return AE_BAD_PARAMETER;
892 result = raw_pci_read(pci_id->segment, pci_id->bus,
893 PCI_DEVFN(pci_id->device, pci_id->function),
894 reg, size, &value32);
897 return (result ? AE_ERROR : AE_OK);
901 acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
902 u64 value, u32 width)
920 result = raw_pci_write(pci_id->segment, pci_id->bus,
921 PCI_DEVFN(pci_id->device, pci_id->function),
924 return (result ? AE_ERROR : AE_OK);
927 static void acpi_os_execute_deferred(struct work_struct *work)
929 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
932 acpi_os_wait_events_complete(NULL);
934 dpc->function(dpc->context);
938 /*******************************************************************************
940 * FUNCTION: acpi_os_execute
942 * PARAMETERS: Type - Type of the callback
943 * Function - Function to be executed
944 * Context - Function parameters
948 * DESCRIPTION: Depending on type, either queues function for deferred execution or
949 * immediately executes function on a separate thread.
951 ******************************************************************************/
953 static acpi_status __acpi_os_execute(acpi_execute_type type,
954 acpi_osd_exec_callback function, void *context, int hp)
956 acpi_status status = AE_OK;
957 struct acpi_os_dpc *dpc;
958 struct workqueue_struct *queue;
960 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
961 "Scheduling function [%p(%p)] for deferred execution.\n",
965 * Allocate/initialize DPC structure. Note that this memory will be
966 * freed by the callee. The kernel handles the work_struct list in a
967 * way that allows us to also free its memory inside the callee.
968 * Because we may want to schedule several tasks with different
969 * parameters we can't use the approach some kernel code uses of
970 * having a static work_struct.
973 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
977 dpc->function = function;
978 dpc->context = context;
981 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
982 * because the hotplug code may call driver .remove() functions,
983 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
984 * to flush these workqueues.
986 queue = hp ? kacpi_hotplug_wq :
987 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
988 dpc->wait = hp ? 1 : 0;
990 if (queue == kacpi_hotplug_wq)
991 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
992 else if (queue == kacpi_notify_wq)
993 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
995 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
998 * On some machines, a software-initiated SMI causes corruption unless
999 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
1000 * typically it's done in GPE-related methods that are run via
1001 * workqueues, so we can avoid the known corruption cases by always
1002 * queueing on CPU 0.
1004 ret = queue_work_on(0, queue, &dpc->work);
1007 printk(KERN_ERR PREFIX
1008 "Call to queue_work() failed.\n");
1015 acpi_status acpi_os_execute(acpi_execute_type type,
1016 acpi_osd_exec_callback function, void *context)
1018 return __acpi_os_execute(type, function, context, 0);
1020 EXPORT_SYMBOL(acpi_os_execute);
1022 acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
1025 return __acpi_os_execute(0, function, context, 1);
1028 void acpi_os_wait_events_complete(void *context)
1030 flush_workqueue(kacpid_wq);
1031 flush_workqueue(kacpi_notify_wq);
1034 EXPORT_SYMBOL(acpi_os_wait_events_complete);
1037 acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
1039 struct semaphore *sem = NULL;
1041 sem = acpi_os_allocate(sizeof(struct semaphore));
1043 return AE_NO_MEMORY;
1044 memset(sem, 0, sizeof(struct semaphore));
1046 sema_init(sem, initial_units);
1048 *handle = (acpi_handle *) sem;
1050 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
1051 *handle, initial_units));
1057 * TODO: A better way to delete semaphores? Linux doesn't have a
1058 * 'delete_semaphore()' function -- may result in an invalid
1059 * pointer dereference for non-synchronized consumers. Should
1060 * we at least check for blocked threads and signal/cancel them?
1063 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
1065 struct semaphore *sem = (struct semaphore *)handle;
1068 return AE_BAD_PARAMETER;
1070 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
1072 BUG_ON(!list_empty(&sem->wait_list));
1080 * TODO: Support for units > 1?
1082 acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
1084 acpi_status status = AE_OK;
1085 struct semaphore *sem = (struct semaphore *)handle;
1089 if (!sem || (units < 1))
1090 return AE_BAD_PARAMETER;
1095 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
1096 handle, units, timeout));
1098 if (timeout == ACPI_WAIT_FOREVER)
1099 jiffies = MAX_SCHEDULE_TIMEOUT;
1101 jiffies = msecs_to_jiffies(timeout);
1103 ret = down_timeout(sem, jiffies);
1107 if (ACPI_FAILURE(status)) {
1108 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
1109 "Failed to acquire semaphore[%p|%d|%d], %s",
1110 handle, units, timeout,
1111 acpi_format_exception(status)));
1113 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
1114 "Acquired semaphore[%p|%d|%d]", handle,
1122 * TODO: Support for units > 1?
1124 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
1126 struct semaphore *sem = (struct semaphore *)handle;
1128 if (!sem || (units < 1))
1129 return AE_BAD_PARAMETER;
1134 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1142 #ifdef ACPI_FUTURE_USAGE
1143 u32 acpi_os_get_line(char *buffer)
1146 #ifdef ENABLE_DEBUGGER
1147 if (acpi_in_debugger) {
1150 kdb_read(buffer, sizeof(line_buf));
1152 /* remove the CR kdb includes */
1153 chars = strlen(buffer) - 1;
1154 buffer[chars] = '\0';
1160 #endif /* ACPI_FUTURE_USAGE */
1162 acpi_status acpi_os_signal(u32 function, void *info)
1165 case ACPI_SIGNAL_FATAL:
1166 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1168 case ACPI_SIGNAL_BREAKPOINT:
1171 * ACPI spec. says to treat it as a NOP unless
1172 * you are debugging. So if/when we integrate
1173 * AML debugger into the kernel debugger its
1174 * hook will go here. But until then it is
1175 * not useful to print anything on breakpoints.
1185 static int __init acpi_os_name_setup(char *str)
1187 char *p = acpi_os_name;
1188 int count = ACPI_MAX_OVERRIDE_LEN - 1;
1193 for (; count-- && str && *str; str++) {
1194 if (isalnum(*str) || *str == ' ' || *str == ':')
1196 else if (*str == '\'' || *str == '"')
1207 __setup("acpi_os_name=", acpi_os_name_setup);
1209 #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
1210 #define OSI_STRING_ENTRIES_MAX 16 /* arbitrary */
1212 struct osi_setup_entry {
1213 char string[OSI_STRING_LENGTH_MAX];
1217 static struct osi_setup_entry __initdata
1218 osi_setup_entries[OSI_STRING_ENTRIES_MAX] = {
1219 {"Module Device", true},
1220 {"Processor Device", true},
1221 {"3.0 _SCP Extensions", true},
1222 {"Processor Aggregator Device", true},
1225 void __init acpi_osi_setup(char *str)
1227 struct osi_setup_entry *osi;
1231 if (!acpi_gbl_create_osi_method)
1234 if (str == NULL || *str == '\0') {
1235 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1236 acpi_gbl_create_osi_method = FALSE;
1245 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1246 osi = &osi_setup_entries[i];
1247 if (!strcmp(osi->string, str)) {
1248 osi->enable = enable;
1250 } else if (osi->string[0] == '\0') {
1251 osi->enable = enable;
1252 strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
1258 static void __init set_osi_linux(unsigned int enable)
1260 if (osi_linux.enable != enable)
1261 osi_linux.enable = enable;
1263 if (osi_linux.enable)
1264 acpi_osi_setup("Linux");
1266 acpi_osi_setup("!Linux");
1271 static void __init acpi_cmdline_osi_linux(unsigned int enable)
1273 osi_linux.cmdline = 1; /* cmdline set the default and override DMI */
1275 set_osi_linux(enable);
1280 void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1282 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1287 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
1288 set_osi_linux(enable);
1294 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1296 * empty string disables _OSI
1297 * string starting with '!' disables that string
1298 * otherwise string is added to list, augmenting built-in strings
1300 static void __init acpi_osi_setup_late(void)
1302 struct osi_setup_entry *osi;
1307 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1308 osi = &osi_setup_entries[i];
1314 status = acpi_install_interface(str);
1316 if (ACPI_SUCCESS(status))
1317 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1319 status = acpi_remove_interface(str);
1321 if (ACPI_SUCCESS(status))
1322 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1327 static int __init osi_setup(char *str)
1329 if (str && !strcmp("Linux", str))
1330 acpi_cmdline_osi_linux(1);
1331 else if (str && !strcmp("!Linux", str))
1332 acpi_cmdline_osi_linux(0);
1334 acpi_osi_setup(str);
1339 __setup("acpi_osi=", osi_setup);
1341 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1342 static int __init acpi_serialize_setup(char *str)
1344 printk(KERN_INFO PREFIX "serialize enabled\n");
1346 acpi_gbl_all_methods_serialized = TRUE;
1351 __setup("acpi_serialize", acpi_serialize_setup);
1353 /* Check of resource interference between native drivers and ACPI
1354 * OperationRegions (SystemIO and System Memory only).
1355 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1356 * in arbitrary AML code and can interfere with legacy drivers.
1357 * acpi_enforce_resources= can be set to:
1359 * - strict (default) (2)
1360 * -> further driver trying to access the resources will not load
1362 * -> further driver trying to access the resources will load, but you
1363 * get a system message that something might go wrong...
1366 * -> ACPI Operation Region resources will not be registered
1369 #define ENFORCE_RESOURCES_STRICT 2
1370 #define ENFORCE_RESOURCES_LAX 1
1371 #define ENFORCE_RESOURCES_NO 0
1373 static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1375 static int __init acpi_enforce_resources_setup(char *str)
1377 if (str == NULL || *str == '\0')
1380 if (!strcmp("strict", str))
1381 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1382 else if (!strcmp("lax", str))
1383 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1384 else if (!strcmp("no", str))
1385 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1390 __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1392 /* Check for resource conflicts between ACPI OperationRegions and native
1394 int acpi_check_resource_conflict(const struct resource *res)
1396 acpi_adr_space_type space_id;
1401 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1403 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1406 if (res->flags & IORESOURCE_IO)
1407 space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1409 space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
1411 length = res->end - res->start + 1;
1412 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO)
1414 clash = acpi_check_address_range(space_id, res->start, length, warn);
1417 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
1418 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1419 printk(KERN_NOTICE "ACPI: This conflict may"
1420 " cause random problems and system"
1422 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1423 " for this device, you should use it instead of"
1424 " the native driver\n");
1426 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1431 EXPORT_SYMBOL(acpi_check_resource_conflict);
1433 int acpi_check_region(resource_size_t start, resource_size_t n,
1436 struct resource res = {
1438 .end = start + n - 1,
1440 .flags = IORESOURCE_IO,
1443 return acpi_check_resource_conflict(&res);
1445 EXPORT_SYMBOL(acpi_check_region);
1448 * Let drivers know whether the resource checks are effective
1450 int acpi_resources_are_enforced(void)
1452 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1454 EXPORT_SYMBOL(acpi_resources_are_enforced);
1457 * Deallocate the memory for a spinlock.
1459 void acpi_os_delete_lock(acpi_spinlock handle)
1465 * Acquire a spinlock.
1467 * handle is a pointer to the spinlock_t.
1470 acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
1472 acpi_cpu_flags flags;
1473 spin_lock_irqsave(lockp, flags);
1478 * Release a spinlock. See above.
1481 void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
1483 spin_unlock_irqrestore(lockp, flags);
1486 #ifndef ACPI_USE_LOCAL_CACHE
1488 /*******************************************************************************
1490 * FUNCTION: acpi_os_create_cache
1492 * PARAMETERS: name - Ascii name for the cache
1493 * size - Size of each cached object
1494 * depth - Maximum depth of the cache (in objects) <ignored>
1495 * cache - Where the new cache object is returned
1499 * DESCRIPTION: Create a cache object
1501 ******************************************************************************/
1504 acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
1506 *cache = kmem_cache_create(name, size, 0, 0, NULL);
1513 /*******************************************************************************
1515 * FUNCTION: acpi_os_purge_cache
1517 * PARAMETERS: Cache - Handle to cache object
1521 * DESCRIPTION: Free all objects within the requested cache.
1523 ******************************************************************************/
1525 acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
1527 kmem_cache_shrink(cache);
1531 /*******************************************************************************
1533 * FUNCTION: acpi_os_delete_cache
1535 * PARAMETERS: Cache - Handle to cache object
1539 * DESCRIPTION: Free all objects within the requested cache and delete the
1542 ******************************************************************************/
1544 acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
1546 kmem_cache_destroy(cache);
1550 /*******************************************************************************
1552 * FUNCTION: acpi_os_release_object
1554 * PARAMETERS: Cache - Handle to cache object
1555 * Object - The object to be released
1559 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1560 * the object is deleted.
1562 ******************************************************************************/
1564 acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
1566 kmem_cache_free(cache, object);
1571 acpi_status __init acpi_os_initialize(void)
1573 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1574 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1575 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1576 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1581 acpi_status __init acpi_os_initialize1(void)
1583 kacpid_wq = alloc_workqueue("kacpid", 0, 1);
1584 kacpi_notify_wq = alloc_workqueue("kacpi_notify", 0, 1);
1585 kacpi_hotplug_wq = alloc_workqueue("kacpi_hotplug", 0, 1);
1587 BUG_ON(!kacpi_notify_wq);
1588 BUG_ON(!kacpi_hotplug_wq);
1589 acpi_install_interface_handler(acpi_osi_handler);
1590 acpi_osi_setup_late();
1594 acpi_status acpi_os_terminate(void)
1596 if (acpi_irq_handler) {
1597 acpi_os_remove_interrupt_handler(acpi_gbl_FADT.sci_interrupt,
1601 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1602 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1603 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1604 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1606 destroy_workqueue(kacpid_wq);
1607 destroy_workqueue(kacpi_notify_wq);
1608 destroy_workqueue(kacpi_hotplug_wq);