1/*  *********************************************************************
2    *  Broadcom Common Firmware Environment (CFE)
3    *
4    *  Physical Memory (arena) manager		File: sb1250_arena.c
5    *
6    *  This module describes the physical memory available to the
7    *  firmware.
8    *
9    *  Author:  Mitch Lichtenberg
10    *
11    *********************************************************************
12    *
13    *  Copyright 2000,2001,2002,2003
14    *  Broadcom Corporation. All rights reserved.
15    *
16    *  This software is furnished under license and may be used and
17    *  copied only in accordance with the following terms and
18    *  conditions.  Subject to these conditions, you may download,
19    *  copy, install, use, modify and distribute modified or unmodified
20    *  copies of this software in source and/or binary form.  No title
21    *  or ownership is transferred hereby.
22    *
23    *  1) Any source code used, modified or distributed must reproduce
24    *     and retain this copyright notice and list of conditions
25    *     as they appear in the source file.
26    *
27    *  2) No right is granted to use any trade name, trademark, or
28    *     logo of Broadcom Corporation.  The "Broadcom Corporation"
29    *     name may not be used to endorse or promote products derived
30    *     from this software without the prior written permission of
31    *     Broadcom Corporation.
32    *
33    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
34    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
35    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
36    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
37    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
38    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
39    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
41    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
42    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
43    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
44    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
45    *     THE POSSIBILITY OF SUCH DAMAGE.
46    ********************************************************************* */
47
48#include "cfe.h"
49#include "cfe_mem.h"
50#include "lib_arena.h"
51
52#include "initdata.h"
53
54#define _NOPROTOS_
55#include "cfe_boot.h"
56#undef _NOPROTOS_
57
58/*  *********************************************************************
59    *  Constants
60    ********************************************************************* */
61
62
63#define MEG	(1024*1024)
64#define KB      1024
65#define PAGESIZE 4096
66#define CFE_BOOTAREA_SIZE (256*KB)
67#define CFE_BOOTAREA_ADDR 0x20000000
68
69
70/*  *********************************************************************
71    *  Globals
72    ********************************************************************* */
73
74extern arena_t cfe_arena;
75uint64_t *cfe_pagetable = NULL;
76
77extern void CPUCFG_PAGETBLINIT(uint64_t *ptaddr,unsigned int ptstart);
78
79void cfe_bootarea_init(void);
80extern void _setcontext(int64_t);
81
82
83/*  *********************************************************************
84    *  CFE_BOOTAREA_INIT()
85    *
86    *  Initialize the page table and map our boot program area.
87    *
88    *  Input parameters:
89    *  	   nothing
90    *
91    *  Return value:
92    *  	   nothing
93    ********************************************************************* */
94
95void cfe_bootarea_init(void)
96{
97    unsigned char *pte;
98    int64_t pte_int;
99    unsigned int addr = 16*MEG;
100    unsigned int topmem;
101    unsigned int topcfe;
102    unsigned int botcfe;
103    unsigned int beforecfe;
104    unsigned int aftercfe;
105
106    /*
107     * Calculate the location where the boot area will
108     * live.  It lives either above or below the
109     * firmware, depending on where there's more space.
110     */
111
112    /*
113     * The firmware will always be loaded in the first
114     * 256M.  Calculate the top of that region.  The bottom
115     * of that region is always the beginning of our
116     * data segment.
117     */
118    if (mem_totalsize > (uint64_t)256) {
119	topmem = 256*MEG;
120	}
121    else {
122	topmem = (unsigned int) (mem_totalsize << 20);
123	}
124    botcfe = (unsigned int) K1_TO_PHYS(mem_bottomofmem);
125    topcfe = (unsigned int) K1_TO_PHYS(mem_topofmem);
126
127    beforecfe = botcfe;
128    aftercfe = topmem-topcfe;
129
130    if (beforecfe > aftercfe) {
131	botcfe -= (PAGESIZE-1);
132	botcfe &= ~(PAGESIZE-1);	/* round down to page boundary */
133	addr = botcfe - CFE_BOOTAREA_SIZE;	/* this is the address */
134	}
135    else {
136	topcfe += (PAGESIZE-1);		/* round *up* to a page address */
137	topcfe &= ~(PAGESIZE-1);
138	addr = topcfe;
139	}
140
141    mem_bootarea_start = addr;
142    mem_bootarea_size = CFE_BOOTAREA_SIZE;
143
144    /*
145     * Allocate the page table
146     */
147
148    pte = KMALLOC(1024,1024);
149
150#ifdef __long64
151    pte_int = (int64_t) pte;
152#else
153    pte_int = (int64_t) ((int) pte);
154#endif
155
156    /*
157     * Set the CP0 CONTEXT register to point at the page table
158     */
159
160    pte_int <<= 13;
161    cfe_pagetable = (uint64_t *) pte;
162
163    _setcontext(pte_int);
164
165
166    /*
167     * Initialize page table entries
168     */
169
170    CPUCFG_PAGETBLINIT(cfe_pagetable,addr);
171
172
173}
174
175