aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/mm/init.c
diff options
context:
space:
mode:
authorPekka Enberg <penberg@cs.helsinki.fi>2009-03-05 07:55:05 -0500
committerIngo Molnar <mingo@elte.hu>2009-03-05 08:17:17 -0500
commitf765090a2617b8d9cb73b71e0aa850c29460d8be (patch)
treeb23dff6b8639a0f74f99a5206e6b7c9def588f6f /arch/x86/mm/init.c
parent0c0f756fd679d9747d52dad51fce3a5bb362eec3 (diff)
x86: move init_memory_mapping() to common mm/init.c
Impact: cleanup This patch moves the init_memory_mapping() function to common mm/init.c. Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi> Cc: Yinghai Lu <yinghai@kernel.org> LKML-Reference: <1236257708-27269-14-git-send-email-penberg@cs.helsinki.fi> Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'arch/x86/mm/init.c')
-rw-r--r--arch/x86/mm/init.c328
1 files changed, 328 insertions, 0 deletions
diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c
index cc7fe660f33d..3a21b136da24 100644
--- a/arch/x86/mm/init.c
+++ b/arch/x86/mm/init.c
@@ -2,10 +2,338 @@
2#include <linux/swap.h> 2#include <linux/swap.h>
3 3
4#include <asm/cacheflush.h> 4#include <asm/cacheflush.h>
5#include <asm/e820.h>
5#include <asm/page.h> 6#include <asm/page.h>
6#include <asm/page_types.h> 7#include <asm/page_types.h>
7#include <asm/sections.h> 8#include <asm/sections.h>
8#include <asm/system.h> 9#include <asm/system.h>
10#include <asm/tlbflush.h>
11
12#ifdef CONFIG_X86_32
13extern void __init early_ioremap_page_table_range_init(void);
14extern 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
20extern unsigned long __meminit
21kernel_physical_mapping_init(unsigned long start,
22 unsigned long end,
23 unsigned long page_size_mask);
24#endif
25
26unsigned long __initdata table_start;
27unsigned long __meminitdata table_end;
28unsigned long __meminitdata table_top;
29
30int after_bootmem;
31
32int direct_gbpages
33#ifdef CONFIG_DIRECT_GBPAGES
34 = 1
35#endif
36;
37
38static 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
98struct 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
110static 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
127static 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
135static 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 */
145unsigned 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
9 337
10/* 338/*
11 * devmem_is_allowed() checks to see if /dev/mem access to a certain address 339 * devmem_is_allowed() checks to see if /dev/mem access to a certain address