1717#include <rtos/alloc.h>
1818#include <rtos/cache.h>
1919#include <sof/lib/notifier.h>
20+ #include <sof/lib/vregion.h>
2021#include <sof/list.h>
2122#include <sof/schedule/dp_schedule.h>
2223#include <rtos/spinlock.h>
@@ -159,15 +160,19 @@ static void comp_buffer_free(struct sof_audio_buffer *audio_buffer)
159160 /* In case some listeners didn't unregister from buffer's callbacks */
160161 notifier_unregister_all (NULL , buffer );
161162
162- struct k_heap * heap = buffer -> audio_buffer .heap ;
163+ struct mod_alloc_ctx * alloc = buffer -> audio_buffer .alloc ;
163164
164165 rfree (buffer -> stream .addr );
165- sof_heap_free (heap , buffer );
166- if (heap ) {
167- struct dp_heap_user * mod_heap_user = container_of (heap , struct dp_heap_user , heap );
166+ if (alloc && alloc -> vreg )
167+ vregion_free (alloc -> vreg , buffer );
168+ else
169+ sof_heap_free (alloc ? alloc -> heap : NULL , buffer );
168170
169- if (!-- mod_heap_user -> client_count )
170- rfree (mod_heap_user );
171+ if (alloc && alloc -> client && !-- alloc -> client -> client_count ) {
172+ rfree (alloc -> client );
173+ alloc -> client = NULL ;
174+ /* NULL is allowed */
175+ vregion_put (alloc -> vreg );
171176 }
172177}
173178
@@ -198,7 +203,7 @@ static const struct audio_buffer_ops audio_buffer_ops = {
198203 .set_alignment_constants = comp_buffer_set_alignment_constants ,
199204};
200205
201- static struct comp_buffer * buffer_alloc_struct (struct k_heap * heap ,
206+ static struct comp_buffer * buffer_alloc_struct (struct mod_alloc_ctx * alloc ,
202207 void * stream_addr , size_t size ,
203208 uint32_t flags , bool is_shared )
204209{
@@ -210,7 +215,12 @@ static struct comp_buffer *buffer_alloc_struct(struct k_heap *heap,
210215 if (is_shared )
211216 flags |= SOF_MEM_FLAG_COHERENT ;
212217
213- buffer = sof_heap_alloc (heap , flags , sizeof (* buffer ), 0 );
218+ if (!alloc || !alloc -> vreg )
219+ buffer = sof_heap_alloc (alloc ? alloc -> heap : NULL , flags , sizeof (* buffer ), 0 );
220+ else if (is_shared )
221+ buffer = vregion_alloc_coherent (alloc -> vreg , VREGION_MEM_TYPE_INTERIM , sizeof (* buffer ));
222+ else
223+ buffer = vregion_alloc (alloc -> vreg , VREGION_MEM_TYPE_INTERIM , sizeof (* buffer ));
214224 if (!buffer ) {
215225 tr_err (& buffer_tr , "could not alloc structure" );
216226 return NULL ;
@@ -232,16 +242,16 @@ static struct comp_buffer *buffer_alloc_struct(struct k_heap *heap,
232242
233243 audio_stream_set_underrun (& buffer -> stream , !!(flags & SOF_BUF_UNDERRUN_PERMITTED ));
234244 audio_stream_set_overrun (& buffer -> stream , !!(flags & SOF_BUF_OVERRUN_PERMITTED ));
235- buffer -> audio_buffer .heap = heap ;
245+ buffer -> audio_buffer .alloc = alloc ;
236246
237247 comp_buffer_reset_source_list (buffer );
238248 comp_buffer_reset_sink_list (buffer );
239249
240250 return buffer ;
241251}
242252
243- struct comp_buffer * buffer_alloc (struct k_heap * heap , size_t size , uint32_t flags , uint32_t align ,
244- bool is_shared )
253+ struct comp_buffer * buffer_alloc (struct mod_alloc_ctx * alloc , size_t size , uint32_t flags ,
254+ uint32_t align , bool is_shared )
245255{
246256 struct comp_buffer * buffer ;
247257 void * stream_addr ;
@@ -261,7 +271,7 @@ struct comp_buffer *buffer_alloc(struct k_heap *heap, size_t size, uint32_t flag
261271 return NULL ;
262272 }
263273
264- buffer = buffer_alloc_struct (heap , stream_addr , size , flags , is_shared );
274+ buffer = buffer_alloc_struct (alloc , stream_addr , size , flags , is_shared );
265275 if (!buffer ) {
266276 tr_err (& buffer_tr , "could not alloc buffer structure" );
267277 rfree (stream_addr );
@@ -270,7 +280,7 @@ struct comp_buffer *buffer_alloc(struct k_heap *heap, size_t size, uint32_t flag
270280 return buffer ;
271281}
272282
273- struct comp_buffer * buffer_alloc_range (struct k_heap * heap , size_t preferred_size ,
283+ struct comp_buffer * buffer_alloc_range (struct mod_alloc_ctx * alloc , size_t preferred_size ,
274284 size_t minimum_size ,
275285 uint32_t flags , uint32_t align , bool is_shared )
276286{
@@ -305,7 +315,7 @@ struct comp_buffer *buffer_alloc_range(struct k_heap *heap, size_t preferred_siz
305315 return NULL ;
306316 }
307317
308- buffer = buffer_alloc_struct (heap , stream_addr , size , flags , is_shared );
318+ buffer = buffer_alloc_struct (alloc , stream_addr , size , flags , is_shared );
309319 if (!buffer ) {
310320 tr_err (& buffer_tr , "could not alloc buffer structure" );
311321 rfree (stream_addr );
0 commit comments