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