1616#include <rtos/interrupt.h>
1717#include <rtos/alloc.h>
1818#include <rtos/cache.h>
19+ #include <sof/lib/vregion.h>
1920#include <sof/list.h>
2021#include <sof/schedule/dp_schedule.h>
2122#include <rtos/spinlock.h>
@@ -153,15 +154,19 @@ static void comp_buffer_free(struct sof_audio_buffer *audio_buffer)
153154 buffer -> probe_cb_free (buffer -> probe_cb_arg );
154155#endif
155156
156- struct k_heap * heap = buffer -> audio_buffer .heap ;
157+ struct mod_alloc_ctx * alloc = buffer -> audio_buffer .alloc ;
157158
158159 rfree (buffer -> stream .addr );
159- sof_heap_free (heap , buffer );
160- if (heap ) {
161- struct dp_heap_user * mod_heap_user = container_of (heap , struct dp_heap_user , heap );
160+ if (alloc && alloc -> vreg )
161+ vregion_free (alloc -> vreg , buffer );
162+ else
163+ sof_heap_free (alloc ? alloc -> heap : NULL , buffer );
162164
163- if (!-- mod_heap_user -> client_count )
164- rfree (mod_heap_user );
165+ if (alloc && alloc -> client && !-- alloc -> client -> client_count ) {
166+ rfree (alloc -> client );
167+ alloc -> client = NULL ;
168+ /* NULL is allowed */
169+ vregion_put (alloc -> vreg );
165170 }
166171}
167172
@@ -192,7 +197,7 @@ static const struct audio_buffer_ops audio_buffer_ops = {
192197 .set_alignment_constants = comp_buffer_set_alignment_constants ,
193198};
194199
195- static struct comp_buffer * buffer_alloc_struct (struct k_heap * heap ,
200+ static struct comp_buffer * buffer_alloc_struct (struct mod_alloc_ctx * alloc ,
196201 void * stream_addr , size_t size ,
197202 uint32_t flags , bool is_shared )
198203{
@@ -204,7 +209,12 @@ static struct comp_buffer *buffer_alloc_struct(struct k_heap *heap,
204209 if (is_shared )
205210 flags |= SOF_MEM_FLAG_COHERENT ;
206211
207- buffer = sof_heap_alloc (heap , flags , sizeof (* buffer ), 0 );
212+ if (!alloc || !alloc -> vreg )
213+ buffer = sof_heap_alloc (alloc ? alloc -> heap : NULL , flags , sizeof (* buffer ), 0 );
214+ else if (is_shared )
215+ buffer = vregion_alloc_coherent (alloc -> vreg , VREGION_MEM_TYPE_INTERIM , sizeof (* buffer ));
216+ else
217+ buffer = vregion_alloc (alloc -> vreg , VREGION_MEM_TYPE_INTERIM , sizeof (* buffer ));
208218 if (!buffer ) {
209219 tr_err (& buffer_tr , "could not alloc structure" );
210220 return NULL ;
@@ -226,16 +236,16 @@ static struct comp_buffer *buffer_alloc_struct(struct k_heap *heap,
226236
227237 audio_stream_set_underrun (& buffer -> stream , !!(flags & SOF_BUF_UNDERRUN_PERMITTED ));
228238 audio_stream_set_overrun (& buffer -> stream , !!(flags & SOF_BUF_OVERRUN_PERMITTED ));
229- buffer -> audio_buffer .heap = heap ;
239+ buffer -> audio_buffer .alloc = alloc ;
230240
231241 comp_buffer_reset_source_list (buffer );
232242 comp_buffer_reset_sink_list (buffer );
233243
234244 return buffer ;
235245}
236246
237- struct comp_buffer * buffer_alloc (struct k_heap * heap , size_t size , uint32_t flags , uint32_t align ,
238- bool is_shared )
247+ struct comp_buffer * buffer_alloc (struct mod_alloc_ctx * alloc , size_t size , uint32_t flags ,
248+ uint32_t align , bool is_shared )
239249{
240250 struct comp_buffer * buffer ;
241251 void * stream_addr ;
@@ -255,7 +265,7 @@ struct comp_buffer *buffer_alloc(struct k_heap *heap, size_t size, uint32_t flag
255265 return NULL ;
256266 }
257267
258- buffer = buffer_alloc_struct (heap , stream_addr , size , flags , is_shared );
268+ buffer = buffer_alloc_struct (alloc , stream_addr , size , flags , is_shared );
259269 if (!buffer ) {
260270 tr_err (& buffer_tr , "could not alloc buffer structure" );
261271 rfree (stream_addr );
@@ -264,7 +274,7 @@ struct comp_buffer *buffer_alloc(struct k_heap *heap, size_t size, uint32_t flag
264274 return buffer ;
265275}
266276
267- struct comp_buffer * buffer_alloc_range (struct k_heap * heap , size_t preferred_size ,
277+ struct comp_buffer * buffer_alloc_range (struct mod_alloc_ctx * alloc , size_t preferred_size ,
268278 size_t minimum_size ,
269279 uint32_t flags , uint32_t align , bool is_shared )
270280{
@@ -299,7 +309,7 @@ struct comp_buffer *buffer_alloc_range(struct k_heap *heap, size_t preferred_siz
299309 return NULL ;
300310 }
301311
302- buffer = buffer_alloc_struct (heap , stream_addr , size , flags , is_shared );
312+ buffer = buffer_alloc_struct (alloc , stream_addr , size , flags , is_shared );
303313 if (!buffer ) {
304314 tr_err (& buffer_tr , "could not alloc buffer structure" );
305315 rfree (stream_addr );
0 commit comments