blob: 6368b86b84e2cc64a929dccd54e93b70c80bdd4c [file] [log] [blame]
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09001#include <linux/gfp.h>
Jaswinder Singh Rajput2c1b2842009-04-11 00:03:10 +05302#include <linux/initrd.h>
Pekka Enberg540aca02009-03-04 11:46:40 +02003#include <linux/ioport.h>
Pekka Enberge5b2bb52009-03-03 13:15:06 +02004#include <linux/swap.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -07005#include <linux/memblock.h>
Pekka Enberg17623912011-11-01 15:58:22 +02006#include <linux/bootmem.h> /* for max_low_pfn */
Pekka Enberg540aca02009-03-04 11:46:40 +02007
Pekka Enberge5b2bb52009-03-03 13:15:06 +02008#include <asm/cacheflush.h>
Pekka Enbergf7650902009-03-05 14:55:05 +02009#include <asm/e820.h>
Pekka Enberg4fcb2082009-03-05 14:55:08 +020010#include <asm/init.h>
Pekka Enberge5b2bb52009-03-03 13:15:06 +020011#include <asm/page.h>
Pekka Enberg540aca02009-03-04 11:46:40 +020012#include <asm/page_types.h>
Pekka Enberge5b2bb52009-03-03 13:15:06 +020013#include <asm/sections.h>
Jan Beulich49834392009-05-06 13:06:47 +010014#include <asm/setup.h>
Pekka Enbergf7650902009-03-05 14:55:05 +020015#include <asm/tlbflush.h>
Pekka Enberg9518e0e2009-04-28 16:00:50 +030016#include <asm/tlb.h>
Jaswinder Singh Rajput76c06922009-07-01 19:54:23 +053017#include <asm/proto.h>
Pekka Enberg17623912011-11-01 15:58:22 +020018#include <asm/dma.h> /* for MAX_DMA_PFN */
Pekka Enberg9518e0e2009-04-28 16:00:50 +030019
Yinghai Lud1b19422011-02-24 14:46:24 +010020unsigned long __initdata pgt_buf_start;
21unsigned long __meminitdata pgt_buf_end;
22unsigned long __meminitdata pgt_buf_top;
Pekka Enbergf7650902009-03-05 14:55:05 +020023
24int after_bootmem;
25
26int direct_gbpages
27#ifdef CONFIG_DIRECT_GBPAGES
28 = 1
29#endif
30;
31
Jacob Shin844ab6f2012-10-24 14:24:44 -050032struct map_range {
33 unsigned long start;
34 unsigned long end;
35 unsigned page_size_mask;
36};
37
Yinghai Lufa62aaf2012-11-16 19:38:38 -080038static int page_size_mask;
Jacob Shin844ab6f2012-10-24 14:24:44 -050039/*
40 * First calculate space needed for kernel direct mapping page tables to cover
41 * mr[0].start to mr[nr_range - 1].end, while accounting for possible 2M and 1GB
42 * pages. Then find enough contiguous space for those page tables.
43 */
44static void __init find_early_table_space(struct map_range *mr, int nr_range)
Pekka Enbergf7650902009-03-05 14:55:05 +020045{
Jacob Shin844ab6f2012-10-24 14:24:44 -050046 int i;
47 unsigned long puds = 0, pmds = 0, ptes = 0, tables;
48 unsigned long start = 0, good_end;
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070049 phys_addr_t base;
Pekka Enbergf7650902009-03-05 14:55:05 +020050
Jacob Shin844ab6f2012-10-24 14:24:44 -050051 for (i = 0; i < nr_range; i++) {
52 unsigned long range, extra;
Pekka Enbergf7650902009-03-05 14:55:05 +020053
Jacob Shin844ab6f2012-10-24 14:24:44 -050054 range = mr[i].end - mr[i].start;
55 puds += (range + PUD_SIZE - 1) >> PUD_SHIFT;
Pekka Enbergf7650902009-03-05 14:55:05 +020056
Jacob Shin844ab6f2012-10-24 14:24:44 -050057 if (mr[i].page_size_mask & (1 << PG_LEVEL_1G)) {
58 extra = range - ((range >> PUD_SHIFT) << PUD_SHIFT);
59 pmds += (extra + PMD_SIZE - 1) >> PMD_SHIFT;
60 } else {
61 pmds += (range + PMD_SIZE - 1) >> PMD_SHIFT;
62 }
Pekka Enbergf7650902009-03-05 14:55:05 +020063
Jacob Shin844ab6f2012-10-24 14:24:44 -050064 if (mr[i].page_size_mask & (1 << PG_LEVEL_2M)) {
65 extra = range - ((range >> PMD_SHIFT) << PMD_SHIFT);
Pekka Enbergf7650902009-03-05 14:55:05 +020066#ifdef CONFIG_X86_32
Jacob Shin844ab6f2012-10-24 14:24:44 -050067 extra += PMD_SIZE;
Pekka Enbergf7650902009-03-05 14:55:05 +020068#endif
Jacob Shin844ab6f2012-10-24 14:24:44 -050069 ptes += (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
70 } else {
71 ptes += (range + PAGE_SIZE - 1) >> PAGE_SHIFT;
72 }
73 }
74
75 tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
76 tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
Pekka Enbergf7650902009-03-05 14:55:05 +020077 tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
78
79#ifdef CONFIG_X86_32
80 /* for fixmap */
81 tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
Yinghai Lu80989ce2009-05-09 23:47:42 -070082#endif
Takashi Iwai8548c842011-10-23 23:19:12 +020083 good_end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu1411e0e2010-12-27 16:48:17 -080084
Yinghai Lu4b239f42010-12-17 16:58:28 -080085 base = memblock_find_in_range(start, good_end, tables, PAGE_SIZE);
Tejun Heo1f5026a2011-07-12 09:58:09 +020086 if (!base)
Pekka Enbergf7650902009-03-05 14:55:05 +020087 panic("Cannot find space for the kernel page tables");
88
Yinghai Lud1b19422011-02-24 14:46:24 +010089 pgt_buf_start = base >> PAGE_SHIFT;
90 pgt_buf_end = pgt_buf_start;
91 pgt_buf_top = pgt_buf_start + (tables >> PAGE_SHIFT);
Pekka Enbergf7650902009-03-05 14:55:05 +020092
Bjorn Helgaas365811d2012-05-29 15:06:29 -070093 printk(KERN_DEBUG "kernel direct mapping tables up to %#lx @ [mem %#010lx-%#010lx]\n",
Jacob Shin844ab6f2012-10-24 14:24:44 -050094 mr[nr_range - 1].end - 1, pgt_buf_start << PAGE_SHIFT,
Bjorn Helgaas365811d2012-05-29 15:06:29 -070095 (pgt_buf_top << PAGE_SHIFT) - 1);
Pekka Enbergf7650902009-03-05 14:55:05 +020096}
97
Yinghai Lufa62aaf2012-11-16 19:38:38 -080098void probe_page_size_mask(void)
99{
100#if !defined(CONFIG_DEBUG_PAGEALLOC) && !defined(CONFIG_KMEMCHECK)
101 /*
102 * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
103 * This will simplify cpa(), which otherwise needs to support splitting
104 * large pages into small in interrupt context, etc.
105 */
106 if (direct_gbpages)
107 page_size_mask |= 1 << PG_LEVEL_1G;
108 if (cpu_has_pse)
109 page_size_mask |= 1 << PG_LEVEL_2M;
110#endif
111
112 /* Enable PSE if available */
113 if (cpu_has_pse)
114 set_in_cr4(X86_CR4_PSE);
115
116 /* Enable PGE if available */
117 if (cpu_has_pge) {
118 set_in_cr4(X86_CR4_PGE);
119 __supported_pte_mask |= _PAGE_GLOBAL;
120 }
121}
Sedat Dilek53f80232011-04-17 16:17:34 +0200122void __init native_pagetable_reserve(u64 start, u64 end)
Stefano Stabellini279b7062011-04-14 15:49:41 +0100123{
Tejun Heo24aa0782011-07-12 11:16:06 +0200124 memblock_reserve(start, end - start);
Stefano Stabellini279b7062011-04-14 15:49:41 +0100125}
126
Pekka Enbergf7650902009-03-05 14:55:05 +0200127#ifdef CONFIG_X86_32
128#define NR_RANGE_MR 3
129#else /* CONFIG_X86_64 */
130#define NR_RANGE_MR 5
131#endif
132
Jan Beulichdc9dd5c2009-03-12 12:40:06 +0000133static int __meminit save_mr(struct map_range *mr, int nr_range,
134 unsigned long start_pfn, unsigned long end_pfn,
135 unsigned long page_size_mask)
Pekka Enbergf7650902009-03-05 14:55:05 +0200136{
137 if (start_pfn < end_pfn) {
138 if (nr_range >= NR_RANGE_MR)
139 panic("run out of range for init_memory_mapping\n");
140 mr[nr_range].start = start_pfn<<PAGE_SHIFT;
141 mr[nr_range].end = end_pfn<<PAGE_SHIFT;
142 mr[nr_range].page_size_mask = page_size_mask;
143 nr_range++;
144 }
145
146 return nr_range;
147}
148
Yinghai Lu4e33e062012-11-16 19:38:39 -0800149static int __meminit split_mem_range(struct map_range *mr, int nr_range,
150 unsigned long start,
151 unsigned long end)
Pekka Enbergf7650902009-03-05 14:55:05 +0200152{
Pekka Enbergf7650902009-03-05 14:55:05 +0200153 unsigned long start_pfn, end_pfn;
154 unsigned long pos;
Yinghai Lu4e33e062012-11-16 19:38:39 -0800155 int i;
Pekka Enbergf7650902009-03-05 14:55:05 +0200156
157 /* head if not big page alignment ? */
158 start_pfn = start >> PAGE_SHIFT;
159 pos = start_pfn << PAGE_SHIFT;
160#ifdef CONFIG_X86_32
161 /*
162 * Don't use a large page for the first 2/4MB of memory
163 * because there are often fixed size MTRRs in there
164 * and overlapping MTRRs into large pages can cause
165 * slowdowns.
166 */
167 if (pos == 0)
168 end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
169 else
170 end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
171 << (PMD_SHIFT - PAGE_SHIFT);
172#else /* CONFIG_X86_64 */
173 end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT)
174 << (PMD_SHIFT - PAGE_SHIFT);
175#endif
176 if (end_pfn > (end >> PAGE_SHIFT))
177 end_pfn = end >> PAGE_SHIFT;
178 if (start_pfn < end_pfn) {
179 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
180 pos = end_pfn << PAGE_SHIFT;
181 }
182
183 /* big page (2M) range */
184 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
185 << (PMD_SHIFT - PAGE_SHIFT);
186#ifdef CONFIG_X86_32
187 end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
188#else /* CONFIG_X86_64 */
189 end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
190 << (PUD_SHIFT - PAGE_SHIFT);
191 if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)))
192 end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT));
193#endif
194
195 if (start_pfn < end_pfn) {
196 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
197 page_size_mask & (1<<PG_LEVEL_2M));
198 pos = end_pfn << PAGE_SHIFT;
199 }
200
201#ifdef CONFIG_X86_64
202 /* big page (1G) range */
203 start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
204 << (PUD_SHIFT - PAGE_SHIFT);
205 end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
206 if (start_pfn < end_pfn) {
207 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
208 page_size_mask &
209 ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
210 pos = end_pfn << PAGE_SHIFT;
211 }
212
213 /* tail is not big page (1G) alignment */
214 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
215 << (PMD_SHIFT - PAGE_SHIFT);
216 end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
217 if (start_pfn < end_pfn) {
218 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
219 page_size_mask & (1<<PG_LEVEL_2M));
220 pos = end_pfn << PAGE_SHIFT;
221 }
222#endif
223
224 /* tail is not big page (2M) alignment */
225 start_pfn = pos>>PAGE_SHIFT;
226 end_pfn = end>>PAGE_SHIFT;
227 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
228
229 /* try to merge same page size and continuous */
230 for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
231 unsigned long old_start;
232 if (mr[i].end != mr[i+1].start ||
233 mr[i].page_size_mask != mr[i+1].page_size_mask)
234 continue;
235 /* move it */
236 old_start = mr[i].start;
237 memmove(&mr[i], &mr[i+1],
238 (nr_range - 1 - i) * sizeof(struct map_range));
239 mr[i--].start = old_start;
240 nr_range--;
241 }
242
243 for (i = 0; i < nr_range; i++)
Bjorn Helgaas365811d2012-05-29 15:06:29 -0700244 printk(KERN_DEBUG " [mem %#010lx-%#010lx] page %s\n",
245 mr[i].start, mr[i].end - 1,
Pekka Enbergf7650902009-03-05 14:55:05 +0200246 (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
247 (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
248
Yinghai Lu4e33e062012-11-16 19:38:39 -0800249 return nr_range;
250}
251
252/*
253 * Setup the direct mapping of the physical memory at PAGE_OFFSET.
254 * This runs before bootmem is initialized and gets pages directly from
255 * the physical memory. To access them they are temporarily mapped.
256 */
257unsigned long __init_refok init_memory_mapping(unsigned long start,
258 unsigned long end)
259{
260 struct map_range mr[NR_RANGE_MR];
261 unsigned long ret = 0;
262 int nr_range, i;
263
264 pr_info("init_memory_mapping: [mem %#010lx-%#010lx]\n",
265 start, end - 1);
266
267 memset(mr, 0, sizeof(mr));
268 nr_range = split_mem_range(mr, 0, start, end);
269
Pekka Enbergf7650902009-03-05 14:55:05 +0200270 /*
271 * Find space for the kernel direct mapping tables.
272 *
273 * Later we should allocate these tables in the local node of the
274 * memory mapped. Unfortunately this is done currently before the
275 * nodes are discovered.
276 */
277 if (!after_bootmem)
Jacob Shin844ab6f2012-10-24 14:24:44 -0500278 find_early_table_space(mr, nr_range);
Pekka Enbergf7650902009-03-05 14:55:05 +0200279
Pekka Enbergf7650902009-03-05 14:55:05 +0200280 for (i = 0; i < nr_range; i++)
281 ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
282 mr[i].page_size_mask);
Pekka Enbergf7650902009-03-05 14:55:05 +0200283
284#ifdef CONFIG_X86_32
285 early_ioremap_page_table_range_init();
286
287 load_cr3(swapper_pg_dir);
288#endif
289
Pekka Enbergf7650902009-03-05 14:55:05 +0200290 __flush_tlb_all();
291
Stefano Stabellini279b7062011-04-14 15:49:41 +0100292 /*
293 * Reserve the kernel pagetable pages we used (pgt_buf_start -
294 * pgt_buf_end) and free the other ones (pgt_buf_end - pgt_buf_top)
295 * so that they can be reused for other purposes.
296 *
Tejun Heo24aa0782011-07-12 11:16:06 +0200297 * On native it just means calling memblock_reserve, on Xen it also
298 * means marking RW the pagetable pages that we allocated before
Stefano Stabellini279b7062011-04-14 15:49:41 +0100299 * but that haven't been used.
300 *
301 * In fact on xen we mark RO the whole range pgt_buf_start -
302 * pgt_buf_top, because we have to make sure that when
303 * init_memory_mapping reaches the pagetable pages area, it maps
304 * RO all the pagetable pages, including the ones that are beyond
305 * pgt_buf_end at that time.
306 */
Yinghai Lud1b19422011-02-24 14:46:24 +0100307 if (!after_bootmem && pgt_buf_end > pgt_buf_start)
Stefano Stabellini279b7062011-04-14 15:49:41 +0100308 x86_init.mapping.pagetable_reserve(PFN_PHYS(pgt_buf_start),
309 PFN_PHYS(pgt_buf_end));
Pekka Enbergf7650902009-03-05 14:55:05 +0200310
311 if (!after_bootmem)
312 early_memtest(start, end);
313
314 return ret >> PAGE_SHIFT;
315}
316
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200317
Pekka Enberg540aca02009-03-04 11:46:40 +0200318/*
319 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
320 * is valid. The argument is a physical page number.
321 *
322 *
323 * On x86, access has to be given to the first megabyte of ram because that area
324 * contains bios code and data regions used by X and dosemu and similar apps.
325 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
326 * mmio resources as well as potential bios/acpi data regions.
327 */
328int devmem_is_allowed(unsigned long pagenr)
329{
T Makphaibulchoke73e8f3d2012-08-28 21:21:43 -0600330 if (pagenr < 256)
Pekka Enberg540aca02009-03-04 11:46:40 +0200331 return 1;
332 if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
333 return 0;
334 if (!page_is_ram(pagenr))
335 return 1;
336 return 0;
337}
338
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200339void free_init_pages(char *what, unsigned long begin, unsigned long end)
340{
Yinghai Luc967da62010-03-28 19:42:55 -0700341 unsigned long addr;
342 unsigned long begin_aligned, end_aligned;
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200343
Yinghai Luc967da62010-03-28 19:42:55 -0700344 /* Make sure boundaries are page aligned */
345 begin_aligned = PAGE_ALIGN(begin);
346 end_aligned = end & PAGE_MASK;
347
348 if (WARN_ON(begin_aligned != begin || end_aligned != end)) {
349 begin = begin_aligned;
350 end = end_aligned;
351 }
352
353 if (begin >= end)
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200354 return;
355
Yinghai Luc967da62010-03-28 19:42:55 -0700356 addr = begin;
357
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200358 /*
359 * If debugging page accesses then do not free this memory but
360 * mark them not present - any buggy init-section access will
361 * create a kernel page fault:
362 */
363#ifdef CONFIG_DEBUG_PAGEALLOC
Bjorn Helgaas365811d2012-05-29 15:06:29 -0700364 printk(KERN_INFO "debug: unmapping init [mem %#010lx-%#010lx]\n",
365 begin, end - 1);
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200366 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
367#else
368 /*
369 * We just marked the kernel text read only above, now that
370 * we are going to free part of that, we need to make that
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100371 * writeable and non-executable first.
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200372 */
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100373 set_memory_nx(begin, (end - begin) >> PAGE_SHIFT);
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200374 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
375
376 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
377
378 for (; addr < end; addr += PAGE_SIZE) {
379 ClearPageReserved(virt_to_page(addr));
380 init_page_count(virt_to_page(addr));
Yinghai Luc967da62010-03-28 19:42:55 -0700381 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200382 free_page(addr);
383 totalram_pages++;
384 }
385#endif
386}
387
388void free_initmem(void)
389{
390 free_init_pages("unused kernel memory",
391 (unsigned long)(&__init_begin),
392 (unsigned long)(&__init_end));
393}
Pekka Enberg731ddea2009-03-04 11:13:40 +0200394
395#ifdef CONFIG_BLK_DEV_INITRD
Jan Beulich0d26d1d2012-06-18 11:30:20 +0100396void __init free_initrd_mem(unsigned long start, unsigned long end)
Pekka Enberg731ddea2009-03-04 11:13:40 +0200397{
Yinghai Luc967da62010-03-28 19:42:55 -0700398 /*
399 * end could be not aligned, and We can not align that,
400 * decompresser could be confused by aligned initrd_end
401 * We already reserve the end partial page before in
402 * - i386_start_kernel()
403 * - x86_64_start_kernel()
404 * - relocate_initrd()
405 * So here We can do PAGE_ALIGN() safely to get partial page to be freed
406 */
407 free_init_pages("initrd memory", start, PAGE_ALIGN(end));
Pekka Enberg731ddea2009-03-04 11:13:40 +0200408}
409#endif
Pekka Enberg17623912011-11-01 15:58:22 +0200410
411void __init zone_sizes_init(void)
412{
413 unsigned long max_zone_pfns[MAX_NR_ZONES];
414
415 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
416
417#ifdef CONFIG_ZONE_DMA
418 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
419#endif
420#ifdef CONFIG_ZONE_DMA32
421 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
422#endif
423 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
424#ifdef CONFIG_HIGHMEM
425 max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
426#endif
427
428 free_area_init_nodes(max_zone_pfns);
429}
430