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 (mpl@broadcom.com)
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 "sbmips.h"
49
50#include "lib_types.h"
51#include "lib_string.h"
52#include "lib_queue.h"
53#include "lib_malloc.h"
54#include "lib_printf.h"
55#include "lib_arena.h"
56
57#include "cfe_error.h"
58
59#include "cfe.h"
60#include "cfe_mem.h"
61
62#include "initdata.h"
63
64#define _NOPROTOS_
65#include "cfe_boot.h"
66#undef _NOPROTOS_
67
68
69/*  *********************************************************************
70    *  Macros
71    ********************************************************************* */
72
73#define ARENA_RANGE(bottom,top,type) arena_markrange(&cfe_arena,(uint64_t)(bottom), \
74                                     (uint64_t)(top)-(uint64_t)bottom+1,(type),NULL)
75
76#define MEG	(1024*1024)
77#define KB      1024
78#define PAGESIZE 4096
79#define CFE_BOOTAREA_SIZE (256*KB)
80#define CFE_BOOTAREA_ADDR 0x20000000
81
82/*  *********************************************************************
83    *  Globals
84    ********************************************************************* */
85
86extern arena_t cfe_arena;
87
88unsigned int mem_bootarea_start;
89unsigned int mem_bootarea_size;
90
91void sb1250_arena_init(void);
92void sb1250_pagetable_init(uint64_t *ptaddr,unsigned int physaddr);
93
94
95/*  *********************************************************************
96    *  sb1250_arena_init()
97    *
98    *  Create the initial map of physical memory
99    *
100    *  Input parameters:
101    *  	   nothing
102    *
103    *  Return value:
104    *  	   nothing
105    ********************************************************************* */
106
107void sb1250_arena_init(void)
108{
109    int64_t memleft;
110    int64_t mem256 = (256*1024*1024);		/* 256 MBytes */
111    int64_t memamt;
112
113    arena_init(&cfe_arena,0x0,0x10000000000);
114
115    /*
116     * Mark the ranges from the SB1250's memory map
117     */
118
119    ARENA_RANGE(0x0000000000,0x000FFFFFFF,MEMTYPE_DRAM_NOTINSTALLED);
120    ARENA_RANGE(0x0080000000,0x009FFFFFFF,MEMTYPE_DRAM_NOTINSTALLED);
121    ARENA_RANGE(0x00C0000000,0x00CFFFFFFF,MEMTYPE_DRAM_NOTINSTALLED);
122    ARENA_RANGE(0x0100000000,0x7FFFFFFFFF,MEMTYPE_DRAM_NOTINSTALLED);
123    /*
124     * Mark LDT and PCI regions
125     */
126
127    ARENA_RANGE(0x0040000000,0x005FFFFFFF,MEMTYPE_LDT_PCI);
128    ARENA_RANGE(0x0060000000,0x007FFFFFFF,MEMTYPE_LDT_PCI);
129
130    ARENA_RANGE(0x00D8000000,0x00DFFFFFFF,MEMTYPE_LDT_PCI);
131
132    ARENA_RANGE(0x00F8000000,0x00FFFFFFFF,MEMTYPE_LDT_PCI);
133    ARENA_RANGE(0xF800000000,0xF9FFFFFFFF,MEMTYPE_LDT_PCI);
134
135    ARENA_RANGE(0xFD00000000,0xFFFFFFFFFF,MEMTYPE_LDT_PCI);
136
137    /*
138     * System IO registers
139     */
140
141    ARENA_RANGE(0x0010000000,0x001002FFFF,MEMTYPE_IOREGISTERS);
142
143    /*
144     * Now, fix up the map with what is known about *this* system.
145     *
146     * Do each 256MB chunk.
147     */
148
149    memleft = ((int64_t) mem_totalsize) << 20;
150
151    memamt = (memleft > mem256) ? mem256 : memleft;
152
153    arena_markrange(&cfe_arena,0x00000000,memamt,MEMTYPE_DRAM_AVAILABLE,NULL);
154    memleft -= memamt;
155
156    if (memleft) {
157	memamt = (memleft > mem256*2) ? mem256*2 : memleft;
158	arena_markrange(&cfe_arena,0x80000000,memamt,MEMTYPE_DRAM_AVAILABLE,NULL);
159	memleft -= memamt;
160	}
161
162    if (memleft) {
163	memamt = (memleft > mem256) ? mem256 : memleft;
164	arena_markrange(&cfe_arena,0xC0000000,memamt,MEMTYPE_DRAM_AVAILABLE,NULL);
165	memleft -= memamt;
166	}
167
168    if (memleft) {
169	arena_markrange(&cfe_arena,0x100000000,memleft,MEMTYPE_DRAM_AVAILABLE,NULL);
170	}
171
172
173    /*
174     * Do the boot ROM
175     */
176
177    arena_markrange(&cfe_arena,0x1FC00000,2*1024*1024,MEMTYPE_BOOTROM,NULL);
178
179}
180
181
182/*  *********************************************************************
183    *  SB1250_PAGETABLE_INIT(ptaddr,physaddr)
184    *
185    *  This routine constructs the page table.  256KB is mapped
186    *  starting at physical address 'physaddr' - the resulting
187    *  table entries are placed at 'ptaddr'
188    *
189    *  Input parameters:
190    *  	   ptaddr - base of page table
191    *  	   physaddr - starting physical addr of area to map
192    *
193    *  Return value:
194    *  	   nothing
195    ********************************************************************* */
196
197void sb1250_pagetable_init(uint64_t *ptaddr,unsigned int physaddr)
198{
199    int idx;
200
201    for (idx = 0; idx < (CFE_BOOTAREA_SIZE/PAGESIZE); idx++) {
202	ptaddr[idx] = (physaddr >> 6) |
203	    V_TLBLO_CALG(K_CALG_COH_SHAREABLE) |
204	    M_TLBLO_V |
205	    M_TLBLO_D;
206	physaddr += PAGESIZE;
207	}
208
209}
210
211