irq.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 int sb1250_set_affinity(unsigned int irq, const struct cpumask *mask);
  49. #endif
  50. #ifdef CONFIG_SIBYTE_HAS_LDT
  51. extern unsigned long ldt_eoi_space;
  52. #endif
  53. static struct irq_chip sb1250_irq_type = {
  54. .name = "SB1250-IMR",
  55. .ack = ack_sb1250_irq,
  56. .mask = disable_sb1250_irq,
  57. .mask_ack = ack_sb1250_irq,
  58. .unmask = enable_sb1250_irq,
  59. .end = end_sb1250_irq,
  60. #ifdef CONFIG_SMP
  61. .set_affinity = sb1250_set_affinity
  62. #endif
  63. };
  64. /* Store the CPU id (not the logical number) */
  65. int sb1250_irq_owner[SB1250_NR_IRQS];
  66. DEFINE_SPINLOCK(sb1250_imr_lock);
  67. void sb1250_mask_irq(int cpu, int irq)
  68. {
  69. unsigned long flags;
  70. u64 cur_ints;
  71. spin_lock_irqsave(&sb1250_imr_lock, flags);
  72. cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(cpu) +
  73. R_IMR_INTERRUPT_MASK));
  74. cur_ints |= (((u64) 1) << irq);
  75. ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(cpu) +
  76. R_IMR_INTERRUPT_MASK));
  77. spin_unlock_irqrestore(&sb1250_imr_lock, flags);
  78. }
  79. void sb1250_unmask_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. #ifdef CONFIG_SMP
  92. static int sb1250_set_affinity(unsigned int irq, const struct cpumask *mask)
  93. {
  94. int i = 0, old_cpu, cpu, int_on;
  95. u64 cur_ints;
  96. unsigned long flags;
  97. i = cpumask_first(mask);
  98. if (cpumask_weight(mask) > 1) {
  99. printk("attempted to set irq affinity for irq %d to multiple CPUs\n", irq);
  100. return -1;
  101. }
  102. /* Convert logical CPU to physical CPU */
  103. cpu = cpu_logical_map(i);
  104. /* Protect against other affinity changers and IMR manipulation */
  105. spin_lock_irqsave(&sb1250_imr_lock, flags);
  106. /* Swizzle each CPU's IMR (but leave the IP selection alone) */
  107. old_cpu = sb1250_irq_owner[irq];
  108. cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(old_cpu) +
  109. R_IMR_INTERRUPT_MASK));
  110. int_on = !(cur_ints & (((u64) 1) << irq));
  111. if (int_on) {
  112. /* If it was on, mask it */
  113. cur_ints |= (((u64) 1) << irq);
  114. ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(old_cpu) +
  115. R_IMR_INTERRUPT_MASK));
  116. }
  117. sb1250_irq_owner[irq] = cpu;
  118. if (int_on) {
  119. /* unmask for the new CPU */
  120. cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(cpu) +
  121. R_IMR_INTERRUPT_MASK));
  122. cur_ints &= ~(((u64) 1) << irq);
  123. ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(cpu) +
  124. R_IMR_INTERRUPT_MASK));
  125. }
  126. spin_unlock_irqrestore(&sb1250_imr_lock, flags);
  127. return 0;
  128. }
  129. #endif
  130. /*****************************************************************************/
  131. static void disable_sb1250_irq(unsigned int irq)
  132. {
  133. sb1250_mask_irq(sb1250_irq_owner[irq], irq);
  134. }
  135. static void enable_sb1250_irq(unsigned int irq)
  136. {
  137. sb1250_unmask_irq(sb1250_irq_owner[irq], irq);
  138. }
  139. static void ack_sb1250_irq(unsigned int irq)
  140. {
  141. #ifdef CONFIG_SIBYTE_HAS_LDT
  142. u64 pending;
  143. /*
  144. * If the interrupt was an HT interrupt, now is the time to
  145. * clear it. NOTE: we assume the HT bridge was set up to
  146. * deliver the interrupts to all CPUs (which makes affinity
  147. * changing easier for us)
  148. */
  149. pending = __raw_readq(IOADDR(A_IMR_REGISTER(sb1250_irq_owner[irq],
  150. R_IMR_LDT_INTERRUPT)));
  151. pending &= ((u64)1 << (irq));
  152. if (pending) {
  153. int i;
  154. for (i=0; i<NR_CPUS; i++) {
  155. int cpu;
  156. #ifdef CONFIG_SMP
  157. cpu = cpu_logical_map(i);
  158. #else
  159. cpu = i;
  160. #endif
  161. /*
  162. * Clear for all CPUs so an affinity switch
  163. * doesn't find an old status
  164. */
  165. __raw_writeq(pending,
  166. IOADDR(A_IMR_REGISTER(cpu,
  167. R_IMR_LDT_INTERRUPT_CLR)));
  168. }
  169. /*
  170. * Generate EOI. For Pass 1 parts, EOI is a nop. For
  171. * Pass 2, the LDT world may be edge-triggered, but
  172. * this EOI shouldn't hurt. If they are
  173. * level-sensitive, the EOI is required.
  174. */
  175. *(uint32_t *)(ldt_eoi_space+(irq<<16)+(7<<2)) = 0;
  176. }
  177. #endif
  178. sb1250_mask_irq(sb1250_irq_owner[irq], irq);
  179. }
  180. static void end_sb1250_irq(unsigned int irq)
  181. {
  182. if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
  183. sb1250_unmask_irq(sb1250_irq_owner[irq], irq);
  184. }
  185. }
  186. void __init init_sb1250_irqs(void)
  187. {
  188. int i;
  189. for (i = 0; i < SB1250_NR_IRQS; i++) {
  190. set_irq_chip_and_handler(i, &sb1250_irq_type, handle_level_irq);
  191. sb1250_irq_owner[i] = 0;
  192. }
  193. }
  194. /*
  195. * arch_init_irq is called early in the boot sequence from init/main.c via
  196. * init_IRQ. It is responsible for setting up the interrupt mapper and
  197. * installing the handler that will be responsible for dispatching interrupts
  198. * to the "right" place.
  199. */
  200. /*
  201. * For now, map all interrupts to IP[2]. We could save
  202. * some cycles by parceling out system interrupts to different
  203. * IP lines, but keep it simple for bringup. We'll also direct
  204. * all interrupts to a single CPU; we should probably route
  205. * PCI and LDT to one cpu and everything else to the other
  206. * to balance the load a bit.
  207. *
  208. * On the second cpu, everything is set to IP5, which is
  209. * ignored, EXCEPT the mailbox interrupt. That one is
  210. * set to IP[2] so it is handled. This is needed so we
  211. * can do cross-cpu function calls, as requred by SMP
  212. */
  213. #define IMR_IP2_VAL K_INT_MAP_I0
  214. #define IMR_IP3_VAL K_INT_MAP_I1
  215. #define IMR_IP4_VAL K_INT_MAP_I2
  216. #define IMR_IP5_VAL K_INT_MAP_I3
  217. #define IMR_IP6_VAL K_INT_MAP_I4
  218. void __init arch_init_irq(void)
  219. {
  220. unsigned int i;
  221. u64 tmp;
  222. unsigned int imask = STATUSF_IP4 | STATUSF_IP3 | STATUSF_IP2 |
  223. STATUSF_IP1 | STATUSF_IP0;
  224. /* Default everything to IP2 */
  225. for (i = 0; i < SB1250_NR_IRQS; i++) { /* was I0 */
  226. __raw_writeq(IMR_IP2_VAL,
  227. IOADDR(A_IMR_REGISTER(0,
  228. R_IMR_INTERRUPT_MAP_BASE) +
  229. (i << 3)));
  230. __raw_writeq(IMR_IP2_VAL,
  231. IOADDR(A_IMR_REGISTER(1,
  232. R_IMR_INTERRUPT_MAP_BASE) +
  233. (i << 3)));
  234. }
  235. init_sb1250_irqs();
  236. /*
  237. * Map the high 16 bits of the mailbox registers to IP[3], for
  238. * inter-cpu messages
  239. */
  240. /* Was I1 */
  241. __raw_writeq(IMR_IP3_VAL,
  242. IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_MAP_BASE) +
  243. (K_INT_MBOX_0 << 3)));
  244. __raw_writeq(IMR_IP3_VAL,
  245. IOADDR(A_IMR_REGISTER(1, R_IMR_INTERRUPT_MAP_BASE) +
  246. (K_INT_MBOX_0 << 3)));
  247. /* Clear the mailboxes. The firmware may leave them dirty */
  248. __raw_writeq(0xffffffffffffffffULL,
  249. IOADDR(A_IMR_REGISTER(0, R_IMR_MAILBOX_CLR_CPU)));
  250. __raw_writeq(0xffffffffffffffffULL,
  251. IOADDR(A_IMR_REGISTER(1, R_IMR_MAILBOX_CLR_CPU)));
  252. /* Mask everything except the mailbox registers for both cpus */
  253. tmp = ~((u64) 0) ^ (((u64) 1) << K_INT_MBOX_0);
  254. __raw_writeq(tmp, IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_MASK)));
  255. __raw_writeq(tmp, IOADDR(A_IMR_REGISTER(1, R_IMR_INTERRUPT_MASK)));
  256. /*
  257. * Note that the timer interrupts are also mapped, but this is
  258. * done in sb1250_time_init(). Also, the profiling driver
  259. * does its own management of IP7.
  260. */
  261. /* Enable necessary IPs, disable the rest */
  262. change_c0_status(ST0_IM, imask);
  263. }
  264. extern void sb1250_mailbox_interrupt(void);
  265. static inline void dispatch_ip2(void)
  266. {
  267. unsigned int cpu = smp_processor_id();
  268. unsigned long long mask;
  269. /*
  270. * Default...we've hit an IP[2] interrupt, which means we've got to
  271. * check the 1250 interrupt registers to figure out what to do. Need
  272. * to detect which CPU we're on, now that smp_affinity is supported.
  273. */
  274. mask = __raw_readq(IOADDR(A_IMR_REGISTER(cpu,
  275. R_IMR_INTERRUPT_STATUS_BASE)));
  276. if (mask)
  277. do_IRQ(fls64(mask) - 1);
  278. }
  279. asmlinkage void plat_irq_dispatch(void)
  280. {
  281. unsigned int cpu = smp_processor_id();
  282. unsigned int pending;
  283. /*
  284. * What a pain. We have to be really careful saving the upper 32 bits
  285. * of any * register across function calls if we don't want them
  286. * trashed--since were running in -o32, the calling routing never saves
  287. * the full 64 bits of a register across a function call. Being the
  288. * interrupt handler, we're guaranteed that interrupts are disabled
  289. * during this code so we don't have to worry about random interrupts
  290. * blasting the high 32 bits.
  291. */
  292. pending = read_c0_cause() & read_c0_status() & ST0_IM;
  293. if (pending & CAUSEF_IP7) /* CPU performance counter interrupt */
  294. do_IRQ(MIPS_CPU_IRQ_BASE + 7);
  295. else if (pending & CAUSEF_IP4)
  296. do_IRQ(K_INT_TIMER_0 + cpu); /* sb1250_timer_interrupt() */
  297. #ifdef CONFIG_SMP
  298. else if (pending & CAUSEF_IP3)
  299. sb1250_mailbox_interrupt();
  300. #endif
  301. else if (pending & CAUSEF_IP2)
  302. dispatch_ip2();
  303. else
  304. spurious_interrupt();
  305. }