numa_32.c 13.1 KB
Newer Older
L
Linus Torvalds 已提交
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
/*
 * Written by: Patricia Gaughen <gone@us.ibm.com>, IBM Corporation
 * August 2002: added remote node KVA remap - Martin J. Bligh 
 *
 * Copyright (C) 2002, IBM Corp.
 *
 * All rights reserved.          
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 * NON INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/mm.h>
#include <linux/bootmem.h>
#include <linux/mmzone.h>
#include <linux/highmem.h>
#include <linux/initrd.h>
#include <linux/nodemask.h>
31
#include <linux/module.h>
32
#include <linux/kexec.h>
D
Dave Hansen 已提交
33
#include <linux/pfn.h>
34
#include <linux/swap.h>
M
Mel Gorman 已提交
35
#include <linux/acpi.h>
36

L
Linus Torvalds 已提交
37 38 39
#include <asm/e820.h>
#include <asm/setup.h>
#include <asm/mmzone.h>
40
#include <asm/bios_ebda.h>
41
#include <asm/proto.h>
L
Linus Torvalds 已提交
42

43
struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
44
EXPORT_SYMBOL(node_data);
L
Linus Torvalds 已提交
45 46

/*
A
Adrian Bunk 已提交
47
 * numa interface - we expect the numa architecture specific code to have
L
Linus Torvalds 已提交
48 49 50
 *                  populated the following initialisation.
 *
 * 1) node_online_map  - the map of all nodes configured (online) in the system
51
 * 2) node_start_pfn   - the starting page frame number for a node
L
Linus Torvalds 已提交
52 53
 * 3) node_end_pfn     - the ending page fram number for a node
 */
54 55
unsigned long node_start_pfn[MAX_NUMNODES] __read_mostly;
unsigned long node_end_pfn[MAX_NUMNODES] __read_mostly;
56

L
Linus Torvalds 已提交
57

58
#ifdef CONFIG_DISCONTIGMEM
L
Linus Torvalds 已提交
59
/*
60
 * 4) physnode_map     - the mapping between a pfn and owning node
L
Linus Torvalds 已提交
61
 * physnode_map keeps track of the physical memory layout of a generic
62 63
 * numa node on a 64Mb break (each element of the array will
 * represent 64Mb of memory and will be marked by the node id.  so,
L
Linus Torvalds 已提交
64 65 66
 * if the first gig is on node 0, and the second gig is on node 1
 * physnode_map will contain:
 *
67 68 69
 *     physnode_map[0-15] = 0;
 *     physnode_map[16-31] = 1;
 *     physnode_map[32- ] = -1;
L
Linus Torvalds 已提交
70
 */
71
s8 physnode_map[MAX_ELEMENTS] __read_mostly = { [0 ... (MAX_ELEMENTS - 1)] = -1};
72
EXPORT_SYMBOL(physnode_map);
L
Linus Torvalds 已提交
73 74 75 76 77

void memory_present(int nid, unsigned long start, unsigned long end)
{
	unsigned long pfn;

78
	printk(KERN_INFO "Node: %d, start_pfn: %lx, end_pfn: %lx\n",
L
Linus Torvalds 已提交
79 80 81 82 83
			nid, start, end);
	printk(KERN_DEBUG "  Setting physnode_map array to node %d for pfns:\n", nid);
	printk(KERN_DEBUG "  ");
	for (pfn = start; pfn < end; pfn += PAGES_PER_ELEMENT) {
		physnode_map[pfn / PAGES_PER_ELEMENT] = nid;
84
		printk(KERN_CONT "%lx ", pfn);
L
Linus Torvalds 已提交
85
	}
86
	printk(KERN_CONT "\n");
L
Linus Torvalds 已提交
87 88 89 90 91 92 93 94 95 96 97 98
}

unsigned long node_memmap_size_bytes(int nid, unsigned long start_pfn,
					      unsigned long end_pfn)
{
	unsigned long nr_pages = end_pfn - start_pfn;

	if (!nr_pages)
		return 0;

	return (nr_pages + 1) * sizeof(struct page);
}
99
#endif
L
Linus Torvalds 已提交
100 101 102 103 104 105 106

