irq-armada-370-xp.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Marvell Armada 370 and Armada XP SoC IRQ handling
  3. *
  4. * Copyright (C) 2012 Marvell
  5. *
  6. * Lior Amsalem <alior@marvell.com>
  7. * Gregory CLEMENT <gregory.clement@free-electrons.com>
  8. * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  9. * Ben Dooks <ben.dooks@codethink.co.uk>
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/irq.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/irqdomain.h>
  24. #include <asm/mach/arch.h>
  25. #include <asm/exception.h>
  26. #include <asm/smp_plat.h>
  27. #include <asm/hardware/cache-l2x0.h>
  28. /* Interrupt Controller Registers Map */
  29. #define ARMADA_370_XP_INT_SET_MASK_OFFS (0x48)
  30. #define ARMADA_370_XP_INT_CLEAR_MASK_OFFS (0x4C)
  31. #define ARMADA_370_XP_INT_CONTROL (0x00)
  32. #define ARMADA_370_XP_INT_SET_ENABLE_OFFS (0x30)
  33. #define ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS (0x34)
  34. #define ARMADA_370_XP_INT_SOURCE_CTL(irq) (0x100 + irq*4)
  35. #define ARMADA_370_XP_CPU_INTACK_OFFS (0x44)
  36. #define ARMADA_370_XP_SW_TRIG_INT_OFFS (0x4)
  37. #define ARMADA_370_XP_IN_DRBEL_MSK_OFFS (0xc)
  38. #define ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS (0x8)
  39. #define ARMADA_370_XP_MAX_PER_CPU_IRQS (28)
  40. #define ARMADA_370_XP_TIMER0_PER_CPU_IRQ (5)
  41. #define ACTIVE_DOORBELLS (8)
  42. static DEFINE_RAW_SPINLOCK(irq_controller_lock);
  43. static void __iomem *per_cpu_int_base;
  44. static void __iomem *main_int_base;
  45. static struct irq_domain *armada_370_xp_mpic_domain;
  46. /*
  47. * In SMP mode:
  48. * For shared global interrupts, mask/unmask global enable bit
  49. * For CPU interrtups, mask/unmask the calling CPU's bit
  50. */
  51. static void armada_370_xp_irq_mask(struct irq_data *d)
  52. {
  53. #ifdef CONFIG_SMP
  54. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  55. if (hwirq != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
  56. writel(hwirq, main_int_base +
  57. ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS);
  58. else
  59. writel(hwirq, per_cpu_int_base +
  60. ARMADA_370_XP_INT_SET_MASK_OFFS);
  61. #else
  62. writel(irqd_to_hwirq(d),
  63. per_cpu_int_base + ARMADA_370_XP_INT_SET_MASK_OFFS);
  64. #endif
  65. }
  66. static void armada_370_xp_irq_unmask(struct irq_data *d)
  67. {
  68. #ifdef CONFIG_SMP
  69. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  70. if (hwirq != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
  71. writel(hwirq, main_int_base +
  72. ARMADA_370_XP_INT_SET_ENABLE_OFFS);
  73. else
  74. writel(hwirq, per_cpu_int_base +
  75. ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  76. #else
  77. writel(irqd_to_hwirq(d),
  78. per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  79. #endif
  80. }
  81. #ifdef CONFIG_SMP
  82. static int armada_xp_set_affinity(struct irq_data *d,
  83. const struct cpumask *mask_val, bool force)
  84. {
  85. unsigned long reg;
  86. unsigned long new_mask = 0;
  87. unsigned long online_mask = 0;
  88. unsigned long count = 0;
  89. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  90. int cpu;
  91. for_each_cpu(cpu, mask_val) {
  92. new_mask |= 1 << cpu_logical_map(cpu);
  93. count++;
  94. }
  95. /*
  96. * Forbid mutlicore interrupt affinity
  97. * This is required since the MPIC HW doesn't limit
  98. * several CPUs from acknowledging the same interrupt.
  99. */
  100. if (count > 1)
  101. return -EINVAL;
  102. for_each_cpu(cpu, cpu_online_mask)
  103. online_mask |= 1 << cpu_logical_map(cpu);
  104. raw_spin_lock(&irq_controller_lock);
  105. reg = readl(main_int_base + ARMADA_370_XP_INT_SOURCE_CTL(hwirq));
  106. reg = (reg & (~online_mask)) | new_mask;
  107. writel(reg, main_int_base + ARMADA_370_XP_INT_SOURCE_CTL(hwirq));
  108. raw_spin_unlock(&irq_controller_lock);
  109. return 0;
  110. }
  111. #endif
  112. static struct irq_chip armada_370_xp_irq_chip = {
  113. .name = "armada_370_xp_irq",
  114. .irq_mask = armada_370_xp_irq_mask,
  115. .irq_mask_ack = armada_370_xp_irq_mask,
  116. .irq_unmask = armada_370_xp_irq_unmask,
  117. #ifdef CONFIG_SMP
  118. .irq_set_affinity = armada_xp_set_affinity,
  119. #endif
  120. };
  121. static int armada_370_xp_mpic_irq_map(struct irq_domain *h,
  122. unsigned int virq, irq_hw_number_t hw)
  123. {
  124. armada_370_xp_irq_mask(irq_get_irq_data(virq));
  125. writel(hw, main_int_base + ARMADA_370_XP_INT_SET_ENABLE_OFFS);
  126. irq_set_status_flags(virq, IRQ_LEVEL);
  127. if (hw == ARMADA_370_XP_TIMER0_PER_CPU_IRQ) {
  128. irq_set_percpu_devid(virq);
  129. irq_set_chip_and_handler(virq, &armada_370_xp_irq_chip,
  130. handle_percpu_devid_irq);
  131. } else {
  132. irq_set_chip_and_handler(virq, &armada_370_xp_irq_chip,
  133. handle_level_irq);
  134. }
  135. set_irq_flags(virq, IRQF_VALID | IRQF_PROBE);
  136. return 0;
  137. }
  138. #ifdef CONFIG_SMP
  139. void armada_mpic_send_doorbell(const struct cpumask *mask, unsigned int irq)
  140. {
  141. int cpu;
  142. unsigned long map = 0;
  143. /* Convert our logical CPU mask into a physical one. */
  144. for_each_cpu(cpu, mask)
  145. map |= 1 << cpu_logical_map(cpu);
  146. /*
  147. * Ensure that stores to Normal memory are visible to the
  148. * other CPUs before issuing the IPI.
  149. */
  150. dsb();
  151. /* submit softirq */
  152. writel((map << 8) | irq, main_int_base +
  153. ARMADA_370_XP_SW_TRIG_INT_OFFS);
  154. }
  155. void armada_xp_mpic_smp_cpu_init(void)
  156. {
  157. /* Clear pending IPIs */
  158. writel(0, per_cpu_int_base + ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
  159. /* Enable first 8 IPIs */
  160. writel((1 << ACTIVE_DOORBELLS) - 1, per_cpu_int_base +
  161. ARMADA_370_XP_IN_DRBEL_MSK_OFFS);
  162. /* Unmask IPI interrupt */
  163. writel(0, per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  164. }
  165. #endif /* CONFIG_SMP */
  166. static struct irq_domain_ops armada_370_xp_mpic_irq_ops = {
  167. .map = armada_370_xp_mpic_irq_map,
  168. .xlate = irq_domain_xlate_onecell,
  169. };
  170. static int __init armada_370_xp_mpic_of_init(struct device_node *node,
  171. struct device_node *parent)
  172. {
  173. u32 control;
  174. main_int_base = of_iomap(node, 0);
  175. per_cpu_int_base = of_iomap(node, 1);
  176. BUG_ON(!main_int_base);
  177. BUG_ON(!per_cpu_int_base);
  178. control = readl(main_int_base + ARMADA_370_XP_INT_CONTROL);
  179. armada_370_xp_mpic_domain =
  180. irq_domain_add_linear(node, (control >> 2) & 0x3ff,
  181. &armada_370_xp_mpic_irq_ops, NULL);
  182. if (!armada_370_xp_mpic_domain)
  183. panic("Unable to add Armada_370_Xp MPIC irq domain (DT)\n");
  184. irq_set_default_host(armada_370_xp_mpic_domain);
  185. #ifdef CONFIG_SMP
  186. armada_xp_mpic_smp_cpu_init();
  187. /*
  188. * Set the default affinity from all CPUs to the boot cpu.
  189. * This is required since the MPIC doesn't limit several CPUs
  190. * from acknowledging the same interrupt.
  191. */
  192. cpumask_clear(irq_default_affinity);
  193. cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
  194. #endif
  195. return 0;
  196. }
  197. asmlinkage void __exception_irq_entry armada_370_xp_handle_irq(struct pt_regs
  198. *regs)
  199. {
  200. u32 irqstat, irqnr;
  201. do {
  202. irqstat = readl_relaxed(per_cpu_int_base +
  203. ARMADA_370_XP_CPU_INTACK_OFFS);
  204. irqnr = irqstat & 0x3FF;
  205. if (irqnr > 1022)
  206. break;
  207. if (irqnr > 0) {
  208. irqnr = irq_find_mapping(armada_370_xp_mpic_domain,
  209. irqnr);
  210. handle_IRQ(irqnr, regs);
  211. continue;
  212. }
  213. #ifdef CONFIG_SMP
  214. /* IPI Handling */
  215. if (irqnr == 0) {
  216. u32 ipimask, ipinr;
  217. ipimask = readl_relaxed(per_cpu_int_base +
  218. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS)
  219. & 0xFF;
  220. writel(0x0, per_cpu_int_base +
  221. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
  222. /* Handle all pending doorbells */
  223. for (ipinr = 0; ipinr < ACTIVE_DOORBELLS; ipinr++) {
  224. if (ipimask & (0x1 << ipinr))
  225. handle_IPI(ipinr, regs);
  226. }
  227. continue;
  228. }
  229. #endif
  230. } while (1);
  231. }
  232. static const struct of_device_id mpic_of_match[] __initconst = {
  233. {.compatible = "marvell,mpic", .data = armada_370_xp_mpic_of_init},
  234. {},
  235. };
  236. void __init armada_370_xp_init_irq(void)
  237. {
  238. of_irq_init(mpic_of_match);
  239. #ifdef CONFIG_CACHE_L2X0
  240. l2x0_of_init(0, ~0UL);
  241. #endif
  242. }