blob: 1087efeca9b1a1dbce96a89540264d5e37824c9e [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
123struct osi_linux {
124 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 Brown5229e872008-02-06 01:26:55 -0500335 u32 handled;
336
337 handled = (*acpi_irq_handler) (acpi_irq_context);
338
339 if (handled) {
340 acpi_irq_handled++;
341 return IRQ_HANDLED;
342 } else
343 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400347acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
348 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 unsigned int irq;
351
Len Brown5229e872008-02-06 01:26:55 -0500352 acpi_irq_stats_init();
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 /*
355 * Ignore the GSI from the core, and use the value in our copy of the
356 * FADT. It may not be the same if an interrupt source override exists
357 * for the SCI.
358 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300359 gsi = acpi_gbl_FADT.sci_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
361 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
362 gsi);
363 return AE_OK;
364 }
365
366 acpi_irq_handler = handler;
367 acpi_irq_context = context;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700368 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
370 return AE_NOT_ACQUIRED;
371 }
372 acpi_irq_irq = irq;
373
374 return AE_OK;
375}
376
Len Brown4be44fc2005-08-05 00:44:28 -0400377acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 if (irq) {
380 free_irq(irq, acpi_irq);
381 acpi_irq_handler = NULL;
382 acpi_irq_irq = 0;
383 }
384
385 return AE_OK;
386}
387
388/*
389 * Running in interpreter thread context, safe to sleep
390 */
391
Len Brown4be44fc2005-08-05 00:44:28 -0400392void acpi_os_sleep(acpi_integer ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800394 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
Len Brown4be44fc2005-08-05 00:44:28 -0400396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397EXPORT_SYMBOL(acpi_os_sleep);
398
Len Brown4be44fc2005-08-05 00:44:28 -0400399void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 while (us) {
402 u32 delay = 1000;
403
404 if (delay > us)
405 delay = us;
406 udelay(delay);
407 touch_nmi_watchdog();
408 us -= delay;
409 }
410}
Len Brown4be44fc2005-08-05 00:44:28 -0400411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412EXPORT_SYMBOL(acpi_os_stall);
413
414/*
415 * Support ACPI 3.0 AML Timer operand
416 * Returns 64-bit free-running, monotonically increasing timer
417 * with 100ns granularity
418 */
Len Brown4be44fc2005-08-05 00:44:28 -0400419u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 static u64 t;
422
423#ifdef CONFIG_HPET
424 /* TBD: use HPET if available */
425#endif
426
427#ifdef CONFIG_X86_PM_TIMER
428 /* TBD: default to PM timer if HPET was not available */
429#endif
430 if (!t)
431 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
432
433 return ++t;
434}
435
Len Brown4be44fc2005-08-05 00:44:28 -0400436acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 u32 dummy;
439
440 if (!value)
441 value = &dummy;
442
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800443 *value = 0;
444 if (width <= 8) {
Len Brown4be44fc2005-08-05 00:44:28 -0400445 *(u8 *) value = inb(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800446 } else if (width <= 16) {
Len Brown4be44fc2005-08-05 00:44:28 -0400447 *(u16 *) value = inw(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800448 } else if (width <= 32) {
Len Brown4be44fc2005-08-05 00:44:28 -0400449 *(u32 *) value = inl(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800450 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 BUG();
452 }
453
454 return AE_OK;
455}
Len Brown4be44fc2005-08-05 00:44:28 -0400456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457EXPORT_SYMBOL(acpi_os_read_port);
458
Len Brown4be44fc2005-08-05 00:44:28 -0400459acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800461 if (width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 outb(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800463 } else if (width <= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 outw(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800465 } else if (width <= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 outl(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800467 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 BUG();
469 }
470
471 return AE_OK;
472}
Len Brown4be44fc2005-08-05 00:44:28 -0400473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474EXPORT_SYMBOL(acpi_os_write_port);
475
476acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400477acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Len Brown4be44fc2005-08-05 00:44:28 -0400479 u32 dummy;
480 void __iomem *virt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800482 virt_addr = ioremap(phys_addr, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (!value)
484 value = &dummy;
485
486 switch (width) {
487 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400488 *(u8 *) value = readb(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 break;
490 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400491 *(u16 *) value = readw(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
493 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400494 *(u32 *) value = readl(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 break;
496 default:
497 BUG();
498 }
499
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800500 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 return AE_OK;
503}
504
505acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400506acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Len Brown4be44fc2005-08-05 00:44:28 -0400508 void __iomem *virt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800510 virt_addr = ioremap(phys_addr, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 switch (width) {
513 case 8:
514 writeb(value, virt_addr);
515 break;
516 case 16:
517 writew(value, virt_addr);
518 break;
519 case 32:
520 writel(value, virt_addr);
521 break;
522 default:
523 BUG();
524 }
525
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800526 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 return AE_OK;
529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400532acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
533 void *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 int result, size;
536
537 if (!value)
538 return AE_BAD_PARAMETER;
539
540 switch (width) {
541 case 8:
542 size = 1;
543 break;
544 case 16:
545 size = 2;
546 break;
547 case 32:
548 size = 4;
549 break;
550 default:
551 return AE_ERROR;
552 }
553
554 BUG_ON(!raw_pci_ops);
555
556 result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
Len Brown4be44fc2005-08-05 00:44:28 -0400557 PCI_DEVFN(pci_id->device, pci_id->function),
558 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 return (result ? AE_ERROR : AE_OK);
561}
Len Brown4be44fc2005-08-05 00:44:28 -0400562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563EXPORT_SYMBOL(acpi_os_read_pci_configuration);
564
565acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400566acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
567 acpi_integer value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 int result, size;
570
571 switch (width) {
572 case 8:
573 size = 1;
574 break;
575 case 16:
576 size = 2;
577 break;
578 case 32:
579 size = 4;
580 break;
581 default:
582 return AE_ERROR;
583 }
584
585 BUG_ON(!raw_pci_ops);
586
587 result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
Len Brown4be44fc2005-08-05 00:44:28 -0400588 PCI_DEVFN(pci_id->device, pci_id->function),
589 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 return (result ? AE_ERROR : AE_OK);
592}
593
594/* TODO: Change code to take advantage of driver model more */
Len Brown4be44fc2005-08-05 00:44:28 -0400595static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
596 acpi_handle chandle, /* current node */
597 struct acpi_pci_id **id,
598 int *is_bridge, u8 * bus_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Len Brown4be44fc2005-08-05 00:44:28 -0400600 acpi_handle handle;
601 struct acpi_pci_id *pci_id = *id;
602 acpi_status status;
603 unsigned long temp;
604 acpi_object_type type;
605 u8 tu8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 acpi_get_parent(chandle, &handle);
608 if (handle != rhandle) {
Len Brown4be44fc2005-08-05 00:44:28 -0400609 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
610 bus_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 status = acpi_get_type(handle, &type);
Len Brown4be44fc2005-08-05 00:44:28 -0400613 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return;
615
Len Brown4be44fc2005-08-05 00:44:28 -0400616 status =
617 acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
618 &temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (ACPI_SUCCESS(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400620 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
621 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (*is_bridge)
624 pci_id->bus = *bus_number;
625
626 /* any nicer way to get bus number of bridge ? */
Len Brown4be44fc2005-08-05 00:44:28 -0400627 status =
628 acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
629 8);
630 if (ACPI_SUCCESS(status)
631 && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
632 status =
633 acpi_os_read_pci_configuration(pci_id, 0x18,
634 &tu8, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (!ACPI_SUCCESS(status)) {
636 /* Certainly broken... FIX ME */
637 return;
638 }
639 *is_bridge = 1;
640 pci_id->bus = tu8;
Len Brown4be44fc2005-08-05 00:44:28 -0400641 status =
642 acpi_os_read_pci_configuration(pci_id, 0x19,
643 &tu8, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (ACPI_SUCCESS(status)) {
645 *bus_number = tu8;
646 }
647 } else
648 *is_bridge = 0;
649 }
650 }
651}
652
Len Brown4be44fc2005-08-05 00:44:28 -0400653void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
654 acpi_handle chandle, /* current node */
655 struct acpi_pci_id **id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 int is_bridge = 1;
658 u8 bus_number = (*id)->bus;
659
660 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
661}
662
David Howells65f27f32006-11-22 14:55:48 +0000663static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
David Howells65f27f32006-11-22 14:55:48 +0000665 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400666 if (!dpc) {
667 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
668 return;
669 }
670
671 dpc->function(dpc->context);
672 kfree(dpc);
673
674 /* Yield cpu to notify thread */
675 cond_resched();
676
677 return;
678}
679
680static void acpi_os_execute_notify(struct work_struct *work)
681{
682 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (!dpc) {
Len Brown64684632006-06-26 23:41:38 -0400685 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400686 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688
689 dpc->function(dpc->context);
690
691 kfree(dpc);
692
Patrick Mocheld550d982006-06-27 00:41:40 -0400693 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400696/*******************************************************************************
697 *
698 * FUNCTION: acpi_os_execute
699 *
700 * PARAMETERS: Type - Type of the callback
701 * Function - Function to be executed
702 * Context - Function parameters
703 *
704 * RETURN: Status
705 *
706 * DESCRIPTION: Depending on type, either queues function for deferred execution or
707 * immediately executes function on a separate thread.
708 *
709 ******************************************************************************/
710
711acpi_status acpi_os_execute(acpi_execute_type type,
Len Brown4be44fc2005-08-05 00:44:28 -0400712 acpi_osd_exec_callback function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Len Brown4be44fc2005-08-05 00:44:28 -0400714 acpi_status status = AE_OK;
715 struct acpi_os_dpc *dpc;
Len Brown72945b22006-07-12 22:46:42 -0400716
Len Brown72945b22006-07-12 22:46:42 -0400717 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
718 "Scheduling function [%p(%p)] for deferred execution.\n",
719 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 if (!function)
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400722 return AE_BAD_PARAMETER;
Len Brown72945b22006-07-12 22:46:42 -0400723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /*
725 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +0000726 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * way that allows us to also free its memory inside the callee.
728 * Because we may want to schedule several tasks with different
729 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +0000730 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 */
Len Brown72945b22006-07-12 22:46:42 -0400732
David Howells65f27f32006-11-22 14:55:48 +0000733 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (!dpc)
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800735 return_ACPI_STATUS(AE_NO_MEMORY);
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 dpc->function = function;
738 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800739
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400740 if (type == OSL_NOTIFY_HANDLER) {
741 INIT_WORK(&dpc->work, acpi_os_execute_notify);
742 if (!queue_work(kacpi_notify_wq, &dpc->work)) {
743 status = AE_ERROR;
744 kfree(dpc);
745 }
746 } else {
747 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
748 if (!queue_work(kacpid_wq, &dpc->work)) {
749 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800750 "Call to queue_work() failed.\n"));
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400751 status = AE_ERROR;
752 kfree(dpc);
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800755 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
Len Brown4be44fc2005-08-05 00:44:28 -0400757
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400758EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Len Brown4be44fc2005-08-05 00:44:28 -0400760void acpi_os_wait_events_complete(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 flush_workqueue(kacpid_wq);
763}
Len Brown4be44fc2005-08-05 00:44:28 -0400764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765EXPORT_SYMBOL(acpi_os_wait_events_complete);
766
767/*
768 * Allocate the memory for a spinlock and initialize it.
769 */
Bob Moore967440e32006-06-23 17:04:00 -0400770acpi_status acpi_os_create_lock(acpi_spinlock * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Bob Moore967440e32006-06-23 17:04:00 -0400772 spin_lock_init(*handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Patrick Mocheld550d982006-06-27 00:41:40 -0400774 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777/*
778 * Deallocate the memory for a spinlock.
779 */
Bob Moore967440e32006-06-23 17:04:00 -0400780void acpi_os_delete_lock(acpi_spinlock handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Patrick Mocheld550d982006-06-27 00:41:40 -0400782 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400786acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Len Brown4be44fc2005-08-05 00:44:28 -0400788 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 sem = acpi_os_allocate(sizeof(struct semaphore));
792 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400793 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 memset(sem, 0, sizeof(struct semaphore));
795
796 sema_init(sem, initial_units);
797
Len Brown4be44fc2005-08-05 00:44:28 -0400798 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Len Brown4be44fc2005-08-05 00:44:28 -0400800 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
801 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Patrick Mocheld550d982006-06-27 00:41:40 -0400803 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Len Brown4be44fc2005-08-05 00:44:28 -0400806EXPORT_SYMBOL(acpi_os_create_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808/*
809 * TODO: A better way to delete semaphores? Linux doesn't have a
810 * 'delete_semaphore()' function -- may result in an invalid
811 * pointer dereference for non-synchronized consumers. Should
812 * we at least check for blocked threads and signal/cancel them?
813 */
814
Len Brown4be44fc2005-08-05 00:44:28 -0400815acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Len Brown4be44fc2005-08-05 00:44:28 -0400817 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400821 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Len Brown4be44fc2005-08-05 00:44:28 -0400823 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Len Brown02438d82006-06-30 03:19:10 -0400825 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -0400826 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Patrick Mocheld550d982006-06-27 00:41:40 -0400828 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Len Brown4be44fc2005-08-05 00:44:28 -0400831EXPORT_SYMBOL(acpi_os_delete_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833/*
834 * TODO: The kernel doesn't have a 'down_timeout' function -- had to
835 * improvise. The process is to sleep for one scheduler quantum
836 * until the semaphore becomes available. Downside is that this
837 * may result in starvation for timeout-based waits when there's
838 * lots of semaphore activity.
839 *
840 * TODO: Support for units > 1?
841 */
Len Brown4be44fc2005-08-05 00:44:28 -0400842acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Len Brown4be44fc2005-08-05 00:44:28 -0400844 acpi_status status = AE_OK;
845 struct semaphore *sem = (struct semaphore *)handle;
846 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400850 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400853 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Len Brown4be44fc2005-08-05 00:44:28 -0400855 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
856 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Len Brownd68909f2006-08-16 19:16:58 -0400858 /*
859 * This can be called during resume with interrupts off.
860 * Like boot-time, we should be single threaded and will
861 * always get the lock if we try -- timeout or not.
862 * If this doesn't succeed, then we will oops courtesy of
863 * might_sleep() in down().
864 */
865 if (!down_trylock(sem))
866 return AE_OK;
867
Len Brown4be44fc2005-08-05 00:44:28 -0400868 switch (timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 /*
870 * No Wait:
871 * --------
872 * A zero timeout value indicates that we shouldn't wait - just
873 * acquire the semaphore if available otherwise return AE_TIME
874 * (a.k.a. 'would block').
875 */
Len Brown4be44fc2005-08-05 00:44:28 -0400876 case 0:
877 if (down_trylock(sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 status = AE_TIME;
879 break;
880
881 /*
882 * Wait Indefinitely:
883 * ------------------
884 */
Len Brown4be44fc2005-08-05 00:44:28 -0400885 case ACPI_WAIT_FOREVER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 down(sem);
887 break;
888
889 /*
890 * Wait w/ Timeout:
891 * ----------------
892 */
Len Brown4be44fc2005-08-05 00:44:28 -0400893 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 // TODO: A better timeout algorithm?
895 {
896 int i = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400897 static const int quantum_ms = 1000 / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 ret = down_trylock(sem);
Yu Lumingdacd9b82005-12-31 01:45:00 -0500900 for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800901 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 ret = down_trylock(sem);
903 }
Len Brown4be44fc2005-08-05 00:44:28 -0400904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (ret != 0)
906 status = AE_TIME;
907 }
908 break;
909 }
910
911 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -0400912 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400913 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -0400914 handle, units, timeout,
915 acpi_format_exception(status)));
916 } else {
917 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400918 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400919 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
Patrick Mocheld550d982006-06-27 00:41:40 -0400922 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Len Brown4be44fc2005-08-05 00:44:28 -0400925EXPORT_SYMBOL(acpi_os_wait_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927/*
928 * TODO: Support for units > 1?
929 */
Len Brown4be44fc2005-08-05 00:44:28 -0400930acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Len Brown4be44fc2005-08-05 00:44:28 -0400932 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400936 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400939 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Len Brown4be44fc2005-08-05 00:44:28 -0400941 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
942 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 up(sem);
945
Patrick Mocheld550d982006-06-27 00:41:40 -0400946 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
Len Brown4be44fc2005-08-05 00:44:28 -0400948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949EXPORT_SYMBOL(acpi_os_signal_semaphore);
950
951#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -0400952u32 acpi_os_get_line(char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954
955#ifdef ENABLE_DEBUGGER
956 if (acpi_in_debugger) {
957 u32 chars;
958
959 kdb_read(buffer, sizeof(line_buf));
960
961 /* remove the CR kdb includes */
962 chars = strlen(buffer) - 1;
963 buffer[chars] = '\0';
964 }
965#endif
966
967 return 0;
968}
Len Brown4be44fc2005-08-05 00:44:28 -0400969#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Len Brown4be44fc2005-08-05 00:44:28 -0400971acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Len Brown4be44fc2005-08-05 00:44:28 -0400973 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 case ACPI_SIGNAL_FATAL:
975 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
976 break;
977 case ACPI_SIGNAL_BREAKPOINT:
978 /*
979 * AML Breakpoint
980 * ACPI spec. says to treat it as a NOP unless
981 * you are debugging. So if/when we integrate
982 * AML debugger into the kernel debugger its
983 * hook will go here. But until then it is
984 * not useful to print anything on breakpoints.
985 */
986 break;
987 default:
988 break;
989 }
990
991 return AE_OK;
992}
Len Brown4be44fc2005-08-05 00:44:28 -0400993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994EXPORT_SYMBOL(acpi_os_signal);
995
Len Brown4be44fc2005-08-05 00:44:28 -0400996static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -0400999 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 if (!str || !*str)
1002 return 0;
1003
1004 for (; count-- && str && *str; str++) {
1005 if (isalnum(*str) || *str == ' ' || *str == ':')
1006 *p++ = *str;
1007 else if (*str == '\'' || *str == '"')
1008 continue;
1009 else
1010 break;
1011 }
1012 *p = 0;
1013
1014 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
1018__setup("acpi_os_name=", acpi_os_name_setup);
1019
Len Brownd4b7dc42008-01-23 20:50:56 -05001020static void __init set_osi_linux(unsigned int enable)
1021{
1022 if (osi_linux.enable != enable) {
1023 osi_linux.enable = enable;
1024 printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
1025 enable ? "Add": "Delet");
1026 }
1027 return;
1028}
Len Brownf5076542007-05-30 00:10:38 -04001029
Len Brownd4b7dc42008-01-23 20:50:56 -05001030static void __init acpi_cmdline_osi_linux(unsigned int enable)
1031{
1032 osi_linux.cmdline = 1; /* cmdline set the default */
1033 set_osi_linux(enable);
Len Brownf5076542007-05-30 00:10:38 -04001034
Len Brownd4b7dc42008-01-23 20:50:56 -05001035 return;
1036}
1037
1038void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1039{
1040 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
1041
1042 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1043
1044 if (enable == -1)
1045 return;
1046
1047 osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
1048
1049 set_osi_linux(enable);
1050
Len Brownf5076542007-05-30 00:10:38 -04001051 return;
1052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/*
Len Brownae00d812007-05-29 18:43:33 -04001055 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1056 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -04001058 * string starting with '!' disables that string
1059 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 */
Len Brown4be44fc2005-08-05 00:44:28 -04001061static int __init acpi_osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 if (str == NULL || *str == '\0') {
1064 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1065 acpi_gbl_create_osi_method = FALSE;
Len Brownaa2e09d2007-07-02 23:57:45 -04001066 } else if (!strcmp("!Linux", str)) {
Len Brownd4b7dc42008-01-23 20:50:56 -05001067 acpi_cmdline_osi_linux(0); /* !enable */
Len Brownae00d812007-05-29 18:43:33 -04001068 } else if (*str == '!') {
1069 if (acpi_osi_invalidate(++str) == AE_OK)
1070 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
Len Brownf5076542007-05-30 00:10:38 -04001071 } else if (!strcmp("Linux", str)) {
Len Brownd4b7dc42008-01-23 20:50:56 -05001072 acpi_cmdline_osi_linux(1); /* enable */
Len Brownae00d812007-05-29 18:43:33 -04001073 } else if (*osi_additional_string == '\0') {
1074 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
1075 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
1078 return 1;
1079}
1080
1081__setup("acpi_osi=", acpi_osi_setup);
1082
1083/* enable serialization to combat AE_ALREADY_EXISTS errors */
Len Brown4be44fc2005-08-05 00:44:28 -04001084static int __init acpi_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
1086 printk(KERN_INFO PREFIX "serialize enabled\n");
1087
1088 acpi_gbl_all_methods_serialized = TRUE;
1089
1090 return 1;
1091}
1092
1093__setup("acpi_serialize", acpi_serialize_setup);
1094
1095/*
1096 * Wake and Run-Time GPES are expected to be separate.
1097 * We disable wake-GPEs at run-time to prevent spurious
1098 * interrupts.
1099 *
1100 * However, if a system exists that shares Wake and
1101 * Run-time events on the same GPE this flag is available
1102 * to tell Linux to keep the wake-time GPEs enabled at run-time.
1103 */
Len Brown4be44fc2005-08-05 00:44:28 -04001104static int __init acpi_wake_gpes_always_on_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
1107
1108 acpi_gbl_leave_wake_gpes_disabled = FALSE;
1109
1110 return 1;
1111}
1112
1113__setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115/*
Robert Moore73459f72005-06-24 00:00:00 -04001116 * Acquire a spinlock.
1117 *
1118 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001119 */
1120
Bob Moore967440e32006-06-23 17:04:00 -04001121acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001122{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001123 acpi_cpu_flags flags;
Bob Moore967440e32006-06-23 17:04:00 -04001124 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001125 return flags;
1126}
1127
1128/*
1129 * Release a spinlock. See above.
1130 */
1131
Bob Moore967440e32006-06-23 17:04:00 -04001132void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001133{
Bob Moore967440e32006-06-23 17:04:00 -04001134 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001135}
1136
Robert Moore73459f72005-06-24 00:00:00 -04001137#ifndef ACPI_USE_LOCAL_CACHE
1138
1139/*******************************************************************************
1140 *
1141 * FUNCTION: acpi_os_create_cache
1142 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001143 * PARAMETERS: name - Ascii name for the cache
1144 * size - Size of each cached object
1145 * depth - Maximum depth of the cache (in objects) <ignored>
1146 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001147 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001148 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001149 *
1150 * DESCRIPTION: Create a cache object
1151 *
1152 ******************************************************************************/
1153
1154acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001155acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001156{
Paul Mundt20c2df82007-07-20 10:11:58 +09001157 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001158 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001159 return AE_ERROR;
1160 else
1161 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001162}
1163
1164/*******************************************************************************
1165 *
1166 * FUNCTION: acpi_os_purge_cache
1167 *
1168 * PARAMETERS: Cache - Handle to cache object
1169 *
1170 * RETURN: Status
1171 *
1172 * DESCRIPTION: Free all objects within the requested cache.
1173 *
1174 ******************************************************************************/
1175
Len Brown4be44fc2005-08-05 00:44:28 -04001176acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001177{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001178 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001179 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001180}
1181
1182/*******************************************************************************
1183 *
1184 * FUNCTION: acpi_os_delete_cache
1185 *
1186 * PARAMETERS: Cache - Handle to cache object
1187 *
1188 * RETURN: Status
1189 *
1190 * DESCRIPTION: Free all objects within the requested cache and delete the
1191 * cache object.
1192 *
1193 ******************************************************************************/
1194
Len Brown4be44fc2005-08-05 00:44:28 -04001195acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001196{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001197 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001198 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001199}
1200
1201/*******************************************************************************
1202 *
1203 * FUNCTION: acpi_os_release_object
1204 *
1205 * PARAMETERS: Cache - Handle to cache object
1206 * Object - The object to be released
1207 *
1208 * RETURN: None
1209 *
1210 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1211 * the object is deleted.
1212 *
1213 ******************************************************************************/
1214
Len Brown4be44fc2005-08-05 00:44:28 -04001215acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001216{
Len Brown4be44fc2005-08-05 00:44:28 -04001217 kmem_cache_free(cache, object);
1218 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001219}
1220
Len Brown5a4e1432008-01-23 20:01:22 -05001221/**
1222 * acpi_dmi_dump - dump DMI slots needed for blacklist entry
1223 *
1224 * Returns 0 on success
1225 */
1226int acpi_dmi_dump(void)
1227{
1228
1229 if (!dmi_available)
1230 return -1;
1231
1232 printk(KERN_NOTICE PREFIX "DMI System Vendor: %s\n",
1233 dmi_get_slot(DMI_SYS_VENDOR));
1234 printk(KERN_NOTICE PREFIX "DMI Product Name: %s\n",
1235 dmi_get_slot(DMI_PRODUCT_NAME));
1236 printk(KERN_NOTICE PREFIX "DMI Product Version: %s\n",
1237 dmi_get_slot(DMI_PRODUCT_VERSION));
1238 printk(KERN_NOTICE PREFIX "DMI Board Name: %s\n",
1239 dmi_get_slot(DMI_BOARD_NAME));
1240 printk(KERN_NOTICE PREFIX "DMI BIOS Vendor: %s\n",
1241 dmi_get_slot(DMI_BIOS_VENDOR));
1242 printk(KERN_NOTICE PREFIX "DMI BIOS Date: %s\n",
1243 dmi_get_slot(DMI_BIOS_DATE));
1244
1245 return 0;
1246}
1247
1248
Bob Mooreb229cf92006-04-21 17:15:00 -04001249/******************************************************************************
1250 *
1251 * FUNCTION: acpi_os_validate_interface
1252 *
1253 * PARAMETERS: interface - Requested interface to be validated
1254 *
1255 * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
1256 *
1257 * DESCRIPTION: Match an interface string to the interfaces supported by the
1258 * host. Strings originate from an AML call to the _OSI method.
1259 *
1260 *****************************************************************************/
1261
1262acpi_status
1263acpi_os_validate_interface (char *interface)
1264{
Len Brownae00d812007-05-29 18:43:33 -04001265 if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1266 return AE_OK;
Len Brownf5076542007-05-30 00:10:38 -04001267 if (!strcmp("Linux", interface)) {
Len Brownd4b7dc42008-01-23 20:50:56 -05001268
1269 printk(KERN_NOTICE PREFIX
1270 "BIOS _OSI(Linux) query %s%s\n",
1271 osi_linux.enable ? "honored" : "ignored",
1272 osi_linux.cmdline ? " via cmdline" :
1273 osi_linux.dmi ? " via DMI" : "");
1274
1275 if (!osi_linux.dmi) {
1276 if (acpi_dmi_dump())
1277 printk(KERN_NOTICE PREFIX
1278 "[please extract dmidecode output]\n");
Len Brownf40cd6f2008-01-23 20:04:28 -05001279 printk(KERN_NOTICE PREFIX
Len Brownd4b7dc42008-01-23 20:50:56 -05001280 "Please send DMI info above to "
1281 "linux-acpi@vger.kernel.org\n");
1282 }
1283 if (!osi_linux.known && !osi_linux.cmdline) {
1284 printk(KERN_NOTICE PREFIX
1285 "If \"acpi_osi=%sLinux\" works better, "
1286 "please notify linux-acpi@vger.kernel.org\n",
1287 osi_linux.enable ? "!" : "");
1288 }
1289
1290 if (osi_linux.enable)
Len Brownf5076542007-05-30 00:10:38 -04001291 return AE_OK;
1292 }
Len Brownae00d812007-05-29 18:43:33 -04001293 return AE_SUPPORT;
Bob Mooreb229cf92006-04-21 17:15:00 -04001294}
1295
Bob Mooreb229cf92006-04-21 17:15:00 -04001296/******************************************************************************
1297 *
1298 * FUNCTION: acpi_os_validate_address
1299 *
1300 * PARAMETERS: space_id - ACPI space ID
1301 * address - Physical address
1302 * length - Address length
1303 *
1304 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1305 * should return AE_AML_ILLEGAL_ADDRESS.
1306 *
1307 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1308 * the addresses accessed by AML operation regions.
1309 *
1310 *****************************************************************************/
1311
1312acpi_status
1313acpi_os_validate_address (
1314 u8 space_id,
1315 acpi_physical_address address,
1316 acpi_size length)
1317{
1318
1319 return AE_OK;
1320}
1321
Robert Moore73459f72005-06-24 00:00:00 -04001322#endif