Skip to content

Commit 2c1c94a

Browse files
author
Jyri Sarha
committed
Audio: Copier: All memory allocations through module API
Allocate all memory through module API mod_alloc() and friends and remove all redundant rfree() calls from module unload functions and init error branches. When resources are allocated through module API functions they are automatically freed when the module is unloaded. This simplifies error handling process. NOTE: copier_dai.c and copier_host.c still have their shared memory allocated through the old API. This is to be fixed once we have decided on how the shared memory allocations should work in user-space. Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
1 parent 6a72bbd commit 2c1c94a

5 files changed

Lines changed: 33 additions & 63 deletions

File tree

src/audio/copier/copier.c

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <sof/ipc/topology.h>
1616
#include <rtos/interrupt.h>
1717
#include <rtos/timer.h>
18-
#include <rtos/alloc.h>
1918
#include <rtos/cache.h>
2019
#include <rtos/init.h>
2120
#include <sof/lib/memory.h>
@@ -82,13 +81,12 @@ static void mic_privacy_event(void *arg, enum notify_id type, void *data)
8281
}
8382
}
8483

85-
static int mic_privacy_configure(struct comp_dev *dev, struct copier_data *cd)
84+
static int mic_privacy_configure(struct processing_module *mod, struct copier_data *cd)
8685
{
8786
struct mic_privacy_data *mic_priv_data;
8887
int ret;
8988

90-
mic_priv_data = rzalloc(SOF_MEM_FLAG_USER,
91-
sizeof(struct mic_privacy_data));
89+
mic_priv_data = mod_zalloc(mod, sizeof(struct mic_privacy_data));
9290
if (!mic_priv_data)
9391
return -ENOMEM;
9492

@@ -100,19 +98,15 @@ static int mic_privacy_configure(struct comp_dev *dev, struct copier_data *cd)
10098
uint32_t zeroing_wait_time = (mic_privacy_get_dma_zeroing_wait_time() * 1000) /
10199
ADSP_RTC_FREQUENCY;
102100

103-
ret = copier_gain_set_params(dev, &mic_priv_data->mic_priv_gain_params,
101+
ret = copier_gain_set_params(mod->dev, &mic_priv_data->mic_priv_gain_params,
104102
zeroing_wait_time, SOF_DAI_INTEL_NONE);
105-
if (ret != 0) {
106-
rfree(mic_priv_data);
103+
if (ret != 0)
107104
return ret;
108-
}
109105

110106
cd->mic_priv = mic_priv_data;
111107

112108
ret = notifier_register(cd->mic_priv, NULL, NOTIFIER_ID_MIC_PRIVACY_STATE_CHANGE,
113109
mic_privacy_event, 0);
114-
if (ret != 0)
115-
rfree(mic_priv_data);
116110

117111
return ret;
118112
}
@@ -123,8 +117,6 @@ static void mic_privacy_free(struct copier_data *cd)
123117
mic_privacy_enable_dmic_irq(false);
124118

125119
notifier_unregister(cd->mic_priv, NULL, NOTIFIER_ID_MIC_PRIVACY_STATE_CHANGE);
126-
127-
rfree(cd->mic_priv);
128120
}
129121
#endif
130122

@@ -141,7 +133,7 @@ __cold static int copier_init(struct processing_module *mod)
141133

142134
assert_can_be_cold();
143135

144-
cd = rzalloc(SOF_MEM_FLAG_USER, sizeof(*cd));
136+
cd = mod_zalloc(mod, sizeof(*cd));
145137
if (!cd)
146138
return -ENOMEM;
147139

@@ -151,10 +143,8 @@ __cold static int copier_init(struct processing_module *mod)
151143
* store it, it's only used during IPC processing, besides we haven't
152144
* allocated space for it, so don't "fix" this!
153145
*/
154-
if (memcpy_s(&cd->config, sizeof(cd->config), copier, sizeof(*copier)) < 0) {
155-
ret = -EINVAL;
156-
goto error_cd;
157-
}
146+
if (memcpy_s(&cd->config, sizeof(cd->config), copier, sizeof(*copier)) < 0)
147+
return -EINVAL;
158148

