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/config.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/bootmem.h>
14 #include <linux/swap.h>
15 #ifdef CONFIG_BLK_DEV_INITRD
16 #include <linux/blk.h>
17 #endif
18 
19 #include <asm/hwrpb.h>
20 #include <asm/pgalloc.h>
21 
22 plat_pg_data_t *plat_node_data[MAX_NUMNODES];
23 bootmem_data_t plat_node_bdata[MAX_NUMNODES];
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 PFN_UP(x)       (((x) + PAGE_SIZE-1) >> PAGE_SHIFT)
33 #define PFN_DOWN(x)     ((x) >> PAGE_SHIFT)
34 #define PFN_PHYS(x)     ((x) << PAGE_SHIFT)
35 #define for_each_mem_cluster(memdesc, cluster, i)		\
36 	for ((cluster) = (memdesc)->cluster, (i) = 0;		\
37 	     (i) < (memdesc)->numclusters; (i)++, (cluster)++)
38 
show_mem_layout(void)39 static void __init show_mem_layout(void)
40 {
41 	struct memclust_struct * cluster;
42 	struct memdesc_struct * memdesc;
43 	int i;
44 
45 	/* Find free clusters, and init and free the bootmem accordingly.  */
46 	memdesc = (struct memdesc_struct *)
47 	  (hwrpb->mddt_offset + (unsigned long) hwrpb);
48 
49 	printk("Raw memory layout:\n");
50 	for_each_mem_cluster(memdesc, cluster, i) {
51 		printk(" memcluster %2d, usage %1lx, start %8lu, end %8lu\n",
52 		       i, cluster->usage, cluster->start_pfn,
53 		       cluster->start_pfn + cluster->numpages);
54 	}
55 }
56 
57 static void __init
setup_memory_node(int nid,void * kernel_end)58 setup_memory_node(int nid, void *kernel_end)
59 {
60 	extern unsigned long mem_size_limit;
61 	struct memclust_struct * cluster;
62 	struct memdesc_struct * memdesc;
63 	unsigned long start_kernel_pfn, end_kernel_pfn;
64 	unsigned long bootmap_size, bootmap_pages, bootmap_start;
65 	unsigned long start, end;
66 	unsigned long node_pfn_start, node_pfn_end;
67 	unsigned long node_min_pfn, node_max_pfn;
68 	int i;
69 	unsigned long node_datasz = PFN_UP(sizeof(plat_pg_data_t));
70 	int show_init = 0;
71 
72 	/* Find the bounds of current node */
73 	node_pfn_start = (NODE_MEM_START(nid)) >> PAGE_SHIFT;
74 	node_pfn_end = node_pfn_start + (NODE_MEM_SIZE(nid) >> PAGE_SHIFT);
75 
76 	/* Find free clusters, and init and free the bootmem accordingly.  */
77 	memdesc = (struct memdesc_struct *)
78 	  (hwrpb->mddt_offset + (unsigned long) hwrpb);
79 
80 	/* find the bounds of this node (node_min_pfn/node_max_pfn) */
81 	node_min_pfn = ~0UL;
82 	node_max_pfn = 0UL;
83 	for_each_mem_cluster(memdesc, cluster, i) {
84 		/* Bit 0 is console/PALcode reserved.  Bit 1 is
85 		   non-volatile memory -- we might want to mark
86 		   this for later.  */
87 		if (cluster->usage & 3)
88 			continue;
89 
90 		start = cluster->start_pfn;
91 		end = start + cluster->numpages;
92 
93 		if (start >= node_pfn_end || end <= node_pfn_start)
94 			continue;
95 
96 		if (!show_init) {
97 			show_init = 1;
98 			printk("Initialing bootmem allocator on Node ID %d\n", nid);
99 		}
100 		printk(" memcluster %2d, usage %1lx, start %8lu, end %8lu\n",
101 		       i, cluster->usage, cluster->start_pfn,
102 		       cluster->start_pfn + cluster->numpages);
103 
104 		if (start < node_pfn_start)
105 			start = node_pfn_start;
106 		if (end > node_pfn_end)
107 			end = node_pfn_end;
108 
109 		if (start < node_min_pfn)
110 			node_min_pfn = start;
111 		if (end > node_max_pfn)
112 			node_max_pfn = end;
113 	}
114 
115 	if (mem_size_limit && node_max_pfn > mem_size_limit) {
116 		static int msg_shown = 0;
117 		if (!msg_shown) {
118 			msg_shown = 1;
119 			printk("setup: forcing memory size to %ldK (from %ldK).\n",
120 			       mem_size_limit << (PAGE_SHIFT - 10),
121 			       node_max_pfn   << (PAGE_SHIFT - 10));
122 		}
123 		node_max_pfn = mem_size_limit;
124 	}
125 
126 	if (node_min_pfn >= node_max_pfn)
127 		return;
128 
129 	/* Update global {min,max}_low_pfn from node information. */
130 	if (node_min_pfn < min_low_pfn)
131 		min_low_pfn = node_min_pfn;
132 	if (node_max_pfn > max_low_pfn)
133 		max_low_pfn = node_max_pfn;
134 
135 	num_physpages += node_max_pfn - node_min_pfn;
136 
137 	/* Cute trick to make sure our local node data is on local memory */
138 	PLAT_NODE_DATA(nid) = (plat_pg_data_t *)(__va(node_min_pfn << PAGE_SHIFT));
139 	/* Quasi-mark the plat_pg_data_t as in-use */
140 	node_min_pfn += node_datasz;
141 	if (node_min_pfn >= node_max_pfn) {
142 		printk(" not enough mem to reserve PLAT_NODE_DATA");
143 		return;
144 	}
145 	NODE_DATA(nid)->bdata = &plat_node_bdata[nid];
146 
147 	printk(" Detected node memory:   start %8lu, end %8lu\n",
148 	       node_min_pfn, node_max_pfn);
149 
150 	DBGDCONT(" DISCONTIG: plat_node_data[%d]   is at 0x%p\n", nid, PLAT_NODE_DATA(nid));
151 	DBGDCONT(" DISCONTIG: NODE_DATA(%d)->bdata is at 0x%p\n", nid, NODE_DATA(nid)->bdata);
152 
153 	/* Find the bounds of kernel memory.  */
154 	start_kernel_pfn = PFN_DOWN(KERNEL_START_PHYS);
155 	end_kernel_pfn = PFN_UP(virt_to_phys(kernel_end));
156 	bootmap_start = -1;
157 
158 	if (!nid && (node_max_pfn < end_kernel_pfn || node_min_pfn > start_kernel_pfn))
159 		panic("kernel loaded out of ram");
160 
161 	/* Zone start phys-addr must be 2^(MAX_ORDER-1) aligned */
162 	node_min_pfn = (node_min_pfn + ((1UL << (MAX_ORDER-1))-1)) & ~((1UL << (MAX_ORDER-1))-1);
163 
164 	/* We need to know how many physically contiguous pages
165 	   we'll need for the bootmap.  */
166 	bootmap_pages = bootmem_bootmap_pages(node_max_pfn-node_min_pfn);
167 
168 	/* Now find a good region where to allocate the bootmap.  */
169 	for_each_mem_cluster(memdesc, cluster, i) {
170 		if (cluster->usage & 3)
171 			continue;
172 
173 		start = cluster->start_pfn;
174 		end = start + cluster->numpages;
175 
176 		if (start >= node_max_pfn || end <= node_min_pfn)
177 			continue;
178 
179 		if (end > node_max_pfn)
180 			end = node_max_pfn;
181 		if (start < node_min_pfn)
182 			start = node_min_pfn;
183 
184 		if (start < start_kernel_pfn) {
185 			if (end > end_kernel_pfn
186 			    && end - end_kernel_pfn >= bootmap_pages) {
187 				bootmap_start = end_kernel_pfn;
188 				break;
189 			} else if (end > start_kernel_pfn)
190 				end = start_kernel_pfn;
191 		} else if (start < end_kernel_pfn)
192 			start = end_kernel_pfn;
193 		if (end - start >= bootmap_pages) {
194 			bootmap_start = start;
195 			break;
196 		}
197 	}
198 
199 	if (bootmap_start == -1)
200 		panic("couldn't find a contigous place for the bootmap");
201 
202 	/* Allocate the bootmap and mark the whole MM as reserved.  */
203 	bootmap_size = init_bootmem_node(NODE_DATA(nid), bootmap_start,
204 					 node_min_pfn, node_max_pfn);
205 	DBGDCONT(" bootmap_start %lu, bootmap_size %lu, bootmap_pages %lu\n",
206 		 bootmap_start, bootmap_size, bootmap_pages);
207 
208 	/* Mark the free regions.  */
209 	for_each_mem_cluster(memdesc, cluster, i) {
210 		if (cluster->usage & 3)
211 			continue;
212 
213 		start = cluster->start_pfn;
214 		end = cluster->start_pfn + cluster->numpages;
215 
216 		if (start >= node_max_pfn || end <= node_min_pfn)
217 			continue;
218 
219 		if (end > node_max_pfn)
220 			end = node_max_pfn;
221 		if (start < node_min_pfn)
222 			start = node_min_pfn;
223 
224 		if (start < start_kernel_pfn) {
225 			if (end > end_kernel_pfn) {
226 				free_bootmem_node(NODE_DATA(nid), PFN_PHYS(start),
227 					     (PFN_PHYS(start_kernel_pfn)
228 					      - PFN_PHYS(start)));
229 				printk(" freeing pages %ld:%ld\n",
230 				       start, start_kernel_pfn);
231 				start = end_kernel_pfn;
232 			} else if (end > start_kernel_pfn)
233 				end = start_kernel_pfn;
234 		} else if (start < end_kernel_pfn)
235 			start = end_kernel_pfn;
236 		if (start >= end)
237 			continue;
238 
239 		free_bootmem_node(NODE_DATA(nid), PFN_PHYS(start), PFN_PHYS(end) - PFN_PHYS(start));
240 		printk(" freeing pages %ld:%ld\n", start, end);
241 	}
242 
243 	/* Reserve the bootmap memory.  */
244 	reserve_bootmem_node(NODE_DATA(nid), PFN_PHYS(bootmap_start), bootmap_size);
245 	printk(" reserving pages %ld:%ld\n", bootmap_start, bootmap_start+PFN_UP(bootmap_size));
246 
247 	numnodes++;
248 }
249 
250 void __init
setup_memory(void * kernel_end)251 setup_memory(void *kernel_end)
252 {
253 	int nid;
254 
255 	show_mem_layout();
256 
257 	numnodes = 0;
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 			reserve_bootmem_node(NODE_DATA(KVADDR_TO_NID(initrd_start)),
281 					     virt_to_phys((void *)initrd_start),
282 					     INITRD_SIZE);
283 		}
284 	}
285 #endif /* CONFIG_BLK_DEV_INITRD */
286 }
287 
paging_init(void)288 void __init paging_init(void)
289 {
290 	unsigned int    nid;
291 	unsigned long   zones_size[MAX_NR_ZONES] = {0, };
292 	unsigned long	dma_local_pfn;
293 
294 	/*
295 	 * The old global MAX_DMA_ADDRESS per-arch API doesn't fit
296 	 * in the NUMA model, for now we convert it to a pfn and
297 	 * we interpret this pfn as a local per-node information.
298 	 * This issue isn't very important since none of these machines
299 	 * have legacy ISA slots anyways.
300 	 */
301 	dma_local_pfn = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
302 
303 	for (nid = 0; nid < numnodes; nid++) {
304 		unsigned long start_pfn = plat_node_bdata[nid].node_boot_start >> PAGE_SHIFT;
305 		unsigned long end_pfn = plat_node_bdata[nid].node_low_pfn;
306 		unsigned long lmax_mapnr;
307 
308 		if (dma_local_pfn >= end_pfn - start_pfn)
309 			zones_size[ZONE_DMA] = end_pfn - start_pfn;
310 		else {
311 			zones_size[ZONE_DMA] = dma_local_pfn;
312 			zones_size[ZONE_NORMAL] = (end_pfn - start_pfn) - dma_local_pfn;
313 		}
314 		free_area_init_node(nid, NODE_DATA(nid), NULL, zones_size, start_pfn<<PAGE_SHIFT, NULL);
315 		lmax_mapnr = PLAT_NODE_DATA_STARTNR(nid) + PLAT_NODE_DATA_SIZE(nid);
316 		if (lmax_mapnr > max_mapnr) {
317 			max_mapnr = lmax_mapnr;
318 			DBGDCONT("Grow max_mapnr to %ld\n", max_mapnr);
319 		}
320 	}
321 
322 	/* Initialize the kernel's ZERO_PGE. */
323 	memset((void *)ZERO_PGE, 0, PAGE_SIZE);
324 }
325 
326 #define printkdot()					\
327 do {							\
328 	if (!(i++ % ((100UL*1024*1024)>>PAGE_SHIFT)))	\
329 		printk(".");				\
330 } while(0)
331 
332 #define clobber(p, size) memset(page_address(p), 0xaa, (size))
333 
mem_stress(void)334 void __init mem_stress(void)
335 {
336 	LIST_HEAD(x);
337 	LIST_HEAD(xx);
338 	struct page * p;
339 	unsigned long i = 0;
340 
341 	printk("starting memstress");
342 	while ((p = alloc_pages(GFP_ATOMIC, 1))) {
343 		clobber(p, PAGE_SIZE*2);
344 		list_add(&p->list, &x);
345 		printkdot();
346 	}
347 	while ((p = alloc_page(GFP_ATOMIC))) {
348 		clobber(p, PAGE_SIZE);
349 		list_add(&p->list, &xx);
350 		printkdot();
351 	}
352 	while (!list_empty(&x)) {
353 		p = list_entry(x.next, struct page, list);
354 		clobber(p, PAGE_SIZE*2);
355 		list_del(x.next);
356 		__free_pages(p, 1);
357 		printkdot();
358 	}
359 	while (!list_empty(&xx)) {
360 		p = list_entry(xx.next, struct page, list);
361 		clobber(p, PAGE_SIZE);
362 		list_del(xx.next);
363 		__free_pages(p, 0);
364 		printkdot();
365 	}
366 	printk("I'm still alive duh!\n");
367 }
368 
369 #undef printkdot
370 #undef clobber
371 
mem_init(void)372 void __init mem_init(void)
373 {
374 	unsigned long codesize, reservedpages, datasize, initsize, pfn;
375 	extern int page_is_ram(unsigned long) __init;
376 	extern char _text, _etext, _data, _edata;
377 	extern char __init_begin, __init_end;
378 	extern unsigned long totalram_pages;
379 	unsigned long nid, i;
380 	mem_map_t * lmem_map;
381 
382 	high_memory = (void *) __va(max_mapnr <<PAGE_SHIFT);
383 
384 	reservedpages = 0;
385 	for (nid = 0; nid < numnodes; nid++) {
386 		/*
387 		 * This will free up the bootmem, ie, slot 0 memory
388 		 */
389 		totalram_pages += free_all_bootmem_node(NODE_DATA(nid));
390 
391 		lmem_map = NODE_MEM_MAP(nid);
392 		pfn = NODE_DATA(nid)->node_start_paddr >> PAGE_SHIFT;
393 		for (i = 0; i < PLAT_NODE_DATA_SIZE(nid); i++, pfn++)
394 			if (page_is_ram(pfn) && PageReserved(lmem_map+i))
395 				reservedpages++;
396 	}
397 
398 	codesize =  (unsigned long) &_etext - (unsigned long) &_text;
399 	datasize =  (unsigned long) &_edata - (unsigned long) &_data;
400 	initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
401 
402 	printk("Memory: %luk/%luk available (%luk kernel code, %luk reserved, "
403 	       "%luk data, %luk init)\n",
404 	       (unsigned long)nr_free_pages() << (PAGE_SHIFT-10),
405 	       num_physpages << (PAGE_SHIFT-10),
406 	       codesize >> 10,
407 	       reservedpages << (PAGE_SHIFT-10),
408 	       datasize >> 10,
409 	       initsize >> 10);
410 #if 0
411 	mem_stress();
412 #endif
413 }
414 
415 void
show_mem(void)416 show_mem(void)
417 {
418 	long i,free = 0,total = 0,reserved = 0;
419 	long shared = 0, cached = 0;
420 	int nid;
421 
422 	printk("\nMem-info:\n");
423 	show_free_areas();
424 	printk("Free swap:       %6dkB\n",nr_swap_pages<<(PAGE_SHIFT-10));
425 	for (nid = 0; nid < numnodes; nid++) {
426 		mem_map_t * lmem_map = NODE_MEM_MAP(nid);
427 		i = PLAT_NODE_DATA_SIZE(nid);
428 		while (i-- > 0) {
429 			total++;
430 			if (PageReserved(lmem_map+i))
431 				reserved++;
432 			else if (PageSwapCache(lmem_map+i))
433 				cached++;
434 			else if (!page_count(lmem_map+i))
435 				free++;
436 			else
437 				shared += atomic_read(&lmem_map[i].count) - 1;
438 		}
439 	}
440 	printk("%ld pages of RAM\n",total);
441 	printk("%ld free pages\n",free);
442 	printk("%ld reserved pages\n",reserved);
443 	printk("%ld pages shared\n",shared);
444 	printk("%ld pages swap cached\n",cached);
445 	printk("%ld pages in page table cache\n",pgtable_cache_size);
446 	show_buffers();
447 }
448