Skip to content

Protect the V/I machines: asahi governor, J316/J413/J616 configs, shadow and replay - #6

Closed
RyanTheTide wants to merge 6 commits into
mainfrom
unified/feedforward-20261024
Closed

RyanTheTide wants to merge 6 commits into
mainfrom
unified/feedforward-20261024

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

speakerguardd now protects the Apple laptops with speaker current and voltage sense: the MacBook Pro 16" M1 Pro (J316), the MacBook Air M2 (J413) and the MacBook Pro 16" M4 Pro (J616). Until now it only covered the MacBook Neo. The Neo's behaviour is unchanged (the 0.1.2 legacy governor).

What's in it

  • V/I acquisition: S16 voltage/current sense words (TAS2764-class SN012776, 14 V and 3.75 A full scale). The sense switches are turned on, the amplifier gain is set to its maximum, and there is one volume control per speaker, linked by group. The SN012776 machines' 12-channel sense PCM takes at most 64 KiB per hardware period, so the hardware period is split until the device accepts it.
  • The asahi governor, which writes the card on these machines. It is the behaviour Asahi Linux's speakersafetyd ships, reimplemented from its description: measured V*I per sample into the two-node model with speakersafetyd's constants, the ramp to its min_gain, and peak-hold hysteresis. Credit: the Asahi Linux contributors (speakersafetyd, MIT through 2.0.2). No code was copied. The differences only go toward protection:
    • unmeasured time is charged at the worst case;
    • a negative sensed power isn't counted;
    • a fault hands the speakers to the kernel instead of panicking;
    • a sample rate of 0 at start is idle, not a crash (speakersafetyd 2.0.1 panics on it at boot).
  • Configs for j316, j413 and j616. The feed-forward constants come from each machine's own speaker protection tuning (facts about the hardware). The measured-power constants are speakersafetyd's j316/j413 values; the J616 shares the J316's speaker family.
  • Shadow, record and replay: --shadow logs every governor and estimator once a second. --record keeps raw sense periods, and --replay runs every governor on them offline, optionally louder.
  • The unified governor stays shadow-only. It is a look-ahead regulator on the feed-forward model, after macOS's design, and it writes the card only with --experimental-unified. In replay it was not louder than the current protection on the M1 Pro or the Neo (0.2 to 0.6 dB more mean reduction), so it doesn't govern anywhere.

Evidence

  • Equivalence with speakersafetyd: a 338 s recording from a J316, replayed at 0, +6, +10, +14 and +18 dB, compared against speakersafetyd's own arithmetic. Our gains never ask for less than speakersafetyd's before they have first asked for more. speakersafetyd's own coil model driven by our gains peaks lower for every speaker at every level (at +18 dB: left tweeter 139.59 C vs 140.34 C under its own gains).
  • Real playback, low level, with a mic check (float32 capture, level steps of 6 dB):
    • M1 Pro at -36/-30/-24 dBFS: +6.0/+5.9 dB at the mic, no dropouts.
    • M2 at -40/-34/-28 dBFS: +5.5/+5.7 dB.
    • In both cases the daemon held the interlock throughout.
  • cargo test --locked passes 64 of 64.

Known cost: a sense outage longer than 100 ms is charged at the worst case, a full-scale square at 18.5 dBV into the tweeter model. That mutes the speakers (the kernel holds SN012776 at -100 dB) until the model cools, about 20 s. It is safe, and it's kept.

Using it next to speakersafetyd: the unit conflicts with speakersafetyd. To switch, mask speakersafetyd (systemctl mask --now speakersafetyd). To go back, unmask it and reboot.

Assisted-by: LLM

🤖 Generated with Claude Code

speakerguardd now runs every machine's protection from one engine.

Estimators, per speaker and per sense period:
- feed-forward: the amplifier voltage squared over the speaker's DC
  resistance (r_ff, defaulting to z_nominal), as the machines' own
  protection does; on every machine.
- measured power: mean V*I, where the amplifier has current sense
  (S16 V/I sense words, sense_format = s16); Asahi Linux's
  speakersafetyd method.
- low-band resistance, sum(v*i)/sum(i^2) under 120 Hz, only measured and
  logged for now.

Governors:
- legacy: unchanged (0.1.2 budgets).  What 0.1.1's after-gain budget
  would have asked for is logged as legacy_011_budget, for comparison
  only.
- asahi: speakersafetyd's published behaviour (measured power, a linear
  ramp to its min_gain across t_window, peak-hold hysteresis, its cold
  and warm start rules), reimplemented from its description.  Unmeasured
  time is charged at the worst case and faults hand the speakers to the
  kernel's safe level instead of panicking (sample rate 0 at start is
  plain idle).
- unified: a model-predictive regulator.  It lets through the most power
  that keeps each model's coil at or under the working limit (less
  t_ctrl_headroom) over horizon_s, attacks at once and releases with
  release_s.  Where V/I sense exists, the measured-power model runs too
  and can only tighten.  The 1 s and 60 s budgets act on the power
  before the card's gain.

