summaryrefslogtreecommitdiffstats
path: root/arch/arm/kernel/fiq.c
blob: 7a16832c7f813cac8f53625a7f6865972298b97f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 *  linux/arch/arm/kernel/fiq.c
 *
 *  Copyright (C) 1998 Russell King
 *  Copyright (C) 1998, 1999 Phil Blundell
 *
 *  FIQ support written by Philip Blundell <philb@gnu.org>, 1998.
 *
 *  FIQ support re-written by Russell King to be more generic
 *
 * We now properly support a method by which the FIQ handlers can
 * be stacked onto the vector.  We still do not support sharing
 * the FIQ vector itself.
 *
 * Operation is as follows:
 *  1. Owner A claims FIQ:
 *     - default_fiq relinquishes control.
 *  2. Owner A:
 *     - inserts code.
 *     - sets any registers,
 *     - enables FIQ.
 *  3. Owner B claims FIQ:
 *     - if owner A has a relinquish function.
 *       - disable FIQs.
 *       - saves any registers.
 *       - returns zero.
 *  4. Owner B:
 *     - inserts code.
 *     - sets any registers,
 *     - enables FIQ.
 *  5. Owner B releases FIQ:
 *     - Owner A is asked to reacquire FIQ:
 *	 - inserts code.
 *	 - restores saved registers.
 *	 - enables FIQ.
 *  6. Goto 3
 */
#include <linux/config.h>
#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/init.h>

#include <asm/fiq.h>
#include <asm/io.h>
#include <asm/pgtable.h>
#include <asm/system.h>
#include <asm/uaccess.h>

#define FIQ_VECTOR 0x1c

static unsigned long no_fiq_insn;

#ifdef CONFIG_CPU_32
static inline void unprotect_page_0(void)
{
	modify_domain(DOMAIN_USER, DOMAIN_MANAGER);
}

static inline void protect_page_0(void)
{
	modify_domain(DOMAIN_USER, DOMAIN_CLIENT);
}
#else

#define unprotect_page_0()
#define protect_page_0()

#endif

/* Default reacquire function
 * - we always relinquish FIQ control
 * - we always reacquire FIQ control
 */
int fiq_def_op(void *ref, int relinquish)
{
	if (!relinquish) {
		unprotect_page_0();
		*(unsigned long *)FIQ_VECTOR = no_fiq_insn;
		protect_page_0();
		flush_icache_range(FIQ_VECTOR, FIQ_VECTOR + 4);
	}

	return 0;
}

static struct fiq_handler default_owner =
	{ NULL, "default", fiq_def_op, NULL };
static struct fiq_handler *current_fiq = &default_owner;

int get_fiq_list(char *buf)
{
	char *p = buf;

	if (current_fiq != &default_owner)
		p += sprintf(p, "FIQ:              %s\n",
			     current_fiq->name);

	return p - buf;
}

void set_fiq_handler(void *start, unsigned int length)
{
	unprotect_page_0();

	memcpy((void *)FIQ_VECTOR, start, length);

	protect_page_0();
	flush_icache_range(FIQ_VECTOR, FIQ_VECTOR + length);
}

/*
 * Taking an interrupt in FIQ mode is death, so both these functions
 * disable irqs for the duration. 
 */
void set_fiq_regs(struct pt_regs *regs)
{
	register unsigned long tmp, tmp2;
	__asm__ volatile (
#ifdef CONFIG_CPU_26
	"mov	%0, pc
	bic	%1, %0, #0x3
	orr	%1, %1, #0x0c000001
	teqp	%1, #0		@ select FIQ mode
	mov	r0, r0
	ldmia	%2, {r8 - r14}
	teqp	%0, #0		@ return to SVC mode
	mov	r0, r0"
#endif
#ifdef CONFIG_CPU_32
	"mrs	%0, cpsr
	bic	%1, %0, #0xf
	orr	%1, %1, #0xc1
	msr	cpsr, %1	@ select FIQ mode
	mov	r0, r0
	ldmia	%2, {r8 - r14}
	msr	cpsr, %0	@ return to SVC mode
	mov	r0, r0"
#endif
	: "=r" (tmp), "=r" (tmp2)
	: "r" (&regs->ARM_r8)
	/* These registers aren't modified by the above code in a way
	   visible to the compiler, but we mark them as clobbers anyway
	   so that GCC won't put any of the input or output operands in
	   them.  */
	: "r8", "r9", "r10", "r11", "r12", "r13", "r14");
}

void get_fiq_regs(struct pt_regs *regs)
{
	register unsigned long tmp, tmp2;
	__asm__ volatile (
#ifdef CONFIG_CPU_26
	"mov	%0, pc
	bic	%1, %0, #0x3
	orr	%1, %1, #0x0c000001
	teqp	%1, #0		@ select FIQ mode
	mov	r0, r0
	stmia	%2, {r8 - r14}
	teqp	%0, #0		@ return to SVC mode
	mov	r0, r0"
#endif
#ifdef CONFIG_CPU_32
	"mrs	%0, cpsr
	bic	%1, %0, #0xf
	orr	%1, %1, #0xc1
	msr	cpsr, %1	@ select FIQ mode
	mov	r0, r0
	stmia	%2, {r8 - r14}
	msr	cpsr, %0	@ return to SVC mode
	mov	r0, r0"
#endif
	: "=r" (tmp), "=r" (tmp2)
	: "r" (&regs->ARM_r8)
	/* These registers aren't modified by the above code in a way
	   visible to the compiler, but we mark them as clobbers anyway
	   so that GCC won't put any of the input or output operands in
	   them.  */
	: "r8", "r9", "r10", "r11", "r12", "r13", "r14");
}

int claim_fiq(struct fiq_handler *f)
{
	int ret = 0;

	if (current_fiq) {
		ret = -EBUSY;

		if (current_fiq->fiq_op != NULL)
			ret = current_fiq->fiq_op(current_fiq->dev_id, 1);
	}

	if (!ret) {
		f->next = current_fiq;
		current_fiq = f;
	}

	return ret;
}

void release_fiq(struct fiq_handler *f)
{
	if (current_fiq != f) {
		printk(KERN_ERR "%s FIQ trying to release %s FIQ\n",
		       f->name, current_fiq->name);
#ifdef CONFIG_DEBUG_ERRORS
		__backtrace();
#endif
		return;
	}

	do
		current_fiq = current_fiq->next;
	while (current_fiq->fiq_op(current_fiq->dev_id, 0));
}

void __init init_FIQ(void)
{
	no_fiq_insn = *(unsigned long *)FIQ_VECTOR;
	set_fs(get_fs());
}