Steven J. Hill | 2299c49 | 2012-08-31 16:13:07 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * Copyright (C) 2008 Ralf Baechle (ralf@linux-mips.org) |
| 7 | * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved. |
| 8 | */ |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 9 | #include <linux/bitmap.h> |
Andrew Bresticker | fb8f7be1 | 2014-10-20 12:03:55 -0700 | [diff] [blame] | 10 | #include <linux/clocksource.h> |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 11 | #include <linux/init.h> |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 12 | #include <linux/interrupt.h> |
Andrew Bresticker | fb8f7be1 | 2014-10-20 12:03:55 -0700 | [diff] [blame] | 13 | #include <linux/irq.h> |
Joel Porquet | 41a83e0 | 2015-07-07 17:11:46 -0400 | [diff] [blame] | 14 | #include <linux/irqchip.h> |
Andrew Bresticker | 4060bbe | 2014-10-20 12:03:53 -0700 | [diff] [blame] | 15 | #include <linux/irqchip/mips-gic.h> |
Andrew Bresticker | a705727 | 2014-11-12 11:43:38 -0800 | [diff] [blame] | 16 | #include <linux/of_address.h> |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 17 | #include <linux/sched.h> |
Ralf Baechle | 631330f | 2009-06-19 14:05:26 +0100 | [diff] [blame] | 18 | #include <linux/smp.h> |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 19 | |
Paul Burton | e83f7e0 | 2017-08-12 19:49:41 -0700 | [diff] [blame] | 20 | #include <asm/mips-cps.h> |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 21 | #include <asm/setup.h> |
| 22 | #include <asm/traps.h> |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 23 | |
Andrew Bresticker | a705727 | 2014-11-12 11:43:38 -0800 | [diff] [blame] | 24 | #include <dt-bindings/interrupt-controller/mips-gic.h> |
| 25 | |
Steven J. Hill | ff86714 | 2013-04-10 16:27:04 -0500 | [diff] [blame] | 26 | unsigned int gic_present; |
Paul Burton | 582e2b4 | 2017-08-12 21:36:10 -0700 | [diff] [blame] | 27 | void __iomem *mips_gic_base; |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 28 | |
Jeffrey Deans | 822350b | 2014-07-17 09:20:53 +0100 | [diff] [blame] | 29 | struct gic_pcpu_mask { |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 30 | DECLARE_BITMAP(pcpu_mask, GIC_MAX_INTRS); |
Jeffrey Deans | 822350b | 2014-07-17 09:20:53 +0100 | [diff] [blame] | 31 | }; |
| 32 | |
Alex Smith | c0a9f72 | 2015-10-12 10:40:43 +0100 | [diff] [blame] | 33 | static unsigned long __gic_base_addr; |
Steven J. Hill | 0b271f5 | 2012-08-31 16:05:37 -0500 | [diff] [blame] | 34 | static struct gic_pcpu_mask pcpu_masks[NR_CPUS]; |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 35 | static DEFINE_SPINLOCK(gic_lock); |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 36 | static struct irq_domain *gic_irq_domain; |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 37 | static struct irq_domain *gic_ipi_domain; |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 38 | static int gic_shared_intrs; |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 39 | static int gic_vpes; |
Andrew Bresticker | 3263d08 | 2014-09-18 14:47:28 -0700 | [diff] [blame] | 40 | static unsigned int gic_cpu_pin; |
James Hogan | 1b6af71 | 2015-01-19 15:38:24 +0000 | [diff] [blame] | 41 | static unsigned int timer_cpu_pin; |
Andrew Bresticker | 4a6a3ea3 | 2014-09-18 14:47:26 -0700 | [diff] [blame] | 42 | static struct irq_chip gic_level_irq_controller, gic_edge_irq_controller; |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 43 | DECLARE_BITMAP(ipi_resrv, GIC_MAX_INTRS); |
Paul Burton | f8dcd9e | 2017-04-20 10:07:34 +0100 | [diff] [blame] | 44 | DECLARE_BITMAP(ipi_available, GIC_MAX_INTRS); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 45 | |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 46 | static void __gic_irq_dispatch(void); |
| 47 | |
Markos Chandras | c3f57f0 | 2015-07-14 10:26:09 +0100 | [diff] [blame] | 48 | static inline u32 gic_read32(unsigned int reg) |
Andrew Bresticker | 5f68fea | 2014-10-20 12:03:52 -0700 | [diff] [blame] | 49 | { |
Paul Burton | 582e2b4 | 2017-08-12 21:36:10 -0700 | [diff] [blame] | 50 | return __raw_readl(mips_gic_base + reg); |
Andrew Bresticker | 5f68fea | 2014-10-20 12:03:52 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Markos Chandras | c3f57f0 | 2015-07-14 10:26:09 +0100 | [diff] [blame] | 53 | static inline u64 gic_read64(unsigned int reg) |
Andrew Bresticker | 5f68fea | 2014-10-20 12:03:52 -0700 | [diff] [blame] | 54 | { |
Paul Burton | 582e2b4 | 2017-08-12 21:36:10 -0700 | [diff] [blame] | 55 | return __raw_readq(mips_gic_base + reg); |
Andrew Bresticker | 5f68fea | 2014-10-20 12:03:52 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Markos Chandras | c3f57f0 | 2015-07-14 10:26:09 +0100 | [diff] [blame] | 58 | static inline unsigned long gic_read(unsigned int reg) |
Andrew Bresticker | 5f68fea | 2014-10-20 12:03:52 -0700 | [diff] [blame] | 59 | { |
Markos Chandras | c3f57f0 | 2015-07-14 10:26:09 +0100 | [diff] [blame] | 60 | if (!mips_cm_is64) |
| 61 | return gic_read32(reg); |
| 62 | else |
| 63 | return gic_read64(reg); |
| 64 | } |
| 65 | |
| 66 | static inline void gic_write32(unsigned int reg, u32 val) |
| 67 | { |
Paul Burton | 582e2b4 | 2017-08-12 21:36:10 -0700 | [diff] [blame] | 68 | return __raw_writel(val, mips_gic_base + reg); |
Markos Chandras | c3f57f0 | 2015-07-14 10:26:09 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static inline void gic_write64(unsigned int reg, u64 val) |
| 72 | { |
Paul Burton | 582e2b4 | 2017-08-12 21:36:10 -0700 | [diff] [blame] | 73 | return __raw_writeq(val, mips_gic_base + reg); |
Markos Chandras | c3f57f0 | 2015-07-14 10:26:09 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static inline void gic_write(unsigned int reg, unsigned long val) |
| 77 | { |
| 78 | if (!mips_cm_is64) |
| 79 | return gic_write32(reg, (u32)val); |
| 80 | else |
| 81 | return gic_write64(reg, (u64)val); |
| 82 | } |
| 83 | |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 84 | static bool gic_local_irq_is_routable(int intr) |
| 85 | { |
| 86 | u32 vpe_ctl; |
| 87 | |
| 88 | /* All local interrupts are routable in EIC mode. */ |
| 89 | if (cpu_has_veic) |
| 90 | return true; |
| 91 | |
Markos Chandras | c3f57f0 | 2015-07-14 10:26:09 +0100 | [diff] [blame] | 92 | vpe_ctl = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_CTL)); |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 93 | switch (intr) { |
| 94 | case GIC_LOCAL_INT_TIMER: |
| 95 | return vpe_ctl & GIC_VPE_CTL_TIMER_RTBL_MSK; |
| 96 | case GIC_LOCAL_INT_PERFCTR: |
| 97 | return vpe_ctl & GIC_VPE_CTL_PERFCNT_RTBL_MSK; |
| 98 | case GIC_LOCAL_INT_FDC: |
| 99 | return vpe_ctl & GIC_VPE_CTL_FDC_RTBL_MSK; |
| 100 | case GIC_LOCAL_INT_SWINT0: |
| 101 | case GIC_LOCAL_INT_SWINT1: |
| 102 | return vpe_ctl & GIC_VPE_CTL_SWINT_RTBL_MSK; |
| 103 | default: |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | |
Andrew Bresticker | 3263d08 | 2014-09-18 14:47:28 -0700 | [diff] [blame] | 108 | static void gic_bind_eic_interrupt(int irq, int set) |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 109 | { |
| 110 | /* Convert irq vector # to hw int # */ |
| 111 | irq -= GIC_PIN_TO_VEC_OFFSET; |
| 112 | |
| 113 | /* Set irq to use shadow set */ |
Andrew Bresticker | 5f68fea | 2014-10-20 12:03:52 -0700 | [diff] [blame] | 114 | gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_EIC_SHADOW_SET_BASE) + |
| 115 | GIC_VPE_EIC_SS(irq), set); |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Qais Yousef | bb11cff | 2015-12-08 13:20:28 +0000 | [diff] [blame] | 118 | static void gic_send_ipi(struct irq_data *d, unsigned int cpu) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 119 | { |
Qais Yousef | bb11cff | 2015-12-08 13:20:28 +0000 | [diff] [blame] | 120 | irq_hw_number_t hwirq = GIC_HWIRQ_TO_SHARED(irqd_to_hwirq(d)); |
| 121 | |
Paul Burton | 3680746 | 2017-08-12 21:36:24 -0700 | [diff] [blame^] | 122 | write_gic_wedge(GIC_WEDGE_RW | hwirq); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 125 | int gic_get_c0_compare_int(void) |
| 126 | { |
| 127 | if (!gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER)) |
| 128 | return MIPS_CPU_IRQ_BASE + cp0_compare_irq; |
| 129 | return irq_create_mapping(gic_irq_domain, |
| 130 | GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_TIMER)); |
| 131 | } |
| 132 | |
| 133 | int gic_get_c0_perfcount_int(void) |
| 134 | { |
| 135 | if (!gic_local_irq_is_routable(GIC_LOCAL_INT_PERFCTR)) { |
James Hogan | 7e3e6cb | 2015-01-27 21:45:50 +0000 | [diff] [blame] | 136 | /* Is the performance counter shared with the timer? */ |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 137 | if (cp0_perfcount_irq < 0) |
| 138 | return -1; |
| 139 | return MIPS_CPU_IRQ_BASE + cp0_perfcount_irq; |
| 140 | } |
| 141 | return irq_create_mapping(gic_irq_domain, |
| 142 | GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_PERFCTR)); |
| 143 | } |
| 144 | |
James Hogan | 6429e2b | 2015-01-29 11:14:09 +0000 | [diff] [blame] | 145 | int gic_get_c0_fdc_int(void) |
| 146 | { |
| 147 | if (!gic_local_irq_is_routable(GIC_LOCAL_INT_FDC)) { |
| 148 | /* Is the FDC IRQ even present? */ |
| 149 | if (cp0_fdc_irq < 0) |
| 150 | return -1; |
| 151 | return MIPS_CPU_IRQ_BASE + cp0_fdc_irq; |
| 152 | } |
| 153 | |
James Hogan | 6429e2b | 2015-01-29 11:14:09 +0000 | [diff] [blame] | 154 | return irq_create_mapping(gic_irq_domain, |
| 155 | GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_FDC)); |
| 156 | } |
| 157 | |
Alex Smith | c0a9f72 | 2015-10-12 10:40:43 +0100 | [diff] [blame] | 158 | int gic_get_usm_range(struct resource *gic_usm_res) |
| 159 | { |
| 160 | if (!gic_present) |
| 161 | return -1; |
| 162 | |
| 163 | gic_usm_res->start = __gic_base_addr + USM_VISIBLE_SECTION_OFS; |
| 164 | gic_usm_res->end = gic_usm_res->start + (USM_VISIBLE_SECTION_SIZE - 1); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
Rabin Vincent | 1b3ed36 | 2015-06-12 10:01:56 +0200 | [diff] [blame] | 169 | static void gic_handle_shared_int(bool chained) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 170 | { |
Paul Burton | e98fcb2 | 2017-08-12 21:36:16 -0700 | [diff] [blame] | 171 | unsigned int intr, virq; |
Andrew Bresticker | 8f5ee79 | 2014-10-20 12:03:56 -0700 | [diff] [blame] | 172 | unsigned long *pcpu_mask; |
Andrew Bresticker | 8f5ee79 | 2014-10-20 12:03:56 -0700 | [diff] [blame] | 173 | DECLARE_BITMAP(pending, GIC_MAX_INTRS); |
| 174 | DECLARE_BITMAP(intrmask, GIC_MAX_INTRS); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 175 | |
| 176 | /* Get per-cpu bitmaps */ |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 177 | pcpu_mask = pcpu_masks[smp_processor_id()].pcpu_mask; |
| 178 | |
Paul Burton | e98fcb2 | 2017-08-12 21:36:16 -0700 | [diff] [blame] | 179 | if (mips_cm_is64) { |
| 180 | __ioread64_copy(pending, addr_gic_pend(), |
| 181 | DIV_ROUND_UP(gic_shared_intrs, 64)); |
| 182 | __ioread64_copy(intrmask, addr_gic_mask(), |
| 183 | DIV_ROUND_UP(gic_shared_intrs, 64)); |
| 184 | } else { |
| 185 | __ioread32_copy(pending, addr_gic_pend(), |
| 186 | DIV_ROUND_UP(gic_shared_intrs, 32)); |
| 187 | __ioread32_copy(intrmask, addr_gic_mask(), |
| 188 | DIV_ROUND_UP(gic_shared_intrs, 32)); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 191 | bitmap_and(pending, pending, intrmask, gic_shared_intrs); |
| 192 | bitmap_and(pending, pending, pcpu_mask, gic_shared_intrs); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 193 | |
Paul Burton | cae750b | 2016-08-19 18:11:19 +0100 | [diff] [blame] | 194 | for_each_set_bit(intr, pending, gic_shared_intrs) { |
Qais Yousef | d7eb4f2 | 2015-01-19 11:51:29 +0000 | [diff] [blame] | 195 | virq = irq_linear_revmap(gic_irq_domain, |
| 196 | GIC_SHARED_TO_HWIRQ(intr)); |
Rabin Vincent | 1b3ed36 | 2015-06-12 10:01:56 +0200 | [diff] [blame] | 197 | if (chained) |
| 198 | generic_handle_irq(virq); |
| 199 | else |
| 200 | do_IRQ(virq); |
Qais Yousef | d7eb4f2 | 2015-01-19 11:51:29 +0000 | [diff] [blame] | 201 | } |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 204 | static void gic_mask_irq(struct irq_data *d) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 205 | { |
Paul Burton | 87554b0 | 2017-08-12 21:36:18 -0700 | [diff] [blame] | 206 | write_gic_rmask(BIT(GIC_HWIRQ_TO_SHARED(d->hwirq))); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 209 | static void gic_unmask_irq(struct irq_data *d) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 210 | { |
Paul Burton | 87554b0 | 2017-08-12 21:36:18 -0700 | [diff] [blame] | 211 | write_gic_smask(BIT(GIC_HWIRQ_TO_SHARED(d->hwirq))); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Andrew Bresticker | 5561c9e | 2014-09-18 14:47:20 -0700 | [diff] [blame] | 214 | static void gic_ack_irq(struct irq_data *d) |
| 215 | { |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 216 | unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq); |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 217 | |
Paul Burton | 3680746 | 2017-08-12 21:36:24 -0700 | [diff] [blame^] | 218 | write_gic_wedge(irq); |
Andrew Bresticker | 5561c9e | 2014-09-18 14:47:20 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 221 | static int gic_set_type(struct irq_data *d, unsigned int type) |
| 222 | { |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 223 | unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq); |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 224 | unsigned long flags; |
| 225 | bool is_edge; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 226 | |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 227 | spin_lock_irqsave(&gic_lock, flags); |
| 228 | switch (type & IRQ_TYPE_SENSE_MASK) { |
| 229 | case IRQ_TYPE_EDGE_FALLING: |
Paul Burton | 80e5f9c | 2017-08-12 21:36:19 -0700 | [diff] [blame] | 230 | change_gic_pol(irq, GIC_POL_FALLING_EDGE); |
Paul Burton | 471aa96 | 2017-08-12 21:36:20 -0700 | [diff] [blame] | 231 | change_gic_trig(irq, GIC_TRIG_EDGE); |
Paul Burton | c26ba67 | 2017-08-12 21:36:21 -0700 | [diff] [blame] | 232 | change_gic_dual(irq, GIC_DUAL_SINGLE); |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 233 | is_edge = true; |
| 234 | break; |
| 235 | case IRQ_TYPE_EDGE_RISING: |
Paul Burton | 80e5f9c | 2017-08-12 21:36:19 -0700 | [diff] [blame] | 236 | change_gic_pol(irq, GIC_POL_RISING_EDGE); |
Paul Burton | 471aa96 | 2017-08-12 21:36:20 -0700 | [diff] [blame] | 237 | change_gic_trig(irq, GIC_TRIG_EDGE); |
Paul Burton | c26ba67 | 2017-08-12 21:36:21 -0700 | [diff] [blame] | 238 | change_gic_dual(irq, GIC_DUAL_SINGLE); |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 239 | is_edge = true; |
| 240 | break; |
| 241 | case IRQ_TYPE_EDGE_BOTH: |
| 242 | /* polarity is irrelevant in this case */ |
Paul Burton | 471aa96 | 2017-08-12 21:36:20 -0700 | [diff] [blame] | 243 | change_gic_trig(irq, GIC_TRIG_EDGE); |
Paul Burton | c26ba67 | 2017-08-12 21:36:21 -0700 | [diff] [blame] | 244 | change_gic_dual(irq, GIC_DUAL_DUAL); |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 245 | is_edge = true; |
| 246 | break; |
| 247 | case IRQ_TYPE_LEVEL_LOW: |
Paul Burton | 80e5f9c | 2017-08-12 21:36:19 -0700 | [diff] [blame] | 248 | change_gic_pol(irq, GIC_POL_ACTIVE_LOW); |
Paul Burton | 471aa96 | 2017-08-12 21:36:20 -0700 | [diff] [blame] | 249 | change_gic_trig(irq, GIC_TRIG_LEVEL); |
Paul Burton | c26ba67 | 2017-08-12 21:36:21 -0700 | [diff] [blame] | 250 | change_gic_dual(irq, GIC_DUAL_SINGLE); |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 251 | is_edge = false; |
| 252 | break; |
| 253 | case IRQ_TYPE_LEVEL_HIGH: |
| 254 | default: |
Paul Burton | 80e5f9c | 2017-08-12 21:36:19 -0700 | [diff] [blame] | 255 | change_gic_pol(irq, GIC_POL_ACTIVE_HIGH); |
Paul Burton | 471aa96 | 2017-08-12 21:36:20 -0700 | [diff] [blame] | 256 | change_gic_trig(irq, GIC_TRIG_LEVEL); |
Paul Burton | c26ba67 | 2017-08-12 21:36:21 -0700 | [diff] [blame] | 257 | change_gic_dual(irq, GIC_DUAL_SINGLE); |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 258 | is_edge = false; |
| 259 | break; |
| 260 | } |
| 261 | |
Thomas Gleixner | a595fc5 | 2015-06-23 14:41:25 +0200 | [diff] [blame] | 262 | if (is_edge) |
| 263 | irq_set_chip_handler_name_locked(d, &gic_edge_irq_controller, |
| 264 | handle_edge_irq, NULL); |
| 265 | else |
| 266 | irq_set_chip_handler_name_locked(d, &gic_level_irq_controller, |
| 267 | handle_level_irq, NULL); |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 268 | spin_unlock_irqrestore(&gic_lock, flags); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | #ifdef CONFIG_SMP |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 274 | static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask, |
| 275 | bool force) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 276 | { |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 277 | unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 278 | cpumask_t tmp = CPU_MASK_NONE; |
| 279 | unsigned long flags; |
| 280 | int i; |
| 281 | |
Rusty Russell | 0de2652 | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 282 | cpumask_and(&tmp, cpumask, cpu_online_mask); |
Rusty Russell | f9b531f | 2015-03-05 10:49:16 +1030 | [diff] [blame] | 283 | if (cpumask_empty(&tmp)) |
Andrew Bresticker | 14d160a | 2014-09-18 14:47:22 -0700 | [diff] [blame] | 284 | return -EINVAL; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 285 | |
| 286 | /* Assumption : cpumask refers to a single CPU */ |
| 287 | spin_lock_irqsave(&gic_lock, flags); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 288 | |
Tony Wu | c214c03 | 2013-06-21 10:13:08 +0000 | [diff] [blame] | 289 | /* Re-route this IRQ */ |
Paul Burton | 0efe3cb | 2017-08-12 21:36:23 -0700 | [diff] [blame] | 290 | write_gic_map_vp(irq, BIT(mips_cm_vp_id(cpumask_first(&tmp)))); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 291 | |
Tony Wu | c214c03 | 2013-06-21 10:13:08 +0000 | [diff] [blame] | 292 | /* Update the pcpu_masks */ |
Paul Burton | 91951f9 | 2016-04-21 11:31:54 +0100 | [diff] [blame] | 293 | for (i = 0; i < min(gic_vpes, NR_CPUS); i++) |
Tony Wu | c214c03 | 2013-06-21 10:13:08 +0000 | [diff] [blame] | 294 | clear_bit(irq, pcpu_masks[i].pcpu_mask); |
Rusty Russell | f9b531f | 2015-03-05 10:49:16 +1030 | [diff] [blame] | 295 | set_bit(irq, pcpu_masks[cpumask_first(&tmp)].pcpu_mask); |
Tony Wu | c214c03 | 2013-06-21 10:13:08 +0000 | [diff] [blame] | 296 | |
Jiang Liu | 72f86db | 2015-06-01 16:05:38 +0800 | [diff] [blame] | 297 | cpumask_copy(irq_data_get_affinity_mask(d), cpumask); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 298 | spin_unlock_irqrestore(&gic_lock, flags); |
| 299 | |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 300 | return IRQ_SET_MASK_OK_NOCOPY; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 301 | } |
| 302 | #endif |
| 303 | |
Andrew Bresticker | 4a6a3ea3 | 2014-09-18 14:47:26 -0700 | [diff] [blame] | 304 | static struct irq_chip gic_level_irq_controller = { |
| 305 | .name = "MIPS GIC", |
| 306 | .irq_mask = gic_mask_irq, |
| 307 | .irq_unmask = gic_unmask_irq, |
| 308 | .irq_set_type = gic_set_type, |
| 309 | #ifdef CONFIG_SMP |
| 310 | .irq_set_affinity = gic_set_affinity, |
| 311 | #endif |
| 312 | }; |
| 313 | |
| 314 | static struct irq_chip gic_edge_irq_controller = { |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 315 | .name = "MIPS GIC", |
Andrew Bresticker | 5561c9e | 2014-09-18 14:47:20 -0700 | [diff] [blame] | 316 | .irq_ack = gic_ack_irq, |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 317 | .irq_mask = gic_mask_irq, |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 318 | .irq_unmask = gic_unmask_irq, |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 319 | .irq_set_type = gic_set_type, |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 320 | #ifdef CONFIG_SMP |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 321 | .irq_set_affinity = gic_set_affinity, |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 322 | #endif |
Qais Yousef | bb11cff | 2015-12-08 13:20:28 +0000 | [diff] [blame] | 323 | .ipi_send_single = gic_send_ipi, |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 324 | }; |
| 325 | |
Rabin Vincent | 1b3ed36 | 2015-06-12 10:01:56 +0200 | [diff] [blame] | 326 | static void gic_handle_local_int(bool chained) |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 327 | { |
| 328 | unsigned long pending, masked; |
Qais Yousef | d7eb4f2 | 2015-01-19 11:51:29 +0000 | [diff] [blame] | 329 | unsigned int intr, virq; |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 330 | |
Markos Chandras | c3f57f0 | 2015-07-14 10:26:09 +0100 | [diff] [blame] | 331 | pending = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_PEND)); |
| 332 | masked = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_MASK)); |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 333 | |
| 334 | bitmap_and(&pending, &pending, &masked, GIC_NUM_LOCAL_INTRS); |
| 335 | |
Paul Burton | 0f4ed15 | 2016-09-13 17:54:27 +0100 | [diff] [blame] | 336 | for_each_set_bit(intr, &pending, GIC_NUM_LOCAL_INTRS) { |
Qais Yousef | d7eb4f2 | 2015-01-19 11:51:29 +0000 | [diff] [blame] | 337 | virq = irq_linear_revmap(gic_irq_domain, |
| 338 | GIC_LOCAL_TO_HWIRQ(intr)); |
Rabin Vincent | 1b3ed36 | 2015-06-12 10:01:56 +0200 | [diff] [blame] | 339 | if (chained) |
| 340 | generic_handle_irq(virq); |
| 341 | else |
| 342 | do_IRQ(virq); |
Qais Yousef | d7eb4f2 | 2015-01-19 11:51:29 +0000 | [diff] [blame] | 343 | } |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static void gic_mask_local_irq(struct irq_data *d) |
| 347 | { |
| 348 | int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq); |
| 349 | |
Markos Chandras | c3f57f0 | 2015-07-14 10:26:09 +0100 | [diff] [blame] | 350 | gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_RMASK), 1 << intr); |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static void gic_unmask_local_irq(struct irq_data *d) |
| 354 | { |
| 355 | int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq); |
| 356 | |
Markos Chandras | c3f57f0 | 2015-07-14 10:26:09 +0100 | [diff] [blame] | 357 | gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_SMASK), 1 << intr); |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static struct irq_chip gic_local_irq_controller = { |
| 361 | .name = "MIPS GIC Local", |
| 362 | .irq_mask = gic_mask_local_irq, |
| 363 | .irq_unmask = gic_unmask_local_irq, |
| 364 | }; |
| 365 | |
| 366 | static void gic_mask_local_irq_all_vpes(struct irq_data *d) |
| 367 | { |
| 368 | int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq); |
| 369 | int i; |
| 370 | unsigned long flags; |
| 371 | |
| 372 | spin_lock_irqsave(&gic_lock, flags); |
| 373 | for (i = 0; i < gic_vpes; i++) { |
Paul Burton | d46812b | 2016-02-03 03:15:27 +0000 | [diff] [blame] | 374 | gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), |
| 375 | mips_cm_vp_id(i)); |
Markos Chandras | c3f57f0 | 2015-07-14 10:26:09 +0100 | [diff] [blame] | 376 | gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << intr); |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 377 | } |
| 378 | spin_unlock_irqrestore(&gic_lock, flags); |
| 379 | } |
| 380 | |
| 381 | static void gic_unmask_local_irq_all_vpes(struct irq_data *d) |
| 382 | { |
| 383 | int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq); |
| 384 | int i; |
| 385 | unsigned long flags; |
| 386 | |
| 387 | spin_lock_irqsave(&gic_lock, flags); |
| 388 | for (i = 0; i < gic_vpes; i++) { |
Paul Burton | d46812b | 2016-02-03 03:15:27 +0000 | [diff] [blame] | 389 | gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), |
| 390 | mips_cm_vp_id(i)); |
Markos Chandras | c3f57f0 | 2015-07-14 10:26:09 +0100 | [diff] [blame] | 391 | gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SMASK), 1 << intr); |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 392 | } |
| 393 | spin_unlock_irqrestore(&gic_lock, flags); |
| 394 | } |
| 395 | |
| 396 | static struct irq_chip gic_all_vpes_local_irq_controller = { |
| 397 | .name = "MIPS GIC Local", |
| 398 | .irq_mask = gic_mask_local_irq_all_vpes, |
| 399 | .irq_unmask = gic_unmask_local_irq_all_vpes, |
| 400 | }; |
| 401 | |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 402 | static void __gic_irq_dispatch(void) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 403 | { |
Rabin Vincent | 1b3ed36 | 2015-06-12 10:01:56 +0200 | [diff] [blame] | 404 | gic_handle_local_int(false); |
| 405 | gic_handle_shared_int(false); |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 408 | static void gic_irq_dispatch(struct irq_desc *desc) |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 409 | { |
Rabin Vincent | 1b3ed36 | 2015-06-12 10:01:56 +0200 | [diff] [blame] | 410 | gic_handle_local_int(true); |
| 411 | gic_handle_shared_int(true); |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 414 | static void __init gic_basic_init(void) |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 415 | { |
| 416 | unsigned int i; |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 417 | |
| 418 | board_bind_eic_interrupt = &gic_bind_eic_interrupt; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 419 | |
| 420 | /* Setup defaults */ |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 421 | for (i = 0; i < gic_shared_intrs; i++) { |
Paul Burton | 80e5f9c | 2017-08-12 21:36:19 -0700 | [diff] [blame] | 422 | change_gic_pol(i, GIC_POL_ACTIVE_HIGH); |
Paul Burton | 471aa96 | 2017-08-12 21:36:20 -0700 | [diff] [blame] | 423 | change_gic_trig(i, GIC_TRIG_LEVEL); |
Paul Burton | 87554b0 | 2017-08-12 21:36:18 -0700 | [diff] [blame] | 424 | write_gic_rmask(BIT(i)); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 427 | for (i = 0; i < gic_vpes; i++) { |
| 428 | unsigned int j; |
| 429 | |
Paul Burton | d46812b | 2016-02-03 03:15:27 +0000 | [diff] [blame] | 430 | gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), |
| 431 | mips_cm_vp_id(i)); |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 432 | for (j = 0; j < GIC_NUM_LOCAL_INTRS; j++) { |
| 433 | if (!gic_local_irq_is_routable(j)) |
| 434 | continue; |
Markos Chandras | c3f57f0 | 2015-07-14 10:26:09 +0100 | [diff] [blame] | 435 | gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << j); |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 438 | } |
| 439 | |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 440 | static int gic_local_irq_domain_map(struct irq_domain *d, unsigned int virq, |
| 441 | irq_hw_number_t hw) |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 442 | { |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 443 | int intr = GIC_HWIRQ_TO_LOCAL(hw); |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 444 | int i; |
| 445 | unsigned long flags; |
Paul Burton | a0dc5cb5 | 2017-08-12 21:36:17 -0700 | [diff] [blame] | 446 | u32 val; |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 447 | |
| 448 | if (!gic_local_irq_is_routable(intr)) |
| 449 | return -EPERM; |
| 450 | |
Paul Burton | a0dc5cb5 | 2017-08-12 21:36:17 -0700 | [diff] [blame] | 451 | if (intr > GIC_LOCAL_INT_FDC) { |
| 452 | pr_err("Invalid local IRQ %d\n", intr); |
| 453 | return -EINVAL; |
| 454 | } |
| 455 | |
| 456 | if (intr == GIC_LOCAL_INT_TIMER) { |
| 457 | /* CONFIG_MIPS_CMP workaround (see __gic_init) */ |
| 458 | val = GIC_MAP_PIN_MAP_TO_PIN | timer_cpu_pin; |
| 459 | } else { |
| 460 | val = GIC_MAP_PIN_MAP_TO_PIN | gic_cpu_pin; |
| 461 | } |
| 462 | |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 463 | spin_lock_irqsave(&gic_lock, flags); |
| 464 | for (i = 0; i < gic_vpes; i++) { |
Paul Burton | a0dc5cb5 | 2017-08-12 21:36:17 -0700 | [diff] [blame] | 465 | write_gic_vl_other(mips_cm_vp_id(i)); |
| 466 | write_gic_vo_map(intr, val); |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 467 | } |
| 468 | spin_unlock_irqrestore(&gic_lock, flags); |
| 469 | |
Paul Burton | a0dc5cb5 | 2017-08-12 21:36:17 -0700 | [diff] [blame] | 470 | return 0; |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static int gic_shared_irq_domain_map(struct irq_domain *d, unsigned int virq, |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 474 | irq_hw_number_t hw, unsigned int vpe) |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 475 | { |
| 476 | int intr = GIC_HWIRQ_TO_SHARED(hw); |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 477 | unsigned long flags; |
Qais Yousef | 78930f0 | 2015-12-08 13:20:26 +0000 | [diff] [blame] | 478 | int i; |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 479 | |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 480 | spin_lock_irqsave(&gic_lock, flags); |
Paul Burton | d3e8cf4 | 2017-08-12 21:36:22 -0700 | [diff] [blame] | 481 | write_gic_map_pin(intr, GIC_MAP_PIN_MAP_TO_PIN | gic_cpu_pin); |
Paul Burton | 0efe3cb | 2017-08-12 21:36:23 -0700 | [diff] [blame] | 482 | write_gic_map_vp(intr, BIT(mips_cm_vp_id(vpe))); |
Paul Burton | 91951f9 | 2016-04-21 11:31:54 +0100 | [diff] [blame] | 483 | for (i = 0; i < min(gic_vpes, NR_CPUS); i++) |
Qais Yousef | 78930f0 | 2015-12-08 13:20:26 +0000 | [diff] [blame] | 484 | clear_bit(intr, pcpu_masks[i].pcpu_mask); |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 485 | set_bit(intr, pcpu_masks[vpe].pcpu_mask); |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 486 | spin_unlock_irqrestore(&gic_lock, flags); |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
Paul Burton | b87281e | 2017-04-20 10:07:35 +0100 | [diff] [blame] | 491 | static int gic_irq_domain_xlate(struct irq_domain *d, struct device_node *ctrlr, |
Qais Yousef | c98c1822 | 2015-12-08 13:20:24 +0000 | [diff] [blame] | 492 | const u32 *intspec, unsigned int intsize, |
| 493 | irq_hw_number_t *out_hwirq, |
| 494 | unsigned int *out_type) |
| 495 | { |
| 496 | if (intsize != 3) |
| 497 | return -EINVAL; |
| 498 | |
| 499 | if (intspec[0] == GIC_SHARED) |
| 500 | *out_hwirq = GIC_SHARED_TO_HWIRQ(intspec[1]); |
| 501 | else if (intspec[0] == GIC_LOCAL) |
| 502 | *out_hwirq = GIC_LOCAL_TO_HWIRQ(intspec[1]); |
| 503 | else |
| 504 | return -EINVAL; |
| 505 | *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK; |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
Matt Redfearn | 8ada00a | 2017-04-20 10:07:36 +0100 | [diff] [blame] | 510 | static int gic_irq_domain_map(struct irq_domain *d, unsigned int virq, |
| 511 | irq_hw_number_t hwirq) |
Qais Yousef | c98c1822 | 2015-12-08 13:20:24 +0000 | [diff] [blame] | 512 | { |
Paul Burton | b87281e | 2017-04-20 10:07:35 +0100 | [diff] [blame] | 513 | int err; |
Qais Yousef | c98c1822 | 2015-12-08 13:20:24 +0000 | [diff] [blame] | 514 | |
Matt Redfearn | 8ada00a | 2017-04-20 10:07:36 +0100 | [diff] [blame] | 515 | if (hwirq >= GIC_SHARED_HWIRQ_BASE) { |
Paul Burton | b87281e | 2017-04-20 10:07:35 +0100 | [diff] [blame] | 516 | /* verify that shared irqs don't conflict with an IPI irq */ |
| 517 | if (test_bit(GIC_HWIRQ_TO_SHARED(hwirq), ipi_resrv)) |
| 518 | return -EBUSY; |
Qais Yousef | c98c1822 | 2015-12-08 13:20:24 +0000 | [diff] [blame] | 519 | |
Paul Burton | b87281e | 2017-04-20 10:07:35 +0100 | [diff] [blame] | 520 | err = irq_domain_set_hwirq_and_chip(d, virq, hwirq, |
| 521 | &gic_level_irq_controller, |
| 522 | NULL); |
| 523 | if (err) |
| 524 | return err; |
| 525 | |
| 526 | return gic_shared_irq_domain_map(d, virq, hwirq, 0); |
Qais Yousef | c98c1822 | 2015-12-08 13:20:24 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Paul Burton | b87281e | 2017-04-20 10:07:35 +0100 | [diff] [blame] | 529 | switch (GIC_HWIRQ_TO_LOCAL(hwirq)) { |
| 530 | case GIC_LOCAL_INT_TIMER: |
| 531 | case GIC_LOCAL_INT_PERFCTR: |
| 532 | case GIC_LOCAL_INT_FDC: |
| 533 | /* |
| 534 | * HACK: These are all really percpu interrupts, but |
| 535 | * the rest of the MIPS kernel code does not use the |
| 536 | * percpu IRQ API for them. |
| 537 | */ |
| 538 | err = irq_domain_set_hwirq_and_chip(d, virq, hwirq, |
| 539 | &gic_all_vpes_local_irq_controller, |
| 540 | NULL); |
| 541 | if (err) |
| 542 | return err; |
| 543 | |
| 544 | irq_set_handler(virq, handle_percpu_irq); |
| 545 | break; |
| 546 | |
| 547 | default: |
| 548 | err = irq_domain_set_hwirq_and_chip(d, virq, hwirq, |
| 549 | &gic_local_irq_controller, |
| 550 | NULL); |
| 551 | if (err) |
| 552 | return err; |
| 553 | |
| 554 | irq_set_handler(virq, handle_percpu_devid_irq); |
| 555 | irq_set_percpu_devid(virq); |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | return gic_local_irq_domain_map(d, virq, hwirq); |
Qais Yousef | c98c1822 | 2015-12-08 13:20:24 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Matt Redfearn | 8ada00a | 2017-04-20 10:07:36 +0100 | [diff] [blame] | 562 | static int gic_irq_domain_alloc(struct irq_domain *d, unsigned int virq, |
| 563 | unsigned int nr_irqs, void *arg) |
| 564 | { |
| 565 | struct irq_fwspec *fwspec = arg; |
| 566 | irq_hw_number_t hwirq; |
| 567 | |
| 568 | if (fwspec->param[0] == GIC_SHARED) |
| 569 | hwirq = GIC_SHARED_TO_HWIRQ(fwspec->param[1]); |
| 570 | else |
| 571 | hwirq = GIC_LOCAL_TO_HWIRQ(fwspec->param[1]); |
| 572 | |
| 573 | return gic_irq_domain_map(d, virq, hwirq); |
| 574 | } |
| 575 | |
Paul Burton | b87281e | 2017-04-20 10:07:35 +0100 | [diff] [blame] | 576 | void gic_irq_domain_free(struct irq_domain *d, unsigned int virq, |
Qais Yousef | c98c1822 | 2015-12-08 13:20:24 +0000 | [diff] [blame] | 577 | unsigned int nr_irqs) |
| 578 | { |
Qais Yousef | c98c1822 | 2015-12-08 13:20:24 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Paul Burton | b87281e | 2017-04-20 10:07:35 +0100 | [diff] [blame] | 581 | static const struct irq_domain_ops gic_irq_domain_ops = { |
| 582 | .xlate = gic_irq_domain_xlate, |
| 583 | .alloc = gic_irq_domain_alloc, |
| 584 | .free = gic_irq_domain_free, |
Matt Redfearn | 8ada00a | 2017-04-20 10:07:36 +0100 | [diff] [blame] | 585 | .map = gic_irq_domain_map, |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 586 | }; |
| 587 | |
| 588 | static int gic_ipi_domain_xlate(struct irq_domain *d, struct device_node *ctrlr, |
| 589 | const u32 *intspec, unsigned int intsize, |
| 590 | irq_hw_number_t *out_hwirq, |
| 591 | unsigned int *out_type) |
| 592 | { |
| 593 | /* |
| 594 | * There's nothing to translate here. hwirq is dynamically allocated and |
| 595 | * the irq type is always edge triggered. |
| 596 | * */ |
| 597 | *out_hwirq = 0; |
| 598 | *out_type = IRQ_TYPE_EDGE_RISING; |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | static int gic_ipi_domain_alloc(struct irq_domain *d, unsigned int virq, |
| 604 | unsigned int nr_irqs, void *arg) |
| 605 | { |
| 606 | struct cpumask *ipimask = arg; |
Paul Burton | b87281e | 2017-04-20 10:07:35 +0100 | [diff] [blame] | 607 | irq_hw_number_t hwirq, base_hwirq; |
| 608 | int cpu, ret, i; |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 609 | |
Paul Burton | b87281e | 2017-04-20 10:07:35 +0100 | [diff] [blame] | 610 | base_hwirq = find_first_bit(ipi_available, gic_shared_intrs); |
| 611 | if (base_hwirq == gic_shared_intrs) |
| 612 | return -ENOMEM; |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 613 | |
Paul Burton | b87281e | 2017-04-20 10:07:35 +0100 | [diff] [blame] | 614 | /* check that we have enough space */ |
| 615 | for (i = base_hwirq; i < nr_irqs; i++) { |
| 616 | if (!test_bit(i, ipi_available)) |
| 617 | return -EBUSY; |
| 618 | } |
| 619 | bitmap_clear(ipi_available, base_hwirq, nr_irqs); |
| 620 | |
| 621 | /* map the hwirq for each cpu consecutively */ |
| 622 | i = 0; |
| 623 | for_each_cpu(cpu, ipimask) { |
| 624 | hwirq = GIC_SHARED_TO_HWIRQ(base_hwirq + i); |
| 625 | |
| 626 | ret = irq_domain_set_hwirq_and_chip(d, virq + i, hwirq, |
| 627 | &gic_edge_irq_controller, |
| 628 | NULL); |
| 629 | if (ret) |
| 630 | goto error; |
| 631 | |
| 632 | ret = irq_domain_set_hwirq_and_chip(d->parent, virq + i, hwirq, |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 633 | &gic_edge_irq_controller, |
| 634 | NULL); |
| 635 | if (ret) |
| 636 | goto error; |
| 637 | |
| 638 | ret = irq_set_irq_type(virq + i, IRQ_TYPE_EDGE_RISING); |
| 639 | if (ret) |
| 640 | goto error; |
Paul Burton | b87281e | 2017-04-20 10:07:35 +0100 | [diff] [blame] | 641 | |
| 642 | ret = gic_shared_irq_domain_map(d, virq + i, hwirq, cpu); |
| 643 | if (ret) |
| 644 | goto error; |
| 645 | |
| 646 | i++; |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | return 0; |
| 650 | error: |
Paul Burton | b87281e | 2017-04-20 10:07:35 +0100 | [diff] [blame] | 651 | bitmap_set(ipi_available, base_hwirq, nr_irqs); |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 652 | return ret; |
| 653 | } |
| 654 | |
| 655 | void gic_ipi_domain_free(struct irq_domain *d, unsigned int virq, |
| 656 | unsigned int nr_irqs) |
| 657 | { |
Paul Burton | b87281e | 2017-04-20 10:07:35 +0100 | [diff] [blame] | 658 | irq_hw_number_t base_hwirq; |
| 659 | struct irq_data *data; |
| 660 | |
| 661 | data = irq_get_irq_data(virq); |
| 662 | if (!data) |
| 663 | return; |
| 664 | |
| 665 | base_hwirq = GIC_HWIRQ_TO_SHARED(irqd_to_hwirq(data)); |
| 666 | bitmap_set(ipi_available, base_hwirq, nr_irqs); |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | int gic_ipi_domain_match(struct irq_domain *d, struct device_node *node, |
| 670 | enum irq_domain_bus_token bus_token) |
| 671 | { |
| 672 | bool is_ipi; |
| 673 | |
| 674 | switch (bus_token) { |
| 675 | case DOMAIN_BUS_IPI: |
| 676 | is_ipi = d->bus_token == bus_token; |
Paul Burton | 547aefc | 2016-07-05 14:26:00 +0100 | [diff] [blame] | 677 | return (!node || to_of_node(d->fwnode) == node) && is_ipi; |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 678 | break; |
| 679 | default: |
| 680 | return 0; |
| 681 | } |
| 682 | } |
| 683 | |
Tobias Klauser | 0b7e815 | 2017-06-02 10:20:56 +0200 | [diff] [blame] | 684 | static const struct irq_domain_ops gic_ipi_domain_ops = { |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 685 | .xlate = gic_ipi_domain_xlate, |
| 686 | .alloc = gic_ipi_domain_alloc, |
| 687 | .free = gic_ipi_domain_free, |
| 688 | .match = gic_ipi_domain_match, |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 689 | }; |
| 690 | |
Andrew Bresticker | a705727 | 2014-11-12 11:43:38 -0800 | [diff] [blame] | 691 | static void __init __gic_init(unsigned long gic_base_addr, |
| 692 | unsigned long gic_addrspace_size, |
| 693 | unsigned int cpu_vec, unsigned int irqbase, |
| 694 | struct device_node *node) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 695 | { |
Paul Burton | ba01cf0 | 2016-05-17 15:31:06 +0100 | [diff] [blame] | 696 | unsigned int gicconfig, cpu; |
Qais Yousef | 16a8083 | 2015-12-08 13:20:30 +0000 | [diff] [blame] | 697 | unsigned int v[2]; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 698 | |
Alex Smith | c0a9f72 | 2015-10-12 10:40:43 +0100 | [diff] [blame] | 699 | __gic_base_addr = gic_base_addr; |
| 700 | |
Paul Burton | 582e2b4 | 2017-08-12 21:36:10 -0700 | [diff] [blame] | 701 | mips_gic_base = ioremap_nocache(gic_base_addr, gic_addrspace_size); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 702 | |
Paul Burton | 3680746 | 2017-08-12 21:36:24 -0700 | [diff] [blame^] | 703 | gicconfig = read_gic_config(); |
| 704 | gic_shared_intrs = gicconfig & GIC_CONFIG_NUMINTERRUPTS; |
| 705 | gic_shared_intrs >>= __fls(GIC_CONFIG_NUMINTERRUPTS); |
| 706 | gic_shared_intrs = (gic_shared_intrs + 1) * 8; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 707 | |
Paul Burton | 3680746 | 2017-08-12 21:36:24 -0700 | [diff] [blame^] | 708 | gic_vpes = gicconfig & GIC_CONFIG_PVPS; |
| 709 | gic_vpes >>= __fls(GIC_CONFIG_PVPS); |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 710 | gic_vpes = gic_vpes + 1; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 711 | |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 712 | if (cpu_has_veic) { |
Paul Burton | ba01cf0 | 2016-05-17 15:31:06 +0100 | [diff] [blame] | 713 | /* Set EIC mode for all VPEs */ |
| 714 | for_each_present_cpu(cpu) { |
| 715 | gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), |
| 716 | mips_cm_vp_id(cpu)); |
| 717 | gic_write(GIC_REG(VPE_OTHER, GIC_VPE_CTL), |
| 718 | GIC_VPE_CTL_EIC_MODE_MSK); |
| 719 | } |
| 720 | |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 721 | /* Always use vector 1 in EIC mode */ |
| 722 | gic_cpu_pin = 0; |
James Hogan | 1b6af71 | 2015-01-19 15:38:24 +0000 | [diff] [blame] | 723 | timer_cpu_pin = gic_cpu_pin; |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 724 | set_vi_handler(gic_cpu_pin + GIC_PIN_TO_VEC_OFFSET, |
| 725 | __gic_irq_dispatch); |
| 726 | } else { |
| 727 | gic_cpu_pin = cpu_vec - GIC_CPU_PIN_OFFSET; |
| 728 | irq_set_chained_handler(MIPS_CPU_IRQ_BASE + cpu_vec, |
| 729 | gic_irq_dispatch); |
James Hogan | 1b6af71 | 2015-01-19 15:38:24 +0000 | [diff] [blame] | 730 | /* |
| 731 | * With the CMP implementation of SMP (deprecated), other CPUs |
| 732 | * are started by the bootloader and put into a timer based |
| 733 | * waiting poll loop. We must not re-route those CPU's local |
| 734 | * timer interrupts as the wait instruction will never finish, |
| 735 | * so just handle whatever CPU interrupt it is routed to by |
| 736 | * default. |
| 737 | * |
| 738 | * This workaround should be removed when CMP support is |
| 739 | * dropped. |
| 740 | */ |
| 741 | if (IS_ENABLED(CONFIG_MIPS_CMP) && |
| 742 | gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER)) { |
Markos Chandras | c3f57f0 | 2015-07-14 10:26:09 +0100 | [diff] [blame] | 743 | timer_cpu_pin = gic_read32(GIC_REG(VPE_LOCAL, |
James Hogan | 1b6af71 | 2015-01-19 15:38:24 +0000 | [diff] [blame] | 744 | GIC_VPE_TIMER_MAP)) & |
| 745 | GIC_MAP_MSK; |
| 746 | irq_set_chained_handler(MIPS_CPU_IRQ_BASE + |
| 747 | GIC_CPU_PIN_OFFSET + |
| 748 | timer_cpu_pin, |
| 749 | gic_irq_dispatch); |
| 750 | } else { |
| 751 | timer_cpu_pin = gic_cpu_pin; |
| 752 | } |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Andrew Bresticker | a705727 | 2014-11-12 11:43:38 -0800 | [diff] [blame] | 755 | gic_irq_domain = irq_domain_add_simple(node, GIC_NUM_LOCAL_INTRS + |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 756 | gic_shared_intrs, irqbase, |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 757 | &gic_irq_domain_ops, NULL); |
| 758 | if (!gic_irq_domain) |
| 759 | panic("Failed to add GIC IRQ domain"); |
Steven J. Hill | 0b271f5 | 2012-08-31 16:05:37 -0500 | [diff] [blame] | 760 | |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 761 | gic_ipi_domain = irq_domain_add_hierarchy(gic_irq_domain, |
| 762 | IRQ_DOMAIN_FLAG_IPI_PER_CPU, |
| 763 | GIC_NUM_LOCAL_INTRS + gic_shared_intrs, |
| 764 | node, &gic_ipi_domain_ops, NULL); |
| 765 | if (!gic_ipi_domain) |
| 766 | panic("Failed to add GIC IPI domain"); |
| 767 | |
Marc Zyngier | 96f0d93 | 2017-06-22 11:42:50 +0100 | [diff] [blame] | 768 | irq_domain_update_bus_token(gic_ipi_domain, DOMAIN_BUS_IPI); |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 769 | |
Qais Yousef | 16a8083 | 2015-12-08 13:20:30 +0000 | [diff] [blame] | 770 | if (node && |
| 771 | !of_property_read_u32_array(node, "mti,reserved-ipi-vectors", v, 2)) { |
| 772 | bitmap_set(ipi_resrv, v[0], v[1]); |
| 773 | } else { |
| 774 | /* Make the last 2 * gic_vpes available for IPIs */ |
| 775 | bitmap_set(ipi_resrv, |
| 776 | gic_shared_intrs - 2 * gic_vpes, |
| 777 | 2 * gic_vpes); |
| 778 | } |
Qais Yousef | 2af70a9 | 2015-12-08 13:20:23 +0000 | [diff] [blame] | 779 | |
Paul Burton | f8dcd9e | 2017-04-20 10:07:34 +0100 | [diff] [blame] | 780 | bitmap_copy(ipi_available, ipi_resrv, GIC_MAX_INTRS); |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 781 | gic_basic_init(); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 782 | } |
Andrew Bresticker | a705727 | 2014-11-12 11:43:38 -0800 | [diff] [blame] | 783 | |
| 784 | void __init gic_init(unsigned long gic_base_addr, |
| 785 | unsigned long gic_addrspace_size, |
| 786 | unsigned int cpu_vec, unsigned int irqbase) |
| 787 | { |
| 788 | __gic_init(gic_base_addr, gic_addrspace_size, cpu_vec, irqbase, NULL); |
| 789 | } |
| 790 | |
| 791 | static int __init gic_of_init(struct device_node *node, |
| 792 | struct device_node *parent) |
| 793 | { |
| 794 | struct resource res; |
| 795 | unsigned int cpu_vec, i = 0, reserved = 0; |
| 796 | phys_addr_t gic_base; |
| 797 | size_t gic_len; |
| 798 | |
| 799 | /* Find the first available CPU vector. */ |
| 800 | while (!of_property_read_u32_index(node, "mti,reserved-cpu-vectors", |
| 801 | i++, &cpu_vec)) |
| 802 | reserved |= BIT(cpu_vec); |
| 803 | for (cpu_vec = 2; cpu_vec < 8; cpu_vec++) { |
| 804 | if (!(reserved & BIT(cpu_vec))) |
| 805 | break; |
| 806 | } |
| 807 | if (cpu_vec == 8) { |
| 808 | pr_err("No CPU vectors available for GIC\n"); |
| 809 | return -ENODEV; |
| 810 | } |
| 811 | |
| 812 | if (of_address_to_resource(node, 0, &res)) { |
| 813 | /* |
| 814 | * Probe the CM for the GIC base address if not specified |
| 815 | * in the device-tree. |
| 816 | */ |
| 817 | if (mips_cm_present()) { |
| 818 | gic_base = read_gcr_gic_base() & |
Paul Burton | 93c5bba5 | 2017-08-12 19:49:27 -0700 | [diff] [blame] | 819 | ~CM_GCR_GIC_BASE_GICEN; |
Andrew Bresticker | a705727 | 2014-11-12 11:43:38 -0800 | [diff] [blame] | 820 | gic_len = 0x20000; |
| 821 | } else { |
| 822 | pr_err("Failed to get GIC memory range\n"); |
| 823 | return -ENODEV; |
| 824 | } |
| 825 | } else { |
| 826 | gic_base = res.start; |
| 827 | gic_len = resource_size(&res); |
| 828 | } |
| 829 | |
James Hogan | a0ffec3 | 2017-08-12 21:36:09 -0700 | [diff] [blame] | 830 | if (mips_cm_present()) { |
Paul Burton | 93c5bba5 | 2017-08-12 19:49:27 -0700 | [diff] [blame] | 831 | write_gcr_gic_base(gic_base | CM_GCR_GIC_BASE_GICEN); |
James Hogan | a0ffec3 | 2017-08-12 21:36:09 -0700 | [diff] [blame] | 832 | /* Ensure GIC region is enabled before trying to access it */ |
| 833 | __sync(); |
| 834 | } |
Andrew Bresticker | a705727 | 2014-11-12 11:43:38 -0800 | [diff] [blame] | 835 | gic_present = true; |
| 836 | |
| 837 | __gic_init(gic_base, gic_len, cpu_vec, 0, node); |
| 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | IRQCHIP_DECLARE(mips_gic, "mti,gic", gic_of_init); |