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