init.c 24.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9
/*
 *  linux/arch/parisc/mm/init.c
 *
 *  Copyright (C) 1995	Linus Torvalds
 *  Copyright 1999 SuSE GmbH
 *    changed by Philipp Rumpf
 *  Copyright 1999 Philipp Rumpf (prumpf@tux.org)
 *  Copyright 2004 Randolph Chung (tausq@debian.org)
10
 *  Copyright 2006-2007 Helge Deller (deller@gmx.de)
L
Linus Torvalds 已提交
11 12 13 14 15 16 17
 *
 */


#include <linux/module.h>
#include <linux/mm.h>
#include <linux/bootmem.h>
18
#include <linux/memblock.h>
19
#include <linux/gfp.h>
L
Linus Torvalds 已提交
20 21 22 23 24 25 26
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/pci.h>		/* for hppa_dma_ops and pcxl_dma_ops */
#include <linux/initrd.h>
#include <linux/swap.h>
#include <linux/unistd.h>
#include <linux/nodemask.h>	/* for node_online_map */
27
#include <linux/pagemap.h>	/* for release_pages */
28
#include <linux/compat.h>
L
Linus Torvalds 已提交
29 30

#include <asm/pgalloc.h>
31
#include <asm/pgtable.h>
L
Linus Torvalds 已提交
32 33 34
#include <asm/tlb.h>
#include <asm/pdc_chassis.h>
#include <asm/mmzone.h>
35
#include <asm/sections.h>
36
#include <asm/msgbuf.h>
L
Linus Torvalds 已提交
37 38

extern int  data_start;
39
extern void parisc_kernel_start(void);	/* Kernel entry point in head.S */
L
Linus Torvalds 已提交
40

41
#if CONFIG_PGTABLE_LEVELS == 3
T
Thomas Gleixner 已提交
42 43 44 45 46 47 48 49 50 51 52
/* NOTE: This layout exactly conforms to the hybrid L2/L3 page table layout
 * with the first pmd adjacent to the pgd and below it. gcc doesn't actually
 * guarantee that global objects will be laid out in memory in the same order
 * as the order of declaration, so put these in different sections and use
 * the linker script to order them. */
pmd_t pmd0[PTRS_PER_PMD] __attribute__ ((__section__ (".data..vm0.pmd"), aligned(PAGE_SIZE)));
#endif

pgd_t swapper_pg_dir[PTRS_PER_PGD] __attribute__ ((__section__ (".data..vm0.pgd"), aligned(PAGE_SIZE)));
pte_t pg0[PT_INITIAL * PTRS_PER_PTE] __attribute__ ((__section__ (".data..vm0.pte"), aligned(PAGE_SIZE)));

L
Linus Torvalds 已提交
53
#ifdef CONFIG_DISCONTIGMEM
54
struct node_map_data node_data[MAX_NUMNODES] __read_mostly;
55
signed char pfnnid_map[PFNNID_MAP_MAX] __read_mostly;
L
Linus Torvalds 已提交
56 57 58 59
#endif

static struct resource data_resource = {
	.name	= "Kernel data",
60
	.flags	= IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
L
Linus Torvalds 已提交
61 62 63 64
};

static struct resource code_resource = {
	.name	= "Kernel code",
65
	.flags	= IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73 74
};

static struct resource pdcdata_resource = {
	.name	= "PDC data (Page Zero)",
	.start	= 0,
	.end	= 0x9ff,
	.flags	= IORESOURCE_BUSY | IORESOURCE_MEM,
};

75
static struct resource sysram_resources[MAX_PHYSMEM_RANGES] __read_mostly;
L
Linus Torvalds 已提交
76 77 78 79 80

/* The following array is initialized from the firmware specific
 * information retrieved in kernel/inventory.c.
 */

81 82
physmem_range_t pmem_ranges[MAX_PHYSMEM_RANGES] __read_mostly;
int npmem_ranges __read_mostly;
L
Linus Torvalds 已提交
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
/*
 * get_memblock() allocates pages via memblock.
 * We can't use memblock_find_in_range(0, KERNEL_INITIAL_SIZE) here since it
 * doesn't allocate from bottom to top which is needed because we only created
 * the initial mapping up to KERNEL_INITIAL_SIZE in the assembly bootup code.
 */
static void * __init get_memblock(unsigned long size)
{
	static phys_addr_t search_addr __initdata;
	phys_addr_t phys;

	if (!search_addr)
		search_addr = PAGE_ALIGN(__pa((unsigned long) &_end));
	search_addr = ALIGN(search_addr, size);
	while (!memblock_is_region_memory(search_addr, size) ||
		memblock_is_region_reserved(search_addr, size)) {
		search_addr += size;
	}
	phys = search_addr;

	if (phys)
		memblock_reserve(phys, size);
	else
		panic("get_memblock() failed.\n");

109 110
	memset(__va(phys), 0, size);

111 112 113
	return __va(phys);
}

