1818#include <sof/audio/source_api.h>
1919#include <sof/audio/audio_buffer.h>
2020#include <sof/audio/pipeline.h>
21+ #include <sof/lib/vregion.h>
2122#include <sof/schedule/dp_schedule.h>
2223#include <sof/schedule/ll_schedule_domain.h>
2324#include <sof/common.h>
@@ -57,38 +58,29 @@ struct comp_dev *module_adapter_new(const struct comp_driver *drv,
5758#define PAGE_SZ HOST_PAGE_SIZE
5859#endif
5960
60- static struct k_heap * module_adapter_dp_heap_new (const struct comp_ipc_config * config ,
61- size_t * heap_size )
61+ static struct vregion * module_adapter_dp_heap_new (const struct comp_ipc_config * config ,
62+ size_t * heap_size )
6263{
6364 /* src-lite with 8 channels has been seen allocating 14k in one go */
6465 /* FIXME: the size will be derived from configuration */
6566 const size_t buf_size = 20 * 1024 ;
6667
67- /* Keep uncached to match the default SOF heap! */
68- uint8_t * mod_heap_mem = rballoc_align (SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT ,
69- buf_size , PAGE_SZ );
70-
71- if (!mod_heap_mem )
72- return NULL ;
73-
74- struct k_heap * mod_heap = (struct k_heap * )mod_heap_mem ;
75- const size_t heap_prefix_size = ALIGN_UP (sizeof (* mod_heap ), 4 );
76- void * mod_heap_buf = mod_heap_mem + heap_prefix_size ;
77-
78- * heap_size = buf_size - heap_prefix_size ;
79- k_heap_init (mod_heap , mod_heap_buf , * heap_size );
80- #ifdef __ZEPHYR__
81- mod_heap -> heap .init_mem = mod_heap_buf ;
82- mod_heap -> heap .init_bytes = * heap_size ;
83- #endif
84-
85- return mod_heap ;
68+ /*
69+ * A 1-to-1 replacement of the original heap implementation would be to
70+ * have "lifetime size" equal to 0. But (1) this is invalid for
71+ * vregion_create() and (2) we gradually move objects, that are simple
72+ * to move to the lifetime buffer. Make it 1k for the beginning.
73+ */
74+ return vregion_create (4096 , buf_size - 4096 );
8675}
8776
8877static struct processing_module * module_adapter_mem_alloc (const struct comp_driver * drv ,
8978 const struct comp_ipc_config * config )
9079{
9180 struct k_heap * mod_heap ;
81+ struct vregion * mod_vreg ;
82+ struct processing_module * mod ;
83+ struct comp_dev * dev ;
9284 /*
9385 * For DP shared modules the struct processing_module object must be
9486 * accessible from all cores. Unfortunately at this point there's no
@@ -102,17 +94,24 @@ static struct processing_module *module_adapter_mem_alloc(const struct comp_driv
10294
10395 if (config -> proc_domain == COMP_PROCESSING_DOMAIN_DP && IS_ENABLED (CONFIG_USERSPACE ) &&
10496 !IS_ENABLED (CONFIG_SOF_USERSPACE_USE_DRIVER_HEAP )) {
105- mod_heap = module_adapter_dp_heap_new (config , & heap_size );
106- if (!mod_heap ) {
107- comp_cl_err (drv , "Failed to allocate DP module heap" );
97+ mod_vreg = module_adapter_dp_heap_new (config , & heap_size );
98+ if (!mod_vreg ) {
99+ comp_cl_err (drv , "Failed to allocate DP module heap / vregion " );
108100 return NULL ;
109101 }
102+ mod_heap = NULL ;
110103 } else {
111104 mod_heap = drv -> user_heap ;
112105 heap_size = 0 ;
106+ mod_vreg = NULL ;
113107 }
114108
115- struct processing_module * mod = sof_heap_alloc (mod_heap , flags , sizeof (* mod ), 0 );
109+ if (!mod_vreg )
110+ mod = sof_heap_alloc (mod_heap , flags , sizeof (* mod ), 0 );
111+ else if (flags & SOF_MEM_FLAG_COHERENT )
112+ mod = vregion_alloc_coherent (mod_vreg , VREGION_MEM_TYPE_LIFETIME , sizeof (* mod ));
113+ else
114+ mod = vregion_alloc (mod_vreg , VREGION_MEM_TYPE_LIFETIME , sizeof (* mod ));
116115
117116 if (!mod ) {
118117 comp_cl_err (drv , "failed to allocate memory for module" );
@@ -123,6 +122,7 @@ static struct processing_module *module_adapter_mem_alloc(const struct comp_driv
123122
124123 memset (mod , 0 , sizeof (* mod ));
125124 alloc -> heap = mod_heap ;
125+ alloc -> vreg = mod_vreg ;
126126 mod_resource_init (mod );
127127
128128 /*
@@ -131,7 +131,10 @@ static struct processing_module *module_adapter_mem_alloc(const struct comp_driv
131131 * then it can be cached. Effectively it can be only cached in
132132 * single-core configurations.
133133 */
134- struct comp_dev * dev = sof_heap_alloc (mod_heap , SOF_MEM_FLAG_COHERENT , sizeof (* dev ), 0 );
134+ if (mod_vreg )
135+ dev = vregion_alloc_coherent (mod_vreg , VREGION_MEM_TYPE_LIFETIME , sizeof (* dev ));
136+ else
137+ dev = sof_heap_alloc (mod_heap , SOF_MEM_FLAG_COHERENT , sizeof (* dev ), 0 );
135138
136139 if (!dev ) {
137140 comp_cl_err (drv , "failed to allocate memory for comp_dev" );
@@ -144,15 +147,19 @@ static struct processing_module *module_adapter_mem_alloc(const struct comp_driv
144147 mod -> dev = dev ;
145148 dev -> mod = mod ;
146149
147- if (config -> proc_domain == COMP_PROCESSING_DOMAIN_DP )
148- alloc -> client_count ++ ;
150+ if (mod_vreg )
151+ mod -> priv . resources . alloc . client_count ++ ;
149152
150153 return mod ;
151154
152155err :
153- sof_heap_free (mod_heap , mod );
156+ if (mod_vreg )
157+ vregion_free (mod_vreg , mod );
158+ else
159+ sof_heap_free (mod_heap , mod );
154160emod :
155- rfree (mod_heap );
161+ if (mod_vreg )
162+ vregion_destroy (mod_vreg );
156163
157164 return NULL ;
158165}
@@ -161,7 +168,6 @@ static void module_adapter_mem_free(struct processing_module *mod)
161168{
162169 struct sof_alloc_api * alloc = & mod -> priv .resources .alloc ;
163170 struct k_heap * mod_heap = alloc -> heap ;
164- unsigned int domain = mod -> dev -> ipc_config .proc_domain ;
165171
166172 /*
167173 * In principle it shouldn't even be needed to free individual objects
@@ -170,10 +176,17 @@ static void module_adapter_mem_free(struct processing_module *mod)
170176#if CONFIG_IPC_MAJOR_4
171177 sof_heap_free (mod_heap , mod -> priv .cfg .input_pins );
172178#endif
173- sof_heap_free (mod_heap , mod -> dev );
174- sof_heap_free (mod_heap , mod );
175- if (domain == COMP_PROCESSING_DOMAIN_DP && mod_heap && !-- alloc -> client_count )
176- rfree (mod_heap );
179+ if (alloc -> vreg ) {
180+ struct vregion * mod_vreg = alloc -> vreg ;
181+
182+ vregion_free (mod_vreg , mod -> dev );
183+ vregion_free (mod_vreg , mod );
184+ if (!-- mod -> priv .resources .alloc .client_count )
185+ vregion_destroy (mod_vreg );
186+ } else {
187+ sof_heap_free (mod_heap , mod -> dev );
188+ sof_heap_free (mod_heap , mod );
189+ }
177190}
178191
179192/*
@@ -617,8 +630,7 @@ int module_adapter_prepare(struct comp_dev *dev)
617630 goto free ;
618631 }
619632
620- if (md -> resources .alloc .heap &&
621- md -> resources .alloc .heap != dev -> drv -> user_heap )
633+ if (md -> resources .alloc .vreg )
622634 md -> resources .alloc .client_count ++ ;
623635
624636 irq_local_disable (flags );
0 commit comments