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