blob: 63da3478521fda8764b373ab38665d4c8f5cef2c [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>
13#ifdef CONFIG_BLOCK_DEV_RAM
14#include <linux/blk.h>
15#endif
16#include <linux/swap.h>
17#include <linux/module.h>
18#include <asm/page.h>
19#include <asm/pgalloc.h>
20#include <asm/sections.h>
21#include <asm/arcregs.h>
22
23pgd_t swapper_pg_dir[PTRS_PER_PGD] __aligned(PAGE_SIZE);
24char empty_zero_page[PAGE_SIZE] __aligned(PAGE_SIZE);
25EXPORT_SYMBOL(empty_zero_page);
26
27/* Default tot mem from .config */
28static unsigned long arc_mem_sz = CONFIG_ARC_PLAT_SDRAM_SIZE;
29
30/* User can over-ride above with "mem=nnn[KkMm]" in cmdline */
31static int __init setup_mem_sz(char *str)
32{
33 arc_mem_sz = memparse(str, NULL) & PAGE_MASK;
34
35 /* early console might not be setup yet - it will show up later */
36 pr_info("\"mem=%s\": mem sz set to %ldM\n", str, TO_MB(arc_mem_sz));
37
38 return 0;
39}
40early_param("mem", setup_mem_sz);
41
42/*
43 * First memory setup routine called from setup_arch()
44 * 1. setup swapper's mm @init_mm
45 * 2. Count the pages we have and setup bootmem allocator
46 * 3. zone setup
47 */
48void __init setup_arch_memory(void)
49{
50 unsigned long zones_size[MAX_NR_ZONES] = { 0, 0 };
51 unsigned long end_mem = CONFIG_LINUX_LINK_BASE + arc_mem_sz;
52
53 init_mm.start_code = (unsigned long)_text;
54 init_mm.end_code = (unsigned long)_etext;
55 init_mm.end_data = (unsigned long)_edata;
56 init_mm.brk = (unsigned long)_end;
57
58 /*
59 * We do it here, so that memory is correctly instantiated
60 * even if "mem=xxx" cmline over-ride is not given
61 */
62 memblock_add(CONFIG_LINUX_LINK_BASE, arc_mem_sz);
63
64 /*------------- externs in mm need setting up ---------------*/
65
66 /* first page of system - kernel .vector starts here */
67 min_low_pfn = PFN_DOWN(CONFIG_LINUX_LINK_BASE);
68
69 /* Last usable page of low mem (no HIGHMEM yet for ARC port) */
70 max_low_pfn = max_pfn = PFN_DOWN(end_mem);
71
72 max_mapnr = num_physpages = max_low_pfn - min_low_pfn;
73
74 /*------------- reserve kernel image -----------------------*/
75 memblock_reserve(CONFIG_LINUX_LINK_BASE,
76 __pa(_end) - CONFIG_LINUX_LINK_BASE);
77
78 memblock_dump_all();
79
80 /*-------------- node setup --------------------------------*/
81 memset(zones_size, 0, sizeof(zones_size));
82 zones_size[ZONE_NORMAL] = num_physpages;
83
84 /*
85 * We can't use the helper free_area_init(zones[]) because it uses
86 * PAGE_OFFSET to compute the @min_low_pfn which would be wrong
87 * when our kernel doesn't start at PAGE_OFFSET, i.e.
88 * PAGE_OFFSET != CONFIG_LINUX_LINK_BASE
89 */
90 free_area_init_node(0, /* node-id */
91 zones_size, /* num pages per zone */
92 min_low_pfn, /* first pfn of node */
93 NULL); /* NO holes */
94}
95
96/*
97 * mem_init - initializes memory
98 *
99 * Frees up bootmem
100 * Calculates and displays memory available/used
101 */
102void __init mem_init(void)
103{
104 int codesize, datasize, initsize, reserved_pages, free_pages;
105 int tmp;
106
107 high_memory = (void *)(CONFIG_LINUX_LINK_BASE + arc_mem_sz);
108
109 totalram_pages = free_all_bootmem();
110
111 /* count all reserved pages [kernel code/data/mem_map..] */
112 reserved_pages = 0;
113 for (tmp = 0; tmp < max_mapnr; tmp++)
114 if (PageReserved(mem_map + tmp))
115 reserved_pages++;
116
117 /* XXX: nr_free_pages() is equivalent */
118 free_pages = max_mapnr - reserved_pages;
119
120 /*
121 * For the purpose of display below, split the "reserve mem"
122 * kernel code/data is already shown explicitly,
123 * Show any other reservations (mem_map[ ] et al)
124 */
125 reserved_pages -= (((unsigned int)_end - CONFIG_LINUX_LINK_BASE) >>
126 PAGE_SHIFT);
127
128 codesize = _etext - _text;
129 datasize = _end - _etext;
130 initsize = __init_end - __init_begin;
131
132 pr_info("Memory Available: %dM / %ldM (%dK code, %dK data, %dK init, %dK reserv)\n",
133 PAGES_TO_MB(free_pages),
134 TO_MB(arc_mem_sz),
135 TO_KB(codesize), TO_KB(datasize), TO_KB(initsize),
136 PAGES_TO_KB(reserved_pages));
137}
138
139static void __init free_init_pages(const char *what, unsigned long begin,
140 unsigned long end)
141{
142 unsigned long addr;
143
144 pr_info("Freeing %s: %ldk [%lx] to [%lx]\n",
145 what, TO_KB(end - begin), begin, end);
146
147 /* need to check that the page we free is not a partial page */
148 for (addr = begin; addr + PAGE_SIZE <= end; addr += PAGE_SIZE) {
149 ClearPageReserved(virt_to_page(addr));
150 init_page_count(virt_to_page(addr));
151 free_page(addr);
152 totalram_pages++;
153 }
154}
155
156/*
157 * free_initmem: Free all the __init memory.
158 */
159void __init_refok free_initmem(void)
160{
161 free_init_pages("unused kernel memory",
162 (unsigned long)__init_begin,
163 (unsigned long)__init_end);
164}
165
166#ifdef CONFIG_BLK_DEV_INITRD
167void __init free_initrd_mem(unsigned long start, unsigned long end)
168{
169 free_init_pages("initrd memory", start, end);
170}
171#endif