blob: fbe5f0dbae06eb588d3d6be5106e5ffeeaebdf9b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* init.c: memory initialisation for FRV
2 *
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 * Derived from:
12 * - linux/arch/m68knommu/mm/init.c
13 * - Copyright (C) 1998 D. Jeff Dionne <jeff@lineo.ca>, Kenneth Albanowski <kjahds@kjahds.com>,
14 * - Copyright (C) 2000 Lineo, Inc. (www.lineo.com)
15 * - linux/arch/m68k/mm/init.c
16 * - Copyright (C) 1995 Hamish Macdonald
17 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/signal.h>
20#include <linux/sched.h>
21#include <linux/pagemap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/swap.h>
24#include <linux/mm.h>
25#include <linux/kernel.h>
26#include <linux/string.h>
27#include <linux/types.h>
28#include <linux/bootmem.h>
29#include <linux/highmem.h>
Adrian Bunkfb56f0f2008-05-23 13:05:00 -070030#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/setup.h>
33#include <asm/segment.h>
34#include <asm/page.h>
35#include <asm/pgtable.h>
36#include <asm/system.h>
37#include <asm/mmu_context.h>
38#include <asm/virtconvert.h>
39#include <asm/sections.h>
40#include <asm/tlb.h>
41
42#undef DEBUG
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * BAD_PAGE is the page that is used for page faults when linux
46 * is out-of-memory. Older versions of linux just did a
47 * do_exit(), but using this instead means there is less risk
48 * for a process dying in kernel mode, possibly leaving a inode
49 * unused etc..
50 *
51 * BAD_PAGETABLE is the accompanying page-table: it is initialized
52 * to point to BAD_PAGE entries.
53 *
54 * ZERO_PAGE is a special page that is used for zero-initialized
55 * data and COW.
56 */
57static unsigned long empty_bad_page_table;
58static unsigned long empty_bad_page;
Adrian Bunkfb56f0f2008-05-23 13:05:00 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060unsigned long empty_zero_page;
Adrian Bunkfb56f0f2008-05-23 13:05:00 -070061EXPORT_SYMBOL(empty_zero_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/*****************************************************************************/
64/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * paging_init() continues the virtual memory environment setup which
66 * was begun by the code in arch/head.S.
67 * The parameters are pointers to where to stick the starting and ending
68 * addresses of available kernel virtual memory.
69 */
70void __init paging_init(void)
71{
Christoph Lameterf06a9682006-09-25 23:31:10 -070072 unsigned long zones_size[MAX_NR_ZONES] = {0, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 /* allocate some pages for kernel housekeeping tasks */
75 empty_bad_page_table = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
76 empty_bad_page = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
77 empty_zero_page = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
78
79 memset((void *) empty_zero_page, 0, PAGE_SIZE);
80
David Howells8080f232005-11-28 13:43:51 -080081#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (num_physpages - num_mappedpages) {
83 pgd_t *pge;
84 pud_t *pue;
85 pmd_t *pme;
86
87 pkmap_page_table = alloc_bootmem_pages(PAGE_SIZE);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 pge = swapper_pg_dir + pgd_index_k(PKMAP_BASE);
90 pue = pud_offset(pge, PKMAP_BASE);
91 pme = pmd_offset(pue, PKMAP_BASE);
92 __set_pmd(pme, virt_to_phys(pkmap_page_table) | _PAGE_TABLE);
93 }
94#endif
95
96 /* distribute the allocatable pages across the various zones and pass them to the allocator
97 */
David Howellsfa642752007-02-28 20:11:58 -080098 zones_size[ZONE_NORMAL] = max_low_pfn - min_low_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#ifdef CONFIG_HIGHMEM
100 zones_size[ZONE_HIGHMEM] = num_physpages - num_mappedpages;
101#endif
102
103 free_area_init(zones_size);
104
105#ifdef CONFIG_MMU
106 /* initialise init's MMU context */
107 init_new_context(&init_task, &init_mm);
108#endif
109
110} /* end paging_init() */
111
112/*****************************************************************************/
113/*
114 *
115 */
116void __init mem_init(void)
117{
118 unsigned long npages = (memory_end - memory_start) >> PAGE_SHIFT;
119 unsigned long tmp;
120#ifdef CONFIG_MMU
121 unsigned long loop, pfn;
122 int datapages = 0;
123#endif
124 int codek = 0, datak = 0;
125
126 /* this will put all memory onto the freelists */
127 totalram_pages = free_all_bootmem();
128
129#ifdef CONFIG_MMU
130 for (loop = 0 ; loop < npages ; loop++)
131 if (PageReserved(&mem_map[loop]))
132 datapages++;
133
134#ifdef CONFIG_HIGHMEM
135 for (pfn = num_physpages - 1; pfn >= num_mappedpages; pfn--) {
136 struct page *page = &mem_map[pfn];
137
138 ClearPageReserved(page);
Nick Piggin7835e982006-03-22 00:08:40 -0800139 init_page_count(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 __free_page(page);
141 totalram_pages++;
142 }
143#endif
144
145 codek = ((unsigned long) &_etext - (unsigned long) &_stext) >> 10;
146 datak = datapages << (PAGE_SHIFT - 10);
147
148#else
149 codek = (_etext - _stext) >> 10;
150 datak = 0; //(_ebss - _sdata) >> 10;
151#endif
152
153 tmp = nr_free_pages() << PAGE_SHIFT;
154 printk("Memory available: %luKiB/%luKiB RAM, %luKiB/%luKiB ROM (%dKiB kernel code, %dKiB data)\n",
155 tmp >> 10,
156 npages << (PAGE_SHIFT - 10),
157 (rom_length > 0) ? ((rom_length >> 10) - codek) : 0,
158 rom_length >> 10,
159 codek,
160 datak
161 );
162
163} /* end mem_init() */
164
165/*****************************************************************************/
166/*
167 * free the memory that was only required for initialisation
168 */
David Howellse4fc5a12007-11-06 21:54:44 +0000169void free_initmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171#if defined(CONFIG_RAMKERNEL) && !defined(CONFIG_PROTECT_KERNEL)
172 unsigned long start, end, addr;
173
174 start = PAGE_ALIGN((unsigned long) &__init_begin); /* round up */
175 end = ((unsigned long) &__init_end) & PAGE_MASK; /* round down */
176
177 /* next to check that the page we free is not a partial page */
178 for (addr = start; addr < end; addr += PAGE_SIZE) {
179 ClearPageReserved(virt_to_page(addr));
Nick Piggin7835e982006-03-22 00:08:40 -0800180 init_page_count(virt_to_page(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 free_page(addr);
182 totalram_pages++;
183 }
184
185 printk("Freeing unused kernel memory: %ldKiB freed (0x%lx - 0x%lx)\n",
186 (end - start) >> 10, start, end);
187#endif
188} /* end free_initmem() */
189
190/*****************************************************************************/
191/*
192 * free the initial ramdisk memory
193 */
194#ifdef CONFIG_BLK_DEV_INITRD
195void __init free_initrd_mem(unsigned long start, unsigned long end)
196{
197 int pages = 0;
198 for (; start < end; start += PAGE_SIZE) {
199 ClearPageReserved(virt_to_page(start));
Nick Piggin7835e982006-03-22 00:08:40 -0800200 init_page_count(virt_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 free_page(start);
202 totalram_pages++;
203 pages++;
204 }
205 printk("Freeing initrd memory: %dKiB freed\n", (pages * PAGE_SIZE) >> 10);
206} /* end free_initrd_mem() */
207#endif