xref: /linux/arch/parisc/mm/init.c (revision c9fa406f62ec952bc4689b5120a02759ce42a68d)
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>
171da177e4SLinus Torvalds #include <linux/bootmem.h>
184fe9e1d9SHelge Deller #include <linux/memblock.h>
195a0e3ad6STejun Heo #include <linux/gfp.h>
201da177e4SLinus Torvalds #include <linux/delay.h>
211da177e4SLinus Torvalds #include <linux/init.h>
221da177e4SLinus Torvalds #include <linux/initrd.h>
231da177e4SLinus Torvalds #include <linux/swap.h>
241da177e4SLinus Torvalds #include <linux/unistd.h>
251da177e4SLinus Torvalds #include <linux/nodemask.h>	/* for node_online_map */
26ea1754a0SKirill A. Shutemov #include <linux/pagemap.h>	/* for release_pages */
27d0cf62fbSHelge Deller #include <linux/compat.h>
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include <asm/pgalloc.h>
30ce8420bbSHelge Deller #include <asm/pgtable.h>
311da177e4SLinus Torvalds #include <asm/tlb.h>
321da177e4SLinus Torvalds #include <asm/pdc_chassis.h>
331da177e4SLinus Torvalds #include <asm/mmzone.h>
34a581c2a4SHeiko Carstens #include <asm/sections.h>
35d0cf62fbSHelge Deller #include <asm/msgbuf.h>
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds extern int  data_start;
38161bd3bfSHelge Deller extern void parisc_kernel_start(void);	/* Kernel entry point in head.S */
391da177e4SLinus Torvalds 
40f24ffde4SKirill A. Shutemov #if CONFIG_PGTABLE_LEVELS == 3
41c39f52a9SThomas Gleixner /* NOTE: This layout exactly conforms to the hybrid L2/L3 page table layout
42c39f52a9SThomas Gleixner  * with the first pmd adjacent to the pgd and below it. gcc doesn't actually
43c39f52a9SThomas Gleixner  * guarantee that global objects will be laid out in memory in the same order
44c39f52a9SThomas Gleixner  * as the order of declaration, so put these in different sections and use
45c39f52a9SThomas Gleixner  * the linker script to order them. */
46c39f52a9SThomas Gleixner pmd_t pmd0[PTRS_PER_PMD] __attribute__ ((__section__ (".data..vm0.pmd"), aligned(PAGE_SIZE)));
47c39f52a9SThomas Gleixner #endif
48c39f52a9SThomas Gleixner 
49c39f52a9SThomas Gleixner pgd_t swapper_pg_dir[PTRS_PER_PGD] __attribute__ ((__section__ (".data..vm0.pgd"), aligned(PAGE_SIZE)));
50c39f52a9SThomas Gleixner pte_t pg0[PT_INITIAL * PTRS_PER_PTE] __attribute__ ((__section__ (".data..vm0.pte"), aligned(PAGE_SIZE)));
51c39f52a9SThomas Gleixner 
521da177e4SLinus Torvalds #ifdef CONFIG_DISCONTIGMEM
538039de10SHelge Deller struct node_map_data node_data[MAX_NUMNODES] __read_mostly;
5491ea8207SHelge Deller signed char pfnnid_map[PFNNID_MAP_MAX] __read_mostly;
551da177e4SLinus Torvalds #endif
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds static struct resource data_resource = {
581da177e4SLinus Torvalds 	.name	= "Kernel data",
5935d98e93SToshi Kani 	.flags	= IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
601da177e4SLinus Torvalds };
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds static struct resource code_resource = {
631da177e4SLinus Torvalds 	.name	= "Kernel code",
6435d98e93SToshi Kani 	.flags	= IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds static struct resource pdcdata_resource = {
681da177e4SLinus Torvalds 	.name	= "PDC data (Page Zero)",
691da177e4SLinus Torvalds 	.start	= 0,
701da177e4SLinus Torvalds 	.end	= 0x9ff,
711da177e4SLinus Torvalds 	.flags	= IORESOURCE_BUSY | IORESOURCE_MEM,
721da177e4SLinus Torvalds };
731da177e4SLinus Torvalds 
748039de10SHelge Deller static struct resource sysram_resources[MAX_PHYSMEM_RANGES] __read_mostly;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds /* The following array is initialized from the firmware specific
771da177e4SLinus Torvalds  * information retrieved in kernel/inventory.c.
781da177e4SLinus Torvalds  */
791da177e4SLinus Torvalds 
808039de10SHelge Deller physmem_range_t pmem_ranges[MAX_PHYSMEM_RANGES] __read_mostly;
818039de10SHelge Deller int npmem_ranges __read_mostly;
821da177e4SLinus Torvalds 
834fe9e1d9SHelge Deller /*
844fe9e1d9SHelge Deller  * get_memblock() allocates pages via memblock.
854fe9e1d9SHelge Deller  * We can't use memblock_find_in_range(0, KERNEL_INITIAL_SIZE) here since it
864fe9e1d9SHelge Deller  * doesn't allocate from bottom to top which is needed because we only created
874fe9e1d9SHelge Deller  * the initial mapping up to KERNEL_INITIAL_SIZE in the assembly bootup code.
884fe9e1d9SHelge Deller  */
894fe9e1d9SHelge Deller static void * __init get_memblock(unsigned long size)
904fe9e1d9SHelge Deller {
914fe9e1d9SHelge Deller 	static phys_addr_t search_addr __initdata;
924fe9e1d9SHelge Deller 	phys_addr_t phys;
934fe9e1d9SHelge Deller 
944fe9e1d9SHelge Deller 	if (!search_addr)
954fe9e1d9SHelge Deller 		search_addr = PAGE_ALIGN(__pa((unsigned long) &_end));
964fe9e1d9SHelge Deller 	search_addr = ALIGN(search_addr, size);
974fe9e1d9SHelge Deller 	while (!memblock_is_region_memory(search_addr, size) ||
984fe9e1d9SHelge Deller 		memblock_is_region_reserved(search_addr, size)) {
994fe9e1d9SHelge Deller 		search_addr += size;
1004fe9e1d9SHelge Deller 	}
1014fe9e1d9SHelge Deller 	phys = search_addr;
1024fe9e1d9SHelge Deller 
1034fe9e1d9SHelge Deller 	if (phys)
1044fe9e1d9SHelge Deller 		memblock_reserve(phys, size);
1054fe9e1d9SHelge Deller 	else
1064fe9e1d9SHelge Deller 		panic("get_memblock() failed.\n");
1074fe9e1d9SHelge Deller 
108e3b6a028SHelge Deller 	memset(__va(phys), 0, size);
109e3b6a028SHelge Deller 
1104fe9e1d9SHelge Deller 	return __va(phys);
1114fe9e1d9SHelge Deller }
1124fe9e1d9SHelge Deller 
113a8f44e38SHelge Deller #ifdef CONFIG_64BIT
1141da177e4SLinus Torvalds #define MAX_MEM         (~0UL)
115a8f44e38SHelge Deller #else /* !CONFIG_64BIT */
1161da177e4SLinus Torvalds #define MAX_MEM         (3584U*1024U*1024U)
117a8f44e38SHelge Deller #endif /* !CONFIG_64BIT */
1181da177e4SLinus Torvalds 
1198039de10SHelge Deller static unsigned long mem_limit __read_mostly = MAX_MEM;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds static void __init mem_limit_func(void)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	char *cp, *end;
1241da177e4SLinus Torvalds 	unsigned long limit;
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	/* We need this before __setup() functions are called */
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	limit = MAX_MEM;
129668f9931SAlon Bar-Lev 	for (cp = boot_command_line; *cp; ) {
1301da177e4SLinus Torvalds 		if (memcmp(cp, "mem=", 4) == 0) {
1311da177e4SLinus Torvalds 			cp += 4;
1321da177e4SLinus Torvalds 			limit = memparse(cp, &end);
1331da177e4SLinus Torvalds 			if (end != cp)
1341da177e4SLinus Torvalds 				break;
1351da177e4SLinus Torvalds 			cp = end;
1361da177e4SLinus Torvalds 		} else {
1371da177e4SLinus Torvalds 			while (*cp != ' ' && *cp)
1381da177e4SLinus Torvalds 				++cp;
1391da177e4SLinus Torvalds 			while (*cp == ' ')
1401da177e4SLinus Torvalds 				++cp;
1411da177e4SLinus Torvalds 		}
1421da177e4SLinus Torvalds 	}
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	if (limit < mem_limit)
1451da177e4SLinus Torvalds 		mem_limit = limit;
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds #define MAX_GAP (0x40000000UL >> PAGE_SHIFT)
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds static void __init setup_bootmem(void)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	unsigned long mem_max;
1531da177e4SLinus Torvalds #ifndef CONFIG_DISCONTIGMEM
1541da177e4SLinus Torvalds 	physmem_range_t pmem_holes[MAX_PHYSMEM_RANGES - 1];
1551da177e4SLinus Torvalds 	int npmem_holes;
1561da177e4SLinus Torvalds #endif
1571da177e4SLinus Torvalds 	int i, sysram_resource_count;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	disable_sr_hashing(); /* Turn off space register hashing */
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	/*
1621da177e4SLinus Torvalds 	 * Sort the ranges. Since the number of ranges is typically
1631da177e4SLinus Torvalds 	 * small, and performance is not an issue here, just do
1641da177e4SLinus Torvalds 	 * a simple insertion sort.
1651da177e4SLinus Torvalds 	 */
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	for (i = 1; i < npmem_ranges; i++) {
1681da177e4SLinus Torvalds 		int j;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 		for (j = i; j > 0; j--) {
1711da177e4SLinus Torvalds 			unsigned long tmp;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 			if (pmem_ranges[j-1].start_pfn <
1741da177e4SLinus Torvalds 			    pmem_ranges[j].start_pfn) {
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 				break;
1771da177e4SLinus Torvalds 			}
1781da177e4SLinus Torvalds 			tmp = pmem_ranges[j-1].start_pfn;
1791da177e4SLinus Torvalds 			pmem_ranges[j-1].start_pfn = pmem_ranges[j].start_pfn;
1801da177e4SLinus Torvalds 			pmem_ranges[j].start_pfn = tmp;
1811da177e4SLinus Torvalds 			tmp = pmem_ranges[j-1].pages;
1821da177e4SLinus Torvalds 			pmem_ranges[j-1].pages = pmem_ranges[j].pages;
1831da177e4SLinus Torvalds 			pmem_ranges[j].pages = tmp;
1841da177e4SLinus Torvalds 		}
1851da177e4SLinus Torvalds 	}
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds #ifndef CONFIG_DISCONTIGMEM
1881da177e4SLinus Torvalds 	/*
1891da177e4SLinus Torvalds 	 * Throw out ranges that are too far apart (controlled by
1901da177e4SLinus Torvalds 	 * MAX_GAP).
1911da177e4SLinus Torvalds 	 */
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	for (i = 1; i < npmem_ranges; i++) {
1941da177e4SLinus Torvalds 		if (pmem_ranges[i].start_pfn -
1951da177e4SLinus Torvalds 			(pmem_ranges[i-1].start_pfn +
1961da177e4SLinus Torvalds 			 pmem_ranges[i-1].pages) > MAX_GAP) {
1971da177e4SLinus Torvalds 			npmem_ranges = i;
1981da177e4SLinus Torvalds 			printk("Large gap in memory detected (%ld pages). "
1991da177e4SLinus Torvalds 			       "Consider turning on CONFIG_DISCONTIGMEM\n",
2001da177e4SLinus Torvalds 			       pmem_ranges[i].start_pfn -
2011da177e4SLinus Torvalds 			       (pmem_ranges[i-1].start_pfn +
2021da177e4SLinus Torvalds 			        pmem_ranges[i-1].pages));
2031da177e4SLinus Torvalds 			break;
2041da177e4SLinus Torvalds 		}
2051da177e4SLinus Torvalds 	}
2061da177e4SLinus Torvalds #endif
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	/* Print the memory ranges */
2094fe9e1d9SHelge Deller 	pr_info("Memory Ranges:\n");
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	for (i = 0; i < npmem_ranges; i++) {
2124fe9e1d9SHelge Deller 		struct resource *res = &sysram_resources[i];
2131da177e4SLinus Torvalds 		unsigned long start;
2141da177e4SLinus Torvalds 		unsigned long size;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 		size = (pmem_ranges[i].pages << PAGE_SHIFT);
2171da177e4SLinus Torvalds 		start = (pmem_ranges[i].start_pfn << PAGE_SHIFT);
2184fe9e1d9SHelge Deller 		pr_info("%2d) Start 0x%016lx End 0x%016lx Size %6ld MB\n",
2191da177e4SLinus Torvalds 			i, start, start + (size - 1), size >> 20);
2201da177e4SLinus Torvalds 
2214fe9e1d9SHelge Deller 		/* request memory resource */
2221da177e4SLinus Torvalds 		res->name = "System RAM";
2234fe9e1d9SHelge Deller 		res->start = start;
2244fe9e1d9SHelge Deller 		res->end = start + size - 1;
22535d98e93SToshi Kani 		res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
2261da177e4SLinus Torvalds 		request_resource(&iomem_resource, res);
2271da177e4SLinus Torvalds 	}
2281da177e4SLinus Torvalds 
2294fe9e1d9SHelge Deller 	sysram_resource_count = npmem_ranges;
2304fe9e1d9SHelge Deller 
2311da177e4SLinus Torvalds 	/*
2321da177e4SLinus Torvalds 	 * For 32 bit kernels we limit the amount of memory we can
2331da177e4SLinus Torvalds 	 * support, in order to preserve enough kernel address space
2341da177e4SLinus Torvalds 	 * for other purposes. For 64 bit kernels we don't normally
2351da177e4SLinus Torvalds 	 * limit the memory, but this mechanism can be used to
2361da177e4SLinus Torvalds 	 * artificially limit the amount of memory (and it is written
2371da177e4SLinus Torvalds 	 * to work with multiple memory ranges).
2381da177e4SLinus Torvalds 	 */
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	mem_limit_func();       /* check for "mem=" argument */
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	mem_max = 0;
2431da177e4SLinus Torvalds 	for (i = 0; i < npmem_ranges; i++) {
2441da177e4SLinus Torvalds 		unsigned long rsize;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 		rsize = pmem_ranges[i].pages << PAGE_SHIFT;
2471da177e4SLinus Torvalds 		if ((mem_max + rsize) > mem_limit) {
2481da177e4SLinus Torvalds 			printk(KERN_WARNING "Memory truncated to %ld MB\n", mem_limit >> 20);
2491da177e4SLinus Torvalds 			if (mem_max == mem_limit)
2501da177e4SLinus Torvalds 				npmem_ranges = i;
2511da177e4SLinus Torvalds 			else {
2521da177e4SLinus Torvalds 				pmem_ranges[i].pages =   (mem_limit >> PAGE_SHIFT)
2531da177e4SLinus Torvalds 						       - (mem_max >> PAGE_SHIFT);
2541da177e4SLinus Torvalds 				npmem_ranges = i + 1;
2551da177e4SLinus Torvalds 				mem_max = mem_limit;
2561da177e4SLinus Torvalds 			}
2571da177e4SLinus Torvalds 			break;
2581da177e4SLinus Torvalds 		}
2591da177e4SLinus Torvalds 		mem_max += rsize;
2601da177e4SLinus Torvalds 	}
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	printk(KERN_INFO "Total Memory: %ld MB\n",mem_max >> 20);
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds #ifndef CONFIG_DISCONTIGMEM
2651da177e4SLinus Torvalds 	/* Merge the ranges, keeping track of the holes */
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	{
2681da177e4SLinus Torvalds 		unsigned long end_pfn;
2691da177e4SLinus Torvalds 		unsigned long hole_pages;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 		npmem_holes = 0;
2721da177e4SLinus Torvalds 		end_pfn = pmem_ranges[0].start_pfn + pmem_ranges[0].pages;
2731da177e4SLinus Torvalds 		for (i = 1; i < npmem_ranges; i++) {
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 			hole_pages = pmem_ranges[i].start_pfn - end_pfn;
2761da177e4SLinus Torvalds 			if (hole_pages) {
2771da177e4SLinus Torvalds 				pmem_holes[npmem_holes].start_pfn = end_pfn;
2781da177e4SLinus Torvalds 				pmem_holes[npmem_holes++].pages = hole_pages;
2791da177e4SLinus Torvalds 				end_pfn += hole_pages;
2801da177e4SLinus Torvalds 			}
2811da177e4SLinus Torvalds 			end_pfn += pmem_ranges[i].pages;
2821da177e4SLinus Torvalds 		}
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 		pmem_ranges[0].pages = end_pfn - pmem_ranges[0].start_pfn;
2851da177e4SLinus Torvalds 		npmem_ranges = 1;
2861da177e4SLinus Torvalds 	}
2871da177e4SLinus Torvalds #endif
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds #ifdef CONFIG_DISCONTIGMEM
2901da177e4SLinus Torvalds 	for (i = 0; i < MAX_PHYSMEM_RANGES; i++) {
2911da177e4SLinus Torvalds 		memset(NODE_DATA(i), 0, sizeof(pg_data_t));
2921da177e4SLinus Torvalds 	}
2931da177e4SLinus Torvalds 	memset(pfnnid_map, 0xff, sizeof(pfnnid_map));
2941da177e4SLinus Torvalds 
295d9b41e0bSDavid Rientjes 	for (i = 0; i < npmem_ranges; i++) {
296d9b41e0bSDavid Rientjes 		node_set_state(i, N_NORMAL_MEMORY);
2971da177e4SLinus Torvalds 		node_set_online(i);
298d9b41e0bSDavid Rientjes 	}
2991da177e4SLinus Torvalds #endif
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	/*
3021da177e4SLinus Torvalds 	 * Initialize and free the full range of memory in each range.
3031da177e4SLinus Torvalds 	 */
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	max_pfn = 0;
3061da177e4SLinus Torvalds 	for (i = 0; i < npmem_ranges; i++) {
3071da177e4SLinus Torvalds 		unsigned long start_pfn;
3081da177e4SLinus Torvalds 		unsigned long npages;
3094fe9e1d9SHelge Deller 		unsigned long start;
3104fe9e1d9SHelge Deller 		unsigned long size;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 		start_pfn = pmem_ranges[i].start_pfn;
3131da177e4SLinus Torvalds 		npages = pmem_ranges[i].pages;
3141da177e4SLinus Torvalds 
3154fe9e1d9SHelge Deller 		start = start_pfn << PAGE_SHIFT;
3164fe9e1d9SHelge Deller 		size = npages << PAGE_SHIFT;
3174fe9e1d9SHelge Deller 
3184fe9e1d9SHelge Deller 		/* add system RAM memblock */
3194fe9e1d9SHelge Deller 		memblock_add(start, size);
3204fe9e1d9SHelge Deller 
3211da177e4SLinus Torvalds 		if ((start_pfn + npages) > max_pfn)
3221da177e4SLinus Torvalds 			max_pfn = start_pfn + npages;
3231da177e4SLinus Torvalds 	}
3241da177e4SLinus Torvalds 
3255cdb8205SGrant Grundler 	/* IOMMU is always used to access "high mem" on those boxes
3265cdb8205SGrant Grundler 	 * that can support enough mem that a PCI device couldn't
3275cdb8205SGrant Grundler 	 * directly DMA to any physical addresses.
3285cdb8205SGrant Grundler 	 * ISA DMA support will need to revisit this.
3295cdb8205SGrant Grundler 	 */
3305cdb8205SGrant Grundler 	max_low_pfn = max_pfn;
3315cdb8205SGrant Grundler 
3321da177e4SLinus Torvalds 	/* reserve PAGE0 pdc memory, kernel text/data/bss & bootmap */
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds #define PDC_CONSOLE_IO_IODC_SIZE 32768
3351da177e4SLinus Torvalds 
3364fe9e1d9SHelge Deller 	memblock_reserve(0UL, (unsigned long)(PAGE0->mem_free +
3374fe9e1d9SHelge Deller 				PDC_CONSOLE_IO_IODC_SIZE));
3384fe9e1d9SHelge Deller 	memblock_reserve(__pa(KERNEL_BINARY_TEXT_START),
3394fe9e1d9SHelge Deller 			(unsigned long)(_end - KERNEL_BINARY_TEXT_START));
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds #ifndef CONFIG_DISCONTIGMEM
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	/* reserve the holes */
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	for (i = 0; i < npmem_holes; i++) {
3464fe9e1d9SHelge Deller 		memblock_reserve((pmem_holes[i].start_pfn << PAGE_SHIFT),
3474fe9e1d9SHelge Deller 				(pmem_holes[i].pages << PAGE_SHIFT));
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds #endif
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD
3521da177e4SLinus Torvalds 	if (initrd_start) {
3531da177e4SLinus Torvalds 		printk(KERN_INFO "initrd: %08lx-%08lx\n", initrd_start, initrd_end);
3541da177e4SLinus Torvalds 		if (__pa(initrd_start) < mem_max) {
3551da177e4SLinus Torvalds 			unsigned long initrd_reserve;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 			if (__pa(initrd_end) > mem_max) {
3581da177e4SLinus Torvalds 				initrd_reserve = mem_max - __pa(initrd_start);
3591da177e4SLinus Torvalds 			} else {
3601da177e4SLinus Torvalds 				initrd_reserve = initrd_end - initrd_start;
3611da177e4SLinus Torvalds 			}
3621da177e4SLinus Torvalds 			initrd_below_start_ok = 1;
3631da177e4SLinus Torvalds 			printk(KERN_INFO "initrd: reserving %08lx-%08lx (mem_max %08lx)\n", __pa(initrd_start), __pa(initrd_start) + initrd_reserve, mem_max);
3641da177e4SLinus Torvalds 
3654fe9e1d9SHelge Deller 			memblock_reserve(__pa(initrd_start), initrd_reserve);
3661da177e4SLinus Torvalds 		}
3671da177e4SLinus Torvalds 	}
3681da177e4SLinus Torvalds #endif
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	data_resource.start =  virt_to_phys(&data_start);
371c51d476aSKyle McMartin 	data_resource.end = virt_to_phys(_end) - 1;
372c51d476aSKyle McMartin 	code_resource.start = virt_to_phys(_text);
3731da177e4SLinus Torvalds 	code_resource.end = virt_to_phys(&data_start)-1;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	/* We don't know which region the kernel will be in, so try
3761da177e4SLinus Torvalds 	 * all of them.
3771da177e4SLinus Torvalds 	 */
3781da177e4SLinus Torvalds 	for (i = 0; i < sysram_resource_count; i++) {
3791da177e4SLinus Torvalds 		struct resource *res = &sysram_resources[i];
3801da177e4SLinus Torvalds 		request_resource(res, &code_resource);
3811da177e4SLinus Torvalds 		request_resource(res, &data_resource);
3821da177e4SLinus Torvalds 	}
3831da177e4SLinus Torvalds 	request_resource(&sysram_resources[0], &pdcdata_resource);
384c9c2877dSHelge Deller 
385c9c2877dSHelge Deller 	/* Initialize Page Deallocation Table (PDT) and check for bad memory. */
386c9c2877dSHelge Deller 	pdc_pdt_init();
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
389161bd3bfSHelge Deller static int __init parisc_text_address(unsigned long vaddr)
390161bd3bfSHelge Deller {
391161bd3bfSHelge Deller 	static unsigned long head_ptr __initdata;
392161bd3bfSHelge Deller 
393161bd3bfSHelge Deller 	if (!head_ptr)
394161bd3bfSHelge Deller 		head_ptr = PAGE_MASK & (unsigned long)
395161bd3bfSHelge Deller 			dereference_function_descriptor(&parisc_kernel_start);
396161bd3bfSHelge Deller 
397161bd3bfSHelge Deller 	return core_kernel_text(vaddr) || vaddr == head_ptr;
398161bd3bfSHelge Deller }
399161bd3bfSHelge Deller 
400d7dd2ff1SJames Bottomley static void __init map_pages(unsigned long start_vaddr,
401d7dd2ff1SJames Bottomley 			     unsigned long start_paddr, unsigned long size,
402d7dd2ff1SJames Bottomley 			     pgprot_t pgprot, int force)
403d7dd2ff1SJames Bottomley {
404d7dd2ff1SJames Bottomley 	pgd_t *pg_dir;
405d7dd2ff1SJames Bottomley 	pmd_t *pmd;
406d7dd2ff1SJames Bottomley 	pte_t *pg_table;
407d7dd2ff1SJames Bottomley 	unsigned long end_paddr;
408d7dd2ff1SJames Bottomley 	unsigned long start_pmd;
409d7dd2ff1SJames Bottomley 	unsigned long start_pte;
410d7dd2ff1SJames Bottomley 	unsigned long tmp1;
411d7dd2ff1SJames Bottomley 	unsigned long tmp2;
412d7dd2ff1SJames Bottomley 	unsigned long address;
413d7dd2ff1SJames Bottomley 	unsigned long vaddr;
414d7dd2ff1SJames Bottomley 	unsigned long ro_start;
415d7dd2ff1SJames Bottomley 	unsigned long ro_end;
41641b85a11SHelge Deller 	unsigned long kernel_end;
417d7dd2ff1SJames Bottomley 
418d7dd2ff1SJames Bottomley 	ro_start = __pa((unsigned long)_text);
419d7dd2ff1SJames Bottomley 	ro_end   = __pa((unsigned long)&data_start);
42041b85a11SHelge Deller 	kernel_end  = __pa((unsigned long)&_end);
421d7dd2ff1SJames Bottomley 
422d7dd2ff1SJames Bottomley 	end_paddr = start_paddr + size;
423d7dd2ff1SJames Bottomley 
424d7dd2ff1SJames Bottomley 	pg_dir = pgd_offset_k(start_vaddr);
425d7dd2ff1SJames Bottomley 
426d7dd2ff1SJames Bottomley #if PTRS_PER_PMD == 1
427d7dd2ff1SJames Bottomley 	start_pmd = 0;
428d7dd2ff1SJames Bottomley #else
429d7dd2ff1SJames Bottomley 	start_pmd = ((start_vaddr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
430d7dd2ff1SJames Bottomley #endif
431d7dd2ff1SJames Bottomley 	start_pte = ((start_vaddr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
432d7dd2ff1SJames Bottomley 
433d7dd2ff1SJames Bottomley 	address = start_paddr;
434d7dd2ff1SJames Bottomley 	vaddr = start_vaddr;
435d7dd2ff1SJames Bottomley 	while (address < end_paddr) {
436d7dd2ff1SJames Bottomley #if PTRS_PER_PMD == 1
437d7dd2ff1SJames Bottomley 		pmd = (pmd_t *)__pa(pg_dir);
438d7dd2ff1SJames Bottomley #else
439d7dd2ff1SJames Bottomley 		pmd = (pmd_t *)pgd_address(*pg_dir);
440d7dd2ff1SJames Bottomley 
441d7dd2ff1SJames Bottomley 		/*
442d7dd2ff1SJames Bottomley 		 * pmd is physical at this point
443d7dd2ff1SJames Bottomley 		 */
444d7dd2ff1SJames Bottomley 
445d7dd2ff1SJames Bottomley 		if (!pmd) {
4464fe9e1d9SHelge Deller 			pmd = (pmd_t *) get_memblock(PAGE_SIZE << PMD_ORDER);
447d7dd2ff1SJames Bottomley 			pmd = (pmd_t *) __pa(pmd);
448d7dd2ff1SJames Bottomley 		}
449d7dd2ff1SJames Bottomley 
450d7dd2ff1SJames Bottomley 		pgd_populate(NULL, pg_dir, __va(pmd));
451d7dd2ff1SJames Bottomley #endif
452d7dd2ff1SJames Bottomley 		pg_dir++;
453d7dd2ff1SJames Bottomley 
454d7dd2ff1SJames Bottomley 		/* now change pmd to kernel virtual addresses */
455d7dd2ff1SJames Bottomley 
456d7dd2ff1SJames Bottomley 		pmd = (pmd_t *)__va(pmd) + start_pmd;
457d7dd2ff1SJames Bottomley 		for (tmp1 = start_pmd; tmp1 < PTRS_PER_PMD; tmp1++, pmd++) {
458d7dd2ff1SJames Bottomley 
459d7dd2ff1SJames Bottomley 			/*
460d7dd2ff1SJames Bottomley 			 * pg_table is physical at this point
461d7dd2ff1SJames Bottomley 			 */
462d7dd2ff1SJames Bottomley 
463d7dd2ff1SJames Bottomley 			pg_table = (pte_t *)pmd_address(*pmd);
464d7dd2ff1SJames Bottomley 			if (!pg_table) {
4654fe9e1d9SHelge Deller 				pg_table = (pte_t *) get_memblock(PAGE_SIZE);
466d7dd2ff1SJames Bottomley 				pg_table = (pte_t *) __pa(pg_table);
467d7dd2ff1SJames Bottomley 			}
468d7dd2ff1SJames Bottomley 
469d7dd2ff1SJames Bottomley 			pmd_populate_kernel(NULL, pmd, __va(pg_table));
470d7dd2ff1SJames Bottomley 
471d7dd2ff1SJames Bottomley 			/* now change pg_table to kernel virtual addresses */
472d7dd2ff1SJames Bottomley 
473d7dd2ff1SJames Bottomley 			pg_table = (pte_t *) __va(pg_table) + start_pte;
474d7dd2ff1SJames Bottomley 			for (tmp2 = start_pte; tmp2 < PTRS_PER_PTE; tmp2++, pg_table++) {
475d7dd2ff1SJames Bottomley 				pte_t pte;
476d7dd2ff1SJames Bottomley 
477d7dd2ff1SJames Bottomley 				if (force)
478d7dd2ff1SJames Bottomley 					pte =  __mk_pte(address, pgprot);
47941b85a11SHelge Deller 				else if (parisc_text_address(vaddr)) {
480d7dd2ff1SJames Bottomley 					pte = __mk_pte(address, PAGE_KERNEL_EXEC);
48141b85a11SHelge Deller 					if (address >= ro_start && address < kernel_end)
48241b85a11SHelge Deller 						pte = pte_mkhuge(pte);
48341b85a11SHelge Deller 				}
484d7dd2ff1SJames Bottomley 				else
485d7dd2ff1SJames Bottomley #if defined(CONFIG_PARISC_PAGE_SIZE_4KB)
48641b85a11SHelge Deller 				if (address >= ro_start && address < ro_end) {
48741b85a11SHelge Deller 					pte = __mk_pte(address, PAGE_KERNEL_EXEC);
48841b85a11SHelge Deller 					pte = pte_mkhuge(pte);
48941b85a11SHelge Deller 				} else
490d7dd2ff1SJames Bottomley #endif
49141b85a11SHelge Deller 				{
492d7dd2ff1SJames Bottomley 					pte = __mk_pte(address, pgprot);
49341b85a11SHelge Deller 					if (address >= ro_start && address < kernel_end)
49441b85a11SHelge Deller 						pte = pte_mkhuge(pte);
49541b85a11SHelge Deller 				}
496d7dd2ff1SJames Bottomley 
4973c229b3fSHelge Deller 				if (address >= end_paddr)
498d7dd2ff1SJames Bottomley 					break;
499d7dd2ff1SJames Bottomley 
500d7dd2ff1SJames Bottomley 				set_pte(pg_table, pte);
501d7dd2ff1SJames Bottomley 
502d7dd2ff1SJames Bottomley 				address += PAGE_SIZE;
503d7dd2ff1SJames Bottomley 				vaddr += PAGE_SIZE;
504d7dd2ff1SJames Bottomley 			}
505d7dd2ff1SJames Bottomley 			start_pte = 0;
506d7dd2ff1SJames Bottomley 
507d7dd2ff1SJames Bottomley 			if (address >= end_paddr)
508d7dd2ff1SJames Bottomley 			    break;
509d7dd2ff1SJames Bottomley 		}
510d7dd2ff1SJames Bottomley 		start_pmd = 0;
511d7dd2ff1SJames Bottomley 	}
512d7dd2ff1SJames Bottomley }
513d7dd2ff1SJames Bottomley 
5143847dab7SHelge Deller void __init set_kernel_text_rw(int enable_read_write)
5153847dab7SHelge Deller {
516*c9fa406fSJohn David Anglin 	unsigned long start = (unsigned long)__init_begin;
5173847dab7SHelge Deller 	unsigned long end   = (unsigned long)_etext;
5183847dab7SHelge Deller 
5193847dab7SHelge Deller 	map_pages(start, __pa(start), end-start,
5203847dab7SHelge Deller 		PAGE_KERNEL_RWX, enable_read_write ? 1:0);
5213847dab7SHelge Deller 
522*c9fa406fSJohn David Anglin 	/* force the kernel to see the new page table entries */
523*c9fa406fSJohn David Anglin 	flush_cache_all();
524*c9fa406fSJohn David Anglin 	flush_tlb_all();
5253847dab7SHelge Deller }
5263847dab7SHelge Deller 
5278d73b180SHelge Deller void __ref free_initmem(void)
5281da177e4SLinus Torvalds {
5294fb11781SKyle McMartin 	unsigned long init_begin = (unsigned long)__init_begin;
5304fb11781SKyle McMartin 	unsigned long init_end = (unsigned long)__init_end;
5311da177e4SLinus Torvalds 
532d7dd2ff1SJames Bottomley 	/* The init text pages are marked R-X.  We have to
533d7dd2ff1SJames Bottomley 	 * flush the icache and mark them RW-
534d7dd2ff1SJames Bottomley 	 *
535d7dd2ff1SJames Bottomley 	 * This is tricky, because map_pages is in the init section.
536d7dd2ff1SJames Bottomley 	 * Do a dummy remap of the data section first (the data
537d7dd2ff1SJames Bottomley 	 * section is already PAGE_KERNEL) to pull in the TLB entries
538d7dd2ff1SJames Bottomley 	 * for map_kernel */
539d7dd2ff1SJames Bottomley 	map_pages(init_begin, __pa(init_begin), init_end - init_begin,
540d7dd2ff1SJames Bottomley 		  PAGE_KERNEL_RWX, 1);
541d7dd2ff1SJames Bottomley 	/* now remap at PAGE_KERNEL since the TLB is pre-primed to execute
542d7dd2ff1SJames Bottomley 	 * map_pages */
543d7dd2ff1SJames Bottomley 	map_pages(init_begin, __pa(init_begin), init_end - init_begin,
544d7dd2ff1SJames Bottomley 		  PAGE_KERNEL, 1);
545d7dd2ff1SJames Bottomley 
546d7dd2ff1SJames Bottomley 	/* force the kernel to see the new TLB entries */
547d7dd2ff1SJames Bottomley 	__flush_tlb_range(0, init_begin, init_end);
54841b85a11SHelge Deller 
549d7dd2ff1SJames Bottomley 	/* finally dump all the instructions which were cached, since the
550d7dd2ff1SJames Bottomley 	 * pages are no-longer executable */
5514fb11781SKyle McMartin 	flush_icache_range(init_begin, init_end);
5521da177e4SLinus Torvalds 
55341b85a11SHelge Deller 	free_initmem_default(POISON_FREE_INITMEM);
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	/* set up a new led state on systems shipped LED State panel */
5561da177e4SLinus Torvalds 	pdc_chassis_send_status(PDC_CHASSIS_DIRECT_BCOMPLETE);
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
5591bcdd854SHelge Deller 
5600f5bf6d0SLaura Abbott #ifdef CONFIG_STRICT_KERNEL_RWX
5611bcdd854SHelge Deller void mark_rodata_ro(void)
5621bcdd854SHelge Deller {
5631bcdd854SHelge Deller 	/* rodata memory was already mapped with KERNEL_RO access rights by
5641bcdd854SHelge Deller            pagetable_init() and map_pages(). No need to do additional stuff here */
5651bcdd854SHelge Deller 	printk (KERN_INFO "Write protecting the kernel read-only data: %luk\n",
566a581c2a4SHeiko Carstens 		(unsigned long)(__end_rodata - __start_rodata) >> 10);
5671bcdd854SHelge Deller }
5681bcdd854SHelge Deller #endif
5691bcdd854SHelge Deller 
5701bcdd854SHelge Deller 
5711da177e4SLinus Torvalds /*
5721da177e4SLinus Torvalds  * Just an arbitrary offset to serve as a "hole" between mapping areas
5731da177e4SLinus Torvalds  * (between top of physical memory and a potential pcxl dma mapping
5741da177e4SLinus Torvalds  * area, and below the vmalloc mapping area).
5751da177e4SLinus Torvalds  *
5761da177e4SLinus Torvalds  * The current 32K value just means that there will be a 32K "hole"
5771da177e4SLinus Torvalds  * between mapping areas. That means that  any out-of-bounds memory
5781da177e4SLinus Torvalds  * accesses will hopefully be caught. The vmalloc() routines leaves
5791da177e4SLinus Torvalds  * a hole of 4kB between each vmalloced area for the same reason.
5801da177e4SLinus Torvalds  */
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds  /* Leave room for gateway page expansion */
5831da177e4SLinus Torvalds #if KERNEL_MAP_START < GATEWAY_PAGE_SIZE
5841da177e4SLinus Torvalds #error KERNEL_MAP_START is in gateway reserved region
5851da177e4SLinus Torvalds #endif
5861da177e4SLinus Torvalds #define MAP_START (KERNEL_MAP_START)
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds #define VM_MAP_OFFSET  (32*1024)
5891da177e4SLinus Torvalds #define SET_MAP_OFFSET(x) ((void *)(((unsigned long)(x) + VM_MAP_OFFSET) \
5901da177e4SLinus Torvalds 				     & ~(VM_MAP_OFFSET-1)))
5911da177e4SLinus Torvalds 
5924255f0d2SHelge Deller void *parisc_vmalloc_start __read_mostly;
5934255f0d2SHelge Deller EXPORT_SYMBOL(parisc_vmalloc_start);
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds #ifdef CONFIG_PA11
5968039de10SHelge Deller unsigned long pcxl_dma_start __read_mostly;
5971da177e4SLinus Torvalds #endif
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds void __init mem_init(void)
6001da177e4SLinus Torvalds {
601d0cf62fbSHelge Deller 	/* Do sanity checks on IPC (compat) structures */
602d0cf62fbSHelge Deller 	BUILD_BUG_ON(sizeof(struct ipc64_perm) != 48);
603d0cf62fbSHelge Deller #ifndef CONFIG_64BIT
604d0cf62fbSHelge Deller 	BUILD_BUG_ON(sizeof(struct semid64_ds) != 80);
605d0cf62fbSHelge Deller 	BUILD_BUG_ON(sizeof(struct msqid64_ds) != 104);
606d0cf62fbSHelge Deller 	BUILD_BUG_ON(sizeof(struct shmid64_ds) != 104);
607d0cf62fbSHelge Deller #endif
608d0cf62fbSHelge Deller #ifdef CONFIG_COMPAT
609d0cf62fbSHelge Deller 	BUILD_BUG_ON(sizeof(struct compat_ipc64_perm) != sizeof(struct ipc64_perm));
610d0cf62fbSHelge Deller 	BUILD_BUG_ON(sizeof(struct compat_semid64_ds) != 80);
611d0cf62fbSHelge Deller 	BUILD_BUG_ON(sizeof(struct compat_msqid64_ds) != 104);
612d0cf62fbSHelge Deller 	BUILD_BUG_ON(sizeof(struct compat_shmid64_ds) != 104);
613d0cf62fbSHelge Deller #endif
614d0cf62fbSHelge Deller 
61548d27cb2SHelge Deller 	/* Do sanity checks on page table constants */
61648d27cb2SHelge Deller 	BUILD_BUG_ON(PTE_ENTRY_SIZE != sizeof(pte_t));
61748d27cb2SHelge Deller 	BUILD_BUG_ON(PMD_ENTRY_SIZE != sizeof(pmd_t));
61848d27cb2SHelge Deller 	BUILD_BUG_ON(PGD_ENTRY_SIZE != sizeof(pgd_t));
61948d27cb2SHelge Deller 	BUILD_BUG_ON(PAGE_SHIFT + BITS_PER_PTE + BITS_PER_PMD + BITS_PER_PGD
62048d27cb2SHelge Deller 			> BITS_PER_LONG);
62148d27cb2SHelge Deller 
6221da177e4SLinus Torvalds 	high_memory = __va((max_pfn << PAGE_SHIFT));
623d5c017ddSJiang Liu 	set_max_mapnr(page_to_pfn(virt_to_page(high_memory - 1)) + 1);
6240c988534SJiang Liu 	free_all_bootmem();
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds #ifdef CONFIG_PA11
627a34a9b96SChristoph Hellwig 	if (boot_cpu_data.cpu_type == pcxl2 || boot_cpu_data.cpu_type == pcxl) {
6281da177e4SLinus Torvalds 		pcxl_dma_start = (unsigned long)SET_MAP_OFFSET(MAP_START);
6294255f0d2SHelge Deller 		parisc_vmalloc_start = SET_MAP_OFFSET(pcxl_dma_start
6304255f0d2SHelge Deller 						+ PCXL_DMA_MAP_SIZE);
631a34a9b96SChristoph Hellwig 	} else
6321da177e4SLinus Torvalds #endif
633a34a9b96SChristoph Hellwig 		parisc_vmalloc_start = SET_MAP_OFFSET(MAP_START);
6341da177e4SLinus Torvalds 
6357d2c7747SJiang Liu 	mem_init_print_info(NULL);
636fd8d0ca2SHelge Deller 
637fd8d0ca2SHelge Deller #if 0
638fd8d0ca2SHelge Deller 	/*
639fd8d0ca2SHelge Deller 	 * Do not expose the virtual kernel memory layout to userspace.
640fd8d0ca2SHelge Deller 	 * But keep code for debugging purposes.
641fd8d0ca2SHelge Deller 	 */
642ce8420bbSHelge Deller 	printk("virtual kernel memory layout:\n"
64363b2c373SHelge Deller 	       "    vmalloc : 0x%px - 0x%px   (%4ld MB)\n"
64463b2c373SHelge Deller 	       "    memory  : 0x%px - 0x%px   (%4ld MB)\n"
64563b2c373SHelge Deller 	       "      .init : 0x%px - 0x%px   (%4ld kB)\n"
64663b2c373SHelge Deller 	       "      .data : 0x%px - 0x%px   (%4ld kB)\n"
64763b2c373SHelge Deller 	       "      .text : 0x%px - 0x%px   (%4ld kB)\n",
648ce8420bbSHelge Deller 
649ce8420bbSHelge Deller 	       (void*)VMALLOC_START, (void*)VMALLOC_END,
650ce8420bbSHelge Deller 	       (VMALLOC_END - VMALLOC_START) >> 20,
651ce8420bbSHelge Deller 
652ce8420bbSHelge Deller 	       __va(0), high_memory,
653ce8420bbSHelge Deller 	       ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
654ce8420bbSHelge Deller 
65553faf291SKyle McMartin 	       __init_begin, __init_end,
65653faf291SKyle McMartin 	       ((unsigned long)__init_end - (unsigned long)__init_begin) >> 10,
657ce8420bbSHelge Deller 
65853faf291SKyle McMartin 	       _etext, _edata,
65953faf291SKyle McMartin 	       ((unsigned long)_edata - (unsigned long)_etext) >> 10,
660ce8420bbSHelge Deller 
66153faf291SKyle McMartin 	       _text, _etext,
66253faf291SKyle McMartin 	       ((unsigned long)_etext - (unsigned long)_text) >> 10);
663ce8420bbSHelge Deller #endif
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
6668039de10SHelge Deller unsigned long *empty_zero_page __read_mostly;
66722febf1fSKyle McMartin EXPORT_SYMBOL(empty_zero_page);
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds /*
6701da177e4SLinus Torvalds  * pagetable_init() sets up the page tables
6711da177e4SLinus Torvalds  *
6721da177e4SLinus Torvalds  * Note that gateway_init() places the Linux gateway page at page 0.
6731da177e4SLinus Torvalds  * Since gateway pages cannot be dereferenced this has the desirable
6741da177e4SLinus Torvalds  * side effect of trapping those pesky NULL-reference errors in the
6751da177e4SLinus Torvalds  * kernel.
6761da177e4SLinus Torvalds  */
6771da177e4SLinus Torvalds static void __init pagetable_init(void)
6781da177e4SLinus Torvalds {
6791da177e4SLinus Torvalds 	int range;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	/* Map each physical memory range to its kernel vaddr */
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	for (range = 0; range < npmem_ranges; range++) {
6841da177e4SLinus Torvalds 		unsigned long start_paddr;
6851da177e4SLinus Torvalds 		unsigned long end_paddr;
6861da177e4SLinus Torvalds 		unsigned long size;
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 		start_paddr = pmem_ranges[range].start_pfn << PAGE_SHIFT;
6891da177e4SLinus Torvalds 		size = pmem_ranges[range].pages << PAGE_SHIFT;
69041b85a11SHelge Deller 		end_paddr = start_paddr + size;
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 		map_pages((unsigned long)__va(start_paddr), start_paddr,
693d7dd2ff1SJames Bottomley 			  size, PAGE_KERNEL, 0);
6941da177e4SLinus Torvalds 	}
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD
6971da177e4SLinus Torvalds 	if (initrd_end && initrd_end > mem_limit) {
6981bcdd854SHelge Deller 		printk(KERN_INFO "initrd: mapping %08lx-%08lx\n", initrd_start, initrd_end);
6991da177e4SLinus Torvalds 		map_pages(initrd_start, __pa(initrd_start),
700d7dd2ff1SJames Bottomley 			  initrd_end - initrd_start, PAGE_KERNEL, 0);
7011da177e4SLinus Torvalds 	}
7021da177e4SLinus Torvalds #endif
7031da177e4SLinus Torvalds 
7044fe9e1d9SHelge Deller 	empty_zero_page = get_memblock(PAGE_SIZE);
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds static void __init gateway_init(void)
7081da177e4SLinus Torvalds {
7091da177e4SLinus Torvalds 	unsigned long linux_gateway_page_addr;
7101da177e4SLinus Torvalds 	/* FIXME: This is 'const' in order to trick the compiler
7111da177e4SLinus Torvalds 	   into not treating it as DP-relative data. */
7121da177e4SLinus Torvalds 	extern void * const linux_gateway_page;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	linux_gateway_page_addr = LINUX_GATEWAY_ADDR & PAGE_MASK;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	/*
7171da177e4SLinus Torvalds 	 * Setup Linux Gateway page.
7181da177e4SLinus Torvalds 	 *
7191da177e4SLinus Torvalds 	 * The Linux gateway page will reside in kernel space (on virtual
7201da177e4SLinus Torvalds 	 * page 0), so it doesn't need to be aliased into user space.
7211da177e4SLinus Torvalds 	 */
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	map_pages(linux_gateway_page_addr, __pa(&linux_gateway_page),
724d7dd2ff1SJames Bottomley 		  PAGE_SIZE, PAGE_GATEWAY, 1);
7251da177e4SLinus Torvalds }
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds void __init paging_init(void)
7281da177e4SLinus Torvalds {
7291da177e4SLinus Torvalds 	int i;
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	setup_bootmem();
7321da177e4SLinus Torvalds 	pagetable_init();
7331da177e4SLinus Torvalds 	gateway_init();
7341da177e4SLinus Torvalds 	flush_cache_all_local(); /* start with known state */
735ce33941fSMatthew Wilcox 	flush_tlb_all_local(NULL);
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	for (i = 0; i < npmem_ranges; i++) {
738f06a9684SChristoph Lameter 		unsigned long zones_size[MAX_NR_ZONES] = { 0, };
7391da177e4SLinus Torvalds 
74000592837SChristoph Lameter 		zones_size[ZONE_NORMAL] = pmem_ranges[i].pages;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds #ifdef CONFIG_DISCONTIGMEM
7431da177e4SLinus Torvalds 		/* Need to initialize the pfnnid_map before we can initialize
7441da177e4SLinus Torvalds 		   the zone */
7451da177e4SLinus Torvalds 		{
7461da177e4SLinus Torvalds 		    int j;
7471da177e4SLinus Torvalds 		    for (j = (pmem_ranges[i].start_pfn >> PFNNID_SHIFT);
7481da177e4SLinus Torvalds 			 j <= ((pmem_ranges[i].start_pfn + pmem_ranges[i].pages) >> PFNNID_SHIFT);
7491da177e4SLinus Torvalds 			 j++) {
7501da177e4SLinus Torvalds 			pfnnid_map[j] = i;
7511da177e4SLinus Torvalds 		    }
7521da177e4SLinus Torvalds 		}
7531da177e4SLinus Torvalds #endif
7541da177e4SLinus Torvalds 
7559109fb7bSJohannes Weiner 		free_area_init_node(i, zones_size,
7561da177e4SLinus Torvalds 				pmem_ranges[i].start_pfn, NULL);
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds }
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds #ifdef CONFIG_PA20
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds /*
7637022672eSSimon Arlott  * Currently, all PA20 chips have 18 bit protection IDs, which is the
7641da177e4SLinus Torvalds  * limiting factor (space ids are 32 bits).
7651da177e4SLinus Torvalds  */
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds #define NR_SPACE_IDS 262144
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds #else
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds /*
7727022672eSSimon Arlott  * Currently we have a one-to-one relationship between space IDs and
7737022672eSSimon Arlott  * protection IDs. Older parisc chips (PCXS, PCXT, PCXL, PCXL2) only
7747022672eSSimon Arlott  * support 15 bit protection IDs, so that is the limiting factor.
7757022672eSSimon Arlott  * PCXT' has 18 bit protection IDs, but only 16 bit spaceids, so it's
7761da177e4SLinus Torvalds  * probably not worth the effort for a special case here.
7771da177e4SLinus Torvalds  */
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds #define NR_SPACE_IDS 32768
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds #endif  /* !CONFIG_PA20 */
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds #define RECYCLE_THRESHOLD (NR_SPACE_IDS / 2)
7841da177e4SLinus Torvalds #define SID_ARRAY_SIZE  (NR_SPACE_IDS / (8 * sizeof(long)))
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds static unsigned long space_id[SID_ARRAY_SIZE] = { 1 }; /* disallow space 0 */
7871da177e4SLinus Torvalds static unsigned long dirty_space_id[SID_ARRAY_SIZE];
7881da177e4SLinus Torvalds static unsigned long space_id_index;
7891da177e4SLinus Torvalds static unsigned long free_space_ids = NR_SPACE_IDS - 1;
7901da177e4SLinus Torvalds static unsigned long dirty_space_ids = 0;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds static DEFINE_SPINLOCK(sid_lock);
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds unsigned long alloc_sid(void)
7951da177e4SLinus Torvalds {
7961da177e4SLinus Torvalds 	unsigned long index;
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	spin_lock(&sid_lock);
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	if (free_space_ids == 0) {
8011da177e4SLinus Torvalds 		if (dirty_space_ids != 0) {
8021da177e4SLinus Torvalds 			spin_unlock(&sid_lock);
8031da177e4SLinus Torvalds 			flush_tlb_all(); /* flush_tlb_all() calls recycle_sids() */
8041da177e4SLinus Torvalds 			spin_lock(&sid_lock);
8051da177e4SLinus Torvalds 		}
8062fd83038SHelge Deller 		BUG_ON(free_space_ids == 0);
8071da177e4SLinus Torvalds 	}
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	free_space_ids--;
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	index = find_next_zero_bit(space_id, NR_SPACE_IDS, space_id_index);
8121da177e4SLinus Torvalds 	space_id[index >> SHIFT_PER_LONG] |= (1L << (index & (BITS_PER_LONG - 1)));
8131da177e4SLinus Torvalds 	space_id_index = index;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	spin_unlock(&sid_lock);
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	return index << SPACEID_SHIFT;
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds void free_sid(unsigned long spaceid)
8211da177e4SLinus Torvalds {
8221da177e4SLinus Torvalds 	unsigned long index = spaceid >> SPACEID_SHIFT;
8231da177e4SLinus Torvalds 	unsigned long *dirty_space_offset;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	dirty_space_offset = dirty_space_id + (index >> SHIFT_PER_LONG);
8261da177e4SLinus Torvalds 	index &= (BITS_PER_LONG - 1);
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	spin_lock(&sid_lock);
8291da177e4SLinus Torvalds 
8302fd83038SHelge Deller 	BUG_ON(*dirty_space_offset & (1L << index)); /* attempt to free space id twice */
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	*dirty_space_offset |= (1L << index);
8331da177e4SLinus Torvalds 	dirty_space_ids++;
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	spin_unlock(&sid_lock);
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds #ifdef CONFIG_SMP
8401da177e4SLinus Torvalds static void get_dirty_sids(unsigned long *ndirtyptr,unsigned long *dirty_array)
8411da177e4SLinus Torvalds {
8421da177e4SLinus Torvalds 	int i;
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	/* NOTE: sid_lock must be held upon entry */
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 	*ndirtyptr = dirty_space_ids;
8471da177e4SLinus Torvalds 	if (dirty_space_ids != 0) {
8481da177e4SLinus Torvalds 	    for (i = 0; i < SID_ARRAY_SIZE; i++) {
8491da177e4SLinus Torvalds 		dirty_array[i] = dirty_space_id[i];
8501da177e4SLinus Torvalds 		dirty_space_id[i] = 0;
8511da177e4SLinus Torvalds 	    }
8521da177e4SLinus Torvalds 	    dirty_space_ids = 0;
8531da177e4SLinus Torvalds 	}
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	return;
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds static void recycle_sids(unsigned long ndirty,unsigned long *dirty_array)
8591da177e4SLinus Torvalds {
8601da177e4SLinus Torvalds 	int i;
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	/* NOTE: sid_lock must be held upon entry */
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	if (ndirty != 0) {
8651da177e4SLinus Torvalds 		for (i = 0; i < SID_ARRAY_SIZE; i++) {
8661da177e4SLinus Torvalds 			space_id[i] ^= dirty_array[i];
8671da177e4SLinus Torvalds 		}
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 		free_space_ids += ndirty;
8701da177e4SLinus Torvalds 		space_id_index = 0;
8711da177e4SLinus Torvalds 	}
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds #else /* CONFIG_SMP */
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds static void recycle_sids(void)
8771da177e4SLinus Torvalds {
8781da177e4SLinus Torvalds 	int i;
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	/* NOTE: sid_lock must be held upon entry */
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	if (dirty_space_ids != 0) {
8831da177e4SLinus Torvalds 		for (i = 0; i < SID_ARRAY_SIZE; i++) {
8841da177e4SLinus Torvalds 			space_id[i] ^= dirty_space_id[i];
8851da177e4SLinus Torvalds 			dirty_space_id[i] = 0;
8861da177e4SLinus Torvalds 		}
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 		free_space_ids += dirty_space_ids;
8891da177e4SLinus Torvalds 		dirty_space_ids = 0;
8901da177e4SLinus Torvalds 		space_id_index = 0;
8911da177e4SLinus Torvalds 	}
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds #endif
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds /*
8961da177e4SLinus Torvalds  * flush_tlb_all() calls recycle_sids(), since whenever the entire tlb is
8971da177e4SLinus Torvalds  * purged, we can safely reuse the space ids that were released but
8981da177e4SLinus Torvalds  * not flushed from the tlb.
8991da177e4SLinus Torvalds  */
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds #ifdef CONFIG_SMP
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds static unsigned long recycle_ndirty;
9041da177e4SLinus Torvalds static unsigned long recycle_dirty_array[SID_ARRAY_SIZE];
9052fd83038SHelge Deller static unsigned int recycle_inuse;
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds void flush_tlb_all(void)
9081da177e4SLinus Torvalds {
9091da177e4SLinus Torvalds 	int do_recycle;
9101da177e4SLinus Torvalds 
911416821d3SHelge Deller 	__inc_irq_stat(irq_tlb_count);
9121da177e4SLinus Torvalds 	do_recycle = 0;
9131da177e4SLinus Torvalds 	spin_lock(&sid_lock);
9141da177e4SLinus Torvalds 	if (dirty_space_ids > RECYCLE_THRESHOLD) {
9152fd83038SHelge Deller 	    BUG_ON(recycle_inuse);  /* FIXME: Use a semaphore/wait queue here */
9161da177e4SLinus Torvalds 	    get_dirty_sids(&recycle_ndirty,recycle_dirty_array);
9171da177e4SLinus Torvalds 	    recycle_inuse++;
9181da177e4SLinus Torvalds 	    do_recycle++;
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds 	spin_unlock(&sid_lock);
92115c8b6c1SJens Axboe 	on_each_cpu(flush_tlb_all_local, NULL, 1);
9221da177e4SLinus Torvalds 	if (do_recycle) {
9231da177e4SLinus Torvalds 	    spin_lock(&sid_lock);
9241da177e4SLinus Torvalds 	    recycle_sids(recycle_ndirty,recycle_dirty_array);
9251da177e4SLinus Torvalds 	    recycle_inuse = 0;
9261da177e4SLinus Torvalds 	    spin_unlock(&sid_lock);
9271da177e4SLinus Torvalds 	}
9281da177e4SLinus Torvalds }
9291da177e4SLinus Torvalds #else
9301da177e4SLinus Torvalds void flush_tlb_all(void)
9311da177e4SLinus Torvalds {
932416821d3SHelge Deller 	__inc_irq_stat(irq_tlb_count);
9331da177e4SLinus Torvalds 	spin_lock(&sid_lock);
9341b2425e3SMatthew Wilcox 	flush_tlb_all_local(NULL);
9351da177e4SLinus Torvalds 	recycle_sids();
9361da177e4SLinus Torvalds 	spin_unlock(&sid_lock);
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds #endif
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD
9411da177e4SLinus Torvalds void free_initrd_mem(unsigned long start, unsigned long end)
9421da177e4SLinus Torvalds {
9437d2c7747SJiang Liu 	free_reserved_area((void *)start, (void *)end, -1, "initrd");
9441da177e4SLinus Torvalds }
9451da177e4SLinus Torvalds #endif
946