blob: 885e222bcabd228d1e9b7aac359551d1cdfb55f0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
3 *
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>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04007 * Copyright (c) 2008 Intel Corporation
8 * Author: Matthew Wilcox <willy@linux.intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
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.
16 *
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.
21 *
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
25 *
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 *
28 */
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/slab.h>
33#include <linux/mm.h>
34#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/interrupt.h>
36#include <linux/kmod.h>
37#include <linux/delay.h>
38#include <linux/workqueue.h>
39#include <linux/nmi.h>
Alexey Starikovskiyad718602007-02-02 19:48:19 +030040#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/efi.h>
Thomas Renningerdf92e692008-02-04 23:31:22 -080042#include <linux/ioport.h>
43#include <linux/list.h>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -040044#include <linux/jiffies.h>
45#include <linux/semaphore.h>
46
47#include <asm/io.h>
48#include <asm/uaccess.h>
49
50#include <acpi/acpi.h>
51#include <acpi/acpi_bus.h>
52#include <acpi/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define _COMPONENT ACPI_OS_SERVICES
Len Brownf52fd662007-02-12 22:42:12 -050055ACPI_MODULE_NAME("osl");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define PREFIX "ACPI: "
Len Brown4be44fc2005-08-05 00:44:28 -040057struct acpi_os_dpc {
58 acpi_osd_exec_callback function;
59 void *context;
David Howells65f27f32006-11-22 14:55:48 +000060 struct work_struct work;
Bjorn Helgaas9ac61852009-08-31 22:32:10 +000061 int wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
64#ifdef CONFIG_ACPI_CUSTOM_DSDT
65#include CONFIG_ACPI_CUSTOM_DSDT_FILE
66#endif
67
68#ifdef ENABLE_DEBUGGER
69#include <linux/kdb.h>
70
71/* stuff for debugger support */
72int acpi_in_debugger;
73EXPORT_SYMBOL(acpi_in_debugger);
74
75extern char line_buf[80];
Len Brown4be44fc2005-08-05 00:44:28 -040076#endif /*ENABLE_DEBUGGER */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static unsigned int acpi_irq_irq;
79static acpi_osd_handler acpi_irq_handler;
80static void *acpi_irq_context;
81static struct workqueue_struct *kacpid_wq;
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -040082static struct workqueue_struct *kacpi_notify_wq;
Zhang Ruic02256b2009-06-23 10:20:29 +080083static struct workqueue_struct *kacpi_hotplug_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Thomas Renningerdf92e692008-02-04 23:31:22 -080085struct acpi_res_list {
86 resource_size_t start;
87 resource_size_t end;
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;
Lin Minga5fe1a02009-08-13 10:43:27 +080092 int count;
Thomas Renningerdf92e692008-02-04 23:31:22 -080093};
94
95static LIST_HEAD(resource_list_head);
96static DEFINE_SPINLOCK(acpi_res_lock);
97
Myron Stowe620242a2010-10-21 14:23:53 -060098/*
99 * This list of permanent mappings is for memory that may be accessed from
100 * interrupt context, where we can't do the ioremap().
101 */
102struct acpi_ioremap {
103 struct list_head list;
104 void __iomem *virt;
105 acpi_physical_address phys;
106 acpi_size size;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600107 struct kref ref;
Myron Stowe620242a2010-10-21 14:23:53 -0600108};
109
110static LIST_HEAD(acpi_ioremaps);
111static DEFINE_SPINLOCK(acpi_ioremap_lock);
112
Len Brownae00d812007-05-29 18:43:33 -0400113#define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
114static char osi_additional_string[OSI_STRING_LENGTH_MAX];
115
Len Brownd4b7dc42008-01-23 20:50:56 -0500116/*
Len Browna6e08872008-11-08 01:21:10 -0500117 * The story of _OSI(Linux)
Len Brownd4b7dc42008-01-23 20:50:56 -0500118 *
Len Browna6e08872008-11-08 01:21:10 -0500119 * From pre-history through Linux-2.6.22,
120 * Linux responded TRUE upon a BIOS OSI(Linux) query.
Len Brownd4b7dc42008-01-23 20:50:56 -0500121 *
Len Browna6e08872008-11-08 01:21:10 -0500122 * Unfortunately, reference BIOS writers got wind of this
123 * and put OSI(Linux) in their example code, quickly exposing
124 * this string as ill-conceived and opening the door to
125 * an un-bounded number of BIOS incompatibilities.
Len Brownd4b7dc42008-01-23 20:50:56 -0500126 *
Len Browna6e08872008-11-08 01:21:10 -0500127 * For example, OSI(Linux) was used on resume to re-POST a
128 * video card on one system, because Linux at that time
129 * could not do a speedy restore in its native driver.
130 * But then upon gaining quick native restore capability,
131 * Linux has no way to tell the BIOS to skip the time-consuming
132 * POST -- putting Linux at a permanent performance disadvantage.
133 * On another system, the BIOS writer used OSI(Linux)
134 * to infer native OS support for IPMI! On other systems,
135 * OSI(Linux) simply got in the way of Linux claiming to
136 * be compatible with other operating systems, exposing
137 * BIOS issues such as skipped device initialization.
Len Brownd4b7dc42008-01-23 20:50:56 -0500138 *
Len Browna6e08872008-11-08 01:21:10 -0500139 * So "Linux" turned out to be a really poor chose of
140 * OSI string, and from Linux-2.6.23 onward we respond FALSE.
Len Brownd4b7dc42008-01-23 20:50:56 -0500141 *
142 * BIOS writers should NOT query _OSI(Linux) on future systems.
Len Browna6e08872008-11-08 01:21:10 -0500143 * Linux will complain on the console when it sees it, and return FALSE.
144 * To get Linux to return TRUE for your system will require
145 * a kernel source update to add a DMI entry,
146 * or boot with "acpi_osi=Linux"
Len Brownd4b7dc42008-01-23 20:50:56 -0500147 */
Len Brownd4b7dc42008-01-23 20:50:56 -0500148
Adrian Bunk1d15d842008-01-29 00:10:15 +0200149static struct osi_linux {
Len Brownd4b7dc42008-01-23 20:50:56 -0500150 unsigned int enable:1;
151 unsigned int dmi:1;
152 unsigned int cmdline:1;
153 unsigned int known:1;
Len Browna6e08872008-11-08 01:21:10 -0500154} osi_linux = { 0, 0, 0, 0};
Len Brownf5076542007-05-30 00:10:38 -0400155
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700156static void __init acpi_request_region (struct acpi_generic_address *addr,
157 unsigned int length, char *desc)
158{
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700159 if (!addr->address || !length)
160 return;
161
Andi Kleencfa806f2010-07-20 15:18:36 -0700162 /* Resources are never freed */
Len Browneee3c852007-02-03 01:38:16 -0500163 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Andi Kleencfa806f2010-07-20 15:18:36 -0700164 request_region(addr->address, length, desc);
Len Browneee3c852007-02-03 01:38:16 -0500165 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
Andi Kleencfa806f2010-07-20 15:18:36 -0700166 request_mem_region(addr->address, length, desc);
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700167}
168
169static int __init acpi_reserve_resources(void)
170{
Len Browneee3c852007-02-03 01:38:16 -0500171 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700172 "ACPI PM1a_EVT_BLK");
173
Len Browneee3c852007-02-03 01:38:16 -0500174 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700175 "ACPI PM1b_EVT_BLK");
176
Len Browneee3c852007-02-03 01:38:16 -0500177 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700178 "ACPI PM1a_CNT_BLK");
179
Len Browneee3c852007-02-03 01:38:16 -0500180 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700181 "ACPI PM1b_CNT_BLK");
182
Len Browneee3c852007-02-03 01:38:16 -0500183 if (acpi_gbl_FADT.pm_timer_length == 4)
184 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700185
Len Browneee3c852007-02-03 01:38:16 -0500186 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700187 "ACPI PM2_CNT_BLK");
188
189 /* Length of GPE blocks must be a non-negative multiple of 2 */
190
Len Browneee3c852007-02-03 01:38:16 -0500191 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
192 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
193 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700194
Len Browneee3c852007-02-03 01:38:16 -0500195 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
196 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
197 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700198
199 return 0;
200}
201device_initcall(acpi_reserve_resources);
202
Len Brown4be44fc2005-08-05 00:44:28 -0400203void acpi_os_printf(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 va_list args;
206 va_start(args, fmt);
207 acpi_os_vprintf(fmt, args);
208 va_end(args);
209}
Len Brown4be44fc2005-08-05 00:44:28 -0400210
Len Brown4be44fc2005-08-05 00:44:28 -0400211void acpi_os_vprintf(const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 static char buffer[512];
Len Brown4be44fc2005-08-05 00:44:28 -0400214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 vsprintf(buffer, fmt, args);
216
217#ifdef ENABLE_DEBUGGER
218 if (acpi_in_debugger) {
219 kdb_printf("%s", buffer);
220 } else {
Frank Seidel4d939152009-02-04 17:03:07 +0100221 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223#else
Frank Seidel4d939152009-02-04 17:03:07 +0100224 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#endif
226}
227
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300228acpi_physical_address __init acpi_os_get_root_pointer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 if (efi_enabled) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800231 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300232 return efi.acpi20;
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800233 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300234 return efi.acpi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400236 printk(KERN_ERR PREFIX
237 "System description tables not found\n");
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Len Brown239665a2007-11-23 20:08:02 -0500240 } else {
241 acpi_physical_address pa = 0;
242
243 acpi_find_root_pointer(&pa);
244 return pa;
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600248/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe4a3cba52010-10-21 14:24:14 -0600249static struct acpi_ioremap *
250acpi_map_lookup(acpi_physical_address phys, acpi_size size)
Myron Stowe620242a2010-10-21 14:23:53 -0600251{
252 struct acpi_ioremap *map;
253
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600254 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe620242a2010-10-21 14:23:53 -0600255 if (map->phys <= phys &&
256 phys + size <= map->phys + map->size)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600257 return map;
258
259 return NULL;
260}
261
262/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
263static void __iomem *
264acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
265{
266 struct acpi_ioremap *map;
267
268 map = acpi_map_lookup(phys, size);
269 if (map)
270 return map->virt + (phys - map->phys);
Myron Stowe620242a2010-10-21 14:23:53 -0600271
272 return NULL;
273}
274
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600275/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe620242a2010-10-21 14:23:53 -0600276static struct acpi_ioremap *
277acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
278{
279 struct acpi_ioremap *map;
280
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600281 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600282 if (map->virt <= virt &&
283 virt + size <= map->virt + map->size)
Myron Stowe620242a2010-10-21 14:23:53 -0600284 return map;
285
286 return NULL;
287}
288
Jan Beulich2fdf0742007-12-13 08:33:59 +0000289void __iomem *__init_refok
290acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Myron Stowe4a3cba52010-10-21 14:24:14 -0600292 struct acpi_ioremap *map, *tmp_map;
293 unsigned long flags, pg_sz;
Myron Stowe620242a2010-10-21 14:23:53 -0600294 void __iomem *virt;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600295 phys_addr_t pg_off;
Myron Stowe620242a2010-10-21 14:23:53 -0600296
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800297 if (phys > ULONG_MAX) {
298 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
Randy Dunlap70c08462007-02-13 16:11:36 -0800299 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
Myron Stowe620242a2010-10-21 14:23:53 -0600301
302 if (!acpi_gbl_permanent_mmap)
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300303 return __acpi_map_table((unsigned long)phys, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600304
305 map = kzalloc(sizeof(*map), GFP_KERNEL);
306 if (!map)
307 return NULL;
308
Myron Stowe4a3cba52010-10-21 14:24:14 -0600309 pg_off = round_down(phys, PAGE_SIZE);
310 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
311 virt = ioremap(pg_off, pg_sz);
Myron Stowe620242a2010-10-21 14:23:53 -0600312 if (!virt) {
313 kfree(map);
314 return NULL;
315 }
316
317 INIT_LIST_HEAD(&map->list);
318 map->virt = virt;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600319 map->phys = pg_off;
320 map->size = pg_sz;
321 kref_init(&map->ref);
Myron Stowe620242a2010-10-21 14:23:53 -0600322
323 spin_lock_irqsave(&acpi_ioremap_lock, flags);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600324 /* Check if page has already been mapped. */
325 tmp_map = acpi_map_lookup(phys, size);
326 if (tmp_map) {
327 kref_get(&tmp_map->ref);
328 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
329 iounmap(map->virt);
330 kfree(map);
331 return tmp_map->virt + (phys - tmp_map->phys);
332 }
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600333 list_add_tail_rcu(&map->list, &acpi_ioremaps);
Myron Stowe620242a2010-10-21 14:23:53 -0600334 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
335
Myron Stowe4a3cba52010-10-21 14:24:14 -0600336 return map->virt + (phys - map->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800338EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Myron Stowe4a3cba52010-10-21 14:24:14 -0600340static void acpi_kref_del_iomap(struct kref *ref)
341{
342 struct acpi_ioremap *map;
343
344 map = container_of(ref, struct acpi_ioremap, ref);
345 list_del_rcu(&map->list);
346}
347
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800348void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Myron Stowe620242a2010-10-21 14:23:53 -0600350 struct acpi_ioremap *map;
351 unsigned long flags;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600352 int del;
Myron Stowe620242a2010-10-21 14:23:53 -0600353
354 if (!acpi_gbl_permanent_mmap) {
Yinghai Lu7d972772009-02-07 15:39:41 -0800355 __acpi_unmap_table(virt, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600356 return;
357 }
358
359 spin_lock_irqsave(&acpi_ioremap_lock, flags);
360 map = acpi_map_lookup_virt(virt, size);
361 if (!map) {
362 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
363 printk(KERN_ERR PREFIX "%s: bad address %p\n", __func__, virt);
364 dump_stack();
365 return;
366 }
367
Myron Stowe4a3cba52010-10-21 14:24:14 -0600368 del = kref_put(&map->ref, acpi_kref_del_iomap);
Myron Stowe620242a2010-10-21 14:23:53 -0600369 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
370
Myron Stowe4a3cba52010-10-21 14:24:14 -0600371 if (!del)
372 return;
373
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600374 synchronize_rcu();
Myron Stowe620242a2010-10-21 14:23:53 -0600375 iounmap(map->virt);
376 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800378EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800380void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Yinghai Lu7d972772009-02-07 15:39:41 -0800381{
382 if (!acpi_gbl_permanent_mmap)
383 __acpi_unmap_table(virt, size);
384}
385
Myron Stowe29718522010-10-21 14:23:59 -0600386int acpi_os_map_generic_address(struct acpi_generic_address *addr)
387{
388 void __iomem *virt;
389
390 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
391 return 0;
392
393 if (!addr->address || !addr->bit_width)
394 return -EINVAL;
395
396 virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
397 if (!virt)
398 return -EIO;
399
400 return 0;
401}
402EXPORT_SYMBOL_GPL(acpi_os_map_generic_address);
403
404void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
405{
406 void __iomem *virt;
407 unsigned long flags;
408 acpi_size size = addr->bit_width / 8;
409
410 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
411 return;
412
413 if (!addr->address || !addr->bit_width)
414 return;
415
416 spin_lock_irqsave(&acpi_ioremap_lock, flags);
417 virt = acpi_map_vaddr_lookup(addr->address, size);
418 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
419
420 acpi_os_unmap_memory(virt, size);
421}
422EXPORT_SYMBOL_GPL(acpi_os_unmap_generic_address);
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424#ifdef ACPI_FUTURE_USAGE
425acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400426acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Len Brown4be44fc2005-08-05 00:44:28 -0400428 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return AE_BAD_PARAMETER;
430
431 *phys = virt_to_phys(virt);
432
433 return AE_OK;
434}
435#endif
436
437#define ACPI_MAX_OVERRIDE_LEN 100
438
439static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
440
441acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400442acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
443 acpi_string * new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 if (!init_val || !new_val)
446 return AE_BAD_PARAMETER;
447
448 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400449 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400451 acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 *new_val = acpi_os_name;
453 }
454
455 return AE_OK;
456}
457
458acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400459acpi_os_table_override(struct acpi_table_header * existing_table,
460 struct acpi_table_header ** new_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 if (!existing_table || !new_table)
463 return AE_BAD_PARAMETER;
464
Markus Gaugusch71fc47a2008-02-05 00:04:06 +0100465 *new_table = NULL;
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467#ifdef CONFIG_ACPI_CUSTOM_DSDT
468 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400469 *new_table = (struct acpi_table_header *)AmlCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470#endif
Éric Piel6ed31e92008-02-05 00:04:50 +0100471 if (*new_table != NULL) {
472 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
473 "this is unsafe: tainting kernel\n",
474 existing_table->signature,
475 existing_table->oem_table_id);
476 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return AE_OK;
479}
480
David Howells7d12e782006-10-05 14:55:46 +0100481static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Len Brown5229e872008-02-06 01:26:55 -0500483 u32 handled;
484
485 handled = (*acpi_irq_handler) (acpi_irq_context);
486
487 if (handled) {
488 acpi_irq_handled++;
489 return IRQ_HANDLED;
Len Brown88bea182009-04-21 00:35:47 -0400490 } else {
491 acpi_irq_not_handled++;
Len Brown5229e872008-02-06 01:26:55 -0500492 return IRQ_NONE;
Len Brown88bea182009-04-21 00:35:47 -0400493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400497acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
498 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 unsigned int irq;
501
Len Brown5229e872008-02-06 01:26:55 -0500502 acpi_irq_stats_init();
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /*
505 * Ignore the GSI from the core, and use the value in our copy of the
506 * FADT. It may not be the same if an interrupt source override exists
507 * for the SCI.
508 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300509 gsi = acpi_gbl_FADT.sci_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
511 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
512 gsi);
513 return AE_OK;
514 }
515
516 acpi_irq_handler = handler;
517 acpi_irq_context = context;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700518 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
520 return AE_NOT_ACQUIRED;
521 }
522 acpi_irq_irq = irq;
523
524 return AE_OK;
525}
526
Len Brown4be44fc2005-08-05 00:44:28 -0400527acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 if (irq) {
530 free_irq(irq, acpi_irq);
531 acpi_irq_handler = NULL;
532 acpi_irq_irq = 0;
533 }
534
535 return AE_OK;
536}
537
538/*
539 * Running in interpreter thread context, safe to sleep
540 */
541
Lin Ming439913f2010-01-28 10:53:19 +0800542void acpi_os_sleep(u64 ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800544 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
Len Brown4be44fc2005-08-05 00:44:28 -0400546
Len Brown4be44fc2005-08-05 00:44:28 -0400547void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 while (us) {
550 u32 delay = 1000;
551
552 if (delay > us)
553 delay = us;
554 udelay(delay);
555 touch_nmi_watchdog();
556 us -= delay;
557 }
558}
Len Brown4be44fc2005-08-05 00:44:28 -0400559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560/*
561 * Support ACPI 3.0 AML Timer operand
562 * Returns 64-bit free-running, monotonically increasing timer
563 * with 100ns granularity
564 */
Len Brown4be44fc2005-08-05 00:44:28 -0400565u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 static u64 t;
568
569#ifdef CONFIG_HPET
570 /* TBD: use HPET if available */
571#endif
572
573#ifdef CONFIG_X86_PM_TIMER
574 /* TBD: default to PM timer if HPET was not available */
575#endif
576 if (!t)
577 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
578
579 return ++t;
580}
581
Len Brown4be44fc2005-08-05 00:44:28 -0400582acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 u32 dummy;
585
586 if (!value)
587 value = &dummy;
588
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800589 *value = 0;
590 if (width <= 8) {
Len Brown4be44fc2005-08-05 00:44:28 -0400591 *(u8 *) value = inb(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800592 } else if (width <= 16) {
Len Brown4be44fc2005-08-05 00:44:28 -0400593 *(u16 *) value = inw(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800594 } else if (width <= 32) {
Len Brown4be44fc2005-08-05 00:44:28 -0400595 *(u32 *) value = inl(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800596 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 BUG();
598 }
599
600 return AE_OK;
601}
Len Brown4be44fc2005-08-05 00:44:28 -0400602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603EXPORT_SYMBOL(acpi_os_read_port);
604
Len Brown4be44fc2005-08-05 00:44:28 -0400605acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800607 if (width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 outb(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800609 } else if (width <= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 outw(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800611 } else if (width <= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 outl(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800613 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 BUG();
615 }
616
617 return AE_OK;
618}
Len Brown4be44fc2005-08-05 00:44:28 -0400619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620EXPORT_SYMBOL(acpi_os_write_port);
621
622acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400623acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Len Brown4be44fc2005-08-05 00:44:28 -0400625 u32 dummy;
626 void __iomem *virt_addr;
Myron Stowe620242a2010-10-21 14:23:53 -0600627 int size = width / 8, unmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600629 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600630 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600631 rcu_read_unlock();
Myron Stowe620242a2010-10-21 14:23:53 -0600632 if (!virt_addr) {
633 virt_addr = ioremap(phys_addr, size);
634 unmap = 1;
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (!value)
637 value = &dummy;
638
639 switch (width) {
640 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400641 *(u8 *) value = readb(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 break;
643 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400644 *(u16 *) value = readw(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
646 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400647 *(u32 *) value = readl(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 break;
649 default:
650 BUG();
651 }
652
Myron Stowe620242a2010-10-21 14:23:53 -0600653 if (unmap)
654 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 return AE_OK;
657}
658
659acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400660acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Len Brown4be44fc2005-08-05 00:44:28 -0400662 void __iomem *virt_addr;
Myron Stowe620242a2010-10-21 14:23:53 -0600663 int size = width / 8, unmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600665 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600666 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600667 rcu_read_unlock();
Myron Stowe620242a2010-10-21 14:23:53 -0600668 if (!virt_addr) {
669 virt_addr = ioremap(phys_addr, size);
670 unmap = 1;
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 switch (width) {
674 case 8:
675 writeb(value, virt_addr);
676 break;
677 case 16:
678 writew(value, virt_addr);
679 break;
680 case 32:
681 writel(value, virt_addr);
682 break;
683 default:
684 BUG();
685 }
686
Myron Stowe620242a2010-10-21 14:23:53 -0600687 if (unmap)
688 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 return AE_OK;
691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400694acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Linus Torvalds10270d42008-02-13 09:56:14 -0800695 u32 *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 int result, size;
698
699 if (!value)
700 return AE_BAD_PARAMETER;
701
702 switch (width) {
703 case 8:
704 size = 1;
705 break;
706 case 16:
707 size = 2;
708 break;
709 case 32:
710 size = 4;
711 break;
712 default:
713 return AE_ERROR;
714 }
715
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500716 result = raw_pci_read(pci_id->segment, pci_id->bus,
717 PCI_DEVFN(pci_id->device, pci_id->function),
718 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 return (result ? AE_ERROR : AE_OK);
721}
Len Brown4be44fc2005-08-05 00:44:28 -0400722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400724acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Lin Ming439913f2010-01-28 10:53:19 +0800725 u64 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 int result, size;
728
729 switch (width) {
730 case 8:
731 size = 1;
732 break;
733 case 16:
734 size = 2;
735 break;
736 case 32:
737 size = 4;
738 break;
739 default:
740 return AE_ERROR;
741 }
742
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500743 result = raw_pci_write(pci_id->segment, pci_id->bus,
744 PCI_DEVFN(pci_id->device, pci_id->function),
745 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 return (result ? AE_ERROR : AE_OK);
748}
749
750/* TODO: Change code to take advantage of driver model more */
Len Brown4be44fc2005-08-05 00:44:28 -0400751static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
752 acpi_handle chandle, /* current node */
753 struct acpi_pci_id **id,
754 int *is_bridge, u8 * bus_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Len Brown4be44fc2005-08-05 00:44:28 -0400756 acpi_handle handle;
757 struct acpi_pci_id *pci_id = *id;
758 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400759 unsigned long long temp;
Len Brown4be44fc2005-08-05 00:44:28 -0400760 acpi_object_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 acpi_get_parent(chandle, &handle);
763 if (handle != rhandle) {
Len Brown4be44fc2005-08-05 00:44:28 -0400764 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
765 bus_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 status = acpi_get_type(handle, &type);
Len Brown4be44fc2005-08-05 00:44:28 -0400768 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return;
770
Matthew Wilcox27663c52008-10-10 02:22:59 -0400771 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400772 &temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (ACPI_SUCCESS(status)) {
Linus Torvalds10270d42008-02-13 09:56:14 -0800774 u32 val;
Len Brown4be44fc2005-08-05 00:44:28 -0400775 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
776 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 if (*is_bridge)
779 pci_id->bus = *bus_number;
780
781 /* any nicer way to get bus number of bridge ? */
Len Brown4be44fc2005-08-05 00:44:28 -0400782 status =
Linus Torvalds10270d42008-02-13 09:56:14 -0800783 acpi_os_read_pci_configuration(pci_id, 0x0e, &val,
Len Brown4be44fc2005-08-05 00:44:28 -0400784 8);
785 if (ACPI_SUCCESS(status)
Linus Torvalds10270d42008-02-13 09:56:14 -0800786 && ((val & 0x7f) == 1 || (val & 0x7f) == 2)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400787 status =
788 acpi_os_read_pci_configuration(pci_id, 0x18,
Linus Torvalds10270d42008-02-13 09:56:14 -0800789 &val, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (!ACPI_SUCCESS(status)) {
791 /* Certainly broken... FIX ME */
792 return;
793 }
794 *is_bridge = 1;
Linus Torvalds10270d42008-02-13 09:56:14 -0800795 pci_id->bus = val;
Len Brown4be44fc2005-08-05 00:44:28 -0400796 status =
797 acpi_os_read_pci_configuration(pci_id, 0x19,
Linus Torvalds10270d42008-02-13 09:56:14 -0800798 &val, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (ACPI_SUCCESS(status)) {
Linus Torvalds10270d42008-02-13 09:56:14 -0800800 *bus_number = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 } else
803 *is_bridge = 0;
804 }
805 }
806}
807
Len Brown4be44fc2005-08-05 00:44:28 -0400808void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
809 acpi_handle chandle, /* current node */
810 struct acpi_pci_id **id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 int is_bridge = 1;
813 u8 bus_number = (*id)->bus;
814
815 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
816}
817
David Howells65f27f32006-11-22 14:55:48 +0000818static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
David Howells65f27f32006-11-22 14:55:48 +0000820 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400821
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000822 if (dpc->wait)
823 acpi_os_wait_events_complete(NULL);
Zhang Rui19cd8472008-08-28 10:05:06 +0800824
825 dpc->function(dpc->context);
826 kfree(dpc);
Zhang Rui19cd8472008-08-28 10:05:06 +0800827}
828
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400829/*******************************************************************************
830 *
831 * FUNCTION: acpi_os_execute
832 *
833 * PARAMETERS: Type - Type of the callback
834 * Function - Function to be executed
835 * Context - Function parameters
836 *
837 * RETURN: Status
838 *
839 * DESCRIPTION: Depending on type, either queues function for deferred execution or
840 * immediately executes function on a separate thread.
841 *
842 ******************************************************************************/
843
Zhang Rui19cd8472008-08-28 10:05:06 +0800844static acpi_status __acpi_os_execute(acpi_execute_type type,
845 acpi_osd_exec_callback function, void *context, int hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Len Brown4be44fc2005-08-05 00:44:28 -0400847 acpi_status status = AE_OK;
848 struct acpi_os_dpc *dpc;
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300849 struct workqueue_struct *queue;
Zhang Rui19cd8472008-08-28 10:05:06 +0800850 int ret;
Len Brown72945b22006-07-12 22:46:42 -0400851 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
852 "Scheduling function [%p(%p)] for deferred execution.\n",
853 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /*
856 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +0000857 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * way that allows us to also free its memory inside the callee.
859 * Because we may want to schedule several tasks with different
860 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +0000861 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 */
Len Brown72945b22006-07-12 22:46:42 -0400863
David Howells65f27f32006-11-22 14:55:48 +0000864 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (!dpc)
Lin Ming889c78b2008-12-31 09:23:57 +0800866 return AE_NO_MEMORY;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 dpc->function = function;
869 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800870
Zhang Ruic02256b2009-06-23 10:20:29 +0800871 /*
872 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
873 * because the hotplug code may call driver .remove() functions,
874 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
875 * to flush these workqueues.
876 */
877 queue = hp ? kacpi_hotplug_wq :
878 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000879 dpc->wait = hp ? 1 : 0;
Zhang Ruibc736752010-03-22 15:48:54 +0800880
881 if (queue == kacpi_hotplug_wq)
882 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
883 else if (queue == kacpi_notify_wq)
884 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
885 else
886 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
887
Tejun Heo8fec62b2010-06-29 10:07:09 +0200888 /*
889 * On some machines, a software-initiated SMI causes corruption unless
890 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
891 * typically it's done in GPE-related methods that are run via
892 * workqueues, so we can avoid the known corruption cases by always
893 * queueing on CPU 0.
894 */
895 ret = queue_work_on(0, queue, &dpc->work);
Zhang Rui19cd8472008-08-28 10:05:06 +0800896
897 if (!ret) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800898 printk(KERN_ERR PREFIX
899 "Call to queue_work() failed.\n");
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300900 status = AE_ERROR;
901 kfree(dpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
Lin Ming889c78b2008-12-31 09:23:57 +0800903 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
Len Brown4be44fc2005-08-05 00:44:28 -0400905
Zhang Rui19cd8472008-08-28 10:05:06 +0800906acpi_status acpi_os_execute(acpi_execute_type type,
907 acpi_osd_exec_callback function, void *context)
908{
909 return __acpi_os_execute(type, function, context, 0);
910}
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400911EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Zhang Rui19cd8472008-08-28 10:05:06 +0800913acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
914 void *context)
915{
916 return __acpi_os_execute(0, function, context, 1);
917}
918
Len Brown4be44fc2005-08-05 00:44:28 -0400919void acpi_os_wait_events_complete(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 flush_workqueue(kacpid_wq);
Zhang Rui2f67a062008-04-29 02:34:42 -0400922 flush_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
Len Brown4be44fc2005-08-05 00:44:28 -0400924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925EXPORT_SYMBOL(acpi_os_wait_events_complete);
926
927/*
928 * Allocate the memory for a spinlock and initialize it.
929 */
Bob Moore967440e2006-06-23 17:04:00 -0400930acpi_status acpi_os_create_lock(acpi_spinlock * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Bob Moore967440e2006-06-23 17:04:00 -0400932 spin_lock_init(*handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Patrick Mocheld550d982006-06-27 00:41:40 -0400934 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/*
938 * Deallocate the memory for a spinlock.
939 */
Bob Moore967440e2006-06-23 17:04:00 -0400940void acpi_os_delete_lock(acpi_spinlock handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Patrick Mocheld550d982006-06-27 00:41:40 -0400942 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400946acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Len Brown4be44fc2005-08-05 00:44:28 -0400948 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 sem = acpi_os_allocate(sizeof(struct semaphore));
951 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400952 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 memset(sem, 0, sizeof(struct semaphore));
954
955 sema_init(sem, initial_units);
956
Len Brown4be44fc2005-08-05 00:44:28 -0400957 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Len Brown4be44fc2005-08-05 00:44:28 -0400959 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
960 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Patrick Mocheld550d982006-06-27 00:41:40 -0400962 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965/*
966 * TODO: A better way to delete semaphores? Linux doesn't have a
967 * 'delete_semaphore()' function -- may result in an invalid
968 * pointer dereference for non-synchronized consumers. Should
969 * we at least check for blocked threads and signal/cancel them?
970 */
971
Len Brown4be44fc2005-08-05 00:44:28 -0400972acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Len Brown4be44fc2005-08-05 00:44:28 -0400974 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400977 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Len Brown4be44fc2005-08-05 00:44:28 -0400979 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400981 BUG_ON(!list_empty(&sem->wait_list));
Len Brown02438d82006-06-30 03:19:10 -0400982 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -0400983 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Patrick Mocheld550d982006-06-27 00:41:40 -0400985 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 * TODO: Support for units > 1?
990 */
Len Brown4be44fc2005-08-05 00:44:28 -0400991acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Len Brown4be44fc2005-08-05 00:44:28 -0400993 acpi_status status = AE_OK;
994 struct semaphore *sem = (struct semaphore *)handle;
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400995 long jiffies;
Len Brown4be44fc2005-08-05 00:44:28 -0400996 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400999 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -04001002 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Len Brown4be44fc2005-08-05 00:44:28 -04001004 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
1005 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001007 if (timeout == ACPI_WAIT_FOREVER)
1008 jiffies = MAX_SCHEDULE_TIMEOUT;
1009 else
1010 jiffies = msecs_to_jiffies(timeout);
1011
1012 ret = down_timeout(sem, jiffies);
1013 if (ret)
1014 status = AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -04001017 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -04001018 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -04001019 handle, units, timeout,
1020 acpi_format_exception(status)));
1021 } else {
1022 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -04001023 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001024 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026
Patrick Mocheld550d982006-06-27 00:41:40 -04001027 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030/*
1031 * TODO: Support for units > 1?
1032 */
Len Brown4be44fc2005-08-05 00:44:28 -04001033acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Len Brown4be44fc2005-08-05 00:44:28 -04001035 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001038 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -04001041 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Len Brown4be44fc2005-08-05 00:44:28 -04001043 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1044 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 up(sem);
1047
Patrick Mocheld550d982006-06-27 00:41:40 -04001048 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
Len Brown4be44fc2005-08-05 00:44:28 -04001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -04001052u32 acpi_os_get_line(char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054
1055#ifdef ENABLE_DEBUGGER
1056 if (acpi_in_debugger) {
1057 u32 chars;
1058
1059 kdb_read(buffer, sizeof(line_buf));
1060
1061 /* remove the CR kdb includes */
1062 chars = strlen(buffer) - 1;
1063 buffer[chars] = '\0';
1064 }
1065#endif
1066
1067 return 0;
1068}
Len Brown4be44fc2005-08-05 00:44:28 -04001069#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Len Brown4be44fc2005-08-05 00:44:28 -04001071acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Len Brown4be44fc2005-08-05 00:44:28 -04001073 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 case ACPI_SIGNAL_FATAL:
1075 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1076 break;
1077 case ACPI_SIGNAL_BREAKPOINT:
1078 /*
1079 * AML Breakpoint
1080 * ACPI spec. says to treat it as a NOP unless
1081 * you are debugging. So if/when we integrate
1082 * AML debugger into the kernel debugger its
1083 * hook will go here. But until then it is
1084 * not useful to print anything on breakpoints.
1085 */
1086 break;
1087 default:
1088 break;
1089 }
1090
1091 return AE_OK;
1092}
Len Brown4be44fc2005-08-05 00:44:28 -04001093
Len Brown4be44fc2005-08-05 00:44:28 -04001094static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -04001097 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 if (!str || !*str)
1100 return 0;
1101
1102 for (; count-- && str && *str; str++) {
1103 if (isalnum(*str) || *str == ' ' || *str == ':')
1104 *p++ = *str;
1105 else if (*str == '\'' || *str == '"')
1106 continue;
1107 else
1108 break;
1109 }
1110 *p = 0;
1111
1112 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
1116__setup("acpi_os_name=", acpi_os_name_setup);
1117
Len Brownd4b7dc42008-01-23 20:50:56 -05001118static void __init set_osi_linux(unsigned int enable)
1119{
1120 if (osi_linux.enable != enable) {
1121 osi_linux.enable = enable;
1122 printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
1123 enable ? "Add": "Delet");
1124 }
1125 return;
1126}
Len Brownf5076542007-05-30 00:10:38 -04001127
Len Brownd4b7dc42008-01-23 20:50:56 -05001128static void __init acpi_cmdline_osi_linux(unsigned int enable)
1129{
1130 osi_linux.cmdline = 1; /* cmdline set the default */
1131 set_osi_linux(enable);
Len Brownf5076542007-05-30 00:10:38 -04001132
Len Brownd4b7dc42008-01-23 20:50:56 -05001133 return;
1134}
1135
1136void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1137{
1138 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
1139
1140 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1141
1142 if (enable == -1)
1143 return;
1144
1145 osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
1146
1147 set_osi_linux(enable);
1148
Len Brownf5076542007-05-30 00:10:38 -04001149 return;
1150}
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152/*
Len Brownae00d812007-05-29 18:43:33 -04001153 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1154 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -04001156 * string starting with '!' disables that string
1157 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 */
Len Brown46c1fbd2008-02-13 23:13:25 -05001159int __init acpi_osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 if (str == NULL || *str == '\0') {
1162 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1163 acpi_gbl_create_osi_method = FALSE;
Len Brownaa2e09d2007-07-02 23:57:45 -04001164 } else if (!strcmp("!Linux", str)) {
Len Brownd4b7dc42008-01-23 20:50:56 -05001165 acpi_cmdline_osi_linux(0); /* !enable */
Len Brownae00d812007-05-29 18:43:33 -04001166 } else if (*str == '!') {
1167 if (acpi_osi_invalidate(++str) == AE_OK)
1168 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
Len Brownf5076542007-05-30 00:10:38 -04001169 } else if (!strcmp("Linux", str)) {
Len Brownd4b7dc42008-01-23 20:50:56 -05001170 acpi_cmdline_osi_linux(1); /* enable */
Len Brownae00d812007-05-29 18:43:33 -04001171 } else if (*osi_additional_string == '\0') {
1172 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
1173 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175
1176 return 1;
1177}
1178
1179__setup("acpi_osi=", acpi_osi_setup);
1180
1181/* enable serialization to combat AE_ALREADY_EXISTS errors */
Len Brown4be44fc2005-08-05 00:44:28 -04001182static int __init acpi_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 printk(KERN_INFO PREFIX "serialize enabled\n");
1185
1186 acpi_gbl_all_methods_serialized = TRUE;
1187
1188 return 1;
1189}
1190
1191__setup("acpi_serialize", acpi_serialize_setup);
1192
Thomas Renningerdf92e692008-02-04 23:31:22 -08001193/* Check of resource interference between native drivers and ACPI
1194 * OperationRegions (SystemIO and System Memory only).
1195 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1196 * in arbitrary AML code and can interfere with legacy drivers.
1197 * acpi_enforce_resources= can be set to:
1198 *
Luca Tettamanti7e905602009-03-30 00:01:27 +02001199 * - strict (default) (2)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001200 * -> further driver trying to access the resources will not load
Luca Tettamanti7e905602009-03-30 00:01:27 +02001201 * - lax (1)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001202 * -> further driver trying to access the resources will load, but you
1203 * get a system message that something might go wrong...
1204 *
1205 * - no (0)
1206 * -> ACPI Operation Region resources will not be registered
1207 *
1208 */
1209#define ENFORCE_RESOURCES_STRICT 2
1210#define ENFORCE_RESOURCES_LAX 1
1211#define ENFORCE_RESOURCES_NO 0
1212
Luca Tettamanti7e905602009-03-30 00:01:27 +02001213static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001214
1215static int __init acpi_enforce_resources_setup(char *str)
1216{
1217 if (str == NULL || *str == '\0')
1218 return 0;
1219
1220 if (!strcmp("strict", str))
1221 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1222 else if (!strcmp("lax", str))
1223 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1224 else if (!strcmp("no", str))
1225 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1226
1227 return 1;
1228}
1229
1230__setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1231
1232/* Check for resource conflicts between ACPI OperationRegions and native
1233 * drivers */
Jean Delvare876fba42009-11-11 15:22:15 +01001234int acpi_check_resource_conflict(const struct resource *res)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001235{
1236 struct acpi_res_list *res_list_elem;
1237 int ioport;
1238 int clash = 0;
1239
1240 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1241 return 0;
1242 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1243 return 0;
1244
1245 ioport = res->flags & IORESOURCE_IO;
1246
1247 spin_lock(&acpi_res_lock);
1248 list_for_each_entry(res_list_elem, &resource_list_head,
1249 resource_list) {
1250 if (ioport && (res_list_elem->resource_type
1251 != ACPI_ADR_SPACE_SYSTEM_IO))
1252 continue;
1253 if (!ioport && (res_list_elem->resource_type
1254 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1255 continue;
1256
1257 if (res->end < res_list_elem->start
1258 || res_list_elem->end < res->start)
1259 continue;
1260 clash = 1;
1261 break;
1262 }
1263 spin_unlock(&acpi_res_lock);
1264
1265 if (clash) {
1266 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
Chase Douglas1638bca2010-03-22 15:08:09 -04001267 printk(KERN_WARNING "ACPI: resource %s %pR"
1268 " conflicts with ACPI region %s %pR\n",
1269 res->name, res, res_list_elem->name,
1270 res_list_elem);
Jean Delvare14f03342009-09-08 15:31:46 +02001271 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1272 printk(KERN_NOTICE "ACPI: This conflict may"
1273 " cause random problems and system"
1274 " instability\n");
1275 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1276 " for this device, you should use it instead of"
1277 " the native driver\n");
Thomas Renningerdf92e692008-02-04 23:31:22 -08001278 }
1279 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1280 return -EBUSY;
1281 }
1282 return 0;
1283}
Thomas Renninger443dea72008-02-04 23:31:23 -08001284EXPORT_SYMBOL(acpi_check_resource_conflict);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001285
1286int acpi_check_region(resource_size_t start, resource_size_t n,
1287 const char *name)
1288{
1289 struct resource res = {
1290 .start = start,
1291 .end = start + n - 1,
1292 .name = name,
1293 .flags = IORESOURCE_IO,
1294 };
1295
1296 return acpi_check_resource_conflict(&res);
1297}
1298EXPORT_SYMBOL(acpi_check_region);
1299
1300int acpi_check_mem_region(resource_size_t start, resource_size_t n,
1301 const char *name)
1302{
1303 struct resource res = {
1304 .start = start,
1305 .end = start + n - 1,
1306 .name = name,
1307 .flags = IORESOURCE_MEM,
1308 };
1309
1310 return acpi_check_resource_conflict(&res);
1311
1312}
1313EXPORT_SYMBOL(acpi_check_mem_region);
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315/*
Jean Delvare70dd6be2010-05-27 19:58:37 +02001316 * Let drivers know whether the resource checks are effective
1317 */
1318int acpi_resources_are_enforced(void)
1319{
1320 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1321}
1322EXPORT_SYMBOL(acpi_resources_are_enforced);
1323
1324/*
Robert Moore73459f72005-06-24 00:00:00 -04001325 * Acquire a spinlock.
1326 *
1327 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001328 */
1329
Bob Moore967440e2006-06-23 17:04:00 -04001330acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001331{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001332 acpi_cpu_flags flags;
Bob Moore967440e2006-06-23 17:04:00 -04001333 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001334 return flags;
1335}
1336
1337/*
1338 * Release a spinlock. See above.
1339 */
1340
Bob Moore967440e2006-06-23 17:04:00 -04001341void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001342{
Bob Moore967440e2006-06-23 17:04:00 -04001343 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001344}
1345
Robert Moore73459f72005-06-24 00:00:00 -04001346#ifndef ACPI_USE_LOCAL_CACHE
1347
1348/*******************************************************************************
1349 *
1350 * FUNCTION: acpi_os_create_cache
1351 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001352 * PARAMETERS: name - Ascii name for the cache
1353 * size - Size of each cached object
1354 * depth - Maximum depth of the cache (in objects) <ignored>
1355 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001356 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001357 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001358 *
1359 * DESCRIPTION: Create a cache object
1360 *
1361 ******************************************************************************/
1362
1363acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001364acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001365{
Paul Mundt20c2df82007-07-20 10:11:58 +09001366 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001367 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001368 return AE_ERROR;
1369 else
1370 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001371}
1372
1373/*******************************************************************************
1374 *
1375 * FUNCTION: acpi_os_purge_cache
1376 *
1377 * PARAMETERS: Cache - Handle to cache object
1378 *
1379 * RETURN: Status
1380 *
1381 * DESCRIPTION: Free all objects within the requested cache.
1382 *
1383 ******************************************************************************/
1384
Len Brown4be44fc2005-08-05 00:44:28 -04001385acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001386{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001387 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001388 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001389}
1390
1391/*******************************************************************************
1392 *
1393 * FUNCTION: acpi_os_delete_cache
1394 *
1395 * PARAMETERS: Cache - Handle to cache object
1396 *
1397 * RETURN: Status
1398 *
1399 * DESCRIPTION: Free all objects within the requested cache and delete the
1400 * cache object.
1401 *
1402 ******************************************************************************/
1403
Len Brown4be44fc2005-08-05 00:44:28 -04001404acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001405{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001406 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001407 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001408}
1409
1410/*******************************************************************************
1411 *
1412 * FUNCTION: acpi_os_release_object
1413 *
1414 * PARAMETERS: Cache - Handle to cache object
1415 * Object - The object to be released
1416 *
1417 * RETURN: None
1418 *
1419 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1420 * the object is deleted.
1421 *
1422 ******************************************************************************/
1423
Len Brown4be44fc2005-08-05 00:44:28 -04001424acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001425{
Len Brown4be44fc2005-08-05 00:44:28 -04001426 kmem_cache_free(cache, object);
1427 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001428}
1429
Bob Mooreb229cf92006-04-21 17:15:00 -04001430/******************************************************************************
1431 *
1432 * FUNCTION: acpi_os_validate_interface
1433 *
1434 * PARAMETERS: interface - Requested interface to be validated
1435 *
1436 * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
1437 *
1438 * DESCRIPTION: Match an interface string to the interfaces supported by the
1439 * host. Strings originate from an AML call to the _OSI method.
1440 *
1441 *****************************************************************************/
1442
1443acpi_status
1444acpi_os_validate_interface (char *interface)
1445{
Len Brownae00d812007-05-29 18:43:33 -04001446 if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1447 return AE_OK;
Len Brownf5076542007-05-30 00:10:38 -04001448 if (!strcmp("Linux", interface)) {
Len Brownd4b7dc42008-01-23 20:50:56 -05001449
1450 printk(KERN_NOTICE PREFIX
1451 "BIOS _OSI(Linux) query %s%s\n",
1452 osi_linux.enable ? "honored" : "ignored",
1453 osi_linux.cmdline ? " via cmdline" :
1454 osi_linux.dmi ? " via DMI" : "");
1455
Len Brownd4b7dc42008-01-23 20:50:56 -05001456 if (osi_linux.enable)
Len Brownf5076542007-05-30 00:10:38 -04001457 return AE_OK;
1458 }
Len Brownae00d812007-05-29 18:43:33 -04001459 return AE_SUPPORT;
Bob Mooreb229cf92006-04-21 17:15:00 -04001460}
1461
Lin Minga5fe1a02009-08-13 10:43:27 +08001462static inline int acpi_res_list_add(struct acpi_res_list *res)
1463{
1464 struct acpi_res_list *res_list_elem;
1465
1466 list_for_each_entry(res_list_elem, &resource_list_head,
1467 resource_list) {
1468
1469 if (res->resource_type == res_list_elem->resource_type &&
1470 res->start == res_list_elem->start &&
1471 res->end == res_list_elem->end) {
1472
1473 /*
1474 * The Region(addr,len) already exist in the list,
1475 * just increase the count
1476 */
1477
1478 res_list_elem->count++;
1479 return 0;
1480 }
1481 }
1482
1483 res->count = 1;
1484 list_add(&res->resource_list, &resource_list_head);
1485 return 1;
1486}
1487
1488static inline void acpi_res_list_del(struct acpi_res_list *res)
1489{
1490 struct acpi_res_list *res_list_elem;
1491
1492 list_for_each_entry(res_list_elem, &resource_list_head,
1493 resource_list) {
1494
1495 if (res->resource_type == res_list_elem->resource_type &&
1496 res->start == res_list_elem->start &&
1497 res->end == res_list_elem->end) {
1498
1499 /*
1500 * If the res count is decreased to 0,
1501 * remove and free it
1502 */
1503
1504 if (--res_list_elem->count == 0) {
1505 list_del(&res_list_elem->resource_list);
1506 kfree(res_list_elem);
1507 }
1508 return;
1509 }
1510 }
1511}
1512
1513acpi_status
1514acpi_os_invalidate_address(
1515 u8 space_id,
1516 acpi_physical_address address,
1517 acpi_size length)
1518{
1519 struct acpi_res_list res;
1520
1521 switch (space_id) {
1522 case ACPI_ADR_SPACE_SYSTEM_IO:
1523 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001524 /* Only interference checks against SystemIO and SystemMemory
Lin Minga5fe1a02009-08-13 10:43:27 +08001525 are needed */
1526 res.start = address;
1527 res.end = address + length - 1;
1528 res.resource_type = space_id;
1529 spin_lock(&acpi_res_lock);
1530 acpi_res_list_del(&res);
1531 spin_unlock(&acpi_res_lock);
1532 break;
1533 case ACPI_ADR_SPACE_PCI_CONFIG:
1534 case ACPI_ADR_SPACE_EC:
1535 case ACPI_ADR_SPACE_SMBUS:
1536 case ACPI_ADR_SPACE_CMOS:
1537 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1538 case ACPI_ADR_SPACE_DATA_TABLE:
1539 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1540 break;
1541 }
1542 return AE_OK;
1543}
1544
Bob Mooreb229cf92006-04-21 17:15:00 -04001545/******************************************************************************
1546 *
1547 * FUNCTION: acpi_os_validate_address
1548 *
1549 * PARAMETERS: space_id - ACPI space ID
1550 * address - Physical address
1551 * length - Address length
1552 *
1553 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1554 * should return AE_AML_ILLEGAL_ADDRESS.
1555 *
1556 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1557 * the addresses accessed by AML operation regions.
1558 *
1559 *****************************************************************************/
1560
1561acpi_status
1562acpi_os_validate_address (
1563 u8 space_id,
1564 acpi_physical_address address,
Thomas Renningerdf92e692008-02-04 23:31:22 -08001565 acpi_size length,
1566 char *name)
Bob Mooreb229cf92006-04-21 17:15:00 -04001567{
Thomas Renningerdf92e692008-02-04 23:31:22 -08001568 struct acpi_res_list *res;
Lin Minga5fe1a02009-08-13 10:43:27 +08001569 int added;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001570 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1571 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001572
Thomas Renningerdf92e692008-02-04 23:31:22 -08001573 switch (space_id) {
1574 case ACPI_ADR_SPACE_SYSTEM_IO:
1575 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001576 /* Only interference checks against SystemIO and SystemMemory
Thomas Renningerdf92e692008-02-04 23:31:22 -08001577 are needed */
1578 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1579 if (!res)
1580 return AE_OK;
1581 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1582 strlcpy(res->name, name, 5);
1583 res->start = address;
1584 res->end = address + length - 1;
1585 res->resource_type = space_id;
1586 spin_lock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001587 added = acpi_res_list_add(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001588 spin_unlock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001589 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1590 "name: %s\n", added ? "Added" : "Already exist",
1591 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001592 ? "SystemIO" : "System Memory",
1593 (unsigned long long)res->start,
1594 (unsigned long long)res->end,
1595 res->name);
Lin Minga5fe1a02009-08-13 10:43:27 +08001596 if (!added)
1597 kfree(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001598 break;
1599 case ACPI_ADR_SPACE_PCI_CONFIG:
1600 case ACPI_ADR_SPACE_EC:
1601 case ACPI_ADR_SPACE_SMBUS:
1602 case ACPI_ADR_SPACE_CMOS:
1603 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1604 case ACPI_ADR_SPACE_DATA_TABLE:
1605 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1606 break;
1607 }
1608 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001609}
Robert Moore73459f72005-06-24 00:00:00 -04001610#endif
Myron Stowed362eda2010-10-21 14:24:04 -06001611
1612acpi_status __init acpi_os_initialize(void)
1613{
1614 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1615 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1616 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1617 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1618
1619 return AE_OK;
1620}
1621
1622acpi_status acpi_os_initialize1(void)
1623{
1624 kacpid_wq = create_workqueue("kacpid");
1625 kacpi_notify_wq = create_workqueue("kacpi_notify");
1626 kacpi_hotplug_wq = create_workqueue("kacpi_hotplug");
1627 BUG_ON(!kacpid_wq);
1628 BUG_ON(!kacpi_notify_wq);
1629 BUG_ON(!kacpi_hotplug_wq);
1630 return AE_OK;
1631}
1632
1633acpi_status acpi_os_terminate(void)
1634{
1635 if (acpi_irq_handler) {
1636 acpi_os_remove_interrupt_handler(acpi_irq_irq,
1637 acpi_irq_handler);
1638 }
1639
1640 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1641 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1642 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1643 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1644
1645 destroy_workqueue(kacpid_wq);
1646 destroy_workqueue(kacpi_notify_wq);
1647 destroy_workqueue(kacpi_hotplug_wq);
1648
1649 return AE_OK;
1650}