irq.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2000, 2001, 2002, 2003 Broadcom Corporation
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/init.h>
  7. #include <linux/linkage.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/smp.h>
  11. #include <linux/mm.h>
  12. #include <linux/kernel_stat.h>
  13. #include <asm/errno.h>
  14. #include <asm/signal.h>
  15. #include <asm/time.h>
  16. #include <asm/io.h>
  17. #include <asm/sibyte/sb1250_regs.h>
  18. #include <asm/sibyte/sb1250_int.h>
  19. #include <asm/sibyte/sb1250_uart.h>
  20. #include <asm/sibyte/sb1250_scd.h>
  21. #include <asm/sibyte/sb1250.h>
  22. /*
  23. * These are the routines that handle all the low level interrupt stuff.
  24. * Actions handled here are: initialization of the interrupt map, requesting of
  25. * interrupt lines by handlers, dispatching if interrupts to handlers, probing
  26. * for interrupt lines
  27. */
  28. #ifdef CONFIG_SIBYTE_HAS_LDT
  29. extern unsigned long ldt_eoi_space;
  30. #endif
  31. /* Store the CPU id (not the logical number) */
  32. int sb1250_irq_owner[SB1250_NR_IRQS];
  33. static DEFINE_RAW_SPINLOCK(sb1250_imr_lock);
  34. void sb1250_mask_irq(int cpu, int irq)
  35. {
  36. unsigned long flags;
  37. u64 cur_ints;
  38. raw_spin_lock_irqsave(&sb1250_imr_lock, flags);
  39. cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(cpu) +
  40. R_IMR_INTERRUPT_MASK));
  41. cur_ints |= (((u64) 1) << irq);
  42. ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(cpu) +
  43. R_IMR_INTERRUPT_MASK));
  44. raw_spin_unlock_irqrestore(&sb1250_imr_lock, flags);
  45. }
  46. void sb1250_unmask_irq(int cpu, int irq)
  47. {
  48. unsigned long flags;
  49. u64 cur_ints;
  50. raw_spin_lock_irqsave(&sb1250_imr_lock, flags);
  51. cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(cpu) +
  52. R_IMR_INTERRUPT_MASK));
  53. cur_ints &= ~(((u64) 1) << irq);
  54. ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(cpu) +
  55. R_IMR_INTERRUPT_MASK));
  56. raw_spin_unlock_irqrestore(&sb1250_imr_lock, flags);
  57. }
  58. #ifdef CONFIG_SMP
  59. static int sb1250_set_affinity(struct irq_data *d, const struct cpumask *mask,
  60. bool force)
  61. {
  62. int i = 0, old_cpu, cpu, int_on;
  63. unsigned int irq = d->irq;
  64. u64 cur_ints;
  65. unsigned long flags;
  66. i = cpumask_first_and(mask, cpu_online_mask);
  67. /* Convert logical CPU to physical CPU */
  68. cpu = cpu_logical_map(i);
  69. /* Protect against other affinity changers and IMR manipulation */
  70. raw_spin_lock_irqsave(&sb1250_imr_lock, flags);
  71. /* Swizzle each CPU's IMR (but leave the IP selection alone) */
  72. old_cpu = sb1250_irq_owner[irq];
  73. cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(old_cpu) +
  74. R_IMR_INTERRUPT_MASK));
  75. int_on = !(cur_ints & (((u64) 1) << irq));
  76. if (int_on) {
  77. /* If it was on, mask it */
  78. cur_ints |= (((u64) 1) << irq);
  79. ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(old_cpu) +
  80. R_IMR_INTERRUPT_MASK));
  81. }
  82. sb1250_irq_owner[irq] = cpu;
  83. if (int_on) {
  84. /* unmask for the new CPU */
  85. cur_ints = ____raw_readq(IOADDR(A_IMR_MAPPER(cpu) +
  86. R_IMR_INTERRUPT_MASK));
  87. cur_ints &= ~(((u64) 1) << irq);
  88. ____raw_writeq(cur_ints, IOADDR(A_IMR_MAPPER(cpu) +
  89. R_IMR_INTERRUPT_MASK));
  90. }
  91. raw_spin_unlock_irqrestore(&sb1250_imr_lock, flags);
  92. return 0;
  93. }
  94. #endif
  95. static void disable_sb1250_irq(struct irq_data *d)
  96. {
  97. unsigned int irq = d->irq;
  98. sb1250_mask_irq(sb1250_irq_owner[irq], irq);
  99. }
  100. static void enable_sb1250_irq(struct irq_data *d)
  101. {
  102. unsigned int irq = d->irq;
  103. sb1250_unmask_irq(sb1250_irq_owner[irq], irq);
  104. }
  105. static void ack_sb1250_irq(struct irq_data *d)
  106. {
  107. unsigned int irq = d->irq;
  108. #ifdef CONFIG_SIBYTE_HAS_LDT
  109. u64 pending;
  110. /*
  111. * If the interrupt was an HT interrupt, now is the time to
  112. * clear it. NOTE: we assume the HT bridge was set up to
  113. * deliver the interrupts to all CPUs (which makes affinity
  114. * changing easier for us)
  115. */
  116. pending = __raw_readq(IOADDR(A_IMR_REGISTER(sb1250_irq_owner[irq],
  117. R_IMR_LDT_INTERRUPT)));
  118. pending &= ((u64)1 << (irq));
  119. if (pending) {
  120. int i;
  121. for (i=0; i<NR_CPUS; i++) {
  122. int cpu;
  123. #ifdef CONFIG_SMP
  124. cpu = cpu_logical_map(i);
  125. #else
  126. cpu = i;
  127. #endif
  128. /*
  129. * Clear for all CPUs so an affinity switch
  130. * doesn't find an old status
  131. */
  132. __raw_writeq(pending,
  133. IOADDR(A_IMR_REGISTER(cpu,
  134. R_IMR_LDT_INTERRUPT_CLR)));
  135. }
  136. /*
  137. * Generate EOI. For Pass 1 parts, EOI is a nop. For
  138. * Pass 2, the LDT world may be edge-triggered, but
  139. * this EOI shouldn't hurt. If they are
  140. * level-sensitive, the EOI is required.
  141. */
  142. *(uint32_t *)(ldt_eoi_space+(irq<<16)+(7<<2)) = 0;
  143. }
  144. #endif
  145. sb1250_mask_irq(sb1250_irq_owner[irq], irq);
  146. }
  147. static struct irq_chip sb1250_irq_type = {
  148. .name = "SB1250-IMR",
  149. .irq_mask_ack = ack_sb1250_irq,
  150. .irq_unmask = enable_sb1250_irq,
  151. .irq_mask = disable_sb1250_irq,
  152. #ifdef CONFIG_SMP
  153. .irq_set_affinity = sb1250_set_affinity
  154. #endif
  155. };
  156. void __init init_sb1250_irqs(void)
  157. {
  158. int i;
  159. for (i = 0; i < SB1250_NR_IRQS; i++) {
  160. irq_set_chip_and_handler(i, &sb1250_irq_type,
  161. handle_level_irq);
  162. sb1250_irq_owner[i] = 0;
  163. }
  164. }
  165. /*
  166. * arch_init_irq is called early in the boot sequence from init/main.c via
  167. * init_IRQ. It is responsible for setting up the interrupt mapper and
  168. * installing the handler that will be responsible for dispatching interrupts
  169. * to the "right" place.
  170. */
  171. /*
  172. * For now, map all interrupts to IP[2]. We could save
  173. * some cycles by parceling out system interrupts to different
  174. * IP lines, but keep it simple for bringup. We'll also direct
  175. * all interrupts to a single CPU; we should probably route
  176. * PCI and LDT to one cpu and everything else to the other
  177. * to balance the load a bit.
  178. *
  179. * On the second cpu, everything is set to IP5, which is
  180. * ignored, EXCEPT the mailbox interrupt. That one is
  181. * set to IP[2] so it is handled. This is needed so we
  182. * can do cross-cpu function calls, as required by SMP
  183. */
  184. #define IMR_IP2_VAL K_INT_MAP_I0
  185. #define IMR_IP3_VAL K_INT_MAP_I1
  186. #define IMR_IP4_VAL K_INT_MAP_I2
  187. #define IMR_IP5_VAL K_INT_MAP_I3
  188. #define IMR_IP6_VAL K_INT_MAP_I4
  189. void __init arch_init_irq(void)
  190. {
  191. unsigned int i;
  192. u64 tmp;
  193. unsigned int imask = STATUSF_IP4 | STATUSF_IP3 | STATUSF_IP2 |
  194. STATUSF_IP1 | STATUSF_IP0;
  195. /* Default everything to IP2 */
  196. for (i = 0; i < SB1250_NR_IRQS; i++) { /* was I0 */
  197. __raw_writeq(IMR_IP2_VAL,
  198. IOADDR(A_IMR_REGISTER(0,
  199. R_IMR_INTERRUPT_MAP_BASE) +
  200. (i << 3)));
  201. __raw_writeq(IMR_IP2_VAL,
  202. IOADDR(A_IMR_REGISTER(1,
  203. R_IMR_INTERRUPT_MAP_BASE) +
  204. (i << 3)));
  205. }
  206. init_sb1250_irqs();
  207. /*
  208. * Map the high 16 bits of the mailbox registers to IP[3], for
  209. * inter-cpu messages
  210. */
  211. /* Was I1 */
  212. __raw_writeq(IMR_IP3_VAL,
  213. IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_MAP_BASE) +
  214. (K_INT_MBOX_0 << 3)));
  215. __raw_writeq(IMR_IP3_VAL,
  216. IOADDR(A_IMR_REGISTER(1, R_IMR_INTERRUPT_MAP_BASE) +
  217. (K_INT_MBOX_0 << 3)));
  218. /* Clear the mailboxes. The firmware may leave them dirty */
  219. __raw_writeq(0xffffffffffffffffULL,
  220. IOADDR(A_IMR_REGISTER(0, R_IMR_MAILBOX_CLR_CPU)));
  221. __raw_writeq(0xffffffffffffffffULL,
  222. IOADDR(A_IMR_REGISTER(1, R_IMR_MAILBOX_CLR_CPU)));
  223. /* Mask everything except the mailbox registers for both cpus */
  224. tmp = ~((u64) 0) ^ (((u64) 1) << K_INT_MBOX_0);
  225. __raw_writeq(tmp, IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_MASK)));
  226. __raw_writeq(tmp, IOADDR(A_IMR_REGISTER(1, R_IMR_INTERRUPT_MASK)));
  227. /* Enable necessary IPs, disable the rest */
  228. change_c0_status(ST0_IM, imask);
  229. }
  230. extern void sb1250_mailbox_interrupt(void);
  231. static inline void dispatch_ip2(void)
  232. {
  233. unsigned int cpu = smp_processor_id();
  234. unsigned long long mask;
  235. /*
  236. * Default...we've hit an IP[2] interrupt, which means we've got to
  237. * check the 1250 interrupt registers to figure out what to do. Need
  238. * to detect which CPU we're on, now that smp_affinity is supported.
  239. */
  240. mask = __raw_readq(IOADDR(A_IMR_REGISTER(cpu,
  241. R_IMR_INTERRUPT_STATUS_BASE)));
  242. if (mask)
  243. do_IRQ(fls64(mask) - 1);
  244. }
  245. asmlinkage void plat_irq_dispatch(void)
  246. {
  247. unsigned int cpu = smp_processor_id();
  248. unsigned int pending;
  249. /*
  250. * What a pain. We have to be really careful saving the upper 32 bits
  251. * of any * register across function calls if we don't want them
  252. * trashed--since were running in -o32, the calling routing never saves
  253. * the full 64 bits of a register across a function call. Being the
  254. * interrupt handler, we're guaranteed that interrupts are disabled
  255. * during this code so we don't have to worry about random interrupts
  256. * blasting the high 32 bits.
  257. */
  258. pending = read_c0_cause() & read_c0_status() & ST0_IM;
  259. if (pending & CAUSEF_IP7) /* CPU performance counter interrupt */
  260. do_IRQ(MIPS_CPU_IRQ_BASE + 7);
  261. else if (pending & CAUSEF_IP4)
  262. do_IRQ(K_INT_TIMER_0 + cpu); /* sb1250_timer_interrupt() */
  263. #ifdef CONFIG_SMP
  264. else if (pending & CAUSEF_IP3)
  265. sb1250_mailbox_interrupt();
  266. #endif
  267. else if (pending & CAUSEF_IP2)
  268. dispatch_ip2();
  269. else
  270. spurious_interrupt();
  271. }