blob: 5e1967f14255dc1017ddbdcd3cbd460b941db063 [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>
27
28#include <asm/bootinfo.h>
29#include <asm/cachectl.h>
30#include <asm/cpu.h>
31#include <asm/dma.h>
32#include <asm/mmu_context.h>
33#include <asm/sections.h>
34#include <asm/pgtable.h>
35#include <asm/pgalloc.h>
36#include <asm/tlb.h>
37
38DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
39
40unsigned long highstart_pfn, highend_pfn;
41
42/*
43 * We have up to 8 empty zeroed pages so we can map one of the right colour
44 * when needed. This is necessary only on R4000 / R4400 SC and MC versions
45 * where we have to avoid VCED / VECI exceptions for good performance at
46 * any price. Since page is never written to after the initialization we
47 * don't have to care about aliases on other CPUs.
48 */
49unsigned long empty_zero_page, zero_page_mask;
50
51/*
52 * Not static inline because used by IP27 special magic initialization code
53 */
54unsigned long setup_zero_pages(void)
55{
56 unsigned long order, size;
57 struct page *page;
58
59 if (cpu_has_vce)
60 order = 3;
61 else
62 order = 0;
63
64 empty_zero_page = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
65 if (!empty_zero_page)
66 panic("Oh boy, that early out of memory?");
67
68 page = virt_to_page(empty_zero_page);
69 while (page < virt_to_page(empty_zero_page + (PAGE_SIZE << order))) {
70 set_bit(PG_reserved, &page->flags);
71 set_page_count(page, 0);
72 page++;
73 }
74
75 size = PAGE_SIZE << order;
76 zero_page_mask = (size - 1) & PAGE_MASK;
77
78 return 1UL << order;
79}
80
81#ifdef CONFIG_HIGHMEM
82pte_t *kmap_pte;
83pgprot_t kmap_prot;
84
85#define kmap_get_fixmap_pte(vaddr) \
Ralf Baechlec6e8b582005-02-10 12:19:59 +000086 pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr)), (vaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static void __init kmap_init(void)
89{
90 unsigned long kmap_vstart;
91
92 /* cache the first kmap pte */
93 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
94 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
95
96 kmap_prot = PAGE_KERNEL;
97}
98
Ralf Baechle84fd0892005-02-07 16:13:07 +000099#ifdef CONFIG_32BIT
100void __init fixrange_init(unsigned long start, unsigned long end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 pgd_t *pgd_base)
102{
103 pgd_t *pgd;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000104 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 pmd_t *pmd;
106 pte_t *pte;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000107 int i, j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unsigned long vaddr;
109
110 vaddr = start;
111 i = __pgd_offset(vaddr);
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000112 j = __pud_offset(vaddr);
113 k = __pmd_offset(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 pgd = pgd_base + i;
115
116 for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000117 pud = (pud_t *)pgd;
118 for ( ; (j < PTRS_PER_PUD) && (vaddr != end); pud++, j++) {
119 pmd = (pmd_t *)pud;
120 for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) {
121 if (pmd_none(*pmd)) {
122 pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
123 set_pmd(pmd, __pmd(pte));
124 if (pte != pte_offset_kernel(pmd, 0))
125 BUG();
126 }
127 vaddr += PMD_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000129 k = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 j = 0;
132 }
133}
Ralf Baechle875d43e2005-09-03 15:56:16 -0700134#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#endif /* CONFIG_HIGHMEM */
136
Yoichi Yuasab4819b52005-06-25 14:54:31 -0700137#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138extern void pagetable_init(void);
139
140void __init paging_init(void)
141{
142 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
143 unsigned long max_dma, high, low;
144
145 pagetable_init();
146
147#ifdef CONFIG_HIGHMEM
148 kmap_init();
149#endif
150
151 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
152 low = max_low_pfn;
153 high = highend_pfn;
154
155#ifdef CONFIG_ISA
156 if (low < max_dma)
157 zones_size[ZONE_DMA] = low;
158 else {
159 zones_size[ZONE_DMA] = max_dma;
160 zones_size[ZONE_NORMAL] = low - max_dma;
161 }
162#else
163 zones_size[ZONE_DMA] = low;
164#endif
165#ifdef CONFIG_HIGHMEM
166 if (cpu_has_dc_aliases) {
167 printk(KERN_WARNING "This processor doesn't support highmem.");
168 if (high - low)
169 printk(" %ldk highmem ignored", high - low);
170 printk("\n");
171 } else
172 zones_size[ZONE_HIGHMEM] = high - low;
173#endif
174
175 free_area_init(zones_size);
176}
177
178#define PFN_UP(x) (((x) + PAGE_SIZE - 1) >> PAGE_SHIFT)
179#define PFN_DOWN(x) ((x) >> PAGE_SHIFT)
180
181static 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
203void __init mem_init(void)
204{
205 unsigned long codesize, reservedpages, datasize, initsize;
206 unsigned long tmp, ram;
207
208#ifdef CONFIG_HIGHMEM
209#ifdef CONFIG_DISCONTIGMEM
210#error "CONFIG_HIGHMEM and CONFIG_DISCONTIGMEM dont work together yet"
211#endif
212 max_mapnr = num_physpages = highend_pfn;
213#else
214 max_mapnr = num_physpages = max_low_pfn;
215#endif
216 high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
217
218 totalram_pages += free_all_bootmem();
219 totalram_pages -= setup_zero_pages(); /* Setup zeroed pages. */
220
221 reservedpages = ram = 0;
222 for (tmp = 0; tmp < max_low_pfn; tmp++)
223 if (page_is_ram(tmp)) {
224 ram++;
225 if (PageReserved(mem_map+tmp))
226 reservedpages++;
227 }
228
229#ifdef CONFIG_HIGHMEM
230 for (tmp = highstart_pfn; tmp < highend_pfn; tmp++) {
231 struct page *page = mem_map + tmp;
232
233 if (!page_is_ram(tmp)) {
234 SetPageReserved(page);
235 continue;
236 }
237 ClearPageReserved(page);
238#ifdef CONFIG_LIMITED_DMA
239 set_page_address(page, lowmem_page_address(page));
240#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 set_page_count(page, 1);
242 __free_page(page);
243 totalhigh_pages++;
244 }
245 totalram_pages += totalhigh_pages;
246#endif
247
248 codesize = (unsigned long) &_etext - (unsigned long) &_text;
249 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
250 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
251
252 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
253 "%ldk reserved, %ldk data, %ldk init, %ldk highmem)\n",
254 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
255 ram << (PAGE_SHIFT-10),
256 codesize >> 10,
257 reservedpages << (PAGE_SHIFT-10),
258 datasize >> 10,
259 initsize >> 10,
260 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
261}
Yoichi Yuasab4819b52005-06-25 14:54:31 -0700262#endif /* !CONFIG_NEED_MULTIPLE_NODES */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264#ifdef CONFIG_BLK_DEV_INITRD
265void free_initrd_mem(unsigned long start, unsigned long end)
266{
Ralf Baechle875d43e2005-09-03 15:56:16 -0700267#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* Switch from KSEG0 to XKPHYS addresses */
269 start = (unsigned long)phys_to_virt(CPHYSADDR(start));
270 end = (unsigned long)phys_to_virt(CPHYSADDR(end));
271#endif
272 if (start < end)
273 printk(KERN_INFO "Freeing initrd memory: %ldk freed\n",
274 (end - start) >> 10);
275
276 for (; start < end; start += PAGE_SIZE) {
277 ClearPageReserved(virt_to_page(start));
278 set_page_count(virt_to_page(start), 1);
279 free_page(start);
280 totalram_pages++;
281 }
282}
283#endif
284
285extern unsigned long prom_free_prom_memory(void);
286
287void free_initmem(void)
288{
289 unsigned long addr, page, freed;
290
291 freed = prom_free_prom_memory();
292
293 addr = (unsigned long) &__init_begin;
294 while (addr < (unsigned long) &__init_end) {
Ralf Baechle875d43e2005-09-03 15:56:16 -0700295#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 page = PAGE_OFFSET | CPHYSADDR(addr);
297#else
298 page = addr;
299#endif
300 ClearPageReserved(virt_to_page(page));
301 set_page_count(virt_to_page(page), 1);
302 free_page(page);
303 totalram_pages++;
304 freed += PAGE_SIZE;
305 addr += PAGE_SIZE;
306 }
307 printk(KERN_INFO "Freeing unused kernel memory: %ldk freed\n",
308 freed >> 10);
309}