Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 171 additions & 6 deletions sound/soc/apple/aop_spkr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ pub(crate) struct Service {
pwrd_param: u32,
/// The speaker service's sense ring; `None` for the jack's services.
ring: Option<SenseRing>,
/// The speaker playback channel a stop left running on silence (see `defer_tx_stop`), until
/// the serializer is down to `pw1 `.
deferred_tx: AtomicPtr<bindings::dma_chan>,
/// The speaker playback channel was started (a START succeeded) and not stopped since.
tx_started: AtomicBool,
#[pin]
state: Mutex<ServiceState>,
}
Expand All @@ -166,6 +171,8 @@ impl Service {
tag,
pwrd_param,
ring: ring.then(SenseRing::new),
deferred_tx: AtomicPtr::new(ptr::null_mut()),
tx_started: AtomicBool::new(false),
state <- new_mutex!(ServiceState {
attached: false,
users: 0,
Expand Down Expand Up @@ -241,6 +248,71 @@ impl Service {
Ok(())
}

/// Playback stop for the speakers, from the trigger (atomic): the firmware crashes when its
/// serializer runs (`pwrd`) with the playback DMA stopped for a while
/// (aurora-silicon/linux#2188), and the serializer only goes down to `pw1 ` much later, after
/// the amps muted (or at the next prepare, after an xrun). So the channel keeps running, on
/// a buffer filled with silence, and `finish_tx_stop` stops it only after that `pw1 `: the
/// serializer never runs without host data. A stop just before the `pw1 ` is not enough:
/// the firmware still crashed when the `pw1 ` followed the DMA stop by about a millisecond.
/// The words the stopped serializer leaves in the channel's FIFO do not reach the next
/// stream: a channel start resets its FIFO (measured on J616C with the serializer down: a
/// channel restarted after such a stop takes in its full 256 bytes again, every time), so
/// the next start still meets an empty FIFO on a fresh serializer.
///
/// # Safety
///
/// `substream` is the live speaker playback front-end with a dmaengine runtime, under its
/// stream lock.
unsafe fn defer_tx_stop(&self, substream: *mut bindings::snd_pcm_substream) {
// SAFETY: by the caller's contract.
unsafe {
let rt = (*substream).runtime;
if !(*rt).dma_area.is_null() {
ptr::write_bytes((*rt).dma_area, 0, (*rt).dma_bytes);
}
fence(Ordering::SeqCst);
let chan = bindings::snd_dmaengine_pcm_get_chan(substream);
self.deferred_tx.store(chan, Ordering::Release);
}
}

/// Stops a playback channel `defer_tx_stop` left running. Process context, right after the
/// serializer went down to `pw1 ` (amps already muted).
fn finish_tx_stop(&self) {
let chan = self.deferred_tx.swap(ptr::null_mut(), Ordering::AcqRel);
if chan.is_null() {
return;
}
// SAFETY: the front-end's channel stays requested until its close, which finishes any
// deferred stop before it releases the channel.
unsafe {
let dma = (*chan).device;
if let Some(f) = (*dma).device_terminate_all {
f(chan);
}
if let Some(f) = (*dma).device_synchronize {
f(chan);
}
}
}

/// The front-end prepare, after the back-end's: a stop still deferred with the serializer
/// down ends here. The back-end's own prepare finishes it when the back-end is attached;
/// this covers a front-end whose back-end was stopped without it (a route change mid-stream),
/// whose next START would otherwise be refused.
fn finish_tx_stop_if_down(&self) {
let st = self.state.lock();
if !st.running {
self.finish_tx_stop();
}
}

/// Whether a stop of this channel is still deferred.
fn tx_stop_deferred(&self, chan: *mut bindings::dma_chan) -> bool {
!chan.is_null() && self.deferred_tx.load(Ordering::Acquire) == chan
}

/// A user of the service: the first one attaches it (once per firmware lifetime) and raises
/// `pw1 `.
fn get(&self) -> Result<()> {
Expand Down Expand Up @@ -304,6 +376,8 @@ impl Service {
}
st.running = false;
}
// A deferred playback channel stops once the serializer is down.
self.finish_tx_stop();
// The channel goes while the fabric is still powered.
if let Some(ring) = &self.ring {
ring.release();
Expand All @@ -317,9 +391,9 @@ impl Service {
///
/// The speaker service starts a fresh serializer every time: a running one goes down to
/// `pw1 ` first (the sense ring sees a gap of the two firmware round trips; the playback DMA
/// is stopped, as prepare only runs on a stopped stream), then the sense ring is armed and
/// `pwrd` starts the serializer on it. The playback channel starts after this, in the
/// trigger, from the FIFO the stopped stream drained into the running serializer.
/// is stopped, or still runs on silence after a stop and stops after the `pw1 `), then the
/// sense ring is armed and `pwrd` starts the serializer on it. The playback channel starts
/// after this, in the trigger, on a freshly reset FIFO.
/// `quiesce` runs before a running serializer stops: amps left unmuted (a re-prepare after a
/// resume or an xrun) must not see their clocks stop.
fn run(&self, quiesce: impl FnOnce()) -> Result<()> {
Expand All @@ -336,9 +410,12 @@ impl Service {
};
if st.running {
quiesce();
self.set_power(&mut st, STATE_PW1, 0)?;
let pw1 = self.set_power(&mut st, STATE_PW1, 0);
st.running = false;
self.finish_tx_stop();
pw1?;
}
self.finish_tx_stop();
self.start_with_ring(&mut st, ring)
}

Expand Down Expand Up @@ -369,6 +446,7 @@ impl Service {
}
st.running = false;
}
self.finish_tx_stop();
if let Some(ring) = &self.ring {
ring.stop();
}
Expand Down Expand Up @@ -765,7 +843,7 @@ const BE_DAI_ID_JACK: u32 = 11;
/// DAIs by table index.
const DT_DAI_JACK: u32 = 4;
/// Which service's power a front-end stream needs.
#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq, Eq)]
enum Svc {
Spkr,
Cout,
Expand Down Expand Up @@ -1108,7 +1186,10 @@ fn pcm_open_dmaengine(
substream,
bindings::SNDRV_DMA_TYPE_DEV as i32,
(*(*chan).device).dev,
0,
// Preallocated at the maximum, with no smaller fallback (max 0): a later
// hw_params never frees a buffer that a deferred stop's channel may still be
// reading.
PCM_BUFFER_MAX,
0,
)
};
Expand Down Expand Up @@ -1163,6 +1244,15 @@ unsafe extern "C" fn pcm_close(
svc.put();
return 0;
}
// A stop still deferred (no hw_free ran) ends before the channel is released, after the
// serializer is down.
// SAFETY: the dmaengine runtime was set up by `pcm_open`.
if stream.svc == Svc::Spkr
&& !stream.ring
&& svc.tx_stop_deferred(unsafe { bindings::snd_dmaengine_pcm_get_chan(substream) })
{
svc.stop();
}
// The channel goes first, with the fabric still powered by this front-end's reference. SAFETY:
// the dmaengine runtime was set up by `pcm_open`.
let ret = unsafe { bindings::snd_dmaengine_pcm_close_release_chan(substream) };
Expand Down Expand Up @@ -1208,6 +1298,26 @@ unsafe extern "C" fn pcm_hw_params(
}
}

/// The speaker service behind the speaker playback front-end, `None` for every other stream.
///
/// # Safety
///
/// As for `fe_stream`, and `component` must be this module's registered component.
unsafe fn speaker_tx<'a>(
component: *mut bindings::snd_soc_component,
substream: *mut bindings::snd_pcm_substream,
) -> Option<&'a Service> {
// SAFETY: by the caller's contract.
let (ctx, rtd) = unsafe { (context(component), rtd_of(substream)) };
// SAFETY: as above.
if unsafe { is_be(rtd) || !is_playback(substream) } {
return None;
}
// SAFETY: as above.
let (_, stream) = unsafe { fe_stream(rtd, substream) }?;
(stream.svc == Svc::Spkr && !stream.ring).then(|| ctx.service(Svc::Spkr))
}

/// # Safety
///
/// Called by ASoC on this module's registered component with live arguments.
Expand Down Expand Up @@ -1241,10 +1351,55 @@ unsafe extern "C" fn pcm_trigger(
}
return 0;
}
// The speakers' playback channel stops only once the serializer is down (`defer_tx_stop`).
// SAFETY: as above.
if let Some(svc) = unsafe { speaker_tx(component, substream) } {
match cmd as u32 {
bindings::SNDRV_PCM_TRIGGER_STOP | bindings::SNDRV_PCM_TRIGGER_SUSPEND => {
// Only a channel that runs is left running (a START refused further up the
// chain is undone with a STOP of a channel that never started).
if svc.tx_started.swap(false, Ordering::AcqRel) {
// SAFETY: the speaker playback front-end, under its stream lock.
unsafe { svc.defer_tx_stop(substream) };
return 0;
}
}
bindings::SNDRV_PCM_TRIGGER_START => {
// A start always follows a prepare, which finished any deferred stop (the
// back-end's, or `pcm_prepare` once the serializer is down).
// SAFETY: the dmaengine runtime was set up by `pcm_open`.
let chan = unsafe { bindings::snd_dmaengine_pcm_get_chan(substream) };
if svc.tx_stop_deferred(chan) {
return EBUSY.to_errno();
}
// SAFETY: the dmaengine runtime was set up by `pcm_open`.
let ret = unsafe { bindings::snd_dmaengine_pcm_trigger(substream, cmd) };
if ret == 0 {
svc.tx_started.store(true, Ordering::Release);
}
return ret;
}
_ => {}
}
}
// SAFETY: the dmaengine runtime was set up by `pcm_open`.
unsafe { bindings::snd_dmaengine_pcm_trigger(substream, cmd) }
}

