blob: 83f7d12042fbc781ab91a3a90c70043400f66c81 [file] [log] [blame]
Thomas Gleixner50acfb22019-05-29 07:18:00 -07001// SPDX-License-Identifier: GPL-2.0-only
Palmer Dabbelt76d2a042017-07-10 18:00:26 -07002/*
3 * Copyright (C) 2012 Regents of the University of California
Anup Patel671f9a32019-06-28 13:36:21 -07004 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
Palmer Dabbelt76d2a042017-07-10 18:00:26 -07005 */
6
7#include <linux/init.h>
8#include <linux/mm.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -07009#include <linux/memblock.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070010#include <linux/initrd.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070011#include <linux/swap.h>
Christoph Hellwig5ec9c4f2018-01-16 09:37:50 +010012#include <linux/sizes.h>
Anup Patel0651c262019-02-21 11:25:49 +053013#include <linux/of_fdt.h>
Albert Ou922b0372019-09-27 16:14:18 -070014#include <linux/libfdt.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070015
Anup Patelf2c17aa2019-01-07 20:57:01 +053016#include <asm/fixmap.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070017#include <asm/tlbflush.h>
18#include <asm/sections.h>
19#include <asm/pgtable.h>
20#include <asm/io.h>
21
Anup Patel387181d2019-03-26 08:03:47 +000022unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
23 __page_aligned_bss;
24EXPORT_SYMBOL(empty_zero_page);
25
Anup Pateld90d45d2019-06-07 06:01:29 +000026extern char _start[];
27
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070028static void __init zone_sizes_init(void)
29{
Christoph Hellwig5ec9c4f2018-01-16 09:37:50 +010030 unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070031
Zong Lid5fad482018-06-25 16:49:37 +080032#ifdef CONFIG_ZONE_DMA32
Guo Ren28198c42019-01-12 16:16:27 +080033 max_zone_pfns[ZONE_DMA32] = PFN_DOWN(min(4UL * SZ_1G,
34 (unsigned long) PFN_PHYS(max_low_pfn)));
Zong Lid5fad482018-06-25 16:49:37 +080035#endif
Christoph Hellwig5ec9c4f2018-01-16 09:37:50 +010036 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
37
38 free_area_init_nodes(max_zone_pfns);
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070039}
40
41void setup_zero_page(void)
42{
43 memset((void *)empty_zero_page, 0, PAGE_SIZE);
44}
45
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070046void __init mem_init(void)
47{
48#ifdef CONFIG_FLATMEM
49 BUG_ON(!mem_map);
50#endif /* CONFIG_FLATMEM */
51
52 high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
Mike Rapoportc6ffc5c2018-10-30 15:09:30 -070053 memblock_free_all();
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070054
55 mem_init_print_info(NULL);
56}
57
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070058#ifdef CONFIG_BLK_DEV_INITRD
Anup Patel0651c262019-02-21 11:25:49 +053059static void __init setup_initrd(void)
60{
61 unsigned long size;
62
63 if (initrd_start >= initrd_end) {
64 pr_info("initrd not found or empty");
65 goto disable;
66 }
67 if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
68 pr_err("initrd extends beyond end of memory");
69 goto disable;
70 }
71
72 size = initrd_end - initrd_start;
73 memblock_reserve(__pa(initrd_start), size);
74 initrd_below_start_ok = 1;
75
76 pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
77 (void *)(initrd_start), size);
78 return;
79disable:
80 pr_cont(" - disabling initrd\n");
81 initrd_start = 0;
82 initrd_end = 0;
83}
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070084#endif /* CONFIG_BLK_DEV_INITRD */
Anup Patel0651c262019-02-21 11:25:49 +053085
Albert Ou922b0372019-09-27 16:14:18 -070086static phys_addr_t dtb_early_pa __initdata;
87
Anup Patel0651c262019-02-21 11:25:49 +053088void __init setup_bootmem(void)
89{
90 struct memblock_region *reg;
91 phys_addr_t mem_size = 0;
Anup Pateld90d45d2019-06-07 06:01:29 +000092 phys_addr_t vmlinux_end = __pa(&_end);
93 phys_addr_t vmlinux_start = __pa(&_start);
Anup Patel0651c262019-02-21 11:25:49 +053094
95 /* Find the memory region containing the kernel */
96 for_each_memblock(memory, reg) {
Anup Patel0651c262019-02-21 11:25:49 +053097 phys_addr_t end = reg->base + reg->size;
98
99 if (reg->base <= vmlinux_end && vmlinux_end <= end) {
Anup Patel0651c262019-02-21 11:25:49 +0530100 mem_size = min(reg->size, (phys_addr_t)-PAGE_OFFSET);
Anup Patelf05badd2019-04-05 05:49:34 +0000101
102 /*
103 * Remove memblock from the end of usable area to the
104 * end of region
105 */
106 if (reg->base + mem_size < end)
107 memblock_remove(reg->base + mem_size,
108 end - reg->base - mem_size);
Anup Patel0651c262019-02-21 11:25:49 +0530109 }
110 }
111 BUG_ON(mem_size == 0);
112
Anup Pateld90d45d2019-06-07 06:01:29 +0000113 /* Reserve from the start of the kernel to the end of the kernel */
114 memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
115
Anup Patel0651c262019-02-21 11:25:49 +0530116 set_max_mapnr(PFN_DOWN(mem_size));
117 max_low_pfn = PFN_DOWN(memblock_end_of_DRAM());
118
119#ifdef CONFIG_BLK_DEV_INITRD
120 setup_initrd();
121#endif /* CONFIG_BLK_DEV_INITRD */
122
Albert Ou922b0372019-09-27 16:14:18 -0700123 /*
124 * Avoid using early_init_fdt_reserve_self() since __pa() does
125 * not work for DTB pointers that are fixmap addresses
126 */
127 memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
128
Anup Patel0651c262019-02-21 11:25:49 +0530129 early_init_fdt_scan_reserved_mem();
130 memblock_allow_resize();
131 memblock_dump_all();
132
133 for_each_memblock(memory, reg) {
134 unsigned long start_pfn = memblock_region_memory_base_pfn(reg);
135 unsigned long end_pfn = memblock_region_memory_end_pfn(reg);
136
137 memblock_set_node(PFN_PHYS(start_pfn),
138 PFN_PHYS(end_pfn - start_pfn),
139 &memblock.memory, 0);
140 }
141}
Anup Patel6f1e9e92019-02-13 16:38:36 +0530142
Anup Patel387181d2019-03-26 08:03:47 +0000143unsigned long va_pa_offset;
144EXPORT_SYMBOL(va_pa_offset);
145unsigned long pfn_base;
146EXPORT_SYMBOL(pfn_base);
147
Anup Patel671f9a32019-06-28 13:36:21 -0700148void *dtb_early_va;
Anup Patel6f1e9e92019-02-13 16:38:36 +0530149pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
Anup Patel671f9a32019-06-28 13:36:21 -0700150pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
Anup Patelf2c17aa2019-01-07 20:57:01 +0530151pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
Anup Patel671f9a32019-06-28 13:36:21 -0700152static bool mmu_enabled;
153
154#define MAX_EARLY_MAPPING_SIZE SZ_128M
155
156pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
Anup Patelf2c17aa2019-01-07 20:57:01 +0530157
158void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
159{
160 unsigned long addr = __fix_to_virt(idx);
161 pte_t *ptep;
162
163 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
164
165 ptep = &fixmap_pte[pte_index(addr)];
166
167 if (pgprot_val(prot)) {
168 set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
169 } else {
170 pte_clear(&init_mm, addr, ptep);
171 local_flush_tlb_page(addr);
172 }
173}
174
Anup Patel671f9a32019-06-28 13:36:21 -0700175static pte_t *__init get_pte_virt(phys_addr_t pa)
176{
177 if (mmu_enabled) {
178 clear_fixmap(FIX_PTE);
179 return (pte_t *)set_fixmap_offset(FIX_PTE, pa);
180 } else {
181 return (pte_t *)((uintptr_t)pa);
182 }
183}
184
185static phys_addr_t __init alloc_pte(uintptr_t va)
186{
187 /*
188 * We only create PMD or PGD early mappings so we
189 * should never reach here with MMU disabled.
190 */
191 BUG_ON(!mmu_enabled);
192
193 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
194}
195
196static void __init create_pte_mapping(pte_t *ptep,
197 uintptr_t va, phys_addr_t pa,
198 phys_addr_t sz, pgprot_t prot)
199{
200 uintptr_t pte_index = pte_index(va);
201
202 BUG_ON(sz != PAGE_SIZE);
203
204 if (pte_none(ptep[pte_index]))
205 ptep[pte_index] = pfn_pte(PFN_DOWN(pa), prot);
206}
207
208#ifndef __PAGETABLE_PMD_FOLDED
209
210pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss;
211pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
212
213#if MAX_EARLY_MAPPING_SIZE < PGDIR_SIZE
214#define NUM_EARLY_PMDS 1UL
215#else
216#define NUM_EARLY_PMDS (1UL + MAX_EARLY_MAPPING_SIZE / PGDIR_SIZE)
217#endif
218pmd_t early_pmd[PTRS_PER_PMD * NUM_EARLY_PMDS] __initdata __aligned(PAGE_SIZE);
219
220static pmd_t *__init get_pmd_virt(phys_addr_t pa)
221{
222 if (mmu_enabled) {
223 clear_fixmap(FIX_PMD);
224 return (pmd_t *)set_fixmap_offset(FIX_PMD, pa);
225 } else {
226 return (pmd_t *)((uintptr_t)pa);
227 }
228}
229
230static phys_addr_t __init alloc_pmd(uintptr_t va)
231{
232 uintptr_t pmd_num;
233
234 if (mmu_enabled)
235 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
236
237 pmd_num = (va - PAGE_OFFSET) >> PGDIR_SHIFT;
238 BUG_ON(pmd_num >= NUM_EARLY_PMDS);
239 return (uintptr_t)&early_pmd[pmd_num * PTRS_PER_PMD];
240}
241
242static void __init create_pmd_mapping(pmd_t *pmdp,
243 uintptr_t va, phys_addr_t pa,
244 phys_addr_t sz, pgprot_t prot)
245{
246 pte_t *ptep;
247 phys_addr_t pte_phys;
248 uintptr_t pmd_index = pmd_index(va);
249
250 if (sz == PMD_SIZE) {
251 if (pmd_none(pmdp[pmd_index]))
252 pmdp[pmd_index] = pfn_pmd(PFN_DOWN(pa), prot);
253 return;
254 }
255
256 if (pmd_none(pmdp[pmd_index])) {
257 pte_phys = alloc_pte(va);
258 pmdp[pmd_index] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE);
259 ptep = get_pte_virt(pte_phys);
260 memset(ptep, 0, PAGE_SIZE);
261 } else {
262 pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_index]));
263 ptep = get_pte_virt(pte_phys);
264 }
265
266 create_pte_mapping(ptep, va, pa, sz, prot);
267}
268
269#define pgd_next_t pmd_t
270#define alloc_pgd_next(__va) alloc_pmd(__va)
271#define get_pgd_next_virt(__pa) get_pmd_virt(__pa)
272#define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
273 create_pmd_mapping(__nextp, __va, __pa, __sz, __prot)
274#define PTE_PARENT_SIZE PMD_SIZE
275#define fixmap_pgd_next fixmap_pmd
276#else
277#define pgd_next_t pte_t
278#define alloc_pgd_next(__va) alloc_pte(__va)
279#define get_pgd_next_virt(__pa) get_pte_virt(__pa)
280#define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
281 create_pte_mapping(__nextp, __va, __pa, __sz, __prot)
282#define PTE_PARENT_SIZE PGDIR_SIZE
283#define fixmap_pgd_next fixmap_pte
284#endif
285
286static void __init create_pgd_mapping(pgd_t *pgdp,
287 uintptr_t va, phys_addr_t pa,
288 phys_addr_t sz, pgprot_t prot)
289{
290 pgd_next_t *nextp;
291 phys_addr_t next_phys;
292 uintptr_t pgd_index = pgd_index(va);
293
294 if (sz == PGDIR_SIZE) {
295 if (pgd_val(pgdp[pgd_index]) == 0)
296 pgdp[pgd_index] = pfn_pgd(PFN_DOWN(pa), prot);
297 return;
298 }
299
300 if (pgd_val(pgdp[pgd_index]) == 0) {
301 next_phys = alloc_pgd_next(va);
302 pgdp[pgd_index] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE);
303 nextp = get_pgd_next_virt(next_phys);
304 memset(nextp, 0, PAGE_SIZE);
305 } else {
306 next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_index]));
307 nextp = get_pgd_next_virt(next_phys);
308 }
309
310 create_pgd_next_mapping(nextp, va, pa, sz, prot);
311}
312
313static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
314{
315 uintptr_t map_size = PAGE_SIZE;
316
317 /* Upgrade to PMD/PGDIR mappings whenever possible */
318 if (!(base & (PTE_PARENT_SIZE - 1)) &&
319 !(size & (PTE_PARENT_SIZE - 1)))
320 map_size = PTE_PARENT_SIZE;
321
322 return map_size;
323}
324
Anup Patel387181d2019-03-26 08:03:47 +0000325/*
326 * setup_vm() is called from head.S with MMU-off.
327 *
328 * Following requirements should be honoured for setup_vm() to work
329 * correctly:
330 * 1) It should use PC-relative addressing for accessing kernel symbols.
331 * To achieve this we always use GCC cmodel=medany.
332 * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
333 * so disable compiler instrumentation when FTRACE is enabled.
334 *
335 * Currently, the above requirements are honoured by using custom CFLAGS
336 * for init.o in mm/Makefile.
337 */
338
339#ifndef __riscv_cmodel_medany
340#error "setup_vm() is called from head.S before relocate so it should "
341 "not use absolute addressing."
342#endif
343
Anup Patel671f9a32019-06-28 13:36:21 -0700344asmlinkage void __init setup_vm(uintptr_t dtb_pa)
Anup Patel6f1e9e92019-02-13 16:38:36 +0530345{
Anup Patel671f9a32019-06-28 13:36:21 -0700346 uintptr_t va, end_va;
347 uintptr_t load_pa = (uintptr_t)(&_start);
348 uintptr_t load_sz = (uintptr_t)(&_end) - load_pa;
349 uintptr_t map_size = best_map_size(load_pa, MAX_EARLY_MAPPING_SIZE);
Anup Patel6f1e9e92019-02-13 16:38:36 +0530350
Anup Patel671f9a32019-06-28 13:36:21 -0700351 va_pa_offset = PAGE_OFFSET - load_pa;
352 pfn_base = PFN_DOWN(load_pa);
353
354 /*
355 * Enforce boot alignment requirements of RV32 and
356 * RV64 by only allowing PMD or PGD mappings.
357 */
358 BUG_ON(map_size == PAGE_SIZE);
Anup Patel6f1e9e92019-02-13 16:38:36 +0530359
360 /* Sanity check alignment and size */
361 BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
Anup Patel671f9a32019-06-28 13:36:21 -0700362 BUG_ON((load_pa % map_size) != 0);
363 BUG_ON(load_sz > MAX_EARLY_MAPPING_SIZE);
364
365 /* Setup early PGD for fixmap */
366 create_pgd_mapping(early_pg_dir, FIXADDR_START,
367 (uintptr_t)fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE);
Anup Patel6f1e9e92019-02-13 16:38:36 +0530368
369#ifndef __PAGETABLE_PMD_FOLDED
Anup Patel671f9a32019-06-28 13:36:21 -0700370 /* Setup fixmap PMD */
371 create_pmd_mapping(fixmap_pmd, FIXADDR_START,
372 (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE);
373 /* Setup trampoline PGD and PMD */
374 create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET,
375 (uintptr_t)trampoline_pmd, PGDIR_SIZE, PAGE_TABLE);
376 create_pmd_mapping(trampoline_pmd, PAGE_OFFSET,
377 load_pa, PMD_SIZE, PAGE_KERNEL_EXEC);
Anup Patel6f1e9e92019-02-13 16:38:36 +0530378#else
Anup Patel671f9a32019-06-28 13:36:21 -0700379 /* Setup trampoline PGD */
380 create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET,
381 load_pa, PGDIR_SIZE, PAGE_KERNEL_EXEC);
382#endif
Anup Patel6f1e9e92019-02-13 16:38:36 +0530383
Anup Patel671f9a32019-06-28 13:36:21 -0700384 /*
385 * Setup early PGD covering entire kernel which will allows
386 * us to reach paging_init(). We map all memory banks later
387 * in setup_vm_final() below.
388 */
389 end_va = PAGE_OFFSET + load_sz;
390 for (va = PAGE_OFFSET; va < end_va; va += map_size)
391 create_pgd_mapping(early_pg_dir, va,
392 load_pa + (va - PAGE_OFFSET),
393 map_size, PAGE_KERNEL_EXEC);
Anup Patelf2c17aa2019-01-07 20:57:01 +0530394
Anup Patel671f9a32019-06-28 13:36:21 -0700395 /* Create fixed mapping for early FDT parsing */
396 end_va = __fix_to_virt(FIX_FDT) + FIX_FDT_SIZE;
397 for (va = __fix_to_virt(FIX_FDT); va < end_va; va += PAGE_SIZE)
398 create_pte_mapping(fixmap_pte, va,
399 dtb_pa + (va - __fix_to_virt(FIX_FDT)),
400 PAGE_SIZE, PAGE_KERNEL);
401
402 /* Save pointer to DTB for early FDT parsing */
403 dtb_early_va = (void *)fix_to_virt(FIX_FDT) + (dtb_pa & ~PAGE_MASK);
Albert Ou922b0372019-09-27 16:14:18 -0700404 /* Save physical address for memblock reservation */
405 dtb_early_pa = dtb_pa;
Anup Patel671f9a32019-06-28 13:36:21 -0700406}
407
408static void __init setup_vm_final(void)
409{
410 uintptr_t va, map_size;
411 phys_addr_t pa, start, end;
412 struct memblock_region *reg;
413
414 /* Set mmu_enabled flag */
415 mmu_enabled = true;
416
417 /* Setup swapper PGD for fixmap */
418 create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
419 __pa(fixmap_pgd_next),
420 PGDIR_SIZE, PAGE_TABLE);
421
422 /* Map all memory banks */
423 for_each_memblock(memory, reg) {
424 start = reg->base;
425 end = start + reg->size;
426
427 if (start >= end)
428 break;
429 if (memblock_is_nomap(reg))
430 continue;
431 if (start <= __pa(PAGE_OFFSET) &&
432 __pa(PAGE_OFFSET) < end)
433 start = __pa(PAGE_OFFSET);
434
435 map_size = best_map_size(start, end - start);
436 for (pa = start; pa < end; pa += map_size) {
437 va = (uintptr_t)__va(pa);
438 create_pgd_mapping(swapper_pg_dir, va, pa,
439 map_size, PAGE_KERNEL_EXEC);
440 }
Anup Patel6f1e9e92019-02-13 16:38:36 +0530441 }
Anup Patelf2c17aa2019-01-07 20:57:01 +0530442
Anup Patel671f9a32019-06-28 13:36:21 -0700443 /* Clear fixmap PTE and PMD mappings */
444 clear_fixmap(FIX_PTE);
445 clear_fixmap(FIX_PMD);
446
447 /* Move to swapper page table */
Bin Meng4f3f9002019-08-07 09:13:38 -0700448 csr_write(CSR_SATP, PFN_DOWN(__pa(swapper_pg_dir)) | SATP_MODE);
Anup Patel671f9a32019-06-28 13:36:21 -0700449 local_flush_tlb_all();
450}
451
452void __init paging_init(void)
453{
454 setup_vm_final();
Logan Gunthorped95f1a52019-08-28 15:40:54 -0600455 memblocks_present();
456 sparse_init();
Anup Patel671f9a32019-06-28 13:36:21 -0700457 setup_zero_page();
458 zone_sizes_init();
Anup Patel6f1e9e92019-02-13 16:38:36 +0530459}
Logan Gunthorped95f1a52019-08-28 15:40:54 -0600460
461#ifdef CONFIG_SPARSEMEM
462int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
463 struct vmem_altmap *altmap)
464{
465 return vmemmap_populate_basepages(start, end, node);
466}
467#endif