blob: 97061928249a5bd4827cdc50a4637af1a46a729a [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 Starikovskiyad71860a2007-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 Starikovskiyad71860a2007-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 Starikovskiyad71860a2007-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 Starikovskiyad71860a2007-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 Starikovskiyad71860a2007-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 Starikovskiyad71860a2007-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
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800361void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Myron Stowe620242a2010-10-21 14:23:53 -0600363 struct acpi_ioremap *map;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600364 int del;
Myron Stowe620242a2010-10-21 14:23:53 -0600365
366 if (!acpi_gbl_permanent_mmap) {
Yinghai Lu7d972772009-02-07 15:39:41 -0800367 __acpi_unmap_table(virt, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600368 return;
369 }
370
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100371 mutex_lock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600372 map = acpi_map_lookup_virt(virt, size);
373 if (!map) {
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100374 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600375 printk(KERN_ERR PREFIX "%s: bad address %p\n", __func__, virt);
376 dump_stack();
377 return;
378 }
379
Myron Stowe4a3cba52010-10-21 14:24:14 -0600380 del = kref_put(&map->ref, acpi_kref_del_iomap);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100381 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600382
Myron Stowe4a3cba52010-10-21 14:24:14 -0600383 if (!del)
384 return;
385
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600386 synchronize_rcu();
Myron Stowe620242a2010-10-21 14:23:53 -0600387 iounmap(map->virt);
388 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800390EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800392void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Yinghai Lu7d972772009-02-07 15:39:41 -0800393{
394 if (!acpi_gbl_permanent_mmap)
395 __acpi_unmap_table(virt, size);
396}
397
Rafael J. Wysocki073b4962011-02-08 23:37:31 +0100398static int acpi_os_map_generic_address(struct acpi_generic_address *addr)
Myron Stowe29718522010-10-21 14:23:59 -0600399{
400 void __iomem *virt;
401
402 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
403 return 0;
404
405 if (!addr->address || !addr->bit_width)
406 return -EINVAL;
407
408 virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
409 if (!virt)
410 return -EIO;
411
412 return 0;
413}
Myron Stowe29718522010-10-21 14:23:59 -0600414
Rafael J. Wysocki073b4962011-02-08 23:37:31 +0100415static void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
Myron Stowe29718522010-10-21 14:23:59 -0600416{
417 void __iomem *virt;
Myron Stowe29718522010-10-21 14:23:59 -0600418 acpi_size size = addr->bit_width / 8;
419
420 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
421 return;
422
423 if (!addr->address || !addr->bit_width)
424 return;
425
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100426 mutex_lock(&acpi_ioremap_lock);
Myron Stowe29718522010-10-21 14:23:59 -0600427 virt = acpi_map_vaddr_lookup(addr->address, size);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100428 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe29718522010-10-21 14:23:59 -0600429
430 acpi_os_unmap_memory(virt, size);
431}
Myron Stowe29718522010-10-21 14:23:59 -0600432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433#ifdef ACPI_FUTURE_USAGE
434acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400435acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Len Brown4be44fc2005-08-05 00:44:28 -0400437 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return AE_BAD_PARAMETER;
439
440 *phys = virt_to_phys(virt);
441
442 return AE_OK;
443}
444#endif
445
446#define ACPI_MAX_OVERRIDE_LEN 100
447
448static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
449
450acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400451acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
452 acpi_string * new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 if (!init_val || !new_val)
455 return AE_BAD_PARAMETER;
456
457 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400458 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400460 acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 *new_val = acpi_os_name;
462 }
463
464 return AE_OK;
465}
466
467acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400468acpi_os_table_override(struct acpi_table_header * existing_table,
469 struct acpi_table_header ** new_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 if (!existing_table || !new_table)
472 return AE_BAD_PARAMETER;
473
Markus Gaugusch71fc47a2008-02-05 00:04:06 +0100474 *new_table = NULL;
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#ifdef CONFIG_ACPI_CUSTOM_DSDT
477 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400478 *new_table = (struct acpi_table_header *)AmlCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479#endif
Éric Piel6ed31e92008-02-05 00:04:50 +0100480 if (*new_table != NULL) {
481 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
482 "this is unsafe: tainting kernel\n",
483 existing_table->signature,
484 existing_table->oem_table_id);
485 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return AE_OK;
488}
489
David Howells7d12e782006-10-05 14:55:46 +0100490static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Len Brown5229e872008-02-06 01:26:55 -0500492 u32 handled;
493
494 handled = (*acpi_irq_handler) (acpi_irq_context);
495
496 if (handled) {
497 acpi_irq_handled++;
498 return IRQ_HANDLED;
Len Brown88bea182009-04-21 00:35:47 -0400499 } else {
500 acpi_irq_not_handled++;
Len Brown5229e872008-02-06 01:26:55 -0500501 return IRQ_NONE;
Len Brown88bea182009-04-21 00:35:47 -0400502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400506acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
507 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 unsigned int irq;
510
Len Brown5229e872008-02-06 01:26:55 -0500511 acpi_irq_stats_init();
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /*
514 * Ignore the GSI from the core, and use the value in our copy of the
515 * FADT. It may not be the same if an interrupt source override exists
516 * for the SCI.
517 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300518 gsi = acpi_gbl_FADT.sci_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
520 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
521 gsi);
522 return AE_OK;
523 }
524
525 acpi_irq_handler = handler;
526 acpi_irq_context = context;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700527 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
529 return AE_NOT_ACQUIRED;
530 }
531 acpi_irq_irq = irq;
532
533 return AE_OK;
534}
535
Len Brown4be44fc2005-08-05 00:44:28 -0400536acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 if (irq) {
539 free_irq(irq, acpi_irq);
540 acpi_irq_handler = NULL;
541 acpi_irq_irq = 0;
542 }
543
544 return AE_OK;
545}
546
547/*
548 * Running in interpreter thread context, safe to sleep
549 */
550
Lin Ming439913f2010-01-28 10:53:19 +0800551void acpi_os_sleep(u64 ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800553 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
Len Brown4be44fc2005-08-05 00:44:28 -0400555
Len Brown4be44fc2005-08-05 00:44:28 -0400556void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 while (us) {
559 u32 delay = 1000;
560
561 if (delay > us)
562 delay = us;
563 udelay(delay);
564 touch_nmi_watchdog();
565 us -= delay;
566 }
567}
Len Brown4be44fc2005-08-05 00:44:28 -0400568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569/*
570 * Support ACPI 3.0 AML Timer operand
571 * Returns 64-bit free-running, monotonically increasing timer
572 * with 100ns granularity
573 */
Len Brown4be44fc2005-08-05 00:44:28 -0400574u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 static u64 t;
577
578#ifdef CONFIG_HPET
579 /* TBD: use HPET if available */
580#endif
581
582#ifdef CONFIG_X86_PM_TIMER
583 /* TBD: default to PM timer if HPET was not available */
584#endif
585 if (!t)
586 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
587
588 return ++t;
589}
590
Len Brown4be44fc2005-08-05 00:44:28 -0400591acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 u32 dummy;
594
595 if (!value)
596 value = &dummy;
597
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800598 *value = 0;
599 if (width <= 8) {
Len Brown4be44fc2005-08-05 00:44:28 -0400600 *(u8 *) value = inb(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800601 } else if (width <= 16) {
Len Brown4be44fc2005-08-05 00:44:28 -0400602 *(u16 *) value = inw(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800603 } else if (width <= 32) {
Len Brown4be44fc2005-08-05 00:44:28 -0400604 *(u32 *) value = inl(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800605 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 BUG();
607 }
608
609 return AE_OK;
610}
Len Brown4be44fc2005-08-05 00:44:28 -0400611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612EXPORT_SYMBOL(acpi_os_read_port);
613
Len Brown4be44fc2005-08-05 00:44:28 -0400614acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800616 if (width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 outb(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800618 } else if (width <= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 outw(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800620 } else if (width <= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 outl(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800622 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 BUG();
624 }
625
626 return AE_OK;
627}
Len Brown4be44fc2005-08-05 00:44:28 -0400628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629EXPORT_SYMBOL(acpi_os_write_port);
630
631acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400632acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Len Brown4be44fc2005-08-05 00:44:28 -0400634 void __iomem *virt_addr;
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100635 unsigned int size = width / 8;
636 bool unmap = false;
637 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600639 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600640 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600641 if (!virt_addr) {
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100642 rcu_read_unlock();
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100643 virt_addr = acpi_os_ioremap(phys_addr, size);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100644 if (!virt_addr)
645 return AE_BAD_ADDRESS;
646 unmap = true;
Myron Stowe620242a2010-10-21 14:23:53 -0600647 }
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (!value)
650 value = &dummy;
651
652 switch (width) {
653 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400654 *(u8 *) value = readb(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 break;
656 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400657 *(u16 *) value = readw(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
659 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400660 *(u32 *) value = readl(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 break;
662 default:
663 BUG();
664 }
665
Myron Stowe620242a2010-10-21 14:23:53 -0600666 if (unmap)
667 iounmap(virt_addr);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100668 else
669 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 return AE_OK;
672}
673
674acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400675acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Len Brown4be44fc2005-08-05 00:44:28 -0400677 void __iomem *virt_addr;
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100678 unsigned int size = width / 8;
679 bool unmap = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600681 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600682 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600683 if (!virt_addr) {
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100684 rcu_read_unlock();
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100685 virt_addr = acpi_os_ioremap(phys_addr, size);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100686 if (!virt_addr)
687 return AE_BAD_ADDRESS;
688 unmap = true;
Myron Stowe620242a2010-10-21 14:23:53 -0600689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 switch (width) {
692 case 8:
693 writeb(value, virt_addr);
694 break;
695 case 16:
696 writew(value, virt_addr);
697 break;
698 case 32:
699 writel(value, virt_addr);
700 break;
701 default:
702 BUG();
703 }
704
Myron Stowe620242a2010-10-21 14:23:53 -0600705 if (unmap)
706 iounmap(virt_addr);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100707 else
708 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 return AE_OK;
711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400714acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Bob Moorec5f02312010-08-06 08:57:53 +0800715 u64 *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 int result, size;
Bob Moorec5f02312010-08-06 08:57:53 +0800718 u32 value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 if (!value)
721 return AE_BAD_PARAMETER;
722
723 switch (width) {
724 case 8:
725 size = 1;
726 break;
727 case 16:
728 size = 2;
729 break;
730 case 32:
731 size = 4;
732 break;
733 default:
734 return AE_ERROR;
735 }
736
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500737 result = raw_pci_read(pci_id->segment, pci_id->bus,
738 PCI_DEVFN(pci_id->device, pci_id->function),
Bob Moorec5f02312010-08-06 08:57:53 +0800739 reg, size, &value32);
740 *value = value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 return (result ? AE_ERROR : AE_OK);
743}
Len Brown4be44fc2005-08-05 00:44:28 -0400744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400746acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Lin Ming439913f2010-01-28 10:53:19 +0800747 u64 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 int result, size;
750
751 switch (width) {
752 case 8:
753 size = 1;
754 break;
755 case 16:
756 size = 2;
757 break;
758 case 32:
759 size = 4;
760 break;
761 default:
762 return AE_ERROR;
763 }
764
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500765 result = raw_pci_write(pci_id->segment, pci_id->bus,
766 PCI_DEVFN(pci_id->device, pci_id->function),
767 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 return (result ? AE_ERROR : AE_OK);
770}
771
David Howells65f27f32006-11-22 14:55:48 +0000772static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
David Howells65f27f32006-11-22 14:55:48 +0000774 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400775
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000776 if (dpc->wait)
777 acpi_os_wait_events_complete(NULL);
Zhang Rui19cd8472008-08-28 10:05:06 +0800778
779 dpc->function(dpc->context);
780 kfree(dpc);
Zhang Rui19cd8472008-08-28 10:05:06 +0800781}
782
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400783/*******************************************************************************
784 *
785 * FUNCTION: acpi_os_execute
786 *
787 * PARAMETERS: Type - Type of the callback
788 * Function - Function to be executed
789 * Context - Function parameters
790 *
791 * RETURN: Status
792 *
793 * DESCRIPTION: Depending on type, either queues function for deferred execution or
794 * immediately executes function on a separate thread.
795 *
796 ******************************************************************************/
797
Zhang Rui19cd8472008-08-28 10:05:06 +0800798static acpi_status __acpi_os_execute(acpi_execute_type type,
799 acpi_osd_exec_callback function, void *context, int hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Len Brown4be44fc2005-08-05 00:44:28 -0400801 acpi_status status = AE_OK;
802 struct acpi_os_dpc *dpc;
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300803 struct workqueue_struct *queue;
Zhang Rui19cd8472008-08-28 10:05:06 +0800804 int ret;
Len Brown72945b22006-07-12 22:46:42 -0400805 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
806 "Scheduling function [%p(%p)] for deferred execution.\n",
807 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 /*
810 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +0000811 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 * way that allows us to also free its memory inside the callee.
813 * Because we may want to schedule several tasks with different
814 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +0000815 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Len Brown72945b22006-07-12 22:46:42 -0400817
David Howells65f27f32006-11-22 14:55:48 +0000818 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (!dpc)
Lin Ming889c78b2008-12-31 09:23:57 +0800820 return AE_NO_MEMORY;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 dpc->function = function;
823 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800824
Zhang Ruic02256b2009-06-23 10:20:29 +0800825 /*
826 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
827 * because the hotplug code may call driver .remove() functions,
828 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
829 * to flush these workqueues.
830 */
831 queue = hp ? kacpi_hotplug_wq :
832 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000833 dpc->wait = hp ? 1 : 0;
Zhang Ruibc736752010-03-22 15:48:54 +0800834
835 if (queue == kacpi_hotplug_wq)
836 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
837 else if (queue == kacpi_notify_wq)
838 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
839 else
840 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
841
Tejun Heo8fec62b2010-06-29 10:07:09 +0200842 /*
843 * On some machines, a software-initiated SMI causes corruption unless
844 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
845 * typically it's done in GPE-related methods that are run via
846 * workqueues, so we can avoid the known corruption cases by always
847 * queueing on CPU 0.
848 */
849 ret = queue_work_on(0, queue, &dpc->work);
Zhang Rui19cd8472008-08-28 10:05:06 +0800850
851 if (!ret) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800852 printk(KERN_ERR PREFIX
853 "Call to queue_work() failed.\n");
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300854 status = AE_ERROR;
855 kfree(dpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Lin Ming889c78b2008-12-31 09:23:57 +0800857 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
Len Brown4be44fc2005-08-05 00:44:28 -0400859
Zhang Rui19cd8472008-08-28 10:05:06 +0800860acpi_status acpi_os_execute(acpi_execute_type type,
861 acpi_osd_exec_callback function, void *context)
862{
863 return __acpi_os_execute(type, function, context, 0);
864}
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400865EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Zhang Rui19cd8472008-08-28 10:05:06 +0800867acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
868 void *context)
869{
870 return __acpi_os_execute(0, function, context, 1);
871}
872
Len Brown4be44fc2005-08-05 00:44:28 -0400873void acpi_os_wait_events_complete(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 flush_workqueue(kacpid_wq);
Zhang Rui2f67a062008-04-29 02:34:42 -0400876 flush_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
Len Brown4be44fc2005-08-05 00:44:28 -0400878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879EXPORT_SYMBOL(acpi_os_wait_events_complete);
880
881/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 * Deallocate the memory for a spinlock.
883 */
Bob Moore967440e32006-06-23 17:04:00 -0400884void acpi_os_delete_lock(acpi_spinlock handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Patrick Mocheld550d982006-06-27 00:41:40 -0400886 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400890acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Len Brown4be44fc2005-08-05 00:44:28 -0400892 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 sem = acpi_os_allocate(sizeof(struct semaphore));
895 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400896 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 memset(sem, 0, sizeof(struct semaphore));
898
899 sema_init(sem, initial_units);
900
Len Brown4be44fc2005-08-05 00:44:28 -0400901 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Len Brown4be44fc2005-08-05 00:44:28 -0400903 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
904 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Patrick Mocheld550d982006-06-27 00:41:40 -0400906 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909/*
910 * TODO: A better way to delete semaphores? Linux doesn't have a
911 * 'delete_semaphore()' function -- may result in an invalid
912 * pointer dereference for non-synchronized consumers. Should
913 * we at least check for blocked threads and signal/cancel them?
914 */
915
Len Brown4be44fc2005-08-05 00:44:28 -0400916acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Len Brown4be44fc2005-08-05 00:44:28 -0400918 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400921 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Len Brown4be44fc2005-08-05 00:44:28 -0400923 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400925 BUG_ON(!list_empty(&sem->wait_list));
Len Brown02438d82006-06-30 03:19:10 -0400926 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -0400927 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Patrick Mocheld550d982006-06-27 00:41:40 -0400929 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 * TODO: Support for units > 1?
934 */
Len Brown4be44fc2005-08-05 00:44:28 -0400935acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Len Brown4be44fc2005-08-05 00:44:28 -0400937 acpi_status status = AE_OK;
938 struct semaphore *sem = (struct semaphore *)handle;
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400939 long jiffies;
Len Brown4be44fc2005-08-05 00:44:28 -0400940 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400943 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400946 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Len Brown4be44fc2005-08-05 00:44:28 -0400948 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
949 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400951 if (timeout == ACPI_WAIT_FOREVER)
952 jiffies = MAX_SCHEDULE_TIMEOUT;
953 else
954 jiffies = msecs_to_jiffies(timeout);
955
956 ret = down_timeout(sem, jiffies);
957 if (ret)
958 status = AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -0400961 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400962 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -0400963 handle, units, timeout,
964 acpi_format_exception(status)));
965 } else {
966 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400967 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400968 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970
Patrick Mocheld550d982006-06-27 00:41:40 -0400971 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974/*
975 * TODO: Support for units > 1?
976 */
Len Brown4be44fc2005-08-05 00:44:28 -0400977acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Len Brown4be44fc2005-08-05 00:44:28 -0400979 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400982 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400985 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Len Brown4be44fc2005-08-05 00:44:28 -0400987 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
988 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 up(sem);
991
Patrick Mocheld550d982006-06-27 00:41:40 -0400992 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
Len Brown4be44fc2005-08-05 00:44:28 -0400994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -0400996u32 acpi_os_get_line(char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998
999#ifdef ENABLE_DEBUGGER
1000 if (acpi_in_debugger) {
1001 u32 chars;
1002
1003 kdb_read(buffer, sizeof(line_buf));
1004
1005 /* remove the CR kdb includes */
1006 chars = strlen(buffer) - 1;
1007 buffer[chars] = '\0';
1008 }
1009#endif
1010
1011 return 0;
1012}
Len Brown4be44fc2005-08-05 00:44:28 -04001013#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Len Brown4be44fc2005-08-05 00:44:28 -04001015acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Len Brown4be44fc2005-08-05 00:44:28 -04001017 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 case ACPI_SIGNAL_FATAL:
1019 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1020 break;
1021 case ACPI_SIGNAL_BREAKPOINT:
1022 /*
1023 * AML Breakpoint
1024 * ACPI spec. says to treat it as a NOP unless
1025 * you are debugging. So if/when we integrate
1026 * AML debugger into the kernel debugger its
1027 * hook will go here. But until then it is
1028 * not useful to print anything on breakpoints.
1029 */
1030 break;
1031 default:
1032 break;
1033 }
1034
1035 return AE_OK;
1036}
Len Brown4be44fc2005-08-05 00:44:28 -04001037
Len Brown4be44fc2005-08-05 00:44:28 -04001038static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -04001041 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (!str || !*str)
1044 return 0;
1045
1046 for (; count-- && str && *str; str++) {
1047 if (isalnum(*str) || *str == ' ' || *str == ':')
1048 *p++ = *str;
1049 else if (*str == '\'' || *str == '"')
1050 continue;
1051 else
1052 break;
1053 }
1054 *p = 0;
1055
1056 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
1060__setup("acpi_os_name=", acpi_os_name_setup);
1061
Lin Ming12d32062010-12-09 16:51:06 +08001062#define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
1063#define OSI_STRING_ENTRIES_MAX 16 /* arbitrary */
1064
1065struct osi_setup_entry {
1066 char string[OSI_STRING_LENGTH_MAX];
1067 bool enable;
1068};
1069
1070static struct osi_setup_entry __initdata osi_setup_entries[OSI_STRING_ENTRIES_MAX];
1071
Lin Mingd90aa922010-12-09 16:50:52 +08001072void __init acpi_osi_setup(char *str)
1073{
Lin Ming12d32062010-12-09 16:51:06 +08001074 struct osi_setup_entry *osi;
1075 bool enable = true;
1076 int i;
1077
Lin Mingd90aa922010-12-09 16:50:52 +08001078 if (!acpi_gbl_create_osi_method)
1079 return;
1080
1081 if (str == NULL || *str == '\0') {
1082 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1083 acpi_gbl_create_osi_method = FALSE;
Lin Ming12d32062010-12-09 16:51:06 +08001084 return;
1085 }
1086
1087 if (*str == '!') {
1088 str++;
1089 enable = false;
1090 }
1091
1092 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1093 osi = &osi_setup_entries[i];
1094 if (!strcmp(osi->string, str)) {
1095 osi->enable = enable;
1096 break;
1097 } else if (osi->string[0] == '\0') {
1098 osi->enable = enable;
1099 strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
1100 break;
1101 }
1102 }
Lin Mingd90aa922010-12-09 16:50:52 +08001103}
1104
Len Brownd4b7dc42008-01-23 20:50:56 -05001105static void __init set_osi_linux(unsigned int enable)
1106{
Lin Mingd90aa922010-12-09 16:50:52 +08001107 if (osi_linux.enable != enable)
Len Brownd4b7dc42008-01-23 20:50:56 -05001108 osi_linux.enable = enable;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001109
1110 if (osi_linux.enable)
1111 acpi_osi_setup("Linux");
1112 else
1113 acpi_osi_setup("!Linux");
1114
Len Brownd4b7dc42008-01-23 20:50:56 -05001115 return;
1116}
Len Brownf5076542007-05-30 00:10:38 -04001117
Len Brownd4b7dc42008-01-23 20:50:56 -05001118static void __init acpi_cmdline_osi_linux(unsigned int enable)
1119{
Lin Mingd90aa922010-12-09 16:50:52 +08001120 osi_linux.cmdline = 1; /* cmdline set the default and override DMI */
1121 osi_linux.dmi = 0;
Len Brownd4b7dc42008-01-23 20:50:56 -05001122 set_osi_linux(enable);
Len Brownf5076542007-05-30 00:10:38 -04001123
Len Brownd4b7dc42008-01-23 20:50:56 -05001124 return;
1125}
1126
1127void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1128{
Len Brownd4b7dc42008-01-23 20:50:56 -05001129 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1130
1131 if (enable == -1)
1132 return;
1133
Lin Mingd90aa922010-12-09 16:50:52 +08001134 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
Len Brownd4b7dc42008-01-23 20:50:56 -05001135 set_osi_linux(enable);
1136
Len Brownf5076542007-05-30 00:10:38 -04001137 return;
1138}
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140/*
Len Brownae00d812007-05-29 18:43:33 -04001141 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1142 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -04001144 * string starting with '!' disables that string
1145 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 */
Lin Mingb0ed7a92010-08-06 09:35:51 +08001147static void __init acpi_osi_setup_late(void)
1148{
Lin Ming12d32062010-12-09 16:51:06 +08001149 struct osi_setup_entry *osi;
1150 char *str;
1151 int i;
Lin Mingd90aa922010-12-09 16:50:52 +08001152 acpi_status status;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001153
Lin Ming12d32062010-12-09 16:51:06 +08001154 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1155 osi = &osi_setup_entries[i];
1156 str = osi->string;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001157
Lin Ming12d32062010-12-09 16:51:06 +08001158 if (*str == '\0')
1159 break;
1160 if (osi->enable) {
1161 status = acpi_install_interface(str);
Lin Mingd90aa922010-12-09 16:50:52 +08001162
Lin Ming12d32062010-12-09 16:51:06 +08001163 if (ACPI_SUCCESS(status))
1164 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1165 } else {
1166 status = acpi_remove_interface(str);
Lin Mingd90aa922010-12-09 16:50:52 +08001167
Lin Ming12d32062010-12-09 16:51:06 +08001168 if (ACPI_SUCCESS(status))
1169 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1170 }
Lin Mingb0ed7a92010-08-06 09:35:51 +08001171 }
1172}
1173
Lin Mingd90aa922010-12-09 16:50:52 +08001174static int __init osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Lin Mingd90aa922010-12-09 16:50:52 +08001176 if (str && !strcmp("Linux", str))
1177 acpi_cmdline_osi_linux(1);
1178 else if (str && !strcmp("!Linux", str))
1179 acpi_cmdline_osi_linux(0);
1180 else
1181 acpi_osi_setup(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 return 1;
1184}
1185
Lin Mingd90aa922010-12-09 16:50:52 +08001186__setup("acpi_osi=", osi_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188/* enable serialization to combat AE_ALREADY_EXISTS errors */
Len Brown4be44fc2005-08-05 00:44:28 -04001189static int __init acpi_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 printk(KERN_INFO PREFIX "serialize enabled\n");
1192
1193 acpi_gbl_all_methods_serialized = TRUE;
1194
1195 return 1;
1196}
1197
1198__setup("acpi_serialize", acpi_serialize_setup);
1199
Thomas Renningerdf92e692008-02-04 23:31:22 -08001200/* Check of resource interference between native drivers and ACPI
1201 * OperationRegions (SystemIO and System Memory only).
1202 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1203 * in arbitrary AML code and can interfere with legacy drivers.
1204 * acpi_enforce_resources= can be set to:
1205 *
Luca Tettamanti7e905602009-03-30 00:01:27 +02001206 * - strict (default) (2)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001207 * -> further driver trying to access the resources will not load
Luca Tettamanti7e905602009-03-30 00:01:27 +02001208 * - lax (1)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001209 * -> further driver trying to access the resources will load, but you
1210 * get a system message that something might go wrong...
1211 *
1212 * - no (0)
1213 * -> ACPI Operation Region resources will not be registered
1214 *
1215 */
1216#define ENFORCE_RESOURCES_STRICT 2
1217#define ENFORCE_RESOURCES_LAX 1
1218#define ENFORCE_RESOURCES_NO 0
1219
Luca Tettamanti7e905602009-03-30 00:01:27 +02001220static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001221
1222static int __init acpi_enforce_resources_setup(char *str)
1223{
1224 if (str == NULL || *str == '\0')
1225 return 0;
1226
1227 if (!strcmp("strict", str))
1228 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1229 else if (!strcmp("lax", str))
1230 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1231 else if (!strcmp("no", str))
1232 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1233
1234 return 1;
1235}
1236
1237__setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1238
1239/* Check for resource conflicts between ACPI OperationRegions and native
1240 * drivers */
Jean Delvare876fba42009-11-11 15:22:15 +01001241int acpi_check_resource_conflict(const struct resource *res)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001242{
1243 struct acpi_res_list *res_list_elem;
Thomas Renninger106d1a02010-12-20 12:11:45 +01001244 int ioport = 0, clash = 0;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001245
1246 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1247 return 0;
1248 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1249 return 0;
1250
1251 ioport = res->flags & IORESOURCE_IO;
1252
1253 spin_lock(&acpi_res_lock);
1254 list_for_each_entry(res_list_elem, &resource_list_head,
1255 resource_list) {
1256 if (ioport && (res_list_elem->resource_type
1257 != ACPI_ADR_SPACE_SYSTEM_IO))
1258 continue;
1259 if (!ioport && (res_list_elem->resource_type
1260 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1261 continue;
1262
1263 if (res->end < res_list_elem->start
1264 || res_list_elem->end < res->start)
1265 continue;
1266 clash = 1;
1267 break;
1268 }
1269 spin_unlock(&acpi_res_lock);
1270
1271 if (clash) {
1272 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
Chase Douglas1638bca2010-03-22 15:08:09 -04001273 printk(KERN_WARNING "ACPI: resource %s %pR"
Thomas Renninger106d1a02010-12-20 12:11:45 +01001274 " conflicts with ACPI region %s "
1275 "[%s 0x%zx-0x%zx]\n",
Chase Douglas1638bca2010-03-22 15:08:09 -04001276 res->name, res, res_list_elem->name,
Thomas Renninger106d1a02010-12-20 12:11:45 +01001277 (res_list_elem->resource_type ==
1278 ACPI_ADR_SPACE_SYSTEM_IO) ? "io" : "mem",
1279 (size_t) res_list_elem->start,
1280 (size_t) res_list_elem->end);
Jean Delvare14f03342009-09-08 15:31:46 +02001281 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1282 printk(KERN_NOTICE "ACPI: This conflict may"
1283 " cause random problems and system"
1284 " instability\n");
1285 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1286 " for this device, you should use it instead of"
1287 " the native driver\n");
Thomas Renningerdf92e692008-02-04 23:31:22 -08001288 }
1289 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1290 return -EBUSY;
1291 }
1292 return 0;
1293}
Thomas Renninger443dea72008-02-04 23:31:23 -08001294EXPORT_SYMBOL(acpi_check_resource_conflict);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001295
1296int acpi_check_region(resource_size_t start, resource_size_t n,
1297 const char *name)
1298{
1299 struct resource res = {
1300 .start = start,
1301 .end = start + n - 1,
1302 .name = name,
1303 .flags = IORESOURCE_IO,
1304 };
1305
1306 return acpi_check_resource_conflict(&res);
1307}
1308EXPORT_SYMBOL(acpi_check_region);
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310/*
Jean Delvare70dd6be2010-05-27 19:58:37 +02001311 * Let drivers know whether the resource checks are effective
1312 */
1313int acpi_resources_are_enforced(void)
1314{
1315 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1316}
1317EXPORT_SYMBOL(acpi_resources_are_enforced);
1318
1319/*
Robert Moore73459f72005-06-24 00:00:00 -04001320 * Acquire a spinlock.
1321 *
1322 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001323 */
1324
Bob Moore967440e32006-06-23 17:04:00 -04001325acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001326{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001327 acpi_cpu_flags flags;
Bob Moore967440e32006-06-23 17:04:00 -04001328 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001329 return flags;
1330}
1331
1332/*
1333 * Release a spinlock. See above.
1334 */
1335
Bob Moore967440e32006-06-23 17:04:00 -04001336void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001337{
Bob Moore967440e32006-06-23 17:04:00 -04001338 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001339}
1340
Robert Moore73459f72005-06-24 00:00:00 -04001341#ifndef ACPI_USE_LOCAL_CACHE
1342
1343/*******************************************************************************
1344 *
1345 * FUNCTION: acpi_os_create_cache
1346 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001347 * PARAMETERS: name - Ascii name for the cache
1348 * size - Size of each cached object
1349 * depth - Maximum depth of the cache (in objects) <ignored>
1350 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001351 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001352 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001353 *
1354 * DESCRIPTION: Create a cache object
1355 *
1356 ******************************************************************************/
1357
1358acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001359acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001360{
Paul Mundt20c2df82007-07-20 10:11:58 +09001361 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001362 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001363 return AE_ERROR;
1364 else
1365 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001366}
1367
1368/*******************************************************************************
1369 *
1370 * FUNCTION: acpi_os_purge_cache
1371 *
1372 * PARAMETERS: Cache - Handle to cache object
1373 *
1374 * RETURN: Status
1375 *
1376 * DESCRIPTION: Free all objects within the requested cache.
1377 *
1378 ******************************************************************************/
1379
Len Brown4be44fc2005-08-05 00:44:28 -04001380acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001381{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001382 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001383 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001384}
1385
1386/*******************************************************************************
1387 *
1388 * FUNCTION: acpi_os_delete_cache
1389 *
1390 * PARAMETERS: Cache - Handle to cache object
1391 *
1392 * RETURN: Status
1393 *
1394 * DESCRIPTION: Free all objects within the requested cache and delete the
1395 * cache object.
1396 *
1397 ******************************************************************************/
1398
Len Brown4be44fc2005-08-05 00:44:28 -04001399acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001400{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001401 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001402 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001403}
1404
1405/*******************************************************************************
1406 *
1407 * FUNCTION: acpi_os_release_object
1408 *
1409 * PARAMETERS: Cache - Handle to cache object
1410 * Object - The object to be released
1411 *
1412 * RETURN: None
1413 *
1414 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1415 * the object is deleted.
1416 *
1417 ******************************************************************************/
1418
Len Brown4be44fc2005-08-05 00:44:28 -04001419acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001420{
Len Brown4be44fc2005-08-05 00:44:28 -04001421 kmem_cache_free(cache, object);
1422 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001423}
1424
Lin Minga5fe1a02009-08-13 10:43:27 +08001425static inline int acpi_res_list_add(struct acpi_res_list *res)
1426{
1427 struct acpi_res_list *res_list_elem;
1428
1429 list_for_each_entry(res_list_elem, &resource_list_head,
1430 resource_list) {
1431
1432 if (res->resource_type == res_list_elem->resource_type &&
1433 res->start == res_list_elem->start &&
1434 res->end == res_list_elem->end) {
1435
1436 /*
1437 * The Region(addr,len) already exist in the list,
1438 * just increase the count
1439 */
1440
1441 res_list_elem->count++;
1442 return 0;
1443 }
1444 }
1445
1446 res->count = 1;
1447 list_add(&res->resource_list, &resource_list_head);
1448 return 1;
1449}
1450
1451static inline void acpi_res_list_del(struct acpi_res_list *res)
1452{
1453 struct acpi_res_list *res_list_elem;
1454
1455 list_for_each_entry(res_list_elem, &resource_list_head,
1456 resource_list) {
1457
1458 if (res->resource_type == res_list_elem->resource_type &&
1459 res->start == res_list_elem->start &&
1460 res->end == res_list_elem->end) {
1461
1462 /*
1463 * If the res count is decreased to 0,
1464 * remove and free it
1465 */
1466
1467 if (--res_list_elem->count == 0) {
1468 list_del(&res_list_elem->resource_list);
1469 kfree(res_list_elem);
1470 }
1471 return;
1472 }
1473 }
1474}
1475
1476acpi_status
1477acpi_os_invalidate_address(
1478 u8 space_id,
1479 acpi_physical_address address,
1480 acpi_size length)
1481{
1482 struct acpi_res_list res;
1483
1484 switch (space_id) {
1485 case ACPI_ADR_SPACE_SYSTEM_IO:
1486 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001487 /* Only interference checks against SystemIO and SystemMemory
Lin Minga5fe1a02009-08-13 10:43:27 +08001488 are needed */
1489 res.start = address;
1490 res.end = address + length - 1;
1491 res.resource_type = space_id;
1492 spin_lock(&acpi_res_lock);
1493 acpi_res_list_del(&res);
1494 spin_unlock(&acpi_res_lock);
1495 break;
1496 case ACPI_ADR_SPACE_PCI_CONFIG:
1497 case ACPI_ADR_SPACE_EC:
1498 case ACPI_ADR_SPACE_SMBUS:
1499 case ACPI_ADR_SPACE_CMOS:
1500 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1501 case ACPI_ADR_SPACE_DATA_TABLE:
1502 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1503 break;
1504 }
1505 return AE_OK;
1506}
1507
Bob Mooreb229cf92006-04-21 17:15:00 -04001508/******************************************************************************
1509 *
1510 * FUNCTION: acpi_os_validate_address
1511 *
1512 * PARAMETERS: space_id - ACPI space ID
1513 * address - Physical address
1514 * length - Address length
1515 *
1516 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1517 * should return AE_AML_ILLEGAL_ADDRESS.
1518 *
1519 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1520 * the addresses accessed by AML operation regions.
1521 *
1522 *****************************************************************************/
1523
1524acpi_status
1525acpi_os_validate_address (
1526 u8 space_id,
1527 acpi_physical_address address,
Thomas Renningerdf92e692008-02-04 23:31:22 -08001528 acpi_size length,
1529 char *name)
Bob Mooreb229cf92006-04-21 17:15:00 -04001530{
Thomas Renningerdf92e692008-02-04 23:31:22 -08001531 struct acpi_res_list *res;
Lin Minga5fe1a02009-08-13 10:43:27 +08001532 int added;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001533 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1534 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001535
Thomas Renningerdf92e692008-02-04 23:31:22 -08001536 switch (space_id) {
1537 case ACPI_ADR_SPACE_SYSTEM_IO:
1538 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001539 /* Only interference checks against SystemIO and SystemMemory
Thomas Renningerdf92e692008-02-04 23:31:22 -08001540 are needed */
1541 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1542 if (!res)
1543 return AE_OK;
1544 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1545 strlcpy(res->name, name, 5);
1546 res->start = address;
1547 res->end = address + length - 1;
1548 res->resource_type = space_id;
1549 spin_lock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001550 added = acpi_res_list_add(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001551 spin_unlock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001552 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1553 "name: %s\n", added ? "Added" : "Already exist",
1554 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001555 ? "SystemIO" : "System Memory",
1556 (unsigned long long)res->start,
1557 (unsigned long long)res->end,
1558 res->name);
Lin Minga5fe1a02009-08-13 10:43:27 +08001559 if (!added)
1560 kfree(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001561 break;
1562 case ACPI_ADR_SPACE_PCI_CONFIG:
1563 case ACPI_ADR_SPACE_EC:
1564 case ACPI_ADR_SPACE_SMBUS:
1565 case ACPI_ADR_SPACE_CMOS:
1566 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1567 case ACPI_ADR_SPACE_DATA_TABLE:
1568 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1569 break;
1570 }
1571 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001572}
Robert Moore73459f72005-06-24 00:00:00 -04001573#endif
Myron Stowed362eda2010-10-21 14:24:04 -06001574
1575acpi_status __init acpi_os_initialize(void)
1576{
1577 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1578 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1579 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1580 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1581
1582 return AE_OK;
1583}
1584
Zhang Rui32d47ee2010-12-08 10:40:36 +08001585acpi_status __init acpi_os_initialize1(void)
Myron Stowed362eda2010-10-21 14:24:04 -06001586{
1587 kacpid_wq = create_workqueue("kacpid");
1588 kacpi_notify_wq = create_workqueue("kacpi_notify");
1589 kacpi_hotplug_wq = create_workqueue("kacpi_hotplug");
1590 BUG_ON(!kacpid_wq);
1591 BUG_ON(!kacpi_notify_wq);
1592 BUG_ON(!kacpi_hotplug_wq);
Len Brown1bd64d42010-10-26 14:50:56 -04001593 acpi_install_interface_handler(acpi_osi_handler);
1594 acpi_osi_setup_late();
Myron Stowed362eda2010-10-21 14:24:04 -06001595 return AE_OK;
1596}
1597
1598acpi_status acpi_os_terminate(void)
1599{
1600 if (acpi_irq_handler) {
1601 acpi_os_remove_interrupt_handler(acpi_irq_irq,
1602 acpi_irq_handler);
1603 }
1604
1605 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1606 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1607 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1608 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1609
1610 destroy_workqueue(kacpid_wq);
1611 destroy_workqueue(kacpi_notify_wq);
1612 destroy_workqueue(kacpi_hotplug_wq);
1613
1614 return AE_OK;
1615}