From fa152b667f914f962f1d864c51d962df8123a123 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 12:46:11 +1100 Subject: [PATCH 1/2] dmaengine: apple-admac: Do not keep the client's peripheral config pointer admac_device_config() saves the whole dma_slave_config so that a channel can be set up again after a system sleep took its registers. The copy includes peripheral_config, a pointer to memory the client only guarantees for the duration of the call: the AOP audio driver passes the address of a local variable. admac_restore_chan() then calls admac_device_config() on the saved copy and reads the realign frame size through that pointer again, long after the client returned. Split the register programming out into admac_program_config(), which the restore calls on the saved copy. admac_device_config() takes the frame size from the client's config once (it was assigned twice) and stores the copy with peripheral_config cleared, so a restore keeps the frame size the client configured and never touches the client's memory. Add a KUnit case that configures a capture channel, overwrites the client's frame size, marks the channel lost and restores it. Fixes: d9b438c4ca41 ("arm64: apple: support coordinated suspend-to-RAM on T8140") Signed-off-by: Ryan Murray --- drivers/dma/apple-admac-test.h | 54 ++++++++++++++++++++++++++++++++++ drivers/dma/apple-admac.c | 44 +++++++++++++++++---------- 2 files changed, 82 insertions(+), 16 deletions(-) diff --git a/drivers/dma/apple-admac-test.h b/drivers/dma/apple-admac-test.h index 24d390b9d6d5b..26bb3472ea03e 100644 --- a/drivers/dma/apple-admac-test.h +++ b/drivers/dma/apple-admac-test.h @@ -185,9 +185,63 @@ static void admac_realign_only_a_configured_capture(struct kunit *test) KUNIT_EXPECT_FALSE(test, admac_resume_realigns(&adchan)); } +/* + * The client's peripheral config is only valid while it configures the + * channel: a restore after a system sleep keeps the frame size taken then and + * does not read the client's memory again. + */ +static void admac_restore_keeps_the_frame_size(struct kunit *test) +{ + struct dma_slave_config config = {}; + struct admac_chan *adchan; + struct admac_data *ad; + void *regs; + u32 *frame; + + /* A capture channel (1) of a two-channel controller, its registers plain memory */ + ad = kunit_kzalloc(test, struct_size(ad, channels, 2), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, ad); + ad->nchannels = 2; + regs = kunit_kzalloc(test, REG_CHAN_BASE(ad, 2), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, regs); + ad->base = (void __iomem *)regs; + adchan = &ad->channels[1]; + adchan->no = 1; + adchan->host = ad; + + frame = kunit_kzalloc(test, sizeof(*frame), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, frame); + *frame = 16; + + config.direction = DMA_DEV_TO_MEM; + config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; + config.src_port_window_size = 4; + config.peripheral_config = frame; + config.peripheral_size = sizeof(*frame); + KUNIT_ASSERT_EQ(test, admac_device_config(&adchan->chan, &config), 0); + KUNIT_EXPECT_EQ(test, adchan->realign_bytes, 16); + KUNIT_EXPECT_TRUE(test, adchan->cfg_valid); + KUNIT_EXPECT_NULL(test, adchan->cfg.peripheral_config); + KUNIT_EXPECT_EQ(test, adchan->cfg.peripheral_size, 0); + + /* The client's memory is reused, the channel's registers are lost */ + memset(frame, 0xa5, sizeof(*frame)); + writel_relaxed(0, ad->base + REG_BUS_WIDTH(ad, adchan->no)); + adchan->lost = true; + admac_restore_chan(adchan); + + KUNIT_EXPECT_FALSE(test, adchan->lost); + KUNIT_EXPECT_EQ(test, adchan->realign_bytes, 16); + KUNIT_EXPECT_NULL(test, adchan->cfg.peripheral_config); + /* The rest of the configuration is programmed again */ + KUNIT_EXPECT_EQ(test, readl_relaxed(ad->base + REG_BUS_WIDTH(ad, adchan->no)), + BUS_WIDTH_32BIT | BUS_WIDTH_FRAME_4_WORDS); +} + static struct kunit_case admac_realign_cases[] = { KUNIT_CASE(admac_realign_needs_a_frame_size), KUNIT_CASE(admac_realign_only_a_configured_capture), + KUNIT_CASE(admac_restore_keeps_the_frame_size), {} }; diff --git a/drivers/dma/apple-admac.c b/drivers/dma/apple-admac.c index cc57d9c76847e..a42ba9559db11 100644 --- a/drivers/dma/apple-admac.c +++ b/drivers/dma/apple-admac.c @@ -822,7 +822,8 @@ static void admac_reset_rings(struct admac_chan *adchan) writel_relaxed(0, ad->base + REG_CHAN_CTL(ad, adchan->no)); } -static int admac_device_config(struct dma_chan *chan, struct dma_slave_config *config); +static int admac_program_config(struct admac_chan *adchan, + const struct dma_slave_config *config); /* Registers can't be reached before a client powers the fabric, so this waits for it. */ static void admac_restore_chan(struct admac_chan *adchan) @@ -836,8 +837,9 @@ static void admac_restore_chan(struct admac_chan *adchan) if (ad->split_channels || ad->display) writel_relaxed(T8132_CTL_VAL, ad->base + REG_CTL); writel_relaxed(adchan->carveout, ad->base + REG_CHAN_SRAM_CARVEOUT(ad, adchan->no)); + /* The registers only: realign_bytes was taken when the client configured. */ if (adchan->cfg_valid) - admac_device_config(&adchan->chan, &adchan->cfg); + admac_program_config(adchan, &adchan->cfg); } /* @@ -1465,10 +1467,10 @@ static int admac_display_probe_control(struct admac_data *ad) return 0; } -static int admac_device_config(struct dma_chan *chan, - struct dma_slave_config *config) +/* Program a channel's registers from @config, a client's or the saved copy. */ +static int admac_program_config(struct admac_chan *adchan, + const struct dma_slave_config *config) { - struct admac_chan *adchan = to_admac_chan(chan); struct admac_data *ad = adchan->host; bool is_tx = admac_chan_direction(adchan->no) == DMA_MEM_TO_DEV; int wordsize = 0; @@ -1520,17 +1522,6 @@ static int admac_device_config(struct dma_chan *chan, writel_relaxed(bus_width, ad->base + REG_BUS_WIDTH(ad, adchan->no)); - adchan->realign_bytes = admac_config_realign_bytes(config); - - /* - * Optional: a u32 frame size in bytes, which a paused capture is - * restarted aligned to on resume (see admac_cyclic_realign()). - */ - if (config->peripheral_config && config->peripheral_size == sizeof(u32)) - adchan->realign_bytes = *(u32 *)config->peripheral_config; - else - adchan->realign_bytes = 0; - /* * By FIFOCTL_LIMIT we seem to set the maximal number of bytes allowed to be * held in controller's per-channel FIFO. Transfers seem to be triggered @@ -1549,7 +1540,28 @@ static int admac_device_config(struct dma_chan *chan, ad->base + REG_CHAN_FIFOCTL(ad, adchan->no)); } + return 0; +} + +static int admac_device_config(struct dma_chan *chan, + struct dma_slave_config *config) +{ + struct admac_chan *adchan = to_admac_chan(chan); + int ret; + + ret = admac_program_config(adchan, config); + if (ret) + return ret; + + /* + * The peripheral config is the client's memory and only valid during + * this call: take the frame size from it now and keep no pointer to it + * in the copy a restore programs the channel from. + */ + adchan->realign_bytes = admac_config_realign_bytes(config); adchan->cfg = *config; + adchan->cfg.peripheral_config = NULL; + adchan->cfg.peripheral_size = 0; adchan->cfg_valid = true; return 0; From 85730ab049e3fa178ab7618c1095aa09688c43f8 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 13:06:35 +1100 Subject: [PATCH 2/2] ASoC: apple: aop_lpai: check the period range and the ring size of a format The low-power microphone takes its stream format and, on T6041, its ring geometry from properties of the AOP's audio service. The PCM limits are computed from the rate: the shortest period is PERIOD_MS_MIN of frames, and the most periods is the buffer limit divided by it. A rate that gives no whole frame in that time made the shortest period zero and the division one by zero, and a rate large enough overflowed the product; both stop the kernel when the PCM is opened. Compute the shortest period with checked arithmetic when the format is read, and refuse a format whose shortest period is empty or longer than the longest period the PCM offers. Such a format leaves the PCM out, as the other unusable formats do. The host ring was sized as frames times frame bytes with only a 4 GiB limit, and was allocated before the binding. Bound it by the ring's own format: at most RING_SECONDS_MAX (16) seconds of that format, four times the four seconds the firmware describes at any rate and channel count, and at most a fixed 64 MiB ceiling, which is above four seconds of the fastest format the PCM offers. Refuse a larger ring before anything is allocated, together with the report-interval checks that were already there, in one helper. Add KUnit cases for formats with and without a valid period range and for ring geometries on both sides of the time and byte limits, and for four seconds of every rate and channel count the ring takes. Signed-off-by: Ryan Murray --- sound/soc/apple/aop_lpai.rs | 135 +++++++++++++++++++++++++++++++++--- 1 file changed, 126 insertions(+), 9 deletions(-) diff --git a/sound/soc/apple/aop_lpai.rs b/sound/soc/apple/aop_lpai.rs index d218570a8b999..d72627ecfac52 100644 --- a/sound/soc/apple/aop_lpai.rs +++ b/sound/soc/apple/aop_lpai.rs @@ -118,6 +118,14 @@ const PERIOD_MS_MIN: u32 = 10; const PERIOD_BYTES_MAX: usize = 0x10000; const BUFFER_BYTES_MAX: usize = 512 * 1024; const PERIODS_MIN: u32 = 2; +/// T6041: the longest host ring the driver allocates, in seconds of the +/// ring's own format. The firmware describes four seconds; the limit leaves +/// four times that, at any rate and channel count. +const RING_SECONDS_MAX: u64 = 16; +/// T6041: a ceiling on the ring whatever the format, above four seconds of +/// the fastest format the PCM offers ([`PERIOD_BYTES_MAX`] per +/// [`PERIOD_MS_MIN`]). +const RING_BYTES_MAX: usize = 64 << 20; /// Warning classes, one message per class and driver instance. const WARN_MALFORMED: usize = 0; @@ -319,6 +327,8 @@ impl StreamFormat { return None; } fmt.alsa_format()?; + // The PCM's period range must exist for the format. + fmt.period_bytes_min()?; Some(fmt) } @@ -326,6 +336,18 @@ impl StreamFormat { (self.channels * self.bytes_per_channel) as usize } + /// The bytes of the shortest period, [`PERIOD_MS_MIN`] of whole frames; + /// `None` when that is no frame at all or more than the longest period + /// ([`PERIOD_BYTES_MAX`]), so that the PCM would have no valid period. + fn period_bytes_min(&self) -> Option { + let frames = u64::from(self.rate).checked_mul(u64::from(PERIOD_MS_MIN))? / 1000; + let frame = self.channels.checked_mul(self.bytes_per_channel)?; + let bytes = frames.checked_mul(u64::from(frame))?; + usize::try_from(bytes) + .ok() + .filter(|b| (1..=PERIOD_BYTES_MAX).contains(b)) + } + /// Signed little-endian PCM of the container width. fn alsa_format(&self) -> Option { match self.bytes_per_channel { @@ -406,6 +428,26 @@ pub(crate) enum RingStep { Copy(usize), } +/// The bytes of a host ring of `frames` frames of `format` that reports its +/// producer every `quantum` frames; `None` for a geometry the driver does not +/// take: an empty ring, one longer than [`RING_SECONDS_MAX`] of the format or +/// larger than [`RING_BYTES_MAX`], or a report interval that is zero or more +/// than half the ring. +pub(crate) fn ring_geometry(format: &StreamFormat, frames: u64, quantum: u64) -> Option { + let frames_max = u64::from(format.rate).checked_mul(RING_SECONDS_MAX)?; + let frame_bytes = u64::try_from(format.frame_bytes()).ok()?; + let bytes = usize::try_from(frames.checked_mul(frame_bytes)?).ok()?; + if bytes == 0 + || frames > frames_max + || bytes > RING_BYTES_MAX + || quantum == 0 + || quantum > frames / 2 + { + return None; + } + Some(bytes) +} + pub(crate) fn ring_step(copied: Option, absolute: u64, ring_bytes: usize) -> RingStep { match copied { Some(c) if absolute >= c && ((absolute - c) as usize) < ring_bytes => { @@ -587,7 +629,8 @@ impl LpaiCapture { /// a node without one yields `Ok(None)`. /// /// The geometry is read before the binding so the ring has exactly the - /// size the firmware describes (frames times frame bytes). + /// size the firmware describes (frames times frame bytes); a ring beyond + /// [`ring_geometry`]'s limits is refused before anything is allocated. pub(crate) fn new_ring( bus: &device::Device, dev: &ARef, @@ -624,14 +667,10 @@ impl LpaiCapture { }; let frames = le_u64(&geo, GEOMETRY_RING_FRAMES).ok_or(EIO)?; let quantum = le_u64(&geo, GEOMETRY_REPORT_FRAMES).ok_or(EIO)?; - let ring_bytes = frames - .checked_mul(format.frame_bytes() as u64) - .filter(|b| *b > 0 && *b <= u64::from(u32::MAX)) - .ok_or(EIO)? as usize; - if quantum == 0 || quantum > frames / 2 { + let Some(ring_bytes) = ring_geometry(&format, frames, quantum) else { dev_err!(dev, "invalid lpai ring geometry: {} frames, {} per report\n", frames, quantum); return Err(EIO); - } + }; let ring = adata.bound_ring(bus, ring_bytes, &mut |iova, len| { let req = AudioSetDeviceProp::new( AUDIO_DEV_IO_BUFFER, @@ -784,8 +823,9 @@ impl LpaiCapture { } fn hw(&self) -> bindings::snd_pcm_hardware { - let frame = self.format.frame_bytes(); - let period_min = (self.format.rate * PERIOD_MS_MIN / 1000) as usize * frame; + // `StreamFormat::new` only yields formats with a minimum period in + // 1..=PERIOD_BYTES_MAX. + let period_min = self.format.period_bytes_min().unwrap_or(PERIOD_BYTES_MAX); bindings::snd_pcm_hardware { info: bindings::SNDRV_PCM_INFO_MMAP | bindings::SNDRV_PCM_INFO_MMAP_VALID @@ -1149,6 +1189,83 @@ mod tests { assert!(StreamFormat::parse(&p).is_none()); } + fn format(rate: u32, channels: u32, bytes: u32) -> [u8; 16] { + let mut p = [0u8; 16]; + p[0..4].copy_from_slice(&1u32.to_le_bytes()); + p[4..8].copy_from_slice(&rate.to_le_bytes()); + p[8..12].copy_from_slice(&channels.to_le_bytes()); + p[12..16].copy_from_slice(&bytes.to_le_bytes()); + p + } + + #[test] + fn a_format_without_a_valid_period_range_is_refused() { + let f = StreamFormat::parse(&format(16000, 3, 2)).unwrap(); + assert_eq!(f.period_bytes_min(), Some(160 * 6)); + let f = StreamFormat::parse_ring(&format(16000, 3, 3)).unwrap(); + assert_eq!(f.period_bytes_min(), Some(160 * 12)); + let f = StreamFormat::parse(&format(48000, 8, 4)).unwrap(); + assert_eq!(f.period_bytes_min(), Some(480 * 32)); + // The shortest period holds no frame. + assert!(StreamFormat::parse(&format(50, 3, 2)).is_none()); + assert!(StreamFormat::parse_ring(&format(50, 3, 3)).is_none()); + assert!(StreamFormat::parse(&format(99, 1, 2)).is_none()); + assert!(StreamFormat::parse(&format(100, 1, 2)).is_some()); + // The shortest period is longer than the longest. + assert!(StreamFormat::parse(&format(u32::MAX, 3, 2)).is_none()); + assert!(StreamFormat::parse_ring(&format(u32::MAX, 8, 3)).is_none()); + assert!(StreamFormat::parse(&format(0x2000_0000, 3, 2)).is_none()); + // Every period range the PCM offers is non-empty and in the buffer. + for rate in [100, 8000, 16000, 44100, 48000, 96000, 192000] { + for channels in 1..=8 { + for bytes in [2, 4] { + if let Some(f) = StreamFormat::parse(&format(rate, channels, bytes)) { + let min = f.period_bytes_min().unwrap(); + assert!(min >= f.frame_bytes() && min <= PERIOD_BYTES_MAX); + assert!(BUFFER_BYTES_MAX / min >= PERIODS_MIN as usize); + } + } + } + } + } + + #[test] + fn the_ring_geometry_is_bounded_by_its_format() { + // T6041: four seconds of 16 kHz, three 4-byte channels, 0.2 s reports. + let f = StreamFormat::parse_ring(&format(16000, 3, 4)).unwrap(); + assert_eq!(ring_geometry(&f, 64000, 3200), Some(768000)); + assert_eq!(ring_geometry(&f, 64000, 32000), Some(768000)); + assert_eq!(ring_geometry(&f, 64000, 32001), None); + assert_eq!(ring_geometry(&f, 64000, 0), None); + assert_eq!(ring_geometry(&f, 0, 0), None); + // Longer rings of the same format, up to the time limit. + assert_eq!(ring_geometry(&f, 16000 * 16, 3200), Some(16000 * 16 * 12)); + assert_eq!(ring_geometry(&f, 16000 * 16 + 1, 3200), None); + assert_eq!(ring_geometry(&f, 1 << 28, 3200), None); + assert_eq!(ring_geometry(&f, u64::from(u32::MAX) / 12, 3200), None); + assert_eq!(ring_geometry(&f, u64::MAX, 3200), None); + // Four seconds of other channel counts and rates are taken. + for rate in [8000, 16000, 24000, 32000, 44100, 48000, 96000, 192000] { + for channels in 1..=8 { + for bytes in [2, 3, 4] { + if let Some(f) = StreamFormat::parse_ring(&format(rate, channels, bytes)) { + let frames = u64::from(rate) * 4; + assert_eq!( + ring_geometry(&f, frames, frames / 20), + Some(frames as usize * f.frame_bytes()) + ); + } + } + } + } + // The byte ceiling holds near the fastest format the PCM offers. + let f = StreamFormat::parse_ring(&format(204800, 8, 4)).unwrap(); + assert!(ring_geometry(&f, 204800 * 10, 3200).is_some()); + assert_eq!(ring_geometry(&f, 204800 * 12, 3200), None); + // 10 ms of 204900 Hz is 2049 frames, a period above PERIOD_BYTES_MAX. + assert!(StreamFormat::parse_ring(&format(204900, 8, 4)).is_none()); + } + fn change(last_frame: u64, cursor: u64, property: u32) -> [u8; CHANGE_REPORT_MIN] { let mut r = [0u8; CHANGE_REPORT_MIN]; r[CHANGE_MAGIC..CHANGE_MAGIC + 4].copy_from_slice(&PROPERTY_CHANGE_MAGIC.to_le_bytes());