xref: /openbmc/linux/arch/arc/mm/init.c (revision 176f011b)
1 /*
2  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/mm.h>
11 #include <linux/memblock.h>
12 #ifdef CONFIG_BLK_DEV_INITRD
13 #include <linux/initrd.h>
14 #endif
15 #include <linux/of_fdt.h>
16 #include <linux/swap.h>
17 #include <linux/module.h>
18 #include <linux/highmem.h>
19 #include <asm/page.h>
20 #include <asm/pgalloc.h>
21 #include <asm/sections.h>
22 #include <asm/arcregs.h>
23 
24 pgd_t swapper_pg_dir[PTRS_PER_PGD] __aligned(PAGE_SIZE);
25 char empty_zero_page[PAGE_SIZE] __aligned(PAGE_SIZE);
26 EXPORT_SYMBOL(empty_zero_page);
27 
28 static const unsigned long low_mem_start = CONFIG_LINUX_RAM_BASE;
29 static unsigned long low_mem_sz;
30 
31 #ifdef CONFIG_HIGHMEM
32 static unsigned long min_high_pfn, max_high_pfn;
33 static u64 high_mem_start;
34 static u64 high_mem_sz;
35 #endif
36 
37 #ifdef CONFIG_DISCONTIGMEM
38 struct pglist_data node_data[MAX_NUMNODES] __read_mostly;
39 EXPORT_SYMBOL(node_data);
40 #endif
41 
42 long __init arc_get_mem_sz(void)
43 {
44 	return low_mem_sz;
45 }
46 
47 /* User can over-ride above with "mem=nnn[KkMm]" in cmdline */
48 static int __init setup_mem_sz(char *str)
49 {
50 	low_mem_sz = memparse(str, NULL) & PAGE_MASK;
51 
52 	/* early console might not be setup yet - it will show up later */
53 	pr_info("\"mem=%s\": mem sz set to %ldM\n", str, TO_MB(low_mem_sz));
54 
55 	return 0;
56 }
57 early_param("mem", setup_mem_sz);
58 
59 void __init early_init_dt_add_memory_arch(u64 base, u64 size)
60 {
61 	int in_use = 0;
62 
63 	if (!low_mem_sz) {
64 		if (base != low_mem_start)
65 			panic("CONFIG_LINUX_RAM_BASE != DT memory { }");
66 
67 		low_mem_sz = size;
68 		in_use = 1;
69 	} else {
70 #ifdef CONFIG_HIGHMEM
71 		high_mem_start = base;
72 		high_mem_sz = size;
73 		in_use = 1;
74 #endif
75 	}
76 
77 	pr_info("Memory @ %llx [%lldM] %s\n",
78 		base, TO_MB(size), !in_use ? "Not used":"");
79 }
80 
81 /*
82  * First memory setup routine called from setup_arch()
83  * 1. setup swapper's mm @init_mm
84  * 2. Count the pages we have and setup bootmem allocator
85  * 3. zone setup
86  */
87 void __init setup_arch_memory(void)
88 {
89 	unsigned long zones_size[MAX_NR_ZONES];
90 	unsigned long zones_holes[MAX_NR_ZONES];
91 
92 	init_mm.start_code = (unsigned long)_text;
93 	init_mm.end_code = (unsigned long)_etext;
94 	init_mm.end_data = (unsigned long)_edata;
95 	init_mm.brk = (unsigned long)_end;
96 
97 	/* first page of system - kernel .vector starts here */
98 	min_low_pfn = ARCH_PFN_OFFSET;
99 
100 	/* Last usable page of low mem */
101 	max_low_pfn = max_pfn = PFN_DOWN(low_mem_start + low_mem_sz);
102 
103 #ifdef CONFIG_FLATMEM
104 	/* pfn_valid() uses this */
105 	max_mapnr = max_low_pfn - min_low_pfn;
106 #endif
107 
108 	/*------------- bootmem allocator setup -----------------------*/
109 
110 	/*
111 	 * seed the bootmem allocator after any DT memory node parsing or
112 	 * "mem=xxx" cmdline overrides have potentially updated @arc_mem_sz
113 	 *
114 	 * Only low mem is added, otherwise we have crashes when allocating
115 	 * mem_map[] itself. NO_BOOTMEM allocates mem_map[] at the end of
116 	 * avail memory, ending in highmem with a > 32-bit address. However
117 	 * it then tries to memset it with a truncaed 32-bit handle, causing
118 	 * the crash
119 	 */
120 
121 	memblock_add_node(low_mem_start, low_mem_sz, 0);
122 	memblock_reserve(low_mem_start, __pa(_end) - low_mem_start);
123 
124 #ifdef CONFIG_BLK_DEV_INITRD
125 	if (phys_initrd_size) {
126 		memblock_reserve(phys_initrd_start, phys_initrd_size);
127 		initrd_start = (unsigned long)__va(phys_initrd_start);
128 		initrd_end = initrd_start + phys_initrd_size;
129 	}
130 #endif
131 
132 	early_init_fdt_reserve_self();
133 	early_init_fdt_scan_reserved_mem();
134 
135 	memblock_dump_all();
136 
137 	/*----------------- node/zones setup --------------------------*/
138 	memset(zones_size, 0, sizeof(zones_size));
139 	memset(zones_holes, 0, sizeof(zones_holes));
140 
141 	zones_size[ZONE_NORMAL] = max_low_pfn - min_low_pfn;
142 	zones_holes[ZONE_NORMAL] = 0;
143 
144 	/*
145 	 * We can't use the helper free_area_init(zones[]) because it uses
146 	 * PAGE_OFFSET to compute the @min_low_pfn which would be wrong
147 	 * when our kernel doesn't start at PAGE_OFFSET, i.e.
148 	 * PAGE_OFFSET != CONFIG_LINUX_RAM_BASE
149 	 */
150 	free_area_init_node(0,			/* node-id */
151 			    zones_size,		/* num pages per zone */
152 			    min_low_pfn,	/* first pfn of node */
153 			    zones_holes);	/* holes */
154 
155 #ifdef CONFIG_HIGHMEM
156 	/*
157 	 * Populate a new node with highmem
158 	 *
159 	 * On ARC (w/o PAE) HIGHMEM addresses are actually smaller (0 based)
160 	 * than addresses in normal ala low memory (0x8000_0000 based).
161 	 * Even with PAE, the huge peripheral space hole would waste a lot of
162 	 * mem with single mem_map[]. This warrants a mem_map per region design.
163 	 * Thus HIGHMEM on ARC is imlemented with DISCONTIGMEM.
164 	 *
165 	 * DISCONTIGMEM in turns requires multiple nodes. node 0 above is
166 	 * populated with normal memory zone while node 1 only has highmem
167 	 */
168 	node_set_online(1);
169 
170 	min_high_pfn = PFN_DOWN(high_mem_start);
171 	max_high_pfn = PFN_DOWN(high_mem_start + high_mem_sz);
172 
173 	zones_size[ZONE_NORMAL] = 0;
174 	zones_holes[ZONE_NORMAL] = 0;
175 
176 	zones_size[ZONE_HIGHMEM] = max_high_pfn - min_high_pfn;
177 	zones_holes[ZONE_HIGHMEM] = 0;
178 
179 	free_area_init_node(1,			/* node-id */
180 			    zones_size,		/* num pages per zone */
181 			    min_high_pfn,	/* first pfn of node */
182 			    zones_holes);	/* holes */
183 
184 	high_memory = (void *)(min_high_pfn << PAGE_SHIFT);
185 	kmap_init();
186 #endif
187 }
188 
189 /*
190  * mem_init - initializes memory
191  *
192  * Frees up bootmem
193  * Calculates and displays memory available/used
194  */
195 void __init mem_init(void)
196 {
197 #ifdef CONFIG_HIGHMEM
198 	unsigned long tmp;
199 
200 	reset_all_zones_managed_pages();
201 	for (tmp = min_high_pfn; tmp < max_high_pfn; tmp++)
202 		free_highmem_page(pfn_to_page(tmp));
203 #endif
204 
205 	memblock_free_all();
206 	mem_init_print_info(NULL);
207 }
208 
209 /*
210  * free_initmem: Free all the __init memory.
211  */
212 void __ref free_initmem(void)
213 {
214 	free_initmem_default(-1);
215 }
216 
217 #ifdef CONFIG_BLK_DEV_INITRD
218 void __init free_initrd_mem(unsigned long start, unsigned long end)
219 {
220 	free_reserved_area((void *)start, (void *)end, -1, "initrd");
221 }
222 #endif
223