Setting up repository
[linux-libre-firmware.git] / ath9k_htc / sboot / magpie_1_1 / sboot / cmnos / allocram / src / cmnos_allocram.c
1 /*
2  * Copyright (c) 2013 Qualcomm Atheros, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted (subject to the limitations in the
7  * disclaimer below) provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the
15  *    distribution.
16  *
17  *  * Neither the name of Qualcomm Atheros nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
22  * GRANTED BY THIS LICENSE.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
23  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
33  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #include "sys_cfg.h"
36 #include "athos_api.h"
37
38 #if SYSTEM_MODULE_ALLOCRAM
39
40 /*
41  * Startup time RAM allocation.
42  *
43  * Oddly enough, we allow allocation, but not free.
44  * The central idea is to restrict compile-time RAM demands
45  * of modules to a minimum so that if a module is replaced
46  * at run-time large amounts of RAM are not wasted.
47  *
48  * Addresses returned are A_CACHE_LINE_SIZE aligned.
49  */
50
51 LOCAL A_UINT32 allocram_current_addr;
52 LOCAL A_UINT32 allocram_remaining_bytes;
53
54 LOCAL void *
55 cmnos_allocram_init(void *arena_start, A_UINT32 arena_sz)
56 {
57     A_UINT32 astart = (A_UINT32)arena_start;
58
59 #if defined(__XTENSA__)
60     /*
61      * This hacky line converts from a text or data RAM address
62      * into a data RAM address.  (It's all the same on MIPS, but
63      * text and data are different address spaces on Xtensa.)
64      */
65     //astart = TARG_RAM_ADDRS(TARG_RAM_OFFSET(astart));
66 #endif
67
68 #if 0
69     if (arena_sz == 0) {
70         /* Default arena_sz to most of available RAM */
71         arena_sz = TARG_RAM_SZ - (A_UINT32)TARG_RAM_OFFSET(astart);
72         arena_sz -= HOST_INTEREST->hi_end_RAM_reserve_sz;
73     }
74 #endif
75
76     /* Clear entire area */
77 //    A_MEMSET(astart, 0, arena_sz);
78
79     /* Adjust for cache line alignment */
80 #if 0    
81     allocram_current_addr = A_ROUND_UP(astart, A_CACHE_LINE_SIZE);
82     arena_sz -= (allocram_current_addr-astart);
83 #else
84     allocram_current_addr = astart;
85 #endif    
86     allocram_remaining_bytes = arena_sz;
87
88     //A_DCACHE_FLUSH();
89
90     //A_PRINTF("cmnos_allocram_init: start=0x%x size=%d\n",
91     //    allocram_current_addr, allocram_remaining_bytes);
92
93     return NULL; /* Future implementation may return an arena handle */
94 }
95
96 /*
97  * Allocate nbytes from the arena.  At this point, which_arena should
98  * be set to 0 for the default (and only) arena.  A future allocation
99  * module may support multiple separate arenas.
100  */
101 LOCAL void *
102 cmnos_allocram(void * which_arena, A_UINT32 nbytes)
103 {
104     void *ptr = (void *)allocram_current_addr;
105     //nbytes = A_ROUND_UP(nbytes, A_CACHE_LINE_SIZE);
106     nbytes = A_ROUND_UP(nbytes, 4);
107     if (nbytes <= allocram_remaining_bytes) {
108         allocram_remaining_bytes -= nbytes;
109         allocram_current_addr += nbytes;
110     } else {
111         A_PRINTF("RAM allocation (%d bytes) failed!\n", nbytes);
112         //A_ASSERT(0);
113         adf_os_assert(0);
114     }
115
116     return ptr;
117 }
118
119 void
120 cmnos_allocram_debug(void)
121 {
122     A_PRINTF("ALLOCRAM Current Addr 0x%x\n", allocram_current_addr);
123     A_PRINTF("ALLOCRAM Remaining Bytes %d\n", allocram_remaining_bytes);
124 }
125
126 void
127 cmnos_allocram_module_install(struct allocram_api *tbl)
128 {
129     tbl->cmnos_allocram_init                 = cmnos_allocram_init;
130     tbl->cmnos_allocram                      = cmnos_allocram;
131     tbl->cmnos_allocram_debug                = cmnos_allocram_debug;
132 }
133
134 #endif /* SYSTEM_MODULE_ALLOCRAM */
135