Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

irqchip/sun6i-r: Use a stacked irqchip driver

The R_INTC in the A31 and newer sun8i/sun50i SoCs is more similar to the
original sun4i interrupt controller than the sun7i/sun9i NMI controller.
It is used for two distinct purposes:
- To control the trigger, latch, and mask for the NMI input pin
- To provide the interrupt input for the ARISC coprocessor

As this interrupt controller is not documented, information about it
comes from vendor-provided firmware blobs and from experimentation.

Differences from the sun4i interrupt controller appear to be:
- It only has one or two registers of each kind (max 32 or 64 IRQs)
- Multiplexing logic is added to support additional inputs
- There is no FIQ-related logic
- There is no interrupt priority logic

In order to fulfill its two purposes, this hardware block combines four
types of IRQs. First, the NMI pin is routed to the "IRQ 0" input on this
chip, with a trigger type controlled by the NMI_CTRL_REG. The "IRQ 0
pending" output from this chip, if enabled, is then routed to a SPI IRQ
input on the GIC. In other words, bit 0 of IRQ_ENABLE_REG *does* affect
the NMI IRQ seen at the GIC.

The NMI is followed by a contiguous block of 15 "direct" (my name for
them) IRQ inputs that are connected in parallel to both R_INTC and the
GIC. Or in other words, these bits of IRQ_ENABLE_REG *do not* affect the
IRQs seen at the GIC.

Following the direct IRQs are the ARISC's copy of banked IRQs for shared
peripherals. These are not relevant to Linux. The remaining IRQs are
connected to a multiplexer and provide access to the first (up to) 128
SPIs from the ARISC. This range of SPIs overlaps with the direct IRQs.

Because of the 1:1 correspondence between R_INTC and GIC inputs, this is
a perfect scenario for using a stacked irqchip driver. We want to hook
into setting the NMI trigger type, but not actually handle any IRQ here.

To allow access to all multiplexed IRQs, this driver requires a new
binding where the interrupt number matches the GIC interrupt number.
(This moves the NMI from number 0 to 32 or 96, depending on the SoC.)
For simplicity, copy the three-cell GIC binding; this disambiguates
interrupt 0 in the old binding (the NMI) from interrupt 0 in the new
binding (SPI 0) by the number of cells.

Since R_INTC is in the always-on power domain, and its output is visible
to the power management coprocessor, a stacked irqchip driver provides a
simple way to add wakeup support to any of its IRQs. That is the next
patch; for now, just the NMI is moved over.

