blob: c63d4cb37dabae4d1ce224065596866432c1c013 [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;
107};
108
109static LIST_HEAD(acpi_ioremaps);
110static DEFINE_SPINLOCK(acpi_ioremap_lock);
111
Len Brownae00d812007-05-29 18:43:33 -0400112#define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
113static char osi_additional_string[OSI_STRING_LENGTH_MAX];
114
Len Brownd4b7dc42008-01-23 20:50:56 -0500115/*
Len Browna6e08872008-11-08 01:21:10 -0500116 * The story of _OSI(Linux)
Len Brownd4b7dc42008-01-23 20:50:56 -0500117 *
Len Browna6e08872008-11-08 01:21:10 -0500118 * From pre-history through Linux-2.6.22,
119 * Linux responded TRUE upon a BIOS OSI(Linux) query.
Len Brownd4b7dc42008-01-23 20:50:56 -0500120 *
Len Browna6e08872008-11-08 01:21:10 -0500121 * Unfortunately, reference BIOS writers got wind of this
122 * and put OSI(Linux) in their example code, quickly exposing
123 * this string as ill-conceived and opening the door to
124 * an un-bounded number of BIOS incompatibilities.
Len Brownd4b7dc42008-01-23 20:50:56 -0500125 *
Len Browna6e08872008-11-08 01:21:10 -0500126 * For example, OSI(Linux) was used on resume to re-POST a
127 * video card on one system, because Linux at that time
128 * could not do a speedy restore in its native driver.
129 * But then upon gaining quick native restore capability,
130 * Linux has no way to tell the BIOS to skip the time-consuming
131 * POST -- putting Linux at a permanent performance disadvantage.
132 * On another system, the BIOS writer used OSI(Linux)
133 * to infer native OS support for IPMI! On other systems,
134 * OSI(Linux) simply got in the way of Linux claiming to
135 * be compatible with other operating systems, exposing
136 * BIOS issues such as skipped device initialization.
Len Brownd4b7dc42008-01-23 20:50:56 -0500137 *
Len Browna6e08872008-11-08 01:21:10 -0500138 * So "Linux" turned out to be a really poor chose of
139 * OSI string, and from Linux-2.6.23 onward we respond FALSE.
Len Brownd4b7dc42008-01-23 20:50:56 -0500140 *
141 * BIOS writers should NOT query _OSI(Linux) on future systems.
Len Browna6e08872008-11-08 01:21:10 -0500142 * Linux will complain on the console when it sees it, and return FALSE.
143 * To get Linux to return TRUE for your system will require
144 * a kernel source update to add a DMI entry,
145 * or boot with "acpi_osi=Linux"
Len Brownd4b7dc42008-01-23 20:50:56 -0500146 */
Len Brownd4b7dc42008-01-23 20:50:56 -0500147
Adrian Bunk1d15d842008-01-29 00:10:15 +0200148static struct osi_linux {
Len Brownd4b7dc42008-01-23 20:50:56 -0500149 unsigned int enable:1;
150 unsigned int dmi:1;
151 unsigned int cmdline:1;
152 unsigned int known:1;
Len Browna6e08872008-11-08 01:21:10 -0500153} osi_linux = { 0, 0, 0, 0};
Len Brownf5076542007-05-30 00:10:38 -0400154
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700155static void __init acpi_request_region (struct acpi_generic_address *addr,
156 unsigned int length, char *desc)
157{
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700158 if (!addr->address || !length)
159 return;
160
Andi Kleencfa806f2010-07-20 15:18:36 -0700161 /* Resources are never freed */
Len Browneee3c852007-02-03 01:38:16 -0500162 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Andi Kleencfa806f2010-07-20 15:18:36 -0700163 request_region(addr->address, length, desc);
Len Browneee3c852007-02-03 01:38:16 -0500164 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
Andi Kleencfa806f2010-07-20 15:18:36 -0700165 request_mem_region(addr->address, length, desc);
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700166}
167
168static int __init acpi_reserve_resources(void)
169{
Len Browneee3c852007-02-03 01:38:16 -0500170 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700171 "ACPI PM1a_EVT_BLK");
172
Len Browneee3c852007-02-03 01:38:16 -0500173 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700174 "ACPI PM1b_EVT_BLK");
175
Len Browneee3c852007-02-03 01:38:16 -0500176 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700177 "ACPI PM1a_CNT_BLK");
178
Len Browneee3c852007-02-03 01:38:16 -0500179 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700180 "ACPI PM1b_CNT_BLK");
181
Len Browneee3c852007-02-03 01:38:16 -0500182 if (acpi_gbl_FADT.pm_timer_length == 4)
183 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700184
Len Browneee3c852007-02-03 01:38:16 -0500185 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700186 "ACPI PM2_CNT_BLK");
187
188 /* Length of GPE blocks must be a non-negative multiple of 2 */
189
Len Browneee3c852007-02-03 01:38:16 -0500190 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
191 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
192 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700193
Len Browneee3c852007-02-03 01:38:16 -0500194 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
195 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
196 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700197
198 return 0;
199}
200device_initcall(acpi_reserve_resources);
201
Len Brown4be44fc2005-08-05 00:44:28 -0400202void acpi_os_printf(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 va_list args;
205 va_start(args, fmt);
206 acpi_os_vprintf(fmt, args);
207 va_end(args);
208}
Len Brown4be44fc2005-08-05 00:44:28 -0400209
Len Brown4be44fc2005-08-05 00:44:28 -0400210void acpi_os_vprintf(const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 static char buffer[512];
Len Brown4be44fc2005-08-05 00:44:28 -0400213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 vsprintf(buffer, fmt, args);
215
216#ifdef ENABLE_DEBUGGER
217 if (acpi_in_debugger) {
218 kdb_printf("%s", buffer);
219 } else {
Frank Seidel4d939152009-02-04 17:03:07 +0100220 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222#else
Frank Seidel4d939152009-02-04 17:03:07 +0100223 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224#endif
225}
226
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300227acpi_physical_address __init acpi_os_get_root_pointer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 if (efi_enabled) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800230 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300231 return efi.acpi20;
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800232 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300233 return efi.acpi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400235 printk(KERN_ERR PREFIX
236 "System description tables not found\n");
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Len Brown239665a2007-11-23 20:08:02 -0500239 } else {
240 acpi_physical_address pa = 0;
241
242 acpi_find_root_pointer(&pa);
243 return pa;
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Myron Stowe620242a2010-10-21 14:23:53 -0600247/* Must be called with 'acpi_ioremap_lock' lock held. */
248static void __iomem *
249acpi_map_vaddr_lookup(acpi_physical_address phys, acpi_size size)
250{
251 struct acpi_ioremap *map;
252
253 list_for_each_entry(map, &acpi_ioremaps, list)
254 if (map->phys <= phys &&
255 phys + size <= map->phys + map->size)
256 return map->virt + (phys - map->phys);
257
258 return NULL;
259}
260
261/* Must be called with 'acpi_ioremap_lock' lock held. */
262static struct acpi_ioremap *
263acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
264{
265 struct acpi_ioremap *map;
266
267 list_for_each_entry(map, &acpi_ioremaps, list)
268 if (map->virt == virt && map->size == size)
269 return map;
270
271 return NULL;
272}
273
Jan Beulich2fdf0742007-12-13 08:33:59 +0000274void __iomem *__init_refok
275acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Myron Stowe620242a2010-10-21 14:23:53 -0600277 struct acpi_ioremap *map;
278 unsigned long flags;
279 void __iomem *virt;
280
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800281 if (phys > ULONG_MAX) {
282 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
Randy Dunlap70c08462007-02-13 16:11:36 -0800283 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Myron Stowe620242a2010-10-21 14:23:53 -0600285
286 if (!acpi_gbl_permanent_mmap)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300287 return __acpi_map_table((unsigned long)phys, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600288
289 map = kzalloc(sizeof(*map), GFP_KERNEL);
290 if (!map)
291 return NULL;
292
293 virt = ioremap(phys, size);
294 if (!virt) {
295 kfree(map);
296 return NULL;
297 }
298
299 INIT_LIST_HEAD(&map->list);
300 map->virt = virt;
301 map->phys = phys;
302 map->size = size;
303
304 spin_lock_irqsave(&acpi_ioremap_lock, flags);
305 list_add_tail(&map->list, &acpi_ioremaps);
306 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
307
308 return virt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800310EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800312void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Myron Stowe620242a2010-10-21 14:23:53 -0600314 struct acpi_ioremap *map;
315 unsigned long flags;
316
317 if (!acpi_gbl_permanent_mmap) {
Yinghai Lu7d972772009-02-07 15:39:41 -0800318 __acpi_unmap_table(virt, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600319 return;
320 }
321
322 spin_lock_irqsave(&acpi_ioremap_lock, flags);
323 map = acpi_map_lookup_virt(virt, size);
324 if (!map) {
325 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
326 printk(KERN_ERR PREFIX "%s: bad address %p\n", __func__, virt);
327 dump_stack();
328 return;
329 }
330
331 list_del(&map->list);
332 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
333
334 iounmap(map->virt);
335 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800337EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800339void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Yinghai Lu7d972772009-02-07 15:39:41 -0800340{
341 if (!acpi_gbl_permanent_mmap)
342 __acpi_unmap_table(virt, size);
343}
344
Myron Stowe29718522010-10-21 14:23:59 -0600345int acpi_os_map_generic_address(struct acpi_generic_address *addr)
346{
347 void __iomem *virt;
348
349 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
350 return 0;
351
352 if (!addr->address || !addr->bit_width)
353 return -EINVAL;
354
355 virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
356 if (!virt)
357 return -EIO;
358
359 return 0;
360}
361EXPORT_SYMBOL_GPL(acpi_os_map_generic_address);
362
363void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
364{
365 void __iomem *virt;
366 unsigned long flags;
367 acpi_size size = addr->bit_width / 8;
368
369 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
370 return;
371
372 if (!addr->address || !addr->bit_width)
373 return;
374
375 spin_lock_irqsave(&acpi_ioremap_lock, flags);
376 virt = acpi_map_vaddr_lookup(addr->address, size);
377 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
378
379 acpi_os_unmap_memory(virt, size);
380}
381EXPORT_SYMBOL_GPL(acpi_os_unmap_generic_address);
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#ifdef ACPI_FUTURE_USAGE
384acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400385acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Len Brown4be44fc2005-08-05 00:44:28 -0400387 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return AE_BAD_PARAMETER;
389
390 *phys = virt_to_phys(virt);
391
392 return AE_OK;
393}
394#endif
395
396#define ACPI_MAX_OVERRIDE_LEN 100
397
398static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
399
400acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400401acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
402 acpi_string * new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 if (!init_val || !new_val)
405 return AE_BAD_PARAMETER;
406
407 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400408 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400410 acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *new_val = acpi_os_name;
412 }
413
414 return AE_OK;
415}
416
417acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400418acpi_os_table_override(struct acpi_table_header * existing_table,
419 struct acpi_table_header ** new_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 if (!existing_table || !new_table)
422 return AE_BAD_PARAMETER;
423
Markus Gaugusch71fc47a2008-02-05 00:04:06 +0100424 *new_table = NULL;
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426#ifdef CONFIG_ACPI_CUSTOM_DSDT
427 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400428 *new_table = (struct acpi_table_header *)AmlCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429#endif
Éric Piel6ed31e92008-02-05 00:04:50 +0100430 if (*new_table != NULL) {
431 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
432 "this is unsafe: tainting kernel\n",
433 existing_table->signature,
434 existing_table->oem_table_id);
435 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return AE_OK;
438}
439
David Howells7d12e782006-10-05 14:55:46 +0100440static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Len Brown5229e872008-02-06 01:26:55 -0500442 u32 handled;
443
444 handled = (*acpi_irq_handler) (acpi_irq_context);
445
446 if (handled) {
447 acpi_irq_handled++;
448 return IRQ_HANDLED;
Len Brown88bea182009-04-21 00:35:47 -0400449 } else {
450 acpi_irq_not_handled++;
Len Brown5229e872008-02-06 01:26:55 -0500451 return IRQ_NONE;
Len Brown88bea182009-04-21 00:35:47 -0400452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400456acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
457 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 unsigned int irq;
460
Len Brown5229e872008-02-06 01:26:55 -0500461 acpi_irq_stats_init();
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /*
464 * Ignore the GSI from the core, and use the value in our copy of the
465 * FADT. It may not be the same if an interrupt source override exists
466 * for the SCI.
467 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300468 gsi = acpi_gbl_FADT.sci_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
470 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
471 gsi);
472 return AE_OK;
473 }
474
475 acpi_irq_handler = handler;
476 acpi_irq_context = context;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700477 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
479 return AE_NOT_ACQUIRED;
480 }
481 acpi_irq_irq = irq;
482
483 return AE_OK;
484}
485
Len Brown4be44fc2005-08-05 00:44:28 -0400486acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 if (irq) {
489 free_irq(irq, acpi_irq);
490 acpi_irq_handler = NULL;
491 acpi_irq_irq = 0;
492 }
493
494 return AE_OK;
495}
496
497/*
498 * Running in interpreter thread context, safe to sleep
499 */
500
Lin Ming439913f2010-01-28 10:53:19 +0800501void acpi_os_sleep(u64 ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800503 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
Len Brown4be44fc2005-08-05 00:44:28 -0400505
Len Brown4be44fc2005-08-05 00:44:28 -0400506void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 while (us) {
509 u32 delay = 1000;
510
511 if (delay > us)
512 delay = us;
513 udelay(delay);
514 touch_nmi_watchdog();
515 us -= delay;
516 }
517}
Len Brown4be44fc2005-08-05 00:44:28 -0400518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/*
520 * Support ACPI 3.0 AML Timer operand
521 * Returns 64-bit free-running, monotonically increasing timer
522 * with 100ns granularity
523 */
Len Brown4be44fc2005-08-05 00:44:28 -0400524u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 static u64 t;
527
528#ifdef CONFIG_HPET
529 /* TBD: use HPET if available */
530#endif
531
532#ifdef CONFIG_X86_PM_TIMER
533 /* TBD: default to PM timer if HPET was not available */
534#endif
535 if (!t)
536 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
537
538 return ++t;
539}
540
Len Brown4be44fc2005-08-05 00:44:28 -0400541acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 u32 dummy;
544
545 if (!value)
546 value = &dummy;
547
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800548 *value = 0;
549 if (width <= 8) {
Len Brown4be44fc2005-08-05 00:44:28 -0400550 *(u8 *) value = inb(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800551 } else if (width <= 16) {
Len Brown4be44fc2005-08-05 00:44:28 -0400552 *(u16 *) value = inw(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800553 } else if (width <= 32) {
Len Brown4be44fc2005-08-05 00:44:28 -0400554 *(u32 *) value = inl(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800555 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 BUG();
557 }
558
559 return AE_OK;
560}
Len Brown4be44fc2005-08-05 00:44:28 -0400561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562EXPORT_SYMBOL(acpi_os_read_port);
563
Len Brown4be44fc2005-08-05 00:44:28 -0400564acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800566 if (width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 outb(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800568 } else if (width <= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 outw(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800570 } else if (width <= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 outl(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800572 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 BUG();
574 }
575
576 return AE_OK;
577}
Len Brown4be44fc2005-08-05 00:44:28 -0400578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579EXPORT_SYMBOL(acpi_os_write_port);
580
581acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400582acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Len Brown4be44fc2005-08-05 00:44:28 -0400584 u32 dummy;
585 void __iomem *virt_addr;
Myron Stowe620242a2010-10-21 14:23:53 -0600586 int size = width / 8, unmap = 0;
587 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Myron Stowe620242a2010-10-21 14:23:53 -0600589 spin_lock_irqsave(&acpi_ioremap_lock, flags);
590 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
591 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
592 if (!virt_addr) {
593 virt_addr = ioremap(phys_addr, size);
594 unmap = 1;
595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (!value)
597 value = &dummy;
598
599 switch (width) {
600 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400601 *(u8 *) value = readb(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 break;
603 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400604 *(u16 *) value = readw(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 break;
606 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400607 *(u32 *) value = readl(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 break;
609 default:
610 BUG();
611 }
612
Myron Stowe620242a2010-10-21 14:23:53 -0600613 if (unmap)
614 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 return AE_OK;
617}
618
619acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400620acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Len Brown4be44fc2005-08-05 00:44:28 -0400622 void __iomem *virt_addr;
Myron Stowe620242a2010-10-21 14:23:53 -0600623 int size = width / 8, unmap = 0;
624 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Myron Stowe620242a2010-10-21 14:23:53 -0600626 spin_lock_irqsave(&acpi_ioremap_lock, flags);
627 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
628 spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
629 if (!virt_addr) {
630 virt_addr = ioremap(phys_addr, size);
631 unmap = 1;
632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 switch (width) {
635 case 8:
636 writeb(value, virt_addr);
637 break;
638 case 16:
639 writew(value, virt_addr);
640 break;
641 case 32:
642 writel(value, virt_addr);
643 break;
644 default:
645 BUG();
646 }
647
Myron Stowe620242a2010-10-21 14:23:53 -0600648 if (unmap)
649 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 return AE_OK;
652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400655acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Linus Torvalds10270d42008-02-13 09:56:14 -0800656 u32 *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 int result, size;
659
660 if (!value)
661 return AE_BAD_PARAMETER;
662
663 switch (width) {
664 case 8:
665 size = 1;
666 break;
667 case 16:
668 size = 2;
669 break;
670 case 32:
671 size = 4;
672 break;
673 default:
674 return AE_ERROR;
675 }
676
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500677 result = raw_pci_read(pci_id->segment, pci_id->bus,
678 PCI_DEVFN(pci_id->device, pci_id->function),
679 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 return (result ? AE_ERROR : AE_OK);
682}
Len Brown4be44fc2005-08-05 00:44:28 -0400683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400685acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Lin Ming439913f2010-01-28 10:53:19 +0800686 u64 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 int result, size;
689
690 switch (width) {
691 case 8:
692 size = 1;
693 break;
694 case 16:
695 size = 2;
696 break;
697 case 32:
698 size = 4;
699 break;
700 default:
701 return AE_ERROR;
702 }
703
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500704 result = raw_pci_write(pci_id->segment, pci_id->bus,
705 PCI_DEVFN(pci_id->device, pci_id->function),
706 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 return (result ? AE_ERROR : AE_OK);
709}
710
711/* TODO: Change code to take advantage of driver model more */
Len Brown4be44fc2005-08-05 00:44:28 -0400712static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
713 acpi_handle chandle, /* current node */
714 struct acpi_pci_id **id,
715 int *is_bridge, u8 * bus_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Len Brown4be44fc2005-08-05 00:44:28 -0400717 acpi_handle handle;
718 struct acpi_pci_id *pci_id = *id;
719 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400720 unsigned long long temp;
Len Brown4be44fc2005-08-05 00:44:28 -0400721 acpi_object_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 acpi_get_parent(chandle, &handle);
724 if (handle != rhandle) {
Len Brown4be44fc2005-08-05 00:44:28 -0400725 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
726 bus_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 status = acpi_get_type(handle, &type);
Len Brown4be44fc2005-08-05 00:44:28 -0400729 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return;
731
Matthew Wilcox27663c52008-10-10 02:22:59 -0400732 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400733 &temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (ACPI_SUCCESS(status)) {
Linus Torvalds10270d42008-02-13 09:56:14 -0800735 u32 val;
Len Brown4be44fc2005-08-05 00:44:28 -0400736 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
737 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 if (*is_bridge)
740 pci_id->bus = *bus_number;
741
742 /* any nicer way to get bus number of bridge ? */
Len Brown4be44fc2005-08-05 00:44:28 -0400743 status =
Linus Torvalds10270d42008-02-13 09:56:14 -0800744 acpi_os_read_pci_configuration(pci_id, 0x0e, &val,
Len Brown4be44fc2005-08-05 00:44:28 -0400745 8);
746 if (ACPI_SUCCESS(status)
Linus Torvalds10270d42008-02-13 09:56:14 -0800747 && ((val & 0x7f) == 1 || (val & 0x7f) == 2)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400748 status =
749 acpi_os_read_pci_configuration(pci_id, 0x18,
Linus Torvalds10270d42008-02-13 09:56:14 -0800750 &val, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!ACPI_SUCCESS(status)) {
752 /* Certainly broken... FIX ME */
753 return;
754 }
755 *is_bridge = 1;
Linus Torvalds10270d42008-02-13 09:56:14 -0800756 pci_id->bus = val;
Len Brown4be44fc2005-08-05 00:44:28 -0400757 status =
758 acpi_os_read_pci_configuration(pci_id, 0x19,
Linus Torvalds10270d42008-02-13 09:56:14 -0800759 &val, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (ACPI_SUCCESS(status)) {
Linus Torvalds10270d42008-02-13 09:56:14 -0800761 *bus_number = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763 } else
764 *is_bridge = 0;
765 }
766 }
767}
768
Len Brown4be44fc2005-08-05 00:44:28 -0400769void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
770 acpi_handle chandle, /* current node */
771 struct acpi_pci_id **id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 int is_bridge = 1;
774 u8 bus_number = (*id)->bus;
775
776 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
777}
778
David Howells65f27f32006-11-22 14:55:48 +0000779static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
David Howells65f27f32006-11-22 14:55:48 +0000781 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400782
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000783 if (dpc->wait)
784 acpi_os_wait_events_complete(NULL);
Zhang Rui19cd8472008-08-28 10:05:06 +0800785
786 dpc->function(dpc->context);
787 kfree(dpc);
Zhang Rui19cd8472008-08-28 10:05:06 +0800788}
789
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400790/*******************************************************************************
791 *
792 * FUNCTION: acpi_os_execute
793 *
794 * PARAMETERS: Type - Type of the callback
795 * Function - Function to be executed
796 * Context - Function parameters
797 *
798 * RETURN: Status
799 *
800 * DESCRIPTION: Depending on type, either queues function for deferred execution or
801 * immediately executes function on a separate thread.
802 *
803 ******************************************************************************/
804
Zhang Rui19cd8472008-08-28 10:05:06 +0800805static acpi_status __acpi_os_execute(acpi_execute_type type,
806 acpi_osd_exec_callback function, void *context, int hp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Len Brown4be44fc2005-08-05 00:44:28 -0400808 acpi_status status = AE_OK;
809 struct acpi_os_dpc *dpc;
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300810 struct workqueue_struct *queue;
Zhang Rui19cd8472008-08-28 10:05:06 +0800811 int ret;
Len Brown72945b22006-07-12 22:46:42 -0400812 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
813 "Scheduling function [%p(%p)] for deferred execution.\n",
814 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /*
817 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +0000818 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 * way that allows us to also free its memory inside the callee.
820 * Because we may want to schedule several tasks with different
821 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +0000822 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 */
Len Brown72945b22006-07-12 22:46:42 -0400824
David Howells65f27f32006-11-22 14:55:48 +0000825 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (!dpc)
Lin Ming889c78b2008-12-31 09:23:57 +0800827 return AE_NO_MEMORY;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 dpc->function = function;
830 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800831
Zhang Ruic02256b2009-06-23 10:20:29 +0800832 /*
833 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
834 * because the hotplug code may call driver .remove() functions,
835 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
836 * to flush these workqueues.
837 */
838 queue = hp ? kacpi_hotplug_wq :
839 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
Bjorn Helgaas9ac61852009-08-31 22:32:10 +0000840 dpc->wait = hp ? 1 : 0;
Zhang Ruibc736752010-03-22 15:48:54 +0800841
842 if (queue == kacpi_hotplug_wq)
843 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
844 else if (queue == kacpi_notify_wq)
845 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
846 else
847 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
848
Tejun Heo8fec62b2010-06-29 10:07:09 +0200849 /*
850 * On some machines, a software-initiated SMI causes corruption unless
851 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
852 * typically it's done in GPE-related methods that are run via
853 * workqueues, so we can avoid the known corruption cases by always
854 * queueing on CPU 0.
855 */
856 ret = queue_work_on(0, queue, &dpc->work);
Zhang Rui19cd8472008-08-28 10:05:06 +0800857
858 if (!ret) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800859 printk(KERN_ERR PREFIX
860 "Call to queue_work() failed.\n");
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300861 status = AE_ERROR;
862 kfree(dpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Lin Ming889c78b2008-12-31 09:23:57 +0800864 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
Len Brown4be44fc2005-08-05 00:44:28 -0400866
Zhang Rui19cd8472008-08-28 10:05:06 +0800867acpi_status acpi_os_execute(acpi_execute_type type,
868 acpi_osd_exec_callback function, void *context)
869{
870 return __acpi_os_execute(type, function, context, 0);
871}
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400872EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Zhang Rui19cd8472008-08-28 10:05:06 +0800874acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
875 void *context)
876{
877 return __acpi_os_execute(0, function, context, 1);
878}
879
Len Brown4be44fc2005-08-05 00:44:28 -0400880void acpi_os_wait_events_complete(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 flush_workqueue(kacpid_wq);
Zhang Rui2f67a062008-04-29 02:34:42 -0400883 flush_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
Len Brown4be44fc2005-08-05 00:44:28 -0400885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886EXPORT_SYMBOL(acpi_os_wait_events_complete);
887
888/*
889 * Allocate the memory for a spinlock and initialize it.
890 */
Bob Moore967440e32006-06-23 17:04:00 -0400891acpi_status acpi_os_create_lock(acpi_spinlock * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Bob Moore967440e32006-06-23 17:04:00 -0400893 spin_lock_init(*handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Patrick Mocheld550d982006-06-27 00:41:40 -0400895 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898/*
899 * Deallocate the memory for a spinlock.
900 */
Bob Moore967440e32006-06-23 17:04:00 -0400901void acpi_os_delete_lock(acpi_spinlock handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Patrick Mocheld550d982006-06-27 00:41:40 -0400903 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400907acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Len Brown4be44fc2005-08-05 00:44:28 -0400909 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 sem = acpi_os_allocate(sizeof(struct semaphore));
912 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400913 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 memset(sem, 0, sizeof(struct semaphore));
915
916 sema_init(sem, initial_units);
917
Len Brown4be44fc2005-08-05 00:44:28 -0400918 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Len Brown4be44fc2005-08-05 00:44:28 -0400920 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
921 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Patrick Mocheld550d982006-06-27 00:41:40 -0400923 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926/*
927 * TODO: A better way to delete semaphores? Linux doesn't have a
928 * 'delete_semaphore()' function -- may result in an invalid
929 * pointer dereference for non-synchronized consumers. Should
930 * we at least check for blocked threads and signal/cancel them?
931 */
932
Len Brown4be44fc2005-08-05 00:44:28 -0400933acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Len Brown4be44fc2005-08-05 00:44:28 -0400935 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400938 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Len Brown4be44fc2005-08-05 00:44:28 -0400940 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400942 BUG_ON(!list_empty(&sem->wait_list));
Len Brown02438d82006-06-30 03:19:10 -0400943 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -0400944 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Patrick Mocheld550d982006-06-27 00:41:40 -0400946 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * TODO: Support for units > 1?
951 */
Len Brown4be44fc2005-08-05 00:44:28 -0400952acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Len Brown4be44fc2005-08-05 00:44:28 -0400954 acpi_status status = AE_OK;
955 struct semaphore *sem = (struct semaphore *)handle;
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400956 long jiffies;
Len Brown4be44fc2005-08-05 00:44:28 -0400957 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400960 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400963 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Len Brown4be44fc2005-08-05 00:44:28 -0400965 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
966 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Matthew Wilcoxf1241c82008-03-14 13:43:13 -0400968 if (timeout == ACPI_WAIT_FOREVER)
969 jiffies = MAX_SCHEDULE_TIMEOUT;
970 else
971 jiffies = msecs_to_jiffies(timeout);
972
973 ret = down_timeout(sem, jiffies);
974 if (ret)
975 status = AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -0400978 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400979 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -0400980 handle, units, timeout,
981 acpi_format_exception(status)));
982 } else {
983 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400984 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400985 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987
Patrick Mocheld550d982006-06-27 00:41:40 -0400988 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991/*
992 * TODO: Support for units > 1?
993 */
Len Brown4be44fc2005-08-05 00:44:28 -0400994acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Len Brown4be44fc2005-08-05 00:44:28 -0400996 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400999 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -04001002 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Len Brown4be44fc2005-08-05 00:44:28 -04001004 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1005 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 up(sem);
1008
Patrick Mocheld550d982006-06-27 00:41:40 -04001009 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
Len Brown4be44fc2005-08-05 00:44:28 -04001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -04001013u32 acpi_os_get_line(char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015
1016#ifdef ENABLE_DEBUGGER
1017 if (acpi_in_debugger) {
1018 u32 chars;
1019
1020 kdb_read(buffer, sizeof(line_buf));
1021
1022 /* remove the CR kdb includes */
1023 chars = strlen(buffer) - 1;
1024 buffer[chars] = '\0';
1025 }
1026#endif
1027
1028 return 0;
1029}
Len Brown4be44fc2005-08-05 00:44:28 -04001030#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Len Brown4be44fc2005-08-05 00:44:28 -04001032acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Len Brown4be44fc2005-08-05 00:44:28 -04001034 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 case ACPI_SIGNAL_FATAL:
1036 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1037 break;
1038 case ACPI_SIGNAL_BREAKPOINT:
1039 /*
1040 * AML Breakpoint
1041 * ACPI spec. says to treat it as a NOP unless
1042 * you are debugging. So if/when we integrate
1043 * AML debugger into the kernel debugger its
1044 * hook will go here. But until then it is
1045 * not useful to print anything on breakpoints.
1046 */
1047 break;
1048 default:
1049 break;
1050 }
1051
1052 return AE_OK;
1053}
Len Brown4be44fc2005-08-05 00:44:28 -04001054
Len Brown4be44fc2005-08-05 00:44:28 -04001055static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -04001058 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 if (!str || !*str)
1061 return 0;
1062
1063 for (; count-- && str && *str; str++) {
1064 if (isalnum(*str) || *str == ' ' || *str == ':')
1065 *p++ = *str;
1066 else if (*str == '\'' || *str == '"')
1067 continue;
1068 else
1069 break;
1070 }
1071 *p = 0;
1072
1073 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
1077__setup("acpi_os_name=", acpi_os_name_setup);
1078
Len Brownd4b7dc42008-01-23 20:50:56 -05001079static void __init set_osi_linux(unsigned int enable)
1080{
1081 if (osi_linux.enable != enable) {
1082 osi_linux.enable = enable;
1083 printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
1084 enable ? "Add": "Delet");
1085 }
1086 return;
1087}
Len Brownf5076542007-05-30 00:10:38 -04001088
Len Brownd4b7dc42008-01-23 20:50:56 -05001089static void __init acpi_cmdline_osi_linux(unsigned int enable)
1090{
1091 osi_linux.cmdline = 1; /* cmdline set the default */
1092 set_osi_linux(enable);
Len Brownf5076542007-05-30 00:10:38 -04001093
Len Brownd4b7dc42008-01-23 20:50:56 -05001094 return;
1095}
1096
1097void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1098{
1099 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
1100
1101 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1102
1103 if (enable == -1)
1104 return;
1105
1106 osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
1107
1108 set_osi_linux(enable);
1109
Len Brownf5076542007-05-30 00:10:38 -04001110 return;
1111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113/*
Len Brownae00d812007-05-29 18:43:33 -04001114 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1115 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -04001117 * string starting with '!' disables that string
1118 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 */
Len Brown46c1fbd2008-02-13 23:13:25 -05001120int __init acpi_osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 if (str == NULL || *str == '\0') {
1123 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1124 acpi_gbl_create_osi_method = FALSE;
Len Brownaa2e09d2007-07-02 23:57:45 -04001125 } else if (!strcmp("!Linux", str)) {
Len Brownd4b7dc42008-01-23 20:50:56 -05001126 acpi_cmdline_osi_linux(0); /* !enable */
Len Brownae00d812007-05-29 18:43:33 -04001127 } else if (*str == '!') {
1128 if (acpi_osi_invalidate(++str) == AE_OK)
1129 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
Len Brownf5076542007-05-30 00:10:38 -04001130 } else if (!strcmp("Linux", str)) {
Len Brownd4b7dc42008-01-23 20:50:56 -05001131 acpi_cmdline_osi_linux(1); /* enable */
Len Brownae00d812007-05-29 18:43:33 -04001132 } else if (*osi_additional_string == '\0') {
1133 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
1134 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136
1137 return 1;
1138}
1139
1140__setup("acpi_osi=", acpi_osi_setup);
1141
1142/* enable serialization to combat AE_ALREADY_EXISTS errors */
Len Brown4be44fc2005-08-05 00:44:28 -04001143static int __init acpi_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
1145 printk(KERN_INFO PREFIX "serialize enabled\n");
1146
1147 acpi_gbl_all_methods_serialized = TRUE;
1148
1149 return 1;
1150}
1151
1152__setup("acpi_serialize", acpi_serialize_setup);
1153
Thomas Renningerdf92e692008-02-04 23:31:22 -08001154/* Check of resource interference between native drivers and ACPI
1155 * OperationRegions (SystemIO and System Memory only).
1156 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1157 * in arbitrary AML code and can interfere with legacy drivers.
1158 * acpi_enforce_resources= can be set to:
1159 *
Luca Tettamanti7e905602009-03-30 00:01:27 +02001160 * - strict (default) (2)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001161 * -> further driver trying to access the resources will not load
Luca Tettamanti7e905602009-03-30 00:01:27 +02001162 * - lax (1)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001163 * -> further driver trying to access the resources will load, but you
1164 * get a system message that something might go wrong...
1165 *
1166 * - no (0)
1167 * -> ACPI Operation Region resources will not be registered
1168 *
1169 */
1170#define ENFORCE_RESOURCES_STRICT 2
1171#define ENFORCE_RESOURCES_LAX 1
1172#define ENFORCE_RESOURCES_NO 0
1173
Luca Tettamanti7e905602009-03-30 00:01:27 +02001174static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001175
1176static int __init acpi_enforce_resources_setup(char *str)
1177{
1178 if (str == NULL || *str == '\0')
1179 return 0;
1180
1181 if (!strcmp("strict", str))
1182 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1183 else if (!strcmp("lax", str))
1184 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1185 else if (!strcmp("no", str))
1186 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1187
1188 return 1;
1189}
1190
1191__setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1192
1193/* Check for resource conflicts between ACPI OperationRegions and native
1194 * drivers */
Jean Delvare876fba42009-11-11 15:22:15 +01001195int acpi_check_resource_conflict(const struct resource *res)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001196{
1197 struct acpi_res_list *res_list_elem;
1198 int ioport;
1199 int clash = 0;
1200
1201 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1202 return 0;
1203 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1204 return 0;
1205
1206 ioport = res->flags & IORESOURCE_IO;
1207
1208 spin_lock(&acpi_res_lock);
1209 list_for_each_entry(res_list_elem, &resource_list_head,
1210 resource_list) {
1211 if (ioport && (res_list_elem->resource_type
1212 != ACPI_ADR_SPACE_SYSTEM_IO))
1213 continue;
1214 if (!ioport && (res_list_elem->resource_type
1215 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1216 continue;
1217
1218 if (res->end < res_list_elem->start
1219 || res_list_elem->end < res->start)
1220 continue;
1221 clash = 1;
1222 break;
1223 }
1224 spin_unlock(&acpi_res_lock);
1225
1226 if (clash) {
1227 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
Chase Douglas1638bca2010-03-22 15:08:09 -04001228 printk(KERN_WARNING "ACPI: resource %s %pR"
1229 " conflicts with ACPI region %s %pR\n",
1230 res->name, res, res_list_elem->name,
1231 res_list_elem);
Jean Delvare14f03342009-09-08 15:31:46 +02001232 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1233 printk(KERN_NOTICE "ACPI: This conflict may"
1234 " cause random problems and system"
1235 " instability\n");
1236 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1237 " for this device, you should use it instead of"
1238 " the native driver\n");
Thomas Renningerdf92e692008-02-04 23:31:22 -08001239 }
1240 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1241 return -EBUSY;
1242 }
1243 return 0;
1244}
Thomas Renninger443dea72008-02-04 23:31:23 -08001245EXPORT_SYMBOL(acpi_check_resource_conflict);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001246
1247int acpi_check_region(resource_size_t start, resource_size_t n,
1248 const char *name)
1249{
1250 struct resource res = {
1251 .start = start,
1252 .end = start + n - 1,
1253 .name = name,
1254 .flags = IORESOURCE_IO,
1255 };
1256
1257 return acpi_check_resource_conflict(&res);
1258}
1259EXPORT_SYMBOL(acpi_check_region);
1260
1261int acpi_check_mem_region(resource_size_t start, resource_size_t n,
1262 const char *name)
1263{
1264 struct resource res = {
1265 .start = start,
1266 .end = start + n - 1,
1267 .name = name,
1268 .flags = IORESOURCE_MEM,
1269 };
1270
1271 return acpi_check_resource_conflict(&res);
1272
1273}
1274EXPORT_SYMBOL(acpi_check_mem_region);
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276/*
Jean Delvare70dd6be2010-05-27 19:58:37 +02001277 * Let drivers know whether the resource checks are effective
1278 */
1279int acpi_resources_are_enforced(void)
1280{
1281 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1282}
1283EXPORT_SYMBOL(acpi_resources_are_enforced);
1284
1285/*
Robert Moore73459f72005-06-24 00:00:00 -04001286 * Acquire a spinlock.
1287 *
1288 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001289 */
1290
Bob Moore967440e32006-06-23 17:04:00 -04001291acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001292{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001293 acpi_cpu_flags flags;
Bob Moore967440e32006-06-23 17:04:00 -04001294 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001295 return flags;
1296}
1297
1298/*
1299 * Release a spinlock. See above.
1300 */
1301
Bob Moore967440e32006-06-23 17:04:00 -04001302void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001303{
Bob Moore967440e32006-06-23 17:04:00 -04001304 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001305}
1306
Robert Moore73459f72005-06-24 00:00:00 -04001307#ifndef ACPI_USE_LOCAL_CACHE
1308
1309/*******************************************************************************
1310 *
1311 * FUNCTION: acpi_os_create_cache
1312 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001313 * PARAMETERS: name - Ascii name for the cache
1314 * size - Size of each cached object
1315 * depth - Maximum depth of the cache (in objects) <ignored>
1316 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001317 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001318 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001319 *
1320 * DESCRIPTION: Create a cache object
1321 *
1322 ******************************************************************************/
1323
1324acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001325acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001326{
Paul Mundt20c2df82007-07-20 10:11:58 +09001327 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001328 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001329 return AE_ERROR;
1330 else
1331 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001332}
1333
1334/*******************************************************************************
1335 *
1336 * FUNCTION: acpi_os_purge_cache
1337 *
1338 * PARAMETERS: Cache - Handle to cache object
1339 *
1340 * RETURN: Status
1341 *
1342 * DESCRIPTION: Free all objects within the requested cache.
1343 *
1344 ******************************************************************************/
1345
Len Brown4be44fc2005-08-05 00:44:28 -04001346acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001347{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001348 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001349 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001350}
1351
1352/*******************************************************************************
1353 *
1354 * FUNCTION: acpi_os_delete_cache
1355 *
1356 * PARAMETERS: Cache - Handle to cache object
1357 *
1358 * RETURN: Status
1359 *
1360 * DESCRIPTION: Free all objects within the requested cache and delete the
1361 * cache object.
1362 *
1363 ******************************************************************************/
1364
Len Brown4be44fc2005-08-05 00:44:28 -04001365acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001366{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001367 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001368 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001369}
1370
1371/*******************************************************************************
1372 *
1373 * FUNCTION: acpi_os_release_object
1374 *
1375 * PARAMETERS: Cache - Handle to cache object
1376 * Object - The object to be released
1377 *
1378 * RETURN: None
1379 *
1380 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1381 * the object is deleted.
1382 *
1383 ******************************************************************************/
1384
Len Brown4be44fc2005-08-05 00:44:28 -04001385acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001386{
Len Brown4be44fc2005-08-05 00:44:28 -04001387 kmem_cache_free(cache, object);
1388 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001389}
1390
Bob Mooreb229cf92006-04-21 17:15:00 -04001391/******************************************************************************
1392 *
1393 * FUNCTION: acpi_os_validate_interface
1394 *
1395 * PARAMETERS: interface - Requested interface to be validated
1396 *
1397 * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
1398 *
1399 * DESCRIPTION: Match an interface string to the interfaces supported by the
1400 * host. Strings originate from an AML call to the _OSI method.
1401 *
1402 *****************************************************************************/
1403
1404acpi_status
1405acpi_os_validate_interface (char *interface)
1406{
Len Brownae00d812007-05-29 18:43:33 -04001407 if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1408 return AE_OK;
Len Brownf5076542007-05-30 00:10:38 -04001409 if (!strcmp("Linux", interface)) {
Len Brownd4b7dc42008-01-23 20:50:56 -05001410
1411 printk(KERN_NOTICE PREFIX
1412 "BIOS _OSI(Linux) query %s%s\n",
1413 osi_linux.enable ? "honored" : "ignored",
1414 osi_linux.cmdline ? " via cmdline" :
1415 osi_linux.dmi ? " via DMI" : "");
1416
Len Brownd4b7dc42008-01-23 20:50:56 -05001417 if (osi_linux.enable)
Len Brownf5076542007-05-30 00:10:38 -04001418 return AE_OK;
1419 }
Len Brownae00d812007-05-29 18:43:33 -04001420 return AE_SUPPORT;
Bob Mooreb229cf92006-04-21 17:15:00 -04001421}
1422
Lin Minga5fe1a02009-08-13 10:43:27 +08001423static inline int acpi_res_list_add(struct acpi_res_list *res)
1424{
1425 struct acpi_res_list *res_list_elem;
1426
1427 list_for_each_entry(res_list_elem, &resource_list_head,
1428 resource_list) {
1429
1430 if (res->resource_type == res_list_elem->resource_type &&
1431 res->start == res_list_elem->start &&
1432 res->end == res_list_elem->end) {
1433
1434 /*
1435 * The Region(addr,len) already exist in the list,
1436 * just increase the count
1437 */
1438
1439 res_list_elem->count++;
1440 return 0;
1441 }
1442 }
1443
1444 res->count = 1;
1445 list_add(&res->resource_list, &resource_list_head);
1446 return 1;
1447}
1448
1449static inline void acpi_res_list_del(struct acpi_res_list *res)
1450{
1451 struct acpi_res_list *res_list_elem;
1452
1453 list_for_each_entry(res_list_elem, &resource_list_head,
1454 resource_list) {
1455
1456 if (res->resource_type == res_list_elem->resource_type &&
1457 res->start == res_list_elem->start &&
1458 res->end == res_list_elem->end) {
1459
1460 /*
1461 * If the res count is decreased to 0,
1462 * remove and free it
1463 */
1464
1465 if (--res_list_elem->count == 0) {
1466 list_del(&res_list_elem->resource_list);
1467 kfree(res_list_elem);
1468 }
1469 return;
1470 }
1471 }
1472}
1473
1474acpi_status
1475acpi_os_invalidate_address(
1476 u8 space_id,
1477 acpi_physical_address address,
1478 acpi_size length)
1479{
1480 struct acpi_res_list res;
1481
1482 switch (space_id) {
1483 case ACPI_ADR_SPACE_SYSTEM_IO:
1484 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001485 /* Only interference checks against SystemIO and SystemMemory
Lin Minga5fe1a02009-08-13 10:43:27 +08001486 are needed */
1487 res.start = address;
1488 res.end = address + length - 1;
1489 res.resource_type = space_id;
1490 spin_lock(&acpi_res_lock);
1491 acpi_res_list_del(&res);
1492 spin_unlock(&acpi_res_lock);
1493 break;
1494 case ACPI_ADR_SPACE_PCI_CONFIG:
1495 case ACPI_ADR_SPACE_EC:
1496 case ACPI_ADR_SPACE_SMBUS:
1497 case ACPI_ADR_SPACE_CMOS:
1498 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1499 case ACPI_ADR_SPACE_DATA_TABLE:
1500 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1501 break;
1502 }
1503 return AE_OK;
1504}
1505
Bob Mooreb229cf92006-04-21 17:15:00 -04001506/******************************************************************************
1507 *
1508 * FUNCTION: acpi_os_validate_address
1509 *
1510 * PARAMETERS: space_id - ACPI space ID
1511 * address - Physical address
1512 * length - Address length
1513 *
1514 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1515 * should return AE_AML_ILLEGAL_ADDRESS.
1516 *
1517 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1518 * the addresses accessed by AML operation regions.
1519 *
1520 *****************************************************************************/
1521
1522acpi_status
1523acpi_os_validate_address (
1524 u8 space_id,
1525 acpi_physical_address address,
Thomas Renningerdf92e692008-02-04 23:31:22 -08001526 acpi_size length,
1527 char *name)
Bob Mooreb229cf92006-04-21 17:15:00 -04001528{
Thomas Renningerdf92e692008-02-04 23:31:22 -08001529 struct acpi_res_list *res;
Lin Minga5fe1a02009-08-13 10:43:27 +08001530 int added;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001531 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1532 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001533
Thomas Renningerdf92e692008-02-04 23:31:22 -08001534 switch (space_id) {
1535 case ACPI_ADR_SPACE_SYSTEM_IO:
1536 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Thomas Weber88393162010-03-16 11:47:56 +01001537 /* Only interference checks against SystemIO and SystemMemory
Thomas Renningerdf92e692008-02-04 23:31:22 -08001538 are needed */
1539 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1540 if (!res)
1541 return AE_OK;
1542 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1543 strlcpy(res->name, name, 5);
1544 res->start = address;
1545 res->end = address + length - 1;
1546 res->resource_type = space_id;
1547 spin_lock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001548 added = acpi_res_list_add(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001549 spin_unlock(&acpi_res_lock);
Lin Minga5fe1a02009-08-13 10:43:27 +08001550 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1551 "name: %s\n", added ? "Added" : "Already exist",
1552 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001553 ? "SystemIO" : "System Memory",
1554 (unsigned long long)res->start,
1555 (unsigned long long)res->end,
1556 res->name);
Lin Minga5fe1a02009-08-13 10:43:27 +08001557 if (!added)
1558 kfree(res);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001559 break;
1560 case ACPI_ADR_SPACE_PCI_CONFIG:
1561 case ACPI_ADR_SPACE_EC:
1562 case ACPI_ADR_SPACE_SMBUS:
1563 case ACPI_ADR_SPACE_CMOS:
1564 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1565 case ACPI_ADR_SPACE_DATA_TABLE:
1566 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1567 break;
1568 }
1569 return AE_OK;
Bob Mooreb229cf92006-04-21 17:15:00 -04001570}
Robert Moore73459f72005-06-24 00:00:00 -04001571#endif
Myron Stowed362eda2010-10-21 14:24:04 -06001572
1573acpi_status __init acpi_os_initialize(void)
1574{
1575 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1576 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1577 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1578 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1579
1580 return AE_OK;
1581}
1582
1583acpi_status acpi_os_initialize1(void)
1584{
1585 kacpid_wq = create_workqueue("kacpid");
1586 kacpi_notify_wq = create_workqueue("kacpi_notify");
1587 kacpi_hotplug_wq = create_workqueue("kacpi_hotplug");
1588 BUG_ON(!kacpid_wq);
1589 BUG_ON(!kacpi_notify_wq);
1590 BUG_ON(!kacpi_hotplug_wq);
1591 return AE_OK;
1592}
1593
1594acpi_status acpi_os_terminate(void)
1595{
1596 if (acpi_irq_handler) {
1597 acpi_os_remove_interrupt_handler(acpi_irq_irq,
1598 acpi_irq_handler);
1599 }
1600
1601 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1602 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1603 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1604 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1605
1606 destroy_workqueue(kacpid_wq);
1607 destroy_workqueue(kacpi_notify_wq);
1608 destroy_workqueue(kacpi_hotplug_wq);
1609
1610 return AE_OK;
1611}