blob: 2e285cdbefb1e6bdd075cf8ba3073c3c7cf10976 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static acpi_osd_handler acpi_irq_handler;
80static void *acpi_irq_context;
81static struct workqueue_struct *kacpid_wq;
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -040082static struct workqueue_struct *kacpi_notify_wq;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -040083struct workqueue_struct *kacpi_hotplug_wq;
84EXPORT_SYMBOL(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;
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100108 unsigned long refcount;
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 Brown89976212011-08-02 00:45:48 -0400159 printk_once(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
Myron Stowebc9ffce2011-11-07 16:23:27 -0700169static void __init acpi_request_region (struct acpi_generic_address *gas,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700170 unsigned int length, char *desc)
171{
Myron Stowebc9ffce2011-11-07 16:23:27 -0700172 u64 addr;
173
174 /* Handle possible alignment issues */
175 memcpy(&addr, &gas->address, sizeof(addr));
176 if (!addr || !length)
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700177 return;
178
Andi Kleencfa806f2010-07-20 15:18:36 -0700179 /* Resources are never freed */
Myron Stowebc9ffce2011-11-07 16:23:27 -0700180 if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
181 request_region(addr, length, desc);
182 else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
183 request_mem_region(addr, length, desc);
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700184}
185
186static int __init acpi_reserve_resources(void)
187{
Len Browneee3c852007-02-03 01:38:16 -0500188 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700189 "ACPI PM1a_EVT_BLK");
190
Len Browneee3c852007-02-03 01:38:16 -0500191 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700192 "ACPI PM1b_EVT_BLK");
193
Len Browneee3c852007-02-03 01:38:16 -0500194 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700195 "ACPI PM1a_CNT_BLK");
196
Len Browneee3c852007-02-03 01:38:16 -0500197 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700198 "ACPI PM1b_CNT_BLK");
199
Len Browneee3c852007-02-03 01:38:16 -0500200 if (acpi_gbl_FADT.pm_timer_length == 4)
201 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700202
Len Browneee3c852007-02-03 01:38:16 -0500203 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700204 "ACPI PM2_CNT_BLK");
205
206 /* Length of GPE blocks must be a non-negative multiple of 2 */
207
Len Browneee3c852007-02-03 01:38:16 -0500208 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
209 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
210 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700211
Len Browneee3c852007-02-03 01:38:16 -0500212 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
213 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
214 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700215
216 return 0;
217}
218device_initcall(acpi_reserve_resources);
219
Len Brown4be44fc2005-08-05 00:44:28 -0400220void acpi_os_printf(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 va_list args;
223 va_start(args, fmt);
224 acpi_os_vprintf(fmt, args);
225 va_end(args);
226}
Len Brown4be44fc2005-08-05 00:44:28 -0400227
Len Brown4be44fc2005-08-05 00:44:28 -0400228void acpi_os_vprintf(const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 static char buffer[512];
Len Brown4be44fc2005-08-05 00:44:28 -0400231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 vsprintf(buffer, fmt, args);
233
234#ifdef ENABLE_DEBUGGER
235 if (acpi_in_debugger) {
236 kdb_printf("%s", buffer);
237 } else {
Frank Seidel4d939152009-02-04 17:03:07 +0100238 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240#else
Frank Seidel4d939152009-02-04 17:03:07 +0100241 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#endif
243}
244
Takao Indoh4996c022011-07-14 18:05:21 -0400245#ifdef CONFIG_KEXEC
246static unsigned long acpi_rsdp;
247static int __init setup_acpi_rsdp(char *arg)
248{
249 acpi_rsdp = simple_strtoul(arg, NULL, 16);
250 return 0;
251}
252early_param("acpi_rsdp", setup_acpi_rsdp);
253#endif
254
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300255acpi_physical_address __init acpi_os_get_root_pointer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Takao Indoh4996c022011-07-14 18:05:21 -0400257#ifdef CONFIG_KEXEC
258 if (acpi_rsdp)
259 return acpi_rsdp;
260#endif
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (efi_enabled) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800263 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300264 return efi.acpi20;
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800265 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300266 return efi.acpi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400268 printk(KERN_ERR PREFIX
269 "System description tables not found\n");
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
Len Brown239665a2007-11-23 20:08:02 -0500272 } else {
273 acpi_physical_address pa = 0;
274
275 acpi_find_root_pointer(&pa);
276 return pa;
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600280/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe4a3cba52010-10-21 14:24:14 -0600281static struct acpi_ioremap *
282acpi_map_lookup(acpi_physical_address phys, acpi_size size)
Myron Stowe620242a2010-10-21 14:23:53 -0600283{
284 struct acpi_ioremap *map;
285
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600286 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe620242a2010-10-21 14:23:53 -0600287 if (map->phys <= phys &&
288 phys + size <= map->phys + map->size)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600289 return map;
290
291 return NULL;
292}
293
294/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
295static void __iomem *
296acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
297{
298 struct acpi_ioremap *map;
299
300 map = acpi_map_lookup(phys, size);
301 if (map)
302 return map->virt + (phys - map->phys);
Myron Stowe620242a2010-10-21 14:23:53 -0600303
304 return NULL;
305}
306
Rafael J. Wysocki13606a22011-02-08 23:38:25 +0100307void __iomem *acpi_os_get_iomem(acpi_physical_address phys, unsigned int size)
308{
309 struct acpi_ioremap *map;
310 void __iomem *virt = NULL;
311
312 mutex_lock(&acpi_ioremap_lock);
313 map = acpi_map_lookup(phys, size);
314 if (map) {
315 virt = map->virt + (phys - map->phys);
316 map->refcount++;
317 }
318 mutex_unlock(&acpi_ioremap_lock);
319 return virt;
320}
321EXPORT_SYMBOL_GPL(acpi_os_get_iomem);
322
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600323/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe620242a2010-10-21 14:23:53 -0600324static struct acpi_ioremap *
325acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
326{
327 struct acpi_ioremap *map;
328
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600329 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600330 if (map->virt <= virt &&
331 virt + size <= map->virt + map->size)
Myron Stowe620242a2010-10-21 14:23:53 -0600332 return map;
333
334 return NULL;
335}
336
Jan Beulich2fdf0742007-12-13 08:33:59 +0000337void __iomem *__init_refok
338acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100340 struct acpi_ioremap *map;
Myron Stowe620242a2010-10-21 14:23:53 -0600341 void __iomem *virt;
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100342 acpi_physical_address pg_off;
343 acpi_size pg_sz;
Myron Stowe620242a2010-10-21 14:23:53 -0600344
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800345 if (phys > ULONG_MAX) {
346 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
Randy Dunlap70c08462007-02-13 16:11:36 -0800347 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Myron Stowe620242a2010-10-21 14:23:53 -0600349
350 if (!acpi_gbl_permanent_mmap)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300351 return __acpi_map_table((unsigned long)phys, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600352
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100353 mutex_lock(&acpi_ioremap_lock);
354 /* Check if there's a suitable mapping already. */
355 map = acpi_map_lookup(phys, size);
356 if (map) {
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100357 map->refcount++;
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100358 goto out;
359 }
360
Myron Stowe620242a2010-10-21 14:23:53 -0600361 map = kzalloc(sizeof(*map), GFP_KERNEL);
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100362 if (!map) {
363 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600364 return NULL;
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100365 }
Myron Stowe620242a2010-10-21 14:23:53 -0600366
Myron Stowe4a3cba52010-10-21 14:24:14 -0600367 pg_off = round_down(phys, PAGE_SIZE);
368 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100369 virt = acpi_os_ioremap(pg_off, pg_sz);
Myron Stowe620242a2010-10-21 14:23:53 -0600370 if (!virt) {
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100371 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600372 kfree(map);
373 return NULL;
374 }
375
376 INIT_LIST_HEAD(&map->list);
377 map->virt = virt;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600378 map->phys = pg_off;
379 map->size = pg_sz;
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100380 map->refcount = 1;
Myron Stowe620242a2010-10-21 14:23:53 -0600381
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600382 list_add_tail_rcu(&map->list, &acpi_ioremaps);
Myron Stowe620242a2010-10-21 14:23:53 -0600383
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100384 out:
385 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600386 return map->virt + (phys - map->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800388EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100390static void acpi_os_drop_map_ref(struct acpi_ioremap *map)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600391{
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100392 if (!--map->refcount)
393 list_del_rcu(&map->list);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600394}
Myron Stowe4a3cba52010-10-21 14:24:14 -0600395
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100396static void acpi_os_map_cleanup(struct acpi_ioremap *map)
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100397{
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100398 if (!map->refcount) {
399 synchronize_rcu();
400 iounmap(map->virt);
401 kfree(map);
402 }
Myron Stowe4a3cba52010-10-21 14:24:14 -0600403}
404
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800405void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Myron Stowe620242a2010-10-21 14:23:53 -0600407 struct acpi_ioremap *map;
Myron Stowe620242a2010-10-21 14:23:53 -0600408
409 if (!acpi_gbl_permanent_mmap) {
Yinghai Lu7d972772009-02-07 15:39:41 -0800410 __acpi_unmap_table(virt, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600411 return;
412 }
413
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100414 mutex_lock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600415 map = acpi_map_lookup_virt(virt, size);
416 if (!map) {
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100417 mutex_unlock(&acpi_ioremap_lock);
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100418 WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
Myron Stowe620242a2010-10-21 14:23:53 -0600419 return;
420 }
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100421 acpi_os_drop_map_ref(map);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100422 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600423
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100424 acpi_os_map_cleanup(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800426EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800428void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Yinghai Lu7d972772009-02-07 15:39:41 -0800429{
430 if (!acpi_gbl_permanent_mmap)
431 __acpi_unmap_table(virt, size);
432}
433
Myron Stowebc9ffce2011-11-07 16:23:27 -0700434static int acpi_os_map_generic_address(struct acpi_generic_address *gas)
Myron Stowe29718522010-10-21 14:23:59 -0600435{
Myron Stowebc9ffce2011-11-07 16:23:27 -0700436 u64 addr;
Myron Stowe29718522010-10-21 14:23:59 -0600437 void __iomem *virt;
438
Myron Stowebc9ffce2011-11-07 16:23:27 -0700439 if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
Myron Stowe29718522010-10-21 14:23:59 -0600440 return 0;
441
Myron Stowebc9ffce2011-11-07 16:23:27 -0700442 /* Handle possible alignment issues */
443 memcpy(&addr, &gas->address, sizeof(addr));
444 if (!addr || !gas->bit_width)
Myron Stowe29718522010-10-21 14:23:59 -0600445 return -EINVAL;
446
Myron Stowebc9ffce2011-11-07 16:23:27 -0700447 virt = acpi_os_map_memory(addr, gas->bit_width / 8);
Myron Stowe29718522010-10-21 14:23:59 -0600448 if (!virt)
449 return -EIO;
450
451 return 0;
452}
Myron Stowe29718522010-10-21 14:23:59 -0600453
Myron Stowebc9ffce2011-11-07 16:23:27 -0700454static void acpi_os_unmap_generic_address(struct acpi_generic_address *gas)
Myron Stowe29718522010-10-21 14:23:59 -0600455{
Myron Stowebc9ffce2011-11-07 16:23:27 -0700456 u64 addr;
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100457 struct acpi_ioremap *map;
Myron Stowe29718522010-10-21 14:23:59 -0600458
Myron Stowebc9ffce2011-11-07 16:23:27 -0700459 if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
Myron Stowe29718522010-10-21 14:23:59 -0600460 return;
461
Myron Stowebc9ffce2011-11-07 16:23:27 -0700462 /* Handle possible alignment issues */
463 memcpy(&addr, &gas->address, sizeof(addr));
464 if (!addr || !gas->bit_width)
Myron Stowe29718522010-10-21 14:23:59 -0600465 return;
466
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100467 mutex_lock(&acpi_ioremap_lock);
Myron Stowebc9ffce2011-11-07 16:23:27 -0700468 map = acpi_map_lookup(addr, gas->bit_width / 8);
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100469 if (!map) {
470 mutex_unlock(&acpi_ioremap_lock);
471 return;
472 }
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100473 acpi_os_drop_map_ref(map);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100474 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe29718522010-10-21 14:23:59 -0600475
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100476 acpi_os_map_cleanup(map);
Myron Stowe29718522010-10-21 14:23:59 -0600477}
Myron Stowe29718522010-10-21 14:23:59 -0600478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479#ifdef ACPI_FUTURE_USAGE
480acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400481acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Len Brown4be44fc2005-08-05 00:44:28 -0400483 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return AE_BAD_PARAMETER;
485
486 *phys = virt_to_phys(virt);
487
488 return AE_OK;
489}
490#endif
491
492#define ACPI_MAX_OVERRIDE_LEN 100
493
494static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
495
496acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400497acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
498 acpi_string * new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 if (!init_val || !new_val)
501 return AE_BAD_PARAMETER;
502
503 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400504 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400506 acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 *new_val = acpi_os_name;
508 }
509
510 return AE_OK;
511}
512
513acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400514acpi_os_table_override(struct acpi_table_header * existing_table,
515 struct acpi_table_header ** new_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 if (!existing_table || !new_table)
518 return AE_BAD_PARAMETER;
519
Markus Gaugusch71fc47a2008-02-05 00:04:06 +0100520 *new_table = NULL;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522#ifdef CONFIG_ACPI_CUSTOM_DSDT
523 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400524 *new_table = (struct acpi_table_header *)AmlCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525#endif
Éric Piel6ed31e92008-02-05 00:04:50 +0100526 if (*new_table != NULL) {
527 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
528 "this is unsafe: tainting kernel\n",
529 existing_table->signature,
530 existing_table->oem_table_id);
531 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return AE_OK;
534}
535
David Howells7d12e782006-10-05 14:55:46 +0100536static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Len Brown5229e872008-02-06 01:26:55 -0500538 u32 handled;
539
540 handled = (*acpi_irq_handler) (acpi_irq_context);
541
542 if (handled) {
543 acpi_irq_handled++;
544 return IRQ_HANDLED;
Len Brown88bea182009-04-21 00:35:47 -0400545 } else {
546 acpi_irq_not_handled++;
Len Brown5229e872008-02-06 01:26:55 -0500547 return IRQ_NONE;
Len Brown88bea182009-04-21 00:35:47 -0400548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400552acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
553 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 unsigned int irq;
556
Len Brown5229e872008-02-06 01:26:55 -0500557 acpi_irq_stats_init();
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /*
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100560 * ACPI interrupts different from the SCI in our copy of the FADT are
561 * not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100563 if (gsi != acpi_gbl_FADT.sci_interrupt)
564 return AE_BAD_PARAMETER;
565
566 if (acpi_irq_handler)
567 return AE_ALREADY_ACQUIRED;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
570 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
571 gsi);
572 return AE_OK;
573 }
574
575 acpi_irq_handler = handler;
576 acpi_irq_context = context;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700577 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100579 acpi_irq_handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return AE_NOT_ACQUIRED;
581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 return AE_OK;
584}
585
Len Brown4be44fc2005-08-05 00:44:28 -0400586acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100588 if (irq != acpi_gbl_FADT.sci_interrupt)
589 return AE_BAD_PARAMETER;
590
591 free_irq(irq, acpi_irq);
592 acpi_irq_handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 return AE_OK;
595}
596
597/*
598 * Running in interpreter thread context, safe to sleep
599 */
600
Lin Ming439913f2010-01-28 10:53:19 +0800601void acpi_os_sleep(u64 ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800603 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
Len Brown4be44fc2005-08-05 00:44:28 -0400605
Len Brown4be44fc2005-08-05 00:44:28 -0400606void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 while (us) {
609 u32 delay = 1000;
610
611 if (delay > us)
612 delay = us;
613 udelay(delay);
614 touch_nmi_watchdog();
615 us -= delay;
616 }
617}
Len Brown4be44fc2005-08-05 00:44:28 -0400618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*
620 * Support ACPI 3.0 AML Timer operand
621 * Returns 64-bit free-running, monotonically increasing timer
622 * with 100ns granularity
623 */
Len Brown4be44fc2005-08-05 00:44:28 -0400624u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 static u64 t;
627
628#ifdef CONFIG_HPET
629 /* TBD: use HPET if available */
630#endif
631
632#ifdef CONFIG_X86_PM_TIMER
633 /* TBD: default to PM timer if HPET was not available */
634#endif
635 if (!t)
636 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
637
638 return ++t;
639}
640
Len Brown4be44fc2005-08-05 00:44:28 -0400641acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 u32 dummy;
644
645 if (!value)
646 value = &dummy;
647
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800648 *value = 0;
649 if (width <= 8) {
Len Brown4be44fc2005-08-05 00:44:28 -0400650 *(u8 *) value = inb(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800651 } else if (width <= 16) {
Len Brown4be44fc2005-08-05 00:44:28 -0400652 *(u16 *) value = inw(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800653 } else if (width <= 32) {
Len Brown4be44fc2005-08-05 00:44:28 -0400654 *(u32 *) value = inl(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800655 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 BUG();
657 }
658
659 return AE_OK;
660}
Len Brown4be44fc2005-08-05 00:44:28 -0400661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662EXPORT_SYMBOL(acpi_os_read_port);
663
Len Brown4be44fc2005-08-05 00:44:28 -0400664acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800666 if (width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 outb(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800668 } else if (width <= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 outw(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800670 } else if (width <= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 outl(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800672 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 BUG();
674 }
675
676 return AE_OK;
677}
Len Brown4be44fc2005-08-05 00:44:28 -0400678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679EXPORT_SYMBOL(acpi_os_write_port);
680
681acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400682acpi_os_read_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;
687 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600689 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600690 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600691 if (!virt_addr) {
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100692 rcu_read_unlock();
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100693 virt_addr = acpi_os_ioremap(phys_addr, size);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100694 if (!virt_addr)
695 return AE_BAD_ADDRESS;
696 unmap = true;
Myron Stowe620242a2010-10-21 14:23:53 -0600697 }
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (!value)
700 value = &dummy;
701
702 switch (width) {
703 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400704 *(u8 *) value = readb(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 break;
706 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400707 *(u16 *) value = readw(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 break;
709 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400710 *(u32 *) value = readl(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 break;
712 default:
713 BUG();
714 }
715
Myron Stowe620242a2010-10-21 14:23:53 -0600716 if (unmap)
717 iounmap(virt_addr);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100718 else
719 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 return AE_OK;
722}
723
724acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400725acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Len Brown4be44fc2005-08-05 00:44:28 -0400727 void __iomem *virt_addr;
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100728 unsigned int size = width / 8;
729 bool unmap = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600731 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600732 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600733 if (!virt_addr) {
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100734 rcu_read_unlock();
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100735 virt_addr = acpi_os_ioremap(phys_addr, size);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100736 if (!virt_addr)
737 return AE_BAD_ADDRESS;
738 unmap = true;
Myron Stowe620242a2010-10-21 14:23:53 -0600739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 switch (width) {
742 case 8:
743 writeb(value, virt_addr);
744 break;
745 case 16:
746 writew(value, virt_addr);
747 break;
748 case 32:
749 writel(value, virt_addr);
750 break;
751 default:
752 BUG();
753 }
754
Myron Stowe620242a2010-10-21 14:23:53 -0600755 if (unmap)
756 iounmap(virt_addr);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100757 else
758 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 return AE_OK;
761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400764acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Bob Moorec5f02312010-08-06 08:57:53 +0800765 u64 *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 int result, size;
Bob Moorec5f02312010-08-06 08:57:53 +0800768 u32 value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 if (!value)
771 return AE_BAD_PARAMETER;
772
773 switch (width) {
774 case 8:
775 size = 1;
776 break;
777 case 16:
778 size = 2;
779 break;
780 case 32:
781 size = 4;
782 break;
783 default:
784 return AE_ERROR;
785 }
786
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500787 result = raw_pci_read(pci_id->segment, pci_id->bus,
788 PCI_DEVFN(pci_id->device, pci_id->function),
Bob Moorec5f02312010-08-06 08:57:53 +0800789 reg, size, &value32);
790 *value = value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 return (result ? AE_ERROR : AE_OK);
793}
Len Brown4be44fc2005-08-05 00:44:28 -0400794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400796acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Lin Ming439913f2010-01-28 10:53:19 +0800797 u64 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 int result, size;
800
801 switch (width) {
802 case 8:
803 size = 1;
804 break;
805 case 16:
806 size = 2;
807 break;
808 case 32:
809 size = 4;
810 break;
811 default:
812 return AE_ERROR;
813 }
814
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500815 result = raw_pci_write(pci_id->segment, pci_id->bus,
816 PCI_DEVFN(pci_id->device, pci_id->function),
817 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 return (result ? AE_ERROR : AE_OK);
820}
821
David Howells65f27f32006-11-22 14:55:48 +0000822static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
David Howells65f27f32006-11-22 14:55:48 +0000824 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400825
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000826 if (dpc->wait)
827 acpi_os_wait_events_complete(NULL);
Zhang Rui19cd8472008-08-28 10:05:06 +0800828
829 dpc->function(dpc->context);
830 kfree(dpc);
Zhang Rui19cd8472008-08-28 10:05:06 +0800831}
832
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400833/*******************************************************************************
834 *
835 * FUNCTION: acpi_os_execute
836 *
837 * PARAMETERS: Type - Type of the callback
838 * Function - Function to be executed
839 * Context - Function parameters
840 *
841 * RETURN: Status
842 *
843 * DESCRIPTION: Depending on type, either queues function for deferred execution or
844 * immediately executes function on a separate thread.
845 *
846 ******************************************************************************/
847
Zhang Rui19cd8472008-08-28 10:05:06 +0800848static acpi_status __acpi_os_execute(acpi_execute_type type,
849 acpi_osd_exec_callback function, void *context, int hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Len Brown4be44fc2005-08-05 00:44:28 -0400851 acpi_status status = AE_OK;
852 struct acpi_os_dpc *dpc;
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300853 struct workqueue_struct *queue;
Zhang Rui19cd8472008-08-28 10:05:06 +0800854 int ret;
Len Brown72945b22006-07-12 22:46:42 -0400855 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
856 "Scheduling function [%p(%p)] for deferred execution.\n",
857 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 /*
860 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +0000861 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 * way that allows us to also free its memory inside the callee.
863 * Because we may want to schedule several tasks with different
864 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +0000865 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 */
Len Brown72945b22006-07-12 22:46:42 -0400867
David Howells65f27f32006-11-22 14:55:48 +0000868 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (!dpc)
Lin Ming889c78b2008-12-31 09:23:57 +0800870 return AE_NO_MEMORY;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 dpc->function = function;
873 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800874
Zhang Ruic02256b2009-06-23 10:20:29 +0800875 /*
876 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
877 * because the hotplug code may call driver .remove() functions,
878 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
879 * to flush these workqueues.
880 */
881 queue = hp ? kacpi_hotplug_wq :
882 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000883 dpc->wait = hp ? 1 : 0;
Zhang Ruibc736752010-03-22 15:48:54 +0800884
885 if (queue == kacpi_hotplug_wq)
886 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
887 else if (queue == kacpi_notify_wq)
888 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
889 else
890 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
891
Tejun Heo8fec62b2010-06-29 10:07:09 +0200892 /*
893 * On some machines, a software-initiated SMI causes corruption unless
894 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
895 * typically it's done in GPE-related methods that are run via
896 * workqueues, so we can avoid the known corruption cases by always
897 * queueing on CPU 0.
898 */
899 ret = queue_work_on(0, queue, &dpc->work);
Zhang Rui19cd8472008-08-28 10:05:06 +0800900
901 if (!ret) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800902 printk(KERN_ERR PREFIX
903 "Call to queue_work() failed.\n");
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300904 status = AE_ERROR;
905 kfree(dpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Lin Ming889c78b2008-12-31 09:23:57 +0800907 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
Len Brown4be44fc2005-08-05 00:44:28 -0400909
Zhang Rui19cd8472008-08-28 10:05:06 +0800910acpi_status acpi_os_execute(acpi_execute_type type,
911 acpi_osd_exec_callback function, void *context)
912{
913 return __acpi_os_execute(type, function, context, 0);
914}
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400915EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Zhang Rui19cd8472008-08-28 10:05:06 +0800917acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
918 void *context)
919{
920 return __acpi_os_execute(0, function, context, 1);
921}
922
Len Brown4be44fc2005-08-05 00:44:28 -0400923void acpi_os_wait_events_complete(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 flush_workqueue(kacpid_wq);
Zhang Rui2f67a062008-04-29 02:34:42 -0400926 flush_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
Len Brown4be44fc2005-08-05 00:44:28 -0400928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929EXPORT_SYMBOL(acpi_os_wait_events_complete);
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400932acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Len Brown4be44fc2005-08-05 00:44:28 -0400934 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 sem = acpi_os_allocate(sizeof(struct semaphore));
937 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400938 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 memset(sem, 0, sizeof(struct semaphore));
940
941 sema_init(sem, initial_units);
942
Len Brown4be44fc2005-08-05 00:44:28 -0400943 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Len Brown4be44fc2005-08-05 00:44:28 -0400945 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
946 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Patrick Mocheld550d982006-06-27 00:41:40 -0400948 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951/*
952 * TODO: A better way to delete semaphores? Linux doesn't have a
953 * 'delete_semaphore()' function -- may result in an invalid
954 * pointer dereference for non-synchronized consumers. Should
955 * we at least check for blocked threads and signal/cancel them?
956 */
957
Len Brown4be44fc2005-08-05 00:44:28 -0400958acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Len Brown4be44fc2005-08-05 00:44:28 -0400960 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400963 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Len Brown4be44fc2005-08-05 00:44:28 -0400965 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400967 BUG_ON(!list_empty(&sem->wait_list));
Len Brown02438d82006-06-30 03:19:10 -0400968 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -0400969 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Patrick Mocheld550d982006-06-27 00:41:40 -0400971 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 * TODO: Support for units > 1?
976 */
Len Brown4be44fc2005-08-05 00:44:28 -0400977acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Len Brown4be44fc2005-08-05 00:44:28 -0400979 acpi_status status = AE_OK;
980 struct semaphore *sem = (struct semaphore *)handle;
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400981 long jiffies;
Len Brown4be44fc2005-08-05 00:44:28 -0400982 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400985 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400988 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Len Brown4be44fc2005-08-05 00:44:28 -0400990 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
991 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400993 if (timeout == ACPI_WAIT_FOREVER)
994 jiffies = MAX_SCHEDULE_TIMEOUT;
995 else
996 jiffies = msecs_to_jiffies(timeout);
997
998 ret = down_timeout(sem, jiffies);
999 if (ret)
1000 status = AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -04001003 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -04001004 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -04001005 handle, units, timeout,
1006 acpi_format_exception(status)));
1007 } else {
1008 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -04001009 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001010 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012
Patrick Mocheld550d982006-06-27 00:41:40 -04001013 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016/*
1017 * TODO: Support for units > 1?
1018 */
Len Brown4be44fc2005-08-05 00:44:28 -04001019acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Len Brown4be44fc2005-08-05 00:44:28 -04001021 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001024 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -04001027 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Len Brown4be44fc2005-08-05 00:44:28 -04001029 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1030 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 up(sem);
1033
Patrick Mocheld550d982006-06-27 00:41:40 -04001034 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
Len Brown4be44fc2005-08-05 00:44:28 -04001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -04001038u32 acpi_os_get_line(char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040
1041#ifdef ENABLE_DEBUGGER
1042 if (acpi_in_debugger) {
1043 u32 chars;
1044
1045 kdb_read(buffer, sizeof(line_buf));
1046
1047 /* remove the CR kdb includes */
1048 chars = strlen(buffer) - 1;
1049 buffer[chars] = '\0';
1050 }
1051#endif
1052
1053 return 0;
1054}
Len Brown4be44fc2005-08-05 00:44:28 -04001055#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Len Brown4be44fc2005-08-05 00:44:28 -04001057acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Len Brown4be44fc2005-08-05 00:44:28 -04001059 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 case ACPI_SIGNAL_FATAL:
1061 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1062 break;
1063 case ACPI_SIGNAL_BREAKPOINT:
1064 /*
1065 * AML Breakpoint
1066 * ACPI spec. says to treat it as a NOP unless
1067 * you are debugging. So if/when we integrate
1068 * AML debugger into the kernel debugger its
1069 * hook will go here. But until then it is
1070 * not useful to print anything on breakpoints.
1071 */
1072 break;
1073 default:
1074 break;
1075 }
1076
1077 return AE_OK;
1078}
Len Brown4be44fc2005-08-05 00:44:28 -04001079
Len Brown4be44fc2005-08-05 00:44:28 -04001080static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -04001083 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if (!str || !*str)
1086 return 0;
1087
1088 for (; count-- && str && *str; str++) {
1089 if (isalnum(*str) || *str == ' ' || *str == ':')
1090 *p++ = *str;
1091 else if (*str == '\'' || *str == '"')
1092 continue;
1093 else
1094 break;
1095 }
1096 *p = 0;
1097
1098 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
1102__setup("acpi_os_name=", acpi_os_name_setup);
1103
Lin Ming12d32062010-12-09 16:51:06 +08001104#define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
1105#define OSI_STRING_ENTRIES_MAX 16 /* arbitrary */
1106
1107struct osi_setup_entry {
1108 char string[OSI_STRING_LENGTH_MAX];
1109 bool enable;
1110};
1111
Shaohua Liaa165972011-07-28 13:48:43 -07001112static struct osi_setup_entry __initdata
1113 osi_setup_entries[OSI_STRING_ENTRIES_MAX] = {
1114 {"Module Device", true},
1115 {"Processor Device", true},
1116 {"3.0 _SCP Extensions", true},
1117 {"Processor Aggregator Device", true},
1118};
Lin Ming12d32062010-12-09 16:51:06 +08001119
Lin Mingd90aa922010-12-09 16:50:52 +08001120void __init acpi_osi_setup(char *str)
1121{
Lin Ming12d32062010-12-09 16:51:06 +08001122 struct osi_setup_entry *osi;
1123 bool enable = true;
1124 int i;
1125
Lin Mingd90aa922010-12-09 16:50:52 +08001126 if (!acpi_gbl_create_osi_method)
1127 return;
1128
1129 if (str == NULL || *str == '\0') {
1130 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1131 acpi_gbl_create_osi_method = FALSE;
Lin Ming12d32062010-12-09 16:51:06 +08001132 return;
1133 }
1134
1135 if (*str == '!') {
1136 str++;
1137 enable = false;
1138 }
1139
1140 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1141 osi = &osi_setup_entries[i];
1142 if (!strcmp(osi->string, str)) {
1143 osi->enable = enable;
1144 break;
1145 } else if (osi->string[0] == '\0') {
1146 osi->enable = enable;
1147 strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
1148 break;
1149 }
1150 }
Lin Mingd90aa922010-12-09 16:50:52 +08001151}
1152
Len Brownd4b7dc42008-01-23 20:50:56 -05001153static void __init set_osi_linux(unsigned int enable)
1154{
Lin Mingd90aa922010-12-09 16:50:52 +08001155 if (osi_linux.enable != enable)
Len Brownd4b7dc42008-01-23 20:50:56 -05001156 osi_linux.enable = enable;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001157
1158 if (osi_linux.enable)
1159 acpi_osi_setup("Linux");
1160 else
1161 acpi_osi_setup("!Linux");
1162
Len Brownd4b7dc42008-01-23 20:50:56 -05001163 return;
1164}
Len Brownf5076542007-05-30 00:10:38 -04001165
Len Brownd4b7dc42008-01-23 20:50:56 -05001166static void __init acpi_cmdline_osi_linux(unsigned int enable)
1167{
Lin Mingd90aa922010-12-09 16:50:52 +08001168 osi_linux.cmdline = 1; /* cmdline set the default and override DMI */
1169 osi_linux.dmi = 0;
Len Brownd4b7dc42008-01-23 20:50:56 -05001170 set_osi_linux(enable);
Len Brownf5076542007-05-30 00:10:38 -04001171
Len Brownd4b7dc42008-01-23 20:50:56 -05001172 return;
1173}
1174
1175void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1176{
Len Brownd4b7dc42008-01-23 20:50:56 -05001177 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1178
1179 if (enable == -1)
1180 return;
1181
Lin Mingd90aa922010-12-09 16:50:52 +08001182 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
Len Brownd4b7dc42008-01-23 20:50:56 -05001183 set_osi_linux(enable);
1184
Len Brownf5076542007-05-30 00:10:38 -04001185 return;
1186}
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188/*
Len Brownae00d812007-05-29 18:43:33 -04001189 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1190 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -04001192 * string starting with '!' disables that string
1193 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 */
Lin Mingb0ed7a92010-08-06 09:35:51 +08001195static void __init acpi_osi_setup_late(void)
1196{
Lin Ming12d32062010-12-09 16:51:06 +08001197 struct osi_setup_entry *osi;
1198 char *str;
1199 int i;
Lin Mingd90aa922010-12-09 16:50:52 +08001200 acpi_status status;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001201
Lin Ming12d32062010-12-09 16:51:06 +08001202 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1203 osi = &osi_setup_entries[i];
1204 str = osi->string;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001205
Lin Ming12d32062010-12-09 16:51:06 +08001206 if (*str == '\0')
1207 break;
1208 if (osi->enable) {
1209 status = acpi_install_interface(str);
Lin Mingd90aa922010-12-09 16:50:52 +08001210
Lin Ming12d32062010-12-09 16:51:06 +08001211 if (ACPI_SUCCESS(status))
1212 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1213 } else {
1214 status = acpi_remove_interface(str);
Lin Mingd90aa922010-12-09 16:50:52 +08001215
Lin Ming12d32062010-12-09 16:51:06 +08001216 if (ACPI_SUCCESS(status))
1217 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1218 }
Lin Mingb0ed7a92010-08-06 09:35:51 +08001219 }
1220}
1221
Lin Mingd90aa922010-12-09 16:50:52 +08001222static int __init osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Lin Mingd90aa922010-12-09 16:50:52 +08001224 if (str && !strcmp("Linux", str))
1225 acpi_cmdline_osi_linux(1);
1226 else if (str && !strcmp("!Linux", str))
1227 acpi_cmdline_osi_linux(0);
1228 else
1229 acpi_osi_setup(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 return 1;
1232}
1233
Lin Mingd90aa922010-12-09 16:50:52 +08001234__setup("acpi_osi=", osi_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236/* enable serialization to combat AE_ALREADY_EXISTS errors */
Len Brown4be44fc2005-08-05 00:44:28 -04001237static int __init acpi_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 printk(KERN_INFO PREFIX "serialize enabled\n");
1240
1241 acpi_gbl_all_methods_serialized = TRUE;
1242
1243 return 1;
1244}
1245
1246__setup("acpi_serialize", acpi_serialize_setup);
1247
Thomas Renningerdf92e692008-02-04 23:31:22 -08001248/* Check of resource interference between native drivers and ACPI
1249 * OperationRegions (SystemIO and System Memory only).
1250 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1251 * in arbitrary AML code and can interfere with legacy drivers.
1252 * acpi_enforce_resources= can be set to:
1253 *
Luca Tettamanti7e905602009-03-30 00:01:27 +02001254 * - strict (default) (2)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001255 * -> further driver trying to access the resources will not load
Luca Tettamanti7e905602009-03-30 00:01:27 +02001256 * - lax (1)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001257 * -> further driver trying to access the resources will load, but you
1258 * get a system message that something might go wrong...
1259 *
1260 * - no (0)
1261 * -> ACPI Operation Region resources will not be registered
1262 *
1263 */
1264#define ENFORCE_RESOURCES_STRICT 2
1265#define ENFORCE_RESOURCES_LAX 1
1266#define ENFORCE_RESOURCES_NO 0
1267
Luca Tettamanti7e905602009-03-30 00:01:27 +02001268static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001269
1270static int __init acpi_enforce_resources_setup(char *str)
1271{
1272 if (str == NULL || *str == '\0')
1273 return 0;
1274
1275 if (!strcmp("strict", str))
1276 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1277 else if (!strcmp("lax", str))
1278 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1279 else if (!strcmp("no", str))
1280 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1281
1282 return 1;
1283}
1284
1285__setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1286
1287/* Check for resource conflicts between ACPI OperationRegions and native
1288 * drivers */
Jean Delvare876fba42009-11-11 15:22:15 +01001289int acpi_check_resource_conflict(const struct resource *res)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001290{
1291 struct acpi_res_list *res_list_elem;
Thomas Renninger106d1a02010-12-20 12:11:45 +01001292 int ioport = 0, clash = 0;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001293
1294 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1295 return 0;
1296 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1297 return 0;
1298
1299 ioport = res->flags & IORESOURCE_IO;
1300
1301 spin_lock(&acpi_res_lock);
1302 list_for_each_entry(res_list_elem, &resource_list_head,
1303 resource_list) {
1304 if (ioport && (res_list_elem->resource_type
1305 != ACPI_ADR_SPACE_SYSTEM_IO))
1306 continue;
1307 if (!ioport && (res_list_elem->resource_type
1308 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1309 continue;
1310
1311 if (res->end < res_list_elem->start
1312 || res_list_elem->end < res->start)
1313 continue;
1314 clash = 1;
1315 break;
1316 }
1317 spin_unlock(&acpi_res_lock);
1318
1319 if (clash) {
1320 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
Chase Douglas1638bca2010-03-22 15:08:09 -04001321 printk(KERN_WARNING "ACPI: resource %s %pR"
Thomas Renninger106d1a02010-12-20 12:11:45 +01001322 " conflicts with ACPI region %s "
1323 "[%s 0x%zx-0x%zx]\n",
Chase Douglas1638bca2010-03-22 15:08:09 -04001324 res->name, res, res_list_elem->name,
Thomas Renninger106d1a02010-12-20 12:11:45 +01001325 (res_list_elem->resource_type ==
1326 ACPI_ADR_SPACE_SYSTEM_IO) ? "io" : "mem",
1327 (size_t) res_list_elem->start,
1328 (size_t) res_list_elem->end);
Jean Delvare14f03342009-09-08 15:31:46 +02001329 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1330 printk(KERN_NOTICE "ACPI: This conflict may"
1331 " cause random problems and system"
1332 " instability\n");
1333 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1334 " for this device, you should use it instead of"
1335 " the native driver\n");
Thomas Renningerdf92e692008-02-04 23:31:22 -08001336 }
1337 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1338 return -EBUSY;
1339 }
1340 return 0;
1341}
Thomas Renninger443dea72008-02-04 23:31:23 -08001342EXPORT_SYMBOL(acpi_check_resource_conflict);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001343
1344int acpi_check_region(resource_size_t start, resource_size_t n,
1345 const char *name)
1346{
1347 struct resource res = {
1348 .start = start,
1349 .end = start + n - 1,
1350 .name = name,
1351 .flags = IORESOURCE_IO,
1352 };
1353
1354 return acpi_check_resource_conflict(&res);
1355}
1356EXPORT_SYMBOL(acpi_check_region);
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358/*
Jean Delvare70dd6be2010-05-27 19:58:37 +02001359 * Let drivers know whether the resource checks are effective
1360 */
1361int acpi_resources_are_enforced(void)
1362{
1363 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1364}
1365EXPORT_SYMBOL(acpi_resources_are_enforced);
1366
1367/*
Lin Ming9f63b882011-03-23 17:26:34 +08001368 * Deallocate the memory for a spinlock.
1369 */
1370void acpi_os_delete_lock(acpi_spinlock handle)
1371{
1372 ACPI_FREE(handle);
1373}
1374
1375/*
Robert Moore73459f72005-06-24 00:00:00 -04001376 * Acquire a spinlock.
1377 *
1378 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001379 */
1380
Bob Moore967440e32006-06-23 17:04:00 -04001381acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001382{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001383 acpi_cpu_flags flags;
Bob Moore967440e32006-06-23 17:04:00 -04001384 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001385 return flags;
1386}
1387
1388/*
1389 * Release a spinlock. See above.
1390 */
1391
Bob Moore967440e32006-06-23 17:04:00 -04001392void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001393{
Bob Moore967440e32006-06-23 17:04:00 -04001394 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001395}
1396
Robert Moore73459f72005-06-24 00:00:00 -04001397#ifndef ACPI_USE_LOCAL_CACHE
1398
1399/*******************************************************************************
1400 *
1401 * FUNCTION: acpi_os_create_cache
1402 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001403 * PARAMETERS: name - Ascii name for the cache
1404 * size - Size of each cached object
1405 * depth - Maximum depth of the cache (in objects) <ignored>
1406 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001407 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001408 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001409 *
1410 * DESCRIPTION: Create a cache object
1411 *
1412 ******************************************************************************/
1413
1414acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001415acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001416{
Paul Mundt20c2df82007-07-20 10:11:58 +09001417 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001418 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001419 return AE_ERROR;
1420 else
1421 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001422}
1423
1424/*******************************************************************************
1425 *
1426 * FUNCTION: acpi_os_purge_cache
1427 *
1428 * PARAMETERS: Cache - Handle to cache object
1429 *
1430 * RETURN: Status
1431 *
1432 * DESCRIPTION: Free all objects within the requested cache.
1433 *
1434 ******************************************************************************/
1435
Len Brown4be44fc2005-08-05 00:44:28 -04001436acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001437{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001438 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001439 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001440}
1441
1442/*******************************************************************************
1443 *
1444 * FUNCTION: acpi_os_delete_cache
1445 *
1446 * PARAMETERS: Cache - Handle to cache object
1447 *
1448 * RETURN: Status
1449 *
1450 * DESCRIPTION: Free all objects within the requested cache and delete the
1451 * cache object.
1452 *
1453 ******************************************************************************/
1454
Len Brown4be44fc2005-08-05 00:44:28 -04001455acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001456{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001457 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001458 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001459}
1460
1461/*******************************************************************************
1462 *
1463 * FUNCTION: acpi_os_release_object
1464 *
1465 * PARAMETERS: Cache - Handle to cache object
1466 * Object - The object to be released
1467 *
1468 * RETURN: None
1469 *
1470 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1471 * the object is deleted.
1472 *
1473 ******************************************************************************/
1474
Len Brown4be44fc2005-08-05 00:44:28 -04001475acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001476{
Len Brown4be44fc2005-08-05 00:44:28 -04001477 kmem_cache_free(cache, object);
1478 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001479}
1480
Lin Minga5fe1a02009-08-13 10:43:27 +08001481static inline int acpi_res_list_add(struct acpi_res_list *res)
1482{
1483 struct acpi_res_list *res_list_elem;
1484
1485 list_for_each_entry(res_list_elem, &resource_list_head,
1486 resource_list) {
1487
1488 if (res->resource_type == res_list_elem->resource_type &&
1489 res->start == res_list_elem->start &&
1490 res->end == res_list_elem->end) {
1491
1492 /*
1493 * The Region(addr,len) already exist in the list,
1494 * just increase the count
1495 */
1496
1497 res_list_elem->count++;
1498 return 0;
1499 }
1500 }
1501
1502 res->count = 1;
1503 list_add(&res->resource_list, &resource_list_head);
1504 return 1;
1505}
1506
1507static inline void acpi_res_list_del(struct acpi_res_list *res)
1508{
1509 struct acpi_res_list *res_list_elem;
1510
1511 list_for_each_entry(res_list_elem, &resource_list_head,
1512 resource_list) {
1513
1514 if (res->resource_type == res_list_elem->resource_type &&
1515 res->start == res_list_elem->start &&
1516 res->end == res_list_elem->end) {
1517
1518 /*
1519 * If the res count is decreased to 0,
1520 * remove and free it
1521 */
1522
1523 if (--res_list_elem->count == 0) {
1524 list_del(&res_list_elem->resource_list);
1525 kfree(res_list_elem);
1526 }
1527 return;
1528 }
1529 }
1530}
1531
1532acpi_status
1533acpi_os_invalidate_address(
1534 u8 space_id,
1535 acpi_physical_address address,
1536 acpi_size length)
1537{
1538 struct acpi_res_list res;
1539
1540 switch (space_id) {
1541 case ACPI_ADR_SPACE_SYSTEM_IO:
1542 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001543 /* Only interference checks against SystemIO and SystemMemory
Lin Minga5fe1a02009-08-13 10:43:27 +08001544 are needed */
1545 res.start = address;
1546 res.end = address + length - 1;
1547 res.resource_type = space_id;
1548 spin_lock(&acpi_res_lock);
1549 acpi_res_list_del(&res);
1550 spin_unlock(&acpi_res_lock);
1551 break;
1552 case ACPI_ADR_SPACE_PCI_CONFIG:
1553 case ACPI_ADR_SPACE_EC:
1554 case ACPI_ADR_SPACE_SMBUS:
1555 case ACPI_ADR_SPACE_CMOS:
1556 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1557 case ACPI_ADR_SPACE_DATA_TABLE:
1558 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1559 break;
1560 }
1561 return AE_OK;
1562}
1563
Bob Mooreb229cf92006-04-21 17:15:00 -04001564/******************************************************************************
1565 *
1566 * FUNCTION: acpi_os_validate_address
1567 *
1568 * PARAMETERS: space_id - ACPI space ID
1569 * address - Physical address
1570 * length - Address length
1571 *
1572 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1573 * should return AE_AML_ILLEGAL_ADDRESS.
1574 *
1575 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1576 * the addresses accessed by AML operation regions.
1577 *
1578 *****************************************************************************/
1579
1580acpi_status
1581acpi_os_validate_address (
1582 u8 space_id,
1583 acpi_physical_address address,
Thomas Renningerdf92e692008-02-04 23:31:22 -08001584 acpi_size length,
1585 char *name)
Bob Mooreb229cf92006-04-21 17:15:00 -04001586{
Thomas Renningerdf92e692008-02-04 23:31:22 -08001587 struct acpi_res_list *res;
Lin Minga5fe1a02009-08-13 10:43:27 +08001588 int added;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001589 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1590 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001591
Thomas Renningerdf92e692008-02-04 23:31:22 -08001592 switch (space_id) {
1593 case ACPI_ADR_SPACE_SYSTEM_IO:
1594 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001595 /* Only interference checks against SystemIO and SystemMemory
Thomas Renningerdf92e692008-02-04 23:31:22 -08001596 are needed */
1597 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1598 if (!res)
1599 return AE_OK;
1600 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1601 strlcpy(res->name, name, 5);
1602 res->start = address;
1603 res->end = address + length - 1;
1604 res->resource_type = space_id;
1605 spin_lock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001606 added = acpi_res_list_add(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001607 spin_unlock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001608 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1609 "name: %s\n", added ? "Added" : "Already exist",
1610 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001611 ? "SystemIO" : "System Memory",
1612 (unsigned long long)res->start,
1613 (unsigned long long)res->end,
1614 res->name);
Lin Minga5fe1a02009-08-13 10:43:27 +08001615 if (!added)
1616 kfree(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001617 break;
1618 case ACPI_ADR_SPACE_PCI_CONFIG:
1619 case ACPI_ADR_SPACE_EC:
1620 case ACPI_ADR_SPACE_SMBUS:
1621 case ACPI_ADR_SPACE_CMOS:
1622 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1623 case ACPI_ADR_SPACE_DATA_TABLE:
1624 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1625 break;
1626 }
1627 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001628}
Robert Moore73459f72005-06-24 00:00:00 -04001629#endif
Myron Stowed362eda2010-10-21 14:24:04 -06001630
1631acpi_status __init acpi_os_initialize(void)
1632{
1633 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1634 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1635 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1636 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1637
1638 return AE_OK;
1639}
1640
Zhang Rui32d47ee2010-12-08 10:40:36 +08001641acpi_status __init acpi_os_initialize1(void)
Myron Stowed362eda2010-10-21 14:24:04 -06001642{
Tejun Heo44d25882011-02-01 11:42:42 +01001643 kacpid_wq = alloc_workqueue("kacpid", 0, 1);
1644 kacpi_notify_wq = alloc_workqueue("kacpi_notify", 0, 1);
1645 kacpi_hotplug_wq = alloc_workqueue("kacpi_hotplug", 0, 1);
Myron Stowed362eda2010-10-21 14:24:04 -06001646 BUG_ON(!kacpid_wq);
1647 BUG_ON(!kacpi_notify_wq);
1648 BUG_ON(!kacpi_hotplug_wq);
Len Brown1bd64d42010-10-26 14:50:56 -04001649 acpi_install_interface_handler(acpi_osi_handler);
1650 acpi_osi_setup_late();
Myron Stowed362eda2010-10-21 14:24:04 -06001651 return AE_OK;
1652}
1653
1654acpi_status acpi_os_terminate(void)
1655{
1656 if (acpi_irq_handler) {
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +01001657 acpi_os_remove_interrupt_handler(acpi_gbl_FADT.sci_interrupt,
Myron Stowed362eda2010-10-21 14:24:04 -06001658 acpi_irq_handler);
1659 }
1660
1661 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1662 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1663 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1664 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1665
1666 destroy_workqueue(kacpid_wq);
1667 destroy_workqueue(kacpi_notify_wq);
1668 destroy_workqueue(kacpi_hotplug_wq);
1669
1670 return AE_OK;
1671}