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