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