Merge branch develop-3.10
[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_fdt.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/dma-contiguous.h>
35
36 #include <asm/prom.h>
37 #include <asm/sections.h>
38 #include <asm/setup.h>
39 #include <asm/sizes.h>
40 #include <asm/tlb.h>
41
42 #ifdef CONFIG_ARCH_ROCKCHIP
43 #include <linux/rockchip/common.h>
44 #endif
45
46 #include "mm.h"
47
48 static unsigned long phys_initrd_start __initdata = 0;
49 static unsigned long phys_initrd_size __initdata = 0;
50
51 phys_addr_t memstart_addr __read_mostly = 0;
52
53 void __init early_init_dt_setup_initrd_arch(u64 start, u64 end)
54 {
55         phys_initrd_start = start;
56         phys_initrd_size = end - start;
57 }
58
59 static int __init early_initrd(char *p)
60 {
61         unsigned long start, size;
62         char *endp;
63
64         start = memparse(p, &endp);
65         if (*endp == ',') {
66                 size = memparse(endp + 1, NULL);
67
68                 phys_initrd_start = start;
69                 phys_initrd_size = size;
70         }
71         return 0;
72 }
73 early_param("initrd", early_initrd);
74
75 /*
76  * Return the maximum physical address for ZONE_DMA (DMA_BIT_MASK(32)). It
77  * currently assumes that for memory starting above 4G, 32-bit devices will
78  * use a DMA offset.
79  */
80 static phys_addr_t max_zone_dma_phys(void)
81 {
82         phys_addr_t offset = memblock_start_of_DRAM() & GENMASK_ULL(63, 32);
83         return min(offset + (1ULL << 32), memblock_end_of_DRAM());
84 }
85
86 static void __init zone_sizes_init(unsigned long min, unsigned long max)
87 {
88         struct memblock_region *reg;
89         unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
90         unsigned long max_dma = min;
91
92         memset(zone_size, 0, sizeof(zone_size));
93
94         /* 4GB maximum for 32-bit only capable devices */
95         if (IS_ENABLED(CONFIG_ZONE_DMA)) {
96                 max_dma = PFN_DOWN(max_zone_dma_phys());
97                 zone_size[ZONE_DMA] = max_dma - min;
98         }
99         zone_size[ZONE_NORMAL] = max - max_dma;
100
101         memcpy(zhole_size, zone_size, sizeof(zhole_size));
102
103         for_each_memblock(memory, reg) {
104                 unsigned long start = memblock_region_memory_base_pfn(reg);
105                 unsigned long end = memblock_region_memory_end_pfn(reg);
106
107                 if (start >= max)
108                         continue;
109
110                 if (IS_ENABLED(CONFIG_ZONE_DMA) && start < max_dma) {
111                         unsigned long dma_end = min(end, max_dma);
112                         zhole_size[ZONE_DMA] -= dma_end - start;
113                 }
114
115                 if (end > max_dma) {
116                         unsigned long normal_end = min(end, max);
117                         unsigned long normal_start = max(start, max_dma);
118                         zhole_size[ZONE_NORMAL] -= normal_end - normal_start;
119                 }
120         }
121
122         free_area_init_node(0, zone_size, min, zhole_size);
123 }
124
125 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
126 #define PFN_MASK ((1UL << (64 - PAGE_SHIFT)) - 1)
127
128 int pfn_valid(unsigned long pfn)
129 {
130         return (pfn & PFN_MASK) == pfn && memblock_is_memory(pfn << PAGE_SHIFT);
131 }
132 EXPORT_SYMBOL(pfn_valid);
133 #endif
134
135 #ifndef CONFIG_SPARSEMEM
136 static void arm64_memory_present(void)
137 {
138 }
139 #else
140 static void arm64_memory_present(void)
141 {
142         struct memblock_region *reg;
143
144         for_each_memblock(memory, reg)
145                 memory_present(0, memblock_region_memory_base_pfn(reg),
146                                memblock_region_memory_end_pfn(reg));
147 }
148 #endif
149
150 void __init arm64_memblock_init(void)
151 {
152         u64 *reserve_map, base, size;
153         phys_addr_t dma_phys_limit = 0;
154
155         /*
156          * Register the kernel text, kernel data, initrd, and initial
157          * pagetables with memblock.
158          */
159         memblock_reserve(__pa(_text), _end - _text);
160 #ifdef CONFIG_BLK_DEV_INITRD
161         if (phys_initrd_size) {
162                 memblock_reserve(phys_initrd_start, phys_initrd_size);
163
164                 /* Now convert initrd to virtual addresses */
165                 initrd_start = __phys_to_virt(phys_initrd_start);
166                 initrd_end = initrd_start + phys_initrd_size;
167         }
168 #endif
169
170         /* Reserve the dtb region */
171         memblock_reserve(virt_to_phys(initial_boot_params),
172                          be32_to_cpu(initial_boot_params->totalsize));
173
174         /*
175          * Process the reserve map.  This will probably overlap the initrd
176          * and dtb locations which are already reserved, but overlapping
177          * doesn't hurt anything
178          */
179         reserve_map = ((void*)initial_boot_params) +
180                         be32_to_cpu(initial_boot_params->off_mem_rsvmap);
181         while (1) {
182                 base = be64_to_cpup(reserve_map++);
183                 size = be64_to_cpup(reserve_map++);
184                 if (!size)
185                         break;
186                 memblock_reserve(base, size);
187         }
188
189         early_init_fdt_scan_reserved_mem();
190 #ifdef CONFIG_ARCH_ROCKCHIP
191         /* reserve memory for uboot */
192         rockchip_uboot_mem_reserve();
193
194         /* reserve memory for ION */
195         rockchip_ion_reserve();
196 #endif
197
198         /* 4GB maximum for 32-bit only capable devices */
199         if (IS_ENABLED(CONFIG_ZONE_DMA))
200                 dma_phys_limit = max_zone_dma_phys();
201         dma_contiguous_reserve(dma_phys_limit);
202
203         memblock_allow_resize();
204         memblock_dump_all();
205 }
206
207 void __init bootmem_init(void)
208 {
209         unsigned long min, max;
210
211         min = PFN_UP(memblock_start_of_DRAM());
212         max = PFN_DOWN(memblock_end_of_DRAM());
213
214         /*
215          * Sparsemem tries to allocate bootmem in memory_present(), so must be
216          * done after the fixed reservations.
217          */
218         arm64_memory_present();
219
220         sparse_init();
221         zone_sizes_init(min, max);
222
223         high_memory = __va((max << PAGE_SHIFT) - 1) + 1;
224         max_pfn = max_low_pfn = max;
225 }
226
227 /*
228  * Poison init memory with an undefined instruction (0x0).
229  */
230 static inline void poison_init_mem(void *s, size_t count)
231 {
232         memset(s, 0, count);
233 }
234
235 #ifndef CONFIG_SPARSEMEM_VMEMMAP
236 static inline void free_memmap(unsigned long start_pfn, unsigned long end_pfn)
237 {
238         struct page *start_pg, *end_pg;
239         unsigned long pg, pgend;
240
241         /*
242          * Convert start_pfn/end_pfn to a struct page pointer.
243          */
244         start_pg = pfn_to_page(start_pfn - 1) + 1;
245         end_pg = pfn_to_page(end_pfn - 1) + 1;
246
247         /*
248          * Convert to physical addresses, and round start upwards and end
249          * downwards.
250          */
251         pg = (unsigned long)PAGE_ALIGN(__pa(start_pg));
252         pgend = (unsigned long)__pa(end_pg) & PAGE_MASK;
253
254         /*
255          * If there are free pages between these, free the section of the
256          * memmap array.
257          */
258         if (pg < pgend)
259                 free_bootmem(pg, pgend - pg);
260 }
261
262 /*
263  * The mem_map array can get very big. Free the unused area of the memory map.
264  */
265 static void __init free_unused_memmap(void)
266 {
267         unsigned long start, prev_end = 0;
268         struct memblock_region *reg;
269
270         for_each_memblock(memory, reg) {
271                 start = __phys_to_pfn(reg->base);
272
273 #ifdef CONFIG_SPARSEMEM
274                 /*
275                  * Take care not to free memmap entries that don't exist due
276                  * to SPARSEMEM sections which aren't present.
277                  */
278                 start = min(start, ALIGN(prev_end, PAGES_PER_SECTION));
279 #endif
280                 /*
281                  * If we had a previous bank, and there is a space between the
282                  * current bank and the previous, free it.
283                  */
284                 if (prev_end && prev_end < start)
285                         free_memmap(prev_end, start);
286
287                 /*
288                  * Align up here since the VM subsystem insists that the
289                  * memmap entries are valid from the bank end aligned to
290                  * MAX_ORDER_NR_PAGES.
291                  */
292                 prev_end = ALIGN(start + __phys_to_pfn(reg->size),
293                                  MAX_ORDER_NR_PAGES);
294         }
295
296 #ifdef CONFIG_SPARSEMEM
297         if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION))
298                 free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION));
299 #endif
300 }
301 #endif  /* !CONFIG_SPARSEMEM_VMEMMAP */
302
303 /*
304  * mem_init() marks the free areas in the mem_map and tells us how much memory
305  * is free.  This is done after various parts of the system have claimed their
306  * memory after the kernel image.
307  */
308 void __init mem_init(void)
309 {
310         unsigned long reserved_pages, free_pages;
311         struct memblock_region *reg;
312
313         max_mapnr   = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
314
315 #ifndef CONFIG_SPARSEMEM_VMEMMAP
316         /* this will put all unused low memory onto the freelists */
317         free_unused_memmap();
318 #endif
319
320         totalram_pages += free_all_bootmem();
321
322         reserved_pages = free_pages = 0;
323
324         for_each_memblock(memory, reg) {
325                 unsigned int pfn1, pfn2;
326                 struct page *page, *end;
327
328                 pfn1 = __phys_to_pfn(reg->base);
329                 pfn2 = pfn1 + __phys_to_pfn(reg->size);
330
331                 page = pfn_to_page(pfn1);
332                 end  = pfn_to_page(pfn2 - 1) + 1;
333
334                 do {
335                         if (PageReserved(page))
336                                 reserved_pages++;
337                         else if (!page_count(page))
338                                 free_pages++;
339                         page++;
340                 } while (page < end);
341         }
342
343         /*
344          * Since our memory may not be contiguous, calculate the real number
345          * of pages we have in this system.
346          */
347         pr_info("Memory:");
348         num_physpages = 0;
349         for_each_memblock(memory, reg) {
350                 unsigned long pages = memblock_region_memory_end_pfn(reg) -
351                         memblock_region_memory_base_pfn(reg);
352                 num_physpages += pages;
353                 printk(" %ldMB", pages >> (20 - PAGE_SHIFT));
354         }
355         printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
356
357         pr_notice("Memory: %luk/%luk available, %luk reserved\n",
358                   nr_free_pages() << (PAGE_SHIFT-10),
359                   free_pages << (PAGE_SHIFT-10),
360                   reserved_pages << (PAGE_SHIFT-10));
361
362 #define MLK(b, t) b, t, ((t) - (b)) >> 10
363 #define MLM(b, t) b, t, ((t) - (b)) >> 20
364 #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
365
366         pr_notice("Virtual kernel memory layout:\n"
367                   "    vmalloc : 0x%16lx - 0x%16lx   (%6ld MB)\n"
368 #ifdef CONFIG_SPARSEMEM_VMEMMAP
369                   "    vmemmap : 0x%16lx - 0x%16lx   (%6ld MB)\n"
370 #endif
371                   "    modules : 0x%16lx - 0x%16lx   (%6ld MB)\n"
372                   "    memory  : 0x%16lx - 0x%16lx   (%6ld MB)\n"
373                   "      .init : 0x%p" " - 0x%p" "   (%6ld kB)\n"
374                   "      .text : 0x%p" " - 0x%p" "   (%6ld kB)\n"
375                   "      .data : 0x%p" " - 0x%p" "   (%6ld kB)\n",
376                   MLM(VMALLOC_START, VMALLOC_END),
377 #ifdef CONFIG_SPARSEMEM_VMEMMAP
378                   MLM((unsigned long)virt_to_page(PAGE_OFFSET),
379                       (unsigned long)virt_to_page(high_memory)),
380 #endif
381                   MLM(MODULES_VADDR, MODULES_END),
382                   MLM(PAGE_OFFSET, (unsigned long)high_memory),
383
384                   MLK_ROUNDUP(__init_begin, __init_end),
385                   MLK_ROUNDUP(_text, _etext),
386                   MLK_ROUNDUP(_sdata, _edata));
387
388 #undef MLK
389 #undef MLM
390 #undef MLK_ROUNDUP
391
392         /*
393          * Check boundaries twice: Some fundamental inconsistencies can be
394          * detected at build time already.
395          */
396 #ifdef CONFIG_COMPAT
397         BUILD_BUG_ON(TASK_SIZE_32                       > TASK_SIZE_64);
398 #endif
399         BUILD_BUG_ON(TASK_SIZE_64                       > MODULES_VADDR);
400         BUG_ON(TASK_SIZE_64                             > MODULES_VADDR);
401
402         if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
403                 extern int sysctl_overcommit_memory;
404                 /*
405                  * On a machine this small we won't get anywhere without
406                  * overcommit, so turn it on by default.
407                  */
408                 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
409         }
410 }
411
412 void free_initmem(void)
413 {
414         poison_init_mem(__init_begin, __init_end - __init_begin);
415         free_initmem_default(0);
416 }
417
418 #ifdef CONFIG_BLK_DEV_INITRD
419
420 static int keep_initrd;
421
422 void free_initrd_mem(unsigned long start, unsigned long end)
423 {
424         if (!keep_initrd) {
425                 poison_init_mem((void *)start, PAGE_ALIGN(end) - start);
426                 free_reserved_area(start, end, 0, "initrd");
427         }
428 }
429
430 static int __init keepinitrd_setup(char *__unused)
431 {
432         keep_initrd = 1;
433         return 1;
434 }
435
436 __setup("keepinitrd", keepinitrd_setup);
437 #endif