blob: fde7ac82d6112948976e1977d0df447805afef8a [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>
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 *
26 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/slab.h>
31#include <linux/mm.h>
32#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/interrupt.h>
34#include <linux/kmod.h>
35#include <linux/delay.h>
Len Brownf5076542007-05-30 00:10:38 -040036#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/workqueue.h>
38#include <linux/nmi.h>
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +030039#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <acpi/acpi.h>
41#include <asm/io.h>
42#include <acpi/acpi_bus.h>
43#include <acpi/processor.h>
44#include <asm/uaccess.h>
45
46#include <linux/efi.h>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define _COMPONENT ACPI_OS_SERVICES
Len Brownf52fd662007-02-12 22:42:12 -050049ACPI_MODULE_NAME("osl");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define PREFIX "ACPI: "
Len Brown4be44fc2005-08-05 00:44:28 -040051struct acpi_os_dpc {
52 acpi_osd_exec_callback function;
53 void *context;
David Howells65f27f32006-11-22 14:55:48 +000054 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
57#ifdef CONFIG_ACPI_CUSTOM_DSDT
58#include CONFIG_ACPI_CUSTOM_DSDT_FILE
59#endif
60
61#ifdef ENABLE_DEBUGGER
62#include <linux/kdb.h>
63
64/* stuff for debugger support */
65int acpi_in_debugger;
66EXPORT_SYMBOL(acpi_in_debugger);
67
68extern char line_buf[80];
Len Brown4be44fc2005-08-05 00:44:28 -040069#endif /*ENABLE_DEBUGGER */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71static unsigned int acpi_irq_irq;
72static acpi_osd_handler acpi_irq_handler;
73static void *acpi_irq_context;
74static struct workqueue_struct *kacpid_wq;
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -040075static struct workqueue_struct *kacpi_notify_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Len Brownae00d812007-05-29 18:43:33 -040077#define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
78static char osi_additional_string[OSI_STRING_LENGTH_MAX];
79
Len Brownd4b7dc42008-01-23 20:50:56 -050080/*
81 * "Ode to _OSI(Linux)"
82 *
83 * osi_linux -- Control response to BIOS _OSI(Linux) query.
84 *
85 * As Linux evolves, the features that it supports change.
86 * So an OSI string such as "Linux" is not specific enough
87 * to be useful across multiple versions of Linux. It
88 * doesn't identify any particular feature, interface,
89 * or even any particular version of Linux...
90 *
91 * Unfortunately, Linux-2.6.22 and earlier responded "yes"
92 * to a BIOS _OSI(Linux) query. When
93 * a reference mobile BIOS started using it, its use
94 * started to spread to many vendor platforms.
95 * As it is not supportable, we need to halt that spread.
96 *
97 * Today, most BIOS references to _OSI(Linux) are noise --
98 * they have no functional effect and are just dead code
99 * carried over from the reference BIOS.
100 *
101 * The next most common case is that _OSI(Linux) harms Linux,
102 * usually by causing the BIOS to follow paths that are
103 * not tested during Windows validation.
104 *
105 * Finally, there is a short list of platforms
106 * where OSI(Linux) benefits Linux.
107 *
108 * In Linux-2.6.23, OSI(Linux) is first disabled by default.
109 * DMI is used to disable the dmesg warning about OSI(Linux)
110 * on platforms where it is known to have no effect.
111 * But a dmesg warning remains for systems where
112 * we do not know if OSI(Linux) is good or bad for the system.
113 * DMI is also used to enable OSI(Linux) for the machines
114 * that are known to need it.
115 *
116 * BIOS writers should NOT query _OSI(Linux) on future systems.
117 * It will be ignored by default, and to get Linux to
118 * not ignore it will require a kernel source update to
119 * add a DMI entry, or a boot-time "acpi_osi=Linux" invocation.
120 */
121#define OSI_LINUX_ENABLE 0
122
Adrian Bunk1d15d842008-01-29 00:10:15 +0200123static struct osi_linux {
Len Brownd4b7dc42008-01-23 20:50:56 -0500124 unsigned int enable:1;
125 unsigned int dmi:1;
126 unsigned int cmdline:1;
127 unsigned int known:1;
128} osi_linux = { OSI_LINUX_ENABLE, 0, 0, 0};
Len Brownf5076542007-05-30 00:10:38 -0400129
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700130static void __init acpi_request_region (struct acpi_generic_address *addr,
131 unsigned int length, char *desc)
132{
133 struct resource *res;
134
135 if (!addr->address || !length)
136 return;
137
Len Browneee3c852007-02-03 01:38:16 -0500138 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700139 res = request_region(addr->address, length, desc);
Len Browneee3c852007-02-03 01:38:16 -0500140 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700141 res = request_mem_region(addr->address, length, desc);
142}
143
144static int __init acpi_reserve_resources(void)
145{
Len Browneee3c852007-02-03 01:38:16 -0500146 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700147 "ACPI PM1a_EVT_BLK");
148
Len Browneee3c852007-02-03 01:38:16 -0500149 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700150 "ACPI PM1b_EVT_BLK");
151
Len Browneee3c852007-02-03 01:38:16 -0500152 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700153 "ACPI PM1a_CNT_BLK");
154
Len Browneee3c852007-02-03 01:38:16 -0500155 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700156 "ACPI PM1b_CNT_BLK");
157
Len Browneee3c852007-02-03 01:38:16 -0500158 if (acpi_gbl_FADT.pm_timer_length == 4)
159 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700160
Len Browneee3c852007-02-03 01:38:16 -0500161 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700162 "ACPI PM2_CNT_BLK");
163
164 /* Length of GPE blocks must be a non-negative multiple of 2 */
165
Len Browneee3c852007-02-03 01:38:16 -0500166 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
167 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
168 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700169
Len Browneee3c852007-02-03 01:38:16 -0500170 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
171 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
172 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700173
174 return 0;
175}
176device_initcall(acpi_reserve_resources);
177
Len Browndd272b52007-05-30 00:26:11 -0400178acpi_status __init acpi_os_initialize(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 return AE_OK;
181}
182
Len Brown4be44fc2005-08-05 00:44:28 -0400183acpi_status acpi_os_initialize1(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 /*
186 * Initialize PCI configuration space access, as we'll need to access
187 * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (!raw_pci_ops) {
Len Brown4be44fc2005-08-05 00:44:28 -0400190 printk(KERN_ERR PREFIX
191 "Access to PCI configuration space unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return AE_NULL_ENTRY;
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 kacpid_wq = create_singlethread_workqueue("kacpid");
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400195 kacpi_notify_wq = create_singlethread_workqueue("kacpi_notify");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 BUG_ON(!kacpid_wq);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400197 BUG_ON(!kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return AE_OK;
199}
200
Len Brown4be44fc2005-08-05 00:44:28 -0400201acpi_status acpi_os_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 if (acpi_irq_handler) {
204 acpi_os_remove_interrupt_handler(acpi_irq_irq,
205 acpi_irq_handler);
206 }
207
208 destroy_workqueue(kacpid_wq);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400209 destroy_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 return AE_OK;
212}
213
Len Brown4be44fc2005-08-05 00:44:28 -0400214void acpi_os_printf(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 va_list args;
217 va_start(args, fmt);
218 acpi_os_vprintf(fmt, args);
219 va_end(args);
220}
Len Brown4be44fc2005-08-05 00:44:28 -0400221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222EXPORT_SYMBOL(acpi_os_printf);
223
Len Brown4be44fc2005-08-05 00:44:28 -0400224void acpi_os_vprintf(const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 static char buffer[512];
Len Brown4be44fc2005-08-05 00:44:28 -0400227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 vsprintf(buffer, fmt, args);
229
230#ifdef ENABLE_DEBUGGER
231 if (acpi_in_debugger) {
232 kdb_printf("%s", buffer);
233 } else {
234 printk("%s", buffer);
235 }
236#else
237 printk("%s", buffer);
238#endif
239}
240
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300241acpi_physical_address __init acpi_os_get_root_pointer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 if (efi_enabled) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800244 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300245 return efi.acpi20;
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800246 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300247 return efi.acpi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400249 printk(KERN_ERR PREFIX
250 "System description tables not found\n");
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300253 } else
254 return acpi_find_rsdp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300257void __iomem *acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800259 if (phys > ULONG_MAX) {
260 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
Randy Dunlap70c08462007-02-13 16:11:36 -0800261 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300263 if (acpi_gbl_permanent_mmap)
264 /*
265 * ioremap checks to ensure this is in reserved space
266 */
267 return ioremap((unsigned long)phys, size);
268 else
269 return __acpi_map_table((unsigned long)phys, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800271EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Len Brown4be44fc2005-08-05 00:44:28 -0400273void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300275 if (acpi_gbl_permanent_mmap) {
276 iounmap(virt);
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800279EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281#ifdef ACPI_FUTURE_USAGE
282acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400283acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Len Brown4be44fc2005-08-05 00:44:28 -0400285 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return AE_BAD_PARAMETER;
287
288 *phys = virt_to_phys(virt);
289
290 return AE_OK;
291}
292#endif
293
294#define ACPI_MAX_OVERRIDE_LEN 100
295
296static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
297
298acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400299acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
300 acpi_string * new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 if (!init_val || !new_val)
303 return AE_BAD_PARAMETER;
304
305 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400306 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400308 acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 *new_val = acpi_os_name;
310 }
311
312 return AE_OK;
313}
314
315acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400316acpi_os_table_override(struct acpi_table_header * existing_table,
317 struct acpi_table_header ** new_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 if (!existing_table || !new_table)
320 return AE_BAD_PARAMETER;
321
322#ifdef CONFIG_ACPI_CUSTOM_DSDT
323 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400324 *new_table = (struct acpi_table_header *)AmlCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 else
326 *new_table = NULL;
327#else
328 *new_table = NULL;
329#endif
330 return AE_OK;
331}
332
David Howells7d12e782006-10-05 14:55:46 +0100333static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Len Brown4be44fc2005-08-05 00:44:28 -0400335 return (*acpi_irq_handler) (acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400339acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
340 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 unsigned int irq;
343
344 /*
345 * Ignore the GSI from the core, and use the value in our copy of the
346 * FADT. It may not be the same if an interrupt source override exists
347 * for the SCI.
348 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300349 gsi = acpi_gbl_FADT.sci_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
351 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
352 gsi);
353 return AE_OK;
354 }
355
356 acpi_irq_handler = handler;
357 acpi_irq_context = context;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700358 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
360 return AE_NOT_ACQUIRED;
361 }
362 acpi_irq_irq = irq;
363
364 return AE_OK;
365}
366
Len Brown4be44fc2005-08-05 00:44:28 -0400367acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 if (irq) {
370 free_irq(irq, acpi_irq);
371 acpi_irq_handler = NULL;
372 acpi_irq_irq = 0;
373 }
374
375 return AE_OK;
376}
377
378/*
379 * Running in interpreter thread context, safe to sleep
380 */
381
Len Brown4be44fc2005-08-05 00:44:28 -0400382void acpi_os_sleep(acpi_integer ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800384 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
Len Brown4be44fc2005-08-05 00:44:28 -0400386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387EXPORT_SYMBOL(acpi_os_sleep);
388
Len Brown4be44fc2005-08-05 00:44:28 -0400389void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 while (us) {
392 u32 delay = 1000;
393
394 if (delay > us)
395 delay = us;
396 udelay(delay);
397 touch_nmi_watchdog();
398 us -= delay;
399 }
400}
Len Brown4be44fc2005-08-05 00:44:28 -0400401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402EXPORT_SYMBOL(acpi_os_stall);
403
404/*
405 * Support ACPI 3.0 AML Timer operand
406 * Returns 64-bit free-running, monotonically increasing timer
407 * with 100ns granularity
408 */
Len Brown4be44fc2005-08-05 00:44:28 -0400409u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 static u64 t;
412
413#ifdef CONFIG_HPET
414 /* TBD: use HPET if available */
415#endif
416
417#ifdef CONFIG_X86_PM_TIMER
418 /* TBD: default to PM timer if HPET was not available */
419#endif
420 if (!t)
421 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
422
423 return ++t;
424}
425
Len Brown4be44fc2005-08-05 00:44:28 -0400426acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 u32 dummy;
429
430 if (!value)
431 value = &dummy;
432
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800433 *value = 0;
434 if (width <= 8) {
Len Brown4be44fc2005-08-05 00:44:28 -0400435 *(u8 *) value = inb(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800436 } else if (width <= 16) {
Len Brown4be44fc2005-08-05 00:44:28 -0400437 *(u16 *) value = inw(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800438 } else if (width <= 32) {
Len Brown4be44fc2005-08-05 00:44:28 -0400439 *(u32 *) value = inl(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800440 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 BUG();
442 }
443
444 return AE_OK;
445}
Len Brown4be44fc2005-08-05 00:44:28 -0400446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447EXPORT_SYMBOL(acpi_os_read_port);
448
Len Brown4be44fc2005-08-05 00:44:28 -0400449acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800451 if (width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 outb(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800453 } else if (width <= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 outw(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800455 } else if (width <= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 outl(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800457 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 BUG();
459 }
460
461 return AE_OK;
462}
Len Brown4be44fc2005-08-05 00:44:28 -0400463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464EXPORT_SYMBOL(acpi_os_write_port);
465
466acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400467acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Len Brown4be44fc2005-08-05 00:44:28 -0400469 u32 dummy;
470 void __iomem *virt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800472 virt_addr = ioremap(phys_addr, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (!value)
474 value = &dummy;
475
476 switch (width) {
477 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400478 *(u8 *) value = readb(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400481 *(u16 *) value = readw(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 break;
483 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400484 *(u32 *) value = readl(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 break;
486 default:
487 BUG();
488 }
489
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800490 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 return AE_OK;
493}
494
495acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400496acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Len Brown4be44fc2005-08-05 00:44:28 -0400498 void __iomem *virt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800500 virt_addr = ioremap(phys_addr, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 switch (width) {
503 case 8:
504 writeb(value, virt_addr);
505 break;
506 case 16:
507 writew(value, virt_addr);
508 break;
509 case 32:
510 writel(value, virt_addr);
511 break;
512 default:
513 BUG();
514 }
515
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800516 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 return AE_OK;
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400522acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
523 void *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 int result, size;
526
527 if (!value)
528 return AE_BAD_PARAMETER;
529
530 switch (width) {
531 case 8:
532 size = 1;
533 break;
534 case 16:
535 size = 2;
536 break;
537 case 32:
538 size = 4;
539 break;
540 default:
541 return AE_ERROR;
542 }
543
544 BUG_ON(!raw_pci_ops);
545
546 result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
Len Brown4be44fc2005-08-05 00:44:28 -0400547 PCI_DEVFN(pci_id->device, pci_id->function),
548 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 return (result ? AE_ERROR : AE_OK);
551}
Len Brown4be44fc2005-08-05 00:44:28 -0400552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553EXPORT_SYMBOL(acpi_os_read_pci_configuration);
554
555acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400556acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
557 acpi_integer value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 int result, size;
560
561 switch (width) {
562 case 8:
563 size = 1;
564 break;
565 case 16:
566 size = 2;
567 break;
568 case 32:
569 size = 4;
570 break;
571 default:
572 return AE_ERROR;
573 }
574
575 BUG_ON(!raw_pci_ops);
576
577 result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
Len Brown4be44fc2005-08-05 00:44:28 -0400578 PCI_DEVFN(pci_id->device, pci_id->function),
579 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 return (result ? AE_ERROR : AE_OK);
582}
583
584/* TODO: Change code to take advantage of driver model more */
Len Brown4be44fc2005-08-05 00:44:28 -0400585static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
586 acpi_handle chandle, /* current node */
587 struct acpi_pci_id **id,
588 int *is_bridge, u8 * bus_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Len Brown4be44fc2005-08-05 00:44:28 -0400590 acpi_handle handle;
591 struct acpi_pci_id *pci_id = *id;
592 acpi_status status;
593 unsigned long temp;
594 acpi_object_type type;
595 u8 tu8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 acpi_get_parent(chandle, &handle);
598 if (handle != rhandle) {
Len Brown4be44fc2005-08-05 00:44:28 -0400599 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
600 bus_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 status = acpi_get_type(handle, &type);
Len Brown4be44fc2005-08-05 00:44:28 -0400603 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return;
605
Len Brown4be44fc2005-08-05 00:44:28 -0400606 status =
607 acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
608 &temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (ACPI_SUCCESS(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400610 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
611 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 if (*is_bridge)
614 pci_id->bus = *bus_number;
615
616 /* any nicer way to get bus number of bridge ? */
Len Brown4be44fc2005-08-05 00:44:28 -0400617 status =
618 acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
619 8);
620 if (ACPI_SUCCESS(status)
621 && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
622 status =
623 acpi_os_read_pci_configuration(pci_id, 0x18,
624 &tu8, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (!ACPI_SUCCESS(status)) {
626 /* Certainly broken... FIX ME */
627 return;
628 }
629 *is_bridge = 1;
630 pci_id->bus = tu8;
Len Brown4be44fc2005-08-05 00:44:28 -0400631 status =
632 acpi_os_read_pci_configuration(pci_id, 0x19,
633 &tu8, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (ACPI_SUCCESS(status)) {
635 *bus_number = tu8;
636 }
637 } else
638 *is_bridge = 0;
639 }
640 }
641}
642
Len Brown4be44fc2005-08-05 00:44:28 -0400643void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
644 acpi_handle chandle, /* current node */
645 struct acpi_pci_id **id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 int is_bridge = 1;
648 u8 bus_number = (*id)->bus;
649
650 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
651}
652
David Howells65f27f32006-11-22 14:55:48 +0000653static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
David Howells65f27f32006-11-22 14:55:48 +0000655 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400656 if (!dpc) {
657 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
658 return;
659 }
660
661 dpc->function(dpc->context);
662 kfree(dpc);
663
664 /* Yield cpu to notify thread */
665 cond_resched();
666
667 return;
668}
669
670static void acpi_os_execute_notify(struct work_struct *work)
671{
672 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!dpc) {
Len Brown64684632006-06-26 23:41:38 -0400675 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400676 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
679 dpc->function(dpc->context);
680
681 kfree(dpc);
682
Patrick Mocheld550d982006-06-27 00:41:40 -0400683 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400686/*******************************************************************************
687 *
688 * FUNCTION: acpi_os_execute
689 *
690 * PARAMETERS: Type - Type of the callback
691 * Function - Function to be executed
692 * Context - Function parameters
693 *
694 * RETURN: Status
695 *
696 * DESCRIPTION: Depending on type, either queues function for deferred execution or
697 * immediately executes function on a separate thread.
698 *
699 ******************************************************************************/
700
701acpi_status acpi_os_execute(acpi_execute_type type,
Len Brown4be44fc2005-08-05 00:44:28 -0400702 acpi_osd_exec_callback function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Len Brown4be44fc2005-08-05 00:44:28 -0400704 acpi_status status = AE_OK;
705 struct acpi_os_dpc *dpc;
Len Brown72945b22006-07-12 22:46:42 -0400706
Len Brown72945b22006-07-12 22:46:42 -0400707 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
708 "Scheduling function [%p(%p)] for deferred execution.\n",
709 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 if (!function)
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400712 return AE_BAD_PARAMETER;
Len Brown72945b22006-07-12 22:46:42 -0400713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /*
715 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +0000716 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * way that allows us to also free its memory inside the callee.
718 * Because we may want to schedule several tasks with different
719 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +0000720 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 */
Len Brown72945b22006-07-12 22:46:42 -0400722
David Howells65f27f32006-11-22 14:55:48 +0000723 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (!dpc)
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800725 return_ACPI_STATUS(AE_NO_MEMORY);
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 dpc->function = function;
728 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800729
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400730 if (type == OSL_NOTIFY_HANDLER) {
731 INIT_WORK(&dpc->work, acpi_os_execute_notify);
732 if (!queue_work(kacpi_notify_wq, &dpc->work)) {
733 status = AE_ERROR;
734 kfree(dpc);
735 }
736 } else {
737 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
738 if (!queue_work(kacpid_wq, &dpc->work)) {
739 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800740 "Call to queue_work() failed.\n"));
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400741 status = AE_ERROR;
742 kfree(dpc);
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800745 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
Len Brown4be44fc2005-08-05 00:44:28 -0400747
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400748EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Len Brown4be44fc2005-08-05 00:44:28 -0400750void acpi_os_wait_events_complete(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 flush_workqueue(kacpid_wq);
753}
Len Brown4be44fc2005-08-05 00:44:28 -0400754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755EXPORT_SYMBOL(acpi_os_wait_events_complete);
756
757/*
758 * Allocate the memory for a spinlock and initialize it.
759 */
Bob Moore967440e32006-06-23 17:04:00 -0400760acpi_status acpi_os_create_lock(acpi_spinlock * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Bob Moore967440e32006-06-23 17:04:00 -0400762 spin_lock_init(*handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Patrick Mocheld550d982006-06-27 00:41:40 -0400764 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767/*
768 * Deallocate the memory for a spinlock.
769 */
Bob Moore967440e32006-06-23 17:04:00 -0400770void acpi_os_delete_lock(acpi_spinlock handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Patrick Mocheld550d982006-06-27 00:41:40 -0400772 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400776acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Len Brown4be44fc2005-08-05 00:44:28 -0400778 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 sem = acpi_os_allocate(sizeof(struct semaphore));
782 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400783 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 memset(sem, 0, sizeof(struct semaphore));
785
786 sema_init(sem, initial_units);
787
Len Brown4be44fc2005-08-05 00:44:28 -0400788 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Len Brown4be44fc2005-08-05 00:44:28 -0400790 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
791 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Patrick Mocheld550d982006-06-27 00:41:40 -0400793 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Len Brown4be44fc2005-08-05 00:44:28 -0400796EXPORT_SYMBOL(acpi_os_create_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798/*
799 * TODO: A better way to delete semaphores? Linux doesn't have a
800 * 'delete_semaphore()' function -- may result in an invalid
801 * pointer dereference for non-synchronized consumers. Should
802 * we at least check for blocked threads and signal/cancel them?
803 */
804
Len Brown4be44fc2005-08-05 00:44:28 -0400805acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Len Brown4be44fc2005-08-05 00:44:28 -0400807 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400811 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Len Brown4be44fc2005-08-05 00:44:28 -0400813 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Len Brown02438d82006-06-30 03:19:10 -0400815 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -0400816 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Patrick Mocheld550d982006-06-27 00:41:40 -0400818 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Len Brown4be44fc2005-08-05 00:44:28 -0400821EXPORT_SYMBOL(acpi_os_delete_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823/*
824 * TODO: The kernel doesn't have a 'down_timeout' function -- had to
825 * improvise. The process is to sleep for one scheduler quantum
826 * until the semaphore becomes available. Downside is that this
827 * may result in starvation for timeout-based waits when there's
828 * lots of semaphore activity.
829 *
830 * TODO: Support for units > 1?
831 */
Len Brown4be44fc2005-08-05 00:44:28 -0400832acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Len Brown4be44fc2005-08-05 00:44:28 -0400834 acpi_status status = AE_OK;
835 struct semaphore *sem = (struct semaphore *)handle;
836 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400840 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400843 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Len Brown4be44fc2005-08-05 00:44:28 -0400845 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
846 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Len Brownd68909f2006-08-16 19:16:58 -0400848 /*
849 * This can be called during resume with interrupts off.
850 * Like boot-time, we should be single threaded and will
851 * always get the lock if we try -- timeout or not.
852 * If this doesn't succeed, then we will oops courtesy of
853 * might_sleep() in down().
854 */
855 if (!down_trylock(sem))
856 return AE_OK;
857
Len Brown4be44fc2005-08-05 00:44:28 -0400858 switch (timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 /*
860 * No Wait:
861 * --------
862 * A zero timeout value indicates that we shouldn't wait - just
863 * acquire the semaphore if available otherwise return AE_TIME
864 * (a.k.a. 'would block').
865 */
Len Brown4be44fc2005-08-05 00:44:28 -0400866 case 0:
867 if (down_trylock(sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 status = AE_TIME;
869 break;
870
871 /*
872 * Wait Indefinitely:
873 * ------------------
874 */
Len Brown4be44fc2005-08-05 00:44:28 -0400875 case ACPI_WAIT_FOREVER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 down(sem);
877 break;
878
879 /*
880 * Wait w/ Timeout:
881 * ----------------
882 */
Len Brown4be44fc2005-08-05 00:44:28 -0400883 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 // TODO: A better timeout algorithm?
885 {
886 int i = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400887 static const int quantum_ms = 1000 / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 ret = down_trylock(sem);
Yu Lumingdacd9b82005-12-31 01:45:00 -0500890 for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800891 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 ret = down_trylock(sem);
893 }
Len Brown4be44fc2005-08-05 00:44:28 -0400894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (ret != 0)
896 status = AE_TIME;
897 }
898 break;
899 }
900
901 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -0400902 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400903 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -0400904 handle, units, timeout,
905 acpi_format_exception(status)));
906 } else {
907 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400908 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400909 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911
Patrick Mocheld550d982006-06-27 00:41:40 -0400912 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Len Brown4be44fc2005-08-05 00:44:28 -0400915EXPORT_SYMBOL(acpi_os_wait_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917/*
918 * TODO: Support for units > 1?
919 */
Len Brown4be44fc2005-08-05 00:44:28 -0400920acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Len Brown4be44fc2005-08-05 00:44:28 -0400922 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400926 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400929 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Len Brown4be44fc2005-08-05 00:44:28 -0400931 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
932 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 up(sem);
935
Patrick Mocheld550d982006-06-27 00:41:40 -0400936 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
Len Brown4be44fc2005-08-05 00:44:28 -0400938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939EXPORT_SYMBOL(acpi_os_signal_semaphore);
940
941#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -0400942u32 acpi_os_get_line(char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944
945#ifdef ENABLE_DEBUGGER
946 if (acpi_in_debugger) {
947 u32 chars;
948
949 kdb_read(buffer, sizeof(line_buf));
950
951 /* remove the CR kdb includes */
952 chars = strlen(buffer) - 1;
953 buffer[chars] = '\0';
954 }
955#endif
956
957 return 0;
958}
Len Brown4be44fc2005-08-05 00:44:28 -0400959#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Len Brown4be44fc2005-08-05 00:44:28 -0400961acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Len Brown4be44fc2005-08-05 00:44:28 -0400963 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 case ACPI_SIGNAL_FATAL:
965 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
966 break;
967 case ACPI_SIGNAL_BREAKPOINT:
968 /*
969 * AML Breakpoint
970 * ACPI spec. says to treat it as a NOP unless
971 * you are debugging. So if/when we integrate
972 * AML debugger into the kernel debugger its
973 * hook will go here. But until then it is
974 * not useful to print anything on breakpoints.
975 */
976 break;
977 default:
978 break;
979 }
980
981 return AE_OK;
982}
Len Brown4be44fc2005-08-05 00:44:28 -0400983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984EXPORT_SYMBOL(acpi_os_signal);
985
Len Brown4be44fc2005-08-05 00:44:28 -0400986static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -0400989 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 if (!str || !*str)
992 return 0;
993
994 for (; count-- && str && *str; str++) {
995 if (isalnum(*str) || *str == ' ' || *str == ':')
996 *p++ = *str;
997 else if (*str == '\'' || *str == '"')
998 continue;
999 else
1000 break;
1001 }
1002 *p = 0;
1003
1004 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008__setup("acpi_os_name=", acpi_os_name_setup);
1009
Len Brownd4b7dc42008-01-23 20:50:56 -05001010static void __init set_osi_linux(unsigned int enable)
1011{
1012 if (osi_linux.enable != enable) {
1013 osi_linux.enable = enable;
1014 printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
1015 enable ? "Add": "Delet");
1016 }
1017 return;
1018}
Len Brownf5076542007-05-30 00:10:38 -04001019
Len Brownd4b7dc42008-01-23 20:50:56 -05001020static void __init acpi_cmdline_osi_linux(unsigned int enable)
1021{
1022 osi_linux.cmdline = 1; /* cmdline set the default */
1023 set_osi_linux(enable);
Len Brownf5076542007-05-30 00:10:38 -04001024
Len Brownd4b7dc42008-01-23 20:50:56 -05001025 return;
1026}
1027
1028void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1029{
1030 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
1031
1032 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1033
1034 if (enable == -1)
1035 return;
1036
1037 osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
1038
1039 set_osi_linux(enable);
1040
Len Brownf5076542007-05-30 00:10:38 -04001041 return;
1042}
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044/*
Len Brownae00d812007-05-29 18:43:33 -04001045 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1046 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -04001048 * string starting with '!' disables that string
1049 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 */
Len Brown4be44fc2005-08-05 00:44:28 -04001051static int __init acpi_osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
1053 if (str == NULL || *str == '\0') {
1054 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1055 acpi_gbl_create_osi_method = FALSE;
Len Brownaa2e09d2007-07-02 23:57:45 -04001056 } else if (!strcmp("!Linux", str)) {
Len Brownd4b7dc42008-01-23 20:50:56 -05001057 acpi_cmdline_osi_linux(0); /* !enable */
Len Brownae00d812007-05-29 18:43:33 -04001058 } else if (*str == '!') {
1059 if (acpi_osi_invalidate(++str) == AE_OK)
1060 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
Len Brownf5076542007-05-30 00:10:38 -04001061 } else if (!strcmp("Linux", str)) {
Len Brownd4b7dc42008-01-23 20:50:56 -05001062 acpi_cmdline_osi_linux(1); /* enable */
Len Brownae00d812007-05-29 18:43:33 -04001063 } else if (*osi_additional_string == '\0') {
1064 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
1065 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067
1068 return 1;
1069}
1070
1071__setup("acpi_osi=", acpi_osi_setup);
1072
1073/* enable serialization to combat AE_ALREADY_EXISTS errors */
Len Brown4be44fc2005-08-05 00:44:28 -04001074static int __init acpi_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 printk(KERN_INFO PREFIX "serialize enabled\n");
1077
1078 acpi_gbl_all_methods_serialized = TRUE;
1079
1080 return 1;
1081}
1082
1083__setup("acpi_serialize", acpi_serialize_setup);
1084
1085/*
1086 * Wake and Run-Time GPES are expected to be separate.
1087 * We disable wake-GPEs at run-time to prevent spurious
1088 * interrupts.
1089 *
1090 * However, if a system exists that shares Wake and
1091 * Run-time events on the same GPE this flag is available
1092 * to tell Linux to keep the wake-time GPEs enabled at run-time.
1093 */
Len Brown4be44fc2005-08-05 00:44:28 -04001094static int __init acpi_wake_gpes_always_on_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
1097
1098 acpi_gbl_leave_wake_gpes_disabled = FALSE;
1099
1100 return 1;
1101}
1102
1103__setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105/*
Robert Moore73459f72005-06-24 00:00:00 -04001106 * Acquire a spinlock.
1107 *
1108 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001109 */
1110
Bob Moore967440e32006-06-23 17:04:00 -04001111acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001112{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001113 acpi_cpu_flags flags;
Bob Moore967440e32006-06-23 17:04:00 -04001114 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001115 return flags;
1116}
1117
1118/*
1119 * Release a spinlock. See above.
1120 */
1121
Bob Moore967440e32006-06-23 17:04:00 -04001122void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001123{
Bob Moore967440e32006-06-23 17:04:00 -04001124 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001125}
1126
Robert Moore73459f72005-06-24 00:00:00 -04001127#ifndef ACPI_USE_LOCAL_CACHE
1128
1129/*******************************************************************************
1130 *
1131 * FUNCTION: acpi_os_create_cache
1132 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001133 * PARAMETERS: name - Ascii name for the cache
1134 * size - Size of each cached object
1135 * depth - Maximum depth of the cache (in objects) <ignored>
1136 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001137 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001138 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001139 *
1140 * DESCRIPTION: Create a cache object
1141 *
1142 ******************************************************************************/
1143
1144acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001145acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001146{
Paul Mundt20c2df82007-07-20 10:11:58 +09001147 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001148 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001149 return AE_ERROR;
1150 else
1151 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001152}
1153
1154/*******************************************************************************
1155 *
1156 * FUNCTION: acpi_os_purge_cache
1157 *
1158 * PARAMETERS: Cache - Handle to cache object
1159 *
1160 * RETURN: Status
1161 *
1162 * DESCRIPTION: Free all objects within the requested cache.
1163 *
1164 ******************************************************************************/
1165
Len Brown4be44fc2005-08-05 00:44:28 -04001166acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001167{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001168 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001169 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001170}
1171
1172/*******************************************************************************
1173 *
1174 * FUNCTION: acpi_os_delete_cache
1175 *
1176 * PARAMETERS: Cache - Handle to cache object
1177 *
1178 * RETURN: Status
1179 *
1180 * DESCRIPTION: Free all objects within the requested cache and delete the
1181 * cache object.
1182 *
1183 ******************************************************************************/
1184
Len Brown4be44fc2005-08-05 00:44:28 -04001185acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001186{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001187 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001188 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001189}
1190
1191/*******************************************************************************
1192 *
1193 * FUNCTION: acpi_os_release_object
1194 *
1195 * PARAMETERS: Cache - Handle to cache object
1196 * Object - The object to be released
1197 *
1198 * RETURN: None
1199 *
1200 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1201 * the object is deleted.
1202 *
1203 ******************************************************************************/
1204
Len Brown4be44fc2005-08-05 00:44:28 -04001205acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001206{
Len Brown4be44fc2005-08-05 00:44:28 -04001207 kmem_cache_free(cache, object);
1208 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001209}
1210
Len Brown5a4e1432008-01-23 20:01:22 -05001211/**
1212 * acpi_dmi_dump - dump DMI slots needed for blacklist entry
1213 *
1214 * Returns 0 on success
1215 */
Adrian Bunk12d39312008-01-29 00:10:12 +02001216static int acpi_dmi_dump(void)
Len Brown5a4e1432008-01-23 20:01:22 -05001217{
1218
1219 if (!dmi_available)
1220 return -1;
1221
1222 printk(KERN_NOTICE PREFIX "DMI System Vendor: %s\n",
Len Browne6298c62008-01-25 15:40:02 -05001223 dmi_get_system_info(DMI_SYS_VENDOR));
Len Brown5a4e1432008-01-23 20:01:22 -05001224 printk(KERN_NOTICE PREFIX "DMI Product Name: %s\n",
Len Browne6298c62008-01-25 15:40:02 -05001225 dmi_get_system_info(DMI_PRODUCT_NAME));
Len Brown5a4e1432008-01-23 20:01:22 -05001226 printk(KERN_NOTICE PREFIX "DMI Product Version: %s\n",
Len Browne6298c62008-01-25 15:40:02 -05001227 dmi_get_system_info(DMI_PRODUCT_VERSION));
Len Brown5a4e1432008-01-23 20:01:22 -05001228 printk(KERN_NOTICE PREFIX "DMI Board Name: %s\n",
Len Browne6298c62008-01-25 15:40:02 -05001229 dmi_get_system_info(DMI_BOARD_NAME));
Len Brown5a4e1432008-01-23 20:01:22 -05001230 printk(KERN_NOTICE PREFIX "DMI BIOS Vendor: %s\n",
Len Browne6298c62008-01-25 15:40:02 -05001231 dmi_get_system_info(DMI_BIOS_VENDOR));
Len Brown5a4e1432008-01-23 20:01:22 -05001232 printk(KERN_NOTICE PREFIX "DMI BIOS Date: %s\n",
Len Browne6298c62008-01-25 15:40:02 -05001233 dmi_get_system_info(DMI_BIOS_DATE));
Len Brown5a4e1432008-01-23 20:01:22 -05001234
1235 return 0;
1236}
1237
1238
Bob Mooreb229cf92006-04-21 17:15:00 -04001239/******************************************************************************
1240 *
1241 * FUNCTION: acpi_os_validate_interface
1242 *
1243 * PARAMETERS: interface - Requested interface to be validated
1244 *
1245 * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
1246 *
1247 * DESCRIPTION: Match an interface string to the interfaces supported by the
1248 * host. Strings originate from an AML call to the _OSI method.
1249 *
1250 *****************************************************************************/
1251
1252acpi_status
1253acpi_os_validate_interface (char *interface)
1254{
Len Brownae00d812007-05-29 18:43:33 -04001255 if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1256 return AE_OK;
Len Brownf5076542007-05-30 00:10:38 -04001257 if (!strcmp("Linux", interface)) {
Len Brownd4b7dc42008-01-23 20:50:56 -05001258
1259 printk(KERN_NOTICE PREFIX
1260 "BIOS _OSI(Linux) query %s%s\n",
1261 osi_linux.enable ? "honored" : "ignored",
1262 osi_linux.cmdline ? " via cmdline" :
1263 osi_linux.dmi ? " via DMI" : "");
1264
1265 if (!osi_linux.dmi) {
1266 if (acpi_dmi_dump())
1267 printk(KERN_NOTICE PREFIX
1268 "[please extract dmidecode output]\n");
Len Brownf40cd6f2008-01-23 20:04:28 -05001269 printk(KERN_NOTICE PREFIX
Len Brownd4b7dc42008-01-23 20:50:56 -05001270 "Please send DMI info above to "
1271 "linux-acpi@vger.kernel.org\n");
1272 }
1273 if (!osi_linux.known && !osi_linux.cmdline) {
1274 printk(KERN_NOTICE PREFIX
1275 "If \"acpi_osi=%sLinux\" works better, "
1276 "please notify linux-acpi@vger.kernel.org\n",
1277 osi_linux.enable ? "!" : "");
1278 }
1279
1280 if (osi_linux.enable)
Len Brownf5076542007-05-30 00:10:38 -04001281 return AE_OK;
1282 }
Len Brownae00d812007-05-29 18:43:33 -04001283 return AE_SUPPORT;
Bob Mooreb229cf92006-04-21 17:15:00 -04001284}
1285
Bob Mooreb229cf92006-04-21 17:15:00 -04001286/******************************************************************************
1287 *
1288 * FUNCTION: acpi_os_validate_address
1289 *
1290 * PARAMETERS: space_id - ACPI space ID
1291 * address - Physical address
1292 * length - Address length
1293 *
1294 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1295 * should return AE_AML_ILLEGAL_ADDRESS.
1296 *
1297 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1298 * the addresses accessed by AML operation regions.
1299 *
1300 *****************************************************************************/
1301
1302acpi_status
1303acpi_os_validate_address (
1304 u8 space_id,
1305 acpi_physical_address address,
1306 acpi_size length)
1307{
1308
1309 return AE_OK;
1310}
1311
Robert Moore73459f72005-06-24 00:00:00 -04001312#endif