Skip to content

Commit 32c6c05

Browse files
Stanimir Varbanovkwilczynski
authored andcommitted
irqchip: Add Broadcom BCM2712 MSI-X interrupt controller
Add an interrupt controller driver for MSI-X Interrupt Peripheral (MIP) hardware block found in BCM2712. The interrupt controller is used to handle MSI-X interrupts from peripherials behind PCIe endpoints like RPi1 south bridge found in RPi5. There are two MIPs on BCM2712, the first has 64 consecutive SPIs assigned to 64 output vectors, and the second has 17 SPIs, but only 8 of them are consecutive starting at the 8th output vector. Signed-off-by: Stanimir Varbanov <[email protected]> Reviewed-by: Thomas Gleixner <[email protected]> Tested-by: Ivan T. Ivanov <[email protected]> Link: https://lore.kernel.org/r/[email protected] [kwilczynski: commit log] Signed-off-by: Krzysztof Wilczyński <[email protected]>
1 parent 4215fd0 commit 32c6c05

File tree

3 files changed

+309
-0
lines changed

3 files changed

+309
-0
lines changed

drivers/irqchip/Kconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ config I8259
109109
bool
110110
select IRQ_DOMAIN
111111

112+
config BCM2712_MIP
113+
tristate "Broadcom BCM2712 MSI-X Interrupt Peripheral support"
114+
depends on ARCH_BRCMSTB || COMPILE_TEST
115+
default m if ARCH_BRCMSTB
116+
depends on ARM_GIC
117+
select GENERIC_IRQ_CHIP
118+
select IRQ_DOMAIN_HIERARCHY
119+
select GENERIC_MSI_IRQ
120+
select IRQ_MSI_LIB
121+
help
122+
Enable support for the Broadcom BCM2712 MSI-X target peripheral
123+
(MIP) needed by brcmstb PCIe to handle MSI-X interrupts on
124+
Raspberry Pi 5.
125+
126+
If unsure say n.
127+
112128
config BCM6345_L1_IRQ
113129
bool
114130
select GENERIC_IRQ_CHIP

drivers/irqchip/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ obj-$(CONFIG_XTENSA_MX) += irq-xtensa-mx.o
6363
obj-$(CONFIG_XILINX_INTC) += irq-xilinx-intc.o
6464
obj-$(CONFIG_IRQ_CROSSBAR) += irq-crossbar.o
6565
obj-$(CONFIG_SOC_VF610) += irq-vf610-mscm-ir.o
66+
obj-$(CONFIG_BCM2712_MIP) += irq-bcm2712-mip.o
6667
obj-$(CONFIG_BCM6345_L1_IRQ) += irq-bcm6345-l1.o
6768
obj-$(CONFIG_BCM7038_L1_IRQ) += irq-bcm7038-l1.o
6869
obj-$(CONFIG_BCM7120_L2_IRQ) += irq-bcm7120-l2.o