114
#ifdef CONFIG_64BIT
L
Linus Torvalds 已提交
115
#define MAX_MEM         (~0UL)
116
#else /* !CONFIG_64BIT */
L
Linus Torvalds 已提交
117
#define MAX_MEM         (3584U*1024U*1024U)
118
#endif /* !CONFIG_64BIT */
L
Linus Torvalds 已提交
119

120
static unsigned long mem_limit __read_mostly = MAX_MEM;
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128 129

static void __init mem_limit_func(void)
{
	char *cp, *end;
	unsigned long limit;

	/* We need this before __setup() functions are called */

	limit = MAX_MEM;
130
	for (cp = boot_command_line; *cp; ) {
L
Linus Torvalds 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
		if (memcmp(cp, "mem=", 4) == 0) {
			cp += 4;
			limit = memparse(cp, &end);
			if (end != cp)
				break;
			cp = end;
		} else {
			while (*cp != ' ' && *cp)
				++cp;
			while (*cp == ' ')
				++cp;
		}
	}

	if (limit < mem_limit)
		mem_limit = limit;
}

#define MAX_GAP (0x40000000UL >> PAGE_SHIFT)

static void __init setup_bootmem(void)
{
	unsigned long mem_max;
#ifndef CONFIG_DISCONTIGMEM
	physmem_range_t pmem_holes[MAX_PHYSMEM_RANGES - 1];
	int npmem_holes;
#endif
	int i, sysram_resource_count;

	disable_sr_hashing(); /* Turn off space register hashing */

	/*
	 * Sort the ranges. Since the number of ranges is typically
	 * small, and performance is not an issue here, just do
	 * a simple insertion sort.
	 */

	for (i = 1; i < npmem_ranges; i++) {
		int j;

		for (j = i; j > 0; j--) {
			unsigned long tmp;

			if (pmem_ranges[j-1].start_pfn <
			    pmem_ranges[j].start_pfn) {

				break;
			}
			tmp = pmem_ranges[j-1].start_pfn;
			pmem_ranges[j-1].start_pfn = pmem_ranges[j].start_pfn;
			pmem_ranges[j].start_pfn = tmp;
			tmp = pmem_ranges[j-1].pages;
			pmem_ranges[j-1].pages = pmem_ranges[j].pages;
			pmem_ranges[j].pages = tmp;
		}
	}

#ifndef CONFIG_DISCONTIGMEM
	/*
	 * Throw out ranges that are too far apart (controlled by
	 * MAX_GAP).
	 */

	for (i = 1; i < npmem_ranges; i++) {
		if (pmem_ranges[i].start_pfn -
			(pmem_ranges[i-1].start_pfn +
			 pmem_ranges[i-1].pages) > MAX_GAP) {
			npmem_ranges = i;
			printk("Large gap in memory detected (%ld pages). "
			       "Consider turning on CONFIG_DISCONTIGMEM\n",
			       pmem_ranges[i].start_pfn -
			       (pmem_ranges[i-1].start_pfn +
			        pmem_ranges[i-1].pages));
			break;
		}
	}
#endif

209 210
	/* Print the memory ranges */
	pr_info("Memory Ranges:\n");
L
Linus Torvalds 已提交
211

212 213 214 215
	for (i = 0; i < npmem_ranges; i++) {
		struct resource *res = &sysram_resources[i];
		unsigned long start;
		unsigned long size;
L
Linus Torvalds 已提交
216

217 218 219 220
		size = (pmem_ranges[i].pages << PAGE_SHIFT);
		start = (pmem_ranges[i].start_pfn << PAGE_SHIFT);
		pr_info("%2d) Start 0x%016lx End 0x%016lx Size %6ld MB\n",
			i, start, start + (size - 1), size >> 20);
L
Linus Torvalds 已提交
221

222
		/* request memory resource */
L
Linus Torvalds 已提交
223
		res->name = "System RAM";
224 225
		res->start = start;
		res->end = start + size - 1;
226
		res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
L
Linus Torvalds 已提交
227 228 229
		request_resource(&iomem_resource, res);
	}

230 231
	sysram_resource_count = npmem_ranges;

L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	/*
	 * For 32 bit kernels we limit the amount of memory we can
	 * support, in order to preserve enough kernel address space
	 * for other purposes. For 64 bit kernels we don't normally
	 * limit the memory, but this mechanism can be used to
	 * artificially limit the amount of memory (and it is written
	 * to work with multiple memory ranges).
	 */

	mem_limit_func();       /* check for "mem=" argument */

	mem_max = 0;
	for (i = 0; i < npmem_ranges; i++) {
		unsigned long rsize;

		rsize = pmem_ranges[i].pages << PAGE_SHIFT;
		if ((mem_max + rsize) > mem_limit) {
			printk(KERN_WARNING "Memory truncated to %ld MB\n", mem_limit >> 20);
			if (mem_max == mem_limit)
				npmem_ranges = i;
			else {
				pmem_ranges[i].pages =   (mem_limit >> PAGE_SHIFT)
						       - (mem_max >> PAGE_SHIFT);
				npmem_ranges = i + 1;
				mem_max = mem_limit;
			}
			break;
		}
		mem_max += rsize;
	}

	printk(KERN_INFO "Total Memory: %ld MB\n",mem_max >> 20);

#ifndef CONFIG_DISCONTIGMEM
	/* Merge the ranges, keeping track of the holes */

	{
		unsigned long end_pfn;
		unsigned long hole_pages;

		npmem_holes = 0;
		end_pfn = pmem_ranges[0].start_pfn + pmem_ranges[0].pages;
		for (i = 1; i < npmem_ranges; i++) {

			hole_pages = pmem_ranges[i].start_pfn - end_pfn;
			if (hole_pages) {
				pmem_holes[npmem_holes].start_pfn = end_pfn;
				pmem_holes[npmem_holes++].pages = hole_pages;
				end_pfn += hole_pages;
			}
			end_pfn += pmem_ranges[i].pages;
		}

		pmem_ranges[0].pages = end_pfn - pmem_ranges[0].start_pfn;
		npmem_ranges = 1;
	}
#endif

#ifdef CONFIG_DISCONTIGMEM
	for (i = 0; i < MAX_PHYSMEM_RANGES; i++) {
		memset(NODE_DATA(i), 0, sizeof(pg_data_t));
	}
	memset(pfnnid_map, 0xff, sizeof(pfnnid_map));

296 297
	for (i = 0; i < npmem_ranges; i++) {
		node_set_state(i, N_NORMAL_MEMORY);
L
Linus Torvalds 已提交
298
		node_set_online(i);
299
	}
L
Linus Torvalds 已提交
300 301 302 303 304 305 306 307 308 309
#endif

	/*
	 * Initialize and free the full range of memory in each range.
	 */

	max_pfn = 0;
	for (i = 0; i < npmem_ranges; i++) {
		unsigned long start_pfn;
		unsigned long npages;
310 311
		unsigned long start;
		unsigned long size;
L
Linus Torvalds 已提交
312 313 314 315

		start_pfn = pmem_ranges[i].start_pfn;
		npages = pmem_ranges[i].pages;

316 317 318 319 320 321
		start = start_pfn << PAGE_SHIFT;
		size = npages << PAGE_SHIFT;

		/* add system RAM memblock */
		memblock_add(start, size);

L
Linus Torvalds 已提交
322 323 324 325
		if ((start_pfn + npages) > max_pfn)
			max_pfn = start_pfn + npages;
	}

326 327 328 329 330 331 332
	/* IOMMU is always used to access "high mem" on those boxes
	 * that can support enough mem that a PCI device couldn't
	 * directly DMA to any physical addresses.
	 * ISA DMA support will need to revisit this.
	 */
	max_low_pfn = max_pfn;

L
Linus Torvalds 已提交
333 334 335 336
	/* reserve PAGE0 pdc memory, kernel text/data/bss & bootmap */

#define PDC_CONSOLE_IO_IODC_SIZE 32768

337 338 339 340
	memblock_reserve(0UL, (unsigned long)(PAGE0->mem_free +
				PDC_CONSOLE_IO_IODC_SIZE));
	memblock_reserve(__pa(KERNEL_BINARY_TEXT_START),
			(unsigned long)(_end - KERNEL_BINARY_TEXT_START));
L
Linus Torvalds 已提交
341 342 343 344 345 346

#ifndef CONFIG_DISCONTIGMEM

	/* reserve the holes */

	for (i = 0; i < npmem_holes; i++) {
347 348
		memblock_reserve((pmem_holes[i].start_pfn << PAGE_SHIFT),
				(pmem_holes[i].pages << PAGE_SHIFT));
L
Linus Torvalds 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	}
#endif

#ifdef CONFIG_BLK_DEV_INITRD
	if (initrd_start) {
		printk(KERN_INFO "initrd: %08lx-%08lx\n", initrd_start, initrd_end);
		if (__pa(initrd_start) < mem_max) {
			unsigned long initrd_reserve;

			if (__pa(initrd_end) > mem_max) {
				initrd_reserve = mem_max - __pa(initrd_start);
			} else {
				initrd_reserve = initrd_end - initrd_start;
			}
			initrd_below_start_ok = 1;
			printk(KERN_INFO "initrd: reserving %08lx-%08lx (mem_max %08lx)\n", __pa(initrd_start), __pa(initrd_start) + initrd_reserve, mem_max);

366
			memblock_reserve(__pa(initrd_start), initrd_reserve);
L
Linus Torvalds 已提交
367 368 369 370 371
		}
	}
#endif

	data_resource.start =  virt_to_phys(&data_start);
372 373
	data_resource.end = virt_to_phys(_end) - 1;
	code_resource.start = virt_to_phys(_text);
L
Linus Torvalds 已提交
374 375 376 377 378 379 380 381 382 383 384
	code_resource.end = virt_to_phys(&data_start)-1;

	/* We don't know which region the kernel will be in, so try
	 * all of them.
	 */
	for (i = 0; i < sysram_resource_count; i++) {
		struct resource *res = &sysram_resources[i];
		request_resource(res, &code_resource);
		request_resource(res, &data_resource);
	}
	request_resource(&sysram_resources[0], &pdcdata_resource);
385 386 387

	/* Initialize Page Deallocation Table (PDT) and check for bad memory. */
	pdc_pdt_init();
L
Linus Torvalds 已提交
388 389
}

