Skip to contentSkip to conversation

Bluetooth: btintel_pcie: fix PM flow for S0ix, S3 and S4

PATCH Sergey Lebedev 2 participants 4 messages Sep 2 – Sep 8 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

Ravindra,

Paul Menzel pointed me at this patch from the thread on Vladimir Kondratyev's stale-cache fix, since both change btintel_pcie_set_dxstate(). They overlap textually and, more usefully, they fix different halves of the same failure. I have the hardware and a fixture for one of those halves, so here is what I can add.

Your change moves the flag out of the retry loop:

+ data->gp0_received = false;
+

       do {

- data->gp0_received = false;

That recovers the case where the alive interrupt is merely *late* — it arrives while a later retry is waiting, and previously the per-iteration reset threw it away. The comment you added states the invariant well.

What it does not cover is the interrupt being missed outright. When that happens gp0_received never becomes true, the loop falls through to

      if (dxstate == BTINTEL_PCIE_STATE_D0) {
          if (btintel_pcie_in_d0(data))

and those helpers read data->boot_stage_cache, which only the interrupt handler ever writes. No interrupt, no refresh: the fallback is asked to decide using the value from before the transition, so it reports the old state, every retry is exhausted, and -EBUSY aborts the suspend. Vladimir's patch re-reads BTINTEL_PCIE_CSR_BOOT_STAGE_REG immediately before that check, which is what btintel_pcie_resume() already does.

So the two are complementary rather than competing: yours makes a late interrupt count, his makes a missing one survivable.

I can be concrete about the second half because I reproduced it deliberately. Surface Pro 11 (Intel, Lunar Lake), Intel BE201, 8086:a876 rev 10. The driver was built out of tree with one debug-only parameter that returns from btintel_pcie_msix_gp0_handler() before the boot_stage_cache refresh, and only while alive_intr_ctxt is BTINTEL_PCIE_D0 — the exact state a genuinely missed alive interrupt leaves behind, with the controller still reaching D3.

Under that fixture, moving the flag out of the loop does not help: there is no late interrupt to catch, and the cache stays stale. Three timeouts, -EBUSY, suspend aborted. With the re-read, one timeout and the system suspends. Method and logs are in
Message-ID <[email protected]>.

The failure is not theoretical here. Before any workaround it aborted 2 of the 3 suspends I attempted, and one device returning -EBUSY stops the whole system from sleeping.

Whichever of the two lands first, the other will need a rebase — the hunks touch adjacent lines. If it helps, I am happy to test them applied together against my fixture and on ordinary suspend cycles, and to report per-patch rather than as a combined result.

If you and Vladimir would prefer these as one series rather than two independent patches, I am happy to do the assembly work — rebased, with both of you as the authors of your respective halves and the Fixes: tags kept separate, since they are separate bugs with separate reverts. I did not want to send that uninvited over two patches that are already on the list; say the word and it is a short job.

One question on your patch, from someone who does not have S4 on this machine to check: .thaw now takes the normal D0 path, while .restore forces FLR. After PM_EVENT_FREEZE the controller was put into D3_COLD by btintel_pcie_suspend_late(). Is D3_COLD guaranteed to preserve firmware across a freeze/thaw on this part, or does thaw also need the FLR path?

Thanks,
Sergey

#1 lore raw

I said in my earlier mail that these two patches fix different halves of one failure, and offered to test them. Here is the measurement rather than the argument.

Hardware: Surface Pro 11 (Intel, Lunar Lake), Intel BE201, 8086:a876 rev 10, kernel 7.0.0-30, s2idle. Four builds of btintel_pcie from the same source, differing only in which hunk is present, each carrying one debug-only module parameter that returns from btintel_pcie_msix_gp0_handler() before the boot_stage_cache refresh and only while alive_intr_ctxt is BTINTEL_PCIE_D0. That is a missed alive
interrupt: gp0_received never set, cache never refreshed, controller still reaching D3. Suspend triggered with rtcwake -m freeze -s 45.

build result
---------------------------------------------------------------

  neither hunk                   3 timeouts, -EBUSY, suspend aborted
  Ravindra's hunk only           3 timeouts, -EBUSY, suspend aborted
  Vladimir's hunk only           1 timeout, suspended and resumed
  both hunks                     1 timeout, suspended and resumed

Unpatched and with your hunk alone, identical:

PM: suspend entry (s2idle)
Bluetooth: hci0: Timeout (200 ms) on alive interrupt for D2 entry, retry count 0
Bluetooth: hci0: Timeout (200 ms) on alive interrupt for D2 entry, retry count 1
Bluetooth: hci0: Timeout (200 ms) on alive interrupt for D2 entry, retry count 2 btintel_pcie 0000:00:14.7: PM: failed to suspend async: error -16
PM: Some devices failed to suspend, or early wake event detected
PM: suspend exit <- 1.7 s after entry; it never slept

With the re-read present, alone or alongside your hunk:

PM: suspend entry (s2idle)
Bluetooth: hci0: Timeout (200 ms) on alive interrupt for D2 entry, retry count 0
PM: suspend exit <- 48.8 s after entry; it slept the full period

This is not a criticism of your patch: the fixture removes the interrupt entirely, so there is no late interrupt for your hunk to rescue, and it cannot help by construction. It measures the half it does not cover, and confirms the two do not interfere when applied together.

The single-run table above had one oddity - the both-hunks run slept 8.6 s instead of the full 45 - so I repeated it rather than leave a guess in the archive. Eight further cycles, no failures anywhere:

  both hunks        48.0  48.6  50.8  48.6  50.7 s
  re-read only      48.6  46.6  50.8 s

The 8.6 s did not recur. It was a stray wake, not a property of the combination.

Those runs turned up something better than the caveat they removed. The retry count varies between 1 and 3 from run to run, and the outcome does
not: even when all three retries time out, the re-read then observes D3 and set_dxstate() returns 0. One of the re-read-only runs did exactly that - three timeouts, no -EBUSY, slept 46.6 s - which is the fallback doing the whole job with the retry loop having contributed nothing.

One incidental confirmation, since Paul asked in the other thread whether a register called *boot stage* really changes after boot. Tracing the fallback path prints what it actually reads at D3 entry:

SP11TRACE: retry=0 dxstate=2 gp0_received=0 boot_stage=0x61710007

Bit 24 - BTINTEL_PCIE_CSR_BOOT_STAGE_D3_STATE_READY - is set, on hardware, long after boot, which is why the re-read is able to answer the question the cached value cannot.

One caveat I cannot remove. I have no fixture for the late-interrupt case your hunk addresses, so I have measured only that it does no harm here, not that it works. That needs a different fixture - delaying the interrupt rather than dropping it - and I have not built one.

Raw log and the four builds are available if anyone wants them.

While instrumenting this I caught the failure happening on its own, with the injection disabled, which I had not managed before. One run in six:

PM: suspend entry (s2idle)
Timeout (200 ms) on alive interrupt for D2 entry, retry count 0
SP11TRACE: retry=0 dxstate=2 gp0_received=0 boot_stage=0x61511007 Timeout (200 ms) on alive interrupt for D0 entry, retry count 0
SP11TRACE: retry=0 dxstate=0 gp0_received=0 boot_stage=0xa0db1007 Timeout (3000 ms) on alive interrupt, alive context: intel_reset1

The interrupt went missing in both directions in the same cycle, and the re-read rescued both: bit 24 set at D3 entry, clear at D0 entry, each time matching the state the controller had actually reached. No -EBUSY. So this is not only a fixture artefact - the hardware does drop it, and the register is right when the interrupt is not.

That leads to a question rather than a patch, for whoever owns this code.

Both patches keep the interrupt as the primary signal and the register as a fallback. Given that the driver's own comment calls the missed interrupt a hardware bug, and given the register answers correctly in both directions, would it not be sounder to invert that - poll BOOT_STAGE_REG for the target bit, and treat the interrupt as an early-exit optimisation rather than the thing being waited on?

The file already does exactly this shape for the reset path:

  do {
      reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
      if (reg & BTINTEL_PCIE_CSR_FUNC_CTRL_BUS_MASTER_STS)
          break;
      usleep_range(10000, 12000);
  } while (--retry > 0);

and btintel_pcie.c declares POLL_INTERVAL_US, which nothing uses - so the intent seems to have existed at some point.

The practical difference is not only correctness. Today a missed interrupt costs up to 600 ms of waiting per transition before the fallback is even consulted; a poll would return as soon as the bit flips. And the stale-cache class of bug disappears rather than being caught.

I am not proposing this as a patch over yours - you two own this code and I have one machine. But if either of you thinks the shape is right, I have the hardware, the fixture and now a spontaneous reproduction to test it against.

Vladimir has said he is content either way on the series question, so it rests with you and the maintainers. My offer to do the assembly stands.

Thanks,
Sergey

#2 lore raw
In reply to Sergey Lebedev

Hi Sergey,

Thank you for the detailed analysis and for taking the time to validate the behavior on the hardware.

Quoted 34 lines

Subject: Re: [PATCH v2] Bluetooth: btintel_pcie: fix PM flow for S0ix, S3 and S4

Ravindra,

Paul Menzel pointed me at this patch from the thread on Vladimir Kondratyev's stale-cache fix, since both change btintel_pcie_set_dxstate(). They overlap textually and, more usefully, they fix different halves of the same failure. I have the hardware and a fixture for one of those halves, so here is what I can add.

Your change moves the flag out of the retry loop:

+ data->gp0_received = false;
+

       do {

- data->gp0_received = false;

That recovers the case where the alive interrupt is merely *late* — it arrives while a later retry is waiting, and previously the per-iteration reset threw it away. The comment you added states the invariant well.

What it does not cover is the interrupt being missed outright. When that happens gp0_received never becomes true, the loop falls through to

      if (dxstate == BTINTEL_PCIE_STATE_D0) {
          if (btintel_pcie_in_d0(data))

and those helpers read data->boot_stage_cache, which only the interrupt handler ever writes. No interrupt, no refresh: the fallback is asked to decide using the value from before the transition, so it reports the old state, every retry is exhausted, and -EBUSY aborts the suspend. Vladimir's patch re-reads BTINTEL_PCIE_CSR_BOOT_STAGE_REG immediately before that check, which is what btintel_pcie_resume() already does.

Quoted 3 lines

So the two are complementary rather than competing: yours makes a late interrupt count, his makes a missing one survivable.

I am in alignment with the proposed flow and agree with your assessment that the two changes address different aspects of the same issue. My change handles the case where the alive interrupt is delayed and arrives during a subsequent retry, while Vladimir's fix covers the scenario where the interrupt is missed entirely by refreshing the boot stage state directly from the CSR register before the fallback check.

Quoted 23 lines

I can be concrete about the second half because I reproduced it deliberately. Surface Pro 11 (Intel, Lunar Lake), Intel BE201, 8086:a876 rev 10. The driver was built out of tree with one debug-only parameter that returns from btintel_pcie_msix_gp0_handler() before the boot_stage_cache refresh, and only while alive_intr_ctxt is
BTINTEL_PCIE_D0 — the exact state a genuinely missed alive interrupt leaves behind, with the controller still reaching D3.

Under that fixture, moving the flag out of the loop does not help: there is no late interrupt to catch, and the cache stays stale. Three timeouts, -EBUSY, suspend aborted. With the re-read, one timeout and the system suspends. Method and logs are in Message-ID <20260830151550.44687-1- [email protected]>.

The failure is not theoretical here. Before any workaround it aborted 2 of the 3 suspends I attempted, and one device returning -EBUSY stops the whole system from sleeping.

Whichever of the two lands first, the other will need a rebase — the hunks touch adjacent lines. If it helps, I am happy to test them applied together against my fixture and on ordinary suspend cycles, and to report per-patch rather than as a combined result.

Quoted 7 lines

If you and Vladimir would prefer these as one series rather than two independent patches, I am happy to do the assembly work — rebased, with both of you as the authors of your respective halves and the Fixes: tags kept separate, since they are separate bugs with separate reverts. I did not want to send that uninvited over two patches that are already on the list; say the word and it is a short job.

I am fine with that approach. Since the two patches address separate issues while complementing each other, I have no objection to combining them into a single series with the respective authorship and Fixes: tags preserved for each patch.

I am also fine with rebasing my patch on top of Vladimir's patch if that results in a cleaner integration path.

Thank you for offering to do the assembly and rebase work.

Quoted 6 lines

One question on your patch, from someone who does not have S4 on this machine to check: .thaw now takes the normal D0 path, while .restore forces FLR. After PM_EVENT_FREEZE the controller was put into D3_COLD by btintel_pcie_suspend_late(). Is D3_COLD guaranteed to preserve firmware across a freeze/thaw on this part, or does thaw also need the FLR path?

Regarding the freeze/thaw flow, the firmware state is preserved across a freeze/thaw cycle even when the device is placed into D3_COLD by btintel_pcie_suspend_late(). Therefore, when hibernation is aborted and the kernel executes .thaw(), the device can follow the normal D0 transition path and does not require the FLR-based reinitialization.

Quoted 2 lines

Thanks,
Sergey

Regards,
Ravindra

author Reviewed-by Paul Menzel
#3 lore raw

Ravindra, Vladimir,

Ravindra — thank you, and thank you for the freeze/thaw answer. That was the one thing I could not test here, this machine having no S4, and it settles it: if D3_COLD preserves firmware then .thaw taking the normal D0 path is right, and the asymmetry with .restore is deliberate rather than an oversight. Nothing to change on that account.

Ravindra has agreed to the two patches going as one series, with each of you the author of your own half, the Fixes: tags kept separate, and his rebased on top of Vladimir's. So unless Vladimir would rather not, this is what I will send in a few days:

1/2 Vladimir V. Kondratyev — fix stale cache in set_dxstate fallback
check. v4 unchanged, with Paul's Reviewed-by restored (see below) and my Tested-by.

2/2 Ravindra — fix PM flow for S0ix, S3 and S4. v2 rebased onto 1/2,
unchanged in substance.

with a cover letter carrying the per-hunk measurements from this hardware:
2/2's hunk alone leaves the missed-interrupt case failing exactly as unpatched, 1/2's re-read fixes it, and together they do not interfere. My Signed-off-by goes on neither patch, only on the posting; the authorship, the Fixes: tags and the review tags are yours and stay yours.

Vladimir, if you would rather v4 stayed a standalone patch, say so and I will leave it exactly where it is — it is your patch and your call, and a word is enough.

Two things that argue for doing it, both checked rather than assumed.

First, v4 lost a tag. Paul reviewed v3 at 14:36 UTC on 3 September:

https://lore.kernel.org/all/[email protected]/

v4 went out at 19:22 the same day and does not carry it. Whoever applies v4 as it stands will apply it without Paul's review. That wants fixing whether or not the patches are combined.

Second, neither patch is moving. Both are in patchwork at state "new" and undelegated — v4 since 3 September, Ravindra's v2 since the 2nd. Two independent changes to the same function, arriving separately, are exactly what a maintainer defers until it is clear how they interact. One series with an ordering and the measurements attached answers that before it is asked, and neither of you loses anything by it.

Sergey