Each governor is fed the power its own gains would have let through, so
the ones that do not write the card yield a counterfactual.  --shadow
logs them all once a second to shadow.jsonl next to the state file.
--record keeps raw sense periods, and --replay runs every governor on a
recording offline, optionally louder (--gain-db), and reports what
each would have done.

Cards with a volume control per speaker get per-speaker reductions,
linked by group.  The V/I machines' sense switches are turned on and
their amplifier gain set to its maximum.

New configs for the J316 (M1 Pro) and the J413 (M2), with the
feed-forward constants from the machines' own tuning and the
measured-power constants from speakersafetyd's configs (MIT through
2.0.2; credit to the Asahi Linux contributors).  The J700 config gains
the unified governor's settings.  Its default governor stays legacy.

Assisted-by: LLM
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
- The SN012776 machines' 12-channel S16 sense PCM takes at most 64 KiB
  per hardware period, less than one 4096-frame sense period, so the
  open failed with EINVAL.  Split the hardware period (2, 4, ... per
  sense period) until the device takes it; the buffer still holds four
  sense periods and the reader still waits for a whole one.
- Unified governor: over the target, bring the coil back to it by the
  end of the next period (and let nothing through when even silence
  would not), instead of an unbounded reduction; nothing-may-pass is a
  finite 120 dB.
- --horizon overrides the unified governor's look-ahead (replay sweeps).

Assisted-by: LLM
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
On a 338 s recording from the M1 Pro, replayed at 0 to +18 dB against
speakersafetyd's own arithmetic, the asahi governor's gains differed
by up to 0.25 dB, because the models were stepped once a period on the
period's mean power and long gaps were cooled in one lagged step.
- The asahi models now integrate the measured V*I sample by sample,
  as speakersafetyd does, with 0.5% more power than measured
  (ASAHI_MARGIN, +0.02 dB) so that rounding never leaves them cooler.
- Cooling without power is exact for any length (the cascade's closed
  form), for every model.
- The replay counts gaps as speakersafetyd does: over four periods the
  models cool, shorter gaps hold the temperature.

On that recording our gains now never ask for less than
speakersafetyd's until after they have first asked for more.  Driving
speakersafetyd's own model with our gains gives lower peak coil
temperatures for every speaker at every level.

Assisted-by: LLM
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
The machine's own protection tunes the J616 with the same "spp3"
profile as the J316 (family S2, shared by J316, J416, J516, J616 and
J716), so the feed-forward constants are the J316's and the
measured-power constants are speakersafetyd's j316 values (MIT through
2.0.2; credit to the Asahi Linux contributors).  speakersafetyd has no
J616 profile.  Only the sense channel map differs: the six SN012776
amps' slots on the AOP's serializer give LW1 RW1 LW2 RW2 LT RT.

Assisted-by: LLM
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
unified_caps (or --unified-caps) caps the unified governor's thermal
reduction at t_reduction_max and its power-budget reduction at
p_reduction_max, as macOS's loudspeaker manager does (12 dB and 3 dB on
the J700); past them the coil may run to its hard limit, where the
daemon hands the speakers to the kernel.  Off by default.
--ctrl-headroom overrides t_ctrl_headroom for replay sweeps.

Assisted-by: LLM
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
…w-only

- j316, j413 and j616 select governor = asahi and max_reduction = 7 (as
  speakersafetyd's unit does); [Globals] max_reduction is the default
  for -m.
- The unified governor may write the card only with
  --experimental-unified; elsewhere it runs in shadow.
- The udev rule starts the daemon on those three machines too, and the
  unit conflicts with speakersafetyd: one daemon owns the sense PCM.
- README: the V/I machines, the asahi governor and its evidence,
  switching from speakersafetyd and back, shadow/record/replay.

Assisted-by: LLM
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
RyanTheTide added a commit that referenced this pull request Oct 24, 2026
Porting PR #6 onto 0.1.6 turned up a defect in 0.1.6's hand-off.
Uncovered time was charged at the gain the controls held when the
charge was made, not while the time passed. The hand-off introduced
for R3 writes the safe level first. After a failed sample-rate read,
the disarm runs before the pass's charge, so the time since the last
period was charged at 20 dB although it played at 0 dB: 100 times less
power. The same holds for any write between the end of the model's
account and the charge.

The guard now keeps a short record of what the controls held and
whether the lock was renewed since the account's end. A worst-case
charge takes the least reduction held over its span, with the lease
split as before. Measured frames take the most reduction held, for the
budgets' power before the gain. The record is trimmed to the entry in
effect at the account's end.

Two tests fail without this. A hand-off after 150 ms of unaccounted
playback at 0 dB is charged at 0 dB (71.4 C, against 50.2 C before),
and time after it at the safe level. Through the loop, a rate read that
fails right after a period charges the pending time at 0 dB (57.3 C,
against 50.1 C before).

80 of 80 pass.

Assisted-by: LLM
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Superseded by #8, which ports this onto the hardened guard of #7 and fixes the eight findings of this PR's review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant