xref: /openbmc/linux/arch/csky/mm/init.c (revision 15e3ae36)
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 #include <linux/initrd.h>
23 
24 #include <asm/setup.h>
25 #include <asm/cachectl.h>
26 #include <asm/dma.h>
27 #include <asm/pgtable.h>
28 #include <asm/pgalloc.h>
29 #include <asm/mmu_context.h>
30 #include <asm/sections.h>
31 #include <asm/tlb.h>
32 
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)]
37 						__page_aligned_bss;
38 EXPORT_SYMBOL(empty_zero_page);
39 
40 #ifdef CONFIG_BLK_DEV_INITRD
41 static void __init setup_initrd(void)
42 {
43 	unsigned long size;
44 
45 	if (initrd_start >= initrd_end) {
46 		pr_err("initrd not found or empty");
47 		goto disable;
48 	}
49 
50 	if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
51 		pr_err("initrd extends beyond end of memory");
52 		goto disable;
53 	}
54 
55 	size = initrd_end - initrd_start;
56 
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);
60 		goto disable;
61 	}
62 
63 	memblock_reserve(__pa(initrd_start), size);
64 
65 	pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
66 		(void *)(initrd_start), size);
67 
68 	initrd_below_start_ok = 1;
69 
70 	return;
71 
72 disable:
73 	initrd_start = initrd_end = 0;
74 
75 	pr_err(" - disabling initrd\n");
76 }
77 #endif
78 
79 void __init mem_init(void)
80 {
81 #ifdef CONFIG_HIGHMEM
82 	unsigned long tmp;
83 
84 	max_mapnr = highend_pfn;
85 #else
86 	max_mapnr = max_low_pfn;
87 #endif
88 	high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
89 
90 #ifdef CONFIG_BLK_DEV_INITRD
91 	setup_initrd();
92 #endif
93 
94 	memblock_free_all();
95 
96 #ifdef CONFIG_HIGHMEM
97 	for (tmp = highstart_pfn; tmp < highend_pfn; tmp++) {
98 		struct page *page = pfn_to_page(tmp);
99 
100 		/* FIXME not sure about */
101 		if (!memblock_is_reserved(tmp << PAGE_SHIFT))
102 			free_highmem_page(page);
103 	}
104 #endif
105 	mem_init_print_info(NULL);
106 }
107 
108 extern char __init_begin[], __init_end[];
109 
110 void free_initmem(void)
111 {
112 	unsigned long addr;
113 
114 	addr = (unsigned long) &__init_begin;
115 
116 	while (addr < (unsigned long) &__init_end) {
117 		ClearPageReserved(virt_to_page(addr));
118 		init_page_count(virt_to_page(addr));
119 		free_page(addr);
120 		totalram_pages_inc();
121 		addr += PAGE_SIZE;
122 	}
123 
124 	pr_info("Freeing unused kernel memory: %dk freed\n",
125 	((unsigned int)&__init_end - (unsigned int)&__init_begin) >> 10);
126 }
127 
128 void pgd_init(unsigned long *p)
129 {
130 	int i;
131 
132 	for (i = 0; i < PTRS_PER_PGD; i++)
133 		p[i] = __pa(invalid_pte_table);
134 }
135 
136 void __init pre_mmu_init(void)
137 {
138 	/*
139 	 * Setup page-table and enable TLB-hardrefill
140 	 */
141 	flush_tlb_all();
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);
145 
146 	/* Setup page mask to 4k */
147 	write_mmu_pagemask(0);
148 }
149 
150 void __init fixrange_init(unsigned long start, unsigned long end,
151 			pgd_t *pgd_base)
152 {
153 	pgd_t *pgd;
154 	pud_t *pud;
155 	pmd_t *pmd;
156 	pte_t *pte;
157 	int i, j, k;
158 	unsigned long vaddr;
159 
160 	vaddr = start;
161 	i = __pgd_offset(vaddr);
162 	j = __pud_offset(vaddr);
163 	k = __pmd_offset(vaddr);
164 	pgd = pgd_base + i;
165 
166 	for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
167 		pud = (pud_t *)pgd;
168 		for ( ; (j < PTRS_PER_PUD) && (vaddr != end); pud++, j++) {
169 			pmd = (pmd_t *)pud;
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);
173 					if (!pte)
174 						panic("%s: Failed to allocate %lu bytes align=%lx\n",
175 						      __func__, PAGE_SIZE,
176 						      PAGE_SIZE);
177 
178 					set_pmd(pmd, __pmd(__pa(pte)));
179 					BUG_ON(pte != pte_offset_kernel(pmd, 0));
180 				}
181 				vaddr += PMD_SIZE;
182 			}
183 			k = 0;
184 		}
185 		j = 0;
186 	}
187 }
188 
189 void __init fixaddr_init(void)
190 {
191 	unsigned long vaddr;
192 
193 	vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
194 	fixrange_init(vaddr, vaddr + PMD_SIZE, swapper_pg_dir);
195 }
196