irq-bcm2836.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Root interrupt controller for the BCM2836 (Raspberry Pi 2).
  4. *
  5. * Copyright 2015 Broadcom
  6. */
  7. #include <linux/cpu.h>
  8. #include <linux/of_address.h>
  9. #include <linux/of_irq.h>
  10. #include <linux/irqchip.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/irqchip/chained_irq.h>
  13. #include <linux/irqchip/irq-bcm2836.h>
  14. #include <asm/exception.h>
  15. struct bcm2836_arm_irqchip_intc {
  16. struct irq_domain *domain;
  17. void __iomem *base;
  18. };
  19. static struct bcm2836_arm_irqchip_intc intc __read_mostly;
  20. static void bcm2836_arm_irqchip_mask_per_cpu_irq(unsigned int reg_offset,
  21. unsigned int bit,
  22. int cpu)
  23. {
  24. void __iomem *reg = intc.base + reg_offset + 4 * cpu;
  25. writel(readl(reg) & ~BIT(bit), reg);
  26. }
  27. static void bcm2836_arm_irqchip_unmask_per_cpu_irq(unsigned int reg_offset,
  28. unsigned int bit,
  29. int cpu)
  30. {
  31. void __iomem *reg = intc.base + reg_offset + 4 * cpu;
  32. writel(readl(reg) | BIT(bit), reg);
  33. }
  34. static void bcm2836_arm_irqchip_mask_timer_irq(struct irq_data *d)
  35. {
  36. bcm2836_arm_irqchip_mask_per_cpu_irq(LOCAL_TIMER_INT_CONTROL0,
  37. d->hwirq - LOCAL_IRQ_CNTPSIRQ,
  38. smp_processor_id());
  39. }
  40. static void bcm2836_arm_irqchip_unmask_timer_irq(struct irq_data *d)
  41. {
  42. bcm2836_arm_irqchip_unmask_per_cpu_irq(LOCAL_TIMER_INT_CONTROL0,
  43. d->hwirq - LOCAL_IRQ_CNTPSIRQ,
  44. smp_processor_id());
  45. }
  46. static struct irq_chip bcm2836_arm_irqchip_timer = {
  47. .name = "bcm2836-timer",
  48. .irq_mask = bcm2836_arm_irqchip_mask_timer_irq,
  49. .irq_unmask = bcm2836_arm_irqchip_unmask_timer_irq,
  50. .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_SKIP_SET_WAKE,
  51. };
  52. static void bcm2836_arm_irqchip_mask_pmu_irq(struct irq_data *d)
  53. {
  54. writel(1 << smp_processor_id(), intc.base + LOCAL_PM_ROUTING_CLR);
  55. }
  56. static void bcm2836_arm_irqchip_unmask_pmu_irq(struct irq_data *d)
  57. {
  58. writel(1 << smp_processor_id(), intc.base + LOCAL_PM_ROUTING_SET);
  59. }
  60. static struct irq_chip bcm2836_arm_irqchip_pmu = {
  61. .name = "bcm2836-pmu",
  62. .irq_mask = bcm2836_arm_irqchip_mask_pmu_irq,
  63. .irq_unmask = bcm2836_arm_irqchip_unmask_pmu_irq,
  64. .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_SKIP_SET_WAKE,
  65. };
  66. static void bcm2836_arm_irqchip_mask_gpu_irq(struct irq_data *d)
  67. {
  68. }
  69. static void bcm2836_arm_irqchip_unmask_gpu_irq(struct irq_data *d)
  70. {
  71. }
  72. static struct irq_chip bcm2836_arm_irqchip_gpu = {
  73. .name = "bcm2836-gpu",
  74. .irq_mask = bcm2836_arm_irqchip_mask_gpu_irq,
  75. .irq_unmask = bcm2836_arm_irqchip_unmask_gpu_irq,
  76. .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_SKIP_SET_WAKE,
  77. };
  78. static void bcm2836_arm_irqchip_dummy_op(struct irq_data *d)
  79. {
  80. }
  81. static struct irq_chip bcm2836_arm_irqchip_dummy = {
  82. .name = "bcm2836-dummy",
  83. .irq_eoi = bcm2836_arm_irqchip_dummy_op,
  84. };
  85. static int bcm2836_map(struct irq_domain *d, unsigned int irq,
  86. irq_hw_number_t hw)
  87. {
  88. struct irq_chip *chip;
  89. switch (hw) {
  90. case LOCAL_IRQ_MAILBOX0:
  91. chip = &bcm2836_arm_irqchip_dummy;
  92. break;
  93. case LOCAL_IRQ_CNTPSIRQ:
  94. case LOCAL_IRQ_CNTPNSIRQ:
  95. case LOCAL_IRQ_CNTHPIRQ:
  96. case LOCAL_IRQ_CNTVIRQ:
  97. chip = &bcm2836_arm_irqchip_timer;
  98. break;
  99. case LOCAL_IRQ_GPU_FAST:
  100. chip = &bcm2836_arm_irqchip_gpu;
  101. break;
  102. case LOCAL_IRQ_PMU_FAST:
  103. chip = &bcm2836_arm_irqchip_pmu;
  104. break;
  105. default:
  106. pr_warn_once("Unexpected hw irq: %lu\n", hw);
  107. return -EINVAL;
  108. }
  109. irq_set_percpu_devid(irq);
  110. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  111. handle_percpu_devid_irq, NULL, NULL);
  112. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  113. return 0;
  114. }
  115. static void
  116. __exception_irq_entry bcm2836_arm_irqchip_handle_irq(struct pt_regs *regs)
  117. {
  118. int cpu = smp_processor_id();
  119. u32 stat;
  120. stat = readl_relaxed(intc.base + LOCAL_IRQ_PENDING0 + 4 * cpu);
  121. if (stat) {
  122. u32 hwirq = ffs(stat) - 1;
  123. generic_handle_domain_irq(intc.domain, hwirq);
  124. }
  125. }
  126. #ifdef CONFIG_SMP
  127. static struct irq_domain *ipi_domain;
  128. static void bcm2836_arm_irqchip_handle_ipi(struct irq_desc *desc)
  129. {
  130. struct irq_chip *chip = irq_desc_get_chip(desc);
  131. int cpu = smp_processor_id();
  132. u32 mbox_val;
  133. chained_irq_enter(chip, desc);
  134. mbox_val = readl_relaxed(intc.base + LOCAL_MAILBOX0_CLR0 + 16 * cpu);
  135. if (mbox_val) {
  136. int hwirq = ffs(mbox_val) - 1;
  137. generic_handle_domain_irq(ipi_domain, hwirq);
  138. }
  139. chained_irq_exit(chip, desc);
  140. }
  141. static void bcm2836_arm_irqchip_ipi_ack(struct irq_data *d)
  142. {
  143. int cpu = smp_processor_id();
  144. writel_relaxed(BIT(d->hwirq),
  145. intc.base + LOCAL_MAILBOX0_CLR0 + 16 * cpu);
  146. }
  147. static void bcm2836_arm_irqchip_ipi_send_mask(struct irq_data *d,
  148. const struct cpumask *mask)
  149. {
  150. int cpu;
  151. void __iomem *mailbox0_base = intc.base + LOCAL_MAILBOX0_SET0;
  152. /*
  153. * Ensure that stores to normal memory are visible to the
  154. * other CPUs before issuing the IPI.
  155. */
  156. smp_wmb();
  157. for_each_cpu(cpu, mask)
  158. writel_relaxed(BIT(d->hwirq), mailbox0_base + 16 * cpu);
  159. }
  160. static struct irq_chip bcm2836_arm_irqchip_ipi = {
  161. .name = "IPI",
  162. .irq_mask = bcm2836_arm_irqchip_dummy_op,
  163. .irq_unmask = bcm2836_arm_irqchip_dummy_op,
  164. .irq_ack = bcm2836_arm_irqchip_ipi_ack,
  165. .ipi_send_mask = bcm2836_arm_irqchip_ipi_send_mask,
  166. };
  167. static int bcm2836_arm_irqchip_ipi_alloc(struct irq_domain *d,
  168. unsigned int virq,
  169. unsigned int nr_irqs, void *args)
  170. {
  171. int i;
  172. for (i = 0; i < nr_irqs; i++) {
  173. irq_set_percpu_devid(virq + i);
  174. irq_domain_set_info(d, virq + i, i, &bcm2836_arm_irqchip_ipi,
  175. d->host_data,
  176. handle_percpu_devid_irq,
  177. NULL, NULL);
  178. }
  179. return 0;
  180. }
  181. static void bcm2836_arm_irqchip_ipi_free(struct irq_domain *d,
  182. unsigned int virq,
  183. unsigned int nr_irqs)
  184. {
  185. /* Not freeing IPIs */
  186. }
  187. static const struct irq_domain_ops ipi_domain_ops = {
  188. .alloc = bcm2836_arm_irqchip_ipi_alloc,
  189. .free = bcm2836_arm_irqchip_ipi_free,
  190. };
  191. static int bcm2836_cpu_starting(unsigned int cpu)
  192. {
  193. bcm2836_arm_irqchip_unmask_per_cpu_irq(LOCAL_MAILBOX_INT_CONTROL0, 0,
  194. cpu);
  195. return 0;
  196. }
  197. static int bcm2836_cpu_dying(unsigned int cpu)
  198. {
  199. bcm2836_arm_irqchip_mask_per_cpu_irq(LOCAL_MAILBOX_INT_CONTROL0, 0,
  200. cpu);
  201. return 0;
  202. }
  203. #define BITS_PER_MBOX 32
  204. static void __init bcm2836_arm_irqchip_smp_init(void)
  205. {
  206. struct irq_fwspec ipi_fwspec = {
  207. .fwnode = intc.domain->fwnode,
  208. .param_count = 1,
  209. .param = {
  210. [0] = LOCAL_IRQ_MAILBOX0,
  211. },
  212. };
  213. int base_ipi, mux_irq;
  214. mux_irq = irq_create_fwspec_mapping(&ipi_fwspec);
  215. if (WARN_ON(mux_irq <= 0))
  216. return;
  217. ipi_domain = irq_domain_create_linear(intc.domain->fwnode,
  218. BITS_PER_MBOX, &ipi_domain_ops,
  219. NULL);
  220. if (WARN_ON(!ipi_domain))
  221. return;
  222. ipi_domain->flags |= IRQ_DOMAIN_FLAG_IPI_SINGLE;
  223. irq_domain_update_bus_token(ipi_domain, DOMAIN_BUS_IPI);
  224. base_ipi = irq_domain_alloc_irqs(ipi_domain, BITS_PER_MBOX, NUMA_NO_NODE, NULL);
  225. if (WARN_ON(!base_ipi))
  226. return;
  227. set_smp_ipi_range(base_ipi, BITS_PER_MBOX);
  228. irq_set_chained_handler_and_data(mux_irq,
  229. bcm2836_arm_irqchip_handle_ipi, NULL);
  230. /* Unmask IPIs to the boot CPU. */
  231. cpuhp_setup_state(CPUHP_AP_IRQ_BCM2836_STARTING,
  232. "irqchip/bcm2836:starting", bcm2836_cpu_starting,
  233. bcm2836_cpu_dying);
  234. }
  235. #else
  236. #define bcm2836_arm_irqchip_smp_init() do { } while(0)
  237. #endif
  238. static const struct irq_domain_ops bcm2836_arm_irqchip_intc_ops = {
  239. .xlate = irq_domain_xlate_onetwocell,
  240. .map = bcm2836_map,
  241. };
  242. /*
  243. * The LOCAL_IRQ_CNT* timer firings are based off of the external
  244. * oscillator with some scaling. The firmware sets up CNTFRQ to
  245. * report 19.2Mhz, but doesn't set up the scaling registers.
  246. */
  247. static void bcm2835_init_local_timer_frequency(void)
  248. {
  249. /*
  250. * Set the timer to source from the 19.2Mhz crystal clock (bit
  251. * 8 unset), and only increment by 1 instead of 2 (bit 9
  252. * unset).
  253. */
  254. writel(0, intc.base + LOCAL_CONTROL);
  255. /*
  256. * Set the timer prescaler to 1:1 (timer freq = input freq *
  257. * 2**31 / prescaler)
  258. */
  259. writel(0x80000000, intc.base + LOCAL_PRESCALER);
  260. }
  261. static int __init bcm2836_arm_irqchip_l1_intc_of_init(struct device_node *node,
  262. struct device_node *parent)
  263. {
  264. intc.base = of_iomap(node, 0);
  265. if (!intc.base) {
  266. panic("%pOF: unable to map local interrupt registers\n", node);
  267. }
  268. bcm2835_init_local_timer_frequency();
  269. intc.domain = irq_domain_create_linear(of_fwnode_handle(node), LAST_IRQ + 1,
  270. &bcm2836_arm_irqchip_intc_ops,
  271. NULL);
  272. if (!intc.domain)
  273. panic("%pOF: unable to create IRQ domain\n", node);
  274. irq_domain_update_bus_token(intc.domain, DOMAIN_BUS_WIRED);
  275. bcm2836_arm_irqchip_smp_init();
  276. set_handle_irq(bcm2836_arm_irqchip_handle_irq);
  277. return 0;
  278. }
  279. IRQCHIP_DECLARE(bcm2836_arm_irqchip_l1_intc, "brcm,bcm2836-l1-intc",
  280. bcm2836_arm_irqchip_l1_intc_of_init);