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/bootmem.h> 18 #include <linux/highmem.h> 19 #include <linux/memblock.h> 20 #include <linux/swap.h> 21 #include <linux/proc_fs.h> 22 #include <linux/pfn.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 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] 36 __page_aligned_bss; 37 EXPORT_SYMBOL(empty_zero_page); 38 39 void __init mem_init(void) 40 { 41 #ifdef CONFIG_HIGHMEM 42 unsigned long tmp; 43 44 max_mapnr = highend_pfn; 45 #else 46 max_mapnr = max_low_pfn; 47 #endif 48 high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT); 49 50 free_all_bootmem(); 51 52 #ifdef CONFIG_HIGHMEM 53 for (tmp = highstart_pfn; tmp < highend_pfn; tmp++) { 54 struct page *page = pfn_to_page(tmp); 55 56 /* FIXME not sure about */ 57 if (!memblock_is_reserved(tmp << PAGE_SHIFT)) 58 free_highmem_page(page); 59 } 60 #endif 61 mem_init_print_info(NULL); 62 } 63 64 #ifdef CONFIG_BLK_DEV_INITRD 65 void free_initrd_mem(unsigned long start, unsigned long end) 66 { 67 if (start < end) 68 pr_info("Freeing initrd memory: %ldk freed\n", 69 (end - start) >> 10); 70 71 for (; start < end; start += PAGE_SIZE) { 72 ClearPageReserved(virt_to_page(start)); 73 init_page_count(virt_to_page(start)); 74 free_page(start); 75 totalram_pages++; 76 } 77 } 78 #endif 79 80 extern char __init_begin[], __init_end[]; 81 82 void free_initmem(void) 83 { 84 unsigned long addr; 85 86 addr = (unsigned long) &__init_begin; 87 88 while (addr < (unsigned long) &__init_end) { 89 ClearPageReserved(virt_to_page(addr)); 90 init_page_count(virt_to_page(addr)); 91 free_page(addr); 92 totalram_pages++; 93 addr += PAGE_SIZE; 94 } 95 96 pr_info("Freeing unused kernel memory: %dk freed\n", 97 ((unsigned int)&__init_end - (unsigned int)&__init_begin) >> 10); 98 } 99 100 void pgd_init(unsigned long *p) 101 { 102 int i; 103 104 for (i = 0; i < PTRS_PER_PGD; i++) 105 p[i] = __pa(invalid_pte_table); 106 } 107 108 void __init pre_mmu_init(void) 109 { 110 /* 111 * Setup page-table and enable TLB-hardrefill 112 */ 113 flush_tlb_all(); 114 pgd_init((unsigned long *)swapper_pg_dir); 115 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir); 116 TLBMISS_HANDLER_SETUP_PGD_KERNEL(swapper_pg_dir); 117 118 asid_cache(smp_processor_id()) = ASID_FIRST_VERSION; 119 120 /* Setup page mask to 4k */ 121 write_mmu_pagemask(0); 122 } 123