blob: f31c5c5f1b7e083351e013649f1e1d73fdd74fa6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
3 *
4 * Copyright (C) 2000 Andrew Henroid
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04007 * Copyright (c) 2008 Intel Corporation
8 * Author: Matthew Wilcox <willy@linux.intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 *
28 */
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/slab.h>
33#include <linux/mm.h>
34#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/interrupt.h>
36#include <linux/kmod.h>
37#include <linux/delay.h>
38#include <linux/workqueue.h>
39#include <linux/nmi.h>
Alexey Starikovskiyad718602007-02-02 19:48:19 +030040#include <linux/acpi.h>
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +010041#include <linux/acpi_io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/efi.h>
Thomas Renningerdf92e692008-02-04 23:31:22 -080043#include <linux/ioport.h>
44#include <linux/list.h>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -040045#include <linux/jiffies.h>
46#include <linux/semaphore.h>
47
48#include <asm/io.h>
49#include <asm/uaccess.h>
50
51#include <acpi/acpi.h>
52#include <acpi/acpi_bus.h>
53#include <acpi/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define _COMPONENT ACPI_OS_SERVICES
Len Brownf52fd662007-02-12 22:42:12 -050056ACPI_MODULE_NAME("osl");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define PREFIX "ACPI: "
Len Brown4be44fc2005-08-05 00:44:28 -040058struct acpi_os_dpc {
59 acpi_osd_exec_callback function;
60 void *context;
David Howells65f27f32006-11-22 14:55:48 +000061 struct work_struct work;
Bjorn Helgaas9ac61852009-08-31 22:32:10 +000062 int wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65#ifdef CONFIG_ACPI_CUSTOM_DSDT
66#include CONFIG_ACPI_CUSTOM_DSDT_FILE
67#endif
68
69#ifdef ENABLE_DEBUGGER
70#include <linux/kdb.h>
71
72/* stuff for debugger support */
73int acpi_in_debugger;
74EXPORT_SYMBOL(acpi_in_debugger);
75
76extern char line_buf[80];
Len Brown4be44fc2005-08-05 00:44:28 -040077#endif /*ENABLE_DEBUGGER */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
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
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
Takao Indoh4996c022011-07-14 18:05:21 -0400241#ifdef CONFIG_KEXEC
242static unsigned long acpi_rsdp;
243static int __init setup_acpi_rsdp(char *arg)
244{
245 acpi_rsdp = simple_strtoul(arg, NULL, 16);
246 return 0;
247}
248early_param("acpi_rsdp", setup_acpi_rsdp);
249#endif
250
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300251acpi_physical_address __init acpi_os_get_root_pointer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Takao Indoh4996c022011-07-14 18:05:21 -0400253#ifdef CONFIG_KEXEC
254 if (acpi_rsdp)
255 return acpi_rsdp;
256#endif
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (efi_enabled) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800259 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300260 return efi.acpi20;
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800261 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300262 return efi.acpi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400264 printk(KERN_ERR PREFIX
265 "System description tables not found\n");
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Len Brown239665a2007-11-23 20:08:02 -0500268 } else {
269 acpi_physical_address pa = 0;
270
271 acpi_find_root_pointer(&pa);
272 return pa;
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600276/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe4a3cba52010-10-21 14:24:14 -0600277static struct acpi_ioremap *
278acpi_map_lookup(acpi_physical_address phys, acpi_size size)
Myron Stowe620242a2010-10-21 14:23:53 -0600279{
280 struct acpi_ioremap *map;
281
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600282 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe620242a2010-10-21 14:23:53 -0600283 if (map->phys <= phys &&
284 phys + size <= map->phys + map->size)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600285 return map;
286
287 return NULL;
288}
289
290/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
291static void __iomem *
292acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
293{
294 struct acpi_ioremap *map;
295
296 map = acpi_map_lookup(phys, size);
297 if (map)
298 return map->virt + (phys - map->phys);
Myron Stowe620242a2010-10-21 14:23:53 -0600299
300 return NULL;
301}
302
Rafael J. Wysocki13606a22011-02-08 23:38:25 +0100303void __iomem *acpi_os_get_iomem(acpi_physical_address phys, unsigned int size)
304{
305 struct acpi_ioremap *map;
306 void __iomem *virt = NULL;
307
308 mutex_lock(&acpi_ioremap_lock);
309 map = acpi_map_lookup(phys, size);
310 if (map) {
311 virt = map->virt + (phys - map->phys);
312 map->refcount++;
313 }
314 mutex_unlock(&acpi_ioremap_lock);
315 return virt;
316}
317EXPORT_SYMBOL_GPL(acpi_os_get_iomem);
318
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600319/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe620242a2010-10-21 14:23:53 -0600320static struct acpi_ioremap *
321acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
322{
323 struct acpi_ioremap *map;
324
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600325 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600326 if (map->virt <= virt &&
327 virt + size <= map->virt + map->size)
Myron Stowe620242a2010-10-21 14:23:53 -0600328 return map;
329
330 return NULL;
331}
332
Jan Beulich2fdf0742007-12-13 08:33:59 +0000333void __iomem *__init_refok
334acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100336 struct acpi_ioremap *map;
Myron Stowe620242a2010-10-21 14:23:53 -0600337 void __iomem *virt;
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100338 acpi_physical_address pg_off;
339 acpi_size pg_sz;
Myron Stowe620242a2010-10-21 14:23:53 -0600340
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800341 if (phys > ULONG_MAX) {
342 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
Randy Dunlap70c08462007-02-13 16:11:36 -0800343 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Myron Stowe620242a2010-10-21 14:23:53 -0600345
346 if (!acpi_gbl_permanent_mmap)
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300347 return __acpi_map_table((unsigned long)phys, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600348
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100349 mutex_lock(&acpi_ioremap_lock);
350 /* Check if there's a suitable mapping already. */
351 map = acpi_map_lookup(phys, size);
352 if (map) {
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100353 map->refcount++;
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100354 goto out;
355 }
356
Myron Stowe620242a2010-10-21 14:23:53 -0600357 map = kzalloc(sizeof(*map), GFP_KERNEL);
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100358 if (!map) {
359 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600360 return NULL;
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100361 }
Myron Stowe620242a2010-10-21 14:23:53 -0600362
Myron Stowe4a3cba52010-10-21 14:24:14 -0600363 pg_off = round_down(phys, PAGE_SIZE);
364 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100365 virt = acpi_os_ioremap(pg_off, pg_sz);
Myron Stowe620242a2010-10-21 14:23:53 -0600366 if (!virt) {
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100367 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600368 kfree(map);
369 return NULL;
370 }
371
372 INIT_LIST_HEAD(&map->list);
373 map->virt = virt;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600374 map->phys = pg_off;
375 map->size = pg_sz;
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100376 map->refcount = 1;
Myron Stowe620242a2010-10-21 14:23:53 -0600377
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600378 list_add_tail_rcu(&map->list, &acpi_ioremaps);
Myron Stowe620242a2010-10-21 14:23:53 -0600379
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100380 out:
381 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600382 return map->virt + (phys - map->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800384EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100386static void acpi_os_drop_map_ref(struct acpi_ioremap *map)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600387{
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100388 if (!--map->refcount)
389 list_del_rcu(&map->list);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600390}
Myron Stowe4a3cba52010-10-21 14:24:14 -0600391
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100392static void acpi_os_map_cleanup(struct acpi_ioremap *map)
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100393{
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100394 if (!map->refcount) {
395 synchronize_rcu();
396 iounmap(map->virt);
397 kfree(map);
398 }
Myron Stowe4a3cba52010-10-21 14:24:14 -0600399}
400
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800401void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Myron Stowe620242a2010-10-21 14:23:53 -0600403 struct acpi_ioremap *map;
Myron Stowe620242a2010-10-21 14:23:53 -0600404
405 if (!acpi_gbl_permanent_mmap) {
Yinghai Lu7d972772009-02-07 15:39:41 -0800406 __acpi_unmap_table(virt, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600407 return;
408 }
409
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100410 mutex_lock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600411 map = acpi_map_lookup_virt(virt, size);
412 if (!map) {
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100413 mutex_unlock(&acpi_ioremap_lock);
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100414 WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
Myron Stowe620242a2010-10-21 14:23:53 -0600415 return;
416 }
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100417 acpi_os_drop_map_ref(map);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100418 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600419
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100420 acpi_os_map_cleanup(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800422EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800424void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Yinghai Lu7d972772009-02-07 15:39:41 -0800425{
426 if (!acpi_gbl_permanent_mmap)
427 __acpi_unmap_table(virt, size);
428}
429
Rafael J. Wysocki073b49642011-02-08 23:37:31 +0100430static int acpi_os_map_generic_address(struct acpi_generic_address *addr)
Myron Stowe29718522010-10-21 14:23:59 -0600431{
432 void __iomem *virt;
433
434 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
435 return 0;
436
437 if (!addr->address || !addr->bit_width)
438 return -EINVAL;
439
440 virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
441 if (!virt)
442 return -EIO;
443
444 return 0;
445}
Myron Stowe29718522010-10-21 14:23:59 -0600446
Rafael J. Wysocki073b49642011-02-08 23:37:31 +0100447static void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
Myron Stowe29718522010-10-21 14:23:59 -0600448{
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100449 struct acpi_ioremap *map;
Myron Stowe29718522010-10-21 14:23:59 -0600450
451 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
452 return;
453
454 if (!addr->address || !addr->bit_width)
455 return;
456
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100457 mutex_lock(&acpi_ioremap_lock);
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100458 map = acpi_map_lookup(addr->address, addr->bit_width / 8);
459 if (!map) {
460 mutex_unlock(&acpi_ioremap_lock);
461 return;
462 }
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100463 acpi_os_drop_map_ref(map);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100464 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe29718522010-10-21 14:23:59 -0600465
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100466 acpi_os_map_cleanup(map);
Myron Stowe29718522010-10-21 14:23:59 -0600467}
Myron Stowe29718522010-10-21 14:23:59 -0600468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469#ifdef ACPI_FUTURE_USAGE
470acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400471acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Len Brown4be44fc2005-08-05 00:44:28 -0400473 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return AE_BAD_PARAMETER;
475
476 *phys = virt_to_phys(virt);
477
478 return AE_OK;
479}
480#endif
481
482#define ACPI_MAX_OVERRIDE_LEN 100
483
484static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
485
486acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400487acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
488 acpi_string * new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 if (!init_val || !new_val)
491 return AE_BAD_PARAMETER;
492
493 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400494 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400496 acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 *new_val = acpi_os_name;
498 }
499
500 return AE_OK;
501}
502
503acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400504acpi_os_table_override(struct acpi_table_header * existing_table,
505 struct acpi_table_header ** new_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 if (!existing_table || !new_table)
508 return AE_BAD_PARAMETER;
509
Markus Gaugusch71fc47a2008-02-05 00:04:06 +0100510 *new_table = NULL;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512#ifdef CONFIG_ACPI_CUSTOM_DSDT
513 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400514 *new_table = (struct acpi_table_header *)AmlCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515#endif
Éric Piel6ed31e92008-02-05 00:04:50 +0100516 if (*new_table != NULL) {
517 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
518 "this is unsafe: tainting kernel\n",
519 existing_table->signature,
520 existing_table->oem_table_id);
521 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return AE_OK;
524}
525
David Howells7d12e782006-10-05 14:55:46 +0100526static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Len Brown5229e872008-02-06 01:26:55 -0500528 u32 handled;
529
530 handled = (*acpi_irq_handler) (acpi_irq_context);
531
532 if (handled) {
533 acpi_irq_handled++;
534 return IRQ_HANDLED;
Len Brown88bea182009-04-21 00:35:47 -0400535 } else {
536 acpi_irq_not_handled++;
Len Brown5229e872008-02-06 01:26:55 -0500537 return IRQ_NONE;
Len Brown88bea182009-04-21 00:35:47 -0400538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400542acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
543 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 unsigned int irq;
546
Len Brown5229e872008-02-06 01:26:55 -0500547 acpi_irq_stats_init();
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /*
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100550 * ACPI interrupts different from the SCI in our copy of the FADT are
551 * not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 */
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100553 if (gsi != acpi_gbl_FADT.sci_interrupt)
554 return AE_BAD_PARAMETER;
555
556 if (acpi_irq_handler)
557 return AE_ALREADY_ACQUIRED;
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
560 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
561 gsi);
562 return AE_OK;
563 }
564
565 acpi_irq_handler = handler;
566 acpi_irq_context = context;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700567 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100569 acpi_irq_handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return AE_NOT_ACQUIRED;
571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 return AE_OK;
574}
575
Len Brown4be44fc2005-08-05 00:44:28 -0400576acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100578 if (irq != acpi_gbl_FADT.sci_interrupt)
579 return AE_BAD_PARAMETER;
580
581 free_irq(irq, acpi_irq);
582 acpi_irq_handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 return AE_OK;
585}
586
587/*
588 * Running in interpreter thread context, safe to sleep
589 */
590
Lin Ming439913f2010-01-28 10:53:19 +0800591void acpi_os_sleep(u64 ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800593 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
Len Brown4be44fc2005-08-05 00:44:28 -0400595
Len Brown4be44fc2005-08-05 00:44:28 -0400596void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 while (us) {
599 u32 delay = 1000;
600
601 if (delay > us)
602 delay = us;
603 udelay(delay);
604 touch_nmi_watchdog();
605 us -= delay;
606 }
607}
Len Brown4be44fc2005-08-05 00:44:28 -0400608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/*
610 * Support ACPI 3.0 AML Timer operand
611 * Returns 64-bit free-running, monotonically increasing timer
612 * with 100ns granularity
613 */
Len Brown4be44fc2005-08-05 00:44:28 -0400614u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 static u64 t;
617
618#ifdef CONFIG_HPET
619 /* TBD: use HPET if available */
620#endif
621
622#ifdef CONFIG_X86_PM_TIMER
623 /* TBD: default to PM timer if HPET was not available */
624#endif
625 if (!t)
626 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
627
628 return ++t;
629}
630
Len Brown4be44fc2005-08-05 00:44:28 -0400631acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 u32 dummy;
634
635 if (!value)
636 value = &dummy;
637
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800638 *value = 0;
639 if (width <= 8) {
Len Brown4be44fc2005-08-05 00:44:28 -0400640 *(u8 *) value = inb(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800641 } else if (width <= 16) {
Len Brown4be44fc2005-08-05 00:44:28 -0400642 *(u16 *) value = inw(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800643 } else if (width <= 32) {
Len Brown4be44fc2005-08-05 00:44:28 -0400644 *(u32 *) value = inl(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800645 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 BUG();
647 }
648
649 return AE_OK;
650}
Len Brown4be44fc2005-08-05 00:44:28 -0400651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652EXPORT_SYMBOL(acpi_os_read_port);
653
Len Brown4be44fc2005-08-05 00:44:28 -0400654acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800656 if (width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 outb(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800658 } else if (width <= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 outw(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800660 } else if (width <= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 outl(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800662 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 BUG();
664 }
665
666 return AE_OK;
667}
Len Brown4be44fc2005-08-05 00:44:28 -0400668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669EXPORT_SYMBOL(acpi_os_write_port);
670
671acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400672acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Len Brown4be44fc2005-08-05 00:44:28 -0400674 void __iomem *virt_addr;
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100675 unsigned int size = width / 8;
676 bool unmap = false;
677 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600679 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600680 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600681 if (!virt_addr) {
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100682 rcu_read_unlock();
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100683 virt_addr = acpi_os_ioremap(phys_addr, size);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100684 if (!virt_addr)
685 return AE_BAD_ADDRESS;
686 unmap = true;
Myron Stowe620242a2010-10-21 14:23:53 -0600687 }
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (!value)
690 value = &dummy;
691
692 switch (width) {
693 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400694 *(u8 *) value = readb(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 break;
696 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400697 *(u16 *) value = readw(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 break;
699 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400700 *(u32 *) value = readl(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
702 default:
703 BUG();
704 }
705
Myron Stowe620242a2010-10-21 14:23:53 -0600706 if (unmap)
707 iounmap(virt_addr);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100708 else
709 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 return AE_OK;
712}
713
714acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400715acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Len Brown4be44fc2005-08-05 00:44:28 -0400717 void __iomem *virt_addr;
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100718 unsigned int size = width / 8;
719 bool unmap = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600721 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600722 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600723 if (!virt_addr) {
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100724 rcu_read_unlock();
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100725 virt_addr = acpi_os_ioremap(phys_addr, size);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100726 if (!virt_addr)
727 return AE_BAD_ADDRESS;
728 unmap = true;
Myron Stowe620242a2010-10-21 14:23:53 -0600729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 switch (width) {
732 case 8:
733 writeb(value, virt_addr);
734 break;
735 case 16:
736 writew(value, virt_addr);
737 break;
738 case 32:
739 writel(value, virt_addr);
740 break;
741 default:
742 BUG();
743 }
744
Myron Stowe620242a2010-10-21 14:23:53 -0600745 if (unmap)
746 iounmap(virt_addr);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100747 else
748 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 return AE_OK;
751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400754acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Bob Moorec5f02312010-08-06 08:57:53 +0800755 u64 *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 int result, size;
Bob Moorec5f02312010-08-06 08:57:53 +0800758 u32 value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (!value)
761 return AE_BAD_PARAMETER;
762
763 switch (width) {
764 case 8:
765 size = 1;
766 break;
767 case 16:
768 size = 2;
769 break;
770 case 32:
771 size = 4;
772 break;
773 default:
774 return AE_ERROR;
775 }
776
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500777 result = raw_pci_read(pci_id->segment, pci_id->bus,
778 PCI_DEVFN(pci_id->device, pci_id->function),
Bob Moorec5f02312010-08-06 08:57:53 +0800779 reg, size, &value32);
780 *value = value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 return (result ? AE_ERROR : AE_OK);
783}
Len Brown4be44fc2005-08-05 00:44:28 -0400784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400786acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Lin Ming439913f2010-01-28 10:53:19 +0800787 u64 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 int result, size;
790
791 switch (width) {
792 case 8:
793 size = 1;
794 break;
795 case 16:
796 size = 2;
797 break;
798 case 32:
799 size = 4;
800 break;
801 default:
802 return AE_ERROR;
803 }
804
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500805 result = raw_pci_write(pci_id->segment, pci_id->bus,
806 PCI_DEVFN(pci_id->device, pci_id->function),
807 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 return (result ? AE_ERROR : AE_OK);
810}
811
David Howells65f27f32006-11-22 14:55:48 +0000812static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
David Howells65f27f32006-11-22 14:55:48 +0000814 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400815
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000816 if (dpc->wait)
817 acpi_os_wait_events_complete(NULL);
Zhang Rui19cd8472008-08-28 10:05:06 +0800818
819 dpc->function(dpc->context);
820 kfree(dpc);
Zhang Rui19cd8472008-08-28 10:05:06 +0800821}
822
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400823/*******************************************************************************
824 *
825 * FUNCTION: acpi_os_execute
826 *
827 * PARAMETERS: Type - Type of the callback
828 * Function - Function to be executed
829 * Context - Function parameters
830 *
831 * RETURN: Status
832 *
833 * DESCRIPTION: Depending on type, either queues function for deferred execution or
834 * immediately executes function on a separate thread.
835 *
836 ******************************************************************************/
837
Zhang Rui19cd8472008-08-28 10:05:06 +0800838static acpi_status __acpi_os_execute(acpi_execute_type type,
839 acpi_osd_exec_callback function, void *context, int hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Len Brown4be44fc2005-08-05 00:44:28 -0400841 acpi_status status = AE_OK;
842 struct acpi_os_dpc *dpc;
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300843 struct workqueue_struct *queue;
Zhang Rui19cd8472008-08-28 10:05:06 +0800844 int ret;
Len Brown72945b22006-07-12 22:46:42 -0400845 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
846 "Scheduling function [%p(%p)] for deferred execution.\n",
847 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /*
850 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +0000851 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 * way that allows us to also free its memory inside the callee.
853 * Because we may want to schedule several tasks with different
854 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +0000855 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
Len Brown72945b22006-07-12 22:46:42 -0400857
David Howells65f27f32006-11-22 14:55:48 +0000858 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (!dpc)
Lin Ming889c78b2008-12-31 09:23:57 +0800860 return AE_NO_MEMORY;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 dpc->function = function;
863 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800864
Zhang Ruic02256b2009-06-23 10:20:29 +0800865 /*
866 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
867 * because the hotplug code may call driver .remove() functions,
868 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
869 * to flush these workqueues.
870 */
871 queue = hp ? kacpi_hotplug_wq :
872 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000873 dpc->wait = hp ? 1 : 0;
Zhang Ruibc736752010-03-22 15:48:54 +0800874
875 if (queue == kacpi_hotplug_wq)
876 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
877 else if (queue == kacpi_notify_wq)
878 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
879 else
880 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
881
Tejun Heo8fec62b2010-06-29 10:07:09 +0200882 /*
883 * On some machines, a software-initiated SMI causes corruption unless
884 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
885 * typically it's done in GPE-related methods that are run via
886 * workqueues, so we can avoid the known corruption cases by always
887 * queueing on CPU 0.
888 */
889 ret = queue_work_on(0, queue, &dpc->work);
Zhang Rui19cd8472008-08-28 10:05:06 +0800890
891 if (!ret) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800892 printk(KERN_ERR PREFIX
893 "Call to queue_work() failed.\n");
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300894 status = AE_ERROR;
895 kfree(dpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
Lin Ming889c78b2008-12-31 09:23:57 +0800897 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
Len Brown4be44fc2005-08-05 00:44:28 -0400899
Zhang Rui19cd8472008-08-28 10:05:06 +0800900acpi_status acpi_os_execute(acpi_execute_type type,
901 acpi_osd_exec_callback function, void *context)
902{
903 return __acpi_os_execute(type, function, context, 0);
904}
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400905EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Zhang Rui19cd8472008-08-28 10:05:06 +0800907acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
908 void *context)
909{
910 return __acpi_os_execute(0, function, context, 1);
911}
912
Len Brown4be44fc2005-08-05 00:44:28 -0400913void acpi_os_wait_events_complete(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 flush_workqueue(kacpid_wq);
Zhang Rui2f67a062008-04-29 02:34:42 -0400916 flush_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
Len Brown4be44fc2005-08-05 00:44:28 -0400918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919EXPORT_SYMBOL(acpi_os_wait_events_complete);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400922acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Len Brown4be44fc2005-08-05 00:44:28 -0400924 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 sem = acpi_os_allocate(sizeof(struct semaphore));
927 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400928 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 memset(sem, 0, sizeof(struct semaphore));
930
931 sema_init(sem, initial_units);
932
Len Brown4be44fc2005-08-05 00:44:28 -0400933 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Len Brown4be44fc2005-08-05 00:44:28 -0400935 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
936 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Patrick Mocheld550d982006-06-27 00:41:40 -0400938 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941/*
942 * TODO: A better way to delete semaphores? Linux doesn't have a
943 * 'delete_semaphore()' function -- may result in an invalid
944 * pointer dereference for non-synchronized consumers. Should
945 * we at least check for blocked threads and signal/cancel them?
946 */
947
Len Brown4be44fc2005-08-05 00:44:28 -0400948acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Len Brown4be44fc2005-08-05 00:44:28 -0400950 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400953 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Len Brown4be44fc2005-08-05 00:44:28 -0400955 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400957 BUG_ON(!list_empty(&sem->wait_list));
Len Brown02438d82006-06-30 03:19:10 -0400958 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -0400959 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Patrick Mocheld550d982006-06-27 00:41:40 -0400961 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 * TODO: Support for units > 1?
966 */
Len Brown4be44fc2005-08-05 00:44:28 -0400967acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Len Brown4be44fc2005-08-05 00:44:28 -0400969 acpi_status status = AE_OK;
970 struct semaphore *sem = (struct semaphore *)handle;
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400971 long jiffies;
Len Brown4be44fc2005-08-05 00:44:28 -0400972 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400975 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400978 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Len Brown4be44fc2005-08-05 00:44:28 -0400980 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
981 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400983 if (timeout == ACPI_WAIT_FOREVER)
984 jiffies = MAX_SCHEDULE_TIMEOUT;
985 else
986 jiffies = msecs_to_jiffies(timeout);
987
988 ret = down_timeout(sem, jiffies);
989 if (ret)
990 status = AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -0400993 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400994 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -0400995 handle, units, timeout,
996 acpi_format_exception(status)));
997 } else {
998 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400999 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001000 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002
Patrick Mocheld550d982006-06-27 00:41:40 -04001003 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006/*
1007 * TODO: Support for units > 1?
1008 */
Len Brown4be44fc2005-08-05 00:44:28 -04001009acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Len Brown4be44fc2005-08-05 00:44:28 -04001011 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001014 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -04001017 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Len Brown4be44fc2005-08-05 00:44:28 -04001019 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1020 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 up(sem);
1023
Patrick Mocheld550d982006-06-27 00:41:40 -04001024 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
Len Brown4be44fc2005-08-05 00:44:28 -04001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -04001028u32 acpi_os_get_line(char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030
1031#ifdef ENABLE_DEBUGGER
1032 if (acpi_in_debugger) {
1033 u32 chars;
1034
1035 kdb_read(buffer, sizeof(line_buf));
1036
1037 /* remove the CR kdb includes */
1038 chars = strlen(buffer) - 1;
1039 buffer[chars] = '\0';
1040 }
1041#endif
1042
1043 return 0;
1044}
Len Brown4be44fc2005-08-05 00:44:28 -04001045#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Len Brown4be44fc2005-08-05 00:44:28 -04001047acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Len Brown4be44fc2005-08-05 00:44:28 -04001049 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 case ACPI_SIGNAL_FATAL:
1051 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1052 break;
1053 case ACPI_SIGNAL_BREAKPOINT:
1054 /*
1055 * AML Breakpoint
1056 * ACPI spec. says to treat it as a NOP unless
1057 * you are debugging. So if/when we integrate
1058 * AML debugger into the kernel debugger its
1059 * hook will go here. But until then it is
1060 * not useful to print anything on breakpoints.
1061 */
1062 break;
1063 default:
1064 break;
1065 }
1066
1067 return AE_OK;
1068}
Len Brown4be44fc2005-08-05 00:44:28 -04001069
Len Brown4be44fc2005-08-05 00:44:28 -04001070static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -04001073 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 if (!str || !*str)
1076 return 0;
1077
1078 for (; count-- && str && *str; str++) {
1079 if (isalnum(*str) || *str == ' ' || *str == ':')
1080 *p++ = *str;
1081 else if (*str == '\'' || *str == '"')
1082 continue;
1083 else
1084 break;
1085 }
1086 *p = 0;
1087
1088 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092__setup("acpi_os_name=", acpi_os_name_setup);
1093
Lin Ming12d32062010-12-09 16:51:06 +08001094#define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
1095#define OSI_STRING_ENTRIES_MAX 16 /* arbitrary */
1096
1097struct osi_setup_entry {
1098 char string[OSI_STRING_LENGTH_MAX];
1099 bool enable;
1100};
1101
Shaohua Liaa165972011-07-28 13:48:43 -07001102static struct osi_setup_entry __initdata
1103 osi_setup_entries[OSI_STRING_ENTRIES_MAX] = {
1104 {"Module Device", true},
1105 {"Processor Device", true},
1106 {"3.0 _SCP Extensions", true},
1107 {"Processor Aggregator Device", true},
1108};
Lin Ming12d32062010-12-09 16:51:06 +08001109
Lin Mingd90aa922010-12-09 16:50:52 +08001110void __init acpi_osi_setup(char *str)
1111{
Lin Ming12d32062010-12-09 16:51:06 +08001112 struct osi_setup_entry *osi;
1113 bool enable = true;
1114 int i;
1115
Lin Mingd90aa922010-12-09 16:50:52 +08001116 if (!acpi_gbl_create_osi_method)
1117 return;
1118
1119 if (str == NULL || *str == '\0') {
1120 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1121 acpi_gbl_create_osi_method = FALSE;
Lin Ming12d32062010-12-09 16:51:06 +08001122 return;
1123 }
1124
1125 if (*str == '!') {
1126 str++;
1127 enable = false;
1128 }
1129
1130 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1131 osi = &osi_setup_entries[i];
1132 if (!strcmp(osi->string, str)) {
1133 osi->enable = enable;
1134 break;
1135 } else if (osi->string[0] == '\0') {
1136 osi->enable = enable;
1137 strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
1138 break;
1139 }
1140 }
Lin Mingd90aa922010-12-09 16:50:52 +08001141}
1142
Len Brownd4b7dc42008-01-23 20:50:56 -05001143static void __init set_osi_linux(unsigned int enable)
1144{
Lin Mingd90aa922010-12-09 16:50:52 +08001145 if (osi_linux.enable != enable)
Len Brownd4b7dc42008-01-23 20:50:56 -05001146 osi_linux.enable = enable;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001147
1148 if (osi_linux.enable)
1149 acpi_osi_setup("Linux");
1150 else
1151 acpi_osi_setup("!Linux");
1152
Len Brownd4b7dc42008-01-23 20:50:56 -05001153 return;
1154}
Len Brownf5076542007-05-30 00:10:38 -04001155
Len Brownd4b7dc42008-01-23 20:50:56 -05001156static void __init acpi_cmdline_osi_linux(unsigned int enable)
1157{
Lin Mingd90aa922010-12-09 16:50:52 +08001158 osi_linux.cmdline = 1; /* cmdline set the default and override DMI */
1159 osi_linux.dmi = 0;
Len Brownd4b7dc42008-01-23 20:50:56 -05001160 set_osi_linux(enable);
Len Brownf5076542007-05-30 00:10:38 -04001161
Len Brownd4b7dc42008-01-23 20:50:56 -05001162 return;
1163}
1164
1165void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1166{
Len Brownd4b7dc42008-01-23 20:50:56 -05001167 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1168
1169 if (enable == -1)
1170 return;
1171
Lin Mingd90aa922010-12-09 16:50:52 +08001172 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
Len Brownd4b7dc42008-01-23 20:50:56 -05001173 set_osi_linux(enable);
1174
Len Brownf5076542007-05-30 00:10:38 -04001175 return;
1176}
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178/*
Len Brownae00d812007-05-29 18:43:33 -04001179 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1180 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -04001182 * string starting with '!' disables that string
1183 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 */
Lin Mingb0ed7a92010-08-06 09:35:51 +08001185static void __init acpi_osi_setup_late(void)
1186{
Lin Ming12d32062010-12-09 16:51:06 +08001187 struct osi_setup_entry *osi;
1188 char *str;
1189 int i;
Lin Mingd90aa922010-12-09 16:50:52 +08001190 acpi_status status;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001191
Lin Ming12d32062010-12-09 16:51:06 +08001192 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1193 osi = &osi_setup_entries[i];
1194 str = osi->string;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001195
Lin Ming12d32062010-12-09 16:51:06 +08001196 if (*str == '\0')
1197 break;
1198 if (osi->enable) {
1199 status = acpi_install_interface(str);
Lin Mingd90aa922010-12-09 16:50:52 +08001200
Lin Ming12d32062010-12-09 16:51:06 +08001201 if (ACPI_SUCCESS(status))
1202 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1203 } else {
1204 status = acpi_remove_interface(str);
Lin Mingd90aa922010-12-09 16:50:52 +08001205
Lin Ming12d32062010-12-09 16:51:06 +08001206 if (ACPI_SUCCESS(status))
1207 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1208 }
Lin Mingb0ed7a92010-08-06 09:35:51 +08001209 }
1210}
1211
Lin Mingd90aa922010-12-09 16:50:52 +08001212static int __init osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Lin Mingd90aa922010-12-09 16:50:52 +08001214 if (str && !strcmp("Linux", str))
1215 acpi_cmdline_osi_linux(1);
1216 else if (str && !strcmp("!Linux", str))
1217 acpi_cmdline_osi_linux(0);
1218 else
1219 acpi_osi_setup(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 return 1;
1222}
1223
Lin Mingd90aa922010-12-09 16:50:52 +08001224__setup("acpi_osi=", osi_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226/* enable serialization to combat AE_ALREADY_EXISTS errors */
Len Brown4be44fc2005-08-05 00:44:28 -04001227static int __init acpi_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 printk(KERN_INFO PREFIX "serialize enabled\n");
1230
1231 acpi_gbl_all_methods_serialized = TRUE;
1232
1233 return 1;
1234}
1235
1236__setup("acpi_serialize", acpi_serialize_setup);
1237
Thomas Renningerdf92e692008-02-04 23:31:22 -08001238/* Check of resource interference between native drivers and ACPI
1239 * OperationRegions (SystemIO and System Memory only).
1240 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1241 * in arbitrary AML code and can interfere with legacy drivers.
1242 * acpi_enforce_resources= can be set to:
1243 *
Luca Tettamanti7e905602009-03-30 00:01:27 +02001244 * - strict (default) (2)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001245 * -> further driver trying to access the resources will not load
Luca Tettamanti7e905602009-03-30 00:01:27 +02001246 * - lax (1)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001247 * -> further driver trying to access the resources will load, but you
1248 * get a system message that something might go wrong...
1249 *
1250 * - no (0)
1251 * -> ACPI Operation Region resources will not be registered
1252 *
1253 */
1254#define ENFORCE_RESOURCES_STRICT 2
1255#define ENFORCE_RESOURCES_LAX 1
1256#define ENFORCE_RESOURCES_NO 0
1257
Luca Tettamanti7e905602009-03-30 00:01:27 +02001258static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001259
1260static int __init acpi_enforce_resources_setup(char *str)
1261{
1262 if (str == NULL || *str == '\0')
1263 return 0;
1264
1265 if (!strcmp("strict", str))
1266 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1267 else if (!strcmp("lax", str))
1268 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1269 else if (!strcmp("no", str))
1270 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1271
1272 return 1;
1273}
1274
1275__setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1276
1277/* Check for resource conflicts between ACPI OperationRegions and native
1278 * drivers */
Jean Delvare876fba42009-11-11 15:22:15 +01001279int acpi_check_resource_conflict(const struct resource *res)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001280{
1281 struct acpi_res_list *res_list_elem;
Thomas Renninger106d1a02010-12-20 12:11:45 +01001282 int ioport = 0, clash = 0;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001283
1284 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1285 return 0;
1286 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1287 return 0;
1288
1289 ioport = res->flags & IORESOURCE_IO;
1290
1291 spin_lock(&acpi_res_lock);
1292 list_for_each_entry(res_list_elem, &resource_list_head,
1293 resource_list) {
1294 if (ioport && (res_list_elem->resource_type
1295 != ACPI_ADR_SPACE_SYSTEM_IO))
1296 continue;
1297 if (!ioport && (res_list_elem->resource_type
1298 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1299 continue;
1300
1301 if (res->end < res_list_elem->start
1302 || res_list_elem->end < res->start)
1303 continue;
1304 clash = 1;
1305 break;
1306 }
1307 spin_unlock(&acpi_res_lock);
1308
1309 if (clash) {
1310 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
Chase Douglas1638bca2010-03-22 15:08:09 -04001311 printk(KERN_WARNING "ACPI: resource %s %pR"
Thomas Renninger106d1a02010-12-20 12:11:45 +01001312 " conflicts with ACPI region %s "
1313 "[%s 0x%zx-0x%zx]\n",
Chase Douglas1638bca2010-03-22 15:08:09 -04001314 res->name, res, res_list_elem->name,
Thomas Renninger106d1a02010-12-20 12:11:45 +01001315 (res_list_elem->resource_type ==
1316 ACPI_ADR_SPACE_SYSTEM_IO) ? "io" : "mem",
1317 (size_t) res_list_elem->start,
1318 (size_t) res_list_elem->end);
Jean Delvare14f03342009-09-08 15:31:46 +02001319 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1320 printk(KERN_NOTICE "ACPI: This conflict may"
1321 " cause random problems and system"
1322 " instability\n");
1323 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1324 " for this device, you should use it instead of"
1325 " the native driver\n");
Thomas Renningerdf92e692008-02-04 23:31:22 -08001326 }
1327 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1328 return -EBUSY;
1329 }
1330 return 0;
1331}
Thomas Renninger443dea72008-02-04 23:31:23 -08001332EXPORT_SYMBOL(acpi_check_resource_conflict);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001333
1334int acpi_check_region(resource_size_t start, resource_size_t n,
1335 const char *name)
1336{
1337 struct resource res = {
1338 .start = start,
1339 .end = start + n - 1,
1340 .name = name,
1341 .flags = IORESOURCE_IO,
1342 };
1343
1344 return acpi_check_resource_conflict(&res);
1345}
1346EXPORT_SYMBOL(acpi_check_region);
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348/*
Jean Delvare70dd6be2010-05-27 19:58:37 +02001349 * Let drivers know whether the resource checks are effective
1350 */
1351int acpi_resources_are_enforced(void)
1352{
1353 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1354}
1355EXPORT_SYMBOL(acpi_resources_are_enforced);
1356
1357/*
Lin Ming9f63b882011-03-23 17:26:34 +08001358 * Deallocate the memory for a spinlock.
1359 */
1360void acpi_os_delete_lock(acpi_spinlock handle)
1361{
1362 ACPI_FREE(handle);
1363}
1364
1365/*
Robert Moore73459f72005-06-24 00:00:00 -04001366 * Acquire a spinlock.
1367 *
1368 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001369 */
1370
Bob Moore967440e2006-06-23 17:04:00 -04001371acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001372{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001373 acpi_cpu_flags flags;
Bob Moore967440e2006-06-23 17:04:00 -04001374 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001375 return flags;
1376}
1377
1378/*
1379 * Release a spinlock. See above.
1380 */
1381
Bob Moore967440e2006-06-23 17:04:00 -04001382void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001383{
Bob Moore967440e2006-06-23 17:04:00 -04001384 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001385}
1386
Robert Moore73459f72005-06-24 00:00:00 -04001387#ifndef ACPI_USE_LOCAL_CACHE
1388
1389/*******************************************************************************
1390 *
1391 * FUNCTION: acpi_os_create_cache
1392 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001393 * PARAMETERS: name - Ascii name for the cache
1394 * size - Size of each cached object
1395 * depth - Maximum depth of the cache (in objects) <ignored>
1396 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001397 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001398 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001399 *
1400 * DESCRIPTION: Create a cache object
1401 *
1402 ******************************************************************************/
1403
1404acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001405acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001406{
Paul Mundt20c2df82007-07-20 10:11:58 +09001407 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001408 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001409 return AE_ERROR;
1410 else
1411 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001412}
1413
1414/*******************************************************************************
1415 *
1416 * FUNCTION: acpi_os_purge_cache
1417 *
1418 * PARAMETERS: Cache - Handle to cache object
1419 *
1420 * RETURN: Status
1421 *
1422 * DESCRIPTION: Free all objects within the requested cache.
1423 *
1424 ******************************************************************************/
1425
Len Brown4be44fc2005-08-05 00:44:28 -04001426acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001427{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001428 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001429 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001430}
1431
1432/*******************************************************************************
1433 *
1434 * FUNCTION: acpi_os_delete_cache
1435 *
1436 * PARAMETERS: Cache - Handle to cache object
1437 *
1438 * RETURN: Status
1439 *
1440 * DESCRIPTION: Free all objects within the requested cache and delete the
1441 * cache object.
1442 *
1443 ******************************************************************************/
1444
Len Brown4be44fc2005-08-05 00:44:28 -04001445acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001446{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001447 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001448 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001449}
1450
1451/*******************************************************************************
1452 *
1453 * FUNCTION: acpi_os_release_object
1454 *
1455 * PARAMETERS: Cache - Handle to cache object
1456 * Object - The object to be released
1457 *
1458 * RETURN: None
1459 *
1460 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1461 * the object is deleted.
1462 *
1463 ******************************************************************************/
1464
Len Brown4be44fc2005-08-05 00:44:28 -04001465acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001466{
Len Brown4be44fc2005-08-05 00:44:28 -04001467 kmem_cache_free(cache, object);
1468 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001469}
1470
Lin Minga5fe1a02009-08-13 10:43:27 +08001471static inline int acpi_res_list_add(struct acpi_res_list *res)
1472{
1473 struct acpi_res_list *res_list_elem;
1474
1475 list_for_each_entry(res_list_elem, &resource_list_head,
1476 resource_list) {
1477
1478 if (res->resource_type == res_list_elem->resource_type &&
1479 res->start == res_list_elem->start &&
1480 res->end == res_list_elem->end) {
1481
1482 /*
1483 * The Region(addr,len) already exist in the list,
1484 * just increase the count
1485 */
1486
1487 res_list_elem->count++;
1488 return 0;
1489 }
1490 }
1491
1492 res->count = 1;
1493 list_add(&res->resource_list, &resource_list_head);
1494 return 1;
1495}
1496
1497static inline void acpi_res_list_del(struct acpi_res_list *res)
1498{
1499 struct acpi_res_list *res_list_elem;
1500
1501 list_for_each_entry(res_list_elem, &resource_list_head,
1502 resource_list) {
1503
1504 if (res->resource_type == res_list_elem->resource_type &&
1505 res->start == res_list_elem->start &&
1506 res->end == res_list_elem->end) {
1507
1508 /*
1509 * If the res count is decreased to 0,
1510 * remove and free it
1511 */
1512
1513 if (--res_list_elem->count == 0) {
1514 list_del(&res_list_elem->resource_list);
1515 kfree(res_list_elem);
1516 }
1517 return;
1518 }
1519 }
1520}
1521
1522acpi_status
1523acpi_os_invalidate_address(
1524 u8 space_id,
1525 acpi_physical_address address,
1526 acpi_size length)
1527{
1528 struct acpi_res_list res;
1529
1530 switch (space_id) {
1531 case ACPI_ADR_SPACE_SYSTEM_IO:
1532 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001533 /* Only interference checks against SystemIO and SystemMemory
Lin Minga5fe1a02009-08-13 10:43:27 +08001534 are needed */
1535 res.start = address;
1536 res.end = address + length - 1;
1537 res.resource_type = space_id;
1538 spin_lock(&acpi_res_lock);
1539 acpi_res_list_del(&res);
1540 spin_unlock(&acpi_res_lock);
1541 break;
1542 case ACPI_ADR_SPACE_PCI_CONFIG:
1543 case ACPI_ADR_SPACE_EC:
1544 case ACPI_ADR_SPACE_SMBUS:
1545 case ACPI_ADR_SPACE_CMOS:
1546 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1547 case ACPI_ADR_SPACE_DATA_TABLE:
1548 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1549 break;
1550 }
1551 return AE_OK;
1552}
1553
Bob Mooreb229cf92006-04-21 17:15:00 -04001554/******************************************************************************
1555 *
1556 * FUNCTION: acpi_os_validate_address
1557 *
1558 * PARAMETERS: space_id - ACPI space ID
1559 * address - Physical address
1560 * length - Address length
1561 *
1562 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1563 * should return AE_AML_ILLEGAL_ADDRESS.
1564 *
1565 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1566 * the addresses accessed by AML operation regions.
1567 *
1568 *****************************************************************************/
1569
1570acpi_status
1571acpi_os_validate_address (
1572 u8 space_id,
1573 acpi_physical_address address,
Thomas Renningerdf92e692008-02-04 23:31:22 -08001574 acpi_size length,
1575 char *name)
Bob Mooreb229cf92006-04-21 17:15:00 -04001576{
Thomas Renningerdf92e692008-02-04 23:31:22 -08001577 struct acpi_res_list *res;
Lin Minga5fe1a02009-08-13 10:43:27 +08001578 int added;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001579 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1580 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001581
Thomas Renningerdf92e692008-02-04 23:31:22 -08001582 switch (space_id) {
1583 case ACPI_ADR_SPACE_SYSTEM_IO:
1584 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001585 /* Only interference checks against SystemIO and SystemMemory
Thomas Renningerdf92e692008-02-04 23:31:22 -08001586 are needed */
1587 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1588 if (!res)
1589 return AE_OK;
1590 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1591 strlcpy(res->name, name, 5);
1592 res->start = address;
1593 res->end = address + length - 1;
1594 res->resource_type = space_id;
1595 spin_lock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001596 added = acpi_res_list_add(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001597 spin_unlock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001598 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1599 "name: %s\n", added ? "Added" : "Already exist",
1600 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001601 ? "SystemIO" : "System Memory",
1602 (unsigned long long)res->start,
1603 (unsigned long long)res->end,
1604 res->name);
Lin Minga5fe1a02009-08-13 10:43:27 +08001605 if (!added)
1606 kfree(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001607 break;
1608 case ACPI_ADR_SPACE_PCI_CONFIG:
1609 case ACPI_ADR_SPACE_EC:
1610 case ACPI_ADR_SPACE_SMBUS:
1611 case ACPI_ADR_SPACE_CMOS:
1612 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1613 case ACPI_ADR_SPACE_DATA_TABLE:
1614 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1615 break;
1616 }
1617 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001618}
Robert Moore73459f72005-06-24 00:00:00 -04001619#endif
Myron Stowed362eda2010-10-21 14:24:04 -06001620
1621acpi_status __init acpi_os_initialize(void)
1622{
1623 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1624 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1625 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1626 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1627
1628 return AE_OK;
1629}
1630
Zhang Rui32d47ee2010-12-08 10:40:36 +08001631acpi_status __init acpi_os_initialize1(void)
Myron Stowed362eda2010-10-21 14:24:04 -06001632{
Tejun Heo44d25882011-02-01 11:42:42 +01001633 kacpid_wq = alloc_workqueue("kacpid", 0, 1);
1634 kacpi_notify_wq = alloc_workqueue("kacpi_notify", 0, 1);
1635 kacpi_hotplug_wq = alloc_workqueue("kacpi_hotplug", 0, 1);
Myron Stowed362eda2010-10-21 14:24:04 -06001636 BUG_ON(!kacpid_wq);
1637 BUG_ON(!kacpi_notify_wq);
1638 BUG_ON(!kacpi_hotplug_wq);
Len Brown1bd64d42010-10-26 14:50:56 -04001639 acpi_install_interface_handler(acpi_osi_handler);
1640 acpi_osi_setup_late();
Myron Stowed362eda2010-10-21 14:24:04 -06001641 return AE_OK;
1642}
1643
1644acpi_status acpi_os_terminate(void)
1645{
1646 if (acpi_irq_handler) {
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +01001647 acpi_os_remove_interrupt_handler(acpi_gbl_FADT.sci_interrupt,
Myron Stowed362eda2010-10-21 14:24:04 -06001648 acpi_irq_handler);
1649 }
1650
1651 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1652 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1653 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1654 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1655
1656 destroy_workqueue(kacpid_wq);
1657 destroy_workqueue(kacpi_notify_wq);
1658 destroy_workqueue(kacpi_hotplug_wq);
1659
1660 return AE_OK;
1661}