diff --git a/sound/soc/apple/macaudio-iface-test.h b/sound/soc/apple/macaudio-iface-test.h index 5f4d712878ee7..6b9f506829757 100644 --- a/sound/soc/apple/macaudio-iface-test.h +++ b/sound/soc/apple/macaudio-iface-test.h @@ -463,10 +463,43 @@ static void macaudio_iface_control_is_inactive(struct kunit *test) KUNIT_EXPECT_TRUE(test, iface.access & SNDRV_CTL_ELEM_ACCESS_VOLATILE); } +/* + * A sleep on the volume lock, then the lease and back before the daemon's + * first ping: both switches drop the kept volumes, the way back reports the + * lost lock once, and the unlock after it reports nothing more. + */ +static void macaudio_iface_lease_after_sleep(struct kunit *test) +{ + struct macaudio_test_iface f; + struct macaudio_snd_data *ma; + + macaudio_test_iface_card(test, &f); + ma = f.t.ma; + macaudio_test_select_legacy(test, &f); + macaudio_test_daemon_take(test, &f); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); + macaudio_pm_notify(&ma->pm_nb, PM_SUSPEND_PREPARE, NULL); + macaudio_pm_notify(&ma->pm_nb, PM_POST_SUSPEND, NULL); + KUNIT_EXPECT_NOT_NULL(test, ma->pm_volumes); + KUNIT_EXPECT_TRUE(test, ma->pm_lock_taken); + + KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), 1); + KUNIT_EXPECT_NULL(test, ma->pm_volumes); + KUNIT_EXPECT_FALSE(test, ma->pm_lock_taken); + KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_UNLOCK), 1); + macaudio_test_settle(ma); + + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); + KUNIT_EXPECT_TRUE(test, ma->speaker_volume_unlocked); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); +} + static struct kunit_case macaudio_iface_cases[] = { KUNIT_CASE(macaudio_iface_volume_lock_from_binding), KUNIT_CASE(macaudio_iface_switch_goes_through_the_floor), KUNIT_CASE(macaudio_iface_revert_starts_at_the_floor), + KUNIT_CASE(macaudio_iface_lease_after_sleep), KUNIT_CASE(macaudio_iface_unowned_lease_reverts), KUNIT_CASE(macaudio_iface_protection_write_deselects), KUNIT_CASE(macaudio_iface_needs_cap_sys_admin), diff --git a/sound/soc/apple/macaudio-pm-volumes-test.h b/sound/soc/apple/macaudio-pm-volumes-test.h index 3e827fd7ae3e7..bcf9f65cef82c 100644 --- a/sound/soc/apple/macaudio-pm-volumes-test.h +++ b/sound/soc/apple/macaudio-pm-volumes-test.h @@ -8,6 +8,8 @@ struct macaudio_pmt_vol { struct soc_mixer_control mc; long value; + /* The next read fails once, as the keeping before the sleep can meet */ + int get_err; }; static struct macaudio_pmt_vol *macaudio_pmt_vol(struct snd_kcontrol *kctl) @@ -26,7 +28,14 @@ static int macaudio_pmt_vol_info(struct snd_kcontrol *kctl, struct snd_ctl_elem_ static int macaudio_pmt_vol_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *uval) { - uval->value.integer.value[0] = macaudio_pmt_vol(kctl)->value; + struct macaudio_pmt_vol *vol = macaudio_pmt_vol(kctl); + int err = vol->get_err; + + /* Once only: the clip after the keeping reads the control too */ + vol->get_err = 0; + if (err) + return err; + uval->value.integer.value[0] = vol->value; return 0; } @@ -366,12 +375,84 @@ static void macaudio_pm_volumes_taken_again(struct kunit *test) macaudio_pmt_expect_vols(test, &t, false); } +/* The stream's stop, through lock_update_work, as the back-ends trigger it */ +static void macaudio_pmt_bes(struct macaudio_pmt_pm *t, bool active) +{ + t->ma->bes_active = active; + macaudio_vlimit_update_work(&t->ma->lock_update_work); +} + +/* + * A speaker stream runs at suspend-prepare: the stop that the gate queues + * takes the lock with pm_quiesced already set, before macaudio_vlimit_pm(). + * The volumes are kept there, and come back at the first ping after resume. + */ +static void macaudio_pm_volumes_lock_taken_before_pm(struct kunit *test) +{ + struct macaudio_pmt_pm t; + + macaudio_pmt_pm_card(test, &t); + macaudio_pmt_take(test, &t); + macaudio_pmt_bes(&t, true); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + + WRITE_ONCE(t.ma->pm_quiesced, true); + macaudio_pmt_bes(&t, false); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + KUNIT_EXPECT_NOT_NULL(test, t.ma->pm_volumes); + macaudio_pmt_expect_vols(test, &t, true); + + macaudio_pmt_notify(&t, PM_SUSPEND_PREPARE); + macaudio_pmt_notify(&t, PM_POST_SUSPEND); + macaudio_pmt_expect_vols(test, &t, true); + + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + macaudio_pmt_expect_vols(test, &t, false); + KUNIT_EXPECT_NULL(test, t.ma->pm_volumes); + KUNIT_EXPECT_FALSE(test, t.ma->pm_lock_taken); + /* Set back, not lost: no lost-lock report follows */ + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); +} + +/* + * Keeping the volumes fails: the unlock after resume has nothing to set + * back, so the daemon learns that the lock was lost (its next ping fails + * with -ETIMEDOUT) and sets the volumes again itself. Once only. + */ +static void macaudio_pm_volumes_save_fails(struct kunit *test) +{ + struct macaudio_pmt_pm t; + + macaudio_pmt_pm_card(test, &t); + macaudio_pmt_take(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + + macaudio_pmt_vol(t.vols[1])->get_err = -EIO; + macaudio_pmt_notify(&t, PM_SUSPEND_PREPARE); + KUNIT_EXPECT_NULL(test, t.ma->pm_volumes); + KUNIT_EXPECT_TRUE(test, t.ma->pm_lock_taken); + macaudio_pmt_expect_vols(test, &t, true); + macaudio_pmt_notify(&t, PM_POST_SUSPEND); + + /* The unlock itself: the volumes stay at the ceiling for the daemon */ + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + KUNIT_EXPECT_FALSE(test, t.ma->pm_lock_taken); + macaudio_pmt_expect_vols(test, &t, true); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); +} + static struct kunit_case macaudio_pm_volumes_cases[] = { KUNIT_CASE(macaudio_pm_volumes_ignore_user_elements), KUNIT_CASE(macaudio_pm_volumes_restored_after_sleep), KUNIT_CASE(macaudio_pm_volumes_second_sleep_before_ping), KUNIT_CASE(macaudio_pm_volumes_new_owner), KUNIT_CASE(macaudio_pm_volumes_taken_again), + KUNIT_CASE(macaudio_pm_volumes_lock_taken_before_pm), + KUNIT_CASE(macaudio_pm_volumes_save_fails), {} }; diff --git a/sound/soc/apple/macaudio.c b/sound/soc/apple/macaudio.c index 3cd2d9386b7e4..7b21aced25536 100644 --- a/sound/soc/apple/macaudio.c +++ b/sound/soc/apple/macaudio.c @@ -404,6 +404,8 @@ struct macaudio_snd_data { */ struct macaudio_pm_volume *pm_volumes; unsigned int pm_nr_volumes; + /* The system-sleep lock clipped the volumes and its unlock has not come */ + bool pm_lock_taken; /* Allocated with them, so that setting them again cannot fail */ struct snd_ctl_elem_value *pm_uval; @@ -704,6 +706,7 @@ static void macaudio_pm_volumes_drop(struct macaudio_snd_data *ma) ma->pm_volumes = NULL; ma->pm_nr_volumes = 0; ma->pm_uval = NULL; + ma->pm_lock_taken = false; } /* @@ -762,6 +765,7 @@ static void macaudio_pm_volumes_save(struct macaudio_snd_data *ma) ma->pm_volumes = vols; ma->pm_nr_volumes = i; ma->pm_uval = uval; + dev_dbg(ma->card.dev, "Kept %u speaker volumes for the system sleep\n", i); return; out: kfree(uval); @@ -781,7 +785,7 @@ static void macaudio_pm_volumes_restore(struct macaudio_snd_data *ma) int ret; if (!ma->pm_volumes) - return; + goto out; for (i = 0; i < ma->pm_nr_volumes; i++) { struct snd_kcontrol *kctl = ma->pm_volumes[i].kctl; @@ -798,6 +802,9 @@ static void macaudio_pm_volumes_restore(struct macaudio_snd_data *ma) snd_ctl_notify(ma->card.snd_card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id); } + dev_dbg(ma->card.dev, "Set %u speaker volumes again after the system sleep\n", + ma->pm_nr_volumes); +out: macaudio_pm_volumes_drop(ma); } @@ -805,6 +812,7 @@ static void macaudio_vlimit_update(struct macaudio_snd_data *ma) { unsigned int i, j; bool unlock = true; + bool quiesced; struct snd_kcontrol *kctl; const char *reason; @@ -823,6 +831,13 @@ static void macaudio_vlimit_update(struct macaudio_snd_data *ma) if (macaudio_spk_lease_only(ma->cfg)) return; + /* + * macaudio_pm_notify() writes pm_quiesced without volume_lock_mutex: + * read it once, so the lock reason, the lost-lease flag and the kept + * volumes below all see the same value. + */ + quiesced = READ_ONCE(ma->pm_quiesced); + /* Only after an explicit selection of the volume lock */ if (!ma->spk_legacy_selected) { reason = "Speaker Volume Unlock not selected"; @@ -848,7 +863,7 @@ static void macaudio_vlimit_update(struct macaudio_snd_data *ma) * Lock the volumes before the freeze instead of when the frozen daemon's * lease runs out. */ - if (ma->pm_quiesced) { + if (quiesced) { reason = "System suspend"; unlock = false; } @@ -871,11 +886,33 @@ static void macaudio_vlimit_update(struct macaudio_snd_data *ma) } else { dev_info(ma->card.dev, "Speaker volumes locked: %s\n", reason); /* The system-sleep lock is the kernel's own, not a lease the daemon lost */ - if (!ma->pm_quiesced) + if (!quiesced) ma->speaker_volume_was_locked = true; } + /* + * The system-sleep lock keeps the volumes wherever it is taken. + * With a speaker stream running at suspend-prepare, the stop + * that macaudio_spk_close_gate() queues reaches here through + * lock_update_work before macaudio_vlimit_pm() does; keeping + * them only there left nothing to set again at the unlock, and + * the volumes stayed at the lock's ceiling for the whole boot. + */ + if (!unlock && quiesced) { + macaudio_pm_volumes_save(ma); + ma->pm_lock_taken = true; + } macaudio_vlimit_unlock(ma, unlock); + if (unlock && ma->pm_lock_taken && !ma->pm_volumes) { + /* + * Nothing kept: the daemon does not set volumes it + * believes unchanged, so tell it the lock was lost; it + * sets them again from its model. + */ + dev_warn(ma->card.dev, + "No speaker volumes kept for the system sleep: the daemon sets them again\n"); + ma->speaker_volume_was_locked = true; + } if (unlock) macaudio_pm_volumes_restore(ma); if (!unlock && ma->cfg->amp == AMP_MAX98360A) { @@ -911,7 +948,6 @@ static void macaudio_vlimit_pm(struct macaudio_snd_data *ma, bool quiesce) if (quiesce) { cancel_delayed_work(&ma->lock_timeout_work); ma->speaker_lock_remain = 0; - macaudio_pm_volumes_save(ma); } macaudio_vlimit_update(ma); mutex_unlock(&ma->volume_lock_mutex); @@ -976,6 +1012,11 @@ static void macaudio_vlimit_timeout_work(struct work_struct *wrk) struct macaudio_snd_data, lock_timeout_work); ktime_t now, left; + /* + * An expiry with pm_quiesced set keeps the volumes, which walks the + * card's controls: hold them as macaudio_vlimit_update_work() does. + */ + down_read(&ma->card.snd_card->controls_rwsem); mutex_lock(&ma->volume_lock_mutex); /* @@ -1000,6 +1041,7 @@ static void macaudio_vlimit_timeout_work(struct work_struct *wrk) out: mutex_unlock(&ma->volume_lock_mutex); + up_read(&ma->card.snd_card->controls_rwsem); } static void macaudio_vlimit_update_work(struct work_struct *wrk) @@ -4213,7 +4255,7 @@ static int macaudio_slk_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_v } /* Between suspend-prepare and the freeze: the lease starts again after the resume */ - if (ma->pm_quiesced) { + if (READ_ONCE(ma->pm_quiesced)) { mutex_unlock(&ma->volume_lock_mutex); return 0; }