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>
47 #include <linux/memblock.h>
50 #include <asm/uaccess.h>
52 #include <acpi/acpi.h>
53 #include <acpi/acpi_bus.h>
54 #include <acpi/processor.h>
56 #define _COMPONENT ACPI_OS_SERVICES
57 ACPI_MODULE_NAME("osl");
58 #define PREFIX "ACPI: "
60 acpi_osd_exec_callback function;
62 struct work_struct work;
66 #ifdef CONFIG_ACPI_CUSTOM_DSDT
67 #include CONFIG_ACPI_CUSTOM_DSDT_FILE
70 #ifdef ENABLE_DEBUGGER
71 #include <linux/kdb.h>
73 /* stuff for debugger support */
75 EXPORT_SYMBOL(acpi_in_debugger);
77 extern char line_buf[80];
78 #endif /*ENABLE_DEBUGGER */
80 static acpi_osd_handler acpi_irq_handler;
81 static void *acpi_irq_context;
82 static struct workqueue_struct *kacpid_wq;
83 static struct workqueue_struct *kacpi_notify_wq;
84 struct workqueue_struct *kacpi_hotplug_wq;
85 EXPORT_SYMBOL(kacpi_hotplug_wq);
87 struct acpi_res_list {
88 resource_size_t start;
90 acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
91 char name[5]; /* only can have a length of 4 chars, make use of this
92 one instead of res->name, no need to kalloc then */
93 struct list_head resource_list;
97 static LIST_HEAD(resource_list_head);
98 static DEFINE_SPINLOCK(acpi_res_lock);
101 * This list of permanent mappings is for memory that may be accessed from
102 * interrupt context, where we can't do the ioremap().
104 struct acpi_ioremap {
105 struct list_head list;
107 acpi_physical_address phys;
109 unsigned long refcount;
112 static LIST_HEAD(acpi_ioremaps);
113 static DEFINE_MUTEX(acpi_ioremap_lock);
115 static void __init acpi_osi_setup_late(void);
118 * The story of _OSI(Linux)
120 * From pre-history through Linux-2.6.22,
121 * Linux responded TRUE upon a BIOS OSI(Linux) query.
123 * Unfortunately, reference BIOS writers got wind of this
124 * and put OSI(Linux) in their example code, quickly exposing
125 * this string as ill-conceived and opening the door to
126 * an un-bounded number of BIOS incompatibilities.
128 * For example, OSI(Linux) was used on resume to re-POST a
129 * video card on one system, because Linux at that time
130 * could not do a speedy restore in its native driver.
131 * But then upon gaining quick native restore capability,
132 * Linux has no way to tell the BIOS to skip the time-consuming
133 * POST -- putting Linux at a permanent performance disadvantage.
134 * On another system, the BIOS writer used OSI(Linux)
135 * to infer native OS support for IPMI! On other systems,
136 * OSI(Linux) simply got in the way of Linux claiming to
137 * be compatible with other operating systems, exposing
138 * BIOS issues such as skipped device initialization.
140 * So "Linux" turned out to be a really poor chose of
141 * OSI string, and from Linux-2.6.23 onward we respond FALSE.
143 * BIOS writers should NOT query _OSI(Linux) on future systems.
144 * Linux will complain on the console when it sees it, and return FALSE.
145 * To get Linux to return TRUE for your system will require
146 * a kernel source update to add a DMI entry,
147 * or boot with "acpi_osi=Linux"
150 static struct osi_linux {
151 unsigned int enable:1;
153 unsigned int cmdline:1;
154 } osi_linux = {0, 0, 0};
156 static u32 acpi_osi_handler(acpi_string interface, u32 supported)
158 if (!strcmp("Linux", interface)) {
160 printk_once(KERN_NOTICE FW_BUG PREFIX
161 "BIOS _OSI(Linux) query %s%s\n",
162 osi_linux.enable ? "honored" : "ignored",
163 osi_linux.cmdline ? " via cmdline" :
164 osi_linux.dmi ? " via DMI" : "");
170 static void __init acpi_request_region (struct acpi_generic_address *addr,
171 unsigned int length, char *desc)
173 if (!addr->address || !length)
176 /* Resources are never freed */
177 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
178 request_region(addr->address, length, desc);
179 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
180 request_mem_region(addr->address, length, desc);
183 static int __init acpi_reserve_resources(void)
185 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
186 "ACPI PM1a_EVT_BLK");
188 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
189 "ACPI PM1b_EVT_BLK");
191 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
192 "ACPI PM1a_CNT_BLK");
194 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
195 "ACPI PM1b_CNT_BLK");
197 if (acpi_gbl_FADT.pm_timer_length == 4)
198 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
200 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
203 /* Length of GPE blocks must be a non-negative multiple of 2 */
205 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
206 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
207 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
209 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
210 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
211 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
215 device_initcall(acpi_reserve_resources);
217 void acpi_os_printf(const char *fmt, ...)
221 acpi_os_vprintf(fmt, args);
225 void acpi_os_vprintf(const char *fmt, va_list args)
227 static char buffer[512];
229 vsprintf(buffer, fmt, args);
231 #ifdef ENABLE_DEBUGGER
232 if (acpi_in_debugger) {
233 kdb_printf("%s", buffer);
235 printk(KERN_CONT "%s", buffer);
238 printk(KERN_CONT "%s", buffer);
243 static unsigned long acpi_rsdp;
244 static int __init setup_acpi_rsdp(char *arg)
246 acpi_rsdp = simple_strtoul(arg, NULL, 16);
249 early_param("acpi_rsdp", setup_acpi_rsdp);
252 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' or RCU read lock held. */
278 static struct acpi_ioremap *
279 acpi_map_lookup(acpi_physical_address phys, acpi_size size)
281 struct acpi_ioremap *map;
283 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
284 if (map->phys <= phys &&
285 phys + size <= map->phys + map->size)
291 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
292 static void __iomem *
293 acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
295 struct acpi_ioremap *map;
297 map = acpi_map_lookup(phys, size);
299 return map->virt + (phys - map->phys);
304 void __iomem *acpi_os_get_iomem(acpi_physical_address phys, unsigned int size)
306 struct acpi_ioremap *map;
307 void __iomem *virt = NULL;
309 mutex_lock(&acpi_ioremap_lock);
310 map = acpi_map_lookup(phys, size);
312 virt = map->virt + (phys - map->phys);
315 mutex_unlock(&acpi_ioremap_lock);
318 EXPORT_SYMBOL_GPL(acpi_os_get_iomem);
320 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
321 static struct acpi_ioremap *
322 acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
324 struct acpi_ioremap *map;
326 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
327 if (map->virt <= virt &&
328 virt + size <= map->virt + map->size)
334 void __iomem *__init_refok
335 acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
337 struct acpi_ioremap *map;
339 acpi_physical_address pg_off;
342 if (phys > ULONG_MAX) {
343 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
347 if (!acpi_gbl_permanent_mmap)
348 return __acpi_map_table((unsigned long)phys, size);
350 mutex_lock(&acpi_ioremap_lock);
351 /* Check if there's a suitable mapping already. */
352 map = acpi_map_lookup(phys, size);
358 map = kzalloc(sizeof(*map), GFP_KERNEL);
360 mutex_unlock(&acpi_ioremap_lock);
364 pg_off = round_down(phys, PAGE_SIZE);
365 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
366 virt = acpi_os_ioremap(pg_off, pg_sz);
368 mutex_unlock(&acpi_ioremap_lock);
373 INIT_LIST_HEAD(&map->list);
379 list_add_tail_rcu(&map->list, &acpi_ioremaps);
382 mutex_unlock(&acpi_ioremap_lock);
383 return map->virt + (phys - map->phys);
385 EXPORT_SYMBOL_GPL(acpi_os_map_memory);
387 static void acpi_os_drop_map_ref(struct acpi_ioremap *map)
389 if (!--map->refcount)
390 list_del_rcu(&map->list);
393 static void acpi_os_map_cleanup(struct acpi_ioremap *map)
395 if (!map->refcount) {
402 void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
404 struct acpi_ioremap *map;
406 if (!acpi_gbl_permanent_mmap) {
407 __acpi_unmap_table(virt, size);
411 mutex_lock(&acpi_ioremap_lock);
412 map = acpi_map_lookup_virt(virt, size);
414 mutex_unlock(&acpi_ioremap_lock);
415 WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
418 acpi_os_drop_map_ref(map);
419 mutex_unlock(&acpi_ioremap_lock);
421 acpi_os_map_cleanup(map);
423 EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
425 void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
427 if (!acpi_gbl_permanent_mmap)
428 __acpi_unmap_table(virt, size);
431 static int acpi_os_map_generic_address(struct acpi_generic_address *addr)
435 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
438 if (!addr->address || !addr->bit_width)
441 virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
448 static void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
450 struct acpi_ioremap *map;
452 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
455 if (!addr->address || !addr->bit_width)
458 mutex_lock(&acpi_ioremap_lock);
459 map = acpi_map_lookup(addr->address, addr->bit_width / 8);
461 mutex_unlock(&acpi_ioremap_lock);
464 acpi_os_drop_map_ref(map);
465 mutex_unlock(&acpi_ioremap_lock);
467 acpi_os_map_cleanup(map);
470 #ifdef ACPI_FUTURE_USAGE
472 acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
475 return AE_BAD_PARAMETER;
477 *phys = virt_to_phys(virt);
483 #define ACPI_MAX_OVERRIDE_LEN 100
485 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
488 acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
489 acpi_string * new_val)
491 if (!init_val || !new_val)
492 return AE_BAD_PARAMETER;
495 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
496 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
498 *new_val = acpi_os_name;
504 #ifdef CONFIG_ACPI_INITRD_TABLE_OVERRIDE
505 #include <asm/e820.h>
507 #define ACPI_OVERRIDE_TABLES 10
509 static unsigned long acpi_table_override_offset[ACPI_OVERRIDE_TABLES];
510 static u64 acpi_tables_inram;
512 unsigned long __initdata acpi_initrd_offset;
514 /* Copied from acpica/tbutils.c:acpi_tb_checksum() */
515 u8 __init acpi_table_checksum(u8 *buffer, u32 length)
518 u8 *end = buffer + length;
521 sum = (u8) (sum + *(buffer++));
525 /* All but ACPI_SIG_RSDP and ACPI_SIG_FACS: */
526 #define MAX_ACPI_SIGNATURE 35
527 static const char *table_sigs[MAX_ACPI_SIGNATURE] = {
528 ACPI_SIG_BERT, ACPI_SIG_CPEP, ACPI_SIG_ECDT, ACPI_SIG_EINJ,
529 ACPI_SIG_ERST, ACPI_SIG_HEST, ACPI_SIG_MADT, ACPI_SIG_MSCT,
530 ACPI_SIG_SBST, ACPI_SIG_SLIT, ACPI_SIG_SRAT, ACPI_SIG_ASF,
531 ACPI_SIG_BOOT, ACPI_SIG_DBGP, ACPI_SIG_DMAR, ACPI_SIG_HPET,
532 ACPI_SIG_IBFT, ACPI_SIG_IVRS, ACPI_SIG_MCFG, ACPI_SIG_MCHI,
533 ACPI_SIG_SLIC, ACPI_SIG_SPCR, ACPI_SIG_SPMI, ACPI_SIG_TCPA,
534 ACPI_SIG_UEFI, ACPI_SIG_WAET, ACPI_SIG_WDAT, ACPI_SIG_WDDT,
535 ACPI_SIG_WDRT, ACPI_SIG_DSDT, ACPI_SIG_FADT, ACPI_SIG_PSDT,
536 ACPI_SIG_RSDT, ACPI_SIG_XSDT, ACPI_SIG_SSDT };
538 int __init acpi_initrd_table_override(void *start_addr, void *end_addr)
541 unsigned long offset = 0, max_len = end_addr - start_addr;
544 for (table_nr = 0; table_nr < ACPI_OVERRIDE_TABLES; table_nr++) {
545 struct acpi_table_header *table;
546 if (max_len < offset + sizeof(struct acpi_table_header)) {
550 table = start_addr + offset;
552 for (sig = 0; sig < MAX_ACPI_SIGNATURE; sig++)
553 if (!memcmp(table->signature, table_sigs[sig], 4))
556 if (sig >= MAX_ACPI_SIGNATURE)
559 if (max_len < offset + table->length) {
564 if (acpi_table_checksum(start_addr + offset, table->length)) {
565 WARN(1, "%4.4s has invalid checksum\n",
569 printk(KERN_INFO "%4.4s ACPI table found in initrd"
570 " - size: %d\n", table->signature, table->length);
572 offset += table->length;
573 acpi_table_override_offset[table_nr] = offset;
579 memblock_find_in_range(0, max_low_pfn_mapped << PAGE_SHIFT,
581 if (acpi_tables_inram == MEMBLOCK_ERROR)
582 panic("Cannot find place for ACPI override tables\n");
585 * Only calling e820_add_reserve does not work and the
586 * tables are invalid (memory got used) later.
587 * memblock_x86_reserve_range works as expected and the tables
588 * won't get modified. But it's not enough because ioremap will
589 * complain later (used by acpi_os_map_memory) that the pages
590 * that should get mapped are not marked "reserved".
591 * Both memblock_x86_reserve_range and e820_add_region works fine.
593 memblock_x86_reserve_range(acpi_tables_inram,
594 acpi_tables_inram + offset,
595 "ACPI TABLE OVERRIDE");
596 e820_add_region(acpi_tables_inram, offset, E820_ACPI);
599 p = early_ioremap(acpi_tables_inram, offset);
600 memcpy(p, start_addr, offset);
601 early_iounmap(p, offset);
608 acpi_os_table_override(struct acpi_table_header * existing_table,
609 struct acpi_table_header ** new_table)
611 if (!existing_table || !new_table)
612 return AE_BAD_PARAMETER;
616 #ifdef CONFIG_ACPI_CUSTOM_DSDT
617 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
618 *new_table = (struct acpi_table_header *)AmlCode;
620 if (*new_table != NULL) {
621 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
622 "this is unsafe: tainting kernel\n",
623 existing_table->signature,
624 existing_table->oem_table_id);
625 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
631 acpi_os_phys_table_override(struct acpi_table_header *existing_table,
632 acpi_physical_address *address, u32 *table_length)
635 #ifndef CONFIG_ACPI_INITRD_TABLE_OVERRIDE
643 for (; table_nr < ACPI_OVERRIDE_TABLES &&
644 acpi_table_override_offset[table_nr]; table_nr++) {
647 struct acpi_table_header *table;
652 table_offset = acpi_table_override_offset[table_nr - 1];
654 table_len = acpi_table_override_offset[table_nr] - table_offset;
656 table = acpi_os_map_memory(acpi_tables_inram + table_offset,
659 if (memcmp(existing_table->signature, table->signature, 4)) {
660 acpi_os_unmap_memory(table, table_len);
664 /* Only override tables with matching oem id */
665 if (memcmp(table->oem_table_id, existing_table->oem_table_id,
666 ACPI_OEM_TABLE_ID_SIZE)) {
667 acpi_os_unmap_memory(table, table_len);
671 acpi_os_unmap_memory(table, table_len);
672 *address = acpi_tables_inram + table_offset;
673 *table_length = table_len;
674 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
681 static irqreturn_t acpi_irq(int irq, void *dev_id)
685 handled = (*acpi_irq_handler) (acpi_irq_context);
691 acpi_irq_not_handled++;
697 acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
702 acpi_irq_stats_init();
705 * ACPI interrupts different from the SCI in our copy of the FADT are
708 if (gsi != acpi_gbl_FADT.sci_interrupt)
709 return AE_BAD_PARAMETER;
711 if (acpi_irq_handler)
712 return AE_ALREADY_ACQUIRED;
714 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
715 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
720 acpi_irq_handler = handler;
721 acpi_irq_context = context;
722 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
723 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
724 acpi_irq_handler = NULL;
725 return AE_NOT_ACQUIRED;
731 acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
733 if (irq != acpi_gbl_FADT.sci_interrupt)
734 return AE_BAD_PARAMETER;
736 free_irq(irq, acpi_irq);
737 acpi_irq_handler = NULL;
743 * Running in interpreter thread context, safe to sleep
746 void acpi_os_sleep(u64 ms)
748 schedule_timeout_interruptible(msecs_to_jiffies(ms));
751 void acpi_os_stall(u32 us)
759 touch_nmi_watchdog();
765 * Support ACPI 3.0 AML Timer operand
766 * Returns 64-bit free-running, monotonically increasing timer
767 * with 100ns granularity
769 u64 acpi_os_get_timer(void)
774 /* TBD: use HPET if available */
777 #ifdef CONFIG_X86_PM_TIMER
778 /* TBD: default to PM timer if HPET was not available */
781 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
786 acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
795 *(u8 *) value = inb(port);
796 } else if (width <= 16) {
797 *(u16 *) value = inw(port);
798 } else if (width <= 32) {
799 *(u32 *) value = inl(port);
807 EXPORT_SYMBOL(acpi_os_read_port);
809 acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
813 } else if (width <= 16) {
815 } else if (width <= 32) {
824 EXPORT_SYMBOL(acpi_os_write_port);
827 acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
829 void __iomem *virt_addr;
830 unsigned int size = width / 8;
835 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
838 virt_addr = acpi_os_ioremap(phys_addr, size);
840 return AE_BAD_ADDRESS;
849 *(u8 *) value = readb(virt_addr);
852 *(u16 *) value = readw(virt_addr);
855 *(u32 *) value = readl(virt_addr);
870 acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
872 void __iomem *virt_addr;
873 unsigned int size = width / 8;
877 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
880 virt_addr = acpi_os_ioremap(phys_addr, size);
882 return AE_BAD_ADDRESS;
888 writeb(value, virt_addr);
891 writew(value, virt_addr);
894 writel(value, virt_addr);
909 acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
910 u64 *value, u32 width)
916 return AE_BAD_PARAMETER;
932 result = raw_pci_read(pci_id->segment, pci_id->bus,
933 PCI_DEVFN(pci_id->device, pci_id->function),
934 reg, size, &value32);
937 return (result ? AE_ERROR : AE_OK);
941 acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
942 u64 value, u32 width)
960 result = raw_pci_write(pci_id->segment, pci_id->bus,
961 PCI_DEVFN(pci_id->device, pci_id->function),
964 return (result ? AE_ERROR : AE_OK);
967 static void acpi_os_execute_deferred(struct work_struct *work)
969 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
972 acpi_os_wait_events_complete(NULL);
974 dpc->function(dpc->context);
978 /*******************************************************************************
980 * FUNCTION: acpi_os_execute
982 * PARAMETERS: Type - Type of the callback
983 * Function - Function to be executed
984 * Context - Function parameters
988 * DESCRIPTION: Depending on type, either queues function for deferred execution or
989 * immediately executes function on a separate thread.
991 ******************************************************************************/
993 static acpi_status __acpi_os_execute(acpi_execute_type type,
994 acpi_osd_exec_callback function, void *context, int hp)
996 acpi_status status = AE_OK;
997 struct acpi_os_dpc *dpc;
998 struct workqueue_struct *queue;
1000 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
1001 "Scheduling function [%p(%p)] for deferred execution.\n",
1002 function, context));
1005 * Allocate/initialize DPC structure. Note that this memory will be
1006 * freed by the callee. The kernel handles the work_struct list in a
1007 * way that allows us to also free its memory inside the callee.
1008 * Because we may want to schedule several tasks with different
1009 * parameters we can't use the approach some kernel code uses of
1010 * having a static work_struct.
1013 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
1015 return AE_NO_MEMORY;
1017 dpc->function = function;
1018 dpc->context = context;
1021 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
1022 * because the hotplug code may call driver .remove() functions,
1023 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
1024 * to flush these workqueues.
1026 queue = hp ? kacpi_hotplug_wq :
1027 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
1028 dpc->wait = hp ? 1 : 0;
1030 if (queue == kacpi_hotplug_wq)
1031 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
1032 else if (queue == kacpi_notify_wq)
1033 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
1035 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
1038 * On some machines, a software-initiated SMI causes corruption unless
1039 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
1040 * typically it's done in GPE-related methods that are run via
1041 * workqueues, so we can avoid the known corruption cases by always
1042 * queueing on CPU 0.
1044 ret = queue_work_on(0, queue, &dpc->work);
1047 printk(KERN_ERR PREFIX
1048 "Call to queue_work() failed.\n");
1055 acpi_status acpi_os_execute(acpi_execute_type type,
1056 acpi_osd_exec_callback function, void *context)
1058 return __acpi_os_execute(type, function, context, 0);
1060 EXPORT_SYMBOL(acpi_os_execute);
1062 acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
1065 return __acpi_os_execute(0, function, context, 1);
1068 void acpi_os_wait_events_complete(void *context)
1070 flush_workqueue(kacpid_wq);
1071 flush_workqueue(kacpi_notify_wq);
1074 EXPORT_SYMBOL(acpi_os_wait_events_complete);
1077 acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
1079 struct semaphore *sem = NULL;
1081 sem = acpi_os_allocate(sizeof(struct semaphore));
1083 return AE_NO_MEMORY;
1084 memset(sem, 0, sizeof(struct semaphore));
1086 sema_init(sem, initial_units);
1088 *handle = (acpi_handle *) sem;
1090 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
1091 *handle, initial_units));
1097 * TODO: A better way to delete semaphores? Linux doesn't have a
1098 * 'delete_semaphore()' function -- may result in an invalid
1099 * pointer dereference for non-synchronized consumers. Should
1100 * we at least check for blocked threads and signal/cancel them?
1103 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
1105 struct semaphore *sem = (struct semaphore *)handle;
1108 return AE_BAD_PARAMETER;
1110 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
1112 BUG_ON(!list_empty(&sem->wait_list));
1120 * TODO: Support for units > 1?
1122 acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
1124 acpi_status status = AE_OK;
1125 struct semaphore *sem = (struct semaphore *)handle;
1129 if (!sem || (units < 1))
1130 return AE_BAD_PARAMETER;
1135 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
1136 handle, units, timeout));
1138 if (timeout == ACPI_WAIT_FOREVER)
1139 jiffies = MAX_SCHEDULE_TIMEOUT;
1141 jiffies = msecs_to_jiffies(timeout);
1143 ret = down_timeout(sem, jiffies);
1147 if (ACPI_FAILURE(status)) {
1148 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
1149 "Failed to acquire semaphore[%p|%d|%d], %s",
1150 handle, units, timeout,
1151 acpi_format_exception(status)));
1153 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
1154 "Acquired semaphore[%p|%d|%d]", handle,
1162 * TODO: Support for units > 1?
1164 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
1166 struct semaphore *sem = (struct semaphore *)handle;
1168 if (!sem || (units < 1))
1169 return AE_BAD_PARAMETER;
1174 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1182 #ifdef ACPI_FUTURE_USAGE
1183 u32 acpi_os_get_line(char *buffer)
1186 #ifdef ENABLE_DEBUGGER
1187 if (acpi_in_debugger) {
1190 kdb_read(buffer, sizeof(line_buf));
1192 /* remove the CR kdb includes */
1193 chars = strlen(buffer) - 1;
1194 buffer[chars] = '\0';
1200 #endif /* ACPI_FUTURE_USAGE */
1202 acpi_status acpi_os_signal(u32 function, void *info)
1205 case ACPI_SIGNAL_FATAL:
1206 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1208 case ACPI_SIGNAL_BREAKPOINT:
1211 * ACPI spec. says to treat it as a NOP unless
1212 * you are debugging. So if/when we integrate
1213 * AML debugger into the kernel debugger its
1214 * hook will go here. But until then it is
1215 * not useful to print anything on breakpoints.
1225 static int __init acpi_os_name_setup(char *str)
1227 char *p = acpi_os_name;
1228 int count = ACPI_MAX_OVERRIDE_LEN - 1;
1233 for (; count-- && str && *str; str++) {
1234 if (isalnum(*str) || *str == ' ' || *str == ':')
1236 else if (*str == '\'' || *str == '"')
1247 __setup("acpi_os_name=", acpi_os_name_setup);
1249 #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
1250 #define OSI_STRING_ENTRIES_MAX 16 /* arbitrary */
1252 struct osi_setup_entry {
1253 char string[OSI_STRING_LENGTH_MAX];
1257 static struct osi_setup_entry __initdata
1258 osi_setup_entries[OSI_STRING_ENTRIES_MAX] = {
1259 {"Module Device", true},
1260 {"Processor Device", true},
1261 {"3.0 _SCP Extensions", true},
1262 {"Processor Aggregator Device", true},
1265 void __init acpi_osi_setup(char *str)
1267 struct osi_setup_entry *osi;
1271 if (!acpi_gbl_create_osi_method)
1274 if (str == NULL || *str == '\0') {
1275 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1276 acpi_gbl_create_osi_method = FALSE;
1285 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1286 osi = &osi_setup_entries[i];
1287 if (!strcmp(osi->string, str)) {
1288 osi->enable = enable;
1290 } else if (osi->string[0] == '\0') {
1291 osi->enable = enable;
1292 strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
1298 static void __init set_osi_linux(unsigned int enable)
1300 if (osi_linux.enable != enable)
1301 osi_linux.enable = enable;
1303 if (osi_linux.enable)
1304 acpi_osi_setup("Linux");
1306 acpi_osi_setup("!Linux");
1311 static void __init acpi_cmdline_osi_linux(unsigned int enable)
1313 osi_linux.cmdline = 1; /* cmdline set the default and override DMI */
1315 set_osi_linux(enable);
1320 void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1322 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1327 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
1328 set_osi_linux(enable);
1334 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1336 * empty string disables _OSI
1337 * string starting with '!' disables that string
1338 * otherwise string is added to list, augmenting built-in strings
1340 static void __init acpi_osi_setup_late(void)
1342 struct osi_setup_entry *osi;
1347 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1348 osi = &osi_setup_entries[i];
1354 status = acpi_install_interface(str);
1356 if (ACPI_SUCCESS(status))
1357 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1359 status = acpi_remove_interface(str);
1361 if (ACPI_SUCCESS(status))
1362 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1367 static int __init osi_setup(char *str)
1369 if (str && !strcmp("Linux", str))
1370 acpi_cmdline_osi_linux(1);
1371 else if (str && !strcmp("!Linux", str))
1372 acpi_cmdline_osi_linux(0);
1374 acpi_osi_setup(str);
1379 __setup("acpi_osi=", osi_setup);
1381 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1382 static int __init acpi_serialize_setup(char *str)
1384 printk(KERN_INFO PREFIX "serialize enabled\n");
1386 acpi_gbl_all_methods_serialized = TRUE;
1391 __setup("acpi_serialize", acpi_serialize_setup);
1393 /* Check of resource interference between native drivers and ACPI
1394 * OperationRegions (SystemIO and System Memory only).
1395 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1396 * in arbitrary AML code and can interfere with legacy drivers.
1397 * acpi_enforce_resources= can be set to:
1399 * - strict (default) (2)
1400 * -> further driver trying to access the resources will not load
1402 * -> further driver trying to access the resources will load, but you
1403 * get a system message that something might go wrong...
1406 * -> ACPI Operation Region resources will not be registered
1409 #define ENFORCE_RESOURCES_STRICT 2
1410 #define ENFORCE_RESOURCES_LAX 1
1411 #define ENFORCE_RESOURCES_NO 0
1413 static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1415 static int __init acpi_enforce_resources_setup(char *str)
1417 if (str == NULL || *str == '\0')
1420 if (!strcmp("strict", str))
1421 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1422 else if (!strcmp("lax", str))
1423 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1424 else if (!strcmp("no", str))
1425 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1430 __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1432 /* Check for resource conflicts between ACPI OperationRegions and native
1434 int acpi_check_resource_conflict(const struct resource *res)
1436 struct acpi_res_list *res_list_elem;
1437 int ioport = 0, clash = 0;
1439 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1441 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1444 ioport = res->flags & IORESOURCE_IO;
1446 spin_lock(&acpi_res_lock);
1447 list_for_each_entry(res_list_elem, &resource_list_head,
1449 if (ioport && (res_list_elem->resource_type
1450 != ACPI_ADR_SPACE_SYSTEM_IO))
1452 if (!ioport && (res_list_elem->resource_type
1453 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1456 if (res->end < res_list_elem->start
1457 || res_list_elem->end < res->start)
1462 spin_unlock(&acpi_res_lock);
1465 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
1466 printk(KERN_WARNING "ACPI: resource %s %pR"
1467 " conflicts with ACPI region %s "
1468 "[%s 0x%zx-0x%zx]\n",
1469 res->name, res, res_list_elem->name,
1470 (res_list_elem->resource_type ==
1471 ACPI_ADR_SPACE_SYSTEM_IO) ? "io" : "mem",
1472 (size_t) res_list_elem->start,
1473 (size_t) res_list_elem->end);
1474 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1475 printk(KERN_NOTICE "ACPI: This conflict may"
1476 " cause random problems and system"
1478 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1479 " for this device, you should use it instead of"
1480 " the native driver\n");
1482 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1487 EXPORT_SYMBOL(acpi_check_resource_conflict);
1489 int acpi_check_region(resource_size_t start, resource_size_t n,
1492 struct resource res = {
1494 .end = start + n - 1,
1496 .flags = IORESOURCE_IO,
1499 return acpi_check_resource_conflict(&res);
1501 EXPORT_SYMBOL(acpi_check_region);
1504 * Let drivers know whether the resource checks are effective
1506 int acpi_resources_are_enforced(void)
1508 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1510 EXPORT_SYMBOL(acpi_resources_are_enforced);
1513 * Deallocate the memory for a spinlock.
1515 void acpi_os_delete_lock(acpi_spinlock handle)
1521 * Acquire a spinlock.
1523 * handle is a pointer to the spinlock_t.
1526 acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
1528 acpi_cpu_flags flags;
1529 spin_lock_irqsave(lockp, flags);
1534 * Release a spinlock. See above.
1537 void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
1539 spin_unlock_irqrestore(lockp, flags);
1542 #ifndef ACPI_USE_LOCAL_CACHE
1544 /*******************************************************************************
1546 * FUNCTION: acpi_os_create_cache
1548 * PARAMETERS: name - Ascii name for the cache
1549 * size - Size of each cached object
1550 * depth - Maximum depth of the cache (in objects) <ignored>
1551 * cache - Where the new cache object is returned
1555 * DESCRIPTION: Create a cache object
1557 ******************************************************************************/
1560 acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
1562 *cache = kmem_cache_create(name, size, 0, 0, NULL);
1569 /*******************************************************************************
1571 * FUNCTION: acpi_os_purge_cache
1573 * PARAMETERS: Cache - Handle to cache object
1577 * DESCRIPTION: Free all objects within the requested cache.
1579 ******************************************************************************/
1581 acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
1583 kmem_cache_shrink(cache);
1587 /*******************************************************************************
1589 * FUNCTION: acpi_os_delete_cache
1591 * PARAMETERS: Cache - Handle to cache object
1595 * DESCRIPTION: Free all objects within the requested cache and delete the
1598 ******************************************************************************/
1600 acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
1602 kmem_cache_destroy(cache);
1606 /*******************************************************************************
1608 * FUNCTION: acpi_os_release_object
1610 * PARAMETERS: Cache - Handle to cache object
1611 * Object - The object to be released
1615 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1616 * the object is deleted.
1618 ******************************************************************************/
1620 acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
1622 kmem_cache_free(cache, object);
1626 static inline int acpi_res_list_add(struct acpi_res_list *res)
1628 struct acpi_res_list *res_list_elem;
1630 list_for_each_entry(res_list_elem, &resource_list_head,
1633 if (res->resource_type == res_list_elem->resource_type &&
1634 res->start == res_list_elem->start &&
1635 res->end == res_list_elem->end) {
1638 * The Region(addr,len) already exist in the list,
1639 * just increase the count
1642 res_list_elem->count++;
1648 list_add(&res->resource_list, &resource_list_head);
1652 static inline void acpi_res_list_del(struct acpi_res_list *res)
1654 struct acpi_res_list *res_list_elem;
1656 list_for_each_entry(res_list_elem, &resource_list_head,
1659 if (res->resource_type == res_list_elem->resource_type &&
1660 res->start == res_list_elem->start &&
1661 res->end == res_list_elem->end) {
1664 * If the res count is decreased to 0,
1665 * remove and free it
1668 if (--res_list_elem->count == 0) {
1669 list_del(&res_list_elem->resource_list);
1670 kfree(res_list_elem);
1678 acpi_os_invalidate_address(
1680 acpi_physical_address address,
1683 struct acpi_res_list res;
1686 case ACPI_ADR_SPACE_SYSTEM_IO:
1687 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1688 /* Only interference checks against SystemIO and SystemMemory
1690 res.start = address;
1691 res.end = address + length - 1;
1692 res.resource_type = space_id;
1693 spin_lock(&acpi_res_lock);
1694 acpi_res_list_del(&res);
1695 spin_unlock(&acpi_res_lock);
1697 case ACPI_ADR_SPACE_PCI_CONFIG:
1698 case ACPI_ADR_SPACE_EC:
1699 case ACPI_ADR_SPACE_SMBUS:
1700 case ACPI_ADR_SPACE_CMOS:
1701 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1702 case ACPI_ADR_SPACE_DATA_TABLE:
1703 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1709 /******************************************************************************
1711 * FUNCTION: acpi_os_validate_address
1713 * PARAMETERS: space_id - ACPI space ID
1714 * address - Physical address
1715 * length - Address length
1717 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1718 * should return AE_AML_ILLEGAL_ADDRESS.
1720 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1721 * the addresses accessed by AML operation regions.
1723 *****************************************************************************/
1726 acpi_os_validate_address (
1728 acpi_physical_address address,
1732 struct acpi_res_list *res;
1734 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1738 case ACPI_ADR_SPACE_SYSTEM_IO:
1739 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1740 /* Only interference checks against SystemIO and SystemMemory
1742 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1745 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1746 strlcpy(res->name, name, 5);
1747 res->start = address;
1748 res->end = address + length - 1;
1749 res->resource_type = space_id;
1750 spin_lock(&acpi_res_lock);
1751 added = acpi_res_list_add(res);
1752 spin_unlock(&acpi_res_lock);
1753 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1754 "name: %s\n", added ? "Added" : "Already exist",
1755 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
1756 ? "SystemIO" : "System Memory",
1757 (unsigned long long)res->start,
1758 (unsigned long long)res->end,
1763 case ACPI_ADR_SPACE_PCI_CONFIG:
1764 case ACPI_ADR_SPACE_EC:
1765 case ACPI_ADR_SPACE_SMBUS:
1766 case ACPI_ADR_SPACE_CMOS:
1767 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1768 case ACPI_ADR_SPACE_DATA_TABLE:
1769 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1776 acpi_status __init acpi_os_initialize(void)
1778 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1779 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1780 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1781 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1786 acpi_status __init acpi_os_initialize1(void)
1788 kacpid_wq = alloc_workqueue("kacpid", 0, 1);
1789 kacpi_notify_wq = alloc_workqueue("kacpi_notify", 0, 1);
1790 kacpi_hotplug_wq = alloc_workqueue("kacpi_hotplug", 0, 1);
1792 BUG_ON(!kacpi_notify_wq);
1793 BUG_ON(!kacpi_hotplug_wq);
1794 acpi_install_interface_handler(acpi_osi_handler);
1795 acpi_osi_setup_late();
1799 acpi_status acpi_os_terminate(void)
1801 if (acpi_irq_handler) {
1802 acpi_os_remove_interrupt_handler(acpi_gbl_FADT.sci_interrupt,
1806 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1807 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1808 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1809 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1811 destroy_workqueue(kacpid_wq);
1812 destroy_workqueue(kacpi_notify_wq);
1813 destroy_workqueue(kacpi_hotplug_wq);