blob: d898ed67d890c7c49625c447db711397b788cc45 [file] [log] [blame]
Chris Zankel3f65ce42005-06-23 22:01:24 -07001/*
2 * arch/xtensa/mm/init.c
3 *
4 * Derived from MIPS, PPC.
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 *
10 * Copyright (C) 2001 - 2005 Tensilica Inc.
Max Filippov0e46c112016-04-25 22:08:20 +030011 * Copyright (C) 2014 - 2016 Cadence Design Systems Inc.
Chris Zankel3f65ce42005-06-23 22:01:24 -070012 *
13 * Chris Zankel <chris@zankel.net>
14 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
15 * Marc Gauthier
16 * Kevin Chea
17 */
18
Chris Zankel3f65ce42005-06-23 22:01:24 -070019#include <linux/kernel.h>
20#include <linux/errno.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070021#include <linux/memblock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Max Filippov655591002014-02-04 02:17:09 +040023#include <linux/highmem.h>
Chris Zankel3f65ce42005-06-23 22:01:24 -070024#include <linux/swap.h>
Chris Zankel66569202007-08-22 10:14:51 -070025#include <linux/mman.h>
26#include <linux/nodemask.h>
27#include <linux/mm.h>
Max Filippov4e7c84e2016-07-19 00:37:05 +030028#include <linux/of_fdt.h>
Max Filippov9d2ffe52016-04-25 22:08:52 +030029#include <linux/dma-contiguous.h>
Chris Zankel3f65ce42005-06-23 22:01:24 -070030
Chris Zankel3f65ce42005-06-23 22:01:24 -070031#include <asm/bootparam.h>
Chris Zankel3f65ce42005-06-23 22:01:24 -070032#include <asm/page.h>
Geert Uytterhoevenf022d0f2012-06-20 12:52:59 -070033#include <asm/sections.h>
Max Filippov9ba067f2014-03-23 03:17:43 +040034#include <asm/sysmem.h>
35
Chris Zankel3f65ce42005-06-23 22:01:24 -070036/*
Max Filippove9d6dca2014-01-31 05:38:21 +040037 * Initialize the bootmem system and give it all low memory we have available.
Chris Zankel3f65ce42005-06-23 22:01:24 -070038 */
39
40void __init bootmem_init(void)
41{
Max Filippov0e46c112016-04-25 22:08:20 +030042 /* Reserve all memory below PHYS_OFFSET, as memory
Max Filippov566fb582014-10-04 03:46:34 +040043 * accounting doesn't work for pages below that address.
44 *
Max Filippov0e46c112016-04-25 22:08:20 +030045 * If PHYS_OFFSET is zero reserve page at address 0:
Max Filippov566fb582014-10-04 03:46:34 +040046 * successfull allocations should never return NULL.
47 */
Markus Elfring831c4f32019-07-05 18:33:58 +020048 memblock_reserve(0, PHYS_OFFSET ? PHYS_OFFSET : 1);
Max Filippov566fb582014-10-04 03:46:34 +040049
Max Filippov4e7c84e2016-07-19 00:37:05 +030050 early_init_fdt_scan_reserved_mem();
Chris Zankel3f65ce42005-06-23 22:01:24 -070051
Max Filippov0e46c112016-04-25 22:08:20 +030052 if (!memblock_phys_mem_size())
Chris Zankel3f65ce42005-06-23 22:01:24 -070053 panic("No memory found!\n");
54
Max Filippov0e46c112016-04-25 22:08:20 +030055 min_low_pfn = PFN_UP(memblock_start_of_DRAM());
56 min_low_pfn = max(min_low_pfn, PFN_UP(PHYS_OFFSET));
57 max_pfn = PFN_DOWN(memblock_end_of_DRAM());
Max Filippovd39af902016-04-11 21:14:17 +030058 max_low_pfn = min(max_pfn, MAX_LOW_PFN);
Chris Zankel3f65ce42005-06-23 22:01:24 -070059
Max Filippov4e460f62018-12-14 14:41:58 -080060 early_memtest((phys_addr_t)min_low_pfn << PAGE_SHIFT,
61 (phys_addr_t)max_low_pfn << PAGE_SHIFT);
62
Max Filippov0e46c112016-04-25 22:08:20 +030063 memblock_set_current_limit(PFN_PHYS(max_low_pfn));
Max Filippov9d2ffe52016-04-25 22:08:52 +030064 dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
Chris Zankel3f65ce42005-06-23 22:01:24 -070065
Max Filippov0e46c112016-04-25 22:08:20 +030066 memblock_dump_all();
Chris Zankel3f65ce42005-06-23 22:01:24 -070067}
68
69
Johannes Weinere5083a62009-03-04 16:21:31 +010070void __init zones_init(void)
Chris Zankel3f65ce42005-06-23 22:01:24 -070071{
Chris Zankel3f65ce42005-06-23 22:01:24 -070072 /* All pages are DMA-able, so we put them all in the DMA zone. */
Max Filippov655591002014-02-04 02:17:09 +040073 unsigned long zones_size[MAX_NR_ZONES] = {
Christoph Hellwigfe278d12018-10-14 20:13:04 +020074 [ZONE_NORMAL] = max_low_pfn - ARCH_PFN_OFFSET,
Chris Zankel3f65ce42005-06-23 22:01:24 -070075#ifdef CONFIG_HIGHMEM
Max Filippov655591002014-02-04 02:17:09 +040076 [ZONE_HIGHMEM] = max_pfn - max_low_pfn,
Chris Zankel3f65ce42005-06-23 22:01:24 -070077#endif
Max Filippov655591002014-02-04 02:17:09 +040078 };
Johannes Weinerc947a582009-03-04 16:21:30 +010079 free_area_init_node(0, zones_size, ARCH_PFN_OFFSET, NULL);
Chris Zankel3f65ce42005-06-23 22:01:24 -070080}
81
Max Filippov6ac5a112018-02-13 15:31:05 -080082#ifdef CONFIG_HIGHMEM
83static void __init free_area_high(unsigned long pfn, unsigned long end)
84{
85 for (; pfn < end; pfn++)
86 free_highmem_page(pfn_to_page(pfn));
87}
88
89static void __init free_highpages(void)
90{
91 unsigned long max_low = max_low_pfn;
92 struct memblock_region *mem, *res;
93
94 reset_all_zones_managed_pages();
95 /* set highmem page free */
96 for_each_memblock(memory, mem) {
97 unsigned long start = memblock_region_memory_base_pfn(mem);
98 unsigned long end = memblock_region_memory_end_pfn(mem);
99
100 /* Ignore complete lowmem entries */
101 if (end <= max_low)
102 continue;
103
104 if (memblock_is_nomap(mem))
105 continue;
106
107 /* Truncate partial highmem entries */
108 if (start < max_low)
109 start = max_low;
110
111 /* Find and exclude any reserved regions */
112 for_each_memblock(reserved, res) {
113 unsigned long res_start, res_end;
114
115 res_start = memblock_region_reserved_base_pfn(res);
116 res_end = memblock_region_reserved_end_pfn(res);
117
118 if (res_end < start)
119 continue;
120 if (res_start < start)
121 res_start = start;
122 if (res_start > end)
123 res_start = end;
124 if (res_end > end)
125 res_end = end;
126 if (res_start != start)
127 free_area_high(start, res_start);
128 start = res_end;
129 if (start == end)
130 break;
131 }
132
133 /* And now free anything which remains */
134 if (start < end)
135 free_area_high(start, end);
136 }
137}
138#else
139static void __init free_highpages(void)
140{
141}
142#endif
143
Chris Zankel3f65ce42005-06-23 22:01:24 -0700144/*
Chris Zankel3f65ce42005-06-23 22:01:24 -0700145 * Initialize memory pages.
146 */
147
148void __init mem_init(void)
149{
Max Filippov6ac5a112018-02-13 15:31:05 -0800150 free_highpages();
Chris Zankel3f65ce42005-06-23 22:01:24 -0700151
Max Filippov655591002014-02-04 02:17:09 +0400152 max_mapnr = max_pfn - ARCH_PFN_OFFSET;
153 high_memory = (void *)__va(max_low_pfn << PAGE_SHIFT);
154
Mike Rapoportc6ffc5c2018-10-30 15:09:30 -0700155 memblock_free_all();
Chris Zankel3f65ce42005-06-23 22:01:24 -0700156
Jiang Liu808c2c32013-07-03 15:04:20 -0700157 mem_init_print_info(NULL);
Max Filippov655591002014-02-04 02:17:09 +0400158 pr_info("virtual kernel memory layout:\n"
Max Filippovc6335442017-12-03 13:28:52 -0800159#ifdef CONFIG_KASAN
160 " kasan : 0x%08lx - 0x%08lx (%5lu MB)\n"
161#endif
Max Filippov1af1e8a2017-12-03 19:09:41 -0800162#ifdef CONFIG_MMU
163 " vmalloc : 0x%08lx - 0x%08lx (%5lu MB)\n"
164#endif
Max Filippov655591002014-02-04 02:17:09 +0400165#ifdef CONFIG_HIGHMEM
166 " pkmap : 0x%08lx - 0x%08lx (%5lu kB)\n"
167 " fixmap : 0x%08lx - 0x%08lx (%5lu kB)\n"
168#endif
Max Filippovfed566c2017-12-10 20:15:37 -0800169 " lowmem : 0x%08lx - 0x%08lx (%5lu MB)\n"
170 " .text : 0x%08lx - 0x%08lx (%5lu kB)\n"
171 " .rodata : 0x%08lx - 0x%08lx (%5lu kB)\n"
172 " .data : 0x%08lx - 0x%08lx (%5lu kB)\n"
173 " .init : 0x%08lx - 0x%08lx (%5lu kB)\n"
174 " .bss : 0x%08lx - 0x%08lx (%5lu kB)\n",
Max Filippovc6335442017-12-03 13:28:52 -0800175#ifdef CONFIG_KASAN
176 KASAN_SHADOW_START, KASAN_SHADOW_START + KASAN_SHADOW_SIZE,
177 KASAN_SHADOW_SIZE >> 20,
178#endif
Max Filippov1af1e8a2017-12-03 19:09:41 -0800179#ifdef CONFIG_MMU
180 VMALLOC_START, VMALLOC_END,
181 (VMALLOC_END - VMALLOC_START) >> 20,
Max Filippov655591002014-02-04 02:17:09 +0400182#ifdef CONFIG_HIGHMEM
183 PKMAP_BASE, PKMAP_BASE + LAST_PKMAP * PAGE_SIZE,
184 (LAST_PKMAP*PAGE_SIZE) >> 10,
185 FIXADDR_START, FIXADDR_TOP,
186 (FIXADDR_TOP - FIXADDR_START) >> 10,
187#endif
Max Filippov655591002014-02-04 02:17:09 +0400188 PAGE_OFFSET, PAGE_OFFSET +
189 (max_low_pfn - min_low_pfn) * PAGE_SIZE,
Max Filippov5a0b1d72014-10-04 03:50:33 +0400190#else
191 min_low_pfn * PAGE_SIZE, max_low_pfn * PAGE_SIZE,
192#endif
Max Filippovfed566c2017-12-10 20:15:37 -0800193 ((max_low_pfn - min_low_pfn) * PAGE_SIZE) >> 20,
194 (unsigned long)_text, (unsigned long)_etext,
195 (unsigned long)(_etext - _text) >> 10,
196 (unsigned long)__start_rodata, (unsigned long)_sdata,
197 (unsigned long)(_sdata - __start_rodata) >> 10,
198 (unsigned long)_sdata, (unsigned long)_edata,
199 (unsigned long)(_edata - _sdata) >> 10,
200 (unsigned long)__init_begin, (unsigned long)__init_end,
201 (unsigned long)(__init_end - __init_begin) >> 10,
202 (unsigned long)__bss_start, (unsigned long)__bss_stop,
203 (unsigned long)(__bss_stop - __bss_start) >> 10);
Chris Zankel3f65ce42005-06-23 22:01:24 -0700204}
205
Max Filippov06bd2822014-03-21 21:04:40 +0400206static void __init parse_memmap_one(char *p)
207{
208 char *oldp;
209 unsigned long start_at, mem_size;
210
211 if (!p)
212 return;
213
214 oldp = p;
215 mem_size = memparse(p, &p);
216 if (p == oldp)
217 return;
218
219 switch (*p) {
220 case '@':
221 start_at = memparse(p + 1, &p);
Max Filippov0e46c112016-04-25 22:08:20 +0300222 memblock_add(start_at, mem_size);
Max Filippov06bd2822014-03-21 21:04:40 +0400223 break;
224
225 case '$':
226 start_at = memparse(p + 1, &p);
Max Filippov0e46c112016-04-25 22:08:20 +0300227 memblock_reserve(start_at, mem_size);
Max Filippov06bd2822014-03-21 21:04:40 +0400228 break;
229
230 case 0:
Max Filippov0e46c112016-04-25 22:08:20 +0300231 memblock_reserve(mem_size, -mem_size);
Max Filippov06bd2822014-03-21 21:04:40 +0400232 break;
233
234 default:
235 pr_warn("Unrecognized memmap syntax: %s\n", p);
236 break;
237 }
238}
239
240static int __init parse_memmap_opt(char *str)
241{
242 while (str) {
243 char *k = strchr(str, ',');
244
245 if (k)
246 *k++ = 0;
247
248 parse_memmap_one(str);
249 str = k;
250 }
251
252 return 0;
253}
254early_param("memmap", parse_memmap_opt);