drivers/irqchip/irq-bcm2712-mip.c

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2024 Raspberry Pi Ltd., All Rights Reserved.
4+
* Copyright (c) 2024 SUSE
5+
*/
6+
7+
#include <linux/bitmap.h>
8+
#include <linux/irqchip.h>
9+
#include <linux/irqdomain.h>
10+
#include <linux/msi.h>
11+
#include <linux/of_address.h>
12+
#include <linux/of_platform.h>
13+
14+
#include "irq-msi-lib.h"
15+
16+
#define MIP_INT_RAISE 0x00
17+
#define MIP_INT_CLEAR 0x10
18+
#define MIP_INT_CFGL_HOST 0x20
19+
#define MIP_INT_CFGH_HOST 0x30
20+
#define MIP_INT_MASKL_HOST 0x40
21+
#define MIP_INT_MASKH_HOST 0x50
22+
#define MIP_INT_MASKL_VPU 0x60
23+
#define MIP_INT_MASKH_VPU 0x70
24+
#define MIP_INT_STATUSL_HOST 0x80
25+
#define MIP_INT_STATUSH_HOST 0x90
26+
#define MIP_INT_STATUSL_VPU 0xa0
27+
#define MIP_INT_STATUSH_VPU 0xb0
28+
29+
/**
30+
* struct mip_priv - MSI-X interrupt controller data
31+
* @lock: Used to protect bitmap alloc/free
32+
* @base: Base address of MMIO area
33+
* @msg_addr: PCIe MSI-X address
34+
* @msi_base: MSI base
35+
* @num_msis: Count of MSIs
36+
* @msi_offset: MSI offset
37+
* @bitmap: A bitmap for hwirqs
38+
* @parent: Parent domain (GIC)
39+
* @dev: A device pointer
40+
*/
41+
struct mip_priv {
42+
spinlock_t lock;
43+
void __iomem *base;
44+
u64 msg_addr;
45+
u32 msi_base;
46+
u32 num_msis;
47+
u32 msi_offset;
48+
unsigned long *bitmap;
49+
struct irq_domain *parent;
50+
struct device *dev;
51+
};
52+
53+
static void mip_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
54+
{
55+
struct mip_priv *mip = irq_data_get_irq_chip_data(d);
56+
57+
msg->address_hi = upper_32_bits(mip->msg_addr);
58+
msg->address_lo = lower_32_bits(mip->msg_addr);
59+
msg->data = d->hwirq;
60+
}
61+
62+
static struct irq_chip mip_middle_irq_chip = {
63+
.name = "MIP",
64+
.irq_mask = irq_chip_mask_parent,
65+
.irq_unmask = irq_chip_unmask_parent,
66+
.irq_eoi = irq_chip_eoi_parent,
67+
.irq_set_affinity = irq_chip_set_affinity_parent,
68+
.irq_set_type = irq_chip_set_type_parent,
69+
.irq_compose_msi_msg = mip_compose_msi_msg,
70+
};
71+
72+
static int mip_alloc_hwirq(struct mip_priv *mip, unsigned int nr_irqs)
73+
{
74+
guard(spinlock)(&mip->lock);
75+
return bitmap_find_free_region(mip->bitmap, mip->num_msis, ilog2(nr_irqs));
76+
}
77+
78+
static void mip_free_hwirq(struct mip_priv *mip, unsigned int hwirq,
79+
unsigned int nr_irqs)
80+
{
81+
guard(spinlock)(&mip->lock);
82+
bitmap_release_region(mip->bitmap, hwirq, ilog2(nr_irqs));
83+
}
84+
85+
static int mip_middle_domain_alloc(struct irq_domain *domain, unsigned int virq,
86+
unsigned int nr_irqs, void *arg)
87+
{
88+
struct mip_priv *mip = domain->host_data;
89+
struct irq_fwspec fwspec = {0};
90+
unsigned int hwirq, i;
91+
struct irq_data *irqd;
92+
int irq, ret;
93+
94+
irq = mip_alloc_hwirq(mip, nr_irqs);
95+
if (irq < 0)
96+
return irq;
97+
98+
hwirq = irq + mip->msi_offset;
99+
100+
fwspec.fwnode = domain->parent->fwnode;
101+
fwspec.param_count = 3;
102+
fwspec.param[0] = 0;
103+
fwspec.param[1] = hwirq + mip->msi_base;
104+
fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
105+
106+
ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, &fwspec);
107+
if (ret)
108+
goto err_free_hwirq;
109+
110+
for (i = 0; i < nr_irqs; i++) {
111+
irqd = irq_domain_get_irq_data(domain->parent, virq + i);
112+
irqd->chip->irq_set_type(irqd, IRQ_TYPE_EDGE_RISING);
113+
114+
ret = irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
115+
&mip_middle_irq_chip, mip);
116+
if (ret)
117+
goto err_free;
118+
119+
irqd = irq_get_irq_data(virq + i);
120+
irqd_set_single_target(irqd);
121+
irqd_set_affinity_on_activate(irqd);
122+
}
123+
124+
return 0;
125+
126+
err_free:
127+
irq_domain_free_irqs_parent(domain, virq, nr_irqs);
128+
err_free_hwirq:
129+
mip_free_hwirq(mip, irq, nr_irqs);
130+
return ret;
131+
}
132+
133+
static void mip_middle_domain_free(struct irq_domain *domain, unsigned int virq,
134+
unsigned int nr_irqs)
135+
{
136+
struct irq_data *irqd = irq_domain_get_irq_data(domain, virq);
137+
struct mip_priv *mip;
138+
unsigned int hwirq;
139+
140+
if (!irqd)
141+
return;
142+
143+
mip = irq_data_get_irq_chip_data(irqd);
144+
hwirq = irqd_to_hwirq(irqd);
145+
irq_domain_free_irqs_parent(domain, virq, nr_irqs);
146+
mip_free_hwirq(mip, hwirq - mip->msi_offset, nr_irqs);
147+
}
148+
149+
static const struct irq_domain_ops mip_middle_domain_ops = {
150+
.select = msi_lib_irq_domain_select,
151+
.alloc = mip_middle_domain_alloc,
152+
.free = mip_middle_domain_free,
153+
};
154+
155+
#define MIP_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
156+
MSI_FLAG_USE_DEF_CHIP_OPS | \
157+
MSI_FLAG_PCI_MSI_MASK_PARENT)
158+
159+
#define MIP_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK | \
160+
MSI_FLAG_MULTI_PCI_MSI | \
161+
MSI_FLAG_PCI_MSIX)
162+
163+
static const struct msi_parent_ops mip_msi_parent_ops = {
164+
.supported_flags = MIP_MSI_FLAGS_SUPPORTED,
165+
.required_flags = MIP_MSI_FLAGS_REQUIRED,
166+
.bus_select_token = DOMAIN_BUS_GENERIC_MSI,
167+
.bus_select_mask = MATCH_PCI_MSI,
168+
.prefix = "MIP-MSI-",
169+
.init_dev_msi_info = msi_lib_init_dev_msi_info,
170+
};
171+
172+
static int mip_init_domains(struct mip_priv *mip, struct device_node *np)
173+
{
174+
struct irq_domain *middle;
175+
176+
middle = irq_domain_add_hierarchy(mip->parent, 0, mip->num_msis, np,
177+
&mip_middle_domain_ops, mip);
178+
if (!middle)
179+
return -ENOMEM;
180+
181+
irq_domain_update_bus_token(middle, DOMAIN_BUS_GENERIC_MSI);
182+
middle->dev = mip->dev;
183+
middle->flags |= IRQ_DOMAIN_FLAG_MSI_PARENT;
184+
middle->msi_parent_ops = &mip_msi_parent_ops;
185+
186+
/*
187+
* All MSI-X unmasked for the host, masked for the VPU, and edge-triggered.
188+
*/
189+
writel(0, mip->base + MIP_INT_MASKL_HOST);
190+
writel(0, mip->base + MIP_INT_MASKH_HOST);
191+
writel(~0, mip->base + MIP_INT_MASKL_VPU);
192+
writel(~0, mip->base + MIP_INT_MASKH_VPU);
193+
writel(~0, mip->base + MIP_INT_CFGL_HOST);
194+
writel(~0, mip->base + MIP_INT_CFGH_HOST);
195+
196+
return 0;
197+
}
198+
199+
static int mip_parse_dt(struct mip_priv *mip, struct device_node *np)
200+
{
201+
struct of_phandle_args args;
202+
u64 size;
203+
int ret;
204+
205+
ret = of_property_read_u32(np, "brcm,msi-offset", &mip->msi_offset);
206+
if (ret)
207+
mip->msi_offset = 0;
208+
209+
ret = of_parse_phandle_with_args(np, "msi-ranges", "#interrupt-cells",
210+
0, &args);
211+
if (ret)
212+
return ret;
213+
214+
ret = of_property_read_u32_index(np, "msi-ranges", args.args_count + 1,
215+
&mip->num_msis);
216+
if (ret)
217+
goto err_put;
218+
219+
ret = of_property_read_reg(np, 1, &mip->msg_addr, &size);
220+
if (ret)
221+
goto err_put;
222+
223+
mip->msi_base = args.args[1];
224+
225+
mip->parent = irq_find_host(args.np);
226+
if (!mip->parent)
227+
ret = -EINVAL;
228+
229+
err_put:
230+
of_node_put(args.np);
231+
return ret;
232+
}
233+
234+
static int __init mip_of_msi_init(struct device_node *node, struct device_node *parent)
235+
{
236+
struct platform_device *pdev;
237+
struct mip_priv *mip;
238+
int ret;
239+
240+
pdev = of_find_device_by_node(node);
241+
of_node_put(node);
242+
if (!pdev)
243+
return -EPROBE_DEFER;
244+
245+
mip = kzalloc(sizeof(*mip), GFP_KERNEL);
246+
if (!mip)
247+
return -ENOMEM;
248+
249+
spin_lock_init(&mip->lock);
250+
mip->dev = &pdev->dev;
251+
252+
ret = mip_parse_dt(mip, node);
253+
if (ret)
254+
goto err_priv;
255+
256+
mip->base = of_iomap(node, 0);
257+
if (!mip->base) {
258+
ret = -ENXIO;
259+
goto err_priv;
260+
}
261+
262+
mip->bitmap = bitmap_zalloc(mip->num_msis, GFP_KERNEL);
263+
if (!mip->bitmap) {
264+
ret = -ENOMEM;
265+
goto err_base;
266+
}
267+
268+
ret = mip_init_domains(mip, node);
269+
if (ret)
270+
goto err_map;
271+
272+
dev_dbg(&pdev->dev, "MIP: MSI-X count: %u, base: %u, offset: %u, msg_addr: %llx\n",
273+
mip->num_msis, mip->msi_base, mip->msi_offset, mip->msg_addr);
274+
275+
return 0;
276+
277+
err_map:
278+
bitmap_free(mip->bitmap);
279+
err_base:
280+
iounmap(mip->base);
281+
err_priv:
282+
kfree(mip);
283+
return ret;
284+
}
285+
286+
IRQCHIP_PLATFORM_DRIVER_BEGIN(mip_msi)
287+
IRQCHIP_MATCH("brcm,bcm2712-mip", mip_of_msi_init)
288+
IRQCHIP_PLATFORM_DRIVER_END(mip_msi)
289+
MODULE_DESCRIPTION("Broadcom BCM2712 MSI-X interrupt controller");
290+
MODULE_AUTHOR("Phil Elwell <[email protected]>");
291+
MODULE_AUTHOR("Stanimir Varbanov <[email protected]>");
292+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)