1/******************************************************************************
2 *
3 * Module Name: os.c - Linux OSL functions
4 *		$Revision: 1.1.1.1 $
5 *
6 *****************************************************************************/
7
8/*
9 *  os.c - OS-dependent functions
10 *
11 *  Copyright (C) 2000 Andrew Henroid
12 *  Copyright (C) 2001 Andrew Grover
13 *
14 *  This program is free software; you can redistribute it and/or modify
15 *  it under the terms of the GNU General Public License as published by
16 *  the Free Software Foundation; either version 2 of the License, or
17 *  (at your option) any later version.
18 *
19 *  This program is distributed in the hope that it will be useful,
20 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 *  GNU General Public License for more details.
23 *
24 *  You should have received a copy of the GNU General Public License
25 *  along with this program; if not, write to the Free Software
26 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27 */
28/* Changes
29 *
30 * Christopher Liebman <liebman@sponsera.com> 2001-5-15
31 * - Fixed improper kernel_thread parameters
32 */
33
34#include <linux/kernel.h>
35#include <linux/slab.h>
36#include <linux/mm.h>
37#include <linux/pci.h>
38#include <linux/interrupt.h>
39#include <linux/kmod.h>
40#include <linux/delay.h>
41#include <asm/io.h>
42#include <acpi.h>
43
44#ifdef CONFIG_ACPI_EFI
45#include <linux/efi.h>
46#endif
47
48#ifdef _IA64
49#include <asm/hw_irq.h>
50#endif
51
52#define _COMPONENT	ACPI_OS_SERVICES
53	MODULE_NAME	("os")
54
55typedef struct
56{
57    OSD_EXECUTION_CALLBACK  function;
58    void		    *context;
59} ACPI_OS_DPC;
60
61
62/*****************************************************************************
63 *			       Debugger Stuff
64 *****************************************************************************/
65
66#ifdef ENABLE_DEBUGGER
67
68#include <linux/kdb.h>
69
70/* stuff for debugger support */
71int acpi_in_debugger = 0;
72extern NATIVE_CHAR line_buf[80];
73
74#endif
75
76
77/*****************************************************************************
78 *				    Globals
79 *****************************************************************************/
80
81static int acpi_irq_irq = 0;
82static OSD_HANDLER acpi_irq_handler = NULL;
83static void *acpi_irq_context = NULL;
84
85
86/******************************************************************************
87 *				   Functions
88 *****************************************************************************/
89
90acpi_status
91acpi_os_initialize(void)
92{
93	return AE_OK;
94}
95
96acpi_status
97acpi_os_terminate(void)
98{
99	if (acpi_irq_handler) {
100		acpi_os_remove_interrupt_handler(acpi_irq_irq,
101						 acpi_irq_handler);
102	}
103
104	return AE_OK;
105}
106
107s32
108acpi_os_printf(const NATIVE_CHAR *fmt,...)
109{
110	s32 size;
111	va_list args;
112	va_start(args, fmt);
113	size = acpi_os_vprintf(fmt, args);
114	va_end(args);
115
116	return size;
117}
118
119s32
120acpi_os_vprintf(const NATIVE_CHAR *fmt, va_list args)
121{
122	static char buffer[512];
123	int size = vsprintf(buffer, fmt, args);
124
125#ifdef ENABLE_DEBUGGER
126	if (acpi_in_debugger) {
127		kdb_printf("%s", buffer);
128	} else {
129		printk("%s", buffer);
130	}
131#else
132	printk("%s", buffer);
133#endif
134
135	return size;
136}
137
138void *
139acpi_os_allocate(u32 size)
140{
141	return kmalloc(size, GFP_KERNEL);
142}
143
144void *
145acpi_os_callocate(u32 size)
146{
147	void *ptr = acpi_os_allocate(size);
148	if (ptr)
149		memset(ptr, 0, size);
150
151	return ptr;
152}
153
154void
155acpi_os_free(void *ptr)
156{
157	kfree(ptr);
158}
159
160
161acpi_status
162acpi_os_get_root_pointer(u32 flags, ACPI_PHYSICAL_ADDRESS *phys_addr)
163{
164#ifndef CONFIG_ACPI_EFI
165	if (ACPI_FAILURE(acpi_find_root_pointer(flags, phys_addr))) {
166		printk(KERN_ERR "ACPI: System description tables not found\n");
167		return AE_ERROR;
168	}
169#else /*CONFIG_ACPI_EFI*/
170	if (efi.acpi20)
171		*phys_addr = (ACPI_PHYSICAL_ADDRESS) efi.acpi20;
172	else if (efi.acpi)
173		*phys_addr = (ACPI_PHYSICAL_ADDRESS) efi.acpi;
174	else {
175		printk(KERN_ERR "ACPI: System description tables not found\n");
176		*phys_addr = NULL;
177		return AE_ERROR;
178	}
179#endif /*CONFIG_ACPI_EFI*/
180
181	return AE_OK;
182}
183
184acpi_status
185acpi_os_map_memory(ACPI_PHYSICAL_ADDRESS phys, u32 size, void **virt)
186{
187	if (phys > ULONG_MAX) {
188		printk(KERN_ERR "ACPI: Cannot map memory that high\n");
189		return AE_ERROR;
190	}
191
192	*virt = ioremap((unsigned long) phys, size);
193	if (!*virt)
194		return AE_ERROR;
195
196	return AE_OK;
197}
198
199void
200acpi_os_unmap_memory(void *virt, u32 size)
201{
202	iounmap(virt);
203}
204
205acpi_status
206acpi_os_get_physical_address(void *virt, ACPI_PHYSICAL_ADDRESS *phys)
207{
208	if(!phys || !virt)
209		return AE_BAD_PARAMETER;
210
211	*phys = virt_to_phys(virt);
212
213	return AE_OK;
214}
215
216static void
217acpi_irq(int irq, void *dev_id, struct pt_regs *regs)
218{
219	(*acpi_irq_handler)(acpi_irq_context);
220}
221
222acpi_status
223acpi_os_install_interrupt_handler(u32 irq, OSD_HANDLER handler, void *context)
224{
225#ifdef _IA64
226	irq = isa_irq_to_vector(irq);
227#endif /*_IA64*/
228	acpi_irq_irq = irq;
229	acpi_irq_handler = handler;
230	acpi_irq_context = context;
231	if (request_irq(irq,
232			acpi_irq,
233			SA_SHIRQ,
234			"acpi",
235			acpi_irq)) {
236		printk(KERN_ERR "ACPI: SCI (IRQ%d) allocation failed\n", irq);
237		return AE_ERROR;
238	}
239
240	return AE_OK;
241}
242
243acpi_status
244acpi_os_remove_interrupt_handler(u32 irq, OSD_HANDLER handler)
245{
246	if (acpi_irq_handler) {
247#ifdef _IA64
248		irq = isa_irq_to_vector(irq);
249#endif /*_IA64*/
250		free_irq(irq, acpi_irq);
251		acpi_irq_handler = NULL;
252	}
253
254	return AE_OK;
255}
256
257/*
258 * Running in interpreter thread context, safe to sleep
259 */
260
261void
262acpi_os_sleep(u32 sec, u32 ms)
263{
264	current->state = TASK_INTERRUPTIBLE;
265	schedule_timeout(HZ * sec + (ms * HZ) / 1000);
266}
267
268void
269acpi_os_stall(u32 us)
270{
271	if (us > 10000) {
272		mdelay(us / 1000);
273	}
274	else {
275		udelay(us);
276	}
277}
278
279acpi_status
280acpi_os_read_port(
281	ACPI_IO_ADDRESS	port,
282	void		*value,
283	u32		width)
284{
285	u32 dummy;
286
287	if (!value)
288		value = &dummy;
289
290	switch (width)
291	{
292	case 8:
293		*(u8*)  value = inb(port);
294		break;
295	case 16:
296		*(u16*) value = inw(port);
297		break;
298	case 32:
299		*(u32*) value = inl(port);
300		break;
301	default:
302		BUG();
303	}
304
305	return AE_OK;
306}
307
308acpi_status
309acpi_os_write_port(
310	ACPI_IO_ADDRESS	port,
311	NATIVE_UINT	value,
312	u32		width)
313{
314	switch (width)
315	{
316	case 8:
317		outb(value, port);
318		break;
319	case 16:
320		outw(value, port);
321		break;
322	case 32:
323		outl(value, port);
324		break;
325	default:
326		BUG();
327	}
328
329	return AE_OK;
330}
331
332acpi_status
333acpi_os_read_memory(
334	ACPI_PHYSICAL_ADDRESS	phys_addr,
335	void			*value,
336	u32			width)
337{
338	u32 dummy;
339
340	if (!value)
341		value = &dummy;
342
343	switch (width)
344	{
345	case 8:
346		*(u8*) value = *(u8*) phys_to_virt(phys_addr);
347		break;
348	case 16:
349		*(u16*) value = *(u16*) phys_to_virt(phys_addr);
350		break;
351	case 32:
352		*(u32*) value = *(u32*) phys_to_virt(phys_addr);
353		break;
354	default:
355		BUG();
356	}
357
358	return AE_OK;
359}
360
361acpi_status
362acpi_os_write_memory(
363	ACPI_PHYSICAL_ADDRESS	phys_addr,
364	NATIVE_UINT		value,
365	u32			width)
366{
367	switch (width)
368	{
369	case 8:
370		*(u8*) phys_to_virt(phys_addr) = value;
371		break;
372	case 16:
373		*(u16*) phys_to_virt(phys_addr) = value;
374		break;
375	case 32:
376		*(u32*) phys_to_virt(phys_addr) = value;
377		break;
378	default:
379		BUG();
380	}
381
382	return AE_OK;
383}
384
385
386#ifdef CONFIG_ACPI_PCI
387
388/* Architecture-dependent low-level PCI configuration access functions. */
389extern int (*pci_config_read)(int seg, int bus, int dev, int fn, int reg, int len, u32 *val);
390extern int (*pci_config_write)(int seg, int bus, int dev, int fn, int reg, int len, u32 val);
391
392acpi_status
393acpi_os_read_pci_configuration (
394	acpi_pci_id             *pci_id,
395	u32                     reg,
396	void                    *value,
397	u32                     width)
398{
399	int			result = 0;
400	if (!value)
401		return AE_ERROR;
402
403	switch (width)
404	{
405	case 8:
406		result = pci_config_read(pci_id->segment, pci_id->bus,
407			pci_id->device, pci_id->function, reg, 1, value);
408		break;
409	case 16:
410		result = pci_config_read(pci_id->segment, pci_id->bus,
411			pci_id->device, pci_id->function, reg, 2, value);
412		break;
413	case 32:
414		result = pci_config_read(pci_id->segment, pci_id->bus,
415			pci_id->device, pci_id->function, reg, 4, value);
416		break;
417	default:
418		BUG();
419	}
420
421	return (result ? AE_ERROR : AE_OK);
422}
423
424ACPI_STATUS
425acpi_os_write_pci_configuration (
426	acpi_pci_id             *pci_id,
427	u32                     reg,
428	NATIVE_UINT             value,
429	u32                     width)
430{
431	int			result = 0;
432
433	switch (width)
434	{
435	case 8:
436		result = pci_config_write(pci_id->segment, pci_id->bus,
437			pci_id->device, pci_id->function, reg, 1, value);
438		break;
439	case 16:
440		result = pci_config_write(pci_id->segment, pci_id->bus,
441			pci_id->device, pci_id->function, reg, 2, value);
442		break;
443	case 32:
444		result = pci_config_write(pci_id->segment, pci_id->bus,
445			pci_id->device, pci_id->function, reg, 4, value);
446		break;
447	default:
448		BUG();
449	}
450
451	return (result ? AE_ERROR : AE_OK);
452}
453
454#else /*CONFIG_ACPI_PCI*/
455
456acpi_status
457acpi_os_read_pci_configuration (
458	acpi_pci_id	*pci_id,
459	u32		reg,
460	void		*value,
461	u32		width)
462{
463	int devfn = PCI_DEVFN(pci_id->device, pci_id->function);
464	struct pci_dev *dev = pci_find_slot(pci_id->bus, devfn);
465
466	if (!value || !dev)
467		return AE_ERROR;
468
469	switch (width)
470	{
471	case 8:
472		if (pci_read_config_byte(dev, reg, (u8*) value))
473			return AE_ERROR;
474		break;
475	case 16:
476		if (pci_read_config_word(dev, reg, (u16*) value))
477			return AE_ERROR;
478		break;
479	case 32:
480		if (pci_read_config_dword(dev, reg, (u32*) value))
481			return AE_ERROR;
482		break;
483	default:
484		BUG();
485	}
486
487	return AE_OK;
488}
489
490acpi_status
491acpi_os_write_pci_configuration (
492	acpi_pci_id	*pci_id,
493	u32		reg,
494	NATIVE_UINT	value,
495	u32		width)
496{
497	int devfn = PCI_DEVFN(pci_id->device, pci_id->function);
498	struct pci_dev *dev = pci_find_slot(pci_id->bus, devfn);
499
500	if (!dev)
501		return AE_ERROR;
502
503	switch (width)
504	{
505	case 8:
506		if (pci_write_config_byte(dev, reg, value))
507			return AE_ERROR;
508		break;
509	case 16:
510		if (pci_write_config_word(dev, reg, value))
511			return AE_ERROR;
512		break;
513	case 32:
514		if (pci_write_config_dword(dev, reg, value))
515			return AE_ERROR;
516		break;
517	default:
518		BUG();
519	}
520
521	return AE_OK;
522}
523
524#endif /*CONFIG_ACPI_PCI*/
525
526
527acpi_status
528acpi_os_load_module (
529	char *module_name)
530{
531	PROC_NAME("acpi_os_load_module");
532
533	if (!module_name)
534		return AE_BAD_PARAMETER;
535
536	if (0 > request_module(module_name)) {
537		ACPI_DEBUG_PRINT ((ACPI_DB_WARN, "Unable to load module [%s].\n", module_name));
538		return AE_ERROR;
539	}
540
541	return AE_OK;
542}
543
544acpi_status
545acpi_os_unload_module (
546	char *module_name)
547{
548	if (!module_name)
549		return AE_BAD_PARAMETER;
550
551	/* TODO: How on Linux? */
552	/* this is done automatically for all modules with
553	use_count = 0, I think. see: MOD_INC_USE_COUNT -ASG */
554
555	return AE_OK;
556}
557
558
559/*
560 * See acpi_os_queue_for_execution()
561 */
562static int
563acpi_os_queue_exec (
564	void *context)
565{
566	ACPI_OS_DPC		*dpc = (ACPI_OS_DPC*)context;
567
568	PROC_NAME("acpi_os_queue_exec");
569
570	daemonize();
571	strcpy(current->comm, "kacpidpc");
572
573	if (!dpc || !dpc->function)
574		return AE_BAD_PARAMETER;
575
576	ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Executing function [%p(%p)].\n", dpc->function, dpc->context));
577
578	dpc->function(dpc->context);
579
580	kfree(dpc);
581
582	return 1;
583}
584
585static void
586acpi_os_schedule_exec (
587	void *context)
588{
589	ACPI_OS_DPC		*dpc = NULL;
590	int			thread_pid = -1;
591
592	PROC_NAME("acpi_os_schedule_exec");
593
594	dpc = (ACPI_OS_DPC*)context;
595	if (!dpc) {
596		ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
597		return;
598	}
599
600	ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Creating new thread to run function [%p(%p)].\n", dpc->function, dpc->context));
601
602	thread_pid = kernel_thread(acpi_os_queue_exec, dpc,
603		(CLONE_FS | CLONE_FILES | SIGCHLD));
604	if (thread_pid < 0) {
605		ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Call to kernel_thread() failed.\n"));
606		acpi_os_free(dpc);
607	}
608}
609
610acpi_status
611acpi_os_queue_for_execution(
612	u32			priority,
613	OSD_EXECUTION_CALLBACK	function,
614	void			*context)
615{
616	acpi_status 		status = AE_OK;
617	ACPI_OS_DPC 		*dpc = NULL;
618
619	PROC_NAME("acpi_os_queue_for_execution");
620
621	ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Scheduling function [%p(%p)] for deferred execution.\n", function, context));
622
623	if (!function)
624		return AE_BAD_PARAMETER;
625
626	/*
627	 * Queue via DPC:
628	 * --------------
629	 * Note that we have to use two different processes for queuing DPCs:
630	 *	 Interrupt-Level: Use schedule_task; can't spawn a new thread.
631	 *	    Kernel-Level: Spawn a new kernel thread, as schedule_task has
632	 *			  its limitations (e.g. single-threaded model), and
633	 *			  all other task queues run at interrupt-level.
634	 */
635	switch (priority) {
636
637	case OSD_PRIORITY_GPE:
638	{
639		static struct tq_struct task;
640
641		/*
642		 * Allocate/initialize DPC structure.  Note that this memory will be
643		 * freed by the callee.
644		 */
645		dpc = kmalloc(sizeof(ACPI_OS_DPC), GFP_ATOMIC);
646		if (!dpc)
647			return AE_NO_MEMORY;
648
649		dpc->function = function;
650		dpc->context = context;
651
652		memset(&task, 0, sizeof(struct tq_struct));
653
654		task.routine = acpi_os_schedule_exec;
655		task.data = (void*)dpc;
656
657		if (schedule_task(&task) < 0) {
658			ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Call to schedule_task() failed.\n"));
659			status = AE_ERROR;
660		}
661	}
662	break;
663
664	default:
665		/*
666		 * Allocate/initialize DPC structure.  Note that this memory will be
667		 * freed by the callee.
668		 */
669		dpc = kmalloc(sizeof(ACPI_OS_DPC), GFP_KERNEL);
670		if (!dpc)
671			return AE_NO_MEMORY;
672
673		dpc->function = function;
674		dpc->context = context;
675
676		acpi_os_schedule_exec(dpc);
677		break;
678	}
679
680	return status;
681}
682
683
684acpi_status
685acpi_os_create_semaphore(
686	u32		max_units,
687	u32		initial_units,
688	acpi_handle	*handle)
689{
690	struct semaphore	*sem = NULL;
691
692	PROC_NAME("acpi_os_create_semaphore");
693
694	sem = acpi_os_callocate(sizeof(struct semaphore));
695	if (!sem)
696		return AE_NO_MEMORY;
697
698	sema_init(sem, initial_units);
699
700	*handle = (acpi_handle*)sem;
701
702	ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Creating semaphore[%p|%d].\n", *handle, initial_units));
703
704	return AE_OK;
705}
706
707
708/*
709 * TODO: A better way to delete semaphores?  Linux doesn't have a
710 * 'delete_semaphore()' function -- may result in an invalid
711 * pointer dereference for non-synchronized consumers.	Should
712 * we at least check for blocked threads and signal/cancel them?
713 */
714
715acpi_status
716acpi_os_delete_semaphore(
717	acpi_handle	handle)
718{
719	struct semaphore *sem = (struct semaphore*) handle;
720
721	PROC_NAME("acpi_os_delete_semaphore");
722
723	if (!sem)
724		return AE_BAD_PARAMETER;
725
726	ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Deleting semaphore[%p].\n", handle));
727
728	acpi_os_free(sem); sem =  NULL;
729
730	return AE_OK;
731}
732
733
734/*
735 * TODO: The kernel doesn't have a 'down_timeout' function -- had to
736 * improvise.  The process is to sleep for one scheduler quantum
737 * until the semaphore becomes available.  Downside is that this
738 * may result in starvation for timeout-based waits when there's
739 * lots of semaphore activity.
740 *
741 * TODO: Support for units > 1?
742 */
743acpi_status
744acpi_os_wait_semaphore(
745	acpi_handle		handle,
746	u32			units,
747	u32			timeout)
748{
749	acpi_status		status = AE_OK;
750	struct semaphore	*sem = (struct semaphore*)handle;
751	int			ret = 0;
752
753	PROC_NAME("acpi_os_wait_semaphore");
754
755	if (!sem || (units < 1))
756		return AE_BAD_PARAMETER;
757
758	if (units > 1)
759		return AE_SUPPORT;
760
761	ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Waiting for semaphore[%p|%d|%d]\n", handle, units, timeout));
762
763	switch (timeout)
764	{
765		/*
766		 * No Wait:
767		 * --------
768		 * A zero timeout value indicates that we shouldn't wait - just
769		 * acquire the semaphore if available otherwise return AE_TIME
770		 * (a.k.a. 'would block').
771		 */
772		case 0:
773		if(down_trylock(sem))
774			status = AE_TIME;
775		break;
776
777		/*
778		 * Wait Indefinitely:
779		 * ------------------
780		 */
781		case WAIT_FOREVER:
782		ret = down_interruptible(sem);
783		if (ret < 0)
784			status = AE_ERROR;
785		break;
786
787		/*
788		 * Wait w/ Timeout:
789		 * ----------------
790		 */
791		default:
792		// TODO: A better timeout algorithm?
793		{
794			int i = 0;
795			static const int quantum_ms = 1000/HZ;
796
797			ret = down_trylock(sem);
798			for (i = timeout; (i > 0 && ret < 0); i -= quantum_ms) {
799				current->state = TASK_INTERRUPTIBLE;
800				schedule_timeout(1);
801				ret = down_trylock(sem);
802			}
803
804			if (ret != 0)
805			 status = AE_TIME;
806			}
807		break;
808	}
809
810	if (ACPI_FAILURE(status)) {
811		ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Failed to acquire semaphore[%p|%d|%d]\n", handle, units, timeout));
812	}
813	else {
814		ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Acquired semaphore[%p|%d|%d]\n", handle, units, timeout));
815	}
816
817	return status;
818}
819
820
821/*
822 * TODO: Support for units > 1?
823 */
824acpi_status
825acpi_os_signal_semaphore(
826    acpi_handle 	    handle,
827    u32 		    units)
828{
829	struct semaphore *sem = (struct semaphore *) handle;
830
831	PROC_NAME("acpi_os_signal_semaphore");
832
833	if (!sem || (units < 1))
834		return AE_BAD_PARAMETER;
835
836	if (units > 1)
837		return AE_SUPPORT;
838
839	ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Signaling semaphore[%p|%d]\n", handle, units));
840
841	up(sem);
842
843	return AE_OK;
844}
845
846u32
847acpi_os_get_line(NATIVE_CHAR *buffer)
848{
849
850#ifdef ENABLE_DEBUGGER
851	if (acpi_in_debugger) {
852		u32 chars;
853
854		kdb_read(buffer, sizeof(line_buf));
855
856		/* remove the CR kdb includes */
857		chars = strlen(buffer) - 1;
858		buffer[chars] = '\0';
859	}
860#endif
861
862	return 0;
863}
864
865/*
866 * We just have to assume we're dealing with valid memory
867 */
868
869BOOLEAN
870acpi_os_readable(void *ptr, u32 len)
871{
872	return 1;
873}
874
875BOOLEAN
876acpi_os_writable(void *ptr, u32 len)
877{
878	return 1;
879}
880
881u32
882acpi_os_get_thread_id (void)
883{
884	if (!in_interrupt())
885		return current->pid;
886
887	return 0;
888}
889
890acpi_status
891acpi_os_signal (
892    u32		function,
893    void	*info)
894{
895	switch (function)
896	{
897	case ACPI_SIGNAL_FATAL:
898		printk(KERN_ERR "ACPI: Fatal opcode executed\n");
899		break;
900	case ACPI_SIGNAL_BREAKPOINT:
901		{
902			char *bp_info = (char*) info;
903
904			printk(KERN_ERR "ACPI breakpoint: %s\n", bp_info);
905		}
906	default:
907		break;
908	}
909
910	return AE_OK;
911}
912
913acpi_status
914acpi_os_breakpoint(NATIVE_CHAR *msg)
915{
916	acpi_os_printf("breakpoint: %s", msg);
917
918	return AE_OK;
919}
920
921