]>
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 | ||
4e33e065 YL |
91 | static int __meminit split_mem_range(struct map_range *mr, int nr_range, |
92 | unsigned long start, | |
93 | unsigned long end) | |
f765090a | 94 | { |
f765090a PE |
95 | unsigned long start_pfn, end_pfn; |
96 | unsigned long pos; | |
4e33e065 | 97 | int i; |
f765090a PE |
98 | |
99 | /* head if not big page alignment ? */ | |
100 | start_pfn = start >> PAGE_SHIFT; | |
101 | pos = start_pfn << PAGE_SHIFT; | |
102 | #ifdef CONFIG_X86_32 | |
103 | /* | |
104 | * Don't use a large page for the first 2/4MB of memory | |
105 | * because there are often fixed size MTRRs in there | |
106 | * and overlapping MTRRs into large pages can cause | |
107 | * slowdowns. | |
108 | */ | |
109 | if (pos == 0) | |
110 | end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT); | |
111 | else | |
112 | end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT) | |
113 | << (PMD_SHIFT - PAGE_SHIFT); | |
114 | #else /* CONFIG_X86_64 */ | |
115 | end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT) | |
116 | << (PMD_SHIFT - PAGE_SHIFT); | |
117 | #endif | |
118 | if (end_pfn > (end >> PAGE_SHIFT)) | |
119 | end_pfn = end >> PAGE_SHIFT; | |
120 | if (start_pfn < end_pfn) { | |
121 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0); | |
122 | pos = end_pfn << PAGE_SHIFT; | |
123 | } | |
124 | ||
125 | /* big page (2M) range */ | |
126 | start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT) | |
127 | << (PMD_SHIFT - PAGE_SHIFT); | |
128 | #ifdef CONFIG_X86_32 | |
129 | end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT); | |
130 | #else /* CONFIG_X86_64 */ | |
131 | end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT) | |
132 | << (PUD_SHIFT - PAGE_SHIFT); | |
133 | if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT))) | |
134 | end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)); | |
135 | #endif | |
136 | ||
137 | if (start_pfn < end_pfn) { | |
138 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, | |
139 | page_size_mask & (1<<PG_LEVEL_2M)); | |
140 | pos = end_pfn << PAGE_SHIFT; | |
141 | } | |
142 | ||
143 | #ifdef CONFIG_X86_64 | |
144 | /* big page (1G) range */ | |
145 | start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT) | |
146 | << (PUD_SHIFT - PAGE_SHIFT); | |
147 | end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT); | |
148 | if (start_pfn < end_pfn) { | |
149 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, | |
150 | page_size_mask & | |
151 | ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G))); | |
152 | pos = end_pfn << PAGE_SHIFT; | |
153 | } | |
154 | ||
155 | /* tail is not big page (1G) alignment */ | |
156 | start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT) | |
157 | << (PMD_SHIFT - PAGE_SHIFT); | |
158 | end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT); | |
159 | if (start_pfn < end_pfn) { | |
160 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, | |
161 | page_size_mask & (1<<PG_LEVEL_2M)); | |
162 | pos = end_pfn << PAGE_SHIFT; | |
163 | } | |
164 | #endif | |
165 | ||
166 | /* tail is not big page (2M) alignment */ | |
167 | start_pfn = pos>>PAGE_SHIFT; | |
168 | end_pfn = end>>PAGE_SHIFT; | |
169 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0); | |
170 | ||
171 | /* try to merge same page size and continuous */ | |
172 | for (i = 0; nr_range > 1 && i < nr_range - 1; i++) { | |
173 | unsigned long old_start; | |
174 | if (mr[i].end != mr[i+1].start || | |
175 | mr[i].page_size_mask != mr[i+1].page_size_mask) | |
176 | continue; | |
177 | /* move it */ | |
178 | old_start = mr[i].start; | |
179 | memmove(&mr[i], &mr[i+1], | |
180 | (nr_range - 1 - i) * sizeof(struct map_range)); | |
181 | mr[i--].start = old_start; | |
182 | nr_range--; | |
183 | } | |
184 | ||
185 | for (i = 0; i < nr_range; i++) | |
365811d6 BH |
186 | printk(KERN_DEBUG " [mem %#010lx-%#010lx] page %s\n", |
187 | mr[i].start, mr[i].end - 1, | |
f765090a PE |
188 | (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":( |
189 | (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k")); | |
190 | ||
4e33e065 YL |
191 | return nr_range; |
192 | } | |
193 | ||
2086fe11 YL |
194 | /* |
195 | * First calculate space needed for kernel direct mapping page tables to cover | |
196 | * mr[0].start to mr[nr_range - 1].end, while accounting for possible 2M and 1GB | |
197 | * pages. Then find enough contiguous space for those page tables. | |
198 | */ | |
199 | static void __init find_early_table_space(struct map_range *mr, int nr_range) | |
200 | { | |
201 | int i; | |
202 | unsigned long puds = 0, pmds = 0, ptes = 0, tables; | |
203 | unsigned long start = 0, good_end; | |
204 | phys_addr_t base; | |
205 | ||
206 | for (i = 0; i < nr_range; i++) { | |
207 | unsigned long range, extra; | |
208 | ||
209 | range = mr[i].end - mr[i].start; | |
210 | puds += (range + PUD_SIZE - 1) >> PUD_SHIFT; | |
211 | ||
212 | if (mr[i].page_size_mask & (1 << PG_LEVEL_1G)) { | |
213 | extra = range - ((range >> PUD_SHIFT) << PUD_SHIFT); | |
214 | pmds += (extra + PMD_SIZE - 1) >> PMD_SHIFT; | |
215 | } else { | |
216 | pmds += (range + PMD_SIZE - 1) >> PMD_SHIFT; | |
217 | } | |
218 | ||
219 | if (mr[i].page_size_mask & (1 << PG_LEVEL_2M)) { | |
220 | extra = range - ((range >> PMD_SHIFT) << PMD_SHIFT); | |
221 | #ifdef CONFIG_X86_32 | |
222 | extra += PMD_SIZE; | |
223 | #endif | |
224 | ptes += (extra + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
225 | } else { | |
226 | ptes += (range + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
227 | } | |
228 | } | |
229 | ||
230 | tables = roundup(puds * sizeof(pud_t), PAGE_SIZE); | |
231 | tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE); | |
232 | tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE); | |
233 | ||
234 | #ifdef CONFIG_X86_32 | |
235 | /* for fixmap */ | |
236 | tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE); | |
237 | #endif | |
238 | good_end = max_pfn_mapped << PAGE_SHIFT; | |
239 | ||
240 | base = memblock_find_in_range(start, good_end, tables, PAGE_SIZE); | |
241 | if (!base) | |
242 | panic("Cannot find space for the kernel page tables"); | |
243 | ||
244 | pgt_buf_start = base >> PAGE_SHIFT; | |
245 | pgt_buf_end = pgt_buf_start; | |
246 | pgt_buf_top = pgt_buf_start + (tables >> PAGE_SHIFT); | |
247 | ||
248 | printk(KERN_DEBUG "kernel direct mapping tables up to %#lx @ [mem %#010lx-%#010lx]\n", | |
249 | mr[nr_range - 1].end - 1, pgt_buf_start << PAGE_SHIFT, | |
250 | (pgt_buf_top << PAGE_SHIFT) - 1); | |
251 | } | |
252 | ||
4e33e065 YL |
253 | /* |
254 | * Setup the direct mapping of the physical memory at PAGE_OFFSET. | |
255 | * This runs before bootmem is initialized and gets pages directly from | |
256 | * the physical memory. To access them they are temporarily mapped. | |
257 | */ | |
258 | unsigned long __init_refok init_memory_mapping(unsigned long start, | |
259 | unsigned long end) | |
260 | { | |
261 | struct map_range mr[NR_RANGE_MR]; | |
262 | unsigned long ret = 0; | |
263 | int nr_range, i; | |
264 | ||
265 | pr_info("init_memory_mapping: [mem %#010lx-%#010lx]\n", | |
266 | start, end - 1); | |
267 | ||
268 | memset(mr, 0, sizeof(mr)); | |
269 | nr_range = split_mem_range(mr, 0, start, end); | |
270 | ||
f765090a PE |
271 | /* |
272 | * Find space for the kernel direct mapping tables. | |
273 | * | |
274 | * Later we should allocate these tables in the local node of the | |
275 | * memory mapped. Unfortunately this is done currently before the | |
276 | * nodes are discovered. | |
277 | */ | |
278 | if (!after_bootmem) | |
844ab6f9 | 279 | find_early_table_space(mr, nr_range); |
f765090a | 280 | |
f765090a PE |
281 | for (i = 0; i < nr_range; i++) |
282 | ret = kernel_physical_mapping_init(mr[i].start, mr[i].end, | |
283 | mr[i].page_size_mask); | |
f765090a PE |
284 | |
285 | #ifdef CONFIG_X86_32 | |
286 | early_ioremap_page_table_range_init(); | |
287 | ||
288 | load_cr3(swapper_pg_dir); | |
289 | #endif | |
290 | ||
f765090a PE |
291 | __flush_tlb_all(); |
292 | ||
279b706b SS |
293 | /* |
294 | * Reserve the kernel pagetable pages we used (pgt_buf_start - | |
295 | * pgt_buf_end) and free the other ones (pgt_buf_end - pgt_buf_top) | |
296 | * so that they can be reused for other purposes. | |
297 | * | |
24aa0788 TH |
298 | * On native it just means calling memblock_reserve, on Xen it also |
299 | * means marking RW the pagetable pages that we allocated before | |
279b706b SS |
300 | * but that haven't been used. |
301 | * | |
302 | * In fact on xen we mark RO the whole range pgt_buf_start - | |
303 | * pgt_buf_top, because we have to make sure that when | |
304 | * init_memory_mapping reaches the pagetable pages area, it maps | |
305 | * RO all the pagetable pages, including the ones that are beyond | |
306 | * pgt_buf_end at that time. | |
307 | */ | |
d1b19426 | 308 | if (!after_bootmem && pgt_buf_end > pgt_buf_start) |
279b706b SS |
309 | x86_init.mapping.pagetable_reserve(PFN_PHYS(pgt_buf_start), |
310 | PFN_PHYS(pgt_buf_end)); | |
f765090a PE |
311 | |
312 | if (!after_bootmem) | |
313 | early_memtest(start, end); | |
314 | ||
315 | return ret >> PAGE_SHIFT; | |
316 | } | |
317 | ||
22ddfcaa YL |
318 | void __init init_mem_mapping(void) |
319 | { | |
320 | probe_page_size_mask(); | |
321 | ||
322 | /* max_pfn_mapped is updated here */ | |
323 | max_low_pfn_mapped = init_memory_mapping(0, max_low_pfn<<PAGE_SHIFT); | |
324 | max_pfn_mapped = max_low_pfn_mapped; | |
325 | ||
326 | #ifdef CONFIG_X86_64 | |
327 | if (max_pfn > max_low_pfn) { | |
328 | max_pfn_mapped = init_memory_mapping(1UL<<32, | |
329 | max_pfn<<PAGE_SHIFT); | |
330 | /* can we preseve max_low_pfn ?*/ | |
331 | max_low_pfn = max_pfn; | |
332 | } | |
333 | #endif | |
334 | } | |
e5b2bb55 | 335 | |
540aca06 PE |
336 | /* |
337 | * devmem_is_allowed() checks to see if /dev/mem access to a certain address | |
338 | * is valid. The argument is a physical page number. | |
339 | * | |
340 | * | |
341 | * On x86, access has to be given to the first megabyte of ram because that area | |
342 | * contains bios code and data regions used by X and dosemu and similar apps. | |
343 | * Access has to be given to non-kernel-ram areas as well, these contain the PCI | |
344 | * mmio resources as well as potential bios/acpi data regions. | |
345 | */ | |
346 | int devmem_is_allowed(unsigned long pagenr) | |
347 | { | |
73e8f3d7 | 348 | if (pagenr < 256) |
540aca06 PE |
349 | return 1; |
350 | if (iomem_is_exclusive(pagenr << PAGE_SHIFT)) | |
351 | return 0; | |
352 | if (!page_is_ram(pagenr)) | |
353 | return 1; | |
354 | return 0; | |
355 | } | |
356 | ||
e5b2bb55 PE |
357 | void free_init_pages(char *what, unsigned long begin, unsigned long end) |
358 | { | |
c967da6a YL |
359 | unsigned long addr; |
360 | unsigned long begin_aligned, end_aligned; | |
e5b2bb55 | 361 | |
c967da6a YL |
362 | /* Make sure boundaries are page aligned */ |
363 | begin_aligned = PAGE_ALIGN(begin); | |
364 | end_aligned = end & PAGE_MASK; | |
365 | ||
366 | if (WARN_ON(begin_aligned != begin || end_aligned != end)) { | |
367 | begin = begin_aligned; | |
368 | end = end_aligned; | |
369 | } | |
370 | ||
371 | if (begin >= end) | |
e5b2bb55 PE |
372 | return; |
373 | ||
c967da6a YL |
374 | addr = begin; |
375 | ||
e5b2bb55 PE |
376 | /* |
377 | * If debugging page accesses then do not free this memory but | |
378 | * mark them not present - any buggy init-section access will | |
379 | * create a kernel page fault: | |
380 | */ | |
381 | #ifdef CONFIG_DEBUG_PAGEALLOC | |
365811d6 BH |
382 | printk(KERN_INFO "debug: unmapping init [mem %#010lx-%#010lx]\n", |
383 | begin, end - 1); | |
e5b2bb55 PE |
384 | set_memory_np(begin, (end - begin) >> PAGE_SHIFT); |
385 | #else | |
386 | /* | |
387 | * We just marked the kernel text read only above, now that | |
388 | * we are going to free part of that, we need to make that | |
5bd5a452 | 389 | * writeable and non-executable first. |
e5b2bb55 | 390 | */ |
5bd5a452 | 391 | set_memory_nx(begin, (end - begin) >> PAGE_SHIFT); |
e5b2bb55 PE |
392 | set_memory_rw(begin, (end - begin) >> PAGE_SHIFT); |
393 | ||
394 | printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10); | |
395 | ||
396 | for (; addr < end; addr += PAGE_SIZE) { | |
397 | ClearPageReserved(virt_to_page(addr)); | |
398 | init_page_count(virt_to_page(addr)); | |
c967da6a | 399 | memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE); |
e5b2bb55 PE |
400 | free_page(addr); |
401 | totalram_pages++; | |
402 | } | |
403 | #endif | |
404 | } | |
405 | ||
406 | void free_initmem(void) | |
407 | { | |
408 | free_init_pages("unused kernel memory", | |
409 | (unsigned long)(&__init_begin), | |
410 | (unsigned long)(&__init_end)); | |
411 | } | |
731ddea6 PE |
412 | |
413 | #ifdef CONFIG_BLK_DEV_INITRD | |
0d26d1d8 | 414 | void __init free_initrd_mem(unsigned long start, unsigned long end) |
731ddea6 | 415 | { |
c967da6a YL |
416 | /* |
417 | * end could be not aligned, and We can not align that, | |
418 | * decompresser could be confused by aligned initrd_end | |
419 | * We already reserve the end partial page before in | |
420 | * - i386_start_kernel() | |
421 | * - x86_64_start_kernel() | |
422 | * - relocate_initrd() | |
423 | * So here We can do PAGE_ALIGN() safely to get partial page to be freed | |
424 | */ | |
425 | free_init_pages("initrd memory", start, PAGE_ALIGN(end)); | |
731ddea6 PE |
426 | } |
427 | #endif | |
17623915 PE |
428 | |
429 | void __init zone_sizes_init(void) | |
430 | { | |
431 | unsigned long max_zone_pfns[MAX_NR_ZONES]; | |
432 | ||
433 | memset(max_zone_pfns, 0, sizeof(max_zone_pfns)); | |
434 | ||
435 | #ifdef CONFIG_ZONE_DMA | |
436 | max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN; | |
437 | #endif | |
438 | #ifdef CONFIG_ZONE_DMA32 | |
439 | max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN; | |
440 | #endif | |
441 | max_zone_pfns[ZONE_NORMAL] = max_low_pfn; | |
442 | #ifdef CONFIG_HIGHMEM | |
443 | max_zone_pfns[ZONE_HIGHMEM] = max_pfn; | |
444 | #endif | |
445 | ||
446 | free_area_init_nodes(max_zone_pfns); | |
447 | } | |
448 |