blob: 9c5400b02f43f98ef36fb410d1b039d0fae594fc [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 Mundt19d8f842010-05-10 15:39:05 +09005 * Copyright (C) 2002 - 2010 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/bootmem.h>
Paul Mundt2cb7ce32006-09-27 18:20:58 +090015#include <linux/proc_fs.h>
Paul Mundt27641de2007-05-14 10:48:01 +090016#include <linux/pagemap.h>
Paul Mundt01066622007-03-28 16:38:13 +090017#include <linux/percpu.h>
18#include <linux/io.h>
Paul Mundt19d8f842010-05-10 15:39:05 +090019#include <linux/lmb.h>
Paul Mundt94c28512009-10-27 17:07:45 +090020#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/tlb.h>
23#include <asm/cacheflush.h>
Paul Mundt07cbb412007-06-06 12:23:06 +090024#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/cache.h>
Paul Mundtb0f3ae02010-02-12 15:40:00 +090026#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
29pgd_t swapper_pg_dir[PTRS_PER_PGD];
Stuart Menefyc6feb612008-09-05 16:06:42 +090030
Paul Mundt19d8f842010-05-10 15:39:05 +090031void __init generic_mem_init(void)
32{
33 lmb_add(__MEMORY_START, __MEMORY_SIZE);
34}
35
Yoshinori Sato11cbb702006-12-07 18:07:27 +090036#ifdef CONFIG_MMU
Matt Fleming07cad4d2009-11-17 22:03:41 +000037static pte_t *__get_pte_phys(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 pgd_t *pgd;
Paul Mundt26ff6c12006-09-27 15:13:36 +090040 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 pmd_t *pmd;
42 pte_t *pte;
43
Stuart Menefy99a596f2006-11-21 15:38:05 +090044 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (pgd_none(*pgd)) {
46 pgd_ERROR(*pgd);
Matt Fleming07cad4d2009-11-17 22:03:41 +000047 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 }
49
Stuart Menefy99a596f2006-11-21 15:38:05 +090050 pud = pud_alloc(NULL, pgd, addr);
51 if (unlikely(!pud)) {
52 pud_ERROR(*pud);
Matt Fleming07cad4d2009-11-17 22:03:41 +000053 return NULL;
Paul Mundt26ff6c12006-09-27 15:13:36 +090054 }
55
Stuart Menefy99a596f2006-11-21 15:38:05 +090056 pmd = pmd_alloc(NULL, pud, addr);
57 if (unlikely(!pmd)) {
58 pmd_ERROR(*pmd);
Matt Fleming07cad4d2009-11-17 22:03:41 +000059 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
61
62 pte = pte_offset_kernel(pmd, addr);
Matt Fleming07cad4d2009-11-17 22:03:41 +000063 return pte;
64}
65
66static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
67{
68 pte_t *pte;
69
70 pte = __get_pte_phys(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (!pte_none(*pte)) {
72 pte_ERROR(*pte);
73 return;
74 }
75
76 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
Paul Mundt997d0032009-06-19 15:37:11 +090077 local_flush_tlb_one(get_asid(), addr);
Matt Fleming07cad4d2009-11-17 22:03:41 +000078
79 if (pgprot_val(prot) & _PAGE_WIRED)
80 tlb_wire_entry(NULL, addr, *pte);
81}
82
83static void clear_pte_phys(unsigned long addr, pgprot_t prot)
84{
85 pte_t *pte;
86
87 pte = __get_pte_phys(addr);
88
89 if (pgprot_val(prot) & _PAGE_WIRED)
90 tlb_unwire_entry();
91
92 set_pte(pte, pfn_pte(0, __pgprot(0)));
93 local_flush_tlb_one(get_asid(), addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
97{
98 unsigned long address = __fix_to_virt(idx);
99
100 if (idx >= __end_of_fixed_addresses) {
101 BUG();
102 return;
103 }
104
105 set_pte_phys(address, phys, prot);
106}
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900107
Matt Fleming07cad4d2009-11-17 22:03:41 +0000108void __clear_fixmap(enum fixed_addresses idx, pgprot_t prot)
109{
110 unsigned long address = __fix_to_virt(idx);
111
112 if (idx >= __end_of_fixed_addresses) {
113 BUG();
114 return;
115 }
116
117 clear_pte_phys(address, prot);
118}
119
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900120void __init page_table_range_init(unsigned long start, unsigned long end,
121 pgd_t *pgd_base)
122{
123 pgd_t *pgd;
124 pud_t *pud;
125 pmd_t *pmd;
Paul Mundt0906a3a2009-09-03 17:21:10 +0900126 pte_t *pte;
127 int i, j, k;
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900128 unsigned long vaddr;
129
Paul Mundt0906a3a2009-09-03 17:21:10 +0900130 vaddr = start;
131 i = __pgd_offset(vaddr);
132 j = __pud_offset(vaddr);
133 k = __pmd_offset(vaddr);
134 pgd = pgd_base + i;
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900135
Paul Mundt0906a3a2009-09-03 17:21:10 +0900136 for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
137 pud = (pud_t *)pgd;
138 for ( ; (j < PTRS_PER_PUD) && (vaddr != end); pud++, j++) {
Matt Fleming5d9b4b12009-12-13 14:38:50 +0000139#ifdef __PAGETABLE_PMD_FOLDED
Paul Mundt0906a3a2009-09-03 17:21:10 +0900140 pmd = (pmd_t *)pud;
Matt Fleming5d9b4b12009-12-13 14:38:50 +0000141#else
142 pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
143 pud_populate(&init_mm, pud, pmd);
144 pmd += k;
145#endif
Paul Mundt0906a3a2009-09-03 17:21:10 +0900146 for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) {
147 if (pmd_none(*pmd)) {
148 pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
149 pmd_populate_kernel(&init_mm, pmd, pte);
150 BUG_ON(pte != pte_offset_kernel(pmd, 0));
151 }
152 vaddr += PMD_SIZE;
153 }
154 k = 0;
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900155 }
Paul Mundt0906a3a2009-09-03 17:21:10 +0900156 j = 0;
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900157 }
158}
Yoshinori Sato11cbb702006-12-07 18:07:27 +0900159#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/*
162 * paging_init() sets up the page tables
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 */
164void __init paging_init(void)
165{
Paul Mundt2de212e2007-06-06 12:09:54 +0900166 unsigned long max_zone_pfns[MAX_NR_ZONES];
Paul Mundt0906a3a2009-09-03 17:21:10 +0900167 unsigned long vaddr, end;
Paul Mundt01066622007-03-28 16:38:13 +0900168 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Paul Mundt01066622007-03-28 16:38:13 +0900170 /* We don't need to map the kernel through the TLB, as
171 * it is permanatly mapped using P1. So clear the
172 * entire pgd. */
173 memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Stuart Menefy6e4662f2006-11-21 13:53:44 +0900175 /* Set an initial value for the MMU.TTB so we don't have to
176 * check for a null value. */
177 set_TTB(swapper_pg_dir);
178
Paul Mundtacca4f42008-11-10 20:00:45 +0900179 /*
180 * Populate the relevant portions of swapper_pg_dir so that
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900181 * we can use the fixmap entries without calling kmalloc.
Paul Mundtacca4f42008-11-10 20:00:45 +0900182 * pte's will be filled in by __set_fixmap().
183 */
184 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
Paul Mundt0906a3a2009-09-03 17:21:10 +0900185 end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
186 page_table_range_init(vaddr, end, swapper_pg_dir);
Paul Mundtacca4f42008-11-10 20:00:45 +0900187
188 kmap_coherent_init();
Stuart Menefy2adb4e12007-11-30 17:59:55 +0900189
Paul Mundt2de212e2007-06-06 12:09:54 +0900190 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
191
Paul Mundt01066622007-03-28 16:38:13 +0900192 for_each_online_node(nid) {
193 pg_data_t *pgdat = NODE_DATA(nid);
Paul Mundt01066622007-03-28 16:38:13 +0900194 unsigned long low, start_pfn;
195
Johannes Weiner3560e242008-07-23 21:28:09 -0700196 start_pfn = pgdat->bdata->node_min_pfn;
Paul Mundt01066622007-03-28 16:38:13 +0900197 low = pgdat->bdata->node_low_pfn;
198
Paul Mundt2de212e2007-06-06 12:09:54 +0900199 if (max_zone_pfns[ZONE_NORMAL] < low)
200 max_zone_pfns[ZONE_NORMAL] = low;
Paul Mundt01066622007-03-28 16:38:13 +0900201
202 printk("Node %u: start_pfn = 0x%lx, low = 0x%lx\n",
203 nid, start_pfn, low);
Paul Mundt01066622007-03-28 16:38:13 +0900204 }
Paul Mundt2de212e2007-06-06 12:09:54 +0900205
206 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Paul Mundt94c28512009-10-27 17:07:45 +0900209/*
210 * Early initialization for any I/O MMUs we might have.
211 */
212static void __init iommu_init(void)
213{
214 no_iommu_init();
215}
216
Paul Mundtd9b94872010-01-18 21:08:32 +0900217unsigned int mem_init_done = 0;
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219void __init mem_init(void)
220{
Paul Mundtdfbb9042007-05-23 17:48:36 +0900221 int codesize, datasize, initsize;
Paul Mundt01066622007-03-28 16:38:13 +0900222 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Paul Mundt94c28512009-10-27 17:07:45 +0900224 iommu_init();
225
Paul Mundt2de212e2007-06-06 12:09:54 +0900226 num_physpages = 0;
227 high_memory = NULL;
228
Paul Mundt01066622007-03-28 16:38:13 +0900229 for_each_online_node(nid) {
230 pg_data_t *pgdat = NODE_DATA(nid);
231 unsigned long node_pages = 0;
232 void *node_high_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Paul Mundt01066622007-03-28 16:38:13 +0900234 num_physpages += pgdat->node_present_pages;
235
236 if (pgdat->node_spanned_pages)
237 node_pages = free_all_bootmem_node(pgdat);
238
239 totalram_pages += node_pages;
240
Paul Mundt2de212e2007-06-06 12:09:54 +0900241 node_high_memory = (void *)__va((pgdat->node_start_pfn +
242 pgdat->node_spanned_pages) <<
243 PAGE_SHIFT);
Paul Mundt01066622007-03-28 16:38:13 +0900244 if (node_high_memory > high_memory)
245 high_memory = node_high_memory;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Paul Mundt37443ef2009-08-15 12:29:49 +0900248 /* Set this up early, so we can take care of the zero page */
249 cpu_cache_init();
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* clear the zero-page */
252 memset(empty_zero_page, 0, PAGE_SIZE);
253 __flush_wback_region(empty_zero_page, PAGE_SIZE);
254
Paul Mundt35f99c02010-01-20 18:48:17 +0900255 vsyscall_init();
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 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
Paul Mundtdfbb9042007-05-23 17:48:36 +0900262 "%dk data, %dk init)\n",
Geert Uytterhoevencc013a82009-09-21 17:02:36 -0700263 nr_free_pages() << (PAGE_SHIFT-10),
Paul Mundt2de212e2007-06-06 12:09:54 +0900264 num_physpages << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 codesize >> 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 datasize >> 10,
267 initsize >> 10);
268
Paul Mundt35f99c02010-01-20 18:48:17 +0900269 printk(KERN_INFO "virtual kernel memory layout:\n"
270 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
271#ifdef CONFIG_HIGHMEM
272 " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
273#endif
274 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
Paul Mundt3125ee72010-01-21 15:54:31 +0900275 " lowmem : 0x%08lx - 0x%08lx (%4ld MB) (cached)\n"
Paul Mundtb0f3ae02010-02-12 15:40:00 +0900276#ifdef CONFIG_UNCACHED_MAPPING
Paul Mundt3125ee72010-01-21 15:54:31 +0900277 " : 0x%08lx - 0x%08lx (%4ld MB) (uncached)\n"
Paul Mundtb0f3ae02010-02-12 15:40:00 +0900278#endif
Paul Mundt35f99c02010-01-20 18:48:17 +0900279 " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
280 " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
281 " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
282 FIXADDR_START, FIXADDR_TOP,
283 (FIXADDR_TOP - FIXADDR_START) >> 10,
284
285#ifdef CONFIG_HIGHMEM
286 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
287 (LAST_PKMAP*PAGE_SIZE) >> 10,
288#endif
289
290 (unsigned long)VMALLOC_START, VMALLOC_END,
291 (VMALLOC_END - VMALLOC_START) >> 20,
292
293 (unsigned long)memory_start, (unsigned long)high_memory,
294 ((unsigned long)high_memory - (unsigned long)memory_start) >> 20,
295
Paul Mundtb0f3ae02010-02-12 15:40:00 +0900296#ifdef CONFIG_UNCACHED_MAPPING
Paul Mundt9edef282010-02-17 16:28:00 +0900297 uncached_start, uncached_end, uncached_size >> 20,
Paul Mundtb0f3ae02010-02-12 15:40:00 +0900298#endif
Paul Mundt3125ee72010-01-21 15:54:31 +0900299
Paul Mundt35f99c02010-01-20 18:48:17 +0900300 (unsigned long)&__init_begin, (unsigned long)&__init_end,
301 ((unsigned long)&__init_end -
302 (unsigned long)&__init_begin) >> 10,
303
304 (unsigned long)&_etext, (unsigned long)&_edata,
305 ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
306
307 (unsigned long)&_text, (unsigned long)&_etext,
308 ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
Paul Mundtd9b94872010-01-18 21:08:32 +0900309
310 mem_init_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313void free_initmem(void)
314{
315 unsigned long addr;
Paul Mundt65463b72005-11-07 00:58:24 -0800316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 addr = (unsigned long)(&__init_begin);
318 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
319 ClearPageReserved(virt_to_page(addr));
Nick Piggin7835e982006-03-22 00:08:40 -0800320 init_page_count(virt_to_page(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 free_page(addr);
322 totalram_pages++;
323 }
Paul Mundt07cbb412007-06-06 12:23:06 +0900324 printk("Freeing unused kernel memory: %ldk freed\n",
325 ((unsigned long)&__init_end -
326 (unsigned long)&__init_begin) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329#ifdef CONFIG_BLK_DEV_INITRD
330void free_initrd_mem(unsigned long start, unsigned long end)
331{
332 unsigned long p;
333 for (p = start; p < end; p += PAGE_SIZE) {
334 ClearPageReserved(virt_to_page(p));
Nick Piggin7835e982006-03-22 00:08:40 -0800335 init_page_count(virt_to_page(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 free_page(p);
337 totalram_pages++;
338 }
Paul Mundt2de212e2007-06-06 12:09:54 +0900339 printk("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341#endif
Paul Mundt33d63bd2007-06-07 11:32:52 +0900342
343#ifdef CONFIG_MEMORY_HOTPLUG
Paul Mundt33d63bd2007-06-07 11:32:52 +0900344int arch_add_memory(int nid, u64 start, u64 size)
345{
346 pg_data_t *pgdat;
347 unsigned long start_pfn = start >> PAGE_SHIFT;
348 unsigned long nr_pages = size >> PAGE_SHIFT;
349 int ret;
350
351 pgdat = NODE_DATA(nid);
352
353 /* We only have ZONE_NORMAL, so this is easy.. */
Gary Hadec04fc582009-01-06 14:39:14 -0800354 ret = __add_pages(nid, pgdat->node_zones + ZONE_NORMAL,
355 start_pfn, nr_pages);
Paul Mundt33d63bd2007-06-07 11:32:52 +0900356 if (unlikely(ret))
Harvey Harrison866e6b92008-03-04 15:23:47 -0800357 printk("%s: Failed, __add_pages() == %d\n", __func__, ret);
Paul Mundt33d63bd2007-06-07 11:32:52 +0900358
359 return ret;
360}
361EXPORT_SYMBOL_GPL(arch_add_memory);
362
Paul Mundt357d5942007-06-11 15:32:07 +0900363#ifdef CONFIG_NUMA
Paul Mundt33d63bd2007-06-07 11:32:52 +0900364int memory_add_physaddr_to_nid(u64 addr)
365{
366 /* Node 0 for now.. */
367 return 0;
368}
369EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
370#endif
Matt Fleming1f69b6a2009-10-06 21:22:25 +0000371
Paul Mundt3159e7d2008-09-05 15:39:12 +0900372#endif /* CONFIG_MEMORY_HOTPLUG */