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 #ifdef CONFIG_BLK_DEV_INITRD
70 static void __init setup_initrd(void)
74 if (initrd_start >= initrd_end) {
75 pr_info("initrd not found or empty");
78 if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
79 pr_err("initrd extends beyond end of memory");
83 size = initrd_end - initrd_start;
84 memblock_reserve(__pa(initrd_start), size);
85 initrd_below_start_ok = 1;
87 pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
88 (void *)(initrd_start), size);
91 pr_cont(" - disabling initrd\n");
96 void __init free_initrd_mem(unsigned long start, unsigned long end)
98 free_reserved_area((void *)start, (void *)end, -1, "initrd");
100 #endif /* CONFIG_BLK_DEV_INITRD */
102 void __init setup_bootmem(void)
104 struct memblock_region *reg;
105 phys_addr_t mem_size = 0;
107 /* Find the memory region containing the kernel */
108 for_each_memblock(memory, reg) {
109 phys_addr_t vmlinux_end = __pa(_end);
110 phys_addr_t end = reg->base + reg->size;
112 if (reg->base <= vmlinux_end && vmlinux_end <= end) {
114 * Reserve from the start of the region to the end of
117 memblock_reserve(reg->base, vmlinux_end - reg->base);
118 mem_size = min(reg->size, (phys_addr_t)-PAGE_OFFSET);
121 * Remove memblock from the end of usable area to the
124 if (reg->base + mem_size < end)
125 memblock_remove(reg->base + mem_size,
126 end - reg->base - mem_size);
129 BUG_ON(mem_size == 0);
131 set_max_mapnr(PFN_DOWN(mem_size));
132 max_low_pfn = PFN_DOWN(memblock_end_of_DRAM());
134 #ifdef CONFIG_BLK_DEV_INITRD
136 #endif /* CONFIG_BLK_DEV_INITRD */
138 early_init_fdt_reserve_self();
139 early_init_fdt_scan_reserved_mem();
140 memblock_allow_resize();
143 for_each_memblock(memory, reg) {
144 unsigned long start_pfn = memblock_region_memory_base_pfn(reg);
145 unsigned long end_pfn = memblock_region_memory_end_pfn(reg);
147 memblock_set_node(PFN_PHYS(start_pfn),
148 PFN_PHYS(end_pfn - start_pfn),
149 &memblock.memory, 0);
153 unsigned long va_pa_offset;
154 EXPORT_SYMBOL(va_pa_offset);
155 unsigned long pfn_base;
156 EXPORT_SYMBOL(pfn_base);
158 pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
159 pgd_t trampoline_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
161 #ifndef __PAGETABLE_PMD_FOLDED
162 #define NUM_SWAPPER_PMDS ((uintptr_t)-PAGE_OFFSET >> PGDIR_SHIFT)
163 pmd_t swapper_pmd[PTRS_PER_PMD*((-PAGE_OFFSET)/PGDIR_SIZE)] __page_aligned_bss;
164 pmd_t trampoline_pmd[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
165 pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
168 pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
170 void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
172 unsigned long addr = __fix_to_virt(idx);
175 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
177 ptep = &fixmap_pte[pte_index(addr)];
179 if (pgprot_val(prot)) {
180 set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
182 pte_clear(&init_mm, addr, ptep);
183 local_flush_tlb_page(addr);
188 * setup_vm() is called from head.S with MMU-off.
190 * Following requirements should be honoured for setup_vm() to work
192 * 1) It should use PC-relative addressing for accessing kernel symbols.
193 * To achieve this we always use GCC cmodel=medany.
194 * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
195 * so disable compiler instrumentation when FTRACE is enabled.
197 * Currently, the above requirements are honoured by using custom CFLAGS
198 * for init.o in mm/Makefile.
201 #ifndef __riscv_cmodel_medany
202 #error "setup_vm() is called from head.S before relocate so it should "
203 "not use absolute addressing."
206 asmlinkage void __init setup_vm(void)
210 uintptr_t pa = (uintptr_t) &_start;
211 pgprot_t prot = __pgprot(pgprot_val(PAGE_KERNEL) | _PAGE_EXEC);
213 va_pa_offset = PAGE_OFFSET - pa;
214 pfn_base = PFN_DOWN(pa);
216 /* Sanity check alignment and size */
217 BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
218 BUG_ON((pa % (PAGE_SIZE * PTRS_PER_PTE)) != 0);
220 #ifndef __PAGETABLE_PMD_FOLDED
221 trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
222 pfn_pgd(PFN_DOWN((uintptr_t)trampoline_pmd),
223 __pgprot(_PAGE_TABLE));
224 trampoline_pmd[0] = pfn_pmd(PFN_DOWN(pa), prot);
226 for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
227 size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
230 pfn_pgd(PFN_DOWN((uintptr_t)swapper_pmd) + i,
231 __pgprot(_PAGE_TABLE));
233 for (i = 0; i < ARRAY_SIZE(swapper_pmd); i++)
234 swapper_pmd[i] = pfn_pmd(PFN_DOWN(pa + i * PMD_SIZE), prot);
236 swapper_pg_dir[(FIXADDR_START >> PGDIR_SHIFT) % PTRS_PER_PGD] =
237 pfn_pgd(PFN_DOWN((uintptr_t)fixmap_pmd),
238 __pgprot(_PAGE_TABLE));
239 fixmap_pmd[(FIXADDR_START >> PMD_SHIFT) % PTRS_PER_PMD] =
240 pfn_pmd(PFN_DOWN((uintptr_t)fixmap_pte),
241 __pgprot(_PAGE_TABLE));
243 trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
244 pfn_pgd(PFN_DOWN(pa), prot);
246 for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
247 size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
250 pfn_pgd(PFN_DOWN(pa + i * PGDIR_SIZE), prot);
253 swapper_pg_dir[(FIXADDR_START >> PGDIR_SHIFT) % PTRS_PER_PGD] =
254 pfn_pgd(PFN_DOWN((uintptr_t)fixmap_pte),
255 __pgprot(_PAGE_TABLE));