blob: ad89c442f2998918d52fdac6ad7a98c193fcdfa2 [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>
Dave Hansen22a98352006-03-27 01:16:04 -080028#include <linux/pfn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/bootinfo.h>
31#include <asm/cachectl.h>
32#include <asm/cpu.h>
33#include <asm/dma.h>
34#include <asm/mmu_context.h>
35#include <asm/sections.h>
36#include <asm/pgtable.h>
37#include <asm/pgalloc.h>
38#include <asm/tlb.h>
39
40DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
41
42unsigned long highstart_pfn, highend_pfn;
43
44/*
45 * We have up to 8 empty zeroed pages so we can map one of the right colour
46 * when needed. This is necessary only on R4000 / R4400 SC and MC versions
47 * where we have to avoid VCED / VECI exceptions for good performance at
48 * any price. Since page is never written to after the initialization we
49 * don't have to care about aliases on other CPUs.
50 */
51unsigned long empty_zero_page, zero_page_mask;
52
53/*
54 * Not static inline because used by IP27 special magic initialization code
55 */
56unsigned long setup_zero_pages(void)
57{
Nick Piggin8dfcc9b2006-03-22 00:08:05 -080058 unsigned int order;
59 unsigned long size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct page *page;
61
62 if (cpu_has_vce)
63 order = 3;
64 else
65 order = 0;
66
67 empty_zero_page = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
68 if (!empty_zero_page)
69 panic("Oh boy, that early out of memory?");
70
71 page = virt_to_page(empty_zero_page);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -080072 split_page(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 while (page < virt_to_page(empty_zero_page + (PAGE_SIZE << order))) {
Hugh Dickins68352e62005-12-12 00:37:23 -080074 SetPageReserved(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 page++;
76 }
77
78 size = PAGE_SIZE << order;
79 zero_page_mask = (size - 1) & PAGE_MASK;
80
81 return 1UL << order;
82}
83
84#ifdef CONFIG_HIGHMEM
85pte_t *kmap_pte;
86pgprot_t kmap_prot;
87
88#define kmap_get_fixmap_pte(vaddr) \
Ralf Baechlec6e8b582005-02-10 12:19:59 +000089 pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr)), (vaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91static void __init kmap_init(void)
92{
93 unsigned long kmap_vstart;
94
95 /* cache the first kmap pte */
96 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
97 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
98
99 kmap_prot = PAGE_KERNEL;
100}
101
Ralf Baechle84fd0892005-02-07 16:13:07 +0000102#ifdef CONFIG_32BIT
103void __init fixrange_init(unsigned long start, unsigned long end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 pgd_t *pgd_base)
105{
106 pgd_t *pgd;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000107 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 pmd_t *pmd;
109 pte_t *pte;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000110 int i, j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned long vaddr;
112
113 vaddr = start;
114 i = __pgd_offset(vaddr);
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000115 j = __pud_offset(vaddr);
116 k = __pmd_offset(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 pgd = pgd_base + i;
118
119 for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000120 pud = (pud_t *)pgd;
121 for ( ; (j < PTRS_PER_PUD) && (vaddr != end); pud++, j++) {
122 pmd = (pmd_t *)pud;
123 for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) {
124 if (pmd_none(*pmd)) {
125 pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
126 set_pmd(pmd, __pmd(pte));
127 if (pte != pte_offset_kernel(pmd, 0))
128 BUG();
129 }
130 vaddr += PMD_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000132 k = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134 j = 0;
135 }
136}
Ralf Baechledbc57162005-10-29 19:32:38 +0100137#endif /* CONFIG_32BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#endif /* CONFIG_HIGHMEM */
139
Yoichi Yuasab4819b52005-06-25 14:54:31 -0700140#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141extern void pagetable_init(void);
142
143void __init paging_init(void)
144{
145 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
146 unsigned long max_dma, high, low;
147
148 pagetable_init();
149
150#ifdef CONFIG_HIGHMEM
151 kmap_init();
152#endif
153
154 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
155 low = max_low_pfn;
156 high = highend_pfn;
157
158#ifdef CONFIG_ISA
159 if (low < max_dma)
160 zones_size[ZONE_DMA] = low;
161 else {
162 zones_size[ZONE_DMA] = max_dma;
163 zones_size[ZONE_NORMAL] = low - max_dma;
164 }
165#else
166 zones_size[ZONE_DMA] = low;
167#endif
168#ifdef CONFIG_HIGHMEM
169 if (cpu_has_dc_aliases) {
170 printk(KERN_WARNING "This processor doesn't support highmem.");
171 if (high - low)
172 printk(" %ldk highmem ignored", high - low);
173 printk("\n");
174 } else
175 zones_size[ZONE_HIGHMEM] = high - low;
176#endif
177
178 free_area_init(zones_size);
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static inline int page_is_ram(unsigned long pagenr)
182{
183 int i;
184
185 for (i = 0; i < boot_mem_map.nr_map; i++) {
186 unsigned long addr, end;
187
188 if (boot_mem_map.map[i].type != BOOT_MEM_RAM)
189 /* not usable memory */
190 continue;
191
192 addr = PFN_UP(boot_mem_map.map[i].addr);
193 end = PFN_DOWN(boot_mem_map.map[i].addr +
194 boot_mem_map.map[i].size);
195
196 if (pagenr >= addr && pagenr < end)
197 return 1;
198 }
199
200 return 0;
201}
202
Daniel Jacobowitz3d503752005-01-20 19:59:54 -0500203static struct kcore_list kcore_mem, kcore_vmalloc;
204#ifdef CONFIG_64BIT
205static struct kcore_list kcore_kseg0;
206#endif
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208void __init mem_init(void)
209{
210 unsigned long codesize, reservedpages, datasize, initsize;
211 unsigned long tmp, ram;
212
213#ifdef CONFIG_HIGHMEM
214#ifdef CONFIG_DISCONTIGMEM
215#error "CONFIG_HIGHMEM and CONFIG_DISCONTIGMEM dont work together yet"
216#endif
217 max_mapnr = num_physpages = highend_pfn;
218#else
219 max_mapnr = num_physpages = max_low_pfn;
220#endif
221 high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
222
223 totalram_pages += free_all_bootmem();
224 totalram_pages -= setup_zero_pages(); /* Setup zeroed pages. */
225
226 reservedpages = ram = 0;
227 for (tmp = 0; tmp < max_low_pfn; tmp++)
228 if (page_is_ram(tmp)) {
229 ram++;
230 if (PageReserved(mem_map+tmp))
231 reservedpages++;
232 }
233
234#ifdef CONFIG_HIGHMEM
235 for (tmp = highstart_pfn; tmp < highend_pfn; tmp++) {
236 struct page *page = mem_map + tmp;
237
238 if (!page_is_ram(tmp)) {
239 SetPageReserved(page);
240 continue;
241 }
242 ClearPageReserved(page);
243#ifdef CONFIG_LIMITED_DMA
244 set_page_address(page, lowmem_page_address(page));
245#endif
Nick Piggin7835e982006-03-22 00:08:40 -0800246 init_page_count(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 __free_page(page);
248 totalhigh_pages++;
249 }
250 totalram_pages += totalhigh_pages;
251#endif
252
253 codesize = (unsigned long) &_etext - (unsigned long) &_text;
254 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
255 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
256
Daniel Jacobowitz3d503752005-01-20 19:59:54 -0500257#ifdef CONFIG_64BIT
258 if ((unsigned long) &_text > (unsigned long) CKSEG0)
259 /* The -4 is a hack so that user tools don't have to handle
260 the overflow. */
261 kclist_add(&kcore_kseg0, (void *) CKSEG0, 0x80000000 - 4);
262#endif
263 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
264 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
265 VMALLOC_END-VMALLOC_START);
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
268 "%ldk reserved, %ldk data, %ldk init, %ldk highmem)\n",
269 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
270 ram << (PAGE_SHIFT-10),
271 codesize >> 10,
272 reservedpages << (PAGE_SHIFT-10),
273 datasize >> 10,
274 initsize >> 10,
275 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
276}
Yoichi Yuasab4819b52005-06-25 14:54:31 -0700277#endif /* !CONFIG_NEED_MULTIPLE_NODES */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279#ifdef CONFIG_BLK_DEV_INITRD
280void free_initrd_mem(unsigned long start, unsigned long end)
281{
Ralf Baechle875d43e2005-09-03 15:56:16 -0700282#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* Switch from KSEG0 to XKPHYS addresses */
284 start = (unsigned long)phys_to_virt(CPHYSADDR(start));
285 end = (unsigned long)phys_to_virt(CPHYSADDR(end));
286#endif
287 if (start < end)
288 printk(KERN_INFO "Freeing initrd memory: %ldk freed\n",
289 (end - start) >> 10);
290
291 for (; start < end; start += PAGE_SIZE) {
292 ClearPageReserved(virt_to_page(start));
Nick Piggin7835e982006-03-22 00:08:40 -0800293 init_page_count(virt_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 free_page(start);
295 totalram_pages++;
296 }
297}
298#endif
299
300extern unsigned long prom_free_prom_memory(void);
301
302void free_initmem(void)
303{
304 unsigned long addr, page, freed;
305
306 freed = prom_free_prom_memory();
307
308 addr = (unsigned long) &__init_begin;
309 while (addr < (unsigned long) &__init_end) {
Ralf Baechle875d43e2005-09-03 15:56:16 -0700310#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 page = PAGE_OFFSET | CPHYSADDR(addr);
312#else
313 page = addr;
314#endif
315 ClearPageReserved(virt_to_page(page));
Nick Piggin7835e982006-03-22 00:08:40 -0800316 init_page_count(virt_to_page(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 free_page(page);
318 totalram_pages++;
319 freed += PAGE_SIZE;
320 addr += PAGE_SIZE;
321 }
322 printk(KERN_INFO "Freeing unused kernel memory: %ldk freed\n",
323 freed >> 10);
324}