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