comparison nuc-fw/riviera/rvm/rvm_mem.c @ 118:21de8d8e6ea7

checking in Riviera code from the Sotomodem version
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 29 Oct 2013 07:03:45 +0000
parents
children 15e972110527
comparison
equal deleted inserted replaced
117:e40d8661ecab 118:21de8d8e6ea7
1 /**
2 *
3 * @file rvm_mem.c
4 *
5 * This file contains the functions related to memory management within RVM.
6 *
7 * @author David Lamy-Charrier (d-lamy@ti.com)
8 * @version 0.1
9 *
10 */
11
12 /*
13 * Revision History:
14 *
15 * 10/26/2001 David Lamy-Charrier Create for Riviera 1.6.
16 *
17 * (C) Copyright 2001 by Texas Instruments Incorporated, All Rights Reserved
18 */
19
20 #include "rvm/rvm_i.h"
21 #include "rvm/rvm_gen.h"
22 #include "rvm/rvm_api.h"
23 #include "rvm/rvm_use_id_list.h"
24 #include "rvf/rvf_env.h"
25
26 extern T_RVM_GET_INFO_FUNC RVM_SWE_GET_INFO_ARRAY[];
27
28 extern T_RVM_KNOWN_SWE * rvm_swe_array;
29
30 /*******************************************************************************
31 ** Function rvm_delete_used_memory
32 **
33 ** Description Internal function which deletes used local mem if an error
34 ** was received or at the end of the start/stop process.
35 **
36 *******************************************************************************/
37 T_RVM_RETURN rvm_delete_used_memory ( T_RVM_PROCESSING_SWE * appli)
38 {
39 T_RVF_BUFFER_Q buffer_to_free_q = {0, 0, 0};
40 T_RVM_PROCESSING_SWE * cur_elem = appli;
41
42 while (cur_elem != NULL)
43 {
44 rvf_enqueue (&buffer_to_free_q, cur_elem);
45 cur_elem = cur_elem->next_swe;
46 }
47
48 while (buffer_to_free_q.p_first)
49 {
50 rvf_free_buf (rvf_dequeue (&buffer_to_free_q));
51 }
52
53 return RVM_OK;
54 }
55
56
57 /*******************************************************************************
58 ** Function rvm_delete_created_mb
59 **
60 ** Description Internal function which deletes all created MB if an error
61 ** was received, or in case application has to be stopped.
62 **
63 *******************************************************************************/
64 T_RVM_RETURN rvm_delete_created_mb (T_RVM_PROCESSING_SWE * appli)
65 {
66 UINT8 mb_index;
67 T_RVM_PROCESSING_SWE * cur_elem = appli;
68 volatile T_RV_RET ret_value = RV_OK;
69 UINT8 mb_to_delete;
70
71 while (cur_elem != NULL )
72 {
73 UINT8 swe_index = cur_elem->swe_id;
74
75 /* If more than one appli is using this SWE, cannot delete MB
76 Process to the next SWE. */
77 if (rvm_swe_array[swe_index].nb_using_appli > 1)
78 {
79 cur_elem = cur_elem->next_swe; /* process the next SWE */
80 continue;
81 }
82
83 /* If the state is running, it means that this SWE has not
84 to be stopped. */
85 if (rvm_swe_array[swe_index].swe_state == SWE_RUNNING)
86 {
87 cur_elem = cur_elem->next_swe; /* process the next SWE */
88 continue;
89 }
90
91 /* We're here:
92 - either because swe_state == SWE_NOT_STARTED => error in starting prcess
93 - either because swe_state == SWE_STOPPING => regular stopping process */
94
95 if (cur_elem->nb_created_mb == 0)
96 {
97 cur_elem = cur_elem->next_swe; /* process the next SWE */
98 continue;
99 }
100
101 mb_to_delete = cur_elem->nb_created_mb;
102
103 for( mb_index = 0; mb_index < mb_to_delete; mb_index++)
104 {
105 ret_value = rvf_delete_mb(cur_elem->swe_mem_bank[mb_index].mb_name);
106 if (ret_value != RV_OK)
107 {
108 rvf_send_trace("RVM: Error in deletion of memory bank: ", 39, NULL_PARAM, RV_TRACE_LEVEL_WARNING, RVM_USE_ID );
109 rvf_send_trace(cur_elem->swe_mem_bank[mb_index].mb_name, RVF_MAX_MB_LEN, NULL_PARAM, RV_TRACE_LEVEL_WARNING, RVM_USE_ID );
110 }
111 else
112 {
113 (cur_elem->nb_created_mb)--;
114 }
115
116 ret_value = RVM_OK;
117 }
118
119 cur_elem = cur_elem->next_swe; /* process the next SWE */
120 }
121
122 return ret_value;
123 }
124
125 /*******************************************************************************
126 **
127 ** Function rvm_check_memory_requirement
128 **
129 ** Description This function checks if there is enough memory
130 ** to start a SWE(and all the linked SWEs)
131 **
132 ** Parameters: T_RVM_PROCESSING_SWE * appli: list of required SWEs with their parameters.
133 **
134 ** Returns T_RVM_RETURN: RVM_OK if there is enough memory, else RVM_MEMORY_ERR.
135 **
136 *******************************************************************************/
137 T_RVM_RETURN rvm_verify_memory_requirement( T_RVM_PROCESSING_SWE * appli,
138 T_RVM_GROUP_DIRECTIVE* gd,
139 UINT8 cnt) {
140 T_RVM_PROCESSING_SWE * cur_swe = appli;
141 UINT32 required_mem = 0;
142 UINT32 total_mem = 0;
143 UINT32 used_mem = 0;
144 UINT8 mb_index;
145 UINT8 i=0;
146 UINT16 host_task_mem=0;
147
148
149 /* get available memory from the rvf */
150 if ( rvf_get_available_mem( &total_mem, &used_mem) != RVF_OK )
151 { return RVM_MEMORY_ERR;
152 }
153
154 /* count required memory */
155 while (cur_swe !=NULL ) /* for each SWE */
156 {
157 UINT8 swe_index = cur_swe->swe_id;
158
159 /*
160 ** If SWE is already running => MB already created => do nothing
161 */
162 if (rvm_swe_array[swe_index].swe_state != SWE_RUNNING )
163 {
164 for( mb_index = 0; mb_index < cur_swe->nb_requested_mb; mb_index++) /* for each mb */
165 {
166 required_mem += cur_swe->swe_mem_bank[mb_index].mb_initial_param.size;
167 }
168
169 /* add the necessary stack sizes */
170 /* TO DO: add the stack size for host groups not yet started */
171 if( (cur_swe->swe_type == RVM_SWE_TYPE_3)
172 || (cur_swe->swe_type == RVM_SWE_TYPE_4) )
173 /* || ((cur_swe->swe_type == RVM_SWE_TYPE_2) && (rvm_swe_array[swe_index].group_index == RVM_OWN_GROUP) )) */
174 {
175 required_mem += rvm_swe_array[swe_index].swe_stack_size;
176 required_mem += SYSTEM_TASK_MEM; /* only for type 3 & 4. A-M-E-N-D-E-D! */
177 }
178
179 }
180 cur_swe = cur_swe->next_swe;
181 }
182
183 /* type 2 group host system and stack mem. is catered for here */
184 for(i=0; i<cnt; i++) host_task_mem+=gd[i].stack_size; /* A-M-E-N-D-E-D! */
185 host_task_mem+=(cnt*SYSTEM_TASK_MEM);
186
187 /* compare available memory and required memory (eventually, use a percentage to improve performances) */
188 if ((required_mem+host_task_mem) + used_mem > total_mem ) { /* A-M-E-N-D-E-D! */
189 RVM_TRACE_WARNING_PARAM("RVM: Memory required (incl. used): ", (UINT32)(required_mem+host_task_mem+ used_mem) );
190 RVM_TRACE_WARNING_PARAM("RVM: Total Memory available : ", (UINT32)total_mem);
191 return RVM_MEMORY_ERR;
192 } else{
193 return RVM_OK;
194 }
195 }
196
197
198 /*******************************************************************************
199 **
200 ** Function rvm_allocate_mem
201 **
202 ** Description This function creates all the required memory banks or
203 ** increases their size if needed, to start a SWE.
204 **
205 ** Parameters: T_RVM_PROCESSING_SWE * appli: list of required SWEs with their parameters.
206 **
207 ** Returns T_RVM_RETURN: RVM_OK if all allocations are successful,
208 ** else RVM_MEMORY_ERR and it releases all the allocated memory.
209 **
210 *******************************************************************************/
211 T_RVM_RETURN rvm_allocate_mb( T_RVM_PROCESSING_SWE * appli)
212 { T_RVM_PROCESSING_SWE * cur_swe = appli;
213 UINT8 mb_index;
214 volatile T_RVM_RETURN rvm_ret_value = RVM_OK;
215
216 /* for each SWE in the list */
217 while ((cur_swe != NULL ) && (rvm_ret_value == RVM_OK))
218 {
219 UINT8 swe_index = cur_swe->swe_id;
220
221 if (rvm_swe_array[swe_index].swe_state == SWE_NOT_STARTED)
222 /* If the state is not SWE_RUNNING, then the MBs have to be created */
223 {
224 for( mb_index = 0; mb_index < cur_swe->nb_requested_mb; mb_index++) /* for each mb */
225 { T_RVF_MB_PARAM mb_param;
226 /* add the initial size */
227 mb_param.size = cur_swe->swe_mem_bank[mb_index].mb_initial_param.size;
228 mb_param.watermark = cur_swe->swe_mem_bank[mb_index].mb_initial_param.watermark;
229
230 /* create the mb */
231 if ( mb_param.size != 0)
232 {
233 /* create the mb */
234 if ( rvf_create_mb( cur_swe->swe_mem_bank[mb_index].mb_name, mb_param, &(cur_swe->bk_id_table[mb_index]) ) != RVF_OK)
235 {
236 /* if an error occurs */
237 rvm_ret_value = RVM_MEMORY_ERR;
238 break;
239 }
240 else
241 {
242 cur_swe->nb_created_mb++;
243 }
244 }
245 }
246 }
247
248 cur_swe = cur_swe->next_swe; /* process the next SWE */
249 }
250
251 if (rvm_ret_value != RVM_OK)
252 /* Something went wrong, should release all used memory */
253 {
254 rvf_send_trace("RVM: Problem in memory bank creation !!!", 40, NULL_PARAM, RV_TRACE_LEVEL_WARNING, RVM_USE_ID );
255 if (rvm_delete_created_mb (appli) != RVM_OK)
256 {
257 rvf_send_trace("RVM: MB deleting error!!!", 25, NULL_PARAM, RV_TRACE_LEVEL_WARNING, RVM_USE_ID );
258 }
259
260 }
261 return rvm_ret_value;
262 }
263
264
265 /*******************************************************************************
266 **
267 ** Function rvm_allocate_stack_buffer
268 **
269 ** Description This function allocates a buffer for the stack of a new
270 ** task created by RVM.
271 **
272 ** Parameters: UINT32 stack_size: size of stack.
273 ** T_RVF_BUFFER** stack_ptr: pointer to the allocated buffer.
274 **
275 ** Returns T_RVM_RETURN: RVM_OK if all allocation is successful,
276 ** else RVM_MEMORY_ERR.
277 **
278 *******************************************************************************/
279 T_RVM_RETURN rvm_allocate_stack_buffer( UINT32 stack_size, T_RVF_BUFFER** stack_ptr )
280 { T_RVF_MB_PARAM mb_params;
281
282 /* increase the size of the stack MB before allocating the new buffer */
283 if( rvf_get_mb_param( RVM_STACK_MB, &mb_params) != RV_OK)
284 { return RVM_MEMORY_ERR;
285 }
286
287 mb_params.size += stack_size;
288 mb_params.watermark += stack_size;
289
290 if( rvf_set_mb_param( RVM_STACK_MB, &mb_params) != RV_OK)
291 { return RVM_MEMORY_ERR;
292 }
293
294 if( rvf_get_buf( rvm_stack_mem_bank, stack_size, stack_ptr) == RVF_RED)
295 { return RVM_MEMORY_ERR;
296 }
297 return RVM_OK;
298
299 }
300