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