blob: e1ab2d7f1d646510ab17a4be31b675e88027745b [file] [log] [blame]
Vineet Guptac121c502013-01-18 15:12:20 +05301/*
2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/kernel.h>
10#include <linux/mm.h>
Vineet Guptac121c502013-01-18 15:12:20 +053011#include <linux/memblock.h>
Noam Camusd6579e92012-09-09 11:37:40 +030012#ifdef CONFIG_BLK_DEV_INITRD
13#include <linux/initrd.h>
14#endif
Alexey Brodkin1b10cb22016-04-26 19:29:34 +030015#include <linux/of_fdt.h>
Vineet Guptac121c502013-01-18 15:12:20 +053016#include <linux/swap.h>
17#include <linux/module.h>
Vineet Gupta29e33222015-10-28 19:06:10 +053018#include <linux/highmem.h>
Vineet Guptac121c502013-01-18 15:12:20 +053019#include <asm/page.h>
20#include <asm/pgalloc.h>
21#include <asm/sections.h>
22#include <asm/arcregs.h>
23
24pgd_t swapper_pg_dir[PTRS_PER_PGD] __aligned(PAGE_SIZE);
25char empty_zero_page[PAGE_SIZE] __aligned(PAGE_SIZE);
26EXPORT_SYMBOL(empty_zero_page);
27
Eugeniy Paltsev9ed68782017-08-15 21:13:54 +030028static const unsigned long low_mem_start = CONFIG_LINUX_RAM_BASE;
Vineet Gupta6101be52015-10-28 18:48:17 +053029static unsigned long low_mem_sz;
Vineet Guptac121c502013-01-18 15:12:20 +053030
Vineet Gupta29e33222015-10-28 19:06:10 +053031#ifdef CONFIG_HIGHMEM
Vineet Gupta26f9d5f2016-04-18 10:49:56 +053032static unsigned long min_high_pfn, max_high_pfn;
Vineet Gupta29e33222015-10-28 19:06:10 +053033static u64 high_mem_start;
34static u64 high_mem_sz;
35#endif
36
Vineet Gupta26f9d5f2016-04-18 10:49:56 +053037#ifdef CONFIG_DISCONTIGMEM
38struct pglist_data node_data[MAX_NUMNODES] __read_mostly;
39EXPORT_SYMBOL(node_data);
40#endif
41
Vineet Guptae497c8e2017-01-18 12:59:21 -080042long __init arc_get_mem_sz(void)
43{
44 return low_mem_sz;
45}
46
Vineet Guptac121c502013-01-18 15:12:20 +053047/* User can over-ride above with "mem=nnn[KkMm]" in cmdline */
48static int __init setup_mem_sz(char *str)
49{
Vineet Gupta6101be52015-10-28 18:48:17 +053050 low_mem_sz = memparse(str, NULL) & PAGE_MASK;
Vineet Guptac121c502013-01-18 15:12:20 +053051
52 /* early console might not be setup yet - it will show up later */
Vineet Gupta6101be52015-10-28 18:48:17 +053053 pr_info("\"mem=%s\": mem sz set to %ldM\n", str, TO_MB(low_mem_sz));
Vineet Guptac121c502013-01-18 15:12:20 +053054
55 return 0;
56}
57early_param("mem", setup_mem_sz);
58
Vineet Gupta999159a2013-01-22 17:00:52 +053059void __init early_init_dt_add_memory_arch(u64 base, u64 size)
60{
Vineet Gupta29e33222015-10-28 19:06:10 +053061 int in_use = 0;
Vineet Guptaf759ee52015-01-23 18:10:26 +053062
Vineet Gupta29e33222015-10-28 19:06:10 +053063 if (!low_mem_sz) {
Vineet Guptaff1c0b62015-12-15 13:57:16 +053064 if (base != low_mem_start)
Eugeniy Paltsev9ed68782017-08-15 21:13:54 +030065 panic("CONFIG_LINUX_RAM_BASE != DT memory { }");
Vineet Guptaff1c0b62015-12-15 13:57:16 +053066
Vineet Gupta29e33222015-10-28 19:06:10 +053067 low_mem_sz = size;
68 in_use = 1;
69 } else {
70#ifdef CONFIG_HIGHMEM
71 high_mem_start = base;
72 high_mem_sz = size;
73 in_use = 1;
74#endif
75 }
76
77 pr_info("Memory @ %llx [%lldM] %s\n",
78 base, TO_MB(size), !in_use ? "Not used":"");
Vineet Gupta999159a2013-01-22 17:00:52 +053079}
80
Vineet Guptac121c502013-01-18 15:12:20 +053081/*
82 * First memory setup routine called from setup_arch()
83 * 1. setup swapper's mm @init_mm
84 * 2. Count the pages we have and setup bootmem allocator
85 * 3. zone setup
86 */
87void __init setup_arch_memory(void)
88{
Vineet Guptaf2e2013f72015-02-13 13:59:53 +053089 unsigned long zones_size[MAX_NR_ZONES];
Vineet Gupta29e33222015-10-28 19:06:10 +053090 unsigned long zones_holes[MAX_NR_ZONES];
Vineet Guptac121c502013-01-18 15:12:20 +053091
92 init_mm.start_code = (unsigned long)_text;
93 init_mm.end_code = (unsigned long)_etext;
94 init_mm.end_data = (unsigned long)_edata;
95 init_mm.brk = (unsigned long)_end;
96
Vineet Guptac121c502013-01-18 15:12:20 +053097 /* first page of system - kernel .vector starts here */
Vineet Guptaf2e2013f72015-02-13 13:59:53 +053098 min_low_pfn = ARCH_PFN_OFFSET;
Vineet Guptac121c502013-01-18 15:12:20 +053099
Vineet Gupta6101be52015-10-28 18:48:17 +0530100 /* Last usable page of low mem */
101 max_low_pfn = max_pfn = PFN_DOWN(low_mem_start + low_mem_sz);
Vineet Guptac121c502013-01-18 15:12:20 +0530102
Vineet Gupta26f9d5f2016-04-18 10:49:56 +0530103#ifdef CONFIG_FLATMEM
104 /* pfn_valid() uses this */
105 max_mapnr = max_low_pfn - min_low_pfn;
Vineet Gupta29e33222015-10-28 19:06:10 +0530106#endif
107
Vineet Gupta6101be52015-10-28 18:48:17 +0530108 /*------------- bootmem allocator setup -----------------------*/
Vineet Gupta29e33222015-10-28 19:06:10 +0530109
110 /*
111 * seed the bootmem allocator after any DT memory node parsing or
112 * "mem=xxx" cmdline overrides have potentially updated @arc_mem_sz
113 *
114 * Only low mem is added, otherwise we have crashes when allocating
115 * mem_map[] itself. NO_BOOTMEM allocates mem_map[] at the end of
116 * avail memory, ending in highmem with a > 32-bit address. However
117 * it then tries to memset it with a truncaed 32-bit handle, causing
118 * the crash
119 */
120
Vineet Gupta26f9d5f2016-04-18 10:49:56 +0530121 memblock_add_node(low_mem_start, low_mem_sz, 0);
Eugeniy Paltseva3010a02018-12-19 19:16:16 +0300122 memblock_reserve(CONFIG_LINUX_LINK_BASE,
123 __pa(_end) - CONFIG_LINUX_LINK_BASE);
Vineet Guptac121c502013-01-18 15:12:20 +0530124
Noam Camusd6579e92012-09-09 11:37:40 +0300125#ifdef CONFIG_BLK_DEV_INITRD
Florian Fainelli229c55cc2018-11-05 14:54:31 -0800126 if (phys_initrd_size) {
127 memblock_reserve(phys_initrd_start, phys_initrd_size);
128 initrd_start = (unsigned long)__va(phys_initrd_start);
129 initrd_end = initrd_start + phys_initrd_size;
130 }
Noam Camusd6579e92012-09-09 11:37:40 +0300131#endif
132
Alexey Brodkin1b10cb22016-04-26 19:29:34 +0300133 early_init_fdt_reserve_self();
134 early_init_fdt_scan_reserved_mem();
135
Vineet Guptac121c502013-01-18 15:12:20 +0530136 memblock_dump_all();
137
Vineet Gupta6101be52015-10-28 18:48:17 +0530138 /*----------------- node/zones setup --------------------------*/
Vineet Guptac121c502013-01-18 15:12:20 +0530139 memset(zones_size, 0, sizeof(zones_size));
Vineet Gupta29e33222015-10-28 19:06:10 +0530140 memset(zones_holes, 0, sizeof(zones_holes));
141
Vineet Gupta6101be52015-10-28 18:48:17 +0530142 zones_size[ZONE_NORMAL] = max_low_pfn - min_low_pfn;
Vineet Gupta29e33222015-10-28 19:06:10 +0530143 zones_holes[ZONE_NORMAL] = 0;
144
Vineet Guptac121c502013-01-18 15:12:20 +0530145 /*
146 * We can't use the helper free_area_init(zones[]) because it uses
147 * PAGE_OFFSET to compute the @min_low_pfn which would be wrong
148 * when our kernel doesn't start at PAGE_OFFSET, i.e.
Eugeniy Paltsev9ed68782017-08-15 21:13:54 +0300149 * PAGE_OFFSET != CONFIG_LINUX_RAM_BASE
Vineet Guptac121c502013-01-18 15:12:20 +0530150 */
151 free_area_init_node(0, /* node-id */
152 zones_size, /* num pages per zone */
153 min_low_pfn, /* first pfn of node */
Vineet Gupta29e33222015-10-28 19:06:10 +0530154 zones_holes); /* holes */
Vineet Guptaf2e2013f72015-02-13 13:59:53 +0530155
Vineet Gupta29e33222015-10-28 19:06:10 +0530156#ifdef CONFIG_HIGHMEM
Vineet Gupta26f9d5f2016-04-18 10:49:56 +0530157 /*
158 * Populate a new node with highmem
159 *
160 * On ARC (w/o PAE) HIGHMEM addresses are actually smaller (0 based)
161 * than addresses in normal ala low memory (0x8000_0000 based).
162 * Even with PAE, the huge peripheral space hole would waste a lot of
163 * mem with single mem_map[]. This warrants a mem_map per region design.
164 * Thus HIGHMEM on ARC is imlemented with DISCONTIGMEM.
165 *
166 * DISCONTIGMEM in turns requires multiple nodes. node 0 above is
167 * populated with normal memory zone while node 1 only has highmem
168 */
169 node_set_online(1);
170
171 min_high_pfn = PFN_DOWN(high_mem_start);
172 max_high_pfn = PFN_DOWN(high_mem_start + high_mem_sz);
173
174 zones_size[ZONE_NORMAL] = 0;
175 zones_holes[ZONE_NORMAL] = 0;
176
177 zones_size[ZONE_HIGHMEM] = max_high_pfn - min_high_pfn;
178 zones_holes[ZONE_HIGHMEM] = 0;
179
180 free_area_init_node(1, /* node-id */
181 zones_size, /* num pages per zone */
182 min_high_pfn, /* first pfn of node */
183 zones_holes); /* holes */
184
Vineet Gupta29e33222015-10-28 19:06:10 +0530185 high_memory = (void *)(min_high_pfn << PAGE_SHIFT);
186 kmap_init();
187#endif
Vineet Guptac121c502013-01-18 15:12:20 +0530188}
189
190/*
191 * mem_init - initializes memory
192 *
193 * Frees up bootmem
194 * Calculates and displays memory available/used
195 */
196void __init mem_init(void)
197{
Vineet Gupta29e33222015-10-28 19:06:10 +0530198#ifdef CONFIG_HIGHMEM
199 unsigned long tmp;
200
201 reset_all_zones_managed_pages();
Vineet Gupta26f9d5f2016-04-18 10:49:56 +0530202 for (tmp = min_high_pfn; tmp < max_high_pfn; tmp++)
Vineet Gupta29e33222015-10-28 19:06:10 +0530203 free_highmem_page(pfn_to_page(tmp));
204#endif
205
Mike Rapoportc6ffc5c2018-10-30 15:09:30 -0700206 memblock_free_all();
Jiang Liude35e1b2013-07-03 15:03:47 -0700207 mem_init_print_info(NULL);
Vineet Guptac121c502013-01-18 15:12:20 +0530208}
209
Vineet Guptac121c502013-01-18 15:12:20 +0530210/*
211 * free_initmem: Free all the __init memory.
212 */
Fabian Frederickbd721ea2016-08-02 14:03:33 -0700213void __ref free_initmem(void)
Vineet Guptac121c502013-01-18 15:12:20 +0530214{
Jiang Liudbe67df2013-07-03 15:02:51 -0700215 free_initmem_default(-1);
Vineet Guptac121c502013-01-18 15:12:20 +0530216}
217
218#ifdef CONFIG_BLK_DEV_INITRD
219void __init free_initrd_mem(unsigned long start, unsigned long end)
220{
Jiang Liudbe67df2013-07-03 15:02:51 -0700221 free_reserved_area((void *)start, (void *)end, -1, "initrd");
Vineet Guptac121c502013-01-18 15:12:20 +0530222}
223#endif