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/pgalloc.h>
28 #include <asm/mmu_context.h>
29 #include <asm/sections.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)]
37 EXPORT_SYMBOL(empty_zero_page
);
39 #ifdef CONFIG_BLK_DEV_INITRD
40 static void __init
setup_initrd(void)
44 if (initrd_start
>= initrd_end
) {
45 pr_err("initrd not found or empty");
49 if (__pa(initrd_end
) > PFN_PHYS(max_low_pfn
)) {
50 pr_err("initrd extends beyond end of memory");
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
);
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;
72 initrd_start
= initrd_end
= 0;
74 pr_err(" - disabling initrd\n");
78 void __init
mem_init(void)
83 max_mapnr
= highend_pfn
;
85 max_mapnr
= max_low_pfn
;
87 high_memory
= (void *) __va(max_low_pfn
<< PAGE_SHIFT
);
89 #ifdef CONFIG_BLK_DEV_INITRD
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
);
104 mem_init_print_info(NULL
);
107 extern char __init_begin
[], __init_end
[];
109 void free_initmem(void)
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
));
119 totalram_pages_inc();
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
)
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
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
,
160 i
= pgd_index(vaddr
);
161 j
= pud_index(vaddr
);
162 k
= pmd_index(vaddr
);
165 for ( ; (i
< PTRS_PER_PGD
) && (vaddr
!= end
); pgd
++, i
++) {
167 for ( ; (j
< PTRS_PER_PUD
) && (vaddr
!= end
); pud
++, j
++) {
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
);
173 panic("%s: Failed to allocate %lu bytes align=%lx\n",
177 set_pmd(pmd
, __pmd(__pa(pte
)));
178 BUG_ON(pte
!= pte_offset_kernel(pmd
, 0));
188 void __init
fixaddr_init(void)
192 vaddr
= __fix_to_virt(__end_of_fixed_addresses
- 1) & PMD_MASK
;
193 fixrange_init(vaddr
, vaddr
+ PMD_SIZE
, swapper_pg_dir
);