x86: move init_memory_mapping() to common mm/init.c
[linux-2.6.git] / arch / x86 / mm / init.c
1 #include <linux/ioport.h>
2 #include <linux/swap.h>
3
4 #include <asm/cacheflush.h>
5 #include <asm/e820.h>
6 #include <asm/page.h>
7 #include <asm/page_types.h>
8 #include <asm/sections.h>
9 #include <asm/system.h>
10 #include <asm/tlbflush.h>
11
12 #ifdef CONFIG_X86_32
13 extern void __init early_ioremap_page_table_range_init(void);
14 extern void __init kernel_physical_mapping_init(unsigned long start_pfn,
15                                                 unsigned long end_pfn,
16                                                 int use_pse);
17 #endif
18
19 #ifdef CONFIG_X86_64
20 extern unsigned long __meminit
21 kernel_physical_mapping_init(unsigned long start,
22                              unsigned long end,
23                              unsigned long page_size_mask);
24 #endif
25
26 unsigned long __initdata table_start;
27 unsigned long __meminitdata table_end;
28 unsigned long __meminitdata table_top;
29
30 int after_bootmem;
31
32 int direct_gbpages
33 #ifdef CONFIG_DIRECT_GBPAGES
34                                 = 1
35 #endif
36 ;
37
38 static void __init find_early_table_space(unsigned long end, int use_pse,
39                                           int use_gbpages)
40 {
41         unsigned long puds, pmds, ptes, tables, start;
42
43         puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
44         tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
45
46         if (use_gbpages) {
47                 unsigned long extra;
48
49                 extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT);
50                 pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT;
51         } else
52                 pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
53
54         tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
55
56         if (use_pse) {
57                 unsigned long extra;
58
59                 extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
60 #ifdef CONFIG_X86_32
61                 extra += PMD_SIZE;
62 #endif
63                 ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
64         } else
65                 ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
66
67         tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
68
69 #ifdef CONFIG_X86_32
70         /* for fixmap */
71         tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
72 #endif
73
74         /*
75          * RED-PEN putting page tables only on node 0 could
76          * cause a hotspot and fill up ZONE_DMA. The page tables
77          * need roughly 0.5KB per GB.
78          */
79 #ifdef CONFIG_X86_32
80         start = 0x7000;
81         table_start = find_e820_area(start, max_pfn_mapped<<PAGE_SHIFT,
82                                         tables, PAGE_SIZE);
83 #else /* CONFIG_X86_64 */
84         start = 0x8000;
85         table_start = find_e820_area(start, end, tables, PAGE_SIZE);
86 #endif
87         if (table_start == -1UL)
88                 panic("Cannot find space for the kernel page tables");
89
90         table_start >>= PAGE_SHIFT;
91         table_end = table_start;
92         table_top = table_start + (tables >> PAGE_SHIFT);
93
94         printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
95                 end, table_start << PAGE_SHIFT, table_top << PAGE_SHIFT);
96 }
97
98 struct map_range {
99         unsigned long start;
100         unsigned long end;
101         unsigned page_size_mask;
102 };
103
104 #ifdef CONFIG_X86_32
105 #define NR_RANGE_MR 3
106 #else /* CONFIG_X86_64 */
107 #define NR_RANGE_MR 5
108 #endif
109
110 static int save_mr(struct map_range *mr, int nr_range,
111                    unsigned long start_pfn, unsigned long end_pfn,
112                    unsigned long page_size_mask)
113 {
114         if (start_pfn < end_pfn) {
115                 if (nr_range >= NR_RANGE_MR)
116                         panic("run out of range for init_memory_mapping\n");
117                 mr[nr_range].start = start_pfn<<PAGE_SHIFT;
118                 mr[nr_range].end   = end_pfn<<PAGE_SHIFT;
119                 mr[nr_range].page_size_mask = page_size_mask;
120                 nr_range++;
121         }
122
123         return nr_range;
124 }
125
126 #ifdef CONFIG_X86_64
127 static void __init init_gbpages(void)
128 {
129         if (direct_gbpages && cpu_has_gbpages)
130                 printk(KERN_INFO "Using GB pages for direct mapping\n");
131         else
132                 direct_gbpages = 0;
133 }
134 #else
135 static inline void init_gbpages(void)
136 {
137 }
138 #endif
139
140 /*
141  * Setup the direct mapping of the physical memory at PAGE_OFFSET.
142  * This runs before bootmem is initialized and gets pages directly from
143  * the physical memory. To access them they are temporarily mapped.
144  */
145 unsigned long __init_refok init_memory_mapping(unsigned long start,
146                                                unsigned long end)
147 {
148         unsigned long page_size_mask = 0;
149         unsigned long start_pfn, end_pfn;
150         unsigned long pos;
151         unsigned long ret;
152
153         struct map_range mr[NR_RANGE_MR];
154         int nr_range, i;
155         int use_pse, use_gbpages;
156
157         printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end);
158
159         if (!after_bootmem)
160                 init_gbpages();
161
162 #ifdef CONFIG_DEBUG_PAGEALLOC
163         /*
164          * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
165          * This will simplify cpa(), which otherwise needs to support splitting
166          * large pages into small in interrupt context, etc.
167          */
168         use_pse = use_gbpages = 0;
169 #else
170         use_pse = cpu_has_pse;
171         use_gbpages = direct_gbpages;
172 #endif
173
174 #ifdef CONFIG_X86_32
175 #ifdef CONFIG_X86_PAE
176         set_nx();
177         if (nx_enabled)
178                 printk(KERN_INFO "NX (Execute Disable) protection: active\n");
179 #endif
180
181         /* Enable PSE if available */
182         if (cpu_has_pse)
183                 set_in_cr4(X86_CR4_PSE);
184
185         /* Enable PGE if available */
186         if (cpu_has_pge) {
187                 set_in_cr4(X86_CR4_PGE);
188                 __supported_pte_mask |= _PAGE_GLOBAL;
189         }
190 #endif
191
192         if (use_gbpages)
193                 page_size_mask |= 1 << PG_LEVEL_1G;
194         if (use_pse)
195                 page_size_mask |= 1 << PG_LEVEL_2M;
196
197         memset(mr, 0, sizeof(mr));
198         nr_range = 0;
199
200         /* head if not big page alignment ? */
201         start_pfn = start >> PAGE_SHIFT;
202         pos = start_pfn << PAGE_SHIFT;
203 #ifdef CONFIG_X86_32
204         /*
205          * Don't use a large page for the first 2/4MB of memory
206          * because there are often fixed size MTRRs in there
207          * and overlapping MTRRs into large pages can cause
208          * slowdowns.
209          */
210         if (pos == 0)
211                 end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
212         else
213                 end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
214                                  << (PMD_SHIFT - PAGE_SHIFT);
215 #else /* CONFIG_X86_64 */
216         end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT)
217                         << (PMD_SHIFT - PAGE_SHIFT);
218 #endif
219         if (end_pfn > (end >> PAGE_SHIFT))
220                 end_pfn = end >> PAGE_SHIFT;
221         if (start_pfn < end_pfn) {
222                 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
223                 pos = end_pfn << PAGE_SHIFT;
224         }
225
226         /* big page (2M) range */
227         start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
228                          << (PMD_SHIFT - PAGE_SHIFT);
229 #ifdef CONFIG_X86_32
230         end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
231 #else /* CONFIG_X86_64 */
232         end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
233                          << (PUD_SHIFT - PAGE_SHIFT);
234         if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)))
235                 end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT));
236 #endif
237
238         if (start_pfn < end_pfn) {
239                 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
240                                 page_size_mask & (1<<PG_LEVEL_2M));
241                 pos = end_pfn << PAGE_SHIFT;
242         }
243
244 #ifdef CONFIG_X86_64
245         /* big page (1G) range */
246         start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
247                          << (PUD_SHIFT - PAGE_SHIFT);
248         end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
249         if (start_pfn < end_pfn) {
250                 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
251                                 page_size_mask &
252                                  ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
253                 pos = end_pfn << PAGE_SHIFT;
254         }
255
256         /* tail is not big page (1G) alignment */
257         start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
258                          << (PMD_SHIFT - PAGE_SHIFT);
259         end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
260         if (start_pfn < end_pfn) {
261                 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
262                                 page_size_mask & (1<<PG_LEVEL_2M));
263                 pos = end_pfn << PAGE_SHIFT;
264         }
265 #endif
266
267         /* tail is not big page (2M) alignment */
268         start_pfn = pos>>PAGE_SHIFT;
269         end_pfn = end>>PAGE_SHIFT;
270         nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
271
272         /* try to merge same page size and continuous */
273         for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
274                 unsigned long old_start;
275                 if (mr[i].end != mr[i+1].start ||
276                     mr[i].page_size_mask != mr[i+1].page_size_mask)
277                         continue;
278                 /* move it */
279                 old_start = mr[i].start;
280                 memmove(&mr[i], &mr[i+1],
281                         (nr_range - 1 - i) * sizeof(struct map_range));
282                 mr[i--].start = old_start;
283                 nr_range--;
284         }
285
286         for (i = 0; i < nr_range; i++)
287                 printk(KERN_DEBUG " %010lx - %010lx page %s\n",
288                                 mr[i].start, mr[i].end,
289                         (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
290                          (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
291
292         /*
293          * Find space for the kernel direct mapping tables.
294          *
295          * Later we should allocate these tables in the local node of the
296          * memory mapped. Unfortunately this is done currently before the
297          * nodes are discovered.
298          */
299         if (!after_bootmem)
300                 find_early_table_space(end, use_pse, use_gbpages);
301
302 #ifdef CONFIG_X86_32
303         for (i = 0; i < nr_range; i++)
304                 kernel_physical_mapping_init(
305                                 mr[i].start >> PAGE_SHIFT,
306                                 mr[i].end >> PAGE_SHIFT,
307                                 mr[i].page_size_mask == (1<<PG_LEVEL_2M));
308         ret = end;
309 #else /* CONFIG_X86_64 */
310         for (i = 0; i < nr_range; i++)
311                 ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
312                                                    mr[i].page_size_mask);
313 #endif
314
315 #ifdef CONFIG_X86_32
316         early_ioremap_page_table_range_init();
317
318         load_cr3(swapper_pg_dir);
319 #endif
320
321 #ifdef CONFIG_X86_64
322         if (!after_bootmem)
323                 mmu_cr4_features = read_cr4();
324 #endif
325         __flush_tlb_all();
326
327         if (!after_bootmem && table_end > table_start)
328                 reserve_early(table_start << PAGE_SHIFT,
329                                  table_end << PAGE_SHIFT, "PGTABLE");
330
331         if (!after_bootmem)
332                 early_memtest(start, end);
333
334         return ret >> PAGE_SHIFT;
335 }
336
337
338 /*
339  * devmem_is_allowed() checks to see if /dev/mem access to a certain address
340  * is valid. The argument is a physical page number.
341  *
342  *
343  * On x86, access has to be given to the first megabyte of ram because that area
344  * contains bios code and data regions used by X and dosemu and similar apps.
345  * Access has to be given to non-kernel-ram areas as well, these contain the PCI
346  * mmio resources as well as potential bios/acpi data regions.
347  */
348 int devmem_is_allowed(unsigned long pagenr)
349 {
350         if (pagenr <= 256)
351                 return 1;
352         if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
353                 return 0;
354         if (!page_is_ram(pagenr))
355                 return 1;
356         return 0;
357 }
358
359 void free_init_pages(char *what, unsigned long begin, unsigned long end)
360 {
361         unsigned long addr = begin;
362
363         if (addr >= end)
364                 return;
365
366         /*
367          * If debugging page accesses then do not free this memory but
368          * mark them not present - any buggy init-section access will
369          * create a kernel page fault:
370          */
371 #ifdef CONFIG_DEBUG_PAGEALLOC
372         printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
373                 begin, PAGE_ALIGN(end));
374         set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
375 #else
376         /*
377          * We just marked the kernel text read only above, now that
378          * we are going to free part of that, we need to make that
379          * writeable first.
380          */
381         set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
382
383         printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
384
385         for (; addr < end; addr += PAGE_SIZE) {
386                 ClearPageReserved(virt_to_page(addr));
387                 init_page_count(virt_to_page(addr));
388                 memset((void *)(addr & ~(PAGE_SIZE-1)),
389                         POISON_FREE_INITMEM, PAGE_SIZE);
390                 free_page(addr);
391                 totalram_pages++;
392         }
393 #endif
394 }
395
396 void free_initmem(void)
397 {
398         free_init_pages("unused kernel memory",
399                         (unsigned long)(&__init_begin),
400                         (unsigned long)(&__init_end));
401 }
402
403 #ifdef CONFIG_BLK_DEV_INITRD
404 void free_initrd_mem(unsigned long start, unsigned long end)
405 {
406         free_init_pages("initrd memory", start, end);
407 }
408 #endif