blob: 58ceb18ec997f7c16b62c1bba6035411a61450ab [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 Brownf5076542007-05-30 00:10:38 -040080#define OSI_LINUX_ENABLED
81#ifdef OSI_LINUX_ENABLED
82int osi_linux = 1; /* enable _OSI(Linux) by default */
83#else
84int osi_linux; /* disable _OSI(Linux) by default */
85#endif
86
87
88#ifdef CONFIG_DMI
Len Browndd272b52007-05-30 00:26:11 -040089static struct __initdata dmi_system_id acpi_osl_dmi_table[];
Len Brownf5076542007-05-30 00:10:38 -040090#endif
91
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -070092static void __init acpi_request_region (struct acpi_generic_address *addr,
93 unsigned int length, char *desc)
94{
95 struct resource *res;
96
97 if (!addr->address || !length)
98 return;
99
Len Browneee3c852007-02-03 01:38:16 -0500100 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700101 res = request_region(addr->address, length, desc);
Len Browneee3c852007-02-03 01:38:16 -0500102 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700103 res = request_mem_region(addr->address, length, desc);
104}
105
106static int __init acpi_reserve_resources(void)
107{
Len Browneee3c852007-02-03 01:38:16 -0500108 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700109 "ACPI PM1a_EVT_BLK");
110
Len Browneee3c852007-02-03 01:38:16 -0500111 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700112 "ACPI PM1b_EVT_BLK");
113
Len Browneee3c852007-02-03 01:38:16 -0500114 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700115 "ACPI PM1a_CNT_BLK");
116
Len Browneee3c852007-02-03 01:38:16 -0500117 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700118 "ACPI PM1b_CNT_BLK");
119
Len Browneee3c852007-02-03 01:38:16 -0500120 if (acpi_gbl_FADT.pm_timer_length == 4)
121 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700122
Len Browneee3c852007-02-03 01:38:16 -0500123 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700124 "ACPI PM2_CNT_BLK");
125
126 /* Length of GPE blocks must be a non-negative multiple of 2 */
127
Len Browneee3c852007-02-03 01:38:16 -0500128 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
129 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
130 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700131
Len Browneee3c852007-02-03 01:38:16 -0500132 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
133 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
134 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700135
136 return 0;
137}
138device_initcall(acpi_reserve_resources);
139
Len Browndd272b52007-05-30 00:26:11 -0400140acpi_status __init acpi_os_initialize(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Len Brownf5076542007-05-30 00:10:38 -0400142 dmi_check_system(acpi_osl_dmi_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return AE_OK;
144}
145
Len Brown4be44fc2005-08-05 00:44:28 -0400146acpi_status acpi_os_initialize1(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 /*
149 * Initialize PCI configuration space access, as we'll need to access
150 * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
151 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (!raw_pci_ops) {
Len Brown4be44fc2005-08-05 00:44:28 -0400153 printk(KERN_ERR PREFIX
154 "Access to PCI configuration space unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return AE_NULL_ENTRY;
156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 kacpid_wq = create_singlethread_workqueue("kacpid");
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400158 kacpi_notify_wq = create_singlethread_workqueue("kacpi_notify");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 BUG_ON(!kacpid_wq);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400160 BUG_ON(!kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return AE_OK;
162}
163
Len Brown4be44fc2005-08-05 00:44:28 -0400164acpi_status acpi_os_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 if (acpi_irq_handler) {
167 acpi_os_remove_interrupt_handler(acpi_irq_irq,
168 acpi_irq_handler);
169 }
170
171 destroy_workqueue(kacpid_wq);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400172 destroy_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 return AE_OK;
175}
176
Len Brown4be44fc2005-08-05 00:44:28 -0400177void acpi_os_printf(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 va_list args;
180 va_start(args, fmt);
181 acpi_os_vprintf(fmt, args);
182 va_end(args);
183}
Len Brown4be44fc2005-08-05 00:44:28 -0400184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185EXPORT_SYMBOL(acpi_os_printf);
186
Len Brown4be44fc2005-08-05 00:44:28 -0400187void acpi_os_vprintf(const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 static char buffer[512];
Len Brown4be44fc2005-08-05 00:44:28 -0400190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 vsprintf(buffer, fmt, args);
192
193#ifdef ENABLE_DEBUGGER
194 if (acpi_in_debugger) {
195 kdb_printf("%s", buffer);
196 } else {
197 printk("%s", buffer);
198 }
199#else
200 printk("%s", buffer);
201#endif
202}
203
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300204acpi_physical_address __init acpi_os_get_root_pointer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 if (efi_enabled) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800207 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300208 return efi.acpi20;
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800209 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300210 return efi.acpi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400212 printk(KERN_ERR PREFIX
213 "System description tables not found\n");
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300216 } else
217 return acpi_find_rsdp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300220void __iomem *acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800222 if (phys > ULONG_MAX) {
223 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
Randy Dunlap70c08462007-02-13 16:11:36 -0800224 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300226 if (acpi_gbl_permanent_mmap)
227 /*
228 * ioremap checks to ensure this is in reserved space
229 */
230 return ioremap((unsigned long)phys, size);
231 else
232 return __acpi_map_table((unsigned long)phys, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800234EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Len Brown4be44fc2005-08-05 00:44:28 -0400236void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300238 if (acpi_gbl_permanent_mmap) {
239 iounmap(virt);
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800242EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244#ifdef ACPI_FUTURE_USAGE
245acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400246acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Len Brown4be44fc2005-08-05 00:44:28 -0400248 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return AE_BAD_PARAMETER;
250
251 *phys = virt_to_phys(virt);
252
253 return AE_OK;
254}
255#endif
256
257#define ACPI_MAX_OVERRIDE_LEN 100
258
259static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
260
261acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400262acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
263 acpi_string * new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 if (!init_val || !new_val)
266 return AE_BAD_PARAMETER;
267
268 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400269 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400271 acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 *new_val = acpi_os_name;
273 }
274
275 return AE_OK;
276}
277
278acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400279acpi_os_table_override(struct acpi_table_header * existing_table,
280 struct acpi_table_header ** new_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 if (!existing_table || !new_table)
283 return AE_BAD_PARAMETER;
284
285#ifdef CONFIG_ACPI_CUSTOM_DSDT
286 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400287 *new_table = (struct acpi_table_header *)AmlCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 else
289 *new_table = NULL;
290#else
291 *new_table = NULL;
292#endif
293 return AE_OK;
294}
295
David Howells7d12e782006-10-05 14:55:46 +0100296static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Len Brown4be44fc2005-08-05 00:44:28 -0400298 return (*acpi_irq_handler) (acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400302acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
303 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 unsigned int irq;
306
307 /*
308 * Ignore the GSI from the core, and use the value in our copy of the
309 * FADT. It may not be the same if an interrupt source override exists
310 * for the SCI.
311 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300312 gsi = acpi_gbl_FADT.sci_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
314 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
315 gsi);
316 return AE_OK;
317 }
318
319 acpi_irq_handler = handler;
320 acpi_irq_context = context;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700321 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
323 return AE_NOT_ACQUIRED;
324 }
325 acpi_irq_irq = irq;
326
327 return AE_OK;
328}
329
Len Brown4be44fc2005-08-05 00:44:28 -0400330acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 if (irq) {
333 free_irq(irq, acpi_irq);
334 acpi_irq_handler = NULL;
335 acpi_irq_irq = 0;
336 }
337
338 return AE_OK;
339}
340
341/*
342 * Running in interpreter thread context, safe to sleep
343 */
344
Len Brown4be44fc2005-08-05 00:44:28 -0400345void acpi_os_sleep(acpi_integer ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800347 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
Len Brown4be44fc2005-08-05 00:44:28 -0400349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350EXPORT_SYMBOL(acpi_os_sleep);
351
Len Brown4be44fc2005-08-05 00:44:28 -0400352void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 while (us) {
355 u32 delay = 1000;
356
357 if (delay > us)
358 delay = us;
359 udelay(delay);
360 touch_nmi_watchdog();
361 us -= delay;
362 }
363}
Len Brown4be44fc2005-08-05 00:44:28 -0400364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365EXPORT_SYMBOL(acpi_os_stall);
366
367/*
368 * Support ACPI 3.0 AML Timer operand
369 * Returns 64-bit free-running, monotonically increasing timer
370 * with 100ns granularity
371 */
Len Brown4be44fc2005-08-05 00:44:28 -0400372u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 static u64 t;
375
376#ifdef CONFIG_HPET
377 /* TBD: use HPET if available */
378#endif
379
380#ifdef CONFIG_X86_PM_TIMER
381 /* TBD: default to PM timer if HPET was not available */
382#endif
383 if (!t)
384 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
385
386 return ++t;
387}
388
Len Brown4be44fc2005-08-05 00:44:28 -0400389acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 u32 dummy;
392
393 if (!value)
394 value = &dummy;
395
Len Brown4be44fc2005-08-05 00:44:28 -0400396 switch (width) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400398 *(u8 *) value = inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 break;
400 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400401 *(u16 *) value = inw(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 break;
403 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400404 *(u32 *) value = inl(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 break;
406 default:
407 BUG();
408 }
409
410 return AE_OK;
411}
Len Brown4be44fc2005-08-05 00:44:28 -0400412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413EXPORT_SYMBOL(acpi_os_read_port);
414
Len Brown4be44fc2005-08-05 00:44:28 -0400415acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Len Brown4be44fc2005-08-05 00:44:28 -0400417 switch (width) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 case 8:
419 outb(value, port);
420 break;
421 case 16:
422 outw(value, port);
423 break;
424 case 32:
425 outl(value, port);
426 break;
427 default:
428 BUG();
429 }
430
431 return AE_OK;
432}
Len Brown4be44fc2005-08-05 00:44:28 -0400433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434EXPORT_SYMBOL(acpi_os_write_port);
435
436acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400437acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Len Brown4be44fc2005-08-05 00:44:28 -0400439 u32 dummy;
440 void __iomem *virt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800442 virt_addr = ioremap(phys_addr, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (!value)
444 value = &dummy;
445
446 switch (width) {
447 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400448 *(u8 *) value = readb(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 break;
450 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400451 *(u16 *) value = readw(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
453 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400454 *(u32 *) value = readl(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 break;
456 default:
457 BUG();
458 }
459
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800460 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 return AE_OK;
463}
464
465acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400466acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Len Brown4be44fc2005-08-05 00:44:28 -0400468 void __iomem *virt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800470 virt_addr = ioremap(phys_addr, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 switch (width) {
473 case 8:
474 writeb(value, virt_addr);
475 break;
476 case 16:
477 writew(value, virt_addr);
478 break;
479 case 32:
480 writel(value, virt_addr);
481 break;
482 default:
483 BUG();
484 }
485
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800486 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 return AE_OK;
489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400492acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
493 void *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 int result, size;
496
497 if (!value)
498 return AE_BAD_PARAMETER;
499
500 switch (width) {
501 case 8:
502 size = 1;
503 break;
504 case 16:
505 size = 2;
506 break;
507 case 32:
508 size = 4;
509 break;
510 default:
511 return AE_ERROR;
512 }
513
514 BUG_ON(!raw_pci_ops);
515
516 result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
Len Brown4be44fc2005-08-05 00:44:28 -0400517 PCI_DEVFN(pci_id->device, pci_id->function),
518 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 return (result ? AE_ERROR : AE_OK);
521}
Len Brown4be44fc2005-08-05 00:44:28 -0400522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523EXPORT_SYMBOL(acpi_os_read_pci_configuration);
524
525acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400526acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
527 acpi_integer value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 int result, size;
530
531 switch (width) {
532 case 8:
533 size = 1;
534 break;
535 case 16:
536 size = 2;
537 break;
538 case 32:
539 size = 4;
540 break;
541 default:
542 return AE_ERROR;
543 }
544
545 BUG_ON(!raw_pci_ops);
546
547 result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
Len Brown4be44fc2005-08-05 00:44:28 -0400548 PCI_DEVFN(pci_id->device, pci_id->function),
549 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 return (result ? AE_ERROR : AE_OK);
552}
553
554/* TODO: Change code to take advantage of driver model more */
Len Brown4be44fc2005-08-05 00:44:28 -0400555static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
556 acpi_handle chandle, /* current node */
557 struct acpi_pci_id **id,
558 int *is_bridge, u8 * bus_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Len Brown4be44fc2005-08-05 00:44:28 -0400560 acpi_handle handle;
561 struct acpi_pci_id *pci_id = *id;
562 acpi_status status;
563 unsigned long temp;
564 acpi_object_type type;
565 u8 tu8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 acpi_get_parent(chandle, &handle);
568 if (handle != rhandle) {
Len Brown4be44fc2005-08-05 00:44:28 -0400569 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
570 bus_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 status = acpi_get_type(handle, &type);
Len Brown4be44fc2005-08-05 00:44:28 -0400573 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return;
575
Len Brown4be44fc2005-08-05 00:44:28 -0400576 status =
577 acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
578 &temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (ACPI_SUCCESS(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400580 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
581 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 if (*is_bridge)
584 pci_id->bus = *bus_number;
585
586 /* any nicer way to get bus number of bridge ? */
Len Brown4be44fc2005-08-05 00:44:28 -0400587 status =
588 acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
589 8);
590 if (ACPI_SUCCESS(status)
591 && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
592 status =
593 acpi_os_read_pci_configuration(pci_id, 0x18,
594 &tu8, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!ACPI_SUCCESS(status)) {
596 /* Certainly broken... FIX ME */
597 return;
598 }
599 *is_bridge = 1;
600 pci_id->bus = tu8;
Len Brown4be44fc2005-08-05 00:44:28 -0400601 status =
602 acpi_os_read_pci_configuration(pci_id, 0x19,
603 &tu8, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (ACPI_SUCCESS(status)) {
605 *bus_number = tu8;
606 }
607 } else
608 *is_bridge = 0;
609 }
610 }
611}
612
Len Brown4be44fc2005-08-05 00:44:28 -0400613void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
614 acpi_handle chandle, /* current node */
615 struct acpi_pci_id **id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 int is_bridge = 1;
618 u8 bus_number = (*id)->bus;
619
620 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
621}
622
David Howells65f27f32006-11-22 14:55:48 +0000623static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
David Howells65f27f32006-11-22 14:55:48 +0000625 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400626 if (!dpc) {
627 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
628 return;
629 }
630
631 dpc->function(dpc->context);
632 kfree(dpc);
633
634 /* Yield cpu to notify thread */
635 cond_resched();
636
637 return;
638}
639
640static void acpi_os_execute_notify(struct work_struct *work)
641{
642 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (!dpc) {
Len Brown64684632006-06-26 23:41:38 -0400645 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400646 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
649 dpc->function(dpc->context);
650
651 kfree(dpc);
652
Patrick Mocheld550d982006-06-27 00:41:40 -0400653 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400656/*******************************************************************************
657 *
658 * FUNCTION: acpi_os_execute
659 *
660 * PARAMETERS: Type - Type of the callback
661 * Function - Function to be executed
662 * Context - Function parameters
663 *
664 * RETURN: Status
665 *
666 * DESCRIPTION: Depending on type, either queues function for deferred execution or
667 * immediately executes function on a separate thread.
668 *
669 ******************************************************************************/
670
671acpi_status acpi_os_execute(acpi_execute_type type,
Len Brown4be44fc2005-08-05 00:44:28 -0400672 acpi_osd_exec_callback function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Len Brown4be44fc2005-08-05 00:44:28 -0400674 acpi_status status = AE_OK;
675 struct acpi_os_dpc *dpc;
Len Brown72945b22006-07-12 22:46:42 -0400676
Len Brown72945b22006-07-12 22:46:42 -0400677 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
678 "Scheduling function [%p(%p)] for deferred execution.\n",
679 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 if (!function)
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400682 return AE_BAD_PARAMETER;
Len Brown72945b22006-07-12 22:46:42 -0400683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /*
685 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +0000686 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 * way that allows us to also free its memory inside the callee.
688 * Because we may want to schedule several tasks with different
689 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +0000690 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
Len Brown72945b22006-07-12 22:46:42 -0400692
David Howells65f27f32006-11-22 14:55:48 +0000693 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (!dpc)
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800695 return_ACPI_STATUS(AE_NO_MEMORY);
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 dpc->function = function;
698 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800699
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400700 if (type == OSL_NOTIFY_HANDLER) {
701 INIT_WORK(&dpc->work, acpi_os_execute_notify);
702 if (!queue_work(kacpi_notify_wq, &dpc->work)) {
703 status = AE_ERROR;
704 kfree(dpc);
705 }
706 } else {
707 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
708 if (!queue_work(kacpid_wq, &dpc->work)) {
709 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800710 "Call to queue_work() failed.\n"));
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400711 status = AE_ERROR;
712 kfree(dpc);
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800715 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
Len Brown4be44fc2005-08-05 00:44:28 -0400717
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400718EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Len Brown4be44fc2005-08-05 00:44:28 -0400720void acpi_os_wait_events_complete(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 flush_workqueue(kacpid_wq);
723}
Len Brown4be44fc2005-08-05 00:44:28 -0400724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725EXPORT_SYMBOL(acpi_os_wait_events_complete);
726
727/*
728 * Allocate the memory for a spinlock and initialize it.
729 */
Bob Moore967440e32006-06-23 17:04:00 -0400730acpi_status acpi_os_create_lock(acpi_spinlock * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Bob Moore967440e32006-06-23 17:04:00 -0400732 spin_lock_init(*handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Patrick Mocheld550d982006-06-27 00:41:40 -0400734 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737/*
738 * Deallocate the memory for a spinlock.
739 */
Bob Moore967440e32006-06-23 17:04:00 -0400740void acpi_os_delete_lock(acpi_spinlock handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Patrick Mocheld550d982006-06-27 00:41:40 -0400742 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400746acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Len Brown4be44fc2005-08-05 00:44:28 -0400748 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 sem = acpi_os_allocate(sizeof(struct semaphore));
752 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400753 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 memset(sem, 0, sizeof(struct semaphore));
755
756 sema_init(sem, initial_units);
757
Len Brown4be44fc2005-08-05 00:44:28 -0400758 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Len Brown4be44fc2005-08-05 00:44:28 -0400760 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
761 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Patrick Mocheld550d982006-06-27 00:41:40 -0400763 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Len Brown4be44fc2005-08-05 00:44:28 -0400766EXPORT_SYMBOL(acpi_os_create_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768/*
769 * TODO: A better way to delete semaphores? Linux doesn't have a
770 * 'delete_semaphore()' function -- may result in an invalid
771 * pointer dereference for non-synchronized consumers. Should
772 * we at least check for blocked threads and signal/cancel them?
773 */
774
Len Brown4be44fc2005-08-05 00:44:28 -0400775acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Len Brown4be44fc2005-08-05 00:44:28 -0400777 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400781 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Len Brown4be44fc2005-08-05 00:44:28 -0400783 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Len Brown02438d82006-06-30 03:19:10 -0400785 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -0400786 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Patrick Mocheld550d982006-06-27 00:41:40 -0400788 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Len Brown4be44fc2005-08-05 00:44:28 -0400791EXPORT_SYMBOL(acpi_os_delete_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793/*
794 * TODO: The kernel doesn't have a 'down_timeout' function -- had to
795 * improvise. The process is to sleep for one scheduler quantum
796 * until the semaphore becomes available. Downside is that this
797 * may result in starvation for timeout-based waits when there's
798 * lots of semaphore activity.
799 *
800 * TODO: Support for units > 1?
801 */
Len Brown4be44fc2005-08-05 00:44:28 -0400802acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Len Brown4be44fc2005-08-05 00:44:28 -0400804 acpi_status status = AE_OK;
805 struct semaphore *sem = (struct semaphore *)handle;
806 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400810 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400813 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Len Brown4be44fc2005-08-05 00:44:28 -0400815 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
816 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Len Brownd68909f2006-08-16 19:16:58 -0400818 /*
819 * This can be called during resume with interrupts off.
820 * Like boot-time, we should be single threaded and will
821 * always get the lock if we try -- timeout or not.
822 * If this doesn't succeed, then we will oops courtesy of
823 * might_sleep() in down().
824 */
825 if (!down_trylock(sem))
826 return AE_OK;
827
Len Brown4be44fc2005-08-05 00:44:28 -0400828 switch (timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /*
830 * No Wait:
831 * --------
832 * A zero timeout value indicates that we shouldn't wait - just
833 * acquire the semaphore if available otherwise return AE_TIME
834 * (a.k.a. 'would block').
835 */
Len Brown4be44fc2005-08-05 00:44:28 -0400836 case 0:
837 if (down_trylock(sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 status = AE_TIME;
839 break;
840
841 /*
842 * Wait Indefinitely:
843 * ------------------
844 */
Len Brown4be44fc2005-08-05 00:44:28 -0400845 case ACPI_WAIT_FOREVER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 down(sem);
847 break;
848
849 /*
850 * Wait w/ Timeout:
851 * ----------------
852 */
Len Brown4be44fc2005-08-05 00:44:28 -0400853 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 // TODO: A better timeout algorithm?
855 {
856 int i = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400857 static const int quantum_ms = 1000 / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 ret = down_trylock(sem);
Yu Lumingdacd9b82005-12-31 01:45:00 -0500860 for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800861 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 ret = down_trylock(sem);
863 }
Len Brown4be44fc2005-08-05 00:44:28 -0400864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (ret != 0)
866 status = AE_TIME;
867 }
868 break;
869 }
870
871 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -0400872 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400873 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -0400874 handle, units, timeout,
875 acpi_format_exception(status)));
876 } else {
877 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400878 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400879 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881
Patrick Mocheld550d982006-06-27 00:41:40 -0400882 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Len Brown4be44fc2005-08-05 00:44:28 -0400885EXPORT_SYMBOL(acpi_os_wait_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887/*
888 * TODO: Support for units > 1?
889 */
Len Brown4be44fc2005-08-05 00:44:28 -0400890acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Len Brown4be44fc2005-08-05 00:44:28 -0400892 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400896 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400899 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Len Brown4be44fc2005-08-05 00:44:28 -0400901 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
902 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 up(sem);
905
Patrick Mocheld550d982006-06-27 00:41:40 -0400906 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
Len Brown4be44fc2005-08-05 00:44:28 -0400908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909EXPORT_SYMBOL(acpi_os_signal_semaphore);
910
911#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -0400912u32 acpi_os_get_line(char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914
915#ifdef ENABLE_DEBUGGER
916 if (acpi_in_debugger) {
917 u32 chars;
918
919 kdb_read(buffer, sizeof(line_buf));
920
921 /* remove the CR kdb includes */
922 chars = strlen(buffer) - 1;
923 buffer[chars] = '\0';
924 }
925#endif
926
927 return 0;
928}
Len Brown4be44fc2005-08-05 00:44:28 -0400929#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Len Brown4be44fc2005-08-05 00:44:28 -0400931acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Len Brown4be44fc2005-08-05 00:44:28 -0400933 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 case ACPI_SIGNAL_FATAL:
935 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
936 break;
937 case ACPI_SIGNAL_BREAKPOINT:
938 /*
939 * AML Breakpoint
940 * ACPI spec. says to treat it as a NOP unless
941 * you are debugging. So if/when we integrate
942 * AML debugger into the kernel debugger its
943 * hook will go here. But until then it is
944 * not useful to print anything on breakpoints.
945 */
946 break;
947 default:
948 break;
949 }
950
951 return AE_OK;
952}
Len Brown4be44fc2005-08-05 00:44:28 -0400953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954EXPORT_SYMBOL(acpi_os_signal);
955
Len Brown4be44fc2005-08-05 00:44:28 -0400956static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -0400959 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 if (!str || !*str)
962 return 0;
963
964 for (; count-- && str && *str; str++) {
965 if (isalnum(*str) || *str == ' ' || *str == ':')
966 *p++ = *str;
967 else if (*str == '\'' || *str == '"')
968 continue;
969 else
970 break;
971 }
972 *p = 0;
973
974 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
978__setup("acpi_os_name=", acpi_os_name_setup);
979
Len Brownf5076542007-05-30 00:10:38 -0400980static void enable_osi_linux(int enable) {
981
982 if (osi_linux != enable)
983 printk(KERN_INFO PREFIX "%sabled _OSI(Linux)\n",
984 enable ? "En": "Dis");
985
986 osi_linux = enable;
987 return;
988}
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990/*
Len Brownae00d812007-05-29 18:43:33 -0400991 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
992 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -0400994 * string starting with '!' disables that string
995 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
Len Brown4be44fc2005-08-05 00:44:28 -0400997static int __init acpi_osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 if (str == NULL || *str == '\0') {
1000 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1001 acpi_gbl_create_osi_method = FALSE;
Len Brownae00d812007-05-29 18:43:33 -04001002 } else if (*str == '!') {
1003 if (acpi_osi_invalidate(++str) == AE_OK)
1004 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
Len Brownf5076542007-05-30 00:10:38 -04001005 } else if (!strcmp("!Linux", str)) {
1006 enable_osi_linux(0);
1007 } else if (!strcmp("Linux", str)) {
1008 enable_osi_linux(1);
Len Brownae00d812007-05-29 18:43:33 -04001009 } else if (*osi_additional_string == '\0') {
1010 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
1011 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013
1014 return 1;
1015}
1016
1017__setup("acpi_osi=", acpi_osi_setup);
1018
1019/* enable serialization to combat AE_ALREADY_EXISTS errors */
Len Brown4be44fc2005-08-05 00:44:28 -04001020static int __init acpi_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 printk(KERN_INFO PREFIX "serialize enabled\n");
1023
1024 acpi_gbl_all_methods_serialized = TRUE;
1025
1026 return 1;
1027}
1028
1029__setup("acpi_serialize", acpi_serialize_setup);
1030
1031/*
1032 * Wake and Run-Time GPES are expected to be separate.
1033 * We disable wake-GPEs at run-time to prevent spurious
1034 * interrupts.
1035 *
1036 * However, if a system exists that shares Wake and
1037 * Run-time events on the same GPE this flag is available
1038 * to tell Linux to keep the wake-time GPEs enabled at run-time.
1039 */
Len Brown4be44fc2005-08-05 00:44:28 -04001040static int __init acpi_wake_gpes_always_on_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
1043
1044 acpi_gbl_leave_wake_gpes_disabled = FALSE;
1045
1046 return 1;
1047}
1048
1049__setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/*
1052 * max_cstate is defined in the base kernel so modules can
1053 * change it w/o depending on the state of the processor module.
1054 */
1055unsigned int max_cstate = ACPI_PROCESSOR_MAX_POWER;
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057EXPORT_SYMBOL(max_cstate);
Robert Moore73459f72005-06-24 00:00:00 -04001058
1059/*
1060 * Acquire a spinlock.
1061 *
1062 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001063 */
1064
Bob Moore967440e32006-06-23 17:04:00 -04001065acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001066{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001067 acpi_cpu_flags flags;
Bob Moore967440e32006-06-23 17:04:00 -04001068 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001069 return flags;
1070}
1071
1072/*
1073 * Release a spinlock. See above.
1074 */
1075
Bob Moore967440e32006-06-23 17:04:00 -04001076void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001077{
Bob Moore967440e32006-06-23 17:04:00 -04001078 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001079}
1080
Robert Moore73459f72005-06-24 00:00:00 -04001081#ifndef ACPI_USE_LOCAL_CACHE
1082
1083/*******************************************************************************
1084 *
1085 * FUNCTION: acpi_os_create_cache
1086 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001087 * PARAMETERS: name - Ascii name for the cache
1088 * size - Size of each cached object
1089 * depth - Maximum depth of the cache (in objects) <ignored>
1090 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001091 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001092 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001093 *
1094 * DESCRIPTION: Create a cache object
1095 *
1096 ******************************************************************************/
1097
1098acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001099acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001100{
Len Brown4be44fc2005-08-05 00:44:28 -04001101 *cache = kmem_cache_create(name, size, 0, 0, NULL, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001102 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001103 return AE_ERROR;
1104 else
1105 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001106}
1107
1108/*******************************************************************************
1109 *
1110 * FUNCTION: acpi_os_purge_cache
1111 *
1112 * PARAMETERS: Cache - Handle to cache object
1113 *
1114 * RETURN: Status
1115 *
1116 * DESCRIPTION: Free all objects within the requested cache.
1117 *
1118 ******************************************************************************/
1119
Len Brown4be44fc2005-08-05 00:44:28 -04001120acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001121{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001122 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001123 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001124}
1125
1126/*******************************************************************************
1127 *
1128 * FUNCTION: acpi_os_delete_cache
1129 *
1130 * PARAMETERS: Cache - Handle to cache object
1131 *
1132 * RETURN: Status
1133 *
1134 * DESCRIPTION: Free all objects within the requested cache and delete the
1135 * cache object.
1136 *
1137 ******************************************************************************/
1138
Len Brown4be44fc2005-08-05 00:44:28 -04001139acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001140{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001141 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001142 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001143}
1144
1145/*******************************************************************************
1146 *
1147 * FUNCTION: acpi_os_release_object
1148 *
1149 * PARAMETERS: Cache - Handle to cache object
1150 * Object - The object to be released
1151 *
1152 * RETURN: None
1153 *
1154 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1155 * the object is deleted.
1156 *
1157 ******************************************************************************/
1158
Len Brown4be44fc2005-08-05 00:44:28 -04001159acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001160{
Len Brown4be44fc2005-08-05 00:44:28 -04001161 kmem_cache_free(cache, object);
1162 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001163}
1164
Bob Mooreb229cf92006-04-21 17:15:00 -04001165/******************************************************************************
1166 *
1167 * FUNCTION: acpi_os_validate_interface
1168 *
1169 * PARAMETERS: interface - Requested interface to be validated
1170 *
1171 * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
1172 *
1173 * DESCRIPTION: Match an interface string to the interfaces supported by the
1174 * host. Strings originate from an AML call to the _OSI method.
1175 *
1176 *****************************************************************************/
1177
1178acpi_status
1179acpi_os_validate_interface (char *interface)
1180{
Len Brownae00d812007-05-29 18:43:33 -04001181 if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1182 return AE_OK;
Len Brownf5076542007-05-30 00:10:38 -04001183 if (!strcmp("Linux", interface)) {
1184 printk(KERN_WARNING PREFIX
1185 "System BIOS is requesting _OSI(Linux)\n");
1186#ifdef OSI_LINUX_ENABLED
1187 printk(KERN_WARNING PREFIX
1188 "Please test with \"acpi_osi=!Linux\"\n"
1189 "Please send dmidecode "
1190 "to linux-acpi@vger.kernel.org\n");
1191#else
1192 printk(KERN_WARNING PREFIX
1193 "If \"acpi_osi=Linux\" works better,\n"
1194 "Please send dmidecode "
1195 "to linux-acpi@vger.kernel.org\n");
1196#endif
1197 if(osi_linux)
1198 return AE_OK;
1199 }
Len Brownae00d812007-05-29 18:43:33 -04001200 return AE_SUPPORT;
Bob Mooreb229cf92006-04-21 17:15:00 -04001201}
1202
Bob Mooreb229cf92006-04-21 17:15:00 -04001203/******************************************************************************
1204 *
1205 * FUNCTION: acpi_os_validate_address
1206 *
1207 * PARAMETERS: space_id - ACPI space ID
1208 * address - Physical address
1209 * length - Address length
1210 *
1211 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1212 * should return AE_AML_ILLEGAL_ADDRESS.
1213 *
1214 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1215 * the addresses accessed by AML operation regions.
1216 *
1217 *****************************************************************************/
1218
1219acpi_status
1220acpi_os_validate_address (
1221 u8 space_id,
1222 acpi_physical_address address,
1223 acpi_size length)
1224{
1225
1226 return AE_OK;
1227}
1228
Len Brownf5076542007-05-30 00:10:38 -04001229#ifdef CONFIG_DMI
1230#ifdef OSI_LINUX_ENABLED
1231static int dmi_osi_not_linux(struct dmi_system_id *d)
1232{
1233 printk(KERN_NOTICE "%s detected: requires not _OSI(Linux)\n", d->ident);
1234 enable_osi_linux(0);
1235 return 0;
1236}
1237#else
1238static int dmi_osi_linux(struct dmi_system_id *d)
1239{
1240 printk(KERN_NOTICE "%s detected: requires _OSI(Linux)\n", d->ident);
1241 enable_osi_linux(1);
1242 return 0;
1243}
1244#endif
1245
Len Browndd272b52007-05-30 00:26:11 -04001246static struct dmi_system_id acpi_osl_dmi_table[] __initdata = {
Len Brownf5076542007-05-30 00:10:38 -04001247#ifdef OSI_LINUX_ENABLED
1248 /*
1249 * Boxes that need NOT _OSI(Linux)
1250 */
1251 {
1252 .callback = dmi_osi_not_linux,
1253 .ident = "Toshiba Satellite P100",
1254 .matches = {
1255 DMI_MATCH(DMI_BOARD_VENDOR, "TOSHIBA"),
1256 DMI_MATCH(DMI_BOARD_NAME, "Satellite P100"),
1257 },
1258 },
1259#else
1260 /*
1261 * Boxes that need _OSI(Linux)
1262 */
1263 {
1264 .callback = dmi_osi_linux,
1265 .ident = "Intel Napa CRB",
1266 .matches = {
1267 DMI_MATCH(DMI_BOARD_VENDOR, "Intel Corporation"),
1268 DMI_MATCH(DMI_BOARD_NAME, "MPAD-MSAE Customer Reference Boards"),
1269 },
1270 },
1271#endif
1272 {}
1273};
1274#endif /* CONFIG_DMI */
Bob Mooreb229cf92006-04-21 17:15:00 -04001275
Robert Moore73459f72005-06-24 00:00:00 -04001276#endif