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 28 #include <asm/asm-offsets.h> 29 #include <asm/bootinfo.h> 30 #include <asm/cpu.h> 31 #include <asm/dma.h> 32 #include <asm/mmu_context.h> 33 #include <asm/sections.h> 34 #include <asm/pgtable.h> 35 #include <asm/pgalloc.h> 36 #include <asm/tlb.h> 37 38 /* 39 * We have up to 8 empty zeroed pages so we can map one of the right colour 40 * when needed. Since page is never written to after the initialization we 41 * don't have to care about aliases on other CPUs. 42 */ 43 unsigned long empty_zero_page, zero_page_mask; 44 EXPORT_SYMBOL_GPL(empty_zero_page); 45 EXPORT_SYMBOL(zero_page_mask); 46 47 void setup_zero_pages(void) 48 { 49 unsigned int order, i; 50 struct page *page; 51 52 order = 0; 53 54 empty_zero_page = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order); 55 if (!empty_zero_page) 56 panic("Oh boy, that early out of memory?"); 57 58 page = virt_to_page((void *)empty_zero_page); 59 split_page(page, order); 60 for (i = 0; i < (1 << order); i++, page++) 61 mark_page_reserved(page); 62 63 zero_page_mask = ((PAGE_SIZE << order) - 1) & PAGE_MASK; 64 } 65 66 void copy_user_highpage(struct page *to, struct page *from, 67 unsigned long vaddr, struct vm_area_struct *vma) 68 { 69 void *vfrom, *vto; 70 71 vto = kmap_atomic(to); 72 vfrom = kmap_atomic(from); 73 copy_page(vto, vfrom); 74 kunmap_atomic(vfrom); 75 kunmap_atomic(vto); 76 /* Make sure this page is cleared on other CPU's too before using it */ 77 smp_wmb(); 78 } 79 80 int __ref page_is_ram(unsigned long pfn) 81 { 82 unsigned long addr = PFN_PHYS(pfn); 83 84 return memblock_is_memory(addr) && !memblock_is_reserved(addr); 85 } 86 87 #ifndef CONFIG_NUMA 88 void __init paging_init(void) 89 { 90 unsigned long max_zone_pfns[MAX_NR_ZONES]; 91 92 #ifdef CONFIG_ZONE_DMA 93 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN; 94 #endif 95 #ifdef CONFIG_ZONE_DMA32 96 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN; 97 #endif 98 max_zone_pfns[ZONE_NORMAL] = max_low_pfn; 99 100 free_area_init(max_zone_pfns); 101 } 102 103 void __init mem_init(void) 104 { 105 max_mapnr = max_low_pfn; 106 high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT); 107 108 memblock_free_all(); 109 setup_zero_pages(); /* Setup zeroed pages. */ 110 } 111 #endif /* !CONFIG_NUMA */ 112 113 void __ref free_initmem(void) 114 { 115 free_initmem_default(POISON_FREE_INITMEM); 116 } 117 118 #ifdef CONFIG_MEMORY_HOTPLUG 119 int arch_add_memory(int nid, u64 start, u64 size, struct mhp_params *params) 120 { 121 unsigned long start_pfn = start >> PAGE_SHIFT; 122 unsigned long nr_pages = size >> PAGE_SHIFT; 123 int ret; 124 125 ret = __add_pages(nid, start_pfn, nr_pages, params); 126 127 if (ret) 128 pr_warn("%s: Problem encountered in __add_pages() as ret=%d\n", 129 __func__, ret); 130 131 return ret; 132 } 133 134 void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) 135 { 136 unsigned long start_pfn = start >> PAGE_SHIFT; 137 unsigned long nr_pages = size >> PAGE_SHIFT; 138 struct page *page = pfn_to_page(start_pfn); 139 140 /* With altmap the first mapped page is offset from @start */ 141 if (altmap) 142 page += vmem_altmap_offset(altmap); 143 __remove_pages(start_pfn, nr_pages, altmap); 144 } 145 146 #ifdef CONFIG_NUMA 147 int memory_add_physaddr_to_nid(u64 start) 148 { 149 return pa_to_nid(start); 150 } 151 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid); 152 #endif 153 #endif 154 155 #ifdef CONFIG_SPARSEMEM_VMEMMAP 156 void __meminit vmemmap_set_pmd(pmd_t *pmd, void *p, int node, 157 unsigned long addr, unsigned long next) 158 { 159 pmd_t entry; 160 161 entry = pfn_pmd(virt_to_pfn(p), PAGE_KERNEL); 162 pmd_val(entry) |= _PAGE_HUGE | _PAGE_HGLOBAL; 163 set_pmd_at(&init_mm, addr, pmd, entry); 164 } 165 166 int __meminit vmemmap_check_pmd(pmd_t *pmd, int node, 167 unsigned long addr, unsigned long next) 168 { 169 int huge = pmd_val(*pmd) & _PAGE_HUGE; 170 171 if (huge) 172 vmemmap_verify((pte_t *)pmd, node, addr, next); 173 174 return huge; 175 } 176 177 int __meminit vmemmap_populate(unsigned long start, unsigned long end, 178 int node, struct vmem_altmap *altmap) 179 { 180 #if CONFIG_PGTABLE_LEVELS == 2 181 return vmemmap_populate_basepages(start, end, node, NULL); 182 #else 183 return vmemmap_populate_hugepages(start, end, node, NULL); 184 #endif 185 } 186 187 #ifdef CONFIG_MEMORY_HOTPLUG 188 void vmemmap_free(unsigned long start, unsigned long end, struct vmem_altmap *altmap) 189 { 190 } 191 #endif 192 #endif 193 194 static pte_t *fixmap_pte(unsigned long addr) 195 { 196 pgd_t *pgd; 197 p4d_t *p4d; 198 pud_t *pud; 199 pmd_t *pmd; 200 201 pgd = pgd_offset_k(addr); 202 p4d = p4d_offset(pgd, addr); 203 204 if (pgd_none(*pgd)) { 205 pud_t *new __maybe_unused; 206 207 new = memblock_alloc_low(PAGE_SIZE, PAGE_SIZE); 208 pgd_populate(&init_mm, pgd, new); 209 #ifndef __PAGETABLE_PUD_FOLDED 210 pud_init(new); 211 #endif 212 } 213 214 pud = pud_offset(p4d, addr); 215 if (pud_none(*pud)) { 216 pmd_t *new __maybe_unused; 217 218 new = memblock_alloc_low(PAGE_SIZE, PAGE_SIZE); 219 pud_populate(&init_mm, pud, new); 220 #ifndef __PAGETABLE_PMD_FOLDED 221 pmd_init(new); 222 #endif 223 } 224 225 pmd = pmd_offset(pud, addr); 226 if (pmd_none(*pmd)) { 227 pte_t *new __maybe_unused; 228 229 new = memblock_alloc_low(PAGE_SIZE, PAGE_SIZE); 230 pmd_populate_kernel(&init_mm, pmd, new); 231 } 232 233 return pte_offset_kernel(pmd, addr); 234 } 235 236 void __init __set_fixmap(enum fixed_addresses idx, 237 phys_addr_t phys, pgprot_t flags) 238 { 239 unsigned long addr = __fix_to_virt(idx); 240 pte_t *ptep; 241 242 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses); 243 244 ptep = fixmap_pte(addr); 245 if (!pte_none(*ptep)) { 246 pte_ERROR(*ptep); 247 return; 248 } 249 250 if (pgprot_val(flags)) 251 set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags)); 252 else { 253 pte_clear(&init_mm, addr, ptep); 254 flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 255 } 256 } 257 258 /* 259 * Align swapper_pg_dir in to 64K, allows its address to be loaded 260 * with a single LUI instruction in the TLB handlers. If we used 261 * __aligned(64K), its size would get rounded up to the alignment 262 * size, and waste space. So we place it in its own section and align 263 * it in the linker script. 264 */ 265 pgd_t swapper_pg_dir[_PTRS_PER_PGD] __section(".bss..swapper_pg_dir"); 266 267 pgd_t invalid_pg_dir[_PTRS_PER_PGD] __page_aligned_bss; 268 #ifndef __PAGETABLE_PUD_FOLDED 269 pud_t invalid_pud_table[PTRS_PER_PUD] __page_aligned_bss; 270 #endif 271 #ifndef __PAGETABLE_PMD_FOLDED 272 pmd_t invalid_pmd_table[PTRS_PER_PMD] __page_aligned_bss; 273 EXPORT_SYMBOL_GPL(invalid_pmd_table); 274 #endif 275 pte_t invalid_pte_table[PTRS_PER_PTE] __page_aligned_bss; 276 EXPORT_SYMBOL(invalid_pte_table); 277