blob: a534d52a57bd1dabb2bb79cdb26119f709f840c2 [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>
21#include <linux/swap.h>
Chris Zankel66569202007-08-22 10:14:51 -070022#include <linux/mman.h>
23#include <linux/nodemask.h>
24#include <linux/mm.h>
25#include <linux/slab.h>
Chris Zankel3f65ce42005-06-23 22:01:24 -070026
27#include <asm/pgtable.h>
28#include <asm/bootparam.h>
29#include <asm/mmu_context.h>
30#include <asm/tlb.h>
Chris Zankel3f65ce42005-06-23 22:01:24 -070031#include <asm/page.h>
32#include <asm/pgalloc.h>
Chris Zankel3f65ce42005-06-23 22:01:24 -070033
34
Chris Zankel3f65ce42005-06-23 22:01:24 -070035DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
Chris Zankel3f65ce42005-06-23 22:01:24 -070036
37/* References to section boundaries */
38
39extern char _ftext, _etext, _fdata, _edata, _rodata_end;
40extern char __init_begin, __init_end;
41
42/*
43 * mem_reserve(start, end, must_exist)
44 *
45 * Reserve some memory from the memory pool.
46 *
47 * Parameters:
48 * start Start of region,
49 * end End of region,
50 * must_exist Must exist in memory pool.
51 *
52 * Returns:
53 * 0 (memory area couldn't be mapped)
54 * -1 (success)
55 */
56
57int __init mem_reserve(unsigned long start, unsigned long end, int must_exist)
58{
59 int i;
60
61 if (start == end)
62 return 0;
63
64 start = start & PAGE_MASK;
65 end = PAGE_ALIGN(end);
66
67 for (i = 0; i < sysmem.nr_banks; i++)
68 if (start < sysmem.bank[i].end
69 && end >= sysmem.bank[i].start)
70 break;
71
72 if (i == sysmem.nr_banks) {
73 if (must_exist)
74 printk (KERN_WARNING "mem_reserve: [0x%0lx, 0x%0lx) "
75 "not in any region!\n", start, end);
76 return 0;
77 }
78
79 if (start > sysmem.bank[i].start) {
80 if (end < sysmem.bank[i].end) {
81 /* split entry */
82 if (sysmem.nr_banks >= SYSMEM_BANKS_MAX)
83 panic("meminfo overflow\n");
84 sysmem.bank[sysmem.nr_banks].start = end;
85 sysmem.bank[sysmem.nr_banks].end = sysmem.bank[i].end;
86 sysmem.nr_banks++;
87 }
88 sysmem.bank[i].end = start;
89 } else {
90 if (end < sysmem.bank[i].end)
91 sysmem.bank[i].start = end;
92 else {
93 /* remove entry */
94 sysmem.nr_banks--;
95 sysmem.bank[i].start = sysmem.bank[sysmem.nr_banks].start;
96 sysmem.bank[i].end = sysmem.bank[sysmem.nr_banks].end;
97 }
98 }
99 return -1;
100}
101
102
103/*
104 * Initialize the bootmem system and give it all the memory we have available.
105 */
106
107void __init bootmem_init(void)
108{
109 unsigned long pfn;
110 unsigned long bootmap_start, bootmap_size;
111 int i;
112
113 max_low_pfn = max_pfn = 0;
114 min_low_pfn = ~0;
115
116 for (i=0; i < sysmem.nr_banks; i++) {
117 pfn = PAGE_ALIGN(sysmem.bank[i].start) >> PAGE_SHIFT;
118 if (pfn < min_low_pfn)
119 min_low_pfn = pfn;
120 pfn = PAGE_ALIGN(sysmem.bank[i].end - 1) >> PAGE_SHIFT;
121 if (pfn > max_pfn)
122 max_pfn = pfn;
123 }
124
125 if (min_low_pfn > max_pfn)
126 panic("No memory found!\n");
127
Chris Zankel173d6682006-12-10 02:18:48 -0800128 max_low_pfn = max_pfn < MAX_MEM_PFN >> PAGE_SHIFT ?
129 max_pfn : MAX_MEM_PFN >> PAGE_SHIFT;
Chris Zankel3f65ce42005-06-23 22:01:24 -0700130
131 /* Find an area to use for the bootmem bitmap. */
132
Johannes Weiner264da9f2009-03-04 16:21:29 +0100133 bootmap_size = bootmem_bootmap_pages(max_low_pfn - min_low_pfn);
134 bootmap_size <<= PAGE_SHIFT;
Chris Zankel3f65ce42005-06-23 22:01:24 -0700135 bootmap_start = ~0;
136
137 for (i=0; i<sysmem.nr_banks; i++)
138 if (sysmem.bank[i].end - sysmem.bank[i].start >= bootmap_size) {
139 bootmap_start = sysmem.bank[i].start;
140 break;
141 }
142
143 if (bootmap_start == ~0UL)
144 panic("Cannot find %ld bytes for bootmap\n", bootmap_size);
145
146 /* Reserve the bootmem bitmap area */
147
148 mem_reserve(bootmap_start, bootmap_start + bootmap_size, 1);
Johannes Weiner0bef42e2009-03-04 16:21:29 +0100149 bootmap_size = init_bootmem_node(NODE_DATA(0),
Chris Zankel3f65ce42005-06-23 22:01:24 -0700150 bootmap_start >> PAGE_SHIFT,
Johannes Weiner0bef42e2009-03-04 16:21:29 +0100151 min_low_pfn,
Chris Zankel3f65ce42005-06-23 22:01:24 -0700152 max_low_pfn);
153
154 /* Add all remaining memory pieces into the bootmem map */
155
156 for (i=0; i<sysmem.nr_banks; i++)
157 free_bootmem(sysmem.bank[i].start,
158 sysmem.bank[i].end - sysmem.bank[i].start);
159
160}
161
162
163void __init paging_init(void)
164{
165 unsigned long zones_size[MAX_NR_ZONES];
166 int i;
167
168 /* All pages are DMA-able, so we put them all in the DMA zone. */
169
170 zones_size[ZONE_DMA] = max_low_pfn;
171 for (i = 1; i < MAX_NR_ZONES; i++)
172 zones_size[i] = 0;
173
174#ifdef CONFIG_HIGHMEM
175 zones_size[ZONE_HIGHMEM] = max_pfn - max_low_pfn;
176#endif
177
178 /* Initialize the kernel's page tables. */
179
180 memset(swapper_pg_dir, 0, PAGE_SIZE);
181
182 free_area_init(zones_size);
183}
184
185/*
186 * Flush the mmu and reset associated register to default values.
187 */
188
189void __init init_mmu (void)
190{
191 /* Writing zeros to the <t>TLBCFG special registers ensure
192 * that valid values exist in the register. For existing
193 * PGSZID<w> fields, zero selects the first element of the
Chris Zankel4af410a2007-05-31 17:43:40 -0700194 * page-size array. For nonexistent PGSZID<w> fields, zero is
Chris Zankel3f65ce42005-06-23 22:01:24 -0700195 * the best value to write. Also, when changing PGSZID<w>
196 * fields, the corresponding TLB must be flushed.
197 */
198 set_itlbcfg_register (0);
199 set_dtlbcfg_register (0);
200 flush_tlb_all ();
201
202 /* Set rasid register to a known value. */
203
Chris Zankel173d6682006-12-10 02:18:48 -0800204 set_rasid_register (ASID_USER_FIRST);
Chris Zankel3f65ce42005-06-23 22:01:24 -0700205
206 /* Set PTEVADDR special register to the start of the page
207 * table, which is in kernel mappable space (ie. not
208 * statically mapped). This register's value is undefined on
209 * reset.
210 */
211 set_ptevaddr_register (PGTABLE_START);
212}
213
214/*
215 * Initialize memory pages.
216 */
217
218void __init mem_init(void)
219{
220 unsigned long codesize, reservedpages, datasize, initsize;
221 unsigned long highmemsize, tmp, ram;
222
223 max_mapnr = num_physpages = max_low_pfn;
224 high_memory = (void *) __va(max_mapnr << PAGE_SHIFT);
225 highmemsize = 0;
226
Chris Zankel288a60c2005-09-22 21:44:23 -0700227#ifdef CONFIG_HIGHMEM
Chris Zankel3f65ce42005-06-23 22:01:24 -0700228#error HIGHGMEM not implemented in init.c
229#endif
230
231 totalram_pages += free_all_bootmem();
232
233 reservedpages = ram = 0;
234 for (tmp = 0; tmp < max_low_pfn; tmp++) {
235 ram++;
236 if (PageReserved(mem_map+tmp))
237 reservedpages++;
238 }
239
240 codesize = (unsigned long) &_etext - (unsigned long) &_ftext;
241 datasize = (unsigned long) &_edata - (unsigned long) &_fdata;
242 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
243
244 printk("Memory: %luk/%luk available (%ldk kernel code, %ldk reserved, "
245 "%ldk data, %ldk init %ldk highmem)\n",
246 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
247 ram << (PAGE_SHIFT-10),
248 codesize >> 10,
249 reservedpages << (PAGE_SHIFT-10),
250 datasize >> 10,
251 initsize >> 10,
252 highmemsize >> 10);
253}
254
255void
256free_reserved_mem(void *start, void *end)
257{
258 for (; start < end; start += PAGE_SIZE) {
259 ClearPageReserved(virt_to_page(start));
Nick Piggin7835e982006-03-22 00:08:40 -0800260 init_page_count(virt_to_page(start));
Chris Zankel3f65ce42005-06-23 22:01:24 -0700261 free_page((unsigned long)start);
262 totalram_pages++;
263 }
264}
265
266#ifdef CONFIG_BLK_DEV_INITRD
267extern int initrd_is_mapped;
268
269void free_initrd_mem(unsigned long start, unsigned long end)
270{
271 if (initrd_is_mapped) {
272 free_reserved_mem((void*)start, (void*)end);
273 printk ("Freeing initrd memory: %ldk freed\n",(end-start)>>10);
274 }
275}
276#endif
277
278void free_initmem(void)
279{
280 free_reserved_mem(&__init_begin, &__init_end);
281 printk("Freeing unused kernel memory: %dk freed\n",
282 (&__init_end - &__init_begin) >> 10);
283}
284
Chris Zankel66569202007-08-22 10:14:51 -0700285struct kmem_cache *pgtable_cache __read_mostly;
Chris Zankel3f65ce42005-06-23 22:01:24 -0700286
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700287static void pgd_ctor(void* addr)
Chris Zankel3f65ce42005-06-23 22:01:24 -0700288{
Chris Zankel66569202007-08-22 10:14:51 -0700289 pte_t* ptep = (pte_t*)addr;
290 int i;
Chris Zankel3f65ce42005-06-23 22:01:24 -0700291
Chris Zankel66569202007-08-22 10:14:51 -0700292 for (i = 0; i < 1024; i++, ptep++)
293 pte_clear(NULL, 0, ptep);
Chris Zankel3f65ce42005-06-23 22:01:24 -0700294
Chris Zankel3f65ce42005-06-23 22:01:24 -0700295}
296
Chris Zankel66569202007-08-22 10:14:51 -0700297void __init pgtable_cache_init(void)
Chris Zankel3f65ce42005-06-23 22:01:24 -0700298{
Chris Zankel66569202007-08-22 10:14:51 -0700299 pgtable_cache = kmem_cache_create("pgd",
300 PAGE_SIZE, PAGE_SIZE,
301 SLAB_HWCACHE_ALIGN,
302 pgd_ctor);
Chris Zankel3f65ce42005-06-23 22:01:24 -0700303}