init.c 12.4 KB
Newer Older
1 2
/*
 * linux/arch/sh/mm/init.c
L
Linus Torvalds 已提交
3 4
 *
 *  Copyright (C) 1999  Niibe Yutaka
5
 *  Copyright (C) 2002 - 2010  Paul Mundt
L
Linus Torvalds 已提交
6 7 8 9 10 11 12
 *
 *  Based on linux/arch/i386/mm/init.c:
 *   Copyright (C) 1995  Linus Torvalds
 */
#include <linux/mm.h>
#include <linux/swap.h>
#include <linux/init.h>
13
#include <linux/gfp.h>
L
Linus Torvalds 已提交
14
#include <linux/bootmem.h>
P
Paul Mundt 已提交
15
#include <linux/proc_fs.h>
16
#include <linux/pagemap.h>
17 18
#include <linux/percpu.h>
#include <linux/io.h>
Y
Yinghai Lu 已提交
19
#include <linux/memblock.h>
20
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
21
#include <asm/mmu_context.h>
P
Paul Mundt 已提交
22
#include <asm/mmzone.h>
P
Paul Mundt 已提交
23
#include <asm/kexec.h>
L
Linus Torvalds 已提交
24 25
#include <asm/tlb.h>
#include <asm/cacheflush.h>
26
#include <asm/sections.h>
P
Paul Mundt 已提交
27
#include <asm/setup.h>
L
Linus Torvalds 已提交
28
#include <asm/cache.h>
29
#include <asm/sizes.h>
L
Linus Torvalds 已提交
30 31 32

DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
pgd_t swapper_pg_dir[PTRS_PER_PGD];
33

34 35
void __init generic_mem_init(void)
{
Y
Yinghai Lu 已提交
36
	memblock_add(__MEMORY_START, __MEMORY_SIZE);
37 38
}

P
Paul Mundt 已提交
39 40 41 42 43
void __init __weak plat_mem_setup(void)
{
	/* Nothing to see here, move along. */
}

44
#ifdef CONFIG_MMU
45
static pte_t *__get_pte_phys(unsigned long addr)
L
Linus Torvalds 已提交
46 47
{
	pgd_t *pgd;
48
	pud_t *pud;
L
Linus Torvalds 已提交
49 50 51
	pmd_t *pmd;
	pte_t *pte;

S
Stuart Menefy 已提交
52
	pgd = pgd_offset_k(addr);
L
Linus Torvalds 已提交
53 54
	if (pgd_none(*pgd)) {
		pgd_ERROR(*pgd);
55
		return NULL;
L
Linus Torvalds 已提交
56 57
	}

S
Stuart Menefy 已提交
58 59 60
	pud = pud_alloc(NULL, pgd, addr);
	if (unlikely(!pud)) {
		pud_ERROR(*pud);
61
		return NULL;
62 63
	}

S
Stuart Menefy 已提交
64 65 66
	pmd = pmd_alloc(NULL, pud, addr);
	if (unlikely(!pmd)) {
		pmd_ERROR(*pmd);
67
		return NULL;
L
Linus Torvalds 已提交
68 69 70
	}

	pte = pte_offset_kernel(pmd, addr);
71 72 73 74 75 76 77 78
	return pte;
}

static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
{
	pte_t *pte;

	pte = __get_pte_phys(addr);
L
Linus Torvalds 已提交
79 80 81 82 83 84
	if (!pte_none(*pte)) {
		pte_ERROR(*pte);
		return;
	}

	set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
85
	local_flush_tlb_one(get_asid(), addr);
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

	if (pgprot_val(prot) & _PAGE_WIRED)
		tlb_wire_entry(NULL, addr, *pte);
}

static void clear_pte_phys(unsigned long addr, pgprot_t prot)
{
	pte_t *pte;

	pte = __get_pte_phys(addr);

	if (pgprot_val(prot) & _PAGE_WIRED)
		tlb_unwire_entry();

	set_pte(pte, pfn_pte(0, __pgprot(0)));
	local_flush_tlb_one(get_asid(), addr);
L
Linus Torvalds 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114
}

