summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/freescale/fman/inc/etc/mm_ext.h
blob: fa7c85e3b2b8982839b397d1b890c173c904ea28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* Copyright (c) 2008-2012 Freescale Semiconductor, Inc
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Freescale Semiconductor nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 *
 * ALTERNATIVELY, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") as published by the Free Software
 * Foundation, either version 2 of that License or (at your option) any
 * later version.
 *
 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


/**************************************************************************//**
 @File          mm_ext.h

 @Description   Memory Manager Application Programming Interface
*//***************************************************************************/
#ifndef __MM_EXT
#define __MM_EXT

#include "std_ext.h"

#define MM_MAX_ALIGNMENT    20  /* Alignments from 2 to 128 are available
                                   where maximum alignment defined as
                                   MM_MAX_ALIGNMENT power of 2 */

#define MM_MAX_NAME_LEN     32

/**************************************************************************//**
 @Group         etc_id   Utility Library Application Programming Interface

 @Description   External routines.

 @{
*//***************************************************************************/

/**************************************************************************//**
 @Group         mm_grp Flexible Memory Manager

 @Description   Flexible Memory Manager module functions,definitions and enums.
                (All of the following functions,definitions and enums can be found in mm_ext.h)

 @{
*//***************************************************************************/


/**************************************************************************//**
 @Function      MM_Init

 @Description   Initializes a new MM object.

                It initializes a new memory block consisting of base address
                and size of the available memory by calling to MemBlock_Init
                routine. It is also initializes a new free block for each
                by calling FreeBlock_Init routine, which is pointed to
                the almost all memory started from the required alignment
                from the base address and to the end of the memory.
                The handle to the new MM object is returned via "MM"
                argument (passed by reference).

 @Param[in]     h_MM    - Handle to the MM object.
 @Param[in]     base    - Base address of the MM.
 @Param[in]     size    - Size of the MM.

 @Return        E_OK is returned on success. E_NOMEMORY is returned if the new MM object or a new free block can not be initialized.
*//***************************************************************************/
t_Error     MM_Init(t_Handle *h_MM, uint64_t base, uint64_t size);

/**************************************************************************//**
 @Function      MM_Get

 @Description   Allocates a block of memory according to the given size and the alignment.

                The Alignment argument tells from which
                free list allocate a block of memory. 2^alignment indicates
                the alignment that the base address of the allocated block
                should have. So, the only values 1, 2, 4, 8, 16, 32 and 64
                are available for the alignment argument.
                The routine passes through the specific free list of free
                blocks and seeks for a first block that have anough memory
                that  is required (best fit).
                After the block is found and data is allocated, it calls
                the internal MM_CutFree routine to update all free lists
                do not include a just allocated block. Of course, each
                free list contains a free blocks with the same alignment.
                It is also creates a busy block that holds
                information about an allocated block.

 @Param[in]     h_MM        - Handle to the MM object.
 @Param[in]     size        - Size of the MM.
 @Param[in]     alignment   - Index as a power of two defines a required
                              alignment (in bytes); Should be 1, 2, 4, 8, 16, 32 or 64
 @Param[in]     name        - The name that specifies an allocated block.

 @Return        base address of an allocated block ILLEGAL_BASE if can't allocate a block
*//***************************************************************************/
uint64_t    MM_Get(t_Handle h_MM, uint64_t size, uint64_t alignment, char *name);

/**************************************************************************//**
 @Function      MM_GetBase

 @Description   Gets the base address of the required MM objects.

 @Param[in]     h_MM - Handle to the MM object.

 @Return        base address of the block.
*//***************************************************************************/
uint64_t    MM_GetBase(t_Handle h_MM);

/**************************************************************************//**
 @Function      MM_GetForce

 @Description   Force memory allocation.

                It means to allocate a block of memory of the given
                size from the given base address.
                The routine checks if the required block can be allocated
                (that is it is free) and then, calls the internal MM_CutFree
                routine to update all free lists do not include that block.

 @Param[in]     h_MM    - Handle to the MM object.
 @Param[in]     base    - Base address of the MM.
 @Param[in]     size    - Size of the MM.
 @Param[in]     name    - Name that specifies an allocated block.

 @Return        base address of an allocated block, ILLEGAL_BASE if can't allocate a block.
*//***************************************************************************/
uint64_t    MM_GetForce(t_Handle h_MM, uint64_t base, uint64_t size, char *name);

