summaryrefslogtreecommitdiffstats
path: root/arch/mips64/sgi-ip27/ip27-memory.c
blob: f0db21bc0efd85b582af25b7d0237ed5d5a9bdfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* $Id: ip27-memory.c,v 1.2 2000/01/27 01:05:24 ralf Exp $
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 2000 by Ralf Baechle
 * Copyright (C) 2000 by Silicon Graphics, Inc.
 *
 * On SGI IP27 the ARC memory configuration data is completly bogus but
 * alternate easier to use mechanisms are available.
 */
#include <linux/init.h>
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/bootmem.h>
#include <linux/swap.h>

#include <asm/page.h>
#include <asm/bootinfo.h>
#include <asm/addrspace.h>
#include <asm/pgtable.h>
#include <asm/sn/types.h>
#include <asm/sn/addrs.h>
#include <asm/sn/klconfig.h>
#include <asm/sn/arch.h>
#include <asm/mmzone.h>

extern char _end;

#define PFN_UP(x)	(((x) + PAGE_SIZE-1) >> PAGE_SHIFT)
#define SLOT_IGNORED	0xffff

short slot_lastfilled_cache[MAX_COMPACT_NODES];
unsigned short slot_psize_cache[MAX_COMPACT_NODES][MAX_MEM_SLOTS];
static pfn_t numpages;
static pfn_t pagenr = 0;

plat_pg_data_t *plat_node_data[MAX_COMPACT_NODES];
bootmem_data_t plat_node_bdata[MAX_COMPACT_NODES];

int numa_debug(void)
{
	printk("NUMA debug\n");
	*(int *)0 = 0;
	return(0);
}

/*
 * Return pfn of first free page of memory on a node. PROM may allocate
 * data structures on the first couple of pages of the first slot of each 
 * node. If this is the case, getfirstfree(node) > getslotstart(node, 0).
 */
pfn_t node_getfirstfree(cnodeid_t cnode)
{
	unsigned long loadbase = CKSEG0;
	nasid_t nasid = COMPACT_TO_NASID_NODEID(cnode);

#ifdef CONFIG_MAPPED_KERNEL
	loadbase = CKSSEG;
#endif
	if (cnode == 0)
		return (KDM_TO_PHYS(PAGE_ALIGN((unsigned long)(&_end)) - 
					(loadbase - K0BASE)) >> PAGE_SHIFT);
	return (KDM_TO_PHYS(PAGE_ALIGN(SYMMON_STK_ADDR(nasid, 0))) >> PAGE_SHIFT);
}

/*
 * Return the number of pages of memory provided by the given slot
 * on the specified node.
 */
pfn_t slot_getsize(cnodeid_t node, int slot)
{
	return (pfn_t) slot_psize_cache[node][slot];
}

/*
 * Return highest slot filled
 */
int node_getlastslot(cnodeid_t node)
{
	return (int) slot_lastfilled_cache[node];
}

/*
 * Return the pfn of the last free page of memory on a node.
 */
pfn_t node_getmaxclick(cnodeid_t node)
{
	pfn_t	slot_psize;
	int	slot;

	/*
	 * Start at the top slot. When we find a slot with memory in it,
	 * that's the winner.
	 */
	for (slot = (node_getnumslots(node) - 1); slot >= 0; slot--) {
		if ((slot_psize = slot_getsize(node, slot))) {
			if (slot_psize == SLOT_IGNORED)
				continue;
			/* Return the basepfn + the slot size, minus 1. */
			return slot_getbasepfn(node, slot) + slot_psize - 1;
		}
	}

	/*
	 * If there's no memory on the node, return 0. This is likely
	 * to cause problems.
	 */
	return (pfn_t)0;
}

static pfn_t slot_psize_compute(cnodeid_t node, int slot)
{
	nasid_t nasid;
	lboard_t *brd;
	klmembnk_t *banks;
	unsigned long size;

	nasid = COMPACT_TO_NASID_NODEID(node);
	/* Find the node board */
	brd = find_lboard((lboard_t *)KL_CONFIG_INFO(nasid), KLTYPE_IP27);
	if (!brd)
		return 0;

	/* Get the memory bank structure */
	banks = (klmembnk_t *)find_first_component(brd, KLSTRUCT_MEMBNK);
	if (!banks)
		return 0;

	/* Size in _Megabytes_ */
	size = (unsigned long)banks->membnk_bnksz[slot/4];

	/* hack for 128 dimm banks */
	if (size <= 128) {
		if (slot%4 == 0) {
			size <<= 20;		/* size in bytes */
			return(size >> PAGE_SHIFT);
		} else {
			return 0;
		}
	} else {
		size /= 4;
		size <<= 20;
		return(size >> PAGE_SHIFT);
	}
}

