Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  linux/arch/sparc/mm/init.c
0004  *
0005  *  Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
0006  *  Copyright (C) 1995 Eddie C. Dost (ecd@skynet.be)
0007  *  Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
0008  *  Copyright (C) 2000 Anton Blanchard (anton@samba.org)
0009  */
0010 
0011 #include <linux/module.h>
0012 #include <linux/signal.h>
0013 #include <linux/sched.h>
0014 #include <linux/kernel.h>
0015 #include <linux/errno.h>
0016 #include <linux/string.h>
0017 #include <linux/types.h>
0018 #include <linux/ptrace.h>
0019 #include <linux/mman.h>
0020 #include <linux/mm.h>
0021 #include <linux/swap.h>
0022 #include <linux/initrd.h>
0023 #include <linux/init.h>
0024 #include <linux/highmem.h>
0025 #include <linux/memblock.h>
0026 #include <linux/pagemap.h>
0027 #include <linux/poison.h>
0028 #include <linux/gfp.h>
0029 
0030 #include <asm/sections.h>
0031 #include <asm/page.h>
0032 #include <asm/vaddrs.h>
0033 #include <asm/setup.h>
0034 #include <asm/tlb.h>
0035 #include <asm/prom.h>
0036 #include <asm/leon.h>
0037 
0038 #include "mm_32.h"
0039 
0040 unsigned long *sparc_valid_addr_bitmap;
0041 EXPORT_SYMBOL(sparc_valid_addr_bitmap);
0042 
0043 unsigned long phys_base;
0044 EXPORT_SYMBOL(phys_base);
0045 
0046 unsigned long pfn_base;
0047 EXPORT_SYMBOL(pfn_base);
0048 
0049 struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS+1];
0050 
0051 /* Initial ramdisk setup */
0052 extern unsigned int sparc_ramdisk_image;
0053 extern unsigned int sparc_ramdisk_size;
0054 
0055 unsigned long highstart_pfn, highend_pfn;
0056 
0057 unsigned long last_valid_pfn;
0058 
0059 unsigned long calc_highpages(void)
0060 {
0061     int i;
0062     int nr = 0;
0063 
0064     for (i = 0; sp_banks[i].num_bytes != 0; i++) {
0065         unsigned long start_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
0066         unsigned long end_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
0067 
0068         if (end_pfn <= max_low_pfn)
0069             continue;
0070 
0071         if (start_pfn < max_low_pfn)
0072             start_pfn = max_low_pfn;
0073 
0074         nr += end_pfn - start_pfn;
0075     }
0076 
0077     return nr;
0078 }
0079 
0080 static unsigned long calc_max_low_pfn(void)
0081 {
0082     int i;
0083     unsigned long tmp = pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT);
0084     unsigned long curr_pfn, last_pfn;
0085 
0086     last_pfn = (sp_banks[0].base_addr + sp_banks[0].num_bytes) >> PAGE_SHIFT;
0087     for (i = 1; sp_banks[i].num_bytes != 0; i++) {
0088         curr_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
0089 
0090         if (curr_pfn >= tmp) {
0091             if (last_pfn < tmp)
0092                 tmp = last_pfn;
0093             break;
0094         }
0095 
0096         last_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
0097     }
0098 
0099     return tmp;
0100 }
0101 
0102 static void __init find_ramdisk(unsigned long end_of_phys_memory)
0103 {
0104 #ifdef CONFIG_BLK_DEV_INITRD
0105     unsigned long size;
0106 
0107     /* Now have to check initial ramdisk, so that it won't pass
0108      * the end of memory
0109      */
0110     if (sparc_ramdisk_image) {
0111         if (sparc_ramdisk_image >= (unsigned long)&_end - 2 * PAGE_SIZE)
0112             sparc_ramdisk_image -= KERNBASE;
0113         initrd_start = sparc_ramdisk_image + phys_base;
0114         initrd_end = initrd_start + sparc_ramdisk_size;
0115         if (initrd_end > end_of_phys_memory) {
0116             printk(KERN_CRIT "initrd extends beyond end of memory "
0117                    "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
0118                    initrd_end, end_of_phys_memory);
0119             initrd_start = 0;
0120         } else {
0121             /* Reserve the initrd image area. */
0122             size = initrd_end - initrd_start;
0123             memblock_reserve(initrd_start, size);
0124 
0125             initrd_start = (initrd_start - phys_base) + PAGE_OFFSET;
0126             initrd_end = (initrd_end - phys_base) + PAGE_OFFSET;
0127         }
0128     }
0129 #endif
0130 }
0131 
0132 unsigned long __init bootmem_init(unsigned long *pages_avail)
0133 {
0134     unsigned long start_pfn, bytes_avail, size;
0135     unsigned long end_of_phys_memory = 0;
0136     unsigned long high_pages = 0;
0137     int i;
0138 
0139     memblock_set_bottom_up(true);
0140     memblock_allow_resize();
0141 
0142     bytes_avail = 0UL;
0143     for (i = 0; sp_banks[i].num_bytes != 0; i++) {
0144         end_of_phys_memory = sp_banks[i].base_addr +
0145             sp_banks[i].num_bytes;
0146         bytes_avail += sp_banks[i].num_bytes;
0147         if (cmdline_memory_size) {
0148             if (bytes_avail > cmdline_memory_size) {
0149                 unsigned long slack = bytes_avail - cmdline_memory_size;
0150 
0151                 bytes_avail -= slack;
0152                 end_of_phys_memory -= slack;
0153 
0154                 sp_banks[i].num_bytes -= slack;
0155                 if (sp_banks[i].num_bytes == 0) {
0156                     sp_banks[i].base_addr = 0xdeadbeef;
0157                 } else {
0158                     memblock_add(sp_banks[i].base_addr,
0159                              sp_banks[i].num_bytes);
0160                     sp_banks[i+1].num_bytes = 0;
0161                     sp_banks[i+1].base_addr = 0xdeadbeef;
0162                 }
0163                 break;
0164             }
0165         }
0166         memblock_add(sp_banks[i].base_addr, sp_banks[i].num_bytes);
0167     }
0168 
0169     /* Start with page aligned address of last symbol in kernel
0170      * image.
0171      */
0172     start_pfn  = (unsigned long)__pa(PAGE_ALIGN((unsigned long) &_end));
0173 
0174     /* Now shift down to get the real physical page frame number. */
0175     start_pfn >>= PAGE_SHIFT;
0176 
0177     max_pfn = end_of_phys_memory >> PAGE_SHIFT;
0178 
0179     max_low_pfn = max_pfn;
0180     highstart_pfn = highend_pfn = max_pfn;
0181 
0182     if (max_low_pfn > pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT)) {
0183         highstart_pfn = pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT);
0184         max_low_pfn = calc_max_low_pfn();
0185         high_pages = calc_highpages();
0186         printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
0187             high_pages >> (20 - PAGE_SHIFT));
0188     }
0189 
0190     find_ramdisk(end_of_phys_memory);
0191 
0192     /* Reserve the kernel text/data/bss. */
0193     size = (start_pfn << PAGE_SHIFT) - phys_base;
0194     memblock_reserve(phys_base, size);
0195     memblock_add(phys_base, size);
0196 
0197     size = memblock_phys_mem_size() - memblock_reserved_size();
0198     *pages_avail = (size >> PAGE_SHIFT) - high_pages;
0199 
0200     /* Only allow low memory to be allocated via memblock allocation */
0201     memblock_set_current_limit(max_low_pfn << PAGE_SHIFT);
0202 
0203     return max_pfn;
0204 }
0205 
0206 /*
0207  * paging_init() sets up the page tables: We call the MMU specific
0208  * init routine based upon the Sun model type on the Sparc.
0209  *
0210  */
0211 void __init paging_init(void)
0212 {
0213     srmmu_paging_init();
0214     prom_build_devicetree();
0215     of_fill_in_cpu_data();
0216     device_scan();
0217 }
0218 
0219 static void __init taint_real_pages(void)
0220 {
0221     int i;
0222 
0223     for (i = 0; sp_banks[i].num_bytes; i++) {
0224         unsigned long start, end;
0225 
0226         start = sp_banks[i].base_addr;
0227         end = start + sp_banks[i].num_bytes;
0228 
0229         while (start < end) {
0230             set_bit(start >> 20, sparc_valid_addr_bitmap);
0231             start += PAGE_SIZE;
0232         }
0233     }
0234 }
0235 
0236 static void map_high_region(unsigned long start_pfn, unsigned long end_pfn)
0237 {
0238     unsigned long tmp;
0239 
0240 #ifdef CONFIG_DEBUG_HIGHMEM
0241     printk("mapping high region %08lx - %08lx\n", start_pfn, end_pfn);
0242 #endif
0243 
0244     for (tmp = start_pfn; tmp < end_pfn; tmp++)
0245         free_highmem_page(pfn_to_page(tmp));
0246 }
0247 
0248 void __init mem_init(void)
0249 {
0250     int i;
0251 
0252     if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
0253         prom_printf("BUG: fixmap and pkmap areas overlap\n");
0254         prom_printf("pkbase: 0x%lx pkend: 0x%lx fixstart 0x%lx\n",
0255                PKMAP_BASE,
0256                (unsigned long)PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
0257                FIXADDR_START);
0258         prom_printf("Please mail sparclinux@vger.kernel.org.\n");
0259         prom_halt();
0260     }
0261 
0262 
0263     /* Saves us work later. */
0264     memset((void *)empty_zero_page, 0, PAGE_SIZE);
0265 
0266     i = last_valid_pfn >> ((20 - PAGE_SHIFT) + 5);
0267     i += 1;
0268     sparc_valid_addr_bitmap = (unsigned long *)
0269         memblock_alloc(i << 2, SMP_CACHE_BYTES);
0270 
0271     if (sparc_valid_addr_bitmap == NULL) {
0272         prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
0273         prom_halt();
0274     }
0275     memset(sparc_valid_addr_bitmap, 0, i << 2);
0276 
0277     taint_real_pages();
0278 
0279     max_mapnr = last_valid_pfn - pfn_base;
0280     high_memory = __va(max_low_pfn << PAGE_SHIFT);
0281     memblock_free_all();
0282 
0283     for (i = 0; sp_banks[i].num_bytes != 0; i++) {
0284         unsigned long start_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
0285         unsigned long end_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
0286 
0287         if (end_pfn <= highstart_pfn)
0288             continue;
0289 
0290         if (start_pfn < highstart_pfn)
0291             start_pfn = highstart_pfn;
0292 
0293         map_high_region(start_pfn, end_pfn);
0294     }
0295 }
0296 
0297 void sparc_flush_page_to_ram(struct page *page)
0298 {
0299     unsigned long vaddr = (unsigned long)page_address(page);
0300 
0301     if (vaddr)
0302         __flush_page_to_ram(vaddr);
0303 }
0304 EXPORT_SYMBOL(sparc_flush_page_to_ram);
0305 
0306 static const pgprot_t protection_map[16] = {
0307     [VM_NONE]                   = PAGE_NONE,
0308     [VM_READ]                   = PAGE_READONLY,
0309     [VM_WRITE]                  = PAGE_COPY,
0310     [VM_WRITE | VM_READ]                = PAGE_COPY,
0311     [VM_EXEC]                   = PAGE_READONLY,
0312     [VM_EXEC | VM_READ]             = PAGE_READONLY,
0313     [VM_EXEC | VM_WRITE]                = PAGE_COPY,
0314     [VM_EXEC | VM_WRITE | VM_READ]          = PAGE_COPY,
0315     [VM_SHARED]                 = PAGE_NONE,
0316     [VM_SHARED | VM_READ]               = PAGE_READONLY,
0317     [VM_SHARED | VM_WRITE]              = PAGE_SHARED,
0318     [VM_SHARED | VM_WRITE | VM_READ]        = PAGE_SHARED,
0319     [VM_SHARED | VM_EXEC]               = PAGE_READONLY,
0320     [VM_SHARED | VM_EXEC | VM_READ]         = PAGE_READONLY,
0321     [VM_SHARED | VM_EXEC | VM_WRITE]        = PAGE_SHARED,
0322     [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ]  = PAGE_SHARED
0323 };
0324 DECLARE_VM_GET_PAGE_PROT