blob: d0ecf4efd79da44a0c8c6e7492eb6b77362a0496 [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
Lin Mingb0ed7a92010-08-06 09:35:51 +080099static void __init acpi_osi_setup_late(void);
Len Brownae00d812007-05-29 18:43:33 -0400100
Len Brownd4b7dc42008-01-23 20:50:56 -0500101/*
Len Browna6e08872008-11-08 01:21:10 -0500102 * The story of _OSI(Linux)
Len Brownd4b7dc42008-01-23 20:50:56 -0500103 *
Len Browna6e08872008-11-08 01:21:10 -0500104 * From pre-history through Linux-2.6.22,
105 * Linux responded TRUE upon a BIOS OSI(Linux) query.
Len Brownd4b7dc42008-01-23 20:50:56 -0500106 *
Len Browna6e08872008-11-08 01:21:10 -0500107 * Unfortunately, reference BIOS writers got wind of this
108 * and put OSI(Linux) in their example code, quickly exposing
109 * this string as ill-conceived and opening the door to
110 * an un-bounded number of BIOS incompatibilities.
Len Brownd4b7dc42008-01-23 20:50:56 -0500111 *
Len Browna6e08872008-11-08 01:21:10 -0500112 * For example, OSI(Linux) was used on resume to re-POST a
113 * video card on one system, because Linux at that time
114 * could not do a speedy restore in its native driver.
115 * But then upon gaining quick native restore capability,
116 * Linux has no way to tell the BIOS to skip the time-consuming
117 * POST -- putting Linux at a permanent performance disadvantage.
118 * On another system, the BIOS writer used OSI(Linux)
119 * to infer native OS support for IPMI! On other systems,
120 * OSI(Linux) simply got in the way of Linux claiming to
121 * be compatible with other operating systems, exposing
122 * BIOS issues such as skipped device initialization.
Len Brownd4b7dc42008-01-23 20:50:56 -0500123 *
Len Browna6e08872008-11-08 01:21:10 -0500124 * So "Linux" turned out to be a really poor chose of
125 * OSI string, and from Linux-2.6.23 onward we respond FALSE.
Len Brownd4b7dc42008-01-23 20:50:56 -0500126 *
127 * BIOS writers should NOT query _OSI(Linux) on future systems.
Len Browna6e08872008-11-08 01:21:10 -0500128 * Linux will complain on the console when it sees it, and return FALSE.
129 * To get Linux to return TRUE for your system will require
130 * a kernel source update to add a DMI entry,
131 * or boot with "acpi_osi=Linux"
Len Brownd4b7dc42008-01-23 20:50:56 -0500132 */
Len Brownd4b7dc42008-01-23 20:50:56 -0500133
Adrian Bunk1d15d842008-01-29 00:10:15 +0200134static struct osi_linux {
Len Brownd4b7dc42008-01-23 20:50:56 -0500135 unsigned int enable:1;
136 unsigned int dmi:1;
137 unsigned int cmdline:1;
Lv Zheng5dc17982013-07-22 16:08:25 +0800138 unsigned int default_disabling:1;
139} osi_linux = {0, 0, 0, 0};
Len Brownf5076542007-05-30 00:10:38 -0400140
Lin Mingb0ed7a92010-08-06 09:35:51 +0800141static u32 acpi_osi_handler(acpi_string interface, u32 supported)
142{
143 if (!strcmp("Linux", interface)) {
144
Len Brown89976212011-08-02 00:45:48 -0400145 printk_once(KERN_NOTICE FW_BUG PREFIX
Lin Mingb0ed7a92010-08-06 09:35:51 +0800146 "BIOS _OSI(Linux) query %s%s\n",
147 osi_linux.enable ? "honored" : "ignored",
148 osi_linux.cmdline ? " via cmdline" :
149 osi_linux.dmi ? " via DMI" : "");
150 }
151
Matthew Garrett7bc5a2b2014-09-20 13:19:47 +0200152 if (!strcmp("Darwin", interface)) {
153 /*
154 * Apple firmware will behave poorly if it receives positive
155 * answers to "Darwin" and any other OS. Respond positively
156 * to Darwin and then disable all other vendor strings.
157 */
158 acpi_update_interfaces(ACPI_DISABLE_ALL_VENDOR_STRINGS);
159 supported = ACPI_UINT32_MAX;
160 }
161
Lin Mingb0ed7a92010-08-06 09:35:51 +0800162 return supported;
163}
164
Myron Stowebc9ffce2011-11-07 16:23:27 -0700165static void __init acpi_request_region (struct acpi_generic_address *gas,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700166 unsigned int length, char *desc)
167{
Myron Stowebc9ffce2011-11-07 16:23:27 -0700168 u64 addr;
169
170 /* Handle possible alignment issues */
171 memcpy(&addr, &gas->address, sizeof(addr));
172 if (!addr || !length)
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700173 return;
174
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200175 /* Resources are never freed */
176 if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
177 request_region(addr, length, desc);
178 else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
179 request_mem_region(addr, length, desc);
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700180}
181
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200182static int __init acpi_reserve_resources(void)
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700183{
Len Browneee3c852007-02-03 01:38:16 -0500184 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700185 "ACPI PM1a_EVT_BLK");
186
Len Browneee3c852007-02-03 01:38:16 -0500187 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700188 "ACPI PM1b_EVT_BLK");
189
Len Browneee3c852007-02-03 01:38:16 -0500190 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700191 "ACPI PM1a_CNT_BLK");
192
Len Browneee3c852007-02-03 01:38:16 -0500193 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700194 "ACPI PM1b_CNT_BLK");
195
Len Browneee3c852007-02-03 01:38:16 -0500196 if (acpi_gbl_FADT.pm_timer_length == 4)
197 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700198
Len Browneee3c852007-02-03 01:38:16 -0500199 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700200 "ACPI PM2_CNT_BLK");
201
202 /* Length of GPE blocks must be a non-negative multiple of 2 */
203
Len Browneee3c852007-02-03 01:38:16 -0500204 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
205 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
206 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700207
Len Browneee3c852007-02-03 01:38:16 -0500208 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
209 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
210 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200211
212 return 0;
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700213}
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200214fs_initcall_sync(acpi_reserve_resources);
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700215
Len Brown4be44fc2005-08-05 00:44:28 -0400216void acpi_os_printf(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 va_list args;
219 va_start(args, fmt);
220 acpi_os_vprintf(fmt, args);
221 va_end(args);
222}
Len Brown4be44fc2005-08-05 00:44:28 -0400223
Len Brown4be44fc2005-08-05 00:44:28 -0400224void acpi_os_vprintf(const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 static char buffer[512];
Len Brown4be44fc2005-08-05 00:44:28 -0400227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 vsprintf(buffer, fmt, args);
229
230#ifdef ENABLE_DEBUGGER
231 if (acpi_in_debugger) {
232 kdb_printf("%s", buffer);
233 } else {
Frank Seidel4d939152009-02-04 17:03:07 +0100234 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236#else
Frank Seidel4d939152009-02-04 17:03:07 +0100237 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#endif
239}
240
Takao Indoh4996c022011-07-14 18:05:21 -0400241#ifdef CONFIG_KEXEC
242static unsigned long acpi_rsdp;
243static int __init setup_acpi_rsdp(char *arg)
244{
Christoph Jaeger3d915892014-06-13 21:49:58 +0200245 if (kstrtoul(arg, 16, &acpi_rsdp))
246 return -EINVAL;
Takao Indoh4996c022011-07-14 18:05:21 -0400247 return 0;
248}
249early_param("acpi_rsdp", setup_acpi_rsdp);
250#endif
251
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300252acpi_physical_address __init acpi_os_get_root_pointer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Takao Indoh4996c022011-07-14 18:05:21 -0400254#ifdef CONFIG_KEXEC
255 if (acpi_rsdp)
256 return acpi_rsdp;
257#endif
258
Matt Fleming83e68182012-11-14 09:42:35 +0000259 if (efi_enabled(EFI_CONFIG_TABLES)) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800260 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300261 return efi.acpi20;
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800262 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300263 return efi.acpi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400265 printk(KERN_ERR PREFIX
266 "System description tables not found\n");
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
Graeme Gregory8a1664b2014-07-18 18:02:52 +0800269 } else if (IS_ENABLED(CONFIG_ACPI_LEGACY_TABLES_LOOKUP)) {
Len Brown239665a2007-11-23 20:08:02 -0500270 acpi_physical_address pa = 0;
271
272 acpi_find_root_pointer(&pa);
273 return pa;
274 }
Graeme Gregory8a1664b2014-07-18 18:02:52 +0800275
276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600279/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe4a3cba52010-10-21 14:24:14 -0600280static struct acpi_ioremap *
281acpi_map_lookup(acpi_physical_address phys, acpi_size size)
Myron Stowe620242a2010-10-21 14:23:53 -0600282{
283 struct acpi_ioremap *map;
284
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600285 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe620242a2010-10-21 14:23:53 -0600286 if (map->phys <= phys &&
287 phys + size <= map->phys + map->size)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600288 return map;
289
290 return NULL;
291}
292
293/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
294static void __iomem *
295acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
296{
297 struct acpi_ioremap *map;
298
299 map = acpi_map_lookup(phys, size);
300 if (map)
301 return map->virt + (phys - map->phys);
Myron Stowe620242a2010-10-21 14:23:53 -0600302
303 return NULL;
304}
305
Rafael J. Wysocki13606a22011-02-08 23:38:25 +0100306void __iomem *acpi_os_get_iomem(acpi_physical_address phys, unsigned int size)
307{
308 struct acpi_ioremap *map;
309 void __iomem *virt = NULL;
310
311 mutex_lock(&acpi_ioremap_lock);
312 map = acpi_map_lookup(phys, size);
313 if (map) {
314 virt = map->virt + (phys - map->phys);
315 map->refcount++;
316 }
317 mutex_unlock(&acpi_ioremap_lock);
318 return virt;
319}
320EXPORT_SYMBOL_GPL(acpi_os_get_iomem);
321
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600322/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe620242a2010-10-21 14:23:53 -0600323static struct acpi_ioremap *
324acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
325{
326 struct acpi_ioremap *map;
327
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600328 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600329 if (map->virt <= virt &&
330 virt + size <= map->virt + map->size)
Myron Stowe620242a2010-10-21 14:23:53 -0600331 return map;
332
333 return NULL;
334}
335
Graeme Gregoryaafc65c2015-03-24 14:02:35 +0000336#if defined(CONFIG_IA64) || defined(CONFIG_ARM64)
Myron Stoweba242d52012-01-20 19:13:30 -0700337/* ioremap will take care of cache attributes */
338#define should_use_kmap(pfn) 0
Graeme Gregoryaafc65c2015-03-24 14:02:35 +0000339#else
340#define should_use_kmap(pfn) page_is_ram(pfn)
Myron Stoweba242d52012-01-20 19:13:30 -0700341#endif
342
343static void __iomem *acpi_map(acpi_physical_address pg_off, unsigned long pg_sz)
344{
345 unsigned long pfn;
346
347 pfn = pg_off >> PAGE_SHIFT;
348 if (should_use_kmap(pfn)) {
349 if (pg_sz > PAGE_SIZE)
350 return NULL;
351 return (void __iomem __force *)kmap(pfn_to_page(pfn));
352 } else
353 return acpi_os_ioremap(pg_off, pg_sz);
354}
355
356static void acpi_unmap(acpi_physical_address pg_off, void __iomem *vaddr)
357{
358 unsigned long pfn;
359
360 pfn = pg_off >> PAGE_SHIFT;
Jan Beuliche2526752012-02-24 11:41:53 +0000361 if (should_use_kmap(pfn))
Myron Stoweba242d52012-01-20 19:13:30 -0700362 kunmap(pfn_to_page(pfn));
363 else
364 iounmap(vaddr);
365}
366
Rafael J. Wysocki9d128ed2016-01-02 03:10:29 +0100367/**
368 * acpi_os_map_iomem - Get a virtual address for a given physical address range.
369 * @phys: Start of the physical address range to map.
370 * @size: Size of the physical address range to map.
371 *
372 * Look up the given physical address range in the list of existing ACPI memory
373 * mappings. If found, get a reference to it and return a pointer to it (its
374 * virtual address). If not found, map it, add it to that list and return a
375 * pointer to it.
376 *
377 * During early init (when acpi_gbl_permanent_mmap has not been set yet) this
378 * routine simply calls __acpi_map_table() to get the job done.
379 */
Jan Beulich2fdf0742007-12-13 08:33:59 +0000380void __iomem *__init_refok
Lv Zhenga2383172014-05-20 15:39:41 +0800381acpi_os_map_iomem(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100383 struct acpi_ioremap *map;
Myron Stowe620242a2010-10-21 14:23:53 -0600384 void __iomem *virt;
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100385 acpi_physical_address pg_off;
386 acpi_size pg_sz;
Myron Stowe620242a2010-10-21 14:23:53 -0600387
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800388 if (phys > ULONG_MAX) {
389 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
Randy Dunlap70c08462007-02-13 16:11:36 -0800390 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Myron Stowe620242a2010-10-21 14:23:53 -0600392
393 if (!acpi_gbl_permanent_mmap)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300394 return __acpi_map_table((unsigned long)phys, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600395
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100396 mutex_lock(&acpi_ioremap_lock);
397 /* Check if there's a suitable mapping already. */
398 map = acpi_map_lookup(phys, size);
399 if (map) {
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100400 map->refcount++;
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100401 goto out;
402 }
403
Myron Stowe620242a2010-10-21 14:23:53 -0600404 map = kzalloc(sizeof(*map), GFP_KERNEL);
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100405 if (!map) {
406 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600407 return NULL;
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100408 }
Myron Stowe620242a2010-10-21 14:23:53 -0600409
Myron Stowe4a3cba52010-10-21 14:24:14 -0600410 pg_off = round_down(phys, PAGE_SIZE);
411 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
Myron Stoweba242d52012-01-20 19:13:30 -0700412 virt = acpi_map(pg_off, pg_sz);
Myron Stowe620242a2010-10-21 14:23:53 -0600413 if (!virt) {
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100414 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600415 kfree(map);
416 return NULL;
417 }
418
419 INIT_LIST_HEAD(&map->list);
420 map->virt = virt;
Myron Stowe4a3cba52010-10-21 14:24:14 -0600421 map->phys = pg_off;
422 map->size = pg_sz;
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100423 map->refcount = 1;
Myron Stowe620242a2010-10-21 14:23:53 -0600424
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600425 list_add_tail_rcu(&map->list, &acpi_ioremaps);
Myron Stowe620242a2010-10-21 14:23:53 -0600426
Lv Zhenga2383172014-05-20 15:39:41 +0800427out:
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100428 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600429 return map->virt + (phys - map->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
Lv Zhenga2383172014-05-20 15:39:41 +0800431EXPORT_SYMBOL_GPL(acpi_os_map_iomem);
432
433void *__init_refok
434acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
435{
436 return (void *)acpi_os_map_iomem(phys, size);
437}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800438EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100440static void acpi_os_drop_map_ref(struct acpi_ioremap *map)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600441{
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100442 if (!--map->refcount)
443 list_del_rcu(&map->list);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600444}
Myron Stowe4a3cba52010-10-21 14:24:14 -0600445
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100446static void acpi_os_map_cleanup(struct acpi_ioremap *map)
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100447{
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100448 if (!map->refcount) {
Konstantin Khlebnikov74b51ee2014-11-09 13:53:37 +0400449 synchronize_rcu_expedited();
Myron Stoweba242d52012-01-20 19:13:30 -0700450 acpi_unmap(map->phys, map->virt);
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100451 kfree(map);
452 }
Myron Stowe4a3cba52010-10-21 14:24:14 -0600453}
454
Rafael J. Wysocki9d128ed2016-01-02 03:10:29 +0100455/**
456 * acpi_os_unmap_iomem - Drop a memory mapping reference.
457 * @virt: Start of the address range to drop a reference to.
458 * @size: Size of the address range to drop a reference to.
459 *
460 * Look up the given virtual address range in the list of existing ACPI memory
461 * mappings, drop a reference to it and unmap it if there are no more active
462 * references to it.
463 *
464 * During early init (when acpi_gbl_permanent_mmap has not been set yet) this
465 * routine simply calls __acpi_unmap_table() to get the job done. Since
466 * __acpi_unmap_table() is an __init function, the __ref annotation is needed
467 * here.
468 */
Lv Zhenga2383172014-05-20 15:39:41 +0800469void __ref acpi_os_unmap_iomem(void __iomem *virt, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Myron Stowe620242a2010-10-21 14:23:53 -0600471 struct acpi_ioremap *map;
Myron Stowe620242a2010-10-21 14:23:53 -0600472
473 if (!acpi_gbl_permanent_mmap) {
Yinghai Lu7d972772009-02-07 15:39:41 -0800474 __acpi_unmap_table(virt, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600475 return;
476 }
477
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100478 mutex_lock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600479 map = acpi_map_lookup_virt(virt, size);
480 if (!map) {
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100481 mutex_unlock(&acpi_ioremap_lock);
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100482 WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
Myron Stowe620242a2010-10-21 14:23:53 -0600483 return;
484 }
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100485 acpi_os_drop_map_ref(map);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100486 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600487
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100488 acpi_os_map_cleanup(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
Lv Zhenga2383172014-05-20 15:39:41 +0800490EXPORT_SYMBOL_GPL(acpi_os_unmap_iomem);
491
492void __ref acpi_os_unmap_memory(void *virt, acpi_size size)
493{
494 return acpi_os_unmap_iomem((void __iomem *)virt, size);
495}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800496EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Jeremy Fitzhardinge0d3a9cf2009-02-22 14:58:56 -0800498void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
Yinghai Lu7d972772009-02-07 15:39:41 -0800499{
500 if (!acpi_gbl_permanent_mmap)
501 __acpi_unmap_table(virt, size);
502}
503
Myron Stowe6f68c912011-11-07 16:23:34 -0700504int acpi_os_map_generic_address(struct acpi_generic_address *gas)
Myron Stowe29718522010-10-21 14:23:59 -0600505{
Myron Stowebc9ffce2011-11-07 16:23:27 -0700506 u64 addr;
Myron Stowe29718522010-10-21 14:23:59 -0600507 void __iomem *virt;
508
Myron Stowebc9ffce2011-11-07 16:23:27 -0700509 if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
Myron Stowe29718522010-10-21 14:23:59 -0600510 return 0;
511
Myron Stowebc9ffce2011-11-07 16:23:27 -0700512 /* Handle possible alignment issues */
513 memcpy(&addr, &gas->address, sizeof(addr));
514 if (!addr || !gas->bit_width)
Myron Stowe29718522010-10-21 14:23:59 -0600515 return -EINVAL;
516
Lv Zhenga2383172014-05-20 15:39:41 +0800517 virt = acpi_os_map_iomem(addr, gas->bit_width / 8);
Myron Stowe29718522010-10-21 14:23:59 -0600518 if (!virt)
519 return -EIO;
520
521 return 0;
522}
Myron Stowe6f68c912011-11-07 16:23:34 -0700523EXPORT_SYMBOL(acpi_os_map_generic_address);
Myron Stowe29718522010-10-21 14:23:59 -0600524
Myron Stowe6f68c912011-11-07 16:23:34 -0700525void acpi_os_unmap_generic_address(struct acpi_generic_address *gas)
Myron Stowe29718522010-10-21 14:23:59 -0600526{
Myron Stowebc9ffce2011-11-07 16:23:27 -0700527 u64 addr;
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100528 struct acpi_ioremap *map;
Myron Stowe29718522010-10-21 14:23:59 -0600529
Myron Stowebc9ffce2011-11-07 16:23:27 -0700530 if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
Myron Stowe29718522010-10-21 14:23:59 -0600531 return;
532
Myron Stowebc9ffce2011-11-07 16:23:27 -0700533 /* Handle possible alignment issues */
534 memcpy(&addr, &gas->address, sizeof(addr));
535 if (!addr || !gas->bit_width)
Myron Stowe29718522010-10-21 14:23:59 -0600536 return;
537
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100538 mutex_lock(&acpi_ioremap_lock);
Myron Stowebc9ffce2011-11-07 16:23:27 -0700539 map = acpi_map_lookup(addr, gas->bit_width / 8);
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100540 if (!map) {
541 mutex_unlock(&acpi_ioremap_lock);
542 return;
543 }
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100544 acpi_os_drop_map_ref(map);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100545 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe29718522010-10-21 14:23:59 -0600546
Rafael J. Wysockib7c1fad2011-02-08 23:38:15 +0100547 acpi_os_map_cleanup(map);
Myron Stowe29718522010-10-21 14:23:59 -0600548}
Myron Stowe6f68c912011-11-07 16:23:34 -0700549EXPORT_SYMBOL(acpi_os_unmap_generic_address);
Myron Stowe29718522010-10-21 14:23:59 -0600550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#ifdef ACPI_FUTURE_USAGE
552acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400553acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Len Brown4be44fc2005-08-05 00:44:28 -0400555 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return AE_BAD_PARAMETER;
557
558 *phys = virt_to_phys(virt);
559
560 return AE_OK;
561}
562#endif
563
Rafael J. Wysocki18d78b62015-07-03 01:06:00 +0200564#ifdef CONFIG_ACPI_REV_OVERRIDE_POSSIBLE
565static bool acpi_rev_override;
566
567int __init acpi_rev_override_setup(char *str)
568{
569 acpi_rev_override = true;
570 return 1;
571}
572__setup("acpi_rev_override", acpi_rev_override_setup);
573#else
574#define acpi_rev_override false
575#endif
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577#define ACPI_MAX_OVERRIDE_LEN 100
578
579static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
580
581acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400582acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
Dominik Brodowski2bad7e22015-05-14 15:31:25 +0200583 char **new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 if (!init_val || !new_val)
586 return AE_BAD_PARAMETER;
587
588 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400589 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400591 acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 *new_val = acpi_os_name;
593 }
594
Rafael J. Wysocki18d78b62015-07-03 01:06:00 +0200595 if (!memcmp(init_val->name, "_REV", 4) && acpi_rev_override) {
596 printk(KERN_INFO PREFIX "Overriding _REV return value to 5\n");
597 *new_val = (char *)5;
598 }
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return AE_OK;
601}
602
Thomas Renninger53aac442012-10-01 00:23:54 +0200603#ifdef CONFIG_ACPI_INITRD_TABLE_OVERRIDE
604#include <linux/earlycpio.h>
605#include <linux/memblock.h>
606
607static u64 acpi_tables_addr;
608static int all_tables_size;
609
610/* Copied from acpica/tbutils.c:acpi_tb_checksum() */
Rashika66e162b2013-12-17 14:43:05 +0530611static u8 __init acpi_table_checksum(u8 *buffer, u32 length)
Thomas Renninger53aac442012-10-01 00:23:54 +0200612{
613 u8 sum = 0;
614 u8 *end = buffer + length;
615
616 while (buffer < end)
617 sum = (u8) (sum + *(buffer++));
618 return sum;
619}
620
621/* All but ACPI_SIG_RSDP and ACPI_SIG_FACS: */
622static const char * const table_sigs[] = {
623 ACPI_SIG_BERT, ACPI_SIG_CPEP, ACPI_SIG_ECDT, ACPI_SIG_EINJ,
624 ACPI_SIG_ERST, ACPI_SIG_HEST, ACPI_SIG_MADT, ACPI_SIG_MSCT,
625 ACPI_SIG_SBST, ACPI_SIG_SLIT, ACPI_SIG_SRAT, ACPI_SIG_ASF,
626 ACPI_SIG_BOOT, ACPI_SIG_DBGP, ACPI_SIG_DMAR, ACPI_SIG_HPET,
627 ACPI_SIG_IBFT, ACPI_SIG_IVRS, ACPI_SIG_MCFG, ACPI_SIG_MCHI,
628 ACPI_SIG_SLIC, ACPI_SIG_SPCR, ACPI_SIG_SPMI, ACPI_SIG_TCPA,
629 ACPI_SIG_UEFI, ACPI_SIG_WAET, ACPI_SIG_WDAT, ACPI_SIG_WDDT,
630 ACPI_SIG_WDRT, ACPI_SIG_DSDT, ACPI_SIG_FADT, ACPI_SIG_PSDT,
631 ACPI_SIG_RSDT, ACPI_SIG_XSDT, ACPI_SIG_SSDT, NULL };
632
Thomas Renninger53aac442012-10-01 00:23:54 +0200633#define ACPI_HEADER_SIZE sizeof(struct acpi_table_header)
634
Yinghai Lubee7f9c2013-09-06 19:08:00 -0700635#define ACPI_OVERRIDE_TABLES 64
636static struct cpio_data __initdata acpi_initrd_files[ACPI_OVERRIDE_TABLES];
637
638#define MAP_CHUNK_SIZE (NR_FIX_BTMAPS << PAGE_SHIFT)
Thomas Renninger53aac442012-10-01 00:23:54 +0200639
640void __init acpi_initrd_override(void *data, size_t size)
641{
642 int sig, no, table_nr = 0, total_offset = 0;
643 long offset = 0;
644 struct acpi_table_header *table;
645 char cpio_path[32] = "kernel/firmware/acpi/";
646 struct cpio_data file;
Thomas Renninger53aac442012-10-01 00:23:54 +0200647
648 if (data == NULL || size == 0)
649 return;
650
651 for (no = 0; no < ACPI_OVERRIDE_TABLES; no++) {
652 file = find_cpio_data(cpio_path, data, size, &offset);
653 if (!file.data)
654 break;
655
656 data += offset;
657 size -= offset;
658
Tang Chen7702ae02013-08-14 17:37:08 +0800659 if (file.size < sizeof(struct acpi_table_header)) {
660 pr_err("ACPI OVERRIDE: Table smaller than ACPI header [%s%s]\n",
661 cpio_path, file.name);
662 continue;
663 }
Thomas Renninger53aac442012-10-01 00:23:54 +0200664
665 table = file.data;
666
667 for (sig = 0; table_sigs[sig]; sig++)
668 if (!memcmp(table->signature, table_sigs[sig], 4))
669 break;
670
Tang Chen7702ae02013-08-14 17:37:08 +0800671 if (!table_sigs[sig]) {
672 pr_err("ACPI OVERRIDE: Unknown signature [%s%s]\n",
673 cpio_path, file.name);
674 continue;
675 }
676 if (file.size != table->length) {
677 pr_err("ACPI OVERRIDE: File length does not match table length [%s%s]\n",
678 cpio_path, file.name);
679 continue;
680 }
681 if (acpi_table_checksum(file.data, table->length)) {
682 pr_err("ACPI OVERRIDE: Bad table checksum [%s%s]\n",
683 cpio_path, file.name);
684 continue;
685 }
Thomas Renninger53aac442012-10-01 00:23:54 +0200686
687 pr_info("%4.4s ACPI table found in initrd [%s%s][0x%x]\n",
688 table->signature, cpio_path, file.name, table->length);
689
690 all_tables_size += table->length;
Yinghai Lubee7f9c2013-09-06 19:08:00 -0700691 acpi_initrd_files[table_nr].data = file.data;
692 acpi_initrd_files[table_nr].size = file.size;
Thomas Renninger53aac442012-10-01 00:23:54 +0200693 table_nr++;
694 }
695 if (table_nr == 0)
696 return;
697
698 acpi_tables_addr =
699 memblock_find_in_range(0, max_low_pfn_mapped << PAGE_SHIFT,
700 all_tables_size, PAGE_SIZE);
701 if (!acpi_tables_addr) {
702 WARN_ON(1);
703 return;
704 }
705 /*
706 * Only calling e820_add_reserve does not work and the
707 * tables are invalid (memory got used) later.
708 * memblock_reserve works as expected and the tables won't get modified.
709 * But it's not enough on X86 because ioremap will
710 * complain later (used by acpi_os_map_memory) that the pages
711 * that should get mapped are not marked "reserved".
712 * Both memblock_reserve and e820_add_region (via arch_reserve_mem_area)
713 * works fine.
714 */
Wang YanQinga6432de2013-04-23 01:19:19 +0200715 memblock_reserve(acpi_tables_addr, all_tables_size);
Thomas Renninger53aac442012-10-01 00:23:54 +0200716 arch_reserve_mem_area(acpi_tables_addr, all_tables_size);
717
Yinghai Lubee7f9c2013-09-06 19:08:00 -0700718 /*
719 * early_ioremap only can remap 256k one time. If we map all
720 * tables one time, we will hit the limit. Need to map chunks
721 * one by one during copying the same as that in relocate_initrd().
722 */
Thomas Renninger53aac442012-10-01 00:23:54 +0200723 for (no = 0; no < table_nr; no++) {
Yinghai Lubee7f9c2013-09-06 19:08:00 -0700724 unsigned char *src_p = acpi_initrd_files[no].data;
725 phys_addr_t size = acpi_initrd_files[no].size;
726 phys_addr_t dest_addr = acpi_tables_addr + total_offset;
727 phys_addr_t slop, clen;
728 char *dest_p;
729
730 total_offset += size;
731
732 while (size) {
733 slop = dest_addr & ~PAGE_MASK;
734 clen = size;
735 if (clen > MAP_CHUNK_SIZE - slop)
736 clen = MAP_CHUNK_SIZE - slop;
737 dest_p = early_ioremap(dest_addr & PAGE_MASK,
738 clen + slop);
739 memcpy(dest_p + slop, src_p, clen);
740 early_iounmap(dest_p, clen + slop);
741 src_p += clen;
742 dest_addr += clen;
743 size -= clen;
744 }
Thomas Renninger53aac442012-10-01 00:23:54 +0200745 }
Thomas Renninger53aac442012-10-01 00:23:54 +0200746}
747#endif /* CONFIG_ACPI_INITRD_TABLE_OVERRIDE */
748
Thomas Renninger325a8d362012-10-01 00:23:56 +0200749static void acpi_table_taint(struct acpi_table_header *table)
750{
751 pr_warn(PREFIX
752 "Override [%4.4s-%8.8s], this is unsafe: tainting kernel\n",
753 table->signature, table->oem_table_id);
Rusty Russell373d4d02013-01-21 17:17:39 +1030754 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
Thomas Renninger325a8d362012-10-01 00:23:56 +0200755}
756
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400759acpi_os_table_override(struct acpi_table_header * existing_table,
760 struct acpi_table_header ** new_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 if (!existing_table || !new_table)
763 return AE_BAD_PARAMETER;
764
Markus Gaugusch71fc47a2008-02-05 00:04:06 +0100765 *new_table = NULL;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767#ifdef CONFIG_ACPI_CUSTOM_DSDT
768 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400769 *new_table = (struct acpi_table_header *)AmlCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770#endif
Thomas Renninger325a8d362012-10-01 00:23:56 +0200771 if (*new_table != NULL)
772 acpi_table_taint(existing_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return AE_OK;
774}
775
Bob Mooref7b004a2012-02-14 18:31:56 +0800776acpi_status
777acpi_os_physical_table_override(struct acpi_table_header *existing_table,
Thomas Renningerb2a35002012-10-01 00:23:55 +0200778 acpi_physical_address *address,
779 u32 *table_length)
Bob Mooref7b004a2012-02-14 18:31:56 +0800780{
Thomas Renningerb2a35002012-10-01 00:23:55 +0200781#ifndef CONFIG_ACPI_INITRD_TABLE_OVERRIDE
782 *table_length = 0;
783 *address = 0;
784 return AE_OK;
785#else
786 int table_offset = 0;
787 struct acpi_table_header *table;
Bob Mooref7b004a2012-02-14 18:31:56 +0800788
Thomas Renningerb2a35002012-10-01 00:23:55 +0200789 *table_length = 0;
790 *address = 0;
791
792 if (!acpi_tables_addr)
793 return AE_OK;
794
795 do {
796 if (table_offset + ACPI_HEADER_SIZE > all_tables_size) {
797 WARN_ON(1);
798 return AE_OK;
799 }
800
801 table = acpi_os_map_memory(acpi_tables_addr + table_offset,
802 ACPI_HEADER_SIZE);
803
804 if (table_offset + table->length > all_tables_size) {
805 acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
806 WARN_ON(1);
807 return AE_OK;
808 }
809
810 table_offset += table->length;
811
812 if (memcmp(existing_table->signature, table->signature, 4)) {
813 acpi_os_unmap_memory(table,
814 ACPI_HEADER_SIZE);
815 continue;
816 }
817
818 /* Only override tables with matching oem id */
819 if (memcmp(table->oem_table_id, existing_table->oem_table_id,
820 ACPI_OEM_TABLE_ID_SIZE)) {
821 acpi_os_unmap_memory(table,
822 ACPI_HEADER_SIZE);
823 continue;
824 }
825
826 table_offset -= table->length;
827 *table_length = table->length;
828 acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
829 *address = acpi_tables_addr + table_offset;
830 break;
831 } while (table_offset + ACPI_HEADER_SIZE < all_tables_size);
832
Thomas Renninger325a8d362012-10-01 00:23:56 +0200833 if (*address != 0)
834 acpi_table_taint(existing_table);
Thomas Renningerb2a35002012-10-01 00:23:55 +0200835 return AE_OK;
836#endif
837}
Bob Mooref7b004a2012-02-14 18:31:56 +0800838
David Howells7d12e782006-10-05 14:55:46 +0100839static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Len Brown5229e872008-02-06 01:26:55 -0500841 u32 handled;
842
843 handled = (*acpi_irq_handler) (acpi_irq_context);
844
845 if (handled) {
846 acpi_irq_handled++;
847 return IRQ_HANDLED;
Len Brown88bea182009-04-21 00:35:47 -0400848 } else {
849 acpi_irq_not_handled++;
Len Brown5229e872008-02-06 01:26:55 -0500850 return IRQ_NONE;
Len Brown88bea182009-04-21 00:35:47 -0400851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400855acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
856 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 unsigned int irq;
859
Len Brown5229e872008-02-06 01:26:55 -0500860 acpi_irq_stats_init();
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /*
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100863 * ACPI interrupts different from the SCI in our copy of the FADT are
864 * not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 */
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100866 if (gsi != acpi_gbl_FADT.sci_interrupt)
867 return AE_BAD_PARAMETER;
868
869 if (acpi_irq_handler)
870 return AE_ALREADY_ACQUIRED;
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
873 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
874 gsi);
875 return AE_OK;
876 }
877
878 acpi_irq_handler = handler;
879 acpi_irq_context = context;
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200880 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100882 acpi_irq_handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return AE_NOT_ACQUIRED;
884 }
Chen Yu49e4b8432015-10-25 01:02:19 +0800885 acpi_sci_irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 return AE_OK;
888}
889
Chen Yu49e4b8432015-10-25 01:02:19 +0800890acpi_status acpi_os_remove_interrupt_handler(u32 gsi, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Chen Yu49e4b8432015-10-25 01:02:19 +0800892 if (gsi != acpi_gbl_FADT.sci_interrupt || !acpi_sci_irq_valid())
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100893 return AE_BAD_PARAMETER;
894
Chen Yu49e4b8432015-10-25 01:02:19 +0800895 free_irq(acpi_sci_irq, acpi_irq);
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100896 acpi_irq_handler = NULL;
Chen Yu49e4b8432015-10-25 01:02:19 +0800897 acpi_sci_irq = INVALID_ACPI_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 return AE_OK;
900}
901
902/*
903 * Running in interpreter thread context, safe to sleep
904 */
905
Lin Ming439913f2010-01-28 10:53:19 +0800906void acpi_os_sleep(u64 ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Liu Chuansheng30282292013-09-12 01:42:57 +0800908 msleep(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
Len Brown4be44fc2005-08-05 00:44:28 -0400910
Len Brown4be44fc2005-08-05 00:44:28 -0400911void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
913 while (us) {
914 u32 delay = 1000;
915
916 if (delay > us)
917 delay = us;
918 udelay(delay);
919 touch_nmi_watchdog();
920 us -= delay;
921 }
922}
Len Brown4be44fc2005-08-05 00:44:28 -0400923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924/*
925 * Support ACPI 3.0 AML Timer operand
926 * Returns 64-bit free-running, monotonically increasing timer
927 * with 100ns granularity
928 */
Len Brown4be44fc2005-08-05 00:44:28 -0400929u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Mika Westerberg10619062013-05-23 10:27:46 +0300931 u64 time_ns = ktime_to_ns(ktime_get());
932 do_div(time_ns, 100);
933 return time_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Len Brown4be44fc2005-08-05 00:44:28 -0400936acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 u32 dummy;
939
940 if (!value)
941 value = &dummy;
942
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800943 *value = 0;
944 if (width <= 8) {
Len Brown4be44fc2005-08-05 00:44:28 -0400945 *(u8 *) value = inb(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800946 } else if (width <= 16) {
Len Brown4be44fc2005-08-05 00:44:28 -0400947 *(u16 *) value = inw(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800948 } else if (width <= 32) {
Len Brown4be44fc2005-08-05 00:44:28 -0400949 *(u32 *) value = inl(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800950 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 BUG();
952 }
953
954 return AE_OK;
955}
Len Brown4be44fc2005-08-05 00:44:28 -0400956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957EXPORT_SYMBOL(acpi_os_read_port);
958
Len Brown4be44fc2005-08-05 00:44:28 -0400959acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800961 if (width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 outb(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800963 } else if (width <= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 outw(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800965 } else if (width <= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 outl(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800967 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 BUG();
969 }
970
971 return AE_OK;
972}
Len Brown4be44fc2005-08-05 00:44:28 -0400973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974EXPORT_SYMBOL(acpi_os_write_port);
975
Myron Stowee615bf52012-01-20 19:13:24 -0700976acpi_status
Bob Moore653f4b52012-02-14 18:29:55 +0800977acpi_os_read_memory(acpi_physical_address phys_addr, u64 *value, u32 width)
Myron Stowee615bf52012-01-20 19:13:24 -0700978{
979 void __iomem *virt_addr;
980 unsigned int size = width / 8;
981 bool unmap = false;
982 u64 dummy;
983
984 rcu_read_lock();
985 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
986 if (!virt_addr) {
987 rcu_read_unlock();
988 virt_addr = acpi_os_ioremap(phys_addr, size);
989 if (!virt_addr)
990 return AE_BAD_ADDRESS;
991 unmap = true;
992 }
993
994 if (!value)
995 value = &dummy;
996
997 switch (width) {
998 case 8:
999 *(u8 *) value = readb(virt_addr);
1000 break;
1001 case 16:
1002 *(u16 *) value = readw(virt_addr);
1003 break;
1004 case 32:
1005 *(u32 *) value = readl(virt_addr);
1006 break;
1007 case 64:
Andy Shevchenko3277b4e2015-08-17 17:28:46 +03001008 *(u64 *) value = readq(virt_addr);
Myron Stowee615bf52012-01-20 19:13:24 -07001009 break;
1010 default:
1011 BUG();
1012 }
1013
1014 if (unmap)
1015 iounmap(virt_addr);
1016 else
1017 rcu_read_unlock();
1018
1019 return AE_OK;
1020}
1021
Myron Stowee615bf52012-01-20 19:13:24 -07001022acpi_status
Bob Moore653f4b52012-02-14 18:29:55 +08001023acpi_os_write_memory(acpi_physical_address phys_addr, u64 value, u32 width)
Myron Stowee615bf52012-01-20 19:13:24 -07001024{
1025 void __iomem *virt_addr;
1026 unsigned int size = width / 8;
1027 bool unmap = false;
1028
1029 rcu_read_lock();
1030 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
1031 if (!virt_addr) {
1032 rcu_read_unlock();
1033 virt_addr = acpi_os_ioremap(phys_addr, size);
1034 if (!virt_addr)
1035 return AE_BAD_ADDRESS;
1036 unmap = true;
1037 }
1038
1039 switch (width) {
1040 case 8:
1041 writeb(value, virt_addr);
1042 break;
1043 case 16:
1044 writew(value, virt_addr);
1045 break;
1046 case 32:
1047 writel(value, virt_addr);
1048 break;
1049 case 64:
Andy Shevchenko3277b4e2015-08-17 17:28:46 +03001050 writeq(value, virt_addr);
Myron Stowee615bf52012-01-20 19:13:24 -07001051 break;
1052 default:
1053 BUG();
1054 }
1055
1056 if (unmap)
1057 iounmap(virt_addr);
1058 else
1059 rcu_read_unlock();
1060
1061 return AE_OK;
1062}
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001065acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Bob Moorec5f02312010-08-06 08:57:53 +08001066 u64 *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 int result, size;
Bob Moorec5f02312010-08-06 08:57:53 +08001069 u32 value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 if (!value)
1072 return AE_BAD_PARAMETER;
1073
1074 switch (width) {
1075 case 8:
1076 size = 1;
1077 break;
1078 case 16:
1079 size = 2;
1080 break;
1081 case 32:
1082 size = 4;
1083 break;
1084 default:
1085 return AE_ERROR;
1086 }
1087
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -05001088 result = raw_pci_read(pci_id->segment, pci_id->bus,
1089 PCI_DEVFN(pci_id->device, pci_id->function),
Bob Moorec5f02312010-08-06 08:57:53 +08001090 reg, size, &value32);
1091 *value = value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 return (result ? AE_ERROR : AE_OK);
1094}
Len Brown4be44fc2005-08-05 00:44:28 -04001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001097acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Lin Ming439913f2010-01-28 10:53:19 +08001098 u64 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 int result, size;
1101
1102 switch (width) {
1103 case 8:
1104 size = 1;
1105 break;
1106 case 16:
1107 size = 2;
1108 break;
1109 case 32:
1110 size = 4;
1111 break;
1112 default:
1113 return AE_ERROR;
1114 }
1115
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -05001116 result = raw_pci_write(pci_id->segment, pci_id->bus,
1117 PCI_DEVFN(pci_id->device, pci_id->function),
1118 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 return (result ? AE_ERROR : AE_OK);
1121}
1122
David Howells65f27f32006-11-22 14:55:48 +00001123static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
David Howells65f27f32006-11-22 14:55:48 +00001125 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -04001126
Zhang Rui19cd8472008-08-28 10:05:06 +08001127 dpc->function(dpc->context);
1128 kfree(dpc);
Zhang Rui19cd8472008-08-28 10:05:06 +08001129}
1130
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -04001131/*******************************************************************************
1132 *
1133 * FUNCTION: acpi_os_execute
1134 *
1135 * PARAMETERS: Type - Type of the callback
1136 * Function - Function to be executed
1137 * Context - Function parameters
1138 *
1139 * RETURN: Status
1140 *
1141 * DESCRIPTION: Depending on type, either queues function for deferred execution or
1142 * immediately executes function on a separate thread.
1143 *
1144 ******************************************************************************/
1145
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001146acpi_status acpi_os_execute(acpi_execute_type type,
1147 acpi_osd_exec_callback function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Len Brown4be44fc2005-08-05 00:44:28 -04001149 acpi_status status = AE_OK;
1150 struct acpi_os_dpc *dpc;
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +03001151 struct workqueue_struct *queue;
Zhang Rui19cd8472008-08-28 10:05:06 +08001152 int ret;
Len Brown72945b22006-07-12 22:46:42 -04001153 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
1154 "Scheduling function [%p(%p)] for deferred execution.\n",
1155 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /*
1158 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +00001159 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 * way that allows us to also free its memory inside the callee.
1161 * Because we may want to schedule several tasks with different
1162 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +00001163 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 */
Len Brown72945b22006-07-12 22:46:42 -04001165
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001166 dpc = kzalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (!dpc)
Lin Ming889c78b2008-12-31 09:23:57 +08001168 return AE_NO_MEMORY;
Linus Torvaldsb976fe12006-11-17 19:31:09 -08001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 dpc->function = function;
1171 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -08001172
Zhang Ruic02256b2009-06-23 10:20:29 +08001173 /*
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001174 * To prevent lockdep from complaining unnecessarily, make sure that
1175 * there is a different static lockdep key for each workqueue by using
1176 * INIT_WORK() for each of them separately.
Zhang Ruic02256b2009-06-23 10:20:29 +08001177 */
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001178 if (type == OSL_NOTIFY_HANDLER) {
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001179 queue = kacpi_notify_wq;
Zhang Ruibc736752010-03-22 15:48:54 +08001180 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001181 } else {
1182 queue = kacpid_wq;
Zhang Ruibc736752010-03-22 15:48:54 +08001183 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001184 }
Zhang Ruibc736752010-03-22 15:48:54 +08001185
Tejun Heo8fec62b2010-06-29 10:07:09 +02001186 /*
1187 * On some machines, a software-initiated SMI causes corruption unless
1188 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
1189 * typically it's done in GPE-related methods that are run via
1190 * workqueues, so we can avoid the known corruption cases by always
1191 * queueing on CPU 0.
1192 */
1193 ret = queue_work_on(0, queue, &dpc->work);
Zhang Rui19cd8472008-08-28 10:05:06 +08001194
1195 if (!ret) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001196 printk(KERN_ERR PREFIX
1197 "Call to queue_work() failed.\n");
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +03001198 status = AE_ERROR;
1199 kfree(dpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
Lin Ming889c78b2008-12-31 09:23:57 +08001201 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -04001203EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Lin Mingbd6f10a2012-05-22 16:43:49 +08001205void acpi_os_wait_events_complete(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Lv Zheng90253a72014-11-05 15:06:13 +08001207 /*
1208 * Make sure the GPE handler or the fixed event handler is not used
1209 * on another CPU after removal.
1210 */
Chen Yuefb1cf72015-10-25 01:02:36 +08001211 if (acpi_sci_irq_valid())
1212 synchronize_hardirq(acpi_sci_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 flush_workqueue(kacpid_wq);
Zhang Rui2f67a062008-04-29 02:34:42 -04001214 flush_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
Len Brown4be44fc2005-08-05 00:44:28 -04001216
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001217struct acpi_hp_work {
1218 struct work_struct work;
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +01001219 struct acpi_device *adev;
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001220 u32 src;
1221};
1222
1223static void acpi_hotplug_work_fn(struct work_struct *work)
1224{
1225 struct acpi_hp_work *hpw = container_of(work, struct acpi_hp_work, work);
1226
1227 acpi_os_wait_events_complete();
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +01001228 acpi_device_hotplug(hpw->adev, hpw->src);
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001229 kfree(hpw);
1230}
1231
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +01001232acpi_status acpi_hotplug_schedule(struct acpi_device *adev, u32 src)
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001233{
1234 struct acpi_hp_work *hpw;
1235
1236 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +01001237 "Scheduling hotplug event (%p, %u) for deferred execution.\n",
1238 adev, src));
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001239
1240 hpw = kmalloc(sizeof(*hpw), GFP_KERNEL);
1241 if (!hpw)
1242 return AE_NO_MEMORY;
1243
1244 INIT_WORK(&hpw->work, acpi_hotplug_work_fn);
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +01001245 hpw->adev = adev;
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001246 hpw->src = src;
1247 /*
1248 * We can't run hotplug code in kacpid_wq/kacpid_notify_wq etc., because
1249 * the hotplug code may call driver .remove() functions, which may
1250 * invoke flush_scheduled_work()/acpi_os_wait_events_complete() to flush
1251 * these workqueues.
1252 */
1253 if (!queue_work(kacpi_hotplug_wq, &hpw->work)) {
1254 kfree(hpw);
1255 return AE_ERROR;
1256 }
1257 return AE_OK;
1258}
1259
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001260bool acpi_queue_hotplug_work(struct work_struct *work)
1261{
1262 return queue_work(kacpi_hotplug_wq, work);
1263}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001266acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Len Brown4be44fc2005-08-05 00:44:28 -04001268 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
jhbird.choi@samsung.com2d0acb42014-03-20 16:35:56 +09001270 sem = acpi_os_allocate_zeroed(sizeof(struct semaphore));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -04001272 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 sema_init(sem, initial_units);
1275
Len Brown4be44fc2005-08-05 00:44:28 -04001276 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Len Brown4be44fc2005-08-05 00:44:28 -04001278 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
1279 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Patrick Mocheld550d982006-06-27 00:41:40 -04001281 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284/*
1285 * TODO: A better way to delete semaphores? Linux doesn't have a
1286 * 'delete_semaphore()' function -- may result in an invalid
1287 * pointer dereference for non-synchronized consumers. Should
1288 * we at least check for blocked threads and signal/cancel them?
1289 */
1290
Len Brown4be44fc2005-08-05 00:44:28 -04001291acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Len Brown4be44fc2005-08-05 00:44:28 -04001293 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -04001296 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Len Brown4be44fc2005-08-05 00:44:28 -04001298 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001300 BUG_ON(!list_empty(&sem->wait_list));
Len Brown02438d82006-06-30 03:19:10 -04001301 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -04001302 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Patrick Mocheld550d982006-06-27 00:41:40 -04001304 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 * TODO: Support for units > 1?
1309 */
Len Brown4be44fc2005-08-05 00:44:28 -04001310acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Len Brown4be44fc2005-08-05 00:44:28 -04001312 acpi_status status = AE_OK;
1313 struct semaphore *sem = (struct semaphore *)handle;
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001314 long jiffies;
Len Brown4be44fc2005-08-05 00:44:28 -04001315 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Lv Zheng7901a052015-08-05 16:23:51 +08001317 if (!acpi_os_initialized)
1318 return AE_OK;
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001321 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -04001324 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Len Brown4be44fc2005-08-05 00:44:28 -04001326 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
1327 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001329 if (timeout == ACPI_WAIT_FOREVER)
1330 jiffies = MAX_SCHEDULE_TIMEOUT;
1331 else
1332 jiffies = msecs_to_jiffies(timeout);
Al Stonecad15252013-12-04 12:59:11 -07001333
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001334 ret = down_timeout(sem, jiffies);
1335 if (ret)
1336 status = AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -04001339 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -04001340 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -04001341 handle, units, timeout,
1342 acpi_format_exception(status)));
1343 } else {
1344 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -04001345 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001346 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348
Patrick Mocheld550d982006-06-27 00:41:40 -04001349 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352/*
1353 * TODO: Support for units > 1?
1354 */
Len Brown4be44fc2005-08-05 00:44:28 -04001355acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Len Brown4be44fc2005-08-05 00:44:28 -04001357 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Lv Zheng7901a052015-08-05 16:23:51 +08001359 if (!acpi_os_initialized)
1360 return AE_OK;
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001363 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -04001366 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Len Brown4be44fc2005-08-05 00:44:28 -04001368 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1369 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 up(sem);
1372
Patrick Mocheld550d982006-06-27 00:41:40 -04001373 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
Len Brown4be44fc2005-08-05 00:44:28 -04001375
Lv Zheng4d946f72015-10-19 10:25:56 +08001376acpi_status acpi_os_get_line(char *buffer, u32 buffer_length, u32 *bytes_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378#ifdef ENABLE_DEBUGGER
1379 if (acpi_in_debugger) {
1380 u32 chars;
1381
Lv Zheng4d946f72015-10-19 10:25:56 +08001382 kdb_read(buffer, buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /* remove the CR kdb includes */
1385 chars = strlen(buffer) - 1;
1386 buffer[chars] = '\0';
1387 }
1388#endif
1389
Lv Zheng4d946f72015-10-19 10:25:56 +08001390 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Len Brown4be44fc2005-08-05 00:44:28 -04001393acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Len Brown4be44fc2005-08-05 00:44:28 -04001395 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 case ACPI_SIGNAL_FATAL:
1397 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1398 break;
1399 case ACPI_SIGNAL_BREAKPOINT:
1400 /*
1401 * AML Breakpoint
1402 * ACPI spec. says to treat it as a NOP unless
1403 * you are debugging. So if/when we integrate
1404 * AML debugger into the kernel debugger its
1405 * hook will go here. But until then it is
1406 * not useful to print anything on breakpoints.
1407 */
1408 break;
1409 default:
1410 break;
1411 }
1412
1413 return AE_OK;
1414}
Len Brown4be44fc2005-08-05 00:44:28 -04001415
Len Brown4be44fc2005-08-05 00:44:28 -04001416static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -04001419 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 if (!str || !*str)
1422 return 0;
1423
Dan Carpenter5e2be4e2013-10-18 12:01:43 +03001424 for (; count-- && *str; str++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (isalnum(*str) || *str == ' ' || *str == ':')
1426 *p++ = *str;
1427 else if (*str == '\'' || *str == '"')
1428 continue;
1429 else
1430 break;
1431 }
1432 *p = 0;
1433
1434 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
1438__setup("acpi_os_name=", acpi_os_name_setup);
1439
Lin Ming12d32062010-12-09 16:51:06 +08001440#define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
1441#define OSI_STRING_ENTRIES_MAX 16 /* arbitrary */
1442
1443struct osi_setup_entry {
1444 char string[OSI_STRING_LENGTH_MAX];
1445 bool enable;
1446};
1447
Hanjun Guoe73d3132013-08-13 18:31:15 +08001448static struct osi_setup_entry
1449 osi_setup_entries[OSI_STRING_ENTRIES_MAX] __initdata = {
Shaohua Liaa165972011-07-28 13:48:43 -07001450 {"Module Device", true},
1451 {"Processor Device", true},
1452 {"3.0 _SCP Extensions", true},
1453 {"Processor Aggregator Device", true},
1454};
Lin Ming12d32062010-12-09 16:51:06 +08001455
Lin Mingd90aa922010-12-09 16:50:52 +08001456void __init acpi_osi_setup(char *str)
1457{
Lin Ming12d32062010-12-09 16:51:06 +08001458 struct osi_setup_entry *osi;
1459 bool enable = true;
1460 int i;
1461
Lin Mingd90aa922010-12-09 16:50:52 +08001462 if (!acpi_gbl_create_osi_method)
1463 return;
1464
1465 if (str == NULL || *str == '\0') {
1466 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1467 acpi_gbl_create_osi_method = FALSE;
Lin Ming12d32062010-12-09 16:51:06 +08001468 return;
1469 }
1470
1471 if (*str == '!') {
1472 str++;
Lv Zheng5dc17982013-07-22 16:08:25 +08001473 if (*str == '\0') {
1474 osi_linux.default_disabling = 1;
1475 return;
Lv Zheng741d8122013-07-22 16:08:36 +08001476 } else if (*str == '*') {
1477 acpi_update_interfaces(ACPI_DISABLE_ALL_STRINGS);
1478 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1479 osi = &osi_setup_entries[i];
1480 osi->enable = false;
1481 }
1482 return;
Lv Zheng5dc17982013-07-22 16:08:25 +08001483 }
Lin Ming12d32062010-12-09 16:51:06 +08001484 enable = false;
1485 }
1486
1487 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1488 osi = &osi_setup_entries[i];
1489 if (!strcmp(osi->string, str)) {
1490 osi->enable = enable;
1491 break;
1492 } else if (osi->string[0] == '\0') {
1493 osi->enable = enable;
1494 strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
1495 break;
1496 }
1497 }
Lin Mingd90aa922010-12-09 16:50:52 +08001498}
1499
Len Brownd4b7dc42008-01-23 20:50:56 -05001500static void __init set_osi_linux(unsigned int enable)
1501{
Lin Mingd90aa922010-12-09 16:50:52 +08001502 if (osi_linux.enable != enable)
Len Brownd4b7dc42008-01-23 20:50:56 -05001503 osi_linux.enable = enable;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001504
1505 if (osi_linux.enable)
1506 acpi_osi_setup("Linux");
1507 else
1508 acpi_osi_setup("!Linux");
1509
Len Brownd4b7dc42008-01-23 20:50:56 -05001510 return;
1511}
Len Brownf5076542007-05-30 00:10:38 -04001512
Len Brownd4b7dc42008-01-23 20:50:56 -05001513static void __init acpi_cmdline_osi_linux(unsigned int enable)
1514{
Lin Mingd90aa922010-12-09 16:50:52 +08001515 osi_linux.cmdline = 1; /* cmdline set the default and override DMI */
1516 osi_linux.dmi = 0;
Len Brownd4b7dc42008-01-23 20:50:56 -05001517 set_osi_linux(enable);
Len Brownf5076542007-05-30 00:10:38 -04001518
Len Brownd4b7dc42008-01-23 20:50:56 -05001519 return;
1520}
1521
1522void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1523{
Len Brownd4b7dc42008-01-23 20:50:56 -05001524 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1525
1526 if (enable == -1)
1527 return;
1528
Lin Mingd90aa922010-12-09 16:50:52 +08001529 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
Len Brownd4b7dc42008-01-23 20:50:56 -05001530 set_osi_linux(enable);
1531
Len Brownf5076542007-05-30 00:10:38 -04001532 return;
1533}
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535/*
Len Brownae00d812007-05-29 18:43:33 -04001536 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1537 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 * empty string disables _OSI
Len Brownae00d812007-05-29 18:43:33 -04001539 * string starting with '!' disables that string
1540 * otherwise string is added to list, augmenting built-in strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 */
Lin Mingb0ed7a92010-08-06 09:35:51 +08001542static void __init acpi_osi_setup_late(void)
1543{
Lin Ming12d32062010-12-09 16:51:06 +08001544 struct osi_setup_entry *osi;
1545 char *str;
1546 int i;
Lin Mingd90aa922010-12-09 16:50:52 +08001547 acpi_status status;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001548
Lv Zheng5dc17982013-07-22 16:08:25 +08001549 if (osi_linux.default_disabling) {
1550 status = acpi_update_interfaces(ACPI_DISABLE_ALL_VENDOR_STRINGS);
1551
1552 if (ACPI_SUCCESS(status))
1553 printk(KERN_INFO PREFIX "Disabled all _OSI OS vendors\n");
1554 }
1555
Lin Ming12d32062010-12-09 16:51:06 +08001556 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1557 osi = &osi_setup_entries[i];
1558 str = osi->string;
Lin Mingb0ed7a92010-08-06 09:35:51 +08001559
Lin Ming12d32062010-12-09 16:51:06 +08001560 if (*str == '\0')
1561 break;
1562 if (osi->enable) {
1563 status = acpi_install_interface(str);
Lin Mingd90aa922010-12-09 16:50:52 +08001564
Lin Ming12d32062010-12-09 16:51:06 +08001565 if (ACPI_SUCCESS(status))
1566 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1567 } else {
1568 status = acpi_remove_interface(str);
Lin Mingd90aa922010-12-09 16:50:52 +08001569
Lin Ming12d32062010-12-09 16:51:06 +08001570 if (ACPI_SUCCESS(status))
1571 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1572 }
Lin Mingb0ed7a92010-08-06 09:35:51 +08001573 }
1574}
1575
Lin Mingd90aa922010-12-09 16:50:52 +08001576static int __init osi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Lin Mingd90aa922010-12-09 16:50:52 +08001578 if (str && !strcmp("Linux", str))
1579 acpi_cmdline_osi_linux(1);
1580 else if (str && !strcmp("!Linux", str))
1581 acpi_cmdline_osi_linux(0);
1582 else
1583 acpi_osi_setup(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 return 1;
1586}
1587
Lin Mingd90aa922010-12-09 16:50:52 +08001588__setup("acpi_osi=", osi_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Bob Moore22b5afc2014-03-24 14:49:00 +08001590/*
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001591 * Disable the auto-serialization of named objects creation methods.
Bob Moore22b5afc2014-03-24 14:49:00 +08001592 *
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001593 * This feature is enabled by default. It marks the AML control methods
Bob Moore22b5afc2014-03-24 14:49:00 +08001594 * that contain the opcodes to create named objects as "Serialized".
1595 */
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001596static int __init acpi_no_auto_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001598 acpi_gbl_auto_serialize_methods = FALSE;
1599 pr_info("ACPI: auto-serialization disabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 return 1;
1602}
1603
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001604__setup("acpi_no_auto_serialize", acpi_no_auto_serialize_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Thomas Renningerdf92e692008-02-04 23:31:22 -08001606/* Check of resource interference between native drivers and ACPI
1607 * OperationRegions (SystemIO and System Memory only).
1608 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1609 * in arbitrary AML code and can interfere with legacy drivers.
1610 * acpi_enforce_resources= can be set to:
1611 *
Luca Tettamanti7e905602009-03-30 00:01:27 +02001612 * - strict (default) (2)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001613 * -> further driver trying to access the resources will not load
Luca Tettamanti7e905602009-03-30 00:01:27 +02001614 * - lax (1)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001615 * -> further driver trying to access the resources will load, but you
1616 * get a system message that something might go wrong...
1617 *
1618 * - no (0)
1619 * -> ACPI Operation Region resources will not be registered
1620 *
1621 */
1622#define ENFORCE_RESOURCES_STRICT 2
1623#define ENFORCE_RESOURCES_LAX 1
1624#define ENFORCE_RESOURCES_NO 0
1625
Luca Tettamanti7e905602009-03-30 00:01:27 +02001626static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001627
1628static int __init acpi_enforce_resources_setup(char *str)
1629{
1630 if (str == NULL || *str == '\0')
1631 return 0;
1632
1633 if (!strcmp("strict", str))
1634 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1635 else if (!strcmp("lax", str))
1636 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1637 else if (!strcmp("no", str))
1638 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1639
1640 return 1;
1641}
1642
1643__setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1644
1645/* Check for resource conflicts between ACPI OperationRegions and native
1646 * drivers */
Jean Delvare876fba42009-11-11 15:22:15 +01001647int acpi_check_resource_conflict(const struct resource *res)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001648{
Lin Mingf654c0f2012-01-12 13:10:32 +08001649 acpi_adr_space_type space_id;
1650 acpi_size length;
1651 u8 warn = 0;
1652 int clash = 0;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001653
1654 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1655 return 0;
1656 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1657 return 0;
1658
Lin Mingf654c0f2012-01-12 13:10:32 +08001659 if (res->flags & IORESOURCE_IO)
1660 space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1661 else
1662 space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001663
Alexandru Gheorghiue4f52242013-03-12 08:53:02 +00001664 length = resource_size(res);
Lin Mingf654c0f2012-01-12 13:10:32 +08001665 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO)
1666 warn = 1;
1667 clash = acpi_check_address_range(space_id, res->start, length, warn);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001668
1669 if (clash) {
1670 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
Jean Delvare14f03342009-09-08 15:31:46 +02001671 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1672 printk(KERN_NOTICE "ACPI: This conflict may"
1673 " cause random problems and system"
1674 " instability\n");
1675 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1676 " for this device, you should use it instead of"
1677 " the native driver\n");
Thomas Renningerdf92e692008-02-04 23:31:22 -08001678 }
1679 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1680 return -EBUSY;
1681 }
1682 return 0;
1683}
Thomas Renninger443dea72008-02-04 23:31:23 -08001684EXPORT_SYMBOL(acpi_check_resource_conflict);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001685
1686int acpi_check_region(resource_size_t start, resource_size_t n,
1687 const char *name)
1688{
1689 struct resource res = {
1690 .start = start,
1691 .end = start + n - 1,
1692 .name = name,
1693 .flags = IORESOURCE_IO,
1694 };
1695
1696 return acpi_check_resource_conflict(&res);
1697}
1698EXPORT_SYMBOL(acpi_check_region);
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700/*
Jean Delvare70dd6be2010-05-27 19:58:37 +02001701 * Let drivers know whether the resource checks are effective
1702 */
1703int acpi_resources_are_enforced(void)
1704{
1705 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1706}
1707EXPORT_SYMBOL(acpi_resources_are_enforced);
1708
Hans de Goedea87878b2015-06-16 16:27:46 +02001709bool acpi_osi_is_win8(void)
1710{
1711 return acpi_gbl_osi_data >= ACPI_OSI_WIN_8;
1712}
1713EXPORT_SYMBOL(acpi_osi_is_win8);
1714
Jean Delvare70dd6be2010-05-27 19:58:37 +02001715/*
Lin Ming9f63b882011-03-23 17:26:34 +08001716 * Deallocate the memory for a spinlock.
1717 */
1718void acpi_os_delete_lock(acpi_spinlock handle)
1719{
1720 ACPI_FREE(handle);
1721}
1722
1723/*
Robert Moore73459f72005-06-24 00:00:00 -04001724 * Acquire a spinlock.
1725 *
1726 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001727 */
1728
Bob Moore967440e32006-06-23 17:04:00 -04001729acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001730{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001731 acpi_cpu_flags flags;
Bob Moore967440e32006-06-23 17:04:00 -04001732 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001733 return flags;
1734}
1735
1736/*
1737 * Release a spinlock. See above.
1738 */
1739
Bob Moore967440e32006-06-23 17:04:00 -04001740void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Robert Moore73459f72005-06-24 00:00:00 -04001741{
Bob Moore967440e32006-06-23 17:04:00 -04001742 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001743}
1744
Robert Moore73459f72005-06-24 00:00:00 -04001745#ifndef ACPI_USE_LOCAL_CACHE
1746
1747/*******************************************************************************
1748 *
1749 * FUNCTION: acpi_os_create_cache
1750 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001751 * PARAMETERS: name - Ascii name for the cache
1752 * size - Size of each cached object
1753 * depth - Maximum depth of the cache (in objects) <ignored>
1754 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001755 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001756 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001757 *
1758 * DESCRIPTION: Create a cache object
1759 *
1760 ******************************************************************************/
1761
1762acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001763acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001764{
Paul Mundt20c2df82007-07-20 10:11:58 +09001765 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001766 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001767 return AE_ERROR;
1768 else
1769 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001770}
1771
1772/*******************************************************************************
1773 *
1774 * FUNCTION: acpi_os_purge_cache
1775 *
1776 * PARAMETERS: Cache - Handle to cache object
1777 *
1778 * RETURN: Status
1779 *
1780 * DESCRIPTION: Free all objects within the requested cache.
1781 *
1782 ******************************************************************************/
1783
Len Brown4be44fc2005-08-05 00:44:28 -04001784acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001785{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001786 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001787 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001788}
1789
1790/*******************************************************************************
1791 *
1792 * FUNCTION: acpi_os_delete_cache
1793 *
1794 * PARAMETERS: Cache - Handle to cache object
1795 *
1796 * RETURN: Status
1797 *
1798 * DESCRIPTION: Free all objects within the requested cache and delete the
1799 * cache object.
1800 *
1801 ******************************************************************************/
1802
Len Brown4be44fc2005-08-05 00:44:28 -04001803acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001804{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001805 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001806 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001807}
1808
1809/*******************************************************************************
1810 *
1811 * FUNCTION: acpi_os_release_object
1812 *
1813 * PARAMETERS: Cache - Handle to cache object
1814 * Object - The object to be released
1815 *
1816 * RETURN: None
1817 *
1818 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1819 * the object is deleted.
1820 *
1821 ******************************************************************************/
1822
Len Brown4be44fc2005-08-05 00:44:28 -04001823acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001824{
Len Brown4be44fc2005-08-05 00:44:28 -04001825 kmem_cache_free(cache, object);
1826 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001827}
Robert Moore73459f72005-06-24 00:00:00 -04001828#endif
Myron Stowed362eda2010-10-21 14:24:04 -06001829
Lv Zhenga94e88c2014-04-04 12:39:11 +08001830static int __init acpi_no_static_ssdt_setup(char *s)
Lv Zhengb75dd292013-06-08 00:58:48 +00001831{
Lv Zhenga94e88c2014-04-04 12:39:11 +08001832 acpi_gbl_disable_ssdt_table_install = TRUE;
1833 pr_info("ACPI: static SSDT installation disabled\n");
Lv Zhengb75dd292013-06-08 00:58:48 +00001834
Lv Zhenga94e88c2014-04-04 12:39:11 +08001835 return 0;
Lv Zhengb75dd292013-06-08 00:58:48 +00001836}
1837
Lv Zhenga94e88c2014-04-04 12:39:11 +08001838early_param("acpi_no_static_ssdt", acpi_no_static_ssdt_setup);
Lv Zhengb75dd292013-06-08 00:58:48 +00001839
Lv Zheng4dde5072014-02-11 11:01:52 +08001840static int __init acpi_disable_return_repair(char *s)
1841{
1842 printk(KERN_NOTICE PREFIX
1843 "ACPI: Predefined validation mechanism disabled\n");
1844 acpi_gbl_disable_auto_repair = TRUE;
1845
1846 return 1;
1847}
1848
1849__setup("acpica_no_return_repair", acpi_disable_return_repair);
1850
Myron Stowed362eda2010-10-21 14:24:04 -06001851acpi_status __init acpi_os_initialize(void)
1852{
1853 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1854 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1855 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1856 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
Randy Wrighta4714a82014-06-04 08:55:59 -07001857 if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) {
1858 /*
1859 * Use acpi_os_map_generic_address to pre-map the reset
1860 * register if it's in system memory.
1861 */
1862 int rv;
1863
1864 rv = acpi_os_map_generic_address(&acpi_gbl_FADT.reset_register);
1865 pr_debug(PREFIX "%s: map reset_reg status %d\n", __func__, rv);
1866 }
Lv Zheng7901a052015-08-05 16:23:51 +08001867 acpi_os_initialized = true;
Myron Stowed362eda2010-10-21 14:24:04 -06001868
1869 return AE_OK;
1870}
1871
Zhang Rui32d47ee2010-12-08 10:40:36 +08001872acpi_status __init acpi_os_initialize1(void)
Myron Stowed362eda2010-10-21 14:24:04 -06001873{
Tejun Heo44d25882011-02-01 11:42:42 +01001874 kacpid_wq = alloc_workqueue("kacpid", 0, 1);
1875 kacpi_notify_wq = alloc_workqueue("kacpi_notify", 0, 1);
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001876 kacpi_hotplug_wq = alloc_ordered_workqueue("kacpi_hotplug", 0);
Myron Stowed362eda2010-10-21 14:24:04 -06001877 BUG_ON(!kacpid_wq);
1878 BUG_ON(!kacpi_notify_wq);
1879 BUG_ON(!kacpi_hotplug_wq);
Len Brown1bd64d42010-10-26 14:50:56 -04001880 acpi_install_interface_handler(acpi_osi_handler);
1881 acpi_osi_setup_late();
Myron Stowed362eda2010-10-21 14:24:04 -06001882 return AE_OK;
1883}
1884
1885acpi_status acpi_os_terminate(void)
1886{
1887 if (acpi_irq_handler) {
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +01001888 acpi_os_remove_interrupt_handler(acpi_gbl_FADT.sci_interrupt,
Myron Stowed362eda2010-10-21 14:24:04 -06001889 acpi_irq_handler);
1890 }
1891
1892 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1893 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1894 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1895 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
Randy Wrighta4714a82014-06-04 08:55:59 -07001896 if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER)
1897 acpi_os_unmap_generic_address(&acpi_gbl_FADT.reset_register);
Myron Stowed362eda2010-10-21 14:24:04 -06001898
1899 destroy_workqueue(kacpid_wq);
1900 destroy_workqueue(kacpi_notify_wq);
1901 destroy_workqueue(kacpi_hotplug_wq);
1902
1903 return AE_OK;
1904}
Tang Liang09f98a82011-12-09 10:05:54 +08001905
1906acpi_status acpi_os_prepare_sleep(u8 sleep_state, u32 pm1a_control,
1907 u32 pm1b_control)
1908{
1909 int rc = 0;
1910 if (__acpi_os_prepare_sleep)
1911 rc = __acpi_os_prepare_sleep(sleep_state,
1912 pm1a_control, pm1b_control);
1913 if (rc < 0)
1914 return AE_ERROR;
1915 else if (rc > 0)
1916 return AE_CTRL_SKIP;
1917
1918 return AE_OK;
1919}
1920
1921void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
1922 u32 pm1a_ctrl, u32 pm1b_ctrl))
1923{
1924 __acpi_os_prepare_sleep = func;
1925}
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001926
Ben Guthrod6b47b12013-07-30 08:24:52 -04001927acpi_status acpi_os_prepare_extended_sleep(u8 sleep_state, u32 val_a,
1928 u32 val_b)
1929{
1930 int rc = 0;
1931 if (__acpi_os_prepare_extended_sleep)
1932 rc = __acpi_os_prepare_extended_sleep(sleep_state,
1933 val_a, val_b);
1934 if (rc < 0)
1935 return AE_ERROR;
1936 else if (rc > 0)
1937 return AE_CTRL_SKIP;
1938
1939 return AE_OK;
1940}
1941
1942void acpi_os_set_prepare_extended_sleep(int (*func)(u8 sleep_state,
1943 u32 val_a, u32 val_b))
1944{
1945 __acpi_os_prepare_extended_sleep = func;
1946}