0d38a16385ffb9503df10a02325db43deb0a30ad
[linux-flexiantxendom0-3.2.10.git] / arch / ia64 / mm / init.c
1 /*
2  * Initialize MMU support.
3  *
4  * Copyright (C) 1998-2002 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/mm.h>
13 #include <linux/personality.h>
14 #include <linux/reboot.h>
15 #include <linux/slab.h>
16 #include <linux/swap.h>
17 #include <linux/efi.h>
18 #include <linux/mmzone.h>
19
20 #include <asm/a.out.h>
21 #include <asm/bitops.h>
22 #include <asm/dma.h>
23 #include <asm/ia32.h>
24 #include <asm/io.h>
25 #include <asm/machvec.h>
26 #include <asm/pgalloc.h>
27 #include <asm/sal.h>
28 #include <asm/system.h>
29 #include <asm/uaccess.h>
30 #include <asm/tlb.h>
31
32 struct mmu_gather mmu_gathers[NR_CPUS];
33
34 /* References to section boundaries: */
35 extern char _stext, _etext, _edata, __init_begin, __init_end;
36
37 extern void ia64_tlb_init (void);
38
39 unsigned long MAX_DMA_ADDRESS = PAGE_OFFSET + 0x100000000UL;
40
41 static int pgt_cache_water[2] = { 25, 50 };
42
43 void
44 check_pgt_cache (void)
45 {
46         int low, high;
47
48         low = pgt_cache_water[0];
49         high = pgt_cache_water[1];
50
51         if (pgtable_cache_size > high) {
52                 do {
53                         if (pgd_quicklist)
54                                 free_page((unsigned long)pgd_alloc_one_fast(0));
55                         if (pmd_quicklist)
56                                 free_page((unsigned long)pmd_alloc_one_fast(0, 0));
57                 } while (pgtable_cache_size > low);
58         }
59 }
60
61 /*
62  * This performs some platform-dependent address space initialization.
63  * On IA-64, we want to setup the VM area for the register backing
64  * store (which grows upwards) and install the gateway page which is
65  * used for signal trampolines, etc.
66  */
67 void
68 ia64_init_addr_space (void)
69 {
70         struct vm_area_struct *vma;
71
72         /*
73          * If we're out of memory and kmem_cache_alloc() returns NULL, we simply ignore
74          * the problem.  When the process attempts to write to the register backing store
75          * for the first time, it will get a SEGFAULT in this case.
76          */
77         vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
78         if (vma) {
79                 vma->vm_mm = current->mm;
80                 vma->vm_start = IA64_RBS_BOT;
81                 vma->vm_end = vma->vm_start + PAGE_SIZE;
82                 vma->vm_page_prot = protection_map[VM_DATA_DEFAULT_FLAGS & 0x7];
83                 vma->vm_flags = VM_READ|VM_WRITE|VM_MAYREAD|VM_MAYWRITE|VM_GROWSUP;
84                 vma->vm_ops = NULL;
85                 vma->vm_pgoff = 0;
86                 vma->vm_file = NULL;
87                 vma->vm_private_data = NULL;
88                 insert_vm_struct(current->mm, vma);
89         }
90
91         /* map NaT-page at address zero to speed up speculative dereferencing of NULL: */
92         if (!(current->personality & MMAP_PAGE_ZERO)) {
93                 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
94                 if (vma) {
95                         memset(vma, 0, sizeof(*vma));
96                         vma->vm_mm = current->mm;
97                         vma->vm_end = PAGE_SIZE;
98                         vma->vm_page_prot = __pgprot(pgprot_val(PAGE_READONLY) | _PAGE_MA_NAT);
99                         vma->vm_flags = VM_READ | VM_MAYREAD | VM_IO | VM_RESERVED;
100                         insert_vm_struct(current->mm, vma);
101                 }
102         }
103 }
104
105 void
106 free_initmem (void)
107 {
108         unsigned long addr;
109
110         addr = (unsigned long) &__init_begin;
111         for (; addr < (unsigned long) &__init_end; addr += PAGE_SIZE) {
112                 ClearPageReserved(virt_to_page(addr));
113                 set_page_count(virt_to_page(addr), 1);
114                 free_page(addr);
115                 ++totalram_pages;
116         }
117         printk(KERN_INFO "Freeing unused kernel memory: %ldkB freed\n",
118                (&__init_end - &__init_begin) >> 10);
119 }
120
121 void
122 free_initrd_mem (unsigned long start, unsigned long end)
123 {
124         struct page *page;
125         /*
126          * EFI uses 4KB pages while the kernel can use 4KB  or bigger.
127          * Thus EFI and the kernel may have different page sizes. It is
128          * therefore possible to have the initrd share the same page as
129          * the end of the kernel (given current setup).
130          *
131          * To avoid freeing/using the wrong page (kernel sized) we:
132          *      - align up the beginning of initrd
133          *      - align down the end of initrd
134          *
135          *  |             |
136          *  |=============| a000
137          *  |             |
138          *  |             |
139          *  |             | 9000
140          *  |/////////////|
141          *  |/////////////|
142          *  |=============| 8000
143          *  |///INITRD////|
144          *  |/////////////|
145          *  |/////////////| 7000
146          *  |             |
147          *  |KKKKKKKKKKKKK|
148          *  |=============| 6000
149          *  |KKKKKKKKKKKKK|
150          *  |KKKKKKKKKKKKK|
151          *  K=kernel using 8KB pages
152          *
153          * In this example, we must free page 8000 ONLY. So we must align up
154          * initrd_start and keep initrd_end as is.
155          */
156         start = PAGE_ALIGN(start);
157         end = end & PAGE_MASK;
158
159         if (start < end)
160                 printk(KERN_INFO "Freeing initrd memory: %ldkB freed\n", (end - start) >> 10);
161
162         for (; start < end; start += PAGE_SIZE) {
163                 if (!virt_addr_valid(start))
164                         continue;
165                 page = virt_to_page(start);
166                 ClearPageReserved(page);
167                 set_page_count(page, 1);
168                 free_page(start);
169                 ++totalram_pages;
170         }
171 }
172
173 void
174 show_mem(void)
175 {
176         int i, total = 0, reserved = 0;
177         int shared = 0, cached = 0;
178
179         printk("Mem-info:\n");
180         show_free_areas();
181
182 #ifdef CONFIG_DISCONTIGMEM
183         {
184                 pg_data_t *pgdat;
185
186                 printk("Free swap:       %6dkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
187                 for_each_pgdat(pgdat) {
188                         printk("Node ID: %d\n", pgdat->node_id);
189                         for(i = 0; i < pgdat->node_size; i++) {
190                                 if (PageReserved(pgdat->node_mem_map+i))
191                                         reserved++;
192                                 else if (PageSwapCache(pgdat->node_mem_map+i))
193                                         cached++;
194                                 else if (page_count(pgdat->node_mem_map + i))
195                                         shared += page_count(pgdat->node_mem_map + i) - 1;
196                         }
197                         printk("\t%d pages of RAM\n", pgdat->node_size);
198                         printk("\t%d reserved pages\n", reserved);
199                         printk("\t%d pages shared\n", shared);
200                         printk("\t%d pages swap cached\n", cached);
201                 }
202                 printk("Total of %ld pages in page table cache\n", pgtable_cache_size);
203                 printk("%d free buffer pages\n", nr_free_buffer_pages());
204         }
205 #else /* !CONFIG_DISCONTIGMEM */
206         printk("Free swap:       %6dkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
207         i = max_mapnr;
208         while (i-- > 0) {
209                 total++;
210                 if (PageReserved(mem_map+i))
211                         reserved++;
212                 else if (PageSwapCache(mem_map+i))
213                         cached++;
214                 else if (page_count(mem_map + i))
215                         shared += page_count(mem_map + i) - 1;
216         }
217         printk("%d pages of RAM\n", total);
218         printk("%d reserved pages\n", reserved);
219         printk("%d pages shared\n", shared);
220         printk("%d pages swap cached\n", cached);
221         printk("%ld pages in page table cache\n", pgtable_cache_size);
222 #endif /* !CONFIG_DISCONTIGMEM */
223 }
224
225 /*
226  * This is like put_dirty_page() but installs a clean page with PAGE_GATE protection
227  * (execute-only, typically).
228  */
229 struct page *
230 put_gate_page (struct page *page, unsigned long address)
231 {
232         pgd_t *pgd;
233         pmd_t *pmd;
234         pte_t *pte;
235
236         if (!PageReserved(page))
237                 printk(KERN_ERR "put_gate_page: gate page at 0x%p not in reserved memory\n",
238                        page_address(page));
239
240         pgd = pgd_offset_k(address);            /* note: this is NOT pgd_offset()! */
241
242         spin_lock(&init_mm.page_table_lock);
243         {
244                 pmd = pmd_alloc(&init_mm, pgd, address);
245                 if (!pmd)
246                         goto out;
247                 pte = pte_alloc_map(&init_mm, pmd, address);
248                 if (!pte)
249                         goto out;
250                 if (!pte_none(*pte)) {
251                         pte_unmap(pte);
252                         goto out;
253                 }
254                 set_pte(pte, mk_pte(page, PAGE_GATE));
255                 pte_unmap(pte);
256         }
257   out:  spin_unlock(&init_mm.page_table_lock);
258         /* no need for flush_tlb */
259         return page;
260 }
261
262 void __init
263 ia64_mmu_init (void *my_cpu_data)
264 {
265         unsigned long psr, rid, pta, impl_va_bits;
266         extern void __init tlb_init (void);
267 #ifdef CONFIG_DISABLE_VHPT
268 #       define VHPT_ENABLE_BIT  0
269 #else
270 #       define VHPT_ENABLE_BIT  1
271 #endif
272
273         /*
274          * Set up the kernel identity mapping for regions 6 and 5.  The mapping for region
275          * 7 is setup up in _start().
276          */
277         psr = ia64_clear_ic();
278
279         rid = ia64_rid(IA64_REGION_ID_KERNEL, __IA64_UNCACHED_OFFSET);
280         ia64_set_rr(__IA64_UNCACHED_OFFSET, (rid << 8) | (IA64_GRANULE_SHIFT << 2));
281
282         rid = ia64_rid(IA64_REGION_ID_KERNEL, VMALLOC_START);
283         ia64_set_rr(VMALLOC_START, (rid << 8) | (PAGE_SHIFT << 2) | 1);
284
285         /* ensure rr6 is up-to-date before inserting the PERCPU_ADDR translation: */
286         ia64_srlz_d();
287
288         ia64_itr(0x2, IA64_TR_PERCPU_DATA, PERCPU_ADDR,
289                  pte_val(pfn_pte(__pa(my_cpu_data) >> PAGE_SHIFT, PAGE_KERNEL)),
290                  PERCPU_PAGE_SHIFT);
291
292         ia64_set_psr(psr);
293         ia64_srlz_i();
294
295         /*
296          * Check if the virtually mapped linear page table (VMLPT) overlaps with a mapped
297          * address space.  The IA-64 architecture guarantees that at least 50 bits of
298          * virtual address space are implemented but if we pick a large enough page size
299          * (e.g., 64KB), the mapped address space is big enough that it will overlap with
300          * VMLPT.  I assume that once we run on machines big enough to warrant 64KB pages,
301          * IMPL_VA_MSB will be significantly bigger, so this is unlikely to become a
302          * problem in practice.  Alternatively, we could truncate the top of the mapped
303          * address space to not permit mappings that would overlap with the VMLPT.
304          * --davidm 00/12/06
305          */
306 #       define pte_bits                 3
307 #       define mapped_space_bits        (3*(PAGE_SHIFT - pte_bits) + PAGE_SHIFT)
308         /*
309          * The virtual page table has to cover the entire implemented address space within
310          * a region even though not all of this space may be mappable.  The reason for
311          * this is that the Access bit and Dirty bit fault handlers perform
312          * non-speculative accesses to the virtual page table, so the address range of the
313          * virtual page table itself needs to be covered by virtual page table.
314          */
315 #       define vmlpt_bits               (impl_va_bits - PAGE_SHIFT + pte_bits)
316 #       define POW2(n)                  (1ULL << (n))
317
318         impl_va_bits = ffz(~(local_cpu_data->unimpl_va_mask | (7UL << 61)));
319
320         if (impl_va_bits < 51 || impl_va_bits > 61)
321                 panic("CPU has bogus IMPL_VA_MSB value of %lu!\n", impl_va_bits - 1);
322
323         /* place the VMLPT at the end of each page-table mapped region: */
324         pta = POW2(61) - POW2(vmlpt_bits);
325
326         if (POW2(mapped_space_bits) >= pta)
327                 panic("mm/init: overlap between virtually mapped linear page table and "
328                       "mapped kernel space!");
329         /*
330          * Set the (virtually mapped linear) page table address.  Bit
331          * 8 selects between the short and long format, bits 2-7 the
332          * size of the table, and bit 0 whether the VHPT walker is
333          * enabled.
334          */
335         ia64_set_pta(pta | (0 << 8) | (vmlpt_bits << 2) | VHPT_ENABLE_BIT);
336
337         ia64_tlb_init();
338 }
339
340 /*
341  * Set up the page tables.
342  */
343
344 #ifdef CONFIG_DISCONTIGMEM
345 void
346 paging_init (void)
347 {
348         extern void discontig_paging_init(void);
349
350         discontig_paging_init();
351 }
352 #else /* !CONFIG_DISCONTIGMEM */
353 void
354 paging_init (void)
355 {
356         unsigned long max_dma, zones_size[MAX_NR_ZONES];
357
358         /* initialize mem_map[] */
359
360         memset(zones_size, 0, sizeof(zones_size));
361
362         max_dma = virt_to_phys((void *) MAX_DMA_ADDRESS) >> PAGE_SHIFT;
363         if (max_low_pfn < max_dma)
364                 zones_size[ZONE_DMA] = max_low_pfn;
365         else {
366                 zones_size[ZONE_DMA] = max_dma;
367                 zones_size[ZONE_NORMAL] = max_low_pfn - max_dma;
368         }
369         free_area_init(zones_size);
370 }
371 #endif /* !CONFIG_DISCONTIGMEM */
372
373 static int
374 count_pages (u64 start, u64 end, void *arg)
375 {
376         unsigned long *count = arg;
377
378         *count += (end - start) >> PAGE_SHIFT;
379         return 0;
380 }
381
382 static int
383 count_reserved_pages (u64 start, u64 end, void *arg)
384 {
385         unsigned long num_reserved = 0;
386         unsigned long *count = arg;
387
388         for (; start < end; start += PAGE_SIZE)
389                 if (PageReserved(virt_to_page(start)))
390                         ++num_reserved;
391         *count += num_reserved;
392         return 0;
393 }
394
395 void
396 mem_init (void)
397 {
398         extern char __start_gate_section[];
399         long reserved_pages, codesize, datasize, initsize;
400         unsigned long num_pgt_pages;
401         pg_data_t *pgdat;
402
403 #ifdef CONFIG_PCI
404         /*
405          * This needs to be called _after_ the command line has been parsed but _before_
406          * any drivers that may need the PCI DMA interface are initialized or bootmem has
407          * been freed.
408          */
409         platform_pci_dma_init();
410 #endif
411
412 #ifndef CONFIG_DISCONTIGMEM
413         if (!mem_map)
414                 BUG();
415         max_mapnr = max_low_pfn;
416 #endif
417
418         num_physpages = 0;
419         efi_memmap_walk(count_pages, &num_physpages);
420
421         high_memory = __va(max_low_pfn * PAGE_SIZE);
422
423         for_each_pgdat(pgdat)
424                 totalram_pages += free_all_bootmem_node(pgdat);
425
426         reserved_pages = 0;
427         efi_memmap_walk(count_reserved_pages, &reserved_pages);
428
429         codesize =  (unsigned long) &_etext - (unsigned long) &_stext;
430         datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
431         initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
432
433         printk(KERN_INFO "Memory: %luk/%luk available (%luk code, %luk reserved, "
434                "%luk data, %luk init)\n", (unsigned long) nr_free_pages() << (PAGE_SHIFT - 10),
435                num_physpages << (PAGE_SHIFT - 10), codesize >> 10,
436                reserved_pages << (PAGE_SHIFT - 10), datasize >> 10, initsize >> 10);
437
438         /*
439          * Allow for enough (cached) page table pages so that we can map the entire memory
440          * at least once.  Each task also needs a couple of page tables pages, so add in a
441          * fudge factor for that (don't use "threads-max" here; that would be wrong!).
442          * Don't allow the cache to be more than 10% of total memory, though.
443          */
444 #       define NUM_TASKS        500     /* typical number of tasks */
445         num_pgt_pages = nr_free_pages() / PTRS_PER_PGD + NUM_TASKS;
446         if (num_pgt_pages > nr_free_pages() / 10)
447                 num_pgt_pages = nr_free_pages() / 10;
448         if (num_pgt_pages > pgt_cache_water[1])
449                 pgt_cache_water[1] = num_pgt_pages;
450
451         /* install the gate page in the global page table: */
452         put_gate_page(virt_to_page(__start_gate_section), GATE_ADDR);
453
454 #ifdef CONFIG_IA32_SUPPORT
455         ia32_gdt_init();
456 #endif
457 }