390 391 392 393 394 395 396 397 398 399 400
static int __init parisc_text_address(unsigned long vaddr)
{
	static unsigned long head_ptr __initdata;

	if (!head_ptr)
		head_ptr = PAGE_MASK & (unsigned long)
			dereference_function_descriptor(&parisc_kernel_start);

	return core_kernel_text(vaddr) || vaddr == head_ptr;
}

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
static void __init map_pages(unsigned long start_vaddr,
			     unsigned long start_paddr, unsigned long size,
			     pgprot_t pgprot, int force)
{
	pgd_t *pg_dir;
	pmd_t *pmd;
	pte_t *pg_table;
	unsigned long end_paddr;
	unsigned long start_pmd;
	unsigned long start_pte;
	unsigned long tmp1;
	unsigned long tmp2;
	unsigned long address;
	unsigned long vaddr;
	unsigned long ro_start;
	unsigned long ro_end;
417
	unsigned long kernel_end;
418 419 420

	ro_start = __pa((unsigned long)_text);
	ro_end   = __pa((unsigned long)&data_start);
421
	kernel_end  = __pa((unsigned long)&_end);
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

	end_paddr = start_paddr + size;

	pg_dir = pgd_offset_k(start_vaddr);

#if PTRS_PER_PMD == 1
	start_pmd = 0;
#else
	start_pmd = ((start_vaddr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
#endif
	start_pte = ((start_vaddr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));

	address = start_paddr;
	vaddr = start_vaddr;
	while (address < end_paddr) {
#if PTRS_PER_PMD == 1
		pmd = (pmd_t *)__pa(pg_dir);
#else
		pmd = (pmd_t *)pgd_address(*pg_dir);

		/*
		 * pmd is physical at this point
		 */

		if (!pmd) {
447
			pmd = (pmd_t *) get_memblock(PAGE_SIZE << PMD_ORDER);
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
			pmd = (pmd_t *) __pa(pmd);
		}

		pgd_populate(NULL, pg_dir, __va(pmd));
#endif
		pg_dir++;

		/* now change pmd to kernel virtual addresses */

		pmd = (pmd_t *)__va(pmd) + start_pmd;
		for (tmp1 = start_pmd; tmp1 < PTRS_PER_PMD; tmp1++, pmd++) {

			/*
			 * pg_table is physical at this point
			 */

			pg_table = (pte_t *)pmd_address(*pmd);
			if (!pg_table) {
466
				pg_table = (pte_t *) get_memblock(PAGE_SIZE);
467 468 469 470 471 472 473 474 475 476 477 478 479
				pg_table = (pte_t *) __pa(pg_table);
			}

			pmd_populate_kernel(NULL, pmd, __va(pg_table));

			/* now change pg_table to kernel virtual addresses */

			pg_table = (pte_t *) __va(pg_table) + start_pte;
			for (tmp2 = start_pte; tmp2 < PTRS_PER_PTE; tmp2++, pg_table++) {
				pte_t pte;

				if (force)
					pte =  __mk_pte(address, pgprot);
480
				else if (parisc_text_address(vaddr)) {
481
					pte = __mk_pte(address, PAGE_KERNEL_EXEC);
482 483 484
					if (address >= ro_start && address < kernel_end)
						pte = pte_mkhuge(pte);
				}
485 486
				else
#if defined(CONFIG_PARISC_PAGE_SIZE_4KB)
487 488 489 490
				if (address >= ro_start && address < ro_end) {
					pte = __mk_pte(address, PAGE_KERNEL_EXEC);
					pte = pte_mkhuge(pte);
				} else
491
#endif
492
				{
493
					pte = __mk_pte(address, pgprot);
494 495 496
					if (address >= ro_start && address < kernel_end)
						pte = pte_mkhuge(pte);
				}
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

				if (address >= end_paddr) {
					if (force)
						break;
					else
						pte_val(pte) = 0;
				}

				set_pte(pg_table, pte);

				address += PAGE_SIZE;
				vaddr += PAGE_SIZE;
			}
			start_pte = 0;

			if (address >= end_paddr)
			    break;
		}
		start_pmd = 0;
	}
}

L
Linus Torvalds 已提交
519 520
void free_initmem(void)
{
521 522
	unsigned long init_begin = (unsigned long)__init_begin;
	unsigned long init_end = (unsigned long)__init_end;
L
Linus Torvalds 已提交
523

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
	/* The init text pages are marked R-X.  We have to
	 * flush the icache and mark them RW-
	 *
	 * This is tricky, because map_pages is in the init section.
	 * Do a dummy remap of the data section first (the data
	 * section is already PAGE_KERNEL) to pull in the TLB entries
	 * for map_kernel */
	map_pages(init_begin, __pa(init_begin), init_end - init_begin,
		  PAGE_KERNEL_RWX, 1);
	/* now remap at PAGE_KERNEL since the TLB is pre-primed to execute
	 * map_pages */
	map_pages(init_begin, __pa(init_begin), init_end - init_begin,
		  PAGE_KERNEL, 1);

	/* force the kernel to see the new TLB entries */
	__flush_tlb_range(0, init_begin, init_end);
540

541 542
	/* finally dump all the instructions which were cached, since the
	 * pages are no-longer executable */
543
	flush_icache_range(init_begin, init_end);
L
Linus Torvalds 已提交
544
	
545
	free_initmem_default(POISON_FREE_INITMEM);
L
Linus Torvalds 已提交
546 547 548 549 550

	/* set up a new led state on systems shipped LED State panel */
	pdc_chassis_send_status(PDC_CHASSIS_DIRECT_BCOMPLETE);
}

551

552
#ifdef CONFIG_STRICT_KERNEL_RWX
553 554 555 556 557
void mark_rodata_ro(void)
{
	/* rodata memory was already mapped with KERNEL_RO access rights by
           pagetable_init() and map_pages(). No need to do additional stuff here */
	printk (KERN_INFO "Write protecting the kernel read-only data: %luk\n",
558
		(unsigned long)(__end_rodata - __start_rodata) >> 10);
559 560 561 562
}
#endif


L
Linus Torvalds 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
/*
 * Just an arbitrary offset to serve as a "hole" between mapping areas
 * (between top of physical memory and a potential pcxl dma mapping
 * area, and below the vmalloc mapping area).
 *
 * The current 32K value just means that there will be a 32K "hole"
 * between mapping areas. That means that  any out-of-bounds memory
 * accesses will hopefully be caught. The vmalloc() routines leaves
 * a hole of 4kB between each vmalloced area for the same reason.
 */

 /* Leave room for gateway page expansion */
#if KERNEL_MAP_START < GATEWAY_PAGE_SIZE
#error KERNEL_MAP_START is in gateway reserved region
#endif
#define MAP_START (KERNEL_MAP_START)

#define VM_MAP_OFFSET  (32*1024)
#define SET_MAP_OFFSET(x) ((void *)(((unsigned long)(x) + VM_MAP_OFFSET) \
				     & ~(VM_MAP_OFFSET-1)))

