xref: /linux/arch/loongarch/mm/init.c (revision b6d27a345f9d12fb80d61a1b1801ced9c1d6178a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4  */
5 #include <linux/init.h>
6 #include <linux/export.h>
7 #include <linux/signal.h>
8 #include <linux/sched.h>
9 #include <linux/smp.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/string.h>
13 #include <linux/types.h>
14 #include <linux/pagemap.h>
15 #include <linux/memblock.h>
16 #include <linux/memremap.h>
17 #include <linux/mm.h>
18 #include <linux/mman.h>
19 #include <linux/highmem.h>
20 #include <linux/swap.h>
21 #include <linux/proc_fs.h>
22 #include <linux/pfn.h>
23 #include <linux/hardirq.h>
24 #include <linux/gfp.h>
25 #include <linux/hugetlb.h>
26 #include <linux/mmzone.h>
27 #include <linux/execmem.h>
28 
29 #include <asm/asm-offsets.h>
30 #include <asm/bootinfo.h>
31 #include <asm/cpu.h>
32 #include <asm/dma.h>
33 #include <asm/mmu_context.h>
34 #include <asm/sections.h>
35 #include <asm/pgtable.h>
36 #include <asm/pgalloc.h>
37 #include <asm/tlb.h>
38 
39 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
40 EXPORT_SYMBOL(empty_zero_page);
41 
42 void copy_user_highpage(struct page *to, struct page *from,
43 	unsigned long vaddr, struct vm_area_struct *vma)
44 {
45 	void *vfrom, *vto;
46 
47 	vfrom = kmap_local_page(from);
48 	vto = kmap_local_page(to);
49 	copy_page(vto, vfrom);
50 	kunmap_local(vfrom);
51 	kunmap_local(vto);
52 	/* Make sure this page is cleared on other CPU's too before using it */
53 	smp_wmb();
54 }
55 
56 int __ref page_is_ram(unsigned long pfn)
57 {
58 	unsigned long addr = PFN_PHYS(pfn);
59 
60 	return memblock_is_memory(addr) && !memblock_is_reserved(addr);
61 }
62 
63 #ifndef CONFIG_NUMA
64 void __init paging_init(void)
65 {
66 	unsigned long max_zone_pfns[MAX_NR_ZONES];
67 
68 #ifdef CONFIG_ZONE_DMA
69 	max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
70 #endif
71 #ifdef CONFIG_ZONE_DMA32
72 	max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
73 #endif
74 	max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
75 
76 	free_area_init(max_zone_pfns);
77 }
78 #endif /* !CONFIG_NUMA */
79 
80 void __ref free_initmem(void)
81 {
82 	free_initmem_default(POISON_FREE_INITMEM);
83 }
84 
85 #ifdef CONFIG_MEMORY_HOTPLUG
86 int arch_add_memory(int nid, u64 start, u64 size, struct mhp_params *params)
87 {
88 	unsigned long start_pfn = start >> PAGE_SHIFT;
89 	unsigned long nr_pages = size >> PAGE_SHIFT;
90 	int ret;
91 
92 	ret = __add_pages(nid, start_pfn, nr_pages, params);
93 
94 	if (ret)
95 		pr_warn("%s: Problem encountered in __add_pages() as ret=%d\n",
96 				__func__,  ret);
97 
98 	return ret;
99 }
100 
101 void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
102 {
103 	unsigned long start_pfn = start >> PAGE_SHIFT;
104 	unsigned long nr_pages = size >> PAGE_SHIFT;
105 	struct page *page = pfn_to_page(start_pfn);
106 
107 	/* With altmap the first mapped page is offset from @start */
108 	if (altmap)
109 		page += vmem_altmap_offset(altmap);
110 	__remove_pages(start_pfn, nr_pages, altmap);
111 }
112 
113 #ifdef CONFIG_NUMA
114 int memory_add_physaddr_to_nid(u64 start)
115 {
116 	return pa_to_nid(start);
117 }
118 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
119 #endif
120 #endif
121 
122 #ifdef CONFIG_SPARSEMEM_VMEMMAP
123 void __meminit vmemmap_set_pmd(pmd_t *pmd, void *p, int node,
124 			       unsigned long addr, unsigned long next)
125 {
126 	pmd_t entry;
127 
128 	entry = pfn_pmd(virt_to_pfn(p), PAGE_KERNEL);
129 	pmd_val(entry) |= _PAGE_HUGE | _PAGE_HGLOBAL;
130 	set_pmd_at(&init_mm, addr, pmd, entry);
131 }
132 
133 int __meminit vmemmap_check_pmd(pmd_t *pmd, int node,
134 				unsigned long addr, unsigned long next)
135 {
136 	int huge = pmd_val(pmdp_get(pmd)) & _PAGE_HUGE;
137 
138 	if (huge)
139 		vmemmap_verify((pte_t *)pmd, node, addr, next);
140 
141 	return huge;
142 }
143 
144 int __meminit vmemmap_populate(unsigned long start, unsigned long end,
145 			       int node, struct vmem_altmap *altmap)
146 {
147 #if CONFIG_PGTABLE_LEVELS == 2
148 	return vmemmap_populate_basepages(start, end, node, NULL);
149 #else
150 	return vmemmap_populate_hugepages(start, end, node, NULL);
151 #endif
152 }
153 
154 #ifdef CONFIG_MEMORY_HOTPLUG
155 void vmemmap_free(unsigned long start, unsigned long end, struct vmem_altmap *altmap)
156 {
157 }
158 #endif
159 #endif
160 
161 pte_t * __init populate_kernel_pte(unsigned long addr)
162 {
163 	pgd_t *pgd = pgd_offset_k(addr);
164 	p4d_t *p4d = p4d_offset(pgd, addr);
165 	pud_t *pud;
166 	pmd_t *pmd;
167 
168 	if (p4d_none(p4dp_get(p4d))) {
169 		pud = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
170 		p4d_populate(&init_mm, p4d, pud);
171 #ifndef __PAGETABLE_PUD_FOLDED
172 		pud_init(pud);
173 #endif
174 	}
175 
176 	pud = pud_offset(p4d, addr);
177 	if (pud_none(pudp_get(pud))) {
178 		pmd = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
179 		pud_populate(&init_mm, pud, pmd);
180 #ifndef __PAGETABLE_PMD_FOLDED
181 		pmd_init(pmd);
182 #endif
183 	}
184 
185 	pmd = pmd_offset(pud, addr);
186 	if (!pmd_present(pmdp_get(pmd))) {
187 		pte_t *pte;
188 
189 		pte = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
190 		pmd_populate_kernel(&init_mm, pmd, pte);
191 		kernel_pte_init(pte);
192 	}
193 
194 	return pte_offset_kernel(pmd, addr);
195 }
196 
197 void __init __set_fixmap(enum fixed_addresses idx,
198 			       phys_addr_t phys, pgprot_t flags)
199 {
200 	unsigned long addr = __fix_to_virt(idx);
201 	pte_t *ptep;
202 
203 	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
204 
205 	ptep = populate_kernel_pte(addr);
206 	if (!pte_none(ptep_get(ptep))) {
207 		pte_ERROR(*ptep);
208 		return;
209 	}
210 
211 	if (pgprot_val(flags))
212 		set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags));
213 	else {
214 		pte_clear(&init_mm, addr, ptep);
215 		flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
216 	}
217 }
218 
219 /*
220  * Align swapper_pg_dir in to 64K, allows its address to be loaded
221  * with a single LUI instruction in the TLB handlers.  If we used
222  * __aligned(64K), its size would get rounded up to the alignment
223  * size, and waste space.  So we place it in its own section and align
224  * it in the linker script.
225  */
226 pgd_t swapper_pg_dir[_PTRS_PER_PGD] __section(".bss..swapper_pg_dir");
227 
228 pgd_t invalid_pg_dir[_PTRS_PER_PGD] __page_aligned_bss;
229 #ifndef __PAGETABLE_PUD_FOLDED
230 pud_t invalid_pud_table[PTRS_PER_PUD] __page_aligned_bss;
231 EXPORT_SYMBOL(invalid_pud_table);
232 #endif
233 #ifndef __PAGETABLE_PMD_FOLDED
234 pmd_t invalid_pmd_table[PTRS_PER_PMD] __page_aligned_bss;
235 EXPORT_SYMBOL(invalid_pmd_table);
236 #endif
237 pte_t invalid_pte_table[PTRS_PER_PTE] __page_aligned_bss;
238 EXPORT_SYMBOL(invalid_pte_table);
239 
240 #ifdef CONFIG_EXECMEM
241 static struct execmem_info execmem_info __ro_after_init;
242 
243 struct execmem_info __init *execmem_arch_setup(void)
244 {
245 	execmem_info = (struct execmem_info){
246 		.ranges = {
247 			[EXECMEM_DEFAULT] = {
248 				.start	= MODULES_VADDR,
249 				.end	= MODULES_END,
250 				.pgprot	= PAGE_KERNEL,
251 				.alignment = 1,
252 			},
253 		},
254 	};
255 
256 	return &execmem_info;
257 }
258 #endif /* CONFIG_EXECMEM */
259