mcip.c 8.4 KB

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