2 * Copyright (C) 2012 Regents of the University of California
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/init.h>
16 #include <linux/memblock.h>
17 #include <linux/initrd.h>
18 #include <linux/swap.h>
19 #include <linux/sizes.h>
20 #include <linux/of_fdt.h>
22 #include <asm/fixmap.h>
23 #include <asm/tlbflush.h>
24 #include <asm/sections.h>
25 #include <asm/pgtable.h>
28 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
30 EXPORT_SYMBOL(empty_zero_page);
32 static void __init zone_sizes_init(void)
34 unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
36 #ifdef CONFIG_ZONE_DMA32
37 max_zone_pfns[ZONE_DMA32] = PFN_DOWN(min(4UL * SZ_1G,
38 (unsigned long) PFN_PHYS(max_low_pfn)));
40 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
42 free_area_init_nodes(max_zone_pfns);
45 void setup_zero_page(void)
47 memset((void *)empty_zero_page, 0, PAGE_SIZE);
50 void __init paging_init(void)
53 local_flush_tlb_all();
57 void __init mem_init(void)
61 #endif /* CONFIG_FLATMEM */
63 high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
66 mem_init_print_info(NULL);
69 void free_initmem(void)
71 free_initmem_default(0);
74 #ifdef CONFIG_BLK_DEV_INITRD
75 static void __init setup_initrd(void)
79 if (initrd_start >= initrd_end) {
80 pr_info("initrd not found or empty");
83 if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
84 pr_err("initrd extends beyond end of memory");
88 size = initrd_end - initrd_start;
89 memblock_reserve(__pa(initrd_start), size);
90 initrd_below_start_ok = 1;
92 pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
93 (void *)(initrd_start), size);
96 pr_cont(" - disabling initrd\n");
101 void __init free_initrd_mem(unsigned long start, unsigned long end)
103 free_reserved_area((void *)start, (void *)end, -1, "initrd");
105 #endif /* CONFIG_BLK_DEV_INITRD */
107 void __init setup_bootmem(void)
109 struct memblock_region *reg;
110 phys_addr_t mem_size = 0;
112 /* Find the memory region containing the kernel */
113 for_each_memblock(memory, reg) {
114 phys_addr_t vmlinux_end = __pa(_end);
115 phys_addr_t end = reg->base + reg->size;
117 if (reg->base <= vmlinux_end && vmlinux_end <= end) {
119 * Reserve from the start of the region to the end of
122 memblock_reserve(reg->base, vmlinux_end - reg->base);
123 mem_size = min(reg->size, (phys_addr_t)-PAGE_OFFSET);
126 BUG_ON(mem_size == 0);
128 set_max_mapnr(PFN_DOWN(mem_size));
129 max_low_pfn = PFN_DOWN(memblock_end_of_DRAM());
131 #ifdef CONFIG_BLK_DEV_INITRD
133 #endif /* CONFIG_BLK_DEV_INITRD */
135 early_init_fdt_reserve_self();
136 early_init_fdt_scan_reserved_mem();
137 memblock_allow_resize();
140 for_each_memblock(memory, reg) {
141 unsigned long start_pfn = memblock_region_memory_base_pfn(reg);
142 unsigned long end_pfn = memblock_region_memory_end_pfn(reg);
144 memblock_set_node(PFN_PHYS(start_pfn),
145 PFN_PHYS(end_pfn - start_pfn),
146 &memblock.memory, 0);
150 unsigned long va_pa_offset;
151 EXPORT_SYMBOL(va_pa_offset);
152 unsigned long pfn_base;
153 EXPORT_SYMBOL(pfn_base);
155 pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
156 pgd_t trampoline_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
158 #ifndef __PAGETABLE_PMD_FOLDED
159 #define NUM_SWAPPER_PMDS ((uintptr_t)-PAGE_OFFSET >> PGDIR_SHIFT)
160 pmd_t swapper_pmd[PTRS_PER_PMD*((-PAGE_OFFSET)/PGDIR_SIZE)] __page_aligned_bss;
161 pmd_t trampoline_pmd[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
162 pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
165 pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
167 void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
169 unsigned long addr = __fix_to_virt(idx);
172 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
174 ptep = &fixmap_pte[pte_index(addr)];
176 if (pgprot_val(prot)) {
177 set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
179 pte_clear(&init_mm, addr, ptep);
180 local_flush_tlb_page(addr);
185 * setup_vm() is called from head.S with MMU-off.
187 * Following requirements should be honoured for setup_vm() to work
189 * 1) It should use PC-relative addressing for accessing kernel symbols.
190 * To achieve this we always use GCC cmodel=medany.
191 * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
192 * so disable compiler instrumentation when FTRACE is enabled.
194 * Currently, the above requirements are honoured by using custom CFLAGS
195 * for init.o in mm/Makefile.
198 #ifndef __riscv_cmodel_medany
199 #error "setup_vm() is called from head.S before relocate so it should "
200 "not use absolute addressing."
203 asmlinkage void __init setup_vm(void)
207 uintptr_t pa = (uintptr_t) &_start;
208 pgprot_t prot = __pgprot(pgprot_val(PAGE_KERNEL) | _PAGE_EXEC);
210 va_pa_offset = PAGE_OFFSET - pa;
211 pfn_base = PFN_DOWN(pa);
213 /* Sanity check alignment and size */
214 BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
215 BUG_ON((pa % (PAGE_SIZE * PTRS_PER_PTE)) != 0);
217 #ifndef __PAGETABLE_PMD_FOLDED
218 trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
219 pfn_pgd(PFN_DOWN((uintptr_t)trampoline_pmd),
220 __pgprot(_PAGE_TABLE));
221 trampoline_pmd[0] = pfn_pmd(PFN_DOWN(pa), prot);
223 for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
224 size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
227 pfn_pgd(PFN_DOWN((uintptr_t)swapper_pmd) + i,
228 __pgprot(_PAGE_TABLE));
230 for (i = 0; i < ARRAY_SIZE(swapper_pmd); i++)
231 swapper_pmd[i] = pfn_pmd(PFN_DOWN(pa + i * PMD_SIZE), prot);
233 swapper_pg_dir[(FIXADDR_START >> PGDIR_SHIFT) % PTRS_PER_PGD] =
234 pfn_pgd(PFN_DOWN((uintptr_t)fixmap_pmd),
235 __pgprot(_PAGE_TABLE));
236 fixmap_pmd[(FIXADDR_START >> PMD_SHIFT) % PTRS_PER_PMD] =
237 pfn_pmd(PFN_DOWN((uintptr_t)fixmap_pte),
238 __pgprot(_PAGE_TABLE));
240 trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
241 pfn_pgd(PFN_DOWN(pa), prot);
243 for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
244 size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
247 pfn_pgd(PFN_DOWN(pa + i * PGDIR_SIZE), prot);
250 swapper_pg_dir[(FIXADDR_START >> PGDIR_SHIFT) % PTRS_PER_PGD] =
251 pfn_pgd(PFN_DOWN((uintptr_t)fixmap_pte),
252 __pgprot(_PAGE_TABLE));