dm integrity: fix deadlock with overlapping I/O
[sfrench/cifs-2.6.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 static void __init zone_sizes_init(void)
29 {
30         unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
31
32 #ifdef CONFIG_ZONE_DMA32
33         max_zone_pfns[ZONE_DMA32] = PFN_DOWN(min(4UL * SZ_1G,
34                         (unsigned long) PFN_PHYS(max_low_pfn)));
35 #endif
36         max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
37
38         free_area_init_nodes(max_zone_pfns);
39 }
40
41 void setup_zero_page(void)
42 {
43         memset((void *)empty_zero_page, 0, PAGE_SIZE);
44 }
45
46 void __init paging_init(void)
47 {
48         setup_zero_page();
49         local_flush_tlb_all();
50         zone_sizes_init();
51 }
52
53 void __init mem_init(void)
54 {
55 #ifdef CONFIG_FLATMEM
56         BUG_ON(!mem_map);
57 #endif /* CONFIG_FLATMEM */
58
59         high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
60         memblock_free_all();
61
62         mem_init_print_info(NULL);
63 }
64
65 void free_initmem(void)
66 {
67         free_initmem_default(0);
68 }
69
70 #ifdef CONFIG_BLK_DEV_INITRD
71 static void __init setup_initrd(void)
72 {
73         unsigned long size;
74
75         if (initrd_start >= initrd_end) {
76                 pr_info("initrd not found or empty");
77                 goto disable;
78         }
79         if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
80                 pr_err("initrd extends beyond end of memory");
81                 goto disable;
82         }
83
84         size = initrd_end - initrd_start;
85         memblock_reserve(__pa(initrd_start), size);
86         initrd_below_start_ok = 1;
87
88         pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
89                 (void *)(initrd_start), size);
90         return;
91 disable:
92         pr_cont(" - disabling initrd\n");
93         initrd_start = 0;
94         initrd_end = 0;
95 }
96
97 void __init free_initrd_mem(unsigned long start, unsigned long end)
98 {
99         free_reserved_area((void *)start, (void *)end, -1, "initrd");
100 }
101 #endif /* CONFIG_BLK_DEV_INITRD */
102
103 void __init setup_bootmem(void)
104 {
105         struct memblock_region *reg;
106         phys_addr_t mem_size = 0;
107
108         /* Find the memory region containing the kernel */
109         for_each_memblock(memory, reg) {
110                 phys_addr_t vmlinux_end = __pa(_end);
111                 phys_addr_t end = reg->base + reg->size;
112
113                 if (reg->base <= vmlinux_end && vmlinux_end <= end) {
114                         /*
115                          * Reserve from the start of the region to the end of
116                          * the kernel
117                          */
118                         memblock_reserve(reg->base, vmlinux_end - reg->base);
119                         mem_size = min(reg->size, (phys_addr_t)-PAGE_OFFSET);
120                 }
121         }
122         BUG_ON(mem_size == 0);
123
124         set_max_mapnr(PFN_DOWN(mem_size));
125         max_low_pfn = PFN_DOWN(memblock_end_of_DRAM());
126
127 #ifdef CONFIG_BLK_DEV_INITRD
128         setup_initrd();
129 #endif /* CONFIG_BLK_DEV_INITRD */
130
131         early_init_fdt_reserve_self();
132         early_init_fdt_scan_reserved_mem();
133         memblock_allow_resize();
134         memblock_dump_all();
135
136         for_each_memblock(memory, reg) {
137                 unsigned long start_pfn = memblock_region_memory_base_pfn(reg);
138                 unsigned long end_pfn = memblock_region_memory_end_pfn(reg);
139
140                 memblock_set_node(PFN_PHYS(start_pfn),
141                                   PFN_PHYS(end_pfn - start_pfn),
142                                   &memblock.memory, 0);
143         }
144 }
145
146 pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
147 pgd_t trampoline_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
148
149 #ifndef __PAGETABLE_PMD_FOLDED
150 #define NUM_SWAPPER_PMDS ((uintptr_t)-PAGE_OFFSET >> PGDIR_SHIFT)
151 pmd_t swapper_pmd[PTRS_PER_PMD*((-PAGE_OFFSET)/PGDIR_SIZE)] __page_aligned_bss;
152 pmd_t trampoline_pmd[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
153 pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
154 #endif
155
156 pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
157
158 void __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
175 asmlinkage void __init setup_vm(void)
176 {
177         extern char _start;
178         uintptr_t i;
179         uintptr_t pa = (uintptr_t) &_start;
180         pgprot_t prot = __pgprot(pgprot_val(PAGE_KERNEL) | _PAGE_EXEC);
181
182         va_pa_offset = PAGE_OFFSET - pa;
183         pfn_base = PFN_DOWN(pa);
184
185         /* Sanity check alignment and size */
186         BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
187         BUG_ON((pa % (PAGE_SIZE * PTRS_PER_PTE)) != 0);
188
189 #ifndef __PAGETABLE_PMD_FOLDED
190         trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
191                 pfn_pgd(PFN_DOWN((uintptr_t)trampoline_pmd),
192                         __pgprot(_PAGE_TABLE));
193         trampoline_pmd[0] = pfn_pmd(PFN_DOWN(pa), prot);
194
195         for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
196                 size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
197
198                 swapper_pg_dir[o] =
199                         pfn_pgd(PFN_DOWN((uintptr_t)swapper_pmd) + i,
200                                 __pgprot(_PAGE_TABLE));
201         }
202         for (i = 0; i < ARRAY_SIZE(swapper_pmd); i++)
203                 swapper_pmd[i] = pfn_pmd(PFN_DOWN(pa + i * PMD_SIZE), prot);
204
205         swapper_pg_dir[(FIXADDR_START >> PGDIR_SHIFT) % PTRS_PER_PGD] =
206                 pfn_pgd(PFN_DOWN((uintptr_t)fixmap_pmd),
207                                 __pgprot(_PAGE_TABLE));
208         fixmap_pmd[(FIXADDR_START >> PMD_SHIFT) % PTRS_PER_PMD] =
209                 pfn_pmd(PFN_DOWN((uintptr_t)fixmap_pte),
210                                 __pgprot(_PAGE_TABLE));
211 #else
212         trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
213                 pfn_pgd(PFN_DOWN(pa), prot);
214
215         for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
216                 size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
217
218                 swapper_pg_dir[o] =
219                         pfn_pgd(PFN_DOWN(pa + i * PGDIR_SIZE), prot);
220         }
221
222         swapper_pg_dir[(FIXADDR_START >> PGDIR_SHIFT) % PTRS_PER_PGD] =
223                 pfn_pgd(PFN_DOWN((uintptr_t)fixmap_pte),
224                                 __pgprot(_PAGE_TABLE));
225 #endif
226 }