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