xref: /openbmc/linux/arch/microblaze/mm/init.c (revision e7253313)
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/memblock.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/mm.h> /* mem_init */
14 #include <linux/initrd.h>
15 #include <linux/pagemap.h>
16 #include <linux/pfn.h>
17 #include <linux/slab.h>
18 #include <linux/swap.h>
19 #include <linux/export.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 #include <asm/fixmap.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 #endif /* CONFIG_MMU */
35 
36 char *klimit = _end;
37 
38 /*
39  * Initialize the bootmem system and give it all the memory we
40  * have available.
41  */
42 unsigned long memory_start;
43 EXPORT_SYMBOL(memory_start);
44 unsigned long memory_size;
45 EXPORT_SYMBOL(memory_size);
46 unsigned long lowmem_size;
47 
48 #ifdef CONFIG_HIGHMEM
49 pte_t *kmap_pte;
50 EXPORT_SYMBOL(kmap_pte);
51 pgprot_t kmap_prot;
52 EXPORT_SYMBOL(kmap_prot);
53 
54 static inline pte_t *virt_to_kpte(unsigned long vaddr)
55 {
56 	pgd_t *pgd = pgd_offset_k(vaddr);
57 	p4d_t *p4d = p4d_offset(pgd, vaddr);
58 	pud_t *pud = pud_offset(p4d, vaddr);
59 
60 	return pte_offset_kernel(pmd_offset(pud, vaddr), vaddr);
61 }
62 
63 static void __init highmem_init(void)
64 {
65 	pr_debug("%x\n", (u32)PKMAP_BASE);
66 	map_page(PKMAP_BASE, 0, 0);	/* XXX gross */
67 	pkmap_page_table = virt_to_kpte(PKMAP_BASE);
68 
69 	kmap_pte = virt_to_kpte(__fix_to_virt(FIX_KMAP_BEGIN));
70 	kmap_prot = PAGE_KERNEL;
71 }
72 
73 static void highmem_setup(void)
74 {
75 	unsigned long pfn;
76 
77 	for (pfn = max_low_pfn; pfn < max_pfn; ++pfn) {
78 		struct page *page = pfn_to_page(pfn);
79 
80 		/* FIXME not sure about */
81 		if (!memblock_is_reserved(pfn << PAGE_SHIFT))
82 			free_highmem_page(page);
83 	}
84 }
85 #endif /* CONFIG_HIGHMEM */
86 
87 /*
88  * paging_init() sets up the page tables - in fact we've already done this.
89  */
90 static void __init paging_init(void)
91 {
92 	unsigned long zones_size[MAX_NR_ZONES];
93 #ifdef CONFIG_MMU
94 	int idx;
95 
96 	/* Setup fixmaps */
97 	for (idx = 0; idx < __end_of_fixed_addresses; idx++)
98 		clear_fixmap(idx);
99 #endif
100 
101 	/* Clean every zones */
102 	memset(zones_size, 0, sizeof(zones_size));
103 
104 #ifdef CONFIG_HIGHMEM
105 	highmem_init();
106 
107 	zones_size[ZONE_DMA] = max_low_pfn;
108 	zones_size[ZONE_HIGHMEM] = max_pfn;
109 #else
110 	zones_size[ZONE_DMA] = max_pfn;
111 #endif
112 
113 	/* We don't have holes in memory map */
114 	free_area_init_nodes(zones_size);
115 }
116 
117 void __init setup_memory(void)
118 {
119 	struct memblock_region *reg;
120 
121 #ifndef CONFIG_MMU
122 	u32 kernel_align_start, kernel_align_size;
123 
124 	/* Find main memory where is the kernel */
125 	for_each_memblock(memory, reg) {
126 		memory_start = (u32)reg->base;
127 		lowmem_size = reg->size;
128 		if ((memory_start <= (u32)_text) &&
129 			((u32)_text <= (memory_start + lowmem_size - 1))) {
130 			memory_size = lowmem_size;
131 			PAGE_OFFSET = memory_start;
132 			pr_info("%s: Main mem: 0x%x, size 0x%08x\n",
133 				__func__, (u32) memory_start,
134 					(u32) memory_size);
135 			break;
136 		}
137 	}
138 
139 	if (!memory_start || !memory_size) {
140 		panic("%s: Missing memory setting 0x%08x, size=0x%08x\n",
141 			__func__, (u32) memory_start, (u32) memory_size);
142 	}
143 
144 	/* reservation of region where is the kernel */
145 	kernel_align_start = PAGE_DOWN((u32)_text);
146 	/* ALIGN can be remove because _end in vmlinux.lds.S is align */
147 	kernel_align_size = PAGE_UP((u32)klimit) - kernel_align_start;
148 	pr_info("%s: kernel addr:0x%08x-0x%08x size=0x%08x\n",
149 		__func__, kernel_align_start, kernel_align_start
150 			+ kernel_align_size, kernel_align_size);
151 	memblock_reserve(kernel_align_start, kernel_align_size);
152 #endif
153 	/*
154 	 * Kernel:
155 	 * start: base phys address of kernel - page align
156 	 * end: base phys address of kernel - page align
157 	 *
158 	 * min_low_pfn - the first page (mm/bootmem.c - node_boot_start)
159 	 * max_low_pfn
160 	 * max_mapnr - the first unused page (mm/bootmem.c - node_low_pfn)
161 	 */
162 
163 	/* memory start is from the kernel end (aligned) to higher addr */
164 	min_low_pfn = memory_start >> PAGE_SHIFT; /* minimum for allocation */
165 	/* RAM is assumed contiguous */
166 	max_mapnr = memory_size >> PAGE_SHIFT;
167 	max_low_pfn = ((u64)memory_start + (u64)lowmem_size) >> PAGE_SHIFT;
168 	max_pfn = ((u64)memory_start + (u64)memory_size) >> PAGE_SHIFT;
169 
170 	pr_info("%s: max_mapnr: %#lx\n", __func__, max_mapnr);
171 	pr_info("%s: min_low_pfn: %#lx\n", __func__, min_low_pfn);
172 	pr_info("%s: max_low_pfn: %#lx\n", __func__, max_low_pfn);
173 	pr_info("%s: max_pfn: %#lx\n", __func__, max_pfn);
174 
175 	/* Add active regions with valid PFNs */
176 	for_each_memblock(memory, reg) {
177 		unsigned long start_pfn, end_pfn;
178 
179 		start_pfn = memblock_region_memory_base_pfn(reg);
180 		end_pfn = memblock_region_memory_end_pfn(reg);
181 		memblock_set_node(start_pfn << PAGE_SHIFT,
182 				  (end_pfn - start_pfn) << PAGE_SHIFT,
183 				  &memblock.memory, 0);
184 	}
185 
186 	/* XXX need to clip this if using highmem? */
187 	sparse_memory_present_with_active_regions(0);
188 
189 	paging_init();
190 }
191 
192 void __init mem_init(void)
193 {
194 	high_memory = (void *)__va(memory_start + lowmem_size - 1);
195 
196 	/* this will put all memory onto the freelists */
197 	memblock_free_all();
198 #ifdef CONFIG_HIGHMEM
199 	highmem_setup();
200 #endif
201 
202 	mem_init_print_info(NULL);
203 #ifdef CONFIG_MMU
204 	pr_info("Kernel virtual memory layout:\n");
205 	pr_info("  * 0x%08lx..0x%08lx  : fixmap\n", FIXADDR_START, FIXADDR_TOP);
206 #ifdef CONFIG_HIGHMEM
207 	pr_info("  * 0x%08lx..0x%08lx  : highmem PTEs\n",
208 		PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP));
209 #endif /* CONFIG_HIGHMEM */
210 	pr_info("  * 0x%08lx..0x%08lx  : early ioremap\n",
211 		ioremap_bot, ioremap_base);
212 	pr_info("  * 0x%08lx..0x%08lx  : vmalloc & ioremap\n",
213 		(unsigned long)VMALLOC_START, VMALLOC_END);
214 #endif
215 	mem_init_done = 1;
216 }
217 
218 #ifndef CONFIG_MMU
219 int page_is_ram(unsigned long pfn)
220 {
221 	return __range_ok(pfn, 0);
222 }
223 #else
224 int page_is_ram(unsigned long pfn)
225 {
226 	return pfn < max_low_pfn;
227 }
228 
229 /*
230  * Check for command-line options that affect what MMU_init will do.
231  */
232 static void mm_cmdline_setup(void)
233 {
234 	unsigned long maxmem = 0;
235 	char *p = cmd_line;
236 
237 	/* Look for mem= option on command line */
238 	p = strstr(cmd_line, "mem=");
239 	if (p) {
240 		p += 4;
241 		maxmem = memparse(p, &p);
242 		if (maxmem && memory_size > maxmem) {
243 			memory_size = maxmem;
244 			memblock.memory.regions[0].size = memory_size;
245 		}
246 	}
247 }
248 
249 /*
250  * MMU_init_hw does the chip-specific initialization of the MMU hardware.
251  */
252 static void __init mmu_init_hw(void)
253 {
254 	/*
255 	 * The Zone Protection Register (ZPR) defines how protection will
256 	 * be applied to every page which is a member of a given zone. At
257 	 * present, we utilize only two of the zones.
258 	 * The zone index bits (of ZSEL) in the PTE are used for software
259 	 * indicators, except the LSB.  For user access, zone 1 is used,
260 	 * for kernel access, zone 0 is used.  We set all but zone 1
261 	 * to zero, allowing only kernel access as indicated in the PTE.
262 	 * For zone 1, we set a 01 binary (a value of 10 will not work)
263 	 * to allow user access as indicated in the PTE.  This also allows
264 	 * kernel access as indicated in the PTE.
265 	 */
266 	__asm__ __volatile__ ("ori r11, r0, 0x10000000;" \
267 			"mts rzpr, r11;"
268 			: : : "r11");
269 }
270 
271 /*
272  * MMU_init sets up the basic memory mappings for the kernel,
273  * including both RAM and possibly some I/O regions,
274  * and sets up the page tables and the MMU hardware ready to go.
275  */
276 
277 /* called from head.S */
278 asmlinkage void __init mmu_init(void)
279 {
280 	unsigned int kstart, ksize;
281 
282 	if (!memblock.reserved.cnt) {
283 		pr_emerg("Error memory count\n");
284 		machine_restart(NULL);
285 	}
286 
287 	if ((u32) memblock.memory.regions[0].size < 0x400000) {
288 		pr_emerg("Memory must be greater than 4MB\n");
289 		machine_restart(NULL);
290 	}
291 
292 	if ((u32) memblock.memory.regions[0].size < kernel_tlb) {
293 		pr_emerg("Kernel size is greater than memory node\n");
294 		machine_restart(NULL);
295 	}
296 
297 	/* Find main memory where the kernel is */
298 	memory_start = (u32) memblock.memory.regions[0].base;
299 	lowmem_size = memory_size = (u32) memblock.memory.regions[0].size;
300 
301 	if (lowmem_size > CONFIG_LOWMEM_SIZE) {
302 		lowmem_size = CONFIG_LOWMEM_SIZE;
303 #ifndef CONFIG_HIGHMEM
304 		memory_size = lowmem_size;
305 #endif
306 	}
307 
308 	mm_cmdline_setup(); /* FIXME parse args from command line - not used */
309 
310 	/*
311 	 * Map out the kernel text/data/bss from the available physical
312 	 * memory.
313 	 */
314 	kstart = __pa(CONFIG_KERNEL_START); /* kernel start */
315 	/* kernel size */
316 	ksize = PAGE_ALIGN(((u32)_end - (u32)CONFIG_KERNEL_START));
317 	memblock_reserve(kstart, ksize);
318 
319 #if defined(CONFIG_BLK_DEV_INITRD)
320 	/* Remove the init RAM disk from the available memory. */
321 	if (initrd_start) {
322 		unsigned long size;
323 		size = initrd_end - initrd_start;
324 		memblock_reserve(__virt_to_phys(initrd_start), size);
325 	}
326 #endif /* CONFIG_BLK_DEV_INITRD */
327 
328 	/* Initialize the MMU hardware */
329 	mmu_init_hw();
330 
331 	/* Map in all of RAM starting at CONFIG_KERNEL_START */
332 	mapin_ram();
333 
334 	/* Extend vmalloc and ioremap area as big as possible */
335 #ifdef CONFIG_HIGHMEM
336 	ioremap_base = ioremap_bot = PKMAP_BASE;
337 #else
338 	ioremap_base = ioremap_bot = FIXADDR_START;
339 #endif
340 
341 	/* Initialize the context management stuff */
342 	mmu_context_init();
343 
344 	/* Shortly after that, the entire linear mapping will be available */
345 	/* This will also cause that unflatten device tree will be allocated
346 	 * inside 768MB limit */
347 	memblock_set_current_limit(memory_start + lowmem_size - 1);
348 }
349 
350 /* This is only called until mem_init is done. */
351 void __init *early_get_page(void)
352 {
353 	/*
354 	 * Mem start + kernel_tlb -> here is limit
355 	 * because of mem mapping from head.S
356 	 */
357 	return memblock_alloc_try_nid_raw(PAGE_SIZE, PAGE_SIZE,
358 				MEMBLOCK_LOW_LIMIT, memory_start + kernel_tlb,
359 				NUMA_NO_NODE);
360 }
361 
362 #endif /* CONFIG_MMU */
363 
364 void * __ref zalloc_maybe_bootmem(size_t size, gfp_t mask)
365 {
366 	void *p;
367 
368 	if (mem_init_done) {
369 		p = kzalloc(size, mask);
370 	} else {
371 		p = memblock_alloc(size, SMP_CACHE_BYTES);
372 		if (!p)
373 			panic("%s: Failed to allocate %zu bytes\n",
374 			      __func__, size);
375 	}
376 
377 	return p;
378 }
379