mcip.c 8.6 KB

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