1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
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>
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>
27 #include <asm/pgtable.h>
28 #include <asm/pgalloc.h>
29 #include <asm/mmu_context.h>
30 #include <asm/sections.h>
33 pgd_t swapper_pg_dir
[PTRS_PER_PGD
] __page_aligned_bss
;
34 pte_t invalid_pte_table
[PTRS_PER_PTE
] __page_aligned_bss
;
35 EXPORT_SYMBOL(invalid_pte_table
);
36 unsigned long empty_zero_page
[PAGE_SIZE
/ sizeof(unsigned long)]
38 EXPORT_SYMBOL(empty_zero_page
);
40 #ifdef CONFIG_BLK_DEV_INITRD
41 static void __init
setup_initrd(void)
45 if (initrd_start
>= initrd_end
) {
46 pr_err("initrd not found or empty");
50 if (__pa(initrd_end
) > PFN_PHYS(max_low_pfn
)) {
51 pr_err("initrd extends beyond end of memory");
55 size
= initrd_end
- initrd_start
;
57 if (memblock_is_region_reserved(__pa(initrd_start
), size
)) {
58 pr_err("INITRD: 0x%08lx+0x%08lx overlaps in-use memory region",
59 __pa(initrd_start
), size
);
63 memblock_reserve(__pa(initrd_start
), size
);
65 pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
66 (void *)(initrd_start
), size
);
68 initrd_below_start_ok
= 1;
73 initrd_start
= initrd_end
= 0;
75 pr_err(" - disabling initrd\n");
79 void __init
mem_init(void)
84 max_mapnr
= highend_pfn
;
86 max_mapnr
= max_low_pfn
;
88 high_memory
= (void *) __va(max_low_pfn
<< PAGE_SHIFT
);
90 #ifdef CONFIG_BLK_DEV_INITRD
97 for (tmp
= highstart_pfn
; tmp
< highend_pfn
; tmp
++) {
98 struct page
*page
= pfn_to_page(tmp
);
100 /* FIXME not sure about */
101 if (!memblock_is_reserved(tmp
<< PAGE_SHIFT
))
102 free_highmem_page(page
);
105 mem_init_print_info(NULL
);
108 extern char __init_begin
[], __init_end
[];
110 void free_initmem(void)
114 addr
= (unsigned long) &__init_begin
;
116 while (addr
< (unsigned long) &__init_end
) {
117 ClearPageReserved(virt_to_page(addr
));
118 init_page_count(virt_to_page(addr
));
120 totalram_pages_inc();
124 pr_info("Freeing unused kernel memory: %dk freed\n",
125 ((unsigned int)&__init_end
- (unsigned int)&__init_begin
) >> 10);
128 void pgd_init(unsigned long *p
)
132 for (i
= 0; i
< PTRS_PER_PGD
; i
++)
133 p
[i
] = __pa(invalid_pte_table
);
136 void __init
pre_mmu_init(void)
139 * Setup page-table and enable TLB-hardrefill
142 pgd_init((unsigned long *)swapper_pg_dir
);
143 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir
);
144 TLBMISS_HANDLER_SETUP_PGD_KERNEL(swapper_pg_dir
);
146 /* Setup page mask to 4k */
147 write_mmu_pagemask(0);
150 void __init
fixrange_init(unsigned long start
, unsigned long end
,
161 i
= __pgd_offset(vaddr
);
162 j
= __pud_offset(vaddr
);
163 k
= __pmd_offset(vaddr
);
166 for ( ; (i
< PTRS_PER_PGD
) && (vaddr
!= end
); pgd
++, i
++) {
168 for ( ; (j
< PTRS_PER_PUD
) && (vaddr
!= end
); pud
++, j
++) {
170 for (; (k
< PTRS_PER_PMD
) && (vaddr
!= end
); pmd
++, k
++) {
171 if (pmd_none(*pmd
)) {
172 pte
= (pte_t
*) memblock_alloc_low(PAGE_SIZE
, PAGE_SIZE
);
174 panic("%s: Failed to allocate %lu bytes align=%lx\n",
178 set_pmd(pmd
, __pmd(__pa(pte
)));
179 BUG_ON(pte
!= pte_offset_kernel(pmd
, 0));
189 void __init
fixaddr_init(void)
193 vaddr
= __fix_to_virt(__end_of_fixed_addresses
- 1) & PMD_MASK
;
194 fixrange_init(vaddr
, vaddr
+ PMD_SIZE
, swapper_pg_dir
);