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