xref: /openbmc/linux/arch/csky/mm/init.c (revision 2c6467d2)
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 #ifdef CONFIG_BLK_DEV_INITRD
64 void free_initrd_mem(unsigned long start, unsigned long end)
65 {
66 	if (start < end)
67 		pr_info("Freeing initrd memory: %ldk freed\n",
68 			(end - start) >> 10);
69 
70 	for (; start < end; start += PAGE_SIZE) {
71 		ClearPageReserved(virt_to_page(start));
72 		init_page_count(virt_to_page(start));
73 		free_page(start);
74 		totalram_pages_inc();
75 	}
76 }
77 #endif
78 
79 extern char __init_begin[], __init_end[];
80 
81 void free_initmem(void)
82 {
83 	unsigned long addr;
84 
85 	addr = (unsigned long) &__init_begin;
86 
87 	while (addr < (unsigned long) &__init_end) {
88 		ClearPageReserved(virt_to_page(addr));
89 		init_page_count(virt_to_page(addr));
90 		free_page(addr);
91 		totalram_pages_inc();
92 		addr += PAGE_SIZE;
93 	}
94 
95 	pr_info("Freeing unused kernel memory: %dk freed\n",
96 	((unsigned int)&__init_end - (unsigned int)&__init_begin) >> 10);
97 }
98 
99 void pgd_init(unsigned long *p)
100 {
101 	int i;
102 
103 	for (i = 0; i < PTRS_PER_PGD; i++)
104 		p[i] = __pa(invalid_pte_table);
105 }
106 
107 void __init pre_mmu_init(void)
108 {
109 	/*
110 	 * Setup page-table and enable TLB-hardrefill
111 	 */
112 	flush_tlb_all();
113 	pgd_init((unsigned long *)swapper_pg_dir);
114 	TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir);
115 	TLBMISS_HANDLER_SETUP_PGD_KERNEL(swapper_pg_dir);
116 
117 	asid_cache(smp_processor_id()) = ASID_FIRST_VERSION;
118 
119 	/* Setup page mask to 4k */
120 	write_mmu_pagemask(0);
121 }
122