Merge tag 'libnvdimm-fixes-5.1-rc6' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / arch / riscv / mm / init.c
1 /*
2  * Copyright (C) 2012 Regents of the University of California
3  *
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.
7  *
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.
12  */
13
14 #include <linux/init.h>
15 #include <linux/mm.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>
21
22 #include <asm/fixmap.h>
23 #include <asm/tlbflush.h>
24 #include <asm/sections.h>
25 #include <asm/pgtable.h>
26 #include <asm/io.h>
27
28 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
29                                                         __page_aligned_bss;
30 EXPORT_SYMBOL(empty_zero_page);
31
32 static void __init zone_sizes_init(void)
33 {
34         unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
35
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)));
39 #endif
40         max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
41
42         free_area_init_nodes(max_zone_pfns);
43 }
44
45 void setup_zero_page(void)
46 {
47         memset((void *)empty_zero_page, 0, PAGE_SIZE);
48 }
49
50 void __init paging_init(void)
51 {
52         setup_zero_page();
53         local_flush_tlb_all();
54         zone_sizes_init();
55 }
56
57 void __init mem_init(void)
58 {
59 #ifdef CONFIG_FLATMEM
60         BUG_ON(!mem_map);
61 #endif /* CONFIG_FLATMEM */
62
63         high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
64         memblock_free_all();
65
66         mem_init_print_info(NULL);
67 }
68
69 void free_initmem(void)
70 {
71         free_initmem_default(0);
72 }
73
74 #ifdef CONFIG_BLK_DEV_INITRD
75 static void __init setup_initrd(void)
76 {
77         unsigned long size;
78
79         if (initrd_start >= initrd_end) {
80                 pr_info("initrd not found or empty");
81                 goto disable;
82         }
83         if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
84                 pr_err("initrd extends beyond end of memory");
85                 goto disable;
86         }
87
88         size = initrd_end - initrd_start;
89         memblock_reserve(__pa(initrd_start), size);
90         initrd_below_start_ok = 1;
91
92         pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
93                 (void *)(initrd_start), size);
94         return;
95 disable:
96         pr_cont(" - disabling initrd\n");
97         initrd_start = 0;
98         initrd_end = 0;
99 }
100
101 void __init free_initrd_mem(unsigned long start, unsigned long end)
102 {
103         free_reserved_area((void *)start, (void *)end, -1, "initrd");
104 }
105 #endif /* CONFIG_BLK_DEV_INITRD */
106
107 void __init setup_bootmem(void)
108 {
109         struct memblock_region *reg;
110         phys_addr_t mem_size = 0;
111
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;
116
117                 if (reg->base <= vmlinux_end && vmlinux_end <= end) {
118                         /*
119                          * Reserve from the start of the region to the end of
120                          * the kernel
121                          */
122                         memblock_reserve(reg->base, vmlinux_end - reg->base);
123                         mem_size = min(reg->size, (phys_addr_t)-PAGE_OFFSET);
124                 }
125         }
126         BUG_ON(mem_size == 0);
127
128         set_max_mapnr(PFN_DOWN(mem_size));
129         max_low_pfn = PFN_DOWN(memblock_end_of_DRAM());
130
131 #ifdef CONFIG_BLK_DEV_INITRD
132         setup_initrd();
133 #endif /* CONFIG_BLK_DEV_INITRD */
134
135         early_init_fdt_reserve_self();
136         early_init_fdt_scan_reserved_mem();
137         memblock_allow_resize();
138         memblock_dump_all();
139
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);
143
144                 memblock_set_node(PFN_PHYS(start_pfn),
145                                   PFN_PHYS(end_pfn - start_pfn),
146                                   &memblock.memory, 0);
147         }
148 }
149
150 unsigned long va_pa_offset;
151 EXPORT_SYMBOL(va_pa_offset);
152 unsigned long pfn_base;
153 EXPORT_SYMBOL(pfn_base);
154
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);
157
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;
163 #endif
164
165 pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
166
167 void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
168 {
169         unsigned long addr = __fix_to_virt(idx);
170         pte_t *ptep;
171
172         BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
173
174         ptep = &fixmap_pte[pte_index(addr)];
175
176         if (pgprot_val(prot)) {
177                 set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
178         } else {
179                 pte_clear(&init_mm, addr, ptep);
180                 local_flush_tlb_page(addr);
181         }
182 }
183
184 /*
185  * setup_vm() is called from head.S with MMU-off.
186  *
187  * Following requirements should be honoured for setup_vm() to work
188  * correctly:
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.
193  *
194  * Currently, the above requirements are honoured by using custom CFLAGS
195  * for init.o in mm/Makefile.
196  */
197
198 #ifndef __riscv_cmodel_medany
199 #error "setup_vm() is called from head.S before relocate so it should "
200         "not use absolute addressing."
201 #endif
202
203 asmlinkage void __init setup_vm(void)
204 {
205         extern char _start;
206         uintptr_t i;
207         uintptr_t pa = (uintptr_t) &_start;
208         pgprot_t prot = __pgprot(pgprot_val(PAGE_KERNEL) | _PAGE_EXEC);
209
210         va_pa_offset = PAGE_OFFSET - pa;
211         pfn_base = PFN_DOWN(pa);
212
213         /* Sanity check alignment and size */
214         BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
215         BUG_ON((pa % (PAGE_SIZE * PTRS_PER_PTE)) != 0);
216
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);
222
223         for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
224                 size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
225
226                 swapper_pg_dir[o] =
227                         pfn_pgd(PFN_DOWN((uintptr_t)swapper_pmd) + i,
228                                 __pgprot(_PAGE_TABLE));
229         }
230         for (i = 0; i < ARRAY_SIZE(swapper_pmd); i++)
231                 swapper_pmd[i] = pfn_pmd(PFN_DOWN(pa + i * PMD_SIZE), prot);
232
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));
239 #else
240         trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
241                 pfn_pgd(PFN_DOWN(pa), prot);
242
243         for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
244                 size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
245
246                 swapper_pg_dir[o] =
247                         pfn_pgd(PFN_DOWN(pa + i * PGDIR_SIZE), prot);
248         }
249
250         swapper_pg_dir[(FIXADDR_START >> PGDIR_SHIFT) % PTRS_PER_PGD] =
251                 pfn_pgd(PFN_DOWN((uintptr_t)fixmap_pte),
252                                 __pgprot(_PAGE_TABLE));
253 #endif
254 }