pfn_t szmem(pfn_t fpage, pfn_t maxpmem)
{
	cnodeid_t node;
	int slot, numslots;
	pfn_t num_pages = 0, slot_psize;
	pfn_t slot0sz = 0, nodebytes;	/* Hack to detect problem configs */
	int ignore;

	for (node = 0; node < numnodes; node++) {
		numslots = node_getnumslots(node);
		ignore = nodebytes = 0;
		for (slot = 0; slot < numslots; slot++) {
			slot_psize = slot_psize_compute(node, slot);
			if (slot == 0) slot0sz = slot_psize;
			/* 
			 * We need to refine the hack when we have replicated
			 * kernel text.
			 */
			nodebytes += SLOT_SIZE;
			if ((nodebytes >> PAGE_SHIFT) * (sizeof(struct page)) >
						(slot0sz << PAGE_SHIFT))
				ignore = 1;
			if (ignore && slot_psize) {
				printk("Ignoring slot %d onwards on node %d\n",
								slot, node);
				slot_psize_cache[node][slot] = SLOT_IGNORED;
				slot = numslots;
				continue;
			}
			num_pages += slot_psize;
			slot_psize_cache[node][slot] = 
					(unsigned short) slot_psize;
			if (slot_psize)
				slot_lastfilled_cache[node] = slot;
		}
	}
	if (maxpmem)
		return((maxpmem > num_pages) ? num_pages : maxpmem);
	else
		return num_pages;
}

/*
 * HACK ALERT - Things do not work if this is not here. Maybe this is
 * acting as a pseudocacheflush operation. The write pattern seems to
 * be important, writing a 0 does not help.
 */
void setup_test(cnodeid_t node, pfn_t start, pfn_t end)
{
	unsigned long *ptr = __va(start << PAGE_SHIFT);
	unsigned long size = 4 * 1024 * 1024;	/* 4M L2 caches */

	while (size) {
		size -= sizeof(unsigned long);
		*ptr = (0xdeadbeefbabeb000UL|node);
		/* *ptr = 0; */
		ptr++;
	}
}

/*
 * Currently, the intranode memory hole support assumes that each slot
 * contains at least 32 MBytes of memory. We assume all bootmem data
 * fits on the first slot.
 */
void __init prom_meminit(void)
{
	extern void mlreset(void);
	cnodeid_t node;
	pfn_t slot_firstpfn, slot_lastpfn, slot_freepfn;
	unsigned long bootmap_size;
	int node_datasz;

	node_datasz = PFN_UP(sizeof(plat_pg_data_t));
	mlreset();
	numpages = szmem(0, 0);
	for (node = (numnodes - 1); node >= 0; node--) {
		slot_firstpfn = slot_getbasepfn(node, 0);
		slot_lastpfn = slot_firstpfn + slot_getsize(node, 0);
		slot_freepfn = node_getfirstfree(node);
		/* Foll line hack for non discontigmem; remove once discontigmem
		 * becomes the default. */
		max_low_pfn = (slot_lastpfn - slot_firstpfn);
		if (node != 0) 
			setup_test(node, slot_freepfn, slot_lastpfn);
		/*
		 * Allocate the node data structure on the node first.
		 */
		plat_node_data[node] = (plat_pg_data_t *)(__va(slot_freepfn \
							<< PAGE_SHIFT));
		NODE_DATA(node)->bdata = plat_node_bdata + node;
		slot_freepfn += node_datasz;
	  	bootmap_size = init_bootmem_node(node, slot_freepfn, 
						slot_firstpfn, slot_lastpfn);
		free_bootmem_node(node, slot_firstpfn << PAGE_SHIFT, 
				(slot_lastpfn - slot_firstpfn) << PAGE_SHIFT);
		reserve_bootmem_node(node, slot_firstpfn << PAGE_SHIFT,
		  ((slot_freepfn - slot_firstpfn) << PAGE_SHIFT) + bootmap_size);
	}
	printk("Total memory probed : 0x%lx pages\n", numpages);
}

int __init page_is_ram(unsigned long pagenr)
{
        return 1;
}

void __init
prom_free_prom_memory (void)
{
	/* We got nothing to free here ...  */
}

#ifdef CONFIG_DISCONTIGMEM

