irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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/time.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. static void end_sb1250_irq(unsigned int irq);
  44. static void enable_sb1250_irq(unsigned int irq);
  45. static void disable_sb1250_irq(unsigned int irq);
  46. static void ack_sb1250_irq(unsigned int irq);
  47. #ifdef CONFIG_SMP
  48. static void sb1250_set_affinity(unsigned int irq, cpumask_t mask);
  49. #endif
  50. #ifdef CONFIG_SIBYTE_HAS_LDT
  51. extern unsigned long ldt_eoi_space;
  52. #endif
  53. #ifdef CONFIG_KGDB
  54. static int kgdb_irq;
  55. /* Default to UART1 */
  56. int kgdb_port = 1;
  57. #ifdef CONFIG_SERIAL_SB1250_DUART
  58. extern char sb1250_duart_present[];
  59. #endif
  60. #endif
  61. static struct irq_chip sb1250_irq_type = {
  62. .name = "SB1250-IMR",
  63. .ack = ack_sb1250_irq,
  64. .mask = disable_sb1250_irq,
  65. .mask_ack = ack_sb1250_irq,
  66. .unmask = enable_sb1250_irq,
  67. .end = end_sb1250_irq,
  68. #ifdef CONFIG_SMP
  69. .set_affinity = sb1250_set_affinity
  70. #endif
  71. };
  72. /* Store the CPU id (not the logical number) */
  73. int sb1250_irq_owner[SB1250_NR_IRQS];
  74. DEFINE_SPINLOCK(sb1250_imr_lock);
  75. void sb1250_mask_irq(int cpu, int irq)
  76. {
  77. unsigned long flags;
  78. u64 cur_ints;
  79. spin_lock_irqsave(&sb1250_imr_lock, flags);
  80. cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(cpu) +
  81. R_IMR_INTERRUPT_MASK));
  82. cur_ints |= (((u64) 1) << irq);
  83. ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(cpu) +
  84. R_IMR_INTERRUPT_MASK));
  85. spin_unlock_irqrestore(&sb1250_imr_lock, flags);
  86. }
  87. void sb1250_unmask_irq(int cpu, int irq)
  88. {
  89. unsigned long flags;
  90. u64 cur_ints;
  91. spin_lock_irqsave(&sb1250_imr_lock, flags);
  92. cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(cpu) +
  93. R_IMR_INTERRUPT_MASK));
  94. cur_ints &= ~(((u64) 1) << irq);
  95. ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(cpu) +
  96. R_IMR_INTERRUPT_MASK));
  97. spin_unlock_irqrestore(&sb1250_imr_lock, flags);
  98. }
  99. #ifdef CONFIG_SMP
  100. static void sb1250_set_affinity(unsigned int irq, cpumask_t mask)
  101. {
  102. int i = 0, old_cpu, cpu, int_on;
  103. u64 cur_ints;
  104. struct irq_desc *desc = irq_desc + irq;
  105. unsigned long flags;
  106. i = first_cpu(mask);
  107. if (cpus_weight(mask) > 1) {
  108. printk("attempted to set irq affinity for irq %d to multiple CPUs\n", irq);
  109. return;
  110. }
  111. /* Convert logical CPU to physical CPU */
  112. cpu = cpu_logical_map(i);
  113. /* Protect against other affinity changers and IMR manipulation */
  114. spin_lock_irqsave(&desc->lock, flags);
  115. spin_lock(&sb1250_imr_lock);
  116. /* Swizzle each CPU's IMR (but leave the IP selection alone) */
  117. old_cpu = sb1250_irq_owner[irq];
  118. cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(old_cpu) +
  119. R_IMR_INTERRUPT_MASK));
  120. int_on = !(cur_ints & (((u64) 1) << irq));
  121. if (int_on) {
  122. /* If it was on, mask it */
  123. cur_ints |= (((u64) 1) << irq);
  124. ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(old_cpu) +
  125. R_IMR_INTERRUPT_MASK));
  126. }
  127. sb1250_irq_owner[irq] = cpu;
  128. if (int_on) {
  129. /* unmask for the new CPU */
  130. cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(cpu) +
  131. R_IMR_INTERRUPT_MASK));
  132. cur_ints &= ~(((u64) 1) << irq);
  133. ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(cpu) +
  134. R_IMR_INTERRUPT_MASK));
  135. }
  136. spin_unlock(&sb1250_imr_lock);
  137. spin_unlock_irqrestore(&desc->lock, flags);
  138. }
  139. #endif
  140. /*****************************************************************************/
  141. static void disable_sb1250_irq(unsigned int irq)
  142. {
  143. sb1250_mask_irq(sb1250_irq_owner[irq], irq);
  144. }
  145. static void enable_sb1250_irq(unsigned int irq)
  146. {
  147. sb1250_unmask_irq(sb1250_irq_owner[irq], irq);
  148. }
  149. static void ack_sb1250_irq(unsigned int irq)
  150. {
  151. #ifdef CONFIG_SIBYTE_HAS_LDT
  152. u64 pending;
  153. /*
  154. * If the interrupt was an HT interrupt, now is the time to
  155. * clear it. NOTE: we assume the HT bridge was set up to
  156. * deliver the interrupts to all CPUs (which makes affinity
  157. * changing easier for us)
  158. */
  159. pending = __raw_readq(IOADDR(A_IMR_REGISTER(sb1250_irq_owner[irq],
  160. R_IMR_LDT_INTERRUPT)));
  161. pending &= ((u64)1 << (irq));
  162. if (pending) {
  163. int i;
  164. for (i=0; i<NR_CPUS; i++) {
  165. int cpu;
  166. #ifdef CONFIG_SMP
  167. cpu = cpu_logical_map(i);
  168. #else
  169. cpu = i;
  170. #endif
  171. /*
  172. * Clear for all CPUs so an affinity switch
  173. * doesn't find an old status
  174. */
  175. __raw_writeq(pending,
  176. IOADDR(A_IMR_REGISTER(cpu,
  177. R_IMR_LDT_INTERRUPT_CLR)));
  178. }
  179. /*
  180. * Generate EOI. For Pass 1 parts, EOI is a nop. For
  181. * Pass 2, the LDT world may be edge-triggered, but
  182. * this EOI shouldn't hurt. If they are
  183. * level-sensitive, the EOI is required.
  184. */
  185. *(uint32_t *)(ldt_eoi_space+(irq<<16)+(7<<2)) = 0;
  186. }
  187. #endif
  188. sb1250_mask_irq(sb1250_irq_owner[irq], irq);
  189. }
  190. static void end_sb1250_irq(unsigned int irq)
  191. {
  192. if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
  193. sb1250_unmask_irq(sb1250_irq_owner[irq], irq);
  194. }
  195. }
  196. void __init init_sb1250_irqs(void)
  197. {
  198. int i;
  199. for (i = 0; i < SB1250_NR_IRQS; i++) {
  200. set_irq_chip(i, &sb1250_irq_type);
  201. sb1250_irq_owner[i] = 0;
  202. }
  203. }
  204. static irqreturn_t sb1250_dummy_handler(int irq, void *dev_id)
  205. {
  206. return IRQ_NONE;
  207. }
  208. static struct irqaction sb1250_dummy_action = {
  209. .handler = sb1250_dummy_handler,
  210. .flags = 0,
  211. .mask = CPU_MASK_NONE,
  212. .name = "sb1250-private",
  213. .next = NULL,
  214. .dev_id = 0
  215. };
  216. int sb1250_steal_irq(int irq)
  217. {
  218. struct irq_desc *desc = irq_desc + irq;
  219. unsigned long flags;
  220. int retval = 0;
  221. if (irq >= SB1250_NR_IRQS)
  222. return -EINVAL;
  223. spin_lock_irqsave(&desc->lock, flags);
  224. /* Don't allow sharing at all for these */
  225. if (desc->action != NULL)
  226. retval = -EBUSY;
  227. else {
  228. desc->action = &sb1250_dummy_action;
  229. desc->depth = 0;
  230. }
  231. spin_unlock_irqrestore(&desc->lock, flags);
  232. return 0;
  233. }
  234. /*
  235. * arch_init_irq is called early in the boot sequence from init/main.c via
  236. * init_IRQ. It is responsible for setting up the interrupt mapper and
  237. * installing the handler that will be responsible for dispatching interrupts
  238. * to the "right" place.
  239. */
  240. /*
  241. * For now, map all interrupts to IP[2]. We could save
  242. * some cycles by parceling out system interrupts to different
  243. * IP lines, but keep it simple for bringup. We'll also direct
  244. * all interrupts to a single CPU; we should probably route
  245. * PCI and LDT to one cpu and everything else to the other
  246. * to balance the load a bit.
  247. *
  248. * On the second cpu, everything is set to IP5, which is
  249. * ignored, EXCEPT the mailbox interrupt. That one is
  250. * set to IP[2] so it is handled. This is needed so we
  251. * can do cross-cpu function calls, as requred by SMP
  252. */
  253. #define IMR_IP2_VAL K_INT_MAP_I0
  254. #define IMR_IP3_VAL K_INT_MAP_I1
  255. #define IMR_IP4_VAL K_INT_MAP_I2
  256. #define IMR_IP5_VAL K_INT_MAP_I3
  257. #define IMR_IP6_VAL K_INT_MAP_I4
  258. void __init arch_init_irq(void)
  259. {
  260. unsigned int i;
  261. u64 tmp;
  262. unsigned int imask = STATUSF_IP4 | STATUSF_IP3 | STATUSF_IP2 |
  263. STATUSF_IP1 | STATUSF_IP0;
  264. /* Default everything to IP2 */
  265. for (i = 0; i < SB1250_NR_IRQS; i++) { /* was I0 */
  266. __raw_writeq(IMR_IP2_VAL,
  267. IOADDR(A_IMR_REGISTER(0,
  268. R_IMR_INTERRUPT_MAP_BASE) +
  269. (i << 3)));
  270. __raw_writeq(IMR_IP2_VAL,
  271. IOADDR(A_IMR_REGISTER(1,
  272. R_IMR_INTERRUPT_MAP_BASE) +
  273. (i << 3)));
  274. }
  275. init_sb1250_irqs();
  276. /*
  277. * Map the high 16 bits of the mailbox registers to IP[3], for
  278. * inter-cpu messages
  279. */
  280. /* Was I1 */
  281. __raw_writeq(IMR_IP3_VAL,
  282. IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_MAP_BASE) +
  283. (K_INT_MBOX_0 << 3)));
  284. __raw_writeq(IMR_IP3_VAL,
  285. IOADDR(A_IMR_REGISTER(1, R_IMR_INTERRUPT_MAP_BASE) +
  286. (K_INT_MBOX_0 << 3)));
  287. /* Clear the mailboxes. The firmware may leave them dirty */
  288. __raw_writeq(0xffffffffffffffffULL,
  289. IOADDR(A_IMR_REGISTER(0, R_IMR_MAILBOX_CLR_CPU)));
  290. __raw_writeq(0xffffffffffffffffULL,
  291. IOADDR(A_IMR_REGISTER(1, R_IMR_MAILBOX_CLR_CPU)));
  292. /* Mask everything except the mailbox registers for both cpus */
  293. tmp = ~((u64) 0) ^ (((u64) 1) << K_INT_MBOX_0);
  294. __raw_writeq(tmp, IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_MASK)));
  295. __raw_writeq(tmp, IOADDR(A_IMR_REGISTER(1, R_IMR_INTERRUPT_MASK)));
  296. sb1250_steal_irq(K_INT_MBOX_0);
  297. /*
  298. * Note that the timer interrupts are also mapped, but this is
  299. * done in sb1250_time_init(). Also, the profiling driver
  300. * does its own management of IP7.
  301. */
  302. #ifdef CONFIG_KGDB
  303. imask |= STATUSF_IP6;
  304. #endif
  305. /* Enable necessary IPs, disable the rest */
  306. change_c0_status(ST0_IM, imask);
  307. #ifdef CONFIG_KGDB
  308. if (kgdb_flag) {
  309. kgdb_irq = K_INT_UART_0 + kgdb_port;
  310. #ifdef CONFIG_SERIAL_SB1250_DUART
  311. sb1250_duart_present[kgdb_port] = 0;
  312. #endif
  313. /* Setup uart 1 settings, mapper */
  314. __raw_writeq(M_DUART_IMR_BRK,
  315. IOADDR(A_DUART_IMRREG(kgdb_port)));
  316. sb1250_steal_irq(kgdb_irq);
  317. __raw_writeq(IMR_IP6_VAL,
  318. IOADDR(A_IMR_REGISTER(0,
  319. R_IMR_INTERRUPT_MAP_BASE) +
  320. (kgdb_irq << 3)));
  321. sb1250_unmask_irq(0, kgdb_irq);
  322. }
  323. #endif
  324. }
  325. #ifdef CONFIG_KGDB
  326. #include <linux/delay.h>
  327. #define duart_out(reg, val) csr_out32(val, IOADDR(A_DUART_CHANREG(kgdb_port, reg)))
  328. #define duart_in(reg) csr_in32(IOADDR(A_DUART_CHANREG(kgdb_port, reg)))
  329. static void sb1250_kgdb_interrupt(void)
  330. {
  331. /*
  332. * Clear break-change status (allow some time for the remote
  333. * host to stop the break, since we would see another
  334. * interrupt on the end-of-break too)
  335. */
  336. kstat_this_cpu.irqs[kgdb_irq]++;
  337. mdelay(500);
  338. duart_out(R_DUART_CMD, V_DUART_MISC_CMD_RESET_BREAK_INT |
  339. M_DUART_RX_EN | M_DUART_TX_EN);
  340. set_async_breakpoint(&get_irq_regs()->cp0_epc);
  341. }
  342. #endif /* CONFIG_KGDB */
  343. static inline void sb1250_timer_interrupt(void)
  344. {
  345. int cpu = smp_processor_id();
  346. int irq = K_INT_TIMER_0 + cpu;
  347. irq_enter();
  348. kstat_this_cpu.irqs[irq]++;
  349. write_seqlock(&xtime_lock);
  350. /* ACK interrupt */
  351. ____raw_writeq(M_SCD_TIMER_ENABLE | M_SCD_TIMER_MODE_CONTINUOUS,
  352. IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_CFG)));
  353. /*
  354. * call the generic timer interrupt handling
  355. */
  356. do_timer(1);
  357. write_sequnlock(&xtime_lock);
  358. /*
  359. * In UP mode, we call local_timer_interrupt() to do profiling
  360. * and process accouting.
  361. *
  362. * In SMP mode, local_timer_interrupt() is invoked by appropriate
  363. * low-level local timer interrupt handler.
  364. */
  365. local_timer_interrupt(irq);
  366. irq_exit();
  367. }
  368. extern void sb1250_mailbox_interrupt(void);
  369. asmlinkage void plat_irq_dispatch(void)
  370. {
  371. unsigned int pending;
  372. /*
  373. * What a pain. We have to be really careful saving the upper 32 bits
  374. * of any * register across function calls if we don't want them
  375. * trashed--since were running in -o32, the calling routing never saves
  376. * the full 64 bits of a register across a function call. Being the
  377. * interrupt handler, we're guaranteed that interrupts are disabled
  378. * during this code so we don't have to worry about random interrupts
  379. * blasting the high 32 bits.
  380. */
  381. pending = read_c0_cause() & read_c0_status() & ST0_IM;
  382. if (pending & CAUSEF_IP7) /* CPU performance counter interrupt */
  383. do_IRQ(MIPS_CPU_IRQ_BASE + 7);
  384. else if (pending & CAUSEF_IP4)
  385. sb1250_timer_interrupt();
  386. #ifdef CONFIG_SMP
  387. else if (pending & CAUSEF_IP3)
  388. sb1250_mailbox_interrupt();
  389. #endif
  390. #ifdef CONFIG_KGDB
  391. else if (pending & CAUSEF_IP6) /* KGDB (uart 1) */
  392. sb1250_kgdb_interrupt();
  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);
  406. else
  407. spurious_interrupt();
  408. } else
  409. spurious_interrupt();
  410. }