blob: 79c309780f953a16bf65c621aae490ffe10e9c8d [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];
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027void show_mem(void)
28{
Paul Mundt01066622007-03-28 16:38:13 +090029 int total = 0, reserved = 0, free = 0;
30 int shared = 0, cached = 0, slab = 0;
31 pg_data_t *pgdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33 printk("Mem-info:\n");
34 show_free_areas();
Paul Mundt01066622007-03-28 16:38:13 +090035
36 for_each_online_pgdat(pgdat) {
Paul Mundtdfbb9042007-05-23 17:48:36 +090037 unsigned long flags, i;
Paul Mundt01066622007-03-28 16:38:13 +090038
39 pgdat_resize_lock(pgdat, &flags);
Paul Mundtdfbb9042007-05-23 17:48:36 +090040 for (i = 0; i < pgdat->node_spanned_pages; i++) {
41 struct page *page = pgdat_page_nr(pgdat, i);
Paul Mundt01066622007-03-28 16:38:13 +090042 total++;
43 if (PageReserved(page))
44 reserved++;
45 else if (PageSwapCache(page))
46 cached++;
47 else if (PageSlab(page))
48 slab++;
49 else if (!page_count(page))
50 free++;
51 else
52 shared += page_count(page) - 1;
Paul Mundtdfbb9042007-05-23 17:48:36 +090053 }
Paul Mundt01066622007-03-28 16:38:13 +090054 pgdat_resize_unlock(pgdat, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
Paul Mundt01066622007-03-28 16:38:13 +090056
57 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
58 printk("%d pages of RAM\n", total);
59 printk("%d free pages\n", free);
60 printk("%d reserved pages\n", reserved);
61 printk("%d slab pages\n", slab);
62 printk("%d pages shared\n", shared);
63 printk("%d pages swap cached\n", cached);
Paul Mundt5f8c9902007-05-08 11:55:21 +090064 printk(KERN_INFO "Total of %ld pages in page table cache\n",
65 quicklist_total_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
Yoshinori Sato11cbb702006-12-07 18:07:27 +090068#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
70{
71 pgd_t *pgd;
Paul Mundt26ff6c12006-09-27 15:13:36 +090072 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 pmd_t *pmd;
74 pte_t *pte;
75
Stuart Menefy99a596f2006-11-21 15:38:05 +090076 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (pgd_none(*pgd)) {
78 pgd_ERROR(*pgd);
79 return;
80 }
81
Stuart Menefy99a596f2006-11-21 15:38:05 +090082 pud = pud_alloc(NULL, pgd, addr);
83 if (unlikely(!pud)) {
84 pud_ERROR(*pud);
85 return;
Paul Mundt26ff6c12006-09-27 15:13:36 +090086 }
87
Stuart Menefy99a596f2006-11-21 15:38:05 +090088 pmd = pmd_alloc(NULL, pud, addr);
89 if (unlikely(!pmd)) {
90 pmd_ERROR(*pmd);
91 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
94 pte = pte_offset_kernel(pmd, addr);
95 if (!pte_none(*pte)) {
96 pte_ERROR(*pte);
97 return;
98 }
99
100 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
101
Paul Mundtea9af692006-12-25 19:28:54 +0900102 flush_tlb_one(get_asid(), addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105/*
106 * As a performance optimization, other platforms preserve the fixmap mapping
107 * across a context switch, we don't presently do this, but this could be done
108 * in a similar fashion as to the wired TLB interface that sh64 uses (by way
Simon Arlotte868d612007-05-14 08:15:10 +0900109 * of the memory mapped UTLB configuration) -- this unfortunately forces us to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * give up a TLB entry for each mapping we want to preserve. While this may be
111 * viable for a small number of fixmaps, it's not particularly useful for
112 * everything and needs to be carefully evaluated. (ie, we may want this for
113 * the vsyscall page).
114 *
115 * XXX: Perhaps add a _PAGE_WIRED flag or something similar that we can pass
116 * in at __set_fixmap() time to determine the appropriate behavior to follow.
117 *
118 * -- PFM.
119 */
120void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
121{
122 unsigned long address = __fix_to_virt(idx);
123
124 if (idx >= __end_of_fixed_addresses) {
125 BUG();
126 return;
127 }
128
129 set_pte_phys(address, phys, prot);
130}
Yoshinori Sato11cbb702006-12-07 18:07:27 +0900131#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/*
134 * paging_init() sets up the page tables
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 */
136void __init paging_init(void)
137{
Paul Mundt2de212e2007-06-06 12:09:54 +0900138 unsigned long max_zone_pfns[MAX_NR_ZONES];
Paul Mundt01066622007-03-28 16:38:13 +0900139 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Paul Mundt01066622007-03-28 16:38:13 +0900141 /* We don't need to map the kernel through the TLB, as
142 * it is permanatly mapped using P1. So clear the
143 * entire pgd. */
144 memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Stuart Menefy6e4662f2006-11-21 13:53:44 +0900146 /* Set an initial value for the MMU.TTB so we don't have to
147 * check for a null value. */
148 set_TTB(swapper_pg_dir);
149
Paul Mundt2de212e2007-06-06 12:09:54 +0900150 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
151
Paul Mundt01066622007-03-28 16:38:13 +0900152 for_each_online_node(nid) {
153 pg_data_t *pgdat = NODE_DATA(nid);
Paul Mundt01066622007-03-28 16:38:13 +0900154 unsigned long low, start_pfn;
155
Paul Mundt01066622007-03-28 16:38:13 +0900156 start_pfn = pgdat->bdata->node_boot_start >> PAGE_SHIFT;
157 low = pgdat->bdata->node_low_pfn;
158
Paul Mundt2de212e2007-06-06 12:09:54 +0900159 if (max_zone_pfns[ZONE_NORMAL] < low)
160 max_zone_pfns[ZONE_NORMAL] = low;
Paul Mundt01066622007-03-28 16:38:13 +0900161
162 printk("Node %u: start_pfn = 0x%lx, low = 0x%lx\n",
163 nid, start_pfn, low);
Paul Mundt01066622007-03-28 16:38:13 +0900164 }
Paul Mundt2de212e2007-06-06 12:09:54 +0900165
166 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Paul Mundt2cb7ce32006-09-27 18:20:58 +0900169static struct kcore_list kcore_mem, kcore_vmalloc;
Paul Mundtba2727b2007-11-20 15:14:48 +0900170int after_bootmem = 0;
Paul Mundt2cb7ce32006-09-27 18:20:58 +0900171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172void __init mem_init(void)
173{
Paul Mundtdfbb9042007-05-23 17:48:36 +0900174 int codesize, datasize, initsize;
Paul Mundt01066622007-03-28 16:38:13 +0900175 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Paul Mundt2de212e2007-06-06 12:09:54 +0900177 num_physpages = 0;
178 high_memory = NULL;
179
Paul Mundt01066622007-03-28 16:38:13 +0900180 for_each_online_node(nid) {
181 pg_data_t *pgdat = NODE_DATA(nid);
182 unsigned long node_pages = 0;
183 void *node_high_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Paul Mundt01066622007-03-28 16:38:13 +0900185 num_physpages += pgdat->node_present_pages;
186
187 if (pgdat->node_spanned_pages)
188 node_pages = free_all_bootmem_node(pgdat);
189
190 totalram_pages += node_pages;
191
Paul Mundt2de212e2007-06-06 12:09:54 +0900192 node_high_memory = (void *)__va((pgdat->node_start_pfn +
193 pgdat->node_spanned_pages) <<
194 PAGE_SHIFT);
Paul Mundt01066622007-03-28 16:38:13 +0900195 if (node_high_memory > high_memory)
196 high_memory = node_high_memory;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /* clear the zero-page */
200 memset(empty_zero_page, 0, PAGE_SIZE);
201 __flush_wback_region(empty_zero_page, PAGE_SIZE);
202
Paul Mundtba2727b2007-11-20 15:14:48 +0900203 after_bootmem = 1;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 codesize = (unsigned long) &_etext - (unsigned long) &_text;
206 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
207 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
208
Paul Mundt2cb7ce32006-09-27 18:20:58 +0900209 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
210 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
211 VMALLOC_END - VMALLOC_START);
212
213 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
Paul Mundtdfbb9042007-05-23 17:48:36 +0900214 "%dk data, %dk init)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
Paul Mundt2de212e2007-06-06 12:09:54 +0900216 num_physpages << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 codesize >> 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 datasize >> 10,
219 initsize >> 10);
220
221 p3_cache_init();
Paul Mundt19f9a342006-09-27 18:33:49 +0900222
223 /* Initialize the vDSO */
224 vsyscall_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227void free_initmem(void)
228{
229 unsigned long addr;
Paul Mundt65463b72005-11-07 00:58:24 -0800230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 addr = (unsigned long)(&__init_begin);
232 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
233 ClearPageReserved(virt_to_page(addr));
Nick Piggin7835e982006-03-22 00:08:40 -0800234 init_page_count(virt_to_page(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 free_page(addr);
236 totalram_pages++;
237 }
Paul Mundt07cbb412007-06-06 12:23:06 +0900238 printk("Freeing unused kernel memory: %ldk freed\n",
239 ((unsigned long)&__init_end -
240 (unsigned long)&__init_begin) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243#ifdef CONFIG_BLK_DEV_INITRD
244void free_initrd_mem(unsigned long start, unsigned long end)
245{
246 unsigned long p;
247 for (p = start; p < end; p += PAGE_SIZE) {
248 ClearPageReserved(virt_to_page(p));
Nick Piggin7835e982006-03-22 00:08:40 -0800249 init_page_count(virt_to_page(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 free_page(p);
251 totalram_pages++;
252 }
Paul Mundt2de212e2007-06-06 12:09:54 +0900253 printk("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255#endif
Paul Mundt33d63bd2007-06-07 11:32:52 +0900256
257#ifdef CONFIG_MEMORY_HOTPLUG
258void online_page(struct page *page)
259{
260 ClearPageReserved(page);
261 init_page_count(page);
262 __free_page(page);
263 totalram_pages++;
264 num_physpages++;
265}
266
267int arch_add_memory(int nid, u64 start, u64 size)
268{
269 pg_data_t *pgdat;
270 unsigned long start_pfn = start >> PAGE_SHIFT;
271 unsigned long nr_pages = size >> PAGE_SHIFT;
272 int ret;
273
274 pgdat = NODE_DATA(nid);
275
276 /* We only have ZONE_NORMAL, so this is easy.. */
277 ret = __add_pages(pgdat->node_zones + ZONE_NORMAL, start_pfn, nr_pages);
278 if (unlikely(ret))
279 printk("%s: Failed, __add_pages() == %d\n", __FUNCTION__, ret);
280
281 return ret;
282}
283EXPORT_SYMBOL_GPL(arch_add_memory);
284
Paul Mundt357d5942007-06-11 15:32:07 +0900285#ifdef CONFIG_NUMA
Paul Mundt33d63bd2007-06-07 11:32:52 +0900286int memory_add_physaddr_to_nid(u64 addr)
287{
288 /* Node 0 for now.. */
289 return 0;
290}
291EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
292#endif
Paul Mundt357d5942007-06-11 15:32:07 +0900293#endif