xref: /freebsd/sys/contrib/ncsw/inc/etc/mm_ext.h (revision c2c014f24c10f90d85126ac5fbd4d8524de32b1c)
1*852ba100SJustin Hibbits /* Copyright (c) 2008-2012 Freescale Semiconductor, Inc
20aeed3e9SJustin Hibbits  * All rights reserved.
30aeed3e9SJustin Hibbits  *
40aeed3e9SJustin Hibbits  * Redistribution and use in source and binary forms, with or without
50aeed3e9SJustin Hibbits  * modification, are permitted provided that the following conditions are met:
60aeed3e9SJustin Hibbits  *     * Redistributions of source code must retain the above copyright
70aeed3e9SJustin Hibbits  *       notice, this list of conditions and the following disclaimer.
80aeed3e9SJustin Hibbits  *     * Redistributions in binary form must reproduce the above copyright
90aeed3e9SJustin Hibbits  *       notice, this list of conditions and the following disclaimer in the
100aeed3e9SJustin Hibbits  *       documentation and/or other materials provided with the distribution.
110aeed3e9SJustin Hibbits  *     * Neither the name of Freescale Semiconductor nor the
120aeed3e9SJustin Hibbits  *       names of its contributors may be used to endorse or promote products
130aeed3e9SJustin Hibbits  *       derived from this software without specific prior written permission.
140aeed3e9SJustin Hibbits  *
150aeed3e9SJustin Hibbits  *
160aeed3e9SJustin Hibbits  * ALTERNATIVELY, this software may be distributed under the terms of the
170aeed3e9SJustin Hibbits  * GNU General Public License ("GPL") as published by the Free Software
180aeed3e9SJustin Hibbits  * Foundation, either version 2 of that License or (at your option) any
190aeed3e9SJustin Hibbits  * later version.
200aeed3e9SJustin Hibbits  *
210aeed3e9SJustin Hibbits  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
220aeed3e9SJustin Hibbits  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
230aeed3e9SJustin Hibbits  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
240aeed3e9SJustin Hibbits  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
250aeed3e9SJustin Hibbits  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
260aeed3e9SJustin Hibbits  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
270aeed3e9SJustin Hibbits  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
280aeed3e9SJustin Hibbits  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
290aeed3e9SJustin Hibbits  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
300aeed3e9SJustin Hibbits  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
310aeed3e9SJustin Hibbits  */
320aeed3e9SJustin Hibbits 
330aeed3e9SJustin Hibbits 
34*852ba100SJustin Hibbits /**************************************************************************//**
350aeed3e9SJustin Hibbits  @File          mm_ext.h
360aeed3e9SJustin Hibbits 
370aeed3e9SJustin Hibbits  @Description   Memory Manager Application Programming Interface
380aeed3e9SJustin Hibbits *//***************************************************************************/
390aeed3e9SJustin Hibbits #ifndef __MM_EXT
400aeed3e9SJustin Hibbits #define __MM_EXT
410aeed3e9SJustin Hibbits 
420aeed3e9SJustin Hibbits #include "std_ext.h"
430aeed3e9SJustin Hibbits 
440aeed3e9SJustin Hibbits #define MM_MAX_ALIGNMENT    20  /* Alignments from 2 to 128 are available
450aeed3e9SJustin Hibbits                                    where maximum alignment defined as
460aeed3e9SJustin Hibbits                                    MM_MAX_ALIGNMENT power of 2 */
470aeed3e9SJustin Hibbits 
480aeed3e9SJustin Hibbits #define MM_MAX_NAME_LEN     32
490aeed3e9SJustin Hibbits 
500aeed3e9SJustin Hibbits /**************************************************************************//**
510aeed3e9SJustin Hibbits  @Group         etc_id   Utility Library Application Programming Interface
520aeed3e9SJustin Hibbits 
530aeed3e9SJustin Hibbits  @Description   External routines.
540aeed3e9SJustin Hibbits 
550aeed3e9SJustin Hibbits  @{
560aeed3e9SJustin Hibbits *//***************************************************************************/
570aeed3e9SJustin Hibbits 
580aeed3e9SJustin Hibbits /**************************************************************************//**
590aeed3e9SJustin Hibbits  @Group         mm_grp Flexible Memory Manager
600aeed3e9SJustin Hibbits 
610aeed3e9SJustin Hibbits  @Description   Flexible Memory Manager module functions,definitions and enums.
620aeed3e9SJustin Hibbits                 (All of the following functions,definitions and enums can be found in mm_ext.h)
630aeed3e9SJustin Hibbits 
640aeed3e9SJustin Hibbits  @{
650aeed3e9SJustin Hibbits *//***************************************************************************/
660aeed3e9SJustin Hibbits 
670aeed3e9SJustin Hibbits 
680aeed3e9SJustin Hibbits /**************************************************************************//**
690aeed3e9SJustin Hibbits  @Function      MM_Init
700aeed3e9SJustin Hibbits 
710aeed3e9SJustin Hibbits  @Description   Initializes a new MM object.
720aeed3e9SJustin Hibbits 
730aeed3e9SJustin Hibbits                 It initializes a new memory block consisting of base address
740aeed3e9SJustin Hibbits                 and size of the available memory by calling to MemBlock_Init
750aeed3e9SJustin Hibbits                 routine. It is also initializes a new free block for each
760aeed3e9SJustin Hibbits                 by calling FreeBlock_Init routine, which is pointed to
770aeed3e9SJustin Hibbits                 the almost all memory started from the required alignment
780aeed3e9SJustin Hibbits                 from the base address and to the end of the memory.
790aeed3e9SJustin Hibbits                 The handle to the new MM object is returned via "MM"
800aeed3e9SJustin Hibbits                 argument (passed by reference).
810aeed3e9SJustin Hibbits 
820aeed3e9SJustin Hibbits  @Param[in]     h_MM    - Handle to the MM object.
830aeed3e9SJustin Hibbits  @Param[in]     base    - Base address of the MM.
840aeed3e9SJustin Hibbits  @Param[in]     size    - Size of the MM.
850aeed3e9SJustin Hibbits 
860aeed3e9SJustin Hibbits  @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.
870aeed3e9SJustin Hibbits *//***************************************************************************/
880aeed3e9SJustin Hibbits t_Error     MM_Init(t_Handle *h_MM, uint64_t base, uint64_t size);
890aeed3e9SJustin Hibbits 
900aeed3e9SJustin Hibbits /**************************************************************************//**
910aeed3e9SJustin Hibbits  @Function      MM_Get
920aeed3e9SJustin Hibbits 
930aeed3e9SJustin Hibbits  @Description   Allocates a block of memory according to the given size and the alignment.
940aeed3e9SJustin Hibbits 
950aeed3e9SJustin Hibbits                 The Alignment argument tells from which
960aeed3e9SJustin Hibbits                 free list allocate a block of memory. 2^alignment indicates
970aeed3e9SJustin Hibbits                 the alignment that the base address of the allocated block
980aeed3e9SJustin Hibbits                 should have. So, the only values 1, 2, 4, 8, 16, 32 and 64
990aeed3e9SJustin Hibbits                 are available for the alignment argument.
1000aeed3e9SJustin Hibbits                 The routine passes through the specific free list of free
1010aeed3e9SJustin Hibbits                 blocks and seeks for a first block that have anough memory
1020aeed3e9SJustin Hibbits                 that  is required (best fit).
1030aeed3e9SJustin Hibbits                 After the block is found and data is allocated, it calls
1040aeed3e9SJustin Hibbits                 the internal MM_CutFree routine to update all free lists
1050aeed3e9SJustin Hibbits                 do not include a just allocated block. Of course, each
1060aeed3e9SJustin Hibbits                 free list contains a free blocks with the same alignment.
1070aeed3e9SJustin Hibbits                 It is also creates a busy block that holds
1080aeed3e9SJustin Hibbits                 information about an allocated block.
1090aeed3e9SJustin Hibbits 
1100aeed3e9SJustin Hibbits  @Param[in]     h_MM        - Handle to the MM object.
1110aeed3e9SJustin Hibbits  @Param[in]     size        - Size of the MM.
1120aeed3e9SJustin Hibbits  @Param[in]     alignment   - Index as a power of two defines a required
1130aeed3e9SJustin Hibbits                               alignment (in bytes); Should be 1, 2, 4, 8, 16, 32 or 64
1140aeed3e9SJustin Hibbits  @Param[in]     name        - The name that specifies an allocated block.
1150aeed3e9SJustin Hibbits 
1160aeed3e9SJustin Hibbits  @Return        base address of an allocated block ILLEGAL_BASE if can't allocate a block
1170aeed3e9SJustin Hibbits *//***************************************************************************/
1180aeed3e9SJustin Hibbits uint64_t    MM_Get(t_Handle h_MM, uint64_t size, uint64_t alignment, char *name);
1190aeed3e9SJustin Hibbits 
1200aeed3e9SJustin Hibbits /**************************************************************************//**
1210aeed3e9SJustin Hibbits  @Function      MM_GetBase
1220aeed3e9SJustin Hibbits 
1230aeed3e9SJustin Hibbits  @Description   Gets the base address of the required MM objects.
1240aeed3e9SJustin Hibbits 
1250aeed3e9SJustin Hibbits  @Param[in]     h_MM - Handle to the MM object.
1260aeed3e9SJustin Hibbits 
1270aeed3e9SJustin Hibbits  @Return        base address of the block.
1280aeed3e9SJustin Hibbits *//***************************************************************************/
1290aeed3e9SJustin Hibbits uint64_t    MM_GetBase(t_Handle h_MM);
1300aeed3e9SJustin Hibbits 
1310aeed3e9SJustin Hibbits /**************************************************************************//**
1320aeed3e9SJustin Hibbits  @Function      MM_GetForce
1330aeed3e9SJustin Hibbits 
1340aeed3e9SJustin Hibbits  @Description   Force memory allocation.
1350aeed3e9SJustin Hibbits 
1360aeed3e9SJustin Hibbits                 It means to allocate a block of memory of the given
1370aeed3e9SJustin Hibbits                 size from the given base address.
1380aeed3e9SJustin Hibbits                 The routine checks if the required block can be allocated
1390aeed3e9SJustin Hibbits                 (that is it is free) and then, calls the internal MM_CutFree
1400aeed3e9SJustin Hibbits                 routine to update all free lists do not include that block.
1410aeed3e9SJustin Hibbits 
1420aeed3e9SJustin Hibbits  @Param[in]     h_MM    - Handle to the MM object.
1430aeed3e9SJustin Hibbits  @Param[in]     base    - Base address of the MM.
1440aeed3e9SJustin Hibbits  @Param[in]     size    - Size of the MM.
1450aeed3e9SJustin Hibbits  @Param[in]     name    - Name that specifies an allocated block.
1460aeed3e9SJustin Hibbits 
1470aeed3e9SJustin Hibbits  @Return        base address of an allocated block, ILLEGAL_BASE if can't allocate a block.
1480aeed3e9SJustin Hibbits *//***************************************************************************/
1490aeed3e9SJustin Hibbits uint64_t    MM_GetForce(t_Handle h_MM, uint64_t base, uint64_t size, char *name);
1500aeed3e9SJustin Hibbits 
1510aeed3e9SJustin Hibbits /**************************************************************************//**
1520aeed3e9SJustin Hibbits  @Function      MM_GetForceMin
1530aeed3e9SJustin Hibbits 
1540aeed3e9SJustin Hibbits  @Description   Allocates a block of memory according to the given size, the alignment and minimum base address.
1550aeed3e9SJustin Hibbits 
1560aeed3e9SJustin Hibbits                 The Alignment argument tells from which
1570aeed3e9SJustin Hibbits                 free list allocate a block of memory. 2^alignment indicates
1580aeed3e9SJustin Hibbits                 the alignment that the base address of the allocated block
1590aeed3e9SJustin Hibbits                 should have. So, the only values 1, 2, 4, 8, 16, 32 and 64
1600aeed3e9SJustin Hibbits                 are available for the alignment argument.
1610aeed3e9SJustin Hibbits                 The minimum baser address forces the location of the block
1620aeed3e9SJustin Hibbits                 to be from a given address onward.
1630aeed3e9SJustin Hibbits                 The routine passes through the specific free list of free
1640aeed3e9SJustin Hibbits                 blocks and seeks for the first base address equal or smaller
1650aeed3e9SJustin Hibbits                 than the required minimum address and end address larger than
1660aeed3e9SJustin Hibbits                 than the required base + its size - i.e. that may contain
1670aeed3e9SJustin Hibbits                 the required block.
1680aeed3e9SJustin Hibbits                 After the block is found and data is allocated, it calls
1690aeed3e9SJustin Hibbits                 the internal MM_CutFree routine to update all free lists
1700aeed3e9SJustin Hibbits                 do not include a just allocated block. Of course, each
1710aeed3e9SJustin Hibbits                 free list contains a free blocks with the same alignment.
1720aeed3e9SJustin Hibbits                 It is also creates a busy block that holds
1730aeed3e9SJustin Hibbits                 information about an allocated block.
1740aeed3e9SJustin Hibbits 
1750aeed3e9SJustin Hibbits  @Param[in]     h_MM        - Handle to the MM object.
1760aeed3e9SJustin Hibbits  @Param[in]     size        - Size of the MM.
1770aeed3e9SJustin Hibbits  @Param[in]     alignment   - Index as a power of two defines a required
1780aeed3e9SJustin Hibbits                               alignment (in bytes); Should be 1, 2, 4, 8, 16, 32 or 64
1790aeed3e9SJustin Hibbits  @Param[in]     min         - The minimum base address of the block.
1800aeed3e9SJustin Hibbits  @Param[in]     name        - Name that specifies an allocated block.
1810aeed3e9SJustin Hibbits 
1820aeed3e9SJustin Hibbits  @Return        base address of an allocated block,ILLEGAL_BASE if can't allocate a block.
1830aeed3e9SJustin Hibbits *//***************************************************************************/
1840aeed3e9SJustin Hibbits uint64_t    MM_GetForceMin(t_Handle h_MM,
1850aeed3e9SJustin Hibbits                            uint64_t size,
1860aeed3e9SJustin Hibbits                            uint64_t alignment,
1870aeed3e9SJustin Hibbits                            uint64_t min,
1880aeed3e9SJustin Hibbits                            char     *name);
1890aeed3e9SJustin Hibbits 
1900aeed3e9SJustin Hibbits /**************************************************************************//**
1910aeed3e9SJustin Hibbits  @Function      MM_Put
1920aeed3e9SJustin Hibbits 
1930aeed3e9SJustin Hibbits  @Description   Puts a block of memory of the given base address back to the memory.
1940aeed3e9SJustin Hibbits 
1950aeed3e9SJustin Hibbits                 It checks if there is a busy block with the
1960aeed3e9SJustin Hibbits                 given base address. If not, it returns 0, that
1970aeed3e9SJustin Hibbits                 means can't free a block. Otherwise, it gets parameters of
1980aeed3e9SJustin Hibbits                 the busy block and after it updates lists of free blocks,
1990aeed3e9SJustin Hibbits                 removes that busy block from the list by calling to MM_CutBusy
2000aeed3e9SJustin Hibbits                 routine.
2010aeed3e9SJustin Hibbits                 After that it calls to MM_AddFree routine to add a new free
2020aeed3e9SJustin Hibbits                 block to the free lists.
2030aeed3e9SJustin Hibbits 
2040aeed3e9SJustin Hibbits  @Param[in]     h_MM    - Handle to the MM object.
2050aeed3e9SJustin Hibbits  @Param[in]     base    - Base address of the MM.
2060aeed3e9SJustin Hibbits 
2070aeed3e9SJustin Hibbits  @Return         The size of bytes released, 0 if failed.
2080aeed3e9SJustin Hibbits *//***************************************************************************/
2090aeed3e9SJustin Hibbits uint64_t    MM_Put(t_Handle h_MM, uint64_t base);
2100aeed3e9SJustin Hibbits 
2110aeed3e9SJustin Hibbits /**************************************************************************//**
2120aeed3e9SJustin Hibbits  @Function      MM_PutForce
2130aeed3e9SJustin Hibbits 
2140aeed3e9SJustin Hibbits  @Description   Releases a block of memory of the required size from the required base address.
2150aeed3e9SJustin Hibbits 
2160aeed3e9SJustin Hibbits                 First, it calls to MM_CutBusy routine
2170aeed3e9SJustin Hibbits                 to cut a free block from the busy list. And then, calls to
2180aeed3e9SJustin Hibbits                 MM_AddFree routine to add the free block to the free lists.
2190aeed3e9SJustin Hibbits 
2200aeed3e9SJustin Hibbits  @Param[in]     h_MM    - Handle to the MM object.
2210aeed3e9SJustin Hibbits  @Param[in]     base    - Base address of of a block to free.
2220aeed3e9SJustin Hibbits  @Param[in]     size    - Size of a block to free.
2230aeed3e9SJustin Hibbits 
2240aeed3e9SJustin Hibbits  @Return        The number of bytes released, 0 on failure.
2250aeed3e9SJustin Hibbits *//***************************************************************************/
2260aeed3e9SJustin Hibbits uint64_t    MM_PutForce(t_Handle h_MM, uint64_t base, uint64_t size);
2270aeed3e9SJustin Hibbits 
2280aeed3e9SJustin Hibbits /**************************************************************************//**
2290aeed3e9SJustin Hibbits  @Function      MM_Add
2300aeed3e9SJustin Hibbits 
2310aeed3e9SJustin Hibbits  @Description   Adds a new memory block for memory allocation.
2320aeed3e9SJustin Hibbits 
2330aeed3e9SJustin Hibbits                 When a new memory block is initialized and added to the
2340aeed3e9SJustin Hibbits                 memory list, it calls to MM_AddFree routine to add the
2350aeed3e9SJustin Hibbits                 new free block to the free lists.
2360aeed3e9SJustin Hibbits 
2370aeed3e9SJustin Hibbits  @Param[in]     h_MM    - Handle to the MM object.
2380aeed3e9SJustin Hibbits  @Param[in]     base    - Base address of the memory block.
2390aeed3e9SJustin Hibbits  @Param[in]     size    - Size of the memory block.
2400aeed3e9SJustin Hibbits 
2410aeed3e9SJustin Hibbits  @Return        E_OK on success, otherwise returns an error code.
2420aeed3e9SJustin Hibbits *//***************************************************************************/
2430aeed3e9SJustin Hibbits t_Error     MM_Add(t_Handle h_MM, uint64_t base, uint64_t size);
2440aeed3e9SJustin Hibbits 
2450aeed3e9SJustin Hibbits /**************************************************************************//**
2460aeed3e9SJustin Hibbits  @Function      MM_Dump
2470aeed3e9SJustin Hibbits 
248*852ba100SJustin Hibbits  @Description   Prints results of free and busy lists.
2490aeed3e9SJustin Hibbits 
2500aeed3e9SJustin Hibbits  @Param[in]     h_MM        - Handle to the MM object.
2510aeed3e9SJustin Hibbits *//***************************************************************************/
252*852ba100SJustin Hibbits void        MM_Dump(t_Handle h_MM);
2530aeed3e9SJustin Hibbits 
2540aeed3e9SJustin Hibbits /**************************************************************************//**
2550aeed3e9SJustin Hibbits  @Function      MM_Free
2560aeed3e9SJustin Hibbits 
2570aeed3e9SJustin Hibbits  @Description   Releases memory allocated for MM object.
2580aeed3e9SJustin Hibbits 
2590aeed3e9SJustin Hibbits  @Param[in]     h_MM - Handle of the MM object.
2600aeed3e9SJustin Hibbits *//***************************************************************************/
2610aeed3e9SJustin Hibbits void        MM_Free(t_Handle h_MM);
2620aeed3e9SJustin Hibbits 
2630aeed3e9SJustin Hibbits /**************************************************************************//**
2640aeed3e9SJustin Hibbits  @Function      MM_GetMemBlock
2650aeed3e9SJustin Hibbits 
2660aeed3e9SJustin Hibbits  @Description   Returns base address of the memory block specified by the index.
2670aeed3e9SJustin Hibbits 
2680aeed3e9SJustin Hibbits                 If index is 0, returns base address
2690aeed3e9SJustin Hibbits                 of the first memory block, 1 - returns base address
2700aeed3e9SJustin Hibbits                 of the second memory block, etc.
2710aeed3e9SJustin Hibbits                 Note, those memory blocks are allocated by the
2720aeed3e9SJustin Hibbits                 application before MM_Init or MM_Add and have to
2730aeed3e9SJustin Hibbits                 be released by the application before or after invoking
2740aeed3e9SJustin Hibbits                 the MM_Free routine.
2750aeed3e9SJustin Hibbits 
2760aeed3e9SJustin Hibbits  @Param[in]     h_MM    - Handle to the MM object.
2770aeed3e9SJustin Hibbits  @Param[in]     index   - Index of the memory block.
2780aeed3e9SJustin Hibbits 
2790aeed3e9SJustin Hibbits  @Return        valid base address or ILLEGAL_BASE if no memory block specified by the index.
2800aeed3e9SJustin Hibbits *//***************************************************************************/
2810aeed3e9SJustin Hibbits uint64_t    MM_GetMemBlock(t_Handle h_MM, int index);
2820aeed3e9SJustin Hibbits 
2830aeed3e9SJustin Hibbits /**************************************************************************//**
2840aeed3e9SJustin Hibbits  @Function      MM_InRange
2850aeed3e9SJustin Hibbits 
2860aeed3e9SJustin Hibbits  @Description   Checks if a specific address is in the memory range of the passed MM object.
2870aeed3e9SJustin Hibbits 
2880aeed3e9SJustin Hibbits  @Param[in]     h_MM    - Handle to the MM object.
2890aeed3e9SJustin Hibbits  @Param[in]     addr    - The address to be checked.
2900aeed3e9SJustin Hibbits 
2910aeed3e9SJustin Hibbits  @Return        TRUE if the address is in the address range of the block, FALSE otherwise.
2920aeed3e9SJustin Hibbits *//***************************************************************************/
2930aeed3e9SJustin Hibbits bool        MM_InRange(t_Handle h_MM, uint64_t addr);
2940aeed3e9SJustin Hibbits 
295*852ba100SJustin Hibbits /**************************************************************************//**
296*852ba100SJustin Hibbits  @Function      MM_GetFreeMemSize
297*852ba100SJustin Hibbits 
298*852ba100SJustin Hibbits  @Description   Returns the size (in bytes) of free memory.
299*852ba100SJustin Hibbits 
300*852ba100SJustin Hibbits  @Param[in]     h_MM    - Handle to the MM object.
301*852ba100SJustin Hibbits 
302*852ba100SJustin Hibbits  @Return        Free memory size in bytes.
303*852ba100SJustin Hibbits *//***************************************************************************/
304*852ba100SJustin Hibbits uint64_t MM_GetFreeMemSize(t_Handle h_MM);
305*852ba100SJustin Hibbits 
3060aeed3e9SJustin Hibbits 
3070aeed3e9SJustin Hibbits /** @} */ /* end of mm_grp group */
3080aeed3e9SJustin Hibbits /** @} */ /* end of etc_id group */
3090aeed3e9SJustin Hibbits 
3100aeed3e9SJustin Hibbits #endif /* __MM_EXT_H */
311