xref: /openbsd-src/sys/arch/arm/arm/fiq.c (revision ebd24745f27adb8c93026fb63b2cee354c49d024)
1 /*	$OpenBSD: fiq.c,v 1.7 2016/01/31 00:14:50 jsg Exp $	*/
2 /*	$NetBSD: fiq.c,v 1.5 2002/04/03 23:33:27 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
6  * All rights reserved.
7  *
8  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed for the NetBSD Project by
21  *	Wasabi Systems, Inc.
22  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23  *    or promote products derived from this software without specific prior
24  *    written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 
42 #include <arm/cpufunc.h>
43 #include <arm/fiq.h>
44 
45 TAILQ_HEAD(, fiqhandler) fiqhandler_stack =
46     TAILQ_HEAD_INITIALIZER(fiqhandler_stack);
47 
48 extern char fiqvector[];
49 extern char fiq_nullhandler[], fiq_nullhandler_end[];
50 
51 #define	IRQ_BIT		PSR_I
52 #define	FIQ_BIT		PSR_F
53 
54 /*
55  * fiq_installhandler:
56  *
57  *	Actually install the FIQ handler down at the FIQ vector.
58  *
59  *	Note: If the FIQ is invoked via an extra layer of
60  *	indirection, the actual FIQ code store lives in the
61  *	data segment, so there is no need to manipulate
62  *	the vector page's protection.
63  */
64 static void
fiq_installhandler(void * func,size_t size)65 fiq_installhandler(void *func, size_t size)
66 {
67 #if !defined(__ARM_FIQ_INDIRECT)
68 	vector_page_setprot(PROT_READ | PROT_WRITE | PROT_EXEC);
69 #endif
70 
71 	memcpy(fiqvector, func, size);
72 
73 #if !defined(__ARM_FIQ_INDIRECT)
74 	vector_page_setprot(PROT_READ | PROT_EXEC);
75 #endif
76 	cpu_icache_sync_range((vaddr_t) fiqvector, size);
77 }
78 
79 /*
80  * fiq_claim:
81  *
82  *	Claim the FIQ vector.
83  */
84 int
fiq_claim(struct fiqhandler * fh)85 fiq_claim(struct fiqhandler *fh)
86 {
87 	struct fiqhandler *ofh;
88 	u_int oldirqstate;
89 	int error = 0;
90 
91 	if (fh->fh_size > 0x100)
92 		return (EFBIG);
93 
94 	oldirqstate = disable_interrupts(FIQ_BIT);
95 
96 	if ((ofh = TAILQ_FIRST(&fiqhandler_stack)) != NULL) {
97 		if ((ofh->fh_flags & FH_CANPUSH) == 0) {
98 			error = EBUSY;
99 			goto out;
100 		}
101 
102 		/* Save the previous FIQ handler's registers. */
103 		if (ofh->fh_regs != NULL)
104 			fiq_getregs(ofh->fh_regs);
105 	}
106 
107 	/* Set FIQ mode registers to ours. */
108 	if (fh->fh_regs != NULL)
109 		fiq_setregs(fh->fh_regs);
110 
111 	TAILQ_INSERT_HEAD(&fiqhandler_stack, fh, fh_list);
112 
113 	/* Now copy the actual handler into place. */
114 	fiq_installhandler(fh->fh_func, fh->fh_size);
115 
116 	/* Make sure FIQs are enabled when we return. */
117 	oldirqstate &= ~FIQ_BIT;
118 
119  out:
120 	restore_interrupts(oldirqstate);
121 	return (error);
122 }
123 
124 /*
125  * fiq_release:
126  *
127  *	Release the FIQ vector.
128  */
129 void
fiq_release(struct fiqhandler * fh)130 fiq_release(struct fiqhandler *fh)
131 {
132 	u_int oldirqstate;
133 	struct fiqhandler *ofh;
134 
135 	oldirqstate = disable_interrupts(FIQ_BIT);
136 
137 	/*
138 	 * If we are the currently active FIQ handler, then we
139 	 * need to save our registers and pop the next one back
140 	 * into the vector.
141 	 */
142 	if (fh == TAILQ_FIRST(&fiqhandler_stack)) {
143 		if (fh->fh_regs != NULL)
144 			fiq_getregs(fh->fh_regs);
145 		TAILQ_REMOVE(&fiqhandler_stack, fh, fh_list);
146 		if ((ofh = TAILQ_FIRST(&fiqhandler_stack)) != NULL) {
147 			if (ofh->fh_regs != NULL)
148 				fiq_setregs(ofh->fh_regs);
149 			fiq_installhandler(ofh->fh_func, ofh->fh_size);
150 		}
151 	} else
152 		TAILQ_REMOVE(&fiqhandler_stack, fh, fh_list);
153 
154 	if (TAILQ_FIRST(&fiqhandler_stack) == NULL) {
155 		/* Copy the NULL handler back down into the vector. */
156 		fiq_installhandler(fiq_nullhandler,
157 		    (size_t)(fiq_nullhandler_end - fiq_nullhandler));
158 
159 		/* Make sure FIQs are disabled when we return. */
160 		oldirqstate |= FIQ_BIT;
161 	}
162 
163 	restore_interrupts(oldirqstate);
164 }
165