irq-sifive-plic.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/interrupt.h>
  8. #include <linux/io.h>
  9. #include <linux/irq.h>
  10. #include <linux/irqchip.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/spinlock.h>
  18. #include <asm/smp.h>
  19. /*
  20. * This driver implements a version of the RISC-V PLIC with the actual layout
  21. * specified in chapter 8 of the SiFive U5 Coreplex Series Manual:
  22. *
  23. * https://static.dev.sifive.com/U54-MC-RVCoreIP.pdf
  24. *
  25. * The largest number supported by devices marked as 'sifive,plic-1.0.0', is
  26. * 1024, of which device 0 is defined as non-existent by the RISC-V Privileged
  27. * Spec.
  28. */
  29. #define MAX_DEVICES 1024
  30. #define MAX_CONTEXTS 15872
  31. /*
  32. * Each interrupt source has a priority register associated with it.
  33. * We always hardwire it to one in Linux.
  34. */
  35. #define PRIORITY_BASE 0
  36. #define PRIORITY_PER_ID 4
  37. /*
  38. * Each hart context has a vector of interrupt enable bits associated with it.
  39. * There's one bit for each interrupt source.
  40. */
  41. #define ENABLE_BASE 0x2000
  42. #define ENABLE_PER_HART 0x80
  43. /*
  44. * Each hart context has a set of control registers associated with it. Right
  45. * now there's only two: a source priority threshold over which the hart will
  46. * take an interrupt, and a register to claim interrupts.
  47. */
  48. #define CONTEXT_BASE 0x200000
  49. #define CONTEXT_PER_HART 0x1000
  50. #define CONTEXT_THRESHOLD 0x00
  51. #define CONTEXT_CLAIM 0x04
  52. static void __iomem *plic_regs;
  53. struct plic_handler {
  54. bool present;
  55. int ctxid;
  56. };
  57. static DEFINE_PER_CPU(struct plic_handler, plic_handlers);
  58. static inline void __iomem *plic_hart_offset(int ctxid)
  59. {
  60. return plic_regs + CONTEXT_BASE + ctxid * CONTEXT_PER_HART;
  61. }
  62. static inline u32 __iomem *plic_enable_base(int ctxid)
  63. {
  64. return plic_regs + ENABLE_BASE + ctxid * ENABLE_PER_HART;
  65. }
  66. /*
  67. * Protect mask operations on the registers given that we can't assume that
  68. * atomic memory operations work on them.
  69. */
  70. static DEFINE_RAW_SPINLOCK(plic_toggle_lock);
  71. static inline void plic_toggle(int ctxid, int hwirq, int enable)
  72. {
  73. u32 __iomem *reg = plic_enable_base(ctxid) + (hwirq / 32);
  74. u32 hwirq_mask = 1 << (hwirq % 32);
  75. raw_spin_lock(&plic_toggle_lock);
  76. if (enable)
  77. writel(readl(reg) | hwirq_mask, reg);
  78. else
  79. writel(readl(reg) & ~hwirq_mask, reg);
  80. raw_spin_unlock(&plic_toggle_lock);
  81. }
  82. static inline void plic_irq_toggle(struct irq_data *d, int enable)
  83. {
  84. int cpu;
  85. writel(enable, plic_regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
  86. for_each_cpu(cpu, irq_data_get_affinity_mask(d)) {
  87. struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
  88. if (handler->present)
  89. plic_toggle(handler->ctxid, d->hwirq, enable);
  90. }
  91. }
  92. static void plic_irq_enable(struct irq_data *d)
  93. {
  94. plic_irq_toggle(d, 1);
  95. }
  96. static void plic_irq_disable(struct irq_data *d)
  97. {
  98. plic_irq_toggle(d, 0);
  99. }
  100. static struct irq_chip plic_chip = {
  101. .name = "SiFive PLIC",
  102. /*
  103. * There is no need to mask/unmask PLIC interrupts. They are "masked"
  104. * by reading claim and "unmasked" when writing it back.
  105. */
  106. .irq_enable = plic_irq_enable,
  107. .irq_disable = plic_irq_disable,
  108. };
  109. static int plic_irqdomain_map(struct irq_domain *d, unsigned int irq,
  110. irq_hw_number_t hwirq)
  111. {
  112. irq_set_chip_and_handler(irq, &plic_chip, handle_simple_irq);
  113. irq_set_chip_data(irq, NULL);
  114. irq_set_noprobe(irq);
  115. return 0;
  116. }
  117. static const struct irq_domain_ops plic_irqdomain_ops = {
  118. .map = plic_irqdomain_map,
  119. .xlate = irq_domain_xlate_onecell,
  120. };
  121. static struct irq_domain *plic_irqdomain;
  122. /*
  123. * Handling an interrupt is a two-step process: first you claim the interrupt
  124. * by reading the claim register, then you complete the interrupt by writing
  125. * that source ID back to the same claim register. This automatically enables
  126. * and disables the interrupt, so there's nothing else to do.
  127. */
  128. static void plic_handle_irq(struct pt_regs *regs)
  129. {
  130. struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
  131. void __iomem *claim = plic_hart_offset(handler->ctxid) + CONTEXT_CLAIM;
  132. irq_hw_number_t hwirq;
  133. WARN_ON_ONCE(!handler->present);
  134. csr_clear(sie, SIE_SEIE);
  135. while ((hwirq = readl(claim))) {
  136. int irq = irq_find_mapping(plic_irqdomain, hwirq);
  137. if (unlikely(irq <= 0))
  138. pr_warn_ratelimited("can't find mapping for hwirq %lu\n",
  139. hwirq);
  140. else
  141. generic_handle_irq(irq);
  142. writel(hwirq, claim);
  143. }
  144. csr_set(sie, SIE_SEIE);
  145. }
  146. /*
  147. * Walk up the DT tree until we find an active RISC-V core (HART) node and
  148. * extract the cpuid from it.
  149. */
  150. static int plic_find_hart_id(struct device_node *node)
  151. {
  152. for (; node; node = node->parent) {
  153. if (of_device_is_compatible(node, "riscv"))
  154. return riscv_of_processor_hartid(node);
  155. }
  156. return -1;
  157. }
  158. static int __init plic_init(struct device_node *node,
  159. struct device_node *parent)
  160. {
  161. int error = 0, nr_handlers, nr_mapped = 0, i;
  162. u32 nr_irqs;
  163. if (plic_regs) {
  164. pr_warn("PLIC already present.\n");
  165. return -ENXIO;
  166. }
  167. plic_regs = of_iomap(node, 0);
  168. if (WARN_ON(!plic_regs))
  169. return -EIO;
  170. error = -EINVAL;
  171. of_property_read_u32(node, "riscv,ndev", &nr_irqs);
  172. if (WARN_ON(!nr_irqs))
  173. goto out_iounmap;
  174. nr_handlers = of_irq_count(node);
  175. if (WARN_ON(!nr_handlers))
  176. goto out_iounmap;
  177. if (WARN_ON(nr_handlers < num_possible_cpus()))
  178. goto out_iounmap;
  179. error = -ENOMEM;
  180. plic_irqdomain = irq_domain_add_linear(node, nr_irqs + 1,
  181. &plic_irqdomain_ops, NULL);
  182. if (WARN_ON(!plic_irqdomain))
  183. goto out_iounmap;
  184. for (i = 0; i < nr_handlers; i++) {
  185. struct of_phandle_args parent;
  186. struct plic_handler *handler;
  187. irq_hw_number_t hwirq;
  188. int cpu, hartid;
  189. if (of_irq_parse_one(node, i, &parent)) {
  190. pr_err("failed to parse parent for context %d.\n", i);
  191. continue;
  192. }
  193. /* skip context holes */
  194. if (parent.args[0] == -1)
  195. continue;
  196. hartid = plic_find_hart_id(parent.np);
  197. if (hartid < 0) {
  198. pr_warn("failed to parse hart ID for context %d.\n", i);
  199. continue;
  200. }
  201. cpu = riscv_hartid_to_cpuid(hartid);
  202. handler = per_cpu_ptr(&plic_handlers, cpu);
  203. handler->present = true;
  204. handler->ctxid = i;
  205. /* priority must be > threshold to trigger an interrupt */
  206. writel(0, plic_hart_offset(i) + CONTEXT_THRESHOLD);
  207. for (hwirq = 1; hwirq <= nr_irqs; hwirq++)
  208. plic_toggle(i, hwirq, 0);
  209. nr_mapped++;
  210. }
  211. pr_info("mapped %d interrupts to %d (out of %d) handlers.\n",
  212. nr_irqs, nr_mapped, nr_handlers);
  213. set_handle_irq(plic_handle_irq);
  214. return 0;
  215. out_iounmap:
  216. iounmap(plic_regs);
  217. return error;
  218. }
  219. IRQCHIP_DECLARE(sifive_plic, "sifive,plic-1.0.0", plic_init);
  220. IRQCHIP_DECLARE(riscv_plic0, "riscv,plic0", plic_init); /* for legacy systems */