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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
30 #include <linux/highmem.h>
31 #include <linux/pci.h>
32 #include <linux/interrupt.h>
33 #include <linux/kmod.h>
34 #include <linux/delay.h>
35 #include <linux/workqueue.h>
36 #include <linux/nmi.h>
37 #include <linux/acpi.h>
38 #include <linux/efi.h>
39 #include <linux/ioport.h>
40 #include <linux/list.h>
41 #include <linux/jiffies.h>
42 #include <linux/semaphore.h>
43 #include <linux/module.h>
46 #include <linux/uaccess.h>
47 #include <linux/io-64-nonatomic-lo-hi.h>
51 #define _COMPONENT ACPI_OS_SERVICES
52 ACPI_MODULE_NAME("osl");
55 acpi_osd_exec_callback function
;
57 struct work_struct work
;
60 #ifdef ENABLE_DEBUGGER
61 #include <linux/kdb.h>
63 /* stuff for debugger support */
65 EXPORT_SYMBOL(acpi_in_debugger
);
66 #endif /*ENABLE_DEBUGGER */
68 static int (*__acpi_os_prepare_sleep
)(u8 sleep_state
, u32 pm1a_ctrl
,
70 static int (*__acpi_os_prepare_extended_sleep
)(u8 sleep_state
, u32 val_a
,
73 static acpi_osd_handler acpi_irq_handler
;
74 static void *acpi_irq_context
;
75 static struct workqueue_struct
*kacpid_wq
;
76 static struct workqueue_struct
*kacpi_notify_wq
;
77 static struct workqueue_struct
*kacpi_hotplug_wq
;
78 static bool acpi_os_initialized
;
79 unsigned int acpi_sci_irq
= INVALID_ACPI_IRQ
;
80 bool acpi_permanent_mmap
= false;
83 * This list of permanent mappings is for memory that may be accessed from
84 * interrupt context, where we can't do the ioremap().
87 struct list_head list
;
89 acpi_physical_address phys
;
91 unsigned long refcount
;
94 static LIST_HEAD(acpi_ioremaps
);
95 static DEFINE_MUTEX(acpi_ioremap_lock
);
97 static void __init
acpi_request_region (struct acpi_generic_address
*gas
,
98 unsigned int length
, char *desc
)
102 /* Handle possible alignment issues */
103 memcpy(&addr
, &gas
->address
, sizeof(addr
));
104 if (!addr
|| !length
)
107 /* Resources are never freed */
108 if (gas
->space_id
== ACPI_ADR_SPACE_SYSTEM_IO
)
109 request_region(addr
, length
, desc
);
110 else if (gas
->space_id
== ACPI_ADR_SPACE_SYSTEM_MEMORY
)
111 request_mem_region(addr
, length
, desc
);
114 static int __init
acpi_reserve_resources(void)
116 acpi_request_region(&acpi_gbl_FADT
.xpm1a_event_block
, acpi_gbl_FADT
.pm1_event_length
,
117 "ACPI PM1a_EVT_BLK");
119 acpi_request_region(&acpi_gbl_FADT
.xpm1b_event_block
, acpi_gbl_FADT
.pm1_event_length
,
120 "ACPI PM1b_EVT_BLK");
122 acpi_request_region(&acpi_gbl_FADT
.xpm1a_control_block
, acpi_gbl_FADT
.pm1_control_length
,
123 "ACPI PM1a_CNT_BLK");
125 acpi_request_region(&acpi_gbl_FADT
.xpm1b_control_block
, acpi_gbl_FADT
.pm1_control_length
,
126 "ACPI PM1b_CNT_BLK");
128 if (acpi_gbl_FADT
.pm_timer_length
== 4)
129 acpi_request_region(&acpi_gbl_FADT
.xpm_timer_block
, 4, "ACPI PM_TMR");
131 acpi_request_region(&acpi_gbl_FADT
.xpm2_control_block
, acpi_gbl_FADT
.pm2_control_length
,
134 /* Length of GPE blocks must be a non-negative multiple of 2 */
136 if (!(acpi_gbl_FADT
.gpe0_block_length
& 0x1))
137 acpi_request_region(&acpi_gbl_FADT
.xgpe0_block
,
138 acpi_gbl_FADT
.gpe0_block_length
, "ACPI GPE0_BLK");
140 if (!(acpi_gbl_FADT
.gpe1_block_length
& 0x1))
141 acpi_request_region(&acpi_gbl_FADT
.xgpe1_block
,
142 acpi_gbl_FADT
.gpe1_block_length
, "ACPI GPE1_BLK");
146 fs_initcall_sync(acpi_reserve_resources
);
148 void acpi_os_printf(const char *fmt
, ...)
152 acpi_os_vprintf(fmt
, args
);
155 EXPORT_SYMBOL(acpi_os_printf
);
157 void acpi_os_vprintf(const char *fmt
, va_list args
)
159 static char buffer
[512];
161 vsprintf(buffer
, fmt
, args
);
163 #ifdef ENABLE_DEBUGGER
164 if (acpi_in_debugger
) {
165 kdb_printf("%s", buffer
);
167 if (printk_get_level(buffer
))
168 printk("%s", buffer
);
170 printk(KERN_CONT
"%s", buffer
);
173 if (acpi_debugger_write_log(buffer
) < 0) {
174 if (printk_get_level(buffer
))
175 printk("%s", buffer
);
177 printk(KERN_CONT
"%s", buffer
);
183 static unsigned long acpi_rsdp
;
184 static int __init
setup_acpi_rsdp(char *arg
)
186 return kstrtoul(arg
, 16, &acpi_rsdp
);
188 early_param("acpi_rsdp", setup_acpi_rsdp
);
191 acpi_physical_address __init
acpi_os_get_root_pointer(void)
193 acpi_physical_address pa
= 0;
196 if (acpi_rsdp
&& !secure_modules())
200 if (efi_enabled(EFI_CONFIG_TABLES
)) {
201 if (efi
.acpi20
!= EFI_INVALID_TABLE_ADDR
)
203 if (efi
.acpi
!= EFI_INVALID_TABLE_ADDR
)
205 pr_err(PREFIX
"System description tables not found\n");
206 } else if (IS_ENABLED(CONFIG_ACPI_LEGACY_TABLES_LOOKUP
)) {
207 acpi_find_root_pointer(&pa
);
213 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
214 static struct acpi_ioremap
*
215 acpi_map_lookup(acpi_physical_address phys
, acpi_size size
)
217 struct acpi_ioremap
*map
;
219 list_for_each_entry_rcu(map
, &acpi_ioremaps
, list
)
220 if (map
->phys
<= phys
&&
221 phys
+ size
<= map
->phys
+ map
->size
)
227 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
228 static void __iomem
*
229 acpi_map_vaddr_lookup(acpi_physical_address phys
, unsigned int size
)
231 struct acpi_ioremap
*map
;
233 map
= acpi_map_lookup(phys
, size
);
235 return map
->virt
+ (phys
- map
->phys
);
240 void __iomem
*acpi_os_get_iomem(acpi_physical_address phys
, unsigned int size
)
242 struct acpi_ioremap
*map
;
243 void __iomem
*virt
= NULL
;
245 mutex_lock(&acpi_ioremap_lock
);
246 map
= acpi_map_lookup(phys
, size
);
248 virt
= map
->virt
+ (phys
- map
->phys
);
251 mutex_unlock(&acpi_ioremap_lock
);
254 EXPORT_SYMBOL_GPL(acpi_os_get_iomem
);
256 /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
257 static struct acpi_ioremap
*
258 acpi_map_lookup_virt(void __iomem
*virt
, acpi_size size
)
260 struct acpi_ioremap
*map
;
262 list_for_each_entry_rcu(map
, &acpi_ioremaps
, list
)
263 if (map
->virt
<= virt
&&
264 virt
+ size
<= map
->virt
+ map
->size
)
270 #if defined(CONFIG_IA64) || defined(CONFIG_ARM64)
271 /* ioremap will take care of cache attributes */
272 #define should_use_kmap(pfn) 0
274 #define should_use_kmap(pfn) page_is_ram(pfn)
277 static void __iomem
*acpi_map(acpi_physical_address pg_off
, unsigned long pg_sz
)
281 pfn
= pg_off
>> PAGE_SHIFT
;
282 if (should_use_kmap(pfn
)) {
283 if (pg_sz
> PAGE_SIZE
)
285 return (void __iomem __force
*)kmap(pfn_to_page(pfn
));
287 return acpi_os_ioremap(pg_off
, pg_sz
);
290 static void acpi_unmap(acpi_physical_address pg_off
, void __iomem
*vaddr
)
294 pfn
= pg_off
>> PAGE_SHIFT
;
295 if (should_use_kmap(pfn
))
296 kunmap(pfn_to_page(pfn
));
302 * acpi_os_map_iomem - Get a virtual address for a given physical address range.
303 * @phys: Start of the physical address range to map.
304 * @size: Size of the physical address range to map.
306 * Look up the given physical address range in the list of existing ACPI memory
307 * mappings. If found, get a reference to it and return a pointer to it (its
308 * virtual address). If not found, map it, add it to that list and return a
311 * During early init (when acpi_permanent_mmap has not been set yet) this
312 * routine simply calls __acpi_map_table() to get the job done.
315 acpi_os_map_iomem(acpi_physical_address phys
, acpi_size size
)
317 struct acpi_ioremap
*map
;
319 acpi_physical_address pg_off
;
322 if (phys
> ULONG_MAX
) {
323 printk(KERN_ERR PREFIX
"Cannot map memory that high\n");
327 if (!acpi_permanent_mmap
)
328 return __acpi_map_table((unsigned long)phys
, size
);
330 mutex_lock(&acpi_ioremap_lock
);
331 /* Check if there's a suitable mapping already. */
332 map
= acpi_map_lookup(phys
, size
);
338 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
340 mutex_unlock(&acpi_ioremap_lock
);
344 pg_off
= round_down(phys
, PAGE_SIZE
);
345 pg_sz
= round_up(phys
+ size
, PAGE_SIZE
) - pg_off
;
346 virt
= acpi_map(pg_off
, pg_sz
);
348 mutex_unlock(&acpi_ioremap_lock
);
353 INIT_LIST_HEAD(&map
->list
);
359 list_add_tail_rcu(&map
->list
, &acpi_ioremaps
);
362 mutex_unlock(&acpi_ioremap_lock
);
363 return map
->virt
+ (phys
- map
->phys
);
365 EXPORT_SYMBOL_GPL(acpi_os_map_iomem
);
367 void *__ref
acpi_os_map_memory(acpi_physical_address phys
, acpi_size size
)
369 return (void *)acpi_os_map_iomem(phys
, size
);
371 EXPORT_SYMBOL_GPL(acpi_os_map_memory
);
373 static void acpi_os_drop_map_ref(struct acpi_ioremap
*map
)
375 if (!--map
->refcount
)
376 list_del_rcu(&map
->list
);
379 static void acpi_os_map_cleanup(struct acpi_ioremap
*map
)
381 if (!map
->refcount
) {
382 synchronize_rcu_expedited();
383 acpi_unmap(map
->phys
, map
->virt
);
389 * acpi_os_unmap_iomem - Drop a memory mapping reference.
390 * @virt: Start of the address range to drop a reference to.
391 * @size: Size of the address range to drop a reference to.
393 * Look up the given virtual address range in the list of existing ACPI memory
394 * mappings, drop a reference to it and unmap it if there are no more active
397 * During early init (when acpi_permanent_mmap has not been set yet) this
398 * routine simply calls __acpi_unmap_table() to get the job done. Since
399 * __acpi_unmap_table() is an __init function, the __ref annotation is needed
402 void __ref
acpi_os_unmap_iomem(void __iomem
*virt
, acpi_size size
)
404 struct acpi_ioremap
*map
;
406 if (!acpi_permanent_mmap
) {
407 __acpi_unmap_table(virt
, size
);
411 mutex_lock(&acpi_ioremap_lock
);
412 map
= acpi_map_lookup_virt(virt
, size
);
414 mutex_unlock(&acpi_ioremap_lock
);
415 WARN(true, PREFIX
"%s: bad address %p\n", __func__
, virt
);
418 acpi_os_drop_map_ref(map
);
419 mutex_unlock(&acpi_ioremap_lock
);
421 acpi_os_map_cleanup(map
);
423 EXPORT_SYMBOL_GPL(acpi_os_unmap_iomem
);
425 void __ref
acpi_os_unmap_memory(void *virt
, acpi_size size
)
427 return acpi_os_unmap_iomem((void __iomem
*)virt
, size
);
429 EXPORT_SYMBOL_GPL(acpi_os_unmap_memory
);
431 int acpi_os_map_generic_address(struct acpi_generic_address
*gas
)
436 if (gas
->space_id
!= ACPI_ADR_SPACE_SYSTEM_MEMORY
)
439 /* Handle possible alignment issues */
440 memcpy(&addr
, &gas
->address
, sizeof(addr
));
441 if (!addr
|| !gas
->bit_width
)
444 virt
= acpi_os_map_iomem(addr
, gas
->bit_width
/ 8);
450 EXPORT_SYMBOL(acpi_os_map_generic_address
);
452 void acpi_os_unmap_generic_address(struct acpi_generic_address
*gas
)
455 struct acpi_ioremap
*map
;
457 if (gas
->space_id
!= ACPI_ADR_SPACE_SYSTEM_MEMORY
)
460 /* Handle possible alignment issues */
461 memcpy(&addr
, &gas
->address
, sizeof(addr
));
462 if (!addr
|| !gas
->bit_width
)
465 mutex_lock(&acpi_ioremap_lock
);
466 map
= acpi_map_lookup(addr
, gas
->bit_width
/ 8);
468 mutex_unlock(&acpi_ioremap_lock
);
471 acpi_os_drop_map_ref(map
);
472 mutex_unlock(&acpi_ioremap_lock
);
474 acpi_os_map_cleanup(map
);
476 EXPORT_SYMBOL(acpi_os_unmap_generic_address
);
478 #ifdef ACPI_FUTURE_USAGE
480 acpi_os_get_physical_address(void *virt
, acpi_physical_address
* phys
)
483 return AE_BAD_PARAMETER
;
485 *phys
= virt_to_phys(virt
);
491 #ifdef CONFIG_ACPI_REV_OVERRIDE_POSSIBLE
492 static bool acpi_rev_override
;
494 int __init
acpi_rev_override_setup(char *str
)
496 acpi_rev_override
= true;
499 __setup("acpi_rev_override", acpi_rev_override_setup
);
501 #define acpi_rev_override false
504 #define ACPI_MAX_OVERRIDE_LEN 100
506 static char acpi_os_name
[ACPI_MAX_OVERRIDE_LEN
];
509 acpi_os_predefined_override(const struct acpi_predefined_names
*init_val
,
510 acpi_string
*new_val
)
512 if (!init_val
|| !new_val
)
513 return AE_BAD_PARAMETER
;
516 if (!memcmp(init_val
->name
, "_OS_", 4) && strlen(acpi_os_name
)) {
517 printk(KERN_INFO PREFIX
"Overriding _OS definition to '%s'\n",
519 *new_val
= acpi_os_name
;
522 if (!memcmp(init_val
->name
, "_REV", 4) && acpi_rev_override
) {
523 printk(KERN_INFO PREFIX
"Overriding _REV return value to 5\n");
524 *new_val
= (char *)5;
530 static irqreturn_t
acpi_irq(int irq
, void *dev_id
)
534 handled
= (*acpi_irq_handler
) (acpi_irq_context
);
540 acpi_irq_not_handled
++;
546 acpi_os_install_interrupt_handler(u32 gsi
, acpi_osd_handler handler
,
551 acpi_irq_stats_init();
554 * ACPI interrupts different from the SCI in our copy of the FADT are
557 if (gsi
!= acpi_gbl_FADT
.sci_interrupt
)
558 return AE_BAD_PARAMETER
;
560 if (acpi_irq_handler
)
561 return AE_ALREADY_ACQUIRED
;
563 if (acpi_gsi_to_irq(gsi
, &irq
) < 0) {
564 printk(KERN_ERR PREFIX
"SCI (ACPI GSI %d) not registered\n",
569 acpi_irq_handler
= handler
;
570 acpi_irq_context
= context
;
571 if (request_irq(irq
, acpi_irq
, IRQF_SHARED
, "acpi", acpi_irq
)) {
572 printk(KERN_ERR PREFIX
"SCI (IRQ%d) allocation failed\n", irq
);
573 acpi_irq_handler
= NULL
;
574 return AE_NOT_ACQUIRED
;
581 acpi_status
acpi_os_remove_interrupt_handler(u32 gsi
, acpi_osd_handler handler
)
583 if (gsi
!= acpi_gbl_FADT
.sci_interrupt
|| !acpi_sci_irq_valid())
584 return AE_BAD_PARAMETER
;
586 free_irq(acpi_sci_irq
, acpi_irq
);
587 acpi_irq_handler
= NULL
;
588 acpi_sci_irq
= INVALID_ACPI_IRQ
;
594 * Running in interpreter thread context, safe to sleep
597 void acpi_os_sleep(u64 ms
)
602 void acpi_os_stall(u32 us
)
610 touch_nmi_watchdog();
616 * Support ACPI 3.0 AML Timer operand
617 * Returns 64-bit free-running, monotonically increasing timer
618 * with 100ns granularity
620 u64
acpi_os_get_timer(void)
622 u64 time_ns
= ktime_to_ns(ktime_get());
623 do_div(time_ns
, 100);
627 acpi_status
acpi_os_read_port(acpi_io_address port
, u32
* value
, u32 width
)
636 *(u8
*) value
= inb(port
);
637 } else if (width
<= 16) {
638 *(u16
*) value
= inw(port
);
639 } else if (width
<= 32) {
640 *(u32
*) value
= inl(port
);
648 EXPORT_SYMBOL(acpi_os_read_port
);
650 acpi_status
acpi_os_write_port(acpi_io_address port
, u32 value
, u32 width
)
654 } else if (width
<= 16) {
656 } else if (width
<= 32) {
665 EXPORT_SYMBOL(acpi_os_write_port
);
668 acpi_os_read_memory(acpi_physical_address phys_addr
, u64
*value
, u32 width
)
670 void __iomem
*virt_addr
;
671 unsigned int size
= width
/ 8;
676 virt_addr
= acpi_map_vaddr_lookup(phys_addr
, size
);
679 virt_addr
= acpi_os_ioremap(phys_addr
, size
);
681 return AE_BAD_ADDRESS
;
690 *(u8
*) value
= readb(virt_addr
);
693 *(u16
*) value
= readw(virt_addr
);
696 *(u32
*) value
= readl(virt_addr
);
699 *(u64
*) value
= readq(virt_addr
);
714 acpi_os_write_memory(acpi_physical_address phys_addr
, u64 value
, u32 width
)
716 void __iomem
*virt_addr
;
717 unsigned int size
= width
/ 8;
721 virt_addr
= acpi_map_vaddr_lookup(phys_addr
, size
);
724 virt_addr
= acpi_os_ioremap(phys_addr
, size
);
726 return AE_BAD_ADDRESS
;
732 writeb(value
, virt_addr
);
735 writew(value
, virt_addr
);
738 writel(value
, virt_addr
);
741 writeq(value
, virt_addr
);
756 acpi_os_read_pci_configuration(struct acpi_pci_id
* pci_id
, u32 reg
,
757 u64
*value
, u32 width
)
763 return AE_BAD_PARAMETER
;
779 result
= raw_pci_read(pci_id
->segment
, pci_id
->bus
,
780 PCI_DEVFN(pci_id
->device
, pci_id
->function
),
781 reg
, size
, &value32
);
784 return (result
? AE_ERROR
: AE_OK
);
788 acpi_os_write_pci_configuration(struct acpi_pci_id
* pci_id
, u32 reg
,
789 u64 value
, u32 width
)
807 result
= raw_pci_write(pci_id
->segment
, pci_id
->bus
,
808 PCI_DEVFN(pci_id
->device
, pci_id
->function
),
811 return (result
? AE_ERROR
: AE_OK
);
814 static void acpi_os_execute_deferred(struct work_struct
*work
)
816 struct acpi_os_dpc
*dpc
= container_of(work
, struct acpi_os_dpc
, work
);
818 dpc
->function(dpc
->context
);
822 #ifdef CONFIG_ACPI_DEBUGGER
823 static struct acpi_debugger acpi_debugger
;
824 static bool acpi_debugger_initialized
;
826 int acpi_register_debugger(struct module
*owner
,
827 const struct acpi_debugger_ops
*ops
)
831 mutex_lock(&acpi_debugger
.lock
);
832 if (acpi_debugger
.ops
) {
837 acpi_debugger
.owner
= owner
;
838 acpi_debugger
.ops
= ops
;
841 mutex_unlock(&acpi_debugger
.lock
);
844 EXPORT_SYMBOL(acpi_register_debugger
);
846 void acpi_unregister_debugger(const struct acpi_debugger_ops
*ops
)
848 mutex_lock(&acpi_debugger
.lock
);
849 if (ops
== acpi_debugger
.ops
) {
850 acpi_debugger
.ops
= NULL
;
851 acpi_debugger
.owner
= NULL
;
853 mutex_unlock(&acpi_debugger
.lock
);
855 EXPORT_SYMBOL(acpi_unregister_debugger
);
857 int acpi_debugger_create_thread(acpi_osd_exec_callback function
, void *context
)
860 int (*func
)(acpi_osd_exec_callback
, void *);
861 struct module
*owner
;
863 if (!acpi_debugger_initialized
)
865 mutex_lock(&acpi_debugger
.lock
);
866 if (!acpi_debugger
.ops
) {
870 if (!try_module_get(acpi_debugger
.owner
)) {
874 func
= acpi_debugger
.ops
->create_thread
;
875 owner
= acpi_debugger
.owner
;
876 mutex_unlock(&acpi_debugger
.lock
);
878 ret
= func(function
, context
);
880 mutex_lock(&acpi_debugger
.lock
);
883 mutex_unlock(&acpi_debugger
.lock
);
887 ssize_t
acpi_debugger_write_log(const char *msg
)
890 ssize_t (*func
)(const char *);
891 struct module
*owner
;
893 if (!acpi_debugger_initialized
)
895 mutex_lock(&acpi_debugger
.lock
);
896 if (!acpi_debugger
.ops
) {
900 if (!try_module_get(acpi_debugger
.owner
)) {
904 func
= acpi_debugger
.ops
->write_log
;
905 owner
= acpi_debugger
.owner
;
906 mutex_unlock(&acpi_debugger
.lock
);
910 mutex_lock(&acpi_debugger
.lock
);
913 mutex_unlock(&acpi_debugger
.lock
);
917 ssize_t
acpi_debugger_read_cmd(char *buffer
, size_t buffer_length
)
920 ssize_t (*func
)(char *, size_t);
921 struct module
*owner
;
923 if (!acpi_debugger_initialized
)
925 mutex_lock(&acpi_debugger
.lock
);
926 if (!acpi_debugger
.ops
) {
930 if (!try_module_get(acpi_debugger
.owner
)) {
934 func
= acpi_debugger
.ops
->read_cmd
;
935 owner
= acpi_debugger
.owner
;
936 mutex_unlock(&acpi_debugger
.lock
);
938 ret
= func(buffer
, buffer_length
);
940 mutex_lock(&acpi_debugger
.lock
);
943 mutex_unlock(&acpi_debugger
.lock
);
947 int acpi_debugger_wait_command_ready(void)
950 int (*func
)(bool, char *, size_t);
951 struct module
*owner
;
953 if (!acpi_debugger_initialized
)
955 mutex_lock(&acpi_debugger
.lock
);
956 if (!acpi_debugger
.ops
) {
960 if (!try_module_get(acpi_debugger
.owner
)) {
964 func
= acpi_debugger
.ops
->wait_command_ready
;
965 owner
= acpi_debugger
.owner
;
966 mutex_unlock(&acpi_debugger
.lock
);
968 ret
= func(acpi_gbl_method_executing
,
969 acpi_gbl_db_line_buf
, ACPI_DB_LINE_BUFFER_SIZE
);
971 mutex_lock(&acpi_debugger
.lock
);
974 mutex_unlock(&acpi_debugger
.lock
);
978 int acpi_debugger_notify_command_complete(void)
982 struct module
*owner
;
984 if (!acpi_debugger_initialized
)
986 mutex_lock(&acpi_debugger
.lock
);
987 if (!acpi_debugger
.ops
) {
991 if (!try_module_get(acpi_debugger
.owner
)) {
995 func
= acpi_debugger
.ops
->notify_command_complete
;
996 owner
= acpi_debugger
.owner
;
997 mutex_unlock(&acpi_debugger
.lock
);
1001 mutex_lock(&acpi_debugger
.lock
);
1004 mutex_unlock(&acpi_debugger
.lock
);
1008 int __init
acpi_debugger_init(void)
1010 mutex_init(&acpi_debugger
.lock
);
1011 acpi_debugger_initialized
= true;
1016 /*******************************************************************************
1018 * FUNCTION: acpi_os_execute
1020 * PARAMETERS: Type - Type of the callback
1021 * Function - Function to be executed
1022 * Context - Function parameters
1026 * DESCRIPTION: Depending on type, either queues function for deferred execution or
1027 * immediately executes function on a separate thread.
1029 ******************************************************************************/
1031 acpi_status
acpi_os_execute(acpi_execute_type type
,
1032 acpi_osd_exec_callback function
, void *context
)
1034 acpi_status status
= AE_OK
;
1035 struct acpi_os_dpc
*dpc
;
1036 struct workqueue_struct
*queue
;
1038 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
,
1039 "Scheduling function [%p(%p)] for deferred execution.\n",
1040 function
, context
));
1042 if (type
== OSL_DEBUGGER_MAIN_THREAD
) {
1043 ret
= acpi_debugger_create_thread(function
, context
);
1045 pr_err("Call to kthread_create() failed.\n");
1052 * Allocate/initialize DPC structure. Note that this memory will be
1053 * freed by the callee. The kernel handles the work_struct list in a
1054 * way that allows us to also free its memory inside the callee.
1055 * Because we may want to schedule several tasks with different
1056 * parameters we can't use the approach some kernel code uses of
1057 * having a static work_struct.
1060 dpc
= kzalloc(sizeof(struct acpi_os_dpc
), GFP_ATOMIC
);
1062 return AE_NO_MEMORY
;
1064 dpc
->function
= function
;
1065 dpc
->context
= context
;
1068 * To prevent lockdep from complaining unnecessarily, make sure that
1069 * there is a different static lockdep key for each workqueue by using
1070 * INIT_WORK() for each of them separately.
1072 if (type
== OSL_NOTIFY_HANDLER
) {
1073 queue
= kacpi_notify_wq
;
1074 INIT_WORK(&dpc
->work
, acpi_os_execute_deferred
);
1075 } else if (type
== OSL_GPE_HANDLER
) {
1077 INIT_WORK(&dpc
->work
, acpi_os_execute_deferred
);
1079 pr_err("Unsupported os_execute type %d.\n", type
);
1083 if (ACPI_FAILURE(status
))
1087 * On some machines, a software-initiated SMI causes corruption unless
1088 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
1089 * typically it's done in GPE-related methods that are run via
1090 * workqueues, so we can avoid the known corruption cases by always
1091 * queueing on CPU 0.
1093 ret
= queue_work_on(0, queue
, &dpc
->work
);
1095 printk(KERN_ERR PREFIX
1096 "Call to queue_work() failed.\n");
1100 if (ACPI_FAILURE(status
))
1105 EXPORT_SYMBOL(acpi_os_execute
);
1107 void acpi_os_wait_events_complete(void)
1110 * Make sure the GPE handler or the fixed event handler is not used
1111 * on another CPU after removal.
1113 if (acpi_sci_irq_valid())
1114 synchronize_hardirq(acpi_sci_irq
);
1115 flush_workqueue(kacpid_wq
);
1116 flush_workqueue(kacpi_notify_wq
);
1119 struct acpi_hp_work
{
1120 struct work_struct work
;
1121 struct acpi_device
*adev
;
1125 static void acpi_hotplug_work_fn(struct work_struct
*work
)
1127 struct acpi_hp_work
*hpw
= container_of(work
, struct acpi_hp_work
, work
);
1129 acpi_os_wait_events_complete();
1130 acpi_device_hotplug(hpw
->adev
, hpw
->src
);
1134 acpi_status
acpi_hotplug_schedule(struct acpi_device
*adev
, u32 src
)
1136 struct acpi_hp_work
*hpw
;
1138 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
,
1139 "Scheduling hotplug event (%p, %u) for deferred execution.\n",
1142 hpw
= kmalloc(sizeof(*hpw
), GFP_KERNEL
);
1144 return AE_NO_MEMORY
;
1146 INIT_WORK(&hpw
->work
, acpi_hotplug_work_fn
);
1150 * We can't run hotplug code in kacpid_wq/kacpid_notify_wq etc., because
1151 * the hotplug code may call driver .remove() functions, which may
1152 * invoke flush_scheduled_work()/acpi_os_wait_events_complete() to flush
1155 if (!queue_work(kacpi_hotplug_wq
, &hpw
->work
)) {
1162 bool acpi_queue_hotplug_work(struct work_struct
*work
)
1164 return queue_work(kacpi_hotplug_wq
, work
);
1168 acpi_os_create_semaphore(u32 max_units
, u32 initial_units
, acpi_handle
* handle
)
1170 struct semaphore
*sem
= NULL
;
1172 sem
= acpi_os_allocate_zeroed(sizeof(struct semaphore
));
1174 return AE_NO_MEMORY
;
1176 sema_init(sem
, initial_units
);
1178 *handle
= (acpi_handle
*) sem
;
1180 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX
, "Creating semaphore[%p|%d].\n",
1181 *handle
, initial_units
));
1187 * TODO: A better way to delete semaphores? Linux doesn't have a
1188 * 'delete_semaphore()' function -- may result in an invalid
1189 * pointer dereference for non-synchronized consumers. Should
1190 * we at least check for blocked threads and signal/cancel them?
1193 acpi_status
acpi_os_delete_semaphore(acpi_handle handle
)
1195 struct semaphore
*sem
= (struct semaphore
*)handle
;
1198 return AE_BAD_PARAMETER
;
1200 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX
, "Deleting semaphore[%p].\n", handle
));
1202 BUG_ON(!list_empty(&sem
->wait_list
));
1210 * TODO: Support for units > 1?
1212 acpi_status
acpi_os_wait_semaphore(acpi_handle handle
, u32 units
, u16 timeout
)
1214 acpi_status status
= AE_OK
;
1215 struct semaphore
*sem
= (struct semaphore
*)handle
;
1219 if (!acpi_os_initialized
)
1222 if (!sem
|| (units
< 1))
1223 return AE_BAD_PARAMETER
;
1228 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX
, "Waiting for semaphore[%p|%d|%d]\n",
1229 handle
, units
, timeout
));
1231 if (timeout
== ACPI_WAIT_FOREVER
)
1232 jiffies
= MAX_SCHEDULE_TIMEOUT
;
1234 jiffies
= msecs_to_jiffies(timeout
);
1236 ret
= down_timeout(sem
, jiffies
);
1240 if (ACPI_FAILURE(status
)) {
1241 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX
,
1242 "Failed to acquire semaphore[%p|%d|%d], %s",
1243 handle
, units
, timeout
,
1244 acpi_format_exception(status
)));
1246 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX
,
1247 "Acquired semaphore[%p|%d|%d]", handle
,
1255 * TODO: Support for units > 1?
1257 acpi_status
acpi_os_signal_semaphore(acpi_handle handle
, u32 units
)
1259 struct semaphore
*sem
= (struct semaphore
*)handle
;
1261 if (!acpi_os_initialized
)
1264 if (!sem
|| (units
< 1))
1265 return AE_BAD_PARAMETER
;
1270 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX
, "Signaling semaphore[%p|%d]\n", handle
,
1278 acpi_status
acpi_os_get_line(char *buffer
, u32 buffer_length
, u32
*bytes_read
)
1280 #ifdef ENABLE_DEBUGGER
1281 if (acpi_in_debugger
) {
1284 kdb_read(buffer
, buffer_length
);
1286 /* remove the CR kdb includes */
1287 chars
= strlen(buffer
) - 1;
1288 buffer
[chars
] = '\0';
1293 ret
= acpi_debugger_read_cmd(buffer
, buffer_length
);
1302 EXPORT_SYMBOL(acpi_os_get_line
);
1304 acpi_status
acpi_os_wait_command_ready(void)
1308 ret
= acpi_debugger_wait_command_ready();
1314 acpi_status
acpi_os_notify_command_complete(void)
1318 ret
= acpi_debugger_notify_command_complete();
1324 acpi_status
acpi_os_signal(u32 function
, void *info
)
1327 case ACPI_SIGNAL_FATAL
:
1328 printk(KERN_ERR PREFIX
"Fatal opcode executed\n");
1330 case ACPI_SIGNAL_BREAKPOINT
:
1333 * ACPI spec. says to treat it as a NOP unless
1334 * you are debugging. So if/when we integrate
1335 * AML debugger into the kernel debugger its
1336 * hook will go here. But until then it is
1337 * not useful to print anything on breakpoints.
1347 static int __init
acpi_os_name_setup(char *str
)
1349 char *p
= acpi_os_name
;
1350 int count
= ACPI_MAX_OVERRIDE_LEN
- 1;
1355 for (; count
-- && *str
; str
++) {
1356 if (isalnum(*str
) || *str
== ' ' || *str
== ':')
1358 else if (*str
== '\'' || *str
== '"')
1369 __setup("acpi_os_name=", acpi_os_name_setup
);
1372 * Disable the auto-serialization of named objects creation methods.
1374 * This feature is enabled by default. It marks the AML control methods
1375 * that contain the opcodes to create named objects as "Serialized".
1377 static int __init
acpi_no_auto_serialize_setup(char *str
)
1379 acpi_gbl_auto_serialize_methods
= FALSE
;
1380 pr_info("ACPI: auto-serialization disabled\n");
1385 __setup("acpi_no_auto_serialize", acpi_no_auto_serialize_setup
);
1387 /* Check of resource interference between native drivers and ACPI
1388 * OperationRegions (SystemIO and System Memory only).
1389 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1390 * in arbitrary AML code and can interfere with legacy drivers.
1391 * acpi_enforce_resources= can be set to:
1393 * - strict (default) (2)
1394 * -> further driver trying to access the resources will not load
1396 * -> further driver trying to access the resources will load, but you
1397 * get a system message that something might go wrong...
1400 * -> ACPI Operation Region resources will not be registered
1403 #define ENFORCE_RESOURCES_STRICT 2
1404 #define ENFORCE_RESOURCES_LAX 1
1405 #define ENFORCE_RESOURCES_NO 0
1407 static unsigned int acpi_enforce_resources
= ENFORCE_RESOURCES_STRICT
;
1409 static int __init
acpi_enforce_resources_setup(char *str
)
1411 if (str
== NULL
|| *str
== '\0')
1414 if (!strcmp("strict", str
))
1415 acpi_enforce_resources
= ENFORCE_RESOURCES_STRICT
;
1416 else if (!strcmp("lax", str
))
1417 acpi_enforce_resources
= ENFORCE_RESOURCES_LAX
;
1418 else if (!strcmp("no", str
))
1419 acpi_enforce_resources
= ENFORCE_RESOURCES_NO
;
1424 __setup("acpi_enforce_resources=", acpi_enforce_resources_setup
);
1426 /* Check for resource conflicts between ACPI OperationRegions and native
1428 int acpi_check_resource_conflict(const struct resource
*res
)
1430 acpi_adr_space_type space_id
;
1435 if (acpi_enforce_resources
== ENFORCE_RESOURCES_NO
)
1437 if (!(res
->flags
& IORESOURCE_IO
) && !(res
->flags
& IORESOURCE_MEM
))
1440 if (res
->flags
& IORESOURCE_IO
)
1441 space_id
= ACPI_ADR_SPACE_SYSTEM_IO
;
1443 space_id
= ACPI_ADR_SPACE_SYSTEM_MEMORY
;
1445 length
= resource_size(res
);
1446 if (acpi_enforce_resources
!= ENFORCE_RESOURCES_NO
)
1448 clash
= acpi_check_address_range(space_id
, res
->start
, length
, warn
);
1451 if (acpi_enforce_resources
!= ENFORCE_RESOURCES_NO
) {
1452 if (acpi_enforce_resources
== ENFORCE_RESOURCES_LAX
)
1453 printk(KERN_NOTICE
"ACPI: This conflict may"
1454 " cause random problems and system"
1456 printk(KERN_INFO
"ACPI: If an ACPI driver is available"
1457 " for this device, you should use it instead of"
1458 " the native driver\n");
1460 if (acpi_enforce_resources
== ENFORCE_RESOURCES_STRICT
)
1465 EXPORT_SYMBOL(acpi_check_resource_conflict
);
1467 int acpi_check_region(resource_size_t start
, resource_size_t n
,
1470 struct resource res
= {
1472 .end
= start
+ n
- 1,
1474 .flags
= IORESOURCE_IO
,
1477 return acpi_check_resource_conflict(&res
);
1479 EXPORT_SYMBOL(acpi_check_region
);
1482 * Let drivers know whether the resource checks are effective
1484 int acpi_resources_are_enforced(void)
1486 return acpi_enforce_resources
== ENFORCE_RESOURCES_STRICT
;
1488 EXPORT_SYMBOL(acpi_resources_are_enforced
);
1491 * Deallocate the memory for a spinlock.
1493 void acpi_os_delete_lock(acpi_spinlock handle
)
1499 * Acquire a spinlock.
1501 * handle is a pointer to the spinlock_t.
1504 acpi_cpu_flags
acpi_os_acquire_lock(acpi_spinlock lockp
)
1506 acpi_cpu_flags flags
;
1507 spin_lock_irqsave(lockp
, flags
);
1512 * Release a spinlock. See above.
1515 void acpi_os_release_lock(acpi_spinlock lockp
, acpi_cpu_flags flags
)
1517 spin_unlock_irqrestore(lockp
, flags
);
1520 #ifndef ACPI_USE_LOCAL_CACHE
1522 /*******************************************************************************
1524 * FUNCTION: acpi_os_create_cache
1526 * PARAMETERS: name - Ascii name for the cache
1527 * size - Size of each cached object
1528 * depth - Maximum depth of the cache (in objects) <ignored>
1529 * cache - Where the new cache object is returned
1533 * DESCRIPTION: Create a cache object
1535 ******************************************************************************/
1538 acpi_os_create_cache(char *name
, u16 size
, u16 depth
, acpi_cache_t
** cache
)
1540 *cache
= kmem_cache_create(name
, size
, 0, 0, NULL
);
1547 /*******************************************************************************
1549 * FUNCTION: acpi_os_purge_cache
1551 * PARAMETERS: Cache - Handle to cache object
1555 * DESCRIPTION: Free all objects within the requested cache.
1557 ******************************************************************************/
1559 acpi_status
acpi_os_purge_cache(acpi_cache_t
* cache
)
1561 kmem_cache_shrink(cache
);
1565 /*******************************************************************************
1567 * FUNCTION: acpi_os_delete_cache
1569 * PARAMETERS: Cache - Handle to cache object
1573 * DESCRIPTION: Free all objects within the requested cache and delete the
1576 ******************************************************************************/
1578 acpi_status
acpi_os_delete_cache(acpi_cache_t
* cache
)
1580 kmem_cache_destroy(cache
);
1584 /*******************************************************************************
1586 * FUNCTION: acpi_os_release_object
1588 * PARAMETERS: Cache - Handle to cache object
1589 * Object - The object to be released
1593 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1594 * the object is deleted.
1596 ******************************************************************************/
1598 acpi_status
acpi_os_release_object(acpi_cache_t
* cache
, void *object
)
1600 kmem_cache_free(cache
, object
);
1605 static int __init
acpi_no_static_ssdt_setup(char *s
)
1607 acpi_gbl_disable_ssdt_table_install
= TRUE
;
1608 pr_info("ACPI: static SSDT installation disabled\n");
1613 early_param("acpi_no_static_ssdt", acpi_no_static_ssdt_setup
);
1615 static int __init
acpi_disable_return_repair(char *s
)
1617 printk(KERN_NOTICE PREFIX
1618 "ACPI: Predefined validation mechanism disabled\n");
1619 acpi_gbl_disable_auto_repair
= TRUE
;
1624 __setup("acpica_no_return_repair", acpi_disable_return_repair
);
1626 acpi_status __init
acpi_os_initialize(void)
1628 acpi_os_map_generic_address(&acpi_gbl_FADT
.xpm1a_event_block
);
1629 acpi_os_map_generic_address(&acpi_gbl_FADT
.xpm1b_event_block
);
1630 acpi_os_map_generic_address(&acpi_gbl_FADT
.xgpe0_block
);
1631 acpi_os_map_generic_address(&acpi_gbl_FADT
.xgpe1_block
);
1632 if (acpi_gbl_FADT
.flags
& ACPI_FADT_RESET_REGISTER
) {
1634 * Use acpi_os_map_generic_address to pre-map the reset
1635 * register if it's in system memory.
1639 rv
= acpi_os_map_generic_address(&acpi_gbl_FADT
.reset_register
);
1640 pr_debug(PREFIX
"%s: map reset_reg status %d\n", __func__
, rv
);
1642 acpi_os_initialized
= true;
1647 acpi_status __init
acpi_os_initialize1(void)
1649 kacpid_wq
= alloc_workqueue("kacpid", 0, 1);
1650 kacpi_notify_wq
= alloc_workqueue("kacpi_notify", 0, 1);
1651 kacpi_hotplug_wq
= alloc_ordered_workqueue("kacpi_hotplug", 0);
1653 BUG_ON(!kacpi_notify_wq
);
1654 BUG_ON(!kacpi_hotplug_wq
);
1659 acpi_status
acpi_os_terminate(void)
1661 if (acpi_irq_handler
) {
1662 acpi_os_remove_interrupt_handler(acpi_gbl_FADT
.sci_interrupt
,
1666 acpi_os_unmap_generic_address(&acpi_gbl_FADT
.xgpe1_block
);
1667 acpi_os_unmap_generic_address(&acpi_gbl_FADT
.xgpe0_block
);
1668 acpi_os_unmap_generic_address(&acpi_gbl_FADT
.xpm1b_event_block
);
1669 acpi_os_unmap_generic_address(&acpi_gbl_FADT
.xpm1a_event_block
);
1670 if (acpi_gbl_FADT
.flags
& ACPI_FADT_RESET_REGISTER
)
1671 acpi_os_unmap_generic_address(&acpi_gbl_FADT
.reset_register
);
1673 destroy_workqueue(kacpid_wq
);
1674 destroy_workqueue(kacpi_notify_wq
);
1675 destroy_workqueue(kacpi_hotplug_wq
);
1680 acpi_status
acpi_os_prepare_sleep(u8 sleep_state
, u32 pm1a_control
,
1684 if (__acpi_os_prepare_sleep
)
1685 rc
= __acpi_os_prepare_sleep(sleep_state
,
1686 pm1a_control
, pm1b_control
);
1690 return AE_CTRL_SKIP
;
1695 void acpi_os_set_prepare_sleep(int (*func
)(u8 sleep_state
,
1696 u32 pm1a_ctrl
, u32 pm1b_ctrl
))
1698 __acpi_os_prepare_sleep
= func
;
1701 acpi_status
acpi_os_prepare_extended_sleep(u8 sleep_state
, u32 val_a
,
1705 if (__acpi_os_prepare_extended_sleep
)
1706 rc
= __acpi_os_prepare_extended_sleep(sleep_state
,
1711 return AE_CTRL_SKIP
;
1716 void acpi_os_set_prepare_extended_sleep(int (*func
)(u8 sleep_state
,
1717 u32 val_a
, u32 val_b
))
1719 __acpi_os_prepare_extended_sleep
= func
;