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
  253. mask_irq(int irq)
  254. {
  255. int cpu;
  256. for (cpu = 0; cpu < NR_CPUS; cpu++)
  257. block_irq(irq, cpu);
  258. }
  259. void
  260. unmask_irq(int irq)
  261. {
  262. unblock_irq(irq, irq_cpu(irq));
  263. }
  264. static unsigned int startup_crisv32_irq(unsigned int irq)
  265. {
  266. unmask_irq(irq);
  267. return 0;
  268. }
  269. static void shutdown_crisv32_irq(unsigned int irq)
  270. {
  271. mask_irq(irq);
  272. }
  273. static void enable_crisv32_irq(unsigned int irq)
  274. {
  275. unmask_irq(irq);
  276. }
  277. static void disable_crisv32_irq(unsigned int irq)
  278. {
  279. mask_irq(irq);
  280. }
  281. static void ack_crisv32_irq(unsigned int irq)
  282. {
  283. }
  284. static void end_crisv32_irq(unsigned int irq)
  285. {
  286. }
  287. void set_affinity_crisv32_irq(unsigned int irq, cpumask_t dest)
  288. {
  289. unsigned long flags;
  290. spin_lock_irqsave(&irq_lock, flags);
  291. irq_allocations[irq - FIRST_IRQ].mask = dest;
  292. spin_unlock_irqrestore(&irq_lock, flags);
  293. }
  294. static struct hw_interrupt_type crisv32_irq_type = {
  295. .typename = "CRISv32",
  296. .startup = startup_crisv32_irq,
  297. .shutdown = shutdown_crisv32_irq,
  298. .enable = enable_crisv32_irq,
  299. .disable = disable_crisv32_irq,
  300. .ack = ack_crisv32_irq,
  301. .end = end_crisv32_irq,
  302. .set_affinity = set_affinity_crisv32_irq
  303. };
  304. void
  305. set_exception_vector(int n, irqvectptr addr)
  306. {
  307. etrax_irv->v[n] = (irqvectptr) addr;
  308. }
  309. extern void do_IRQ(int irq, struct pt_regs * regs);
  310. void
  311. crisv32_do_IRQ(int irq, int block, struct pt_regs* regs)
  312. {
  313. /* Interrupts that may not be moved to another CPU and
  314. * are IRQF_DISABLED may skip blocking. This is currently
  315. * only valid for the timer IRQ and the IPI and is used
  316. * for the timer interrupt to avoid watchdog starvation.
  317. */
  318. if (!block) {
  319. do_IRQ(irq, regs);
  320. return;
  321. }
  322. block_irq(irq, smp_processor_id());
  323. do_IRQ(irq, regs);
  324. unblock_irq(irq, irq_cpu(irq));
  325. }
  326. /* If multiple interrupts occur simultaneously we get a multiple
  327. * interrupt from the CPU and software has to sort out which
  328. * interrupts that happened. There are two special cases here:
  329. *
  330. * 1. Timer interrupts may never be blocked because of the
  331. * watchdog (refer to comment in include/asr/arch/irq.h)
  332. * 2. GDB serial port IRQs are unhandled here and will be handled
  333. * as a single IRQ when it strikes again because the GDB
  334. * stubb wants to save the registers in its own fashion.
  335. */
  336. void
  337. crisv32_do_multiple(struct pt_regs* regs)
  338. {
  339. int cpu;
  340. int mask;
  341. int masked[NBR_REGS];
  342. int bit;
  343. int i;
  344. cpu = smp_processor_id();
  345. /* An extra irq_enter here to prevent softIRQs to run after
  346. * each do_IRQ. This will decrease the interrupt latency.
  347. */
  348. irq_enter();
  349. for (i = 0; i < NBR_REGS; i++) {
  350. /* Get which IRQs that happend. */
  351. masked[i] = REG_RD_INT_VECT(intr_vect, irq_regs[cpu],
  352. r_masked_vect, i);
  353. /* Calculate new IRQ mask with these IRQs disabled. */
  354. mask = REG_RD_INT_VECT(intr_vect, irq_regs[cpu], rw_mask, i);
  355. mask &= ~masked[i];
  356. /* Timer IRQ is never masked */
  357. #ifdef TIMER_VECT1
  358. if ((i == 1) && (masked[0] & TIMER_MASK))
  359. mask |= TIMER_MASK;
  360. #else
  361. if ((i == 0) && (masked[0] & TIMER_MASK))
  362. mask |= TIMER_MASK;
  363. #endif
  364. /* Block all the IRQs */
  365. REG_WR_INT_VECT(intr_vect, irq_regs[cpu], rw_mask, i, mask);
  366. /* Check for timer IRQ and handle it special. */
  367. #ifdef TIMER_VECT1
  368. if ((i == 1) && (masked[i] & TIMER_MASK)) {
  369. masked[i] &= ~TIMER_MASK;
  370. do_IRQ(TIMER0_INTR_VECT, regs);
  371. }
  372. #else
  373. if ((i == 0) && (masked[i] & TIMER_MASK)) {
  374. masked[i] &= ~TIMER_MASK;
  375. do_IRQ(TIMER0_INTR_VECT, regs);
  376. }
  377. }
  378. #endif
  379. #ifdef IGNORE_MASK
  380. /* Remove IRQs that can't be handled as multiple. */
  381. masked[0] &= ~IGNORE_MASK;
  382. #endif
  383. /* Handle the rest of the IRQs. */
  384. for (i = 0; i < NBR_REGS; i++) {
  385. for (bit = 0; bit < 32; bit++) {
  386. if (masked[i] & (1 << bit))
  387. do_IRQ(bit + FIRST_IRQ + i*32, regs);
  388. }
  389. }
  390. /* Unblock all the IRQs. */
  391. for (i = 0; i < NBR_REGS; i++) {
  392. mask = REG_RD_INT_VECT(intr_vect, irq_regs[cpu], rw_mask, i);
  393. mask |= masked[i];
  394. REG_WR_INT_VECT(intr_vect, irq_regs[cpu], rw_mask, i, mask);
  395. }
  396. /* This irq_exit() will trigger the soft IRQs. */
  397. irq_exit();
  398. }
  399. /*
  400. * This is called by start_kernel. It fixes the IRQ masks and setup the
  401. * interrupt vector table to point to bad_interrupt pointers.
  402. */
  403. void __init
  404. init_IRQ(void)
  405. {
  406. int i;
  407. int j;
  408. reg_intr_vect_rw_mask vect_mask = {0};
  409. /* Clear all interrupts masks. */
  410. for (i = 0; i < NBR_REGS; i++)
  411. REG_WR_VECT(intr_vect, regi_irq, rw_mask, i, vect_mask);
  412. for (i = 0; i < 256; i++)
  413. etrax_irv->v[i] = weird_irq;
  414. /* Point all IRQ's to bad handlers. */
  415. for (i = FIRST_IRQ, j = 0; j < NR_IRQS; i++, j++) {
  416. irq_desc[j].chip = &crisv32_irq_type;
  417. set_exception_vector(i, interrupt[j]);
  418. }
  419. /* Mark Timer and IPI IRQs as CPU local */
  420. irq_allocations[TIMER0_INTR_VECT - FIRST_IRQ].cpu = CPU_FIXED;
  421. irq_desc[TIMER0_INTR_VECT].status |= IRQ_PER_CPU;
  422. irq_allocations[IPI_INTR_VECT - FIRST_IRQ].cpu = CPU_FIXED;
  423. irq_desc[IPI_INTR_VECT].status |= IRQ_PER_CPU;
  424. set_exception_vector(0x00, nmi_interrupt);
  425. set_exception_vector(0x30, multiple_interrupt);
  426. /* Set up handler for various MMU bus faults. */
  427. set_exception_vector(0x04, i_mmu_refill);
  428. set_exception_vector(0x05, i_mmu_invalid);
  429. set_exception_vector(0x06, i_mmu_access);
  430. set_exception_vector(0x07, i_mmu_execute);
  431. set_exception_vector(0x08, d_mmu_refill);
  432. set_exception_vector(0x09, d_mmu_invalid);
  433. set_exception_vector(0x0a, d_mmu_access);
  434. set_exception_vector(0x0b, d_mmu_write);
  435. #ifdef CONFIG_BUG
  436. /* Break 14 handler, used to implement cheap BUG(). */
  437. set_exception_vector(0x1e, breakh_BUG);
  438. #endif
  439. /* The system-call trap is reached by "break 13". */
  440. set_exception_vector(0x1d, system_call);
  441. /* Exception handlers for debugging, both user-mode and kernel-mode. */
  442. /* Break 8. */
  443. set_exception_vector(0x18, gdb_handle_exception);
  444. /* Hardware single step. */
  445. set_exception_vector(0x3, gdb_handle_exception);
  446. /* Hardware breakpoint. */
  447. set_exception_vector(0xc, gdb_handle_exception);
  448. #ifdef CONFIG_ETRAX_KGDB
  449. kgdb_init();
  450. /* Everything is set up; now trap the kernel. */
  451. breakpoint();
  452. #endif
  453. }