V4L/DVB (12948): v4l1-compat: fix VIDIOC_G_STD handling
[linux/fpc-iii.git] / drivers / acpi / osl.c
blob8dff236f7ed360202ad7a2f6f48c6c3c731442a6
1 /*
2 * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
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 * Copyright (c) 2008 Intel Corporation
8 * Author: Matthew Wilcox <willy@linux.intel.com>
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
33 #include <linux/mm.h>
34 #include <linux/pci.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>
40 #include <linux/acpi.h>
41 #include <linux/efi.h>
42 #include <linux/ioport.h>
43 #include <linux/list.h>
44 #include <linux/jiffies.h>
45 #include <linux/semaphore.h>
47 #include <asm/io.h>
48 #include <asm/uaccess.h>
50 #include <acpi/acpi.h>
51 #include <acpi/acpi_bus.h>
52 #include <acpi/processor.h>
54 #define _COMPONENT ACPI_OS_SERVICES
55 ACPI_MODULE_NAME("osl");
56 #define PREFIX "ACPI: "
57 struct acpi_os_dpc {
58 acpi_osd_exec_callback function;
59 void *context;
60 struct work_struct work;
63 #ifdef CONFIG_ACPI_CUSTOM_DSDT
64 #include CONFIG_ACPI_CUSTOM_DSDT_FILE
65 #endif
67 #ifdef ENABLE_DEBUGGER
68 #include <linux/kdb.h>
70 /* stuff for debugger support */
71 int acpi_in_debugger;
72 EXPORT_SYMBOL(acpi_in_debugger);
74 extern char line_buf[80];
75 #endif /*ENABLE_DEBUGGER */
77 static unsigned int acpi_irq_irq;
78 static acpi_osd_handler acpi_irq_handler;
79 static void *acpi_irq_context;
80 static struct workqueue_struct *kacpid_wq;
81 static struct workqueue_struct *kacpi_notify_wq;
82 static struct workqueue_struct *kacpi_hotplug_wq;
84 struct acpi_res_list {
85 resource_size_t start;
86 resource_size_t end;
87 acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
88 char name[5]; /* only can have a length of 4 chars, make use of this
89 one instead of res->name, no need to kalloc then */
90 struct list_head resource_list;
93 static LIST_HEAD(resource_list_head);
94 static DEFINE_SPINLOCK(acpi_res_lock);
96 #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
97 static char osi_additional_string[OSI_STRING_LENGTH_MAX];
100 * The story of _OSI(Linux)
102 * From pre-history through Linux-2.6.22,
103 * Linux responded TRUE upon a BIOS OSI(Linux) query.
105 * Unfortunately, reference BIOS writers got wind of this
106 * and put OSI(Linux) in their example code, quickly exposing
107 * this string as ill-conceived and opening the door to
108 * an un-bounded number of BIOS incompatibilities.
110 * For example, OSI(Linux) was used on resume to re-POST a
111 * video card on one system, because Linux at that time
112 * could not do a speedy restore in its native driver.
113 * But then upon gaining quick native restore capability,
114 * Linux has no way to tell the BIOS to skip the time-consuming
115 * POST -- putting Linux at a permanent performance disadvantage.
116 * On another system, the BIOS writer used OSI(Linux)
117 * to infer native OS support for IPMI! On other systems,
118 * OSI(Linux) simply got in the way of Linux claiming to
119 * be compatible with other operating systems, exposing
120 * BIOS issues such as skipped device initialization.
122 * So "Linux" turned out to be a really poor chose of
123 * OSI string, and from Linux-2.6.23 onward we respond FALSE.
125 * BIOS writers should NOT query _OSI(Linux) on future systems.
126 * Linux will complain on the console when it sees it, and return FALSE.
127 * To get Linux to return TRUE for your system will require
128 * a kernel source update to add a DMI entry,
129 * or boot with "acpi_osi=Linux"
132 static struct osi_linux {
133 unsigned int enable:1;
134 unsigned int dmi:1;
135 unsigned int cmdline:1;
136 unsigned int known:1;
137 } osi_linux = { 0, 0, 0, 0};
139 static void __init acpi_request_region (struct acpi_generic_address *addr,
140 unsigned int length, char *desc)
142 struct resource *res;
144 if (!addr->address || !length)
145 return;
147 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
148 res = request_region(addr->address, length, desc);
149 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
150 res = request_mem_region(addr->address, length, desc);
153 static int __init acpi_reserve_resources(void)
155 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
156 "ACPI PM1a_EVT_BLK");
158 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
159 "ACPI PM1b_EVT_BLK");
161 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
162 "ACPI PM1a_CNT_BLK");
164 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
165 "ACPI PM1b_CNT_BLK");
167 if (acpi_gbl_FADT.pm_timer_length == 4)
168 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
170 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
171 "ACPI PM2_CNT_BLK");
173 /* Length of GPE blocks must be a non-negative multiple of 2 */
175 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
176 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
177 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
179 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
180 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
181 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
183 return 0;
185 device_initcall(acpi_reserve_resources);
187 acpi_status __init acpi_os_initialize(void)
189 return AE_OK;
192 static void bind_to_cpu0(struct work_struct *work)
194 set_cpus_allowed(current, cpumask_of_cpu(0));
195 kfree(work);
198 static void bind_workqueue(struct workqueue_struct *wq)
200 struct work_struct *work;
202 work = kzalloc(sizeof(struct work_struct), GFP_KERNEL);
203 INIT_WORK(work, bind_to_cpu0);
204 queue_work(wq, work);
207 acpi_status acpi_os_initialize1(void)
210 * On some machines, a software-initiated SMI causes corruption unless
211 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
212 * typically it's done in GPE-related methods that are run via
213 * workqueues, so we can avoid the known corruption cases by binding
214 * the workqueues to CPU 0.
216 kacpid_wq = create_singlethread_workqueue("kacpid");
217 bind_workqueue(kacpid_wq);
218 kacpi_notify_wq = create_singlethread_workqueue("kacpi_notify");
219 bind_workqueue(kacpi_notify_wq);
220 kacpi_hotplug_wq = create_singlethread_workqueue("kacpi_hotplug");
221 bind_workqueue(kacpi_hotplug_wq);
222 BUG_ON(!kacpid_wq);
223 BUG_ON(!kacpi_notify_wq);
224 BUG_ON(!kacpi_hotplug_wq);
225 return AE_OK;
228 acpi_status acpi_os_terminate(void)
230 if (acpi_irq_handler) {
231 acpi_os_remove_interrupt_handler(acpi_irq_irq,
232 acpi_irq_handler);
235 destroy_workqueue(kacpid_wq);
236 destroy_workqueue(kacpi_notify_wq);
237 destroy_workqueue(kacpi_hotplug_wq);
239 return AE_OK;
242 void acpi_os_printf(const char *fmt, ...)
244 va_list args;
245 va_start(args, fmt);
246 acpi_os_vprintf(fmt, args);
247 va_end(args);
250 void acpi_os_vprintf(const char *fmt, va_list args)
252 static char buffer[512];
254 vsprintf(buffer, fmt, args);
256 #ifdef ENABLE_DEBUGGER
257 if (acpi_in_debugger) {
258 kdb_printf("%s", buffer);
259 } else {
260 printk(KERN_CONT "%s", buffer);
262 #else
263 printk(KERN_CONT "%s", buffer);
264 #endif
267 acpi_physical_address __init acpi_os_get_root_pointer(void)
269 if (efi_enabled) {
270 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
271 return efi.acpi20;
272 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
273 return efi.acpi;
274 else {
275 printk(KERN_ERR PREFIX
276 "System description tables not found\n");
277 return 0;
279 } else {
280 acpi_physical_address pa = 0;
282 acpi_find_root_pointer(&pa);
283 return pa;
287 void __iomem *__init_refok
288 acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
290 if (phys > ULONG_MAX) {
291 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
292 return NULL;
294 if (acpi_gbl_permanent_mmap)
296 * ioremap checks to ensure this is in reserved space
298 return ioremap((unsigned long)phys, size);
299 else
300 return __acpi_map_table((unsigned long)phys, size);
302 EXPORT_SYMBOL_GPL(acpi_os_map_memory);
304 void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
306 if (acpi_gbl_permanent_mmap)
307 iounmap(virt);
308 else
309 __acpi_unmap_table(virt, size);
311 EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
313 void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
315 if (!acpi_gbl_permanent_mmap)
316 __acpi_unmap_table(virt, size);
319 #ifdef ACPI_FUTURE_USAGE
320 acpi_status
321 acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
323 if (!phys || !virt)
324 return AE_BAD_PARAMETER;
326 *phys = virt_to_phys(virt);
328 return AE_OK;
330 #endif
332 #define ACPI_MAX_OVERRIDE_LEN 100
334 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
336 acpi_status
337 acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
338 acpi_string * new_val)
340 if (!init_val || !new_val)
341 return AE_BAD_PARAMETER;
343 *new_val = NULL;
344 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
345 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
346 acpi_os_name);
347 *new_val = acpi_os_name;
350 return AE_OK;
353 acpi_status
354 acpi_os_table_override(struct acpi_table_header * existing_table,
355 struct acpi_table_header ** new_table)
357 if (!existing_table || !new_table)
358 return AE_BAD_PARAMETER;
360 *new_table = NULL;
362 #ifdef CONFIG_ACPI_CUSTOM_DSDT
363 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
364 *new_table = (struct acpi_table_header *)AmlCode;
365 #endif
366 if (*new_table != NULL) {
367 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
368 "this is unsafe: tainting kernel\n",
369 existing_table->signature,
370 existing_table->oem_table_id);
371 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
373 return AE_OK;
376 static irqreturn_t acpi_irq(int irq, void *dev_id)
378 u32 handled;
380 handled = (*acpi_irq_handler) (acpi_irq_context);
382 if (handled) {
383 acpi_irq_handled++;
384 return IRQ_HANDLED;
385 } else {
386 acpi_irq_not_handled++;
387 return IRQ_NONE;
391 acpi_status
392 acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
393 void *context)
395 unsigned int irq;
397 acpi_irq_stats_init();
400 * Ignore the GSI from the core, and use the value in our copy of the
401 * FADT. It may not be the same if an interrupt source override exists
402 * for the SCI.
404 gsi = acpi_gbl_FADT.sci_interrupt;
405 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
406 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
407 gsi);
408 return AE_OK;
411 acpi_irq_handler = handler;
412 acpi_irq_context = context;
413 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
414 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
415 return AE_NOT_ACQUIRED;
417 acpi_irq_irq = irq;
419 return AE_OK;
422 acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
424 if (irq) {
425 free_irq(irq, acpi_irq);
426 acpi_irq_handler = NULL;
427 acpi_irq_irq = 0;
430 return AE_OK;
434 * Running in interpreter thread context, safe to sleep
437 void acpi_os_sleep(acpi_integer ms)
439 schedule_timeout_interruptible(msecs_to_jiffies(ms));
442 void acpi_os_stall(u32 us)
444 while (us) {
445 u32 delay = 1000;
447 if (delay > us)
448 delay = us;
449 udelay(delay);
450 touch_nmi_watchdog();
451 us -= delay;
456 * Support ACPI 3.0 AML Timer operand
457 * Returns 64-bit free-running, monotonically increasing timer
458 * with 100ns granularity
460 u64 acpi_os_get_timer(void)
462 static u64 t;
464 #ifdef CONFIG_HPET
465 /* TBD: use HPET if available */
466 #endif
468 #ifdef CONFIG_X86_PM_TIMER
469 /* TBD: default to PM timer if HPET was not available */
470 #endif
471 if (!t)
472 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
474 return ++t;
477 acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
479 u32 dummy;
481 if (!value)
482 value = &dummy;
484 *value = 0;
485 if (width <= 8) {
486 *(u8 *) value = inb(port);
487 } else if (width <= 16) {
488 *(u16 *) value = inw(port);
489 } else if (width <= 32) {
490 *(u32 *) value = inl(port);
491 } else {
492 BUG();
495 return AE_OK;
498 EXPORT_SYMBOL(acpi_os_read_port);
500 acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
502 if (width <= 8) {
503 outb(value, port);
504 } else if (width <= 16) {
505 outw(value, port);
506 } else if (width <= 32) {
507 outl(value, port);
508 } else {
509 BUG();
512 return AE_OK;
515 EXPORT_SYMBOL(acpi_os_write_port);
517 acpi_status
518 acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
520 u32 dummy;
521 void __iomem *virt_addr;
523 virt_addr = ioremap(phys_addr, width);
524 if (!value)
525 value = &dummy;
527 switch (width) {
528 case 8:
529 *(u8 *) value = readb(virt_addr);
530 break;
531 case 16:
532 *(u16 *) value = readw(virt_addr);
533 break;
534 case 32:
535 *(u32 *) value = readl(virt_addr);
536 break;
537 default:
538 BUG();
541 iounmap(virt_addr);
543 return AE_OK;
546 acpi_status
547 acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
549 void __iomem *virt_addr;
551 virt_addr = ioremap(phys_addr, width);
553 switch (width) {
554 case 8:
555 writeb(value, virt_addr);
556 break;
557 case 16:
558 writew(value, virt_addr);
559 break;
560 case 32:
561 writel(value, virt_addr);
562 break;
563 default:
564 BUG();
567 iounmap(virt_addr);
569 return AE_OK;
572 acpi_status
573 acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
574 u32 *value, u32 width)
576 int result, size;
578 if (!value)
579 return AE_BAD_PARAMETER;
581 switch (width) {
582 case 8:
583 size = 1;
584 break;
585 case 16:
586 size = 2;
587 break;
588 case 32:
589 size = 4;
590 break;
591 default:
592 return AE_ERROR;
595 result = raw_pci_read(pci_id->segment, pci_id->bus,
596 PCI_DEVFN(pci_id->device, pci_id->function),
597 reg, size, value);
599 return (result ? AE_ERROR : AE_OK);
602 acpi_status
603 acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
604 acpi_integer value, u32 width)
606 int result, size;
608 switch (width) {
609 case 8:
610 size = 1;
611 break;
612 case 16:
613 size = 2;
614 break;
615 case 32:
616 size = 4;
617 break;
618 default:
619 return AE_ERROR;
622 result = raw_pci_write(pci_id->segment, pci_id->bus,
623 PCI_DEVFN(pci_id->device, pci_id->function),
624 reg, size, value);
626 return (result ? AE_ERROR : AE_OK);
629 /* TODO: Change code to take advantage of driver model more */
630 static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
631 acpi_handle chandle, /* current node */
632 struct acpi_pci_id **id,
633 int *is_bridge, u8 * bus_number)
635 acpi_handle handle;
636 struct acpi_pci_id *pci_id = *id;
637 acpi_status status;
638 unsigned long long temp;
639 acpi_object_type type;
641 acpi_get_parent(chandle, &handle);
642 if (handle != rhandle) {
643 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
644 bus_number);
646 status = acpi_get_type(handle, &type);
647 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
648 return;
650 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
651 &temp);
652 if (ACPI_SUCCESS(status)) {
653 u32 val;
654 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
655 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
657 if (*is_bridge)
658 pci_id->bus = *bus_number;
660 /* any nicer way to get bus number of bridge ? */
661 status =
662 acpi_os_read_pci_configuration(pci_id, 0x0e, &val,
664 if (ACPI_SUCCESS(status)
665 && ((val & 0x7f) == 1 || (val & 0x7f) == 2)) {
666 status =
667 acpi_os_read_pci_configuration(pci_id, 0x18,
668 &val, 8);
669 if (!ACPI_SUCCESS(status)) {
670 /* Certainly broken... FIX ME */
671 return;
673 *is_bridge = 1;
674 pci_id->bus = val;
675 status =
676 acpi_os_read_pci_configuration(pci_id, 0x19,
677 &val, 8);
678 if (ACPI_SUCCESS(status)) {
679 *bus_number = val;
681 } else
682 *is_bridge = 0;
687 void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
688 acpi_handle chandle, /* current node */
689 struct acpi_pci_id **id)
691 int is_bridge = 1;
692 u8 bus_number = (*id)->bus;
694 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
697 static void acpi_os_execute_deferred(struct work_struct *work)
699 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
700 if (!dpc) {
701 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
702 return;
705 dpc->function(dpc->context);
706 kfree(dpc);
708 return;
711 static void acpi_os_execute_hp_deferred(struct work_struct *work)
713 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
714 if (!dpc) {
715 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
716 return;
719 acpi_os_wait_events_complete(NULL);
721 dpc->function(dpc->context);
722 kfree(dpc);
724 return;
727 /*******************************************************************************
729 * FUNCTION: acpi_os_execute
731 * PARAMETERS: Type - Type of the callback
732 * Function - Function to be executed
733 * Context - Function parameters
735 * RETURN: Status
737 * DESCRIPTION: Depending on type, either queues function for deferred execution or
738 * immediately executes function on a separate thread.
740 ******************************************************************************/
742 static acpi_status __acpi_os_execute(acpi_execute_type type,
743 acpi_osd_exec_callback function, void *context, int hp)
745 acpi_status status = AE_OK;
746 struct acpi_os_dpc *dpc;
747 struct workqueue_struct *queue;
748 work_func_t func;
749 int ret;
750 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
751 "Scheduling function [%p(%p)] for deferred execution.\n",
752 function, context));
754 if (!function)
755 return AE_BAD_PARAMETER;
758 * Allocate/initialize DPC structure. Note that this memory will be
759 * freed by the callee. The kernel handles the work_struct list in a
760 * way that allows us to also free its memory inside the callee.
761 * Because we may want to schedule several tasks with different
762 * parameters we can't use the approach some kernel code uses of
763 * having a static work_struct.
766 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
767 if (!dpc)
768 return AE_NO_MEMORY;
770 dpc->function = function;
771 dpc->context = context;
774 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
775 * because the hotplug code may call driver .remove() functions,
776 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
777 * to flush these workqueues.
779 queue = hp ? kacpi_hotplug_wq :
780 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
781 func = hp ? acpi_os_execute_hp_deferred : acpi_os_execute_deferred;
782 INIT_WORK(&dpc->work, func);
783 ret = queue_work(queue, &dpc->work);
785 if (!ret) {
786 printk(KERN_ERR PREFIX
787 "Call to queue_work() failed.\n");
788 status = AE_ERROR;
789 kfree(dpc);
791 return status;
794 acpi_status acpi_os_execute(acpi_execute_type type,
795 acpi_osd_exec_callback function, void *context)
797 return __acpi_os_execute(type, function, context, 0);
799 EXPORT_SYMBOL(acpi_os_execute);
801 acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
802 void *context)
804 return __acpi_os_execute(0, function, context, 1);
807 void acpi_os_wait_events_complete(void *context)
809 flush_workqueue(kacpid_wq);
810 flush_workqueue(kacpi_notify_wq);
813 EXPORT_SYMBOL(acpi_os_wait_events_complete);
816 * Allocate the memory for a spinlock and initialize it.
818 acpi_status acpi_os_create_lock(acpi_spinlock * handle)
820 spin_lock_init(*handle);
822 return AE_OK;
826 * Deallocate the memory for a spinlock.
828 void acpi_os_delete_lock(acpi_spinlock handle)
830 return;
833 acpi_status
834 acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
836 struct semaphore *sem = NULL;
838 sem = acpi_os_allocate(sizeof(struct semaphore));
839 if (!sem)
840 return AE_NO_MEMORY;
841 memset(sem, 0, sizeof(struct semaphore));
843 sema_init(sem, initial_units);
845 *handle = (acpi_handle *) sem;
847 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
848 *handle, initial_units));
850 return AE_OK;
854 * TODO: A better way to delete semaphores? Linux doesn't have a
855 * 'delete_semaphore()' function -- may result in an invalid
856 * pointer dereference for non-synchronized consumers. Should
857 * we at least check for blocked threads and signal/cancel them?
860 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
862 struct semaphore *sem = (struct semaphore *)handle;
864 if (!sem)
865 return AE_BAD_PARAMETER;
867 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
869 BUG_ON(!list_empty(&sem->wait_list));
870 kfree(sem);
871 sem = NULL;
873 return AE_OK;
877 * TODO: Support for units > 1?
879 acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
881 acpi_status status = AE_OK;
882 struct semaphore *sem = (struct semaphore *)handle;
883 long jiffies;
884 int ret = 0;
886 if (!sem || (units < 1))
887 return AE_BAD_PARAMETER;
889 if (units > 1)
890 return AE_SUPPORT;
892 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
893 handle, units, timeout));
895 if (timeout == ACPI_WAIT_FOREVER)
896 jiffies = MAX_SCHEDULE_TIMEOUT;
897 else
898 jiffies = msecs_to_jiffies(timeout);
900 ret = down_timeout(sem, jiffies);
901 if (ret)
902 status = AE_TIME;
904 if (ACPI_FAILURE(status)) {
905 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
906 "Failed to acquire semaphore[%p|%d|%d], %s",
907 handle, units, timeout,
908 acpi_format_exception(status)));
909 } else {
910 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
911 "Acquired semaphore[%p|%d|%d]", handle,
912 units, timeout));
915 return status;
919 * TODO: Support for units > 1?
921 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
923 struct semaphore *sem = (struct semaphore *)handle;
925 if (!sem || (units < 1))
926 return AE_BAD_PARAMETER;
928 if (units > 1)
929 return AE_SUPPORT;
931 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
932 units));
934 up(sem);
936 return AE_OK;
939 #ifdef ACPI_FUTURE_USAGE
940 u32 acpi_os_get_line(char *buffer)
943 #ifdef ENABLE_DEBUGGER
944 if (acpi_in_debugger) {
945 u32 chars;
947 kdb_read(buffer, sizeof(line_buf));
949 /* remove the CR kdb includes */
950 chars = strlen(buffer) - 1;
951 buffer[chars] = '\0';
953 #endif
955 return 0;
957 #endif /* ACPI_FUTURE_USAGE */
959 acpi_status acpi_os_signal(u32 function, void *info)
961 switch (function) {
962 case ACPI_SIGNAL_FATAL:
963 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
964 break;
965 case ACPI_SIGNAL_BREAKPOINT:
967 * AML Breakpoint
968 * ACPI spec. says to treat it as a NOP unless
969 * you are debugging. So if/when we integrate
970 * AML debugger into the kernel debugger its
971 * hook will go here. But until then it is
972 * not useful to print anything on breakpoints.
974 break;
975 default:
976 break;
979 return AE_OK;
982 static int __init acpi_os_name_setup(char *str)
984 char *p = acpi_os_name;
985 int count = ACPI_MAX_OVERRIDE_LEN - 1;
987 if (!str || !*str)
988 return 0;
990 for (; count-- && str && *str; str++) {
991 if (isalnum(*str) || *str == ' ' || *str == ':')
992 *p++ = *str;
993 else if (*str == '\'' || *str == '"')
994 continue;
995 else
996 break;
998 *p = 0;
1000 return 1;
1004 __setup("acpi_os_name=", acpi_os_name_setup);
1006 static void __init set_osi_linux(unsigned int enable)
1008 if (osi_linux.enable != enable) {
1009 osi_linux.enable = enable;
1010 printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
1011 enable ? "Add": "Delet");
1013 return;
1016 static void __init acpi_cmdline_osi_linux(unsigned int enable)
1018 osi_linux.cmdline = 1; /* cmdline set the default */
1019 set_osi_linux(enable);
1021 return;
1024 void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1026 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
1028 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1030 if (enable == -1)
1031 return;
1033 osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
1035 set_osi_linux(enable);
1037 return;
1041 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1043 * empty string disables _OSI
1044 * string starting with '!' disables that string
1045 * otherwise string is added to list, augmenting built-in strings
1047 int __init acpi_osi_setup(char *str)
1049 if (str == NULL || *str == '\0') {
1050 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1051 acpi_gbl_create_osi_method = FALSE;
1052 } else if (!strcmp("!Linux", str)) {
1053 acpi_cmdline_osi_linux(0); /* !enable */
1054 } else if (*str == '!') {
1055 if (acpi_osi_invalidate(++str) == AE_OK)
1056 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1057 } else if (!strcmp("Linux", str)) {
1058 acpi_cmdline_osi_linux(1); /* enable */
1059 } else if (*osi_additional_string == '\0') {
1060 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
1061 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1064 return 1;
1067 __setup("acpi_osi=", acpi_osi_setup);
1069 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1070 static int __init acpi_serialize_setup(char *str)
1072 printk(KERN_INFO PREFIX "serialize enabled\n");
1074 acpi_gbl_all_methods_serialized = TRUE;
1076 return 1;
1079 __setup("acpi_serialize", acpi_serialize_setup);
1082 * Wake and Run-Time GPES are expected to be separate.
1083 * We disable wake-GPEs at run-time to prevent spurious
1084 * interrupts.
1086 * However, if a system exists that shares Wake and
1087 * Run-time events on the same GPE this flag is available
1088 * to tell Linux to keep the wake-time GPEs enabled at run-time.
1090 static int __init acpi_wake_gpes_always_on_setup(char *str)
1092 printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
1094 acpi_gbl_leave_wake_gpes_disabled = FALSE;
1096 return 1;
1099 __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
1101 /* Check of resource interference between native drivers and ACPI
1102 * OperationRegions (SystemIO and System Memory only).
1103 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1104 * in arbitrary AML code and can interfere with legacy drivers.
1105 * acpi_enforce_resources= can be set to:
1107 * - strict (default) (2)
1108 * -> further driver trying to access the resources will not load
1109 * - lax (1)
1110 * -> further driver trying to access the resources will load, but you
1111 * get a system message that something might go wrong...
1113 * - no (0)
1114 * -> ACPI Operation Region resources will not be registered
1117 #define ENFORCE_RESOURCES_STRICT 2
1118 #define ENFORCE_RESOURCES_LAX 1
1119 #define ENFORCE_RESOURCES_NO 0
1121 static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1123 static int __init acpi_enforce_resources_setup(char *str)
1125 if (str == NULL || *str == '\0')
1126 return 0;
1128 if (!strcmp("strict", str))
1129 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1130 else if (!strcmp("lax", str))
1131 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1132 else if (!strcmp("no", str))
1133 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1135 return 1;
1138 __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1140 /* Check for resource conflicts between ACPI OperationRegions and native
1141 * drivers */
1142 int acpi_check_resource_conflict(struct resource *res)
1144 struct acpi_res_list *res_list_elem;
1145 int ioport;
1146 int clash = 0;
1148 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1149 return 0;
1150 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1151 return 0;
1153 ioport = res->flags & IORESOURCE_IO;
1155 spin_lock(&acpi_res_lock);
1156 list_for_each_entry(res_list_elem, &resource_list_head,
1157 resource_list) {
1158 if (ioport && (res_list_elem->resource_type
1159 != ACPI_ADR_SPACE_SYSTEM_IO))
1160 continue;
1161 if (!ioport && (res_list_elem->resource_type
1162 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1163 continue;
1165 if (res->end < res_list_elem->start
1166 || res_list_elem->end < res->start)
1167 continue;
1168 clash = 1;
1169 break;
1171 spin_unlock(&acpi_res_lock);
1173 if (clash) {
1174 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
1175 printk("%sACPI: %s resource %s [0x%llx-0x%llx]"
1176 " conflicts with ACPI region %s"
1177 " [0x%llx-0x%llx]\n",
1178 acpi_enforce_resources == ENFORCE_RESOURCES_LAX
1179 ? KERN_WARNING : KERN_ERR,
1180 ioport ? "I/O" : "Memory", res->name,
1181 (long long) res->start, (long long) res->end,
1182 res_list_elem->name,
1183 (long long) res_list_elem->start,
1184 (long long) res_list_elem->end);
1185 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1186 printk(KERN_NOTICE "ACPI: This conflict may"
1187 " cause random problems and system"
1188 " instability\n");
1189 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1190 " for this device, you should use it instead of"
1191 " the native driver\n");
1193 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1194 return -EBUSY;
1196 return 0;
1198 EXPORT_SYMBOL(acpi_check_resource_conflict);
1200 int acpi_check_region(resource_size_t start, resource_size_t n,
1201 const char *name)
1203 struct resource res = {
1204 .start = start,
1205 .end = start + n - 1,
1206 .name = name,
1207 .flags = IORESOURCE_IO,
1210 return acpi_check_resource_conflict(&res);
1212 EXPORT_SYMBOL(acpi_check_region);
1214 int acpi_check_mem_region(resource_size_t start, resource_size_t n,
1215 const char *name)
1217 struct resource res = {
1218 .start = start,
1219 .end = start + n - 1,
1220 .name = name,
1221 .flags = IORESOURCE_MEM,
1224 return acpi_check_resource_conflict(&res);
1227 EXPORT_SYMBOL(acpi_check_mem_region);
1230 * Acquire a spinlock.
1232 * handle is a pointer to the spinlock_t.
1235 acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
1237 acpi_cpu_flags flags;
1238 spin_lock_irqsave(lockp, flags);
1239 return flags;
1243 * Release a spinlock. See above.
1246 void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
1248 spin_unlock_irqrestore(lockp, flags);
1251 #ifndef ACPI_USE_LOCAL_CACHE
1253 /*******************************************************************************
1255 * FUNCTION: acpi_os_create_cache
1257 * PARAMETERS: name - Ascii name for the cache
1258 * size - Size of each cached object
1259 * depth - Maximum depth of the cache (in objects) <ignored>
1260 * cache - Where the new cache object is returned
1262 * RETURN: status
1264 * DESCRIPTION: Create a cache object
1266 ******************************************************************************/
1268 acpi_status
1269 acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
1271 *cache = kmem_cache_create(name, size, 0, 0, NULL);
1272 if (*cache == NULL)
1273 return AE_ERROR;
1274 else
1275 return AE_OK;
1278 /*******************************************************************************
1280 * FUNCTION: acpi_os_purge_cache
1282 * PARAMETERS: Cache - Handle to cache object
1284 * RETURN: Status
1286 * DESCRIPTION: Free all objects within the requested cache.
1288 ******************************************************************************/
1290 acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
1292 kmem_cache_shrink(cache);
1293 return (AE_OK);
1296 /*******************************************************************************
1298 * FUNCTION: acpi_os_delete_cache
1300 * PARAMETERS: Cache - Handle to cache object
1302 * RETURN: Status
1304 * DESCRIPTION: Free all objects within the requested cache and delete the
1305 * cache object.
1307 ******************************************************************************/
1309 acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
1311 kmem_cache_destroy(cache);
1312 return (AE_OK);
1315 /*******************************************************************************
1317 * FUNCTION: acpi_os_release_object
1319 * PARAMETERS: Cache - Handle to cache object
1320 * Object - The object to be released
1322 * RETURN: None
1324 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1325 * the object is deleted.
1327 ******************************************************************************/
1329 acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
1331 kmem_cache_free(cache, object);
1332 return (AE_OK);
1335 /******************************************************************************
1337 * FUNCTION: acpi_os_validate_interface
1339 * PARAMETERS: interface - Requested interface to be validated
1341 * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
1343 * DESCRIPTION: Match an interface string to the interfaces supported by the
1344 * host. Strings originate from an AML call to the _OSI method.
1346 *****************************************************************************/
1348 acpi_status
1349 acpi_os_validate_interface (char *interface)
1351 if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1352 return AE_OK;
1353 if (!strcmp("Linux", interface)) {
1355 printk(KERN_NOTICE PREFIX
1356 "BIOS _OSI(Linux) query %s%s\n",
1357 osi_linux.enable ? "honored" : "ignored",
1358 osi_linux.cmdline ? " via cmdline" :
1359 osi_linux.dmi ? " via DMI" : "");
1361 if (osi_linux.enable)
1362 return AE_OK;
1364 return AE_SUPPORT;
1367 /******************************************************************************
1369 * FUNCTION: acpi_os_validate_address
1371 * PARAMETERS: space_id - ACPI space ID
1372 * address - Physical address
1373 * length - Address length
1375 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1376 * should return AE_AML_ILLEGAL_ADDRESS.
1378 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1379 * the addresses accessed by AML operation regions.
1381 *****************************************************************************/
1383 acpi_status
1384 acpi_os_validate_address (
1385 u8 space_id,
1386 acpi_physical_address address,
1387 acpi_size length,
1388 char *name)
1390 struct acpi_res_list *res;
1391 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1392 return AE_OK;
1394 switch (space_id) {
1395 case ACPI_ADR_SPACE_SYSTEM_IO:
1396 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1397 /* Only interference checks against SystemIO and SytemMemory
1398 are needed */
1399 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1400 if (!res)
1401 return AE_OK;
1402 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1403 strlcpy(res->name, name, 5);
1404 res->start = address;
1405 res->end = address + length - 1;
1406 res->resource_type = space_id;
1407 spin_lock(&acpi_res_lock);
1408 list_add(&res->resource_list, &resource_list_head);
1409 spin_unlock(&acpi_res_lock);
1410 pr_debug("Added %s resource: start: 0x%llx, end: 0x%llx, "
1411 "name: %s\n", (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
1412 ? "SystemIO" : "System Memory",
1413 (unsigned long long)res->start,
1414 (unsigned long long)res->end,
1415 res->name);
1416 break;
1417 case ACPI_ADR_SPACE_PCI_CONFIG:
1418 case ACPI_ADR_SPACE_EC:
1419 case ACPI_ADR_SPACE_SMBUS:
1420 case ACPI_ADR_SPACE_CMOS:
1421 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1422 case ACPI_ADR_SPACE_DATA_TABLE:
1423 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1424 break;
1426 return AE_OK;
1429 #endif