ACPI: Defer enabling of level GPE until all pending notifies done
[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  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  *
26  */
27
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/mm.h>
32 #include <linux/pci.h>
33 #include <linux/interrupt.h>
34 #include <linux/kmod.h>
35 #include <linux/delay.h>
36 #include <linux/dmi.h>
37 #include <linux/workqueue.h>
38 #include <linux/nmi.h>
39 #include <linux/acpi.h>
40 #include <acpi/acpi.h>
41 #include <asm/io.h>
42 #include <acpi/acpi_bus.h>
43 #include <acpi/processor.h>
44 #include <asm/uaccess.h>
45
46 #include <linux/efi.h>
47
48 #define _COMPONENT              ACPI_OS_SERVICES
49 ACPI_MODULE_NAME("osl");
50 #define PREFIX          "ACPI: "
51 struct acpi_os_dpc {
52         acpi_osd_exec_callback function;
53         void *context;
54         struct work_struct work;
55 };
56
57 #ifdef CONFIG_ACPI_CUSTOM_DSDT
58 #include CONFIG_ACPI_CUSTOM_DSDT_FILE
59 #endif
60
61 #ifdef ENABLE_DEBUGGER
62 #include <linux/kdb.h>
63
64 /* stuff for debugger support */
65 int acpi_in_debugger;
66 EXPORT_SYMBOL(acpi_in_debugger);
67
68 extern char line_buf[80];
69 #endif                          /*ENABLE_DEBUGGER */
70
71 static unsigned int acpi_irq_irq;
72 static acpi_osd_handler acpi_irq_handler;
73 static void *acpi_irq_context;
74 static struct workqueue_struct *kacpid_wq;
75 static struct workqueue_struct *kacpi_notify_wq;
76
77 #define OSI_STRING_LENGTH_MAX 64        /* arbitrary */
78 static char osi_additional_string[OSI_STRING_LENGTH_MAX];
79
80 static int osi_linux;           /* disable _OSI(Linux) by default */
81
82 #ifdef CONFIG_DMI
83 static struct __initdata dmi_system_id acpi_osl_dmi_table[];
84 #endif
85
86 static void __init acpi_request_region (struct acpi_generic_address *addr,
87         unsigned int length, char *desc)
88 {
89         struct resource *res;
90
91         if (!addr->address || !length)
92                 return;
93
94         if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
95                 res = request_region(addr->address, length, desc);
96         else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
97                 res = request_mem_region(addr->address, length, desc);
98 }
99
100 static int __init acpi_reserve_resources(void)
101 {
102         acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
103                 "ACPI PM1a_EVT_BLK");
104
105         acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
106                 "ACPI PM1b_EVT_BLK");
107
108         acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
109                 "ACPI PM1a_CNT_BLK");
110
111         acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
112                 "ACPI PM1b_CNT_BLK");
113
114         if (acpi_gbl_FADT.pm_timer_length == 4)
115                 acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
116
117         acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
118                 "ACPI PM2_CNT_BLK");
119
120         /* Length of GPE blocks must be a non-negative multiple of 2 */
121
122         if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
123                 acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
124                                acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
125
126         if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
127                 acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
128                                acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
129
130         return 0;
131 }
132 device_initcall(acpi_reserve_resources);
133
134 acpi_status __init acpi_os_initialize(void)
135 {
136         dmi_check_system(acpi_osl_dmi_table);
137         return AE_OK;
138 }
139
140 acpi_status acpi_os_initialize1(void)
141 {
142         /*
143          * Initialize PCI configuration space access, as we'll need to access
144          * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
145          */
146         if (!raw_pci_ops) {
147                 printk(KERN_ERR PREFIX
148                        "Access to PCI configuration space unavailable\n");
149                 return AE_NULL_ENTRY;
150         }
151         kacpid_wq = create_singlethread_workqueue("kacpid");
152         kacpi_notify_wq = create_singlethread_workqueue("kacpi_notify");
153         BUG_ON(!kacpid_wq);
154         BUG_ON(!kacpi_notify_wq);
155         return AE_OK;
156 }
157
158 acpi_status acpi_os_terminate(void)
159 {
160         if (acpi_irq_handler) {
161                 acpi_os_remove_interrupt_handler(acpi_irq_irq,
162                                                  acpi_irq_handler);
163         }
164
165         destroy_workqueue(kacpid_wq);
166         destroy_workqueue(kacpi_notify_wq);
167
168         return AE_OK;
169 }
170
171 void acpi_os_printf(const char *fmt, ...)
172 {
173         va_list args;
174         va_start(args, fmt);
175         acpi_os_vprintf(fmt, args);
176         va_end(args);
177 }
178
179 EXPORT_SYMBOL(acpi_os_printf);
180
181 void acpi_os_vprintf(const char *fmt, va_list args)
182 {
183         static char buffer[512];
184
185         vsprintf(buffer, fmt, args);
186
187 #ifdef ENABLE_DEBUGGER
188         if (acpi_in_debugger) {
189                 kdb_printf("%s", buffer);
190         } else {
191                 printk("%s", buffer);
192         }
193 #else
194         printk("%s", buffer);
195 #endif
196 }
197
198 acpi_physical_address __init acpi_os_get_root_pointer(void)
199 {
200         if (efi_enabled) {
201                 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
202                         return efi.acpi20;
203                 else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
204                         return efi.acpi;
205                 else {
206                         printk(KERN_ERR PREFIX
207                                "System description tables not found\n");
208                         return 0;
209                 }
210         } else
211                 return acpi_find_rsdp();
212 }
213
214 void __iomem *acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
215 {
216         if (phys > ULONG_MAX) {
217                 printk(KERN_ERR PREFIX "Cannot map memory that high\n");
218                 return NULL;
219         }
220         if (acpi_gbl_permanent_mmap)
221                 /*
222                 * ioremap checks to ensure this is in reserved space
223                 */
224                 return ioremap((unsigned long)phys, size);
225         else
226                 return __acpi_map_table((unsigned long)phys, size);
227 }
228 EXPORT_SYMBOL_GPL(acpi_os_map_memory);
229
230 void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
231 {
232         if (acpi_gbl_permanent_mmap) {
233                 iounmap(virt);
234         }
235 }
236 EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
237
238 #ifdef ACPI_FUTURE_USAGE
239 acpi_status
240 acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
241 {
242         if (!phys || !virt)
243                 return AE_BAD_PARAMETER;
244
245         *phys = virt_to_phys(virt);
246
247         return AE_OK;
248 }
249 #endif
250
251 #define ACPI_MAX_OVERRIDE_LEN 100
252
253 static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
254
255 acpi_status
256 acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
257                             acpi_string * new_val)
258 {
259         if (!init_val || !new_val)
260                 return AE_BAD_PARAMETER;
261
262         *new_val = NULL;
263         if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
264                 printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
265                        acpi_os_name);
266                 *new_val = acpi_os_name;
267         }
268
269         return AE_OK;
270 }
271
272 acpi_status
273 acpi_os_table_override(struct acpi_table_header * existing_table,
274                        struct acpi_table_header ** new_table)
275 {
276         if (!existing_table || !new_table)
277                 return AE_BAD_PARAMETER;
278
279 #ifdef CONFIG_ACPI_CUSTOM_DSDT
280         if (strncmp(existing_table->signature, "DSDT", 4) == 0)
281                 *new_table = (struct acpi_table_header *)AmlCode;
282         else
283                 *new_table = NULL;
284 #else
285         *new_table = NULL;
286 #endif
287         return AE_OK;
288 }
289
290 static irqreturn_t acpi_irq(int irq, void *dev_id)
291 {
292         return (*acpi_irq_handler) (acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
293 }
294
295 acpi_status
296 acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
297                                   void *context)
298 {
299         unsigned int irq;
300
301         /*
302          * Ignore the GSI from the core, and use the value in our copy of the
303          * FADT. It may not be the same if an interrupt source override exists
304          * for the SCI.
305          */
306         gsi = acpi_gbl_FADT.sci_interrupt;
307         if (acpi_gsi_to_irq(gsi, &irq) < 0) {
308                 printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
309                        gsi);
310                 return AE_OK;
311         }
312
313         acpi_irq_handler = handler;
314         acpi_irq_context = context;
315         if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
316                 printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
317                 return AE_NOT_ACQUIRED;
318         }
319         acpi_irq_irq = irq;
320
321         return AE_OK;
322 }
323
324 acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
325 {
326         if (irq) {
327                 free_irq(irq, acpi_irq);
328                 acpi_irq_handler = NULL;
329                 acpi_irq_irq = 0;
330         }
331
332         return AE_OK;
333 }
334
335 /*
336  * Running in interpreter thread context, safe to sleep
337  */
338
339 void acpi_os_sleep(acpi_integer ms)
340 {
341         schedule_timeout_interruptible(msecs_to_jiffies(ms));
342 }
343
344 EXPORT_SYMBOL(acpi_os_sleep);
345
346 void acpi_os_stall(u32 us)
347 {
348         while (us) {
349                 u32 delay = 1000;
350
351                 if (delay > us)
352                         delay = us;
353                 udelay(delay);
354                 touch_nmi_watchdog();
355                 us -= delay;
356         }
357 }
358
359 EXPORT_SYMBOL(acpi_os_stall);
360
361 /*
362  * Support ACPI 3.0 AML Timer operand
363  * Returns 64-bit free-running, monotonically increasing timer
364  * with 100ns granularity
365  */
366 u64 acpi_os_get_timer(void)
367 {
368         static u64 t;
369
370 #ifdef  CONFIG_HPET
371         /* TBD: use HPET if available */
372 #endif
373
374 #ifdef  CONFIG_X86_PM_TIMER
375         /* TBD: default to PM timer if HPET was not available */
376 #endif
377         if (!t)
378                 printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
379
380         return ++t;
381 }
382
383 acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
384 {
385         u32 dummy;
386
387         if (!value)
388                 value = &dummy;
389
390         *value = 0;
391         if (width <= 8) {
392                 *(u8 *) value = inb(port);
393         } else if (width <= 16) {
394                 *(u16 *) value = inw(port);
395         } else if (width <= 32) {
396                 *(u32 *) value = inl(port);
397         } else {
398                 BUG();
399         }
400
401         return AE_OK;
402 }
403
404 EXPORT_SYMBOL(acpi_os_read_port);
405
406 acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
407 {
408         if (width <= 8) {
409                 outb(value, port);
410         } else if (width <= 16) {
411                 outw(value, port);
412         } else if (width <= 32) {
413                 outl(value, port);
414         } else {
415                 BUG();
416         }
417
418         return AE_OK;
419 }
420
421 EXPORT_SYMBOL(acpi_os_write_port);
422
423 acpi_status
424 acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
425 {
426         u32 dummy;
427         void __iomem *virt_addr;
428
429         virt_addr = ioremap(phys_addr, width);
430         if (!value)
431                 value = &dummy;
432
433         switch (width) {
434         case 8:
435                 *(u8 *) value = readb(virt_addr);
436                 break;
437         case 16:
438                 *(u16 *) value = readw(virt_addr);
439                 break;
440         case 32:
441                 *(u32 *) value = readl(virt_addr);
442                 break;
443         default:
444                 BUG();
445         }
446
447         iounmap(virt_addr);
448
449         return AE_OK;
450 }
451
452 acpi_status
453 acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
454 {
455         void __iomem *virt_addr;
456
457         virt_addr = ioremap(phys_addr, width);
458
459         switch (width) {
460         case 8:
461                 writeb(value, virt_addr);
462                 break;
463         case 16:
464                 writew(value, virt_addr);
465                 break;
466         case 32:
467                 writel(value, virt_addr);
468                 break;
469         default:
470                 BUG();
471         }
472
473         iounmap(virt_addr);
474
475         return AE_OK;
476 }
477
478 acpi_status
479 acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
480                                void *value, u32 width)
481 {
482         int result, size;
483
484         if (!value)
485                 return AE_BAD_PARAMETER;
486
487         switch (width) {
488         case 8:
489                 size = 1;
490                 break;
491         case 16:
492                 size = 2;
493                 break;
494         case 32:
495                 size = 4;
496                 break;
497         default:
498                 return AE_ERROR;
499         }
500
501         BUG_ON(!raw_pci_ops);
502
503         result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
504                                    PCI_DEVFN(pci_id->device, pci_id->function),
505                                    reg, size, value);
506
507         return (result ? AE_ERROR : AE_OK);
508 }
509
510 EXPORT_SYMBOL(acpi_os_read_pci_configuration);
511
512 acpi_status
513 acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
514                                 acpi_integer value, u32 width)
515 {
516         int result, size;
517
518         switch (width) {
519         case 8:
520                 size = 1;
521                 break;
522         case 16:
523                 size = 2;
524                 break;
525         case 32:
526                 size = 4;
527                 break;
528         default:
529                 return AE_ERROR;
530         }
531
532         BUG_ON(!raw_pci_ops);
533
534         result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
535                                     PCI_DEVFN(pci_id->device, pci_id->function),
536                                     reg, size, value);
537
538         return (result ? AE_ERROR : AE_OK);
539 }
540
541 /* TODO: Change code to take advantage of driver model more */
542 static void acpi_os_derive_pci_id_2(acpi_handle rhandle,        /* upper bound  */
543                                     acpi_handle chandle,        /* current node */
544                                     struct acpi_pci_id **id,
545                                     int *is_bridge, u8 * bus_number)
546 {
547         acpi_handle handle;
548         struct acpi_pci_id *pci_id = *id;
549         acpi_status status;
550         unsigned long temp;
551         acpi_object_type type;
552         u8 tu8;
553
554         acpi_get_parent(chandle, &handle);
555         if (handle != rhandle) {
556                 acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
557                                         bus_number);
558
559                 status = acpi_get_type(handle, &type);
560                 if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
561                         return;
562
563                 status =
564                     acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
565                                           &temp);
566                 if (ACPI_SUCCESS(status)) {
567                         pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
568                         pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
569
570                         if (*is_bridge)
571                                 pci_id->bus = *bus_number;
572
573                         /* any nicer way to get bus number of bridge ? */
574                         status =
575                             acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
576                                                            8);
577                         if (ACPI_SUCCESS(status)
578                             && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
579                                 status =
580                                     acpi_os_read_pci_configuration(pci_id, 0x18,
581                                                                    &tu8, 8);
582                                 if (!ACPI_SUCCESS(status)) {
583                                         /* Certainly broken...  FIX ME */
584                                         return;
585                                 }
586                                 *is_bridge = 1;
587                                 pci_id->bus = tu8;
588                                 status =
589                                     acpi_os_read_pci_configuration(pci_id, 0x19,
590                                                                    &tu8, 8);
591                                 if (ACPI_SUCCESS(status)) {
592                                         *bus_number = tu8;
593                                 }
594                         } else
595                                 *is_bridge = 0;
596                 }
597         }
598 }
599
600 void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound  */
601                            acpi_handle chandle, /* current node */
602                            struct acpi_pci_id **id)
603 {
604         int is_bridge = 1;
605         u8 bus_number = (*id)->bus;
606
607         acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
608 }
609
610 static void acpi_os_execute_deferred(struct work_struct *work)
611 {
612         struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
613         if (!dpc) {
614                 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
615                 return;
616         }
617
618         dpc->function(dpc->context);
619         kfree(dpc);
620
621         return;
622 }
623
624 /*******************************************************************************
625  *
626  * FUNCTION:    acpi_os_execute
627  *
628  * PARAMETERS:  Type               - Type of the callback
629  *              Function           - Function to be executed
630  *              Context            - Function parameters
631  *
632  * RETURN:      Status
633  *
634  * DESCRIPTION: Depending on type, either queues function for deferred execution or
635  *              immediately executes function on a separate thread.
636  *
637  ******************************************************************************/
638
639 acpi_status acpi_os_execute(acpi_execute_type type,
640                             acpi_osd_exec_callback function, void *context)
641 {
642         acpi_status status = AE_OK;
643         struct acpi_os_dpc *dpc;
644         struct workqueue_struct *queue;
645         ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
646                           "Scheduling function [%p(%p)] for deferred execution.\n",
647                           function, context));
648
649         if (!function)
650                 return AE_BAD_PARAMETER;
651
652         /*
653          * Allocate/initialize DPC structure.  Note that this memory will be
654          * freed by the callee.  The kernel handles the work_struct list  in a
655          * way that allows us to also free its memory inside the callee.
656          * Because we may want to schedule several tasks with different
657          * parameters we can't use the approach some kernel code uses of
658          * having a static work_struct.
659          */
660
661         dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
662         if (!dpc)
663                 return_ACPI_STATUS(AE_NO_MEMORY);
664
665         dpc->function = function;
666         dpc->context = context;
667
668         INIT_WORK(&dpc->work, acpi_os_execute_deferred);
669         queue = (type == OSL_NOTIFY_HANDLER) ? kacpi_notify_wq : kacpid_wq;
670         if (!queue_work(queue, &dpc->work)) {
671                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
672                           "Call to queue_work() failed.\n"));
673                 status = AE_ERROR;
674                 kfree(dpc);
675         }
676         return_ACPI_STATUS(status);
677 }
678
679 EXPORT_SYMBOL(acpi_os_execute);
680
681 void acpi_os_wait_events_complete(void *context)
682 {
683         flush_workqueue(kacpid_wq);
684 }
685
686 EXPORT_SYMBOL(acpi_os_wait_events_complete);
687
688 /*
689  * Allocate the memory for a spinlock and initialize it.
690  */
691 acpi_status acpi_os_create_lock(acpi_spinlock * handle)
692 {
693         spin_lock_init(*handle);
694
695         return AE_OK;
696 }
697
698 /*
699  * Deallocate the memory for a spinlock.
700  */
701 void acpi_os_delete_lock(acpi_spinlock handle)
702 {
703         return;
704 }
705
706 acpi_status
707 acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
708 {
709         struct semaphore *sem = NULL;
710
711
712         sem = acpi_os_allocate(sizeof(struct semaphore));
713         if (!sem)
714                 return AE_NO_MEMORY;
715         memset(sem, 0, sizeof(struct semaphore));
716
717         sema_init(sem, initial_units);
718
719         *handle = (acpi_handle *) sem;
720
721         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
722                           *handle, initial_units));
723
724         return AE_OK;
725 }
726
727 EXPORT_SYMBOL(acpi_os_create_semaphore);
728
729 /*
730  * TODO: A better way to delete semaphores?  Linux doesn't have a
731  * 'delete_semaphore()' function -- may result in an invalid
732  * pointer dereference for non-synchronized consumers.  Should
733  * we at least check for blocked threads and signal/cancel them?
734  */
735
736 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
737 {
738         struct semaphore *sem = (struct semaphore *)handle;
739
740
741         if (!sem)
742                 return AE_BAD_PARAMETER;
743
744         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
745
746         kfree(sem);
747         sem = NULL;
748
749         return AE_OK;
750 }
751
752 EXPORT_SYMBOL(acpi_os_delete_semaphore);
753
754 /*
755  * TODO: The kernel doesn't have a 'down_timeout' function -- had to
756  * improvise.  The process is to sleep for one scheduler quantum
757  * until the semaphore becomes available.  Downside is that this
758  * may result in starvation for timeout-based waits when there's
759  * lots of semaphore activity.
760  *
761  * TODO: Support for units > 1?
762  */
763 acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
764 {
765         acpi_status status = AE_OK;
766         struct semaphore *sem = (struct semaphore *)handle;
767         int ret = 0;
768
769
770         if (!sem || (units < 1))
771                 return AE_BAD_PARAMETER;
772
773         if (units > 1)
774                 return AE_SUPPORT;
775
776         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
777                           handle, units, timeout));
778
779         /*
780          * This can be called during resume with interrupts off.
781          * Like boot-time, we should be single threaded and will
782          * always get the lock if we try -- timeout or not.
783          * If this doesn't succeed, then we will oops courtesy of
784          * might_sleep() in down().
785          */
786         if (!down_trylock(sem))
787                 return AE_OK;
788
789         switch (timeout) {
790                 /*
791                  * No Wait:
792                  * --------
793                  * A zero timeout value indicates that we shouldn't wait - just
794                  * acquire the semaphore if available otherwise return AE_TIME
795                  * (a.k.a. 'would block').
796                  */
797         case 0:
798                 if (down_trylock(sem))
799                         status = AE_TIME;
800                 break;
801
802                 /*
803                  * Wait Indefinitely:
804                  * ------------------
805                  */
806         case ACPI_WAIT_FOREVER:
807                 down(sem);
808                 break;
809
810                 /*
811                  * Wait w/ Timeout:
812                  * ----------------
813                  */
814         default:
815                 // TODO: A better timeout algorithm?
816                 {
817                         int i = 0;
818                         static const int quantum_ms = 1000 / HZ;
819
820                         ret = down_trylock(sem);
821                         for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
822                                 schedule_timeout_interruptible(1);
823                                 ret = down_trylock(sem);
824                         }
825
826                         if (ret != 0)
827                                 status = AE_TIME;
828                 }
829                 break;
830         }
831
832         if (ACPI_FAILURE(status)) {
833                 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
834                                   "Failed to acquire semaphore[%p|%d|%d], %s",
835                                   handle, units, timeout,
836                                   acpi_format_exception(status)));
837         } else {
838                 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
839                                   "Acquired semaphore[%p|%d|%d]", handle,
840                                   units, timeout));
841         }
842
843         return status;
844 }
845
846 EXPORT_SYMBOL(acpi_os_wait_semaphore);
847
848 /*
849  * TODO: Support for units > 1?
850  */
851 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
852 {
853         struct semaphore *sem = (struct semaphore *)handle;
854
855
856         if (!sem || (units < 1))
857                 return AE_BAD_PARAMETER;
858
859         if (units > 1)
860                 return AE_SUPPORT;
861
862         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
863                           units));
864
865         up(sem);
866
867         return AE_OK;
868 }
869
870 EXPORT_SYMBOL(acpi_os_signal_semaphore);
871
872 #ifdef ACPI_FUTURE_USAGE
873 u32 acpi_os_get_line(char *buffer)
874 {
875
876 #ifdef ENABLE_DEBUGGER
877         if (acpi_in_debugger) {
878                 u32 chars;
879
880                 kdb_read(buffer, sizeof(line_buf));
881
882                 /* remove the CR kdb includes */
883                 chars = strlen(buffer) - 1;
884                 buffer[chars] = '\0';
885         }
886 #endif
887
888         return 0;
889 }
890 #endif                          /*  ACPI_FUTURE_USAGE  */
891
892 acpi_status acpi_os_signal(u32 function, void *info)
893 {
894         switch (function) {
895         case ACPI_SIGNAL_FATAL:
896                 printk(KERN_ERR PREFIX "Fatal opcode executed\n");
897                 break;
898         case ACPI_SIGNAL_BREAKPOINT:
899                 /*
900                  * AML Breakpoint
901                  * ACPI spec. says to treat it as a NOP unless
902                  * you are debugging.  So if/when we integrate
903                  * AML debugger into the kernel debugger its
904                  * hook will go here.  But until then it is
905                  * not useful to print anything on breakpoints.
906                  */
907                 break;
908         default:
909                 break;
910         }
911
912         return AE_OK;
913 }
914
915 EXPORT_SYMBOL(acpi_os_signal);
916
917 static int __init acpi_os_name_setup(char *str)
918 {
919         char *p = acpi_os_name;
920         int count = ACPI_MAX_OVERRIDE_LEN - 1;
921
922         if (!str || !*str)
923                 return 0;
924
925         for (; count-- && str && *str; str++) {
926                 if (isalnum(*str) || *str == ' ' || *str == ':')
927                         *p++ = *str;
928                 else if (*str == '\'' || *str == '"')
929                         continue;
930                 else
931                         break;
932         }
933         *p = 0;
934
935         return 1;
936
937 }
938
939 __setup("acpi_os_name=", acpi_os_name_setup);
940
941 static void enable_osi_linux(int enable) {
942
943         if (osi_linux != enable)
944                 printk(KERN_INFO PREFIX "%sabled _OSI(Linux)\n",
945                         enable ? "En": "Dis");
946
947         osi_linux = enable;
948         return;
949 }
950
951 /*
952  * Modify the list of "OS Interfaces" reported to BIOS via _OSI
953  *
954  * empty string disables _OSI
955  * string starting with '!' disables that string
956  * otherwise string is added to list, augmenting built-in strings
957  */
958 static int __init acpi_osi_setup(char *str)
959 {
960         if (str == NULL || *str == '\0') {
961                 printk(KERN_INFO PREFIX "_OSI method disabled\n");
962                 acpi_gbl_create_osi_method = FALSE;
963         } else if (!strcmp("!Linux", str)) {
964                 enable_osi_linux(0);
965         } else if (*str == '!') {
966                 if (acpi_osi_invalidate(++str) == AE_OK)
967                         printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
968         } else if (!strcmp("Linux", str)) {
969                 enable_osi_linux(1);
970         } else if (*osi_additional_string == '\0') {
971                 strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
972                 printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
973         }
974
975         return 1;
976 }
977
978 __setup("acpi_osi=", acpi_osi_setup);
979
980 /* enable serialization to combat AE_ALREADY_EXISTS errors */
981 static int __init acpi_serialize_setup(char *str)
982 {
983         printk(KERN_INFO PREFIX "serialize enabled\n");
984
985         acpi_gbl_all_methods_serialized = TRUE;
986
987         return 1;
988 }
989
990 __setup("acpi_serialize", acpi_serialize_setup);
991
992 /*
993  * Wake and Run-Time GPES are expected to be separate.
994  * We disable wake-GPEs at run-time to prevent spurious
995  * interrupts.
996  *
997  * However, if a system exists that shares Wake and
998  * Run-time events on the same GPE this flag is available
999  * to tell Linux to keep the wake-time GPEs enabled at run-time.
1000  */
1001 static int __init acpi_wake_gpes_always_on_setup(char *str)
1002 {
1003         printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
1004
1005         acpi_gbl_leave_wake_gpes_disabled = FALSE;
1006
1007         return 1;
1008 }
1009
1010 __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
1011
1012 /*
1013  * Acquire a spinlock.
1014  *
1015  * handle is a pointer to the spinlock_t.
1016  */
1017
1018 acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
1019 {
1020         acpi_cpu_flags flags;
1021         spin_lock_irqsave(lockp, flags);
1022         return flags;
1023 }
1024
1025 /*
1026  * Release a spinlock. See above.
1027  */
1028
1029 void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
1030 {
1031         spin_unlock_irqrestore(lockp, flags);
1032 }
1033
1034 #ifndef ACPI_USE_LOCAL_CACHE
1035
1036 /*******************************************************************************
1037  *
1038  * FUNCTION:    acpi_os_create_cache
1039  *
1040  * PARAMETERS:  name      - Ascii name for the cache
1041  *              size      - Size of each cached object
1042  *              depth     - Maximum depth of the cache (in objects) <ignored>
1043  *              cache     - Where the new cache object is returned
1044  *
1045  * RETURN:      status
1046  *
1047  * DESCRIPTION: Create a cache object
1048  *
1049  ******************************************************************************/
1050
1051 acpi_status
1052 acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
1053 {
1054         *cache = kmem_cache_create(name, size, 0, 0, NULL);
1055         if (*cache == NULL)
1056                 return AE_ERROR;
1057         else
1058                 return AE_OK;
1059 }
1060
1061 /*******************************************************************************
1062  *
1063  * FUNCTION:    acpi_os_purge_cache
1064  *
1065  * PARAMETERS:  Cache           - Handle to cache object
1066  *
1067  * RETURN:      Status
1068  *
1069  * DESCRIPTION: Free all objects within the requested cache.
1070  *
1071  ******************************************************************************/
1072
1073 acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
1074 {
1075         kmem_cache_shrink(cache);
1076         return (AE_OK);
1077 }
1078
1079 /*******************************************************************************
1080  *
1081  * FUNCTION:    acpi_os_delete_cache
1082  *
1083  * PARAMETERS:  Cache           - Handle to cache object
1084  *
1085  * RETURN:      Status
1086  *
1087  * DESCRIPTION: Free all objects within the requested cache and delete the
1088  *              cache object.
1089  *
1090  ******************************************************************************/
1091
1092 acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
1093 {
1094         kmem_cache_destroy(cache);
1095         return (AE_OK);
1096 }
1097
1098 /*******************************************************************************
1099  *
1100  * FUNCTION:    acpi_os_release_object
1101  *
1102  * PARAMETERS:  Cache       - Handle to cache object
1103  *              Object      - The object to be released
1104  *
1105  * RETURN:      None
1106  *
1107  * DESCRIPTION: Release an object to the specified cache.  If cache is full,
1108  *              the object is deleted.
1109  *
1110  ******************************************************************************/
1111
1112 acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
1113 {
1114         kmem_cache_free(cache, object);
1115         return (AE_OK);
1116 }
1117
1118 /******************************************************************************
1119  *
1120  * FUNCTION:    acpi_os_validate_interface
1121  *
1122  * PARAMETERS:  interface           - Requested interface to be validated
1123  *
1124  * RETURN:      AE_OK if interface is supported, AE_SUPPORT otherwise
1125  *
1126  * DESCRIPTION: Match an interface string to the interfaces supported by the
1127  *              host. Strings originate from an AML call to the _OSI method.
1128  *
1129  *****************************************************************************/
1130
1131 acpi_status
1132 acpi_os_validate_interface (char *interface)
1133 {
1134         if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
1135                 return AE_OK;
1136         if (!strcmp("Linux", interface)) {
1137                 printk(KERN_WARNING PREFIX
1138                         "System BIOS is requesting _OSI(Linux)\n");
1139                 printk(KERN_WARNING PREFIX
1140                         "If \"acpi_osi=Linux\" works better,\n"
1141                         "Please send dmidecode "
1142                         "to linux-acpi@vger.kernel.org\n");
1143                 if(osi_linux)
1144                         return AE_OK;
1145         }
1146         return AE_SUPPORT;
1147 }
1148
1149 /******************************************************************************
1150  *
1151  * FUNCTION:    acpi_os_validate_address
1152  *
1153  * PARAMETERS:  space_id             - ACPI space ID
1154  *              address             - Physical address
1155  *              length              - Address length
1156  *
1157  * RETURN:      AE_OK if address/length is valid for the space_id. Otherwise,
1158  *              should return AE_AML_ILLEGAL_ADDRESS.
1159  *
1160  * DESCRIPTION: Validate a system address via the host OS. Used to validate
1161  *              the addresses accessed by AML operation regions.
1162  *
1163  *****************************************************************************/
1164
1165 acpi_status
1166 acpi_os_validate_address (
1167     u8                   space_id,
1168     acpi_physical_address   address,
1169     acpi_size               length)
1170 {
1171
1172     return AE_OK;
1173 }
1174
1175 #ifdef CONFIG_DMI
1176 static int dmi_osi_linux(const struct dmi_system_id *d)
1177 {
1178         printk(KERN_NOTICE "%s detected: enabling _OSI(Linux)\n", d->ident);
1179         enable_osi_linux(1);
1180         return 0;
1181 }
1182
1183 static struct dmi_system_id acpi_osl_dmi_table[] __initdata = {
1184         /*
1185          * Boxes that need _OSI(Linux)
1186          */
1187         {
1188          .callback = dmi_osi_linux,
1189          .ident = "Intel Napa CRB",
1190          .matches = {
1191                      DMI_MATCH(DMI_BOARD_VENDOR, "Intel Corporation"),
1192                      DMI_MATCH(DMI_BOARD_NAME, "MPAD-MSAE Customer Reference Boards"),
1193                      },
1194          },
1195         {}
1196 };
1197 #endif /* CONFIG_DMI */
1198
1199 #endif