irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Copyright (C) 2000, 2001, 2002, 2003 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/linkage.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/smp.h>
  24. #include <linux/mm.h>
  25. #include <linux/slab.h>
  26. #include <linux/kernel_stat.h>
  27. #include <asm/errno.h>
  28. #include <asm/signal.h>
  29. #include <asm/system.h>
  30. #include <asm/ptrace.h>
  31. #include <asm/io.h>
  32. #include <asm/sibyte/sb1250_regs.h>
  33. #include <asm/sibyte/sb1250_int.h>
  34. #include <asm/sibyte/sb1250_uart.h>
  35. #include <asm/sibyte/sb1250_scd.h>
  36. #include <asm/sibyte/sb1250.h>
  37. /*
  38. * These are the routines that handle all the low level interrupt stuff.
  39. * Actions handled here are: initialization of the interrupt map, requesting of
  40. * interrupt lines by handlers, dispatching if interrupts to handlers, probing
  41. * for interrupt lines
  42. */
  43. #define shutdown_sb1250_irq disable_sb1250_irq
  44. static void end_sb1250_irq(unsigned int irq);
  45. static void enable_sb1250_irq(unsigned int irq);
  46. static void disable_sb1250_irq(unsigned int irq);
  47. static unsigned int startup_sb1250_irq(unsigned int irq);
  48. static void ack_sb1250_irq(unsigned int irq);
  49. #ifdef CONFIG_SMP
  50. static void sb1250_set_affinity(unsigned int irq, cpumask_t mask);
  51. #endif
  52. #ifdef CONFIG_SIBYTE_HAS_LDT
  53. extern unsigned long ldt_eoi_space;
  54. #endif
  55. #ifdef CONFIG_KGDB
  56. static int kgdb_irq;
  57. /* Default to UART1 */
  58. int kgdb_port = 1;
  59. #ifdef CONFIG_SIBYTE_SB1250_DUART
  60. extern char sb1250_duart_present[];
  61. #endif
  62. #endif
  63. static struct irq_chip sb1250_irq_type = {
  64. .typename = "SB1250-IMR",
  65. .startup = startup_sb1250_irq,
  66. .shutdown = shutdown_sb1250_irq,
  67. .enable = enable_sb1250_irq,
  68. .disable = disable_sb1250_irq,
  69. .ack = ack_sb1250_irq,
  70. .end = end_sb1250_irq,
  71. #ifdef CONFIG_SMP
  72. .set_affinity = sb1250_set_affinity
  73. #endif
  74. };
  75. /* Store the CPU id (not the logical number) */
  76. int sb1250_irq_owner[SB1250_NR_IRQS];
  77. DEFINE_SPINLOCK(sb1250_imr_lock);
  78. void sb1250_mask_irq(int cpu, int irq)
  79. {
  80. unsigned long flags;
  81. u64 cur_ints;
  82. spin_lock_irqsave(&sb1250_imr_lock, flags);
  83. cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(cpu) +
  84. R_IMR_INTERRUPT_MASK));
  85. cur_ints |= (((u64) 1) << irq);
  86. ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(cpu) +
  87. R_IMR_INTERRUPT_MASK));
  88. spin_unlock_irqrestore(&sb1250_imr_lock, flags);
  89. }
  90. void sb1250_unmask_irq(int cpu, int irq)
  91. {
  92. unsigned long flags;
  93. u64 cur_ints;
  94. spin_lock_irqsave(&sb1250_imr_lock, flags);
  95. cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(cpu) +
  96. R_IMR_INTERRUPT_MASK));
  97. cur_ints &= ~(((u64) 1) << irq);
  98. ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(cpu) +
  99. R_IMR_INTERRUPT_MASK));
  100. spin_unlock_irqrestore(&sb1250_imr_lock, flags);
  101. }
  102. #ifdef CONFIG_SMP
  103. static void sb1250_set_affinity(unsigned int irq, cpumask_t mask)
  104. {
  105. int i = 0, old_cpu, cpu, int_on;
  106. u64 cur_ints;
  107. struct irq_desc *desc = irq_desc + irq;
  108. unsigned long flags;
  109. i = first_cpu(mask);
  110. if (cpus_weight(mask) > 1) {
  111. printk("attempted to set irq affinity for irq %d to multiple CPUs\n", irq);
  112. return;
  113. }
  114. /* Convert logical CPU to physical CPU */
  115. cpu = cpu_logical_map(i);
  116. /* Protect against other affinity changers and IMR manipulation */
  117. spin_lock_irqsave(&desc->lock, flags);
  118. spin_lock(&sb1250_imr_lock);
  119. /* Swizzle each CPU's IMR (but leave the IP selection alone) */
  120. old_cpu = sb1250_irq_owner[irq];
  121. cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(old_cpu) +
  122. R_IMR_INTERRUPT_MASK));
  123. int_on = !(cur_ints & (((u64) 1) << irq));
  124. if (int_on) {
  125. /* If it was on, mask it */
  126. cur_ints |= (((u64) 1) << irq);
  127. ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(old_cpu) +
  128. R_IMR_INTERRUPT_MASK));
  129. }
  130. sb1250_irq_owner[irq] = cpu;
  131. if (int_on) {
  132. /* unmask for the new CPU */
  133. cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(cpu) +
  134. R_IMR_INTERRUPT_MASK));
  135. cur_ints &= ~(((u64) 1) << irq);
  136. ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(cpu) +
  137. R_IMR_INTERRUPT_MASK));
  138. }
  139. spin_unlock(&sb1250_imr_lock);
  140. spin_unlock_irqrestore(&desc->lock, flags);
  141. }
  142. #endif
  143. /*****************************************************************************/
  144. static unsigned int startup_sb1250_irq(unsigned int irq)
  145. {
  146. sb1250_unmask_irq(sb1250_irq_owner[irq], irq);
  147. return 0; /* never anything pending */
  148. }
  149. static void disable_sb1250_irq(unsigned int irq)
  150. {
  151. sb1250_mask_irq(sb1250_irq_owner[irq], irq);
  152. }
  153. static void enable_sb1250_irq(unsigned int irq)
  154. {
  155. sb1250_unmask_irq(sb1250_irq_owner[irq], irq);
  156. }
  157. static void ack_sb1250_irq(unsigned int irq)
  158. {
  159. #ifdef CONFIG_SIBYTE_HAS_LDT
  160. u64 pending;
  161. /*
  162. * If the interrupt was an HT interrupt, now is the time to
  163. * clear it. NOTE: we assume the HT bridge was set up to
  164. * deliver the interrupts to all CPUs (which makes affinity
  165. * changing easier for us)
  166. */
  167. pending = __raw_readq(IOADDR(A_IMR_REGISTER(sb1250_irq_owner[irq],
  168. R_IMR_LDT_INTERRUPT)));
  169. pending &= ((u64)1 << (irq));
  170. if (pending) {
  171. int i;
  172. for (i=0; i<NR_CPUS; i++) {
  173. int cpu;
  174. #ifdef CONFIG_SMP
  175. cpu = cpu_logical_map(i);
  176. #else
  177. cpu = i;
  178. #endif
  179. /*
  180. * Clear for all CPUs so an affinity switch
  181. * doesn't find an old status
  182. */
  183. __raw_writeq(pending,
  184. IOADDR(A_IMR_REGISTER(cpu,
  185. R_IMR_LDT_INTERRUPT_CLR)));
  186. }
  187. /*
  188. * Generate EOI. For Pass 1 parts, EOI is a nop. For
  189. * Pass 2, the LDT world may be edge-triggered, but
  190. * this EOI shouldn't hurt. If they are
  191. * level-sensitive, the EOI is required.
  192. */
  193. *(uint32_t *)(ldt_eoi_space+(irq<<16)+(7<<2)) = 0;
  194. }
  195. #endif
  196. sb1250_mask_irq(sb1250_irq_owner[irq], irq);
  197. }
  198. static void end_sb1250_irq(unsigned int irq)
  199. {
  200. if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
  201. sb1250_unmask_irq(sb1250_irq_owner[irq], irq);
  202. }
  203. }
  204. void __init init_sb1250_irqs(void)
  205. {
  206. int i;
  207. for (i = 0; i < NR_IRQS; i++) {
  208. irq_desc[i].status = IRQ_DISABLED;
  209. irq_desc[i].action = 0;
  210. irq_desc[i].depth = 1;
  211. if (i < SB1250_NR_IRQS) {
  212. irq_desc[i].chip = &sb1250_irq_type;
  213. sb1250_irq_owner[i] = 0;
  214. } else {
  215. irq_desc[i].chip = &no_irq_chip;
  216. }
  217. }
  218. }
  219. static irqreturn_t sb1250_dummy_handler(int irq, void *dev_id,
  220. struct pt_regs *regs)
  221. {
  222. return IRQ_NONE;
  223. }
  224. static struct irqaction sb1250_dummy_action = {
  225. .handler = sb1250_dummy_handler,
  226. .flags = 0,
  227. .mask = CPU_MASK_NONE,
  228. .name = "sb1250-private",
  229. .next = NULL,
  230. .dev_id = 0
  231. };
  232. int sb1250_steal_irq(int irq)
  233. {
  234. struct irq_desc *desc = irq_desc + irq;
  235. unsigned long flags;
  236. int retval = 0;
  237. if (irq >= SB1250_NR_IRQS)
  238. return -EINVAL;
  239. spin_lock_irqsave(&desc->lock,flags);
  240. /* Don't allow sharing at all for these */
  241. if (desc->action != NULL)
  242. retval = -EBUSY;
  243. else {
  244. desc->action = &sb1250_dummy_action;
  245. desc->depth = 0;
  246. }
  247. spin_unlock_irqrestore(&desc->lock,flags);
  248. return 0;
  249. }
  250. /*
  251. * arch_init_irq is called early in the boot sequence from init/main.c via
  252. * init_IRQ. It is responsible for setting up the interrupt mapper and
  253. * installing the handler that will be responsible for dispatching interrupts
  254. * to the "right" place.
  255. */
  256. /*
  257. * For now, map all interrupts to IP[2]. We could save
  258. * some cycles by parceling out system interrupts to different
  259. * IP lines, but keep it simple for bringup. We'll also direct
  260. * all interrupts to a single CPU; we should probably route
  261. * PCI and LDT to one cpu and everything else to the other
  262. * to balance the load a bit.
  263. *
  264. * On the second cpu, everything is set to IP5, which is
  265. * ignored, EXCEPT the mailbox interrupt. That one is
  266. * set to IP[2] so it is handled. This is needed so we
  267. * can do cross-cpu function calls, as requred by SMP
  268. */
  269. #define IMR_IP2_VAL K_INT_MAP_I0
  270. #define IMR_IP3_VAL K_INT_MAP_I1
  271. #define IMR_IP4_VAL K_INT_MAP_I2
  272. #define IMR_IP5_VAL K_INT_MAP_I3
  273. #define IMR_IP6_VAL K_INT_MAP_I4
  274. void __init arch_init_irq(void)
  275. {
  276. unsigned int i;
  277. u64 tmp;
  278. unsigned int imask = STATUSF_IP4 | STATUSF_IP3 | STATUSF_IP2 |
  279. STATUSF_IP1 | STATUSF_IP0;
  280. /* Default everything to IP2 */
  281. for (i = 0; i < SB1250_NR_IRQS; i++) { /* was I0 */
  282. __raw_writeq(IMR_IP2_VAL,
  283. IOADDR(A_IMR_REGISTER(0,
  284. R_IMR_INTERRUPT_MAP_BASE) +
  285. (i << 3)));
  286. __raw_writeq(IMR_IP2_VAL,
  287. IOADDR(A_IMR_REGISTER(1,
  288. R_IMR_INTERRUPT_MAP_BASE) +
  289. (i << 3)));
  290. }
  291. init_sb1250_irqs();
  292. /*
  293. * Map the high 16 bits of the mailbox registers to IP[3], for
  294. * inter-cpu messages
  295. */
  296. /* Was I1 */
  297. __raw_writeq(IMR_IP3_VAL,
  298. IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_MAP_BASE) +
  299. (K_INT_MBOX_0 << 3)));
  300. __raw_writeq(IMR_IP3_VAL,
  301. IOADDR(A_IMR_REGISTER(1, R_IMR_INTERRUPT_MAP_BASE) +
  302. (K_INT_MBOX_0 << 3)));
  303. /* Clear the mailboxes. The firmware may leave them dirty */
  304. __raw_writeq(0xffffffffffffffffULL,
  305. IOADDR(A_IMR_REGISTER(0, R_IMR_MAILBOX_CLR_CPU)));
  306. __raw_writeq(0xffffffffffffffffULL,
  307. IOADDR(A_IMR_REGISTER(1, R_IMR_MAILBOX_CLR_CPU)));
  308. /* Mask everything except the mailbox registers for both cpus */
  309. tmp = ~((u64) 0) ^ (((u64) 1) << K_INT_MBOX_0);
  310. __raw_writeq(tmp, IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_MASK)));
  311. __raw_writeq(tmp, IOADDR(A_IMR_REGISTER(1, R_IMR_INTERRUPT_MASK)));
  312. sb1250_steal_irq(K_INT_MBOX_0);
  313. /*
  314. * Note that the timer interrupts are also mapped, but this is
  315. * done in sb1250_time_init(). Also, the profiling driver
  316. * does its own management of IP7.
  317. */
  318. #ifdef CONFIG_KGDB
  319. imask |= STATUSF_IP6;
  320. #endif
  321. /* Enable necessary IPs, disable the rest */
  322. change_c0_status(ST0_IM, imask);
  323. #ifdef CONFIG_KGDB
  324. if (kgdb_flag) {
  325. kgdb_irq = K_INT_UART_0 + kgdb_port;
  326. #ifdef CONFIG_SIBYTE_SB1250_DUART
  327. sb1250_duart_present[kgdb_port] = 0;
  328. #endif
  329. /* Setup uart 1 settings, mapper */
  330. __raw_writeq(M_DUART_IMR_BRK,
  331. IOADDR(A_DUART_IMRREG(kgdb_port)));
  332. sb1250_steal_irq(kgdb_irq);
  333. __raw_writeq(IMR_IP6_VAL,
  334. IOADDR(A_IMR_REGISTER(0,
  335. R_IMR_INTERRUPT_MAP_BASE) +
  336. (kgdb_irq << 3)));
  337. sb1250_unmask_irq(0, kgdb_irq);
  338. }
  339. #endif
  340. }
  341. #ifdef CONFIG_KGDB
  342. #include <linux/delay.h>
  343. #define duart_out(reg, val) csr_out32(val, IOADDR(A_DUART_CHANREG(kgdb_port,reg)))
  344. #define duart_in(reg) csr_in32(IOADDR(A_DUART_CHANREG(kgdb_port,reg)))
  345. static void sb1250_kgdb_interrupt(struct pt_regs *regs)
  346. {
  347. /*
  348. * Clear break-change status (allow some time for the remote
  349. * host to stop the break, since we would see another
  350. * interrupt on the end-of-break too)
  351. */
  352. kstat_this_cpu.irqs[kgdb_irq]++;
  353. mdelay(500);
  354. duart_out(R_DUART_CMD, V_DUART_MISC_CMD_RESET_BREAK_INT |
  355. M_DUART_RX_EN | M_DUART_TX_EN);
  356. set_async_breakpoint(&regs->cp0_epc);
  357. }
  358. #endif /* CONFIG_KGDB */
  359. extern void sb1250_timer_interrupt(struct pt_regs *regs);
  360. extern void sb1250_mailbox_interrupt(struct pt_regs *regs);
  361. extern void sb1250_kgdb_interrupt(struct pt_regs *regs);
  362. asmlinkage void plat_irq_dispatch(struct pt_regs *regs)
  363. {
  364. unsigned int pending;
  365. #ifdef CONFIG_SIBYTE_SB1250_PROF
  366. /* Set compare to count to silence count/compare timer interrupts */
  367. write_c0_compare(read_c0_count());
  368. #endif
  369. /*
  370. * What a pain. We have to be really careful saving the upper 32 bits
  371. * of any * register across function calls if we don't want them
  372. * trashed--since were running in -o32, the calling routing never saves
  373. * the full 64 bits of a register across a function call. Being the
  374. * interrupt handler, we're guaranteed that interrupts are disabled
  375. * during this code so we don't have to worry about random interrupts
  376. * blasting the high 32 bits.
  377. */
  378. pending = read_c0_cause();
  379. #ifdef CONFIG_SIBYTE_SB1250_PROF
  380. if (pending & CAUSEF_IP7) /* Cpu performance counter interrupt */
  381. sbprof_cpu_intr(exception_epc(regs));
  382. else
  383. #endif
  384. if (pending & CAUSEF_IP4)
  385. sb1250_timer_interrupt(regs);
  386. #ifdef CONFIG_SMP
  387. else if (pending & CAUSEF_IP3)
  388. sb1250_mailbox_interrupt(regs);
  389. #endif
  390. #ifdef CONFIG_KGDB
  391. else if (pending & CAUSEF_IP6) /* KGDB (uart 1) */
  392. sb1250_kgdb_interrupt(regs);
  393. #endif
  394. else if (pending & CAUSEF_IP2) {
  395. unsigned long long mask;
  396. /*
  397. * Default...we've hit an IP[2] interrupt, which means we've
  398. * got to check the 1250 interrupt registers to figure out what
  399. * to do. Need to detect which CPU we're on, now that
  400. * smp_affinity is supported.
  401. */
  402. mask = __raw_readq(IOADDR(A_IMR_REGISTER(smp_processor_id(),
  403. R_IMR_INTERRUPT_STATUS_BASE)));
  404. if (mask)
  405. do_IRQ(fls64(mask) - 1, regs);
  406. }
  407. }