Linux IIO development
 help / color / mirror / Atom feed
From: "Nuno Sá" <noname.nuno@gmail.com>
To: "Uwe Kleine-König" <u.kleine-koenig@baylibre.com>
Cc: Jonathan Cameron <jic23@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	David Lechner <dlechner@baylibre.com>,
	Dumitru Ceclan <dumitru.ceclan@analog.com>,
	Krzysztof Kozlowski	 <krzk+dt@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Michael Hennerich	 <Michael.Hennerich@analog.com>,
	Nuno Sa <nuno.sa@analog.com>, Rob Herring	 <robh@kernel.org>,
	devicetree@vger.kernel.org, linux-iio@vger.kernel.org,
	 Thomas Gleixner <tglx@linutronix.de>,
	Russell King <linux@armlinux.org.uk>
Subject: Re: [PATCH v2 3/4] iio: adc: ad_sigma_delta: Add support for reading irq status using a GPIO
Date: Tue, 05 Nov 2024 11:30:58 +0100	[thread overview]
Message-ID: <1e6aba2a95441abfbd6763f8892c4efa4f05ad12.camel@gmail.com> (raw)
In-Reply-To: <6qecwcncfjxphezk6zwwafo5khpiqn4g7tsklrd6wv2xlzaok2@pm3rbfhaolzw>

On Tue, 2024-11-05 at 10:20 +0100, Uwe Kleine-König wrote:
> Hello Nuno,
> 
> On Mon, Nov 04, 2024 at 02:15:49PM +0100, Nuno Sá wrote:
> > On Mon, 2024-11-04 at 13:49 +0100, Uwe Kleine-König wrote:
> > > On Thu, Oct 31, 2024 at 01:05:21PM +0100, Nuno Sá wrote:
> > > > On Thu, 2024-10-31 at 11:40 +0100, Uwe Kleine-König wrote:
> > > > > On Wed, Oct 30, 2024 at 08:44:29PM +0000, Jonathan Cameron wrote:
> > > > > > On Wed, 30 Oct 2024 14:04:58 +0100
> > > > > > Nuno Sá <noname.nuno@gmail.com> wrote:
> > > > > > > Regarding this, I do share some of the concerns already raised by
> > > > > > > Jonathan.
> > > > > > > I fear
> > > > > > > that we're papering around an issue with the IRQ controller rather
> > > > > > > than
> > > > > > > being an
> > > > > > > issue with the device. When I look at irq_disable() docs [1], it
> > > > > > > feels that
> > > > > > > we're
> > > > > > > already doing what we're supposed to do. IOW, we disable the lazy
> > > > > > > approach
> > > > > > > so we
> > > > > > > *should* not get any pending IRQ.
> > > > > 
> > > > > I think this is wrong and you always have to be prepared to see an irq
> > > > > triggering that became pending while masked.
> > > 
> > > I did some research, here are my findings:
> > > 
> > > https://www.kernel.org/doc/html/v6.12-rc6/core-api/genericirq.html#delayed-interrupt-disable
> > > reads:
> > > 
> > > 	The interrupt is kept enabled and is masked in the flow handler
> > > 	when an interrupt event happens. This prevents losing edge
> > > 	interrupts on hardware which does not store an edge interrupt
> > > 	event while the interrupt is disabled at the hardware level.
> > > 
> > > This suggests that lazy disabling is needed for some controllers that
> > > stop their event detection when disabled. I read that as: *Normally* an
> > > irq event gets pending in hardware while the irq is disabled.
> > 
> > I might be wrong, but I think that the lazy approach is the one for
> > optimization.
> 
> It's both. Needed for some controllers *and* an optimisation (that
> isn't beneficial in some corner cases).
> 
> > I think the reasoning is that __normally__ no more IRQs will come so
> > no need to access the HW. But also thinking more on the subject and
> > looking at what the lazy approach is doing, I take back what I said in
> > previous emails. I *think* the expectation for a received IRQ while
> > the line is masked (or disabled?!), is to keep it as pending (both on
> > HW - when possible - and in SW).
> > 
> > > The lazy disable approach is expected to work fine always, the reason to
> > > implement non-lazy disabling is "only" a performance optimisation. See
> > > commit e9849777d0e27cdd2902805be51da73e7c79578c.
> > 
> > Not sure If I understood you correctly, but I think is the other way
> > around? 
> > Also, as said in the commit, I think it also prevents the same interrupt
> > from
> > happening twice (in some cases).
> 
> The conversation thread isn't complete on lore.kernel.org, so I don't
> know for sure, but the way I understand it is: Normally while you handle
> an irq no new irq comes in and so it's sensible to do lazy disabling.
> Approximately: In 99.9 % of the cases you save 1 µs by not masking and
> in the remaining 0.1% you get another hard irq that costs you say
> 500 µs. So on average you save: 0.999 * 1 µs + 0.001 * (1 - 500) µs = 0.5 µs.
> 
> However if for a certain device it's normal that another irq comes in
> the "improvement" degrades to: In 20 % of the cases you save 1 µs and in
> the remaining 80 % you get a penalty of 500 µs. So in this case it's not
> an expected win anymore and you can better stop doing lazy disabling and
> invest the time to mask the irq improving the average cost from
> - 0.2 * 1 µs + 0.8 * 499 µs = 399.4 µs to 1 µs.
> 
> The interrupt happening twice is not a problem for correctness as the
> second one is not given to the device driver but caught in the irq
> subsystem that only then disables the irq in hardware and marking it
> pending for later consumption. It "only" costs cpu time. (And maybe it's
> given to the driver twice after enable_irq is called?)

