xref: /linux/arch/sh/mm/init.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/arch/sh/mm/init.c
4  *
5  *  Copyright (C) 1999  Niibe Yutaka
6  *  Copyright (C) 2002 - 2011  Paul Mundt
7  *
8  *  Based on linux/arch/i386/mm/init.c:
9  *   Copyright (C) 1995  Linus Torvalds
10  */
11 #include <linux/mm.h>
12 #include <linux/swap.h>
13 #include <linux/init.h>
14 #include <linux/gfp.h>
15 #include <linux/memblock.h>
16 #include <linux/proc_fs.h>
17 #include <linux/pagemap.h>
18 #include <linux/percpu.h>
19 #include <linux/io.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/export.h>
22 #include <asm/mmu_context.h>
23 #include <asm/kexec.h>
24 #include <asm/tlb.h>
25 #include <asm/cacheflush.h>
26 #include <asm/sections.h>
27 #include <asm/setup.h>
28 #include <asm/cache.h>
29 #include <asm/pgalloc.h>
30 #include <linux/sizes.h>
31 #include "ioremap.h"
32 
33 pgd_t swapper_pg_dir[PTRS_PER_PGD];
34 
35 void __init generic_mem_init(void)
36 {
37 	memblock_add(__MEMORY_START, __MEMORY_SIZE);
38 }
39 
40 #ifdef CONFIG_MMU
41 static pte_t *__get_pte_phys(unsigned long addr)
42 {
43 	pgd_t *pgd;
44 	p4d_t *p4d;
45 	pud_t *pud;
46 	pmd_t *pmd;
47 
48 	pgd = pgd_offset_k(addr);
49 	if (pgd_none(*pgd))
50 		return NULL;
51 
52 	p4d = p4d_alloc(NULL, pgd, addr);
53 	if (unlikely(!p4d)) {
54 		pr_err("allocating p4d table failed\n");
55 		return NULL;
56 	}
57 
58 	pud = pud_alloc(NULL, p4d, addr);
59 	if (unlikely(!pud)) {
60 		pr_err("allocating pud table failed\n");
61 		return NULL;
62 	}
63 
64 	pmd = pmd_alloc(NULL, pud, addr);
65 	if (unlikely(!pmd)) {
66 		pr_err("allocating pmd table failed\n");
67 		return NULL;
68 	}
69 
70 	return pte_offset_kernel(pmd, addr);
71 }
72 
73 static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
74 {
75 	pte_t *pte;
76 
77 	pte = __get_pte_phys(addr);
78 	if (!pte_none(*pte)) {
79 		char str[PTVAL_STR_MAX];
80 
81 		ptval_to_str(str, pte_val(*pte));
82 		pr_err("unexpected set PTE at %lx in %s: bad pte %p(%s).\n",
83 			addr, __func__, pte, str);
84 		return;
85 	}
86 
87 	set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
88 	local_flush_tlb_one(get_asid(), addr);
89 
90 	if (pgprot_val(prot) & _PAGE_WIRED)
91 		tlb_wire_entry(NULL, addr, *pte);
92 }
93 
94 static void clear_pte_phys(unsigned long addr, pgprot_t prot)
95 {
96 	pte_t *pte;
97 
98 	pte = __get_pte_phys(addr);
99 
100 	if (pgprot_val(prot) & _PAGE_WIRED)
101 		tlb_unwire_entry();
102 
103 	set_pte(pte, pfn_pte(0, __pgprot(0)));
104 	local_flush_tlb_one(get_asid(), addr);
105 }
106 
107 void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
108 {
109 	unsigned long address = __fix_to_virt(idx);
110 
111 	if (idx >= __end_of_fixed_addresses) {
112 		BUG();
113 		return;
114 	}
115 
116 	set_pte_phys(address, phys, prot);
117 }
118 
119 void __clear_fixmap(enum fixed_addresses idx, pgprot_t prot)
120 {
121 	unsigned long address = __fix_to_virt(idx);
122 
123 	if (idx >= __end_of_fixed_addresses) {
124 		BUG();
125 		return;
126 	}
127 
128 	clear_pte_phys(address, prot);
129 }
130 
131 static pmd_t * __init one_md_table_init(pud_t *pud)
132 {
133 	if (pud_none(*pud)) {
134 		pmd_t *pmd;
135 
136 		pmd = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
137 		pud_populate(&init_mm, pud, pmd);
138 		BUG_ON(pmd != pmd_offset(pud, 0));
139 	}
140 
141 	return pmd_offset(pud, 0);
142 }
143 
144 static pte_t * __init one_page_table_init(pmd_t *pmd)
145 {
146 	if (pmd_none(*pmd)) {
147 		pte_t *pte;
148 
149 		pte = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
150 		pmd_populate_kernel(&init_mm, pmd, pte);
151 		BUG_ON(pte != pte_offset_kernel(pmd, 0));
152 	}
153 
154 	return pte_offset_kernel(pmd, 0);
155 }
156 
157 static pte_t * __init page_table_kmap_check(pte_t *pte, pmd_t *pmd,
158 					    unsigned long vaddr, pte_t *lastpte)
159 {
160 	return pte;
161 }
162 
163 void __init page_table_range_init(unsigned long start, unsigned long end,
164 					 pgd_t *pgd_base)
165 {
166 	pgd_t *pgd;
167 	pud_t *pud;
168 	pmd_t *pmd;
169 	pte_t *pte = NULL;
170 	int i, j, k;
171 	unsigned long vaddr;
172 
173 	vaddr = start;
174 	i = pgd_index(vaddr);
175 	j = pud_index(vaddr);
176 	k = pmd_index(vaddr);
177 	pgd = pgd_base + i;
178 
179 	for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
180 		pud = (pud_t *)pgd;
181 		for ( ; (j < PTRS_PER_PUD) && (vaddr != end); pud++, j++) {
182 			pmd = one_md_table_init(pud);
183 #ifndef __PAGETABLE_PMD_FOLDED
184 			pmd += k;
185 #endif
186 			for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) {
187 				pte = page_table_kmap_check(one_page_table_init(pmd),
188 							    pmd, vaddr, pte);
189 				vaddr += PMD_SIZE;
190 			}
191 			k = 0;
192 		}
193 		j = 0;
194 	}
195 }
196 #endif	/* CONFIG_MMU */
197 
198 static void __init do_init_bootmem(void)
199 {
200 	unsigned long start_pfn, end_pfn;
201 	int i;
202 
203 	/* Add active regions with valid PFNs. */
204 	for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL)
205 		__add_active_range(0, start_pfn, end_pfn);
206 
207 	node_set_online(0);
208 }
209 
210 static void __init early_reserve_mem(void)
211 {
212 	unsigned long start_pfn;
213 	u32 zero_base = (u32)__MEMORY_START + (u32)PHYSICAL_OFFSET;
214 	u32 start = zero_base + (u32)CONFIG_ZERO_PAGE_OFFSET;
215 
216 	/*
217 	 * Partially used pages are not usable - thus
218 	 * we are rounding upwards:
219 	 */
220 	start_pfn = PFN_UP(__pa(_end));
221 
222 	/*
223 	 * Reserve the kernel text and Reserve the bootmem bitmap. We do
224 	 * this in two steps (first step was init_bootmem()), because
225 	 * this catches the (definitely buggy) case of us accidentally
226 	 * initializing the bootmem allocator with an invalid RAM area.
227 	 */
228 	memblock_reserve(start, (PFN_PHYS(start_pfn) + PAGE_SIZE - 1) - start);
229 
230 	/*
231 	 * Reserve physical pages below CONFIG_ZERO_PAGE_OFFSET.
232 	 */
233 	if (CONFIG_ZERO_PAGE_OFFSET != 0)
234 		memblock_reserve(zero_base, CONFIG_ZERO_PAGE_OFFSET);
235 
236 	/*
237 	 * Handle additional early reservations
238 	 */
239 	check_for_initrd();
240 	reserve_crashkernel();
241 }
242 
243 void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
244 {
245 	max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
246 }
247 
248 void __init paging_init(void)
249 {
250 	unsigned long vaddr, end;
251 
252 	sh_mv.mv_mem_init();
253 
254 	early_reserve_mem();
255 
256 	/*
257 	 * Once the early reservations are out of the way, give the
258 	 * platforms a chance to kick out some memory.
259 	 */
260 	if (sh_mv.mv_mem_reserve)
261 		sh_mv.mv_mem_reserve();
262 
263 	memblock_enforce_memory_limit(memory_limit);
264 	memblock_allow_resize();
265 
266 	memblock_dump_all();
267 
268 	/*
269 	 * Determine low and high memory ranges:
270 	 */
271 	max_low_pfn = max_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
272 	min_low_pfn = __MEMORY_START >> PAGE_SHIFT;
273 
274 	nodes_clear(node_online_map);
275 
276 	memory_start = (unsigned long)__va(__MEMORY_START);
277 	memory_end = memory_start + (memory_limit ?: memblock_phys_mem_size());
278 
279 	uncached_init();
280 	pmb_init();
281 	do_init_bootmem();
282 	ioremap_fixed_init();
283 
284 	/* We don't need to map the kernel through the TLB, as
285 	 * it is permanatly mapped using P1. So clear the
286 	 * entire pgd. */
287 	memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
288 
289 	/* Set an initial value for the MMU.TTB so we don't have to
290 	 * check for a null value. */
291 	set_TTB(swapper_pg_dir);
292 
293 	/*
294 	 * Populate the relevant portions of swapper_pg_dir so that
295 	 * we can use the fixmap entries without calling kmalloc.
296 	 * pte's will be filled in by __set_fixmap().
297 	 */
298 	vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
299 	end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
300 	page_table_range_init(vaddr, end, swapper_pg_dir);
301 
302 	kmap_coherent_init();
303 }
304 
305 unsigned int mem_init_done = 0;
306 
307 void __init mem_init(void)
308 {
309 	/* Set this up early, so we can take care of the zero page */
310 	cpu_cache_init();
311 
312 	vsyscall_init();
313 
314 	pr_info("virtual kernel memory layout:\n"
315 		"    fixmap  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
316 		"    vmalloc : 0x%08lx - 0x%08lx   (%4ld MB)\n"
317 		"    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB) (cached)\n"
318 #ifdef CONFIG_UNCACHED_MAPPING
319 		"            : 0x%08lx - 0x%08lx   (%4ld MB) (uncached)\n"
320 #endif
321 		"      .init : 0x%08lx - 0x%08lx   (%4ld kB)\n"
322 		"      .data : 0x%08lx - 0x%08lx   (%4ld kB)\n"
323 		"      .text : 0x%08lx - 0x%08lx   (%4ld kB)\n",
324 		FIXADDR_START, FIXADDR_TOP,
325 		(FIXADDR_TOP - FIXADDR_START) >> 10,
326 
327 		(unsigned long)VMALLOC_START, VMALLOC_END,
328 		(VMALLOC_END - VMALLOC_START) >> 20,
329 
330 		(unsigned long)memory_start, (unsigned long)high_memory,
331 		((unsigned long)high_memory - (unsigned long)memory_start) >> 20,
332 
333 #ifdef CONFIG_UNCACHED_MAPPING
334 		uncached_start, uncached_end, uncached_size >> 20,
335 #endif
336 
337 		(unsigned long)&__init_begin, (unsigned long)&__init_end,
338 		((unsigned long)&__init_end -
339 		 (unsigned long)&__init_begin) >> 10,
340 
341 		(unsigned long)&_etext, (unsigned long)&_edata,
342 		((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
343 
344 		(unsigned long)&_text, (unsigned long)&_etext,
345 		((unsigned long)&_etext - (unsigned long)&_text) >> 10);
346 
347 	mem_init_done = 1;
348 }
349