1 /*
2  *  linux/arch/parisc/mm/init.c
3  *
4  *  Copyright (C) 1995	Linus Torvalds
5  *  Copyright 1999 SuSE GmbH
6  *    changed by Philipp Rumpf
7  *  Copyright 1999 Philipp Rumpf (prumpf@tux.org)
8  *
9  */
10 
11 #include <linux/config.h>
12 
13 #include <linux/mm.h>
14 #include <linux/bootmem.h>
15 #include <linux/delay.h>
16 #include <linux/init.h>
17 #include <linux/pci.h>		/* for hppa_dma_ops and pcxl_dma_ops */
18 #include <linux/blk.h>          /* for initrd_start and initrd_end */
19 #include <linux/swap.h>
20 #include <linux/unistd.h>
21 
22 #include <asm/pgalloc.h>
23 #include <asm/tlb.h>
24 #include <asm/pdc_chassis.h>
25 
26 mmu_gather_t mmu_gathers[NR_CPUS];
27 
28 extern char _text;	/* start of kernel code, defined by linker */
29 extern int  data_start;
30 extern char _end;	/* end of BSS, defined by linker */
31 extern char __init_begin, __init_end;
32 
33 #ifdef CONFIG_DISCONTIGMEM
34 struct node_map_data node_data[MAX_PHYSMEM_RANGES];
35 bootmem_data_t bmem_data[MAX_PHYSMEM_RANGES];
36 unsigned char *chunkmap;
37 unsigned int maxchunkmap;
38 #endif
39 
40 static struct resource data_resource = {
41 	name:	"Kernel data",
42 	flags:	IORESOURCE_BUSY | IORESOURCE_MEM,
43 };
44 
45 static struct resource code_resource = {
46 	name:	"Kernel code",
47 	flags:	IORESOURCE_BUSY | IORESOURCE_MEM,
48 };
49 
50 static struct resource pdcdata_resource = {
51 	name:	"PDC data (Page Zero)",
52 	start:	0,
53 	end:	0x9ff,
54 	flags:	IORESOURCE_BUSY | IORESOURCE_MEM,
55 };
56 
57 static struct resource sysram_resources[MAX_PHYSMEM_RANGES];
58 
59 static unsigned long max_pfn;
60 
61 /* The following array is initialized from the firmware specific
62  * information retrieved in kernel/inventory.c.
63  */
64 
65 physmem_range_t pmem_ranges[MAX_PHYSMEM_RANGES];
66 int npmem_ranges;
67 
68 #ifdef __LP64__
69 #define MAX_MEM         (~0UL)
70 #else /* !__LP64__ */
71 #define MAX_MEM         (3584U*1024U*1024U)
72 #endif /* !__LP64__ */
73 
74 static unsigned long mem_limit = MAX_MEM;
75 
mem_limit_func(void)76 static void __init mem_limit_func(void)
77 {
78 	char *cp, *end;
79 	unsigned long limit;
80 	extern char saved_command_line[];
81 
82 	/* We need this before __setup() functions are called */
83 
84 	limit = MAX_MEM;
85 	for (cp = saved_command_line; *cp; ) {
86 		if (memcmp(cp, "mem=", 4) == 0) {
87 			cp += 4;
88 			limit = memparse(cp, &end);
89 			if (end != cp)
90 				break;
91 			cp = end;
92 		} else {
93 			while (*cp != ' ' && *cp)
94 				++cp;
95 			while (*cp == ' ')
96 				++cp;
97 		}
98 	}
99 
100 	if (limit < mem_limit)
101 		mem_limit = limit;
102 }
103 
104 #define MAX_GAP (0x40000000UL >> PAGE_SHIFT)
105 
setup_bootmem(void)106 static void __init setup_bootmem(void)
107 {
108 	unsigned long bootmap_size;
109 	unsigned long mem_max;
110 	unsigned long bootmap_pages;
111 	unsigned long bootmap_start_pfn;
112 	unsigned long bootmap_pfn;
113 #ifndef CONFIG_DISCONTIGMEM
114 	physmem_range_t pmem_holes[MAX_PHYSMEM_RANGES - 1];
115 	int npmem_holes;
116 #endif
117 	int i, sysram_resource_count;
118 
119 	disable_sr_hashing(); /* Turn off space register hashing */
120 
121 #ifdef CONFIG_DISCONTIGMEM
122 	/*
123 	 * The below is still true as of 2.4.2. If this is ever fixed,
124 	 * we can remove this warning!
125 	 */
126 
127 	printk(KERN_WARNING "\n\n");
128 	printk(KERN_WARNING "CONFIG_DISCONTIGMEM is enabled, which is probably a mistake. This\n");
129 	printk(KERN_WARNING "option can lead to heavy swapping, even when there are gigabytes\n");
130 	printk(KERN_WARNING "of free memory.\n\n");
131 #endif
132 
133 #ifdef __LP64__
134 
135 #ifndef CONFIG_DISCONTIGMEM
136 	/*
137 	 * Sort the ranges. Since the number of ranges is typically
138 	 * small, and performance is not an issue here, just do
139 	 * a simple insertion sort.
140 	 */
141 
142 	for (i = 1; i < npmem_ranges; i++) {
143 		int j;
144 
145 		for (j = i; j > 0; j--) {
146 			unsigned long tmp;
147 
148 			if (pmem_ranges[j-1].start_pfn <
149 			    pmem_ranges[j].start_pfn) {
150 
151 				break;
152 			}
153 			tmp = pmem_ranges[j-1].start_pfn;
154 			pmem_ranges[j-1].start_pfn = pmem_ranges[j].start_pfn;
155 			pmem_ranges[j].start_pfn = tmp;
156 			tmp = pmem_ranges[j-1].pages;
157 			pmem_ranges[j-1].pages = pmem_ranges[j].pages;
158 			pmem_ranges[j].pages = tmp;
159 		}
160 	}
161 
162 	/*
163 	 * Throw out ranges that are too far apart (controlled by
164 	 * MAX_GAP). If CONFIG_DISCONTIGMEM wasn't implemented so
165 	 * poorly, we would recommend enabling that option, but,
166 	 * until it is fixed, this is the best way to go.
167 	 */
168 
169 	for (i = 1; i < npmem_ranges; i++) {
170 		if (pmem_ranges[i].start_pfn -
171 			(pmem_ranges[i-1].start_pfn +
172 			 pmem_ranges[i-1].pages) > MAX_GAP) {
173 			npmem_ranges = i;
174 			break;
175 		}
176 	}
177 #endif
178 
179 	if (npmem_ranges > 1) {
180 
181 		/* Print the memory ranges */
182 
183 		printk(KERN_INFO "Memory Ranges:\n");
184 
185 		for (i = 0; i < npmem_ranges; i++) {
186 			unsigned long start;
187 			unsigned long size;
188 
189 			size = (pmem_ranges[i].pages << PAGE_SHIFT);
190 			start = (pmem_ranges[i].start_pfn << PAGE_SHIFT);
191 			printk(KERN_INFO "%2d) Start 0x%016lx End 0x%016lx Size %6ld Mb\n",
192 				i,start, start + (size - 1), size >> 20);
193 		}
194 	}
195 
196 #endif /* __LP64__ */
197 
198 #if 1
199 	/* KLUGE! this really belongs in kernel/resource.c! */
200 	iomem_resource.end = ~0UL;
201 #endif
202 
203 	sysram_resource_count = npmem_ranges;
204 	for (i = 0; i < sysram_resource_count; i++) {
205 		struct resource *res = &sysram_resources[i];
206 		res->name = "System RAM";
207 		res->start = pmem_ranges[i].start_pfn << PAGE_SHIFT;
208 		res->end = res->start + (pmem_ranges[i].pages << PAGE_SHIFT)-1;
209 		res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
210 		request_resource(&iomem_resource, res);
211 	}
212 
213 	/*
214 	 * For 32 bit kernels we limit the amount of memory we can
215 	 * support, in order to preserve enough kernel address space
216 	 * for other purposes. For 64 bit kernels we don't normally
217 	 * limit the memory, but this mechanism can be used to
218 	 * artificially limit the amount of memory (and it is written
219 	 * to work with multiple memory ranges).
220 	 */
221 
222 	mem_limit_func();       /* check for "mem=" argument */
223 
224 	mem_max = 0;
225 	for (i = 0; i < npmem_ranges; i++) {
226 		unsigned long rsize;
227 
228 		rsize = pmem_ranges[i].pages << PAGE_SHIFT;
229 		if ((mem_max + rsize) > mem_limit) {
230 			printk(KERN_WARNING "Memory truncated to %ld Mb\n", mem_limit >> 20);
231 			if (mem_max == mem_limit)
232 				npmem_ranges = i;
233 			else {
234 				pmem_ranges[i].pages =   (mem_limit >> PAGE_SHIFT)
235 						       - (mem_max >> PAGE_SHIFT);
236 				npmem_ranges = i + 1;
237 				mem_max = mem_limit;
238 			}
239 			break;
240 		}
241 		mem_max += rsize;
242 	}
243 
244 	printk(KERN_INFO "Total Memory: %ld Mb\n",mem_max >> 20);
245 
246 #ifndef CONFIG_DISCONTIGMEM
247 
248 	/* Merge the ranges, keeping track of the holes */
249 
250 	{
251 		unsigned long end_pfn;
252 		unsigned long hole_pages;
253 
254 		npmem_holes = 0;
255 		end_pfn = pmem_ranges[0].start_pfn + pmem_ranges[0].pages;
256 		for (i = 1; i < npmem_ranges; i++) {
257 
258 			hole_pages = pmem_ranges[i].start_pfn - end_pfn;
259 			if (hole_pages) {
260 				pmem_holes[npmem_holes].start_pfn = end_pfn;
261 				pmem_holes[npmem_holes++].pages = hole_pages;
262 				end_pfn += hole_pages;
263 			}
264 			end_pfn += pmem_ranges[i].pages;
265 		}
266 
267 		pmem_ranges[0].pages = end_pfn - pmem_ranges[0].start_pfn;
268 		npmem_ranges = 1;
269 	}
270 #endif
271 
272 	bootmap_pages = 0;
273 	for (i = 0; i < npmem_ranges; i++)
274 		bootmap_pages += bootmem_bootmap_pages(pmem_ranges[i].pages);
275 
276 	bootmap_start_pfn = PAGE_ALIGN(__pa((unsigned long) &_end)) >> PAGE_SHIFT;
277 
278 #ifdef CONFIG_DISCONTIGMEM
279 	for (i = 0; i < npmem_ranges; i++)
280 		node_data[i].pg_data.bdata = &bmem_data[i];
281 #endif
282 	/*
283 	 * Initialize and free the full range of memory in each range.
284 	 * Note that the only writing these routines do are to the bootmap,
285 	 * and we've made sure to locate the bootmap properly so that they
286 	 * won't be writing over anything important.
287 	 */
288 
289 	bootmap_pfn = bootmap_start_pfn;
290 	max_pfn = 0;
291 	for (i = 0; i < npmem_ranges; i++) {
292 		unsigned long start_pfn;
293 		unsigned long npages;
294 
295 		start_pfn = pmem_ranges[i].start_pfn;
296 		npages = pmem_ranges[i].pages;
297 
298 		bootmap_size = init_bootmem_node(NODE_DATA(i),
299 						bootmap_pfn,
300 						start_pfn,
301 						(start_pfn + npages) );
302 		free_bootmem_node(NODE_DATA(i),
303 				  (start_pfn << PAGE_SHIFT),
304 				  (npages << PAGE_SHIFT) );
305 		bootmap_pfn += (bootmap_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
306 		if ((start_pfn + npages) > max_pfn)
307 			max_pfn = start_pfn + npages;
308 	}
309 
310 	if ((bootmap_pfn - bootmap_start_pfn) != bootmap_pages) {
311 		printk(KERN_WARNING "WARNING! bootmap sizing is messed up!\n");
312 		BUG();
313 	}
314 
315 	/* reserve PAGE0 pdc memory, kernel text/data/bss & bootmap */
316 
317 #define PDC_CONSOLE_IO_IODC_SIZE 32768
318 
319 	reserve_bootmem_node(NODE_DATA(0), 0UL,
320 			(unsigned long)(PAGE0->mem_free + PDC_CONSOLE_IO_IODC_SIZE));
321 	reserve_bootmem_node(NODE_DATA(0),__pa((unsigned long)&_text),
322 			(unsigned long)(&_end - &_text));
323 	reserve_bootmem_node(NODE_DATA(0), (bootmap_start_pfn << PAGE_SHIFT),
324 			((bootmap_pfn - bootmap_start_pfn) << PAGE_SHIFT));
325 
326 #ifndef CONFIG_DISCONTIGMEM
327 
328 	/* reserve the holes */
329 
330 	for (i = 0; i < npmem_holes; i++) {
331 		reserve_bootmem_node(NODE_DATA(0),
332 				(pmem_holes[i].start_pfn << PAGE_SHIFT),
333 				(pmem_holes[i].pages << PAGE_SHIFT));
334 	}
335 #endif
336 
337 #ifdef CONFIG_BLK_DEV_INITRD
338 	if (initrd_start) {
339 		printk(KERN_INFO "initrd: %08lx-%08lx\n", initrd_start, initrd_end);
340 		if (__pa(initrd_start) < mem_max) {
341 			unsigned long initrd_reserve;
342 
343 			if (__pa(initrd_end) > mem_max) {
344 				initrd_reserve = mem_max - __pa(initrd_start);
345 			} else {
346 				initrd_reserve = initrd_end - initrd_start;
347 			}
348 			initrd_below_start_ok = 1;
349 			printk(KERN_INFO "initrd: reserving %08lx-%08lx (mem_max %08lx)\n", __pa(initrd_start), __pa(initrd_start) + initrd_reserve, mem_max);
350 
351 			reserve_bootmem_node(NODE_DATA(0),__pa(initrd_start), initrd_reserve);
352 		}
353 	}
354 #endif
355 
356 	data_resource.start =  virt_to_phys(&data_start);
357 	data_resource.end = virt_to_phys(&_end)-1;
358 	code_resource.start = virt_to_phys(&_text);
359 	code_resource.end = virt_to_phys(&data_start)-1;
360 
361 	/* We don't know which region the kernel will be in, so try
362 	 * all of them.
363 	 */
364 	for (i = 0; i < sysram_resource_count; i++) {
365 		struct resource *res = &sysram_resources[i];
366 		request_resource(res, &code_resource);
367 		request_resource(res, &data_resource);
368 	}
369 	request_resource(&sysram_resources[0], &pdcdata_resource);
370 }
371 
free_initmem(void)372 void free_initmem(void)
373 {
374 	/* FIXME: */
375 #if 0
376 	printk(KERN_INFO "NOT FREEING INITMEM (%dk)\n",
377 			(&__init_end - &__init_begin) >> 10);
378 	return;
379 #endif
380 	unsigned long addr;
381 
382 	printk(KERN_INFO "Freeing unused kernel memory: ");
383 
384 #if 1
385 	/* Attempt to catch anyone trying to execute code here
386 	 * by filling the page with BRK insns.
387 	 *
388 	 * If we disable interrupts for all CPUs, then IPI stops working.
389 	 * Kinda breaks the global cache flushing.
390 	 */
391 	local_irq_disable();
392 
393 	memset(&__init_begin, 0x00,
394 		(unsigned long)&__init_end - (unsigned long)&__init_begin);
395 
396 	flush_data_cache();
397 	asm volatile("sync" : : );
398 	flush_icache_range((unsigned long)&__init_begin, (unsigned long)&__init_end);
399 	asm volatile("sync" : : );
400 
401 	local_irq_enable();
402 #endif
403 
404 	addr = (unsigned long)(&__init_begin);
405 	for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
406 		ClearPageReserved(virt_to_page(addr));
407 		set_page_count(virt_to_page(addr), 1);
408 		free_page(addr);
409 		num_physpages++;
410 	}
411 
412 	printk("%luk freed\n", (unsigned long)(&__init_end - &__init_begin) >> 10);
413 
414 	/* set up a new led state on systems shipped LED State panel */
415 	pdc_chassis_send_status(PDC_CHASSIS_DIRECT_BCOMPLETE);
416 }
417 
418 /*
419  * Just an arbitrary offset to serve as a "hole" between mapping areas
420  * (between top of physical memory and a potential pcxl dma mapping
421  * area, and below the vmalloc mapping area).
422  *
423  * The current 32K value just means that there will be a 32K "hole"
424  * between mapping areas. That means that  any out-of-bounds memory
425  * accesses will hopefully be caught. The vmalloc() routines leaves
426  * a hole of 4kB between each vmalloced area for the same reason.
427  */
428 
429 #define MAP_START 0x4000 /* Leave room for gateway page expansion */
430 #define VM_MAP_OFFSET  (32*1024)
431 #define SET_MAP_OFFSET(x) ((void *)(((unsigned long)(x) + VM_MAP_OFFSET) \
432 				     & ~(VM_MAP_OFFSET-1)))
433 
434 void *vmalloc_start;
435 #ifdef CONFIG_PA11
436 unsigned long pcxl_dma_start;
437 #endif
438 
mem_init(void)439 void __init mem_init(void)
440 {
441 	int i;
442 
443 	high_memory = __va((max_pfn << PAGE_SHIFT));
444 	max_mapnr = (virt_to_page(high_memory - 1) - mem_map) + 1;
445 
446 	num_physpages = 0;
447 	for (i = 0; i < npmem_ranges; i++)
448 		num_physpages += free_all_bootmem_node(NODE_DATA(i));
449 
450 	printk(KERN_INFO "Memory: %luk available\n", num_physpages << (PAGE_SHIFT-10));
451 
452 #ifdef CONFIG_PA11
453 	if (hppa_dma_ops == &pcxl_dma_ops) {
454 	    pcxl_dma_start = (unsigned long)SET_MAP_OFFSET(MAP_START);
455 	    vmalloc_start = SET_MAP_OFFSET(pcxl_dma_start + PCXL_DMA_MAP_SIZE);
456 	}
457 	else {
458 	    pcxl_dma_start = 0;
459 	    vmalloc_start = SET_MAP_OFFSET(MAP_START);
460 	}
461 #else
462 	vmalloc_start = SET_MAP_OFFSET(MAP_START);
463 #endif
464 
465 }
466 
do_check_pgt_cache(int low,int high)467 int do_check_pgt_cache(int low, int high)
468 {
469 	return 0;
470 }
471 
472 unsigned long *empty_zero_page;
473 
show_mem(void)474 void show_mem(void)
475 {
476 	int i,free = 0,total = 0,reserved = 0;
477 	int shared = 0, cached = 0;
478 
479 	printk(KERN_INFO "Mem-info:\n");
480 	show_free_areas();
481 	printk(KERN_INFO "Free swap:	 %6dkB\n",nr_swap_pages<<(PAGE_SHIFT-10));
482 	i = max_mapnr;
483 	while (i-- > 0) {
484 		total++;
485 		if (PageReserved(mem_map+i))
486 			reserved++;
487 		else if (PageSwapCache(mem_map+i))
488 			cached++;
489 		else if (!atomic_read(&mem_map[i].count))
490 			free++;
491 		else
492 			shared += atomic_read(&mem_map[i].count) - 1;
493 	}
494 	printk(KERN_INFO "%d pages of RAM\n", total);
495 	printk(KERN_INFO "%d reserved pages\n", reserved);
496 	printk(KERN_INFO "%d pages shared\n", shared);
497 	printk(KERN_INFO "%d pages swap cached\n", cached);
498 	show_buffers();
499 }
500 
501 
map_pages(unsigned long start_vaddr,unsigned long start_paddr,unsigned long size,pgprot_t pgprot)502 static void __init map_pages(unsigned long start_vaddr, unsigned long start_paddr, unsigned long size, pgprot_t pgprot)
503 {
504 	pgd_t *pg_dir;
505 	pmd_t *pmd;
506 	pte_t *pg_table;
507 	unsigned long end_paddr;
508 	unsigned long start_pmd;
509 	unsigned long start_pte;
510 	unsigned long tmp1;
511 	unsigned long tmp2;
512 	unsigned long address;
513 	unsigned long ro_start;
514 	unsigned long ro_end;
515 	unsigned long fv_addr;
516 	unsigned long gw_addr;
517 	extern const unsigned long fault_vector_20;
518 	extern void * const linux_gateway_page;
519 
520 	ro_start = __pa((unsigned long)&_text);
521 	ro_end   = __pa((unsigned long)&data_start);
522 	fv_addr  = __pa((unsigned long)&fault_vector_20) & PAGE_MASK;
523 	gw_addr  = __pa((unsigned long)&linux_gateway_page) & PAGE_MASK;
524 
525 	end_paddr = start_paddr + size;
526 
527 	pg_dir = pgd_offset_k(start_vaddr);
528 
529 #if PTRS_PER_PMD == 1
530 	start_pmd = 0;
531 #else
532 	start_pmd = ((start_vaddr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
533 #endif
534 	start_pte = ((start_vaddr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
535 
536 	address = start_paddr;
537 	while (address < end_paddr) {
538 #if PTRS_PER_PMD == 1
539 		pmd = (pmd_t *)__pa(pg_dir);
540 #else
541 		pmd = (pmd_t *) (PAGE_MASK & pgd_val(*pg_dir));
542 
543 		/*
544 		 * pmd is physical at this point
545 		 */
546 
547 		if (!pmd) {
548 			pmd = (pmd_t *) alloc_bootmem_low_pages_node(NODE_DATA(0),PAGE_SIZE);
549 			pmd = (pmd_t *) __pa(pmd);
550 		}
551 
552 		pgd_val(*pg_dir) = _PAGE_TABLE | (unsigned long) pmd;
553 #endif
554 		pg_dir++;
555 
556 		/* now change pmd to kernel virtual addresses */
557 
558 		pmd = (pmd_t *)__va(pmd) + start_pmd;
559 		for (tmp1 = start_pmd; tmp1 < PTRS_PER_PMD; tmp1++,pmd++) {
560 
561 			/*
562 			 * pg_table is physical at this point
563 			 */
564 
565 			pg_table = (pte_t *) (PAGE_MASK & pmd_val(*pmd));
566 			if (!pg_table) {
567 				pg_table = (pte_t *)
568 					alloc_bootmem_low_pages_node(NODE_DATA(0),PAGE_SIZE);
569 				pg_table = (pte_t *) __pa(pg_table);
570 			}
571 
572 			pmd_val(*pmd) = _PAGE_TABLE |
573 					   (unsigned long) pg_table;
574 
575 			/* now change pg_table to kernel virtual addresses */
576 
577 			pg_table = (pte_t *) __va(pg_table) + start_pte;
578 			for (tmp2 = start_pte; tmp2 < PTRS_PER_PTE; tmp2++,pg_table++) {
579 				pte_t pte;
580 
581 #if !defined(CONFIG_STI_CONSOLE)
582 #warning STI console should explicitly allocate executable pages but does not
583 				/*
584 				 * Map the fault vector writable so we can
585 				 * write the HPMC checksum.
586 				 */
587 				if (address >= ro_start && address < ro_end
588 							&& address != fv_addr
589 							&& address != gw_addr)
590 				    pte = __mk_pte(address, PAGE_KERNEL_RO);
591 				else
592 #endif
593 				    pte = __mk_pte(address, pgprot);
594 
595 				if (address >= end_paddr)
596 					pte_val(pte) = 0;
597 
598 				set_pte(pg_table, pte);
599 
600 				address += PAGE_SIZE;
601 			}
602 			start_pte = 0;
603 
604 			if (address >= end_paddr)
605 			    break;
606 		}
607 		start_pmd = 0;
608 	}
609 }
610 
611 /*
612  * pagetable_init() sets up the page tables
613  *
614  * Note that gateway_init() places the Linux gateway page at page 0.
615  * Since gateway pages cannot be dereferenced this has the desirable
616  * side effect of trapping those pesky NULL-reference errors in the
617  * kernel.
618  */
pagetable_init(void)619 static void __init pagetable_init(void)
620 {
621 	int range;
622 
623 	printk("pagetable_init\n");
624 
625 	/* Map each physical memory range to its kernel vaddr */
626 
627 	for (range = 0; range < npmem_ranges; range++) {
628 		unsigned long start_paddr;
629 		unsigned long end_paddr;
630 		unsigned long size;
631 
632 		start_paddr = pmem_ranges[range].start_pfn << PAGE_SHIFT;
633 		end_paddr = start_paddr + (pmem_ranges[range].pages << PAGE_SHIFT);
634 		size = pmem_ranges[range].pages << PAGE_SHIFT;
635 
636 		map_pages((unsigned long)__va(start_paddr), start_paddr,
637 			size, PAGE_KERNEL);
638 	}
639 
640 #ifdef CONFIG_BLK_DEV_INITRD
641 	if (initrd_end && initrd_end > mem_limit) {
642 		printk("initrd: mapping %08lx-%08lx\n", initrd_start, initrd_end);
643 		map_pages(initrd_start, __pa(initrd_start),
644 			initrd_end - initrd_start, PAGE_KERNEL);
645 	}
646 #endif
647 
648 	empty_zero_page = alloc_bootmem_pages(PAGE_SIZE);
649 	memset(empty_zero_page, 0, PAGE_SIZE);
650 }
651 
gateway_init(void)652 static void __init gateway_init(void)
653 {
654 	unsigned long linux_gateway_page_addr;
655 	/* FIXME: This is 'const' in order to trick the compiler
656 	   into not treating it as DP-relative data. */
657 	extern void * const linux_gateway_page;
658 
659 	linux_gateway_page_addr = LINUX_GATEWAY_ADDR & PAGE_MASK;
660 
661 	/*
662 	 * Setup Linux Gateway page.
663 	 *
664 	 * The Linux gateway page will reside in kernel space (on virtual
665 	 * page 0), so it doesn't need to be aliased into user space.
666 	 */
667 
668 	map_pages(linux_gateway_page_addr, __pa(&linux_gateway_page),
669 		PAGE_SIZE, PAGE_GATEWAY);
670 }
671 
672 void
map_hpux_gateway_page(struct task_struct * tsk,struct mm_struct * mm)673 map_hpux_gateway_page(struct task_struct *tsk, struct mm_struct *mm)
674 {
675 	pgd_t *pg_dir;
676 	pmd_t *pmd;
677 	pte_t *pg_table;
678 	unsigned long start_pmd;
679 	unsigned long start_pte;
680 	unsigned long address;
681 	unsigned long hpux_gw_page_addr;
682 	/* FIXME: This is 'const' in order to trick the compiler
683 	   into not treating it as DP-relative data. */
684 	extern void * const hpux_gateway_page;
685 
686 	hpux_gw_page_addr = HPUX_GATEWAY_ADDR & PAGE_MASK;
687 
688 	/*
689 	 * Setup HP-UX Gateway page.
690 	 *
691 	 * The HP-UX gateway page resides in the user address space,
692 	 * so it needs to be aliased into each process.
693 	 */
694 
695 	pg_dir = pgd_offset(mm,hpux_gw_page_addr);
696 
697 #if PTRS_PER_PMD == 1
698 	start_pmd = 0;
699 #else
700 	start_pmd = ((hpux_gw_page_addr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
701 #endif
702 	start_pte = ((hpux_gw_page_addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
703 
704 	address = __pa(&hpux_gateway_page);
705 #if PTRS_PER_PMD == 1
706 	pmd = (pmd_t *)__pa(pg_dir);
707 #else
708 	pmd = (pmd_t *) (PAGE_MASK & pgd_val(*pg_dir));
709 
710 	/*
711 	 * pmd is physical at this point
712 	 */
713 
714 	if (!pmd) {
715 		pmd = (pmd_t *) get_zeroed_page(GFP_KERNEL);
716 		pmd = (pmd_t *) __pa(pmd);
717 	}
718 
719 	pgd_val(*pg_dir) = _PAGE_TABLE | (unsigned long) pmd;
720 #endif
721 	/* now change pmd to kernel virtual addresses */
722 
723 	pmd = (pmd_t *)__va(pmd) + start_pmd;
724 
725 	/*
726 	 * pg_table is physical at this point
727 	 */
728 
729 	pg_table = (pte_t *) (PAGE_MASK & pmd_val(*pmd));
730 	if (!pg_table)
731 		pg_table = (pte_t *) __pa(get_zeroed_page(GFP_KERNEL));
732 
733 	pmd_val(*pmd) = _PAGE_TABLE | (unsigned long) pg_table;
734 
735 	/* now change pg_table to kernel virtual addresses */
736 
737 	pg_table = (pte_t *) __va(pg_table) + start_pte;
738 	set_pte(pg_table, __mk_pte(address, PAGE_GATEWAY));
739 }
740 
741 extern void flush_tlb_all_local(void);
742 
paging_init(void)743 void __init paging_init(void)
744 {
745 	int i;
746 
747 	setup_bootmem();
748 	pagetable_init();
749 	gateway_init();
750 	flush_cache_all_local(); /* start with known state */
751 	flush_tlb_all_local();
752 
753 	for (i = 0; i < npmem_ranges; i++) {
754 		unsigned long zones_size[MAX_NR_ZONES] = { 0, 0, 0, };
755 
756 		zones_size[ZONE_DMA] = pmem_ranges[i].pages;
757 		free_area_init_node(i,NODE_DATA(i),NULL,zones_size,
758 				(pmem_ranges[i].start_pfn << PAGE_SHIFT),0);
759 	}
760 
761 #ifdef CONFIG_DISCONTIGMEM
762 	/*
763 	 * Initialize support for virt_to_page() macro.
764 	 *
765 	 * Note that MAX_ADDRESS is the largest virtual address that
766 	 * we can map. However, since we map all physical memory into
767 	 * the kernel address space, it also has an effect on the maximum
768 	 * physical address we can map (MAX_ADDRESS - PAGE_OFFSET).
769 	 */
770 
771 	maxchunkmap = MAX_ADDRESS >> CHUNKSHIFT;
772 	chunkmap = (unsigned char *)alloc_bootmem(maxchunkmap);
773 
774 	for (i = 0; i < maxchunkmap; i++)
775 	    chunkmap[i] = BADCHUNK;
776 
777 	for (i = 0; i < npmem_ranges; i++) {
778 
779 		ADJ_NODE_MEM_MAP(i) = NODE_MEM_MAP(i) - pmem_ranges[i].start_pfn;
780 		{
781 			unsigned long chunk_paddr;
782 			unsigned long end_paddr;
783 			int chunknum;
784 
785 			chunk_paddr = (pmem_ranges[i].start_pfn << PAGE_SHIFT);
786 			end_paddr = chunk_paddr + (pmem_ranges[i].pages << PAGE_SHIFT);
787 			chunk_paddr &= CHUNKMASK;
788 
789 			chunknum = (int)CHUNKNUM(chunk_paddr);
790 			while (chunk_paddr < end_paddr) {
791 				if (chunknum >= maxchunkmap)
792 					goto badchunkmap1;
793 				if (chunkmap[chunknum] != BADCHUNK)
794 					goto badchunkmap2;
795 				chunkmap[chunknum] = (unsigned char)i;
796 				chunk_paddr += CHUNKSZ;
797 				chunknum++;
798 			}
799 		}
800 	}
801 
802 	return;
803 
804 badchunkmap1:
805 	panic("paging_init: Physical address exceeds maximum address space!\n");
806 badchunkmap2:
807 	panic("paging_init: Collision in chunk map array. CHUNKSZ needs to be smaller\n");
808 #endif
809 }
810 
811 #ifdef CONFIG_PA20
812 
813 /*
814  * Currently, all PA20 chips have 18 bit protection id's, which is the
815  * limiting factor (space ids are 32 bits).
816  */
817 
818 #define NR_SPACE_IDS 262144
819 
820 #else
821 
822 /*
823  * Currently we have a one-to-one relationship between space id's and
824  * protection id's. Older parisc chips (PCXS, PCXT, PCXL, PCXL2) only
825  * support 15 bit protection id's, so that is the limiting factor.
826  * PCXT' has 18 bit protection id's, but only 16 bit spaceids, so it's
827  * probably not worth the effort for a special case here.
828  */
829 
830 #define NR_SPACE_IDS 32768
831 
832 #endif  /* !CONFIG_PA20 */
833 
834 #define RECYCLE_THRESHOLD (NR_SPACE_IDS / 2)
835 #define SID_ARRAY_SIZE  (NR_SPACE_IDS / (8 * sizeof(long)))
836 
837 static unsigned long space_id[SID_ARRAY_SIZE] = { 1 }; /* disallow space 0 */
838 static unsigned long dirty_space_id[SID_ARRAY_SIZE];
839 static unsigned long space_id_index;
840 static unsigned long free_space_ids = NR_SPACE_IDS - 1;
841 static unsigned long dirty_space_ids = 0;
842 
843 static spinlock_t sid_lock = SPIN_LOCK_UNLOCKED;
844 
alloc_sid(void)845 unsigned long alloc_sid(void)
846 {
847 	unsigned long index;
848 
849 	spin_lock(&sid_lock);
850 
851 	if (free_space_ids == 0) {
852 		if (dirty_space_ids != 0) {
853 			spin_unlock(&sid_lock);
854 			flush_tlb_all(); /* flush_tlb_all() calls recycle_sids() */
855 			spin_lock(&sid_lock);
856 		}
857 		if (free_space_ids == 0)
858 			BUG();
859 	}
860 
861 	free_space_ids--;
862 
863 	index = find_next_zero_bit(space_id, NR_SPACE_IDS, space_id_index);
864 	space_id[index >> SHIFT_PER_LONG] |= (1L << (index & (BITS_PER_LONG - 1)));
865 	space_id_index = index;
866 
867 	spin_unlock(&sid_lock);
868 
869 	return index << SPACEID_SHIFT;
870 }
871 
free_sid(unsigned long spaceid)872 void free_sid(unsigned long spaceid)
873 {
874 	unsigned long index = spaceid >> SPACEID_SHIFT;
875 	unsigned long *dirty_space_offset;
876 
877 	dirty_space_offset = dirty_space_id + (index >> SHIFT_PER_LONG);
878 	index &= (BITS_PER_LONG - 1);
879 
880 	spin_lock(&sid_lock);
881 
882 	if (*dirty_space_offset & (1L << index))
883 	    BUG(); /* attempt to free space id twice */
884 
885 	*dirty_space_offset |= (1L << index);
886 	dirty_space_ids++;
887 
888 	spin_unlock(&sid_lock);
889 }
890 
891 
892 #ifdef CONFIG_SMP
get_dirty_sids(unsigned long * ndirtyptr,unsigned long * dirty_array)893 static void get_dirty_sids(unsigned long *ndirtyptr,unsigned long *dirty_array)
894 {
895 	int i;
896 
897 	/* NOTE: sid_lock must be held upon entry */
898 
899 	*ndirtyptr = dirty_space_ids;
900 	if (dirty_space_ids != 0) {
901 	    for (i = 0; i < SID_ARRAY_SIZE; i++) {
902 		dirty_array[i] = dirty_space_id[i];
903 		dirty_space_id[i] = 0;
904 	    }
905 	    dirty_space_ids = 0;
906 	}
907 
908 	return;
909 }
910 
recycle_sids(unsigned long ndirty,unsigned long * dirty_array)911 static void recycle_sids(unsigned long ndirty,unsigned long *dirty_array)
912 {
913 	int i;
914 
915 	/* NOTE: sid_lock must be held upon entry */
916 
917 	if (ndirty != 0) {
918 		for (i = 0; i < SID_ARRAY_SIZE; i++) {
919 			space_id[i] ^= dirty_array[i];
920 		}
921 
922 		free_space_ids += ndirty;
923 		space_id_index = 0;
924 	}
925 }
926 
927 #else /* CONFIG_SMP */
928 
recycle_sids(void)929 static void recycle_sids(void)
930 {
931 	int i;
932 
933 	/* NOTE: sid_lock must be held upon entry */
934 
935 	if (dirty_space_ids != 0) {
936 		for (i = 0; i < SID_ARRAY_SIZE; i++) {
937 			space_id[i] ^= dirty_space_id[i];
938 			dirty_space_id[i] = 0;
939 		}
940 
941 		free_space_ids += dirty_space_ids;
942 		dirty_space_ids = 0;
943 		space_id_index = 0;
944 	}
945 }
946 #endif
947 
948 /*
949  * flush_tlb_all() calls recycle_sids(), since whenever the entire tlb is
950  * purged, we can safely reuse the space ids that were released but
951  * not flushed from the tlb.
952  */
953 
954 #ifdef CONFIG_SMP
955 
956 static unsigned long recycle_ndirty;
957 static unsigned long recycle_dirty_array[SID_ARRAY_SIZE];
958 static unsigned int recycle_inuse = 0;
959 
flush_tlb_all(void)960 void flush_tlb_all(void)
961 {
962 	int do_recycle;
963 
964 	do_recycle = 0;
965 	spin_lock(&sid_lock);
966 	if (dirty_space_ids > RECYCLE_THRESHOLD) {
967 	    if (recycle_inuse) {
968 		BUG();  /* FIXME: Use a semaphore/wait queue here */
969 	    }
970 	    get_dirty_sids(&recycle_ndirty,recycle_dirty_array);
971 	    recycle_inuse++;
972 	    do_recycle++;
973 	}
974 	spin_unlock(&sid_lock);
975 	smp_call_function((void (*)(void *))flush_tlb_all_local, NULL, 1, 1);
976 	flush_tlb_all_local();
977 	if (do_recycle) {
978 	    spin_lock(&sid_lock);
979 	    recycle_sids(recycle_ndirty,recycle_dirty_array);
980 	    recycle_inuse = 0;
981 	    spin_unlock(&sid_lock);
982 	}
983 }
984 #else
flush_tlb_all(void)985 void flush_tlb_all(void)
986 {
987 	spin_lock(&sid_lock);
988 	flush_tlb_all_local();
989 	recycle_sids();
990 	spin_unlock(&sid_lock);
991 }
992 #endif
993 
994 #ifdef CONFIG_BLK_DEV_INITRD
free_initrd_mem(unsigned long start,unsigned long end)995 void free_initrd_mem(unsigned long start, unsigned long end)
996 {
997 #if 0
998 	if (start < end)
999 		printk(KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1000 	for (; start < end; start += PAGE_SIZE) {
1001 		ClearPageReserved(virt_to_page(start));
1002 		set_page_count(virt_to_page(start), 1);
1003 		free_page(start);
1004 		num_physpages++;
1005 	}
1006 #endif
1007 }
1008 #endif
1009 
si_meminfo(struct sysinfo * val)1010 void si_meminfo(struct sysinfo *val)
1011 {
1012 	val->totalram = num_physpages;
1013 	val->sharedram = 0;
1014 	val->freeram = nr_free_pages();
1015 	val->bufferram = atomic_read(&buffermem_pages);
1016 	val->totalhigh = 0;
1017 	val->freehigh = 0;
1018 	val->mem_unit = PAGE_SIZE;
1019 	return;
1020 }
1021