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