[PATCH v3 4/5] soundwire: Include the current stream when preparing again

From: Bard Liao

Date: Thu Nov 12 2026 - 09:59:36 EST


sdw_prepare_stream() accepts a stream in the DEPREPARED state without
changing its state before parameter computation. However, the generic
allocator excludes DEPREPARED streams from rate groups and port
computation, and only includes a CONFIGURED current stream in group
payload calculations.

Consequently, preparing an existing stream again can reserve bandwidth
without computing the corresponding transport parameters. The filters
need to distinguish preparing that stream again from depreparing it.

Pass the prepare context through the group and port helpers. Include
the current stream during prepare even when it is still DEPREPARED,
while preserving the filtering of all other runtimes and the behavior
during deprepare. This supports preparing again without temporarily
changing stream state.

Signed-off-by: Bard Liao <yung-chuan.liao@xxxxxxxxxxxxxxx>
Reviewed-by: Péter Ujfalusi <peter.ujfalusi@xxxxxxxxxxxxxxx>
---
.../soundwire/generic_bandwidth_allocation.c | 36 ++++++++++++-------
1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/drivers/soundwire/generic_bandwidth_allocation.c b/drivers/soundwire/generic_bandwidth_allocation.c
index e8e68dbe180b..9bacde2c67a1 100644
--- a/drivers/soundwire/generic_bandwidth_allocation.c
+++ b/drivers/soundwire/generic_bandwidth_allocation.c
@@ -188,7 +188,8 @@ static void sdw_compute_master_ports(struct sdw_master_runtime *m_rt,
}

static void _sdw_compute_port_params(struct sdw_bus *bus,
- struct sdw_group_params *params, int count)
+ struct sdw_group_params *params, int count,
+ struct sdw_stream_runtime *stream, bool prepare)
{
struct sdw_master_runtime *m_rt;
int port_bo, i, l;
@@ -209,9 +210,12 @@ static void _sdw_compute_port_params(struct sdw_bus *bus,
/*
* Only runtimes with CONFIGURED, PREPARED, ENABLED, and DISABLED
* states should be included in the bandwidth calculation.
+ * Include the current stream during prepare even if it is
+ * still in the DEPREPARED state.
*/
- if (m_rt->stream->state > SDW_STREAM_DISABLED ||
- m_rt->stream->state < SDW_STREAM_CONFIGURED)
+ if (!(prepare && m_rt->stream == stream) &&
+ (m_rt->stream->state > SDW_STREAM_DISABLED ||
+ m_rt->stream->state < SDW_STREAM_CONFIGURED))
continue;
sdw_compute_master_ports(m_rt, &params[i], &port_bo, hstop);
}
@@ -224,7 +228,7 @@ static void _sdw_compute_port_params(struct sdw_bus *bus,
static int sdw_compute_group_params(struct sdw_bus *bus,
struct sdw_stream_runtime *stream,
struct sdw_group_params *params,
- struct sdw_group *group)
+ struct sdw_group *group, bool prepare)
{
struct sdw_master_runtime *m_rt;
struct sdw_port_runtime *p_rt;
@@ -242,7 +246,7 @@ static int sdw_compute_group_params(struct sdw_bus *bus,
list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
if (m_rt->stream == stream) {
/* Only runtime during prepare should be added */
- if (stream->state != SDW_STREAM_CONFIGURED)
+ if (!prepare)
continue;
} else {
/*
@@ -331,7 +335,8 @@ static int sdw_add_element_group_count(struct sdw_group *group,
}

static int sdw_get_group_count(struct sdw_bus *bus,
- struct sdw_group *group)
+ struct sdw_group *group,
+ struct sdw_stream_runtime *stream, bool prepare)
{
struct sdw_master_runtime *m_rt;
struct sdw_port_runtime *p_rt;
@@ -352,7 +357,12 @@ static int sdw_get_group_count(struct sdw_bus *bus,
}

list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
- if (m_rt->stream->state == SDW_STREAM_DEPREPARED)
+ /*
+ * Include the current stream during prepare even if it is still
+ * in the DEPREPARED state.
+ */
+ if (!(prepare && m_rt->stream == stream) &&
+ m_rt->stream->state == SDW_STREAM_DEPREPARED)
continue;

rate = m_rt->stream->params.rate;
@@ -383,14 +393,16 @@ static int sdw_get_group_count(struct sdw_bus *bus,
*
* @bus: SDW Bus instance
* @stream: Soundwire stream
+ * @prepare: True when preparing, false when depreparing
*/
-static int sdw_compute_port_params(struct sdw_bus *bus, struct sdw_stream_runtime *stream)
+static int sdw_compute_port_params(struct sdw_bus *bus, struct sdw_stream_runtime *stream,
+ bool prepare)
{
struct sdw_group_params *params = NULL;
struct sdw_group group;
int ret;

- ret = sdw_get_group_count(bus, &group);
+ ret = sdw_get_group_count(bus, &group, stream, prepare);
if (ret < 0)
return ret;

@@ -404,11 +416,11 @@ static int sdw_compute_port_params(struct sdw_bus *bus, struct sdw_stream_runtim
}

/* Compute transport parameters for grouped streams */
- ret = sdw_compute_group_params(bus, stream, params, &group);
+ ret = sdw_compute_group_params(bus, stream, params, &group, prepare);
if (ret < 0)
goto free_params;

- _sdw_compute_port_params(bus, params, group.count);
+ _sdw_compute_port_params(bus, params, group.count, stream, prepare);

free_params:
kfree(params);
@@ -686,7 +698,7 @@ int sdw_compute_params(struct sdw_bus *bus, struct sdw_stream_runtime *stream,
}

/* Compute transport and port params */
- ret = sdw_compute_port_params(bus, stream);
+ ret = sdw_compute_port_params(bus, stream, prepare);
if (ret < 0) {
dev_err(bus->dev, "Compute transport params failed: %d\n", ret);
return ret;
--
2.43.0