blob: 4d030988b3681b3102e859d1f4464afbd23cdef8 [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 Mundt01066622007-03-28 16:38:13 +090015#include <linux/percpu.h>
16#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/tlb.h>
19#include <asm/cacheflush.h>
20#include <asm/cache.h>
21
22DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
23pgd_t swapper_pg_dir[PTRS_PER_PGD];
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025void (*copy_page)(void *from, void *to);
26void (*clear_page)(void *to);
27
28void show_mem(void)
29{
Paul Mundt01066622007-03-28 16:38:13 +090030 int total = 0, reserved = 0, free = 0;
31 int shared = 0, cached = 0, slab = 0;
32 pg_data_t *pgdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34 printk("Mem-info:\n");
35 show_free_areas();
Paul Mundt01066622007-03-28 16:38:13 +090036
37 for_each_online_pgdat(pgdat) {
38 struct page *page, *end;
39 unsigned long flags;
40
41 pgdat_resize_lock(pgdat, &flags);
42 page = pgdat->node_mem_map;
43 end = page + pgdat->node_spanned_pages;
44
45 do {
46 total++;
47 if (PageReserved(page))
48 reserved++;
49 else if (PageSwapCache(page))
50 cached++;
51 else if (PageSlab(page))
52 slab++;
53 else if (!page_count(page))
54 free++;
55 else
56 shared += page_count(page) - 1;
57 page++;
58 } while (page < end);
59
60 pgdat_resize_unlock(pgdat, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 }
Paul Mundt01066622007-03-28 16:38:13 +090062
63 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
64 printk("%d pages of RAM\n", total);
65 printk("%d free pages\n", free);
66 printk("%d reserved pages\n", reserved);
67 printk("%d slab pages\n", slab);
68 printk("%d pages shared\n", shared);
69 printk("%d pages swap cached\n", cached);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
Yoshinori Sato11cbb702006-12-07 18:07:27 +090072#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
74{
75 pgd_t *pgd;
Paul Mundt26ff6c12006-09-27 15:13:36 +090076 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 pmd_t *pmd;
78 pte_t *pte;
79
Stuart Menefy99a596f2006-11-21 15:38:05 +090080 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (pgd_none(*pgd)) {
82 pgd_ERROR(*pgd);
83 return;
84 }
85
Stuart Menefy99a596f2006-11-21 15:38:05 +090086 pud = pud_alloc(NULL, pgd, addr);
87 if (unlikely(!pud)) {
88 pud_ERROR(*pud);
89 return;
Paul Mundt26ff6c12006-09-27 15:13:36 +090090 }
91
Stuart Menefy99a596f2006-11-21 15:38:05 +090092 pmd = pmd_alloc(NULL, pud, addr);
93 if (unlikely(!pmd)) {
94 pmd_ERROR(*pmd);
95 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97
98 pte = pte_offset_kernel(pmd, addr);
99 if (!pte_none(*pte)) {
100 pte_ERROR(*pte);
101 return;
102 }
103
104 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
105
Paul Mundtea9af692006-12-25 19:28:54 +0900106 flush_tlb_one(get_asid(), addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/*
110 * As a performance optimization, other platforms preserve the fixmap mapping
111 * across a context switch, we don't presently do this, but this could be done
112 * in a similar fashion as to the wired TLB interface that sh64 uses (by way
113 * of the memorry mapped UTLB configuration) -- this unfortunately forces us to
114 * give up a TLB entry for each mapping we want to preserve. While this may be
115 * viable for a small number of fixmaps, it's not particularly useful for
116 * everything and needs to be carefully evaluated. (ie, we may want this for
117 * the vsyscall page).
118 *
119 * XXX: Perhaps add a _PAGE_WIRED flag or something similar that we can pass
120 * in at __set_fixmap() time to determine the appropriate behavior to follow.
121 *
122 * -- PFM.
123 */
124void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
125{
126 unsigned long address = __fix_to_virt(idx);
127
128 if (idx >= __end_of_fixed_addresses) {
129 BUG();
130 return;
131 }
132
133 set_pte_phys(address, phys, prot);
134}
Yoshinori Sato11cbb702006-12-07 18:07:27 +0900135#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/* References to section boundaries */
138
139extern char _text, _etext, _edata, __bss_start, _end;
140extern char __init_begin, __init_end;
141
142/*
143 * paging_init() sets up the page tables
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 */
145void __init paging_init(void)
146{
Paul Mundt01066622007-03-28 16:38:13 +0900147 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Paul Mundt01066622007-03-28 16:38:13 +0900149 /* We don't need to map the kernel through the TLB, as
150 * it is permanatly mapped using P1. So clear the
151 * entire pgd. */
152 memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Stuart Menefy6e4662f2006-11-21 13:53:44 +0900154 /* Set an initial value for the MMU.TTB so we don't have to
155 * check for a null value. */
156 set_TTB(swapper_pg_dir);
157
Paul Mundt01066622007-03-28 16:38:13 +0900158 for_each_online_node(nid) {
159 pg_data_t *pgdat = NODE_DATA(nid);
160 unsigned long max_zone_pfns[MAX_NR_ZONES];
161 unsigned long low, start_pfn;
162
163 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
164
165 start_pfn = pgdat->bdata->node_boot_start >> PAGE_SHIFT;
166 low = pgdat->bdata->node_low_pfn;
167
168 max_zone_pfns[ZONE_NORMAL] = low;
169 add_active_range(nid, start_pfn, low);
170
171 printk("Node %u: start_pfn = 0x%lx, low = 0x%lx\n",
172 nid, start_pfn, low);
173
174 free_area_init_nodes(max_zone_pfns);
175
176 printk("Node %u: mem_map starts at %p\n",
177 pgdat->node_id, pgdat->node_mem_map);
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Paul Mundt2cb7ce32006-09-27 18:20:58 +0900181static struct kcore_list kcore_mem, kcore_vmalloc;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183void __init mem_init(void)
184{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int codesize, reservedpages, datasize, initsize;
Paul Mundt01066622007-03-28 16:38:13 +0900186 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Paul Mundt01066622007-03-28 16:38:13 +0900188 reservedpages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Paul Mundt01066622007-03-28 16:38:13 +0900190 for_each_online_node(nid) {
191 pg_data_t *pgdat = NODE_DATA(nid);
192 unsigned long node_pages = 0;
193 void *node_high_memory;
194 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Paul Mundt01066622007-03-28 16:38:13 +0900196 num_physpages += pgdat->node_present_pages;
197
198 if (pgdat->node_spanned_pages)
199 node_pages = free_all_bootmem_node(pgdat);
200
201 totalram_pages += node_pages;
202
203 for (i = 0; i < node_pages; i++)
204 if (PageReserved(pgdat->node_mem_map + i))
205 reservedpages++;
206
207 node_high_memory = (void *)((pgdat->node_start_pfn +
208 pgdat->node_spanned_pages) <<
209 PAGE_SHIFT);
210 if (node_high_memory > high_memory)
211 high_memory = node_high_memory;
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* clear the zero-page */
215 memset(empty_zero_page, 0, PAGE_SIZE);
216 __flush_wback_region(empty_zero_page, PAGE_SIZE);
217
Paul Mundt65463b72005-11-07 00:58:24 -0800218 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * Setup wrappers for copy/clear_page(), these will get overridden
220 * later in the boot process if a better method is available.
221 */
Yoshinori Satoe96636c2006-09-27 17:21:02 +0900222#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 copy_page = copy_page_slow;
224 clear_page = clear_page_slow;
Yoshinori Satoe96636c2006-09-27 17:21:02 +0900225#else
226 copy_page = copy_page_nommu;
227 clear_page = clear_page_nommu;
228#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 codesize = (unsigned long) &_etext - (unsigned long) &_text;
231 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
232 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
233
Paul Mundt2cb7ce32006-09-27 18:20:58 +0900234 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
235 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
236 VMALLOC_END - VMALLOC_START);
237
238 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
239 "%dk reserved, %dk data, %dk init)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
Paul Mundt01066622007-03-28 16:38:13 +0900241 totalram_pages << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 codesize >> 10,
243 reservedpages << (PAGE_SHIFT-10),
244 datasize >> 10,
245 initsize >> 10);
246
247 p3_cache_init();
Paul Mundt19f9a342006-09-27 18:33:49 +0900248
249 /* Initialize the vDSO */
250 vsyscall_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253void free_initmem(void)
254{
255 unsigned long addr;
Paul Mundt65463b72005-11-07 00:58:24 -0800256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 addr = (unsigned long)(&__init_begin);
258 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
259 ClearPageReserved(virt_to_page(addr));
Nick Piggin7835e982006-03-22 00:08:40 -0800260 init_page_count(virt_to_page(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 free_page(addr);
262 totalram_pages++;
263 }
264 printk ("Freeing unused kernel memory: %dk freed\n", (&__init_end - &__init_begin) >> 10);
265}
266
267#ifdef CONFIG_BLK_DEV_INITRD
268void free_initrd_mem(unsigned long start, unsigned long end)
269{
270 unsigned long p;
271 for (p = start; p < end; p += PAGE_SIZE) {
272 ClearPageReserved(virt_to_page(p));
Nick Piggin7835e982006-03-22 00:08:40 -0800273 init_page_count(virt_to_page(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 free_page(p);
275 totalram_pages++;
276 }
277 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
278}
279#endif