From c144670bdd87678ded834716ec21a2aca635e1e1 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 20 Nov 2026 18:31:02 +0000 Subject: [PATCH 1/2] 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 ee11cdf9ac07..30f749fd5296 100644 --- a/src/include/sof/lib/notifier.h +++ b/src/include/sof/lib/notifier.h @@ -24,7 +24,6 @@ enum notify_id { NOTIFIER_ID_DMA_DOMAIN_CHANGE, /* struct dma_chan_data * */ NOTIFIER_ID_DMA_COPY, /* struct dma_cb_data* */ NOTIFIER_ID_DMA_IRQ, /* struct dma_chan_data * */ - 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) From 467b35dbe7e6303f0c2ff29f1e589770ed585f41 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sun, 29 Nov 2026 20:07:52 +0000 Subject: [PATCH 2/2] audio: mic privacy: start a copier muted when the mic is muted A copier that configures mic privacy starts unmuted, and only mutes at the next mic privacy state change. A capture started while the mic is muted records the mic until then: tested on WCL, with a test hook that toggles the mute state every 2.5 s, DMIC captures started while muted recorded the mic for their first 0.9 to 2.3 s, until the next change. The state word holds the last state posted, so a copier can start from it: take its mute state and ramp time when configuring mic privacy, and post the state at boot, including after D3, when a mic privacy policy is set, so that the word holds it before the first change. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Liam Girdwood --- src/audio/copier/copier.c | 13 +++++++++++-- .../mic_privacy_manager/mic_privacy_manager_intel.c | 12 ++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/audio/copier/copier.c b/src/audio/copier/copier.c index 06900abce766..79f19cec416a 100644 --- a/src/audio/copier/copier.c +++ b/src/audio/copier/copier.c @@ -89,6 +89,7 @@ static void mic_privacy_take_state(struct mic_privacy_data *mic_priv_data) __cold static int mic_privacy_configure(struct processing_module *mod, struct copier_data *cd) { struct mic_privacy_data *mic_priv_data; + atomic_val_t word; int ret; assert_can_be_cold(); @@ -114,8 +115,16 @@ __cold static int mic_privacy_configure(struct processing_module *mod, struct co cd->mic_priv = mic_priv_data; - /* 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)); + /* + * Start from the current mic privacy state, which the DMIC IRQ enable + * above may just have posted, and take only the changes posted after it. + */ + word = atomic_get(&mic_privacy_state_word); + mic_priv_data->state_seq = MIC_PRIV_STATE_SEQ(word); + if (MIC_PRIV_STATE_MUTE(word)) + mic_priv_data->mic_privacy_state = MIC_PRIV_MUTED; + mic_priv_data->max_ramp_time_in_ms = (MIC_PRIV_STATE_RAMP(word) * 1000) / + ADSP_RTC_FREQUENCY; return 0; } 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 d10016f4a8c3..c9a2b8d9a5bc 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 @@ -108,6 +108,18 @@ __cold int mic_privacy_manager_init(void) enable_fw_managed_irq(true); } + /* + * Post the state at boot, including after D3, so that copiers configured + * before the first change start from it. + */ + if (mic_privacy_policy == MIC_PRIVACY_HW_MANAGED || + mic_privacy_policy == MIC_PRIVACY_FW_MANAGED) { + struct mic_privacy_settings settings; + + mic_privacy_fill_settings(&settings, mic_privacy_get_mic_disable_status()); + mic_privacy_propagate_settings(&settings); + } + return 0; }