ac40c64f59c45237eba5ffcad0f41f6ef93de185
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / build / magpie_1_1 / sboot / cmnos / allocram / src / cmnos_allocram.c
1 /*
2  * Copyright (c) 2007 Atheros Communications Inc.
3  * All rights reserved.
4  */
5
6 #include "sys_cfg.h"
7 #include "athos_api.h"
8
9 #if SYSTEM_MODULE_ALLOCRAM
10
11 /*
12  * Startup time RAM allocation.
13  *
14  * Oddly enough, we allow allocation, but not free.
15  * The central idea is to restrict compile-time RAM demands
16  * of modules to a minimum so that if a module is replaced
17  * at run-time large amounts of RAM are not wasted.
18  *
19  * Addresses returned are A_CACHE_LINE_SIZE aligned.
20  */
21
22 LOCAL A_UINT32 allocram_current_addr;
23 LOCAL A_UINT32 allocram_remaining_bytes;
24
25 LOCAL void *
26 cmnos_allocram_init(void *arena_start, A_UINT32 arena_sz)
27 {
28     A_UINT32 astart = (A_UINT32)arena_start;
29
30 #if defined(__XTENSA__)
31     /*
32      * This hacky line converts from a text or data RAM address
33      * into a data RAM address.  (It's all the same on MIPS, but
34      * text and data are different address spaces on Xtensa.)
35      */
36     //astart = TARG_RAM_ADDRS(TARG_RAM_OFFSET(astart));
37 #endif
38
39 #if 0
40     if (arena_sz == 0) {
41         /* Default arena_sz to most of available RAM */
42         arena_sz = TARG_RAM_SZ - (A_UINT32)TARG_RAM_OFFSET(astart);
43         arena_sz -= HOST_INTEREST->hi_end_RAM_reserve_sz;
44     }
45 #endif
46
47     /* Clear entire area */
48 //    A_MEMSET(astart, 0, arena_sz);
49
50     /* Adjust for cache line alignment */
51 #if 0    
52     allocram_current_addr = A_ROUND_UP(astart, A_CACHE_LINE_SIZE);
53     arena_sz -= (allocram_current_addr-astart);
54 #else
55     allocram_current_addr = astart;
56 #endif    
57     allocram_remaining_bytes = arena_sz;
58
59     //A_DCACHE_FLUSH();
60
61     //A_PRINTF("cmnos_allocram_init: start=0x%x size=%d\n",
62     //    allocram_current_addr, allocram_remaining_bytes);
63
64     return NULL; /* Future implementation may return an arena handle */
65 }
66
67 /*
68  * Allocate nbytes from the arena.  At this point, which_arena should
69  * be set to 0 for the default (and only) arena.  A future allocation
70  * module may support multiple separate arenas.
71  */
72 LOCAL void *
73 cmnos_allocram(void * which_arena, A_UINT32 nbytes)
74 {
75     void *ptr = (void *)allocram_current_addr;
76     //nbytes = A_ROUND_UP(nbytes, A_CACHE_LINE_SIZE);
77     nbytes = A_ROUND_UP(nbytes, 4);
78     if (nbytes <= allocram_remaining_bytes) {
79         allocram_remaining_bytes -= nbytes;
80         allocram_current_addr += nbytes;
81     } else {
82         A_PRINTF("RAM allocation (%d bytes) failed!\n", nbytes);
83         //A_ASSERT(0);
84         adf_os_assert(0);
85     }
86
87     return ptr;
88 }
89
90 void
91 cmnos_allocram_debug(void)
92 {
93     A_PRINTF("ALLOCRAM Current Addr 0x%x\n", allocram_current_addr);
94     A_PRINTF("ALLOCRAM Remaining Bytes %d\n", allocram_remaining_bytes);
95 }
96
97 void
98 cmnos_allocram_module_install(struct allocram_api *tbl)
99 {
100     tbl->cmnos_allocram_init                 = cmnos_allocram_init;
101     tbl->cmnos_allocram                      = cmnos_allocram;
102     tbl->cmnos_allocram_debug                = cmnos_allocram_debug;
103 }
104
105 #endif /* SYSTEM_MODULE_ALLOCRAM */
106