]>
Commit | Line | Data |
---|---|---|
5a0e3ad6 | 1 | #include <linux/gfp.h> |
2c1b284e | 2 | #include <linux/initrd.h> |
540aca06 | 3 | #include <linux/ioport.h> |
e5b2bb55 | 4 | #include <linux/swap.h> |
a9ce6bc1 | 5 | #include <linux/memblock.h> |
17623915 | 6 | #include <linux/bootmem.h> /* for max_low_pfn */ |
540aca06 | 7 | |
e5b2bb55 | 8 | #include <asm/cacheflush.h> |
f765090a | 9 | #include <asm/e820.h> |
4fcb2083 | 10 | #include <asm/init.h> |
e5b2bb55 | 11 | #include <asm/page.h> |
540aca06 | 12 | #include <asm/page_types.h> |
e5b2bb55 | 13 | #include <asm/sections.h> |
49834396 | 14 | #include <asm/setup.h> |
f765090a | 15 | #include <asm/tlbflush.h> |
9518e0e4 | 16 | #include <asm/tlb.h> |
76c06927 | 17 | #include <asm/proto.h> |
17623915 | 18 | #include <asm/dma.h> /* for MAX_DMA_PFN */ |
9518e0e4 | 19 | |
d1b19426 YL |
20 | unsigned long __initdata pgt_buf_start; |
21 | unsigned long __meminitdata pgt_buf_end; | |
22 | unsigned long __meminitdata pgt_buf_top; | |
f765090a PE |
23 | |
24 | int after_bootmem; | |
25 | ||
26 | int direct_gbpages | |
27 | #ifdef CONFIG_DIRECT_GBPAGES | |
28 | = 1 | |
29 | #endif | |
30 | ; | |
31 | ||
844ab6f9 JS |
32 | struct map_range { |
33 | unsigned long start; | |
34 | unsigned long end; | |
35 | unsigned page_size_mask; | |
36 | }; | |
37 | ||
fa62aafe | 38 | static int page_size_mask; |
f765090a | 39 | |
22ddfcaa | 40 | static void __init probe_page_size_mask(void) |
fa62aafe YL |
41 | { |
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 | } | |
53f8023f | 64 | void __init native_pagetable_reserve(u64 start, u64 end) |
279b706b | 65 | { |
24aa0788 | 66 | memblock_reserve(start, end - start); |
279b706b SS |
67 | } |
68 | ||
f765090a PE |
69 | #ifdef CONFIG_X86_32 |
70 | #define NR_RANGE_MR 3 | |
71 | #else /* CONFIG_X86_64 */ | |
72 | #define NR_RANGE_MR 5 | |
73 | #endif | |
74 | ||
dc9dd5cc JB |
75 | static 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) | |
f765090a PE |
78 | { |
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 | ||
aeebe84c YL |
91 | /* |
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 | */ | |
95 | static 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 | ||
4e33e065 YL |
125 | static int __meminit split_mem_range(struct map_range *mr, int nr_range, |
126 | unsigned long start, | |
127 | unsigned long end) | |
f765090a | 128 | { |
f765090a PE |
129 | unsigned long start_pfn, end_pfn; |
130 | unsigned long pos; | |
4e33e065 | 131 | int i; |
f765090a PE |
132 | |
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 | ||
aeebe84c YL |
219 | if (!after_bootmem) |
220 | adjust_range_page_size_mask(mr, nr_range); | |
221 | ||
f765090a | 222 | for (i = 0; i < nr_range; i++) |
365811d6 BH |
223 | printk(KERN_DEBUG " [mem %#010lx-%#010lx] page %s\n", |
224 | mr[i].start, mr[i].end - 1, | |
f765090a PE |
225 | (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":( |
226 | (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k")); | |
227 | ||
4e33e065 YL |
228 | return nr_range; |
229 | } | |
230 | ||
2086fe11 YL |
231 | /* |
232 | * 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 | */ | |
ab951937 | 236 | static unsigned long __init calculate_table_space_size(unsigned long start, unsigned long end) |
2086fe11 YL |
237 | { |
238 | int i; | |
239 | unsigned long puds = 0, pmds = 0, ptes = 0, tables; | |
84f1ae30 YL |
240 | 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); | |
2086fe11 YL |
246 | |
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); | |
28b6ff66 | 278 | #endif |
2086fe11 | 279 | |
ab951937 YL |
280 | return tables; |
281 | } | |
282 | ||
66520ebc JS |
283 | static 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 | ||
ab951937 YL |
315 | static 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 | ||
2086fe11 YL |
321 | 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); | |
2086fe11 YL |
328 | } |
329 | ||
66520ebc JS |
330 | static struct range pfn_mapped[E820_X_MAX]; |
331 | static int nr_pfn_mapped; | |
332 | ||
333 | static 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 | ||
346 | bool 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 | ||
4e33e065 YL |
358 | /* |
359 | * 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 | */ | |
363 | unsigned 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 | ||
f765090a PE |
376 | 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); | |
f765090a PE |
379 | |
380 | #ifdef CONFIG_X86_32 | |
381 | early_ioremap_page_table_range_init(); | |
382 | ||
383 | load_cr3(swapper_pg_dir); | |
384 | #endif | |
385 | ||
f765090a PE |
386 | __flush_tlb_all(); |
387 | ||
66520ebc JS |
388 | add_pfn_range_mapped(start >> PAGE_SHIFT, ret >> PAGE_SHIFT); |
389 | ||
c14fa0b6 YL |
390 | return ret >> PAGE_SHIFT; |
391 | } | |
392 | ||
66520ebc JS |
393 | /* |
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 | */ | |
f763ad1d YL |
401 | static void __init init_range_memory_mapping(unsigned long range_start, |
402 | unsigned long range_end) | |
66520ebc JS |
403 | { |
404 | unsigned long start_pfn, end_pfn; | |
405 | int i; | |
406 | ||
66520ebc JS |
407 | 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 | ||
f763ad1d | 411 | if (end <= range_start) |
66520ebc JS |
412 | continue; |
413 | ||
f763ad1d YL |
414 | if (start < range_start) |
415 | start = range_start; | |
416 | ||
417 | if (start >= range_end) | |
66520ebc JS |
418 | continue; |
419 | ||
f763ad1d YL |
420 | if (end > range_end) |
421 | end = range_end; | |
66520ebc | 422 | |
f763ad1d | 423 | init_memory_mapping(start, end); |
66520ebc | 424 | } |
66520ebc JS |
425 | } |
426 | ||
c14fa0b6 YL |
427 | void __init init_mem_mapping(void) |
428 | { | |
ab951937 YL |
429 | unsigned long tables, good_end, end; |
430 | ||
c14fa0b6 YL |
431 | 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 | |
ab951937 YL |
441 | end = max_pfn << PAGE_SHIFT; |
442 | good_end = end; | |
c14fa0b6 | 443 | #else |
ab951937 YL |
444 | end = max_low_pfn << PAGE_SHIFT; |
445 | good_end = max_pfn_mapped << PAGE_SHIFT; | |
c14fa0b6 | 446 | #endif |
66520ebc | 447 | tables = calculate_all_table_space_size(); |
ab951937 YL |
448 | 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 | ||
66520ebc | 453 | max_pfn_mapped = 0; /* will get exact value next */ |
f763ad1d YL |
454 | /* 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 | |
279b706b SS |
463 | /* |
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 | * | |
24aa0788 TH |
468 | * On native it just means calling memblock_reserve, on Xen it also |
469 | * means marking RW the pagetable pages that we allocated before | |
279b706b SS |
470 | * 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 | */ | |
ab951937 YL |
478 | 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); | |
279b706b SS |
482 | x86_init.mapping.pagetable_reserve(PFN_PHYS(pgt_buf_start), |
483 | PFN_PHYS(pgt_buf_end)); | |
ab951937 | 484 | } |
f765090a | 485 | |
c14fa0b6 YL |
486 | /* stop the wrong using */ |
487 | pgt_buf_top = 0; | |
22ddfcaa | 488 | |
c14fa0b6 | 489 | early_memtest(0, max_pfn_mapped << PAGE_SHIFT); |
22ddfcaa | 490 | } |
e5b2bb55 | 491 | |
540aca06 PE |
492 | /* |
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 | */ | |
502 | int devmem_is_allowed(unsigned long pagenr) | |
503 | { | |
73e8f3d7 | 504 | if (pagenr < 256) |
540aca06 PE |
505 | 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 | ||
e5b2bb55 PE |
513 | void free_init_pages(char *what, unsigned long begin, unsigned long end) |
514 | { | |
c967da6a YL |
515 | unsigned long addr; |
516 | unsigned long begin_aligned, end_aligned; | |
e5b2bb55 | 517 | |
c967da6a YL |
518 | /* 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) | |
e5b2bb55 PE |
528 | return; |
529 | ||
c967da6a YL |
530 | addr = begin; |
531 | ||
e5b2bb55 PE |
532 | /* |
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 | |
365811d6 BH |
538 | printk(KERN_INFO "debug: unmapping init [mem %#010lx-%#010lx]\n", |
539 | begin, end - 1); | |
e5b2bb55 PE |
540 | 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 | |
5bd5a452 | 545 | * writeable and non-executable first. |
e5b2bb55 | 546 | */ |
5bd5a452 | 547 | set_memory_nx(begin, (end - begin) >> PAGE_SHIFT); |
e5b2bb55 PE |
548 | 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)); | |
c967da6a | 555 | memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE); |
e5b2bb55 PE |
556 | free_page(addr); |
557 | totalram_pages++; | |
558 | } | |
559 | #endif | |
560 | } | |
561 | ||
562 | void free_initmem(void) | |
563 | { | |
564 | free_init_pages("unused kernel memory", | |
565 | (unsigned long)(&__init_begin), | |
566 | (unsigned long)(&__init_end)); | |
567 | } | |
731ddea6 PE |
568 | |
569 | #ifdef CONFIG_BLK_DEV_INITRD | |
0d26d1d8 | 570 | void __init free_initrd_mem(unsigned long start, unsigned long end) |
731ddea6 | 571 | { |
c967da6a YL |
572 | /* |
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)); | |
731ddea6 PE |
582 | } |
583 | #endif | |
17623915 PE |
584 | |
585 | void __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 |