Repository navigation
media: apple: isp: run MLVNR noise reduction on the ANE for T604x - #4445
jgoyvaerts wants to merge 9 commits into
Conversation
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>
There was a problem hiding this comment.
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.
|
Checked T6000 (M1 Pro, J316s) ADT and Linux driver state for a follow-up port once this lands:
Next on J316s we will capture the 13.5 |
|
Quick CI note: |
|
Tested this series adapted to M5 MacBook Air (J813 / T8142, H17 ANE via
(Also checked M1 Air J313 / T8103: not applicable — register-driven H13 ANE |
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,aneDT binding (new optional property). Macs: T6040/T6041 (J614s, J616s, J614c, J616c) only. Other SoCs have nopeer_doorbelland noapple,anelink, so they keep today's path. The forced-stop restart (commits 5 and 7) touches every SoC withfw_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.afppfrom the Mac's restore image toapple/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'sSET2(0x300c). Teardown is the reverse: ISP unset (0x300d) before the firmware stops, ANEUNSET2(0x2b) andUNLOAD_AFPP(0x206) while the ISP is still powered, then the ISP powers down.Commits:
accel/ane: serve a peer coprocessor's surface maps on SHAREDMALLOC.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.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.apple,aneon the ISP.cherry-pick -x): after a crash, the firmware is booted once more and stopped cleanly, which powers the camera module down.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.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.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.apple,ane = <&ane>on the T604x ISP.For the reviewer:
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:
AFPPLoadedwithout the networks,afppInterSpillDsid != 0xFFwithout the DSID command,out of command cachewithout 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):
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 stopStop MLVNRProc, thenH16 peer endpoint unsetand 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.kunitconfig343/343,drivers/media/platform/apple/isp66/66;checkpatch --strictclean apart from MAINTAINERS notes for the new files.