584 585
void *parisc_vmalloc_start __read_mostly;
EXPORT_SYMBOL(parisc_vmalloc_start);
L
Linus Torvalds 已提交
586 587

#ifdef CONFIG_PA11
588
unsigned long pcxl_dma_start __read_mostly;
L
Linus Torvalds 已提交
589 590 591 592
#endif

void __init mem_init(void)
{
593 594 595 596 597 598 599 600 601 602 603 604 605 606
	/* Do sanity checks on IPC (compat) structures */
	BUILD_BUG_ON(sizeof(struct ipc64_perm) != 48);
#ifndef CONFIG_64BIT
	BUILD_BUG_ON(sizeof(struct semid64_ds) != 80);
	BUILD_BUG_ON(sizeof(struct msqid64_ds) != 104);
	BUILD_BUG_ON(sizeof(struct shmid64_ds) != 104);
#endif
#ifdef CONFIG_COMPAT
	BUILD_BUG_ON(sizeof(struct compat_ipc64_perm) != sizeof(struct ipc64_perm));
	BUILD_BUG_ON(sizeof(struct compat_semid64_ds) != 80);
	BUILD_BUG_ON(sizeof(struct compat_msqid64_ds) != 104);
	BUILD_BUG_ON(sizeof(struct compat_shmid64_ds) != 104);
#endif

607 608 609 610 611 612 613
	/* Do sanity checks on page table constants */
	BUILD_BUG_ON(PTE_ENTRY_SIZE != sizeof(pte_t));
	BUILD_BUG_ON(PMD_ENTRY_SIZE != sizeof(pmd_t));
	BUILD_BUG_ON(PGD_ENTRY_SIZE != sizeof(pgd_t));
	BUILD_BUG_ON(PAGE_SHIFT + BITS_PER_PTE + BITS_PER_PMD + BITS_PER_PGD
			> BITS_PER_LONG);

L
Linus Torvalds 已提交
614
	high_memory = __va((max_pfn << PAGE_SHIFT));
615
	set_max_mapnr(page_to_pfn(virt_to_page(high_memory - 1)) + 1);
616
	free_all_bootmem();
L
Linus Torvalds 已提交
617 618 619 620

#ifdef CONFIG_PA11
	if (hppa_dma_ops == &pcxl_dma_ops) {
		pcxl_dma_start = (unsigned long)SET_MAP_OFFSET(MAP_START);
621 622
		parisc_vmalloc_start = SET_MAP_OFFSET(pcxl_dma_start
						+ PCXL_DMA_MAP_SIZE);
L
Linus Torvalds 已提交
623 624
	} else {
		pcxl_dma_start = 0;
625
		parisc_vmalloc_start = SET_MAP_OFFSET(MAP_START);
L
Linus Torvalds 已提交
626 627
	}
#else
628
	parisc_vmalloc_start = SET_MAP_OFFSET(MAP_START);
L
Linus Torvalds 已提交
629 630
#endif

631
	mem_init_print_info(NULL);
632 633 634 635 636 637

#if 0
	/*
	 * Do not expose the virtual kernel memory layout to userspace.
	 * But keep code for debugging purposes.
	 */
638
	printk("virtual kernel memory layout:\n"
639 640 641 642 643
	       "    vmalloc : 0x%px - 0x%px   (%4ld MB)\n"
	       "    memory  : 0x%px - 0x%px   (%4ld MB)\n"
	       "      .init : 0x%px - 0x%px   (%4ld kB)\n"
	       "      .data : 0x%px - 0x%px   (%4ld kB)\n"
	       "      .text : 0x%px - 0x%px   (%4ld kB)\n",
644 645 646 647 648 649 650

	       (void*)VMALLOC_START, (void*)VMALLOC_END,
	       (VMALLOC_END - VMALLOC_START) >> 20,

	       __va(0), high_memory,
	       ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,

651 652
	       __init_begin, __init_end,
	       ((unsigned long)__init_end - (unsigned long)__init_begin) >> 10,
653

654 655
	       _etext, _edata,
	       ((unsigned long)_edata - (unsigned long)_etext) >> 10,
656

657 658
	       _text, _etext,
	       ((unsigned long)_etext - (unsigned long)_text) >> 10);
659
#endif
L
Linus Torvalds 已提交
660 661
}