Yeah, enable_irq() was what I meant. So, in the commit message, it's stated:

"Unfortunately there are devices which do not allow the interrupt to be
disabled easily at the device level. They are forced to use
disable_irq_nosync(). This can result in taking each interrupt twice."

And the taking twice part was something that I was not getting it. Still not
100% sure but I think that what is meant is that when we enable the IRQ we might
get it through [1] and then afterwards through the IRQ controller for devices
that latch the events (as soon as you unmask the line, the event should
trigger). On [1], there's a retrigger path that goes through HW and I'm not sure
if that one is problematic. But I would expect the SW one to be...

[1]: https://elixir.bootlin.com/linux/v6.11.6/source/kernel/irq/chip.c#L283

- Nuno Sá
> 
> Looking at the problem at hand with the shared DOUT/̅R̅D̅Y line: It's
> (nearly?) 100% of the cases that the line toggles while the irq is
> logically disabled/masked. So it makes sense to do
> 
> 	irq_set_status_flags(sigma_delta->irq_line, IRQ_DISABLE_UNLAZY);
> 
> which however should only have an effect iff the irq controller doesn't
> miss an edge while the irq is disabled.
> 
> So assuming my understanding is correct, there is something to do about
> the raspberry pi gpio controller to prevent setting IRQ_DISABLE_UNLAZY
> have an effect, because that one looses events.
> 
> > > However that makes me wonder what is the difference between the
> > > irq_mask() and irq_disable() callbacks defined in struct irq_chip.
> > 
> > Wondering the same...
> > 
> > Thanks for digging into this. This has been a long standing thing with sigma
> > delta
> > ADCs (I'm fairly sure this discussion about being an issue on the IRQ
> > controller or
> > not already happened before).
> 
> I keep that paragraph in my reply because the question is still open
> even though I don't add new infos here.
> 
> Best regards
> Uwe


  reply	other threads:[~2024-11-05 10:26 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-28 16:07 [PATCH v2 0/4] iio: adc: ad7124: Make it work on de10-nano Uwe Kleine-König
2024-10-28 16:07 ` [PATCH v2 1/4] dt-bindings: iio: adc: adi,ad7124: Use symbolic name for interrupt flag Uwe Kleine-König
2024-10-29  7:36   ` Krzysztof Kozlowski
2024-10-28 16:07 ` [PATCH v2 2/4] dt-bindings: iio: adc: adi,ad7124: Allow specifications of a gpio for irq line Uwe Kleine-König
2024-11-01 19:20   ` Rob Herring
2024-10-28 16:07 ` [PATCH v2 3/4] iio: adc: ad_sigma_delta: Add support for reading irq status using a GPIO Uwe Kleine-König
2024-10-30 13:04   ` Nuno Sá
2024-10-30 20:44     ` Jonathan Cameron
2024-10-31 10:40       ` Uwe Kleine-König
2024-10-31 12:05         ` Nuno Sá
2024-10-31 12:28           ` Nuno Sá
2024-11-04 12:49           ` Uwe Kleine-König
2024-11-04 13:15             ` Nuno Sá
2024-11-05  9:20               ` Uwe Kleine-König
2024-11-05 10:30                 ` Nuno Sá [this message]
2024-10-28 16:07 ` [PATCH v2 4/4] iio: adc: ad7124: Disable all channels at probe time Uwe Kleine-König
2024-10-30  7:17   ` Nuno Sá
2024-10-28 16:38 ` [PATCH v2 0/4] iio: adc: ad7124: Make it work on de10-nano David Lechner
2024-11-18 18:12 ` Uwe Kleine-König
2024-11-23 14:24   ` Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1e6aba2a95441abfbd6763f8892c4efa4f05ad12.camel@gmail.com \
    --to=noname.nuno@gmail.com \
    --cc=Michael.Hennerich@analog.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=dumitru.ceclan@analog.com \
    --cc=jic23@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=nuno.sa@analog.com \
    --cc=robh@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=u.kleine-koenig@baylibre.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox