2 * arch/xtensa/mm/init.c
4 * Derived from MIPS, PPC.
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
10 * Copyright (C) 2001 - 2005 Tensilica Inc.
11 * Copyright (C) 2014 Cadence Design Systems Inc.
13 * Chris Zankel <chris@zankel.net>
14 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/bootmem.h>
22 #include <linux/gfp.h>
23 #include <linux/highmem.h>
24 #include <linux/swap.h>
25 #include <linux/mman.h>
26 #include <linux/nodemask.h>
29 #include <asm/bootparam.h>
31 #include <asm/sections.h>
32 #include <asm/sysmem.h>
34 struct sysmem_info sysmem __initdata
;
36 static void __init
sysmem_dump(void)
40 pr_debug("Sysmem:\n");
41 for (i
= 0; i
< sysmem
.nr_banks
; ++i
)
42 pr_debug(" 0x%08lx - 0x%08lx (%ldK)\n",
43 sysmem
.bank
[i
].start
, sysmem
.bank
[i
].end
,
44 (sysmem
.bank
[i
].end
- sysmem
.bank
[i
].start
) >> 10);
48 * Find bank with maximal .start such that bank.start <= start
50 static inline struct meminfo
* __init
find_bank(unsigned long start
)
53 struct meminfo
*it
= NULL
;
55 for (i
= 0; i
< sysmem
.nr_banks
; ++i
)
56 if (sysmem
.bank
[i
].start
<= start
)
64 * Move all memory banks starting at 'from' to a new place at 'to',
65 * adjust nr_banks accordingly.
66 * Both 'from' and 'to' must be inside the sysmem.bank.
68 * Returns: 0 (success), -ENOMEM (not enough space in the sysmem.bank).
70 static int __init
move_banks(struct meminfo
*to
, struct meminfo
*from
)
72 unsigned n
= sysmem
.nr_banks
- (from
- sysmem
.bank
);
74 if (to
> from
&& to
- from
+ sysmem
.nr_banks
> SYSMEM_BANKS_MAX
)
77 memmove(to
, from
, n
* sizeof(struct meminfo
));
78 sysmem
.nr_banks
+= to
- from
;
83 * Add new bank to sysmem. Resulting sysmem is the union of bytes of the
84 * original sysmem and the new bank.
86 * Returns: 0 (success), < 0 (error)
88 int __init
add_sysmem_bank(unsigned long start
, unsigned long end
)
91 struct meminfo
*it
= NULL
;
93 unsigned long bank_sz
= 0;
96 (start
< end
) != (PAGE_ALIGN(start
) < (end
& PAGE_MASK
))) {
97 pr_warn("Ignoring small memory bank 0x%08lx size: %ld bytes\n",
102 start
= PAGE_ALIGN(start
);
106 it
= find_bank(start
);
109 bank_sz
= it
->end
- it
->start
;
111 if (it
&& bank_sz
>= start
- it
->start
) {
112 if (end
- it
->start
> bank_sz
)
122 if (it
- sysmem
.bank
< sysmem
.nr_banks
&&
123 it
->start
- start
<= sz
) {
125 if (it
->end
- it
->start
< sz
)
130 if (move_banks(it
+ 1, it
) < 0) {
131 pr_warn("Ignoring memory bank 0x%08lx size %ld bytes\n",
140 sz
= it
->end
- it
->start
;
141 for (i
= it
+ 1 - sysmem
.bank
; i
< sysmem
.nr_banks
; ++i
)
142 if (sysmem
.bank
[i
].start
- it
->start
<= sz
) {
143 if (sz
< sysmem
.bank
[i
].end
- it
->start
)
144 it
->end
= sysmem
.bank
[i
].end
;
149 move_banks(it
+ 1, sysmem
.bank
+ i
);
154 * mem_reserve(start, end, must_exist)
156 * Reserve some memory from the memory pool.
157 * If must_exist is set and a part of the region being reserved does not exist
158 * memory map is not altered.
161 * start Start of region,
163 * must_exist Must exist in memory pool.
170 int __init
mem_reserve(unsigned long start
, unsigned long end
, int must_exist
)
173 struct meminfo
*rm
= NULL
;
175 unsigned long bank_sz
= 0;
177 start
= start
& PAGE_MASK
;
178 end
= PAGE_ALIGN(end
);
183 it
= find_bank(start
);
186 bank_sz
= it
->end
- it
->start
;
188 if ((!it
|| end
- it
->start
> bank_sz
) && must_exist
) {
189 pr_warn("mem_reserve: [0x%0lx, 0x%0lx) not in any region!\n",
194 if (it
&& start
- it
->start
< bank_sz
) {
195 if (start
== it
->start
) {
196 if (end
- it
->start
< bank_sz
) {
204 if (end
- it
->start
< bank_sz
)
205 return add_sysmem_bank(end
,
206 it
->start
+ bank_sz
);
214 for (; it
< sysmem
.bank
+ sysmem
.nr_banks
; ++it
) {
215 if (it
->end
- start
<= sz
) {
219 if (it
->start
- start
< sz
)
233 * Initialize the bootmem system and give it all low memory we have available.
236 void __init
bootmem_init(void)
239 unsigned long bootmap_start
, bootmap_size
;
243 max_low_pfn
= max_pfn
= 0;
246 for (i
=0; i
< sysmem
.nr_banks
; i
++) {
247 pfn
= PAGE_ALIGN(sysmem
.bank
[i
].start
) >> PAGE_SHIFT
;
248 if (pfn
< min_low_pfn
)
250 pfn
= PAGE_ALIGN(sysmem
.bank
[i
].end
- 1) >> PAGE_SHIFT
;
255 if (min_low_pfn
> max_pfn
)
256 panic("No memory found!\n");
258 max_low_pfn
= max_pfn
< MAX_MEM_PFN
>> PAGE_SHIFT
?
259 max_pfn
: MAX_MEM_PFN
>> PAGE_SHIFT
;
261 /* Find an area to use for the bootmem bitmap. */
263 bootmap_size
= bootmem_bootmap_pages(max_low_pfn
- min_low_pfn
);
264 bootmap_size
<<= PAGE_SHIFT
;
267 for (i
=0; i
<sysmem
.nr_banks
; i
++)
268 if (sysmem
.bank
[i
].end
- sysmem
.bank
[i
].start
>= bootmap_size
) {
269 bootmap_start
= sysmem
.bank
[i
].start
;
273 if (bootmap_start
== ~0UL)
274 panic("Cannot find %ld bytes for bootmap\n", bootmap_size
);
276 /* Reserve the bootmem bitmap area */
278 mem_reserve(bootmap_start
, bootmap_start
+ bootmap_size
, 1);
279 bootmap_size
= init_bootmem_node(NODE_DATA(0),
280 bootmap_start
>> PAGE_SHIFT
,
284 /* Add all remaining memory pieces into the bootmem map */
286 for (i
= 0; i
< sysmem
.nr_banks
; i
++) {
287 if (sysmem
.bank
[i
].start
>> PAGE_SHIFT
< max_low_pfn
) {
288 unsigned long end
= min(max_low_pfn
<< PAGE_SHIFT
,
290 free_bootmem(sysmem
.bank
[i
].start
,
291 end
- sysmem
.bank
[i
].start
);
298 void __init
zones_init(void)
300 /* All pages are DMA-able, so we put them all in the DMA zone. */
301 unsigned long zones_size
[MAX_NR_ZONES
] = {
302 [ZONE_DMA
] = max_low_pfn
- ARCH_PFN_OFFSET
,
303 #ifdef CONFIG_HIGHMEM
304 [ZONE_HIGHMEM
] = max_pfn
- max_low_pfn
,
307 free_area_init_node(0, zones_size
, ARCH_PFN_OFFSET
, NULL
);
311 * Initialize memory pages.
314 void __init
mem_init(void)
316 #ifdef CONFIG_HIGHMEM
319 reset_all_zones_managed_pages();
320 for (tmp
= max_low_pfn
; tmp
< max_pfn
; tmp
++)
321 free_highmem_page(pfn_to_page(tmp
));
324 max_mapnr
= max_pfn
- ARCH_PFN_OFFSET
;
325 high_memory
= (void *)__va(max_low_pfn
<< PAGE_SHIFT
);
329 mem_init_print_info(NULL
);
330 pr_info("virtual kernel memory layout:\n"
331 #ifdef CONFIG_HIGHMEM
332 " pkmap : 0x%08lx - 0x%08lx (%5lu kB)\n"
333 " fixmap : 0x%08lx - 0x%08lx (%5lu kB)\n"
335 " vmalloc : 0x%08x - 0x%08x (%5u MB)\n"
336 " lowmem : 0x%08x - 0x%08lx (%5lu MB)\n",
337 #ifdef CONFIG_HIGHMEM
338 PKMAP_BASE
, PKMAP_BASE
+ LAST_PKMAP
* PAGE_SIZE
,
339 (LAST_PKMAP
*PAGE_SIZE
) >> 10,
340 FIXADDR_START
, FIXADDR_TOP
,
341 (FIXADDR_TOP
- FIXADDR_START
) >> 10,
343 VMALLOC_START
, VMALLOC_END
,
344 (VMALLOC_END
- VMALLOC_START
) >> 20,
345 PAGE_OFFSET
, PAGE_OFFSET
+
346 (max_low_pfn
- min_low_pfn
) * PAGE_SIZE
,
347 ((max_low_pfn
- min_low_pfn
) * PAGE_SIZE
) >> 20);
350 #ifdef CONFIG_BLK_DEV_INITRD
351 extern int initrd_is_mapped
;
353 void free_initrd_mem(unsigned long start
, unsigned long end
)
355 if (initrd_is_mapped
)
356 free_reserved_area((void *)start
, (void *)end
, -1, "initrd");
360 void free_initmem(void)
362 free_initmem_default(-1);
365 static void __init
parse_memmap_one(char *p
)
368 unsigned long start_at
, mem_size
;
374 mem_size
= memparse(p
, &p
);
380 start_at
= memparse(p
+ 1, &p
);
381 add_sysmem_bank(start_at
, start_at
+ mem_size
);
385 start_at
= memparse(p
+ 1, &p
);
386 mem_reserve(start_at
, start_at
+ mem_size
, 0);
390 mem_reserve(mem_size
, 0, 0);
394 pr_warn("Unrecognized memmap syntax: %s\n", p
);
399 static int __init
parse_memmap_opt(char *str
)
402 char *k
= strchr(str
, ',');
407 parse_memmap_one(str
);
413 early_param("memmap", parse_memmap_opt
);