]> Git Repo - linux.git/blob - arch/nds32/mm/init.c
mm/page_alloc: free pages in a single pass during bulk free
[linux.git] / arch / nds32 / mm / init.c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 1995-2005 Russell King
3 // Copyright (C) 2012 ARM Ltd.
4 // Copyright (C) 2013-2017 Andes Technology Corporation
5
6 #include <linux/kernel.h>
7 #include <linux/errno.h>
8 #include <linux/swap.h>
9 #include <linux/init.h>
10 #include <linux/memblock.h>
11 #include <linux/mman.h>
12 #include <linux/nodemask.h>
13 #include <linux/initrd.h>
14 #include <linux/highmem.h>
15
16 #include <asm/sections.h>
17 #include <asm/setup.h>
18 #include <asm/tlb.h>
19 #include <asm/page.h>
20
21 DEFINE_SPINLOCK(anon_alias_lock);
22 extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
23
24 /*
25  * empty_zero_page is a special page that is used for
26  * zero-initialized data and COW.
27  */
28 struct page *empty_zero_page;
29 EXPORT_SYMBOL(empty_zero_page);
30
31 static void __init zone_sizes_init(void)
32 {
33         unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
34
35         max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
36 #ifdef CONFIG_HIGHMEM
37         max_zone_pfn[ZONE_HIGHMEM] = max_pfn;
38 #endif
39         free_area_init(max_zone_pfn);
40
41 }
42
43 /*
44  * Map all physical memory under high_memory into kernel's address space.
45  *
46  * This is explicitly coded for two-level page tables, so if you need
47  * something else then this needs to change.
48  */
49 static void __init map_ram(void)
50 {
51         unsigned long v, p, e;
52         pgd_t *pge;
53         p4d_t *p4e;
54         pud_t *pue;
55         pmd_t *pme;
56         pte_t *pte;
57         /* These mark extents of read-only kernel pages...
58          * ...from vmlinux.lds.S
59          */
60
61         p = (u32) memblock_start_of_DRAM() & PAGE_MASK;
62         e = min((u32) memblock_end_of_DRAM(), (u32) __pa(high_memory));
63
64         v = (u32) __va(p);
65         pge = pgd_offset_k(v);
66
67         while (p < e) {
68                 int j;
69                 p4e = p4d_offset(pge, v);
70                 pue = pud_offset(p4e, v);
71                 pme = pmd_offset(pue, v);
72
73                 if ((u32) pue != (u32) pge || (u32) pme != (u32) pge) {
74                         panic("%s: Kernel hardcoded for "
75                               "two-level page tables", __func__);
76                 }
77
78                 /* Alloc one page for holding PTE's... */
79                 pte = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
80                 if (!pte)
81                         panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
82                               __func__, PAGE_SIZE, PAGE_SIZE);
83                 set_pmd(pme, __pmd(__pa(pte) + _PAGE_KERNEL_TABLE));
84
85                 /* Fill the newly allocated page with PTE'S */
86                 for (j = 0; p < e && j < PTRS_PER_PTE;
87                      v += PAGE_SIZE, p += PAGE_SIZE, j++, pte++) {
88                         /* Create mapping between p and v. */
89                         /* TODO: more fine grant for page access permission */
90                         set_pte(pte, __pte(p + pgprot_val(PAGE_KERNEL)));
91                 }
92
93                 pge++;
94         }
95 }
96 static pmd_t *fixmap_pmd_p;
97 static void __init fixedrange_init(void)
98 {
99         unsigned long vaddr;
100         pmd_t *pmd;
101 #ifdef CONFIG_HIGHMEM
102         pte_t *pte;
103 #endif /* CONFIG_HIGHMEM */
104
105         /*
106          * Fixed mappings:
107          */
108         vaddr = __fix_to_virt(__end_of_fixed_addresses - 1);
109         pmd = pmd_off_k(vaddr);
110         fixmap_pmd_p = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
111         if (!fixmap_pmd_p)
112                 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
113                       __func__, PAGE_SIZE, PAGE_SIZE);
114         set_pmd(pmd, __pmd(__pa(fixmap_pmd_p) + _PAGE_KERNEL_TABLE));
115
116 #ifdef CONFIG_HIGHMEM
117         /*
118          * Permanent kmaps:
119          */
120         vaddr = PKMAP_BASE;
121
122         pmd = pmd_off_k(vaddr);
123         pte = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
124         if (!pte)
125                 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
126                       __func__, PAGE_SIZE, PAGE_SIZE);
127         set_pmd(pmd, __pmd(__pa(pte) + _PAGE_KERNEL_TABLE));
128         pkmap_page_table = pte;
129 #endif /* CONFIG_HIGHMEM */
130 }
131
132 /*
133  * paging_init() sets up the page tables, initialises the zone memory
134  * maps, and sets up the zero page, bad page and bad page tables.
135  */
136 void __init paging_init(void)
137 {
138         int i;
139         void *zero_page;
140
141         pr_info("Setting up paging and PTEs.\n");
142         /* clear out the init_mm.pgd that will contain the kernel's mappings */
143         for (i = 0; i < PTRS_PER_PGD; i++)
144                 swapper_pg_dir[i] = __pgd(1);
145
146         map_ram();
147
148         fixedrange_init();
149
150         /* allocate space for empty_zero_page */
151         zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
152         if (!zero_page)
153                 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
154                       __func__, PAGE_SIZE, PAGE_SIZE);
155         zone_sizes_init();
156
157         empty_zero_page = virt_to_page(zero_page);
158         flush_dcache_page(empty_zero_page);
159 }
160
161 static inline void __init free_highmem(void)
162 {
163 #ifdef CONFIG_HIGHMEM
164         unsigned long pfn;
165         for (pfn = PFN_UP(__pa(high_memory)); pfn < max_pfn; pfn++) {
166                 phys_addr_t paddr = (phys_addr_t) pfn << PAGE_SHIFT;
167                 if (!memblock_is_reserved(paddr))
168                         free_highmem_page(pfn_to_page(pfn));
169         }
170 #endif
171 }
172
173 static void __init set_max_mapnr_init(void)
174 {
175         max_mapnr = max_pfn;
176 }
177
178 /*
179  * mem_init() marks the free areas in the mem_map and tells us how much
180  * memory is free.  This is done after various parts of the system have
181  * claimed their memory after the kernel image.
182  */
183 void __init mem_init(void)
184 {
185         phys_addr_t memory_start = memblock_start_of_DRAM();
186         BUG_ON(!mem_map);
187         set_max_mapnr_init();
188
189         free_highmem();
190
191         /* this will put all low memory onto the freelists */
192         memblock_free_all();
193
194         pr_info("virtual kernel memory layout:\n"
195                 "    fixmap  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
196 #ifdef CONFIG_HIGHMEM
197                 "    pkmap   : 0x%08lx - 0x%08lx   (%4ld kB)\n"
198 #endif
199                 "    consist : 0x%08lx - 0x%08lx   (%4ld MB)\n"
200                 "    vmalloc : 0x%08lx - 0x%08lx   (%4ld MB)\n"
201                 "    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB)\n"
202                 "      .init : 0x%08lx - 0x%08lx   (%4ld kB)\n"
203                 "      .data : 0x%08lx - 0x%08lx   (%4ld kB)\n"
204                 "      .text : 0x%08lx - 0x%08lx   (%4ld kB)\n",
205                 FIXADDR_START, FIXADDR_TOP, (FIXADDR_TOP - FIXADDR_START) >> 10,
206 #ifdef CONFIG_HIGHMEM
207                 PKMAP_BASE, PKMAP_BASE + LAST_PKMAP * PAGE_SIZE,
208                 (LAST_PKMAP * PAGE_SIZE) >> 10,
209 #endif
210                 CONSISTENT_BASE, CONSISTENT_END,
211                 ((CONSISTENT_END) - (CONSISTENT_BASE)) >> 20, VMALLOC_START,
212                 (unsigned long)VMALLOC_END, (VMALLOC_END - VMALLOC_START) >> 20,
213                 (unsigned long)__va(memory_start), (unsigned long)high_memory,
214                 ((unsigned long)high_memory -
215                  (unsigned long)__va(memory_start)) >> 20,
216                 (unsigned long)&__init_begin, (unsigned long)&__init_end,
217                 ((unsigned long)&__init_end -
218                  (unsigned long)&__init_begin) >> 10, (unsigned long)&_etext,
219                 (unsigned long)&_edata,
220                 ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
221                 (unsigned long)&_text, (unsigned long)&_etext,
222                 ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
223
224         /*
225          * Check boundaries twice: Some fundamental inconsistencies can
226          * be detected at build time already.
227          */
228 #ifdef CONFIG_HIGHMEM
229         BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > FIXADDR_START);
230         BUILD_BUG_ON((CONSISTENT_END) > PKMAP_BASE);
231 #endif
232         BUILD_BUG_ON(VMALLOC_END > CONSISTENT_BASE);
233         BUILD_BUG_ON(VMALLOC_START >= VMALLOC_END);
234
235 #ifdef CONFIG_HIGHMEM
236         BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > FIXADDR_START);
237         BUG_ON(CONSISTENT_END > PKMAP_BASE);
238 #endif
239         BUG_ON(VMALLOC_END > CONSISTENT_BASE);
240         BUG_ON(VMALLOC_START >= VMALLOC_END);
241         BUG_ON((unsigned long)high_memory > VMALLOC_START);
242
243         return;
244 }
245
246 void __set_fixmap(enum fixed_addresses idx,
247                                phys_addr_t phys, pgprot_t flags)
248 {
249         unsigned long addr = __fix_to_virt(idx);
250         pte_t *pte;
251
252         BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
253
254         pte = (pte_t *)&fixmap_pmd_p[pte_index(addr)];
255
256         if (pgprot_val(flags)) {
257                 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
258         } else {
259                 pte_clear(&init_mm, addr, pte);
260                 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
261         }
262 }
This page took 0.047978 seconds and 4 git commands to generate.