Merge tag 'sched-urgent-2020-12-27' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / arch / csky / mm / init.c
blobaf627128314f81867a10af034e1923936a59f195
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
4 #include <linux/bug.h>
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/signal.h>
8 #include <linux/sched.h>
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/string.h>
12 #include <linux/types.h>
13 #include <linux/pagemap.h>
14 #include <linux/ptrace.h>
15 #include <linux/mman.h>
16 #include <linux/mm.h>
17 #include <linux/highmem.h>
18 #include <linux/memblock.h>
19 #include <linux/swap.h>
20 #include <linux/proc_fs.h>
21 #include <linux/pfn.h>
22 #include <linux/initrd.h>
24 #include <asm/setup.h>
25 #include <asm/cachectl.h>
26 #include <asm/dma.h>
27 #include <asm/pgalloc.h>
28 #include <asm/mmu_context.h>
29 #include <asm/sections.h>
30 #include <asm/tlb.h>
32 pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
33 pte_t invalid_pte_table[PTRS_PER_PTE] __page_aligned_bss;
34 EXPORT_SYMBOL(invalid_pte_table);
35 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
36 __page_aligned_bss;
37 EXPORT_SYMBOL(empty_zero_page);
39 #ifdef CONFIG_BLK_DEV_INITRD
40 static void __init setup_initrd(void)
42 unsigned long size;
44 if (initrd_start >= initrd_end) {
45 pr_err("initrd not found or empty");
46 goto disable;
49 if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
50 pr_err("initrd extends beyond end of memory");
51 goto disable;
54 size = initrd_end - initrd_start;
56 if (memblock_is_region_reserved(__pa(initrd_start), size)) {
57 pr_err("INITRD: 0x%08lx+0x%08lx overlaps in-use memory region",
58 __pa(initrd_start), size);
59 goto disable;
62 memblock_reserve(__pa(initrd_start), size);
64 pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
65 (void *)(initrd_start), size);
67 initrd_below_start_ok = 1;
69 return;
71 disable:
72 initrd_start = initrd_end = 0;
74 pr_err(" - disabling initrd\n");
76 #endif
78 void __init mem_init(void)
80 #ifdef CONFIG_HIGHMEM
81 unsigned long tmp;
83 max_mapnr = highend_pfn;
84 #else
85 max_mapnr = max_low_pfn;
86 #endif
87 high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
89 #ifdef CONFIG_BLK_DEV_INITRD
90 setup_initrd();
91 #endif
93 memblock_free_all();
95 #ifdef CONFIG_HIGHMEM
96 for (tmp = highstart_pfn; tmp < highend_pfn; tmp++) {
97 struct page *page = pfn_to_page(tmp);
99 /* FIXME not sure about */
100 if (!memblock_is_reserved(tmp << PAGE_SHIFT))
101 free_highmem_page(page);
103 #endif
104 mem_init_print_info(NULL);
107 extern char __init_begin[], __init_end[];
109 void free_initmem(void)
111 unsigned long addr;
113 addr = (unsigned long) &__init_begin;
115 while (addr < (unsigned long) &__init_end) {
116 ClearPageReserved(virt_to_page(addr));
117 init_page_count(virt_to_page(addr));
118 free_page(addr);
119 totalram_pages_inc();
120 addr += PAGE_SIZE;
123 pr_info("Freeing unused kernel memory: %dk freed\n",
124 ((unsigned int)&__init_end - (unsigned int)&__init_begin) >> 10);
127 void pgd_init(unsigned long *p)
129 int i;
131 for (i = 0; i < PTRS_PER_PGD; i++)
132 p[i] = __pa(invalid_pte_table);
135 void __init pre_mmu_init(void)
138 * Setup page-table and enable TLB-hardrefill
140 flush_tlb_all();
141 pgd_init((unsigned long *)swapper_pg_dir);
142 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir);
143 TLBMISS_HANDLER_SETUP_PGD_KERNEL(swapper_pg_dir);
145 /* Setup page mask to 4k */
146 write_mmu_pagemask(0);
149 void __init fixrange_init(unsigned long start, unsigned long end,
150 pgd_t *pgd_base)
152 pgd_t *pgd;
153 pud_t *pud;
154 pmd_t *pmd;
155 pte_t *pte;
156 int i, j, k;
157 unsigned long vaddr;
159 vaddr = start;
160 i = pgd_index(vaddr);
161 j = pud_index(vaddr);
162 k = pmd_index(vaddr);
163 pgd = pgd_base + i;
165 for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
166 pud = (pud_t *)pgd;
167 for ( ; (j < PTRS_PER_PUD) && (vaddr != end); pud++, j++) {
168 pmd = (pmd_t *)pud;
169 for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) {
170 if (pmd_none(*pmd)) {
171 pte = (pte_t *) memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
172 if (!pte)
173 panic("%s: Failed to allocate %lu bytes align=%lx\n",
174 __func__, PAGE_SIZE,
175 PAGE_SIZE);
177 set_pmd(pmd, __pmd(__pa(pte)));
178 BUG_ON(pte != pte_offset_kernel(pmd, 0));
180 vaddr += PMD_SIZE;
182 k = 0;
184 j = 0;
188 void __init fixaddr_init(void)
190 unsigned long vaddr;
192 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
193 fixrange_init(vaddr, vaddr + PMD_SIZE, swapper_pg_dir);