662
unsigned long *empty_zero_page __read_mostly;
K
Kyle McMartin 已提交
663
EXPORT_SYMBOL(empty_zero_page);
L
Linus Torvalds 已提交
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685

/*
 * pagetable_init() sets up the page tables
 *
 * Note that gateway_init() places the Linux gateway page at page 0.
 * Since gateway pages cannot be dereferenced this has the desirable
 * side effect of trapping those pesky NULL-reference errors in the
 * kernel.
 */
static void __init pagetable_init(void)
{
	int range;

	/* Map each physical memory range to its kernel vaddr */

	for (range = 0; range < npmem_ranges; range++) {
		unsigned long start_paddr;
		unsigned long end_paddr;
		unsigned long size;

		start_paddr = pmem_ranges[range].start_pfn << PAGE_SHIFT;
		size = pmem_ranges[range].pages << PAGE_SHIFT;
686
		end_paddr = start_paddr + size;
L
Linus Torvalds 已提交
687 688

		map_pages((unsigned long)__va(start_paddr), start_paddr,
689
			  size, PAGE_KERNEL, 0);
L
Linus Torvalds 已提交
690 691 692 693
	}

#ifdef CONFIG_BLK_DEV_INITRD
	if (initrd_end && initrd_end > mem_limit) {
694
		printk(KERN_INFO "initrd: mapping %08lx-%08lx\n", initrd_start, initrd_end);
L
Linus Torvalds 已提交
695
		map_pages(initrd_start, __pa(initrd_start),
696
			  initrd_end - initrd_start, PAGE_KERNEL, 0);
L
Linus Torvalds 已提交
697 698 699
	}
#endif

700
	empty_zero_page = get_memblock(PAGE_SIZE);
L
Linus Torvalds 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
}

