blob: 655cb967a1f2fe0b9c41f5d3484dbebe6be7331d [file] [log] [blame]
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +02001/*
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
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/irq.h>
20#include <linux/interrupt.h>
Joel Porquet41a83e02015-07-07 17:11:46 -040021#include <linux/irqchip.h>
Ezequiel Garciabc69b8a2014-02-10 17:00:02 -030022#include <linux/irqchip/chained_irq.h>
Thomas Petazzonid7df84b2014-04-14 15:54:02 +020023#include <linux/cpu.h>
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020024#include <linux/io.h>
25#include <linux/of_address.h>
26#include <linux/of_irq.h>
Thomas Petazzoni31f614e2013-08-09 22:27:11 +020027#include <linux/of_pci.h>
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020028#include <linux/irqdomain.h>
Thomas Petazzoni31f614e2013-08-09 22:27:11 +020029#include <linux/slab.h>
Thomas Petazzoni0f077eb2014-11-21 17:00:00 +010030#include <linux/syscore_ops.h>
Thomas Petazzoni31f614e2013-08-09 22:27:11 +020031#include <linux/msi.h>
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020032#include <asm/mach/arch.h>
33#include <asm/exception.h>
Gregory CLEMENT344e8732012-08-02 11:19:12 +030034#include <asm/smp_plat.h>
Thomas Petazzoni9339d432013-04-09 23:26:15 +020035#include <asm/mach/irq.h>
36
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020037/* Interrupt Controller Registers Map */
38#define ARMADA_370_XP_INT_SET_MASK_OFFS (0x48)
39#define ARMADA_370_XP_INT_CLEAR_MASK_OFFS (0x4C)
Maxime Ripard28da06d2015-03-03 11:43:16 +010040#define ARMADA_370_XP_INT_FABRIC_MASK_OFFS (0x54)
41#define ARMADA_370_XP_INT_CAUSE_PERF(cpu) (1 << cpu)
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020042
Ben Dooksf3e16cc2012-06-04 18:50:12 +020043#define ARMADA_370_XP_INT_CONTROL (0x00)
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020044#define ARMADA_370_XP_INT_SET_ENABLE_OFFS (0x30)
45#define ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS (0x34)
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010046#define ARMADA_370_XP_INT_SOURCE_CTL(irq) (0x100 + irq*4)
Thomas Gleixner8cc3cfc2014-03-04 20:43:41 +000047#define ARMADA_370_XP_INT_SOURCE_CPU_MASK 0xF
Grzegorz Jaszczyk758e8362014-09-25 13:17:19 +020048#define ARMADA_370_XP_INT_IRQ_FIQ_MASK(cpuid) ((BIT(0) | BIT(8)) << cpuid)
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020049
50#define ARMADA_370_XP_CPU_INTACK_OFFS (0x44)
Ezequiel Garciabc69b8a2014-02-10 17:00:02 -030051#define ARMADA_375_PPI_CAUSE (0x10)
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020052
Gregory CLEMENT344e8732012-08-02 11:19:12 +030053#define ARMADA_370_XP_SW_TRIG_INT_OFFS (0x4)
54#define ARMADA_370_XP_IN_DRBEL_MSK_OFFS (0xc)
55#define ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS (0x8)
56
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010057#define ARMADA_370_XP_MAX_PER_CPU_IRQS (28)
58
Gregory CLEMENT7f23f622013-03-20 16:09:35 +010059#define ARMADA_370_XP_TIMER0_PER_CPU_IRQ (5)
Maxime Ripard28da06d2015-03-03 11:43:16 +010060#define ARMADA_370_XP_FABRIC_IRQ (3)
Gregory CLEMENT7f23f622013-03-20 16:09:35 +010061
Thomas Petazzoni5ec69012013-04-09 23:26:17 +020062#define IPI_DOORBELL_START (0)
63#define IPI_DOORBELL_END (8)
64#define IPI_DOORBELL_MASK 0xFF
Thomas Petazzoni31f614e2013-08-09 22:27:11 +020065#define PCI_MSI_DOORBELL_START (16)
66#define PCI_MSI_DOORBELL_NR (16)
67#define PCI_MSI_DOORBELL_END (32)
68#define PCI_MSI_DOORBELL_MASK 0xFFFF0000
Gregory CLEMENT344e8732012-08-02 11:19:12 +030069
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020070static void __iomem *per_cpu_int_base;
71static void __iomem *main_int_base;
72static struct irq_domain *armada_370_xp_mpic_domain;
Thomas Petazzoni0f077eb2014-11-21 17:00:00 +010073static u32 doorbell_mask_reg;
Maxime Ripard5724be82015-03-03 11:27:23 +010074static int parent_irq;
Thomas Petazzoni31f614e2013-08-09 22:27:11 +020075#ifdef CONFIG_PCI_MSI
76static struct irq_domain *armada_370_xp_msi_domain;
77static DECLARE_BITMAP(msi_used, PCI_MSI_DOORBELL_NR);
78static DEFINE_MUTEX(msi_used_lock);
79static phys_addr_t msi_doorbell_addr;
80#endif
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020081
Ezequiel Garcia2c299de2015-03-03 11:43:15 +010082static inline bool is_percpu_irq(irq_hw_number_t irq)
83{
84 switch (irq) {
85 case ARMADA_370_XP_TIMER0_PER_CPU_IRQ:
Maxime Ripard28da06d2015-03-03 11:43:16 +010086 case ARMADA_370_XP_FABRIC_IRQ:
Ezequiel Garcia2c299de2015-03-03 11:43:15 +010087 return true;
88 default:
89 return false;
90 }
91}
92
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010093/*
94 * In SMP mode:
95 * For shared global interrupts, mask/unmask global enable bit
Marek Belisko097ef182013-03-15 23:34:04 +010096 * For CPU interrupts, mask/unmask the calling CPU's bit
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010097 */
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020098static void armada_370_xp_irq_mask(struct irq_data *d)
99{
Gregory CLEMENT3202bf02012-12-05 21:43:23 +0100100 irq_hw_number_t hwirq = irqd_to_hwirq(d);
101
Ezequiel Garcia2c299de2015-03-03 11:43:15 +0100102 if (!is_percpu_irq(hwirq))
Gregory CLEMENT3202bf02012-12-05 21:43:23 +0100103 writel(hwirq, main_int_base +
104 ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS);
105 else
106 writel(hwirq, per_cpu_int_base +
107 ARMADA_370_XP_INT_SET_MASK_OFFS);
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200108}
109
110static void armada_370_xp_irq_unmask(struct irq_data *d)
111{
Gregory CLEMENT3202bf02012-12-05 21:43:23 +0100112 irq_hw_number_t hwirq = irqd_to_hwirq(d);
113
Ezequiel Garcia2c299de2015-03-03 11:43:15 +0100114 if (!is_percpu_irq(hwirq))
Gregory CLEMENT3202bf02012-12-05 21:43:23 +0100115 writel(hwirq, main_int_base +
116 ARMADA_370_XP_INT_SET_ENABLE_OFFS);
117 else
118 writel(hwirq, per_cpu_int_base +
119 ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200120}
121
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200122#ifdef CONFIG_PCI_MSI
123
124static int armada_370_xp_alloc_msi(void)
125{
126 int hwirq;
127
128 mutex_lock(&msi_used_lock);
129 hwirq = find_first_zero_bit(&msi_used, PCI_MSI_DOORBELL_NR);
130 if (hwirq >= PCI_MSI_DOORBELL_NR)
131 hwirq = -ENOSPC;
132 else
133 set_bit(hwirq, msi_used);
134 mutex_unlock(&msi_used_lock);
135
136 return hwirq;
137}
138
139static void armada_370_xp_free_msi(int hwirq)
140{
141 mutex_lock(&msi_used_lock);
142 if (!test_bit(hwirq, msi_used))
143 pr_err("trying to free unused MSI#%d\n", hwirq);
144 else
145 clear_bit(hwirq, msi_used);
146 mutex_unlock(&msi_used_lock);
147}
148
Yijing Wangc2791b82014-11-11 17:45:45 -0700149static int armada_370_xp_setup_msi_irq(struct msi_controller *chip,
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200150 struct pci_dev *pdev,
151 struct msi_desc *desc)
152{
153 struct msi_msg msg;
Thomas Petazzonida343fc2014-04-18 14:19:47 +0200154 int virq, hwirq;
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200155
Alexander Gordeev39301152014-09-07 20:57:54 +0200156 /* We support MSI, but not MSI-X */
157 if (desc->msi_attrib.is_msix)
158 return -EINVAL;
159
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200160 hwirq = armada_370_xp_alloc_msi();
161 if (hwirq < 0)
162 return hwirq;
163
164 virq = irq_create_mapping(armada_370_xp_msi_domain, hwirq);
165 if (!virq) {
166 armada_370_xp_free_msi(hwirq);
167 return -EINVAL;
168 }
169
170 irq_set_msi_desc(virq, desc);
171
172 msg.address_lo = msi_doorbell_addr;
173 msg.address_hi = 0;
174 msg.data = 0xf00 | (hwirq + 16);
175
Jiang Liu83a18912014-11-09 23:10:34 +0800176 pci_write_msi_msg(virq, &msg);
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200177 return 0;
178}
179
Yijing Wangc2791b82014-11-11 17:45:45 -0700180static void armada_370_xp_teardown_msi_irq(struct msi_controller *chip,
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200181 unsigned int irq)
182{
183 struct irq_data *d = irq_get_irq_data(irq);
Neil Greatorexff3c6642014-04-18 14:19:49 +0200184 unsigned long hwirq = d->hwirq;
185
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200186 irq_dispose_mapping(irq);
Neil Greatorexff3c6642014-04-18 14:19:49 +0200187 armada_370_xp_free_msi(hwirq);
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200188}
189
190static struct irq_chip armada_370_xp_msi_irq_chip = {
191 .name = "armada_370_xp_msi_irq",
Thomas Gleixner280510f2014-11-23 12:23:20 +0100192 .irq_enable = pci_msi_unmask_irq,
193 .irq_disable = pci_msi_mask_irq,
194 .irq_mask = pci_msi_mask_irq,
195 .irq_unmask = pci_msi_unmask_irq,
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200196};
197
198static int armada_370_xp_msi_map(struct irq_domain *domain, unsigned int virq,
199 irq_hw_number_t hw)
200{
201 irq_set_chip_and_handler(virq, &armada_370_xp_msi_irq_chip,
202 handle_simple_irq);
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200203
204 return 0;
205}
206
207static const struct irq_domain_ops armada_370_xp_msi_irq_ops = {
208 .map = armada_370_xp_msi_map,
209};
210
211static int armada_370_xp_msi_init(struct device_node *node,
212 phys_addr_t main_int_phys_base)
213{
Yijing Wangc2791b82014-11-11 17:45:45 -0700214 struct msi_controller *msi_chip;
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200215 u32 reg;
216 int ret;
217
218 msi_doorbell_addr = main_int_phys_base +
219 ARMADA_370_XP_SW_TRIG_INT_OFFS;
220
221 msi_chip = kzalloc(sizeof(*msi_chip), GFP_KERNEL);
222 if (!msi_chip)
223 return -ENOMEM;
224
225 msi_chip->setup_irq = armada_370_xp_setup_msi_irq;
226 msi_chip->teardown_irq = armada_370_xp_teardown_msi_irq;
227 msi_chip->of_node = node;
228
229 armada_370_xp_msi_domain =
230 irq_domain_add_linear(NULL, PCI_MSI_DOORBELL_NR,
231 &armada_370_xp_msi_irq_ops,
232 NULL);
233 if (!armada_370_xp_msi_domain) {
234 kfree(msi_chip);
235 return -ENOMEM;
236 }
237
238 ret = of_pci_msi_chip_add(msi_chip);
239 if (ret < 0) {
240 irq_domain_remove(armada_370_xp_msi_domain);
241 kfree(msi_chip);
242 return ret;
243 }
244
245 reg = readl(per_cpu_int_base + ARMADA_370_XP_IN_DRBEL_MSK_OFFS)
246 | PCI_MSI_DOORBELL_MASK;
247
248 writel(reg, per_cpu_int_base +
249 ARMADA_370_XP_IN_DRBEL_MSK_OFFS);
250
251 /* Unmask IPI interrupt */
252 writel(1, per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
253
254 return 0;
255}
256#else
257static inline int armada_370_xp_msi_init(struct device_node *node,
258 phys_addr_t main_int_phys_base)
259{
260 return 0;
261}
262#endif
263
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300264#ifdef CONFIG_SMP
Arnaud Ebalard19e61d42014-01-20 22:52:05 +0100265static DEFINE_RAW_SPINLOCK(irq_controller_lock);
266
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300267static int armada_xp_set_affinity(struct irq_data *d,
268 const struct cpumask *mask_val, bool force)
269{
Gregory CLEMENT3202bf02012-12-05 21:43:23 +0100270 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Thomas Gleixner8cc3cfc2014-03-04 20:43:41 +0000271 unsigned long reg, mask;
Gregory CLEMENT3202bf02012-12-05 21:43:23 +0100272 int cpu;
273
Thomas Gleixner8cc3cfc2014-03-04 20:43:41 +0000274 /* Select a single core from the affinity mask which is online */
275 cpu = cpumask_any_and(mask_val, cpu_online_mask);
276 mask = 1UL << cpu_logical_map(cpu);
Gregory CLEMENT3202bf02012-12-05 21:43:23 +0100277
278 raw_spin_lock(&irq_controller_lock);
Gregory CLEMENT3202bf02012-12-05 21:43:23 +0100279 reg = readl(main_int_base + ARMADA_370_XP_INT_SOURCE_CTL(hwirq));
Thomas Gleixner8cc3cfc2014-03-04 20:43:41 +0000280 reg = (reg & (~ARMADA_370_XP_INT_SOURCE_CPU_MASK)) | mask;
Gregory CLEMENT3202bf02012-12-05 21:43:23 +0100281 writel(reg, main_int_base + ARMADA_370_XP_INT_SOURCE_CTL(hwirq));
Gregory CLEMENT3202bf02012-12-05 21:43:23 +0100282 raw_spin_unlock(&irq_controller_lock);
283
Thomas Petazzoni1dacf192014-10-24 13:59:16 +0200284 return IRQ_SET_MASK_OK;
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300285}
286#endif
287
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200288static struct irq_chip armada_370_xp_irq_chip = {
289 .name = "armada_370_xp_irq",
290 .irq_mask = armada_370_xp_irq_mask,
291 .irq_mask_ack = armada_370_xp_irq_mask,
292 .irq_unmask = armada_370_xp_irq_unmask,
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300293#ifdef CONFIG_SMP
294 .irq_set_affinity = armada_xp_set_affinity,
295#endif
Gregory CLEMENT0d8e1d82015-03-30 16:04:37 +0200296 .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MASK_ON_SUSPEND,
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200297};
298
299static int armada_370_xp_mpic_irq_map(struct irq_domain *h,
300 unsigned int virq, irq_hw_number_t hw)
301{
302 armada_370_xp_irq_mask(irq_get_irq_data(virq));
Ezequiel Garcia2c299de2015-03-03 11:43:15 +0100303 if (!is_percpu_irq(hw))
Gregory CLEMENT600468d2013-04-05 14:32:52 +0200304 writel(hw, per_cpu_int_base +
305 ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
306 else
307 writel(hw, main_int_base + ARMADA_370_XP_INT_SET_ENABLE_OFFS);
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200308 irq_set_status_flags(virq, IRQ_LEVEL);
Gregory CLEMENT3a6f08a2013-01-25 18:32:41 +0100309
Ezequiel Garcia2c299de2015-03-03 11:43:15 +0100310 if (is_percpu_irq(hw)) {
Gregory CLEMENT3a6f08a2013-01-25 18:32:41 +0100311 irq_set_percpu_devid(virq);
312 irq_set_chip_and_handler(virq, &armada_370_xp_irq_chip,
313 handle_percpu_devid_irq);
314
315 } else {
316 irq_set_chip_and_handler(virq, &armada_370_xp_irq_chip,
317 handle_level_irq);
318 }
Rob Herringd17cab42015-08-29 18:01:22 -0500319 irq_set_probe(virq);
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200320
321 return 0;
322}
323
Thomas Petazzonid7df84b2014-04-14 15:54:02 +0200324static void armada_xp_mpic_smp_cpu_init(void)
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300325{
Thomas Petazzonib73842b2014-05-30 22:18:18 +0200326 u32 control;
327 int nr_irqs, i;
328
329 control = readl(main_int_base + ARMADA_370_XP_INT_CONTROL);
330 nr_irqs = (control >> 2) & 0x3ff;
331
332 for (i = 0; i < nr_irqs; i++)
333 writel(i, per_cpu_int_base + ARMADA_370_XP_INT_SET_MASK_OFFS);
334
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300335 /* Clear pending IPIs */
336 writel(0, per_cpu_int_base + ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
337
338 /* Enable first 8 IPIs */
Thomas Petazzoni5ec69012013-04-09 23:26:17 +0200339 writel(IPI_DOORBELL_MASK, per_cpu_int_base +
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300340 ARMADA_370_XP_IN_DRBEL_MSK_OFFS);
341
342 /* Unmask IPI interrupt */
343 writel(0, per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
344}
Thomas Petazzonid7df84b2014-04-14 15:54:02 +0200345
Maxime Ripard28da06d2015-03-03 11:43:16 +0100346static void armada_xp_mpic_perf_init(void)
347{
348 unsigned long cpuid = cpu_logical_map(smp_processor_id());
349
350 /* Enable Performance Counter Overflow interrupts */
351 writel(ARMADA_370_XP_INT_CAUSE_PERF(cpuid),
352 per_cpu_int_base + ARMADA_370_XP_INT_FABRIC_MASK_OFFS);
353}
354
Ezequiel Garcia933a24b2015-03-03 11:43:14 +0100355#ifdef CONFIG_SMP
356static void armada_mpic_send_doorbell(const struct cpumask *mask,
357 unsigned int irq)
358{
359 int cpu;
360 unsigned long map = 0;
361
362 /* Convert our logical CPU mask into a physical one. */
363 for_each_cpu(cpu, mask)
364 map |= 1 << cpu_logical_map(cpu);
365
366 /*
367 * Ensure that stores to Normal memory are visible to the
368 * other CPUs before issuing the IPI.
369 */
370 dsb();
371
372 /* submit softirq */
373 writel((map << 8) | irq, main_int_base +
374 ARMADA_370_XP_SW_TRIG_INT_OFFS);
375}
376
Thomas Petazzonid7df84b2014-04-14 15:54:02 +0200377static int armada_xp_mpic_secondary_init(struct notifier_block *nfb,
378 unsigned long action, void *hcpu)
379{
Maxime Ripard28da06d2015-03-03 11:43:16 +0100380 if (action == CPU_STARTING || action == CPU_STARTING_FROZEN) {
381 armada_xp_mpic_perf_init();
Thomas Petazzonid7df84b2014-04-14 15:54:02 +0200382 armada_xp_mpic_smp_cpu_init();
Maxime Ripard28da06d2015-03-03 11:43:16 +0100383 }
Maxime Ripard5724be82015-03-03 11:27:23 +0100384
Thomas Petazzonid7df84b2014-04-14 15:54:02 +0200385 return NOTIFY_OK;
386}
387
388static struct notifier_block armada_370_xp_mpic_cpu_notifier = {
389 .notifier_call = armada_xp_mpic_secondary_init,
390 .priority = 100,
391};
392
Maxime Ripard5724be82015-03-03 11:27:23 +0100393static int mpic_cascaded_secondary_init(struct notifier_block *nfb,
394 unsigned long action, void *hcpu)
395{
Maxime Ripard28da06d2015-03-03 11:43:16 +0100396 if (action == CPU_STARTING || action == CPU_STARTING_FROZEN) {
397 armada_xp_mpic_perf_init();
Maxime Ripard5724be82015-03-03 11:27:23 +0100398 enable_percpu_irq(parent_irq, IRQ_TYPE_NONE);
Maxime Ripard28da06d2015-03-03 11:43:16 +0100399 }
Maxime Ripard5724be82015-03-03 11:27:23 +0100400
401 return NOTIFY_OK;
402}
403
404static struct notifier_block mpic_cascaded_cpu_notifier = {
405 .notifier_call = mpic_cascaded_secondary_init,
406 .priority = 100,
407};
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300408#endif /* CONFIG_SMP */
409
Krzysztof Kozlowski96009732015-04-27 21:54:24 +0900410static const struct irq_domain_ops armada_370_xp_mpic_irq_ops = {
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200411 .map = armada_370_xp_mpic_irq_map,
412 .xlate = irq_domain_xlate_onecell,
413};
414
Ezequiel Garcia9b8cf772014-02-10 17:00:01 -0300415#ifdef CONFIG_PCI_MSI
Ezequiel Garciabc69b8a2014-02-10 17:00:02 -0300416static void armada_370_xp_handle_msi_irq(struct pt_regs *regs, bool is_chained)
Ezequiel Garcia9b8cf772014-02-10 17:00:01 -0300417{
418 u32 msimask, msinr;
419
420 msimask = readl_relaxed(per_cpu_int_base +
421 ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS)
422 & PCI_MSI_DOORBELL_MASK;
423
424 writel(~msimask, per_cpu_int_base +
425 ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
426
427 for (msinr = PCI_MSI_DOORBELL_START;
428 msinr < PCI_MSI_DOORBELL_END; msinr++) {
429 int irq;
430
431 if (!(msimask & BIT(msinr)))
432 continue;
433
Marc Zyngiere89c6a02014-08-26 11:03:21 +0100434 if (is_chained) {
435 irq = irq_find_mapping(armada_370_xp_msi_domain,
436 msinr - 16);
Ezequiel Garciabc69b8a2014-02-10 17:00:02 -0300437 generic_handle_irq(irq);
Marc Zyngiere89c6a02014-08-26 11:03:21 +0100438 } else {
439 irq = msinr - 16;
440 handle_domain_irq(armada_370_xp_msi_domain,
441 irq, regs);
442 }
Ezequiel Garcia9b8cf772014-02-10 17:00:01 -0300443 }
444}
445#else
Ezequiel Garciabc69b8a2014-02-10 17:00:02 -0300446static void armada_370_xp_handle_msi_irq(struct pt_regs *r, bool b) {}
Ezequiel Garcia9b8cf772014-02-10 17:00:01 -0300447#endif
448
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200449static void armada_370_xp_mpic_handle_cascade_irq(struct irq_desc *desc)
Ezequiel Garciabc69b8a2014-02-10 17:00:02 -0300450{
Jiang Liu5b292642015-06-04 12:13:20 +0800451 struct irq_chip *chip = irq_desc_get_chip(desc);
Grzegorz Jaszczyk758e8362014-09-25 13:17:19 +0200452 unsigned long irqmap, irqn, irqsrc, cpuid;
Ezequiel Garciabc69b8a2014-02-10 17:00:02 -0300453 unsigned int cascade_irq;
454
455 chained_irq_enter(chip, desc);
456
457 irqmap = readl_relaxed(per_cpu_int_base + ARMADA_375_PPI_CAUSE);
Grzegorz Jaszczyk758e8362014-09-25 13:17:19 +0200458 cpuid = cpu_logical_map(smp_processor_id());
Ezequiel Garciabc69b8a2014-02-10 17:00:02 -0300459
460 for_each_set_bit(irqn, &irqmap, BITS_PER_LONG) {
Grzegorz Jaszczyk758e8362014-09-25 13:17:19 +0200461 irqsrc = readl_relaxed(main_int_base +
462 ARMADA_370_XP_INT_SOURCE_CTL(irqn));
463
464 /* Check if the interrupt is not masked on current CPU.
465 * Test IRQ (0-1) and FIQ (8-9) mask bits.
466 */
467 if (!(irqsrc & ARMADA_370_XP_INT_IRQ_FIQ_MASK(cpuid)))
468 continue;
469
470 if (irqn == 1) {
471 armada_370_xp_handle_msi_irq(NULL, true);
472 continue;
473 }
474
Ezequiel Garciabc69b8a2014-02-10 17:00:02 -0300475 cascade_irq = irq_find_mapping(armada_370_xp_mpic_domain, irqn);
476 generic_handle_irq(cascade_irq);
477 }
478
479 chained_irq_exit(chip, desc);
480}
481
Stephen Boyd8783dd32014-03-04 16:40:30 -0800482static void __exception_irq_entry
Thomas Petazzoni9339d432013-04-09 23:26:15 +0200483armada_370_xp_handle_irq(struct pt_regs *regs)
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200484{
485 u32 irqstat, irqnr;
486
487 do {
488 irqstat = readl_relaxed(per_cpu_int_base +
489 ARMADA_370_XP_CPU_INTACK_OFFS);
490 irqnr = irqstat & 0x3FF;
491
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300492 if (irqnr > 1022)
493 break;
494
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200495 if (irqnr > 1) {
Marc Zyngiere89c6a02014-08-26 11:03:21 +0100496 handle_domain_irq(armada_370_xp_mpic_domain,
497 irqnr, regs);
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200498 continue;
499 }
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200500
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200501 /* MSI handling */
Ezequiel Garcia9b8cf772014-02-10 17:00:01 -0300502 if (irqnr == 1)
Ezequiel Garciabc69b8a2014-02-10 17:00:02 -0300503 armada_370_xp_handle_msi_irq(regs, false);
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200504
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300505#ifdef CONFIG_SMP
506 /* IPI Handling */
507 if (irqnr == 0) {
508 u32 ipimask, ipinr;
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200509
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300510 ipimask = readl_relaxed(per_cpu_int_base +
511 ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS)
Thomas Petazzoni5ec69012013-04-09 23:26:17 +0200512 & IPI_DOORBELL_MASK;
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300513
Lior Amsalema6f089e2013-11-25 17:26:44 +0100514 writel(~ipimask, per_cpu_int_base +
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300515 ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
516
517 /* Handle all pending doorbells */
Thomas Petazzoni5ec69012013-04-09 23:26:17 +0200518 for (ipinr = IPI_DOORBELL_START;
519 ipinr < IPI_DOORBELL_END; ipinr++) {
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300520 if (ipimask & (0x1 << ipinr))
521 handle_IPI(ipinr, regs);
522 }
523 continue;
524 }
525#endif
526
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200527 } while (1);
528}
529
Thomas Petazzoni0f077eb2014-11-21 17:00:00 +0100530static int armada_370_xp_mpic_suspend(void)
531{
532 doorbell_mask_reg = readl(per_cpu_int_base +
533 ARMADA_370_XP_IN_DRBEL_MSK_OFFS);
534 return 0;
535}
536
537static void armada_370_xp_mpic_resume(void)
538{
539 int nirqs;
540 irq_hw_number_t irq;
541
542 /* Re-enable interrupts */
543 nirqs = (readl(main_int_base + ARMADA_370_XP_INT_CONTROL) >> 2) & 0x3ff;
544 for (irq = 0; irq < nirqs; irq++) {
545 struct irq_data *data;
546 int virq;
547
548 virq = irq_linear_revmap(armada_370_xp_mpic_domain, irq);
549 if (virq == 0)
550 continue;
551
552 if (irq != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
553 writel(irq, per_cpu_int_base +
554 ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
555 else
556 writel(irq, main_int_base +
557 ARMADA_370_XP_INT_SET_ENABLE_OFFS);
558
559 data = irq_get_irq_data(virq);
560 if (!irqd_irq_disabled(data))
561 armada_370_xp_irq_unmask(data);
562 }
563
564 /* Reconfigure doorbells for IPIs and MSIs */
565 writel(doorbell_mask_reg,
566 per_cpu_int_base + ARMADA_370_XP_IN_DRBEL_MSK_OFFS);
567 if (doorbell_mask_reg & IPI_DOORBELL_MASK)
568 writel(0, per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
569 if (doorbell_mask_reg & PCI_MSI_DOORBELL_MASK)
570 writel(1, per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
571}
572
573struct syscore_ops armada_370_xp_mpic_syscore_ops = {
574 .suspend = armada_370_xp_mpic_suspend,
575 .resume = armada_370_xp_mpic_resume,
576};
577
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200578static int __init armada_370_xp_mpic_of_init(struct device_node *node,
579 struct device_node *parent)
580{
Thomas Petazzoni627dfcc2013-08-09 22:27:10 +0200581 struct resource main_int_res, per_cpu_int_res;
Maxime Ripard5724be82015-03-03 11:27:23 +0100582 int nr_irqs, i;
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200583 u32 control;
584
Thomas Petazzoni627dfcc2013-08-09 22:27:10 +0200585 BUG_ON(of_address_to_resource(node, 0, &main_int_res));
586 BUG_ON(of_address_to_resource(node, 1, &per_cpu_int_res));
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200587
Thomas Petazzoni627dfcc2013-08-09 22:27:10 +0200588 BUG_ON(!request_mem_region(main_int_res.start,
589 resource_size(&main_int_res),
590 node->full_name));
591 BUG_ON(!request_mem_region(per_cpu_int_res.start,
592 resource_size(&per_cpu_int_res),
593 node->full_name));
594
595 main_int_base = ioremap(main_int_res.start,
596 resource_size(&main_int_res));
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200597 BUG_ON(!main_int_base);
Thomas Petazzoni627dfcc2013-08-09 22:27:10 +0200598
599 per_cpu_int_base = ioremap(per_cpu_int_res.start,
600 resource_size(&per_cpu_int_res));
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200601 BUG_ON(!per_cpu_int_base);
Gregory CLEMENTd792b1e2012-09-26 18:02:48 +0200602
603 control = readl(main_int_base + ARMADA_370_XP_INT_CONTROL);
Thomas Petazzonib73842b2014-05-30 22:18:18 +0200604 nr_irqs = (control >> 2) & 0x3ff;
605
606 for (i = 0; i < nr_irqs; i++)
607 writel(i, main_int_base + ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS);
Gregory CLEMENTd792b1e2012-09-26 18:02:48 +0200608
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200609 armada_370_xp_mpic_domain =
Thomas Petazzonib73842b2014-05-30 22:18:18 +0200610 irq_domain_add_linear(node, nr_irqs,
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200611 &armada_370_xp_mpic_irq_ops, NULL);
612
Thomas Petazzoni627dfcc2013-08-09 22:27:10 +0200613 BUG_ON(!armada_370_xp_mpic_domain);
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200614
Ezequiel Garcia933a24b2015-03-03 11:43:14 +0100615 /* Setup for the boot CPU */
Maxime Ripard28da06d2015-03-03 11:43:16 +0100616 armada_xp_mpic_perf_init();
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200617 armada_xp_mpic_smp_cpu_init();
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200618
Thomas Petazzoni31f614e2013-08-09 22:27:11 +0200619 armada_370_xp_msi_init(node, main_int_res.start);
620
Ezequiel Garciabc69b8a2014-02-10 17:00:02 -0300621 parent_irq = irq_of_parse_and_map(node, 0);
622 if (parent_irq <= 0) {
623 irq_set_default_host(armada_370_xp_mpic_domain);
624 set_handle_irq(armada_370_xp_handle_irq);
Thomas Petazzonief37d332014-04-14 15:54:01 +0200625#ifdef CONFIG_SMP
626 set_smp_cross_call(armada_mpic_send_doorbell);
Thomas Petazzonid7df84b2014-04-14 15:54:02 +0200627 register_cpu_notifier(&armada_370_xp_mpic_cpu_notifier);
Thomas Petazzonief37d332014-04-14 15:54:01 +0200628#endif
Ezequiel Garciabc69b8a2014-02-10 17:00:02 -0300629 } else {
Maxime Ripard5724be82015-03-03 11:27:23 +0100630#ifdef CONFIG_SMP
631 register_cpu_notifier(&mpic_cascaded_cpu_notifier);
632#endif
Ezequiel Garciabc69b8a2014-02-10 17:00:02 -0300633 irq_set_chained_handler(parent_irq,
634 armada_370_xp_mpic_handle_cascade_irq);
635 }
Thomas Petazzonib313ada2013-04-09 23:26:16 +0200636
Thomas Petazzoni0f077eb2014-11-21 17:00:00 +0100637 register_syscore_ops(&armada_370_xp_mpic_syscore_ops);
638
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200639 return 0;
640}
641
Thomas Petazzoni9339d432013-04-09 23:26:15 +0200642IRQCHIP_DECLARE(armada_370_xp_mpic, "marvell,mpic", armada_370_xp_mpic_of_init);