irq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2000,2001,2002,2003,2004 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/smp.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/mm.h>
  12. #include <linux/kernel_stat.h>
  13. #include <asm/errno.h>
  14. #include <asm/irq_regs.h>
  15. #include <asm/signal.h>
  16. #include <asm/io.h>
  17. #include <asm/sibyte/bcm1480_regs.h>
  18. #include <asm/sibyte/bcm1480_int.h>
  19. #include <asm/sibyte/bcm1480_scd.h>
  20. #include <asm/sibyte/sb1250_uart.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_PCI
  29. extern unsigned long ht_eoi_space;
  30. #endif
  31. /* Store the CPU id (not the logical number) */
  32. int bcm1480_irq_owner[BCM1480_NR_IRQS];
  33. static DEFINE_RAW_SPINLOCK(bcm1480_imr_lock);
  34. void bcm1480_mask_irq(int cpu, int irq)
  35. {
  36. unsigned long flags, hl_spacing;
  37. u64 cur_ints;
  38. raw_spin_lock_irqsave(&bcm1480_imr_lock, flags);
  39. hl_spacing = 0;
  40. if ((irq >= BCM1480_NR_IRQS_HALF) && (irq <= BCM1480_NR_IRQS)) {
  41. hl_spacing = BCM1480_IMR_HL_SPACING;
  42. irq -= BCM1480_NR_IRQS_HALF;
  43. }
  44. cur_ints = ____raw_readq(IOADDR(A_BCM1480_IMR_MAPPER(cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + hl_spacing));
  45. cur_ints |= (((u64) 1) << irq);
  46. ____raw_writeq(cur_ints, IOADDR(A_BCM1480_IMR_MAPPER(cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + hl_spacing));
  47. raw_spin_unlock_irqrestore(&bcm1480_imr_lock, flags);
  48. }
  49. void bcm1480_unmask_irq(int cpu, int irq)
  50. {
  51. unsigned long flags, hl_spacing;
  52. u64 cur_ints;
  53. raw_spin_lock_irqsave(&bcm1480_imr_lock, flags);
  54. hl_spacing = 0;
  55. if ((irq >= BCM1480_NR_IRQS_HALF) && (irq <= BCM1480_NR_IRQS)) {
  56. hl_spacing = BCM1480_IMR_HL_SPACING;
  57. irq -= BCM1480_NR_IRQS_HALF;
  58. }
  59. cur_ints = ____raw_readq(IOADDR(A_BCM1480_IMR_MAPPER(cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + hl_spacing));
  60. cur_ints &= ~(((u64) 1) << irq);
  61. ____raw_writeq(cur_ints, IOADDR(A_BCM1480_IMR_MAPPER(cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + hl_spacing));
  62. raw_spin_unlock_irqrestore(&bcm1480_imr_lock, flags);
  63. }
  64. #ifdef CONFIG_SMP
  65. static int bcm1480_set_affinity(struct irq_data *d, const struct cpumask *mask,
  66. bool force)
  67. {
  68. unsigned int irq_dirty, irq = d->irq;
  69. int i = 0, old_cpu, cpu, int_on, k;
  70. u64 cur_ints;
  71. unsigned long flags;
  72. i = cpumask_first_and(mask, cpu_online_mask);
  73. /* Convert logical CPU to physical CPU */
  74. cpu = cpu_logical_map(i);
  75. /* Protect against other affinity changers and IMR manipulation */
  76. raw_spin_lock_irqsave(&bcm1480_imr_lock, flags);
  77. /* Swizzle each CPU's IMR (but leave the IP selection alone) */
  78. old_cpu = bcm1480_irq_owner[irq];
  79. irq_dirty = irq;
  80. if ((irq_dirty >= BCM1480_NR_IRQS_HALF) && (irq_dirty <= BCM1480_NR_IRQS)) {
  81. irq_dirty -= BCM1480_NR_IRQS_HALF;
  82. }
  83. for (k=0; k<2; k++) { /* Loop through high and low interrupt mask register */
  84. cur_ints = ____raw_readq(IOADDR(A_BCM1480_IMR_MAPPER(old_cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + (k*BCM1480_IMR_HL_SPACING)));
  85. int_on = !(cur_ints & (((u64) 1) << irq_dirty));
  86. if (int_on) {
  87. /* If it was on, mask it */
  88. cur_ints |= (((u64) 1) << irq_dirty);
  89. ____raw_writeq(cur_ints, IOADDR(A_BCM1480_IMR_MAPPER(old_cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + (k*BCM1480_IMR_HL_SPACING)));
  90. }
  91. bcm1480_irq_owner[irq] = cpu;
  92. if (int_on) {
  93. /* unmask for the new CPU */
  94. cur_ints = ____raw_readq(IOADDR(A_BCM1480_IMR_MAPPER(cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + (k*BCM1480_IMR_HL_SPACING)));
  95. cur_ints &= ~(((u64) 1) << irq_dirty);
  96. ____raw_writeq(cur_ints, IOADDR(A_BCM1480_IMR_MAPPER(cpu) + R_BCM1480_IMR_INTERRUPT_MASK_H + (k*BCM1480_IMR_HL_SPACING)));
  97. }
  98. }
  99. raw_spin_unlock_irqrestore(&bcm1480_imr_lock, flags);
  100. return 0;
  101. }
  102. #endif
  103. /*****************************************************************************/
  104. static void disable_bcm1480_irq(struct irq_data *d)
  105. {
  106. unsigned int irq = d->irq;
  107. bcm1480_mask_irq(bcm1480_irq_owner[irq], irq);
  108. }
  109. static void enable_bcm1480_irq(struct irq_data *d)
  110. {
  111. unsigned int irq = d->irq;
  112. bcm1480_unmask_irq(bcm1480_irq_owner[irq], irq);
  113. }
  114. static void ack_bcm1480_irq(struct irq_data *d)
  115. {
  116. unsigned int irq_dirty, irq = d->irq;
  117. u64 pending;
  118. int k;
  119. /*
  120. * If the interrupt was an HT interrupt, now is the time to
  121. * clear it. NOTE: we assume the HT bridge was set up to
  122. * deliver the interrupts to all CPUs (which makes affinity
  123. * changing easier for us)
  124. */
  125. irq_dirty = irq;
  126. if ((irq_dirty >= BCM1480_NR_IRQS_HALF) && (irq_dirty <= BCM1480_NR_IRQS)) {
  127. irq_dirty -= BCM1480_NR_IRQS_HALF;
  128. }
  129. for (k=0; k<2; k++) { /* Loop through high and low LDT interrupts */
  130. pending = __raw_readq(IOADDR(A_BCM1480_IMR_REGISTER(bcm1480_irq_owner[irq],
  131. R_BCM1480_IMR_LDT_INTERRUPT_H + (k*BCM1480_IMR_HL_SPACING))));
  132. pending &= ((u64)1 << (irq_dirty));
  133. if (pending) {
  134. #ifdef CONFIG_SMP
  135. int i;
  136. for (i=0; i<NR_CPUS; i++) {
  137. /*
  138. * Clear for all CPUs so an affinity switch
  139. * doesn't find an old status
  140. */
  141. __raw_writeq(pending, IOADDR(A_BCM1480_IMR_REGISTER(cpu_logical_map(i),
  142. R_BCM1480_IMR_LDT_INTERRUPT_CLR_H + (k*BCM1480_IMR_HL_SPACING))));
  143. }
  144. #else
  145. __raw_writeq(pending, IOADDR(A_BCM1480_IMR_REGISTER(0, R_BCM1480_IMR_LDT_INTERRUPT_CLR_H + (k*BCM1480_IMR_HL_SPACING))));
  146. #endif
  147. /*
  148. * Generate EOI. For Pass 1 parts, EOI is a nop. For
  149. * Pass 2, the LDT world may be edge-triggered, but
  150. * this EOI shouldn't hurt. If they are
  151. * level-sensitive, the EOI is required.
  152. */
  153. #ifdef CONFIG_PCI
  154. if (ht_eoi_space)
  155. *(uint32_t *)(ht_eoi_space+(irq<<16)+(7<<2)) = 0;
  156. #endif
  157. }
  158. }
  159. bcm1480_mask_irq(bcm1480_irq_owner[irq], irq);
  160. }
  161. static struct irq_chip bcm1480_irq_type = {
  162. .name = "BCM1480-IMR",
  163. .irq_mask_ack = ack_bcm1480_irq,
  164. .irq_mask = disable_bcm1480_irq,
  165. .irq_unmask = enable_bcm1480_irq,
  166. #ifdef CONFIG_SMP
  167. .irq_set_affinity = bcm1480_set_affinity
  168. #endif
  169. };
  170. void __init init_bcm1480_irqs(void)
  171. {
  172. int i;
  173. for (i = 0; i < BCM1480_NR_IRQS; i++) {
  174. irq_set_chip_and_handler(i, &bcm1480_irq_type,
  175. handle_level_irq);
  176. bcm1480_irq_owner[i] = 0;
  177. }
  178. }
  179. /*
  180. * init_IRQ is called early in the boot sequence from init/main.c. It
  181. * is responsible for setting up the interrupt mapper and installing the
  182. * handler that will be responsible for dispatching interrupts to the
  183. * "right" place.
  184. */
  185. /*
  186. * For now, map all interrupts to IP[2]. We could save
  187. * some cycles by parceling out system interrupts to different
  188. * IP lines, but keep it simple for bringup. We'll also direct
  189. * all interrupts to a single CPU; we should probably route
  190. * PCI and LDT to one cpu and everything else to the other
  191. * to balance the load a bit.
  192. *
  193. * On the second cpu, everything is set to IP5, which is
  194. * ignored, EXCEPT the mailbox interrupt. That one is
  195. * set to IP[2] so it is handled. This is needed so we
  196. * can do cross-cpu function calls, as required by SMP
  197. */
  198. #define IMR_IP2_VAL K_BCM1480_INT_MAP_I0
  199. #define IMR_IP3_VAL K_BCM1480_INT_MAP_I1
  200. #define IMR_IP4_VAL K_BCM1480_INT_MAP_I2
  201. #define IMR_IP5_VAL K_BCM1480_INT_MAP_I3
  202. #define IMR_IP6_VAL K_BCM1480_INT_MAP_I4
  203. void __init arch_init_irq(void)
  204. {
  205. unsigned int i, cpu;
  206. u64 tmp;
  207. unsigned int imask = STATUSF_IP4 | STATUSF_IP3 | STATUSF_IP2 |
  208. STATUSF_IP1 | STATUSF_IP0;
  209. /* Default everything to IP2 */
  210. /* Start with _high registers which has no bit 0 interrupt source */
  211. for (i = 1; i < BCM1480_NR_IRQS_HALF; i++) { /* was I0 */
  212. for (cpu = 0; cpu < 4; cpu++) {
  213. __raw_writeq(IMR_IP2_VAL,
  214. IOADDR(A_BCM1480_IMR_REGISTER(cpu,
  215. R_BCM1480_IMR_INTERRUPT_MAP_BASE_H) + (i << 3)));
  216. }
  217. }
  218. /* Now do _low registers */
  219. for (i = 0; i < BCM1480_NR_IRQS_HALF; i++) {
  220. for (cpu = 0; cpu < 4; cpu++) {
  221. __raw_writeq(IMR_IP2_VAL,
  222. IOADDR(A_BCM1480_IMR_REGISTER(cpu,
  223. R_BCM1480_IMR_INTERRUPT_MAP_BASE_L) + (i << 3)));
  224. }
  225. }
  226. init_bcm1480_irqs();
  227. /*
  228. * Map the high 16 bits of mailbox_0 registers to IP[3], for
  229. * inter-cpu messages
  230. */
  231. /* Was I1 */
  232. for (cpu = 0; cpu < 4; cpu++) {
  233. __raw_writeq(IMR_IP3_VAL, IOADDR(A_BCM1480_IMR_REGISTER(cpu, R_BCM1480_IMR_INTERRUPT_MAP_BASE_H) +
  234. (K_BCM1480_INT_MBOX_0_0 << 3)));
  235. }
  236. /* Clear the mailboxes. The firmware may leave them dirty */
  237. for (cpu = 0; cpu < 4; cpu++) {
  238. __raw_writeq(0xffffffffffffffffULL,
  239. IOADDR(A_BCM1480_IMR_REGISTER(cpu, R_BCM1480_IMR_MAILBOX_0_CLR_CPU)));
  240. __raw_writeq(0xffffffffffffffffULL,
  241. IOADDR(A_BCM1480_IMR_REGISTER(cpu, R_BCM1480_IMR_MAILBOX_1_CLR_CPU)));
  242. }
  243. /* Mask everything except the high 16 bit of mailbox_0 registers for all cpus */
  244. tmp = ~((u64) 0) ^ ( (((u64) 1) << K_BCM1480_INT_MBOX_0_0));
  245. for (cpu = 0; cpu < 4; cpu++) {
  246. __raw_writeq(tmp, IOADDR(A_BCM1480_IMR_REGISTER(cpu, R_BCM1480_IMR_INTERRUPT_MASK_H)));
  247. }
  248. tmp = ~((u64) 0);
  249. for (cpu = 0; cpu < 4; cpu++) {
  250. __raw_writeq(tmp, IOADDR(A_BCM1480_IMR_REGISTER(cpu, R_BCM1480_IMR_INTERRUPT_MASK_L)));
  251. }
  252. /*
  253. * Note that the timer interrupts are also mapped, but this is
  254. * done in bcm1480_time_init(). Also, the profiling driver
  255. * does its own management of IP7.
  256. */
  257. /* Enable necessary IPs, disable the rest */
  258. change_c0_status(ST0_IM, imask);
  259. }
  260. extern void bcm1480_mailbox_interrupt(void);
  261. static inline void dispatch_ip2(void)
  262. {
  263. unsigned long long mask_h, mask_l;
  264. unsigned int cpu = smp_processor_id();
  265. unsigned long base;
  266. /*
  267. * Default...we've hit an IP[2] interrupt, which means we've got to
  268. * check the 1480 interrupt registers to figure out what to do. Need
  269. * to detect which CPU we're on, now that smp_affinity is supported.
  270. */
  271. base = A_BCM1480_IMR_MAPPER(cpu);
  272. mask_h = __raw_readq(
  273. IOADDR(base + R_BCM1480_IMR_INTERRUPT_STATUS_BASE_H));
  274. mask_l = __raw_readq(
  275. IOADDR(base + R_BCM1480_IMR_INTERRUPT_STATUS_BASE_L));
  276. if (mask_h) {
  277. if (mask_h ^ 1)
  278. do_IRQ(fls64(mask_h) - 1);
  279. else if (mask_l)
  280. do_IRQ(63 + fls64(mask_l));
  281. }
  282. }
  283. asmlinkage void plat_irq_dispatch(void)
  284. {
  285. unsigned int cpu = smp_processor_id();
  286. unsigned int pending;
  287. pending = read_c0_cause() & read_c0_status();
  288. if (pending & CAUSEF_IP4)
  289. do_IRQ(K_BCM1480_INT_TIMER_0 + cpu);
  290. #ifdef CONFIG_SMP
  291. else if (pending & CAUSEF_IP3)
  292. bcm1480_mailbox_interrupt();
  293. #endif
  294. else if (pending & CAUSEF_IP2)
  295. dispatch_ip2();
  296. }