blob: f725813d0cce6a4c5e0418334a9d4bd812dc8ef8 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
4 *
5 * Copyright (C) 2000 Andrew Henroid
6 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
7 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04008 * Copyright (c) 2008 Intel Corporation
9 * Author: Matthew Wilcox <willy@linux.intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +010012#define pr_fmt(fmt) "ACPI: OSL: " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
17#include <linux/mm.h>
Myron Stoweba242d52012-01-20 19:13:30 -070018#include <linux/highmem.h>
Joel Fernandes (Google)bee6f872019-07-16 18:12:28 -040019#include <linux/lockdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/interrupt.h>
22#include <linux/kmod.h>
23#include <linux/delay.h>
24#include <linux/workqueue.h>
25#include <linux/nmi.h>
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +030026#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/efi.h>
Thomas Renningerdf92e692008-02-04 23:31:22 -080028#include <linux/ioport.h>
29#include <linux/list.h>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -040030#include <linux/jiffies.h>
31#include <linux/semaphore.h>
Josh Boyer41fa1ee2019-08-19 17:17:51 -070032#include <linux/security.h>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -040033
34#include <asm/io.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080035#include <linux/uaccess.h>
Christoph Hellwig2f8e2c82015-08-28 09:27:14 +020036#include <linux/io-64-nonatomic-lo-hi.h>
Matthew Wilcoxf1241c82008-03-14 13:43:13 -040037
Heikki Krogerusd2d2e3c2018-06-21 16:43:17 +030038#include "acpica/accommon.h"
Lv Zheng1129c922013-07-23 16:11:55 +080039#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +010041/* Definitions for ACPI_DEBUG_PRINT() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define _COMPONENT ACPI_OS_SERVICES
Len Brownf52fd662007-02-12 22:42:12 -050043ACPI_MODULE_NAME("osl");
Hanjun Guo07070e12014-03-13 12:47:39 +080044
Len Brown4be44fc2005-08-05 00:44:28 -040045struct acpi_os_dpc {
46 acpi_osd_exec_callback function;
47 void *context;
David Howells65f27f32006-11-22 14:55:48 +000048 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#ifdef ENABLE_DEBUGGER
52#include <linux/kdb.h>
53
54/* stuff for debugger support */
55int acpi_in_debugger;
56EXPORT_SYMBOL(acpi_in_debugger);
Len Brown4be44fc2005-08-05 00:44:28 -040057#endif /*ENABLE_DEBUGGER */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Tang Liang09f98a82011-12-09 10:05:54 +080059static int (*__acpi_os_prepare_sleep)(u8 sleep_state, u32 pm1a_ctrl,
60 u32 pm1b_ctrl);
Ben Guthrod6b47b12013-07-30 08:24:52 -040061static int (*__acpi_os_prepare_extended_sleep)(u8 sleep_state, u32 val_a,
62 u32 val_b);
Tang Liang09f98a82011-12-09 10:05:54 +080063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static acpi_osd_handler acpi_irq_handler;
65static void *acpi_irq_context;
66static struct workqueue_struct *kacpid_wq;
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -040067static struct workqueue_struct *kacpi_notify_wq;
Yinghai Lu92d8aff2013-01-21 13:20:47 -080068static struct workqueue_struct *kacpi_hotplug_wq;
Lv Zheng7901a052015-08-05 16:23:51 +080069static bool acpi_os_initialized;
Chen Yu49e4b8432015-10-25 01:02:19 +080070unsigned int acpi_sci_irq = INVALID_ACPI_IRQ;
Lv Zheng8d3523f2016-12-14 15:04:46 +080071bool acpi_permanent_mmap = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Myron Stowe620242a2010-10-21 14:23:53 -060073/*
74 * This list of permanent mappings is for memory that may be accessed from
75 * interrupt context, where we can't do the ioremap().
76 */
77struct acpi_ioremap {
78 struct list_head list;
79 void __iomem *virt;
80 acpi_physical_address phys;
81 acpi_size size;
Rafael J. Wysocki17576592020-07-02 13:19:12 +020082 union {
83 unsigned long refcount;
84 struct rcu_work rwork;
85 } track;
Myron Stowe620242a2010-10-21 14:23:53 -060086};
87
88static LIST_HEAD(acpi_ioremaps);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +010089static DEFINE_MUTEX(acpi_ioremap_lock);
Joel Fernandes (Google)bee6f872019-07-16 18:12:28 -040090#define acpi_ioremap_lock_held() lock_is_held(&acpi_ioremap_lock.dep_map)
Myron Stowe620242a2010-10-21 14:23:53 -060091
Myron Stowebc9ffce2011-11-07 16:23:27 -070092static void __init acpi_request_region (struct acpi_generic_address *gas,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -070093 unsigned int length, char *desc)
94{
Myron Stowebc9ffce2011-11-07 16:23:27 -070095 u64 addr;
96
97 /* Handle possible alignment issues */
98 memcpy(&addr, &gas->address, sizeof(addr));
99 if (!addr || !length)
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700100 return;
101
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200102 /* Resources are never freed */
103 if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
104 request_region(addr, length, desc);
105 else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
106 request_mem_region(addr, length, desc);
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700107}
108
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200109static int __init acpi_reserve_resources(void)
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700110{
Len Browneee3c852007-02-03 01:38:16 -0500111 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700112 "ACPI PM1a_EVT_BLK");
113
Len Browneee3c852007-02-03 01:38:16 -0500114 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700115 "ACPI PM1b_EVT_BLK");
116
Len Browneee3c852007-02-03 01:38:16 -0500117 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700118 "ACPI PM1a_CNT_BLK");
119
Len Browneee3c852007-02-03 01:38:16 -0500120 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700121 "ACPI PM1b_CNT_BLK");
122
Len Browneee3c852007-02-03 01:38:16 -0500123 if (acpi_gbl_FADT.pm_timer_length == 4)
124 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700125
Len Browneee3c852007-02-03 01:38:16 -0500126 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700127 "ACPI PM2_CNT_BLK");
128
129 /* Length of GPE blocks must be a non-negative multiple of 2 */
130
Len Browneee3c852007-02-03 01:38:16 -0500131 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
132 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
133 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700134
Len Browneee3c852007-02-03 01:38:16 -0500135 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
136 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
137 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200138
139 return 0;
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700140}
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200141fs_initcall_sync(acpi_reserve_resources);
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700142
Len Brown4be44fc2005-08-05 00:44:28 -0400143void acpi_os_printf(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 va_list args;
146 va_start(args, fmt);
147 acpi_os_vprintf(fmt, args);
148 va_end(args);
149}
Lv Zheng836d083012015-12-03 10:43:14 +0800150EXPORT_SYMBOL(acpi_os_printf);
Len Brown4be44fc2005-08-05 00:44:28 -0400151
Len Brown4be44fc2005-08-05 00:44:28 -0400152void acpi_os_vprintf(const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 static char buffer[512];
Len Brown4be44fc2005-08-05 00:44:28 -0400155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 vsprintf(buffer, fmt, args);
157
158#ifdef ENABLE_DEBUGGER
159 if (acpi_in_debugger) {
160 kdb_printf("%s", buffer);
161 } else {
Joe Perchesabc4b9a2016-10-12 11:50:34 -0700162 if (printk_get_level(buffer))
163 printk("%s", buffer);
164 else
165 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167#else
Joe Perchesabc4b9a2016-10-12 11:50:34 -0700168 if (acpi_debugger_write_log(buffer) < 0) {
169 if (printk_get_level(buffer))
170 printk("%s", buffer);
171 else
172 printk(KERN_CONT "%s", buffer);
173 }
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{
Andy Shevchenko5dcb9ca2016-12-02 19:42:46 +0200181 return kstrtoul(arg, 16, &acpi_rsdp);
Takao Indoh4996c022011-07-14 18:05:21 -0400182}
183early_param("acpi_rsdp", setup_acpi_rsdp);
184#endif
185
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300186acpi_physical_address __init acpi_os_get_root_pointer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Juergen Grossdfc93272018-02-19 11:09:04 +0100188 acpi_physical_address pa;
Andy Shevchenko2fb65f02016-12-02 19:42:47 +0200189
Takao Indoh4996c022011-07-14 18:05:21 -0400190#ifdef CONFIG_KEXEC
Josh Boyer41fa1ee2019-08-19 17:17:51 -0700191 /*
192 * We may have been provided with an RSDP on the command line,
193 * but if a malicious user has done so they may be pointing us
194 * at modified ACPI tables that could alter kernel behaviour -
195 * so, we check the lockdown status before making use of
196 * it. If we trust it then also stash it in an architecture
197 * specific location (if appropriate) so it can be carried
198 * over further kexec()s.
199 */
200 if (acpi_rsdp && !security_locked_down(LOCKDOWN_ACPI_TABLES)) {
201 acpi_arch_set_root_pointer(acpi_rsdp);
Takao Indoh4996c022011-07-14 18:05:21 -0400202 return acpi_rsdp;
Josh Boyer41fa1ee2019-08-19 17:17:51 -0700203 }
Takao Indoh4996c022011-07-14 18:05:21 -0400204#endif
Juergen Grossdfc93272018-02-19 11:09:04 +0100205 pa = acpi_arch_get_root_pointer();
206 if (pa)
207 return pa;
Takao Indoh4996c022011-07-14 18:05:21 -0400208
Matt Fleming83e68182012-11-14 09:42:35 +0000209 if (efi_enabled(EFI_CONFIG_TABLES)) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800210 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300211 return efi.acpi20;
Andy Shevchenko2fb65f02016-12-02 19:42:47 +0200212 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300213 return efi.acpi;
Hanjun Guo2e670de2021-06-02 16:54:29 +0800214 pr_err("System description tables not found\n");
Graeme Gregory8a1664b2014-07-18 18:02:52 +0800215 } else if (IS_ENABLED(CONFIG_ACPI_LEGACY_TABLES_LOOKUP)) {
Len Brown239665a2007-11-23 20:08:02 -0500216 acpi_find_root_pointer(&pa);
Len Brown239665a2007-11-23 20:08:02 -0500217 }
Graeme Gregory8a1664b2014-07-18 18:02:52 +0800218
Andy Shevchenko2fb65f02016-12-02 19:42:47 +0200219 return pa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600222/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe4a3cba52010-10-21 14:24:14 -0600223static struct acpi_ioremap *
224acpi_map_lookup(acpi_physical_address phys, acpi_size size)
Myron Stowe620242a2010-10-21 14:23:53 -0600225{
226 struct acpi_ioremap *map;
227
Joel Fernandes (Google)bee6f872019-07-16 18:12:28 -0400228 list_for_each_entry_rcu(map, &acpi_ioremaps, list, acpi_ioremap_lock_held())
Myron Stowe620242a2010-10-21 14:23:53 -0600229 if (map->phys <= phys &&
230 phys + size <= map->phys + map->size)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600231 return map;
232
233 return NULL;
234}
235
236/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
237static void __iomem *
238acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
239{
240 struct acpi_ioremap *map;
241
242 map = acpi_map_lookup(phys, size);
243 if (map)
244 return map->virt + (phys - map->phys);
Myron Stowe620242a2010-10-21 14:23:53 -0600245
246 return NULL;
247}
248
Rafael J. Wysocki13606a22011-02-08 23:38:25 +0100249void __iomem *acpi_os_get_iomem(acpi_physical_address phys, unsigned int size)
250{
251 struct acpi_ioremap *map;
252 void __iomem *virt = NULL;
253
254 mutex_lock(&acpi_ioremap_lock);
255 map = acpi_map_lookup(phys, size);
256 if (map) {
257 virt = map->virt + (phys - map->phys);
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200258 map->track.refcount++;
Rafael J. Wysocki13606a22011-02-08 23:38:25 +0100259 }
260 mutex_unlock(&acpi_ioremap_lock);
261 return virt;
262}
263EXPORT_SYMBOL_GPL(acpi_os_get_iomem);
264
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600265/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe620242a2010-10-21 14:23:53 -0600266static struct acpi_ioremap *
267acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
268{
269 struct acpi_ioremap *map;
270
Joel Fernandes (Google)bee6f872019-07-16 18:12:28 -0400271 list_for_each_entry_rcu(map, &acpi_ioremaps, list, acpi_ioremap_lock_held())
Myron Stowe4a3cba52010-10-21 14:24:14 -0600272 if (map->virt <= virt &&
273 virt + size <= map->virt + map->size)
Myron Stowe620242a2010-10-21 14:23:53 -0600274 return map;
275
276 return NULL;
277}
278
Sunil V L214c2362023-05-15 11:19:12 +0530279#if defined(CONFIG_IA64) || defined(CONFIG_ARM64) || defined(CONFIG_RISCV)
Myron Stoweba242d52012-01-20 19:13:30 -0700280/* ioremap will take care of cache attributes */
281#define should_use_kmap(pfn) 0
Graeme Gregoryaafc65c2015-03-24 14:02:35 +0000282#else
283#define should_use_kmap(pfn) page_is_ram(pfn)
Myron Stoweba242d52012-01-20 19:13:30 -0700284#endif
285
Jia He12064c12021-09-23 11:35:57 +0800286static void __iomem *acpi_map(acpi_physical_address pg_off, unsigned long pg_sz)
Myron Stoweba242d52012-01-20 19:13:30 -0700287{
288 unsigned long pfn;
289
290 pfn = pg_off >> PAGE_SHIFT;
291 if (should_use_kmap(pfn)) {
292 if (pg_sz > PAGE_SIZE)
293 return NULL;
294 return (void __iomem __force *)kmap(pfn_to_page(pfn));
295 } else
Jia He12064c12021-09-23 11:35:57 +0800296 return acpi_os_ioremap(pg_off, pg_sz);
Myron Stoweba242d52012-01-20 19:13:30 -0700297}
298
299static void acpi_unmap(acpi_physical_address pg_off, void __iomem *vaddr)
300{
301 unsigned long pfn;
302
303 pfn = pg_off >> PAGE_SHIFT;
Jan Beuliche2526752012-02-24 11:41:53 +0000304 if (should_use_kmap(pfn))
Myron Stoweba242d52012-01-20 19:13:30 -0700305 kunmap(pfn_to_page(pfn));
306 else
307 iounmap(vaddr);
308}
309
Rafael J. Wysocki9d128ed2016-01-02 03:10:29 +0100310/**
Jia He12064c12021-09-23 11:35:57 +0800311 * acpi_os_map_iomem - Get a virtual address for a given physical address range.
Rafael J. Wysocki9d128ed2016-01-02 03:10:29 +0100312 * @phys: Start of the physical address range to map.
313 * @size: Size of the physical address range to map.
314 *
315 * Look up the given physical address range in the list of existing ACPI memory
316 * mappings. If found, get a reference to it and return a pointer to it (its
317 * virtual address). If not found, map it, add it to that list and return a
318 * pointer to it.
319 *
Lv Zheng8d3523f2016-12-14 15:04:46 +0800320 * During early init (when acpi_permanent_mmap has not been set yet) this
Rafael J. Wysocki9d128ed2016-01-02 03:10:29 +0100321 * routine simply calls __acpi_map_table() to get the job done.
322 */
Jia He12064c12021-09-23 11:35:57 +0800323void __iomem __ref
324*acpi_os_map_iomem(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100326 struct acpi_ioremap *map;
Myron Stowe620242a2010-10-21 14:23:53 -0600327 void __iomem *virt;
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100328 acpi_physical_address pg_off;
329 acpi_size pg_sz;
Myron Stowe620242a2010-10-21 14:23:53 -0600330
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800331 if (phys > ULONG_MAX) {
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +0100332 pr_err("Cannot map memory that high: 0x%llx\n", phys);
Randy Dunlap70c08462007-02-13 16:11:36 -0800333 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Myron Stowe620242a2010-10-21 14:23:53 -0600335
Lv Zheng8d3523f2016-12-14 15:04:46 +0800336 if (!acpi_permanent_mmap)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300337 return __acpi_map_table((unsigned long)phys, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600338
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100339 mutex_lock(&acpi_ioremap_lock);
340 /* Check if there's a suitable mapping already. */
341 map = acpi_map_lookup(phys, size);
342 if (map) {
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200343 map->track.refcount++;
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100344 goto out;
345 }
346
Myron Stowe620242a2010-10-21 14:23:53 -0600347 map = kzalloc(sizeof(*map), GFP_KERNEL);
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100348 if (!map) {
349 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600350 return NULL;
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100351 }
Myron Stowe620242a2010-10-21 14:23:53 -0600352
Myron Stowe4a3cba52010-10-21 14:24:14 -0600353 pg_off = round_down(phys, PAGE_SIZE);
354 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
Jia He12064c12021-09-23 11:35:57 +0800355 virt = acpi_map(phys, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600356 if (!virt) {
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100357 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600358 kfree(map);
359 return NULL;
360 }
361
362 INIT_LIST_HEAD(&map->list);
Ard Biesheuvel17189d912020-08-18 11:13:53 +0200363 map->virt = (void __iomem __force *)((unsigned long)virt & PAGE_MASK);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600364 map->phys = pg_off;
365 map->size = pg_sz;
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200366 map->track.refcount = 1;
Myron Stowe620242a2010-10-21 14:23:53 -0600367
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600368 list_add_tail_rcu(&map->list, &acpi_ioremaps);
Myron Stowe620242a2010-10-21 14:23:53 -0600369
Lv Zhenga2383172014-05-20 15:39:41 +0800370out:
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100371 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600372 return map->virt + (phys - map->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
Lv Zhenga2383172014-05-20 15:39:41 +0800374EXPORT_SYMBOL_GPL(acpi_os_map_iomem);
375
Fabian Frederickbd721ea2016-08-02 14:03:33 -0700376void *__ref acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
Lv Zhenga2383172014-05-20 15:39:41 +0800377{
Jia He12064c12021-09-23 11:35:57 +0800378 return (void *)acpi_os_map_iomem(phys, size);
Lv Zhenga2383172014-05-20 15:39:41 +0800379}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800380EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Rafael J. Wysocki5003ad72020-07-02 14:14:05 +0200382static void acpi_os_map_remove(struct work_struct *work)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600383{
Rafael J. Wysocki5003ad72020-07-02 14:14:05 +0200384 struct acpi_ioremap *map = container_of(to_rcu_work(work),
385 struct acpi_ioremap,
386 track.rwork);
387
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200388 acpi_unmap(map->phys, map->virt);
389 kfree(map);
390}
Francesco Ruggeri833a4262019-11-19 21:47:27 -0800391
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200392/* Must be called with mutex_lock(&acpi_ioremap_lock) */
Rafael J. Wysockia968fba2020-07-02 14:13:46 +0200393static void acpi_os_drop_map_ref(struct acpi_ioremap *map)
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200394{
395 if (--map->track.refcount)
Rafael J. Wysockia968fba2020-07-02 14:13:46 +0200396 return;
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200397
398 list_del_rcu(&map->list);
399
Rafael J. Wysocki5003ad72020-07-02 14:14:05 +0200400 INIT_RCU_WORK(&map->track.rwork, acpi_os_map_remove);
Rafael J. Wysockia968fba2020-07-02 14:13:46 +0200401 queue_rcu_work(system_wq, &map->track.rwork);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600402}
Myron Stowe4a3cba52010-10-21 14:24:14 -0600403
Rafael J. Wysockia968fba2020-07-02 14:13:46 +0200404/**
405 * acpi_os_unmap_iomem - Drop a memory mapping reference.
406 * @virt: Start of the address range to drop a reference to.
407 * @size: Size of the address range to drop a reference to.
408 *
409 * Look up the given virtual address range in the list of existing ACPI memory
410 * mappings, drop a reference to it and if there are no more active references
411 * to it, queue it up for later removal.
412 *
413 * During early init (when acpi_permanent_mmap has not been set yet) this
414 * routine simply calls __acpi_unmap_table() to get the job done. Since
415 * __acpi_unmap_table() is an __init function, the __ref annotation is needed
416 * here.
417 */
418void __ref acpi_os_unmap_iomem(void __iomem *virt, acpi_size size)
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200419{
420 struct acpi_ioremap *map;
421
422 if (!acpi_permanent_mmap) {
423 __acpi_unmap_table(virt, size);
424 return;
425 }
426
427 mutex_lock(&acpi_ioremap_lock);
428
429 map = acpi_map_lookup_virt(virt, size);
430 if (!map) {
431 mutex_unlock(&acpi_ioremap_lock);
Hanjun Guo2e670de2021-06-02 16:54:29 +0800432 WARN(true, "ACPI: %s: bad address %p\n", __func__, virt);
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200433 return;
434 }
Rafael J. Wysockia968fba2020-07-02 14:13:46 +0200435 acpi_os_drop_map_ref(map);
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200436
437 mutex_unlock(&acpi_ioremap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
Lv Zhenga2383172014-05-20 15:39:41 +0800439EXPORT_SYMBOL_GPL(acpi_os_unmap_iomem);
440
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200441/**
442 * acpi_os_unmap_memory - Drop a memory mapping reference.
443 * @virt: Start of the address range to drop a reference to.
444 * @size: Size of the address range to drop a reference to.
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200445 */
Lv Zhenga2383172014-05-20 15:39:41 +0800446void __ref acpi_os_unmap_memory(void *virt, acpi_size size)
447{
Rafael J. Wysockia968fba2020-07-02 14:13:46 +0200448 acpi_os_unmap_iomem((void __iomem *)virt, size);
Lv Zhenga2383172014-05-20 15:39:41 +0800449}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800450EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Rafael J. Wysocki69155642020-09-11 14:59:35 +0200452void __iomem *acpi_os_map_generic_address(struct acpi_generic_address *gas)
Myron Stowe29718522010-10-21 14:23:59 -0600453{
Myron Stowebc9ffce2011-11-07 16:23:27 -0700454 u64 addr;
Myron Stowe29718522010-10-21 14:23:59 -0600455
Myron Stowebc9ffce2011-11-07 16:23:27 -0700456 if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
Rafael J. Wysocki69155642020-09-11 14:59:35 +0200457 return NULL;
Myron Stowe29718522010-10-21 14:23:59 -0600458
Myron Stowebc9ffce2011-11-07 16:23:27 -0700459 /* Handle possible alignment issues */
460 memcpy(&addr, &gas->address, sizeof(addr));
461 if (!addr || !gas->bit_width)
Rafael J. Wysocki69155642020-09-11 14:59:35 +0200462 return NULL;
Myron Stowe29718522010-10-21 14:23:59 -0600463
Rafael J. Wysocki69155642020-09-11 14:59:35 +0200464 return acpi_os_map_iomem(addr, gas->bit_width / 8);
Myron Stowe29718522010-10-21 14:23:59 -0600465}
Myron Stowe6f68c912011-11-07 16:23:34 -0700466EXPORT_SYMBOL(acpi_os_map_generic_address);
Myron Stowe29718522010-10-21 14:23:59 -0600467
Myron Stowe6f68c912011-11-07 16:23:34 -0700468void acpi_os_unmap_generic_address(struct acpi_generic_address *gas)
Myron Stowe29718522010-10-21 14:23:59 -0600469{
Myron Stowebc9ffce2011-11-07 16:23:27 -0700470 u64 addr;
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100471 struct acpi_ioremap *map;
Myron Stowe29718522010-10-21 14:23:59 -0600472
Myron Stowebc9ffce2011-11-07 16:23:27 -0700473 if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
Myron Stowe29718522010-10-21 14:23:59 -0600474 return;
475
Myron Stowebc9ffce2011-11-07 16:23:27 -0700476 /* Handle possible alignment issues */
477 memcpy(&addr, &gas->address, sizeof(addr));
478 if (!addr || !gas->bit_width)
Myron Stowe29718522010-10-21 14:23:59 -0600479 return;
480
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100481 mutex_lock(&acpi_ioremap_lock);
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200482
Myron Stowebc9ffce2011-11-07 16:23:27 -0700483 map = acpi_map_lookup(addr, gas->bit_width / 8);
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100484 if (!map) {
485 mutex_unlock(&acpi_ioremap_lock);
486 return;
487 }
Rafael J. Wysockia968fba2020-07-02 14:13:46 +0200488 acpi_os_drop_map_ref(map);
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200489
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100490 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe29718522010-10-21 14:23:59 -0600491}
Myron Stowe6f68c912011-11-07 16:23:34 -0700492EXPORT_SYMBOL(acpi_os_unmap_generic_address);
Myron Stowe29718522010-10-21 14:23:59 -0600493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494#ifdef ACPI_FUTURE_USAGE
495acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400496acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Len Brown4be44fc2005-08-05 00:44:28 -0400498 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return AE_BAD_PARAMETER;
500
501 *phys = virt_to_phys(virt);
502
503 return AE_OK;
504}
505#endif
506
Rafael J. Wysocki18d78b62015-07-03 01:06:00 +0200507#ifdef CONFIG_ACPI_REV_OVERRIDE_POSSIBLE
508static bool acpi_rev_override;
509
510int __init acpi_rev_override_setup(char *str)
511{
512 acpi_rev_override = true;
513 return 1;
514}
515__setup("acpi_rev_override", acpi_rev_override_setup);
516#else
517#define acpi_rev_override false
518#endif
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520#define ACPI_MAX_OVERRIDE_LEN 100
521
522static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
523
524acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400525acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
Lv Zheng80b28812016-03-24 09:38:28 +0800526 acpi_string *new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 if (!init_val || !new_val)
529 return AE_BAD_PARAMETER;
530
531 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400532 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +0100533 pr_info("Overriding _OS definition to '%s'\n", acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 *new_val = acpi_os_name;
535 }
536
Rafael J. Wysocki18d78b62015-07-03 01:06:00 +0200537 if (!memcmp(init_val->name, "_REV", 4) && acpi_rev_override) {
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +0100538 pr_info("Overriding _REV return value to 5\n");
Rafael J. Wysocki18d78b62015-07-03 01:06:00 +0200539 *new_val = (char *)5;
540 }
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return AE_OK;
543}
544
David Howells7d12e782006-10-05 14:55:46 +0100545static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Len Brown5229e872008-02-06 01:26:55 -0500547 u32 handled;
548
549 handled = (*acpi_irq_handler) (acpi_irq_context);
550
551 if (handled) {
552 acpi_irq_handled++;
553 return IRQ_HANDLED;
Len Brown88bea182009-04-21 00:35:47 -0400554 } else {
555 acpi_irq_not_handled++;
Len Brown5229e872008-02-06 01:26:55 -0500556 return IRQ_NONE;
Len Brown88bea182009-04-21 00:35:47 -0400557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400561acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
562 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 unsigned int irq;
565
Len Brown5229e872008-02-06 01:26:55 -0500566 acpi_irq_stats_init();
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /*
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100569 * ACPI interrupts different from the SCI in our copy of the FADT are
570 * not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 */
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100572 if (gsi != acpi_gbl_FADT.sci_interrupt)
573 return AE_BAD_PARAMETER;
574
575 if (acpi_irq_handler)
576 return AE_ALREADY_ACQUIRED;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +0100579 pr_err("SCI (ACPI GSI %d) not registered\n", gsi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return AE_OK;
581 }
582
583 acpi_irq_handler = handler;
584 acpi_irq_context = context;
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200585 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +0100586 pr_err("SCI (IRQ%d) allocation failed\n", irq);
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100587 acpi_irq_handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return AE_NOT_ACQUIRED;
589 }
Chen Yu49e4b8432015-10-25 01:02:19 +0800590 acpi_sci_irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 return AE_OK;
593}
594
Chen Yu49e4b8432015-10-25 01:02:19 +0800595acpi_status acpi_os_remove_interrupt_handler(u32 gsi, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Chen Yu49e4b8432015-10-25 01:02:19 +0800597 if (gsi != acpi_gbl_FADT.sci_interrupt || !acpi_sci_irq_valid())
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100598 return AE_BAD_PARAMETER;
599
Chen Yu49e4b8432015-10-25 01:02:19 +0800600 free_irq(acpi_sci_irq, acpi_irq);
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100601 acpi_irq_handler = NULL;
Chen Yu49e4b8432015-10-25 01:02:19 +0800602 acpi_sci_irq = INVALID_ACPI_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 return AE_OK;
605}
606
607/*
608 * Running in interpreter thread context, safe to sleep
609 */
610
Lin Ming439913f2010-01-28 10:53:19 +0800611void acpi_os_sleep(u64 ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Liu Chuansheng30282292013-09-12 01:42:57 +0800613 msleep(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
Len Brown4be44fc2005-08-05 00:44:28 -0400615
Len Brown4be44fc2005-08-05 00:44:28 -0400616void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 while (us) {
619 u32 delay = 1000;
620
621 if (delay > us)
622 delay = us;
623 udelay(delay);
624 touch_nmi_watchdog();
625 us -= delay;
626 }
627}
Len Brown4be44fc2005-08-05 00:44:28 -0400628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629/*
Bart Van Assche83b23482018-10-17 13:24:56 -0700630 * Support ACPI 3.0 AML Timer operand. Returns a 64-bit free-running,
631 * monotonically increasing timer with 100ns granularity. Do not use
632 * ktime_get() to implement this function because this function may get
633 * called after timekeeping has been suspended. Note: calling this function
634 * after timekeeping has been suspended may lead to unexpected results
635 * because when timekeeping is suspended the jiffies counter is not
636 * incremented. See also timekeeping_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
Len Brown4be44fc2005-08-05 00:44:28 -0400638u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Bart Van Assche83b23482018-10-17 13:24:56 -0700640 return (get_jiffies_64() - INITIAL_JIFFIES) *
641 (ACPI_100NSEC_PER_SEC / HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Rafael J. Wysocki2e433a92022-01-12 19:31:48 +0100644acpi_status acpi_os_read_port(acpi_io_address port, u32 *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 u32 dummy;
647
Rafael J. Wysocki2e433a92022-01-12 19:31:48 +0100648 if (value)
649 *value = 0;
650 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 value = &dummy;
652
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800653 if (width <= 8) {
Rafael J. Wysocki2e433a92022-01-12 19:31:48 +0100654 *value = inb(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800655 } else if (width <= 16) {
Rafael J. Wysocki2e433a92022-01-12 19:31:48 +0100656 *value = inw(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800657 } else if (width <= 32) {
Rafael J. Wysocki2e433a92022-01-12 19:31:48 +0100658 *value = inl(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800659 } else {
Rafael J. Wysocki2e433a92022-01-12 19:31:48 +0100660 pr_debug("%s: Access width %d not supported\n", __func__, width);
661 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
664 return AE_OK;
665}
Len Brown4be44fc2005-08-05 00:44:28 -0400666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667EXPORT_SYMBOL(acpi_os_read_port);
668
Len Brown4be44fc2005-08-05 00:44:28 -0400669acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800671 if (width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 outb(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800673 } else if (width <= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 outw(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800675 } else if (width <= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 outl(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800677 } else {
Rafael J. Wysocki2e433a92022-01-12 19:31:48 +0100678 pr_debug("%s: Access width %d not supported\n", __func__, width);
679 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
682 return AE_OK;
683}
Len Brown4be44fc2005-08-05 00:44:28 -0400684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685EXPORT_SYMBOL(acpi_os_write_port);
686
Srinivas Pandruvadaeeb2d802017-10-05 16:24:03 -0700687int acpi_os_read_iomem(void __iomem *virt_addr, u64 *value, u32 width)
Myron Stowee615bf52012-01-20 19:13:24 -0700688{
Myron Stowee615bf52012-01-20 19:13:24 -0700689
690 switch (width) {
691 case 8:
692 *(u8 *) value = readb(virt_addr);
693 break;
694 case 16:
695 *(u16 *) value = readw(virt_addr);
696 break;
697 case 32:
698 *(u32 *) value = readl(virt_addr);
699 break;
700 case 64:
Andy Shevchenko3277b4e2015-08-17 17:28:46 +0300701 *(u64 *) value = readq(virt_addr);
Myron Stowee615bf52012-01-20 19:13:24 -0700702 break;
703 default:
Srinivas Pandruvadaeeb2d802017-10-05 16:24:03 -0700704 return -EINVAL;
Myron Stowee615bf52012-01-20 19:13:24 -0700705 }
706
Srinivas Pandruvadaeeb2d802017-10-05 16:24:03 -0700707 return 0;
708}
709
710acpi_status
711acpi_os_read_memory(acpi_physical_address phys_addr, u64 *value, u32 width)
712{
713 void __iomem *virt_addr;
714 unsigned int size = width / 8;
715 bool unmap = false;
716 u64 dummy;
717 int error;
718
719 rcu_read_lock();
720 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
721 if (!virt_addr) {
722 rcu_read_unlock();
723 virt_addr = acpi_os_ioremap(phys_addr, size);
724 if (!virt_addr)
725 return AE_BAD_ADDRESS;
726 unmap = true;
727 }
728
729 if (!value)
730 value = &dummy;
731
732 error = acpi_os_read_iomem(virt_addr, value, width);
733 BUG_ON(error);
734
Myron Stowee615bf52012-01-20 19:13:24 -0700735 if (unmap)
736 iounmap(virt_addr);
737 else
738 rcu_read_unlock();
739
740 return AE_OK;
741}
742
Myron Stowee615bf52012-01-20 19:13:24 -0700743acpi_status
Bob Moore653f4b52012-02-14 18:29:55 +0800744acpi_os_write_memory(acpi_physical_address phys_addr, u64 value, u32 width)
Myron Stowee615bf52012-01-20 19:13:24 -0700745{
746 void __iomem *virt_addr;
747 unsigned int size = width / 8;
748 bool unmap = false;
749
750 rcu_read_lock();
751 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
752 if (!virt_addr) {
753 rcu_read_unlock();
754 virt_addr = acpi_os_ioremap(phys_addr, size);
755 if (!virt_addr)
756 return AE_BAD_ADDRESS;
757 unmap = true;
758 }
759
760 switch (width) {
761 case 8:
762 writeb(value, virt_addr);
763 break;
764 case 16:
765 writew(value, virt_addr);
766 break;
767 case 32:
768 writel(value, virt_addr);
769 break;
770 case 64:
Andy Shevchenko3277b4e2015-08-17 17:28:46 +0300771 writeq(value, virt_addr);
Myron Stowee615bf52012-01-20 19:13:24 -0700772 break;
773 default:
774 BUG();
775 }
776
777 if (unmap)
778 iounmap(virt_addr);
779 else
780 rcu_read_unlock();
781
782 return AE_OK;
783}
784
Sinan Kayabd23fac2018-12-19 22:46:55 +0000785#ifdef CONFIG_PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400787acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Bob Moorec5f02312010-08-06 08:57:53 +0800788 u64 *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 int result, size;
Bob Moorec5f02312010-08-06 08:57:53 +0800791 u32 value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (!value)
794 return AE_BAD_PARAMETER;
795
796 switch (width) {
797 case 8:
798 size = 1;
799 break;
800 case 16:
801 size = 2;
802 break;
803 case 32:
804 size = 4;
805 break;
806 default:
807 return AE_ERROR;
808 }
809
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500810 result = raw_pci_read(pci_id->segment, pci_id->bus,
811 PCI_DEVFN(pci_id->device, pci_id->function),
Bob Moorec5f02312010-08-06 08:57:53 +0800812 reg, size, &value32);
813 *value = value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 return (result ? AE_ERROR : AE_OK);
816}
Len Brown4be44fc2005-08-05 00:44:28 -0400817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400819acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Lin Ming439913f2010-01-28 10:53:19 +0800820 u64 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 int result, size;
823
824 switch (width) {
825 case 8:
826 size = 1;
827 break;
828 case 16:
829 size = 2;
830 break;
831 case 32:
832 size = 4;
833 break;
834 default:
835 return AE_ERROR;
836 }
837
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500838 result = raw_pci_write(pci_id->segment, pci_id->bus,
839 PCI_DEVFN(pci_id->device, pci_id->function),
840 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 return (result ? AE_ERROR : AE_OK);
843}
Sinan Kayabd23fac2018-12-19 22:46:55 +0000844#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
David Howells65f27f32006-11-22 14:55:48 +0000846static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
David Howells65f27f32006-11-22 14:55:48 +0000848 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400849
Zhang Rui19cd8472008-08-28 10:05:06 +0800850 dpc->function(dpc->context);
851 kfree(dpc);
Zhang Rui19cd8472008-08-28 10:05:06 +0800852}
853
Lv Zheng836d083012015-12-03 10:43:14 +0800854#ifdef CONFIG_ACPI_DEBUGGER
855static struct acpi_debugger acpi_debugger;
856static bool acpi_debugger_initialized;
857
858int acpi_register_debugger(struct module *owner,
859 const struct acpi_debugger_ops *ops)
860{
861 int ret = 0;
862
863 mutex_lock(&acpi_debugger.lock);
864 if (acpi_debugger.ops) {
865 ret = -EBUSY;
866 goto err_lock;
867 }
868
869 acpi_debugger.owner = owner;
870 acpi_debugger.ops = ops;
871
872err_lock:
873 mutex_unlock(&acpi_debugger.lock);
874 return ret;
875}
876EXPORT_SYMBOL(acpi_register_debugger);
877
878void acpi_unregister_debugger(const struct acpi_debugger_ops *ops)
879{
880 mutex_lock(&acpi_debugger.lock);
881 if (ops == acpi_debugger.ops) {
882 acpi_debugger.ops = NULL;
883 acpi_debugger.owner = NULL;
884 }
885 mutex_unlock(&acpi_debugger.lock);
886}
887EXPORT_SYMBOL(acpi_unregister_debugger);
888
889int acpi_debugger_create_thread(acpi_osd_exec_callback function, void *context)
890{
891 int ret;
892 int (*func)(acpi_osd_exec_callback, void *);
893 struct module *owner;
894
895 if (!acpi_debugger_initialized)
896 return -ENODEV;
897 mutex_lock(&acpi_debugger.lock);
898 if (!acpi_debugger.ops) {
899 ret = -ENODEV;
900 goto err_lock;
901 }
902 if (!try_module_get(acpi_debugger.owner)) {
903 ret = -ENODEV;
904 goto err_lock;
905 }
906 func = acpi_debugger.ops->create_thread;
907 owner = acpi_debugger.owner;
908 mutex_unlock(&acpi_debugger.lock);
909
910 ret = func(function, context);
911
912 mutex_lock(&acpi_debugger.lock);
913 module_put(owner);
914err_lock:
915 mutex_unlock(&acpi_debugger.lock);
916 return ret;
917}
918
919ssize_t acpi_debugger_write_log(const char *msg)
920{
921 ssize_t ret;
922 ssize_t (*func)(const char *);
923 struct module *owner;
924
925 if (!acpi_debugger_initialized)
926 return -ENODEV;
927 mutex_lock(&acpi_debugger.lock);
928 if (!acpi_debugger.ops) {
929 ret = -ENODEV;
930 goto err_lock;
931 }
932 if (!try_module_get(acpi_debugger.owner)) {
933 ret = -ENODEV;
934 goto err_lock;
935 }
936 func = acpi_debugger.ops->write_log;
937 owner = acpi_debugger.owner;
938 mutex_unlock(&acpi_debugger.lock);
939
940 ret = func(msg);
941
942 mutex_lock(&acpi_debugger.lock);
943 module_put(owner);
944err_lock:
945 mutex_unlock(&acpi_debugger.lock);
946 return ret;
947}
948
949ssize_t acpi_debugger_read_cmd(char *buffer, size_t buffer_length)
950{
951 ssize_t ret;
952 ssize_t (*func)(char *, size_t);
953 struct module *owner;
954
955 if (!acpi_debugger_initialized)
956 return -ENODEV;
957 mutex_lock(&acpi_debugger.lock);
958 if (!acpi_debugger.ops) {
959 ret = -ENODEV;
960 goto err_lock;
961 }
962 if (!try_module_get(acpi_debugger.owner)) {
963 ret = -ENODEV;
964 goto err_lock;
965 }
966 func = acpi_debugger.ops->read_cmd;
967 owner = acpi_debugger.owner;
968 mutex_unlock(&acpi_debugger.lock);
969
970 ret = func(buffer, buffer_length);
971
972 mutex_lock(&acpi_debugger.lock);
973 module_put(owner);
974err_lock:
975 mutex_unlock(&acpi_debugger.lock);
976 return ret;
977}
978
979int acpi_debugger_wait_command_ready(void)
980{
981 int ret;
982 int (*func)(bool, char *, size_t);
983 struct module *owner;
984
985 if (!acpi_debugger_initialized)
986 return -ENODEV;
987 mutex_lock(&acpi_debugger.lock);
988 if (!acpi_debugger.ops) {
989 ret = -ENODEV;
990 goto err_lock;
991 }
992 if (!try_module_get(acpi_debugger.owner)) {
993 ret = -ENODEV;
994 goto err_lock;
995 }
996 func = acpi_debugger.ops->wait_command_ready;
997 owner = acpi_debugger.owner;
998 mutex_unlock(&acpi_debugger.lock);
999
1000 ret = func(acpi_gbl_method_executing,
1001 acpi_gbl_db_line_buf, ACPI_DB_LINE_BUFFER_SIZE);
1002
1003 mutex_lock(&acpi_debugger.lock);
1004 module_put(owner);
1005err_lock:
1006 mutex_unlock(&acpi_debugger.lock);
1007 return ret;
1008}
1009
1010int acpi_debugger_notify_command_complete(void)
1011{
1012 int ret;
1013 int (*func)(void);
1014 struct module *owner;
1015
1016 if (!acpi_debugger_initialized)
1017 return -ENODEV;
1018 mutex_lock(&acpi_debugger.lock);
1019 if (!acpi_debugger.ops) {
1020 ret = -ENODEV;
1021 goto err_lock;
1022 }
1023 if (!try_module_get(acpi_debugger.owner)) {
1024 ret = -ENODEV;
1025 goto err_lock;
1026 }
1027 func = acpi_debugger.ops->notify_command_complete;
1028 owner = acpi_debugger.owner;
1029 mutex_unlock(&acpi_debugger.lock);
1030
1031 ret = func();
1032
1033 mutex_lock(&acpi_debugger.lock);
1034 module_put(owner);
1035err_lock:
1036 mutex_unlock(&acpi_debugger.lock);
1037 return ret;
1038}
1039
1040int __init acpi_debugger_init(void)
1041{
1042 mutex_init(&acpi_debugger.lock);
1043 acpi_debugger_initialized = true;
1044 return 0;
1045}
1046#endif
1047
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -04001048/*******************************************************************************
1049 *
1050 * FUNCTION: acpi_os_execute
1051 *
1052 * PARAMETERS: Type - Type of the callback
1053 * Function - Function to be executed
1054 * Context - Function parameters
1055 *
1056 * RETURN: Status
1057 *
1058 * DESCRIPTION: Depending on type, either queues function for deferred execution or
1059 * immediately executes function on a separate thread.
1060 *
1061 ******************************************************************************/
1062
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001063acpi_status acpi_os_execute(acpi_execute_type type,
1064 acpi_osd_exec_callback function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Len Brown4be44fc2005-08-05 00:44:28 -04001066 acpi_status status = AE_OK;
1067 struct acpi_os_dpc *dpc;
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +03001068 struct workqueue_struct *queue;
Zhang Rui19cd8472008-08-28 10:05:06 +08001069 int ret;
Len Brown72945b22006-07-12 22:46:42 -04001070 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
1071 "Scheduling function [%p(%p)] for deferred execution.\n",
1072 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001074 if (type == OSL_DEBUGGER_MAIN_THREAD) {
Lv Zheng836d083012015-12-03 10:43:14 +08001075 ret = acpi_debugger_create_thread(function, context);
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001076 if (ret) {
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001077 pr_err("Kernel thread creation failed\n");
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001078 status = AE_ERROR;
1079 }
1080 goto out_thread;
1081 }
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /*
1084 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +00001085 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 * way that allows us to also free its memory inside the callee.
1087 * Because we may want to schedule several tasks with different
1088 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +00001089 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 */
Len Brown72945b22006-07-12 22:46:42 -04001091
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001092 dpc = kzalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (!dpc)
Lin Ming889c78b2008-12-31 09:23:57 +08001094 return AE_NO_MEMORY;
Linus Torvaldsb976fe12006-11-17 19:31:09 -08001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 dpc->function = function;
1097 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -08001098
Zhang Ruic02256b2009-06-23 10:20:29 +08001099 /*
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001100 * To prevent lockdep from complaining unnecessarily, make sure that
1101 * there is a different static lockdep key for each workqueue by using
1102 * INIT_WORK() for each of them separately.
Zhang Ruic02256b2009-06-23 10:20:29 +08001103 */
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001104 if (type == OSL_NOTIFY_HANDLER) {
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001105 queue = kacpi_notify_wq;
Zhang Ruibc736752010-03-22 15:48:54 +08001106 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001107 } else if (type == OSL_GPE_HANDLER) {
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001108 queue = kacpid_wq;
Zhang Ruibc736752010-03-22 15:48:54 +08001109 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001110 } else {
1111 pr_err("Unsupported os_execute type %d.\n", type);
1112 status = AE_ERROR;
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001113 }
Zhang Ruibc736752010-03-22 15:48:54 +08001114
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001115 if (ACPI_FAILURE(status))
1116 goto err_workqueue;
1117
Tejun Heo8fec62b2010-06-29 10:07:09 +02001118 /*
1119 * On some machines, a software-initiated SMI causes corruption unless
1120 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
1121 * typically it's done in GPE-related methods that are run via
1122 * workqueues, so we can avoid the known corruption cases by always
1123 * queueing on CPU 0.
1124 */
1125 ret = queue_work_on(0, queue, &dpc->work);
Zhang Rui19cd8472008-08-28 10:05:06 +08001126 if (!ret) {
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001127 pr_err("Unable to queue work\n");
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +03001128 status = AE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001130err_workqueue:
1131 if (ACPI_FAILURE(status))
1132 kfree(dpc);
1133out_thread:
Lin Ming889c78b2008-12-31 09:23:57 +08001134 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -04001136EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Lin Mingbd6f10a2012-05-22 16:43:49 +08001138void acpi_os_wait_events_complete(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Lv Zheng90253a72014-11-05 15:06:13 +08001140 /*
1141 * Make sure the GPE handler or the fixed event handler is not used
1142 * on another CPU after removal.
1143 */
Chen Yuefb1cf72015-10-25 01:02:36 +08001144 if (acpi_sci_irq_valid())
1145 synchronize_hardirq(acpi_sci_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 flush_workqueue(kacpid_wq);
Zhang Rui2f67a062008-04-29 02:34:42 -04001147 flush_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
Ronald Tschalär757c9682018-09-30 19:53:13 -07001149EXPORT_SYMBOL(acpi_os_wait_events_complete);
Len Brown4be44fc2005-08-05 00:44:28 -04001150
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001151struct acpi_hp_work {
1152 struct work_struct work;
Rafael J. Wysocki1e3bcb592014-03-03 00:40:38 +01001153 struct acpi_device *adev;
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001154 u32 src;
1155};
1156
1157static void acpi_hotplug_work_fn(struct work_struct *work)
1158{
1159 struct acpi_hp_work *hpw = container_of(work, struct acpi_hp_work, work);
1160
1161 acpi_os_wait_events_complete();
Rafael J. Wysocki1e3bcb592014-03-03 00:40:38 +01001162 acpi_device_hotplug(hpw->adev, hpw->src);
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001163 kfree(hpw);
1164}
1165
Rafael J. Wysocki1e3bcb592014-03-03 00:40:38 +01001166acpi_status acpi_hotplug_schedule(struct acpi_device *adev, u32 src)
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001167{
1168 struct acpi_hp_work *hpw;
1169
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001170 acpi_handle_debug(adev->handle,
1171 "Scheduling hotplug event %u for deferred handling\n",
1172 src);
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001173
1174 hpw = kmalloc(sizeof(*hpw), GFP_KERNEL);
1175 if (!hpw)
1176 return AE_NO_MEMORY;
1177
1178 INIT_WORK(&hpw->work, acpi_hotplug_work_fn);
Rafael J. Wysocki1e3bcb592014-03-03 00:40:38 +01001179 hpw->adev = adev;
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001180 hpw->src = src;
1181 /*
1182 * We can't run hotplug code in kacpid_wq/kacpid_notify_wq etc., because
1183 * the hotplug code may call driver .remove() functions, which may
1184 * invoke flush_scheduled_work()/acpi_os_wait_events_complete() to flush
1185 * these workqueues.
1186 */
1187 if (!queue_work(kacpi_hotplug_wq, &hpw->work)) {
1188 kfree(hpw);
1189 return AE_ERROR;
1190 }
1191 return AE_OK;
1192}
1193
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001194bool acpi_queue_hotplug_work(struct work_struct *work)
1195{
1196 return queue_work(kacpi_hotplug_wq, work);
1197}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001200acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Len Brown4be44fc2005-08-05 00:44:28 -04001202 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
jhbird.choi@samsung.com2d0acb42014-03-20 16:35:56 +09001204 sem = acpi_os_allocate_zeroed(sizeof(struct semaphore));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -04001206 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 sema_init(sem, initial_units);
1209
Len Brown4be44fc2005-08-05 00:44:28 -04001210 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Len Brown4be44fc2005-08-05 00:44:28 -04001212 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
1213 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Patrick Mocheld550d982006-06-27 00:41:40 -04001215 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218/*
1219 * TODO: A better way to delete semaphores? Linux doesn't have a
1220 * 'delete_semaphore()' function -- may result in an invalid
1221 * pointer dereference for non-synchronized consumers. Should
1222 * we at least check for blocked threads and signal/cancel them?
1223 */
1224
Len Brown4be44fc2005-08-05 00:44:28 -04001225acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Len Brown4be44fc2005-08-05 00:44:28 -04001227 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -04001230 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Len Brown4be44fc2005-08-05 00:44:28 -04001232 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001234 BUG_ON(!list_empty(&sem->wait_list));
Len Brown02438d82006-06-30 03:19:10 -04001235 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -04001236 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Patrick Mocheld550d982006-06-27 00:41:40 -04001238 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 * TODO: Support for units > 1?
1243 */
Len Brown4be44fc2005-08-05 00:44:28 -04001244acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Len Brown4be44fc2005-08-05 00:44:28 -04001246 acpi_status status = AE_OK;
1247 struct semaphore *sem = (struct semaphore *)handle;
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001248 long jiffies;
Len Brown4be44fc2005-08-05 00:44:28 -04001249 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Lv Zheng7901a052015-08-05 16:23:51 +08001251 if (!acpi_os_initialized)
1252 return AE_OK;
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001255 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -04001258 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Len Brown4be44fc2005-08-05 00:44:28 -04001260 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
1261 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001263 if (timeout == ACPI_WAIT_FOREVER)
1264 jiffies = MAX_SCHEDULE_TIMEOUT;
1265 else
1266 jiffies = msecs_to_jiffies(timeout);
Al Stonecad15252013-12-04 12:59:11 -07001267
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001268 ret = down_timeout(sem, jiffies);
1269 if (ret)
1270 status = AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -04001273 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -04001274 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -04001275 handle, units, timeout,
1276 acpi_format_exception(status)));
1277 } else {
1278 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -04001279 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001280 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
1282
Patrick Mocheld550d982006-06-27 00:41:40 -04001283 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286/*
1287 * TODO: Support for units > 1?
1288 */
Len Brown4be44fc2005-08-05 00:44:28 -04001289acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
Len Brown4be44fc2005-08-05 00:44:28 -04001291 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Lv Zheng7901a052015-08-05 16:23:51 +08001293 if (!acpi_os_initialized)
1294 return AE_OK;
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001297 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -04001300 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Len Brown4be44fc2005-08-05 00:44:28 -04001302 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1303 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 up(sem);
1306
Patrick Mocheld550d982006-06-27 00:41:40 -04001307 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
Len Brown4be44fc2005-08-05 00:44:28 -04001309
Lv Zheng4d946f72015-10-19 10:25:56 +08001310acpi_status acpi_os_get_line(char *buffer, u32 buffer_length, u32 *bytes_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312#ifdef ENABLE_DEBUGGER
1313 if (acpi_in_debugger) {
1314 u32 chars;
1315
Lv Zheng4d946f72015-10-19 10:25:56 +08001316 kdb_read(buffer, buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 /* remove the CR kdb includes */
1319 chars = strlen(buffer) - 1;
1320 buffer[chars] = '\0';
1321 }
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001322#else
1323 int ret;
1324
Lv Zheng836d083012015-12-03 10:43:14 +08001325 ret = acpi_debugger_read_cmd(buffer, buffer_length);
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001326 if (ret < 0)
1327 return AE_ERROR;
1328 if (bytes_read)
1329 *bytes_read = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330#endif
1331
Lv Zheng4d946f72015-10-19 10:25:56 +08001332 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
Lv Zheng836d083012015-12-03 10:43:14 +08001334EXPORT_SYMBOL(acpi_os_get_line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001336acpi_status acpi_os_wait_command_ready(void)
1337{
1338 int ret;
1339
Lv Zheng836d083012015-12-03 10:43:14 +08001340 ret = acpi_debugger_wait_command_ready();
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001341 if (ret < 0)
1342 return AE_ERROR;
1343 return AE_OK;
1344}
1345
1346acpi_status acpi_os_notify_command_complete(void)
1347{
1348 int ret;
1349
Lv Zheng836d083012015-12-03 10:43:14 +08001350 ret = acpi_debugger_notify_command_complete();
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001351 if (ret < 0)
1352 return AE_ERROR;
1353 return AE_OK;
1354}
1355
Len Brown4be44fc2005-08-05 00:44:28 -04001356acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Len Brown4be44fc2005-08-05 00:44:28 -04001358 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 case ACPI_SIGNAL_FATAL:
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001360 pr_err("Fatal opcode executed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 break;
1362 case ACPI_SIGNAL_BREAKPOINT:
1363 /*
1364 * AML Breakpoint
1365 * ACPI spec. says to treat it as a NOP unless
1366 * you are debugging. So if/when we integrate
1367 * AML debugger into the kernel debugger its
1368 * hook will go here. But until then it is
1369 * not useful to print anything on breakpoints.
1370 */
1371 break;
1372 default:
1373 break;
1374 }
1375
1376 return AE_OK;
1377}
Len Brown4be44fc2005-08-05 00:44:28 -04001378
Len Brown4be44fc2005-08-05 00:44:28 -04001379static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -04001382 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 if (!str || !*str)
1385 return 0;
1386
Dan Carpenter5e2be4e2013-10-18 12:01:43 +03001387 for (; count-- && *str; str++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (isalnum(*str) || *str == ' ' || *str == ':')
1389 *p++ = *str;
1390 else if (*str == '\'' || *str == '"')
1391 continue;
1392 else
1393 break;
1394 }
1395 *p = 0;
1396
1397 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
1401__setup("acpi_os_name=", acpi_os_name_setup);
1402
Bob Moore22b5afc2014-03-24 14:49:00 +08001403/*
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001404 * Disable the auto-serialization of named objects creation methods.
Bob Moore22b5afc2014-03-24 14:49:00 +08001405 *
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001406 * This feature is enabled by default. It marks the AML control methods
Bob Moore22b5afc2014-03-24 14:49:00 +08001407 * that contain the opcodes to create named objects as "Serialized".
1408 */
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001409static int __init acpi_no_auto_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001411 acpi_gbl_auto_serialize_methods = FALSE;
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001412 pr_info("Auto-serialization disabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 return 1;
1415}
1416
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001417__setup("acpi_no_auto_serialize", acpi_no_auto_serialize_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Thomas Renningerdf92e692008-02-04 23:31:22 -08001419/* Check of resource interference between native drivers and ACPI
1420 * OperationRegions (SystemIO and System Memory only).
1421 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1422 * in arbitrary AML code and can interfere with legacy drivers.
1423 * acpi_enforce_resources= can be set to:
1424 *
Luca Tettamanti7e905602009-03-30 00:01:27 +02001425 * - strict (default) (2)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001426 * -> further driver trying to access the resources will not load
Luca Tettamanti7e905602009-03-30 00:01:27 +02001427 * - lax (1)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001428 * -> further driver trying to access the resources will load, but you
1429 * get a system message that something might go wrong...
1430 *
1431 * - no (0)
1432 * -> ACPI Operation Region resources will not be registered
1433 *
1434 */
1435#define ENFORCE_RESOURCES_STRICT 2
1436#define ENFORCE_RESOURCES_LAX 1
1437#define ENFORCE_RESOURCES_NO 0
1438
Luca Tettamanti7e905602009-03-30 00:01:27 +02001439static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001440
1441static int __init acpi_enforce_resources_setup(char *str)
1442{
1443 if (str == NULL || *str == '\0')
1444 return 0;
1445
1446 if (!strcmp("strict", str))
1447 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1448 else if (!strcmp("lax", str))
1449 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1450 else if (!strcmp("no", str))
1451 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1452
1453 return 1;
1454}
1455
1456__setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1457
1458/* Check for resource conflicts between ACPI OperationRegions and native
1459 * drivers */
Jean Delvare876fba42009-11-11 15:22:15 +01001460int acpi_check_resource_conflict(const struct resource *res)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001461{
Lin Mingf654c0f2012-01-12 13:10:32 +08001462 acpi_adr_space_type space_id;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001463
1464 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1465 return 0;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001466
Lin Mingf654c0f2012-01-12 13:10:32 +08001467 if (res->flags & IORESOURCE_IO)
1468 space_id = ACPI_ADR_SPACE_SYSTEM_IO;
Rafael J. Wysocki505ca2f2021-02-09 15:59:04 +01001469 else if (res->flags & IORESOURCE_MEM)
Lin Mingf654c0f2012-01-12 13:10:32 +08001470 space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
Rafael J. Wysocki505ca2f2021-02-09 15:59:04 +01001471 else
1472 return 0;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001473
Rafael J. Wysocki505ca2f2021-02-09 15:59:04 +01001474 if (!acpi_check_address_range(space_id, res->start, resource_size(res), 1))
1475 return 0;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001476
Rafael J. Wysocki505ca2f2021-02-09 15:59:04 +01001477 pr_info("Resource conflict; ACPI support missing from driver?\n");
1478
1479 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1480 return -EBUSY;
1481
1482 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1483 pr_notice("Resource conflict: System may be unstable or behave erratically\n");
1484
Thomas Renningerdf92e692008-02-04 23:31:22 -08001485 return 0;
1486}
Thomas Renninger443dea72008-02-04 23:31:23 -08001487EXPORT_SYMBOL(acpi_check_resource_conflict);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001488
1489int acpi_check_region(resource_size_t start, resource_size_t n,
1490 const char *name)
1491{
Zhen Lei01c3d592021-06-01 14:50:32 +08001492 struct resource res = DEFINE_RES_IO_NAMED(start, n, name);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001493
1494 return acpi_check_resource_conflict(&res);
1495}
1496EXPORT_SYMBOL(acpi_check_region);
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498/*
Jean Delvare70dd6be2010-05-27 19:58:37 +02001499 * Let drivers know whether the resource checks are effective
1500 */
1501int acpi_resources_are_enforced(void)
1502{
1503 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1504}
1505EXPORT_SYMBOL(acpi_resources_are_enforced);
1506
1507/*
Lin Ming9f63b882011-03-23 17:26:34 +08001508 * Deallocate the memory for a spinlock.
1509 */
1510void acpi_os_delete_lock(acpi_spinlock handle)
1511{
1512 ACPI_FREE(handle);
1513}
1514
1515/*
Robert Moore73459f72005-06-24 00:00:00 -04001516 * Acquire a spinlock.
1517 *
1518 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001519 */
1520
Bob Moore967440e32006-06-23 17:04:00 -04001521acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Jules Irenge2288eba2020-02-23 23:17:03 +00001522 __acquires(lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001523{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001524 acpi_cpu_flags flags;
Bob Moore967440e32006-06-23 17:04:00 -04001525 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001526 return flags;
1527}
1528
1529/*
1530 * Release a spinlock. See above.
1531 */
1532
Bob Moore967440e32006-06-23 17:04:00 -04001533void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Jules Irenge2288eba2020-02-23 23:17:03 +00001534 __releases(lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001535{
Bob Moore967440e32006-06-23 17:04:00 -04001536 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001537}
1538
Robert Moore73459f72005-06-24 00:00:00 -04001539#ifndef ACPI_USE_LOCAL_CACHE
1540
1541/*******************************************************************************
1542 *
1543 * FUNCTION: acpi_os_create_cache
1544 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001545 * PARAMETERS: name - Ascii name for the cache
1546 * size - Size of each cached object
1547 * depth - Maximum depth of the cache (in objects) <ignored>
1548 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001549 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001550 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001551 *
1552 * DESCRIPTION: Create a cache object
1553 *
1554 ******************************************************************************/
1555
1556acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001557acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001558{
Paul Mundt20c2df82007-07-20 10:11:58 +09001559 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001560 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001561 return AE_ERROR;
1562 else
1563 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001564}
1565
1566/*******************************************************************************
1567 *
1568 * FUNCTION: acpi_os_purge_cache
1569 *
1570 * PARAMETERS: Cache - Handle to cache object
1571 *
1572 * RETURN: Status
1573 *
1574 * DESCRIPTION: Free all objects within the requested cache.
1575 *
1576 ******************************************************************************/
1577
Len Brown4be44fc2005-08-05 00:44:28 -04001578acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001579{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001580 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001581 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001582}
1583
1584/*******************************************************************************
1585 *
1586 * FUNCTION: acpi_os_delete_cache
1587 *
1588 * PARAMETERS: Cache - Handle to cache object
1589 *
1590 * RETURN: Status
1591 *
1592 * DESCRIPTION: Free all objects within the requested cache and delete the
1593 * cache object.
1594 *
1595 ******************************************************************************/
1596
Len Brown4be44fc2005-08-05 00:44:28 -04001597acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001598{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001599 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001600 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001601}
1602
1603/*******************************************************************************
1604 *
1605 * FUNCTION: acpi_os_release_object
1606 *
1607 * PARAMETERS: Cache - Handle to cache object
1608 * Object - The object to be released
1609 *
1610 * RETURN: None
1611 *
1612 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1613 * the object is deleted.
1614 *
1615 ******************************************************************************/
1616
Len Brown4be44fc2005-08-05 00:44:28 -04001617acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001618{
Len Brown4be44fc2005-08-05 00:44:28 -04001619 kmem_cache_free(cache, object);
1620 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001621}
Robert Moore73459f72005-06-24 00:00:00 -04001622#endif
Myron Stowed362eda2010-10-21 14:24:04 -06001623
Lv Zhenga94e88c2014-04-04 12:39:11 +08001624static int __init acpi_no_static_ssdt_setup(char *s)
Lv Zhengb75dd292013-06-08 00:58:48 +00001625{
Lv Zhenga94e88c2014-04-04 12:39:11 +08001626 acpi_gbl_disable_ssdt_table_install = TRUE;
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001627 pr_info("Static SSDT installation disabled\n");
Lv Zhengb75dd292013-06-08 00:58:48 +00001628
Lv Zhenga94e88c2014-04-04 12:39:11 +08001629 return 0;
Lv Zhengb75dd292013-06-08 00:58:48 +00001630}
1631
Lv Zhenga94e88c2014-04-04 12:39:11 +08001632early_param("acpi_no_static_ssdt", acpi_no_static_ssdt_setup);
Lv Zhengb75dd292013-06-08 00:58:48 +00001633
Lv Zheng4dde5072014-02-11 11:01:52 +08001634static int __init acpi_disable_return_repair(char *s)
1635{
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001636 pr_notice("Predefined validation mechanism disabled\n");
Lv Zheng4dde5072014-02-11 11:01:52 +08001637 acpi_gbl_disable_auto_repair = TRUE;
1638
1639 return 1;
1640}
1641
1642__setup("acpica_no_return_repair", acpi_disable_return_repair);
1643
Myron Stowed362eda2010-10-21 14:24:04 -06001644acpi_status __init acpi_os_initialize(void)
1645{
1646 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1647 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
Rafael J. Wysocki85f94022020-09-11 14:59:42 +02001648
1649 acpi_gbl_xgpe0_block_logical_address =
1650 (unsigned long)acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1651 acpi_gbl_xgpe1_block_logical_address =
1652 (unsigned long)acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1653
Randy Wrighta4714a82014-06-04 08:55:59 -07001654 if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) {
1655 /*
1656 * Use acpi_os_map_generic_address to pre-map the reset
1657 * register if it's in system memory.
1658 */
Rafael J. Wysocki69155642020-09-11 14:59:35 +02001659 void *rv;
Randy Wrighta4714a82014-06-04 08:55:59 -07001660
1661 rv = acpi_os_map_generic_address(&acpi_gbl_FADT.reset_register);
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001662 pr_debug("%s: Reset register mapping %s\n", __func__,
Rafael J. Wysocki69155642020-09-11 14:59:35 +02001663 rv ? "successful" : "failed");
Randy Wrighta4714a82014-06-04 08:55:59 -07001664 }
Lv Zheng7901a052015-08-05 16:23:51 +08001665 acpi_os_initialized = true;
Myron Stowed362eda2010-10-21 14:24:04 -06001666
1667 return AE_OK;
1668}
1669
Zhang Rui32d47ee2010-12-08 10:40:36 +08001670acpi_status __init acpi_os_initialize1(void)
Myron Stowed362eda2010-10-21 14:24:04 -06001671{
Tejun Heo44d25882011-02-01 11:42:42 +01001672 kacpid_wq = alloc_workqueue("kacpid", 0, 1);
1673 kacpi_notify_wq = alloc_workqueue("kacpi_notify", 0, 1);
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001674 kacpi_hotplug_wq = alloc_ordered_workqueue("kacpi_hotplug", 0);
Myron Stowed362eda2010-10-21 14:24:04 -06001675 BUG_ON(!kacpid_wq);
1676 BUG_ON(!kacpi_notify_wq);
1677 BUG_ON(!kacpi_hotplug_wq);
Lv Zhenge5f660e2016-05-03 16:49:01 +08001678 acpi_osi_init();
Myron Stowed362eda2010-10-21 14:24:04 -06001679 return AE_OK;
1680}
1681
1682acpi_status acpi_os_terminate(void)
1683{
1684 if (acpi_irq_handler) {
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +01001685 acpi_os_remove_interrupt_handler(acpi_gbl_FADT.sci_interrupt,
Myron Stowed362eda2010-10-21 14:24:04 -06001686 acpi_irq_handler);
1687 }
1688
1689 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1690 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
Rafael J. Wysocki85f94022020-09-11 14:59:42 +02001691 acpi_gbl_xgpe0_block_logical_address = 0UL;
1692 acpi_gbl_xgpe1_block_logical_address = 0UL;
1693
Myron Stowed362eda2010-10-21 14:24:04 -06001694 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1695 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
Rafael J. Wysocki85f94022020-09-11 14:59:42 +02001696
Randy Wrighta4714a82014-06-04 08:55:59 -07001697 if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER)
1698 acpi_os_unmap_generic_address(&acpi_gbl_FADT.reset_register);
Myron Stowed362eda2010-10-21 14:24:04 -06001699
1700 destroy_workqueue(kacpid_wq);
1701 destroy_workqueue(kacpi_notify_wq);
1702 destroy_workqueue(kacpi_hotplug_wq);
1703
1704 return AE_OK;
1705}
Tang Liang09f98a82011-12-09 10:05:54 +08001706
1707acpi_status acpi_os_prepare_sleep(u8 sleep_state, u32 pm1a_control,
1708 u32 pm1b_control)
1709{
1710 int rc = 0;
1711 if (__acpi_os_prepare_sleep)
1712 rc = __acpi_os_prepare_sleep(sleep_state,
1713 pm1a_control, pm1b_control);
1714 if (rc < 0)
1715 return AE_ERROR;
1716 else if (rc > 0)
Lv Zheng0fc5e8f2016-12-28 15:28:49 +08001717 return AE_CTRL_TERMINATE;
Tang Liang09f98a82011-12-09 10:05:54 +08001718
1719 return AE_OK;
1720}
1721
1722void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
1723 u32 pm1a_ctrl, u32 pm1b_ctrl))
1724{
1725 __acpi_os_prepare_sleep = func;
1726}
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001727
Lv Zheng0fc5e8f2016-12-28 15:28:49 +08001728#if (ACPI_REDUCED_HARDWARE)
Ben Guthrod6b47b12013-07-30 08:24:52 -04001729acpi_status acpi_os_prepare_extended_sleep(u8 sleep_state, u32 val_a,
1730 u32 val_b)
1731{
1732 int rc = 0;
1733 if (__acpi_os_prepare_extended_sleep)
1734 rc = __acpi_os_prepare_extended_sleep(sleep_state,
1735 val_a, val_b);
1736 if (rc < 0)
1737 return AE_ERROR;
1738 else if (rc > 0)
Lv Zheng0fc5e8f2016-12-28 15:28:49 +08001739 return AE_CTRL_TERMINATE;
Ben Guthrod6b47b12013-07-30 08:24:52 -04001740
1741 return AE_OK;
1742}
Lv Zheng0fc5e8f2016-12-28 15:28:49 +08001743#else
1744acpi_status acpi_os_prepare_extended_sleep(u8 sleep_state, u32 val_a,
1745 u32 val_b)
1746{
1747 return AE_OK;
1748}
1749#endif
Ben Guthrod6b47b12013-07-30 08:24:52 -04001750
1751void acpi_os_set_prepare_extended_sleep(int (*func)(u8 sleep_state,
1752 u32 val_a, u32 val_b))
1753{
1754 __acpi_os_prepare_extended_sleep = func;
1755}
Lv Zheng0fc5e8f2016-12-28 15:28:49 +08001756
1757acpi_status acpi_os_enter_sleep(u8 sleep_state,
1758 u32 reg_a_value, u32 reg_b_value)
1759{
1760 acpi_status status;
1761
1762 if (acpi_gbl_reduced_hardware)
1763 status = acpi_os_prepare_extended_sleep(sleep_state,
1764 reg_a_value,
1765 reg_b_value);
1766 else
1767 status = acpi_os_prepare_sleep(sleep_state,
1768 reg_a_value, reg_b_value);
1769 return status;
1770}