1 From 89b748416358e4e04765b9a4f20e1c3d256b9d9e Mon Sep 17 00:00:00 2001
2 From: Phil Elwell <phil@raspberrypi.com>
3 Date: Wed, 28 Jul 2021 11:13:39 +0100
4 Subject: [PATCH] irqchip: irq-bcm2712-mip: Support for 2712's MIP
6 irqchip: irq-bcm2712-mip: specify bitmap search size as ilog2(N) not N
8 Freeing also has the same interface.
10 irqchip: irq-bcm2712-mip: Fix build warnings
12 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
14 irqchip: bcm2712-mip: add a quick hack to optionally shift MSI vectors
16 There are two MIP peripherals in bcm2712, the first gets a first-class
17 treatment where 64 consecutive GIC SPIs are assigned to all 64 output
18 vectors. The second gets an agglomeration of 17 GIC SPIs, but only 8 of
19 these are consecutive starting at the 8th output vector.
21 For now, allow the use of this smaller contiguous range within a larger
24 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.com>
26 drivers/irqchip/Kconfig | 8 +
27 drivers/irqchip/Makefile | 1 +
28 drivers/irqchip/irq-bcm2712-mip.c | 325 ++++++++++++++++++++++++++++++
29 3 files changed, 334 insertions(+)
30 create mode 100644 drivers/irqchip/irq-bcm2712-mip.c
32 --- a/drivers/irqchip/Kconfig
33 +++ b/drivers/irqchip/Kconfig
34 @@ -109,6 +109,14 @@ config I8259
39 + bool "Broadcom 2712 MSI-X Interrupt Peripheral support"
41 + select GENERIC_IRQ_CHIP
44 + Enable support for the Broadcom BCM2712 MSI-X target peripheral.
48 select GENERIC_IRQ_CHIP
49 --- a/drivers/irqchip/Makefile
50 +++ b/drivers/irqchip/Makefile
51 @@ -63,6 +63,7 @@ obj-$(CONFIG_XTENSA_MX) += irq-xtensa-
52 obj-$(CONFIG_XILINX_INTC) += irq-xilinx-intc.o
53 obj-$(CONFIG_IRQ_CROSSBAR) += irq-crossbar.o
54 obj-$(CONFIG_SOC_VF610) += irq-vf610-mscm-ir.o
55 +obj-$(CONFIG_BCM2712_MIP) += irq-bcm2712-mip.o
56 obj-$(CONFIG_BCM6345_L1_IRQ) += irq-bcm6345-l1.o
57 obj-$(CONFIG_BCM7038_L1_IRQ) += irq-bcm7038-l1.o
58 obj-$(CONFIG_BCM7120_L2_IRQ) += irq-bcm7120-l2.o
60 +++ b/drivers/irqchip/irq-bcm2712-mip.c
62 +// SPDX-License-Identifier: GPL-2.0-only
64 + * Copyright (C) 2021 Raspberry Pi Ltd., All Rights Reserved.
67 +#include <linux/pci.h>
68 +#include <linux/msi.h>
69 +#include <linux/of.h>
70 +#include <linux/of_address.h>
71 +#include <linux/of_irq.h>
72 +#include <linux/of_pci.h>
74 +#include <linux/irqchip.h>
76 +#define MIP_INT_RAISED 0x00
77 +#define MIP_INT_CLEARED 0x10
78 +#define MIP_INT_CFGL_HOST 0x20
79 +#define MIP_INT_CFGH_HOST 0x30
80 +#define MIP_INT_MASKL_HOST 0x40
81 +#define MIP_INT_MASKH_HOST 0x50
82 +#define MIP_INT_MASKL_VPU 0x60
83 +#define MIP_INT_MASKH_VPU 0x70
84 +#define MIP_INT_STATUSL_HOST 0x80
85 +#define MIP_INT_STATUSH_HOST 0x90
86 +#define MIP_INT_STATUSL_VPU 0xa0
87 +#define MIP_INT_STATUSH_VPU 0xb0
90 + spinlock_t msi_map_lock;
92 + void * __iomem base;
93 + phys_addr_t msg_addr;
94 + u32 msi_base; /* The SGI number that MSIs start */
95 + u32 num_msis; /* The number of SGIs for MSIs */
96 + u32 msi_offset; /* Shift the allocated msi up by N */
97 + unsigned long *msi_map;
100 +static void mip_mask_msi_irq(struct irq_data *d)
102 + pci_msi_mask_irq(d);
103 + irq_chip_mask_parent(d);
106 +static void mip_unmask_msi_irq(struct irq_data *d)
108 + pci_msi_unmask_irq(d);
109 + irq_chip_unmask_parent(d);
112 +static void mip_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
114 + struct mip_priv *priv = irq_data_get_irq_chip_data(d);
116 + msg->address_hi = upper_32_bits(priv->msg_addr);
117 + msg->address_lo = lower_32_bits(priv->msg_addr);
118 + msg->data = d->hwirq;
121 +// The "bus-specific" irq_chip (the MIP doesn't _have_ to be used with PCIe)
123 +static struct irq_chip mip_msi_irq_chip = {
125 + .irq_unmask = mip_unmask_msi_irq,
126 + .irq_mask = mip_mask_msi_irq,
127 + .irq_eoi = irq_chip_eoi_parent,
128 + .irq_set_affinity = irq_chip_set_affinity_parent,
131 +static struct msi_domain_info mip_msi_domain_info = {
132 + .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
133 + MSI_FLAG_PCI_MSIX),
134 + .chip = &mip_msi_irq_chip,
137 +// The "middle" irq_chip (the hardware control part)
139 +static struct irq_chip mip_irq_chip = {
141 + .irq_mask = irq_chip_mask_parent,
142 + .irq_unmask = irq_chip_unmask_parent,
143 + .irq_eoi = irq_chip_eoi_parent,
144 + .irq_set_affinity = irq_chip_set_affinity_parent,
145 + .irq_set_type = irq_chip_set_type_parent,
146 + .irq_compose_msi_msg = mip_compose_msi_msg,
150 +// And a domain to connect it to its parent (the GIC)
152 +static int mip_irq_domain_alloc(struct irq_domain *domain,
153 + unsigned int virq, unsigned int nr_irqs,
156 + struct mip_priv *priv = domain->host_data;
157 + struct irq_fwspec fwspec;
158 + struct irq_data *irqd;
161 + spin_lock(&priv->msi_map_lock);
163 + hwirq = bitmap_find_free_region(priv->msi_map, priv->num_msis, ilog2(nr_irqs));
165 + spin_unlock(&priv->msi_map_lock);
170 + hwirq += priv->msi_offset;
171 + fwspec.fwnode = domain->parent->fwnode;
172 + fwspec.param_count = 3;
173 + fwspec.param[0] = 0;
174 + fwspec.param[1] = hwirq + priv->msi_base;
175 + fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
177 + ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, &fwspec);
181 + for (i = 0; i < nr_irqs; i++) {
182 + irqd = irq_domain_get_irq_data(domain->parent, virq + i);
183 + irqd->chip->irq_set_type(irqd, IRQ_TYPE_EDGE_RISING);
185 + irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
186 + &mip_irq_chip, priv);
187 + irqd = irq_get_irq_data(virq + i);
188 + irqd_set_single_target(irqd);
189 + irqd_set_affinity_on_activate(irqd);
195 +static void mip_irq_domain_free(struct irq_domain *domain,
196 + unsigned int virq, unsigned int nr_irqs)
198 + struct irq_data *d = irq_domain_get_irq_data(domain, virq);
199 + struct mip_priv *priv = irq_data_get_irq_chip_data(d);
201 + irq_domain_free_irqs_parent(domain, virq, nr_irqs);
202 + d->hwirq -= priv->msi_offset;
204 + spin_lock(&priv->msi_map_lock);
206 + bitmap_release_region(priv->msi_map, d->hwirq, ilog2(nr_irqs));
208 + spin_unlock(&priv->msi_map_lock);
212 +static int mip_irq_domain_activate(struct irq_domain *domain,
213 + struct irq_data *d, bool reserve)
215 + struct mip_priv *priv = irq_data_get_irq_chip_data(d);
216 + unsigned long flags;
217 + unsigned int irq = d->hwirq;
218 + void *__iomem reg = priv->base +
219 + ((irq < 32) ? MIP_INT_MASKL_HOST : MIP_INT_MASKH_HOST);
222 + spin_lock_irqsave(&priv->hw_lock, flags);
224 + val &= ~(1 << (irq % 32)); // Clear the mask
226 + spin_unlock_irqrestore(&priv->hw_lock, flags);
230 +static void mip_irq_domain_deactivate(struct irq_domain *domain,
231 + struct irq_data *d)
233 + struct mip_priv *priv = irq_data_get_irq_chip_data(d);
234 + unsigned long flags;
235 + unsigned int irq = d->hwirq - priv->msi_base;
236 + void *__iomem reg = priv->base +
237 + ((irq < 32) ? MIP_INT_MASKL_HOST : MIP_INT_MASKH_HOST);
240 + spin_lock_irqsave(&priv->hw_lock, flags);
242 + val |= (1 << (irq % 32)); // Mask it out
244 + spin_unlock_irqrestore(&priv->hw_lock, flags);
248 +static const struct irq_domain_ops mip_irq_domain_ops = {
249 + .alloc = mip_irq_domain_alloc,
250 + .free = mip_irq_domain_free,
251 + //.activate = mip_irq_domain_activate,
252 + //.deactivate = mip_irq_domain_deactivate,
255 +static int mip_init_domains(struct mip_priv *priv,
256 + struct device_node *node)
258 + struct irq_domain *middle_domain, *msi_domain, *gic_domain;
259 + struct device_node *gic_node;
261 + gic_node = of_irq_find_parent(node);
263 + pr_err("Failed to find the GIC node\n");
267 + gic_domain = irq_find_host(gic_node);
269 + pr_err("Failed to find the GIC domain\n");
273 + middle_domain = irq_domain_add_tree(NULL,
274 + &mip_irq_domain_ops,
276 + if (!middle_domain) {
277 + pr_err("Failed to create the MIP middle domain\n");
281 + middle_domain->parent = gic_domain;
283 + msi_domain = pci_msi_create_irq_domain(of_node_to_fwnode(node),
284 + &mip_msi_domain_info,
287 + pr_err("Failed to create MSI domain\n");
288 + irq_domain_remove(middle_domain);
295 +static int __init mip_of_msi_init(struct device_node *node,
296 + struct device_node *parent)
298 + struct mip_priv *priv;
299 + struct resource res;
302 + priv = kzalloc(sizeof(*priv), GFP_KERNEL);
306 + spin_lock_init(&priv->msi_map_lock);
307 + spin_lock_init(&priv->hw_lock);
309 + ret = of_address_to_resource(node, 0, &res);
311 + pr_err("Failed to allocate resource\n");
315 + if (of_property_read_u32(node, "brcm,msi-base-spi", &priv->msi_base)) {
316 + pr_err("Unable to parse MSI base\n");
321 + if (of_property_read_u32(node, "brcm,msi-num-spis", &priv->num_msis)) {
322 + pr_err("Unable to parse MSI numbers\n");
327 + if (of_property_read_u32(node, "brcm,msi-offset", &priv->msi_offset))
328 + priv->msi_offset = 0;
330 + if (of_property_read_u64(node, "brcm,msi-pci-addr", &priv->msg_addr)) {
331 + pr_err("Unable to parse MSI address\n");
336 + priv->base = ioremap(res.start, resource_size(&res));
338 + pr_err("Failed to ioremap regs\n");
343 + priv->msi_map = kcalloc(BITS_TO_LONGS(priv->num_msis),
344 + sizeof(*priv->msi_map),
346 + if (!priv->msi_map) {
351 + pr_debug("Registering %d msixs, starting at %d\n",
352 + priv->num_msis, priv->msi_base);
355 + * Begin with all MSI-Xs masked in for the host, masked out for the
356 + * VPU, and edge-triggered.
358 + writel(0, priv->base + MIP_INT_MASKL_HOST);
359 + writel(0, priv->base + MIP_INT_MASKH_HOST);
360 + writel(~0, priv->base + MIP_INT_MASKL_VPU);
361 + writel(~0, priv->base + MIP_INT_MASKH_VPU);
362 + writel(~0, priv->base + MIP_INT_CFGL_HOST);
363 + writel(~0, priv->base + MIP_INT_CFGH_HOST);
365 + ret = mip_init_domains(priv, node);
367 + pr_err("Failed to allocate msi_map\n");
374 + kfree(priv->msi_map);
377 + iounmap(priv->base);
382 + pr_err("%s: failed - err %d\n", __func__, ret);
386 +IRQCHIP_DECLARE(bcm_mip, "brcm,bcm2712-mip-intc", mip_of_msi_init);