init.c 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/bootmem.h>
#include <linux/memblock.h>
N
Noam Camus 已提交
13 14 15
#ifdef CONFIG_BLK_DEV_INITRD
#include <linux/initrd.h>
#endif
16 17
#include <linux/swap.h>
#include <linux/module.h>
18
#include <linux/highmem.h>
19 20 21 22 23 24 25 26 27
#include <asm/page.h>
#include <asm/pgalloc.h>
#include <asm/sections.h>
#include <asm/arcregs.h>

pgd_t swapper_pg_dir[PTRS_PER_PGD] __aligned(PAGE_SIZE);
char empty_zero_page[PAGE_SIZE] __aligned(PAGE_SIZE);
EXPORT_SYMBOL(empty_zero_page);

28 29
static const unsigned long low_mem_start = CONFIG_LINUX_LINK_BASE;
static unsigned long low_mem_sz;
30

31 32 33 34 35 36
#ifdef CONFIG_HIGHMEM
static unsigned long min_high_pfn;
static u64 high_mem_start;
static u64 high_mem_sz;
#endif

37 38 39
/* User can over-ride above with "mem=nnn[KkMm]" in cmdline */
static int __init setup_mem_sz(char *str)
{
40
	low_mem_sz = memparse(str, NULL) & PAGE_MASK;
41 42

	/* early console might not be setup yet - it will show up later */
43
	pr_info("\"mem=%s\": mem sz set to %ldM\n", str, TO_MB(low_mem_sz));
44 45 46 47 48

	return 0;
}
early_param("mem", setup_mem_sz);

V
Vineet Gupta 已提交
49 50
void __init early_init_dt_add_memory_arch(u64 base, u64 size)
{
51 52 53 54 55 56 57 58 59 60 61 62 63
	int in_use = 0;

	if (!low_mem_sz) {
		BUG_ON(base != low_mem_start);
		low_mem_sz = size;
		in_use = 1;
	} else {
#ifdef CONFIG_HIGHMEM
		high_mem_start = base;
		high_mem_sz = size;
		in_use = 1;
#endif
	}
64

65 66
	pr_info("Memory @ %llx [%lldM] %s\n",
		base, TO_MB(size), !in_use ? "Not used":"");
V
Vineet Gupta 已提交
67 68
}

N
Noam Camus 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#ifdef CONFIG_BLK_DEV_INITRD
static int __init early_initrd(char *p)
{
	unsigned long start, size;
	char *endp;

	start = memparse(p, &endp);
	if (*endp == ',') {
		size = memparse(endp + 1, NULL);

		initrd_start = (unsigned long)__va(start);
		initrd_end = (unsigned long)__va(start + size);
	}
	return 0;
}
early_param("initrd", early_initrd);
#endif

87 88 89 90 91 92 93 94
/*
 * First memory setup routine called from setup_arch()
 * 1. setup swapper's mm @init_mm
 * 2. Count the pages we have and setup bootmem allocator
 * 3. zone setup
 */
void __init setup_arch_memory(void)
{
95
	unsigned long zones_size[MAX_NR_ZONES];
96
	unsigned long zones_holes[MAX_NR_ZONES];
97 98 99 100 101 102 103

	init_mm.start_code = (unsigned long)_text;
	init_mm.end_code = (unsigned long)_etext;
	init_mm.end_data = (unsigned long)_edata;
	init_mm.brk = (unsigned long)_end;

	/* first page of system - kernel .vector starts here */
104
	min_low_pfn = ARCH_PFN_OFFSET;
105

106 107
	/* Last usable page of low mem */
	max_low_pfn = max_pfn = PFN_DOWN(low_mem_start + low_mem_sz);
108

109 110 111 112 113 114
#ifdef CONFIG_HIGHMEM
	min_high_pfn = PFN_DOWN(high_mem_start);
	max_pfn = PFN_DOWN(high_mem_start + high_mem_sz);
#endif

	max_mapnr = max_pfn - min_low_pfn;
115

116
	/*------------- bootmem allocator setup -----------------------*/
117 118 119 120 121 122 123 124 125 126 127 128

	/*
	 * seed the bootmem allocator after any DT memory node parsing or
	 * "mem=xxx" cmdline overrides have potentially updated @arc_mem_sz
	 *
	 * Only low mem is added, otherwise we have crashes when allocating
	 * mem_map[] itself. NO_BOOTMEM allocates mem_map[] at the end of
	 * avail memory, ending in highmem with a > 32-bit address. However
	 * it then tries to memset it with a truncaed 32-bit handle, causing
	 * the crash
	 */

129 130
	memblock_add(low_mem_start, low_mem_sz);
	memblock_reserve(low_mem_start, __pa(_end) - low_mem_start);
131

N
Noam Camus 已提交
132 133 134 135 136
#ifdef CONFIG_BLK_DEV_INITRD
	if (initrd_start)
		memblock_reserve(__pa(initrd_start), initrd_end - initrd_start);
#endif

137 138
	memblock_dump_all();

139
	/*----------------- node/zones setup --------------------------*/
140
	memset(zones_size, 0, sizeof(zones_size));
141 142
	memset(zones_holes, 0, sizeof(zones_holes));

143
	zones_size[ZONE_NORMAL] = max_low_pfn - min_low_pfn;
144 145 146 147 148 149 150 151
	zones_holes[ZONE_NORMAL] = 0;

#ifdef CONFIG_HIGHMEM
	zones_size[ZONE_HIGHMEM] = max_pfn - max_low_pfn;

	/* This handles the peripheral address space hole */
	zones_holes[ZONE_HIGHMEM] = min_high_pfn - max_low_pfn;
#endif
152 153 154 155 156 157 158 159 160 161

	/*
	 * We can't use the helper free_area_init(zones[]) because it uses
	 * PAGE_OFFSET to compute the @min_low_pfn which would be wrong
	 * when our kernel doesn't start at PAGE_OFFSET, i.e.
	 * PAGE_OFFSET != CONFIG_LINUX_LINK_BASE
	 */
	free_area_init_node(0,			/* node-id */
			    zones_size,		/* num pages per zone */
			    min_low_pfn,	/* first pfn of node */
162
			    zones_holes);	/* holes */
163

164 165 166 167
#ifdef CONFIG_HIGHMEM
	high_memory = (void *)(min_high_pfn << PAGE_SHIFT);
	kmap_init();
#endif
168 169 170 171 172 173 174 175 176 177
}

/*
 * mem_init - initializes memory
 *
 * Frees up bootmem
 * Calculates and displays memory available/used
 */
void __init mem_init(void)
{
178 179 180 181 182 183 184 185
#ifdef CONFIG_HIGHMEM
	unsigned long tmp;

	reset_all_zones_managed_pages();
	for (tmp = min_high_pfn; tmp < max_pfn; tmp++)
		free_highmem_page(pfn_to_page(tmp));
#endif

186
	free_all_bootmem();
187
	mem_init_print_info(NULL);
188 189 190 191 192 193 194
}

/*
 * free_initmem: Free all the __init memory.
 */
void __init_refok free_initmem(void)
{
195
	free_initmem_default(-1);
196 197 198 199 200
}

#ifdef CONFIG_BLK_DEV_INITRD
void __init free_initrd_mem(unsigned long start, unsigned long end)
{
201
	free_reserved_area((void *)start, (void *)end, -1, "initrd");
202 203
}
#endif