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  *
9  * On SGI IP27 the ARC memory configuration data is completly bogus but
10  * alternate easier to use mechanisms are available.
11  */
12 #include <linux/init.h>
13 #include <linux/config.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/bootmem.h>
17 #include <linux/swap.h>
18 #include <linux/module.h>
19 
20 #include <asm/page.h>
21 #include <asm/bootinfo.h>
22 #include <asm/addrspace.h>
23 #include <asm/pgtable.h>
24 #include <asm/pgalloc.h>
25 #include <asm/sn/types.h>
26 #include <asm/sn/addrs.h>
27 #include <asm/sn/klconfig.h>
28 #include <asm/sn/arch.h>
29 #include <asm/mmzone.h>
30 
31 /* ip27-klnuma.c   */
32 extern pfn_t node_getfirstfree(cnodeid_t cnode);
33 
34 #define PFN_UP(x)	(((x) + PAGE_SIZE-1) >> PAGE_SHIFT)
35 #define SLOT_IGNORED	0xffff
36 
37 short slot_lastfilled_cache[MAX_COMPACT_NODES];
38 unsigned short slot_psize_cache[MAX_COMPACT_NODES][MAX_MEM_SLOTS];
39 static pfn_t numpages;
40 
41 plat_pg_data_t *plat_node_data[MAX_COMPACT_NODES];
42 bootmem_data_t plat_node_bdata[MAX_COMPACT_NODES];
43 
44 EXPORT_SYMBOL(plat_node_data);
45 
numa_debug(void)46 int numa_debug(void)
47 {
48 	printk("NUMA debug\n");
49 	*(int *)0 = 0;
50 	return(0);
51 }
52 
53 EXPORT_SYMBOL(numa_debug);
54 
55 /*
56  * Return the number of pages of memory provided by the given slot
57  * on the specified node.
58  */
slot_getsize(cnodeid_t node,int slot)59 pfn_t slot_getsize(cnodeid_t node, int slot)
60 {
61 	return (pfn_t) slot_psize_cache[node][slot];
62 }
63 
64 /*
65  * Return highest slot filled
66  */
node_getlastslot(cnodeid_t node)67 int node_getlastslot(cnodeid_t node)
68 {
69 	return (int) slot_lastfilled_cache[node];
70 }
71 
72 /*
73  * Return the pfn of the last free page of memory on a node.
74  */
node_getmaxclick(cnodeid_t node)75 pfn_t node_getmaxclick(cnodeid_t node)
76 {
77 	pfn_t	slot_psize;
78 	int	slot;
79 
80 	/*
81 	 * Start at the top slot. When we find a slot with memory in it,
82 	 * that's the winner.
83 	 */
84 	for (slot = (node_getnumslots(node) - 1); slot >= 0; slot--) {
85 		if ((slot_psize = slot_getsize(node, slot))) {
86 			if (slot_psize == SLOT_IGNORED)
87 				continue;
88 			/* Return the basepfn + the slot size, minus 1. */
89 			return slot_getbasepfn(node, slot) + slot_psize - 1;
90 		}
91 	}
92 
93 	/*
94 	 * If there's no memory on the node, return 0. This is likely
95 	 * to cause problems.
96 	 */
97 	return (pfn_t)0;
98 }
99 
slot_psize_compute(cnodeid_t node,int slot)100 static pfn_t slot_psize_compute(cnodeid_t node, int slot)
101 {
102 	nasid_t nasid;
103 	lboard_t *brd;
104 	klmembnk_t *banks;
105 	unsigned long size;
106 
107 	nasid = COMPACT_TO_NASID_NODEID(node);
108 	/* Find the node board */
109 	brd = find_lboard((lboard_t *)KL_CONFIG_INFO(nasid), KLTYPE_IP27);
110 	if (!brd)
111 		return 0;
112 
113 	/* Get the memory bank structure */
114 	banks = (klmembnk_t *)find_first_component(brd, KLSTRUCT_MEMBNK);
115 	if (!banks)
116 		return 0;
117 
118 	/* Size in _Megabytes_ */
119 	size = (unsigned long)banks->membnk_bnksz[slot/4];
120 
121 	/* hack for 128 dimm banks */
122 	if (size <= 128) {
123 		if (slot%4 == 0) {
124 			size <<= 20;		/* size in bytes */
125 			return(size >> PAGE_SHIFT);
126 		} else {
127 			return 0;
128 		}
129 	} else {
130 		size /= 4;
131 		size <<= 20;
132 		return(size >> PAGE_SHIFT);
133 	}
134 }
135 
szmem(pfn_t fpage,pfn_t maxpmem)136 pfn_t szmem(pfn_t fpage, pfn_t maxpmem)
137 {
138 	cnodeid_t node;
139 	int slot, numslots;
140 	pfn_t num_pages = 0, slot_psize;
141 	pfn_t slot0sz = 0, nodebytes;	/* Hack to detect problem configs */
142 	int ignore;
143 
144 	for (node = 0; node < numnodes; node++) {
145 		numslots = node_getnumslots(node);
146 		ignore = nodebytes = 0;
147 		for (slot = 0; slot < numslots; slot++) {
148 			slot_psize = slot_psize_compute(node, slot);
149 			if (slot == 0) slot0sz = slot_psize;
150 			/*
151 			 * We need to refine the hack when we have replicated
152 			 * kernel text.
153 			 */
154 			nodebytes += SLOT_SIZE;
155 			if ((nodebytes >> PAGE_SHIFT) * (sizeof(struct page)) >
156 						(slot0sz << PAGE_SHIFT))
157 				ignore = 1;
158 			if (ignore && slot_psize) {
159 				printk("Ignoring slot %d onwards on node %d\n",
160 								slot, node);
161 				slot_psize_cache[node][slot] = SLOT_IGNORED;
162 				slot = numslots;
163 				continue;
164 			}
165 			num_pages += slot_psize;
166 			slot_psize_cache[node][slot] =
167 					(unsigned short) slot_psize;
168 			if (slot_psize)
169 				slot_lastfilled_cache[node] = slot;
170 		}
171 	}
172 	if (maxpmem)
173 		return((maxpmem > num_pages) ? num_pages : maxpmem);
174 	else
175 		return num_pages;
176 }
177 
178 /*
179  * Currently, the intranode memory hole support assumes that each slot
180  * contains at least 32 MBytes of memory. We assume all bootmem data
181  * fits on the first slot.
182  */
prom_meminit(void)183 void __init prom_meminit(void)
184 {
185 	extern void mlreset(void);
186 	cnodeid_t node;
187 	pfn_t slot_firstpfn, slot_lastpfn, slot_freepfn;
188 	unsigned long bootmap_size;
189 	int node_datasz;
190 
191 	node_datasz = PFN_UP(sizeof(plat_pg_data_t));
192 	mlreset();
193 	numpages = szmem(0, 0);
194 	for (node = (numnodes - 1); node >= 0; node--) {
195 		slot_firstpfn = slot_getbasepfn(node, 0);
196 		slot_lastpfn = slot_firstpfn + slot_getsize(node, 0);
197 		slot_freepfn = node_getfirstfree(node);
198 		/* Foll line hack for non discontigmem; remove once discontigmem
199 		 * becomes the default. */
200 		max_low_pfn = (slot_lastpfn - slot_firstpfn);
201 
202 		/*
203 		 * Allocate the node data structure on the node first.
204 		 */
205 		plat_node_data[node] = (plat_pg_data_t *)(__va(slot_freepfn \
206 							<< PAGE_SHIFT));
207 		NODE_DATA(node)->bdata = plat_node_bdata + node;
208 		slot_freepfn += node_datasz;
209 	  	bootmap_size = init_bootmem_node(NODE_DATA(node), slot_freepfn,
210 						slot_firstpfn, slot_lastpfn);
211 		free_bootmem_node(NODE_DATA(node), slot_firstpfn << PAGE_SHIFT,
212 				(slot_lastpfn - slot_firstpfn) << PAGE_SHIFT);
213 		reserve_bootmem_node(NODE_DATA(node), slot_firstpfn << PAGE_SHIFT,
214 		  ((slot_freepfn - slot_firstpfn) << PAGE_SHIFT) + bootmap_size);
215 	}
216 	printk("Total memory probed : 0x%lx pages\n", numpages);
217 }
218 
page_is_ram(unsigned long pagenr)219 int __init page_is_ram(unsigned long pagenr)
220 {
221         return 1;
222 }
223 
224 void __init
prom_free_prom_memory(void)225 prom_free_prom_memory (void)
226 {
227 	/* We got nothing to free here ...  */
228 }
229 
230 #ifdef CONFIG_DISCONTIGMEM
231 
232 static pfn_t pagenr = 0;
233 
paging_init(void)234 void __init paging_init(void)
235 {
236 	pmd_t *pmd = kpmdtbl;
237 	pte_t *pte = kptbl;
238 
239 	cnodeid_t node;
240 	unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
241 	int i;
242 
243 	/* Initialize the entire pgd.  */
244 	pgd_init((unsigned long)swapper_pg_dir);
245 	pmd_init((unsigned long)invalid_pmd_table, (unsigned long)invalid_pte_table);
246 	memset((void *)invalid_pte_table, 0, sizeof(pte_t) * PTRS_PER_PTE);
247 
248 	/* This is for vmalloc  */
249 	memset((void *)kptbl, 0, PAGE_SIZE << PGD_ORDER);
250 	memset((void *)kpmdtbl, 0, PAGE_SIZE);
251 	pgd_set(swapper_pg_dir, kpmdtbl);
252 	for (i = 0; i < (1 << PGD_ORDER); pmd++,i++,pte+=PTRS_PER_PTE)
253 		pmd_val(*pmd) = (unsigned long)pte;
254 
255 	for (node = 0; node < numnodes; node++) {
256 		pfn_t start_pfn = slot_getbasepfn(node, 0);
257 		pfn_t end_pfn = node_getmaxclick(node);
258 
259 		zones_size[ZONE_DMA] = end_pfn + 1 - start_pfn;
260 		free_area_init_node(node, NODE_DATA(node), 0, zones_size,
261 						start_pfn << PAGE_SHIFT, 0);
262 		if ((PLAT_NODE_DATA_STARTNR(node) +
263 					PLAT_NODE_DATA_SIZE(node)) > pagenr)
264 			pagenr = PLAT_NODE_DATA_STARTNR(node) +
265 					PLAT_NODE_DATA_SIZE(node);
266 	}
267 }
268 
mem_init(void)269 void __init mem_init(void)
270 {
271 	extern char _stext, _etext, _fdata, _edata;
272 	extern char __init_begin, __init_end;
273 	extern unsigned long totalram_pages;
274 	extern unsigned long setup_zero_pages(void);
275 	cnodeid_t nid;
276 	unsigned long tmp;
277 	unsigned long codesize, datasize, initsize;
278 	int slot, numslots;
279 	struct page *pg, *pslot;
280 	pfn_t pgnr;
281 
282 	num_physpages = numpages;	/* memory already sized by szmem */
283 	max_mapnr = pagenr;		/* already found during paging_init */
284 	high_memory = (void *) __va(max_mapnr << PAGE_SHIFT);
285 
286 	for (nid = 0; nid < numnodes; nid++) {
287 
288 		/*
289 		 * Hack till free_area_init_core() zeroes free_pages
290 		 */
291 		for (tmp = 0; tmp < MAX_NR_ZONES; tmp++)
292 			PLAT_NODE_DATA(nid)->gendata.node_zones[tmp].free_pages=0;
293 		/*
294 	 	 * This will free up the bootmem, ie, slot 0 memory.
295 	 	 */
296 		totalram_pages += free_all_bootmem_node(NODE_DATA(nid));
297 
298 		/*
299 		 * We need to manually do the other slots.
300 		 */
301 		pg = NODE_DATA(nid)->node_mem_map + slot_getsize(nid, 0);
302 		pgnr = PLAT_NODE_DATA_STARTNR(nid) + slot_getsize(nid, 0);
303 		numslots = node_getlastslot(nid);
304 		for (slot = 1; slot <= numslots; slot++) {
305 			pslot = NODE_DATA(nid)->node_mem_map +
306 			   slot_getbasepfn(nid, slot) - slot_getbasepfn(nid, 0);
307 
308 			/*
309 			 * Mark holes in previous slot. May also want to
310 			 * free up the pages that hold the memmap entries.
311 			 */
312 			while (pg < pslot) {
313 				pg++; pgnr++;
314 			}
315 
316 			/*
317 			 * Free valid memory in current slot.
318 			 */
319 			pslot += slot_getsize(nid, slot);
320 			while (pg < pslot) {
321 				if (!page_is_ram(pgnr))
322 					continue;
323 				ClearPageReserved(pg);
324 				atomic_set(&pg->count, 1);
325 				__free_page(pg);
326 				totalram_pages++;
327 				pg++; pgnr++;
328 			}
329 		}
330 	}
331 
332 	totalram_pages -= setup_zero_pages();	/* This comes from node 0 */
333 
334 	codesize =  (unsigned long) &_etext - (unsigned long) &_stext;
335 	datasize =  (unsigned long) &_edata - (unsigned long) &_fdata;
336 	initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
337 
338 	tmp = (unsigned long) nr_free_pages();
339 	printk("Memory: %luk/%luk available (%ldk kernel code, %ldk reserved, "
340 		"%ldk data, %ldk init)\n",
341 		tmp << (PAGE_SHIFT-10),
342 		num_physpages << (PAGE_SHIFT-10),
343 		codesize >> 10,
344 		(num_physpages - tmp) << (PAGE_SHIFT-10),
345 		datasize >> 10,
346 		initsize >> 10);
347 }
348 
349 #endif /* CONFIG_DISCONTIGMEM */
350