1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/arch/parisc/mm/init.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1995 Linus Torvalds 61da177e4SLinus Torvalds * Copyright 1999 SuSE GmbH 71da177e4SLinus Torvalds * changed by Philipp Rumpf 81da177e4SLinus Torvalds * Copyright 1999 Philipp Rumpf (prumpf@tux.org) 91da177e4SLinus Torvalds * Copyright 2004 Randolph Chung (tausq@debian.org) 10a8f44e38SHelge Deller * Copyright 2006-2007 Helge Deller (deller@gmx.de) 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds #include <linux/module.h> 161da177e4SLinus Torvalds #include <linux/mm.h> 174fe9e1d9SHelge Deller #include <linux/memblock.h> 185a0e3ad6STejun Heo #include <linux/gfp.h> 191da177e4SLinus Torvalds #include <linux/delay.h> 201da177e4SLinus Torvalds #include <linux/init.h> 211da177e4SLinus Torvalds #include <linux/initrd.h> 221da177e4SLinus Torvalds #include <linux/swap.h> 231da177e4SLinus Torvalds #include <linux/unistd.h> 241da177e4SLinus Torvalds #include <linux/nodemask.h> /* for node_online_map */ 25ea1754a0SKirill A. Shutemov #include <linux/pagemap.h> /* for release_pages */ 26d0cf62fbSHelge Deller #include <linux/compat.h> 270cc2dc49SMike Rapoport (IBM) #include <linux/execmem.h> 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds #include <asm/pgalloc.h> 301da177e4SLinus Torvalds #include <asm/tlb.h> 311da177e4SLinus Torvalds #include <asm/pdc_chassis.h> 321da177e4SLinus Torvalds #include <asm/mmzone.h> 33a581c2a4SHeiko Carstens #include <asm/sections.h> 34d0cf62fbSHelge Deller #include <asm/msgbuf.h> 35dbdf0760SHelge Deller #include <asm/sparsemem.h> 36e5ef93d0SHelge Deller #include <asm/asm-offsets.h> 37bc46ef3cSKent Overstreet #include <asm/shmbuf.h> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds extern int data_start; 40161bd3bfSHelge Deller extern void parisc_kernel_start(void); /* Kernel entry point in head.S */ 411da177e4SLinus Torvalds 42f24ffde4SKirill A. Shutemov #if CONFIG_PGTABLE_LEVELS == 3 4333def849SJoe Perches pmd_t pmd0[PTRS_PER_PMD] __section(".data..vm0.pmd") __attribute__ ((aligned(PAGE_SIZE))); 44c39f52a9SThomas Gleixner #endif 45c39f52a9SThomas Gleixner 4633def849SJoe Perches pgd_t swapper_pg_dir[PTRS_PER_PGD] __section(".data..vm0.pgd") __attribute__ ((aligned(PAGE_SIZE))); 4733def849SJoe Perches pte_t pg0[PT_INITIAL * PTRS_PER_PTE] __section(".data..vm0.pte") __attribute__ ((aligned(PAGE_SIZE))); 48c39f52a9SThomas Gleixner 491da177e4SLinus Torvalds static struct resource data_resource = { 501da177e4SLinus Torvalds .name = "Kernel data", 5135d98e93SToshi Kani .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM, 521da177e4SLinus Torvalds }; 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds static struct resource code_resource = { 551da177e4SLinus Torvalds .name = "Kernel code", 5635d98e93SToshi Kani .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM, 571da177e4SLinus Torvalds }; 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds static struct resource pdcdata_resource = { 601da177e4SLinus Torvalds .name = "PDC data (Page Zero)", 611da177e4SLinus Torvalds .start = 0, 621da177e4SLinus Torvalds .end = 0x9ff, 631da177e4SLinus Torvalds .flags = IORESOURCE_BUSY | IORESOURCE_MEM, 641da177e4SLinus Torvalds }; 651da177e4SLinus Torvalds 664e617c86SHelge Deller static struct resource sysram_resources[MAX_PHYSMEM_RANGES] __ro_after_init; 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /* The following array is initialized from the firmware specific 691da177e4SLinus Torvalds * information retrieved in kernel/inventory.c. 701da177e4SLinus Torvalds */ 711da177e4SLinus Torvalds 72dbdf0760SHelge Deller physmem_range_t pmem_ranges[MAX_PHYSMEM_RANGES] __initdata; 73dbdf0760SHelge Deller int npmem_ranges __initdata; 741da177e4SLinus Torvalds 75a8f44e38SHelge Deller #ifdef CONFIG_64BIT 76dbdf0760SHelge Deller #define MAX_MEM (1UL << MAX_PHYSMEM_BITS) 77a8f44e38SHelge Deller #else /* !CONFIG_64BIT */ 781da177e4SLinus Torvalds #define MAX_MEM (3584U*1024U*1024U) 79a8f44e38SHelge Deller #endif /* !CONFIG_64BIT */ 801da177e4SLinus Torvalds 818039de10SHelge Deller static unsigned long mem_limit __read_mostly = MAX_MEM; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds static void __init mem_limit_func(void) 841da177e4SLinus Torvalds { 851da177e4SLinus Torvalds char *cp, *end; 861da177e4SLinus Torvalds unsigned long limit; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds /* We need this before __setup() functions are called */ 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds limit = MAX_MEM; 91668f9931SAlon Bar-Lev for (cp = boot_command_line; *cp; ) { 921da177e4SLinus Torvalds if (memcmp(cp, "mem=", 4) == 0) { 931da177e4SLinus Torvalds cp += 4; 941da177e4SLinus Torvalds limit = memparse(cp, &end); 951da177e4SLinus Torvalds if (end != cp) 961da177e4SLinus Torvalds break; 971da177e4SLinus Torvalds cp = end; 981da177e4SLinus Torvalds } else { 991da177e4SLinus Torvalds while (*cp != ' ' && *cp) 1001da177e4SLinus Torvalds ++cp; 1011da177e4SLinus Torvalds while (*cp == ' ') 1021da177e4SLinus Torvalds ++cp; 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds if (limit < mem_limit) 1071da177e4SLinus Torvalds mem_limit = limit; 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds #define MAX_GAP (0x40000000UL >> PAGE_SHIFT) 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds static void __init setup_bootmem(void) 1131da177e4SLinus Torvalds { 1141da177e4SLinus Torvalds unsigned long mem_max; 115dbdf0760SHelge Deller #ifndef CONFIG_SPARSEMEM 1161da177e4SLinus Torvalds physmem_range_t pmem_holes[MAX_PHYSMEM_RANGES - 1]; 1171da177e4SLinus Torvalds int npmem_holes; 1181da177e4SLinus Torvalds #endif 1191da177e4SLinus Torvalds int i, sysram_resource_count; 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds disable_sr_hashing(); /* Turn off space register hashing */ 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds /* 1241da177e4SLinus Torvalds * Sort the ranges. Since the number of ranges is typically 1251da177e4SLinus Torvalds * small, and performance is not an issue here, just do 1261da177e4SLinus Torvalds * a simple insertion sort. 1271da177e4SLinus Torvalds */ 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds for (i = 1; i < npmem_ranges; i++) { 1301da177e4SLinus Torvalds int j; 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds for (j = i; j > 0; j--) { 1331da177e4SLinus Torvalds if (pmem_ranges[j-1].start_pfn < 1341da177e4SLinus Torvalds pmem_ranges[j].start_pfn) { 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds break; 1371da177e4SLinus Torvalds } 1381ae8e91eSYihao Han swap(pmem_ranges[j-1], pmem_ranges[j]); 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds 142dbdf0760SHelge Deller #ifndef CONFIG_SPARSEMEM 1431da177e4SLinus Torvalds /* 1441da177e4SLinus Torvalds * Throw out ranges that are too far apart (controlled by 1451da177e4SLinus Torvalds * MAX_GAP). 1461da177e4SLinus Torvalds */ 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds for (i = 1; i < npmem_ranges; i++) { 1491da177e4SLinus Torvalds if (pmem_ranges[i].start_pfn - 1501da177e4SLinus Torvalds (pmem_ranges[i-1].start_pfn + 1511da177e4SLinus Torvalds pmem_ranges[i-1].pages) > MAX_GAP) { 1521da177e4SLinus Torvalds npmem_ranges = i; 1531da177e4SLinus Torvalds printk("Large gap in memory detected (%ld pages). " 154dbdf0760SHelge Deller "Consider turning on CONFIG_SPARSEMEM\n", 1551da177e4SLinus Torvalds pmem_ranges[i].start_pfn - 1561da177e4SLinus Torvalds (pmem_ranges[i-1].start_pfn + 1571da177e4SLinus Torvalds pmem_ranges[i-1].pages)); 1581da177e4SLinus Torvalds break; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds #endif 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds /* Print the memory ranges */ 1644fe9e1d9SHelge Deller pr_info("Memory Ranges:\n"); 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds for (i = 0; i < npmem_ranges; i++) { 1674fe9e1d9SHelge Deller struct resource *res = &sysram_resources[i]; 1681da177e4SLinus Torvalds unsigned long start; 1691da177e4SLinus Torvalds unsigned long size; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds size = (pmem_ranges[i].pages << PAGE_SHIFT); 1721da177e4SLinus Torvalds start = (pmem_ranges[i].start_pfn << PAGE_SHIFT); 1734fe9e1d9SHelge Deller pr_info("%2d) Start 0x%016lx End 0x%016lx Size %6ld MB\n", 1741da177e4SLinus Torvalds i, start, start + (size - 1), size >> 20); 1751da177e4SLinus Torvalds 1764fe9e1d9SHelge Deller /* request memory resource */ 1771da177e4SLinus Torvalds res->name = "System RAM"; 1784fe9e1d9SHelge Deller res->start = start; 1794fe9e1d9SHelge Deller res->end = start + size - 1; 18035d98e93SToshi Kani res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 1811da177e4SLinus Torvalds request_resource(&iomem_resource, res); 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 1844fe9e1d9SHelge Deller sysram_resource_count = npmem_ranges; 1854fe9e1d9SHelge Deller 1861da177e4SLinus Torvalds /* 1871da177e4SLinus Torvalds * For 32 bit kernels we limit the amount of memory we can 1881da177e4SLinus Torvalds * support, in order to preserve enough kernel address space 1891da177e4SLinus Torvalds * for other purposes. For 64 bit kernels we don't normally 1901da177e4SLinus Torvalds * limit the memory, but this mechanism can be used to 1911da177e4SLinus Torvalds * artificially limit the amount of memory (and it is written 1921da177e4SLinus Torvalds * to work with multiple memory ranges). 1931da177e4SLinus Torvalds */ 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds mem_limit_func(); /* check for "mem=" argument */ 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds mem_max = 0; 1981da177e4SLinus Torvalds for (i = 0; i < npmem_ranges; i++) { 1991da177e4SLinus Torvalds unsigned long rsize; 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds rsize = pmem_ranges[i].pages << PAGE_SHIFT; 2021da177e4SLinus Torvalds if ((mem_max + rsize) > mem_limit) { 2031da177e4SLinus Torvalds printk(KERN_WARNING "Memory truncated to %ld MB\n", mem_limit >> 20); 2041da177e4SLinus Torvalds if (mem_max == mem_limit) 2051da177e4SLinus Torvalds npmem_ranges = i; 2061da177e4SLinus Torvalds else { 2071da177e4SLinus Torvalds pmem_ranges[i].pages = (mem_limit >> PAGE_SHIFT) 2081da177e4SLinus Torvalds - (mem_max >> PAGE_SHIFT); 2091da177e4SLinus Torvalds npmem_ranges = i + 1; 2101da177e4SLinus Torvalds mem_max = mem_limit; 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds break; 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds mem_max += rsize; 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds printk(KERN_INFO "Total Memory: %ld MB\n",mem_max >> 20); 2181da177e4SLinus Torvalds 219dbdf0760SHelge Deller #ifndef CONFIG_SPARSEMEM 2201da177e4SLinus Torvalds /* Merge the ranges, keeping track of the holes */ 2211da177e4SLinus Torvalds { 2221da177e4SLinus Torvalds unsigned long end_pfn; 2231da177e4SLinus Torvalds unsigned long hole_pages; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds npmem_holes = 0; 2261da177e4SLinus Torvalds end_pfn = pmem_ranges[0].start_pfn + pmem_ranges[0].pages; 2271da177e4SLinus Torvalds for (i = 1; i < npmem_ranges; i++) { 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds hole_pages = pmem_ranges[i].start_pfn - end_pfn; 2301da177e4SLinus Torvalds if (hole_pages) { 2311da177e4SLinus Torvalds pmem_holes[npmem_holes].start_pfn = end_pfn; 2321da177e4SLinus Torvalds pmem_holes[npmem_holes++].pages = hole_pages; 2331da177e4SLinus Torvalds end_pfn += hole_pages; 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds end_pfn += pmem_ranges[i].pages; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds pmem_ranges[0].pages = end_pfn - pmem_ranges[0].start_pfn; 2391da177e4SLinus Torvalds npmem_ranges = 1; 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds #endif 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds /* 2441da177e4SLinus Torvalds * Initialize and free the full range of memory in each range. 2451da177e4SLinus Torvalds */ 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds max_pfn = 0; 2481da177e4SLinus Torvalds for (i = 0; i < npmem_ranges; i++) { 2491da177e4SLinus Torvalds unsigned long start_pfn; 2501da177e4SLinus Torvalds unsigned long npages; 2514fe9e1d9SHelge Deller unsigned long start; 2524fe9e1d9SHelge Deller unsigned long size; 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds start_pfn = pmem_ranges[i].start_pfn; 2551da177e4SLinus Torvalds npages = pmem_ranges[i].pages; 2561da177e4SLinus Torvalds 2574fe9e1d9SHelge Deller start = start_pfn << PAGE_SHIFT; 2584fe9e1d9SHelge Deller size = npages << PAGE_SHIFT; 2594fe9e1d9SHelge Deller 2604fe9e1d9SHelge Deller /* add system RAM memblock */ 2614fe9e1d9SHelge Deller memblock_add(start, size); 2624fe9e1d9SHelge Deller 2631da177e4SLinus Torvalds if ((start_pfn + npages) > max_pfn) 2641da177e4SLinus Torvalds max_pfn = start_pfn + npages; 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2676a528001SMike Rapoport /* 2686a528001SMike Rapoport * We can't use memblock top-down allocations because we only 2696a528001SMike Rapoport * created the initial mapping up to KERNEL_INITIAL_SIZE in 2706a528001SMike Rapoport * the assembly bootup code. 2716a528001SMike Rapoport */ 2726a528001SMike Rapoport memblock_set_bottom_up(true); 2736a528001SMike Rapoport 2745cdb8205SGrant Grundler /* IOMMU is always used to access "high mem" on those boxes 2755cdb8205SGrant Grundler * that can support enough mem that a PCI device couldn't 2765cdb8205SGrant Grundler * directly DMA to any physical addresses. 2775cdb8205SGrant Grundler * ISA DMA support will need to revisit this. 2785cdb8205SGrant Grundler */ 2795cdb8205SGrant Grundler max_low_pfn = max_pfn; 2805cdb8205SGrant Grundler 2811da177e4SLinus Torvalds /* reserve PAGE0 pdc memory, kernel text/data/bss & bootmap */ 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds #define PDC_CONSOLE_IO_IODC_SIZE 32768 2841da177e4SLinus Torvalds 2854fe9e1d9SHelge Deller memblock_reserve(0UL, (unsigned long)(PAGE0->mem_free + 2864fe9e1d9SHelge Deller PDC_CONSOLE_IO_IODC_SIZE)); 2874fe9e1d9SHelge Deller memblock_reserve(__pa(KERNEL_BINARY_TEXT_START), 2884fe9e1d9SHelge Deller (unsigned long)(_end - KERNEL_BINARY_TEXT_START)); 2891da177e4SLinus Torvalds 290dbdf0760SHelge Deller #ifndef CONFIG_SPARSEMEM 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds /* reserve the holes */ 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds for (i = 0; i < npmem_holes; i++) { 2954fe9e1d9SHelge Deller memblock_reserve((pmem_holes[i].start_pfn << PAGE_SHIFT), 2964fe9e1d9SHelge Deller (pmem_holes[i].pages << PAGE_SHIFT)); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds #endif 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD 3011da177e4SLinus Torvalds if (initrd_start) { 3021da177e4SLinus Torvalds printk(KERN_INFO "initrd: %08lx-%08lx\n", initrd_start, initrd_end); 3031da177e4SLinus Torvalds if (__pa(initrd_start) < mem_max) { 3041da177e4SLinus Torvalds unsigned long initrd_reserve; 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds if (__pa(initrd_end) > mem_max) { 3071da177e4SLinus Torvalds initrd_reserve = mem_max - __pa(initrd_start); 3081da177e4SLinus Torvalds } else { 3091da177e4SLinus Torvalds initrd_reserve = initrd_end - initrd_start; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds initrd_below_start_ok = 1; 3121da177e4SLinus Torvalds printk(KERN_INFO "initrd: reserving %08lx-%08lx (mem_max %08lx)\n", __pa(initrd_start), __pa(initrd_start) + initrd_reserve, mem_max); 3131da177e4SLinus Torvalds 3144fe9e1d9SHelge Deller memblock_reserve(__pa(initrd_start), initrd_reserve); 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds #endif 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds data_resource.start = virt_to_phys(&data_start); 320c51d476aSKyle McMartin data_resource.end = virt_to_phys(_end) - 1; 321c51d476aSKyle McMartin code_resource.start = virt_to_phys(_text); 3221da177e4SLinus Torvalds code_resource.end = virt_to_phys(&data_start)-1; 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds /* We don't know which region the kernel will be in, so try 3251da177e4SLinus Torvalds * all of them. 3261da177e4SLinus Torvalds */ 3271da177e4SLinus Torvalds for (i = 0; i < sysram_resource_count; i++) { 3281da177e4SLinus Torvalds struct resource *res = &sysram_resources[i]; 3291da177e4SLinus Torvalds request_resource(res, &code_resource); 3301da177e4SLinus Torvalds request_resource(res, &data_resource); 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds request_resource(&sysram_resources[0], &pdcdata_resource); 333c9c2877dSHelge Deller 334c9c2877dSHelge Deller /* Initialize Page Deallocation Table (PDT) and check for bad memory. */ 335c9c2877dSHelge Deller pdc_pdt_init(); 336dbdf0760SHelge Deller 337dbdf0760SHelge Deller memblock_allow_resize(); 338dbdf0760SHelge Deller memblock_dump_all(); 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 3418d0e051cSHelge Deller static bool kernel_set_to_readonly; 342161bd3bfSHelge Deller 3439129886bSJohn David Anglin static void __ref map_pages(unsigned long start_vaddr, 344d7dd2ff1SJames Bottomley unsigned long start_paddr, unsigned long size, 345d7dd2ff1SJames Bottomley pgprot_t pgprot, int force) 346d7dd2ff1SJames Bottomley { 347d7dd2ff1SJames Bottomley pmd_t *pmd; 348d7dd2ff1SJames Bottomley pte_t *pg_table; 349d7dd2ff1SJames Bottomley unsigned long end_paddr; 350d7dd2ff1SJames Bottomley unsigned long start_pmd; 351d7dd2ff1SJames Bottomley unsigned long start_pte; 352d7dd2ff1SJames Bottomley unsigned long tmp1; 353d7dd2ff1SJames Bottomley unsigned long tmp2; 354d7dd2ff1SJames Bottomley unsigned long address; 355d7dd2ff1SJames Bottomley unsigned long vaddr; 356d7dd2ff1SJames Bottomley unsigned long ro_start; 357d7dd2ff1SJames Bottomley unsigned long ro_end; 3588d0e051cSHelge Deller unsigned long kernel_start, kernel_end; 359d7dd2ff1SJames Bottomley 360d7dd2ff1SJames Bottomley ro_start = __pa((unsigned long)_text); 361d7dd2ff1SJames Bottomley ro_end = __pa((unsigned long)&data_start); 3628d0e051cSHelge Deller kernel_start = __pa((unsigned long)&__init_begin); 36341b85a11SHelge Deller kernel_end = __pa((unsigned long)&_end); 364d7dd2ff1SJames Bottomley 365d7dd2ff1SJames Bottomley end_paddr = start_paddr + size; 366d7dd2ff1SJames Bottomley 3678121fbc4SMike Rapoport /* for 2-level configuration PTRS_PER_PMD is 0 so start_pmd will be 0 */ 368d7dd2ff1SJames Bottomley start_pmd = ((start_vaddr >> PMD_SHIFT) & (PTRS_PER_PMD - 1)); 369d7dd2ff1SJames Bottomley start_pte = ((start_vaddr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)); 370d7dd2ff1SJames Bottomley 371d7dd2ff1SJames Bottomley address = start_paddr; 372d7dd2ff1SJames Bottomley vaddr = start_vaddr; 373d7dd2ff1SJames Bottomley while (address < end_paddr) { 3748121fbc4SMike Rapoport pgd_t *pgd = pgd_offset_k(vaddr); 3758121fbc4SMike Rapoport p4d_t *p4d = p4d_offset(pgd, vaddr); 3768121fbc4SMike Rapoport pud_t *pud = pud_offset(p4d, vaddr); 377d7dd2ff1SJames Bottomley 3788121fbc4SMike Rapoport #if CONFIG_PGTABLE_LEVELS == 3 3798121fbc4SMike Rapoport if (pud_none(*pud)) { 3807bf82eb3SMatthew Wilcox (Oracle) pmd = memblock_alloc(PAGE_SIZE << PMD_TABLE_ORDER, 3817bf82eb3SMatthew Wilcox (Oracle) PAGE_SIZE << PMD_TABLE_ORDER); 3826a528001SMike Rapoport if (!pmd) 3836a528001SMike Rapoport panic("pmd allocation failed.\n"); 3848121fbc4SMike Rapoport pud_populate(NULL, pud, pmd); 385d7dd2ff1SJames Bottomley } 386d7dd2ff1SJames Bottomley #endif 387d7dd2ff1SJames Bottomley 3888121fbc4SMike Rapoport pmd = pmd_offset(pud, vaddr); 389d7dd2ff1SJames Bottomley for (tmp1 = start_pmd; tmp1 < PTRS_PER_PMD; tmp1++, pmd++) { 3908121fbc4SMike Rapoport if (pmd_none(*pmd)) { 3918121fbc4SMike Rapoport pg_table = memblock_alloc(PAGE_SIZE, PAGE_SIZE); 3926a528001SMike Rapoport if (!pg_table) 3936a528001SMike Rapoport panic("page table allocation failed\n"); 3948121fbc4SMike Rapoport pmd_populate_kernel(NULL, pmd, pg_table); 395d7dd2ff1SJames Bottomley } 396d7dd2ff1SJames Bottomley 3978121fbc4SMike Rapoport pg_table = pte_offset_kernel(pmd, vaddr); 398d7dd2ff1SJames Bottomley for (tmp2 = start_pte; tmp2 < PTRS_PER_PTE; tmp2++, pg_table++) { 399d7dd2ff1SJames Bottomley pte_t pte; 4008d0e051cSHelge Deller pgprot_t prot; 4018d0e051cSHelge Deller bool huge = false; 402d7dd2ff1SJames Bottomley 4038d0e051cSHelge Deller if (force) { 4048d0e051cSHelge Deller prot = pgprot; 4058d0e051cSHelge Deller } else if (address < kernel_start || address >= kernel_end) { 4068d0e051cSHelge Deller /* outside kernel memory */ 4078d0e051cSHelge Deller prot = PAGE_KERNEL; 4088d0e051cSHelge Deller } else if (!kernel_set_to_readonly) { 4098d0e051cSHelge Deller /* still initializing, allow writing to RO memory */ 4108d0e051cSHelge Deller prot = PAGE_KERNEL_RWX; 4118d0e051cSHelge Deller huge = true; 4128d0e051cSHelge Deller } else if (address >= ro_start) { 4138d0e051cSHelge Deller /* Code (ro) and Data areas */ 4148d0e051cSHelge Deller prot = (address < ro_end) ? 4158d0e051cSHelge Deller PAGE_KERNEL_EXEC : PAGE_KERNEL; 4168d0e051cSHelge Deller huge = true; 4178d0e051cSHelge Deller } else { 4188d0e051cSHelge Deller prot = PAGE_KERNEL; 41941b85a11SHelge Deller } 4208d0e051cSHelge Deller 4218d0e051cSHelge Deller pte = __mk_pte(address, prot); 4228d0e051cSHelge Deller if (huge) 42341b85a11SHelge Deller pte = pte_mkhuge(pte); 424d7dd2ff1SJames Bottomley 4253c229b3fSHelge Deller if (address >= end_paddr) 426d7dd2ff1SJames Bottomley break; 427d7dd2ff1SJames Bottomley 428d7dd2ff1SJames Bottomley set_pte(pg_table, pte); 429d7dd2ff1SJames Bottomley 430d7dd2ff1SJames Bottomley address += PAGE_SIZE; 431d7dd2ff1SJames Bottomley vaddr += PAGE_SIZE; 432d7dd2ff1SJames Bottomley } 433d7dd2ff1SJames Bottomley start_pte = 0; 434d7dd2ff1SJames Bottomley 435d7dd2ff1SJames Bottomley if (address >= end_paddr) 436d7dd2ff1SJames Bottomley break; 437d7dd2ff1SJames Bottomley } 438d7dd2ff1SJames Bottomley start_pmd = 0; 439d7dd2ff1SJames Bottomley } 440d7dd2ff1SJames Bottomley } 441d7dd2ff1SJames Bottomley 4423847dab7SHelge Deller void __init set_kernel_text_rw(int enable_read_write) 4433847dab7SHelge Deller { 444d19a1290SHelge Deller unsigned long start = (unsigned long) __init_begin; 445dfbaecb2SHelge Deller unsigned long end = (unsigned long) &data_start; 4463847dab7SHelge Deller 4473847dab7SHelge Deller map_pages(start, __pa(start), end-start, 4483847dab7SHelge Deller PAGE_KERNEL_RWX, enable_read_write ? 1:0); 4493847dab7SHelge Deller 450c9fa406fSJohn David Anglin /* force the kernel to see the new page table entries */ 451c9fa406fSJohn David Anglin flush_cache_all(); 452c9fa406fSJohn David Anglin flush_tlb_all(); 4533847dab7SHelge Deller } 4543847dab7SHelge Deller 4559129886bSJohn David Anglin void free_initmem(void) 4561da177e4SLinus Torvalds { 4574fb11781SKyle McMartin unsigned long init_begin = (unsigned long)__init_begin; 4584fb11781SKyle McMartin unsigned long init_end = (unsigned long)__init_end; 4598d0e051cSHelge Deller unsigned long kernel_end = (unsigned long)&_end; 4608d0e051cSHelge Deller 4618d0e051cSHelge Deller /* Remap kernel text and data, but do not touch init section yet. */ 4628d0e051cSHelge Deller map_pages(init_end, __pa(init_end), kernel_end - init_end, 4638d0e051cSHelge Deller PAGE_KERNEL, 0); 4641da177e4SLinus Torvalds 465d7dd2ff1SJames Bottomley /* The init text pages are marked R-X. We have to 466d7dd2ff1SJames Bottomley * flush the icache and mark them RW- 467d7dd2ff1SJames Bottomley * 468d7dd2ff1SJames Bottomley * Do a dummy remap of the data section first (the data 469d7dd2ff1SJames Bottomley * section is already PAGE_KERNEL) to pull in the TLB entries 470d7dd2ff1SJames Bottomley * for map_kernel */ 471d7dd2ff1SJames Bottomley map_pages(init_begin, __pa(init_begin), init_end - init_begin, 472d7dd2ff1SJames Bottomley PAGE_KERNEL_RWX, 1); 473d7dd2ff1SJames Bottomley /* now remap at PAGE_KERNEL since the TLB is pre-primed to execute 474d7dd2ff1SJames Bottomley * map_pages */ 475d7dd2ff1SJames Bottomley map_pages(init_begin, __pa(init_begin), init_end - init_begin, 476d7dd2ff1SJames Bottomley PAGE_KERNEL, 1); 477d7dd2ff1SJames Bottomley 478d7dd2ff1SJames Bottomley /* force the kernel to see the new TLB entries */ 4798d0e051cSHelge Deller __flush_tlb_range(0, init_begin, kernel_end); 48041b85a11SHelge Deller 481d7dd2ff1SJames Bottomley /* finally dump all the instructions which were cached, since the 482d7dd2ff1SJames Bottomley * pages are no-longer executable */ 4834fb11781SKyle McMartin flush_icache_range(init_begin, init_end); 4841da177e4SLinus Torvalds 48541b85a11SHelge Deller free_initmem_default(POISON_FREE_INITMEM); 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds /* set up a new led state on systems shipped LED State panel */ 4881da177e4SLinus Torvalds pdc_chassis_send_status(PDC_CHASSIS_DIRECT_BCOMPLETE); 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 4911bcdd854SHelge Deller 4920f5bf6d0SLaura Abbott #ifdef CONFIG_STRICT_KERNEL_RWX 4931bcdd854SHelge Deller void mark_rodata_ro(void) 4941bcdd854SHelge Deller { 495*213aa670SHelge Deller unsigned long start = (unsigned long) &__start_rodata; 496*213aa670SHelge Deller unsigned long end = (unsigned long) &__end_rodata; 4978d0e051cSHelge Deller 498*213aa670SHelge Deller pr_info("Write protecting the kernel read-only data: %luk\n", 499*213aa670SHelge Deller (end - start) >> 10); 500*213aa670SHelge Deller 501*213aa670SHelge Deller kernel_set_to_readonly = true; 502*213aa670SHelge Deller map_pages(start, __pa(start), end - start, PAGE_KERNEL, 0); 503*213aa670SHelge Deller 504*213aa670SHelge Deller /* force the kernel to see the new page table entries */ 505*213aa670SHelge Deller flush_cache_all(); 506*213aa670SHelge Deller flush_tlb_all(); 5071bcdd854SHelge Deller } 5081bcdd854SHelge Deller #endif 5091bcdd854SHelge Deller 5101bcdd854SHelge Deller 5111da177e4SLinus Torvalds /* 5121da177e4SLinus Torvalds * Just an arbitrary offset to serve as a "hole" between mapping areas 5131da177e4SLinus Torvalds * (between top of physical memory and a potential pcxl dma mapping 5141da177e4SLinus Torvalds * area, and below the vmalloc mapping area). 5151da177e4SLinus Torvalds * 5161da177e4SLinus Torvalds * The current 32K value just means that there will be a 32K "hole" 5171da177e4SLinus Torvalds * between mapping areas. That means that any out-of-bounds memory 5181da177e4SLinus Torvalds * accesses will hopefully be caught. The vmalloc() routines leaves 5191da177e4SLinus Torvalds * a hole of 4kB between each vmalloced area for the same reason. 5201da177e4SLinus Torvalds */ 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds /* Leave room for gateway page expansion */ 5231da177e4SLinus Torvalds #if KERNEL_MAP_START < GATEWAY_PAGE_SIZE 5241da177e4SLinus Torvalds #error KERNEL_MAP_START is in gateway reserved region 5251da177e4SLinus Torvalds #endif 5261da177e4SLinus Torvalds #define MAP_START (KERNEL_MAP_START) 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds #define VM_MAP_OFFSET (32*1024) 5291da177e4SLinus Torvalds #define SET_MAP_OFFSET(x) ((void *)(((unsigned long)(x) + VM_MAP_OFFSET) \ 5301da177e4SLinus Torvalds & ~(VM_MAP_OFFSET-1))) 5311da177e4SLinus Torvalds 5324e617c86SHelge Deller void *parisc_vmalloc_start __ro_after_init; 5334255f0d2SHelge Deller EXPORT_SYMBOL(parisc_vmalloc_start); 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds void __init mem_init(void) 5361da177e4SLinus Torvalds { 537d0cf62fbSHelge Deller /* Do sanity checks on IPC (compat) structures */ 538d0cf62fbSHelge Deller BUILD_BUG_ON(sizeof(struct ipc64_perm) != 48); 539d0cf62fbSHelge Deller #ifndef CONFIG_64BIT 540d0cf62fbSHelge Deller BUILD_BUG_ON(sizeof(struct semid64_ds) != 80); 541d0cf62fbSHelge Deller BUILD_BUG_ON(sizeof(struct msqid64_ds) != 104); 542d0cf62fbSHelge Deller BUILD_BUG_ON(sizeof(struct shmid64_ds) != 104); 543d0cf62fbSHelge Deller #endif 544d0cf62fbSHelge Deller #ifdef CONFIG_COMPAT 545d0cf62fbSHelge Deller BUILD_BUG_ON(sizeof(struct compat_ipc64_perm) != sizeof(struct ipc64_perm)); 546d0cf62fbSHelge Deller BUILD_BUG_ON(sizeof(struct compat_semid64_ds) != 80); 547d0cf62fbSHelge Deller BUILD_BUG_ON(sizeof(struct compat_msqid64_ds) != 104); 548d0cf62fbSHelge Deller BUILD_BUG_ON(sizeof(struct compat_shmid64_ds) != 104); 549d0cf62fbSHelge Deller #endif 550d0cf62fbSHelge Deller 55148d27cb2SHelge Deller /* Do sanity checks on page table constants */ 55248d27cb2SHelge Deller BUILD_BUG_ON(PTE_ENTRY_SIZE != sizeof(pte_t)); 55348d27cb2SHelge Deller BUILD_BUG_ON(PMD_ENTRY_SIZE != sizeof(pmd_t)); 55448d27cb2SHelge Deller BUILD_BUG_ON(PGD_ENTRY_SIZE != sizeof(pgd_t)); 55548d27cb2SHelge Deller BUILD_BUG_ON(PAGE_SHIFT + BITS_PER_PTE + BITS_PER_PMD + BITS_PER_PGD 55648d27cb2SHelge Deller > BITS_PER_LONG); 557b7795074SHelge Deller #if CONFIG_PGTABLE_LEVELS == 3 558b7795074SHelge Deller BUILD_BUG_ON(PT_INITIAL > PTRS_PER_PMD); 559b7795074SHelge Deller #else 560b7795074SHelge Deller BUILD_BUG_ON(PT_INITIAL > PTRS_PER_PGD); 561b7795074SHelge Deller #endif 56248d27cb2SHelge Deller 56344eeb9b5SHelge Deller #ifdef CONFIG_64BIT 56444eeb9b5SHelge Deller /* avoid ldil_%L() asm statements to sign-extend into upper 32-bits */ 56544eeb9b5SHelge Deller BUILD_BUG_ON(__PAGE_OFFSET >= 0x80000000); 56644eeb9b5SHelge Deller BUILD_BUG_ON(TMPALIAS_MAP_START >= 0x80000000); 56744eeb9b5SHelge Deller #endif 56844eeb9b5SHelge Deller 5691da177e4SLinus Torvalds high_memory = __va((max_pfn << PAGE_SHIFT)); 570bf71bc16SHelge Deller set_max_mapnr(max_low_pfn); 571c6ffc5caSMike Rapoport memblock_free_all(); 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds #ifdef CONFIG_PA11 574a34a9b96SChristoph Hellwig if (boot_cpu_data.cpu_type == pcxl2 || boot_cpu_data.cpu_type == pcxl) { 5751da177e4SLinus Torvalds pcxl_dma_start = (unsigned long)SET_MAP_OFFSET(MAP_START); 5764255f0d2SHelge Deller parisc_vmalloc_start = SET_MAP_OFFSET(pcxl_dma_start 5774255f0d2SHelge Deller + PCXL_DMA_MAP_SIZE); 578a34a9b96SChristoph Hellwig } else 5791da177e4SLinus Torvalds #endif 580a34a9b96SChristoph Hellwig parisc_vmalloc_start = SET_MAP_OFFSET(MAP_START); 5811da177e4SLinus Torvalds 582fd8d0ca2SHelge Deller #if 0 583fd8d0ca2SHelge Deller /* 584fd8d0ca2SHelge Deller * Do not expose the virtual kernel memory layout to userspace. 585fd8d0ca2SHelge Deller * But keep code for debugging purposes. 586fd8d0ca2SHelge Deller */ 587ce8420bbSHelge Deller printk("virtual kernel memory layout:\n" 58863b2c373SHelge Deller " vmalloc : 0x%px - 0x%px (%4ld MB)\n" 589ccfbc68dSSven Schnelle " fixmap : 0x%px - 0x%px (%4ld kB)\n" 59063b2c373SHelge Deller " memory : 0x%px - 0x%px (%4ld MB)\n" 59163b2c373SHelge Deller " .init : 0x%px - 0x%px (%4ld kB)\n" 59263b2c373SHelge Deller " .data : 0x%px - 0x%px (%4ld kB)\n" 59363b2c373SHelge Deller " .text : 0x%px - 0x%px (%4ld kB)\n", 594ce8420bbSHelge Deller 595ce8420bbSHelge Deller (void*)VMALLOC_START, (void*)VMALLOC_END, 596ce8420bbSHelge Deller (VMALLOC_END - VMALLOC_START) >> 20, 597ce8420bbSHelge Deller 598ccfbc68dSSven Schnelle (void *)FIXMAP_START, (void *)(FIXMAP_START + FIXMAP_SIZE), 599ccfbc68dSSven Schnelle (unsigned long)(FIXMAP_SIZE / 1024), 600ccfbc68dSSven Schnelle 601ce8420bbSHelge Deller __va(0), high_memory, 602ce8420bbSHelge Deller ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20, 603ce8420bbSHelge Deller 60453faf291SKyle McMartin __init_begin, __init_end, 60553faf291SKyle McMartin ((unsigned long)__init_end - (unsigned long)__init_begin) >> 10, 606ce8420bbSHelge Deller 60753faf291SKyle McMartin _etext, _edata, 60853faf291SKyle McMartin ((unsigned long)_edata - (unsigned long)_etext) >> 10, 609ce8420bbSHelge Deller 61053faf291SKyle McMartin _text, _etext, 61153faf291SKyle McMartin ((unsigned long)_etext - (unsigned long)_text) >> 10); 612ce8420bbSHelge Deller #endif 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds 6154e617c86SHelge Deller unsigned long *empty_zero_page __ro_after_init; 61622febf1fSKyle McMartin EXPORT_SYMBOL(empty_zero_page); 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds /* 6191da177e4SLinus Torvalds * pagetable_init() sets up the page tables 6201da177e4SLinus Torvalds * 6211da177e4SLinus Torvalds * Note that gateway_init() places the Linux gateway page at page 0. 6221da177e4SLinus Torvalds * Since gateway pages cannot be dereferenced this has the desirable 6231da177e4SLinus Torvalds * side effect of trapping those pesky NULL-reference errors in the 6241da177e4SLinus Torvalds * kernel. 6251da177e4SLinus Torvalds */ 6261da177e4SLinus Torvalds static void __init pagetable_init(void) 6271da177e4SLinus Torvalds { 6281da177e4SLinus Torvalds int range; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds /* Map each physical memory range to its kernel vaddr */ 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds for (range = 0; range < npmem_ranges; range++) { 6331da177e4SLinus Torvalds unsigned long start_paddr; 6341da177e4SLinus Torvalds unsigned long size; 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds start_paddr = pmem_ranges[range].start_pfn << PAGE_SHIFT; 6371da177e4SLinus Torvalds size = pmem_ranges[range].pages << PAGE_SHIFT; 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds map_pages((unsigned long)__va(start_paddr), start_paddr, 640d7dd2ff1SJames Bottomley size, PAGE_KERNEL, 0); 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD 6441da177e4SLinus Torvalds if (initrd_end && initrd_end > mem_limit) { 6451bcdd854SHelge Deller printk(KERN_INFO "initrd: mapping %08lx-%08lx\n", initrd_start, initrd_end); 6461da177e4SLinus Torvalds map_pages(initrd_start, __pa(initrd_start), 647d7dd2ff1SJames Bottomley initrd_end - initrd_start, PAGE_KERNEL, 0); 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds #endif 6501da177e4SLinus Torvalds 6516a528001SMike Rapoport empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE); 6526a528001SMike Rapoport if (!empty_zero_page) 6536a528001SMike Rapoport panic("zero page allocation failed.\n"); 6546a528001SMike Rapoport 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds static void __init gateway_init(void) 6581da177e4SLinus Torvalds { 6591da177e4SLinus Torvalds unsigned long linux_gateway_page_addr; 6601da177e4SLinus Torvalds /* FIXME: This is 'const' in order to trick the compiler 6611da177e4SLinus Torvalds into not treating it as DP-relative data. */ 6621da177e4SLinus Torvalds extern void * const linux_gateway_page; 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds linux_gateway_page_addr = LINUX_GATEWAY_ADDR & PAGE_MASK; 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds /* 6671da177e4SLinus Torvalds * Setup Linux Gateway page. 6681da177e4SLinus Torvalds * 6691da177e4SLinus Torvalds * The Linux gateway page will reside in kernel space (on virtual 6701da177e4SLinus Torvalds * page 0), so it doesn't need to be aliased into user space. 6711da177e4SLinus Torvalds */ 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds map_pages(linux_gateway_page_addr, __pa(&linux_gateway_page), 674d7dd2ff1SJames Bottomley PAGE_SIZE, PAGE_GATEWAY, 1); 6751da177e4SLinus Torvalds } 6761da177e4SLinus Torvalds 677c2ff2b73SMike Rapoport (IBM) static void __init fixmap_init(void) 678c2ff2b73SMike Rapoport (IBM) { 679c2ff2b73SMike Rapoport (IBM) unsigned long addr = FIXMAP_START; 680c2ff2b73SMike Rapoport (IBM) unsigned long end = FIXMAP_START + FIXMAP_SIZE; 681c2ff2b73SMike Rapoport (IBM) pgd_t *pgd = pgd_offset_k(addr); 682c2ff2b73SMike Rapoport (IBM) p4d_t *p4d = p4d_offset(pgd, addr); 683c2ff2b73SMike Rapoport (IBM) pud_t *pud = pud_offset(p4d, addr); 684c2ff2b73SMike Rapoport (IBM) pmd_t *pmd; 685c2ff2b73SMike Rapoport (IBM) 686c2ff2b73SMike Rapoport (IBM) BUILD_BUG_ON(FIXMAP_SIZE > PMD_SIZE); 687c2ff2b73SMike Rapoport (IBM) 688c2ff2b73SMike Rapoport (IBM) #if CONFIG_PGTABLE_LEVELS == 3 689c2ff2b73SMike Rapoport (IBM) if (pud_none(*pud)) { 690c2ff2b73SMike Rapoport (IBM) pmd = memblock_alloc(PAGE_SIZE << PMD_TABLE_ORDER, 691c2ff2b73SMike Rapoport (IBM) PAGE_SIZE << PMD_TABLE_ORDER); 692c2ff2b73SMike Rapoport (IBM) if (!pmd) 693c2ff2b73SMike Rapoport (IBM) panic("fixmap: pmd allocation failed.\n"); 694c2ff2b73SMike Rapoport (IBM) pud_populate(NULL, pud, pmd); 695c2ff2b73SMike Rapoport (IBM) } 696c2ff2b73SMike Rapoport (IBM) #endif 697c2ff2b73SMike Rapoport (IBM) 698c2ff2b73SMike Rapoport (IBM) pmd = pmd_offset(pud, addr); 699c2ff2b73SMike Rapoport (IBM) do { 700c2ff2b73SMike Rapoport (IBM) pte_t *pte = memblock_alloc(PAGE_SIZE, PAGE_SIZE); 701c2ff2b73SMike Rapoport (IBM) if (!pte) 702c2ff2b73SMike Rapoport (IBM) panic("fixmap: pte allocation failed.\n"); 703c2ff2b73SMike Rapoport (IBM) 704c2ff2b73SMike Rapoport (IBM) pmd_populate_kernel(&init_mm, pmd, pte); 705c2ff2b73SMike Rapoport (IBM) 706c2ff2b73SMike Rapoport (IBM) addr += PAGE_SIZE; 707c2ff2b73SMike Rapoport (IBM) } while (addr < end); 708c2ff2b73SMike Rapoport (IBM) } 709c2ff2b73SMike Rapoport (IBM) 710dbdf0760SHelge Deller static void __init parisc_bootmem_free(void) 7111da177e4SLinus Torvalds { 712625bf73eSMike Rapoport unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0, }; 7131da177e4SLinus Torvalds 714625bf73eSMike Rapoport max_zone_pfn[0] = memblock_end_of_DRAM(); 715dbdf0760SHelge Deller 716625bf73eSMike Rapoport free_area_init(max_zone_pfn); 717dbdf0760SHelge Deller } 718dbdf0760SHelge Deller 719dbdf0760SHelge Deller void __init paging_init(void) 720dbdf0760SHelge Deller { 7211da177e4SLinus Torvalds setup_bootmem(); 7221da177e4SLinus Torvalds pagetable_init(); 7231da177e4SLinus Torvalds gateway_init(); 724c2ff2b73SMike Rapoport (IBM) fixmap_init(); 7251da177e4SLinus Torvalds flush_cache_all_local(); /* start with known state */ 726ce33941fSMatthew Wilcox flush_tlb_all_local(NULL); 7271da177e4SLinus Torvalds 728dbdf0760SHelge Deller sparse_init(); 729dbdf0760SHelge Deller parisc_bootmem_free(); 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds 732e5ef93d0SHelge Deller static void alloc_btlb(unsigned long start, unsigned long end, int *slot, 733e5ef93d0SHelge Deller unsigned long entry_info) 734e5ef93d0SHelge Deller { 735e5ef93d0SHelge Deller const int slot_max = btlb_info.fixed_range_info.num_comb; 736e5ef93d0SHelge Deller int min_num_pages = btlb_info.min_size; 737e5ef93d0SHelge Deller unsigned long size; 738e5ef93d0SHelge Deller 739e5ef93d0SHelge Deller /* map at minimum 4 pages */ 740e5ef93d0SHelge Deller if (min_num_pages < 4) 741e5ef93d0SHelge Deller min_num_pages = 4; 742e5ef93d0SHelge Deller 743e5ef93d0SHelge Deller size = HUGEPAGE_SIZE; 744e5ef93d0SHelge Deller while (start < end && *slot < slot_max && size >= PAGE_SIZE) { 745e5ef93d0SHelge Deller /* starting address must have same alignment as size! */ 746e5ef93d0SHelge Deller /* if correctly aligned and fits in double size, increase */ 747e5ef93d0SHelge Deller if (((start & (2 * size - 1)) == 0) && 748e5ef93d0SHelge Deller (end - start) >= (2 * size)) { 749e5ef93d0SHelge Deller size <<= 1; 750e5ef93d0SHelge Deller continue; 751e5ef93d0SHelge Deller } 752e5ef93d0SHelge Deller /* if current size alignment is too big, try smaller size */ 753e5ef93d0SHelge Deller if ((start & (size - 1)) != 0) { 754e5ef93d0SHelge Deller size >>= 1; 755e5ef93d0SHelge Deller continue; 756e5ef93d0SHelge Deller } 757e5ef93d0SHelge Deller if ((end - start) >= size) { 758e5ef93d0SHelge Deller if ((size >> PAGE_SHIFT) >= min_num_pages) 759e5ef93d0SHelge Deller pdc_btlb_insert(start >> PAGE_SHIFT, __pa(start) >> PAGE_SHIFT, 760e5ef93d0SHelge Deller size >> PAGE_SHIFT, entry_info, *slot); 761e5ef93d0SHelge Deller (*slot)++; 762e5ef93d0SHelge Deller start += size; 763e5ef93d0SHelge Deller continue; 764e5ef93d0SHelge Deller } 765e5ef93d0SHelge Deller size /= 2; 766e5ef93d0SHelge Deller continue; 767e5ef93d0SHelge Deller } 768e5ef93d0SHelge Deller } 769e5ef93d0SHelge Deller 770e5ef93d0SHelge Deller void btlb_init_per_cpu(void) 771e5ef93d0SHelge Deller { 772e5ef93d0SHelge Deller unsigned long s, t, e; 773e5ef93d0SHelge Deller int slot; 774e5ef93d0SHelge Deller 775e5ef93d0SHelge Deller /* BTLBs are not available on 64-bit CPUs */ 776e5ef93d0SHelge Deller if (IS_ENABLED(CONFIG_PA20)) 777e5ef93d0SHelge Deller return; 778e5ef93d0SHelge Deller else if (pdc_btlb_info(&btlb_info) < 0) { 779e5ef93d0SHelge Deller memset(&btlb_info, 0, sizeof btlb_info); 780e5ef93d0SHelge Deller } 781e5ef93d0SHelge Deller 782e5ef93d0SHelge Deller /* insert BLTLBs for code and data segments */ 783e5ef93d0SHelge Deller s = (uintptr_t) dereference_function_descriptor(&_stext); 784e5ef93d0SHelge Deller e = (uintptr_t) dereference_function_descriptor(&_etext); 785e5ef93d0SHelge Deller t = (uintptr_t) dereference_function_descriptor(&_sdata); 786e5ef93d0SHelge Deller BUG_ON(t != e); 787e5ef93d0SHelge Deller 788e5ef93d0SHelge Deller /* code segments */ 789e5ef93d0SHelge Deller slot = 0; 790e5ef93d0SHelge Deller alloc_btlb(s, e, &slot, 0x13800000); 791e5ef93d0SHelge Deller 792e5ef93d0SHelge Deller /* sanity check */ 793e5ef93d0SHelge Deller t = (uintptr_t) dereference_function_descriptor(&_edata); 794e5ef93d0SHelge Deller e = (uintptr_t) dereference_function_descriptor(&__bss_start); 795e5ef93d0SHelge Deller BUG_ON(t != e); 796e5ef93d0SHelge Deller 797e5ef93d0SHelge Deller /* data segments */ 798e5ef93d0SHelge Deller s = (uintptr_t) dereference_function_descriptor(&_sdata); 799e5ef93d0SHelge Deller e = (uintptr_t) dereference_function_descriptor(&__bss_stop); 800e5ef93d0SHelge Deller alloc_btlb(s, e, &slot, 0x11800000); 801e5ef93d0SHelge Deller } 802e5ef93d0SHelge Deller 8031da177e4SLinus Torvalds #ifdef CONFIG_PA20 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds /* 8067022672eSSimon Arlott * Currently, all PA20 chips have 18 bit protection IDs, which is the 8071da177e4SLinus Torvalds * limiting factor (space ids are 32 bits). 8081da177e4SLinus Torvalds */ 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds #define NR_SPACE_IDS 262144 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds #else 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds /* 8157022672eSSimon Arlott * Currently we have a one-to-one relationship between space IDs and 8167022672eSSimon Arlott * protection IDs. Older parisc chips (PCXS, PCXT, PCXL, PCXL2) only 8177022672eSSimon Arlott * support 15 bit protection IDs, so that is the limiting factor. 8187022672eSSimon Arlott * PCXT' has 18 bit protection IDs, but only 16 bit spaceids, so it's 8191da177e4SLinus Torvalds * probably not worth the effort for a special case here. 8201da177e4SLinus Torvalds */ 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds #define NR_SPACE_IDS 32768 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds #endif /* !CONFIG_PA20 */ 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds #define RECYCLE_THRESHOLD (NR_SPACE_IDS / 2) 8271da177e4SLinus Torvalds #define SID_ARRAY_SIZE (NR_SPACE_IDS / (8 * sizeof(long))) 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds static unsigned long space_id[SID_ARRAY_SIZE] = { 1 }; /* disallow space 0 */ 8301da177e4SLinus Torvalds static unsigned long dirty_space_id[SID_ARRAY_SIZE]; 8311da177e4SLinus Torvalds static unsigned long space_id_index; 8321da177e4SLinus Torvalds static unsigned long free_space_ids = NR_SPACE_IDS - 1; 833cbe263b6SJason Wang static unsigned long dirty_space_ids; 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds static DEFINE_SPINLOCK(sid_lock); 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds unsigned long alloc_sid(void) 8381da177e4SLinus Torvalds { 8391da177e4SLinus Torvalds unsigned long index; 8401da177e4SLinus Torvalds 8411da177e4SLinus Torvalds spin_lock(&sid_lock); 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds if (free_space_ids == 0) { 8441da177e4SLinus Torvalds if (dirty_space_ids != 0) { 8451da177e4SLinus Torvalds spin_unlock(&sid_lock); 8461da177e4SLinus Torvalds flush_tlb_all(); /* flush_tlb_all() calls recycle_sids() */ 8471da177e4SLinus Torvalds spin_lock(&sid_lock); 8481da177e4SLinus Torvalds } 8492fd83038SHelge Deller BUG_ON(free_space_ids == 0); 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds free_space_ids--; 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds index = find_next_zero_bit(space_id, NR_SPACE_IDS, space_id_index); 855208151bfSHelge Deller space_id[BIT_WORD(index)] |= BIT_MASK(index); 8561da177e4SLinus Torvalds space_id_index = index; 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds spin_unlock(&sid_lock); 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds return index << SPACEID_SHIFT; 8611da177e4SLinus Torvalds } 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds void free_sid(unsigned long spaceid) 8641da177e4SLinus Torvalds { 8651da177e4SLinus Torvalds unsigned long index = spaceid >> SPACEID_SHIFT; 866208151bfSHelge Deller unsigned long *dirty_space_offset, mask; 8671da177e4SLinus Torvalds 868208151bfSHelge Deller dirty_space_offset = &dirty_space_id[BIT_WORD(index)]; 869208151bfSHelge Deller mask = BIT_MASK(index); 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds spin_lock(&sid_lock); 8721da177e4SLinus Torvalds 873208151bfSHelge Deller BUG_ON(*dirty_space_offset & mask); /* attempt to free space id twice */ 8741da177e4SLinus Torvalds 875208151bfSHelge Deller *dirty_space_offset |= mask; 8761da177e4SLinus Torvalds dirty_space_ids++; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds spin_unlock(&sid_lock); 8791da177e4SLinus Torvalds } 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds #ifdef CONFIG_SMP 8831da177e4SLinus Torvalds static void get_dirty_sids(unsigned long *ndirtyptr,unsigned long *dirty_array) 8841da177e4SLinus Torvalds { 8851da177e4SLinus Torvalds int i; 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds /* NOTE: sid_lock must be held upon entry */ 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds *ndirtyptr = dirty_space_ids; 8901da177e4SLinus Torvalds if (dirty_space_ids != 0) { 8911da177e4SLinus Torvalds for (i = 0; i < SID_ARRAY_SIZE; i++) { 8921da177e4SLinus Torvalds dirty_array[i] = dirty_space_id[i]; 8931da177e4SLinus Torvalds dirty_space_id[i] = 0; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds dirty_space_ids = 0; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds return; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds static void recycle_sids(unsigned long ndirty,unsigned long *dirty_array) 9021da177e4SLinus Torvalds { 9031da177e4SLinus Torvalds int i; 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds /* NOTE: sid_lock must be held upon entry */ 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds if (ndirty != 0) { 9081da177e4SLinus Torvalds for (i = 0; i < SID_ARRAY_SIZE; i++) { 9091da177e4SLinus Torvalds space_id[i] ^= dirty_array[i]; 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds free_space_ids += ndirty; 9131da177e4SLinus Torvalds space_id_index = 0; 9141da177e4SLinus Torvalds } 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds #else /* CONFIG_SMP */ 9181da177e4SLinus Torvalds 9191da177e4SLinus Torvalds static void recycle_sids(void) 9201da177e4SLinus Torvalds { 9211da177e4SLinus Torvalds int i; 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds /* NOTE: sid_lock must be held upon entry */ 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds if (dirty_space_ids != 0) { 9261da177e4SLinus Torvalds for (i = 0; i < SID_ARRAY_SIZE; i++) { 9271da177e4SLinus Torvalds space_id[i] ^= dirty_space_id[i]; 9281da177e4SLinus Torvalds dirty_space_id[i] = 0; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds free_space_ids += dirty_space_ids; 9321da177e4SLinus Torvalds dirty_space_ids = 0; 9331da177e4SLinus Torvalds space_id_index = 0; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds #endif 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds /* 9391da177e4SLinus Torvalds * flush_tlb_all() calls recycle_sids(), since whenever the entire tlb is 9401da177e4SLinus Torvalds * purged, we can safely reuse the space ids that were released but 9411da177e4SLinus Torvalds * not flushed from the tlb. 9421da177e4SLinus Torvalds */ 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds #ifdef CONFIG_SMP 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds static unsigned long recycle_ndirty; 9471da177e4SLinus Torvalds static unsigned long recycle_dirty_array[SID_ARRAY_SIZE]; 9482fd83038SHelge Deller static unsigned int recycle_inuse; 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds void flush_tlb_all(void) 9511da177e4SLinus Torvalds { 9521da177e4SLinus Torvalds int do_recycle; 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds do_recycle = 0; 9551da177e4SLinus Torvalds spin_lock(&sid_lock); 9561030d681SSven Schnelle __inc_irq_stat(irq_tlb_count); 9571da177e4SLinus Torvalds if (dirty_space_ids > RECYCLE_THRESHOLD) { 9582fd83038SHelge Deller BUG_ON(recycle_inuse); /* FIXME: Use a semaphore/wait queue here */ 9591da177e4SLinus Torvalds get_dirty_sids(&recycle_ndirty,recycle_dirty_array); 9601da177e4SLinus Torvalds recycle_inuse++; 9611da177e4SLinus Torvalds do_recycle++; 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds spin_unlock(&sid_lock); 96415c8b6c1SJens Axboe on_each_cpu(flush_tlb_all_local, NULL, 1); 9651da177e4SLinus Torvalds if (do_recycle) { 9661da177e4SLinus Torvalds spin_lock(&sid_lock); 9671da177e4SLinus Torvalds recycle_sids(recycle_ndirty,recycle_dirty_array); 9681da177e4SLinus Torvalds recycle_inuse = 0; 9691da177e4SLinus Torvalds spin_unlock(&sid_lock); 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds } 9721da177e4SLinus Torvalds #else 9731da177e4SLinus Torvalds void flush_tlb_all(void) 9741da177e4SLinus Torvalds { 9751da177e4SLinus Torvalds spin_lock(&sid_lock); 9761030d681SSven Schnelle __inc_irq_stat(irq_tlb_count); 9771b2425e3SMatthew Wilcox flush_tlb_all_local(NULL); 9781da177e4SLinus Torvalds recycle_sids(); 9791da177e4SLinus Torvalds spin_unlock(&sid_lock); 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds #endif 982252358f1SAnshuman Khandual 983252358f1SAnshuman Khandual static const pgprot_t protection_map[16] = { 984252358f1SAnshuman Khandual [VM_NONE] = PAGE_NONE, 985252358f1SAnshuman Khandual [VM_READ] = PAGE_READONLY, 986252358f1SAnshuman Khandual [VM_WRITE] = PAGE_NONE, 987252358f1SAnshuman Khandual [VM_WRITE | VM_READ] = PAGE_READONLY, 988252358f1SAnshuman Khandual [VM_EXEC] = PAGE_EXECREAD, 989252358f1SAnshuman Khandual [VM_EXEC | VM_READ] = PAGE_EXECREAD, 990252358f1SAnshuman Khandual [VM_EXEC | VM_WRITE] = PAGE_EXECREAD, 991252358f1SAnshuman Khandual [VM_EXEC | VM_WRITE | VM_READ] = PAGE_EXECREAD, 992252358f1SAnshuman Khandual [VM_SHARED] = PAGE_NONE, 993252358f1SAnshuman Khandual [VM_SHARED | VM_READ] = PAGE_READONLY, 994252358f1SAnshuman Khandual [VM_SHARED | VM_WRITE] = PAGE_WRITEONLY, 995252358f1SAnshuman Khandual [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED, 996252358f1SAnshuman Khandual [VM_SHARED | VM_EXEC] = PAGE_EXECREAD, 997252358f1SAnshuman Khandual [VM_SHARED | VM_EXEC | VM_READ] = PAGE_EXECREAD, 998252358f1SAnshuman Khandual [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_RWX, 999252358f1SAnshuman Khandual [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_RWX 1000252358f1SAnshuman Khandual }; 1001252358f1SAnshuman Khandual DECLARE_VM_GET_PAGE_PROT 10020cc2dc49SMike Rapoport (IBM) 10030cc2dc49SMike Rapoport (IBM) #ifdef CONFIG_EXECMEM 10040cc2dc49SMike Rapoport (IBM) static struct execmem_info execmem_info __ro_after_init; 10050cc2dc49SMike Rapoport (IBM) 10060cc2dc49SMike Rapoport (IBM) struct execmem_info __init *execmem_arch_setup(void) 10070cc2dc49SMike Rapoport (IBM) { 10080cc2dc49SMike Rapoport (IBM) execmem_info = (struct execmem_info){ 10090cc2dc49SMike Rapoport (IBM) .ranges = { 10100cc2dc49SMike Rapoport (IBM) [EXECMEM_DEFAULT] = { 10110cc2dc49SMike Rapoport (IBM) .start = VMALLOC_START, 10120cc2dc49SMike Rapoport (IBM) .end = VMALLOC_END, 10130cc2dc49SMike Rapoport (IBM) .pgprot = PAGE_KERNEL_RWX, 10140cc2dc49SMike Rapoport (IBM) .alignment = 1, 10150cc2dc49SMike Rapoport (IBM) }, 10160cc2dc49SMike Rapoport (IBM) }, 10170cc2dc49SMike Rapoport (IBM) }; 10180cc2dc49SMike Rapoport (IBM) 10190cc2dc49SMike Rapoport (IBM) return &execmem_info; 10200cc2dc49SMike Rapoport (IBM) } 10210cc2dc49SMike Rapoport (IBM) #endif /* CONFIG_EXECMEM */ 1022