irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 <hwregs/reg_map.h>
  18. #include <hwregs/reg_rdwr.h>
  19. #include <hwregs/intr_vect.h>
  20. #include <hwregs/intr_vect_defs.h>
  21. #define CPU_FIXED -1
  22. /* IRQ masks (refer to comment for crisv32_do_multiple) */
  23. #if TIMER0_INTR_VECT - FIRST_IRQ < 32
  24. #define TIMER_MASK (1 << (TIMER0_INTR_VECT - FIRST_IRQ))
  25. #undef TIMER_VECT1
  26. #else
  27. #define TIMER_MASK (1 << (TIMER0_INTR_VECT - FIRST_IRQ - 32))
  28. #define TIMER_VECT1
  29. #endif
  30. #ifdef CONFIG_ETRAX_KGDB
  31. #if defined(CONFIG_ETRAX_KGDB_PORT0)
  32. #define IGNOREMASK (1 << (SER0_INTR_VECT - FIRST_IRQ))
  33. #elif defined(CONFIG_ETRAX_KGDB_PORT1)
  34. #define IGNOREMASK (1 << (SER1_INTR_VECT - FIRST_IRQ))
  35. #elif defined(CONFIG_ETRAX_KGB_PORT2)
  36. #define IGNOREMASK (1 << (SER2_INTR_VECT - FIRST_IRQ))
  37. #elif defined(CONFIG_ETRAX_KGDB_PORT3)
  38. #define IGNOREMASK (1 << (SER3_INTR_VECT - FIRST_IRQ))
  39. #endif
  40. #endif
  41. DEFINE_SPINLOCK(irq_lock);
  42. struct cris_irq_allocation
  43. {
  44. int cpu; /* The CPU to which the IRQ is currently allocated. */
  45. cpumask_t mask; /* The CPUs to which the IRQ may be allocated. */
  46. };
  47. struct cris_irq_allocation irq_allocations[NR_REAL_IRQS] =
  48. { [0 ... NR_REAL_IRQS - 1] = {0, CPU_MASK_ALL} };
  49. static unsigned long irq_regs[NR_CPUS] =
  50. {
  51. regi_irq,
  52. #ifdef CONFIG_SMP
  53. regi_irq2,
  54. #endif
  55. };
  56. #if NR_REAL_IRQS > 32
  57. #define NBR_REGS 2
  58. #else
  59. #define NBR_REGS 1
  60. #endif
  61. unsigned long cpu_irq_counters[NR_CPUS];
  62. unsigned long irq_counters[NR_REAL_IRQS];
  63. /* From irq.c. */
  64. extern void weird_irq(void);
  65. /* From entry.S. */
  66. extern void system_call(void);
  67. extern void nmi_interrupt(void);
  68. extern void multiple_interrupt(void);
  69. extern void gdb_handle_exception(void);
  70. extern void i_mmu_refill(void);
  71. extern void i_mmu_invalid(void);
  72. extern void i_mmu_access(void);
  73. extern void i_mmu_execute(void);
  74. extern void d_mmu_refill(void);
  75. extern void d_mmu_invalid(void);
  76. extern void d_mmu_access(void);
  77. extern void d_mmu_write(void);
  78. /* From kgdb.c. */
  79. extern void kgdb_init(void);
  80. extern void breakpoint(void);
  81. /* From traps.c. */
  82. extern void breakh_BUG(void);
  83. /*
  84. * Build the IRQ handler stubs using macros from irq.h.
  85. */
  86. BUILD_IRQ(0x31)
  87. BUILD_IRQ(0x32)
  88. BUILD_IRQ(0x33)
  89. BUILD_IRQ(0x34)
  90. BUILD_IRQ(0x35)
  91. BUILD_IRQ(0x36)
  92. BUILD_IRQ(0x37)
  93. BUILD_IRQ(0x38)
  94. BUILD_IRQ(0x39)
  95. BUILD_IRQ(0x3a)
  96. BUILD_IRQ(0x3b)
  97. BUILD_IRQ(0x3c)
  98. BUILD_IRQ(0x3d)
  99. BUILD_IRQ(0x3e)
  100. BUILD_IRQ(0x3f)
  101. BUILD_IRQ(0x40)
  102. BUILD_IRQ(0x41)
  103. BUILD_IRQ(0x42)
  104. BUILD_IRQ(0x43)
  105. BUILD_IRQ(0x44)
  106. BUILD_IRQ(0x45)
  107. BUILD_IRQ(0x46)
  108. BUILD_IRQ(0x47)
  109. BUILD_IRQ(0x48)
  110. BUILD_IRQ(0x49)
  111. BUILD_IRQ(0x4a)
  112. BUILD_IRQ(0x4b)
  113. BUILD_IRQ(0x4c)
  114. BUILD_IRQ(0x4d)
  115. BUILD_IRQ(0x4e)
  116. BUILD_IRQ(0x4f)
  117. BUILD_IRQ(0x50)
  118. #if MACH_IRQS > 32
  119. BUILD_IRQ(0x51)
  120. BUILD_IRQ(0x52)
  121. BUILD_IRQ(0x53)
  122. BUILD_IRQ(0x54)
  123. BUILD_IRQ(0x55)
  124. BUILD_IRQ(0x56)
  125. BUILD_IRQ(0x57)
  126. BUILD_IRQ(0x58)
  127. BUILD_IRQ(0x59)
  128. BUILD_IRQ(0x5a)
  129. BUILD_IRQ(0x5b)
  130. BUILD_IRQ(0x5c)
  131. BUILD_IRQ(0x5d)
  132. BUILD_IRQ(0x5e)
  133. BUILD_IRQ(0x5f)
  134. BUILD_IRQ(0x60)
  135. BUILD_IRQ(0x61)
  136. BUILD_IRQ(0x62)
  137. BUILD_IRQ(0x63)
  138. BUILD_IRQ(0x64)
  139. BUILD_IRQ(0x65)
  140. BUILD_IRQ(0x66)
  141. BUILD_IRQ(0x67)
  142. BUILD_IRQ(0x68)
  143. BUILD_IRQ(0x69)
  144. BUILD_IRQ(0x6a)
  145. BUILD_IRQ(0x6b)
  146. BUILD_IRQ(0x6c)
  147. BUILD_IRQ(0x6d)
  148. BUILD_IRQ(0x6e)
  149. BUILD_IRQ(0x6f)
  150. BUILD_IRQ(0x70)
  151. #endif
  152. /* Pointers to the low-level handlers. */
  153. static void (*interrupt[MACH_IRQS])(void) = {
  154. IRQ0x31_interrupt, IRQ0x32_interrupt, IRQ0x33_interrupt,
  155. IRQ0x34_interrupt, IRQ0x35_interrupt, IRQ0x36_interrupt,
  156. IRQ0x37_interrupt, IRQ0x38_interrupt, IRQ0x39_interrupt,
  157. IRQ0x3a_interrupt, IRQ0x3b_interrupt, IRQ0x3c_interrupt,
  158. IRQ0x3d_interrupt, IRQ0x3e_interrupt, IRQ0x3f_interrupt,
  159. IRQ0x40_interrupt, IRQ0x41_interrupt, IRQ0x42_interrupt,
  160. IRQ0x43_interrupt, IRQ0x44_interrupt, IRQ0x45_interrupt,
  161. IRQ0x46_interrupt, IRQ0x47_interrupt, IRQ0x48_interrupt,
  162. IRQ0x49_interrupt, IRQ0x4a_interrupt, IRQ0x4b_interrupt,
  163. IRQ0x4c_interrupt, IRQ0x4d_interrupt, IRQ0x4e_interrupt,
  164. IRQ0x4f_interrupt, IRQ0x50_interrupt,
  165. #if MACH_IRQS > 32
  166. IRQ0x51_interrupt, IRQ0x52_interrupt, IRQ0x53_interrupt,
  167. IRQ0x54_interrupt, IRQ0x55_interrupt, IRQ0x56_interrupt,
  168. IRQ0x57_interrupt, IRQ0x58_interrupt, IRQ0x59_interrupt,
  169. IRQ0x5a_interrupt, IRQ0x5b_interrupt, IRQ0x5c_interrupt,
  170. IRQ0x5d_interrupt, IRQ0x5e_interrupt, IRQ0x5f_interrupt,
  171. IRQ0x60_interrupt, IRQ0x61_interrupt, IRQ0x62_interrupt,
  172. IRQ0x63_interrupt, IRQ0x64_interrupt, IRQ0x65_interrupt,
  173. IRQ0x66_interrupt, IRQ0x67_interrupt, IRQ0x68_interrupt,
  174. IRQ0x69_interrupt, IRQ0x6a_interrupt, IRQ0x6b_interrupt,
  175. IRQ0x6c_interrupt, IRQ0x6d_interrupt, IRQ0x6e_interrupt,
  176. IRQ0x6f_interrupt, IRQ0x70_interrupt,
  177. #endif
  178. };
  179. void
  180. block_irq(int irq, int cpu)
  181. {
  182. int intr_mask;
  183. unsigned long flags;
  184. spin_lock_irqsave(&irq_lock, flags);
  185. if (irq - FIRST_IRQ < 32)
  186. intr_mask = REG_RD_INT_VECT(intr_vect, irq_regs[cpu],
  187. rw_mask, 0);
  188. else
  189. intr_mask = REG_RD_INT_VECT(intr_vect, irq_regs[cpu],
  190. rw_mask, 1);
  191. /* Remember; 1 let thru, 0 block. */
  192. if (irq - FIRST_IRQ < 32)
  193. intr_mask &= ~(1 << (irq - FIRST_IRQ));
  194. else
  195. intr_mask &= ~(1 << (irq - FIRST_IRQ - 32));
  196. if (irq - FIRST_IRQ < 32)
  197. REG_WR_INT_VECT(intr_vect, irq_regs[cpu], rw_mask,
  198. 0, intr_mask);
  199. else
  200. REG_WR_INT_VECT(intr_vect, irq_regs[cpu], rw_mask,
  201. 1, intr_mask);
  202. spin_unlock_irqrestore(&irq_lock, flags);
  203. }
  204. void
  205. unblock_irq(int irq, int cpu)
  206. {
  207. int intr_mask;
  208. unsigned long flags;
  209. spin_lock_irqsave(&irq_lock, flags);
  210. if (irq - FIRST_IRQ < 32)
  211. intr_mask = REG_RD_INT_VECT(intr_vect, irq_regs[cpu],
  212. rw_mask, 0);
  213. else
  214. intr_mask = REG_RD_INT_VECT(intr_vect, irq_regs[cpu],
  215. rw_mask, 1);
  216. /* Remember; 1 let thru, 0 block. */
  217. if (irq - FIRST_IRQ < 32)
  218. intr_mask |= (1 << (irq - FIRST_IRQ));
  219. else
  220. intr_mask |= (1 << (irq - FIRST_IRQ - 32));
  221. if (irq - FIRST_IRQ < 32)
  222. REG_WR_INT_VECT(intr_vect, irq_regs[cpu], rw_mask,
  223. 0, intr_mask);
  224. else
  225. REG_WR_INT_VECT(intr_vect, irq_regs[cpu], rw_mask,
  226. 1, intr_mask);
  227. spin_unlock_irqrestore(&irq_lock, flags);
  228. }
  229. /* Find out which CPU the irq should be allocated to. */
  230. static int irq_cpu(int irq)
  231. {
  232. int cpu;
  233. unsigned long flags;
  234. spin_lock_irqsave(&irq_lock, flags);
  235. cpu = irq_allocations[irq - FIRST_IRQ].cpu;
  236. /* Fixed interrupts stay on the local CPU. */
  237. if (cpu == CPU_FIXED)
  238. {
  239. spin_unlock_irqrestore(&irq_lock, flags);
  240. return smp_processor_id();
  241. }
  242. /* Let the interrupt stay if possible */
  243. if (cpu_isset(cpu, irq_allocations[irq - FIRST_IRQ].mask))
  244. goto out;
  245. /* IRQ must be moved to another CPU. */
  246. cpu = first_cpu(irq_allocations[irq - FIRST_IRQ].mask);
  247. irq_allocations[irq - FIRST_IRQ].cpu = cpu;
  248. out:
  249. spin_unlock_irqrestore(&irq_lock, flags);
  250. return cpu;
  251. }
  252. void crisv32_mask_irq(int irq)
  253. {
  254. int cpu;
  255. for (cpu = 0; cpu < NR_CPUS; cpu++)
  256. block_irq(irq, cpu);
  257. }
  258. void crisv32_unmask_irq(int irq)
  259. {
  260. unblock_irq(irq, irq_cpu(irq));
  261. }
  262. static unsigned int startup_crisv32_irq(unsigned int irq)
  263. {
  264. crisv32_unmask_irq(irq);
  265. return 0;
  266. }
  267. static void shutdown_crisv32_irq(unsigned int irq)
  268. {
  269. crisv32_mask_irq(irq);
  270. }
  271. static void enable_crisv32_irq(unsigned int irq)
  272. {
  273. crisv32_unmask_irq(irq);
  274. }
  275. static void disable_crisv32_irq(unsigned int irq)
  276. {
  277. crisv32_mask_irq(irq);
  278. }
  279. static void ack_crisv32_irq(unsigned int irq)
  280. {
  281. }
  282. static void end_crisv32_irq(unsigned int irq)
  283. {
  284. }
  285. int set_affinity_crisv32_irq(unsigned int irq, const struct cpumask *dest)
  286. {
  287. unsigned long flags;
  288. spin_lock_irqsave(&irq_lock, flags);
  289. irq_allocations[irq - FIRST_IRQ].mask = *dest;
  290. spin_unlock_irqrestore(&irq_lock, flags);
  291. return 0;
  292. }
  293. static struct irq_chip crisv32_irq_type = {
  294. .name = "CRISv32",
  295. .startup = startup_crisv32_irq,
  296. .shutdown = shutdown_crisv32_irq,
  297. .enable = enable_crisv32_irq,
  298. .disable = disable_crisv32_irq,
  299. .ack = ack_crisv32_irq,
  300. .end = end_crisv32_irq,
  301. .set_affinity = set_affinity_crisv32_irq
  302. };
  303. void
  304. set_exception_vector(int n, irqvectptr addr)
  305. {
  306. etrax_irv->v[n] = (irqvectptr) addr;
  307. }
  308. extern void do_IRQ(int irq, struct pt_regs * regs);
  309. void
  310. crisv32_do_IRQ(int irq, int block, struct pt_regs* regs)
  311. {
  312. /* Interrupts that may not be moved to another CPU and
  313. * are IRQF_DISABLED may skip blocking. This is currently
  314. * only valid for the timer IRQ and the IPI and is used
  315. * for the timer interrupt to avoid watchdog starvation.
  316. */
  317. if (!block) {
  318. do_IRQ(irq, regs);
  319. return;
  320. }
  321. block_irq(irq, smp_processor_id());
  322. do_IRQ(irq, regs);
  323. unblock_irq(irq, irq_cpu(irq));
  324. }
  325. /* If multiple interrupts occur simultaneously we get a multiple
  326. * interrupt from the CPU and software has to sort out which
  327. * interrupts that happened. There are two special cases here:
  328. *
  329. * 1. Timer interrupts may never be blocked because of the
  330. * watchdog (refer to comment in include/asr/arch/irq.h)
  331. * 2. GDB serial port IRQs are unhandled here and will be handled
  332. * as a single IRQ when it strikes again because the GDB
  333. * stubb wants to save the registers in its own fashion.
  334. */
  335. void
  336. crisv32_do_multiple(struct pt_regs* regs)
  337. {
  338. int cpu;
  339. int mask;
  340. int masked[NBR_REGS];
  341. int bit;
  342. int i;
  343. cpu = smp_processor_id();
  344. /* An extra irq_enter here to prevent softIRQs to run after
  345. * each do_IRQ. This will decrease the interrupt latency.
  346. */
  347. irq_enter();
  348. for (i = 0; i < NBR_REGS; i++) {
  349. /* Get which IRQs that happend. */
  350. masked[i] = REG_RD_INT_VECT(intr_vect, irq_regs[cpu],
  351. r_masked_vect, i);
  352. /* Calculate new IRQ mask with these IRQs disabled. */
  353. mask = REG_RD_INT_VECT(intr_vect, irq_regs[cpu], rw_mask, i);
  354. mask &= ~masked[i];
  355. /* Timer IRQ is never masked */
  356. #ifdef TIMER_VECT1
  357. if ((i == 1) && (masked[0] & TIMER_MASK))
  358. mask |= TIMER_MASK;
  359. #else
  360. if ((i == 0) && (masked[0] & TIMER_MASK))
  361. mask |= TIMER_MASK;
  362. #endif
  363. /* Block all the IRQs */
  364. REG_WR_INT_VECT(intr_vect, irq_regs[cpu], rw_mask, i, mask);
  365. /* Check for timer IRQ and handle it special. */
  366. #ifdef TIMER_VECT1
  367. if ((i == 1) && (masked[i] & TIMER_MASK)) {
  368. masked[i] &= ~TIMER_MASK;
  369. do_IRQ(TIMER0_INTR_VECT, regs);
  370. }
  371. #else
  372. if ((i == 0) && (masked[i] & TIMER_MASK)) {
  373. masked[i] &= ~TIMER_MASK;
  374. do_IRQ(TIMER0_INTR_VECT, regs);
  375. }
  376. #endif
  377. }
  378. #ifdef IGNORE_MASK
  379. /* Remove IRQs that can't be handled as multiple. */
  380. masked[0] &= ~IGNORE_MASK;
  381. #endif
  382. /* Handle the rest of the IRQs. */
  383. for (i = 0; i < NBR_REGS; i++) {
  384. for (bit = 0; bit < 32; bit++) {
  385. if (masked[i] & (1 << bit))
  386. do_IRQ(bit + FIRST_IRQ + i*32, regs);
  387. }
  388. }
  389. /* Unblock all the IRQs. */
  390. for (i = 0; i < NBR_REGS; i++) {
  391. mask = REG_RD_INT_VECT(intr_vect, irq_regs[cpu], rw_mask, i);
  392. mask |= masked[i];
  393. REG_WR_INT_VECT(intr_vect, irq_regs[cpu], rw_mask, i, mask);
  394. }
  395. /* This irq_exit() will trigger the soft IRQs. */
  396. irq_exit();
  397. }
  398. /*
  399. * This is called by start_kernel. It fixes the IRQ masks and setup the
  400. * interrupt vector table to point to bad_interrupt pointers.
  401. */
  402. void __init
  403. init_IRQ(void)
  404. {
  405. int i;
  406. int j;
  407. reg_intr_vect_rw_mask vect_mask = {0};
  408. /* Clear all interrupts masks. */
  409. for (i = 0; i < NBR_REGS; i++)
  410. REG_WR_VECT(intr_vect, regi_irq, rw_mask, i, vect_mask);
  411. for (i = 0; i < 256; i++)
  412. etrax_irv->v[i] = weird_irq;
  413. /* Point all IRQ's to bad handlers. */
  414. for (i = FIRST_IRQ, j = 0; j < NR_IRQS; i++, j++) {
  415. irq_desc[j].chip = &crisv32_irq_type;
  416. set_exception_vector(i, interrupt[j]);
  417. }
  418. /* Mark Timer and IPI IRQs as CPU local */
  419. irq_allocations[TIMER0_INTR_VECT - FIRST_IRQ].cpu = CPU_FIXED;
  420. irq_desc[TIMER0_INTR_VECT].status |= IRQ_PER_CPU;
  421. irq_allocations[IPI_INTR_VECT - FIRST_IRQ].cpu = CPU_FIXED;
  422. irq_desc[IPI_INTR_VECT].status |= IRQ_PER_CPU;
  423. set_exception_vector(0x00, nmi_interrupt);
  424. set_exception_vector(0x30, multiple_interrupt);
  425. /* Set up handler for various MMU bus faults. */
  426. set_exception_vector(0x04, i_mmu_refill);
  427. set_exception_vector(0x05, i_mmu_invalid);
  428. set_exception_vector(0x06, i_mmu_access);
  429. set_exception_vector(0x07, i_mmu_execute);
  430. set_exception_vector(0x08, d_mmu_refill);
  431. set_exception_vector(0x09, d_mmu_invalid);
  432. set_exception_vector(0x0a, d_mmu_access);
  433. set_exception_vector(0x0b, d_mmu_write);
  434. #ifdef CONFIG_BUG
  435. /* Break 14 handler, used to implement cheap BUG(). */
  436. set_exception_vector(0x1e, breakh_BUG);
  437. #endif
  438. /* The system-call trap is reached by "break 13". */
  439. set_exception_vector(0x1d, system_call);
  440. /* Exception handlers for debugging, both user-mode and kernel-mode. */
  441. /* Break 8. */
  442. set_exception_vector(0x18, gdb_handle_exception);
  443. /* Hardware single step. */
  444. set_exception_vector(0x3, gdb_handle_exception);
  445. /* Hardware breakpoint. */
  446. set_exception_vector(0xc, gdb_handle_exception);
  447. #ifdef CONFIG_ETRAX_KGDB
  448. kgdb_init();
  449. /* Everything is set up; now trap the kernel. */
  450. breakpoint();
  451. #endif
  452. }