mcip.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. /*
  21. * Set mask to halt GFRC if any online core in SMP cluster is halted.
  22. * Only works for ARC HS v3.0+, on earlier versions has no effect.
  23. */
  24. static void mcip_update_gfrc_halt_mask(int cpu)
  25. {
  26. struct bcr_generic gfrc;
  27. unsigned long flags;
  28. u32 gfrc_halt_mask;
  29. READ_BCR(ARC_REG_GFRC_BUILD, gfrc);
  30. /*
  31. * CMD_GFRC_SET_CORE and CMD_GFRC_READ_CORE commands were added in
  32. * GFRC 0x3 version.
  33. */
  34. if (gfrc.ver < 0x3)
  35. return;
  36. raw_spin_lock_irqsave(&mcip_lock, flags);
  37. __mcip_cmd(CMD_GFRC_READ_CORE, 0);
  38. gfrc_halt_mask = read_aux_reg(ARC_REG_MCIP_READBACK);
  39. gfrc_halt_mask |= BIT(cpu);
  40. __mcip_cmd_data(CMD_GFRC_SET_CORE, 0, gfrc_halt_mask);
  41. raw_spin_unlock_irqrestore(&mcip_lock, flags);
  42. }
  43. static void mcip_update_debug_halt_mask(int cpu)
  44. {
  45. u32 mcip_mask = 0;
  46. unsigned long flags;
  47. raw_spin_lock_irqsave(&mcip_lock, flags);
  48. /*
  49. * mcip_mask is same for CMD_DEBUG_SET_SELECT and CMD_DEBUG_SET_MASK
  50. * commands. So read it once instead of reading both CMD_DEBUG_READ_MASK
  51. * and CMD_DEBUG_READ_SELECT.
  52. */
  53. __mcip_cmd(CMD_DEBUG_READ_SELECT, 0);
  54. mcip_mask = read_aux_reg(ARC_REG_MCIP_READBACK);
  55. mcip_mask |= BIT(cpu);
  56. __mcip_cmd_data(CMD_DEBUG_SET_SELECT, 0, mcip_mask);
  57. /*
  58. * Parameter specified halt cause:
  59. * STATUS32[H]/actionpoint/breakpoint/self-halt
  60. * We choose all of them (0xF).
  61. */
  62. __mcip_cmd_data(CMD_DEBUG_SET_MASK, 0xF, mcip_mask);
  63. raw_spin_unlock_irqrestore(&mcip_lock, flags);
  64. }
  65. static void mcip_setup_per_cpu(int cpu)
  66. {
  67. struct mcip_bcr mp;
  68. READ_BCR(ARC_REG_MCIP_BCR, mp);
  69. smp_ipi_irq_setup(cpu, IPI_IRQ);
  70. smp_ipi_irq_setup(cpu, SOFTIRQ_IRQ);
  71. /* Update GFRC halt mask as new CPU came online */
  72. if (mp.gfrc)
  73. mcip_update_gfrc_halt_mask(cpu);
  74. /* Update MCIP debug mask as new CPU came online */
  75. if (mp.dbg)
  76. mcip_update_debug_halt_mask(cpu);
  77. }
  78. static void mcip_ipi_send(int cpu)
  79. {
  80. unsigned long flags;
  81. int ipi_was_pending;
  82. /* ARConnect can only send IPI to others */
  83. if (unlikely(cpu == raw_smp_processor_id())) {
  84. arc_softirq_trigger(SOFTIRQ_IRQ);
  85. return;
  86. }
  87. raw_spin_lock_irqsave(&mcip_lock, flags);
  88. /*
  89. * If receiver already has a pending interrupt, elide sending this one.
  90. * Linux cross core calling works well with concurrent IPIs
  91. * coalesced into one
  92. * see arch/arc/kernel/smp.c: ipi_send_msg_one()
  93. */
  94. __mcip_cmd(CMD_INTRPT_READ_STATUS, cpu);
  95. ipi_was_pending = read_aux_reg(ARC_REG_MCIP_READBACK);
  96. if (!ipi_was_pending)
  97. __mcip_cmd(CMD_INTRPT_GENERATE_IRQ, cpu);
  98. raw_spin_unlock_irqrestore(&mcip_lock, flags);
  99. }
  100. static void mcip_ipi_clear(int irq)
  101. {
  102. unsigned int cpu, c;
  103. unsigned long flags;
  104. if (unlikely(irq == SOFTIRQ_IRQ)) {
  105. arc_softirq_clear(irq);
  106. return;
  107. }
  108. raw_spin_lock_irqsave(&mcip_lock, flags);
  109. /* Who sent the IPI */
  110. __mcip_cmd(CMD_INTRPT_CHECK_SOURCE, 0);
  111. cpu = read_aux_reg(ARC_REG_MCIP_READBACK); /* 1,2,4,8... */
  112. /*
  113. * In rare case, multiple concurrent IPIs sent to same target can
  114. * possibly be coalesced by MCIP into 1 asserted IRQ, so @cpus can be
  115. * "vectored" (multiple bits sets) as opposed to typical single bit
  116. */
  117. do {
  118. c = __ffs(cpu); /* 0,1,2,3 */
  119. __mcip_cmd(CMD_INTRPT_GENERATE_ACK, c);
  120. cpu &= ~(1U << c);
  121. } while (cpu);
  122. raw_spin_unlock_irqrestore(&mcip_lock, flags);
  123. }
  124. static void mcip_probe_n_setup(void)
  125. {
  126. struct mcip_bcr mp;
  127. READ_BCR(ARC_REG_MCIP_BCR, mp);
  128. sprintf(smp_cpuinfo_buf,
  129. "Extn [SMP]\t: ARConnect (v%d): %d cores with %s%s%s%s\n",
  130. mp.ver, mp.num_cores,
  131. IS_AVAIL1(mp.ipi, "IPI "),
  132. IS_AVAIL1(mp.idu, "IDU "),
  133. IS_AVAIL1(mp.dbg, "DEBUG "),
  134. IS_AVAIL1(mp.gfrc, "GFRC"));
  135. cpuinfo_arc700[0].extn.gfrc = mp.gfrc;
  136. }
  137. struct plat_smp_ops plat_smp_ops = {
  138. .info = smp_cpuinfo_buf,
  139. .init_early_smp = mcip_probe_n_setup,
  140. .init_per_cpu = mcip_setup_per_cpu,
  141. .ipi_send = mcip_ipi_send,
  142. .ipi_clear = mcip_ipi_clear,
  143. };
  144. #endif
  145. /***************************************************************************
  146. * ARCv2 Interrupt Distribution Unit (IDU)
  147. *
  148. * Connects external "COMMON" IRQs to core intc, providing:
  149. * -dynamic routing (IRQ affinity)
  150. * -load balancing (Round Robin interrupt distribution)
  151. * -1:N distribution
  152. *
  153. * It physically resides in the MCIP hw block
  154. */
  155. #include <linux/irqchip.h>
  156. #include <linux/of.h>
  157. #include <linux/of_irq.h>
  158. /*
  159. * Set the DEST for @cmn_irq to @cpu_mask (1 bit per core)
  160. */
  161. static void idu_set_dest(unsigned int cmn_irq, unsigned int cpu_mask)
  162. {
  163. __mcip_cmd_data(CMD_IDU_SET_DEST, cmn_irq, cpu_mask);
  164. }
  165. static void idu_set_mode(unsigned int cmn_irq, unsigned int lvl,
  166. unsigned int distr)
  167. {
  168. union {
  169. unsigned int word;
  170. struct {
  171. unsigned int distr:2, pad:2, lvl:1, pad2:27;
  172. };
  173. } data;
  174. data.distr = distr;
  175. data.lvl = lvl;
  176. __mcip_cmd_data(CMD_IDU_SET_MODE, cmn_irq, data.word);
  177. }
  178. static void idu_irq_mask_raw(irq_hw_number_t hwirq)
  179. {
  180. unsigned long flags;
  181. raw_spin_lock_irqsave(&mcip_lock, flags);
  182. __mcip_cmd_data(CMD_IDU_SET_MASK, hwirq, 1);
  183. raw_spin_unlock_irqrestore(&mcip_lock, flags);
  184. }
  185. static void idu_irq_mask(struct irq_data *data)
  186. {
  187. idu_irq_mask_raw(data->hwirq);
  188. }
  189. static void idu_irq_unmask(struct irq_data *data)
  190. {
  191. unsigned long flags;
  192. raw_spin_lock_irqsave(&mcip_lock, flags);
  193. __mcip_cmd_data(CMD_IDU_SET_MASK, data->hwirq, 0);
  194. raw_spin_unlock_irqrestore(&mcip_lock, flags);
  195. }
  196. static int
  197. idu_irq_set_affinity(struct irq_data *data, const struct cpumask *cpumask,
  198. bool force)
  199. {
  200. unsigned long flags;
  201. cpumask_t online;
  202. unsigned int destination_bits;
  203. unsigned int distribution_mode;
  204. /* errout if no online cpu per @cpumask */
  205. if (!cpumask_and(&online, cpumask, cpu_online_mask))
  206. return -EINVAL;
  207. raw_spin_lock_irqsave(&mcip_lock, flags);
  208. destination_bits = cpumask_bits(&online)[0];
  209. idu_set_dest(data->hwirq, destination_bits);
  210. if (ffs(destination_bits) == fls(destination_bits))
  211. distribution_mode = IDU_M_DISTRI_DEST;
  212. else
  213. distribution_mode = IDU_M_DISTRI_RR;
  214. idu_set_mode(data->hwirq, IDU_M_TRIG_LEVEL, distribution_mode);
  215. raw_spin_unlock_irqrestore(&mcip_lock, flags);
  216. return IRQ_SET_MASK_OK;
  217. }
  218. static void idu_irq_enable(struct irq_data *data)
  219. {
  220. /*
  221. * By default send all common interrupts to all available online CPUs.
  222. * The affinity of common interrupts in IDU must be set manually since
  223. * in some cases the kernel will not call irq_set_affinity() by itself:
  224. * 1. When the kernel is not configured with support of SMP.
  225. * 2. When the kernel is configured with support of SMP but upper
  226. * interrupt controllers does not support setting of the affinity
  227. * and cannot propagate it to IDU.
  228. */
  229. idu_irq_set_affinity(data, cpu_online_mask, false);
  230. idu_irq_unmask(data);
  231. }
  232. static struct irq_chip idu_irq_chip = {
  233. .name = "MCIP IDU Intc",
  234. .irq_mask = idu_irq_mask,
  235. .irq_unmask = idu_irq_unmask,
  236. .irq_enable = idu_irq_enable,
  237. #ifdef CONFIG_SMP
  238. .irq_set_affinity = idu_irq_set_affinity,
  239. #endif
  240. };
  241. static void idu_cascade_isr(struct irq_desc *desc)
  242. {
  243. struct irq_domain *idu_domain = irq_desc_get_handler_data(desc);
  244. struct irq_chip *core_chip = irq_desc_get_chip(desc);
  245. irq_hw_number_t core_hwirq = irqd_to_hwirq(irq_desc_get_irq_data(desc));
  246. irq_hw_number_t idu_hwirq = core_hwirq - FIRST_EXT_IRQ;
  247. chained_irq_enter(core_chip, desc);
  248. generic_handle_irq(irq_find_mapping(idu_domain, idu_hwirq));
  249. chained_irq_exit(core_chip, desc);
  250. }
  251. static int idu_irq_map(struct irq_domain *d, unsigned int virq, irq_hw_number_t hwirq)
  252. {
  253. irq_set_chip_and_handler(virq, &idu_irq_chip, handle_level_irq);
  254. irq_set_status_flags(virq, IRQ_MOVE_PCNTXT);
  255. return 0;
  256. }
  257. static const struct irq_domain_ops idu_irq_ops = {
  258. .xlate = irq_domain_xlate_onecell,
  259. .map = idu_irq_map,
  260. };
  261. /*
  262. * [16, 23]: Statically assigned always private-per-core (Timers, WDT, IPI)
  263. * [24, 23+C]: If C > 0 then "C" common IRQs
  264. * [24+C, N]: Not statically assigned, private-per-core
  265. */
  266. static int __init
  267. idu_of_init(struct device_node *intc, struct device_node *parent)
  268. {
  269. struct irq_domain *domain;
  270. int nr_irqs;
  271. int i, virq;
  272. struct mcip_bcr mp;
  273. struct mcip_idu_bcr idu_bcr;
  274. READ_BCR(ARC_REG_MCIP_BCR, mp);
  275. if (!mp.idu)
  276. panic("IDU not detected, but DeviceTree using it");
  277. READ_BCR(ARC_REG_MCIP_IDU_BCR, idu_bcr);
  278. nr_irqs = mcip_idu_bcr_to_nr_irqs(idu_bcr);
  279. pr_info("MCIP: IDU supports %u common irqs\n", nr_irqs);
  280. domain = irq_domain_add_linear(intc, nr_irqs, &idu_irq_ops, NULL);
  281. /* Parent interrupts (core-intc) are already mapped */
  282. for (i = 0; i < nr_irqs; i++) {
  283. /* Mask all common interrupts by default */
  284. idu_irq_mask_raw(i);
  285. /*
  286. * Return parent uplink IRQs (towards core intc) 24,25,.....
  287. * this step has been done before already
  288. * however we need it to get the parent virq and set IDU handler
  289. * as first level isr
  290. */
  291. virq = irq_create_mapping(NULL, i + FIRST_EXT_IRQ);
  292. BUG_ON(!virq);
  293. irq_set_chained_handler_and_data(virq, idu_cascade_isr, domain);
  294. }
  295. __mcip_cmd(CMD_IDU_ENABLE, 0);
  296. return 0;
  297. }
  298. IRQCHIP_DECLARE(arcv2_idu_intc, "snps,archs-idu-intc", idu_of_init);