mcip.c 10 KB

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