IPVS: netns, svc counters moved in ip_vs_ctl,c
[linux/fpc-iii.git] / arch / x86 / mm / init.c
blob947f42abe820eed9e47388ff3fcfd6fc937bb96a
1 #include <linux/gfp.h>
2 #include <linux/initrd.h>
3 #include <linux/ioport.h>
4 #include <linux/swap.h>
5 #include <linux/memblock.h>
7 #include <asm/cacheflush.h>
8 #include <asm/e820.h>
9 #include <asm/init.h>
10 #include <asm/page.h>
11 #include <asm/page_types.h>
12 #include <asm/sections.h>
13 #include <asm/setup.h>
14 #include <asm/system.h>
15 #include <asm/tlbflush.h>
16 #include <asm/tlb.h>
17 #include <asm/proto.h>
19 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
21 unsigned long __initdata e820_table_start;
22 unsigned long __meminitdata e820_table_end;
23 unsigned long __meminitdata e820_table_top;
25 int after_bootmem;
27 int direct_gbpages
28 #ifdef CONFIG_DIRECT_GBPAGES
29 = 1
30 #endif
33 static void __init find_early_table_space(unsigned long end, int use_pse,
34 int use_gbpages)
36 unsigned long puds, pmds, ptes, tables, start;
37 phys_addr_t base;
39 puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
40 tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
42 if (use_gbpages) {
43 unsigned long extra;
45 extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT);
46 pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT;
47 } else
48 pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
50 tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
52 if (use_pse) {
53 unsigned long extra;
55 extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
56 #ifdef CONFIG_X86_32
57 extra += PMD_SIZE;
58 #endif
59 ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
60 } else
61 ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
63 tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
65 #ifdef CONFIG_X86_32
66 /* for fixmap */
67 tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
68 #endif
71 * RED-PEN putting page tables only on node 0 could
72 * cause a hotspot and fill up ZONE_DMA. The page tables
73 * need roughly 0.5KB per GB.
75 #ifdef CONFIG_X86_32
76 start = 0x7000;
77 #else
78 start = 0x8000;
79 #endif
80 base = memblock_find_in_range(start, max_pfn_mapped<<PAGE_SHIFT,
81 tables, PAGE_SIZE);
82 if (base == MEMBLOCK_ERROR)
83 panic("Cannot find space for the kernel page tables");
85 e820_table_start = base >> PAGE_SHIFT;
86 e820_table_end = e820_table_start;
87 e820_table_top = e820_table_start + (tables >> PAGE_SHIFT);
89 printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
90 end, e820_table_start << PAGE_SHIFT, e820_table_top << PAGE_SHIFT);
93 struct map_range {
94 unsigned long start;
95 unsigned long end;
96 unsigned page_size_mask;
99 #ifdef CONFIG_X86_32
100 #define NR_RANGE_MR 3
101 #else /* CONFIG_X86_64 */
102 #define NR_RANGE_MR 5
103 #endif
105 static int __meminit save_mr(struct map_range *mr, int nr_range,
106 unsigned long start_pfn, unsigned long end_pfn,
107 unsigned long page_size_mask)
109 if (start_pfn < end_pfn) {
110 if (nr_range >= NR_RANGE_MR)
111 panic("run out of range for init_memory_mapping\n");
112 mr[nr_range].start = start_pfn<<PAGE_SHIFT;
113 mr[nr_range].end = end_pfn<<PAGE_SHIFT;
114 mr[nr_range].page_size_mask = page_size_mask;
115 nr_range++;
118 return nr_range;
122 * Setup the direct mapping of the physical memory at PAGE_OFFSET.
123 * This runs before bootmem is initialized and gets pages directly from
124 * the physical memory. To access them they are temporarily mapped.
126 unsigned long __init_refok init_memory_mapping(unsigned long start,
127 unsigned long end)
129 unsigned long page_size_mask = 0;
130 unsigned long start_pfn, end_pfn;
131 unsigned long ret = 0;
132 unsigned long pos;
134 struct map_range mr[NR_RANGE_MR];
135 int nr_range, i;
136 int use_pse, use_gbpages;
138 printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end);
140 #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
142 * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
143 * This will simplify cpa(), which otherwise needs to support splitting
144 * large pages into small in interrupt context, etc.
146 use_pse = use_gbpages = 0;
147 #else
148 use_pse = cpu_has_pse;
149 use_gbpages = direct_gbpages;
150 #endif
152 /* Enable PSE if available */
153 if (cpu_has_pse)
154 set_in_cr4(X86_CR4_PSE);
156 /* Enable PGE if available */
157 if (cpu_has_pge) {
158 set_in_cr4(X86_CR4_PGE);
159 __supported_pte_mask |= _PAGE_GLOBAL;
162 if (use_gbpages)
163 page_size_mask |= 1 << PG_LEVEL_1G;
164 if (use_pse)
165 page_size_mask |= 1 << PG_LEVEL_2M;
167 memset(mr, 0, sizeof(mr));
168 nr_range = 0;
170 /* head if not big page alignment ? */
171 start_pfn = start >> PAGE_SHIFT;
172 pos = start_pfn << PAGE_SHIFT;
173 #ifdef CONFIG_X86_32
175 * Don't use a large page for the first 2/4MB of memory
176 * because there are often fixed size MTRRs in there
177 * and overlapping MTRRs into large pages can cause
178 * slowdowns.
180 if (pos == 0)
181 end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
182 else
183 end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
184 << (PMD_SHIFT - PAGE_SHIFT);
185 #else /* CONFIG_X86_64 */
186 end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT)
187 << (PMD_SHIFT - PAGE_SHIFT);
188 #endif
189 if (end_pfn > (end >> PAGE_SHIFT))
190 end_pfn = end >> PAGE_SHIFT;
191 if (start_pfn < end_pfn) {
192 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
193 pos = end_pfn << PAGE_SHIFT;
196 /* big page (2M) range */
197 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
198 << (PMD_SHIFT - PAGE_SHIFT);
199 #ifdef CONFIG_X86_32
200 end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
201 #else /* CONFIG_X86_64 */
202 end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
203 << (PUD_SHIFT - PAGE_SHIFT);
204 if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)))
205 end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT));
206 #endif
208 if (start_pfn < end_pfn) {
209 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
210 page_size_mask & (1<<PG_LEVEL_2M));
211 pos = end_pfn << PAGE_SHIFT;
214 #ifdef CONFIG_X86_64
215 /* big page (1G) range */
216 start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
217 << (PUD_SHIFT - PAGE_SHIFT);
218 end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
219 if (start_pfn < end_pfn) {
220 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
221 page_size_mask &
222 ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
223 pos = end_pfn << PAGE_SHIFT;
226 /* tail is not big page (1G) alignment */
227 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
228 << (PMD_SHIFT - PAGE_SHIFT);
229 end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
230 if (start_pfn < end_pfn) {
231 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
232 page_size_mask & (1<<PG_LEVEL_2M));
233 pos = end_pfn << PAGE_SHIFT;
235 #endif
237 /* tail is not big page (2M) alignment */
238 start_pfn = pos>>PAGE_SHIFT;
239 end_pfn = end>>PAGE_SHIFT;
240 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
242 /* try to merge same page size and continuous */
243 for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
244 unsigned long old_start;
245 if (mr[i].end != mr[i+1].start ||
246 mr[i].page_size_mask != mr[i+1].page_size_mask)
247 continue;
248 /* move it */
249 old_start = mr[i].start;
250 memmove(&mr[i], &mr[i+1],
251 (nr_range - 1 - i) * sizeof(struct map_range));
252 mr[i--].start = old_start;
253 nr_range--;
256 for (i = 0; i < nr_range; i++)
257 printk(KERN_DEBUG " %010lx - %010lx page %s\n",
258 mr[i].start, mr[i].end,
259 (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
260 (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
263 * Find space for the kernel direct mapping tables.
265 * Later we should allocate these tables in the local node of the
266 * memory mapped. Unfortunately this is done currently before the
267 * nodes are discovered.
269 if (!after_bootmem)
270 find_early_table_space(end, use_pse, use_gbpages);
272 for (i = 0; i < nr_range; i++)
273 ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
274 mr[i].page_size_mask);
276 #ifdef CONFIG_X86_32
277 early_ioremap_page_table_range_init();
279 load_cr3(swapper_pg_dir);
280 #endif
282 #ifdef CONFIG_X86_64
283 if (!after_bootmem && !start) {
284 pud_t *pud;
285 pmd_t *pmd;
287 mmu_cr4_features = read_cr4();
290 * _brk_end cannot change anymore, but it and _end may be
291 * located on different 2M pages. cleanup_highmap(), however,
292 * can only consider _end when it runs, so destroy any
293 * mappings beyond _brk_end here.
295 pud = pud_offset(pgd_offset_k(_brk_end), _brk_end);
296 pmd = pmd_offset(pud, _brk_end - 1);
297 while (++pmd <= pmd_offset(pud, (unsigned long)_end - 1))
298 pmd_clear(pmd);
300 #endif
301 __flush_tlb_all();
303 if (!after_bootmem && e820_table_end > e820_table_start)
304 memblock_x86_reserve_range(e820_table_start << PAGE_SHIFT,
305 e820_table_end << PAGE_SHIFT, "PGTABLE");
307 if (!after_bootmem)
308 early_memtest(start, end);
310 return ret >> PAGE_SHIFT;
315 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
316 * is valid. The argument is a physical page number.
319 * On x86, access has to be given to the first megabyte of ram because that area
320 * contains bios code and data regions used by X and dosemu and similar apps.
321 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
322 * mmio resources as well as potential bios/acpi data regions.
324 int devmem_is_allowed(unsigned long pagenr)
326 if (pagenr <= 256)
327 return 1;
328 if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
329 return 0;
330 if (!page_is_ram(pagenr))
331 return 1;
332 return 0;
335 void free_init_pages(char *what, unsigned long begin, unsigned long end)
337 unsigned long addr;
338 unsigned long begin_aligned, end_aligned;
340 /* Make sure boundaries are page aligned */
341 begin_aligned = PAGE_ALIGN(begin);
342 end_aligned = end & PAGE_MASK;
344 if (WARN_ON(begin_aligned != begin || end_aligned != end)) {
345 begin = begin_aligned;
346 end = end_aligned;
349 if (begin >= end)
350 return;
352 addr = begin;
355 * If debugging page accesses then do not free this memory but
356 * mark them not present - any buggy init-section access will
357 * create a kernel page fault:
359 #ifdef CONFIG_DEBUG_PAGEALLOC
360 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
361 begin, end);
362 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
363 #else
365 * We just marked the kernel text read only above, now that
366 * we are going to free part of that, we need to make that
367 * writeable and non-executable first.
369 set_memory_nx(begin, (end - begin) >> PAGE_SHIFT);
370 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
372 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
374 for (; addr < end; addr += PAGE_SIZE) {
375 ClearPageReserved(virt_to_page(addr));
376 init_page_count(virt_to_page(addr));
377 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
378 free_page(addr);
379 totalram_pages++;
381 #endif
384 void free_initmem(void)
386 free_init_pages("unused kernel memory",
387 (unsigned long)(&__init_begin),
388 (unsigned long)(&__init_end));
391 #ifdef CONFIG_BLK_DEV_INITRD
392 void free_initrd_mem(unsigned long start, unsigned long end)
395 * end could be not aligned, and We can not align that,
396 * decompresser could be confused by aligned initrd_end
397 * We already reserve the end partial page before in
398 * - i386_start_kernel()
399 * - x86_64_start_kernel()
400 * - relocate_initrd()
401 * So here We can do PAGE_ALIGN() safely to get partial page to be freed
403 free_init_pages("initrd memory", start, PAGE_ALIGN(end));
405 #endif