ipipe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. static int create_irq_threads;
  37. DEFINE_PER_CPU(struct pt_regs, __ipipe_tick_regs);
  38. static DEFINE_PER_CPU(unsigned long, pending_irqthread_mask);
  39. static DEFINE_PER_CPU(int [IVG13 + 1], pending_irq_count);
  40. asmlinkage void asm_do_IRQ(unsigned int irq, struct pt_regs *regs);
  41. static void __ipipe_no_irqtail(void);
  42. unsigned long __ipipe_irq_tail_hook = (unsigned long)&__ipipe_no_irqtail;
  43. EXPORT_SYMBOL(__ipipe_irq_tail_hook);
  44. unsigned long __ipipe_core_clock;
  45. EXPORT_SYMBOL(__ipipe_core_clock);
  46. unsigned long __ipipe_freq_scale;
  47. EXPORT_SYMBOL(__ipipe_freq_scale);
  48. atomic_t __ipipe_irq_lvdepth[IVG15 + 1];
  49. unsigned long __ipipe_irq_lvmask = __all_masked_irq_flags;
  50. EXPORT_SYMBOL(__ipipe_irq_lvmask);
  51. static void __ipipe_ack_irq(unsigned irq, struct irq_desc *desc)
  52. {
  53. desc->ipipe_ack(irq, desc);
  54. }
  55. /*
  56. * __ipipe_enable_pipeline() -- We are running on the boot CPU, hw
  57. * interrupts are off, and secondary CPUs are still lost in space.
  58. */
  59. void __ipipe_enable_pipeline(void)
  60. {
  61. unsigned irq;
  62. __ipipe_core_clock = get_cclk(); /* Fetch this once. */
  63. __ipipe_freq_scale = 1000000000UL / __ipipe_core_clock;
  64. for (irq = 0; irq < NR_IRQS; ++irq)
  65. ipipe_virtualize_irq(ipipe_root_domain,
  66. irq,
  67. (ipipe_irq_handler_t)&asm_do_IRQ,
  68. NULL,
  69. &__ipipe_ack_irq,
  70. IPIPE_HANDLE_MASK | IPIPE_PASS_MASK);
  71. }
  72. /*
  73. * __ipipe_handle_irq() -- IPIPE's generic IRQ handler. An optimistic
  74. * interrupt protection log is maintained here for each domain. Hw
  75. * interrupts are masked on entry.
  76. */
  77. void __ipipe_handle_irq(unsigned irq, struct pt_regs *regs)
  78. {
  79. struct ipipe_domain *this_domain, *next_domain;
  80. struct list_head *head, *pos;
  81. int m_ack, s = -1;
  82. /*
  83. * Software-triggered IRQs do not need any ack. The contents
  84. * of the register frame should only be used when processing
  85. * the timer interrupt, but not for handling any other
  86. * interrupt.
  87. */
  88. m_ack = (regs == NULL || irq == IRQ_SYSTMR || irq == IRQ_CORETMR);
  89. this_domain = ipipe_current_domain;
  90. if (unlikely(test_bit(IPIPE_STICKY_FLAG, &this_domain->irqs[irq].control)))
  91. head = &this_domain->p_link;
  92. else {
  93. head = __ipipe_pipeline.next;
  94. next_domain = list_entry(head, struct ipipe_domain, p_link);
  95. if (likely(test_bit(IPIPE_WIRED_FLAG, &next_domain->irqs[irq].control))) {
  96. if (!m_ack && next_domain->irqs[irq].acknowledge != NULL)
  97. next_domain->irqs[irq].acknowledge(irq, irq_desc + irq);
  98. if (test_bit(IPIPE_ROOTLOCK_FLAG, &ipipe_root_domain->flags))
  99. s = __test_and_set_bit(IPIPE_STALL_FLAG,
  100. &ipipe_root_cpudom_var(status));
  101. __ipipe_dispatch_wired(next_domain, irq);
  102. goto finalize;
  103. return;
  104. }
  105. }
  106. /* Ack the interrupt. */
  107. pos = head;
  108. while (pos != &__ipipe_pipeline) {
  109. next_domain = list_entry(pos, struct ipipe_domain, p_link);
  110. /*
  111. * For each domain handling the incoming IRQ, mark it
  112. * as pending in its log.
  113. */
  114. if (test_bit(IPIPE_HANDLE_FLAG, &next_domain->irqs[irq].control)) {
  115. /*
  116. * Domains that handle this IRQ are polled for
  117. * acknowledging it by decreasing priority
  118. * order. The interrupt must be made pending
  119. * _first_ in the domain's status flags before
  120. * the PIC is unlocked.
  121. */
  122. __ipipe_set_irq_pending(next_domain, irq);
  123. if (!m_ack && next_domain->irqs[irq].acknowledge != NULL) {
  124. next_domain->irqs[irq].acknowledge(irq, irq_desc + irq);
  125. m_ack = 1;
  126. }
  127. }
  128. /*
  129. * If the domain does not want the IRQ to be passed
  130. * down the interrupt pipe, exit the loop now.
  131. */
  132. if (!test_bit(IPIPE_PASS_FLAG, &next_domain->irqs[irq].control))
  133. break;
  134. pos = next_domain->p_link.next;
  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
  142. * additional root stage lock (blackfin-specific). */
  143. if (test_bit(IPIPE_ROOTLOCK_FLAG, &ipipe_root_domain->flags))
  144. s = __test_and_set_bit(IPIPE_STALL_FLAG,
  145. &ipipe_root_cpudom_var(status));
  146. finalize:
  147. __ipipe_walk_pipeline(head);
  148. if (!s)
  149. __clear_bit(IPIPE_STALL_FLAG,
  150. &ipipe_root_cpudom_var(status));
  151. }
  152. int __ipipe_check_root(void)
  153. {
  154. return ipipe_root_domain_p;
  155. }
  156. void __ipipe_enable_irqdesc(struct ipipe_domain *ipd, unsigned irq)
  157. {
  158. struct irq_desc *desc = irq_desc + irq;
  159. int prio = desc->ic_prio;
  160. desc->depth = 0;
  161. if (ipd != &ipipe_root &&
  162. atomic_inc_return(&__ipipe_irq_lvdepth[prio]) == 1)
  163. __set_bit(prio, &__ipipe_irq_lvmask);
  164. }
  165. EXPORT_SYMBOL(__ipipe_enable_irqdesc);
  166. void __ipipe_disable_irqdesc(struct ipipe_domain *ipd, unsigned irq)
  167. {
  168. struct irq_desc *desc = irq_desc + irq;
  169. int prio = desc->ic_prio;
  170. if (ipd != &ipipe_root &&
  171. atomic_dec_and_test(&__ipipe_irq_lvdepth[prio]))
  172. __clear_bit(prio, &__ipipe_irq_lvmask);
  173. }
  174. EXPORT_SYMBOL(__ipipe_disable_irqdesc);
  175. void __ipipe_stall_root_raw(void)
  176. {
  177. /*
  178. * This code is called by the ins{bwl} routines (see
  179. * arch/blackfin/lib/ins.S), which are heavily used by the
  180. * network stack. It masks all interrupts but those handled by
  181. * non-root domains, so that we keep decent network transfer
  182. * rates for Linux without inducing pathological jitter for
  183. * the real-time domain.
  184. */
  185. __asm__ __volatile__ ("sti %0;" : : "d"(__ipipe_irq_lvmask));
  186. __set_bit(IPIPE_STALL_FLAG,
  187. &ipipe_root_cpudom_var(status));
  188. }
  189. void __ipipe_unstall_root_raw(void)
  190. {
  191. __clear_bit(IPIPE_STALL_FLAG,
  192. &ipipe_root_cpudom_var(status));
  193. __asm__ __volatile__ ("sti %0;" : : "d"(bfin_irq_flags));
  194. }
  195. int __ipipe_syscall_root(struct pt_regs *regs)
  196. {
  197. unsigned long flags;
  198. /* We need to run the IRQ tail hook whenever we don't
  199. * propagate a syscall to higher domains, because we know that
  200. * important operations might be pending there (e.g. Xenomai
  201. * deferred rescheduling). */
  202. if (!__ipipe_syscall_watched_p(current, regs->orig_p0)) {
  203. void (*hook)(void) = (void (*)(void))__ipipe_irq_tail_hook;
  204. hook();
  205. return 0;
  206. }
  207. /*
  208. * This routine either returns:
  209. * 0 -- if the syscall is to be passed to Linux;
  210. * 1 -- if the syscall should not be passed to Linux, and no
  211. * tail work should be performed;
  212. * -1 -- if the syscall should not be passed to Linux but the
  213. * tail work has to be performed (for handling signals etc).
  214. */
  215. if (__ipipe_event_monitored_p(IPIPE_EVENT_SYSCALL) &&
  216. __ipipe_dispatch_event(IPIPE_EVENT_SYSCALL, regs) > 0) {
  217. if (ipipe_root_domain_p && !in_atomic()) {
  218. /*
  219. * Sync pending VIRQs before _TIF_NEED_RESCHED
  220. * is tested.
  221. */
  222. local_irq_save_hw(flags);
  223. if ((ipipe_root_cpudom_var(irqpend_himask) & IPIPE_IRQMASK_VIRT) != 0)
  224. __ipipe_sync_pipeline(IPIPE_IRQMASK_VIRT);
  225. local_irq_restore_hw(flags);
  226. return -1;
  227. }
  228. return 1;
  229. }
  230. return 0;
  231. }
  232. unsigned long ipipe_critical_enter(void (*syncfn) (void))
  233. {
  234. unsigned long flags;
  235. local_irq_save_hw(flags);
  236. return flags;
  237. }
  238. void ipipe_critical_exit(unsigned long flags)
  239. {
  240. local_irq_restore_hw(flags);
  241. }
  242. static void __ipipe_no_irqtail(void)
  243. {
  244. }
  245. int ipipe_get_sysinfo(struct ipipe_sysinfo *info)
  246. {
  247. info->ncpus = num_online_cpus();
  248. info->cpufreq = ipipe_cpu_freq();
  249. info->archdep.tmirq = IPIPE_TIMER_IRQ;
  250. info->archdep.tmfreq = info->cpufreq;
  251. return 0;
  252. }
  253. /*
  254. * ipipe_trigger_irq() -- Push the interrupt at front of the pipeline
  255. * just like if it has been actually received from a hw source. Also
  256. * works for virtual interrupts.
  257. */
  258. int ipipe_trigger_irq(unsigned irq)
  259. {
  260. unsigned long flags;
  261. if (irq >= IPIPE_NR_IRQS ||
  262. (ipipe_virtual_irq_p(irq)
  263. && !test_bit(irq - IPIPE_VIRQ_BASE, &__ipipe_virtual_irq_map)))
  264. return -EINVAL;
  265. local_irq_save_hw(flags);
  266. __ipipe_handle_irq(irq, NULL);
  267. local_irq_restore_hw(flags);
  268. return 1;
  269. }
  270. /* Move Linux IRQ to threads. */
  271. static int do_irqd(void *__desc)
  272. {
  273. struct irq_desc *desc = __desc;
  274. unsigned irq = desc - irq_desc;
  275. int thrprio = desc->thr_prio;
  276. int thrmask = 1 << thrprio;
  277. int cpu = smp_processor_id();
  278. cpumask_t cpumask;
  279. sigfillset(&current->blocked);
  280. current->flags |= PF_NOFREEZE;
  281. cpumask = cpumask_of_cpu(cpu);
  282. set_cpus_allowed(current, cpumask);
  283. ipipe_setscheduler_root(current, SCHED_FIFO, 50 + thrprio);
  284. while (!kthread_should_stop()) {
  285. local_irq_disable();
  286. if (!(desc->status & IRQ_SCHEDULED)) {
  287. set_current_state(TASK_INTERRUPTIBLE);
  288. resched:
  289. local_irq_enable();
  290. schedule();
  291. local_irq_disable();
  292. }
  293. __set_current_state(TASK_RUNNING);
  294. /*
  295. * If higher priority interrupt servers are ready to
  296. * run, reschedule immediately. We need this for the
  297. * GPIO demux IRQ handler to unmask the interrupt line
  298. * _last_, after all GPIO IRQs have run.
  299. */
  300. if (per_cpu(pending_irqthread_mask, cpu) & ~(thrmask|(thrmask-1)))
  301. goto resched;
  302. if (--per_cpu(pending_irq_count[thrprio], cpu) == 0)
  303. per_cpu(pending_irqthread_mask, cpu) &= ~thrmask;
  304. desc->status &= ~IRQ_SCHEDULED;
  305. desc->thr_handler(irq, &__raw_get_cpu_var(__ipipe_tick_regs));
  306. local_irq_enable();
  307. }
  308. __set_current_state(TASK_RUNNING);
  309. return 0;
  310. }
  311. static void kick_irqd(unsigned irq, void *cookie)
  312. {
  313. struct irq_desc *desc = irq_desc + irq;
  314. int thrprio = desc->thr_prio;
  315. int thrmask = 1 << thrprio;
  316. int cpu = smp_processor_id();
  317. if (!(desc->status & IRQ_SCHEDULED)) {
  318. desc->status |= IRQ_SCHEDULED;
  319. per_cpu(pending_irqthread_mask, cpu) |= thrmask;
  320. ++per_cpu(pending_irq_count[thrprio], cpu);
  321. wake_up_process(desc->thread);
  322. }
  323. }
  324. int ipipe_start_irq_thread(unsigned irq, struct irq_desc *desc)
  325. {
  326. if (desc->thread || !create_irq_threads)
  327. return 0;
  328. desc->thread = kthread_create(do_irqd, desc, "IRQ %d", irq);
  329. if (desc->thread == NULL) {
  330. printk(KERN_ERR "irqd: could not create IRQ thread %d!\n", irq);
  331. return -ENOMEM;
  332. }
  333. wake_up_process(desc->thread);
  334. desc->thr_handler = ipipe_root_domain->irqs[irq].handler;
  335. ipipe_root_domain->irqs[irq].handler = &kick_irqd;
  336. return 0;
  337. }
  338. void __init ipipe_init_irq_threads(void)
  339. {
  340. unsigned irq;
  341. struct irq_desc *desc;
  342. create_irq_threads = 1;
  343. for (irq = 0; irq < NR_IRQS; irq++) {
  344. desc = irq_desc + irq;
  345. if (desc->action != NULL ||
  346. (desc->status & IRQ_NOREQUEST) != 0)
  347. ipipe_start_irq_thread(irq, desc);
  348. }
  349. }
  350. EXPORT_SYMBOL(show_stack);
  351. #ifdef CONFIG_IPIPE_TRACE_MCOUNT
  352. void notrace _mcount(void);
  353. EXPORT_SYMBOL(_mcount);
  354. #endif /* CONFIG_IPIPE_TRACE_MCOUNT */