18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015 Broadcom Corporation
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
78c2ecf20Sopenharmony_ci#include <linux/irqchip/chained_irq.h>
88c2ecf20Sopenharmony_ci#include <linux/irqdomain.h>
98c2ecf20Sopenharmony_ci#include <linux/msi.h>
108c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
118c2ecf20Sopenharmony_ci#include <linux/of_pci.h>
128c2ecf20Sopenharmony_ci#include <linux/pci.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include "pcie-iproc.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define IPROC_MSI_INTR_EN_SHIFT        11
178c2ecf20Sopenharmony_ci#define IPROC_MSI_INTR_EN              BIT(IPROC_MSI_INTR_EN_SHIFT)
188c2ecf20Sopenharmony_ci#define IPROC_MSI_INT_N_EVENT_SHIFT    1
198c2ecf20Sopenharmony_ci#define IPROC_MSI_INT_N_EVENT          BIT(IPROC_MSI_INT_N_EVENT_SHIFT)
208c2ecf20Sopenharmony_ci#define IPROC_MSI_EQ_EN_SHIFT          0
218c2ecf20Sopenharmony_ci#define IPROC_MSI_EQ_EN                BIT(IPROC_MSI_EQ_EN_SHIFT)
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define IPROC_MSI_EQ_MASK              0x3f
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* Max number of GIC interrupts */
268c2ecf20Sopenharmony_ci#define NR_HW_IRQS                     6
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* Number of entries in each event queue */
298c2ecf20Sopenharmony_ci#define EQ_LEN                         64
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* Size of each event queue memory region */
328c2ecf20Sopenharmony_ci#define EQ_MEM_REGION_SIZE             SZ_4K
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* Size of each MSI address region */
358c2ecf20Sopenharmony_ci#define MSI_MEM_REGION_SIZE            SZ_4K
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cienum iproc_msi_reg {
388c2ecf20Sopenharmony_ci	IPROC_MSI_EQ_PAGE = 0,
398c2ecf20Sopenharmony_ci	IPROC_MSI_EQ_PAGE_UPPER,
408c2ecf20Sopenharmony_ci	IPROC_MSI_PAGE,
418c2ecf20Sopenharmony_ci	IPROC_MSI_PAGE_UPPER,
428c2ecf20Sopenharmony_ci	IPROC_MSI_CTRL,
438c2ecf20Sopenharmony_ci	IPROC_MSI_EQ_HEAD,
448c2ecf20Sopenharmony_ci	IPROC_MSI_EQ_TAIL,
458c2ecf20Sopenharmony_ci	IPROC_MSI_INTS_EN,
468c2ecf20Sopenharmony_ci	IPROC_MSI_REG_SIZE,
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistruct iproc_msi;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/**
528c2ecf20Sopenharmony_ci * iProc MSI group
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * One MSI group is allocated per GIC interrupt, serviced by one iProc MSI
558c2ecf20Sopenharmony_ci * event queue.
568c2ecf20Sopenharmony_ci *
578c2ecf20Sopenharmony_ci * @msi: pointer to iProc MSI data
588c2ecf20Sopenharmony_ci * @gic_irq: GIC interrupt
598c2ecf20Sopenharmony_ci * @eq: Event queue number
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_cistruct iproc_msi_grp {
628c2ecf20Sopenharmony_ci	struct iproc_msi *msi;
638c2ecf20Sopenharmony_ci	int gic_irq;
648c2ecf20Sopenharmony_ci	unsigned int eq;
658c2ecf20Sopenharmony_ci};
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/**
688c2ecf20Sopenharmony_ci * iProc event queue based MSI
698c2ecf20Sopenharmony_ci *
708c2ecf20Sopenharmony_ci * Only meant to be used on platforms without MSI support integrated into the
718c2ecf20Sopenharmony_ci * GIC.
728c2ecf20Sopenharmony_ci *
738c2ecf20Sopenharmony_ci * @pcie: pointer to iProc PCIe data
748c2ecf20Sopenharmony_ci * @reg_offsets: MSI register offsets
758c2ecf20Sopenharmony_ci * @grps: MSI groups
768c2ecf20Sopenharmony_ci * @nr_irqs: number of total interrupts connected to GIC
778c2ecf20Sopenharmony_ci * @nr_cpus: number of toal CPUs
788c2ecf20Sopenharmony_ci * @has_inten_reg: indicates the MSI interrupt enable register needs to be
798c2ecf20Sopenharmony_ci * set explicitly (required for some legacy platforms)
808c2ecf20Sopenharmony_ci * @bitmap: MSI vector bitmap
818c2ecf20Sopenharmony_ci * @bitmap_lock: lock to protect access to the MSI bitmap
828c2ecf20Sopenharmony_ci * @nr_msi_vecs: total number of MSI vectors
838c2ecf20Sopenharmony_ci * @inner_domain: inner IRQ domain
848c2ecf20Sopenharmony_ci * @msi_domain: MSI IRQ domain
858c2ecf20Sopenharmony_ci * @nr_eq_region: required number of 4K aligned memory region for MSI event
868c2ecf20Sopenharmony_ci * queues
878c2ecf20Sopenharmony_ci * @nr_msi_region: required number of 4K aligned address region for MSI posted
888c2ecf20Sopenharmony_ci * writes
898c2ecf20Sopenharmony_ci * @eq_cpu: pointer to allocated memory region for MSI event queues
908c2ecf20Sopenharmony_ci * @eq_dma: DMA address of MSI event queues
918c2ecf20Sopenharmony_ci * @msi_addr: MSI address
928c2ecf20Sopenharmony_ci */
938c2ecf20Sopenharmony_cistruct iproc_msi {
948c2ecf20Sopenharmony_ci	struct iproc_pcie *pcie;
958c2ecf20Sopenharmony_ci	const u16 (*reg_offsets)[IPROC_MSI_REG_SIZE];
968c2ecf20Sopenharmony_ci	struct iproc_msi_grp *grps;
978c2ecf20Sopenharmony_ci	int nr_irqs;
988c2ecf20Sopenharmony_ci	int nr_cpus;
998c2ecf20Sopenharmony_ci	bool has_inten_reg;
1008c2ecf20Sopenharmony_ci	unsigned long *bitmap;
1018c2ecf20Sopenharmony_ci	struct mutex bitmap_lock;
1028c2ecf20Sopenharmony_ci	unsigned int nr_msi_vecs;
1038c2ecf20Sopenharmony_ci	struct irq_domain *inner_domain;
1048c2ecf20Sopenharmony_ci	struct irq_domain *msi_domain;
1058c2ecf20Sopenharmony_ci	unsigned int nr_eq_region;
1068c2ecf20Sopenharmony_ci	unsigned int nr_msi_region;
1078c2ecf20Sopenharmony_ci	void *eq_cpu;
1088c2ecf20Sopenharmony_ci	dma_addr_t eq_dma;
1098c2ecf20Sopenharmony_ci	phys_addr_t msi_addr;
1108c2ecf20Sopenharmony_ci};
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic const u16 iproc_msi_reg_paxb[NR_HW_IRQS][IPROC_MSI_REG_SIZE] = {
1138c2ecf20Sopenharmony_ci	{ 0x200, 0x2c0, 0x204, 0x2c4, 0x210, 0x250, 0x254, 0x208 },
1148c2ecf20Sopenharmony_ci	{ 0x200, 0x2c0, 0x204, 0x2c4, 0x214, 0x258, 0x25c, 0x208 },
1158c2ecf20Sopenharmony_ci	{ 0x200, 0x2c0, 0x204, 0x2c4, 0x218, 0x260, 0x264, 0x208 },
1168c2ecf20Sopenharmony_ci	{ 0x200, 0x2c0, 0x204, 0x2c4, 0x21c, 0x268, 0x26c, 0x208 },
1178c2ecf20Sopenharmony_ci	{ 0x200, 0x2c0, 0x204, 0x2c4, 0x220, 0x270, 0x274, 0x208 },
1188c2ecf20Sopenharmony_ci	{ 0x200, 0x2c0, 0x204, 0x2c4, 0x224, 0x278, 0x27c, 0x208 },
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic const u16 iproc_msi_reg_paxc[NR_HW_IRQS][IPROC_MSI_REG_SIZE] = {
1228c2ecf20Sopenharmony_ci	{ 0xc00, 0xc04, 0xc08, 0xc0c, 0xc40, 0xc50, 0xc60 },
1238c2ecf20Sopenharmony_ci	{ 0xc10, 0xc14, 0xc18, 0xc1c, 0xc44, 0xc54, 0xc64 },
1248c2ecf20Sopenharmony_ci	{ 0xc20, 0xc24, 0xc28, 0xc2c, 0xc48, 0xc58, 0xc68 },
1258c2ecf20Sopenharmony_ci	{ 0xc30, 0xc34, 0xc38, 0xc3c, 0xc4c, 0xc5c, 0xc6c },
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic inline u32 iproc_msi_read_reg(struct iproc_msi *msi,
1298c2ecf20Sopenharmony_ci				     enum iproc_msi_reg reg,
1308c2ecf20Sopenharmony_ci				     unsigned int eq)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	struct iproc_pcie *pcie = msi->pcie;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return readl_relaxed(pcie->base + msi->reg_offsets[eq][reg]);
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic inline void iproc_msi_write_reg(struct iproc_msi *msi,
1388c2ecf20Sopenharmony_ci				       enum iproc_msi_reg reg,
1398c2ecf20Sopenharmony_ci				       int eq, u32 val)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	struct iproc_pcie *pcie = msi->pcie;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	writel_relaxed(val, pcie->base + msi->reg_offsets[eq][reg]);
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic inline u32 hwirq_to_group(struct iproc_msi *msi, unsigned long hwirq)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	return (hwirq % msi->nr_irqs);
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic inline unsigned int iproc_msi_addr_offset(struct iproc_msi *msi,
1528c2ecf20Sopenharmony_ci						 unsigned long hwirq)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	if (msi->nr_msi_region > 1)
1558c2ecf20Sopenharmony_ci		return hwirq_to_group(msi, hwirq) * MSI_MEM_REGION_SIZE;
1568c2ecf20Sopenharmony_ci	else
1578c2ecf20Sopenharmony_ci		return hwirq_to_group(msi, hwirq) * sizeof(u32);
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic inline unsigned int iproc_msi_eq_offset(struct iproc_msi *msi, u32 eq)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	if (msi->nr_eq_region > 1)
1638c2ecf20Sopenharmony_ci		return eq * EQ_MEM_REGION_SIZE;
1648c2ecf20Sopenharmony_ci	else
1658c2ecf20Sopenharmony_ci		return eq * EQ_LEN * sizeof(u32);
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic struct irq_chip iproc_msi_irq_chip = {
1698c2ecf20Sopenharmony_ci	.name = "iProc-MSI",
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic struct msi_domain_info iproc_msi_domain_info = {
1738c2ecf20Sopenharmony_ci	.flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
1748c2ecf20Sopenharmony_ci		MSI_FLAG_PCI_MSIX,
1758c2ecf20Sopenharmony_ci	.chip = &iproc_msi_irq_chip,
1768c2ecf20Sopenharmony_ci};
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci/*
1798c2ecf20Sopenharmony_ci * In iProc PCIe core, each MSI group is serviced by a GIC interrupt and a
1808c2ecf20Sopenharmony_ci * dedicated event queue.  Each MSI group can support up to 64 MSI vectors.
1818c2ecf20Sopenharmony_ci *
1828c2ecf20Sopenharmony_ci * The number of MSI groups varies between different iProc SoCs.  The total
1838c2ecf20Sopenharmony_ci * number of CPU cores also varies.  To support MSI IRQ affinity, we
1848c2ecf20Sopenharmony_ci * distribute GIC interrupts across all available CPUs.  MSI vector is moved
1858c2ecf20Sopenharmony_ci * from one GIC interrupt to another to steer to the target CPU.
1868c2ecf20Sopenharmony_ci *
1878c2ecf20Sopenharmony_ci * Assuming:
1888c2ecf20Sopenharmony_ci * - the number of MSI groups is M
1898c2ecf20Sopenharmony_ci * - the number of CPU cores is N
1908c2ecf20Sopenharmony_ci * - M is always a multiple of N
1918c2ecf20Sopenharmony_ci *
1928c2ecf20Sopenharmony_ci * Total number of raw MSI vectors = M * 64
1938c2ecf20Sopenharmony_ci * Total number of supported MSI vectors = (M * 64) / N
1948c2ecf20Sopenharmony_ci */
1958c2ecf20Sopenharmony_cistatic inline int hwirq_to_cpu(struct iproc_msi *msi, unsigned long hwirq)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	return (hwirq % msi->nr_cpus);
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic inline unsigned long hwirq_to_canonical_hwirq(struct iproc_msi *msi,
2018c2ecf20Sopenharmony_ci						     unsigned long hwirq)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	return (hwirq - hwirq_to_cpu(msi, hwirq));
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic int iproc_msi_irq_set_affinity(struct irq_data *data,
2078c2ecf20Sopenharmony_ci				      const struct cpumask *mask, bool force)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	struct iproc_msi *msi = irq_data_get_irq_chip_data(data);
2108c2ecf20Sopenharmony_ci	int target_cpu = cpumask_first(mask);
2118c2ecf20Sopenharmony_ci	int curr_cpu;
2128c2ecf20Sopenharmony_ci	int ret;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	curr_cpu = hwirq_to_cpu(msi, data->hwirq);
2158c2ecf20Sopenharmony_ci	if (curr_cpu == target_cpu)
2168c2ecf20Sopenharmony_ci		ret = IRQ_SET_MASK_OK_DONE;
2178c2ecf20Sopenharmony_ci	else {
2188c2ecf20Sopenharmony_ci		/* steer MSI to the target CPU */
2198c2ecf20Sopenharmony_ci		data->hwirq = hwirq_to_canonical_hwirq(msi, data->hwirq) + target_cpu;
2208c2ecf20Sopenharmony_ci		ret = IRQ_SET_MASK_OK;
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	irq_data_update_effective_affinity(data, cpumask_of(target_cpu));
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	return ret;
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic void iproc_msi_irq_compose_msi_msg(struct irq_data *data,
2298c2ecf20Sopenharmony_ci					  struct msi_msg *msg)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	struct iproc_msi *msi = irq_data_get_irq_chip_data(data);
2328c2ecf20Sopenharmony_ci	dma_addr_t addr;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	addr = msi->msi_addr + iproc_msi_addr_offset(msi, data->hwirq);
2358c2ecf20Sopenharmony_ci	msg->address_lo = lower_32_bits(addr);
2368c2ecf20Sopenharmony_ci	msg->address_hi = upper_32_bits(addr);
2378c2ecf20Sopenharmony_ci	msg->data = data->hwirq << 5;
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cistatic struct irq_chip iproc_msi_bottom_irq_chip = {
2418c2ecf20Sopenharmony_ci	.name = "MSI",
2428c2ecf20Sopenharmony_ci	.irq_set_affinity = iproc_msi_irq_set_affinity,
2438c2ecf20Sopenharmony_ci	.irq_compose_msi_msg = iproc_msi_irq_compose_msi_msg,
2448c2ecf20Sopenharmony_ci};
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic int iproc_msi_irq_domain_alloc(struct irq_domain *domain,
2478c2ecf20Sopenharmony_ci				      unsigned int virq, unsigned int nr_irqs,
2488c2ecf20Sopenharmony_ci				      void *args)
2498c2ecf20Sopenharmony_ci{
2508c2ecf20Sopenharmony_ci	struct iproc_msi *msi = domain->host_data;
2518c2ecf20Sopenharmony_ci	int hwirq, i;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	if (msi->nr_cpus > 1 && nr_irqs > 1)
2548c2ecf20Sopenharmony_ci		return -EINVAL;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	mutex_lock(&msi->bitmap_lock);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	/*
2598c2ecf20Sopenharmony_ci	 * Allocate 'nr_irqs' multiplied by 'nr_cpus' number of MSI vectors
2608c2ecf20Sopenharmony_ci	 * each time
2618c2ecf20Sopenharmony_ci	 */
2628c2ecf20Sopenharmony_ci	hwirq = bitmap_find_free_region(msi->bitmap, msi->nr_msi_vecs,
2638c2ecf20Sopenharmony_ci					order_base_2(msi->nr_cpus * nr_irqs));
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	mutex_unlock(&msi->bitmap_lock);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	if (hwirq < 0)
2688c2ecf20Sopenharmony_ci		return -ENOSPC;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	for (i = 0; i < nr_irqs; i++) {
2718c2ecf20Sopenharmony_ci		irq_domain_set_info(domain, virq + i, hwirq + i,
2728c2ecf20Sopenharmony_ci				    &iproc_msi_bottom_irq_chip,
2738c2ecf20Sopenharmony_ci				    domain->host_data, handle_simple_irq,
2748c2ecf20Sopenharmony_ci				    NULL, NULL);
2758c2ecf20Sopenharmony_ci	}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	return 0;
2788c2ecf20Sopenharmony_ci}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_cistatic void iproc_msi_irq_domain_free(struct irq_domain *domain,
2818c2ecf20Sopenharmony_ci				      unsigned int virq, unsigned int nr_irqs)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	struct irq_data *data = irq_domain_get_irq_data(domain, virq);
2848c2ecf20Sopenharmony_ci	struct iproc_msi *msi = irq_data_get_irq_chip_data(data);
2858c2ecf20Sopenharmony_ci	unsigned int hwirq;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	mutex_lock(&msi->bitmap_lock);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	hwirq = hwirq_to_canonical_hwirq(msi, data->hwirq);
2908c2ecf20Sopenharmony_ci	bitmap_release_region(msi->bitmap, hwirq,
2918c2ecf20Sopenharmony_ci			      order_base_2(msi->nr_cpus * nr_irqs));
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	mutex_unlock(&msi->bitmap_lock);
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_cistatic const struct irq_domain_ops msi_domain_ops = {
2998c2ecf20Sopenharmony_ci	.alloc = iproc_msi_irq_domain_alloc,
3008c2ecf20Sopenharmony_ci	.free = iproc_msi_irq_domain_free,
3018c2ecf20Sopenharmony_ci};
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_cistatic inline u32 decode_msi_hwirq(struct iproc_msi *msi, u32 eq, u32 head)
3048c2ecf20Sopenharmony_ci{
3058c2ecf20Sopenharmony_ci	u32 __iomem *msg;
3068c2ecf20Sopenharmony_ci	u32 hwirq;
3078c2ecf20Sopenharmony_ci	unsigned int offs;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	offs = iproc_msi_eq_offset(msi, eq) + head * sizeof(u32);
3108c2ecf20Sopenharmony_ci	msg = (u32 __iomem *)(msi->eq_cpu + offs);
3118c2ecf20Sopenharmony_ci	hwirq = readl(msg);
3128c2ecf20Sopenharmony_ci	hwirq = (hwirq >> 5) + (hwirq & 0x1f);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	/*
3158c2ecf20Sopenharmony_ci	 * Since we have multiple hwirq mapped to a single MSI vector,
3168c2ecf20Sopenharmony_ci	 * now we need to derive the hwirq at CPU0.  It can then be used to
3178c2ecf20Sopenharmony_ci	 * mapped back to virq.
3188c2ecf20Sopenharmony_ci	 */
3198c2ecf20Sopenharmony_ci	return hwirq_to_canonical_hwirq(msi, hwirq);
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic void iproc_msi_handler(struct irq_desc *desc)
3238c2ecf20Sopenharmony_ci{
3248c2ecf20Sopenharmony_ci	struct irq_chip *chip = irq_desc_get_chip(desc);
3258c2ecf20Sopenharmony_ci	struct iproc_msi_grp *grp;
3268c2ecf20Sopenharmony_ci	struct iproc_msi *msi;
3278c2ecf20Sopenharmony_ci	u32 eq, head, tail, nr_events;
3288c2ecf20Sopenharmony_ci	unsigned long hwirq;
3298c2ecf20Sopenharmony_ci	int virq;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	chained_irq_enter(chip, desc);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	grp = irq_desc_get_handler_data(desc);
3348c2ecf20Sopenharmony_ci	msi = grp->msi;
3358c2ecf20Sopenharmony_ci	eq = grp->eq;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	/*
3388c2ecf20Sopenharmony_ci	 * iProc MSI event queue is tracked by head and tail pointers.  Head
3398c2ecf20Sopenharmony_ci	 * pointer indicates the next entry (MSI data) to be consumed by SW in
3408c2ecf20Sopenharmony_ci	 * the queue and needs to be updated by SW.  iProc MSI core uses the
3418c2ecf20Sopenharmony_ci	 * tail pointer as the next data insertion point.
3428c2ecf20Sopenharmony_ci	 *
3438c2ecf20Sopenharmony_ci	 * Entries between head and tail pointers contain valid MSI data.  MSI
3448c2ecf20Sopenharmony_ci	 * data is guaranteed to be in the event queue memory before the tail
3458c2ecf20Sopenharmony_ci	 * pointer is updated by the iProc MSI core.
3468c2ecf20Sopenharmony_ci	 */
3478c2ecf20Sopenharmony_ci	head = iproc_msi_read_reg(msi, IPROC_MSI_EQ_HEAD,
3488c2ecf20Sopenharmony_ci				  eq) & IPROC_MSI_EQ_MASK;
3498c2ecf20Sopenharmony_ci	do {
3508c2ecf20Sopenharmony_ci		tail = iproc_msi_read_reg(msi, IPROC_MSI_EQ_TAIL,
3518c2ecf20Sopenharmony_ci					  eq) & IPROC_MSI_EQ_MASK;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci		/*
3548c2ecf20Sopenharmony_ci		 * Figure out total number of events (MSI data) to be
3558c2ecf20Sopenharmony_ci		 * processed.
3568c2ecf20Sopenharmony_ci		 */
3578c2ecf20Sopenharmony_ci		nr_events = (tail < head) ?
3588c2ecf20Sopenharmony_ci			(EQ_LEN - (head - tail)) : (tail - head);
3598c2ecf20Sopenharmony_ci		if (!nr_events)
3608c2ecf20Sopenharmony_ci			break;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci		/* process all outstanding events */
3638c2ecf20Sopenharmony_ci		while (nr_events--) {
3648c2ecf20Sopenharmony_ci			hwirq = decode_msi_hwirq(msi, eq, head);
3658c2ecf20Sopenharmony_ci			virq = irq_find_mapping(msi->inner_domain, hwirq);
3668c2ecf20Sopenharmony_ci			generic_handle_irq(virq);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci			head++;
3698c2ecf20Sopenharmony_ci			head %= EQ_LEN;
3708c2ecf20Sopenharmony_ci		}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci		/*
3738c2ecf20Sopenharmony_ci		 * Now all outstanding events have been processed.  Update the
3748c2ecf20Sopenharmony_ci		 * head pointer.
3758c2ecf20Sopenharmony_ci		 */
3768c2ecf20Sopenharmony_ci		iproc_msi_write_reg(msi, IPROC_MSI_EQ_HEAD, eq, head);
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci		/*
3798c2ecf20Sopenharmony_ci		 * Now go read the tail pointer again to see if there are new
3808c2ecf20Sopenharmony_ci		 * outstanding events that came in during the above window.
3818c2ecf20Sopenharmony_ci		 */
3828c2ecf20Sopenharmony_ci	} while (true);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	chained_irq_exit(chip, desc);
3858c2ecf20Sopenharmony_ci}
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_cistatic void iproc_msi_enable(struct iproc_msi *msi)
3888c2ecf20Sopenharmony_ci{
3898c2ecf20Sopenharmony_ci	int i, eq;
3908c2ecf20Sopenharmony_ci	u32 val;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	/* Program memory region for each event queue */
3938c2ecf20Sopenharmony_ci	for (i = 0; i < msi->nr_eq_region; i++) {
3948c2ecf20Sopenharmony_ci		dma_addr_t addr = msi->eq_dma + (i * EQ_MEM_REGION_SIZE);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci		iproc_msi_write_reg(msi, IPROC_MSI_EQ_PAGE, i,
3978c2ecf20Sopenharmony_ci				    lower_32_bits(addr));
3988c2ecf20Sopenharmony_ci		iproc_msi_write_reg(msi, IPROC_MSI_EQ_PAGE_UPPER, i,
3998c2ecf20Sopenharmony_ci				    upper_32_bits(addr));
4008c2ecf20Sopenharmony_ci	}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	/* Program address region for MSI posted writes */
4038c2ecf20Sopenharmony_ci	for (i = 0; i < msi->nr_msi_region; i++) {
4048c2ecf20Sopenharmony_ci		phys_addr_t addr = msi->msi_addr + (i * MSI_MEM_REGION_SIZE);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci		iproc_msi_write_reg(msi, IPROC_MSI_PAGE, i,
4078c2ecf20Sopenharmony_ci				    lower_32_bits(addr));
4088c2ecf20Sopenharmony_ci		iproc_msi_write_reg(msi, IPROC_MSI_PAGE_UPPER, i,
4098c2ecf20Sopenharmony_ci				    upper_32_bits(addr));
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	for (eq = 0; eq < msi->nr_irqs; eq++) {
4138c2ecf20Sopenharmony_ci		/* Enable MSI event queue */
4148c2ecf20Sopenharmony_ci		val = IPROC_MSI_INTR_EN | IPROC_MSI_INT_N_EVENT |
4158c2ecf20Sopenharmony_ci			IPROC_MSI_EQ_EN;
4168c2ecf20Sopenharmony_ci		iproc_msi_write_reg(msi, IPROC_MSI_CTRL, eq, val);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci		/*
4198c2ecf20Sopenharmony_ci		 * Some legacy platforms require the MSI interrupt enable
4208c2ecf20Sopenharmony_ci		 * register to be set explicitly.
4218c2ecf20Sopenharmony_ci		 */
4228c2ecf20Sopenharmony_ci		if (msi->has_inten_reg) {
4238c2ecf20Sopenharmony_ci			val = iproc_msi_read_reg(msi, IPROC_MSI_INTS_EN, eq);
4248c2ecf20Sopenharmony_ci			val |= BIT(eq);
4258c2ecf20Sopenharmony_ci			iproc_msi_write_reg(msi, IPROC_MSI_INTS_EN, eq, val);
4268c2ecf20Sopenharmony_ci		}
4278c2ecf20Sopenharmony_ci	}
4288c2ecf20Sopenharmony_ci}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_cistatic void iproc_msi_disable(struct iproc_msi *msi)
4318c2ecf20Sopenharmony_ci{
4328c2ecf20Sopenharmony_ci	u32 eq, val;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	for (eq = 0; eq < msi->nr_irqs; eq++) {
4358c2ecf20Sopenharmony_ci		if (msi->has_inten_reg) {
4368c2ecf20Sopenharmony_ci			val = iproc_msi_read_reg(msi, IPROC_MSI_INTS_EN, eq);
4378c2ecf20Sopenharmony_ci			val &= ~BIT(eq);
4388c2ecf20Sopenharmony_ci			iproc_msi_write_reg(msi, IPROC_MSI_INTS_EN, eq, val);
4398c2ecf20Sopenharmony_ci		}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci		val = iproc_msi_read_reg(msi, IPROC_MSI_CTRL, eq);
4428c2ecf20Sopenharmony_ci		val &= ~(IPROC_MSI_INTR_EN | IPROC_MSI_INT_N_EVENT |
4438c2ecf20Sopenharmony_ci			 IPROC_MSI_EQ_EN);
4448c2ecf20Sopenharmony_ci		iproc_msi_write_reg(msi, IPROC_MSI_CTRL, eq, val);
4458c2ecf20Sopenharmony_ci	}
4468c2ecf20Sopenharmony_ci}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_cistatic int iproc_msi_alloc_domains(struct device_node *node,
4498c2ecf20Sopenharmony_ci				   struct iproc_msi *msi)
4508c2ecf20Sopenharmony_ci{
4518c2ecf20Sopenharmony_ci	msi->inner_domain = irq_domain_add_linear(NULL, msi->nr_msi_vecs,
4528c2ecf20Sopenharmony_ci						  &msi_domain_ops, msi);
4538c2ecf20Sopenharmony_ci	if (!msi->inner_domain)
4548c2ecf20Sopenharmony_ci		return -ENOMEM;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	msi->msi_domain = pci_msi_create_irq_domain(of_node_to_fwnode(node),
4578c2ecf20Sopenharmony_ci						    &iproc_msi_domain_info,
4588c2ecf20Sopenharmony_ci						    msi->inner_domain);
4598c2ecf20Sopenharmony_ci	if (!msi->msi_domain) {
4608c2ecf20Sopenharmony_ci		irq_domain_remove(msi->inner_domain);
4618c2ecf20Sopenharmony_ci		return -ENOMEM;
4628c2ecf20Sopenharmony_ci	}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	return 0;
4658c2ecf20Sopenharmony_ci}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_cistatic void iproc_msi_free_domains(struct iproc_msi *msi)
4688c2ecf20Sopenharmony_ci{
4698c2ecf20Sopenharmony_ci	if (msi->msi_domain)
4708c2ecf20Sopenharmony_ci		irq_domain_remove(msi->msi_domain);
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	if (msi->inner_domain)
4738c2ecf20Sopenharmony_ci		irq_domain_remove(msi->inner_domain);
4748c2ecf20Sopenharmony_ci}
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_cistatic void iproc_msi_irq_free(struct iproc_msi *msi, unsigned int cpu)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	int i;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	for (i = cpu; i < msi->nr_irqs; i += msi->nr_cpus) {
4818c2ecf20Sopenharmony_ci		irq_set_chained_handler_and_data(msi->grps[i].gic_irq,
4828c2ecf20Sopenharmony_ci						 NULL, NULL);
4838c2ecf20Sopenharmony_ci	}
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_cistatic int iproc_msi_irq_setup(struct iproc_msi *msi, unsigned int cpu)
4878c2ecf20Sopenharmony_ci{
4888c2ecf20Sopenharmony_ci	int i, ret;
4898c2ecf20Sopenharmony_ci	cpumask_var_t mask;
4908c2ecf20Sopenharmony_ci	struct iproc_pcie *pcie = msi->pcie;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	for (i = cpu; i < msi->nr_irqs; i += msi->nr_cpus) {
4938c2ecf20Sopenharmony_ci		irq_set_chained_handler_and_data(msi->grps[i].gic_irq,
4948c2ecf20Sopenharmony_ci						 iproc_msi_handler,
4958c2ecf20Sopenharmony_ci						 &msi->grps[i]);
4968c2ecf20Sopenharmony_ci		/* Dedicate GIC interrupt to each CPU core */
4978c2ecf20Sopenharmony_ci		if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
4988c2ecf20Sopenharmony_ci			cpumask_clear(mask);
4998c2ecf20Sopenharmony_ci			cpumask_set_cpu(cpu, mask);
5008c2ecf20Sopenharmony_ci			ret = irq_set_affinity(msi->grps[i].gic_irq, mask);
5018c2ecf20Sopenharmony_ci			if (ret)
5028c2ecf20Sopenharmony_ci				dev_err(pcie->dev,
5038c2ecf20Sopenharmony_ci					"failed to set affinity for IRQ%d\n",
5048c2ecf20Sopenharmony_ci					msi->grps[i].gic_irq);
5058c2ecf20Sopenharmony_ci			free_cpumask_var(mask);
5068c2ecf20Sopenharmony_ci		} else {
5078c2ecf20Sopenharmony_ci			dev_err(pcie->dev, "failed to alloc CPU mask\n");
5088c2ecf20Sopenharmony_ci			ret = -EINVAL;
5098c2ecf20Sopenharmony_ci		}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci		if (ret) {
5128c2ecf20Sopenharmony_ci			/* Free all configured/unconfigured IRQs */
5138c2ecf20Sopenharmony_ci			iproc_msi_irq_free(msi, cpu);
5148c2ecf20Sopenharmony_ci			return ret;
5158c2ecf20Sopenharmony_ci		}
5168c2ecf20Sopenharmony_ci	}
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	return 0;
5198c2ecf20Sopenharmony_ci}
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ciint iproc_msi_init(struct iproc_pcie *pcie, struct device_node *node)
5228c2ecf20Sopenharmony_ci{
5238c2ecf20Sopenharmony_ci	struct iproc_msi *msi;
5248c2ecf20Sopenharmony_ci	int i, ret;
5258c2ecf20Sopenharmony_ci	unsigned int cpu;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	if (!of_device_is_compatible(node, "brcm,iproc-msi"))
5288c2ecf20Sopenharmony_ci		return -ENODEV;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	if (!of_find_property(node, "msi-controller", NULL))
5318c2ecf20Sopenharmony_ci		return -ENODEV;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	if (pcie->msi)
5348c2ecf20Sopenharmony_ci		return -EBUSY;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	msi = devm_kzalloc(pcie->dev, sizeof(*msi), GFP_KERNEL);
5378c2ecf20Sopenharmony_ci	if (!msi)
5388c2ecf20Sopenharmony_ci		return -ENOMEM;
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	msi->pcie = pcie;
5418c2ecf20Sopenharmony_ci	pcie->msi = msi;
5428c2ecf20Sopenharmony_ci	msi->msi_addr = pcie->base_addr;
5438c2ecf20Sopenharmony_ci	mutex_init(&msi->bitmap_lock);
5448c2ecf20Sopenharmony_ci	msi->nr_cpus = num_possible_cpus();
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	if (msi->nr_cpus == 1)
5478c2ecf20Sopenharmony_ci		iproc_msi_domain_info.flags |=  MSI_FLAG_MULTI_PCI_MSI;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	msi->nr_irqs = of_irq_count(node);
5508c2ecf20Sopenharmony_ci	if (!msi->nr_irqs) {
5518c2ecf20Sopenharmony_ci		dev_err(pcie->dev, "found no MSI GIC interrupt\n");
5528c2ecf20Sopenharmony_ci		return -ENODEV;
5538c2ecf20Sopenharmony_ci	}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	if (msi->nr_irqs > NR_HW_IRQS) {
5568c2ecf20Sopenharmony_ci		dev_warn(pcie->dev, "too many MSI GIC interrupts defined %d\n",
5578c2ecf20Sopenharmony_ci			 msi->nr_irqs);
5588c2ecf20Sopenharmony_ci		msi->nr_irqs = NR_HW_IRQS;
5598c2ecf20Sopenharmony_ci	}
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	if (msi->nr_irqs < msi->nr_cpus) {
5628c2ecf20Sopenharmony_ci		dev_err(pcie->dev,
5638c2ecf20Sopenharmony_ci			"not enough GIC interrupts for MSI affinity\n");
5648c2ecf20Sopenharmony_ci		return -EINVAL;
5658c2ecf20Sopenharmony_ci	}
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	if (msi->nr_irqs % msi->nr_cpus != 0) {
5688c2ecf20Sopenharmony_ci		msi->nr_irqs -= msi->nr_irqs % msi->nr_cpus;
5698c2ecf20Sopenharmony_ci		dev_warn(pcie->dev, "Reducing number of interrupts to %d\n",
5708c2ecf20Sopenharmony_ci			 msi->nr_irqs);
5718c2ecf20Sopenharmony_ci	}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	switch (pcie->type) {
5748c2ecf20Sopenharmony_ci	case IPROC_PCIE_PAXB_BCMA:
5758c2ecf20Sopenharmony_ci	case IPROC_PCIE_PAXB:
5768c2ecf20Sopenharmony_ci		msi->reg_offsets = iproc_msi_reg_paxb;
5778c2ecf20Sopenharmony_ci		msi->nr_eq_region = 1;
5788c2ecf20Sopenharmony_ci		msi->nr_msi_region = 1;
5798c2ecf20Sopenharmony_ci		break;
5808c2ecf20Sopenharmony_ci	case IPROC_PCIE_PAXC:
5818c2ecf20Sopenharmony_ci		msi->reg_offsets = iproc_msi_reg_paxc;
5828c2ecf20Sopenharmony_ci		msi->nr_eq_region = msi->nr_irqs;
5838c2ecf20Sopenharmony_ci		msi->nr_msi_region = msi->nr_irqs;
5848c2ecf20Sopenharmony_ci		break;
5858c2ecf20Sopenharmony_ci	default:
5868c2ecf20Sopenharmony_ci		dev_err(pcie->dev, "incompatible iProc PCIe interface\n");
5878c2ecf20Sopenharmony_ci		return -EINVAL;
5888c2ecf20Sopenharmony_ci	}
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	if (of_find_property(node, "brcm,pcie-msi-inten", NULL))
5918c2ecf20Sopenharmony_ci		msi->has_inten_reg = true;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	msi->nr_msi_vecs = msi->nr_irqs * EQ_LEN;
5948c2ecf20Sopenharmony_ci	msi->bitmap = devm_kcalloc(pcie->dev, BITS_TO_LONGS(msi->nr_msi_vecs),
5958c2ecf20Sopenharmony_ci				   sizeof(*msi->bitmap), GFP_KERNEL);
5968c2ecf20Sopenharmony_ci	if (!msi->bitmap)
5978c2ecf20Sopenharmony_ci		return -ENOMEM;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	msi->grps = devm_kcalloc(pcie->dev, msi->nr_irqs, sizeof(*msi->grps),
6008c2ecf20Sopenharmony_ci				 GFP_KERNEL);
6018c2ecf20Sopenharmony_ci	if (!msi->grps)
6028c2ecf20Sopenharmony_ci		return -ENOMEM;
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	for (i = 0; i < msi->nr_irqs; i++) {
6058c2ecf20Sopenharmony_ci		unsigned int irq = irq_of_parse_and_map(node, i);
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci		if (!irq) {
6088c2ecf20Sopenharmony_ci			dev_err(pcie->dev, "unable to parse/map interrupt\n");
6098c2ecf20Sopenharmony_ci			ret = -ENODEV;
6108c2ecf20Sopenharmony_ci			goto free_irqs;
6118c2ecf20Sopenharmony_ci		}
6128c2ecf20Sopenharmony_ci		msi->grps[i].gic_irq = irq;
6138c2ecf20Sopenharmony_ci		msi->grps[i].msi = msi;
6148c2ecf20Sopenharmony_ci		msi->grps[i].eq = i;
6158c2ecf20Sopenharmony_ci	}
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	/* Reserve memory for event queue and make sure memories are zeroed */
6188c2ecf20Sopenharmony_ci	msi->eq_cpu = dma_alloc_coherent(pcie->dev,
6198c2ecf20Sopenharmony_ci					 msi->nr_eq_region * EQ_MEM_REGION_SIZE,
6208c2ecf20Sopenharmony_ci					 &msi->eq_dma, GFP_KERNEL);
6218c2ecf20Sopenharmony_ci	if (!msi->eq_cpu) {
6228c2ecf20Sopenharmony_ci		ret = -ENOMEM;
6238c2ecf20Sopenharmony_ci		goto free_irqs;
6248c2ecf20Sopenharmony_ci	}
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	ret = iproc_msi_alloc_domains(node, msi);
6278c2ecf20Sopenharmony_ci	if (ret) {
6288c2ecf20Sopenharmony_ci		dev_err(pcie->dev, "failed to create MSI domains\n");
6298c2ecf20Sopenharmony_ci		goto free_eq_dma;
6308c2ecf20Sopenharmony_ci	}
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	for_each_online_cpu(cpu) {
6338c2ecf20Sopenharmony_ci		ret = iproc_msi_irq_setup(msi, cpu);
6348c2ecf20Sopenharmony_ci		if (ret)
6358c2ecf20Sopenharmony_ci			goto free_msi_irq;
6368c2ecf20Sopenharmony_ci	}
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	iproc_msi_enable(msi);
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	return 0;
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_cifree_msi_irq:
6438c2ecf20Sopenharmony_ci	for_each_online_cpu(cpu)
6448c2ecf20Sopenharmony_ci		iproc_msi_irq_free(msi, cpu);
6458c2ecf20Sopenharmony_ci	iproc_msi_free_domains(msi);
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_cifree_eq_dma:
6488c2ecf20Sopenharmony_ci	dma_free_coherent(pcie->dev, msi->nr_eq_region * EQ_MEM_REGION_SIZE,
6498c2ecf20Sopenharmony_ci			  msi->eq_cpu, msi->eq_dma);
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_cifree_irqs:
6528c2ecf20Sopenharmony_ci	for (i = 0; i < msi->nr_irqs; i++) {
6538c2ecf20Sopenharmony_ci		if (msi->grps[i].gic_irq)
6548c2ecf20Sopenharmony_ci			irq_dispose_mapping(msi->grps[i].gic_irq);
6558c2ecf20Sopenharmony_ci	}
6568c2ecf20Sopenharmony_ci	pcie->msi = NULL;
6578c2ecf20Sopenharmony_ci	return ret;
6588c2ecf20Sopenharmony_ci}
6598c2ecf20Sopenharmony_ciEXPORT_SYMBOL(iproc_msi_init);
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_civoid iproc_msi_exit(struct iproc_pcie *pcie)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	struct iproc_msi *msi = pcie->msi;
6648c2ecf20Sopenharmony_ci	unsigned int i, cpu;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	if (!msi)
6678c2ecf20Sopenharmony_ci		return;
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	iproc_msi_disable(msi);
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	for_each_online_cpu(cpu)
6728c2ecf20Sopenharmony_ci		iproc_msi_irq_free(msi, cpu);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	iproc_msi_free_domains(msi);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	dma_free_coherent(pcie->dev, msi->nr_eq_region * EQ_MEM_REGION_SIZE,
6778c2ecf20Sopenharmony_ci			  msi->eq_cpu, msi->eq_dma);
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	for (i = 0; i < msi->nr_irqs; i++) {
6808c2ecf20Sopenharmony_ci		if (msi->grps[i].gic_irq)
6818c2ecf20Sopenharmony_ci			irq_dispose_mapping(msi->grps[i].gic_irq);
6828c2ecf20Sopenharmony_ci	}
6838c2ecf20Sopenharmony_ci}
6848c2ecf20Sopenharmony_ciEXPORT_SYMBOL(iproc_msi_exit);
685