Skip to content

media: apple: add a scaler/CSC (MSR) mem2mem driver - #1162

Closed
RyanTheTide wants to merge 5 commits into
integration/aurorafrom
msr/scaler
Closed

RyanTheTide wants to merge 5 commits into
integration/aurorafrom
msr/scaler

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

Adds a V4L2 mem2mem driver for the scaler and colour-space converter (MSR) that every Apple silicon SoC has. Nothing on Linux used it, so YUV/RGB conversion and scaling fell back to the CPU or GPU.

Four commits:

  1. Probe-only driver. Binds the existing apple,t6041-scaler and apple,t8140-scaler nodes, attaches MSR-V and the ASE core domain with runtime PM links, requires the DART, reads the identity and idle state, masks and clears the interrupt.
  2. Register encodings with KUnit. The formats the engine is known to take (ARGB32/XRGB32, NV12/NV12M, P010), the CSC as a Q3.12 matrix with 2^-24 offsets derived from the BT.601, BT.709 and BT.2020 Kr/Kb for limited and full range in both directions, and the Q22 scale ratio.
  3. The job builder. Turns a source image, a destination image and an optional matrix into the ordered register writes for one same-size job, ending with the pre-start tag and START.
  4. The m2m device. NV12, NV12M, P010, ARGB32 and XRGB32 on both queues at the same size, colorimetry from the Y'CbCr side, MMIO submission, completion and errors on the MSR interrupt, 500 ms watchdog, PMGR reset before the next job after any failure.

How the register values were found: a dtrace capture of the macOS driver on an M4 Pro (macOS 27.0) while known VideoToolbox and IOSurfaceAccelerator conversions ran. macOS feeds every job to the engine as a register stream, so the capture has each offset and value. All six CSC matrices the driver computes match what macOS programmed bit for bit, and KUnit rebuilds three captured 1080p jobs (NV12 BT.709 to ARGB32, ARGB32 to NV12, P010 BT.2020 to ARGB32) register for register. The hardware output of those jobs on macOS matched a software BT.601/709 reference within 1 LSB. Image-quality tuning seen in the capture (polyphase filter tables, de-ringing parameters) is not used; scaling comes later with filters generated from a public kernel.

Scope: hardware-version 10 to 22 (M1 to M4, A18 Pro) program the engine directly. Hardware-version 23 and later (M5, M6) put firmware in front and are not matched. The match table has T6041 and T8140 for now; the other direct-family compatibles follow once their bindings land.

Testing:

  • W=1 build clean (LLVM, arm64).
  • KUnit apple-scaler 14/14 pass (arm64 qemu).
  • Not yet run on hardware. The DT nodes stay disabled, so nothing changes on existing systems. First hardware check is a one-shot trial DTB on the J616s with the scaler and its DART enabled, then the DT enable goes in a follow-up once it passes.

Draft until the hardware run.

🤖 Generated with Claude Code

Every Apple silicon SoC has one or more MSR engines: memory-to-memory
scalers with a programmable colour-space converter, chroma resampling,
flip/rotate and alpha fill. macOS reaches them through IOSurfaceAccelerator
and VideoToolbox; Linux has had no driver, so scaling and YUV/RGB
conversion fall back to the CPU or GPU.

Hardware-version 10 (M1) through 22 (A18 Pro) program the engine through
MMIO and signal completion with an interrupt. This first step binds the
existing apple,scaler nodes for T6041 (M4 Pro/Max) and T8140 (A18 Pro):
it attaches the MSR power domains with runtime-PM links, requires the
DART, reads the identity and idle state, clears and masks the interrupt
and keeps the engine idle. The video device and job submission follow
once the RGB path is qualified on hardware.

The DT nodes stay disabled, so nothing changes on existing systems.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The job path needs three pieces of register encoding that do not depend
on the hardware being present, so add them with KUnit coverage first:

- the formats the engine is known to take: 32-bit B,G,R,A (ARGB32 and
  XRGB32 in V4L2 terms), NV12/NV12M and P010, with their layout class,
  format code, subsampling descriptor and component order;
- the colour-space converter: a 3x3 Q3.12 matrix with input and output
  offsets (2^-24 of full scale, 26-bit signed), derived from the Kr/Kb
  luma coefficients of BT.601, BT.709 and BT.2020 for limited and full
  range, in both directions;
- the scaler ratio register, Q22 with the two lowest bits clear.

The tests compare against the register values used for the same
conversions in a capture of the engine on macOS 27 on an M4 Pro: all six
matrices and the offsets match bit for bit.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
Add the job builder the submission path will use: it turns a source
image, a destination image and an optional colour matrix into the
ordered register writes for one job, ending with the pre-start tag and
START. It covers conversions without scaling, which are the common case
(decoder output to RGB, RGB to encoder input):

