1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
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>
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>
24 #include <asm/setup.h>
25 #include <asm/cachectl.h>
27 #include <asm/pgalloc.h>
28 #include <asm/mmu_context.h>
29 #include <asm/sections.h>
31 #include <asm/cacheflush.h>
33 #define PTRS_KERN_TABLE \
34 ((PTRS_PER_PGD - USER_PTRS_PER_PGD) * PTRS_PER_PTE)
36 pgd_t swapper_pg_dir
[PTRS_PER_PGD
] __page_aligned_bss
;
37 pte_t invalid_pte_table
[PTRS_PER_PTE
] __page_aligned_bss
;
38 pte_t kernel_pte_tables
[PTRS_KERN_TABLE
] __page_aligned_bss
;
40 EXPORT_SYMBOL(invalid_pte_table
);
41 unsigned long empty_zero_page
[PAGE_SIZE
/ sizeof(unsigned long)]
43 EXPORT_SYMBOL(empty_zero_page
);
45 #ifdef CONFIG_BLK_DEV_INITRD
46 static void __init
setup_initrd(void)
50 if (initrd_start
>= initrd_end
) {
51 pr_err("initrd not found or empty");
55 if (__pa(initrd_end
) > PFN_PHYS(max_low_pfn
)) {
56 pr_err("initrd extends beyond end of memory");
60 size
= initrd_end
- initrd_start
;
62 if (memblock_is_region_reserved(__pa(initrd_start
), size
)) {
63 pr_err("INITRD: 0x%08lx+0x%08lx overlaps in-use memory region",
64 __pa(initrd_start
), size
);
68 memblock_reserve(__pa(initrd_start
), size
);
70 pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
71 (void *)(initrd_start
), size
);
73 initrd_below_start_ok
= 1;
78 initrd_start
= initrd_end
= 0;
80 pr_err(" - disabling initrd\n");
84 void __init
mem_init(void)
89 set_max_mapnr(highend_pfn
- ARCH_PFN_OFFSET
);
91 set_max_mapnr(max_low_pfn
- ARCH_PFN_OFFSET
);
93 high_memory
= (void *) __va(max_low_pfn
<< PAGE_SHIFT
);
95 #ifdef CONFIG_BLK_DEV_INITRD
101 #ifdef CONFIG_HIGHMEM
102 for (tmp
= highstart_pfn
; tmp
< highend_pfn
; tmp
++) {
103 struct page
*page
= pfn_to_page(tmp
);
105 /* FIXME not sure about */
106 if (!memblock_is_reserved(tmp
<< PAGE_SHIFT
))
107 free_highmem_page(page
);
112 void free_initmem(void)
114 free_initmem_default(-1);
117 void pgd_init(unsigned long *p
)
121 for (i
= 0; i
< PTRS_PER_PGD
; i
++)
122 p
[i
] = __pa(invalid_pte_table
);
125 local_icache_inv_all(NULL
);
128 void __init
mmu_init(unsigned long min_pfn
, unsigned long max_pfn
)
132 for (i
= 0; i
< USER_PTRS_PER_PGD
; i
++)
133 swapper_pg_dir
[i
].pgd
= __pa(invalid_pte_table
);
135 for (i
= USER_PTRS_PER_PGD
; i
< PTRS_PER_PGD
; i
++)
136 swapper_pg_dir
[i
].pgd
=
137 __pa(kernel_pte_tables
+ (PTRS_PER_PTE
* (i
- USER_PTRS_PER_PGD
)));
139 for (i
= 0; i
< PTRS_KERN_TABLE
; i
++)
140 set_pte(&kernel_pte_tables
[i
], __pte(_PAGE_GLOBAL
));
142 for (i
= min_pfn
; i
< max_pfn
; i
++)
143 set_pte(&kernel_pte_tables
[i
- PFN_DOWN(va_pa_offset
)], pfn_pte(i
, PAGE_KERNEL
));
146 local_icache_inv_all(NULL
);
148 /* Setup page mask to 4k */
149 write_mmu_pagemask(0);
151 setup_pgd(swapper_pg_dir
, 0);
154 void __init
fixrange_init(unsigned long start
, unsigned long end
,
165 i
= pgd_index(vaddr
);
166 j
= pud_index(vaddr
);
167 k
= pmd_index(vaddr
);
170 for ( ; (i
< PTRS_PER_PGD
) && (vaddr
!= end
); pgd
++, i
++) {
172 for ( ; (j
< PTRS_PER_PUD
) && (vaddr
!= end
); pud
++, j
++) {
174 for (; (k
< PTRS_PER_PMD
) && (vaddr
!= end
); pmd
++, k
++) {
175 if (pmd_none(*pmd
)) {
176 pte
= (pte_t
*) memblock_alloc_low(PAGE_SIZE
, PAGE_SIZE
);
178 panic("%s: Failed to allocate %lu bytes align=%lx\n",
182 set_pmd(pmd
, __pmd(__pa(pte
)));
183 BUG_ON(pte
!= pte_offset_kernel(pmd
, 0));
193 void __init
fixaddr_init(void)
197 vaddr
= __fix_to_virt(__end_of_fixed_addresses
- 1) & PMD_MASK
;
198 fixrange_init(vaddr
, vaddr
+ PMD_SIZE
, swapper_pg_dir
);
201 static const pgprot_t protection_map
[16] = {
202 [VM_NONE
] = PAGE_NONE
,
203 [VM_READ
] = PAGE_READ
,
204 [VM_WRITE
] = PAGE_READ
,
205 [VM_WRITE
| VM_READ
] = PAGE_READ
,
206 [VM_EXEC
] = PAGE_READ
,
207 [VM_EXEC
| VM_READ
] = PAGE_READ
,
208 [VM_EXEC
| VM_WRITE
] = PAGE_READ
,
209 [VM_EXEC
| VM_WRITE
| VM_READ
] = PAGE_READ
,
210 [VM_SHARED
] = PAGE_NONE
,
211 [VM_SHARED
| VM_READ
] = PAGE_READ
,
212 [VM_SHARED
| VM_WRITE
] = PAGE_WRITE
,
213 [VM_SHARED
| VM_WRITE
| VM_READ
] = PAGE_WRITE
,
214 [VM_SHARED
| VM_EXEC
] = PAGE_READ
,
215 [VM_SHARED
| VM_EXEC
| VM_READ
] = PAGE_READ
,
216 [VM_SHARED
| VM_EXEC
| VM_WRITE
] = PAGE_WRITE
,
217 [VM_SHARED
| VM_EXEC
| VM_WRITE
| VM_READ
] = PAGE_WRITE
219 DECLARE_VM_GET_PAGE_PROT