irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright (C) 2003, Axis Communications AB.
  3. */
  4. #include <asm/irq.h>
  5. #include <linux/irq.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/smp.h>
  8. #include <linux/kernel.h>
  9. #include <linux/errno.h>
  10. #include <linux/init.h>
  11. #include <linux/profile.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/threads.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/kernel_stat.h>
  17. #include <asm/arch/hwregs/reg_map.h>
  18. #include <asm/arch/hwregs/reg_rdwr.h>
  19. #include <asm/arch/hwregs/intr_vect.h>
  20. #include <asm/arch/hwregs/intr_vect_defs.h>
  21. #define CPU_FIXED -1
  22. /* IRQ masks (refer to comment for crisv32_do_multiple) */
  23. #define TIMER_MASK (1 << (TIMER_INTR_VECT - FIRST_IRQ))
  24. #ifdef CONFIG_ETRAX_KGDB
  25. #if defined(CONFIG_ETRAX_KGDB_PORT0)
  26. #define IGNOREMASK (1 << (SER0_INTR_VECT - FIRST_IRQ))
  27. #elif defined(CONFIG_ETRAX_KGDB_PORT1)
  28. #define IGNOREMASK (1 << (SER1_INTR_VECT - FIRST_IRQ))
  29. #elif defined(CONFIG_ETRAX_KGB_PORT2)
  30. #define IGNOREMASK (1 << (SER2_INTR_VECT - FIRST_IRQ))
  31. #elif defined(CONFIG_ETRAX_KGDB_PORT3)
  32. #define IGNOREMASK (1 << (SER3_INTR_VECT - FIRST_IRQ))
  33. #endif
  34. #endif
  35. DEFINE_SPINLOCK(irq_lock);
  36. struct cris_irq_allocation
  37. {
  38. int cpu; /* The CPU to which the IRQ is currently allocated. */
  39. cpumask_t mask; /* The CPUs to which the IRQ may be allocated. */
  40. };
  41. struct cris_irq_allocation irq_allocations[NR_IRQS] =
  42. {[0 ... NR_IRQS - 1] = {0, CPU_MASK_ALL}};
  43. static unsigned long irq_regs[NR_CPUS] =
  44. {
  45. regi_irq,
  46. #ifdef CONFIG_SMP
  47. regi_irq2,
  48. #endif
  49. };
  50. unsigned long cpu_irq_counters[NR_CPUS];
  51. unsigned long irq_counters[NR_REAL_IRQS];
  52. /* From irq.c. */
  53. extern void weird_irq(void);
  54. /* From entry.S. */
  55. extern void system_call(void);
  56. extern void nmi_interrupt(void);
  57. extern void multiple_interrupt(void);
  58. extern void gdb_handle_exception(void);
  59. extern void i_mmu_refill(void);
  60. extern void i_mmu_invalid(void);
  61. extern void i_mmu_access(void);
  62. extern void i_mmu_execute(void);
  63. extern void d_mmu_refill(void);
  64. extern void d_mmu_invalid(void);
  65. extern void d_mmu_access(void);
  66. extern void d_mmu_write(void);
  67. /* From kgdb.c. */
  68. extern void kgdb_init(void);
  69. extern void breakpoint(void);
  70. /*
  71. * Build the IRQ handler stubs using macros from irq.h. First argument is the
  72. * IRQ number, the second argument is the corresponding bit in
  73. * intr_rw_vect_mask found in asm/arch/hwregs/intr_vect_defs.h.
  74. */
  75. BUILD_IRQ(0x31, (1 << 0)) /* memarb */
  76. BUILD_IRQ(0x32, (1 << 1)) /* gen_io */
  77. BUILD_IRQ(0x33, (1 << 2)) /* iop0 */
  78. BUILD_IRQ(0x34, (1 << 3)) /* iop1 */
  79. BUILD_IRQ(0x35, (1 << 4)) /* iop2 */
  80. BUILD_IRQ(0x36, (1 << 5)) /* iop3 */
  81. BUILD_IRQ(0x37, (1 << 6)) /* dma0 */
  82. BUILD_IRQ(0x38, (1 << 7)) /* dma1 */
  83. BUILD_IRQ(0x39, (1 << 8)) /* dma2 */
  84. BUILD_IRQ(0x3a, (1 << 9)) /* dma3 */
  85. BUILD_IRQ(0x3b, (1 << 10)) /* dma4 */
  86. BUILD_IRQ(0x3c, (1 << 11)) /* dma5 */
  87. BUILD_IRQ(0x3d, (1 << 12)) /* dma6 */
  88. BUILD_IRQ(0x3e, (1 << 13)) /* dma7 */
  89. BUILD_IRQ(0x3f, (1 << 14)) /* dma8 */
  90. BUILD_IRQ(0x40, (1 << 15)) /* dma9 */
  91. BUILD_IRQ(0x41, (1 << 16)) /* ata */
  92. BUILD_IRQ(0x42, (1 << 17)) /* sser0 */
  93. BUILD_IRQ(0x43, (1 << 18)) /* sser1 */
  94. BUILD_IRQ(0x44, (1 << 19)) /* ser0 */
  95. BUILD_IRQ(0x45, (1 << 20)) /* ser1 */
  96. BUILD_IRQ(0x46, (1 << 21)) /* ser2 */
  97. BUILD_IRQ(0x47, (1 << 22)) /* ser3 */
  98. BUILD_IRQ(0x48, (1 << 23))
  99. BUILD_IRQ(0x49, (1 << 24)) /* eth0 */
  100. BUILD_IRQ(0x4a, (1 << 25)) /* eth1 */
  101. BUILD_TIMER_IRQ(0x4b, (1 << 26))/* timer */
  102. BUILD_IRQ(0x4c, (1 << 27)) /* bif_arb */
  103. BUILD_IRQ(0x4d, (1 << 28)) /* bif_dma */
  104. BUILD_IRQ(0x4e, (1 << 29)) /* ext */
  105. BUILD_IRQ(0x4f, (1 << 29)) /* ipi */
  106. /* Pointers to the low-level handlers. */
  107. static void (*interrupt[NR_IRQS])(void) = {
  108. IRQ0x31_interrupt, IRQ0x32_interrupt, IRQ0x33_interrupt,
  109. IRQ0x34_interrupt, IRQ0x35_interrupt, IRQ0x36_interrupt,
  110. IRQ0x37_interrupt, IRQ0x38_interrupt, IRQ0x39_interrupt,
  111. IRQ0x3a_interrupt, IRQ0x3b_interrupt, IRQ0x3c_interrupt,
  112. IRQ0x3d_interrupt, IRQ0x3e_interrupt, IRQ0x3f_interrupt,
  113. IRQ0x40_interrupt, IRQ0x41_interrupt, IRQ0x42_interrupt,
  114. IRQ0x43_interrupt, IRQ0x44_interrupt, IRQ0x45_interrupt,
  115. IRQ0x46_interrupt, IRQ0x47_interrupt, IRQ0x48_interrupt,
  116. IRQ0x49_interrupt, IRQ0x4a_interrupt, IRQ0x4b_interrupt,
  117. IRQ0x4c_interrupt, IRQ0x4d_interrupt, IRQ0x4e_interrupt,
  118. IRQ0x4f_interrupt
  119. };
  120. void
  121. block_irq(int irq, int cpu)
  122. {
  123. int intr_mask;
  124. unsigned long flags;
  125. spin_lock_irqsave(&irq_lock, flags);
  126. intr_mask = REG_RD_INT(intr_vect, irq_regs[cpu], rw_mask);
  127. /* Remember; 1 let thru, 0 block. */
  128. intr_mask &= ~(1 << (irq - FIRST_IRQ));
  129. REG_WR_INT(intr_vect, irq_regs[cpu], rw_mask, intr_mask);
  130. spin_unlock_irqrestore(&irq_lock, flags);
  131. }
  132. void
  133. unblock_irq(int irq, int cpu)
  134. {
  135. int intr_mask;
  136. unsigned long flags;
  137. spin_lock_irqsave(&irq_lock, flags);
  138. intr_mask = REG_RD_INT(intr_vect, irq_regs[cpu], rw_mask);
  139. /* Remember; 1 let thru, 0 block. */
  140. intr_mask |= (1 << (irq - FIRST_IRQ));
  141. REG_WR_INT(intr_vect, irq_regs[cpu], rw_mask, intr_mask);
  142. spin_unlock_irqrestore(&irq_lock, flags);
  143. }
  144. /* Find out which CPU the irq should be allocated to. */
  145. static int irq_cpu(int irq)
  146. {
  147. int cpu;
  148. unsigned long flags;
  149. spin_lock_irqsave(&irq_lock, flags);
  150. cpu = irq_allocations[irq - FIRST_IRQ].cpu;
  151. /* Fixed interrupts stay on the local CPU. */
  152. if (cpu == CPU_FIXED)
  153. {
  154. spin_unlock_irqrestore(&irq_lock, flags);
  155. return smp_processor_id();
  156. }
  157. /* Let the interrupt stay if possible */
  158. if (cpu_isset(cpu, irq_allocations[irq - FIRST_IRQ].mask))
  159. goto out;
  160. /* IRQ must be moved to another CPU. */
  161. cpu = first_cpu(irq_allocations[irq - FIRST_IRQ].mask);
  162. irq_allocations[irq - FIRST_IRQ].cpu = cpu;
  163. out:
  164. spin_unlock_irqrestore(&irq_lock, flags);
  165. return cpu;
  166. }
  167. void
  168. mask_irq(int irq)
  169. {
  170. int cpu;
  171. for (cpu = 0; cpu < NR_CPUS; cpu++)
  172. block_irq(irq, cpu);
  173. }
  174. void
  175. unmask_irq(int irq)
  176. {
  177. unblock_irq(irq, irq_cpu(irq));
  178. }
  179. static unsigned int startup_crisv32_irq(unsigned int irq)
  180. {
  181. unmask_irq(irq);
  182. return 0;
  183. }
  184. static void shutdown_crisv32_irq(unsigned int irq)
  185. {
  186. mask_irq(irq);
  187. }
  188. static void enable_crisv32_irq(unsigned int irq)
  189. {
  190. unmask_irq(irq);
  191. }
  192. static void disable_crisv32_irq(unsigned int irq)
  193. {
  194. mask_irq(irq);
  195. }
  196. static void ack_crisv32_irq(unsigned int irq)
  197. {
  198. }
  199. static void end_crisv32_irq(unsigned int irq)
  200. {
  201. }
  202. void set_affinity_crisv32_irq(unsigned int irq, cpumask_t dest)
  203. {
  204. unsigned long flags;
  205. spin_lock_irqsave(&irq_lock, flags);
  206. irq_allocations[irq - FIRST_IRQ].mask = dest;
  207. spin_unlock_irqrestore(&irq_lock, flags);
  208. }
  209. static struct hw_interrupt_type crisv32_irq_type = {
  210. .typename = "CRISv32",
  211. .startup = startup_crisv32_irq,
  212. .shutdown = shutdown_crisv32_irq,
  213. .enable = enable_crisv32_irq,
  214. .disable = disable_crisv32_irq,
  215. .ack = ack_crisv32_irq,
  216. .end = end_crisv32_irq,
  217. .set_affinity = set_affinity_crisv32_irq
  218. };
  219. void
  220. set_exception_vector(int n, irqvectptr addr)
  221. {
  222. etrax_irv->v[n] = (irqvectptr) addr;
  223. }
  224. extern void do_IRQ(int irq, struct pt_regs * regs);
  225. void
  226. crisv32_do_IRQ(int irq, int block, struct pt_regs* regs)
  227. {
  228. /* Interrupts that may not be moved to another CPU and
  229. * are IRQF_DISABLED may skip blocking. This is currently
  230. * only valid for the timer IRQ and the IPI and is used
  231. * for the timer interrupt to avoid watchdog starvation.
  232. */
  233. if (!block) {
  234. do_IRQ(irq, regs);
  235. return;
  236. }
  237. block_irq(irq, smp_processor_id());
  238. do_IRQ(irq, regs);
  239. unblock_irq(irq, irq_cpu(irq));
  240. }
  241. /* If multiple interrupts occur simultaneously we get a multiple
  242. * interrupt from the CPU and software has to sort out which
  243. * interrupts that happened. There are two special cases here:
  244. *
  245. * 1. Timer interrupts may never be blocked because of the
  246. * watchdog (refer to comment in include/asr/arch/irq.h)
  247. * 2. GDB serial port IRQs are unhandled here and will be handled
  248. * as a single IRQ when it strikes again because the GDB
  249. * stubb wants to save the registers in its own fashion.
  250. */
  251. void
  252. crisv32_do_multiple(struct pt_regs* regs)
  253. {
  254. int cpu;
  255. int mask;
  256. int masked;
  257. int bit;
  258. cpu = smp_processor_id();
  259. /* An extra irq_enter here to prevent softIRQs to run after
  260. * each do_IRQ. This will decrease the interrupt latency.
  261. */
  262. irq_enter();
  263. /* Get which IRQs that happend. */
  264. masked = REG_RD_INT(intr_vect, irq_regs[cpu], r_masked_vect);
  265. /* Calculate new IRQ mask with these IRQs disabled. */
  266. mask = REG_RD_INT(intr_vect, irq_regs[cpu], rw_mask);
  267. mask &= ~masked;
  268. /* Timer IRQ is never masked */
  269. if (masked & TIMER_MASK)
  270. mask |= TIMER_MASK;
  271. /* Block all the IRQs */
  272. REG_WR_INT(intr_vect, irq_regs[cpu], rw_mask, mask);
  273. /* Check for timer IRQ and handle it special. */
  274. if (masked & TIMER_MASK) {
  275. masked &= ~TIMER_MASK;
  276. do_IRQ(TIMER_INTR_VECT, regs);
  277. }
  278. #ifdef IGNORE_MASK
  279. /* Remove IRQs that can't be handled as multiple. */
  280. masked &= ~IGNORE_MASK;
  281. #endif
  282. /* Handle the rest of the IRQs. */
  283. for (bit = 0; bit < 32; bit++)
  284. {
  285. if (masked & (1 << bit))
  286. do_IRQ(bit + FIRST_IRQ, regs);
  287. }
  288. /* Unblock all the IRQs. */
  289. mask = REG_RD_INT(intr_vect, irq_regs[cpu], rw_mask);
  290. mask |= masked;
  291. REG_WR_INT(intr_vect, irq_regs[cpu], rw_mask, mask);
  292. /* This irq_exit() will trigger the soft IRQs. */
  293. irq_exit();
  294. }
  295. /*
  296. * This is called by start_kernel. It fixes the IRQ masks and setup the
  297. * interrupt vector table to point to bad_interrupt pointers.
  298. */
  299. void __init
  300. init_IRQ(void)
  301. {
  302. int i;
  303. int j;
  304. reg_intr_vect_rw_mask vect_mask = {0};
  305. /* Clear all interrupts masks. */
  306. REG_WR(intr_vect, regi_irq, rw_mask, vect_mask);
  307. for (i = 0; i < 256; i++)
  308. etrax_irv->v[i] = weird_irq;
  309. /* Point all IRQ's to bad handlers. */
  310. for (i = FIRST_IRQ, j = 0; j < NR_IRQS; i++, j++) {
  311. irq_desc[j].chip = &crisv32_irq_type;
  312. set_exception_vector(i, interrupt[j]);
  313. }
  314. /* Mark Timer and IPI IRQs as CPU local */
  315. irq_allocations[TIMER_INTR_VECT - FIRST_IRQ].cpu = CPU_FIXED;
  316. irq_desc[TIMER_INTR_VECT].status |= IRQ_PER_CPU;
  317. irq_allocations[IPI_INTR_VECT - FIRST_IRQ].cpu = CPU_FIXED;
  318. irq_desc[IPI_INTR_VECT].status |= IRQ_PER_CPU;
  319. set_exception_vector(0x00, nmi_interrupt);
  320. set_exception_vector(0x30, multiple_interrupt);
  321. /* Set up handler for various MMU bus faults. */
  322. set_exception_vector(0x04, i_mmu_refill);
  323. set_exception_vector(0x05, i_mmu_invalid);
  324. set_exception_vector(0x06, i_mmu_access);
  325. set_exception_vector(0x07, i_mmu_execute);
  326. set_exception_vector(0x08, d_mmu_refill);
  327. set_exception_vector(0x09, d_mmu_invalid);
  328. set_exception_vector(0x0a, d_mmu_access);
  329. set_exception_vector(0x0b, d_mmu_write);
  330. /* The system-call trap is reached by "break 13". */
  331. set_exception_vector(0x1d, system_call);
  332. /* Exception handlers for debugging, both user-mode and kernel-mode. */
  333. /* Break 8. */
  334. set_exception_vector(0x18, gdb_handle_exception);
  335. /* Hardware single step. */
  336. set_exception_vector(0x3, gdb_handle_exception);
  337. /* Hardware breakpoint. */
  338. set_exception_vector(0xc, gdb_handle_exception);
  339. #ifdef CONFIG_ETRAX_KGDB
  340. kgdb_init();
  341. /* Everything is set up; now trap the kernel. */
  342. breakpoint();
  343. #endif
  344. }