/// # Safety
///
/// Called by ASoC on this module's registered component with live arguments.
unsafe extern "C" fn pcm_prepare(
component: *mut bindings::snd_soc_component,
substream: *mut bindings::snd_pcm_substream,
) -> i32 {
// SAFETY: as for `pcm_open`.
if let Some(svc) = unsafe { speaker_tx(component, substream) } {
svc.finish_tx_stop_if_down();
}
0
}

/// # Safety
///
/// Called by ASoC on this module's registered component with live arguments.
Expand All @@ -1256,6 +1411,15 @@ unsafe extern "C" fn pcm_sync_stop(
if unsafe { is_be(rtd_of(substream)) || sense_ring(component, substream).is_some() } {
return 0;
}
// A deferred stop is still running its channel: the back-end's prepare or hw_free ends it.
// SAFETY: as above.
if let Some(svc) = unsafe { speaker_tx(component, substream) } {
// SAFETY: the dmaengine runtime was set up by `pcm_open`.
let chan = unsafe { bindings::snd_dmaengine_pcm_get_chan(substream) };
if svc.tx_stop_deferred(chan) {
return 0;
}
}
// SAFETY: the dmaengine runtime was set up by `pcm_open`.
unsafe { bindings::snd_dmaengine_pcm_sync_stop(substream) }
}
Expand Down Expand Up @@ -1615,6 +1779,7 @@ fn component_driver(name: &'static CStr) -> bindings::snd_soc_component_driver {
open: Some(pcm_open),
close: Some(pcm_close),
hw_params: Some(pcm_hw_params),
prepare: Some(pcm_prepare),
trigger: Some(pcm_trigger),
sync_stop: Some(pcm_sync_stop),
pointer: Some(pcm_pointer),
Expand Down
Loading