blob: 08e280d7cc7e929cd8946f99988c0643d3a05a8c [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>
Paul Mundt94c28512009-10-27 17:07:45 +090018#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/tlb.h>
21#include <asm/cacheflush.h>
Paul Mundt07cbb412007-06-06 12:23:06 +090022#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/cache.h>
Paul Mundtb0f3ae02010-02-12 15:40:00 +090024#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
27pgd_t swapper_pg_dir[PTRS_PER_PGD];
Stuart Menefyc6feb612008-09-05 16:06:42 +090028
Yoshinori Sato11cbb702006-12-07 18:07:27 +090029#ifdef CONFIG_MMU
Matt Fleming07cad4d2009-11-17 22:03:41 +000030static pte_t *__get_pte_phys(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 pgd_t *pgd;
Paul Mundt26ff6c12006-09-27 15:13:36 +090033 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 pmd_t *pmd;
35 pte_t *pte;
36
Stuart Menefy99a596f2006-11-21 15:38:05 +090037 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 if (pgd_none(*pgd)) {
39 pgd_ERROR(*pgd);
Matt Fleming07cad4d2009-11-17 22:03:41 +000040 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 }
42
Stuart Menefy99a596f2006-11-21 15:38:05 +090043 pud = pud_alloc(NULL, pgd, addr);
44 if (unlikely(!pud)) {
45 pud_ERROR(*pud);
Matt Fleming07cad4d2009-11-17 22:03:41 +000046 return NULL;
Paul Mundt26ff6c12006-09-27 15:13:36 +090047 }
48
Stuart Menefy99a596f2006-11-21 15:38:05 +090049 pmd = pmd_alloc(NULL, pud, addr);
50 if (unlikely(!pmd)) {
51 pmd_ERROR(*pmd);
Matt Fleming07cad4d2009-11-17 22:03:41 +000052 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 }
54
55 pte = pte_offset_kernel(pmd, addr);
Matt Fleming07cad4d2009-11-17 22:03:41 +000056 return pte;
57}
58
59static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
60{
61 pte_t *pte;
62
63 pte = __get_pte_phys(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (!pte_none(*pte)) {
65 pte_ERROR(*pte);
66 return;
67 }
68
69 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
Paul Mundt997d0032009-06-19 15:37:11 +090070 local_flush_tlb_one(get_asid(), addr);
Matt Fleming07cad4d2009-11-17 22:03:41 +000071
72 if (pgprot_val(prot) & _PAGE_WIRED)
73 tlb_wire_entry(NULL, addr, *pte);
74}
75
76static void clear_pte_phys(unsigned long addr, pgprot_t prot)
77{
78 pte_t *pte;
79
80 pte = __get_pte_phys(addr);
81
82 if (pgprot_val(prot) & _PAGE_WIRED)
83 tlb_unwire_entry();
84
85 set_pte(pte, pfn_pte(0, __pgprot(0)));
86 local_flush_tlb_one(get_asid(), addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
90{
91 unsigned long address = __fix_to_virt(idx);
92
93 if (idx >= __end_of_fixed_addresses) {
94 BUG();
95 return;
96 }
97
98 set_pte_phys(address, phys, prot);
99}
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900100
Matt Fleming07cad4d2009-11-17 22:03:41 +0000101void __clear_fixmap(enum fixed_addresses idx, pgprot_t prot)
102{
103 unsigned long address = __fix_to_virt(idx);
104
105 if (idx >= __end_of_fixed_addresses) {
106 BUG();
107 return;
108 }
109
110 clear_pte_phys(address, prot);
111}
112
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900113void __init page_table_range_init(unsigned long start, unsigned long end,
114 pgd_t *pgd_base)
115{
116 pgd_t *pgd;
117 pud_t *pud;
118 pmd_t *pmd;
Paul Mundt0906a3a2009-09-03 17:21:10 +0900119 pte_t *pte;
120 int i, j, k;
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900121 unsigned long vaddr;
122
Paul Mundt0906a3a2009-09-03 17:21:10 +0900123 vaddr = start;
124 i = __pgd_offset(vaddr);
125 j = __pud_offset(vaddr);
126 k = __pmd_offset(vaddr);
127 pgd = pgd_base + i;
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900128
Paul Mundt0906a3a2009-09-03 17:21:10 +0900129 for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
130 pud = (pud_t *)pgd;
131 for ( ; (j < PTRS_PER_PUD) && (vaddr != end); pud++, j++) {
Matt Fleming5d9b4b12009-12-13 14:38:50 +0000132#ifdef __PAGETABLE_PMD_FOLDED
Paul Mundt0906a3a2009-09-03 17:21:10 +0900133 pmd = (pmd_t *)pud;
Matt Fleming5d9b4b12009-12-13 14:38:50 +0000134#else
135 pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
136 pud_populate(&init_mm, pud, pmd);
137 pmd += k;
138#endif
Paul Mundt0906a3a2009-09-03 17:21:10 +0900139 for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) {
140 if (pmd_none(*pmd)) {
141 pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
142 pmd_populate_kernel(&init_mm, pmd, pte);
143 BUG_ON(pte != pte_offset_kernel(pmd, 0));
144 }
145 vaddr += PMD_SIZE;
146 }
147 k = 0;
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900148 }
Paul Mundt0906a3a2009-09-03 17:21:10 +0900149 j = 0;
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900150 }
151}
Yoshinori Sato11cbb702006-12-07 18:07:27 +0900152#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
155 * paging_init() sets up the page tables
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
157void __init paging_init(void)
158{
Paul Mundt2de212e2007-06-06 12:09:54 +0900159 unsigned long max_zone_pfns[MAX_NR_ZONES];
Paul Mundt0906a3a2009-09-03 17:21:10 +0900160 unsigned long vaddr, end;
Paul Mundt01066622007-03-28 16:38:13 +0900161 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Paul Mundt01066622007-03-28 16:38:13 +0900163 /* We don't need to map the kernel through the TLB, as
164 * it is permanatly mapped using P1. So clear the
165 * entire pgd. */
166 memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Stuart Menefy6e4662f2006-11-21 13:53:44 +0900168 /* Set an initial value for the MMU.TTB so we don't have to
169 * check for a null value. */
170 set_TTB(swapper_pg_dir);
171
Paul Mundtacca4f42008-11-10 20:00:45 +0900172 /*
173 * Populate the relevant portions of swapper_pg_dir so that
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900174 * we can use the fixmap entries without calling kmalloc.
Paul Mundtacca4f42008-11-10 20:00:45 +0900175 * pte's will be filled in by __set_fixmap().
176 */
177 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
Paul Mundt0906a3a2009-09-03 17:21:10 +0900178 end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
179 page_table_range_init(vaddr, end, swapper_pg_dir);
Paul Mundtacca4f42008-11-10 20:00:45 +0900180
181 kmap_coherent_init();
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900182
Paul Mundt2de212e2007-06-06 12:09:54 +0900183 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
184
Paul Mundt01066622007-03-28 16:38:13 +0900185 for_each_online_node(nid) {
186 pg_data_t *pgdat = NODE_DATA(nid);
Paul Mundt01066622007-03-28 16:38:13 +0900187 unsigned long low, start_pfn;
188
Johannes Weiner3560e242008-07-23 21:28:09 -0700189 start_pfn = pgdat->bdata->node_min_pfn;
Paul Mundt01066622007-03-28 16:38:13 +0900190 low = pgdat->bdata->node_low_pfn;
191
Paul Mundt2de212e2007-06-06 12:09:54 +0900192 if (max_zone_pfns[ZONE_NORMAL] < low)
193 max_zone_pfns[ZONE_NORMAL] = low;
Paul Mundt01066622007-03-28 16:38:13 +0900194
195 printk("Node %u: start_pfn = 0x%lx, low = 0x%lx\n",
196 nid, start_pfn, low);
Paul Mundt01066622007-03-28 16:38:13 +0900197 }
Paul Mundt2de212e2007-06-06 12:09:54 +0900198
199 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Paul Mundt94c28512009-10-27 17:07:45 +0900202/*
203 * Early initialization for any I/O MMUs we might have.
204 */
205static void __init iommu_init(void)
206{
207 no_iommu_init();
208}
209
Paul Mundtd9b94872010-01-18 21:08:32 +0900210unsigned int mem_init_done = 0;
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212void __init mem_init(void)
213{
Paul Mundtdfbb9042007-05-23 17:48:36 +0900214 int codesize, datasize, initsize;
Paul Mundt01066622007-03-28 16:38:13 +0900215 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Paul Mundt94c28512009-10-27 17:07:45 +0900217 iommu_init();
218
Paul Mundt2de212e2007-06-06 12:09:54 +0900219 num_physpages = 0;
220 high_memory = NULL;
221
Paul Mundt01066622007-03-28 16:38:13 +0900222 for_each_online_node(nid) {
223 pg_data_t *pgdat = NODE_DATA(nid);
224 unsigned long node_pages = 0;
225 void *node_high_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Paul Mundt01066622007-03-28 16:38:13 +0900227 num_physpages += pgdat->node_present_pages;
228
229 if (pgdat->node_spanned_pages)
230 node_pages = free_all_bootmem_node(pgdat);
231
232 totalram_pages += node_pages;
233
Paul Mundt2de212e2007-06-06 12:09:54 +0900234 node_high_memory = (void *)__va((pgdat->node_start_pfn +
235 pgdat->node_spanned_pages) <<
236 PAGE_SHIFT);
Paul Mundt01066622007-03-28 16:38:13 +0900237 if (node_high_memory > high_memory)
238 high_memory = node_high_memory;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Paul Mundt37443ef2009-08-15 12:29:49 +0900241 /* Set this up early, so we can take care of the zero page */
242 cpu_cache_init();
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* clear the zero-page */
245 memset(empty_zero_page, 0, PAGE_SIZE);
246 __flush_wback_region(empty_zero_page, PAGE_SIZE);
247
Paul Mundt9edef282010-02-17 16:28:00 +0900248 uncached_init();
Paul Mundt35f99c02010-01-20 18:48:17 +0900249 vsyscall_init();
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 codesize = (unsigned long) &_etext - (unsigned long) &_text;
252 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
253 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
254
Paul Mundt2cb7ce32006-09-27 18:20:58 +0900255 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
Paul Mundtdfbb9042007-05-23 17:48:36 +0900256 "%dk data, %dk init)\n",
Geert Uytterhoevencc013a82009-09-21 17:02:36 -0700257 nr_free_pages() << (PAGE_SHIFT-10),
Paul Mundt2de212e2007-06-06 12:09:54 +0900258 num_physpages << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 codesize >> 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 datasize >> 10,
261 initsize >> 10);
262
Paul Mundt35f99c02010-01-20 18:48:17 +0900263 printk(KERN_INFO "virtual kernel memory layout:\n"
264 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
265#ifdef CONFIG_HIGHMEM
266 " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
267#endif
268 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
Paul Mundt3125ee72010-01-21 15:54:31 +0900269 " lowmem : 0x%08lx - 0x%08lx (%4ld MB) (cached)\n"
Paul Mundtb0f3ae02010-02-12 15:40:00 +0900270#ifdef CONFIG_UNCACHED_MAPPING
Paul Mundt3125ee72010-01-21 15:54:31 +0900271 " : 0x%08lx - 0x%08lx (%4ld MB) (uncached)\n"
Paul Mundtb0f3ae02010-02-12 15:40:00 +0900272#endif
Paul Mundt35f99c02010-01-20 18:48:17 +0900273 " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
274 " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
275 " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
276 FIXADDR_START, FIXADDR_TOP,
277 (FIXADDR_TOP - FIXADDR_START) >> 10,
278
279#ifdef CONFIG_HIGHMEM
280 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
281 (LAST_PKMAP*PAGE_SIZE) >> 10,
282#endif
283
284 (unsigned long)VMALLOC_START, VMALLOC_END,
285 (VMALLOC_END - VMALLOC_START) >> 20,
286
287 (unsigned long)memory_start, (unsigned long)high_memory,
288 ((unsigned long)high_memory - (unsigned long)memory_start) >> 20,
289
Paul Mundtb0f3ae02010-02-12 15:40:00 +0900290#ifdef CONFIG_UNCACHED_MAPPING
Paul Mundt9edef282010-02-17 16:28:00 +0900291 uncached_start, uncached_end, uncached_size >> 20,
Paul Mundtb0f3ae02010-02-12 15:40:00 +0900292#endif
Paul Mundt3125ee72010-01-21 15:54:31 +0900293
Paul Mundt35f99c02010-01-20 18:48:17 +0900294 (unsigned long)&__init_begin, (unsigned long)&__init_end,
295 ((unsigned long)&__init_end -
296 (unsigned long)&__init_begin) >> 10,
297
298 (unsigned long)&_etext, (unsigned long)&_edata,
299 ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
300
301 (unsigned long)&_text, (unsigned long)&_etext,
302 ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
Paul Mundtd9b94872010-01-18 21:08:32 +0900303
304 mem_init_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307void free_initmem(void)
308{
309 unsigned long addr;
Paul Mundt65463b72005-11-07 00:58:24 -0800310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 addr = (unsigned long)(&__init_begin);
312 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
313 ClearPageReserved(virt_to_page(addr));
Nick Piggin7835e982006-03-22 00:08:40 -0800314 init_page_count(virt_to_page(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 free_page(addr);
316 totalram_pages++;
317 }
Paul Mundt07cbb412007-06-06 12:23:06 +0900318 printk("Freeing unused kernel memory: %ldk freed\n",
319 ((unsigned long)&__init_end -
320 (unsigned long)&__init_begin) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323#ifdef CONFIG_BLK_DEV_INITRD
324void free_initrd_mem(unsigned long start, unsigned long end)
325{
326 unsigned long p;
327 for (p = start; p < end; p += PAGE_SIZE) {
328 ClearPageReserved(virt_to_page(p));
Nick Piggin7835e982006-03-22 00:08:40 -0800329 init_page_count(virt_to_page(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 free_page(p);
331 totalram_pages++;
332 }
Paul Mundt2de212e2007-06-06 12:09:54 +0900333 printk("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335#endif
Paul Mundt33d63bd2007-06-07 11:32:52 +0900336
337#ifdef CONFIG_MEMORY_HOTPLUG
Paul Mundt33d63bd2007-06-07 11:32:52 +0900338int arch_add_memory(int nid, u64 start, u64 size)
339{
340 pg_data_t *pgdat;
341 unsigned long start_pfn = start >> PAGE_SHIFT;
342 unsigned long nr_pages = size >> PAGE_SHIFT;
343 int ret;
344
345 pgdat = NODE_DATA(nid);
346
347 /* We only have ZONE_NORMAL, so this is easy.. */
Gary Hadec04fc582009-01-06 14:39:14 -0800348 ret = __add_pages(nid, pgdat->node_zones + ZONE_NORMAL,
349 start_pfn, nr_pages);
Paul Mundt33d63bd2007-06-07 11:32:52 +0900350 if (unlikely(ret))
Harvey Harrison866e6b92008-03-04 15:23:47 -0800351 printk("%s: Failed, __add_pages() == %d\n", __func__, ret);
Paul Mundt33d63bd2007-06-07 11:32:52 +0900352
353 return ret;
354}
355EXPORT_SYMBOL_GPL(arch_add_memory);
356
Paul Mundt357d5942007-06-11 15:32:07 +0900357#ifdef CONFIG_NUMA
Paul Mundt33d63bd2007-06-07 11:32:52 +0900358int memory_add_physaddr_to_nid(u64 addr)
359{
360 /* Node 0 for now.. */
361 return 0;
362}
363EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
364#endif
Matt Fleming1f69b6a2009-10-06 21:22:25 +0000365
Paul Mundt3159e7d2008-09-05 15:39:12 +0900366#endif /* CONFIG_MEMORY_HOTPLUG */