irq.c 13 KB

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