init.c 10.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 - 2011  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>
M
Mike Rapoport 已提交
14
#include <linux/memblock.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>
19
#include <linux/dma-mapping.h>
20
#include <linux/export.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

pgd_t swapper_pg_dir[PTRS_PER_PGD];
32

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

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

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

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

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

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

68
	return pte_offset_kernel(pmd, addr);
69 70 71 72 73 74 75
}

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 已提交
76 77 78 79 80 81
	if (!pte_none(*pte)) {
		pte_ERROR(*pte);
		return;
	}

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

	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 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111
}

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);
}
112

113 114 115 116 117 118 119 120 121 122 123 124
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);
}

125 126 127 128 129
static pmd_t * __init one_md_table_init(pud_t *pud)
{
	if (pud_none(*pud)) {
		pmd_t *pmd;

130
		pmd = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
131 132 133
		if (!pmd)
			panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
			      __func__, PAGE_SIZE, PAGE_SIZE);
134 135 136 137 138 139 140 141 142 143 144 145
		pud_populate(&init_mm, pud, pmd);
		BUG_ON(pmd != pmd_offset(pud, 0));
	}

	return pmd_offset(pud, 0);
}

static pte_t * __init one_page_table_init(pmd_t *pmd)
{
	if (pmd_none(*pmd)) {
		pte_t *pte;

146
		pte = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
147 148 149
		if (!pte)
			panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
			      __func__, PAGE_SIZE, PAGE_SIZE);
150 151 152 153 154 155 156 157 158 159 160 161 162
		pmd_populate_kernel(&init_mm, pmd, pte);
		BUG_ON(pte != pte_offset_kernel(pmd, 0));
	}

	return pte_offset_kernel(pmd, 0);
}

static pte_t * __init page_table_kmap_check(pte_t *pte, pmd_t *pmd,
					    unsigned long vaddr, pte_t *lastpte)
{
	return pte;
}

163 164 165 166 167 168
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;
169
	pte_t *pte = NULL;
170
	int i, j, k;
171 172
	unsigned long vaddr;

173 174 175 176 177 178 179 180 181
	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++) {
182 183
			pmd = one_md_table_init(pud);
#ifndef __PAGETABLE_PMD_FOLDED
184 185
			pmd += k;
#endif
186
			for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) {
187 188
				pte = page_table_kmap_check(one_page_table_init(pmd),
							    pmd, vaddr, pte);
189 190 191
				vaddr += PMD_SIZE;
			}
			k = 0;
192
		}
193
		j = 0;
194 195
	}
}
196
#endif	/* CONFIG_MMU */
L
Linus Torvalds 已提交
197

P
Paul Mundt 已提交
198 199 200 201 202 203 204
void __init allocate_pgdat(unsigned int nid)
{
	unsigned long start_pfn, end_pfn;

	get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);

#ifdef CONFIG_NEED_MULTIPLE_NODES
205
	NODE_DATA(nid) = memblock_alloc_try_nid(
206 207 208 209
				sizeof(struct pglist_data),
				SMP_CACHE_BYTES, MEMBLOCK_LOW_LIMIT,
				MEMBLOCK_ALLOC_ACCESSIBLE, nid);
	if (!NODE_DATA(nid))
P
Paul Mundt 已提交
210 211 212 213 214 215 216 217 218
		panic("Can't allocate pgdat for node %d\n", nid);
#endif

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

