Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 1 | #include <linux/gfp.h> |
Jaswinder Singh Rajput | 2c1b284 | 2009-04-11 00:03:10 +0530 | [diff] [blame] | 2 | #include <linux/initrd.h> |
Pekka Enberg | 540aca0 | 2009-03-04 11:46:40 +0200 | [diff] [blame] | 3 | #include <linux/ioport.h> |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 4 | #include <linux/swap.h> |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 5 | #include <linux/memblock.h> |
Pekka Enberg | 1762391 | 2011-11-01 15:58:22 +0200 | [diff] [blame] | 6 | #include <linux/bootmem.h> /* for max_low_pfn */ |
Pekka Enberg | 540aca0 | 2009-03-04 11:46:40 +0200 | [diff] [blame] | 7 | |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 8 | #include <asm/cacheflush.h> |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 9 | #include <asm/e820.h> |
Pekka Enberg | 4fcb208 | 2009-03-05 14:55:08 +0200 | [diff] [blame] | 10 | #include <asm/init.h> |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 11 | #include <asm/page.h> |
Pekka Enberg | 540aca0 | 2009-03-04 11:46:40 +0200 | [diff] [blame] | 12 | #include <asm/page_types.h> |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 13 | #include <asm/sections.h> |
Jan Beulich | 4983439 | 2009-05-06 13:06:47 +0100 | [diff] [blame] | 14 | #include <asm/setup.h> |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 15 | #include <asm/tlbflush.h> |
Pekka Enberg | 9518e0e | 2009-04-28 16:00:50 +0300 | [diff] [blame] | 16 | #include <asm/tlb.h> |
Jaswinder Singh Rajput | 76c0692 | 2009-07-01 19:54:23 +0530 | [diff] [blame] | 17 | #include <asm/proto.h> |
Pekka Enberg | 1762391 | 2011-11-01 15:58:22 +0200 | [diff] [blame] | 18 | #include <asm/dma.h> /* for MAX_DMA_PFN */ |
Pekka Enberg | 9518e0e | 2009-04-28 16:00:50 +0300 | [diff] [blame] | 19 | |
Yinghai Lu | d1b1942 | 2011-02-24 14:46:24 +0100 | [diff] [blame] | 20 | unsigned long __initdata pgt_buf_start; |
| 21 | unsigned long __meminitdata pgt_buf_end; |
| 22 | unsigned long __meminitdata pgt_buf_top; |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 23 | |
| 24 | int after_bootmem; |
| 25 | |
| 26 | int direct_gbpages |
| 27 | #ifdef CONFIG_DIRECT_GBPAGES |
| 28 | = 1 |
| 29 | #endif |
| 30 | ; |
| 31 | |
Jacob Shin | 844ab6f | 2012-10-24 14:24:44 -0500 | [diff] [blame] | 32 | struct map_range { |
| 33 | unsigned long start; |
| 34 | unsigned long end; |
| 35 | unsigned page_size_mask; |
| 36 | }; |
| 37 | |
Yinghai Lu | fa62aaf | 2012-11-16 19:38:38 -0800 | [diff] [blame] | 38 | static int page_size_mask; |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 39 | |
Yinghai Lu | 22ddfca | 2012-11-16 19:38:41 -0800 | [diff] [blame] | 40 | static void __init probe_page_size_mask(void) |
Yinghai Lu | fa62aaf | 2012-11-16 19:38:38 -0800 | [diff] [blame] | 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 | } |
Sedat Dilek | 53f8023 | 2011-04-17 16:17:34 +0200 | [diff] [blame] | 64 | void __init native_pagetable_reserve(u64 start, u64 end) |
Stefano Stabellini | 279b706 | 2011-04-14 15:49:41 +0100 | [diff] [blame] | 65 | { |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 66 | memblock_reserve(start, end - start); |
Stefano Stabellini | 279b706 | 2011-04-14 15:49:41 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 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 | |
Jan Beulich | dc9dd5c | 2009-03-12 12:40:06 +0000 | [diff] [blame] | 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) |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 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 | |
Yinghai Lu | 4e33e06 | 2012-11-16 19:38:39 -0800 | [diff] [blame] | 91 | static int __meminit split_mem_range(struct map_range *mr, int nr_range, |
| 92 | unsigned long start, |
| 93 | unsigned long end) |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 94 | { |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 95 | unsigned long start_pfn, end_pfn; |
| 96 | unsigned long pos; |
Yinghai Lu | 4e33e06 | 2012-11-16 19:38:39 -0800 | [diff] [blame] | 97 | int i; |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 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++) |
Bjorn Helgaas | 365811d | 2012-05-29 15:06:29 -0700 | [diff] [blame] | 186 | printk(KERN_DEBUG " [mem %#010lx-%#010lx] page %s\n", |
| 187 | mr[i].start, mr[i].end - 1, |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 188 | (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":( |
| 189 | (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k")); |
| 190 | |
Yinghai Lu | 4e33e06 | 2012-11-16 19:38:39 -0800 | [diff] [blame] | 191 | return nr_range; |
| 192 | } |
| 193 | |
| 194 | /* |
Yinghai Lu | 2086fe1 | 2012-11-16 19:38:40 -0800 | [diff] [blame] | 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 | */ |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 199 | static unsigned long __init calculate_table_space_size(unsigned long start, unsigned long end) |
Yinghai Lu | 2086fe1 | 2012-11-16 19:38:40 -0800 | [diff] [blame] | 200 | { |
| 201 | int i; |
| 202 | unsigned long puds = 0, pmds = 0, ptes = 0, tables; |
Yinghai Lu | 84f1ae3 | 2012-11-16 19:38:43 -0800 | [diff] [blame] | 203 | struct map_range mr[NR_RANGE_MR]; |
| 204 | int nr_range; |
| 205 | |
| 206 | memset(mr, 0, sizeof(mr)); |
| 207 | nr_range = 0; |
| 208 | nr_range = split_mem_range(mr, nr_range, start, end); |
Yinghai Lu | 2086fe1 | 2012-11-16 19:38:40 -0800 | [diff] [blame] | 209 | |
| 210 | for (i = 0; i < nr_range; i++) { |
| 211 | unsigned long range, extra; |
| 212 | |
| 213 | range = mr[i].end - mr[i].start; |
| 214 | puds += (range + PUD_SIZE - 1) >> PUD_SHIFT; |
| 215 | |
| 216 | if (mr[i].page_size_mask & (1 << PG_LEVEL_1G)) { |
| 217 | extra = range - ((range >> PUD_SHIFT) << PUD_SHIFT); |
| 218 | pmds += (extra + PMD_SIZE - 1) >> PMD_SHIFT; |
| 219 | } else { |
| 220 | pmds += (range + PMD_SIZE - 1) >> PMD_SHIFT; |
| 221 | } |
| 222 | |
| 223 | if (mr[i].page_size_mask & (1 << PG_LEVEL_2M)) { |
| 224 | extra = range - ((range >> PMD_SHIFT) << PMD_SHIFT); |
| 225 | #ifdef CONFIG_X86_32 |
| 226 | extra += PMD_SIZE; |
| 227 | #endif |
| 228 | ptes += (extra + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 229 | } else { |
| 230 | ptes += (range + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | tables = roundup(puds * sizeof(pud_t), PAGE_SIZE); |
| 235 | tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE); |
| 236 | tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE); |
| 237 | |
| 238 | #ifdef CONFIG_X86_32 |
| 239 | /* for fixmap */ |
| 240 | tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE); |
Yinghai Lu | 28b6ff6 | 2012-11-16 19:38:42 -0800 | [diff] [blame] | 241 | #endif |
Yinghai Lu | 2086fe1 | 2012-11-16 19:38:40 -0800 | [diff] [blame] | 242 | |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 243 | return tables; |
| 244 | } |
| 245 | |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame^] | 246 | static unsigned long __init calculate_all_table_space_size(void) |
| 247 | { |
| 248 | unsigned long start_pfn, end_pfn; |
| 249 | unsigned long tables; |
| 250 | int i; |
| 251 | |
| 252 | /* the ISA range is always mapped regardless of memory holes */ |
| 253 | tables = calculate_table_space_size(0, ISA_END_ADDRESS); |
| 254 | |
| 255 | for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) { |
| 256 | u64 start = start_pfn << PAGE_SHIFT; |
| 257 | u64 end = end_pfn << PAGE_SHIFT; |
| 258 | |
| 259 | if (end <= ISA_END_ADDRESS) |
| 260 | continue; |
| 261 | |
| 262 | if (start < ISA_END_ADDRESS) |
| 263 | start = ISA_END_ADDRESS; |
| 264 | #ifdef CONFIG_X86_32 |
| 265 | /* on 32 bit, we only map up to max_low_pfn */ |
| 266 | if ((start >> PAGE_SHIFT) >= max_low_pfn) |
| 267 | continue; |
| 268 | |
| 269 | if ((end >> PAGE_SHIFT) > max_low_pfn) |
| 270 | end = max_low_pfn << PAGE_SHIFT; |
| 271 | #endif |
| 272 | tables += calculate_table_space_size(start, end); |
| 273 | } |
| 274 | |
| 275 | return tables; |
| 276 | } |
| 277 | |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 278 | static void __init find_early_table_space(unsigned long start, |
| 279 | unsigned long good_end, |
| 280 | unsigned long tables) |
| 281 | { |
| 282 | phys_addr_t base; |
| 283 | |
Yinghai Lu | 2086fe1 | 2012-11-16 19:38:40 -0800 | [diff] [blame] | 284 | base = memblock_find_in_range(start, good_end, tables, PAGE_SIZE); |
| 285 | if (!base) |
| 286 | panic("Cannot find space for the kernel page tables"); |
| 287 | |
| 288 | pgt_buf_start = base >> PAGE_SHIFT; |
| 289 | pgt_buf_end = pgt_buf_start; |
| 290 | pgt_buf_top = pgt_buf_start + (tables >> PAGE_SHIFT); |
Yinghai Lu | 2086fe1 | 2012-11-16 19:38:40 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame^] | 293 | static struct range pfn_mapped[E820_X_MAX]; |
| 294 | static int nr_pfn_mapped; |
| 295 | |
| 296 | static void add_pfn_range_mapped(unsigned long start_pfn, unsigned long end_pfn) |
| 297 | { |
| 298 | nr_pfn_mapped = add_range_with_merge(pfn_mapped, E820_X_MAX, |
| 299 | nr_pfn_mapped, start_pfn, end_pfn); |
| 300 | nr_pfn_mapped = clean_sort_range(pfn_mapped, E820_X_MAX); |
| 301 | |
| 302 | max_pfn_mapped = max(max_pfn_mapped, end_pfn); |
| 303 | |
| 304 | if (start_pfn < (1UL<<(32-PAGE_SHIFT))) |
| 305 | max_low_pfn_mapped = max(max_low_pfn_mapped, |
| 306 | min(end_pfn, 1UL<<(32-PAGE_SHIFT))); |
| 307 | } |
| 308 | |
| 309 | bool pfn_range_is_mapped(unsigned long start_pfn, unsigned long end_pfn) |
| 310 | { |
| 311 | int i; |
| 312 | |
| 313 | for (i = 0; i < nr_pfn_mapped; i++) |
| 314 | if ((start_pfn >= pfn_mapped[i].start) && |
| 315 | (end_pfn <= pfn_mapped[i].end)) |
| 316 | return true; |
| 317 | |
| 318 | return false; |
| 319 | } |
| 320 | |
Yinghai Lu | 2086fe1 | 2012-11-16 19:38:40 -0800 | [diff] [blame] | 321 | /* |
Yinghai Lu | 4e33e06 | 2012-11-16 19:38:39 -0800 | [diff] [blame] | 322 | * Setup the direct mapping of the physical memory at PAGE_OFFSET. |
| 323 | * This runs before bootmem is initialized and gets pages directly from |
| 324 | * the physical memory. To access them they are temporarily mapped. |
| 325 | */ |
| 326 | unsigned long __init_refok init_memory_mapping(unsigned long start, |
| 327 | unsigned long end) |
| 328 | { |
| 329 | struct map_range mr[NR_RANGE_MR]; |
| 330 | unsigned long ret = 0; |
| 331 | int nr_range, i; |
| 332 | |
| 333 | pr_info("init_memory_mapping: [mem %#010lx-%#010lx]\n", |
| 334 | start, end - 1); |
| 335 | |
| 336 | memset(mr, 0, sizeof(mr)); |
| 337 | nr_range = split_mem_range(mr, 0, start, end); |
| 338 | |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 339 | for (i = 0; i < nr_range; i++) |
| 340 | ret = kernel_physical_mapping_init(mr[i].start, mr[i].end, |
| 341 | mr[i].page_size_mask); |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 342 | |
| 343 | #ifdef CONFIG_X86_32 |
| 344 | early_ioremap_page_table_range_init(); |
| 345 | |
| 346 | load_cr3(swapper_pg_dir); |
| 347 | #endif |
| 348 | |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 349 | __flush_tlb_all(); |
| 350 | |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame^] | 351 | add_pfn_range_mapped(start >> PAGE_SHIFT, ret >> PAGE_SHIFT); |
| 352 | |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 353 | return ret >> PAGE_SHIFT; |
| 354 | } |
| 355 | |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame^] | 356 | /* |
| 357 | * Iterate through E820 memory map and create direct mappings for only E820_RAM |
| 358 | * regions. We cannot simply create direct mappings for all pfns from |
| 359 | * [0 to max_low_pfn) and [4GB to max_pfn) because of possible memory holes in |
| 360 | * high addresses that cannot be marked as UC by fixed/variable range MTRRs. |
| 361 | * Depending on the alignment of E820 ranges, this may possibly result in using |
| 362 | * smaller size (i.e. 4K instead of 2M or 1G) page tables. |
| 363 | */ |
| 364 | static void __init init_all_memory_mapping(void) |
| 365 | { |
| 366 | unsigned long start_pfn, end_pfn; |
| 367 | int i; |
| 368 | |
| 369 | /* the ISA range is always mapped regardless of memory holes */ |
| 370 | init_memory_mapping(0, ISA_END_ADDRESS); |
| 371 | |
| 372 | for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) { |
| 373 | u64 start = (u64)start_pfn << PAGE_SHIFT; |
| 374 | u64 end = (u64)end_pfn << PAGE_SHIFT; |
| 375 | |
| 376 | if (end <= ISA_END_ADDRESS) |
| 377 | continue; |
| 378 | |
| 379 | if (start < ISA_END_ADDRESS) |
| 380 | start = ISA_END_ADDRESS; |
| 381 | #ifdef CONFIG_X86_32 |
| 382 | /* on 32 bit, we only map up to max_low_pfn */ |
| 383 | if ((start >> PAGE_SHIFT) >= max_low_pfn) |
| 384 | continue; |
| 385 | |
| 386 | if ((end >> PAGE_SHIFT) > max_low_pfn) |
| 387 | end = max_low_pfn << PAGE_SHIFT; |
| 388 | #endif |
| 389 | init_memory_mapping(start, end); |
| 390 | } |
| 391 | |
| 392 | #ifdef CONFIG_X86_64 |
| 393 | if (max_pfn > max_low_pfn) { |
| 394 | /* can we preseve max_low_pfn ?*/ |
| 395 | max_low_pfn = max_pfn; |
| 396 | } |
| 397 | #endif |
| 398 | } |
| 399 | |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 400 | void __init init_mem_mapping(void) |
| 401 | { |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 402 | unsigned long tables, good_end, end; |
| 403 | |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 404 | probe_page_size_mask(); |
| 405 | |
| 406 | /* |
| 407 | * Find space for the kernel direct mapping tables. |
| 408 | * |
| 409 | * Later we should allocate these tables in the local node of the |
| 410 | * memory mapped. Unfortunately this is done currently before the |
| 411 | * nodes are discovered. |
| 412 | */ |
| 413 | #ifdef CONFIG_X86_64 |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 414 | end = max_pfn << PAGE_SHIFT; |
| 415 | good_end = end; |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 416 | #else |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 417 | end = max_low_pfn << PAGE_SHIFT; |
| 418 | good_end = max_pfn_mapped << PAGE_SHIFT; |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 419 | #endif |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame^] | 420 | tables = calculate_all_table_space_size(); |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 421 | find_early_table_space(0, good_end, tables); |
| 422 | printk(KERN_DEBUG "kernel direct mapping tables up to %#lx @ [mem %#010lx-%#010lx] prealloc\n", |
| 423 | end - 1, pgt_buf_start << PAGE_SHIFT, |
| 424 | (pgt_buf_top << PAGE_SHIFT) - 1); |
| 425 | |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame^] | 426 | max_pfn_mapped = 0; /* will get exact value next */ |
| 427 | init_all_memory_mapping(); |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 428 | |
Stefano Stabellini | 279b706 | 2011-04-14 15:49:41 +0100 | [diff] [blame] | 429 | /* |
| 430 | * Reserve the kernel pagetable pages we used (pgt_buf_start - |
| 431 | * pgt_buf_end) and free the other ones (pgt_buf_end - pgt_buf_top) |
| 432 | * so that they can be reused for other purposes. |
| 433 | * |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 434 | * On native it just means calling memblock_reserve, on Xen it also |
| 435 | * means marking RW the pagetable pages that we allocated before |
Stefano Stabellini | 279b706 | 2011-04-14 15:49:41 +0100 | [diff] [blame] | 436 | * but that haven't been used. |
| 437 | * |
| 438 | * In fact on xen we mark RO the whole range pgt_buf_start - |
| 439 | * pgt_buf_top, because we have to make sure that when |
| 440 | * init_memory_mapping reaches the pagetable pages area, it maps |
| 441 | * RO all the pagetable pages, including the ones that are beyond |
| 442 | * pgt_buf_end at that time. |
| 443 | */ |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 444 | if (pgt_buf_end > pgt_buf_start) { |
| 445 | printk(KERN_DEBUG "kernel direct mapping tables up to %#lx @ [mem %#010lx-%#010lx] final\n", |
| 446 | end - 1, pgt_buf_start << PAGE_SHIFT, |
| 447 | (pgt_buf_end << PAGE_SHIFT) - 1); |
Stefano Stabellini | 279b706 | 2011-04-14 15:49:41 +0100 | [diff] [blame] | 448 | x86_init.mapping.pagetable_reserve(PFN_PHYS(pgt_buf_start), |
| 449 | PFN_PHYS(pgt_buf_end)); |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 450 | } |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 451 | |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 452 | /* stop the wrong using */ |
| 453 | pgt_buf_top = 0; |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 454 | |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 455 | early_memtest(0, max_pfn_mapped << PAGE_SHIFT); |
Yinghai Lu | 22ddfca | 2012-11-16 19:38:41 -0800 | [diff] [blame] | 456 | } |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 457 | |
Pekka Enberg | 540aca0 | 2009-03-04 11:46:40 +0200 | [diff] [blame] | 458 | /* |
| 459 | * devmem_is_allowed() checks to see if /dev/mem access to a certain address |
| 460 | * is valid. The argument is a physical page number. |
| 461 | * |
| 462 | * |
| 463 | * On x86, access has to be given to the first megabyte of ram because that area |
| 464 | * contains bios code and data regions used by X and dosemu and similar apps. |
| 465 | * Access has to be given to non-kernel-ram areas as well, these contain the PCI |
| 466 | * mmio resources as well as potential bios/acpi data regions. |
| 467 | */ |
| 468 | int devmem_is_allowed(unsigned long pagenr) |
| 469 | { |
T Makphaibulchoke | 73e8f3d | 2012-08-28 21:21:43 -0600 | [diff] [blame] | 470 | if (pagenr < 256) |
Pekka Enberg | 540aca0 | 2009-03-04 11:46:40 +0200 | [diff] [blame] | 471 | return 1; |
| 472 | if (iomem_is_exclusive(pagenr << PAGE_SHIFT)) |
| 473 | return 0; |
| 474 | if (!page_is_ram(pagenr)) |
| 475 | return 1; |
| 476 | return 0; |
| 477 | } |
| 478 | |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 479 | void free_init_pages(char *what, unsigned long begin, unsigned long end) |
| 480 | { |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 481 | unsigned long addr; |
| 482 | unsigned long begin_aligned, end_aligned; |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 483 | |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 484 | /* Make sure boundaries are page aligned */ |
| 485 | begin_aligned = PAGE_ALIGN(begin); |
| 486 | end_aligned = end & PAGE_MASK; |
| 487 | |
| 488 | if (WARN_ON(begin_aligned != begin || end_aligned != end)) { |
| 489 | begin = begin_aligned; |
| 490 | end = end_aligned; |
| 491 | } |
| 492 | |
| 493 | if (begin >= end) |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 494 | return; |
| 495 | |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 496 | addr = begin; |
| 497 | |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 498 | /* |
| 499 | * If debugging page accesses then do not free this memory but |
| 500 | * mark them not present - any buggy init-section access will |
| 501 | * create a kernel page fault: |
| 502 | */ |
| 503 | #ifdef CONFIG_DEBUG_PAGEALLOC |
Bjorn Helgaas | 365811d | 2012-05-29 15:06:29 -0700 | [diff] [blame] | 504 | printk(KERN_INFO "debug: unmapping init [mem %#010lx-%#010lx]\n", |
| 505 | begin, end - 1); |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 506 | set_memory_np(begin, (end - begin) >> PAGE_SHIFT); |
| 507 | #else |
| 508 | /* |
| 509 | * We just marked the kernel text read only above, now that |
| 510 | * we are going to free part of that, we need to make that |
Matthieu Castet | 5bd5a45 | 2010-11-16 22:31:26 +0100 | [diff] [blame] | 511 | * writeable and non-executable first. |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 512 | */ |
Matthieu Castet | 5bd5a45 | 2010-11-16 22:31:26 +0100 | [diff] [blame] | 513 | set_memory_nx(begin, (end - begin) >> PAGE_SHIFT); |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 514 | set_memory_rw(begin, (end - begin) >> PAGE_SHIFT); |
| 515 | |
| 516 | printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10); |
| 517 | |
| 518 | for (; addr < end; addr += PAGE_SIZE) { |
| 519 | ClearPageReserved(virt_to_page(addr)); |
| 520 | init_page_count(virt_to_page(addr)); |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 521 | memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE); |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 522 | free_page(addr); |
| 523 | totalram_pages++; |
| 524 | } |
| 525 | #endif |
| 526 | } |
| 527 | |
| 528 | void free_initmem(void) |
| 529 | { |
| 530 | free_init_pages("unused kernel memory", |
| 531 | (unsigned long)(&__init_begin), |
| 532 | (unsigned long)(&__init_end)); |
| 533 | } |
Pekka Enberg | 731ddea | 2009-03-04 11:13:40 +0200 | [diff] [blame] | 534 | |
| 535 | #ifdef CONFIG_BLK_DEV_INITRD |
Jan Beulich | 0d26d1d | 2012-06-18 11:30:20 +0100 | [diff] [blame] | 536 | void __init free_initrd_mem(unsigned long start, unsigned long end) |
Pekka Enberg | 731ddea | 2009-03-04 11:13:40 +0200 | [diff] [blame] | 537 | { |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 538 | /* |
| 539 | * end could be not aligned, and We can not align that, |
| 540 | * decompresser could be confused by aligned initrd_end |
| 541 | * We already reserve the end partial page before in |
| 542 | * - i386_start_kernel() |
| 543 | * - x86_64_start_kernel() |
| 544 | * - relocate_initrd() |
| 545 | * So here We can do PAGE_ALIGN() safely to get partial page to be freed |
| 546 | */ |
| 547 | free_init_pages("initrd memory", start, PAGE_ALIGN(end)); |
Pekka Enberg | 731ddea | 2009-03-04 11:13:40 +0200 | [diff] [blame] | 548 | } |
| 549 | #endif |
Pekka Enberg | 1762391 | 2011-11-01 15:58:22 +0200 | [diff] [blame] | 550 | |
| 551 | void __init zone_sizes_init(void) |
| 552 | { |
| 553 | unsigned long max_zone_pfns[MAX_NR_ZONES]; |
| 554 | |
| 555 | memset(max_zone_pfns, 0, sizeof(max_zone_pfns)); |
| 556 | |
| 557 | #ifdef CONFIG_ZONE_DMA |
| 558 | max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN; |
| 559 | #endif |
| 560 | #ifdef CONFIG_ZONE_DMA32 |
| 561 | max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN; |
| 562 | #endif |
| 563 | max_zone_pfns[ZONE_NORMAL] = max_low_pfn; |
| 564 | #ifdef CONFIG_HIGHMEM |
| 565 | max_zone_pfns[ZONE_HIGHMEM] = max_pfn; |
| 566 | #endif |
| 567 | |
| 568 | free_area_init_nodes(max_zone_pfns); |
| 569 | } |
| 570 | |