extern unsigned long find_max_low_pfn(void);
extern unsigned long highend_pfn, highstart_pfn;

#define LARGE_PAGE_BYTES (PTRS_PER_PTE * PAGE_SIZE)

unsigned long node_remap_size[MAX_NUMNODES];
A
Adrian Bunk 已提交
107
static void *node_remap_start_vaddr[MAX_NUMNODES];
L
Linus Torvalds 已提交
108 109
void set_pmd_pfn(unsigned long vaddr, unsigned long pfn, pgprot_t flags);

110 111
static unsigned long kva_start_pfn;
static unsigned long kva_pages;
L
Linus Torvalds 已提交
112 113 114 115 116 117 118
/*
 * FLAT - support for basic PC memory model with discontig enabled, essentially
 *        a single node with all available processors in it with a flat
 *        memory map.
 */
int __init get_memcfg_numa_flat(void)
{
119
	printk(KERN_DEBUG "NUMA - single node, flat memory mode\n");
L
Linus Torvalds 已提交
120 121 122

	node_start_pfn[0] = 0;
	node_end_pfn[0] = max_pfn;
123
	e820_register_active_regions(0, 0, max_pfn);
L
Linus Torvalds 已提交
124
	memory_present(0, 0, max_pfn);
125
	node_remap_size[0] = node_memmap_size_bytes(0, 0, max_pfn);
L
Linus Torvalds 已提交
126 127 128 129 130 131 132 133 134 135

        /* Indicate there is one node available. */
	nodes_clear(node_online_map);
	node_set_online(0);
	return 1;
}

/*
 * Find the highest page frame number we have available for the node
 */
136
static void __init propagate_e820_map_node(int nid)
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145
{
	if (node_end_pfn[nid] > max_pfn)
		node_end_pfn[nid] = max_pfn;
	/*
	 * if a user has given mem=XXXX, then we need to make sure 
	 * that the node _starts_ before that, too, not just ends
	 */
	if (node_start_pfn[nid] > max_pfn)
		node_start_pfn[nid] = max_pfn;
E
Eric Sesterhenn 已提交
146
	BUG_ON(node_start_pfn[nid] > node_end_pfn[nid]);
L
Linus Torvalds 已提交
147 148 149 150 151 152 153 154 155 156 157
}

/* 
 * Allocate memory for the pg_data_t for this node via a crude pre-bootmem
 * method.  For node zero take this from the bottom of memory, for
 * subsequent nodes place them at node_remap_start_vaddr which contains
 * node local data in physically node local memory.  See setup_memory()
 * for details.
 */
static void __init allocate_pgdat(int nid)
{
Y
Yinghai Lu 已提交
158 159 160
	char buf[16];

	if (node_has_online_mem(nid) && node_remap_start_vaddr[nid])
L
Linus Torvalds 已提交
161 162
		NODE_DATA(nid) = (pg_data_t *)node_remap_start_vaddr[nid];
	else {
163 164
		unsigned long pgdat_phys;
		pgdat_phys = find_e820_area(min_low_pfn<<PAGE_SHIFT,
Y
Yinghai Lu 已提交
165
				 max_pfn_mapped<<PAGE_SHIFT,
166
				 sizeof(pg_data_t),
167 168
				 PAGE_SIZE);
		NODE_DATA(nid) = (pg_data_t *)(pfn_to_kaddr(pgdat_phys>>PAGE_SHIFT));
Y
Yinghai Lu 已提交
169 170 171
		memset(buf, 0, sizeof(buf));
		sprintf(buf, "NODE_DATA %d",  nid);
		reserve_early(pgdat_phys, pgdat_phys + sizeof(pg_data_t), buf);
L
Linus Torvalds 已提交
172
	}
173 174
	printk(KERN_DEBUG "allocate_pgdat: node %d NODE_DATA %08lx\n",
		nid, (unsigned long)NODE_DATA(nid));
L
Linus Torvalds 已提交
175 176
}

M
Mel Gorman 已提交
177
/*
178 179 180 181 182 183
 * In the DISCONTIGMEM and SPARSEMEM memory model, a portion of the kernel
 * virtual address space (KVA) is reserved and portions of nodes are mapped
 * using it. This is to allow node-local memory to be allocated for
 * structures that would normally require ZONE_NORMAL. The memory is
 * allocated with alloc_remap() and callers should be prepared to allocate
 * from the bootmem allocator instead.
M
Mel Gorman 已提交
184 185 186 187 188 189
 */
static unsigned long node_remap_start_pfn[MAX_NUMNODES];
static void *node_remap_end_vaddr[MAX_NUMNODES];
static void *node_remap_alloc_vaddr[MAX_NUMNODES];
static unsigned long node_remap_offset[MAX_NUMNODES];

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
void *alloc_remap(int nid, unsigned long size)
{
	void *allocation = node_remap_alloc_vaddr[nid];

	size = ALIGN(size, L1_CACHE_BYTES);

	if (!allocation || (allocation + size) >= node_remap_end_vaddr[nid])
		return 0;

	node_remap_alloc_vaddr[nid] += size;
	memset(allocation, 0, size);

	return allocation;
}

205
static void __init remap_numa_kva(void)
L
Linus Torvalds 已提交
206 207 208 209 210 211
{
	void *vaddr;
	unsigned long pfn;
	int node;

	for_each_online_node(node) {
212
		printk(KERN_DEBUG "remap_numa_kva: node %d\n", node);
L
Linus Torvalds 已提交
213 214
		for (pfn=0; pfn < node_remap_size[node]; pfn += PTRS_PER_PTE) {
			vaddr = node_remap_start_vaddr[node]+(pfn<<PAGE_SHIFT);
215 216 217
			printk(KERN_DEBUG "remap_numa_kva: %08lx to pfn %08lx\n",
				(unsigned long)vaddr,
				node_remap_start_pfn[node] + pfn);
L
Linus Torvalds 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230
			set_pmd_pfn((ulong) vaddr, 
				node_remap_start_pfn[node] + pfn, 
				PAGE_KERNEL_LARGE);
		}
	}
}

static unsigned long calculate_numa_remap_pages(void)
{
	int nid;
	unsigned long size, reserve_pages = 0;

	for_each_online_node(nid) {
231 232
		u64 node_kva_target;
		u64 node_kva_final;
233

L
Linus Torvalds 已提交
234 235 236 237
		/*
		 * The acpi/srat node info can show hot-add memroy zones
		 * where memory could be added but not currently present.
		 */
238
		printk(KERN_DEBUG "node %d pfn: [%lx - %lx]\n",
239
			nid, node_start_pfn[nid], node_end_pfn[nid]);
L
Linus Torvalds 已提交
240 241
		if (node_start_pfn[nid] > max_pfn)
			continue;
242 243
		if (!node_end_pfn[nid])
			continue;
L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251 252 253
		if (node_end_pfn[nid] > max_pfn)
			node_end_pfn[nid] = max_pfn;

		/* ensure the remap includes space for the pgdat. */
		size = node_remap_size[nid] + sizeof(pg_data_t);

		/* convert size to large (pmd size) pages, rounding up */
		size = (size + LARGE_PAGE_BYTES - 1) / LARGE_PAGE_BYTES;
		/* now the roundup is correct, convert to PAGE_SIZE pages */
		size = size * PTRS_PER_PTE;
254

255
		node_kva_target = round_down(node_end_pfn[nid] - size,
256
						 PTRS_PER_PTE);
257
		node_kva_target <<= PAGE_SHIFT;
258
		do {
259
			node_kva_final = find_e820_area(node_kva_target,
260 261 262
					((u64)node_end_pfn[nid])<<PAGE_SHIFT,
						((u64)size)<<PAGE_SHIFT,
						LARGE_PAGE_BYTES);
263 264 265
			node_kva_target -= LARGE_PAGE_BYTES;
		} while (node_kva_final == -1ULL &&
			 (node_kva_target>>PAGE_SHIFT) > (node_start_pfn[nid]));
266

267
		if (node_kva_final == -1ULL)
268
			panic("Can not get kva ram\n");
269

L
Linus Torvalds 已提交
270 271
		node_remap_size[nid] = size;
		node_remap_offset[nid] = reserve_pages;
272
		reserve_pages += size;
273 274
		printk(KERN_DEBUG "Reserving %ld pages of KVA for lmem_map of"
				  " node %d at %llx\n",
275
				size, nid, node_kva_final>>PAGE_SHIFT);
276 277 278 279 280

		/*
		 *  prevent kva address below max_low_pfn want it on system
		 *  with less memory later.
		 *  layout will be: KVA address , KVA RAM
281 282 283 284 285 286
		 *
		 *  we are supposed to only record the one less then max_low_pfn
		 *  but we could have some hole in high memory, and it will only
		 *  check page_is_ram(pfn) && !page_is_reserved_early(pfn) to decide
		 *  to use it as free.
		 *  So reserve_early here, hope we don't run out of that array
287
		 */
288 289 290
		reserve_early(node_kva_final,
			      node_kva_final+(((u64)size)<<PAGE_SHIFT),
			      "KVA RAM");
291

292 293 294
		node_remap_start_pfn[nid] = node_kva_final>>PAGE_SHIFT;
		remove_active_range(nid, node_remap_start_pfn[nid],
					 node_remap_start_pfn[nid] + size);
L
Linus Torvalds 已提交
295
	}
296
	printk(KERN_INFO "Reserving total of %lx pages for numa KVA remap\n",
L
Linus Torvalds 已提交
297 298 299 300
			reserve_pages);
	return reserve_pages;
}

M
Mel Gorman 已提交
301 302 303 304 305 306 307 308 309
static void init_remap_allocator(int nid)
{
	node_remap_start_vaddr[nid] = pfn_to_kaddr(
			kva_start_pfn + node_remap_offset[nid]);
	node_remap_end_vaddr[nid] = node_remap_start_vaddr[nid] +
		(node_remap_size[nid] * PAGE_SIZE);
	node_remap_alloc_vaddr[nid] = node_remap_start_vaddr[nid] +
		ALIGN(sizeof(pg_data_t), PAGE_SIZE);

310
	printk(KERN_DEBUG "node %d will remap to vaddr %08lx - %08lx\n", nid,
M
Mel Gorman 已提交
311
		(ulong) node_remap_start_vaddr[nid],
312
		(ulong) node_remap_end_vaddr[nid]);
M
Mel Gorman 已提交
313 314
}

315
void __init initmem_init(unsigned long start_pfn,
316
				  unsigned long end_pfn)
L
Linus Torvalds 已提交
317 318
{
	int nid;
319
	long kva_target_pfn;
L
Linus Torvalds 已提交
320 321 322 323 324

	/*
	 * When mapping a NUMA machine we allocate the node_mem_map arrays
	 * from node local memory.  They are then mapped directly into KVA
	 * between zone normal and vmalloc space.  Calculate the size of
S
Simon Arlott 已提交
325
	 * this space and use it to adjust the boundary between ZONE_NORMAL
L
Linus Torvalds 已提交
326 327
	 * and ZONE_HIGHMEM.
	 */
328

L
Linus Torvalds 已提交
329 330
	get_memcfg_numa();

331
	kva_pages = roundup(calculate_numa_remap_pages(), PTRS_PER_PTE);
L
Linus Torvalds 已提交
332

333 334 335 336 337 338 339 340
	kva_target_pfn = round_down(max_low_pfn - kva_pages, PTRS_PER_PTE);
	do {
		kva_start_pfn = find_e820_area(kva_target_pfn<<PAGE_SHIFT,
					max_low_pfn<<PAGE_SHIFT,
					kva_pages<<PAGE_SHIFT,
					PTRS_PER_PTE<<PAGE_SHIFT) >> PAGE_SHIFT;
		kva_target_pfn -= PTRS_PER_PTE;
	} while (kva_start_pfn == -1UL && kva_target_pfn > min_low_pfn);
M
Mel Gorman 已提交
341

342 343
	if (kva_start_pfn == -1UL)
		panic("Can not get kva space\n");
344

345
	printk(KERN_INFO "kva_start_pfn ~ %lx max_low_pfn ~ %lx\n",
346
		kva_start_pfn, max_low_pfn);
347
	printk(KERN_INFO "max_pfn = %lx\n", max_pfn);
348 349 350 351 352

	/* avoid clash with initrd */
	reserve_early(kva_start_pfn<<PAGE_SHIFT,
		      (kva_start_pfn + kva_pages)<<PAGE_SHIFT,
		     "KVA PG");
L
Linus Torvalds 已提交
353 354
#ifdef CONFIG_HIGHMEM
	highstart_pfn = highend_pfn = max_pfn;
355 356
	if (max_pfn > max_low_pfn)
		highstart_pfn = max_low_pfn;
L
Linus Torvalds 已提交
357 358
	printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
	       pages_to_mb(highend_pfn - highstart_pfn));
359 360 361
	num_physpages = highend_pfn;
	high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
#else
362 363
	num_physpages = max_low_pfn;
	high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
L
Linus Torvalds 已提交
364 365
#endif
	printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
366
			pages_to_mb(max_low_pfn));
367 368
	printk(KERN_DEBUG "max_low_pfn = %lx, highstart_pfn = %lx\n",
			max_low_pfn, highstart_pfn);
L
Linus Torvalds 已提交
369

370
	printk(KERN_DEBUG "Low memory ends at vaddr %08lx\n",
L
Linus Torvalds 已提交
371 372
			(ulong) pfn_to_kaddr(max_low_pfn));
	for_each_online_node(nid) {
M
Mel Gorman 已提交
373
		init_remap_allocator(nid);
374

L
Linus Torvalds 已提交
375 376
		allocate_pgdat(nid);
	}
377 378
	remap_numa_kva();

379
	printk(KERN_DEBUG "High memory starts at vaddr %08lx\n",
L
Linus Torvalds 已提交
380 381
			(ulong) pfn_to_kaddr(highstart_pfn));
	for_each_online_node(nid)
382
		propagate_e820_map_node(nid);
L
Linus Torvalds 已提交
383

384 385 386
	for_each_online_node(nid)
		memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));

387
	NODE_DATA(0)->bdata = &bootmem_node_data[0];
L
Linus Torvalds 已提交
388 389 390
	setup_bootmem_allocator();
}

Y
Yinghai Lu 已提交
391
void __init set_highmem_pages_init(void)
L
Linus Torvalds 已提交
392 393 394
{
#ifdef CONFIG_HIGHMEM
	struct zone *zone;
395
	int nid;
L
Linus Torvalds 已提交
396 397

	for_each_zone(zone) {
398
		unsigned long zone_start_pfn, zone_end_pfn;
399

L
Linus Torvalds 已提交
400 401 402 403
		if (!is_highmem(zone))
			continue;

		zone_start_pfn = zone->zone_start_pfn;
404 405
		zone_end_pfn = zone_start_pfn + zone->spanned_pages;

406
		nid = zone_to_nid(zone);
407
		printk(KERN_INFO "Initializing %s for node %d (%08lx:%08lx)\n",
408 409 410
				zone->name, nid, zone_start_pfn, zone_end_pfn);

		add_highpages_with_active_regions(nid, zone_start_pfn,
Y
Yinghai Lu 已提交
411
				 zone_end_pfn);
L
Linus Torvalds 已提交
412 413 414 415
	}
	totalram_pages += totalhigh_pages;
#endif
}
416 417

#ifdef CONFIG_MEMORY_HOTPLUG
418
static int paddr_to_nid(u64 addr)
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
{
	int nid;
	unsigned long pfn = PFN_DOWN(addr);

	for_each_node(nid)
		if (node_start_pfn[nid] <= pfn &&
		    pfn < node_end_pfn[nid])
			return nid;

	return -1;
}

/*
 * This function is used to ask node id BEFORE memmap and mem_section's
 * initialization (pfn_to_nid() can't be used yet).
 * If _PXM is not defined on ACPI's DSDT, node id must be found by this.
 */
int memory_add_physaddr_to_nid(u64 addr)
{
	int nid = paddr_to_nid(addr);
	return (nid >= 0) ? nid : 0;
}

EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
#endif
444