irq-armada-370-xp.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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/mach/irq.h>
  28. #include "irqchip.h"
  29. /* Interrupt Controller Registers Map */
  30. #define ARMADA_370_XP_INT_SET_MASK_OFFS (0x48)
  31. #define ARMADA_370_XP_INT_CLEAR_MASK_OFFS (0x4C)
  32. #define ARMADA_370_XP_INT_CONTROL (0x00)
  33. #define ARMADA_370_XP_INT_SET_ENABLE_OFFS (0x30)
  34. #define ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS (0x34)
  35. #define ARMADA_370_XP_INT_SOURCE_CTL(irq) (0x100 + irq*4)
  36. #define ARMADA_370_XP_CPU_INTACK_OFFS (0x44)
  37. #define ARMADA_370_XP_SW_TRIG_INT_OFFS (0x4)
  38. #define ARMADA_370_XP_IN_DRBEL_MSK_OFFS (0xc)
  39. #define ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS (0x8)
  40. #define ARMADA_370_XP_MAX_PER_CPU_IRQS (28)
  41. #define ARMADA_370_XP_TIMER0_PER_CPU_IRQ (5)
  42. #define IPI_DOORBELL_START (0)
  43. #define IPI_DOORBELL_END (8)
  44. #define IPI_DOORBELL_MASK 0xFF
  45. static DEFINE_RAW_SPINLOCK(irq_controller_lock);
  46. static void __iomem *per_cpu_int_base;
  47. static void __iomem *main_int_base;
  48. static struct irq_domain *armada_370_xp_mpic_domain;
  49. /*
  50. * In SMP mode:
  51. * For shared global interrupts, mask/unmask global enable bit
  52. * For CPU interrupts, mask/unmask the calling CPU's bit
  53. */
  54. static void armada_370_xp_irq_mask(struct irq_data *d)
  55. {
  56. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  57. if (hwirq != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
  58. writel(hwirq, main_int_base +
  59. ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS);
  60. else
  61. writel(hwirq, per_cpu_int_base +
  62. ARMADA_370_XP_INT_SET_MASK_OFFS);
  63. }
  64. static void armada_370_xp_irq_unmask(struct irq_data *d)
  65. {
  66. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  67. if (hwirq != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
  68. writel(hwirq, main_int_base +
  69. ARMADA_370_XP_INT_SET_ENABLE_OFFS);
  70. else
  71. writel(hwirq, per_cpu_int_base +
  72. ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  73. }
  74. #ifdef CONFIG_SMP
  75. static int armada_xp_set_affinity(struct irq_data *d,
  76. const struct cpumask *mask_val, bool force)
  77. {
  78. unsigned long reg;
  79. unsigned long new_mask = 0;
  80. unsigned long online_mask = 0;
  81. unsigned long count = 0;
  82. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  83. int cpu;
  84. for_each_cpu(cpu, mask_val) {
  85. new_mask |= 1 << cpu_logical_map(cpu);
  86. count++;
  87. }
  88. /*
  89. * Forbid mutlicore interrupt affinity
  90. * This is required since the MPIC HW doesn't limit
  91. * several CPUs from acknowledging the same interrupt.
  92. */
  93. if (count > 1)
  94. return -EINVAL;
  95. for_each_cpu(cpu, cpu_online_mask)
  96. online_mask |= 1 << cpu_logical_map(cpu);
  97. raw_spin_lock(&irq_controller_lock);
  98. reg = readl(main_int_base + ARMADA_370_XP_INT_SOURCE_CTL(hwirq));
  99. reg = (reg & (~online_mask)) | new_mask;
  100. writel(reg, main_int_base + ARMADA_370_XP_INT_SOURCE_CTL(hwirq));
  101. raw_spin_unlock(&irq_controller_lock);
  102. return 0;
  103. }
  104. #endif
  105. static struct irq_chip armada_370_xp_irq_chip = {
  106. .name = "armada_370_xp_irq",
  107. .irq_mask = armada_370_xp_irq_mask,
  108. .irq_mask_ack = armada_370_xp_irq_mask,
  109. .irq_unmask = armada_370_xp_irq_unmask,
  110. #ifdef CONFIG_SMP
  111. .irq_set_affinity = armada_xp_set_affinity,
  112. #endif
  113. };
  114. static int armada_370_xp_mpic_irq_map(struct irq_domain *h,
  115. unsigned int virq, irq_hw_number_t hw)
  116. {
  117. armada_370_xp_irq_mask(irq_get_irq_data(virq));
  118. if (hw != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
  119. writel(hw, per_cpu_int_base +
  120. ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  121. else
  122. writel(hw, main_int_base + ARMADA_370_XP_INT_SET_ENABLE_OFFS);
  123. irq_set_status_flags(virq, IRQ_LEVEL);
  124. if (hw == ARMADA_370_XP_TIMER0_PER_CPU_IRQ) {
  125. irq_set_percpu_devid(virq);
  126. irq_set_chip_and_handler(virq, &armada_370_xp_irq_chip,
  127. handle_percpu_devid_irq);
  128. } else {
  129. irq_set_chip_and_handler(virq, &armada_370_xp_irq_chip,
  130. handle_level_irq);
  131. }
  132. set_irq_flags(virq, IRQF_VALID | IRQF_PROBE);
  133. return 0;
  134. }
  135. #ifdef CONFIG_SMP
  136. void armada_mpic_send_doorbell(const struct cpumask *mask, unsigned int irq)
  137. {
  138. int cpu;
  139. unsigned long map = 0;
  140. /* Convert our logical CPU mask into a physical one. */
  141. for_each_cpu(cpu, mask)
  142. map |= 1 << cpu_logical_map(cpu);
  143. /*
  144. * Ensure that stores to Normal memory are visible to the
  145. * other CPUs before issuing the IPI.
  146. */
  147. dsb();
  148. /* submit softirq */
  149. writel((map << 8) | irq, main_int_base +
  150. ARMADA_370_XP_SW_TRIG_INT_OFFS);
  151. }
  152. void armada_xp_mpic_smp_cpu_init(void)
  153. {
  154. /* Clear pending IPIs */
  155. writel(0, per_cpu_int_base + ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
  156. /* Enable first 8 IPIs */
  157. writel(IPI_DOORBELL_MASK, per_cpu_int_base +
  158. ARMADA_370_XP_IN_DRBEL_MSK_OFFS);
  159. /* Unmask IPI interrupt */
  160. writel(0, per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  161. }
  162. #endif /* CONFIG_SMP */
  163. static struct irq_domain_ops armada_370_xp_mpic_irq_ops = {
  164. .map = armada_370_xp_mpic_irq_map,
  165. .xlate = irq_domain_xlate_onecell,
  166. };
  167. static asmlinkage void __exception_irq_entry
  168. armada_370_xp_handle_irq(struct pt_regs *regs)
  169. {
  170. u32 irqstat, irqnr;
  171. do {
  172. irqstat = readl_relaxed(per_cpu_int_base +
  173. ARMADA_370_XP_CPU_INTACK_OFFS);
  174. irqnr = irqstat & 0x3FF;
  175. if (irqnr > 1022)
  176. break;
  177. if (irqnr > 0) {
  178. irqnr = irq_find_mapping(armada_370_xp_mpic_domain,
  179. irqnr);
  180. handle_IRQ(irqnr, regs);
  181. continue;
  182. }
  183. #ifdef CONFIG_SMP
  184. /* IPI Handling */
  185. if (irqnr == 0) {
  186. u32 ipimask, ipinr;
  187. ipimask = readl_relaxed(per_cpu_int_base +
  188. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS)
  189. & IPI_DOORBELL_MASK;
  190. writel(~IPI_DOORBELL_MASK, per_cpu_int_base +
  191. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
  192. /* Handle all pending doorbells */
  193. for (ipinr = IPI_DOORBELL_START;
  194. ipinr < IPI_DOORBELL_END; ipinr++) {
  195. if (ipimask & (0x1 << ipinr))
  196. handle_IPI(ipinr, regs);
  197. }
  198. continue;
  199. }
  200. #endif
  201. } while (1);
  202. }
  203. static int __init armada_370_xp_mpic_of_init(struct device_node *node,
  204. struct device_node *parent)
  205. {
  206. u32 control;
  207. main_int_base = of_iomap(node, 0);
  208. per_cpu_int_base = of_iomap(node, 1);
  209. BUG_ON(!main_int_base);
  210. BUG_ON(!per_cpu_int_base);
  211. control = readl(main_int_base + ARMADA_370_XP_INT_CONTROL);
  212. armada_370_xp_mpic_domain =
  213. irq_domain_add_linear(node, (control >> 2) & 0x3ff,
  214. &armada_370_xp_mpic_irq_ops, NULL);
  215. if (!armada_370_xp_mpic_domain)
  216. panic("Unable to add Armada_370_Xp MPIC irq domain (DT)\n");
  217. irq_set_default_host(armada_370_xp_mpic_domain);
  218. #ifdef CONFIG_SMP
  219. armada_xp_mpic_smp_cpu_init();
  220. /*
  221. * Set the default affinity from all CPUs to the boot cpu.
  222. * This is required since the MPIC doesn't limit several CPUs
  223. * from acknowledging the same interrupt.
  224. */
  225. cpumask_clear(irq_default_affinity);
  226. cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
  227. #endif
  228. set_handle_irq(armada_370_xp_handle_irq);
  229. return 0;
  230. }
  231. IRQCHIP_DECLARE(armada_370_xp_mpic, "marvell,mpic", armada_370_xp_mpic_of_init);