patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / ia64 / mm / init.c
1 /*
2  * Initialize MMU support.
3  *
4  * Copyright (C) 1998-2003 Hewlett-Packard Co
5  *      David Mosberger-Tang <davidm@hpl.hp.com>
6  */
7 #include <linux/config.h>
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10
11 #include <linux/bootmem.h>
12 #include <linux/efi.h>
13 #include <linux/elf.h>
14 #include <linux/mm.h>
15 #include <linux/mmzone.h>
16 #include <linux/module.h>
17 #include <linux/personality.h>
18 #include <linux/reboot.h>
19 #include <linux/slab.h>
20 #include <linux/swap.h>
21 #include <linux/proc_fs.h>
22
23 #include <asm/a.out.h>
24 #include <asm/bitops.h>
25 #include <asm/dma.h>
26 #include <asm/ia32.h>
27 #include <asm/io.h>
28 #include <asm/machvec.h>
29 #include <asm/numa.h>
30 #include <asm/patch.h>
31 #include <asm/pgalloc.h>
32 #include <asm/sal.h>
33 #include <asm/sections.h>
34 #include <asm/system.h>
35 #include <asm/tlb.h>
36 #include <asm/uaccess.h>
37 #include <asm/unistd.h>
38 #include <asm/mca.h>
39
40 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
41
42 extern void ia64_tlb_init (void);
43
44 unsigned long MAX_DMA_ADDRESS = PAGE_OFFSET + 0x100000000UL;
45
46 #ifdef CONFIG_VIRTUAL_MEM_MAP
47 unsigned long vmalloc_end = VMALLOC_END_INIT;
48 EXPORT_SYMBOL(vmalloc_end);
49 struct page *vmem_map;
50 EXPORT_SYMBOL(vmem_map);
51 #endif
52
53 static int pgt_cache_water[2] = { 25, 50 };
54
55 struct page *zero_page_memmap_ptr;              /* map entry for zero page */
56 EXPORT_SYMBOL(zero_page_memmap_ptr);
57
58 void
59 check_pgt_cache (void)
60 {
61         int low, high;
62
63         low = pgt_cache_water[0];
64         high = pgt_cache_water[1];
65
66         if (pgtable_cache_size > (u64) high) {
67                 do {
68                         if (pgd_quicklist)
69                                 free_page((unsigned long)pgd_alloc_one_fast(0));
70                         if (pmd_quicklist)
71                                 free_page((unsigned long)pmd_alloc_one_fast(0, 0));
72                 } while (pgtable_cache_size > (u64) low);
73         }
74 }
75
76 void
77 update_mmu_cache (struct vm_area_struct *vma, unsigned long vaddr, pte_t pte)
78 {
79         unsigned long addr;
80         struct page *page;
81
82         if (!pte_exec(pte))
83                 return;                         /* not an executable page... */
84
85         page = pte_page(pte);
86         /* don't use VADDR: it may not be mapped on this CPU (or may have just been flushed): */
87         addr = (unsigned long) page_address(page);
88
89         if (test_bit(PG_arch_1, &page->flags))
90                 return;                         /* i-cache is already coherent with d-cache */
91
92         flush_icache_range(addr, addr + PAGE_SIZE);
93         set_bit(PG_arch_1, &page->flags);       /* mark page as clean */
94 }
95
96 inline void
97 ia64_set_rbs_bot (void)
98 {
99         unsigned long stack_size = current->rlim[RLIMIT_STACK].rlim_max & -16;
100
101         if (stack_size > MAX_USER_STACK_SIZE)
102                 stack_size = MAX_USER_STACK_SIZE;
103         current->thread.rbs_bot = STACK_TOP - stack_size;
104 }
105
106 /*
107  * This performs some platform-dependent address space initialization.
108  * On IA-64, we want to setup the VM area for the register backing
109  * store (which grows upwards) and install the gateway page which is
110  * used for signal trampolines, etc.
111  */
112 void
113 ia64_init_addr_space (void)
114 {
115         struct vm_area_struct *vma;
116
117         ia64_set_rbs_bot();
118
119         /*
120          * If we're out of memory and kmem_cache_alloc() returns NULL, we simply ignore
121          * the problem.  When the process attempts to write to the register backing store
122          * for the first time, it will get a SEGFAULT in this case.
123          */
124         vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
125         if (vma) {
126                 memset(vma, 0, sizeof(*vma));
127                 vma->vm_mm = current->mm;
128                 vma->vm_start = current->thread.rbs_bot & PAGE_MASK;
129                 vma->vm_end = vma->vm_start + PAGE_SIZE;
130                 vma->vm_page_prot = protection_map[VM_DATA_DEFAULT_FLAGS & 0x7];
131                 vma->vm_flags = VM_READ|VM_WRITE|VM_MAYREAD|VM_MAYWRITE|VM_GROWSUP;
132                 insert_vm_struct(current->mm, vma);
133         }
134
135         /* map NaT-page at address zero to speed up speculative dereferencing of NULL: */
136         if (!(current->personality & MMAP_PAGE_ZERO)) {
137                 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
138                 if (vma) {
139                         memset(vma, 0, sizeof(*vma));
140                         vma->vm_mm = current->mm;
141                         vma->vm_end = PAGE_SIZE;
142                         vma->vm_page_prot = __pgprot(pgprot_val(PAGE_READONLY) | _PAGE_MA_NAT);
143                         vma->vm_flags = VM_READ | VM_MAYREAD | VM_IO | VM_RESERVED;
144                         insert_vm_struct(current->mm, vma);
145                 }
146         }
147 }
148
149 void
150 free_initmem (void)
151 {
152         unsigned long addr, eaddr;
153
154         addr = (unsigned long) ia64_imva(__init_begin);
155         eaddr = (unsigned long) ia64_imva(__init_end);
156         while (addr < eaddr) {
157                 ClearPageReserved(virt_to_page(addr));
158                 set_page_count(virt_to_page(addr), 1);
159                 free_page(addr);
160                 ++totalram_pages;
161                 addr += PAGE_SIZE;
162         }
163         printk(KERN_INFO "Freeing unused kernel memory: %ldkB freed\n",
164                (__init_end - __init_begin) >> 10);
165 }
166
167 void
168 free_initrd_mem (unsigned long start, unsigned long end)
169 {
170         struct page *page;
171         /*
172          * EFI uses 4KB pages while the kernel can use 4KB  or bigger.
173          * Thus EFI and the kernel may have different page sizes. It is
174          * therefore possible to have the initrd share the same page as
175          * the end of the kernel (given current setup).
176          *
177          * To avoid freeing/using the wrong page (kernel sized) we:
178          *      - align up the beginning of initrd
179          *      - align down the end of initrd
180          *
181          *  |             |
182          *  |=============| a000
183          *  |             |
184          *  |             |
185          *  |             | 9000
186          *  |/////////////|
187          *  |/////////////|
188          *  |=============| 8000
189          *  |///INITRD////|
190          *  |/////////////|
191          *  |/////////////| 7000
192          *  |             |
193          *  |KKKKKKKKKKKKK|
194          *  |=============| 6000
195          *  |KKKKKKKKKKKKK|
196          *  |KKKKKKKKKKKKK|
197          *  K=kernel using 8KB pages
198          *
199          * In this example, we must free page 8000 ONLY. So we must align up
200          * initrd_start and keep initrd_end as is.
201          */
202         start = PAGE_ALIGN(start);
203         end = end & PAGE_MASK;
204
205         if (start < end)
206                 printk(KERN_INFO "Freeing initrd memory: %ldkB freed\n", (end - start) >> 10);
207
208         for (; start < end; start += PAGE_SIZE) {
209                 if (!virt_addr_valid(start))
210                         continue;
211                 page = virt_to_page(start);
212                 ClearPageReserved(page);
213                 set_page_count(page, 1);
214                 free_page(start);
215                 ++totalram_pages;
216         }
217 }
218
219 /*
220  * This installs a clean page in the kernel's page table.
221  */
222 struct page *
223 put_kernel_page (struct page *page, unsigned long address, pgprot_t pgprot)
224 {
225         pgd_t *pgd;
226         pmd_t *pmd;
227         pte_t *pte;
228
229         if (!PageReserved(page))
230                 printk(KERN_ERR "put_kernel_page: page at 0x%p not in reserved memory\n",
231                        page_address(page));
232
233         pgd = pgd_offset_k(address);            /* note: this is NOT pgd_offset()! */
234
235         spin_lock(&init_mm.page_table_lock);
236         {
237                 pmd = pmd_alloc(&init_mm, pgd, address);
238                 if (!pmd)
239                         goto out;
240                 pte = pte_alloc_map(&init_mm, pmd, address);
241                 if (!pte)
242                         goto out;
243                 if (!pte_none(*pte)) {
244                         pte_unmap(pte);
245                         goto out;
246                 }
247                 set_pte(pte, mk_pte(page, pgprot));
248                 pte_unmap(pte);
249         }
250   out:  spin_unlock(&init_mm.page_table_lock);
251         /* no need for flush_tlb */
252         return page;
253 }
254
255 static void
256 setup_gate (void)
257 {
258         struct page *page;
259
260         /*
261          * Map the gate page twice: once read-only to export the ELF headers etc. and once
262          * execute-only page to enable privilege-promotion via "epc":
263          */
264         page = virt_to_page(ia64_imva(__start_gate_section));
265         put_kernel_page(page, GATE_ADDR, PAGE_READONLY);
266 #ifdef HAVE_BUGGY_SEGREL
267         page = virt_to_page(ia64_imva(__start_gate_section + PAGE_SIZE));
268         put_kernel_page(page, GATE_ADDR + PAGE_SIZE, PAGE_GATE);
269 #else
270         put_kernel_page(page, GATE_ADDR + PERCPU_PAGE_SIZE, PAGE_GATE);
271 #endif
272         ia64_patch_gate();
273 }
274
275 void __devinit
276 ia64_mmu_init (void *my_cpu_data)
277 {
278         unsigned long psr, pta, impl_va_bits;
279         extern void __devinit tlb_init (void);
280         int cpu;
281
282 #ifdef CONFIG_DISABLE_VHPT
283 #       define VHPT_ENABLE_BIT  0
284 #else
285 #       define VHPT_ENABLE_BIT  1
286 #endif
287
288         /* Pin mapping for percpu area into TLB */
289         psr = ia64_clear_ic();
290         ia64_itr(0x2, IA64_TR_PERCPU_DATA, PERCPU_ADDR,
291                  pte_val(pfn_pte(__pa(my_cpu_data) >> PAGE_SHIFT, PAGE_KERNEL)),
292                  PERCPU_PAGE_SHIFT);
293
294         ia64_set_psr(psr);
295         ia64_srlz_i();
296
297         /*
298          * Check if the virtually mapped linear page table (VMLPT) overlaps with a mapped
299          * address space.  The IA-64 architecture guarantees that at least 50 bits of
300          * virtual address space are implemented but if we pick a large enough page size
301          * (e.g., 64KB), the mapped address space is big enough that it will overlap with
302          * VMLPT.  I assume that once we run on machines big enough to warrant 64KB pages,
303          * IMPL_VA_MSB will be significantly bigger, so this is unlikely to become a
304          * problem in practice.  Alternatively, we could truncate the top of the mapped
305          * address space to not permit mappings that would overlap with the VMLPT.
306          * --davidm 00/12/06
307          */
308 #       define pte_bits                 3
309 #       define mapped_space_bits        (3*(PAGE_SHIFT - pte_bits) + PAGE_SHIFT)
310         /*
311          * The virtual page table has to cover the entire implemented address space within
312          * a region even though not all of this space may be mappable.  The reason for
313          * this is that the Access bit and Dirty bit fault handlers perform
314          * non-speculative accesses to the virtual page table, so the address range of the
315          * virtual page table itself needs to be covered by virtual page table.
316          */
317 #       define vmlpt_bits               (impl_va_bits - PAGE_SHIFT + pte_bits)
318 #       define POW2(n)                  (1ULL << (n))
319
320         impl_va_bits = ffz(~(local_cpu_data->unimpl_va_mask | (7UL << 61)));
321
322         if (impl_va_bits < 51 || impl_va_bits > 61)
323                 panic("CPU has bogus IMPL_VA_MSB value of %lu!\n", impl_va_bits - 1);
324
325         /* place the VMLPT at the end of each page-table mapped region: */
326         pta = POW2(61) - POW2(vmlpt_bits);
327
328         if (POW2(mapped_space_bits) >= pta)
329                 panic("mm/init: overlap between virtually mapped linear page table and "
330                       "mapped kernel space!");
331         /*
332          * Set the (virtually mapped linear) page table address.  Bit
333          * 8 selects between the short and long format, bits 2-7 the
334          * size of the table, and bit 0 whether the VHPT walker is
335          * enabled.
336          */
337         ia64_set_pta(pta | (0 << 8) | (vmlpt_bits << 2) | VHPT_ENABLE_BIT);
338
339         ia64_tlb_init();
340
341 #ifdef  CONFIG_HUGETLB_PAGE
342         ia64_set_rr(HPAGE_REGION_BASE, HPAGE_SHIFT << 2);
343         ia64_srlz_d();
344 #endif
345
346         cpu = smp_processor_id();
347
348         /* mca handler uses cr.lid as key to pick the right entry */
349         ia64_mca_tlb_list[cpu].cr_lid = ia64_getreg(_IA64_REG_CR_LID);
350
351         /* insert this percpu data information into our list for MCA recovery purposes */
352         ia64_mca_tlb_list[cpu].percpu_paddr = pte_val(mk_pte_phys(__pa(my_cpu_data), PAGE_KERNEL));
353         /* Also save per-cpu tlb flush recipe for use in physical mode mca handler */
354         ia64_mca_tlb_list[cpu].ptce_base = local_cpu_data->ptce_base;
355         ia64_mca_tlb_list[cpu].ptce_count[0] = local_cpu_data->ptce_count[0];
356         ia64_mca_tlb_list[cpu].ptce_count[1] = local_cpu_data->ptce_count[1];
357         ia64_mca_tlb_list[cpu].ptce_stride[0] = local_cpu_data->ptce_stride[0];
358         ia64_mca_tlb_list[cpu].ptce_stride[1] = local_cpu_data->ptce_stride[1];
359 }
360
361 #ifdef CONFIG_VIRTUAL_MEM_MAP
362
363 int
364 create_mem_map_page_table (u64 start, u64 end, void *arg)
365 {
366         unsigned long address, start_page, end_page;
367         struct page *map_start, *map_end;
368         int node;
369         pgd_t *pgd;
370         pmd_t *pmd;
371         pte_t *pte;
372
373         map_start = vmem_map + (__pa(start) >> PAGE_SHIFT);
374         map_end   = vmem_map + (__pa(end) >> PAGE_SHIFT);
375
376         start_page = (unsigned long) map_start & PAGE_MASK;
377         end_page = PAGE_ALIGN((unsigned long) map_end);
378         node = paddr_to_nid(__pa(start));
379
380         for (address = start_page; address < end_page; address += PAGE_SIZE) {
381                 pgd = pgd_offset_k(address);
382                 if (pgd_none(*pgd))
383                         pgd_populate(&init_mm, pgd, alloc_bootmem_pages_node(NODE_DATA(node), PAGE_SIZE));
384                 pmd = pmd_offset(pgd, address);
385
386                 if (pmd_none(*pmd))
387                         pmd_populate_kernel(&init_mm, pmd, alloc_bootmem_pages_node(NODE_DATA(node), PAGE_SIZE));
388                 pte = pte_offset_kernel(pmd, address);
389
390                 if (pte_none(*pte))
391                         set_pte(pte, pfn_pte(__pa(alloc_bootmem_pages_node(NODE_DATA(node), PAGE_SIZE)) >> PAGE_SHIFT,
392                                              PAGE_KERNEL));
393         }
394         return 0;
395 }
396
397 struct memmap_init_callback_data {
398         struct page *start;
399         struct page *end;
400         int nid;
401         unsigned long zone;
402 };
403
404 static int
405 virtual_memmap_init (u64 start, u64 end, void *arg)
406 {
407         struct memmap_init_callback_data *args;
408         struct page *map_start, *map_end;
409
410         args = (struct memmap_init_callback_data *) arg;
411
412         map_start = vmem_map + (__pa(start) >> PAGE_SHIFT);
413         map_end   = vmem_map + (__pa(end) >> PAGE_SHIFT);
414
415         if (map_start < args->start)
416                 map_start = args->start;
417         if (map_end > args->end)
418                 map_end = args->end;
419
420         /*
421          * We have to initialize "out of bounds" struct page elements that fit completely
422          * on the same pages that were allocated for the "in bounds" elements because they
423          * may be referenced later (and found to be "reserved").
424          */
425         map_start -= ((unsigned long) map_start & (PAGE_SIZE - 1)) / sizeof(struct page);
426         map_end += ((PAGE_ALIGN((unsigned long) map_end) - (unsigned long) map_end)
427                     / sizeof(struct page));
428
429         if (map_start < map_end)
430                 memmap_init_zone(map_start, (unsigned long) (map_end - map_start),
431                                  args->nid, args->zone, page_to_pfn(map_start));
432         return 0;
433 }
434
435 void
436 memmap_init (struct page *start, unsigned long size, int nid,
437              unsigned long zone, unsigned long start_pfn)
438 {
439         if (!vmem_map)
440                 memmap_init_zone(start, size, nid, zone, start_pfn);
441         else {
442                 struct memmap_init_callback_data args;
443
444                 args.start = start;
445                 args.end = start + size;
446                 args.nid = nid;
447                 args.zone = zone;
448
449                 efi_memmap_walk(virtual_memmap_init, &args);
450         }
451 }
452
453 int
454 ia64_pfn_valid (unsigned long pfn)
455 {
456         char byte;
457         struct page *pg = pfn_to_page(pfn);
458
459         return     (__get_user(byte, (char *) pg) == 0)
460                 && ((((u64)pg & PAGE_MASK) == (((u64)(pg + 1) - 1) & PAGE_MASK))
461                         || (__get_user(byte, (char *) (pg + 1) - 1) == 0));
462 }
463 EXPORT_SYMBOL(ia64_pfn_valid);
464
465 int
466 find_largest_hole (u64 start, u64 end, void *arg)
467 {
468         u64 *max_gap = arg;
469
470         static u64 last_end = PAGE_OFFSET;
471
472         /* NOTE: this algorithm assumes efi memmap table is ordered */
473
474         if (*max_gap < (start - last_end))
475                 *max_gap = start - last_end;
476         last_end = end;
477         return 0;
478 }
479 #endif /* CONFIG_VIRTUAL_MEM_MAP */
480
481 static int
482 count_reserved_pages (u64 start, u64 end, void *arg)
483 {
484         unsigned long num_reserved = 0;
485         unsigned long *count = arg;
486
487         for (; start < end; start += PAGE_SIZE)
488                 if (PageReserved(virt_to_page(start)))
489                         ++num_reserved;
490         *count += num_reserved;
491         return 0;
492 }
493
494 /*
495  * Boot command-line option "nolwsys" can be used to disable the use of any light-weight
496  * system call handler.  When this option is in effect, all fsyscalls will end up bubbling
497  * down into the kernel and calling the normal (heavy-weight) syscall handler.  This is
498  * useful for performance testing, but conceivably could also come in handy for debugging
499  * purposes.
500  */
501
502 static int nolwsys;
503
504 static int __init
505 nolwsys_setup (char *s)
506 {
507         nolwsys = 1;
508         return 1;
509 }
510
511 __setup("nolwsys", nolwsys_setup);
512
513 void
514 mem_init (void)
515 {
516         long reserved_pages, codesize, datasize, initsize;
517         unsigned long num_pgt_pages;
518         pg_data_t *pgdat;
519         int i;
520         static struct kcore_list kcore_mem, kcore_vmem, kcore_kernel;
521
522 #ifdef CONFIG_PCI
523         /*
524          * This needs to be called _after_ the command line has been parsed but _before_
525          * any drivers that may need the PCI DMA interface are initialized or bootmem has
526          * been freed.
527          */
528         platform_dma_init();
529 #endif
530
531 #ifndef CONFIG_DISCONTIGMEM
532         if (!mem_map)
533                 BUG();
534         max_mapnr = max_low_pfn;
535 #endif
536
537         high_memory = __va(max_low_pfn * PAGE_SIZE);
538
539         kclist_add(&kcore_mem, __va(0), max_low_pfn * PAGE_SIZE);
540         kclist_add(&kcore_vmem, (void *)VMALLOC_START, VMALLOC_END-VMALLOC_START);
541         kclist_add(&kcore_kernel, _stext, _end - _stext);
542
543         for_each_pgdat(pgdat)
544                 totalram_pages += free_all_bootmem_node(pgdat);
545
546         reserved_pages = 0;
547         efi_memmap_walk(count_reserved_pages, &reserved_pages);
548
549         codesize =  (unsigned long) _etext - (unsigned long) _stext;
550         datasize =  (unsigned long) _edata - (unsigned long) _etext;
551         initsize =  (unsigned long) __init_end - (unsigned long) __init_begin;
552
553         printk(KERN_INFO "Memory: %luk/%luk available (%luk code, %luk reserved, "
554                "%luk data, %luk init)\n", (unsigned long) nr_free_pages() << (PAGE_SHIFT - 10),
555                num_physpages << (PAGE_SHIFT - 10), codesize >> 10,
556                reserved_pages << (PAGE_SHIFT - 10), datasize >> 10, initsize >> 10);
557
558         /*
559          * Allow for enough (cached) page table pages so that we can map the entire memory
560          * at least once.  Each task also needs a couple of page tables pages, so add in a
561          * fudge factor for that (don't use "threads-max" here; that would be wrong!).
562          * Don't allow the cache to be more than 10% of total memory, though.
563          */
564 #       define NUM_TASKS        500     /* typical number of tasks */
565         num_pgt_pages = nr_free_pages() / PTRS_PER_PGD + NUM_TASKS;
566         if (num_pgt_pages > nr_free_pages() / 10)
567                 num_pgt_pages = nr_free_pages() / 10;
568         if (num_pgt_pages > (u64) pgt_cache_water[1])
569                 pgt_cache_water[1] = num_pgt_pages;
570
571         /*
572          * For fsyscall entrpoints with no light-weight handler, use the ordinary
573          * (heavy-weight) handler, but mark it by setting bit 0, so the fsyscall entry
574          * code can tell them apart.
575          */
576         for (i = 0; i < NR_syscalls; ++i) {
577                 extern unsigned long fsyscall_table[NR_syscalls];
578                 extern unsigned long sys_call_table[NR_syscalls];
579
580                 if (!fsyscall_table[i] || nolwsys)
581                         fsyscall_table[i] = sys_call_table[i] | 1;
582         }
583         setup_gate();   /* setup gate pages before we free up boot memory... */
584
585 #ifdef CONFIG_IA32_SUPPORT
586         ia32_boot_gdt_init();
587 #endif
588 }