/**************************************************************************//**
 @Function      MM_GetForceMin

 @Description   Allocates a block of memory according to the given size, the alignment and minimum base address.

                The Alignment argument tells from which
                free list allocate a block of memory. 2^alignment indicates
                the alignment that the base address of the allocated block
                should have. So, the only values 1, 2, 4, 8, 16, 32 and 64
                are available for the alignment argument.
                The minimum baser address forces the location of the block
                to be from a given address onward.
                The routine passes through the specific free list of free
                blocks and seeks for the first base address equal or smaller
                than the required minimum address and end address larger than
                than the required base + its size - i.e. that may contain
                the required block.
                After the block is found and data is allocated, it calls
                the internal MM_CutFree routine to update all free lists
                do not include a just allocated block. Of course, each
                free list contains a free blocks with the same alignment.
                It is also creates a busy block that holds
                information about an allocated block.

 @Param[in]     h_MM        - Handle to the MM object.
 @Param[in]     size        - Size of the MM.
 @Param[in]     alignment   - Index as a power of two defines a required
                              alignment (in bytes); Should be 1, 2, 4, 8, 16, 32 or 64
 @Param[in]     min         - The minimum base address of the block.
 @Param[in]     name        - Name that specifies an allocated block.

 @Return        base address of an allocated block,ILLEGAL_BASE if can't allocate a block.
*//***************************************************************************/
uint64_t    MM_GetForceMin(t_Handle h_MM,
                           uint64_t size,
                           uint64_t alignment,
                           uint64_t min,
                           char     *name);

/**************************************************************************//**
 @Function      MM_Put

 @Description   Puts a block of memory of the given base address back to the memory.

                It checks if there is a busy block with the
                given base address. If not, it returns 0, that
                means can't free a block. Otherwise, it gets parameters of
                the busy block and after it updates lists of free blocks,
                removes that busy block from the list by calling to MM_CutBusy
                routine.
                After that it calls to MM_AddFree routine to add a new free
                block to the free lists.

 @Param[in]     h_MM    - Handle to the MM object.
 @Param[in]     base    - Base address of the MM.

 @Return         The size of bytes released, 0 if failed.
*//***************************************************************************/
uint64_t    MM_Put(t_Handle h_MM, uint64_t base);

/**************************************************************************//**
 @Function      MM_PutForce

 @Description   Releases a block of memory of the required size from the required base address.

                First, it calls to MM_CutBusy routine
                to cut a free block from the busy list. And then, calls to
                MM_AddFree routine to add the free block to the free lists.

 @Param[in]     h_MM    - Handle to the MM object.
 @Param[in]     base    - Base address of of a block to free.
 @Param[in]     size    - Size of a block to free.

 @Return        The number of bytes released, 0 on failure.
*//***************************************************************************/
uint64_t    MM_PutForce(t_Handle h_MM, uint64_t base, uint64_t size);

/**************************************************************************//**
 @Function      MM_Add

 @Description   Adds a new memory block for memory allocation.

                When a new memory block is initialized and added to the
                memory list, it calls to MM_AddFree routine to add the
                new free block to the free lists.

 @Param[in]     h_MM    - Handle to the MM object.
 @Param[in]     base    - Base address of the memory block.
 @Param[in]     size    - Size of the memory block.

 @Return        E_OK on success, otherwise returns an error code.
*//***************************************************************************/
t_Error     MM_Add(t_Handle h_MM, uint64_t base, uint64_t size);

/**************************************************************************//**
 @Function      MM_Dump

 @Description   Prints results of free and busy lists.

 @Param[in]     h_MM        - Handle to the MM object.
*//***************************************************************************/
void        MM_Dump(t_Handle h_MM);

/**************************************************************************//**
 @Function      MM_Free

 @Description   Releases memory allocated for MM object.

 @Param[in]     h_MM - Handle of the MM object.
*//***************************************************************************/
void        MM_Free(t_Handle h_MM);

/**************************************************************************//**
 @Function      MM_GetMemBlock

 @Description   Returns base address of the memory block specified by the index.

                If index is 0, returns base address
                of the first memory block, 1 - returns base address
                of the second memory block, etc.
                Note, those memory blocks are allocated by the
                application before MM_Init or MM_Add and have to
                be released by the application before or after invoking
                the MM_Free routine.

 @Param[in]     h_MM    - Handle to the MM object.
 @Param[in]     index   - Index of the memory block.

 @Return        valid base address or ILLEGAL_BASE if no memory block specified by the index.
*//***************************************************************************/
uint64_t    MM_GetMemBlock(t_Handle h_MM, int index);

/**************************************************************************//**
 @Function      MM_InRange

 @Description   Checks if a specific address is in the memory range of the passed MM object.

 @Param[in]     h_MM    - Handle to the MM object.
 @Param[in]     addr    - The address to be checked.

 @Return        TRUE if the address is in the address range of the block, FALSE otherwise.
*//***************************************************************************/
bool        MM_InRange(t_Handle h_MM, uint64_t addr);

/**************************************************************************//**
 @Function      MM_GetFreeMemSize

 @Description   Returns the size (in bytes) of free memory.

 @Param[in]     h_MM    - Handle to the MM object.

 @Return        Free memory size in bytes.
*//***************************************************************************/
uint64_t MM_GetFreeMemSize(t_Handle h_MM);


/** @} */ /* end of mm_grp group */
/** @} */ /* end of etc_id group */

#endif /* __MM_EXT_H */