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