void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
{
	unsigned long address = __fix_to_virt(idx);

	if (idx >= __end_of_fixed_addresses) {
		BUG();
		return;
	}

	set_pte_phys(address, phys, prot);
}
115

116 117 118 119 120 121 122 123 124 125 126 127
void __clear_fixmap(enum fixed_addresses idx, pgprot_t prot)
{
	unsigned long address = __fix_to_virt(idx);

	if (idx >= __end_of_fixed_addresses) {
		BUG();
		return;
	}

	clear_pte_phys(address, prot);
}

128 129 130 131 132 133
void __init page_table_range_init(unsigned long start, unsigned long end,
					 pgd_t *pgd_base)
{
	pgd_t *pgd;
	pud_t *pud;
	pmd_t *pmd;
134 135
	pte_t *pte;
	int i, j, k;
136 137
	unsigned long vaddr;

138 139 140 141 142 143 144 145 146
	vaddr = start;
	i = __pgd_offset(vaddr);
	j = __pud_offset(vaddr);
	k = __pmd_offset(vaddr);
	pgd = pgd_base + i;

	for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
		pud = (pud_t *)pgd;
		for ( ; (j < PTRS_PER_PUD) && (vaddr != end); pud++, j++) {
147
#ifdef __PAGETABLE_PMD_FOLDED
148
			pmd = (pmd_t *)pud;
149 150 151 152 153
#else
			pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
			pud_populate(&init_mm, pud, pmd);
			pmd += k;
#endif
154 155 156 157 158 159 160 161 162
			for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) {
				if (pmd_none(*pmd)) {
					pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
					pmd_populate_kernel(&init_mm, pmd, pte);
					BUG_ON(pte != pte_offset_kernel(pmd, 0));
				}
				vaddr += PMD_SIZE;
			}
			k = 0;
163
		}
164
		j = 0;
165 166
	}
}
167
#endif	/* CONFIG_MMU */
L
Linus Torvalds 已提交
168

P
Paul Mundt 已提交
169 170 171 172 173 174 175 176 177 178
void __init allocate_pgdat(unsigned int nid)
{
	unsigned long start_pfn, end_pfn;
#ifdef CONFIG_NEED_MULTIPLE_NODES
	unsigned long phys;
#endif

	get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);

#ifdef CONFIG_NEED_MULTIPLE_NODES
Y
Yinghai Lu 已提交
179
	phys = __memblock_alloc_base(sizeof(struct pglist_data),
P
Paul Mundt 已提交
180 181 182
				SMP_CACHE_BYTES, end_pfn << PAGE_SHIFT);
	/* Retry with all of system memory */
	if (!phys)
Y
Yinghai Lu 已提交
183 184
		phys = __memblock_alloc_base(sizeof(struct pglist_data),
					SMP_CACHE_BYTES, memblock_end_of_DRAM());
P
Paul Mundt 已提交
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
	if (!phys)
		panic("Can't allocate pgdat for node %d\n", nid);

	NODE_DATA(nid) = __va(phys);
	memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));

	NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
#endif

	NODE_DATA(nid)->node_start_pfn = start_pfn;
	NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
}

static void __init bootmem_init_one_node(unsigned int nid)
{
	unsigned long total_pages, paddr;
	unsigned long end_pfn;
	struct pglist_data *p;

	p = NODE_DATA(nid);

	/* Nothing to do.. */
	if (!p->node_spanned_pages)
		return;

	end_pfn = p->node_start_pfn + p->node_spanned_pages;

	total_pages = bootmem_bootmap_pages(p->node_spanned_pages);

Y
Yinghai Lu 已提交
214
	paddr = memblock_alloc(total_pages << PAGE_SHIFT, PAGE_SIZE);
P
Paul Mundt 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227
	if (!paddr)
		panic("Can't allocate bootmap for nid[%d]\n", nid);

	init_bootmem_node(p, paddr >> PAGE_SHIFT, p->node_start_pfn, end_pfn);

	free_bootmem_with_active_regions(nid, end_pfn);

	/*
	 * XXX Handle initial reservations for the system memory node
	 * only for the moment, we'll refactor this later for handling
	 * reservations in other nodes.
	 */
	if (nid == 0) {
228 229
		struct memblock_region *reg;

P
Paul Mundt 已提交
230
		/* Reserve the sections we're already using. */
231 232 233
		for_each_memblock(reserved, reg) {
			reserve_bootmem(reg->base, reg->size, BOOTMEM_DEFAULT);
		}
P
Paul Mundt 已提交
234 235 236 237 238 239 240
	}

	sparse_memory_present_with_active_regions(nid);
}