- read and write DMA: layout, format code, subsampling, plane addresses,
  strides, component order, picture and per-plane sizes;
- alpha fill when the source has no alpha and the destination does;
- chroma upsampling for 4:2:x sources and downsampling for 4:2:x
  destinations, ordered dither when going from 10 to 8 bits;
- the scaler at 1:1 (filters off, unit Q22 ratios);
- the CSC matrix, offsets and clamps;
- the per-plane memory request words and control words the engine is
  driven with on every job.

KUnit builds three 1080p jobs (NV12 BT.709 to ARGB32, ARGB32 to NV12,
P010 BT.2020 to ARGB32) and checks that every register ends with the
value the engine was given for the same job on macOS, and that nothing
else is written.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
Expose the engine as a V4L2 memory-to-memory device. The OUTPUT queue
takes NV12, NV12M, P010, ARGB32 or XRGB32, the CAPTURE queue any of
them at the same size. Y'CbCr to R'G'B' and back uses the colorimetry of
the Y'CbCr side (BT.601, BT.709 or BT.2020, limited or full range); the
R'G'B' side is full range and keeps the primaries and transfer function,
since the engine only changes the encoding.

Each job is written with MMIO in one go from the register list the job
builder produces, START last. Completion and errors arrive on the MSR
interrupt; an error or a 500 ms timeout fails the job and resets the
engine through its power-domain reset before the next one. Interrupts
are enabled on the first job after every power-up.

Not yet tested on hardware: the DT nodes stay disabled until the
qualification run.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
After every power-up and after a failed job, wait for the engine to go
idle, write RESET 1 then 0 without reading anything in between (the
register file does not answer while reset is held), wait for idle again,
then clear stale status and enable the done and error interrupts. Only if
the engine never goes idle fall back to the PMGR reset of its power
domain and retry once; otherwise fail the job. This is the start-up
order the engine expects on every generation from M1 Max onwards.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

First J616s hardware attempt (2026-11-08 04:54 AEDT), with the module built from 44d118c against 7.3.0-aurora-fleet27 and a one-shot trial DTB (never committed) that sets scaler@521000000 and iommu@521260000 to okay:

apple-dart 521260000.iommu: deferred probe timeout, ignoring dependency
apple-dart 521260000.iommu: probe with driver apple-dart failed with error -110
apple-scaler 521000000.scaler: deferred probe timeout, ignoring dependency
apple-scaler 521000000.scaler: error -ENODEV: no IOMMU

The driver never reached the hardware. Both nodes sit in the msr0 power domain (pmgr power-controller@470), and the scaler is also in msr0_ase_core (power-controller@478). Both pwrstate nodes are status = "disabled" in the integration J616s DT, so the DART has no power domain and its probe times out. insmod/rmmod were clean, with no DART faults and 0 AVC.

Next: retry with a trial DTB that also enables those two pwrstate nodes. The DT patch that enables the scaler on the J616s has to enable them too.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

J616s hardware PASS, second trial (2026-11-08 05:14 AEDT): the same module (44d118c, built for 7.3.0-aurora-fleet27) and a one-shot trial DTB (never committed) that also sets pmgr msr0 (power-controller@470) and msr0_ase_core (@478) to okay.

apple-scaler 521000000.scaler: MSR hardware-version 21, id 0x40e0037, stale status 0x0
nv12m_709l_argb: PASS maxerr 0 best 1838.3 us mean 1846.1 us (n=19)
nv12_601l_argb:  PASS maxerr 1 best 1844.8 us mean 1853.5 us (n=19)
nv12m_709f_argb: PASS maxerr 1 best 1276.8 us mean 1281.4 us (n=19)
argb_nv12m_709l: PASS maxerr 1 best 1385.2 us mean 1406.0 us (n=19)
p010_2020l_argb: PASS maxerr 1 best 1280.3 us mean 1286.4 us (n=19)
RESULT PASS
  • /dev/video5 "Apple scaler/CSC"; no DART faults; runtime_status = suspended when idle; rmmod clean; SELinux enforcing, 0 AVC.
  • v4l2-compliance: 47/48. VIDIOC_S_FMT fails at fmt_cap.g_ycbcr_enc() != fmt_out.g_ycbcr_enc(). For a CSC m2m device, the capture format should report the colorimetry it actually produces (or copy the output queue's), consistently with what S_FMT returns.
  • The DT patch that enables the scaler on the J616s needs scaler0, its DART and both pwrstate nodes okay.

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