http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / kernel / linux / arch / arm / kernel / fiq.c
1 /*
2  *  linux/arch/arm/kernel/fiq.c
3  *
4  *  Copyright (C) 1998 Russell King
5  *  Copyright (C) 1998, 1999 Phil Blundell
6  *
7  *  FIQ support written by Philip Blundell <philb@gnu.org>, 1998.
8  *
9  *  FIQ support re-written by Russell King to be more generic
10  *
11  * We now properly support a method by which the FIQ handlers can
12  * be stacked onto the vector.  We still do not support sharing
13  * the FIQ vector itself.
14  *
15  * Operation is as follows:
16  *  1. Owner A claims FIQ:
17  *     - default_fiq relinquishes control.
18  *  2. Owner A:
19  *     - inserts code.
20  *     - sets any registers,
21  *     - enables FIQ.
22  *  3. Owner B claims FIQ:
23  *     - if owner A has a relinquish function.
24  *       - disable FIQs.
25  *       - saves any registers.
26  *       - returns zero.
27  *  4. Owner B:
28  *     - inserts code.
29  *     - sets any registers,
30  *     - enables FIQ.
31  *  5. Owner B releases FIQ:
32  *     - Owner A is asked to reacquire FIQ:
33  *       - inserts code.
34  *       - restores saved registers.
35  *       - enables FIQ.
36  *  6. Goto 3
37  */
38 #include <linux/module.h>
39 #include <linux/kernel.h>
40 #include <linux/init.h>
41 #include <linux/seq_file.h>
42
43 #include <asm/cacheflush.h>
44 #include <asm/fiq.h>
45 #include <asm/irq.h>
46 #include <asm/system.h>
47 #include <asm/uaccess.h>
48
49 #define FIQ_VECTOR (vectors_base() + 0x1c)
50
51 static unsigned long no_fiq_insn;
52
53 static inline void unprotect_page_0(void)
54 {
55         modify_domain(DOMAIN_USER, DOMAIN_MANAGER);
56 }
57
58 static inline void protect_page_0(void)
59 {
60         modify_domain(DOMAIN_USER, DOMAIN_CLIENT);
61 }
62
63 /* Default reacquire function
64  * - we always relinquish FIQ control
65  * - we always reacquire FIQ control
66  */
67 static int fiq_def_op(void *ref, int relinquish)
68 {
69         if (!relinquish) {
70                 unprotect_page_0();
71                 *(unsigned long *)FIQ_VECTOR = no_fiq_insn;
72                 protect_page_0();
73                 flush_icache_range(FIQ_VECTOR, FIQ_VECTOR + 4);
74         }
75
76         return 0;
77 }
78
79 static struct fiq_handler default_owner = {
80         .name   = "default",
81         .fiq_op = fiq_def_op,
82 };
83
84 static struct fiq_handler *current_fiq = &default_owner;
85
86 int show_fiq_list(struct seq_file *p, void *v)
87 {
88         if (current_fiq != &default_owner)
89                 seq_printf(p, "FIQ:              %s\n", current_fiq->name);
90
91         return 0;
92 }
93
94 void set_fiq_handler(void *start, unsigned int length)
95 {
96         unprotect_page_0();
97
98         memcpy((void *)FIQ_VECTOR, start, length);
99
100         protect_page_0();
101         flush_icache_range(FIQ_VECTOR, FIQ_VECTOR + length);
102 }
103
104 /*
105  * Taking an interrupt in FIQ mode is death, so both these functions
106  * disable irqs for the duration. 
107  */
108 void set_fiq_regs(struct pt_regs *regs)
109 {
110         register unsigned long tmp, tmp2;
111         __asm__ volatile (
112         "mrs    %0, cpsr\n\
113         mov     %1, %3\n\
114         msr     cpsr_c, %1      @ select FIQ mode\n\
115         mov     r0, r0\n\
116         ldmia   %2, {r8 - r14}\n\
117         msr     cpsr_c, %0      @ return to SVC mode\n\
118         mov     r0, r0"
119         : "=&r" (tmp), "=&r" (tmp2)
120         : "r" (&regs->ARM_r8), "I" (PSR_I_BIT | PSR_F_BIT | FIQ_MODE)
121         /* These registers aren't modified by the above code in a way
122            visible to the compiler, but we mark them as clobbers anyway
123            so that GCC won't put any of the input or output operands in
124            them.  */
125         : "r8", "r9", "r10", "r11", "r12", "r13", "r14");
126 }
127
128 void get_fiq_regs(struct pt_regs *regs)
129 {
130         register unsigned long tmp, tmp2;
131         __asm__ volatile (
132         "mrs    %0, cpsr\n\
133         mov     %1, %3\n\
134         msr     cpsr_c, %1      @ select FIQ mode\n\
135         mov     r0, r0\n\
136         stmia   %2, {r8 - r14}\n\
137         msr     cpsr_c, %0      @ return to SVC mode\n\
138         mov     r0, r0"
139         : "=&r" (tmp), "=&r" (tmp2)
140         : "r" (&regs->ARM_r8), "I" (PSR_I_BIT | PSR_F_BIT | FIQ_MODE)
141         /* These registers aren't modified by the above code in a way
142            visible to the compiler, but we mark them as clobbers anyway
143            so that GCC won't put any of the input or output operands in
144            them.  */
145         : "r8", "r9", "r10", "r11", "r12", "r13", "r14");
146 }
147
148 int claim_fiq(struct fiq_handler *f)
149 {
150         int ret = 0;
151
152         if (current_fiq) {
153                 ret = -EBUSY;
154
155                 if (current_fiq->fiq_op != NULL)
156                         ret = current_fiq->fiq_op(current_fiq->dev_id, 1);
157         }
158
159         if (!ret) {
160                 f->next = current_fiq;
161                 current_fiq = f;
162         }
163
164         return ret;
165 }
166
167 void release_fiq(struct fiq_handler *f)
168 {
169         if (current_fiq != f) {
170                 printk(KERN_ERR "%s FIQ trying to release %s FIQ\n",
171                        f->name, current_fiq->name);
172                 dump_stack();
173                 return;
174         }
175
176         do
177                 current_fiq = current_fiq->next;
178         while (current_fiq->fiq_op(current_fiq->dev_id, 0));
179 }
180
181 void enable_fiq(int fiq)
182 {
183         enable_irq(fiq + FIQ_START);
184 }
185
186 void disable_fiq(int fiq)
187 {
188         disable_irq(fiq + FIQ_START);
189 }
190
191 EXPORT_SYMBOL(set_fiq_handler);
192 EXPORT_SYMBOL(set_fiq_regs);
193 EXPORT_SYMBOL(get_fiq_regs);
194 EXPORT_SYMBOL(claim_fiq);
195 EXPORT_SYMBOL(release_fiq);
196 EXPORT_SYMBOL(enable_fiq);
197 EXPORT_SYMBOL(disable_fiq);
198
199 void __init init_FIQ(void)
200 {
201         no_fiq_insn = *(unsigned long *)FIQ_VECTOR;
202         set_fs(get_fs());
203 }