blob: c688ea3887f20643e3afd9b1b3839e10161be14e [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 */
Yinghai Luf763ad12012-11-16 19:38:57 -0800401static void __init init_range_memory_mapping(unsigned long range_start,
402 unsigned long range_end)
Jacob Shin66520eb2012-11-16 19:38:52 -0800403{
404 unsigned long start_pfn, end_pfn;
405 int i;
406
Jacob Shin66520eb2012-11-16 19:38:52 -0800407 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
408 u64 start = (u64)start_pfn << PAGE_SHIFT;
409 u64 end = (u64)end_pfn << PAGE_SHIFT;
410
Yinghai Luf763ad12012-11-16 19:38:57 -0800411 if (end <= range_start)
Jacob Shin66520eb2012-11-16 19:38:52 -0800412 continue;
413
Yinghai Luf763ad12012-11-16 19:38:57 -0800414 if (start < range_start)
415 start = range_start;
416
417 if (start >= range_end)
Jacob Shin66520eb2012-11-16 19:38:52 -0800418 continue;
419
Yinghai Luf763ad12012-11-16 19:38:57 -0800420 if (end > range_end)
421 end = range_end;
422
Jacob Shin66520eb2012-11-16 19:38:52 -0800423 init_memory_mapping(start, end);
424 }
Jacob Shin66520eb2012-11-16 19:38:52 -0800425}
426
Yinghai Luc14fa0b2012-11-16 19:38:44 -0800427void __init init_mem_mapping(void)
428{
Yinghai Luab951932012-11-16 19:38:45 -0800429 unsigned long tables, good_end, end;
430
Yinghai Luc14fa0b2012-11-16 19:38:44 -0800431 probe_page_size_mask();
432
433 /*
434 * Find space for the kernel direct mapping tables.
435 *
436 * Later we should allocate these tables in the local node of the
437 * memory mapped. Unfortunately this is done currently before the
438 * nodes are discovered.
439 */
440#ifdef CONFIG_X86_64
Yinghai Luab951932012-11-16 19:38:45 -0800441 end = max_pfn << PAGE_SHIFT;
442 good_end = end;
Yinghai Luc14fa0b2012-11-16 19:38:44 -0800443#else
Yinghai Luab951932012-11-16 19:38:45 -0800444 end = max_low_pfn << PAGE_SHIFT;
445 good_end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Luc14fa0b2012-11-16 19:38:44 -0800446#endif
Jacob Shin66520eb2012-11-16 19:38:52 -0800447 tables = calculate_all_table_space_size();
Yinghai Luab951932012-11-16 19:38:45 -0800448 find_early_table_space(0, good_end, tables);
449 printk(KERN_DEBUG "kernel direct mapping tables up to %#lx @ [mem %#010lx-%#010lx] prealloc\n",
450 end - 1, pgt_buf_start << PAGE_SHIFT,
451 (pgt_buf_top << PAGE_SHIFT) - 1);
452
Jacob Shin66520eb2012-11-16 19:38:52 -0800453 max_pfn_mapped = 0; /* will get exact value next */
Yinghai Luf763ad12012-11-16 19:38:57 -0800454 /* the ISA range is always mapped regardless of memory holes */
455 init_memory_mapping(0, ISA_END_ADDRESS);
456 init_range_memory_mapping(ISA_END_ADDRESS, end);
457#ifdef CONFIG_X86_64
458 if (max_pfn > max_low_pfn) {
459 /* can we preseve max_low_pfn ?*/
460 max_low_pfn = max_pfn;
461 }
462#endif
Stefano Stabellini279b7062011-04-14 15:49:41 +0100463 /*
464 * Reserve the kernel pagetable pages we used (pgt_buf_start -
465 * pgt_buf_end) and free the other ones (pgt_buf_end - pgt_buf_top)
466 * so that they can be reused for other purposes.
467 *
Tejun Heo24aa0782011-07-12 11:16:06 +0200468 * On native it just means calling memblock_reserve, on Xen it also
469 * means marking RW the pagetable pages that we allocated before
Stefano Stabellini279b7062011-04-14 15:49:41 +0100470 * but that haven't been used.
471 *
472 * In fact on xen we mark RO the whole range pgt_buf_start -
473 * pgt_buf_top, because we have to make sure that when
474 * init_memory_mapping reaches the pagetable pages area, it maps
475 * RO all the pagetable pages, including the ones that are beyond
476 * pgt_buf_end at that time.
477 */
Yinghai Luab951932012-11-16 19:38:45 -0800478 if (pgt_buf_end > pgt_buf_start) {
479 printk(KERN_DEBUG "kernel direct mapping tables up to %#lx @ [mem %#010lx-%#010lx] final\n",
480 end - 1, pgt_buf_start << PAGE_SHIFT,
481 (pgt_buf_end << PAGE_SHIFT) - 1);
Stefano Stabellini279b7062011-04-14 15:49:41 +0100482 x86_init.mapping.pagetable_reserve(PFN_PHYS(pgt_buf_start),
483 PFN_PHYS(pgt_buf_end));
Yinghai Luab951932012-11-16 19:38:45 -0800484 }
Pekka Enbergf7650902009-03-05 14:55:05 +0200485
Yinghai Luc14fa0b2012-11-16 19:38:44 -0800486 /* stop the wrong using */
487 pgt_buf_top = 0;
Pekka Enbergf7650902009-03-05 14:55:05 +0200488
Yinghai Luc14fa0b2012-11-16 19:38:44 -0800489 early_memtest(0, max_pfn_mapped << PAGE_SHIFT);
Yinghai Lu22ddfca2012-11-16 19:38:41 -0800490}
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200491
Pekka Enberg540aca02009-03-04 11:46:40 +0200492/*
493 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
494 * is valid. The argument is a physical page number.
495 *
496 *
497 * On x86, access has to be given to the first megabyte of ram because that area
498 * contains bios code and data regions used by X and dosemu and similar apps.
499 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
500 * mmio resources as well as potential bios/acpi data regions.
501 */
502int devmem_is_allowed(unsigned long pagenr)
503{
T Makphaibulchoke73e8f3d2012-08-28 21:21:43 -0600504 if (pagenr < 256)
Pekka Enberg540aca02009-03-04 11:46:40 +0200505 return 1;
506 if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
507 return 0;
508 if (!page_is_ram(pagenr))
509 return 1;
510 return 0;
511}
512
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200513void free_init_pages(char *what, unsigned long begin, unsigned long end)
514{
Yinghai Luc967da62010-03-28 19:42:55 -0700515 unsigned long addr;
516 unsigned long begin_aligned, end_aligned;
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200517
Yinghai Luc967da62010-03-28 19:42:55 -0700518 /* Make sure boundaries are page aligned */
519 begin_aligned = PAGE_ALIGN(begin);
520 end_aligned = end & PAGE_MASK;
521
522 if (WARN_ON(begin_aligned != begin || end_aligned != end)) {
523 begin = begin_aligned;
524 end = end_aligned;
525 }
526
527 if (begin >= end)
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200528 return;
529
Yinghai Luc967da62010-03-28 19:42:55 -0700530 addr = begin;
531
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200532 /*
533 * If debugging page accesses then do not free this memory but
534 * mark them not present - any buggy init-section access will
535 * create a kernel page fault:
536 */
537#ifdef CONFIG_DEBUG_PAGEALLOC
Bjorn Helgaas365811d2012-05-29 15:06:29 -0700538 printk(KERN_INFO "debug: unmapping init [mem %#010lx-%#010lx]\n",
539 begin, end - 1);
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200540 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
541#else
542 /*
543 * We just marked the kernel text read only above, now that
544 * we are going to free part of that, we need to make that
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100545 * writeable and non-executable first.
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200546 */
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100547 set_memory_nx(begin, (end - begin) >> PAGE_SHIFT);
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200548 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
549
550 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
551
552 for (; addr < end; addr += PAGE_SIZE) {
553 ClearPageReserved(virt_to_page(addr));
554 init_page_count(virt_to_page(addr));
Yinghai Luc967da62010-03-28 19:42:55 -0700555 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200556 free_page(addr);
557 totalram_pages++;
558 }
559#endif
560}
561
562void free_initmem(void)
563{
564 free_init_pages("unused kernel memory",
565 (unsigned long)(&__init_begin),
566 (unsigned long)(&__init_end));
567}
Pekka Enberg731ddea2009-03-04 11:13:40 +0200568
569#ifdef CONFIG_BLK_DEV_INITRD
Jan Beulich0d26d1d2012-06-18 11:30:20 +0100570void __init free_initrd_mem(unsigned long start, unsigned long end)
Pekka Enberg731ddea2009-03-04 11:13:40 +0200571{
Yinghai Luc967da62010-03-28 19:42:55 -0700572 /*
573 * end could be not aligned, and We can not align that,
574 * decompresser could be confused by aligned initrd_end
575 * We already reserve the end partial page before in
576 * - i386_start_kernel()
577 * - x86_64_start_kernel()
578 * - relocate_initrd()
579 * So here We can do PAGE_ALIGN() safely to get partial page to be freed
580 */
581 free_init_pages("initrd memory", start, PAGE_ALIGN(end));
Pekka Enberg731ddea2009-03-04 11:13:40 +0200582}
583#endif
Pekka Enberg17623912011-11-01 15:58:22 +0200584
585void __init zone_sizes_init(void)
586{
587 unsigned long max_zone_pfns[MAX_NR_ZONES];
588
589 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
590
591#ifdef CONFIG_ZONE_DMA
592 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
593#endif
594#ifdef CONFIG_ZONE_DMA32
595 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
596#endif
597 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
598#ifdef CONFIG_HIGHMEM
599 max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
600#endif
601
602 free_area_init_nodes(max_zone_pfns);
603}
604