Message ID | 20231204203357.2897008-3-ben.wolsieffer@hefring.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3024978vqy; Mon, 4 Dec 2023 12:38:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IGi5FSqkC2kbwQ+0ajIOf9UzzpvOkaqZgeghEgHT304ZMzl6oqRLt/bwSM0ZaNAQbDZR3wT X-Received: by 2002:a05:6a20:4303:b0:18f:4779:6781 with SMTP id h3-20020a056a20430300b0018f47796781mr3315377pzk.105.1701722294839; Mon, 04 Dec 2023 12:38:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701722294; cv=none; d=google.com; s=arc-20160816; b=v9e01M/4aJQkVUq6ew/KJfj9wTcApN0Ysx506nwN6SRI/ZmSjXyHBTo4Ov3ll7nJpD ch0ET6t9jJfgE/pLPN5+ZPM+Ays1Y2mZrt7CPCM2ksNtlwmUabUYNRBM9iONZlmObCFk Rtp+vTgK8sDK8qVBQyEiJtgoTWY5iom8s3/zo/pSpq8rLzIRguOgo6sXt9NEbab1PX9K UR7ShNSQAEPZNyMidrLIBWdPjBB1LwY6fqih3NcuTOpcDWL39sPAKJQWNyoXHwtHxZ61 HcV+zyEnQyngCZY2VMhkFnaGiO7RnIsYC3BFh4ziCw2vSiXujP02+MvDAvjyUe03dXlS C0qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=FWagGRv/i1MW5G1zwpH94jc78fOBc/tv6/pgOnwcx6k=; fh=6R95xM684t/RtLGS1mkzgWVhZ9Mrm3iDfH0StUmJDQc=; b=DdBaPNQtFig4A3m0y9m9Yj2mJOehdUmOOp00sDtv+LjGgiPtKgWtDYZpYeyDkdL/fu ec2bRkLX7H3azPMGCjJr5ghBQmXBkEBNwvQoSObzUAr/n/97NKl0Nvjl+NhHhCB90usJ IJ87d8kj1HuVnMuuYoGzDyKvTK3T1SS54DRv55dEMiaN4C1uXf5NXm3IoWjQw9Crcxbj Dt65U8DTFCeHCLCzc+v5BLbyz9Yc/yDf+sb75zx1gWVxzpzAvp6I9kup+381JemIWIc6 Egs1nHe8DztsN+1j4fyzBGdvk5Rb+mcFfUr+f1htK7epIyJczPveTlrg+qQrOaKG35AE s7bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@hefring-com.20230601.gappssmtp.com header.s=20230601 header.b=b+vDXRMz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id h14-20020a056a00230e00b006cbe53e19fdsi8400445pfh.222.2023.12.04.12.38.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 12:38:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@hefring-com.20230601.gappssmtp.com header.s=20230601 header.b=b+vDXRMz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 4076180A9AB8; Mon, 4 Dec 2023 12:38:12 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345790AbjLDUhy (ORCPT <rfc822;chrisfriedt@gmail.com> + 99 others); Mon, 4 Dec 2023 15:37:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235536AbjLDUhY (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 4 Dec 2023 15:37:24 -0500 Received: from mail-oo1-xc32.google.com (mail-oo1-xc32.google.com [IPv6:2607:f8b0:4864:20::c32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7DD9A1BFB for <linux-kernel@vger.kernel.org>; Mon, 4 Dec 2023 12:35:39 -0800 (PST) Received: by mail-oo1-xc32.google.com with SMTP id 006d021491bc7-58d9a4e9464so2021190eaf.0 for <linux-kernel@vger.kernel.org>; Mon, 04 Dec 2023 12:35:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hefring-com.20230601.gappssmtp.com; s=20230601; t=1701722138; x=1702326938; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FWagGRv/i1MW5G1zwpH94jc78fOBc/tv6/pgOnwcx6k=; b=b+vDXRMze/C1r4e24CUeK2VhFvtLtGE/yojAiTPjpqNP0zOjoF5EGSYgkfebMM87Ok 41+lNvkLHVfg5wYqgC1KSv/uAwiPfJSyj4j7Wd1KK50zRp8lCKOJ1j47MMBI5scT3KD/ WAQ+MtoH1Wjx5c0IqOD9lgVW3pCnmt35I8R4Rs4bsn7a2x0XWEBGWQ8tfy/5XhRoIA2h Hgw2AlUxovbI+3Wt1XQKjrH6EjX5lLQfObCXV0uUrlyzWLCZB4o7LlxBuhU/hwYBVx9f g7qKnCxGz68O4qb8h1t+Nz5dGEw3Bn8nkXAHX0vsYyFMLcVKhpQ7D+EgH3/bOuJMjNoC YdDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701722138; x=1702326938; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FWagGRv/i1MW5G1zwpH94jc78fOBc/tv6/pgOnwcx6k=; b=kBLLVQ/0+VJscP7aT3TRBfNtpHBCI2UCcZVzfnN+0Z/rpwNmVsnGGGnLgXR5YB/Zm7 Mbki3n6GPL5tqsQW/uQlys2zyaFunk/uyC2xNNNUAZe1PN3nwMfI9nanksUzOn8RALKe qWpuw1Za3n/dOCoXw6d/dOHHdv6aJfFCEvjPqD+0I4eMR9m3etXLroEUtNRMZ6/goum5 b5DV/GndkQ4WqGawZYTfdeM4GoED9ymc0FwclZM1IEWS3Rb543A2qgTLe05dl/8BQH2H +QlFjApURHP21Mjsa+A6FD7++IZoBWHUd9yruvt231IgWU+F1+jBDBAVgKx+1AFtZuF0 6Fnw== X-Gm-Message-State: AOJu0YzPMTC3xM1QaTQtl/ASM95Z//SVuqkEY03newOf2VlHC+QOttwt Boz5feoK03dLuc+hzjDRvcAEMSFcCY32PQI3/Tk= X-Received: by 2002:a05:6358:640b:b0:16d:f932:b5f with SMTP id f11-20020a056358640b00b0016df9320b5fmr1254764rwh.30.1701722138274; Mon, 04 Dec 2023 12:35:38 -0800 (PST) Received: from localhost.localdomain ([50.212.55.89]) by smtp.gmail.com with ESMTPSA id jo23-20020a056214501700b0067ac930d17asm1123469qvb.141.2023.12.04.12.35.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 12:35:37 -0800 (PST) From: Ben Wolsieffer <ben.wolsieffer@hefring.com> To: linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-gpio@vger.kernel.org Cc: Thomas Gleixner <tglx@linutronix.de>, Maxime Coquelin <mcoquelin.stm32@gmail.com>, Alexandre Torgue <alexandre.torgue@foss.st.com>, Linus Walleij <linus.walleij@linaro.org>, Ben Wolsieffer <ben.wolsieffer@hefring.com> Subject: [PATCH 2/2] pinctrl: stm32: fix GPIO level interrupts Date: Mon, 4 Dec 2023 15:33:57 -0500 Message-ID: <20231204203357.2897008-3-ben.wolsieffer@hefring.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231204203357.2897008-1-ben.wolsieffer@hefring.com> References: <20231204203357.2897008-1-ben.wolsieffer@hefring.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 04 Dec 2023 12:38:12 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784385157276999275 X-GMAIL-MSGID: 1784385157276999275 |
Series |
stm32: fix GPIO level interrupts
|
|
Commit Message
Ben Wolsieffer
Dec. 4, 2023, 8:33 p.m. UTC
The STM32 doesn't support GPIO level interrupts in hardware, so the
driver tries to emulate them using edge interrupts, by retriggering the
interrupt if necessary based on the pin state after the handler
finishes.
Currently, this functionality does not work because the irqchip uses
handle_edge_irq(), which doesn't run the irq_eoi() or irq_unmask()
callbacks after handling the interrupt. This patch fixes this by using
handle_level_irq() for level interrupts, which causes irq_unmask() to be
called to retrigger the interrupt.
Signed-off-by: Ben Wolsieffer <ben.wolsieffer@hefring.com>
---
drivers/pinctrl/stm32/pinctrl-stm32.c | 3 +++
1 file changed, 3 insertions(+)
Comments
On Mon, Dec 4, 2023 at 9:35 PM Ben Wolsieffer <ben.wolsieffer@hefring.com> wrote: > The STM32 doesn't support GPIO level interrupts in hardware, so the > driver tries to emulate them using edge interrupts, by retriggering the > interrupt if necessary based on the pin state after the handler > finishes. > > Currently, this functionality does not work because the irqchip uses > handle_edge_irq(), which doesn't run the irq_eoi() or irq_unmask() > callbacks after handling the interrupt. This patch fixes this by using > handle_level_irq() for level interrupts, which causes irq_unmask() to be > called to retrigger the interrupt. > > Signed-off-by: Ben Wolsieffer <ben.wolsieffer@hefring.com> Reviewed-by: Linus Walleij <linus.walleij@linaro.org> Marc Z can apply all the patches to the irq tree once he's happy with the solution. Yours, Linus Walleij
On Thu, Dec 7, 2023 at 10:56 AM Linus Walleij <linus.walleij@linaro.org> wrote: > On Mon, Dec 4, 2023 at 9:35 PM Ben Wolsieffer > <ben.wolsieffer@hefring.com> wrote: > > > The STM32 doesn't support GPIO level interrupts in hardware, so the > > driver tries to emulate them using edge interrupts, by retriggering the > > interrupt if necessary based on the pin state after the handler > > finishes. > > > > Currently, this functionality does not work because the irqchip uses > > handle_edge_irq(), which doesn't run the irq_eoi() or irq_unmask() > > callbacks after handling the interrupt. This patch fixes this by using > > handle_level_irq() for level interrupts, which causes irq_unmask() to be > > called to retrigger the interrupt. > > > > Signed-off-by: Ben Wolsieffer <ben.wolsieffer@hefring.com> > > Reviewed-by: Linus Walleij <linus.walleij@linaro.org> > > Marc Z can apply all the patches to the irq tree once he's happy > with the solution. I see Marc stopped doing irq_chips, so tglx then! Yours, Linus Walleij
Ben! On Mon, Dec 04 2023 at 15:33, Ben Wolsieffer wrote: > The STM32 doesn't support GPIO level interrupts in hardware, so the > driver tries to emulate them using edge interrupts, by retriggering the > interrupt if necessary based on the pin state after the handler > finishes. > > Currently, this functionality does not work because the irqchip uses > handle_edge_irq(), which doesn't run the irq_eoi() or irq_unmask() > callbacks after handling the interrupt. This patch fixes this by using > handle_level_irq() for level interrupts, which causes irq_unmask() to be > called to retrigger the interrupt. This does not make any sense at all. irq_unmask() does not retrigger anything. It sets the corresponding bit in the mask register, not more not less. Switching to handle_level_irq() makes the following difference vs. handle_edge_irq() when an interrupt is handled (ignoring the inner loop): + irq_mask(); irq_ack(); .... handle(); .... + irq_unmask(); So in both cases irq_ack() clears the interrupt in the Pending register, right? Now comes the interesting difference. When the interrupt is raised again after irq_ack() while the handler is running, i.e. a full toggle from active to inactive and back to active where the back to active transition causes the edge detector to trigger, then: 1) in case of handle_edge_irq() this should immediately set it in the pending register again and raise another CPU interrupt, which should be handled once the interrupt service routine returned. 2) in case of handle_level_irq() this does not set it in the pending register because it's masked. The unmask will set the pending register bit _if_ and only _if_ the edge detector has latched the detection. No idea whether that's the case. The manual is exceptionally blury about this. So in theory both #1 and #2 should work. But the explanation in the changelog is fairy tale material. As I couldn't figure out why #1 would not work, I looked at the driver in more detail and also at the STM32 manual. That tells me that the irqchip driver is at least suboptimal. Why? The EXTI controller is just an intermediate between the peripheral (including GPIO pins) and the NVIC: |--------------| | Edge config | |-----------------| Source -| |---| Int. Mask logic |---> Dedicated NVIC interrupt | Edge detect | | |-----------------| |--------------| | | |-----------------| |-| Evt. Mask logic |---> CPU event input | |-----------------| | | |-----------------| |-| Wakeup logic |--->.... |-----------------| So there are two classes of sources conntect to EXTI: 1) Direct events - Have a fixed edge - Can be masked for Interrupt and Event generation - No software trigger - Not tracked in the Pending register - Can evtl. wakeup the CPUs or from D3 2) Configurable events - Have a configurable edge - Can be masked for Interrupt and Event generation - Software trigger - Tracked in the Pending register - Can evtl. wakeup the CPUs or from D3 The CPU event is a single input to the CPU which can be triggered by any source which has the Event mask enabled. For both classes there are sources which have no connection to the NVIC, they can only be used to generate CPU events or trigger the wakeup logic. For direct events there is a category where the peripherial interrupt is routed to both the EXTI and the NVIC directly. The EXTI does not provide a connection to the NVIC and the event cannot be masked in EXTI to prevent CPU interrupts. Only the CPU event masking works. GPIO pins are configurable events which are connected to the NVIC via the EXTI. But the EXTI driver implements a chained interrupt handler which listens on a ton of NVIC interrupts. I.e. for the STM32H7 on: <1>, <2>, <3>, <6>, <7>, <8>, <9>, <10>, <23>, <40>, <41>, <62>, <76> NVIC 1: PVD_PVM EXTI-SRC 16 NVIC 2: RTC_TAMP_STAMP EXTI-SRC 18 NVIC 3: RTC_WAKEUP EXTI-SRC 19 NVIC 6: EXTI0 EXTI-SRC 0 NVIC 7: EXTI1 EXTI-SRC 1 NVIC 8: EXTI2 EXTI-SRC 2 NVIC 9: EXTI3 EXTI-SRC 3 NVIC 10: EXTI4 EXTI-SRC 4 NVIC 23: EXTI5-9 EXTI-SRC 5-9 NVIC 40: EXTI10-15 EXTI-SRC 10-15 NVIC 41: RTC_ALARM EXTI-SRC 17 NVIC 62: ETH_WKUP EXTI-SRC 86 NVIC 76: OTG_HS_WKUP EXTI-SRC 43 Each of these chained interrupts handles the full EXTI interrupt domain with all three banks. This does not make any sense at all especially not on a SMP machine. Though it _should_ work, but it might cause interrupts handlers to be invoked when nothing is pending when the edge handler is active. Which in turn can confuse the underlying device driver depending on the quality... CPU0 CPU1 NVIC int X NVIC int Y // read_pending() is serialized by a lock, but both read the same state pend = read_pending() pend = read_pending() for_each_bit(bit, pend) for_each_bit(bit, pend) handle_irq(domain, base + bit) handle_irq(domain, base + bit) lock(desc); lock(desc); ack(); do { clear(PENDING); set(IN_PROGRESS); unlock(desc); handle(); if (IN_PROGRESS) { lock(desc); ack(); set(PENDING); unlock(desc); clear(IN_PROGRESS); return; } } while (PENDING); <- Will loop See? In fact the only NVIC interrupts which actually need demultiplexing are NVIC #23 and NVIC #40 and those should only care about the EXTI interrupts which are actually multiplexed on them. This let's randomly run whatever is pending on any demux handler is far from correct. All others are direct NVIC interrupts which just have the extra EXTI interrupt masking, event masking and the wakeup magic. The indirection via the chained handler is just pointless overhead and not necessarily correct. The exti_h variant of that driver does the right thing and installs a hierarchical interrupt domain which acts as man in the middle between the source and the NVIC. Though granted they don't have the odd problem of multiplexing several GPIO interrupts to a single NVIC interrupt. But in fact the regular exti driver could do the same and just handle the two NVIC interrupts which need demultiplexing separately and let everything else go through the hierarchy without bells and whistles. Thanks, tglx
Thank you for taking the time to look through the STM32 manual and review the driver. Unfortunately, I don't have enough experience with the Linux IRQ subsystem to give you a correspondingly in depth response, but I will attempt to address the parts that relate to the bug in question. On Fri, Dec 08, 2023 at 09:43:21PM +0100, Thomas Gleixner wrote: > Ben! > > On Mon, Dec 04 2023 at 15:33, Ben Wolsieffer wrote: > > The STM32 doesn't support GPIO level interrupts in hardware, so the > > driver tries to emulate them using edge interrupts, by retriggering the > > interrupt if necessary based on the pin state after the handler > > finishes. > > > > Currently, this functionality does not work because the irqchip uses > > handle_edge_irq(), which doesn't run the irq_eoi() or irq_unmask() > > callbacks after handling the interrupt. This patch fixes this by using > > handle_level_irq() for level interrupts, which causes irq_unmask() to be > > called to retrigger the interrupt. > > This does not make any sense at all. irq_unmask() does not retrigger > anything. It sets the corresponding bit in the mask register, not more > not less. I don't think this is correct. I was referring to stm32_gpio_irq_unmask(), which calls stm32_gpio_irq_trigger(), which in turn (for level interrupts) checks the GPIO pin state and retriggers the interrupt if necessary. > > Switching to handle_level_irq() makes the following difference > vs. handle_edge_irq() when an interrupt is handled (ignoring the inner > loop): > > + irq_mask(); > irq_ack(); > .... > handle(); > .... > + irq_unmask(); Yes, the additional call to irq_unmask() is the key difference here, as that callback performs the retriggering for level interrupts. > > So in both cases irq_ack() clears the interrupt in the Pending register, > right? > > Now comes the interesting difference. > > When the interrupt is raised again after irq_ack() while the handler is > running, i.e. a full toggle from active to inactive and back to active > where the back to active transition causes the edge detector to trigger, > then: I don't see how this is relevant. The bug occurs with level interrupts in the case where there are no new transitions while the handler is running. For example, with a high level interrupt, if the pin is still high after the handler finishes, the interrupt should be immediately triggered again. > > 1) in case of handle_edge_irq() this should immediately set it in the > pending register again and raise another CPU interrupt, which > should be handled once the interrupt service routine returned. > > 2) in case of handle_level_irq() this does not set it in the pending > register because it's masked. The unmask will set the pending > register bit _if_ and only _if_ the edge detector has latched the > detection. No idea whether that's the case. The manual is > exceptionally blury about this. > > So in theory both #1 and #2 should work. But the explanation in the > changelog is fairy tale material. > > As I couldn't figure out why #1 would not work, I looked at the driver > in more detail and also at the STM32 manual. That tells me that the > irqchip driver is at least suboptimal. Why? > > The EXTI controller is just an intermediate between the peripheral > (including GPIO pins) and the NVIC: > > |--------------| > | Edge config | |-----------------| > Source -| |---| Int. Mask logic |---> Dedicated NVIC interrupt > | Edge detect | | |-----------------| > |--------------| | > | |-----------------| > |-| Evt. Mask logic |---> CPU event input > | |-----------------| > | > | |-----------------| > |-| Wakeup logic |--->.... > |-----------------| > > So there are two classes of sources conntect to EXTI: > > 1) Direct events > > - Have a fixed edge > - Can be masked for Interrupt and Event generation > - No software trigger > - Not tracked in the Pending register > - Can evtl. wakeup the CPUs or from D3 > > 2) Configurable events > > - Have a configurable edge > - Can be masked for Interrupt and Event generation > - Software trigger > - Tracked in the Pending register > - Can evtl. wakeup the CPUs or from D3 > > The CPU event is a single input to the CPU which can be triggered by any > source which has the Event mask enabled. > > For both classes there are sources which have no connection to the NVIC, > they can only be used to generate CPU events or trigger the wakeup > logic. > > For direct events there is a category where the peripherial interrupt is > routed to both the EXTI and the NVIC directly. The EXTI does not provide > a connection to the NVIC and the event cannot be masked in EXTI to > prevent CPU interrupts. Only the CPU event masking works. > > GPIO pins are configurable events which are connected to the NVIC via > the EXTI. > > But the EXTI driver implements a chained interrupt handler which listens > on a ton of NVIC interrupts. I.e. for the STM32H7 on: > > <1>, <2>, <3>, <6>, <7>, <8>, <9>, <10>, <23>, <40>, <41>, <62>, <76> > > NVIC 1: PVD_PVM EXTI-SRC 16 > NVIC 2: RTC_TAMP_STAMP EXTI-SRC 18 > NVIC 3: RTC_WAKEUP EXTI-SRC 19 > NVIC 6: EXTI0 EXTI-SRC 0 > NVIC 7: EXTI1 EXTI-SRC 1 > NVIC 8: EXTI2 EXTI-SRC 2 > NVIC 9: EXTI3 EXTI-SRC 3 > NVIC 10: EXTI4 EXTI-SRC 4 > NVIC 23: EXTI5-9 EXTI-SRC 5-9 > NVIC 40: EXTI10-15 EXTI-SRC 10-15 > NVIC 41: RTC_ALARM EXTI-SRC 17 > NVIC 62: ETH_WKUP EXTI-SRC 86 > NVIC 76: OTG_HS_WKUP EXTI-SRC 43 > > Each of these chained interrupts handles the full EXTI interrupt domain > with all three banks. This does not make any sense at all especially not > on a SMP machine. > > Though it _should_ work, but it might cause interrupts handlers to be > invoked when nothing is pending when the edge handler is active. Which > in turn can confuse the underlying device driver depending on the > quality... > > CPU0 CPU1 > > NVIC int X NVIC int Y > > // read_pending() is serialized by a lock, but both read the same state > pend = read_pending() pend = read_pending() > > for_each_bit(bit, pend) for_each_bit(bit, pend) > handle_irq(domain, base + bit) handle_irq(domain, base + bit) > lock(desc); lock(desc); > ack(); > do { > clear(PENDING); > set(IN_PROGRESS); > unlock(desc); > handle(); if (IN_PROGRESS) { > lock(desc); ack(); > set(PENDING); > unlock(desc); > clear(IN_PROGRESS); return; > } > } while (PENDING); <- Will loop > > See? Currently, this driver is only used on single core machines, and the more complex devices use exti_h, so this shouldn't be a problem in practice. > > In fact the only NVIC interrupts which actually need demultiplexing are > NVIC #23 and NVIC #40 and those should only care about the EXTI > interrupts which are actually multiplexed on them. This let's randomly > run whatever is pending on any demux handler is far from correct. > > All others are direct NVIC interrupts which just have the extra EXTI > interrupt masking, event masking and the wakeup magic. The indirection > via the chained handler is just pointless overhead and not necessarily > correct. > > The exti_h variant of that driver does the right thing and installs a > hierarchical interrupt domain which acts as man in the middle between > the source and the NVIC. Though granted they don't have the odd problem > of multiplexing several GPIO interrupts to a single NVIC interrupt. > > But in fact the regular exti driver could do the same and just handle > the two NVIC interrupts which need demultiplexing separately and let > everything else go through the hierarchy without bells and whistles. This sounds reasonable to me. It did seem strange to me that the exti and exti_h drivers used such different approaches, although I wasn't aware of the reasons behind them. I think this refactoring is out of scope of this bug fix though. > > Thanks, > > tglx Thanks, Ben
Ben! On Tue, Dec 12 2023 at 10:35, Ben Wolsieffer wrote: > On Fri, Dec 08, 2023 at 09:43:21PM +0100, Thomas Gleixner wrote: >> On Mon, Dec 04 2023 at 15:33, Ben Wolsieffer wrote: >> > The STM32 doesn't support GPIO level interrupts in hardware, so the >> > driver tries to emulate them using edge interrupts, by retriggering the >> > interrupt if necessary based on the pin state after the handler >> > finishes. >> > >> > Currently, this functionality does not work because the irqchip uses >> > handle_edge_irq(), which doesn't run the irq_eoi() or irq_unmask() >> > callbacks after handling the interrupt. This patch fixes this by using >> > handle_level_irq() for level interrupts, which causes irq_unmask() to be >> > called to retrigger the interrupt. >> >> This does not make any sense at all. irq_unmask() does not retrigger >> anything. It sets the corresponding bit in the mask register, not more >> not less. > > I don't think this is correct. I was referring to > stm32_gpio_irq_unmask(), which calls stm32_gpio_irq_trigger(), which in > turn (for level interrupts) checks the GPIO pin state and retriggers the > interrupt if necessary. Ah. That makes a lot more sense. Sorry that I missed that driver detail. The changelog could mention explicitely where the retrigger comes from. >> Switching to handle_level_irq() makes the following difference >> vs. handle_edge_irq() when an interrupt is handled (ignoring the inner >> loop): >> >> + irq_mask(); >> irq_ack(); >> .... >> handle(); >> .... >> + irq_unmask(); > > Yes, the additional call to irq_unmask() is the key difference here, as > that callback performs the retriggering for level interrupts. Sorry to be pedantic here. irq_unmask() is a function in the interrupt core code which eventually ends up invoking the irqchip::irq_unmask(). >> When the interrupt is raised again after irq_ack() while the handler is >> running, i.e. a full toggle from active to inactive and back to active >> where the back to active transition causes the edge detector to trigger, >> then: > > I don't see how this is relevant. The bug occurs with level interrupts > in the case where there are no new transitions while the handler is > running. For example, with a high level interrupt, if the pin is still > high after the handler finishes, the interrupt should be immediately > triggered again. Ah. That's the problem you are trying to solve. Now we are getting closer to a proper description :) >> But in fact the regular exti driver could do the same and just handle >> the two NVIC interrupts which need demultiplexing separately and let >> everything else go through the hierarchy without bells and whistles. > > This sounds reasonable to me. It did seem strange to me that the exti > and exti_h drivers used such different approaches, although I wasn't > aware of the reasons behind them. I think this refactoring is out of > scope of this bug fix though. Sure. It just occured to me while looking at this stuff.. Care to resend with a proper explanation in the changelog? Thanks, tglx
On Mon, 2023-12-04 at 15:33 -0500, Ben Wolsieffer wrote: > The STM32 doesn't support GPIO level interrupts in hardware, so the > driver tries to emulate them using edge interrupts, by retriggering the > interrupt if necessary based on the pin state after the handler > finishes. > > Currently, this functionality does not work because the irqchip uses > handle_edge_irq(), which doesn't run the irq_eoi() or irq_unmask() > callbacks after handling the interrupt. This patch fixes this by using > handle_level_irq() for level interrupts, which causes irq_unmask() to be > called to retrigger the interrupt. > > Signed-off-by: Ben Wolsieffer <ben.wolsieffer@hefring.com> > --- > drivers/pinctrl/stm32/pinctrl-stm32.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/drivers/pinctrl/stm32/pinctrl-stm32.c b/drivers/pinctrl/stm32/pinctrl-stm32.c > index 603f900e88c1..fb9532601cbb 100644 > --- a/drivers/pinctrl/stm32/pinctrl-stm32.c > +++ b/drivers/pinctrl/stm32/pinctrl-stm32.c > @@ -348,12 +348,15 @@ static int stm32_gpio_set_type(struct irq_data *d, unsigned int type) > case IRQ_TYPE_EDGE_RISING: > case IRQ_TYPE_EDGE_FALLING: > case IRQ_TYPE_EDGE_BOTH: > + irq_set_handler_locked(d, handle_edge_irq); Hi, this patch causes a NULL pointer dereference and crashes the kernel boot on STM32 MPU's, either STM32MP13x, STM32MP15x and the new STM32MP25x. Please do not merge it as is. This pinctrl-stm32 driver is shared between STM32 MCUs and MPUs. In both cases the EXTI is the parent interrupt controller of this pinctrl, but despite the fact that there is a single file irq-stm32-exti.c, it contains two independent drivers, one for MCUs and the other for MPUs. Swapping in this function the irq_desc::handle_irq between handle_edge_irq() and handle_level_irq() is probably fine for MCU (I have not tested it). But on MPUs the default handler is handle_fasteoi_irq(); should not be changed here. Checking quickly ... this function calls irq_chip_set_type_parent() at the very end. It will in turn call EXTI's irq_set_type(), which has different implementations for MCU and MPU. By moving this handler swap in the MCU specific stm32_irq_set_type() it will not impact MPUs. Best Regards, Antonio > parent_type = type; > break; > case IRQ_TYPE_LEVEL_HIGH: > + irq_set_handler_locked(d, handle_level_irq); > parent_type = IRQ_TYPE_EDGE_RISING; > break; > case IRQ_TYPE_LEVEL_LOW: > + irq_set_handler_locked(d, handle_level_irq); > parent_type = IRQ_TYPE_EDGE_FALLING; > break; > default:
diff --git a/drivers/pinctrl/stm32/pinctrl-stm32.c b/drivers/pinctrl/stm32/pinctrl-stm32.c index 603f900e88c1..fb9532601cbb 100644 --- a/drivers/pinctrl/stm32/pinctrl-stm32.c +++ b/drivers/pinctrl/stm32/pinctrl-stm32.c @@ -348,12 +348,15 @@ static int stm32_gpio_set_type(struct irq_data *d, unsigned int type) case IRQ_TYPE_EDGE_RISING: case IRQ_TYPE_EDGE_FALLING: case IRQ_TYPE_EDGE_BOTH: + irq_set_handler_locked(d, handle_edge_irq); parent_type = type; break; case IRQ_TYPE_LEVEL_HIGH: + irq_set_handler_locked(d, handle_level_irq); parent_type = IRQ_TYPE_EDGE_RISING; break; case IRQ_TYPE_LEVEL_LOW: + irq_set_handler_locked(d, handle_level_irq); parent_type = IRQ_TYPE_EDGE_FALLING; break; default: