discontig.c 22.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2000, 2003 Silicon Graphics, Inc.  All rights reserved.
 * Copyright (c) 2001 Intel Corp.
 * Copyright (c) 2001 Tony Luck <tony.luck@intel.com>
 * Copyright (c) 2002 NEC Corp.
 * Copyright (c) 2002 Kimio Suganuma <k-suganuma@da.jp.nec.com>
 * Copyright (c) 2004 Silicon Graphics, Inc
 *	Russ Anderson <rja@sgi.com>
 *	Jesse Barnes <jbarnes@sgi.com>
 *	Jack Steiner <steiner@sgi.com>
 */

/*
 * Platform initialization for Discontig Memory
 */

#include <linux/kernel.h>
#include <linux/mm.h>
19
#include <linux/nmi.h>
L
Linus Torvalds 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <linux/swap.h>
#include <linux/bootmem.h>
#include <linux/acpi.h>
#include <linux/efi.h>
#include <linux/nodemask.h>
#include <asm/pgalloc.h>
#include <asm/tlb.h>
#include <asm/meminit.h>
#include <asm/numa.h>
#include <asm/sections.h>

/*
 * Track per-node information needed to setup the boot memory allocator, the
 * per-node areas, and the real VM.
 */
struct early_node_data {
	struct ia64_node_data *node_data;
	unsigned long pernode_addr;
	unsigned long pernode_size;
	unsigned long num_physpages;
40
#ifdef CONFIG_ZONE_DMA
L
Linus Torvalds 已提交
41
	unsigned long num_dma_physpages;
42
#endif
L
Linus Torvalds 已提交
43 44 45 46 47
	unsigned long min_pfn;
	unsigned long max_pfn;
};

static struct early_node_data mem_data[MAX_NUMNODES] __initdata;
B
bob.picco 已提交
48
static nodemask_t memory_less_mask __initdata;
L
Linus Torvalds 已提交
49

K
Ken'ichi Ohmichi 已提交
50
pg_data_t *pgdat_list[MAX_NUMNODES];
51

L
Linus Torvalds 已提交
52 53 54 55
/*
 * To prevent cache aliasing effects, align per-node structures so that they
 * start at addresses that are strided by node number.
 */
56
#define MAX_NODE_ALIGN_OFFSET	(32 * 1024 * 1024)
L
Linus Torvalds 已提交
57
#define NODEDATA_ALIGN(addr, node)						\
58 59
	((((addr) + 1024*1024-1) & ~(1024*1024-1)) + 				\
	     (((node)*PERCPU_PAGE_SIZE) & (MAX_NODE_ALIGN_OFFSET - 1)))
L
Linus Torvalds 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

/**
 * build_node_maps - callback to setup bootmem structs for each node
 * @start: physical start of range
 * @len: length of range
 * @node: node where this range resides
 *
 * We allocate a struct bootmem_data for each piece of memory that we wish to
 * treat as a virtually contiguous block (i.e. each node). Each such block
 * must start on an %IA64_GRANULE_SIZE boundary, so we round the address down
 * if necessary.  Any non-existent pages will simply be part of the virtual
 * memmap.  We also update min_low_pfn and max_low_pfn here as we receive
 * memory ranges from the caller.
 */
static int __init build_node_maps(unsigned long start, unsigned long len,
				  int node)
{
77
	unsigned long spfn, epfn, end = start + len;
78
	struct bootmem_data *bdp = &bootmem_node_data[node];
L
Linus Torvalds 已提交
79 80

	epfn = GRANULEROUNDUP(end) >> PAGE_SHIFT;
81
	spfn = GRANULEROUNDDOWN(start) >> PAGE_SHIFT;
L
Linus Torvalds 已提交
82 83

	if (!bdp->node_low_pfn) {
84
		bdp->node_min_pfn = spfn;
L
Linus Torvalds 已提交
85 86
		bdp->node_low_pfn = epfn;
	} else {
87
		bdp->node_min_pfn = min(spfn, bdp->node_min_pfn);
L
Linus Torvalds 已提交
88 89 90 91 92 93 94
		bdp->node_low_pfn = max(epfn, bdp->node_low_pfn);
	}

	return 0;
}

/**
B
bob.picco 已提交
95
 * early_nr_cpus_node - return number of cpus on a given node
L
Linus Torvalds 已提交
96 97
 * @node: node to check
 *
B
bob.picco 已提交
98
 * Count the number of cpus on @node.  We can't use nr_cpus_node() yet because
L
Linus Torvalds 已提交
99
 * acpi_boot_init() (which builds the node_to_cpu_mask array) hasn't been
B
bob.picco 已提交
100
 * called yet.  Note that node 0 will also count all non-existent cpus.
L
Linus Torvalds 已提交
101
 */
102
static int __meminit early_nr_cpus_node(int node)
L
Linus Torvalds 已提交
103 104 105
{
	int cpu, n = 0;

106
	for_each_possible_early_cpu(cpu)
L
Linus Torvalds 已提交
107
		if (node == node_cpuid[cpu].nid)
B
bob.picco 已提交
108
			n++;
L
Linus Torvalds 已提交
109 110 111 112

	return n;
}

B
bob.picco 已提交
113 114 115 116
/**
 * compute_pernodesize - compute size of pernode data
 * @node: the node id.
 */
117
static unsigned long __meminit compute_pernodesize(int node)
B
bob.picco 已提交
118 119 120 121 122 123 124 125
{
	unsigned long pernodesize = 0, cpus;

	cpus = early_nr_cpus_node(node);
	pernodesize += PERCPU_PAGE_SIZE * cpus;
	pernodesize += node * L1_CACHE_BYTES;
	pernodesize += L1_CACHE_ALIGN(sizeof(pg_data_t));
	pernodesize += L1_CACHE_ALIGN(sizeof(struct ia64_node_data));
126
	pernodesize += L1_CACHE_ALIGN(sizeof(pg_data_t));
B
bob.picco 已提交
127 128 129
	pernodesize = PAGE_ALIGN(pernodesize);
	return pernodesize;
}
L
Linus Torvalds 已提交
130

T
Tony Luck 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144
/**
 * per_cpu_node_setup - setup per-cpu areas on each node
 * @cpu_data: per-cpu area on this node
 * @node: node to setup
 *
 * Copy the static per-cpu data into the region we just set aside and then
 * setup __per_cpu_offset for each CPU on this node.  Return a pointer to
 * the end of the area.
 */
static void *per_cpu_node_setup(void *cpu_data, int node)
{
#ifdef CONFIG_SMP
	int cpu;

145
	for_each_possible_early_cpu(cpu) {
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
		void *src = cpu == 0 ? __cpu0_per_cpu : __phys_per_cpu_start;

		if (node != node_cpuid[cpu].nid)
			continue;

		memcpy(__va(cpu_data), src, __per_cpu_end - __per_cpu_start);
		__per_cpu_offset[cpu] = (char *)__va(cpu_data) -
			__per_cpu_start;

		/*
		 * percpu area for cpu0 is moved from the __init area
		 * which is setup by head.S and used till this point.
		 * Update ar.k3.  This move is ensures that percpu
		 * area for cpu0 is on the correct node and its
		 * virtual address isn't insanely far from other
		 * percpu areas which is important for congruent
		 * percpu allocator.
		 */
		if (cpu == 0)
			ia64_set_kr(IA64_KR_PER_CPU_DATA,
				    (unsigned long)cpu_data -
				    (unsigned long)__per_cpu_start);

		cpu_data += PERCPU_PAGE_SIZE;
T
Tony Luck 已提交
170 171 172 173 174
	}
#endif
	return cpu_data;
}

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
#ifdef CONFIG_SMP
/**
 * setup_per_cpu_areas - setup percpu areas
 *
 * Arch code has already allocated and initialized percpu areas.  All
 * this function has to do is to teach the determined layout to the
 * dynamic percpu allocator, which happens to be more complex than
 * creating whole new ones using helpers.
 */
void __init setup_per_cpu_areas(void)
{
	struct pcpu_alloc_info *ai;
	struct pcpu_group_info *uninitialized_var(gi);
	unsigned int *cpu_map;
	void *base;
	unsigned long base_offset;
	unsigned int cpu;
	ssize_t static_size, reserved_size, dyn_size;
	int node, prev_node, unit, nr_units, rc;

	ai = pcpu_alloc_alloc_info(MAX_NUMNODES, nr_cpu_ids);
	if (!ai)
		panic("failed to allocate pcpu_alloc_info");
	cpu_map = ai->groups[0].cpu_map;

	/* determine base */
	base = (void *)ULONG_MAX;
	for_each_possible_cpu(cpu)
		base = min(base,
			   (void *)(__per_cpu_offset[cpu] + __per_cpu_start));
	base_offset = (void *)__per_cpu_start - base;

	/* build cpu_map, units are grouped by node */
	unit = 0;
	for_each_node(node)
		for_each_possible_cpu(cpu)
			if (node == node_cpuid[cpu].nid)
				cpu_map[unit++] = cpu;
	nr_units = unit;

	/* set basic parameters */
	static_size = __per_cpu_end - __per_cpu_start;
	reserved_size = PERCPU_MODULE_RESERVE;
	dyn_size = PERCPU_PAGE_SIZE - static_size - reserved_size;
	if (dyn_size < 0)
		panic("percpu area overflow static=%zd reserved=%zd\n",
		      static_size, reserved_size);

	ai->static_size		= static_size;
	ai->reserved_size	= reserved_size;
	ai->dyn_size		= dyn_size;
	ai->unit_size		= PERCPU_PAGE_SIZE;
	ai->atom_size		= PAGE_SIZE;
	ai->alloc_size		= PERCPU_PAGE_SIZE;

	/*
	 * CPUs are put into groups according to node.  Walk cpu_map
	 * and create new groups at node boundaries.
	 */
	prev_node = -1;
	ai->nr_groups = 0;
	for (unit = 0; unit < nr_units; unit++) {
		cpu = cpu_map[unit];
		node = node_cpuid[cpu].nid;

		if (node == prev_node) {
			gi->nr_units++;
			continue;
		}
		prev_node = node;

		gi = &ai->groups[ai->nr_groups++];
		gi->nr_units		= 1;
		gi->base_offset		= __per_cpu_offset[cpu] + base_offset;
		gi->cpu_map		= &cpu_map[unit];
	}

	rc = pcpu_setup_first_chunk(ai, base);
	if (rc)
		panic("failed to setup percpu area (err=%d)", rc);

	pcpu_free_alloc_info(ai);
}
#endif

L
Linus Torvalds 已提交
260
/**
B
bob.picco 已提交
261 262 263 264
 * fill_pernode - initialize pernode data.
 * @node: the node id.
 * @pernode: physical address of pernode data
 * @pernodesize: size of the pernode data
L
Linus Torvalds 已提交
265
 */
B
bob.picco 已提交
266 267
static void __init fill_pernode(int node, unsigned long pernode,
	unsigned long pernodesize)
L
Linus Torvalds 已提交
268
{
B
bob.picco 已提交
269
	void *cpu_data;
T
Tony Luck 已提交
270
	int cpus = early_nr_cpus_node(node);
271
	struct bootmem_data *bdp = &bootmem_node_data[node];
L
Linus Torvalds 已提交
272

B
bob.picco 已提交
273 274 275
	mem_data[node].pernode_addr = pernode;
	mem_data[node].pernode_size = pernodesize;
	memset(__va(pernode), 0, pernodesize);
L
Linus Torvalds 已提交
276

B
bob.picco 已提交
277 278 279 280
	cpu_data = (void *)pernode;
	pernode += PERCPU_PAGE_SIZE * cpus;
	pernode += node * L1_CACHE_BYTES;

281
	pgdat_list[node] = __va(pernode);
B
bob.picco 已提交
282 283 284 285 286
	pernode += L1_CACHE_ALIGN(sizeof(pg_data_t));

	mem_data[node].node_data = __va(pernode);
	pernode += L1_CACHE_ALIGN(sizeof(struct ia64_node_data));

287
	pgdat_list[node]->bdata = bdp;
B
bob.picco 已提交
288 289
	pernode += L1_CACHE_ALIGN(sizeof(pg_data_t));

T
Tony Luck 已提交
290
	cpu_data = per_cpu_node_setup(cpu_data, node);
L
Linus Torvalds 已提交
291

B
bob.picco 已提交
292 293
	return;
}
T
Tony Luck 已提交
294

L
Linus Torvalds 已提交
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
/**
 * find_pernode_space - allocate memory for memory map and per-node structures
 * @start: physical start of range
 * @len: length of range
 * @node: node where this range resides
 *
 * This routine reserves space for the per-cpu data struct, the list of
 * pg_data_ts and the per-node data struct.  Each node will have something like
 * the following in the first chunk of addr. space large enough to hold it.
 *
 *    ________________________
 *   |                        |
 *   |~~~~~~~~~~~~~~~~~~~~~~~~| <-- NODEDATA_ALIGN(start, node) for the first
 *   |    PERCPU_PAGE_SIZE *  |     start and length big enough
 *   |    cpus_on_this_node   | Node 0 will also have entries for all non-existent cpus.
 *   |------------------------|
 *   |   local pg_data_t *    |
 *   |------------------------|
 *   |  local ia64_node_data  |
 *   |------------------------|
 *   |          ???           |
 *   |________________________|
 *
 * Once this space has been set aside, the bootmem maps are initialized.  We
 * could probably move the allocation of the per-cpu and ia64_node_data space
 * outside of this function and use alloc_bootmem_node(), but doing it here
 * is straightforward and we get the alignments we want so...
 */
static int __init find_pernode_space(unsigned long start, unsigned long len,
				     int node)
{
326
	unsigned long spfn, epfn;
L
Linus Torvalds 已提交
327
	unsigned long pernodesize = 0, pernode, pages, mapsize;
328
	struct bootmem_data *bdp = &bootmem_node_data[node];
L
Linus Torvalds 已提交
329

330
	spfn = start >> PAGE_SHIFT;
L
Linus Torvalds 已提交
331 332
	epfn = (start + len) >> PAGE_SHIFT;

333
	pages = bdp->node_low_pfn - bdp->node_min_pfn;
L
Linus Torvalds 已提交
334 335 336 337 338 339
	mapsize = bootmem_bootmap_pages(pages) << PAGE_SHIFT;

	/*
	 * Make sure this memory falls within this node's usable memory
	 * since we may have thrown some away in build_maps().
	 */
340
	if (spfn < bdp->node_min_pfn || epfn > bdp->node_low_pfn)
L
Linus Torvalds 已提交
341 342 343 344 345 346 347 348 349 350
		return 0;

	/* Don't setup this node's local space twice... */
	if (mem_data[node].pernode_addr)
		return 0;

	/*
	 * Calculate total size needed, incl. what's necessary
	 * for good alignment and alias prevention.
	 */
B
bob.picco 已提交
351
	pernodesize = compute_pernodesize(node);
L
Linus Torvalds 已提交
352 353 354
	pernode = NODEDATA_ALIGN(start, node);

	/* Is this range big enough for what we want to store here? */
B
bob.picco 已提交
355 356
	if (start + len > (pernode + pernodesize + mapsize))
		fill_pernode(node, pernode, pernodesize);
L
Linus Torvalds 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

	return 0;
}

/**
 * free_node_bootmem - free bootmem allocator memory for use
 * @start: physical start of range
 * @len: length of range
 * @node: node where this range resides
 *
 * Simply calls the bootmem allocator to free the specified ranged from
 * the given pg_data_t's bdata struct.  After this function has been called
 * for all the entries in the EFI memory map, the bootmem allocator will
 * be ready to service allocation requests.
 */
static int __init free_node_bootmem(unsigned long start, unsigned long len,
				    int node)
{
375
	free_bootmem_node(pgdat_list[node], start, len);
L
Linus Torvalds 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393

	return 0;
}

/**
 * reserve_pernode_space - reserve memory for per-node space
 *
 * Reserve the space used by the bootmem maps & per-node space in the boot
 * allocator so that when we actually create the real mem maps we don't
 * use their memory.
 */
static void __init reserve_pernode_space(void)
{
	unsigned long base, size, pages;
	struct bootmem_data *bdp;
	int node;

	for_each_online_node(node) {
394
		pg_data_t *pdp = pgdat_list[node];
L
Linus Torvalds 已提交
395

B
bob.picco 已提交
396 397 398
		if (node_isset(node, memory_less_mask))
			continue;

L
Linus Torvalds 已提交
399 400 401
		bdp = pdp->bdata;

		/* First the bootmem_map itself */
402
		pages = bdp->node_low_pfn - bdp->node_min_pfn;
L
Linus Torvalds 已提交
403 404
		size = bootmem_bootmap_pages(pages) << PAGE_SHIFT;
		base = __pa(bdp->node_bootmem_map);
405
		reserve_bootmem_node(pdp, base, size, BOOTMEM_DEFAULT);
L
Linus Torvalds 已提交
406 407 408 409

		/* Now the per-node space */
		size = mem_data[node].pernode_size;
		base = __pa(mem_data[node].pernode_addr);
410
		reserve_bootmem_node(pdp, base, size, BOOTMEM_DEFAULT);
L
Linus Torvalds 已提交
411 412 413
	}
}

414 415 416 417 418
static void __meminit scatter_node_data(void)
{
	pg_data_t **dst;
	int node;

419 420 421 422 423
	/*
	 * for_each_online_node() can't be used at here.
	 * node_online_map is not set for hot-added nodes at this time,
	 * because we are halfway through initialization of the new node's
	 * structures.  If for_each_online_node() is used, a new node's
S
Simon Arlott 已提交
424
	 * pg_data_ptrs will be not initialized. Instead of using it,
425 426 427 428 429 430 431
	 * pgdat_list[] is checked.
	 */
	for_each_node(node) {
		if (pgdat_list[node]) {
			dst = LOCAL_DATA_ADDR(pgdat_list[node])->pg_data_ptrs;
			memcpy(dst, pgdat_list, sizeof(pgdat_list));
		}
432 433 434
	}
}

L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442 443 444
/**
 * initialize_pernode_data - fixup per-cpu & per-node pointers
 *
 * Each node's per-node area has a copy of the global pg_data_t list, so
 * we copy that to each node here, as well as setting the per-cpu pointer
 * to the local node data structure.  The active_cpus field of the per-node
 * structure gets setup by the platform_cpu_init() function later.
 */
static void __init initialize_pernode_data(void)
{
T
Tony Luck 已提交
445
	int cpu, node;
L
Linus Torvalds 已提交
446

447 448
	scatter_node_data();

T
Tony Luck 已提交
449
#ifdef CONFIG_SMP
L
Linus Torvalds 已提交
450
	/* Set the node_data pointer for each per-cpu struct */
451
	for_each_possible_early_cpu(cpu) {
L
Linus Torvalds 已提交
452 453 454
		node = node_cpuid[cpu].nid;
		per_cpu(cpu_info, cpu).node_data = mem_data[node].node_data;
	}
T
Tony Luck 已提交
455 456 457 458 459 460 461 462 463 464
#else
	{
		struct cpuinfo_ia64 *cpu0_cpu_info;
		cpu = 0;
		node = node_cpuid[cpu].nid;
		cpu0_cpu_info = (struct cpuinfo_ia64 *)(__phys_per_cpu_start +
			((char *)&per_cpu__cpu_info - __per_cpu_start));
		cpu0_cpu_info->node_data = mem_data[node].node_data;
	}
#endif /* CONFIG_SMP */
L
Linus Torvalds 已提交
465 466
}

B
bob.picco 已提交
467 468 469 470 471 472 473
/**
 * memory_less_node_alloc - * attempt to allocate memory on the best NUMA slit
 * 	node but fall back to any other node when __alloc_bootmem_node fails
 *	for best.
 * @nid: node id
 * @pernodesize: size of this node's pernode data
 */
