Skip to content

drm/apple: m4: expose panel_adaptation sysfs for white-point adaptation - #4297

Merged
RyanTheTide merged 2 commits into
aurora-silicon:mainfrom
IronsideXXVI:j604/dcp-m4-panel-adaptation
Dec 5, 2026
Merged

RyanTheTide merged 2 commits into
aurora-silicon:mainfrom
IronsideXXVI:j604/dcp-m4-panel-adaptation

Conversation

@IronsideXXVI

Copy link
Copy Markdown
Contributor

What: Expose a read/write panel_adaptation sysfs attribute on the internal Apple T8132 (M4) firmware 26.6.2 DCP platform device (/sys/bus/platform/drivers/apple-dcp/*/panel_adaptation, absent on external DCPs) so userspace white-point adaptation daemons (True Tone) can drive set_matrix (A423, location 9) without competing with compositor-owned CRTC KMS CTM (night light):

  • Accepts nine space-separated decimal signed S31.32 two's-complement coefficients in row-major order (4294967296 0 0 0 4294967296 0 0 0 4294967296 is identity), rejects malformed or trailing input with -EINVAL, and clamps each coefficient to [-4.0, +4.0].
  • Sends nothing at boot or on identity/unchanged writes; writing identity clears a previously active adaptation once.
  • Queues set_matrix (A423, location 9) on dcp->runtime->wq for live writes so True Tone ramps step smoothly without waiting for a swap.
  • Never issues an RPC while the display is blanked (display_off), power-gated (gated), suspended (powered_off), or quiescing — preventing session quarantine when a daemon ramps while the DCP CPU is stopped. Pending or active adaptations are applied before the first swap on wake (iomfb_m4_present_at()) and re-marked pending across dcp_m4_set_power_state(false), iomfb_m4_power_ungate(), and iomfb_m4_resume().
  • Documents the sysfs ABI in Documentation/ABI/testing/sysfs-driver-apple-dcp (registered in MAINTAINERS) and adds the m4-panel-adaptation host selftest in tools/testing/selftests/drivers/gpu/apple-dcp/.

Tested:

  • Offline at exact head 2074dc12b0 on origin/main (8ae72dc32d, git merge-tree --write-tree clean a31bd899e251):
    • scripts/checkpatch.pl --strict --no-tree: 0 errors, 0 warnings, 0 checks (569 lines checked).
    • Linux host selftests (tools/testing/selftests/drivers/gpu/apple-dcp/run.py --group m4, Clang 23.1.5, -Wall -Werror -fsanitize=address,undefined): 11/11 PASSED (including m4-dpms-retirement, new m4-panel-adaptation, and m4-power-gate).
    • ARM64 LLVM=1 W=1 KCFLAGS="-Werror" (Clang 18.1.8, CONFIG_DRM_APPLE=m, CONFIG_DRM_APPLE_DCP_T8132=y):
      • CONFIG_PM_SLEEP=y: drivers/gpu/drm/apple/iomfb_m4.o (908536 B) — 0 warnings.
      • CONFIG_PM_SLEEP=n: drivers/gpu/drm/apple/iomfb_m4.o (904640 B) — 0 warnings.
  • Hardware (Apple M4 MacBook Pro 14", J604, T8132, firmware 26.6.2 / 25G83) running this panel_adaptation ABI and power-gate guard on live linux-aurora (1.934 through 1.1699 / 1.1700):
    • Full 3x3 True Tone adaptation ramp (~1.9 s) active via /sys/bus/platform/drivers/apple-dcp/*/panel_adaptation with 0 DCP faults.
    • Power-gated blank + deep-sleep regression (l.18931): writing panel_adaptation while display was off past power_gate_ms (both across deep sleep and 25 s blank without sleep) produced 0 quarantined sessions, panel lit cleanly on wake, and adaptation matrix re-applied.

Risk: two-way door · T8132 (M4) internal firmware 26.6.2 DCP only (!external). External DCPs do not create the sysfs group and return -ENODEV; a desktop that never writes panel_adaptation sends zero A423 RPCs. A plain revert removes the attribute cleanly.

Not yet:

  • Standalone exact-head origin/main (2074dc12b0) hardware boot and DPMS/deep-sleep run on J604, J713, and J715.
  • Composition with open approved PR #3262 (drm/apple: m4: colour matrix (CTM) on the internal panel): #3262 also targets set_matrix (A423) location 9 for the CRTC KMS CTM property. Once #3262 merges (or if #3262 lands first), panel_adaptation composes cleanly on top as out = adaptation * CTM * in (Q32.32 multiply (a >> 16) * (b >> 16) with both matrices clamped to [-4.0, +4.0]) so a compositor's night light and True Tone share location 9 without replacing each other.
Details and verification

Scope (j604/dcp-m4-panel-adaptation, head 2074dc12b0 on main 8ae72dc32d)

  • drivers/gpu/drm/apple/iomfb_m4.c (+164 / -1): adapt_work, adapt[9], adapt_active, adapt_pending, iomfb_m4_send_adaptation(), panel_adaptation sysfs group registered after iomfb_m4_release() on !external, pre-swap application in iomfb_m4_present_at(), re-mark across blank/power-gate/resume, and cancel_work_sync() in iomfb_m4_quiesce().
  • Documentation/ABI/testing/sysfs-driver-apple-dcp (+26): sysfs ABI documentation.
  • MAINTAINERS (+1): registers Documentation/ABI/testing/sysfs-driver-apple-dcp under APPLE DCP.
  • tools/testing/selftests/drivers/gpu/apple-dcp/m4.py & run.py (+187 / -7): updates m4-dpms-retirement and m4-power-gate for the new fields/helper and adds m4-panel-adaptation covering boot identity no-op, malformed input -EINVAL, [-4.0, +4.0] clamping, 80-byte A423 wire encoding, duplicate rewrite suppression, gated/blanked deferral + P->M->S wake order, firmware status refusal, identity clear, external -ENODEV, and transport failure before swap.
  • Sign-off: Signed-off-by: Dylan Ironside <dylan@dylanironside.io>.

Offline Verification

  • scripts/checkpatch.pl --strict --no-tree: 0 errors, 0 warnings, 0 checks (569 lines checked).
  • git merge-tree --write-tree origin/main HEAD: clean (a31bd899e251b43705464cdb860633f951a44a11).
  • Linux host selftests (run.py --group m4, Clang 23.1.5, ASan/UBSan -Wall -Werror): 11/11 PASSED.
  • ARM64 LLVM=1 W=1 KCFLAGS="-Werror" (Clang 18.1.8): drivers/gpu/drm/apple/iomfb_m4.o 0 warnings with both CONFIG_PM_SLEEP=y (908536 B) and CONFIG_PM_SLEEP=n (904640 B).

On T8132 (M4) internal panels running firmware 26.6.2, macOS drives
True Tone white-point adaptation by sending a 3x3 colour matrix to the
DCP with set_matrix (A423) at location 9 (keeping factory calibration at
location 8). On Linux, the CRTC KMS CTM property is owned by the
compositor for night-light clients, leaving no separate interface for a
panel white-point adaptation daemon.

Expose a read/write panel_adaptation sysfs attribute on the internal M4
DCP platform device (absent on external DCPs) accepting nine decimal
signed S31.32 two's-complement values in row-major order, clamped to
[-4.0, +4.0]:
- Nothing is sent at boot or on identity/unchanged writes; writing
  identity clears a previously active adaptation once.
- Live writes queue set_matrix (A423) at location 9 on dcp->runtime->wq
  so userspace ramps can step smoothly without waiting for a swap.
- Writes arriving while the display is blanked, power-gated, or
  suspended never issue an RPC to a stopped coprocessor (avoiding
  session quarantine); the matrix stays pending and is applied before
  the first swap on wake, and active adaptations are re-marked pending
  across blank, power-gate, and system resume.

Document the sysfs ABI in Documentation/ABI/testing/sysfs-driver-apple-dcp
and add an m4-panel-adaptation host selftest in
tools/testing/selftests/drivers/gpu/apple-dcp/.

Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
@IronsideXXVI
IronsideXXVI requested review from a team as code owners December 4, 2026 09:59
@github-actions github-actions Bot added area:display Area from CODEOWNERS (bot) blast:m4 Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 4, 2026
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Merge readiness at 995f113c: not ready yet

Check Detail
CI at head ok 9 checks passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head no no line with the head SHA 995f113c, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text ok Risk: two-way · T8132 (M4) internal firmware 26.6.2 DCP only (!external). External DCPs do not create the sysfs group and return -ENODEV; a desktop that never writes paneladaptation sends zero A423 RPCs. A plain revert removes the attribute cleanly.

Areas: area:display

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: REQUEST_CHANGES at 2074dc12b0e597554838b1a8147cb1786a1fedaf

Blockers:

Failure: panel_adaptation_store() at iomfb_m4.c:3738 accepts a malformed matrix with two adjacent coefficients and commits it. For example, 1-2 0 0 4294967296 0 0 0 4294967296 is accepted as nine values with leading coefficients 1 and -2. The whitespace in the scan format can consume zero bytes, as the kernel's vsscanf() specifies. The store also accepts a complete matrix followed by an embedded NUL and trailing bytes, returning the full write count and queuing the matrix. This contradicts the documented rejection of malformed and trailing input. Validate the delimiters and the full write length before storing or queuing an update.

Checked: Replayed the exact store function and checked the kernel scanning rules. The control with visible trailing text returned -EINVAL, and the space-separated control succeeded. Reviewed power-state guards, wake ordering and device-managed attribute lifetime. The delta changes no DT files.

Evidence

Both malformed writes returned the full count and queued an update in the host replay. The delimiter behavior follows the kernel scanner, and the store only uses the write count for its return value.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Cross-PR note for whoever merges this or #3262 second: #3262 (KMS CTM / night light on the same M4 internal panel) also sends set_matrix (A423) at location 9. Once both are on main, the last writer wins: a True Tone write through panel_adaptation replaces the compositor's night-light CTM, and the reverse. That contradicts the PR's 'without competing with compositor-owned CRTC KMS CTM'. Whichever lands second should send one product matrix (adaptation × CTM) through a single sender, so both effects apply.

…n_store

Replace the sscanf() call in panel_adaptation_store() with an explicit
length-bounded decimal parser that rejects embedded NUL bytes anywhere
within count, requires at least one ASCII whitespace delimiter between
consecutive coefficients, rejects out-of-range s64 values, and rejects
any trailing non-whitespace bytes before updating state or queuing work.

Expand the m4-panel-adaptation host selftest to cover adjacent signed
coefficients, embedded NUL with trailing bytes, hex/overflow/empty
inputs, whitespace variations, and S64 boundary clamping.

Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
@IronsideXXVI

Copy link
Copy Markdown
Contributor Author

Addressed the panel_adaptation_store() validation findings in 995f113c6:

  • Replaced sscanf() with a length-bounded decimal parser (iomfb_m4_parse_adaptation()) operating strictly over [buf, buf + count).
  • Any embedded NUL byte within count is rejected with -EINVAL (memchr(buf, '\0', count)).
  • At least one ASCII whitespace delimiter is required between consecutive coefficients, rejecting adjacent signed values such as 1-2 0 0 ... and 1+2 0 0 ....
  • Out-of-range s64 values, non-decimal prefixes, and any trailing non-whitespace bytes are rejected with -EINVAL before updating dcp->adapt or queuing work.
  • Expanded m4-panel-adaptation in tools/testing/selftests/drivers/gpu/apple-dcp/m4.py to cover adjacent signed coefficients, embedded NUL + trailing bytes, trailing NUL, hex/overflow/empty inputs, whitespace variations, and S64_MIN/S64_MAX clamping.

Offline verification:

  • scripts/checkpatch.pl --strict: 0 errors, 0 warnings, 0 checks.
  • ARCH=arm64 LLVM=1 W=1 KCFLAGS="-Werror" drivers/gpu/drm/apple/iomfb_m4.o: clean compile (0 warnings).
  • tools/testing/selftests/drivers/gpu/apple-dcp/run.py --group m4 with -fsanitize=address,undefined under both Clang and GCC: 11/11 PASS on both compilers.

@github-actions github-actions Bot added the needs-hw-row No hardware row at the PR head (bot) label Dec 4, 2026

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 995f113

This only re-checks the earlier App blockers at 2074dc1. panel_adaptation_store() now uses its own parser instead of sscanf.

Checked:

  • Adjacent tokens: from the second coefficient on, at least one whitespace byte is required before each token, so 1-2 ... and 1+2 ... (and the same at the last position) return -EINVAL. 0x10 stops at x, and the parse then fails the delimiter or end check.
  • Trailing input: after nine values, only whitespace may remain up to count, and any NUL inside the written count bytes is rejected (memchr). Both NUL cases from the earlier finding are now host-test cases.
  • Overflow: mag > (limit - d) / 10 keeps mag * 10 + d <= limit, with limit = S64_MAX for positive values and 2^63 for negative ones, so -9223372036854775808 parses and ±1 beyond fails. A rejected write leaves stored state untouched (the assertions check adapt_active/pending/queued/matrix_calls).
  • The only other change is the host-test harness picking up the new function. I approved the real fork CI runs.

Follow-ups (not blocking):

  • As noted on the PR, #3262 also writes set_matrix location 9. Whichever of the two lands second needs one combined sender.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ORCH5 merge sweep: passes the six-point gate (CI green at 995f113, independent review approval by aurora-silicon-review[bot], no taint, no Stage 1/LocalPolicy change).

@RyanTheTide
RyanTheTide merged commit 7b6b74f into aurora-silicon:main Dec 5, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:display Area from CODEOWNERS (bot) blast:m4 Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants