blob: 2032b39c229aa83839854622e98591065568332c [file] [log] [blame]
Paul Mundt01066622007-03-28 16:38:13 +09001/*
2 * linux/arch/sh/mm/init.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 1999 Niibe Yutaka
Paul Mundt01066622007-03-28 16:38:13 +09005 * Copyright (C) 2002 - 2007 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Based on linux/arch/i386/mm/init.c:
8 * Copyright (C) 1995 Linus Torvalds
9 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/mm.h>
11#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/bootmem.h>
Paul Mundt2cb7ce32006-09-27 18:20:58 +090014#include <linux/proc_fs.h>
Paul Mundt27641de2007-05-14 10:48:01 +090015#include <linux/pagemap.h>
Paul Mundt01066622007-03-28 16:38:13 +090016#include <linux/percpu.h>
17#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/tlb.h>
20#include <asm/cacheflush.h>
21#include <asm/cache.h>
22
23DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
24pgd_t swapper_pg_dir[PTRS_PER_PGD];
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026void (*copy_page)(void *from, void *to);
27void (*clear_page)(void *to);
28
29void show_mem(void)
30{
Paul Mundt01066622007-03-28 16:38:13 +090031 int total = 0, reserved = 0, free = 0;
32 int shared = 0, cached = 0, slab = 0;
33 pg_data_t *pgdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35 printk("Mem-info:\n");
36 show_free_areas();
Paul Mundt01066622007-03-28 16:38:13 +090037
38 for_each_online_pgdat(pgdat) {
Paul Mundtdfbb9042007-05-23 17:48:36 +090039 unsigned long flags, i;
Paul Mundt01066622007-03-28 16:38:13 +090040
41 pgdat_resize_lock(pgdat, &flags);
Paul Mundtdfbb9042007-05-23 17:48:36 +090042 for (i = 0; i < pgdat->node_spanned_pages; i++) {
43 struct page *page = pgdat_page_nr(pgdat, i);
Paul Mundt01066622007-03-28 16:38:13 +090044 total++;
45 if (PageReserved(page))
46 reserved++;
47 else if (PageSwapCache(page))
48 cached++;
49 else if (PageSlab(page))
50 slab++;
51 else if (!page_count(page))
52 free++;
53 else
54 shared += page_count(page) - 1;
Paul Mundtdfbb9042007-05-23 17:48:36 +090055 }
Paul Mundt01066622007-03-28 16:38:13 +090056 pgdat_resize_unlock(pgdat, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 }
Paul Mundt01066622007-03-28 16:38:13 +090058
59 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
60 printk("%d pages of RAM\n", total);
61 printk("%d free pages\n", free);
62 printk("%d reserved pages\n", reserved);
63 printk("%d slab pages\n", slab);
64 printk("%d pages shared\n", shared);
65 printk("%d pages swap cached\n", cached);
Paul Mundt5f8c9902007-05-08 11:55:21 +090066 printk(KERN_INFO "Total of %ld pages in page table cache\n",
67 quicklist_total_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Yoshinori Sato11cbb702006-12-07 18:07:27 +090070#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
72{
73 pgd_t *pgd;
Paul Mundt26ff6c12006-09-27 15:13:36 +090074 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 pmd_t *pmd;
76 pte_t *pte;
77
Stuart Menefy99a596f2006-11-21 15:38:05 +090078 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (pgd_none(*pgd)) {
80 pgd_ERROR(*pgd);
81 return;
82 }
83
Stuart Menefy99a596f2006-11-21 15:38:05 +090084 pud = pud_alloc(NULL, pgd, addr);
85 if (unlikely(!pud)) {
86 pud_ERROR(*pud);
87 return;
Paul Mundt26ff6c12006-09-27 15:13:36 +090088 }
89
Stuart Menefy99a596f2006-11-21 15:38:05 +090090 pmd = pmd_alloc(NULL, pud, addr);
91 if (unlikely(!pmd)) {
92 pmd_ERROR(*pmd);
93 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95
96 pte = pte_offset_kernel(pmd, addr);
97 if (!pte_none(*pte)) {
98 pte_ERROR(*pte);
99 return;
100 }
101
102 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
103
Paul Mundtea9af692006-12-25 19:28:54 +0900104 flush_tlb_one(get_asid(), addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107/*
108 * As a performance optimization, other platforms preserve the fixmap mapping
109 * across a context switch, we don't presently do this, but this could be done
110 * in a similar fashion as to the wired TLB interface that sh64 uses (by way
Simon Arlotte868d612007-05-14 08:15:10 +0900111 * of the memory mapped UTLB configuration) -- this unfortunately forces us to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * give up a TLB entry for each mapping we want to preserve. While this may be
113 * viable for a small number of fixmaps, it's not particularly useful for
114 * everything and needs to be carefully evaluated. (ie, we may want this for
115 * the vsyscall page).
116 *
117 * XXX: Perhaps add a _PAGE_WIRED flag or something similar that we can pass
118 * in at __set_fixmap() time to determine the appropriate behavior to follow.
119 *
120 * -- PFM.
121 */
122void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
123{
124 unsigned long address = __fix_to_virt(idx);
125
126 if (idx >= __end_of_fixed_addresses) {
127 BUG();
128 return;
129 }
130
131 set_pte_phys(address, phys, prot);
132}
Yoshinori Sato11cbb702006-12-07 18:07:27 +0900133#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/* References to section boundaries */
136
137extern char _text, _etext, _edata, __bss_start, _end;
138extern char __init_begin, __init_end;
139
140/*
141 * paging_init() sets up the page tables
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 */
143void __init paging_init(void)
144{
Paul Mundt2de212e2007-06-06 12:09:54 +0900145 unsigned long max_zone_pfns[MAX_NR_ZONES];
Paul Mundt01066622007-03-28 16:38:13 +0900146 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Paul Mundt01066622007-03-28 16:38:13 +0900148 /* We don't need to map the kernel through the TLB, as
149 * it is permanatly mapped using P1. So clear the
150 * entire pgd. */
151 memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Stuart Menefy6e4662f2006-11-21 13:53:44 +0900153 /* Set an initial value for the MMU.TTB so we don't have to
154 * check for a null value. */
155 set_TTB(swapper_pg_dir);
156
Paul Mundt2de212e2007-06-06 12:09:54 +0900157 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
158
Paul Mundt01066622007-03-28 16:38:13 +0900159 for_each_online_node(nid) {
160 pg_data_t *pgdat = NODE_DATA(nid);
Paul Mundt01066622007-03-28 16:38:13 +0900161 unsigned long low, start_pfn;
162
Paul Mundt01066622007-03-28 16:38:13 +0900163 start_pfn = pgdat->bdata->node_boot_start >> PAGE_SHIFT;
164 low = pgdat->bdata->node_low_pfn;
165
Paul Mundt2de212e2007-06-06 12:09:54 +0900166 if (max_zone_pfns[ZONE_NORMAL] < low)
167 max_zone_pfns[ZONE_NORMAL] = low;
Paul Mundt01066622007-03-28 16:38:13 +0900168
169 printk("Node %u: start_pfn = 0x%lx, low = 0x%lx\n",
170 nid, start_pfn, low);
Paul Mundt01066622007-03-28 16:38:13 +0900171 }
Paul Mundt2de212e2007-06-06 12:09:54 +0900172
173 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Paul Mundt2cb7ce32006-09-27 18:20:58 +0900176static struct kcore_list kcore_mem, kcore_vmalloc;
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178void __init mem_init(void)
179{
Paul Mundtdfbb9042007-05-23 17:48:36 +0900180 int codesize, datasize, initsize;
Paul Mundt01066622007-03-28 16:38:13 +0900181 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Paul Mundt2de212e2007-06-06 12:09:54 +0900183 num_physpages = 0;
184 high_memory = NULL;
185
Paul Mundt01066622007-03-28 16:38:13 +0900186 for_each_online_node(nid) {
187 pg_data_t *pgdat = NODE_DATA(nid);
188 unsigned long node_pages = 0;
189 void *node_high_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Paul Mundt01066622007-03-28 16:38:13 +0900191 num_physpages += pgdat->node_present_pages;
192
193 if (pgdat->node_spanned_pages)
194 node_pages = free_all_bootmem_node(pgdat);
195
196 totalram_pages += node_pages;
197
Paul Mundt2de212e2007-06-06 12:09:54 +0900198 node_high_memory = (void *)__va((pgdat->node_start_pfn +
199 pgdat->node_spanned_pages) <<
200 PAGE_SHIFT);
Paul Mundt01066622007-03-28 16:38:13 +0900201 if (node_high_memory > high_memory)
202 high_memory = node_high_memory;
203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* clear the zero-page */
206 memset(empty_zero_page, 0, PAGE_SIZE);
207 __flush_wback_region(empty_zero_page, PAGE_SIZE);
208
Paul Mundt65463b72005-11-07 00:58:24 -0800209 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * Setup wrappers for copy/clear_page(), these will get overridden
211 * later in the boot process if a better method is available.
212 */
Yoshinori Satoe96636c2006-09-27 17:21:02 +0900213#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 copy_page = copy_page_slow;
215 clear_page = clear_page_slow;
Yoshinori Satoe96636c2006-09-27 17:21:02 +0900216#else
217 copy_page = copy_page_nommu;
218 clear_page = clear_page_nommu;
219#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 codesize = (unsigned long) &_etext - (unsigned long) &_text;
222 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
223 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
224
Paul Mundt2cb7ce32006-09-27 18:20:58 +0900225 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
226 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
227 VMALLOC_END - VMALLOC_START);
228
229 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
Paul Mundtdfbb9042007-05-23 17:48:36 +0900230 "%dk data, %dk init)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
Paul Mundt2de212e2007-06-06 12:09:54 +0900232 num_physpages << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 codesize >> 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 datasize >> 10,
235 initsize >> 10);
236
237 p3_cache_init();
Paul Mundt19f9a342006-09-27 18:33:49 +0900238
239 /* Initialize the vDSO */
240 vsyscall_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243void free_initmem(void)
244{
245 unsigned long addr;
Paul Mundt65463b72005-11-07 00:58:24 -0800246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 addr = (unsigned long)(&__init_begin);
248 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
249 ClearPageReserved(virt_to_page(addr));
Nick Piggin7835e982006-03-22 00:08:40 -0800250 init_page_count(virt_to_page(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 free_page(addr);
252 totalram_pages++;
253 }
Paul Mundt2de212e2007-06-06 12:09:54 +0900254 printk("Freeing unused kernel memory: %dk freed\n",
255 (&__init_end - &__init_begin) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258#ifdef CONFIG_BLK_DEV_INITRD
259void free_initrd_mem(unsigned long start, unsigned long end)
260{
261 unsigned long p;
262 for (p = start; p < end; p += PAGE_SIZE) {
263 ClearPageReserved(virt_to_page(p));
Nick Piggin7835e982006-03-22 00:08:40 -0800264 init_page_count(virt_to_page(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 free_page(p);
266 totalram_pages++;
267 }
Paul Mundt2de212e2007-06-06 12:09:54 +0900268 printk("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270#endif