irq-sifive-plic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017 SiFive
  4. * Copyright (C) 2018 Christoph Hellwig
  5. */
  6. #define pr_fmt(fmt) "plic: " fmt
  7. #include <linux/cpu.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/io.h>
  10. #include <linux/irq.h>
  11. #include <linux/irqchip.h>
  12. #include <linux/irqchip/chained_irq.h>
  13. #include <linux/irqdomain.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/spinlock.h>
  20. #include <asm/smp.h>
  21. /*
  22. * This driver implements a version of the RISC-V PLIC with the actual layout
  23. * specified in chapter 8 of the SiFive U5 Coreplex Series Manual:
  24. *
  25. * https://static.dev.sifive.com/U54-MC-RVCoreIP.pdf
  26. *
  27. * The largest number supported by devices marked as 'sifive,plic-1.0.0', is
  28. * 1024, of which device 0 is defined as non-existent by the RISC-V Privileged
  29. * Spec.
  30. */
  31. #define MAX_DEVICES 1024
  32. #define MAX_CONTEXTS 15872
  33. /*
  34. * Each interrupt source has a priority register associated with it.
  35. * We always hardwire it to one in Linux.
  36. */
  37. #define PRIORITY_BASE 0
  38. #define PRIORITY_PER_ID 4
  39. /*
  40. * Each hart context has a vector of interrupt enable bits associated with it.
  41. * There's one bit for each interrupt source.
  42. */
  43. #define CONTEXT_ENABLE_BASE 0x2000
  44. #define CONTEXT_ENABLE_SIZE 0x80
  45. /*
  46. * Each hart context has a set of control registers associated with it. Right
  47. * now there's only two: a source priority threshold over which the hart will
  48. * take an interrupt, and a register to claim interrupts.
  49. */
  50. #define CONTEXT_BASE 0x200000
  51. #define CONTEXT_SIZE 0x1000
  52. #define CONTEXT_THRESHOLD 0x00
  53. #define CONTEXT_CLAIM 0x04
  54. #define PLIC_DISABLE_THRESHOLD 0x7
  55. #define PLIC_ENABLE_THRESHOLD 0
  56. #define PLIC_QUIRK_EDGE_INTERRUPT 0
  57. struct plic_priv {
  58. struct cpumask lmask;
  59. struct irq_domain *irqdomain;
  60. void __iomem *regs;
  61. unsigned long plic_quirks;
  62. };
  63. struct plic_handler {
  64. bool present;
  65. void __iomem *hart_base;
  66. /*
  67. * Protect mask operations on the registers given that we can't
  68. * assume atomic memory operations work on them.
  69. */
  70. raw_spinlock_t enable_lock;
  71. void __iomem *enable_base;
  72. struct plic_priv *priv;
  73. };
  74. static int plic_parent_irq __ro_after_init;
  75. static bool plic_cpuhp_setup_done __ro_after_init;
  76. static DEFINE_PER_CPU(struct plic_handler, plic_handlers);
  77. static int plic_irq_set_type(struct irq_data *d, unsigned int type);
  78. static void __plic_toggle(void __iomem *enable_base, int hwirq, int enable)
  79. {
  80. u32 __iomem *reg = enable_base + (hwirq / 32) * sizeof(u32);
  81. u32 hwirq_mask = 1 << (hwirq % 32);
  82. if (enable)
  83. writel(readl(reg) | hwirq_mask, reg);
  84. else
  85. writel(readl(reg) & ~hwirq_mask, reg);
  86. }
  87. static void plic_toggle(struct plic_handler *handler, int hwirq, int enable)
  88. {
  89. raw_spin_lock(&handler->enable_lock);
  90. __plic_toggle(handler->enable_base, hwirq, enable);
  91. raw_spin_unlock(&handler->enable_lock);
  92. }
  93. static inline void plic_irq_toggle(const struct cpumask *mask,
  94. struct irq_data *d, int enable)
  95. {
  96. int cpu;
  97. for_each_cpu(cpu, mask) {
  98. struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
  99. plic_toggle(handler, d->hwirq, enable);
  100. }
  101. }
  102. static void plic_irq_enable(struct irq_data *d)
  103. {
  104. plic_irq_toggle(irq_data_get_effective_affinity_mask(d), d, 1);
  105. }
  106. static void plic_irq_disable(struct irq_data *d)
  107. {
  108. plic_irq_toggle(irq_data_get_effective_affinity_mask(d), d, 0);
  109. }
  110. static void plic_irq_unmask(struct irq_data *d)
  111. {
  112. struct plic_priv *priv = irq_data_get_irq_chip_data(d);
  113. writel(1, priv->regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
  114. }
  115. static void plic_irq_mask(struct irq_data *d)
  116. {
  117. struct plic_priv *priv = irq_data_get_irq_chip_data(d);
  118. writel(0, priv->regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
  119. }
  120. static void plic_irq_eoi(struct irq_data *d)
  121. {
  122. struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
  123. writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
  124. }
  125. #ifdef CONFIG_SMP
  126. static int plic_set_affinity(struct irq_data *d,
  127. const struct cpumask *mask_val, bool force)
  128. {
  129. unsigned int cpu;
  130. struct cpumask amask;
  131. struct plic_priv *priv = irq_data_get_irq_chip_data(d);
  132. cpumask_and(&amask, &priv->lmask, mask_val);
  133. if (force)
  134. cpu = cpumask_first(&amask);
  135. else
  136. cpu = cpumask_any_and(&amask, cpu_online_mask);
  137. if (cpu >= nr_cpu_ids)
  138. return -EINVAL;
  139. plic_irq_disable(d);
  140. irq_data_update_effective_affinity(d, cpumask_of(cpu));
  141. if (!irqd_irq_disabled(d))
  142. plic_irq_enable(d);
  143. return IRQ_SET_MASK_OK_DONE;
  144. }
  145. #endif
  146. static struct irq_chip plic_edge_chip = {
  147. .name = "SiFive PLIC",
  148. .irq_enable = plic_irq_enable,
  149. .irq_disable = plic_irq_disable,
  150. .irq_ack = plic_irq_eoi,
  151. .irq_mask = plic_irq_mask,
  152. .irq_unmask = plic_irq_unmask,
  153. #ifdef CONFIG_SMP
  154. .irq_set_affinity = plic_set_affinity,
  155. #endif
  156. .irq_set_type = plic_irq_set_type,
  157. .flags = IRQCHIP_AFFINITY_PRE_STARTUP,
  158. };
  159. static struct irq_chip plic_chip = {
  160. .name = "SiFive PLIC",
  161. .irq_enable = plic_irq_enable,
  162. .irq_disable = plic_irq_disable,
  163. .irq_mask = plic_irq_mask,
  164. .irq_unmask = plic_irq_unmask,
  165. .irq_eoi = plic_irq_eoi,
  166. #ifdef CONFIG_SMP
  167. .irq_set_affinity = plic_set_affinity,
  168. #endif
  169. .irq_set_type = plic_irq_set_type,
  170. .flags = IRQCHIP_AFFINITY_PRE_STARTUP,
  171. };
  172. static int plic_irq_set_type(struct irq_data *d, unsigned int type)
  173. {
  174. struct plic_priv *priv = irq_data_get_irq_chip_data(d);
  175. if (!test_bit(PLIC_QUIRK_EDGE_INTERRUPT, &priv->plic_quirks))
  176. return IRQ_SET_MASK_OK_NOCOPY;
  177. switch (type) {
  178. case IRQ_TYPE_EDGE_RISING:
  179. irq_set_chip_handler_name_locked(d, &plic_edge_chip,
  180. handle_edge_irq, NULL);
  181. break;
  182. case IRQ_TYPE_LEVEL_HIGH:
  183. irq_set_chip_handler_name_locked(d, &plic_chip,
  184. handle_fasteoi_irq, NULL);
  185. break;
  186. default:
  187. return -EINVAL;
  188. }
  189. return IRQ_SET_MASK_OK;
  190. }
  191. static int plic_irqdomain_map(struct irq_domain *d, unsigned int irq,
  192. irq_hw_number_t hwirq)
  193. {
  194. struct plic_priv *priv = d->host_data;
  195. irq_domain_set_info(d, irq, hwirq, &plic_chip, d->host_data,
  196. handle_fasteoi_irq, NULL, NULL);
  197. irq_set_noprobe(irq);
  198. irq_set_affinity(irq, &priv->lmask);
  199. return 0;
  200. }
  201. static int plic_irq_domain_translate(struct irq_domain *d,
  202. struct irq_fwspec *fwspec,
  203. unsigned long *hwirq,
  204. unsigned int *type)
  205. {
  206. struct plic_priv *priv = d->host_data;
  207. if (test_bit(PLIC_QUIRK_EDGE_INTERRUPT, &priv->plic_quirks))
  208. return irq_domain_translate_twocell(d, fwspec, hwirq, type);
  209. return irq_domain_translate_onecell(d, fwspec, hwirq, type);
  210. }
  211. static int plic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  212. unsigned int nr_irqs, void *arg)
  213. {
  214. int i, ret;
  215. irq_hw_number_t hwirq;
  216. unsigned int type;
  217. struct irq_fwspec *fwspec = arg;
  218. ret = plic_irq_domain_translate(domain, fwspec, &hwirq, &type);
  219. if (ret)
  220. return ret;
  221. for (i = 0; i < nr_irqs; i++) {
  222. ret = plic_irqdomain_map(domain, virq + i, hwirq + i);
  223. if (ret)
  224. return ret;
  225. }
  226. return 0;
  227. }
  228. static const struct irq_domain_ops plic_irqdomain_ops = {
  229. .translate = plic_irq_domain_translate,
  230. .alloc = plic_irq_domain_alloc,
  231. .free = irq_domain_free_irqs_top,
  232. };
  233. /*
  234. * Handling an interrupt is a two-step process: first you claim the interrupt
  235. * by reading the claim register, then you complete the interrupt by writing
  236. * that source ID back to the same claim register. This automatically enables
  237. * and disables the interrupt, so there's nothing else to do.
  238. */
  239. static void plic_handle_irq(struct irq_desc *desc)
  240. {
  241. struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
  242. struct irq_chip *chip = irq_desc_get_chip(desc);
  243. void __iomem *claim = handler->hart_base + CONTEXT_CLAIM;
  244. irq_hw_number_t hwirq;
  245. WARN_ON_ONCE(!handler->present);
  246. chained_irq_enter(chip, desc);
  247. while ((hwirq = readl(claim))) {
  248. int err = generic_handle_domain_irq(handler->priv->irqdomain,
  249. hwirq);
  250. if (unlikely(err))
  251. pr_warn_ratelimited("can't find mapping for hwirq %lu\n",
  252. hwirq);
  253. }
  254. chained_irq_exit(chip, desc);
  255. }
  256. static void plic_set_threshold(struct plic_handler *handler, u32 threshold)
  257. {
  258. /* priority must be > threshold to trigger an interrupt */
  259. writel(threshold, handler->hart_base + CONTEXT_THRESHOLD);
  260. }
  261. static int plic_dying_cpu(unsigned int cpu)
  262. {
  263. if (plic_parent_irq)
  264. disable_percpu_irq(plic_parent_irq);
  265. return 0;
  266. }
  267. static int plic_starting_cpu(unsigned int cpu)
  268. {
  269. struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
  270. if (plic_parent_irq)
  271. enable_percpu_irq(plic_parent_irq,
  272. irq_get_trigger_type(plic_parent_irq));
  273. else
  274. pr_warn("cpu%d: parent irq not available\n", cpu);
  275. plic_set_threshold(handler, PLIC_ENABLE_THRESHOLD);
  276. return 0;
  277. }
  278. static int __init __plic_init(struct device_node *node,
  279. struct device_node *parent,
  280. unsigned long plic_quirks)
  281. {
  282. int error = 0, nr_contexts, nr_handlers = 0, i;
  283. u32 nr_irqs;
  284. struct plic_priv *priv;
  285. struct plic_handler *handler;
  286. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  287. if (!priv)
  288. return -ENOMEM;
  289. priv->plic_quirks = plic_quirks;
  290. priv->regs = of_iomap(node, 0);
  291. if (WARN_ON(!priv->regs)) {
  292. error = -EIO;
  293. goto out_free_priv;
  294. }
  295. error = -EINVAL;
  296. of_property_read_u32(node, "riscv,ndev", &nr_irqs);
  297. if (WARN_ON(!nr_irqs))
  298. goto out_iounmap;
  299. nr_contexts = of_irq_count(node);
  300. if (WARN_ON(!nr_contexts))
  301. goto out_iounmap;
  302. error = -ENOMEM;
  303. priv->irqdomain = irq_domain_add_linear(node, nr_irqs + 1,
  304. &plic_irqdomain_ops, priv);
  305. if (WARN_ON(!priv->irqdomain))
  306. goto out_iounmap;
  307. for (i = 0; i < nr_contexts; i++) {
  308. struct of_phandle_args parent;
  309. irq_hw_number_t hwirq;
  310. int cpu;
  311. unsigned long hartid;
  312. if (of_irq_parse_one(node, i, &parent)) {
  313. pr_err("failed to parse parent for context %d.\n", i);
  314. continue;
  315. }
  316. /*
  317. * Skip contexts other than external interrupts for our
  318. * privilege level.
  319. */
  320. if (parent.args[0] != RV_IRQ_EXT) {
  321. /* Disable S-mode enable bits if running in M-mode. */
  322. if (IS_ENABLED(CONFIG_RISCV_M_MODE)) {
  323. void __iomem *enable_base = priv->regs +
  324. CONTEXT_ENABLE_BASE +
  325. i * CONTEXT_ENABLE_SIZE;
  326. for (hwirq = 1; hwirq <= nr_irqs; hwirq++)
  327. __plic_toggle(enable_base, hwirq, 0);
  328. }
  329. continue;
  330. }
  331. error = riscv_of_parent_hartid(parent.np, &hartid);
  332. if (error < 0) {
  333. pr_warn("failed to parse hart ID for context %d.\n", i);
  334. continue;
  335. }
  336. cpu = riscv_hartid_to_cpuid(hartid);
  337. if (cpu < 0) {
  338. pr_warn("Invalid cpuid for context %d\n", i);
  339. continue;
  340. }
  341. /* Find parent domain and register chained handler */
  342. if (!plic_parent_irq && irq_find_host(parent.np)) {
  343. plic_parent_irq = irq_of_parse_and_map(node, i);
  344. if (plic_parent_irq)
  345. irq_set_chained_handler(plic_parent_irq,
  346. plic_handle_irq);
  347. }
  348. /*
  349. * When running in M-mode we need to ignore the S-mode handler.
  350. * Here we assume it always comes later, but that might be a
  351. * little fragile.
  352. */
  353. handler = per_cpu_ptr(&plic_handlers, cpu);
  354. if (handler->present) {
  355. pr_warn("handler already present for context %d.\n", i);
  356. plic_set_threshold(handler, PLIC_DISABLE_THRESHOLD);
  357. goto done;
  358. }
  359. cpumask_set_cpu(cpu, &priv->lmask);
  360. handler->present = true;
  361. handler->hart_base = priv->regs + CONTEXT_BASE +
  362. i * CONTEXT_SIZE;
  363. raw_spin_lock_init(&handler->enable_lock);
  364. handler->enable_base = priv->regs + CONTEXT_ENABLE_BASE +
  365. i * CONTEXT_ENABLE_SIZE;
  366. handler->priv = priv;
  367. done:
  368. for (hwirq = 1; hwirq <= nr_irqs; hwirq++) {
  369. plic_toggle(handler, hwirq, 0);
  370. writel(1, priv->regs + PRIORITY_BASE +
  371. hwirq * PRIORITY_PER_ID);
  372. }
  373. nr_handlers++;
  374. }
  375. /*
  376. * We can have multiple PLIC instances so setup cpuhp state only
  377. * when context handler for current/boot CPU is present.
  378. */
  379. handler = this_cpu_ptr(&plic_handlers);
  380. if (handler->present && !plic_cpuhp_setup_done) {
  381. cpuhp_setup_state(CPUHP_AP_IRQ_SIFIVE_PLIC_STARTING,
  382. "irqchip/sifive/plic:starting",
  383. plic_starting_cpu, plic_dying_cpu);
  384. plic_cpuhp_setup_done = true;
  385. }
  386. pr_info("%pOFP: mapped %d interrupts with %d handlers for"
  387. " %d contexts.\n", node, nr_irqs, nr_handlers, nr_contexts);
  388. return 0;
  389. out_iounmap:
  390. iounmap(priv->regs);
  391. out_free_priv:
  392. kfree(priv);
  393. return error;
  394. }
  395. static int __init plic_init(struct device_node *node,
  396. struct device_node *parent)
  397. {
  398. return __plic_init(node, parent, 0);
  399. }
  400. IRQCHIP_DECLARE(sifive_plic, "sifive,plic-1.0.0", plic_init);
  401. IRQCHIP_DECLARE(riscv_plic0, "riscv,plic0", plic_init); /* for legacy systems */
  402. static int __init plic_edge_init(struct device_node *node,
  403. struct device_node *parent)
  404. {
  405. return __plic_init(node, parent, BIT(PLIC_QUIRK_EDGE_INTERRUPT));
  406. }
  407. IRQCHIP_DECLARE(andestech_nceplic100, "andestech,nceplic100", plic_edge_init);
  408. IRQCHIP_DECLARE(thead_c900_plic, "thead,c900-plic", plic_edge_init);