void __init paging_init(void)
{
	cnodeid_t node;
	unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};

	/* Initialize the entire pgd.  */
	pgd_init((unsigned long)swapper_pg_dir);
	pmd_init((unsigned long)invalid_pmd_table);
	memset((void *)invalid_pte_table, 0, sizeof(pte_t) * 2 * PTRS_PER_PTE);

	for (node = 0; node < numnodes; node++) {
		pfn_t start_pfn = slot_getbasepfn(node, 0);
		pfn_t end_pfn = node_getmaxclick(node);

		zones_size[ZONE_DMA] = end_pfn + 1 - start_pfn;
		free_area_init_node(node, NODE_DATA(node), zones_size, 
						start_pfn << PAGE_SHIFT, 0);
		if ((PLAT_NODE_DATA_STARTNR(node) + 
					PLAT_NODE_DATA_SIZE(node)) > pagenr)
			pagenr = PLAT_NODE_DATA_STARTNR(node) +
					PLAT_NODE_DATA_SIZE(node);
	}
}

void __init mem_init(void)
{
	extern char _stext, _etext, _fdata, _edata;
	extern char __init_begin, __init_end;
	extern unsigned long totalram_pages;
	extern unsigned long setup_zero_pages(void);
	cnodeid_t nid;
	unsigned long tmp, ram;
	unsigned long codesize, reservedpages, datasize, initsize;
	int slot, numslots;
	struct page *pg, *pslot;
	pfn_t pgnr;

	num_physpages = numpages;	/* memory already sized by szmem */
	max_mapnr = pagenr;		/* already found during paging_init */
	high_memory = (void *) __va(max_mapnr << PAGE_SHIFT);

	for (nid = 0; nid < numnodes; nid++) {

		/*
		 * Hack till free_area_init_core() zeroes free_pages
		 */
		for (tmp = 0; tmp < MAX_NR_ZONES; tmp++)
			PLAT_NODE_DATA(nid)->gendata.node_zones[tmp].free_pages=0;
		/*
	 	 * This will free up the bootmem, ie, slot 0 memory.
	 	 */
		totalram_pages += free_all_bootmem_node(nid);

		/*
		 * We need to manually do the other slots.
		 */
		pg = NODE_DATA(nid)->node_mem_map + slot_getsize(nid, 0);
		pgnr = PLAT_NODE_DATA_STARTNR(nid) + slot_getsize(nid, 0);
		numslots = node_getlastslot(nid);
		for (slot = 1; slot <= numslots; slot++) {
			pslot = NODE_DATA(nid)->node_mem_map + 
			   slot_getbasepfn(nid, slot) - slot_getbasepfn(nid, 0);

			/*
			 * Mark holes in previous slot. May also want to
			 * free up the pages that hold the memmap entries.
			 */
			while (pg < pslot) {
				pg->flags |= (1<<PG_skip);
				pg++; pgnr++;
			}

			/*
			 * Free valid memory in current slot.
			 */
			pslot += slot_getsize(nid, slot);
			while (pg < pslot) {
				if (!page_is_ram(pgnr))
					continue;
				ClearPageReserved(pg);
				atomic_set(&pg->count, 1);
				__free_page(pg);
				totalram_pages++;
				pg++; pgnr++;
			}
		}
	}

	totalram_pages -= setup_zero_pages();	/* This comes from node 0 */

	reservedpages = ram = 0;
	for (nid = 0; nid < numnodes; nid++) {
		for (tmp = PLAT_NODE_DATA_STARTNR(nid); tmp < 
			(PLAT_NODE_DATA_STARTNR(nid) +
			PLAT_NODE_DATA_SIZE(nid)); tmp++) {
			/* Ignore holes */
			if (PageSkip(mem_map+tmp))
				continue;
			if (page_is_ram(tmp)) {
				ram++;
				if (PageReserved(mem_map+tmp))
					reservedpages++;
			}
		}
	}

	codesize =  (unsigned long) &_etext - (unsigned long) &_stext;
	datasize =  (unsigned long) &_edata - (unsigned long) &_fdata;
	initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;

	printk("Memory: %luk/%luk available (%ldk kernel code, %ldk reserved, "
		"%ldk data, %ldk init)\n",
		(unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
		ram << (PAGE_SHIFT-10),
		codesize >> 10,
		reservedpages << (PAGE_SHIFT-10),
		datasize >> 10,
		initsize >> 10);
}

#endif /* CONFIG_DISCONTIGMEM */