static void __init do_init_bootmem(void)
{
241
	struct memblock_region *reg;
P
Paul Mundt 已提交
242 243 244
	int i;

	/* Add active regions with valid PFNs. */
245
	for_each_memblock(memory, reg) {
P
Paul Mundt 已提交
246
		unsigned long start_pfn, end_pfn;
247 248
		start_pfn = memblock_region_base_pfn(reg);
		end_pfn = memblock_region_end_pfn(reg);
P
Paul Mundt 已提交
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
		__add_active_range(0, start_pfn, end_pfn);
	}

	/* All of system RAM sits in node 0 for the non-NUMA case */
	allocate_pgdat(0);
	node_set_online(0);

	plat_mem_setup();

	for_each_online_node(i)
		bootmem_init_one_node(i);

	sparse_init();
}

static void __init early_reserve_mem(void)
{
	unsigned long start_pfn;

	/*
	 * Partially used pages are not usable - thus
	 * we are rounding upwards:
	 */
	start_pfn = PFN_UP(__pa(_end));

	/*
	 * Reserve the kernel text and Reserve the bootmem bitmap. We do
	 * this in two steps (first step was init_bootmem()), because
	 * this catches the (definitely buggy) case of us accidentally
	 * initializing the bootmem allocator with an invalid RAM area.
	 */
Y
Yinghai Lu 已提交
280
	memblock_reserve(__MEMORY_START + CONFIG_ZERO_PAGE_OFFSET,
P
Paul Mundt 已提交
281 282 283 284 285 286 287
		    (PFN_PHYS(start_pfn) + PAGE_SIZE - 1) -
		    (__MEMORY_START + CONFIG_ZERO_PAGE_OFFSET));

	/*
	 * Reserve physical pages below CONFIG_ZERO_PAGE_OFFSET.
	 */
	if (CONFIG_ZERO_PAGE_OFFSET != 0)
Y
Yinghai Lu 已提交
288
		memblock_reserve(__MEMORY_START, CONFIG_ZERO_PAGE_OFFSET);
P
Paul Mundt 已提交
289 290 291 292 293 294 295 296

	/*
	 * Handle additional early reservations
	 */
	check_for_initrd();
	reserve_crashkernel();
}

