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