Repository navigation
media: apple: avd: AX03 capture, reference pins, AV1 reuse order and H.264 FLUSH after HOLD - #3978
tully-8888 wants to merge 10 commits into
Conversation
AVD already writes every picture as an Apple Interchange reference image. Let userspace take that image as the CAPTURE picture, so the linear output write (DECOMP, HDR bit 12) can be skipped. AX12 is 8-bit 4:2:0 and AX03 is 10-bit 4:2:0. The image starts at offset 0 and holds Y data, Y metadata, CbCr data and CbCr metadata, each 128-byte aligned, with each plane's metadata right after its data. Tiles are sized at 10 and 20 bits per element for AX03, which is the 1280 / 640 byte tile stride the hardware writes on J714s (the linear-path reservation of 2048 / 768 bytes is larger than needed). At 7680x4320 AX03 the regions start at 0, 41472000, 43569152 and 64305152 and the image ends at 64829440. bytesperline is 0. The codec tails (AV1 CDFs, colour and segmentation, H.264/HEVC MV and colour) stay at the end of the buffer, after the image. Linear NV12, NV16 and P010 negotiation, sizing and DECOMP are unchanged, and stay the default first format. The Apple frame-params backend does not offer the new formats, and H.264 4:0:0 is refused with Interchange because its grey chroma is filled by the CPU into linear rows. HEVC 4:0:0 and 4:2:2 are refused with Interchange too. Behind an Interchange image the H.264 MV table starts on a 256-byte boundary, as the HEVC one does. The image is only 128-byte aligned, and the LSR address form drops the low 8 bits, which put the table of a 128x64 AX03 picture (image end 15744) into its CbCr metadata at 15616. The regions sit where the tile grid of the CAPTURE format puts them, so each run checks that the picture has the same 32x32 tile grid. Padding inside the last tile row or column (1080 rows in a 1088 row format) is fine; another grid fails the frame, and userspace has to renegotiate the format. An AV1 superres frame must also keep its coded and upscaled widths on the same tile columns until its reference grid is known. A coded width steps by 64, so it also stands for pictures up to 63 pixels narrower. Interchange CAPTURE widths step by the 32 pixel tile instead and may sit up to 32 pixels below the coded width: a 96x64 picture (OUTPUT 128x64) negotiates 96x64 and keeps its 3 tile columns. VP9 checks such a frame against the CAPTURE width on the same 32 pixel step. The H.264 MV table is placed for the coded size, so it is reserved for the larger of the coded and CAPTURE sizes. H.264 finds reference and colocated MV buffers by timestamp, and a failed DMA-BUF prepare can release a plane while its timestamp stays. Each such CAPTURE buffer must be mapped and still hold the picture, the reference image and the MV table before its address is used, or the frame fails. The MV offset cannot wrap for a short buffer. The H.264, VP9 and AV1 host tests get fixtures for the new helpers. The layout follows the avd/interchange-output-v1 draft (PR aurora-silicon#709). Signed-off-by: vlad <tulituvlad@gmail.com>
The submit worker looked up reference CAPTURE buffers without the vb2 queue lock. A failed DMA-BUF prepare could unmap a reference between the backing check and the DMA address read, and a later prepare could attach new memory to a buffer that still carried the old timestamp. Keep AVD's own record of decoded buffers: the preamble publishes the destination with the picture's timestamp on a per-context list under ref_lock, and buf_cleanup and CAPTURE STREAMOFF withdraw it. avd_find_ref() searches only that list and pins each reference a frame reads. The pins drop when the frame retires (done, error, cancel or recovery). Each frame resolves a timestamp once, hit or miss, so every later lookup in that frame returns the same buffer and generation. A new CAPTURE buf_cleanup forgets the timestamp, bumps the buffer's generation, waits for the pins, and clears the compression layout only after the last pin is gone. vb2 calls it under the queue lock before it drops the memory. The submit path never takes the queue lock, so the lock order stays vdev_lock -> ref_lock and queue_mutex -> ref_lock, and retire never waits on vdev_lock. The watchdog bounds the wait. The submit worker defers a job whose destination is still pinned by a frame in flight. The retirement that drops the pin restarts it, so nothing waits under queue_mutex. Drain completes the request controls of a deferred job that it ends with an error. A held H.264 picture is dropped when CAPTURE stops (a new cap_stop codec hook, called under queue_mutex after the drain), and when a continuation slice does not find the destination that m2m still holds, in the same buffer and memory generation as the first slice. HEVC takes the colocated MV offset from vb2_plane_size() instead of the V4L2 plane length, which QBUF overwrites before validation. HEVC, VP9 and AV1 go through the same lookup. A reference whose memory was dropped is now missing rather than rejected. Signed-off-by: vlad <tulituvlad@gmail.com>
__prepare_dmabuf() checks each new fd and plane length before it touches vb->planes. When one of those checks failed, the error path still fell through to __vb2_buf_dmabuf_put() and released the buffer's current attachment, without calling buf_cleanup first. A driver that still reads that memory (for example a decoder using the buffer as a reference picture) was never told it went away, and the buffer kept its copied timestamp, so a later prepare could give new memory the identity of the old picture. Drop only the new DMA-BUF references on a validation failure and keep the existing attachment, as nothing about it changed. Signed-off-by: vlad <tulituvlad@gmail.com>
__prepare_dmabuf() takes a reference on the DMA-BUF of every plane before it attaches any of them. When attaching or mapping a plane fails, the error path releases only the planes already moved into vb->planes, so the references of the planes that follow are leaked. Clear each reference once it moves into vb->planes and put the ones that are left on the error path. Signed-off-by: vlad <tulituvlad@gmail.com>
A client may queue a CAPTURE buffer as the destination of a new picture while earlier pictures still read it as a reference. PP runs in submission order, so the pixels are safe, but the motion field and segmentation map behind them are read by DMA and written by the new picture's prepare and VP without that order. Record which references each bank reads the tail of and hold a picture in job_ready while a busy bank still reads its destination. Report the guarantee through a read-only V4L2_CID_APPLE_AVD_AV1_REF_REUSE control so userspace can drop its own wait on earlier pictures before reusing a reference buffer. Two CAPTURE indices may import one DMA-BUF and so share one tail, so a busy bank whose tail reference has the destination's plane 0 DMA-BUF counts as busy too. The guarantee holds only while the buffer keeps the same memory; a requeue with another DMA-BUF still needs a wait. Signed-off-by: vlad <tulituvlad@gmail.com>
A frame with use_ref_frame_mvs waited for every named reference to be retired by PP before it could start. The reference motion field is written by the VP stage, so it is complete once that reference reports VP_DONE, the same point the named reference wait already uses. Waiting for PP retirement only serialised VP of a frame behind PP of the frame before it, and almost every frame references its predecessor. Drop the extra PP wait. Segmentation maps keep their PP barrier, and the CAPTURE tail reuse check keeps a buffer from being rewritten while an earlier frame still reads its motion field. Full 8K AV1 clip, linear P010: framemd5 matches libdav1d on all 570 frames in 3 runs, with about 145 of 588 pictures running VP while the previous PP is still active. Signed-off-by: vlad <tulituvlad@gmail.com>
The submit worker defers a job whose CAPTURE destination is still pinned as a reference by a frame in flight, and restarts it only after that frame is retired by PP. With AV1 banks the driver already orders that reuse, so the deferral only serialised VP of the next picture behind PP of the frame before it. AV1 8K AX03 decode drops from 108.99 to 100.41 fps with the deferral in place. Add a dst_reuse_ordered codec hook and skip the deferral when it holds. AV1 sets it when the stream has two or more banks and no busy bank reads the destination's tail. H.264, HEVC, VP9 and single bank AV1 have no hook or return false and keep the pin wait unchanged. Each hazard and what covers it: - Pixels earlier frames read: the new picture's PP writes them after theirs, as the single PP engine runs in submission order. - Motion field and segmentation map that a busy bank reads by DMA: avd_av1_ready() holds the picture while avd_av1_tail_busy() is true, and the hook checks it again for a retried job, falling back to the pin wait. - CPU writes at prepare to the destination's tail (CDFs in avd_av1_prepare_prob() and avd_av1_set_prob(), the map in avd_av1_prepare_seg() and avd_av1_set_seg()): the map region is read by DMA only by banks that noted the buffer as a tail reference, so the tail check covers it. The CDF tail is read only by the CPU while a later picture is prepared, never by a frame in flight. A frame size no larger than the CAPTURE format keeps the tail above the pixels. - Timestamp, layout and AV1 buffer info rewritten at prepare: frames in flight resolved and read them while they were built, each timestamp once. - Memory lifetime: pins are still taken and dropped as before, and avd_buf_cleanup() still withdraws the buffer, changes its generation and waits for the pins. - Changed memory or layout: the deferral is skipped only while the buffer is published, its compression layout equals the context's, and every pinning frame found its current generation; otherwise the job still waits. Signed-off-by: vlad <tulituvlad@gmail.com>
Slice-based H.264 collects the slices of a picture in a bank and sends the picture to the decoder only with a slice that clears V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF. A slice with HOLD ends without a submit and m2m keeps the CAPTURE buffer held. When userspace then sends V4L2_DEC_CMD_FLUSH with no OUTPUT buffer queued, the generic stateless helper returns the held buffer as DONE. That buffer was never decoded: userspace takes stale memory for the last frame, and later frames that reference its timestamp read the same stale memory. The bank also stays busy with its slices, and its marker stays on the returned buffer, until a later picture or CAPTURE STREAMOFF cleans them up. Decoding the held picture on FLUSH does not fit the m2m model: m2m dispatches a job only for a queued OUTPUT buffer, and the OUTPUT buffer of the HOLD slice and its request are already complete. Sending the picture from the ioctl would bypass the m2m job serialization and need a frame slot without an OUTPUT buffer, which retirement does not support. So wrap vidioc_decoder_cmd. When the codec provides flush_held, no OUTPUT buffer is queued and the CAPTURE buffer at the head of the queue is held, drop the held picture (slices, bank busy flag, unsubmitted job table), clear the bank marker of the buffer even when OUTPUT STREAMOFF already freed the banks, withdraw the buffer from the published references, and return it as ERROR, so userspace sees V4L2_BUF_FLAG_ERROR and no later frame reads it as a reference. The held buffer is the head of the queue, the one the HOLD slice decoded into, not the last one queued. A held H.264 buffer never holds a decoded picture: frame-based mode rejects HOLD, and a rejected slice already dropped its picture. Every other case, and every other codec, still goes through the generic helper. The wrapper runs under vdev_lock like every ioctl and request queue, so no OUTPUT buffer can arrive. It takes queue_mutex, which device_run preparation, retirement and cap_stop() hold while they change held buffers and bank state, so no job of this context can touch the bank while it is dropped. The slices are freed under that mutex, outside any spinlock. Signed-off-by: vlad <tulituvlad@gmail.com>
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
|
Hardware verification at exact head Setup
Results
|
The AV1 preamble now calls avd_check_ix_grid() and avd_av1_set_seg() calls avd_av1_note_tail_ref(). The harness extracts both callers but neither helper, so it no longer compiled. Stub them: the cases here use linear CAPTURE formats and check the tail's layout and contents, not the reuse order. Signed-off-by: DjDeveloperr <hey@djdev.me>
DjDeveloperr
left a comment
There was a problem hiding this comment.
MERGE once the rows below pass. The code reads right and I found no blocker.
Pushed: 55549b61 stubs avd_check_ix_grid() and avd_av1_note_tail_ref() in tools/asahi/avd-av1-tail-tests.py, which no longer compiled against this branch. Nothing else changed.
Worth knowing
avd-v4l2.c:41,avd-drv.c:101: AX03 is offered on every open-firmware AVD, but the 1280/640-byte 10-bit tile size is only measured on J714s. If another generation writes the 2048/768 bytes that the linear reservation allows for, the Y data would run into the regions behind it. It is opt-in and NV12/P010 stay first, so nothing that works today breaks, but it needs a second-generation row. If that can't be had, a flag in the variant data for AX03 would do. AX12 has the same tile sizes as before.avd-v4l2.c:1126: the wait for pins is uninterruptible and holdsvdev_lock, which all AVD clients share. One client that swaps the DMA-BUF of a buffer still in use stalls everyone's ioctls until that frame retires: milliseconds normally, up to the 2 s watchdog after a hang. Still better than unmapping under DMA.avd-queue.c:409: a job deferred for a pinned destination keeps the single m2m job, so other streams wait behind it. H.264, HEVC and VP9 now defer where they didn't before, and neither that nor two streams at once has been timed.- Follow-up, not for this PR: the two formats and the control have no text under
Documentation/userspace-api/media/v4l.
videobuf2: both commits are right for other drivers. A QBUF that fails validation used to drop the old attachment without buf_cleanup; now it stays, and the next QBUF of the same fd is simply "not reacquired". On attach, map or buf_init failure every new reference is put exactly once, including duplicated planes.
Also read: pin and generation lifetime (put on retire, error, hold and recovery; a deferred job is always restarted because retirement runs the worker), vdev_lock → queue_mutex → ref_lock order, the 16-entry reference table against H.264/HEVC/AV1/VP9 (no way to overflow it from userspace that I could find), the FLUSH wrapper, and the MV, CDF and segmentation tails behind Interchange images.
Checks (merge of 2232c9cd into main 53732c47; it still merges cleanly into b9029378)
- W=1, LLVM arm64:
drivers/media/platform/apple/avd/,videobuf2/,v4l2-mem2mem,v4l2-ioctl,v4l2-ctrls-core, 0 warnings. - KUnit on UML (avd suites): 228/229.
avd_pm_open_boot_replay_testis the known UML-only failure on main. - AVD fixtures (
program,dispatch,recovery,av1,vp9,queue-host): all pass.tools/asahi:avd-av1-tail12/12 with the pushed fix,avd-codec-tail2/2,avd-fifo-size,avd-pp-countandapple-scaler-recoverypass. Their old-revision controls were not run. - checkpatch --strict on the 8 commits: clean.
- Not run: the full host-test suite and KUnit on arm64. The fork's hosted runs at this head still wait for approval.
Rows owed at the exact head: J714s (AX03 8K AV1, AX12, the H.264 FLUSH-after-HOLD case; the PR's own J714s runs used kernels with a local stopgap), and a second generation, J773g preferred over J700 because it has three AV1 engines: AX03 and NV12/P010 for H.264, HEVC, VP9 and AV1 against software, two streams at once with fps against main, clean DART and kernel log. satchlj's J775c row at 2232c9cd still counts for the kernel code, since the pushed commit only touches a host test. It did not exercise AX03. J414s and J516s are down right now.
There was a problem hiding this comment.
Verdict: MERGE at 55549b6
Checked: The current delta from 2232c9cdefb5 is confined to four lines in the existing AV1 host fixture. I ran it at this head and all 1,804 production controls passed. In the driver, validation releases only staged DMA-BUF references, attach unwind separates staged and acquired references, and reference pins survive until the frame gives up DMA. The H.264 FLUSH path withdraws the held picture and completes its buffer as ERROR. The preceding J775c row exercised byte-identical driver code.
Follow-ups: Please keep the generic videobuf2 reference-ownership fix separate from the larger AVD feature series in future submissions.
What: Apple AVD fixes for J714s (T6050), in 8 commits:
The H.264 OUTPUT completion order bug (GStreamer v4l2slh264dec gets QBUF EINVAL on multi-slice clips) is not in this PR; #3562 fixes it. Our J714s test kernel carries a local stopgap until #3562 lands.
Tested:
Behavior change (commit 8): On V4L2_DEC_CMD_FLUSH after a final slice that carried V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF, avd now drops that picture. The held CAPTURE buffer comes back with V4L2_BUF_FLAG_ERROR and bytesused 0, where the core helper returns it as DONE. The documentation of V4L2_DEC_CMD_FLUSH expects the held buffer to contain the last decoded frame. That is true for decoders that decode each slice as it arrives. AVD sends a picture to the hardware only with its last slice (no HOLD), so a held buffer has no decoded picture yet; returning it as DONE gives userspace stale memory as the last frame and leaves the slice bank busy. Userspace loses that last picture. Decoding the held slices at FLUSH is not done: HOLD means more slices of the picture follow, so the bank can hold only part of a picture, and starting a hardware job from the ioctl would bypass the m2m job, slot and retire paths. Libva v4l2_request sends the last slice of every picture without HOLD, so it does not reach this path in normal decode.
Risk: Two-way door. Affects the Apple AVD driver and two videobuf2 DMA-BUF error paths.
Not yet: Only J714s was tested. Other Macs were not run, and HEVC and VP9 were checked only on the desk.