mcip.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * ARC ARConnect (MultiCore IP) support (formerly known as MCIP)
  3. *
  4. * Copyright (C) 2013 Synopsys, Inc. (www.synopsys.com)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/smp.h>
  11. #include <linux/irq.h>
  12. #include <linux/irqchip/chained_irq.h>
  13. #include <linux/spinlock.h>
  14. #include <soc/arc/mcip.h>
  15. #include <asm/irqflags-arcv2.h>
  16. #include <asm/setup.h>
  17. static DEFINE_RAW_SPINLOCK(mcip_lock);
  18. #ifdef CONFIG_SMP
  19. static char smp_cpuinfo_buf[128];
  20. static void mcip_setup_per_cpu(int cpu)
  21. {
  22. smp_ipi_irq_setup(cpu, IPI_IRQ);
  23. smp_ipi_irq_setup(cpu, SOFTIRQ_IRQ);
  24. }
  25. static void mcip_ipi_send(int cpu)
  26. {
  27. unsigned long flags;
  28. int ipi_was_pending;
  29. /* ARConnect can only send IPI to others */
  30. if (unlikely(cpu == raw_smp_processor_id())) {
  31. arc_softirq_trigger(SOFTIRQ_IRQ);
  32. return;
  33. }
  34. raw_spin_lock_irqsave(&mcip_lock, flags);
  35. /*
  36. * If receiver already has a pending interrupt, elide sending this one.
  37. * Linux cross core calling works well with concurrent IPIs
  38. * coalesced into one
  39. * see arch/arc/kernel/smp.c: ipi_send_msg_one()
  40. */
  41. __mcip_cmd(CMD_INTRPT_READ_STATUS, cpu);
  42. ipi_was_pending = read_aux_reg(ARC_REG_MCIP_READBACK);
  43. if (!ipi_was_pending)
  44. __mcip_cmd(CMD_INTRPT_GENERATE_IRQ, cpu);
  45. raw_spin_unlock_irqrestore(&mcip_lock, flags);
  46. }
  47. static void mcip_ipi_clear(int irq)
  48. {
  49. unsigned int cpu, c;
  50. unsigned long flags;
  51. if (unlikely(irq == SOFTIRQ_IRQ)) {
  52. arc_softirq_clear(irq);
  53. return;
  54. }
  55. raw_spin_lock_irqsave(&mcip_lock, flags);
  56. /* Who sent the IPI */
  57. __mcip_cmd(CMD_INTRPT_CHECK_SOURCE, 0);
  58. cpu = read_aux_reg(ARC_REG_MCIP_READBACK); /* 1,2,4,8... */
  59. /*
  60. * In rare case, multiple concurrent IPIs sent to same target can
  61. * possibly be coalesced by MCIP into 1 asserted IRQ, so @cpus can be
  62. * "vectored" (multiple bits sets) as opposed to typical single bit
  63. */
  64. do {
  65. c = __ffs(cpu); /* 0,1,2,3 */
  66. __mcip_cmd(CMD_INTRPT_GENERATE_ACK, c);
  67. cpu &= ~(1U << c);
  68. } while (cpu);
  69. raw_spin_unlock_irqrestore(&mcip_lock, flags);
  70. }
  71. static void mcip_probe_n_setup(void)
  72. {
  73. struct mcip_bcr mp;
  74. READ_BCR(ARC_REG_MCIP_BCR, mp);
  75. sprintf(smp_cpuinfo_buf,
  76. "Extn [SMP]\t: ARConnect (v%d): %d cores with %s%s%s%s\n",
  77. mp.ver, mp.num_cores,
  78. IS_AVAIL1(mp.ipi, "IPI "),
  79. IS_AVAIL1(mp.idu, "IDU "),
  80. IS_AVAIL1(mp.dbg, "DEBUG "),
  81. IS_AVAIL1(mp.gfrc, "GFRC"));
  82. cpuinfo_arc700[0].extn.gfrc = mp.gfrc;
  83. if (mp.dbg) {
  84. __mcip_cmd_data(CMD_DEBUG_SET_SELECT, 0, 0xf);
  85. __mcip_cmd_data(CMD_DEBUG_SET_MASK, 0xf, 0xf);
  86. }
  87. }
  88. struct plat_smp_ops plat_smp_ops = {
  89. .info = smp_cpuinfo_buf,
  90. .init_early_smp = mcip_probe_n_setup,
  91. .init_per_cpu = mcip_setup_per_cpu,
  92. .ipi_send = mcip_ipi_send,
  93. .ipi_clear = mcip_ipi_clear,
  94. };
  95. #endif
  96. /***************************************************************************
  97. * ARCv2 Interrupt Distribution Unit (IDU)
  98. *
  99. * Connects external "COMMON" IRQs to core intc, providing:
  100. * -dynamic routing (IRQ affinity)
  101. * -load balancing (Round Robin interrupt distribution)
  102. * -1:N distribution
  103. *
  104. * It physically resides in the MCIP hw block
  105. */
  106. #include <linux/irqchip.h>
  107. #include <linux/of.h>
  108. #include <linux/of_irq.h>
  109. /*
  110. * Set the DEST for @cmn_irq to @cpu_mask (1 bit per core)
  111. */
  112. static void idu_set_dest(unsigned int cmn_irq, unsigned int cpu_mask)
  113. {
  114. __mcip_cmd_data(CMD_IDU_SET_DEST, cmn_irq, cpu_mask);
  115. }
  116. static void idu_set_mode(unsigned int cmn_irq, unsigned int lvl,
  117. unsigned int distr)
  118. {
  119. union {
  120. unsigned int word;
  121. struct {
  122. unsigned int distr:2, pad:2, lvl:1, pad2:27;
  123. };
  124. } data;
  125. data.distr = distr;
  126. data.lvl = lvl;
  127. __mcip_cmd_data(CMD_IDU_SET_MODE, cmn_irq, data.word);
  128. }
  129. static void idu_irq_mask(struct irq_data *data)
  130. {
  131. unsigned long flags;
  132. raw_spin_lock_irqsave(&mcip_lock, flags);
  133. __mcip_cmd_data(CMD_IDU_SET_MASK, data->hwirq, 1);
  134. raw_spin_unlock_irqrestore(&mcip_lock, flags);
  135. }
  136. static void idu_irq_unmask(struct irq_data *data)
  137. {
  138. unsigned long flags;
  139. raw_spin_lock_irqsave(&mcip_lock, flags);
  140. __mcip_cmd_data(CMD_IDU_SET_MASK, data->hwirq, 0);
  141. raw_spin_unlock_irqrestore(&mcip_lock, flags);
  142. }
  143. static int
  144. idu_irq_set_affinity(struct irq_data *data, const struct cpumask *cpumask,
  145. bool force)
  146. {
  147. unsigned long flags;
  148. cpumask_t online;
  149. unsigned int destination_bits;
  150. unsigned int distribution_mode;
  151. /* errout if no online cpu per @cpumask */
  152. if (!cpumask_and(&online, cpumask, cpu_online_mask))
  153. return -EINVAL;
  154. raw_spin_lock_irqsave(&mcip_lock, flags);
  155. destination_bits = cpumask_bits(&online)[0];
  156. idu_set_dest(data->hwirq, destination_bits);
  157. if (ffs(destination_bits) == fls(destination_bits))
  158. distribution_mode = IDU_M_DISTRI_DEST;
  159. else
  160. distribution_mode = IDU_M_DISTRI_RR;
  161. idu_set_mode(data->hwirq, IDU_M_TRIG_LEVEL, distribution_mode);
  162. raw_spin_unlock_irqrestore(&mcip_lock, flags);
  163. return IRQ_SET_MASK_OK;
  164. }
  165. static void idu_irq_enable(struct irq_data *data)
  166. {
  167. /*
  168. * By default send all common interrupts to all available online CPUs.
  169. * The affinity of common interrupts in IDU must be set manually since
  170. * in some cases the kernel will not call irq_set_affinity() by itself:
  171. * 1. When the kernel is not configured with support of SMP.
  172. * 2. When the kernel is configured with support of SMP but upper
  173. * interrupt controllers does not support setting of the affinity
  174. * and cannot propagate it to IDU.
  175. */
  176. idu_irq_set_affinity(data, cpu_online_mask, false);
  177. idu_irq_unmask(data);
  178. }
  179. static struct irq_chip idu_irq_chip = {
  180. .name = "MCIP IDU Intc",
  181. .irq_mask = idu_irq_mask,
  182. .irq_unmask = idu_irq_unmask,
  183. .irq_enable = idu_irq_enable,
  184. #ifdef CONFIG_SMP
  185. .irq_set_affinity = idu_irq_set_affinity,
  186. #endif
  187. };
  188. static void idu_cascade_isr(struct irq_desc *desc)
  189. {
  190. struct irq_domain *idu_domain = irq_desc_get_handler_data(desc);
  191. struct irq_chip *core_chip = irq_desc_get_chip(desc);
  192. irq_hw_number_t core_hwirq = irqd_to_hwirq(irq_desc_get_irq_data(desc));
  193. irq_hw_number_t idu_hwirq = core_hwirq - FIRST_EXT_IRQ;
  194. chained_irq_enter(core_chip, desc);
  195. generic_handle_irq(irq_find_mapping(idu_domain, idu_hwirq));
  196. chained_irq_exit(core_chip, desc);
  197. }
  198. static int idu_irq_map(struct irq_domain *d, unsigned int virq, irq_hw_number_t hwirq)
  199. {
  200. irq_set_chip_and_handler(virq, &idu_irq_chip, handle_level_irq);
  201. irq_set_status_flags(virq, IRQ_MOVE_PCNTXT);
  202. return 0;
  203. }
  204. static int idu_irq_xlate(struct irq_domain *d, struct device_node *n,
  205. const u32 *intspec, unsigned int intsize,
  206. irq_hw_number_t *out_hwirq, unsigned int *out_type)
  207. {
  208. /*
  209. * Ignore value of interrupt distribution mode for common interrupts in
  210. * IDU which resides in intspec[1] since setting an affinity using value
  211. * from Device Tree is deprecated in ARC.
  212. */
  213. *out_hwirq = intspec[0];
  214. *out_type = IRQ_TYPE_NONE;
  215. return 0;
  216. }
  217. static const struct irq_domain_ops idu_irq_ops = {
  218. .xlate = idu_irq_xlate,
  219. .map = idu_irq_map,
  220. };
  221. /*
  222. * [16, 23]: Statically assigned always private-per-core (Timers, WDT, IPI)
  223. * [24, 23+C]: If C > 0 then "C" common IRQs
  224. * [24+C, N]: Not statically assigned, private-per-core
  225. */
  226. static int __init
  227. idu_of_init(struct device_node *intc, struct device_node *parent)
  228. {
  229. struct irq_domain *domain;
  230. int nr_irqs;
  231. int i, virq;
  232. struct mcip_bcr mp;
  233. struct mcip_idu_bcr idu_bcr;
  234. READ_BCR(ARC_REG_MCIP_BCR, mp);
  235. if (!mp.idu)
  236. panic("IDU not detected, but DeviceTree using it");
  237. READ_BCR(ARC_REG_MCIP_IDU_BCR, idu_bcr);
  238. nr_irqs = mcip_idu_bcr_to_nr_irqs(idu_bcr);
  239. pr_info("MCIP: IDU supports %u common irqs\n", nr_irqs);
  240. domain = irq_domain_add_linear(intc, nr_irqs, &idu_irq_ops, NULL);
  241. /* Parent interrupts (core-intc) are already mapped */
  242. for (i = 0; i < nr_irqs; i++) {
  243. /*
  244. * Return parent uplink IRQs (towards core intc) 24,25,.....
  245. * this step has been done before already
  246. * however we need it to get the parent virq and set IDU handler
  247. * as first level isr
  248. */
  249. virq = irq_create_mapping(NULL, i + FIRST_EXT_IRQ);
  250. BUG_ON(!virq);
  251. irq_set_chained_handler_and_data(virq, idu_cascade_isr, domain);
  252. }
  253. __mcip_cmd(CMD_IDU_ENABLE, 0);
  254. return 0;
  255. }
  256. IRQCHIP_DECLARE(arcv2_idu_intc, "snps,archs-idu-intc", idu_of_init);