1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2c37ddd93SSam Ravnborg /*
3c37ddd93SSam Ravnborg * linux/arch/sparc/mm/init.c
4c37ddd93SSam Ravnborg *
5c37ddd93SSam Ravnborg * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6c37ddd93SSam Ravnborg * Copyright (C) 1995 Eddie C. Dost (ecd@skynet.be)
7c37ddd93SSam Ravnborg * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
8c37ddd93SSam Ravnborg * Copyright (C) 2000 Anton Blanchard (anton@samba.org)
9c37ddd93SSam Ravnborg */
10c37ddd93SSam Ravnborg
11c37ddd93SSam Ravnborg #include <linux/module.h>
12c37ddd93SSam Ravnborg #include <linux/signal.h>
13c37ddd93SSam Ravnborg #include <linux/sched.h>
14c37ddd93SSam Ravnborg #include <linux/kernel.h>
15c37ddd93SSam Ravnborg #include <linux/errno.h>
16c37ddd93SSam Ravnborg #include <linux/string.h>
17c37ddd93SSam Ravnborg #include <linux/types.h>
18c37ddd93SSam Ravnborg #include <linux/ptrace.h>
19c37ddd93SSam Ravnborg #include <linux/mman.h>
20c37ddd93SSam Ravnborg #include <linux/mm.h>
21c37ddd93SSam Ravnborg #include <linux/swap.h>
22c37ddd93SSam Ravnborg #include <linux/initrd.h>
23c37ddd93SSam Ravnborg #include <linux/init.h>
24c37ddd93SSam Ravnborg #include <linux/highmem.h>
25cca079efSMike Rapoport #include <linux/memblock.h>
26c37ddd93SSam Ravnborg #include <linux/pagemap.h>
27c37ddd93SSam Ravnborg #include <linux/poison.h>
285a0e3ad6STejun Heo #include <linux/gfp.h>
29c37ddd93SSam Ravnborg
3086ed40bdSSam Ravnborg #include <asm/sections.h>
31c37ddd93SSam Ravnborg #include <asm/page.h>
32c37ddd93SSam Ravnborg #include <asm/vaddrs.h>
33d191723fSSam Ravnborg #include <asm/setup.h>
34c37ddd93SSam Ravnborg #include <asm/tlb.h>
35c37ddd93SSam Ravnborg #include <asm/prom.h>
360fd7ef1fSKonrad Eisele #include <asm/leon.h>
37c37ddd93SSam Ravnborg
38e8c29c83SSam Ravnborg #include "mm_32.h"
39e8c29c83SSam Ravnborg
40e025ab84SKefeng Wang static unsigned long *sparc_valid_addr_bitmap;
41c37ddd93SSam Ravnborg
42c37ddd93SSam Ravnborg unsigned long phys_base;
436943f3daSSam Ravnborg EXPORT_SYMBOL(phys_base);
446943f3daSSam Ravnborg
45c37ddd93SSam Ravnborg unsigned long pfn_base;
466943f3daSSam Ravnborg EXPORT_SYMBOL(pfn_base);
47c37ddd93SSam Ravnborg
48c37ddd93SSam Ravnborg struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS+1];
49c37ddd93SSam Ravnborg
50c37ddd93SSam Ravnborg /* Initial ramdisk setup */
51c37ddd93SSam Ravnborg extern unsigned int sparc_ramdisk_image;
52c37ddd93SSam Ravnborg extern unsigned int sparc_ramdisk_size;
53c37ddd93SSam Ravnborg
54c37ddd93SSam Ravnborg unsigned long highstart_pfn, highend_pfn;
55c37ddd93SSam Ravnborg
56c37ddd93SSam Ravnborg unsigned long last_valid_pfn;
57c37ddd93SSam Ravnborg
calc_highpages(void)58c37ddd93SSam Ravnborg unsigned long calc_highpages(void)
59c37ddd93SSam Ravnborg {
60c37ddd93SSam Ravnborg int i;
61c37ddd93SSam Ravnborg int nr = 0;
62c37ddd93SSam Ravnborg
63c37ddd93SSam Ravnborg for (i = 0; sp_banks[i].num_bytes != 0; i++) {
64c37ddd93SSam Ravnborg unsigned long start_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
65c37ddd93SSam Ravnborg unsigned long end_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
66c37ddd93SSam Ravnborg
67c37ddd93SSam Ravnborg if (end_pfn <= max_low_pfn)
68c37ddd93SSam Ravnborg continue;
69c37ddd93SSam Ravnborg
70c37ddd93SSam Ravnborg if (start_pfn < max_low_pfn)
71c37ddd93SSam Ravnborg start_pfn = max_low_pfn;
72c37ddd93SSam Ravnborg
73c37ddd93SSam Ravnborg nr += end_pfn - start_pfn;
74c37ddd93SSam Ravnborg }
75c37ddd93SSam Ravnborg
76c37ddd93SSam Ravnborg return nr;
77c37ddd93SSam Ravnborg }
78c37ddd93SSam Ravnborg
calc_max_low_pfn(void)79c37ddd93SSam Ravnborg static unsigned long calc_max_low_pfn(void)
80c37ddd93SSam Ravnborg {
81c37ddd93SSam Ravnborg int i;
82c37ddd93SSam Ravnborg unsigned long tmp = pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT);
83c37ddd93SSam Ravnborg unsigned long curr_pfn, last_pfn;
84c37ddd93SSam Ravnborg
85c37ddd93SSam Ravnborg last_pfn = (sp_banks[0].base_addr + sp_banks[0].num_bytes) >> PAGE_SHIFT;
86c37ddd93SSam Ravnborg for (i = 1; sp_banks[i].num_bytes != 0; i++) {
87c37ddd93SSam Ravnborg curr_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
88c37ddd93SSam Ravnborg
89c37ddd93SSam Ravnborg if (curr_pfn >= tmp) {
90c37ddd93SSam Ravnborg if (last_pfn < tmp)
91c37ddd93SSam Ravnborg tmp = last_pfn;
92c37ddd93SSam Ravnborg break;
93c37ddd93SSam Ravnborg }
94c37ddd93SSam Ravnborg
95c37ddd93SSam Ravnborg last_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
96c37ddd93SSam Ravnborg }
97c37ddd93SSam Ravnborg
98c37ddd93SSam Ravnborg return tmp;
99c37ddd93SSam Ravnborg }
100c37ddd93SSam Ravnborg
find_ramdisk(unsigned long end_of_phys_memory)1018818d89cSMike Rapoport static void __init find_ramdisk(unsigned long end_of_phys_memory)
1028818d89cSMike Rapoport {
1038818d89cSMike Rapoport #ifdef CONFIG_BLK_DEV_INITRD
1048818d89cSMike Rapoport unsigned long size;
1058818d89cSMike Rapoport
1068818d89cSMike Rapoport /* Now have to check initial ramdisk, so that it won't pass
1078818d89cSMike Rapoport * the end of memory
1088818d89cSMike Rapoport */
1098818d89cSMike Rapoport if (sparc_ramdisk_image) {
1108818d89cSMike Rapoport if (sparc_ramdisk_image >= (unsigned long)&_end - 2 * PAGE_SIZE)
1118818d89cSMike Rapoport sparc_ramdisk_image -= KERNBASE;
1128818d89cSMike Rapoport initrd_start = sparc_ramdisk_image + phys_base;
1138818d89cSMike Rapoport initrd_end = initrd_start + sparc_ramdisk_size;
1148818d89cSMike Rapoport if (initrd_end > end_of_phys_memory) {
1158818d89cSMike Rapoport printk(KERN_CRIT "initrd extends beyond end of memory "
1168818d89cSMike Rapoport "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
1178818d89cSMike Rapoport initrd_end, end_of_phys_memory);
1188818d89cSMike Rapoport initrd_start = 0;
1198818d89cSMike Rapoport } else {
1208818d89cSMike Rapoport /* Reserve the initrd image area. */
1218818d89cSMike Rapoport size = initrd_end - initrd_start;
1228818d89cSMike Rapoport memblock_reserve(initrd_start, size);
1238818d89cSMike Rapoport
1248818d89cSMike Rapoport initrd_start = (initrd_start - phys_base) + PAGE_OFFSET;
1258818d89cSMike Rapoport initrd_end = (initrd_end - phys_base) + PAGE_OFFSET;
1268818d89cSMike Rapoport }
1278818d89cSMike Rapoport }
1288818d89cSMike Rapoport #endif
1298818d89cSMike Rapoport }
1308818d89cSMike Rapoport
bootmem_init(unsigned long * pages_avail)131c37ddd93SSam Ravnborg unsigned long __init bootmem_init(unsigned long *pages_avail)
132c37ddd93SSam Ravnborg {
133cca079efSMike Rapoport unsigned long start_pfn, bytes_avail, size;
134cca079efSMike Rapoport unsigned long end_of_phys_memory = 0;
135cca079efSMike Rapoport unsigned long high_pages = 0;
136c37ddd93SSam Ravnborg int i;
137c37ddd93SSam Ravnborg
138cca079efSMike Rapoport memblock_set_bottom_up(true);
139cca079efSMike Rapoport memblock_allow_resize();
140cca079efSMike Rapoport
141c37ddd93SSam Ravnborg bytes_avail = 0UL;
142c37ddd93SSam Ravnborg for (i = 0; sp_banks[i].num_bytes != 0; i++) {
143c37ddd93SSam Ravnborg end_of_phys_memory = sp_banks[i].base_addr +
144c37ddd93SSam Ravnborg sp_banks[i].num_bytes;
145c37ddd93SSam Ravnborg bytes_avail += sp_banks[i].num_bytes;
146c37ddd93SSam Ravnborg if (cmdline_memory_size) {
147c37ddd93SSam Ravnborg if (bytes_avail > cmdline_memory_size) {
148c37ddd93SSam Ravnborg unsigned long slack = bytes_avail - cmdline_memory_size;
149c37ddd93SSam Ravnborg
150c37ddd93SSam Ravnborg bytes_avail -= slack;
151c37ddd93SSam Ravnborg end_of_phys_memory -= slack;
152c37ddd93SSam Ravnborg
153c37ddd93SSam Ravnborg sp_banks[i].num_bytes -= slack;
154c37ddd93SSam Ravnborg if (sp_banks[i].num_bytes == 0) {
155c37ddd93SSam Ravnborg sp_banks[i].base_addr = 0xdeadbeef;
156c37ddd93SSam Ravnborg } else {
157cca079efSMike Rapoport memblock_add(sp_banks[i].base_addr,
158cca079efSMike Rapoport sp_banks[i].num_bytes);
159c37ddd93SSam Ravnborg sp_banks[i+1].num_bytes = 0;
160c37ddd93SSam Ravnborg sp_banks[i+1].base_addr = 0xdeadbeef;
161c37ddd93SSam Ravnborg }
162c37ddd93SSam Ravnborg break;
163c37ddd93SSam Ravnborg }
164c37ddd93SSam Ravnborg }
165cca079efSMike Rapoport memblock_add(sp_banks[i].base_addr, sp_banks[i].num_bytes);
166c37ddd93SSam Ravnborg }
167c37ddd93SSam Ravnborg
168c37ddd93SSam Ravnborg /* Start with page aligned address of last symbol in kernel
169c37ddd93SSam Ravnborg * image.
170c37ddd93SSam Ravnborg */
171c37ddd93SSam Ravnborg start_pfn = (unsigned long)__pa(PAGE_ALIGN((unsigned long) &_end));
172c37ddd93SSam Ravnborg
173c37ddd93SSam Ravnborg /* Now shift down to get the real physical page frame number. */
174c37ddd93SSam Ravnborg start_pfn >>= PAGE_SHIFT;
175c37ddd93SSam Ravnborg
176c37ddd93SSam Ravnborg max_pfn = end_of_phys_memory >> PAGE_SHIFT;
177c37ddd93SSam Ravnborg
178c37ddd93SSam Ravnborg max_low_pfn = max_pfn;
179c37ddd93SSam Ravnborg highstart_pfn = highend_pfn = max_pfn;
180c37ddd93SSam Ravnborg
181c37ddd93SSam Ravnborg if (max_low_pfn > pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT)) {
182c37ddd93SSam Ravnborg highstart_pfn = pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT);
183c37ddd93SSam Ravnborg max_low_pfn = calc_max_low_pfn();
184cca079efSMike Rapoport high_pages = calc_highpages();
185c37ddd93SSam Ravnborg printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
186cca079efSMike Rapoport high_pages >> (20 - PAGE_SHIFT));
187c37ddd93SSam Ravnborg }
188c37ddd93SSam Ravnborg
1898818d89cSMike Rapoport find_ramdisk(end_of_phys_memory);
190c37ddd93SSam Ravnborg
191c37ddd93SSam Ravnborg /* Reserve the kernel text/data/bss. */
192c37ddd93SSam Ravnborg size = (start_pfn << PAGE_SHIFT) - phys_base;
193cca079efSMike Rapoport memblock_reserve(phys_base, size);
1944360dfa9SMike Rapoport memblock_add(phys_base, size);
195c37ddd93SSam Ravnborg
196cca079efSMike Rapoport size = memblock_phys_mem_size() - memblock_reserved_size();
197cca079efSMike Rapoport *pages_avail = (size >> PAGE_SHIFT) - high_pages;
198c37ddd93SSam Ravnborg
199bda16693SAndreas Larsson /* Only allow low memory to be allocated via memblock allocation */
200bda16693SAndreas Larsson memblock_set_current_limit(max_low_pfn << PAGE_SHIFT);
201bda16693SAndreas Larsson
202c37ddd93SSam Ravnborg return max_pfn;
203c37ddd93SSam Ravnborg }
204c37ddd93SSam Ravnborg
205c37ddd93SSam Ravnborg /*
206c37ddd93SSam Ravnborg * paging_init() sets up the page tables: We call the MMU specific
207c37ddd93SSam Ravnborg * init routine based upon the Sun model type on the Sparc.
208c37ddd93SSam Ravnborg *
209c37ddd93SSam Ravnborg */
paging_init(void)210c37ddd93SSam Ravnborg void __init paging_init(void)
211c37ddd93SSam Ravnborg {
212c37ddd93SSam Ravnborg srmmu_paging_init();
213c37ddd93SSam Ravnborg prom_build_devicetree();
214890db403SDavid S. Miller of_fill_in_cpu_data();
215c37ddd93SSam Ravnborg device_scan();
216c37ddd93SSam Ravnborg }
217c37ddd93SSam Ravnborg
taint_real_pages(void)218c37ddd93SSam Ravnborg static void __init taint_real_pages(void)
219c37ddd93SSam Ravnborg {
220c37ddd93SSam Ravnborg int i;
221c37ddd93SSam Ravnborg
222c37ddd93SSam Ravnborg for (i = 0; sp_banks[i].num_bytes; i++) {
223c37ddd93SSam Ravnborg unsigned long start, end;
224c37ddd93SSam Ravnborg
225c37ddd93SSam Ravnborg start = sp_banks[i].base_addr;
226c37ddd93SSam Ravnborg end = start + sp_banks[i].num_bytes;
227c37ddd93SSam Ravnborg
228c37ddd93SSam Ravnborg while (start < end) {
229c37ddd93SSam Ravnborg set_bit(start >> 20, sparc_valid_addr_bitmap);
230c37ddd93SSam Ravnborg start += PAGE_SIZE;
231c37ddd93SSam Ravnborg }
232c37ddd93SSam Ravnborg }
233c37ddd93SSam Ravnborg }
234c37ddd93SSam Ravnborg
map_high_region(unsigned long start_pfn,unsigned long end_pfn)235c37ddd93SSam Ravnborg static void map_high_region(unsigned long start_pfn, unsigned long end_pfn)
236c37ddd93SSam Ravnborg {
237c37ddd93SSam Ravnborg unsigned long tmp;
238c37ddd93SSam Ravnborg
239c37ddd93SSam Ravnborg #ifdef CONFIG_DEBUG_HIGHMEM
240c37ddd93SSam Ravnborg printk("mapping high region %08lx - %08lx\n", start_pfn, end_pfn);
241c37ddd93SSam Ravnborg #endif
242c37ddd93SSam Ravnborg
24332e1a109SJiang Liu for (tmp = start_pfn; tmp < end_pfn; tmp++)
24432e1a109SJiang Liu free_highmem_page(pfn_to_page(tmp));
245c37ddd93SSam Ravnborg }
246c37ddd93SSam Ravnborg
mem_init(void)247c37ddd93SSam Ravnborg void __init mem_init(void)
248c37ddd93SSam Ravnborg {
249c37ddd93SSam Ravnborg int i;
250c37ddd93SSam Ravnborg
251c37ddd93SSam Ravnborg if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
252c37ddd93SSam Ravnborg prom_printf("BUG: fixmap and pkmap areas overlap\n");
253c37ddd93SSam Ravnborg prom_printf("pkbase: 0x%lx pkend: 0x%lx fixstart 0x%lx\n",
254c37ddd93SSam Ravnborg PKMAP_BASE,
255c37ddd93SSam Ravnborg (unsigned long)PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
256c37ddd93SSam Ravnborg FIXADDR_START);
257c37ddd93SSam Ravnborg prom_printf("Please mail sparclinux@vger.kernel.org.\n");
258c37ddd93SSam Ravnborg prom_halt();
259c37ddd93SSam Ravnborg }
260c37ddd93SSam Ravnborg
261c37ddd93SSam Ravnborg
262c37ddd93SSam Ravnborg /* Saves us work later. */
263deba804cSOrlando Arias memset((void *)empty_zero_page, 0, PAGE_SIZE);
264c37ddd93SSam Ravnborg
265c37ddd93SSam Ravnborg i = last_valid_pfn >> ((20 - PAGE_SHIFT) + 5);
266c37ddd93SSam Ravnborg i += 1;
267c37ddd93SSam Ravnborg sparc_valid_addr_bitmap = (unsigned long *)
2689415673eSMike Rapoport memblock_alloc(i << 2, SMP_CACHE_BYTES);
269c37ddd93SSam Ravnborg
270c37ddd93SSam Ravnborg if (sparc_valid_addr_bitmap == NULL) {
271c37ddd93SSam Ravnborg prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
272c37ddd93SSam Ravnborg prom_halt();
273c37ddd93SSam Ravnborg }
274c37ddd93SSam Ravnborg memset(sparc_valid_addr_bitmap, 0, i << 2);
275c37ddd93SSam Ravnborg
276c37ddd93SSam Ravnborg taint_real_pages();
277c37ddd93SSam Ravnborg
278c37ddd93SSam Ravnborg max_mapnr = last_valid_pfn - pfn_base;
279c37ddd93SSam Ravnborg high_memory = __va(max_low_pfn << PAGE_SHIFT);
280c6ffc5caSMike Rapoport memblock_free_all();
281c37ddd93SSam Ravnborg
282c37ddd93SSam Ravnborg for (i = 0; sp_banks[i].num_bytes != 0; i++) {
283c37ddd93SSam Ravnborg unsigned long start_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
284c37ddd93SSam Ravnborg unsigned long end_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
285c37ddd93SSam Ravnborg
286c37ddd93SSam Ravnborg if (end_pfn <= highstart_pfn)
287c37ddd93SSam Ravnborg continue;
288c37ddd93SSam Ravnborg
289c37ddd93SSam Ravnborg if (start_pfn < highstart_pfn)
290c37ddd93SSam Ravnborg start_pfn = highstart_pfn;
291c37ddd93SSam Ravnborg
292c37ddd93SSam Ravnborg map_high_region(start_pfn, end_pfn);
293c37ddd93SSam Ravnborg }
294c37ddd93SSam Ravnborg }
295c37ddd93SSam Ravnborg
sparc_flush_page_to_ram(struct page * page)296c37ddd93SSam Ravnborg void sparc_flush_page_to_ram(struct page *page)
297c37ddd93SSam Ravnborg {
298c37ddd93SSam Ravnborg unsigned long vaddr = (unsigned long)page_address(page);
299c37ddd93SSam Ravnborg
300c37ddd93SSam Ravnborg __flush_page_to_ram(vaddr);
301c37ddd93SSam Ravnborg }
3026943f3daSSam Ravnborg EXPORT_SYMBOL(sparc_flush_page_to_ram);
30325740d31SAnshuman Khandual
sparc_flush_folio_to_ram(struct folio * folio)304*665f6402SMatthew Wilcox (Oracle) void sparc_flush_folio_to_ram(struct folio *folio)
305*665f6402SMatthew Wilcox (Oracle) {
306*665f6402SMatthew Wilcox (Oracle) unsigned long vaddr = (unsigned long)folio_address(folio);
307*665f6402SMatthew Wilcox (Oracle) unsigned int i, nr = folio_nr_pages(folio);
308*665f6402SMatthew Wilcox (Oracle)
309*665f6402SMatthew Wilcox (Oracle) for (i = 0; i < nr; i++)
310*665f6402SMatthew Wilcox (Oracle) __flush_page_to_ram(vaddr + i * PAGE_SIZE);
311*665f6402SMatthew Wilcox (Oracle) }
312*665f6402SMatthew Wilcox (Oracle) EXPORT_SYMBOL(sparc_flush_folio_to_ram);
313*665f6402SMatthew Wilcox (Oracle)
31425740d31SAnshuman Khandual static const pgprot_t protection_map[16] = {
31525740d31SAnshuman Khandual [VM_NONE] = PAGE_NONE,
31625740d31SAnshuman Khandual [VM_READ] = PAGE_READONLY,
31725740d31SAnshuman Khandual [VM_WRITE] = PAGE_COPY,
31825740d31SAnshuman Khandual [VM_WRITE | VM_READ] = PAGE_COPY,
31925740d31SAnshuman Khandual [VM_EXEC] = PAGE_READONLY,
32025740d31SAnshuman Khandual [VM_EXEC | VM_READ] = PAGE_READONLY,
32125740d31SAnshuman Khandual [VM_EXEC | VM_WRITE] = PAGE_COPY,
32225740d31SAnshuman Khandual [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY,
32325740d31SAnshuman Khandual [VM_SHARED] = PAGE_NONE,
32425740d31SAnshuman Khandual [VM_SHARED | VM_READ] = PAGE_READONLY,
32525740d31SAnshuman Khandual [VM_SHARED | VM_WRITE] = PAGE_SHARED,
32625740d31SAnshuman Khandual [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED,
32725740d31SAnshuman Khandual [VM_SHARED | VM_EXEC] = PAGE_READONLY,
32825740d31SAnshuman Khandual [VM_SHARED | VM_EXEC | VM_READ] = PAGE_READONLY,
32925740d31SAnshuman Khandual [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED,
33025740d31SAnshuman Khandual [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED
33125740d31SAnshuman Khandual };
33225740d31SAnshuman Khandual DECLARE_VM_GET_PAGE_PROT
333