From 89a9b961362eeb810e7875e950f85031a729cbea Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 20 Nov 2026 18:31:02 +0000 Subject: [PATCH] audio: mic privacy: post state changes in a word instead of the notifier The mic privacy manager tells the copiers about mic privacy state changes with a notifier event. The events come from the mic privacy interrupts, the base firmware IPC and DAI resume, all kernel code, and reach copiers on other cores through IDC. The notifier is not usable from user space, and Phase 5 of the kernel / user-space partition removes it. Post the state in a single atomic word instead: a sequence number, the mic disable state and the DMA data zeroing wait time, the 16-bit DDZWT field. Each copier with mic privacy takes it at the start of its next copy, in its own LL thread, on any core. As with the notifier, which the copier registered for when it configured mic privacy, a copier only takes the states posted since then. A copier that is not running takes the latest state when it runs again, instead of every change in turn. With the notifier, a mute followed by an unmute while a copier was stopped left it fading out: the unmute only applies to a muted copier. Now it stays unmuted. Remove NOTIFIER_ID_MIC_PRIVACY_STATE_CHANGE. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Liam Girdwood --- src/audio/copier/copier.c | 57 ++++++++++--------- src/include/sof/audio/mic_privacy_manager.h | 19 ++++++- src/include/sof/lib/notifier.h | 1 - .../mic_privacy_manager_intel.c | 20 ++++++- 4 files changed, 65 insertions(+), 32 deletions(-) diff --git a/src/audio/copier/copier.c b/src/audio/copier/copier.c index e8976ec9add6..06900abce766 100644 --- a/src/audio/copier/copier.c +++ b/src/audio/copier/copier.c @@ -57,30 +57,33 @@ LOG_MODULE_REGISTER(copier, CONFIG_SOF_LOG_LEVEL); SOF_DEFINE_REG_UUID(copier); #if CONFIG_INTEL_ADSP_MIC_PRIVACY -static void mic_privacy_event(void *arg, enum notify_id type, void *data) +/* Take the mic privacy state posted since the last copy, see mic_privacy_propagate_settings() */ +static void mic_privacy_take_state(struct mic_privacy_data *mic_priv_data) { - struct mic_privacy_data *mic_priv_data = arg; - struct mic_privacy_settings *mic_privacy_settings = data; + uint32_t word = atomic_get(&mic_privacy_state_word); - if (type == NOTIFIER_ID_MIC_PRIVACY_STATE_CHANGE) { - LOG_INF("state1 = %d, state2 = %d ", - mic_privacy_settings->mic_privacy_state, mic_priv_data->mic_privacy_state); + if (MIC_PRIV_STATE_SEQ(word) == mic_priv_data->state_seq) + return; - if (mic_privacy_settings->mic_privacy_state == MIC_PRIV_UNMUTED) { - if (mic_priv_data->mic_privacy_state == MIC_PRIV_MUTED) { - mic_priv_data->mic_privacy_state = MIC_PRIV_FADE_IN; - LOG_INF("switch to FADE_IN"); - } - } else { - /* In case when mute would be triggered before copier instantiation. */ - if (mic_priv_data->mic_privacy_state != MIC_PRIV_MUTED) { - mic_priv_data->mic_privacy_state = MIC_PRIV_FADE_OUT; - LOG_INF("switch to FADE_OUT"); - } + mic_priv_data->state_seq = MIC_PRIV_STATE_SEQ(word); + + LOG_INF("state1 = %u, state2 = %d ", + MIC_PRIV_STATE_MUTE(word), mic_priv_data->mic_privacy_state); + + if (!MIC_PRIV_STATE_MUTE(word)) { + if (mic_priv_data->mic_privacy_state == MIC_PRIV_MUTED) { + mic_priv_data->mic_privacy_state = MIC_PRIV_FADE_IN; + LOG_INF("switch to FADE_IN"); + } + } else { + /* In case when mute would be triggered before copier instantiation. */ + if (mic_priv_data->mic_privacy_state != MIC_PRIV_MUTED) { + mic_priv_data->mic_privacy_state = MIC_PRIV_FADE_OUT; + LOG_INF("switch to FADE_OUT"); } - mic_priv_data->max_ramp_time_in_ms = (mic_privacy_settings->max_ramp_time * 1000) / - ADSP_RTC_FREQUENCY; } + mic_priv_data->max_ramp_time_in_ms = (MIC_PRIV_STATE_RAMP(word) * 1000) / + ADSP_RTC_FREQUENCY; } __cold static int mic_privacy_configure(struct processing_module *mod, struct copier_data *cd) @@ -111,13 +114,10 @@ __cold static int mic_privacy_configure(struct processing_module *mod, struct co cd->mic_priv = mic_priv_data; - ret = notifier_register(cd->mic_priv, NULL, NOTIFIER_ID_MIC_PRIVACY_STATE_CHANGE, - mic_privacy_event); + /* only take the mic privacy states posted from now on */ + mic_priv_data->state_seq = MIC_PRIV_STATE_SEQ(atomic_get(&mic_privacy_state_word)); - if (ret != 0) - mod_free(mod, mic_priv_data); - - return ret; + return 0; } __cold static void mic_privacy_free(struct processing_module *mod) @@ -129,8 +129,6 @@ __cold static void mic_privacy_free(struct processing_module *mod) if (cd->gtw_type == ipc4_gtw_dmic) mic_privacy_enable_dmic_irq(false); - notifier_unregister(cd->mic_priv, NULL, NOTIFIER_ID_MIC_PRIVACY_STATE_CHANGE); - mod_free(mod, cd->mic_priv); } #endif @@ -818,6 +816,11 @@ static int copier_process(struct processing_module *mod, comp_dbg(dev, "entry"); +#if CONFIG_INTEL_ADSP_MIC_PRIVACY + if (cd->mic_priv) + mic_privacy_take_state(cd->mic_priv); +#endif + switch (dev->ipc_config.type) { case SOF_COMP_HOST: if (!cd->ipc_gtw) diff --git a/src/include/sof/audio/mic_privacy_manager.h b/src/include/sof/audio/mic_privacy_manager.h index 61689343a37e..ccb638c0fc53 100644 --- a/src/include/sof/audio/mic_privacy_manager.h +++ b/src/include/sof/audio/mic_privacy_manager.h @@ -8,18 +8,35 @@ #define ADSP_FW_MIC_PRIVACY_MANAGER_H #include -#include +#include #include #include "../audio/copier/copier_gain.h" #define ADSP_RTC_FREQUENCY 32768 +/* + * The mic privacy state for the copiers is a single word, so that the kernel + * can post it from the mic privacy interrupts and IPC, and the copiers can + * read it in their LL threads without a lock: an 8-bit sequence number, the + * mic disable state and the DMA data zeroing wait time in RTC ticks. + */ +#define MIC_PRIV_STATE_WORD(seq, mute, ramp) ((((uint32_t)(seq) & 0xff) << 24) | \ + ((uint32_t)!!(mute) << 16) | \ + ((uint32_t)(ramp) & 0xffff)) +#define MIC_PRIV_STATE_SEQ(word) (((uint32_t)(word) >> 24) & 0xff) +#define MIC_PRIV_STATE_MUTE(word) (((uint32_t)(word) >> 16) & 0x1) +#define MIC_PRIV_STATE_RAMP(word) ((uint32_t)(word) & 0xffff) + +/* the last mic privacy state, posted by mic_privacy_propagate_settings() */ +extern atomic_t mic_privacy_state_word; + struct mic_privacy_data { enum ipc4_sampling_frequency audio_freq; uint32_t mic_privacy_state; bool dma_data_zeroing; uint32_t fade_in_out_bytes; uint32_t max_ramp_time_in_ms; + uint8_t state_seq; /**< sequence number of the last mic privacy state taken */ struct copier_gain_params mic_priv_gain_params; }; diff --git a/src/include/sof/lib/notifier.h b/src/include/sof/lib/notifier.h index e5413a512cb4..2d1c443897a7 100644 --- a/src/include/sof/lib/notifier.h +++ b/src/include/sof/lib/notifier.h @@ -27,7 +27,6 @@ enum notify_id { NOTIFIER_ID_DMA_COPY, /* struct dma_cb_data* */ NOTIFIER_ID_DMA_IRQ, /* struct dma_chan_data * */ NOTIFIER_ID_DAI_TRIGGER, /* struct dai_group * */ - NOTIFIER_ID_MIC_PRIVACY_STATE_CHANGE, /* struct mic_privacy_settings * */ NOTIFIER_ID_VAD, /* int * (WEBRTC_VAD_SPEECH / WEBRTC_VAD_SILENCE) */ NOTIFIER_ID_COUNT }; diff --git a/src/kernel/audio/mic_privacy_manager/mic_privacy_manager_intel.c b/src/kernel/audio/mic_privacy_manager/mic_privacy_manager_intel.c index a5ff86a04fd1..d10016f4a8c3 100644 --- a/src/kernel/audio/mic_privacy_manager/mic_privacy_manager_intel.c +++ b/src/kernel/audio/mic_privacy_manager/mic_privacy_manager_intel.c @@ -11,6 +11,7 @@ #include #include #include +#include const struct device *mic_priv_dev; @@ -126,11 +127,24 @@ uint32_t mic_privacy_get_policy_register(void) return mic_privacy_api->get_privacy_policy_register_raw_value(); } +/* + * Kernel code writes it, the copiers read it in the LL threads, which can be + * user threads. + */ +APP_SYSUSER_BSS atomic_t mic_privacy_state_word; + +/* Post the mic privacy state for the copiers, which take it in their next copy. */ void mic_privacy_propagate_settings(struct mic_privacy_settings *settings) { - notifier_event(mic_priv_dev, NOTIFIER_ID_MIC_PRIVACY_STATE_CHANGE, - NOTIFIER_TARGET_CORE_ALL_MASK, settings, - sizeof(struct mic_privacy_settings)); + atomic_val_t old; + atomic_val_t new; + + do { + old = atomic_get(&mic_privacy_state_word); + new = MIC_PRIV_STATE_WORD(MIC_PRIV_STATE_SEQ(old) + 1, + settings->mic_privacy_state, + settings->max_ramp_time); + } while (!atomic_cas(&mic_privacy_state_word, old, new)); } uint32_t mic_privacy_get_dma_zeroing_wait_time(void)