kaiser: add "nokaiser" boot option, using ALTERNATIVE
[pandora-kernel.git] / arch / x86 / mm / init.c
1 #include <linux/gfp.h>
2 #include <linux/initrd.h>
3 #include <linux/ioport.h>
4 #include <linux/swap.h>
5 #include <linux/memblock.h>
6
7 #include <asm/cacheflush.h>
8 #include <asm/e820.h>
9 #include <asm/init.h>
10 #include <asm/page.h>
11 #include <asm/page_types.h>
12 #include <asm/sections.h>
13 #include <asm/setup.h>
14 #include <asm/system.h>
15 #include <asm/tlbflush.h>
16 #include <asm/tlb.h>
17 #include <asm/proto.h>
18
19 unsigned long __initdata pgt_buf_start;
20 unsigned long __meminitdata pgt_buf_end;
21 unsigned long __meminitdata pgt_buf_top;
22
23 int after_bootmem;
24
25 int direct_gbpages
26 #ifdef CONFIG_DIRECT_GBPAGES
27                                 = 1
28 #endif
29 ;
30
31 struct map_range {
32         unsigned long start;
33         unsigned long end;
34         unsigned page_size_mask;
35 };
36
37 /*
38  * First calculate space needed for kernel direct mapping page tables to cover
39  * mr[0].start to mr[nr_range - 1].end, while accounting for possible 2M and 1GB
40  * pages. Then find enough contiguous space for those page tables.
41  */
42 static void __init find_early_table_space(struct map_range *mr, int nr_range)
43 {
44         int i;
45         unsigned long puds = 0, pmds = 0, ptes = 0, tables;
46         unsigned long start = 0, good_end;
47         unsigned long pgd_extra = 0;
48         phys_addr_t base;
49
50         for (i = 0; i < nr_range; i++) {
51                 unsigned long range, extra;
52
53                 if ((mr[i].end >> PGDIR_SHIFT) - (mr[i].start >> PGDIR_SHIFT))
54                         pgd_extra++;
55
56                 range = mr[i].end - mr[i].start;
57                 puds += (range + PUD_SIZE - 1) >> PUD_SHIFT;
58
59                 if (mr[i].page_size_mask & (1 << PG_LEVEL_1G)) {
60                         extra = range - ((range >> PUD_SHIFT) << PUD_SHIFT);
61                         pmds += (extra + PMD_SIZE - 1) >> PMD_SHIFT;
62                 } else {
63                         pmds += (range + PMD_SIZE - 1) >> PMD_SHIFT;
64                 }
65
66                 if (mr[i].page_size_mask & (1 << PG_LEVEL_2M)) {
67                         extra = range - ((range >> PMD_SHIFT) << PMD_SHIFT);
68 #ifdef CONFIG_X86_32
69                         extra += PMD_SIZE;
70 #endif
71                         ptes += (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
72                 } else {
73                         ptes += (range + PAGE_SIZE - 1) >> PAGE_SHIFT;
74                 }
75         }
76
77         tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
78         tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
79         tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
80         tables += (pgd_extra * PAGE_SIZE);
81
82 #ifdef CONFIG_X86_32
83         /* for fixmap */
84         tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
85 #endif
86         good_end = max_pfn_mapped << PAGE_SHIFT;
87
88         base = memblock_find_in_range(start, good_end, tables, PAGE_SIZE);
89         if (base == MEMBLOCK_ERROR)
90                 panic("Cannot find space for the kernel page tables");
91
92         pgt_buf_start = base >> PAGE_SHIFT;
93         pgt_buf_end = pgt_buf_start;
94         pgt_buf_top = pgt_buf_start + (tables >> PAGE_SHIFT);
95
96         printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
97                 mr[nr_range - 1].end, pgt_buf_start << PAGE_SHIFT,
98                 pgt_buf_top << PAGE_SHIFT);
99 }
100
101 void __init native_pagetable_reserve(u64 start, u64 end)
102 {
103         memblock_x86_reserve_range(start, end, "PGTABLE");
104 }
105
106 #ifdef CONFIG_X86_32
107 #define NR_RANGE_MR 3
108 #else /* CONFIG_X86_64 */
109 #define NR_RANGE_MR 5
110 #endif
111
112 static int __meminit save_mr(struct map_range *mr, int nr_range,
113                              unsigned long start_pfn, unsigned long end_pfn,
114                              unsigned long page_size_mask)
115 {
116         if (start_pfn < end_pfn) {
117                 if (nr_range >= NR_RANGE_MR)
118                         panic("run out of range for init_memory_mapping\n");
119                 mr[nr_range].start = start_pfn<<PAGE_SHIFT;
120                 mr[nr_range].end   = end_pfn<<PAGE_SHIFT;
121                 mr[nr_range].page_size_mask = page_size_mask;
122                 nr_range++;
123         }
124
125         return nr_range;
126 }
127
128 /*
129  * Setup the direct mapping of the physical memory at PAGE_OFFSET.
130  * This runs before bootmem is initialized and gets pages directly from
131  * the physical memory. To access them they are temporarily mapped.
132  */
133 unsigned long __init_refok init_memory_mapping(unsigned long start,
134                                                unsigned long end)
135 {
136         unsigned long page_size_mask = 0;
137         unsigned long start_pfn, end_pfn;
138         unsigned long ret = 0;
139         unsigned long pos;
140
141         struct map_range mr[NR_RANGE_MR];
142         int nr_range, i;
143         int use_pse, use_gbpages;
144
145         printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end);
146
147 #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
148         /*
149          * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
150          * This will simplify cpa(), which otherwise needs to support splitting
151          * large pages into small in interrupt context, etc.
152          */
153         use_pse = use_gbpages = 0;
154 #else
155         use_pse = cpu_has_pse;
156         use_gbpages = direct_gbpages;
157 #endif
158
159         /* Enable PSE if available */
160         if (cpu_has_pse)
161                 set_in_cr4(X86_CR4_PSE);
162
163         /* Enable PGE if available */
164         if (cpu_has_pge && !kaiser_enabled) {
165                 set_in_cr4(X86_CR4_PGE);
166                 __supported_pte_mask |= _PAGE_GLOBAL;
167         }
168
169         if (use_gbpages)
170                 page_size_mask |= 1 << PG_LEVEL_1G;
171         if (use_pse)
172                 page_size_mask |= 1 << PG_LEVEL_2M;
173
174         memset(mr, 0, sizeof(mr));
175         nr_range = 0;
176
177         /* head if not big page alignment ? */
178         start_pfn = start >> PAGE_SHIFT;
179         pos = start_pfn << PAGE_SHIFT;
180 #ifdef CONFIG_X86_32
181         /*
182          * Don't use a large page for the first 2/4MB of memory
183          * because there are often fixed size MTRRs in there
184          * and overlapping MTRRs into large pages can cause
185          * slowdowns.
186          */
187         if (pos == 0)
188                 end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
189         else
190                 end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
191                                  << (PMD_SHIFT - PAGE_SHIFT);
192 #else /* CONFIG_X86_64 */
193         end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT)
194                         << (PMD_SHIFT - PAGE_SHIFT);
195 #endif
196         if (end_pfn > (end >> PAGE_SHIFT))
197                 end_pfn = end >> PAGE_SHIFT;
198         if (start_pfn < end_pfn) {
199                 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
200                 pos = end_pfn << PAGE_SHIFT;
201         }
202
203         /* big page (2M) range */
204         start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
205                          << (PMD_SHIFT - PAGE_SHIFT);
206 #ifdef CONFIG_X86_32
207         end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
208 #else /* CONFIG_X86_64 */
209         end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
210                          << (PUD_SHIFT - PAGE_SHIFT);
211         if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)))
212                 end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT));
213 #endif
214
215         if (start_pfn < end_pfn) {
216                 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
217                                 page_size_mask & (1<<PG_LEVEL_2M));
218                 pos = end_pfn << PAGE_SHIFT;
219         }
220
221 #ifdef CONFIG_X86_64
222         /* big page (1G) range */
223         start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
224                          << (PUD_SHIFT - PAGE_SHIFT);
225         end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
226         if (start_pfn < end_pfn) {
227                 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
228                                 page_size_mask &
229                                  ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
230                 pos = end_pfn << PAGE_SHIFT;
231         }
232
233         /* tail is not big page (1G) alignment */
234         start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
235                          << (PMD_SHIFT - PAGE_SHIFT);
236         end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
237         if (start_pfn < end_pfn) {
238                 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
239                                 page_size_mask & (1<<PG_LEVEL_2M));
240                 pos = end_pfn << PAGE_SHIFT;
241         }
242 #endif
243
244         /* tail is not big page (2M) alignment */
245         start_pfn = pos>>PAGE_SHIFT;
246         end_pfn = end>>PAGE_SHIFT;
247         nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
248
249         /* try to merge same page size and continuous */
250         for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
251                 unsigned long old_start;
252                 if (mr[i].end != mr[i+1].start ||
253                     mr[i].page_size_mask != mr[i+1].page_size_mask)
254                         continue;
255                 /* move it */
256                 old_start = mr[i].start;
257                 memmove(&mr[i], &mr[i+1],
258                         (nr_range - 1 - i) * sizeof(struct map_range));
259                 mr[i--].start = old_start;
260                 nr_range--;
261         }
262
263         for (i = 0; i < nr_range; i++)
264                 printk(KERN_DEBUG " %010lx - %010lx page %s\n",
265                                 mr[i].start, mr[i].end,
266                         (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
267                          (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
268
269         /*
270          * Find space for the kernel direct mapping tables.
271          *
272          * Later we should allocate these tables in the local node of the
273          * memory mapped. Unfortunately this is done currently before the
274          * nodes are discovered.
275          */
276         if (!after_bootmem)
277                 find_early_table_space(mr, nr_range);
278
279         for (i = 0; i < nr_range; i++)
280                 ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
281                                                    mr[i].page_size_mask);
282
283 #ifdef CONFIG_X86_32
284         early_ioremap_page_table_range_init();
285
286         load_cr3(swapper_pg_dir);
287 #endif
288
289         __flush_tlb_all();
290
291         /*
292          * Reserve the kernel pagetable pages we used (pgt_buf_start -
293          * pgt_buf_end) and free the other ones (pgt_buf_end - pgt_buf_top)
294          * so that they can be reused for other purposes.
295          *
296          * On native it just means calling memblock_x86_reserve_range, on Xen it
297          * also means marking RW the pagetable pages that we allocated before
298          * but that haven't been used.
299          *
300          * In fact on xen we mark RO the whole range pgt_buf_start -
301          * pgt_buf_top, because we have to make sure that when
302          * init_memory_mapping reaches the pagetable pages area, it maps
303          * RO all the pagetable pages, including the ones that are beyond
304          * pgt_buf_end at that time.
305          */
306         if (!after_bootmem && pgt_buf_end > pgt_buf_start)
307                 x86_init.mapping.pagetable_reserve(PFN_PHYS(pgt_buf_start),
308                                 PFN_PHYS(pgt_buf_end));
309
310         if (!after_bootmem)
311                 early_memtest(start, end);
312
313         return ret >> PAGE_SHIFT;
314 }
315
316
317 /*
318  * devmem_is_allowed() checks to see if /dev/mem access to a certain address
319  * is valid. The argument is a physical page number.
320  *
321  * On x86, access has to be given to the first megabyte of RAM because that
322  * area traditionally contains BIOS code and data regions used by X, dosemu,
323  * and similar apps. Since they map the entire memory range, the whole range
324  * must be allowed (for mapping), but any areas that would otherwise be
325  * disallowed are flagged as being "zero filled" instead of rejected.
326  * Access has to be given to non-kernel-ram areas as well, these contain the
327  * PCI mmio resources as well as potential bios/acpi data regions.
328  */
329 int devmem_is_allowed(unsigned long pagenr)
330 {
331         if (page_is_ram(pagenr)) {
332                 /*
333                  * For disallowed memory regions in the low 1MB range,
334                  * request that the page be shown as all zeros.
335                  */
336                 if (pagenr < 256)
337                         return 2;
338
339                 return 0;
340         }
341
342         /*
343          * This must follow RAM test, since System RAM is considered a
344          * restricted resource under CONFIG_STRICT_IOMEM.
345          */
346         if (iomem_is_exclusive(pagenr << PAGE_SHIFT)) {
347                 /* Low 1MB bypasses iomem restrictions. */
348                 if (pagenr < 256)
349                         return 1;
350
351                 return 0;
352         }
353
354         return 1;
355 }
356
357 void free_init_pages(char *what, unsigned long begin, unsigned long end)
358 {
359         unsigned long addr;
360         unsigned long begin_aligned, end_aligned;
361
362         /* Make sure boundaries are page aligned */
363         begin_aligned = PAGE_ALIGN(begin);
364         end_aligned   = end & PAGE_MASK;
365
366         if (WARN_ON(begin_aligned != begin || end_aligned != end)) {
367                 begin = begin_aligned;
368                 end   = end_aligned;
369         }
370
371         if (begin >= end)
372                 return;
373
374         addr = begin;
375
376         /*
377          * If debugging page accesses then do not free this memory but
378          * mark them not present - any buggy init-section access will
379          * create a kernel page fault:
380          */
381 #ifdef CONFIG_DEBUG_PAGEALLOC
382         printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
383                 begin, end);
384         set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
385 #else
386         /*
387          * We just marked the kernel text read only above, now that
388          * we are going to free part of that, we need to make that
389          * writeable and non-executable first.
390          */
391         set_memory_nx(begin, (end - begin) >> PAGE_SHIFT);
392         set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
393
394         printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
395
396         for (; addr < end; addr += PAGE_SIZE) {
397                 ClearPageReserved(virt_to_page(addr));
398                 init_page_count(virt_to_page(addr));
399                 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
400                 free_page(addr);
401                 totalram_pages++;
402         }
403 #endif
404 }
405
406 void free_initmem(void)
407 {
408         free_init_pages("unused kernel memory",
409                         (unsigned long)(&__init_begin),
410                         (unsigned long)(&__init_end));
411 }
412
413 #ifdef CONFIG_BLK_DEV_INITRD
414 void free_initrd_mem(unsigned long start, unsigned long end)
415 {
416         /*
417          * end could be not aligned, and We can not align that,
418          * decompresser could be confused by aligned initrd_end
419          * We already reserve the end partial page before in
420          *   - i386_start_kernel()
421          *   - x86_64_start_kernel()
422          *   - relocate_initrd()
423          * So here We can do PAGE_ALIGN() safely to get partial page to be freed
424          */
425         free_init_pages("initrd memory", start, PAGE_ALIGN(end));
426 }
427 #endif