Revert "ACPICA: fix AML mutex re-entrancy"
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / acpi / osl.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
28#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/slab.h>
31#include <linux/mm.h>
32#include <linux/pci.h>
1da177e4
LT
33#include <linux/interrupt.h>
34#include <linux/kmod.h>
35#include <linux/delay.h>
36#include <linux/workqueue.h>
37#include <linux/nmi.h>
ad71860a 38#include <linux/acpi.h>
1da177e4
LT
39#include <acpi/acpi.h>
40#include <asm/io.h>
41#include <acpi/acpi_bus.h>
42#include <acpi/processor.h>
43#include <asm/uaccess.h>
44
45#include <linux/efi.h>
46
1da177e4 47#define _COMPONENT ACPI_OS_SERVICES
f52fd66d 48ACPI_MODULE_NAME("osl");
1da177e4 49#define PREFIX "ACPI: "
4be44fcd
LB
50struct acpi_os_dpc {
51 acpi_osd_exec_callback function;
52 void *context;
65f27f38 53 struct work_struct work;
1da177e4
LT
54};
55
56#ifdef CONFIG_ACPI_CUSTOM_DSDT
57#include CONFIG_ACPI_CUSTOM_DSDT_FILE
58#endif
59
60#ifdef ENABLE_DEBUGGER
61#include <linux/kdb.h>
62
63/* stuff for debugger support */
64int acpi_in_debugger;
65EXPORT_SYMBOL(acpi_in_debugger);
66
67extern char line_buf[80];
4be44fcd 68#endif /*ENABLE_DEBUGGER */
1da177e4
LT
69
70static unsigned int acpi_irq_irq;
71static acpi_osd_handler acpi_irq_handler;
72static void *acpi_irq_context;
73static struct workqueue_struct *kacpid_wq;
74
9a47cdb1
BH
75static void __init acpi_request_region (struct acpi_generic_address *addr,
76 unsigned int length, char *desc)
77{
78 struct resource *res;
79
80 if (!addr->address || !length)
81 return;
82
eee3c859 83 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
9a47cdb1 84 res = request_region(addr->address, length, desc);
eee3c859 85 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
9a47cdb1
BH
86 res = request_mem_region(addr->address, length, desc);
87}
88
89static int __init acpi_reserve_resources(void)
90{
eee3c859 91 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
9a47cdb1
BH
92 "ACPI PM1a_EVT_BLK");
93
eee3c859 94 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
9a47cdb1
BH
95 "ACPI PM1b_EVT_BLK");
96
eee3c859 97 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
9a47cdb1
BH
98 "ACPI PM1a_CNT_BLK");
99
eee3c859 100 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
9a47cdb1
BH
101 "ACPI PM1b_CNT_BLK");
102
eee3c859
LB
103 if (acpi_gbl_FADT.pm_timer_length == 4)
104 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
9a47cdb1 105
eee3c859 106 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
9a47cdb1
BH
107 "ACPI PM2_CNT_BLK");
108
109 /* Length of GPE blocks must be a non-negative multiple of 2 */
110
eee3c859
LB
111 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
112 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
113 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
9a47cdb1 114
eee3c859
LB
115 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
116 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
117 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
9a47cdb1
BH
118
119 return 0;
120}
121device_initcall(acpi_reserve_resources);
122
4be44fcd 123acpi_status acpi_os_initialize(void)
1da177e4
LT
124{
125 return AE_OK;
126}
127
4be44fcd 128acpi_status acpi_os_initialize1(void)
1da177e4
LT
129{
130 /*
131 * Initialize PCI configuration space access, as we'll need to access
132 * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
133 */
1da177e4 134 if (!raw_pci_ops) {
4be44fcd
LB
135 printk(KERN_ERR PREFIX
136 "Access to PCI configuration space unavailable\n");
1da177e4
LT
137 return AE_NULL_ENTRY;
138 }
1da177e4
LT
139 kacpid_wq = create_singlethread_workqueue("kacpid");
140 BUG_ON(!kacpid_wq);
b976fe19 141
1da177e4
LT
142 return AE_OK;
143}
144
4be44fcd 145acpi_status acpi_os_terminate(void)
1da177e4
LT
146{
147 if (acpi_irq_handler) {
148 acpi_os_remove_interrupt_handler(acpi_irq_irq,
149 acpi_irq_handler);
150 }
151
152 destroy_workqueue(kacpid_wq);
153
154 return AE_OK;
155}
156
4be44fcd 157void acpi_os_printf(const char *fmt, ...)
1da177e4
LT
158{
159 va_list args;
160 va_start(args, fmt);
161 acpi_os_vprintf(fmt, args);
162 va_end(args);
163}
4be44fcd 164
1da177e4
LT
165EXPORT_SYMBOL(acpi_os_printf);
166
4be44fcd 167void acpi_os_vprintf(const char *fmt, va_list args)
1da177e4
LT
168{
169 static char buffer[512];
4be44fcd 170
1da177e4
LT
171 vsprintf(buffer, fmt, args);
172
173#ifdef ENABLE_DEBUGGER
174 if (acpi_in_debugger) {
175 kdb_printf("%s", buffer);
176 } else {
177 printk("%s", buffer);
178 }
179#else
180 printk("%s", buffer);
181#endif
182}
183
ad71860a 184acpi_physical_address __init acpi_os_get_root_pointer(void)
1da177e4
LT
185{
186 if (efi_enabled) {
b2c99e3c 187 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
ad71860a 188 return efi.acpi20;
b2c99e3c 189 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
ad71860a 190 return efi.acpi;
1da177e4 191 else {
4be44fcd
LB
192 printk(KERN_ERR PREFIX
193 "System description tables not found\n");
ad71860a 194 return 0;
1da177e4 195 }
ad71860a
AS
196 } else
197 return acpi_find_rsdp();
1da177e4
LT
198}
199
ad71860a 200void __iomem *acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
1da177e4 201{
9f4fd61f
BH
202 if (phys > ULONG_MAX) {
203 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
70c0846e 204 return NULL;
1da177e4 205 }
ad71860a
AS
206 if (acpi_gbl_permanent_mmap)
207 /*
208 * ioremap checks to ensure this is in reserved space
209 */
210 return ioremap((unsigned long)phys, size);
211 else
212 return __acpi_map_table((unsigned long)phys, size);
1da177e4 213}
55a82ab3 214EXPORT_SYMBOL_GPL(acpi_os_map_memory);
1da177e4 215
4be44fcd 216void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
1da177e4 217{
ad71860a
AS
218 if (acpi_gbl_permanent_mmap) {
219 iounmap(virt);
220 }
1da177e4 221}
55a82ab3 222EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
1da177e4
LT
223
224#ifdef ACPI_FUTURE_USAGE
225acpi_status
4be44fcd 226acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
1da177e4 227{
4be44fcd 228 if (!phys || !virt)
1da177e4
LT
229 return AE_BAD_PARAMETER;
230
231 *phys = virt_to_phys(virt);
232
233 return AE_OK;
234}
235#endif
236
237#define ACPI_MAX_OVERRIDE_LEN 100
238
239static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
240
241acpi_status
4be44fcd
LB
242acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
243 acpi_string * new_val)
1da177e4
LT
244{
245 if (!init_val || !new_val)
246 return AE_BAD_PARAMETER;
247
248 *new_val = NULL;
4be44fcd 249 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
1da177e4 250 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
4be44fcd 251 acpi_os_name);
1da177e4
LT
252 *new_val = acpi_os_name;
253 }
254
255 return AE_OK;
256}
257
258acpi_status
4be44fcd
LB
259acpi_os_table_override(struct acpi_table_header * existing_table,
260 struct acpi_table_header ** new_table)
1da177e4
LT
261{
262 if (!existing_table || !new_table)
263 return AE_BAD_PARAMETER;
264
265#ifdef CONFIG_ACPI_CUSTOM_DSDT
266 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
4be44fcd 267 *new_table = (struct acpi_table_header *)AmlCode;
1da177e4
LT
268 else
269 *new_table = NULL;
270#else
271 *new_table = NULL;
272#endif
273 return AE_OK;
274}
275
7d12e780 276static irqreturn_t acpi_irq(int irq, void *dev_id)
1da177e4 277{
4be44fcd 278 return (*acpi_irq_handler) (acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
1da177e4
LT
279}
280
281acpi_status
4be44fcd
LB
282acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
283 void *context)
1da177e4
LT
284{
285 unsigned int irq;
286
287 /*
288 * Ignore the GSI from the core, and use the value in our copy of the
289 * FADT. It may not be the same if an interrupt source override exists
290 * for the SCI.
291 */
cee324b1 292 gsi = acpi_gbl_FADT.sci_interrupt;
1da177e4
LT
293 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
294 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
295 gsi);
296 return AE_OK;
297 }
298
299 acpi_irq_handler = handler;
300 acpi_irq_context = context;
dace1453 301 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
1da177e4
LT
302 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
303 return AE_NOT_ACQUIRED;
304 }
305 acpi_irq_irq = irq;
306
307 return AE_OK;
308}
309
4be44fcd 310acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
1da177e4
LT
311{
312 if (irq) {
313 free_irq(irq, acpi_irq);
314 acpi_irq_handler = NULL;
315 acpi_irq_irq = 0;
316 }
317
318 return AE_OK;
319}
320
321/*
322 * Running in interpreter thread context, safe to sleep
323 */
324
4be44fcd 325void acpi_os_sleep(acpi_integer ms)
1da177e4 326{
01a527ec 327 schedule_timeout_interruptible(msecs_to_jiffies(ms));
1da177e4 328}
4be44fcd 329
1da177e4
LT
330EXPORT_SYMBOL(acpi_os_sleep);
331
4be44fcd 332void acpi_os_stall(u32 us)
1da177e4
LT
333{
334 while (us) {
335 u32 delay = 1000;
336
337 if (delay > us)
338 delay = us;
339 udelay(delay);
340 touch_nmi_watchdog();
341 us -= delay;
342 }
343}
4be44fcd 344
1da177e4
LT
345EXPORT_SYMBOL(acpi_os_stall);
346
347/*
348 * Support ACPI 3.0 AML Timer operand
349 * Returns 64-bit free-running, monotonically increasing timer
350 * with 100ns granularity
351 */
4be44fcd 352u64 acpi_os_get_timer(void)
1da177e4
LT
353{
354 static u64 t;
355
356#ifdef CONFIG_HPET
357 /* TBD: use HPET if available */
358#endif
359
360#ifdef CONFIG_X86_PM_TIMER
361 /* TBD: default to PM timer if HPET was not available */
362#endif
363 if (!t)
364 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
365
366 return ++t;
367}
368
4be44fcd 369acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
1da177e4
LT
370{
371 u32 dummy;
372
373 if (!value)
374 value = &dummy;
375
4be44fcd 376 switch (width) {
1da177e4 377 case 8:
4be44fcd 378 *(u8 *) value = inb(port);
1da177e4
LT
379 break;
380 case 16:
4be44fcd 381 *(u16 *) value = inw(port);
1da177e4
LT
382 break;
383 case 32:
4be44fcd 384 *(u32 *) value = inl(port);
1da177e4
LT
385 break;
386 default:
387 BUG();
388 }
389
390 return AE_OK;
391}
4be44fcd 392
1da177e4
LT
393EXPORT_SYMBOL(acpi_os_read_port);
394
4be44fcd 395acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
1da177e4 396{
4be44fcd 397 switch (width) {
1da177e4
LT
398 case 8:
399 outb(value, port);
400 break;
401 case 16:
402 outw(value, port);
403 break;
404 case 32:
405 outl(value, port);
406 break;
407 default:
408 BUG();
409 }
410
411 return AE_OK;
412}
4be44fcd 413
1da177e4
LT
414EXPORT_SYMBOL(acpi_os_write_port);
415
416acpi_status
4be44fcd 417acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
1da177e4 418{
4be44fcd
LB
419 u32 dummy;
420 void __iomem *virt_addr;
1da177e4 421
9f4fd61f 422 virt_addr = ioremap(phys_addr, width);
1da177e4
LT
423 if (!value)
424 value = &dummy;
425
426 switch (width) {
427 case 8:
4be44fcd 428 *(u8 *) value = readb(virt_addr);
1da177e4
LT
429 break;
430 case 16:
4be44fcd 431 *(u16 *) value = readw(virt_addr);
1da177e4
LT
432 break;
433 case 32:
4be44fcd 434 *(u32 *) value = readl(virt_addr);
1da177e4
LT
435 break;
436 default:
437 BUG();
438 }
439
9f4fd61f 440 iounmap(virt_addr);
1da177e4
LT
441
442 return AE_OK;
443}
444
445acpi_status
4be44fcd 446acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
1da177e4 447{
4be44fcd 448 void __iomem *virt_addr;
1da177e4 449
9f4fd61f 450 virt_addr = ioremap(phys_addr, width);
1da177e4
LT
451
452 switch (width) {
453 case 8:
454 writeb(value, virt_addr);
455 break;
456 case 16:
457 writew(value, virt_addr);
458 break;
459 case 32:
460 writel(value, virt_addr);
461 break;
462 default:
463 BUG();
464 }
465
9f4fd61f 466 iounmap(virt_addr);
1da177e4
LT
467
468 return AE_OK;
469}
470
1da177e4 471acpi_status
4be44fcd
LB
472acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
473 void *value, u32 width)
1da177e4
LT
474{
475 int result, size;
476
477 if (!value)
478 return AE_BAD_PARAMETER;
479
480 switch (width) {
481 case 8:
482 size = 1;
483 break;
484 case 16:
485 size = 2;
486 break;
487 case 32:
488 size = 4;
489 break;
490 default:
491 return AE_ERROR;
492 }
493
494 BUG_ON(!raw_pci_ops);
495
496 result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
4be44fcd
LB
497 PCI_DEVFN(pci_id->device, pci_id->function),
498 reg, size, value);
1da177e4
LT
499
500 return (result ? AE_ERROR : AE_OK);
501}
4be44fcd 502
1da177e4
LT
503EXPORT_SYMBOL(acpi_os_read_pci_configuration);
504
505acpi_status
4be44fcd
LB
506acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
507 acpi_integer value, u32 width)
1da177e4
LT
508{
509 int result, size;
510
511 switch (width) {
512 case 8:
513 size = 1;
514 break;
515 case 16:
516 size = 2;
517 break;
518 case 32:
519 size = 4;
520 break;
521 default:
522 return AE_ERROR;
523 }
524
525 BUG_ON(!raw_pci_ops);
526
527 result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
4be44fcd
LB
528 PCI_DEVFN(pci_id->device, pci_id->function),
529 reg, size, value);
1da177e4
LT
530
531 return (result ? AE_ERROR : AE_OK);
532}
533
534/* TODO: Change code to take advantage of driver model more */
4be44fcd
LB
535static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
536 acpi_handle chandle, /* current node */
537 struct acpi_pci_id **id,
538 int *is_bridge, u8 * bus_number)
1da177e4 539{
4be44fcd
LB
540 acpi_handle handle;
541 struct acpi_pci_id *pci_id = *id;
542 acpi_status status;
543 unsigned long temp;
544 acpi_object_type type;
545 u8 tu8;
1da177e4
LT
546
547 acpi_get_parent(chandle, &handle);
548 if (handle != rhandle) {
4be44fcd
LB
549 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
550 bus_number);
1da177e4
LT
551
552 status = acpi_get_type(handle, &type);
4be44fcd 553 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
1da177e4
LT
554 return;
555
4be44fcd
LB
556 status =
557 acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
558 &temp);
1da177e4 559 if (ACPI_SUCCESS(status)) {
4be44fcd
LB
560 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
561 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
1da177e4
LT
562
563 if (*is_bridge)
564 pci_id->bus = *bus_number;
565
566 /* any nicer way to get bus number of bridge ? */
4be44fcd
LB
567 status =
568 acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
569 8);
570 if (ACPI_SUCCESS(status)
571 && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
572 status =
573 acpi_os_read_pci_configuration(pci_id, 0x18,
574 &tu8, 8);
1da177e4
LT
575 if (!ACPI_SUCCESS(status)) {
576 /* Certainly broken... FIX ME */
577 return;
578 }
579 *is_bridge = 1;
580 pci_id->bus = tu8;
4be44fcd
LB
581 status =
582 acpi_os_read_pci_configuration(pci_id, 0x19,
583 &tu8, 8);
1da177e4
LT
584 if (ACPI_SUCCESS(status)) {
585 *bus_number = tu8;
586 }
587 } else
588 *is_bridge = 0;
589 }
590 }
591}
592
4be44fcd
LB
593void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
594 acpi_handle chandle, /* current node */
595 struct acpi_pci_id **id)
1da177e4
LT
596{
597 int is_bridge = 1;
598 u8 bus_number = (*id)->bus;
599
600 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
601}
602
65f27f38 603static void acpi_os_execute_deferred(struct work_struct *work)
1da177e4 604{
65f27f38 605 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
1da177e4 606
1da177e4 607 if (!dpc) {
6468463a 608 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
d550d98d 609 return;
1da177e4
LT
610 }
611
612 dpc->function(dpc->context);
613
614 kfree(dpc);
615
d550d98d 616 return;
1da177e4
LT
617}
618
b8d35192
AS
619/*******************************************************************************
620 *
621 * FUNCTION: acpi_os_execute
622 *
623 * PARAMETERS: Type - Type of the callback
624 * Function - Function to be executed
625 * Context - Function parameters
626 *
627 * RETURN: Status
628 *
629 * DESCRIPTION: Depending on type, either queues function for deferred execution or
630 * immediately executes function on a separate thread.
631 *
632 ******************************************************************************/
633
634acpi_status acpi_os_execute(acpi_execute_type type,
4be44fcd 635 acpi_osd_exec_callback function, void *context)
1da177e4 636{
4be44fcd
LB
637 acpi_status status = AE_OK;
638 struct acpi_os_dpc *dpc;
72945b2b 639
b976fe19
LT
640 ACPI_FUNCTION_TRACE("os_queue_for_execution");
641
72945b2b
LB
642 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
643 "Scheduling function [%p(%p)] for deferred execution.\n",
644 function, context));
1da177e4
LT
645
646 if (!function)
b976fe19 647 return_ACPI_STATUS(AE_BAD_PARAMETER);
72945b2b 648
1da177e4
LT
649 /*
650 * Allocate/initialize DPC structure. Note that this memory will be
65f27f38 651 * freed by the callee. The kernel handles the work_struct list in a
1da177e4
LT
652 * way that allows us to also free its memory inside the callee.
653 * Because we may want to schedule several tasks with different
654 * parameters we can't use the approach some kernel code uses of
65f27f38 655 * having a static work_struct.
1da177e4 656 */
72945b2b 657
65f27f38 658 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
1da177e4 659 if (!dpc)
b976fe19
LT
660 return_ACPI_STATUS(AE_NO_MEMORY);
661
1da177e4
LT
662 dpc->function = function;
663 dpc->context = context;
b976fe19 664
65f27f38
DH
665 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
666 if (!queue_work(kacpid_wq, &dpc->work)) {
b976fe19
LT
667 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
668 "Call to queue_work() failed.\n"));
37605a69 669 kfree(dpc);
b976fe19 670 status = AE_ERROR;
1da177e4 671 }
b976fe19
LT
672
673 return_ACPI_STATUS(status);
1da177e4 674}
4be44fcd 675
b8d35192 676EXPORT_SYMBOL(acpi_os_execute);
1da177e4 677
4be44fcd 678void acpi_os_wait_events_complete(void *context)
1da177e4
LT
679{
680 flush_workqueue(kacpid_wq);
681}
4be44fcd 682
1da177e4
LT
683EXPORT_SYMBOL(acpi_os_wait_events_complete);
684
685/*
686 * Allocate the memory for a spinlock and initialize it.
687 */
967440e3 688acpi_status acpi_os_create_lock(acpi_spinlock * handle)
1da177e4 689{
967440e3 690 spin_lock_init(*handle);
1da177e4 691
d550d98d 692 return AE_OK;
1da177e4
LT
693}
694
1da177e4
LT
695/*
696 * Deallocate the memory for a spinlock.
697 */
967440e3 698void acpi_os_delete_lock(acpi_spinlock handle)
1da177e4 699{
d550d98d 700 return;
1da177e4
LT
701}
702
1da177e4 703acpi_status
4be44fcd 704acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
1da177e4 705{
4be44fcd 706 struct semaphore *sem = NULL;
1da177e4 707
1da177e4
LT
708
709 sem = acpi_os_allocate(sizeof(struct semaphore));
710 if (!sem)
d550d98d 711 return AE_NO_MEMORY;
1da177e4
LT
712 memset(sem, 0, sizeof(struct semaphore));
713
714 sema_init(sem, initial_units);
715
4be44fcd 716 *handle = (acpi_handle *) sem;
1da177e4 717
4be44fcd
LB
718 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
719 *handle, initial_units));
1da177e4 720
d550d98d 721 return AE_OK;
1da177e4 722}
1da177e4 723
4be44fcd 724EXPORT_SYMBOL(acpi_os_create_semaphore);
1da177e4
LT
725
726/*
727 * TODO: A better way to delete semaphores? Linux doesn't have a
728 * 'delete_semaphore()' function -- may result in an invalid
729 * pointer dereference for non-synchronized consumers. Should
730 * we at least check for blocked threads and signal/cancel them?
731 */
732
4be44fcd 733acpi_status acpi_os_delete_semaphore(acpi_handle handle)
1da177e4 734{
4be44fcd 735 struct semaphore *sem = (struct semaphore *)handle;
1da177e4 736
1da177e4
LT
737
738 if (!sem)
d550d98d 739 return AE_BAD_PARAMETER;
1da177e4 740
4be44fcd 741 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
1da177e4 742
02438d87 743 kfree(sem);
4be44fcd 744 sem = NULL;
1da177e4 745
d550d98d 746 return AE_OK;
1da177e4 747}
1da177e4 748
4be44fcd 749EXPORT_SYMBOL(acpi_os_delete_semaphore);
1da177e4
LT
750
751/*
752 * TODO: The kernel doesn't have a 'down_timeout' function -- had to
753 * improvise. The process is to sleep for one scheduler quantum
754 * until the semaphore becomes available. Downside is that this
755 * may result in starvation for timeout-based waits when there's
756 * lots of semaphore activity.
757 *
758 * TODO: Support for units > 1?
759 */
4be44fcd 760acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
1da177e4 761{
4be44fcd
LB
762 acpi_status status = AE_OK;
763 struct semaphore *sem = (struct semaphore *)handle;
764 int ret = 0;
1da177e4 765
1da177e4
LT
766
767 if (!sem || (units < 1))
d550d98d 768 return AE_BAD_PARAMETER;
1da177e4
LT
769
770 if (units > 1)
d550d98d 771 return AE_SUPPORT;
1da177e4 772
4be44fcd
LB
773 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
774 handle, units, timeout));
1da177e4 775
d68909f4
LB
776 /*
777 * This can be called during resume with interrupts off.
778 * Like boot-time, we should be single threaded and will
779 * always get the lock if we try -- timeout or not.
780 * If this doesn't succeed, then we will oops courtesy of
781 * might_sleep() in down().
782 */
783 if (!down_trylock(sem))
784 return AE_OK;
785
4be44fcd 786 switch (timeout) {
1da177e4
LT
787 /*
788 * No Wait:
789 * --------
790 * A zero timeout value indicates that we shouldn't wait - just
791 * acquire the semaphore if available otherwise return AE_TIME
792 * (a.k.a. 'would block').
793 */
4be44fcd
LB
794 case 0:
795 if (down_trylock(sem))
1da177e4
LT
796 status = AE_TIME;
797 break;
798
799 /*
800 * Wait Indefinitely:
801 * ------------------
802 */
4be44fcd 803 case ACPI_WAIT_FOREVER:
1da177e4
LT
804 down(sem);
805 break;
806
807 /*
808 * Wait w/ Timeout:
809 * ----------------
810 */
4be44fcd 811 default:
1da177e4
LT
812 // TODO: A better timeout algorithm?
813 {
814 int i = 0;
4be44fcd 815 static const int quantum_ms = 1000 / HZ;
1da177e4
LT
816
817 ret = down_trylock(sem);
dacd9b80 818 for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
01a527ec 819 schedule_timeout_interruptible(1);
1da177e4
LT
820 ret = down_trylock(sem);
821 }
4be44fcd 822
1da177e4
LT
823 if (ret != 0)
824 status = AE_TIME;
825 }
826 break;
827 }
828
829 if (ACPI_FAILURE(status)) {
9e7e2c04 830 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
a6fc6720 831 "Failed to acquire semaphore[%p|%d|%d], %s",
4be44fcd
LB
832 handle, units, timeout,
833 acpi_format_exception(status)));
834 } else {
835 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
a6fc6720 836 "Acquired semaphore[%p|%d|%d]", handle,
4be44fcd 837 units, timeout));
1da177e4
LT
838 }
839
d550d98d 840 return status;
1da177e4 841}
1da177e4 842
4be44fcd 843EXPORT_SYMBOL(acpi_os_wait_semaphore);
1da177e4
LT
844
845/*
846 * TODO: Support for units > 1?
847 */
4be44fcd 848acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
1da177e4 849{
4be44fcd 850 struct semaphore *sem = (struct semaphore *)handle;
1da177e4 851
1da177e4
LT
852
853 if (!sem || (units < 1))
d550d98d 854 return AE_BAD_PARAMETER;
1da177e4
LT
855
856 if (units > 1)
d550d98d 857 return AE_SUPPORT;
1da177e4 858
4be44fcd
LB
859 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
860 units));
1da177e4
LT
861
862 up(sem);
863
d550d98d 864 return AE_OK;
1da177e4 865}
4be44fcd 866
1da177e4
LT
867EXPORT_SYMBOL(acpi_os_signal_semaphore);
868
869#ifdef ACPI_FUTURE_USAGE
4be44fcd 870u32 acpi_os_get_line(char *buffer)
1da177e4
LT
871{
872
873#ifdef ENABLE_DEBUGGER
874 if (acpi_in_debugger) {
875 u32 chars;
876
877 kdb_read(buffer, sizeof(line_buf));
878
879 /* remove the CR kdb includes */
880 chars = strlen(buffer) - 1;
881 buffer[chars] = '\0';
882 }
883#endif
884
885 return 0;
886}
4be44fcd 887#endif /* ACPI_FUTURE_USAGE */
1da177e4 888
4be44fcd 889acpi_status acpi_os_signal(u32 function, void *info)
1da177e4 890{
4be44fcd 891 switch (function) {
1da177e4
LT
892 case ACPI_SIGNAL_FATAL:
893 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
894 break;
895 case ACPI_SIGNAL_BREAKPOINT:
896 /*
897 * AML Breakpoint
898 * ACPI spec. says to treat it as a NOP unless
899 * you are debugging. So if/when we integrate
900 * AML debugger into the kernel debugger its
901 * hook will go here. But until then it is
902 * not useful to print anything on breakpoints.
903 */
904 break;
905 default:
906 break;
907 }
908
909 return AE_OK;
910}
4be44fcd 911
1da177e4
LT
912EXPORT_SYMBOL(acpi_os_signal);
913
4be44fcd 914static int __init acpi_os_name_setup(char *str)
1da177e4
LT
915{
916 char *p = acpi_os_name;
4be44fcd 917 int count = ACPI_MAX_OVERRIDE_LEN - 1;
1da177e4
LT
918
919 if (!str || !*str)
920 return 0;
921
922 for (; count-- && str && *str; str++) {
923 if (isalnum(*str) || *str == ' ' || *str == ':')
924 *p++ = *str;
925 else if (*str == '\'' || *str == '"')
926 continue;
927 else
928 break;
929 }
930 *p = 0;
931
932 return 1;
4be44fcd 933
1da177e4
LT
934}
935
936__setup("acpi_os_name=", acpi_os_name_setup);
937
938/*
939 * _OSI control
940 * empty string disables _OSI
941 * TBD additional string adds to _OSI
942 */
4be44fcd 943static int __init acpi_osi_setup(char *str)
1da177e4
LT
944{
945 if (str == NULL || *str == '\0') {
946 printk(KERN_INFO PREFIX "_OSI method disabled\n");
947 acpi_gbl_create_osi_method = FALSE;
4be44fcd 948 } else {
1da177e4 949 /* TBD */
4be44fcd
LB
950 printk(KERN_ERR PREFIX "_OSI additional string ignored -- %s\n",
951 str);
1da177e4
LT
952 }
953
954 return 1;
955}
956
957__setup("acpi_osi=", acpi_osi_setup);
958
959/* enable serialization to combat AE_ALREADY_EXISTS errors */
4be44fcd 960static int __init acpi_serialize_setup(char *str)
1da177e4
LT
961{
962 printk(KERN_INFO PREFIX "serialize enabled\n");
963
964 acpi_gbl_all_methods_serialized = TRUE;
965
966 return 1;
967}
968
969__setup("acpi_serialize", acpi_serialize_setup);
970
971/*
972 * Wake and Run-Time GPES are expected to be separate.
973 * We disable wake-GPEs at run-time to prevent spurious
974 * interrupts.
975 *
976 * However, if a system exists that shares Wake and
977 * Run-time events on the same GPE this flag is available
978 * to tell Linux to keep the wake-time GPEs enabled at run-time.
979 */
4be44fcd 980static int __init acpi_wake_gpes_always_on_setup(char *str)
1da177e4
LT
981{
982 printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
983
984 acpi_gbl_leave_wake_gpes_disabled = FALSE;
985
986 return 1;
987}
988
989__setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
990
991/*
992 * max_cstate is defined in the base kernel so modules can
993 * change it w/o depending on the state of the processor module.
994 */
995unsigned int max_cstate = ACPI_PROCESSOR_MAX_POWER;
996
1da177e4 997EXPORT_SYMBOL(max_cstate);
73459f73
RM
998
999/*
1000 * Acquire a spinlock.
1001 *
1002 * handle is a pointer to the spinlock_t.
73459f73
RM
1003 */
1004
967440e3 1005acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
73459f73 1006{
b8e4d893 1007 acpi_cpu_flags flags;
967440e3 1008 spin_lock_irqsave(lockp, flags);
73459f73
RM
1009 return flags;
1010}
1011
1012/*
1013 * Release a spinlock. See above.
1014 */
1015
967440e3 1016void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
73459f73 1017{
967440e3 1018 spin_unlock_irqrestore(lockp, flags);
73459f73
RM
1019}
1020
73459f73
RM
1021#ifndef ACPI_USE_LOCAL_CACHE
1022
1023/*******************************************************************************
1024 *
1025 * FUNCTION: acpi_os_create_cache
1026 *
b229cf92
BM
1027 * PARAMETERS: name - Ascii name for the cache
1028 * size - Size of each cached object
1029 * depth - Maximum depth of the cache (in objects) <ignored>
1030 * cache - Where the new cache object is returned
73459f73 1031 *
b229cf92 1032 * RETURN: status
73459f73
RM
1033 *
1034 * DESCRIPTION: Create a cache object
1035 *
1036 ******************************************************************************/
1037
1038acpi_status
4be44fcd 1039acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
73459f73 1040{
4be44fcd 1041 *cache = kmem_cache_create(name, size, 0, 0, NULL, NULL);
a6fdbf90 1042 if (*cache == NULL)
b229cf92
BM
1043 return AE_ERROR;
1044 else
1045 return AE_OK;
73459f73
RM
1046}
1047
1048/*******************************************************************************
1049 *
1050 * FUNCTION: acpi_os_purge_cache
1051 *
1052 * PARAMETERS: Cache - Handle to cache object
1053 *
1054 * RETURN: Status
1055 *
1056 * DESCRIPTION: Free all objects within the requested cache.
1057 *
1058 ******************************************************************************/
1059
4be44fcd 1060acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
73459f73 1061{
50dd0969 1062 kmem_cache_shrink(cache);
4be44fcd 1063 return (AE_OK);
73459f73
RM
1064}
1065
1066/*******************************************************************************
1067 *
1068 * FUNCTION: acpi_os_delete_cache
1069 *
1070 * PARAMETERS: Cache - Handle to cache object
1071 *
1072 * RETURN: Status
1073 *
1074 * DESCRIPTION: Free all objects within the requested cache and delete the
1075 * cache object.
1076 *
1077 ******************************************************************************/
1078
4be44fcd 1079acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
73459f73 1080{
1a1d92c1 1081 kmem_cache_destroy(cache);
4be44fcd 1082 return (AE_OK);
73459f73
RM
1083}
1084
1085/*******************************************************************************
1086 *
1087 * FUNCTION: acpi_os_release_object
1088 *
1089 * PARAMETERS: Cache - Handle to cache object
1090 * Object - The object to be released
1091 *
1092 * RETURN: None
1093 *
1094 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1095 * the object is deleted.
1096 *
1097 ******************************************************************************/
1098
4be44fcd 1099acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
73459f73 1100{
4be44fcd
LB
1101 kmem_cache_free(cache, object);
1102 return (AE_OK);
73459f73
RM
1103}
1104
b229cf92
BM
1105/******************************************************************************
1106 *
1107 * FUNCTION: acpi_os_validate_interface
1108 *
1109 * PARAMETERS: interface - Requested interface to be validated
1110 *
1111 * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
1112 *
1113 * DESCRIPTION: Match an interface string to the interfaces supported by the
1114 * host. Strings originate from an AML call to the _OSI method.
1115 *
1116 *****************************************************************************/
1117
1118acpi_status
1119acpi_os_validate_interface (char *interface)
1120{
1121
1122 return AE_SUPPORT;
1123}
1124
1125
1126/******************************************************************************
1127 *
1128 * FUNCTION: acpi_os_validate_address
1129 *
1130 * PARAMETERS: space_id - ACPI space ID
1131 * address - Physical address
1132 * length - Address length
1133 *
1134 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1135 * should return AE_AML_ILLEGAL_ADDRESS.
1136 *
1137 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1138 * the addresses accessed by AML operation regions.
1139 *
1140 *****************************************************************************/
1141
1142acpi_status
1143acpi_os_validate_address (
1144 u8 space_id,
1145 acpi_physical_address address,
1146 acpi_size length)
1147{
1148
1149 return AE_OK;
1150}
1151
1152
73459f73 1153#endif