blob: 45c5c0e45e3327a3c6a1fe1acf81f2b7c709f379 [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"
39#include "acpica/acnamesp.h"
Lv Zheng1129c922013-07-23 16:11:55 +080040#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +010042/* Definitions for ACPI_DEBUG_PRINT() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define _COMPONENT ACPI_OS_SERVICES
Len Brownf52fd662007-02-12 22:42:12 -050044ACPI_MODULE_NAME("osl");
Hanjun Guo07070e12014-03-13 12:47:39 +080045
Len Brown4be44fc2005-08-05 00:44:28 -040046struct acpi_os_dpc {
47 acpi_osd_exec_callback function;
48 void *context;
David Howells65f27f32006-11-22 14:55:48 +000049 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#ifdef ENABLE_DEBUGGER
53#include <linux/kdb.h>
54
55/* stuff for debugger support */
56int acpi_in_debugger;
57EXPORT_SYMBOL(acpi_in_debugger);
Len Brown4be44fc2005-08-05 00:44:28 -040058#endif /*ENABLE_DEBUGGER */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Tang Liang09f98a82011-12-09 10:05:54 +080060static int (*__acpi_os_prepare_sleep)(u8 sleep_state, u32 pm1a_ctrl,
61 u32 pm1b_ctrl);
Ben Guthrod6b47b12013-07-30 08:24:52 -040062static int (*__acpi_os_prepare_extended_sleep)(u8 sleep_state, u32 val_a,
63 u32 val_b);
Tang Liang09f98a82011-12-09 10:05:54 +080064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static acpi_osd_handler acpi_irq_handler;
66static void *acpi_irq_context;
67static struct workqueue_struct *kacpid_wq;
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -040068static struct workqueue_struct *kacpi_notify_wq;
Yinghai Lu92d8aff2013-01-21 13:20:47 -080069static struct workqueue_struct *kacpi_hotplug_wq;
Lv Zheng7901a052015-08-05 16:23:51 +080070static bool acpi_os_initialized;
Chen Yu49e4b8432015-10-25 01:02:19 +080071unsigned int acpi_sci_irq = INVALID_ACPI_IRQ;
Lv Zheng8d3523f2016-12-14 15:04:46 +080072bool acpi_permanent_mmap = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Myron Stowe620242a2010-10-21 14:23:53 -060074/*
75 * This list of permanent mappings is for memory that may be accessed from
76 * interrupt context, where we can't do the ioremap().
77 */
78struct acpi_ioremap {
79 struct list_head list;
80 void __iomem *virt;
81 acpi_physical_address phys;
82 acpi_size size;
Rafael J. Wysocki17576592020-07-02 13:19:12 +020083 union {
84 unsigned long refcount;
85 struct rcu_work rwork;
86 } track;
Myron Stowe620242a2010-10-21 14:23:53 -060087};
88
89static LIST_HEAD(acpi_ioremaps);
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +010090static DEFINE_MUTEX(acpi_ioremap_lock);
Joel Fernandes (Google)bee6f872019-07-16 18:12:28 -040091#define acpi_ioremap_lock_held() lock_is_held(&acpi_ioremap_lock.dep_map)
Myron Stowe620242a2010-10-21 14:23:53 -060092
Myron Stowebc9ffce2011-11-07 16:23:27 -070093static void __init acpi_request_region (struct acpi_generic_address *gas,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -070094 unsigned int length, char *desc)
95{
Myron Stowebc9ffce2011-11-07 16:23:27 -070096 u64 addr;
97
98 /* Handle possible alignment issues */
99 memcpy(&addr, &gas->address, sizeof(addr));
100 if (!addr || !length)
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700101 return;
102
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200103 /* Resources are never freed */
104 if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
105 request_region(addr, length, desc);
106 else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
107 request_mem_region(addr, length, desc);
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700108}
109
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200110static int __init acpi_reserve_resources(void)
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700111{
Len Browneee3c852007-02-03 01:38:16 -0500112 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700113 "ACPI PM1a_EVT_BLK");
114
Len Browneee3c852007-02-03 01:38:16 -0500115 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700116 "ACPI PM1b_EVT_BLK");
117
Len Browneee3c852007-02-03 01:38:16 -0500118 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700119 "ACPI PM1a_CNT_BLK");
120
Len Browneee3c852007-02-03 01:38:16 -0500121 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700122 "ACPI PM1b_CNT_BLK");
123
Len Browneee3c852007-02-03 01:38:16 -0500124 if (acpi_gbl_FADT.pm_timer_length == 4)
125 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700126
Len Browneee3c852007-02-03 01:38:16 -0500127 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700128 "ACPI PM2_CNT_BLK");
129
130 /* Length of GPE blocks must be a non-negative multiple of 2 */
131
Len Browneee3c852007-02-03 01:38:16 -0500132 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
133 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
134 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700135
Len Browneee3c852007-02-03 01:38:16 -0500136 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
137 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
138 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200139
140 return 0;
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700141}
Rafael J. Wysocki02941122015-07-04 03:09:03 +0200142fs_initcall_sync(acpi_reserve_resources);
Bjorn Helgaas9a47cdb2007-01-18 16:42:55 -0700143
Len Brown4be44fc2005-08-05 00:44:28 -0400144void acpi_os_printf(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 va_list args;
147 va_start(args, fmt);
148 acpi_os_vprintf(fmt, args);
149 va_end(args);
150}
Lv Zheng836d083012015-12-03 10:43:14 +0800151EXPORT_SYMBOL(acpi_os_printf);
Len Brown4be44fc2005-08-05 00:44:28 -0400152
Len Brown4be44fc2005-08-05 00:44:28 -0400153void acpi_os_vprintf(const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 static char buffer[512];
Len Brown4be44fc2005-08-05 00:44:28 -0400156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 vsprintf(buffer, fmt, args);
158
159#ifdef ENABLE_DEBUGGER
160 if (acpi_in_debugger) {
161 kdb_printf("%s", buffer);
162 } else {
Joe Perchesabc4b9a2016-10-12 11:50:34 -0700163 if (printk_get_level(buffer))
164 printk("%s", buffer);
165 else
166 printk(KERN_CONT "%s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168#else
Joe Perchesabc4b9a2016-10-12 11:50:34 -0700169 if (acpi_debugger_write_log(buffer) < 0) {
170 if (printk_get_level(buffer))
171 printk("%s", buffer);
172 else
173 printk(KERN_CONT "%s", buffer);
174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#endif
176}
177
Takao Indoh4996c022011-07-14 18:05:21 -0400178#ifdef CONFIG_KEXEC
179static unsigned long acpi_rsdp;
180static int __init setup_acpi_rsdp(char *arg)
181{
Andy Shevchenko5dcb9ca2016-12-02 19:42:46 +0200182 return kstrtoul(arg, 16, &acpi_rsdp);
Takao Indoh4996c022011-07-14 18:05:21 -0400183}
184early_param("acpi_rsdp", setup_acpi_rsdp);
185#endif
186
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300187acpi_physical_address __init acpi_os_get_root_pointer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Juergen Grossdfc93272018-02-19 11:09:04 +0100189 acpi_physical_address pa;
Andy Shevchenko2fb65f02016-12-02 19:42:47 +0200190
Takao Indoh4996c022011-07-14 18:05:21 -0400191#ifdef CONFIG_KEXEC
Josh Boyer41fa1ee2019-08-19 17:17:51 -0700192 /*
193 * We may have been provided with an RSDP on the command line,
194 * but if a malicious user has done so they may be pointing us
195 * at modified ACPI tables that could alter kernel behaviour -
196 * so, we check the lockdown status before making use of
197 * it. If we trust it then also stash it in an architecture
198 * specific location (if appropriate) so it can be carried
199 * over further kexec()s.
200 */
201 if (acpi_rsdp && !security_locked_down(LOCKDOWN_ACPI_TABLES)) {
202 acpi_arch_set_root_pointer(acpi_rsdp);
Takao Indoh4996c022011-07-14 18:05:21 -0400203 return acpi_rsdp;
Josh Boyer41fa1ee2019-08-19 17:17:51 -0700204 }
Takao Indoh4996c022011-07-14 18:05:21 -0400205#endif
Juergen Grossdfc93272018-02-19 11:09:04 +0100206 pa = acpi_arch_get_root_pointer();
207 if (pa)
208 return pa;
Takao Indoh4996c022011-07-14 18:05:21 -0400209
Matt Fleming83e68182012-11-14 09:42:35 +0000210 if (efi_enabled(EFI_CONFIG_TABLES)) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800211 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300212 return efi.acpi20;
Andy Shevchenko2fb65f02016-12-02 19:42:47 +0200213 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300214 return efi.acpi;
Hanjun Guo2e670de2021-06-02 16:54:29 +0800215 pr_err("System description tables not found\n");
Graeme Gregory8a1664b2014-07-18 18:02:52 +0800216 } else if (IS_ENABLED(CONFIG_ACPI_LEGACY_TABLES_LOOKUP)) {
Len Brown239665a2007-11-23 20:08:02 -0500217 acpi_find_root_pointer(&pa);
Len Brown239665a2007-11-23 20:08:02 -0500218 }
Graeme Gregory8a1664b2014-07-18 18:02:52 +0800219
Andy Shevchenko2fb65f02016-12-02 19:42:47 +0200220 return pa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600223/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe4a3cba52010-10-21 14:24:14 -0600224static struct acpi_ioremap *
225acpi_map_lookup(acpi_physical_address phys, acpi_size size)
Myron Stowe620242a2010-10-21 14:23:53 -0600226{
227 struct acpi_ioremap *map;
228
Joel Fernandes (Google)bee6f872019-07-16 18:12:28 -0400229 list_for_each_entry_rcu(map, &acpi_ioremaps, list, acpi_ioremap_lock_held())
Myron Stowe620242a2010-10-21 14:23:53 -0600230 if (map->phys <= phys &&
231 phys + size <= map->phys + map->size)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600232 return map;
233
234 return NULL;
235}
236
237/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
238static void __iomem *
239acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
240{
241 struct acpi_ioremap *map;
242
243 map = acpi_map_lookup(phys, size);
244 if (map)
245 return map->virt + (phys - map->phys);
Myron Stowe620242a2010-10-21 14:23:53 -0600246
247 return NULL;
248}
249
Rafael J. Wysocki13606a22011-02-08 23:38:25 +0100250void __iomem *acpi_os_get_iomem(acpi_physical_address phys, unsigned int size)
251{
252 struct acpi_ioremap *map;
253 void __iomem *virt = NULL;
254
255 mutex_lock(&acpi_ioremap_lock);
256 map = acpi_map_lookup(phys, size);
257 if (map) {
258 virt = map->virt + (phys - map->phys);
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200259 map->track.refcount++;
Rafael J. Wysocki13606a22011-02-08 23:38:25 +0100260 }
261 mutex_unlock(&acpi_ioremap_lock);
262 return virt;
263}
264EXPORT_SYMBOL_GPL(acpi_os_get_iomem);
265
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600266/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
Myron Stowe620242a2010-10-21 14:23:53 -0600267static struct acpi_ioremap *
268acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
269{
270 struct acpi_ioremap *map;
271
Joel Fernandes (Google)bee6f872019-07-16 18:12:28 -0400272 list_for_each_entry_rcu(map, &acpi_ioremaps, list, acpi_ioremap_lock_held())
Myron Stowe4a3cba52010-10-21 14:24:14 -0600273 if (map->virt <= virt &&
274 virt + size <= map->virt + map->size)
Myron Stowe620242a2010-10-21 14:23:53 -0600275 return map;
276
277 return NULL;
278}
279
Graeme Gregoryaafc65c2015-03-24 14:02:35 +0000280#if defined(CONFIG_IA64) || defined(CONFIG_ARM64)
Myron Stoweba242d52012-01-20 19:13:30 -0700281/* ioremap will take care of cache attributes */
282#define should_use_kmap(pfn) 0
Graeme Gregoryaafc65c2015-03-24 14:02:35 +0000283#else
284#define should_use_kmap(pfn) page_is_ram(pfn)
Myron Stoweba242d52012-01-20 19:13:30 -0700285#endif
286
287static void __iomem *acpi_map(acpi_physical_address pg_off, unsigned long pg_sz)
288{
289 unsigned long pfn;
290
291 pfn = pg_off >> PAGE_SHIFT;
292 if (should_use_kmap(pfn)) {
293 if (pg_sz > PAGE_SIZE)
294 return NULL;
295 return (void __iomem __force *)kmap(pfn_to_page(pfn));
296 } else
297 return acpi_os_ioremap(pg_off, pg_sz);
298}
299
300static void acpi_unmap(acpi_physical_address pg_off, void __iomem *vaddr)
301{
302 unsigned long pfn;
303
304 pfn = pg_off >> PAGE_SHIFT;
Jan Beuliche2526752012-02-24 11:41:53 +0000305 if (should_use_kmap(pfn))
Myron Stoweba242d52012-01-20 19:13:30 -0700306 kunmap(pfn_to_page(pfn));
307 else
308 iounmap(vaddr);
309}
310
Rafael J. Wysocki9d128ed2016-01-02 03:10:29 +0100311/**
312 * acpi_os_map_iomem - Get a virtual address for a given physical address range.
313 * @phys: Start of the physical address range to map.
314 * @size: Size of the physical address range to map.
315 *
316 * Look up the given physical address range in the list of existing ACPI memory
317 * mappings. If found, get a reference to it and return a pointer to it (its
318 * virtual address). If not found, map it, add it to that list and return a
319 * pointer to it.
320 *
Lv Zheng8d3523f2016-12-14 15:04:46 +0800321 * During early init (when acpi_permanent_mmap has not been set yet) this
Rafael J. Wysocki9d128ed2016-01-02 03:10:29 +0100322 * routine simply calls __acpi_map_table() to get the job done.
323 */
Qian Cai9fe51602019-06-03 16:28:35 -0400324void __iomem __ref
325*acpi_os_map_iomem(acpi_physical_address phys, acpi_size size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100327 struct acpi_ioremap *map;
Myron Stowe620242a2010-10-21 14:23:53 -0600328 void __iomem *virt;
Rafael J. Wysocki2d6d9fd2011-01-19 22:27:14 +0100329 acpi_physical_address pg_off;
330 acpi_size pg_sz;
Myron Stowe620242a2010-10-21 14:23:53 -0600331
Bjorn Helgaas9f4fd612006-03-26 01:37:10 -0800332 if (phys > ULONG_MAX) {
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +0100333 pr_err("Cannot map memory that high: 0x%llx\n", phys);
Randy Dunlap70c08462007-02-13 16:11:36 -0800334 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Myron Stowe620242a2010-10-21 14:23:53 -0600336
Lv Zheng8d3523f2016-12-14 15:04:46 +0800337 if (!acpi_permanent_mmap)
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300338 return __acpi_map_table((unsigned long)phys, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600339
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100340 mutex_lock(&acpi_ioremap_lock);
341 /* Check if there's a suitable mapping already. */
342 map = acpi_map_lookup(phys, size);
343 if (map) {
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200344 map->track.refcount++;
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100345 goto out;
346 }
347
Myron Stowe620242a2010-10-21 14:23:53 -0600348 map = kzalloc(sizeof(*map), GFP_KERNEL);
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100349 if (!map) {
350 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600351 return NULL;
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100352 }
Myron Stowe620242a2010-10-21 14:23:53 -0600353
Myron Stowe4a3cba52010-10-21 14:24:14 -0600354 pg_off = round_down(phys, PAGE_SIZE);
355 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
Ard Biesheuvel17189d912020-08-18 11:13:53 +0200356 virt = acpi_map(phys, size);
Myron Stowe620242a2010-10-21 14:23:53 -0600357 if (!virt) {
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100358 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe620242a2010-10-21 14:23:53 -0600359 kfree(map);
360 return NULL;
361 }
362
363 INIT_LIST_HEAD(&map->list);
Ard Biesheuvel17189d912020-08-18 11:13:53 +0200364 map->virt = (void __iomem __force *)((unsigned long)virt & PAGE_MASK);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600365 map->phys = pg_off;
366 map->size = pg_sz;
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200367 map->track.refcount = 1;
Myron Stowe620242a2010-10-21 14:23:53 -0600368
Myron Stowe78cdb3e2010-10-21 14:24:09 -0600369 list_add_tail_rcu(&map->list, &acpi_ioremaps);
Myron Stowe620242a2010-10-21 14:23:53 -0600370
Lv Zhenga2383172014-05-20 15:39:41 +0800371out:
Rafael J. Wysocki7ffd0442011-02-08 23:38:05 +0100372 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600373 return map->virt + (phys - map->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
Lv Zhenga2383172014-05-20 15:39:41 +0800375EXPORT_SYMBOL_GPL(acpi_os_map_iomem);
376
Fabian Frederickbd721ea2016-08-02 14:03:33 -0700377void *__ref acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
Lv Zhenga2383172014-05-20 15:39:41 +0800378{
379 return (void *)acpi_os_map_iomem(phys, size);
380}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800381EXPORT_SYMBOL_GPL(acpi_os_map_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Rafael J. Wysocki5003ad72020-07-02 14:14:05 +0200383static void acpi_os_map_remove(struct work_struct *work)
Myron Stowe4a3cba52010-10-21 14:24:14 -0600384{
Rafael J. Wysocki5003ad72020-07-02 14:14:05 +0200385 struct acpi_ioremap *map = container_of(to_rcu_work(work),
386 struct acpi_ioremap,
387 track.rwork);
388
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200389 acpi_unmap(map->phys, map->virt);
390 kfree(map);
391}
Francesco Ruggeri833a4262019-11-19 21:47:27 -0800392
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200393/* Must be called with mutex_lock(&acpi_ioremap_lock) */
Rafael J. Wysockia968fba2020-07-02 14:13:46 +0200394static void acpi_os_drop_map_ref(struct acpi_ioremap *map)
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200395{
396 if (--map->track.refcount)
Rafael J. Wysockia968fba2020-07-02 14:13:46 +0200397 return;
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200398
399 list_del_rcu(&map->list);
400
Rafael J. Wysocki5003ad72020-07-02 14:14:05 +0200401 INIT_RCU_WORK(&map->track.rwork, acpi_os_map_remove);
Rafael J. Wysockia968fba2020-07-02 14:13:46 +0200402 queue_rcu_work(system_wq, &map->track.rwork);
Myron Stowe4a3cba52010-10-21 14:24:14 -0600403}
Myron Stowe4a3cba52010-10-21 14:24:14 -0600404
Rafael J. Wysockia968fba2020-07-02 14:13:46 +0200405/**
406 * acpi_os_unmap_iomem - Drop a memory mapping reference.
407 * @virt: Start of the address range to drop a reference to.
408 * @size: Size of the address range to drop a reference to.
409 *
410 * Look up the given virtual address range in the list of existing ACPI memory
411 * mappings, drop a reference to it and if there are no more active references
412 * to it, queue it up for later removal.
413 *
414 * During early init (when acpi_permanent_mmap has not been set yet) this
415 * routine simply calls __acpi_unmap_table() to get the job done. Since
416 * __acpi_unmap_table() is an __init function, the __ref annotation is needed
417 * here.
418 */
419void __ref acpi_os_unmap_iomem(void __iomem *virt, acpi_size size)
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200420{
421 struct acpi_ioremap *map;
422
423 if (!acpi_permanent_mmap) {
424 __acpi_unmap_table(virt, size);
425 return;
426 }
427
428 mutex_lock(&acpi_ioremap_lock);
429
430 map = acpi_map_lookup_virt(virt, size);
431 if (!map) {
432 mutex_unlock(&acpi_ioremap_lock);
Hanjun Guo2e670de2021-06-02 16:54:29 +0800433 WARN(true, "ACPI: %s: bad address %p\n", __func__, virt);
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200434 return;
435 }
Rafael J. Wysockia968fba2020-07-02 14:13:46 +0200436 acpi_os_drop_map_ref(map);
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200437
438 mutex_unlock(&acpi_ioremap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
Lv Zhenga2383172014-05-20 15:39:41 +0800440EXPORT_SYMBOL_GPL(acpi_os_unmap_iomem);
441
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200442/**
443 * acpi_os_unmap_memory - Drop a memory mapping reference.
444 * @virt: Start of the address range to drop a reference to.
445 * @size: Size of the address range to drop a reference to.
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200446 */
Lv Zhenga2383172014-05-20 15:39:41 +0800447void __ref acpi_os_unmap_memory(void *virt, acpi_size size)
448{
Rafael J. Wysockia968fba2020-07-02 14:13:46 +0200449 acpi_os_unmap_iomem((void __iomem *)virt, size);
Lv Zhenga2383172014-05-20 15:39:41 +0800450}
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800451EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Rafael J. Wysocki69155642020-09-11 14:59:35 +0200453void __iomem *acpi_os_map_generic_address(struct acpi_generic_address *gas)
Myron Stowe29718522010-10-21 14:23:59 -0600454{
Myron Stowebc9ffce2011-11-07 16:23:27 -0700455 u64 addr;
Myron Stowe29718522010-10-21 14:23:59 -0600456
Myron Stowebc9ffce2011-11-07 16:23:27 -0700457 if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
Rafael J. Wysocki69155642020-09-11 14:59:35 +0200458 return NULL;
Myron Stowe29718522010-10-21 14:23:59 -0600459
Myron Stowebc9ffce2011-11-07 16:23:27 -0700460 /* Handle possible alignment issues */
461 memcpy(&addr, &gas->address, sizeof(addr));
462 if (!addr || !gas->bit_width)
Rafael J. Wysocki69155642020-09-11 14:59:35 +0200463 return NULL;
Myron Stowe29718522010-10-21 14:23:59 -0600464
Rafael J. Wysocki69155642020-09-11 14:59:35 +0200465 return acpi_os_map_iomem(addr, gas->bit_width / 8);
Myron Stowe29718522010-10-21 14:23:59 -0600466}
Myron Stowe6f68c912011-11-07 16:23:34 -0700467EXPORT_SYMBOL(acpi_os_map_generic_address);
Myron Stowe29718522010-10-21 14:23:59 -0600468
Myron Stowe6f68c912011-11-07 16:23:34 -0700469void acpi_os_unmap_generic_address(struct acpi_generic_address *gas)
Myron Stowe29718522010-10-21 14:23:59 -0600470{
Myron Stowebc9ffce2011-11-07 16:23:27 -0700471 u64 addr;
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100472 struct acpi_ioremap *map;
Myron Stowe29718522010-10-21 14:23:59 -0600473
Myron Stowebc9ffce2011-11-07 16:23:27 -0700474 if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
Myron Stowe29718522010-10-21 14:23:59 -0600475 return;
476
Myron Stowebc9ffce2011-11-07 16:23:27 -0700477 /* Handle possible alignment issues */
478 memcpy(&addr, &gas->address, sizeof(addr));
479 if (!addr || !gas->bit_width)
Myron Stowe29718522010-10-21 14:23:59 -0600480 return;
481
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100482 mutex_lock(&acpi_ioremap_lock);
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200483
Myron Stowebc9ffce2011-11-07 16:23:27 -0700484 map = acpi_map_lookup(addr, gas->bit_width / 8);
Rafael J. Wysocki7fe135d2011-02-08 23:37:53 +0100485 if (!map) {
486 mutex_unlock(&acpi_ioremap_lock);
487 return;
488 }
Rafael J. Wysockia968fba2020-07-02 14:13:46 +0200489 acpi_os_drop_map_ref(map);
Rafael J. Wysocki17576592020-07-02 13:19:12 +0200490
Rafael J. Wysocki7bbb8902011-02-08 23:37:42 +0100491 mutex_unlock(&acpi_ioremap_lock);
Myron Stowe29718522010-10-21 14:23:59 -0600492}
Myron Stowe6f68c912011-11-07 16:23:34 -0700493EXPORT_SYMBOL(acpi_os_unmap_generic_address);
Myron Stowe29718522010-10-21 14:23:59 -0600494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495#ifdef ACPI_FUTURE_USAGE
496acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400497acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Len Brown4be44fc2005-08-05 00:44:28 -0400499 if (!phys || !virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return AE_BAD_PARAMETER;
501
502 *phys = virt_to_phys(virt);
503
504 return AE_OK;
505}
506#endif
507
Rafael J. Wysocki18d78b62015-07-03 01:06:00 +0200508#ifdef CONFIG_ACPI_REV_OVERRIDE_POSSIBLE
509static bool acpi_rev_override;
510
511int __init acpi_rev_override_setup(char *str)
512{
513 acpi_rev_override = true;
514 return 1;
515}
516__setup("acpi_rev_override", acpi_rev_override_setup);
517#else
518#define acpi_rev_override false
519#endif
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521#define ACPI_MAX_OVERRIDE_LEN 100
522
523static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
524
525acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400526acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
Lv Zheng80b28812016-03-24 09:38:28 +0800527 acpi_string *new_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 if (!init_val || !new_val)
530 return AE_BAD_PARAMETER;
531
532 *new_val = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400533 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +0100534 pr_info("Overriding _OS definition to '%s'\n", acpi_os_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 *new_val = acpi_os_name;
536 }
537
Rafael J. Wysocki18d78b62015-07-03 01:06:00 +0200538 if (!memcmp(init_val->name, "_REV", 4) && acpi_rev_override) {
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +0100539 pr_info("Overriding _REV return value to 5\n");
Rafael J. Wysocki18d78b62015-07-03 01:06:00 +0200540 *new_val = (char *)5;
541 }
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return AE_OK;
544}
545
David Howells7d12e782006-10-05 14:55:46 +0100546static irqreturn_t acpi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Len Brown5229e872008-02-06 01:26:55 -0500548 u32 handled;
549
550 handled = (*acpi_irq_handler) (acpi_irq_context);
551
552 if (handled) {
553 acpi_irq_handled++;
554 return IRQ_HANDLED;
Len Brown88bea182009-04-21 00:35:47 -0400555 } else {
556 acpi_irq_not_handled++;
Len Brown5229e872008-02-06 01:26:55 -0500557 return IRQ_NONE;
Len Brown88bea182009-04-21 00:35:47 -0400558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400562acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
563 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 unsigned int irq;
566
Len Brown5229e872008-02-06 01:26:55 -0500567 acpi_irq_stats_init();
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /*
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100570 * ACPI interrupts different from the SCI in our copy of the FADT are
571 * not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 */
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100573 if (gsi != acpi_gbl_FADT.sci_interrupt)
574 return AE_BAD_PARAMETER;
575
576 if (acpi_irq_handler)
577 return AE_ALREADY_ACQUIRED;
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +0100580 pr_err("SCI (ACPI GSI %d) not registered\n", gsi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return AE_OK;
582 }
583
584 acpi_irq_handler = handler;
585 acpi_irq_context = context;
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200586 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +0100587 pr_err("SCI (IRQ%d) allocation failed\n", irq);
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100588 acpi_irq_handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return AE_NOT_ACQUIRED;
590 }
Chen Yu49e4b8432015-10-25 01:02:19 +0800591 acpi_sci_irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 return AE_OK;
594}
595
Chen Yu49e4b8432015-10-25 01:02:19 +0800596acpi_status acpi_os_remove_interrupt_handler(u32 gsi, acpi_osd_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Chen Yu49e4b8432015-10-25 01:02:19 +0800598 if (gsi != acpi_gbl_FADT.sci_interrupt || !acpi_sci_irq_valid())
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100599 return AE_BAD_PARAMETER;
600
Chen Yu49e4b8432015-10-25 01:02:19 +0800601 free_irq(acpi_sci_irq, acpi_irq);
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +0100602 acpi_irq_handler = NULL;
Chen Yu49e4b8432015-10-25 01:02:19 +0800603 acpi_sci_irq = INVALID_ACPI_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 return AE_OK;
606}
607
608/*
609 * Running in interpreter thread context, safe to sleep
610 */
611
Lin Ming439913f2010-01-28 10:53:19 +0800612void acpi_os_sleep(u64 ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Liu Chuansheng30282292013-09-12 01:42:57 +0800614 msleep(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
Len Brown4be44fc2005-08-05 00:44:28 -0400616
Len Brown4be44fc2005-08-05 00:44:28 -0400617void acpi_os_stall(u32 us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 while (us) {
620 u32 delay = 1000;
621
622 if (delay > us)
623 delay = us;
624 udelay(delay);
625 touch_nmi_watchdog();
626 us -= delay;
627 }
628}
Len Brown4be44fc2005-08-05 00:44:28 -0400629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/*
Bart Van Assche83b23482018-10-17 13:24:56 -0700631 * Support ACPI 3.0 AML Timer operand. Returns a 64-bit free-running,
632 * monotonically increasing timer with 100ns granularity. Do not use
633 * ktime_get() to implement this function because this function may get
634 * called after timekeeping has been suspended. Note: calling this function
635 * after timekeeping has been suspended may lead to unexpected results
636 * because when timekeeping is suspended the jiffies counter is not
637 * incremented. See also timekeeping_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Len Brown4be44fc2005-08-05 00:44:28 -0400639u64 acpi_os_get_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Bart Van Assche83b23482018-10-17 13:24:56 -0700641 return (get_jiffies_64() - INITIAL_JIFFIES) *
642 (ACPI_100NSEC_PER_SEC / HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Len Brown4be44fc2005-08-05 00:44:28 -0400645acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 u32 dummy;
648
649 if (!value)
650 value = &dummy;
651
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800652 *value = 0;
653 if (width <= 8) {
Len Brown4be44fc2005-08-05 00:44:28 -0400654 *(u8 *) value = inb(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800655 } else if (width <= 16) {
Len Brown4be44fc2005-08-05 00:44:28 -0400656 *(u16 *) value = inw(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800657 } else if (width <= 32) {
Len Brown4be44fc2005-08-05 00:44:28 -0400658 *(u32 *) value = inl(port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800659 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 BUG();
661 }
662
663 return AE_OK;
664}
Len Brown4be44fc2005-08-05 00:44:28 -0400665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666EXPORT_SYMBOL(acpi_os_read_port);
667
Len Brown4be44fc2005-08-05 00:44:28 -0400668acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800670 if (width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 outb(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800672 } else if (width <= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 outw(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800674 } else if (width <= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 outl(value, port);
Zhao Yakui49fbabf2007-11-15 17:01:06 +0800676 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 BUG();
678 }
679
680 return AE_OK;
681}
Len Brown4be44fc2005-08-05 00:44:28 -0400682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683EXPORT_SYMBOL(acpi_os_write_port);
684
Srinivas Pandruvadaeeb2d802017-10-05 16:24:03 -0700685int acpi_os_read_iomem(void __iomem *virt_addr, u64 *value, u32 width)
Myron Stowee615bf52012-01-20 19:13:24 -0700686{
Myron Stowee615bf52012-01-20 19:13:24 -0700687
688 switch (width) {
689 case 8:
690 *(u8 *) value = readb(virt_addr);
691 break;
692 case 16:
693 *(u16 *) value = readw(virt_addr);
694 break;
695 case 32:
696 *(u32 *) value = readl(virt_addr);
697 break;
698 case 64:
Andy Shevchenko3277b4e2015-08-17 17:28:46 +0300699 *(u64 *) value = readq(virt_addr);
Myron Stowee615bf52012-01-20 19:13:24 -0700700 break;
701 default:
Srinivas Pandruvadaeeb2d802017-10-05 16:24:03 -0700702 return -EINVAL;
Myron Stowee615bf52012-01-20 19:13:24 -0700703 }
704
Srinivas Pandruvadaeeb2d802017-10-05 16:24:03 -0700705 return 0;
706}
707
708acpi_status
709acpi_os_read_memory(acpi_physical_address phys_addr, u64 *value, u32 width)
710{
711 void __iomem *virt_addr;
712 unsigned int size = width / 8;
713 bool unmap = false;
714 u64 dummy;
715 int error;
716
717 rcu_read_lock();
718 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
719 if (!virt_addr) {
720 rcu_read_unlock();
721 virt_addr = acpi_os_ioremap(phys_addr, size);
722 if (!virt_addr)
723 return AE_BAD_ADDRESS;
724 unmap = true;
725 }
726
727 if (!value)
728 value = &dummy;
729
730 error = acpi_os_read_iomem(virt_addr, value, width);
731 BUG_ON(error);
732
Myron Stowee615bf52012-01-20 19:13:24 -0700733 if (unmap)
734 iounmap(virt_addr);
735 else
736 rcu_read_unlock();
737
738 return AE_OK;
739}
740
Myron Stowee615bf52012-01-20 19:13:24 -0700741acpi_status
Bob Moore653f4b52012-02-14 18:29:55 +0800742acpi_os_write_memory(acpi_physical_address phys_addr, u64 value, u32 width)
Myron Stowee615bf52012-01-20 19:13:24 -0700743{
744 void __iomem *virt_addr;
745 unsigned int size = width / 8;
746 bool unmap = false;
747
748 rcu_read_lock();
749 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
750 if (!virt_addr) {
751 rcu_read_unlock();
752 virt_addr = acpi_os_ioremap(phys_addr, size);
753 if (!virt_addr)
754 return AE_BAD_ADDRESS;
755 unmap = true;
756 }
757
758 switch (width) {
759 case 8:
760 writeb(value, virt_addr);
761 break;
762 case 16:
763 writew(value, virt_addr);
764 break;
765 case 32:
766 writel(value, virt_addr);
767 break;
768 case 64:
Andy Shevchenko3277b4e2015-08-17 17:28:46 +0300769 writeq(value, virt_addr);
Myron Stowee615bf52012-01-20 19:13:24 -0700770 break;
771 default:
772 BUG();
773 }
774
775 if (unmap)
776 iounmap(virt_addr);
777 else
778 rcu_read_unlock();
779
780 return AE_OK;
781}
782
Sinan Kayabd23fac2018-12-19 22:46:55 +0000783#ifdef CONFIG_PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400785acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Bob Moorec5f02312010-08-06 08:57:53 +0800786 u64 *value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 int result, size;
Bob Moorec5f02312010-08-06 08:57:53 +0800789 u32 value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (!value)
792 return AE_BAD_PARAMETER;
793
794 switch (width) {
795 case 8:
796 size = 1;
797 break;
798 case 16:
799 size = 2;
800 break;
801 case 32:
802 size = 4;
803 break;
804 default:
805 return AE_ERROR;
806 }
807
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500808 result = raw_pci_read(pci_id->segment, pci_id->bus,
809 PCI_DEVFN(pci_id->device, pci_id->function),
Bob Moorec5f02312010-08-06 08:57:53 +0800810 reg, size, &value32);
811 *value = value32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 return (result ? AE_ERROR : AE_OK);
814}
Len Brown4be44fc2005-08-05 00:44:28 -0400815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400817acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
Lin Ming439913f2010-01-28 10:53:19 +0800818 u64 value, u32 width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 int result, size;
821
822 switch (width) {
823 case 8:
824 size = 1;
825 break;
826 case 16:
827 size = 2;
828 break;
829 case 32:
830 size = 4;
831 break;
832 default:
833 return AE_ERROR;
834 }
835
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500836 result = raw_pci_write(pci_id->segment, pci_id->bus,
837 PCI_DEVFN(pci_id->device, pci_id->function),
838 reg, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 return (result ? AE_ERROR : AE_OK);
841}
Sinan Kayabd23fac2018-12-19 22:46:55 +0000842#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
David Howells65f27f32006-11-22 14:55:48 +0000844static void acpi_os_execute_deferred(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
David Howells65f27f32006-11-22 14:55:48 +0000846 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
Alexey Starikovskiy88db5e12007-05-09 23:31:03 -0400847
Zhang Rui19cd8472008-08-28 10:05:06 +0800848 dpc->function(dpc->context);
849 kfree(dpc);
Zhang Rui19cd8472008-08-28 10:05:06 +0800850}
851
Lv Zheng836d083012015-12-03 10:43:14 +0800852#ifdef CONFIG_ACPI_DEBUGGER
853static struct acpi_debugger acpi_debugger;
854static bool acpi_debugger_initialized;
855
856int acpi_register_debugger(struct module *owner,
857 const struct acpi_debugger_ops *ops)
858{
859 int ret = 0;
860
861 mutex_lock(&acpi_debugger.lock);
862 if (acpi_debugger.ops) {
863 ret = -EBUSY;
864 goto err_lock;
865 }
866
867 acpi_debugger.owner = owner;
868 acpi_debugger.ops = ops;
869
870err_lock:
871 mutex_unlock(&acpi_debugger.lock);
872 return ret;
873}
874EXPORT_SYMBOL(acpi_register_debugger);
875
876void acpi_unregister_debugger(const struct acpi_debugger_ops *ops)
877{
878 mutex_lock(&acpi_debugger.lock);
879 if (ops == acpi_debugger.ops) {
880 acpi_debugger.ops = NULL;
881 acpi_debugger.owner = NULL;
882 }
883 mutex_unlock(&acpi_debugger.lock);
884}
885EXPORT_SYMBOL(acpi_unregister_debugger);
886
887int acpi_debugger_create_thread(acpi_osd_exec_callback function, void *context)
888{
889 int ret;
890 int (*func)(acpi_osd_exec_callback, void *);
891 struct module *owner;
892
893 if (!acpi_debugger_initialized)
894 return -ENODEV;
895 mutex_lock(&acpi_debugger.lock);
896 if (!acpi_debugger.ops) {
897 ret = -ENODEV;
898 goto err_lock;
899 }
900 if (!try_module_get(acpi_debugger.owner)) {
901 ret = -ENODEV;
902 goto err_lock;
903 }
904 func = acpi_debugger.ops->create_thread;
905 owner = acpi_debugger.owner;
906 mutex_unlock(&acpi_debugger.lock);
907
908 ret = func(function, context);
909
910 mutex_lock(&acpi_debugger.lock);
911 module_put(owner);
912err_lock:
913 mutex_unlock(&acpi_debugger.lock);
914 return ret;
915}
916
917ssize_t acpi_debugger_write_log(const char *msg)
918{
919 ssize_t ret;
920 ssize_t (*func)(const char *);
921 struct module *owner;
922
923 if (!acpi_debugger_initialized)
924 return -ENODEV;
925 mutex_lock(&acpi_debugger.lock);
926 if (!acpi_debugger.ops) {
927 ret = -ENODEV;
928 goto err_lock;
929 }
930 if (!try_module_get(acpi_debugger.owner)) {
931 ret = -ENODEV;
932 goto err_lock;
933 }
934 func = acpi_debugger.ops->write_log;
935 owner = acpi_debugger.owner;
936 mutex_unlock(&acpi_debugger.lock);
937
938 ret = func(msg);
939
940 mutex_lock(&acpi_debugger.lock);
941 module_put(owner);
942err_lock:
943 mutex_unlock(&acpi_debugger.lock);
944 return ret;
945}
946
947ssize_t acpi_debugger_read_cmd(char *buffer, size_t buffer_length)
948{
949 ssize_t ret;
950 ssize_t (*func)(char *, size_t);
951 struct module *owner;
952
953 if (!acpi_debugger_initialized)
954 return -ENODEV;
955 mutex_lock(&acpi_debugger.lock);
956 if (!acpi_debugger.ops) {
957 ret = -ENODEV;
958 goto err_lock;
959 }
960 if (!try_module_get(acpi_debugger.owner)) {
961 ret = -ENODEV;
962 goto err_lock;
963 }
964 func = acpi_debugger.ops->read_cmd;
965 owner = acpi_debugger.owner;
966 mutex_unlock(&acpi_debugger.lock);
967
968 ret = func(buffer, buffer_length);
969
970 mutex_lock(&acpi_debugger.lock);
971 module_put(owner);
972err_lock:
973 mutex_unlock(&acpi_debugger.lock);
974 return ret;
975}
976
977int acpi_debugger_wait_command_ready(void)
978{
979 int ret;
980 int (*func)(bool, char *, size_t);
981 struct module *owner;
982
983 if (!acpi_debugger_initialized)
984 return -ENODEV;
985 mutex_lock(&acpi_debugger.lock);
986 if (!acpi_debugger.ops) {
987 ret = -ENODEV;
988 goto err_lock;
989 }
990 if (!try_module_get(acpi_debugger.owner)) {
991 ret = -ENODEV;
992 goto err_lock;
993 }
994 func = acpi_debugger.ops->wait_command_ready;
995 owner = acpi_debugger.owner;
996 mutex_unlock(&acpi_debugger.lock);
997
998 ret = func(acpi_gbl_method_executing,
999 acpi_gbl_db_line_buf, ACPI_DB_LINE_BUFFER_SIZE);
1000
1001 mutex_lock(&acpi_debugger.lock);
1002 module_put(owner);
1003err_lock:
1004 mutex_unlock(&acpi_debugger.lock);
1005 return ret;
1006}
1007
1008int acpi_debugger_notify_command_complete(void)
1009{
1010 int ret;
1011 int (*func)(void);
1012 struct module *owner;
1013
1014 if (!acpi_debugger_initialized)
1015 return -ENODEV;
1016 mutex_lock(&acpi_debugger.lock);
1017 if (!acpi_debugger.ops) {
1018 ret = -ENODEV;
1019 goto err_lock;
1020 }
1021 if (!try_module_get(acpi_debugger.owner)) {
1022 ret = -ENODEV;
1023 goto err_lock;
1024 }
1025 func = acpi_debugger.ops->notify_command_complete;
1026 owner = acpi_debugger.owner;
1027 mutex_unlock(&acpi_debugger.lock);
1028
1029 ret = func();
1030
1031 mutex_lock(&acpi_debugger.lock);
1032 module_put(owner);
1033err_lock:
1034 mutex_unlock(&acpi_debugger.lock);
1035 return ret;
1036}
1037
1038int __init acpi_debugger_init(void)
1039{
1040 mutex_init(&acpi_debugger.lock);
1041 acpi_debugger_initialized = true;
1042 return 0;
1043}
1044#endif
1045
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -04001046/*******************************************************************************
1047 *
1048 * FUNCTION: acpi_os_execute
1049 *
1050 * PARAMETERS: Type - Type of the callback
1051 * Function - Function to be executed
1052 * Context - Function parameters
1053 *
1054 * RETURN: Status
1055 *
1056 * DESCRIPTION: Depending on type, either queues function for deferred execution or
1057 * immediately executes function on a separate thread.
1058 *
1059 ******************************************************************************/
1060
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001061acpi_status acpi_os_execute(acpi_execute_type type,
1062 acpi_osd_exec_callback function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Len Brown4be44fc2005-08-05 00:44:28 -04001064 acpi_status status = AE_OK;
1065 struct acpi_os_dpc *dpc;
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +03001066 struct workqueue_struct *queue;
Zhang Rui19cd8472008-08-28 10:05:06 +08001067 int ret;
Len Brown72945b22006-07-12 22:46:42 -04001068 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
1069 "Scheduling function [%p(%p)] for deferred execution.\n",
1070 function, context));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001072 if (type == OSL_DEBUGGER_MAIN_THREAD) {
Lv Zheng836d083012015-12-03 10:43:14 +08001073 ret = acpi_debugger_create_thread(function, context);
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001074 if (ret) {
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001075 pr_err("Kernel thread creation failed\n");
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001076 status = AE_ERROR;
1077 }
1078 goto out_thread;
1079 }
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /*
1082 * Allocate/initialize DPC structure. Note that this memory will be
David Howells65f27f32006-11-22 14:55:48 +00001083 * freed by the callee. The kernel handles the work_struct list in a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 * way that allows us to also free its memory inside the callee.
1085 * Because we may want to schedule several tasks with different
1086 * parameters we can't use the approach some kernel code uses of
David Howells65f27f32006-11-22 14:55:48 +00001087 * having a static work_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 */
Len Brown72945b22006-07-12 22:46:42 -04001089
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001090 dpc = kzalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (!dpc)
Lin Ming889c78b2008-12-31 09:23:57 +08001092 return AE_NO_MEMORY;
Linus Torvaldsb976fe12006-11-17 19:31:09 -08001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 dpc->function = function;
1095 dpc->context = context;
Linus Torvaldsb976fe12006-11-17 19:31:09 -08001096
Zhang Ruic02256b2009-06-23 10:20:29 +08001097 /*
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001098 * To prevent lockdep from complaining unnecessarily, make sure that
1099 * there is a different static lockdep key for each workqueue by using
1100 * INIT_WORK() for each of them separately.
Zhang Ruic02256b2009-06-23 10:20:29 +08001101 */
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001102 if (type == OSL_NOTIFY_HANDLER) {
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001103 queue = kacpi_notify_wq;
Zhang Ruibc736752010-03-22 15:48:54 +08001104 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001105 } else if (type == OSL_GPE_HANDLER) {
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001106 queue = kacpid_wq;
Zhang Ruibc736752010-03-22 15:48:54 +08001107 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001108 } else {
1109 pr_err("Unsupported os_execute type %d.\n", type);
1110 status = AE_ERROR;
Rafael J. Wysocki3ae45a22012-11-02 13:09:08 +01001111 }
Zhang Ruibc736752010-03-22 15:48:54 +08001112
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001113 if (ACPI_FAILURE(status))
1114 goto err_workqueue;
1115
Tejun Heo8fec62b2010-06-29 10:07:09 +02001116 /*
1117 * On some machines, a software-initiated SMI causes corruption unless
1118 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
1119 * typically it's done in GPE-related methods that are run via
1120 * workqueues, so we can avoid the known corruption cases by always
1121 * queueing on CPU 0.
1122 */
1123 ret = queue_work_on(0, queue, &dpc->work);
Zhang Rui19cd8472008-08-28 10:05:06 +08001124 if (!ret) {
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001125 pr_err("Unable to queue work\n");
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +03001126 status = AE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001128err_workqueue:
1129 if (ACPI_FAILURE(status))
1130 kfree(dpc);
1131out_thread:
Lin Ming889c78b2008-12-31 09:23:57 +08001132 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -04001134EXPORT_SYMBOL(acpi_os_execute);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Lin Mingbd6f10a2012-05-22 16:43:49 +08001136void acpi_os_wait_events_complete(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Lv Zheng90253a72014-11-05 15:06:13 +08001138 /*
1139 * Make sure the GPE handler or the fixed event handler is not used
1140 * on another CPU after removal.
1141 */
Chen Yuefb1cf72015-10-25 01:02:36 +08001142 if (acpi_sci_irq_valid())
1143 synchronize_hardirq(acpi_sci_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 flush_workqueue(kacpid_wq);
Zhang Rui2f67a062008-04-29 02:34:42 -04001145 flush_workqueue(kacpi_notify_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
Ronald Tschalär757c9682018-09-30 19:53:13 -07001147EXPORT_SYMBOL(acpi_os_wait_events_complete);
Len Brown4be44fc2005-08-05 00:44:28 -04001148
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001149struct acpi_hp_work {
1150 struct work_struct work;
Rafael J. Wysocki1e3bcb592014-03-03 00:40:38 +01001151 struct acpi_device *adev;
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001152 u32 src;
1153};
1154
1155static void acpi_hotplug_work_fn(struct work_struct *work)
1156{
1157 struct acpi_hp_work *hpw = container_of(work, struct acpi_hp_work, work);
1158
1159 acpi_os_wait_events_complete();
Rafael J. Wysocki1e3bcb592014-03-03 00:40:38 +01001160 acpi_device_hotplug(hpw->adev, hpw->src);
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001161 kfree(hpw);
1162}
1163
Rafael J. Wysocki1e3bcb592014-03-03 00:40:38 +01001164acpi_status acpi_hotplug_schedule(struct acpi_device *adev, u32 src)
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001165{
1166 struct acpi_hp_work *hpw;
1167
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001168 acpi_handle_debug(adev->handle,
1169 "Scheduling hotplug event %u for deferred handling\n",
1170 src);
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001171
1172 hpw = kmalloc(sizeof(*hpw), GFP_KERNEL);
1173 if (!hpw)
1174 return AE_NO_MEMORY;
1175
1176 INIT_WORK(&hpw->work, acpi_hotplug_work_fn);
Rafael J. Wysocki1e3bcb592014-03-03 00:40:38 +01001177 hpw->adev = adev;
Rafael J. Wysocki7b981182013-11-07 01:45:40 +01001178 hpw->src = src;
1179 /*
1180 * We can't run hotplug code in kacpid_wq/kacpid_notify_wq etc., because
1181 * the hotplug code may call driver .remove() functions, which may
1182 * invoke flush_scheduled_work()/acpi_os_wait_events_complete() to flush
1183 * these workqueues.
1184 */
1185 if (!queue_work(kacpi_hotplug_wq, &hpw->work)) {
1186 kfree(hpw);
1187 return AE_ERROR;
1188 }
1189 return AE_OK;
1190}
1191
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001192bool acpi_queue_hotplug_work(struct work_struct *work)
1193{
1194 return queue_work(kacpi_hotplug_wq, work);
1195}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001198acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Len Brown4be44fc2005-08-05 00:44:28 -04001200 struct semaphore *sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
jhbird.choi@samsung.com2d0acb42014-03-20 16:35:56 +09001202 sem = acpi_os_allocate_zeroed(sizeof(struct semaphore));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -04001204 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 sema_init(sem, initial_units);
1207
Len Brown4be44fc2005-08-05 00:44:28 -04001208 *handle = (acpi_handle *) sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Len Brown4be44fc2005-08-05 00:44:28 -04001210 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
1211 *handle, initial_units));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Patrick Mocheld550d982006-06-27 00:41:40 -04001213 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216/*
1217 * TODO: A better way to delete semaphores? Linux doesn't have a
1218 * 'delete_semaphore()' function -- may result in an invalid
1219 * pointer dereference for non-synchronized consumers. Should
1220 * we at least check for blocked threads and signal/cancel them?
1221 */
1222
Len Brown4be44fc2005-08-05 00:44:28 -04001223acpi_status acpi_os_delete_semaphore(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Len Brown4be44fc2005-08-05 00:44:28 -04001225 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (!sem)
Patrick Mocheld550d982006-06-27 00:41:40 -04001228 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Len Brown4be44fc2005-08-05 00:44:28 -04001230 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001232 BUG_ON(!list_empty(&sem->wait_list));
Len Brown02438d82006-06-30 03:19:10 -04001233 kfree(sem);
Len Brown4be44fc2005-08-05 00:44:28 -04001234 sem = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Patrick Mocheld550d982006-06-27 00:41:40 -04001236 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 * TODO: Support for units > 1?
1241 */
Len Brown4be44fc2005-08-05 00:44:28 -04001242acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Len Brown4be44fc2005-08-05 00:44:28 -04001244 acpi_status status = AE_OK;
1245 struct semaphore *sem = (struct semaphore *)handle;
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001246 long jiffies;
Len Brown4be44fc2005-08-05 00:44:28 -04001247 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Lv Zheng7901a052015-08-05 16:23:51 +08001249 if (!acpi_os_initialized)
1250 return AE_OK;
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001253 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -04001256 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Len Brown4be44fc2005-08-05 00:44:28 -04001258 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
1259 handle, units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001261 if (timeout == ACPI_WAIT_FOREVER)
1262 jiffies = MAX_SCHEDULE_TIMEOUT;
1263 else
1264 jiffies = msecs_to_jiffies(timeout);
Al Stonecad15252013-12-04 12:59:11 -07001265
Matthew Wilcoxf1241c82008-03-14 13:43:13 -04001266 ret = down_timeout(sem, jiffies);
1267 if (ret)
1268 status = AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 if (ACPI_FAILURE(status)) {
Bjorn Helgaas9e7e2c02006-04-27 05:25:00 -04001271 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -04001272 "Failed to acquire semaphore[%p|%d|%d], %s",
Len Brown4be44fc2005-08-05 00:44:28 -04001273 handle, units, timeout,
1274 acpi_format_exception(status)));
1275 } else {
1276 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
Thomas Renningera6fc6722006-06-26 23:58:43 -04001277 "Acquired semaphore[%p|%d|%d]", handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001278 units, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
Patrick Mocheld550d982006-06-27 00:41:40 -04001281 return status;
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: Support for units > 1?
1286 */
Len Brown4be44fc2005-08-05 00:44:28 -04001287acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Len Brown4be44fc2005-08-05 00:44:28 -04001289 struct semaphore *sem = (struct semaphore *)handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Lv Zheng7901a052015-08-05 16:23:51 +08001291 if (!acpi_os_initialized)
1292 return AE_OK;
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (!sem || (units < 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001295 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 if (units > 1)
Patrick Mocheld550d982006-06-27 00:41:40 -04001298 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Len Brown4be44fc2005-08-05 00:44:28 -04001300 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
1301 units));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 up(sem);
1304
Patrick Mocheld550d982006-06-27 00:41:40 -04001305 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
Len Brown4be44fc2005-08-05 00:44:28 -04001307
Lv Zheng4d946f72015-10-19 10:25:56 +08001308acpi_status acpi_os_get_line(char *buffer, u32 buffer_length, u32 *bytes_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310#ifdef ENABLE_DEBUGGER
1311 if (acpi_in_debugger) {
1312 u32 chars;
1313
Lv Zheng4d946f72015-10-19 10:25:56 +08001314 kdb_read(buffer, buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 /* remove the CR kdb includes */
1317 chars = strlen(buffer) - 1;
1318 buffer[chars] = '\0';
1319 }
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001320#else
1321 int ret;
1322
Lv Zheng836d083012015-12-03 10:43:14 +08001323 ret = acpi_debugger_read_cmd(buffer, buffer_length);
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001324 if (ret < 0)
1325 return AE_ERROR;
1326 if (bytes_read)
1327 *bytes_read = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328#endif
1329
Lv Zheng4d946f72015-10-19 10:25:56 +08001330 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
Lv Zheng836d083012015-12-03 10:43:14 +08001332EXPORT_SYMBOL(acpi_os_get_line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001334acpi_status acpi_os_wait_command_ready(void)
1335{
1336 int ret;
1337
Lv Zheng836d083012015-12-03 10:43:14 +08001338 ret = acpi_debugger_wait_command_ready();
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001339 if (ret < 0)
1340 return AE_ERROR;
1341 return AE_OK;
1342}
1343
1344acpi_status acpi_os_notify_command_complete(void)
1345{
1346 int ret;
1347
Lv Zheng836d083012015-12-03 10:43:14 +08001348 ret = acpi_debugger_notify_command_complete();
Lv Zheng8cfb0cd2015-12-03 10:43:00 +08001349 if (ret < 0)
1350 return AE_ERROR;
1351 return AE_OK;
1352}
1353
Len Brown4be44fc2005-08-05 00:44:28 -04001354acpi_status acpi_os_signal(u32 function, void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Len Brown4be44fc2005-08-05 00:44:28 -04001356 switch (function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 case ACPI_SIGNAL_FATAL:
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001358 pr_err("Fatal opcode executed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 break;
1360 case ACPI_SIGNAL_BREAKPOINT:
1361 /*
1362 * AML Breakpoint
1363 * ACPI spec. says to treat it as a NOP unless
1364 * you are debugging. So if/when we integrate
1365 * AML debugger into the kernel debugger its
1366 * hook will go here. But until then it is
1367 * not useful to print anything on breakpoints.
1368 */
1369 break;
1370 default:
1371 break;
1372 }
1373
1374 return AE_OK;
1375}
Len Brown4be44fc2005-08-05 00:44:28 -04001376
Len Brown4be44fc2005-08-05 00:44:28 -04001377static int __init acpi_os_name_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 char *p = acpi_os_name;
Len Brown4be44fc2005-08-05 00:44:28 -04001380 int count = ACPI_MAX_OVERRIDE_LEN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 if (!str || !*str)
1383 return 0;
1384
Dan Carpenter5e2be4e2013-10-18 12:01:43 +03001385 for (; count-- && *str; str++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (isalnum(*str) || *str == ' ' || *str == ':')
1387 *p++ = *str;
1388 else if (*str == '\'' || *str == '"')
1389 continue;
1390 else
1391 break;
1392 }
1393 *p = 0;
1394
1395 return 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
1399__setup("acpi_os_name=", acpi_os_name_setup);
1400
Bob Moore22b5afc2014-03-24 14:49:00 +08001401/*
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001402 * Disable the auto-serialization of named objects creation methods.
Bob Moore22b5afc2014-03-24 14:49:00 +08001403 *
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001404 * This feature is enabled by default. It marks the AML control methods
Bob Moore22b5afc2014-03-24 14:49:00 +08001405 * that contain the opcodes to create named objects as "Serialized".
1406 */
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001407static int __init acpi_no_auto_serialize_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001409 acpi_gbl_auto_serialize_methods = FALSE;
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001410 pr_info("Auto-serialization disabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 return 1;
1413}
1414
Lv Zheng08e1d7c2014-03-24 14:49:22 +08001415__setup("acpi_no_auto_serialize", acpi_no_auto_serialize_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Thomas Renningerdf92e692008-02-04 23:31:22 -08001417/* Check of resource interference between native drivers and ACPI
1418 * OperationRegions (SystemIO and System Memory only).
1419 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1420 * in arbitrary AML code and can interfere with legacy drivers.
1421 * acpi_enforce_resources= can be set to:
1422 *
Luca Tettamanti7e905602009-03-30 00:01:27 +02001423 * - strict (default) (2)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001424 * -> further driver trying to access the resources will not load
Luca Tettamanti7e905602009-03-30 00:01:27 +02001425 * - lax (1)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001426 * -> further driver trying to access the resources will load, but you
1427 * get a system message that something might go wrong...
1428 *
1429 * - no (0)
1430 * -> ACPI Operation Region resources will not be registered
1431 *
1432 */
1433#define ENFORCE_RESOURCES_STRICT 2
1434#define ENFORCE_RESOURCES_LAX 1
1435#define ENFORCE_RESOURCES_NO 0
1436
Luca Tettamanti7e905602009-03-30 00:01:27 +02001437static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001438
1439static int __init acpi_enforce_resources_setup(char *str)
1440{
1441 if (str == NULL || *str == '\0')
1442 return 0;
1443
1444 if (!strcmp("strict", str))
1445 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1446 else if (!strcmp("lax", str))
1447 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1448 else if (!strcmp("no", str))
1449 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1450
1451 return 1;
1452}
1453
1454__setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1455
1456/* Check for resource conflicts between ACPI OperationRegions and native
1457 * drivers */
Jean Delvare876fba42009-11-11 15:22:15 +01001458int acpi_check_resource_conflict(const struct resource *res)
Thomas Renningerdf92e692008-02-04 23:31:22 -08001459{
Lin Mingf654c0f2012-01-12 13:10:32 +08001460 acpi_adr_space_type space_id;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001461
1462 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1463 return 0;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001464
Lin Mingf654c0f2012-01-12 13:10:32 +08001465 if (res->flags & IORESOURCE_IO)
1466 space_id = ACPI_ADR_SPACE_SYSTEM_IO;
Rafael J. Wysocki505ca2f2021-02-09 15:59:04 +01001467 else if (res->flags & IORESOURCE_MEM)
Lin Mingf654c0f2012-01-12 13:10:32 +08001468 space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
Rafael J. Wysocki505ca2f2021-02-09 15:59:04 +01001469 else
1470 return 0;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001471
Rafael J. Wysocki505ca2f2021-02-09 15:59:04 +01001472 if (!acpi_check_address_range(space_id, res->start, resource_size(res), 1))
1473 return 0;
Thomas Renningerdf92e692008-02-04 23:31:22 -08001474
Rafael J. Wysocki505ca2f2021-02-09 15:59:04 +01001475 pr_info("Resource conflict; ACPI support missing from driver?\n");
1476
1477 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1478 return -EBUSY;
1479
1480 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1481 pr_notice("Resource conflict: System may be unstable or behave erratically\n");
1482
Thomas Renningerdf92e692008-02-04 23:31:22 -08001483 return 0;
1484}
Thomas Renninger443dea72008-02-04 23:31:23 -08001485EXPORT_SYMBOL(acpi_check_resource_conflict);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001486
1487int acpi_check_region(resource_size_t start, resource_size_t n,
1488 const char *name)
1489{
Zhen Lei01c3d592021-06-01 14:50:32 +08001490 struct resource res = DEFINE_RES_IO_NAMED(start, n, name);
Thomas Renningerdf92e692008-02-04 23:31:22 -08001491
1492 return acpi_check_resource_conflict(&res);
1493}
1494EXPORT_SYMBOL(acpi_check_region);
1495
Heikki Krogerusd2d2e3c2018-06-21 16:43:17 +03001496static acpi_status acpi_deactivate_mem_region(acpi_handle handle, u32 level,
1497 void *_res, void **return_value)
1498{
1499 struct acpi_mem_space_context **mem_ctx;
1500 union acpi_operand_object *handler_obj;
1501 union acpi_operand_object *region_obj2;
1502 union acpi_operand_object *region_obj;
1503 struct resource *res = _res;
1504 acpi_status status;
1505
1506 region_obj = acpi_ns_get_attached_object(handle);
1507 if (!region_obj)
1508 return AE_OK;
1509
1510 handler_obj = region_obj->region.handler;
1511 if (!handler_obj)
1512 return AE_OK;
1513
1514 if (region_obj->region.space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
1515 return AE_OK;
1516
1517 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE))
1518 return AE_OK;
1519
1520 region_obj2 = acpi_ns_get_secondary_object(region_obj);
1521 if (!region_obj2)
1522 return AE_OK;
1523
1524 mem_ctx = (void *)&region_obj2->extra.region_context;
1525
1526 if (!(mem_ctx[0]->address >= res->start &&
1527 mem_ctx[0]->address < res->end))
1528 return AE_OK;
1529
1530 status = handler_obj->address_space.setup(region_obj,
1531 ACPI_REGION_DEACTIVATE,
1532 NULL, (void **)mem_ctx);
1533 if (ACPI_SUCCESS(status))
1534 region_obj->region.flags &= ~(AOPOBJ_SETUP_COMPLETE);
1535
1536 return status;
1537}
1538
1539/**
1540 * acpi_release_memory - Release any mappings done to a memory region
1541 * @handle: Handle to namespace node
1542 * @res: Memory resource
1543 * @level: A level that terminates the search
1544 *
1545 * Walks through @handle and unmaps all SystemMemory Operation Regions that
1546 * overlap with @res and that have already been activated (mapped).
1547 *
1548 * This is a helper that allows drivers to place special requirements on memory
1549 * region that may overlap with operation regions, primarily allowing them to
1550 * safely map the region as non-cached memory.
1551 *
1552 * The unmapped Operation Regions will be automatically remapped next time they
1553 * are called, so the drivers do not need to do anything else.
1554 */
1555acpi_status acpi_release_memory(acpi_handle handle, struct resource *res,
1556 u32 level)
1557{
Rafael J. Wysocki33f61d72020-08-21 19:42:55 +02001558 acpi_status status;
1559
Heikki Krogerusd2d2e3c2018-06-21 16:43:17 +03001560 if (!(res->flags & IORESOURCE_MEM))
1561 return AE_TYPE;
1562
Rafael J. Wysocki33f61d72020-08-21 19:42:55 +02001563 status = acpi_walk_namespace(ACPI_TYPE_REGION, handle, level,
1564 acpi_deactivate_mem_region, NULL,
1565 res, NULL);
1566 if (ACPI_FAILURE(status))
1567 return status;
1568
1569 /*
1570 * Wait for all of the mappings queued up for removal by
1571 * acpi_deactivate_mem_region() to actually go away.
1572 */
1573 synchronize_rcu();
1574 rcu_barrier();
1575 flush_scheduled_work();
1576
1577 return AE_OK;
Heikki Krogerusd2d2e3c2018-06-21 16:43:17 +03001578}
1579EXPORT_SYMBOL_GPL(acpi_release_memory);
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581/*
Jean Delvare70dd6be2010-05-27 19:58:37 +02001582 * Let drivers know whether the resource checks are effective
1583 */
1584int acpi_resources_are_enforced(void)
1585{
1586 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1587}
1588EXPORT_SYMBOL(acpi_resources_are_enforced);
1589
1590/*
Lin Ming9f63b882011-03-23 17:26:34 +08001591 * Deallocate the memory for a spinlock.
1592 */
1593void acpi_os_delete_lock(acpi_spinlock handle)
1594{
1595 ACPI_FREE(handle);
1596}
1597
1598/*
Robert Moore73459f72005-06-24 00:00:00 -04001599 * Acquire a spinlock.
1600 *
1601 * handle is a pointer to the spinlock_t.
Robert Moore73459f72005-06-24 00:00:00 -04001602 */
1603
Bob Moore967440e32006-06-23 17:04:00 -04001604acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
Jules Irenge2288eba2020-02-23 23:17:03 +00001605 __acquires(lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001606{
Bob Mooreb8e4d892006-01-27 16:43:00 -05001607 acpi_cpu_flags flags;
Bob Moore967440e32006-06-23 17:04:00 -04001608 spin_lock_irqsave(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001609 return flags;
1610}
1611
1612/*
1613 * Release a spinlock. See above.
1614 */
1615
Bob Moore967440e32006-06-23 17:04:00 -04001616void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
Jules Irenge2288eba2020-02-23 23:17:03 +00001617 __releases(lockp)
Robert Moore73459f72005-06-24 00:00:00 -04001618{
Bob Moore967440e32006-06-23 17:04:00 -04001619 spin_unlock_irqrestore(lockp, flags);
Robert Moore73459f72005-06-24 00:00:00 -04001620}
1621
Robert Moore73459f72005-06-24 00:00:00 -04001622#ifndef ACPI_USE_LOCAL_CACHE
1623
1624/*******************************************************************************
1625 *
1626 * FUNCTION: acpi_os_create_cache
1627 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001628 * PARAMETERS: name - Ascii name for the cache
1629 * size - Size of each cached object
1630 * depth - Maximum depth of the cache (in objects) <ignored>
1631 * cache - Where the new cache object is returned
Robert Moore73459f72005-06-24 00:00:00 -04001632 *
Bob Mooreb229cf92006-04-21 17:15:00 -04001633 * RETURN: status
Robert Moore73459f72005-06-24 00:00:00 -04001634 *
1635 * DESCRIPTION: Create a cache object
1636 *
1637 ******************************************************************************/
1638
1639acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001640acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
Robert Moore73459f72005-06-24 00:00:00 -04001641{
Paul Mundt20c2df82007-07-20 10:11:58 +09001642 *cache = kmem_cache_create(name, size, 0, 0, NULL);
Adrian Bunka6fdbf92006-12-19 12:56:13 -08001643 if (*cache == NULL)
Bob Mooreb229cf92006-04-21 17:15:00 -04001644 return AE_ERROR;
1645 else
1646 return AE_OK;
Robert Moore73459f72005-06-24 00:00:00 -04001647}
1648
1649/*******************************************************************************
1650 *
1651 * FUNCTION: acpi_os_purge_cache
1652 *
1653 * PARAMETERS: Cache - Handle to cache object
1654 *
1655 * RETURN: Status
1656 *
1657 * DESCRIPTION: Free all objects within the requested cache.
1658 *
1659 ******************************************************************************/
1660
Len Brown4be44fc2005-08-05 00:44:28 -04001661acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001662{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001663 kmem_cache_shrink(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001664 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001665}
1666
1667/*******************************************************************************
1668 *
1669 * FUNCTION: acpi_os_delete_cache
1670 *
1671 * PARAMETERS: Cache - Handle to cache object
1672 *
1673 * RETURN: Status
1674 *
1675 * DESCRIPTION: Free all objects within the requested cache and delete the
1676 * cache object.
1677 *
1678 ******************************************************************************/
1679
Len Brown4be44fc2005-08-05 00:44:28 -04001680acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
Robert Moore73459f72005-06-24 00:00:00 -04001681{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001682 kmem_cache_destroy(cache);
Len Brown4be44fc2005-08-05 00:44:28 -04001683 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001684}
1685
1686/*******************************************************************************
1687 *
1688 * FUNCTION: acpi_os_release_object
1689 *
1690 * PARAMETERS: Cache - Handle to cache object
1691 * Object - The object to be released
1692 *
1693 * RETURN: None
1694 *
1695 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1696 * the object is deleted.
1697 *
1698 ******************************************************************************/
1699
Len Brown4be44fc2005-08-05 00:44:28 -04001700acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -04001701{
Len Brown4be44fc2005-08-05 00:44:28 -04001702 kmem_cache_free(cache, object);
1703 return (AE_OK);
Robert Moore73459f72005-06-24 00:00:00 -04001704}
Robert Moore73459f72005-06-24 00:00:00 -04001705#endif
Myron Stowed362eda2010-10-21 14:24:04 -06001706
Lv Zhenga94e88c2014-04-04 12:39:11 +08001707static int __init acpi_no_static_ssdt_setup(char *s)
Lv Zhengb75dd292013-06-08 00:58:48 +00001708{
Lv Zhenga94e88c2014-04-04 12:39:11 +08001709 acpi_gbl_disable_ssdt_table_install = TRUE;
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001710 pr_info("Static SSDT installation disabled\n");
Lv Zhengb75dd292013-06-08 00:58:48 +00001711
Lv Zhenga94e88c2014-04-04 12:39:11 +08001712 return 0;
Lv Zhengb75dd292013-06-08 00:58:48 +00001713}
1714
Lv Zhenga94e88c2014-04-04 12:39:11 +08001715early_param("acpi_no_static_ssdt", acpi_no_static_ssdt_setup);
Lv Zhengb75dd292013-06-08 00:58:48 +00001716
Lv Zheng4dde5072014-02-11 11:01:52 +08001717static int __init acpi_disable_return_repair(char *s)
1718{
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001719 pr_notice("Predefined validation mechanism disabled\n");
Lv Zheng4dde5072014-02-11 11:01:52 +08001720 acpi_gbl_disable_auto_repair = TRUE;
1721
1722 return 1;
1723}
1724
1725__setup("acpica_no_return_repair", acpi_disable_return_repair);
1726
Myron Stowed362eda2010-10-21 14:24:04 -06001727acpi_status __init acpi_os_initialize(void)
1728{
1729 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1730 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
Rafael J. Wysocki85f94022020-09-11 14:59:42 +02001731
1732 acpi_gbl_xgpe0_block_logical_address =
1733 (unsigned long)acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1734 acpi_gbl_xgpe1_block_logical_address =
1735 (unsigned long)acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1736
Randy Wrighta4714a82014-06-04 08:55:59 -07001737 if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) {
1738 /*
1739 * Use acpi_os_map_generic_address to pre-map the reset
1740 * register if it's in system memory.
1741 */
Rafael J. Wysocki69155642020-09-11 14:59:35 +02001742 void *rv;
Randy Wrighta4714a82014-06-04 08:55:59 -07001743
1744 rv = acpi_os_map_generic_address(&acpi_gbl_FADT.reset_register);
Rafael J. Wysocki8aef2732021-02-10 19:09:43 +01001745 pr_debug("%s: Reset register mapping %s\n", __func__,
Rafael J. Wysocki69155642020-09-11 14:59:35 +02001746 rv ? "successful" : "failed");
Randy Wrighta4714a82014-06-04 08:55:59 -07001747 }
Lv Zheng7901a052015-08-05 16:23:51 +08001748 acpi_os_initialized = true;
Myron Stowed362eda2010-10-21 14:24:04 -06001749
1750 return AE_OK;
1751}
1752
Zhang Rui32d47ee2010-12-08 10:40:36 +08001753acpi_status __init acpi_os_initialize1(void)
Myron Stowed362eda2010-10-21 14:24:04 -06001754{
Tejun Heo44d25882011-02-01 11:42:42 +01001755 kacpid_wq = alloc_workqueue("kacpid", 0, 1);
1756 kacpi_notify_wq = alloc_workqueue("kacpi_notify", 0, 1);
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001757 kacpi_hotplug_wq = alloc_ordered_workqueue("kacpi_hotplug", 0);
Myron Stowed362eda2010-10-21 14:24:04 -06001758 BUG_ON(!kacpid_wq);
1759 BUG_ON(!kacpi_notify_wq);
1760 BUG_ON(!kacpi_hotplug_wq);
Lv Zhenge5f660e2016-05-03 16:49:01 +08001761 acpi_osi_init();
Myron Stowed362eda2010-10-21 14:24:04 -06001762 return AE_OK;
1763}
1764
1765acpi_status acpi_os_terminate(void)
1766{
1767 if (acpi_irq_handler) {
Rafael J. Wysocki23fe3632011-02-08 23:48:16 +01001768 acpi_os_remove_interrupt_handler(acpi_gbl_FADT.sci_interrupt,
Myron Stowed362eda2010-10-21 14:24:04 -06001769 acpi_irq_handler);
1770 }
1771
1772 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1773 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
Rafael J. Wysocki85f94022020-09-11 14:59:42 +02001774 acpi_gbl_xgpe0_block_logical_address = 0UL;
1775 acpi_gbl_xgpe1_block_logical_address = 0UL;
1776
Myron Stowed362eda2010-10-21 14:24:04 -06001777 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1778 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
Rafael J. Wysocki85f94022020-09-11 14:59:42 +02001779
Randy Wrighta4714a82014-06-04 08:55:59 -07001780 if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER)
1781 acpi_os_unmap_generic_address(&acpi_gbl_FADT.reset_register);
Myron Stowed362eda2010-10-21 14:24:04 -06001782
1783 destroy_workqueue(kacpid_wq);
1784 destroy_workqueue(kacpi_notify_wq);
1785 destroy_workqueue(kacpi_hotplug_wq);
1786
1787 return AE_OK;
1788}
Tang Liang09f98a82011-12-09 10:05:54 +08001789
1790acpi_status acpi_os_prepare_sleep(u8 sleep_state, u32 pm1a_control,
1791 u32 pm1b_control)
1792{
1793 int rc = 0;
1794 if (__acpi_os_prepare_sleep)
1795 rc = __acpi_os_prepare_sleep(sleep_state,
1796 pm1a_control, pm1b_control);
1797 if (rc < 0)
1798 return AE_ERROR;
1799 else if (rc > 0)
Lv Zheng0fc5e8f2016-12-28 15:28:49 +08001800 return AE_CTRL_TERMINATE;
Tang Liang09f98a82011-12-09 10:05:54 +08001801
1802 return AE_OK;
1803}
1804
1805void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
1806 u32 pm1a_ctrl, u32 pm1b_ctrl))
1807{
1808 __acpi_os_prepare_sleep = func;
1809}
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001810
Lv Zheng0fc5e8f2016-12-28 15:28:49 +08001811#if (ACPI_REDUCED_HARDWARE)
Ben Guthrod6b47b12013-07-30 08:24:52 -04001812acpi_status acpi_os_prepare_extended_sleep(u8 sleep_state, u32 val_a,
1813 u32 val_b)
1814{
1815 int rc = 0;
1816 if (__acpi_os_prepare_extended_sleep)
1817 rc = __acpi_os_prepare_extended_sleep(sleep_state,
1818 val_a, val_b);
1819 if (rc < 0)
1820 return AE_ERROR;
1821 else if (rc > 0)
Lv Zheng0fc5e8f2016-12-28 15:28:49 +08001822 return AE_CTRL_TERMINATE;
Ben Guthrod6b47b12013-07-30 08:24:52 -04001823
1824 return AE_OK;
1825}
Lv Zheng0fc5e8f2016-12-28 15:28:49 +08001826#else
1827acpi_status acpi_os_prepare_extended_sleep(u8 sleep_state, u32 val_a,
1828 u32 val_b)
1829{
1830 return AE_OK;
1831}
1832#endif
Ben Guthrod6b47b12013-07-30 08:24:52 -04001833
1834void acpi_os_set_prepare_extended_sleep(int (*func)(u8 sleep_state,
1835 u32 val_a, u32 val_b))
1836{
1837 __acpi_os_prepare_extended_sleep = func;
1838}
Lv Zheng0fc5e8f2016-12-28 15:28:49 +08001839
1840acpi_status acpi_os_enter_sleep(u8 sleep_state,
1841 u32 reg_a_value, u32 reg_b_value)
1842{
1843 acpi_status status;
1844
1845 if (acpi_gbl_reduced_hardware)
1846 status = acpi_os_prepare_extended_sleep(sleep_state,
1847 reg_a_value,
1848 reg_b_value);
1849 else
1850 status = acpi_os_prepare_sleep(sleep_state,
1851 reg_a_value, reg_b_value);
1852 return status;
1853}