blob: 29af6b40c93ff1f68222f08db19c76a691bb7dbe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
3 *
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>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04007 * Copyright (c) 2008 Intel Corporation
8 * Author: Matthew Wilcox <willy@linux.intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
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.
16 *
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.
21 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 *
24 */
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/slab.h>
29#include <linux/mm.h>
Myron Stoweba242d52012-01-20 19:13:30 -070030#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/interrupt.h>
33#include <linux/kmod.h>
34#include <linux/delay.h>
35#include <linux/workqueue.h>
36#include <linux/nmi.h>
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +030037#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/efi.h>
Thomas Renningerdf92e692008-02-04 23:31:22 -080039#include <linux/ioport.h>
40#include <linux/list.h>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -040041#include <linux/jiffies.h>
42#include <linux/semaphore.h>
43
44#include <asm/io.h>
45#include <asm/uaccess.h>
Christoph Hellwig2f8e2c82015-08-28 09:27:14 +020046#include <linux/io-64-nonatomic-lo-hi.h>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -040047
Lv Zheng1129c922013-07-23 16:11:55 +080048#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define _COMPONENT ACPI_OS_SERVICES
Len Brownf52fd662007-02-12 22:42:12 -050051ACPI_MODULE_NAME("osl");
Hanjun Guo07070e12014-03-13 12:47:39 +080052
Len Brown4be44fc2005-08-05 00:44:28 -040053struct acpi_os_dpc {
54 acpi_osd_exec_callback function;
55 void *context;
David Howells65f27f32006-11-22 14:55:48 +000056 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
59#ifdef CONFIG_ACPI_CUSTOM_DSDT
60#include CONFIG_ACPI_CUSTOM_DSDT_FILE
61#endif
62
63#ifdef ENABLE_DEBUGGER
64#include <linux/kdb.h>
65
66/* stuff for debugger support */
67int acpi_in_debugger;
68EXPORT_SYMBOL(acpi_in_debugger);
Len Brown4be44fc2005-08-05 00:44:28 -040069#endif /*ENABLE_DEBUGGER */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Tang Liang09f98a82011-12-09 10:05:54 +080071static int (*__acpi_os_prepare_sleep)(u8 sleep_state, u32 pm1a_ctrl,
72 u32 pm1b_ctrl);
Ben Guthrod6b47b12013-07-30 08:24:52 -040073static int (*__acpi_os_prepare_extended_sleep)(u8 sleep_state, u32 val_a,
74 u32 val_b);
Tang Liang09f98a82011-12-09 10:05:54 +080075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static acpi_osd_handler acpi_irq_handler;
77static void *acpi_irq_context;
78static struct workqueue_struct *kacpid_wq;
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -040079static struct workqueue_struct *kacpi_notify_wq;
Yinghai Lu92d8aff2013-01-21 13:20:47 -080080static struct workqueue_struct *kacpi_hotplug_wq;
Lv Zheng7901a052015-08-05 16:23:51 +080081static bool acpi_os_initialized;
Chen Yu49e4b8432015-10-25 01:02:19 +080082unsigned int acpi_sci_irq = INVALID_ACPI_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Myron Stowe620242a2010-10-21 14:23:53 -060084/*
85 * This list of permanent mappings is for memory that may be accessed from
86 * interrupt context, where we can't do the ioremap().
87 */
88struct acpi_ioremap {
89 struct list_head list;
90 void __iomem *virt;
91 acpi_physical_address phys;
92 acpi_size size;
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +010093 unsigned long refcount;
Myron Stowe620242a2010-10-21 14:23:53 -060094};
95
96static LIST_HEAD(acpi_ioremaps);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +010097static DEFINE_MUTEX(acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -060098
Myron Stowebc9ffce2011-11-07 16:23:27 -070099static void __init acpi_request_region (struct acpi_generic_address *gas,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700100 unsigned int length, char *desc)
101{
Myron Stowebc9ffce2011-11-07 16:23:27 -0700102 u64 addr;
103
104 /* Handle possible alignment issues */
105 memcpy(&addr, &gas->address, sizeof(addr));
106 if (!addr || !length)
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700107 return;
108
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200109 /* Resources are never freed */
110 if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
111 request_region(addr, length, desc);
112 else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
113 request_mem_region(addr, length, desc);
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700114}
115
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200116static int __init acpi_reserve_resources(void)
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700117{
Len Browneee3c852007-02-03 01:38:16 -0500118 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700119 "ACPI PM1a_EVT_BLK");
120
Len Browneee3c852007-02-03 01:38:16 -0500121 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700122 "ACPI PM1b_EVT_BLK");
123
Len Browneee3c852007-02-03 01:38:16 -0500124 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700125 "ACPI PM1a_CNT_BLK");
126
Len Browneee3c852007-02-03 01:38:16 -0500127 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700128 "ACPI PM1b_CNT_BLK");
129
Len Browneee3c852007-02-03 01:38:16 -0500130 if (acpi_gbl_FADT.pm_timer_length == 4)
131 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700132
Len Browneee3c852007-02-03 01:38:16 -0500133 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700134 "ACPI PM2_CNT_BLK");
135
136 /* Length of GPE blocks must be a non-negative multiple of 2 */
137
Len Browneee3c852007-02-03 01:38:16 -0500138 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
139 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
140 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700141
Len Browneee3c852007-02-03 01:38:16 -0500142 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
143 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
144 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200145
146 return 0;
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700147}
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200148fs_initcall_sync(acpi_reserve_resources);
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700149
Len Brown4be44fc2005-08-05 00:44:28 -0400150void acpi_os_printf(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 va_list args;
153 va_start(args, fmt);
154 acpi_os_vprintf(fmt, args);
155 va_end(args);
156}
Lv Zheng836d083012015-12-03 10:43:14 +0800157EXPORT_SYMBOL(acpi_os_printf);
Len Brown4be44fc2005-08-05 00:44:28 -0400158
Len Brown4be44fc2005-08-05 00:44:28 -0400159void acpi_os_vprintf(const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 static char buffer[512];
Len Brown4be44fc2005-08-05 00:44:28 -0400162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 vsprintf(buffer, fmt, args);
164
165#ifdef ENABLE_DEBUGGER
166 if (acpi_in_debugger) {
167 kdb_printf("%s", buffer);
168 } else {
Frank Seidel4d939152009-02-04 17:03:07 +0100169 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171#else
Lv Zheng836d083012015-12-03 10:43:14 +0800172 if (acpi_debugger_write_log(buffer) < 0)
Lv Zheng8cfb0cd2015-12-03 10:43:00 +0800173 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#endif
175}
176
Takao Indoh4996c022011-07-14 18:05:21 -0400177#ifdef CONFIG_KEXEC
178static unsigned long acpi_rsdp;
179static int __init setup_acpi_rsdp(char *arg)
180{
Christoph Jaeger3d915892014-06-13 21:49:58 +0200181 if (kstrtoul(arg, 16, &acpi_rsdp))
182 return -EINVAL;
Takao Indoh4996c022011-07-14 18:05:21 -0400183 return 0;
184}
185early_param("acpi_rsdp", setup_acpi_rsdp);
186#endif
187
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300188acpi_physical_address __init acpi_os_get_root_pointer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Takao Indoh4996c022011-07-14 18:05:21 -0400190#ifdef CONFIG_KEXEC
191 if (acpi_rsdp)
192 return acpi_rsdp;
193#endif
194
Matt Fleming83e68182012-11-14 09:42:35 +0000195 if (efi_enabled(EFI_CONFIG_TABLES)) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800196 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300197 return efi.acpi20;
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800198 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300199 return efi.acpi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400201 printk(KERN_ERR PREFIX
202 "System description tables not found\n");
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Graeme Gregory8a1664b2014-07-18 18:02:52 +0800205 } else if (IS_ENABLED(CONFIG_ACPI_LEGACY_TABLES_LOOKUP)) {
Len Brown239665a2007-11-23 20:08:02 -0500206 acpi_physical_address pa = 0;
207
208 acpi_find_root_pointer(&pa);
209 return pa;
210 }
Graeme Gregory8a1664b2014-07-18 18:02:52 +0800211
212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600215/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe4a3cba52010-10-21 14:24:14 -0600216static struct acpi_ioremap *
217acpi_map_lookup(acpi_physical_address phys, acpi_size size)
Myron Stowe620242a2010-10-21 14:23:53 -0600218{
219 struct acpi_ioremap *map;
220
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600221 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe620242a2010-10-21 14:23:53 -0600222 if (map->phys <= phys &&
223 phys + size <= map->phys + map->size)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600224 return map;
225
226 return NULL;
227}
228
229/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
230static void __iomem *
231acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
232{
233 struct acpi_ioremap *map;
234
235 map = acpi_map_lookup(phys, size);
236 if (map)
237 return map->virt + (phys - map->phys);
Myron Stowe620242a2010-10-21 14:23:53 -0600238
239 return NULL;
240}
241
Rafael J. Wysocki13606a22011-02-08 23:38:25 +0100242void __iomem *acpi_os_get_iomem(acpi_physical_address phys, unsigned int size)
243{
244 struct acpi_ioremap *map;
245 void __iomem *virt = NULL;
246
247 mutex_lock(&acpi_ioremap_lock);
248 map = acpi_map_lookup(phys, size);
249 if (map) {
250 virt = map->virt + (phys - map->phys);
251 map->refcount++;
252 }
253 mutex_unlock(&acpi_ioremap_lock);
254 return virt;
255}
256EXPORT_SYMBOL_GPL(acpi_os_get_iomem);
257
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600258/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe620242a2010-10-21 14:23:53 -0600259static struct acpi_ioremap *
260acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
261{
262 struct acpi_ioremap *map;
263
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600264 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600265 if (map->virt <= virt &&
266 virt + size <= map->virt + map->size)
Myron Stowe620242a2010-10-21 14:23:53 -0600267 return map;
268
269 return NULL;
270}
271
Graeme Gregoryaafc65c2015-03-24 14:02:35 +0000272#if defined(CONFIG_IA64) || defined(CONFIG_ARM64)
Myron Stoweba242d52012-01-20 19:13:30 -0700273/* ioremap will take care of cache attributes */
274#define should_use_kmap(pfn) 0
Graeme Gregoryaafc65c2015-03-24 14:02:35 +0000275#else
276#define should_use_kmap(pfn) page_is_ram(pfn)
Myron Stoweba242d52012-01-20 19:13:30 -0700277#endif
278
279static void __iomem *acpi_map(acpi_physical_address pg_off, unsigned long pg_sz)
280{
281 unsigned long pfn;
282
283 pfn = pg_off >> PAGE_SHIFT;
284 if (should_use_kmap(pfn)) {
285 if (pg_sz > PAGE_SIZE)
286 return NULL;
287 return (void __iomem __force *)kmap(pfn_to_page(pfn));
288 } else
289 return acpi_os_ioremap(pg_off, pg_sz);
290}
291
292static void acpi_unmap(acpi_physical_address pg_off, void __iomem *vaddr)
293{
294 unsigned long pfn;
295
296 pfn = pg_off >> PAGE_SHIFT;
Jan Beuliche2526752012-02-24 11:41:53 +0000297 if (should_use_kmap(pfn))
Myron Stoweba242d52012-01-20 19:13:30 -0700298 kunmap(pfn_to_page(pfn));
299 else
300 iounmap(vaddr);
301}
302
Rafael J. Wysocki9d128ed2016-01-02 03:10:29 +0100303/**
304 * acpi_os_map_iomem - Get a virtual address for a given physical address range.
305 * @phys: Start of the physical address range to map.
306 * @size: Size of the physical address range to map.
307 *
308 * Look up the given physical address range in the list of existing ACPI memory
309 * mappings. If found, get a reference to it and return a pointer to it (its
310 * virtual address). If not found, map it, add it to that list and return a
311 * pointer to it.
312 *
313 * During early init (when acpi_gbl_permanent_mmap has not been set yet) this
314 * routine simply calls __acpi_map_table() to get the job done.
315 */
Jan Beulich2fdf0742007-12-13 08:33:59 +0000316void __iomem *__init_refok
Lv Zhenga2383172014-05-20 15:39:41 +0800317acpi_os_map_iomem(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100319 struct acpi_ioremap *map;
Myron Stowe620242a2010-10-21 14:23:53 -0600320 void __iomem *virt;
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100321 acpi_physical_address pg_off;
322 acpi_size pg_sz;
Myron Stowe620242a2010-10-21 14:23:53 -0600323
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800324 if (phys > ULONG_MAX) {
325 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
Randy Dunlap70c08462007-02-13 16:11:36 -0800326 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Myron Stowe620242a2010-10-21 14:23:53 -0600328
329 if (!acpi_gbl_permanent_mmap)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300330 return __acpi_map_table((unsigned long)phys, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600331
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100332 mutex_lock(&acpi_ioremap_lock);
333 /* Check if there's a suitable mapping already. */
334 map = acpi_map_lookup(phys, size);
335 if (map) {
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100336 map->refcount++;
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100337 goto out;
338 }
339
Myron Stowe620242a2010-10-21 14:23:53 -0600340 map = kzalloc(sizeof(*map), GFP_KERNEL);
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100341 if (!map) {
342 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600343 return NULL;
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100344 }
Myron Stowe620242a2010-10-21 14:23:53 -0600345
Myron Stowe4a3cba52010-10-21 14:24:14 -0600346 pg_off = round_down(phys, PAGE_SIZE);
347 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
Myron Stoweba242d52012-01-20 19:13:30 -0700348 virt = acpi_map(pg_off, pg_sz);
Myron Stowe620242a2010-10-21 14:23:53 -0600349 if (!virt) {
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100350 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600351 kfree(map);
352 return NULL;
353 }
354
355 INIT_LIST_HEAD(&map->list);
356 map->virt = virt;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600357 map->phys = pg_off;
358 map->size = pg_sz;
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100359 map->refcount = 1;
Myron Stowe620242a2010-10-21 14:23:53 -0600360
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600361 list_add_tail_rcu(&map->list, &acpi_ioremaps);
Myron Stowe620242a2010-10-21 14:23:53 -0600362
Lv Zhenga2383172014-05-20 15:39:41 +0800363out:
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100364 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600365 return map->virt + (phys - map->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
Lv Zhenga2383172014-05-20 15:39:41 +0800367EXPORT_SYMBOL_GPL(acpi_os_map_iomem);
368
369void *__init_refok
370acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
371{
372 return (void *)acpi_os_map_iomem(phys, size);
373}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800374EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100376static void acpi_os_drop_map_ref(struct acpi_ioremap *map)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600377{
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100378 if (!--map->refcount)
379 list_del_rcu(&map->list);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600380}
Myron Stowe4a3cba52010-10-21 14:24:14 -0600381
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100382static void acpi_os_map_cleanup(struct acpi_ioremap *map)
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100383{
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100384 if (!map->refcount) {
Konstantin Khlebnikov74b51ee2014-11-09 13:53:37 +0400385 synchronize_rcu_expedited();
Myron Stoweba242d52012-01-20 19:13:30 -0700386 acpi_unmap(map->phys, map->virt);
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100387 kfree(map);
388 }
Myron Stowe4a3cba52010-10-21 14:24:14 -0600389}
390
Rafael J. Wysocki9d128ed2016-01-02 03:10:29 +0100391/**
392 * acpi_os_unmap_iomem - Drop a memory mapping reference.
393 * @virt: Start of the address range to drop a reference to.
394 * @size: Size of the address range to drop a reference to.
395 *
396 * Look up the given virtual address range in the list of existing ACPI memory
397 * mappings, drop a reference to it and unmap it if there are no more active
398 * references to it.
399 *
400 * During early init (when acpi_gbl_permanent_mmap has not been set yet) this
401 * routine simply calls __acpi_unmap_table() to get the job done. Since
402 * __acpi_unmap_table() is an __init function, the __ref annotation is needed
403 * here.
404 */
Lv Zhenga2383172014-05-20 15:39:41 +0800405void __ref acpi_os_unmap_iomem(void __iomem *virt, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Myron Stowe620242a2010-10-21 14:23:53 -0600407 struct acpi_ioremap *map;
Myron Stowe620242a2010-10-21 14:23:53 -0600408
409 if (!acpi_gbl_permanent_mmap) {
Yinghai Lu7d972772009-02-07 15:39:41 -0800410 __acpi_unmap_table(virt, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600411 return;
412 }
413
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100414 mutex_lock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600415 map = acpi_map_lookup_virt(virt, size);
416 if (!map) {
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100417 mutex_unlock(&acpi_ioremap_lock);
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100418 WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
Myron Stowe620242a2010-10-21 14:23:53 -0600419 return;
420 }
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100421 acpi_os_drop_map_ref(map);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100422 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600423
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100424 acpi_os_map_cleanup(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
Lv Zhenga2383172014-05-20 15:39:41 +0800426EXPORT_SYMBOL_GPL(acpi_os_unmap_iomem);
427
428void __ref acpi_os_unmap_memory(void *virt, acpi_size size)
429{
430 return acpi_os_unmap_iomem((void __iomem *)virt, size);
431}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800432EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800434void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Yinghai Lu7d972772009-02-07 15:39:41 -0800435{
436 if (!acpi_gbl_permanent_mmap)
437 __acpi_unmap_table(virt, size);
438}
439
Myron Stowe6f68c912011-11-07 16:23:34 -0700440int acpi_os_map_generic_address(struct acpi_generic_address *gas)
Myron Stowe29718522010-10-21 14:23:59 -0600441{
Myron Stowebc9ffce2011-11-07 16:23:27 -0700442 u64 addr;
Myron Stowe29718522010-10-21 14:23:59 -0600443 void __iomem *virt;
444
Myron Stowebc9ffce2011-11-07 16:23:27 -0700445 if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
Myron Stowe29718522010-10-21 14:23:59 -0600446 return 0;
447
Myron Stowebc9ffce2011-11-07 16:23:27 -0700448 /* Handle possible alignment issues */
449 memcpy(&addr, &gas->address, sizeof(addr));
450 if (!addr || !gas->bit_width)
Myron Stowe29718522010-10-21 14:23:59 -0600451 return -EINVAL;
452
Lv Zhenga2383172014-05-20 15:39:41 +0800453 virt = acpi_os_map_iomem(addr, gas->bit_width / 8);
Myron Stowe29718522010-10-21 14:23:59 -0600454 if (!virt)
455 return -EIO;
456
457 return 0;
458}
Myron Stowe6f68c912011-11-07 16:23:34 -0700459EXPORT_SYMBOL(acpi_os_map_generic_address);
Myron Stowe29718522010-10-21 14:23:59 -0600460
Myron Stowe6f68c912011-11-07 16:23:34 -0700461void acpi_os_unmap_generic_address(struct acpi_generic_address *gas)
Myron Stowe29718522010-10-21 14:23:59 -0600462{
Myron Stowebc9ffce2011-11-07 16:23:27 -0700463 u64 addr;
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100464 struct acpi_ioremap *map;
Myron Stowe29718522010-10-21 14:23:59 -0600465
Myron Stowebc9ffce2011-11-07 16:23:27 -0700466 if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
Myron Stowe29718522010-10-21 14:23:59 -0600467 return;
468
Myron Stowebc9ffce2011-11-07 16:23:27 -0700469 /* Handle possible alignment issues */
470 memcpy(&addr, &gas->address, sizeof(addr));
471 if (!addr || !gas->bit_width)
Myron Stowe29718522010-10-21 14:23:59 -0600472 return;
473
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100474 mutex_lock(&acpi_ioremap_lock);
Myron Stowebc9ffce2011-11-07 16:23:27 -0700475 map = acpi_map_lookup(addr, gas->bit_width / 8);
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100476 if (!map) {
477 mutex_unlock(&acpi_ioremap_lock);
478 return;
479 }
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100480 acpi_os_drop_map_ref(map);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100481 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe29718522010-10-21 14:23:59 -0600482
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100483 acpi_os_map_cleanup(map);
Myron Stowe29718522010-10-21 14:23:59 -0600484}
Myron Stowe6f68c912011-11-07 16:23:34 -0700485EXPORT_SYMBOL(acpi_os_unmap_generic_address);
Myron Stowe29718522010-10-21 14:23:59 -0600486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487#ifdef ACPI_FUTURE_USAGE
488acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400489acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Len Brown4be44fc2005-08-05 00:44:28 -0400491 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return AE_BAD_PARAMETER;
493
494 *phys = virt_to_phys(virt);
495
496 return AE_OK;
497}
498#endif
499
Rafael J. Wysocki18d78b62015-07-03 01:06:00 +0200500#ifdef CONFIG_ACPI_REV_OVERRIDE_POSSIBLE
501static bool acpi_rev_override;
502
503int __init acpi_rev_override_setup(char *str)
504{
505 acpi_rev_override = true;
506 return 1;
507}
508__setup("acpi_rev_override", acpi_rev_override_setup);
509#else
510#define acpi_rev_override false
511#endif
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513#define ACPI_MAX_OVERRIDE_LEN 100
514
515static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
516
517acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400518acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
Dominik Brodowski2bad7e22015-05-14 15:31:25 +0200519 char **new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 if (!init_val || !new_val)
522 return AE_BAD_PARAMETER;
523
524 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400525 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400527 acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *new_val = acpi_os_name;
529 }
530
Rafael J. Wysocki18d78b62015-07-03 01:06:00 +0200531 if (!memcmp(init_val->name, "_REV", 4) && acpi_rev_override) {
532 printk(KERN_INFO PREFIX "Overriding _REV return value to 5\n");
533 *new_val = (char *)5;
534 }
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return AE_OK;
537}
538
Lv Zhenga543132e2016-03-02 14:16:17 +0800539static void acpi_table_taint(struct acpi_table_header *table)
540{
541 pr_warn(PREFIX
542 "Override [%4.4s-%8.8s], this is unsafe: tainting kernel\n",
543 table->signature, table->oem_table_id);
544 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
545}
546
Thomas Renninger53aac442012-10-01 00:23:54 +0200547#ifdef CONFIG_ACPI_INITRD_TABLE_OVERRIDE
548#include <linux/earlycpio.h>
549#include <linux/memblock.h>
550
551static u64 acpi_tables_addr;
552static int all_tables_size;
553
554/* Copied from acpica/tbutils.c:acpi_tb_checksum() */
Rashika66e162b2013-12-17 14:43:05 +0530555static u8 __init acpi_table_checksum(u8 *buffer, u32 length)
Thomas Renninger53aac442012-10-01 00:23:54 +0200556{
557 u8 sum = 0;
558 u8 *end = buffer + length;
559
560 while (buffer < end)
561 sum = (u8) (sum + *(buffer++));
562 return sum;
563}
564
565/* All but ACPI_SIG_RSDP and ACPI_SIG_FACS: */
566static const char * const table_sigs[] = {
567 ACPI_SIG_BERT, ACPI_SIG_CPEP, ACPI_SIG_ECDT, ACPI_SIG_EINJ,
568 ACPI_SIG_ERST, ACPI_SIG_HEST, ACPI_SIG_MADT, ACPI_SIG_MSCT,
569 ACPI_SIG_SBST, ACPI_SIG_SLIT, ACPI_SIG_SRAT, ACPI_SIG_ASF,
570 ACPI_SIG_BOOT, ACPI_SIG_DBGP, ACPI_SIG_DMAR, ACPI_SIG_HPET,
571 ACPI_SIG_IBFT, ACPI_SIG_IVRS, ACPI_SIG_MCFG, ACPI_SIG_MCHI,
572 ACPI_SIG_SLIC, ACPI_SIG_SPCR, ACPI_SIG_SPMI, ACPI_SIG_TCPA,
573 ACPI_SIG_UEFI, ACPI_SIG_WAET, ACPI_SIG_WDAT, ACPI_SIG_WDDT,
574 ACPI_SIG_WDRT, ACPI_SIG_DSDT, ACPI_SIG_FADT, ACPI_SIG_PSDT,
575 ACPI_SIG_RSDT, ACPI_SIG_XSDT, ACPI_SIG_SSDT, NULL };
576
Thomas Renninger53aac442012-10-01 00:23:54 +0200577#define ACPI_HEADER_SIZE sizeof(struct acpi_table_header)
578
Yinghai Lubee7f9c2013-09-06 19:08:00 -0700579#define ACPI_OVERRIDE_TABLES 64
580static struct cpio_data __initdata acpi_initrd_files[ACPI_OVERRIDE_TABLES];
Lv Zhengc85cc812016-03-02 14:16:25 +0800581static DECLARE_BITMAP(acpi_initrd_installed, ACPI_OVERRIDE_TABLES);
Yinghai Lubee7f9c2013-09-06 19:08:00 -0700582
583#define MAP_CHUNK_SIZE (NR_FIX_BTMAPS << PAGE_SHIFT)
Thomas Renninger53aac442012-10-01 00:23:54 +0200584
585void __init acpi_initrd_override(void *data, size_t size)
586{
587 int sig, no, table_nr = 0, total_offset = 0;
588 long offset = 0;
589 struct acpi_table_header *table;
590 char cpio_path[32] = "kernel/firmware/acpi/";
591 struct cpio_data file;
Thomas Renninger53aac442012-10-01 00:23:54 +0200592
593 if (data == NULL || size == 0)
594 return;
595
596 for (no = 0; no < ACPI_OVERRIDE_TABLES; no++) {
597 file = find_cpio_data(cpio_path, data, size, &offset);
598 if (!file.data)
599 break;
600
601 data += offset;
602 size -= offset;
603
Tang Chen7702ae02013-08-14 17:37:08 +0800604 if (file.size < sizeof(struct acpi_table_header)) {
605 pr_err("ACPI OVERRIDE: Table smaller than ACPI header [%s%s]\n",
606 cpio_path, file.name);
607 continue;
608 }
Thomas Renninger53aac442012-10-01 00:23:54 +0200609
610 table = file.data;
611
612 for (sig = 0; table_sigs[sig]; sig++)
613 if (!memcmp(table->signature, table_sigs[sig], 4))
614 break;
615
Tang Chen7702ae02013-08-14 17:37:08 +0800616 if (!table_sigs[sig]) {
617 pr_err("ACPI OVERRIDE: Unknown signature [%s%s]\n",
618 cpio_path, file.name);
619 continue;
620 }
621 if (file.size != table->length) {
622 pr_err("ACPI OVERRIDE: File length does not match table length [%s%s]\n",
623 cpio_path, file.name);
624 continue;
625 }
626 if (acpi_table_checksum(file.data, table->length)) {
627 pr_err("ACPI OVERRIDE: Bad table checksum [%s%s]\n",
628 cpio_path, file.name);
629 continue;
630 }
Thomas Renninger53aac442012-10-01 00:23:54 +0200631
632 pr_info("%4.4s ACPI table found in initrd [%s%s][0x%x]\n",
633 table->signature, cpio_path, file.name, table->length);
634
635 all_tables_size += table->length;
Yinghai Lubee7f9c2013-09-06 19:08:00 -0700636 acpi_initrd_files[table_nr].data = file.data;
637 acpi_initrd_files[table_nr].size = file.size;
Thomas Renninger53aac442012-10-01 00:23:54 +0200638 table_nr++;
639 }
640 if (table_nr == 0)
641 return;
642
643 acpi_tables_addr =
644 memblock_find_in_range(0, max_low_pfn_mapped << PAGE_SHIFT,
645 all_tables_size, PAGE_SIZE);
646 if (!acpi_tables_addr) {
647 WARN_ON(1);
648 return;
649 }
650 /*
651 * Only calling e820_add_reserve does not work and the
652 * tables are invalid (memory got used) later.
653 * memblock_reserve works as expected and the tables won't get modified.
654 * But it's not enough on X86 because ioremap will
655 * complain later (used by acpi_os_map_memory) that the pages
656 * that should get mapped are not marked "reserved".
657 * Both memblock_reserve and e820_add_region (via arch_reserve_mem_area)
658 * works fine.
659 */
Wang YanQinga6432de2013-04-23 01:19:19 +0200660 memblock_reserve(acpi_tables_addr, all_tables_size);
Thomas Renninger53aac442012-10-01 00:23:54 +0200661 arch_reserve_mem_area(acpi_tables_addr, all_tables_size);
662
Yinghai Lubee7f9c2013-09-06 19:08:00 -0700663 /*
664 * early_ioremap only can remap 256k one time. If we map all
665 * tables one time, we will hit the limit. Need to map chunks
666 * one by one during copying the same as that in relocate_initrd().
667 */
Thomas Renninger53aac442012-10-01 00:23:54 +0200668 for (no = 0; no < table_nr; no++) {
Yinghai Lubee7f9c2013-09-06 19:08:00 -0700669 unsigned char *src_p = acpi_initrd_files[no].data;
670 phys_addr_t size = acpi_initrd_files[no].size;
671 phys_addr_t dest_addr = acpi_tables_addr + total_offset;
672 phys_addr_t slop, clen;
673 char *dest_p;
674
675 total_offset += size;
676
677 while (size) {
678 slop = dest_addr & ~PAGE_MASK;
679 clen = size;
680 if (clen > MAP_CHUNK_SIZE - slop)
681 clen = MAP_CHUNK_SIZE - slop;
682 dest_p = early_ioremap(dest_addr & PAGE_MASK,
683 clen + slop);
684 memcpy(dest_p + slop, src_p, clen);
685 early_iounmap(dest_p, clen + slop);
686 src_p += clen;
687 dest_addr += clen;
688 size -= clen;
689 }
Thomas Renninger53aac442012-10-01 00:23:54 +0200690 }
Thomas Renninger53aac442012-10-01 00:23:54 +0200691}
Thomas Renninger325a8d362012-10-01 00:23:56 +0200692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693acpi_status
Lv Zhenga543132e2016-03-02 14:16:17 +0800694acpi_os_physical_table_override(struct acpi_table_header *existing_table,
695 acpi_physical_address *address, u32 *length)
696{
697 int table_offset = 0;
Lv Zhengc85cc812016-03-02 14:16:25 +0800698 int table_index = 0;
Lv Zhenga543132e2016-03-02 14:16:17 +0800699 struct acpi_table_header *table;
700 u32 table_length;
701
702 *length = 0;
703 *address = 0;
704 if (!acpi_tables_addr)
705 return AE_OK;
706
707 while (table_offset + ACPI_HEADER_SIZE <= all_tables_size) {
708 table = acpi_os_map_memory(acpi_tables_addr + table_offset,
709 ACPI_HEADER_SIZE);
710 if (table_offset + table->length > all_tables_size) {
711 acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
712 WARN_ON(1);
713 return AE_OK;
714 }
715
716 table_length = table->length;
717
718 /* Only override tables matched */
Lv Zhengc85cc812016-03-02 14:16:25 +0800719 if (test_bit(table_index, acpi_initrd_installed) ||
720 memcmp(existing_table->signature, table->signature, 4) ||
Lv Zhenga543132e2016-03-02 14:16:17 +0800721 memcmp(table->oem_table_id, existing_table->oem_table_id,
722 ACPI_OEM_TABLE_ID_SIZE)) {
723 acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
724 goto next_table;
725 }
726
727 *length = table_length;
728 *address = acpi_tables_addr + table_offset;
729 acpi_table_taint(existing_table);
730 acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
Lv Zhengc85cc812016-03-02 14:16:25 +0800731 set_bit(table_index, acpi_initrd_installed);
Lv Zhenga543132e2016-03-02 14:16:17 +0800732 break;
733
734next_table:
735 table_offset += table_length;
Lv Zhengc85cc812016-03-02 14:16:25 +0800736 table_index++;
Lv Zhenga543132e2016-03-02 14:16:17 +0800737 }
738 return AE_OK;
739}
Lv Zhengc85cc812016-03-02 14:16:25 +0800740
741void __init acpi_initrd_initialize_tables(void)
742{
743 int table_offset = 0;
744 int table_index = 0;
745 u32 table_length;
746 struct acpi_table_header *table;
747
748 if (!acpi_tables_addr)
749 return;
750
751 while (table_offset + ACPI_HEADER_SIZE <= all_tables_size) {
752 table = acpi_os_map_memory(acpi_tables_addr + table_offset,
753 ACPI_HEADER_SIZE);
754 if (table_offset + table->length > all_tables_size) {
755 acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
756 WARN_ON(1);
757 return;
758 }
759
760 table_length = table->length;
761
762 /* Skip RSDT/XSDT which should only be used for override */
763 if (test_bit(table_index, acpi_initrd_installed) ||
764 ACPI_COMPARE_NAME(table->signature, ACPI_SIG_RSDT) ||
765 ACPI_COMPARE_NAME(table->signature, ACPI_SIG_XSDT)) {
766 acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
767 goto next_table;
768 }
769
770 acpi_table_taint(table);
771 acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
772 acpi_install_table(acpi_tables_addr + table_offset, TRUE);
773 set_bit(table_index, acpi_initrd_installed);
774next_table:
775 table_offset += table_length;
776 table_index++;
777 }
778}
Lv Zhenga543132e2016-03-02 14:16:17 +0800779#else
780acpi_status
781acpi_os_physical_table_override(struct acpi_table_header *existing_table,
782 acpi_physical_address *address,
783 u32 *table_length)
784{
785 *table_length = 0;
786 *address = 0;
787 return AE_OK;
788}
Lv Zhengc85cc812016-03-02 14:16:25 +0800789
790void __init acpi_initrd_initialize_tables(void)
791{
792}
Lv Zhenga543132e2016-03-02 14:16:17 +0800793#endif /* CONFIG_ACPI_INITRD_TABLE_OVERRIDE */
794
795acpi_status
796acpi_os_table_override(struct acpi_table_header *existing_table,
797 struct acpi_table_header **new_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 if (!existing_table || !new_table)
800 return AE_BAD_PARAMETER;
801
Markus Gaugusch71fc47a2008-02-05 00:04:06 +0100802 *new_table = NULL;
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804#ifdef CONFIG_ACPI_CUSTOM_DSDT
805 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400806 *new_table = (struct acpi_table_header *)AmlCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807#endif
Thomas Renninger325a8d362012-10-01 00:23:56 +0200808 if (*new_table != NULL)
809 acpi_table_taint(existing_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return AE_OK;
811}
812
David Howells7d12e782006-10-05 14:55:46 +0100813static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Len Brown5229e872008-02-06 01:26:55 -0500815 u32 handled;
816
817 handled = (*acpi_irq_handler) (acpi_irq_context);
818
819 if (handled) {
820 acpi_irq_handled++;
821 return IRQ_HANDLED;
Len Brown88bea182009-04-21 00:35:47 -0400822 } else {
823 acpi_irq_not_handled++;
Len Brown5229e872008-02-06 01:26:55 -0500824 return IRQ_NONE;
Len Brown88bea182009-04-21 00:35:47 -0400825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
828acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400829acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
830 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 unsigned int irq;
833
Len Brown5229e872008-02-06 01:26:55 -0500834 acpi_irq_stats_init();
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /*
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100837 * ACPI interrupts different from the SCI in our copy of the FADT are
838 * not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 */
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100840 if (gsi != acpi_gbl_FADT.sci_interrupt)
841 return AE_BAD_PARAMETER;
842
843 if (acpi_irq_handler)
844 return AE_ALREADY_ACQUIRED;
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
847 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
848 gsi);
849 return AE_OK;
850 }
851
852 acpi_irq_handler = handler;
853 acpi_irq_context = context;
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200854 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100856 acpi_irq_handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return AE_NOT_ACQUIRED;
858 }
Chen Yu49e4b8432015-10-25 01:02:19 +0800859 acpi_sci_irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 return AE_OK;
862}
863
Chen Yu49e4b8432015-10-25 01:02:19 +0800864acpi_status acpi_os_remove_interrupt_handler(u32 gsi, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Chen Yu49e4b8432015-10-25 01:02:19 +0800866 if (gsi != acpi_gbl_FADT.sci_interrupt || !acpi_sci_irq_valid())
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100867 return AE_BAD_PARAMETER;
868
Chen Yu49e4b8432015-10-25 01:02:19 +0800869 free_irq(acpi_sci_irq, acpi_irq);
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100870 acpi_irq_handler = NULL;
Chen Yu49e4b8432015-10-25 01:02:19 +0800871 acpi_sci_irq = INVALID_ACPI_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 return AE_OK;
874}
875
876/*
877 * Running in interpreter thread context, safe to sleep
878 */
879
Lin Ming439913f2010-01-28 10:53:19 +0800880void acpi_os_sleep(u64 ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Liu Chuansheng30282292013-09-12 01:42:57 +0800882 msleep(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
Len Brown4be44fc2005-08-05 00:44:28 -0400884
Len Brown4be44fc2005-08-05 00:44:28 -0400885void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 while (us) {
888 u32 delay = 1000;
889
890 if (delay > us)
891 delay = us;
892 udelay(delay);
893 touch_nmi_watchdog();
894 us -= delay;
895 }
896}
Len Brown4be44fc2005-08-05 00:44:28 -0400897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898/*
899 * Support ACPI 3.0 AML Timer operand
900 * Returns 64-bit free-running, monotonically increasing timer
901 * with 100ns granularity
902 */
Len Brown4be44fc2005-08-05 00:44:28 -0400903u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Mika Westerberg10619062013-05-23 10:27:46 +0300905 u64 time_ns = ktime_to_ns(ktime_get());
906 do_div(time_ns, 100);
907 return time_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Len Brown4be44fc2005-08-05 00:44:28 -0400910acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 u32 dummy;
913
914 if (!value)
915 value = &dummy;
916
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800917 *value = 0;
918 if (width <= 8) {
Len Brown4be44fc2005-08-05 00:44:28 -0400919 *(u8 *) value = inb(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800920 } else if (width <= 16) {
Len Brown4be44fc2005-08-05 00:44:28 -0400921 *(u16 *) value = inw(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800922 } else if (width <= 32) {
Len Brown4be44fc2005-08-05 00:44:28 -0400923 *(u32 *) value = inl(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800924 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 BUG();
926 }
927
928 return AE_OK;
929}
Len Brown4be44fc2005-08-05 00:44:28 -0400930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931EXPORT_SYMBOL(acpi_os_read_port);
932
Len Brown4be44fc2005-08-05 00:44:28 -0400933acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800935 if (width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 outb(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800937 } else if (width <= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 outw(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800939 } else if (width <= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 outl(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800941 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 BUG();
943 }
944
945 return AE_OK;
946}
Len Brown4be44fc2005-08-05 00:44:28 -0400947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948EXPORT_SYMBOL(acpi_os_write_port);
949
Myron Stowee615bf52012-01-20 19:13:24 -0700950acpi_status
Bob Moore653f4b52012-02-14 18:29:55 +0800951acpi_os_read_memory(acpi_physical_address phys_addr, u64 *value, u32 width)
Myron Stowee615bf52012-01-20 19:13:24 -0700952{
953 void __iomem *virt_addr;
954 unsigned int size = width / 8;
955 bool unmap = false;
956 u64 dummy;
957
958 rcu_read_lock();
959 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
960 if (!virt_addr) {
961 rcu_read_unlock();
962 virt_addr = acpi_os_ioremap(phys_addr, size);
963 if (!virt_addr)
964 return AE_BAD_ADDRESS;
965 unmap = true;
966 }
967
968 if (!value)
969 value = &dummy;
970
971 switch (width) {
972 case 8:
973 *(u8 *) value = readb(virt_addr);
974 break;
975 case 16:
976 *(u16 *) value = readw(virt_addr);
977 break;
978 case 32:
979 *(u32 *) value = readl(virt_addr);
980 break;
981 case 64:
Andy Shevchenko3277b4e2015-08-17 17:28:46 +0300982 *(u64 *) value = readq(virt_addr);
Myron Stowee615bf52012-01-20 19:13:24 -0700983 break;
984 default:
985 BUG();
986 }
987
988 if (unmap)
989 iounmap(virt_addr);
990 else
991 rcu_read_unlock();
992
993 return AE_OK;
994}
995
Myron Stowee615bf52012-01-20 19:13:24 -0700996acpi_status
Bob Moore653f4b52012-02-14 18:29:55 +0800997acpi_os_write_memory(acpi_physical_address phys_addr, u64 value, u32 width)
Myron Stowee615bf52012-01-20 19:13:24 -0700998{
999 void __iomem *virt_addr;
1000 unsigned int size = width / 8;
1001 bool unmap = false;
1002
1003 rcu_read_lock();
1004 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
1005 if (!virt_addr) {
1006 rcu_read_unlock();
1007 virt_addr = acpi_os_ioremap(phys_addr, size);
1008 if (!virt_addr)
1009 return AE_BAD_ADDRESS;
1010 unmap = true;
1011 }
1012
1013 switch (width) {
1014 case 8:
1015 writeb(value, virt_addr);
1016 break;
1017 case 16:
1018 writew(value, virt_addr);
1019 break;
1020 case 32:
1021 writel(value, virt_addr);
1022 break;
1023 case 64:
Andy Shevchenko3277b4e2015-08-17 17:28:46 +03001024 writeq(value, virt_addr);
Myron Stowee615bf52012-01-20 19:13:24 -07001025 break;
1026 default:
1027 BUG();
1028 }
1029
1030 if (unmap)
1031 iounmap(virt_addr);
1032 else
1033 rcu_read_unlock();
1034
1035 return AE_OK;
1036}
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001039acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Bob Moorec5f02312010-08-06 08:57:53 +08001040 u64 *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 int result, size;
Bob Moorec5f02312010-08-06 08:57:53 +08001043 u32 value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 if (!value)
1046 return AE_BAD_PARAMETER;
1047
1048 switch (width) {
1049 case 8:
1050 size = 1;
1051 break;
1052 case 16:
1053 size = 2;
1054 break;
1055 case 32:
1056 size = 4;
1057 break;
1058 default:
1059 return AE_ERROR;
1060 }
1061
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -05001062 result = raw_pci_read(pci_id->segment, pci_id->bus,
1063 PCI_DEVFN(pci_id->device, pci_id->function),
Bob Moorec5f02312010-08-06 08:57:53 +08001064 reg, size, &value32);
1065 *value = value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 return (result ? AE_ERROR : AE_OK);
1068}
Len Brown4be44fc2005-08-05 00:44:28 -04001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001071acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Lin Ming439913f2010-01-28 10:53:19 +08001072 u64 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 int result, size;
1075
1076 switch (width) {
1077 case 8:
1078 size = 1;
1079 break;
1080 case 16:
1081 size = 2;
1082 break;
1083 case 32:
1084 size = 4;
1085 break;
1086 default:
1087 return AE_ERROR;
1088 }
1089
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -05001090 result = raw_pci_write(pci_id->segment, pci_id->bus,
1091 PCI_DEVFN(pci_id->device, pci_id->function),
1092 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 return (result ? AE_ERROR : AE_OK);
1095}
1096
David Howells65f27f32006-11-22 14:55:48 +00001097static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
David Howells65f27f32006-11-22 14:55:48 +00001099 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -04001100
Zhang Rui19cd8472008-08-28 10:05:06 +08001101 dpc->function(dpc->context);
1102 kfree(dpc);
Zhang Rui19cd8472008-08-28 10:05:06 +08001103}
1104
Lv Zheng836d083012015-12-03 10:43:14 +08001105#ifdef CONFIG_ACPI_DEBUGGER
1106static struct acpi_debugger acpi_debugger;
1107static bool acpi_debugger_initialized;
1108
1109int acpi_register_debugger(struct module *owner,
1110 const struct acpi_debugger_ops *ops)
1111{
1112 int ret = 0;
1113
1114 mutex_lock(&acpi_debugger.lock);
1115 if (acpi_debugger.ops) {
1116 ret = -EBUSY;
1117 goto err_lock;
1118 }
1119
1120 acpi_debugger.owner = owner;
1121 acpi_debugger.ops = ops;
1122
1123err_lock:
1124 mutex_unlock(&acpi_debugger.lock);
1125 return ret;
1126}
1127EXPORT_SYMBOL(acpi_register_debugger);
1128
1129void acpi_unregister_debugger(const struct acpi_debugger_ops *ops)
1130{
1131 mutex_lock(&acpi_debugger.lock);
1132 if (ops == acpi_debugger.ops) {
1133 acpi_debugger.ops = NULL;
1134 acpi_debugger.owner = NULL;
1135 }
1136 mutex_unlock(&acpi_debugger.lock);
1137}
1138EXPORT_SYMBOL(acpi_unregister_debugger);
1139
1140int acpi_debugger_create_thread(acpi_osd_exec_callback function, void *context)
1141{
1142 int ret;
1143 int (*func)(acpi_osd_exec_callback, void *);
1144 struct module *owner;
1145
1146 if (!acpi_debugger_initialized)
1147 return -ENODEV;
1148 mutex_lock(&acpi_debugger.lock);
1149 if (!acpi_debugger.ops) {
1150 ret = -ENODEV;
1151 goto err_lock;
1152 }
1153 if (!try_module_get(acpi_debugger.owner)) {
1154 ret = -ENODEV;
1155 goto err_lock;
1156 }
1157 func = acpi_debugger.ops->create_thread;
1158 owner = acpi_debugger.owner;
1159 mutex_unlock(&acpi_debugger.lock);
1160
1161 ret = func(function, context);
1162
1163 mutex_lock(&acpi_debugger.lock);
1164 module_put(owner);
1165err_lock:
1166 mutex_unlock(&acpi_debugger.lock);
1167 return ret;
1168}
1169
1170ssize_t acpi_debugger_write_log(const char *msg)
1171{
1172 ssize_t ret;
1173 ssize_t (*func)(const char *);
1174 struct module *owner;
1175
1176 if (!acpi_debugger_initialized)
1177 return -ENODEV;
1178 mutex_lock(&acpi_debugger.lock);
1179 if (!acpi_debugger.ops) {
1180 ret = -ENODEV;
1181 goto err_lock;
1182 }
1183 if (!try_module_get(acpi_debugger.owner)) {
1184 ret = -ENODEV;
1185 goto err_lock;
1186 }
1187 func = acpi_debugger.ops->write_log;
1188 owner = acpi_debugger.owner;
1189 mutex_unlock(&acpi_debugger.lock);
1190
1191 ret = func(msg);
1192
1193 mutex_lock(&acpi_debugger.lock);
1194 module_put(owner);
1195err_lock:
1196 mutex_unlock(&acpi_debugger.lock);
1197 return ret;
1198}
1199
1200ssize_t acpi_debugger_read_cmd(char *buffer, size_t buffer_length)
1201{
1202 ssize_t ret;
1203 ssize_t (*func)(char *, size_t);
1204 struct module *owner;
1205
1206 if (!acpi_debugger_initialized)
1207 return -ENODEV;
1208 mutex_lock(&acpi_debugger.lock);
1209 if (!acpi_debugger.ops) {
1210 ret = -ENODEV;
1211 goto err_lock;
1212 }
1213 if (!try_module_get(acpi_debugger.owner)) {
1214 ret = -ENODEV;
1215 goto err_lock;
1216 }
1217 func = acpi_debugger.ops->read_cmd;
1218 owner = acpi_debugger.owner;
1219 mutex_unlock(&acpi_debugger.lock);
1220
1221 ret = func(buffer, buffer_length);
1222
1223 mutex_lock(&acpi_debugger.lock);
1224 module_put(owner);
1225err_lock:
1226 mutex_unlock(&acpi_debugger.lock);
1227 return ret;
1228}
1229
1230int acpi_debugger_wait_command_ready(void)
1231{
1232 int ret;
1233 int (*func)(bool, char *, size_t);
1234 struct module *owner;
1235
1236 if (!acpi_debugger_initialized)
1237 return -ENODEV;
1238 mutex_lock(&acpi_debugger.lock);
1239 if (!acpi_debugger.ops) {
1240 ret = -ENODEV;
1241 goto err_lock;
1242 }
1243 if (!try_module_get(acpi_debugger.owner)) {
1244 ret = -ENODEV;
1245 goto err_lock;
1246 }
1247 func = acpi_debugger.ops->wait_command_ready;
1248 owner = acpi_debugger.owner;
1249 mutex_unlock(&acpi_debugger.lock);
1250
1251 ret = func(acpi_gbl_method_executing,
1252 acpi_gbl_db_line_buf, ACPI_DB_LINE_BUFFER_SIZE);
1253
1254 mutex_lock(&acpi_debugger.lock);
1255 module_put(owner);
1256err_lock:
1257 mutex_unlock(&acpi_debugger.lock);
1258 return ret;
1259}
1260
1261int acpi_debugger_notify_command_complete(void)
1262{
1263 int ret;
1264 int (*func)(void);
1265 struct module *owner;
1266
1267 if (!acpi_debugger_initialized)
1268 return -ENODEV;
1269 mutex_lock(&acpi_debugger.lock);
1270 if (!acpi_debugger.ops) {
1271 ret = -ENODEV;
1272 goto err_lock;
1273 }
1274 if (!try_module_get(acpi_debugger.owner)) {
1275 ret = -ENODEV;
1276 goto err_lock;
1277 }
1278 func = acpi_debugger.ops->notify_command_complete;
1279 owner = acpi_debugger.owner;
1280 mutex_unlock(&acpi_debugger.lock);
1281
1282 ret = func();
1283
1284 mutex_lock(&acpi_debugger.lock);
1285 module_put(owner);
1286err_lock:
1287 mutex_unlock(&acpi_debugger.lock);
1288 return ret;
1289}
1290
1291int __init acpi_debugger_init(void)
1292{
1293 mutex_init(&acpi_debugger.lock);
1294 acpi_debugger_initialized = true;
1295 return 0;
1296}
1297#endif
1298
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -04001299/*******************************************************************************
1300 *
1301 * FUNCTION: acpi_os_execute
1302 *
1303 * PARAMETERS: Type - Type of the callback
1304 * Function - Function to be executed
1305 * Context - Function parameters
1306 *
1307 * RETURN: Status
1308 *
1309 * DESCRIPTION: Depending on type, either queues function for deferred execution or
1310 * immediately executes function on a separate thread.
1311 *
1312 ******************************************************************************/
1313
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001314acpi_status acpi_os_execute(acpi_execute_type type,
1315 acpi_osd_exec_callback function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Len Brown4be44fc2005-08-05 00:44:28 -04001317 acpi_status status = AE_OK;
1318 struct acpi_os_dpc *dpc;
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +03001319 struct workqueue_struct *queue;
Zhang Rui19cd8472008-08-28 10:05:06 +08001320 int ret;
Len Brown72945b22006-07-12 22:46:42 -04001321 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
1322 "Scheduling function [%p(%p)] for deferred execution.\n",
1323 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001325 if (type == OSL_DEBUGGER_MAIN_THREAD) {
Lv Zheng836d083012015-12-03 10:43:14 +08001326 ret = acpi_debugger_create_thread(function, context);
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001327 if (ret) {
1328 pr_err("Call to kthread_create() failed.\n");
1329 status = AE_ERROR;
1330 }
1331 goto out_thread;
1332 }
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /*
1335 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +00001336 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * way that allows us to also free its memory inside the callee.
1338 * Because we may want to schedule several tasks with different
1339 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +00001340 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 */
Len Brown72945b22006-07-12 22:46:42 -04001342
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001343 dpc = kzalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (!dpc)
Lin Ming889c78b2008-12-31 09:23:57 +08001345 return AE_NO_MEMORY;
Linus Torvaldsb976fe12006-11-17 19:31:09 -08001346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 dpc->function = function;
1348 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -08001349
Zhang Ruic02256b2009-06-23 10:20:29 +08001350 /*
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001351 * To prevent lockdep from complaining unnecessarily, make sure that
1352 * there is a different static lockdep key for each workqueue by using
1353 * INIT_WORK() for each of them separately.
Zhang Ruic02256b2009-06-23 10:20:29 +08001354 */
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001355 if (type == OSL_NOTIFY_HANDLER) {
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001356 queue = kacpi_notify_wq;
Zhang Ruibc736752010-03-22 15:48:54 +08001357 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001358 } else if (type == OSL_GPE_HANDLER) {
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001359 queue = kacpid_wq;
Zhang Ruibc736752010-03-22 15:48:54 +08001360 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001361 } else {
1362 pr_err("Unsupported os_execute type %d.\n", type);
1363 status = AE_ERROR;
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001364 }
Zhang Ruibc736752010-03-22 15:48:54 +08001365
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001366 if (ACPI_FAILURE(status))
1367 goto err_workqueue;
1368
Tejun Heo8fec62b2010-06-29 10:07:09 +02001369 /*
1370 * On some machines, a software-initiated SMI causes corruption unless
1371 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
1372 * typically it's done in GPE-related methods that are run via
1373 * workqueues, so we can avoid the known corruption cases by always
1374 * queueing on CPU 0.
1375 */
1376 ret = queue_work_on(0, queue, &dpc->work);
Zhang Rui19cd8472008-08-28 10:05:06 +08001377 if (!ret) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001378 printk(KERN_ERR PREFIX
1379 "Call to queue_work() failed.\n");
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +03001380 status = AE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001382err_workqueue:
1383 if (ACPI_FAILURE(status))
1384 kfree(dpc);
1385out_thread:
Lin Ming889c78b2008-12-31 09:23:57 +08001386 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -04001388EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Lin Mingbd6f10a2012-05-22 16:43:49 +08001390void acpi_os_wait_events_complete(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Lv Zheng90253a72014-11-05 15:06:13 +08001392 /*
1393 * Make sure the GPE handler or the fixed event handler is not used
1394 * on another CPU after removal.
1395 */
Chen Yuefb1cf72015-10-25 01:02:36 +08001396 if (acpi_sci_irq_valid())
1397 synchronize_hardirq(acpi_sci_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 flush_workqueue(kacpid_wq);
Zhang Rui2f67a062008-04-29 02:34:42 -04001399 flush_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
Len Brown4be44fc2005-08-05 00:44:28 -04001401
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001402struct acpi_hp_work {
1403 struct work_struct work;
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +01001404 struct acpi_device *adev;
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001405 u32 src;
1406};
1407
1408static void acpi_hotplug_work_fn(struct work_struct *work)
1409{
1410 struct acpi_hp_work *hpw = container_of(work, struct acpi_hp_work, work);
1411
1412 acpi_os_wait_events_complete();
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +01001413 acpi_device_hotplug(hpw->adev, hpw->src);
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001414 kfree(hpw);
1415}
1416
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +01001417acpi_status acpi_hotplug_schedule(struct acpi_device *adev, u32 src)
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001418{
1419 struct acpi_hp_work *hpw;
1420
1421 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +01001422 "Scheduling hotplug event (%p, %u) for deferred execution.\n",
1423 adev, src));
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001424
1425 hpw = kmalloc(sizeof(*hpw), GFP_KERNEL);
1426 if (!hpw)
1427 return AE_NO_MEMORY;
1428
1429 INIT_WORK(&hpw->work, acpi_hotplug_work_fn);
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +01001430 hpw->adev = adev;
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001431 hpw->src = src;
1432 /*
1433 * We can't run hotplug code in kacpid_wq/kacpid_notify_wq etc., because
1434 * the hotplug code may call driver .remove() functions, which may
1435 * invoke flush_scheduled_work()/acpi_os_wait_events_complete() to flush
1436 * these workqueues.
1437 */
1438 if (!queue_work(kacpi_hotplug_wq, &hpw->work)) {
1439 kfree(hpw);
1440 return AE_ERROR;
1441 }
1442 return AE_OK;
1443}
1444
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001445bool acpi_queue_hotplug_work(struct work_struct *work)
1446{
1447 return queue_work(kacpi_hotplug_wq, work);
1448}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001451acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Len Brown4be44fc2005-08-05 00:44:28 -04001453 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
jhbird.choi@samsung.com2d0acb42014-03-20 16:35:56 +09001455 sem = acpi_os_allocate_zeroed(sizeof(struct semaphore));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -04001457 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 sema_init(sem, initial_units);
1460
Len Brown4be44fc2005-08-05 00:44:28 -04001461 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Len Brown4be44fc2005-08-05 00:44:28 -04001463 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
1464 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Patrick Mocheld550d982006-06-27 00:41:40 -04001466 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469/*
1470 * TODO: A better way to delete semaphores? Linux doesn't have a
1471 * 'delete_semaphore()' function -- may result in an invalid
1472 * pointer dereference for non-synchronized consumers. Should
1473 * we at least check for blocked threads and signal/cancel them?
1474 */
1475
Len Brown4be44fc2005-08-05 00:44:28 -04001476acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Len Brown4be44fc2005-08-05 00:44:28 -04001478 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -04001481 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Len Brown4be44fc2005-08-05 00:44:28 -04001483 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001485 BUG_ON(!list_empty(&sem->wait_list));
Len Brown02438d82006-06-30 03:19:10 -04001486 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -04001487 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Patrick Mocheld550d982006-06-27 00:41:40 -04001489 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 * TODO: Support for units > 1?
1494 */
Len Brown4be44fc2005-08-05 00:44:28 -04001495acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Len Brown4be44fc2005-08-05 00:44:28 -04001497 acpi_status status = AE_OK;
1498 struct semaphore *sem = (struct semaphore *)handle;
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001499 long jiffies;
Len Brown4be44fc2005-08-05 00:44:28 -04001500 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Lv Zheng7901a052015-08-05 16:23:51 +08001502 if (!acpi_os_initialized)
1503 return AE_OK;
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001506 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -04001509 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Len Brown4be44fc2005-08-05 00:44:28 -04001511 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
1512 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001514 if (timeout == ACPI_WAIT_FOREVER)
1515 jiffies = MAX_SCHEDULE_TIMEOUT;
1516 else
1517 jiffies = msecs_to_jiffies(timeout);
Al Stonecad1525a2013-12-04 12:59:11 -07001518
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001519 ret = down_timeout(sem, jiffies);
1520 if (ret)
1521 status = AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -04001524 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -04001525 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -04001526 handle, units, timeout,
1527 acpi_format_exception(status)));
1528 } else {
1529 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -04001530 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001531 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533
Patrick Mocheld550d982006-06-27 00:41:40 -04001534 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/*
1538 * TODO: Support for units > 1?
1539 */
Len Brown4be44fc2005-08-05 00:44:28 -04001540acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Len Brown4be44fc2005-08-05 00:44:28 -04001542 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Lv Zheng7901a052015-08-05 16:23:51 +08001544 if (!acpi_os_initialized)
1545 return AE_OK;
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001548 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -04001551 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Len Brown4be44fc2005-08-05 00:44:28 -04001553 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1554 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 up(sem);
1557
Patrick Mocheld550d982006-06-27 00:41:40 -04001558 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
Len Brown4be44fc2005-08-05 00:44:28 -04001560
Lv Zheng4d946f72015-10-19 10:25:56 +08001561acpi_status acpi_os_get_line(char *buffer, u32 buffer_length, u32 *bytes_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563#ifdef ENABLE_DEBUGGER
1564 if (acpi_in_debugger) {
1565 u32 chars;
1566
Lv Zheng4d946f72015-10-19 10:25:56 +08001567 kdb_read(buffer, buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 /* remove the CR kdb includes */
1570 chars = strlen(buffer) - 1;
1571 buffer[chars] = '\0';
1572 }
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001573#else
1574 int ret;
1575
Lv Zheng836d083012015-12-03 10:43:14 +08001576 ret = acpi_debugger_read_cmd(buffer, buffer_length);
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001577 if (ret < 0)
1578 return AE_ERROR;
1579 if (bytes_read)
1580 *bytes_read = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581#endif
1582
Lv Zheng4d946f72015-10-19 10:25:56 +08001583 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
Lv Zheng836d083012015-12-03 10:43:14 +08001585EXPORT_SYMBOL(acpi_os_get_line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001587acpi_status acpi_os_wait_command_ready(void)
1588{
1589 int ret;
1590
Lv Zheng836d083012015-12-03 10:43:14 +08001591 ret = acpi_debugger_wait_command_ready();
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001592 if (ret < 0)
1593 return AE_ERROR;
1594 return AE_OK;
1595}
1596
1597acpi_status acpi_os_notify_command_complete(void)
1598{
1599 int ret;
1600
Lv Zheng836d083012015-12-03 10:43:14 +08001601 ret = acpi_debugger_notify_command_complete();
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001602 if (ret < 0)
1603 return AE_ERROR;
1604 return AE_OK;
1605}
1606
Len Brown4be44fc2005-08-05 00:44:28 -04001607acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
Len Brown4be44fc2005-08-05 00:44:28 -04001609 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 case ACPI_SIGNAL_FATAL:
1611 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1612 break;
1613 case ACPI_SIGNAL_BREAKPOINT:
1614 /*
1615 * AML Breakpoint
1616 * ACPI spec. says to treat it as a NOP unless
1617 * you are debugging. So if/when we integrate
1618 * AML debugger into the kernel debugger its
1619 * hook will go here. But until then it is
1620 * not useful to print anything on breakpoints.
1621 */
1622 break;
1623 default:
1624 break;
1625 }
1626
1627 return AE_OK;
1628}
Len Brown4be44fc2005-08-05 00:44:28 -04001629
Len Brown4be44fc2005-08-05 00:44:28 -04001630static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
1632 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -04001633 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 if (!str || !*str)
1636 return 0;
1637
Dan Carpenter5e2be4e2013-10-18 12:01:43 +03001638 for (; count-- && *str; str++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (isalnum(*str) || *str == ' ' || *str == ':')
1640 *p++ = *str;
1641 else if (*str == '\'' || *str == '"')
1642 continue;
1643 else
1644 break;
1645 }
1646 *p = 0;
1647
1648 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
1652__setup("acpi_os_name=", acpi_os_name_setup);
1653
Bob Moore22b5afc2014-03-24 14:49:00 +08001654/*
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001655 * Disable the auto-serialization of named objects creation methods.
Bob Moore22b5afc2014-03-24 14:49:00 +08001656 *
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001657 * This feature is enabled by default. It marks the AML control methods
Bob Moore22b5afc2014-03-24 14:49:00 +08001658 * that contain the opcodes to create named objects as "Serialized".
1659 */
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001660static int __init acpi_no_auto_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001662 acpi_gbl_auto_serialize_methods = FALSE;
1663 pr_info("ACPI: auto-serialization disabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 return 1;
1666}
1667
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001668__setup("acpi_no_auto_serialize", acpi_no_auto_serialize_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Thomas Renningerdf92e692008-02-04 23:31:22 -08001670/* Check of resource interference between native drivers and ACPI
1671 * OperationRegions (SystemIO and System Memory only).
1672 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1673 * in arbitrary AML code and can interfere with legacy drivers.
1674 * acpi_enforce_resources= can be set to:
1675 *
Luca Tettamanti7e905602009-03-30 00:01:27 +02001676 * - strict (default) (2)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001677 * -> further driver trying to access the resources will not load
Luca Tettamanti7e905602009-03-30 00:01:27 +02001678 * - lax (1)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001679 * -> further driver trying to access the resources will load, but you
1680 * get a system message that something might go wrong...
1681 *
1682 * - no (0)
1683 * -> ACPI Operation Region resources will not be registered
1684 *
1685 */
1686#define ENFORCE_RESOURCES_STRICT 2
1687#define ENFORCE_RESOURCES_LAX 1
1688#define ENFORCE_RESOURCES_NO 0
1689
Luca Tettamanti7e905602009-03-30 00:01:27 +02001690static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001691
1692static int __init acpi_enforce_resources_setup(char *str)
1693{
1694 if (str == NULL || *str == '\0')
1695 return 0;
1696
1697 if (!strcmp("strict", str))
1698 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1699 else if (!strcmp("lax", str))
1700 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1701 else if (!strcmp("no", str))
1702 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1703
1704 return 1;
1705}
1706
1707__setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1708
1709/* Check for resource conflicts between ACPI OperationRegions and native
1710 * drivers */
Jean Delvare876fba42009-11-11 15:22:15 +01001711int acpi_check_resource_conflict(const struct resource *res)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001712{
Lin Mingf654c0f2012-01-12 13:10:32 +08001713 acpi_adr_space_type space_id;
1714 acpi_size length;
1715 u8 warn = 0;
1716 int clash = 0;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001717
1718 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1719 return 0;
1720 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1721 return 0;
1722
Lin Mingf654c0f2012-01-12 13:10:32 +08001723 if (res->flags & IORESOURCE_IO)
1724 space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1725 else
1726 space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001727
Alexandru Gheorghiue4f52242013-03-12 08:53:02 +00001728 length = resource_size(res);
Lin Mingf654c0f2012-01-12 13:10:32 +08001729 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO)
1730 warn = 1;
1731 clash = acpi_check_address_range(space_id, res->start, length, warn);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001732
1733 if (clash) {
1734 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
Jean Delvare14f03342009-09-08 15:31:46 +02001735 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1736 printk(KERN_NOTICE "ACPI: This conflict may"
1737 " cause random problems and system"
1738 " instability\n");
1739 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1740 " for this device, you should use it instead of"
1741 " the native driver\n");
Thomas Renningerdf92e692008-02-04 23:31:22 -08001742 }
1743 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1744 return -EBUSY;
1745 }
1746 return 0;
1747}
Thomas Renninger443dea72008-02-04 23:31:23 -08001748EXPORT_SYMBOL(acpi_check_resource_conflict);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001749
1750int acpi_check_region(resource_size_t start, resource_size_t n,
1751 const char *name)
1752{
1753 struct resource res = {
1754 .start = start,
1755 .end = start + n - 1,
1756 .name = name,
1757 .flags = IORESOURCE_IO,
1758 };
1759
1760 return acpi_check_resource_conflict(&res);
1761}
1762EXPORT_SYMBOL(acpi_check_region);
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764/*
Jean Delvare70dd6be2010-05-27 19:58:37 +02001765 * Let drivers know whether the resource checks are effective
1766 */
1767int acpi_resources_are_enforced(void)
1768{
1769 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1770}
1771EXPORT_SYMBOL(acpi_resources_are_enforced);
1772
1773/*
Lin Ming9f63b882011-03-23 17:26:34 +08001774 * Deallocate the memory for a spinlock.
1775 */
1776void acpi_os_delete_lock(acpi_spinlock handle)
1777{
1778 ACPI_FREE(handle);
1779}
1780
1781/*
Robert Moore73459f72005-06-24 00:00:00 -04001782 * Acquire a spinlock.
1783 *
1784 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001785 */
1786
Bob Moore967440e32006-06-23 17:04:00 -04001787acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001788{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001789 acpi_cpu_flags flags;
Bob Moore967440e32006-06-23 17:04:00 -04001790 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001791 return flags;
1792}
1793
1794/*
1795 * Release a spinlock. See above.
1796 */
1797
Bob Moore967440e32006-06-23 17:04:00 -04001798void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001799{
Bob Moore967440e32006-06-23 17:04:00 -04001800 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001801}
1802
Robert Moore73459f72005-06-24 00:00:00 -04001803#ifndef ACPI_USE_LOCAL_CACHE
1804
1805/*******************************************************************************
1806 *
1807 * FUNCTION: acpi_os_create_cache
1808 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001809 * PARAMETERS: name - Ascii name for the cache
1810 * size - Size of each cached object
1811 * depth - Maximum depth of the cache (in objects) <ignored>
1812 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001813 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001814 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001815 *
1816 * DESCRIPTION: Create a cache object
1817 *
1818 ******************************************************************************/
1819
1820acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001821acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001822{
Paul Mundt20c2df82007-07-20 10:11:58 +09001823 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001824 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001825 return AE_ERROR;
1826 else
1827 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001828}
1829
1830/*******************************************************************************
1831 *
1832 * FUNCTION: acpi_os_purge_cache
1833 *
1834 * PARAMETERS: Cache - Handle to cache object
1835 *
1836 * RETURN: Status
1837 *
1838 * DESCRIPTION: Free all objects within the requested cache.
1839 *
1840 ******************************************************************************/
1841
Len Brown4be44fc2005-08-05 00:44:28 -04001842acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001843{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001844 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001845 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001846}
1847
1848/*******************************************************************************
1849 *
1850 * FUNCTION: acpi_os_delete_cache
1851 *
1852 * PARAMETERS: Cache - Handle to cache object
1853 *
1854 * RETURN: Status
1855 *
1856 * DESCRIPTION: Free all objects within the requested cache and delete the
1857 * cache object.
1858 *
1859 ******************************************************************************/
1860
Len Brown4be44fc2005-08-05 00:44:28 -04001861acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001862{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001863 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001864 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001865}
1866
1867/*******************************************************************************
1868 *
1869 * FUNCTION: acpi_os_release_object
1870 *
1871 * PARAMETERS: Cache - Handle to cache object
1872 * Object - The object to be released
1873 *
1874 * RETURN: None
1875 *
1876 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1877 * the object is deleted.
1878 *
1879 ******************************************************************************/
1880
Len Brown4be44fc2005-08-05 00:44:28 -04001881acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001882{
Len Brown4be44fc2005-08-05 00:44:28 -04001883 kmem_cache_free(cache, object);
1884 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001885}
Robert Moore73459f72005-06-24 00:00:00 -04001886#endif
Myron Stowed362eda2010-10-21 14:24:04 -06001887
Lv Zhenga94e88c2014-04-04 12:39:11 +08001888static int __init acpi_no_static_ssdt_setup(char *s)
Lv Zhengb75dd292013-06-08 00:58:48 +00001889{
Lv Zhenga94e88c2014-04-04 12:39:11 +08001890 acpi_gbl_disable_ssdt_table_install = TRUE;
1891 pr_info("ACPI: static SSDT installation disabled\n");
Lv Zhengb75dd292013-06-08 00:58:48 +00001892
Lv Zhenga94e88c2014-04-04 12:39:11 +08001893 return 0;
Lv Zhengb75dd292013-06-08 00:58:48 +00001894}
1895
Lv Zhenga94e88c2014-04-04 12:39:11 +08001896early_param("acpi_no_static_ssdt", acpi_no_static_ssdt_setup);
Lv Zhengb75dd292013-06-08 00:58:48 +00001897
Lv Zheng4dde5072014-02-11 11:01:52 +08001898static int __init acpi_disable_return_repair(char *s)
1899{
1900 printk(KERN_NOTICE PREFIX
1901 "ACPI: Predefined validation mechanism disabled\n");
1902 acpi_gbl_disable_auto_repair = TRUE;
1903
1904 return 1;
1905}
1906
1907__setup("acpica_no_return_repair", acpi_disable_return_repair);
1908
Myron Stowed362eda2010-10-21 14:24:04 -06001909acpi_status __init acpi_os_initialize(void)
1910{
1911 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1912 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1913 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1914 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
Randy Wrighta4714a82014-06-04 08:55:59 -07001915 if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) {
1916 /*
1917 * Use acpi_os_map_generic_address to pre-map the reset
1918 * register if it's in system memory.
1919 */
1920 int rv;
1921
1922 rv = acpi_os_map_generic_address(&acpi_gbl_FADT.reset_register);
1923 pr_debug(PREFIX "%s: map reset_reg status %d\n", __func__, rv);
1924 }
Lv Zheng7901a052015-08-05 16:23:51 +08001925 acpi_os_initialized = true;
Myron Stowed362eda2010-10-21 14:24:04 -06001926
1927 return AE_OK;
1928}
1929
Zhang Rui32d47ee2010-12-08 10:40:36 +08001930acpi_status __init acpi_os_initialize1(void)
Myron Stowed362eda2010-10-21 14:24:04 -06001931{
Tejun Heo44d25882011-02-01 11:42:42 +01001932 kacpid_wq = alloc_workqueue("kacpid", 0, 1);
1933 kacpi_notify_wq = alloc_workqueue("kacpi_notify", 0, 1);
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001934 kacpi_hotplug_wq = alloc_ordered_workqueue("kacpi_hotplug", 0);
Myron Stowed362eda2010-10-21 14:24:04 -06001935 BUG_ON(!kacpid_wq);
1936 BUG_ON(!kacpi_notify_wq);
1937 BUG_ON(!kacpi_hotplug_wq);
Lv Zhenge5f660e2016-05-03 16:49:01 +08001938 acpi_osi_init();
Myron Stowed362eda2010-10-21 14:24:04 -06001939 return AE_OK;
1940}
1941
1942acpi_status acpi_os_terminate(void)
1943{
1944 if (acpi_irq_handler) {
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +01001945 acpi_os_remove_interrupt_handler(acpi_gbl_FADT.sci_interrupt,
Myron Stowed362eda2010-10-21 14:24:04 -06001946 acpi_irq_handler);
1947 }
1948
1949 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1950 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1951 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1952 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
Randy Wrighta4714a82014-06-04 08:55:59 -07001953 if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER)
1954 acpi_os_unmap_generic_address(&acpi_gbl_FADT.reset_register);
Myron Stowed362eda2010-10-21 14:24:04 -06001955
1956 destroy_workqueue(kacpid_wq);
1957 destroy_workqueue(kacpi_notify_wq);
1958 destroy_workqueue(kacpi_hotplug_wq);
1959
1960 return AE_OK;
1961}
Tang Liang09f98a82011-12-09 10:05:54 +08001962
1963acpi_status acpi_os_prepare_sleep(u8 sleep_state, u32 pm1a_control,
1964 u32 pm1b_control)
1965{
1966 int rc = 0;
1967 if (__acpi_os_prepare_sleep)
1968 rc = __acpi_os_prepare_sleep(sleep_state,
1969 pm1a_control, pm1b_control);
1970 if (rc < 0)
1971 return AE_ERROR;
1972 else if (rc > 0)
1973 return AE_CTRL_SKIP;
1974
1975 return AE_OK;
1976}
1977
1978void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
1979 u32 pm1a_ctrl, u32 pm1b_ctrl))
1980{
1981 __acpi_os_prepare_sleep = func;
1982}
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001983
Ben Guthrod6b47b12013-07-30 08:24:52 -04001984acpi_status acpi_os_prepare_extended_sleep(u8 sleep_state, u32 val_a,
1985 u32 val_b)
1986{
1987 int rc = 0;
1988 if (__acpi_os_prepare_extended_sleep)
1989 rc = __acpi_os_prepare_extended_sleep(sleep_state,
1990 val_a, val_b);
1991 if (rc < 0)
1992 return AE_ERROR;
1993 else if (rc > 0)
1994 return AE_CTRL_SKIP;
1995
1996 return AE_OK;
1997}
1998
1999void acpi_os_set_prepare_extended_sleep(int (*func)(u8 sleep_state,
2000 u32 val_a, u32 val_b))
2001{
2002 __acpi_os_prepare_extended_sleep = func;
2003}