static void __init gateway_init(void)
{
	unsigned long linux_gateway_page_addr;
	/* FIXME: This is 'const' in order to trick the compiler
	   into not treating it as DP-relative data. */
	extern void * const linux_gateway_page;

	linux_gateway_page_addr = LINUX_GATEWAY_ADDR & PAGE_MASK;

	/*
	 * Setup Linux Gateway page.
	 *
	 * The Linux gateway page will reside in kernel space (on virtual
	 * page 0), so it doesn't need to be aliased into user space.
	 */

	map_pages(linux_gateway_page_addr, __pa(&linux_gateway_page),
720
		  PAGE_SIZE, PAGE_GATEWAY, 1);
L
Linus Torvalds 已提交
721 722 723 724 725 726 727 728 729 730
}

void __init paging_init(void)
{
	int i;

	setup_bootmem();
	pagetable_init();
	gateway_init();
	flush_cache_all_local(); /* start with known state */
731
	flush_tlb_all_local(NULL);
L
Linus Torvalds 已提交
732 733

	for (i = 0; i < npmem_ranges; i++) {
734
		unsigned long zones_size[MAX_NR_ZONES] = { 0, };
L
Linus Torvalds 已提交
735

736
		zones_size[ZONE_NORMAL] = pmem_ranges[i].pages;
L
Linus Torvalds 已提交
737 738 739 740 741 742 743 744 745 746 747 748 749 750

#ifdef CONFIG_DISCONTIGMEM
		/* Need to initialize the pfnnid_map before we can initialize
		   the zone */
		{
		    int j;
		    for (j = (pmem_ranges[i].start_pfn >> PFNNID_SHIFT);
			 j <= ((pmem_ranges[i].start_pfn + pmem_ranges[i].pages) >> PFNNID_SHIFT);
			 j++) {
			pfnnid_map[j] = i;
		    }
		}
#endif

751
		free_area_init_node(i, zones_size,
L
Linus Torvalds 已提交
752 753 754 755 756 757 758
				pmem_ranges[i].start_pfn, NULL);
	}
}

