blob: cc60d10cf8f7b446a16e1567966f85d4f538dc28 [file] [log] [blame]
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001/*
2 * Copyright (C) 2004-2006 Atmel Corporation
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/kernel.h>
10#include <linux/mm.h>
11#include <linux/swap.h>
12#include <linux/init.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070013#include <linux/mmzone.h>
Haavard Skinnemoen8bd89742008-06-08 23:28:45 +020014#include <linux/module.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070015#include <linux/bootmem.h>
16#include <linux/pagemap.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070017#include <linux/nodemask.h>
18
19#include <asm/page.h>
20#include <asm/mmu_context.h>
21#include <asm/tlb.h>
22#include <asm/io.h>
23#include <asm/dma.h>
24#include <asm/setup.h>
25#include <asm/sections.h>
26
27DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
28
Tim Abbottabe1ee32009-09-20 18:14:15 -040029pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_data;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070030
31struct page *empty_zero_page;
Haavard Skinnemoen8bd89742008-06-08 23:28:45 +020032EXPORT_SYMBOL(empty_zero_page);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070033
34/*
35 * Cache of MMU context last used.
36 */
37unsigned long mmu_context_cache = NO_CONTEXT;
38
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070039/*
40 * paging_init() sets up the page tables
41 *
42 * This routine also unmaps the page at virtual kernel address 0, so
43 * that we can trap those pesky NULL-reference errors in the kernel.
44 */
45void __init paging_init(void)
46{
47 extern unsigned long _evba;
48 void *zero_page;
49 int nid;
50
51 /*
52 * Make sure we can handle exceptions before enabling
53 * paging. Not that we should ever _get_ any exceptions this
54 * early, but you never know...
55 */
56 printk("Exception vectors start at %p\n", &_evba);
57 sysreg_write(EVBA, (unsigned long)&_evba);
58
59 /*
60 * Since we are ready to handle exceptions now, we should let
61 * the CPU generate them...
62 */
63 __asm__ __volatile__ ("csrf %0" : : "i"(SR_EM_BIT));
64
65 /*
66 * Allocate the zero page. The allocator will panic if it
67 * can't satisfy the request, so no need to check.
68 */
69 zero_page = alloc_bootmem_low_pages_node(NODE_DATA(0),
70 PAGE_SIZE);
71
Haavard Skinnemoenebe74592008-01-14 22:16:50 +010072 sysreg_write(PTBR, (unsigned long)swapper_pg_dir);
73 enable_mmu();
74 printk ("CPU: Paging enabled\n");
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070075
76 for_each_online_node(nid) {
77 pg_data_t *pgdat = NODE_DATA(nid);
78 unsigned long zones_size[MAX_NR_ZONES];
79 unsigned long low, start_pfn;
80
Johannes Weiner3560e242008-07-23 21:28:09 -070081 start_pfn = pgdat->bdata->node_min_pfn;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070082 low = pgdat->bdata->node_low_pfn;
83
84 memset(zones_size, 0, sizeof(zones_size));
85 zones_size[ZONE_NORMAL] = low - start_pfn;
86
87 printk("Node %u: start_pfn = 0x%lx, low = 0x%lx\n",
88 nid, start_pfn, low);
89
Johannes Weiner9109fb72008-07-23 21:27:20 -070090 free_area_init_node(nid, zones_size, start_pfn, NULL);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070091
92 printk("Node %u: mem_map starts at %p\n",
93 pgdat->node_id, pgdat->node_mem_map);
94 }
95
96 mem_map = NODE_DATA(0)->node_mem_map;
97
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070098 empty_zero_page = virt_to_page(zero_page);
99 flush_dcache_page(empty_zero_page);
100}
101
102void __init mem_init(void)
103{
104 int codesize, reservedpages, datasize, initsize;
105 int nid, i;
106
107 reservedpages = 0;
108 high_memory = NULL;
109
110 /* this will put all low memory onto the freelists */
111 for_each_online_node(nid) {
112 pg_data_t *pgdat = NODE_DATA(nid);
113 unsigned long node_pages = 0;
114 void *node_high_memory;
115
116 num_physpages += pgdat->node_present_pages;
117
118 if (pgdat->node_spanned_pages != 0)
119 node_pages = free_all_bootmem_node(pgdat);
120
121 totalram_pages += node_pages;
122
123 for (i = 0; i < node_pages; i++)
124 if (PageReserved(pgdat->node_mem_map + i))
125 reservedpages++;
126
127 node_high_memory = (void *)((pgdat->node_start_pfn
128 + pgdat->node_spanned_pages)
129 << PAGE_SHIFT);
130 if (node_high_memory > high_memory)
131 high_memory = node_high_memory;
132 }
133
134 max_mapnr = MAP_NR(high_memory);
135
136 codesize = (unsigned long)_etext - (unsigned long)_text;
137 datasize = (unsigned long)_edata - (unsigned long)_data;
138 initsize = (unsigned long)__init_end - (unsigned long)__init_begin;
139
140 printk ("Memory: %luk/%luk available (%dk kernel code, "
141 "%dk reserved, %dk data, %dk init)\n",
142 (unsigned long)nr_free_pages() << (PAGE_SHIFT - 10),
143 totalram_pages << (PAGE_SHIFT - 10),
144 codesize >> 10,
145 reservedpages << (PAGE_SHIFT - 10),
146 datasize >> 10,
147 initsize >> 10);
148}
149
150static inline void free_area(unsigned long addr, unsigned long end, char *s)
151{
152 unsigned int size = (end - addr) >> 10;
153
154 for (; addr < end; addr += PAGE_SIZE) {
155 struct page *page = virt_to_page(addr);
156 ClearPageReserved(page);
157 init_page_count(page);
158 free_page(addr);
159 totalram_pages++;
160 }
161
162 if (size && s)
163 printk(KERN_INFO "Freeing %s memory: %dK (%lx - %lx)\n",
164 s, size, end - (size << 10), end);
165}
166
167void free_initmem(void)
168{
169 free_area((unsigned long)__init_begin, (unsigned long)__init_end,
170 "init");
171}
172
173#ifdef CONFIG_BLK_DEV_INITRD
174
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700175void free_initrd_mem(unsigned long start, unsigned long end)
176{
Robert P. J. Day193fdd12007-09-19 08:43:42 -0400177 free_area(start, end, "initrd");
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700178}
179
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700180#endif