Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/kernel/irq/handle.c | |
3 | * | |
a34db9b2 IM |
4 | * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar |
5 | * Copyright (C) 2005-2006, Thomas Gleixner, Russell King | |
1da177e4 LT |
6 | * |
7 | * This file contains the core interrupt handling code. | |
a34db9b2 IM |
8 | * |
9 | * Detailed information is available in Documentation/DocBook/genericirq | |
10 | * | |
1da177e4 LT |
11 | */ |
12 | ||
13 | #include <linux/irq.h> | |
1da177e4 | 14 | #include <linux/random.h> |
3795de23 | 15 | #include <linux/sched.h> |
1da177e4 LT |
16 | #include <linux/interrupt.h> |
17 | #include <linux/kernel_stat.h> | |
6fa3eb70 | 18 | #include <linux/slab.h> |
3795de23 | 19 | |
ad8d75ff | 20 | #include <trace/events/irq.h> |
1da177e4 LT |
21 | |
22 | #include "internals.h" | |
6fa3eb70 S |
23 | #define TIME_6MS 6000000 |
24 | #define TIME_3MS 3000000 | |
25 | #ifdef CONFIG_MTPROF_CPUTIME | |
26 | /* cputime monitor en/disable value */ | |
27 | extern int mtsched_enabled; | |
28 | #ifdef CONFIG_MT_ENG_BUILD | |
29 | #define MAX_THREAD_COUNT 6000 // max debug thread count, if reach the level, stop store new thread informaiton. | |
30 | #else | |
31 | #define MAX_THREAD_COUNT 3000 | |
32 | #endif | |
33 | extern int proc_count; | |
34 | #endif | |
1da177e4 | 35 | |
6a6de9ef TG |
36 | /** |
37 | * handle_bad_irq - handle spurious and unhandled irqs | |
43a1dd50 HK |
38 | * @irq: the interrupt number |
39 | * @desc: description of the interrupt | |
43a1dd50 HK |
40 | * |
41 | * Handles spurious and unhandled IRQ's. It also prints a debugmessage. | |
6a6de9ef | 42 | */ |
d6c88a50 | 43 | void handle_bad_irq(unsigned int irq, struct irq_desc *desc) |
6a6de9ef | 44 | { |
43f77759 | 45 | print_irq_desc(irq, desc); |
d6c88a50 | 46 | kstat_incr_irqs_this_cpu(irq, desc); |
6a6de9ef TG |
47 | ack_bad_irq(irq); |
48 | } | |
49 | ||
1da177e4 LT |
50 | /* |
51 | * Special, empty irq handler: | |
52 | */ | |
7d12e780 | 53 | irqreturn_t no_action(int cpl, void *dev_id) |
1da177e4 LT |
54 | { |
55 | return IRQ_NONE; | |
56 | } | |
57 | ||
f48fe81e TG |
58 | static void warn_no_thread(unsigned int irq, struct irqaction *action) |
59 | { | |
60 | if (test_and_set_bit(IRQTF_WARNED, &action->thread_flags)) | |
61 | return; | |
62 | ||
63 | printk(KERN_WARNING "IRQ %d device %s returned IRQ_WAKE_THREAD " | |
64 | "but no thread function available.", irq, action->name); | |
65 | } | |
66 | ||
b5faba21 TG |
67 | static void irq_wake_thread(struct irq_desc *desc, struct irqaction *action) |
68 | { | |
69 | /* | |
69592db2 AG |
70 | * In case the thread crashed and was killed we just pretend that |
71 | * we handled the interrupt. The hardirq handler has disabled the | |
72 | * device interrupt, so no irq storm is lurking. | |
73 | */ | |
74 | if (action->thread->flags & PF_EXITING) | |
75 | return; | |
76 | ||
77 | /* | |
78 | * Wake up the handler thread for this action. If the | |
b5faba21 TG |
79 | * RUNTHREAD bit is already set, nothing to do. |
80 | */ | |
69592db2 | 81 | if (test_and_set_bit(IRQTF_RUNTHREAD, &action->thread_flags)) |
b5faba21 TG |
82 | return; |
83 | ||
84 | /* | |
85 | * It's safe to OR the mask lockless here. We have only two | |
86 | * places which write to threads_oneshot: This code and the | |
87 | * irq thread. | |
88 | * | |
89 | * This code is the hard irq context and can never run on two | |
90 | * cpus in parallel. If it ever does we have more serious | |
91 | * problems than this bitmask. | |
92 | * | |
93 | * The irq threads of this irq which clear their "running" bit | |
94 | * in threads_oneshot are serialized via desc->lock against | |
95 | * each other and they are serialized against this code by | |
96 | * IRQS_INPROGRESS. | |
97 | * | |
98 | * Hard irq handler: | |
99 | * | |
100 | * spin_lock(desc->lock); | |
101 | * desc->state |= IRQS_INPROGRESS; | |
102 | * spin_unlock(desc->lock); | |
103 | * set_bit(IRQTF_RUNTHREAD, &action->thread_flags); | |
104 | * desc->threads_oneshot |= mask; | |
105 | * spin_lock(desc->lock); | |
106 | * desc->state &= ~IRQS_INPROGRESS; | |
107 | * spin_unlock(desc->lock); | |
108 | * | |
109 | * irq thread: | |
110 | * | |
111 | * again: | |
112 | * spin_lock(desc->lock); | |
113 | * if (desc->state & IRQS_INPROGRESS) { | |
114 | * spin_unlock(desc->lock); | |
115 | * while(desc->state & IRQS_INPROGRESS) | |
116 | * cpu_relax(); | |
117 | * goto again; | |
118 | * } | |
119 | * if (!test_bit(IRQTF_RUNTHREAD, &action->thread_flags)) | |
120 | * desc->threads_oneshot &= ~mask; | |
121 | * spin_unlock(desc->lock); | |
122 | * | |
123 | * So either the thread waits for us to clear IRQS_INPROGRESS | |
124 | * or we are waiting in the flow handler for desc->lock to be | |
125 | * released before we reach this point. The thread also checks | |
126 | * IRQTF_RUNTHREAD under desc->lock. If set it leaves | |
127 | * threads_oneshot untouched and runs the thread another time. | |
128 | */ | |
129 | desc->threads_oneshot |= action->thread_mask; | |
7140ea19 IY |
130 | |
131 | /* | |
132 | * We increment the threads_active counter in case we wake up | |
133 | * the irq thread. The irq thread decrements the counter when | |
134 | * it returns from the handler or in the exit path and wakes | |
135 | * up waiters which are stuck in synchronize_irq() when the | |
136 | * active count becomes zero. synchronize_irq() is serialized | |
137 | * against this code (hard irq handler) via IRQS_INPROGRESS | |
138 | * like the finalize_oneshot() code. See comment above. | |
139 | */ | |
140 | atomic_inc(&desc->threads_active); | |
141 | ||
b5faba21 TG |
142 | wake_up_process(action->thread); |
143 | } | |
144 | ||
1277a532 TG |
145 | irqreturn_t |
146 | handle_irq_event_percpu(struct irq_desc *desc, struct irqaction *action) | |
1da177e4 | 147 | { |
70433c01 | 148 | irqreturn_t retval = IRQ_NONE; |
775f4b29 | 149 | unsigned int flags = 0, irq = desc->irq_data.irq; |
1da177e4 | 150 | |
6fa3eb70 S |
151 | #ifdef CONFIG_MTPROF_IRQ_DURATION |
152 | unsigned long long t1, t2, dur; | |
153 | #endif | |
1da177e4 | 154 | do { |
70433c01 TG |
155 | irqreturn_t res; |
156 | ||
af39241b | 157 | trace_irq_handler_entry(irq, action); |
6fa3eb70 S |
158 | #ifdef CONFIG_MTPROF_IRQ_DURATION |
159 | t1 = sched_clock(); | |
70433c01 | 160 | res = action->handler(irq, action->dev_id); |
6fa3eb70 S |
161 | t2 = sched_clock(); |
162 | dur = t2 - t1; | |
163 | action->duration += dur; | |
164 | action->count++; | |
165 | action->dur_max = max(dur,action->dur_max); | |
166 | action->dur_min = min(dur,action->dur_min); | |
167 | #ifdef CONFIG_MTPROF_CPUTIME | |
168 | if(mtsched_enabled == 1) | |
169 | { | |
170 | int isr_find = 0; | |
171 | struct mtk_isr_info *mtk_isr_point = current->se.mtk_isr; | |
172 | struct mtk_isr_info *mtk_isr_current = mtk_isr_point; | |
173 | char *isr_name = NULL; | |
174 | ||
175 | current->se.mtk_isr_time += dur; | |
176 | while((current->se.mtk_isr!= NULL) && (mtk_isr_point != NULL)) | |
177 | { | |
178 | if(mtk_isr_point->isr_num == irq) | |
179 | { | |
180 | mtk_isr_point->isr_time += dur; | |
181 | mtk_isr_point->isr_count++; | |
182 | isr_find = 1; | |
183 | break; | |
184 | } | |
185 | mtk_isr_current = mtk_isr_point; | |
186 | mtk_isr_point = mtk_isr_point -> next; | |
187 | } | |
188 | ||
189 | if((isr_find == 0) && (proc_count < MAX_THREAD_COUNT)) | |
190 | { | |
191 | mtk_isr_point = kmalloc(sizeof(struct mtk_isr_info), GFP_ATOMIC); | |
192 | if(mtk_isr_point == NULL) | |
193 | { | |
194 | printk(KERN_ERR"cant' alloc mtk_isr_info mem!\n"); | |
195 | } | |
196 | else | |
197 | { | |
198 | mtk_isr_point->isr_num = irq; | |
199 | mtk_isr_point->isr_time = dur; | |
200 | mtk_isr_point->isr_count = 1; | |
201 | mtk_isr_point->next = NULL; | |
202 | if(mtk_isr_current == NULL) | |
203 | { | |
204 | current->se.mtk_isr = mtk_isr_point; | |
205 | } | |
206 | else | |
207 | { | |
208 | mtk_isr_current->next = mtk_isr_point; | |
209 | } | |
210 | ||
211 | isr_name = kmalloc(sizeof(action->name),GFP_ATOMIC); | |
212 | if(isr_name != NULL) | |
213 | { | |
214 | strcpy(isr_name, action->name); | |
215 | mtk_isr_point->isr_name = isr_name; | |
216 | } | |
217 | else | |
218 | { | |
219 | printk(KERN_ERR"cant' alloc isr_name mem!\n"); | |
220 | } | |
221 | current->se.mtk_isr_count++; | |
222 | } | |
223 | } | |
224 | } | |
225 | #endif | |
226 | #else | |
227 | res = action->handler(irq, action->dev_id); | |
228 | #endif | |
70433c01 | 229 | trace_irq_handler_exit(irq, action, res); |
3aa551c9 | 230 | |
1204e956 TG |
231 | if (WARN_ONCE(!irqs_disabled(),"irq %u handler %pF enabled interrupts\n", |
232 | irq, action->handler)) | |
b738a50a TG |
233 | local_irq_disable(); |
234 | ||
70433c01 | 235 | switch (res) { |
3aa551c9 | 236 | case IRQ_WAKE_THREAD: |
f48fe81e TG |
237 | /* |
238 | * Catch drivers which return WAKE_THREAD but | |
239 | * did not set up a thread function | |
240 | */ | |
241 | if (unlikely(!action->thread_fn)) { | |
242 | warn_no_thread(irq, action); | |
243 | break; | |
244 | } | |
245 | ||
b5faba21 | 246 | irq_wake_thread(desc, action); |
3aa551c9 | 247 | |
3aa551c9 TG |
248 | /* Fall through to add to randomness */ |
249 | case IRQ_HANDLED: | |
775f4b29 | 250 | flags |= action->flags; |
3aa551c9 TG |
251 | break; |
252 | ||
253 | default: | |
254 | break; | |
255 | } | |
256 | ||
70433c01 | 257 | retval |= res; |
1da177e4 LT |
258 | action = action->next; |
259 | } while (action); | |
260 | ||
775f4b29 | 261 | add_interrupt_randomness(irq, flags); |
1da177e4 | 262 | |
4912609f | 263 | if (!noirqdebug) |
70433c01 | 264 | note_interrupt(irq, desc, retval); |
1277a532 | 265 | return retval; |
4912609f TG |
266 | } |
267 | ||
268 | irqreturn_t handle_irq_event(struct irq_desc *desc) | |
269 | { | |
270 | struct irqaction *action = desc->action; | |
271 | irqreturn_t ret; | |
272 | ||
2a0d6fb3 | 273 | desc->istate &= ~IRQS_PENDING; |
32f4125e | 274 | irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS); |
4912609f TG |
275 | raw_spin_unlock(&desc->lock); |
276 | ||
277 | ret = handle_irq_event_percpu(desc, action); | |
278 | ||
279 | raw_spin_lock(&desc->lock); | |
32f4125e | 280 | irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS); |
4912609f TG |
281 | return ret; |
282 | } |