blob: 5fc424803a1788409a3b51d479c242d9ee16b6ae [file] [log] [blame]
Yi Li6a01f232009-01-07 23:14:39 +08001/* -*- linux-c -*-
2 * linux/arch/blackfin/kernel/ipipe.c
3 *
4 * Copyright (C) 2005-2007 Philippe Gerum.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
9 * USA; either version 2 of the License, or (at your option) any later
10 * version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 *
21 * Architecture-dependent I-pipe support for the Blackfin.
22 */
23
24#include <linux/kernel.h>
25#include <linux/sched.h>
26#include <linux/module.h>
27#include <linux/interrupt.h>
28#include <linux/percpu.h>
29#include <linux/bitops.h>
30#include <linux/slab.h>
31#include <linux/errno.h>
32#include <linux/kthread.h>
33#include <asm/unistd.h>
34#include <asm/system.h>
35#include <asm/atomic.h>
36#include <asm/io.h>
37
Yi Li6a01f232009-01-07 23:14:39 +080038DEFINE_PER_CPU(struct pt_regs, __ipipe_tick_regs);
39
Yi Li6a01f232009-01-07 23:14:39 +080040asmlinkage void asm_do_IRQ(unsigned int irq, struct pt_regs *regs);
41
42static void __ipipe_no_irqtail(void);
43
44unsigned long __ipipe_irq_tail_hook = (unsigned long)&__ipipe_no_irqtail;
45EXPORT_SYMBOL(__ipipe_irq_tail_hook);
46
47unsigned long __ipipe_core_clock;
48EXPORT_SYMBOL(__ipipe_core_clock);
49
50unsigned long __ipipe_freq_scale;
51EXPORT_SYMBOL(__ipipe_freq_scale);
52
53atomic_t __ipipe_irq_lvdepth[IVG15 + 1];
54
55unsigned long __ipipe_irq_lvmask = __all_masked_irq_flags;
56EXPORT_SYMBOL(__ipipe_irq_lvmask);
57
58static void __ipipe_ack_irq(unsigned irq, struct irq_desc *desc)
59{
60 desc->ipipe_ack(irq, desc);
61}
62
63/*
64 * __ipipe_enable_pipeline() -- We are running on the boot CPU, hw
65 * interrupts are off, and secondary CPUs are still lost in space.
66 */
67void __ipipe_enable_pipeline(void)
68{
69 unsigned irq;
70
71 __ipipe_core_clock = get_cclk(); /* Fetch this once. */
72 __ipipe_freq_scale = 1000000000UL / __ipipe_core_clock;
73
74 for (irq = 0; irq < NR_IRQS; ++irq)
75 ipipe_virtualize_irq(ipipe_root_domain,
76 irq,
77 (ipipe_irq_handler_t)&asm_do_IRQ,
78 NULL,
79 &__ipipe_ack_irq,
80 IPIPE_HANDLE_MASK | IPIPE_PASS_MASK);
81}
82
83/*
84 * __ipipe_handle_irq() -- IPIPE's generic IRQ handler. An optimistic
85 * interrupt protection log is maintained here for each domain. Hw
86 * interrupts are masked on entry.
87 */
88void __ipipe_handle_irq(unsigned irq, struct pt_regs *regs)
89{
Philippe Gerum9bd50df2009-03-04 16:52:38 +080090 struct ipipe_percpu_domain_data *p = ipipe_root_cpudom_ptr();
Yi Li6a01f232009-01-07 23:14:39 +080091 struct ipipe_domain *this_domain, *next_domain;
92 struct list_head *head, *pos;
93 int m_ack, s = -1;
94
95 /*
96 * Software-triggered IRQs do not need any ack. The contents
97 * of the register frame should only be used when processing
98 * the timer interrupt, but not for handling any other
99 * interrupt.
100 */
101 m_ack = (regs == NULL || irq == IRQ_SYSTMR || irq == IRQ_CORETMR);
Yi Li6a01f232009-01-07 23:14:39 +0800102 this_domain = ipipe_current_domain;
103
104 if (unlikely(test_bit(IPIPE_STICKY_FLAG, &this_domain->irqs[irq].control)))
105 head = &this_domain->p_link;
106 else {
107 head = __ipipe_pipeline.next;
108 next_domain = list_entry(head, struct ipipe_domain, p_link);
109 if (likely(test_bit(IPIPE_WIRED_FLAG, &next_domain->irqs[irq].control))) {
110 if (!m_ack && next_domain->irqs[irq].acknowledge != NULL)
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800111 next_domain->irqs[irq].acknowledge(irq, irq_to_desc(irq));
112 if (test_bit(IPIPE_SYNCDEFER_FLAG, &p->status))
113 s = __test_and_set_bit(IPIPE_STALL_FLAG, &p->status);
Yi Li6a01f232009-01-07 23:14:39 +0800114 __ipipe_dispatch_wired(next_domain, irq);
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800115 goto out;
Yi Li6a01f232009-01-07 23:14:39 +0800116 }
117 }
118
119 /* Ack the interrupt. */
120
121 pos = head;
Yi Li6a01f232009-01-07 23:14:39 +0800122 while (pos != &__ipipe_pipeline) {
123 next_domain = list_entry(pos, struct ipipe_domain, p_link);
Yi Li6a01f232009-01-07 23:14:39 +0800124 if (test_bit(IPIPE_HANDLE_FLAG, &next_domain->irqs[irq].control)) {
Yi Li6a01f232009-01-07 23:14:39 +0800125 __ipipe_set_irq_pending(next_domain, irq);
Yi Li6a01f232009-01-07 23:14:39 +0800126 if (!m_ack && next_domain->irqs[irq].acknowledge != NULL) {
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800127 next_domain->irqs[irq].acknowledge(irq, irq_to_desc(irq));
Yi Li6a01f232009-01-07 23:14:39 +0800128 m_ack = 1;
129 }
130 }
Yi Li6a01f232009-01-07 23:14:39 +0800131 if (!test_bit(IPIPE_PASS_FLAG, &next_domain->irqs[irq].control))
132 break;
Yi Li6a01f232009-01-07 23:14:39 +0800133 pos = next_domain->p_link.next;
134 }
135
136 /*
137 * Now walk the pipeline, yielding control to the highest
138 * priority domain that has pending interrupt(s) or
139 * immediately to the current domain if the interrupt has been
140 * marked as 'sticky'. This search does not go beyond the
141 * current domain in the pipeline. We also enforce the
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800142 * additional root stage lock (blackfin-specific).
143 */
144 if (test_bit(IPIPE_SYNCDEFER_FLAG, &p->status))
145 s = __test_and_set_bit(IPIPE_STALL_FLAG, &p->status);
Yi Li6a01f232009-01-07 23:14:39 +0800146
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800147 /*
148 * If the interrupt preempted the head domain, then do not
149 * even try to walk the pipeline, unless an interrupt is
150 * pending for it.
151 */
152 if (test_bit(IPIPE_AHEAD_FLAG, &this_domain->flags) &&
153 ipipe_head_cpudom_var(irqpend_himask) == 0)
154 goto out;
Yi Li6a01f232009-01-07 23:14:39 +0800155
156 __ipipe_walk_pipeline(head);
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800157out:
Yi Li6a01f232009-01-07 23:14:39 +0800158 if (!s)
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800159 __clear_bit(IPIPE_STALL_FLAG, &p->status);
Yi Li6a01f232009-01-07 23:14:39 +0800160}
161
162int __ipipe_check_root(void)
163{
164 return ipipe_root_domain_p;
165}
166
167void __ipipe_enable_irqdesc(struct ipipe_domain *ipd, unsigned irq)
168{
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800169 struct irq_desc *desc = irq_to_desc(irq);
Philippe Gerum51387002009-04-08 07:41:55 +0000170 int prio = __ipipe_get_irq_priority(irq);
Yi Li6a01f232009-01-07 23:14:39 +0800171
172 desc->depth = 0;
173 if (ipd != &ipipe_root &&
174 atomic_inc_return(&__ipipe_irq_lvdepth[prio]) == 1)
175 __set_bit(prio, &__ipipe_irq_lvmask);
176}
177EXPORT_SYMBOL(__ipipe_enable_irqdesc);
178
179void __ipipe_disable_irqdesc(struct ipipe_domain *ipd, unsigned irq)
180{
Philippe Gerum51387002009-04-08 07:41:55 +0000181 int prio = __ipipe_get_irq_priority(irq);
Yi Li6a01f232009-01-07 23:14:39 +0800182
183 if (ipd != &ipipe_root &&
184 atomic_dec_and_test(&__ipipe_irq_lvdepth[prio]))
185 __clear_bit(prio, &__ipipe_irq_lvmask);
186}
187EXPORT_SYMBOL(__ipipe_disable_irqdesc);
188
189void __ipipe_stall_root_raw(void)
190{
191 /*
192 * This code is called by the ins{bwl} routines (see
193 * arch/blackfin/lib/ins.S), which are heavily used by the
194 * network stack. It masks all interrupts but those handled by
195 * non-root domains, so that we keep decent network transfer
196 * rates for Linux without inducing pathological jitter for
197 * the real-time domain.
198 */
199 __asm__ __volatile__ ("sti %0;" : : "d"(__ipipe_irq_lvmask));
200
201 __set_bit(IPIPE_STALL_FLAG,
202 &ipipe_root_cpudom_var(status));
203}
204
205void __ipipe_unstall_root_raw(void)
206{
207 __clear_bit(IPIPE_STALL_FLAG,
208 &ipipe_root_cpudom_var(status));
209
210 __asm__ __volatile__ ("sti %0;" : : "d"(bfin_irq_flags));
211}
212
213int __ipipe_syscall_root(struct pt_regs *regs)
214{
215 unsigned long flags;
216
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800217 /*
218 * We need to run the IRQ tail hook whenever we don't
Yi Li6a01f232009-01-07 23:14:39 +0800219 * propagate a syscall to higher domains, because we know that
220 * important operations might be pending there (e.g. Xenomai
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800221 * deferred rescheduling).
222 */
Yi Li6a01f232009-01-07 23:14:39 +0800223
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800224 if (regs->orig_p0 < NR_syscalls) {
Yi Li6a01f232009-01-07 23:14:39 +0800225 void (*hook)(void) = (void (*)(void))__ipipe_irq_tail_hook;
226 hook();
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800227 if ((current->flags & PF_EVNOTIFY) == 0)
228 return 0;
Yi Li6a01f232009-01-07 23:14:39 +0800229 }
230
231 /*
232 * This routine either returns:
233 * 0 -- if the syscall is to be passed to Linux;
234 * 1 -- if the syscall should not be passed to Linux, and no
235 * tail work should be performed;
236 * -1 -- if the syscall should not be passed to Linux but the
237 * tail work has to be performed (for handling signals etc).
238 */
239
240 if (__ipipe_event_monitored_p(IPIPE_EVENT_SYSCALL) &&
241 __ipipe_dispatch_event(IPIPE_EVENT_SYSCALL, regs) > 0) {
242 if (ipipe_root_domain_p && !in_atomic()) {
243 /*
244 * Sync pending VIRQs before _TIF_NEED_RESCHED
245 * is tested.
246 */
247 local_irq_save_hw(flags);
248 if ((ipipe_root_cpudom_var(irqpend_himask) & IPIPE_IRQMASK_VIRT) != 0)
249 __ipipe_sync_pipeline(IPIPE_IRQMASK_VIRT);
250 local_irq_restore_hw(flags);
251 return -1;
252 }
253 return 1;
254 }
255
256 return 0;
257}
258
259unsigned long ipipe_critical_enter(void (*syncfn) (void))
260{
261 unsigned long flags;
262
263 local_irq_save_hw(flags);
264
265 return flags;
266}
267
268void ipipe_critical_exit(unsigned long flags)
269{
270 local_irq_restore_hw(flags);
271}
272
273static void __ipipe_no_irqtail(void)
274{
275}
276
277int ipipe_get_sysinfo(struct ipipe_sysinfo *info)
278{
279 info->ncpus = num_online_cpus();
280 info->cpufreq = ipipe_cpu_freq();
281 info->archdep.tmirq = IPIPE_TIMER_IRQ;
282 info->archdep.tmfreq = info->cpufreq;
283
284 return 0;
285}
286
287/*
288 * ipipe_trigger_irq() -- Push the interrupt at front of the pipeline
289 * just like if it has been actually received from a hw source. Also
290 * works for virtual interrupts.
291 */
292int ipipe_trigger_irq(unsigned irq)
293{
294 unsigned long flags;
295
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800296#ifdef CONFIG_IPIPE_DEBUG
Yi Li6a01f232009-01-07 23:14:39 +0800297 if (irq >= IPIPE_NR_IRQS ||
298 (ipipe_virtual_irq_p(irq)
299 && !test_bit(irq - IPIPE_VIRQ_BASE, &__ipipe_virtual_irq_map)))
300 return -EINVAL;
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800301#endif
Yi Li6a01f232009-01-07 23:14:39 +0800302
303 local_irq_save_hw(flags);
Yi Li6a01f232009-01-07 23:14:39 +0800304 __ipipe_handle_irq(irq, NULL);
Yi Li6a01f232009-01-07 23:14:39 +0800305 local_irq_restore_hw(flags);
306
307 return 1;
308}
309
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800310asmlinkage void __ipipe_sync_root(void)
Yi Li6a01f232009-01-07 23:14:39 +0800311{
Philippe Gerum51387002009-04-08 07:41:55 +0000312 void (*irq_tail_hook)(void) = (void (*)(void))__ipipe_irq_tail_hook;
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800313 unsigned long flags;
Yi Li6a01f232009-01-07 23:14:39 +0800314
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800315 BUG_ON(irqs_disabled());
Yi Li6a01f232009-01-07 23:14:39 +0800316
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800317 local_irq_save_hw(flags);
318
Philippe Gerum51387002009-04-08 07:41:55 +0000319 if (irq_tail_hook)
320 irq_tail_hook();
321
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800322 clear_thread_flag(TIF_IRQ_SYNC);
323
324 if (ipipe_root_cpudom_var(irqpend_himask) != 0)
325 __ipipe_sync_pipeline(IPIPE_IRQMASK_ANY);
326
327 local_irq_restore_hw(flags);
Yi Li6a01f232009-01-07 23:14:39 +0800328}
329
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800330void ___ipipe_sync_pipeline(unsigned long syncmask)
Yi Li6a01f232009-01-07 23:14:39 +0800331{
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800332 struct ipipe_domain *ipd = ipipe_current_domain;
Yi Li6a01f232009-01-07 23:14:39 +0800333
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800334 if (ipd == ipipe_root_domain) {
335 if (test_bit(IPIPE_SYNCDEFER_FLAG, &ipipe_root_cpudom_var(status)))
336 return;
Yi Li6a01f232009-01-07 23:14:39 +0800337 }
338
Philippe Gerum9bd50df2009-03-04 16:52:38 +0800339 __ipipe_sync_stage(syncmask);
Yi Li6a01f232009-01-07 23:14:39 +0800340}
341
342EXPORT_SYMBOL(show_stack);
343
344#ifdef CONFIG_IPIPE_TRACE_MCOUNT
345void notrace _mcount(void);
346EXPORT_SYMBOL(_mcount);
347#endif /* CONFIG_IPIPE_TRACE_MCOUNT */