xref: /kernel/linux/linux-5.10/arch/parisc/mm/init.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  linux/arch/parisc/mm/init.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 1995	Linus Torvalds
68c2ecf20Sopenharmony_ci *  Copyright 1999 SuSE GmbH
78c2ecf20Sopenharmony_ci *    changed by Philipp Rumpf
88c2ecf20Sopenharmony_ci *  Copyright 1999 Philipp Rumpf (prumpf@tux.org)
98c2ecf20Sopenharmony_ci *  Copyright 2004 Randolph Chung (tausq@debian.org)
108c2ecf20Sopenharmony_ci *  Copyright 2006-2007 Helge Deller (deller@gmx.de)
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/mm.h>
178c2ecf20Sopenharmony_ci#include <linux/memblock.h>
188c2ecf20Sopenharmony_ci#include <linux/gfp.h>
198c2ecf20Sopenharmony_ci#include <linux/delay.h>
208c2ecf20Sopenharmony_ci#include <linux/init.h>
218c2ecf20Sopenharmony_ci#include <linux/initrd.h>
228c2ecf20Sopenharmony_ci#include <linux/swap.h>
238c2ecf20Sopenharmony_ci#include <linux/unistd.h>
248c2ecf20Sopenharmony_ci#include <linux/nodemask.h>	/* for node_online_map */
258c2ecf20Sopenharmony_ci#include <linux/pagemap.h>	/* for release_pages */
268c2ecf20Sopenharmony_ci#include <linux/compat.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <asm/pgalloc.h>
298c2ecf20Sopenharmony_ci#include <asm/tlb.h>
308c2ecf20Sopenharmony_ci#include <asm/pdc_chassis.h>
318c2ecf20Sopenharmony_ci#include <asm/mmzone.h>
328c2ecf20Sopenharmony_ci#include <asm/sections.h>
338c2ecf20Sopenharmony_ci#include <asm/msgbuf.h>
348c2ecf20Sopenharmony_ci#include <asm/sparsemem.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ciextern int  data_start;
378c2ecf20Sopenharmony_ciextern void parisc_kernel_start(void);	/* Kernel entry point in head.S */
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#if CONFIG_PGTABLE_LEVELS == 3
408c2ecf20Sopenharmony_cipmd_t pmd0[PTRS_PER_PMD] __section(".data..vm0.pmd") __attribute__ ((aligned(PAGE_SIZE)));
418c2ecf20Sopenharmony_ci#endif
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cipgd_t swapper_pg_dir[PTRS_PER_PGD] __section(".data..vm0.pgd") __attribute__ ((aligned(PAGE_SIZE)));
448c2ecf20Sopenharmony_cipte_t pg0[PT_INITIAL * PTRS_PER_PTE] __section(".data..vm0.pte") __attribute__ ((aligned(PAGE_SIZE)));
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic struct resource data_resource = {
478c2ecf20Sopenharmony_ci	.name	= "Kernel data",
488c2ecf20Sopenharmony_ci	.flags	= IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic struct resource code_resource = {
528c2ecf20Sopenharmony_ci	.name	= "Kernel code",
538c2ecf20Sopenharmony_ci	.flags	= IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
548c2ecf20Sopenharmony_ci};
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic struct resource pdcdata_resource = {
578c2ecf20Sopenharmony_ci	.name	= "PDC data (Page Zero)",
588c2ecf20Sopenharmony_ci	.start	= 0,
598c2ecf20Sopenharmony_ci	.end	= 0x9ff,
608c2ecf20Sopenharmony_ci	.flags	= IORESOURCE_BUSY | IORESOURCE_MEM,
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic struct resource sysram_resources[MAX_PHYSMEM_RANGES] __ro_after_init;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/* The following array is initialized from the firmware specific
668c2ecf20Sopenharmony_ci * information retrieved in kernel/inventory.c.
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ciphysmem_range_t pmem_ranges[MAX_PHYSMEM_RANGES] __initdata;
708c2ecf20Sopenharmony_ciint npmem_ranges __initdata;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
738c2ecf20Sopenharmony_ci#define MAX_MEM         (1UL << MAX_PHYSMEM_BITS)
748c2ecf20Sopenharmony_ci#else /* !CONFIG_64BIT */
758c2ecf20Sopenharmony_ci#define MAX_MEM         (3584U*1024U*1024U)
768c2ecf20Sopenharmony_ci#endif /* !CONFIG_64BIT */
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic unsigned long mem_limit __read_mostly = MAX_MEM;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic void __init mem_limit_func(void)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	char *cp, *end;
838c2ecf20Sopenharmony_ci	unsigned long limit;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	/* We need this before __setup() functions are called */
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	limit = MAX_MEM;
888c2ecf20Sopenharmony_ci	for (cp = boot_command_line; *cp; ) {
898c2ecf20Sopenharmony_ci		if (memcmp(cp, "mem=", 4) == 0) {
908c2ecf20Sopenharmony_ci			cp += 4;
918c2ecf20Sopenharmony_ci			limit = memparse(cp, &end);
928c2ecf20Sopenharmony_ci			if (end != cp)
938c2ecf20Sopenharmony_ci				break;
948c2ecf20Sopenharmony_ci			cp = end;
958c2ecf20Sopenharmony_ci		} else {
968c2ecf20Sopenharmony_ci			while (*cp != ' ' && *cp)
978c2ecf20Sopenharmony_ci				++cp;
988c2ecf20Sopenharmony_ci			while (*cp == ' ')
998c2ecf20Sopenharmony_ci				++cp;
1008c2ecf20Sopenharmony_ci		}
1018c2ecf20Sopenharmony_ci	}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	if (limit < mem_limit)
1048c2ecf20Sopenharmony_ci		mem_limit = limit;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci#define MAX_GAP (0x40000000UL >> PAGE_SHIFT)
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic void __init setup_bootmem(void)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	unsigned long mem_max;
1128c2ecf20Sopenharmony_ci#ifndef CONFIG_SPARSEMEM
1138c2ecf20Sopenharmony_ci	physmem_range_t pmem_holes[MAX_PHYSMEM_RANGES - 1];
1148c2ecf20Sopenharmony_ci	int npmem_holes;
1158c2ecf20Sopenharmony_ci#endif
1168c2ecf20Sopenharmony_ci	int i, sysram_resource_count;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	disable_sr_hashing(); /* Turn off space register hashing */
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/*
1218c2ecf20Sopenharmony_ci	 * Sort the ranges. Since the number of ranges is typically
1228c2ecf20Sopenharmony_ci	 * small, and performance is not an issue here, just do
1238c2ecf20Sopenharmony_ci	 * a simple insertion sort.
1248c2ecf20Sopenharmony_ci	 */
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	for (i = 1; i < npmem_ranges; i++) {
1278c2ecf20Sopenharmony_ci		int j;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci		for (j = i; j > 0; j--) {
1308c2ecf20Sopenharmony_ci			physmem_range_t tmp;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci			if (pmem_ranges[j-1].start_pfn <
1338c2ecf20Sopenharmony_ci			    pmem_ranges[j].start_pfn) {
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci				break;
1368c2ecf20Sopenharmony_ci			}
1378c2ecf20Sopenharmony_ci			tmp = pmem_ranges[j-1];
1388c2ecf20Sopenharmony_ci			pmem_ranges[j-1] = pmem_ranges[j];
1398c2ecf20Sopenharmony_ci			pmem_ranges[j] = tmp;
1408c2ecf20Sopenharmony_ci		}
1418c2ecf20Sopenharmony_ci	}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci#ifndef CONFIG_SPARSEMEM
1448c2ecf20Sopenharmony_ci	/*
1458c2ecf20Sopenharmony_ci	 * Throw out ranges that are too far apart (controlled by
1468c2ecf20Sopenharmony_ci	 * MAX_GAP).
1478c2ecf20Sopenharmony_ci	 */
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	for (i = 1; i < npmem_ranges; i++) {
1508c2ecf20Sopenharmony_ci		if (pmem_ranges[i].start_pfn -
1518c2ecf20Sopenharmony_ci			(pmem_ranges[i-1].start_pfn +
1528c2ecf20Sopenharmony_ci			 pmem_ranges[i-1].pages) > MAX_GAP) {
1538c2ecf20Sopenharmony_ci			npmem_ranges = i;
1548c2ecf20Sopenharmony_ci			printk("Large gap in memory detected (%ld pages). "
1558c2ecf20Sopenharmony_ci			       "Consider turning on CONFIG_SPARSEMEM\n",
1568c2ecf20Sopenharmony_ci			       pmem_ranges[i].start_pfn -
1578c2ecf20Sopenharmony_ci			       (pmem_ranges[i-1].start_pfn +
1588c2ecf20Sopenharmony_ci			        pmem_ranges[i-1].pages));
1598c2ecf20Sopenharmony_ci			break;
1608c2ecf20Sopenharmony_ci		}
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci#endif
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	/* Print the memory ranges */
1658c2ecf20Sopenharmony_ci	pr_info("Memory Ranges:\n");
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	for (i = 0; i < npmem_ranges; i++) {
1688c2ecf20Sopenharmony_ci		struct resource *res = &sysram_resources[i];
1698c2ecf20Sopenharmony_ci		unsigned long start;
1708c2ecf20Sopenharmony_ci		unsigned long size;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci		size = (pmem_ranges[i].pages << PAGE_SHIFT);
1738c2ecf20Sopenharmony_ci		start = (pmem_ranges[i].start_pfn << PAGE_SHIFT);
1748c2ecf20Sopenharmony_ci		pr_info("%2d) Start 0x%016lx End 0x%016lx Size %6ld MB\n",
1758c2ecf20Sopenharmony_ci			i, start, start + (size - 1), size >> 20);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci		/* request memory resource */
1788c2ecf20Sopenharmony_ci		res->name = "System RAM";
1798c2ecf20Sopenharmony_ci		res->start = start;
1808c2ecf20Sopenharmony_ci		res->end = start + size - 1;
1818c2ecf20Sopenharmony_ci		res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
1828c2ecf20Sopenharmony_ci		request_resource(&iomem_resource, res);
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	sysram_resource_count = npmem_ranges;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	/*
1888c2ecf20Sopenharmony_ci	 * For 32 bit kernels we limit the amount of memory we can
1898c2ecf20Sopenharmony_ci	 * support, in order to preserve enough kernel address space
1908c2ecf20Sopenharmony_ci	 * for other purposes. For 64 bit kernels we don't normally
1918c2ecf20Sopenharmony_ci	 * limit the memory, but this mechanism can be used to
1928c2ecf20Sopenharmony_ci	 * artificially limit the amount of memory (and it is written
1938c2ecf20Sopenharmony_ci	 * to work with multiple memory ranges).
1948c2ecf20Sopenharmony_ci	 */
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	mem_limit_func();       /* check for "mem=" argument */
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	mem_max = 0;
1998c2ecf20Sopenharmony_ci	for (i = 0; i < npmem_ranges; i++) {
2008c2ecf20Sopenharmony_ci		unsigned long rsize;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci		rsize = pmem_ranges[i].pages << PAGE_SHIFT;
2038c2ecf20Sopenharmony_ci		if ((mem_max + rsize) > mem_limit) {
2048c2ecf20Sopenharmony_ci			printk(KERN_WARNING "Memory truncated to %ld MB\n", mem_limit >> 20);
2058c2ecf20Sopenharmony_ci			if (mem_max == mem_limit)
2068c2ecf20Sopenharmony_ci				npmem_ranges = i;
2078c2ecf20Sopenharmony_ci			else {
2088c2ecf20Sopenharmony_ci				pmem_ranges[i].pages =   (mem_limit >> PAGE_SHIFT)
2098c2ecf20Sopenharmony_ci						       - (mem_max >> PAGE_SHIFT);
2108c2ecf20Sopenharmony_ci				npmem_ranges = i + 1;
2118c2ecf20Sopenharmony_ci				mem_max = mem_limit;
2128c2ecf20Sopenharmony_ci			}
2138c2ecf20Sopenharmony_ci			break;
2148c2ecf20Sopenharmony_ci		}
2158c2ecf20Sopenharmony_ci		mem_max += rsize;
2168c2ecf20Sopenharmony_ci	}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	printk(KERN_INFO "Total Memory: %ld MB\n",mem_max >> 20);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci#ifndef CONFIG_SPARSEMEM
2218c2ecf20Sopenharmony_ci	/* Merge the ranges, keeping track of the holes */
2228c2ecf20Sopenharmony_ci	{
2238c2ecf20Sopenharmony_ci		unsigned long end_pfn;
2248c2ecf20Sopenharmony_ci		unsigned long hole_pages;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci		npmem_holes = 0;
2278c2ecf20Sopenharmony_ci		end_pfn = pmem_ranges[0].start_pfn + pmem_ranges[0].pages;
2288c2ecf20Sopenharmony_ci		for (i = 1; i < npmem_ranges; i++) {
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci			hole_pages = pmem_ranges[i].start_pfn - end_pfn;
2318c2ecf20Sopenharmony_ci			if (hole_pages) {
2328c2ecf20Sopenharmony_ci				pmem_holes[npmem_holes].start_pfn = end_pfn;
2338c2ecf20Sopenharmony_ci				pmem_holes[npmem_holes++].pages = hole_pages;
2348c2ecf20Sopenharmony_ci				end_pfn += hole_pages;
2358c2ecf20Sopenharmony_ci			}
2368c2ecf20Sopenharmony_ci			end_pfn += pmem_ranges[i].pages;
2378c2ecf20Sopenharmony_ci		}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci		pmem_ranges[0].pages = end_pfn - pmem_ranges[0].start_pfn;
2408c2ecf20Sopenharmony_ci		npmem_ranges = 1;
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci#endif
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	/*
2458c2ecf20Sopenharmony_ci	 * Initialize and free the full range of memory in each range.
2468c2ecf20Sopenharmony_ci	 */
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	max_pfn = 0;
2498c2ecf20Sopenharmony_ci	for (i = 0; i < npmem_ranges; i++) {
2508c2ecf20Sopenharmony_ci		unsigned long start_pfn;
2518c2ecf20Sopenharmony_ci		unsigned long npages;
2528c2ecf20Sopenharmony_ci		unsigned long start;
2538c2ecf20Sopenharmony_ci		unsigned long size;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci		start_pfn = pmem_ranges[i].start_pfn;
2568c2ecf20Sopenharmony_ci		npages = pmem_ranges[i].pages;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci		start = start_pfn << PAGE_SHIFT;
2598c2ecf20Sopenharmony_ci		size = npages << PAGE_SHIFT;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci		/* add system RAM memblock */
2628c2ecf20Sopenharmony_ci		memblock_add(start, size);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci		if ((start_pfn + npages) > max_pfn)
2658c2ecf20Sopenharmony_ci			max_pfn = start_pfn + npages;
2668c2ecf20Sopenharmony_ci	}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	/*
2698c2ecf20Sopenharmony_ci	 * We can't use memblock top-down allocations because we only
2708c2ecf20Sopenharmony_ci	 * created the initial mapping up to KERNEL_INITIAL_SIZE in
2718c2ecf20Sopenharmony_ci	 * the assembly bootup code.
2728c2ecf20Sopenharmony_ci	 */
2738c2ecf20Sopenharmony_ci	memblock_set_bottom_up(true);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	/* IOMMU is always used to access "high mem" on those boxes
2768c2ecf20Sopenharmony_ci	 * that can support enough mem that a PCI device couldn't
2778c2ecf20Sopenharmony_ci	 * directly DMA to any physical addresses.
2788c2ecf20Sopenharmony_ci	 * ISA DMA support will need to revisit this.
2798c2ecf20Sopenharmony_ci	 */
2808c2ecf20Sopenharmony_ci	max_low_pfn = max_pfn;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	/* reserve PAGE0 pdc memory, kernel text/data/bss & bootmap */
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci#define PDC_CONSOLE_IO_IODC_SIZE 32768
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	memblock_reserve(0UL, (unsigned long)(PAGE0->mem_free +
2878c2ecf20Sopenharmony_ci				PDC_CONSOLE_IO_IODC_SIZE));
2888c2ecf20Sopenharmony_ci	memblock_reserve(__pa(KERNEL_BINARY_TEXT_START),
2898c2ecf20Sopenharmony_ci			(unsigned long)(_end - KERNEL_BINARY_TEXT_START));
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci#ifndef CONFIG_SPARSEMEM
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	/* reserve the holes */
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	for (i = 0; i < npmem_holes; i++) {
2968c2ecf20Sopenharmony_ci		memblock_reserve((pmem_holes[i].start_pfn << PAGE_SHIFT),
2978c2ecf20Sopenharmony_ci				(pmem_holes[i].pages << PAGE_SHIFT));
2988c2ecf20Sopenharmony_ci	}
2998c2ecf20Sopenharmony_ci#endif
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_INITRD
3028c2ecf20Sopenharmony_ci	if (initrd_start) {
3038c2ecf20Sopenharmony_ci		printk(KERN_INFO "initrd: %08lx-%08lx\n", initrd_start, initrd_end);
3048c2ecf20Sopenharmony_ci		if (__pa(initrd_start) < mem_max) {
3058c2ecf20Sopenharmony_ci			unsigned long initrd_reserve;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci			if (__pa(initrd_end) > mem_max) {
3088c2ecf20Sopenharmony_ci				initrd_reserve = mem_max - __pa(initrd_start);
3098c2ecf20Sopenharmony_ci			} else {
3108c2ecf20Sopenharmony_ci				initrd_reserve = initrd_end - initrd_start;
3118c2ecf20Sopenharmony_ci			}
3128c2ecf20Sopenharmony_ci			initrd_below_start_ok = 1;
3138c2ecf20Sopenharmony_ci			printk(KERN_INFO "initrd: reserving %08lx-%08lx (mem_max %08lx)\n", __pa(initrd_start), __pa(initrd_start) + initrd_reserve, mem_max);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci			memblock_reserve(__pa(initrd_start), initrd_reserve);
3168c2ecf20Sopenharmony_ci		}
3178c2ecf20Sopenharmony_ci	}
3188c2ecf20Sopenharmony_ci#endif
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	data_resource.start =  virt_to_phys(&data_start);
3218c2ecf20Sopenharmony_ci	data_resource.end = virt_to_phys(_end) - 1;
3228c2ecf20Sopenharmony_ci	code_resource.start = virt_to_phys(_text);
3238c2ecf20Sopenharmony_ci	code_resource.end = virt_to_phys(&data_start)-1;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	/* We don't know which region the kernel will be in, so try
3268c2ecf20Sopenharmony_ci	 * all of them.
3278c2ecf20Sopenharmony_ci	 */
3288c2ecf20Sopenharmony_ci	for (i = 0; i < sysram_resource_count; i++) {
3298c2ecf20Sopenharmony_ci		struct resource *res = &sysram_resources[i];
3308c2ecf20Sopenharmony_ci		request_resource(res, &code_resource);
3318c2ecf20Sopenharmony_ci		request_resource(res, &data_resource);
3328c2ecf20Sopenharmony_ci	}
3338c2ecf20Sopenharmony_ci	request_resource(&sysram_resources[0], &pdcdata_resource);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	/* Initialize Page Deallocation Table (PDT) and check for bad memory. */
3368c2ecf20Sopenharmony_ci	pdc_pdt_init();
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	memblock_allow_resize();
3398c2ecf20Sopenharmony_ci	memblock_dump_all();
3408c2ecf20Sopenharmony_ci}
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_cistatic bool kernel_set_to_readonly;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_cistatic void __ref map_pages(unsigned long start_vaddr,
3458c2ecf20Sopenharmony_ci			    unsigned long start_paddr, unsigned long size,
3468c2ecf20Sopenharmony_ci			    pgprot_t pgprot, int force)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	pmd_t *pmd;
3498c2ecf20Sopenharmony_ci	pte_t *pg_table;
3508c2ecf20Sopenharmony_ci	unsigned long end_paddr;
3518c2ecf20Sopenharmony_ci	unsigned long start_pmd;
3528c2ecf20Sopenharmony_ci	unsigned long start_pte;
3538c2ecf20Sopenharmony_ci	unsigned long tmp1;
3548c2ecf20Sopenharmony_ci	unsigned long tmp2;
3558c2ecf20Sopenharmony_ci	unsigned long address;
3568c2ecf20Sopenharmony_ci	unsigned long vaddr;
3578c2ecf20Sopenharmony_ci	unsigned long ro_start;
3588c2ecf20Sopenharmony_ci	unsigned long ro_end;
3598c2ecf20Sopenharmony_ci	unsigned long kernel_start, kernel_end;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	ro_start = __pa((unsigned long)_text);
3628c2ecf20Sopenharmony_ci	ro_end   = __pa((unsigned long)&data_start);
3638c2ecf20Sopenharmony_ci	kernel_start = __pa((unsigned long)&__init_begin);
3648c2ecf20Sopenharmony_ci	kernel_end  = __pa((unsigned long)&_end);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	end_paddr = start_paddr + size;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	/* for 2-level configuration PTRS_PER_PMD is 0 so start_pmd will be 0 */
3698c2ecf20Sopenharmony_ci	start_pmd = ((start_vaddr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
3708c2ecf20Sopenharmony_ci	start_pte = ((start_vaddr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	address = start_paddr;
3738c2ecf20Sopenharmony_ci	vaddr = start_vaddr;
3748c2ecf20Sopenharmony_ci	while (address < end_paddr) {
3758c2ecf20Sopenharmony_ci		pgd_t *pgd = pgd_offset_k(vaddr);
3768c2ecf20Sopenharmony_ci		p4d_t *p4d = p4d_offset(pgd, vaddr);
3778c2ecf20Sopenharmony_ci		pud_t *pud = pud_offset(p4d, vaddr);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci#if CONFIG_PGTABLE_LEVELS == 3
3808c2ecf20Sopenharmony_ci		if (pud_none(*pud)) {
3818c2ecf20Sopenharmony_ci			pmd = memblock_alloc(PAGE_SIZE << PMD_ORDER,
3828c2ecf20Sopenharmony_ci					     PAGE_SIZE << PMD_ORDER);
3838c2ecf20Sopenharmony_ci			if (!pmd)
3848c2ecf20Sopenharmony_ci				panic("pmd allocation failed.\n");
3858c2ecf20Sopenharmony_ci			pud_populate(NULL, pud, pmd);
3868c2ecf20Sopenharmony_ci		}
3878c2ecf20Sopenharmony_ci#endif
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci		pmd = pmd_offset(pud, vaddr);
3908c2ecf20Sopenharmony_ci		for (tmp1 = start_pmd; tmp1 < PTRS_PER_PMD; tmp1++, pmd++) {
3918c2ecf20Sopenharmony_ci			if (pmd_none(*pmd)) {
3928c2ecf20Sopenharmony_ci				pg_table = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
3938c2ecf20Sopenharmony_ci				if (!pg_table)
3948c2ecf20Sopenharmony_ci					panic("page table allocation failed\n");
3958c2ecf20Sopenharmony_ci				pmd_populate_kernel(NULL, pmd, pg_table);
3968c2ecf20Sopenharmony_ci			}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci			pg_table = pte_offset_kernel(pmd, vaddr);
3998c2ecf20Sopenharmony_ci			for (tmp2 = start_pte; tmp2 < PTRS_PER_PTE; tmp2++, pg_table++) {
4008c2ecf20Sopenharmony_ci				pte_t pte;
4018c2ecf20Sopenharmony_ci				pgprot_t prot;
4028c2ecf20Sopenharmony_ci				bool huge = false;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci				if (force) {
4058c2ecf20Sopenharmony_ci					prot = pgprot;
4068c2ecf20Sopenharmony_ci				} else if (address < kernel_start || address >= kernel_end) {
4078c2ecf20Sopenharmony_ci					/* outside kernel memory */
4088c2ecf20Sopenharmony_ci					prot = PAGE_KERNEL;
4098c2ecf20Sopenharmony_ci				} else if (!kernel_set_to_readonly) {
4108c2ecf20Sopenharmony_ci					/* still initializing, allow writing to RO memory */
4118c2ecf20Sopenharmony_ci					prot = PAGE_KERNEL_RWX;
4128c2ecf20Sopenharmony_ci					huge = true;
4138c2ecf20Sopenharmony_ci				} else if (address >= ro_start) {
4148c2ecf20Sopenharmony_ci					/* Code (ro) and Data areas */
4158c2ecf20Sopenharmony_ci					prot = (address < ro_end) ?
4168c2ecf20Sopenharmony_ci						PAGE_KERNEL_EXEC : PAGE_KERNEL;
4178c2ecf20Sopenharmony_ci					huge = true;
4188c2ecf20Sopenharmony_ci				} else {
4198c2ecf20Sopenharmony_ci					prot = PAGE_KERNEL;
4208c2ecf20Sopenharmony_ci				}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci				pte = __mk_pte(address, prot);
4238c2ecf20Sopenharmony_ci				if (huge)
4248c2ecf20Sopenharmony_ci					pte = pte_mkhuge(pte);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci				if (address >= end_paddr)
4278c2ecf20Sopenharmony_ci					break;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci				set_pte(pg_table, pte);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci				address += PAGE_SIZE;
4328c2ecf20Sopenharmony_ci				vaddr += PAGE_SIZE;
4338c2ecf20Sopenharmony_ci			}
4348c2ecf20Sopenharmony_ci			start_pte = 0;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci			if (address >= end_paddr)
4378c2ecf20Sopenharmony_ci			    break;
4388c2ecf20Sopenharmony_ci		}
4398c2ecf20Sopenharmony_ci		start_pmd = 0;
4408c2ecf20Sopenharmony_ci	}
4418c2ecf20Sopenharmony_ci}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_civoid __init set_kernel_text_rw(int enable_read_write)
4448c2ecf20Sopenharmony_ci{
4458c2ecf20Sopenharmony_ci	unsigned long start = (unsigned long) __init_begin;
4468c2ecf20Sopenharmony_ci	unsigned long end   = (unsigned long) &data_start;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	map_pages(start, __pa(start), end-start,
4498c2ecf20Sopenharmony_ci		PAGE_KERNEL_RWX, enable_read_write ? 1:0);
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	/* force the kernel to see the new page table entries */
4528c2ecf20Sopenharmony_ci	flush_cache_all();
4538c2ecf20Sopenharmony_ci	flush_tlb_all();
4548c2ecf20Sopenharmony_ci}
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_civoid free_initmem(void)
4578c2ecf20Sopenharmony_ci{
4588c2ecf20Sopenharmony_ci	unsigned long init_begin = (unsigned long)__init_begin;
4598c2ecf20Sopenharmony_ci	unsigned long init_end = (unsigned long)__init_end;
4608c2ecf20Sopenharmony_ci	unsigned long kernel_end  = (unsigned long)&_end;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	/* Remap kernel text and data, but do not touch init section yet. */
4638c2ecf20Sopenharmony_ci	kernel_set_to_readonly = true;
4648c2ecf20Sopenharmony_ci	map_pages(init_end, __pa(init_end), kernel_end - init_end,
4658c2ecf20Sopenharmony_ci		  PAGE_KERNEL, 0);
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	/* The init text pages are marked R-X.  We have to
4688c2ecf20Sopenharmony_ci	 * flush the icache and mark them RW-
4698c2ecf20Sopenharmony_ci	 *
4708c2ecf20Sopenharmony_ci	 * Do a dummy remap of the data section first (the data
4718c2ecf20Sopenharmony_ci	 * section is already PAGE_KERNEL) to pull in the TLB entries
4728c2ecf20Sopenharmony_ci	 * for map_kernel */
4738c2ecf20Sopenharmony_ci	map_pages(init_begin, __pa(init_begin), init_end - init_begin,
4748c2ecf20Sopenharmony_ci		  PAGE_KERNEL_RWX, 1);
4758c2ecf20Sopenharmony_ci	/* now remap at PAGE_KERNEL since the TLB is pre-primed to execute
4768c2ecf20Sopenharmony_ci	 * map_pages */
4778c2ecf20Sopenharmony_ci	map_pages(init_begin, __pa(init_begin), init_end - init_begin,
4788c2ecf20Sopenharmony_ci		  PAGE_KERNEL, 1);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	/* force the kernel to see the new TLB entries */
4818c2ecf20Sopenharmony_ci	__flush_tlb_range(0, init_begin, kernel_end);
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	/* finally dump all the instructions which were cached, since the
4848c2ecf20Sopenharmony_ci	 * pages are no-longer executable */
4858c2ecf20Sopenharmony_ci	flush_icache_range(init_begin, init_end);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	free_initmem_default(POISON_FREE_INITMEM);
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	/* set up a new led state on systems shipped LED State panel */
4908c2ecf20Sopenharmony_ci	pdc_chassis_send_status(PDC_CHASSIS_DIRECT_BCOMPLETE);
4918c2ecf20Sopenharmony_ci}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci#ifdef CONFIG_STRICT_KERNEL_RWX
4958c2ecf20Sopenharmony_civoid mark_rodata_ro(void)
4968c2ecf20Sopenharmony_ci{
4978c2ecf20Sopenharmony_ci	/* rodata memory was already mapped with KERNEL_RO access rights by
4988c2ecf20Sopenharmony_ci           pagetable_init() and map_pages(). No need to do additional stuff here */
4998c2ecf20Sopenharmony_ci	unsigned long roai_size = __end_ro_after_init - __start_ro_after_init;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	pr_info("Write protected read-only-after-init data: %luk\n", roai_size >> 10);
5028c2ecf20Sopenharmony_ci}
5038c2ecf20Sopenharmony_ci#endif
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci/*
5078c2ecf20Sopenharmony_ci * Just an arbitrary offset to serve as a "hole" between mapping areas
5088c2ecf20Sopenharmony_ci * (between top of physical memory and a potential pcxl dma mapping
5098c2ecf20Sopenharmony_ci * area, and below the vmalloc mapping area).
5108c2ecf20Sopenharmony_ci *
5118c2ecf20Sopenharmony_ci * The current 32K value just means that there will be a 32K "hole"
5128c2ecf20Sopenharmony_ci * between mapping areas. That means that  any out-of-bounds memory
5138c2ecf20Sopenharmony_ci * accesses will hopefully be caught. The vmalloc() routines leaves
5148c2ecf20Sopenharmony_ci * a hole of 4kB between each vmalloced area for the same reason.
5158c2ecf20Sopenharmony_ci */
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci /* Leave room for gateway page expansion */
5188c2ecf20Sopenharmony_ci#if KERNEL_MAP_START < GATEWAY_PAGE_SIZE
5198c2ecf20Sopenharmony_ci#error KERNEL_MAP_START is in gateway reserved region
5208c2ecf20Sopenharmony_ci#endif
5218c2ecf20Sopenharmony_ci#define MAP_START (KERNEL_MAP_START)
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci#define VM_MAP_OFFSET  (32*1024)
5248c2ecf20Sopenharmony_ci#define SET_MAP_OFFSET(x) ((void *)(((unsigned long)(x) + VM_MAP_OFFSET) \
5258c2ecf20Sopenharmony_ci				     & ~(VM_MAP_OFFSET-1)))
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_civoid *parisc_vmalloc_start __ro_after_init;
5288c2ecf20Sopenharmony_ciEXPORT_SYMBOL(parisc_vmalloc_start);
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci#ifdef CONFIG_PA11
5318c2ecf20Sopenharmony_ciunsigned long pcxl_dma_start __ro_after_init;
5328c2ecf20Sopenharmony_ci#endif
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_civoid __init mem_init(void)
5358c2ecf20Sopenharmony_ci{
5368c2ecf20Sopenharmony_ci	/* Do sanity checks on IPC (compat) structures */
5378c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct ipc64_perm) != 48);
5388c2ecf20Sopenharmony_ci#ifndef CONFIG_64BIT
5398c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct semid64_ds) != 80);
5408c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct msqid64_ds) != 104);
5418c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct shmid64_ds) != 104);
5428c2ecf20Sopenharmony_ci#endif
5438c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT
5448c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct compat_ipc64_perm) != sizeof(struct ipc64_perm));
5458c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct compat_semid64_ds) != 80);
5468c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct compat_msqid64_ds) != 104);
5478c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct compat_shmid64_ds) != 104);
5488c2ecf20Sopenharmony_ci#endif
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	/* Do sanity checks on page table constants */
5518c2ecf20Sopenharmony_ci	BUILD_BUG_ON(PTE_ENTRY_SIZE != sizeof(pte_t));
5528c2ecf20Sopenharmony_ci	BUILD_BUG_ON(PMD_ENTRY_SIZE != sizeof(pmd_t));
5538c2ecf20Sopenharmony_ci	BUILD_BUG_ON(PGD_ENTRY_SIZE != sizeof(pgd_t));
5548c2ecf20Sopenharmony_ci	BUILD_BUG_ON(PAGE_SHIFT + BITS_PER_PTE + BITS_PER_PMD + BITS_PER_PGD
5558c2ecf20Sopenharmony_ci			> BITS_PER_LONG);
5568c2ecf20Sopenharmony_ci#if CONFIG_PGTABLE_LEVELS == 3
5578c2ecf20Sopenharmony_ci	BUILD_BUG_ON(PT_INITIAL > PTRS_PER_PMD);
5588c2ecf20Sopenharmony_ci#else
5598c2ecf20Sopenharmony_ci	BUILD_BUG_ON(PT_INITIAL > PTRS_PER_PGD);
5608c2ecf20Sopenharmony_ci#endif
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	high_memory = __va((max_pfn << PAGE_SHIFT));
5638c2ecf20Sopenharmony_ci	set_max_mapnr(max_low_pfn);
5648c2ecf20Sopenharmony_ci	memblock_free_all();
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci#ifdef CONFIG_PA11
5678c2ecf20Sopenharmony_ci	if (boot_cpu_data.cpu_type == pcxl2 || boot_cpu_data.cpu_type == pcxl) {
5688c2ecf20Sopenharmony_ci		pcxl_dma_start = (unsigned long)SET_MAP_OFFSET(MAP_START);
5698c2ecf20Sopenharmony_ci		parisc_vmalloc_start = SET_MAP_OFFSET(pcxl_dma_start
5708c2ecf20Sopenharmony_ci						+ PCXL_DMA_MAP_SIZE);
5718c2ecf20Sopenharmony_ci	} else
5728c2ecf20Sopenharmony_ci#endif
5738c2ecf20Sopenharmony_ci		parisc_vmalloc_start = SET_MAP_OFFSET(MAP_START);
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	mem_init_print_info(NULL);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci#if 0
5788c2ecf20Sopenharmony_ci	/*
5798c2ecf20Sopenharmony_ci	 * Do not expose the virtual kernel memory layout to userspace.
5808c2ecf20Sopenharmony_ci	 * But keep code for debugging purposes.
5818c2ecf20Sopenharmony_ci	 */
5828c2ecf20Sopenharmony_ci	printk("virtual kernel memory layout:\n"
5838c2ecf20Sopenharmony_ci	       "     vmalloc : 0x%px - 0x%px   (%4ld MB)\n"
5848c2ecf20Sopenharmony_ci	       "     fixmap  : 0x%px - 0x%px   (%4ld kB)\n"
5858c2ecf20Sopenharmony_ci	       "     memory  : 0x%px - 0x%px   (%4ld MB)\n"
5868c2ecf20Sopenharmony_ci	       "       .init : 0x%px - 0x%px   (%4ld kB)\n"
5878c2ecf20Sopenharmony_ci	       "       .data : 0x%px - 0x%px   (%4ld kB)\n"
5888c2ecf20Sopenharmony_ci	       "       .text : 0x%px - 0x%px   (%4ld kB)\n",
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	       (void*)VMALLOC_START, (void*)VMALLOC_END,
5918c2ecf20Sopenharmony_ci	       (VMALLOC_END - VMALLOC_START) >> 20,
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	       (void *)FIXMAP_START, (void *)(FIXMAP_START + FIXMAP_SIZE),
5948c2ecf20Sopenharmony_ci	       (unsigned long)(FIXMAP_SIZE / 1024),
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	       __va(0), high_memory,
5978c2ecf20Sopenharmony_ci	       ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	       __init_begin, __init_end,
6008c2ecf20Sopenharmony_ci	       ((unsigned long)__init_end - (unsigned long)__init_begin) >> 10,
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	       _etext, _edata,
6038c2ecf20Sopenharmony_ci	       ((unsigned long)_edata - (unsigned long)_etext) >> 10,
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	       _text, _etext,
6068c2ecf20Sopenharmony_ci	       ((unsigned long)_etext - (unsigned long)_text) >> 10);
6078c2ecf20Sopenharmony_ci#endif
6088c2ecf20Sopenharmony_ci}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ciunsigned long *empty_zero_page __ro_after_init;
6118c2ecf20Sopenharmony_ciEXPORT_SYMBOL(empty_zero_page);
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci/*
6148c2ecf20Sopenharmony_ci * pagetable_init() sets up the page tables
6158c2ecf20Sopenharmony_ci *
6168c2ecf20Sopenharmony_ci * Note that gateway_init() places the Linux gateway page at page 0.
6178c2ecf20Sopenharmony_ci * Since gateway pages cannot be dereferenced this has the desirable
6188c2ecf20Sopenharmony_ci * side effect of trapping those pesky NULL-reference errors in the
6198c2ecf20Sopenharmony_ci * kernel.
6208c2ecf20Sopenharmony_ci */
6218c2ecf20Sopenharmony_cistatic void __init pagetable_init(void)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	int range;
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	/* Map each physical memory range to its kernel vaddr */
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	for (range = 0; range < npmem_ranges; range++) {
6288c2ecf20Sopenharmony_ci		unsigned long start_paddr;
6298c2ecf20Sopenharmony_ci		unsigned long end_paddr;
6308c2ecf20Sopenharmony_ci		unsigned long size;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci		start_paddr = pmem_ranges[range].start_pfn << PAGE_SHIFT;
6338c2ecf20Sopenharmony_ci		size = pmem_ranges[range].pages << PAGE_SHIFT;
6348c2ecf20Sopenharmony_ci		end_paddr = start_paddr + size;
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci		map_pages((unsigned long)__va(start_paddr), start_paddr,
6378c2ecf20Sopenharmony_ci			  size, PAGE_KERNEL, 0);
6388c2ecf20Sopenharmony_ci	}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_INITRD
6418c2ecf20Sopenharmony_ci	if (initrd_end && initrd_end > mem_limit) {
6428c2ecf20Sopenharmony_ci		printk(KERN_INFO "initrd: mapping %08lx-%08lx\n", initrd_start, initrd_end);
6438c2ecf20Sopenharmony_ci		map_pages(initrd_start, __pa(initrd_start),
6448c2ecf20Sopenharmony_ci			  initrd_end - initrd_start, PAGE_KERNEL, 0);
6458c2ecf20Sopenharmony_ci	}
6468c2ecf20Sopenharmony_ci#endif
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
6498c2ecf20Sopenharmony_ci	if (!empty_zero_page)
6508c2ecf20Sopenharmony_ci		panic("zero page allocation failed.\n");
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci}
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_cistatic void __init gateway_init(void)
6558c2ecf20Sopenharmony_ci{
6568c2ecf20Sopenharmony_ci	unsigned long linux_gateway_page_addr;
6578c2ecf20Sopenharmony_ci	/* FIXME: This is 'const' in order to trick the compiler
6588c2ecf20Sopenharmony_ci	   into not treating it as DP-relative data. */
6598c2ecf20Sopenharmony_ci	extern void * const linux_gateway_page;
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	linux_gateway_page_addr = LINUX_GATEWAY_ADDR & PAGE_MASK;
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	/*
6648c2ecf20Sopenharmony_ci	 * Setup Linux Gateway page.
6658c2ecf20Sopenharmony_ci	 *
6668c2ecf20Sopenharmony_ci	 * The Linux gateway page will reside in kernel space (on virtual
6678c2ecf20Sopenharmony_ci	 * page 0), so it doesn't need to be aliased into user space.
6688c2ecf20Sopenharmony_ci	 */
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	map_pages(linux_gateway_page_addr, __pa(&linux_gateway_page),
6718c2ecf20Sopenharmony_ci		  PAGE_SIZE, PAGE_GATEWAY, 1);
6728c2ecf20Sopenharmony_ci}
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_cistatic void __init parisc_bootmem_free(void)
6758c2ecf20Sopenharmony_ci{
6768c2ecf20Sopenharmony_ci	unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0, };
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	max_zone_pfn[0] = memblock_end_of_DRAM();
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	free_area_init(max_zone_pfn);
6818c2ecf20Sopenharmony_ci}
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_civoid __init paging_init(void)
6848c2ecf20Sopenharmony_ci{
6858c2ecf20Sopenharmony_ci	setup_bootmem();
6868c2ecf20Sopenharmony_ci	pagetable_init();
6878c2ecf20Sopenharmony_ci	gateway_init();
6888c2ecf20Sopenharmony_ci	flush_cache_all_local(); /* start with known state */
6898c2ecf20Sopenharmony_ci	flush_tlb_all_local(NULL);
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	sparse_init();
6928c2ecf20Sopenharmony_ci	parisc_bootmem_free();
6938c2ecf20Sopenharmony_ci}
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci#ifdef CONFIG_PA20
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci/*
6988c2ecf20Sopenharmony_ci * Currently, all PA20 chips have 18 bit protection IDs, which is the
6998c2ecf20Sopenharmony_ci * limiting factor (space ids are 32 bits).
7008c2ecf20Sopenharmony_ci */
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci#define NR_SPACE_IDS 262144
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci#else
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci/*
7078c2ecf20Sopenharmony_ci * Currently we have a one-to-one relationship between space IDs and
7088c2ecf20Sopenharmony_ci * protection IDs. Older parisc chips (PCXS, PCXT, PCXL, PCXL2) only
7098c2ecf20Sopenharmony_ci * support 15 bit protection IDs, so that is the limiting factor.
7108c2ecf20Sopenharmony_ci * PCXT' has 18 bit protection IDs, but only 16 bit spaceids, so it's
7118c2ecf20Sopenharmony_ci * probably not worth the effort for a special case here.
7128c2ecf20Sopenharmony_ci */
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci#define NR_SPACE_IDS 32768
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci#endif  /* !CONFIG_PA20 */
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci#define RECYCLE_THRESHOLD (NR_SPACE_IDS / 2)
7198c2ecf20Sopenharmony_ci#define SID_ARRAY_SIZE  (NR_SPACE_IDS / (8 * sizeof(long)))
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_cistatic unsigned long space_id[SID_ARRAY_SIZE] = { 1 }; /* disallow space 0 */
7228c2ecf20Sopenharmony_cistatic unsigned long dirty_space_id[SID_ARRAY_SIZE];
7238c2ecf20Sopenharmony_cistatic unsigned long space_id_index;
7248c2ecf20Sopenharmony_cistatic unsigned long free_space_ids = NR_SPACE_IDS - 1;
7258c2ecf20Sopenharmony_cistatic unsigned long dirty_space_ids = 0;
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(sid_lock);
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ciunsigned long alloc_sid(void)
7308c2ecf20Sopenharmony_ci{
7318c2ecf20Sopenharmony_ci	unsigned long index;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	spin_lock(&sid_lock);
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	if (free_space_ids == 0) {
7368c2ecf20Sopenharmony_ci		if (dirty_space_ids != 0) {
7378c2ecf20Sopenharmony_ci			spin_unlock(&sid_lock);
7388c2ecf20Sopenharmony_ci			flush_tlb_all(); /* flush_tlb_all() calls recycle_sids() */
7398c2ecf20Sopenharmony_ci			spin_lock(&sid_lock);
7408c2ecf20Sopenharmony_ci		}
7418c2ecf20Sopenharmony_ci		BUG_ON(free_space_ids == 0);
7428c2ecf20Sopenharmony_ci	}
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	free_space_ids--;
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	index = find_next_zero_bit(space_id, NR_SPACE_IDS, space_id_index);
7478c2ecf20Sopenharmony_ci	space_id[BIT_WORD(index)] |= BIT_MASK(index);
7488c2ecf20Sopenharmony_ci	space_id_index = index;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	spin_unlock(&sid_lock);
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	return index << SPACEID_SHIFT;
7538c2ecf20Sopenharmony_ci}
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_civoid free_sid(unsigned long spaceid)
7568c2ecf20Sopenharmony_ci{
7578c2ecf20Sopenharmony_ci	unsigned long index = spaceid >> SPACEID_SHIFT;
7588c2ecf20Sopenharmony_ci	unsigned long *dirty_space_offset, mask;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	dirty_space_offset = &dirty_space_id[BIT_WORD(index)];
7618c2ecf20Sopenharmony_ci	mask = BIT_MASK(index);
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	spin_lock(&sid_lock);
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	BUG_ON(*dirty_space_offset & mask); /* attempt to free space id twice */
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	*dirty_space_offset |= mask;
7688c2ecf20Sopenharmony_ci	dirty_space_ids++;
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	spin_unlock(&sid_lock);
7718c2ecf20Sopenharmony_ci}
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
7758c2ecf20Sopenharmony_cistatic void get_dirty_sids(unsigned long *ndirtyptr,unsigned long *dirty_array)
7768c2ecf20Sopenharmony_ci{
7778c2ecf20Sopenharmony_ci	int i;
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	/* NOTE: sid_lock must be held upon entry */
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	*ndirtyptr = dirty_space_ids;
7828c2ecf20Sopenharmony_ci	if (dirty_space_ids != 0) {
7838c2ecf20Sopenharmony_ci	    for (i = 0; i < SID_ARRAY_SIZE; i++) {
7848c2ecf20Sopenharmony_ci		dirty_array[i] = dirty_space_id[i];
7858c2ecf20Sopenharmony_ci		dirty_space_id[i] = 0;
7868c2ecf20Sopenharmony_ci	    }
7878c2ecf20Sopenharmony_ci	    dirty_space_ids = 0;
7888c2ecf20Sopenharmony_ci	}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	return;
7918c2ecf20Sopenharmony_ci}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_cistatic void recycle_sids(unsigned long ndirty,unsigned long *dirty_array)
7948c2ecf20Sopenharmony_ci{
7958c2ecf20Sopenharmony_ci	int i;
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	/* NOTE: sid_lock must be held upon entry */
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	if (ndirty != 0) {
8008c2ecf20Sopenharmony_ci		for (i = 0; i < SID_ARRAY_SIZE; i++) {
8018c2ecf20Sopenharmony_ci			space_id[i] ^= dirty_array[i];
8028c2ecf20Sopenharmony_ci		}
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci		free_space_ids += ndirty;
8058c2ecf20Sopenharmony_ci		space_id_index = 0;
8068c2ecf20Sopenharmony_ci	}
8078c2ecf20Sopenharmony_ci}
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci#else /* CONFIG_SMP */
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_cistatic void recycle_sids(void)
8128c2ecf20Sopenharmony_ci{
8138c2ecf20Sopenharmony_ci	int i;
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	/* NOTE: sid_lock must be held upon entry */
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	if (dirty_space_ids != 0) {
8188c2ecf20Sopenharmony_ci		for (i = 0; i < SID_ARRAY_SIZE; i++) {
8198c2ecf20Sopenharmony_ci			space_id[i] ^= dirty_space_id[i];
8208c2ecf20Sopenharmony_ci			dirty_space_id[i] = 0;
8218c2ecf20Sopenharmony_ci		}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci		free_space_ids += dirty_space_ids;
8248c2ecf20Sopenharmony_ci		dirty_space_ids = 0;
8258c2ecf20Sopenharmony_ci		space_id_index = 0;
8268c2ecf20Sopenharmony_ci	}
8278c2ecf20Sopenharmony_ci}
8288c2ecf20Sopenharmony_ci#endif
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci/*
8318c2ecf20Sopenharmony_ci * flush_tlb_all() calls recycle_sids(), since whenever the entire tlb is
8328c2ecf20Sopenharmony_ci * purged, we can safely reuse the space ids that were released but
8338c2ecf20Sopenharmony_ci * not flushed from the tlb.
8348c2ecf20Sopenharmony_ci */
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_cistatic unsigned long recycle_ndirty;
8398c2ecf20Sopenharmony_cistatic unsigned long recycle_dirty_array[SID_ARRAY_SIZE];
8408c2ecf20Sopenharmony_cistatic unsigned int recycle_inuse;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_civoid flush_tlb_all(void)
8438c2ecf20Sopenharmony_ci{
8448c2ecf20Sopenharmony_ci	int do_recycle;
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	do_recycle = 0;
8478c2ecf20Sopenharmony_ci	spin_lock(&sid_lock);
8488c2ecf20Sopenharmony_ci	__inc_irq_stat(irq_tlb_count);
8498c2ecf20Sopenharmony_ci	if (dirty_space_ids > RECYCLE_THRESHOLD) {
8508c2ecf20Sopenharmony_ci	    BUG_ON(recycle_inuse);  /* FIXME: Use a semaphore/wait queue here */
8518c2ecf20Sopenharmony_ci	    get_dirty_sids(&recycle_ndirty,recycle_dirty_array);
8528c2ecf20Sopenharmony_ci	    recycle_inuse++;
8538c2ecf20Sopenharmony_ci	    do_recycle++;
8548c2ecf20Sopenharmony_ci	}
8558c2ecf20Sopenharmony_ci	spin_unlock(&sid_lock);
8568c2ecf20Sopenharmony_ci	on_each_cpu(flush_tlb_all_local, NULL, 1);
8578c2ecf20Sopenharmony_ci	if (do_recycle) {
8588c2ecf20Sopenharmony_ci	    spin_lock(&sid_lock);
8598c2ecf20Sopenharmony_ci	    recycle_sids(recycle_ndirty,recycle_dirty_array);
8608c2ecf20Sopenharmony_ci	    recycle_inuse = 0;
8618c2ecf20Sopenharmony_ci	    spin_unlock(&sid_lock);
8628c2ecf20Sopenharmony_ci	}
8638c2ecf20Sopenharmony_ci}
8648c2ecf20Sopenharmony_ci#else
8658c2ecf20Sopenharmony_civoid flush_tlb_all(void)
8668c2ecf20Sopenharmony_ci{
8678c2ecf20Sopenharmony_ci	spin_lock(&sid_lock);
8688c2ecf20Sopenharmony_ci	__inc_irq_stat(irq_tlb_count);
8698c2ecf20Sopenharmony_ci	flush_tlb_all_local(NULL);
8708c2ecf20Sopenharmony_ci	recycle_sids();
8718c2ecf20Sopenharmony_ci	spin_unlock(&sid_lock);
8728c2ecf20Sopenharmony_ci}
8738c2ecf20Sopenharmony_ci#endif
874