xref: /openbmc/linux/arch/csky/mm/init.c (revision 3dc4b6fb)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3 
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 
23 #include <asm/setup.h>
24 #include <asm/cachectl.h>
25 #include <asm/dma.h>
26 #include <asm/pgtable.h>
27 #include <asm/pgalloc.h>
28 #include <asm/mmu_context.h>
29 #include <asm/sections.h>
30 #include <asm/tlb.h>
31 
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 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
35 						__page_aligned_bss;
36 EXPORT_SYMBOL(empty_zero_page);
37 
38 void __init mem_init(void)
39 {
40 #ifdef CONFIG_HIGHMEM
41 	unsigned long tmp;
42 
43 	max_mapnr = highend_pfn;
44 #else
45 	max_mapnr = max_low_pfn;
46 #endif
47 	high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
48 
49 	memblock_free_all();
50 
51 #ifdef CONFIG_HIGHMEM
52 	for (tmp = highstart_pfn; tmp < highend_pfn; tmp++) {
53 		struct page *page = pfn_to_page(tmp);
54 
55 		/* FIXME not sure about */
56 		if (!memblock_is_reserved(tmp << PAGE_SHIFT))
57 			free_highmem_page(page);
58 	}
59 #endif
60 	mem_init_print_info(NULL);
61 }
62 
63 extern char __init_begin[], __init_end[];
64 
65 void free_initmem(void)
66 {
67 	unsigned long addr;
68 
69 	addr = (unsigned long) &__init_begin;
70 
71 	while (addr < (unsigned long) &__init_end) {
72 		ClearPageReserved(virt_to_page(addr));
73 		init_page_count(virt_to_page(addr));
74 		free_page(addr);
75 		totalram_pages_inc();
76 		addr += PAGE_SIZE;
77 	}
78 
79 	pr_info("Freeing unused kernel memory: %dk freed\n",
80 	((unsigned int)&__init_end - (unsigned int)&__init_begin) >> 10);
81 }
82 
83 void pgd_init(unsigned long *p)
84 {
85 	int i;
86 
87 	for (i = 0; i < PTRS_PER_PGD; i++)
88 		p[i] = __pa(invalid_pte_table);
89 }
90 
91 void __init pre_mmu_init(void)
92 {
93 	/*
94 	 * Setup page-table and enable TLB-hardrefill
95 	 */
96 	flush_tlb_all();
97 	pgd_init((unsigned long *)swapper_pg_dir);
98 	TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir);
99 	TLBMISS_HANDLER_SETUP_PGD_KERNEL(swapper_pg_dir);
100 
101 	/* Setup page mask to 4k */
102 	write_mmu_pagemask(0);
103 }
104