blob: ae957a9323754a2eb12c50a62e5c9cb436d304fb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: init.c,v 1.19 2004/02/21 04:42:16 kkojima Exp $
2 *
3 * linux/arch/sh/mm/init.c
4 *
5 * Copyright (C) 1999 Niibe Yutaka
6 * Copyright (C) 2002, 2004 Paul Mundt
7 *
8 * Based on linux/arch/i386/mm/init.c:
9 * Copyright (C) 1995 Linus Torvalds
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/signal.h>
13#include <linux/sched.h>
14#include <linux/kernel.h>
15#include <linux/errno.h>
16#include <linux/string.h>
17#include <linux/types.h>
18#include <linux/ptrace.h>
19#include <linux/mman.h>
20#include <linux/mm.h>
21#include <linux/swap.h>
22#include <linux/smp.h>
23#include <linux/init.h>
24#include <linux/highmem.h>
25#include <linux/bootmem.h>
26#include <linux/pagemap.h>
Paul Mundt2cb7ce32006-09-27 18:20:58 +090027#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/processor.h>
29#include <asm/system.h>
30#include <asm/uaccess.h>
31#include <asm/pgtable.h>
32#include <asm/pgalloc.h>
33#include <asm/mmu_context.h>
34#include <asm/io.h>
35#include <asm/tlb.h>
36#include <asm/cacheflush.h>
37#include <asm/cache.h>
38
39DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
40pgd_t swapper_pg_dir[PTRS_PER_PGD];
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#ifdef CONFIG_MMU
43/* It'd be good if these lines were in the standard header file. */
44#define START_PFN (NODE_DATA(0)->bdata->node_boot_start >> PAGE_SHIFT)
45#define MAX_LOW_PFN (NODE_DATA(0)->bdata->node_low_pfn)
46#endif
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048void (*copy_page)(void *from, void *to);
49void (*clear_page)(void *to);
50
51void show_mem(void)
52{
53 int i, total = 0, reserved = 0;
54 int shared = 0, cached = 0;
55
56 printk("Mem-info:\n");
57 show_free_areas();
58 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
59 i = max_mapnr;
60 while (i-- > 0) {
61 total++;
62 if (PageReserved(mem_map+i))
63 reserved++;
64 else if (PageSwapCache(mem_map+i))
65 cached++;
66 else if (page_count(mem_map+i))
67 shared += page_count(mem_map+i) - 1;
68 }
69 printk("%d pages of RAM\n",total);
70 printk("%d reserved pages\n",reserved);
71 printk("%d pages shared\n",shared);
72 printk("%d pages swap cached\n",cached);
73}
74
Yoshinori Sato11cbb702006-12-07 18:07:27 +090075#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
77{
78 pgd_t *pgd;
Paul Mundt26ff6c12006-09-27 15:13:36 +090079 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 pmd_t *pmd;
81 pte_t *pte;
82
Stuart Menefy99a596f2006-11-21 15:38:05 +090083 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (pgd_none(*pgd)) {
85 pgd_ERROR(*pgd);
86 return;
87 }
88
Stuart Menefy99a596f2006-11-21 15:38:05 +090089 pud = pud_alloc(NULL, pgd, addr);
90 if (unlikely(!pud)) {
91 pud_ERROR(*pud);
92 return;
Paul Mundt26ff6c12006-09-27 15:13:36 +090093 }
94
Stuart Menefy99a596f2006-11-21 15:38:05 +090095 pmd = pmd_alloc(NULL, pud, addr);
96 if (unlikely(!pmd)) {
97 pmd_ERROR(*pmd);
98 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100
101 pte = pte_offset_kernel(pmd, addr);
102 if (!pte_none(*pte)) {
103 pte_ERROR(*pte);
104 return;
105 }
106
107 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
108
Paul Mundtea9af692006-12-25 19:28:54 +0900109 flush_tlb_one(get_asid(), addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112/*
113 * As a performance optimization, other platforms preserve the fixmap mapping
114 * across a context switch, we don't presently do this, but this could be done
115 * in a similar fashion as to the wired TLB interface that sh64 uses (by way
116 * of the memorry mapped UTLB configuration) -- this unfortunately forces us to
117 * give up a TLB entry for each mapping we want to preserve. While this may be
118 * viable for a small number of fixmaps, it's not particularly useful for
119 * everything and needs to be carefully evaluated. (ie, we may want this for
120 * the vsyscall page).
121 *
122 * XXX: Perhaps add a _PAGE_WIRED flag or something similar that we can pass
123 * in at __set_fixmap() time to determine the appropriate behavior to follow.
124 *
125 * -- PFM.
126 */
127void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
128{
129 unsigned long address = __fix_to_virt(idx);
130
131 if (idx >= __end_of_fixed_addresses) {
132 BUG();
133 return;
134 }
135
136 set_pte_phys(address, phys, prot);
137}
Yoshinori Sato11cbb702006-12-07 18:07:27 +0900138#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/* References to section boundaries */
141
142extern char _text, _etext, _edata, __bss_start, _end;
143extern char __init_begin, __init_end;
144
145/*
146 * paging_init() sets up the page tables
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 */
148void __init paging_init(void)
149{
150 unsigned long zones_size[MAX_NR_ZONES] = { 0, };
151
152 /*
153 * Setup some defaults for the zone sizes.. these should be safe
154 * regardless of distcontiguous memory or MMU settings.
155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 zones_size[ZONE_NORMAL] = __MEMORY_SIZE >> PAGE_SHIFT;
157#ifdef CONFIG_HIGHMEM
158 zones_size[ZONE_HIGHMEM] = 0 >> PAGE_SHIFT;
159#endif
160
161#ifdef CONFIG_MMU
162 /*
163 * If we have an MMU, and want to be using it .. we need to adjust
164 * the zone sizes accordingly, in addition to turning it on.
165 */
166 {
Stuart Menefy6e4662f2006-11-21 13:53:44 +0900167 /* We don't need to map the kernel through the TLB, as
168 * it is permanatly mapped using P1. So clear the
169 * entire pgd. */
170 memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /* Turn on the MMU */
173 enable_mmu();
Christoph Lameter339ba9b2007-02-10 01:43:13 -0800174 zones_size[ZONE_NORMAL] = MAX_LOW_PFN - START_PFN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176
Stuart Menefy6e4662f2006-11-21 13:53:44 +0900177 /* Set an initial value for the MMU.TTB so we don't have to
178 * check for a null value. */
179 set_TTB(swapper_pg_dir);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#elif defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4)
182 /*
183 * If we don't have CONFIG_MMU set and the processor in question
184 * still has an MMU, care needs to be taken to make sure it doesn't
185 * stay on.. Since the boot loader could have potentially already
186 * turned it on, and we clearly don't want it, we simply turn it off.
187 *
188 * We don't need to do anything special for the zone sizes, since the
189 * default values that were already configured up above should be
190 * satisfactory.
191 */
192 disable_mmu();
193#endif
194 NODE_DATA(0)->node_mem_map = NULL;
195 free_area_init_node(0, NODE_DATA(0), zones_size, __MEMORY_START >> PAGE_SHIFT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Paul Mundt2cb7ce32006-09-27 18:20:58 +0900198static struct kcore_list kcore_mem, kcore_vmalloc;
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200void __init mem_init(void)
201{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int codesize, reservedpages, datasize, initsize;
203 int tmp;
204 extern unsigned long memory_start;
205
206#ifdef CONFIG_MMU
207 high_memory = (void *)__va(MAX_LOW_PFN * PAGE_SIZE);
208#else
209 extern unsigned long memory_end;
210
211 high_memory = (void *)(memory_end & PAGE_MASK);
212#endif
213
214 max_mapnr = num_physpages = MAP_NR(high_memory) - MAP_NR(memory_start);
215
216 /* clear the zero-page */
217 memset(empty_zero_page, 0, PAGE_SIZE);
218 __flush_wback_region(empty_zero_page, PAGE_SIZE);
219
Paul Mundt65463b72005-11-07 00:58:24 -0800220 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * Setup wrappers for copy/clear_page(), these will get overridden
222 * later in the boot process if a better method is available.
223 */
Yoshinori Satoe96636c2006-09-27 17:21:02 +0900224#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 copy_page = copy_page_slow;
226 clear_page = clear_page_slow;
Yoshinori Satoe96636c2006-09-27 17:21:02 +0900227#else
228 copy_page = copy_page_nommu;
229 clear_page = clear_page_nommu;
230#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* this will put all low memory onto the freelists */
233 totalram_pages += free_all_bootmem_node(NODE_DATA(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 reservedpages = 0;
235 for (tmp = 0; tmp < num_physpages; tmp++)
236 /*
237 * Only count reserved RAM pages
238 */
239 if (PageReserved(mem_map+tmp))
240 reservedpages++;
241
242 codesize = (unsigned long) &_etext - (unsigned long) &_text;
243 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
244 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
245
Paul Mundt2cb7ce32006-09-27 18:20:58 +0900246 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
247 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
248 VMALLOC_END - VMALLOC_START);
249
250 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
251 "%dk reserved, %dk data, %dk init)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
253 max_mapnr << (PAGE_SHIFT-10),
254 codesize >> 10,
255 reservedpages << (PAGE_SHIFT-10),
256 datasize >> 10,
257 initsize >> 10);
258
259 p3_cache_init();
Paul Mundt19f9a342006-09-27 18:33:49 +0900260
261 /* Initialize the vDSO */
262 vsyscall_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265void free_initmem(void)
266{
267 unsigned long addr;
Paul Mundt65463b72005-11-07 00:58:24 -0800268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 addr = (unsigned long)(&__init_begin);
270 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
271 ClearPageReserved(virt_to_page(addr));
Nick Piggin7835e982006-03-22 00:08:40 -0800272 init_page_count(virt_to_page(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 free_page(addr);
274 totalram_pages++;
275 }
276 printk ("Freeing unused kernel memory: %dk freed\n", (&__init_end - &__init_begin) >> 10);
277}
278
279#ifdef CONFIG_BLK_DEV_INITRD
280void free_initrd_mem(unsigned long start, unsigned long end)
281{
282 unsigned long p;
283 for (p = start; p < end; p += PAGE_SIZE) {
284 ClearPageReserved(virt_to_page(p));
Nick Piggin7835e982006-03-22 00:08:40 -0800285 init_page_count(virt_to_page(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 free_page(p);
287 totalram_pages++;
288 }
289 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
290}
291#endif
292