diff --git a/board/aiit-arm32-board/board.c b/board/aiit-arm32-board/board.c index f3c221b7..5aa16126 100644 --- a/board/aiit-arm32-board/board.c +++ b/board/aiit-arm32-board/board.c @@ -47,6 +47,7 @@ extern int Stm32HwRtcInit(); extern int Stm32HwTouchBusInit(void); extern int Stm32HwCanBusInit(void); extern int HwSdioInit(); +extern int HwSramInit(void); static void ClockConfiguration() { @@ -143,6 +144,11 @@ struct InitSequenceDesc _board_init[] = #ifdef BSP_USING_SDIO {"hw sdcard init",HwSdioInit}, #endif +// #ifdef BSP_USING_EXTMEM +// #ifdef DATA_IN_ExtSRAM +// {"hw ext sram",HwSramInit}, +// #endif +// #endif { " NONE ",NONE }, }; @@ -155,9 +161,12 @@ void InitBoardHardware() NVIC_Configuration(); SysTickConfiguration(); + InitBoardMemory((void*)MEMORY_START_ADDRESS, (void*)MEMORY_END_ADDRESS); + #ifdef BSP_USING_UART Stm32HwUsartInit(); #endif + #ifdef KERNEL_CONSOLE InstallConsole(KERNEL_CONSOLE_BUS_NAME, KERNEL_CONSOLE_DRV_NAME, KERNEL_CONSOLE_DEVICE_NAME); KPrintf("\nconsole init completed.\n"); diff --git a/board/aiit-arm32-board/third_party_driver/extmem/Kconfig b/board/aiit-arm32-board/third_party_driver/extmem/Kconfig index e69de29b..4010be56 100644 --- a/board/aiit-arm32-board/third_party_driver/extmem/Kconfig +++ b/board/aiit-arm32-board/third_party_driver/extmem/Kconfig @@ -0,0 +1,22 @@ +menu "Extern Sram Config" + config DATA_IN_ExtSRAM + bool "support extern sram" + default n + if DATA_IN_ExtSRAM + config EXTSRAM_MAX_NUM + int + default 4 + + config BSP_USING_FSMC_BANK1_NORSRAM3 + bool "config fsmc bank1 sram3" + default n + if BSP_USING_FSMC_BANK1_NORSRAM3 + config BANK1_NORSRAM3_SIZE + hex "config sram chip size" + default 0x100000 + config BANK1_NORSRAM3_DATA_WIDTH + int "sram chip data width" + default 16 + endif + endif +endmenu diff --git a/board/stm32f407zgt6/board.c b/board/stm32f407zgt6/board.c index 2f431e62..c672d824 100644 --- a/board/stm32f407zgt6/board.c +++ b/board/stm32f407zgt6/board.c @@ -117,6 +117,7 @@ void InitBoardHardware() NVIC_Configuration(); SysTickConfiguration(); + InitBoardMemory((void*)MEMORY_START_ADDRESS, (void*)MEMORY_END_ADDRESS); #ifdef BSP_USING_UART Stm32HwUsartInit(); #endif @@ -131,7 +132,7 @@ void InitBoardHardware() HwSramInit(); #endif - InitBoardMemory((void*)MEMORY_START_ADDRESS, (void*)MEMORY_END_ADDRESS); + // InitBoardMemory((void*)MEMORY_START_ADDRESS, (void*)MEMORY_END_ADDRESS); #ifdef SEPARATE_COMPILE diff --git a/board/stm32f407zgt6/third_party_driver/extmem/connect_fsmc.c b/board/stm32f407zgt6/third_party_driver/extmem/connect_fsmc.c index 3e6993ca..232154a1 100644 --- a/board/stm32f407zgt6/third_party_driver/extmem/connect_fsmc.c +++ b/board/stm32f407zgt6/third_party_driver/extmem/connect_fsmc.c @@ -18,6 +18,7 @@ * @date 2021-05-28 */ +#include #include "connect_fsmc.h" #include "hardware_fsmc.h" #include "hardware_gpio.h" @@ -173,6 +174,11 @@ int HwSramInit(void) #endif FSMC_NORSRAMInit(&hsram); FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE); + + extern void ExtSramInitBoardMemory(void *start_phy_address, void *end_phy_address, uint8 extsram_idx); + #define START_ADDRESS 0x68000000 + ExtSramInitBoardMemory((void*)(START_ADDRESS), (void*)((START_ADDRESS + BANK1_NORSRAM3_SIZE)), 2); + #endif #ifdef BSP_USING_FSMC_BANK1_NORSRAM4 diff --git a/kernel/include/xs_memory.h b/kernel/include/xs_memory.h index c54b4fc2..ad404cdb 100644 --- a/kernel/include/xs_memory.h +++ b/kernel/include/xs_memory.h @@ -85,6 +85,9 @@ void FreeBlockMemGather(void *data_block); #endif void InitBoardMemory(void *begin_addr, void *end_addr); +#ifdef DATA_IN_ExtSRAM +void ExtSramInitBoardMemory(void *start_phy_address, void *end_phy_address, uint8 extsram_idx); +#endif void *x_malloc(x_size_t nbytes); void x_free(void *ptr); void *x_realloc(void *ptr, x_size_t nbytes); diff --git a/kernel/kernel_test/extsram_test.c b/kernel/kernel_test/extsram_test.c index 58e60820..1e074143 100644 --- a/kernel/kernel_test/extsram_test.c +++ b/kernel/kernel_test/extsram_test.c @@ -3,7 +3,7 @@ /* parameters for sram peripheral */ /* stm32f4 Bank3:0X68000000 */ -#define SRAM_BANK_ADDR ((uint32_t)0X60000000) +#define SRAM_BANK_ADDR ((uint32_t)0X68000000) /* data width: 8, 16, 32 */ #define SRAM_DATA_WIDTH 16 /* sram size */ diff --git a/kernel/kernel_test/test_mem.c b/kernel/kernel_test/test_mem.c index 286f673c..8b1ca2ae 100644 --- a/kernel/kernel_test/test_mem.c +++ b/kernel/kernel_test/test_mem.c @@ -140,10 +140,10 @@ int FendGrin() int tempvalue = 1; while( i< MEM_GRIN_COUNT) { - KPrintf("\033[32;1malloc memory [%d]\033[0m\n",tempvalue); ptr[i] = x_malloc(tempvalue); if (ptr[i]) { + KPrintf("\033[32;1malloc memory [%d] ptr[%d] = 0x%x\033[0m\n",tempvalue,i,ptr[i]); arr_grin[i] = tempvalue; i++; tempvalue++; diff --git a/kernel/memory/byte_manage.c b/kernel/memory/byte_manage.c index fdebc105..72a745e8 100644 --- a/kernel/memory/byte_manage.c +++ b/kernel/memory/byte_manage.c @@ -22,6 +22,8 @@ #include #include +#define DATA_IN_ExtSRAM + #define MEM_STATS /* Covert pointer to other structure */ @@ -43,6 +45,13 @@ /* These masks are used to get the flags and data field of memory blocks */ #define STATIC_BLOCK_MASK 0x80000000 #define DYNAMIC_BLOCK_MASK 0x40000000 +#define DYNAMIC_BLOCK_NO_EXTMEM_MASK DYNAMIC_BLOCK_MASK +// #define DYNAMIC_BLOCK_EXTMEM1_MASK 0x40010000 ///< dynamic memory block external SRAM 1 +// #define DYNAMIC_BLOCK_EXTMEM2_MASK 0x40020000 ///< dynamic memory block external SRAM 2 +// #define DYNAMIC_BLOCK_EXTMEM3_MASK 0x40030000 ///< dynamic memory block external SRAM 3 +// #define DYNAMIC_BLOCK_EXTMEM4_MASK 0x40040000 ///< dynamic memory block external SRAM 4 +#define DYNAMIC_BLOCK_EXTMEMn_MASK(n) (DYNAMIC_BLOCK_MASK | (0xFF & n) << 16) + #define ALLOC_BLOCK_MASK 0xc0000000 #define DYNAMIC_REMAINING_MASK 0x3fffffff @@ -59,6 +68,7 @@ struct DynamicAllocNode { x_size_t size; /* the size of dynamicAllocNode */ uint32 prev_adj_size; /* the size of the previous adjacent node, (dynamic alloc node or dynamic free node */ + uint32 flag; /* |static_dynamic[32-24]|ext_sram[23-16]|res[15-8]|res[7-0]| */ }; /** @@ -68,7 +78,8 @@ struct DynamicFreeNode { x_size_t size; /* the size of dynamicAllocNode */ uint32 prev_adj_size; /* the size of the previous adjacent node, (dynamic alloc node or dynamic free node */ - + uint32 flag; /* |static_dynamic_region_flag[32-24]|ext_sram_idx[23-16]|res[15-8]|res[7-0]| */ + struct DynamicFreeNode *next; struct DynamicFreeNode *prev; }; @@ -144,7 +155,7 @@ struct StaticMemoryDone struct DynamicBuddyMemoryDone { void (*init)(struct DynamicBuddyMemory *dynamic_buddy, x_ubase dynamic_buddy_start,x_ubase dynamic_buddy_size); - void* (*malloc)(struct DynamicBuddyMemory *dynamic_buddy, x_size_t size); + void* (*malloc)(struct DynamicBuddyMemory *dynamic_buddy, x_size_t size, uint32 extsram_mask); void (*release)(struct ByteMemory *byte_memory, void *pointer); int (*JudgeLegal)(struct DynamicBuddyMemory *dynamic_buddy, void *pointer); }; @@ -155,6 +166,10 @@ static struct ByteMemory ByteManager; #ifdef SEPARATE_COMPILE static struct ByteMemory UserByteManager; #endif + +#ifdef DATA_IN_ExtSRAM +static struct ByteMemory ExtByteManager[EXTSRAM_MAX_NUM] = {0}; +#endif /** * This function determines whether the address is valid. * @@ -187,7 +202,8 @@ static int SmallMemTypeAlloc(struct DynamicAllocNode *address) { NULL_PARAM_CHECK(address); - if(address->prev_adj_size & STATIC_BLOCK_MASK) { + // if(address->prev_adj_size & STATIC_BLOCK_MASK) { + if(address->flag & STATIC_BLOCK_MASK) { return RET_TRUE; } @@ -205,7 +221,8 @@ static int MmAllocNode(struct DynamicAllocNode *memory_ptr) { NULL_PARAM_CHECK(memory_ptr); - if(memory_ptr->prev_adj_size & ALLOC_BLOCK_MASK) { + // if(memory_ptr->prev_adj_size & ALLOC_BLOCK_MASK) { + if(memory_ptr->flag & ALLOC_BLOCK_MASK) { return RET_TRUE; } return 0; @@ -233,7 +250,7 @@ static int CaculateBuddyIndex(x_size_t size) } else { ndx = MEM_LINKNRS - 1; } - + KPrintf("hehehe ndx = %d, size = %d\n",ndx, size); return ndx; } @@ -289,21 +306,28 @@ static void InitBuddy(struct DynamicBuddyMemory *dynamic_buddy, x_ubase dynamic_ /* record the start boundary of dynamic buddy memory */ dynamic_buddy->mm_dynamic_start[0] = PTR2ALLOCNODE(dynamic_buddy_start); dynamic_buddy->mm_dynamic_start[0]->size = SIZEOF_DYNAMICALLOCNODE_MEM; - dynamic_buddy->mm_dynamic_start[0]->prev_adj_size = DYNAMIC_BLOCK_MASK; + dynamic_buddy->mm_dynamic_start[0]->prev_adj_size = 0; + dynamic_buddy->mm_dynamic_start[0]->flag = DYNAMIC_BLOCK_MASK; /* the initialized free node */ node =(struct DynamicFreeNode *) ((x_ubase)dynamic_buddy_start + SIZEOF_DYNAMICALLOCNODE_MEM); - node->size=(uint32)(dynamic_buddy_size - 2* SIZEOF_DYNAMICALLOCNODE_MEM); + KPrintf("kaishi : 0x%x, node : 0x%x, size : 0x%x\n", (x_ubase)dynamic_buddy_start, node, dynamic_buddy_size); + node->size=(dynamic_buddy_size - 2* SIZEOF_DYNAMICALLOCNODE_MEM); + KPrintf("hahaha size = %d\n", node->size); node->prev_adj_size= SIZEOF_DYNAMICALLOCNODE_MEM; + node->flag= DYNAMIC_BLOCK_MASK; /* record the end boundary of dynamic buddy memory */ dynamic_buddy->mm_dynamic_end[0] = PTR2ALLOCNODE((x_ubase)dynamic_buddy_start + (x_ubase)dynamic_buddy_size - SIZEOF_DYNAMICALLOCNODE_MEM); dynamic_buddy->mm_dynamic_end[0]->size = SIZEOF_DYNAMICALLOCNODE_MEM; dynamic_buddy->mm_dynamic_end[0]->prev_adj_size = node->size; - dynamic_buddy->mm_dynamic_end[0]->prev_adj_size |= DYNAMIC_BLOCK_MASK; + // dynamic_buddy->mm_dynamic_end[0]->prev_adj_size |= DYNAMIC_BLOCK_MASK; + dynamic_buddy->mm_dynamic_end[0]->flag = DYNAMIC_BLOCK_MASK; /* insert node into dynamic buddy memory */ AddNewNodeIntoBuddy(dynamic_buddy,node); + KPrintf("14 : %d\n", dynamic_buddy->mm_freenode_list[14].size); + KPrintf("dynamic_buddy_start = 0x%x,dynamic_buddy_size = 0x%x\n",dynamic_buddy_start,dynamic_buddy_size); } /** @@ -311,13 +335,15 @@ static void InitBuddy(struct DynamicBuddyMemory *dynamic_buddy, x_ubase dynamic_ * * @param dynamic_buddy the heart dynamic buddy structure * @param size the memory size to be allocated + * @param extsram_mask mask the memory region comes from ext sram * * @return pointer address on success; NULL on failure */ -static void* BigMemMalloc(struct DynamicBuddyMemory *dynamic_buddy, x_size_t size) +static void* BigMemMalloc(struct DynamicBuddyMemory *dynamic_buddy, x_size_t size, uint32 extsram_mask) { int ndx = 0; - x_size_t allocsize = 0; + // x_size_t allocsize = 0; + uint32 allocsize = 0; void *result = NONE; struct DynamicFreeNode *node = NONE; @@ -325,7 +351,7 @@ static void* BigMemMalloc(struct DynamicBuddyMemory *dynamic_buddy, x_size_t siz /* calculate the real size */ allocsize = size + SIZEOF_DYNAMICALLOCNODE_MEM; - +KPrintf("allocsize = %d\n", allocsize); /* if the size exceeds the upper limit, return MEM_LINKNRS - 1 */ if (allocsize >= MEM_HIGH_RANGE) { ndx = MEM_LINKNRS - 1; @@ -339,12 +365,13 @@ static void* BigMemMalloc(struct DynamicBuddyMemory *dynamic_buddy, x_size_t siz node && (node->size < allocsize); node = node->next) { }; - + KPrintf("node 0x%x mm_freenode_list[%d] = 0x%x dynamic_buddy_start = 0x%x allocsize = %d\n",node,ndx,&dynamic_buddy->mm_freenode_list[ndx],dynamic_buddy->dynamic_buddy_start,allocsize); /* get the best-fit freeNode */ if (node && (node->size > allocsize)) { struct DynamicFreeNode *remainder; struct DynamicFreeNode *next; - x_size_t remaining; + // x_size_t remaining; + uint32 remaining; node->prev->next = node->next; if (node->next) { @@ -352,31 +379,42 @@ static void* BigMemMalloc(struct DynamicBuddyMemory *dynamic_buddy, x_size_t siz } remaining = node->size - allocsize; + KPrintf("node = 0x%x node size = %d alloc size = %d remaining size = %d \n",node,node->size,allocsize,remaining); if (remaining >= MEM_LOW_RANGE){ next = PTR2FREENODE(((char *)node) + node->size); /* create the remainder node */ remainder = PTR2FREENODE(((char *)node) + allocsize); remainder->size = remaining; + // KPrintf("remainder 0x%x node 0x%x next 0x%x,a s %d r %d remainder s %d fg 0x%x\n",remainder,node,next,allocsize,remaining,remainder->size,remainder->flag); + remainder->prev_adj_size = allocsize; + // KPrintf("0 remainder 0x%x node 0x%x next 0x%x,a s %d r %d remainder s %d, p s = %d fg 0x%x\n",remainder,node,next,allocsize,remaining,remainder->size,remainder->prev_adj_size,remainder->flag); /* adjust the size of the node */ node->size = allocsize; - next->prev_adj_size = (remaining|(next->prev_adj_size & ALLOC_BLOCK_MASK)); + // next->prev_adj_size = (remaining|(next->prev_adj_size & ALLOC_BLOCK_MASK)); + next->prev_adj_size = remaining; /* insert the remainder freeNode back into the dynamic buddy memory */ + KPrintf("1 remainder 0x%x node 0x%x next 0x%x,a s %d r %d remainder s %d, p s = %d fg 0x%x\n",remainder,node,next,node->size,remaining,remainder->size,remainder->prev_adj_size,remainder->flag); AddNewNodeIntoBuddy(dynamic_buddy, remainder); } /* handle the case of an exact size match */ - node->prev_adj_size &= DYNAMIC_REMAINING_MASK; - node->prev_adj_size |= DYNAMIC_BLOCK_MASK; + // node->prev_adj_size &= DYNAMIC_REMAINING_MASK; + // node->prev_adj_size |= DYNAMIC_BLOCK_MASK; + + node->flag = extsram_mask; + // KPrintf("node flag = 0x%x prev_adj_size = 0x%x\n",node->flag,node->prev_adj_size); result = (void *)((char *)node + SIZEOF_DYNAMICALLOCNODE_MEM); } /* failure allocation */ if(result == NONE) { +#ifndef DATA_IN_ExtSRAM KPrintf("%s: allocation failed, size %d.\n", __func__,allocsize); +#endif return result; } @@ -413,7 +451,8 @@ static void BigMemFree( struct ByteMemory *byte_memory, void *pointer) /* get the next sibling freeNode */ next = PTR2FREENODE((char*)node+node->size); - if(((next->prev_adj_size & DYNAMIC_BLOCK_MASK) == 0)) { + // if(((next->prev_adj_size & DYNAMIC_BLOCK_MASK) == 0)) { + if(((next->flag & DYNAMIC_BLOCK_MASK) == 0)) { struct DynamicAllocNode *andbeyond; andbeyond = PTR2ALLOCNODE((char*)next + next->size); @@ -423,21 +462,25 @@ static void BigMemFree( struct ByteMemory *byte_memory, void *pointer) } node->size += next->size; - andbeyond->prev_adj_size = (node->size | (andbeyond->prev_adj_size & ALLOC_BLOCK_MASK)); + // andbeyond->prev_adj_size = (node->size | (andbeyond->prev_adj_size & ALLOC_BLOCK_MASK)); + andbeyond->prev_adj_size = node->size; next = (struct DynamicFreeNode*)andbeyond; } /* get the prev sibling freeNode */ - prev = (struct DynamicFreeNode*)((char*)node - (node->prev_adj_size & DYNAMIC_REMAINING_MASK)); - if((prev->prev_adj_size & DYNAMIC_BLOCK_MASK)==0) { + // prev = (struct DynamicFreeNode*)((char*)node - (node->prev_adj_size & DYNAMIC_REMAINING_MASK)); + prev = (struct DynamicFreeNode*)((char*)node - node->prev_adj_size ); + // if((prev->prev_adj_size & DYNAMIC_BLOCK_MASK)==0) { + if((prev->flag & DYNAMIC_BLOCK_MASK)==0) { prev->prev->next=prev->next; if(prev->next){ prev->next->prev = prev->prev; } prev->size += node->size; - next->prev_adj_size = (prev->size | (next->prev_adj_size & ALLOC_BLOCK_MASK)); + // next->prev_adj_size = (prev->size | (next->prev_adj_size & ALLOC_BLOCK_MASK)); + next->prev_adj_size = prev->size; node = prev; } - node->prev_adj_size &= DYNAMIC_REMAINING_MASK; + // node->prev_adj_size &= DYNAMIC_REMAINING_MASK; /* insert freeNode into dynamic buddy memory */ AddNewNodeIntoBuddy(&byte_memory->dynamic_buddy_manager,node); @@ -466,7 +509,7 @@ static void SmallMemInit(struct ByteMemory *byte_memory) item = &byte_memory->static_manager[MM_SEGMENT_32B]; /* allocate memory zone for [32b] */ - item->freelist = byte_memory->dynamic_buddy_manager.done->malloc(&byte_memory->dynamic_buddy_manager, SMALL_SIZE_32B(SIZEOF_32B)); + item->freelist = byte_memory->dynamic_buddy_manager.done->malloc(&byte_memory->dynamic_buddy_manager, SMALL_SIZE_32B(SIZEOF_32B), DYNAMIC_BLOCK_NO_EXTMEM_MASK); if(!item->freelist) { KPrintf("%s: no memory for small memory[32B].\n",__func__); item->block_free_count = 0; @@ -481,14 +524,14 @@ static void SmallMemInit(struct ByteMemory *byte_memory) for(offset = 0; offset < item->block_total_count; offset++) { node = PTR2ALLOCNODE((char*)item->freelist + offset * (SIZEOF_32B + SIZEOF_DYNAMICALLOCNODE_MEM)); node->size =(x_size_t) ((char*)item->freelist + (offset + 1) * (SIZEOF_32B + SIZEOF_DYNAMICALLOCNODE_MEM)); - node->prev_adj_size = STATIC_BLOCK_MASK; + node->flag = STATIC_BLOCK_MASK; } node->size = NONE; item = &byte_memory->static_manager[MM_SEGMENT_64B]; /* allocate memory zone for [64B] */ - item->freelist = byte_memory->dynamic_buddy_manager.done->malloc(&byte_memory->dynamic_buddy_manager, SMALL_SIZE_64B(SIZEOF_64B)); + item->freelist = byte_memory->dynamic_buddy_manager.done->malloc(&byte_memory->dynamic_buddy_manager, SMALL_SIZE_64B(SIZEOF_64B),DYNAMIC_BLOCK_NO_EXTMEM_MASK); if(!item->freelist) { KPrintf("%s: no memory for small memory[64B].\n",__func__); return; @@ -502,7 +545,7 @@ static void SmallMemInit(struct ByteMemory *byte_memory) for(offset = 0; offset < item->block_total_count; offset++) { node = PTR2ALLOCNODE((char*)item->freelist + offset * (SIZEOF_64B + SIZEOF_DYNAMICALLOCNODE_MEM)); node->size =(x_size_t) ((char*)item->freelist + (offset + 1) * (SIZEOF_64B + SIZEOF_DYNAMICALLOCNODE_MEM)); - node->prev_adj_size = STATIC_BLOCK_MASK; + node->flag = STATIC_BLOCK_MASK; } node->size = NONE; @@ -548,6 +591,7 @@ static void SmallMemFree(void *pointer) */ static void *SmallMemMalloc(struct ByteMemory *byte_memory, x_size_t size) { + uint8 i = 0; void *result = NONE; struct DynamicAllocNode *node = NONE; struct segment *static_segment = NONE; @@ -564,7 +608,7 @@ static void *SmallMemMalloc(struct ByteMemory *byte_memory, x_size_t size) /* get the head static memory block */ result = static_segment->freelist; node = PTR2ALLOCNODE(static_segment->freelist); - node->prev_adj_size = STATIC_BLOCK_MASK; + node->flag = STATIC_BLOCK_MASK; /* update the statistic information of static segment */ static_segment->freelist = (uint8 *)(long)(node->size); @@ -578,7 +622,20 @@ static void *SmallMemMalloc(struct ByteMemory *byte_memory, x_size_t size) } /* the static memory block is exhausted, now turn to dynamic buddy memory for allocation. */ - result = byte_memory->dynamic_buddy_manager.done->malloc(&byte_memory->dynamic_buddy_manager, size); + result = byte_memory->dynamic_buddy_manager.done->malloc(&byte_memory->dynamic_buddy_manager, size, DYNAMIC_BLOCK_NO_EXTMEM_MASK); +#ifdef DATA_IN_ExtSRAM + if(NONE == result) { + for(i = 0; i < EXTSRAM_MAX_NUM; i++) { + if(NONE != ExtByteManager[i].done) { + result = ExtByteManager[i].dynamic_buddy_manager.done->malloc(&ExtByteManager[i].dynamic_buddy_manager, size, DYNAMIC_BLOCK_EXTMEMn_MASK(i + 1)); + if (result){ + CHECK(ExtByteManager[i].dynamic_buddy_manager.done->JudgeLegal(&ExtByteManager[i].dynamic_buddy_manager, ret - SIZEOF_DYNAMICALLOCNODE_MEM)); + break; + } + } + } + } +#endif return result; } @@ -597,18 +654,31 @@ static struct StaticMemoryDone StaticDone = { */ void *x_malloc(x_size_t size) { + uint8 i = 0; void *ret = NONE; register x_base lock = 0; - /* parameter detection */ + // /* parameter detection */ + // if((size == 0) || (size > ByteManager.dynamic_buddy_manager.dynamic_buddy_end - ByteManager.dynamic_buddy_manager.dynamic_buddy_start - ByteManager.dynamic_buddy_manager.active_memory)) + // return NONE; +#ifdef DATA_IN_ExtSRAM + /* parameter detection */ + if(size == 0 ){ + return NONE; + } + // if((size > ByteManager.dynamic_buddy_manager.dynamic_buddy_end - ByteManager.dynamic_buddy_manager.dynamic_buddy_start - ByteManager.dynamic_buddy_manager.active_memory)){ + // goto try_extmem; + // } + +#else + /* parameter detection */ if((size == 0) || (size > ByteManager.dynamic_buddy_manager.dynamic_buddy_end - ByteManager.dynamic_buddy_manager.dynamic_buddy_start - ByteManager.dynamic_buddy_manager.active_memory)) return NONE; - +#endif /* hold lock before allocation */ lock = CriticalAreaLock(); /* alignment */ size = ALIGN_MEN_UP(size, MEM_ALIGN_SIZE); - /* determine allocation operation from static segments or dynamic buddy memory */ #ifdef KERNEL_SMALL_MEM_ALLOC if(size <= SIZEOF_32B) { @@ -618,10 +688,28 @@ void *x_malloc(x_size_t size) } else #endif { - ret = ByteManager.dynamic_buddy_manager.done->malloc(&ByteManager.dynamic_buddy_manager,size); + ret = ByteManager.dynamic_buddy_manager.done->malloc(&ByteManager.dynamic_buddy_manager, size, DYNAMIC_BLOCK_NO_EXTMEM_MASK); + if(ret != NONE) + CHECK(ByteManager.dynamic_buddy_manager.done->JudgeLegal(&ByteManager.dynamic_buddy_manager, ret - SIZEOF_DYNAMICALLOCNODE_MEM)); +// try_extmem: +#ifdef DATA_IN_ExtSRAM + if(NONE == ret) { + KPrintf("wwg debug ...\n"); + for(i = 0; i < EXTSRAM_MAX_NUM; i++) { + if(NONE != ExtByteManager[i].done) { + KPrintf(" ExtByteManager[%d].done = 0x%x \n",i,ExtByteManager[i].done); + ret = ExtByteManager[i].dynamic_buddy_manager.done->malloc(&ExtByteManager[i].dynamic_buddy_manager, size, DYNAMIC_BLOCK_EXTMEMn_MASK(i + 1)); + if (ret){ + CHECK(ExtByteManager[i].dynamic_buddy_manager.done->JudgeLegal(&ExtByteManager[i].dynamic_buddy_manager, ret - SIZEOF_DYNAMICALLOCNODE_MEM)); + break; + } + KPrintf("ret = 0x%x \n",ret); + } + } + } +#endif } - if(ret != NONE) - CHECK(ByteManager.dynamic_buddy_manager.done->JudgeLegal(&ByteManager.dynamic_buddy_manager, ret - SIZEOF_DYNAMICALLOCNODE_MEM)); + KPrintf("malloc i = %d size %d ret = 0x%x\n",i,size,ret); /* release lock */ CriticalAreaUnLock(lock); return ret; @@ -726,20 +814,83 @@ void x_free(void *pointer) lock = CriticalAreaLock(); node = PTR2ALLOCNODE((char*)pointer-SIZEOF_DYNAMICALLOCNODE_MEM); CHECK(ByteManager.done->JudgeAllocated(node)); - + /* judge release the memory block ro static_segment or dynamic buddy memory */ #ifdef KERNEL_SMALL_MEM_ALLOC - if(node->prev_adj_size & STATIC_BLOCK_MASK) { + if(node->flag & STATIC_BLOCK_MASK) { ByteManager.static_manager->done->release(pointer); } else #endif { +#ifdef DATA_IN_ExtSRAM + /* judge the pointer is not malloced from extern memory*/ + if(0 == (node->flag & 0xFF0000)) { ByteManager.dynamic_buddy_manager.done->release(&ByteManager,pointer); } + + /* judge the pointer is malloced from extern memory*/ + if(0 != (node->flag & 0xFF0000)) { + KPrintf("wwg debug node->flag = 0x%x\n",node->flag); + ExtByteManager[((node->flag & 0xFF0000) >> 16) - 1].dynamic_buddy_manager.done->release(&ExtByteManager[((node->flag & 0xFF0000) >> 16) - 1],pointer); + } +#else + ByteManager.dynamic_buddy_manager.done->release(&ByteManager,pointer); +#endif + } /* release the lock */ CriticalAreaUnLock(lock); } +#ifdef DATA_IN_ExtSRAM +/** + * This function initializes the dynamic buddy memory of extern sram. + * + * @param start_phy_address the start physical address for static and dynamic memory + * @param end_phy_address the end physical address for static and dynamic memory + * @param extsram_idx the idx of extsram chip + */ +void ExtSramInitBoardMemory(void *start_phy_address, void *end_phy_address, uint8 extsram_idx) +{ + register x_size_t offset = 0; + + NULL_PARAM_CHECK(start_phy_address); + NULL_PARAM_CHECK(end_phy_address); + + KDEBUG_NOT_IN_INTERRUPT; + struct DynamicBuddyMemory *uheap = &ExtByteManager[extsram_idx].dynamic_buddy_manager; + + /* align begin and end addr to page */ + ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_start = ALIGN_MEN_UP((x_ubase)start_phy_address, MM_PAGE_SIZE); + ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_end = ALIGN_MEN_DOWN((x_ubase)end_phy_address, MM_PAGE_SIZE); + KPrintf("%s: 0x%x-0x%x extsram_idx = %d\n",__func__,ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_start,ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_end, extsram_idx); + + /* parameter detection */ + if (ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_start >= ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_end) { + KPrintf("ExtSramInitBoardMemory, wrong address[0x%x - 0x%x]\n", + (x_ubase)start_phy_address, (x_ubase)end_phy_address); + return; + } + + uheap->mm_total_size = 0; + memset(uheap->mm_freenode_list, 0, SIZEOF_XSFREENODE_MEM * MEM_LINKNRS); + + /* initialize the freeNodeList */ + for (offset = 1; offset < MEM_LINKNRS; offset++) { + uheap->mm_freenode_list[offset - 1].next = &uheap->mm_freenode_list[offset]; + uheap->mm_freenode_list[offset].prev = &uheap->mm_freenode_list[offset - 1]; + } + + ExtByteManager[extsram_idx].dynamic_buddy_manager.done = &DynamicDone; + ExtByteManager[extsram_idx].done = &NodeDone; + + + /* dynamic buddy memory initialization */ + ExtByteManager[extsram_idx].dynamic_buddy_manager.done->init(&ExtByteManager[extsram_idx].dynamic_buddy_manager, ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_start, ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_end - ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_start); + KPrintf("[0x%x - 0x%x]ExtByteManager[%d].dynamic_buddy_manager.done = 0x%x\n",start_phy_address,end_phy_address,extsram_idx,ExtByteManager[extsram_idx].dynamic_buddy_manager.done); + +} +#endif + /** * This function initializes the static segments and dynamic buddy memory structures. * @@ -1025,7 +1176,7 @@ void ShowBuddy(void) KPrintf("\n\033[41;1mlist memory information\033[0m\n", __func__); for (debug = ByteManager.dynamic_buddy_manager.mm_freenode_list[0].next; debug;debug = debug->next){ - KPrintf("%s,current is %x,next is %x, size %u, flag %x\n",__func__, debug, debug->next,debug->size,debug->prev_adj_size & ALLOC_BLOCK_MASK); + KPrintf("%s,current is %x,next is %x, size %u, flag %x\n",__func__, debug, debug->next,debug->size,debug->flag & ALLOC_BLOCK_MASK); }; KPrintf("\nlist memory information\n\n"); CriticalAreaUnLock(lock);