[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / acpi / osl.c
blob7c1c59ea9ec68368d2056a07fbc82b9e717557b9
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;
61 int wait;
64 #ifdef CONFIG_ACPI_CUSTOM_DSDT
65 #include CONFIG_ACPI_CUSTOM_DSDT_FILE
66 #endif
68 #ifdef ENABLE_DEBUGGER
69 #include <linux/kdb.h>
71 /* stuff for debugger support */
72 int acpi_in_debugger;
73 EXPORT_SYMBOL(acpi_in_debugger);
75 extern char line_buf[80];
76 #endif /*ENABLE_DEBUGGER */
78 static unsigned int acpi_irq_irq;
79 static acpi_osd_handler acpi_irq_handler;
80 static void *acpi_irq_context;
81 static struct workqueue_struct *kacpid_wq;
82 static struct workqueue_struct *kacpi_notify_wq;
83 static struct workqueue_struct *kacpi_hotplug_wq;
85 struct acpi_res_list {
86 resource_size_t start;
87 resource_size_t end;
88 acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
89 char name[5]; /* only can have a length of 4 chars, make use of this
90 one instead of res->name, no need to kalloc then */
91 struct list_head resource_list;
92 int count;
95 static LIST_HEAD(resource_list_head);
96 static DEFINE_SPINLOCK(acpi_res_lock);
98 #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
99 static char osi_additional_string[OSI_STRING_LENGTH_MAX];
102 * The story of _OSI(Linux)
104 * From pre-history through Linux-2.6.22,
105 * Linux responded TRUE upon a BIOS OSI(Linux) query.
107 * Unfortunately, reference BIOS writers got wind of this
108 * and put OSI(Linux) in their example code, quickly exposing
109 * this string as ill-conceived and opening the door to
110 * an un-bounded number of BIOS incompatibilities.
112 * For example, OSI(Linux) was used on resume to re-POST a
113 * video card on one system, because Linux at that time
114 * could not do a speedy restore in its native driver.
115 * But then upon gaining quick native restore capability,
116 * Linux has no way to tell the BIOS to skip the time-consuming
117 * POST -- putting Linux at a permanent performance disadvantage.
118 * On another system, the BIOS writer used OSI(Linux)
119 * to infer native OS support for IPMI! On other systems,
120 * OSI(Linux) simply got in the way of Linux claiming to
121 * be compatible with other operating systems, exposing
122 * BIOS issues such as skipped device initialization.
124 * So "Linux" turned out to be a really poor chose of
125 * OSI string, and from Linux-2.6.23 onward we respond FALSE.
127 * BIOS writers should NOT query _OSI(Linux) on future systems.
128 * Linux will complain on the console when it sees it, and return FALSE.
129 * To get Linux to return TRUE for your system will require
130 * a kernel source update to add a DMI entry,
131 * or boot with "acpi_osi=Linux"
134 static struct osi_linux {
135 unsigned int enable:1;
136 unsigned int dmi:1;
137 unsigned int cmdline:1;
138 unsigned int known:1;
139 } osi_linux = { 0, 0, 0, 0};
141 static void __init acpi_request_region (struct acpi_generic_address *addr,
142 unsigned int length, char *desc)
144 struct resource *res;
146 if (!addr->address || !length)
147 return;
149 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
150 res = request_region(addr->address, length, desc);
151 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
152 res = request_mem_region(addr->address, length, desc);
155 static int __init acpi_reserve_resources(void)
157 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
158 "ACPI PM1a_EVT_BLK");
160 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
161 "ACPI PM1b_EVT_BLK");
163 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
164 "ACPI PM1a_CNT_BLK");
166 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
167 "ACPI PM1b_CNT_BLK");
169 if (acpi_gbl_FADT.pm_timer_length == 4)
170 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
172 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
173 "ACPI PM2_CNT_BLK");
175 /* Length of GPE blocks must be a non-negative multiple of 2 */
177 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
178 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
179 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
181 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
182 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
183 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
185 return 0;
187 device_initcall(acpi_reserve_resources);
189 acpi_status __init acpi_os_initialize(void)
191 return AE_OK;
194 static void bind_to_cpu0(struct work_struct *work)
196 set_cpus_allowed_ptr(current, cpumask_of(0));
197 kfree(work);
200 static void bind_workqueue(struct workqueue_struct *wq)
202 struct work_struct *work;
204 work = kzalloc(sizeof(struct work_struct), GFP_KERNEL);
205 INIT_WORK(work, bind_to_cpu0);
206 queue_work(wq, work);
209 acpi_status acpi_os_initialize1(void)
212 * On some machines, a software-initiated SMI causes corruption unless
213 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
214 * typically it's done in GPE-related methods that are run via
215 * workqueues, so we can avoid the known corruption cases by binding
216 * the workqueues to CPU 0.
218 kacpid_wq = create_singlethread_workqueue("kacpid");
219 bind_workqueue(kacpid_wq);
220 kacpi_notify_wq = create_singlethread_workqueue("kacpi_notify");
221 bind_workqueue(kacpi_notify_wq);
222 kacpi_hotplug_wq = create_singlethread_workqueue("kacpi_hotplug");
223 bind_workqueue(kacpi_hotplug_wq);
224 BUG_ON(!kacpid_wq);
225 BUG_ON(!kacpi_notify_wq);
226 BUG_ON(!kacpi_hotplug_wq);
227 return AE_OK;
230 acpi_status acpi_os_terminate(void)
232 if (acpi_irq_handler) {
233 acpi_os_remove_interrupt_handler(acpi_irq_irq,
234 acpi_irq_handler);
237 destroy_workqueue(kacpid_wq);
238 destroy_workqueue(kacpi_notify_wq);
239 destroy_workqueue(kacpi_hotplug_wq);
241 return AE_OK;
244 void acpi_os_printf(const char *fmt, ...)
246 va_list args;
247 va_start(args, fmt);
248 acpi_os_vprintf(fmt, args);
249 va_end(args);
252 void acpi_os_vprintf(const char *fmt, va_list args)
254 static char buffer[512];
256 vsprintf(buffer, fmt, args);
258 #ifdef ENABLE_DEBUGGER
259 if (acpi_in_debugger) {
260 kdb_printf("%s", buffer);
261 } else {
262 printk(KERN_CONT "%s", buffer);
264 #else
265 printk(KERN_CONT "%s", buffer);
266 #endif
269 acpi_physical_address __init acpi_os_get_root_pointer(void)
271 if (efi_enabled) {
272 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
273 return efi.acpi20;
274 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
275 return efi.acpi;
276 else {
277 printk(KERN_ERR PREFIX
278 "System description tables not found\n");
279 return 0;
281 } else {
282 acpi_physical_address pa = 0;
284 acpi_find_root_pointer(&pa);
285 return pa;
289 void __iomem *__init_refok
290 acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
292 if (phys > ULONG_MAX) {
293 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
294 return NULL;
296 if (acpi_gbl_permanent_mmap)
298 * ioremap checks to ensure this is in reserved space
300 return ioremap((unsigned long)phys, size);
301 else
302 return __acpi_map_table((unsigned long)phys, size);
304 EXPORT_SYMBOL_GPL(acpi_os_map_memory);
306 void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
308 if (acpi_gbl_permanent_mmap)
309 iounmap(virt);
310 else
311 __acpi_unmap_table(virt, size);
313 EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
315 void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
317 if (!acpi_gbl_permanent_mmap)
318 __acpi_unmap_table(virt, size);
321 #ifdef ACPI_FUTURE_USAGE
322 acpi_status
323 acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
325 if (!phys || !virt)
326 return AE_BAD_PARAMETER;
328 *phys = virt_to_phys(virt);
330 return AE_OK;
332 #endif
334 #define ACPI_MAX_OVERRIDE_LEN 100
336 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
338 acpi_status
339 acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
340 acpi_string * new_val)
342 if (!init_val || !new_val)
343 return AE_BAD_PARAMETER;
345 *new_val = NULL;
346 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
347 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
348 acpi_os_name);
349 *new_val = acpi_os_name;
352 return AE_OK;
355 acpi_status
356 acpi_os_table_override(struct acpi_table_header * existing_table,
357 struct acpi_table_header ** new_table)
359 if (!existing_table || !new_table)
360 return AE_BAD_PARAMETER;
362 *new_table = NULL;
364 #ifdef CONFIG_ACPI_CUSTOM_DSDT
365 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
366 *new_table = (struct acpi_table_header *)AmlCode;
367 #endif
368 if (*new_table != NULL) {
369 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
370 "this is unsafe: tainting kernel\n",
371 existing_table->signature,
372 existing_table->oem_table_id);
373 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
375 return AE_OK;
378 static irqreturn_t acpi_irq(int irq, void *dev_id)
380 u32 handled;
382 handled = (*acpi_irq_handler) (acpi_irq_context);
384 if (handled) {
385 acpi_irq_handled++;
386 return IRQ_HANDLED;
387 } else {
388 acpi_irq_not_handled++;
389 return IRQ_NONE;
393 acpi_status
394 acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
395 void *context)
397 unsigned int irq;
399 acpi_irq_stats_init();
402 * Ignore the GSI from the core, and use the value in our copy of the
403 * FADT. It may not be the same if an interrupt source override exists
404 * for the SCI.
406 gsi = acpi_gbl_FADT.sci_interrupt;
407 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
408 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
409 gsi);
410 return AE_OK;
413 acpi_irq_handler = handler;
414 acpi_irq_context = context;
415 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
416 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
417 return AE_NOT_ACQUIRED;
419 acpi_irq_irq = irq;
421 return AE_OK;
424 acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
426 if (irq) {
427 free_irq(irq, acpi_irq);
428 acpi_irq_handler = NULL;
429 acpi_irq_irq = 0;
432 return AE_OK;
436 * Running in interpreter thread context, safe to sleep
439 void acpi_os_sleep(acpi_integer ms)
441 schedule_timeout_interruptible(msecs_to_jiffies(ms));
444 void acpi_os_stall(u32 us)
446 while (us) {
447 u32 delay = 1000;
449 if (delay > us)
450 delay = us;
451 udelay(delay);
452 touch_nmi_watchdog();
453 us -= delay;
458 * Support ACPI 3.0 AML Timer operand
459 * Returns 64-bit free-running, monotonically increasing timer
460 * with 100ns granularity
462 u64 acpi_os_get_timer(void)
464 static u64 t;
466 #ifdef CONFIG_HPET
467 /* TBD: use HPET if available */
468 #endif
470 #ifdef CONFIG_X86_PM_TIMER
471 /* TBD: default to PM timer if HPET was not available */
472 #endif
473 if (!t)
474 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
476 return ++t;
479 acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
481 u32 dummy;
483 if (!value)
484 value = &dummy;
486 *value = 0;
487 if (width <= 8) {
488 *(u8 *) value = inb(port);
489 } else if (width <= 16) {
490 *(u16 *) value = inw(port);
491 } else if (width <= 32) {
492 *(u32 *) value = inl(port);
493 } else {
494 BUG();
497 return AE_OK;
500 EXPORT_SYMBOL(acpi_os_read_port);
502 acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
504 if (width <= 8) {
505 outb(value, port);
506 } else if (width <= 16) {
507 outw(value, port);
508 } else if (width <= 32) {
509 outl(value, port);
510 } else {
511 BUG();
514 return AE_OK;
517 EXPORT_SYMBOL(acpi_os_write_port);
519 acpi_status
520 acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
522 u32 dummy;
523 void __iomem *virt_addr;
525 virt_addr = ioremap(phys_addr, width);
526 if (!value)
527 value = &dummy;
529 switch (width) {
530 case 8:
531 *(u8 *) value = readb(virt_addr);
532 break;
533 case 16:
534 *(u16 *) value = readw(virt_addr);
535 break;
536 case 32:
537 *(u32 *) value = readl(virt_addr);
538 break;
539 default:
540 BUG();
543 iounmap(virt_addr);
545 return AE_OK;
548 acpi_status
549 acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
551 void __iomem *virt_addr;
553 virt_addr = ioremap(phys_addr, width);
555 switch (width) {
556 case 8:
557 writeb(value, virt_addr);
558 break;
559 case 16:
560 writew(value, virt_addr);
561 break;
562 case 32:
563 writel(value, virt_addr);
564 break;
565 default:
566 BUG();
569 iounmap(virt_addr);
571 return AE_OK;
574 acpi_status
575 acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
576 u32 *value, u32 width)
578 int result, size;
580 if (!value)
581 return AE_BAD_PARAMETER;
583 switch (width) {
584 case 8:
585 size = 1;
586 break;
587 case 16:
588 size = 2;
589 break;
590 case 32:
591 size = 4;
592 break;
593 default:
594 return AE_ERROR;
597 result = raw_pci_read(pci_id->segment, pci_id->bus,
598 PCI_DEVFN(pci_id->device, pci_id->function),
599 reg, size, value);
601 return (result ? AE_ERROR : AE_OK);
604 acpi_status
605 acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
606 acpi_integer value, u32 width)
608 int result, size;
610 switch (width) {
611 case 8:
612 size = 1;
613 break;
614 case 16:
615 size = 2;
616 break;
617 case 32:
618 size = 4;
619 break;
620 default:
621 return AE_ERROR;
624 result = raw_pci_write(pci_id->segment, pci_id->bus,
625 PCI_DEVFN(pci_id->device, pci_id->function),
626 reg, size, value);
628 return (result ? AE_ERROR : AE_OK);
631 /* TODO: Change code to take advantage of driver model more */
632 static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
633 acpi_handle chandle, /* current node */
634 struct acpi_pci_id **id,
635 int *is_bridge, u8 * bus_number)
637 acpi_handle handle;
638 struct acpi_pci_id *pci_id = *id;
639 acpi_status status;
640 unsigned long long temp;
641 acpi_object_type type;
643 acpi_get_parent(chandle, &handle);
644 if (handle != rhandle) {
645 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
646 bus_number);
648 status = acpi_get_type(handle, &type);
649 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
650 return;
652 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
653 &temp);
654 if (ACPI_SUCCESS(status)) {
655 u32 val;
656 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
657 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
659 if (*is_bridge)
660 pci_id->bus = *bus_number;
662 /* any nicer way to get bus number of bridge ? */
663 status =
664 acpi_os_read_pci_configuration(pci_id, 0x0e, &val,
666 if (ACPI_SUCCESS(status)
667 && ((val & 0x7f) == 1 || (val & 0x7f) == 2)) {
668 status =
669 acpi_os_read_pci_configuration(pci_id, 0x18,
670 &val, 8);
671 if (!ACPI_SUCCESS(status)) {
672 /* Certainly broken... FIX ME */
673 return;
675 *is_bridge = 1;
676 pci_id->bus = val;
677 status =
678 acpi_os_read_pci_configuration(pci_id, 0x19,
679 &val, 8);
680 if (ACPI_SUCCESS(status)) {
681 *bus_number = val;
683 } else
684 *is_bridge = 0;
689 void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
690 acpi_handle chandle, /* current node */
691 struct acpi_pci_id **id)
693 int is_bridge = 1;
694 u8 bus_number = (*id)->bus;
696 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
699 static void acpi_os_execute_deferred(struct work_struct *work)
701 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
703 if (dpc->wait)
704 acpi_os_wait_events_complete(NULL);
706 dpc->function(dpc->context);
707 kfree(dpc);
710 /*******************************************************************************
712 * FUNCTION: acpi_os_execute
714 * PARAMETERS: Type - Type of the callback
715 * Function - Function to be executed
716 * Context - Function parameters
718 * RETURN: Status
720 * DESCRIPTION: Depending on type, either queues function for deferred execution or
721 * immediately executes function on a separate thread.
723 ******************************************************************************/
725 static acpi_status __acpi_os_execute(acpi_execute_type type,
726 acpi_osd_exec_callback function, void *context, int hp)
728 acpi_status status = AE_OK;
729 struct acpi_os_dpc *dpc;
730 struct workqueue_struct *queue;
731 int ret;
732 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
733 "Scheduling function [%p(%p)] for deferred execution.\n",
734 function, context));
737 * Allocate/initialize DPC structure. Note that this memory will be
738 * freed by the callee. The kernel handles the work_struct list in a
739 * way that allows us to also free its memory inside the callee.
740 * Because we may want to schedule several tasks with different
741 * parameters we can't use the approach some kernel code uses of
742 * having a static work_struct.
745 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
746 if (!dpc)
747 return AE_NO_MEMORY;
749 dpc->function = function;
750 dpc->context = context;
753 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
754 * because the hotplug code may call driver .remove() functions,
755 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
756 * to flush these workqueues.
758 queue = hp ? kacpi_hotplug_wq :
759 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
760 dpc->wait = hp ? 1 : 0;
761 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
762 ret = queue_work(queue, &dpc->work);
764 if (!ret) {
765 printk(KERN_ERR PREFIX
766 "Call to queue_work() failed.\n");
767 status = AE_ERROR;
768 kfree(dpc);
770 return status;
773 acpi_status acpi_os_execute(acpi_execute_type type,
774 acpi_osd_exec_callback function, void *context)
776 return __acpi_os_execute(type, function, context, 0);
778 EXPORT_SYMBOL(acpi_os_execute);
780 acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
781 void *context)
783 return __acpi_os_execute(0, function, context, 1);
786 void acpi_os_wait_events_complete(void *context)
788 flush_workqueue(kacpid_wq);
789 flush_workqueue(kacpi_notify_wq);
792 EXPORT_SYMBOL(acpi_os_wait_events_complete);
795 * Allocate the memory for a spinlock and initialize it.
797 acpi_status acpi_os_create_lock(acpi_spinlock * handle)
799 spin_lock_init(*handle);
801 return AE_OK;
805 * Deallocate the memory for a spinlock.
807 void acpi_os_delete_lock(acpi_spinlock handle)
809 return;
812 acpi_status
813 acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
815 struct semaphore *sem = NULL;
817 sem = acpi_os_allocate(sizeof(struct semaphore));
818 if (!sem)
819 return AE_NO_MEMORY;
820 memset(sem, 0, sizeof(struct semaphore));
822 sema_init(sem, initial_units);
824 *handle = (acpi_handle *) sem;
826 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
827 *handle, initial_units));
829 return AE_OK;
833 * TODO: A better way to delete semaphores? Linux doesn't have a
834 * 'delete_semaphore()' function -- may result in an invalid
835 * pointer dereference for non-synchronized consumers. Should
836 * we at least check for blocked threads and signal/cancel them?
839 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
841 struct semaphore *sem = (struct semaphore *)handle;
843 if (!sem)
844 return AE_BAD_PARAMETER;
846 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
848 BUG_ON(!list_empty(&sem->wait_list));
849 kfree(sem);
850 sem = NULL;
852 return AE_OK;
856 * TODO: Support for units > 1?
858 acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
860 acpi_status status = AE_OK;
861 struct semaphore *sem = (struct semaphore *)handle;
862 long jiffies;
863 int ret = 0;
865 if (!sem || (units < 1))
866 return AE_BAD_PARAMETER;
868 if (units > 1)
869 return AE_SUPPORT;
871 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
872 handle, units, timeout));
874 if (timeout == ACPI_WAIT_FOREVER)
875 jiffies = MAX_SCHEDULE_TIMEOUT;
876 else
877 jiffies = msecs_to_jiffies(timeout);
879 ret = down_timeout(sem, jiffies);
880 if (ret)
881 status = AE_TIME;
883 if (ACPI_FAILURE(status)) {
884 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
885 "Failed to acquire semaphore[%p|%d|%d], %s",
886 handle, units, timeout,
887 acpi_format_exception(status)));
888 } else {
889 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
890 "Acquired semaphore[%p|%d|%d]", handle,
891 units, timeout));
894 return status;
898 * TODO: Support for units > 1?
900 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
902 struct semaphore *sem = (struct semaphore *)handle;
904 if (!sem || (units < 1))
905 return AE_BAD_PARAMETER;
907 if (units > 1)
908 return AE_SUPPORT;
910 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
911 units));
913 up(sem);
915 return AE_OK;
918 #ifdef ACPI_FUTURE_USAGE
919 u32 acpi_os_get_line(char *buffer)
922 #ifdef ENABLE_DEBUGGER
923 if (acpi_in_debugger) {
924 u32 chars;
926 kdb_read(buffer, sizeof(line_buf));
928 /* remove the CR kdb includes */
929 chars = strlen(buffer) - 1;
930 buffer[chars] = '\0';
932 #endif
934 return 0;
936 #endif /* ACPI_FUTURE_USAGE */
938 acpi_status acpi_os_signal(u32 function, void *info)
940 switch (function) {
941 case ACPI_SIGNAL_FATAL:
942 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
943 break;
944 case ACPI_SIGNAL_BREAKPOINT:
946 * AML Breakpoint
947 * ACPI spec. says to treat it as a NOP unless
948 * you are debugging. So if/when we integrate
949 * AML debugger into the kernel debugger its
950 * hook will go here. But until then it is
951 * not useful to print anything on breakpoints.
953 break;
954 default:
955 break;
958 return AE_OK;
961 static int __init acpi_os_name_setup(char *str)
963 char *p = acpi_os_name;
964 int count = ACPI_MAX_OVERRIDE_LEN - 1;
966 if (!str || !*str)
967 return 0;
969 for (; count-- && str && *str; str++) {
970 if (isalnum(*str) || *str == ' ' || *str == ':')
971 *p++ = *str;
972 else if (*str == '\'' || *str == '"')
973 continue;
974 else
975 break;
977 *p = 0;
979 return 1;
983 __setup("acpi_os_name=", acpi_os_name_setup);
985 static void __init set_osi_linux(unsigned int enable)
987 if (osi_linux.enable != enable) {
988 osi_linux.enable = enable;
989 printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
990 enable ? "Add": "Delet");
992 return;
995 static void __init acpi_cmdline_osi_linux(unsigned int enable)
997 osi_linux.cmdline = 1; /* cmdline set the default */
998 set_osi_linux(enable);
1000 return;
1003 void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1005 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
1007 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1009 if (enable == -1)
1010 return;
1012 osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
1014 set_osi_linux(enable);
1016 return;
1020 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1022 * empty string disables _OSI
1023 * string starting with '!' disables that string
1024 * otherwise string is added to list, augmenting built-in strings
1026 int __init acpi_osi_setup(char *str)
1028 if (str == NULL || *str == '\0') {
1029 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1030 acpi_gbl_create_osi_method = FALSE;
1031 } else if (!strcmp("!Linux", str)) {
1032 acpi_cmdline_osi_linux(0); /* !enable */
1033 } else if (*str == '!') {
1034 if (acpi_osi_invalidate(++str) == AE_OK)
1035 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1036 } else if (!strcmp("Linux", str)) {
1037 acpi_cmdline_osi_linux(1); /* enable */
1038 } else if (*osi_additional_string == '\0') {
1039 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
1040 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1043 return 1;
1046 __setup("acpi_osi=", acpi_osi_setup);
1048 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1049 static int __init acpi_serialize_setup(char *str)
1051 printk(KERN_INFO PREFIX "serialize enabled\n");
1053 acpi_gbl_all_methods_serialized = TRUE;
1055 return 1;
1058 __setup("acpi_serialize", acpi_serialize_setup);
1061 * Wake and Run-Time GPES are expected to be separate.
1062 * We disable wake-GPEs at run-time to prevent spurious
1063 * interrupts.
1065 * However, if a system exists that shares Wake and
1066 * Run-time events on the same GPE this flag is available
1067 * to tell Linux to keep the wake-time GPEs enabled at run-time.
1069 static int __init acpi_wake_gpes_always_on_setup(char *str)
1071 printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
1073 acpi_gbl_leave_wake_gpes_disabled = FALSE;
1075 return 1;
1078 __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
1080 /* Check of resource interference between native drivers and ACPI
1081 * OperationRegions (SystemIO and System Memory only).
1082 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1083 * in arbitrary AML code and can interfere with legacy drivers.
1084 * acpi_enforce_resources= can be set to:
1086 * - strict (default) (2)
1087 * -> further driver trying to access the resources will not load
1088 * - lax (1)
1089 * -> further driver trying to access the resources will load, but you
1090 * get a system message that something might go wrong...
1092 * - no (0)
1093 * -> ACPI Operation Region resources will not be registered
1096 #define ENFORCE_RESOURCES_STRICT 2
1097 #define ENFORCE_RESOURCES_LAX 1
1098 #define ENFORCE_RESOURCES_NO 0
1100 static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1102 static int __init acpi_enforce_resources_setup(char *str)
1104 if (str == NULL || *str == '\0')
1105 return 0;
1107 if (!strcmp("strict", str))
1108 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1109 else if (!strcmp("lax", str))
1110 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1111 else if (!strcmp("no", str))
1112 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1114 return 1;
1117 __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1119 /* Check for resource conflicts between ACPI OperationRegions and native
1120 * drivers */
1121 int acpi_check_resource_conflict(struct resource *res)
1123 struct acpi_res_list *res_list_elem;
1124 int ioport;
1125 int clash = 0;
1127 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1128 return 0;
1129 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1130 return 0;
1132 ioport = res->flags & IORESOURCE_IO;
1134 spin_lock(&acpi_res_lock);
1135 list_for_each_entry(res_list_elem, &resource_list_head,
1136 resource_list) {
1137 if (ioport && (res_list_elem->resource_type
1138 != ACPI_ADR_SPACE_SYSTEM_IO))
1139 continue;
1140 if (!ioport && (res_list_elem->resource_type
1141 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1142 continue;
1144 if (res->end < res_list_elem->start
1145 || res_list_elem->end < res->start)
1146 continue;
1147 clash = 1;
1148 break;
1150 spin_unlock(&acpi_res_lock);
1152 if (clash) {
1153 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
1154 printk("%sACPI: %s resource %s [0x%llx-0x%llx]"
1155 " conflicts with ACPI region %s"
1156 " [0x%llx-0x%llx]\n",
1157 acpi_enforce_resources == ENFORCE_RESOURCES_LAX
1158 ? KERN_WARNING : KERN_ERR,
1159 ioport ? "I/O" : "Memory", res->name,
1160 (long long) res->start, (long long) res->end,
1161 res_list_elem->name,
1162 (long long) res_list_elem->start,
1163 (long long) res_list_elem->end);
1164 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1165 printk(KERN_NOTICE "ACPI: This conflict may"
1166 " cause random problems and system"
1167 " instability\n");
1168 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1169 " for this device, you should use it instead of"
1170 " the native driver\n");
1172 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1173 return -EBUSY;
1175 return 0;
1177 EXPORT_SYMBOL(acpi_check_resource_conflict);
1179 int acpi_check_region(resource_size_t start, resource_size_t n,
1180 const char *name)
1182 struct resource res = {
1183 .start = start,
1184 .end = start + n - 1,
1185 .name = name,
1186 .flags = IORESOURCE_IO,
1189 return acpi_check_resource_conflict(&res);
1191 EXPORT_SYMBOL(acpi_check_region);
1193 int acpi_check_mem_region(resource_size_t start, resource_size_t n,
1194 const char *name)
1196 struct resource res = {
1197 .start = start,
1198 .end = start + n - 1,
1199 .name = name,
1200 .flags = IORESOURCE_MEM,
1203 return acpi_check_resource_conflict(&res);
1206 EXPORT_SYMBOL(acpi_check_mem_region);
1209 * Acquire a spinlock.
1211 * handle is a pointer to the spinlock_t.
1214 acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
1216 acpi_cpu_flags flags;
1217 spin_lock_irqsave(lockp, flags);
1218 return flags;
1222 * Release a spinlock. See above.
1225 void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
1227 spin_unlock_irqrestore(lockp, flags);
1230 #ifndef ACPI_USE_LOCAL_CACHE
1232 /*******************************************************************************
1234 * FUNCTION: acpi_os_create_cache
1236 * PARAMETERS: name - Ascii name for the cache
1237 * size - Size of each cached object
1238 * depth - Maximum depth of the cache (in objects) <ignored>
1239 * cache - Where the new cache object is returned
1241 * RETURN: status
1243 * DESCRIPTION: Create a cache object
1245 ******************************************************************************/
1247 acpi_status
1248 acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
1250 *cache = kmem_cache_create(name, size, 0, 0, NULL);
1251 if (*cache == NULL)
1252 return AE_ERROR;
1253 else
1254 return AE_OK;
1257 /*******************************************************************************
1259 * FUNCTION: acpi_os_purge_cache
1261 * PARAMETERS: Cache - Handle to cache object
1263 * RETURN: Status
1265 * DESCRIPTION: Free all objects within the requested cache.
1267 ******************************************************************************/
1269 acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
1271 kmem_cache_shrink(cache);
1272 return (AE_OK);
1275 /*******************************************************************************
1277 * FUNCTION: acpi_os_delete_cache
1279 * PARAMETERS: Cache - Handle to cache object
1281 * RETURN: Status
1283 * DESCRIPTION: Free all objects within the requested cache and delete the
1284 * cache object.
1286 ******************************************************************************/
1288 acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
1290 kmem_cache_destroy(cache);
1291 return (AE_OK);
1294 /*******************************************************************************
1296 * FUNCTION: acpi_os_release_object
1298 * PARAMETERS: Cache - Handle to cache object
1299 * Object - The object to be released
1301 * RETURN: None
1303 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1304 * the object is deleted.
1306 ******************************************************************************/
1308 acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
1310 kmem_cache_free(cache, object);
1311 return (AE_OK);
1314 /******************************************************************************
1316 * FUNCTION: acpi_os_validate_interface
1318 * PARAMETERS: interface - Requested interface to be validated
1320 * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
1322 * DESCRIPTION: Match an interface string to the interfaces supported by the
1323 * host. Strings originate from an AML call to the _OSI method.
1325 *****************************************************************************/
1327 acpi_status
1328 acpi_os_validate_interface (char *interface)
1330 if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1331 return AE_OK;
1332 if (!strcmp("Linux", interface)) {
1334 printk(KERN_NOTICE PREFIX
1335 "BIOS _OSI(Linux) query %s%s\n",
1336 osi_linux.enable ? "honored" : "ignored",
1337 osi_linux.cmdline ? " via cmdline" :
1338 osi_linux.dmi ? " via DMI" : "");
1340 if (osi_linux.enable)
1341 return AE_OK;
1343 return AE_SUPPORT;
1346 static inline int acpi_res_list_add(struct acpi_res_list *res)
1348 struct acpi_res_list *res_list_elem;
1350 list_for_each_entry(res_list_elem, &resource_list_head,
1351 resource_list) {
1353 if (res->resource_type == res_list_elem->resource_type &&
1354 res->start == res_list_elem->start &&
1355 res->end == res_list_elem->end) {
1358 * The Region(addr,len) already exist in the list,
1359 * just increase the count
1362 res_list_elem->count++;
1363 return 0;
1367 res->count = 1;
1368 list_add(&res->resource_list, &resource_list_head);
1369 return 1;
1372 static inline void acpi_res_list_del(struct acpi_res_list *res)
1374 struct acpi_res_list *res_list_elem;
1376 list_for_each_entry(res_list_elem, &resource_list_head,
1377 resource_list) {
1379 if (res->resource_type == res_list_elem->resource_type &&
1380 res->start == res_list_elem->start &&
1381 res->end == res_list_elem->end) {
1384 * If the res count is decreased to 0,
1385 * remove and free it
1388 if (--res_list_elem->count == 0) {
1389 list_del(&res_list_elem->resource_list);
1390 kfree(res_list_elem);
1392 return;
1397 acpi_status
1398 acpi_os_invalidate_address(
1399 u8 space_id,
1400 acpi_physical_address address,
1401 acpi_size length)
1403 struct acpi_res_list res;
1405 switch (space_id) {
1406 case ACPI_ADR_SPACE_SYSTEM_IO:
1407 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1408 /* Only interference checks against SystemIO and SytemMemory
1409 are needed */
1410 res.start = address;
1411 res.end = address + length - 1;
1412 res.resource_type = space_id;
1413 spin_lock(&acpi_res_lock);
1414 acpi_res_list_del(&res);
1415 spin_unlock(&acpi_res_lock);
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 /******************************************************************************
1431 * FUNCTION: acpi_os_validate_address
1433 * PARAMETERS: space_id - ACPI space ID
1434 * address - Physical address
1435 * length - Address length
1437 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1438 * should return AE_AML_ILLEGAL_ADDRESS.
1440 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1441 * the addresses accessed by AML operation regions.
1443 *****************************************************************************/
1445 acpi_status
1446 acpi_os_validate_address (
1447 u8 space_id,
1448 acpi_physical_address address,
1449 acpi_size length,
1450 char *name)
1452 struct acpi_res_list *res;
1453 int added;
1454 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1455 return AE_OK;
1457 switch (space_id) {
1458 case ACPI_ADR_SPACE_SYSTEM_IO:
1459 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1460 /* Only interference checks against SystemIO and SytemMemory
1461 are needed */
1462 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1463 if (!res)
1464 return AE_OK;
1465 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1466 strlcpy(res->name, name, 5);
1467 res->start = address;
1468 res->end = address + length - 1;
1469 res->resource_type = space_id;
1470 spin_lock(&acpi_res_lock);
1471 added = acpi_res_list_add(res);
1472 spin_unlock(&acpi_res_lock);
1473 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1474 "name: %s\n", added ? "Added" : "Already exist",
1475 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
1476 ? "SystemIO" : "System Memory",
1477 (unsigned long long)res->start,
1478 (unsigned long long)res->end,
1479 res->name);
1480 if (!added)
1481 kfree(res);
1482 break;
1483 case ACPI_ADR_SPACE_PCI_CONFIG:
1484 case ACPI_ADR_SPACE_EC:
1485 case ACPI_ADR_SPACE_SMBUS:
1486 case ACPI_ADR_SPACE_CMOS:
1487 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1488 case ACPI_ADR_SPACE_DATA_TABLE:
1489 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1490 break;
1492 return AE_OK;
1495 #endif