irq.c 11 KB

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