ip30-irq.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ip30-irq.c: Highlevel interrupt handling for IP30 architecture.
  4. */
  5. #include <linux/errno.h>
  6. #include <linux/init.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/irq.h>
  9. #include <linux/irqdomain.h>
  10. #include <linux/percpu.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/tick.h>
  13. #include <linux/types.h>
  14. #include <asm/irq_cpu.h>
  15. #include <asm/sgi/heart.h>
  16. #include "ip30-common.h"
  17. struct heart_irq_data {
  18. u64 *irq_mask;
  19. int cpu;
  20. };
  21. static DECLARE_BITMAP(heart_irq_map, HEART_NUM_IRQS);
  22. static DEFINE_PER_CPU(unsigned long, irq_enable_mask);
  23. static inline int heart_alloc_int(void)
  24. {
  25. int bit;
  26. again:
  27. bit = find_first_zero_bit(heart_irq_map, HEART_NUM_IRQS);
  28. if (bit >= HEART_NUM_IRQS)
  29. return -ENOSPC;
  30. if (test_and_set_bit(bit, heart_irq_map))
  31. goto again;
  32. return bit;
  33. }
  34. static void ip30_error_irq(struct irq_desc *desc)
  35. {
  36. u64 pending, mask, cause, error_irqs, err_reg;
  37. int cpu = smp_processor_id();
  38. int i;
  39. pending = heart_read(&heart_regs->isr);
  40. mask = heart_read(&heart_regs->imr[cpu]);
  41. cause = heart_read(&heart_regs->cause);
  42. error_irqs = (pending & HEART_L4_INT_MASK & mask);
  43. /* Bail if there's nothing to process (how did we get here, then?) */
  44. if (unlikely(!error_irqs))
  45. return;
  46. /* Prevent any of the error IRQs from firing again. */
  47. heart_write(mask & ~(pending), &heart_regs->imr[cpu]);
  48. /* Ack all error IRQs. */
  49. heart_write(HEART_L4_INT_MASK, &heart_regs->clear_isr);
  50. /*
  51. * If we also have a cause value, then something happened, so loop
  52. * through the error IRQs and report a "heart attack" for each one
  53. * and print the value of the HEART cause register. This is really
  54. * primitive right now, but it should hopefully work until a more
  55. * robust error handling routine can be put together.
  56. *
  57. * Refer to heart.h for the HC_* macros to work out the cause
  58. * that got us here.
  59. */
  60. if (cause) {
  61. pr_alert("IP30: CPU%d: HEART ATTACK! ISR = 0x%.16llx, IMR = 0x%.16llx, CAUSE = 0x%.16llx\n",
  62. cpu, pending, mask, cause);
  63. if (cause & HC_COR_MEM_ERR) {
  64. err_reg = heart_read(&heart_regs->mem_err_addr);
  65. pr_alert(" HEART_MEMERR_ADDR = 0x%.16llx\n", err_reg);
  66. }
  67. /* i = 63; i >= 51; i-- */
  68. for (i = HEART_ERR_MASK_END; i >= HEART_ERR_MASK_START; i--)
  69. if ((pending >> i) & 1)
  70. pr_alert(" HEART Error IRQ #%d\n", i);
  71. /* XXX: Seems possible to loop forever here, so panic(). */
  72. panic("IP30: Fatal Error !\n");
  73. }
  74. /* Unmask the error IRQs. */
  75. heart_write(mask, &heart_regs->imr[cpu]);
  76. }
  77. static void ip30_normal_irq(struct irq_desc *desc)
  78. {
  79. int cpu = smp_processor_id();
  80. struct irq_domain *domain;
  81. u64 pend, mask;
  82. int ret;
  83. pend = heart_read(&heart_regs->isr);
  84. mask = (heart_read(&heart_regs->imr[cpu]) &
  85. (HEART_L0_INT_MASK | HEART_L1_INT_MASK | HEART_L2_INT_MASK));
  86. pend &= mask;
  87. if (unlikely(!pend))
  88. return;
  89. #ifdef CONFIG_SMP
  90. if (pend & BIT_ULL(HEART_L2_INT_RESCHED_CPU_0)) {
  91. heart_write(BIT_ULL(HEART_L2_INT_RESCHED_CPU_0),
  92. &heart_regs->clear_isr);
  93. scheduler_ipi();
  94. } else if (pend & BIT_ULL(HEART_L2_INT_RESCHED_CPU_1)) {
  95. heart_write(BIT_ULL(HEART_L2_INT_RESCHED_CPU_1),
  96. &heart_regs->clear_isr);
  97. scheduler_ipi();
  98. } else if (pend & BIT_ULL(HEART_L2_INT_CALL_CPU_0)) {
  99. heart_write(BIT_ULL(HEART_L2_INT_CALL_CPU_0),
  100. &heart_regs->clear_isr);
  101. generic_smp_call_function_interrupt();
  102. } else if (pend & BIT_ULL(HEART_L2_INT_CALL_CPU_1)) {
  103. heart_write(BIT_ULL(HEART_L2_INT_CALL_CPU_1),
  104. &heart_regs->clear_isr);
  105. generic_smp_call_function_interrupt();
  106. } else
  107. #endif
  108. {
  109. domain = irq_desc_get_handler_data(desc);
  110. ret = generic_handle_domain_irq(domain, __ffs(pend));
  111. if (ret)
  112. spurious_interrupt();
  113. }
  114. }
  115. static void ip30_ack_heart_irq(struct irq_data *d)
  116. {
  117. heart_write(BIT_ULL(d->hwirq), &heart_regs->clear_isr);
  118. }
  119. static void ip30_mask_heart_irq(struct irq_data *d)
  120. {
  121. struct heart_irq_data *hd = irq_data_get_irq_chip_data(d);
  122. unsigned long *mask = &per_cpu(irq_enable_mask, hd->cpu);
  123. clear_bit(d->hwirq, mask);
  124. heart_write(*mask, &heart_regs->imr[hd->cpu]);
  125. }
  126. static void ip30_mask_and_ack_heart_irq(struct irq_data *d)
  127. {
  128. struct heart_irq_data *hd = irq_data_get_irq_chip_data(d);
  129. unsigned long *mask = &per_cpu(irq_enable_mask, hd->cpu);
  130. clear_bit(d->hwirq, mask);
  131. heart_write(*mask, &heart_regs->imr[hd->cpu]);
  132. heart_write(BIT_ULL(d->hwirq), &heart_regs->clear_isr);
  133. }
  134. static void ip30_unmask_heart_irq(struct irq_data *d)
  135. {
  136. struct heart_irq_data *hd = irq_data_get_irq_chip_data(d);
  137. unsigned long *mask = &per_cpu(irq_enable_mask, hd->cpu);
  138. set_bit(d->hwirq, mask);
  139. heart_write(*mask, &heart_regs->imr[hd->cpu]);
  140. }
  141. static int ip30_set_heart_irq_affinity(struct irq_data *d,
  142. const struct cpumask *mask, bool force)
  143. {
  144. struct heart_irq_data *hd = irq_data_get_irq_chip_data(d);
  145. if (!hd)
  146. return -EINVAL;
  147. if (irqd_is_started(d))
  148. ip30_mask_and_ack_heart_irq(d);
  149. hd->cpu = cpumask_first_and(mask, cpu_online_mask);
  150. if (irqd_is_started(d))
  151. ip30_unmask_heart_irq(d);
  152. irq_data_update_effective_affinity(d, cpumask_of(hd->cpu));
  153. return 0;
  154. }
  155. static struct irq_chip heart_irq_chip = {
  156. .name = "HEART",
  157. .irq_ack = ip30_ack_heart_irq,
  158. .irq_mask = ip30_mask_heart_irq,
  159. .irq_mask_ack = ip30_mask_and_ack_heart_irq,
  160. .irq_unmask = ip30_unmask_heart_irq,
  161. .irq_set_affinity = ip30_set_heart_irq_affinity,
  162. };
  163. static int heart_domain_alloc(struct irq_domain *domain, unsigned int virq,
  164. unsigned int nr_irqs, void *arg)
  165. {
  166. struct irq_alloc_info *info = arg;
  167. struct heart_irq_data *hd;
  168. int hwirq;
  169. if (nr_irqs > 1 || !info)
  170. return -EINVAL;
  171. hd = kzalloc(sizeof(*hd), GFP_KERNEL);
  172. if (!hd)
  173. return -ENOMEM;
  174. hwirq = heart_alloc_int();
  175. if (hwirq < 0) {
  176. kfree(hd);
  177. return -EAGAIN;
  178. }
  179. irq_domain_set_info(domain, virq, hwirq, &heart_irq_chip, hd,
  180. handle_level_irq, NULL, NULL);
  181. return 0;
  182. }
  183. static void heart_domain_free(struct irq_domain *domain,
  184. unsigned int virq, unsigned int nr_irqs)
  185. {
  186. struct irq_data *irqd;
  187. if (nr_irqs > 1)
  188. return;
  189. irqd = irq_domain_get_irq_data(domain, virq);
  190. if (irqd) {
  191. clear_bit(irqd->hwirq, heart_irq_map);
  192. kfree(irqd->chip_data);
  193. }
  194. }
  195. static const struct irq_domain_ops heart_domain_ops = {
  196. .alloc = heart_domain_alloc,
  197. .free = heart_domain_free,
  198. };
  199. void __init ip30_install_ipi(void)
  200. {
  201. int cpu = smp_processor_id();
  202. unsigned long *mask = &per_cpu(irq_enable_mask, cpu);
  203. set_bit(HEART_L2_INT_RESCHED_CPU_0 + cpu, mask);
  204. heart_write(BIT_ULL(HEART_L2_INT_RESCHED_CPU_0 + cpu),
  205. &heart_regs->clear_isr);
  206. set_bit(HEART_L2_INT_CALL_CPU_0 + cpu, mask);
  207. heart_write(BIT_ULL(HEART_L2_INT_CALL_CPU_0 + cpu),
  208. &heart_regs->clear_isr);
  209. heart_write(*mask, &heart_regs->imr[cpu]);
  210. }
  211. void __init arch_init_irq(void)
  212. {
  213. struct irq_domain *domain;
  214. struct fwnode_handle *fn;
  215. unsigned long *mask;
  216. int i;
  217. mips_cpu_irq_init();
  218. /* Mask all IRQs. */
  219. heart_write(HEART_CLR_ALL_MASK, &heart_regs->imr[0]);
  220. heart_write(HEART_CLR_ALL_MASK, &heart_regs->imr[1]);
  221. heart_write(HEART_CLR_ALL_MASK, &heart_regs->imr[2]);
  222. heart_write(HEART_CLR_ALL_MASK, &heart_regs->imr[3]);
  223. /* Ack everything. */
  224. heart_write(HEART_ACK_ALL_MASK, &heart_regs->clear_isr);
  225. /* Enable specific HEART error IRQs for each CPU. */
  226. mask = &per_cpu(irq_enable_mask, 0);
  227. *mask |= HEART_CPU0_ERR_MASK;
  228. heart_write(*mask, &heart_regs->imr[0]);
  229. mask = &per_cpu(irq_enable_mask, 1);
  230. *mask |= HEART_CPU1_ERR_MASK;
  231. heart_write(*mask, &heart_regs->imr[1]);
  232. /*
  233. * Some HEART bits are reserved by hardware or by software convention.
  234. * Mark these as reserved right away so they won't be accidentally
  235. * used later.
  236. */
  237. set_bit(HEART_L0_INT_GENERIC, heart_irq_map);
  238. set_bit(HEART_L0_INT_FLOW_CTRL_HWTR_0, heart_irq_map);
  239. set_bit(HEART_L0_INT_FLOW_CTRL_HWTR_1, heart_irq_map);
  240. set_bit(HEART_L2_INT_RESCHED_CPU_0, heart_irq_map);
  241. set_bit(HEART_L2_INT_RESCHED_CPU_1, heart_irq_map);
  242. set_bit(HEART_L2_INT_CALL_CPU_0, heart_irq_map);
  243. set_bit(HEART_L2_INT_CALL_CPU_1, heart_irq_map);
  244. set_bit(HEART_L3_INT_TIMER, heart_irq_map);
  245. /* Reserve the error interrupts (#51 to #63). */
  246. for (i = HEART_L4_INT_XWID_ERR_9; i <= HEART_L4_INT_HEART_EXCP; i++)
  247. set_bit(i, heart_irq_map);
  248. fn = irq_domain_alloc_named_fwnode("HEART");
  249. WARN_ON(fn == NULL);
  250. if (!fn)
  251. return;
  252. domain = irq_domain_create_linear(fn, HEART_NUM_IRQS,
  253. &heart_domain_ops, NULL);
  254. WARN_ON(domain == NULL);
  255. if (!domain)
  256. return;
  257. irq_set_default_host(domain);
  258. irq_set_percpu_devid(IP30_HEART_L0_IRQ);
  259. irq_set_chained_handler_and_data(IP30_HEART_L0_IRQ, ip30_normal_irq,
  260. domain);
  261. irq_set_percpu_devid(IP30_HEART_L1_IRQ);
  262. irq_set_chained_handler_and_data(IP30_HEART_L1_IRQ, ip30_normal_irq,
  263. domain);
  264. irq_set_percpu_devid(IP30_HEART_L2_IRQ);
  265. irq_set_chained_handler_and_data(IP30_HEART_L2_IRQ, ip30_normal_irq,
  266. domain);
  267. irq_set_percpu_devid(IP30_HEART_ERR_IRQ);
  268. irq_set_chained_handler_and_data(IP30_HEART_ERR_IRQ, ip30_error_irq,
  269. domain);
  270. }