blob: f7227e990b0f1694bcf00e13b6c09ba6bd263d44 [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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 * Deallocate the memory for a spinlock.
879 */
Bob Moore967440e32006-06-23 17:04:00 -0400880void acpi_os_delete_lock(acpi_spinlock handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Patrick Mocheld550d982006-06-27 00:41:40 -0400882 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400886acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Len Brown4be44fc2005-08-05 00:44:28 -0400888 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 sem = acpi_os_allocate(sizeof(struct semaphore));
891 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400892 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 memset(sem, 0, sizeof(struct semaphore));
894
895 sema_init(sem, initial_units);
896
Len Brown4be44fc2005-08-05 00:44:28 -0400897 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Len Brown4be44fc2005-08-05 00:44:28 -0400899 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
900 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Patrick Mocheld550d982006-06-27 00:41:40 -0400902 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905/*
906 * TODO: A better way to delete semaphores? Linux doesn't have a
907 * 'delete_semaphore()' function -- may result in an invalid
908 * pointer dereference for non-synchronized consumers. Should
909 * we at least check for blocked threads and signal/cancel them?
910 */
911
Len Brown4be44fc2005-08-05 00:44:28 -0400912acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Len Brown4be44fc2005-08-05 00:44:28 -0400914 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400917 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Len Brown4be44fc2005-08-05 00:44:28 -0400919 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400921 BUG_ON(!list_empty(&sem->wait_list));
Len Brown02438d82006-06-30 03:19:10 -0400922 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -0400923 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Patrick Mocheld550d982006-06-27 00:41:40 -0400925 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 * TODO: Support for units > 1?
930 */
Len Brown4be44fc2005-08-05 00:44:28 -0400931acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Len Brown4be44fc2005-08-05 00:44:28 -0400933 acpi_status status = AE_OK;
934 struct semaphore *sem = (struct semaphore *)handle;
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400935 long jiffies;
Len Brown4be44fc2005-08-05 00:44:28 -0400936 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400939 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400942 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Len Brown4be44fc2005-08-05 00:44:28 -0400944 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
945 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400947 if (timeout == ACPI_WAIT_FOREVER)
948 jiffies = MAX_SCHEDULE_TIMEOUT;
949 else
950 jiffies = msecs_to_jiffies(timeout);
951
952 ret = down_timeout(sem, jiffies);
953 if (ret)
954 status = AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -0400957 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400958 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -0400959 handle, units, timeout,
960 acpi_format_exception(status)));
961 } else {
962 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400963 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400964 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
Patrick Mocheld550d982006-06-27 00:41:40 -0400967 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970/*
971 * TODO: Support for units > 1?
972 */
Len Brown4be44fc2005-08-05 00:44:28 -0400973acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Len Brown4be44fc2005-08-05 00:44:28 -0400975 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400978 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400981 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Len Brown4be44fc2005-08-05 00:44:28 -0400983 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
984 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 up(sem);
987
Patrick Mocheld550d982006-06-27 00:41:40 -0400988 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
Len Brown4be44fc2005-08-05 00:44:28 -0400990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -0400992u32 acpi_os_get_line(char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994
995#ifdef ENABLE_DEBUGGER
996 if (acpi_in_debugger) {
997 u32 chars;
998
999 kdb_read(buffer, sizeof(line_buf));
1000
1001 /* remove the CR kdb includes */
1002 chars = strlen(buffer) - 1;
1003 buffer[chars] = '\0';
1004 }
1005#endif
1006
1007 return 0;
1008}
Len Brown4be44fc2005-08-05 00:44:28 -04001009#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Len Brown4be44fc2005-08-05 00:44:28 -04001011acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Len Brown4be44fc2005-08-05 00:44:28 -04001013 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 case ACPI_SIGNAL_FATAL:
1015 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1016 break;
1017 case ACPI_SIGNAL_BREAKPOINT:
1018 /*
1019 * AML Breakpoint
1020 * ACPI spec. says to treat it as a NOP unless
1021 * you are debugging. So if/when we integrate
1022 * AML debugger into the kernel debugger its
1023 * hook will go here. But until then it is
1024 * not useful to print anything on breakpoints.
1025 */
1026 break;
1027 default:
1028 break;
1029 }
1030
1031 return AE_OK;
1032}
Len Brown4be44fc2005-08-05 00:44:28 -04001033
Len Brown4be44fc2005-08-05 00:44:28 -04001034static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -04001037 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 if (!str || !*str)
1040 return 0;
1041
1042 for (; count-- && str && *str; str++) {
1043 if (isalnum(*str) || *str == ' ' || *str == ':')
1044 *p++ = *str;
1045 else if (*str == '\'' || *str == '"')
1046 continue;
1047 else
1048 break;
1049 }
1050 *p = 0;
1051
1052 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
1056__setup("acpi_os_name=", acpi_os_name_setup);
1057
Len Brownd4b7dc42008-01-23 20:50:56 -05001058static void __init set_osi_linux(unsigned int enable)
1059{
1060 if (osi_linux.enable != enable) {
1061 osi_linux.enable = enable;
1062 printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
1063 enable ? "Add": "Delet");
1064 }
Lin Mingb0ed7a92010-08-06 09:35:51 +08001065
1066 if (osi_linux.enable)
1067 acpi_osi_setup("Linux");
1068 else
1069 acpi_osi_setup("!Linux");
1070
Len Brownd4b7dc42008-01-23 20:50:56 -05001071 return;
1072}
Len Brownf5076542007-05-30 00:10:38 -04001073
Len Brownd4b7dc42008-01-23 20:50:56 -05001074static void __init acpi_cmdline_osi_linux(unsigned int enable)
1075{
1076 osi_linux.cmdline = 1; /* cmdline set the default */
1077 set_osi_linux(enable);
Len Brownf5076542007-05-30 00:10:38 -04001078
Len Brownd4b7dc42008-01-23 20:50:56 -05001079 return;
1080}
1081
1082void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1083{
1084 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
1085
1086 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1087
1088 if (enable == -1)
1089 return;
1090
1091 osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
1092
1093 set_osi_linux(enable);
1094
Len Brownf5076542007-05-30 00:10:38 -04001095 return;
1096}
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098/*
Len Brownae00d812007-05-29 18:43:33 -04001099 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1100 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -04001102 * string starting with '!' disables that string
1103 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
Lin Mingb0ed7a92010-08-06 09:35:51 +08001105static void __init acpi_osi_setup_late(void)
1106{
1107 char *str = osi_setup_string;
1108
1109 if (*str == '\0')
1110 return;
1111
1112 if (!strcmp("!Linux", str)) {
1113 acpi_cmdline_osi_linux(0); /* !enable */
1114 } else if (*str == '!') {
1115 if (acpi_remove_interface(++str) == AE_OK)
1116 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1117 } else if (!strcmp("Linux", str)) {
1118 acpi_cmdline_osi_linux(1); /* enable */
1119 } else {
1120 if (acpi_install_interface(str) == AE_OK)
1121 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1122 }
1123}
1124
Len Brown46c1fbd2008-02-13 23:13:25 -05001125int __init acpi_osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 if (str == NULL || *str == '\0') {
1128 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1129 acpi_gbl_create_osi_method = FALSE;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001130 } else {
1131 strncpy(osi_setup_string, str, OSI_STRING_LENGTH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133
1134 return 1;
1135}
1136
1137__setup("acpi_osi=", acpi_osi_setup);
1138
1139/* enable serialization to combat AE_ALREADY_EXISTS errors */
Len Brown4be44fc2005-08-05 00:44:28 -04001140static int __init acpi_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 printk(KERN_INFO PREFIX "serialize enabled\n");
1143
1144 acpi_gbl_all_methods_serialized = TRUE;
1145
1146 return 1;
1147}
1148
1149__setup("acpi_serialize", acpi_serialize_setup);
1150
Thomas Renningerdf92e692008-02-04 23:31:22 -08001151/* Check of resource interference between native drivers and ACPI
1152 * OperationRegions (SystemIO and System Memory only).
1153 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1154 * in arbitrary AML code and can interfere with legacy drivers.
1155 * acpi_enforce_resources= can be set to:
1156 *
Luca Tettamanti7e905602009-03-30 00:01:27 +02001157 * - strict (default) (2)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001158 * -> further driver trying to access the resources will not load
Luca Tettamanti7e905602009-03-30 00:01:27 +02001159 * - lax (1)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001160 * -> further driver trying to access the resources will load, but you
1161 * get a system message that something might go wrong...
1162 *
1163 * - no (0)
1164 * -> ACPI Operation Region resources will not be registered
1165 *
1166 */
1167#define ENFORCE_RESOURCES_STRICT 2
1168#define ENFORCE_RESOURCES_LAX 1
1169#define ENFORCE_RESOURCES_NO 0
1170
Luca Tettamanti7e905602009-03-30 00:01:27 +02001171static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001172
1173static int __init acpi_enforce_resources_setup(char *str)
1174{
1175 if (str == NULL || *str == '\0')
1176 return 0;
1177
1178 if (!strcmp("strict", str))
1179 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1180 else if (!strcmp("lax", str))
1181 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1182 else if (!strcmp("no", str))
1183 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1184
1185 return 1;
1186}
1187
1188__setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1189
1190/* Check for resource conflicts between ACPI OperationRegions and native
1191 * drivers */
Jean Delvare876fba42009-11-11 15:22:15 +01001192int acpi_check_resource_conflict(const struct resource *res)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001193{
1194 struct acpi_res_list *res_list_elem;
1195 int ioport;
1196 int clash = 0;
1197
1198 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1199 return 0;
1200 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1201 return 0;
1202
1203 ioport = res->flags & IORESOURCE_IO;
1204
1205 spin_lock(&acpi_res_lock);
1206 list_for_each_entry(res_list_elem, &resource_list_head,
1207 resource_list) {
1208 if (ioport && (res_list_elem->resource_type
1209 != ACPI_ADR_SPACE_SYSTEM_IO))
1210 continue;
1211 if (!ioport && (res_list_elem->resource_type
1212 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1213 continue;
1214
1215 if (res->end < res_list_elem->start
1216 || res_list_elem->end < res->start)
1217 continue;
1218 clash = 1;
1219 break;
1220 }
1221 spin_unlock(&acpi_res_lock);
1222
1223 if (clash) {
1224 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
Chase Douglas1638bca2010-03-22 15:08:09 -04001225 printk(KERN_WARNING "ACPI: resource %s %pR"
1226 " conflicts with ACPI region %s %pR\n",
1227 res->name, res, res_list_elem->name,
1228 res_list_elem);
Jean Delvare14f03342009-09-08 15:31:46 +02001229 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1230 printk(KERN_NOTICE "ACPI: This conflict may"
1231 " cause random problems and system"
1232 " instability\n");
1233 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1234 " for this device, you should use it instead of"
1235 " the native driver\n");
Thomas Renningerdf92e692008-02-04 23:31:22 -08001236 }
1237 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1238 return -EBUSY;
1239 }
1240 return 0;
1241}
Thomas Renninger443dea72008-02-04 23:31:23 -08001242EXPORT_SYMBOL(acpi_check_resource_conflict);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001243
1244int acpi_check_region(resource_size_t start, resource_size_t n,
1245 const char *name)
1246{
1247 struct resource res = {
1248 .start = start,
1249 .end = start + n - 1,
1250 .name = name,
1251 .flags = IORESOURCE_IO,
1252 };
1253
1254 return acpi_check_resource_conflict(&res);
1255}
1256EXPORT_SYMBOL(acpi_check_region);
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258/*
Jean Delvare70dd6be2010-05-27 19:58:37 +02001259 * Let drivers know whether the resource checks are effective
1260 */
1261int acpi_resources_are_enforced(void)
1262{
1263 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1264}
1265EXPORT_SYMBOL(acpi_resources_are_enforced);
1266
1267/*
Robert Moore73459f72005-06-24 00:00:00 -04001268 * Acquire a spinlock.
1269 *
1270 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001271 */
1272
Bob Moore967440e32006-06-23 17:04:00 -04001273acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001274{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001275 acpi_cpu_flags flags;
Bob Moore967440e32006-06-23 17:04:00 -04001276 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001277 return flags;
1278}
1279
1280/*
1281 * Release a spinlock. See above.
1282 */
1283
Bob Moore967440e32006-06-23 17:04:00 -04001284void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001285{
Bob Moore967440e32006-06-23 17:04:00 -04001286 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001287}
1288
Robert Moore73459f72005-06-24 00:00:00 -04001289#ifndef ACPI_USE_LOCAL_CACHE
1290
1291/*******************************************************************************
1292 *
1293 * FUNCTION: acpi_os_create_cache
1294 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001295 * PARAMETERS: name - Ascii name for the cache
1296 * size - Size of each cached object
1297 * depth - Maximum depth of the cache (in objects) <ignored>
1298 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001299 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001300 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001301 *
1302 * DESCRIPTION: Create a cache object
1303 *
1304 ******************************************************************************/
1305
1306acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001307acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001308{
Paul Mundt20c2df82007-07-20 10:11:58 +09001309 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001310 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001311 return AE_ERROR;
1312 else
1313 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001314}
1315
1316/*******************************************************************************
1317 *
1318 * FUNCTION: acpi_os_purge_cache
1319 *
1320 * PARAMETERS: Cache - Handle to cache object
1321 *
1322 * RETURN: Status
1323 *
1324 * DESCRIPTION: Free all objects within the requested cache.
1325 *
1326 ******************************************************************************/
1327
Len Brown4be44fc2005-08-05 00:44:28 -04001328acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001329{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001330 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001331 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001332}
1333
1334/*******************************************************************************
1335 *
1336 * FUNCTION: acpi_os_delete_cache
1337 *
1338 * PARAMETERS: Cache - Handle to cache object
1339 *
1340 * RETURN: Status
1341 *
1342 * DESCRIPTION: Free all objects within the requested cache and delete the
1343 * cache object.
1344 *
1345 ******************************************************************************/
1346
Len Brown4be44fc2005-08-05 00:44:28 -04001347acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001348{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001349 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001350 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001351}
1352
1353/*******************************************************************************
1354 *
1355 * FUNCTION: acpi_os_release_object
1356 *
1357 * PARAMETERS: Cache - Handle to cache object
1358 * Object - The object to be released
1359 *
1360 * RETURN: None
1361 *
1362 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1363 * the object is deleted.
1364 *
1365 ******************************************************************************/
1366
Len Brown4be44fc2005-08-05 00:44:28 -04001367acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001368{
Len Brown4be44fc2005-08-05 00:44:28 -04001369 kmem_cache_free(cache, object);
1370 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001371}
1372
Lin Minga5fe1a02009-08-13 10:43:27 +08001373static inline int acpi_res_list_add(struct acpi_res_list *res)
1374{
1375 struct acpi_res_list *res_list_elem;
1376
1377 list_for_each_entry(res_list_elem, &resource_list_head,
1378 resource_list) {
1379
1380 if (res->resource_type == res_list_elem->resource_type &&
1381 res->start == res_list_elem->start &&
1382 res->end == res_list_elem->end) {
1383
1384 /*
1385 * The Region(addr,len) already exist in the list,
1386 * just increase the count
1387 */
1388
1389 res_list_elem->count++;
1390 return 0;
1391 }
1392 }
1393
1394 res->count = 1;
1395 list_add(&res->resource_list, &resource_list_head);
1396 return 1;
1397}
1398
1399static inline void acpi_res_list_del(struct acpi_res_list *res)
1400{
1401 struct acpi_res_list *res_list_elem;
1402
1403 list_for_each_entry(res_list_elem, &resource_list_head,
1404 resource_list) {
1405
1406 if (res->resource_type == res_list_elem->resource_type &&
1407 res->start == res_list_elem->start &&
1408 res->end == res_list_elem->end) {
1409
1410 /*
1411 * If the res count is decreased to 0,
1412 * remove and free it
1413 */
1414
1415 if (--res_list_elem->count == 0) {
1416 list_del(&res_list_elem->resource_list);
1417 kfree(res_list_elem);
1418 }
1419 return;
1420 }
1421 }
1422}
1423
1424acpi_status
1425acpi_os_invalidate_address(
1426 u8 space_id,
1427 acpi_physical_address address,
1428 acpi_size length)
1429{
1430 struct acpi_res_list res;
1431
1432 switch (space_id) {
1433 case ACPI_ADR_SPACE_SYSTEM_IO:
1434 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001435 /* Only interference checks against SystemIO and SystemMemory
Lin Minga5fe1a02009-08-13 10:43:27 +08001436 are needed */
1437 res.start = address;
1438 res.end = address + length - 1;
1439 res.resource_type = space_id;
1440 spin_lock(&acpi_res_lock);
1441 acpi_res_list_del(&res);
1442 spin_unlock(&acpi_res_lock);
1443 break;
1444 case ACPI_ADR_SPACE_PCI_CONFIG:
1445 case ACPI_ADR_SPACE_EC:
1446 case ACPI_ADR_SPACE_SMBUS:
1447 case ACPI_ADR_SPACE_CMOS:
1448 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1449 case ACPI_ADR_SPACE_DATA_TABLE:
1450 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1451 break;
1452 }
1453 return AE_OK;
1454}
1455
Bob Mooreb229cf92006-04-21 17:15:00 -04001456/******************************************************************************
1457 *
1458 * FUNCTION: acpi_os_validate_address
1459 *
1460 * PARAMETERS: space_id - ACPI space ID
1461 * address - Physical address
1462 * length - Address length
1463 *
1464 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1465 * should return AE_AML_ILLEGAL_ADDRESS.
1466 *
1467 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1468 * the addresses accessed by AML operation regions.
1469 *
1470 *****************************************************************************/
1471
1472acpi_status
1473acpi_os_validate_address (
1474 u8 space_id,
1475 acpi_physical_address address,
Thomas Renningerdf92e692008-02-04 23:31:22 -08001476 acpi_size length,
1477 char *name)
Bob Mooreb229cf92006-04-21 17:15:00 -04001478{
Thomas Renningerdf92e692008-02-04 23:31:22 -08001479 struct acpi_res_list *res;
Lin Minga5fe1a02009-08-13 10:43:27 +08001480 int added;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001481 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1482 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001483
Thomas Renningerdf92e692008-02-04 23:31:22 -08001484 switch (space_id) {
1485 case ACPI_ADR_SPACE_SYSTEM_IO:
1486 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001487 /* Only interference checks against SystemIO and SystemMemory
Thomas Renningerdf92e692008-02-04 23:31:22 -08001488 are needed */
1489 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1490 if (!res)
1491 return AE_OK;
1492 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1493 strlcpy(res->name, name, 5);
1494 res->start = address;
1495 res->end = address + length - 1;
1496 res->resource_type = space_id;
1497 spin_lock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001498 added = acpi_res_list_add(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001499 spin_unlock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001500 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1501 "name: %s\n", added ? "Added" : "Already exist",
1502 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001503 ? "SystemIO" : "System Memory",
1504 (unsigned long long)res->start,
1505 (unsigned long long)res->end,
1506 res->name);
Lin Minga5fe1a02009-08-13 10:43:27 +08001507 if (!added)
1508 kfree(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001509 break;
1510 case ACPI_ADR_SPACE_PCI_CONFIG:
1511 case ACPI_ADR_SPACE_EC:
1512 case ACPI_ADR_SPACE_SMBUS:
1513 case ACPI_ADR_SPACE_CMOS:
1514 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1515 case ACPI_ADR_SPACE_DATA_TABLE:
1516 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1517 break;
1518 }
1519 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001520}
Robert Moore73459f72005-06-24 00:00:00 -04001521#endif
Myron Stowed362eda2010-10-21 14:24:04 -06001522
1523acpi_status __init acpi_os_initialize(void)
1524{
1525 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1526 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1527 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1528 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1529
1530 return AE_OK;
1531}
1532
Zhang Rui32d47ee2010-12-08 10:40:36 +08001533acpi_status __init acpi_os_initialize1(void)
Myron Stowed362eda2010-10-21 14:24:04 -06001534{
1535 kacpid_wq = create_workqueue("kacpid");
1536 kacpi_notify_wq = create_workqueue("kacpi_notify");
1537 kacpi_hotplug_wq = create_workqueue("kacpi_hotplug");
1538 BUG_ON(!kacpid_wq);
1539 BUG_ON(!kacpi_notify_wq);
1540 BUG_ON(!kacpi_hotplug_wq);
Len Brown1bd64d42010-10-26 14:50:56 -04001541 acpi_install_interface_handler(acpi_osi_handler);
1542 acpi_osi_setup_late();
Myron Stowed362eda2010-10-21 14:24:04 -06001543 return AE_OK;
1544}
1545
1546acpi_status acpi_os_terminate(void)
1547{
1548 if (acpi_irq_handler) {
1549 acpi_os_remove_interrupt_handler(acpi_irq_irq,
1550 acpi_irq_handler);
1551 }
1552
1553 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1554 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1555 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1556 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1557
1558 destroy_workqueue(kacpid_wq);
1559 destroy_workqueue(kacpi_notify_wq);
1560 destroy_workqueue(kacpi_hotplug_wq);
1561
1562 return AE_OK;
1563}