159149
/* Allocate memory and store gateway_cfg in runtime. Gateway cfg has to
160150
* be kept even after copier is created e.g. during SET_PIPELINE_STATE
@@ -163,18 +153,15 @@ __cold static int copier_init(struct processing_module *mod)
163153
*/
164154
if (copier->gtw_cfg.config_length) {
165155
gtw_cfg_size = copier->gtw_cfg.config_length << 2;
166-
gtw_cfg = rmalloc(SOF_MEM_FLAG_USER,
167-
gtw_cfg_size);
168-
if (!gtw_cfg) {
169-
ret = -ENOMEM;
170-
goto error_cd;
171-
}
156+
gtw_cfg = mod_alloc(mod, gtw_cfg_size);
157+
if (!gtw_cfg)
158+
return -ENOMEM;
172159

173160
ret = memcpy_s(gtw_cfg, gtw_cfg_size, &copier->gtw_cfg.config_data,
174161
gtw_cfg_size);
175162
if (ret) {
176163
comp_err(dev, "Unable to copy gateway config from copier blob");
177-
goto error;
164+
return ret;
178165
}
179166

180167
cd->gtw_cfg = gtw_cfg;
@@ -191,18 +178,18 @@ __cold static int copier_init(struct processing_module *mod)
191178
switch (node_id.f.dma_type) {
192179
case ipc4_hda_host_output_class:
193180
case ipc4_hda_host_input_class:
194-
ret = copier_host_create(dev, cd, copier, dev->pipeline);
181+
ret = copier_host_create(mod, cd, copier, dev->pipeline);
195182
if (ret < 0) {
196183
comp_err(dev, "unable to create host");
197-
goto error;
184+
return ret;
198185
}
199186
#if CONFIG_INTEL_ADSP_MIC_PRIVACY
200187
if (cd->direction == SOF_IPC_STREAM_CAPTURE &&
201188
node_id.f.dma_type == ipc4_hda_host_output_class) {
202-
ret = mic_privacy_configure(dev, cd);
189+
ret = mic_privacy_configure(mod, cd);
203190
if (ret < 0) {
204191
comp_err(dev, "unable to configure mic privacy");
205-
goto error;
192+
return ret;
206193
}
207194
}
208195
#endif
@@ -217,32 +204,31 @@ __cold static int copier_init(struct processing_module *mod)
217204
ret = copier_dai_create(dev, cd, copier, dev->pipeline);
218205
if (ret < 0) {
219206
comp_err(dev, "unable to create dai");
220-
goto error;
207+
return ret;
221208
}
222209
#if CONFIG_INTEL_ADSP_MIC_PRIVACY
223210
if (cd->direction == SOF_IPC_STREAM_CAPTURE) {
224-
ret = mic_privacy_configure(dev, cd);
211+
ret = mic_privacy_configure(mod, cd);
225212
if (ret < 0) {
226213
comp_err(dev, "unable to configure mic privacy");
227-
goto error;
214+
return ret;
228215
}
229216
}
230217
#endif
231218
break;
232219
#if CONFIG_IPC4_GATEWAY
233220
case ipc4_ipc_output_class:
234221
case ipc4_ipc_input_class:
235-
ret = copier_ipcgtw_create(dev, cd, copier, dev->pipeline);
222+
ret = copier_ipcgtw_create(mod, cd, copier, dev->pipeline);
236223
if (ret < 0) {
237224
comp_err(dev, "unable to create IPC gateway");
238-
goto error;
225+
return ret;
239226
}
240227
break;
241228
#endif
242229
default:
243230
comp_err(dev, "unsupported dma type %x", (uint32_t)node_id.f.dma_type);
244-
ret = -EINVAL;
245-
goto error;
231+
return -EINVAL;
246232
};
247233

248234
dev->direction_set = true;
@@ -256,11 +242,6 @@ __cold static int copier_init(struct processing_module *mod)
256242
dev->direction = cd->direction;
257243
dev->state = COMP_STATE_READY;
258244
return 0;
259-
error:
260-
rfree(gtw_cfg);
261-
error_cd:
262-
rfree(cd);
263-
return ret;
264245
}
265246

266247
__cold static int copier_free(struct processing_module *mod)
@@ -289,10 +270,6 @@ __cold static int copier_free(struct processing_module *mod)
289270
break;
290271
}
291272

292-
if (cd)
293-
rfree(cd->gtw_cfg);
294-
rfree(cd);
295-
296273
return 0;
297274
}
298275

