Merge branch 'linux-linaro-lsk-v4.4' into linux-linaro-lsk-v4.4-android
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / mm / init.c
1 /*
2  * Based on arch/arm/mm/init.c
3  *
4  * Copyright (C) 1995-2005 Russell King
5  * Copyright (C) 2012 ARM Ltd.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/export.h>
22 #include <linux/errno.h>
23 #include <linux/swap.h>
24 #include <linux/init.h>
25 #include <linux/bootmem.h>
26 #include <linux/mman.h>
27 #include <linux/nodemask.h>
28 #include <linux/initrd.h>
29 #include <linux/gfp.h>
30 #include <linux/memblock.h>
31 #include <linux/sort.h>
32 #include <linux/of.h>
33 #include <linux/of_fdt.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/dma-contiguous.h>
36 #include <linux/efi.h>
37 #include <linux/swiotlb.h>
38 #include <linux/kexec.h>
39 #include <linux/crash_dump.h>
40
41 #include <asm/boot.h>
42 #include <asm/fixmap.h>
43 #include <asm/kasan.h>
44 #include <asm/kernel-pgtable.h>
45 #include <asm/memory.h>
46 #include <asm/sections.h>
47 #include <asm/setup.h>
48 #include <asm/sizes.h>
49 #include <asm/tlb.h>
50 #include <asm/alternative.h>
51
52 #include "mm.h"
53
54 /*
55  * We need to be able to catch inadvertent references to memstart_addr
56  * that occur (potentially in generic code) before arm64_memblock_init()
57  * executes, which assigns it its actual value. So use a default value
58  * that cannot be mistaken for a real physical address.
59  */
60 s64 memstart_addr __read_mostly = -1;
61 phys_addr_t arm64_dma_phys_limit __read_mostly;
62
63 #ifdef CONFIG_BLK_DEV_INITRD
64 static int __init early_initrd(char *p)
65 {
66         unsigned long start, size;
67         char *endp;
68
69         start = memparse(p, &endp);
70         if (*endp == ',') {
71                 size = memparse(endp + 1, NULL);
72
73                 initrd_start = start;
74                 initrd_end = start + size;
75         }
76         return 0;
77 }
78 early_param("initrd", early_initrd);
79 #endif
80
81 #ifdef CONFIG_KEXEC_CORE
82 /*
83  * reserve_crashkernel() - reserves memory for crash kernel
84  *
85  * This function reserves memory area given in "crashkernel=" kernel command
86  * line parameter. The memory reserved is used by dump capture kernel when
87  * primary kernel is crashing.
88  */
89 static void __init reserve_crashkernel(void)
90 {
91         unsigned long long crash_base, crash_size;
92         int ret;
93
94         ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
95                                 &crash_size, &crash_base);
96         /* no crashkernel= or invalid value specified */
97         if (ret || !crash_size)
98                 return;
99
100         crash_size = PAGE_ALIGN(crash_size);
101
102         if (crash_base == 0) {
103                 /* Current arm64 boot protocol requires 2MB alignment */
104                 crash_base = memblock_find_in_range(0, ARCH_LOW_ADDRESS_LIMIT,
105                                 crash_size, SZ_2M);
106                 if (crash_base == 0) {
107                         pr_warn("cannot allocate crashkernel (size:0x%llx)\n",
108                                 crash_size);
109                         return;
110                 }
111         } else {
112                 /* User specifies base address explicitly. */
113                 if (!memblock_is_region_memory(crash_base, crash_size)) {
114                         pr_warn("cannot reserve crashkernel: region is not memory\n");
115                         return;
116                 }
117
118                 if (memblock_is_region_reserved(crash_base, crash_size)) {
119                         pr_warn("cannot reserve crashkernel: region overlaps reserved memory\n");
120                         return;
121                 }
122
123                 if (!IS_ALIGNED(crash_base, SZ_2M)) {
124                         pr_warn("cannot reserve crashkernel: base address is not 2MB aligned\n");
125                         return;
126                 }
127         }
128         memblock_reserve(crash_base, crash_size);
129
130         pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n",
131                 crash_base, crash_base + crash_size, crash_size >> 20);
132
133         crashk_res.start = crash_base;
134         crashk_res.end = crash_base + crash_size - 1;
135 }
136
137 static void __init kexec_reserve_crashkres_pages(void)
138 {
139 #ifdef CONFIG_HIBERNATION
140         phys_addr_t addr;
141         struct page *page;
142
143         if (!crashk_res.end)
144                 return;
145
146         /*
147          * To reduce the size of hibernation image, all the pages are
148          * marked as Reserved initially.
149          */
150         for (addr = crashk_res.start; addr < (crashk_res.end + 1);
151                         addr += PAGE_SIZE) {
152                 page = phys_to_page(addr);
153                 SetPageReserved(page);
154         }
155 #endif
156 }
157 #else
158 static void __init reserve_crashkernel(void)
159 {
160 }
161
162 static void __init kexec_reserve_crashkres_pages(void)
163 {
164 }
165 #endif /* CONFIG_KEXEC_CORE */
166
167 #ifdef CONFIG_CRASH_DUMP
168 static int __init early_init_dt_scan_elfcorehdr(unsigned long node,
169                 const char *uname, int depth, void *data)
170 {
171         const __be32 *reg;
172         int len;
173
174         if (depth != 1 || strcmp(uname, "chosen") != 0)
175                 return 0;
176
177         reg = of_get_flat_dt_prop(node, "linux,elfcorehdr", &len);
178         if (!reg || (len < (dt_root_addr_cells + dt_root_size_cells)))
179                 return 1;
180
181         elfcorehdr_addr = dt_mem_next_cell(dt_root_addr_cells, &reg);
182         elfcorehdr_size = dt_mem_next_cell(dt_root_size_cells, &reg);
183
184         return 1;
185 }
186
187 /*
188  * reserve_elfcorehdr() - reserves memory for elf core header
189  *
190  * This function reserves the memory occupied by an elf core header
191  * described in the device tree. This region contains all the
192  * information about primary kernel's core image and is used by a dump
193  * capture kernel to access the system memory on primary kernel.
194  */
195 static void __init reserve_elfcorehdr(void)
196 {
197         of_scan_flat_dt(early_init_dt_scan_elfcorehdr, NULL);
198
199         if (!elfcorehdr_size)
200                 return;
201
202         if (memblock_is_region_reserved(elfcorehdr_addr, elfcorehdr_size)) {
203                 pr_warn("elfcorehdr is overlapped\n");
204                 return;
205         }
206
207         memblock_reserve(elfcorehdr_addr, elfcorehdr_size);
208
209         pr_info("Reserving %lldKB of memory at 0x%llx for elfcorehdr\n",
210                 elfcorehdr_size >> 10, elfcorehdr_addr);
211 }
212 #else
213 static void __init reserve_elfcorehdr(void)
214 {
215 }
216 #endif /* CONFIG_CRASH_DUMP */
217 /*
218  * Return the maximum physical address for ZONE_DMA (DMA_BIT_MASK(32)). It
219  * currently assumes that for memory starting above 4G, 32-bit devices will
220  * use a DMA offset.
221  */
222 static phys_addr_t __init max_zone_dma_phys(void)
223 {
224         phys_addr_t offset = memblock_start_of_DRAM() & GENMASK_ULL(63, 32);
225         return min(offset + (1ULL << 32), memblock_end_of_DRAM());
226 }
227
228 static void __init zone_sizes_init(unsigned long min, unsigned long max)
229 {
230         struct memblock_region *reg;
231         unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
232         unsigned long max_dma = min;
233
234         memset(zone_size, 0, sizeof(zone_size));
235
236         /* 4GB maximum for 32-bit only capable devices */
237 #ifdef CONFIG_ZONE_DMA
238         max_dma = PFN_DOWN(arm64_dma_phys_limit);
239         zone_size[ZONE_DMA] = max_dma - min;
240 #endif
241         zone_size[ZONE_NORMAL] = max - max_dma;
242
243         memcpy(zhole_size, zone_size, sizeof(zhole_size));
244
245         for_each_memblock(memory, reg) {
246                 unsigned long start = memblock_region_memory_base_pfn(reg);
247                 unsigned long end = memblock_region_memory_end_pfn(reg);
248
249                 if (start >= max)
250                         continue;
251
252 #ifdef CONFIG_ZONE_DMA
253                 if (start < max_dma) {
254                         unsigned long dma_end = min(end, max_dma);
255                         zhole_size[ZONE_DMA] -= dma_end - start;
256                 }
257 #endif
258                 if (end > max_dma) {
259                         unsigned long normal_end = min(end, max);
260                         unsigned long normal_start = max(start, max_dma);
261                         zhole_size[ZONE_NORMAL] -= normal_end - normal_start;
262                 }
263         }
264
265         free_area_init_node(0, zone_size, min, zhole_size);
266 }
267
268 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
269 #define PFN_MASK ((1UL << (64 - PAGE_SHIFT)) - 1)
270
271 int pfn_valid(unsigned long pfn)
272 {
273         return (pfn & PFN_MASK) == pfn && memblock_is_map_memory(pfn << PAGE_SHIFT);
274 }
275 EXPORT_SYMBOL(pfn_valid);
276 #endif
277
278 #ifndef CONFIG_SPARSEMEM
279 static void __init arm64_memory_present(void)
280 {
281 }
282 #else
283 static void __init arm64_memory_present(void)
284 {
285         struct memblock_region *reg;
286
287         for_each_memblock(memory, reg)
288                 memory_present(0, memblock_region_memory_base_pfn(reg),
289                                memblock_region_memory_end_pfn(reg));
290 }
291 #endif
292
293 static phys_addr_t memory_limit = (phys_addr_t)ULLONG_MAX;
294
295 /*
296  * Limit the memory size that was specified via FDT.
297  */
298 static int __init early_mem(char *p)
299 {
300         if (!p)
301                 return 1;
302
303         memory_limit = memparse(p, &p) & PAGE_MASK;
304         pr_notice("Memory limited to %lldMB\n", memory_limit >> 20);
305
306         return 0;
307 }
308 early_param("mem", early_mem);
309
310 static int __init early_init_dt_scan_usablemem(unsigned long node,
311                 const char *uname, int depth, void *data)
312 {
313         struct memblock_region *usablemem = data;
314         const __be32 *reg;
315         int len;
316
317         if (depth != 1 || strcmp(uname, "chosen") != 0)
318                 return 0;
319
320         reg = of_get_flat_dt_prop(node, "linux,usable-memory-range", &len);
321         if (!reg || (len < (dt_root_addr_cells + dt_root_size_cells)))
322                 return 1;
323
324         usablemem->base = dt_mem_next_cell(dt_root_addr_cells, &reg);
325         usablemem->size = dt_mem_next_cell(dt_root_size_cells, &reg);
326
327         return 1;
328 }
329
330 static void __init fdt_enforce_memory_region(void)
331 {
332         struct memblock_region reg = {
333                 .size = 0,
334         };
335
336         of_scan_flat_dt(early_init_dt_scan_usablemem, &reg);
337
338         if (reg.size)
339                 memblock_cap_memory_range(reg.base, reg.size);
340 }
341
342 void __init arm64_memblock_init(void)
343 {
344         const s64 linear_region_size = -(s64)PAGE_OFFSET;
345
346         /* Handle linux,usable-memory-range property */
347         fdt_enforce_memory_region();
348
349         /*
350          * Ensure that the linear region takes up exactly half of the kernel
351          * virtual address space. This way, we can distinguish a linear address
352          * from a kernel/module/vmalloc address by testing a single bit.
353          */
354         BUILD_BUG_ON(linear_region_size != BIT(VA_BITS - 1));
355
356         /*
357          * Select a suitable value for the base of physical memory.
358          */
359         memstart_addr = round_down(memblock_start_of_DRAM(),
360                                    ARM64_MEMSTART_ALIGN);
361
362         /*
363          * Remove the memory that we will not be able to cover with the
364          * linear mapping. Take care not to clip the kernel which may be
365          * high in memory.
366          */
367         memblock_remove(max_t(u64, memstart_addr + linear_region_size, __pa(_end)),
368                         ULLONG_MAX);
369         if (memstart_addr + linear_region_size < memblock_end_of_DRAM()) {
370                 /* ensure that memstart_addr remains sufficiently aligned */
371                 memstart_addr = round_up(memblock_end_of_DRAM() - linear_region_size,
372                                          ARM64_MEMSTART_ALIGN);
373                 memblock_remove(0, memstart_addr);
374         }
375
376         /*
377          * Apply the memory limit if it was set. Since the kernel may be loaded
378          * high up in memory, add back the kernel region that must be accessible
379          * via the linear mapping.
380          */
381         if (memory_limit != (phys_addr_t)ULLONG_MAX) {
382                 memblock_enforce_memory_limit(memory_limit);
383                 memblock_add(__pa(_text), (u64)(_end - _text));
384         }
385
386         if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
387                 extern u16 memstart_offset_seed;
388                 u64 range = linear_region_size -
389                             (memblock_end_of_DRAM() - memblock_start_of_DRAM());
390
391                 /*
392                  * If the size of the linear region exceeds, by a sufficient
393                  * margin, the size of the region that the available physical
394                  * memory spans, randomize the linear region as well.
395                  */
396                 if (memstart_offset_seed > 0 && range >= ARM64_MEMSTART_ALIGN) {
397                         range = range / ARM64_MEMSTART_ALIGN + 1;
398                         memstart_addr -= ARM64_MEMSTART_ALIGN *
399                                          ((range * memstart_offset_seed) >> 16);
400                 }
401         }
402
403         /*
404          * Register the kernel text, kernel data, initrd, and initial
405          * pagetables with memblock.
406          */
407         memblock_reserve(__pa(_text), _end - _text);
408 #ifdef CONFIG_BLK_DEV_INITRD
409         if (initrd_start) {
410                 memblock_reserve(initrd_start, initrd_end - initrd_start);
411
412                 /* the generic initrd code expects virtual addresses */
413                 initrd_start = __phys_to_virt(initrd_start);
414                 initrd_end = __phys_to_virt(initrd_end);
415         }
416 #endif
417
418         early_init_fdt_scan_reserved_mem();
419
420         /* 4GB maximum for 32-bit only capable devices */
421         if (IS_ENABLED(CONFIG_ZONE_DMA))
422                 arm64_dma_phys_limit = max_zone_dma_phys();
423         else
424                 arm64_dma_phys_limit = PHYS_MASK + 1;
425
426         reserve_crashkernel();
427
428         reserve_elfcorehdr();
429
430         dma_contiguous_reserve(arm64_dma_phys_limit);
431
432         memblock_allow_resize();
433         memblock_dump_all();
434 }
435
436 void __init bootmem_init(void)
437 {
438         unsigned long min, max;
439
440         min = PFN_UP(memblock_start_of_DRAM());
441         max = PFN_DOWN(memblock_end_of_DRAM());
442
443         early_memtest(min << PAGE_SHIFT, max << PAGE_SHIFT);
444
445         /*
446          * Sparsemem tries to allocate bootmem in memory_present(), so must be
447          * done after the fixed reservations.
448          */
449         arm64_memory_present();
450
451         sparse_init();
452         zone_sizes_init(min, max);
453
454         high_memory = __va((max << PAGE_SHIFT) - 1) + 1;
455         max_pfn = max_low_pfn = max;
456 }
457
458 #ifndef CONFIG_SPARSEMEM_VMEMMAP
459 static inline void free_memmap(unsigned long start_pfn, unsigned long end_pfn)
460 {
461         struct page *start_pg, *end_pg;
462         unsigned long pg, pgend;
463
464         /*
465          * Convert start_pfn/end_pfn to a struct page pointer.
466          */
467         start_pg = pfn_to_page(start_pfn - 1) + 1;
468         end_pg = pfn_to_page(end_pfn - 1) + 1;
469
470         /*
471          * Convert to physical addresses, and round start upwards and end
472          * downwards.
473          */
474         pg = (unsigned long)PAGE_ALIGN(__pa(start_pg));
475         pgend = (unsigned long)__pa(end_pg) & PAGE_MASK;
476
477         /*
478          * If there are free pages between these, free the section of the
479          * memmap array.
480          */
481         if (pg < pgend)
482                 free_bootmem(pg, pgend - pg);
483 }
484
485 /*
486  * The mem_map array can get very big. Free the unused area of the memory map.
487  */
488 static void __init free_unused_memmap(void)
489 {
490         unsigned long start, prev_end = 0;
491         struct memblock_region *reg;
492
493         for_each_memblock(memory, reg) {
494                 start = __phys_to_pfn(reg->base);
495
496 #ifdef CONFIG_SPARSEMEM
497                 /*
498                  * Take care not to free memmap entries that don't exist due
499                  * to SPARSEMEM sections which aren't present.
500                  */
501                 start = min(start, ALIGN(prev_end, PAGES_PER_SECTION));
502 #endif
503                 /*
504                  * If we had a previous bank, and there is a space between the
505                  * current bank and the previous, free it.
506                  */
507                 if (prev_end && prev_end < start)
508                         free_memmap(prev_end, start);
509
510                 /*
511                  * Align up here since the VM subsystem insists that the
512                  * memmap entries are valid from the bank end aligned to
513                  * MAX_ORDER_NR_PAGES.
514                  */
515                 prev_end = ALIGN(__phys_to_pfn(reg->base + reg->size),
516                                  MAX_ORDER_NR_PAGES);
517         }
518
519 #ifdef CONFIG_SPARSEMEM
520         if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION))
521                 free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION));
522 #endif
523 }
524 #endif  /* !CONFIG_SPARSEMEM_VMEMMAP */
525
526 /*
527  * mem_init() marks the free areas in the mem_map and tells us how much memory
528  * is free.  This is done after various parts of the system have claimed their
529  * memory after the kernel image.
530  */
531 void __init mem_init(void)
532 {
533         swiotlb_init(1);
534
535         set_max_mapnr(pfn_to_page(max_pfn) - mem_map);
536
537 #ifndef CONFIG_SPARSEMEM_VMEMMAP
538         free_unused_memmap();
539 #endif
540         /* this will put all unused low memory onto the freelists */
541         free_all_bootmem();
542
543         kexec_reserve_crashkres_pages();
544
545         mem_init_print_info(NULL);
546
547 #define MLK(b, t) b, t, ((t) - (b)) >> 10
548 #define MLM(b, t) b, t, ((t) - (b)) >> 20
549 #define MLG(b, t) b, t, ((t) - (b)) >> 30
550 #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
551
552         pr_notice("Virtual kernel memory layout:\n"
553 #ifdef CONFIG_KASAN
554                   "    kasan   : 0x%16lx - 0x%16lx   (%6ld GB)\n"
555 #endif
556                   "    modules : 0x%16lx - 0x%16lx   (%6ld MB)\n"
557                   "    vmalloc : 0x%16lx - 0x%16lx   (%6ld GB)\n"
558                   "      .init : 0x%p" " - 0x%p" "   (%6ld KB)\n"
559                   "      .text : 0x%p" " - 0x%p" "   (%6ld KB)\n"
560                   "    .rodata : 0x%p" " - 0x%p" "   (%6ld KB)\n"
561                   "      .data : 0x%p" " - 0x%p" "   (%6ld KB)\n"
562 #ifdef CONFIG_SPARSEMEM_VMEMMAP
563                   "    vmemmap : 0x%16lx - 0x%16lx   (%6ld GB maximum)\n"
564                   "              0x%16lx - 0x%16lx   (%6ld MB actual)\n"
565 #endif
566                   "    fixed   : 0x%16lx - 0x%16lx   (%6ld KB)\n"
567                   "    PCI I/O : 0x%16lx - 0x%16lx   (%6ld MB)\n"
568                   "    memory  : 0x%16lx - 0x%16lx   (%6ld MB)\n",
569 #ifdef CONFIG_KASAN
570                   MLG(KASAN_SHADOW_START, KASAN_SHADOW_END),
571 #endif
572                   MLM(MODULES_VADDR, MODULES_END),
573                   MLG(VMALLOC_START, VMALLOC_END),
574                   MLK_ROUNDUP(__init_begin, __init_end),
575                   MLK_ROUNDUP(_text, _etext),
576                   MLK_ROUNDUP(__start_rodata, __init_begin),
577                   MLK_ROUNDUP(_sdata, _edata),
578 #ifdef CONFIG_SPARSEMEM_VMEMMAP
579                   MLG(VMEMMAP_START,
580                       VMEMMAP_START + VMEMMAP_SIZE),
581                   MLM((unsigned long)phys_to_page(memblock_start_of_DRAM()),
582                       (unsigned long)virt_to_page(high_memory)),
583 #endif
584                   MLK(FIXADDR_START, FIXADDR_TOP),
585                   MLM(PCI_IO_START, PCI_IO_END),
586                   MLM(__phys_to_virt(memblock_start_of_DRAM()),
587                       (unsigned long)high_memory));
588
589 #undef MLK
590 #undef MLM
591 #undef MLK_ROUNDUP
592
593         /*
594          * Check boundaries twice: Some fundamental inconsistencies can be
595          * detected at build time already.
596          */
597 #ifdef CONFIG_COMPAT
598         BUILD_BUG_ON(TASK_SIZE_32                       > TASK_SIZE_64);
599 #endif
600         BUILD_BUG_ON(TASK_SIZE_64                       > MODULES_VADDR);
601         BUG_ON(TASK_SIZE_64                             > MODULES_VADDR);
602
603         if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
604                 extern int sysctl_overcommit_memory;
605                 /*
606                  * On a machine this small we won't get anywhere without
607                  * overcommit, so turn it on by default.
608                  */
609                 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
610         }
611 }
612
613 void free_initmem(void)
614 {
615         free_initmem_default(0);
616         fixup_init();
617 }
618
619 #ifdef CONFIG_BLK_DEV_INITRD
620
621 static int keep_initrd __initdata;
622
623 void __init free_initrd_mem(unsigned long start, unsigned long end)
624 {
625         if (!keep_initrd)
626                 free_reserved_area((void *)start, (void *)end, 0, "initrd");
627 }
628
629 static int __init keepinitrd_setup(char *__unused)
630 {
631         keep_initrd = 1;
632         return 1;
633 }
634
635 __setup("keepinitrd", keepinitrd_setup);
636 #endif
637
638 /*
639  * Dump out memory limit information on panic.
640  */
641 static int dump_mem_limit(struct notifier_block *self, unsigned long v, void *p)
642 {
643         if (memory_limit != (phys_addr_t)ULLONG_MAX) {
644                 pr_emerg("Memory Limit: %llu MB\n", memory_limit >> 20);
645         } else {
646                 pr_emerg("Memory Limit: none\n");
647         }
648         return 0;
649 }
650
651 static struct notifier_block mem_limit_notifier = {
652         .notifier_call = dump_mem_limit,
653 };
654
655 static int __init register_mem_limit_dumper(void)
656 {
657         atomic_notifier_chain_register(&panic_notifier_list,
658                                        &mem_limit_notifier);
659         return 0;
660 }
661 __initcall(register_mem_limit_dumper);