L
Linus Torvalds 已提交
297 298
void __init paging_init(void)
{
299
	unsigned long max_zone_pfns[MAX_NR_ZONES];
300
	unsigned long vaddr, end;
301
	int nid;
L
Linus Torvalds 已提交
302

Y
Yinghai Lu 已提交
303
	memblock_init();
P
Paul Mundt 已提交
304 305 306 307 308

	sh_mv.mv_mem_init();

	early_reserve_mem();

Y
Yinghai Lu 已提交
309 310
	memblock_enforce_memory_limit(memory_limit);
	memblock_analyze();
P
Paul Mundt 已提交
311

Y
Yinghai Lu 已提交
312
	memblock_dump_all();
P
Paul Mundt 已提交
313 314 315 316

	/*
	 * Determine low and high memory ranges:
	 */
Y
Yinghai Lu 已提交
317
	max_low_pfn = max_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
P
Paul Mundt 已提交
318 319 320 321 322
	min_low_pfn = __MEMORY_START >> PAGE_SHIFT;

	nodes_clear(node_online_map);

	memory_start = (unsigned long)__va(__MEMORY_START);
Y
Yinghai Lu 已提交
323
	memory_end = memory_start + (memory_limit ?: memblock_phys_mem_size());
P
Paul Mundt 已提交
324 325 326 327 328 329

	uncached_init();
	pmb_init();
	do_init_bootmem();
	ioremap_fixed_init();

330 331 332 333
	/* We don't need to map the kernel through the TLB, as
	 * it is permanatly mapped using P1. So clear the
	 * entire pgd. */
	memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
L
Linus Torvalds 已提交
334

335 336 337 338
	/* Set an initial value for the MMU.TTB so we don't have to
	 * check for a null value. */
	set_TTB(swapper_pg_dir);

339 340
	/*
	 * Populate the relevant portions of swapper_pg_dir so that
341
	 * we can use the fixmap entries without calling kmalloc.
342 343 344
	 * pte's will be filled in by __set_fixmap().
	 */
	vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
345 346
	end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
	page_table_range_init(vaddr, end, swapper_pg_dir);
347 348

	kmap_coherent_init();
349

350 351
	memset(max_zone_pfns, 0, sizeof(max_zone_pfns));

352 353 354 355
	for_each_online_node(nid) {
		pg_data_t *pgdat = NODE_DATA(nid);
		unsigned long low, start_pfn;

356
		start_pfn = pgdat->bdata->node_min_pfn;
357 358
		low = pgdat->bdata->node_low_pfn;

359 360
		if (max_zone_pfns[ZONE_NORMAL] < low)
			max_zone_pfns[ZONE_NORMAL] = low;
361 362 363 364

		printk("Node %u: start_pfn = 0x%lx, low = 0x%lx\n",
		       nid, start_pfn, low);
	}
365 366

	free_area_init_nodes(max_zone_pfns);
L
Linus Torvalds 已提交
367 368
}

369 370 371 372 373 374 375 376
/*
 * Early initialization for any I/O MMUs we might have.
 */
static void __init iommu_init(void)
{
	no_iommu_init();
}

377 378
unsigned int mem_init_done = 0;

L
Linus Torvalds 已提交
379 380
void __init mem_init(void)
{
P
Paul Mundt 已提交
381
	int codesize, datasize, initsize;
382
	int nid;
L
Linus Torvalds 已提交
383

384 385
	iommu_init();

386 387 388
	num_physpages = 0;
	high_memory = NULL;

389 390 391 392 393 394 395 396 397 398 399
	for_each_online_node(nid) {
		pg_data_t *pgdat = NODE_DATA(nid);
		unsigned long node_pages = 0;
		void *node_high_memory;

		num_physpages += pgdat->node_present_pages;

		if (pgdat->node_spanned_pages)
			node_pages = free_all_bootmem_node(pgdat);

		totalram_pages += node_pages;
L
Linus Torvalds 已提交
400

401 402 403
		node_high_memory = (void *)__va((pgdat->node_start_pfn +
						 pgdat->node_spanned_pages) <<
						 PAGE_SHIFT);
404 405 406
		if (node_high_memory > high_memory)
			high_memory = node_high_memory;
	}
L
Linus Torvalds 已提交
407

408 409 410
	/* Set this up early, so we can take care of the zero page */
	cpu_cache_init();

L
Linus Torvalds 已提交
411 412 413 414
	/* clear the zero-page */
	memset(empty_zero_page, 0, PAGE_SIZE);
	__flush_wback_region(empty_zero_page, PAGE_SIZE);

415 416
	vsyscall_init();

L
Linus Torvalds 已提交
417 418 419 420
	codesize =  (unsigned long) &_etext - (unsigned long) &_text;
	datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
	initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;

P
Paul Mundt 已提交
421
	printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
P
Paul Mundt 已提交
422
	       "%dk data, %dk init)\n",
423
		nr_free_pages() << (PAGE_SHIFT-10),
424
		num_physpages << (PAGE_SHIFT-10),
L
Linus Torvalds 已提交
425 426 427 428
		codesize >> 10,
		datasize >> 10,
		initsize >> 10);

