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>
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>
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: "
58 acpi_osd_exec_callback function
;
60 struct work_struct work
;
64 #ifdef CONFIG_ACPI_CUSTOM_DSDT
65 #include CONFIG_ACPI_CUSTOM_DSDT_FILE
68 #ifdef ENABLE_DEBUGGER
69 #include <linux/kdb.h>
71 /* stuff for debugger support */
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
;
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
;
95 static LIST_HEAD(resource_list_head
);
96 static DEFINE_SPINLOCK(acpi_res_lock
);
99 * This list of permanent mappings is for memory that may be accessed from
100 * interrupt context, where we can't do the ioremap().
102 struct acpi_ioremap
{
103 struct list_head list
;
105 acpi_physical_address phys
;
109 static LIST_HEAD(acpi_ioremaps
);
110 static DEFINE_SPINLOCK(acpi_ioremap_lock
);
112 #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
113 static char osi_additional_string
[OSI_STRING_LENGTH_MAX
];
116 * The story of _OSI(Linux)
118 * From pre-history through Linux-2.6.22,
119 * Linux responded TRUE upon a BIOS OSI(Linux) query.
121 * Unfortunately, reference BIOS writers got wind of this
122 * and put OSI(Linux) in their example code, quickly exposing
123 * this string as ill-conceived and opening the door to
124 * an un-bounded number of BIOS incompatibilities.
126 * For example, OSI(Linux) was used on resume to re-POST a
127 * video card on one system, because Linux at that time
128 * could not do a speedy restore in its native driver.
129 * But then upon gaining quick native restore capability,
130 * Linux has no way to tell the BIOS to skip the time-consuming
131 * POST -- putting Linux at a permanent performance disadvantage.
132 * On another system, the BIOS writer used OSI(Linux)
133 * to infer native OS support for IPMI! On other systems,
134 * OSI(Linux) simply got in the way of Linux claiming to
135 * be compatible with other operating systems, exposing
136 * BIOS issues such as skipped device initialization.
138 * So "Linux" turned out to be a really poor chose of
139 * OSI string, and from Linux-2.6.23 onward we respond FALSE.
141 * BIOS writers should NOT query _OSI(Linux) on future systems.
142 * Linux will complain on the console when it sees it, and return FALSE.
143 * To get Linux to return TRUE for your system will require
144 * a kernel source update to add a DMI entry,
145 * or boot with "acpi_osi=Linux"
148 static struct osi_linux
{
149 unsigned int enable
:1;
151 unsigned int cmdline
:1;
152 unsigned int known
:1;
153 } osi_linux
= { 0, 0, 0, 0};
155 static void __init
acpi_request_region (struct acpi_generic_address
*addr
,
156 unsigned int length
, char *desc
)
158 if (!addr
->address
|| !length
)
161 /* Resources are never freed */
162 if (addr
->space_id
== ACPI_ADR_SPACE_SYSTEM_IO
)
163 request_region(addr
->address
, length
, desc
);
164 else if (addr
->space_id
== ACPI_ADR_SPACE_SYSTEM_MEMORY
)
165 request_mem_region(addr
->address
, length
, desc
);
168 static int __init
acpi_reserve_resources(void)
170 acpi_request_region(&acpi_gbl_FADT
.xpm1a_event_block
, acpi_gbl_FADT
.pm1_event_length
,
171 "ACPI PM1a_EVT_BLK");
173 acpi_request_region(&acpi_gbl_FADT
.xpm1b_event_block
, acpi_gbl_FADT
.pm1_event_length
,
174 "ACPI PM1b_EVT_BLK");
176 acpi_request_region(&acpi_gbl_FADT
.xpm1a_control_block
, acpi_gbl_FADT
.pm1_control_length
,
177 "ACPI PM1a_CNT_BLK");
179 acpi_request_region(&acpi_gbl_FADT
.xpm1b_control_block
, acpi_gbl_FADT
.pm1_control_length
,
180 "ACPI PM1b_CNT_BLK");
182 if (acpi_gbl_FADT
.pm_timer_length
== 4)
183 acpi_request_region(&acpi_gbl_FADT
.xpm_timer_block
, 4, "ACPI PM_TMR");
185 acpi_request_region(&acpi_gbl_FADT
.xpm2_control_block
, acpi_gbl_FADT
.pm2_control_length
,
188 /* Length of GPE blocks must be a non-negative multiple of 2 */
190 if (!(acpi_gbl_FADT
.gpe0_block_length
& 0x1))
191 acpi_request_region(&acpi_gbl_FADT
.xgpe0_block
,
192 acpi_gbl_FADT
.gpe0_block_length
, "ACPI GPE0_BLK");
194 if (!(acpi_gbl_FADT
.gpe1_block_length
& 0x1))
195 acpi_request_region(&acpi_gbl_FADT
.xgpe1_block
,
196 acpi_gbl_FADT
.gpe1_block_length
, "ACPI GPE1_BLK");
200 device_initcall(acpi_reserve_resources
);
202 void acpi_os_printf(const char *fmt
, ...)
206 acpi_os_vprintf(fmt
, args
);
210 void acpi_os_vprintf(const char *fmt
, va_list args
)
212 static char buffer
[512];
214 vsprintf(buffer
, fmt
, args
);
216 #ifdef ENABLE_DEBUGGER
217 if (acpi_in_debugger
) {
218 kdb_printf("%s", buffer
);
220 printk(KERN_CONT
"%s", buffer
);
223 printk(KERN_CONT
"%s", buffer
);
227 acpi_physical_address __init
acpi_os_get_root_pointer(void)
230 if (efi
.acpi20
!= EFI_INVALID_TABLE_ADDR
)
232 else if (efi
.acpi
!= EFI_INVALID_TABLE_ADDR
)
235 printk(KERN_ERR PREFIX
236 "System description tables not found\n");
240 acpi_physical_address pa
= 0;
242 acpi_find_root_pointer(&pa
);
247 /* Must be called with 'acpi_ioremap_lock' lock held. */
248 static void __iomem
*
249 acpi_map_vaddr_lookup(acpi_physical_address phys
, acpi_size size
)
251 struct acpi_ioremap
*map
;
253 list_for_each_entry(map
, &acpi_ioremaps
, list
)
254 if (map
->phys
<= phys
&&
255 phys
+ size
<= map
->phys
+ map
->size
)
256 return map
->virt
+ (phys
- map
->phys
);
261 /* Must be called with 'acpi_ioremap_lock' lock held. */
262 static struct acpi_ioremap
*
263 acpi_map_lookup_virt(void __iomem
*virt
, acpi_size size
)
265 struct acpi_ioremap
*map
;
267 list_for_each_entry(map
, &acpi_ioremaps
, list
)
268 if (map
->virt
== virt
&& map
->size
== size
)
274 void __iomem
*__init_refok
275 acpi_os_map_memory(acpi_physical_address phys
, acpi_size size
)
277 struct acpi_ioremap
*map
;
281 if (phys
> ULONG_MAX
) {
282 printk(KERN_ERR PREFIX
"Cannot map memory that high\n");
286 if (!acpi_gbl_permanent_mmap
)
287 return __acpi_map_table((unsigned long)phys
, size
);
289 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
293 virt
= ioremap(phys
, size
);
299 INIT_LIST_HEAD(&map
->list
);
304 spin_lock_irqsave(&acpi_ioremap_lock
, flags
);
305 list_add_tail(&map
->list
, &acpi_ioremaps
);
306 spin_unlock_irqrestore(&acpi_ioremap_lock
, flags
);
310 EXPORT_SYMBOL_GPL(acpi_os_map_memory
);
312 void __ref
acpi_os_unmap_memory(void __iomem
*virt
, acpi_size size
)
314 struct acpi_ioremap
*map
;
317 if (!acpi_gbl_permanent_mmap
) {
318 __acpi_unmap_table(virt
, size
);
322 spin_lock_irqsave(&acpi_ioremap_lock
, flags
);
323 map
= acpi_map_lookup_virt(virt
, size
);
325 spin_unlock_irqrestore(&acpi_ioremap_lock
, flags
);
326 printk(KERN_ERR PREFIX
"%s: bad address %p\n", __func__
, virt
);
331 list_del(&map
->list
);
332 spin_unlock_irqrestore(&acpi_ioremap_lock
, flags
);
337 EXPORT_SYMBOL_GPL(acpi_os_unmap_memory
);
339 void __init
early_acpi_os_unmap_memory(void __iomem
*virt
, acpi_size size
)
341 if (!acpi_gbl_permanent_mmap
)
342 __acpi_unmap_table(virt
, size
);
345 int acpi_os_map_generic_address(struct acpi_generic_address
*addr
)
349 if (addr
->space_id
!= ACPI_ADR_SPACE_SYSTEM_MEMORY
)
352 if (!addr
->address
|| !addr
->bit_width
)
355 virt
= acpi_os_map_memory(addr
->address
, addr
->bit_width
/ 8);
361 EXPORT_SYMBOL_GPL(acpi_os_map_generic_address
);
363 void acpi_os_unmap_generic_address(struct acpi_generic_address
*addr
)
367 acpi_size size
= addr
->bit_width
/ 8;
369 if (addr
->space_id
!= ACPI_ADR_SPACE_SYSTEM_MEMORY
)
372 if (!addr
->address
|| !addr
->bit_width
)
375 spin_lock_irqsave(&acpi_ioremap_lock
, flags
);
376 virt
= acpi_map_vaddr_lookup(addr
->address
, size
);
377 spin_unlock_irqrestore(&acpi_ioremap_lock
, flags
);
379 acpi_os_unmap_memory(virt
, size
);
381 EXPORT_SYMBOL_GPL(acpi_os_unmap_generic_address
);
383 #ifdef ACPI_FUTURE_USAGE
385 acpi_os_get_physical_address(void *virt
, acpi_physical_address
* phys
)
388 return AE_BAD_PARAMETER
;
390 *phys
= virt_to_phys(virt
);
396 #define ACPI_MAX_OVERRIDE_LEN 100
398 static char acpi_os_name
[ACPI_MAX_OVERRIDE_LEN
];
401 acpi_os_predefined_override(const struct acpi_predefined_names
*init_val
,
402 acpi_string
* new_val
)
404 if (!init_val
|| !new_val
)
405 return AE_BAD_PARAMETER
;
408 if (!memcmp(init_val
->name
, "_OS_", 4) && strlen(acpi_os_name
)) {
409 printk(KERN_INFO PREFIX
"Overriding _OS definition to '%s'\n",
411 *new_val
= acpi_os_name
;
418 acpi_os_table_override(struct acpi_table_header
* existing_table
,
419 struct acpi_table_header
** new_table
)
421 if (!existing_table
|| !new_table
)
422 return AE_BAD_PARAMETER
;
426 #ifdef CONFIG_ACPI_CUSTOM_DSDT
427 if (strncmp(existing_table
->signature
, "DSDT", 4) == 0)
428 *new_table
= (struct acpi_table_header
*)AmlCode
;
430 if (*new_table
!= NULL
) {
431 printk(KERN_WARNING PREFIX
"Override [%4.4s-%8.8s], "
432 "this is unsafe: tainting kernel\n",
433 existing_table
->signature
,
434 existing_table
->oem_table_id
);
435 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE
);
440 static irqreturn_t
acpi_irq(int irq
, void *dev_id
)
444 handled
= (*acpi_irq_handler
) (acpi_irq_context
);
450 acpi_irq_not_handled
++;
456 acpi_os_install_interrupt_handler(u32 gsi
, acpi_osd_handler handler
,
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
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",
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
;
486 acpi_status
acpi_os_remove_interrupt_handler(u32 irq
, acpi_osd_handler handler
)
489 free_irq(irq
, acpi_irq
);
490 acpi_irq_handler
= NULL
;
498 * Running in interpreter thread context, safe to sleep
501 void acpi_os_sleep(u64 ms
)
503 schedule_timeout_interruptible(msecs_to_jiffies(ms
));
506 void acpi_os_stall(u32 us
)
514 touch_nmi_watchdog();
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)
529 /* TBD: use HPET if available */
532 #ifdef CONFIG_X86_PM_TIMER
533 /* TBD: default to PM timer if HPET was not available */
536 printk(KERN_ERR PREFIX
"acpi_os_get_timer() TBD\n");
541 acpi_status
acpi_os_read_port(acpi_io_address port
, u32
* value
, u32 width
)
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
);
562 EXPORT_SYMBOL(acpi_os_read_port
);
564 acpi_status
acpi_os_write_port(acpi_io_address port
, u32 value
, u32 width
)
568 } else if (width
<= 16) {
570 } else if (width
<= 32) {
579 EXPORT_SYMBOL(acpi_os_write_port
);
582 acpi_os_read_memory(acpi_physical_address phys_addr
, u32
* value
, u32 width
)
585 void __iomem
*virt_addr
;
586 int size
= width
/ 8, unmap
= 0;
589 spin_lock_irqsave(&acpi_ioremap_lock
, flags
);
590 virt_addr
= acpi_map_vaddr_lookup(phys_addr
, size
);
591 spin_unlock_irqrestore(&acpi_ioremap_lock
, flags
);
593 virt_addr
= ioremap(phys_addr
, size
);
601 *(u8
*) value
= readb(virt_addr
);
604 *(u16
*) value
= readw(virt_addr
);
607 *(u32
*) value
= readl(virt_addr
);
620 acpi_os_write_memory(acpi_physical_address phys_addr
, u32 value
, u32 width
)
622 void __iomem
*virt_addr
;
623 int size
= width
/ 8, unmap
= 0;
626 spin_lock_irqsave(&acpi_ioremap_lock
, flags
);
627 virt_addr
= acpi_map_vaddr_lookup(phys_addr
, size
);
628 spin_unlock_irqrestore(&acpi_ioremap_lock
, flags
);
630 virt_addr
= ioremap(phys_addr
, size
);
636 writeb(value
, virt_addr
);
639 writew(value
, virt_addr
);
642 writel(value
, virt_addr
);
655 acpi_os_read_pci_configuration(struct acpi_pci_id
* pci_id
, u32 reg
,
656 u32
*value
, u32 width
)
661 return AE_BAD_PARAMETER
;
677 result
= raw_pci_read(pci_id
->segment
, pci_id
->bus
,
678 PCI_DEVFN(pci_id
->device
, pci_id
->function
),
681 return (result
? AE_ERROR
: AE_OK
);
685 acpi_os_write_pci_configuration(struct acpi_pci_id
* pci_id
, u32 reg
,
686 u64 value
, u32 width
)
704 result
= raw_pci_write(pci_id
->segment
, pci_id
->bus
,
705 PCI_DEVFN(pci_id
->device
, pci_id
->function
),
708 return (result
? AE_ERROR
: AE_OK
);
711 /* TODO: Change code to take advantage of driver model more */
712 static void acpi_os_derive_pci_id_2(acpi_handle rhandle
, /* upper bound */
713 acpi_handle chandle
, /* current node */
714 struct acpi_pci_id
**id
,
715 int *is_bridge
, u8
* bus_number
)
718 struct acpi_pci_id
*pci_id
= *id
;
720 unsigned long long temp
;
721 acpi_object_type type
;
723 acpi_get_parent(chandle
, &handle
);
724 if (handle
!= rhandle
) {
725 acpi_os_derive_pci_id_2(rhandle
, handle
, &pci_id
, is_bridge
,
728 status
= acpi_get_type(handle
, &type
);
729 if ((ACPI_FAILURE(status
)) || (type
!= ACPI_TYPE_DEVICE
))
732 status
= acpi_evaluate_integer(handle
, METHOD_NAME__ADR
, NULL
,
734 if (ACPI_SUCCESS(status
)) {
736 pci_id
->device
= ACPI_HIWORD(ACPI_LODWORD(temp
));
737 pci_id
->function
= ACPI_LOWORD(ACPI_LODWORD(temp
));
740 pci_id
->bus
= *bus_number
;
742 /* any nicer way to get bus number of bridge ? */
744 acpi_os_read_pci_configuration(pci_id
, 0x0e, &val
,
746 if (ACPI_SUCCESS(status
)
747 && ((val
& 0x7f) == 1 || (val
& 0x7f) == 2)) {
749 acpi_os_read_pci_configuration(pci_id
, 0x18,
751 if (!ACPI_SUCCESS(status
)) {
752 /* Certainly broken... FIX ME */
758 acpi_os_read_pci_configuration(pci_id
, 0x19,
760 if (ACPI_SUCCESS(status
)) {
769 void acpi_os_derive_pci_id(acpi_handle rhandle
, /* upper bound */
770 acpi_handle chandle
, /* current node */
771 struct acpi_pci_id
**id
)
774 u8 bus_number
= (*id
)->bus
;
776 acpi_os_derive_pci_id_2(rhandle
, chandle
, id
, &is_bridge
, &bus_number
);
779 static void acpi_os_execute_deferred(struct work_struct
*work
)
781 struct acpi_os_dpc
*dpc
= container_of(work
, struct acpi_os_dpc
, work
);
784 acpi_os_wait_events_complete(NULL
);
786 dpc
->function(dpc
->context
);
790 /*******************************************************************************
792 * FUNCTION: acpi_os_execute
794 * PARAMETERS: Type - Type of the callback
795 * Function - Function to be executed
796 * Context - Function parameters
800 * DESCRIPTION: Depending on type, either queues function for deferred execution or
801 * immediately executes function on a separate thread.
803 ******************************************************************************/
805 static acpi_status
__acpi_os_execute(acpi_execute_type type
,
806 acpi_osd_exec_callback function
, void *context
, int hp
)
808 acpi_status status
= AE_OK
;
809 struct acpi_os_dpc
*dpc
;
810 struct workqueue_struct
*queue
;
812 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
,
813 "Scheduling function [%p(%p)] for deferred execution.\n",
817 * Allocate/initialize DPC structure. Note that this memory will be
818 * freed by the callee. The kernel handles the work_struct list in a
819 * way that allows us to also free its memory inside the callee.
820 * Because we may want to schedule several tasks with different
821 * parameters we can't use the approach some kernel code uses of
822 * having a static work_struct.
825 dpc
= kmalloc(sizeof(struct acpi_os_dpc
), GFP_ATOMIC
);
829 dpc
->function
= function
;
830 dpc
->context
= context
;
833 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
834 * because the hotplug code may call driver .remove() functions,
835 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
836 * to flush these workqueues.
838 queue
= hp
? kacpi_hotplug_wq
:
839 (type
== OSL_NOTIFY_HANDLER
? kacpi_notify_wq
: kacpid_wq
);
840 dpc
->wait
= hp
? 1 : 0;
842 if (queue
== kacpi_hotplug_wq
)
843 INIT_WORK(&dpc
->work
, acpi_os_execute_deferred
);
844 else if (queue
== kacpi_notify_wq
)
845 INIT_WORK(&dpc
->work
, acpi_os_execute_deferred
);
847 INIT_WORK(&dpc
->work
, acpi_os_execute_deferred
);
850 * On some machines, a software-initiated SMI causes corruption unless
851 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
852 * typically it's done in GPE-related methods that are run via
853 * workqueues, so we can avoid the known corruption cases by always
856 ret
= queue_work_on(0, queue
, &dpc
->work
);
859 printk(KERN_ERR PREFIX
860 "Call to queue_work() failed.\n");
867 acpi_status
acpi_os_execute(acpi_execute_type type
,
868 acpi_osd_exec_callback function
, void *context
)
870 return __acpi_os_execute(type
, function
, context
, 0);
872 EXPORT_SYMBOL(acpi_os_execute
);
874 acpi_status
acpi_os_hotplug_execute(acpi_osd_exec_callback function
,
877 return __acpi_os_execute(0, function
, context
, 1);
880 void acpi_os_wait_events_complete(void *context
)
882 flush_workqueue(kacpid_wq
);
883 flush_workqueue(kacpi_notify_wq
);
886 EXPORT_SYMBOL(acpi_os_wait_events_complete
);
889 * Allocate the memory for a spinlock and initialize it.
891 acpi_status
acpi_os_create_lock(acpi_spinlock
* handle
)
893 spin_lock_init(*handle
);
899 * Deallocate the memory for a spinlock.
901 void acpi_os_delete_lock(acpi_spinlock handle
)
907 acpi_os_create_semaphore(u32 max_units
, u32 initial_units
, acpi_handle
* handle
)
909 struct semaphore
*sem
= NULL
;
911 sem
= acpi_os_allocate(sizeof(struct semaphore
));
914 memset(sem
, 0, sizeof(struct semaphore
));
916 sema_init(sem
, initial_units
);
918 *handle
= (acpi_handle
*) sem
;
920 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX
, "Creating semaphore[%p|%d].\n",
921 *handle
, initial_units
));
927 * TODO: A better way to delete semaphores? Linux doesn't have a
928 * 'delete_semaphore()' function -- may result in an invalid
929 * pointer dereference for non-synchronized consumers. Should
930 * we at least check for blocked threads and signal/cancel them?
933 acpi_status
acpi_os_delete_semaphore(acpi_handle handle
)
935 struct semaphore
*sem
= (struct semaphore
*)handle
;
938 return AE_BAD_PARAMETER
;
940 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX
, "Deleting semaphore[%p].\n", handle
));
942 BUG_ON(!list_empty(&sem
->wait_list
));
950 * TODO: Support for units > 1?
952 acpi_status
acpi_os_wait_semaphore(acpi_handle handle
, u32 units
, u16 timeout
)
954 acpi_status status
= AE_OK
;
955 struct semaphore
*sem
= (struct semaphore
*)handle
;
959 if (!sem
|| (units
< 1))
960 return AE_BAD_PARAMETER
;
965 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX
, "Waiting for semaphore[%p|%d|%d]\n",
966 handle
, units
, timeout
));
968 if (timeout
== ACPI_WAIT_FOREVER
)
969 jiffies
= MAX_SCHEDULE_TIMEOUT
;
971 jiffies
= msecs_to_jiffies(timeout
);
973 ret
= down_timeout(sem
, jiffies
);
977 if (ACPI_FAILURE(status
)) {
978 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX
,
979 "Failed to acquire semaphore[%p|%d|%d], %s",
980 handle
, units
, timeout
,
981 acpi_format_exception(status
)));
983 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX
,
984 "Acquired semaphore[%p|%d|%d]", handle
,
992 * TODO: Support for units > 1?
994 acpi_status
acpi_os_signal_semaphore(acpi_handle handle
, u32 units
)
996 struct semaphore
*sem
= (struct semaphore
*)handle
;
998 if (!sem
|| (units
< 1))
999 return AE_BAD_PARAMETER
;
1004 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX
, "Signaling semaphore[%p|%d]\n", handle
,
1012 #ifdef ACPI_FUTURE_USAGE
1013 u32
acpi_os_get_line(char *buffer
)
1016 #ifdef ENABLE_DEBUGGER
1017 if (acpi_in_debugger
) {
1020 kdb_read(buffer
, sizeof(line_buf
));
1022 /* remove the CR kdb includes */
1023 chars
= strlen(buffer
) - 1;
1024 buffer
[chars
] = '\0';
1030 #endif /* ACPI_FUTURE_USAGE */
1032 acpi_status
acpi_os_signal(u32 function
, void *info
)
1035 case ACPI_SIGNAL_FATAL
:
1036 printk(KERN_ERR PREFIX
"Fatal opcode executed\n");
1038 case ACPI_SIGNAL_BREAKPOINT
:
1041 * ACPI spec. says to treat it as a NOP unless
1042 * you are debugging. So if/when we integrate
1043 * AML debugger into the kernel debugger its
1044 * hook will go here. But until then it is
1045 * not useful to print anything on breakpoints.
1055 static int __init
acpi_os_name_setup(char *str
)
1057 char *p
= acpi_os_name
;
1058 int count
= ACPI_MAX_OVERRIDE_LEN
- 1;
1063 for (; count
-- && str
&& *str
; str
++) {
1064 if (isalnum(*str
) || *str
== ' ' || *str
== ':')
1066 else if (*str
== '\'' || *str
== '"')
1077 __setup("acpi_os_name=", acpi_os_name_setup
);
1079 static void __init
set_osi_linux(unsigned int enable
)
1081 if (osi_linux
.enable
!= enable
) {
1082 osi_linux
.enable
= enable
;
1083 printk(KERN_NOTICE PREFIX
"%sed _OSI(Linux)\n",
1084 enable
? "Add": "Delet");
1089 static void __init
acpi_cmdline_osi_linux(unsigned int enable
)
1091 osi_linux
.cmdline
= 1; /* cmdline set the default */
1092 set_osi_linux(enable
);
1097 void __init
acpi_dmi_osi_linux(int enable
, const struct dmi_system_id
*d
)
1099 osi_linux
.dmi
= 1; /* DMI knows that this box asks OSI(Linux) */
1101 printk(KERN_NOTICE PREFIX
"DMI detected: %s\n", d
->ident
);
1106 osi_linux
.known
= 1; /* DMI knows which OSI(Linux) default needed */
1108 set_osi_linux(enable
);
1114 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1116 * empty string disables _OSI
1117 * string starting with '!' disables that string
1118 * otherwise string is added to list, augmenting built-in strings
1120 int __init
acpi_osi_setup(char *str
)
1122 if (str
== NULL
|| *str
== '\0') {
1123 printk(KERN_INFO PREFIX
"_OSI method disabled\n");
1124 acpi_gbl_create_osi_method
= FALSE
;
1125 } else if (!strcmp("!Linux", str
)) {
1126 acpi_cmdline_osi_linux(0); /* !enable */
1127 } else if (*str
== '!') {
1128 if (acpi_osi_invalidate(++str
) == AE_OK
)
1129 printk(KERN_INFO PREFIX
"Deleted _OSI(%s)\n", str
);
1130 } else if (!strcmp("Linux", str
)) {
1131 acpi_cmdline_osi_linux(1); /* enable */
1132 } else if (*osi_additional_string
== '\0') {
1133 strncpy(osi_additional_string
, str
, OSI_STRING_LENGTH_MAX
);
1134 printk(KERN_INFO PREFIX
"Added _OSI(%s)\n", str
);
1140 __setup("acpi_osi=", acpi_osi_setup
);
1142 /* enable serialization to combat AE_ALREADY_EXISTS errors */
1143 static int __init
acpi_serialize_setup(char *str
)
1145 printk(KERN_INFO PREFIX
"serialize enabled\n");
1147 acpi_gbl_all_methods_serialized
= TRUE
;
1152 __setup("acpi_serialize", acpi_serialize_setup
);
1154 /* Check of resource interference between native drivers and ACPI
1155 * OperationRegions (SystemIO and System Memory only).
1156 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1157 * in arbitrary AML code and can interfere with legacy drivers.
1158 * acpi_enforce_resources= can be set to:
1160 * - strict (default) (2)
1161 * -> further driver trying to access the resources will not load
1163 * -> further driver trying to access the resources will load, but you
1164 * get a system message that something might go wrong...
1167 * -> ACPI Operation Region resources will not be registered
1170 #define ENFORCE_RESOURCES_STRICT 2
1171 #define ENFORCE_RESOURCES_LAX 1
1172 #define ENFORCE_RESOURCES_NO 0
1174 static unsigned int acpi_enforce_resources
= ENFORCE_RESOURCES_STRICT
;
1176 static int __init
acpi_enforce_resources_setup(char *str
)
1178 if (str
== NULL
|| *str
== '\0')
1181 if (!strcmp("strict", str
))
1182 acpi_enforce_resources
= ENFORCE_RESOURCES_STRICT
;
1183 else if (!strcmp("lax", str
))
1184 acpi_enforce_resources
= ENFORCE_RESOURCES_LAX
;
1185 else if (!strcmp("no", str
))
1186 acpi_enforce_resources
= ENFORCE_RESOURCES_NO
;
1191 __setup("acpi_enforce_resources=", acpi_enforce_resources_setup
);
1193 /* Check for resource conflicts between ACPI OperationRegions and native
1195 int acpi_check_resource_conflict(const struct resource
*res
)
1197 struct acpi_res_list
*res_list_elem
;
1201 if (acpi_enforce_resources
== ENFORCE_RESOURCES_NO
)
1203 if (!(res
->flags
& IORESOURCE_IO
) && !(res
->flags
& IORESOURCE_MEM
))
1206 ioport
= res
->flags
& IORESOURCE_IO
;
1208 spin_lock(&acpi_res_lock
);
1209 list_for_each_entry(res_list_elem
, &resource_list_head
,
1211 if (ioport
&& (res_list_elem
->resource_type
1212 != ACPI_ADR_SPACE_SYSTEM_IO
))
1214 if (!ioport
&& (res_list_elem
->resource_type
1215 != ACPI_ADR_SPACE_SYSTEM_MEMORY
))
1218 if (res
->end
< res_list_elem
->start
1219 || res_list_elem
->end
< res
->start
)
1224 spin_unlock(&acpi_res_lock
);
1227 if (acpi_enforce_resources
!= ENFORCE_RESOURCES_NO
) {
1228 printk(KERN_WARNING
"ACPI: resource %s %pR"
1229 " conflicts with ACPI region %s %pR\n",
1230 res
->name
, res
, res_list_elem
->name
,
1232 if (acpi_enforce_resources
== ENFORCE_RESOURCES_LAX
)
1233 printk(KERN_NOTICE
"ACPI: This conflict may"
1234 " cause random problems and system"
1236 printk(KERN_INFO
"ACPI: If an ACPI driver is available"
1237 " for this device, you should use it instead of"
1238 " the native driver\n");
1240 if (acpi_enforce_resources
== ENFORCE_RESOURCES_STRICT
)
1245 EXPORT_SYMBOL(acpi_check_resource_conflict
);
1247 int acpi_check_region(resource_size_t start
, resource_size_t n
,
1250 struct resource res
= {
1252 .end
= start
+ n
- 1,
1254 .flags
= IORESOURCE_IO
,
1257 return acpi_check_resource_conflict(&res
);
1259 EXPORT_SYMBOL(acpi_check_region
);
1261 int acpi_check_mem_region(resource_size_t start
, resource_size_t n
,
1264 struct resource res
= {
1266 .end
= start
+ n
- 1,
1268 .flags
= IORESOURCE_MEM
,
1271 return acpi_check_resource_conflict(&res
);
1274 EXPORT_SYMBOL(acpi_check_mem_region
);
1277 * Let drivers know whether the resource checks are effective
1279 int acpi_resources_are_enforced(void)
1281 return acpi_enforce_resources
== ENFORCE_RESOURCES_STRICT
;
1283 EXPORT_SYMBOL(acpi_resources_are_enforced
);
1286 * Acquire a spinlock.
1288 * handle is a pointer to the spinlock_t.
1291 acpi_cpu_flags
acpi_os_acquire_lock(acpi_spinlock lockp
)
1293 acpi_cpu_flags flags
;
1294 spin_lock_irqsave(lockp
, flags
);
1299 * Release a spinlock. See above.
1302 void acpi_os_release_lock(acpi_spinlock lockp
, acpi_cpu_flags flags
)
1304 spin_unlock_irqrestore(lockp
, flags
);
1307 #ifndef ACPI_USE_LOCAL_CACHE
1309 /*******************************************************************************
1311 * FUNCTION: acpi_os_create_cache
1313 * PARAMETERS: name - Ascii name for the cache
1314 * size - Size of each cached object
1315 * depth - Maximum depth of the cache (in objects) <ignored>
1316 * cache - Where the new cache object is returned
1320 * DESCRIPTION: Create a cache object
1322 ******************************************************************************/
1325 acpi_os_create_cache(char *name
, u16 size
, u16 depth
, acpi_cache_t
** cache
)
1327 *cache
= kmem_cache_create(name
, size
, 0, 0, NULL
);
1334 /*******************************************************************************
1336 * FUNCTION: acpi_os_purge_cache
1338 * PARAMETERS: Cache - Handle to cache object
1342 * DESCRIPTION: Free all objects within the requested cache.
1344 ******************************************************************************/
1346 acpi_status
acpi_os_purge_cache(acpi_cache_t
* cache
)
1348 kmem_cache_shrink(cache
);
1352 /*******************************************************************************
1354 * FUNCTION: acpi_os_delete_cache
1356 * PARAMETERS: Cache - Handle to cache object
1360 * DESCRIPTION: Free all objects within the requested cache and delete the
1363 ******************************************************************************/
1365 acpi_status
acpi_os_delete_cache(acpi_cache_t
* cache
)
1367 kmem_cache_destroy(cache
);
1371 /*******************************************************************************
1373 * FUNCTION: acpi_os_release_object
1375 * PARAMETERS: Cache - Handle to cache object
1376 * Object - The object to be released
1380 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1381 * the object is deleted.
1383 ******************************************************************************/
1385 acpi_status
acpi_os_release_object(acpi_cache_t
* cache
, void *object
)
1387 kmem_cache_free(cache
, object
);
1391 /******************************************************************************
1393 * FUNCTION: acpi_os_validate_interface
1395 * PARAMETERS: interface - Requested interface to be validated
1397 * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
1399 * DESCRIPTION: Match an interface string to the interfaces supported by the
1400 * host. Strings originate from an AML call to the _OSI method.
1402 *****************************************************************************/
1405 acpi_os_validate_interface (char *interface
)
1407 if (!strncmp(osi_additional_string
, interface
, OSI_STRING_LENGTH_MAX
))
1409 if (!strcmp("Linux", interface
)) {
1411 printk(KERN_NOTICE PREFIX
1412 "BIOS _OSI(Linux) query %s%s\n",
1413 osi_linux
.enable
? "honored" : "ignored",
1414 osi_linux
.cmdline
? " via cmdline" :
1415 osi_linux
.dmi
? " via DMI" : "");
1417 if (osi_linux
.enable
)
1423 static inline int acpi_res_list_add(struct acpi_res_list
*res
)
1425 struct acpi_res_list
*res_list_elem
;
1427 list_for_each_entry(res_list_elem
, &resource_list_head
,
1430 if (res
->resource_type
== res_list_elem
->resource_type
&&
1431 res
->start
== res_list_elem
->start
&&
1432 res
->end
== res_list_elem
->end
) {
1435 * The Region(addr,len) already exist in the list,
1436 * just increase the count
1439 res_list_elem
->count
++;
1445 list_add(&res
->resource_list
, &resource_list_head
);
1449 static inline void acpi_res_list_del(struct acpi_res_list
*res
)
1451 struct acpi_res_list
*res_list_elem
;
1453 list_for_each_entry(res_list_elem
, &resource_list_head
,
1456 if (res
->resource_type
== res_list_elem
->resource_type
&&
1457 res
->start
== res_list_elem
->start
&&
1458 res
->end
== res_list_elem
->end
) {
1461 * If the res count is decreased to 0,
1462 * remove and free it
1465 if (--res_list_elem
->count
== 0) {
1466 list_del(&res_list_elem
->resource_list
);
1467 kfree(res_list_elem
);
1475 acpi_os_invalidate_address(
1477 acpi_physical_address address
,
1480 struct acpi_res_list res
;
1483 case ACPI_ADR_SPACE_SYSTEM_IO
:
1484 case ACPI_ADR_SPACE_SYSTEM_MEMORY
:
1485 /* Only interference checks against SystemIO and SystemMemory
1487 res
.start
= address
;
1488 res
.end
= address
+ length
- 1;
1489 res
.resource_type
= space_id
;
1490 spin_lock(&acpi_res_lock
);
1491 acpi_res_list_del(&res
);
1492 spin_unlock(&acpi_res_lock
);
1494 case ACPI_ADR_SPACE_PCI_CONFIG
:
1495 case ACPI_ADR_SPACE_EC
:
1496 case ACPI_ADR_SPACE_SMBUS
:
1497 case ACPI_ADR_SPACE_CMOS
:
1498 case ACPI_ADR_SPACE_PCI_BAR_TARGET
:
1499 case ACPI_ADR_SPACE_DATA_TABLE
:
1500 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
1506 /******************************************************************************
1508 * FUNCTION: acpi_os_validate_address
1510 * PARAMETERS: space_id - ACPI space ID
1511 * address - Physical address
1512 * length - Address length
1514 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1515 * should return AE_AML_ILLEGAL_ADDRESS.
1517 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1518 * the addresses accessed by AML operation regions.
1520 *****************************************************************************/
1523 acpi_os_validate_address (
1525 acpi_physical_address address
,
1529 struct acpi_res_list
*res
;
1531 if (acpi_enforce_resources
== ENFORCE_RESOURCES_NO
)
1535 case ACPI_ADR_SPACE_SYSTEM_IO
:
1536 case ACPI_ADR_SPACE_SYSTEM_MEMORY
:
1537 /* Only interference checks against SystemIO and SystemMemory
1539 res
= kzalloc(sizeof(struct acpi_res_list
), GFP_KERNEL
);
1542 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1543 strlcpy(res
->name
, name
, 5);
1544 res
->start
= address
;
1545 res
->end
= address
+ length
- 1;
1546 res
->resource_type
= space_id
;
1547 spin_lock(&acpi_res_lock
);
1548 added
= acpi_res_list_add(res
);
1549 spin_unlock(&acpi_res_lock
);
1550 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1551 "name: %s\n", added
? "Added" : "Already exist",
1552 (space_id
== ACPI_ADR_SPACE_SYSTEM_IO
)
1553 ? "SystemIO" : "System Memory",
1554 (unsigned long long)res
->start
,
1555 (unsigned long long)res
->end
,
1560 case ACPI_ADR_SPACE_PCI_CONFIG
:
1561 case ACPI_ADR_SPACE_EC
:
1562 case ACPI_ADR_SPACE_SMBUS
:
1563 case ACPI_ADR_SPACE_CMOS
:
1564 case ACPI_ADR_SPACE_PCI_BAR_TARGET
:
1565 case ACPI_ADR_SPACE_DATA_TABLE
:
1566 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
1573 acpi_status __init
acpi_os_initialize(void)
1575 acpi_os_map_generic_address(&acpi_gbl_FADT
.xpm1a_event_block
);
1576 acpi_os_map_generic_address(&acpi_gbl_FADT
.xpm1b_event_block
);
1577 acpi_os_map_generic_address(&acpi_gbl_FADT
.xgpe0_block
);
1578 acpi_os_map_generic_address(&acpi_gbl_FADT
.xgpe1_block
);
1583 acpi_status
acpi_os_initialize1(void)
1585 kacpid_wq
= create_workqueue("kacpid");
1586 kacpi_notify_wq
= create_workqueue("kacpi_notify");
1587 kacpi_hotplug_wq
= create_workqueue("kacpi_hotplug");
1589 BUG_ON(!kacpi_notify_wq
);
1590 BUG_ON(!kacpi_hotplug_wq
);
1594 acpi_status
acpi_os_terminate(void)
1596 if (acpi_irq_handler
) {
1597 acpi_os_remove_interrupt_handler(acpi_irq_irq
,
1601 acpi_os_unmap_generic_address(&acpi_gbl_FADT
.xgpe1_block
);
1602 acpi_os_unmap_generic_address(&acpi_gbl_FADT
.xgpe0_block
);
1603 acpi_os_unmap_generic_address(&acpi_gbl_FADT
.xpm1b_event_block
);
1604 acpi_os_unmap_generic_address(&acpi_gbl_FADT
.xpm1a_event_block
);
1606 destroy_workqueue(kacpid_wq
);
1607 destroy_workqueue(kacpi_notify_wq
);
1608 destroy_workqueue(kacpi_hotplug_wq
);