ipipe.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* -*- 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. #include <linux/kernel.h>
  24. #include <linux/sched.h>
  25. #include <linux/module.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/percpu.h>
  28. #include <linux/bitops.h>
  29. #include <linux/slab.h>
  30. #include <linux/errno.h>
  31. #include <linux/kthread.h>
  32. #include <asm/unistd.h>
  33. #include <asm/system.h>
  34. #include <asm/atomic.h>
  35. #include <asm/io.h>
  36. DEFINE_PER_CPU(struct pt_regs, __ipipe_tick_regs);
  37. asmlinkage void asm_do_IRQ(unsigned int irq, struct pt_regs *regs);
  38. static void __ipipe_no_irqtail(void);
  39. unsigned long __ipipe_irq_tail_hook = (unsigned long)&__ipipe_no_irqtail;
  40. EXPORT_SYMBOL(__ipipe_irq_tail_hook);
  41. unsigned long __ipipe_core_clock;
  42. EXPORT_SYMBOL(__ipipe_core_clock);
  43. unsigned long __ipipe_freq_scale;
  44. EXPORT_SYMBOL(__ipipe_freq_scale);
  45. atomic_t __ipipe_irq_lvdepth[IVG15 + 1];
  46. unsigned long __ipipe_irq_lvmask = __all_masked_irq_flags;
  47. EXPORT_SYMBOL(__ipipe_irq_lvmask);
  48. static void __ipipe_ack_irq(unsigned irq, struct irq_desc *desc)
  49. {
  50. desc->ipipe_ack(irq, desc);
  51. }
  52. /*
  53. * __ipipe_enable_pipeline() -- We are running on the boot CPU, hw
  54. * interrupts are off, and secondary CPUs are still lost in space.
  55. */
  56. void __ipipe_enable_pipeline(void)
  57. {
  58. unsigned irq;
  59. __ipipe_core_clock = get_cclk(); /* Fetch this once. */
  60. __ipipe_freq_scale = 1000000000UL / __ipipe_core_clock;
  61. for (irq = 0; irq < NR_IRQS; ++irq)
  62. ipipe_virtualize_irq(ipipe_root_domain,
  63. irq,
  64. (ipipe_irq_handler_t)&asm_do_IRQ,
  65. NULL,
  66. &__ipipe_ack_irq,
  67. IPIPE_HANDLE_MASK | IPIPE_PASS_MASK);
  68. }
  69. /*
  70. * __ipipe_handle_irq() -- IPIPE's generic IRQ handler. An optimistic
  71. * interrupt protection log is maintained here for each domain. Hw
  72. * interrupts are masked on entry.
  73. */
  74. void __ipipe_handle_irq(unsigned irq, struct pt_regs *regs)
  75. {
  76. struct ipipe_percpu_domain_data *p = ipipe_root_cpudom_ptr();
  77. struct ipipe_domain *this_domain, *next_domain;
  78. struct list_head *head, *pos;
  79. int m_ack, s = -1;
  80. /*
  81. * Software-triggered IRQs do not need any ack. The contents
  82. * of the register frame should only be used when processing
  83. * the timer interrupt, but not for handling any other
  84. * interrupt.
  85. */
  86. m_ack = (regs == NULL || irq == IRQ_SYSTMR || irq == IRQ_CORETMR);
  87. this_domain = ipipe_current_domain;
  88. if (unlikely(test_bit(IPIPE_STICKY_FLAG, &this_domain->irqs[irq].control)))
  89. head = &this_domain->p_link;
  90. else {
  91. head = __ipipe_pipeline.next;
  92. next_domain = list_entry(head, struct ipipe_domain, p_link);
  93. if (likely(test_bit(IPIPE_WIRED_FLAG, &next_domain->irqs[irq].control))) {
  94. if (!m_ack && next_domain->irqs[irq].acknowledge != NULL)
  95. next_domain->irqs[irq].acknowledge(irq, irq_to_desc(irq));
  96. if (test_bit(IPIPE_SYNCDEFER_FLAG, &p->status))
  97. s = __test_and_set_bit(IPIPE_STALL_FLAG, &p->status);
  98. __ipipe_dispatch_wired(next_domain, irq);
  99. goto out;
  100. }
  101. }
  102. /* Ack the interrupt. */
  103. pos = head;
  104. while (pos != &__ipipe_pipeline) {
  105. next_domain = list_entry(pos, struct ipipe_domain, p_link);
  106. if (test_bit(IPIPE_HANDLE_FLAG, &next_domain->irqs[irq].control)) {
  107. __ipipe_set_irq_pending(next_domain, irq);
  108. if (!m_ack && next_domain->irqs[irq].acknowledge != NULL) {
  109. next_domain->irqs[irq].acknowledge(irq, irq_to_desc(irq));
  110. m_ack = 1;
  111. }
  112. }
  113. if (!test_bit(IPIPE_PASS_FLAG, &next_domain->irqs[irq].control))
  114. break;
  115. pos = next_domain->p_link.next;
  116. }
  117. /*
  118. * Now walk the pipeline, yielding control to the highest
  119. * priority domain that has pending interrupt(s) or
  120. * immediately to the current domain if the interrupt has been
  121. * marked as 'sticky'. This search does not go beyond the
  122. * current domain in the pipeline. We also enforce the
  123. * additional root stage lock (blackfin-specific).
  124. */
  125. if (test_bit(IPIPE_SYNCDEFER_FLAG, &p->status))
  126. s = __test_and_set_bit(IPIPE_STALL_FLAG, &p->status);
  127. /*
  128. * If the interrupt preempted the head domain, then do not
  129. * even try to walk the pipeline, unless an interrupt is
  130. * pending for it.
  131. */
  132. if (test_bit(IPIPE_AHEAD_FLAG, &this_domain->flags) &&
  133. ipipe_head_cpudom_var(irqpend_himask) == 0)
  134. goto out;
  135. __ipipe_walk_pipeline(head);
  136. out:
  137. if (!s)
  138. __clear_bit(IPIPE_STALL_FLAG, &p->status);
  139. }
  140. int __ipipe_check_root(void)
  141. {
  142. return ipipe_root_domain_p;
  143. }
  144. void __ipipe_enable_irqdesc(struct ipipe_domain *ipd, unsigned irq)
  145. {
  146. struct irq_desc *desc = irq_to_desc(irq);
  147. int prio = desc->ic_prio;
  148. desc->depth = 0;
  149. if (ipd != &ipipe_root &&
  150. atomic_inc_return(&__ipipe_irq_lvdepth[prio]) == 1)
  151. __set_bit(prio, &__ipipe_irq_lvmask);
  152. }
  153. EXPORT_SYMBOL(__ipipe_enable_irqdesc);
  154. void __ipipe_disable_irqdesc(struct ipipe_domain *ipd, unsigned irq)
  155. {
  156. struct irq_desc *desc = irq_to_desc(irq);
  157. int prio = desc->ic_prio;
  158. if (ipd != &ipipe_root &&
  159. atomic_dec_and_test(&__ipipe_irq_lvdepth[prio]))
  160. __clear_bit(prio, &__ipipe_irq_lvmask);
  161. }
  162. EXPORT_SYMBOL(__ipipe_disable_irqdesc);
  163. void __ipipe_stall_root_raw(void)
  164. {
  165. /*
  166. * This code is called by the ins{bwl} routines (see
  167. * arch/blackfin/lib/ins.S), which are heavily used by the
  168. * network stack. It masks all interrupts but those handled by
  169. * non-root domains, so that we keep decent network transfer
  170. * rates for Linux without inducing pathological jitter for
  171. * the real-time domain.
  172. */
  173. __asm__ __volatile__ ("sti %0;" : : "d"(__ipipe_irq_lvmask));
  174. __set_bit(IPIPE_STALL_FLAG,
  175. &ipipe_root_cpudom_var(status));
  176. }
  177. void __ipipe_unstall_root_raw(void)
  178. {
  179. __clear_bit(IPIPE_STALL_FLAG,
  180. &ipipe_root_cpudom_var(status));
  181. __asm__ __volatile__ ("sti %0;" : : "d"(bfin_irq_flags));
  182. }
  183. int __ipipe_syscall_root(struct pt_regs *regs)
  184. {
  185. unsigned long flags;
  186. /*
  187. * We need to run the IRQ tail hook whenever we don't
  188. * propagate a syscall to higher domains, because we know that
  189. * important operations might be pending there (e.g. Xenomai
  190. * deferred rescheduling).
  191. */
  192. if (regs->orig_p0 < NR_syscalls) {
  193. void (*hook)(void) = (void (*)(void))__ipipe_irq_tail_hook;
  194. hook();
  195. if ((current->flags & PF_EVNOTIFY) == 0)
  196. return 0;
  197. }
  198. /*
  199. * This routine either returns:
  200. * 0 -- if the syscall is to be passed to Linux;
  201. * 1 -- if the syscall should not be passed to Linux, and no
  202. * tail work should be performed;
  203. * -1 -- if the syscall should not be passed to Linux but the
  204. * tail work has to be performed (for handling signals etc).
  205. */
  206. if (__ipipe_event_monitored_p(IPIPE_EVENT_SYSCALL) &&
  207. __ipipe_dispatch_event(IPIPE_EVENT_SYSCALL, regs) > 0) {
  208. if (ipipe_root_domain_p && !in_atomic()) {
  209. /*
  210. * Sync pending VIRQs before _TIF_NEED_RESCHED
  211. * is tested.
  212. */
  213. local_irq_save_hw(flags);
  214. if ((ipipe_root_cpudom_var(irqpend_himask) & IPIPE_IRQMASK_VIRT) != 0)
  215. __ipipe_sync_pipeline(IPIPE_IRQMASK_VIRT);
  216. local_irq_restore_hw(flags);
  217. return -1;
  218. }
  219. return 1;
  220. }
  221. return 0;
  222. }
  223. unsigned long ipipe_critical_enter(void (*syncfn) (void))
  224. {
  225. unsigned long flags;
  226. local_irq_save_hw(flags);
  227. return flags;
  228. }
  229. void ipipe_critical_exit(unsigned long flags)
  230. {
  231. local_irq_restore_hw(flags);
  232. }
  233. static void __ipipe_no_irqtail(void)
  234. {
  235. }
  236. int ipipe_get_sysinfo(struct ipipe_sysinfo *info)
  237. {
  238. info->ncpus = num_online_cpus();
  239. info->cpufreq = ipipe_cpu_freq();
  240. info->archdep.tmirq = IPIPE_TIMER_IRQ;
  241. info->archdep.tmfreq = info->cpufreq;
  242. return 0;
  243. }
  244. /*
  245. * ipipe_trigger_irq() -- Push the interrupt at front of the pipeline
  246. * just like if it has been actually received from a hw source. Also
  247. * works for virtual interrupts.
  248. */
  249. int ipipe_trigger_irq(unsigned irq)
  250. {
  251. unsigned long flags;
  252. #ifdef CONFIG_IPIPE_DEBUG
  253. if (irq >= IPIPE_NR_IRQS ||
  254. (ipipe_virtual_irq_p(irq)
  255. && !test_bit(irq - IPIPE_VIRQ_BASE, &__ipipe_virtual_irq_map)))
  256. return -EINVAL;
  257. #endif
  258. local_irq_save_hw(flags);
  259. __ipipe_handle_irq(irq, NULL);
  260. local_irq_restore_hw(flags);
  261. return 1;
  262. }
  263. asmlinkage void __ipipe_sync_root(void)
  264. {
  265. unsigned long flags;
  266. BUG_ON(irqs_disabled());
  267. local_irq_save_hw(flags);
  268. clear_thread_flag(TIF_IRQ_SYNC);
  269. if (ipipe_root_cpudom_var(irqpend_himask) != 0)
  270. __ipipe_sync_pipeline(IPIPE_IRQMASK_ANY);
  271. local_irq_restore_hw(flags);
  272. }
  273. void ___ipipe_sync_pipeline(unsigned long syncmask)
  274. {
  275. struct ipipe_domain *ipd = ipipe_current_domain;
  276. if (ipd == ipipe_root_domain) {
  277. if (test_bit(IPIPE_SYNCDEFER_FLAG, &ipipe_root_cpudom_var(status)))
  278. return;
  279. }
  280. __ipipe_sync_stage(syncmask);
  281. }
  282. EXPORT_SYMBOL(show_stack);
  283. #ifdef CONFIG_IPIPE_TRACE_MCOUNT
  284. void notrace _mcount(void);
  285. EXPORT_SYMBOL(_mcount);
  286. #endif /* CONFIG_IPIPE_TRACE_MCOUNT */