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 | aeebe84 | 2012-11-16 19:38:55 -0800 | [diff] [blame] | 91 | /* |
| 92 | * adjust the page_size_mask for small range to go with |
| 93 | * big page size instead small one if nearby are ram too. |
| 94 | */ |
| 95 | static void __init_refok adjust_range_page_size_mask(struct map_range *mr, |
| 96 | int nr_range) |
| 97 | { |
| 98 | int i; |
| 99 | |
| 100 | for (i = 0; i < nr_range; i++) { |
| 101 | if ((page_size_mask & (1<<PG_LEVEL_2M)) && |
| 102 | !(mr[i].page_size_mask & (1<<PG_LEVEL_2M))) { |
| 103 | unsigned long start = round_down(mr[i].start, PMD_SIZE); |
| 104 | unsigned long end = round_up(mr[i].end, PMD_SIZE); |
| 105 | |
| 106 | #ifdef CONFIG_X86_32 |
| 107 | if ((end >> PAGE_SHIFT) > max_low_pfn) |
| 108 | continue; |
| 109 | #endif |
| 110 | |
| 111 | if (memblock_is_region_memory(start, end - start)) |
| 112 | mr[i].page_size_mask |= 1<<PG_LEVEL_2M; |
| 113 | } |
| 114 | if ((page_size_mask & (1<<PG_LEVEL_1G)) && |
| 115 | !(mr[i].page_size_mask & (1<<PG_LEVEL_1G))) { |
| 116 | unsigned long start = round_down(mr[i].start, PUD_SIZE); |
| 117 | unsigned long end = round_up(mr[i].end, PUD_SIZE); |
| 118 | |
| 119 | if (memblock_is_region_memory(start, end - start)) |
| 120 | mr[i].page_size_mask |= 1<<PG_LEVEL_1G; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
Yinghai Lu | 4e33e06 | 2012-11-16 19:38:39 -0800 | [diff] [blame] | 125 | static int __meminit split_mem_range(struct map_range *mr, int nr_range, |
| 126 | unsigned long start, |
| 127 | unsigned long end) |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 128 | { |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 129 | unsigned long start_pfn, end_pfn; |
| 130 | unsigned long pos; |
Yinghai Lu | 4e33e06 | 2012-11-16 19:38:39 -0800 | [diff] [blame] | 131 | int i; |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 132 | |
| 133 | /* head if not big page alignment ? */ |
| 134 | start_pfn = start >> PAGE_SHIFT; |
| 135 | pos = start_pfn << PAGE_SHIFT; |
| 136 | #ifdef CONFIG_X86_32 |
| 137 | /* |
| 138 | * Don't use a large page for the first 2/4MB of memory |
| 139 | * because there are often fixed size MTRRs in there |
| 140 | * and overlapping MTRRs into large pages can cause |
| 141 | * slowdowns. |
| 142 | */ |
| 143 | if (pos == 0) |
| 144 | end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT); |
| 145 | else |
| 146 | end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT) |
| 147 | << (PMD_SHIFT - PAGE_SHIFT); |
| 148 | #else /* CONFIG_X86_64 */ |
| 149 | end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT) |
| 150 | << (PMD_SHIFT - PAGE_SHIFT); |
| 151 | #endif |
| 152 | if (end_pfn > (end >> PAGE_SHIFT)) |
| 153 | end_pfn = end >> PAGE_SHIFT; |
| 154 | if (start_pfn < end_pfn) { |
| 155 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0); |
| 156 | pos = end_pfn << PAGE_SHIFT; |
| 157 | } |
| 158 | |
| 159 | /* big page (2M) range */ |
| 160 | start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT) |
| 161 | << (PMD_SHIFT - PAGE_SHIFT); |
| 162 | #ifdef CONFIG_X86_32 |
| 163 | end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT); |
| 164 | #else /* CONFIG_X86_64 */ |
| 165 | end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT) |
| 166 | << (PUD_SHIFT - PAGE_SHIFT); |
| 167 | if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT))) |
| 168 | end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)); |
| 169 | #endif |
| 170 | |
| 171 | if (start_pfn < end_pfn) { |
| 172 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, |
| 173 | page_size_mask & (1<<PG_LEVEL_2M)); |
| 174 | pos = end_pfn << PAGE_SHIFT; |
| 175 | } |
| 176 | |
| 177 | #ifdef CONFIG_X86_64 |
| 178 | /* big page (1G) range */ |
| 179 | start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT) |
| 180 | << (PUD_SHIFT - PAGE_SHIFT); |
| 181 | end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT); |
| 182 | if (start_pfn < end_pfn) { |
| 183 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, |
| 184 | page_size_mask & |
| 185 | ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G))); |
| 186 | pos = end_pfn << PAGE_SHIFT; |
| 187 | } |
| 188 | |
| 189 | /* tail is not big page (1G) alignment */ |
| 190 | start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT) |
| 191 | << (PMD_SHIFT - PAGE_SHIFT); |
| 192 | end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT); |
| 193 | if (start_pfn < end_pfn) { |
| 194 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, |
| 195 | page_size_mask & (1<<PG_LEVEL_2M)); |
| 196 | pos = end_pfn << PAGE_SHIFT; |
| 197 | } |
| 198 | #endif |
| 199 | |
| 200 | /* tail is not big page (2M) alignment */ |
| 201 | start_pfn = pos>>PAGE_SHIFT; |
| 202 | end_pfn = end>>PAGE_SHIFT; |
| 203 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0); |
| 204 | |
| 205 | /* try to merge same page size and continuous */ |
| 206 | for (i = 0; nr_range > 1 && i < nr_range - 1; i++) { |
| 207 | unsigned long old_start; |
| 208 | if (mr[i].end != mr[i+1].start || |
| 209 | mr[i].page_size_mask != mr[i+1].page_size_mask) |
| 210 | continue; |
| 211 | /* move it */ |
| 212 | old_start = mr[i].start; |
| 213 | memmove(&mr[i], &mr[i+1], |
| 214 | (nr_range - 1 - i) * sizeof(struct map_range)); |
| 215 | mr[i--].start = old_start; |
| 216 | nr_range--; |
| 217 | } |
| 218 | |
Yinghai Lu | aeebe84 | 2012-11-16 19:38:55 -0800 | [diff] [blame] | 219 | if (!after_bootmem) |
| 220 | adjust_range_page_size_mask(mr, nr_range); |
| 221 | |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 222 | for (i = 0; i < nr_range; i++) |
Bjorn Helgaas | 365811d | 2012-05-29 15:06:29 -0700 | [diff] [blame] | 223 | printk(KERN_DEBUG " [mem %#010lx-%#010lx] page %s\n", |
| 224 | mr[i].start, mr[i].end - 1, |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 225 | (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":( |
| 226 | (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k")); |
| 227 | |
Yinghai Lu | 4e33e06 | 2012-11-16 19:38:39 -0800 | [diff] [blame] | 228 | return nr_range; |
| 229 | } |
| 230 | |
| 231 | /* |
Yinghai Lu | 2086fe1 | 2012-11-16 19:38:40 -0800 | [diff] [blame] | 232 | * First calculate space needed for kernel direct mapping page tables to cover |
| 233 | * mr[0].start to mr[nr_range - 1].end, while accounting for possible 2M and 1GB |
| 234 | * pages. Then find enough contiguous space for those page tables. |
| 235 | */ |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 236 | 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] | 237 | { |
| 238 | int i; |
| 239 | unsigned long puds = 0, pmds = 0, ptes = 0, tables; |
Yinghai Lu | 84f1ae3 | 2012-11-16 19:38:43 -0800 | [diff] [blame] | 240 | struct map_range mr[NR_RANGE_MR]; |
| 241 | int nr_range; |
| 242 | |
| 243 | memset(mr, 0, sizeof(mr)); |
| 244 | nr_range = 0; |
| 245 | nr_range = split_mem_range(mr, nr_range, start, end); |
Yinghai Lu | 2086fe1 | 2012-11-16 19:38:40 -0800 | [diff] [blame] | 246 | |
| 247 | for (i = 0; i < nr_range; i++) { |
| 248 | unsigned long range, extra; |
| 249 | |
| 250 | range = mr[i].end - mr[i].start; |
| 251 | puds += (range + PUD_SIZE - 1) >> PUD_SHIFT; |
| 252 | |
| 253 | if (mr[i].page_size_mask & (1 << PG_LEVEL_1G)) { |
| 254 | extra = range - ((range >> PUD_SHIFT) << PUD_SHIFT); |
| 255 | pmds += (extra + PMD_SIZE - 1) >> PMD_SHIFT; |
| 256 | } else { |
| 257 | pmds += (range + PMD_SIZE - 1) >> PMD_SHIFT; |
| 258 | } |
| 259 | |
| 260 | if (mr[i].page_size_mask & (1 << PG_LEVEL_2M)) { |
| 261 | extra = range - ((range >> PMD_SHIFT) << PMD_SHIFT); |
| 262 | #ifdef CONFIG_X86_32 |
| 263 | extra += PMD_SIZE; |
| 264 | #endif |
| 265 | ptes += (extra + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 266 | } else { |
| 267 | ptes += (range + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | tables = roundup(puds * sizeof(pud_t), PAGE_SIZE); |
| 272 | tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE); |
| 273 | tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE); |
| 274 | |
| 275 | #ifdef CONFIG_X86_32 |
| 276 | /* for fixmap */ |
| 277 | tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE); |
Yinghai Lu | 28b6ff6 | 2012-11-16 19:38:42 -0800 | [diff] [blame] | 278 | #endif |
Yinghai Lu | 2086fe1 | 2012-11-16 19:38:40 -0800 | [diff] [blame] | 279 | |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 280 | return tables; |
| 281 | } |
| 282 | |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame] | 283 | static unsigned long __init calculate_all_table_space_size(void) |
| 284 | { |
| 285 | unsigned long start_pfn, end_pfn; |
| 286 | unsigned long tables; |
| 287 | int i; |
| 288 | |
| 289 | /* the ISA range is always mapped regardless of memory holes */ |
| 290 | tables = calculate_table_space_size(0, ISA_END_ADDRESS); |
| 291 | |
| 292 | for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) { |
| 293 | u64 start = start_pfn << PAGE_SHIFT; |
| 294 | u64 end = end_pfn << PAGE_SHIFT; |
| 295 | |
| 296 | if (end <= ISA_END_ADDRESS) |
| 297 | continue; |
| 298 | |
| 299 | if (start < ISA_END_ADDRESS) |
| 300 | start = ISA_END_ADDRESS; |
| 301 | #ifdef CONFIG_X86_32 |
| 302 | /* on 32 bit, we only map up to max_low_pfn */ |
| 303 | if ((start >> PAGE_SHIFT) >= max_low_pfn) |
| 304 | continue; |
| 305 | |
| 306 | if ((end >> PAGE_SHIFT) > max_low_pfn) |
| 307 | end = max_low_pfn << PAGE_SHIFT; |
| 308 | #endif |
| 309 | tables += calculate_table_space_size(start, end); |
| 310 | } |
| 311 | |
| 312 | return tables; |
| 313 | } |
| 314 | |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 315 | static void __init find_early_table_space(unsigned long start, |
| 316 | unsigned long good_end, |
| 317 | unsigned long tables) |
| 318 | { |
| 319 | phys_addr_t base; |
| 320 | |
Yinghai Lu | 2086fe1 | 2012-11-16 19:38:40 -0800 | [diff] [blame] | 321 | base = memblock_find_in_range(start, good_end, tables, PAGE_SIZE); |
| 322 | if (!base) |
| 323 | panic("Cannot find space for the kernel page tables"); |
| 324 | |
| 325 | pgt_buf_start = base >> PAGE_SHIFT; |
| 326 | pgt_buf_end = pgt_buf_start; |
| 327 | pgt_buf_top = pgt_buf_start + (tables >> PAGE_SHIFT); |
Yinghai Lu | 2086fe1 | 2012-11-16 19:38:40 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame] | 330 | static struct range pfn_mapped[E820_X_MAX]; |
| 331 | static int nr_pfn_mapped; |
| 332 | |
| 333 | static void add_pfn_range_mapped(unsigned long start_pfn, unsigned long end_pfn) |
| 334 | { |
| 335 | nr_pfn_mapped = add_range_with_merge(pfn_mapped, E820_X_MAX, |
| 336 | nr_pfn_mapped, start_pfn, end_pfn); |
| 337 | nr_pfn_mapped = clean_sort_range(pfn_mapped, E820_X_MAX); |
| 338 | |
| 339 | max_pfn_mapped = max(max_pfn_mapped, end_pfn); |
| 340 | |
| 341 | if (start_pfn < (1UL<<(32-PAGE_SHIFT))) |
| 342 | max_low_pfn_mapped = max(max_low_pfn_mapped, |
| 343 | min(end_pfn, 1UL<<(32-PAGE_SHIFT))); |
| 344 | } |
| 345 | |
| 346 | bool pfn_range_is_mapped(unsigned long start_pfn, unsigned long end_pfn) |
| 347 | { |
| 348 | int i; |
| 349 | |
| 350 | for (i = 0; i < nr_pfn_mapped; i++) |
| 351 | if ((start_pfn >= pfn_mapped[i].start) && |
| 352 | (end_pfn <= pfn_mapped[i].end)) |
| 353 | return true; |
| 354 | |
| 355 | return false; |
| 356 | } |
| 357 | |
Yinghai Lu | 2086fe1 | 2012-11-16 19:38:40 -0800 | [diff] [blame] | 358 | /* |
Yinghai Lu | 4e33e06 | 2012-11-16 19:38:39 -0800 | [diff] [blame] | 359 | * Setup the direct mapping of the physical memory at PAGE_OFFSET. |
| 360 | * This runs before bootmem is initialized and gets pages directly from |
| 361 | * the physical memory. To access them they are temporarily mapped. |
| 362 | */ |
| 363 | unsigned long __init_refok init_memory_mapping(unsigned long start, |
| 364 | unsigned long end) |
| 365 | { |
| 366 | struct map_range mr[NR_RANGE_MR]; |
| 367 | unsigned long ret = 0; |
| 368 | int nr_range, i; |
| 369 | |
| 370 | pr_info("init_memory_mapping: [mem %#010lx-%#010lx]\n", |
| 371 | start, end - 1); |
| 372 | |
| 373 | memset(mr, 0, sizeof(mr)); |
| 374 | nr_range = split_mem_range(mr, 0, start, end); |
| 375 | |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 376 | for (i = 0; i < nr_range; i++) |
| 377 | ret = kernel_physical_mapping_init(mr[i].start, mr[i].end, |
| 378 | mr[i].page_size_mask); |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 379 | |
| 380 | #ifdef CONFIG_X86_32 |
| 381 | early_ioremap_page_table_range_init(); |
| 382 | |
| 383 | load_cr3(swapper_pg_dir); |
| 384 | #endif |
| 385 | |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 386 | __flush_tlb_all(); |
| 387 | |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame] | 388 | add_pfn_range_mapped(start >> PAGE_SHIFT, ret >> PAGE_SHIFT); |
| 389 | |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 390 | return ret >> PAGE_SHIFT; |
| 391 | } |
| 392 | |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame] | 393 | /* |
| 394 | * Iterate through E820 memory map and create direct mappings for only E820_RAM |
| 395 | * regions. We cannot simply create direct mappings for all pfns from |
| 396 | * [0 to max_low_pfn) and [4GB to max_pfn) because of possible memory holes in |
| 397 | * high addresses that cannot be marked as UC by fixed/variable range MTRRs. |
| 398 | * Depending on the alignment of E820 ranges, this may possibly result in using |
| 399 | * smaller size (i.e. 4K instead of 2M or 1G) page tables. |
| 400 | */ |
| 401 | static void __init init_all_memory_mapping(void) |
| 402 | { |
| 403 | unsigned long start_pfn, end_pfn; |
| 404 | int i; |
| 405 | |
| 406 | /* the ISA range is always mapped regardless of memory holes */ |
| 407 | init_memory_mapping(0, ISA_END_ADDRESS); |
| 408 | |
| 409 | for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) { |
| 410 | u64 start = (u64)start_pfn << PAGE_SHIFT; |
| 411 | u64 end = (u64)end_pfn << PAGE_SHIFT; |
| 412 | |
| 413 | if (end <= ISA_END_ADDRESS) |
| 414 | continue; |
| 415 | |
| 416 | if (start < ISA_END_ADDRESS) |
| 417 | start = ISA_END_ADDRESS; |
| 418 | #ifdef CONFIG_X86_32 |
| 419 | /* on 32 bit, we only map up to max_low_pfn */ |
| 420 | if ((start >> PAGE_SHIFT) >= max_low_pfn) |
| 421 | continue; |
| 422 | |
| 423 | if ((end >> PAGE_SHIFT) > max_low_pfn) |
| 424 | end = max_low_pfn << PAGE_SHIFT; |
| 425 | #endif |
| 426 | init_memory_mapping(start, end); |
| 427 | } |
| 428 | |
| 429 | #ifdef CONFIG_X86_64 |
| 430 | if (max_pfn > max_low_pfn) { |
| 431 | /* can we preseve max_low_pfn ?*/ |
| 432 | max_low_pfn = max_pfn; |
| 433 | } |
| 434 | #endif |
| 435 | } |
| 436 | |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 437 | void __init init_mem_mapping(void) |
| 438 | { |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 439 | unsigned long tables, good_end, end; |
| 440 | |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 441 | probe_page_size_mask(); |
| 442 | |
| 443 | /* |
| 444 | * Find space for the kernel direct mapping tables. |
| 445 | * |
| 446 | * Later we should allocate these tables in the local node of the |
| 447 | * memory mapped. Unfortunately this is done currently before the |
| 448 | * nodes are discovered. |
| 449 | */ |
| 450 | #ifdef CONFIG_X86_64 |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 451 | end = max_pfn << PAGE_SHIFT; |
| 452 | good_end = end; |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 453 | #else |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 454 | end = max_low_pfn << PAGE_SHIFT; |
| 455 | good_end = max_pfn_mapped << PAGE_SHIFT; |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 456 | #endif |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame] | 457 | tables = calculate_all_table_space_size(); |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 458 | find_early_table_space(0, good_end, tables); |
| 459 | printk(KERN_DEBUG "kernel direct mapping tables up to %#lx @ [mem %#010lx-%#010lx] prealloc\n", |
| 460 | end - 1, pgt_buf_start << PAGE_SHIFT, |
| 461 | (pgt_buf_top << PAGE_SHIFT) - 1); |
| 462 | |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame] | 463 | max_pfn_mapped = 0; /* will get exact value next */ |
| 464 | init_all_memory_mapping(); |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 465 | |
Stefano Stabellini | 279b706 | 2011-04-14 15:49:41 +0100 | [diff] [blame] | 466 | /* |
| 467 | * Reserve the kernel pagetable pages we used (pgt_buf_start - |
| 468 | * pgt_buf_end) and free the other ones (pgt_buf_end - pgt_buf_top) |
| 469 | * so that they can be reused for other purposes. |
| 470 | * |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 471 | * On native it just means calling memblock_reserve, on Xen it also |
| 472 | * means marking RW the pagetable pages that we allocated before |
Stefano Stabellini | 279b706 | 2011-04-14 15:49:41 +0100 | [diff] [blame] | 473 | * but that haven't been used. |
| 474 | * |
| 475 | * In fact on xen we mark RO the whole range pgt_buf_start - |
| 476 | * pgt_buf_top, because we have to make sure that when |
| 477 | * init_memory_mapping reaches the pagetable pages area, it maps |
| 478 | * RO all the pagetable pages, including the ones that are beyond |
| 479 | * pgt_buf_end at that time. |
| 480 | */ |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 481 | if (pgt_buf_end > pgt_buf_start) { |
| 482 | printk(KERN_DEBUG "kernel direct mapping tables up to %#lx @ [mem %#010lx-%#010lx] final\n", |
| 483 | end - 1, pgt_buf_start << PAGE_SHIFT, |
| 484 | (pgt_buf_end << PAGE_SHIFT) - 1); |
Stefano Stabellini | 279b706 | 2011-04-14 15:49:41 +0100 | [diff] [blame] | 485 | x86_init.mapping.pagetable_reserve(PFN_PHYS(pgt_buf_start), |
| 486 | PFN_PHYS(pgt_buf_end)); |
Yinghai Lu | ab95193 | 2012-11-16 19:38:45 -0800 | [diff] [blame] | 487 | } |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 488 | |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 489 | /* stop the wrong using */ |
| 490 | pgt_buf_top = 0; |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 491 | |
Yinghai Lu | c14fa0b | 2012-11-16 19:38:44 -0800 | [diff] [blame] | 492 | early_memtest(0, max_pfn_mapped << PAGE_SHIFT); |
Yinghai Lu | 22ddfca | 2012-11-16 19:38:41 -0800 | [diff] [blame] | 493 | } |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 494 | |
Pekka Enberg | 540aca0 | 2009-03-04 11:46:40 +0200 | [diff] [blame] | 495 | /* |
| 496 | * devmem_is_allowed() checks to see if /dev/mem access to a certain address |
| 497 | * is valid. The argument is a physical page number. |
| 498 | * |
| 499 | * |
| 500 | * On x86, access has to be given to the first megabyte of ram because that area |
| 501 | * contains bios code and data regions used by X and dosemu and similar apps. |
| 502 | * Access has to be given to non-kernel-ram areas as well, these contain the PCI |
| 503 | * mmio resources as well as potential bios/acpi data regions. |
| 504 | */ |
| 505 | int devmem_is_allowed(unsigned long pagenr) |
| 506 | { |
T Makphaibulchoke | 73e8f3d | 2012-08-28 21:21:43 -0600 | [diff] [blame] | 507 | if (pagenr < 256) |
Pekka Enberg | 540aca0 | 2009-03-04 11:46:40 +0200 | [diff] [blame] | 508 | return 1; |
| 509 | if (iomem_is_exclusive(pagenr << PAGE_SHIFT)) |
| 510 | return 0; |
| 511 | if (!page_is_ram(pagenr)) |
| 512 | return 1; |
| 513 | return 0; |
| 514 | } |
| 515 | |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 516 | void free_init_pages(char *what, unsigned long begin, unsigned long end) |
| 517 | { |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 518 | unsigned long addr; |
| 519 | unsigned long begin_aligned, end_aligned; |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 520 | |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 521 | /* Make sure boundaries are page aligned */ |
| 522 | begin_aligned = PAGE_ALIGN(begin); |
| 523 | end_aligned = end & PAGE_MASK; |
| 524 | |
| 525 | if (WARN_ON(begin_aligned != begin || end_aligned != end)) { |
| 526 | begin = begin_aligned; |
| 527 | end = end_aligned; |
| 528 | } |
| 529 | |
| 530 | if (begin >= end) |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 531 | return; |
| 532 | |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 533 | addr = begin; |
| 534 | |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 535 | /* |
| 536 | * If debugging page accesses then do not free this memory but |
| 537 | * mark them not present - any buggy init-section access will |
| 538 | * create a kernel page fault: |
| 539 | */ |
| 540 | #ifdef CONFIG_DEBUG_PAGEALLOC |
Bjorn Helgaas | 365811d | 2012-05-29 15:06:29 -0700 | [diff] [blame] | 541 | printk(KERN_INFO "debug: unmapping init [mem %#010lx-%#010lx]\n", |
| 542 | begin, end - 1); |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 543 | set_memory_np(begin, (end - begin) >> PAGE_SHIFT); |
| 544 | #else |
| 545 | /* |
| 546 | * We just marked the kernel text read only above, now that |
| 547 | * 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] | 548 | * writeable and non-executable first. |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 549 | */ |
Matthieu Castet | 5bd5a45 | 2010-11-16 22:31:26 +0100 | [diff] [blame] | 550 | set_memory_nx(begin, (end - begin) >> PAGE_SHIFT); |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 551 | set_memory_rw(begin, (end - begin) >> PAGE_SHIFT); |
| 552 | |
| 553 | printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10); |
| 554 | |
| 555 | for (; addr < end; addr += PAGE_SIZE) { |
| 556 | ClearPageReserved(virt_to_page(addr)); |
| 557 | init_page_count(virt_to_page(addr)); |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 558 | memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE); |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 559 | free_page(addr); |
| 560 | totalram_pages++; |
| 561 | } |
| 562 | #endif |
| 563 | } |
| 564 | |
| 565 | void free_initmem(void) |
| 566 | { |
| 567 | free_init_pages("unused kernel memory", |
| 568 | (unsigned long)(&__init_begin), |
| 569 | (unsigned long)(&__init_end)); |
| 570 | } |
Pekka Enberg | 731ddea | 2009-03-04 11:13:40 +0200 | [diff] [blame] | 571 | |
| 572 | #ifdef CONFIG_BLK_DEV_INITRD |
Jan Beulich | 0d26d1d | 2012-06-18 11:30:20 +0100 | [diff] [blame] | 573 | void __init free_initrd_mem(unsigned long start, unsigned long end) |
Pekka Enberg | 731ddea | 2009-03-04 11:13:40 +0200 | [diff] [blame] | 574 | { |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 575 | /* |
| 576 | * end could be not aligned, and We can not align that, |
| 577 | * decompresser could be confused by aligned initrd_end |
| 578 | * We already reserve the end partial page before in |
| 579 | * - i386_start_kernel() |
| 580 | * - x86_64_start_kernel() |
| 581 | * - relocate_initrd() |
| 582 | * So here We can do PAGE_ALIGN() safely to get partial page to be freed |
| 583 | */ |
| 584 | free_init_pages("initrd memory", start, PAGE_ALIGN(end)); |
Pekka Enberg | 731ddea | 2009-03-04 11:13:40 +0200 | [diff] [blame] | 585 | } |
| 586 | #endif |
Pekka Enberg | 1762391 | 2011-11-01 15:58:22 +0200 | [diff] [blame] | 587 | |
| 588 | void __init zone_sizes_init(void) |
| 589 | { |
| 590 | unsigned long max_zone_pfns[MAX_NR_ZONES]; |
| 591 | |
| 592 | memset(max_zone_pfns, 0, sizeof(max_zone_pfns)); |
| 593 | |
| 594 | #ifdef CONFIG_ZONE_DMA |
| 595 | max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN; |
| 596 | #endif |
| 597 | #ifdef CONFIG_ZONE_DMA32 |
| 598 | max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN; |
| 599 | #endif |
| 600 | max_zone_pfns[ZONE_NORMAL] = max_low_pfn; |
| 601 | #ifdef CONFIG_HIGHMEM |
| 602 | max_zone_pfns[ZONE_HIGHMEM] = max_pfn; |
| 603 | #endif |
| 604 | |
| 605 | free_area_init_nodes(max_zone_pfns); |
| 606 | } |
| 607 | |