irq.c 12 KB

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