#ifdef CONFIG_PA20

/*
759
 * Currently, all PA20 chips have 18 bit protection IDs, which is the
L
Linus Torvalds 已提交
760 761 762 763 764 765 766 767
 * limiting factor (space ids are 32 bits).
 */

#define NR_SPACE_IDS 262144

#else

/*
768 769 770 771
 * Currently we have a one-to-one relationship between space IDs and
 * protection IDs. Older parisc chips (PCXS, PCXT, PCXL, PCXL2) only
 * support 15 bit protection IDs, so that is the limiting factor.
 * PCXT' has 18 bit protection IDs, but only 16 bit spaceids, so it's
L
Linus Torvalds 已提交
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
 * probably not worth the effort for a special case here.
 */

#define NR_SPACE_IDS 32768

#endif  /* !CONFIG_PA20 */

#define RECYCLE_THRESHOLD (NR_SPACE_IDS / 2)
#define SID_ARRAY_SIZE  (NR_SPACE_IDS / (8 * sizeof(long)))

static unsigned long space_id[SID_ARRAY_SIZE] = { 1 }; /* disallow space 0 */
static unsigned long dirty_space_id[SID_ARRAY_SIZE];
static unsigned long space_id_index;
static unsigned long free_space_ids = NR_SPACE_IDS - 1;
static unsigned long dirty_space_ids = 0;

static DEFINE_SPINLOCK(sid_lock);