B
Bob Picco 已提交
474
static void __init *memory_less_node_alloc(int nid, unsigned long pernodesize)
B
bob.picco 已提交
475 476 477
{
	void *ptr = NULL;
	u8 best = 0xff;
B
Bob Picco 已提交
478
	int bestnode = -1, node, anynode = 0;
B
bob.picco 已提交
479 480 481 482 483 484 485 486

	for_each_online_node(node) {
		if (node_isset(node, memory_less_mask))
			continue;
		else if (node_distance(nid, node) < best) {
			best = node_distance(nid, node);
			bestnode = node;
		}
B
Bob Picco 已提交
487
		anynode = node;
B
bob.picco 已提交
488 489
	}

B
Bob Picco 已提交
490 491 492
	if (bestnode == -1)
		bestnode = anynode;

493
	ptr = __alloc_bootmem_node(pgdat_list[bestnode], pernodesize,
B
Bob Picco 已提交
494
		PERCPU_PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
B
bob.picco 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510

	return ptr;
}

/**
 * memory_less_nodes - allocate and initialize CPU only nodes pernode
 *	information.
 */
static void __init memory_less_nodes(void)
{
	unsigned long pernodesize;
	void *pernode;
	int node;

	for_each_node_mask(node, memory_less_mask) {
		pernodesize = compute_pernodesize(node);
B
Bob Picco 已提交
511
		pernode = memory_less_node_alloc(node, pernodesize);
B
bob.picco 已提交
512 513 514 515 516 517
		fill_pernode(node, __pa(pernode), pernodesize);
	}

	return;
}

L
Linus Torvalds 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
/**
 * find_memory - walk the EFI memory map and setup the bootmem allocator
 *
 * Called early in boot to setup the bootmem allocator, and to
 * allocate the per-cpu and per-node structures.
 */
void __init find_memory(void)
{
	int node;

	reserve_memory();

	if (num_online_nodes() == 0) {
		printk(KERN_ERR "node info missing!\n");
		node_set_online(0);
	}

B
bob.picco 已提交
535
	nodes_or(memory_less_mask, memory_less_mask, node_online_map);
L
Linus Torvalds 已提交
536 537 538 539 540 541
	min_low_pfn = -1;
	max_low_pfn = 0;

	/* These actually end up getting called by call_pernode_memory() */
	efi_memmap_walk(filter_rsvd_memory, build_node_maps);
	efi_memmap_walk(filter_rsvd_memory, find_pernode_space);
542
	efi_memmap_walk(find_max_min_low_pfn, NULL);
L
Linus Torvalds 已提交
543

B
bob.picco 已提交
544
	for_each_online_node(node)
545
		if (bootmem_node_data[node].node_low_pfn) {
B
bob.picco 已提交
546 547 548
			node_clear(node, memory_less_mask);
			mem_data[node].min_pfn = ~0UL;
		}
549

550
	efi_memmap_walk(filter_memory, register_active_ranges);
551

L
Linus Torvalds 已提交
552 553 554 555 556 557 558 559 560 561
	/*
	 * Initialize the boot memory maps in reverse order since that's
	 * what the bootmem allocator expects
	 */
	for (node = MAX_NUMNODES - 1; node >= 0; node--) {
		unsigned long pernode, pernodesize, map;
		struct bootmem_data *bdp;

		if (!node_online(node))
			continue;
B
bob.picco 已提交
562 563
		else if (node_isset(node, memory_less_mask))
			continue;
L
Linus Torvalds 已提交
564

565
		bdp = &bootmem_node_data[node];
L
Linus Torvalds 已提交
566 567 568 569
		pernode = mem_data[node].pernode_addr;
		pernodesize = mem_data[node].pernode_size;
		map = pernode + pernodesize;

570
		init_bootmem_node(pgdat_list[node],
L
Linus Torvalds 已提交
571
				  map>>PAGE_SHIFT,
572
				  bdp->node_min_pfn,
L
Linus Torvalds 已提交
573 574 575 576 577 578
				  bdp->node_low_pfn);
	}

	efi_memmap_walk(filter_rsvd_memory, free_node_bootmem);

	reserve_pernode_space();
B
bob.picco 已提交
579
	memory_less_nodes();
L
Linus Torvalds 已提交
580 581 582 583 584 585 586
	initialize_pernode_data();

	max_pfn = max_low_pfn;

	find_initrd();
}

T
Tony Luck 已提交
587
#ifdef CONFIG_SMP
L
Linus Torvalds 已提交
588 589 590 591 592 593
/**
 * per_cpu_init - setup per-cpu variables
 *
 * find_pernode_space() does most of this already, we just need to set
 * local_per_cpu_offset
 */
594
void __cpuinit *per_cpu_init(void)
L
Linus Torvalds 已提交
595 596
{
	int cpu;
A
Ashok Raj 已提交
597 598 599 600
	static int first_time = 1;

	if (first_time) {
		first_time = 0;
601
		for_each_possible_early_cpu(cpu)
A
Ashok Raj 已提交
602 603
			per_cpu(local_per_cpu_offset, cpu) = __per_cpu_offset[cpu];
	}
L
Linus Torvalds 已提交
604 605 606

	return __per_cpu_start + __per_cpu_offset[smp_processor_id()];
}
T
Tony Luck 已提交
607
#endif /* CONFIG_SMP */
L
Linus Torvalds 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620 621

/**
 * show_mem - give short summary of memory stats
 *
 * Shows a simple page count of reserved and used pages in the system.
 * For discontig machines, it does this on a per-pgdat basis.
 */
void show_mem(void)
{
	int i, total_reserved = 0;
	int total_shared = 0, total_cached = 0;
	unsigned long total_present = 0;
	pg_data_t *pgdat;

J
Jes Sorensen 已提交
622
	printk(KERN_INFO "Mem-info:\n");
L
Linus Torvalds 已提交
623
	show_free_areas();
J
Jes Sorensen 已提交
624
	printk(KERN_INFO "Node memory in pages:\n");
625
	for_each_online_pgdat(pgdat) {
626 627
		unsigned long present;
		unsigned long flags;
L
Linus Torvalds 已提交
628
		int shared = 0, cached = 0, reserved = 0;
629 630 631

		pgdat_resize_lock(pgdat, &flags);
		present = pgdat->node_present_pages;
L
Linus Torvalds 已提交
632
		for(i = 0; i < pgdat->node_spanned_pages; i++) {
633
			struct page *page;
634 635
			if (unlikely(i % MAX_ORDER_NR_PAGES == 0))
				touch_nmi_watchdog();
636 637
			if (pfn_valid(pgdat->node_start_pfn + i))
				page = pfn_to_page(pgdat->node_start_pfn + i);
638
			else {
639 640
				i = vmemmap_find_next_valid_pfn(pgdat->node_id,
					 i) - 1;
L
Linus Torvalds 已提交
641
				continue;
642
			}
643
			if (PageReserved(page))
L
Linus Torvalds 已提交
644
				reserved++;
645
			else if (PageSwapCache(page))
L
Linus Torvalds 已提交
646
				cached++;
647 648
			else if (page_count(page))
				shared += page_count(page)-1;
L
Linus Torvalds 已提交
649
		}
650
		pgdat_resize_unlock(pgdat, &flags);
L
Linus Torvalds 已提交
651 652 653 654
		total_present += present;
		total_reserved += reserved;
		total_cached += cached;
		total_shared += shared;
J
Jes Sorensen 已提交
655 656 657
		printk(KERN_INFO "Node %4d:  RAM: %11ld, rsvd: %8d, "
		       "shrd: %10d, swpd: %10d\n", pgdat->node_id,
		       present, reserved, shared, cached);
L
Linus Torvalds 已提交
658
	}
J
Jes Sorensen 已提交
659 660 661 662 663
	printk(KERN_INFO "%ld pages of RAM\n", total_present);
	printk(KERN_INFO "%d reserved pages\n", total_reserved);
	printk(KERN_INFO "%d pages shared\n", total_shared);
	printk(KERN_INFO "%d pages swap cached\n", total_cached);
	printk(KERN_INFO "Total of %ld pages in page table cache\n",
664
	       quicklist_total_size());
J
Jes Sorensen 已提交
665
	printk(KERN_INFO "%d free buffer pages\n", nr_free_buffer_pages());
L
Linus Torvalds 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
}

/**
 * call_pernode_memory - use SRAT to call callback functions with node info
 * @start: physical start of range
 * @len: length of range
 * @arg: function to call for each range
 *
 * efi_memmap_walk() knows nothing about layout of memory across nodes. Find
 * out to which node a block of memory belongs.  Ignore memory that we cannot
 * identify, and split blocks that run across multiple nodes.
 *
 * Take this opportunity to round the start address up and the end address
 * down to page boundaries.
 */
void call_pernode_memory(unsigned long start, unsigned long len, void *arg)
{
	unsigned long rs, re, end = start + len;
	void (*func)(unsigned long, unsigned long, int);
	int i;

	start = PAGE_ALIGN(start);
	end &= PAGE_MASK;
	if (start >= end)
		return;

	func = arg;

	if (!num_node_memblks) {
		/* No SRAT table, so assume one node (node 0) */
		if (start < end)
			(*func)(start, end - start, 0);
		return;
	}

	for (i = 0; i < num_node_memblks; i++) {
		rs = max(start, node_memblk[i].start_paddr);
		re = min(end, node_memblk[i].start_paddr +
			 node_memblk[i].size);

		if (rs < re)
			(*func)(rs, re - rs, node_memblk[i].nid);

		if (re == end)
			break;
	}
}

/**
 * count_node_pages - callback to build per-node memory info structures
 * @start: physical start of range
 * @len: length of range
 * @node: node where this range resides
 *
 * Each node has it's own number of physical pages, DMAable pages, start, and
 * end page frame number.  This routine will be called by call_pernode_memory()
 * for each piece of usable memory and will setup these values for each node.
 * Very similar to build_maps().
 */
static __init int count_node_pages(unsigned long start, unsigned long len, int node)
{
	unsigned long end = start + len;

	mem_data[node].num_physpages += len >> PAGE_SHIFT;
730
#ifdef CONFIG_ZONE_DMA
L
Linus Torvalds 已提交
731 732 733
	if (start <= __pa(MAX_DMA_ADDRESS))
		mem_data[node].num_dma_physpages +=
			(min(end, __pa(MAX_DMA_ADDRESS)) - start) >>PAGE_SHIFT;
734
#endif
L
Linus Torvalds 已提交
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
	start = GRANULEROUNDDOWN(start);
	end = GRANULEROUNDUP(end);
	mem_data[node].max_pfn = max(mem_data[node].max_pfn,
				     end >> PAGE_SHIFT);
	mem_data[node].min_pfn = min(mem_data[node].min_pfn,
				     start >> PAGE_SHIFT);

	return 0;
}

/**
 * paging_init - setup page tables
 *
 * paging_init() sets up the page tables for each node of the system and frees
 * the bootmem allocator memory for general use.
 */
void __init paging_init(void)
{
	unsigned long max_dma;
	unsigned long pfn_offset = 0;
755
	unsigned long max_pfn = 0;
L
Linus Torvalds 已提交
756
	int node;
757
	unsigned long max_zone_pfns[MAX_NR_ZONES];
L
Linus Torvalds 已提交
758 759 760 761 762

	max_dma = virt_to_phys((void *) MAX_DMA_ADDRESS) >> PAGE_SHIFT;

	efi_memmap_walk(filter_rsvd_memory, count_node_pages);

763 764 765
	sparse_memory_present_with_active_regions(MAX_NUMNODES);
	sparse_init();

766
#ifdef CONFIG_VIRTUAL_MEM_MAP
767
	VMALLOC_END -= PAGE_ALIGN(ALIGN(max_low_pfn, MAX_ORDER_NR_PAGES) *
768
		sizeof(struct page));
769
	vmem_map = (struct page *) VMALLOC_END;
B
bob.picco 已提交
770 771
	efi_memmap_walk(create_mem_map_page_table, NULL);
	printk("Virtual mem_map starts at 0x%p\n", vmem_map);
772
#endif
B
bob.picco 已提交
773

L
Linus Torvalds 已提交
774 775 776 777
	for_each_online_node(node) {
		num_physpages += mem_data[node].num_physpages;
		pfn_offset = mem_data[node].min_pfn;

778
#ifdef CONFIG_VIRTUAL_MEM_MAP
L
Linus Torvalds 已提交
779
		NODE_DATA(node)->node_mem_map = vmem_map + pfn_offset;
780
#endif
781 782
		if (mem_data[node].max_pfn > max_pfn)
			max_pfn = mem_data[node].max_pfn;
L
Linus Torvalds 已提交
783 784
	}

785
	memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
786
#ifdef CONFIG_ZONE_DMA
787
	max_zone_pfns[ZONE_DMA] = max_dma;
788
#endif
789 790 791
	max_zone_pfns[ZONE_NORMAL] = max_pfn;
	free_area_init_nodes(max_zone_pfns);

L
Linus Torvalds 已提交
792 793
	zero_page_memmap_ptr = virt_to_page(ia64_imva(empty_zero_page));
}
794

795
#ifdef CONFIG_MEMORY_HOTPLUG
796 797 798 799 800 801 802 803 804 805 806 807
pg_data_t *arch_alloc_nodedata(int nid)
{
	unsigned long size = compute_pernodesize(nid);

	return kzalloc(size, GFP_KERNEL);
}

void arch_free_nodedata(pg_data_t *pgdat)
{
	kfree(pgdat);
}

808 809 810 811 812
void arch_refresh_nodedata(int update_node, pg_data_t *update_pgdat)
{
	pgdat_list[update_node] = update_pgdat;
	scatter_node_data();
}
813
#endif
814 815 816 817 818 819 820 821

#ifdef CONFIG_SPARSEMEM_VMEMMAP
int __meminit vmemmap_populate(struct page *start_page,
						unsigned long size, int node)
{
	return vmemmap_populate_basepages(start_page, size, node);
}
#endif