]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/acpi/osl.c
ACPI: Avoid walking the list of memory mappings in osl.c twice in a row
[mirror_ubuntu-hirsute-kernel.git] / drivers / acpi / osl.c
CommitLineData
1da177e4
LT
1/*
2 * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
3 *
4 * Copyright (C) 2000 Andrew Henroid
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
f1241c87
MW
7 * Copyright (c) 2008 Intel Corporation
8 * Author: Matthew Wilcox <willy@linux.intel.com>
1da177e4
LT
9 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 *
28 */
29
1da177e4
LT
30#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/slab.h>
33#include <linux/mm.h>
34#include <linux/pci.h>
1da177e4
LT
35#include <linux/interrupt.h>
36#include <linux/kmod.h>
37#include <linux/delay.h>
38#include <linux/workqueue.h>
39#include <linux/nmi.h>
ad71860a 40#include <linux/acpi.h>
2d6d9fd3 41#include <linux/acpi_io.h>
1da177e4 42#include <linux/efi.h>
df92e695
TR
43#include <linux/ioport.h>
44#include <linux/list.h>
f1241c87
MW
45#include <linux/jiffies.h>
46#include <linux/semaphore.h>
47
48#include <asm/io.h>
49#include <asm/uaccess.h>
50
51#include <acpi/acpi.h>
52#include <acpi/acpi_bus.h>
53#include <acpi/processor.h>
1da177e4 54
1da177e4 55#define _COMPONENT ACPI_OS_SERVICES
f52fd66d 56ACPI_MODULE_NAME("osl");
1da177e4 57#define PREFIX "ACPI: "
4be44fcd
LB
58struct acpi_os_dpc {
59 acpi_osd_exec_callback function;
60 void *context;
65f27f38 61 struct work_struct work;
9ac61856 62 int wait;
1da177e4
LT
63};
64
65#ifdef CONFIG_ACPI_CUSTOM_DSDT
66#include CONFIG_ACPI_CUSTOM_DSDT_FILE
67#endif
68
69#ifdef ENABLE_DEBUGGER
70#include <linux/kdb.h>
71
72/* stuff for debugger support */
73int acpi_in_debugger;
74EXPORT_SYMBOL(acpi_in_debugger);
75
76extern char line_buf[80];
4be44fcd 77#endif /*ENABLE_DEBUGGER */
1da177e4
LT
78
79static unsigned int acpi_irq_irq;
80static acpi_osd_handler acpi_irq_handler;
81static void *acpi_irq_context;
82static struct workqueue_struct *kacpid_wq;
88db5e14 83static struct workqueue_struct *kacpi_notify_wq;
c02256be 84static struct workqueue_struct *kacpi_hotplug_wq;
1da177e4 85
df92e695
TR
86struct acpi_res_list {
87 resource_size_t start;
88 resource_size_t end;
89 acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
90 char name[5]; /* only can have a length of 4 chars, make use of this
91 one instead of res->name, no need to kalloc then */
92 struct list_head resource_list;
a5fe1a03 93 int count;
df92e695
TR
94};
95
96static LIST_HEAD(resource_list_head);
97static DEFINE_SPINLOCK(acpi_res_lock);
98
620242ae
MS
99/*
100 * This list of permanent mappings is for memory that may be accessed from
101 * interrupt context, where we can't do the ioremap().
102 */
103struct acpi_ioremap {
104 struct list_head list;
105 void __iomem *virt;
106 acpi_physical_address phys;
107 acpi_size size;
4a3cba5e 108 struct kref ref;
620242ae
MS
109};
110
111static LIST_HEAD(acpi_ioremaps);
7bbb8903 112static DEFINE_MUTEX(acpi_ioremap_lock);
620242ae 113
b0ed7a91 114static void __init acpi_osi_setup_late(void);
ae00d812 115
d4b7dc49 116/*
a6e0887f 117 * The story of _OSI(Linux)
d4b7dc49 118 *
a6e0887f
LB
119 * From pre-history through Linux-2.6.22,
120 * Linux responded TRUE upon a BIOS OSI(Linux) query.
d4b7dc49 121 *
a6e0887f
LB
122 * Unfortunately, reference BIOS writers got wind of this
123 * and put OSI(Linux) in their example code, quickly exposing
124 * this string as ill-conceived and opening the door to
125 * an un-bounded number of BIOS incompatibilities.
d4b7dc49 126 *
a6e0887f
LB
127 * For example, OSI(Linux) was used on resume to re-POST a
128 * video card on one system, because Linux at that time
129 * could not do a speedy restore in its native driver.
130 * But then upon gaining quick native restore capability,
131 * Linux has no way to tell the BIOS to skip the time-consuming
132 * POST -- putting Linux at a permanent performance disadvantage.
133 * On another system, the BIOS writer used OSI(Linux)
134 * to infer native OS support for IPMI! On other systems,
135 * OSI(Linux) simply got in the way of Linux claiming to
136 * be compatible with other operating systems, exposing
137 * BIOS issues such as skipped device initialization.
d4b7dc49 138 *
a6e0887f
LB
139 * So "Linux" turned out to be a really poor chose of
140 * OSI string, and from Linux-2.6.23 onward we respond FALSE.
d4b7dc49
LB
141 *
142 * BIOS writers should NOT query _OSI(Linux) on future systems.
a6e0887f
LB
143 * Linux will complain on the console when it sees it, and return FALSE.
144 * To get Linux to return TRUE for your system will require
145 * a kernel source update to add a DMI entry,
146 * or boot with "acpi_osi=Linux"
d4b7dc49 147 */
d4b7dc49 148
1d15d84e 149static struct osi_linux {
d4b7dc49
LB
150 unsigned int enable:1;
151 unsigned int dmi:1;
152 unsigned int cmdline:1;
d90aa92c 153} osi_linux = {0, 0, 0};
f507654d 154
b0ed7a91
LM
155static u32 acpi_osi_handler(acpi_string interface, u32 supported)
156{
157 if (!strcmp("Linux", interface)) {
158
3af283e1 159 printk(KERN_NOTICE FW_BUG PREFIX
b0ed7a91
LM
160 "BIOS _OSI(Linux) query %s%s\n",
161 osi_linux.enable ? "honored" : "ignored",
162 osi_linux.cmdline ? " via cmdline" :
163 osi_linux.dmi ? " via DMI" : "");
164 }
165
166 return supported;
167}
168
9a47cdb1
BH
169static void __init acpi_request_region (struct acpi_generic_address *addr,
170 unsigned int length, char *desc)
171{
9a47cdb1
BH
172 if (!addr->address || !length)
173 return;
174
cfa806f0 175 /* Resources are never freed */
eee3c859 176 if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
cfa806f0 177 request_region(addr->address, length, desc);
eee3c859 178 else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
cfa806f0 179 request_mem_region(addr->address, length, desc);
9a47cdb1
BH
180}
181
182static int __init acpi_reserve_resources(void)
183{
eee3c859 184 acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
9a47cdb1
BH
185 "ACPI PM1a_EVT_BLK");
186
eee3c859 187 acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
9a47cdb1
BH
188 "ACPI PM1b_EVT_BLK");
189
eee3c859 190 acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
9a47cdb1
BH
191 "ACPI PM1a_CNT_BLK");
192
eee3c859 193 acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
9a47cdb1
BH
194 "ACPI PM1b_CNT_BLK");
195
eee3c859
LB
196 if (acpi_gbl_FADT.pm_timer_length == 4)
197 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
9a47cdb1 198
eee3c859 199 acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
9a47cdb1
BH
200 "ACPI PM2_CNT_BLK");
201
202 /* Length of GPE blocks must be a non-negative multiple of 2 */
203
eee3c859
LB
204 if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
205 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
206 acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
9a47cdb1 207
eee3c859
LB
208 if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
209 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
210 acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
9a47cdb1
BH
211
212 return 0;
213}
214device_initcall(acpi_reserve_resources);
215
4be44fcd 216void acpi_os_printf(const char *fmt, ...)
1da177e4
LT
217{
218 va_list args;
219 va_start(args, fmt);
220 acpi_os_vprintf(fmt, args);
221 va_end(args);
222}
4be44fcd 223
4be44fcd 224void acpi_os_vprintf(const char *fmt, va_list args)
1da177e4
LT
225{
226 static char buffer[512];
4be44fcd 227
1da177e4
LT
228 vsprintf(buffer, fmt, args);
229
230#ifdef ENABLE_DEBUGGER
231 if (acpi_in_debugger) {
232 kdb_printf("%s", buffer);
233 } else {
4d939155 234 printk(KERN_CONT "%s", buffer);
1da177e4
LT
235 }
236#else
4d939155 237 printk(KERN_CONT "%s", buffer);
1da177e4
LT
238#endif
239}
240
ad71860a 241acpi_physical_address __init acpi_os_get_root_pointer(void)
1da177e4
LT
242{
243 if (efi_enabled) {
b2c99e3c 244 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
ad71860a 245 return efi.acpi20;
b2c99e3c 246 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
ad71860a 247 return efi.acpi;
1da177e4 248 else {
4be44fcd
LB
249 printk(KERN_ERR PREFIX
250 "System description tables not found\n");
ad71860a 251 return 0;
1da177e4 252 }
239665a3
LB
253 } else {
254 acpi_physical_address pa = 0;
255
256 acpi_find_root_pointer(&pa);
257 return pa;
258 }
1da177e4
LT
259}
260
78cdb3ed 261/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
4a3cba5e
MS
262static struct acpi_ioremap *
263acpi_map_lookup(acpi_physical_address phys, acpi_size size)
620242ae
MS
264{
265 struct acpi_ioremap *map;
266
78cdb3ed 267 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
620242ae
MS
268 if (map->phys <= phys &&
269 phys + size <= map->phys + map->size)
4a3cba5e
MS
270 return map;
271
272 return NULL;
273}
274
275/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
276static void __iomem *
277acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
278{
279 struct acpi_ioremap *map;
280
281 map = acpi_map_lookup(phys, size);
282 if (map)
283 return map->virt + (phys - map->phys);
620242ae
MS
284
285 return NULL;
286}
287
78cdb3ed 288/* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
620242ae
MS
289static struct acpi_ioremap *
290acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
291{
292 struct acpi_ioremap *map;
293
78cdb3ed 294 list_for_each_entry_rcu(map, &acpi_ioremaps, list)
4a3cba5e
MS
295 if (map->virt <= virt &&
296 virt + size <= map->virt + map->size)
620242ae
MS
297 return map;
298
299 return NULL;
300}
301
2fdf0741
JB
302void __iomem *__init_refok
303acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
1da177e4 304{
4a3cba5e 305 struct acpi_ioremap *map, *tmp_map;
620242ae 306 void __iomem *virt;
2d6d9fd3
RW
307 acpi_physical_address pg_off;
308 acpi_size pg_sz;
620242ae 309
9f4fd61f
BH
310 if (phys > ULONG_MAX) {
311 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
70c0846e 312 return NULL;
1da177e4 313 }
620242ae
MS
314
315 if (!acpi_gbl_permanent_mmap)
ad71860a 316 return __acpi_map_table((unsigned long)phys, size);
620242ae
MS
317
318 map = kzalloc(sizeof(*map), GFP_KERNEL);
319 if (!map)
320 return NULL;
321
4a3cba5e
MS
322 pg_off = round_down(phys, PAGE_SIZE);
323 pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
2d6d9fd3 324 virt = acpi_os_ioremap(pg_off, pg_sz);
620242ae
MS
325 if (!virt) {
326 kfree(map);
327 return NULL;
328 }
329
330 INIT_LIST_HEAD(&map->list);
331 map->virt = virt;
4a3cba5e
MS
332 map->phys = pg_off;
333 map->size = pg_sz;
334 kref_init(&map->ref);
620242ae 335
7bbb8903 336 mutex_lock(&acpi_ioremap_lock);
4a3cba5e
MS
337 /* Check if page has already been mapped. */
338 tmp_map = acpi_map_lookup(phys, size);
339 if (tmp_map) {
340 kref_get(&tmp_map->ref);
7bbb8903 341 mutex_unlock(&acpi_ioremap_lock);
4a3cba5e
MS
342 iounmap(map->virt);
343 kfree(map);
344 return tmp_map->virt + (phys - tmp_map->phys);
345 }
78cdb3ed 346 list_add_tail_rcu(&map->list, &acpi_ioremaps);
7bbb8903 347 mutex_unlock(&acpi_ioremap_lock);
620242ae 348
4a3cba5e 349 return map->virt + (phys - map->phys);
1da177e4 350}
55a82ab3 351EXPORT_SYMBOL_GPL(acpi_os_map_memory);
1da177e4 352
4a3cba5e
MS
353static void acpi_kref_del_iomap(struct kref *ref)
354{
355 struct acpi_ioremap *map;
356
357 map = container_of(ref, struct acpi_ioremap, ref);
358 list_del_rcu(&map->list);
359}
360
7fe135dc
RW
361static void acpi_os_remove_map(struct acpi_ioremap *map)
362{
363 synchronize_rcu();
364 iounmap(map->virt);
365 kfree(map);
366}
367
0d3a9cf5 368void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
1da177e4 369{
620242ae 370 struct acpi_ioremap *map;
4a3cba5e 371 int del;
620242ae
MS
372
373 if (!acpi_gbl_permanent_mmap) {
7d97277b 374 __acpi_unmap_table(virt, size);
620242ae
MS
375 return;
376 }
377
7bbb8903 378 mutex_lock(&acpi_ioremap_lock);
620242ae
MS
379 map = acpi_map_lookup_virt(virt, size);
380 if (!map) {
7bbb8903 381 mutex_unlock(&acpi_ioremap_lock);
7fe135dc 382 WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
620242ae
MS
383 return;
384 }
4a3cba5e 385 del = kref_put(&map->ref, acpi_kref_del_iomap);
7bbb8903 386 mutex_unlock(&acpi_ioremap_lock);
620242ae 387
7fe135dc
RW
388 if (del)
389 acpi_os_remove_map(map);
1da177e4 390}
55a82ab3 391EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
1da177e4 392
0d3a9cf5 393void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
7d97277b
YL
394{
395 if (!acpi_gbl_permanent_mmap)
396 __acpi_unmap_table(virt, size);
397}
398
073b4964 399static int acpi_os_map_generic_address(struct acpi_generic_address *addr)
29718521
MS
400{
401 void __iomem *virt;
402
403 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
404 return 0;
405
406 if (!addr->address || !addr->bit_width)
407 return -EINVAL;
408
409 virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
410 if (!virt)
411 return -EIO;
412
413 return 0;
414}
29718521 415
073b4964 416static void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
29718521 417{
7fe135dc
RW
418 struct acpi_ioremap *map;
419 int del;
29718521
MS
420
421 if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
422 return;
423
424 if (!addr->address || !addr->bit_width)
425 return;
426
7bbb8903 427 mutex_lock(&acpi_ioremap_lock);
7fe135dc
RW
428 map = acpi_map_lookup(addr->address, addr->bit_width / 8);
429 if (!map) {
430 mutex_unlock(&acpi_ioremap_lock);
431 return;
432 }
433 del = kref_put(&map->ref, acpi_kref_del_iomap);
7bbb8903 434 mutex_unlock(&acpi_ioremap_lock);
29718521 435
7fe135dc
RW
436 if (del)
437 acpi_os_remove_map(map);
29718521 438}
29718521 439
1da177e4
LT
440#ifdef ACPI_FUTURE_USAGE
441acpi_status
4be44fcd 442acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
1da177e4 443{
4be44fcd 444 if (!phys || !virt)
1da177e4
LT
445 return AE_BAD_PARAMETER;
446
447 *phys = virt_to_phys(virt);
448
449 return AE_OK;
450}
451#endif
452
453#define ACPI_MAX_OVERRIDE_LEN 100
454
455static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
456
457acpi_status
4be44fcd
LB
458acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
459 acpi_string * new_val)
1da177e4
LT
460{
461 if (!init_val || !new_val)
462 return AE_BAD_PARAMETER;
463
464 *new_val = NULL;
4be44fcd 465 if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
1da177e4 466 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
4be44fcd 467 acpi_os_name);
1da177e4
LT
468 *new_val = acpi_os_name;
469 }
470
471 return AE_OK;
472}
473
474acpi_status
4be44fcd
LB
475acpi_os_table_override(struct acpi_table_header * existing_table,
476 struct acpi_table_header ** new_table)
1da177e4
LT
477{
478 if (!existing_table || !new_table)
479 return AE_BAD_PARAMETER;
480
71fc47a9
MG
481 *new_table = NULL;
482
1da177e4
LT
483#ifdef CONFIG_ACPI_CUSTOM_DSDT
484 if (strncmp(existing_table->signature, "DSDT", 4) == 0)
4be44fcd 485 *new_table = (struct acpi_table_header *)AmlCode;
1da177e4 486#endif
6ed31e92
ÉP
487 if (*new_table != NULL) {
488 printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
489 "this is unsafe: tainting kernel\n",
490 existing_table->signature,
491 existing_table->oem_table_id);
492 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
493 }
1da177e4
LT
494 return AE_OK;
495}
496
7d12e780 497static irqreturn_t acpi_irq(int irq, void *dev_id)
1da177e4 498{
5229e87d
LB
499 u32 handled;
500
501 handled = (*acpi_irq_handler) (acpi_irq_context);
502
503 if (handled) {
504 acpi_irq_handled++;
505 return IRQ_HANDLED;
88bea188
LB
506 } else {
507 acpi_irq_not_handled++;
5229e87d 508 return IRQ_NONE;
88bea188 509 }
1da177e4
LT
510}
511
512acpi_status
4be44fcd
LB
513acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
514 void *context)
1da177e4
LT
515{
516 unsigned int irq;
517
5229e87d
LB
518 acpi_irq_stats_init();
519
1da177e4
LT
520 /*
521 * Ignore the GSI from the core, and use the value in our copy of the
522 * FADT. It may not be the same if an interrupt source override exists
523 * for the SCI.
524 */
cee324b1 525 gsi = acpi_gbl_FADT.sci_interrupt;
1da177e4
LT
526 if (acpi_gsi_to_irq(gsi, &irq) < 0) {
527 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
528 gsi);
529 return AE_OK;
530 }
531
532 acpi_irq_handler = handler;
533 acpi_irq_context = context;
dace1453 534 if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
1da177e4
LT
535 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
536 return AE_NOT_ACQUIRED;
537 }
538 acpi_irq_irq = irq;
539
540 return AE_OK;
541}
542
4be44fcd 543acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
1da177e4
LT
544{
545 if (irq) {
546 free_irq(irq, acpi_irq);
547 acpi_irq_handler = NULL;
548 acpi_irq_irq = 0;
549 }
550
551 return AE_OK;
552}
553
554/*
555 * Running in interpreter thread context, safe to sleep
556 */
557
439913ff 558void acpi_os_sleep(u64 ms)
1da177e4 559{
01a527ec 560 schedule_timeout_interruptible(msecs_to_jiffies(ms));
1da177e4 561}
4be44fcd 562
4be44fcd 563void acpi_os_stall(u32 us)
1da177e4
LT
564{
565 while (us) {
566 u32 delay = 1000;
567
568 if (delay > us)
569 delay = us;
570 udelay(delay);
571 touch_nmi_watchdog();
572 us -= delay;
573 }
574}
4be44fcd 575
1da177e4
LT
576/*
577 * Support ACPI 3.0 AML Timer operand
578 * Returns 64-bit free-running, monotonically increasing timer
579 * with 100ns granularity
580 */
4be44fcd 581u64 acpi_os_get_timer(void)
1da177e4
LT
582{
583 static u64 t;
584
585#ifdef CONFIG_HPET
586 /* TBD: use HPET if available */
587#endif
588
589#ifdef CONFIG_X86_PM_TIMER
590 /* TBD: default to PM timer if HPET was not available */
591#endif
592 if (!t)
593 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
594
595 return ++t;
596}
597
4be44fcd 598acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
1da177e4
LT
599{
600 u32 dummy;
601
602 if (!value)
603 value = &dummy;
604
49fbabf5
ZY
605 *value = 0;
606 if (width <= 8) {
4be44fcd 607 *(u8 *) value = inb(port);
49fbabf5 608 } else if (width <= 16) {
4be44fcd 609 *(u16 *) value = inw(port);
49fbabf5 610 } else if (width <= 32) {
4be44fcd 611 *(u32 *) value = inl(port);
49fbabf5 612 } else {
1da177e4
LT
613 BUG();
614 }
615
616 return AE_OK;
617}
4be44fcd 618
1da177e4
LT
619EXPORT_SYMBOL(acpi_os_read_port);
620
4be44fcd 621acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
1da177e4 622{
49fbabf5 623 if (width <= 8) {
1da177e4 624 outb(value, port);
49fbabf5 625 } else if (width <= 16) {
1da177e4 626 outw(value, port);
49fbabf5 627 } else if (width <= 32) {
1da177e4 628 outl(value, port);
49fbabf5 629 } else {
1da177e4
LT
630 BUG();
631 }
632
633 return AE_OK;
634}
4be44fcd 635
1da177e4
LT
636EXPORT_SYMBOL(acpi_os_write_port);
637
638acpi_status
4be44fcd 639acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
1da177e4 640{
4be44fcd 641 void __iomem *virt_addr;
884b821f
RW
642 unsigned int size = width / 8;
643 bool unmap = false;
644 u32 dummy;
620242ae 645
78cdb3ed 646 rcu_read_lock();
620242ae 647 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
620242ae 648 if (!virt_addr) {
884b821f 649 rcu_read_unlock();
2d6d9fd3 650 virt_addr = acpi_os_ioremap(phys_addr, size);
884b821f
RW
651 if (!virt_addr)
652 return AE_BAD_ADDRESS;
653 unmap = true;
620242ae 654 }
884b821f 655
1da177e4
LT
656 if (!value)
657 value = &dummy;
658
659 switch (width) {
660 case 8:
4be44fcd 661 *(u8 *) value = readb(virt_addr);
1da177e4
LT
662 break;
663 case 16:
4be44fcd 664 *(u16 *) value = readw(virt_addr);
1da177e4
LT
665 break;
666 case 32:
4be44fcd 667 *(u32 *) value = readl(virt_addr);
1da177e4
LT
668 break;
669 default:
670 BUG();
671 }
672
620242ae
MS
673 if (unmap)
674 iounmap(virt_addr);
884b821f
RW
675 else
676 rcu_read_unlock();
1da177e4
LT
677
678 return AE_OK;
679}
680
681acpi_status
4be44fcd 682acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
1da177e4 683{
4be44fcd 684 void __iomem *virt_addr;
884b821f
RW
685 unsigned int size = width / 8;
686 bool unmap = false;
620242ae 687
78cdb3ed 688 rcu_read_lock();
620242ae 689 virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
620242ae 690 if (!virt_addr) {
884b821f 691 rcu_read_unlock();
2d6d9fd3 692 virt_addr = acpi_os_ioremap(phys_addr, size);
884b821f
RW
693 if (!virt_addr)
694 return AE_BAD_ADDRESS;
695 unmap = true;
620242ae 696 }
1da177e4
LT
697
698 switch (width) {
699 case 8:
700 writeb(value, virt_addr);
701 break;
702 case 16:
703 writew(value, virt_addr);
704 break;
705 case 32:
706 writel(value, virt_addr);
707 break;
708 default:
709 BUG();
710 }
711
620242ae
MS
712 if (unmap)
713 iounmap(virt_addr);
884b821f
RW
714 else
715 rcu_read_unlock();
1da177e4
LT
716
717 return AE_OK;
718}
719
1da177e4 720acpi_status
4be44fcd 721acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
c5f0231e 722 u64 *value, u32 width)
1da177e4
LT
723{
724 int result, size;
c5f0231e 725 u32 value32;
1da177e4
LT
726
727 if (!value)
728 return AE_BAD_PARAMETER;
729
730 switch (width) {
731 case 8:
732 size = 1;
733 break;
734 case 16:
735 size = 2;
736 break;
737 case 32:
738 size = 4;
739 break;
740 default:
741 return AE_ERROR;
742 }
743
b6ce068a
MW
744 result = raw_pci_read(pci_id->segment, pci_id->bus,
745 PCI_DEVFN(pci_id->device, pci_id->function),
c5f0231e
BM
746 reg, size, &value32);
747 *value = value32;
1da177e4
LT
748
749 return (result ? AE_ERROR : AE_OK);
750}
4be44fcd 751
1da177e4 752acpi_status
4be44fcd 753acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
439913ff 754 u64 value, u32 width)
1da177e4
LT
755{
756 int result, size;
757
758 switch (width) {
759 case 8:
760 size = 1;
761 break;
762 case 16:
763 size = 2;
764 break;
765 case 32:
766 size = 4;
767 break;
768 default:
769 return AE_ERROR;
770 }
771
b6ce068a
MW
772 result = raw_pci_write(pci_id->segment, pci_id->bus,
773 PCI_DEVFN(pci_id->device, pci_id->function),
774 reg, size, value);
1da177e4
LT
775
776 return (result ? AE_ERROR : AE_OK);
777}
778
65f27f38 779static void acpi_os_execute_deferred(struct work_struct *work)
88db5e14
AS
780{
781 struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
88db5e14 782
9ac61856
BH
783 if (dpc->wait)
784 acpi_os_wait_events_complete(NULL);
19cd847a
ZR
785
786 dpc->function(dpc->context);
787 kfree(dpc);
19cd847a
ZR
788}
789
b8d35192
AS
790/*******************************************************************************
791 *
792 * FUNCTION: acpi_os_execute
793 *
794 * PARAMETERS: Type - Type of the callback
795 * Function - Function to be executed
796 * Context - Function parameters
797 *
798 * RETURN: Status
799 *
800 * DESCRIPTION: Depending on type, either queues function for deferred execution or
801 * immediately executes function on a separate thread.
802 *
803 ******************************************************************************/
804
19cd847a
ZR
805static acpi_status __acpi_os_execute(acpi_execute_type type,
806 acpi_osd_exec_callback function, void *context, int hp)
1da177e4 807{
4be44fcd
LB
808 acpi_status status = AE_OK;
809 struct acpi_os_dpc *dpc;
17bc54ee 810 struct workqueue_struct *queue;
19cd847a 811 int ret;
72945b2b
LB
812 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
813 "Scheduling function [%p(%p)] for deferred execution.\n",
814 function, context));
1da177e4 815
1da177e4
LT
816 /*
817 * Allocate/initialize DPC structure. Note that this memory will be
65f27f38 818 * freed by the callee. The kernel handles the work_struct list in a
1da177e4
LT
819 * way that allows us to also free its memory inside the callee.
820 * Because we may want to schedule several tasks with different
821 * parameters we can't use the approach some kernel code uses of
65f27f38 822 * having a static work_struct.
1da177e4 823 */
72945b2b 824
65f27f38 825 dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
1da177e4 826 if (!dpc)
889c78be 827 return AE_NO_MEMORY;
b976fe19 828
1da177e4
LT
829 dpc->function = function;
830 dpc->context = context;
b976fe19 831
c02256be
ZR
832 /*
833 * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
834 * because the hotplug code may call driver .remove() functions,
835 * which invoke flush_scheduled_work/acpi_os_wait_events_complete
836 * to flush these workqueues.
837 */
838 queue = hp ? kacpi_hotplug_wq :
839 (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
9ac61856 840 dpc->wait = hp ? 1 : 0;
bc73675b
ZR
841
842 if (queue == kacpi_hotplug_wq)
843 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
844 else if (queue == kacpi_notify_wq)
845 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
846 else
847 INIT_WORK(&dpc->work, acpi_os_execute_deferred);
848
8fec62b2
TH
849 /*
850 * On some machines, a software-initiated SMI causes corruption unless
851 * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
852 * typically it's done in GPE-related methods that are run via
853 * workqueues, so we can avoid the known corruption cases by always
854 * queueing on CPU 0.
855 */
856 ret = queue_work_on(0, queue, &dpc->work);
19cd847a
ZR
857
858 if (!ret) {
55ac9a01
LM
859 printk(KERN_ERR PREFIX
860 "Call to queue_work() failed.\n");
17bc54ee
AS
861 status = AE_ERROR;
862 kfree(dpc);
1da177e4 863 }
889c78be 864 return status;
1da177e4 865}
4be44fcd 866
19cd847a
ZR
867acpi_status acpi_os_execute(acpi_execute_type type,
868 acpi_osd_exec_callback function, void *context)
869{
870 return __acpi_os_execute(type, function, context, 0);
871}
b8d35192 872EXPORT_SYMBOL(acpi_os_execute);
1da177e4 873
19cd847a
ZR
874acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
875 void *context)
876{
877 return __acpi_os_execute(0, function, context, 1);
878}
879
4be44fcd 880void acpi_os_wait_events_complete(void *context)
1da177e4
LT
881{
882 flush_workqueue(kacpid_wq);
2f67a069 883 flush_workqueue(kacpi_notify_wq);
1da177e4 884}
4be44fcd 885
1da177e4
LT
886EXPORT_SYMBOL(acpi_os_wait_events_complete);
887
1da177e4
LT
888/*
889 * Deallocate the memory for a spinlock.
890 */
967440e3 891void acpi_os_delete_lock(acpi_spinlock handle)
1da177e4 892{
d550d98d 893 return;
1da177e4
LT
894}
895
1da177e4 896acpi_status
4be44fcd 897acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
1da177e4 898{
4be44fcd 899 struct semaphore *sem = NULL;
1da177e4 900
1da177e4
LT
901 sem = acpi_os_allocate(sizeof(struct semaphore));
902 if (!sem)
d550d98d 903 return AE_NO_MEMORY;
1da177e4
LT
904 memset(sem, 0, sizeof(struct semaphore));
905
906 sema_init(sem, initial_units);
907
4be44fcd 908 *handle = (acpi_handle *) sem;
1da177e4 909
4be44fcd
LB
910 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
911 *handle, initial_units));
1da177e4 912
d550d98d 913 return AE_OK;
1da177e4 914}
1da177e4 915
1da177e4
LT
916/*
917 * TODO: A better way to delete semaphores? Linux doesn't have a
918 * 'delete_semaphore()' function -- may result in an invalid
919 * pointer dereference for non-synchronized consumers. Should
920 * we at least check for blocked threads and signal/cancel them?
921 */
922
4be44fcd 923acpi_status acpi_os_delete_semaphore(acpi_handle handle)
1da177e4 924{
4be44fcd 925 struct semaphore *sem = (struct semaphore *)handle;
1da177e4 926
1da177e4 927 if (!sem)
d550d98d 928 return AE_BAD_PARAMETER;
1da177e4 929
4be44fcd 930 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
1da177e4 931
f1241c87 932 BUG_ON(!list_empty(&sem->wait_list));
02438d87 933 kfree(sem);
4be44fcd 934 sem = NULL;
1da177e4 935
d550d98d 936 return AE_OK;
1da177e4 937}
1da177e4 938
1da177e4 939/*
1da177e4
LT
940 * TODO: Support for units > 1?
941 */
4be44fcd 942acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
1da177e4 943{
4be44fcd
LB
944 acpi_status status = AE_OK;
945 struct semaphore *sem = (struct semaphore *)handle;
f1241c87 946 long jiffies;
4be44fcd 947 int ret = 0;
1da177e4 948
1da177e4 949 if (!sem || (units < 1))
d550d98d 950 return AE_BAD_PARAMETER;
1da177e4
LT
951
952 if (units > 1)
d550d98d 953 return AE_SUPPORT;
1da177e4 954
4be44fcd
LB
955 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
956 handle, units, timeout));
1da177e4 957
f1241c87
MW
958 if (timeout == ACPI_WAIT_FOREVER)
959 jiffies = MAX_SCHEDULE_TIMEOUT;
960 else
961 jiffies = msecs_to_jiffies(timeout);
962
963 ret = down_timeout(sem, jiffies);
964 if (ret)
965 status = AE_TIME;
1da177e4
LT
966
967 if (ACPI_FAILURE(status)) {
9e7e2c04 968 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
a6fc6720 969 "Failed to acquire semaphore[%p|%d|%d], %s",
4be44fcd
LB
970 handle, units, timeout,
971 acpi_format_exception(status)));
972 } else {
973 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
a6fc6720 974 "Acquired semaphore[%p|%d|%d]", handle,
4be44fcd 975 units, timeout));
1da177e4
LT
976 }
977
d550d98d 978 return status;
1da177e4 979}
1da177e4 980
1da177e4
LT
981/*
982 * TODO: Support for units > 1?
983 */
4be44fcd 984acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
1da177e4 985{
4be44fcd 986 struct semaphore *sem = (struct semaphore *)handle;
1da177e4 987
1da177e4 988 if (!sem || (units < 1))
d550d98d 989 return AE_BAD_PARAMETER;
1da177e4
LT
990
991 if (units > 1)
d550d98d 992 return AE_SUPPORT;
1da177e4 993
4be44fcd
LB
994 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
995 units));
1da177e4
LT
996
997 up(sem);
998
d550d98d 999 return AE_OK;
1da177e4 1000}
4be44fcd 1001
1da177e4 1002#ifdef ACPI_FUTURE_USAGE
4be44fcd 1003u32 acpi_os_get_line(char *buffer)
1da177e4
LT
1004{
1005
1006#ifdef ENABLE_DEBUGGER
1007 if (acpi_in_debugger) {
1008 u32 chars;
1009
1010 kdb_read(buffer, sizeof(line_buf));
1011
1012 /* remove the CR kdb includes */
1013 chars = strlen(buffer) - 1;
1014 buffer[chars] = '\0';
1015 }
1016#endif
1017
1018 return 0;
1019}
4be44fcd 1020#endif /* ACPI_FUTURE_USAGE */
1da177e4 1021
4be44fcd 1022acpi_status acpi_os_signal(u32 function, void *info)
1da177e4 1023{
4be44fcd 1024 switch (function) {
1da177e4
LT
1025 case ACPI_SIGNAL_FATAL:
1026 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
1027 break;
1028 case ACPI_SIGNAL_BREAKPOINT:
1029 /*
1030 * AML Breakpoint
1031 * ACPI spec. says to treat it as a NOP unless
1032 * you are debugging. So if/when we integrate
1033 * AML debugger into the kernel debugger its
1034 * hook will go here. But until then it is
1035 * not useful to print anything on breakpoints.
1036 */
1037 break;
1038 default:
1039 break;
1040 }
1041
1042 return AE_OK;
1043}
4be44fcd 1044
4be44fcd 1045static int __init acpi_os_name_setup(char *str)
1da177e4
LT
1046{
1047 char *p = acpi_os_name;
4be44fcd 1048 int count = ACPI_MAX_OVERRIDE_LEN - 1;
1da177e4
LT
1049
1050 if (!str || !*str)
1051 return 0;
1052
1053 for (; count-- && str && *str; str++) {
1054 if (isalnum(*str) || *str == ' ' || *str == ':')
1055 *p++ = *str;
1056 else if (*str == '\'' || *str == '"')
1057 continue;
1058 else
1059 break;
1060 }
1061 *p = 0;
1062
1063 return 1;
4be44fcd 1064
1da177e4
LT
1065}
1066
1067__setup("acpi_os_name=", acpi_os_name_setup);
1068
12d32064
LM
1069#define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
1070#define OSI_STRING_ENTRIES_MAX 16 /* arbitrary */
1071
1072struct osi_setup_entry {
1073 char string[OSI_STRING_LENGTH_MAX];
1074 bool enable;
1075};
1076
1077static struct osi_setup_entry __initdata osi_setup_entries[OSI_STRING_ENTRIES_MAX];
1078
d90aa92c
LM
1079void __init acpi_osi_setup(char *str)
1080{
12d32064
LM
1081 struct osi_setup_entry *osi;
1082 bool enable = true;
1083 int i;
1084
d90aa92c
LM
1085 if (!acpi_gbl_create_osi_method)
1086 return;
1087
1088 if (str == NULL || *str == '\0') {
1089 printk(KERN_INFO PREFIX "_OSI method disabled\n");
1090 acpi_gbl_create_osi_method = FALSE;
12d32064
LM
1091 return;
1092 }
1093
1094 if (*str == '!') {
1095 str++;
1096 enable = false;
1097 }
1098
1099 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1100 osi = &osi_setup_entries[i];
1101 if (!strcmp(osi->string, str)) {
1102 osi->enable = enable;
1103 break;
1104 } else if (osi->string[0] == '\0') {
1105 osi->enable = enable;
1106 strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
1107 break;
1108 }
1109 }
d90aa92c
LM
1110}
1111
d4b7dc49
LB
1112static void __init set_osi_linux(unsigned int enable)
1113{
d90aa92c 1114 if (osi_linux.enable != enable)
d4b7dc49 1115 osi_linux.enable = enable;
b0ed7a91
LM
1116
1117 if (osi_linux.enable)
1118 acpi_osi_setup("Linux");
1119 else
1120 acpi_osi_setup("!Linux");
1121
d4b7dc49
LB
1122 return;
1123}
1124
1125static void __init acpi_cmdline_osi_linux(unsigned int enable)
1126{
d90aa92c
LM
1127 osi_linux.cmdline = 1; /* cmdline set the default and override DMI */
1128 osi_linux.dmi = 0;
d4b7dc49
LB
1129 set_osi_linux(enable);
1130
1131 return;
1132}
1133
1134void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
1135{
d4b7dc49
LB
1136 printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
1137
1138 if (enable == -1)
1139 return;
1140
d90aa92c 1141 osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
d4b7dc49 1142 set_osi_linux(enable);
f507654d 1143
f507654d
LB
1144 return;
1145}
1146
1da177e4 1147/*
ae00d812
LB
1148 * Modify the list of "OS Interfaces" reported to BIOS via _OSI
1149 *
1da177e4 1150 * empty string disables _OSI
ae00d812
LB
1151 * string starting with '!' disables that string
1152 * otherwise string is added to list, augmenting built-in strings
1da177e4 1153 */
b0ed7a91 1154static void __init acpi_osi_setup_late(void)
1da177e4 1155{
12d32064
LM
1156 struct osi_setup_entry *osi;
1157 char *str;
1158 int i;
d90aa92c 1159 acpi_status status;
b0ed7a91 1160
12d32064
LM
1161 for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
1162 osi = &osi_setup_entries[i];
1163 str = osi->string;
b0ed7a91 1164
12d32064
LM
1165 if (*str == '\0')
1166 break;
1167 if (osi->enable) {
1168 status = acpi_install_interface(str);
d90aa92c 1169
12d32064
LM
1170 if (ACPI_SUCCESS(status))
1171 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
1172 } else {
1173 status = acpi_remove_interface(str);
d90aa92c 1174
12d32064
LM
1175 if (ACPI_SUCCESS(status))
1176 printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
1177 }
b0ed7a91
LM
1178 }
1179}
1180
d90aa92c 1181static int __init osi_setup(char *str)
b0ed7a91 1182{
d90aa92c
LM
1183 if (str && !strcmp("Linux", str))
1184 acpi_cmdline_osi_linux(1);
1185 else if (str && !strcmp("!Linux", str))
1186 acpi_cmdline_osi_linux(0);
1187 else
1188 acpi_osi_setup(str);
1da177e4
LT
1189
1190 return 1;
1191}
1192
d90aa92c 1193__setup("acpi_osi=", osi_setup);
1da177e4
LT
1194
1195/* enable serialization to combat AE_ALREADY_EXISTS errors */
4be44fcd 1196static int __init acpi_serialize_setup(char *str)
1da177e4
LT
1197{
1198 printk(KERN_INFO PREFIX "serialize enabled\n");
1199
1200 acpi_gbl_all_methods_serialized = TRUE;
1201
1202 return 1;
1203}
1204
1205__setup("acpi_serialize", acpi_serialize_setup);
1206
df92e695
TR
1207/* Check of resource interference between native drivers and ACPI
1208 * OperationRegions (SystemIO and System Memory only).
1209 * IO ports and memory declared in ACPI might be used by the ACPI subsystem
1210 * in arbitrary AML code and can interfere with legacy drivers.
1211 * acpi_enforce_resources= can be set to:
1212 *
7e90560c 1213 * - strict (default) (2)
df92e695 1214 * -> further driver trying to access the resources will not load
7e90560c 1215 * - lax (1)
df92e695
TR
1216 * -> further driver trying to access the resources will load, but you
1217 * get a system message that something might go wrong...
1218 *
1219 * - no (0)
1220 * -> ACPI Operation Region resources will not be registered
1221 *
1222 */
1223#define ENFORCE_RESOURCES_STRICT 2
1224#define ENFORCE_RESOURCES_LAX 1
1225#define ENFORCE_RESOURCES_NO 0
1226
7e90560c 1227static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
df92e695
TR
1228
1229static int __init acpi_enforce_resources_setup(char *str)
1230{
1231 if (str == NULL || *str == '\0')
1232 return 0;
1233
1234 if (!strcmp("strict", str))
1235 acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
1236 else if (!strcmp("lax", str))
1237 acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
1238 else if (!strcmp("no", str))
1239 acpi_enforce_resources = ENFORCE_RESOURCES_NO;
1240
1241 return 1;
1242}
1243
1244__setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
1245
1246/* Check for resource conflicts between ACPI OperationRegions and native
1247 * drivers */
876fba43 1248int acpi_check_resource_conflict(const struct resource *res)
df92e695
TR
1249{
1250 struct acpi_res_list *res_list_elem;
106d1a0a 1251 int ioport = 0, clash = 0;
df92e695
TR
1252
1253 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1254 return 0;
1255 if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
1256 return 0;
1257
1258 ioport = res->flags & IORESOURCE_IO;
1259
1260 spin_lock(&acpi_res_lock);
1261 list_for_each_entry(res_list_elem, &resource_list_head,
1262 resource_list) {
1263 if (ioport && (res_list_elem->resource_type
1264 != ACPI_ADR_SPACE_SYSTEM_IO))
1265 continue;
1266 if (!ioport && (res_list_elem->resource_type
1267 != ACPI_ADR_SPACE_SYSTEM_MEMORY))
1268 continue;
1269
1270 if (res->end < res_list_elem->start
1271 || res_list_elem->end < res->start)
1272 continue;
1273 clash = 1;
1274 break;
1275 }
1276 spin_unlock(&acpi_res_lock);
1277
1278 if (clash) {
1279 if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
1638bca8 1280 printk(KERN_WARNING "ACPI: resource %s %pR"
106d1a0a
TR
1281 " conflicts with ACPI region %s "
1282 "[%s 0x%zx-0x%zx]\n",
1638bca8 1283 res->name, res, res_list_elem->name,
106d1a0a
TR
1284 (res_list_elem->resource_type ==
1285 ACPI_ADR_SPACE_SYSTEM_IO) ? "io" : "mem",
1286 (size_t) res_list_elem->start,
1287 (size_t) res_list_elem->end);
14f03343
JD
1288 if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
1289 printk(KERN_NOTICE "ACPI: This conflict may"
1290 " cause random problems and system"
1291 " instability\n");
1292 printk(KERN_INFO "ACPI: If an ACPI driver is available"
1293 " for this device, you should use it instead of"
1294 " the native driver\n");
df92e695
TR
1295 }
1296 if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
1297 return -EBUSY;
1298 }
1299 return 0;
1300}
443dea72 1301EXPORT_SYMBOL(acpi_check_resource_conflict);
df92e695
TR
1302
1303int acpi_check_region(resource_size_t start, resource_size_t n,
1304 const char *name)
1305{
1306 struct resource res = {
1307 .start = start,
1308 .end = start + n - 1,
1309 .name = name,
1310 .flags = IORESOURCE_IO,
1311 };
1312
1313 return acpi_check_resource_conflict(&res);
1314}
1315EXPORT_SYMBOL(acpi_check_region);
1316
70dd6bea
JD
1317/*
1318 * Let drivers know whether the resource checks are effective
1319 */
1320int acpi_resources_are_enforced(void)
1321{
1322 return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
1323}
1324EXPORT_SYMBOL(acpi_resources_are_enforced);
1325
73459f73
RM
1326/*
1327 * Acquire a spinlock.
1328 *
1329 * handle is a pointer to the spinlock_t.
73459f73
RM
1330 */
1331
967440e3 1332acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
73459f73 1333{
b8e4d893 1334 acpi_cpu_flags flags;
967440e3 1335 spin_lock_irqsave(lockp, flags);
73459f73
RM
1336 return flags;
1337}
1338
1339/*
1340 * Release a spinlock. See above.
1341 */
1342
967440e3 1343void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
73459f73 1344{
967440e3 1345 spin_unlock_irqrestore(lockp, flags);
73459f73
RM
1346}
1347
73459f73
RM
1348#ifndef ACPI_USE_LOCAL_CACHE
1349
1350/*******************************************************************************
1351 *
1352 * FUNCTION: acpi_os_create_cache
1353 *
b229cf92
BM
1354 * PARAMETERS: name - Ascii name for the cache
1355 * size - Size of each cached object
1356 * depth - Maximum depth of the cache (in objects) <ignored>
1357 * cache - Where the new cache object is returned
73459f73 1358 *
b229cf92 1359 * RETURN: status
73459f73
RM
1360 *
1361 * DESCRIPTION: Create a cache object
1362 *
1363 ******************************************************************************/
1364
1365acpi_status
4be44fcd 1366acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
73459f73 1367{
20c2df83 1368 *cache = kmem_cache_create(name, size, 0, 0, NULL);
a6fdbf90 1369 if (*cache == NULL)
b229cf92
BM
1370 return AE_ERROR;
1371 else
1372 return AE_OK;
73459f73
RM
1373}
1374
1375/*******************************************************************************
1376 *
1377 * FUNCTION: acpi_os_purge_cache
1378 *
1379 * PARAMETERS: Cache - Handle to cache object
1380 *
1381 * RETURN: Status
1382 *
1383 * DESCRIPTION: Free all objects within the requested cache.
1384 *
1385 ******************************************************************************/
1386
4be44fcd 1387acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
73459f73 1388{
50dd0969 1389 kmem_cache_shrink(cache);
4be44fcd 1390 return (AE_OK);
73459f73
RM
1391}
1392
1393/*******************************************************************************
1394 *
1395 * FUNCTION: acpi_os_delete_cache
1396 *
1397 * PARAMETERS: Cache - Handle to cache object
1398 *
1399 * RETURN: Status
1400 *
1401 * DESCRIPTION: Free all objects within the requested cache and delete the
1402 * cache object.
1403 *
1404 ******************************************************************************/
1405
4be44fcd 1406acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
73459f73 1407{
1a1d92c1 1408 kmem_cache_destroy(cache);
4be44fcd 1409 return (AE_OK);
73459f73
RM
1410}
1411
1412/*******************************************************************************
1413 *
1414 * FUNCTION: acpi_os_release_object
1415 *
1416 * PARAMETERS: Cache - Handle to cache object
1417 * Object - The object to be released
1418 *
1419 * RETURN: None
1420 *
1421 * DESCRIPTION: Release an object to the specified cache. If cache is full,
1422 * the object is deleted.
1423 *
1424 ******************************************************************************/
1425
4be44fcd 1426acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
73459f73 1427{
4be44fcd
LB
1428 kmem_cache_free(cache, object);
1429 return (AE_OK);
73459f73
RM
1430}
1431
a5fe1a03
LM
1432static inline int acpi_res_list_add(struct acpi_res_list *res)
1433{
1434 struct acpi_res_list *res_list_elem;
1435
1436 list_for_each_entry(res_list_elem, &resource_list_head,
1437 resource_list) {
1438
1439 if (res->resource_type == res_list_elem->resource_type &&
1440 res->start == res_list_elem->start &&
1441 res->end == res_list_elem->end) {
1442
1443 /*
1444 * The Region(addr,len) already exist in the list,
1445 * just increase the count
1446 */
1447
1448 res_list_elem->count++;
1449 return 0;
1450 }
1451 }
1452
1453 res->count = 1;
1454 list_add(&res->resource_list, &resource_list_head);
1455 return 1;
1456}
1457
1458static inline void acpi_res_list_del(struct acpi_res_list *res)
1459{
1460 struct acpi_res_list *res_list_elem;
1461
1462 list_for_each_entry(res_list_elem, &resource_list_head,
1463 resource_list) {
1464
1465 if (res->resource_type == res_list_elem->resource_type &&
1466 res->start == res_list_elem->start &&
1467 res->end == res_list_elem->end) {
1468
1469 /*
1470 * If the res count is decreased to 0,
1471 * remove and free it
1472 */
1473
1474 if (--res_list_elem->count == 0) {
1475 list_del(&res_list_elem->resource_list);
1476 kfree(res_list_elem);
1477 }
1478 return;
1479 }
1480 }
1481}
1482
1483acpi_status
1484acpi_os_invalidate_address(
1485 u8 space_id,
1486 acpi_physical_address address,
1487 acpi_size length)
1488{
1489 struct acpi_res_list res;
1490
1491 switch (space_id) {
1492 case ACPI_ADR_SPACE_SYSTEM_IO:
1493 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
88393161 1494 /* Only interference checks against SystemIO and SystemMemory
a5fe1a03
LM
1495 are needed */
1496 res.start = address;
1497 res.end = address + length - 1;
1498 res.resource_type = space_id;
1499 spin_lock(&acpi_res_lock);
1500 acpi_res_list_del(&res);
1501 spin_unlock(&acpi_res_lock);
1502 break;
1503 case ACPI_ADR_SPACE_PCI_CONFIG:
1504 case ACPI_ADR_SPACE_EC:
1505 case ACPI_ADR_SPACE_SMBUS:
1506 case ACPI_ADR_SPACE_CMOS:
1507 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1508 case ACPI_ADR_SPACE_DATA_TABLE:
1509 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1510 break;
1511 }
1512 return AE_OK;
1513}
1514
b229cf92
BM
1515/******************************************************************************
1516 *
1517 * FUNCTION: acpi_os_validate_address
1518 *
1519 * PARAMETERS: space_id - ACPI space ID
1520 * address - Physical address
1521 * length - Address length
1522 *
1523 * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
1524 * should return AE_AML_ILLEGAL_ADDRESS.
1525 *
1526 * DESCRIPTION: Validate a system address via the host OS. Used to validate
1527 * the addresses accessed by AML operation regions.
1528 *
1529 *****************************************************************************/
1530
1531acpi_status
1532acpi_os_validate_address (
1533 u8 space_id,
1534 acpi_physical_address address,
df92e695
TR
1535 acpi_size length,
1536 char *name)
b229cf92 1537{
df92e695 1538 struct acpi_res_list *res;
a5fe1a03 1539 int added;
df92e695
TR
1540 if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
1541 return AE_OK;
b229cf92 1542
df92e695
TR
1543 switch (space_id) {
1544 case ACPI_ADR_SPACE_SYSTEM_IO:
1545 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
88393161 1546 /* Only interference checks against SystemIO and SystemMemory
df92e695
TR
1547 are needed */
1548 res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
1549 if (!res)
1550 return AE_OK;
1551 /* ACPI names are fixed to 4 bytes, still better use strlcpy */
1552 strlcpy(res->name, name, 5);
1553 res->start = address;
1554 res->end = address + length - 1;
1555 res->resource_type = space_id;
1556 spin_lock(&acpi_res_lock);
a5fe1a03 1557 added = acpi_res_list_add(res);
df92e695 1558 spin_unlock(&acpi_res_lock);
a5fe1a03
LM
1559 pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
1560 "name: %s\n", added ? "Added" : "Already exist",
1561 (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
df92e695
TR
1562 ? "SystemIO" : "System Memory",
1563 (unsigned long long)res->start,
1564 (unsigned long long)res->end,
1565 res->name);
a5fe1a03
LM
1566 if (!added)
1567 kfree(res);
df92e695
TR
1568 break;
1569 case ACPI_ADR_SPACE_PCI_CONFIG:
1570 case ACPI_ADR_SPACE_EC:
1571 case ACPI_ADR_SPACE_SMBUS:
1572 case ACPI_ADR_SPACE_CMOS:
1573 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1574 case ACPI_ADR_SPACE_DATA_TABLE:
1575 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1576 break;
1577 }
1578 return AE_OK;
b229cf92 1579}
73459f73 1580#endif
d362edaf
MS
1581
1582acpi_status __init acpi_os_initialize(void)
1583{
1584 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1585 acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1586 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
1587 acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
1588
1589 return AE_OK;
1590}
1591
32d47eef 1592acpi_status __init acpi_os_initialize1(void)
d362edaf
MS
1593{
1594 kacpid_wq = create_workqueue("kacpid");
1595 kacpi_notify_wq = create_workqueue("kacpi_notify");
1596 kacpi_hotplug_wq = create_workqueue("kacpi_hotplug");
1597 BUG_ON(!kacpid_wq);
1598 BUG_ON(!kacpi_notify_wq);
1599 BUG_ON(!kacpi_hotplug_wq);
1bd64d42
LB
1600 acpi_install_interface_handler(acpi_osi_handler);
1601 acpi_osi_setup_late();
d362edaf
MS
1602 return AE_OK;
1603}
1604
1605acpi_status acpi_os_terminate(void)
1606{
1607 if (acpi_irq_handler) {
1608 acpi_os_remove_interrupt_handler(acpi_irq_irq,
1609 acpi_irq_handler);
1610 }
1611
1612 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
1613 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
1614 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
1615 acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
1616
1617 destroy_workqueue(kacpid_wq);
1618 destroy_workqueue(kacpi_notify_wq);
1619 destroy_workqueue(kacpi_hotplug_wq);
1620
1621 return AE_OK;
1622}