blob: 4f78264e8bd810703db6b99d3ba7307314e2e83b [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.
11 *
12 * Chris Zankel <chris@zankel.net>
13 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
14 * Marc Gauthier
15 * Kevin Chea
16 */
17
Chris Zankel3f65ce42005-06-23 22:01:24 -070018#include <linux/kernel.h>
19#include <linux/errno.h>
Chris Zankel3f65ce42005-06-23 22:01:24 -070020#include <linux/bootmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/gfp.h>
Chris Zankel3f65ce42005-06-23 22:01:24 -070022#include <linux/swap.h>
Chris Zankel66569202007-08-22 10:14:51 -070023#include <linux/mman.h>
24#include <linux/nodemask.h>
25#include <linux/mm.h>
Chris Zankel3f65ce42005-06-23 22:01:24 -070026
Chris Zankel3f65ce42005-06-23 22:01:24 -070027#include <asm/bootparam.h>
Chris Zankel3f65ce42005-06-23 22:01:24 -070028#include <asm/page.h>
Geert Uytterhoevenf022d0f2012-06-20 12:52:59 -070029#include <asm/sections.h>
Max Filippov9ba067f2014-03-23 03:17:43 +040030#include <asm/sysmem.h>
31
32struct sysmem_info sysmem __initdata;
33
34int __init add_sysmem_bank(unsigned long start, unsigned long end)
35{
36 if (sysmem.nr_banks >= SYSMEM_BANKS_MAX) {
37 pr_warn("Ignoring memory bank 0x%08lx size %ldKB\n",
38 start, end - start);
39 return -EINVAL;
40 }
41 sysmem.bank[sysmem.nr_banks].start = PAGE_ALIGN(start);
42 sysmem.bank[sysmem.nr_banks].end = end & PAGE_MASK;
43 sysmem.nr_banks++;
44
45 return 0;
46}
Chris Zankel3f65ce42005-06-23 22:01:24 -070047
48/*
49 * mem_reserve(start, end, must_exist)
50 *
51 * Reserve some memory from the memory pool.
52 *
53 * Parameters:
54 * start Start of region,
55 * end End of region,
56 * must_exist Must exist in memory pool.
57 *
58 * Returns:
59 * 0 (memory area couldn't be mapped)
60 * -1 (success)
61 */
62
63int __init mem_reserve(unsigned long start, unsigned long end, int must_exist)
64{
65 int i;
66
67 if (start == end)
68 return 0;
69
70 start = start & PAGE_MASK;
71 end = PAGE_ALIGN(end);
72
73 for (i = 0; i < sysmem.nr_banks; i++)
74 if (start < sysmem.bank[i].end
75 && end >= sysmem.bank[i].start)
76 break;
77
78 if (i == sysmem.nr_banks) {
79 if (must_exist)
80 printk (KERN_WARNING "mem_reserve: [0x%0lx, 0x%0lx) "
81 "not in any region!\n", start, end);
82 return 0;
83 }
84
85 if (start > sysmem.bank[i].start) {
86 if (end < sysmem.bank[i].end) {
87 /* split entry */
88 if (sysmem.nr_banks >= SYSMEM_BANKS_MAX)
89 panic("meminfo overflow\n");
90 sysmem.bank[sysmem.nr_banks].start = end;
91 sysmem.bank[sysmem.nr_banks].end = sysmem.bank[i].end;
92 sysmem.nr_banks++;
93 }
94 sysmem.bank[i].end = start;
Chris Zankelc4c45942012-11-28 16:53:51 -080095
96 } else if (end < sysmem.bank[i].end) {
97 sysmem.bank[i].start = end;
98
Chris Zankel3f65ce42005-06-23 22:01:24 -070099 } else {
Chris Zankelc4c45942012-11-28 16:53:51 -0800100 /* remove entry */
101 sysmem.nr_banks--;
102 sysmem.bank[i].start = sysmem.bank[sysmem.nr_banks].start;
103 sysmem.bank[i].end = sysmem.bank[sysmem.nr_banks].end;
Chris Zankel3f65ce42005-06-23 22:01:24 -0700104 }
105 return -1;
106}
107
108
109/*
Max Filippove9d6dca2014-01-31 05:38:21 +0400110 * Initialize the bootmem system and give it all low memory we have available.
Chris Zankel3f65ce42005-06-23 22:01:24 -0700111 */
112
113void __init bootmem_init(void)
114{
115 unsigned long pfn;
116 unsigned long bootmap_start, bootmap_size;
117 int i;
118
119 max_low_pfn = max_pfn = 0;
120 min_low_pfn = ~0;
121
122 for (i=0; i < sysmem.nr_banks; i++) {
123 pfn = PAGE_ALIGN(sysmem.bank[i].start) >> PAGE_SHIFT;
124 if (pfn < min_low_pfn)
125 min_low_pfn = pfn;
126 pfn = PAGE_ALIGN(sysmem.bank[i].end - 1) >> PAGE_SHIFT;
127 if (pfn > max_pfn)
128 max_pfn = pfn;
129 }
130
131 if (min_low_pfn > max_pfn)
132 panic("No memory found!\n");
133
Chris Zankel173d6682006-12-10 02:18:48 -0800134 max_low_pfn = max_pfn < MAX_MEM_PFN >> PAGE_SHIFT ?
135 max_pfn : MAX_MEM_PFN >> PAGE_SHIFT;
Chris Zankel3f65ce42005-06-23 22:01:24 -0700136
137 /* Find an area to use for the bootmem bitmap. */
138
Johannes Weiner264da9f2009-03-04 16:21:29 +0100139 bootmap_size = bootmem_bootmap_pages(max_low_pfn - min_low_pfn);
140 bootmap_size <<= PAGE_SHIFT;
Chris Zankel3f65ce42005-06-23 22:01:24 -0700141 bootmap_start = ~0;
142
143 for (i=0; i<sysmem.nr_banks; i++)
144 if (sysmem.bank[i].end - sysmem.bank[i].start >= bootmap_size) {
145 bootmap_start = sysmem.bank[i].start;
146 break;
147 }
148
149 if (bootmap_start == ~0UL)
150 panic("Cannot find %ld bytes for bootmap\n", bootmap_size);
151
152 /* Reserve the bootmem bitmap area */
153
154 mem_reserve(bootmap_start, bootmap_start + bootmap_size, 1);
Johannes Weiner0bef42e2009-03-04 16:21:29 +0100155 bootmap_size = init_bootmem_node(NODE_DATA(0),
Chris Zankel3f65ce42005-06-23 22:01:24 -0700156 bootmap_start >> PAGE_SHIFT,
Johannes Weiner0bef42e2009-03-04 16:21:29 +0100157 min_low_pfn,
Chris Zankel3f65ce42005-06-23 22:01:24 -0700158 max_low_pfn);
159
160 /* Add all remaining memory pieces into the bootmem map */
161
Max Filippove9d6dca2014-01-31 05:38:21 +0400162 for (i = 0; i < sysmem.nr_banks; i++) {
163 if (sysmem.bank[i].start >> PAGE_SHIFT < max_low_pfn) {
164 unsigned long end = min(max_low_pfn << PAGE_SHIFT,
165 sysmem.bank[i].end);
166 free_bootmem(sysmem.bank[i].start,
167 end - sysmem.bank[i].start);
168 }
169 }
Chris Zankel3f65ce42005-06-23 22:01:24 -0700170
171}
172
173
Johannes Weinere5083a62009-03-04 16:21:31 +0100174void __init zones_init(void)
Chris Zankel3f65ce42005-06-23 22:01:24 -0700175{
176 unsigned long zones_size[MAX_NR_ZONES];
177 int i;
178
179 /* All pages are DMA-able, so we put them all in the DMA zone. */
180
Johannes Weinerc947a582009-03-04 16:21:30 +0100181 zones_size[ZONE_DMA] = max_low_pfn - ARCH_PFN_OFFSET;
Chris Zankel3f65ce42005-06-23 22:01:24 -0700182 for (i = 1; i < MAX_NR_ZONES; i++)
183 zones_size[i] = 0;
184
185#ifdef CONFIG_HIGHMEM
186 zones_size[ZONE_HIGHMEM] = max_pfn - max_low_pfn;
187#endif
188
Johannes Weinerc947a582009-03-04 16:21:30 +0100189 free_area_init_node(0, zones_size, ARCH_PFN_OFFSET, NULL);
Chris Zankel3f65ce42005-06-23 22:01:24 -0700190}
191
192/*
Chris Zankel3f65ce42005-06-23 22:01:24 -0700193 * Initialize memory pages.
194 */
195
196void __init mem_init(void)
197{
Jiang Liu808c2c32013-07-03 15:04:20 -0700198 max_mapnr = max_low_pfn - ARCH_PFN_OFFSET;
Johannes Weinerc947a582009-03-04 16:21:30 +0100199 high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
Chris Zankel3f65ce42005-06-23 22:01:24 -0700200
Chris Zankel288a60c2005-09-22 21:44:23 -0700201#ifdef CONFIG_HIGHMEM
Chris Zankel3f65ce42005-06-23 22:01:24 -0700202#error HIGHGMEM not implemented in init.c
203#endif
204
Jiang Liu0c988532013-07-03 15:03:24 -0700205 free_all_bootmem();
Chris Zankel3f65ce42005-06-23 22:01:24 -0700206
Jiang Liu808c2c32013-07-03 15:04:20 -0700207 mem_init_print_info(NULL);
Chris Zankel3f65ce42005-06-23 22:01:24 -0700208}
209
Chris Zankel3f65ce42005-06-23 22:01:24 -0700210#ifdef CONFIG_BLK_DEV_INITRD
211extern int initrd_is_mapped;
212
213void free_initrd_mem(unsigned long start, unsigned long end)
214{
Jiang Liu7acb2c22013-04-29 15:06:55 -0700215 if (initrd_is_mapped)
Jiang Liudbe67df2013-07-03 15:02:51 -0700216 free_reserved_area((void *)start, (void *)end, -1, "initrd");
Chris Zankel3f65ce42005-06-23 22:01:24 -0700217}
218#endif
219
220void free_initmem(void)
221{
Jiang Liudbe67df2013-07-03 15:02:51 -0700222 free_initmem_default(-1);
Chris Zankel3f65ce42005-06-23 22:01:24 -0700223}