1/*
2 *  linux/arch/alpha/mm/numa.c
3 *
4 *  DISCONTIGMEM NUMA alpha support.
5 *
6 *  Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
7 */
8
9#include <linux/types.h>
10#include <linux/kernel.h>
11#include <linux/mm.h>
12#include <linux/bootmem.h>
13#include <linux/swap.h>
14#include <linux/initrd.h>
15#include <linux/pfn.h>
16#include <linux/module.h>
17
18#include <asm/hwrpb.h>
19#include <asm/pgalloc.h>
20#include <asm/sections.h>
21
22pg_data_t node_data[MAX_NUMNODES];
23EXPORT_SYMBOL(node_data);
24
25#undef DEBUG_DISCONTIG
26#ifdef DEBUG_DISCONTIG
27#define DBGDCONT(args...) printk(args)
28#else
29#define DBGDCONT(args...)
30#endif
31
32#define for_each_mem_cluster(memdesc, _cluster, i)		\
33	for ((_cluster) = (memdesc)->cluster, (i) = 0;		\
34	     (i) < (memdesc)->numclusters; (i)++, (_cluster)++)
35
36static void __init show_mem_layout(void)
37{
38	struct memclust_struct * cluster;
39	struct memdesc_struct * memdesc;
40	int i;
41
42	/* Find free clusters, and init and free the bootmem accordingly.  */
43	memdesc = (struct memdesc_struct *)
44	  (hwrpb->mddt_offset + (unsigned long) hwrpb);
45
46	printk("Raw memory layout:\n");
47	for_each_mem_cluster(memdesc, cluster, i) {
48		printk(" memcluster %2d, usage %1lx, start %8lu, end %8lu\n",
49		       i, cluster->usage, cluster->start_pfn,
50		       cluster->start_pfn + cluster->numpages);
51	}
52}
53
54static void __init
55setup_memory_node(int nid, void *kernel_end)
56{
57	extern unsigned long mem_size_limit;
58	struct memclust_struct * cluster;
59	struct memdesc_struct * memdesc;
60	unsigned long start_kernel_pfn, end_kernel_pfn;
61	unsigned long bootmap_size, bootmap_pages, bootmap_start;
62	unsigned long start, end;
63	unsigned long node_pfn_start, node_pfn_end;
64	unsigned long node_min_pfn, node_max_pfn;
65	int i;
66	unsigned long node_datasz = PFN_UP(sizeof(pg_data_t));
67	int show_init = 0;
68
69	/* Find the bounds of current node */
70	node_pfn_start = (node_mem_start(nid)) >> PAGE_SHIFT;
71	node_pfn_end = node_pfn_start + (node_mem_size(nid) >> PAGE_SHIFT);
72
73	/* Find free clusters, and init and free the bootmem accordingly.  */
74	memdesc = (struct memdesc_struct *)
75	  (hwrpb->mddt_offset + (unsigned long) hwrpb);
76
77	/* find the bounds of this node (node_min_pfn/node_max_pfn) */
78	node_min_pfn = ~0UL;
79	node_max_pfn = 0UL;
80	for_each_mem_cluster(memdesc, cluster, i) {
81		/* Bit 0 is console/PALcode reserved.  Bit 1 is
82		   non-volatile memory -- we might want to mark
83		   this for later.  */
84		if (cluster->usage & 3)
85			continue;
86
87		start = cluster->start_pfn;
88		end = start + cluster->numpages;
89
90		if (start >= node_pfn_end || end <= node_pfn_start)
91			continue;
92
93		if (!show_init) {
94			show_init = 1;
95			printk("Initializing bootmem allocator on Node ID %d\n", nid);
96		}
97		printk(" memcluster %2d, usage %1lx, start %8lu, end %8lu\n",
98		       i, cluster->usage, cluster->start_pfn,
99		       cluster->start_pfn + cluster->numpages);
100
101		if (start < node_pfn_start)
102			start = node_pfn_start;
103		if (end > node_pfn_end)
104			end = node_pfn_end;
105
106		if (start < node_min_pfn)
107			node_min_pfn = start;
108		if (end > node_max_pfn)
109			node_max_pfn = end;
110	}
111
112	if (mem_size_limit && node_max_pfn > mem_size_limit) {
113		static int msg_shown = 0;
114		if (!msg_shown) {
115			msg_shown = 1;
116			printk("setup: forcing memory size to %ldK (from %ldK).\n",
117			       mem_size_limit << (PAGE_SHIFT - 10),
118			       node_max_pfn    << (PAGE_SHIFT - 10));
119		}
120		node_max_pfn = mem_size_limit;
121	}
122
123	if (node_min_pfn >= node_max_pfn)
124		return;
125
126	/* Update global {min,max}_low_pfn from node information. */
127	if (node_min_pfn < min_low_pfn)
128		min_low_pfn = node_min_pfn;
129	if (node_max_pfn > max_low_pfn)
130		max_pfn = max_low_pfn = node_max_pfn;
131
132#if 0 /* we'll try this one again in a little while */
133	/* Cute trick to make sure our local node data is on local memory */
134	node_data[nid] = (pg_data_t *)(__va(node_min_pfn << PAGE_SHIFT));
135#endif
136	/* Quasi-mark the pg_data_t as in-use */
137	node_min_pfn += node_datasz;
138	if (node_min_pfn >= node_max_pfn) {
139		printk(" not enough mem to reserve NODE_DATA");
140		return;
141	}
142	NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
143
144	printk(" Detected node memory:   start %8lu, end %8lu\n",
145	       node_min_pfn, node_max_pfn);
146
147	DBGDCONT(" DISCONTIG: node_data[%d]   is at 0x%p\n", nid, NODE_DATA(nid));
148	DBGDCONT(" DISCONTIG: NODE_DATA(%d)->bdata is at 0x%p\n", nid, NODE_DATA(nid)->bdata);
149
150	/* Find the bounds of kernel memory.  */
151	start_kernel_pfn = PFN_DOWN(KERNEL_START_PHYS);
152	end_kernel_pfn = PFN_UP(virt_to_phys(kernel_end));
153	bootmap_start = -1;
154
155	if (!nid && (node_max_pfn < end_kernel_pfn || node_min_pfn > start_kernel_pfn))
156		panic("kernel loaded out of ram");
157
158	/* Zone start phys-addr must be 2^(MAX_ORDER-1) aligned.
159	   Note that we round this down, not up - node memory
160	   has much larger alignment than 8Mb, so it's safe. */
161	node_min_pfn &= ~((1UL << (MAX_ORDER-1))-1);
162
163	/* We need to know how many physically contiguous pages
164	   we'll need for the bootmap.  */
165	bootmap_pages = bootmem_bootmap_pages(node_max_pfn-node_min_pfn);
166
167	/* Now find a good region where to allocate the bootmap.  */
168	for_each_mem_cluster(memdesc, cluster, i) {
169		if (cluster->usage & 3)
170			continue;
171
172		start = cluster->start_pfn;
173		end = start + cluster->numpages;
174
175		if (start >= node_max_pfn || end <= node_min_pfn)
176			continue;
177
178		if (end > node_max_pfn)
179			end = node_max_pfn;
180		if (start < node_min_pfn)
181			start = node_min_pfn;
182
183		if (start < start_kernel_pfn) {
184			if (end > end_kernel_pfn
185			    && end - end_kernel_pfn >= bootmap_pages) {
186				bootmap_start = end_kernel_pfn;
187				break;
188			} else if (end > start_kernel_pfn)
189				end = start_kernel_pfn;
190		} else if (start < end_kernel_pfn)
191			start = end_kernel_pfn;
192		if (end - start >= bootmap_pages) {
193			bootmap_start = start;
194			break;
195		}
196	}
197
198	if (bootmap_start == -1)
199		panic("couldn't find a contiguous place for the bootmap");
200
201	/* Allocate the bootmap and mark the whole MM as reserved.  */
202	bootmap_size = init_bootmem_node(NODE_DATA(nid), bootmap_start,
203					 node_min_pfn, node_max_pfn);
204	DBGDCONT(" bootmap_start %lu, bootmap_size %lu, bootmap_pages %lu\n",
205		 bootmap_start, bootmap_size, bootmap_pages);
206
207	/* Mark the free regions.  */
208	for_each_mem_cluster(memdesc, cluster, i) {
209		if (cluster->usage & 3)
210			continue;
211
212		start = cluster->start_pfn;
213		end = cluster->start_pfn + cluster->numpages;
214
215		if (start >= node_max_pfn || end <= node_min_pfn)
216			continue;
217
218		if (end > node_max_pfn)
219			end = node_max_pfn;
220		if (start < node_min_pfn)
221			start = node_min_pfn;
222
223		if (start < start_kernel_pfn) {
224			if (end > end_kernel_pfn) {
225				free_bootmem_node(NODE_DATA(nid), PFN_PHYS(start),
226					     (PFN_PHYS(start_kernel_pfn)
227					      - PFN_PHYS(start)));
228				printk(" freeing pages %ld:%ld\n",
229				       start, start_kernel_pfn);
230				start = end_kernel_pfn;
231			} else if (end > start_kernel_pfn)
232				end = start_kernel_pfn;
233		} else if (start < end_kernel_pfn)
234			start = end_kernel_pfn;
235		if (start >= end)
236			continue;
237
238		free_bootmem_node(NODE_DATA(nid), PFN_PHYS(start), PFN_PHYS(end) - PFN_PHYS(start));
239		printk(" freeing pages %ld:%ld\n", start, end);
240	}
241
242	/* Reserve the bootmap memory.  */
243	reserve_bootmem_node(NODE_DATA(nid), PFN_PHYS(bootmap_start),
244			bootmap_size, BOOTMEM_DEFAULT);
245	printk(" reserving pages %ld:%ld\n", bootmap_start, bootmap_start+PFN_UP(bootmap_size));
246
247	node_set_online(nid);
248}
249
250void __init
251setup_memory(void *kernel_end)
252{
253	int nid;
254
255	show_mem_layout();
256
257	nodes_clear(node_online_map);
258
259	min_low_pfn = ~0UL;
260	max_low_pfn = 0UL;
261	for (nid = 0; nid < MAX_NUMNODES; nid++)
262		setup_memory_node(nid, kernel_end);
263
264#ifdef CONFIG_BLK_DEV_INITRD
265	initrd_start = INITRD_START;
266	if (initrd_start) {
267		extern void *move_initrd(unsigned long);
268
269		initrd_end = initrd_start+INITRD_SIZE;
270		printk("Initial ramdisk at: 0x%p (%lu bytes)\n",
271		       (void *) initrd_start, INITRD_SIZE);
272
273		if ((void *)initrd_end > phys_to_virt(PFN_PHYS(max_low_pfn))) {
274			if (!move_initrd(PFN_PHYS(max_low_pfn)))
275				printk("initrd extends beyond end of memory "
276				       "(0x%08lx > 0x%p)\ndisabling initrd\n",
277				       initrd_end,
278				       phys_to_virt(PFN_PHYS(max_low_pfn)));
279		} else {
280			nid = kvaddr_to_nid(initrd_start);
281			reserve_bootmem_node(NODE_DATA(nid),
282					     virt_to_phys((void *)initrd_start),
283					     INITRD_SIZE, BOOTMEM_DEFAULT);
284		}
285	}
286#endif /* CONFIG_BLK_DEV_INITRD */
287}
288
289void __init paging_init(void)
290{
291	unsigned int    nid;
292	unsigned long   zones_size[MAX_NR_ZONES] = {0, };
293	unsigned long	dma_local_pfn;
294
295	/*
296	 * The old global MAX_DMA_ADDRESS per-arch API doesn't fit
297	 * in the NUMA model, for now we convert it to a pfn and
298	 * we interpret this pfn as a local per-node information.
299	 * This issue isn't very important since none of these machines
300	 * have legacy ISA slots anyways.
301	 */
302	dma_local_pfn = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
303
304	for_each_online_node(nid) {
305		bootmem_data_t *bdata = &bootmem_node_data[nid];
306		unsigned long start_pfn = bdata->node_min_pfn;
307		unsigned long end_pfn = bdata->node_low_pfn;
308
309		if (dma_local_pfn >= end_pfn - start_pfn)
310			zones_size[ZONE_DMA] = end_pfn - start_pfn;
311		else {
312			zones_size[ZONE_DMA] = dma_local_pfn;
313			zones_size[ZONE_NORMAL] = (end_pfn - start_pfn) - dma_local_pfn;
314		}
315		node_set_state(nid, N_NORMAL_MEMORY);
316		free_area_init_node(nid, zones_size, start_pfn, NULL);
317	}
318
319	/* Initialize the kernel's ZERO_PGE. */
320	memset((void *)ZERO_PGE, 0, PAGE_SIZE);
321}
322