429 430 431 432 433 434
	printk(KERN_INFO "virtual kernel memory layout:\n"
		"    fixmap  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
#ifdef CONFIG_HIGHMEM
		"    pkmap   : 0x%08lx - 0x%08lx   (%4ld kB)\n"
#endif
		"    vmalloc : 0x%08lx - 0x%08lx   (%4ld MB)\n"
435
		"    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB) (cached)\n"
436
#ifdef CONFIG_UNCACHED_MAPPING
437
		"            : 0x%08lx - 0x%08lx   (%4ld MB) (uncached)\n"
438
#endif
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
		"      .init : 0x%08lx - 0x%08lx   (%4ld kB)\n"
		"      .data : 0x%08lx - 0x%08lx   (%4ld kB)\n"
		"      .text : 0x%08lx - 0x%08lx   (%4ld kB)\n",
		FIXADDR_START, FIXADDR_TOP,
		(FIXADDR_TOP - FIXADDR_START) >> 10,

#ifdef CONFIG_HIGHMEM
		PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
		(LAST_PKMAP*PAGE_SIZE) >> 10,
#endif

		(unsigned long)VMALLOC_START, VMALLOC_END,
		(VMALLOC_END - VMALLOC_START) >> 20,

		(unsigned long)memory_start, (unsigned long)high_memory,
		((unsigned long)high_memory - (unsigned long)memory_start) >> 20,

456
#ifdef CONFIG_UNCACHED_MAPPING
P
Paul Mundt 已提交
457
		uncached_start, uncached_end, uncached_size >> 20,
458
#endif
459

460 461 462 463 464 465 466 467 468
		(unsigned long)&__init_begin, (unsigned long)&__init_end,
		((unsigned long)&__init_end -
		 (unsigned long)&__init_begin) >> 10,

		(unsigned long)&_etext, (unsigned long)&_edata,
		((unsigned long)&_edata - (unsigned long)&_etext) >> 10,

		(unsigned long)&_text, (unsigned long)&_etext,
		((unsigned long)&_etext - (unsigned long)&_text) >> 10);
469 470

	mem_init_done = 1;
L
Linus Torvalds 已提交
471 472 473 474 475
}

void free_initmem(void)
{
	unsigned long addr;
476

L
Linus Torvalds 已提交
477 478 479
	addr = (unsigned long)(&__init_begin);
	for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
		ClearPageReserved(virt_to_page(addr));
480
		init_page_count(virt_to_page(addr));
L
Linus Torvalds 已提交
481 482 483
		free_page(addr);
		totalram_pages++;
	}
484 485 486
	printk("Freeing unused kernel memory: %ldk freed\n",
	       ((unsigned long)&__init_end -
	        (unsigned long)&__init_begin) >> 10);
L
Linus Torvalds 已提交
487 488 489 490 491 492 493 494
}

#ifdef CONFIG_BLK_DEV_INITRD
void free_initrd_mem(unsigned long start, unsigned long end)
{
	unsigned long p;
	for (p = start; p < end; p += PAGE_SIZE) {
		ClearPageReserved(virt_to_page(p));
495
		init_page_count(virt_to_page(p));
L
Linus Torvalds 已提交
496 497 498
		free_page(p);
		totalram_pages++;
	}
499
	printk("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
L
Linus Torvalds 已提交
500 501
}
#endif
502 503 504 505 506 507 508 509 510 511 512 513

#ifdef CONFIG_MEMORY_HOTPLUG
int arch_add_memory(int nid, u64 start, u64 size)
{
	pg_data_t *pgdat;
	unsigned long start_pfn = start >> PAGE_SHIFT;
	unsigned long nr_pages = size >> PAGE_SHIFT;
	int ret;

	pgdat = NODE_DATA(nid);

	/* We only have ZONE_NORMAL, so this is easy.. */
514 515
	ret = __add_pages(nid, pgdat->node_zones + ZONE_NORMAL,
				start_pfn, nr_pages);
516
	if (unlikely(ret))
517
		printk("%s: Failed, __add_pages() == %d\n", __func__, ret);
518 519 520 521 522

	return ret;
}
EXPORT_SYMBOL_GPL(arch_add_memory);

523
#ifdef CONFIG_NUMA
524 525 526 527 528 529 530
int memory_add_physaddr_to_nid(u64 addr)
{
	/* Node 0 for now.. */
	return 0;
}
EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
#endif
M
Matt Fleming 已提交
531

532
#endif /* CONFIG_MEMORY_HOTPLUG */