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