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
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.
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>
24 #include <linux/bootmem.h>
25 #include <linux/highmem.h>
26 #include <linux/swap.h>
27 #include <linux/proc_fs.h>
29 #include <asm/bootinfo.h>
30 #include <asm/cachectl.h>
33 #include <asm/mmu_context.h>
34 #include <asm/sections.h>
35 #include <asm/pgtable.h>
36 #include <asm/pgalloc.h>
39 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
41 unsigned long highstart_pfn, highend_pfn;
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.
50 unsigned long empty_zero_page, zero_page_mask;
53 * Not static inline because used by IP27 special magic initialization code
55 unsigned long setup_zero_pages(void)
57 unsigned long order, size;
65 empty_zero_page = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
67 panic("Oh boy, that early out of memory?");
69 page = virt_to_page(empty_zero_page);
70 while (page < virt_to_page(empty_zero_page + (PAGE_SIZE << order))) {
71 SetPageReserved(page);
72 set_page_count(page, 1);
76 size = PAGE_SIZE << order;
77 zero_page_mask = (size - 1) & PAGE_MASK;
86 #define kmap_get_fixmap_pte(vaddr) \
87 pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr)), (vaddr))
89 static void __init kmap_init(void)
91 unsigned long kmap_vstart;
93 /* cache the first kmap pte */
94 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
95 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
97 kmap_prot = PAGE_KERNEL;
101 void __init fixrange_init(unsigned long start, unsigned long end,
112 i = __pgd_offset(vaddr);
113 j = __pud_offset(vaddr);
114 k = __pmd_offset(vaddr);
117 for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
119 for ( ; (j < PTRS_PER_PUD) && (vaddr != end); pud++, j++) {
121 for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) {
122 if (pmd_none(*pmd)) {
123 pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
124 set_pmd(pmd, __pmd(pte));
125 if (pte != pte_offset_kernel(pmd, 0))
135 #endif /* CONFIG_32BIT */
136 #endif /* CONFIG_HIGHMEM */
138 #ifndef CONFIG_NEED_MULTIPLE_NODES
139 extern void pagetable_init(void);
141 void __init paging_init(void)
143 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
144 unsigned long max_dma, high, low;
148 #ifdef CONFIG_HIGHMEM
152 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
158 zones_size[ZONE_DMA] = low;
160 zones_size[ZONE_DMA] = max_dma;
161 zones_size[ZONE_NORMAL] = low - max_dma;
164 zones_size[ZONE_DMA] = low;
166 #ifdef CONFIG_HIGHMEM
167 if (cpu_has_dc_aliases) {
168 printk(KERN_WARNING "This processor doesn't support highmem.");
170 printk(" %ldk highmem ignored", high - low);
173 zones_size[ZONE_HIGHMEM] = high - low;
176 free_area_init(zones_size);
179 #define PFN_UP(x) (((x) + PAGE_SIZE - 1) >> PAGE_SHIFT)
180 #define PFN_DOWN(x) ((x) >> PAGE_SHIFT)
182 static inline int page_is_ram(unsigned long pagenr)
186 for (i = 0; i < boot_mem_map.nr_map; i++) {
187 unsigned long addr, end;
189 if (boot_mem_map.map[i].type != BOOT_MEM_RAM)
190 /* not usable memory */
193 addr = PFN_UP(boot_mem_map.map[i].addr);
194 end = PFN_DOWN(boot_mem_map.map[i].addr +
195 boot_mem_map.map[i].size);
197 if (pagenr >= addr && pagenr < end)
204 static struct kcore_list kcore_mem, kcore_vmalloc;
206 static struct kcore_list kcore_kseg0;
209 void __init mem_init(void)
211 unsigned long codesize, reservedpages, datasize, initsize;
212 unsigned long tmp, ram;
214 #ifdef CONFIG_HIGHMEM
215 #ifdef CONFIG_DISCONTIGMEM
216 #error "CONFIG_HIGHMEM and CONFIG_DISCONTIGMEM dont work together yet"
218 max_mapnr = num_physpages = highend_pfn;
220 max_mapnr = num_physpages = max_low_pfn;
222 high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
224 totalram_pages += free_all_bootmem();
225 totalram_pages -= setup_zero_pages(); /* Setup zeroed pages. */
227 reservedpages = ram = 0;
228 for (tmp = 0; tmp < max_low_pfn; tmp++)
229 if (page_is_ram(tmp)) {
231 if (PageReserved(mem_map+tmp))
235 #ifdef CONFIG_HIGHMEM
236 for (tmp = highstart_pfn; tmp < highend_pfn; tmp++) {
237 struct page *page = mem_map + tmp;
239 if (!page_is_ram(tmp)) {
240 SetPageReserved(page);
243 ClearPageReserved(page);
244 #ifdef CONFIG_LIMITED_DMA
245 set_page_address(page, lowmem_page_address(page));
247 set_page_count(page, 1);
251 totalram_pages += totalhigh_pages;
254 codesize = (unsigned long) &_etext - (unsigned long) &_text;
255 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
256 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
259 if ((unsigned long) &_text > (unsigned long) CKSEG0)
260 /* The -4 is a hack so that user tools don't have to handle
262 kclist_add(&kcore_kseg0, (void *) CKSEG0, 0x80000000 - 4);
264 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
265 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
266 VMALLOC_END-VMALLOC_START);
268 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
269 "%ldk reserved, %ldk data, %ldk init, %ldk highmem)\n",
270 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
271 ram << (PAGE_SHIFT-10),
273 reservedpages << (PAGE_SHIFT-10),
276 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
278 #endif /* !CONFIG_NEED_MULTIPLE_NODES */
280 #ifdef CONFIG_BLK_DEV_INITRD
281 void free_initrd_mem(unsigned long start, unsigned long end)
284 /* Switch from KSEG0 to XKPHYS addresses */
285 start = (unsigned long)phys_to_virt(CPHYSADDR(start));
286 end = (unsigned long)phys_to_virt(CPHYSADDR(end));
289 printk(KERN_INFO "Freeing initrd memory: %ldk freed\n",
290 (end - start) >> 10);
292 for (; start < end; start += PAGE_SIZE) {
293 ClearPageReserved(virt_to_page(start));
294 set_page_count(virt_to_page(start), 1);
301 extern unsigned long prom_free_prom_memory(void);
303 void free_initmem(void)
305 unsigned long addr, page, freed;
307 freed = prom_free_prom_memory();
309 addr = (unsigned long) &__init_begin;
310 while (addr < (unsigned long) &__init_end) {
312 page = PAGE_OFFSET | CPHYSADDR(addr);
316 ClearPageReserved(virt_to_page(page));
317 set_page_count(virt_to_page(page), 1);
323 printk(KERN_INFO "Freeing unused kernel memory: %ldk freed\n",