blob: cf750a7a952317376e0af14250b984e2b0de3f32 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
3 *
4 * Copyright (C) 2000 Andrew Henroid
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04007 * Copyright (c) 2008 Intel Corporation
8 * Author: Matthew Wilcox <willy@linux.intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 *
28 */
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/slab.h>
33#include <linux/mm.h>
34#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/interrupt.h>
36#include <linux/kmod.h>
37#include <linux/delay.h>
38#include <linux/workqueue.h>
39#include <linux/nmi.h>
Alexey Starikovskiyad718602007-02-02 19:48:19 +030040#include <linux/acpi.h>
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +010041#include <linux/acpi_io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/efi.h>
Thomas Renningerdf92e692008-02-04 23:31:22 -080043#include <linux/ioport.h>
44#include <linux/list.h>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -040045#include <linux/jiffies.h>
46#include <linux/semaphore.h>
47
48#include <asm/io.h>
49#include <asm/uaccess.h>
50
51#include <acpi/acpi.h>
52#include <acpi/acpi_bus.h>
53#include <acpi/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define _COMPONENT ACPI_OS_SERVICES
Len Brownf52fd662007-02-12 22:42:12 -050056ACPI_MODULE_NAME("osl");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define PREFIX "ACPI: "
Len Brown4be44fc2005-08-05 00:44:28 -040058struct acpi_os_dpc {
59 acpi_osd_exec_callback function;
60 void *context;
David Howells65f27f32006-11-22 14:55:48 +000061 struct work_struct work;
Bjorn Helgaas9ac61852009-08-31 22:32:10 +000062 int wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65#ifdef CONFIG_ACPI_CUSTOM_DSDT
66#include CONFIG_ACPI_CUSTOM_DSDT_FILE
67#endif
68
69#ifdef ENABLE_DEBUGGER
70#include <linux/kdb.h>
71
72/* stuff for debugger support */
73int acpi_in_debugger;
74EXPORT_SYMBOL(acpi_in_debugger);
75
76extern char line_buf[80];
Len Brown4be44fc2005-08-05 00:44:28 -040077#endif /*ENABLE_DEBUGGER */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static unsigned int acpi_irq_irq;
80static acpi_osd_handler acpi_irq_handler;
81static void *acpi_irq_context;
82static struct workqueue_struct *kacpid_wq;
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -040083static struct workqueue_struct *kacpi_notify_wq;
Zhang Ruic02256b2009-06-23 10:20:29 +080084static struct workqueue_struct *kacpi_hotplug_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Thomas Renningerdf92e692008-02-04 23:31:22 -080086struct acpi_res_list {
87 resource_size_t start;
88 resource_size_t end;
89 acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
90 char name[5]; /* only can have a length of 4 chars, make use of this
91 one instead of res->name, no need to kalloc then */
92 struct list_head resource_list;
Lin Minga5fe1a02009-08-13 10:43:27 +080093 int count;
Thomas Renningerdf92e692008-02-04 23:31:22 -080094};
95
96static LIST_HEAD(resource_list_head);
97static DEFINE_SPINLOCK(acpi_res_lock);
98
Myron Stowe620242a2010-10-21 14:23:53 -060099/*
100 * This list of permanent mappings is for memory that may be accessed from
101 * interrupt context, where we can't do the ioremap().
102 */
103struct acpi_ioremap {
104 struct list_head list;
105 void __iomem *virt;
106 acpi_physical_address phys;
107 acpi_size size;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600108 struct kref ref;
Myron Stowe620242a2010-10-21 14:23:53 -0600109};
110
111static LIST_HEAD(acpi_ioremaps);
112static DEFINE_SPINLOCK(acpi_ioremap_lock);
113
Lin Mingb0ed7a92010-08-06 09:35:51 +0800114static void __init acpi_osi_setup_late(void);
Len Brownae00d812007-05-29 18:43:33 -0400115
Len Brownd4b7dc42008-01-23 20:50:56 -0500116/*
Len Browna6e08872008-11-08 01:21:10 -0500117 * The story of _OSI(Linux)
Len Brownd4b7dc42008-01-23 20:50:56 -0500118 *
Len Browna6e08872008-11-08 01:21:10 -0500119 * From pre-history through Linux-2.6.22,
120 * Linux responded TRUE upon a BIOS OSI(Linux) query.
Len Brownd4b7dc42008-01-23 20:50:56 -0500121 *
Len Browna6e08872008-11-08 01:21:10 -0500122 * Unfortunately, reference BIOS writers got wind of this
123 * and put OSI(Linux) in their example code, quickly exposing
124 * this string as ill-conceived and opening the door to
125 * an un-bounded number of BIOS incompatibilities.
Len Brownd4b7dc42008-01-23 20:50:56 -0500126 *
Len Browna6e08872008-11-08 01:21:10 -0500127 * For example, OSI(Linux) was used on resume to re-POST a
128 * video card on one system, because Linux at that time
129 * could not do a speedy restore in its native driver.
130 * But then upon gaining quick native restore capability,
131 * Linux has no way to tell the BIOS to skip the time-consuming
132 * POST -- putting Linux at a permanent performance disadvantage.
133 * On another system, the BIOS writer used OSI(Linux)
134 * to infer native OS support for IPMI! On other systems,
135 * OSI(Linux) simply got in the way of Linux claiming to
136 * be compatible with other operating systems, exposing
137 * BIOS issues such as skipped device initialization.
Len Brownd4b7dc42008-01-23 20:50:56 -0500138 *
Len Browna6e08872008-11-08 01:21:10 -0500139 * So "Linux" turned out to be a really poor chose of
140 * OSI string, and from Linux-2.6.23 onward we respond FALSE.
Len Brownd4b7dc42008-01-23 20:50:56 -0500141 *
142 * BIOS writers should NOT query _OSI(Linux) on future systems.
Len Browna6e08872008-11-08 01:21:10 -0500143 * Linux will complain on the console when it sees it, and return FALSE.
144 * To get Linux to return TRUE for your system will require
145 * a kernel source update to add a DMI entry,
146 * or boot with "acpi_osi=Linux"
Len Brownd4b7dc42008-01-23 20:50:56 -0500147 */
Len Brownd4b7dc42008-01-23 20:50:56 -0500148
Adrian Bunk1d15d842008-01-29 00:10:15 +0200149static struct osi_linux {
Len Brownd4b7dc42008-01-23 20:50:56 -0500150 unsigned int enable:1;
151 unsigned int dmi:1;
152 unsigned int cmdline:1;
Lin Mingd90aa922010-12-09 16:50:52 +0800153} osi_linux = {0, 0, 0};
Len Brownf5076542007-05-30 00:10:38 -0400154
Lin Mingb0ed7a92010-08-06 09:35:51 +0800155static u32 acpi_osi_handler(acpi_string interface, u32 supported)
156{
157 if (!strcmp("Linux", interface)) {
158
Len Brown3af283e2010-10-15 21:38:57 -0400159 printk(KERN_NOTICE FW_BUG PREFIX
Lin Mingb0ed7a92010-08-06 09:35:51 +0800160 "BIOS _OSI(Linux) query %s%s\n",
161 osi_linux.enable ? "honored" : "ignored",
162 osi_linux.cmdline ? " via cmdline" :
163 osi_linux.dmi ? " via DMI" : "");
164 }
165
166 return supported;
167}
168
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700169static void __init acpi_request_region (struct acpi_generic_address *addr,
170 unsigned int length, char *desc)
171{
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700172 if (!addr->address || !length)
173 return;
174
Andi Kleencfa806f2010-07-20 15:18:36 -0700175 /* Resources are never freed */
Len Browneee3c852007-02-03 01:38:16 -0500176 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Andi Kleencfa806f2010-07-20 15:18:36 -0700177 request_region(addr->address, length, desc);
Len Browneee3c852007-02-03 01:38:16 -0500178 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
Andi Kleencfa806f2010-07-20 15:18:36 -0700179 request_mem_region(addr->address, length, desc);
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700180}
181
182static int __init acpi_reserve_resources(void)
183{
Len Browneee3c852007-02-03 01:38:16 -0500184 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700185 "ACPI PM1a_EVT_BLK");
186
Len Browneee3c852007-02-03 01:38:16 -0500187 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700188 "ACPI PM1b_EVT_BLK");
189
Len Browneee3c852007-02-03 01:38:16 -0500190 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700191 "ACPI PM1a_CNT_BLK");
192
Len Browneee3c852007-02-03 01:38:16 -0500193 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700194 "ACPI PM1b_CNT_BLK");
195
Len Browneee3c852007-02-03 01:38:16 -0500196 if (acpi_gbl_FADT.pm_timer_length == 4)
197 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700198
Len Browneee3c852007-02-03 01:38:16 -0500199 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700200 "ACPI PM2_CNT_BLK");
201
202 /* Length of GPE blocks must be a non-negative multiple of 2 */
203
Len Browneee3c852007-02-03 01:38:16 -0500204 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
205 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
206 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700207
Len Browneee3c852007-02-03 01:38:16 -0500208 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
209 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
210 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700211
212 return 0;
213}
214device_initcall(acpi_reserve_resources);
215
Len Brown4be44fc2005-08-05 00:44:28 -0400216void acpi_os_printf(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 va_list args;
219 va_start(args, fmt);
220 acpi_os_vprintf(fmt, args);
221 va_end(args);
222}
Len Brown4be44fc2005-08-05 00:44:28 -0400223
Len Brown4be44fc2005-08-05 00:44:28 -0400224void acpi_os_vprintf(const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 static char buffer[512];
Len Brown4be44fc2005-08-05 00:44:28 -0400227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 vsprintf(buffer, fmt, args);
229
230#ifdef ENABLE_DEBUGGER
231 if (acpi_in_debugger) {
232 kdb_printf("%s", buffer);
233 } else {
Frank Seidel4d939152009-02-04 17:03:07 +0100234 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236#else
Frank Seidel4d939152009-02-04 17:03:07 +0100237 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#endif
239}
240
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300241acpi_physical_address __init acpi_os_get_root_pointer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 if (efi_enabled) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800244 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300245 return efi.acpi20;
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800246 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300247 return efi.acpi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400249 printk(KERN_ERR PREFIX
250 "System description tables not found\n");
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Len Brown239665a2007-11-23 20:08:02 -0500253 } else {
254 acpi_physical_address pa = 0;
255
256 acpi_find_root_pointer(&pa);
257 return pa;
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600261/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe4a3cba52010-10-21 14:24:14 -0600262static struct acpi_ioremap *
263acpi_map_lookup(acpi_physical_address phys, acpi_size size)
Myron Stowe620242a2010-10-21 14:23:53 -0600264{
265 struct acpi_ioremap *map;
266
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600267 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe620242a2010-10-21 14:23:53 -0600268 if (map->phys <= phys &&
269 phys + size <= map->phys + map->size)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600270 return map;
271
272 return NULL;
273}
274
275/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
276static void __iomem *
277acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
278{
279 struct acpi_ioremap *map;
280
281 map = acpi_map_lookup(phys, size);
282 if (map)
283 return map->virt + (phys - map->phys);
Myron Stowe620242a2010-10-21 14:23:53 -0600284
285 return NULL;
286}
287
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600288/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe620242a2010-10-21 14:23:53 -0600289static struct acpi_ioremap *
290acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
291{
292 struct acpi_ioremap *map;
293
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600294 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600295 if (map->virt <= virt &&
296 virt + size <= map->virt + map->size)
Myron Stowe620242a2010-10-21 14:23:53 -0600297 return map;
298
299 return NULL;
300}
301
Jan Beulich2fdf0742007-12-13 08:33:59 +0000302void __iomem *__init_refok
303acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Myron Stowe4a3cba52010-10-21 14:24:14 -0600305 struct acpi_ioremap *map, *tmp_map;
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100306 unsigned long flags;
Myron Stowe620242a2010-10-21 14:23:53 -0600307 void __iomem *virt;
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100308 acpi_physical_address pg_off;
309 acpi_size pg_sz;
Myron Stowe620242a2010-10-21 14:23:53 -0600310
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800311 if (phys > ULONG_MAX) {
312 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
Randy Dunlap70c08462007-02-13 16:11:36 -0800313 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Myron Stowe620242a2010-10-21 14:23:53 -0600315
316 if (!acpi_gbl_permanent_mmap)
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300317 return __acpi_map_table((unsigned long)phys, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600318
319 map = kzalloc(sizeof(*map), GFP_KERNEL);
320 if (!map)
321 return NULL;
322
Myron Stowe4a3cba52010-10-21 14:24:14 -0600323 pg_off = round_down(phys, PAGE_SIZE);
324 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100325 virt = acpi_os_ioremap(pg_off, pg_sz);
Myron Stowe620242a2010-10-21 14:23:53 -0600326 if (!virt) {
327 kfree(map);
328 return NULL;
329 }
330
331 INIT_LIST_HEAD(&map->list);
332 map->virt = virt;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600333 map->phys = pg_off;
334 map->size = pg_sz;
335 kref_init(&map->ref);
Myron Stowe620242a2010-10-21 14:23:53 -0600336
337 spin_lock_irqsave(&acpi_ioremap_lock, flags);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600338 /* Check if page has already been mapped. */
339 tmp_map = acpi_map_lookup(phys, size);
340 if (tmp_map) {
341 kref_get(&tmp_map->ref);
342 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
343 iounmap(map->virt);
344 kfree(map);
345 return tmp_map->virt + (phys - tmp_map->phys);
346 }
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600347 list_add_tail_rcu(&map->list, &acpi_ioremaps);
Myron Stowe620242a2010-10-21 14:23:53 -0600348 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
349
Myron Stowe4a3cba52010-10-21 14:24:14 -0600350 return map->virt + (phys - map->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800352EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Myron Stowe4a3cba52010-10-21 14:24:14 -0600354static void acpi_kref_del_iomap(struct kref *ref)
355{
356 struct acpi_ioremap *map;
357
358 map = container_of(ref, struct acpi_ioremap, ref);
359 list_del_rcu(&map->list);
360}
361
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800362void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Myron Stowe620242a2010-10-21 14:23:53 -0600364 struct acpi_ioremap *map;
365 unsigned long flags;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600366 int del;
Myron Stowe620242a2010-10-21 14:23:53 -0600367
368 if (!acpi_gbl_permanent_mmap) {
Yinghai Lu7d972772009-02-07 15:39:41 -0800369 __acpi_unmap_table(virt, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600370 return;
371 }
372
373 spin_lock_irqsave(&acpi_ioremap_lock, flags);
374 map = acpi_map_lookup_virt(virt, size);
375 if (!map) {
376 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
377 printk(KERN_ERR PREFIX "%s: bad address %p\n", __func__, virt);
378 dump_stack();
379 return;
380 }
381
Myron Stowe4a3cba52010-10-21 14:24:14 -0600382 del = kref_put(&map->ref, acpi_kref_del_iomap);
Myron Stowe620242a2010-10-21 14:23:53 -0600383 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
384
Myron Stowe4a3cba52010-10-21 14:24:14 -0600385 if (!del)
386 return;
387
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600388 synchronize_rcu();
Myron Stowe620242a2010-10-21 14:23:53 -0600389 iounmap(map->virt);
390 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800392EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800394void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Yinghai Lu7d972772009-02-07 15:39:41 -0800395{
396 if (!acpi_gbl_permanent_mmap)
397 __acpi_unmap_table(virt, size);
398}
399
Myron Stowe29718522010-10-21 14:23:59 -0600400int acpi_os_map_generic_address(struct acpi_generic_address *addr)
401{
402 void __iomem *virt;
403
404 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
405 return 0;
406
407 if (!addr->address || !addr->bit_width)
408 return -EINVAL;
409
410 virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
411 if (!virt)
412 return -EIO;
413
414 return 0;
415}
416EXPORT_SYMBOL_GPL(acpi_os_map_generic_address);
417
418void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
419{
420 void __iomem *virt;
421 unsigned long flags;
422 acpi_size size = addr->bit_width / 8;
423
424 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
425 return;
426
427 if (!addr->address || !addr->bit_width)
428 return;
429
430 spin_lock_irqsave(&acpi_ioremap_lock, flags);
431 virt = acpi_map_vaddr_lookup(addr->address, size);
432 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
433
434 acpi_os_unmap_memory(virt, size);
435}
436EXPORT_SYMBOL_GPL(acpi_os_unmap_generic_address);
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438#ifdef ACPI_FUTURE_USAGE
439acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400440acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Len Brown4be44fc2005-08-05 00:44:28 -0400442 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return AE_BAD_PARAMETER;
444
445 *phys = virt_to_phys(virt);
446
447 return AE_OK;
448}
449#endif
450
451#define ACPI_MAX_OVERRIDE_LEN 100
452
453static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
454
455acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400456acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
457 acpi_string * new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 if (!init_val || !new_val)
460 return AE_BAD_PARAMETER;
461
462 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400463 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400465 acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 *new_val = acpi_os_name;
467 }
468
469 return AE_OK;
470}
471
472acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400473acpi_os_table_override(struct acpi_table_header * existing_table,
474 struct acpi_table_header ** new_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 if (!existing_table || !new_table)
477 return AE_BAD_PARAMETER;
478
Markus Gaugusch71fc47a2008-02-05 00:04:06 +0100479 *new_table = NULL;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481#ifdef CONFIG_ACPI_CUSTOM_DSDT
482 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400483 *new_table = (struct acpi_table_header *)AmlCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#endif
Éric Piel6ed31e92008-02-05 00:04:50 +0100485 if (*new_table != NULL) {
486 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
487 "this is unsafe: tainting kernel\n",
488 existing_table->signature,
489 existing_table->oem_table_id);
490 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return AE_OK;
493}
494
David Howells7d12e782006-10-05 14:55:46 +0100495static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Len Brown5229e872008-02-06 01:26:55 -0500497 u32 handled;
498
499 handled = (*acpi_irq_handler) (acpi_irq_context);
500
501 if (handled) {
502 acpi_irq_handled++;
503 return IRQ_HANDLED;
Len Brown88bea182009-04-21 00:35:47 -0400504 } else {
505 acpi_irq_not_handled++;
Len Brown5229e872008-02-06 01:26:55 -0500506 return IRQ_NONE;
Len Brown88bea182009-04-21 00:35:47 -0400507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400511acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
512 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 unsigned int irq;
515
Len Brown5229e872008-02-06 01:26:55 -0500516 acpi_irq_stats_init();
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /*
519 * Ignore the GSI from the core, and use the value in our copy of the
520 * FADT. It may not be the same if an interrupt source override exists
521 * for the SCI.
522 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300523 gsi = acpi_gbl_FADT.sci_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
525 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
526 gsi);
527 return AE_OK;
528 }
529
530 acpi_irq_handler = handler;
531 acpi_irq_context = context;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700532 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
534 return AE_NOT_ACQUIRED;
535 }
536 acpi_irq_irq = irq;
537
538 return AE_OK;
539}
540
Len Brown4be44fc2005-08-05 00:44:28 -0400541acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 if (irq) {
544 free_irq(irq, acpi_irq);
545 acpi_irq_handler = NULL;
546 acpi_irq_irq = 0;
547 }
548
549 return AE_OK;
550}
551
552/*
553 * Running in interpreter thread context, safe to sleep
554 */
555
Lin Ming439913f2010-01-28 10:53:19 +0800556void acpi_os_sleep(u64 ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800558 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
Len Brown4be44fc2005-08-05 00:44:28 -0400560
Len Brown4be44fc2005-08-05 00:44:28 -0400561void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 while (us) {
564 u32 delay = 1000;
565
566 if (delay > us)
567 delay = us;
568 udelay(delay);
569 touch_nmi_watchdog();
570 us -= delay;
571 }
572}
Len Brown4be44fc2005-08-05 00:44:28 -0400573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574/*
575 * Support ACPI 3.0 AML Timer operand
576 * Returns 64-bit free-running, monotonically increasing timer
577 * with 100ns granularity
578 */
Len Brown4be44fc2005-08-05 00:44:28 -0400579u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
581 static u64 t;
582
583#ifdef CONFIG_HPET
584 /* TBD: use HPET if available */
585#endif
586
587#ifdef CONFIG_X86_PM_TIMER
588 /* TBD: default to PM timer if HPET was not available */
589#endif
590 if (!t)
591 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
592
593 return ++t;
594}
595
Len Brown4be44fc2005-08-05 00:44:28 -0400596acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 u32 dummy;
599
600 if (!value)
601 value = &dummy;
602
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800603 *value = 0;
604 if (width <= 8) {
Len Brown4be44fc2005-08-05 00:44:28 -0400605 *(u8 *) value = inb(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800606 } else if (width <= 16) {
Len Brown4be44fc2005-08-05 00:44:28 -0400607 *(u16 *) value = inw(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800608 } else if (width <= 32) {
Len Brown4be44fc2005-08-05 00:44:28 -0400609 *(u32 *) value = inl(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800610 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 BUG();
612 }
613
614 return AE_OK;
615}
Len Brown4be44fc2005-08-05 00:44:28 -0400616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617EXPORT_SYMBOL(acpi_os_read_port);
618
Len Brown4be44fc2005-08-05 00:44:28 -0400619acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800621 if (width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 outb(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800623 } else if (width <= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 outw(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800625 } else if (width <= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 outl(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800627 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 BUG();
629 }
630
631 return AE_OK;
632}
Len Brown4be44fc2005-08-05 00:44:28 -0400633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634EXPORT_SYMBOL(acpi_os_write_port);
635
636acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400637acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Len Brown4be44fc2005-08-05 00:44:28 -0400639 void __iomem *virt_addr;
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100640 unsigned int size = width / 8;
641 bool unmap = false;
642 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600644 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600645 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600646 if (!virt_addr) {
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100647 rcu_read_unlock();
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100648 virt_addr = acpi_os_ioremap(phys_addr, size);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100649 if (!virt_addr)
650 return AE_BAD_ADDRESS;
651 unmap = true;
Myron Stowe620242a2010-10-21 14:23:53 -0600652 }
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (!value)
655 value = &dummy;
656
657 switch (width) {
658 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400659 *(u8 *) value = readb(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 break;
661 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400662 *(u16 *) value = readw(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 break;
664 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400665 *(u32 *) value = readl(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 break;
667 default:
668 BUG();
669 }
670
Myron Stowe620242a2010-10-21 14:23:53 -0600671 if (unmap)
672 iounmap(virt_addr);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100673 else
674 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 return AE_OK;
677}
678
679acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400680acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Len Brown4be44fc2005-08-05 00:44:28 -0400682 void __iomem *virt_addr;
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100683 unsigned int size = width / 8;
684 bool unmap = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600686 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600687 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600688 if (!virt_addr) {
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100689 rcu_read_unlock();
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100690 virt_addr = acpi_os_ioremap(phys_addr, size);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100691 if (!virt_addr)
692 return AE_BAD_ADDRESS;
693 unmap = true;
Myron Stowe620242a2010-10-21 14:23:53 -0600694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 switch (width) {
697 case 8:
698 writeb(value, virt_addr);
699 break;
700 case 16:
701 writew(value, virt_addr);
702 break;
703 case 32:
704 writel(value, virt_addr);
705 break;
706 default:
707 BUG();
708 }
709
Myron Stowe620242a2010-10-21 14:23:53 -0600710 if (unmap)
711 iounmap(virt_addr);
Rafael J. Wysocki884b8212011-02-08 23:37:16 +0100712 else
713 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 return AE_OK;
716}
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400719acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Bob Moorec5f02312010-08-06 08:57:53 +0800720 u64 *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 int result, size;
Bob Moorec5f02312010-08-06 08:57:53 +0800723 u32 value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 if (!value)
726 return AE_BAD_PARAMETER;
727
728 switch (width) {
729 case 8:
730 size = 1;
731 break;
732 case 16:
733 size = 2;
734 break;
735 case 32:
736 size = 4;
737 break;
738 default:
739 return AE_ERROR;
740 }
741
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500742 result = raw_pci_read(pci_id->segment, pci_id->bus,
743 PCI_DEVFN(pci_id->device, pci_id->function),
Bob Moorec5f02312010-08-06 08:57:53 +0800744 reg, size, &value32);
745 *value = value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 return (result ? AE_ERROR : AE_OK);
748}
Len Brown4be44fc2005-08-05 00:44:28 -0400749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400751acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Lin Ming439913f2010-01-28 10:53:19 +0800752 u64 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 int result, size;
755
756 switch (width) {
757 case 8:
758 size = 1;
759 break;
760 case 16:
761 size = 2;
762 break;
763 case 32:
764 size = 4;
765 break;
766 default:
767 return AE_ERROR;
768 }
769
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500770 result = raw_pci_write(pci_id->segment, pci_id->bus,
771 PCI_DEVFN(pci_id->device, pci_id->function),
772 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 return (result ? AE_ERROR : AE_OK);
775}
776
David Howells65f27f32006-11-22 14:55:48 +0000777static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
David Howells65f27f32006-11-22 14:55:48 +0000779 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400780
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000781 if (dpc->wait)
782 acpi_os_wait_events_complete(NULL);
Zhang Rui19cd8472008-08-28 10:05:06 +0800783
784 dpc->function(dpc->context);
785 kfree(dpc);
Zhang Rui19cd8472008-08-28 10:05:06 +0800786}
787
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400788/*******************************************************************************
789 *
790 * FUNCTION: acpi_os_execute
791 *
792 * PARAMETERS: Type - Type of the callback
793 * Function - Function to be executed
794 * Context - Function parameters
795 *
796 * RETURN: Status
797 *
798 * DESCRIPTION: Depending on type, either queues function for deferred execution or
799 * immediately executes function on a separate thread.
800 *
801 ******************************************************************************/
802
Zhang Rui19cd8472008-08-28 10:05:06 +0800803static acpi_status __acpi_os_execute(acpi_execute_type type,
804 acpi_osd_exec_callback function, void *context, int hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Len Brown4be44fc2005-08-05 00:44:28 -0400806 acpi_status status = AE_OK;
807 struct acpi_os_dpc *dpc;
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300808 struct workqueue_struct *queue;
Zhang Rui19cd8472008-08-28 10:05:06 +0800809 int ret;
Len Brown72945b22006-07-12 22:46:42 -0400810 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
811 "Scheduling function [%p(%p)] for deferred execution.\n",
812 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /*
815 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +0000816 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 * way that allows us to also free its memory inside the callee.
818 * Because we may want to schedule several tasks with different
819 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +0000820 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 */
Len Brown72945b22006-07-12 22:46:42 -0400822
David Howells65f27f32006-11-22 14:55:48 +0000823 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (!dpc)
Lin Ming889c78b2008-12-31 09:23:57 +0800825 return AE_NO_MEMORY;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 dpc->function = function;
828 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800829
Zhang Ruic02256b2009-06-23 10:20:29 +0800830 /*
831 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
832 * because the hotplug code may call driver .remove() functions,
833 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
834 * to flush these workqueues.
835 */
836 queue = hp ? kacpi_hotplug_wq :
837 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000838 dpc->wait = hp ? 1 : 0;
Zhang Ruibc736752010-03-22 15:48:54 +0800839
840 if (queue == kacpi_hotplug_wq)
841 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
842 else if (queue == kacpi_notify_wq)
843 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
844 else
845 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
846
Tejun Heo8fec62b2010-06-29 10:07:09 +0200847 /*
848 * On some machines, a software-initiated SMI causes corruption unless
849 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
850 * typically it's done in GPE-related methods that are run via
851 * workqueues, so we can avoid the known corruption cases by always
852 * queueing on CPU 0.
853 */
854 ret = queue_work_on(0, queue, &dpc->work);
Zhang Rui19cd8472008-08-28 10:05:06 +0800855
856 if (!ret) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800857 printk(KERN_ERR PREFIX
858 "Call to queue_work() failed.\n");
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300859 status = AE_ERROR;
860 kfree(dpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Lin Ming889c78b2008-12-31 09:23:57 +0800862 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
Len Brown4be44fc2005-08-05 00:44:28 -0400864
Zhang Rui19cd8472008-08-28 10:05:06 +0800865acpi_status acpi_os_execute(acpi_execute_type type,
866 acpi_osd_exec_callback function, void *context)
867{
868 return __acpi_os_execute(type, function, context, 0);
869}
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400870EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Zhang Rui19cd8472008-08-28 10:05:06 +0800872acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
873 void *context)
874{
875 return __acpi_os_execute(0, function, context, 1);
876}
877
Len Brown4be44fc2005-08-05 00:44:28 -0400878void acpi_os_wait_events_complete(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 flush_workqueue(kacpid_wq);
Zhang Rui2f67a062008-04-29 02:34:42 -0400881 flush_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
Len Brown4be44fc2005-08-05 00:44:28 -0400883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884EXPORT_SYMBOL(acpi_os_wait_events_complete);
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400887acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Len Brown4be44fc2005-08-05 00:44:28 -0400889 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 sem = acpi_os_allocate(sizeof(struct semaphore));
892 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400893 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 memset(sem, 0, sizeof(struct semaphore));
895
896 sema_init(sem, initial_units);
897
Len Brown4be44fc2005-08-05 00:44:28 -0400898 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Len Brown4be44fc2005-08-05 00:44:28 -0400900 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
901 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Patrick Mocheld550d982006-06-27 00:41:40 -0400903 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906/*
907 * TODO: A better way to delete semaphores? Linux doesn't have a
908 * 'delete_semaphore()' function -- may result in an invalid
909 * pointer dereference for non-synchronized consumers. Should
910 * we at least check for blocked threads and signal/cancel them?
911 */
912
Len Brown4be44fc2005-08-05 00:44:28 -0400913acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Len Brown4be44fc2005-08-05 00:44:28 -0400915 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400918 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Len Brown4be44fc2005-08-05 00:44:28 -0400920 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400922 BUG_ON(!list_empty(&sem->wait_list));
Len Brown02438d82006-06-30 03:19:10 -0400923 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -0400924 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Patrick Mocheld550d982006-06-27 00:41:40 -0400926 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 * TODO: Support for units > 1?
931 */
Len Brown4be44fc2005-08-05 00:44:28 -0400932acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Len Brown4be44fc2005-08-05 00:44:28 -0400934 acpi_status status = AE_OK;
935 struct semaphore *sem = (struct semaphore *)handle;
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400936 long jiffies;
Len Brown4be44fc2005-08-05 00:44:28 -0400937 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400940 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400943 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Len Brown4be44fc2005-08-05 00:44:28 -0400945 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
946 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400948 if (timeout == ACPI_WAIT_FOREVER)
949 jiffies = MAX_SCHEDULE_TIMEOUT;
950 else
951 jiffies = msecs_to_jiffies(timeout);
952
953 ret = down_timeout(sem, jiffies);
954 if (ret)
955 status = AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -0400958 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400959 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -0400960 handle, units, timeout,
961 acpi_format_exception(status)));
962 } else {
963 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400964 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400965 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967
Patrick Mocheld550d982006-06-27 00:41:40 -0400968 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/*
972 * TODO: Support for units > 1?
973 */
Len Brown4be44fc2005-08-05 00:44:28 -0400974acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Len Brown4be44fc2005-08-05 00:44:28 -0400976 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400979 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400982 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Len Brown4be44fc2005-08-05 00:44:28 -0400984 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
985 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 up(sem);
988
Patrick Mocheld550d982006-06-27 00:41:40 -0400989 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
Len Brown4be44fc2005-08-05 00:44:28 -0400991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -0400993u32 acpi_os_get_line(char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995
996#ifdef ENABLE_DEBUGGER
997 if (acpi_in_debugger) {
998 u32 chars;
999
1000 kdb_read(buffer, sizeof(line_buf));
1001
1002 /* remove the CR kdb includes */
1003 chars = strlen(buffer) - 1;
1004 buffer[chars] = '\0';
1005 }
1006#endif
1007
1008 return 0;
1009}
Len Brown4be44fc2005-08-05 00:44:28 -04001010#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Len Brown4be44fc2005-08-05 00:44:28 -04001012acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Len Brown4be44fc2005-08-05 00:44:28 -04001014 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 case ACPI_SIGNAL_FATAL:
1016 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1017 break;
1018 case ACPI_SIGNAL_BREAKPOINT:
1019 /*
1020 * AML Breakpoint
1021 * ACPI spec. says to treat it as a NOP unless
1022 * you are debugging. So if/when we integrate
1023 * AML debugger into the kernel debugger its
1024 * hook will go here. But until then it is
1025 * not useful to print anything on breakpoints.
1026 */
1027 break;
1028 default:
1029 break;
1030 }
1031
1032 return AE_OK;
1033}
Len Brown4be44fc2005-08-05 00:44:28 -04001034
Len Brown4be44fc2005-08-05 00:44:28 -04001035static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -04001038 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 if (!str || !*str)
1041 return 0;
1042
1043 for (; count-- && str && *str; str++) {
1044 if (isalnum(*str) || *str == ' ' || *str == ':')
1045 *p++ = *str;
1046 else if (*str == '\'' || *str == '"')
1047 continue;
1048 else
1049 break;
1050 }
1051 *p = 0;
1052
1053 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057__setup("acpi_os_name=", acpi_os_name_setup);
1058
Lin Ming12d32062010-12-09 16:51:06 +08001059#define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
1060#define OSI_STRING_ENTRIES_MAX 16 /* arbitrary */
1061
1062struct osi_setup_entry {
1063 char string[OSI_STRING_LENGTH_MAX];
1064 bool enable;
1065};
1066
1067static struct osi_setup_entry __initdata osi_setup_entries[OSI_STRING_ENTRIES_MAX];
1068
Lin Mingd90aa922010-12-09 16:50:52 +08001069void __init acpi_osi_setup(char *str)
1070{
Lin Ming12d32062010-12-09 16:51:06 +08001071 struct osi_setup_entry *osi;
1072 bool enable = true;
1073 int i;
1074
Lin Mingd90aa922010-12-09 16:50:52 +08001075 if (!acpi_gbl_create_osi_method)
1076 return;
1077
1078 if (str == NULL || *str == '\0') {
1079 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1080 acpi_gbl_create_osi_method = FALSE;
Lin Ming12d32062010-12-09 16:51:06 +08001081 return;
1082 }
1083
1084 if (*str == '!') {
1085 str++;
1086 enable = false;
1087 }
1088
1089 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1090 osi = &osi_setup_entries[i];
1091 if (!strcmp(osi->string, str)) {
1092 osi->enable = enable;
1093 break;
1094 } else if (osi->string[0] == '\0') {
1095 osi->enable = enable;
1096 strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
1097 break;
1098 }
1099 }
Lin Mingd90aa922010-12-09 16:50:52 +08001100}
1101
Len Brownd4b7dc42008-01-23 20:50:56 -05001102static void __init set_osi_linux(unsigned int enable)
1103{
Lin Mingd90aa922010-12-09 16:50:52 +08001104 if (osi_linux.enable != enable)
Len Brownd4b7dc42008-01-23 20:50:56 -05001105 osi_linux.enable = enable;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001106
1107 if (osi_linux.enable)
1108 acpi_osi_setup("Linux");
1109 else
1110 acpi_osi_setup("!Linux");
1111
Len Brownd4b7dc42008-01-23 20:50:56 -05001112 return;
1113}
Len Brownf5076542007-05-30 00:10:38 -04001114
Len Brownd4b7dc42008-01-23 20:50:56 -05001115static void __init acpi_cmdline_osi_linux(unsigned int enable)
1116{
Lin Mingd90aa922010-12-09 16:50:52 +08001117 osi_linux.cmdline = 1; /* cmdline set the default and override DMI */
1118 osi_linux.dmi = 0;
Len Brownd4b7dc42008-01-23 20:50:56 -05001119 set_osi_linux(enable);
Len Brownf5076542007-05-30 00:10:38 -04001120
Len Brownd4b7dc42008-01-23 20:50:56 -05001121 return;
1122}
1123
1124void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1125{
Len Brownd4b7dc42008-01-23 20:50:56 -05001126 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1127
1128 if (enable == -1)
1129 return;
1130
Lin Mingd90aa922010-12-09 16:50:52 +08001131 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
Len Brownd4b7dc42008-01-23 20:50:56 -05001132 set_osi_linux(enable);
1133
Len Brownf5076542007-05-30 00:10:38 -04001134 return;
1135}
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137/*
Len Brownae00d812007-05-29 18:43:33 -04001138 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1139 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -04001141 * string starting with '!' disables that string
1142 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 */
Lin Mingb0ed7a92010-08-06 09:35:51 +08001144static void __init acpi_osi_setup_late(void)
1145{
Lin Ming12d32062010-12-09 16:51:06 +08001146 struct osi_setup_entry *osi;
1147 char *str;
1148 int i;
Lin Mingd90aa922010-12-09 16:50:52 +08001149 acpi_status status;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001150
Lin Ming12d32062010-12-09 16:51:06 +08001151 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1152 osi = &osi_setup_entries[i];
1153 str = osi->string;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001154
Lin Ming12d32062010-12-09 16:51:06 +08001155 if (*str == '\0')
1156 break;
1157 if (osi->enable) {
1158 status = acpi_install_interface(str);
Lin Mingd90aa922010-12-09 16:50:52 +08001159
Lin Ming12d32062010-12-09 16:51:06 +08001160 if (ACPI_SUCCESS(status))
1161 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1162 } else {
1163 status = acpi_remove_interface(str);
Lin Mingd90aa922010-12-09 16:50:52 +08001164
Lin Ming12d32062010-12-09 16:51:06 +08001165 if (ACPI_SUCCESS(status))
1166 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1167 }
Lin Mingb0ed7a92010-08-06 09:35:51 +08001168 }
1169}
1170
Lin Mingd90aa922010-12-09 16:50:52 +08001171static int __init osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Lin Mingd90aa922010-12-09 16:50:52 +08001173 if (str && !strcmp("Linux", str))
1174 acpi_cmdline_osi_linux(1);
1175 else if (str && !strcmp("!Linux", str))
1176 acpi_cmdline_osi_linux(0);
1177 else
1178 acpi_osi_setup(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 return 1;
1181}
1182
Lin Mingd90aa922010-12-09 16:50:52 +08001183__setup("acpi_osi=", osi_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185/* enable serialization to combat AE_ALREADY_EXISTS errors */
Len Brown4be44fc2005-08-05 00:44:28 -04001186static int __init acpi_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 printk(KERN_INFO PREFIX "serialize enabled\n");
1189
1190 acpi_gbl_all_methods_serialized = TRUE;
1191
1192 return 1;
1193}
1194
1195__setup("acpi_serialize", acpi_serialize_setup);
1196
Thomas Renningerdf92e692008-02-04 23:31:22 -08001197/* Check of resource interference between native drivers and ACPI
1198 * OperationRegions (SystemIO and System Memory only).
1199 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1200 * in arbitrary AML code and can interfere with legacy drivers.
1201 * acpi_enforce_resources= can be set to:
1202 *
Luca Tettamanti7e905602009-03-30 00:01:27 +02001203 * - strict (default) (2)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001204 * -> further driver trying to access the resources will not load
Luca Tettamanti7e905602009-03-30 00:01:27 +02001205 * - lax (1)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001206 * -> further driver trying to access the resources will load, but you
1207 * get a system message that something might go wrong...
1208 *
1209 * - no (0)
1210 * -> ACPI Operation Region resources will not be registered
1211 *
1212 */
1213#define ENFORCE_RESOURCES_STRICT 2
1214#define ENFORCE_RESOURCES_LAX 1
1215#define ENFORCE_RESOURCES_NO 0
1216
Luca Tettamanti7e905602009-03-30 00:01:27 +02001217static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001218
1219static int __init acpi_enforce_resources_setup(char *str)
1220{
1221 if (str == NULL || *str == '\0')
1222 return 0;
1223
1224 if (!strcmp("strict", str))
1225 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1226 else if (!strcmp("lax", str))
1227 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1228 else if (!strcmp("no", str))
1229 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1230
1231 return 1;
1232}
1233
1234__setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1235
1236/* Check for resource conflicts between ACPI OperationRegions and native
1237 * drivers */
Jean Delvare876fba42009-11-11 15:22:15 +01001238int acpi_check_resource_conflict(const struct resource *res)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001239{
1240 struct acpi_res_list *res_list_elem;
Thomas Renninger106d1a02010-12-20 12:11:45 +01001241 int ioport = 0, clash = 0;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001242
1243 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1244 return 0;
1245 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1246 return 0;
1247
1248 ioport = res->flags & IORESOURCE_IO;
1249
1250 spin_lock(&acpi_res_lock);
1251 list_for_each_entry(res_list_elem, &resource_list_head,
1252 resource_list) {
1253 if (ioport && (res_list_elem->resource_type
1254 != ACPI_ADR_SPACE_SYSTEM_IO))
1255 continue;
1256 if (!ioport && (res_list_elem->resource_type
1257 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1258 continue;
1259
1260 if (res->end < res_list_elem->start
1261 || res_list_elem->end < res->start)
1262 continue;
1263 clash = 1;
1264 break;
1265 }
1266 spin_unlock(&acpi_res_lock);
1267
1268 if (clash) {
1269 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
Chase Douglas1638bca2010-03-22 15:08:09 -04001270 printk(KERN_WARNING "ACPI: resource %s %pR"
Thomas Renninger106d1a02010-12-20 12:11:45 +01001271 " conflicts with ACPI region %s "
1272 "[%s 0x%zx-0x%zx]\n",
Chase Douglas1638bca2010-03-22 15:08:09 -04001273 res->name, res, res_list_elem->name,
Thomas Renninger106d1a02010-12-20 12:11:45 +01001274 (res_list_elem->resource_type ==
1275 ACPI_ADR_SPACE_SYSTEM_IO) ? "io" : "mem",
1276 (size_t) res_list_elem->start,
1277 (size_t) res_list_elem->end);
Jean Delvare14f03342009-09-08 15:31:46 +02001278 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1279 printk(KERN_NOTICE "ACPI: This conflict may"
1280 " cause random problems and system"
1281 " instability\n");
1282 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1283 " for this device, you should use it instead of"
1284 " the native driver\n");
Thomas Renningerdf92e692008-02-04 23:31:22 -08001285 }
1286 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1287 return -EBUSY;
1288 }
1289 return 0;
1290}
Thomas Renninger443dea72008-02-04 23:31:23 -08001291EXPORT_SYMBOL(acpi_check_resource_conflict);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001292
1293int acpi_check_region(resource_size_t start, resource_size_t n,
1294 const char *name)
1295{
1296 struct resource res = {
1297 .start = start,
1298 .end = start + n - 1,
1299 .name = name,
1300 .flags = IORESOURCE_IO,
1301 };
1302
1303 return acpi_check_resource_conflict(&res);
1304}
1305EXPORT_SYMBOL(acpi_check_region);
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307/*
Jean Delvare70dd6be2010-05-27 19:58:37 +02001308 * Let drivers know whether the resource checks are effective
1309 */
1310int acpi_resources_are_enforced(void)
1311{
1312 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1313}
1314EXPORT_SYMBOL(acpi_resources_are_enforced);
1315
1316/*
Lin Ming9f63b882011-03-23 17:26:34 +08001317 * Create and initialize a spinlock.
1318 */
1319acpi_status
1320acpi_os_create_lock(acpi_spinlock *out_handle)
1321{
1322 spinlock_t *lock;
1323
1324 lock = ACPI_ALLOCATE(sizeof(spinlock_t));
1325 if (!lock)
1326 return AE_NO_MEMORY;
1327 spin_lock_init(lock);
1328 *out_handle = lock;
1329
1330 return AE_OK;
1331}
1332
1333/*
1334 * Deallocate the memory for a spinlock.
1335 */
1336void acpi_os_delete_lock(acpi_spinlock handle)
1337{
1338 ACPI_FREE(handle);
1339}
1340
1341/*
Robert Moore73459f72005-06-24 00:00:00 -04001342 * Acquire a spinlock.
1343 *
1344 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001345 */
1346
Bob Moore967440e2006-06-23 17:04:00 -04001347acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001348{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001349 acpi_cpu_flags flags;
Bob Moore967440e2006-06-23 17:04:00 -04001350 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001351 return flags;
1352}
1353
1354/*
1355 * Release a spinlock. See above.
1356 */
1357
Bob Moore967440e2006-06-23 17:04:00 -04001358void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001359{
Bob Moore967440e2006-06-23 17:04:00 -04001360 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001361}
1362
Robert Moore73459f72005-06-24 00:00:00 -04001363#ifndef ACPI_USE_LOCAL_CACHE
1364
1365/*******************************************************************************
1366 *
1367 * FUNCTION: acpi_os_create_cache
1368 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001369 * PARAMETERS: name - Ascii name for the cache
1370 * size - Size of each cached object
1371 * depth - Maximum depth of the cache (in objects) <ignored>
1372 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001373 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001374 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001375 *
1376 * DESCRIPTION: Create a cache object
1377 *
1378 ******************************************************************************/
1379
1380acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001381acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001382{
Paul Mundt20c2df82007-07-20 10:11:58 +09001383 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001384 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001385 return AE_ERROR;
1386 else
1387 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001388}
1389
1390/*******************************************************************************
1391 *
1392 * FUNCTION: acpi_os_purge_cache
1393 *
1394 * PARAMETERS: Cache - Handle to cache object
1395 *
1396 * RETURN: Status
1397 *
1398 * DESCRIPTION: Free all objects within the requested cache.
1399 *
1400 ******************************************************************************/
1401
Len Brown4be44fc2005-08-05 00:44:28 -04001402acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001403{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001404 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001405 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001406}
1407
1408/*******************************************************************************
1409 *
1410 * FUNCTION: acpi_os_delete_cache
1411 *
1412 * PARAMETERS: Cache - Handle to cache object
1413 *
1414 * RETURN: Status
1415 *
1416 * DESCRIPTION: Free all objects within the requested cache and delete the
1417 * cache object.
1418 *
1419 ******************************************************************************/
1420
Len Brown4be44fc2005-08-05 00:44:28 -04001421acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001422{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001423 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001424 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001425}
1426
1427/*******************************************************************************
1428 *
1429 * FUNCTION: acpi_os_release_object
1430 *
1431 * PARAMETERS: Cache - Handle to cache object
1432 * Object - The object to be released
1433 *
1434 * RETURN: None
1435 *
1436 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1437 * the object is deleted.
1438 *
1439 ******************************************************************************/
1440
Len Brown4be44fc2005-08-05 00:44:28 -04001441acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001442{
Len Brown4be44fc2005-08-05 00:44:28 -04001443 kmem_cache_free(cache, object);
1444 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001445}
1446
Lin Minga5fe1a02009-08-13 10:43:27 +08001447static inline int acpi_res_list_add(struct acpi_res_list *res)
1448{
1449 struct acpi_res_list *res_list_elem;
1450
1451 list_for_each_entry(res_list_elem, &resource_list_head,
1452 resource_list) {
1453
1454 if (res->resource_type == res_list_elem->resource_type &&
1455 res->start == res_list_elem->start &&
1456 res->end == res_list_elem->end) {
1457
1458 /*
1459 * The Region(addr,len) already exist in the list,
1460 * just increase the count
1461 */
1462
1463 res_list_elem->count++;
1464 return 0;
1465 }
1466 }
1467
1468 res->count = 1;
1469 list_add(&res->resource_list, &resource_list_head);
1470 return 1;
1471}
1472
1473static inline void acpi_res_list_del(struct acpi_res_list *res)
1474{
1475 struct acpi_res_list *res_list_elem;
1476
1477 list_for_each_entry(res_list_elem, &resource_list_head,
1478 resource_list) {
1479
1480 if (res->resource_type == res_list_elem->resource_type &&
1481 res->start == res_list_elem->start &&
1482 res->end == res_list_elem->end) {
1483
1484 /*
1485 * If the res count is decreased to 0,
1486 * remove and free it
1487 */
1488
1489 if (--res_list_elem->count == 0) {
1490 list_del(&res_list_elem->resource_list);
1491 kfree(res_list_elem);
1492 }
1493 return;
1494 }
1495 }
1496}
1497
1498acpi_status
1499acpi_os_invalidate_address(
1500 u8 space_id,
1501 acpi_physical_address address,
1502 acpi_size length)
1503{
1504 struct acpi_res_list res;
1505
1506 switch (space_id) {
1507 case ACPI_ADR_SPACE_SYSTEM_IO:
1508 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001509 /* Only interference checks against SystemIO and SystemMemory
Lin Minga5fe1a02009-08-13 10:43:27 +08001510 are needed */
1511 res.start = address;
1512 res.end = address + length - 1;
1513 res.resource_type = space_id;
1514 spin_lock(&acpi_res_lock);
1515 acpi_res_list_del(&res);
1516 spin_unlock(&acpi_res_lock);
1517 break;
1518 case ACPI_ADR_SPACE_PCI_CONFIG:
1519 case ACPI_ADR_SPACE_EC:
1520 case ACPI_ADR_SPACE_SMBUS:
1521 case ACPI_ADR_SPACE_CMOS:
1522 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1523 case ACPI_ADR_SPACE_DATA_TABLE:
1524 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1525 break;
1526 }
1527 return AE_OK;
1528}
1529
Bob Mooreb229cf92006-04-21 17:15:00 -04001530/******************************************************************************
1531 *
1532 * FUNCTION: acpi_os_validate_address
1533 *
1534 * PARAMETERS: space_id - ACPI space ID
1535 * address - Physical address
1536 * length - Address length
1537 *
1538 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1539 * should return AE_AML_ILLEGAL_ADDRESS.
1540 *
1541 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1542 * the addresses accessed by AML operation regions.
1543 *
1544 *****************************************************************************/
1545
1546acpi_status
1547acpi_os_validate_address (
1548 u8 space_id,
1549 acpi_physical_address address,
Thomas Renningerdf92e692008-02-04 23:31:22 -08001550 acpi_size length,
1551 char *name)
Bob Mooreb229cf92006-04-21 17:15:00 -04001552{
Thomas Renningerdf92e692008-02-04 23:31:22 -08001553 struct acpi_res_list *res;
Lin Minga5fe1a02009-08-13 10:43:27 +08001554 int added;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001555 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1556 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001557
Thomas Renningerdf92e692008-02-04 23:31:22 -08001558 switch (space_id) {
1559 case ACPI_ADR_SPACE_SYSTEM_IO:
1560 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001561 /* Only interference checks against SystemIO and SystemMemory
Thomas Renningerdf92e692008-02-04 23:31:22 -08001562 are needed */
1563 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1564 if (!res)
1565 return AE_OK;
1566 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1567 strlcpy(res->name, name, 5);
1568 res->start = address;
1569 res->end = address + length - 1;
1570 res->resource_type = space_id;
1571 spin_lock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001572 added = acpi_res_list_add(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001573 spin_unlock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001574 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1575 "name: %s\n", added ? "Added" : "Already exist",
1576 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001577 ? "SystemIO" : "System Memory",
1578 (unsigned long long)res->start,
1579 (unsigned long long)res->end,
1580 res->name);
Lin Minga5fe1a02009-08-13 10:43:27 +08001581 if (!added)
1582 kfree(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001583 break;
1584 case ACPI_ADR_SPACE_PCI_CONFIG:
1585 case ACPI_ADR_SPACE_EC:
1586 case ACPI_ADR_SPACE_SMBUS:
1587 case ACPI_ADR_SPACE_CMOS:
1588 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1589 case ACPI_ADR_SPACE_DATA_TABLE:
1590 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1591 break;
1592 }
1593 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001594}
Robert Moore73459f72005-06-24 00:00:00 -04001595#endif
Myron Stowed362eda2010-10-21 14:24:04 -06001596
1597acpi_status __init acpi_os_initialize(void)
1598{
1599 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1600 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1601 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1602 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1603
1604 return AE_OK;
1605}
1606
Zhang Rui32d47ee2010-12-08 10:40:36 +08001607acpi_status __init acpi_os_initialize1(void)
Myron Stowed362eda2010-10-21 14:24:04 -06001608{
1609 kacpid_wq = create_workqueue("kacpid");
1610 kacpi_notify_wq = create_workqueue("kacpi_notify");
1611 kacpi_hotplug_wq = create_workqueue("kacpi_hotplug");
1612 BUG_ON(!kacpid_wq);
1613 BUG_ON(!kacpi_notify_wq);
1614 BUG_ON(!kacpi_hotplug_wq);
Len Brown1bd64d42010-10-26 14:50:56 -04001615 acpi_install_interface_handler(acpi_osi_handler);
1616 acpi_osi_setup_late();
Myron Stowed362eda2010-10-21 14:24:04 -06001617 return AE_OK;
1618}
1619
1620acpi_status acpi_os_terminate(void)
1621{
1622 if (acpi_irq_handler) {
1623 acpi_os_remove_interrupt_handler(acpi_irq_irq,
1624 acpi_irq_handler);
1625 }
1626
1627 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1628 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1629 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1630 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1631
1632 destroy_workqueue(kacpid_wq);
1633 destroy_workqueue(kacpi_notify_wq);
1634 destroy_workqueue(kacpi_hotplug_wq);
1635
1636 return AE_OK;
1637}