blob: 101691ef66cb0231e0265414762106d869aecc36 [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
Adrian Bunk33121112007-07-03 00:56:05 -040080static int osi_linux; /* disable _OSI(Linux) by default */
Len Brownf5076542007-05-30 00:10:38 -040081
82#ifdef CONFIG_DMI
Len Browndd272b52007-05-30 00:26:11 -040083static struct __initdata dmi_system_id acpi_osl_dmi_table[];
Len Brownf5076542007-05-30 00:10:38 -040084#endif
85
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -070086static void __init acpi_request_region (struct acpi_generic_address *addr,
87 unsigned int length, char *desc)
88{
89 struct resource *res;
90
91 if (!addr->address || !length)
92 return;
93
Len Browneee3c852007-02-03 01:38:16 -050094 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -070095 res = request_region(addr->address, length, desc);
Len Browneee3c852007-02-03 01:38:16 -050096 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -070097 res = request_mem_region(addr->address, length, desc);
98}
99
100static int __init acpi_reserve_resources(void)
101{
Len Browneee3c852007-02-03 01:38:16 -0500102 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700103 "ACPI PM1a_EVT_BLK");
104
Len Browneee3c852007-02-03 01:38:16 -0500105 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700106 "ACPI PM1b_EVT_BLK");
107
Len Browneee3c852007-02-03 01:38:16 -0500108 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700109 "ACPI PM1a_CNT_BLK");
110
Len Browneee3c852007-02-03 01:38:16 -0500111 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700112 "ACPI PM1b_CNT_BLK");
113
Len Browneee3c852007-02-03 01:38:16 -0500114 if (acpi_gbl_FADT.pm_timer_length == 4)
115 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700116
Len Browneee3c852007-02-03 01:38:16 -0500117 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700118 "ACPI PM2_CNT_BLK");
119
120 /* Length of GPE blocks must be a non-negative multiple of 2 */
121
Len Browneee3c852007-02-03 01:38:16 -0500122 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
123 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
124 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700125
Len Browneee3c852007-02-03 01:38:16 -0500126 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
127 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
128 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700129
130 return 0;
131}
132device_initcall(acpi_reserve_resources);
133
Len Browndd272b52007-05-30 00:26:11 -0400134acpi_status __init acpi_os_initialize(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Len Brownf5076542007-05-30 00:10:38 -0400136 dmi_check_system(acpi_osl_dmi_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return AE_OK;
138}
139
Len Brown4be44fc2005-08-05 00:44:28 -0400140acpi_status acpi_os_initialize1(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 /*
143 * Initialize PCI configuration space access, as we'll need to access
144 * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (!raw_pci_ops) {
Len Brown4be44fc2005-08-05 00:44:28 -0400147 printk(KERN_ERR PREFIX
148 "Access to PCI configuration space unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return AE_NULL_ENTRY;
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 kacpid_wq = create_singlethread_workqueue("kacpid");
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400152 kacpi_notify_wq = create_singlethread_workqueue("kacpi_notify");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 BUG_ON(!kacpid_wq);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400154 BUG_ON(!kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return AE_OK;
156}
157
Len Brown4be44fc2005-08-05 00:44:28 -0400158acpi_status acpi_os_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 if (acpi_irq_handler) {
161 acpi_os_remove_interrupt_handler(acpi_irq_irq,
162 acpi_irq_handler);
163 }
164
165 destroy_workqueue(kacpid_wq);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400166 destroy_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 return AE_OK;
169}
170
Len Brown4be44fc2005-08-05 00:44:28 -0400171void acpi_os_printf(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 va_list args;
174 va_start(args, fmt);
175 acpi_os_vprintf(fmt, args);
176 va_end(args);
177}
Len Brown4be44fc2005-08-05 00:44:28 -0400178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179EXPORT_SYMBOL(acpi_os_printf);
180
Len Brown4be44fc2005-08-05 00:44:28 -0400181void acpi_os_vprintf(const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 static char buffer[512];
Len Brown4be44fc2005-08-05 00:44:28 -0400184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 vsprintf(buffer, fmt, args);
186
187#ifdef ENABLE_DEBUGGER
188 if (acpi_in_debugger) {
189 kdb_printf("%s", buffer);
190 } else {
191 printk("%s", buffer);
192 }
193#else
194 printk("%s", buffer);
195#endif
196}
197
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300198acpi_physical_address __init acpi_os_get_root_pointer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 if (efi_enabled) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800201 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300202 return efi.acpi20;
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800203 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300204 return efi.acpi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400206 printk(KERN_ERR PREFIX
207 "System description tables not found\n");
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Len Brown239665a2007-11-23 20:08:02 -0500210 } else {
211 acpi_physical_address pa = 0;
212
213 acpi_find_root_pointer(&pa);
214 return pa;
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300218void __iomem *acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800220 if (phys > ULONG_MAX) {
221 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
Randy Dunlap70c08462007-02-13 16:11:36 -0800222 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300224 if (acpi_gbl_permanent_mmap)
225 /*
226 * ioremap checks to ensure this is in reserved space
227 */
228 return ioremap((unsigned long)phys, size);
229 else
230 return __acpi_map_table((unsigned long)phys, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800232EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Len Brown4be44fc2005-08-05 00:44:28 -0400234void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300236 if (acpi_gbl_permanent_mmap) {
237 iounmap(virt);
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800240EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242#ifdef ACPI_FUTURE_USAGE
243acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400244acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Len Brown4be44fc2005-08-05 00:44:28 -0400246 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return AE_BAD_PARAMETER;
248
249 *phys = virt_to_phys(virt);
250
251 return AE_OK;
252}
253#endif
254
255#define ACPI_MAX_OVERRIDE_LEN 100
256
257static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
258
259acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400260acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
261 acpi_string * new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 if (!init_val || !new_val)
264 return AE_BAD_PARAMETER;
265
266 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400267 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400269 acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *new_val = acpi_os_name;
271 }
272
273 return AE_OK;
274}
275
276acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400277acpi_os_table_override(struct acpi_table_header * existing_table,
278 struct acpi_table_header ** new_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 if (!existing_table || !new_table)
281 return AE_BAD_PARAMETER;
282
283#ifdef CONFIG_ACPI_CUSTOM_DSDT
284 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400285 *new_table = (struct acpi_table_header *)AmlCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 else
287 *new_table = NULL;
288#else
289 *new_table = NULL;
290#endif
291 return AE_OK;
292}
293
David Howells7d12e782006-10-05 14:55:46 +0100294static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Len Brown4be44fc2005-08-05 00:44:28 -0400296 return (*acpi_irq_handler) (acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400300acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
301 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 unsigned int irq;
304
305 /*
306 * Ignore the GSI from the core, and use the value in our copy of the
307 * FADT. It may not be the same if an interrupt source override exists
308 * for the SCI.
309 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300310 gsi = acpi_gbl_FADT.sci_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
312 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
313 gsi);
314 return AE_OK;
315 }
316
317 acpi_irq_handler = handler;
318 acpi_irq_context = context;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700319 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
321 return AE_NOT_ACQUIRED;
322 }
323 acpi_irq_irq = irq;
324
325 return AE_OK;
326}
327
Len Brown4be44fc2005-08-05 00:44:28 -0400328acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 if (irq) {
331 free_irq(irq, acpi_irq);
332 acpi_irq_handler = NULL;
333 acpi_irq_irq = 0;
334 }
335
336 return AE_OK;
337}
338
339/*
340 * Running in interpreter thread context, safe to sleep
341 */
342
Len Brown4be44fc2005-08-05 00:44:28 -0400343void acpi_os_sleep(acpi_integer ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800345 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
Len Brown4be44fc2005-08-05 00:44:28 -0400347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348EXPORT_SYMBOL(acpi_os_sleep);
349
Len Brown4be44fc2005-08-05 00:44:28 -0400350void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 while (us) {
353 u32 delay = 1000;
354
355 if (delay > us)
356 delay = us;
357 udelay(delay);
358 touch_nmi_watchdog();
359 us -= delay;
360 }
361}
Len Brown4be44fc2005-08-05 00:44:28 -0400362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363EXPORT_SYMBOL(acpi_os_stall);
364
365/*
366 * Support ACPI 3.0 AML Timer operand
367 * Returns 64-bit free-running, monotonically increasing timer
368 * with 100ns granularity
369 */
Len Brown4be44fc2005-08-05 00:44:28 -0400370u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 static u64 t;
373
374#ifdef CONFIG_HPET
375 /* TBD: use HPET if available */
376#endif
377
378#ifdef CONFIG_X86_PM_TIMER
379 /* TBD: default to PM timer if HPET was not available */
380#endif
381 if (!t)
382 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
383
384 return ++t;
385}
386
Len Brown4be44fc2005-08-05 00:44:28 -0400387acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 u32 dummy;
390
391 if (!value)
392 value = &dummy;
393
Len Brown4be44fc2005-08-05 00:44:28 -0400394 switch (width) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400396 *(u8 *) value = inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 break;
398 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400399 *(u16 *) value = inw(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 break;
401 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400402 *(u32 *) value = inl(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 break;
404 default:
405 BUG();
406 }
407
408 return AE_OK;
409}
Len Brown4be44fc2005-08-05 00:44:28 -0400410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411EXPORT_SYMBOL(acpi_os_read_port);
412
Len Brown4be44fc2005-08-05 00:44:28 -0400413acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Len Brown4be44fc2005-08-05 00:44:28 -0400415 switch (width) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 case 8:
417 outb(value, port);
418 break;
419 case 16:
420 outw(value, port);
421 break;
422 case 32:
423 outl(value, port);
424 break;
425 default:
426 BUG();
427 }
428
429 return AE_OK;
430}
Len Brown4be44fc2005-08-05 00:44:28 -0400431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432EXPORT_SYMBOL(acpi_os_write_port);
433
434acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400435acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Len Brown4be44fc2005-08-05 00:44:28 -0400437 u32 dummy;
438 void __iomem *virt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800440 virt_addr = ioremap(phys_addr, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (!value)
442 value = &dummy;
443
444 switch (width) {
445 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400446 *(u8 *) value = readb(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
448 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400449 *(u16 *) value = readw(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
451 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400452 *(u32 *) value = readl(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
454 default:
455 BUG();
456 }
457
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800458 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 return AE_OK;
461}
462
463acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400464acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Len Brown4be44fc2005-08-05 00:44:28 -0400466 void __iomem *virt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800468 virt_addr = ioremap(phys_addr, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 switch (width) {
471 case 8:
472 writeb(value, virt_addr);
473 break;
474 case 16:
475 writew(value, virt_addr);
476 break;
477 case 32:
478 writel(value, virt_addr);
479 break;
480 default:
481 BUG();
482 }
483
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800484 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 return AE_OK;
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400490acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
491 void *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 int result, size;
494
495 if (!value)
496 return AE_BAD_PARAMETER;
497
498 switch (width) {
499 case 8:
500 size = 1;
501 break;
502 case 16:
503 size = 2;
504 break;
505 case 32:
506 size = 4;
507 break;
508 default:
509 return AE_ERROR;
510 }
511
512 BUG_ON(!raw_pci_ops);
513
514 result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
Len Brown4be44fc2005-08-05 00:44:28 -0400515 PCI_DEVFN(pci_id->device, pci_id->function),
516 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 return (result ? AE_ERROR : AE_OK);
519}
Len Brown4be44fc2005-08-05 00:44:28 -0400520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521EXPORT_SYMBOL(acpi_os_read_pci_configuration);
522
523acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400524acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
525 acpi_integer value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 int result, size;
528
529 switch (width) {
530 case 8:
531 size = 1;
532 break;
533 case 16:
534 size = 2;
535 break;
536 case 32:
537 size = 4;
538 break;
539 default:
540 return AE_ERROR;
541 }
542
543 BUG_ON(!raw_pci_ops);
544
545 result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
Len Brown4be44fc2005-08-05 00:44:28 -0400546 PCI_DEVFN(pci_id->device, pci_id->function),
547 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 return (result ? AE_ERROR : AE_OK);
550}
551
552/* TODO: Change code to take advantage of driver model more */
Len Brown4be44fc2005-08-05 00:44:28 -0400553static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
554 acpi_handle chandle, /* current node */
555 struct acpi_pci_id **id,
556 int *is_bridge, u8 * bus_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Len Brown4be44fc2005-08-05 00:44:28 -0400558 acpi_handle handle;
559 struct acpi_pci_id *pci_id = *id;
560 acpi_status status;
561 unsigned long temp;
562 acpi_object_type type;
563 u8 tu8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 acpi_get_parent(chandle, &handle);
566 if (handle != rhandle) {
Len Brown4be44fc2005-08-05 00:44:28 -0400567 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
568 bus_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 status = acpi_get_type(handle, &type);
Len Brown4be44fc2005-08-05 00:44:28 -0400571 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return;
573
Len Brown4be44fc2005-08-05 00:44:28 -0400574 status =
575 acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
576 &temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (ACPI_SUCCESS(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400578 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
579 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 if (*is_bridge)
582 pci_id->bus = *bus_number;
583
584 /* any nicer way to get bus number of bridge ? */
Len Brown4be44fc2005-08-05 00:44:28 -0400585 status =
586 acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
587 8);
588 if (ACPI_SUCCESS(status)
589 && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
590 status =
591 acpi_os_read_pci_configuration(pci_id, 0x18,
592 &tu8, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!ACPI_SUCCESS(status)) {
594 /* Certainly broken... FIX ME */
595 return;
596 }
597 *is_bridge = 1;
598 pci_id->bus = tu8;
Len Brown4be44fc2005-08-05 00:44:28 -0400599 status =
600 acpi_os_read_pci_configuration(pci_id, 0x19,
601 &tu8, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (ACPI_SUCCESS(status)) {
603 *bus_number = tu8;
604 }
605 } else
606 *is_bridge = 0;
607 }
608 }
609}
610
Len Brown4be44fc2005-08-05 00:44:28 -0400611void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
612 acpi_handle chandle, /* current node */
613 struct acpi_pci_id **id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 int is_bridge = 1;
616 u8 bus_number = (*id)->bus;
617
618 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
619}
620
David Howells65f27f32006-11-22 14:55:48 +0000621static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
David Howells65f27f32006-11-22 14:55:48 +0000623 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400624 if (!dpc) {
625 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
626 return;
627 }
628
629 dpc->function(dpc->context);
630 kfree(dpc);
631
632 /* Yield cpu to notify thread */
633 cond_resched();
634
635 return;
636}
637
638static void acpi_os_execute_notify(struct work_struct *work)
639{
640 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (!dpc) {
Len Brown64684632006-06-26 23:41:38 -0400643 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400644 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
647 dpc->function(dpc->context);
648
649 kfree(dpc);
650
Patrick Mocheld550d982006-06-27 00:41:40 -0400651 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400654/*******************************************************************************
655 *
656 * FUNCTION: acpi_os_execute
657 *
658 * PARAMETERS: Type - Type of the callback
659 * Function - Function to be executed
660 * Context - Function parameters
661 *
662 * RETURN: Status
663 *
664 * DESCRIPTION: Depending on type, either queues function for deferred execution or
665 * immediately executes function on a separate thread.
666 *
667 ******************************************************************************/
668
669acpi_status acpi_os_execute(acpi_execute_type type,
Len Brown4be44fc2005-08-05 00:44:28 -0400670 acpi_osd_exec_callback function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Len Brown4be44fc2005-08-05 00:44:28 -0400672 acpi_status status = AE_OK;
673 struct acpi_os_dpc *dpc;
Len Brown72945b22006-07-12 22:46:42 -0400674
Len Brown72945b22006-07-12 22:46:42 -0400675 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
676 "Scheduling function [%p(%p)] for deferred execution.\n",
677 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 if (!function)
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400680 return AE_BAD_PARAMETER;
Len Brown72945b22006-07-12 22:46:42 -0400681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /*
683 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +0000684 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 * way that allows us to also free its memory inside the callee.
686 * Because we may want to schedule several tasks with different
687 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +0000688 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 */
Len Brown72945b22006-07-12 22:46:42 -0400690
David Howells65f27f32006-11-22 14:55:48 +0000691 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (!dpc)
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800693 return_ACPI_STATUS(AE_NO_MEMORY);
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 dpc->function = function;
696 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800697
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400698 if (type == OSL_NOTIFY_HANDLER) {
699 INIT_WORK(&dpc->work, acpi_os_execute_notify);
700 if (!queue_work(kacpi_notify_wq, &dpc->work)) {
701 status = AE_ERROR;
702 kfree(dpc);
703 }
704 } else {
705 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
706 if (!queue_work(kacpid_wq, &dpc->work)) {
707 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800708 "Call to queue_work() failed.\n"));
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400709 status = AE_ERROR;
710 kfree(dpc);
711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800713 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
Len Brown4be44fc2005-08-05 00:44:28 -0400715
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400716EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Len Brown4be44fc2005-08-05 00:44:28 -0400718void acpi_os_wait_events_complete(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 flush_workqueue(kacpid_wq);
721}
Len Brown4be44fc2005-08-05 00:44:28 -0400722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723EXPORT_SYMBOL(acpi_os_wait_events_complete);
724
725/*
726 * Allocate the memory for a spinlock and initialize it.
727 */
Bob Moore967440e32006-06-23 17:04:00 -0400728acpi_status acpi_os_create_lock(acpi_spinlock * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Bob Moore967440e32006-06-23 17:04:00 -0400730 spin_lock_init(*handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Patrick Mocheld550d982006-06-27 00:41:40 -0400732 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/*
736 * Deallocate the memory for a spinlock.
737 */
Bob Moore967440e32006-06-23 17:04:00 -0400738void acpi_os_delete_lock(acpi_spinlock handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Patrick Mocheld550d982006-06-27 00:41:40 -0400740 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400744acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Len Brown4be44fc2005-08-05 00:44:28 -0400746 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 sem = acpi_os_allocate(sizeof(struct semaphore));
750 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400751 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 memset(sem, 0, sizeof(struct semaphore));
753
754 sema_init(sem, initial_units);
755
Len Brown4be44fc2005-08-05 00:44:28 -0400756 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Len Brown4be44fc2005-08-05 00:44:28 -0400758 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
759 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Patrick Mocheld550d982006-06-27 00:41:40 -0400761 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Len Brown4be44fc2005-08-05 00:44:28 -0400764EXPORT_SYMBOL(acpi_os_create_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766/*
767 * TODO: A better way to delete semaphores? Linux doesn't have a
768 * 'delete_semaphore()' function -- may result in an invalid
769 * pointer dereference for non-synchronized consumers. Should
770 * we at least check for blocked threads and signal/cancel them?
771 */
772
Len Brown4be44fc2005-08-05 00:44:28 -0400773acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Len Brown4be44fc2005-08-05 00:44:28 -0400775 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400779 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Len Brown4be44fc2005-08-05 00:44:28 -0400781 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Len Brown02438d82006-06-30 03:19:10 -0400783 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -0400784 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Patrick Mocheld550d982006-06-27 00:41:40 -0400786 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Len Brown4be44fc2005-08-05 00:44:28 -0400789EXPORT_SYMBOL(acpi_os_delete_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791/*
792 * TODO: The kernel doesn't have a 'down_timeout' function -- had to
793 * improvise. The process is to sleep for one scheduler quantum
794 * until the semaphore becomes available. Downside is that this
795 * may result in starvation for timeout-based waits when there's
796 * lots of semaphore activity.
797 *
798 * TODO: Support for units > 1?
799 */
Len Brown4be44fc2005-08-05 00:44:28 -0400800acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Len Brown4be44fc2005-08-05 00:44:28 -0400802 acpi_status status = AE_OK;
803 struct semaphore *sem = (struct semaphore *)handle;
804 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400808 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400811 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Len Brown4be44fc2005-08-05 00:44:28 -0400813 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
814 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Len Brownd68909f2006-08-16 19:16:58 -0400816 /*
817 * This can be called during resume with interrupts off.
818 * Like boot-time, we should be single threaded and will
819 * always get the lock if we try -- timeout or not.
820 * If this doesn't succeed, then we will oops courtesy of
821 * might_sleep() in down().
822 */
823 if (!down_trylock(sem))
824 return AE_OK;
825
Len Brown4be44fc2005-08-05 00:44:28 -0400826 switch (timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /*
828 * No Wait:
829 * --------
830 * A zero timeout value indicates that we shouldn't wait - just
831 * acquire the semaphore if available otherwise return AE_TIME
832 * (a.k.a. 'would block').
833 */
Len Brown4be44fc2005-08-05 00:44:28 -0400834 case 0:
835 if (down_trylock(sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 status = AE_TIME;
837 break;
838
839 /*
840 * Wait Indefinitely:
841 * ------------------
842 */
Len Brown4be44fc2005-08-05 00:44:28 -0400843 case ACPI_WAIT_FOREVER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 down(sem);
845 break;
846
847 /*
848 * Wait w/ Timeout:
849 * ----------------
850 */
Len Brown4be44fc2005-08-05 00:44:28 -0400851 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 // TODO: A better timeout algorithm?
853 {
854 int i = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400855 static const int quantum_ms = 1000 / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 ret = down_trylock(sem);
Yu Lumingdacd9b82005-12-31 01:45:00 -0500858 for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800859 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 ret = down_trylock(sem);
861 }
Len Brown4be44fc2005-08-05 00:44:28 -0400862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (ret != 0)
864 status = AE_TIME;
865 }
866 break;
867 }
868
869 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -0400870 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400871 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -0400872 handle, units, timeout,
873 acpi_format_exception(status)));
874 } else {
875 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400876 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400877 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
Patrick Mocheld550d982006-06-27 00:41:40 -0400880 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Len Brown4be44fc2005-08-05 00:44:28 -0400883EXPORT_SYMBOL(acpi_os_wait_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885/*
886 * TODO: Support for units > 1?
887 */
Len Brown4be44fc2005-08-05 00:44:28 -0400888acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Len Brown4be44fc2005-08-05 00:44:28 -0400890 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400894 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400897 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Len Brown4be44fc2005-08-05 00:44:28 -0400899 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
900 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 up(sem);
903
Patrick Mocheld550d982006-06-27 00:41:40 -0400904 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
Len Brown4be44fc2005-08-05 00:44:28 -0400906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907EXPORT_SYMBOL(acpi_os_signal_semaphore);
908
909#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -0400910u32 acpi_os_get_line(char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912
913#ifdef ENABLE_DEBUGGER
914 if (acpi_in_debugger) {
915 u32 chars;
916
917 kdb_read(buffer, sizeof(line_buf));
918
919 /* remove the CR kdb includes */
920 chars = strlen(buffer) - 1;
921 buffer[chars] = '\0';
922 }
923#endif
924
925 return 0;
926}
Len Brown4be44fc2005-08-05 00:44:28 -0400927#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Len Brown4be44fc2005-08-05 00:44:28 -0400929acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Len Brown4be44fc2005-08-05 00:44:28 -0400931 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 case ACPI_SIGNAL_FATAL:
933 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
934 break;
935 case ACPI_SIGNAL_BREAKPOINT:
936 /*
937 * AML Breakpoint
938 * ACPI spec. says to treat it as a NOP unless
939 * you are debugging. So if/when we integrate
940 * AML debugger into the kernel debugger its
941 * hook will go here. But until then it is
942 * not useful to print anything on breakpoints.
943 */
944 break;
945 default:
946 break;
947 }
948
949 return AE_OK;
950}
Len Brown4be44fc2005-08-05 00:44:28 -0400951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952EXPORT_SYMBOL(acpi_os_signal);
953
Len Brown4be44fc2005-08-05 00:44:28 -0400954static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -0400957 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 if (!str || !*str)
960 return 0;
961
962 for (; count-- && str && *str; str++) {
963 if (isalnum(*str) || *str == ' ' || *str == ':')
964 *p++ = *str;
965 else if (*str == '\'' || *str == '"')
966 continue;
967 else
968 break;
969 }
970 *p = 0;
971
972 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976__setup("acpi_os_name=", acpi_os_name_setup);
977
Len Brownf5076542007-05-30 00:10:38 -0400978static void enable_osi_linux(int enable) {
979
980 if (osi_linux != enable)
981 printk(KERN_INFO PREFIX "%sabled _OSI(Linux)\n",
982 enable ? "En": "Dis");
983
984 osi_linux = enable;
985 return;
986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988/*
Len Brownae00d812007-05-29 18:43:33 -0400989 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
990 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -0400992 * string starting with '!' disables that string
993 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 */
Len Brown4be44fc2005-08-05 00:44:28 -0400995static int __init acpi_osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 if (str == NULL || *str == '\0') {
998 printk(KERN_INFO PREFIX "_OSI method disabled\n");
999 acpi_gbl_create_osi_method = FALSE;
Len Brownaa2e09d2007-07-02 23:57:45 -04001000 } else if (!strcmp("!Linux", str)) {
1001 enable_osi_linux(0);
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(1);
Len Brownae00d812007-05-29 18:43:33 -04001007 } else if (*osi_additional_string == '\0') {
1008 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
1009 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011
1012 return 1;
1013}
1014
1015__setup("acpi_osi=", acpi_osi_setup);
1016
1017/* enable serialization to combat AE_ALREADY_EXISTS errors */
Len Brown4be44fc2005-08-05 00:44:28 -04001018static int __init acpi_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 printk(KERN_INFO PREFIX "serialize enabled\n");
1021
1022 acpi_gbl_all_methods_serialized = TRUE;
1023
1024 return 1;
1025}
1026
1027__setup("acpi_serialize", acpi_serialize_setup);
1028
1029/*
1030 * Wake and Run-Time GPES are expected to be separate.
1031 * We disable wake-GPEs at run-time to prevent spurious
1032 * interrupts.
1033 *
1034 * However, if a system exists that shares Wake and
1035 * Run-time events on the same GPE this flag is available
1036 * to tell Linux to keep the wake-time GPEs enabled at run-time.
1037 */
Len Brown4be44fc2005-08-05 00:44:28 -04001038static int __init acpi_wake_gpes_always_on_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
1041
1042 acpi_gbl_leave_wake_gpes_disabled = FALSE;
1043
1044 return 1;
1045}
1046
1047__setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049/*
Robert Moore73459f72005-06-24 00:00:00 -04001050 * Acquire a spinlock.
1051 *
1052 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001053 */
1054
Bob Moore967440e32006-06-23 17:04:00 -04001055acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001056{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001057 acpi_cpu_flags flags;
Bob Moore967440e32006-06-23 17:04:00 -04001058 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001059 return flags;
1060}
1061
1062/*
1063 * Release a spinlock. See above.
1064 */
1065
Bob Moore967440e32006-06-23 17:04:00 -04001066void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001067{
Bob Moore967440e32006-06-23 17:04:00 -04001068 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001069}
1070
Robert Moore73459f72005-06-24 00:00:00 -04001071#ifndef ACPI_USE_LOCAL_CACHE
1072
1073/*******************************************************************************
1074 *
1075 * FUNCTION: acpi_os_create_cache
1076 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001077 * PARAMETERS: name - Ascii name for the cache
1078 * size - Size of each cached object
1079 * depth - Maximum depth of the cache (in objects) <ignored>
1080 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001081 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001082 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001083 *
1084 * DESCRIPTION: Create a cache object
1085 *
1086 ******************************************************************************/
1087
1088acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001089acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001090{
Paul Mundt20c2df82007-07-20 10:11:58 +09001091 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001092 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001093 return AE_ERROR;
1094 else
1095 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001096}
1097
1098/*******************************************************************************
1099 *
1100 * FUNCTION: acpi_os_purge_cache
1101 *
1102 * PARAMETERS: Cache - Handle to cache object
1103 *
1104 * RETURN: Status
1105 *
1106 * DESCRIPTION: Free all objects within the requested cache.
1107 *
1108 ******************************************************************************/
1109
Len Brown4be44fc2005-08-05 00:44:28 -04001110acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001111{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001112 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001113 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001114}
1115
1116/*******************************************************************************
1117 *
1118 * FUNCTION: acpi_os_delete_cache
1119 *
1120 * PARAMETERS: Cache - Handle to cache object
1121 *
1122 * RETURN: Status
1123 *
1124 * DESCRIPTION: Free all objects within the requested cache and delete the
1125 * cache object.
1126 *
1127 ******************************************************************************/
1128
Len Brown4be44fc2005-08-05 00:44:28 -04001129acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001130{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001131 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001132 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001133}
1134
1135/*******************************************************************************
1136 *
1137 * FUNCTION: acpi_os_release_object
1138 *
1139 * PARAMETERS: Cache - Handle to cache object
1140 * Object - The object to be released
1141 *
1142 * RETURN: None
1143 *
1144 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1145 * the object is deleted.
1146 *
1147 ******************************************************************************/
1148
Len Brown4be44fc2005-08-05 00:44:28 -04001149acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001150{
Len Brown4be44fc2005-08-05 00:44:28 -04001151 kmem_cache_free(cache, object);
1152 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001153}
1154
Bob Mooreb229cf92006-04-21 17:15:00 -04001155/******************************************************************************
1156 *
1157 * FUNCTION: acpi_os_validate_interface
1158 *
1159 * PARAMETERS: interface - Requested interface to be validated
1160 *
1161 * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
1162 *
1163 * DESCRIPTION: Match an interface string to the interfaces supported by the
1164 * host. Strings originate from an AML call to the _OSI method.
1165 *
1166 *****************************************************************************/
1167
1168acpi_status
1169acpi_os_validate_interface (char *interface)
1170{
Len Brownae00d812007-05-29 18:43:33 -04001171 if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1172 return AE_OK;
Len Brownf5076542007-05-30 00:10:38 -04001173 if (!strcmp("Linux", interface)) {
1174 printk(KERN_WARNING PREFIX
1175 "System BIOS is requesting _OSI(Linux)\n");
Len Brownf5076542007-05-30 00:10:38 -04001176 printk(KERN_WARNING PREFIX
1177 "If \"acpi_osi=Linux\" works better,\n"
1178 "Please send dmidecode "
1179 "to linux-acpi@vger.kernel.org\n");
Len Brownf5076542007-05-30 00:10:38 -04001180 if(osi_linux)
1181 return AE_OK;
1182 }
Len Brownae00d812007-05-29 18:43:33 -04001183 return AE_SUPPORT;
Bob Mooreb229cf92006-04-21 17:15:00 -04001184}
1185
Bob Mooreb229cf92006-04-21 17:15:00 -04001186/******************************************************************************
1187 *
1188 * FUNCTION: acpi_os_validate_address
1189 *
1190 * PARAMETERS: space_id - ACPI space ID
1191 * address - Physical address
1192 * length - Address length
1193 *
1194 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1195 * should return AE_AML_ILLEGAL_ADDRESS.
1196 *
1197 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1198 * the addresses accessed by AML operation regions.
1199 *
1200 *****************************************************************************/
1201
1202acpi_status
1203acpi_os_validate_address (
1204 u8 space_id,
1205 acpi_physical_address address,
1206 acpi_size length)
1207{
1208
1209 return AE_OK;
1210}
1211
Len Brownf5076542007-05-30 00:10:38 -04001212#ifdef CONFIG_DMI
Jeff Garzik18552562007-10-03 15:15:40 -04001213static int dmi_osi_linux(const struct dmi_system_id *d)
Len Brownf5076542007-05-30 00:10:38 -04001214{
Len Brown072971d2007-06-09 01:42:00 -04001215 printk(KERN_NOTICE "%s detected: enabling _OSI(Linux)\n", d->ident);
Len Brownf5076542007-05-30 00:10:38 -04001216 enable_osi_linux(1);
1217 return 0;
1218}
Len Brownf5076542007-05-30 00:10:38 -04001219
Len Browndd272b52007-05-30 00:26:11 -04001220static struct dmi_system_id acpi_osl_dmi_table[] __initdata = {
Len Brownf5076542007-05-30 00:10:38 -04001221 /*
1222 * Boxes that need _OSI(Linux)
1223 */
1224 {
1225 .callback = dmi_osi_linux,
1226 .ident = "Intel Napa CRB",
1227 .matches = {
1228 DMI_MATCH(DMI_BOARD_VENDOR, "Intel Corporation"),
1229 DMI_MATCH(DMI_BOARD_NAME, "MPAD-MSAE Customer Reference Boards"),
1230 },
1231 },
Len Brownf5076542007-05-30 00:10:38 -04001232 {}
1233};
1234#endif /* CONFIG_DMI */
Bob Mooreb229cf92006-04-21 17:15:00 -04001235
Robert Moore73459f72005-06-24 00:00:00 -04001236#endif