xref: /linux/arch/microblaze/mm/init.c (revision d49004c5f0c140bb83c87fab46dcf449cf00eb24)
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/dma-map-ops.h>
11 #include <linux/memblock.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h> /* mem_init */
15 #include <linux/initrd.h>
16 #include <linux/of_fdt.h>
17 #include <linux/pagemap.h>
18 #include <linux/pfn.h>
19 #include <linux/slab.h>
20 #include <linux/swap.h>
21 #include <linux/export.h>
22 
23 #include <asm/page.h>
24 #include <asm/mmu_context.h>
25 #include <asm/pgalloc.h>
26 #include <asm/sections.h>
27 #include <asm/tlb.h>
28 #include <asm/fixmap.h>
29 
30 /* Use for MMU and noMMU because of PCI generic code */
31 int mem_init_done;
32 
33 char *klimit = _end;
34 
35 /*
36  * Initialize the bootmem system and give it all the memory we
37  * have available.
38  */
39 unsigned long memory_start;
40 EXPORT_SYMBOL(memory_start);
41 unsigned long memory_size;
42 EXPORT_SYMBOL(memory_size);
43 unsigned long lowmem_size;
44 
45 EXPORT_SYMBOL(min_low_pfn);
46 EXPORT_SYMBOL(max_low_pfn);
47 
48 #ifdef CONFIG_HIGHMEM
49 static void __init highmem_init(void)
50 {
51 	pr_debug("%x\n", (u32)PKMAP_BASE);
52 	map_page(PKMAP_BASE, 0, 0);	/* XXX gross */
53 	pkmap_page_table = virt_to_kpte(PKMAP_BASE);
54 }
55 #endif /* CONFIG_HIGHMEM */
56 
57 void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
58 {
59 #ifdef CONFIG_HIGHMEM
60 	max_zone_pfns[ZONE_DMA] = max_low_pfn;
61 	max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
62 #else
63 	max_zone_pfns[ZONE_DMA] = max_pfn;
64 #endif
65 }
66 
67 /*
68  * paging_init() sets up the page tables - in fact we've already done this.
69  */
70 static void __init paging_init(void)
71 {
72 	int idx;
73 
74 	/* Setup fixmaps */
75 	for (idx = 0; idx < __end_of_fixed_addresses; idx++)
76 		clear_fixmap(idx);
77 
78 #ifdef CONFIG_HIGHMEM
79 	highmem_init();
80 #endif
81 }
82 
83 void __init setup_memory(void)
84 {
85 	/*
86 	 * Kernel:
87 	 * start: base phys address of kernel - page align
88 	 * end: base phys address of kernel - page align
89 	 *
90 	 * min_low_pfn - the first page (mm/bootmem.c - node_boot_start)
91 	 * max_low_pfn
92 	 */
93 
94 	/* memory start is from the kernel end (aligned) to higher addr */
95 	min_low_pfn = memory_start >> PAGE_SHIFT; /* minimum for allocation */
96 	max_low_pfn = ((u64)memory_start + (u64)lowmem_size) >> PAGE_SHIFT;
97 	max_pfn = ((u64)memory_start + (u64)memory_size) >> PAGE_SHIFT;
98 
99 	pr_info("%s: min_low_pfn: %#lx\n", __func__, min_low_pfn);
100 	pr_info("%s: max_low_pfn: %#lx\n", __func__, max_low_pfn);
101 	pr_info("%s: max_pfn: %#lx\n", __func__, max_pfn);
102 
103 	paging_init();
104 }
105 
106 void __init mem_init(void)
107 {
108 	mem_init_done = 1;
109 }
110 
111 int page_is_ram(unsigned long pfn)
112 {
113 	return pfn < max_low_pfn;
114 }
115 
116 /*
117  * Check for command-line options that affect what MMU_init will do.
118  */
119 static void __init mm_cmdline_setup(void)
120 {
121 	unsigned long maxmem = 0;
122 	char *p = cmd_line;
123 
124 	/* Look for mem= option on command line */
125 	p = strstr(cmd_line, "mem=");
126 	if (p) {
127 		p += 4;
128 		maxmem = memparse(p, &p);
129 		if (maxmem && memory_size > maxmem) {
130 			memory_size = maxmem;
131 			memblock.memory.regions[0].size = memory_size;
132 		}
133 	}
134 }
135 
136 /*
137  * MMU_init_hw does the chip-specific initialization of the MMU hardware.
138  */
139 static void __init mmu_init_hw(void)
140 {
141 	/*
142 	 * The Zone Protection Register (ZPR) defines how protection will
143 	 * be applied to every page which is a member of a given zone. At
144 	 * present, we utilize only two of the zones.
145 	 * The zone index bits (of ZSEL) in the PTE are used for software
146 	 * indicators, except the LSB.  For user access, zone 1 is used,
147 	 * for kernel access, zone 0 is used.  We set all but zone 1
148 	 * to zero, allowing only kernel access as indicated in the PTE.
149 	 * For zone 1, we set a 01 binary (a value of 10 will not work)
150 	 * to allow user access as indicated in the PTE.  This also allows
151 	 * kernel access as indicated in the PTE.
152 	 */
153 	__asm__ __volatile__ ("ori r11, r0, 0x10000000;" \
154 			"mts rzpr, r11;"
155 			: : : "r11");
156 }
157 
158 /*
159  * MMU_init sets up the basic memory mappings for the kernel,
160  * including both RAM and possibly some I/O regions,
161  * and sets up the page tables and the MMU hardware ready to go.
162  */
163 
164 /* called from head.S */
165 asmlinkage void __init mmu_init(void)
166 {
167 	unsigned int kstart, ksize;
168 
169 	if ((u32) memblock.memory.regions[0].size < 0x400000) {
170 		pr_emerg("Memory must be greater than 4MB\n");
171 		machine_restart(NULL);
172 	}
173 
174 	if ((u32) memblock.memory.regions[0].size < kernel_tlb) {
175 		pr_emerg("Kernel size is greater than memory node\n");
176 		machine_restart(NULL);
177 	}
178 
179 	/* Find main memory where the kernel is */
180 	memory_start = (u32) memblock.memory.regions[0].base;
181 	lowmem_size = memory_size = (u32) memblock.memory.regions[0].size;
182 
183 	if (lowmem_size > CONFIG_LOWMEM_SIZE) {
184 		lowmem_size = CONFIG_LOWMEM_SIZE;
185 #ifndef CONFIG_HIGHMEM
186 		memory_size = lowmem_size;
187 #endif
188 	}
189 
190 	mm_cmdline_setup(); /* FIXME parse args from command line - not used */
191 
192 	/*
193 	 * Map out the kernel text/data/bss from the available physical
194 	 * memory.
195 	 */
196 	kstart = __pa(CONFIG_KERNEL_START); /* kernel start */
197 	/* kernel size */
198 	ksize = PAGE_ALIGN(((u32)_end - (u32)CONFIG_KERNEL_START));
199 	memblock_reserve(kstart, ksize);
200 
201 #if defined(CONFIG_BLK_DEV_INITRD)
202 	/* Remove the init RAM disk from the available memory. */
203 	if (initrd_start) {
204 		unsigned long size;
205 		size = initrd_end - initrd_start;
206 		memblock_reserve(__virt_to_phys(initrd_start), size);
207 	}
208 #endif /* CONFIG_BLK_DEV_INITRD */
209 
210 	/* Initialize the MMU hardware */
211 	mmu_init_hw();
212 
213 	/* Map in all of RAM starting at CONFIG_KERNEL_START */
214 	mapin_ram();
215 
216 	/* Extend vmalloc and ioremap area as big as possible */
217 #ifdef CONFIG_HIGHMEM
218 	ioremap_base = ioremap_bot = PKMAP_BASE;
219 #else
220 	ioremap_base = ioremap_bot = FIXADDR_START;
221 #endif
222 
223 	/* Initialize the context management stuff */
224 	mmu_context_init();
225 
226 	/* Shortly after that, the entire linear mapping will be available */
227 	/* This will also cause that unflatten device tree will be allocated
228 	 * inside 768MB limit */
229 	memblock_set_current_limit(memory_start + lowmem_size - 1);
230 
231 	parse_early_param();
232 
233 	early_init_fdt_scan_reserved_mem();
234 
235 	/* CMA initialization */
236 	dma_contiguous_reserve(memory_start + lowmem_size - 1);
237 
238 	memblock_dump_all();
239 }
240 
241 static const pgprot_t protection_map[16] = {
242 	[VM_NONE]					= PAGE_NONE,
243 	[VM_READ]					= PAGE_READONLY_X,
244 	[VM_WRITE]					= PAGE_COPY,
245 	[VM_WRITE | VM_READ]				= PAGE_COPY_X,
246 	[VM_EXEC]					= PAGE_READONLY,
247 	[VM_EXEC | VM_READ]				= PAGE_READONLY_X,
248 	[VM_EXEC | VM_WRITE]				= PAGE_COPY,
249 	[VM_EXEC | VM_WRITE | VM_READ]			= PAGE_COPY_X,
250 	[VM_SHARED]					= PAGE_NONE,
251 	[VM_SHARED | VM_READ]				= PAGE_READONLY_X,
252 	[VM_SHARED | VM_WRITE]				= PAGE_SHARED,
253 	[VM_SHARED | VM_WRITE | VM_READ]		= PAGE_SHARED_X,
254 	[VM_SHARED | VM_EXEC]				= PAGE_READONLY,
255 	[VM_SHARED | VM_EXEC | VM_READ]			= PAGE_READONLY_X,
256 	[VM_SHARED | VM_EXEC | VM_WRITE]		= PAGE_SHARED,
257 	[VM_SHARED | VM_EXEC | VM_WRITE | VM_READ]	= PAGE_SHARED_X
258 };
259 DECLARE_VM_GET_PAGE_PROT
260