unsigned long alloc_sid(void)
{
	unsigned long index;

	spin_lock(&sid_lock);

	if (free_space_ids == 0) {
		if (dirty_space_ids != 0) {
			spin_unlock(&sid_lock);
			flush_tlb_all(); /* flush_tlb_all() calls recycle_sids() */
			spin_lock(&sid_lock);
		}
802
		BUG_ON(free_space_ids == 0);
L
Linus Torvalds 已提交
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
	}

	free_space_ids--;

	index = find_next_zero_bit(space_id, NR_SPACE_IDS, space_id_index);
	space_id[index >> SHIFT_PER_LONG] |= (1L << (index & (BITS_PER_LONG - 1)));
	space_id_index = index;

	spin_unlock(&sid_lock);

	return index << SPACEID_SHIFT;
}

void free_sid(unsigned long spaceid)
{
	unsigned long index = spaceid >> SPACEID_SHIFT;
	unsigned long *dirty_space_offset;

	dirty_space_offset = dirty_space_id + (index >> SHIFT_PER_LONG);
	index &= (BITS_PER_LONG - 1);

	spin_lock(&sid_lock);

826
	BUG_ON(*dirty_space_offset & (1L << index)); /* attempt to free space id twice */
L
Linus Torvalds 已提交
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900

	*dirty_space_offset |= (1L << index);
	dirty_space_ids++;

	spin_unlock(&sid_lock);
}


#ifdef CONFIG_SMP
static void get_dirty_sids(unsigned long *ndirtyptr,unsigned long *dirty_array)
{
	int i;

	/* NOTE: sid_lock must be held upon entry */

	*ndirtyptr = dirty_space_ids;
	if (dirty_space_ids != 0) {
	    for (i = 0; i < SID_ARRAY_SIZE; i++) {
		dirty_array[i] = dirty_space_id[i];
		dirty_space_id[i] = 0;
	    }
	    dirty_space_ids = 0;
	}

	return;
}

static void recycle_sids(unsigned long ndirty,unsigned long *dirty_array)
{
	int i;

	/* NOTE: sid_lock must be held upon entry */

	if (ndirty != 0) {
		for (i = 0; i < SID_ARRAY_SIZE; i++) {
			space_id[i] ^= dirty_array[i];
		}

		free_space_ids += ndirty;
		space_id_index = 0;
	}
}

#else /* CONFIG_SMP */

static void recycle_sids(void)
{
	int i;

	/* NOTE: sid_lock must be held upon entry */

	if (dirty_space_ids != 0) {
		for (i = 0; i < SID_ARRAY_SIZE; i++) {
			space_id[i] ^= dirty_space_id[i];
			dirty_space_id[i] = 0;
		}

		free_space_ids += dirty_space_ids;
		dirty_space_ids = 0;
		space_id_index = 0;
	}
}
#endif

/*
 * flush_tlb_all() calls recycle_sids(), since whenever the entire tlb is
 * purged, we can safely reuse the space ids that were released but
 * not flushed from the tlb.
 */

#ifdef CONFIG_SMP

static unsigned long recycle_ndirty;
static unsigned long recycle_dirty_array[SID_ARRAY_SIZE];
901
static unsigned int recycle_inuse;
L
Linus Torvalds 已提交
902 903 904 905 906

void flush_tlb_all(void)
{
	int do_recycle;

907
	__inc_irq_stat(irq_tlb_count);
L
Linus Torvalds 已提交
908 909 910
	do_recycle = 0;
	spin_lock(&sid_lock);
	if (dirty_space_ids > RECYCLE_THRESHOLD) {
911
	    BUG_ON(recycle_inuse);  /* FIXME: Use a semaphore/wait queue here */
L
Linus Torvalds 已提交
912 913 914 915 916
	    get_dirty_sids(&recycle_ndirty,recycle_dirty_array);
	    recycle_inuse++;
	    do_recycle++;
	}
	spin_unlock(&sid_lock);
917
	on_each_cpu(flush_tlb_all_local, NULL, 1);
L
Linus Torvalds 已提交
918 919 920 921 922 923 924 925 926 927
	if (do_recycle) {
	    spin_lock(&sid_lock);
	    recycle_sids(recycle_ndirty,recycle_dirty_array);
	    recycle_inuse = 0;
	    spin_unlock(&sid_lock);
	}
}
#else
void flush_tlb_all(void)
{
928
	__inc_irq_stat(irq_tlb_count);
L
Linus Torvalds 已提交
929
	spin_lock(&sid_lock);
930
	flush_tlb_all_local(NULL);
L
Linus Torvalds 已提交
931 932 933 934 935 936 937 938
	recycle_sids();
	spin_unlock(&sid_lock);
}
#endif

#ifdef CONFIG_BLK_DEV_INITRD
void free_initrd_mem(unsigned long start, unsigned long end)
{
939
	free_reserved_area((void *)start, (void *)end, -1, "initrd");
L
Linus Torvalds 已提交
940 941
}
#endif