Message ID | 20231218-rkisp-shirq-fix-v1-2-173007628248@ideasonboard.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-3131-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1088717dyi; Sun, 17 Dec 2023 23:55:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IFx7jRyInR/xj2TQkYe1yZuhjKPdeIismJ+G7LpeKqHkZmNrnCSfPxK9tPsZgQtc5sUYVsf X-Received: by 2002:a17:902:b786:b0:1d3:a9b3:e73e with SMTP id e6-20020a170902b78600b001d3a9b3e73emr2473016pls.9.1702886132642; Sun, 17 Dec 2023 23:55:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702886132; cv=none; d=google.com; s=arc-20160816; b=GcjSaNrjhjoZdLWbx1G3WlyqZjX9SkAh40p9331mM5W12rRUVektQvABrpOmk1O0Zm ihpIeY2xSSeYzZhdZ1v6m+1z7JX0U4PmgXAUi4QG9YXiVU15HTAAV3yAJuUoLxkI+qqb 41Y9Pc70jbVgOleysq1voAVvFoF0rgLgcxZ6SpuES9LoI3O+rx6m89dOXm0XZUNCT1ee /stkCtEuZJ3P3wN3RGeW4UdDdaDfjT6NK7LbOft5Aepzadvc9wEUkpsSB10e3QZYRfRc UDtARfA/Hq7J+Kbr5oPnsLYBPmFMt8lV84/y6bABhbzMlzPs8H2X8OdBmcXNIWHMHtrM CHYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=8WFsPCMiGv/VOBCL+NJvE15t4h8bXq09SOvqpRIehto=; fh=YrMUyF2BbiVWhYBsBmN9eCQP9kdPlePMowO92cqH0JY=; b=yth085eAPUgplG5cJWYsGPmQMCsvZoTdAUmq6cHURfI3Xx7K7bjztKAM5QqxAwT5n3 6p3sGTjk6y22laKKkB7eEqsGL2i7efeaz7l1UnWSAhiqfdKBB4CaSneslPaKbsaFsM06 IzS9D5WZrqAPAqxmOjZ2ow2rnHLGTSpAREoTUsvJLWydMQ4/+/pueUJJDBFSox5oquZe URe5bVBBejdK8yv7k3ihejVqEdpEH1MflS+r9KCIsEBINxRBoorpU15NyDPc3lOTKNya 3Kn+2l671wIKNvzqMbUbE62rvkUUHH+50+hLyjtp85OVbN23LPc7M2cbJrcyJS0Fee+p F8zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b=foXNIT6o; spf=pass (google.com: domain of linux-kernel+bounces-3131-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3131-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id h2-20020a170902f54200b001cfa70f3a2asi17900155plf.245.2023.12.17.23.55.32 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Dec 2023 23:55:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3131-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b=foXNIT6o; spf=pass (google.com: domain of linux-kernel+bounces-3131-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3131-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 649C6282D87 for <ouuuleilei@gmail.com>; Mon, 18 Dec 2023 07:55:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6022D111A1; Mon, 18 Dec 2023 07:54:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="foXNIT6o" X-Original-To: linux-kernel@vger.kernel.org Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2175B101EC; Mon, 18 Dec 2023 07:54:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ideasonboard.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ideasonboard.com Received: from [127.0.1.1] (91-158-149-209.elisa-laajakaista.fi [91.158.149.209]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 6C61C17E1; Mon, 18 Dec 2023 08:53:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1702886023; bh=VDN1suJaU5z+jG7P1v66MdGb21xQzNSju14tW5jmG7o=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=foXNIT6oCI3FbtDsp/JLvb8jXGr4NkF0SUp2AmuPmd+bthhLkmrrfeODZBeabQfoQ PGFjQt0RqS0nZlQFTSnWpgNhfnLX50q5FK7X9ClZk4/BUEjDWOiREQylaZALyS+7/e YViUDSwLvu2L0x0eidHR/5lTW79T8B/OP0vHlW9M= From: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com> Date: Mon, 18 Dec 2023 09:54:01 +0200 Subject: [PATCH 2/2] media: rkisp1: Fix IRQ handling due to shared interrupts Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20231218-rkisp-shirq-fix-v1-2-173007628248@ideasonboard.com> References: <20231218-rkisp-shirq-fix-v1-0-173007628248@ideasonboard.com> In-Reply-To: <20231218-rkisp-shirq-fix-v1-0-173007628248@ideasonboard.com> To: Dafna Hirschfeld <dafna@fastmail.com>, Laurent Pinchart <laurent.pinchart@ideasonboard.com>, Mauro Carvalho Chehab <mchehab@kernel.org>, Heiko Stuebner <heiko@sntech.de>, Mikhail Rudenko <mike.rudenko@gmail.com> Cc: Kieran Bingham <kieran.bingham@ideasonboard.com>, Paul Elder <paul.elder@ideasonboard.com>, linux-media@vger.kernel.org, linux-rockchip@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com> X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=openpgp-sha256; l=4836; i=tomi.valkeinen@ideasonboard.com; h=from:subject:message-id; bh=VDN1suJaU5z+jG7P1v66MdGb21xQzNSju14tW5jmG7o=; b=owEBbQKS/ZANAwAIAfo9qoy8lh71AcsmYgBlf/q0hIJva5BYwMg2k1BnwgKKL0b0LTvAfpzZI uM6u3QAj9eJAjMEAAEIAB0WIQTEOAw+ll79gQef86f6PaqMvJYe9QUCZX/6tAAKCRD6PaqMvJYe 9R8MD/4+BFDmq2y0WAYJccdpaV/7jBxDo1utw3M8tpuTNA5b6aN4Ta+v7ASUp3hdefBtnyNweEq Sr7GjqXxDmTXyivRGDHamXT+WEyYauQTcy2q6O4uM6n8KMVE+0d0zleQ13KIuhsa2CQhdrLcISc FkMvtbv6OY7Q94eYFUMXv3XYkpqfLAcXOziuelsl0lCMDKYZCIUWNiSQM15lIYddpsIlsCu0/L3 XFaaYXwmT4Een1UpMMwW9qSMJrwfGxsv4p9oJj28yfIxoxR8OqEmk5rz8nyNPRieZmpGi/DIpK1 2D+95EIlXs5LUfjqNXYvhHP8NBrjSFzfQLLSUqVDyw4IdeVxR5EDEdWHQ2klCjDBUWc1P8QcJlw O85F2lIGlOm231lWmIKglJA0stleqUKATzcsw7ixEAOxweh5cpfbdb57RVX1iIh2X0iTfM1IoR/ 8Up9CPFIuRJRB5Tfd8qb0wZrYXyblEej75WN05r5u8N8qvkHhTr+Hk62kPvm+pnvfn4Hd+g2Hu1 YytCOvFsLg4xBSy6lenERjN2jZxw6OfPTcTm0kuNG7MHK9hrxKksO0evUKceoQ8xPllVMx5bL7A 1fwTAUDLwJ+sVv33FmTAsBYWRMMrWpMDRTSqBgoI58ugNdel1WWcMEJuDdTM/4gme9/HJkxYtNK ZGwkT9NuHv3P72Q== X-Developer-Key: i=tomi.valkeinen@ideasonboard.com; a=openpgp; fpr=C4380C3E965EFD81079FF3A7FA3DAA8CBC961EF5 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785605529106643341 X-GMAIL-MSGID: 1785605529106643341 |
Series |
media: rkisp1: Fix shared interrupt handling
|
|
Commit Message
Tomi Valkeinen
Dec. 18, 2023, 7:54 a.m. UTC
The driver requests the interrupts as IRQF_SHARED, so the interrupt
handlers can be called at any time. If such a call happens while the ISP
is powered down, the SoC will hang as the driver tries to access the
ISP registers.
This can be reproduced even without the platform sharing the IRQ line:
Enable CONFIG_DEBUG_SHIRQ and unload the driver, and the board will
hang.
Fix this by adding a new field, 'irqs_enabled', which is used to bail
out from the interrupt handler when the ISP is not operational.
Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
---
.../platform/rockchip/rkisp1/rkisp1-capture.c | 3 +++
.../media/platform/rockchip/rkisp1/rkisp1-common.h | 2 ++
.../media/platform/rockchip/rkisp1/rkisp1-csi.c | 3 +++
.../media/platform/rockchip/rkisp1/rkisp1-dev.c | 22 ++++++++++++++++++++++
.../media/platform/rockchip/rkisp1/rkisp1-isp.c | 3 +++
5 files changed, 33 insertions(+)
Comments
Hi Tomi, Thank you for the patch. On Mon, Dec 18, 2023 at 09:54:01AM +0200, Tomi Valkeinen wrote: > The driver requests the interrupts as IRQF_SHARED, so the interrupt > handlers can be called at any time. If such a call happens while the ISP > is powered down, the SoC will hang as the driver tries to access the > ISP registers. > > This can be reproduced even without the platform sharing the IRQ line: > Enable CONFIG_DEBUG_SHIRQ and unload the driver, and the board will > hang. > > Fix this by adding a new field, 'irqs_enabled', which is used to bail > out from the interrupt handler when the ISP is not operational. > > Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com> > --- > .../platform/rockchip/rkisp1/rkisp1-capture.c | 3 +++ > .../media/platform/rockchip/rkisp1/rkisp1-common.h | 2 ++ > .../media/platform/rockchip/rkisp1/rkisp1-csi.c | 3 +++ > .../media/platform/rockchip/rkisp1/rkisp1-dev.c | 22 ++++++++++++++++++++++ > .../media/platform/rockchip/rkisp1/rkisp1-isp.c | 3 +++ > 5 files changed, 33 insertions(+) > > diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c > index aebd3c12020b..c381c22135a2 100644 > --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c > +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c > @@ -725,6 +725,9 @@ irqreturn_t rkisp1_capture_isr(int irq, void *ctx) > unsigned int i; > u32 status; > > + if (!rkisp1->irqs_enabled) > + return IRQ_NONE; Given that this is something all drivers that use shared IRQs have to do, would it make sense to use a standard helper here, such as pm_runtime_suspended() for instance ? I haven't looked at which one would be the most appropriate (if any), there's also pm_runtime_active() and pm_runtime_status_suspended(). That would simplify this patch. > + > status = rkisp1_read(rkisp1, RKISP1_CIF_MI_MIS); > if (!status) > return IRQ_NONE; > diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h b/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h > index 4b6b28c05b89..b757f75edecf 100644 > --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h > +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h > @@ -450,6 +450,7 @@ struct rkisp1_debug { > * @debug: debug params to be exposed on debugfs > * @info: version-specific ISP information > * @irqs: IRQ line numbers > + * @irqs_enabled: the hardware is enabled and can cause interrupts > */ > struct rkisp1_device { > void __iomem *base_addr; > @@ -471,6 +472,7 @@ struct rkisp1_device { > struct rkisp1_debug debug; > const struct rkisp1_info *info; > int irqs[RKISP1_NUM_IRQS]; > + bool irqs_enabled; > }; > > /* > diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c > index b6e47e2f1b94..4202642e0523 100644 > --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c > +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c > @@ -196,6 +196,9 @@ irqreturn_t rkisp1_csi_isr(int irq, void *ctx) > struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); > u32 val, status; > > + if (!rkisp1->irqs_enabled) > + return IRQ_NONE; > + > status = rkisp1_read(rkisp1, RKISP1_CIF_MIPI_MIS); > if (!status) > return IRQ_NONE; > diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c > index acc559652d6e..73cf08a74011 100644 > --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c > +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c > @@ -305,6 +305,24 @@ static int __maybe_unused rkisp1_runtime_suspend(struct device *dev) > { > struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); > > + rkisp1->irqs_enabled = false; > + /* Make sure the IRQ handler will see the above */ > + mb(); > + > + /* > + * Wait until any running IRQ handler has returned. The IRQ handler > + * may get called even after this (as it's a shared interrupt line) > + * but the 'irqs_enabled' flag will make the handler return immediately. > + */ > + for (unsigned int il = 0; il < ARRAY_SIZE(rkisp1->irqs); ++il) { > + if (rkisp1->irqs[il] == -1) > + continue; > + > + /* Skip if the irq line is the same as previous */ > + if (il == 0 || rkisp1->irqs[il - 1] != rkisp1->irqs[il]) > + synchronize_irq(rkisp1->irqs[il]); > + } > + > clk_bulk_disable_unprepare(rkisp1->clk_size, rkisp1->clks); > return pinctrl_pm_select_sleep_state(dev); > } > @@ -321,6 +339,10 @@ static int __maybe_unused rkisp1_runtime_resume(struct device *dev) > if (ret) > return ret; > > + rkisp1->irqs_enabled = true; > + /* Make sure the IRQ handler will see the above */ > + mb(); > + > return 0; > } > > diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c > index f00873d31c42..78a1f7a1499b 100644 > --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c > +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c > @@ -976,6 +976,9 @@ irqreturn_t rkisp1_isp_isr(int irq, void *ctx) > struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); > u32 status, isp_err; > > + if (!rkisp1->irqs_enabled) > + return IRQ_NONE; > + > status = rkisp1_read(rkisp1, RKISP1_CIF_ISP_MIS); > if (!status) > return IRQ_NONE;
On 18/12/2023 11:22, Laurent Pinchart wrote: > Hi Tomi, > > Thank you for the patch. > > On Mon, Dec 18, 2023 at 09:54:01AM +0200, Tomi Valkeinen wrote: >> The driver requests the interrupts as IRQF_SHARED, so the interrupt >> handlers can be called at any time. If such a call happens while the ISP >> is powered down, the SoC will hang as the driver tries to access the >> ISP registers. >> >> This can be reproduced even without the platform sharing the IRQ line: >> Enable CONFIG_DEBUG_SHIRQ and unload the driver, and the board will >> hang. >> >> Fix this by adding a new field, 'irqs_enabled', which is used to bail >> out from the interrupt handler when the ISP is not operational. >> >> Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com> >> --- >> .../platform/rockchip/rkisp1/rkisp1-capture.c | 3 +++ >> .../media/platform/rockchip/rkisp1/rkisp1-common.h | 2 ++ >> .../media/platform/rockchip/rkisp1/rkisp1-csi.c | 3 +++ >> .../media/platform/rockchip/rkisp1/rkisp1-dev.c | 22 ++++++++++++++++++++++ >> .../media/platform/rockchip/rkisp1/rkisp1-isp.c | 3 +++ >> 5 files changed, 33 insertions(+) >> >> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c >> index aebd3c12020b..c381c22135a2 100644 >> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c >> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c >> @@ -725,6 +725,9 @@ irqreturn_t rkisp1_capture_isr(int irq, void *ctx) >> unsigned int i; >> u32 status; >> >> + if (!rkisp1->irqs_enabled) >> + return IRQ_NONE; > > Given that this is something all drivers that use shared IRQs have to > do, would it make sense to use a standard helper here, such as > pm_runtime_suspended() for instance ? I haven't looked at which one > would be the most appropriate (if any), there's also > pm_runtime_active() and pm_runtime_status_suspended(). That would > simplify this patch. I did consider that when writing the patch. But I just wasn't very comfortable using the runtime PM here, even if it would make sense, as I'm just not quite sure how it works. For example, pm_runtime_suspended() checks if the device is in RPM_SUSPENDED state, and the device will be in RPM_SUSPENDED after the driver's suspend callback has finished. This makes sense. However, _while_ suspending (not after we have suspended), we want to make sure that 1) no new irq handling will start, 2) we'll wait until any currently running irq handler has finished. For 1), we can't use pm_runtime_suspended() in the irq handler, as the status is not RPM_SUSPENDED. We could probably check for: spin_lock(&dev->power.lock); off = dev->power.runtime_status == RPM_SUSPENDED || dev->power.runtime_status == RPM_SUSPENDING; spin_unlock(&dev->power.lock); if (off) return IRQ_NONE; That would not work if we would depend on the irq handling while in the suspend callback (e.g. waiting for an irq which signals that the device has finished processing). But we don't do that at the moment, and that kind of this probably can usually be done before calling runtime_put(). When we take into account the resume part, I think we could just check for RPM_ACTIVE in the irq handler, which would then rule out RPM_RESUMING, RPM_SUSPENDED and RPM_SUSPENDING. But we can't use pm_runtime_active(), as that also checks for dev->power.disable_depth. In other words, when we disable the PM for our device (e.g. when unloading the driver), the PM framework says our device became active. Soo... I think this should work in the irq handler: spin_lock(&dev->power.lock); active = dev->power.runtime_status == RPM_ACTIVE; spin_unlock(&dev->power.lock); if (!active) return IRQ_NONE; I think the driver depends on runtime PM, but if no-PM was an option, I guess we'd need to ifdef the above away, and trust that the device is always powered on. So, as I said in the beginning, "I just wasn't very comfortable using the runtime PM here". And that's still the case =). The runtime PM is horribly complex. If you think the above is clearer, and you think it's correct, I can make the change. Tomi > >> + >> status = rkisp1_read(rkisp1, RKISP1_CIF_MI_MIS); >> if (!status) >> return IRQ_NONE; >> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h b/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h >> index 4b6b28c05b89..b757f75edecf 100644 >> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h >> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h >> @@ -450,6 +450,7 @@ struct rkisp1_debug { >> * @debug: debug params to be exposed on debugfs >> * @info: version-specific ISP information >> * @irqs: IRQ line numbers >> + * @irqs_enabled: the hardware is enabled and can cause interrupts >> */ >> struct rkisp1_device { >> void __iomem *base_addr; >> @@ -471,6 +472,7 @@ struct rkisp1_device { >> struct rkisp1_debug debug; >> const struct rkisp1_info *info; >> int irqs[RKISP1_NUM_IRQS]; >> + bool irqs_enabled; >> }; >> >> /* >> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c >> index b6e47e2f1b94..4202642e0523 100644 >> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c >> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c >> @@ -196,6 +196,9 @@ irqreturn_t rkisp1_csi_isr(int irq, void *ctx) >> struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); >> u32 val, status; >> >> + if (!rkisp1->irqs_enabled) >> + return IRQ_NONE; >> + >> status = rkisp1_read(rkisp1, RKISP1_CIF_MIPI_MIS); >> if (!status) >> return IRQ_NONE; >> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c >> index acc559652d6e..73cf08a74011 100644 >> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c >> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c >> @@ -305,6 +305,24 @@ static int __maybe_unused rkisp1_runtime_suspend(struct device *dev) >> { >> struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); >> >> + rkisp1->irqs_enabled = false; >> + /* Make sure the IRQ handler will see the above */ >> + mb(); >> + >> + /* >> + * Wait until any running IRQ handler has returned. The IRQ handler >> + * may get called even after this (as it's a shared interrupt line) >> + * but the 'irqs_enabled' flag will make the handler return immediately. >> + */ >> + for (unsigned int il = 0; il < ARRAY_SIZE(rkisp1->irqs); ++il) { >> + if (rkisp1->irqs[il] == -1) >> + continue; >> + >> + /* Skip if the irq line is the same as previous */ >> + if (il == 0 || rkisp1->irqs[il - 1] != rkisp1->irqs[il]) >> + synchronize_irq(rkisp1->irqs[il]); >> + } >> + >> clk_bulk_disable_unprepare(rkisp1->clk_size, rkisp1->clks); >> return pinctrl_pm_select_sleep_state(dev); >> } >> @@ -321,6 +339,10 @@ static int __maybe_unused rkisp1_runtime_resume(struct device *dev) >> if (ret) >> return ret; >> >> + rkisp1->irqs_enabled = true; >> + /* Make sure the IRQ handler will see the above */ >> + mb(); >> + >> return 0; >> } >> >> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c >> index f00873d31c42..78a1f7a1499b 100644 >> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c >> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c >> @@ -976,6 +976,9 @@ irqreturn_t rkisp1_isp_isr(int irq, void *ctx) >> struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); >> u32 status, isp_err; >> >> + if (!rkisp1->irqs_enabled) >> + return IRQ_NONE; >> + >> status = rkisp1_read(rkisp1, RKISP1_CIF_ISP_MIS); >> if (!status) >> return IRQ_NONE; >
Hi Tomi, CC'ing Sakari On Tue, Dec 19, 2023 at 10:50:05AM +0200, Tomi Valkeinen wrote: > On 18/12/2023 11:22, Laurent Pinchart wrote: > > On Mon, Dec 18, 2023 at 09:54:01AM +0200, Tomi Valkeinen wrote: > >> The driver requests the interrupts as IRQF_SHARED, so the interrupt > >> handlers can be called at any time. If such a call happens while the ISP > >> is powered down, the SoC will hang as the driver tries to access the > >> ISP registers. > >> > >> This can be reproduced even without the platform sharing the IRQ line: > >> Enable CONFIG_DEBUG_SHIRQ and unload the driver, and the board will > >> hang. > >> > >> Fix this by adding a new field, 'irqs_enabled', which is used to bail > >> out from the interrupt handler when the ISP is not operational. > >> > >> Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com> > >> --- > >> .../platform/rockchip/rkisp1/rkisp1-capture.c | 3 +++ > >> .../media/platform/rockchip/rkisp1/rkisp1-common.h | 2 ++ > >> .../media/platform/rockchip/rkisp1/rkisp1-csi.c | 3 +++ > >> .../media/platform/rockchip/rkisp1/rkisp1-dev.c | 22 ++++++++++++++++++++++ > >> .../media/platform/rockchip/rkisp1/rkisp1-isp.c | 3 +++ > >> 5 files changed, 33 insertions(+) > >> > >> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c > >> index aebd3c12020b..c381c22135a2 100644 > >> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c > >> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c > >> @@ -725,6 +725,9 @@ irqreturn_t rkisp1_capture_isr(int irq, void *ctx) > >> unsigned int i; > >> u32 status; > >> > >> + if (!rkisp1->irqs_enabled) > >> + return IRQ_NONE; > > > > Given that this is something all drivers that use shared IRQs have to > > do, would it make sense to use a standard helper here, such as > > pm_runtime_suspended() for instance ? I haven't looked at which one > > would be the most appropriate (if any), there's also > > pm_runtime_active() and pm_runtime_status_suspended(). That would > > simplify this patch. > > I did consider that when writing the patch. But I just wasn't very > comfortable using the runtime PM here, even if it would make sense, as > I'm just not quite sure how it works. > > For example, pm_runtime_suspended() checks if the device is in > RPM_SUSPENDED state, and the device will be in RPM_SUSPENDED after the > driver's suspend callback has finished. This makes sense. > > However, _while_ suspending (not after we have suspended), we want to > make sure that 1) no new irq handling will start, 2) we'll wait until > any currently running irq handler has finished. For 1), we can't use > pm_runtime_suspended() in the irq handler, as the status is not > RPM_SUSPENDED. We could probably check for: > > spin_lock(&dev->power.lock); > off = dev->power.runtime_status == RPM_SUSPENDED || > dev->power.runtime_status == RPM_SUSPENDING; > spin_unlock(&dev->power.lock); > if (off) > return IRQ_NONE; > > That would not work if we would depend on the irq handling while in the > suspend callback (e.g. waiting for an irq which signals that the device > has finished processing). But we don't do that at the moment, and that > kind of this probably can usually be done before calling runtime_put(). > > When we take into account the resume part, I think we could just check > for RPM_ACTIVE in the irq handler, which would then rule out > RPM_RESUMING, RPM_SUSPENDED and RPM_SUSPENDING. > > But we can't use pm_runtime_active(), as that also checks for > dev->power.disable_depth. In other words, when we disable the PM for our > device (e.g. when unloading the driver), the PM framework says our > device became active. > > Soo... I think this should work in the irq handler: > > spin_lock(&dev->power.lock); > active = dev->power.runtime_status == RPM_ACTIVE; It would be nice to use pm_runtime_active() instead. This would however require unregistering the IRQ handler before disabling runtime PM in the remove path. I think that should be done nonetheless though, as relying on devm to unregister the IRQ handler means it will happen after .remove() returns, which could cause all sort of issues (I'm thinking about the calls to dev_get_drvdata() in the IRQ handlers for instance). What do you think ? > spin_unlock(&dev->power.lock); > if (!active) > return IRQ_NONE; > > I think the driver depends on runtime PM, but if no-PM was an option, I > guess we'd need to ifdef the above away, and trust that the device is > always powered on. > > So, as I said in the beginning, "I just wasn't very comfortable using > the runtime PM here". And that's still the case =). The runtime PM is > horribly complex. If you think the above is clearer, and you think it's > correct, I can make the change. It sounds it may require some more work, and we should land this fix in v6.8, with the revert, right ? If so, I'm fine merging this patch, and moving to runtime PM checks on top if we decide to do so. Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> By the way, I wonder if it would make sense to handle this in the driver core. The prospect of copying this code in all drivers doesn't make me happy. > >> + > >> status = rkisp1_read(rkisp1, RKISP1_CIF_MI_MIS); > >> if (!status) > >> return IRQ_NONE; > >> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h b/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h > >> index 4b6b28c05b89..b757f75edecf 100644 > >> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h > >> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h > >> @@ -450,6 +450,7 @@ struct rkisp1_debug { > >> * @debug: debug params to be exposed on debugfs > >> * @info: version-specific ISP information > >> * @irqs: IRQ line numbers > >> + * @irqs_enabled: the hardware is enabled and can cause interrupts > >> */ > >> struct rkisp1_device { > >> void __iomem *base_addr; > >> @@ -471,6 +472,7 @@ struct rkisp1_device { > >> struct rkisp1_debug debug; > >> const struct rkisp1_info *info; > >> int irqs[RKISP1_NUM_IRQS]; > >> + bool irqs_enabled; > >> }; > >> > >> /* > >> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c > >> index b6e47e2f1b94..4202642e0523 100644 > >> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c > >> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c > >> @@ -196,6 +196,9 @@ irqreturn_t rkisp1_csi_isr(int irq, void *ctx) > >> struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); > >> u32 val, status; > >> > >> + if (!rkisp1->irqs_enabled) > >> + return IRQ_NONE; > >> + > >> status = rkisp1_read(rkisp1, RKISP1_CIF_MIPI_MIS); > >> if (!status) > >> return IRQ_NONE; > >> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c > >> index acc559652d6e..73cf08a74011 100644 > >> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c > >> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c > >> @@ -305,6 +305,24 @@ static int __maybe_unused rkisp1_runtime_suspend(struct device *dev) > >> { > >> struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); > >> > >> + rkisp1->irqs_enabled = false; > >> + /* Make sure the IRQ handler will see the above */ > >> + mb(); > >> + > >> + /* > >> + * Wait until any running IRQ handler has returned. The IRQ handler > >> + * may get called even after this (as it's a shared interrupt line) > >> + * but the 'irqs_enabled' flag will make the handler return immediately. > >> + */ > >> + for (unsigned int il = 0; il < ARRAY_SIZE(rkisp1->irqs); ++il) { > >> + if (rkisp1->irqs[il] == -1) > >> + continue; > >> + > >> + /* Skip if the irq line is the same as previous */ > >> + if (il == 0 || rkisp1->irqs[il - 1] != rkisp1->irqs[il]) > >> + synchronize_irq(rkisp1->irqs[il]); > >> + } > >> + > >> clk_bulk_disable_unprepare(rkisp1->clk_size, rkisp1->clks); > >> return pinctrl_pm_select_sleep_state(dev); > >> } > >> @@ -321,6 +339,10 @@ static int __maybe_unused rkisp1_runtime_resume(struct device *dev) > >> if (ret) > >> return ret; > >> > >> + rkisp1->irqs_enabled = true; > >> + /* Make sure the IRQ handler will see the above */ > >> + mb(); > >> + > >> return 0; > >> } > >> > >> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c > >> index f00873d31c42..78a1f7a1499b 100644 > >> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c > >> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c > >> @@ -976,6 +976,9 @@ irqreturn_t rkisp1_isp_isr(int irq, void *ctx) > >> struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); > >> u32 status, isp_err; > >> > >> + if (!rkisp1->irqs_enabled) > >> + return IRQ_NONE; > >> + > >> status = rkisp1_read(rkisp1, RKISP1_CIF_ISP_MIS); > >> if (!status) > >> return IRQ_NONE;
On 19/12/2023 15:08, Laurent Pinchart wrote: > Hi Tomi, > > CC'ing Sakari > > On Tue, Dec 19, 2023 at 10:50:05AM +0200, Tomi Valkeinen wrote: >> On 18/12/2023 11:22, Laurent Pinchart wrote: >>> On Mon, Dec 18, 2023 at 09:54:01AM +0200, Tomi Valkeinen wrote: >>>> The driver requests the interrupts as IRQF_SHARED, so the interrupt >>>> handlers can be called at any time. If such a call happens while the ISP >>>> is powered down, the SoC will hang as the driver tries to access the >>>> ISP registers. >>>> >>>> This can be reproduced even without the platform sharing the IRQ line: >>>> Enable CONFIG_DEBUG_SHIRQ and unload the driver, and the board will >>>> hang. >>>> >>>> Fix this by adding a new field, 'irqs_enabled', which is used to bail >>>> out from the interrupt handler when the ISP is not operational. >>>> >>>> Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com> >>>> --- >>>> .../platform/rockchip/rkisp1/rkisp1-capture.c | 3 +++ >>>> .../media/platform/rockchip/rkisp1/rkisp1-common.h | 2 ++ >>>> .../media/platform/rockchip/rkisp1/rkisp1-csi.c | 3 +++ >>>> .../media/platform/rockchip/rkisp1/rkisp1-dev.c | 22 ++++++++++++++++++++++ >>>> .../media/platform/rockchip/rkisp1/rkisp1-isp.c | 3 +++ >>>> 5 files changed, 33 insertions(+) >>>> >>>> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c >>>> index aebd3c12020b..c381c22135a2 100644 >>>> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c >>>> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c >>>> @@ -725,6 +725,9 @@ irqreturn_t rkisp1_capture_isr(int irq, void *ctx) >>>> unsigned int i; >>>> u32 status; >>>> >>>> + if (!rkisp1->irqs_enabled) >>>> + return IRQ_NONE; >>> >>> Given that this is something all drivers that use shared IRQs have to >>> do, would it make sense to use a standard helper here, such as >>> pm_runtime_suspended() for instance ? I haven't looked at which one >>> would be the most appropriate (if any), there's also >>> pm_runtime_active() and pm_runtime_status_suspended(). That would >>> simplify this patch. >> >> I did consider that when writing the patch. But I just wasn't very >> comfortable using the runtime PM here, even if it would make sense, as >> I'm just not quite sure how it works. >> >> For example, pm_runtime_suspended() checks if the device is in >> RPM_SUSPENDED state, and the device will be in RPM_SUSPENDED after the >> driver's suspend callback has finished. This makes sense. >> >> However, _while_ suspending (not after we have suspended), we want to >> make sure that 1) no new irq handling will start, 2) we'll wait until >> any currently running irq handler has finished. For 1), we can't use >> pm_runtime_suspended() in the irq handler, as the status is not >> RPM_SUSPENDED. We could probably check for: >> >> spin_lock(&dev->power.lock); >> off = dev->power.runtime_status == RPM_SUSPENDED || >> dev->power.runtime_status == RPM_SUSPENDING; >> spin_unlock(&dev->power.lock); >> if (off) >> return IRQ_NONE; >> >> That would not work if we would depend on the irq handling while in the >> suspend callback (e.g. waiting for an irq which signals that the device >> has finished processing). But we don't do that at the moment, and that >> kind of this probably can usually be done before calling runtime_put(). >> >> When we take into account the resume part, I think we could just check >> for RPM_ACTIVE in the irq handler, which would then rule out >> RPM_RESUMING, RPM_SUSPENDED and RPM_SUSPENDING. >> >> But we can't use pm_runtime_active(), as that also checks for >> dev->power.disable_depth. In other words, when we disable the PM for our >> device (e.g. when unloading the driver), the PM framework says our >> device became active. >> >> Soo... I think this should work in the irq handler: >> >> spin_lock(&dev->power.lock); >> active = dev->power.runtime_status == RPM_ACTIVE; > > It would be nice to use pm_runtime_active() instead. This would however > require unregistering the IRQ handler before disabling runtime PM in the > remove path. I think that should be done nonetheless though, as relying > on devm to unregister the IRQ handler means it will happen after > .remove() returns, which could cause all sort of issues (I'm thinking > about the calls to dev_get_drvdata() in the IRQ handlers for instance). > What do you think ? I agree, devm with irqs sounds scary. What I'd like to do is reserve an irq handler without activating it. That call could return an error, if, e.g. there's no such irq. Then later I would enable it (could be in the resume callback, but as well in the start-stream call), which would never return an error. Having those both combined in a single call is not nice, as we have to deal with irq handler calls even when the driver knows it doesn't want them. >> spin_unlock(&dev->power.lock); >> if (!active) >> return IRQ_NONE; >> >> I think the driver depends on runtime PM, but if no-PM was an option, I >> guess we'd need to ifdef the above away, and trust that the device is >> always powered on. >> >> So, as I said in the beginning, "I just wasn't very comfortable using >> the runtime PM here". And that's still the case =). The runtime PM is >> horribly complex. If you think the above is clearer, and you think it's >> correct, I can make the change. > > It sounds it may require some more work, and we should land this fix in > v6.8, with the revert, right ? If so, I'm fine merging this patch, and > moving to runtime PM checks on top if we decide to do so. Yes. I think this fix should work as it is now. That said, I haven't heard anyone else reporting this issue, so maybe just applying the revert (to fix the driver) would be enough. And we could then figure out later how exactly to handle the shared interrupts. > Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> > > By the way, I wonder if it would make sense to handle this in the driver > core. The prospect of copying this code in all drivers doesn't make me > happy. Indeed. But I wonder if it's always like this. E.g. a "wakeup" irq which is supposed to happen while the device is off. Tomi >>>> + >>>> status = rkisp1_read(rkisp1, RKISP1_CIF_MI_MIS); >>>> if (!status) >>>> return IRQ_NONE; >>>> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h b/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h >>>> index 4b6b28c05b89..b757f75edecf 100644 >>>> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h >>>> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h >>>> @@ -450,6 +450,7 @@ struct rkisp1_debug { >>>> * @debug: debug params to be exposed on debugfs >>>> * @info: version-specific ISP information >>>> * @irqs: IRQ line numbers >>>> + * @irqs_enabled: the hardware is enabled and can cause interrupts >>>> */ >>>> struct rkisp1_device { >>>> void __iomem *base_addr; >>>> @@ -471,6 +472,7 @@ struct rkisp1_device { >>>> struct rkisp1_debug debug; >>>> const struct rkisp1_info *info; >>>> int irqs[RKISP1_NUM_IRQS]; >>>> + bool irqs_enabled; >>>> }; >>>> >>>> /* >>>> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c >>>> index b6e47e2f1b94..4202642e0523 100644 >>>> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c >>>> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c >>>> @@ -196,6 +196,9 @@ irqreturn_t rkisp1_csi_isr(int irq, void *ctx) >>>> struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); >>>> u32 val, status; >>>> >>>> + if (!rkisp1->irqs_enabled) >>>> + return IRQ_NONE; >>>> + >>>> status = rkisp1_read(rkisp1, RKISP1_CIF_MIPI_MIS); >>>> if (!status) >>>> return IRQ_NONE; >>>> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c >>>> index acc559652d6e..73cf08a74011 100644 >>>> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c >>>> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c >>>> @@ -305,6 +305,24 @@ static int __maybe_unused rkisp1_runtime_suspend(struct device *dev) >>>> { >>>> struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); >>>> >>>> + rkisp1->irqs_enabled = false; >>>> + /* Make sure the IRQ handler will see the above */ >>>> + mb(); >>>> + >>>> + /* >>>> + * Wait until any running IRQ handler has returned. The IRQ handler >>>> + * may get called even after this (as it's a shared interrupt line) >>>> + * but the 'irqs_enabled' flag will make the handler return immediately. >>>> + */ >>>> + for (unsigned int il = 0; il < ARRAY_SIZE(rkisp1->irqs); ++il) { >>>> + if (rkisp1->irqs[il] == -1) >>>> + continue; >>>> + >>>> + /* Skip if the irq line is the same as previous */ >>>> + if (il == 0 || rkisp1->irqs[il - 1] != rkisp1->irqs[il]) >>>> + synchronize_irq(rkisp1->irqs[il]); >>>> + } >>>> + >>>> clk_bulk_disable_unprepare(rkisp1->clk_size, rkisp1->clks); >>>> return pinctrl_pm_select_sleep_state(dev); >>>> } >>>> @@ -321,6 +339,10 @@ static int __maybe_unused rkisp1_runtime_resume(struct device *dev) >>>> if (ret) >>>> return ret; >>>> >>>> + rkisp1->irqs_enabled = true; >>>> + /* Make sure the IRQ handler will see the above */ >>>> + mb(); >>>> + >>>> return 0; >>>> } >>>> >>>> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c >>>> index f00873d31c42..78a1f7a1499b 100644 >>>> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c >>>> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c >>>> @@ -976,6 +976,9 @@ irqreturn_t rkisp1_isp_isr(int irq, void *ctx) >>>> struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); >>>> u32 status, isp_err; >>>> >>>> + if (!rkisp1->irqs_enabled) >>>> + return IRQ_NONE; >>>> + >>>> status = rkisp1_read(rkisp1, RKISP1_CIF_ISP_MIS); >>>> if (!status) >>>> return IRQ_NONE; >
Hi Laurent, Tomi, On Tue, Dec 19, 2023 at 03:08:49PM +0200, Laurent Pinchart wrote: > Hi Tomi, > > CC'ing Sakari > > On Tue, Dec 19, 2023 at 10:50:05AM +0200, Tomi Valkeinen wrote: > > On 18/12/2023 11:22, Laurent Pinchart wrote: > > > On Mon, Dec 18, 2023 at 09:54:01AM +0200, Tomi Valkeinen wrote: > > >> The driver requests the interrupts as IRQF_SHARED, so the interrupt > > >> handlers can be called at any time. If such a call happens while the ISP > > >> is powered down, the SoC will hang as the driver tries to access the > > >> ISP registers. > > >> > > >> This can be reproduced even without the platform sharing the IRQ line: > > >> Enable CONFIG_DEBUG_SHIRQ and unload the driver, and the board will > > >> hang. > > >> > > >> Fix this by adding a new field, 'irqs_enabled', which is used to bail > > >> out from the interrupt handler when the ISP is not operational. > > >> > > >> Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com> > > >> --- > > >> .../platform/rockchip/rkisp1/rkisp1-capture.c | 3 +++ > > >> .../media/platform/rockchip/rkisp1/rkisp1-common.h | 2 ++ > > >> .../media/platform/rockchip/rkisp1/rkisp1-csi.c | 3 +++ > > >> .../media/platform/rockchip/rkisp1/rkisp1-dev.c | 22 ++++++++++++++++++++++ > > >> .../media/platform/rockchip/rkisp1/rkisp1-isp.c | 3 +++ > > >> 5 files changed, 33 insertions(+) > > >> > > >> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c > > >> index aebd3c12020b..c381c22135a2 100644 > > >> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c > > >> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c > > >> @@ -725,6 +725,9 @@ irqreturn_t rkisp1_capture_isr(int irq, void *ctx) > > >> unsigned int i; > > >> u32 status; > > >> > > >> + if (!rkisp1->irqs_enabled) > > >> + return IRQ_NONE; > > > > > > Given that this is something all drivers that use shared IRQs have to > > > do, would it make sense to use a standard helper here, such as > > > pm_runtime_suspended() for instance ? I haven't looked at which one > > > would be the most appropriate (if any), there's also > > > pm_runtime_active() and pm_runtime_status_suspended(). That would > > > simplify this patch. > > > > I did consider that when writing the patch. But I just wasn't very > > comfortable using the runtime PM here, even if it would make sense, as > > I'm just not quite sure how it works. > > > > For example, pm_runtime_suspended() checks if the device is in > > RPM_SUSPENDED state, and the device will be in RPM_SUSPENDED after the > > driver's suspend callback has finished. This makes sense. > > > > However, _while_ suspending (not after we have suspended), we want to > > make sure that 1) no new irq handling will start, 2) we'll wait until > > any currently running irq handler has finished. For 1), we can't use > > pm_runtime_suspended() in the irq handler, as the status is not > > RPM_SUSPENDED. We could probably check for: > > > > spin_lock(&dev->power.lock); > > off = dev->power.runtime_status == RPM_SUSPENDED || > > dev->power.runtime_status == RPM_SUSPENDING; > > spin_unlock(&dev->power.lock); > > if (off) > > return IRQ_NONE; > > > > That would not work if we would depend on the irq handling while in the > > suspend callback (e.g. waiting for an irq which signals that the device > > has finished processing). But we don't do that at the moment, and that > > kind of this probably can usually be done before calling runtime_put(). > > > > When we take into account the resume part, I think we could just check > > for RPM_ACTIVE in the irq handler, which would then rule out > > RPM_RESUMING, RPM_SUSPENDED and RPM_SUSPENDING. > > > > But we can't use pm_runtime_active(), as that also checks for > > dev->power.disable_depth. In other words, when we disable the PM for our > > device (e.g. when unloading the driver), the PM framework says our > > device became active. > > > > Soo... I think this should work in the irq handler: > > > > spin_lock(&dev->power.lock); > > active = dev->power.runtime_status == RPM_ACTIVE; > > It would be nice to use pm_runtime_active() instead. This would however > require unregistering the IRQ handler before disabling runtime PM in the > remove path. I think that should be done nonetheless though, as relying > on devm to unregister the IRQ handler means it will happen after > .remove() returns, which could cause all sort of issues (I'm thinking > about the calls to dev_get_drvdata() in the IRQ handlers for instance). > What do you think ? You should use pm_runtime_get_if_active() as otherwise the device may be powered off right after checking it was active. If it returns 0, the device is not active. Don't put the usage_count by calling pm_runtime_put*() however as the usage_count isn't incremented on error (which you typically get when runtime PM is disabled). It's up to the driver to decide what to do then I think, presumably the device is powered on e.g. in case CONFIG_PM is disabled. This is actually similar to what e.g. sensor drivers do in the s_ctrl() callback. It would be the best if you could ensure no interrupt will be triggered by the device while it's about to suspend as you won't be able to ack it when the device is suspending (and no longer active). > > > spin_unlock(&dev->power.lock); > > if (!active) > > return IRQ_NONE; > > > > I think the driver depends on runtime PM, but if no-PM was an option, I > > guess we'd need to ifdef the above away, and trust that the device is > > always powered on. > > > > So, as I said in the beginning, "I just wasn't very comfortable using > > the runtime PM here". And that's still the case =). The runtime PM is > > horribly complex. If you think the above is clearer, and you think it's > > correct, I can make the change. > > It sounds it may require some more work, and we should land this fix in > v6.8, with the revert, right ? If so, I'm fine merging this patch, and > moving to runtime PM checks on top if we decide to do so. > > Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> > > By the way, I wonder if it would make sense to handle this in the driver > core. The prospect of copying this code in all drivers doesn't make me > happy. > > > >> + > > >> status = rkisp1_read(rkisp1, RKISP1_CIF_MI_MIS); > > >> if (!status) > > >> return IRQ_NONE; > > >> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h b/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h > > >> index 4b6b28c05b89..b757f75edecf 100644 > > >> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h > > >> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h > > >> @@ -450,6 +450,7 @@ struct rkisp1_debug { > > >> * @debug: debug params to be exposed on debugfs > > >> * @info: version-specific ISP information > > >> * @irqs: IRQ line numbers > > >> + * @irqs_enabled: the hardware is enabled and can cause interrupts > > >> */ > > >> struct rkisp1_device { > > >> void __iomem *base_addr; > > >> @@ -471,6 +472,7 @@ struct rkisp1_device { > > >> struct rkisp1_debug debug; > > >> const struct rkisp1_info *info; > > >> int irqs[RKISP1_NUM_IRQS]; > > >> + bool irqs_enabled; > > >> }; > > >> > > >> /* > > >> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c > > >> index b6e47e2f1b94..4202642e0523 100644 > > >> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c > > >> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c > > >> @@ -196,6 +196,9 @@ irqreturn_t rkisp1_csi_isr(int irq, void *ctx) > > >> struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); > > >> u32 val, status; > > >> > > >> + if (!rkisp1->irqs_enabled) > > >> + return IRQ_NONE; > > >> + > > >> status = rkisp1_read(rkisp1, RKISP1_CIF_MIPI_MIS); > > >> if (!status) > > >> return IRQ_NONE; > > >> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c > > >> index acc559652d6e..73cf08a74011 100644 > > >> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c > > >> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c > > >> @@ -305,6 +305,24 @@ static int __maybe_unused rkisp1_runtime_suspend(struct device *dev) > > >> { > > >> struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); > > >> > > >> + rkisp1->irqs_enabled = false; > > >> + /* Make sure the IRQ handler will see the above */ > > >> + mb(); > > >> + > > >> + /* > > >> + * Wait until any running IRQ handler has returned. The IRQ handler > > >> + * may get called even after this (as it's a shared interrupt line) > > >> + * but the 'irqs_enabled' flag will make the handler return immediately. > > >> + */ > > >> + for (unsigned int il = 0; il < ARRAY_SIZE(rkisp1->irqs); ++il) { > > >> + if (rkisp1->irqs[il] == -1) > > >> + continue; > > >> + > > >> + /* Skip if the irq line is the same as previous */ > > >> + if (il == 0 || rkisp1->irqs[il - 1] != rkisp1->irqs[il]) > > >> + synchronize_irq(rkisp1->irqs[il]); > > >> + } > > >> + > > >> clk_bulk_disable_unprepare(rkisp1->clk_size, rkisp1->clks); > > >> return pinctrl_pm_select_sleep_state(dev); > > >> } > > >> @@ -321,6 +339,10 @@ static int __maybe_unused rkisp1_runtime_resume(struct device *dev) > > >> if (ret) > > >> return ret; > > >> > > >> + rkisp1->irqs_enabled = true; > > >> + /* Make sure the IRQ handler will see the above */ > > >> + mb(); > > >> + > > >> return 0; > > >> } > > >> > > >> diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c > > >> index f00873d31c42..78a1f7a1499b 100644 > > >> --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c > > >> +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c > > >> @@ -976,6 +976,9 @@ irqreturn_t rkisp1_isp_isr(int irq, void *ctx) > > >> struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); > > >> u32 status, isp_err; > > >> > > >> + if (!rkisp1->irqs_enabled) > > >> + return IRQ_NONE; > > >> + > > >> status = rkisp1_read(rkisp1, RKISP1_CIF_ISP_MIS); > > >> if (!status) > > >> return IRQ_NONE; >
diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c index aebd3c12020b..c381c22135a2 100644 --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c @@ -725,6 +725,9 @@ irqreturn_t rkisp1_capture_isr(int irq, void *ctx) unsigned int i; u32 status; + if (!rkisp1->irqs_enabled) + return IRQ_NONE; + status = rkisp1_read(rkisp1, RKISP1_CIF_MI_MIS); if (!status) return IRQ_NONE; diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h b/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h index 4b6b28c05b89..b757f75edecf 100644 --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-common.h @@ -450,6 +450,7 @@ struct rkisp1_debug { * @debug: debug params to be exposed on debugfs * @info: version-specific ISP information * @irqs: IRQ line numbers + * @irqs_enabled: the hardware is enabled and can cause interrupts */ struct rkisp1_device { void __iomem *base_addr; @@ -471,6 +472,7 @@ struct rkisp1_device { struct rkisp1_debug debug; const struct rkisp1_info *info; int irqs[RKISP1_NUM_IRQS]; + bool irqs_enabled; }; /* diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c index b6e47e2f1b94..4202642e0523 100644 --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-csi.c @@ -196,6 +196,9 @@ irqreturn_t rkisp1_csi_isr(int irq, void *ctx) struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); u32 val, status; + if (!rkisp1->irqs_enabled) + return IRQ_NONE; + status = rkisp1_read(rkisp1, RKISP1_CIF_MIPI_MIS); if (!status) return IRQ_NONE; diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c index acc559652d6e..73cf08a74011 100644 --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c @@ -305,6 +305,24 @@ static int __maybe_unused rkisp1_runtime_suspend(struct device *dev) { struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); + rkisp1->irqs_enabled = false; + /* Make sure the IRQ handler will see the above */ + mb(); + + /* + * Wait until any running IRQ handler has returned. The IRQ handler + * may get called even after this (as it's a shared interrupt line) + * but the 'irqs_enabled' flag will make the handler return immediately. + */ + for (unsigned int il = 0; il < ARRAY_SIZE(rkisp1->irqs); ++il) { + if (rkisp1->irqs[il] == -1) + continue; + + /* Skip if the irq line is the same as previous */ + if (il == 0 || rkisp1->irqs[il - 1] != rkisp1->irqs[il]) + synchronize_irq(rkisp1->irqs[il]); + } + clk_bulk_disable_unprepare(rkisp1->clk_size, rkisp1->clks); return pinctrl_pm_select_sleep_state(dev); } @@ -321,6 +339,10 @@ static int __maybe_unused rkisp1_runtime_resume(struct device *dev) if (ret) return ret; + rkisp1->irqs_enabled = true; + /* Make sure the IRQ handler will see the above */ + mb(); + return 0; } diff --git a/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c b/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c index f00873d31c42..78a1f7a1499b 100644 --- a/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c +++ b/drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c @@ -976,6 +976,9 @@ irqreturn_t rkisp1_isp_isr(int irq, void *ctx) struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); u32 status, isp_err; + if (!rkisp1->irqs_enabled) + return IRQ_NONE; + status = rkisp1_read(rkisp1, RKISP1_CIF_ISP_MIS); if (!status) return IRQ_NONE;