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