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