Skip to content

media: apple: isp: run MLVNR noise reduction on the ANE for T604x - #4445

Draft
jgoyvaerts wants to merge 9 commits into
aurora-silicon:mainfrom
jgoyvaerts:camera/j616s-mlvnr
Draft

jgoyvaerts wants to merge 9 commits into
aurora-silicon:mainfrom
jgoyvaerts:camera/j616s-mlvnr

Conversation

@jgoyvaerts

@jgoyvaerts jgoyvaerts commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

What: The J616s ISP firmware runs MLVNR, its ML video noise reduction, on the Neural Engine. macOS sets up an ISP↔ANE endpoint for this, and Linux never did, so the camera ran without it. This series sets up the endpoint as macOS does, configures MLVNR, and makes sure the camera light goes off after a firmware that had to be stopped by force. It replaces the draft branch camera/isp-ane-mlvnr.
Tested: J616s M4 Pro at c9e775b: not yet run. An earlier form of this series (same protocol, test kernel 7.3.0-m4.243) streams with MLVNR: noise 0.62 with MLVNR vs 3.18 without, same scene and gain, no firmware asserts, clean stop. Build and KUnit at every commit: accel/ane 343/343, apple-isp 66/66, W=1 clean.
Risk: two-way door for the drivers; one-way for the apple,ane DT binding (new optional property). Macs: T6040/T6041 (J614s, J616s, J614c, J616c) only. Other SoCs have no peer_doorbell and no apple,ane link, so they keep today's path. The forced-stop restart (commits 5 and 7) touches every SoC with fw_restart (T8140, T6040, T6041, T8142).
Not yet: a run at the PR head on the J616s; the restart after a forced stop on the J616s (Nick's half is verified on J815); the installer side, which has to copy /System/Library/Isp/afpp/<board>/ispane.afpp from the Mac's restore image to apple/ispane-<board>.afpp (aurora-silicon/aurora-installer#355); the 0x300000 mcache grant macOS gives DSID 0x0e in the cache controller (not done on Linux, no effect seen).

Details

How it works. When the camera firmware starts, macOS powers the ANE, has its firmware load the camera's networks (ispane.afpp), gives the ANE firmware the ISP's two 16 KiB rings and the ISP's doorbell, then gives the ISP firmware the same rings and the ANE's doorbell (CISP_CMD_IPC_ENDPOINT_SET2). The ANE firmware then maps the ISP's surfaces by the IDs the ISP firmware got for them at SHAREDMALLOC.

Bring-up on the ANE (H16) channel 1: MCACHE_SIZE_GET (0x26), MCACHE_DSID_SET (0x2e: prefetch DSID 0, intermediate spill DSID 0x0e), LOAD_AFPP (0x205), IPC_ENDPOINT_SET2 (0x2a: the ring the engine reads, the ring it writes, the ISP's doorbell); then the ISP's SET2 (0x300c). Teardown is the reverse: ISP unset (0x300d) before the firmware stops, ANE UNSET2 (0x2b) and UNLOAD_AFPP (0x206) while the ISP is still powered, then the ISP powers down.

Commits:

  1. accel/ane: serve a peer coprocessor's surface maps on SHAREDMALLOC.
  2. accel/ane: h16: the endpoint commands, encoders and the session state. An ANE restart clears the peer state, so the new firmware is never sent an unset for an endpoint it never had.
  3. accel/ane: h16: the attach/detach API for the ISP (<linux/soc/apple/ane.h>). The ANE stays powered while a peer is attached. Its IO_T2H reports are drained while only the peer runs; without that the ANE firmware runs out of command cache after a few seconds.
  4. dt-bindings: apple,ane on the ISP.
  5. Nick's fix from media: apple: isp: H17 stream settings, lens-circle crops and crash light for the J813/J815 IMX958 #2843 (cherry-pick -x): after a crash, the firmware is booted once more and stopped cleanly, which powers the camera module down.
  6. apple-isp: the endpoint. It is set up only with the link, a running ANE and the networks file. If any of them is missing, one line says so and the camera runs as before. If a stream fails to start with a new endpoint, or the firmware has to be stopped by force while one is attached, the driver stops using the endpoint until it is loaded again. This keeps the camera working without MLVNR.
  7. apple-isp: Nick's restart ran only when an IPC error was set. On J616s the stop command itself timed out with the ANE client stalled ("failed to send OPCODE 0x0101", "firmware did not stop"), with no IPC error set, and the light stayed on. The restart now follows every forced stop. The restart work is also cancelled after the last halt in remove, a failed probe and shutdown, so it can no longer boot firmware on a removed device.
  8. apple-isp: APPLE_CH_MLVNR_MODE_CONFIG_SET (0xc11f) as macOS sends it: automatic, gain thresholds per sensor (IMX958), 30 fps, a 30-frame fade.
  9. dts: apple,ane = <&ane> on the T604x ISP.

For the reviewer:

  • Both doorbells are per-SoC entries: the ISP doorbell (0x581668000, value 0x10) in the T6041 hw table, next to the existing 1:1 firmware MMIO windows, and the ANE doorbell offset (engine window + 0x1660000, value 1) in the ANE platform data. The ADT has no property for either.
  • The MLVNR thresholds (enter at 3584, leave below 2816) are the values macOS sends to the firmware, read from the ISP firmware's own log of the command on macOS. They are interface values for the sensor, not a tuning file. media: apple: isp: H17 stream settings, lens-circle crops and MLVNR for the J813/J815 IMX958 #2645 carried the same command for the J815.
  • The networks file is per board, as on macOS, and is never in the tree. The installer copies it from the Mac's own install.

Provenance. Command layouts and the sequence come from macOS 27.2.1 camera and ANE driver logs on the J616s, from hypervisor-trace write-ups of the ISP↔ANE exchange, and from the ISP and ANE firmwares' own log lines on macOS and on Linux. Firmware asserts on Linux confirmed each step: AFPPLoaded without the networks, afppInterSpillDsid != 0xFF without the DSID command, out of command cache without draining the reports. Nothing was read out of a firmware image or a compiled macOS driver.

Thanks to Ryan Murray for the hypervisor-trace write-ups and the original draft, and to Nick for the crash-light fix.

Evidence

Same-scene A/B, J616s, MLVNR on vs off in one stream (luma temporal std = noise, chroma std, detail = Laplacian energy, luma, total gain):

kernel MLVNR luma std chroma std detail luma gain
7.3.0-m4.243 on 0.62 0.50 1.58 131.0 865
7.3.0-m4.243 off 3.18 1.07 2.13 131.5 865
7.3.0-m4.242 on 0.60 0.50 1.52 125.6 980
7.3.0-m4.242 off 3.22 1.17 2.09 125.5 980

The detail measure includes noise, so part of its drop is the noise going away. To the eye, the frames with MLVNR are clean with crisp edges, and the ones without are grainy.

ISP firmware log with the endpoint (m4.242/m4.243): AneClient … Initialize done, MLVNR Mode Config ctrl=2 … 3584:2816, LoadProg 'mlnr' 26, NetInit (11 procedures, bufCnt 6), MLVNRProc start done, all downstream pipes started; at stream stop Stop MLVNRProc, then H16 peer endpoint unset and the ANE idles. No asserts, timeouts or ANE recoveries.

Per-commit checks at 49424e0..c9e775b: make W=1 drivers/accel/ane/ drivers/media/platform/apple/isp/ clean at every commit; KUnit (arm64 QEMU) drivers/accel/ane/transport.kunitconfig 343/343, drivers/media/platform/apple/isp 66/66; checkpatch --strict clean apart from MAINTAINERS notes for the new files.

jgoyvaerts and others added 9 commits December 5, 2026 04:12
With the ISP's endpoint set, the H16 firmware asks the host to map the
ISP's surfaces into its domain by the ID the ISP firmware got for each
of them: a SHAREDMALLOC request with zero DVA and size, and the ID in
the third word. The host answers with the DVA (bit 0 set), the size and
the ID. The firmware gives a surface back with an ordinary FREE of its
DVA, answered 1, 0, 0.

Add optional peer_map and peer_unmap transport ops for that. A FREE of
a DVA the peer does not know stays an ordinary FREE.

The request and answer shapes come from a written summary of a
hypervisor trace of macOS 27.0 starting the camera on a J616s.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
On the J616s the ISP firmware runs its ML video noise reduction (MLVNR)
on the ANE. When the camera starts, macOS gives the ANE firmware the
ISP's endpoint with these IO commands, each answered in place:

  MCACHE_SIZE_GET (0x0026)     the firmware answers the mcache size it
                               wants (0x300000)
  MCACHE_DSID_SET (0x002e)     a count of 2, then (3, 0) for the
                               prefetch and (4, 0xe) for the
                               intermediate spill of the networks
  LOAD_AFPP (0x0205)           the ISP's networks, one buffer
  IPC_ENDPOINT_SET2 (0x002a)   endpoint index, the ring the engine
                               reads, the ring it writes, the ISP's
                               doorbell and its value

and when it stops, IPC_ENDPOINT_UNSET2 (0x002b), after which the
firmware asks to unmap the ISP's surfaces, then UNLOAD_AFPP (0x0206).

Add the commands and the session calls for them. Gating stays off while
an endpoint is set, since the ISP hands the firmware work on its own.
The firmware is served outside any request, as the ISP's work runs while
no host request waits: SHAREDMALLOC for the ISP's surfaces, and its
reports on IO_T2H, each of which holds one of its command slots until
read. A failed command logs its ID and prints the firmware's log before
the recovery boots it again.

The command names are from macOS's ANE driver log (27.2.1, request and
teardown lines), the layouts from written summaries of hypervisor traces
of macOS 27.0 starting the camera on a J616s. Three facts are from runs
on the J616s, through the firmware's own log: the networks must be
loaded before SET2 (it asserts "AFPPLoaded" otherwise), the spill DSID
must be set (it asserts "afppInterSpillDsid != (0xFF)" on the first
frame otherwise), and its reports must be read while the ISP runs (it
asserts "out of command cache" within seconds otherwise). The ring
order is from the same runs: with the ring the engine writes first, the
firmware takes the ISP's doorbell but never reads its ring.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
Add <linux/soc/apple/ane.h> for the ISP driver: it finds the engine by
its apple,ane phandle and attaches its two rings, its doorbell and its
networks file. The engine then stays up (a runtime PM reference), the
rings and the networks are mapped, the endpoint is set up with the
spill DSID from the platform data, and the engine's doorbell for the
ISP goes back to the ISP: engine window + 0x1660000, value 1 on the
J616s, as macOS gives them to the ISP firmware there.

The firmware's map requests and reports come while nothing waits, so
with a peer attached the channel IRQ queues a work item that serves
them. Peer surfaces come from the program window and are unmapped when
the firmware frees them or the peer detaches, which also unmaps the
rings and the networks. A device link orders system sleep: the ISP
suspends first.

The doorbells and the spill DSID are those macOS uses on the J616s
(its camera and ANE driver logs, and a written summary of a hypervisor
trace of macOS 27.0 there).

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
The ISP firmware runs its ML noise reduction on the Neural Engine; the
ADT links the two (isp0's function-ane_ep_control, -data_param_set and
-data_param_get point at ane0). Describe that link.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
A crashed firmware is forced off: the stop command times out ("coproc
not in WFI"), so the firmware never sends the camera module its
power-down. The sensor stays powered, and its green status light stays
on until the next camera use. Seen on J815 (2026-11-21) after an ISP
firmware assert.

On hardware whose firmware starts again (hw->fw_restart), a halt after
an IPC error now schedules fw_recover_work. It boots the firmware once
more and stops it the normal way, which
powers the module down. It runs once: a second crash is left as it is.
On J815 the light went off after the crash, and the log said "crashed
firmware: restarted and stopped it to power the camera down".

Signed-off-by: Nick <nekreshen@yandex.ru>
(cherry picked from commit 69dc771)
The ISP firmware runs MLVNR, its ML video noise reduction, on the
Neural Engine. When the camera starts, macOS loads the camera's
networks into the ANE, gives the ANE firmware the ISP's two 16 KiB rings
and the ISP's doorbell, and then gives the ISP firmware the same rings
and the ANE's doorbell (CISP_CMD_IPC_ENDPOINT_SET2). Without it the ISP
firmware's ANE client is never set up, and MLVNR asserts in its
allocator.

Do the same after START when the device tree links the ISP to an ANE
that is up (apple,ane) and the networks are installed:
apple/ispane-<board>.afpp, macOS's /System/Library/Isp/afpp/<board>/
ispane.afpp from the Mac's own install, read once. On T604x the ANE may
write the ISP's doorbell (t604x.dtsi). Without the file, the link or the
ANE, one line says so and the camera runs as before.

- The ANE firmware names the ISP's surfaces by the ID the ISP firmware
  got for each at SHAREDMALLOC, which macOS hands out there; hand out
  IDs too. A surface the ANE still maps is freed only once it lets go.
- Before the firmware stops, it unsets the endpoint, and the ANE
  detaches while the ISP is still powered: until then the ANE firmware
  may ring the ISP's doorbell, and an access to the powered-down block
  would hang the fabric.
- The camera must work without MLVNR: a stream that fails to start with
  an endpoint no stream has used yet, or a firmware that has to be
  stopped by force with the endpoint attached, ends the endpoint until
  the driver loads again.

The sequence and the values are from macOS's camera and ANE driver logs
on the J616s (27.2.1) and the ISP firmware's own log of the commands it
receives, on macOS and on Linux.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
The restart that powers the camera module down after a crash only ran
when an IPC error was set at the halt. A firmware that hangs without
one is forced off too: on J616s, with the ISP firmware's ANE client
stalled, the stop command timed out ("IO: failed to send OPCODE
0x0101", "firmware did not stop, stopping the coprocessor") and the
camera light stayed on until the next camera use. No IPC error was
set, so no restart followed.

Schedule the restart whenever the stop was forced. That covers the IPC
error case too: a firmware with an IPC error takes no stop command, so
its stop is always forced.

The restart work was cancelled before the last halt in remove, the
failed probe and shutdown, and before the halt in
apple_isp_firmware_flush(): a forced stop there left it queued, to
boot firmware on a removed device. Cancel it after those halts.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
macOS configures MLVNR on the J616s before each stream with
APPLE_CH_MLVNR_MODE_CONFIG_SET (0xc11f): automatic (2), entering at a
total gain of 3584 and leaving below 2816, at 30 fps either way, fading
out over 30 frames. The ISP firmware logs these words under those names
when it receives them.

Send the same when the ANE endpoint is set, with the thresholds per
sensor (the IMX958), whichever setfile is loaded.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
The ISP firmware runs MLVNR on the Neural Engine (ADT isp0's
function-ane_ep_control, -data_param_set and -data_param_get point at
ane0). The ANE's address filter already lets it ring the ISP's doorbell
(0x581668000), and the ISP's the ANE's (0x485660000).

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>

@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 c9e775b

Failure: A failed MLVNR stream start followed by a retry during the ISP keepalive window still sends the MLVNR configuration command. The failure helper sets ane_failed, but the firmware-running fast path retains ane_set and isp_ch_configure_mlvnr checks only ane_set. GCC and Clang UBSan replays of the exact decision paths each issued one MLVNR command and returned the injected error on retry. The guard control issued zero and succeeded. Ensure a retained endpoint cannot re-enable MLVNR on the fallback path.

Failure: The LED recovery worker reports a clean stop if its restart succeeds but its second stop is forced. Replaying the exact worker and halt code with that input produced one forced stop, one clean-recovery message and no retry. Preserve the one-attempt bound and report the failed stop accurately.

Failure: isp_mlvnr_gains commits per-sensor enter/exit gain settings copied from the macOS camera configuration, as the source comment and PR describe. These are camera tuning choices under the public-clean rule. Remove the copied settings from the public source and description and use independently derived settings or the supported per-machine data path.

Checked: Built and decompiled five T604x DTBs, including the J616s tether variant, and confirmed the ANE links. The J616s stock comparison is available; exact candidate qualification remains on the machine-owner route.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Checked T6000 (M1 Pro, J316s) ADT and Linux driver state for a follow-up port once this lands:

  • T6000 ADT hardware facts (live J316s 13.5 ADT):
    • /arm-io/isp0 has the same three peer links (function-ane_ep_control, function-ane_data_param_get, function-ane_data_param_set -> ane0).
    • /arm-io/dart-isp0 filter-data-instance-0[10] exposes 0x285460000..0x285460003 (ane0 + 0x1460000, ISP->ANE peer doorbell).
    • /arm-io/dart-ane0 filter-data-instance-0[2] exposes 0x38545c000..0x38545c003 (isp0 + 0x145c000, ANE->ISP peer doorbell).
  • Current T6000 prerequisites before porting #4445:
    1. <linux/soc/apple/ane.h> is provided by ane_h16_backend.c (ane_t6021.ko firmware IPC), whereas T6000 currently binds ane.ko (direct-MMIO TM/TQ, no ASC firmware boot, and no Aura apple,t6000-ane preload handoff in kboot.c).
    2. J316s is still on the 13.5 stub (T8112 only joined ane_h16_backend on 27.2 preload 1bfc4b2b5373).

Next on J316s we will capture the 13.5 apple_isp ISPASC: log (CAneClient/MLVNRProc) and check macOS 26.2 /System/Library/Isp/afpp/ presence so we know whether a T6000 port requires 27.2 stub + H13 firmware backend first. Happy to help with the follow-up PR once those prerequisites are in place.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Quick CI note: apple-build, dt-checks, and KUnit ane-boot ane-pm isp are all green at c9e775b7f51f. The sole host-tests failure is tools/asahi/isp-tests.py missing stubs/declarations for the two newly called helpers isp_ane_stream_failed(isp) and isp_ane_stream_started(isp) in its generated session.c harness.

@sithglan

sithglan commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Tested this series adapted to M5 MacBook Air (J813 / T8142, H17 ANE via ane_fw_cold, aura 1.0.0b18-100-g99488019, apple/ispane-j813.afpp 0xc5a040 bytes) and opened a stacked Draft follow-up PR at #4466:

  • T8142 ADT doorbells: dart-isp dapf-instance-0 exposes 0x421660000..0x421660003 (ane + 0x1660000, value 0x1), dart-ane dapf-instance-0 exposes 0x4a1668000..0x4a1668003 (isp + 0x1668000, value 0x10), spill DSID 0xe, mcache 0x300000.
  • End-to-end J813 result (1280×720 NV12, 60 frames, same scene):
    • Config 7 (3040x2880): MLVNR ON luma_std=1.071, chroma_std=0.227 (ANE IRQs +229, Load Program 0.6–0.7 ms, 0 asserts, 5/5 clean start/stop cycles) vs OFF luma_std=4.113, chroma_std=1.053.
    • Config 5 (1920x1080): MLVNR ON luma_std=1.674, chroma_std=0.341 (ANE IRQs +150, 0 asserts) vs OFF luma_std=3.285, chroma_std=0.957.
    • Config 8 (1520x1440 binned) OFF: luma_std=1.694, chroma_std=0.533.
  • Important finding for M5 / binned IMX958 boards (J813, J714s): ispane-j813.afpp contains 11 compiled networks covering full-resolution modes (3040x2880, 1920x1080, etc.) and none for 2×2 binned 1520x1440 (config 8). Leaving config 8 active with MLVNR configured triggers NetInputCheck, 1046: wrong input buffer size: actual: 3072, 1440 → ASSERT: CMLVNRProcImp.cpp, 1046: 0 as soon as gain crosses enter_gain (3584). Commit 7's forced-stop restart also proved itself live here: it cleanly restarted/stopped the ISP, turned the camera indicator light off, unmapped every peer surface, and disabled the ANE endpoint until reload.

(Also checked M1 Air J313 / T8103: not applicable — register-driven H13 ANE ane.ko and IMX248, no RTKit ANE firmware or ispane.afpp.)

This branch has not been deployed

No deployments
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.

4 participants