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