@@ -74,28 +74,65 @@ struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv,
7474 return NULL ;
7575 }
7676
77- dev = comp_alloc (drv , sizeof (* dev ));
78- if (!dev ) {
79- comp_cl_err (drv , "module_adapter_new(), failed to allocate memory for comp_dev" );
80- return NULL ;
77+ uint8_t * mod_heap_mem ;
78+ struct k_heap * mod_heap ;
79+ int flags ;
80+
81+ if (config -> proc_domain == COMP_PROCESSING_DOMAIN_DP ) {
82+ const size_t heap_size = 8 * 1024 ;
83+
84+ /* Keep uncached to match the default SOF heap! */
85+ mod_heap_mem = rballoc_align (SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT ,
86+ heap_size , 4096 );
87+ if (!mod_heap_mem )
88+ return NULL ;
89+
90+ const size_t heap_pref_size = ALIGN_UP (sizeof (* mod_heap ), 8 );
91+ void * mod_heap_buf = mod_heap_mem + heap_pref_size ;
92+
93+ mod_heap = (struct k_heap * )mod_heap_mem ;
94+ k_heap_init (mod_heap , mod_heap_buf , heap_size - heap_pref_size );
95+
96+ flags = SOF_MEM_FLAG_COHERENT ;
97+ } else {
98+ mod_heap_mem = NULL ;
99+ mod_heap = NULL ;
100+
101+ flags = 0 ;
81102 }
82- dev -> ipc_config = * config ;
83103
84104 /* allocate module information.
85105 * for DP shared modules this struct must be accessible from all cores
86106 * Unfortunately at this point there's no information of components the module
87107 * will be bound to. So we need to allocate shared memory for each DP module
88108 * To be removed when pipeline 2.0 is ready
89109 */
90- int flags = config -> proc_domain == COMP_PROCESSING_DOMAIN_DP ?
91- SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT : SOF_MEM_FLAG_USER ;
92110
93- mod = rzalloc ( flags , sizeof (* mod ));
111+ mod = sof_heap_alloc ( mod_heap , flags , sizeof (* mod ), 0 );
94112 if (!mod ) {
95- comp_err (dev , "module_adapter_new(), failed to allocate memory for module" );
113+ comp_cl_err (drv , "module_adapter_new(), failed to allocate memory for module" );
114+ goto emod ;
115+ }
116+
117+ memset (mod , 0 , sizeof (* mod ));
118+ mod -> priv .resources .heap = mod_heap ;
119+ mod -> priv .resources .heap_mem = mod_heap_mem ;
120+
121+ /*
122+ * comp_alloc() always allocated dev uncached. Would be difficult to optimize. Only
123+ * if the whole currently active topology is running on the primary core, then it
124+ * can be cached. Effectively it can be only cached in single-core configurations.
125+ */
126+ dev = sof_heap_alloc (mod_heap , SOF_MEM_FLAG_COHERENT , sizeof (* dev ), 0 );
127+ if (!dev ) {
128+ comp_cl_err (drv , "module_adapter_new(), failed to allocate memory for comp_dev" );
96129 goto err ;
97130 }
98131
132+ memset (dev , 0 , sizeof (* dev ));
133+ comp_init (drv , dev , sizeof (* dev ));
134+ dev -> ipc_config = * config ;
135+
99136 dst = & mod -> priv .cfg ;
100137
101138 module_set_private_data (mod , mod_priv );
@@ -159,13 +196,17 @@ struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv,
159196
160197 comp_dbg (dev , "module_adapter_new() done" );
161198 return dev ;
199+
162200err :
163201#if CONFIG_IPC_MAJOR_4
164202 if (mod )
165203 rfree (mod -> priv .cfg .input_pins );
166204#endif
167- rfree (mod );
168- rfree (dev );
205+ sof_heap_free (mod_heap , dev );
206+ sof_heap_free (mod_heap , mod );
207+ emod :
208+ rfree (mod_heap_mem );
209+
169210 return NULL ;
170211}
171212EXPORT_SYMBOL (module_adapter_new );
@@ -1285,8 +1326,13 @@ void module_adapter_free(struct comp_dev *dev)
12851326#endif
12861327
12871328 rfree (mod -> stream_params );
1288- rfree (mod );
1289- rfree (dev );
1329+
1330+ struct k_heap * mod_heap = mod -> priv .resources .heap ;
1331+ void * mem = mod -> priv .resources .heap_mem ;
1332+
1333+ sof_heap_free (mod_heap , mod );
1334+ sof_heap_free (mod_heap , dev );
1335+ rfree (mem );
12901336}
12911337EXPORT_SYMBOL (module_adapter_free );
12921338
0 commit comments