irq.c 12 KB

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