blob: d7df26bd1e5468b0f79a702220bf85352c61a0bc [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>
Paul Mundt07cbb412007-06-06 12:23:06 +090021#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/cache.h>
23
24DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
25pgd_t swapper_pg_dir[PTRS_PER_PGD];
Stuart Menefycbaa1182007-11-30 17:06:36 +090026unsigned long cached_to_uncached = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028void 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) {
Paul Mundtdfbb9042007-05-23 17:48:36 +090038 unsigned long flags, i;
Paul Mundt01066622007-03-28 16:38:13 +090039
40 pgdat_resize_lock(pgdat, &flags);
Paul Mundtdfbb9042007-05-23 17:48:36 +090041 for (i = 0; i < pgdat->node_spanned_pages; i++) {
42 struct page *page = pgdat_page_nr(pgdat, i);
Paul Mundt01066622007-03-28 16:38:13 +090043 total++;
44 if (PageReserved(page))
45 reserved++;
46 else if (PageSwapCache(page))
47 cached++;
48 else if (PageSlab(page))
49 slab++;
50 else if (!page_count(page))
51 free++;
52 else
53 shared += page_count(page) - 1;
Paul Mundtdfbb9042007-05-23 17:48:36 +090054 }
Paul Mundt01066622007-03-28 16:38:13 +090055 pgdat_resize_unlock(pgdat, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 }
Paul Mundt01066622007-03-28 16:38:13 +090057
58 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
59 printk("%d pages of RAM\n", total);
60 printk("%d free pages\n", free);
61 printk("%d reserved pages\n", reserved);
62 printk("%d slab pages\n", slab);
63 printk("%d pages shared\n", shared);
64 printk("%d pages swap cached\n", cached);
Paul Mundt5f8c9902007-05-08 11:55:21 +090065 printk(KERN_INFO "Total of %ld pages in page table cache\n",
66 quicklist_total_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Yoshinori Sato11cbb702006-12-07 18:07:27 +090069#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
71{
72 pgd_t *pgd;
Paul Mundt26ff6c12006-09-27 15:13:36 +090073 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 pmd_t *pmd;
75 pte_t *pte;
76
Stuart Menefy99a596f2006-11-21 15:38:05 +090077 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (pgd_none(*pgd)) {
79 pgd_ERROR(*pgd);
80 return;
81 }
82
Stuart Menefy99a596f2006-11-21 15:38:05 +090083 pud = pud_alloc(NULL, pgd, addr);
84 if (unlikely(!pud)) {
85 pud_ERROR(*pud);
86 return;
Paul Mundt26ff6c12006-09-27 15:13:36 +090087 }
88
Stuart Menefy99a596f2006-11-21 15:38:05 +090089 pmd = pmd_alloc(NULL, pud, addr);
90 if (unlikely(!pmd)) {
91 pmd_ERROR(*pmd);
92 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94
95 pte = pte_offset_kernel(pmd, addr);
96 if (!pte_none(*pte)) {
97 pte_ERROR(*pte);
98 return;
99 }
100
101 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
102
Stuart Menefycbaa1182007-11-30 17:06:36 +0900103 if (cached_to_uncached)
104 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}
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900133
134void __init page_table_range_init(unsigned long start, unsigned long end,
135 pgd_t *pgd_base)
136{
137 pgd_t *pgd;
138 pud_t *pud;
139 pmd_t *pmd;
140 int pgd_idx;
141 unsigned long vaddr;
142
143 vaddr = start & PMD_MASK;
144 end = (end + PMD_SIZE - 1) & PMD_MASK;
145 pgd_idx = pgd_index(vaddr);
146 pgd = pgd_base + pgd_idx;
147
148 for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
149 BUG_ON(pgd_none(*pgd));
150 pud = pud_offset(pgd, 0);
151 BUG_ON(pud_none(*pud));
152 pmd = pmd_offset(pud, 0);
153
154 if (!pmd_present(*pmd)) {
155 pte_t *pte_table;
156 pte_table = (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
157 memset(pte_table, 0, PAGE_SIZE);
158 pmd_populate_kernel(&init_mm, pmd, pte_table);
159 }
160
161 vaddr += PMD_SIZE;
162 }
163}
Yoshinori Sato11cbb702006-12-07 18:07:27 +0900164#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*
167 * paging_init() sets up the page tables
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
169void __init paging_init(void)
170{
Paul Mundt2de212e2007-06-06 12:09:54 +0900171 unsigned long max_zone_pfns[MAX_NR_ZONES];
Paul Mundt01066622007-03-28 16:38:13 +0900172 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Paul Mundt01066622007-03-28 16:38:13 +0900174 /* We don't need to map the kernel through the TLB, as
175 * it is permanatly mapped using P1. So clear the
176 * entire pgd. */
177 memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Stuart Menefy6e4662f2006-11-21 13:53:44 +0900179 /* Set an initial value for the MMU.TTB so we don't have to
180 * check for a null value. */
181 set_TTB(swapper_pg_dir);
182
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900183 /* Populate the relevant portions of swapper_pg_dir so that
184 * we can use the fixmap entries without calling kmalloc.
185 * pte's will be filled in by __set_fixmap(). */
186 page_table_range_init(FIXADDR_START, FIXADDR_TOP, swapper_pg_dir);
187
Paul Mundt2de212e2007-06-06 12:09:54 +0900188 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
189
Paul Mundt01066622007-03-28 16:38:13 +0900190 for_each_online_node(nid) {
191 pg_data_t *pgdat = NODE_DATA(nid);
Paul Mundt01066622007-03-28 16:38:13 +0900192 unsigned long low, start_pfn;
193
Paul Mundt01066622007-03-28 16:38:13 +0900194 start_pfn = pgdat->bdata->node_boot_start >> PAGE_SHIFT;
195 low = pgdat->bdata->node_low_pfn;
196
Paul Mundt2de212e2007-06-06 12:09:54 +0900197 if (max_zone_pfns[ZONE_NORMAL] < low)
198 max_zone_pfns[ZONE_NORMAL] = low;
Paul Mundt01066622007-03-28 16:38:13 +0900199
200 printk("Node %u: start_pfn = 0x%lx, low = 0x%lx\n",
201 nid, start_pfn, low);
Paul Mundt01066622007-03-28 16:38:13 +0900202 }
Paul Mundt2de212e2007-06-06 12:09:54 +0900203
204 free_area_init_nodes(max_zone_pfns);
Stuart Menefycbaa1182007-11-30 17:06:36 +0900205
Paul Mundtdb026122008-02-13 20:18:01 +0900206#ifdef CONFIG_SUPERH32
Stuart Menefycbaa1182007-11-30 17:06:36 +0900207 /* Set up the uncached fixmap */
208 set_fixmap_nocache(FIX_UNCACHED, __pa(&__uncached_start));
209
210#ifdef CONFIG_29BIT
211 /*
212 * Handle trivial transitions between cached and uncached
213 * segments, making use of the 1:1 mapping relationship in
214 * 512MB lowmem.
215 */
216 cached_to_uncached = P2SEG - P1SEG;
217#endif
Paul Mundtdb026122008-02-13 20:18:01 +0900218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Paul Mundt2cb7ce32006-09-27 18:20:58 +0900221static struct kcore_list kcore_mem, kcore_vmalloc;
Paul Mundtba2727b2007-11-20 15:14:48 +0900222int after_bootmem = 0;
Paul Mundt2cb7ce32006-09-27 18:20:58 +0900223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224void __init mem_init(void)
225{
Paul Mundtdfbb9042007-05-23 17:48:36 +0900226 int codesize, datasize, initsize;
Paul Mundt01066622007-03-28 16:38:13 +0900227 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Paul Mundt2de212e2007-06-06 12:09:54 +0900229 num_physpages = 0;
230 high_memory = NULL;
231
Paul Mundt01066622007-03-28 16:38:13 +0900232 for_each_online_node(nid) {
233 pg_data_t *pgdat = NODE_DATA(nid);
234 unsigned long node_pages = 0;
235 void *node_high_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Paul Mundt01066622007-03-28 16:38:13 +0900237 num_physpages += pgdat->node_present_pages;
238
239 if (pgdat->node_spanned_pages)
240 node_pages = free_all_bootmem_node(pgdat);
241
242 totalram_pages += node_pages;
243
Paul Mundt2de212e2007-06-06 12:09:54 +0900244 node_high_memory = (void *)__va((pgdat->node_start_pfn +
245 pgdat->node_spanned_pages) <<
246 PAGE_SHIFT);
Paul Mundt01066622007-03-28 16:38:13 +0900247 if (node_high_memory > high_memory)
248 high_memory = node_high_memory;
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /* clear the zero-page */
252 memset(empty_zero_page, 0, PAGE_SIZE);
253 __flush_wback_region(empty_zero_page, PAGE_SIZE);
254
Paul Mundtba2727b2007-11-20 15:14:48 +0900255 after_bootmem = 1;
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 codesize = (unsigned long) &_etext - (unsigned long) &_text;
258 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
259 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
260
Paul Mundt2cb7ce32006-09-27 18:20:58 +0900261 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
262 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
263 VMALLOC_END - VMALLOC_START);
264
265 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
Paul Mundtdfbb9042007-05-23 17:48:36 +0900266 "%dk data, %dk init)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
Paul Mundt2de212e2007-06-06 12:09:54 +0900268 num_physpages << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 codesize >> 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 datasize >> 10,
271 initsize >> 10);
272
273 p3_cache_init();
Paul Mundt19f9a342006-09-27 18:33:49 +0900274
275 /* Initialize the vDSO */
276 vsyscall_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279void free_initmem(void)
280{
281 unsigned long addr;
Paul Mundt65463b72005-11-07 00:58:24 -0800282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 addr = (unsigned long)(&__init_begin);
284 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
285 ClearPageReserved(virt_to_page(addr));
Nick Piggin7835e982006-03-22 00:08:40 -0800286 init_page_count(virt_to_page(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 free_page(addr);
288 totalram_pages++;
289 }
Paul Mundt07cbb412007-06-06 12:23:06 +0900290 printk("Freeing unused kernel memory: %ldk freed\n",
291 ((unsigned long)&__init_end -
292 (unsigned long)&__init_begin) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295#ifdef CONFIG_BLK_DEV_INITRD
296void free_initrd_mem(unsigned long start, unsigned long end)
297{
298 unsigned long p;
299 for (p = start; p < end; p += PAGE_SIZE) {
300 ClearPageReserved(virt_to_page(p));
Nick Piggin7835e982006-03-22 00:08:40 -0800301 init_page_count(virt_to_page(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 free_page(p);
303 totalram_pages++;
304 }
Paul Mundt2de212e2007-06-06 12:09:54 +0900305 printk("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307#endif
Paul Mundt33d63bd2007-06-07 11:32:52 +0900308
309#ifdef CONFIG_MEMORY_HOTPLUG
Paul Mundt33d63bd2007-06-07 11:32:52 +0900310int arch_add_memory(int nid, u64 start, u64 size)
311{
312 pg_data_t *pgdat;
313 unsigned long start_pfn = start >> PAGE_SHIFT;
314 unsigned long nr_pages = size >> PAGE_SHIFT;
315 int ret;
316
317 pgdat = NODE_DATA(nid);
318
319 /* We only have ZONE_NORMAL, so this is easy.. */
320 ret = __add_pages(pgdat->node_zones + ZONE_NORMAL, start_pfn, nr_pages);
321 if (unlikely(ret))
Harvey Harrison866e6b92008-03-04 15:23:47 -0800322 printk("%s: Failed, __add_pages() == %d\n", __func__, ret);
Paul Mundt33d63bd2007-06-07 11:32:52 +0900323
324 return ret;
325}
326EXPORT_SYMBOL_GPL(arch_add_memory);
327
Paul Mundt357d5942007-06-11 15:32:07 +0900328#ifdef CONFIG_NUMA
Paul Mundt33d63bd2007-06-07 11:32:52 +0900329int memory_add_physaddr_to_nid(u64 addr)
330{
331 /* Node 0 for now.. */
332 return 0;
333}
334EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
335#endif
Paul Mundt357d5942007-06-11 15:32:07 +0900336#endif