Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / acpi / osl.c
blob2664c233fa5327fd5a3323291a1bc352817a8745
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>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/mm.h>
32 #include <linux/pci.h>
33 #include <linux/interrupt.h>
34 #include <linux/kmod.h>
35 #include <linux/delay.h>
36 #include <linux/dmi.h>
37 #include <linux/workqueue.h>
38 #include <linux/nmi.h>
39 #include <linux/acpi.h>
40 #include <acpi/acpi.h>
41 #include <asm/io.h>
42 #include <acpi/acpi_bus.h>
43 #include <acpi/processor.h>
44 #include <asm/uaccess.h>
46 #include <linux/efi.h>
47 #include <linux/ioport.h>
48 #include <linux/list.h>
50 #define _COMPONENT ACPI_OS_SERVICES
51 ACPI_MODULE_NAME("osl");
52 #define PREFIX "ACPI: "
53 struct acpi_os_dpc {
54 acpi_osd_exec_callback function;
55 void *context;
56 struct work_struct work;
59 #ifdef CONFIG_ACPI_CUSTOM_DSDT
60 #include CONFIG_ACPI_CUSTOM_DSDT_FILE
61 #endif
63 #ifdef ENABLE_DEBUGGER
64 #include <linux/kdb.h>
66 /* stuff for debugger support */
67 int acpi_in_debugger;
68 EXPORT_SYMBOL(acpi_in_debugger);
70 extern char line_buf[80];
71 #endif /*ENABLE_DEBUGGER */
73 static unsigned int acpi_irq_irq;
74 static acpi_osd_handler acpi_irq_handler;
75 static void *acpi_irq_context;
76 static struct workqueue_struct *kacpid_wq;
77 static struct workqueue_struct *kacpi_notify_wq;
79 struct acpi_res_list {
80 resource_size_t start;
81 resource_size_t end;
82 acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
83 char name[5]; /* only can have a length of 4 chars, make use of this
84 one instead of res->name, no need to kalloc then */
85 struct list_head resource_list;
88 static LIST_HEAD(resource_list_head);
89 static DEFINE_SPINLOCK(acpi_res_lock);
91 #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
92 static char osi_additional_string[OSI_STRING_LENGTH_MAX];
94 <<<<<<< HEAD:drivers/acpi/osl.c
95 #ifdef CONFIG_ACPI_CUSTOM_DSDT_INITRD
96 static int acpi_no_initrd_override;
97 #endif
99 =======
100 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/osl.c
102 * "Ode to _OSI(Linux)"
104 * osi_linux -- Control response to BIOS _OSI(Linux) query.
106 * As Linux evolves, the features that it supports change.
107 * So an OSI string such as "Linux" is not specific enough
108 * to be useful across multiple versions of Linux. It
109 * doesn't identify any particular feature, interface,
110 * or even any particular version of Linux...
112 * Unfortunately, Linux-2.6.22 and earlier responded "yes"
113 * to a BIOS _OSI(Linux) query. When
114 * a reference mobile BIOS started using it, its use
115 * started to spread to many vendor platforms.
116 * As it is not supportable, we need to halt that spread.
118 * Today, most BIOS references to _OSI(Linux) are noise --
119 * they have no functional effect and are just dead code
120 * carried over from the reference BIOS.
122 * The next most common case is that _OSI(Linux) harms Linux,
123 * usually by causing the BIOS to follow paths that are
124 * not tested during Windows validation.
126 * Finally, there is a short list of platforms
127 * where OSI(Linux) benefits Linux.
129 * In Linux-2.6.23, OSI(Linux) is first disabled by default.
130 * DMI is used to disable the dmesg warning about OSI(Linux)
131 * on platforms where it is known to have no effect.
132 * But a dmesg warning remains for systems where
133 * we do not know if OSI(Linux) is good or bad for the system.
134 * DMI is also used to enable OSI(Linux) for the machines
135 * that are known to need it.
137 * BIOS writers should NOT query _OSI(Linux) on future systems.
138 * It will be ignored by default, and to get Linux to
139 * not ignore it will require a kernel source update to
140 * add a DMI entry, or a boot-time "acpi_osi=Linux" invocation.
142 #define OSI_LINUX_ENABLE 0
144 static struct osi_linux {
145 unsigned int enable:1;
146 unsigned int dmi:1;
147 unsigned int cmdline:1;
148 unsigned int known:1;
149 } osi_linux = { OSI_LINUX_ENABLE, 0, 0, 0};
151 static void __init acpi_request_region (struct acpi_generic_address *addr,
152 unsigned int length, char *desc)
154 struct resource *res;
156 if (!addr->address || !length)
157 return;
159 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
160 res = request_region(addr->address, length, desc);
161 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
162 res = request_mem_region(addr->address, length, desc);
165 static int __init acpi_reserve_resources(void)
167 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
168 "ACPI PM1a_EVT_BLK");
170 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
171 "ACPI PM1b_EVT_BLK");
173 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
174 "ACPI PM1a_CNT_BLK");
176 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
177 "ACPI PM1b_CNT_BLK");
179 if (acpi_gbl_FADT.pm_timer_length == 4)
180 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
182 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
183 "ACPI PM2_CNT_BLK");
185 /* Length of GPE blocks must be a non-negative multiple of 2 */
187 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
188 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
189 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
191 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
192 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
193 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
195 return 0;
197 device_initcall(acpi_reserve_resources);
199 acpi_status __init acpi_os_initialize(void)
201 return AE_OK;
204 acpi_status acpi_os_initialize1(void)
206 kacpid_wq = create_singlethread_workqueue("kacpid");
207 kacpi_notify_wq = create_singlethread_workqueue("kacpi_notify");
208 BUG_ON(!kacpid_wq);
209 BUG_ON(!kacpi_notify_wq);
210 return AE_OK;
213 acpi_status acpi_os_terminate(void)
215 if (acpi_irq_handler) {
216 acpi_os_remove_interrupt_handler(acpi_irq_irq,
217 acpi_irq_handler);
220 destroy_workqueue(kacpid_wq);
221 destroy_workqueue(kacpi_notify_wq);
223 return AE_OK;
226 void acpi_os_printf(const char *fmt, ...)
228 va_list args;
229 va_start(args, fmt);
230 acpi_os_vprintf(fmt, args);
231 va_end(args);
234 void acpi_os_vprintf(const char *fmt, va_list args)
236 static char buffer[512];
238 vsprintf(buffer, fmt, args);
240 #ifdef ENABLE_DEBUGGER
241 if (acpi_in_debugger) {
242 kdb_printf("%s", buffer);
243 } else {
244 printk("%s", buffer);
246 #else
247 printk("%s", buffer);
248 #endif
251 acpi_physical_address __init acpi_os_get_root_pointer(void)
253 if (efi_enabled) {
254 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
255 return efi.acpi20;
256 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
257 return efi.acpi;
258 else {
259 printk(KERN_ERR PREFIX
260 "System description tables not found\n");
261 return 0;
263 } else {
264 acpi_physical_address pa = 0;
266 acpi_find_root_pointer(&pa);
267 return pa;
271 void __iomem *__init_refok
272 acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
274 if (phys > ULONG_MAX) {
275 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
276 return NULL;
278 if (acpi_gbl_permanent_mmap)
280 * ioremap checks to ensure this is in reserved space
282 return ioremap((unsigned long)phys, size);
283 else
284 return __acpi_map_table((unsigned long)phys, size);
286 EXPORT_SYMBOL_GPL(acpi_os_map_memory);
288 void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
290 if (acpi_gbl_permanent_mmap) {
291 iounmap(virt);
294 EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
296 #ifdef ACPI_FUTURE_USAGE
297 acpi_status
298 acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
300 if (!phys || !virt)
301 return AE_BAD_PARAMETER;
303 *phys = virt_to_phys(virt);
305 return AE_OK;
307 #endif
309 #define ACPI_MAX_OVERRIDE_LEN 100
311 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
313 acpi_status
314 acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
315 acpi_string * new_val)
317 if (!init_val || !new_val)
318 return AE_BAD_PARAMETER;
320 *new_val = NULL;
321 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
322 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
323 acpi_os_name);
324 *new_val = acpi_os_name;
327 return AE_OK;
330 <<<<<<< HEAD:drivers/acpi/osl.c
331 #ifdef CONFIG_ACPI_CUSTOM_DSDT_INITRD
332 static struct acpi_table_header *acpi_find_dsdt_initrd(void)
334 struct file *firmware_file;
335 mm_segment_t oldfs;
336 unsigned long len, len2;
337 struct acpi_table_header *dsdt_buffer, *ret = NULL;
338 struct kstat stat;
339 char *ramfs_dsdt_name = "/DSDT.aml";
341 printk(KERN_INFO PREFIX "Checking initramfs for custom DSDT\n");
344 * Never do this at home, only the user-space is allowed to open a file.
345 * The clean way would be to use the firmware loader.
346 * But this code must be run before there is any userspace available.
347 * A static/init firmware infrastructure doesn't exist yet...
349 if (vfs_stat(ramfs_dsdt_name, &stat) < 0)
350 return ret;
352 len = stat.size;
353 /* check especially against empty files */
354 if (len <= 4) {
355 printk(KERN_ERR PREFIX "Failed: DSDT only %lu bytes.\n", len);
356 return ret;
359 firmware_file = filp_open(ramfs_dsdt_name, O_RDONLY, 0);
360 if (IS_ERR(firmware_file)) {
361 printk(KERN_ERR PREFIX "Failed to open %s.\n", ramfs_dsdt_name);
362 return ret;
365 dsdt_buffer = kmalloc(len, GFP_ATOMIC);
366 if (!dsdt_buffer) {
367 printk(KERN_ERR PREFIX "Failed to allocate %lu bytes.\n", len);
368 goto err;
371 oldfs = get_fs();
372 set_fs(KERNEL_DS);
373 len2 = vfs_read(firmware_file, (char __user *)dsdt_buffer, len,
374 &firmware_file->f_pos);
375 set_fs(oldfs);
376 if (len2 < len) {
377 printk(KERN_ERR PREFIX "Failed to read %lu bytes from %s.\n",
378 len, ramfs_dsdt_name);
379 ACPI_FREE(dsdt_buffer);
380 goto err;
383 printk(KERN_INFO PREFIX "Found %lu byte DSDT in %s.\n",
384 len, ramfs_dsdt_name);
385 ret = dsdt_buffer;
386 err:
387 filp_close(firmware_file, NULL);
388 return ret;
390 #endif
392 =======
393 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/osl.c
394 acpi_status
395 acpi_os_table_override(struct acpi_table_header * existing_table,
396 struct acpi_table_header ** new_table)
398 if (!existing_table || !new_table)
399 return AE_BAD_PARAMETER;
401 *new_table = NULL;
403 #ifdef CONFIG_ACPI_CUSTOM_DSDT
404 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
405 *new_table = (struct acpi_table_header *)AmlCode;
406 #endif
407 <<<<<<< HEAD:drivers/acpi/osl.c
408 #ifdef CONFIG_ACPI_CUSTOM_DSDT_INITRD
409 if ((strncmp(existing_table->signature, "DSDT", 4) == 0) &&
410 !acpi_no_initrd_override) {
411 struct acpi_table_header *initrd_table;
413 initrd_table = acpi_find_dsdt_initrd();
414 if (initrd_table)
415 *new_table = initrd_table;
417 #endif
418 =======
419 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/osl.c
420 if (*new_table != NULL) {
421 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
422 "this is unsafe: tainting kernel\n",
423 existing_table->signature,
424 existing_table->oem_table_id);
425 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
427 return AE_OK;
430 <<<<<<< HEAD:drivers/acpi/osl.c
431 #ifdef CONFIG_ACPI_CUSTOM_DSDT_INITRD
432 static int __init acpi_no_initrd_override_setup(char *s)
434 acpi_no_initrd_override = 1;
435 return 1;
437 __setup("acpi_no_initrd_override", acpi_no_initrd_override_setup);
438 #endif
440 =======
441 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/osl.c
442 static irqreturn_t acpi_irq(int irq, void *dev_id)
444 u32 handled;
446 handled = (*acpi_irq_handler) (acpi_irq_context);
448 if (handled) {
449 acpi_irq_handled++;
450 return IRQ_HANDLED;
451 } else
452 return IRQ_NONE;
455 acpi_status
456 acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
457 void *context)
459 unsigned int irq;
461 acpi_irq_stats_init();
464 * Ignore the GSI from the core, and use the value in our copy of the
465 * FADT. It may not be the same if an interrupt source override exists
466 * for the SCI.
468 gsi = acpi_gbl_FADT.sci_interrupt;
469 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
470 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
471 gsi);
472 return AE_OK;
475 acpi_irq_handler = handler;
476 acpi_irq_context = context;
477 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
478 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
479 return AE_NOT_ACQUIRED;
481 acpi_irq_irq = irq;
483 return AE_OK;
486 acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
488 if (irq) {
489 free_irq(irq, acpi_irq);
490 acpi_irq_handler = NULL;
491 acpi_irq_irq = 0;
494 return AE_OK;
498 * Running in interpreter thread context, safe to sleep
501 void acpi_os_sleep(acpi_integer ms)
503 schedule_timeout_interruptible(msecs_to_jiffies(ms));
506 void acpi_os_stall(u32 us)
508 while (us) {
509 u32 delay = 1000;
511 if (delay > us)
512 delay = us;
513 udelay(delay);
514 touch_nmi_watchdog();
515 us -= delay;
520 * Support ACPI 3.0 AML Timer operand
521 * Returns 64-bit free-running, monotonically increasing timer
522 * with 100ns granularity
524 u64 acpi_os_get_timer(void)
526 static u64 t;
528 #ifdef CONFIG_HPET
529 /* TBD: use HPET if available */
530 #endif
532 #ifdef CONFIG_X86_PM_TIMER
533 /* TBD: default to PM timer if HPET was not available */
534 #endif
535 if (!t)
536 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
538 return ++t;
541 acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
543 u32 dummy;
545 if (!value)
546 value = &dummy;
548 *value = 0;
549 if (width <= 8) {
550 *(u8 *) value = inb(port);
551 } else if (width <= 16) {
552 *(u16 *) value = inw(port);
553 } else if (width <= 32) {
554 *(u32 *) value = inl(port);
555 } else {
556 BUG();
559 return AE_OK;
562 EXPORT_SYMBOL(acpi_os_read_port);
564 acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
566 if (width <= 8) {
567 outb(value, port);
568 } else if (width <= 16) {
569 outw(value, port);
570 } else if (width <= 32) {
571 outl(value, port);
572 } else {
573 BUG();
576 return AE_OK;
579 EXPORT_SYMBOL(acpi_os_write_port);
581 acpi_status
582 acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
584 u32 dummy;
585 void __iomem *virt_addr;
587 virt_addr = ioremap(phys_addr, width);
588 if (!value)
589 value = &dummy;
591 switch (width) {
592 case 8:
593 *(u8 *) value = readb(virt_addr);
594 break;
595 case 16:
596 *(u16 *) value = readw(virt_addr);
597 break;
598 case 32:
599 *(u32 *) value = readl(virt_addr);
600 break;
601 default:
602 BUG();
605 iounmap(virt_addr);
607 return AE_OK;
610 acpi_status
611 acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
613 void __iomem *virt_addr;
615 virt_addr = ioremap(phys_addr, width);
617 switch (width) {
618 case 8:
619 writeb(value, virt_addr);
620 break;
621 case 16:
622 writew(value, virt_addr);
623 break;
624 case 32:
625 writel(value, virt_addr);
626 break;
627 default:
628 BUG();
631 iounmap(virt_addr);
633 return AE_OK;
636 acpi_status
637 acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
638 u32 *value, u32 width)
640 int result, size;
642 if (!value)
643 return AE_BAD_PARAMETER;
645 switch (width) {
646 case 8:
647 size = 1;
648 break;
649 case 16:
650 size = 2;
651 break;
652 case 32:
653 size = 4;
654 break;
655 default:
656 return AE_ERROR;
659 result = raw_pci_read(pci_id->segment, pci_id->bus,
660 PCI_DEVFN(pci_id->device, pci_id->function),
661 reg, size, value);
663 return (result ? AE_ERROR : AE_OK);
666 acpi_status
667 acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
668 acpi_integer value, u32 width)
670 int result, size;
672 switch (width) {
673 case 8:
674 size = 1;
675 break;
676 case 16:
677 size = 2;
678 break;
679 case 32:
680 size = 4;
681 break;
682 default:
683 return AE_ERROR;
686 result = raw_pci_write(pci_id->segment, pci_id->bus,
687 PCI_DEVFN(pci_id->device, pci_id->function),
688 reg, size, value);
690 return (result ? AE_ERROR : AE_OK);
693 /* TODO: Change code to take advantage of driver model more */
694 static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
695 acpi_handle chandle, /* current node */
696 struct acpi_pci_id **id,
697 int *is_bridge, u8 * bus_number)
699 acpi_handle handle;
700 struct acpi_pci_id *pci_id = *id;
701 acpi_status status;
702 unsigned long temp;
703 acpi_object_type type;
705 acpi_get_parent(chandle, &handle);
706 if (handle != rhandle) {
707 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
708 bus_number);
710 status = acpi_get_type(handle, &type);
711 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
712 return;
714 status =
715 acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
716 &temp);
717 if (ACPI_SUCCESS(status)) {
718 u32 val;
719 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
720 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
722 if (*is_bridge)
723 pci_id->bus = *bus_number;
725 /* any nicer way to get bus number of bridge ? */
726 status =
727 acpi_os_read_pci_configuration(pci_id, 0x0e, &val,
729 if (ACPI_SUCCESS(status)
730 && ((val & 0x7f) == 1 || (val & 0x7f) == 2)) {
731 status =
732 acpi_os_read_pci_configuration(pci_id, 0x18,
733 &val, 8);
734 if (!ACPI_SUCCESS(status)) {
735 /* Certainly broken... FIX ME */
736 return;
738 *is_bridge = 1;
739 pci_id->bus = val;
740 status =
741 acpi_os_read_pci_configuration(pci_id, 0x19,
742 &val, 8);
743 if (ACPI_SUCCESS(status)) {
744 *bus_number = val;
746 } else
747 *is_bridge = 0;
752 void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
753 acpi_handle chandle, /* current node */
754 struct acpi_pci_id **id)
756 int is_bridge = 1;
757 u8 bus_number = (*id)->bus;
759 acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
762 static void acpi_os_execute_deferred(struct work_struct *work)
764 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
765 if (!dpc) {
766 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
767 return;
770 dpc->function(dpc->context);
771 kfree(dpc);
773 return;
776 /*******************************************************************************
778 * FUNCTION: acpi_os_execute
780 * PARAMETERS: Type - Type of the callback
781 * Function - Function to be executed
782 * Context - Function parameters
784 * RETURN: Status
786 * DESCRIPTION: Depending on type, either queues function for deferred execution or
787 * immediately executes function on a separate thread.
789 ******************************************************************************/
791 acpi_status acpi_os_execute(acpi_execute_type type,
792 acpi_osd_exec_callback function, void *context)
794 acpi_status status = AE_OK;
795 struct acpi_os_dpc *dpc;
796 struct workqueue_struct *queue;
797 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
798 "Scheduling function [%p(%p)] for deferred execution.\n",
799 function, context));
801 if (!function)
802 return AE_BAD_PARAMETER;
805 * Allocate/initialize DPC structure. Note that this memory will be
806 * freed by the callee. The kernel handles the work_struct list in a
807 * way that allows us to also free its memory inside the callee.
808 * Because we may want to schedule several tasks with different
809 * parameters we can't use the approach some kernel code uses of
810 * having a static work_struct.
813 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
814 if (!dpc)
815 return_ACPI_STATUS(AE_NO_MEMORY);
817 dpc->function = function;
818 dpc->context = context;
820 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
821 queue = (type == OSL_NOTIFY_HANDLER) ? kacpi_notify_wq : kacpid_wq;
822 if (!queue_work(queue, &dpc->work)) {
823 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
824 "Call to queue_work() failed.\n"));
825 status = AE_ERROR;
826 kfree(dpc);
828 return_ACPI_STATUS(status);
831 EXPORT_SYMBOL(acpi_os_execute);
833 void acpi_os_wait_events_complete(void *context)
835 flush_workqueue(kacpid_wq);
838 EXPORT_SYMBOL(acpi_os_wait_events_complete);
841 * Allocate the memory for a spinlock and initialize it.
843 acpi_status acpi_os_create_lock(acpi_spinlock * handle)
845 spin_lock_init(*handle);
847 return AE_OK;
851 * Deallocate the memory for a spinlock.
853 void acpi_os_delete_lock(acpi_spinlock handle)
855 return;
858 acpi_status
859 acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
861 struct semaphore *sem = NULL;
864 sem = acpi_os_allocate(sizeof(struct semaphore));
865 if (!sem)
866 return AE_NO_MEMORY;
867 memset(sem, 0, sizeof(struct semaphore));
869 sema_init(sem, initial_units);
871 *handle = (acpi_handle *) sem;
873 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
874 *handle, initial_units));
876 return AE_OK;
880 * TODO: A better way to delete semaphores? Linux doesn't have a
881 * 'delete_semaphore()' function -- may result in an invalid
882 * pointer dereference for non-synchronized consumers. Should
883 * we at least check for blocked threads and signal/cancel them?
886 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
888 struct semaphore *sem = (struct semaphore *)handle;
891 if (!sem)
892 return AE_BAD_PARAMETER;
894 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
896 kfree(sem);
897 sem = NULL;
899 return AE_OK;
903 * TODO: The kernel doesn't have a 'down_timeout' function -- had to
904 * improvise. The process is to sleep for one scheduler quantum
905 * until the semaphore becomes available. Downside is that this
906 * may result in starvation for timeout-based waits when there's
907 * lots of semaphore activity.
909 * TODO: Support for units > 1?
911 acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
913 acpi_status status = AE_OK;
914 struct semaphore *sem = (struct semaphore *)handle;
915 int ret = 0;
918 if (!sem || (units < 1))
919 return AE_BAD_PARAMETER;
921 if (units > 1)
922 return AE_SUPPORT;
924 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
925 handle, units, timeout));
928 * This can be called during resume with interrupts off.
929 * Like boot-time, we should be single threaded and will
930 * always get the lock if we try -- timeout or not.
931 * If this doesn't succeed, then we will oops courtesy of
932 * might_sleep() in down().
934 if (!down_trylock(sem))
935 return AE_OK;
937 switch (timeout) {
939 * No Wait:
940 * --------
941 * A zero timeout value indicates that we shouldn't wait - just
942 * acquire the semaphore if available otherwise return AE_TIME
943 * (a.k.a. 'would block').
945 case 0:
946 if (down_trylock(sem))
947 status = AE_TIME;
948 break;
951 * Wait Indefinitely:
952 * ------------------
954 case ACPI_WAIT_FOREVER:
955 down(sem);
956 break;
959 * Wait w/ Timeout:
960 * ----------------
962 default:
963 // TODO: A better timeout algorithm?
965 int i = 0;
966 static const int quantum_ms = 1000 / HZ;
968 ret = down_trylock(sem);
969 for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
970 schedule_timeout_interruptible(1);
971 ret = down_trylock(sem);
974 if (ret != 0)
975 status = AE_TIME;
977 break;
980 if (ACPI_FAILURE(status)) {
981 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
982 "Failed to acquire semaphore[%p|%d|%d], %s",
983 handle, units, timeout,
984 acpi_format_exception(status)));
985 } else {
986 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
987 "Acquired semaphore[%p|%d|%d]", handle,
988 units, timeout));
991 return status;
995 * TODO: Support for units > 1?
997 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
999 struct semaphore *sem = (struct semaphore *)handle;
1002 if (!sem || (units < 1))
1003 return AE_BAD_PARAMETER;
1005 if (units > 1)
1006 return AE_SUPPORT;
1008 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1009 units));
1011 up(sem);
1013 return AE_OK;
1016 #ifdef ACPI_FUTURE_USAGE
1017 u32 acpi_os_get_line(char *buffer)
1020 #ifdef ENABLE_DEBUGGER
1021 if (acpi_in_debugger) {
1022 u32 chars;
1024 kdb_read(buffer, sizeof(line_buf));
1026 /* remove the CR kdb includes */
1027 chars = strlen(buffer) - 1;
1028 buffer[chars] = '\0';
1030 #endif
1032 return 0;
1034 #endif /* ACPI_FUTURE_USAGE */
1036 acpi_status acpi_os_signal(u32 function, void *info)
1038 switch (function) {
1039 case ACPI_SIGNAL_FATAL:
1040 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1041 break;
1042 case ACPI_SIGNAL_BREAKPOINT:
1044 * AML Breakpoint
1045 * ACPI spec. says to treat it as a NOP unless
1046 * you are debugging. So if/when we integrate
1047 * AML debugger into the kernel debugger its
1048 * hook will go here. But until then it is
1049 * not useful to print anything on breakpoints.
1051 break;
1052 default:
1053 break;
1056 return AE_OK;
1059 static int __init acpi_os_name_setup(char *str)
1061 char *p = acpi_os_name;
1062 int count = ACPI_MAX_OVERRIDE_LEN - 1;
1064 if (!str || !*str)
1065 return 0;
1067 for (; count-- && str && *str; str++) {
1068 if (isalnum(*str) || *str == ' ' || *str == ':')
1069 *p++ = *str;
1070 else if (*str == '\'' || *str == '"')
1071 continue;
1072 else
1073 break;
1075 *p = 0;
1077 return 1;
1081 __setup("acpi_os_name=", acpi_os_name_setup);
1083 static void __init set_osi_linux(unsigned int enable)
1085 if (osi_linux.enable != enable) {
1086 osi_linux.enable = enable;
1087 printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
1088 enable ? "Add": "Delet");
1090 return;
1093 static void __init acpi_cmdline_osi_linux(unsigned int enable)
1095 osi_linux.cmdline = 1; /* cmdline set the default */
1096 set_osi_linux(enable);
1098 return;
1101 void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1103 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
1105 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1107 if (enable == -1)
1108 return;
1110 osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
1112 set_osi_linux(enable);
1114 return;
1118 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1120 * empty string disables _OSI
1121 * string starting with '!' disables that string
1122 * otherwise string is added to list, augmenting built-in strings
1124 int __init acpi_osi_setup(char *str)
1126 if (str == NULL || *str == '\0') {
1127 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1128 acpi_gbl_create_osi_method = FALSE;
1129 } else if (!strcmp("!Linux", str)) {
1130 acpi_cmdline_osi_linux(0); /* !enable */
1131 } else if (*str == '!') {
1132 if (acpi_osi_invalidate(++str) == AE_OK)
1133 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1134 } else if (!strcmp("Linux", str)) {
1135 acpi_cmdline_osi_linux(1); /* enable */
1136 } else if (*osi_additional_string == '\0') {
1137 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
1138 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1141 return 1;
1144 __setup("acpi_osi=", acpi_osi_setup);
1146 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1147 static int __init acpi_serialize_setup(char *str)
1149 printk(KERN_INFO PREFIX "serialize enabled\n");
1151 acpi_gbl_all_methods_serialized = TRUE;
1153 return 1;
1156 __setup("acpi_serialize", acpi_serialize_setup);
1159 * Wake and Run-Time GPES are expected to be separate.
1160 * We disable wake-GPEs at run-time to prevent spurious
1161 * interrupts.
1163 * However, if a system exists that shares Wake and
1164 * Run-time events on the same GPE this flag is available
1165 * to tell Linux to keep the wake-time GPEs enabled at run-time.
1167 static int __init acpi_wake_gpes_always_on_setup(char *str)
1169 printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
1171 acpi_gbl_leave_wake_gpes_disabled = FALSE;
1173 return 1;
1176 __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
1178 /* Check of resource interference between native drivers and ACPI
1179 * OperationRegions (SystemIO and System Memory only).
1180 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1181 * in arbitrary AML code and can interfere with legacy drivers.
1182 * acpi_enforce_resources= can be set to:
1184 * - strict (2)
1185 * -> further driver trying to access the resources will not load
1186 * - lax (default) (1)
1187 * -> further driver trying to access the resources will load, but you
1188 * get a system message that something might go wrong...
1190 * - no (0)
1191 * -> ACPI Operation Region resources will not be registered
1194 #define ENFORCE_RESOURCES_STRICT 2
1195 #define ENFORCE_RESOURCES_LAX 1
1196 #define ENFORCE_RESOURCES_NO 0
1198 static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1200 static int __init acpi_enforce_resources_setup(char *str)
1202 if (str == NULL || *str == '\0')
1203 return 0;
1205 if (!strcmp("strict", str))
1206 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1207 else if (!strcmp("lax", str))
1208 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1209 else if (!strcmp("no", str))
1210 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1212 return 1;
1215 __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1217 /* Check for resource conflicts between ACPI OperationRegions and native
1218 * drivers */
1219 int acpi_check_resource_conflict(struct resource *res)
1221 struct acpi_res_list *res_list_elem;
1222 int ioport;
1223 int clash = 0;
1225 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1226 return 0;
1227 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1228 return 0;
1230 ioport = res->flags & IORESOURCE_IO;
1232 spin_lock(&acpi_res_lock);
1233 list_for_each_entry(res_list_elem, &resource_list_head,
1234 resource_list) {
1235 if (ioport && (res_list_elem->resource_type
1236 != ACPI_ADR_SPACE_SYSTEM_IO))
1237 continue;
1238 if (!ioport && (res_list_elem->resource_type
1239 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1240 continue;
1242 if (res->end < res_list_elem->start
1243 || res_list_elem->end < res->start)
1244 continue;
1245 clash = 1;
1246 break;
1248 spin_unlock(&acpi_res_lock);
1250 if (clash) {
1251 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
1252 <<<<<<< HEAD:drivers/acpi/osl.c
1253 printk(KERN_INFO "%sACPI: %s resource %s [0x%llx-0x%llx]"
1254 =======
1255 printk("%sACPI: %s resource %s [0x%llx-0x%llx]"
1256 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/osl.c
1257 " conflicts with ACPI region %s"
1258 " [0x%llx-0x%llx]\n",
1259 acpi_enforce_resources == ENFORCE_RESOURCES_LAX
1260 ? KERN_WARNING : KERN_ERR,
1261 ioport ? "I/O" : "Memory", res->name,
1262 (long long) res->start, (long long) res->end,
1263 res_list_elem->name,
1264 (long long) res_list_elem->start,
1265 (long long) res_list_elem->end);
1266 printk(KERN_INFO "ACPI: Device needs an ACPI driver\n");
1268 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1269 return -EBUSY;
1271 return 0;
1273 EXPORT_SYMBOL(acpi_check_resource_conflict);
1275 int acpi_check_region(resource_size_t start, resource_size_t n,
1276 const char *name)
1278 struct resource res = {
1279 .start = start,
1280 .end = start + n - 1,
1281 .name = name,
1282 .flags = IORESOURCE_IO,
1285 return acpi_check_resource_conflict(&res);
1287 EXPORT_SYMBOL(acpi_check_region);
1289 int acpi_check_mem_region(resource_size_t start, resource_size_t n,
1290 const char *name)
1292 struct resource res = {
1293 .start = start,
1294 .end = start + n - 1,
1295 .name = name,
1296 .flags = IORESOURCE_MEM,
1299 return acpi_check_resource_conflict(&res);
1302 EXPORT_SYMBOL(acpi_check_mem_region);
1305 * Acquire a spinlock.
1307 * handle is a pointer to the spinlock_t.
1310 acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
1312 acpi_cpu_flags flags;
1313 spin_lock_irqsave(lockp, flags);
1314 return flags;
1318 * Release a spinlock. See above.
1321 void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
1323 spin_unlock_irqrestore(lockp, flags);
1326 #ifndef ACPI_USE_LOCAL_CACHE
1328 /*******************************************************************************
1330 * FUNCTION: acpi_os_create_cache
1332 * PARAMETERS: name - Ascii name for the cache
1333 * size - Size of each cached object
1334 * depth - Maximum depth of the cache (in objects) <ignored>
1335 * cache - Where the new cache object is returned
1337 * RETURN: status
1339 * DESCRIPTION: Create a cache object
1341 ******************************************************************************/
1343 acpi_status
1344 acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
1346 *cache = kmem_cache_create(name, size, 0, 0, NULL);
1347 if (*cache == NULL)
1348 return AE_ERROR;
1349 else
1350 return AE_OK;
1353 /*******************************************************************************
1355 * FUNCTION: acpi_os_purge_cache
1357 * PARAMETERS: Cache - Handle to cache object
1359 * RETURN: Status
1361 * DESCRIPTION: Free all objects within the requested cache.
1363 ******************************************************************************/
1365 acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
1367 kmem_cache_shrink(cache);
1368 return (AE_OK);
1371 /*******************************************************************************
1373 * FUNCTION: acpi_os_delete_cache
1375 * PARAMETERS: Cache - Handle to cache object
1377 * RETURN: Status
1379 * DESCRIPTION: Free all objects within the requested cache and delete the
1380 * cache object.
1382 ******************************************************************************/
1384 acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
1386 kmem_cache_destroy(cache);
1387 return (AE_OK);
1390 /*******************************************************************************
1392 * FUNCTION: acpi_os_release_object
1394 * PARAMETERS: Cache - Handle to cache object
1395 * Object - The object to be released
1397 * RETURN: None
1399 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1400 * the object is deleted.
1402 ******************************************************************************/
1404 acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
1406 kmem_cache_free(cache, object);
1407 return (AE_OK);
1411 * acpi_dmi_dump - dump DMI slots needed for blacklist entry
1413 * Returns 0 on success
1415 static int acpi_dmi_dump(void)
1418 if (!dmi_available)
1419 return -1;
1421 printk(KERN_NOTICE PREFIX "DMI System Vendor: %s\n",
1422 dmi_get_system_info(DMI_SYS_VENDOR));
1423 printk(KERN_NOTICE PREFIX "DMI Product Name: %s\n",
1424 dmi_get_system_info(DMI_PRODUCT_NAME));
1425 printk(KERN_NOTICE PREFIX "DMI Product Version: %s\n",
1426 dmi_get_system_info(DMI_PRODUCT_VERSION));
1427 printk(KERN_NOTICE PREFIX "DMI Board Name: %s\n",
1428 dmi_get_system_info(DMI_BOARD_NAME));
1429 printk(KERN_NOTICE PREFIX "DMI BIOS Vendor: %s\n",
1430 dmi_get_system_info(DMI_BIOS_VENDOR));
1431 printk(KERN_NOTICE PREFIX "DMI BIOS Date: %s\n",
1432 dmi_get_system_info(DMI_BIOS_DATE));
1434 return 0;
1438 /******************************************************************************
1440 * FUNCTION: acpi_os_validate_interface
1442 * PARAMETERS: interface - Requested interface to be validated
1444 * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
1446 * DESCRIPTION: Match an interface string to the interfaces supported by the
1447 * host. Strings originate from an AML call to the _OSI method.
1449 *****************************************************************************/
1451 acpi_status
1452 acpi_os_validate_interface (char *interface)
1454 if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1455 return AE_OK;
1456 if (!strcmp("Linux", interface)) {
1458 printk(KERN_NOTICE PREFIX
1459 "BIOS _OSI(Linux) query %s%s\n",
1460 osi_linux.enable ? "honored" : "ignored",
1461 osi_linux.cmdline ? " via cmdline" :
1462 osi_linux.dmi ? " via DMI" : "");
1464 if (!osi_linux.dmi) {
1465 if (acpi_dmi_dump())
1466 printk(KERN_NOTICE PREFIX
1467 "[please extract dmidecode output]\n");
1468 printk(KERN_NOTICE PREFIX
1469 "Please send DMI info above to "
1470 "linux-acpi@vger.kernel.org\n");
1472 if (!osi_linux.known && !osi_linux.cmdline) {
1473 printk(KERN_NOTICE PREFIX
1474 "If \"acpi_osi=%sLinux\" works better, "
1475 "please notify linux-acpi@vger.kernel.org\n",
1476 osi_linux.enable ? "!" : "");
1479 if (osi_linux.enable)
1480 return AE_OK;
1482 return AE_SUPPORT;
1485 /******************************************************************************
1487 * FUNCTION: acpi_os_validate_address
1489 * PARAMETERS: space_id - ACPI space ID
1490 * address - Physical address
1491 * length - Address length
1493 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1494 * should return AE_AML_ILLEGAL_ADDRESS.
1496 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1497 * the addresses accessed by AML operation regions.
1499 *****************************************************************************/
1501 acpi_status
1502 acpi_os_validate_address (
1503 u8 space_id,
1504 acpi_physical_address address,
1505 acpi_size length,
1506 char *name)
1508 struct acpi_res_list *res;
1509 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1510 return AE_OK;
1512 switch (space_id) {
1513 case ACPI_ADR_SPACE_SYSTEM_IO:
1514 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1515 /* Only interference checks against SystemIO and SytemMemory
1516 are needed */
1517 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1518 if (!res)
1519 return AE_OK;
1520 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1521 strlcpy(res->name, name, 5);
1522 res->start = address;
1523 res->end = address + length - 1;
1524 res->resource_type = space_id;
1525 spin_lock(&acpi_res_lock);
1526 list_add(&res->resource_list, &resource_list_head);
1527 spin_unlock(&acpi_res_lock);
1528 pr_debug("Added %s resource: start: 0x%llx, end: 0x%llx, "
1529 "name: %s\n", (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
1530 ? "SystemIO" : "System Memory",
1531 (unsigned long long)res->start,
1532 (unsigned long long)res->end,
1533 res->name);
1534 break;
1535 case ACPI_ADR_SPACE_PCI_CONFIG:
1536 case ACPI_ADR_SPACE_EC:
1537 case ACPI_ADR_SPACE_SMBUS:
1538 case ACPI_ADR_SPACE_CMOS:
1539 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1540 case ACPI_ADR_SPACE_DATA_TABLE:
1541 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1542 break;
1544 return AE_OK;
1547 #endif