ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / sgi-ip27 / ip27-memory.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2000 by Ralf Baechle
7  * Copyright (C) 2000 by Silicon Graphics, Inc.
8  * Copyright (C) 2004 by Christoph Hellwig
9  *
10  * On SGI IP27 the ARC memory configuration data is completly bogus but
11  * alternate easier to use mechanisms are available.
12  */
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/mmzone.h>
17 #include <linux/swap.h>
18 #include <linux/bootmem.h>
19 #include <asm/page.h>
20 #include <asm/sections.h>
21
22 #include <asm/sn/arch.h>
23 #include <asm/sn/hub.h>
24 #include <asm/sn/klconfig.h>
25 #include <asm/sn/sn_private.h>
26
27
28 #define PFN_UP(x)               (((x) + PAGE_SIZE-1) >> PAGE_SHIFT)
29
30 #define SLOT_PFNSHIFT           (SLOT_SHIFT - PAGE_SHIFT)
31 #define PFN_NASIDSHFT           (NASID_SHFT - PAGE_SHIFT)
32
33 #define SLOT_IGNORED            0xffff
34
35 static short __initdata slot_lastfilled_cache[MAX_COMPACT_NODES];
36 static unsigned short __initdata slot_psize_cache[MAX_COMPACT_NODES][MAX_MEM_SLOTS];
37 static struct bootmem_data __initdata plat_node_bdata[MAX_COMPACT_NODES];
38
39 struct pglist_data *node_data[MAX_COMPACT_NODES];
40 struct hub_data *hub_data[MAX_COMPACT_NODES];
41
42 static pfn_t __init slot_getbasepfn(cnodeid_t cnode, int slot)
43 {
44         nasid_t nasid = COMPACT_TO_NASID_NODEID(cnode);
45
46         return ((pfn_t)nasid << PFN_NASIDSHFT) | (slot << SLOT_PFNSHIFT);
47 }
48
49 /*
50  * Return the number of pages of memory provided by the given slot
51  * on the specified node.
52  */
53 static pfn_t __init slot_getsize(cnodeid_t node, int slot)
54 {
55         return (pfn_t) slot_psize_cache[node][slot];
56 }
57
58 /*
59  * Return highest slot filled
60  */
61 static int __init node_getlastslot(cnodeid_t node)
62 {
63         return (int) slot_lastfilled_cache[node];
64 }
65
66 /*
67  * Return the pfn of the last free page of memory on a node.
68  */
69 static pfn_t __init node_getmaxclick(cnodeid_t node)
70 {
71         pfn_t   slot_psize;
72         int     slot;
73
74         /*
75          * Start at the top slot. When we find a slot with memory in it,
76          * that's the winner.
77          */
78         for (slot = (MAX_MEM_SLOTS - 1); slot >= 0; slot--) {
79                 if ((slot_psize = slot_getsize(node, slot))) {
80                         if (slot_psize == SLOT_IGNORED)
81                                 continue;
82                         /* Return the basepfn + the slot size, minus 1. */
83                         return slot_getbasepfn(node, slot) + slot_psize - 1;
84                 }
85         }
86
87         /*
88          * If there's no memory on the node, return 0. This is likely
89          * to cause problems.
90          */
91         return 0;
92 }
93
94 static pfn_t __init slot_psize_compute(cnodeid_t node, int slot)
95 {
96         nasid_t nasid;
97         lboard_t *brd;
98         klmembnk_t *banks;
99         unsigned long size;
100
101         nasid = COMPACT_TO_NASID_NODEID(node);
102         /* Find the node board */
103         brd = find_lboard((lboard_t *)KL_CONFIG_INFO(nasid), KLTYPE_IP27);
104         if (!brd)
105                 return 0;
106
107         /* Get the memory bank structure */
108         banks = (klmembnk_t *) find_first_component(brd, KLSTRUCT_MEMBNK);
109         if (!banks)
110                 return 0;
111
112         /* Size in _Megabytes_ */
113         size = (unsigned long)banks->membnk_bnksz[slot/4];
114
115         /* hack for 128 dimm banks */
116         if (size <= 128) {
117                 if (slot % 4 == 0) {
118                         size <<= 20;            /* size in bytes */
119                         return(size >> PAGE_SHIFT);
120                 } else
121                         return 0;
122         } else {
123                 size /= 4;
124                 size <<= 20;
125                 return size >> PAGE_SHIFT;
126         }
127 }
128
129 static void __init szmem(void)
130 {
131         pfn_t slot_psize, slot0sz = 0, nodebytes;       /* Hack to detect problem configs */
132         int slot, ignore;
133         cnodeid_t node;
134
135         num_physpages = 0;
136
137         for (node = 0; node < numnodes; node++) {
138                 ignore = nodebytes = 0;
139                 for (slot = 0; slot < MAX_MEM_SLOTS; slot++) {
140                         slot_psize = slot_psize_compute(node, slot);
141                         if (slot == 0)
142                                 slot0sz = slot_psize;
143                         /*
144                          * We need to refine the hack when we have replicated
145                          * kernel text.
146                          */
147                         nodebytes += (1LL << SLOT_SHIFT);
148                         if ((nodebytes >> PAGE_SHIFT) * (sizeof(struct page)) >
149                                                 (slot0sz << PAGE_SHIFT))
150                                 ignore = 1;
151                         if (ignore && slot_psize) {
152                                 printk("Ignoring slot %d onwards on node %d\n",
153                                                                 slot, node);
154                                 slot_psize_cache[node][slot] = SLOT_IGNORED;
155                                 slot = MAX_MEM_SLOTS;
156                                 continue;
157                         }
158                         num_physpages += slot_psize;
159                         slot_psize_cache[node][slot] =
160                                         (unsigned short) slot_psize;
161                         if (slot_psize)
162                                 slot_lastfilled_cache[node] = slot;
163                 }
164         }
165 }
166
167 /*
168  * Currently, the intranode memory hole support assumes that each slot
169  * contains at least 32 MBytes of memory. We assume all bootmem data
170  * fits on the first slot.
171  */
172 void __init prom_meminit(void)
173 {
174         cnodeid_t node;
175
176         mlreset();
177         szmem();
178
179         for (node = 0; node < numnodes; node++) {
180                 pfn_t slot_firstpfn = slot_getbasepfn(node, 0);
181                 pfn_t slot_lastpfn = slot_firstpfn + slot_getsize(node, 0);
182                 pfn_t slot_freepfn = node_getfirstfree(node);
183                 unsigned long bootmap_size;
184
185                 /*
186                  * Allocate the node data structures on the node first.
187                  */
188                 node_data[node] = __va(slot_freepfn << PAGE_SHIFT);
189                 node_data[node]->bdata = &plat_node_bdata[node];
190
191                 hub_data[node] = (struct hub_data *)(node_data[node] + 1);
192
193                 cpus_clear(hub_data[node]->h_cpus);
194
195                 slot_freepfn += PFN_UP(sizeof(struct pglist_data) +
196                                        sizeof(struct hub_data));
197         
198                 bootmap_size = init_bootmem_node(NODE_DATA(node), slot_freepfn,
199                                                 slot_firstpfn, slot_lastpfn);
200                 free_bootmem_node(NODE_DATA(node), slot_firstpfn << PAGE_SHIFT,
201                                 (slot_lastpfn - slot_firstpfn) << PAGE_SHIFT);
202                 reserve_bootmem_node(NODE_DATA(node), slot_firstpfn << PAGE_SHIFT,
203                   ((slot_freepfn - slot_firstpfn) << PAGE_SHIFT) + bootmap_size);
204         }
205 }
206
207 unsigned long __init prom_free_prom_memory(void)
208 {
209         /* We got nothing to free here ...  */
210         return 0;
211 }
212
213 extern void pagetable_init(void);
214 extern unsigned long setup_zero_pages(void);
215
216 void __init paging_init(void)
217 {
218         unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
219         unsigned node;
220
221         pagetable_init();
222
223         for (node = 0; node < numnodes; node++) {
224                 pfn_t start_pfn = slot_getbasepfn(node, 0);
225                 pfn_t end_pfn = node_getmaxclick(node) + 1;
226
227                 zones_size[ZONE_DMA] = end_pfn - start_pfn;
228                 free_area_init_node(node, NODE_DATA(node), NULL,
229                                 zones_size, start_pfn, NULL);
230
231                 if (end_pfn > max_low_pfn)
232                         max_low_pfn = end_pfn;
233         }
234 }
235
236 void __init mem_init(void)
237 {
238         unsigned long codesize, datasize, initsize, tmp;
239         unsigned node;
240
241         high_memory = (void *) __va(num_physpages << PAGE_SHIFT);
242
243         for (node = 0; node < numnodes; node++) {
244                 unsigned slot, numslots;
245                 struct page *end, *p;
246         
247                 /*
248                  * This will free up the bootmem, ie, slot 0 memory.
249                  */
250                 totalram_pages += free_all_bootmem_node(NODE_DATA(node));
251
252                 /*
253                  * We need to manually do the other slots.
254                  */
255                 numslots = node_getlastslot(node);
256                 for (slot = 1; slot <= numslots; slot++) {
257                         p = NODE_DATA(node)->node_mem_map +
258                                 (slot_getbasepfn(node, slot) -
259                                  slot_getbasepfn(node, 0));
260
261                         /*
262                          * Free valid memory in current slot.
263                          */
264                         for (end = p + slot_getsize(node, slot); p < end; p++) {
265                                 /* if (!page_is_ram(pgnr)) continue; */
266                                 /* commented out until page_is_ram works */
267                                 ClearPageReserved(p);
268                                 set_page_count(p, 1);
269                                 __free_page(p);
270                                 totalram_pages++;
271                         }
272                 }
273         }
274
275         totalram_pages -= setup_zero_pages();   /* This comes from node 0 */
276
277         codesize =  (unsigned long) &_etext - (unsigned long) &_text;
278         datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
279         initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
280
281         tmp = nr_free_pages();
282         printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
283                "%ldk reserved, %ldk data, %ldk init, %ldk highmem)\n",
284                tmp << (PAGE_SHIFT-10),
285                num_physpages << (PAGE_SHIFT-10),
286                codesize >> 10,
287                (num_physpages - tmp) << (PAGE_SHIFT-10),
288                datasize >> 10,
289                initsize >> 10,
290                (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
291 }