12c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent/*
22c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * Copyright (C) 2004-2010 NXP Software
32c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * Copyright (C) 2010 The Android Open Source Project
42c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *
52c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * Licensed under the Apache License, Version 2.0 (the "License");
62c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * you may not use this file except in compliance with the License.
72c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * You may obtain a copy of the License at
82c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *
92c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *      http://www.apache.org/licenses/LICENSE-2.0
102c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *
112c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * Unless required by applicable law or agreed to in writing, software
122c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * distributed under the License is distributed on an "AS IS" BASIS,
132c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
142c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * See the License for the specific language governing permissions and
152c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * limitations under the License.
162c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent */
172c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
182c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent#include "InstAlloc.h"
192c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
202c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent/****************************************************************************************
212c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Name        : InstAlloc_Init()
222c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Input       : pms  - Pointer to the INST_ALLOC instance
232c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                  StartAddr - Base address of the instance memory
242c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Returns     : Error code
252c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Description : Initializes the instance distribution and memory size calculation function
262c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Remarks     :
272c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent ****************************************************************************************/
282c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
292c87e9c923b0362fabf8c97ff63997542394c428Eric Laurentvoid    InstAlloc_Init( INST_ALLOC      *pms,
302c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                        void            *StartAddr )
312c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent{
322c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms->TotalSize = 3;
332c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms->pNextMember = (LVM_UINT32)(((LVM_UINT32)StartAddr + 3) & 0xFFFFFFFC);/* This code will fail if the platform address space is more than 32-bits*/
342c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent}
352c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
362c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
372c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent/****************************************************************************************
382c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Name        : InstAlloc_AddMember()
392c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Input       : pms  - Pointer to the INST_ALLOC instance
402c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                  Size - The size in bytes of the new added member
412c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Returns     : A pointer to the new added member
422c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Description : Allocates space for a new member in the instance memory and returns
432c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                  a pointer to this new member.  The start address of all members will
442c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                  be 32 bit alligned.
452c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Remarks     :
462c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent ****************************************************************************************/
472c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
482c87e9c923b0362fabf8c97ff63997542394c428Eric Laurentvoid*   InstAlloc_AddMember( INST_ALLOC         *pms,
492c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                             LVM_UINT32           Size )
502c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent{
512c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    void *NewMemberAddress; /* Variable to temporarily store the return value */
522c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    NewMemberAddress = (void*)pms->pNextMember;
532c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
542c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    Size = ((Size + 3) & 0xFFFFFFFC); /* Ceil the size to a multiple of four */
552c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
562c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms->TotalSize += Size;
572c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms->pNextMember += Size;
582c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
592c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    return(NewMemberAddress);
602c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent}
612c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
622c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
632c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent/****************************************************************************************
642c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Name        : InstAlloc_GetTotal()
652c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Input       : pms  - Pointer to the INST_ALLOC instance
662c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Returns     : The instance memory size
672c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Description : This functions returns the calculated instance memory size
682c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Remarks     :
692c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent ****************************************************************************************/
702c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
712c87e9c923b0362fabf8c97ff63997542394c428Eric LaurentLVM_UINT32 InstAlloc_GetTotal( INST_ALLOC *pms)
722c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent{
732c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    if (pms->TotalSize > 3)
742c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    {
752c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent        return(pms->TotalSize);
762c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    }
772c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    else
782c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    {
792c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent        return 0;           /* No memory added */
802c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    }
812c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent}
822c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
832c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
842c87e9c923b0362fabf8c97ff63997542394c428Eric Laurentvoid    InstAlloc_InitAll( INST_ALLOC                      *pms,
852c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                           LVM_MemoryTable_st             *pMemoryTable)
862c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent{
872c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    LVM_UINT32 StartAddr;
882c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
892c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    StartAddr = (LVM_UINT32)pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].pBaseAddress;
902c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
912c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[0].TotalSize = 3;
922c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[0].pNextMember = (LVM_UINT32)(((LVM_UINT32)StartAddr + 3) & 0xFFFFFFFC);
932c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
942c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
952c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    StartAddr = (LVM_UINT32)pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].pBaseAddress;
962c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
972c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[1].TotalSize = 3;
982c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[1].pNextMember = (LVM_UINT32)(((LVM_UINT32)StartAddr + 3) & 0xFFFFFFFC);
992c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1002c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1012c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    StartAddr = (LVM_UINT32)pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].pBaseAddress;
1022c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1032c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[2].TotalSize = 3;
1042c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[2].pNextMember = (LVM_UINT32)(((LVM_UINT32)StartAddr + 3) & 0xFFFFFFFC);
1052c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1062c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1072c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    StartAddr = (LVM_UINT32)pMemoryTable->Region[LVM_TEMPORARY_FAST].pBaseAddress;
1082c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1092c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[3].TotalSize = 3;
1102c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[3].pNextMember = (LVM_UINT32)(((LVM_UINT32)StartAddr + 3) & 0xFFFFFFFC);
1112c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1122c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent}
1132c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1142c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent/****************************************************************************************
1152c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Name        : InstAlloc_InitAll_NULL()
1162c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Input       : pms  - Pointer to array of four INST_ALLOC instances
1172c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Returns     : Nothing
1182c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Description : This function reserves Size of 3 bytes for all memory regions and
1192c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *                intializes pNextMember for all regions to 0
1202c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *  Remarks     :
1212c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent ****************************************************************************************/
1222c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1232c87e9c923b0362fabf8c97ff63997542394c428Eric Laurentvoid    InstAlloc_InitAll_NULL( INST_ALLOC  *pms)
1242c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent{
1252c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[0].TotalSize = 3;
1262c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[0].pNextMember = 0;
1272c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1282c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1292c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[1].TotalSize = 3;
1302c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[1].pNextMember = 0;
1312c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1322c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[2].TotalSize = 3;
1332c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[2].pNextMember = 0;
1342c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1352c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[3].TotalSize = 3;
1362c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pms[3].pNextMember = 0;
1372c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1382c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent}
1392c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1402c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1412c87e9c923b0362fabf8c97ff63997542394c428Eric Laurentvoid*   InstAlloc_AddMemberAll( INST_ALLOC                     *pms,
1422c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                                 LVM_UINT32                   Size[],
1432c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                                 LVM_MemoryTable_st           *pMemoryTable)
1442c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent{
1452c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    void *NewMemberAddress; /* Variable to temporarily store the return value */
1462c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1472c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    /* coverity[returned_pointer] Ignore coverity warning that ptr is not used */
1482c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    NewMemberAddress = InstAlloc_AddMember(&pms[LVM_PERSISTENT_SLOW_DATA], Size[LVM_PERSISTENT_SLOW_DATA]);
1492c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1502c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].Size         = InstAlloc_GetTotal(&pms[LVM_PERSISTENT_SLOW_DATA]);
1512c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].Type         = LVM_PERSISTENT_SLOW_DATA;
1522c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].pBaseAddress = LVM_NULL;
1532c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1542c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    NewMemberAddress = InstAlloc_AddMember(&pms[LVM_PERSISTENT_FAST_DATA], Size[LVM_PERSISTENT_FAST_DATA]);
1552c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1562c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].Size         = InstAlloc_GetTotal(&pms[LVM_PERSISTENT_FAST_DATA]);
1572c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].Type         = LVM_PERSISTENT_FAST_DATA;
1582c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].pBaseAddress = LVM_NULL;
1592c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1602c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    NewMemberAddress = InstAlloc_AddMember(&pms[LVM_PERSISTENT_FAST_COEF], Size[LVM_PERSISTENT_FAST_COEF]);
1612c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1622c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].Size         = InstAlloc_GetTotal(&pms[LVM_PERSISTENT_FAST_COEF]);
1632c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].Type         = LVM_PERSISTENT_FAST_COEF;
1642c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].pBaseAddress = LVM_NULL;
1652c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1662c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    NewMemberAddress = InstAlloc_AddMember(&pms[LVM_TEMPORARY_FAST], Size[LVM_TEMPORARY_FAST]);
1672c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1682c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pMemoryTable->Region[LVM_TEMPORARY_FAST].Size                 = InstAlloc_GetTotal(&pms[LVM_TEMPORARY_FAST]);
1692c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pMemoryTable->Region[LVM_TEMPORARY_FAST].Type                 = LVM_TEMPORARY_FAST;
1702c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    pMemoryTable->Region[LVM_TEMPORARY_FAST].pBaseAddress         = LVM_NULL;
1712c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1722c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    return(NewMemberAddress);
1732c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent}
1742c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1752c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1762c87e9c923b0362fabf8c97ff63997542394c428Eric Laurentvoid*   InstAlloc_AddMemberAllRet(     INST_ALLOC                 *pms,
1772c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                                     LVM_UINT32               Size[],
1782c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                                     void                    **ptr)
1792c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent{
1802c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    ptr[0] = InstAlloc_AddMember(&pms[LVM_PERSISTENT_SLOW_DATA], Size[LVM_PERSISTENT_SLOW_DATA]);
1812c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    ptr[1] = InstAlloc_AddMember(&pms[LVM_PERSISTENT_FAST_DATA], Size[LVM_PERSISTENT_FAST_DATA]);
1822c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    ptr[2] = InstAlloc_AddMember(&pms[LVM_PERSISTENT_FAST_COEF], Size[LVM_PERSISTENT_FAST_COEF]);
1832c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    ptr[3] = InstAlloc_AddMember(&pms[LVM_TEMPORARY_FAST], Size[LVM_TEMPORARY_FAST]);
1842c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1852c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    return (ptr[0]);
1862c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent}
187