blob: aabc6ca4a81c72463529c3e711b012f8b6e909a7 [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 }
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300210 } else
211 return acpi_find_rsdp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300214void __iomem *acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800216 if (phys > ULONG_MAX) {
217 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
Randy Dunlap70c08462007-02-13 16:11:36 -0800218 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300220 if (acpi_gbl_permanent_mmap)
221 /*
222 * ioremap checks to ensure this is in reserved space
223 */
224 return ioremap((unsigned long)phys, size);
225 else
226 return __acpi_map_table((unsigned long)phys, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800228EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Len Brown4be44fc2005-08-05 00:44:28 -0400230void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300232 if (acpi_gbl_permanent_mmap) {
233 iounmap(virt);
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800236EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238#ifdef ACPI_FUTURE_USAGE
239acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400240acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Len Brown4be44fc2005-08-05 00:44:28 -0400242 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return AE_BAD_PARAMETER;
244
245 *phys = virt_to_phys(virt);
246
247 return AE_OK;
248}
249#endif
250
251#define ACPI_MAX_OVERRIDE_LEN 100
252
253static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
254
255acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400256acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
257 acpi_string * new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 if (!init_val || !new_val)
260 return AE_BAD_PARAMETER;
261
262 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400263 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400265 acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 *new_val = acpi_os_name;
267 }
268
269 return AE_OK;
270}
271
272acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400273acpi_os_table_override(struct acpi_table_header * existing_table,
274 struct acpi_table_header ** new_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 if (!existing_table || !new_table)
277 return AE_BAD_PARAMETER;
278
279#ifdef CONFIG_ACPI_CUSTOM_DSDT
280 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400281 *new_table = (struct acpi_table_header *)AmlCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 else
283 *new_table = NULL;
284#else
285 *new_table = NULL;
286#endif
287 return AE_OK;
288}
289
David Howells7d12e782006-10-05 14:55:46 +0100290static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Len Brown4be44fc2005-08-05 00:44:28 -0400292 return (*acpi_irq_handler) (acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400296acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
297 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 unsigned int irq;
300
301 /*
302 * Ignore the GSI from the core, and use the value in our copy of the
303 * FADT. It may not be the same if an interrupt source override exists
304 * for the SCI.
305 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300306 gsi = acpi_gbl_FADT.sci_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
308 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
309 gsi);
310 return AE_OK;
311 }
312
313 acpi_irq_handler = handler;
314 acpi_irq_context = context;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700315 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
317 return AE_NOT_ACQUIRED;
318 }
319 acpi_irq_irq = irq;
320
321 return AE_OK;
322}
323
Len Brown4be44fc2005-08-05 00:44:28 -0400324acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 if (irq) {
327 free_irq(irq, acpi_irq);
328 acpi_irq_handler = NULL;
329 acpi_irq_irq = 0;
330 }
331
332 return AE_OK;
333}
334
335/*
336 * Running in interpreter thread context, safe to sleep
337 */
338
Len Brown4be44fc2005-08-05 00:44:28 -0400339void acpi_os_sleep(acpi_integer ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800341 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
Len Brown4be44fc2005-08-05 00:44:28 -0400343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344EXPORT_SYMBOL(acpi_os_sleep);
345
Len Brown4be44fc2005-08-05 00:44:28 -0400346void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 while (us) {
349 u32 delay = 1000;
350
351 if (delay > us)
352 delay = us;
353 udelay(delay);
354 touch_nmi_watchdog();
355 us -= delay;
356 }
357}
Len Brown4be44fc2005-08-05 00:44:28 -0400358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359EXPORT_SYMBOL(acpi_os_stall);
360
361/*
362 * Support ACPI 3.0 AML Timer operand
363 * Returns 64-bit free-running, monotonically increasing timer
364 * with 100ns granularity
365 */
Len Brown4be44fc2005-08-05 00:44:28 -0400366u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 static u64 t;
369
370#ifdef CONFIG_HPET
371 /* TBD: use HPET if available */
372#endif
373
374#ifdef CONFIG_X86_PM_TIMER
375 /* TBD: default to PM timer if HPET was not available */
376#endif
377 if (!t)
378 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
379
380 return ++t;
381}
382
Len Brown4be44fc2005-08-05 00:44:28 -0400383acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 u32 dummy;
386
387 if (!value)
388 value = &dummy;
389
Len Brown4be44fc2005-08-05 00:44:28 -0400390 switch (width) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400392 *(u8 *) value = inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 break;
394 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400395 *(u16 *) value = inw(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 break;
397 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400398 *(u32 *) value = inl(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 break;
400 default:
401 BUG();
402 }
403
404 return AE_OK;
405}
Len Brown4be44fc2005-08-05 00:44:28 -0400406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407EXPORT_SYMBOL(acpi_os_read_port);
408
Len Brown4be44fc2005-08-05 00:44:28 -0400409acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Len Brown4be44fc2005-08-05 00:44:28 -0400411 switch (width) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 case 8:
413 outb(value, port);
414 break;
415 case 16:
416 outw(value, port);
417 break;
418 case 32:
419 outl(value, port);
420 break;
421 default:
422 BUG();
423 }
424
425 return AE_OK;
426}
Len Brown4be44fc2005-08-05 00:44:28 -0400427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428EXPORT_SYMBOL(acpi_os_write_port);
429
430acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400431acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Len Brown4be44fc2005-08-05 00:44:28 -0400433 u32 dummy;
434 void __iomem *virt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800436 virt_addr = ioremap(phys_addr, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (!value)
438 value = &dummy;
439
440 switch (width) {
441 case 8:
Len Brown4be44fc2005-08-05 00:44:28 -0400442 *(u8 *) value = readb(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 break;
444 case 16:
Len Brown4be44fc2005-08-05 00:44:28 -0400445 *(u16 *) value = readw(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 break;
447 case 32:
Len Brown4be44fc2005-08-05 00:44:28 -0400448 *(u32 *) value = readl(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 break;
450 default:
451 BUG();
452 }
453
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800454 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 return AE_OK;
457}
458
459acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400460acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Len Brown4be44fc2005-08-05 00:44:28 -0400462 void __iomem *virt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800464 virt_addr = ioremap(phys_addr, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 switch (width) {
467 case 8:
468 writeb(value, virt_addr);
469 break;
470 case 16:
471 writew(value, virt_addr);
472 break;
473 case 32:
474 writel(value, virt_addr);
475 break;
476 default:
477 BUG();
478 }
479
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800480 iounmap(virt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 return AE_OK;
483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400486acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
487 void *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 int result, size;
490
491 if (!value)
492 return AE_BAD_PARAMETER;
493
494 switch (width) {
495 case 8:
496 size = 1;
497 break;
498 case 16:
499 size = 2;
500 break;
501 case 32:
502 size = 4;
503 break;
504 default:
505 return AE_ERROR;
506 }
507
508 BUG_ON(!raw_pci_ops);
509
510 result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
Len Brown4be44fc2005-08-05 00:44:28 -0400511 PCI_DEVFN(pci_id->device, pci_id->function),
512 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 return (result ? AE_ERROR : AE_OK);
515}
Len Brown4be44fc2005-08-05 00:44:28 -0400516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517EXPORT_SYMBOL(acpi_os_read_pci_configuration);
518
519acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400520acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
521 acpi_integer value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 int result, size;
524
525 switch (width) {
526 case 8:
527 size = 1;
528 break;
529 case 16:
530 size = 2;
531 break;
532 case 32:
533 size = 4;
534 break;
535 default:
536 return AE_ERROR;
537 }
538
539 BUG_ON(!raw_pci_ops);
540
541 result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
Len Brown4be44fc2005-08-05 00:44:28 -0400542 PCI_DEVFN(pci_id->device, pci_id->function),
543 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 return (result ? AE_ERROR : AE_OK);
546}
547
548/* TODO: Change code to take advantage of driver model more */
Len Brown4be44fc2005-08-05 00:44:28 -0400549static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
550 acpi_handle chandle, /* current node */
551 struct acpi_pci_id **id,
552 int *is_bridge, u8 * bus_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Len Brown4be44fc2005-08-05 00:44:28 -0400554 acpi_handle handle;
555 struct acpi_pci_id *pci_id = *id;
556 acpi_status status;
557 unsigned long temp;
558 acpi_object_type type;
559 u8 tu8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 acpi_get_parent(chandle, &handle);
562 if (handle != rhandle) {
Len Brown4be44fc2005-08-05 00:44:28 -0400563 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
564 bus_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 status = acpi_get_type(handle, &type);
Len Brown4be44fc2005-08-05 00:44:28 -0400567 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return;
569
Len Brown4be44fc2005-08-05 00:44:28 -0400570 status =
571 acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
572 &temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (ACPI_SUCCESS(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400574 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
575 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 if (*is_bridge)
578 pci_id->bus = *bus_number;
579
580 /* any nicer way to get bus number of bridge ? */
Len Brown4be44fc2005-08-05 00:44:28 -0400581 status =
582 acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
583 8);
584 if (ACPI_SUCCESS(status)
585 && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
586 status =
587 acpi_os_read_pci_configuration(pci_id, 0x18,
588 &tu8, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (!ACPI_SUCCESS(status)) {
590 /* Certainly broken... FIX ME */
591 return;
592 }
593 *is_bridge = 1;
594 pci_id->bus = tu8;
Len Brown4be44fc2005-08-05 00:44:28 -0400595 status =
596 acpi_os_read_pci_configuration(pci_id, 0x19,
597 &tu8, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (ACPI_SUCCESS(status)) {
599 *bus_number = tu8;
600 }
601 } else
602 *is_bridge = 0;
603 }
604 }
605}
606
Len Brown4be44fc2005-08-05 00:44:28 -0400607void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
608 acpi_handle chandle, /* current node */
609 struct acpi_pci_id **id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 int is_bridge = 1;
612 u8 bus_number = (*id)->bus;
613
614 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
615}
616
David Howells65f27f32006-11-22 14:55:48 +0000617static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
David Howells65f27f32006-11-22 14:55:48 +0000619 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400620 if (!dpc) {
621 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
622 return;
623 }
624
625 dpc->function(dpc->context);
626 kfree(dpc);
627
628 /* Yield cpu to notify thread */
629 cond_resched();
630
631 return;
632}
633
634static void acpi_os_execute_notify(struct work_struct *work)
635{
636 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (!dpc) {
Len Brown64684632006-06-26 23:41:38 -0400639 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400640 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
643 dpc->function(dpc->context);
644
645 kfree(dpc);
646
Patrick Mocheld550d982006-06-27 00:41:40 -0400647 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400650/*******************************************************************************
651 *
652 * FUNCTION: acpi_os_execute
653 *
654 * PARAMETERS: Type - Type of the callback
655 * Function - Function to be executed
656 * Context - Function parameters
657 *
658 * RETURN: Status
659 *
660 * DESCRIPTION: Depending on type, either queues function for deferred execution or
661 * immediately executes function on a separate thread.
662 *
663 ******************************************************************************/
664
665acpi_status acpi_os_execute(acpi_execute_type type,
Len Brown4be44fc2005-08-05 00:44:28 -0400666 acpi_osd_exec_callback function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Len Brown4be44fc2005-08-05 00:44:28 -0400668 acpi_status status = AE_OK;
669 struct acpi_os_dpc *dpc;
Len Brown72945b22006-07-12 22:46:42 -0400670
Len Brown72945b22006-07-12 22:46:42 -0400671 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
672 "Scheduling function [%p(%p)] for deferred execution.\n",
673 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 if (!function)
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400676 return AE_BAD_PARAMETER;
Len Brown72945b22006-07-12 22:46:42 -0400677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /*
679 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +0000680 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 * way that allows us to also free its memory inside the callee.
682 * Because we may want to schedule several tasks with different
683 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +0000684 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Len Brown72945b22006-07-12 22:46:42 -0400686
David Howells65f27f32006-11-22 14:55:48 +0000687 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (!dpc)
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800689 return_ACPI_STATUS(AE_NO_MEMORY);
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 dpc->function = function;
692 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800693
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400694 if (type == OSL_NOTIFY_HANDLER) {
695 INIT_WORK(&dpc->work, acpi_os_execute_notify);
696 if (!queue_work(kacpi_notify_wq, &dpc->work)) {
697 status = AE_ERROR;
698 kfree(dpc);
699 }
700 } else {
701 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
702 if (!queue_work(kacpid_wq, &dpc->work)) {
703 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800704 "Call to queue_work() failed.\n"));
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400705 status = AE_ERROR;
706 kfree(dpc);
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
Linus Torvaldsb976fe12006-11-17 19:31:09 -0800709 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
Len Brown4be44fc2005-08-05 00:44:28 -0400711
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400712EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Len Brown4be44fc2005-08-05 00:44:28 -0400714void acpi_os_wait_events_complete(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 flush_workqueue(kacpid_wq);
717}
Len Brown4be44fc2005-08-05 00:44:28 -0400718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719EXPORT_SYMBOL(acpi_os_wait_events_complete);
720
721/*
722 * Allocate the memory for a spinlock and initialize it.
723 */
Bob Moore967440e32006-06-23 17:04:00 -0400724acpi_status acpi_os_create_lock(acpi_spinlock * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Bob Moore967440e32006-06-23 17:04:00 -0400726 spin_lock_init(*handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Patrick Mocheld550d982006-06-27 00:41:40 -0400728 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/*
732 * Deallocate the memory for a spinlock.
733 */
Bob Moore967440e32006-06-23 17:04:00 -0400734void acpi_os_delete_lock(acpi_spinlock handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Patrick Mocheld550d982006-06-27 00:41:40 -0400736 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400740acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Len Brown4be44fc2005-08-05 00:44:28 -0400742 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 sem = acpi_os_allocate(sizeof(struct semaphore));
746 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400747 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 memset(sem, 0, sizeof(struct semaphore));
749
750 sema_init(sem, initial_units);
751
Len Brown4be44fc2005-08-05 00:44:28 -0400752 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Len Brown4be44fc2005-08-05 00:44:28 -0400754 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
755 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Patrick Mocheld550d982006-06-27 00:41:40 -0400757 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Len Brown4be44fc2005-08-05 00:44:28 -0400760EXPORT_SYMBOL(acpi_os_create_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762/*
763 * TODO: A better way to delete semaphores? Linux doesn't have a
764 * 'delete_semaphore()' function -- may result in an invalid
765 * pointer dereference for non-synchronized consumers. Should
766 * we at least check for blocked threads and signal/cancel them?
767 */
768
Len Brown4be44fc2005-08-05 00:44:28 -0400769acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Len Brown4be44fc2005-08-05 00:44:28 -0400771 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -0400775 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Len Brown4be44fc2005-08-05 00:44:28 -0400777 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Len Brown02438d82006-06-30 03:19:10 -0400779 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -0400780 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Patrick Mocheld550d982006-06-27 00:41:40 -0400782 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Len Brown4be44fc2005-08-05 00:44:28 -0400785EXPORT_SYMBOL(acpi_os_delete_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787/*
788 * TODO: The kernel doesn't have a 'down_timeout' function -- had to
789 * improvise. The process is to sleep for one scheduler quantum
790 * until the semaphore becomes available. Downside is that this
791 * may result in starvation for timeout-based waits when there's
792 * lots of semaphore activity.
793 *
794 * TODO: Support for units > 1?
795 */
Len Brown4be44fc2005-08-05 00:44:28 -0400796acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Len Brown4be44fc2005-08-05 00:44:28 -0400798 acpi_status status = AE_OK;
799 struct semaphore *sem = (struct semaphore *)handle;
800 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400804 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400807 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Len Brown4be44fc2005-08-05 00:44:28 -0400809 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
810 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Len Brownd68909f2006-08-16 19:16:58 -0400812 /*
813 * This can be called during resume with interrupts off.
814 * Like boot-time, we should be single threaded and will
815 * always get the lock if we try -- timeout or not.
816 * If this doesn't succeed, then we will oops courtesy of
817 * might_sleep() in down().
818 */
819 if (!down_trylock(sem))
820 return AE_OK;
821
Len Brown4be44fc2005-08-05 00:44:28 -0400822 switch (timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /*
824 * No Wait:
825 * --------
826 * A zero timeout value indicates that we shouldn't wait - just
827 * acquire the semaphore if available otherwise return AE_TIME
828 * (a.k.a. 'would block').
829 */
Len Brown4be44fc2005-08-05 00:44:28 -0400830 case 0:
831 if (down_trylock(sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 status = AE_TIME;
833 break;
834
835 /*
836 * Wait Indefinitely:
837 * ------------------
838 */
Len Brown4be44fc2005-08-05 00:44:28 -0400839 case ACPI_WAIT_FOREVER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 down(sem);
841 break;
842
843 /*
844 * Wait w/ Timeout:
845 * ----------------
846 */
Len Brown4be44fc2005-08-05 00:44:28 -0400847 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 // TODO: A better timeout algorithm?
849 {
850 int i = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400851 static const int quantum_ms = 1000 / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 ret = down_trylock(sem);
Yu Lumingdacd9b82005-12-31 01:45:00 -0500854 for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
Nishanth Aravamudan01a527e2005-11-07 01:01:14 -0800855 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 ret = down_trylock(sem);
857 }
Len Brown4be44fc2005-08-05 00:44:28 -0400858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (ret != 0)
860 status = AE_TIME;
861 }
862 break;
863 }
864
865 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -0400866 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400867 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -0400868 handle, units, timeout,
869 acpi_format_exception(status)));
870 } else {
871 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -0400872 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400873 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
Patrick Mocheld550d982006-06-27 00:41:40 -0400876 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Len Brown4be44fc2005-08-05 00:44:28 -0400879EXPORT_SYMBOL(acpi_os_wait_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881/*
882 * TODO: Support for units > 1?
883 */
Len Brown4be44fc2005-08-05 00:44:28 -0400884acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Len Brown4be44fc2005-08-05 00:44:28 -0400886 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400890 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -0400893 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Len Brown4be44fc2005-08-05 00:44:28 -0400895 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
896 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 up(sem);
899
Patrick Mocheld550d982006-06-27 00:41:40 -0400900 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
Len Brown4be44fc2005-08-05 00:44:28 -0400902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903EXPORT_SYMBOL(acpi_os_signal_semaphore);
904
905#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -0400906u32 acpi_os_get_line(char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908
909#ifdef ENABLE_DEBUGGER
910 if (acpi_in_debugger) {
911 u32 chars;
912
913 kdb_read(buffer, sizeof(line_buf));
914
915 /* remove the CR kdb includes */
916 chars = strlen(buffer) - 1;
917 buffer[chars] = '\0';
918 }
919#endif
920
921 return 0;
922}
Len Brown4be44fc2005-08-05 00:44:28 -0400923#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Len Brown4be44fc2005-08-05 00:44:28 -0400925acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Len Brown4be44fc2005-08-05 00:44:28 -0400927 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 case ACPI_SIGNAL_FATAL:
929 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
930 break;
931 case ACPI_SIGNAL_BREAKPOINT:
932 /*
933 * AML Breakpoint
934 * ACPI spec. says to treat it as a NOP unless
935 * you are debugging. So if/when we integrate
936 * AML debugger into the kernel debugger its
937 * hook will go here. But until then it is
938 * not useful to print anything on breakpoints.
939 */
940 break;
941 default:
942 break;
943 }
944
945 return AE_OK;
946}
Len Brown4be44fc2005-08-05 00:44:28 -0400947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948EXPORT_SYMBOL(acpi_os_signal);
949
Len Brown4be44fc2005-08-05 00:44:28 -0400950static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -0400953 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 if (!str || !*str)
956 return 0;
957
958 for (; count-- && str && *str; str++) {
959 if (isalnum(*str) || *str == ' ' || *str == ':')
960 *p++ = *str;
961 else if (*str == '\'' || *str == '"')
962 continue;
963 else
964 break;
965 }
966 *p = 0;
967
968 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972__setup("acpi_os_name=", acpi_os_name_setup);
973
Len Brownf5076542007-05-30 00:10:38 -0400974static void enable_osi_linux(int enable) {
975
976 if (osi_linux != enable)
977 printk(KERN_INFO PREFIX "%sabled _OSI(Linux)\n",
978 enable ? "En": "Dis");
979
980 osi_linux = enable;
981 return;
982}
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984/*
Len Brownae00d812007-05-29 18:43:33 -0400985 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
986 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -0400988 * string starting with '!' disables that string
989 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 */
Len Brown4be44fc2005-08-05 00:44:28 -0400991static int __init acpi_osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 if (str == NULL || *str == '\0') {
994 printk(KERN_INFO PREFIX "_OSI method disabled\n");
995 acpi_gbl_create_osi_method = FALSE;
Len Brownaa2e09d2007-07-02 23:57:45 -0400996 } else if (!strcmp("!Linux", str)) {
997 enable_osi_linux(0);
Len Brownae00d812007-05-29 18:43:33 -0400998 } else if (*str == '!') {
999 if (acpi_osi_invalidate(++str) == AE_OK)
1000 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
Len Brownf5076542007-05-30 00:10:38 -04001001 } else if (!strcmp("Linux", str)) {
1002 enable_osi_linux(1);
Len Brownae00d812007-05-29 18:43:33 -04001003 } else if (*osi_additional_string == '\0') {
1004 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
1005 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007
1008 return 1;
1009}
1010
1011__setup("acpi_osi=", acpi_osi_setup);
1012
1013/* enable serialization to combat AE_ALREADY_EXISTS errors */
Len Brown4be44fc2005-08-05 00:44:28 -04001014static int __init acpi_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 printk(KERN_INFO PREFIX "serialize enabled\n");
1017
1018 acpi_gbl_all_methods_serialized = TRUE;
1019
1020 return 1;
1021}
1022
1023__setup("acpi_serialize", acpi_serialize_setup);
1024
1025/*
1026 * Wake and Run-Time GPES are expected to be separate.
1027 * We disable wake-GPEs at run-time to prevent spurious
1028 * interrupts.
1029 *
1030 * However, if a system exists that shares Wake and
1031 * Run-time events on the same GPE this flag is available
1032 * to tell Linux to keep the wake-time GPEs enabled at run-time.
1033 */
Len Brown4be44fc2005-08-05 00:44:28 -04001034static int __init acpi_wake_gpes_always_on_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
1037
1038 acpi_gbl_leave_wake_gpes_disabled = FALSE;
1039
1040 return 1;
1041}
1042
1043__setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045/*
Robert Moore73459f72005-06-24 00:00:00 -04001046 * Acquire a spinlock.
1047 *
1048 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001049 */
1050
Bob Moore967440e32006-06-23 17:04:00 -04001051acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001052{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001053 acpi_cpu_flags flags;
Bob Moore967440e32006-06-23 17:04:00 -04001054 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001055 return flags;
1056}
1057
1058/*
1059 * Release a spinlock. See above.
1060 */
1061
Bob Moore967440e32006-06-23 17:04:00 -04001062void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001063{
Bob Moore967440e32006-06-23 17:04:00 -04001064 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001065}
1066
Robert Moore73459f72005-06-24 00:00:00 -04001067#ifndef ACPI_USE_LOCAL_CACHE
1068
1069/*******************************************************************************
1070 *
1071 * FUNCTION: acpi_os_create_cache
1072 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001073 * PARAMETERS: name - Ascii name for the cache
1074 * size - Size of each cached object
1075 * depth - Maximum depth of the cache (in objects) <ignored>
1076 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001077 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001078 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001079 *
1080 * DESCRIPTION: Create a cache object
1081 *
1082 ******************************************************************************/
1083
1084acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001085acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001086{
Paul Mundt20c2df82007-07-20 10:11:58 +09001087 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001088 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001089 return AE_ERROR;
1090 else
1091 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001092}
1093
1094/*******************************************************************************
1095 *
1096 * FUNCTION: acpi_os_purge_cache
1097 *
1098 * PARAMETERS: Cache - Handle to cache object
1099 *
1100 * RETURN: Status
1101 *
1102 * DESCRIPTION: Free all objects within the requested cache.
1103 *
1104 ******************************************************************************/
1105
Len Brown4be44fc2005-08-05 00:44:28 -04001106acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001107{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001108 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001109 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001110}
1111
1112/*******************************************************************************
1113 *
1114 * FUNCTION: acpi_os_delete_cache
1115 *
1116 * PARAMETERS: Cache - Handle to cache object
1117 *
1118 * RETURN: Status
1119 *
1120 * DESCRIPTION: Free all objects within the requested cache and delete the
1121 * cache object.
1122 *
1123 ******************************************************************************/
1124
Len Brown4be44fc2005-08-05 00:44:28 -04001125acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001126{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001127 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001128 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001129}
1130
1131/*******************************************************************************
1132 *
1133 * FUNCTION: acpi_os_release_object
1134 *
1135 * PARAMETERS: Cache - Handle to cache object
1136 * Object - The object to be released
1137 *
1138 * RETURN: None
1139 *
1140 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1141 * the object is deleted.
1142 *
1143 ******************************************************************************/
1144
Len Brown4be44fc2005-08-05 00:44:28 -04001145acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001146{
Len Brown4be44fc2005-08-05 00:44:28 -04001147 kmem_cache_free(cache, object);
1148 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001149}
1150
Bob Mooreb229cf92006-04-21 17:15:00 -04001151/******************************************************************************
1152 *
1153 * FUNCTION: acpi_os_validate_interface
1154 *
1155 * PARAMETERS: interface - Requested interface to be validated
1156 *
1157 * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
1158 *
1159 * DESCRIPTION: Match an interface string to the interfaces supported by the
1160 * host. Strings originate from an AML call to the _OSI method.
1161 *
1162 *****************************************************************************/
1163
1164acpi_status
1165acpi_os_validate_interface (char *interface)
1166{
Len Brownae00d812007-05-29 18:43:33 -04001167 if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1168 return AE_OK;
Len Brownf5076542007-05-30 00:10:38 -04001169 if (!strcmp("Linux", interface)) {
1170 printk(KERN_WARNING PREFIX
1171 "System BIOS is requesting _OSI(Linux)\n");
Len Brownf5076542007-05-30 00:10:38 -04001172 printk(KERN_WARNING PREFIX
1173 "If \"acpi_osi=Linux\" works better,\n"
1174 "Please send dmidecode "
1175 "to linux-acpi@vger.kernel.org\n");
Len Brownf5076542007-05-30 00:10:38 -04001176 if(osi_linux)
1177 return AE_OK;
1178 }
Len Brownae00d812007-05-29 18:43:33 -04001179 return AE_SUPPORT;
Bob Mooreb229cf92006-04-21 17:15:00 -04001180}
1181
Bob Mooreb229cf92006-04-21 17:15:00 -04001182/******************************************************************************
1183 *
1184 * FUNCTION: acpi_os_validate_address
1185 *
1186 * PARAMETERS: space_id - ACPI space ID
1187 * address - Physical address
1188 * length - Address length
1189 *
1190 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1191 * should return AE_AML_ILLEGAL_ADDRESS.
1192 *
1193 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1194 * the addresses accessed by AML operation regions.
1195 *
1196 *****************************************************************************/
1197
1198acpi_status
1199acpi_os_validate_address (
1200 u8 space_id,
1201 acpi_physical_address address,
1202 acpi_size length)
1203{
1204
1205 return AE_OK;
1206}
1207
Len Brownf5076542007-05-30 00:10:38 -04001208#ifdef CONFIG_DMI
Jeff Garzik18552562007-10-03 15:15:40 -04001209static int dmi_osi_linux(const struct dmi_system_id *d)
Len Brownf5076542007-05-30 00:10:38 -04001210{
Len Brown072971d2007-06-09 01:42:00 -04001211 printk(KERN_NOTICE "%s detected: enabling _OSI(Linux)\n", d->ident);
Len Brownf5076542007-05-30 00:10:38 -04001212 enable_osi_linux(1);
1213 return 0;
1214}
Len Brownf5076542007-05-30 00:10:38 -04001215
Len Browndd272b52007-05-30 00:26:11 -04001216static struct dmi_system_id acpi_osl_dmi_table[] __initdata = {
Len Brownf5076542007-05-30 00:10:38 -04001217 /*
1218 * Boxes that need _OSI(Linux)
1219 */
1220 {
1221 .callback = dmi_osi_linux,
1222 .ident = "Intel Napa CRB",
1223 .matches = {
1224 DMI_MATCH(DMI_BOARD_VENDOR, "Intel Corporation"),
1225 DMI_MATCH(DMI_BOARD_NAME, "MPAD-MSAE Customer Reference Boards"),
1226 },
1227 },
Len Brownf5076542007-05-30 00:10:38 -04001228 {}
1229};
1230#endif /* CONFIG_DMI */
Bob Mooreb229cf92006-04-21 17:15:00 -04001231
Robert Moore73459f72005-06-24 00:00:00 -04001232#endif