src/audio/copier/copier_host.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ __cold static int init_pipeline_reg(struct comp_dev *dev)
131131
* Sof host component can support this case so copier reuses host
132132
* component to support host gateway.
133133
*/
134-
__cold int copier_host_create(struct comp_dev *dev, struct copier_data *cd,
134+
__cold int copier_host_create(struct processing_module *mod, struct copier_data *cd,
135135
const struct ipc4_copier_module_cfg *copier_cfg,
136136
struct pipeline *pipeline)
137137
{
138-
struct processing_module *mod = comp_mod(dev);
138+
struct comp_dev *dev = mod->dev;
139139
struct comp_ipc_config *config = &dev->ipc_config;
140140
struct ipc_config_host ipc_host;
141141
struct host_data *hd;
@@ -177,16 +177,17 @@ __cold int copier_host_create(struct comp_dev *dev, struct copier_data *cd,
177177
ipc_host.dma_buffer_size = copier_cfg->gtw_cfg.dma_buffer_size;
178178
ipc_host.feature_mask = copier_cfg->copier_feature_mask;
179179

180-
hd = rzalloc(SOF_MEM_FLAG_USER, sizeof(*hd));
180+
hd = mod_zalloc(mod, sizeof(*hd));
181181
if (!hd)
182182
return -ENOMEM;
183183

184184
ret = host_common_new(hd, dev, &ipc_host, config->id);
185185
if (ret < 0) {
186186
comp_err(dev, "copier: host new failed with exit");
187-
goto e_data;
187+
return ret;
188188
}
189189
#if CONFIG_HOST_DMA_STREAM_SYNCHRONIZATION
190+
/* NOTE: Should use goto e_conv this #if section, not direct return */
190191
/* Size of a configuration without optional parameters. */
191192
const uint32_t basic_size = sizeof(*copier_cfg) +
192193
(copier_cfg->gtw_cfg.config_length - 1) * sizeof(uint32_t);
@@ -248,8 +249,6 @@ __cold int copier_host_create(struct comp_dev *dev, struct copier_data *cd,
248249

249250
e_conv:
250251
host_common_free(hd);
251-
e_data:
252-
rfree(hd);
253252

254253
return ret;
255254
}
@@ -263,7 +262,6 @@ __cold void copier_host_free(struct copier_data *cd)
263262
delete_from_fpi_sync_group(cd->hd);
264263
#endif
265264
host_common_free(cd->hd);
266-
rfree(cd->hd);
267265
}
268266

269267
/* This is called by DMA driver every time when DMA completes its current

src/audio/copier/copier_ipcgtw.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// Copyright 2023 Intel Corporation. All rights reserved.
44

5+
#include <sof/audio/module_adapter/module/generic.h>
56
#include <sof/audio/component_ext.h>
67
#include <sof/trace/trace.h>
78
#include <sof/lib/memory.h>
@@ -207,15 +208,15 @@ void copier_ipcgtw_reset(struct comp_dev *dev)
207208
}
208209
}
209210

210-
__cold int copier_ipcgtw_create(struct comp_dev *dev, struct copier_data *cd,
211+
__cold int copier_ipcgtw_create(struct processing_module *mod, struct copier_data *cd,
211212
const struct ipc4_copier_module_cfg *copier,
212213
struct pipeline *pipeline)
213214
{
215+
struct comp_dev *dev = mod->dev;
214216
struct comp_ipc_config *config = &dev->ipc_config;
215217
struct ipcgtw_data *ipcgtw_data;
216218
const struct ipc4_copier_gateway_cfg *gtw_cfg;
217219
const struct ipc4_ipc_gateway_config_blob *blob;
218-
int ret;
219220

220221
assert_can_be_cold();
221222

@@ -231,7 +232,7 @@ __cold int copier_ipcgtw_create(struct comp_dev *dev, struct copier_data *cd,
231232
config->type = SOF_COMP_HOST;
232233
cd->gtw_type = ipc4_gtw_host;
233234

234-
ipcgtw_data = rzalloc(SOF_MEM_FLAG_USER, sizeof(*ipcgtw_data));
235+
ipcgtw_data = mod_zalloc(mod, sizeof(*ipcgtw_data));
235236
if (!ipcgtw_data)
236237
return -ENOMEM;
237238

@@ -254,8 +255,7 @@ __cold int copier_ipcgtw_create(struct comp_dev *dev, struct copier_data *cd,
254255
if (!cd->converter[IPC4_COPIER_GATEWAY_PIN]) {
255256
comp_err(dev, "failed to get converter for IPC gateway, dir %d",
256257
cd->direction);
257-
ret = -EINVAL;
258-
goto e_ipcgtw;
258+
return -EINVAL;
259259
}
260260

261261
if (cd->direction == SOF_IPC_STREAM_PLAYBACK) {
@@ -271,16 +271,11 @@ __cold int copier_ipcgtw_create(struct comp_dev *dev, struct copier_data *cd,
271271
cd->endpoint_num++;
272272

273273
return 0;
274-
275-
e_ipcgtw:
276-
rfree(ipcgtw_data);
277-
return ret;
278274
}
279275

280276
__cold void copier_ipcgtw_free(struct copier_data *cd)
281277
{
282278
assert_can_be_cold();
283279

284280
list_item_del(&cd->ipcgtw_data->item);
285-
rfree(cd->ipcgtw_data);
286281
}

src/audio/copier/host_copier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static inline int host_common_copy(struct host_data *hd, struct comp_dev *dev, c
129129
}
130130
void host_common_update(struct host_data *hd, struct comp_dev *dev, uint32_t bytes);
131131
void host_common_one_shot(struct host_data *hd, uint32_t bytes);
132-
int copier_host_create(struct comp_dev *dev, struct copier_data *cd,
132+
int copier_host_create(struct processing_module *mod, struct copier_data *cd,
133133
const struct ipc4_copier_module_cfg *copier_cfg,
134134
struct pipeline *pipeline);
135135
void copier_host_free(struct copier_data *cd);

src/audio/copier/ipcgtw_copier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ struct ipc4_ipc_gateway_cmd_data_reply {
9595
int copier_ipcgtw_process(const struct ipc4_ipcgtw_cmd *cmd,
9696
void *reply_payload, uint32_t *reply_payload_size);
9797

98-
int copier_ipcgtw_create(struct comp_dev *dev, struct copier_data *cd,
98+
int copier_ipcgtw_create(struct processing_module *mod, struct copier_data *cd,
9999
const struct ipc4_copier_module_cfg *copier, struct pipeline *pipeline);
100100

101101
#if CONFIG_IPC4_GATEWAY

0 commit comments

Comments
 (0)