static void __init do_init_bootmem(void)
{
219
	struct memblock_region *reg;
P
Paul Mundt 已提交
220 221

	/* Add active regions with valid PFNs. */
222
	for_each_memblock(memory, reg) {
P
Paul Mundt 已提交
223
		unsigned long start_pfn, end_pfn;
224 225
		start_pfn = memblock_region_memory_base_pfn(reg);
		end_pfn = memblock_region_memory_end_pfn(reg);
P
Paul Mundt 已提交
226 227 228 229 230 231 232 233 234
		__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();

R
Rob Herring 已提交
235 236
	for_each_memblock(memory, reg) {
		int nid = memblock_get_region_node(reg);
P
Paul Mundt 已提交
237

R
Rob Herring 已提交
238 239 240
		memory_present(nid, memblock_region_memory_base_pfn(reg),
			memblock_region_memory_end_pfn(reg));
	}
P
Paul Mundt 已提交
241 242 243 244 245 246
	sparse_init();
}

static void __init early_reserve_mem(void)
{
	unsigned long start_pfn;
S
Simon Horman 已提交
247 248
	u32 zero_base = (u32)__MEMORY_START + (u32)PHYSICAL_OFFSET;
	u32 start = zero_base + (u32)CONFIG_ZERO_PAGE_OFFSET;
P
Paul Mundt 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261

	/*
	 * 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.
	 */
S
Simon Horman 已提交
262
	memblock_reserve(start, (PFN_PHYS(start_pfn) + PAGE_SIZE - 1) - start);
P
Paul Mundt 已提交
263 264 265 266 267

	/*
	 * Reserve physical pages below CONFIG_ZERO_PAGE_OFFSET.
	 */
	if (CONFIG_ZERO_PAGE_OFFSET != 0)
S
Simon Horman 已提交
268
		memblock_reserve(zero_base, CONFIG_ZERO_PAGE_OFFSET);
P
Paul Mundt 已提交
269 270 271 272 273 274 275 276

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

L
Linus Torvalds 已提交
277 278
void __init paging_init(void)
{
279
	unsigned long max_zone_pfns[MAX_NR_ZONES];
280
	unsigned long vaddr, end;
L
Linus Torvalds 已提交
281

P
Paul Mundt 已提交
282 283 284 285
	sh_mv.mv_mem_init();

	early_reserve_mem();

286 287 288 289 290 291 292
	/*
	 * Once the early reservations are out of the way, give the
	 * platforms a chance to kick out some memory.
	 */
	if (sh_mv.mv_mem_reserve)
		sh_mv.mv_mem_reserve();

Y
Yinghai Lu 已提交
293
	memblock_enforce_memory_limit(memory_limit);
294
	memblock_allow_resize();
P
Paul Mundt 已提交
295

Y
Yinghai Lu 已提交
296
	memblock_dump_all();
P
Paul Mundt 已提交
297 298 299 300

	/*
	 * Determine low and high memory ranges:
	 */
Y
Yinghai Lu 已提交
301
	max_low_pfn = max_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
P
Paul Mundt 已提交
302 303 304 305 306
	min_low_pfn = __MEMORY_START >> PAGE_SHIFT;

	nodes_clear(node_online_map);

	memory_start = (unsigned long)__va(__MEMORY_START);
Y
Yinghai Lu 已提交
307
	memory_end = memory_start + (memory_limit ?: memblock_phys_mem_size());
P
Paul Mundt 已提交
308 309 310 311 312 313

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

314 315 316 317
	/* 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 已提交
318

319 320 321 322
	/* Set an initial value for the MMU.TTB so we don't have to
	 * check for a null value. */
	set_TTB(swapper_pg_dir);

323 324
	/*
	 * Populate the relevant portions of swapper_pg_dir so that
325
	 * we can use the fixmap entries without calling kmalloc.
326 327 328
	 * pte's will be filled in by __set_fixmap().
	 */
	vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
329 330
	end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
	page_table_range_init(vaddr, end, swapper_pg_dir);
331 332

	kmap_coherent_init();
333

334
	memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
R
Rob Herring 已提交
335
	max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
336
	free_area_init_nodes(max_zone_pfns);
L
Linus Torvalds 已提交
337 338
}

339 340
unsigned int mem_init_done = 0;

L
Linus Torvalds 已提交
341 342
void __init mem_init(void)
{
343
	pg_data_t *pgdat;
L
Linus Torvalds 已提交
344

345
	high_memory = NULL;
346 347 348
	for_each_online_pgdat(pgdat)
		high_memory = max_t(void *, high_memory,
				    __va(pgdat_end_pfn(pgdat) << PAGE_SHIFT));
349

350
	memblock_free_all();
L
Linus Torvalds 已提交
351

352 353 354
	/* Set this up early, so we can take care of the zero page */
	cpu_cache_init();

L
Linus Torvalds 已提交
355 356 357 358
	/* clear the zero-page */
	memset(empty_zero_page, 0, PAGE_SIZE);
	__flush_wback_region(empty_zero_page, PAGE_SIZE);

359 360
	vsyscall_init();

361 362
	mem_init_print_info(NULL);
	pr_info("virtual kernel memory layout:\n"
363 364 365 366 367
		"    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"
368
		"    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB) (cached)\n"
369
#ifdef CONFIG_UNCACHED_MAPPING
370
		"            : 0x%08lx - 0x%08lx   (%4ld MB) (uncached)\n"
371
#endif
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
		"      .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,

389
#ifdef CONFIG_UNCACHED_MAPPING
P
Paul Mundt 已提交
390
		uncached_start, uncached_end, uncached_size >> 20,
391
#endif
392

393 394 395 396 397 398 399 400 401
		(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);
402 403

	mem_init_done = 1;
L
Linus Torvalds 已提交
404 405
}

406
#ifdef CONFIG_MEMORY_HOTPLUG
407 408
int arch_add_memory(int nid, u64 start, u64 size,
			struct mhp_restrictions *restrictions)
409
{
A
Alexander Kuleshov 已提交
410
	unsigned long start_pfn = PFN_DOWN(start);
411 412 413 414
	unsigned long nr_pages = size >> PAGE_SHIFT;
	int ret;

	/* We only have ZONE_NORMAL, so this is easy.. */
415
	ret = __add_pages(nid, start_pfn, nr_pages, restrictions);
416
	if (unlikely(ret))
417
		printk("%s: Failed, __add_pages() == %d\n", __func__, ret);
418 419 420 421

	return ret;
}

422
#ifdef CONFIG_NUMA
423 424 425 426 427 428 429
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 已提交
430

431
#ifdef CONFIG_MEMORY_HOTREMOVE
432
int arch_remove_memory(int nid, u64 start, u64 size, struct vmem_altmap *altmap)
433
{
A
Alexander Kuleshov 已提交
434
	unsigned long start_pfn = PFN_DOWN(start);
435 436 437 438 439
	unsigned long nr_pages = size >> PAGE_SHIFT;
	struct zone *zone;
	int ret;

	zone = page_zone(pfn_to_page(start_pfn));
440
	ret = __remove_pages(zone, start_pfn, nr_pages, altmap);
441 442 443 444 445 446 447
	if (unlikely(ret))
		pr_warn("%s: Failed, __remove_pages() == %d\n", __func__,
			ret);

	return ret;
}
#endif
448
#endif /* CONFIG_MEMORY_HOTPLUG */