blob: a140da9732db82204bfe48f91655bece10abda19 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 1994 - 2000 Ralf Baechle
7 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
8 * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
9 * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved.
10 */
11#include <linux/config.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/signal.h>
15#include <linux/sched.h>
16#include <linux/kernel.h>
17#include <linux/errno.h>
18#include <linux/string.h>
19#include <linux/types.h>
20#include <linux/pagemap.h>
21#include <linux/ptrace.h>
22#include <linux/mman.h>
23#include <linux/mm.h>
24#include <linux/bootmem.h>
25#include <linux/highmem.h>
26#include <linux/swap.h>
Daniel Jacobowitz3d503752005-01-20 19:59:54 -050027#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/bootinfo.h>
30#include <asm/cachectl.h>
31#include <asm/cpu.h>
32#include <asm/dma.h>
33#include <asm/mmu_context.h>
34#include <asm/sections.h>
35#include <asm/pgtable.h>
36#include <asm/pgalloc.h>
37#include <asm/tlb.h>
38
39DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
40
41unsigned long highstart_pfn, highend_pfn;
42
43/*
44 * We have up to 8 empty zeroed pages so we can map one of the right colour
45 * when needed. This is necessary only on R4000 / R4400 SC and MC versions
46 * where we have to avoid VCED / VECI exceptions for good performance at
47 * any price. Since page is never written to after the initialization we
48 * don't have to care about aliases on other CPUs.
49 */
50unsigned long empty_zero_page, zero_page_mask;
51
52/*
53 * Not static inline because used by IP27 special magic initialization code
54 */
55unsigned long setup_zero_pages(void)
56{
Nick Piggin8dfcc9b2006-03-22 00:08:05 -080057 unsigned int order;
58 unsigned long size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 struct page *page;
60
61 if (cpu_has_vce)
62 order = 3;
63 else
64 order = 0;
65
66 empty_zero_page = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
67 if (!empty_zero_page)
68 panic("Oh boy, that early out of memory?");
69
70 page = virt_to_page(empty_zero_page);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -080071 split_page(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 while (page < virt_to_page(empty_zero_page + (PAGE_SIZE << order))) {
Hugh Dickins68352e62005-12-12 00:37:23 -080073 SetPageReserved(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 page++;
75 }
76
77 size = PAGE_SIZE << order;
78 zero_page_mask = (size - 1) & PAGE_MASK;
79
80 return 1UL << order;
81}
82
83#ifdef CONFIG_HIGHMEM
84pte_t *kmap_pte;
85pgprot_t kmap_prot;
86
87#define kmap_get_fixmap_pte(vaddr) \
Ralf Baechlec6e8b582005-02-10 12:19:59 +000088 pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr)), (vaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90static void __init kmap_init(void)
91{
92 unsigned long kmap_vstart;
93
94 /* cache the first kmap pte */
95 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
96 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
97
98 kmap_prot = PAGE_KERNEL;
99}
100
Ralf Baechle84fd0892005-02-07 16:13:07 +0000101#ifdef CONFIG_32BIT
102void __init fixrange_init(unsigned long start, unsigned long end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 pgd_t *pgd_base)
104{
105 pgd_t *pgd;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000106 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 pmd_t *pmd;
108 pte_t *pte;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000109 int i, j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 unsigned long vaddr;
111
112 vaddr = start;
113 i = __pgd_offset(vaddr);
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000114 j = __pud_offset(vaddr);
115 k = __pmd_offset(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 pgd = pgd_base + i;
117
118 for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000119 pud = (pud_t *)pgd;
120 for ( ; (j < PTRS_PER_PUD) && (vaddr != end); pud++, j++) {
121 pmd = (pmd_t *)pud;
122 for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) {
123 if (pmd_none(*pmd)) {
124 pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
125 set_pmd(pmd, __pmd(pte));
126 if (pte != pte_offset_kernel(pmd, 0))
127 BUG();
128 }
129 vaddr += PMD_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000131 k = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133 j = 0;
134 }
135}
Ralf Baechledbc57162005-10-29 19:32:38 +0100136#endif /* CONFIG_32BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#endif /* CONFIG_HIGHMEM */
138
Yoichi Yuasab4819b52005-06-25 14:54:31 -0700139#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140extern void pagetable_init(void);
141
142void __init paging_init(void)
143{
144 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
145 unsigned long max_dma, high, low;
146
147 pagetable_init();
148
149#ifdef CONFIG_HIGHMEM
150 kmap_init();
151#endif
152
153 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
154 low = max_low_pfn;
155 high = highend_pfn;
156
157#ifdef CONFIG_ISA
158 if (low < max_dma)
159 zones_size[ZONE_DMA] = low;
160 else {
161 zones_size[ZONE_DMA] = max_dma;
162 zones_size[ZONE_NORMAL] = low - max_dma;
163 }
164#else
165 zones_size[ZONE_DMA] = low;
166#endif
167#ifdef CONFIG_HIGHMEM
168 if (cpu_has_dc_aliases) {
169 printk(KERN_WARNING "This processor doesn't support highmem.");
170 if (high - low)
171 printk(" %ldk highmem ignored", high - low);
172 printk("\n");
173 } else
174 zones_size[ZONE_HIGHMEM] = high - low;
175#endif
176
177 free_area_init(zones_size);
178}
179
180#define PFN_UP(x) (((x) + PAGE_SIZE - 1) >> PAGE_SHIFT)
181#define PFN_DOWN(x) ((x) >> PAGE_SHIFT)
182
183static inline int page_is_ram(unsigned long pagenr)
184{
185 int i;
186
187 for (i = 0; i < boot_mem_map.nr_map; i++) {
188 unsigned long addr, end;
189
190 if (boot_mem_map.map[i].type != BOOT_MEM_RAM)
191 /* not usable memory */
192 continue;
193
194 addr = PFN_UP(boot_mem_map.map[i].addr);
195 end = PFN_DOWN(boot_mem_map.map[i].addr +
196 boot_mem_map.map[i].size);
197
198 if (pagenr >= addr && pagenr < end)
199 return 1;
200 }
201
202 return 0;
203}
204
Daniel Jacobowitz3d503752005-01-20 19:59:54 -0500205static struct kcore_list kcore_mem, kcore_vmalloc;
206#ifdef CONFIG_64BIT
207static struct kcore_list kcore_kseg0;
208#endif
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210void __init mem_init(void)
211{
212 unsigned long codesize, reservedpages, datasize, initsize;
213 unsigned long tmp, ram;
214
215#ifdef CONFIG_HIGHMEM
216#ifdef CONFIG_DISCONTIGMEM
217#error "CONFIG_HIGHMEM and CONFIG_DISCONTIGMEM dont work together yet"
218#endif
219 max_mapnr = num_physpages = highend_pfn;
220#else
221 max_mapnr = num_physpages = max_low_pfn;
222#endif
223 high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
224
225 totalram_pages += free_all_bootmem();
226 totalram_pages -= setup_zero_pages(); /* Setup zeroed pages. */
227
228 reservedpages = ram = 0;
229 for (tmp = 0; tmp < max_low_pfn; tmp++)
230 if (page_is_ram(tmp)) {
231 ram++;
232 if (PageReserved(mem_map+tmp))
233 reservedpages++;
234 }
235
236#ifdef CONFIG_HIGHMEM
237 for (tmp = highstart_pfn; tmp < highend_pfn; tmp++) {
238 struct page *page = mem_map + tmp;
239
240 if (!page_is_ram(tmp)) {
241 SetPageReserved(page);
242 continue;
243 }
244 ClearPageReserved(page);
245#ifdef CONFIG_LIMITED_DMA
246 set_page_address(page, lowmem_page_address(page));
247#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 set_page_count(page, 1);
249 __free_page(page);
250 totalhigh_pages++;
251 }
252 totalram_pages += totalhigh_pages;
253#endif
254
255 codesize = (unsigned long) &_etext - (unsigned long) &_text;
256 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
257 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
258
Daniel Jacobowitz3d503752005-01-20 19:59:54 -0500259#ifdef CONFIG_64BIT
260 if ((unsigned long) &_text > (unsigned long) CKSEG0)
261 /* The -4 is a hack so that user tools don't have to handle
262 the overflow. */
263 kclist_add(&kcore_kseg0, (void *) CKSEG0, 0x80000000 - 4);
264#endif
265 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
266 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
267 VMALLOC_END-VMALLOC_START);
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
270 "%ldk reserved, %ldk data, %ldk init, %ldk highmem)\n",
271 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
272 ram << (PAGE_SHIFT-10),
273 codesize >> 10,
274 reservedpages << (PAGE_SHIFT-10),
275 datasize >> 10,
276 initsize >> 10,
277 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
278}
Yoichi Yuasab4819b52005-06-25 14:54:31 -0700279#endif /* !CONFIG_NEED_MULTIPLE_NODES */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281#ifdef CONFIG_BLK_DEV_INITRD
282void free_initrd_mem(unsigned long start, unsigned long end)
283{
Ralf Baechle875d43e2005-09-03 15:56:16 -0700284#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* Switch from KSEG0 to XKPHYS addresses */
286 start = (unsigned long)phys_to_virt(CPHYSADDR(start));
287 end = (unsigned long)phys_to_virt(CPHYSADDR(end));
288#endif
289 if (start < end)
290 printk(KERN_INFO "Freeing initrd memory: %ldk freed\n",
291 (end - start) >> 10);
292
293 for (; start < end; start += PAGE_SIZE) {
294 ClearPageReserved(virt_to_page(start));
295 set_page_count(virt_to_page(start), 1);
296 free_page(start);
297 totalram_pages++;
298 }
299}
300#endif
301
302extern unsigned long prom_free_prom_memory(void);
303
304void free_initmem(void)
305{
306 unsigned long addr, page, freed;
307
308 freed = prom_free_prom_memory();
309
310 addr = (unsigned long) &__init_begin;
311 while (addr < (unsigned long) &__init_end) {
Ralf Baechle875d43e2005-09-03 15:56:16 -0700312#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 page = PAGE_OFFSET | CPHYSADDR(addr);
314#else
315 page = addr;
316#endif
317 ClearPageReserved(virt_to_page(page));
318 set_page_count(virt_to_page(page), 1);
319 free_page(page);
320 totalram_pages++;
321 freed += PAGE_SIZE;
322 addr += PAGE_SIZE;
323 }
324 printk(KERN_INFO "Freeing unused kernel memory: %ldk freed\n",
325 freed >> 10);
326}