mcip.c 8.0 KB

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