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