Repository navigation
dmaengine: apple-sio: hold tags and ended transfers until the firmware is done with them - #3195
Conversation
…tags A reply of the SIO firmware names a tag, an endpoint and its type. sio_recv_msg() only checked that the tag was outstanding. An ACK or NACK naming another channel than the one its tag's ISSUE went to ran the ISSUE callback on that other channel. In the old accounting this took one of that channel's pending ISSUEs, and could advance its current transfer when the DMA cookies matched, which is common as each channel numbers its own. In the ring accounting it cleared that channel's issuing state and advanced its last issued descriptor. The tag was then freed, so the real ACK of the first channel was dropped. A STARTED naming any outstanding tag was taken as its ACK, and set the protocol number. sio_call() also freed the tag of a call as soon as its 100 ms wait timed out. The tag on the wire has no generation and the allocator hands out tags 1 to 15 in turn, so the 15th request after that took the tag again, and a late reply to the timed-out call was taken for its reply: a CONFIGURE or TERMINATE answered by another request's ACK or NACK, or an ISSUE callback run for it. Bind each tag, when it is handed out, to the endpoint byte of its request and to the reply it takes: STARTED for a START, an ACK for any other request, a NACK for any. A reply that does not answer the request of its tag is dropped with a rate-limited warning, and the tag stays with its request. A call that times out keeps its tag until its late reply comes, which then frees the tag and answers nothing else, or until the firmware is reset (a refused sleep restarts it). A reply that comes after the timeout, before the call gives up, still answers the call. While half the tags are kept this way, calls are refused with -EBUSY, so that the ISSUEs of running channels still get tags. A spinlock orders a reply against the allocation and release of its tag, and an ISSUE callback runs after its tag is released, unlocked, as it may issue again. A request whose send failed gives back its tag only if a reply did not free it and hand it to another request in the meantime. Firmware that answers each request on its own endpoint, in time, sees no change. Add KUnit cases: a reply naming another endpoint, or of the wrong type, is dropped and its tag kept; another channel's ACK leaves a channel's old and ring ISSUE accounting alone, with the same DMA cookie on both; a timed-out call keeps its tag through two rounds of the tag space, and its late reply answers neither a call nor an ISSUE on the same endpoint; a reply after the timeout but before the give-up answers the call; calls are refused once half the tags are kept while ISSUEs get the rest, until a late reply or a reset frees them; and a failed send leaves a tag that went to another request alone. Fixes: 5fdca02 ("dmaengine: apple-sio: Add Apple SIO driver") Signed-off-by: Dj <[email protected]>
The SIO suites (apple-sio-msg, apple-sio-boot, apple-sio-probe, apple-sio-t6030 and apple-sio-t6041) had no kunitconfig, and the Apple build workflow neither compiled drivers/dma nor ran on SIO changes, so no check ran them. Add drivers/dma/apple-sio.kunitconfig, with spinlock debugging and lockdep for the tag lock, and list it with the SIO files in MAINTAINERS. Run it in the KUnit matrix, compile the SIO driver in the Apple build job, check that the Mac configuration still builds it, and start the workflow on SIO changes. Signed-off-by: Dj <[email protected]>
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
PROVE_LOCKING and DEBUG_SPINLOCK depend on DEBUG_KERNEL, which the KUnit arm64 configuration does not set, so kunit.py stopped at the configuration with both options missing. Set it, as the ISP kunitconfig does. Signed-off-by: Dj <[email protected]>
…pped sio_terminate_work() sent TERMINATE, took a NACK as success and only logged a timeout, waited 500 ms for the transfer's descriptors, and then freed the transfer whatever came of it. sio_synchronize() returned after that, so the client could free its buffer while the firmware still held descriptors pointing at it, and another transfer could take those descriptor slots. Free an ended transfer only once the firmware no longer reads its descriptors: it acknowledged a TERMINATE, after which nothing it holds is played (as sio_stop_quiet() takes it), or it reported all of them. A TERMINATE refused or not answered while descriptors are still in flight is sent again after 500 ms, twice in all. If the channel is still not stopped, the transfer stays its current one and keeps its descriptors. Nothing else is issued on the channel, which keeps refusing a system sleep as busy, and each sio_synchronize() tries to stop it again. A transfer with nothing in flight is freed whatever TERMINATE answers. In the old accounting, an ISSUE acknowledged after the transfer ended was not counted, so its descriptor looked complete: count it until its report. A report no longer calls the client back for a transfer it ended, as in the ring accounting, since a held transfer can outlive its client. Firmware that acknowledges TERMINATE sees no change, except that the client is not called back for a transfer it ended. Add KUnit cases, with TERMINATE stubbed: a TERMINATE refused or not answered with descriptors in flight keeps the transfer, its descriptor slots and the channel, and a queued transfer does not start, until an acknowledged TERMINATE frees it; with nothing in flight it is freed anyway; and in the old accounting an ISSUE acknowledged after the end is in flight until its report, with no client callback. Fixes: 5fdca02 ("dmaengine: apple-sio: Add Apple SIO driver") Signed-off-by: Dj <[email protected]>
|
fb1af1e J473 (M2 Mac mini, T8112, batched 0-diff merge onto main) PASS: 200/200 pre-sleep + 1/1 rtc1 s2idle + 20/20 post-sleep silent /dev/zero PCM cycles on external DisplayPort card 8 (0 SIO timeout/terminate/refuse errors vs 0 baseline, 4 apple-sio lines vs 4 baseline), Enforcing, 0 failed, 0 AVC, 0 new audio/SIO/DMA kernel errors; fleet89 restored. |
|
Verdict at Blockers: none. My last blocker (the bounded window letting a stream's close free a buffer the firmware still reads) is fixed.
Checked
Evidence
|
|
J516s (MacBook Pro 16" M3 Pro, 27.2 stub) row for |
|
J714s (MacBook Pro 14" M5 Pro, T6050) final row at
|
When the firmware refused or did not answer TERMINATE with descriptors still in flight, sio_synchronize() returned anyway. The transfer was held with its descriptor slots, but the client then freed its buffer, which the firmware could still read. In the ring accounting, transfers retired with descriptors that did not complete, on a channel that did not stop, were left the same way after a WARN. sio_synchronize() now returns only once the firmware no longer reads those buffers. A held transfer is freed after an acknowledged TERMINATE, once its descriptors were reported, or after a firmware crash, from which nothing more is issued. Retired transfers are freed once their descriptors completed or the channel stopped. Until then, TERMINATE is sent again every round, and a channel still not stopped is reported every 30 rounds. sio_terminate_work() keeps its two bounded tries, as it runs on the system workqueue. A held channel no longer outlives sio_synchronize(), so it refuses a system sleep only while its client waits for it to stop. Firmware that acknowledges TERMINATE sees no change. Add KUnit cases: synchronizing waits through refused TERMINATEs until one is acknowledged; a held transfer is freed once its descriptors are reported or the firmware crashed; and in the ring accounting retired transfers are kept through refusals and freed once the channel stops. Fixes: 5fdca02 ("dmaengine: apple-sio: Add Apple SIO driver") Signed-off-by: DjDeveloperr <[email protected]>
…uffers Synchronizing now waits until the channel stops, but it also runs where no buffer is freed next: in a system sleep's prepare, where the DisplayPort audio stream is only suspended, and when a display is unplugged or its processor recovers, where the DCP audio driver holds its link lock. With firmware that does not stop the channel, the sleep hung where it used to be refused, and the unplug would hang with that lock held. Bound it there. From a system sleep's prepare to its end, as the PM core notifies them, sio_synchronize() stops the channel once more at most, and a transfer still not stopped stays held, so sio_suspend_late() refuses the sleep as before. apple_sio_synchronize_bounded() does the same, for a caller that keeps the buffers, and says whether the channel still holds transfers. A synchronize that waits until the channel stops may then run concurrently with its client's bounded calls. Synchronizing and setting or removing the idle transfer now take a lock of the channel. A bounded caller waits for it as long as synchronizing a channel that stops takes, and fails with -EBUSY, leaving the channel as it is, while a synchronize waits for a channel that does not stop. Firmware that stops the channel when asked sees no change. Add KUnit cases: a bounded synchronize stops the channel once and keeps a held or retired transfer, the channel busy; a bounded one that finds the lock held fails after that time, touching nothing; and the sleep notifier bounds synchronizing from the prepare to the end. Fixes: 5fdca02 ("dmaengine: apple-sio: Add Apple SIO driver") Signed-off-by: DjDeveloperr <[email protected]>
…lock dcpaud_release_link() and sync_stop's handover to the held link's silence synchronize the SIO channel with the link lock held. The display's unplug and its processor's recovery take that lock, and dcpaud_release_link() also runs in a system sleep's prepare. Neither frees the stream's buffer. Synchronize there with apple_sio_synchronize_bounded(): a DMA the firmware did not stop stays with the channel, and the link is torn down as before. sync_stop, after which the buffer may be freed, then waits for such a DMA with dmaengine_synchronize(), without the link lock. DRM_APPLE depends on APPLE_SIO || !APPLE_SIO, as for the DART driver: the native audio links call into the SIO driver. Signed-off-by: DjDeveloperr <[email protected]>
|
3525449 J473 (M2 Mac mini, T8112, exact-head 0-diff merge onto main) PASS: 200/200 pre-sleep (51.40 s, max 257 ms/cycle vs 50.64 s stock, no stop/close stall) + 1/1 rtc1 s2idle + 20/20 post-sleep (5.14 s, max 257 ms/cycle) silent /dev/zero PCM cycles on external DisplayPort card 8; 0 SIO error patterns (vs 0 baseline), 0 TERMINATE retry log lines, 4 apple-sio lines (vs 4 baseline), Enforcing, 0 failed, 0 AVC, 0 new SIO/DMA/audio kernel errors; fleet89 restored. |
Synchronizing is bounded during a system sleep, where streams are only suspended and keep their buffers. The flag for that came from the PM notifier, which runs before tasks are frozen and after they are thawed. A player closing its stream in those windows got the bounded synchronize, and could free its buffer while the firmware still held descriptors pointing at it. Set the flag from the SIO's own prepare and clear it from its complete instead. Both run with tasks frozen, and the SIO's prepare runs before those of its DMA clients, which its device links order after it. Anywhere else, a synchronize waits until the firmware stops the channel. Replace the notifier's KUnit case: a synchronize before the SIO's prepare and after its complete waits through refused TERMINATEs until one is acknowledged, and only in between is it bounded, leaving the transfer held. Signed-off-by: DjDeveloperr <[email protected]>
|
J700 (MacBook Neo, DP audio through SIO) at
Earlier head
|
|
8e07966 J616s (M4 Pro, T6040, exact head, default cmdline) PASS: apple-sio RTKit v12 / SIO protocol v10 handshake matches stock; 200/200 pre-sleep (115.20 s, max 577 ms/cycle vs 577 ms stock, zero stop/close stall) + 1/1 rtc1 s2idle + 20/20 post-sleep (11.56 s, max 611 ms/cycle) silent /dev/zero S24_LE cycles on external USB-C DisplayPort card 9; 0 SIO/DCP-audio errors, 0 TERMINATE retries, 0 -EBUSY sleep refusals, Enforcing, 0 failed, 0 AVC, stock fleet89 restored. (Note: checked lab J713 T8132 — dma-controller@3ade00000 is status=disabled on its current Stage 2 as well.) |
|
8e07966 J473 (M2 Mac mini, T8112, exact head, default cmdline) PASS: |
There was a problem hiding this comment.
Verdict: FINISH, then merge at 8e079667
Blockers:
- Merge conflict (
CONFLICTING) againstmain; rebase onto currentmain(or mergemain) and re-run CI (4 checks were cancelled at8e079667).
Checked:
- Verified mergeability (
CONFLICTING) and existingJ473M2 PASS row at8e079667. - Deferred final diff approval until the conflict against
mainis resolved and CI finishes green.
Evidence
- Head
8e079667vsmain:CONFLICTING; CI 29 passed, 4 cancelled.
Forward-merge main. Only apple-build-kunit.yml conflicted: main added APPLE_CENTAURI_T6050_WIFI to the required-options loop and this branch added APPLE_SIO there, so the loop keeps both. Signed-off-by: Ryan Murray <[email protected]>
|
Forward-merged main into this branch (merge commit fb303ec, no force push). The only conflict was the required-options loop in apple-build-kunit.yml: main added |
There was a problem hiding this comment.
Verdict: MERGE at fb303ec26989
Blockers: None (wait for queued CI, including KUnit apple-sio, to finish green).
Checked:
- Forward merge
fb303ec26989ofmain(MERGEABLE):.github/workflows/apple-build-kunit.ymlcleanly retains bothAPPLE_SIO=mandAPPLE_CENTAURI_T6050_WIFI=yand registers theapple-sioKUnit suite (CONFIG_PROVE_LOCKING=y);apple-sio.cpreservesmain'sapple,t8132-siomatch andaudio.cpreservesmain's T6030 replaced-instance changes. - Full driver audit of
drivers/dma/apple-sio.c,drivers/gpu/drm/apple/audio.c,drivers/gpu/drm/apple/Kconfig, andinclude/linux/dma/apple-sio.h:- Tag binding and stale-tag quarantine (
sio_alloc_tag,sio_cancel_tag,sio_recv_reply,sio_call_result,sio_release_stale):tags->lockserializes tag allocation, reply matching (endpoint +MSG_STARTED/MSG_ACK/MSG_NACKcheck), ABA-safe cancellation viaheld_by[tag] == generation, timeout-vs-reply race closure viatry_wait_for_completion(),SIO_MAX_STALEreservation for atomicISSUEs, and stale-tag release on firmware reset. - Quarantined-transfer teardown and bounded/unbounded synchronize (
sio_stop_tx,sio_end_tx,sio_stop_held,sio_wait_retired,sio_sync,sio_sync_lock,sio_pm_prepare/sio_pm_complete): prevents freeing DMA buffers still read by firmware while keepingsio_synchronize()bounded during frozen system sleep and underdcpaud->link_lock(dcp_pcm_native_sync_stop()drops&dcpaud->link_lockbefore falling back to unboundeddmaengine_synchronize()). DRM_APPLEdepends on APPLE_SIO || !APPLE_SIOprevents modular-link mismatch whenDRM_APPLE=yandAPPLE_SIO=m.
- Tag binding and stale-tag quarantine (
- Existing hardware rows (
J473M2 200 pre-sleep +s2idle+ 20 post-sleep + unplug/replug, plusJ616s/J700/J714s) stand with unchanged driver logic;apple-sio.oandaudio.obuild clean for arm64 (LLVM=1).
Evidence
- Verified
sio_recv_reply()releasestags->lockbefore invokingack_callback(&sio->channels[sioep], ...)so callbacks can allocate new tags without lock recursion, andsioep == tags->ep[tag]guaranteessioep < sio->nchannelsfor atomic channel requests. - Verified
sio_sync_lock()/sio_sync_unlock()wake_up(&siochan->sync_wait)avoids lost wakeups across bounded and unbounded callers. - 28 CI checks at
fb303ec26989are queued at review time (0failures); approve now so merge can proceed once CI goes green.
|
code-owner ping @DjDeveloperr: closeout merge at |
|
Merge-train review (non-author) at |
|
Merging under the 6-point rule (Ryan 09:29, permanent), at |
main ended SIO transfers differently (#3195): an ended transfer is freed only once the firmware no longer reads its descriptors (sio_end_tx(), sio_release_tx()), and is held with its channel otherwise; synchronize waits for that (sio_sync()), bounded only from the SIO's prepare to its complete, under a per-channel lock. Carry the runtime PM of this branch onto that lifecycle: - sio_terminate_work() resumes the SIO around sio_end_tx() and hands its reference to a next transfer started without one. If the resume fails, sio_end_tx() still frees the transfer only once it is known stopped. - sio_release_tx(), where an ended or held transfer is finally freed, drops the channel's transfer reference with its last transfer, and aborts issued descriptors when there is no firmware to start them on (removal, or stopped), as the old terminate path did. - sio_stop_quiet() keeps main's result and gains the runtime PM hold and the transfer reference release. - sio_sync() flushes the deferred issue worker before ending anything, and kills the tasklet before returning: descriptors aborted with no transfer to end complete through it, and dmaengine_synchronize() must not return before their callbacks have run. - A held transfer stays the channel's current one, so it keeps the channel busy and its runtime PM reference, and the firmware up. Tests: initialise the per-channel sync lock in the runtime PM KUnit fixture; give main's tag fixture an issue worker and a runtime-active, held device as probe leaves it; extend the sio-rpm-issue host model with main's held-transfer code and the fields it uses. Signed-off-by: Erik Bourget <[email protected]>
What: Three fixes to how
apple-sioholds its own state until the firmware has answered for it:sio_synchronize()returns only once the firmware no longer reads the client's buffer. Callers that keep the buffer (a system sleep's prepare, a display unplug under the DCP audio link lock) get a bounded wait instead, and the sleep is refused as before.The SIO KUnit suites also run in CI now.
Tested:
KUnit on arm64 QEMU with lockdep, at 8e07966:
W=1 and checkpatch --strict are clean.
W=1 and checkpatch --strict are clean.
Risk: two-way door · every Mac with an SIO (DisplayPort/HDMI audio DMA): M1, M2, M3 Pro, M4, M4 Pro/Max, M5, Neo. The DCP audio change affects native audio links (M3 Pro, M4 Pro/Max, Neo).
Not yet: rows at 8e07966:
Open by design (see Details): a firmware that never stops the channel blocks the stream's close, and a crashed firmware is taken as stopped once its descriptors had 500 ms to drain.
Details
Tags: the problem. A firmware reply names a tag, an endpoint byte and a type.
sio_recv_msg()checked only that the tag was outstanding:issuingstate and advanced its last issued descriptor.sio_call()freed a call's tag as soon as its 100 ms wait timed out. Tags on the wire are 1 to 15, handed out in turn, with no generation. So the 15th request after a timeout got that tag again, and a late reply to the timed-out call was taken as the new request's reply.Tags: the change.
sio_alloc_tag()binds the tag to the request's endpoint byte, and to whether it's a START. For an ISSUE it also stores the callback and cookie. This happens under a newtags.lock, before the message is sent.sio_recv_reply()matches each reply under that lock. A reply answers its tag only if:Anything else is dropped with a rate-limited warning, and the tag stays outstanding.
try_wait_for_completion()under the lock).-EBUSY. ISSUEs from running channels still get the other 8.vc.lock→tags.lock, andtags.lockis never held while taking a channel lock.Ended transfers: the problem.
sio_terminate_work()sent TERMINATE, took a NACK as success and only logged a timeout. It waited 500 ms for the descriptors and then freed the transfer whatever came of it.sio_synchronize()returned after that, so the client could free its buffer while the firmware still held descriptors pointing at it, and another transfer could take those descriptor slots.Ended transfers: the change.
A transfer is freed only once the firmware no longer reads its descriptors, which means one of:
sio_stop_quiet()already uses);apple_rtkit_is_crashed()), so it issues nothing more, and the descriptors had 500 ms to drain.sio_terminate_work(), which runs on the system workqueue, tries TERMINATE twice, each followed by a 500 ms drain wait. If the channel still isn't stopped, the transfer stays as the channel's current one, keeps its descriptor slots, and nothing else is issued on that channel.sio_synchronize()doesn't return while a transfer is held. It sends TERMINATE again every round until one of the stops above happens, and logs a still-stopped channel on the first round and then every 30 rounds. So the client can't free a buffer the firmware may still read, and a held channel no longer outlives synchronize. That means it refuses a system sleep only while its client is waiting for it.The ring accounting gets the same treatment. Transfers retired by the handover to the idle transfer are kept until their descriptors complete or the channel stops:
sio_synchronize()repeatssio_stop_quiet()instead of returning after adev_WARN_ONCE.A transfer with nothing in flight is freed whatever TERMINATE answers.
In the old accounting, an ISSUE ACKed after the transfer ended is now counted (
nlate) until its report.A report no longer calls the client back for a transfer it ended, as in the ring accounting.
Bounded where nothing is freed. Synchronizing also runs where the buffer stays allocated:
There it's bounded, as it was before this PR:
.prepareto its.complete,sio_synchronize()stops the channel once more at most. A transfer still not stopped stays held, sosio_suspend_late()refuses the sleep. Both callbacks run with tasks frozen, and the SIO's prepare runs before its DMA clients', which its device links order after it. So a stream closed at any other time always gets the unbounded wait.apple_sio_synchronize_bounded()(new, ininclude/linux/dma/apple-sio.h) does the same for a caller that keeps the buffers, and returns-EBUSYwhile transfers are still held. The DCP audio driver uses it under the link lock indcpaud_release_link()and insync_stop's handover to the held link's silence. Thatsync_stopthen waits for a DMA that didn't stop withdmaengine_synchronize(), after dropping the link lock and before the buffer can be freed.Concurrency. A synchronize that waits until the channel stops can now run while its client's bounded calls run. Synchronizing and setting or removing the idle transfer take a per-channel
sync_lock. A bounded caller waits for it as long as synchronizing a channel that stops takes, then fails with-EBUSYand touches nothing.Kconfig.
DRM_APPLEdepends onAPPLE_SIO || !APPLE_SIO, as it already does for the DART.What stays open:
hw_free) wait until it stops. That's the safe side: the buffer stays allocated, and sleep and unplug aren't held up. Resetting the SIO at runtime would also stop every other SIO channel and needs the service set up again, so it isn't done here.Firmware that answers each request on its own endpoint, in time, and ACKs TERMINATE sees no change. The one exception is that the client isn't called back for a transfer it has already ended.
CI. No check ran the SIO KUnit suites before. This adds
drivers/dma/apple-sio.kunitconfig(with spinlock debugging and lockdep) to the Apple build and KUnit workflow, buildsdrivers/dma/apple-sio.oin the Apple build job, and starts the workflow on SIO changes.Evidence
kunit.py run --arch=arm64 --make_options LLVM=1 --kunitconfig=drivers/dma/apple-sio.kunitconfigat 8e07966 (44 run, 43 passed, 1 skipped):sio_sleep_bounds_synchronize_test)PROVE_LOCKING and DEBUG_SPINLOCK are on, with no lockdep report.
Negative controls:
sio_stop_tx()reporting a stop on a refused TERMINATE, and the late-ISSUE count dropped,sio_terminate_unconfirmed_testandsio_terminate_late_issue_testfail;sio_stop_held()andsio_wait_retired()giving up after one round (the earlier behaviour),sio_terminate_unconfirmed_testandsio_terminate_ring_retired_testfail;sio_sync_bounded_testfails;sio_pm_prepare()not setting the flag,sio_sleep_bounds_synchronize_testfails.kunit.py run --kunitconfig=drivers/gpu/drm/apple/tests/display.kunitconfig --kconfig_add CONFIG_SOUND=y CONFIG_SND=y CONFIG_DMADEVICES=y CONFIG_APPLE_SIO=y CONFIG_DRM_APPLE_AUDIO=y CONFIG_DRM_APPLE_DCP_T6030_AUDIO=yat 3c3a4bd: 234 run, 233 passed, 1 skipped. This includes apple-dcpaud-release-link 22/22, apple-av-native-audio 18/18 and apple-dcpaud-legacy 5/5.make W=1 drivers/dma/apple-sio.o drivers/gpu/drm/apple/audio.o: no warnings. checkpatch --strict: all eight commits clean.The first CI run stopped at kunit.py's configuration step, because PROVE_LOCKING needs DEBUG_KERNEL. 63b7f02 sets it.
CI
KUnit apple-sioat fb1af1e (job 139759732253): 39 run, 38 passed, 1 skipped, apple-sio-terminate 3/3.Thanks to erikwb for reporting these.