blob: fae70904ed08dd85d04c7b04862e42f3db91bb07 [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 Shin3fd37d22012-10-24 14:24:44 -050032struct map_range {
33 unsigned long start;
34 unsigned long end;
35 unsigned page_size_mask;
36};
37
38/*
39 * First calculate space needed for kernel direct mapping page tables to cover
40 * mr[0].start to mr[nr_range - 1].end, while accounting for possible 2M and 1GB
41 * pages. Then find enough contiguous space for those page tables.
42 */
43static void __init find_early_table_space(struct map_range *mr, int nr_range)
Pekka Enbergf7650902009-03-05 14:55:05 +020044{
Jacob Shin3fd37d22012-10-24 14:24:44 -050045 int i;
46 unsigned long puds = 0, pmds = 0, ptes = 0, tables;
47 unsigned long start = 0, good_end;
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070048 phys_addr_t base;
Pekka Enbergf7650902009-03-05 14:55:05 +020049
Jacob Shin3fd37d22012-10-24 14:24:44 -050050 for (i = 0; i < nr_range; i++) {
51 unsigned long range, extra;
Pekka Enbergf7650902009-03-05 14:55:05 +020052
Jacob Shin3fd37d22012-10-24 14:24:44 -050053 range = mr[i].end - mr[i].start;
54 puds += (range + PUD_SIZE - 1) >> PUD_SHIFT;
Pekka Enbergf7650902009-03-05 14:55:05 +020055
Jacob Shin3fd37d22012-10-24 14:24:44 -050056 if (mr[i].page_size_mask & (1 << PG_LEVEL_1G)) {
57 extra = range - ((range >> PUD_SHIFT) << PUD_SHIFT);
58 pmds += (extra + PMD_SIZE - 1) >> PMD_SHIFT;
59 } else {
60 pmds += (range + PMD_SIZE - 1) >> PMD_SHIFT;
61 }
Pekka Enbergf7650902009-03-05 14:55:05 +020062
Jacob Shin3fd37d22012-10-24 14:24:44 -050063 if (mr[i].page_size_mask & (1 << PG_LEVEL_2M)) {
64 extra = range - ((range >> PMD_SHIFT) << PMD_SHIFT);
Pekka Enbergf7650902009-03-05 14:55:05 +020065#ifdef CONFIG_X86_32
Jacob Shin3fd37d22012-10-24 14:24:44 -050066 extra += PMD_SIZE;
Pekka Enbergf7650902009-03-05 14:55:05 +020067#endif
Jacob Shin3fd37d22012-10-24 14:24:44 -050068 ptes += (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
69 } else {
70 ptes += (range + PAGE_SIZE - 1) >> PAGE_SHIFT;
71 }
72 }
73
74 tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
75 tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
Pekka Enbergf7650902009-03-05 14:55:05 +020076 tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
77
78#ifdef CONFIG_X86_32
79 /* for fixmap */
80 tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
Yinghai Lu80989ce2009-05-09 23:47:42 -070081#endif
Takashi Iwai8548c842011-10-23 23:19:12 +020082 good_end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu1411e0e2010-12-27 16:48:17 -080083
Yinghai Lu4b239f42010-12-17 16:58:28 -080084 base = memblock_find_in_range(start, good_end, tables, PAGE_SIZE);
Tejun Heo1f5026a2011-07-12 09:58:09 +020085 if (!base)
Pekka Enbergf7650902009-03-05 14:55:05 +020086 panic("Cannot find space for the kernel page tables");
87
Yinghai Lud1b19422011-02-24 14:46:24 +010088 pgt_buf_start = base >> PAGE_SHIFT;
89 pgt_buf_end = pgt_buf_start;
90 pgt_buf_top = pgt_buf_start + (tables >> PAGE_SHIFT);
Pekka Enbergf7650902009-03-05 14:55:05 +020091
Jacob Shin3fd37d22012-10-24 14:24:44 -050092 printk(KERN_DEBUG "kernel direct mapping tables up to %#lx @ [mem %#010lx-%#010lx]\n",
93 mr[nr_range - 1].end - 1, pgt_buf_start << PAGE_SHIFT,
94 (pgt_buf_top << PAGE_SHIFT) - 1);
Pekka Enbergf7650902009-03-05 14:55:05 +020095}
96
Sedat Dilek53f80232011-04-17 16:17:34 +020097void __init native_pagetable_reserve(u64 start, u64 end)
Stefano Stabellini279b7062011-04-14 15:49:41 +010098{
Tejun Heo24aa0782011-07-12 11:16:06 +020099 memblock_reserve(start, end - start);
Stefano Stabellini279b7062011-04-14 15:49:41 +0100100}
101
Pekka Enbergf7650902009-03-05 14:55:05 +0200102#ifdef CONFIG_X86_32
103#define NR_RANGE_MR 3
104#else /* CONFIG_X86_64 */
105#define NR_RANGE_MR 5
106#endif
107
Jan Beulichdc9dd5c2009-03-12 12:40:06 +0000108static int __meminit save_mr(struct map_range *mr, int nr_range,
109 unsigned long start_pfn, unsigned long end_pfn,
110 unsigned long page_size_mask)
Pekka Enbergf7650902009-03-05 14:55:05 +0200111{
112 if (start_pfn < end_pfn) {
113 if (nr_range >= NR_RANGE_MR)
114 panic("run out of range for init_memory_mapping\n");
115 mr[nr_range].start = start_pfn<<PAGE_SHIFT;
116 mr[nr_range].end = end_pfn<<PAGE_SHIFT;
117 mr[nr_range].page_size_mask = page_size_mask;
118 nr_range++;
119 }
120
121 return nr_range;
122}
123
Pekka Enbergf7650902009-03-05 14:55:05 +0200124/*
125 * Setup the direct mapping of the physical memory at PAGE_OFFSET.
126 * This runs before bootmem is initialized and gets pages directly from
127 * the physical memory. To access them they are temporarily mapped.
128 */
129unsigned long __init_refok init_memory_mapping(unsigned long start,
130 unsigned long end)
131{
132 unsigned long page_size_mask = 0;
133 unsigned long start_pfn, end_pfn;
Pekka Enbergc77a3b52009-03-05 17:04:26 +0200134 unsigned long ret = 0;
Pekka Enbergf7650902009-03-05 14:55:05 +0200135 unsigned long pos;
Pekka Enbergf7650902009-03-05 14:55:05 +0200136
137 struct map_range mr[NR_RANGE_MR];
138 int nr_range, i;
139 int use_pse, use_gbpages;
140
141 printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end);
142
Vegard Nossumf8561292008-04-04 00:53:23 +0200143#if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
Pekka Enbergf7650902009-03-05 14:55:05 +0200144 /*
145 * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
146 * This will simplify cpa(), which otherwise needs to support splitting
147 * large pages into small in interrupt context, etc.
148 */
149 use_pse = use_gbpages = 0;
150#else
151 use_pse = cpu_has_pse;
152 use_gbpages = direct_gbpages;
153#endif
154
Pekka Enbergf7650902009-03-05 14:55:05 +0200155 /* Enable PSE if available */
156 if (cpu_has_pse)
157 set_in_cr4(X86_CR4_PSE);
158
159 /* Enable PGE if available */
160 if (cpu_has_pge) {
161 set_in_cr4(X86_CR4_PGE);
162 __supported_pte_mask |= _PAGE_GLOBAL;
163 }
Pekka Enbergf7650902009-03-05 14:55:05 +0200164
165 if (use_gbpages)
166 page_size_mask |= 1 << PG_LEVEL_1G;
167 if (use_pse)
168 page_size_mask |= 1 << PG_LEVEL_2M;
169
170 memset(mr, 0, sizeof(mr));
171 nr_range = 0;
172
173 /* head if not big page alignment ? */
174 start_pfn = start >> PAGE_SHIFT;
175 pos = start_pfn << PAGE_SHIFT;
176#ifdef CONFIG_X86_32
177 /*
178 * Don't use a large page for the first 2/4MB of memory
179 * because there are often fixed size MTRRs in there
180 * and overlapping MTRRs into large pages can cause
181 * slowdowns.
182 */
183 if (pos == 0)
184 end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
185 else
186 end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
187 << (PMD_SHIFT - PAGE_SHIFT);
188#else /* CONFIG_X86_64 */
189 end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT)
190 << (PMD_SHIFT - PAGE_SHIFT);
191#endif
192 if (end_pfn > (end >> PAGE_SHIFT))
193 end_pfn = end >> PAGE_SHIFT;
194 if (start_pfn < end_pfn) {
195 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
196 pos = end_pfn << PAGE_SHIFT;
197 }
198
199 /* big page (2M) range */
200 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
201 << (PMD_SHIFT - PAGE_SHIFT);
202#ifdef CONFIG_X86_32
203 end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
204#else /* CONFIG_X86_64 */
205 end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
206 << (PUD_SHIFT - PAGE_SHIFT);
207 if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)))
208 end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT));
209#endif
210
211 if (start_pfn < end_pfn) {
212 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
213 page_size_mask & (1<<PG_LEVEL_2M));
214 pos = end_pfn << PAGE_SHIFT;
215 }
216
217#ifdef CONFIG_X86_64
218 /* big page (1G) range */
219 start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
220 << (PUD_SHIFT - PAGE_SHIFT);
221 end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
222 if (start_pfn < end_pfn) {
223 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
224 page_size_mask &
225 ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
226 pos = end_pfn << PAGE_SHIFT;
227 }
228
229 /* tail is not big page (1G) alignment */
230 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
231 << (PMD_SHIFT - PAGE_SHIFT);
232 end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
233 if (start_pfn < end_pfn) {
234 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
235 page_size_mask & (1<<PG_LEVEL_2M));
236 pos = end_pfn << PAGE_SHIFT;
237 }
238#endif
239
240 /* tail is not big page (2M) alignment */
241 start_pfn = pos>>PAGE_SHIFT;
242 end_pfn = end>>PAGE_SHIFT;
243 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
244
245 /* try to merge same page size and continuous */
246 for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
247 unsigned long old_start;
248 if (mr[i].end != mr[i+1].start ||
249 mr[i].page_size_mask != mr[i+1].page_size_mask)
250 continue;
251 /* move it */
252 old_start = mr[i].start;
253 memmove(&mr[i], &mr[i+1],
254 (nr_range - 1 - i) * sizeof(struct map_range));
255 mr[i--].start = old_start;
256 nr_range--;
257 }
258
259 for (i = 0; i < nr_range; i++)
260 printk(KERN_DEBUG " %010lx - %010lx page %s\n",
261 mr[i].start, mr[i].end,
262 (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
263 (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
264
265 /*
266 * Find space for the kernel direct mapping tables.
267 *
268 * Later we should allocate these tables in the local node of the
269 * memory mapped. Unfortunately this is done currently before the
270 * nodes are discovered.
271 */
272 if (!after_bootmem)
Jacob Shin3fd37d22012-10-24 14:24:44 -0500273 find_early_table_space(mr, nr_range);
Pekka Enbergf7650902009-03-05 14:55:05 +0200274
Pekka Enbergf7650902009-03-05 14:55:05 +0200275 for (i = 0; i < nr_range; i++)
276 ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
277 mr[i].page_size_mask);
Pekka Enbergf7650902009-03-05 14:55:05 +0200278
279#ifdef CONFIG_X86_32
280 early_ioremap_page_table_range_init();
281
282 load_cr3(swapper_pg_dir);
283#endif
284
Pekka Enbergf7650902009-03-05 14:55:05 +0200285 __flush_tlb_all();
286
Stefano Stabellini279b7062011-04-14 15:49:41 +0100287 /*
288 * Reserve the kernel pagetable pages we used (pgt_buf_start -
289 * pgt_buf_end) and free the other ones (pgt_buf_end - pgt_buf_top)
290 * so that they can be reused for other purposes.
291 *
Tejun Heo24aa0782011-07-12 11:16:06 +0200292 * On native it just means calling memblock_reserve, on Xen it also
293 * means marking RW the pagetable pages that we allocated before
Stefano Stabellini279b7062011-04-14 15:49:41 +0100294 * but that haven't been used.
295 *
296 * In fact on xen we mark RO the whole range pgt_buf_start -
297 * pgt_buf_top, because we have to make sure that when
298 * init_memory_mapping reaches the pagetable pages area, it maps
299 * RO all the pagetable pages, including the ones that are beyond
300 * pgt_buf_end at that time.
301 */
Yinghai Lud1b19422011-02-24 14:46:24 +0100302 if (!after_bootmem && pgt_buf_end > pgt_buf_start)
Stefano Stabellini279b7062011-04-14 15:49:41 +0100303 x86_init.mapping.pagetable_reserve(PFN_PHYS(pgt_buf_start),
304 PFN_PHYS(pgt_buf_end));
Pekka Enbergf7650902009-03-05 14:55:05 +0200305
306 if (!after_bootmem)
307 early_memtest(start, end);
308
309 return ret >> PAGE_SHIFT;
310}
311
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200312
Pekka Enberg540aca02009-03-04 11:46:40 +0200313/*
314 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
315 * is valid. The argument is a physical page number.
316 *
317 *
318 * On x86, access has to be given to the first megabyte of ram because that area
319 * contains bios code and data regions used by X and dosemu and similar apps.
320 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
321 * mmio resources as well as potential bios/acpi data regions.
322 */
323int devmem_is_allowed(unsigned long pagenr)
324{
325 if (pagenr <= 256)
326 return 1;
327 if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
328 return 0;
329 if (!page_is_ram(pagenr))
330 return 1;
331 return 0;
332}
333
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200334void free_init_pages(char *what, unsigned long begin, unsigned long end)
335{
Yinghai Luc967da62010-03-28 19:42:55 -0700336 unsigned long addr;
337 unsigned long begin_aligned, end_aligned;
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200338
Yinghai Luc967da62010-03-28 19:42:55 -0700339 /* Make sure boundaries are page aligned */
340 begin_aligned = PAGE_ALIGN(begin);
341 end_aligned = end & PAGE_MASK;
342
343 if (WARN_ON(begin_aligned != begin || end_aligned != end)) {
344 begin = begin_aligned;
345 end = end_aligned;
346 }
347
348 if (begin >= end)
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200349 return;
350
Yinghai Luc967da62010-03-28 19:42:55 -0700351 addr = begin;
352
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200353 /*
354 * If debugging page accesses then do not free this memory but
355 * mark them not present - any buggy init-section access will
356 * create a kernel page fault:
357 */
358#ifdef CONFIG_DEBUG_PAGEALLOC
359 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
Yinghai Luc967da62010-03-28 19:42:55 -0700360 begin, end);
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200361 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
362#else
363 /*
364 * We just marked the kernel text read only above, now that
365 * we are going to free part of that, we need to make that
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100366 * writeable and non-executable first.
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200367 */
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100368 set_memory_nx(begin, (end - begin) >> PAGE_SHIFT);
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200369 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
370
371 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
372
373 for (; addr < end; addr += PAGE_SIZE) {
374 ClearPageReserved(virt_to_page(addr));
375 init_page_count(virt_to_page(addr));
Yinghai Luc967da62010-03-28 19:42:55 -0700376 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200377 free_page(addr);
378 totalram_pages++;
379 }
380#endif
381}
382
383void free_initmem(void)
384{
385 free_init_pages("unused kernel memory",
386 (unsigned long)(&__init_begin),
387 (unsigned long)(&__init_end));
388}
Pekka Enberg731ddea2009-03-04 11:13:40 +0200389
390#ifdef CONFIG_BLK_DEV_INITRD
391void free_initrd_mem(unsigned long start, unsigned long end)
392{
Yinghai Luc967da62010-03-28 19:42:55 -0700393 /*
394 * end could be not aligned, and We can not align that,
395 * decompresser could be confused by aligned initrd_end
396 * We already reserve the end partial page before in
397 * - i386_start_kernel()
398 * - x86_64_start_kernel()
399 * - relocate_initrd()
400 * So here We can do PAGE_ALIGN() safely to get partial page to be freed
401 */
402 free_init_pages("initrd memory", start, PAGE_ALIGN(end));
Pekka Enberg731ddea2009-03-04 11:13:40 +0200403}
404#endif
Pekka Enberg17623912011-11-01 15:58:22 +0200405
406void __init zone_sizes_init(void)
407{
408 unsigned long max_zone_pfns[MAX_NR_ZONES];
409
410 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
411
412#ifdef CONFIG_ZONE_DMA
413 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
414#endif
415#ifdef CONFIG_ZONE_DMA32
416 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
417#endif
418 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
419#ifdef CONFIG_HIGHMEM
420 max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
421#endif
422
423 free_area_init_nodes(max_zone_pfns);
424}
425