blob: da591ebc8d1221f191745fb7a9391afb01cc1103 [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;
Pekka Enbergf7650902009-03-05 14:55:05 +020039
Yinghai Lu22ddfca2012-11-16 19:38:41 -080040static void __init probe_page_size_mask(void)
Yinghai Lufa62aaf2012-11-16 19:38:38 -080041{
42#if !defined(CONFIG_DEBUG_PAGEALLOC) && !defined(CONFIG_KMEMCHECK)
43 /*
44 * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
45 * This will simplify cpa(), which otherwise needs to support splitting
46 * large pages into small in interrupt context, etc.
47 */
48 if (direct_gbpages)
49 page_size_mask |= 1 << PG_LEVEL_1G;
50 if (cpu_has_pse)
51 page_size_mask |= 1 << PG_LEVEL_2M;
52#endif
53
54 /* Enable PSE if available */
55 if (cpu_has_pse)
56 set_in_cr4(X86_CR4_PSE);
57
58 /* Enable PGE if available */
59 if (cpu_has_pge) {
60 set_in_cr4(X86_CR4_PGE);
61 __supported_pte_mask |= _PAGE_GLOBAL;
62 }
63}
Sedat Dilek53f80232011-04-17 16:17:34 +020064void __init native_pagetable_reserve(u64 start, u64 end)
Stefano Stabellini279b7062011-04-14 15:49:41 +010065{
Tejun Heo24aa0782011-07-12 11:16:06 +020066 memblock_reserve(start, end - start);
Stefano Stabellini279b7062011-04-14 15:49:41 +010067}
68
Pekka Enbergf7650902009-03-05 14:55:05 +020069#ifdef CONFIG_X86_32
70#define NR_RANGE_MR 3
71#else /* CONFIG_X86_64 */
72#define NR_RANGE_MR 5
73#endif
74
Jan Beulichdc9dd5c2009-03-12 12:40:06 +000075static int __meminit save_mr(struct map_range *mr, int nr_range,
76 unsigned long start_pfn, unsigned long end_pfn,
77 unsigned long page_size_mask)
Pekka Enbergf7650902009-03-05 14:55:05 +020078{
79 if (start_pfn < end_pfn) {
80 if (nr_range >= NR_RANGE_MR)
81 panic("run out of range for init_memory_mapping\n");
82 mr[nr_range].start = start_pfn<<PAGE_SHIFT;
83 mr[nr_range].end = end_pfn<<PAGE_SHIFT;
84 mr[nr_range].page_size_mask = page_size_mask;
85 nr_range++;
86 }
87
88 return nr_range;
89}
90
Yinghai Luaeebe842012-11-16 19:38:55 -080091/*
92 * adjust the page_size_mask for small range to go with
93 * big page size instead small one if nearby are ram too.
94 */
95static void __init_refok adjust_range_page_size_mask(struct map_range *mr,
96 int nr_range)
97{
98 int i;
99
100 for (i = 0; i < nr_range; i++) {
101 if ((page_size_mask & (1<<PG_LEVEL_2M)) &&
102 !(mr[i].page_size_mask & (1<<PG_LEVEL_2M))) {
103 unsigned long start = round_down(mr[i].start, PMD_SIZE);
104 unsigned long end = round_up(mr[i].end, PMD_SIZE);
105
106#ifdef CONFIG_X86_32
107 if ((end >> PAGE_SHIFT) > max_low_pfn)
108 continue;
109#endif
110
111 if (memblock_is_region_memory(start, end - start))
112 mr[i].page_size_mask |= 1<<PG_LEVEL_2M;
113 }
114 if ((page_size_mask & (1<<PG_LEVEL_1G)) &&
115 !(mr[i].page_size_mask & (1<<PG_LEVEL_1G))) {
116 unsigned long start = round_down(mr[i].start, PUD_SIZE);
117 unsigned long end = round_up(mr[i].end, PUD_SIZE);
118
119 if (memblock_is_region_memory(start, end - start))
120 mr[i].page_size_mask |= 1<<PG_LEVEL_1G;
121 }
122 }
123}
124
Yinghai Lu4e33e062012-11-16 19:38:39 -0800125static int __meminit split_mem_range(struct map_range *mr, int nr_range,
126 unsigned long start,
127 unsigned long end)
Pekka Enbergf7650902009-03-05 14:55:05 +0200128{
Pekka Enbergf7650902009-03-05 14:55:05 +0200129 unsigned long start_pfn, end_pfn;
130 unsigned long pos;
Yinghai Lu4e33e062012-11-16 19:38:39 -0800131 int i;
Pekka Enbergf7650902009-03-05 14:55:05 +0200132
133 /* head if not big page alignment ? */
134 start_pfn = start >> PAGE_SHIFT;
135 pos = start_pfn << PAGE_SHIFT;
136#ifdef CONFIG_X86_32
137 /*
138 * Don't use a large page for the first 2/4MB of memory
139 * because there are often fixed size MTRRs in there
140 * and overlapping MTRRs into large pages can cause
141 * slowdowns.
142 */
143 if (pos == 0)
144 end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
145 else
146 end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
147 << (PMD_SHIFT - PAGE_SHIFT);
148#else /* CONFIG_X86_64 */
149 end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT)
150 << (PMD_SHIFT - PAGE_SHIFT);
151#endif
152 if (end_pfn > (end >> PAGE_SHIFT))
153 end_pfn = end >> PAGE_SHIFT;
154 if (start_pfn < end_pfn) {
155 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
156 pos = end_pfn << PAGE_SHIFT;
157 }
158
159 /* big page (2M) range */
160 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
161 << (PMD_SHIFT - PAGE_SHIFT);
162#ifdef CONFIG_X86_32
163 end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
164#else /* CONFIG_X86_64 */
165 end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
166 << (PUD_SHIFT - PAGE_SHIFT);
167 if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)))
168 end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT));
169#endif
170
171 if (start_pfn < end_pfn) {
172 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
173 page_size_mask & (1<<PG_LEVEL_2M));
174 pos = end_pfn << PAGE_SHIFT;
175 }
176
177#ifdef CONFIG_X86_64
178 /* big page (1G) range */
179 start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
180 << (PUD_SHIFT - PAGE_SHIFT);
181 end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
182 if (start_pfn < end_pfn) {
183 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
184 page_size_mask &
185 ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
186 pos = end_pfn << PAGE_SHIFT;
187 }
188
189 /* tail is not big page (1G) alignment */
190 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
191 << (PMD_SHIFT - PAGE_SHIFT);
192 end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
193 if (start_pfn < end_pfn) {
194 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
195 page_size_mask & (1<<PG_LEVEL_2M));
196 pos = end_pfn << PAGE_SHIFT;
197 }
198#endif
199
200 /* tail is not big page (2M) alignment */
201 start_pfn = pos>>PAGE_SHIFT;
202 end_pfn = end>>PAGE_SHIFT;
203 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
204
205 /* try to merge same page size and continuous */
206 for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
207 unsigned long old_start;
208 if (mr[i].end != mr[i+1].start ||
209 mr[i].page_size_mask != mr[i+1].page_size_mask)
210 continue;
211 /* move it */
212 old_start = mr[i].start;
213 memmove(&mr[i], &mr[i+1],
214 (nr_range - 1 - i) * sizeof(struct map_range));
215 mr[i--].start = old_start;
216 nr_range--;
217 }
218
Yinghai Luaeebe842012-11-16 19:38:55 -0800219 if (!after_bootmem)
220 adjust_range_page_size_mask(mr, nr_range);
221
Pekka Enbergf7650902009-03-05 14:55:05 +0200222 for (i = 0; i < nr_range; i++)
Bjorn Helgaas365811d2012-05-29 15:06:29 -0700223 printk(KERN_DEBUG " [mem %#010lx-%#010lx] page %s\n",
224 mr[i].start, mr[i].end - 1,
Pekka Enbergf7650902009-03-05 14:55:05 +0200225 (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
226 (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
227
Yinghai Lu4e33e062012-11-16 19:38:39 -0800228 return nr_range;
229}
230
231/*
Yinghai Lu2086fe12012-11-16 19:38:40 -0800232 * First calculate space needed for kernel direct mapping page tables to cover
233 * mr[0].start to mr[nr_range - 1].end, while accounting for possible 2M and 1GB
234 * pages. Then find enough contiguous space for those page tables.
235 */
Yinghai Luab951932012-11-16 19:38:45 -0800236static unsigned long __init calculate_table_space_size(unsigned long start, unsigned long end)
Yinghai Lu2086fe12012-11-16 19:38:40 -0800237{
238 int i;
239 unsigned long puds = 0, pmds = 0, ptes = 0, tables;
Yinghai Lu84f1ae32012-11-16 19:38:43 -0800240 struct map_range mr[NR_RANGE_MR];
241 int nr_range;
242
243 memset(mr, 0, sizeof(mr));
244 nr_range = 0;
245 nr_range = split_mem_range(mr, nr_range, start, end);
Yinghai Lu2086fe12012-11-16 19:38:40 -0800246
247 for (i = 0; i < nr_range; i++) {
248 unsigned long range, extra;
249
250 range = mr[i].end - mr[i].start;
251 puds += (range + PUD_SIZE - 1) >> PUD_SHIFT;
252
253 if (mr[i].page_size_mask & (1 << PG_LEVEL_1G)) {
254 extra = range - ((range >> PUD_SHIFT) << PUD_SHIFT);
255 pmds += (extra + PMD_SIZE - 1) >> PMD_SHIFT;
256 } else {
257 pmds += (range + PMD_SIZE - 1) >> PMD_SHIFT;
258 }
259
260 if (mr[i].page_size_mask & (1 << PG_LEVEL_2M)) {
261 extra = range - ((range >> PMD_SHIFT) << PMD_SHIFT);
262#ifdef CONFIG_X86_32
263 extra += PMD_SIZE;
264#endif
265 ptes += (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
266 } else {
267 ptes += (range + PAGE_SIZE - 1) >> PAGE_SHIFT;
268 }
269 }
270
271 tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
272 tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
273 tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
274
275#ifdef CONFIG_X86_32
276 /* for fixmap */
277 tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
Yinghai Lu28b6ff62012-11-16 19:38:42 -0800278#endif
Yinghai Lu2086fe12012-11-16 19:38:40 -0800279
Yinghai Luab951932012-11-16 19:38:45 -0800280 return tables;
281}
282
Jacob Shin66520eb2012-11-16 19:38:52 -0800283static unsigned long __init calculate_all_table_space_size(void)
284{
285 unsigned long start_pfn, end_pfn;
286 unsigned long tables;
287 int i;
288
289 /* the ISA range is always mapped regardless of memory holes */
290 tables = calculate_table_space_size(0, ISA_END_ADDRESS);
291
292 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
293 u64 start = start_pfn << PAGE_SHIFT;
294 u64 end = end_pfn << PAGE_SHIFT;
295
296 if (end <= ISA_END_ADDRESS)
297 continue;
298
299 if (start < ISA_END_ADDRESS)
300 start = ISA_END_ADDRESS;
301#ifdef CONFIG_X86_32
302 /* on 32 bit, we only map up to max_low_pfn */
303 if ((start >> PAGE_SHIFT) >= max_low_pfn)
304 continue;
305
306 if ((end >> PAGE_SHIFT) > max_low_pfn)
307 end = max_low_pfn << PAGE_SHIFT;
308#endif
309 tables += calculate_table_space_size(start, end);
310 }
311
312 return tables;
313}
314
Yinghai Luab951932012-11-16 19:38:45 -0800315static void __init find_early_table_space(unsigned long start,
316 unsigned long good_end,
317 unsigned long tables)
318{
319 phys_addr_t base;
320
Yinghai Lu2086fe12012-11-16 19:38:40 -0800321 base = memblock_find_in_range(start, good_end, tables, PAGE_SIZE);
322 if (!base)
323 panic("Cannot find space for the kernel page tables");
324
325 pgt_buf_start = base >> PAGE_SHIFT;
326 pgt_buf_end = pgt_buf_start;
327 pgt_buf_top = pgt_buf_start + (tables >> PAGE_SHIFT);
Yinghai Lu2086fe12012-11-16 19:38:40 -0800328}
329
Jacob Shin66520eb2012-11-16 19:38:52 -0800330static struct range pfn_mapped[E820_X_MAX];
331static int nr_pfn_mapped;
332
333static void add_pfn_range_mapped(unsigned long start_pfn, unsigned long end_pfn)
334{
335 nr_pfn_mapped = add_range_with_merge(pfn_mapped, E820_X_MAX,
336 nr_pfn_mapped, start_pfn, end_pfn);
337 nr_pfn_mapped = clean_sort_range(pfn_mapped, E820_X_MAX);
338
339 max_pfn_mapped = max(max_pfn_mapped, end_pfn);
340
341 if (start_pfn < (1UL<<(32-PAGE_SHIFT)))
342 max_low_pfn_mapped = max(max_low_pfn_mapped,
343 min(end_pfn, 1UL<<(32-PAGE_SHIFT)));
344}
345
346bool pfn_range_is_mapped(unsigned long start_pfn, unsigned long end_pfn)
347{
348 int i;
349
350 for (i = 0; i < nr_pfn_mapped; i++)
351 if ((start_pfn >= pfn_mapped[i].start) &&
352 (end_pfn <= pfn_mapped[i].end))
353 return true;
354
355 return false;
356}
357
Yinghai Lu2086fe12012-11-16 19:38:40 -0800358/*
Yinghai Lu4e33e062012-11-16 19:38:39 -0800359 * Setup the direct mapping of the physical memory at PAGE_OFFSET.
360 * This runs before bootmem is initialized and gets pages directly from
361 * the physical memory. To access them they are temporarily mapped.
362 */
363unsigned long __init_refok init_memory_mapping(unsigned long start,
364 unsigned long end)
365{
366 struct map_range mr[NR_RANGE_MR];
367 unsigned long ret = 0;
368 int nr_range, i;
369
370 pr_info("init_memory_mapping: [mem %#010lx-%#010lx]\n",
371 start, end - 1);
372
373 memset(mr, 0, sizeof(mr));
374 nr_range = split_mem_range(mr, 0, start, end);
375
Pekka Enbergf7650902009-03-05 14:55:05 +0200376 for (i = 0; i < nr_range; i++)
377 ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
378 mr[i].page_size_mask);
Pekka Enbergf7650902009-03-05 14:55:05 +0200379
380#ifdef CONFIG_X86_32
381 early_ioremap_page_table_range_init();
382
383 load_cr3(swapper_pg_dir);
384#endif
385
Pekka Enbergf7650902009-03-05 14:55:05 +0200386 __flush_tlb_all();
387
Jacob Shin66520eb2012-11-16 19:38:52 -0800388 add_pfn_range_mapped(start >> PAGE_SHIFT, ret >> PAGE_SHIFT);
389
Yinghai Luc14fa0b2012-11-16 19:38:44 -0800390 return ret >> PAGE_SHIFT;
391}
392
Jacob Shin66520eb2012-11-16 19:38:52 -0800393/*
394 * Iterate through E820 memory map and create direct mappings for only E820_RAM
395 * regions. We cannot simply create direct mappings for all pfns from
396 * [0 to max_low_pfn) and [4GB to max_pfn) because of possible memory holes in
397 * high addresses that cannot be marked as UC by fixed/variable range MTRRs.
398 * Depending on the alignment of E820 ranges, this may possibly result in using
399 * smaller size (i.e. 4K instead of 2M or 1G) page tables.
400 */
401static void __init init_all_memory_mapping(void)
402{
403 unsigned long start_pfn, end_pfn;
404 int i;
405
406 /* the ISA range is always mapped regardless of memory holes */
407 init_memory_mapping(0, ISA_END_ADDRESS);
408
409 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
410 u64 start = (u64)start_pfn << PAGE_SHIFT;
411 u64 end = (u64)end_pfn << PAGE_SHIFT;
412
413 if (end <= ISA_END_ADDRESS)
414 continue;
415
416 if (start < ISA_END_ADDRESS)
417 start = ISA_END_ADDRESS;
418#ifdef CONFIG_X86_32
419 /* on 32 bit, we only map up to max_low_pfn */
420 if ((start >> PAGE_SHIFT) >= max_low_pfn)
421 continue;
422
423 if ((end >> PAGE_SHIFT) > max_low_pfn)
424 end = max_low_pfn << PAGE_SHIFT;
425#endif
426 init_memory_mapping(start, end);
427 }
428
429#ifdef CONFIG_X86_64
430 if (max_pfn > max_low_pfn) {
431 /* can we preseve max_low_pfn ?*/
432 max_low_pfn = max_pfn;
433 }
434#endif
435}
436
Yinghai Luc14fa0b2012-11-16 19:38:44 -0800437void __init init_mem_mapping(void)
438{
Yinghai Luab951932012-11-16 19:38:45 -0800439 unsigned long tables, good_end, end;
440
Yinghai Luc14fa0b2012-11-16 19:38:44 -0800441 probe_page_size_mask();
442
443 /*
444 * Find space for the kernel direct mapping tables.
445 *
446 * Later we should allocate these tables in the local node of the
447 * memory mapped. Unfortunately this is done currently before the
448 * nodes are discovered.
449 */
450#ifdef CONFIG_X86_64
Yinghai Luab951932012-11-16 19:38:45 -0800451 end = max_pfn << PAGE_SHIFT;
452 good_end = end;
Yinghai Luc14fa0b2012-11-16 19:38:44 -0800453#else
Yinghai Luab951932012-11-16 19:38:45 -0800454 end = max_low_pfn << PAGE_SHIFT;
455 good_end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Luc14fa0b2012-11-16 19:38:44 -0800456#endif
Jacob Shin66520eb2012-11-16 19:38:52 -0800457 tables = calculate_all_table_space_size();
Yinghai Luab951932012-11-16 19:38:45 -0800458 find_early_table_space(0, good_end, tables);
459 printk(KERN_DEBUG "kernel direct mapping tables up to %#lx @ [mem %#010lx-%#010lx] prealloc\n",
460 end - 1, pgt_buf_start << PAGE_SHIFT,
461 (pgt_buf_top << PAGE_SHIFT) - 1);
462
Jacob Shin66520eb2012-11-16 19:38:52 -0800463 max_pfn_mapped = 0; /* will get exact value next */
464 init_all_memory_mapping();
Yinghai Luc14fa0b2012-11-16 19:38:44 -0800465
Stefano Stabellini279b7062011-04-14 15:49:41 +0100466 /*
467 * Reserve the kernel pagetable pages we used (pgt_buf_start -
468 * pgt_buf_end) and free the other ones (pgt_buf_end - pgt_buf_top)
469 * so that they can be reused for other purposes.
470 *
Tejun Heo24aa0782011-07-12 11:16:06 +0200471 * On native it just means calling memblock_reserve, on Xen it also
472 * means marking RW the pagetable pages that we allocated before
Stefano Stabellini279b7062011-04-14 15:49:41 +0100473 * but that haven't been used.
474 *
475 * In fact on xen we mark RO the whole range pgt_buf_start -
476 * pgt_buf_top, because we have to make sure that when
477 * init_memory_mapping reaches the pagetable pages area, it maps
478 * RO all the pagetable pages, including the ones that are beyond
479 * pgt_buf_end at that time.
480 */
Yinghai Luab951932012-11-16 19:38:45 -0800481 if (pgt_buf_end > pgt_buf_start) {
482 printk(KERN_DEBUG "kernel direct mapping tables up to %#lx @ [mem %#010lx-%#010lx] final\n",
483 end - 1, pgt_buf_start << PAGE_SHIFT,
484 (pgt_buf_end << PAGE_SHIFT) - 1);
Stefano Stabellini279b7062011-04-14 15:49:41 +0100485 x86_init.mapping.pagetable_reserve(PFN_PHYS(pgt_buf_start),
486 PFN_PHYS(pgt_buf_end));
Yinghai Luab951932012-11-16 19:38:45 -0800487 }
Pekka Enbergf7650902009-03-05 14:55:05 +0200488
Yinghai Luc14fa0b2012-11-16 19:38:44 -0800489 /* stop the wrong using */
490 pgt_buf_top = 0;
Pekka Enbergf7650902009-03-05 14:55:05 +0200491
Yinghai Luc14fa0b2012-11-16 19:38:44 -0800492 early_memtest(0, max_pfn_mapped << PAGE_SHIFT);
Yinghai Lu22ddfca2012-11-16 19:38:41 -0800493}
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200494
Pekka Enberg540aca02009-03-04 11:46:40 +0200495/*
496 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
497 * is valid. The argument is a physical page number.
498 *
499 *
500 * On x86, access has to be given to the first megabyte of ram because that area
501 * contains bios code and data regions used by X and dosemu and similar apps.
502 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
503 * mmio resources as well as potential bios/acpi data regions.
504 */
505int devmem_is_allowed(unsigned long pagenr)
506{
T Makphaibulchoke73e8f3d2012-08-28 21:21:43 -0600507 if (pagenr < 256)
Pekka Enberg540aca02009-03-04 11:46:40 +0200508 return 1;
509 if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
510 return 0;
511 if (!page_is_ram(pagenr))
512 return 1;
513 return 0;
514}
515
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200516void free_init_pages(char *what, unsigned long begin, unsigned long end)
517{
Yinghai Luc967da62010-03-28 19:42:55 -0700518 unsigned long addr;
519 unsigned long begin_aligned, end_aligned;
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200520
Yinghai Luc967da62010-03-28 19:42:55 -0700521 /* Make sure boundaries are page aligned */
522 begin_aligned = PAGE_ALIGN(begin);
523 end_aligned = end & PAGE_MASK;
524
525 if (WARN_ON(begin_aligned != begin || end_aligned != end)) {
526 begin = begin_aligned;
527 end = end_aligned;
528 }
529
530 if (begin >= end)
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200531 return;
532
Yinghai Luc967da62010-03-28 19:42:55 -0700533 addr = begin;
534
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200535 /*
536 * If debugging page accesses then do not free this memory but
537 * mark them not present - any buggy init-section access will
538 * create a kernel page fault:
539 */
540#ifdef CONFIG_DEBUG_PAGEALLOC
Bjorn Helgaas365811d2012-05-29 15:06:29 -0700541 printk(KERN_INFO "debug: unmapping init [mem %#010lx-%#010lx]\n",
542 begin, end - 1);
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200543 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
544#else
545 /*
546 * We just marked the kernel text read only above, now that
547 * we are going to free part of that, we need to make that
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100548 * writeable and non-executable first.
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200549 */
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100550 set_memory_nx(begin, (end - begin) >> PAGE_SHIFT);
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200551 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
552
553 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
554
555 for (; addr < end; addr += PAGE_SIZE) {
556 ClearPageReserved(virt_to_page(addr));
557 init_page_count(virt_to_page(addr));
Yinghai Luc967da62010-03-28 19:42:55 -0700558 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200559 free_page(addr);
560 totalram_pages++;
561 }
562#endif
563}
564
565void free_initmem(void)
566{
567 free_init_pages("unused kernel memory",
568 (unsigned long)(&__init_begin),
569 (unsigned long)(&__init_end));
570}
Pekka Enberg731ddea2009-03-04 11:13:40 +0200571
572#ifdef CONFIG_BLK_DEV_INITRD
Jan Beulich0d26d1d2012-06-18 11:30:20 +0100573void __init free_initrd_mem(unsigned long start, unsigned long end)
Pekka Enberg731ddea2009-03-04 11:13:40 +0200574{
Yinghai Luc967da62010-03-28 19:42:55 -0700575 /*
576 * end could be not aligned, and We can not align that,
577 * decompresser could be confused by aligned initrd_end
578 * We already reserve the end partial page before in
579 * - i386_start_kernel()
580 * - x86_64_start_kernel()
581 * - relocate_initrd()
582 * So here We can do PAGE_ALIGN() safely to get partial page to be freed
583 */
584 free_init_pages("initrd memory", start, PAGE_ALIGN(end));
Pekka Enberg731ddea2009-03-04 11:13:40 +0200585}
586#endif
Pekka Enberg17623912011-11-01 15:58:22 +0200587
588void __init zone_sizes_init(void)
589{
590 unsigned long max_zone_pfns[MAX_NR_ZONES];
591
592 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
593
594#ifdef CONFIG_ZONE_DMA
595 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
596#endif
597#ifdef CONFIG_ZONE_DMA32
598 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
599#endif
600 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
601#ifdef CONFIG_HIGHMEM
602 max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
603#endif
604
605 free_area_init_nodes(max_zone_pfns);
606}
607