ipipe.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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/errno.h>
  30. #include <linux/kthread.h>
  31. #include <linux/unistd.h>
  32. #include <linux/io.h>
  33. #include <linux/atomic.h>
  34. #include <asm/irq_handler.h>
  35. DEFINE_PER_CPU(struct pt_regs, __ipipe_tick_regs);
  36. asmlinkage void asm_do_IRQ(unsigned int irq, struct pt_regs *regs);
  37. static void __ipipe_no_irqtail(void);
  38. unsigned long __ipipe_irq_tail_hook = (unsigned long)&__ipipe_no_irqtail;
  39. EXPORT_SYMBOL(__ipipe_irq_tail_hook);
  40. unsigned long __ipipe_core_clock;
  41. EXPORT_SYMBOL(__ipipe_core_clock);
  42. unsigned long __ipipe_freq_scale;
  43. EXPORT_SYMBOL(__ipipe_freq_scale);
  44. atomic_t __ipipe_irq_lvdepth[IVG15 + 1];
  45. unsigned long __ipipe_irq_lvmask = bfin_no_irqs;
  46. EXPORT_SYMBOL(__ipipe_irq_lvmask);
  47. static void __ipipe_ack_irq(unsigned irq, struct irq_desc *desc)
  48. {
  49. desc->ipipe_ack(irq, desc);
  50. }
  51. /*
  52. * __ipipe_enable_pipeline() -- We are running on the boot CPU, hw
  53. * interrupts are off, and secondary CPUs are still lost in space.
  54. */
  55. void __ipipe_enable_pipeline(void)
  56. {
  57. unsigned irq;
  58. __ipipe_core_clock = get_cclk(); /* Fetch this once. */
  59. __ipipe_freq_scale = 1000000000UL / __ipipe_core_clock;
  60. for (irq = 0; irq < NR_IRQS; ++irq)
  61. ipipe_virtualize_irq(ipipe_root_domain,
  62. irq,
  63. (ipipe_irq_handler_t)&asm_do_IRQ,
  64. NULL,
  65. &__ipipe_ack_irq,
  66. IPIPE_HANDLE_MASK | IPIPE_PASS_MASK);
  67. }
  68. /*
  69. * __ipipe_handle_irq() -- IPIPE's generic IRQ handler. An optimistic
  70. * interrupt protection log is maintained here for each domain. Hw
  71. * interrupts are masked on entry.
  72. */
  73. void __ipipe_handle_irq(unsigned irq, struct pt_regs *regs)
  74. {
  75. struct ipipe_percpu_domain_data *p = ipipe_root_cpudom_ptr();
  76. struct ipipe_domain *this_domain, *next_domain;
  77. struct list_head *head, *pos;
  78. struct ipipe_irqdesc *idesc;
  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. idesc = &this_domain->irqs[irq];
  89. if (unlikely(test_bit(IPIPE_STICKY_FLAG, &idesc->control)))
  90. head = &this_domain->p_link;
  91. else {
  92. head = __ipipe_pipeline.next;
  93. next_domain = list_entry(head, struct ipipe_domain, p_link);
  94. idesc = &next_domain->irqs[irq];
  95. if (likely(test_bit(IPIPE_WIRED_FLAG, &idesc->control))) {
  96. if (!m_ack && idesc->acknowledge != NULL)
  97. idesc->acknowledge(irq, irq_to_desc(irq));
  98. if (test_bit(IPIPE_SYNCDEFER_FLAG, &p->status))
  99. s = __test_and_set_bit(IPIPE_STALL_FLAG,
  100. &p->status);
  101. __ipipe_dispatch_wired(next_domain, irq);
  102. goto out;
  103. }
  104. }
  105. /* Ack the interrupt. */
  106. pos = head;
  107. while (pos != &__ipipe_pipeline) {
  108. next_domain = list_entry(pos, struct ipipe_domain, p_link);
  109. idesc = &next_domain->irqs[irq];
  110. if (test_bit(IPIPE_HANDLE_FLAG, &idesc->control)) {
  111. __ipipe_set_irq_pending(next_domain, irq);
  112. if (!m_ack && idesc->acknowledge != NULL) {
  113. idesc->acknowledge(irq, irq_to_desc(irq));
  114. m_ack = 1;
  115. }
  116. }
  117. if (!test_bit(IPIPE_PASS_FLAG, &idesc->control))
  118. break;
  119. pos = next_domain->p_link.next;
  120. }
  121. /*
  122. * Now walk the pipeline, yielding control to the highest
  123. * priority domain that has pending interrupt(s) or
  124. * immediately to the current domain if the interrupt has been
  125. * marked as 'sticky'. This search does not go beyond the
  126. * current domain in the pipeline. We also enforce the
  127. * additional root stage lock (blackfin-specific).
  128. */
  129. if (test_bit(IPIPE_SYNCDEFER_FLAG, &p->status))
  130. s = __test_and_set_bit(IPIPE_STALL_FLAG, &p->status);
  131. /*
  132. * If the interrupt preempted the head domain, then do not
  133. * even try to walk the pipeline, unless an interrupt is
  134. * pending for it.
  135. */
  136. if (test_bit(IPIPE_AHEAD_FLAG, &this_domain->flags) &&
  137. !__ipipe_ipending_p(ipipe_head_cpudom_ptr()))
  138. goto out;
  139. __ipipe_walk_pipeline(head);
  140. out:
  141. if (!s)
  142. __clear_bit(IPIPE_STALL_FLAG, &p->status);
  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 = __ipipe_get_irq_priority(irq);
  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. int prio = __ipipe_get_irq_priority(irq);
  157. if (ipd != &ipipe_root &&
  158. atomic_dec_and_test(&__ipipe_irq_lvdepth[prio]))
  159. __clear_bit(prio, &__ipipe_irq_lvmask);
  160. }
  161. EXPORT_SYMBOL(__ipipe_disable_irqdesc);
  162. asmlinkage int __ipipe_syscall_root(struct pt_regs *regs)
  163. {
  164. struct ipipe_percpu_domain_data *p;
  165. void (*hook)(void);
  166. int ret;
  167. WARN_ON_ONCE(irqs_disabled_hw());
  168. /*
  169. * We need to run the IRQ tail hook each time we intercept a
  170. * syscall, because we know that important operations might be
  171. * pending there (e.g. Xenomai deferred rescheduling).
  172. */
  173. hook = (__typeof__(hook))__ipipe_irq_tail_hook;
  174. hook();
  175. /*
  176. * This routine either returns:
  177. * 0 -- if the syscall is to be passed to Linux;
  178. * >0 -- if the syscall should not be passed to Linux, and no
  179. * tail work should be performed;
  180. * <0 -- if the syscall should not be passed to Linux but the
  181. * tail work has to be performed (for handling signals etc).
  182. */
  183. if (!__ipipe_syscall_watched_p(current, regs->orig_p0) ||
  184. !__ipipe_event_monitored_p(IPIPE_EVENT_SYSCALL))
  185. return 0;
  186. ret = __ipipe_dispatch_event(IPIPE_EVENT_SYSCALL, regs);
  187. hard_local_irq_disable();
  188. /*
  189. * This is the end of the syscall path, so we may
  190. * safely assume a valid Linux task stack here.
  191. */
  192. if (current->ipipe_flags & PF_EVTRET) {
  193. current->ipipe_flags &= ~PF_EVTRET;
  194. __ipipe_dispatch_event(IPIPE_EVENT_RETURN, regs);
  195. }
  196. if (!__ipipe_root_domain_p)
  197. ret = -1;
  198. else {
  199. p = ipipe_root_cpudom_ptr();
  200. if (__ipipe_ipending_p(p))
  201. __ipipe_sync_pipeline();
  202. }
  203. hard_local_irq_enable();
  204. return -ret;
  205. }
  206. static void __ipipe_no_irqtail(void)
  207. {
  208. }
  209. int ipipe_get_sysinfo(struct ipipe_sysinfo *info)
  210. {
  211. info->sys_nr_cpus = num_online_cpus();
  212. info->sys_cpu_freq = ipipe_cpu_freq();
  213. info->sys_hrtimer_irq = IPIPE_TIMER_IRQ;
  214. info->sys_hrtimer_freq = __ipipe_core_clock;
  215. info->sys_hrclock_freq = __ipipe_core_clock;
  216. return 0;
  217. }
  218. /*
  219. * ipipe_trigger_irq() -- Push the interrupt at front of the pipeline
  220. * just like if it has been actually received from a hw source. Also
  221. * works for virtual interrupts.
  222. */
  223. int ipipe_trigger_irq(unsigned irq)
  224. {
  225. unsigned long flags;
  226. #ifdef CONFIG_IPIPE_DEBUG
  227. if (irq >= IPIPE_NR_IRQS ||
  228. (ipipe_virtual_irq_p(irq)
  229. && !test_bit(irq - IPIPE_VIRQ_BASE, &__ipipe_virtual_irq_map)))
  230. return -EINVAL;
  231. #endif
  232. flags = hard_local_irq_save();
  233. __ipipe_handle_irq(irq, NULL);
  234. hard_local_irq_restore(flags);
  235. return 1;
  236. }
  237. asmlinkage void __ipipe_sync_root(void)
  238. {
  239. void (*irq_tail_hook)(void) = (void (*)(void))__ipipe_irq_tail_hook;
  240. struct ipipe_percpu_domain_data *p;
  241. unsigned long flags;
  242. BUG_ON(irqs_disabled());
  243. flags = hard_local_irq_save();
  244. if (irq_tail_hook)
  245. irq_tail_hook();
  246. clear_thread_flag(TIF_IRQ_SYNC);
  247. p = ipipe_root_cpudom_ptr();
  248. if (__ipipe_ipending_p(p))
  249. __ipipe_sync_pipeline();
  250. hard_local_irq_restore(flags);
  251. }
  252. void ___ipipe_sync_pipeline(void)
  253. {
  254. if (__ipipe_root_domain_p &&
  255. test_bit(IPIPE_SYNCDEFER_FLAG, &ipipe_root_cpudom_var(status)))
  256. return;
  257. __ipipe_sync_stage();
  258. }
  259. void __ipipe_disable_root_irqs_hw(void)
  260. {
  261. /*
  262. * This code is called by the ins{bwl} routines (see
  263. * arch/blackfin/lib/ins.S), which are heavily used by the
  264. * network stack. It masks all interrupts but those handled by
  265. * non-root domains, so that we keep decent network transfer
  266. * rates for Linux without inducing pathological jitter for
  267. * the real-time domain.
  268. */
  269. bfin_sti(__ipipe_irq_lvmask);
  270. __set_bit(IPIPE_STALL_FLAG, &ipipe_root_cpudom_var(status));
  271. }
  272. void __ipipe_enable_root_irqs_hw(void)
  273. {
  274. __clear_bit(IPIPE_STALL_FLAG, &ipipe_root_cpudom_var(status));
  275. bfin_sti(bfin_irq_flags);
  276. }
  277. /*
  278. * We could use standard atomic bitops in the following root status
  279. * manipulation routines, but let's prepare for SMP support in the
  280. * same move, preventing CPU migration as required.
  281. */
  282. void __ipipe_stall_root(void)
  283. {
  284. unsigned long *p, flags;
  285. flags = hard_local_irq_save();
  286. p = &__ipipe_root_status;
  287. __set_bit(IPIPE_STALL_FLAG, p);
  288. hard_local_irq_restore(flags);
  289. }
  290. EXPORT_SYMBOL(__ipipe_stall_root);
  291. unsigned long __ipipe_test_and_stall_root(void)
  292. {
  293. unsigned long *p, flags;
  294. int x;
  295. flags = hard_local_irq_save();
  296. p = &__ipipe_root_status;
  297. x = __test_and_set_bit(IPIPE_STALL_FLAG, p);
  298. hard_local_irq_restore(flags);
  299. return x;
  300. }
  301. EXPORT_SYMBOL(__ipipe_test_and_stall_root);
  302. unsigned long __ipipe_test_root(void)
  303. {
  304. const unsigned long *p;
  305. unsigned long flags;
  306. int x;
  307. flags = hard_local_irq_save_smp();
  308. p = &__ipipe_root_status;
  309. x = test_bit(IPIPE_STALL_FLAG, p);
  310. hard_local_irq_restore_smp(flags);
  311. return x;
  312. }
  313. EXPORT_SYMBOL(__ipipe_test_root);
  314. void __ipipe_lock_root(void)
  315. {
  316. unsigned long *p, flags;
  317. flags = hard_local_irq_save();
  318. p = &__ipipe_root_status;
  319. __set_bit(IPIPE_SYNCDEFER_FLAG, p);
  320. hard_local_irq_restore(flags);
  321. }
  322. EXPORT_SYMBOL(__ipipe_lock_root);
  323. void __ipipe_unlock_root(void)
  324. {
  325. unsigned long *p, flags;
  326. flags = hard_local_irq_save();
  327. p = &__ipipe_root_status;
  328. __clear_bit(IPIPE_SYNCDEFER_FLAG, p);
  329. hard_local_irq_restore(flags);
  330. }
  331. EXPORT_SYMBOL(__ipipe_unlock_root);