blob: 7d2c4fbf4f22eb1402bc666c077c652c40f38361 [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>
11#include <linux/bootmem.h>
12#include <linux/memblock.h>
Noam Camusd6579e92012-09-09 11:37:40 +030013#ifdef CONFIG_BLK_DEV_INITRD
14#include <linux/initrd.h>
15#endif
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
Vineet Gupta6101be52015-10-28 18:48:17 +053028static const unsigned long low_mem_start = CONFIG_LINUX_LINK_BASE;
29static unsigned long low_mem_sz;
Vineet Guptac121c502013-01-18 15:12:20 +053030
Vineet Gupta29e33222015-10-28 19:06:10 +053031#ifdef CONFIG_HIGHMEM
32static unsigned long min_high_pfn;
33static u64 high_mem_start;
34static u64 high_mem_sz;
35#endif
36
Vineet Guptac121c502013-01-18 15:12:20 +053037/* User can over-ride above with "mem=nnn[KkMm]" in cmdline */
38static int __init setup_mem_sz(char *str)
39{
Vineet Gupta6101be52015-10-28 18:48:17 +053040 low_mem_sz = memparse(str, NULL) & PAGE_MASK;
Vineet Guptac121c502013-01-18 15:12:20 +053041
42 /* early console might not be setup yet - it will show up later */
Vineet Gupta6101be52015-10-28 18:48:17 +053043 pr_info("\"mem=%s\": mem sz set to %ldM\n", str, TO_MB(low_mem_sz));
Vineet Guptac121c502013-01-18 15:12:20 +053044
45 return 0;
46}
47early_param("mem", setup_mem_sz);
48
Vineet Gupta999159a2013-01-22 17:00:52 +053049void __init early_init_dt_add_memory_arch(u64 base, u64 size)
50{
Vineet Gupta29e33222015-10-28 19:06:10 +053051 int in_use = 0;
Vineet Guptaf759ee52015-01-23 18:10:26 +053052
Vineet Gupta29e33222015-10-28 19:06:10 +053053 if (!low_mem_sz) {
Vineet Guptaff1c0b62015-12-15 13:57:16 +053054 if (base != low_mem_start)
55 panic("CONFIG_LINUX_LINK_BASE != DT memory { }");
56
Vineet Gupta29e33222015-10-28 19:06:10 +053057 low_mem_sz = size;
58 in_use = 1;
59 } else {
60#ifdef CONFIG_HIGHMEM
61 high_mem_start = base;
62 high_mem_sz = size;
63 in_use = 1;
64#endif
65 }
66
67 pr_info("Memory @ %llx [%lldM] %s\n",
68 base, TO_MB(size), !in_use ? "Not used":"");
Vineet Gupta999159a2013-01-22 17:00:52 +053069}
70
Noam Camusd6579e92012-09-09 11:37:40 +030071#ifdef CONFIG_BLK_DEV_INITRD
72static int __init early_initrd(char *p)
73{
74 unsigned long start, size;
75 char *endp;
76
77 start = memparse(p, &endp);
78 if (*endp == ',') {
79 size = memparse(endp + 1, NULL);
80
81 initrd_start = (unsigned long)__va(start);
82 initrd_end = (unsigned long)__va(start + size);
83 }
84 return 0;
85}
86early_param("initrd", early_initrd);
87#endif
88
Vineet Guptac121c502013-01-18 15:12:20 +053089/*
90 * First memory setup routine called from setup_arch()
91 * 1. setup swapper's mm @init_mm
92 * 2. Count the pages we have and setup bootmem allocator
93 * 3. zone setup
94 */
95void __init setup_arch_memory(void)
96{
Vineet Guptaf2e2013f72015-02-13 13:59:53 +053097 unsigned long zones_size[MAX_NR_ZONES];
Vineet Gupta29e33222015-10-28 19:06:10 +053098 unsigned long zones_holes[MAX_NR_ZONES];
Vineet Guptac121c502013-01-18 15:12:20 +053099
100 init_mm.start_code = (unsigned long)_text;
101 init_mm.end_code = (unsigned long)_etext;
102 init_mm.end_data = (unsigned long)_edata;
103 init_mm.brk = (unsigned long)_end;
104
Vineet Guptac121c502013-01-18 15:12:20 +0530105 /* first page of system - kernel .vector starts here */
Vineet Guptaf2e2013f72015-02-13 13:59:53 +0530106 min_low_pfn = ARCH_PFN_OFFSET;
Vineet Guptac121c502013-01-18 15:12:20 +0530107
Vineet Gupta6101be52015-10-28 18:48:17 +0530108 /* Last usable page of low mem */
109 max_low_pfn = max_pfn = PFN_DOWN(low_mem_start + low_mem_sz);
Vineet Guptac121c502013-01-18 15:12:20 +0530110
Vineet Gupta29e33222015-10-28 19:06:10 +0530111#ifdef CONFIG_HIGHMEM
112 min_high_pfn = PFN_DOWN(high_mem_start);
113 max_pfn = PFN_DOWN(high_mem_start + high_mem_sz);
114#endif
115
116 max_mapnr = max_pfn - min_low_pfn;
Vineet Guptac121c502013-01-18 15:12:20 +0530117
Vineet Gupta6101be52015-10-28 18:48:17 +0530118 /*------------- bootmem allocator setup -----------------------*/
Vineet Gupta29e33222015-10-28 19:06:10 +0530119
120 /*
121 * seed the bootmem allocator after any DT memory node parsing or
122 * "mem=xxx" cmdline overrides have potentially updated @arc_mem_sz
123 *
124 * Only low mem is added, otherwise we have crashes when allocating
125 * mem_map[] itself. NO_BOOTMEM allocates mem_map[] at the end of
126 * avail memory, ending in highmem with a > 32-bit address. However
127 * it then tries to memset it with a truncaed 32-bit handle, causing
128 * the crash
129 */
130
Vineet Gupta6101be52015-10-28 18:48:17 +0530131 memblock_add(low_mem_start, low_mem_sz);
132 memblock_reserve(low_mem_start, __pa(_end) - low_mem_start);
Vineet Guptac121c502013-01-18 15:12:20 +0530133
Noam Camusd6579e92012-09-09 11:37:40 +0300134#ifdef CONFIG_BLK_DEV_INITRD
Noam Camusd6579e92012-09-09 11:37:40 +0300135 if (initrd_start)
136 memblock_reserve(__pa(initrd_start), initrd_end - initrd_start);
137#endif
138
Vineet Guptac121c502013-01-18 15:12:20 +0530139 memblock_dump_all();
140
Vineet Gupta6101be52015-10-28 18:48:17 +0530141 /*----------------- node/zones setup --------------------------*/
Vineet Guptac121c502013-01-18 15:12:20 +0530142 memset(zones_size, 0, sizeof(zones_size));
Vineet Gupta29e33222015-10-28 19:06:10 +0530143 memset(zones_holes, 0, sizeof(zones_holes));
144
Vineet Gupta6101be52015-10-28 18:48:17 +0530145 zones_size[ZONE_NORMAL] = max_low_pfn - min_low_pfn;
Vineet Gupta29e33222015-10-28 19:06:10 +0530146 zones_holes[ZONE_NORMAL] = 0;
147
148#ifdef CONFIG_HIGHMEM
149 zones_size[ZONE_HIGHMEM] = max_pfn - max_low_pfn;
150
151 /* This handles the peripheral address space hole */
152 zones_holes[ZONE_HIGHMEM] = min_high_pfn - max_low_pfn;
153#endif
Vineet Guptac121c502013-01-18 15:12:20 +0530154
155 /*
156 * We can't use the helper free_area_init(zones[]) because it uses
157 * PAGE_OFFSET to compute the @min_low_pfn which would be wrong
158 * when our kernel doesn't start at PAGE_OFFSET, i.e.
159 * PAGE_OFFSET != CONFIG_LINUX_LINK_BASE
160 */
161 free_area_init_node(0, /* node-id */
162 zones_size, /* num pages per zone */
163 min_low_pfn, /* first pfn of node */
Vineet Gupta29e33222015-10-28 19:06:10 +0530164 zones_holes); /* holes */
Vineet Guptaf2e2013f72015-02-13 13:59:53 +0530165
Vineet Gupta29e33222015-10-28 19:06:10 +0530166#ifdef CONFIG_HIGHMEM
167 high_memory = (void *)(min_high_pfn << PAGE_SHIFT);
168 kmap_init();
169#endif
Vineet Guptac121c502013-01-18 15:12:20 +0530170}
171
172/*
173 * mem_init - initializes memory
174 *
175 * Frees up bootmem
176 * Calculates and displays memory available/used
177 */
178void __init mem_init(void)
179{
Vineet Gupta29e33222015-10-28 19:06:10 +0530180#ifdef CONFIG_HIGHMEM
181 unsigned long tmp;
182
183 reset_all_zones_managed_pages();
184 for (tmp = min_high_pfn; tmp < max_pfn; tmp++)
185 free_highmem_page(pfn_to_page(tmp));
186#endif
187
Jiang Liu0c988532013-07-03 15:03:24 -0700188 free_all_bootmem();
Jiang Liude35e1b2013-07-03 15:03:47 -0700189 mem_init_print_info(NULL);
Vineet Guptac121c502013-01-18 15:12:20 +0530190}
191
Vineet Guptac121c502013-01-18 15:12:20 +0530192/*
193 * free_initmem: Free all the __init memory.
194 */
195void __init_refok free_initmem(void)
196{
Jiang Liudbe67df2013-07-03 15:02:51 -0700197 free_initmem_default(-1);
Vineet Guptac121c502013-01-18 15:12:20 +0530198}
199
200#ifdef CONFIG_BLK_DEV_INITRD
201void __init free_initrd_mem(unsigned long start, unsigned long end)
202{
Jiang Liudbe67df2013-07-03 15:02:51 -0700203 free_reserved_area((void *)start, (void *)end, -1, "initrd");
Vineet Guptac121c502013-01-18 15:12:20 +0530204}
205#endif