blob: 0c2e445410ab2871b08d3749f4f054a39177fc64 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
3 *
4 * Copyright (C) 2000 Andrew Henroid
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04007 * Copyright (c) 2008 Intel Corporation
8 * Author: Matthew Wilcox <willy@linux.intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 *
28 */
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/slab.h>
33#include <linux/mm.h>
34#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/interrupt.h>
36#include <linux/kmod.h>
37#include <linux/delay.h>
38#include <linux/workqueue.h>
39#include <linux/nmi.h>
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +030040#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/efi.h>
Thomas Renningerdf92e692008-02-04 23:31:22 -080042#include <linux/ioport.h>
43#include <linux/list.h>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -040044#include <linux/jiffies.h>
45#include <linux/semaphore.h>
46
47#include <asm/io.h>
48#include <asm/uaccess.h>
49
50#include <acpi/acpi.h>
51#include <acpi/acpi_bus.h>
52#include <acpi/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define _COMPONENT ACPI_OS_SERVICES
Len Brownf52fd662007-02-12 22:42:12 -050055ACPI_MODULE_NAME("osl");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define PREFIX "ACPI: "
Len Brown4be44fc2005-08-05 00:44:28 -040057struct acpi_os_dpc {
58 acpi_osd_exec_callback function;
59 void *context;
David Howells65f27f32006-11-22 14:55:48 +000060 struct work_struct work;
Bjorn Helgaas9ac61852009-08-31 22:32:10 +000061 int wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
64#ifdef CONFIG_ACPI_CUSTOM_DSDT
65#include CONFIG_ACPI_CUSTOM_DSDT_FILE
66#endif
67
68#ifdef ENABLE_DEBUGGER
69#include <linux/kdb.h>
70
71/* stuff for debugger support */
72int acpi_in_debugger;
73EXPORT_SYMBOL(acpi_in_debugger);
74
75extern char line_buf[80];
Len Brown4be44fc2005-08-05 00:44:28 -040076#endif /*ENABLE_DEBUGGER */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static unsigned int acpi_irq_irq;
79static acpi_osd_handler acpi_irq_handler;
80static void *acpi_irq_context;
81static struct workqueue_struct *kacpid_wq;
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -040082static struct workqueue_struct *kacpi_notify_wq;
Zhang Ruic02256b2009-06-23 10:20:29 +080083static struct workqueue_struct *kacpi_hotplug_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Thomas Renningerdf92e692008-02-04 23:31:22 -080085struct acpi_res_list {
86 resource_size_t start;
87 resource_size_t end;
88 acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
89 char name[5]; /* only can have a length of 4 chars, make use of this
90 one instead of res->name, no need to kalloc then */
91 struct list_head resource_list;
Lin Minga5fe1a02009-08-13 10:43:27 +080092 int count;
Thomas Renningerdf92e692008-02-04 23:31:22 -080093};
94
95static LIST_HEAD(resource_list_head);
96static DEFINE_SPINLOCK(acpi_res_lock);
97
Myron Stowe620242a2010-10-21 14:23:53 -060098/*
99 * This list of permanent mappings is for memory that may be accessed from
100 * interrupt context, where we can't do the ioremap().
101 */
102struct acpi_ioremap {
103 struct list_head list;
104 void __iomem *virt;
105 acpi_physical_address phys;
106 acpi_size size;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600107 struct kref ref;
Myron Stowe620242a2010-10-21 14:23:53 -0600108};
109
110static LIST_HEAD(acpi_ioremaps);
111static DEFINE_SPINLOCK(acpi_ioremap_lock);
112
Len Brownae00d812007-05-29 18:43:33 -0400113#define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
Lin Mingb0ed7a92010-08-06 09:35:51 +0800114static char osi_setup_string[OSI_STRING_LENGTH_MAX];
115
116static void __init acpi_osi_setup_late(void);
Len Brownae00d812007-05-29 18:43:33 -0400117
Len Brownd4b7dc42008-01-23 20:50:56 -0500118/*
Len Browna6e08872008-11-08 01:21:10 -0500119 * The story of _OSI(Linux)
Len Brownd4b7dc42008-01-23 20:50:56 -0500120 *
Len Browna6e08872008-11-08 01:21:10 -0500121 * From pre-history through Linux-2.6.22,
122 * Linux responded TRUE upon a BIOS OSI(Linux) query.
Len Brownd4b7dc42008-01-23 20:50:56 -0500123 *
Len Browna6e08872008-11-08 01:21:10 -0500124 * Unfortunately, reference BIOS writers got wind of this
125 * and put OSI(Linux) in their example code, quickly exposing
126 * this string as ill-conceived and opening the door to
127 * an un-bounded number of BIOS incompatibilities.
Len Brownd4b7dc42008-01-23 20:50:56 -0500128 *
Len Browna6e08872008-11-08 01:21:10 -0500129 * For example, OSI(Linux) was used on resume to re-POST a
130 * video card on one system, because Linux at that time
131 * could not do a speedy restore in its native driver.
132 * But then upon gaining quick native restore capability,
133 * Linux has no way to tell the BIOS to skip the time-consuming
134 * POST -- putting Linux at a permanent performance disadvantage.
135 * On another system, the BIOS writer used OSI(Linux)
136 * to infer native OS support for IPMI! On other systems,
137 * OSI(Linux) simply got in the way of Linux claiming to
138 * be compatible with other operating systems, exposing
139 * BIOS issues such as skipped device initialization.
Len Brownd4b7dc42008-01-23 20:50:56 -0500140 *
Len Browna6e08872008-11-08 01:21:10 -0500141 * So "Linux" turned out to be a really poor chose of
142 * OSI string, and from Linux-2.6.23 onward we respond FALSE.
Len Brownd4b7dc42008-01-23 20:50:56 -0500143 *
144 * BIOS writers should NOT query _OSI(Linux) on future systems.
Len Browna6e08872008-11-08 01:21:10 -0500145 * Linux will complain on the console when it sees it, and return FALSE.
146 * To get Linux to return TRUE for your system will require
147 * a kernel source update to add a DMI entry,
148 * or boot with "acpi_osi=Linux"
Len Brownd4b7dc42008-01-23 20:50:56 -0500149 */
Len Brownd4b7dc42008-01-23 20:50:56 -0500150
Adrian Bunk1d15d842008-01-29 00:10:15 +0200151static struct osi_linux {
Len Brownd4b7dc42008-01-23 20:50:56 -0500152 unsigned int enable:1;
153 unsigned int dmi:1;
154 unsigned int cmdline:1;
155 unsigned int known:1;
Len Browna6e08872008-11-08 01:21:10 -0500156} osi_linux = { 0, 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 Brown3af283e2010-10-15 21:38:57 -0400162 printk(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
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300244acpi_physical_address __init acpi_os_get_root_pointer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 if (efi_enabled) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800247 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300248 return efi.acpi20;
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800249 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300250 return efi.acpi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400252 printk(KERN_ERR PREFIX
253 "System description tables not found\n");
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
Len Brown239665a2007-11-23 20:08:02 -0500256 } else {
257 acpi_physical_address pa = 0;
258
259 acpi_find_root_pointer(&pa);
260 return pa;
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600264/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe4a3cba52010-10-21 14:24:14 -0600265static struct acpi_ioremap *
266acpi_map_lookup(acpi_physical_address phys, acpi_size size)
Myron Stowe620242a2010-10-21 14:23:53 -0600267{
268 struct acpi_ioremap *map;
269
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600270 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe620242a2010-10-21 14:23:53 -0600271 if (map->phys <= phys &&
272 phys + size <= map->phys + map->size)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600273 return map;
274
275 return NULL;
276}
277
278/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
279static void __iomem *
280acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
281{
282 struct acpi_ioremap *map;
283
284 map = acpi_map_lookup(phys, size);
285 if (map)
286 return map->virt + (phys - map->phys);
Myron Stowe620242a2010-10-21 14:23:53 -0600287
288 return NULL;
289}
290
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600291/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe620242a2010-10-21 14:23:53 -0600292static struct acpi_ioremap *
293acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
294{
295 struct acpi_ioremap *map;
296
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600297 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600298 if (map->virt <= virt &&
299 virt + size <= map->virt + map->size)
Myron Stowe620242a2010-10-21 14:23:53 -0600300 return map;
301
302 return NULL;
303}
304
Jan Beulich2fdf0742007-12-13 08:33:59 +0000305void __iomem *__init_refok
306acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Myron Stowe4a3cba52010-10-21 14:24:14 -0600308 struct acpi_ioremap *map, *tmp_map;
309 unsigned long flags, pg_sz;
Myron Stowe620242a2010-10-21 14:23:53 -0600310 void __iomem *virt;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600311 phys_addr_t pg_off;
Myron Stowe620242a2010-10-21 14:23:53 -0600312
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800313 if (phys > ULONG_MAX) {
314 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
Randy Dunlap70c08462007-02-13 16:11:36 -0800315 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
Myron Stowe620242a2010-10-21 14:23:53 -0600317
318 if (!acpi_gbl_permanent_mmap)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300319 return __acpi_map_table((unsigned long)phys, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600320
321 map = kzalloc(sizeof(*map), GFP_KERNEL);
322 if (!map)
323 return NULL;
324
Myron Stowe4a3cba52010-10-21 14:24:14 -0600325 pg_off = round_down(phys, PAGE_SIZE);
326 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
327 virt = ioremap(pg_off, pg_sz);
Myron Stowe620242a2010-10-21 14:23:53 -0600328 if (!virt) {
329 kfree(map);
330 return NULL;
331 }
332
333 INIT_LIST_HEAD(&map->list);
334 map->virt = virt;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600335 map->phys = pg_off;
336 map->size = pg_sz;
337 kref_init(&map->ref);
Myron Stowe620242a2010-10-21 14:23:53 -0600338
339 spin_lock_irqsave(&acpi_ioremap_lock, flags);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600340 /* Check if page has already been mapped. */
341 tmp_map = acpi_map_lookup(phys, size);
342 if (tmp_map) {
343 kref_get(&tmp_map->ref);
344 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
345 iounmap(map->virt);
346 kfree(map);
347 return tmp_map->virt + (phys - tmp_map->phys);
348 }
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600349 list_add_tail_rcu(&map->list, &acpi_ioremaps);
Myron Stowe620242a2010-10-21 14:23:53 -0600350 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
351
Myron Stowe4a3cba52010-10-21 14:24:14 -0600352 return map->virt + (phys - map->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800354EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Myron Stowe4a3cba52010-10-21 14:24:14 -0600356static void acpi_kref_del_iomap(struct kref *ref)
357{
358 struct acpi_ioremap *map;
359
360 map = container_of(ref, struct acpi_ioremap, ref);
361 list_del_rcu(&map->list);
362}
363
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800364void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Myron Stowe620242a2010-10-21 14:23:53 -0600366 struct acpi_ioremap *map;
367 unsigned long flags;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600368 int del;
Myron Stowe620242a2010-10-21 14:23:53 -0600369
370 if (!acpi_gbl_permanent_mmap) {
Yinghai Lu7d972772009-02-07 15:39:41 -0800371 __acpi_unmap_table(virt, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600372 return;
373 }
374
375 spin_lock_irqsave(&acpi_ioremap_lock, flags);
376 map = acpi_map_lookup_virt(virt, size);
377 if (!map) {
378 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
379 printk(KERN_ERR PREFIX "%s: bad address %p\n", __func__, virt);
380 dump_stack();
381 return;
382 }
383
Myron Stowe4a3cba52010-10-21 14:24:14 -0600384 del = kref_put(&map->ref, acpi_kref_del_iomap);
Myron Stowe620242a2010-10-21 14:23:53 -0600385 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
386
Myron Stowe4a3cba52010-10-21 14:24:14 -0600387 if (!del)
388 return;
389
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600390 synchronize_rcu();
Myron Stowe620242a2010-10-21 14:23:53 -0600391 iounmap(map->virt);
392 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800394EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800396void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Yinghai Lu7d972772009-02-07 15:39:41 -0800397{
398 if (!acpi_gbl_permanent_mmap)
399 __acpi_unmap_table(virt, size);
400}
401
Myron Stowe29718522010-10-21 14:23:59 -0600402int acpi_os_map_generic_address(struct acpi_generic_address *addr)
403{
404 void __iomem *virt;
405
406 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
407 return 0;
408
409 if (!addr->address || !addr->bit_width)
410 return -EINVAL;
411
412 virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
413 if (!virt)
414 return -EIO;
415
416 return 0;
417}
418EXPORT_SYMBOL_GPL(acpi_os_map_generic_address);
419
420void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
421{
422 void __iomem *virt;
423 unsigned long flags;
424 acpi_size size = addr->bit_width / 8;
425
426 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
427 return;
428
429 if (!addr->address || !addr->bit_width)
430 return;
431
432 spin_lock_irqsave(&acpi_ioremap_lock, flags);
433 virt = acpi_map_vaddr_lookup(addr->address, size);
434 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
435
436 acpi_os_unmap_memory(virt, size);
437}
438EXPORT_SYMBOL_GPL(acpi_os_unmap_generic_address);
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440#ifdef ACPI_FUTURE_USAGE
441acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400442acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Len Brown4be44fc2005-08-05 00:44:28 -0400444 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return AE_BAD_PARAMETER;
446
447 *phys = virt_to_phys(virt);
448
449 return AE_OK;
450}
451#endif
452
453#define ACPI_MAX_OVERRIDE_LEN 100
454
455static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
456
457acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400458acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
459 acpi_string * new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 if (!init_val || !new_val)
462 return AE_BAD_PARAMETER;
463
464 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400465 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400467 acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 *new_val = acpi_os_name;
469 }
470
471 return AE_OK;
472}
473
474acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400475acpi_os_table_override(struct acpi_table_header * existing_table,
476 struct acpi_table_header ** new_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 if (!existing_table || !new_table)
479 return AE_BAD_PARAMETER;
480
Markus Gaugusch71fc47a2008-02-05 00:04:06 +0100481 *new_table = NULL;
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483#ifdef CONFIG_ACPI_CUSTOM_DSDT
484 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400485 *new_table = (struct acpi_table_header *)AmlCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486#endif
Éric Piel6ed31e92008-02-05 00:04:50 +0100487 if (*new_table != NULL) {
488 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
489 "this is unsafe: tainting kernel\n",
490 existing_table->signature,
491 existing_table->oem_table_id);
492 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return AE_OK;
495}
496
David Howells7d12e782006-10-05 14:55:46 +0100497static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Len Brown5229e872008-02-06 01:26:55 -0500499 u32 handled;
500
501 handled = (*acpi_irq_handler) (acpi_irq_context);
502
503 if (handled) {
504 acpi_irq_handled++;
505 return IRQ_HANDLED;
Len Brown88bea182009-04-21 00:35:47 -0400506 } else {
507 acpi_irq_not_handled++;
Len Brown5229e872008-02-06 01:26:55 -0500508 return IRQ_NONE;
Len Brown88bea182009-04-21 00:35:47 -0400509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400513acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
514 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 unsigned int irq;
517
Len Brown5229e872008-02-06 01:26:55 -0500518 acpi_irq_stats_init();
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /*
521 * Ignore the GSI from the core, and use the value in our copy of the
522 * FADT. It may not be the same if an interrupt source override exists
523 * for the SCI.
524 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300525 gsi = acpi_gbl_FADT.sci_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
527 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
528 gsi);
529 return AE_OK;
530 }
531
532 acpi_irq_handler = handler;
533 acpi_irq_context = context;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700534 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
536 return AE_NOT_ACQUIRED;
537 }
538 acpi_irq_irq = irq;
539
540 return AE_OK;
541}
542
Len Brown4be44fc2005-08-05 00:44:28 -0400543acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 if (irq) {
546 free_irq(irq, acpi_irq);
547 acpi_irq_handler = NULL;
548 acpi_irq_irq = 0;
549 }
550
551 return AE_OK;
552}
553
554/*
555 * Running in interpreter thread context, safe to sleep
556 */
557
Lin Ming439913f2010-01-28 10:53:19 +0800558void acpi_os_sleep(u64 ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800560 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
Len Brown4be44fc2005-08-05 00:44:28 -0400562
Len Brown4be44fc2005-08-05 00:44:28 -0400563void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 while (us) {
566 u32 delay = 1000;
567
568 if (delay > us)
569 delay = us;
570 udelay(delay);
571 touch_nmi_watchdog();
572 us -= delay;
573 }
574}
Len Brown4be44fc2005-08-05 00:44:28 -0400575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/*
577 * Support ACPI 3.0 AML Timer operand
578 * Returns 64-bit free-running, monotonically increasing timer
579 * with 100ns granularity
580 */
Len Brown4be44fc2005-08-05 00:44:28 -0400581u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 static u64 t;
584
585#ifdef CONFIG_HPET
586 /* TBD: use HPET if available */
587#endif
588
589#ifdef CONFIG_X86_PM_TIMER
590 /* TBD: default to PM timer if HPET was not available */
591#endif
592 if (!t)
593 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
594
595 return ++t;
596}
597
Len Brown4be44fc2005-08-05 00:44:28 -0400598acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 u32 dummy;
601
602 if (!value)
603 value = &dummy;
604
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800605 *value = 0;
606 if (width <= 8) {
Len Brown4be44fc2005-08-05 00:44:28 -0400607 *(u8 *) value = inb(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800608 } else if (width <= 16) {
Len Brown4be44fc2005-08-05 00:44:28 -0400609 *(u16 *) value = inw(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800610 } else if (width <= 32) {
Len Brown4be44fc2005-08-05 00:44:28 -0400611 *(u32 *) value = inl(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800612 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 BUG();
614 }
615
616 return AE_OK;
617}
Len Brown4be44fc2005-08-05 00:44:28 -0400618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619EXPORT_SYMBOL(acpi_os_read_port);
620
Len Brown4be44fc2005-08-05 00:44:28 -0400621acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800623 if (width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 outb(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800625 } else if (width <= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 outw(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800627 } else if (width <= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 outl(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800629 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 BUG();
631 }
632
633 return AE_OK;
634}
Len Brown4be44fc2005-08-05 00:44:28 -0400635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636EXPORT_SYMBOL(acpi_os_write_port);
637
638acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400639acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Len Brown4be44fc2005-08-05 00:44:28 -0400641 u32 dummy;
642 void __iomem *virt_addr;
Myron Stowe620242a2010-10-21 14:23:53 -0600643 int size = width / 8, unmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600645 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600646 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600647 rcu_read_unlock();
Myron Stowe620242a2010-10-21 14:23:53 -0600648 if (!virt_addr) {
649 virt_addr = ioremap(phys_addr, size);
650 unmap = 1;
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (!value)
653 value = &dummy;
654
655 switch (width) {
656 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400657 *(u8 *) value = readb(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
659 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400660 *(u16 *) value = readw(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 break;
662 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400663 *(u32 *) value = readl(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 break;
665 default:
666 BUG();
667 }
668
Myron Stowe620242a2010-10-21 14:23:53 -0600669 if (unmap)
670 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 return AE_OK;
673}
674
675acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400676acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Len Brown4be44fc2005-08-05 00:44:28 -0400678 void __iomem *virt_addr;
Myron Stowe620242a2010-10-21 14:23:53 -0600679 int size = width / 8, unmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600681 rcu_read_lock();
Myron Stowe620242a2010-10-21 14:23:53 -0600682 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600683 rcu_read_unlock();
Myron Stowe620242a2010-10-21 14:23:53 -0600684 if (!virt_addr) {
685 virt_addr = ioremap(phys_addr, size);
686 unmap = 1;
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 switch (width) {
690 case 8:
691 writeb(value, virt_addr);
692 break;
693 case 16:
694 writew(value, virt_addr);
695 break;
696 case 32:
697 writel(value, virt_addr);
698 break;
699 default:
700 BUG();
701 }
702
Myron Stowe620242a2010-10-21 14:23:53 -0600703 if (unmap)
704 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 return AE_OK;
707}
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400710acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Bob Moorec5f02312010-08-06 08:57:53 +0800711 u64 *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 int result, size;
Bob Moorec5f02312010-08-06 08:57:53 +0800714 u32 value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if (!value)
717 return AE_BAD_PARAMETER;
718
719 switch (width) {
720 case 8:
721 size = 1;
722 break;
723 case 16:
724 size = 2;
725 break;
726 case 32:
727 size = 4;
728 break;
729 default:
730 return AE_ERROR;
731 }
732
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500733 result = raw_pci_read(pci_id->segment, pci_id->bus,
734 PCI_DEVFN(pci_id->device, pci_id->function),
Bob Moorec5f02312010-08-06 08:57:53 +0800735 reg, size, &value32);
736 *value = value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 return (result ? AE_ERROR : AE_OK);
739}
Len Brown4be44fc2005-08-05 00:44:28 -0400740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400742acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Lin Ming439913f2010-01-28 10:53:19 +0800743 u64 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 int result, size;
746
747 switch (width) {
748 case 8:
749 size = 1;
750 break;
751 case 16:
752 size = 2;
753 break;
754 case 32:
755 size = 4;
756 break;
757 default:
758 return AE_ERROR;
759 }
760
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500761 result = raw_pci_write(pci_id->segment, pci_id->bus,
762 PCI_DEVFN(pci_id->device, pci_id->function),
763 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 return (result ? AE_ERROR : AE_OK);
766}
767
David Howells65f27f32006-11-22 14:55:48 +0000768static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
David Howells65f27f32006-11-22 14:55:48 +0000770 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400771
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000772 if (dpc->wait)
773 acpi_os_wait_events_complete(NULL);
Zhang Rui19cd8472008-08-28 10:05:06 +0800774
775 dpc->function(dpc->context);
776 kfree(dpc);
Zhang Rui19cd8472008-08-28 10:05:06 +0800777}
778
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400779/*******************************************************************************
780 *
781 * FUNCTION: acpi_os_execute
782 *
783 * PARAMETERS: Type - Type of the callback
784 * Function - Function to be executed
785 * Context - Function parameters
786 *
787 * RETURN: Status
788 *
789 * DESCRIPTION: Depending on type, either queues function for deferred execution or
790 * immediately executes function on a separate thread.
791 *
792 ******************************************************************************/
793
Zhang Rui19cd8472008-08-28 10:05:06 +0800794static acpi_status __acpi_os_execute(acpi_execute_type type,
795 acpi_osd_exec_callback function, void *context, int hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Len Brown4be44fc2005-08-05 00:44:28 -0400797 acpi_status status = AE_OK;
798 struct acpi_os_dpc *dpc;
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300799 struct workqueue_struct *queue;
Zhang Rui19cd8472008-08-28 10:05:06 +0800800 int ret;
Len Brown72945b22006-07-12 22:46:42 -0400801 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
802 "Scheduling function [%p(%p)] for deferred execution.\n",
803 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /*
806 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +0000807 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * way that allows us to also free its memory inside the callee.
809 * Because we may want to schedule several tasks with different
810 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +0000811 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
Len Brown72945b22006-07-12 22:46:42 -0400813
David Howells65f27f32006-11-22 14:55:48 +0000814 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!dpc)
Lin Ming889c78b2008-12-31 09:23:57 +0800816 return AE_NO_MEMORY;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 dpc->function = function;
819 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800820
Zhang Ruic02256b2009-06-23 10:20:29 +0800821 /*
822 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
823 * because the hotplug code may call driver .remove() functions,
824 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
825 * to flush these workqueues.
826 */
827 queue = hp ? kacpi_hotplug_wq :
828 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000829 dpc->wait = hp ? 1 : 0;
Zhang Ruibc736752010-03-22 15:48:54 +0800830
831 if (queue == kacpi_hotplug_wq)
832 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
833 else if (queue == kacpi_notify_wq)
834 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
835 else
836 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
837
Tejun Heo8fec62b2010-06-29 10:07:09 +0200838 /*
839 * On some machines, a software-initiated SMI causes corruption unless
840 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
841 * typically it's done in GPE-related methods that are run via
842 * workqueues, so we can avoid the known corruption cases by always
843 * queueing on CPU 0.
844 */
845 ret = queue_work_on(0, queue, &dpc->work);
Zhang Rui19cd8472008-08-28 10:05:06 +0800846
847 if (!ret) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800848 printk(KERN_ERR PREFIX
849 "Call to queue_work() failed.\n");
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300850 status = AE_ERROR;
851 kfree(dpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
Lin Ming889c78b2008-12-31 09:23:57 +0800853 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
Len Brown4be44fc2005-08-05 00:44:28 -0400855
Zhang Rui19cd8472008-08-28 10:05:06 +0800856acpi_status acpi_os_execute(acpi_execute_type type,
857 acpi_osd_exec_callback function, void *context)
858{
859 return __acpi_os_execute(type, function, context, 0);
860}
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400861EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Zhang Rui19cd8472008-08-28 10:05:06 +0800863acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
864 void *context)
865{
866 return __acpi_os_execute(0, function, context, 1);
867}
868
Len Brown4be44fc2005-08-05 00:44:28 -0400869void acpi_os_wait_events_complete(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 flush_workqueue(kacpid_wq);
Zhang Rui2f67a062008-04-29 02:34:42 -0400872 flush_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
Len Brown4be44fc2005-08-05 00:44:28 -0400874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875EXPORT_SYMBOL(acpi_os_wait_events_complete);
876
877/*
878 * Allocate the memory for a spinlock and initialize it.
879 */
Bob Moore967440e32006-06-23 17:04:00 -0400880acpi_status acpi_os_create_lock(acpi_spinlock * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Bob Moore967440e32006-06-23 17:04:00 -0400882 spin_lock_init(*handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Patrick Mocheld550d982006-06-27 00:41:40 -0400884 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887/*
888 * Deallocate the memory for a spinlock.
889 */
Bob Moore967440e32006-06-23 17:04:00 -0400890void acpi_os_delete_lock(acpi_spinlock handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Patrick Mocheld550d982006-06-27 00:41:40 -0400892 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400896acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Len Brown4be44fc2005-08-05 00:44:28 -0400898 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 sem = acpi_os_allocate(sizeof(struct semaphore));
901 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400902 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 memset(sem, 0, sizeof(struct semaphore));
904
905 sema_init(sem, initial_units);
906
Len Brown4be44fc2005-08-05 00:44:28 -0400907 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Len Brown4be44fc2005-08-05 00:44:28 -0400909 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
910 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Patrick Mocheld550d982006-06-27 00:41:40 -0400912 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915/*
916 * TODO: A better way to delete semaphores? Linux doesn't have a
917 * 'delete_semaphore()' function -- may result in an invalid
918 * pointer dereference for non-synchronized consumers. Should
919 * we at least check for blocked threads and signal/cancel them?
920 */
921
Len Brown4be44fc2005-08-05 00:44:28 -0400922acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Len Brown4be44fc2005-08-05 00:44:28 -0400924 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400927 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Len Brown4be44fc2005-08-05 00:44:28 -0400929 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400931 BUG_ON(!list_empty(&sem->wait_list));
Len Brown02438d82006-06-30 03:19:10 -0400932 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -0400933 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Patrick Mocheld550d982006-06-27 00:41:40 -0400935 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 * TODO: Support for units > 1?
940 */
Len Brown4be44fc2005-08-05 00:44:28 -0400941acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Len Brown4be44fc2005-08-05 00:44:28 -0400943 acpi_status status = AE_OK;
944 struct semaphore *sem = (struct semaphore *)handle;
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400945 long jiffies;
Len Brown4be44fc2005-08-05 00:44:28 -0400946 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400949 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400952 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Len Brown4be44fc2005-08-05 00:44:28 -0400954 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
955 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400957 if (timeout == ACPI_WAIT_FOREVER)
958 jiffies = MAX_SCHEDULE_TIMEOUT;
959 else
960 jiffies = msecs_to_jiffies(timeout);
961
962 ret = down_timeout(sem, jiffies);
963 if (ret)
964 status = AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -0400967 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400968 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -0400969 handle, units, timeout,
970 acpi_format_exception(status)));
971 } else {
972 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400973 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400974 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
Patrick Mocheld550d982006-06-27 00:41:40 -0400977 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980/*
981 * TODO: Support for units > 1?
982 */
Len Brown4be44fc2005-08-05 00:44:28 -0400983acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Len Brown4be44fc2005-08-05 00:44:28 -0400985 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400988 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400991 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Len Brown4be44fc2005-08-05 00:44:28 -0400993 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
994 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 up(sem);
997
Patrick Mocheld550d982006-06-27 00:41:40 -0400998 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
Len Brown4be44fc2005-08-05 00:44:28 -04001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -04001002u32 acpi_os_get_line(char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004
1005#ifdef ENABLE_DEBUGGER
1006 if (acpi_in_debugger) {
1007 u32 chars;
1008
1009 kdb_read(buffer, sizeof(line_buf));
1010
1011 /* remove the CR kdb includes */
1012 chars = strlen(buffer) - 1;
1013 buffer[chars] = '\0';
1014 }
1015#endif
1016
1017 return 0;
1018}
Len Brown4be44fc2005-08-05 00:44:28 -04001019#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Len Brown4be44fc2005-08-05 00:44:28 -04001021acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Len Brown4be44fc2005-08-05 00:44:28 -04001023 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 case ACPI_SIGNAL_FATAL:
1025 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1026 break;
1027 case ACPI_SIGNAL_BREAKPOINT:
1028 /*
1029 * AML Breakpoint
1030 * ACPI spec. says to treat it as a NOP unless
1031 * you are debugging. So if/when we integrate
1032 * AML debugger into the kernel debugger its
1033 * hook will go here. But until then it is
1034 * not useful to print anything on breakpoints.
1035 */
1036 break;
1037 default:
1038 break;
1039 }
1040
1041 return AE_OK;
1042}
Len Brown4be44fc2005-08-05 00:44:28 -04001043
Len Brown4be44fc2005-08-05 00:44:28 -04001044static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -04001047 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 if (!str || !*str)
1050 return 0;
1051
1052 for (; count-- && str && *str; str++) {
1053 if (isalnum(*str) || *str == ' ' || *str == ':')
1054 *p++ = *str;
1055 else if (*str == '\'' || *str == '"')
1056 continue;
1057 else
1058 break;
1059 }
1060 *p = 0;
1061
1062 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066__setup("acpi_os_name=", acpi_os_name_setup);
1067
Len Brownd4b7dc42008-01-23 20:50:56 -05001068static void __init set_osi_linux(unsigned int enable)
1069{
1070 if (osi_linux.enable != enable) {
1071 osi_linux.enable = enable;
1072 printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
1073 enable ? "Add": "Delet");
1074 }
Lin Mingb0ed7a92010-08-06 09:35:51 +08001075
1076 if (osi_linux.enable)
1077 acpi_osi_setup("Linux");
1078 else
1079 acpi_osi_setup("!Linux");
1080
Len Brownd4b7dc42008-01-23 20:50:56 -05001081 return;
1082}
Len Brownf5076542007-05-30 00:10:38 -04001083
Len Brownd4b7dc42008-01-23 20:50:56 -05001084static void __init acpi_cmdline_osi_linux(unsigned int enable)
1085{
1086 osi_linux.cmdline = 1; /* cmdline set the default */
1087 set_osi_linux(enable);
Len Brownf5076542007-05-30 00:10:38 -04001088
Len Brownd4b7dc42008-01-23 20:50:56 -05001089 return;
1090}
1091
1092void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1093{
1094 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
1095
1096 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1097
1098 if (enable == -1)
1099 return;
1100
1101 osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
1102
1103 set_osi_linux(enable);
1104
Len Brownf5076542007-05-30 00:10:38 -04001105 return;
1106}
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108/*
Len Brownae00d812007-05-29 18:43:33 -04001109 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1110 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -04001112 * string starting with '!' disables that string
1113 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 */
Lin Mingb0ed7a92010-08-06 09:35:51 +08001115static void __init acpi_osi_setup_late(void)
1116{
1117 char *str = osi_setup_string;
1118
1119 if (*str == '\0')
1120 return;
1121
1122 if (!strcmp("!Linux", str)) {
1123 acpi_cmdline_osi_linux(0); /* !enable */
1124 } else if (*str == '!') {
1125 if (acpi_remove_interface(++str) == AE_OK)
1126 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1127 } else if (!strcmp("Linux", str)) {
1128 acpi_cmdline_osi_linux(1); /* enable */
1129 } else {
1130 if (acpi_install_interface(str) == AE_OK)
1131 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1132 }
1133}
1134
Len Brown46c1fbd2008-02-13 23:13:25 -05001135int __init acpi_osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 if (str == NULL || *str == '\0') {
1138 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1139 acpi_gbl_create_osi_method = FALSE;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001140 } else {
1141 strncpy(osi_setup_string, str, OSI_STRING_LENGTH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
1144 return 1;
1145}
1146
1147__setup("acpi_osi=", acpi_osi_setup);
1148
1149/* enable serialization to combat AE_ALREADY_EXISTS errors */
Len Brown4be44fc2005-08-05 00:44:28 -04001150static int __init acpi_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 printk(KERN_INFO PREFIX "serialize enabled\n");
1153
1154 acpi_gbl_all_methods_serialized = TRUE;
1155
1156 return 1;
1157}
1158
1159__setup("acpi_serialize", acpi_serialize_setup);
1160
Thomas Renningerdf92e692008-02-04 23:31:22 -08001161/* Check of resource interference between native drivers and ACPI
1162 * OperationRegions (SystemIO and System Memory only).
1163 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1164 * in arbitrary AML code and can interfere with legacy drivers.
1165 * acpi_enforce_resources= can be set to:
1166 *
Luca Tettamanti7e905602009-03-30 00:01:27 +02001167 * - strict (default) (2)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001168 * -> further driver trying to access the resources will not load
Luca Tettamanti7e905602009-03-30 00:01:27 +02001169 * - lax (1)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001170 * -> further driver trying to access the resources will load, but you
1171 * get a system message that something might go wrong...
1172 *
1173 * - no (0)
1174 * -> ACPI Operation Region resources will not be registered
1175 *
1176 */
1177#define ENFORCE_RESOURCES_STRICT 2
1178#define ENFORCE_RESOURCES_LAX 1
1179#define ENFORCE_RESOURCES_NO 0
1180
Luca Tettamanti7e905602009-03-30 00:01:27 +02001181static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001182
1183static int __init acpi_enforce_resources_setup(char *str)
1184{
1185 if (str == NULL || *str == '\0')
1186 return 0;
1187
1188 if (!strcmp("strict", str))
1189 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1190 else if (!strcmp("lax", str))
1191 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1192 else if (!strcmp("no", str))
1193 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1194
1195 return 1;
1196}
1197
1198__setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1199
1200/* Check for resource conflicts between ACPI OperationRegions and native
1201 * drivers */
Jean Delvare876fba42009-11-11 15:22:15 +01001202int acpi_check_resource_conflict(const struct resource *res)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001203{
1204 struct acpi_res_list *res_list_elem;
1205 int ioport;
1206 int clash = 0;
1207
1208 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1209 return 0;
1210 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1211 return 0;
1212
1213 ioport = res->flags & IORESOURCE_IO;
1214
1215 spin_lock(&acpi_res_lock);
1216 list_for_each_entry(res_list_elem, &resource_list_head,
1217 resource_list) {
1218 if (ioport && (res_list_elem->resource_type
1219 != ACPI_ADR_SPACE_SYSTEM_IO))
1220 continue;
1221 if (!ioport && (res_list_elem->resource_type
1222 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1223 continue;
1224
1225 if (res->end < res_list_elem->start
1226 || res_list_elem->end < res->start)
1227 continue;
1228 clash = 1;
1229 break;
1230 }
1231 spin_unlock(&acpi_res_lock);
1232
1233 if (clash) {
1234 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
Chase Douglas1638bca2010-03-22 15:08:09 -04001235 printk(KERN_WARNING "ACPI: resource %s %pR"
1236 " conflicts with ACPI region %s %pR\n",
1237 res->name, res, res_list_elem->name,
1238 res_list_elem);
Jean Delvare14f03342009-09-08 15:31:46 +02001239 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1240 printk(KERN_NOTICE "ACPI: This conflict may"
1241 " cause random problems and system"
1242 " instability\n");
1243 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1244 " for this device, you should use it instead of"
1245 " the native driver\n");
Thomas Renningerdf92e692008-02-04 23:31:22 -08001246 }
1247 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1248 return -EBUSY;
1249 }
1250 return 0;
1251}
Thomas Renninger443dea72008-02-04 23:31:23 -08001252EXPORT_SYMBOL(acpi_check_resource_conflict);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001253
1254int acpi_check_region(resource_size_t start, resource_size_t n,
1255 const char *name)
1256{
1257 struct resource res = {
1258 .start = start,
1259 .end = start + n - 1,
1260 .name = name,
1261 .flags = IORESOURCE_IO,
1262 };
1263
1264 return acpi_check_resource_conflict(&res);
1265}
1266EXPORT_SYMBOL(acpi_check_region);
1267
1268int acpi_check_mem_region(resource_size_t start, resource_size_t n,
1269 const char *name)
1270{
1271 struct resource res = {
1272 .start = start,
1273 .end = start + n - 1,
1274 .name = name,
1275 .flags = IORESOURCE_MEM,
1276 };
1277
1278 return acpi_check_resource_conflict(&res);
1279
1280}
1281EXPORT_SYMBOL(acpi_check_mem_region);
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283/*
Jean Delvare70dd6be2010-05-27 19:58:37 +02001284 * Let drivers know whether the resource checks are effective
1285 */
1286int acpi_resources_are_enforced(void)
1287{
1288 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1289}
1290EXPORT_SYMBOL(acpi_resources_are_enforced);
1291
1292/*
Robert Moore73459f72005-06-24 00:00:00 -04001293 * Acquire a spinlock.
1294 *
1295 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001296 */
1297
Bob Moore967440e32006-06-23 17:04:00 -04001298acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001299{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001300 acpi_cpu_flags flags;
Bob Moore967440e32006-06-23 17:04:00 -04001301 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001302 return flags;
1303}
1304
1305/*
1306 * Release a spinlock. See above.
1307 */
1308
Bob Moore967440e32006-06-23 17:04:00 -04001309void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001310{
Bob Moore967440e32006-06-23 17:04:00 -04001311 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001312}
1313
Robert Moore73459f72005-06-24 00:00:00 -04001314#ifndef ACPI_USE_LOCAL_CACHE
1315
1316/*******************************************************************************
1317 *
1318 * FUNCTION: acpi_os_create_cache
1319 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001320 * PARAMETERS: name - Ascii name for the cache
1321 * size - Size of each cached object
1322 * depth - Maximum depth of the cache (in objects) <ignored>
1323 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001324 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001325 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001326 *
1327 * DESCRIPTION: Create a cache object
1328 *
1329 ******************************************************************************/
1330
1331acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001332acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001333{
Paul Mundt20c2df82007-07-20 10:11:58 +09001334 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001335 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001336 return AE_ERROR;
1337 else
1338 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001339}
1340
1341/*******************************************************************************
1342 *
1343 * FUNCTION: acpi_os_purge_cache
1344 *
1345 * PARAMETERS: Cache - Handle to cache object
1346 *
1347 * RETURN: Status
1348 *
1349 * DESCRIPTION: Free all objects within the requested cache.
1350 *
1351 ******************************************************************************/
1352
Len Brown4be44fc2005-08-05 00:44:28 -04001353acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001354{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001355 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001356 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001357}
1358
1359/*******************************************************************************
1360 *
1361 * FUNCTION: acpi_os_delete_cache
1362 *
1363 * PARAMETERS: Cache - Handle to cache object
1364 *
1365 * RETURN: Status
1366 *
1367 * DESCRIPTION: Free all objects within the requested cache and delete the
1368 * cache object.
1369 *
1370 ******************************************************************************/
1371
Len Brown4be44fc2005-08-05 00:44:28 -04001372acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001373{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001374 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001375 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001376}
1377
1378/*******************************************************************************
1379 *
1380 * FUNCTION: acpi_os_release_object
1381 *
1382 * PARAMETERS: Cache - Handle to cache object
1383 * Object - The object to be released
1384 *
1385 * RETURN: None
1386 *
1387 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1388 * the object is deleted.
1389 *
1390 ******************************************************************************/
1391
Len Brown4be44fc2005-08-05 00:44:28 -04001392acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001393{
Len Brown4be44fc2005-08-05 00:44:28 -04001394 kmem_cache_free(cache, object);
1395 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001396}
1397
Lin Minga5fe1a02009-08-13 10:43:27 +08001398static inline int acpi_res_list_add(struct acpi_res_list *res)
1399{
1400 struct acpi_res_list *res_list_elem;
1401
1402 list_for_each_entry(res_list_elem, &resource_list_head,
1403 resource_list) {
1404
1405 if (res->resource_type == res_list_elem->resource_type &&
1406 res->start == res_list_elem->start &&
1407 res->end == res_list_elem->end) {
1408
1409 /*
1410 * The Region(addr,len) already exist in the list,
1411 * just increase the count
1412 */
1413
1414 res_list_elem->count++;
1415 return 0;
1416 }
1417 }
1418
1419 res->count = 1;
1420 list_add(&res->resource_list, &resource_list_head);
1421 return 1;
1422}
1423
1424static inline void acpi_res_list_del(struct acpi_res_list *res)
1425{
1426 struct acpi_res_list *res_list_elem;
1427
1428 list_for_each_entry(res_list_elem, &resource_list_head,
1429 resource_list) {
1430
1431 if (res->resource_type == res_list_elem->resource_type &&
1432 res->start == res_list_elem->start &&
1433 res->end == res_list_elem->end) {
1434
1435 /*
1436 * If the res count is decreased to 0,
1437 * remove and free it
1438 */
1439
1440 if (--res_list_elem->count == 0) {
1441 list_del(&res_list_elem->resource_list);
1442 kfree(res_list_elem);
1443 }
1444 return;
1445 }
1446 }
1447}
1448
1449acpi_status
1450acpi_os_invalidate_address(
1451 u8 space_id,
1452 acpi_physical_address address,
1453 acpi_size length)
1454{
1455 struct acpi_res_list res;
1456
1457 switch (space_id) {
1458 case ACPI_ADR_SPACE_SYSTEM_IO:
1459 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001460 /* Only interference checks against SystemIO and SystemMemory
Lin Minga5fe1a02009-08-13 10:43:27 +08001461 are needed */
1462 res.start = address;
1463 res.end = address + length - 1;
1464 res.resource_type = space_id;
1465 spin_lock(&acpi_res_lock);
1466 acpi_res_list_del(&res);
1467 spin_unlock(&acpi_res_lock);
1468 break;
1469 case ACPI_ADR_SPACE_PCI_CONFIG:
1470 case ACPI_ADR_SPACE_EC:
1471 case ACPI_ADR_SPACE_SMBUS:
1472 case ACPI_ADR_SPACE_CMOS:
1473 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1474 case ACPI_ADR_SPACE_DATA_TABLE:
1475 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1476 break;
1477 }
1478 return AE_OK;
1479}
1480
Bob Mooreb229cf92006-04-21 17:15:00 -04001481/******************************************************************************
1482 *
1483 * FUNCTION: acpi_os_validate_address
1484 *
1485 * PARAMETERS: space_id - ACPI space ID
1486 * address - Physical address
1487 * length - Address length
1488 *
1489 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1490 * should return AE_AML_ILLEGAL_ADDRESS.
1491 *
1492 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1493 * the addresses accessed by AML operation regions.
1494 *
1495 *****************************************************************************/
1496
1497acpi_status
1498acpi_os_validate_address (
1499 u8 space_id,
1500 acpi_physical_address address,
Thomas Renningerdf92e692008-02-04 23:31:22 -08001501 acpi_size length,
1502 char *name)
Bob Mooreb229cf92006-04-21 17:15:00 -04001503{
Thomas Renningerdf92e692008-02-04 23:31:22 -08001504 struct acpi_res_list *res;
Lin Minga5fe1a02009-08-13 10:43:27 +08001505 int added;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001506 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1507 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001508
Thomas Renningerdf92e692008-02-04 23:31:22 -08001509 switch (space_id) {
1510 case ACPI_ADR_SPACE_SYSTEM_IO:
1511 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001512 /* Only interference checks against SystemIO and SystemMemory
Thomas Renningerdf92e692008-02-04 23:31:22 -08001513 are needed */
1514 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1515 if (!res)
1516 return AE_OK;
1517 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1518 strlcpy(res->name, name, 5);
1519 res->start = address;
1520 res->end = address + length - 1;
1521 res->resource_type = space_id;
1522 spin_lock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001523 added = acpi_res_list_add(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001524 spin_unlock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001525 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1526 "name: %s\n", added ? "Added" : "Already exist",
1527 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001528 ? "SystemIO" : "System Memory",
1529 (unsigned long long)res->start,
1530 (unsigned long long)res->end,
1531 res->name);
Lin Minga5fe1a02009-08-13 10:43:27 +08001532 if (!added)
1533 kfree(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001534 break;
1535 case ACPI_ADR_SPACE_PCI_CONFIG:
1536 case ACPI_ADR_SPACE_EC:
1537 case ACPI_ADR_SPACE_SMBUS:
1538 case ACPI_ADR_SPACE_CMOS:
1539 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1540 case ACPI_ADR_SPACE_DATA_TABLE:
1541 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1542 break;
1543 }
1544 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001545}
Robert Moore73459f72005-06-24 00:00:00 -04001546#endif
Myron Stowed362eda2010-10-21 14:24:04 -06001547
1548acpi_status __init acpi_os_initialize(void)
1549{
1550 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1551 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1552 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1553 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1554
1555 return AE_OK;
1556}
1557
1558acpi_status acpi_os_initialize1(void)
1559{
1560 kacpid_wq = create_workqueue("kacpid");
1561 kacpi_notify_wq = create_workqueue("kacpi_notify");
1562 kacpi_hotplug_wq = create_workqueue("kacpi_hotplug");
1563 BUG_ON(!kacpid_wq);
1564 BUG_ON(!kacpi_notify_wq);
1565 BUG_ON(!kacpi_hotplug_wq);
Len Brown1bd64d42010-10-26 14:50:56 -04001566 acpi_install_interface_handler(acpi_osi_handler);
1567 acpi_osi_setup_late();
Myron Stowed362eda2010-10-21 14:24:04 -06001568 return AE_OK;
1569}
1570
1571acpi_status acpi_os_terminate(void)
1572{
1573 if (acpi_irq_handler) {
1574 acpi_os_remove_interrupt_handler(acpi_irq_irq,
1575 acpi_irq_handler);
1576 }
1577
1578 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1579 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1580 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1581 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1582
1583 destroy_workqueue(kacpid_wq);
1584 destroy_workqueue(kacpi_notify_wq);
1585 destroy_workqueue(kacpi_hotplug_wq);
1586
1587 return AE_OK;
1588}