18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Hisilicon HiP04 INTC
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2002-2014 ARM Limited.
68c2ecf20Sopenharmony_ci * Copyright (c) 2013-2014 Hisilicon Ltd.
78c2ecf20Sopenharmony_ci * Copyright (c) 2013-2014 Linaro Ltd.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Interrupt architecture for the HIP04 INTC:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * o There is one Interrupt Distributor, which receives interrupts
128c2ecf20Sopenharmony_ci *   from system devices and sends them to the Interrupt Controllers.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * o There is one CPU Interface per CPU, which sends interrupts sent
158c2ecf20Sopenharmony_ci *   by the Distributor, and interrupts generated locally, to the
168c2ecf20Sopenharmony_ci *   associated CPU. The base address of the CPU interface is usually
178c2ecf20Sopenharmony_ci *   aliased so that the same address points to different chips depending
188c2ecf20Sopenharmony_ci *   on the CPU it is accessed from.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * Note that IRQs 0-31 are special - they are local to each CPU.
218c2ecf20Sopenharmony_ci * As such, the enable set/clear, pending set/clear and active bit
228c2ecf20Sopenharmony_ci * registers are banked per-cpu for these sources.
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <linux/init.h>
268c2ecf20Sopenharmony_ci#include <linux/kernel.h>
278c2ecf20Sopenharmony_ci#include <linux/err.h>
288c2ecf20Sopenharmony_ci#include <linux/module.h>
298c2ecf20Sopenharmony_ci#include <linux/list.h>
308c2ecf20Sopenharmony_ci#include <linux/smp.h>
318c2ecf20Sopenharmony_ci#include <linux/cpu.h>
328c2ecf20Sopenharmony_ci#include <linux/cpu_pm.h>
338c2ecf20Sopenharmony_ci#include <linux/cpumask.h>
348c2ecf20Sopenharmony_ci#include <linux/io.h>
358c2ecf20Sopenharmony_ci#include <linux/of.h>
368c2ecf20Sopenharmony_ci#include <linux/of_address.h>
378c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
388c2ecf20Sopenharmony_ci#include <linux/irqdomain.h>
398c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
408c2ecf20Sopenharmony_ci#include <linux/slab.h>
418c2ecf20Sopenharmony_ci#include <linux/irqchip.h>
428c2ecf20Sopenharmony_ci#include <linux/irqchip/arm-gic.h>
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#include <asm/irq.h>
458c2ecf20Sopenharmony_ci#include <asm/exception.h>
468c2ecf20Sopenharmony_ci#include <asm/smp_plat.h>
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#include "irq-gic-common.h"
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define HIP04_MAX_IRQS		510
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistruct hip04_irq_data {
538c2ecf20Sopenharmony_ci	void __iomem *dist_base;
548c2ecf20Sopenharmony_ci	void __iomem *cpu_base;
558c2ecf20Sopenharmony_ci	struct irq_domain *domain;
568c2ecf20Sopenharmony_ci	unsigned int nr_irqs;
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic DEFINE_RAW_SPINLOCK(irq_controller_lock);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/*
628c2ecf20Sopenharmony_ci * The GIC mapping of CPU interfaces does not necessarily match
638c2ecf20Sopenharmony_ci * the logical CPU numbering.  Let's use a mapping as returned
648c2ecf20Sopenharmony_ci * by the GIC itself.
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_ci#define NR_HIP04_CPU_IF 16
678c2ecf20Sopenharmony_cistatic u16 hip04_cpu_map[NR_HIP04_CPU_IF] __read_mostly;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic struct hip04_irq_data hip04_data __read_mostly;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic inline void __iomem *hip04_dist_base(struct irq_data *d)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	struct hip04_irq_data *hip04_data = irq_data_get_irq_chip_data(d);
748c2ecf20Sopenharmony_ci	return hip04_data->dist_base;
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic inline void __iomem *hip04_cpu_base(struct irq_data *d)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	struct hip04_irq_data *hip04_data = irq_data_get_irq_chip_data(d);
808c2ecf20Sopenharmony_ci	return hip04_data->cpu_base;
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic inline unsigned int hip04_irq(struct irq_data *d)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	return d->hwirq;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/*
898c2ecf20Sopenharmony_ci * Routines to acknowledge, disable and enable interrupts
908c2ecf20Sopenharmony_ci */
918c2ecf20Sopenharmony_cistatic void hip04_mask_irq(struct irq_data *d)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	u32 mask = 1 << (hip04_irq(d) % 32);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	raw_spin_lock(&irq_controller_lock);
968c2ecf20Sopenharmony_ci	writel_relaxed(mask, hip04_dist_base(d) + GIC_DIST_ENABLE_CLEAR +
978c2ecf20Sopenharmony_ci		       (hip04_irq(d) / 32) * 4);
988c2ecf20Sopenharmony_ci	raw_spin_unlock(&irq_controller_lock);
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic void hip04_unmask_irq(struct irq_data *d)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	u32 mask = 1 << (hip04_irq(d) % 32);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	raw_spin_lock(&irq_controller_lock);
1068c2ecf20Sopenharmony_ci	writel_relaxed(mask, hip04_dist_base(d) + GIC_DIST_ENABLE_SET +
1078c2ecf20Sopenharmony_ci		       (hip04_irq(d) / 32) * 4);
1088c2ecf20Sopenharmony_ci	raw_spin_unlock(&irq_controller_lock);
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic void hip04_eoi_irq(struct irq_data *d)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	writel_relaxed(hip04_irq(d), hip04_cpu_base(d) + GIC_CPU_EOI);
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic int hip04_irq_set_type(struct irq_data *d, unsigned int type)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	void __iomem *base = hip04_dist_base(d);
1198c2ecf20Sopenharmony_ci	unsigned int irq = hip04_irq(d);
1208c2ecf20Sopenharmony_ci	int ret;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	/* Interrupt configuration for SGIs can't be changed */
1238c2ecf20Sopenharmony_ci	if (irq < 16)
1248c2ecf20Sopenharmony_ci		return -EINVAL;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/* SPIs have restrictions on the supported types */
1278c2ecf20Sopenharmony_ci	if (irq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
1288c2ecf20Sopenharmony_ci			 type != IRQ_TYPE_EDGE_RISING)
1298c2ecf20Sopenharmony_ci		return -EINVAL;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	raw_spin_lock(&irq_controller_lock);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	ret = gic_configure_irq(irq, type, base + GIC_DIST_CONFIG, NULL);
1348c2ecf20Sopenharmony_ci	if (ret && irq < 32) {
1358c2ecf20Sopenharmony_ci		/* Misconfigured PPIs are usually not fatal */
1368c2ecf20Sopenharmony_ci		pr_warn("GIC: PPI%d is secure or misconfigured\n", irq - 16);
1378c2ecf20Sopenharmony_ci		ret = 0;
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	raw_spin_unlock(&irq_controller_lock);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	return ret;
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
1468c2ecf20Sopenharmony_cistatic int hip04_irq_set_affinity(struct irq_data *d,
1478c2ecf20Sopenharmony_ci				  const struct cpumask *mask_val,
1488c2ecf20Sopenharmony_ci				  bool force)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	void __iomem *reg;
1518c2ecf20Sopenharmony_ci	unsigned int cpu, shift = (hip04_irq(d) % 2) * 16;
1528c2ecf20Sopenharmony_ci	u32 val, mask, bit;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	if (!force)
1558c2ecf20Sopenharmony_ci		cpu = cpumask_any_and(mask_val, cpu_online_mask);
1568c2ecf20Sopenharmony_ci	else
1578c2ecf20Sopenharmony_ci		cpu = cpumask_first(mask_val);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	if (cpu >= NR_HIP04_CPU_IF || cpu >= nr_cpu_ids)
1608c2ecf20Sopenharmony_ci		return -EINVAL;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	raw_spin_lock(&irq_controller_lock);
1638c2ecf20Sopenharmony_ci	reg = hip04_dist_base(d) + GIC_DIST_TARGET + ((hip04_irq(d) * 2) & ~3);
1648c2ecf20Sopenharmony_ci	mask = 0xffff << shift;
1658c2ecf20Sopenharmony_ci	bit = hip04_cpu_map[cpu] << shift;
1668c2ecf20Sopenharmony_ci	val = readl_relaxed(reg) & ~mask;
1678c2ecf20Sopenharmony_ci	writel_relaxed(val | bit, reg);
1688c2ecf20Sopenharmony_ci	raw_spin_unlock(&irq_controller_lock);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	irq_data_update_effective_affinity(d, cpumask_of(cpu));
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	return IRQ_SET_MASK_OK;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic void hip04_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	int cpu;
1788c2ecf20Sopenharmony_ci	unsigned long flags, map = 0;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&irq_controller_lock, flags);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	/* Convert our logical CPU mask into a physical one. */
1838c2ecf20Sopenharmony_ci	for_each_cpu(cpu, mask)
1848c2ecf20Sopenharmony_ci		map |= hip04_cpu_map[cpu];
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	/*
1878c2ecf20Sopenharmony_ci	 * Ensure that stores to Normal memory are visible to the
1888c2ecf20Sopenharmony_ci	 * other CPUs before they observe us issuing the IPI.
1898c2ecf20Sopenharmony_ci	 */
1908c2ecf20Sopenharmony_ci	dmb(ishst);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/* this always happens on GIC0 */
1938c2ecf20Sopenharmony_ci	writel_relaxed(map << 8 | d->hwirq, hip04_data.dist_base + GIC_DIST_SOFTINT);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci#endif
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic void __exception_irq_entry hip04_handle_irq(struct pt_regs *regs)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	u32 irqstat, irqnr;
2028c2ecf20Sopenharmony_ci	void __iomem *cpu_base = hip04_data.cpu_base;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	do {
2058c2ecf20Sopenharmony_ci		irqstat = readl_relaxed(cpu_base + GIC_CPU_INTACK);
2068c2ecf20Sopenharmony_ci		irqnr = irqstat & GICC_IAR_INT_ID_MASK;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci		if (irqnr <= HIP04_MAX_IRQS)
2098c2ecf20Sopenharmony_ci			handle_domain_irq(hip04_data.domain, irqnr, regs);
2108c2ecf20Sopenharmony_ci	} while (irqnr > HIP04_MAX_IRQS);
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic struct irq_chip hip04_irq_chip = {
2148c2ecf20Sopenharmony_ci	.name			= "HIP04 INTC",
2158c2ecf20Sopenharmony_ci	.irq_mask		= hip04_mask_irq,
2168c2ecf20Sopenharmony_ci	.irq_unmask		= hip04_unmask_irq,
2178c2ecf20Sopenharmony_ci	.irq_eoi		= hip04_eoi_irq,
2188c2ecf20Sopenharmony_ci	.irq_set_type		= hip04_irq_set_type,
2198c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
2208c2ecf20Sopenharmony_ci	.irq_set_affinity	= hip04_irq_set_affinity,
2218c2ecf20Sopenharmony_ci	.ipi_send_mask		= hip04_ipi_send_mask,
2228c2ecf20Sopenharmony_ci#endif
2238c2ecf20Sopenharmony_ci	.flags			= IRQCHIP_SET_TYPE_MASKED |
2248c2ecf20Sopenharmony_ci				  IRQCHIP_SKIP_SET_WAKE |
2258c2ecf20Sopenharmony_ci				  IRQCHIP_MASK_ON_SUSPEND,
2268c2ecf20Sopenharmony_ci};
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic u16 hip04_get_cpumask(struct hip04_irq_data *intc)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	void __iomem *base = intc->dist_base;
2318c2ecf20Sopenharmony_ci	u32 mask, i;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	for (i = mask = 0; i < 32; i += 2) {
2348c2ecf20Sopenharmony_ci		mask = readl_relaxed(base + GIC_DIST_TARGET + i * 2);
2358c2ecf20Sopenharmony_ci		mask |= mask >> 16;
2368c2ecf20Sopenharmony_ci		if (mask)
2378c2ecf20Sopenharmony_ci			break;
2388c2ecf20Sopenharmony_ci	}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	if (!mask)
2418c2ecf20Sopenharmony_ci		pr_crit("GIC CPU mask not found - kernel will fail to boot.\n");
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	return mask;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic void __init hip04_irq_dist_init(struct hip04_irq_data *intc)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	unsigned int i;
2498c2ecf20Sopenharmony_ci	u32 cpumask;
2508c2ecf20Sopenharmony_ci	unsigned int nr_irqs = intc->nr_irqs;
2518c2ecf20Sopenharmony_ci	void __iomem *base = intc->dist_base;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	writel_relaxed(0, base + GIC_DIST_CTRL);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	/*
2568c2ecf20Sopenharmony_ci	 * Set all global interrupts to this CPU only.
2578c2ecf20Sopenharmony_ci	 */
2588c2ecf20Sopenharmony_ci	cpumask = hip04_get_cpumask(intc);
2598c2ecf20Sopenharmony_ci	cpumask |= cpumask << 16;
2608c2ecf20Sopenharmony_ci	for (i = 32; i < nr_irqs; i += 2)
2618c2ecf20Sopenharmony_ci		writel_relaxed(cpumask, base + GIC_DIST_TARGET + ((i * 2) & ~3));
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	gic_dist_config(base, nr_irqs, NULL);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	writel_relaxed(1, base + GIC_DIST_CTRL);
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic void hip04_irq_cpu_init(struct hip04_irq_data *intc)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	void __iomem *dist_base = intc->dist_base;
2718c2ecf20Sopenharmony_ci	void __iomem *base = intc->cpu_base;
2728c2ecf20Sopenharmony_ci	unsigned int cpu_mask, cpu = smp_processor_id();
2738c2ecf20Sopenharmony_ci	int i;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	/*
2768c2ecf20Sopenharmony_ci	 * Get what the GIC says our CPU mask is.
2778c2ecf20Sopenharmony_ci	 */
2788c2ecf20Sopenharmony_ci	BUG_ON(cpu >= NR_HIP04_CPU_IF);
2798c2ecf20Sopenharmony_ci	cpu_mask = hip04_get_cpumask(intc);
2808c2ecf20Sopenharmony_ci	hip04_cpu_map[cpu] = cpu_mask;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	/*
2838c2ecf20Sopenharmony_ci	 * Clear our mask from the other map entries in case they're
2848c2ecf20Sopenharmony_ci	 * still undefined.
2858c2ecf20Sopenharmony_ci	 */
2868c2ecf20Sopenharmony_ci	for (i = 0; i < NR_HIP04_CPU_IF; i++)
2878c2ecf20Sopenharmony_ci		if (i != cpu)
2888c2ecf20Sopenharmony_ci			hip04_cpu_map[i] &= ~cpu_mask;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	gic_cpu_config(dist_base, 32, NULL);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
2938c2ecf20Sopenharmony_ci	writel_relaxed(1, base + GIC_CPU_CTRL);
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic int hip04_irq_domain_map(struct irq_domain *d, unsigned int irq,
2978c2ecf20Sopenharmony_ci				irq_hw_number_t hw)
2988c2ecf20Sopenharmony_ci{
2998c2ecf20Sopenharmony_ci	if (hw < 16) {
3008c2ecf20Sopenharmony_ci		irq_set_percpu_devid(irq);
3018c2ecf20Sopenharmony_ci		irq_set_chip_and_handler(irq, &hip04_irq_chip,
3028c2ecf20Sopenharmony_ci					 handle_percpu_devid_fasteoi_ipi);
3038c2ecf20Sopenharmony_ci	} else if (hw < 32) {
3048c2ecf20Sopenharmony_ci		irq_set_percpu_devid(irq);
3058c2ecf20Sopenharmony_ci		irq_set_chip_and_handler(irq, &hip04_irq_chip,
3068c2ecf20Sopenharmony_ci					 handle_percpu_devid_irq);
3078c2ecf20Sopenharmony_ci	} else {
3088c2ecf20Sopenharmony_ci		irq_set_chip_and_handler(irq, &hip04_irq_chip,
3098c2ecf20Sopenharmony_ci					 handle_fasteoi_irq);
3108c2ecf20Sopenharmony_ci		irq_set_probe(irq);
3118c2ecf20Sopenharmony_ci		irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(irq)));
3128c2ecf20Sopenharmony_ci	}
3138c2ecf20Sopenharmony_ci	irq_set_chip_data(irq, d->host_data);
3148c2ecf20Sopenharmony_ci	return 0;
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic int hip04_irq_domain_xlate(struct irq_domain *d,
3188c2ecf20Sopenharmony_ci				  struct device_node *controller,
3198c2ecf20Sopenharmony_ci				  const u32 *intspec, unsigned int intsize,
3208c2ecf20Sopenharmony_ci				  unsigned long *out_hwirq,
3218c2ecf20Sopenharmony_ci				  unsigned int *out_type)
3228c2ecf20Sopenharmony_ci{
3238c2ecf20Sopenharmony_ci	if (irq_domain_get_of_node(d) != controller)
3248c2ecf20Sopenharmony_ci		return -EINVAL;
3258c2ecf20Sopenharmony_ci	if (intsize == 1 && intspec[0] < 16) {
3268c2ecf20Sopenharmony_ci		*out_hwirq = intspec[0];
3278c2ecf20Sopenharmony_ci		*out_type = IRQ_TYPE_EDGE_RISING;
3288c2ecf20Sopenharmony_ci		return 0;
3298c2ecf20Sopenharmony_ci	}
3308c2ecf20Sopenharmony_ci	if (intsize < 3)
3318c2ecf20Sopenharmony_ci		return -EINVAL;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	/* Get the interrupt number and add 16 to skip over SGIs */
3348c2ecf20Sopenharmony_ci	*out_hwirq = intspec[1] + 16;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	/* For SPIs, we need to add 16 more to get the irq ID number */
3378c2ecf20Sopenharmony_ci	if (!intspec[0])
3388c2ecf20Sopenharmony_ci		*out_hwirq += 16;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	*out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	return 0;
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic int hip04_irq_starting_cpu(unsigned int cpu)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	hip04_irq_cpu_init(&hip04_data);
3488c2ecf20Sopenharmony_ci	return 0;
3498c2ecf20Sopenharmony_ci}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cistatic const struct irq_domain_ops hip04_irq_domain_ops = {
3528c2ecf20Sopenharmony_ci	.map	= hip04_irq_domain_map,
3538c2ecf20Sopenharmony_ci	.xlate	= hip04_irq_domain_xlate,
3548c2ecf20Sopenharmony_ci};
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic int __init
3578c2ecf20Sopenharmony_cihip04_of_init(struct device_node *node, struct device_node *parent)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	int nr_irqs, irq_base, i;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	if (WARN_ON(!node))
3628c2ecf20Sopenharmony_ci		return -ENODEV;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	hip04_data.dist_base = of_iomap(node, 0);
3658c2ecf20Sopenharmony_ci	WARN(!hip04_data.dist_base, "fail to map hip04 intc dist registers\n");
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	hip04_data.cpu_base = of_iomap(node, 1);
3688c2ecf20Sopenharmony_ci	WARN(!hip04_data.cpu_base, "unable to map hip04 intc cpu registers\n");
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	/*
3718c2ecf20Sopenharmony_ci	 * Initialize the CPU interface map to all CPUs.
3728c2ecf20Sopenharmony_ci	 * It will be refined as each CPU probes its ID.
3738c2ecf20Sopenharmony_ci	 */
3748c2ecf20Sopenharmony_ci	for (i = 0; i < NR_HIP04_CPU_IF; i++)
3758c2ecf20Sopenharmony_ci		hip04_cpu_map[i] = 0xffff;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	/*
3788c2ecf20Sopenharmony_ci	 * Find out how many interrupts are supported.
3798c2ecf20Sopenharmony_ci	 * The HIP04 INTC only supports up to 510 interrupt sources.
3808c2ecf20Sopenharmony_ci	 */
3818c2ecf20Sopenharmony_ci	nr_irqs = readl_relaxed(hip04_data.dist_base + GIC_DIST_CTR) & 0x1f;
3828c2ecf20Sopenharmony_ci	nr_irqs = (nr_irqs + 1) * 32;
3838c2ecf20Sopenharmony_ci	if (nr_irqs > HIP04_MAX_IRQS)
3848c2ecf20Sopenharmony_ci		nr_irqs = HIP04_MAX_IRQS;
3858c2ecf20Sopenharmony_ci	hip04_data.nr_irqs = nr_irqs;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	irq_base = irq_alloc_descs(-1, 0, nr_irqs, numa_node_id());
3888c2ecf20Sopenharmony_ci	if (irq_base < 0) {
3898c2ecf20Sopenharmony_ci		pr_err("failed to allocate IRQ numbers\n");
3908c2ecf20Sopenharmony_ci		return -EINVAL;
3918c2ecf20Sopenharmony_ci	}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	hip04_data.domain = irq_domain_add_legacy(node, nr_irqs, irq_base,
3948c2ecf20Sopenharmony_ci						  0,
3958c2ecf20Sopenharmony_ci						  &hip04_irq_domain_ops,
3968c2ecf20Sopenharmony_ci						  &hip04_data);
3978c2ecf20Sopenharmony_ci	if (WARN_ON(!hip04_data.domain))
3988c2ecf20Sopenharmony_ci		return -EINVAL;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
4018c2ecf20Sopenharmony_ci	set_smp_ipi_range(irq_base, 16);
4028c2ecf20Sopenharmony_ci#endif
4038c2ecf20Sopenharmony_ci	set_handle_irq(hip04_handle_irq);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	hip04_irq_dist_init(&hip04_data);
4068c2ecf20Sopenharmony_ci	cpuhp_setup_state(CPUHP_AP_IRQ_HIP04_STARTING, "irqchip/hip04:starting",
4078c2ecf20Sopenharmony_ci			  hip04_irq_starting_cpu, NULL);
4088c2ecf20Sopenharmony_ci	return 0;
4098c2ecf20Sopenharmony_ci}
4108c2ecf20Sopenharmony_ciIRQCHIP_DECLARE(hip04_intc, "hisilicon,hip04-intc", hip04_of_init);
411