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