This commit mostly reverts commit 173bda53b340 ("irqchip/sunxi-nmi:
Support sun6i-a31-r-intc compatible").

Acked-by: Maxime Ripard <mripard@kernel.org>
Signed-off-by: Samuel Holland <samuel@sholland.org>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20210118055040.21910-4-samuel@sholland.org

authored by

Samuel Holland and committed by
Marc Zyngier
4e346146 6436eb44

+292 -23
+2
arch/arm/mach-sunxi/Kconfig
··· 6 6 select CLKSRC_MMIO 7 7 select GENERIC_IRQ_CHIP 8 8 select GPIOLIB 9 + select IRQ_DOMAIN_HIERARCHY 10 + select IRQ_FASTEOI_HIERARCHY_HANDLERS 9 11 select PINCTRL 10 12 select PM_OPP 11 13 select SUN4I_TIMER
+2
arch/arm64/Kconfig.platforms
··· 17 17 bool "Allwinner sunxi 64-bit SoC Family" 18 18 select ARCH_HAS_RESET_CONTROLLER 19 19 select GENERIC_IRQ_CHIP 20 + select IRQ_DOMAIN_HIERARCHY 21 + select IRQ_FASTEOI_HIERARCHY_HANDLERS 20 22 select PINCTRL 21 23 select RESET_CONTROLLER 22 24 help
+1
drivers/irqchip/Makefile
··· 24 24 obj-$(CONFIG_ORION_IRQCHIP) += irq-orion.o 25 25 obj-$(CONFIG_OMAP_IRQCHIP) += irq-omap-intc.o 26 26 obj-$(CONFIG_ARCH_SUNXI) += irq-sun4i.o 27 + obj-$(CONFIG_ARCH_SUNXI) += irq-sun6i-r.o 27 28 obj-$(CONFIG_ARCH_SUNXI) += irq-sunxi-nmi.o 28 29 obj-$(CONFIG_ARCH_SPEAR3XX) += spear-shirq.o 29 30 obj-$(CONFIG_ARM_GIC) += irq-gic.o irq-gic-common.o
+284
drivers/irqchip/irq-sun6i-r.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * The R_INTC in Allwinner A31 and newer SoCs manages several types of 4 + * interrupts, as shown below: 5 + * 6 + * NMI IRQ DIRECT IRQs MUXED IRQs 7 + * bit 0 bits 1-15^ bits 19-31 8 + * 9 + * +---------+ +---------+ +---------+ +---------+ 10 + * | NMI Pad | | IRQ d | | IRQ m | | IRQ m+7 | 11 + * +---------+ +---------+ +---------+ +---------+ 12 + * | | | | | | | 13 + * | | | | |......| | 14 + * +------V------+ +------------+ | | | +--V------V--+ | 15 + * | Invert/ | | Write 1 to | | | | | AND with | | 16 + * | Edge Detect | | PENDING[0] | | | | | MUX[m/8] | | 17 + * +-------------+ +------------+ | | | +------------+ | 18 + * | | | | | | | 19 + * +--V-------V--+ +--V--+ | +--V--+ | +--V--+ 20 + * | Set Reset| | GIC | | | GIC | | | GIC | 21 + * | Latch | | SPI | | | SPI |... | ...| SPI | 22 + * +-------------+ | N+d | | | m | | | m+7 | 23 + * | | +-----+ | +-----+ | +-----+ 24 + * | | | | 25 + * +-------V-+ +-V----------+ +---------V--+ +--------V--------+ 26 + * | GIC SPI | | AND with | | AND with | | AND with | 27 + * | N (=32) | | ENABLE[0] | | ENABLE[d] | | ENABLE[19+m/8] | 28 + * +---------+ +------------+ +------------+ +-----------------+ 29 + * | | | 30 + * +------V-----+ +------V-----+ +--------V--------+ 31 + * | Read | | Read | | Read | 32 + * | PENDING[0] | | PENDING[d] | | PENDING[19+m/8] | 33 + * +------------+ +------------+ +-----------------+ 34 + * 35 + * ^ bits 16-18 are direct IRQs for peripherals with banked interrupts, such as 36 + * the MSGBOX. These IRQs do not map to any GIC SPI. 37 + * 38 + * The H6 variant adds two more (banked) direct IRQs and implements the full 39 + * set of 128 mux bits. This requires a second set of top-level registers. 40 + */ 41 + 42 + #include <linux/interrupt.h> 43 + #include <linux/irq.h> 44 + #include <linux/irqchip.h> 45 + #include <linux/irqdomain.h> 46 + #include <linux/of.h> 47 + #include <linux/of_address.h> 48 + #include <linux/of_irq.h> 49 + 50 + #include <dt-bindings/interrupt-controller/arm-gic.h> 51 + 52 + #define SUN6I_NMI_CTRL (0x0c) 53 + #define SUN6I_IRQ_PENDING(n) (0x10 + 4 * (n)) 54 + #define SUN6I_IRQ_ENABLE(n) (0x40 + 4 * (n)) 55 + #define SUN6I_MUX_ENABLE(n) (0xc0 + 4 * (n)) 56 + 57 + #define SUN6I_NMI_SRC_TYPE_LEVEL_LOW 0 58 + #define SUN6I_NMI_SRC_TYPE_EDGE_FALLING 1 59 + #define SUN6I_NMI_SRC_TYPE_LEVEL_HIGH 2 60 + #define SUN6I_NMI_SRC_TYPE_EDGE_RISING 3 61 + 62 + #define SUN6I_NMI_BIT BIT(0) 63 + 64 + #define SUN6I_NMI_NEEDS_ACK ((void *)1) 65 + 66 + #define SUN6I_NR_TOP_LEVEL_IRQS 64 67 + #define SUN6I_NR_DIRECT_IRQS 16 68 + #define SUN6I_NR_MUX_BITS 128 69 + 70 + static void __iomem *base; 71 + static irq_hw_number_t nmi_hwirq; 72 + 73 + static void sun6i_r_intc_ack_nmi(void) 74 + { 75 + writel_relaxed(SUN6I_NMI_BIT, base + SUN6I_IRQ_PENDING(0)); 76 + } 77 + 78 + static void sun6i_r_intc_nmi_ack(struct irq_data *data) 79 + { 80 + if (irqd_get_trigger_type(data) & IRQ_TYPE_EDGE_BOTH) 81 + sun6i_r_intc_ack_nmi(); 82 + else 83 + data->chip_data = SUN6I_NMI_NEEDS_ACK; 84 + } 85 + 86 + static void sun6i_r_intc_nmi_eoi(struct irq_data *data) 87 + { 88 + /* For oneshot IRQs, delay the ack until the IRQ is unmasked. */ 89 + if (data->chip_data == SUN6I_NMI_NEEDS_ACK && !irqd_irq_masked(data)) { 90 + data->chip_data = NULL; 91 + sun6i_r_intc_ack_nmi(); 92 + } 93 + 94 + irq_chip_eoi_parent(data); 95 + } 96 + 97 + static void sun6i_r_intc_nmi_unmask(struct irq_data *data) 98 + { 99 + if (data->chip_data == SUN6I_NMI_NEEDS_ACK) { 100 + data->chip_data = NULL; 101 + sun6i_r_intc_ack_nmi(); 102 + } 103 + 104 + irq_chip_unmask_parent(data); 105 + } 106 + 107 + static int sun6i_r_intc_nmi_set_type(struct irq_data *data, unsigned int type) 108 + { 109 + u32 nmi_src_type; 110 + 111 + switch (type) { 112 + case IRQ_TYPE_EDGE_RISING: 113 + nmi_src_type = SUN6I_NMI_SRC_TYPE_EDGE_RISING; 114 + break; 115 + case IRQ_TYPE_EDGE_FALLING: 116 + nmi_src_type = SUN6I_NMI_SRC_TYPE_EDGE_FALLING; 117 + break; 118 + case IRQ_TYPE_LEVEL_HIGH: 119 + nmi_src_type = SUN6I_NMI_SRC_TYPE_LEVEL_HIGH; 120 + break; 121 + case IRQ_TYPE_LEVEL_LOW: 122 + nmi_src_type = SUN6I_NMI_SRC_TYPE_LEVEL_LOW; 123 + break; 124 + default: 125 + return -EINVAL; 126 + } 127 + 128 + writel_relaxed(nmi_src_type, base + SUN6I_NMI_CTRL); 129 + 130 + /* 131 + * The "External NMI" GIC input connects to a latch inside R_INTC, not 132 + * directly to the pin. So the GIC trigger type does not depend on the 133 + * NMI pin trigger type. 134 + */ 135 + return irq_chip_set_type_parent(data, IRQ_TYPE_LEVEL_HIGH); 136 + } 137 + 138 + static int sun6i_r_intc_nmi_set_irqchip_state(struct irq_data *data, 139 + enum irqchip_irq_state which, 140 + bool state) 141 + { 142 + if (which == IRQCHIP_STATE_PENDING && !state) 143 + sun6i_r_intc_ack_nmi(); 144 + 145 + return irq_chip_set_parent_state(data, which, state); 146 + } 147 + 148 + static struct irq_chip sun6i_r_intc_nmi_chip = { 149 + .name = "sun6i-r-intc", 150 + .irq_ack = sun6i_r_intc_nmi_ack, 151 + .irq_mask = irq_chip_mask_parent, 152 + .irq_unmask = sun6i_r_intc_nmi_unmask, 153 + .irq_eoi = sun6i_r_intc_nmi_eoi, 154 + .irq_set_affinity = irq_chip_set_affinity_parent, 155 + .irq_set_type = sun6i_r_intc_nmi_set_type, 156 + .irq_set_irqchip_state = sun6i_r_intc_nmi_set_irqchip_state, 157 + .flags = IRQCHIP_SET_TYPE_MASKED | 158 + IRQCHIP_SKIP_SET_WAKE, 159 + }; 160 + 161 + static int sun6i_r_intc_domain_translate(struct irq_domain *domain, 162 + struct irq_fwspec *fwspec, 163 + unsigned long *hwirq, 164 + unsigned int *type) 165 + { 166 + /* Accept the old two-cell binding for the NMI only. */ 167 + if (fwspec->param_count == 2 && fwspec->param[0] == 0) { 168 + *hwirq = nmi_hwirq; 169 + *type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK; 170 + return 0; 171 + } 172 + 173 + /* Otherwise this binding should match the GIC SPI binding. */ 174 + if (fwspec->param_count < 3) 175 + return -EINVAL; 176 + if (fwspec->param[0] != GIC_SPI) 177 + return -EINVAL; 178 + 179 + *hwirq = fwspec->param[1]; 180 + *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK; 181 + 182 + return 0; 183 + } 184 + 185 + static int sun6i_r_intc_domain_alloc(struct irq_domain *domain, 186 + unsigned int virq, 187 + unsigned int nr_irqs, void *arg) 188 + { 189 + struct irq_fwspec *fwspec = arg; 190 + struct irq_fwspec gic_fwspec; 191 + unsigned long hwirq; 192 + unsigned int type; 193 + int i, ret; 194 + 195 + ret = sun6i_r_intc_domain_translate(domain, fwspec, &hwirq, &type); 196 + if (ret) 197 + return ret; 198 + if (hwirq + nr_irqs > SUN6I_NR_MUX_BITS) 199 + return -EINVAL; 200 + 201 + /* Construct a GIC-compatible fwspec from this fwspec. */ 202 + gic_fwspec = (struct irq_fwspec) { 203 + .fwnode = domain->parent->fwnode, 204 + .param_count = 3, 205 + .param = { GIC_SPI, hwirq, type }, 206 + }; 207 + 208 + ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, &gic_fwspec); 209 + if (ret) 210 + return ret; 211 + 212 + for (i = 0; i < nr_irqs; ++i, ++hwirq, ++virq) { 213 + if (hwirq == nmi_hwirq) { 214 + irq_domain_set_hwirq_and_chip(domain, virq, hwirq, 215 + &sun6i_r_intc_nmi_chip, 0); 216 + irq_set_handler(virq, handle_fasteoi_ack_irq); 217 + } else { 218 + /* Only the NMI is currently supported. */ 219 + return -EINVAL; 220 + } 221 + } 222 + 223 + return 0; 224 + } 225 + 226 + static const struct irq_domain_ops sun6i_r_intc_domain_ops = { 227 + .translate = sun6i_r_intc_domain_translate, 228 + .alloc = sun6i_r_intc_domain_alloc, 229 + .free = irq_domain_free_irqs_common, 230 + }; 231 + 232 + static void sun6i_r_intc_resume(void) 233 + { 234 + int i; 235 + 236 + /* Only the NMI is relevant during normal operation. */ 237 + writel_relaxed(SUN6I_NMI_BIT, base + SUN6I_IRQ_ENABLE(0)); 238 + for (i = 1; i < BITS_TO_U32(SUN6I_NR_TOP_LEVEL_IRQS); ++i) 239 + writel_relaxed(0, base + SUN6I_IRQ_ENABLE(i)); 240 + } 241 + 242 + static int __init sun6i_r_intc_init(struct device_node *node, 243 + struct device_node *parent) 244 + { 245 + struct irq_domain *domain, *parent_domain; 246 + struct of_phandle_args nmi_parent; 247 + int ret; 248 + 249 + /* Extract the NMI hwirq number from the OF node. */ 250 + ret = of_irq_parse_one(node, 0, &nmi_parent); 251 + if (ret) 252 + return ret; 253 + if (nmi_parent.args_count < 3 || 254 + nmi_parent.args[0] != GIC_SPI || 255 + nmi_parent.args[2] != IRQ_TYPE_LEVEL_HIGH) 256 + return -EINVAL; 257 + nmi_hwirq = nmi_parent.args[1]; 258 + 259 + parent_domain = irq_find_host(parent); 260 + if (!parent_domain) { 261 + pr_err("%pOF: Failed to obtain parent domain\n", node); 262 + return -ENXIO; 263 + } 264 + 265 + base = of_io_request_and_map(node, 0, NULL); 266 + if (IS_ERR(base)) { 267 + pr_err("%pOF: Failed to map MMIO region\n", node); 268 + return PTR_ERR(base); 269 + } 270 + 271 + domain = irq_domain_add_hierarchy(parent_domain, 0, 0, node, 272 + &sun6i_r_intc_domain_ops, NULL); 273 + if (!domain) { 274 + pr_err("%pOF: Failed to allocate domain\n", node); 275 + iounmap(base); 276 + return -ENOMEM; 277 + } 278 + 279 + sun6i_r_intc_ack_nmi(); 280 + sun6i_r_intc_resume(); 281 + 282 + return 0; 283 + } 284 + IRQCHIP_DECLARE(sun6i_r_intc, "allwinner,sun6i-a31-r-intc", sun6i_r_intc_init);
+3 -23
drivers/irqchip/irq-sunxi-nmi.c
··· 27 27 28 28 #define SUNXI_NMI_IRQ_BIT BIT(0) 29 29 30 - #define SUN6I_R_INTC_CTRL 0x0c 31 - #define SUN6I_R_INTC_PENDING 0x10 32 - #define SUN6I_R_INTC_ENABLE 0x40 33 - 34 30 /* 35 31 * For deprecated sun6i-a31-sc-nmi compatible. 36 - * Registers are offset by 0x0c. 37 32 */ 38 - #define SUN6I_R_INTC_NMI_OFFSET 0x0c 39 - #define SUN6I_NMI_CTRL (SUN6I_R_INTC_CTRL - SUN6I_R_INTC_NMI_OFFSET) 40 - #define SUN6I_NMI_PENDING (SUN6I_R_INTC_PENDING - SUN6I_R_INTC_NMI_OFFSET) 41 - #define SUN6I_NMI_ENABLE (SUN6I_R_INTC_ENABLE - SUN6I_R_INTC_NMI_OFFSET) 33 + #define SUN6I_NMI_CTRL 0x00 34 + #define SUN6I_NMI_PENDING 0x04 35 + #define SUN6I_NMI_ENABLE 0x34 42 36 43 37 #define SUN7I_NMI_CTRL 0x00 44 38 #define SUN7I_NMI_PENDING 0x04 ··· 53 59 u32 ctrl; 54 60 u32 pend; 55 61 u32 enable; 56 - }; 57 - 58 - static const struct sunxi_sc_nmi_reg_offs sun6i_r_intc_reg_offs __initconst = { 59 - .ctrl = SUN6I_R_INTC_CTRL, 60 - .pend = SUN6I_R_INTC_PENDING, 61 - .enable = SUN6I_R_INTC_ENABLE, 62 62 }; 63 63 64 64 static const struct sunxi_sc_nmi_reg_offs sun6i_reg_offs __initconst = { ··· 219 231 220 232 return ret; 221 233 } 222 - 223 - static int __init sun6i_r_intc_irq_init(struct device_node *node, 224 - struct device_node *parent) 225 - { 226 - return sunxi_sc_nmi_irq_init(node, &sun6i_r_intc_reg_offs); 227 - } 228 - IRQCHIP_DECLARE(sun6i_r_intc, "allwinner,sun6i-a31-r-intc", 229 - sun6i_r_intc_irq_init); 230 234 231 235 static int __init sun6i_sc_nmi_irq_init(struct device_node *node, 232 236 struct device_node *parent)