1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/arch/arm/kernel/fiq.c
4 *
5 * Copyright (C) 1998 Russell King
6 * Copyright (C) 1998, 1999 Phil Blundell
7 *
8 * FIQ support written by Philip Blundell <philb@gnu.org>, 1998.
9 *
10 * FIQ support re-written by Russell King to be more generic
11 *
12 * We now properly support a method by which the FIQ handlers can
13 * be stacked onto the vector. We still do not support sharing
14 * the FIQ vector itself.
15 *
16 * Operation is as follows:
17 * 1. Owner A claims FIQ:
18 * - default_fiq relinquishes control.
19 * 2. Owner A:
20 * - inserts code.
21 * - sets any registers,
22 * - enables FIQ.
23 * 3. Owner B claims FIQ:
24 * - if owner A has a relinquish function.
25 * - disable FIQs.
26 * - saves any registers.
27 * - returns zero.
28 * 4. Owner B:
29 * - inserts code.
30 * - sets any registers,
31 * - enables FIQ.
32 * 5. Owner B releases FIQ:
33 * - Owner A is asked to reacquire FIQ:
34 * - inserts code.
35 * - restores saved registers.
36 * - enables FIQ.
37 * 6. Goto 3
38 */
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <linux/init.h>
42 #include <linux/interrupt.h>
43 #include <linux/seq_file.h>
44
45 #include <asm/cacheflush.h>
46 #include <asm/cp15.h>
47 #include <asm/fiq.h>
48 #include <asm/irq.h>
49 #include <asm/traps.h>
50
51 #ifdef CONFIG_RANDOMIZE_BASE
52 #define FIQ_OFFSET ({ \
53 extern void *vector_fiq_offset; \
54 (unsigned)&vector_fiq_offset - kaslr_offset(); \
55 })
56 #else
57 #define FIQ_OFFSET ({ \
58 extern void *vector_fiq_offset; \
59 (unsigned)&vector_fiq_offset; \
60 })
61 #endif
62
63 static unsigned long dfl_fiq_insn;
64 static struct pt_regs dfl_fiq_regs;
65
66 /* Default reacquire function
67 * - we always relinquish FIQ control
68 * - we always reacquire FIQ control
69 */
fiq_def_op(void * ref,int relinquish)70 static int fiq_def_op(void *ref, int relinquish)
71 {
72 if (!relinquish) {
73 /* Restore default handler and registers */
74 local_fiq_disable();
75 set_fiq_regs(&dfl_fiq_regs);
76 set_fiq_handler(&dfl_fiq_insn, sizeof(dfl_fiq_insn));
77 local_fiq_enable();
78
79 /* FIXME: notify irq controller to standard enable FIQs */
80 }
81
82 return 0;
83 }
84
85 static struct fiq_handler default_owner = {
86 .name = "default",
87 .fiq_op = fiq_def_op,
88 };
89
90 static struct fiq_handler *current_fiq = &default_owner;
91
show_fiq_list(struct seq_file * p,int prec)92 int show_fiq_list(struct seq_file *p, int prec)
93 {
94 if (current_fiq != &default_owner)
95 seq_printf(p, "%*s: %s\n", prec, "FIQ",
96 current_fiq->name);
97
98 return 0;
99 }
100
set_fiq_handler(void * start,unsigned int length)101 void set_fiq_handler(void *start, unsigned int length)
102 {
103 void *base = vectors_page;
104 unsigned offset = FIQ_OFFSET;
105
106 memcpy(base + offset, start, length);
107 if (!cache_is_vipt_nonaliasing())
108 flush_icache_range((unsigned long)base + offset,
109 (unsigned long)base + offset + length);
110 flush_icache_range(0xffff0000 + offset, 0xffff0000 + offset + length);
111 }
112
claim_fiq(struct fiq_handler * f)113 int claim_fiq(struct fiq_handler *f)
114 {
115 int ret = 0;
116
117 if (current_fiq) {
118 ret = -EBUSY;
119
120 if (current_fiq->fiq_op != NULL)
121 ret = current_fiq->fiq_op(current_fiq->dev_id, 1);
122 }
123
124 if (!ret) {
125 f->next = current_fiq;
126 current_fiq = f;
127 }
128
129 return ret;
130 }
131
release_fiq(struct fiq_handler * f)132 void release_fiq(struct fiq_handler *f)
133 {
134 if (current_fiq != f) {
135 pr_err("%s FIQ trying to release %s FIQ\n",
136 f->name, current_fiq->name);
137 dump_stack();
138 return;
139 }
140
141 do
142 current_fiq = current_fiq->next;
143 while (current_fiq->fiq_op(current_fiq->dev_id, 0));
144 }
145
146 static int fiq_start;
147
enable_fiq(int fiq)148 void enable_fiq(int fiq)
149 {
150 enable_irq(fiq + fiq_start);
151 }
152
disable_fiq(int fiq)153 void disable_fiq(int fiq)
154 {
155 disable_irq(fiq + fiq_start);
156 }
157
158 EXPORT_SYMBOL(set_fiq_handler);
159 EXPORT_SYMBOL(__set_fiq_regs); /* defined in fiqasm.S */
160 EXPORT_SYMBOL(__get_fiq_regs); /* defined in fiqasm.S */
161 EXPORT_SYMBOL(claim_fiq);
162 EXPORT_SYMBOL(release_fiq);
163 EXPORT_SYMBOL(enable_fiq);
164 EXPORT_SYMBOL(disable_fiq);
165
init_FIQ(int start)166 void __init init_FIQ(int start)
167 {
168 unsigned offset = FIQ_OFFSET;
169 dfl_fiq_insn = *(unsigned long *)(0xffff0000 + offset);
170 get_fiq_regs(&dfl_fiq_regs);
171 fiq_start = start;
172 }
173