blob: 452ee5b8f30909fae2da150dcbd3cf4eaa67fa08 [file] [log] [blame]
Jaswinder Singh Rajput2c1b2842009-04-11 00:03:10 +05301#include <linux/initrd.h>
Pekka Enberg540aca02009-03-04 11:46:40 +02002#include <linux/ioport.h>
Pekka Enberge5b2bb52009-03-03 13:15:06 +02003#include <linux/swap.h>
Pekka Enberg540aca02009-03-04 11:46:40 +02004
Pekka Enberge5b2bb52009-03-03 13:15:06 +02005#include <asm/cacheflush.h>
Pekka Enbergf7650902009-03-05 14:55:05 +02006#include <asm/e820.h>
Pekka Enberg4fcb2082009-03-05 14:55:08 +02007#include <asm/init.h>
Pekka Enberge5b2bb52009-03-03 13:15:06 +02008#include <asm/page.h>
Pekka Enberg540aca02009-03-04 11:46:40 +02009#include <asm/page_types.h>
Pekka Enberge5b2bb52009-03-03 13:15:06 +020010#include <asm/sections.h>
Jan Beulich49834392009-05-06 13:06:47 +010011#include <asm/setup.h>
Pekka Enberge5b2bb52009-03-03 13:15:06 +020012#include <asm/system.h>
Pekka Enbergf7650902009-03-05 14:55:05 +020013#include <asm/tlbflush.h>
Pekka Enberg9518e0e2009-04-28 16:00:50 +030014#include <asm/tlb.h>
Jaswinder Singh Rajput76c06922009-07-01 19:54:23 +053015#include <asm/proto.h>
Pekka Enberg9518e0e2009-04-28 16:00:50 +030016
17DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
Pekka Enbergf7650902009-03-05 14:55:05 +020018
Pekka Enberg298af9d2009-03-05 14:55:06 +020019unsigned long __initdata e820_table_start;
20unsigned long __meminitdata e820_table_end;
21unsigned long __meminitdata e820_table_top;
Pekka Enbergf7650902009-03-05 14:55:05 +020022
23int after_bootmem;
24
25int direct_gbpages
26#ifdef CONFIG_DIRECT_GBPAGES
27 = 1
28#endif
29;
30
31static void __init find_early_table_space(unsigned long end, int use_pse,
32 int use_gbpages)
33{
34 unsigned long puds, pmds, ptes, tables, start;
35
36 puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
37 tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
38
39 if (use_gbpages) {
40 unsigned long extra;
41
42 extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT);
43 pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT;
44 } else
45 pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
46
47 tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
48
49 if (use_pse) {
50 unsigned long extra;
51
52 extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
53#ifdef CONFIG_X86_32
54 extra += PMD_SIZE;
55#endif
56 ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
57 } else
58 ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
59
60 tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
61
62#ifdef CONFIG_X86_32
63 /* for fixmap */
64 tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
65#endif
66
67 /*
68 * RED-PEN putting page tables only on node 0 could
69 * cause a hotspot and fill up ZONE_DMA. The page tables
70 * need roughly 0.5KB per GB.
71 */
72#ifdef CONFIG_X86_32
73 start = 0x7000;
Yinghai Lu80989ce2009-05-09 23:47:42 -070074#else
75 start = 0x8000;
76#endif
Pekka Enberg298af9d2009-03-05 14:55:06 +020077 e820_table_start = find_e820_area(start, max_pfn_mapped<<PAGE_SHIFT,
Pekka Enbergf7650902009-03-05 14:55:05 +020078 tables, PAGE_SIZE);
Pekka Enberg298af9d2009-03-05 14:55:06 +020079 if (e820_table_start == -1UL)
Pekka Enbergf7650902009-03-05 14:55:05 +020080 panic("Cannot find space for the kernel page tables");
81
Pekka Enberg298af9d2009-03-05 14:55:06 +020082 e820_table_start >>= PAGE_SHIFT;
83 e820_table_end = e820_table_start;
84 e820_table_top = e820_table_start + (tables >> PAGE_SHIFT);
Pekka Enbergf7650902009-03-05 14:55:05 +020085
86 printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
Pekka Enberg298af9d2009-03-05 14:55:06 +020087 end, e820_table_start << PAGE_SHIFT, e820_table_top << PAGE_SHIFT);
Pekka Enbergf7650902009-03-05 14:55:05 +020088}
89
90struct map_range {
91 unsigned long start;
92 unsigned long end;
93 unsigned page_size_mask;
94};
95
96#ifdef CONFIG_X86_32
97#define NR_RANGE_MR 3
98#else /* CONFIG_X86_64 */
99#define NR_RANGE_MR 5
100#endif
101
Jan Beulichdc9dd5c2009-03-12 12:40:06 +0000102static int __meminit save_mr(struct map_range *mr, int nr_range,
103 unsigned long start_pfn, unsigned long end_pfn,
104 unsigned long page_size_mask)
Pekka Enbergf7650902009-03-05 14:55:05 +0200105{
106 if (start_pfn < end_pfn) {
107 if (nr_range >= NR_RANGE_MR)
108 panic("run out of range for init_memory_mapping\n");
109 mr[nr_range].start = start_pfn<<PAGE_SHIFT;
110 mr[nr_range].end = end_pfn<<PAGE_SHIFT;
111 mr[nr_range].page_size_mask = page_size_mask;
112 nr_range++;
113 }
114
115 return nr_range;
116}
117
Pekka Enbergf7650902009-03-05 14:55:05 +0200118/*
119 * Setup the direct mapping of the physical memory at PAGE_OFFSET.
120 * This runs before bootmem is initialized and gets pages directly from
121 * the physical memory. To access them they are temporarily mapped.
122 */
123unsigned long __init_refok init_memory_mapping(unsigned long start,
124 unsigned long end)
125{
126 unsigned long page_size_mask = 0;
127 unsigned long start_pfn, end_pfn;
Pekka Enbergc77a3b52009-03-05 17:04:26 +0200128 unsigned long ret = 0;
Pekka Enbergf7650902009-03-05 14:55:05 +0200129 unsigned long pos;
Pekka Enbergf7650902009-03-05 14:55:05 +0200130
131 struct map_range mr[NR_RANGE_MR];
132 int nr_range, i;
133 int use_pse, use_gbpages;
134
135 printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end);
136
Vegard Nossumf8561292008-04-04 00:53:23 +0200137#if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
Pekka Enbergf7650902009-03-05 14:55:05 +0200138 /*
139 * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
140 * This will simplify cpa(), which otherwise needs to support splitting
141 * large pages into small in interrupt context, etc.
142 */
143 use_pse = use_gbpages = 0;
144#else
145 use_pse = cpu_has_pse;
146 use_gbpages = direct_gbpages;
147#endif
148
Pekka Enbergf7650902009-03-05 14:55:05 +0200149 /* Enable PSE if available */
150 if (cpu_has_pse)
151 set_in_cr4(X86_CR4_PSE);
152
153 /* Enable PGE if available */
154 if (cpu_has_pge) {
155 set_in_cr4(X86_CR4_PGE);
156 __supported_pte_mask |= _PAGE_GLOBAL;
157 }
Pekka Enbergf7650902009-03-05 14:55:05 +0200158
159 if (use_gbpages)
160 page_size_mask |= 1 << PG_LEVEL_1G;
161 if (use_pse)
162 page_size_mask |= 1 << PG_LEVEL_2M;
163
164 memset(mr, 0, sizeof(mr));
165 nr_range = 0;
166
167 /* head if not big page alignment ? */
168 start_pfn = start >> PAGE_SHIFT;
169 pos = start_pfn << PAGE_SHIFT;
170#ifdef CONFIG_X86_32
171 /*
172 * Don't use a large page for the first 2/4MB of memory
173 * because there are often fixed size MTRRs in there
174 * and overlapping MTRRs into large pages can cause
175 * slowdowns.
176 */
177 if (pos == 0)
178 end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
179 else
180 end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
181 << (PMD_SHIFT - PAGE_SHIFT);
182#else /* CONFIG_X86_64 */
183 end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT)
184 << (PMD_SHIFT - PAGE_SHIFT);
185#endif
186 if (end_pfn > (end >> PAGE_SHIFT))
187 end_pfn = end >> PAGE_SHIFT;
188 if (start_pfn < end_pfn) {
189 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
190 pos = end_pfn << PAGE_SHIFT;
191 }
192
193 /* big page (2M) range */
194 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
195 << (PMD_SHIFT - PAGE_SHIFT);
196#ifdef CONFIG_X86_32
197 end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
198#else /* CONFIG_X86_64 */
199 end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
200 << (PUD_SHIFT - PAGE_SHIFT);
201 if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)))
202 end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT));
203#endif
204
205 if (start_pfn < end_pfn) {
206 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
207 page_size_mask & (1<<PG_LEVEL_2M));
208 pos = end_pfn << PAGE_SHIFT;
209 }
210
211#ifdef CONFIG_X86_64
212 /* big page (1G) range */
213 start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
214 << (PUD_SHIFT - PAGE_SHIFT);
215 end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
216 if (start_pfn < end_pfn) {
217 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
218 page_size_mask &
219 ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
220 pos = end_pfn << PAGE_SHIFT;
221 }
222
223 /* tail is not big page (1G) alignment */
224 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
225 << (PMD_SHIFT - PAGE_SHIFT);
226 end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
227 if (start_pfn < end_pfn) {
228 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
229 page_size_mask & (1<<PG_LEVEL_2M));
230 pos = end_pfn << PAGE_SHIFT;
231 }
232#endif
233
234 /* tail is not big page (2M) alignment */
235 start_pfn = pos>>PAGE_SHIFT;
236 end_pfn = end>>PAGE_SHIFT;
237 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
238
239 /* try to merge same page size and continuous */
240 for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
241 unsigned long old_start;
242 if (mr[i].end != mr[i+1].start ||
243 mr[i].page_size_mask != mr[i+1].page_size_mask)
244 continue;
245 /* move it */
246 old_start = mr[i].start;
247 memmove(&mr[i], &mr[i+1],
248 (nr_range - 1 - i) * sizeof(struct map_range));
249 mr[i--].start = old_start;
250 nr_range--;
251 }
252
253 for (i = 0; i < nr_range; i++)
254 printk(KERN_DEBUG " %010lx - %010lx page %s\n",
255 mr[i].start, mr[i].end,
256 (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
257 (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
258
259 /*
260 * Find space for the kernel direct mapping tables.
261 *
262 * Later we should allocate these tables in the local node of the
263 * memory mapped. Unfortunately this is done currently before the
264 * nodes are discovered.
265 */
266 if (!after_bootmem)
267 find_early_table_space(end, use_pse, use_gbpages);
268
Pekka Enbergf7650902009-03-05 14:55:05 +0200269 for (i = 0; i < nr_range; i++)
270 ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
271 mr[i].page_size_mask);
Pekka Enbergf7650902009-03-05 14:55:05 +0200272
273#ifdef CONFIG_X86_32
274 early_ioremap_page_table_range_init();
275
276 load_cr3(swapper_pg_dir);
277#endif
278
279#ifdef CONFIG_X86_64
Jan Beulich49834392009-05-06 13:06:47 +0100280 if (!after_bootmem && !start) {
281 pud_t *pud;
282 pmd_t *pmd;
283
Pekka Enbergf7650902009-03-05 14:55:05 +0200284 mmu_cr4_features = read_cr4();
Jan Beulich49834392009-05-06 13:06:47 +0100285
286 /*
287 * _brk_end cannot change anymore, but it and _end may be
288 * located on different 2M pages. cleanup_highmap(), however,
289 * can only consider _end when it runs, so destroy any
290 * mappings beyond _brk_end here.
291 */
292 pud = pud_offset(pgd_offset_k(_brk_end), _brk_end);
293 pmd = pmd_offset(pud, _brk_end - 1);
294 while (++pmd <= pmd_offset(pud, (unsigned long)_end - 1))
295 pmd_clear(pmd);
296 }
Pekka Enbergf7650902009-03-05 14:55:05 +0200297#endif
298 __flush_tlb_all();
299
Pekka Enberg298af9d2009-03-05 14:55:06 +0200300 if (!after_bootmem && e820_table_end > e820_table_start)
301 reserve_early(e820_table_start << PAGE_SHIFT,
302 e820_table_end << PAGE_SHIFT, "PGTABLE");
Pekka Enbergf7650902009-03-05 14:55:05 +0200303
304 if (!after_bootmem)
305 early_memtest(start, end);
306
307 return ret >> PAGE_SHIFT;
308}
309
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200310
Pekka Enberg540aca02009-03-04 11:46:40 +0200311/*
312 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
313 * is valid. The argument is a physical page number.
314 *
315 *
316 * On x86, access has to be given to the first megabyte of ram because that area
317 * contains bios code and data regions used by X and dosemu and similar apps.
318 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
319 * mmio resources as well as potential bios/acpi data regions.
320 */
321int devmem_is_allowed(unsigned long pagenr)
322{
323 if (pagenr <= 256)
324 return 1;
325 if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
326 return 0;
327 if (!page_is_ram(pagenr))
328 return 1;
329 return 0;
330}
331
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200332void free_init_pages(char *what, unsigned long begin, unsigned long end)
333{
Yinghai Luc967da62010-03-28 19:42:55 -0700334 unsigned long addr;
335 unsigned long begin_aligned, end_aligned;
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200336
Yinghai Luc967da62010-03-28 19:42:55 -0700337 /* Make sure boundaries are page aligned */
338 begin_aligned = PAGE_ALIGN(begin);
339 end_aligned = end & PAGE_MASK;
340
341 if (WARN_ON(begin_aligned != begin || end_aligned != end)) {
342 begin = begin_aligned;
343 end = end_aligned;
344 }
345
346 if (begin >= end)
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200347 return;
348
Yinghai Luc967da62010-03-28 19:42:55 -0700349 addr = begin;
350
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200351 /*
352 * If debugging page accesses then do not free this memory but
353 * mark them not present - any buggy init-section access will
354 * create a kernel page fault:
355 */
356#ifdef CONFIG_DEBUG_PAGEALLOC
357 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
Yinghai Luc967da62010-03-28 19:42:55 -0700358 begin, end);
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200359 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
360#else
361 /*
362 * We just marked the kernel text read only above, now that
363 * we are going to free part of that, we need to make that
364 * writeable first.
365 */
366 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
367
368 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
369
370 for (; addr < end; addr += PAGE_SIZE) {
371 ClearPageReserved(virt_to_page(addr));
372 init_page_count(virt_to_page(addr));
Yinghai Luc967da62010-03-28 19:42:55 -0700373 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
Pekka Enberge5b2bb52009-03-03 13:15:06 +0200374 free_page(addr);
375 totalram_pages++;
376 }
377#endif
378}
379
380void free_initmem(void)
381{
382 free_init_pages("unused kernel memory",
383 (unsigned long)(&__init_begin),
384 (unsigned long)(&__init_end));
385}
Pekka Enberg731ddea2009-03-04 11:13:40 +0200386
387#ifdef CONFIG_BLK_DEV_INITRD
388void free_initrd_mem(unsigned long start, unsigned long end)
389{
Yinghai Luc967da62010-03-28 19:42:55 -0700390 /*
391 * end could be not aligned, and We can not align that,
392 * decompresser could be confused by aligned initrd_end
393 * We already reserve the end partial page before in
394 * - i386_start_kernel()
395 * - x86_64_start_kernel()
396 * - relocate_initrd()
397 * So here We can do PAGE_ALIGN() safely to get partial page to be freed
398 */
399 free_init_pages("initrd memory", start, PAGE_ALIGN(end));
Pekka Enberg731ddea2009-03-04 11:13:40 +0200400}
401#endif