blob: f6785bced215b9d99b899fa6f769ad0074d3efe1 [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>
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +010041#include <linux/acpi_io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/efi.h>
Thomas Renningerdf92e692008-02-04 23:31:22 -080043#include <linux/ioport.h>
44#include <linux/list.h>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -040045#include <linux/jiffies.h>
46#include <linux/semaphore.h>
47
48#include <asm/io.h>
49#include <asm/uaccess.h>
50
51#include <acpi/acpi.h>
52#include <acpi/acpi_bus.h>
53#include <acpi/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define _COMPONENT ACPI_OS_SERVICES
Len Brownf52fd662007-02-12 22:42:12 -050056ACPI_MODULE_NAME("osl");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define PREFIX "ACPI: "
Len Brown4be44fc2005-08-05 00:44:28 -040058struct acpi_os_dpc {
59 acpi_osd_exec_callback function;
60 void *context;
David Howells65f27f32006-11-22 14:55:48 +000061 struct work_struct work;
Bjorn Helgaas9ac61852009-08-31 22:32:10 +000062 int wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65#ifdef CONFIG_ACPI_CUSTOM_DSDT
66#include CONFIG_ACPI_CUSTOM_DSDT_FILE
67#endif
68
69#ifdef ENABLE_DEBUGGER
70#include <linux/kdb.h>
71
72/* stuff for debugger support */
73int acpi_in_debugger;
74EXPORT_SYMBOL(acpi_in_debugger);
75
76extern char line_buf[80];
Len Brown4be44fc2005-08-05 00:44:28 -040077#endif /*ENABLE_DEBUGGER */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static unsigned int acpi_irq_irq;
80static acpi_osd_handler acpi_irq_handler;
81static void *acpi_irq_context;
82static struct workqueue_struct *kacpid_wq;
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -040083static struct workqueue_struct *kacpi_notify_wq;
Zhang Ruic02256b2009-06-23 10:20:29 +080084static struct workqueue_struct *kacpi_hotplug_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Thomas Renningerdf92e692008-02-04 23:31:22 -080086struct acpi_res_list {
87 resource_size_t start;
88 resource_size_t end;
89 acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
90 char name[5]; /* only can have a length of 4 chars, make use of this
91 one instead of res->name, no need to kalloc then */
92 struct list_head resource_list;
Lin Minga5fe1a02009-08-13 10:43:27 +080093 int count;
Thomas Renningerdf92e692008-02-04 23:31:22 -080094};
95
96static LIST_HEAD(resource_list_head);
97static DEFINE_SPINLOCK(acpi_res_lock);
98
Myron Stowe620242a2010-10-21 14:23:53 -060099/*
100 * This list of permanent mappings is for memory that may be accessed from
101 * interrupt context, where we can't do the ioremap().
102 */
103struct acpi_ioremap {
104 struct list_head list;
105 void __iomem *virt;
106 acpi_physical_address phys;
107 acpi_size size;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600108 struct kref ref;
Myron Stowe620242a2010-10-21 14:23:53 -0600109};
110
111static LIST_HEAD(acpi_ioremaps);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100112static DEFINE_MUTEX(acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600113
Lin Mingb0ed7a92010-08-06 09:35:51 +0800114static void __init acpi_osi_setup_late(void);
Len Brownae00d812007-05-29 18:43:33 -0400115
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;
Lin Mingd90aa922010-12-09 16:50:52 +0800153} osi_linux = {0, 0, 0};
Len Brownf5076542007-05-30 00:10:38 -0400154
Lin Mingb0ed7a92010-08-06 09:35:51 +0800155static u32 acpi_osi_handler(acpi_string interface, u32 supported)
156{
157 if (!strcmp("Linux", interface)) {
158
Len Brown3af283e2010-10-15 21:38:57 -0400159 printk(KERN_NOTICE FW_BUG PREFIX
Lin Mingb0ed7a92010-08-06 09:35:51 +0800160 "BIOS _OSI(Linux) query %s%s\n",
161 osi_linux.enable ? "honored" : "ignored",
162 osi_linux.cmdline ? " via cmdline" :
163 osi_linux.dmi ? " via DMI" : "");
164 }
165
166 return supported;
167}
168
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700169static void __init acpi_request_region (struct acpi_generic_address *addr,
170 unsigned int length, char *desc)
171{
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700172 if (!addr->address || !length)
173 return;
174
Andi Kleencfa806f2010-07-20 15:18:36 -0700175 /* Resources are never freed */
Len Browneee3c852007-02-03 01:38:16 -0500176 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Andi Kleencfa806f2010-07-20 15:18:36 -0700177 request_region(addr->address, length, desc);
Len Browneee3c852007-02-03 01:38:16 -0500178 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
Andi Kleencfa806f2010-07-20 15:18:36 -0700179 request_mem_region(addr->address, length, desc);
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700180}
181
182static int __init acpi_reserve_resources(void)
183{
Len Browneee3c852007-02-03 01:38:16 -0500184 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700185 "ACPI PM1a_EVT_BLK");
186
Len Browneee3c852007-02-03 01:38:16 -0500187 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700188 "ACPI PM1b_EVT_BLK");
189
Len Browneee3c852007-02-03 01:38:16 -0500190 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700191 "ACPI PM1a_CNT_BLK");
192
Len Browneee3c852007-02-03 01:38:16 -0500193 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700194 "ACPI PM1b_CNT_BLK");
195
Len Browneee3c852007-02-03 01:38:16 -0500196 if (acpi_gbl_FADT.pm_timer_length == 4)
197 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700198
Len Browneee3c852007-02-03 01:38:16 -0500199 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700200 "ACPI PM2_CNT_BLK");
201
202 /* Length of GPE blocks must be a non-negative multiple of 2 */
203
Len Browneee3c852007-02-03 01:38:16 -0500204 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
205 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
206 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700207
Len Browneee3c852007-02-03 01:38:16 -0500208 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
209 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
210 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700211
212 return 0;
213}
214device_initcall(acpi_reserve_resources);
215
Len Brown4be44fc2005-08-05 00:44:28 -0400216void acpi_os_printf(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 va_list args;
219 va_start(args, fmt);
220 acpi_os_vprintf(fmt, args);
221 va_end(args);
222}
Len Brown4be44fc2005-08-05 00:44:28 -0400223
Len Brown4be44fc2005-08-05 00:44:28 -0400224void acpi_os_vprintf(const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 static char buffer[512];
Len Brown4be44fc2005-08-05 00:44:28 -0400227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 vsprintf(buffer, fmt, args);
229
230#ifdef ENABLE_DEBUGGER
231 if (acpi_in_debugger) {
232 kdb_printf("%s", buffer);
233 } else {
Frank Seidel4d939152009-02-04 17:03:07 +0100234 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236#else
Frank Seidel4d939152009-02-04 17:03:07 +0100237 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#endif
239}
240
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300241acpi_physical_address __init acpi_os_get_root_pointer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 if (efi_enabled) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800244 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300245 return efi.acpi20;
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800246 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300247 return efi.acpi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400249 printk(KERN_ERR PREFIX
250 "System description tables not found\n");
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Len Brown239665a2007-11-23 20:08:02 -0500253 } else {
254 acpi_physical_address pa = 0;
255
256 acpi_find_root_pointer(&pa);
257 return pa;
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600261/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe4a3cba52010-10-21 14:24:14 -0600262static struct acpi_ioremap *
263acpi_map_lookup(acpi_physical_address phys, acpi_size size)
Myron Stowe620242a2010-10-21 14:23:53 -0600264{
265 struct acpi_ioremap *map;
266
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600267 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe620242a2010-10-21 14:23:53 -0600268 if (map->phys <= phys &&
269 phys + size <= map->phys + map->size)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600270 return map;
271
272 return NULL;
273}
274
275/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
276static void __iomem *
277acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
278{
279 struct acpi_ioremap *map;
280
281 map = acpi_map_lookup(phys, size);
282 if (map)
283 return map->virt + (phys - map->phys);
Myron Stowe620242a2010-10-21 14:23:53 -0600284
285 return NULL;
286}
287
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600288/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe620242a2010-10-21 14:23:53 -0600289static struct acpi_ioremap *
290acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
291{
292 struct acpi_ioremap *map;
293
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600294 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600295 if (map->virt <= virt &&
296 virt + size <= map->virt + map->size)
Myron Stowe620242a2010-10-21 14:23:53 -0600297 return map;
298
299 return NULL;
300}
301
Jan Beulich2fdf0742007-12-13 08:33:59 +0000302void __iomem *__init_refok
303acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Myron Stowe4a3cba52010-10-21 14:24:14 -0600305 struct acpi_ioremap *map, *tmp_map;
Myron Stowe620242a2010-10-21 14:23:53 -0600306 void __iomem *virt;
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100307 acpi_physical_address pg_off;
308 acpi_size pg_sz;
Myron Stowe620242a2010-10-21 14:23:53 -0600309
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800310 if (phys > ULONG_MAX) {
311 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
Randy Dunlap70c08462007-02-13 16:11:36 -0800312 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Myron Stowe620242a2010-10-21 14:23:53 -0600314
315 if (!acpi_gbl_permanent_mmap)
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300316 return __acpi_map_table((unsigned long)phys, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600317
318 map = kzalloc(sizeof(*map), GFP_KERNEL);
319 if (!map)
320 return NULL;
321
Myron Stowe4a3cba52010-10-21 14:24:14 -0600322 pg_off = round_down(phys, PAGE_SIZE);
323 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100324 virt = acpi_os_ioremap(pg_off, pg_sz);
Myron Stowe620242a2010-10-21 14:23:53 -0600325 if (!virt) {
326 kfree(map);
327 return NULL;
328 }
329
330 INIT_LIST_HEAD(&map->list);
331 map->virt = virt;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600332 map->phys = pg_off;
333 map->size = pg_sz;
334 kref_init(&map->ref);
Myron Stowe620242a2010-10-21 14:23:53 -0600335
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100336 mutex_lock(&acpi_ioremap_lock);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600337 /* Check if page has already been mapped. */
338 tmp_map = acpi_map_lookup(phys, size);
339 if (tmp_map) {
340 kref_get(&tmp_map->ref);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100341 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600342 iounmap(map->virt);
343 kfree(map);
344 return tmp_map->virt + (phys - tmp_map->phys);
345 }
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600346 list_add_tail_rcu(&map->list, &acpi_ioremaps);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100347 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600348
Myron Stowe4a3cba52010-10-21 14:24:14 -0600349 return map->virt + (phys - map->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800351EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Myron Stowe4a3cba52010-10-21 14:24:14 -0600353static void acpi_kref_del_iomap(struct kref *ref)
354{
355 struct acpi_ioremap *map;
356
357 map = container_of(ref, struct acpi_ioremap, ref);
358 list_del_rcu(&map->list);
359}
360
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100361static void acpi_os_remove_map(struct acpi_ioremap *map)
362{
363 synchronize_rcu();
364 iounmap(map->virt);
365 kfree(map);
366}
367
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800368void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Myron Stowe620242a2010-10-21 14:23:53 -0600370 struct acpi_ioremap *map;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600371 int del;
Myron Stowe620242a2010-10-21 14:23:53 -0600372
373 if (!acpi_gbl_permanent_mmap) {
Yinghai Lu7d972772009-02-07 15:39:41 -0800374 __acpi_unmap_table(virt, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600375 return;
376 }
377
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100378 mutex_lock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600379 map = acpi_map_lookup_virt(virt, size);
380 if (!map) {
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100381 mutex_unlock(&acpi_ioremap_lock);
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100382 WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
Myron Stowe620242a2010-10-21 14:23:53 -0600383 return;
384 }
Myron Stowe4a3cba52010-10-21 14:24:14 -0600385 del = kref_put(&map->ref, acpi_kref_del_iomap);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100386 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600387
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100388 if (del)
389 acpi_os_remove_map(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800391EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800393void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Yinghai Lu7d972772009-02-07 15:39:41 -0800394{
395 if (!acpi_gbl_permanent_mmap)
396 __acpi_unmap_table(virt, size);
397}
398
Rafael J. Wysocki073b4962011-02-08 23:37:31 +0100399static int acpi_os_map_generic_address(struct acpi_generic_address *addr)
Myron Stowe29718522010-10-21 14:23:59 -0600400{
401 void __iomem *virt;
402
403 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
404 return 0;
405
406 if (!addr->address || !addr->bit_width)
407 return -EINVAL;
408
409 virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
410 if (!virt)
411 return -EIO;
412
413 return 0;
414}
Myron Stowe29718522010-10-21 14:23:59 -0600415
Rafael J. Wysocki073b4962011-02-08 23:37:31 +0100416static void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
Myron Stowe29718522010-10-21 14:23:59 -0600417{
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100418 struct acpi_ioremap *map;
419 int del;
Myron Stowe29718522010-10-21 14:23:59 -0600420
421 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
422 return;
423
424 if (!addr->address || !addr->bit_width)
425 return;
426
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100427 mutex_lock(&acpi_ioremap_lock);
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100428 map = acpi_map_lookup(addr->address, addr->bit_width / 8);
429 if (!map) {
430 mutex_unlock(&acpi_ioremap_lock);
431 return;
432 }
433 del = kref_put(&map->ref, acpi_kref_del_iomap);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100434 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe29718522010-10-21 14:23:59 -0600435
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100436 if (del)
437 acpi_os_remove_map(map);
Myron Stowe29718522010-10-21 14:23:59 -0600438}
Myron Stowe29718522010-10-21 14:23:59 -0600439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440#ifdef ACPI_FUTURE_USAGE
441acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400442acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Len Brown4be44fc2005-08-05 00:44:28 -0400444 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return AE_BAD_PARAMETER;
446
447 *phys = virt_to_phys(virt);
448
449 return AE_OK;
450}
451#endif
452
453#define ACPI_MAX_OVERRIDE_LEN 100
454
455static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
456
457acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400458acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
459 acpi_string * new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 if (!init_val || !new_val)
462 return AE_BAD_PARAMETER;
463
464 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400465 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400467 acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 *new_val = acpi_os_name;
469 }
470
471 return AE_OK;
472}
473
474acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400475acpi_os_table_override(struct acpi_table_header * existing_table,
476 struct acpi_table_header ** new_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 if (!existing_table || !new_table)
479 return AE_BAD_PARAMETER;
480
Markus Gaugusch71fc47a2008-02-05 00:04:06 +0100481 *new_table = NULL;
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483#ifdef CONFIG_ACPI_CUSTOM_DSDT
484 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400485 *new_table = (struct acpi_table_header *)AmlCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486#endif
Éric Piel6ed31e92008-02-05 00:04:50 +0100487 if (*new_table != NULL) {
488 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
489 "this is unsafe: tainting kernel\n",
490 existing_table->signature,
491 existing_table->oem_table_id);
492 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return AE_OK;
495}
496
David Howells7d12e782006-10-05 14:55:46 +0100497static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Len Brown5229e872008-02-06 01:26:55 -0500499 u32 handled;
500
501 handled = (*acpi_irq_handler) (acpi_irq_context);
502
503 if (handled) {
504 acpi_irq_handled++;
505 return IRQ_HANDLED;
Len Brown88bea182009-04-21 00:35:47 -0400506 } else {
507 acpi_irq_not_handled++;
Len Brown5229e872008-02-06 01:26:55 -0500508 return IRQ_NONE;
Len Brown88bea182009-04-21 00:35:47 -0400509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400513acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
514 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 unsigned int irq;
517
Len Brown5229e872008-02-06 01:26:55 -0500518 acpi_irq_stats_init();
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /*
521 * Ignore the GSI from the core, and use the value in our copy of the
522 * FADT. It may not be the same if an interrupt source override exists
523 * for the SCI.
524 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300525 gsi = acpi_gbl_FADT.sci_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
527 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
528 gsi);
529 return AE_OK;
530 }
531
532 acpi_irq_handler = handler;
533 acpi_irq_context = context;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700534 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
536 return AE_NOT_ACQUIRED;
537 }
538 acpi_irq_irq = irq;
539
540 return AE_OK;
541}
542
Len Brown4be44fc2005-08-05 00:44:28 -0400543acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 if (irq) {
546 free_irq(irq, acpi_irq);
547 acpi_irq_handler = NULL;
548 acpi_irq_irq = 0;
549 }
550
551 return AE_OK;
552}
553
554/*
555 * Running in interpreter thread context, safe to sleep
556 */
557
Lin Ming439913f2010-01-28 10:53:19 +0800558void acpi_os_sleep(u64 ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800560 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
Len Brown4be44fc2005-08-05 00:44:28 -0400562
Len Brown4be44fc2005-08-05 00:44:28 -0400563void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 while (us) {
566 u32 delay = 1000;
567
568 if (delay > us)
569 delay = us;
570 udelay(delay);
571 touch_nmi_watchdog();
572 us -= delay;
573 }
574}
Len Brown4be44fc2005-08-05 00:44:28 -0400575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/*
577 * Support ACPI 3.0 AML Timer operand
578 * Returns 64-bit free-running, monotonically increasing timer
579 * with 100ns granularity
580 */
Len Brown4be44fc2005-08-05 00:44:28 -0400581u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 static u64 t;
584
585#ifdef CONFIG_HPET
586 /* TBD: use HPET if available */
587#endif
588
589#ifdef CONFIG_X86_PM_TIMER
590 /* TBD: default to PM timer if HPET was not available */
591#endif
592 if (!t)
593 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
594
595 return ++t;
596}
597
Len Brown4be44fc2005-08-05 00:44:28 -0400598acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 u32 dummy;
601
602 if (!value)
603 value = &dummy;
604
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800605 *value = 0;
606 if (width <= 8) {
Len Brown4be44fc2005-08-05 00:44:28 -0400607 *(u8 *) value = inb(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800608 } else if (width <= 16) {
Len Brown4be44fc2005-08-05 00:44:28 -0400609 *(u16 *) value = inw(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800610 } else if (width <= 32) {
Len Brown4be44fc2005-08-05 00:44:28 -0400611 *(u32 *) value = inl(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800612 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 BUG();
614 }
615
616 return AE_OK;
617}
Len Brown4be44fc2005-08-05 00:44:28 -0400618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619EXPORT_SYMBOL(acpi_os_read_port);
620
Len Brown4be44fc2005-08-05 00:44:28 -0400621acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800623 if (width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 outb(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800625 } else if (width <= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 outw(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800627 } else if (width <= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 outl(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800629 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 BUG();
631 }
632
633 return AE_OK;
634}
Len Brown4be44fc2005-08-05 00:44:28 -0400635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636EXPORT_SYMBOL(acpi_os_write_port);
637
638acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400639acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Len Brown4be44fc2005-08-05 00:44:28 -0400641 void __iomem *virt_addr;
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100642 unsigned int size = width / 8;
643 bool unmap = false;
644 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600646 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600647 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600648 if (!virt_addr) {
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100649 rcu_read_unlock();
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100650 virt_addr = acpi_os_ioremap(phys_addr, size);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100651 if (!virt_addr)
652 return AE_BAD_ADDRESS;
653 unmap = true;
Myron Stowe620242a2010-10-21 14:23:53 -0600654 }
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (!value)
657 value = &dummy;
658
659 switch (width) {
660 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400661 *(u8 *) value = readb(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 break;
663 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400664 *(u16 *) value = readw(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 break;
666 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400667 *(u32 *) value = readl(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 break;
669 default:
670 BUG();
671 }
672
Myron Stowe620242a2010-10-21 14:23:53 -0600673 if (unmap)
674 iounmap(virt_addr);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100675 else
676 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 return AE_OK;
679}
680
681acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400682acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Len Brown4be44fc2005-08-05 00:44:28 -0400684 void __iomem *virt_addr;
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100685 unsigned int size = width / 8;
686 bool unmap = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600688 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600689 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600690 if (!virt_addr) {
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100691 rcu_read_unlock();
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100692 virt_addr = acpi_os_ioremap(phys_addr, size);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100693 if (!virt_addr)
694 return AE_BAD_ADDRESS;
695 unmap = true;
Myron Stowe620242a2010-10-21 14:23:53 -0600696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 switch (width) {
699 case 8:
700 writeb(value, virt_addr);
701 break;
702 case 16:
703 writew(value, virt_addr);
704 break;
705 case 32:
706 writel(value, virt_addr);
707 break;
708 default:
709 BUG();
710 }
711
Myron Stowe620242a2010-10-21 14:23:53 -0600712 if (unmap)
713 iounmap(virt_addr);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100714 else
715 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 return AE_OK;
718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400721acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Bob Moorec5f02312010-08-06 08:57:53 +0800722 u64 *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 int result, size;
Bob Moorec5f02312010-08-06 08:57:53 +0800725 u32 value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 if (!value)
728 return AE_BAD_PARAMETER;
729
730 switch (width) {
731 case 8:
732 size = 1;
733 break;
734 case 16:
735 size = 2;
736 break;
737 case 32:
738 size = 4;
739 break;
740 default:
741 return AE_ERROR;
742 }
743
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500744 result = raw_pci_read(pci_id->segment, pci_id->bus,
745 PCI_DEVFN(pci_id->device, pci_id->function),
Bob Moorec5f02312010-08-06 08:57:53 +0800746 reg, size, &value32);
747 *value = value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 return (result ? AE_ERROR : AE_OK);
750}
Len Brown4be44fc2005-08-05 00:44:28 -0400751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400753acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Lin Ming439913f2010-01-28 10:53:19 +0800754 u64 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 int result, size;
757
758 switch (width) {
759 case 8:
760 size = 1;
761 break;
762 case 16:
763 size = 2;
764 break;
765 case 32:
766 size = 4;
767 break;
768 default:
769 return AE_ERROR;
770 }
771
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500772 result = raw_pci_write(pci_id->segment, pci_id->bus,
773 PCI_DEVFN(pci_id->device, pci_id->function),
774 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 return (result ? AE_ERROR : AE_OK);
777}
778
David Howells65f27f32006-11-22 14:55:48 +0000779static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
David Howells65f27f32006-11-22 14:55:48 +0000781 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400782
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000783 if (dpc->wait)
784 acpi_os_wait_events_complete(NULL);
Zhang Rui19cd8472008-08-28 10:05:06 +0800785
786 dpc->function(dpc->context);
787 kfree(dpc);
Zhang Rui19cd8472008-08-28 10:05:06 +0800788}
789
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400790/*******************************************************************************
791 *
792 * FUNCTION: acpi_os_execute
793 *
794 * PARAMETERS: Type - Type of the callback
795 * Function - Function to be executed
796 * Context - Function parameters
797 *
798 * RETURN: Status
799 *
800 * DESCRIPTION: Depending on type, either queues function for deferred execution or
801 * immediately executes function on a separate thread.
802 *
803 ******************************************************************************/
804
Zhang Rui19cd8472008-08-28 10:05:06 +0800805static acpi_status __acpi_os_execute(acpi_execute_type type,
806 acpi_osd_exec_callback function, void *context, int hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Len Brown4be44fc2005-08-05 00:44:28 -0400808 acpi_status status = AE_OK;
809 struct acpi_os_dpc *dpc;
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300810 struct workqueue_struct *queue;
Zhang Rui19cd8472008-08-28 10:05:06 +0800811 int ret;
Len Brown72945b22006-07-12 22:46:42 -0400812 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
813 "Scheduling function [%p(%p)] for deferred execution.\n",
814 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /*
817 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +0000818 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 * way that allows us to also free its memory inside the callee.
820 * Because we may want to schedule several tasks with different
821 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +0000822 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 */
Len Brown72945b22006-07-12 22:46:42 -0400824
David Howells65f27f32006-11-22 14:55:48 +0000825 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (!dpc)
Lin Ming889c78b2008-12-31 09:23:57 +0800827 return AE_NO_MEMORY;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 dpc->function = function;
830 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800831
Zhang Ruic02256b2009-06-23 10:20:29 +0800832 /*
833 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
834 * because the hotplug code may call driver .remove() functions,
835 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
836 * to flush these workqueues.
837 */
838 queue = hp ? kacpi_hotplug_wq :
839 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000840 dpc->wait = hp ? 1 : 0;
Zhang Ruibc736752010-03-22 15:48:54 +0800841
842 if (queue == kacpi_hotplug_wq)
843 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
844 else if (queue == kacpi_notify_wq)
845 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
846 else
847 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
848
Tejun Heo8fec62b2010-06-29 10:07:09 +0200849 /*
850 * On some machines, a software-initiated SMI causes corruption unless
851 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
852 * typically it's done in GPE-related methods that are run via
853 * workqueues, so we can avoid the known corruption cases by always
854 * queueing on CPU 0.
855 */
856 ret = queue_work_on(0, queue, &dpc->work);
Zhang Rui19cd8472008-08-28 10:05:06 +0800857
858 if (!ret) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800859 printk(KERN_ERR PREFIX
860 "Call to queue_work() failed.\n");
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300861 status = AE_ERROR;
862 kfree(dpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Lin Ming889c78b2008-12-31 09:23:57 +0800864 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
Len Brown4be44fc2005-08-05 00:44:28 -0400866
Zhang Rui19cd8472008-08-28 10:05:06 +0800867acpi_status acpi_os_execute(acpi_execute_type type,
868 acpi_osd_exec_callback function, void *context)
869{
870 return __acpi_os_execute(type, function, context, 0);
871}
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400872EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Zhang Rui19cd8472008-08-28 10:05:06 +0800874acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
875 void *context)
876{
877 return __acpi_os_execute(0, function, context, 1);
878}
879
Len Brown4be44fc2005-08-05 00:44:28 -0400880void acpi_os_wait_events_complete(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 flush_workqueue(kacpid_wq);
Zhang Rui2f67a062008-04-29 02:34:42 -0400883 flush_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
Len Brown4be44fc2005-08-05 00:44:28 -0400885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886EXPORT_SYMBOL(acpi_os_wait_events_complete);
887
888/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 * Deallocate the memory for a spinlock.
890 */
Bob Moore967440e2006-06-23 17:04:00 -0400891void acpi_os_delete_lock(acpi_spinlock handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Patrick Mocheld550d982006-06-27 00:41:40 -0400893 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400897acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Len Brown4be44fc2005-08-05 00:44:28 -0400899 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 sem = acpi_os_allocate(sizeof(struct semaphore));
902 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400903 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 memset(sem, 0, sizeof(struct semaphore));
905
906 sema_init(sem, initial_units);
907
Len Brown4be44fc2005-08-05 00:44:28 -0400908 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Len Brown4be44fc2005-08-05 00:44:28 -0400910 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
911 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Patrick Mocheld550d982006-06-27 00:41:40 -0400913 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916/*
917 * TODO: A better way to delete semaphores? Linux doesn't have a
918 * 'delete_semaphore()' function -- may result in an invalid
919 * pointer dereference for non-synchronized consumers. Should
920 * we at least check for blocked threads and signal/cancel them?
921 */
922
Len Brown4be44fc2005-08-05 00:44:28 -0400923acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Len Brown4be44fc2005-08-05 00:44:28 -0400925 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400928 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Len Brown4be44fc2005-08-05 00:44:28 -0400930 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400932 BUG_ON(!list_empty(&sem->wait_list));
Len Brown02438d82006-06-30 03:19:10 -0400933 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -0400934 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Patrick Mocheld550d982006-06-27 00:41:40 -0400936 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * TODO: Support for units > 1?
941 */
Len Brown4be44fc2005-08-05 00:44:28 -0400942acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Len Brown4be44fc2005-08-05 00:44:28 -0400944 acpi_status status = AE_OK;
945 struct semaphore *sem = (struct semaphore *)handle;
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400946 long jiffies;
Len Brown4be44fc2005-08-05 00:44:28 -0400947 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400950 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400953 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Len Brown4be44fc2005-08-05 00:44:28 -0400955 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
956 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400958 if (timeout == ACPI_WAIT_FOREVER)
959 jiffies = MAX_SCHEDULE_TIMEOUT;
960 else
961 jiffies = msecs_to_jiffies(timeout);
962
963 ret = down_timeout(sem, jiffies);
964 if (ret)
965 status = AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -0400968 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400969 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -0400970 handle, units, timeout,
971 acpi_format_exception(status)));
972 } else {
973 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400974 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400975 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977
Patrick Mocheld550d982006-06-27 00:41:40 -0400978 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981/*
982 * TODO: Support for units > 1?
983 */
Len Brown4be44fc2005-08-05 00:44:28 -0400984acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Len Brown4be44fc2005-08-05 00:44:28 -0400986 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400989 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400992 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Len Brown4be44fc2005-08-05 00:44:28 -0400994 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
995 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 up(sem);
998
Patrick Mocheld550d982006-06-27 00:41:40 -0400999 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
Len Brown4be44fc2005-08-05 00:44:28 -04001001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -04001003u32 acpi_os_get_line(char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005
1006#ifdef ENABLE_DEBUGGER
1007 if (acpi_in_debugger) {
1008 u32 chars;
1009
1010 kdb_read(buffer, sizeof(line_buf));
1011
1012 /* remove the CR kdb includes */
1013 chars = strlen(buffer) - 1;
1014 buffer[chars] = '\0';
1015 }
1016#endif
1017
1018 return 0;
1019}
Len Brown4be44fc2005-08-05 00:44:28 -04001020#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Len Brown4be44fc2005-08-05 00:44:28 -04001022acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Len Brown4be44fc2005-08-05 00:44:28 -04001024 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 case ACPI_SIGNAL_FATAL:
1026 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1027 break;
1028 case ACPI_SIGNAL_BREAKPOINT:
1029 /*
1030 * AML Breakpoint
1031 * ACPI spec. says to treat it as a NOP unless
1032 * you are debugging. So if/when we integrate
1033 * AML debugger into the kernel debugger its
1034 * hook will go here. But until then it is
1035 * not useful to print anything on breakpoints.
1036 */
1037 break;
1038 default:
1039 break;
1040 }
1041
1042 return AE_OK;
1043}
Len Brown4be44fc2005-08-05 00:44:28 -04001044
Len Brown4be44fc2005-08-05 00:44:28 -04001045static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -04001048 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 if (!str || !*str)
1051 return 0;
1052
1053 for (; count-- && str && *str; str++) {
1054 if (isalnum(*str) || *str == ' ' || *str == ':')
1055 *p++ = *str;
1056 else if (*str == '\'' || *str == '"')
1057 continue;
1058 else
1059 break;
1060 }
1061 *p = 0;
1062
1063 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067__setup("acpi_os_name=", acpi_os_name_setup);
1068
Lin Ming12d32062010-12-09 16:51:06 +08001069#define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
1070#define OSI_STRING_ENTRIES_MAX 16 /* arbitrary */
1071
1072struct osi_setup_entry {
1073 char string[OSI_STRING_LENGTH_MAX];
1074 bool enable;
1075};
1076
1077static struct osi_setup_entry __initdata osi_setup_entries[OSI_STRING_ENTRIES_MAX];
1078
Lin Mingd90aa922010-12-09 16:50:52 +08001079void __init acpi_osi_setup(char *str)
1080{
Lin Ming12d32062010-12-09 16:51:06 +08001081 struct osi_setup_entry *osi;
1082 bool enable = true;
1083 int i;
1084
Lin Mingd90aa922010-12-09 16:50:52 +08001085 if (!acpi_gbl_create_osi_method)
1086 return;
1087
1088 if (str == NULL || *str == '\0') {
1089 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1090 acpi_gbl_create_osi_method = FALSE;
Lin Ming12d32062010-12-09 16:51:06 +08001091 return;
1092 }
1093
1094 if (*str == '!') {
1095 str++;
1096 enable = false;
1097 }
1098
1099 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1100 osi = &osi_setup_entries[i];
1101 if (!strcmp(osi->string, str)) {
1102 osi->enable = enable;
1103 break;
1104 } else if (osi->string[0] == '\0') {
1105 osi->enable = enable;
1106 strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
1107 break;
1108 }
1109 }
Lin Mingd90aa922010-12-09 16:50:52 +08001110}
1111
Len Brownd4b7dc42008-01-23 20:50:56 -05001112static void __init set_osi_linux(unsigned int enable)
1113{
Lin Mingd90aa922010-12-09 16:50:52 +08001114 if (osi_linux.enable != enable)
Len Brownd4b7dc42008-01-23 20:50:56 -05001115 osi_linux.enable = enable;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001116
1117 if (osi_linux.enable)
1118 acpi_osi_setup("Linux");
1119 else
1120 acpi_osi_setup("!Linux");
1121
Len Brownd4b7dc42008-01-23 20:50:56 -05001122 return;
1123}
Len Brownf5076542007-05-30 00:10:38 -04001124
Len Brownd4b7dc42008-01-23 20:50:56 -05001125static void __init acpi_cmdline_osi_linux(unsigned int enable)
1126{
Lin Mingd90aa922010-12-09 16:50:52 +08001127 osi_linux.cmdline = 1; /* cmdline set the default and override DMI */
1128 osi_linux.dmi = 0;
Len Brownd4b7dc42008-01-23 20:50:56 -05001129 set_osi_linux(enable);
Len Brownf5076542007-05-30 00:10:38 -04001130
Len Brownd4b7dc42008-01-23 20:50:56 -05001131 return;
1132}
1133
1134void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1135{
Len Brownd4b7dc42008-01-23 20:50:56 -05001136 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1137
1138 if (enable == -1)
1139 return;
1140
Lin Mingd90aa922010-12-09 16:50:52 +08001141 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
Len Brownd4b7dc42008-01-23 20:50:56 -05001142 set_osi_linux(enable);
1143
Len Brownf5076542007-05-30 00:10:38 -04001144 return;
1145}
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147/*
Len Brownae00d812007-05-29 18:43:33 -04001148 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1149 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -04001151 * string starting with '!' disables that string
1152 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
Lin Mingb0ed7a92010-08-06 09:35:51 +08001154static void __init acpi_osi_setup_late(void)
1155{
Lin Ming12d32062010-12-09 16:51:06 +08001156 struct osi_setup_entry *osi;
1157 char *str;
1158 int i;
Lin Mingd90aa922010-12-09 16:50:52 +08001159 acpi_status status;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001160
Lin Ming12d32062010-12-09 16:51:06 +08001161 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1162 osi = &osi_setup_entries[i];
1163 str = osi->string;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001164
Lin Ming12d32062010-12-09 16:51:06 +08001165 if (*str == '\0')
1166 break;
1167 if (osi->enable) {
1168 status = acpi_install_interface(str);
Lin Mingd90aa922010-12-09 16:50:52 +08001169
Lin Ming12d32062010-12-09 16:51:06 +08001170 if (ACPI_SUCCESS(status))
1171 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1172 } else {
1173 status = acpi_remove_interface(str);
Lin Mingd90aa922010-12-09 16:50:52 +08001174
Lin Ming12d32062010-12-09 16:51:06 +08001175 if (ACPI_SUCCESS(status))
1176 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1177 }
Lin Mingb0ed7a92010-08-06 09:35:51 +08001178 }
1179}
1180
Lin Mingd90aa922010-12-09 16:50:52 +08001181static int __init osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Lin Mingd90aa922010-12-09 16:50:52 +08001183 if (str && !strcmp("Linux", str))
1184 acpi_cmdline_osi_linux(1);
1185 else if (str && !strcmp("!Linux", str))
1186 acpi_cmdline_osi_linux(0);
1187 else
1188 acpi_osi_setup(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 return 1;
1191}
1192
Lin Mingd90aa922010-12-09 16:50:52 +08001193__setup("acpi_osi=", osi_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195/* enable serialization to combat AE_ALREADY_EXISTS errors */
Len Brown4be44fc2005-08-05 00:44:28 -04001196static int __init acpi_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
1198 printk(KERN_INFO PREFIX "serialize enabled\n");
1199
1200 acpi_gbl_all_methods_serialized = TRUE;
1201
1202 return 1;
1203}
1204
1205__setup("acpi_serialize", acpi_serialize_setup);
1206
Thomas Renningerdf92e692008-02-04 23:31:22 -08001207/* Check of resource interference between native drivers and ACPI
1208 * OperationRegions (SystemIO and System Memory only).
1209 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1210 * in arbitrary AML code and can interfere with legacy drivers.
1211 * acpi_enforce_resources= can be set to:
1212 *
Luca Tettamanti7e905602009-03-30 00:01:27 +02001213 * - strict (default) (2)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001214 * -> further driver trying to access the resources will not load
Luca Tettamanti7e905602009-03-30 00:01:27 +02001215 * - lax (1)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001216 * -> further driver trying to access the resources will load, but you
1217 * get a system message that something might go wrong...
1218 *
1219 * - no (0)
1220 * -> ACPI Operation Region resources will not be registered
1221 *
1222 */
1223#define ENFORCE_RESOURCES_STRICT 2
1224#define ENFORCE_RESOURCES_LAX 1
1225#define ENFORCE_RESOURCES_NO 0
1226
Luca Tettamanti7e905602009-03-30 00:01:27 +02001227static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001228
1229static int __init acpi_enforce_resources_setup(char *str)
1230{
1231 if (str == NULL || *str == '\0')
1232 return 0;
1233
1234 if (!strcmp("strict", str))
1235 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1236 else if (!strcmp("lax", str))
1237 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1238 else if (!strcmp("no", str))
1239 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1240
1241 return 1;
1242}
1243
1244__setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1245
1246/* Check for resource conflicts between ACPI OperationRegions and native
1247 * drivers */
Jean Delvare876fba42009-11-11 15:22:15 +01001248int acpi_check_resource_conflict(const struct resource *res)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001249{
1250 struct acpi_res_list *res_list_elem;
Thomas Renninger106d1a02010-12-20 12:11:45 +01001251 int ioport = 0, clash = 0;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001252
1253 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1254 return 0;
1255 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1256 return 0;
1257
1258 ioport = res->flags & IORESOURCE_IO;
1259
1260 spin_lock(&acpi_res_lock);
1261 list_for_each_entry(res_list_elem, &resource_list_head,
1262 resource_list) {
1263 if (ioport && (res_list_elem->resource_type
1264 != ACPI_ADR_SPACE_SYSTEM_IO))
1265 continue;
1266 if (!ioport && (res_list_elem->resource_type
1267 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1268 continue;
1269
1270 if (res->end < res_list_elem->start
1271 || res_list_elem->end < res->start)
1272 continue;
1273 clash = 1;
1274 break;
1275 }
1276 spin_unlock(&acpi_res_lock);
1277
1278 if (clash) {
1279 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
Chase Douglas1638bca2010-03-22 15:08:09 -04001280 printk(KERN_WARNING "ACPI: resource %s %pR"
Thomas Renninger106d1a02010-12-20 12:11:45 +01001281 " conflicts with ACPI region %s "
1282 "[%s 0x%zx-0x%zx]\n",
Chase Douglas1638bca2010-03-22 15:08:09 -04001283 res->name, res, res_list_elem->name,
Thomas Renninger106d1a02010-12-20 12:11:45 +01001284 (res_list_elem->resource_type ==
1285 ACPI_ADR_SPACE_SYSTEM_IO) ? "io" : "mem",
1286 (size_t) res_list_elem->start,
1287 (size_t) res_list_elem->end);
Jean Delvare14f03342009-09-08 15:31:46 +02001288 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1289 printk(KERN_NOTICE "ACPI: This conflict may"
1290 " cause random problems and system"
1291 " instability\n");
1292 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1293 " for this device, you should use it instead of"
1294 " the native driver\n");
Thomas Renningerdf92e692008-02-04 23:31:22 -08001295 }
1296 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1297 return -EBUSY;
1298 }
1299 return 0;
1300}
Thomas Renninger443dea72008-02-04 23:31:23 -08001301EXPORT_SYMBOL(acpi_check_resource_conflict);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001302
1303int acpi_check_region(resource_size_t start, resource_size_t n,
1304 const char *name)
1305{
1306 struct resource res = {
1307 .start = start,
1308 .end = start + n - 1,
1309 .name = name,
1310 .flags = IORESOURCE_IO,
1311 };
1312
1313 return acpi_check_resource_conflict(&res);
1314}
1315EXPORT_SYMBOL(acpi_check_region);
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317/*
Jean Delvare70dd6be2010-05-27 19:58:37 +02001318 * Let drivers know whether the resource checks are effective
1319 */
1320int acpi_resources_are_enforced(void)
1321{
1322 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1323}
1324EXPORT_SYMBOL(acpi_resources_are_enforced);
1325
1326/*
Robert Moore73459f72005-06-24 00:00:00 -04001327 * Acquire a spinlock.
1328 *
1329 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001330 */
1331
Bob Moore967440e2006-06-23 17:04:00 -04001332acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001333{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001334 acpi_cpu_flags flags;
Bob Moore967440e2006-06-23 17:04:00 -04001335 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001336 return flags;
1337}
1338
1339/*
1340 * Release a spinlock. See above.
1341 */
1342
Bob Moore967440e2006-06-23 17:04:00 -04001343void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001344{
Bob Moore967440e2006-06-23 17:04:00 -04001345 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001346}
1347
Robert Moore73459f72005-06-24 00:00:00 -04001348#ifndef ACPI_USE_LOCAL_CACHE
1349
1350/*******************************************************************************
1351 *
1352 * FUNCTION: acpi_os_create_cache
1353 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001354 * PARAMETERS: name - Ascii name for the cache
1355 * size - Size of each cached object
1356 * depth - Maximum depth of the cache (in objects) <ignored>
1357 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001358 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001359 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001360 *
1361 * DESCRIPTION: Create a cache object
1362 *
1363 ******************************************************************************/
1364
1365acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001366acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001367{
Paul Mundt20c2df82007-07-20 10:11:58 +09001368 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001369 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001370 return AE_ERROR;
1371 else
1372 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001373}
1374
1375/*******************************************************************************
1376 *
1377 * FUNCTION: acpi_os_purge_cache
1378 *
1379 * PARAMETERS: Cache - Handle to cache object
1380 *
1381 * RETURN: Status
1382 *
1383 * DESCRIPTION: Free all objects within the requested cache.
1384 *
1385 ******************************************************************************/
1386
Len Brown4be44fc2005-08-05 00:44:28 -04001387acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001388{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001389 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001390 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001391}
1392
1393/*******************************************************************************
1394 *
1395 * FUNCTION: acpi_os_delete_cache
1396 *
1397 * PARAMETERS: Cache - Handle to cache object
1398 *
1399 * RETURN: Status
1400 *
1401 * DESCRIPTION: Free all objects within the requested cache and delete the
1402 * cache object.
1403 *
1404 ******************************************************************************/
1405
Len Brown4be44fc2005-08-05 00:44:28 -04001406acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001407{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001408 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001409 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001410}
1411
1412/*******************************************************************************
1413 *
1414 * FUNCTION: acpi_os_release_object
1415 *
1416 * PARAMETERS: Cache - Handle to cache object
1417 * Object - The object to be released
1418 *
1419 * RETURN: None
1420 *
1421 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1422 * the object is deleted.
1423 *
1424 ******************************************************************************/
1425
Len Brown4be44fc2005-08-05 00:44:28 -04001426acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001427{
Len Brown4be44fc2005-08-05 00:44:28 -04001428 kmem_cache_free(cache, object);
1429 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001430}
1431
Lin Minga5fe1a02009-08-13 10:43:27 +08001432static inline int acpi_res_list_add(struct acpi_res_list *res)
1433{
1434 struct acpi_res_list *res_list_elem;
1435
1436 list_for_each_entry(res_list_elem, &resource_list_head,
1437 resource_list) {
1438
1439 if (res->resource_type == res_list_elem->resource_type &&
1440 res->start == res_list_elem->start &&
1441 res->end == res_list_elem->end) {
1442
1443 /*
1444 * The Region(addr,len) already exist in the list,
1445 * just increase the count
1446 */
1447
1448 res_list_elem->count++;
1449 return 0;
1450 }
1451 }
1452
1453 res->count = 1;
1454 list_add(&res->resource_list, &resource_list_head);
1455 return 1;
1456}
1457
1458static inline void acpi_res_list_del(struct acpi_res_list *res)
1459{
1460 struct acpi_res_list *res_list_elem;
1461
1462 list_for_each_entry(res_list_elem, &resource_list_head,
1463 resource_list) {
1464
1465 if (res->resource_type == res_list_elem->resource_type &&
1466 res->start == res_list_elem->start &&
1467 res->end == res_list_elem->end) {
1468
1469 /*
1470 * If the res count is decreased to 0,
1471 * remove and free it
1472 */
1473
1474 if (--res_list_elem->count == 0) {
1475 list_del(&res_list_elem->resource_list);
1476 kfree(res_list_elem);
1477 }
1478 return;
1479 }
1480 }
1481}
1482
1483acpi_status
1484acpi_os_invalidate_address(
1485 u8 space_id,
1486 acpi_physical_address address,
1487 acpi_size length)
1488{
1489 struct acpi_res_list res;
1490
1491 switch (space_id) {
1492 case ACPI_ADR_SPACE_SYSTEM_IO:
1493 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001494 /* Only interference checks against SystemIO and SystemMemory
Lin Minga5fe1a02009-08-13 10:43:27 +08001495 are needed */
1496 res.start = address;
1497 res.end = address + length - 1;
1498 res.resource_type = space_id;
1499 spin_lock(&acpi_res_lock);
1500 acpi_res_list_del(&res);
1501 spin_unlock(&acpi_res_lock);
1502 break;
1503 case ACPI_ADR_SPACE_PCI_CONFIG:
1504 case ACPI_ADR_SPACE_EC:
1505 case ACPI_ADR_SPACE_SMBUS:
1506 case ACPI_ADR_SPACE_CMOS:
1507 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1508 case ACPI_ADR_SPACE_DATA_TABLE:
1509 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1510 break;
1511 }
1512 return AE_OK;
1513}
1514
Bob Mooreb229cf92006-04-21 17:15:00 -04001515/******************************************************************************
1516 *
1517 * FUNCTION: acpi_os_validate_address
1518 *
1519 * PARAMETERS: space_id - ACPI space ID
1520 * address - Physical address
1521 * length - Address length
1522 *
1523 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1524 * should return AE_AML_ILLEGAL_ADDRESS.
1525 *
1526 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1527 * the addresses accessed by AML operation regions.
1528 *
1529 *****************************************************************************/
1530
1531acpi_status
1532acpi_os_validate_address (
1533 u8 space_id,
1534 acpi_physical_address address,
Thomas Renningerdf92e692008-02-04 23:31:22 -08001535 acpi_size length,
1536 char *name)
Bob Mooreb229cf92006-04-21 17:15:00 -04001537{
Thomas Renningerdf92e692008-02-04 23:31:22 -08001538 struct acpi_res_list *res;
Lin Minga5fe1a02009-08-13 10:43:27 +08001539 int added;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001540 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1541 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001542
Thomas Renningerdf92e692008-02-04 23:31:22 -08001543 switch (space_id) {
1544 case ACPI_ADR_SPACE_SYSTEM_IO:
1545 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001546 /* Only interference checks against SystemIO and SystemMemory
Thomas Renningerdf92e692008-02-04 23:31:22 -08001547 are needed */
1548 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1549 if (!res)
1550 return AE_OK;
1551 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1552 strlcpy(res->name, name, 5);
1553 res->start = address;
1554 res->end = address + length - 1;
1555 res->resource_type = space_id;
1556 spin_lock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001557 added = acpi_res_list_add(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001558 spin_unlock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001559 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1560 "name: %s\n", added ? "Added" : "Already exist",
1561 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001562 ? "SystemIO" : "System Memory",
1563 (unsigned long long)res->start,
1564 (unsigned long long)res->end,
1565 res->name);
Lin Minga5fe1a02009-08-13 10:43:27 +08001566 if (!added)
1567 kfree(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001568 break;
1569 case ACPI_ADR_SPACE_PCI_CONFIG:
1570 case ACPI_ADR_SPACE_EC:
1571 case ACPI_ADR_SPACE_SMBUS:
1572 case ACPI_ADR_SPACE_CMOS:
1573 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1574 case ACPI_ADR_SPACE_DATA_TABLE:
1575 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1576 break;
1577 }
1578 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001579}
Robert Moore73459f72005-06-24 00:00:00 -04001580#endif
Myron Stowed362eda2010-10-21 14:24:04 -06001581
1582acpi_status __init acpi_os_initialize(void)
1583{
1584 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1585 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1586 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1587 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1588
1589 return AE_OK;
1590}
1591
Zhang Rui32d47ee2010-12-08 10:40:36 +08001592acpi_status __init acpi_os_initialize1(void)
Myron Stowed362eda2010-10-21 14:24:04 -06001593{
1594 kacpid_wq = create_workqueue("kacpid");
1595 kacpi_notify_wq = create_workqueue("kacpi_notify");
1596 kacpi_hotplug_wq = create_workqueue("kacpi_hotplug");
1597 BUG_ON(!kacpid_wq);
1598 BUG_ON(!kacpi_notify_wq);
1599 BUG_ON(!kacpi_hotplug_wq);
Len Brown1bd64d42010-10-26 14:50:56 -04001600 acpi_install_interface_handler(acpi_osi_handler);
1601 acpi_osi_setup_late();
Myron Stowed362eda2010-10-21 14:24:04 -06001602 return AE_OK;
1603}
1604
1605acpi_status acpi_os_terminate(void)
1606{
1607 if (acpi_irq_handler) {
1608 acpi_os_remove_interrupt_handler(acpi_irq_irq,
1609 acpi_irq_handler);
1610 }
1611
1612 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1613 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1614 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1615 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1616
1617 destroy_workqueue(kacpid_wq);
1618 destroy_workqueue(kacpi_notify_wq);
1619 destroy_workqueue(kacpi_hotplug_wq);
1620
1621 return AE_OK;
1622}