xref: /openbmc/linux/arch/microblaze/mm/init.c (revision 7490ca1e)
1 /*
2  * Copyright (C) 2007-2008 Michal Simek <monstr@monstr.eu>
3  * Copyright (C) 2006 Atmark Techno, Inc.
4  *
5  * This file is subject to the terms and conditions of the GNU General Public
6  * License. See the file "COPYING" in the main directory of this archive
7  * for more details.
8  */
9 
10 #include <linux/bootmem.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/memblock.h>
14 #include <linux/mm.h> /* mem_init */
15 #include <linux/initrd.h>
16 #include <linux/pagemap.h>
17 #include <linux/pfn.h>
18 #include <linux/slab.h>
19 #include <linux/swap.h>
20 #include <linux/export.h>
21 
22 #include <asm/page.h>
23 #include <asm/mmu_context.h>
24 #include <asm/pgalloc.h>
25 #include <asm/sections.h>
26 #include <asm/tlb.h>
27 
28 /* Use for MMU and noMMU because of PCI generic code */
29 int mem_init_done;
30 
31 #ifndef CONFIG_MMU
32 unsigned int __page_offset;
33 EXPORT_SYMBOL(__page_offset);
34 
35 #else
36 static int init_bootmem_done;
37 #endif /* CONFIG_MMU */
38 
39 char *klimit = _end;
40 
41 /*
42  * Initialize the bootmem system and give it all the memory we
43  * have available.
44  */
45 unsigned long memory_start;
46 EXPORT_SYMBOL(memory_start);
47 unsigned long memory_end; /* due to mm/nommu.c */
48 unsigned long memory_size;
49 EXPORT_SYMBOL(memory_size);
50 
51 /*
52  * paging_init() sets up the page tables - in fact we've already done this.
53  */
54 static void __init paging_init(void)
55 {
56 	unsigned long zones_size[MAX_NR_ZONES];
57 
58 	/* Clean every zones */
59 	memset(zones_size, 0, sizeof(zones_size));
60 
61 	/*
62 	 * old: we can DMA to/from any address.put all page into ZONE_DMA
63 	 * We use only ZONE_NORMAL
64 	 */
65 	zones_size[ZONE_NORMAL] = max_mapnr;
66 
67 	free_area_init(zones_size);
68 }
69 
70 void __init setup_memory(void)
71 {
72 	unsigned long map_size;
73 	struct memblock_region *reg;
74 
75 #ifndef CONFIG_MMU
76 	u32 kernel_align_start, kernel_align_size;
77 
78 	/* Find main memory where is the kernel */
79 	for_each_memblock(memory, reg) {
80 		memory_start = (u32)reg->base;
81 		memory_end = (u32) reg->base + reg->size;
82 		if ((memory_start <= (u32)_text) &&
83 					((u32)_text <= memory_end)) {
84 			memory_size = memory_end - memory_start;
85 			PAGE_OFFSET = memory_start;
86 			printk(KERN_INFO "%s: Main mem: 0x%x-0x%x, "
87 				"size 0x%08x\n", __func__, (u32) memory_start,
88 					(u32) memory_end, (u32) memory_size);
89 			break;
90 		}
91 	}
92 
93 	if (!memory_start || !memory_end) {
94 		panic("%s: Missing memory setting 0x%08x-0x%08x\n",
95 			__func__, (u32) memory_start, (u32) memory_end);
96 	}
97 
98 	/* reservation of region where is the kernel */
99 	kernel_align_start = PAGE_DOWN((u32)_text);
100 	/* ALIGN can be remove because _end in vmlinux.lds.S is align */
101 	kernel_align_size = PAGE_UP((u32)klimit) - kernel_align_start;
102 	memblock_reserve(kernel_align_start, kernel_align_size);
103 	printk(KERN_INFO "%s: kernel addr=0x%08x-0x%08x size=0x%08x\n",
104 		__func__, kernel_align_start, kernel_align_start
105 			+ kernel_align_size, kernel_align_size);
106 
107 #endif
108 	/*
109 	 * Kernel:
110 	 * start: base phys address of kernel - page align
111 	 * end: base phys address of kernel - page align
112 	 *
113 	 * min_low_pfn - the first page (mm/bootmem.c - node_boot_start)
114 	 * max_low_pfn
115 	 * max_mapnr - the first unused page (mm/bootmem.c - node_low_pfn)
116 	 * num_physpages - number of all pages
117 	 */
118 
119 	/* memory start is from the kernel end (aligned) to higher addr */
120 	min_low_pfn = memory_start >> PAGE_SHIFT; /* minimum for allocation */
121 	/* RAM is assumed contiguous */
122 	num_physpages = max_mapnr = memory_size >> PAGE_SHIFT;
123 	max_pfn = max_low_pfn = memory_end >> PAGE_SHIFT;
124 
125 	printk(KERN_INFO "%s: max_mapnr: %#lx\n", __func__, max_mapnr);
126 	printk(KERN_INFO "%s: min_low_pfn: %#lx\n", __func__, min_low_pfn);
127 	printk(KERN_INFO "%s: max_low_pfn: %#lx\n", __func__, max_low_pfn);
128 
129 	/*
130 	 * Find an area to use for the bootmem bitmap.
131 	 * We look for the first area which is at least
132 	 * 128kB in length (128kB is enough for a bitmap
133 	 * for 4GB of memory, using 4kB pages), plus 1 page
134 	 * (in case the address isn't page-aligned).
135 	 */
136 	map_size = init_bootmem_node(NODE_DATA(0),
137 		PFN_UP(TOPHYS((u32)klimit)), min_low_pfn, max_low_pfn);
138 	memblock_reserve(PFN_UP(TOPHYS((u32)klimit)) << PAGE_SHIFT, map_size);
139 
140 	/* free bootmem is whole main memory */
141 	free_bootmem(memory_start, memory_size);
142 
143 	/* reserve allocate blocks */
144 	for_each_memblock(reserved, reg) {
145 		pr_debug("reserved - 0x%08x-0x%08x\n",
146 			 (u32) reg->base, (u32) reg->size);
147 		reserve_bootmem(reg->base, reg->size, BOOTMEM_DEFAULT);
148 	}
149 #ifdef CONFIG_MMU
150 	init_bootmem_done = 1;
151 #endif
152 	paging_init();
153 }
154 
155 void free_init_pages(char *what, unsigned long begin, unsigned long end)
156 {
157 	unsigned long addr;
158 
159 	for (addr = begin; addr < end; addr += PAGE_SIZE) {
160 		ClearPageReserved(virt_to_page(addr));
161 		init_page_count(virt_to_page(addr));
162 		free_page(addr);
163 		totalram_pages++;
164 	}
165 	printk(KERN_INFO "Freeing %s: %ldk freed\n", what, (end - begin) >> 10);
166 }
167 
168 #ifdef CONFIG_BLK_DEV_INITRD
169 void free_initrd_mem(unsigned long start, unsigned long end)
170 {
171 	int pages = 0;
172 	for (; start < end; start += PAGE_SIZE) {
173 		ClearPageReserved(virt_to_page(start));
174 		init_page_count(virt_to_page(start));
175 		free_page(start);
176 		totalram_pages++;
177 		pages++;
178 	}
179 	printk(KERN_NOTICE "Freeing initrd memory: %dk freed\n",
180 					(int)(pages * (PAGE_SIZE / 1024)));
181 }
182 #endif
183 
184 void free_initmem(void)
185 {
186 	free_init_pages("unused kernel memory",
187 			(unsigned long)(&__init_begin),
188 			(unsigned long)(&__init_end));
189 }
190 
191 void __init mem_init(void)
192 {
193 	high_memory = (void *)__va(memory_end);
194 	/* this will put all memory onto the freelists */
195 	totalram_pages += free_all_bootmem();
196 
197 	printk(KERN_INFO "Memory: %luk/%luk available\n",
198 	       nr_free_pages() << (PAGE_SHIFT-10),
199 	       num_physpages << (PAGE_SHIFT-10));
200 	mem_init_done = 1;
201 }
202 
203 #ifndef CONFIG_MMU
204 int page_is_ram(unsigned long pfn)
205 {
206 	return __range_ok(pfn, 0);
207 }
208 #else
209 int page_is_ram(unsigned long pfn)
210 {
211 	return pfn < max_low_pfn;
212 }
213 
214 /*
215  * Check for command-line options that affect what MMU_init will do.
216  */
217 static void mm_cmdline_setup(void)
218 {
219 	unsigned long maxmem = 0;
220 	char *p = cmd_line;
221 
222 	/* Look for mem= option on command line */
223 	p = strstr(cmd_line, "mem=");
224 	if (p) {
225 		p += 4;
226 		maxmem = memparse(p, &p);
227 		if (maxmem && memory_size > maxmem) {
228 			memory_size = maxmem;
229 			memory_end = memory_start + memory_size;
230 			memblock.memory.regions[0].size = memory_size;
231 		}
232 	}
233 }
234 
235 /*
236  * MMU_init_hw does the chip-specific initialization of the MMU hardware.
237  */
238 static void __init mmu_init_hw(void)
239 {
240 	/*
241 	 * The Zone Protection Register (ZPR) defines how protection will
242 	 * be applied to every page which is a member of a given zone. At
243 	 * present, we utilize only two of the zones.
244 	 * The zone index bits (of ZSEL) in the PTE are used for software
245 	 * indicators, except the LSB.  For user access, zone 1 is used,
246 	 * for kernel access, zone 0 is used.  We set all but zone 1
247 	 * to zero, allowing only kernel access as indicated in the PTE.
248 	 * For zone 1, we set a 01 binary (a value of 10 will not work)
249 	 * to allow user access as indicated in the PTE.  This also allows
250 	 * kernel access as indicated in the PTE.
251 	 */
252 	__asm__ __volatile__ ("ori r11, r0, 0x10000000;" \
253 			"mts rzpr, r11;"
254 			: : : "r11");
255 }
256 
257 /*
258  * MMU_init sets up the basic memory mappings for the kernel,
259  * including both RAM and possibly some I/O regions,
260  * and sets up the page tables and the MMU hardware ready to go.
261  */
262 
263 /* called from head.S */
264 asmlinkage void __init mmu_init(void)
265 {
266 	unsigned int kstart, ksize;
267 
268 	if (!memblock.reserved.cnt) {
269 		printk(KERN_EMERG "Error memory count\n");
270 		machine_restart(NULL);
271 	}
272 
273 	if ((u32) memblock.memory.regions[0].size < 0x1000000) {
274 		printk(KERN_EMERG "Memory must be greater than 16MB\n");
275 		machine_restart(NULL);
276 	}
277 	/* Find main memory where the kernel is */
278 	memory_start = (u32) memblock.memory.regions[0].base;
279 	memory_end = (u32) memblock.memory.regions[0].base +
280 				(u32) memblock.memory.regions[0].size;
281 	memory_size = memory_end - memory_start;
282 
283 	mm_cmdline_setup(); /* FIXME parse args from command line - not used */
284 
285 	/*
286 	 * Map out the kernel text/data/bss from the available physical
287 	 * memory.
288 	 */
289 	kstart = __pa(CONFIG_KERNEL_START); /* kernel start */
290 	/* kernel size */
291 	ksize = PAGE_ALIGN(((u32)_end - (u32)CONFIG_KERNEL_START));
292 	memblock_reserve(kstart, ksize);
293 
294 #if defined(CONFIG_BLK_DEV_INITRD)
295 	/* Remove the init RAM disk from the available memory. */
296 /*	if (initrd_start) {
297 		mem_pieces_remove(&phys_avail, __pa(initrd_start),
298 				  initrd_end - initrd_start, 1);
299 	}*/
300 #endif /* CONFIG_BLK_DEV_INITRD */
301 
302 	/* Initialize the MMU hardware */
303 	mmu_init_hw();
304 
305 	/* Map in all of RAM starting at CONFIG_KERNEL_START */
306 	mapin_ram();
307 
308 #ifdef CONFIG_HIGHMEM_START_BOOL
309 	ioremap_base = CONFIG_HIGHMEM_START;
310 #else
311 	ioremap_base = 0xfe000000UL;	/* for now, could be 0xfffff000 */
312 #endif /* CONFIG_HIGHMEM_START_BOOL */
313 	ioremap_bot = ioremap_base;
314 
315 	/* Initialize the context management stuff */
316 	mmu_context_init();
317 }
318 
319 /* This is only called until mem_init is done. */
320 void __init *early_get_page(void)
321 {
322 	void *p;
323 	if (init_bootmem_done) {
324 		p = alloc_bootmem_pages(PAGE_SIZE);
325 	} else {
326 		/*
327 		 * Mem start + 32MB -> here is limit
328 		 * because of mem mapping from head.S
329 		 */
330 		p = __va(memblock_alloc_base(PAGE_SIZE, PAGE_SIZE,
331 					memory_start + 0x2000000));
332 	}
333 	return p;
334 }
335 
336 #endif /* CONFIG_MMU */
337 
338 void * __init_refok alloc_maybe_bootmem(size_t size, gfp_t mask)
339 {
340 	if (mem_init_done)
341 		return kmalloc(size, mask);
342 	else
343 		return alloc_bootmem(size);
344 }
345 
346 void * __init_refok zalloc_maybe_bootmem(size_t size, gfp_t mask)
347 {
348 	void *p;
349 
350 	if (mem_init_done)
351 		p = kzalloc(size, mask);
352 	else {
353 		p = alloc_bootmem(size);
354 		if (p)
355 			memset(p, 0, size);
356 	}
357 	return p;
358 }
359