blob: f3aae4ba507eb2aee7c02e45361b81ae86cb5d99 [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
Tang Liang09f98a82011-12-09 10:05:54 +080079static int (*__acpi_os_prepare_sleep)(u8 sleep_state, u32 pm1a_ctrl,
80 u32 pm1b_ctrl);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static acpi_osd_handler acpi_irq_handler;
83static void *acpi_irq_context;
84static struct workqueue_struct *kacpid_wq;
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -040085static struct workqueue_struct *kacpi_notify_wq;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -040086struct workqueue_struct *kacpi_hotplug_wq;
87EXPORT_SYMBOL(kacpi_hotplug_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Thomas Renningerdf92e692008-02-04 23:31:22 -080089struct acpi_res_list {
90 resource_size_t start;
91 resource_size_t end;
92 acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
93 char name[5]; /* only can have a length of 4 chars, make use of this
94 one instead of res->name, no need to kalloc then */
95 struct list_head resource_list;
Lin Minga5fe1a02009-08-13 10:43:27 +080096 int count;
Thomas Renningerdf92e692008-02-04 23:31:22 -080097};
98
99static LIST_HEAD(resource_list_head);
100static DEFINE_SPINLOCK(acpi_res_lock);
101
Myron Stowe620242a2010-10-21 14:23:53 -0600102/*
103 * This list of permanent mappings is for memory that may be accessed from
104 * interrupt context, where we can't do the ioremap().
105 */
106struct acpi_ioremap {
107 struct list_head list;
108 void __iomem *virt;
109 acpi_physical_address phys;
110 acpi_size size;
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100111 unsigned long refcount;
Myron Stowe620242a2010-10-21 14:23:53 -0600112};
113
114static LIST_HEAD(acpi_ioremaps);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100115static DEFINE_MUTEX(acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600116
Lin Mingb0ed7a92010-08-06 09:35:51 +0800117static void __init acpi_osi_setup_late(void);
Len Brownae00d812007-05-29 18:43:33 -0400118
Len Brownd4b7dc42008-01-23 20:50:56 -0500119/*
Len Browna6e08872008-11-08 01:21:10 -0500120 * The story of _OSI(Linux)
Len Brownd4b7dc42008-01-23 20:50:56 -0500121 *
Len Browna6e08872008-11-08 01:21:10 -0500122 * From pre-history through Linux-2.6.22,
123 * Linux responded TRUE upon a BIOS OSI(Linux) query.
Len Brownd4b7dc42008-01-23 20:50:56 -0500124 *
Len Browna6e08872008-11-08 01:21:10 -0500125 * Unfortunately, reference BIOS writers got wind of this
126 * and put OSI(Linux) in their example code, quickly exposing
127 * this string as ill-conceived and opening the door to
128 * an un-bounded number of BIOS incompatibilities.
Len Brownd4b7dc42008-01-23 20:50:56 -0500129 *
Len Browna6e08872008-11-08 01:21:10 -0500130 * For example, OSI(Linux) was used on resume to re-POST a
131 * video card on one system, because Linux at that time
132 * could not do a speedy restore in its native driver.
133 * But then upon gaining quick native restore capability,
134 * Linux has no way to tell the BIOS to skip the time-consuming
135 * POST -- putting Linux at a permanent performance disadvantage.
136 * On another system, the BIOS writer used OSI(Linux)
137 * to infer native OS support for IPMI! On other systems,
138 * OSI(Linux) simply got in the way of Linux claiming to
139 * be compatible with other operating systems, exposing
140 * BIOS issues such as skipped device initialization.
Len Brownd4b7dc42008-01-23 20:50:56 -0500141 *
Len Browna6e08872008-11-08 01:21:10 -0500142 * So "Linux" turned out to be a really poor chose of
143 * OSI string, and from Linux-2.6.23 onward we respond FALSE.
Len Brownd4b7dc42008-01-23 20:50:56 -0500144 *
145 * BIOS writers should NOT query _OSI(Linux) on future systems.
Len Browna6e08872008-11-08 01:21:10 -0500146 * Linux will complain on the console when it sees it, and return FALSE.
147 * To get Linux to return TRUE for your system will require
148 * a kernel source update to add a DMI entry,
149 * or boot with "acpi_osi=Linux"
Len Brownd4b7dc42008-01-23 20:50:56 -0500150 */
Len Brownd4b7dc42008-01-23 20:50:56 -0500151
Adrian Bunk1d15d842008-01-29 00:10:15 +0200152static struct osi_linux {
Len Brownd4b7dc42008-01-23 20:50:56 -0500153 unsigned int enable:1;
154 unsigned int dmi:1;
155 unsigned int cmdline:1;
Lin Mingd90aa922010-12-09 16:50:52 +0800156} osi_linux = {0, 0, 0};
Len Brownf5076542007-05-30 00:10:38 -0400157
Lin Mingb0ed7a92010-08-06 09:35:51 +0800158static u32 acpi_osi_handler(acpi_string interface, u32 supported)
159{
160 if (!strcmp("Linux", interface)) {
161
Len Brown89976212011-08-02 00:45:48 -0400162 printk_once(KERN_NOTICE FW_BUG PREFIX
Lin Mingb0ed7a92010-08-06 09:35:51 +0800163 "BIOS _OSI(Linux) query %s%s\n",
164 osi_linux.enable ? "honored" : "ignored",
165 osi_linux.cmdline ? " via cmdline" :
166 osi_linux.dmi ? " via DMI" : "");
167 }
168
169 return supported;
170}
171
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700172static void __init acpi_request_region (struct acpi_generic_address *addr,
173 unsigned int length, char *desc)
174{
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700175 if (!addr->address || !length)
176 return;
177
Andi Kleencfa806f2010-07-20 15:18:36 -0700178 /* Resources are never freed */
Len Browneee3c852007-02-03 01:38:16 -0500179 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Andi Kleencfa806f2010-07-20 15:18:36 -0700180 request_region(addr->address, length, desc);
Len Browneee3c852007-02-03 01:38:16 -0500181 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
Andi Kleencfa806f2010-07-20 15:18:36 -0700182 request_mem_region(addr->address, length, desc);
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700183}
184
185static int __init acpi_reserve_resources(void)
186{
Len Browneee3c852007-02-03 01:38:16 -0500187 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700188 "ACPI PM1a_EVT_BLK");
189
Len Browneee3c852007-02-03 01:38:16 -0500190 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700191 "ACPI PM1b_EVT_BLK");
192
Len Browneee3c852007-02-03 01:38:16 -0500193 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700194 "ACPI PM1a_CNT_BLK");
195
Len Browneee3c852007-02-03 01:38:16 -0500196 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700197 "ACPI PM1b_CNT_BLK");
198
Len Browneee3c852007-02-03 01:38:16 -0500199 if (acpi_gbl_FADT.pm_timer_length == 4)
200 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700201
Len Browneee3c852007-02-03 01:38:16 -0500202 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700203 "ACPI PM2_CNT_BLK");
204
205 /* Length of GPE blocks must be a non-negative multiple of 2 */
206
Len Browneee3c852007-02-03 01:38:16 -0500207 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
208 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
209 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700210
Len Browneee3c852007-02-03 01:38:16 -0500211 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
212 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
213 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700214
215 return 0;
216}
217device_initcall(acpi_reserve_resources);
218
Len Brown4be44fc2005-08-05 00:44:28 -0400219void acpi_os_printf(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 va_list args;
222 va_start(args, fmt);
223 acpi_os_vprintf(fmt, args);
224 va_end(args);
225}
Len Brown4be44fc2005-08-05 00:44:28 -0400226
Len Brown4be44fc2005-08-05 00:44:28 -0400227void acpi_os_vprintf(const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 static char buffer[512];
Len Brown4be44fc2005-08-05 00:44:28 -0400230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 vsprintf(buffer, fmt, args);
232
233#ifdef ENABLE_DEBUGGER
234 if (acpi_in_debugger) {
235 kdb_printf("%s", buffer);
236 } else {
Frank Seidel4d939152009-02-04 17:03:07 +0100237 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239#else
Frank Seidel4d939152009-02-04 17:03:07 +0100240 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241#endif
242}
243
Takao Indoh4996c022011-07-14 18:05:21 -0400244#ifdef CONFIG_KEXEC
245static unsigned long acpi_rsdp;
246static int __init setup_acpi_rsdp(char *arg)
247{
248 acpi_rsdp = simple_strtoul(arg, NULL, 16);
249 return 0;
250}
251early_param("acpi_rsdp", setup_acpi_rsdp);
252#endif
253
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300254acpi_physical_address __init acpi_os_get_root_pointer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Takao Indoh4996c022011-07-14 18:05:21 -0400256#ifdef CONFIG_KEXEC
257 if (acpi_rsdp)
258 return acpi_rsdp;
259#endif
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (efi_enabled) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800262 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300263 return efi.acpi20;
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800264 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300265 return efi.acpi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400267 printk(KERN_ERR PREFIX
268 "System description tables not found\n");
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
Len Brown239665a2007-11-23 20:08:02 -0500271 } else {
272 acpi_physical_address pa = 0;
273
274 acpi_find_root_pointer(&pa);
275 return pa;
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600279/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe4a3cba52010-10-21 14:24:14 -0600280static struct acpi_ioremap *
281acpi_map_lookup(acpi_physical_address phys, acpi_size size)
Myron Stowe620242a2010-10-21 14:23:53 -0600282{
283 struct acpi_ioremap *map;
284
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600285 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe620242a2010-10-21 14:23:53 -0600286 if (map->phys <= phys &&
287 phys + size <= map->phys + map->size)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600288 return map;
289
290 return NULL;
291}
292
293/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
294static void __iomem *
295acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
296{
297 struct acpi_ioremap *map;
298
299 map = acpi_map_lookup(phys, size);
300 if (map)
301 return map->virt + (phys - map->phys);
Myron Stowe620242a2010-10-21 14:23:53 -0600302
303 return NULL;
304}
305
Rafael J. Wysocki13606a22011-02-08 23:38:25 +0100306void __iomem *acpi_os_get_iomem(acpi_physical_address phys, unsigned int size)
307{
308 struct acpi_ioremap *map;
309 void __iomem *virt = NULL;
310
311 mutex_lock(&acpi_ioremap_lock);
312 map = acpi_map_lookup(phys, size);
313 if (map) {
314 virt = map->virt + (phys - map->phys);
315 map->refcount++;
316 }
317 mutex_unlock(&acpi_ioremap_lock);
318 return virt;
319}
320EXPORT_SYMBOL_GPL(acpi_os_get_iomem);
321
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600322/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe620242a2010-10-21 14:23:53 -0600323static struct acpi_ioremap *
324acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
325{
326 struct acpi_ioremap *map;
327
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600328 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600329 if (map->virt <= virt &&
330 virt + size <= map->virt + map->size)
Myron Stowe620242a2010-10-21 14:23:53 -0600331 return map;
332
333 return NULL;
334}
335
Jan Beulich2fdf0742007-12-13 08:33:59 +0000336void __iomem *__init_refok
337acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100339 struct acpi_ioremap *map;
Myron Stowe620242a2010-10-21 14:23:53 -0600340 void __iomem *virt;
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100341 acpi_physical_address pg_off;
342 acpi_size pg_sz;
Myron Stowe620242a2010-10-21 14:23:53 -0600343
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800344 if (phys > ULONG_MAX) {
345 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
Randy Dunlap70c08462007-02-13 16:11:36 -0800346 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Myron Stowe620242a2010-10-21 14:23:53 -0600348
349 if (!acpi_gbl_permanent_mmap)
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300350 return __acpi_map_table((unsigned long)phys, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600351
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100352 mutex_lock(&acpi_ioremap_lock);
353 /* Check if there's a suitable mapping already. */
354 map = acpi_map_lookup(phys, size);
355 if (map) {
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100356 map->refcount++;
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100357 goto out;
358 }
359
Myron Stowe620242a2010-10-21 14:23:53 -0600360 map = kzalloc(sizeof(*map), GFP_KERNEL);
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100361 if (!map) {
362 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600363 return NULL;
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100364 }
Myron Stowe620242a2010-10-21 14:23:53 -0600365
Myron Stowe4a3cba52010-10-21 14:24:14 -0600366 pg_off = round_down(phys, PAGE_SIZE);
367 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100368 virt = acpi_os_ioremap(pg_off, pg_sz);
Myron Stowe620242a2010-10-21 14:23:53 -0600369 if (!virt) {
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100370 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600371 kfree(map);
372 return NULL;
373 }
374
375 INIT_LIST_HEAD(&map->list);
376 map->virt = virt;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600377 map->phys = pg_off;
378 map->size = pg_sz;
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100379 map->refcount = 1;
Myron Stowe620242a2010-10-21 14:23:53 -0600380
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600381 list_add_tail_rcu(&map->list, &acpi_ioremaps);
Myron Stowe620242a2010-10-21 14:23:53 -0600382
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100383 out:
384 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600385 return map->virt + (phys - map->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800387EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100389static void acpi_os_drop_map_ref(struct acpi_ioremap *map)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600390{
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100391 if (!--map->refcount)
392 list_del_rcu(&map->list);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600393}
Myron Stowe4a3cba52010-10-21 14:24:14 -0600394
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100395static void acpi_os_map_cleanup(struct acpi_ioremap *map)
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100396{
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100397 if (!map->refcount) {
398 synchronize_rcu();
399 iounmap(map->virt);
400 kfree(map);
401 }
Myron Stowe4a3cba52010-10-21 14:24:14 -0600402}
403
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800404void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Myron Stowe620242a2010-10-21 14:23:53 -0600406 struct acpi_ioremap *map;
Myron Stowe620242a2010-10-21 14:23:53 -0600407
408 if (!acpi_gbl_permanent_mmap) {
Yinghai Lu7d972772009-02-07 15:39:41 -0800409 __acpi_unmap_table(virt, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600410 return;
411 }
412
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100413 mutex_lock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600414 map = acpi_map_lookup_virt(virt, size);
415 if (!map) {
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100416 mutex_unlock(&acpi_ioremap_lock);
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100417 WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
Myron Stowe620242a2010-10-21 14:23:53 -0600418 return;
419 }
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100420 acpi_os_drop_map_ref(map);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100421 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600422
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100423 acpi_os_map_cleanup(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800425EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800427void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Yinghai Lu7d972772009-02-07 15:39:41 -0800428{
429 if (!acpi_gbl_permanent_mmap)
430 __acpi_unmap_table(virt, size);
431}
432
Rafael J. Wysocki073b49642011-02-08 23:37:31 +0100433static int acpi_os_map_generic_address(struct acpi_generic_address *addr)
Myron Stowe29718522010-10-21 14:23:59 -0600434{
435 void __iomem *virt;
436
437 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
438 return 0;
439
440 if (!addr->address || !addr->bit_width)
441 return -EINVAL;
442
443 virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
444 if (!virt)
445 return -EIO;
446
447 return 0;
448}
Myron Stowe29718522010-10-21 14:23:59 -0600449
Rafael J. Wysocki073b49642011-02-08 23:37:31 +0100450static void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
Myron Stowe29718522010-10-21 14:23:59 -0600451{
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100452 struct acpi_ioremap *map;
Myron Stowe29718522010-10-21 14:23:59 -0600453
454 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
455 return;
456
457 if (!addr->address || !addr->bit_width)
458 return;
459
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100460 mutex_lock(&acpi_ioremap_lock);
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100461 map = acpi_map_lookup(addr->address, addr->bit_width / 8);
462 if (!map) {
463 mutex_unlock(&acpi_ioremap_lock);
464 return;
465 }
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100466 acpi_os_drop_map_ref(map);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100467 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe29718522010-10-21 14:23:59 -0600468
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100469 acpi_os_map_cleanup(map);
Myron Stowe29718522010-10-21 14:23:59 -0600470}
Myron Stowe29718522010-10-21 14:23:59 -0600471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#ifdef ACPI_FUTURE_USAGE
473acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400474acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Len Brown4be44fc2005-08-05 00:44:28 -0400476 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return AE_BAD_PARAMETER;
478
479 *phys = virt_to_phys(virt);
480
481 return AE_OK;
482}
483#endif
484
485#define ACPI_MAX_OVERRIDE_LEN 100
486
487static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
488
489acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400490acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
491 acpi_string * new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 if (!init_val || !new_val)
494 return AE_BAD_PARAMETER;
495
496 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400497 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400499 acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 *new_val = acpi_os_name;
501 }
502
503 return AE_OK;
504}
505
506acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400507acpi_os_table_override(struct acpi_table_header * existing_table,
508 struct acpi_table_header ** new_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 if (!existing_table || !new_table)
511 return AE_BAD_PARAMETER;
512
Markus Gaugusch71fc47a2008-02-05 00:04:06 +0100513 *new_table = NULL;
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515#ifdef CONFIG_ACPI_CUSTOM_DSDT
516 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400517 *new_table = (struct acpi_table_header *)AmlCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518#endif
Éric Piel6ed31e92008-02-05 00:04:50 +0100519 if (*new_table != NULL) {
520 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
521 "this is unsafe: tainting kernel\n",
522 existing_table->signature,
523 existing_table->oem_table_id);
524 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return AE_OK;
527}
528
David Howells7d12e782006-10-05 14:55:46 +0100529static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Len Brown5229e872008-02-06 01:26:55 -0500531 u32 handled;
532
533 handled = (*acpi_irq_handler) (acpi_irq_context);
534
535 if (handled) {
536 acpi_irq_handled++;
537 return IRQ_HANDLED;
Len Brown88bea182009-04-21 00:35:47 -0400538 } else {
539 acpi_irq_not_handled++;
Len Brown5229e872008-02-06 01:26:55 -0500540 return IRQ_NONE;
Len Brown88bea182009-04-21 00:35:47 -0400541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400545acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
546 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 unsigned int irq;
549
Len Brown5229e872008-02-06 01:26:55 -0500550 acpi_irq_stats_init();
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /*
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100553 * ACPI interrupts different from the SCI in our copy of the FADT are
554 * not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 */
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100556 if (gsi != acpi_gbl_FADT.sci_interrupt)
557 return AE_BAD_PARAMETER;
558
559 if (acpi_irq_handler)
560 return AE_ALREADY_ACQUIRED;
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
563 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
564 gsi);
565 return AE_OK;
566 }
567
568 acpi_irq_handler = handler;
569 acpi_irq_context = context;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700570 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100572 acpi_irq_handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return AE_NOT_ACQUIRED;
574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 return AE_OK;
577}
578
Len Brown4be44fc2005-08-05 00:44:28 -0400579acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100581 if (irq != acpi_gbl_FADT.sci_interrupt)
582 return AE_BAD_PARAMETER;
583
584 free_irq(irq, acpi_irq);
585 acpi_irq_handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 return AE_OK;
588}
589
590/*
591 * Running in interpreter thread context, safe to sleep
592 */
593
Lin Ming439913f2010-01-28 10:53:19 +0800594void acpi_os_sleep(u64 ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800596 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
Len Brown4be44fc2005-08-05 00:44:28 -0400598
Len Brown4be44fc2005-08-05 00:44:28 -0400599void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 while (us) {
602 u32 delay = 1000;
603
604 if (delay > us)
605 delay = us;
606 udelay(delay);
607 touch_nmi_watchdog();
608 us -= delay;
609 }
610}
Len Brown4be44fc2005-08-05 00:44:28 -0400611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/*
613 * Support ACPI 3.0 AML Timer operand
614 * Returns 64-bit free-running, monotonically increasing timer
615 * with 100ns granularity
616 */
Len Brown4be44fc2005-08-05 00:44:28 -0400617u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 static u64 t;
620
621#ifdef CONFIG_HPET
622 /* TBD: use HPET if available */
623#endif
624
625#ifdef CONFIG_X86_PM_TIMER
626 /* TBD: default to PM timer if HPET was not available */
627#endif
628 if (!t)
629 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
630
631 return ++t;
632}
633
Len Brown4be44fc2005-08-05 00:44:28 -0400634acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 u32 dummy;
637
638 if (!value)
639 value = &dummy;
640
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800641 *value = 0;
642 if (width <= 8) {
Len Brown4be44fc2005-08-05 00:44:28 -0400643 *(u8 *) value = inb(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800644 } else if (width <= 16) {
Len Brown4be44fc2005-08-05 00:44:28 -0400645 *(u16 *) value = inw(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800646 } else if (width <= 32) {
Len Brown4be44fc2005-08-05 00:44:28 -0400647 *(u32 *) value = inl(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800648 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 BUG();
650 }
651
652 return AE_OK;
653}
Len Brown4be44fc2005-08-05 00:44:28 -0400654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655EXPORT_SYMBOL(acpi_os_read_port);
656
Len Brown4be44fc2005-08-05 00:44:28 -0400657acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800659 if (width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 outb(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800661 } else if (width <= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 outw(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800663 } else if (width <= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 outl(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800665 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 BUG();
667 }
668
669 return AE_OK;
670}
Len Brown4be44fc2005-08-05 00:44:28 -0400671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672EXPORT_SYMBOL(acpi_os_write_port);
673
674acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400675acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Len Brown4be44fc2005-08-05 00:44:28 -0400677 void __iomem *virt_addr;
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100678 unsigned int size = width / 8;
679 bool unmap = false;
680 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600682 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600683 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600684 if (!virt_addr) {
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100685 rcu_read_unlock();
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100686 virt_addr = acpi_os_ioremap(phys_addr, size);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100687 if (!virt_addr)
688 return AE_BAD_ADDRESS;
689 unmap = true;
Myron Stowe620242a2010-10-21 14:23:53 -0600690 }
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (!value)
693 value = &dummy;
694
695 switch (width) {
696 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400697 *(u8 *) value = readb(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 break;
699 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400700 *(u16 *) value = readw(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
702 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400703 *(u32 *) value = readl(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 break;
705 default:
706 BUG();
707 }
708
Myron Stowe620242a2010-10-21 14:23:53 -0600709 if (unmap)
710 iounmap(virt_addr);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100711 else
712 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 return AE_OK;
715}
716
717acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400718acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Len Brown4be44fc2005-08-05 00:44:28 -0400720 void __iomem *virt_addr;
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100721 unsigned int size = width / 8;
722 bool unmap = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600724 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600725 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600726 if (!virt_addr) {
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100727 rcu_read_unlock();
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100728 virt_addr = acpi_os_ioremap(phys_addr, size);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100729 if (!virt_addr)
730 return AE_BAD_ADDRESS;
731 unmap = true;
Myron Stowe620242a2010-10-21 14:23:53 -0600732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 switch (width) {
735 case 8:
736 writeb(value, virt_addr);
737 break;
738 case 16:
739 writew(value, virt_addr);
740 break;
741 case 32:
742 writel(value, virt_addr);
743 break;
744 default:
745 BUG();
746 }
747
Myron Stowe620242a2010-10-21 14:23:53 -0600748 if (unmap)
749 iounmap(virt_addr);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100750 else
751 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 return AE_OK;
754}
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400757acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Bob Moorec5f02312010-08-06 08:57:53 +0800758 u64 *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 int result, size;
Bob Moorec5f02312010-08-06 08:57:53 +0800761 u32 value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 if (!value)
764 return AE_BAD_PARAMETER;
765
766 switch (width) {
767 case 8:
768 size = 1;
769 break;
770 case 16:
771 size = 2;
772 break;
773 case 32:
774 size = 4;
775 break;
776 default:
777 return AE_ERROR;
778 }
779
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500780 result = raw_pci_read(pci_id->segment, pci_id->bus,
781 PCI_DEVFN(pci_id->device, pci_id->function),
Bob Moorec5f02312010-08-06 08:57:53 +0800782 reg, size, &value32);
783 *value = value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 return (result ? AE_ERROR : AE_OK);
786}
Len Brown4be44fc2005-08-05 00:44:28 -0400787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400789acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Lin Ming439913f2010-01-28 10:53:19 +0800790 u64 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 int result, size;
793
794 switch (width) {
795 case 8:
796 size = 1;
797 break;
798 case 16:
799 size = 2;
800 break;
801 case 32:
802 size = 4;
803 break;
804 default:
805 return AE_ERROR;
806 }
807
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500808 result = raw_pci_write(pci_id->segment, pci_id->bus,
809 PCI_DEVFN(pci_id->device, pci_id->function),
810 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 return (result ? AE_ERROR : AE_OK);
813}
814
David Howells65f27f32006-11-22 14:55:48 +0000815static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
David Howells65f27f32006-11-22 14:55:48 +0000817 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400818
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000819 if (dpc->wait)
820 acpi_os_wait_events_complete(NULL);
Zhang Rui19cd8472008-08-28 10:05:06 +0800821
822 dpc->function(dpc->context);
823 kfree(dpc);
Zhang Rui19cd8472008-08-28 10:05:06 +0800824}
825
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400826/*******************************************************************************
827 *
828 * FUNCTION: acpi_os_execute
829 *
830 * PARAMETERS: Type - Type of the callback
831 * Function - Function to be executed
832 * Context - Function parameters
833 *
834 * RETURN: Status
835 *
836 * DESCRIPTION: Depending on type, either queues function for deferred execution or
837 * immediately executes function on a separate thread.
838 *
839 ******************************************************************************/
840
Zhang Rui19cd8472008-08-28 10:05:06 +0800841static acpi_status __acpi_os_execute(acpi_execute_type type,
842 acpi_osd_exec_callback function, void *context, int hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Len Brown4be44fc2005-08-05 00:44:28 -0400844 acpi_status status = AE_OK;
845 struct acpi_os_dpc *dpc;
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300846 struct workqueue_struct *queue;
Zhang Rui19cd8472008-08-28 10:05:06 +0800847 int ret;
Len Brown72945b22006-07-12 22:46:42 -0400848 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
849 "Scheduling function [%p(%p)] for deferred execution.\n",
850 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /*
853 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +0000854 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 * way that allows us to also free its memory inside the callee.
856 * Because we may want to schedule several tasks with different
857 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +0000858 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 */
Len Brown72945b22006-07-12 22:46:42 -0400860
David Howells65f27f32006-11-22 14:55:48 +0000861 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (!dpc)
Lin Ming889c78b2008-12-31 09:23:57 +0800863 return AE_NO_MEMORY;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 dpc->function = function;
866 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800867
Zhang Ruic02256b2009-06-23 10:20:29 +0800868 /*
869 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
870 * because the hotplug code may call driver .remove() functions,
871 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
872 * to flush these workqueues.
873 */
874 queue = hp ? kacpi_hotplug_wq :
875 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000876 dpc->wait = hp ? 1 : 0;
Zhang Ruibc736752010-03-22 15:48:54 +0800877
878 if (queue == kacpi_hotplug_wq)
879 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
880 else if (queue == kacpi_notify_wq)
881 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
882 else
883 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
884
Tejun Heo8fec62b2010-06-29 10:07:09 +0200885 /*
886 * On some machines, a software-initiated SMI causes corruption unless
887 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
888 * typically it's done in GPE-related methods that are run via
889 * workqueues, so we can avoid the known corruption cases by always
890 * queueing on CPU 0.
891 */
892 ret = queue_work_on(0, queue, &dpc->work);
Zhang Rui19cd8472008-08-28 10:05:06 +0800893
894 if (!ret) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800895 printk(KERN_ERR PREFIX
896 "Call to queue_work() failed.\n");
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300897 status = AE_ERROR;
898 kfree(dpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Lin Ming889c78b2008-12-31 09:23:57 +0800900 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
Len Brown4be44fc2005-08-05 00:44:28 -0400902
Zhang Rui19cd8472008-08-28 10:05:06 +0800903acpi_status acpi_os_execute(acpi_execute_type type,
904 acpi_osd_exec_callback function, void *context)
905{
906 return __acpi_os_execute(type, function, context, 0);
907}
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400908EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Zhang Rui19cd8472008-08-28 10:05:06 +0800910acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
911 void *context)
912{
913 return __acpi_os_execute(0, function, context, 1);
914}
915
Len Brown4be44fc2005-08-05 00:44:28 -0400916void acpi_os_wait_events_complete(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 flush_workqueue(kacpid_wq);
Zhang Rui2f67a062008-04-29 02:34:42 -0400919 flush_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
Len Brown4be44fc2005-08-05 00:44:28 -0400921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922EXPORT_SYMBOL(acpi_os_wait_events_complete);
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400925acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Len Brown4be44fc2005-08-05 00:44:28 -0400927 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 sem = acpi_os_allocate(sizeof(struct semaphore));
930 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400931 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 memset(sem, 0, sizeof(struct semaphore));
933
934 sema_init(sem, initial_units);
935
Len Brown4be44fc2005-08-05 00:44:28 -0400936 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Len Brown4be44fc2005-08-05 00:44:28 -0400938 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
939 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Patrick Mocheld550d982006-06-27 00:41:40 -0400941 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/*
945 * TODO: A better way to delete semaphores? Linux doesn't have a
946 * 'delete_semaphore()' function -- may result in an invalid
947 * pointer dereference for non-synchronized consumers. Should
948 * we at least check for blocked threads and signal/cancel them?
949 */
950
Len Brown4be44fc2005-08-05 00:44:28 -0400951acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Len Brown4be44fc2005-08-05 00:44:28 -0400953 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400956 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Len Brown4be44fc2005-08-05 00:44:28 -0400958 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400960 BUG_ON(!list_empty(&sem->wait_list));
Len Brown02438d82006-06-30 03:19:10 -0400961 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -0400962 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Patrick Mocheld550d982006-06-27 00:41:40 -0400964 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 * TODO: Support for units > 1?
969 */
Len Brown4be44fc2005-08-05 00:44:28 -0400970acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Len Brown4be44fc2005-08-05 00:44:28 -0400972 acpi_status status = AE_OK;
973 struct semaphore *sem = (struct semaphore *)handle;
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400974 long jiffies;
Len Brown4be44fc2005-08-05 00:44:28 -0400975 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400978 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400981 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Len Brown4be44fc2005-08-05 00:44:28 -0400983 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
984 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400986 if (timeout == ACPI_WAIT_FOREVER)
987 jiffies = MAX_SCHEDULE_TIMEOUT;
988 else
989 jiffies = msecs_to_jiffies(timeout);
990
991 ret = down_timeout(sem, jiffies);
992 if (ret)
993 status = AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -0400996 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400997 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -0400998 handle, units, timeout,
999 acpi_format_exception(status)));
1000 } else {
1001 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -04001002 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001003 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005
Patrick Mocheld550d982006-06-27 00:41:40 -04001006 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009/*
1010 * TODO: Support for units > 1?
1011 */
Len Brown4be44fc2005-08-05 00:44:28 -04001012acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Len Brown4be44fc2005-08-05 00:44:28 -04001014 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001017 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -04001020 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Len Brown4be44fc2005-08-05 00:44:28 -04001022 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1023 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 up(sem);
1026
Patrick Mocheld550d982006-06-27 00:41:40 -04001027 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
Len Brown4be44fc2005-08-05 00:44:28 -04001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -04001031u32 acpi_os_get_line(char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033
1034#ifdef ENABLE_DEBUGGER
1035 if (acpi_in_debugger) {
1036 u32 chars;
1037
1038 kdb_read(buffer, sizeof(line_buf));
1039
1040 /* remove the CR kdb includes */
1041 chars = strlen(buffer) - 1;
1042 buffer[chars] = '\0';
1043 }
1044#endif
1045
1046 return 0;
1047}
Len Brown4be44fc2005-08-05 00:44:28 -04001048#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Len Brown4be44fc2005-08-05 00:44:28 -04001050acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Len Brown4be44fc2005-08-05 00:44:28 -04001052 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 case ACPI_SIGNAL_FATAL:
1054 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1055 break;
1056 case ACPI_SIGNAL_BREAKPOINT:
1057 /*
1058 * AML Breakpoint
1059 * ACPI spec. says to treat it as a NOP unless
1060 * you are debugging. So if/when we integrate
1061 * AML debugger into the kernel debugger its
1062 * hook will go here. But until then it is
1063 * not useful to print anything on breakpoints.
1064 */
1065 break;
1066 default:
1067 break;
1068 }
1069
1070 return AE_OK;
1071}
Len Brown4be44fc2005-08-05 00:44:28 -04001072
Len Brown4be44fc2005-08-05 00:44:28 -04001073static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -04001076 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 if (!str || !*str)
1079 return 0;
1080
1081 for (; count-- && str && *str; str++) {
1082 if (isalnum(*str) || *str == ' ' || *str == ':')
1083 *p++ = *str;
1084 else if (*str == '\'' || *str == '"')
1085 continue;
1086 else
1087 break;
1088 }
1089 *p = 0;
1090
1091 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
1095__setup("acpi_os_name=", acpi_os_name_setup);
1096
Lin Ming12d32062010-12-09 16:51:06 +08001097#define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
1098#define OSI_STRING_ENTRIES_MAX 16 /* arbitrary */
1099
1100struct osi_setup_entry {
1101 char string[OSI_STRING_LENGTH_MAX];
1102 bool enable;
1103};
1104
Shaohua Liaa165972011-07-28 13:48:43 -07001105static struct osi_setup_entry __initdata
1106 osi_setup_entries[OSI_STRING_ENTRIES_MAX] = {
1107 {"Module Device", true},
1108 {"Processor Device", true},
1109 {"3.0 _SCP Extensions", true},
1110 {"Processor Aggregator Device", true},
1111};
Lin Ming12d32062010-12-09 16:51:06 +08001112
Lin Mingd90aa922010-12-09 16:50:52 +08001113void __init acpi_osi_setup(char *str)
1114{
Lin Ming12d32062010-12-09 16:51:06 +08001115 struct osi_setup_entry *osi;
1116 bool enable = true;
1117 int i;
1118
Lin Mingd90aa922010-12-09 16:50:52 +08001119 if (!acpi_gbl_create_osi_method)
1120 return;
1121
1122 if (str == NULL || *str == '\0') {
1123 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1124 acpi_gbl_create_osi_method = FALSE;
Lin Ming12d32062010-12-09 16:51:06 +08001125 return;
1126 }
1127
1128 if (*str == '!') {
1129 str++;
1130 enable = false;
1131 }
1132
1133 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1134 osi = &osi_setup_entries[i];
1135 if (!strcmp(osi->string, str)) {
1136 osi->enable = enable;
1137 break;
1138 } else if (osi->string[0] == '\0') {
1139 osi->enable = enable;
1140 strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
1141 break;
1142 }
1143 }
Lin Mingd90aa922010-12-09 16:50:52 +08001144}
1145
Len Brownd4b7dc42008-01-23 20:50:56 -05001146static void __init set_osi_linux(unsigned int enable)
1147{
Lin Mingd90aa922010-12-09 16:50:52 +08001148 if (osi_linux.enable != enable)
Len Brownd4b7dc42008-01-23 20:50:56 -05001149 osi_linux.enable = enable;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001150
1151 if (osi_linux.enable)
1152 acpi_osi_setup("Linux");
1153 else
1154 acpi_osi_setup("!Linux");
1155
Len Brownd4b7dc42008-01-23 20:50:56 -05001156 return;
1157}
Len Brownf5076542007-05-30 00:10:38 -04001158
Len Brownd4b7dc42008-01-23 20:50:56 -05001159static void __init acpi_cmdline_osi_linux(unsigned int enable)
1160{
Lin Mingd90aa922010-12-09 16:50:52 +08001161 osi_linux.cmdline = 1; /* cmdline set the default and override DMI */
1162 osi_linux.dmi = 0;
Len Brownd4b7dc42008-01-23 20:50:56 -05001163 set_osi_linux(enable);
Len Brownf5076542007-05-30 00:10:38 -04001164
Len Brownd4b7dc42008-01-23 20:50:56 -05001165 return;
1166}
1167
1168void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1169{
Len Brownd4b7dc42008-01-23 20:50:56 -05001170 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1171
1172 if (enable == -1)
1173 return;
1174
Lin Mingd90aa922010-12-09 16:50:52 +08001175 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
Len Brownd4b7dc42008-01-23 20:50:56 -05001176 set_osi_linux(enable);
1177
Len Brownf5076542007-05-30 00:10:38 -04001178 return;
1179}
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181/*
Len Brownae00d812007-05-29 18:43:33 -04001182 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1183 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -04001185 * string starting with '!' disables that string
1186 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 */
Lin Mingb0ed7a92010-08-06 09:35:51 +08001188static void __init acpi_osi_setup_late(void)
1189{
Lin Ming12d32062010-12-09 16:51:06 +08001190 struct osi_setup_entry *osi;
1191 char *str;
1192 int i;
Lin Mingd90aa922010-12-09 16:50:52 +08001193 acpi_status status;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001194
Lin Ming12d32062010-12-09 16:51:06 +08001195 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1196 osi = &osi_setup_entries[i];
1197 str = osi->string;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001198
Lin Ming12d32062010-12-09 16:51:06 +08001199 if (*str == '\0')
1200 break;
1201 if (osi->enable) {
1202 status = acpi_install_interface(str);
Lin Mingd90aa922010-12-09 16:50:52 +08001203
Lin Ming12d32062010-12-09 16:51:06 +08001204 if (ACPI_SUCCESS(status))
1205 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1206 } else {
1207 status = acpi_remove_interface(str);
Lin Mingd90aa922010-12-09 16:50:52 +08001208
Lin Ming12d32062010-12-09 16:51:06 +08001209 if (ACPI_SUCCESS(status))
1210 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1211 }
Lin Mingb0ed7a92010-08-06 09:35:51 +08001212 }
1213}
1214
Lin Mingd90aa922010-12-09 16:50:52 +08001215static int __init osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Lin Mingd90aa922010-12-09 16:50:52 +08001217 if (str && !strcmp("Linux", str))
1218 acpi_cmdline_osi_linux(1);
1219 else if (str && !strcmp("!Linux", str))
1220 acpi_cmdline_osi_linux(0);
1221 else
1222 acpi_osi_setup(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 return 1;
1225}
1226
Lin Mingd90aa922010-12-09 16:50:52 +08001227__setup("acpi_osi=", osi_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229/* enable serialization to combat AE_ALREADY_EXISTS errors */
Len Brown4be44fc2005-08-05 00:44:28 -04001230static int __init acpi_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 printk(KERN_INFO PREFIX "serialize enabled\n");
1233
1234 acpi_gbl_all_methods_serialized = TRUE;
1235
1236 return 1;
1237}
1238
1239__setup("acpi_serialize", acpi_serialize_setup);
1240
Thomas Renningerdf92e692008-02-04 23:31:22 -08001241/* Check of resource interference between native drivers and ACPI
1242 * OperationRegions (SystemIO and System Memory only).
1243 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1244 * in arbitrary AML code and can interfere with legacy drivers.
1245 * acpi_enforce_resources= can be set to:
1246 *
Luca Tettamanti7e905602009-03-30 00:01:27 +02001247 * - strict (default) (2)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001248 * -> further driver trying to access the resources will not load
Luca Tettamanti7e905602009-03-30 00:01:27 +02001249 * - lax (1)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001250 * -> further driver trying to access the resources will load, but you
1251 * get a system message that something might go wrong...
1252 *
1253 * - no (0)
1254 * -> ACPI Operation Region resources will not be registered
1255 *
1256 */
1257#define ENFORCE_RESOURCES_STRICT 2
1258#define ENFORCE_RESOURCES_LAX 1
1259#define ENFORCE_RESOURCES_NO 0
1260
Luca Tettamanti7e905602009-03-30 00:01:27 +02001261static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001262
1263static int __init acpi_enforce_resources_setup(char *str)
1264{
1265 if (str == NULL || *str == '\0')
1266 return 0;
1267
1268 if (!strcmp("strict", str))
1269 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1270 else if (!strcmp("lax", str))
1271 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1272 else if (!strcmp("no", str))
1273 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1274
1275 return 1;
1276}
1277
1278__setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1279
1280/* Check for resource conflicts between ACPI OperationRegions and native
1281 * drivers */
Jean Delvare876fba42009-11-11 15:22:15 +01001282int acpi_check_resource_conflict(const struct resource *res)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001283{
1284 struct acpi_res_list *res_list_elem;
Thomas Renninger106d1a02010-12-20 12:11:45 +01001285 int ioport = 0, clash = 0;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001286
1287 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1288 return 0;
1289 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1290 return 0;
1291
1292 ioport = res->flags & IORESOURCE_IO;
1293
1294 spin_lock(&acpi_res_lock);
1295 list_for_each_entry(res_list_elem, &resource_list_head,
1296 resource_list) {
1297 if (ioport && (res_list_elem->resource_type
1298 != ACPI_ADR_SPACE_SYSTEM_IO))
1299 continue;
1300 if (!ioport && (res_list_elem->resource_type
1301 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1302 continue;
1303
1304 if (res->end < res_list_elem->start
1305 || res_list_elem->end < res->start)
1306 continue;
1307 clash = 1;
1308 break;
1309 }
1310 spin_unlock(&acpi_res_lock);
1311
1312 if (clash) {
1313 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
Chase Douglas1638bca2010-03-22 15:08:09 -04001314 printk(KERN_WARNING "ACPI: resource %s %pR"
Thomas Renninger106d1a02010-12-20 12:11:45 +01001315 " conflicts with ACPI region %s "
1316 "[%s 0x%zx-0x%zx]\n",
Chase Douglas1638bca2010-03-22 15:08:09 -04001317 res->name, res, res_list_elem->name,
Thomas Renninger106d1a02010-12-20 12:11:45 +01001318 (res_list_elem->resource_type ==
1319 ACPI_ADR_SPACE_SYSTEM_IO) ? "io" : "mem",
1320 (size_t) res_list_elem->start,
1321 (size_t) res_list_elem->end);
Jean Delvare14f03342009-09-08 15:31:46 +02001322 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1323 printk(KERN_NOTICE "ACPI: This conflict may"
1324 " cause random problems and system"
1325 " instability\n");
1326 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1327 " for this device, you should use it instead of"
1328 " the native driver\n");
Thomas Renningerdf92e692008-02-04 23:31:22 -08001329 }
1330 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1331 return -EBUSY;
1332 }
1333 return 0;
1334}
Thomas Renninger443dea72008-02-04 23:31:23 -08001335EXPORT_SYMBOL(acpi_check_resource_conflict);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001336
1337int acpi_check_region(resource_size_t start, resource_size_t n,
1338 const char *name)
1339{
1340 struct resource res = {
1341 .start = start,
1342 .end = start + n - 1,
1343 .name = name,
1344 .flags = IORESOURCE_IO,
1345 };
1346
1347 return acpi_check_resource_conflict(&res);
1348}
1349EXPORT_SYMBOL(acpi_check_region);
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351/*
Jean Delvare70dd6be2010-05-27 19:58:37 +02001352 * Let drivers know whether the resource checks are effective
1353 */
1354int acpi_resources_are_enforced(void)
1355{
1356 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1357}
1358EXPORT_SYMBOL(acpi_resources_are_enforced);
1359
1360/*
Lin Ming9f63b882011-03-23 17:26:34 +08001361 * Deallocate the memory for a spinlock.
1362 */
1363void acpi_os_delete_lock(acpi_spinlock handle)
1364{
1365 ACPI_FREE(handle);
1366}
1367
1368/*
Robert Moore73459f72005-06-24 00:00:00 -04001369 * Acquire a spinlock.
1370 *
1371 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001372 */
1373
Bob Moore967440e2006-06-23 17:04:00 -04001374acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001375{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001376 acpi_cpu_flags flags;
Bob Moore967440e2006-06-23 17:04:00 -04001377 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001378 return flags;
1379}
1380
1381/*
1382 * Release a spinlock. See above.
1383 */
1384
Bob Moore967440e2006-06-23 17:04:00 -04001385void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001386{
Bob Moore967440e2006-06-23 17:04:00 -04001387 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001388}
1389
Robert Moore73459f72005-06-24 00:00:00 -04001390#ifndef ACPI_USE_LOCAL_CACHE
1391
1392/*******************************************************************************
1393 *
1394 * FUNCTION: acpi_os_create_cache
1395 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001396 * PARAMETERS: name - Ascii name for the cache
1397 * size - Size of each cached object
1398 * depth - Maximum depth of the cache (in objects) <ignored>
1399 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001400 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001401 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001402 *
1403 * DESCRIPTION: Create a cache object
1404 *
1405 ******************************************************************************/
1406
1407acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001408acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001409{
Paul Mundt20c2df82007-07-20 10:11:58 +09001410 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001411 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001412 return AE_ERROR;
1413 else
1414 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001415}
1416
1417/*******************************************************************************
1418 *
1419 * FUNCTION: acpi_os_purge_cache
1420 *
1421 * PARAMETERS: Cache - Handle to cache object
1422 *
1423 * RETURN: Status
1424 *
1425 * DESCRIPTION: Free all objects within the requested cache.
1426 *
1427 ******************************************************************************/
1428
Len Brown4be44fc2005-08-05 00:44:28 -04001429acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001430{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001431 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001432 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001433}
1434
1435/*******************************************************************************
1436 *
1437 * FUNCTION: acpi_os_delete_cache
1438 *
1439 * PARAMETERS: Cache - Handle to cache object
1440 *
1441 * RETURN: Status
1442 *
1443 * DESCRIPTION: Free all objects within the requested cache and delete the
1444 * cache object.
1445 *
1446 ******************************************************************************/
1447
Len Brown4be44fc2005-08-05 00:44:28 -04001448acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001449{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001450 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001451 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001452}
1453
1454/*******************************************************************************
1455 *
1456 * FUNCTION: acpi_os_release_object
1457 *
1458 * PARAMETERS: Cache - Handle to cache object
1459 * Object - The object to be released
1460 *
1461 * RETURN: None
1462 *
1463 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1464 * the object is deleted.
1465 *
1466 ******************************************************************************/
1467
Len Brown4be44fc2005-08-05 00:44:28 -04001468acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001469{
Len Brown4be44fc2005-08-05 00:44:28 -04001470 kmem_cache_free(cache, object);
1471 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001472}
1473
Lin Minga5fe1a02009-08-13 10:43:27 +08001474static inline int acpi_res_list_add(struct acpi_res_list *res)
1475{
1476 struct acpi_res_list *res_list_elem;
1477
1478 list_for_each_entry(res_list_elem, &resource_list_head,
1479 resource_list) {
1480
1481 if (res->resource_type == res_list_elem->resource_type &&
1482 res->start == res_list_elem->start &&
1483 res->end == res_list_elem->end) {
1484
1485 /*
1486 * The Region(addr,len) already exist in the list,
1487 * just increase the count
1488 */
1489
1490 res_list_elem->count++;
1491 return 0;
1492 }
1493 }
1494
1495 res->count = 1;
1496 list_add(&res->resource_list, &resource_list_head);
1497 return 1;
1498}
1499
1500static inline void acpi_res_list_del(struct acpi_res_list *res)
1501{
1502 struct acpi_res_list *res_list_elem;
1503
1504 list_for_each_entry(res_list_elem, &resource_list_head,
1505 resource_list) {
1506
1507 if (res->resource_type == res_list_elem->resource_type &&
1508 res->start == res_list_elem->start &&
1509 res->end == res_list_elem->end) {
1510
1511 /*
1512 * If the res count is decreased to 0,
1513 * remove and free it
1514 */
1515
1516 if (--res_list_elem->count == 0) {
1517 list_del(&res_list_elem->resource_list);
1518 kfree(res_list_elem);
1519 }
1520 return;
1521 }
1522 }
1523}
1524
1525acpi_status
1526acpi_os_invalidate_address(
1527 u8 space_id,
1528 acpi_physical_address address,
1529 acpi_size length)
1530{
1531 struct acpi_res_list res;
1532
1533 switch (space_id) {
1534 case ACPI_ADR_SPACE_SYSTEM_IO:
1535 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001536 /* Only interference checks against SystemIO and SystemMemory
Lin Minga5fe1a02009-08-13 10:43:27 +08001537 are needed */
1538 res.start = address;
1539 res.end = address + length - 1;
1540 res.resource_type = space_id;
1541 spin_lock(&acpi_res_lock);
1542 acpi_res_list_del(&res);
1543 spin_unlock(&acpi_res_lock);
1544 break;
1545 case ACPI_ADR_SPACE_PCI_CONFIG:
1546 case ACPI_ADR_SPACE_EC:
1547 case ACPI_ADR_SPACE_SMBUS:
1548 case ACPI_ADR_SPACE_CMOS:
1549 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1550 case ACPI_ADR_SPACE_DATA_TABLE:
1551 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1552 break;
1553 }
1554 return AE_OK;
1555}
1556
Bob Mooreb229cf92006-04-21 17:15:00 -04001557/******************************************************************************
1558 *
1559 * FUNCTION: acpi_os_validate_address
1560 *
1561 * PARAMETERS: space_id - ACPI space ID
1562 * address - Physical address
1563 * length - Address length
1564 *
1565 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1566 * should return AE_AML_ILLEGAL_ADDRESS.
1567 *
1568 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1569 * the addresses accessed by AML operation regions.
1570 *
1571 *****************************************************************************/
1572
1573acpi_status
1574acpi_os_validate_address (
1575 u8 space_id,
1576 acpi_physical_address address,
Thomas Renningerdf92e692008-02-04 23:31:22 -08001577 acpi_size length,
1578 char *name)
Bob Mooreb229cf92006-04-21 17:15:00 -04001579{
Thomas Renningerdf92e692008-02-04 23:31:22 -08001580 struct acpi_res_list *res;
Lin Minga5fe1a02009-08-13 10:43:27 +08001581 int added;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001582 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1583 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001584
Thomas Renningerdf92e692008-02-04 23:31:22 -08001585 switch (space_id) {
1586 case ACPI_ADR_SPACE_SYSTEM_IO:
1587 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001588 /* Only interference checks against SystemIO and SystemMemory
Thomas Renningerdf92e692008-02-04 23:31:22 -08001589 are needed */
1590 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1591 if (!res)
1592 return AE_OK;
1593 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1594 strlcpy(res->name, name, 5);
1595 res->start = address;
1596 res->end = address + length - 1;
1597 res->resource_type = space_id;
1598 spin_lock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001599 added = acpi_res_list_add(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001600 spin_unlock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001601 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1602 "name: %s\n", added ? "Added" : "Already exist",
1603 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001604 ? "SystemIO" : "System Memory",
1605 (unsigned long long)res->start,
1606 (unsigned long long)res->end,
1607 res->name);
Lin Minga5fe1a02009-08-13 10:43:27 +08001608 if (!added)
1609 kfree(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001610 break;
1611 case ACPI_ADR_SPACE_PCI_CONFIG:
1612 case ACPI_ADR_SPACE_EC:
1613 case ACPI_ADR_SPACE_SMBUS:
1614 case ACPI_ADR_SPACE_CMOS:
1615 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1616 case ACPI_ADR_SPACE_DATA_TABLE:
1617 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1618 break;
1619 }
1620 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001621}
Robert Moore73459f72005-06-24 00:00:00 -04001622#endif
Myron Stowed362eda2010-10-21 14:24:04 -06001623
1624acpi_status __init acpi_os_initialize(void)
1625{
1626 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1627 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1628 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1629 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1630
1631 return AE_OK;
1632}
1633
Zhang Rui32d47ee2010-12-08 10:40:36 +08001634acpi_status __init acpi_os_initialize1(void)
Myron Stowed362eda2010-10-21 14:24:04 -06001635{
Tejun Heo44d25882011-02-01 11:42:42 +01001636 kacpid_wq = alloc_workqueue("kacpid", 0, 1);
1637 kacpi_notify_wq = alloc_workqueue("kacpi_notify", 0, 1);
1638 kacpi_hotplug_wq = alloc_workqueue("kacpi_hotplug", 0, 1);
Myron Stowed362eda2010-10-21 14:24:04 -06001639 BUG_ON(!kacpid_wq);
1640 BUG_ON(!kacpi_notify_wq);
1641 BUG_ON(!kacpi_hotplug_wq);
Len Brown1bd64d42010-10-26 14:50:56 -04001642 acpi_install_interface_handler(acpi_osi_handler);
1643 acpi_osi_setup_late();
Myron Stowed362eda2010-10-21 14:24:04 -06001644 return AE_OK;
1645}
1646
1647acpi_status acpi_os_terminate(void)
1648{
1649 if (acpi_irq_handler) {
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +01001650 acpi_os_remove_interrupt_handler(acpi_gbl_FADT.sci_interrupt,
Myron Stowed362eda2010-10-21 14:24:04 -06001651 acpi_irq_handler);
1652 }
1653
1654 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1655 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1656 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1657 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1658
1659 destroy_workqueue(kacpid_wq);
1660 destroy_workqueue(kacpi_notify_wq);
1661 destroy_workqueue(kacpi_hotplug_wq);
1662
1663 return AE_OK;
1664}
Tang Liang09f98a82011-12-09 10:05:54 +08001665
1666acpi_status acpi_os_prepare_sleep(u8 sleep_state, u32 pm1a_control,
1667 u32 pm1b_control)
1668{
1669 int rc = 0;
1670 if (__acpi_os_prepare_sleep)
1671 rc = __acpi_os_prepare_sleep(sleep_state,
1672 pm1a_control, pm1b_control);
1673 if (rc < 0)
1674 return AE_ERROR;
1675 else if (rc > 0)
1676 return AE_CTRL_SKIP;
1677
1678 return AE_OK;
1679}
1680
1681void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
1682 u32 pm1a_ctrl, u32 pm1b_ctrl))
1683{
1684 __acpi_os_prepare_sleep = func;
1685}