blob: 6dce086a2edb5aa800d6512f745db00305159bd2 [file] [log] [blame]
Adrian Bunk88278ca2008-05-19 16:53:02 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * linux/arch/sparc/mm/init.c
3 *
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1995 Eddie C. Dost (ecd@skynet.be)
6 * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 * Copyright (C) 2000 Anton Blanchard (anton@samba.org)
8 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/signal.h>
12#include <linux/sched.h>
13#include <linux/kernel.h>
14#include <linux/errno.h>
15#include <linux/string.h>
16#include <linux/types.h>
17#include <linux/ptrace.h>
18#include <linux/mman.h>
19#include <linux/mm.h>
20#include <linux/swap.h>
21#include <linux/initrd.h>
22#include <linux/init.h>
23#include <linux/highmem.h>
24#include <linux/bootmem.h>
Robert Reiff538f3d2008-07-14 22:57:29 -070025#include <linux/pagemap.h>
Robert Reif1aa03652008-09-03 16:29:42 -070026#include <linux/poison.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Sam Ravnborg86ed40b2008-12-27 00:35:12 -080029#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/page.h>
31#include <asm/pgtable.h>
32#include <asm/vaddrs.h>
33#include <asm/pgalloc.h> /* bug in asm-generic/tlb.h: check_pgt_cache */
34#include <asm/tlb.h>
David S. Miller942a6bd2006-06-23 15:53:31 -070035#include <asm/prom.h>
Konrad Eisele0fd7ef12009-08-17 00:13:31 +000036#include <asm/leon.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038unsigned long *sparc_valid_addr_bitmap;
Sam Ravnborg6943f3d2009-01-08 16:58:05 -080039EXPORT_SYMBOL(sparc_valid_addr_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41unsigned long phys_base;
Sam Ravnborg6943f3d2009-01-08 16:58:05 -080042EXPORT_SYMBOL(phys_base);
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044unsigned long pfn_base;
Sam Ravnborg6943f3d2009-01-08 16:58:05 -080045EXPORT_SYMBOL(pfn_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49struct pgtable_cache_struct pgt_quicklists;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/* Initial ramdisk setup */
52extern unsigned int sparc_ramdisk_image;
53extern unsigned int sparc_ramdisk_size;
54
55unsigned long highstart_pfn, highend_pfn;
56
57pte_t *kmap_pte;
58pgprot_t kmap_prot;
59
60#define kmap_get_fixmap_pte(vaddr) \
61 pte_offset_kernel(pmd_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr))
62
63void __init kmap_init(void)
64{
65 /* cache the first kmap pte */
66 kmap_pte = kmap_get_fixmap_pte(__fix_to_virt(FIX_KMAP_BEGIN));
67 kmap_prot = __pgprot(SRMMU_ET_PTE | SRMMU_PRIV | SRMMU_CACHE);
68}
69
David Rientjesb2b755b2011-03-24 15:18:15 -070070void show_mem(unsigned int filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 printk("Mem-info:\n");
David Rientjes7bf02ea2011-05-24 17:11:16 -070073 show_free_areas(filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 printk("Free swap: %6ldkB\n",
75 nr_swap_pages << (PAGE_SHIFT-10));
76 printk("%ld pages of RAM\n", totalram_pages);
Al Virof0f0d0c2007-03-14 09:18:50 +000077 printk("%ld free pages\n", nr_free_pages());
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#if 0 /* undefined pgtable_cache_size, pgd_cache_size */
79 printk("%ld pages in page table cache\n",pgtable_cache_size);
80#ifndef CONFIG_SMP
81 if (sparc_cpu_model == sun4m || sparc_cpu_model == sun4d)
82 printk("%ld entries in page dir cache\n",pgd_cache_size);
83#endif
84#endif
85}
86
87void __init sparc_context_init(int numctx)
88{
89 int ctx;
90
91 ctx_list_pool = __alloc_bootmem(numctx * sizeof(struct ctx_list), SMP_CACHE_BYTES, 0UL);
92
93 for(ctx = 0; ctx < numctx; ctx++) {
94 struct ctx_list *clist;
95
96 clist = (ctx_list_pool + ctx);
97 clist->ctx_number = ctx;
98 clist->ctx_mm = NULL;
99 }
100 ctx_free.next = ctx_free.prev = &ctx_free;
101 ctx_used.next = ctx_used.prev = &ctx_used;
102 for(ctx = 0; ctx < numctx; ctx++)
103 add_to_free_ctxlist(ctx_list_pool + ctx);
104}
105
106extern unsigned long cmdline_memory_size;
107unsigned long last_valid_pfn;
108
109unsigned long calc_highpages(void)
110{
111 int i;
112 int nr = 0;
113
114 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
115 unsigned long start_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
116 unsigned long end_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
117
118 if (end_pfn <= max_low_pfn)
119 continue;
120
121 if (start_pfn < max_low_pfn)
122 start_pfn = max_low_pfn;
123
124 nr += end_pfn - start_pfn;
125 }
126
127 return nr;
128}
129
Adrian Bunk50215d62008-06-05 11:41:51 -0700130static unsigned long calc_max_low_pfn(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 int i;
133 unsigned long tmp = pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT);
134 unsigned long curr_pfn, last_pfn;
135
136 last_pfn = (sp_banks[0].base_addr + sp_banks[0].num_bytes) >> PAGE_SHIFT;
137 for (i = 1; sp_banks[i].num_bytes != 0; i++) {
138 curr_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
139
140 if (curr_pfn >= tmp) {
141 if (last_pfn < tmp)
142 tmp = last_pfn;
143 break;
144 }
145
146 last_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
147 }
148
149 return tmp;
150}
151
152unsigned long __init bootmem_init(unsigned long *pages_avail)
153{
154 unsigned long bootmap_size, start_pfn;
155 unsigned long end_of_phys_memory = 0UL;
156 unsigned long bootmap_pfn, bytes_avail, size;
157 int i;
158
159 bytes_avail = 0UL;
160 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
161 end_of_phys_memory = sp_banks[i].base_addr +
162 sp_banks[i].num_bytes;
163 bytes_avail += sp_banks[i].num_bytes;
164 if (cmdline_memory_size) {
165 if (bytes_avail > cmdline_memory_size) {
166 unsigned long slack = bytes_avail - cmdline_memory_size;
167
168 bytes_avail -= slack;
169 end_of_phys_memory -= slack;
170
171 sp_banks[i].num_bytes -= slack;
172 if (sp_banks[i].num_bytes == 0) {
173 sp_banks[i].base_addr = 0xdeadbeef;
174 } else {
175 sp_banks[i+1].num_bytes = 0;
176 sp_banks[i+1].base_addr = 0xdeadbeef;
177 }
178 break;
179 }
180 }
181 }
182
183 /* Start with page aligned address of last symbol in kernel
184 * image.
185 */
186 start_pfn = (unsigned long)__pa(PAGE_ALIGN((unsigned long) &_end));
187
188 /* Now shift down to get the real physical page frame number. */
189 start_pfn >>= PAGE_SHIFT;
190
191 bootmap_pfn = start_pfn;
192
193 max_pfn = end_of_phys_memory >> PAGE_SHIFT;
194
195 max_low_pfn = max_pfn;
196 highstart_pfn = highend_pfn = max_pfn;
197
198 if (max_low_pfn > pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT)) {
199 highstart_pfn = pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT);
200 max_low_pfn = calc_max_low_pfn();
201 printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
202 calc_highpages() >> (20 - PAGE_SHIFT));
203 }
204
205#ifdef CONFIG_BLK_DEV_INITRD
206 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
207 if (sparc_ramdisk_image) {
David S. Millerefb89632007-08-20 15:52:18 -0700208 if (sparc_ramdisk_image >= (unsigned long)&_end - 2 * PAGE_SIZE)
209 sparc_ramdisk_image -= KERNBASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 initrd_start = sparc_ramdisk_image + phys_base;
211 initrd_end = initrd_start + sparc_ramdisk_size;
212 if (initrd_end > end_of_phys_memory) {
213 printk(KERN_CRIT "initrd extends beyond end of memory "
214 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
215 initrd_end, end_of_phys_memory);
216 initrd_start = 0;
217 }
218 if (initrd_start) {
219 if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
220 initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
221 bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
222 }
223 }
224#endif
225 /* Initialize the boot-time allocator. */
226 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, pfn_base,
227 max_low_pfn);
228
229 /* Now register the available physical memory with the
230 * allocator.
231 */
232 *pages_avail = 0;
233 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
234 unsigned long curr_pfn, last_pfn;
235
236 curr_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
237 if (curr_pfn >= max_low_pfn)
238 break;
239
240 last_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
241 if (last_pfn > max_low_pfn)
242 last_pfn = max_low_pfn;
243
244 /*
245 * .. finally, did all the rounding and playing
246 * around just make the area go away?
247 */
248 if (last_pfn <= curr_pfn)
249 continue;
250
251 size = (last_pfn - curr_pfn) << PAGE_SHIFT;
252 *pages_avail += last_pfn - curr_pfn;
253
254 free_bootmem(sp_banks[i].base_addr, size);
255 }
256
257#ifdef CONFIG_BLK_DEV_INITRD
258 if (initrd_start) {
259 /* Reserve the initrd image area. */
260 size = initrd_end - initrd_start;
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800261 reserve_bootmem(initrd_start, size, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
263
264 initrd_start = (initrd_start - phys_base) + PAGE_OFFSET;
265 initrd_end = (initrd_end - phys_base) + PAGE_OFFSET;
266 }
267#endif
268 /* Reserve the kernel text/data/bss. */
269 size = (start_pfn << PAGE_SHIFT) - phys_base;
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800270 reserve_bootmem(phys_base, size, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
272
273 /* Reserve the bootmem map. We do not account for it
274 * in pages_avail because we will release that memory
275 * in free_all_bootmem.
276 */
277 size = bootmap_size;
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800278 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
280
281 return max_pfn;
282}
283
284/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * paging_init() sets up the page tables: We call the MMU specific
286 * init routine based upon the Sun model type on the Sparc.
287 *
288 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289extern void srmmu_paging_init(void);
290extern void device_scan(void);
291
292void __init paging_init(void)
293{
Sam Ravnborgcc52aea2012-07-26 11:02:22 +0000294 srmmu_paging_init();
David S. Miller942a6bd2006-06-23 15:53:31 -0700295 prom_build_devicetree();
David S. Miller890db402009-04-01 03:13:15 -0700296 of_fill_in_cpu_data();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 device_scan();
298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300static void __init taint_real_pages(void)
301{
302 int i;
303
304 for (i = 0; sp_banks[i].num_bytes; i++) {
305 unsigned long start, end;
306
307 start = sp_banks[i].base_addr;
308 end = start + sp_banks[i].num_bytes;
309
310 while (start < end) {
311 set_bit(start >> 20, sparc_valid_addr_bitmap);
312 start += PAGE_SIZE;
313 }
314 }
315}
316
Adrian Bunk50215d62008-06-05 11:41:51 -0700317static void map_high_region(unsigned long start_pfn, unsigned long end_pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 unsigned long tmp;
320
321#ifdef CONFIG_DEBUG_HIGHMEM
322 printk("mapping high region %08lx - %08lx\n", start_pfn, end_pfn);
323#endif
324
325 for (tmp = start_pfn; tmp < end_pfn; tmp++) {
326 struct page *page = pfn_to_page(tmp);
327
328 ClearPageReserved(page);
Nick Piggin7835e982006-03-22 00:08:40 -0800329 init_page_count(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 __free_page(page);
331 totalhigh_pages++;
332 }
333}
334
335void __init mem_init(void)
336{
337 int codepages = 0;
338 int datapages = 0;
339 int initpages = 0;
340 int reservedpages = 0;
341 int i;
342
343 if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
344 prom_printf("BUG: fixmap and pkmap areas overlap\n");
345 prom_printf("pkbase: 0x%lx pkend: 0x%lx fixstart 0x%lx\n",
346 PKMAP_BASE,
347 (unsigned long)PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
348 FIXADDR_START);
349 prom_printf("Please mail sparclinux@vger.kernel.org.\n");
350 prom_halt();
351 }
352
353
354 /* Saves us work later. */
355 memset((void *)&empty_zero_page, 0, PAGE_SIZE);
356
357 i = last_valid_pfn >> ((20 - PAGE_SHIFT) + 5);
358 i += 1;
359 sparc_valid_addr_bitmap = (unsigned long *)
360 __alloc_bootmem(i << 2, SMP_CACHE_BYTES, 0UL);
361
362 if (sparc_valid_addr_bitmap == NULL) {
363 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
364 prom_halt();
365 }
366 memset(sparc_valid_addr_bitmap, 0, i << 2);
367
368 taint_real_pages();
369
370 max_mapnr = last_valid_pfn - pfn_base;
371 high_memory = __va(max_low_pfn << PAGE_SHIFT);
372
373 totalram_pages = free_all_bootmem();
374
375 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
376 unsigned long start_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
377 unsigned long end_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
378
379 num_physpages += sp_banks[i].num_bytes >> PAGE_SHIFT;
380
381 if (end_pfn <= highstart_pfn)
382 continue;
383
384 if (start_pfn < highstart_pfn)
385 start_pfn = highstart_pfn;
386
387 map_high_region(start_pfn, end_pfn);
388 }
389
390 totalram_pages += totalhigh_pages;
391
Sam Ravnborg86ed40b2008-12-27 00:35:12 -0800392 codepages = (((unsigned long) &_etext) - ((unsigned long)&_start));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
Sam Ravnborg86ed40b2008-12-27 00:35:12 -0800394 datapages = (((unsigned long) &_edata) - ((unsigned long)&_etext));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
396 initpages = (((unsigned long) &__init_end) - ((unsigned long) &__init_begin));
397 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
398
399 /* Ignore memory holes for the purpose of counting reserved pages */
400 for (i=0; i < max_low_pfn; i++)
401 if (test_bit(i >> (20 - PAGE_SHIFT), sparc_valid_addr_bitmap)
402 && PageReserved(pfn_to_page(i)))
403 reservedpages++;
404
405 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init, %ldk highmem)\n",
Geert Uytterhoevencc013a82009-09-21 17:02:36 -0700406 nr_free_pages() << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 num_physpages << (PAGE_SHIFT - 10),
408 codepages << (PAGE_SHIFT-10),
409 reservedpages << (PAGE_SHIFT - 10),
410 datapages << (PAGE_SHIFT-10),
411 initpages << (PAGE_SHIFT-10),
412 totalhigh_pages << (PAGE_SHIFT-10));
413}
414
415void free_initmem (void)
416{
417 unsigned long addr;
Sam Ravnborg86ed40b2008-12-27 00:35:12 -0800418 unsigned long freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 addr = (unsigned long)(&__init_begin);
Sam Ravnborg86ed40b2008-12-27 00:35:12 -0800421 freed = (unsigned long)(&__init_end) - addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
423 struct page *p;
424
Robert Reif1aa03652008-09-03 16:29:42 -0700425 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 p = virt_to_page(addr);
427
428 ClearPageReserved(p);
Nick Piggin7835e982006-03-22 00:08:40 -0800429 init_page_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 __free_page(p);
431 totalram_pages++;
432 num_physpages++;
433 }
Sam Ravnborg86ed40b2008-12-27 00:35:12 -0800434 printk(KERN_INFO "Freeing unused kernel memory: %ldk freed\n",
435 freed >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
438#ifdef CONFIG_BLK_DEV_INITRD
439void free_initrd_mem(unsigned long start, unsigned long end)
440{
441 if (start < end)
Robert Reif1aa03652008-09-03 16:29:42 -0700442 printk(KERN_INFO "Freeing initrd memory: %ldk freed\n",
443 (end - start) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 for (; start < end; start += PAGE_SIZE) {
Robert Reif1aa03652008-09-03 16:29:42 -0700445 struct page *p;
446
447 memset((void *)start, POISON_FREE_INITMEM, PAGE_SIZE);
448 p = virt_to_page(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 ClearPageReserved(p);
Nick Piggin7835e982006-03-22 00:08:40 -0800451 init_page_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 __free_page(p);
Robert Reif1aa03652008-09-03 16:29:42 -0700453 totalram_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 num_physpages++;
455 }
456}
457#endif
458
459void sparc_flush_page_to_ram(struct page *page)
460{
461 unsigned long vaddr = (unsigned long)page_address(page);
462
463 if (vaddr)
464 __flush_page_to_ram(vaddr);
465}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800466EXPORT_SYMBOL(sparc_flush_page_to_ram);