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